]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/scsi/zfcp_fsf.c
scsi: zfcp: add handling for FCP_RESID_OVER to the fcp ingress path
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / scsi / zfcp_fsf.c
CommitLineData
1da177e4 1/*
553448f6 2 * zfcp device driver
1da177e4 3 *
553448f6 4 * Implementation of FSF commands.
1da177e4 5 *
bd77befa 6 * Copyright IBM Corp. 2002, 2015
1da177e4
LT
7 */
8
ecf39d42
CS
9#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
0997f1c5 12#include <linux/blktrace_api.h>
5a0e3ad6 13#include <linux/slab.h>
9d05ce2c 14#include <scsi/fc/fc_els.h>
1da177e4 15#include "zfcp_ext.h"
4318e08c 16#include "zfcp_fc.h"
dcd20e23 17#include "zfcp_dbf.h"
34c2b712 18#include "zfcp_qdio.h"
b6bd2fb9 19#include "zfcp_reqlist.h"
1da177e4 20
259afe2e
CS
21struct kmem_cache *zfcp_fsf_qtcb_cache;
22
287ac01a
CS
23static void zfcp_fsf_request_timeout_handler(unsigned long data)
24{
25 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
339f4f4e 26 zfcp_qdio_siosl(adapter);
5ffd51a5 27 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 28 "fsrth_1");
287ac01a
CS
29}
30
31static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
32 unsigned long timeout)
33{
34 fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
35 fsf_req->timer.data = (unsigned long) fsf_req->adapter;
36 fsf_req->timer.expires = jiffies + timeout;
37 add_timer(&fsf_req->timer);
38}
39
40static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
41{
42 BUG_ON(!fsf_req->erp_action);
43 fsf_req->timer.function = zfcp_erp_timeout_handler;
44 fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
45 fsf_req->timer.expires = jiffies + 30 * HZ;
46 add_timer(&fsf_req->timer);
47}
48
1da177e4
LT
49/* association between FSF command and FSF QTCB type */
50static u32 fsf_qtcb_type[] = {
51 [FSF_QTCB_FCP_CMND] = FSF_IO_COMMAND,
52 [FSF_QTCB_ABORT_FCP_CMND] = FSF_SUPPORT_COMMAND,
53 [FSF_QTCB_OPEN_PORT_WITH_DID] = FSF_SUPPORT_COMMAND,
54 [FSF_QTCB_OPEN_LUN] = FSF_SUPPORT_COMMAND,
55 [FSF_QTCB_CLOSE_LUN] = FSF_SUPPORT_COMMAND,
56 [FSF_QTCB_CLOSE_PORT] = FSF_SUPPORT_COMMAND,
57 [FSF_QTCB_CLOSE_PHYSICAL_PORT] = FSF_SUPPORT_COMMAND,
58 [FSF_QTCB_SEND_ELS] = FSF_SUPPORT_COMMAND,
59 [FSF_QTCB_SEND_GENERIC] = FSF_SUPPORT_COMMAND,
60 [FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
61 [FSF_QTCB_EXCHANGE_PORT_DATA] = FSF_PORT_COMMAND,
62 [FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
63 [FSF_QTCB_UPLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND
64};
65
553448f6
CS
66static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
67{
ff3b24fa
CS
68 dev_err(&req->adapter->ccw_device->dev, "FCP device not "
69 "operational because of an unsupported FC class\n");
ea4a3a6a 70 zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1");
553448f6
CS
71 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
72}
73
c41f8cbd
SS
74/**
75 * zfcp_fsf_req_free - free memory used by fsf request
76 * @fsf_req: pointer to struct zfcp_fsf_req
1da177e4 77 */
c41f8cbd 78void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
1da177e4 79{
c41f8cbd 80 if (likely(req->pool)) {
a4623c46
SS
81 if (likely(req->qtcb))
82 mempool_free(req->qtcb, req->adapter->pool.qtcb_pool);
c41f8cbd 83 mempool_free(req, req->pool);
dd52e0ea
HC
84 return;
85 }
86
a4623c46 87 if (likely(req->qtcb))
259afe2e 88 kmem_cache_free(zfcp_fsf_qtcb_cache, req->qtcb);
a4623c46 89 kfree(req);
1da177e4
LT
90}
91
c41f8cbd 92static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
1da177e4 93{
ecf0c772 94 unsigned long flags;
c41f8cbd
SS
95 struct fsf_status_read_buffer *sr_buf = req->data;
96 struct zfcp_adapter *adapter = req->adapter;
97 struct zfcp_port *port;
800c0cad 98 int d_id = ntoh24(sr_buf->d_id);
1da177e4 99
ecf0c772
SS
100 read_lock_irqsave(&adapter->port_list_lock, flags);
101 list_for_each_entry(port, &adapter->port_list, list)
c41f8cbd 102 if (port->d_id == d_id) {
ea4a3a6a 103 zfcp_erp_port_reopen(port, 0, "fssrpc1");
ecf0c772 104 break;
c41f8cbd 105 }
ecf0c772 106 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1da177e4
LT
107}
108
edaed859 109static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req,
c41f8cbd 110 struct fsf_link_down_info *link_down)
aef4a983 111{
c41f8cbd 112 struct zfcp_adapter *adapter = req->adapter;
698ec016 113
c41f8cbd 114 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
ee69ab7a
MS
115 return;
116
805de8f4 117 atomic_or(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
70932935 118
a2fa0aed 119 zfcp_scsi_schedule_rports_block(adapter);
ee69ab7a 120
c41f8cbd 121 if (!link_down)
2f8f3ed5 122 goto out;
ee69ab7a 123
aef4a983
MS
124 switch (link_down->error_code) {
125 case FSF_PSQ_LINK_NO_LIGHT:
c41f8cbd 126 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
127 "There is no light signal from the local "
128 "fibre channel cable\n");
aef4a983
MS
129 break;
130 case FSF_PSQ_LINK_WRAP_PLUG:
c41f8cbd 131 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
132 "There is a wrap plug instead of a fibre "
133 "channel cable\n");
aef4a983
MS
134 break;
135 case FSF_PSQ_LINK_NO_FCP:
c41f8cbd 136 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
137 "The adjacent fibre channel node does not "
138 "support FCP\n");
aef4a983
MS
139 break;
140 case FSF_PSQ_LINK_FIRMWARE_UPDATE:
c41f8cbd 141 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
142 "The FCP device is suspended because of a "
143 "firmware update\n");
553448f6 144 break;
aef4a983 145 case FSF_PSQ_LINK_INVALID_WWPN:
c41f8cbd 146 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
147 "The FCP device detected a WWPN that is "
148 "duplicate or not valid\n");
aef4a983
MS
149 break;
150 case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
c41f8cbd 151 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 152 "The fibre channel fabric does not support NPIV\n");
aef4a983
MS
153 break;
154 case FSF_PSQ_LINK_NO_FCP_RESOURCES:
c41f8cbd 155 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 156 "The FCP adapter cannot support more NPIV ports\n");
aef4a983
MS
157 break;
158 case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
c41f8cbd 159 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
160 "The adjacent switch cannot support "
161 "more NPIV ports\n");
aef4a983
MS
162 break;
163 case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
c41f8cbd 164 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
165 "The FCP adapter could not log in to the "
166 "fibre channel fabric\n");
aef4a983
MS
167 break;
168 case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
c41f8cbd 169 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
170 "The WWPN assignment file on the FCP adapter "
171 "has been damaged\n");
aef4a983
MS
172 break;
173 case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
c41f8cbd 174 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
175 "The mode table on the FCP adapter "
176 "has been damaged\n");
aef4a983
MS
177 break;
178 case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
c41f8cbd 179 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
180 "All NPIV ports on the FCP adapter have "
181 "been assigned\n");
aef4a983
MS
182 break;
183 default:
c41f8cbd 184 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
185 "The link between the FCP adapter and "
186 "the FC fabric is down\n");
aef4a983 187 }
c41f8cbd 188out:
edaed859 189 zfcp_erp_set_adapter_status(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
aef4a983
MS
190}
191
c41f8cbd 192static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
1da177e4 193{
c41f8cbd
SS
194 struct fsf_status_read_buffer *sr_buf = req->data;
195 struct fsf_link_down_info *ldi =
196 (struct fsf_link_down_info *) &sr_buf->payload;
1da177e4 197
c41f8cbd
SS
198 switch (sr_buf->status_subtype) {
199 case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
edaed859 200 zfcp_fsf_link_down_info_eval(req, ldi);
1da177e4 201 break;
c41f8cbd 202 case FSF_STATUS_READ_SUB_FDISC_FAILED:
edaed859 203 zfcp_fsf_link_down_info_eval(req, ldi);
1da177e4 204 break;
c41f8cbd 205 case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
edaed859 206 zfcp_fsf_link_down_info_eval(req, NULL);
3b974874 207 }
c41f8cbd 208}
1da177e4 209
c41f8cbd
SS
210static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
211{
212 struct zfcp_adapter *adapter = req->adapter;
213 struct fsf_status_read_buffer *sr_buf = req->data;
1da177e4 214
c41f8cbd 215 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
a54ca0f6 216 zfcp_dbf_hba_fsf_uss("fssrh_1", req);
c7b279ae 217 mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
c41f8cbd
SS
218 zfcp_fsf_req_free(req);
219 return;
220 }
1da177e4 221
0100998d 222 zfcp_dbf_hba_fsf_uss("fssrh_4", req);
1da177e4 223
c41f8cbd
SS
224 switch (sr_buf->status_type) {
225 case FSF_STATUS_READ_PORT_CLOSED:
226 zfcp_fsf_status_read_port_closed(req);
1da177e4 227 break;
c41f8cbd
SS
228 case FSF_STATUS_READ_INCOMING_ELS:
229 zfcp_fc_incoming_els(req);
1da177e4 230 break;
c41f8cbd 231 case FSF_STATUS_READ_SENSE_DATA_AVAIL:
1da177e4 232 break;
c41f8cbd 233 case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
ff3b24fa
CS
234 dev_warn(&adapter->ccw_device->dev,
235 "The error threshold for checksum statistics "
236 "has been exceeded\n");
a54ca0f6 237 zfcp_dbf_hba_bit_err("fssrh_3", req);
1da177e4 238 break;
c41f8cbd
SS
239 case FSF_STATUS_READ_LINK_DOWN:
240 zfcp_fsf_status_read_link_down(req);
2d1e547f 241 zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKDOWN, 0);
c41f8cbd
SS
242 break;
243 case FSF_STATUS_READ_LINK_UP:
244 dev_info(&adapter->ccw_device->dev,
ff3b24fa 245 "The local link has been restored\n");
c41f8cbd 246 /* All ports should be marked as ready to run again */
edaed859
SS
247 zfcp_erp_set_adapter_status(adapter,
248 ZFCP_STATUS_COMMON_RUNNING);
c41f8cbd
SS
249 zfcp_erp_adapter_reopen(adapter,
250 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
251 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 252 "fssrh_2");
2d1e547f
SS
253 zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKUP, 0);
254
c41f8cbd
SS
255 break;
256 case FSF_STATUS_READ_NOTIFICATION_LOST:
c41f8cbd 257 if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
43f60cbd 258 zfcp_fc_conditional_port_scan(adapter);
c41f8cbd 259 break;
c41f8cbd
SS
260 case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
261 adapter->adapter_features = sr_buf->payload.word[0];
1da177e4 262 break;
1da177e4
LT
263 }
264
c7b279ae 265 mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
c41f8cbd 266 zfcp_fsf_req_free(req);
1da177e4 267
c41f8cbd 268 atomic_inc(&adapter->stat_miss);
4544683a 269 queue_work(adapter->work_queue, &adapter->stat_work);
c41f8cbd 270}
1da177e4 271
c41f8cbd
SS
272static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
273{
274 switch (req->qtcb->header.fsf_status_qual.word[0]) {
275 case FSF_SQ_FCP_RSP_AVAILABLE:
276 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
277 case FSF_SQ_NO_RETRY_POSSIBLE:
278 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
279 return;
280 case FSF_SQ_COMMAND_ABORTED:
c41f8cbd
SS
281 break;
282 case FSF_SQ_NO_RECOM:
283 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa
CS
284 "The FCP adapter reported a problem "
285 "that cannot be recovered\n");
339f4f4e 286 zfcp_qdio_siosl(req->adapter);
ea4a3a6a 287 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1");
c41f8cbd
SS
288 break;
289 }
290 /* all non-return stats set FSFREQ_ERROR*/
291 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
292}
293
c41f8cbd 294static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
1da177e4 295{
c41f8cbd
SS
296 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
297 return;
1da177e4 298
c41f8cbd
SS
299 switch (req->qtcb->header.fsf_status) {
300 case FSF_UNKNOWN_COMMAND:
301 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa 302 "The FCP adapter does not recognize the command 0x%x\n",
c41f8cbd 303 req->qtcb->header.fsf_command);
ea4a3a6a 304 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1");
c41f8cbd
SS
305 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
306 break;
307 case FSF_ADAPTER_STATUS_AVAILABLE:
308 zfcp_fsf_fsfstatus_qual_eval(req);
309 break;
310 }
311}
1da177e4 312
c41f8cbd
SS
313static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
314{
315 struct zfcp_adapter *adapter = req->adapter;
316 struct fsf_qtcb *qtcb = req->qtcb;
317 union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
1da177e4 318
5771710b 319 zfcp_dbf_hba_fsf_response(req);
1da177e4 320
c41f8cbd 321 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
4c571c65 322 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
323 return;
324 }
1da177e4 325
c41f8cbd
SS
326 switch (qtcb->prefix.prot_status) {
327 case FSF_PROT_GOOD:
328 case FSF_PROT_FSF_STATUS_PRESENTED:
329 return;
330 case FSF_PROT_QTCB_VERSION_ERROR:
331 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
332 "QTCB version 0x%x not supported by FCP adapter "
333 "(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
334 psq->word[0], psq->word[1]);
ea4a3a6a 335 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1");
c41f8cbd
SS
336 break;
337 case FSF_PROT_ERROR_STATE:
338 case FSF_PROT_SEQ_NUMB_ERROR:
ea4a3a6a 339 zfcp_erp_adapter_reopen(adapter, 0, "fspse_2");
4c571c65 340 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
341 break;
342 case FSF_PROT_UNSUPP_QTCB_TYPE:
343 dev_err(&adapter->ccw_device->dev,
ff3b24fa 344 "The QTCB type is not supported by the FCP adapter\n");
ea4a3a6a 345 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3");
c41f8cbd
SS
346 break;
347 case FSF_PROT_HOST_CONNECTION_INITIALIZING:
805de8f4 348 atomic_or(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
c41f8cbd
SS
349 &adapter->status);
350 break;
351 case FSF_PROT_DUPLICATE_REQUEST_ID:
352 dev_err(&adapter->ccw_device->dev,
ff3b24fa 353 "0x%Lx is an ambiguous request identifier\n",
c41f8cbd 354 (unsigned long long)qtcb->bottom.support.req_handle);
ea4a3a6a 355 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4");
c41f8cbd
SS
356 break;
357 case FSF_PROT_LINK_DOWN:
edaed859 358 zfcp_fsf_link_down_info_eval(req, &psq->link_down_info);
452b505c 359 /* go through reopen to flush pending requests */
ea4a3a6a 360 zfcp_erp_adapter_reopen(adapter, 0, "fspse_6");
c41f8cbd
SS
361 break;
362 case FSF_PROT_REEST_QUEUE:
363 /* All ports should be marked as ready to run again */
edaed859
SS
364 zfcp_erp_set_adapter_status(adapter,
365 ZFCP_STATUS_COMMON_RUNNING);
c41f8cbd
SS
366 zfcp_erp_adapter_reopen(adapter,
367 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
5ffd51a5 368 ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 369 "fspse_8");
c41f8cbd
SS
370 break;
371 default:
372 dev_err(&adapter->ccw_device->dev,
ff3b24fa 373 "0x%x is not a valid transfer protocol status\n",
c41f8cbd 374 qtcb->prefix.prot_status);
339f4f4e 375 zfcp_qdio_siosl(adapter);
ea4a3a6a 376 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9");
c41f8cbd
SS
377 }
378 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
379}
380
c41f8cbd
SS
381/**
382 * zfcp_fsf_req_complete - process completion of a FSF request
383 * @fsf_req: The FSF request that has been completed.
384 *
385 * When a request has been completed either from the FCP adapter,
386 * or it has been dismissed due to a queue shutdown, this function
387 * is called to process the completion status and trigger further
388 * events related to the FSF request.
389 */
bd63eaf4 390static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
1da177e4 391{
c41f8cbd
SS
392 if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
393 zfcp_fsf_status_read_handler(req);
394 return;
395 }
1da177e4 396
c41f8cbd
SS
397 del_timer(&req->timer);
398 zfcp_fsf_protstatus_eval(req);
399 zfcp_fsf_fsfstatus_eval(req);
400 req->handler(req);
1da177e4 401
c41f8cbd 402 if (req->erp_action)
287ac01a 403 zfcp_erp_notify(req->erp_action, 0);
1da177e4 404
c41f8cbd
SS
405 if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
406 zfcp_fsf_req_free(req);
407 else
058b8647 408 complete(&req->completion);
c41f8cbd 409}
1da177e4 410
bd63eaf4
SS
411/**
412 * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
413 * @adapter: pointer to struct zfcp_adapter
414 *
415 * Never ever call this without shutting down the adapter first.
416 * Otherwise the adapter would continue using and corrupting s390 storage.
417 * Included BUG_ON() call to ensure this is done.
418 * ERP is supposed to be the only user of this function.
419 */
420void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
421{
422 struct zfcp_fsf_req *req, *tmp;
bd63eaf4 423 LIST_HEAD(remove_queue);
bd63eaf4
SS
424
425 BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
b6bd2fb9 426 zfcp_reqlist_move(adapter->req_list, &remove_queue);
bd63eaf4
SS
427
428 list_for_each_entry_safe(req, tmp, &remove_queue, list) {
429 list_del(&req->list);
430 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
431 zfcp_fsf_req_complete(req);
432 }
433}
434
d2201977
SM
435#define ZFCP_FSF_PORTSPEED_1GBIT (1 << 0)
436#define ZFCP_FSF_PORTSPEED_2GBIT (1 << 1)
437#define ZFCP_FSF_PORTSPEED_4GBIT (1 << 2)
438#define ZFCP_FSF_PORTSPEED_10GBIT (1 << 3)
439#define ZFCP_FSF_PORTSPEED_8GBIT (1 << 4)
440#define ZFCP_FSF_PORTSPEED_16GBIT (1 << 5)
441#define ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED (1 << 15)
442
443static u32 zfcp_fsf_convert_portspeed(u32 fsf_speed)
444{
445 u32 fdmi_speed = 0;
446 if (fsf_speed & ZFCP_FSF_PORTSPEED_1GBIT)
447 fdmi_speed |= FC_PORTSPEED_1GBIT;
448 if (fsf_speed & ZFCP_FSF_PORTSPEED_2GBIT)
449 fdmi_speed |= FC_PORTSPEED_2GBIT;
450 if (fsf_speed & ZFCP_FSF_PORTSPEED_4GBIT)
451 fdmi_speed |= FC_PORTSPEED_4GBIT;
452 if (fsf_speed & ZFCP_FSF_PORTSPEED_10GBIT)
453 fdmi_speed |= FC_PORTSPEED_10GBIT;
454 if (fsf_speed & ZFCP_FSF_PORTSPEED_8GBIT)
455 fdmi_speed |= FC_PORTSPEED_8GBIT;
456 if (fsf_speed & ZFCP_FSF_PORTSPEED_16GBIT)
457 fdmi_speed |= FC_PORTSPEED_16GBIT;
458 if (fsf_speed & ZFCP_FSF_PORTSPEED_NOT_NEGOTIATED)
459 fdmi_speed |= FC_PORTSPEED_NOT_NEGOTIATED;
460 return fdmi_speed;
461}
462
c41f8cbd
SS
463static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
464{
9d05ce2c 465 struct fsf_qtcb_bottom_config *bottom = &req->qtcb->bottom.config;
c41f8cbd
SS
466 struct zfcp_adapter *adapter = req->adapter;
467 struct Scsi_Host *shost = adapter->scsi_host;
9d05ce2c 468 struct fc_els_flogi *nsp, *plogi;
1da177e4 469
9d05ce2c
CS
470 /* adjust pointers for missing command code */
471 nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
472 - sizeof(u32));
473 plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
474 - sizeof(u32));
1da177e4 475
c41f8cbd
SS
476 if (req->data)
477 memcpy(req->data, bottom, sizeof(*bottom));
478
9d05ce2c
CS
479 fc_host_port_name(shost) = nsp->fl_wwpn;
480 fc_host_node_name(shost) = nsp->fl_wwnn;
c41f8cbd
SS
481 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
482
faf4cd85 483 adapter->timer_ticks = bottom->timer_interval & ZFCP_FSF_TIMER_INT_MASK;
8d88cf3f
CS
484 adapter->stat_read_buf_num = max(bottom->status_read_buf_num,
485 (u16)FSF_STATUS_READS_RECOM);
c41f8cbd
SS
486
487 if (fc_host_permanent_port_name(shost) == -1)
488 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
489
9edf7d75
SM
490 zfcp_scsi_set_prot(adapter);
491
492 /* no error return above here, otherwise must fix call chains */
493 /* do not evaluate invalid fields */
494 if (req->qtcb->header.fsf_status == FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE)
495 return 0;
496
497 fc_host_port_id(shost) = ntoh24(bottom->s_id);
498 fc_host_speed(shost) =
499 zfcp_fsf_convert_portspeed(bottom->fc_link_speed);
500
501 adapter->hydra_version = bottom->adapter_type;
502
c41f8cbd
SS
503 switch (bottom->fc_topology) {
504 case FSF_TOPO_P2P:
800c0cad 505 adapter->peer_d_id = ntoh24(bottom->peer_d_id);
9d05ce2c
CS
506 adapter->peer_wwpn = plogi->fl_wwpn;
507 adapter->peer_wwnn = plogi->fl_wwnn;
c41f8cbd 508 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
c41f8cbd
SS
509 break;
510 case FSF_TOPO_FABRIC:
bd77befa
SM
511 if (bottom->connection_features & FSF_FEATURE_NPIV_MODE)
512 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
513 else
514 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
c41f8cbd
SS
515 break;
516 case FSF_TOPO_AL:
517 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
dceab655 518 /* fall through */
c41f8cbd 519 default:
c41f8cbd 520 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
521 "Unknown or unsupported arbitrated loop "
522 "fibre channel topology detected\n");
ea4a3a6a 523 zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1");
c41f8cbd 524 return -EIO;
1da177e4 525 }
c41f8cbd 526
1da177e4
LT
527 return 0;
528}
529
c41f8cbd 530static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
553448f6
CS
531{
532 struct zfcp_adapter *adapter = req->adapter;
c41f8cbd
SS
533 struct fsf_qtcb *qtcb = req->qtcb;
534 struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
535 struct Scsi_Host *shost = adapter->scsi_host;
553448f6 536
c41f8cbd
SS
537 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
538 return;
1da177e4 539
c41f8cbd
SS
540 adapter->fsf_lic_version = bottom->lic_version;
541 adapter->adapter_features = bottom->adapter_features;
542 adapter->connection_features = bottom->connection_features;
543 adapter->peer_wwpn = 0;
544 adapter->peer_wwnn = 0;
545 adapter->peer_d_id = 0;
8a36e453 546
c41f8cbd
SS
547 switch (qtcb->header.fsf_status) {
548 case FSF_GOOD:
549 if (zfcp_fsf_exchange_config_evaluate(req))
550 return;
1da177e4 551
c41f8cbd
SS
552 if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
553 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
554 "FCP adapter maximum QTCB size (%d bytes) "
555 "is too small\n",
556 bottom->max_qtcb_size);
ea4a3a6a 557 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1");
c41f8cbd
SS
558 return;
559 }
805de8f4 560 atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
c41f8cbd 561 &adapter->status);
1da177e4 562 break;
c41f8cbd
SS
563 case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
564 fc_host_node_name(shost) = 0;
565 fc_host_port_name(shost) = 0;
566 fc_host_port_id(shost) = 0;
567 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
568 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
569 adapter->hydra_version = 0;
1da177e4 570
f76ccaac
DH
571 /* avoids adapter shutdown to be able to recognize
572 * events such as LINK UP */
805de8f4 573 atomic_or(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
f76ccaac 574 &adapter->status);
edaed859 575 zfcp_fsf_link_down_info_eval(req,
c41f8cbd 576 &qtcb->header.fsf_status_qual.link_down_info);
9edf7d75
SM
577 if (zfcp_fsf_exchange_config_evaluate(req))
578 return;
1da177e4 579 break;
c41f8cbd 580 default:
ea4a3a6a 581 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3");
c41f8cbd
SS
582 return;
583 }
1da177e4 584
c41f8cbd
SS
585 if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
586 adapter->hardware_version = bottom->hardware_version;
587 memcpy(fc_host_serial_number(shost), bottom->serial_number,
588 min(FC_SERIAL_NUMBER_SIZE, 17));
589 EBCASC(fc_host_serial_number(shost),
590 min(FC_SERIAL_NUMBER_SIZE, 17));
591 }
1da177e4 592
c41f8cbd
SS
593 if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
594 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
595 "The FCP adapter only supports newer "
596 "control block versions\n");
ea4a3a6a 597 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4");
c41f8cbd
SS
598 return;
599 }
600 if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
601 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
602 "The FCP adapter only supports older "
603 "control block versions\n");
ea4a3a6a 604 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5");
c41f8cbd
SS
605 }
606}
1da177e4 607
c41f8cbd
SS
608static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
609{
610 struct zfcp_adapter *adapter = req->adapter;
611 struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
612 struct Scsi_Host *shost = adapter->scsi_host;
1da177e4 613
c41f8cbd
SS
614 if (req->data)
615 memcpy(req->data, bottom, sizeof(*bottom));
9eb69aff 616
0282985d 617 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
c41f8cbd 618 fc_host_permanent_port_name(shost) = bottom->wwpn;
0282985d 619 } else
c41f8cbd
SS
620 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
621 fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
d2201977
SM
622 fc_host_supported_speeds(shost) =
623 zfcp_fsf_convert_portspeed(bottom->supported_speed);
2d8e62bb
CS
624 memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
625 FC_FC4_LIST_SIZE);
626 memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
627 FC_FC4_LIST_SIZE);
c41f8cbd 628}
1da177e4 629
c41f8cbd
SS
630static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
631{
c41f8cbd
SS
632 struct fsf_qtcb *qtcb = req->qtcb;
633
634 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
635 return;
636
637 switch (qtcb->header.fsf_status) {
638 case FSF_GOOD:
639 zfcp_fsf_exchange_port_evaluate(req);
c41f8cbd
SS
640 break;
641 case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
642 zfcp_fsf_exchange_port_evaluate(req);
edaed859 643 zfcp_fsf_link_down_info_eval(req,
c41f8cbd 644 &qtcb->header.fsf_status_qual.link_down_info);
aef4a983 645 break;
1da177e4 646 }
c41f8cbd 647}
d26ab06e 648
a4623c46 649static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
c41f8cbd
SS
650{
651 struct zfcp_fsf_req *req;
a4623c46
SS
652
653 if (likely(pool))
654 req = mempool_alloc(pool, GFP_ATOMIC);
655 else
656 req = kmalloc(sizeof(*req), GFP_ATOMIC);
657
658 if (unlikely(!req))
c41f8cbd 659 return NULL;
a4623c46 660
c41f8cbd 661 memset(req, 0, sizeof(*req));
88f2a977 662 req->pool = pool;
c41f8cbd
SS
663 return req;
664}
665
a4623c46 666static struct fsf_qtcb *zfcp_qtcb_alloc(mempool_t *pool)
c41f8cbd 667{
a4623c46 668 struct fsf_qtcb *qtcb;
c41f8cbd
SS
669
670 if (likely(pool))
671 qtcb = mempool_alloc(pool, GFP_ATOMIC);
672 else
259afe2e 673 qtcb = kmem_cache_alloc(zfcp_fsf_qtcb_cache, GFP_ATOMIC);
a4623c46 674
c41f8cbd
SS
675 if (unlikely(!qtcb))
676 return NULL;
677
678 memset(qtcb, 0, sizeof(*qtcb));
a4623c46 679 return qtcb;
c41f8cbd
SS
680}
681
564e1c86 682static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
3ec90878 683 u32 fsf_cmd, u8 sbtype,
1674b405 684 mempool_t *pool)
1da177e4 685{
564e1c86 686 struct zfcp_adapter *adapter = qdio->adapter;
a4623c46 687 struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
1da177e4 688
c41f8cbd 689 if (unlikely(!req))
1e9b1643 690 return ERR_PTR(-ENOMEM);
1da177e4 691
c41f8cbd
SS
692 if (adapter->req_no == 0)
693 adapter->req_no++;
1da177e4 694
c41f8cbd
SS
695 INIT_LIST_HEAD(&req->list);
696 init_timer(&req->timer);
058b8647 697 init_completion(&req->completion);
1da177e4 698
c41f8cbd
SS
699 req->adapter = adapter;
700 req->fsf_command = fsf_cmd;
52bfb558 701 req->req_id = adapter->req_no;
c41f8cbd 702
a4623c46
SS
703 if (likely(fsf_cmd != FSF_QTCB_UNSOLICITED_STATUS)) {
704 if (likely(pool))
705 req->qtcb = zfcp_qtcb_alloc(adapter->pool.qtcb_pool);
706 else
707 req->qtcb = zfcp_qtcb_alloc(NULL);
708
709 if (unlikely(!req->qtcb)) {
710 zfcp_fsf_req_free(req);
711 return ERR_PTR(-ENOMEM);
712 }
713
5bdecd22 714 req->seq_no = adapter->fsf_req_seq_no;
564e1c86 715 req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
c41f8cbd
SS
716 req->qtcb->prefix.req_id = req->req_id;
717 req->qtcb->prefix.ulp_info = 26;
718 req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
719 req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
720 req->qtcb->header.req_handle = req->req_id;
721 req->qtcb->header.fsf_command = req->fsf_command;
c41f8cbd
SS
722 }
723
1674b405
CS
724 zfcp_qdio_req_init(adapter->qdio, &req->qdio_req, req->req_id, sbtype,
725 req->qtcb, sizeof(struct fsf_qtcb));
726
c41f8cbd 727 return req;
1da177e4
LT
728}
729
c41f8cbd
SS
730static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
731{
732 struct zfcp_adapter *adapter = req->adapter;
564e1c86 733 struct zfcp_qdio *qdio = adapter->qdio;
b6bd2fb9 734 int with_qtcb = (req->qtcb != NULL);
e60a6d69 735 int req_id = req->req_id;
c41f8cbd 736
b6bd2fb9 737 zfcp_reqlist_add(adapter->req_list, req);
c41f8cbd 738
706eca49 739 req->qdio_req.qdio_outb_usage = atomic_read(&qdio->req_q_free);
1aae0560 740 req->issued = get_tod_clock();
34c2b712 741 if (zfcp_qdio_send(qdio, &req->qdio_req)) {
c41f8cbd 742 del_timer(&req->timer);
3765138a 743 /* lookup request again, list might have changed */
b6bd2fb9 744 zfcp_reqlist_find_rm(adapter->req_list, req_id);
ea4a3a6a 745 zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1");
c41f8cbd
SS
746 return -EIO;
747 }
748
749 /* Don't increase for unsolicited status */
135ea137 750 if (with_qtcb)
c41f8cbd 751 adapter->fsf_req_seq_no++;
52bfb558 752 adapter->req_no++;
c41f8cbd
SS
753
754 return 0;
755}
756
757/**
758 * zfcp_fsf_status_read - send status read request
759 * @adapter: pointer to struct zfcp_adapter
760 * @req_flags: request flags
761 * Returns: 0 on success, ERROR otherwise
1da177e4 762 */
564e1c86 763int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
1da177e4 764{
564e1c86 765 struct zfcp_adapter *adapter = qdio->adapter;
c41f8cbd
SS
766 struct zfcp_fsf_req *req;
767 struct fsf_status_read_buffer *sr_buf;
c7b279ae 768 struct page *page;
c41f8cbd 769 int retval = -EIO;
1da177e4 770
44a24cb3 771 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 772 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
773 goto out;
774
75a1408d
MP
775 req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS,
776 SBAL_SFLAGS0_TYPE_STATUS,
a4623c46 777 adapter->pool.status_read_req);
025270f0 778 if (IS_ERR(req)) {
c41f8cbd
SS
779 retval = PTR_ERR(req);
780 goto out;
1da177e4
LT
781 }
782
c7b279ae
CS
783 page = mempool_alloc(adapter->pool.sr_data, GFP_ATOMIC);
784 if (!page) {
c41f8cbd
SS
785 retval = -ENOMEM;
786 goto failed_buf;
787 }
c7b279ae 788 sr_buf = page_address(page);
c41f8cbd
SS
789 memset(sr_buf, 0, sizeof(*sr_buf));
790 req->data = sr_buf;
1674b405
CS
791
792 zfcp_qdio_fill_next(qdio, &req->qdio_req, sr_buf, sizeof(*sr_buf));
793 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
059c97d0 794
c41f8cbd
SS
795 retval = zfcp_fsf_req_send(req);
796 if (retval)
797 goto failed_req_send;
1da177e4 798
c41f8cbd
SS
799 goto out;
800
801failed_req_send:
a54ca0f6 802 req->data = NULL;
c7b279ae 803 mempool_free(virt_to_page(sr_buf), adapter->pool.sr_data);
c41f8cbd 804failed_buf:
a54ca0f6 805 zfcp_dbf_hba_fsf_uss("fssr__1", req);
c41f8cbd 806 zfcp_fsf_req_free(req);
c41f8cbd 807out:
44a24cb3 808 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd
SS
809 return retval;
810}
811
812static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
813{
b62a8d9b 814 struct scsi_device *sdev = req->data;
d436de8c 815 struct zfcp_scsi_dev *zfcp_sdev;
c41f8cbd
SS
816 union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
817
818 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
819 return;
820
d436de8c
MP
821 zfcp_sdev = sdev_to_zfcp(sdev);
822
c41f8cbd 823 switch (req->qtcb->header.fsf_status) {
1da177e4 824 case FSF_PORT_HANDLE_NOT_VALID:
c41f8cbd 825 if (fsq->word[0] == fsq->word[1]) {
b62a8d9b 826 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0,
ea4a3a6a 827 "fsafch1");
c41f8cbd 828 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
829 }
830 break;
1da177e4 831 case FSF_LUN_HANDLE_NOT_VALID:
c41f8cbd 832 if (fsq->word[0] == fsq->word[1]) {
ea4a3a6a 833 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fsafch2");
c41f8cbd 834 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
835 }
836 break;
1da177e4 837 case FSF_FCP_COMMAND_DOES_NOT_EXIST:
c41f8cbd 838 req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
1da177e4 839 break;
1da177e4 840 case FSF_PORT_BOXED:
edaed859
SS
841 zfcp_erp_set_port_status(zfcp_sdev->port,
842 ZFCP_STATUS_COMMON_ACCESS_BOXED);
843 zfcp_erp_port_reopen(zfcp_sdev->port,
ea4a3a6a 844 ZFCP_STATUS_COMMON_ERP_FAILED, "fsafch3");
4c571c65 845 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 846 break;
1da177e4 847 case FSF_LUN_BOXED:
edaed859
SS
848 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
849 zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 850 "fsafch4");
4c571c65 851 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 852 break;
1da177e4 853 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 854 switch (fsq->word[0]) {
1da177e4 855 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 856 zfcp_fc_test_link(zfcp_sdev->port);
dceab655 857 /* fall through */
1da177e4 858 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 859 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 860 break;
1da177e4
LT
861 }
862 break;
1da177e4 863 case FSF_GOOD:
c41f8cbd 864 req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1da177e4 865 break;
1da177e4 866 }
1da177e4
LT
867}
868
869/**
b62a8d9b
CS
870 * zfcp_fsf_abort_fcp_cmnd - abort running SCSI command
871 * @scmnd: The SCSI command to abort
c41f8cbd 872 * Returns: pointer to struct zfcp_fsf_req
1da177e4 873 */
1da177e4 874
b62a8d9b 875struct zfcp_fsf_req *zfcp_fsf_abort_fcp_cmnd(struct scsi_cmnd *scmnd)
1da177e4 876{
c41f8cbd 877 struct zfcp_fsf_req *req = NULL;
b62a8d9b
CS
878 struct scsi_device *sdev = scmnd->device;
879 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
880 struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
881 unsigned long old_req_id = (unsigned long) scmnd->host_scribble;
8a36e453 882
44a24cb3 883 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 884 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 885 goto out;
564e1c86 886 req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
3ec90878 887 SBAL_SFLAGS0_TYPE_READ,
564e1c86 888 qdio->adapter->pool.scsi_abort);
633528c3
SS
889 if (IS_ERR(req)) {
890 req = NULL;
c41f8cbd 891 goto out;
633528c3 892 }
2abbe866 893
b62a8d9b 894 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
895 ZFCP_STATUS_COMMON_UNBLOCKED)))
896 goto out_error_free;
1da177e4 897
1674b405 898 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 899
6fbf25e8 900 req->data = sdev;
c41f8cbd 901 req->handler = zfcp_fsf_abort_fcp_command_handler;
b62a8d9b
CS
902 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
903 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
c41f8cbd
SS
904 req->qtcb->bottom.support.req_handle = (u64) old_req_id;
905
906 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
907 if (!zfcp_fsf_req_send(req))
908 goto out;
909
910out_error_free:
911 zfcp_fsf_req_free(req);
912 req = NULL;
913out:
44a24cb3 914 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 915 return req;
1da177e4
LT
916}
917
c41f8cbd 918static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
1da177e4 919{
c41f8cbd 920 struct zfcp_adapter *adapter = req->adapter;
7c7dc196 921 struct zfcp_fsf_ct_els *ct = req->data;
c41f8cbd 922 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 923
7c7dc196 924 ct->status = -EINVAL;
1da177e4 925
c41f8cbd 926 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
927 goto skip_fsfstatus;
928
1da177e4 929 switch (header->fsf_status) {
1da177e4 930 case FSF_GOOD:
0100998d 931 zfcp_dbf_san_res("fsscth2", req);
7c7dc196 932 ct->status = 0;
1da177e4 933 break;
1da177e4 934 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 935 zfcp_fsf_class_not_supp(req);
1da177e4 936 break;
1da177e4 937 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
938 switch (header->fsf_status_qual.word[0]){
939 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 940 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 941 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 942 break;
1da177e4
LT
943 }
944 break;
1da177e4 945 case FSF_PORT_BOXED:
4c571c65 946 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 947 break;
c41f8cbd 948 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 949 zfcp_erp_adapter_reopen(adapter, 0, "fsscth1");
dceab655 950 /* fall through */
c41f8cbd 951 case FSF_GENERIC_COMMAND_REJECTED:
1da177e4 952 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 953 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 954 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 955 case FSF_SBAL_MISMATCH:
c41f8cbd 956 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
957 break;
958 }
959
960skip_fsfstatus:
7c7dc196
CS
961 if (ct->handler)
962 ct->handler(ct->handler_data);
c41f8cbd 963}
1da177e4 964
1674b405
CS
965static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
966 struct zfcp_qdio_req *q_req,
426f6059
CS
967 struct scatterlist *sg_req,
968 struct scatterlist *sg_resp)
969{
1674b405
CS
970 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
971 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
972 zfcp_qdio_set_sbale_last(qdio, q_req);
426f6059
CS
973}
974
39eb7e9a
CS
975static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
976 struct scatterlist *sg_req,
01b04759 977 struct scatterlist *sg_resp)
c41f8cbd 978{
42428f74 979 struct zfcp_adapter *adapter = req->adapter;
86a9668a
SS
980 struct zfcp_qdio *qdio = adapter->qdio;
981 struct fsf_qtcb *qtcb = req->qtcb;
42428f74 982 u32 feat = adapter->adapter_features;
c41f8cbd 983
86a9668a
SS
984 if (zfcp_adapter_multi_buffer_active(adapter)) {
985 if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
986 return -EIO;
70369f8e
SM
987 qtcb->bottom.support.req_buf_length =
988 zfcp_qdio_real_bytes(sg_req);
86a9668a
SS
989 if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
990 return -EIO;
70369f8e
SM
991 qtcb->bottom.support.resp_buf_length =
992 zfcp_qdio_real_bytes(sg_resp);
39eb7e9a 993
7d91869c 994 zfcp_qdio_set_data_div(qdio, &req->qdio_req, sg_nents(sg_req));
86a9668a
SS
995 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
996 zfcp_qdio_set_scount(qdio, &req->qdio_req);
426f6059
CS
997 return 0;
998 }
999
1000 /* use single, unchained SBAL if it can hold the request */
30b6777b 1001 if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
86a9668a 1002 zfcp_fsf_setup_ct_els_unchained(qdio, &req->qdio_req,
1674b405 1003 sg_req, sg_resp);
39eb7e9a
CS
1004 return 0;
1005 }
1006
86a9668a
SS
1007 if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS))
1008 return -EOPNOTSUPP;
1009
1010 if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_req))
9072df4d 1011 return -EIO;
c41f8cbd 1012
86a9668a
SS
1013 qtcb->bottom.support.req_buf_length = zfcp_qdio_real_bytes(sg_req);
1014
1015 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1016 zfcp_qdio_skip_to_last_sbale(qdio, &req->qdio_req);
1017
1018 if (zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, sg_resp))
9072df4d 1019 return -EIO;
86a9668a
SS
1020
1021 qtcb->bottom.support.resp_buf_length = zfcp_qdio_real_bytes(sg_resp);
1022
1023 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
98fc4d5c 1024
b1a58985
CS
1025 return 0;
1026}
1027
1028static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
1029 struct scatterlist *sg_req,
1030 struct scatterlist *sg_resp,
01b04759 1031 unsigned int timeout)
b1a58985
CS
1032{
1033 int ret;
1034
01b04759 1035 ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp);
b1a58985
CS
1036 if (ret)
1037 return ret;
1038
98fc4d5c 1039 /* common settings for ct/gs and els requests */
51375ee8
SS
1040 if (timeout > 255)
1041 timeout = 255; /* max value accepted by hardware */
98fc4d5c 1042 req->qtcb->bottom.support.service_class = FSF_CLASS_3;
51375ee8
SS
1043 req->qtcb->bottom.support.timeout = timeout;
1044 zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
c41f8cbd
SS
1045
1046 return 0;
1da177e4
LT
1047}
1048
1049/**
c41f8cbd
SS
1050 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1051 * @ct: pointer to struct zfcp_send_ct with data for request
1052 * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1da177e4 1053 */
7c7dc196 1054int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
51375ee8
SS
1055 struct zfcp_fsf_ct_els *ct, mempool_t *pool,
1056 unsigned int timeout)
1da177e4 1057{
564e1c86 1058 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
c41f8cbd
SS
1059 struct zfcp_fsf_req *req;
1060 int ret = -EIO;
1da177e4 1061
44a24cb3 1062 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1063 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1064 goto out;
1da177e4 1065
1674b405 1066 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
3ec90878 1067 SBAL_SFLAGS0_TYPE_WRITE_READ, pool);
09a46c6e 1068
025270f0 1069 if (IS_ERR(req)) {
c41f8cbd
SS
1070 ret = PTR_ERR(req);
1071 goto out;
3f0ca62a
CS
1072 }
1073
09a46c6e 1074 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759 1075 ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp, timeout);
553448f6 1076 if (ret)
1da177e4 1077 goto failed_send;
1da177e4 1078
c41f8cbd 1079 req->handler = zfcp_fsf_send_ct_handler;
5ab944f9 1080 req->qtcb->header.port_handle = wka_port->handle;
771bf035 1081 ct->d_id = wka_port->d_id;
c41f8cbd
SS
1082 req->data = ct;
1083
2c55b750 1084 zfcp_dbf_san_req("fssct_1", req, wka_port->d_id);
1da177e4 1085
c41f8cbd
SS
1086 ret = zfcp_fsf_req_send(req);
1087 if (ret)
1088 goto failed_send;
1da177e4 1089
c41f8cbd 1090 goto out;
1da177e4 1091
c41f8cbd
SS
1092failed_send:
1093 zfcp_fsf_req_free(req);
c41f8cbd 1094out:
44a24cb3 1095 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1096 return ret;
1da177e4
LT
1097}
1098
c41f8cbd 1099static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1da177e4 1100{
7c7dc196 1101 struct zfcp_fsf_ct_els *send_els = req->data;
c41f8cbd 1102 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 1103
c41f8cbd 1104 send_els->status = -EINVAL;
1da177e4 1105
c41f8cbd 1106 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
1107 goto skip_fsfstatus;
1108
1109 switch (header->fsf_status) {
1da177e4 1110 case FSF_GOOD:
2c55b750 1111 zfcp_dbf_san_res("fsselh1", req);
c41f8cbd 1112 send_els->status = 0;
1da177e4 1113 break;
1da177e4 1114 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 1115 zfcp_fsf_class_not_supp(req);
1da177e4 1116 break;
1da177e4 1117 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1118 switch (header->fsf_status_qual.word[0]){
1119 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1120 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1121 case FSF_SQ_RETRY_IF_POSSIBLE:
c41f8cbd 1122 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1123 break;
1da177e4
LT
1124 }
1125 break;
1da177e4 1126 case FSF_ELS_COMMAND_REJECTED:
1da177e4 1127 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 1128 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 1129 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 1130 break;
c41f8cbd 1131 case FSF_SBAL_MISMATCH:
25985edc 1132 /* should never occur, avoided in zfcp_fsf_send_els */
c41f8cbd 1133 /* fall through */
1da177e4 1134 default:
c41f8cbd 1135 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1136 break;
1137 }
1da177e4 1138skip_fsfstatus:
aa551daf 1139 if (send_els->handler)
1da177e4 1140 send_els->handler(send_els->handler_data);
c41f8cbd 1141}
1da177e4 1142
c41f8cbd
SS
1143/**
1144 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1145 * @els: pointer to struct zfcp_send_els with data for the command
1146 */
7c7dc196 1147int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
51375ee8 1148 struct zfcp_fsf_ct_els *els, unsigned int timeout)
c41f8cbd
SS
1149{
1150 struct zfcp_fsf_req *req;
7c7dc196 1151 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1152 int ret = -EIO;
1153
44a24cb3 1154 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1155 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1156 goto out;
09a46c6e 1157
1674b405 1158 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
3ec90878 1159 SBAL_SFLAGS0_TYPE_WRITE_READ, NULL);
09a46c6e 1160
025270f0 1161 if (IS_ERR(req)) {
c41f8cbd
SS
1162 ret = PTR_ERR(req);
1163 goto out;
1164 }
1165
09a46c6e 1166 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759 1167
86a9668a
SS
1168 if (!zfcp_adapter_multi_buffer_active(adapter))
1169 zfcp_qdio_sbal_limit(qdio, &req->qdio_req, 2);
01b04759
SS
1170
1171 ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, timeout);
44cc76f2 1172
c41f8cbd
SS
1173 if (ret)
1174 goto failed_send;
1175
7c7dc196 1176 hton24(req->qtcb->bottom.support.d_id, d_id);
c41f8cbd 1177 req->handler = zfcp_fsf_send_els_handler;
771bf035 1178 els->d_id = d_id;
c41f8cbd
SS
1179 req->data = els;
1180
2c55b750 1181 zfcp_dbf_san_req("fssels1", req, d_id);
c41f8cbd 1182
c41f8cbd
SS
1183 ret = zfcp_fsf_req_send(req);
1184 if (ret)
1185 goto failed_send;
1186
1187 goto out;
1188
1189failed_send:
1190 zfcp_fsf_req_free(req);
1191out:
44a24cb3 1192 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1193 return ret;
1da177e4
LT
1194}
1195
c41f8cbd 1196int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1da177e4 1197{
c41f8cbd 1198 struct zfcp_fsf_req *req;
564e1c86 1199 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1200 int retval = -EIO;
1201
44a24cb3 1202 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1203 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1204 goto out;
09a46c6e 1205
564e1c86 1206 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
3ec90878 1207 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1208 qdio->adapter->pool.erp_req);
09a46c6e 1209
025270f0 1210 if (IS_ERR(req)) {
c41f8cbd
SS
1211 retval = PTR_ERR(req);
1212 goto out;
1da177e4
LT
1213 }
1214
09a46c6e 1215 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1216 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1217
c41f8cbd 1218 req->qtcb->bottom.config.feature_selection =
9eb69aff 1219 FSF_FEATURE_NOTIFICATION_LOST |
aef4a983 1220 FSF_FEATURE_UPDATE_ALERT;
c41f8cbd
SS
1221 req->erp_action = erp_action;
1222 req->handler = zfcp_fsf_exchange_config_data_handler;
e60a6d69 1223 erp_action->fsf_req_id = req->req_id;
1da177e4 1224
287ac01a 1225 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1226 retval = zfcp_fsf_req_send(req);
1da177e4 1227 if (retval) {
c41f8cbd 1228 zfcp_fsf_req_free(req);
e60a6d69 1229 erp_action->fsf_req_id = 0;
1da177e4 1230 }
c41f8cbd 1231out:
44a24cb3 1232 spin_unlock_irq(&qdio->req_q_lock);
52ef11a7
SS
1233 return retval;
1234}
1da177e4 1235
564e1c86 1236int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1237 struct fsf_qtcb_bottom_config *data)
52ef11a7 1238{
c41f8cbd
SS
1239 struct zfcp_fsf_req *req = NULL;
1240 int retval = -EIO;
1241
44a24cb3 1242 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1243 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1244 goto out_unlock;
c41f8cbd 1245
1674b405 1246 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
3ec90878 1247 SBAL_SFLAGS0_TYPE_READ, NULL);
09a46c6e 1248
025270f0 1249 if (IS_ERR(req)) {
c41f8cbd 1250 retval = PTR_ERR(req);
ada81b74 1251 goto out_unlock;
52ef11a7
SS
1252 }
1253
1674b405 1254 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
c41f8cbd 1255 req->handler = zfcp_fsf_exchange_config_data_handler;
52ef11a7 1256
c41f8cbd 1257 req->qtcb->bottom.config.feature_selection =
52ef11a7
SS
1258 FSF_FEATURE_NOTIFICATION_LOST |
1259 FSF_FEATURE_UPDATE_ALERT;
1260
1261 if (data)
c41f8cbd 1262 req->data = data;
52ef11a7 1263
c41f8cbd
SS
1264 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1265 retval = zfcp_fsf_req_send(req);
44a24cb3 1266 spin_unlock_irq(&qdio->req_q_lock);
553448f6 1267 if (!retval)
058b8647 1268 wait_for_completion(&req->completion);
52ef11a7 1269
c41f8cbd 1270 zfcp_fsf_req_free(req);
ada81b74 1271 return retval;
52ef11a7 1272
ada81b74 1273out_unlock:
44a24cb3 1274 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1275 return retval;
1276}
1277
1da177e4
LT
1278/**
1279 * zfcp_fsf_exchange_port_data - request information about local port
aef4a983 1280 * @erp_action: ERP action for the adapter for which port data is requested
c41f8cbd 1281 * Returns: 0 on success, error otherwise
1da177e4 1282 */
c41f8cbd 1283int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1da177e4 1284{
564e1c86 1285 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd 1286 struct zfcp_fsf_req *req;
c41f8cbd 1287 int retval = -EIO;
1da177e4 1288
564e1c86 1289 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1290 return -EOPNOTSUPP;
1da177e4 1291
44a24cb3 1292 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1293 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1294 goto out;
09a46c6e 1295
564e1c86 1296 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
3ec90878 1297 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1298 qdio->adapter->pool.erp_req);
09a46c6e 1299
025270f0 1300 if (IS_ERR(req)) {
c41f8cbd
SS
1301 retval = PTR_ERR(req);
1302 goto out;
aef4a983
MS
1303 }
1304
09a46c6e 1305 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1306 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1307
c41f8cbd
SS
1308 req->handler = zfcp_fsf_exchange_port_data_handler;
1309 req->erp_action = erp_action;
e60a6d69 1310 erp_action->fsf_req_id = req->req_id;
52ef11a7 1311
287ac01a 1312 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1313 retval = zfcp_fsf_req_send(req);
1da177e4 1314 if (retval) {
c41f8cbd 1315 zfcp_fsf_req_free(req);
e60a6d69 1316 erp_action->fsf_req_id = 0;
52ef11a7 1317 }
c41f8cbd 1318out:
44a24cb3 1319 spin_unlock_irq(&qdio->req_q_lock);
52ef11a7
SS
1320 return retval;
1321}
1322
52ef11a7
SS
1323/**
1324 * zfcp_fsf_exchange_port_data_sync - request information about local port
564e1c86 1325 * @qdio: pointer to struct zfcp_qdio
c41f8cbd
SS
1326 * @data: pointer to struct fsf_qtcb_bottom_port
1327 * Returns: 0 on success, error otherwise
52ef11a7 1328 */
564e1c86 1329int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1330 struct fsf_qtcb_bottom_port *data)
52ef11a7 1331{
c41f8cbd
SS
1332 struct zfcp_fsf_req *req = NULL;
1333 int retval = -EIO;
52ef11a7 1334
564e1c86 1335 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1336 return -EOPNOTSUPP;
52ef11a7 1337
44a24cb3 1338 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1339 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1340 goto out_unlock;
c41f8cbd 1341
1674b405 1342 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
3ec90878 1343 SBAL_SFLAGS0_TYPE_READ, NULL);
09a46c6e 1344
025270f0 1345 if (IS_ERR(req)) {
c41f8cbd 1346 retval = PTR_ERR(req);
ada81b74 1347 goto out_unlock;
1da177e4
LT
1348 }
1349
52ef11a7 1350 if (data)
c41f8cbd 1351 req->data = data;
52ef11a7 1352
1674b405 1353 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
52ef11a7 1354
c41f8cbd
SS
1355 req->handler = zfcp_fsf_exchange_port_data_handler;
1356 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1357 retval = zfcp_fsf_req_send(req);
44a24cb3 1358 spin_unlock_irq(&qdio->req_q_lock);
ada81b74 1359
553448f6 1360 if (!retval)
058b8647
SS
1361 wait_for_completion(&req->completion);
1362
c41f8cbd 1363 zfcp_fsf_req_free(req);
1da177e4 1364
1da177e4 1365 return retval;
ada81b74
CS
1366
1367out_unlock:
44a24cb3 1368 spin_unlock_irq(&qdio->req_q_lock);
ada81b74 1369 return retval;
1da177e4
LT
1370}
1371
c41f8cbd 1372static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1da177e4 1373{
c41f8cbd
SS
1374 struct zfcp_port *port = req->data;
1375 struct fsf_qtcb_header *header = &req->qtcb->header;
9d05ce2c 1376 struct fc_els_flogi *plogi;
1da177e4 1377
c41f8cbd 1378 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a17c5855 1379 goto out;
1da177e4 1380
1da177e4 1381 switch (header->fsf_status) {
1da177e4 1382 case FSF_PORT_ALREADY_OPEN:
1da177e4 1383 break;
1da177e4 1384 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
c41f8cbd 1385 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 1386 "Not enough FCP adapter resources to open "
7ba58c9c
SS
1387 "remote port 0x%016Lx\n",
1388 (unsigned long long)port->wwpn);
edaed859
SS
1389 zfcp_erp_set_port_status(port,
1390 ZFCP_STATUS_COMMON_ERP_FAILED);
c41f8cbd 1391 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1392 break;
1da177e4 1393 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1394 switch (header->fsf_status_qual.word[0]) {
1395 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1396 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1397 case FSF_SQ_NO_RETRY_POSSIBLE:
c41f8cbd 1398 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1399 break;
1400 }
1401 break;
1da177e4 1402 case FSF_GOOD:
1da177e4 1403 port->handle = header->port_handle;
805de8f4 1404 atomic_or(ZFCP_STATUS_COMMON_OPEN |
1da177e4 1405 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
805de8f4 1406 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_BOXED,
d736a27b 1407 &port->status);
1da177e4
LT
1408 /* check whether D_ID has changed during open */
1409 /*
1410 * FIXME: This check is not airtight, as the FCP channel does
1411 * not monitor closures of target port connections caused on
1412 * the remote side. Thus, they might miss out on invalidating
1413 * locally cached WWPNs (and other N_Port parameters) of gone
1414 * target ports. So, our heroic attempt to make things safe
1415 * could be undermined by 'open port' response data tagged with
1416 * obsolete WWPNs. Another reason to monitor potential
1417 * connection closures ourself at least (by interpreting
1418 * incoming ELS' and unsolicited status). It just crosses my
1419 * mind that one should be able to cross-check by means of
1420 * another GID_PN straight after a port has been opened.
1421 * Alternately, an ADISC/PDISC ELS should suffice, as well.
1422 */
9d05ce2c 1423 plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
39eb7e9a 1424 if (req->qtcb->bottom.support.els1_length >=
9d05ce2c 1425 FSF_PLOGI_MIN_LEN)
c41f8cbd 1426 zfcp_fc_plogi_evaluate(port, plogi);
1da177e4 1427 break;
1da177e4 1428 case FSF_UNKNOWN_OP_SUBTYPE:
c41f8cbd 1429 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1430 break;
1431 }
a17c5855
MP
1432
1433out:
615f59e0 1434 put_device(&port->dev);
1da177e4
LT
1435}
1436
c41f8cbd
SS
1437/**
1438 * zfcp_fsf_open_port - create and send open port request
1439 * @erp_action: pointer to struct zfcp_erp_action
1440 * Returns: 0 on success, error otherwise
1da177e4 1441 */
c41f8cbd 1442int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1da177e4 1443{
564e1c86 1444 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
a17c5855 1445 struct zfcp_port *port = erp_action->port;
564e1c86 1446 struct zfcp_fsf_req *req;
c41f8cbd
SS
1447 int retval = -EIO;
1448
44a24cb3 1449 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1450 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1451 goto out;
1452
564e1c86 1453 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
3ec90878 1454 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1455 qdio->adapter->pool.erp_req);
09a46c6e 1456
025270f0 1457 if (IS_ERR(req)) {
c41f8cbd 1458 retval = PTR_ERR(req);
1da177e4 1459 goto out;
c41f8cbd 1460 }
1da177e4 1461
09a46c6e 1462 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1463 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1464
c41f8cbd 1465 req->handler = zfcp_fsf_open_port_handler;
800c0cad 1466 hton24(req->qtcb->bottom.support.d_id, port->d_id);
a17c5855 1467 req->data = port;
c41f8cbd 1468 req->erp_action = erp_action;
e60a6d69 1469 erp_action->fsf_req_id = req->req_id;
615f59e0 1470 get_device(&port->dev);
c41f8cbd 1471
287ac01a 1472 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1473 retval = zfcp_fsf_req_send(req);
1da177e4 1474 if (retval) {
c41f8cbd 1475 zfcp_fsf_req_free(req);
e60a6d69 1476 erp_action->fsf_req_id = 0;
615f59e0 1477 put_device(&port->dev);
1da177e4 1478 }
c41f8cbd 1479out:
44a24cb3 1480 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1481 return retval;
1482}
1483
c41f8cbd 1484static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1da177e4 1485{
c41f8cbd 1486 struct zfcp_port *port = req->data;
1da177e4 1487
c41f8cbd 1488 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1489 return;
1da177e4 1490
c41f8cbd 1491 switch (req->qtcb->header.fsf_status) {
1da177e4 1492 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 1493 zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1");
c41f8cbd 1494 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1495 break;
1da177e4 1496 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4 1497 break;
1da177e4 1498 case FSF_GOOD:
edaed859 1499 zfcp_erp_clear_port_status(port, ZFCP_STATUS_COMMON_OPEN);
1da177e4 1500 break;
1da177e4 1501 }
1da177e4
LT
1502}
1503
c41f8cbd
SS
1504/**
1505 * zfcp_fsf_close_port - create and send close port request
1506 * @erp_action: pointer to struct zfcp_erp_action
1507 * Returns: 0 on success, error otherwise
1da177e4 1508 */
c41f8cbd 1509int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1da177e4 1510{
564e1c86 1511 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1512 struct zfcp_fsf_req *req;
1513 int retval = -EIO;
1514
44a24cb3 1515 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1516 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1517 goto out;
1518
564e1c86 1519 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
3ec90878 1520 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1521 qdio->adapter->pool.erp_req);
09a46c6e 1522
025270f0 1523 if (IS_ERR(req)) {
c41f8cbd 1524 retval = PTR_ERR(req);
1da177e4 1525 goto out;
c41f8cbd 1526 }
1da177e4 1527
09a46c6e 1528 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1529 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1530
c41f8cbd
SS
1531 req->handler = zfcp_fsf_close_port_handler;
1532 req->data = erp_action->port;
1533 req->erp_action = erp_action;
1534 req->qtcb->header.port_handle = erp_action->port->handle;
e60a6d69 1535 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1536
287ac01a 1537 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1538 retval = zfcp_fsf_req_send(req);
1da177e4 1539 if (retval) {
c41f8cbd 1540 zfcp_fsf_req_free(req);
e60a6d69 1541 erp_action->fsf_req_id = 0;
1da177e4 1542 }
c41f8cbd 1543out:
44a24cb3 1544 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1545 return retval;
1546}
1547
5ab944f9
SS
1548static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1549{
bd0072ec 1550 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1551 struct fsf_qtcb_header *header = &req->qtcb->header;
1552
1553 if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
bd0072ec 1554 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1555 goto out;
1556 }
1557
1558 switch (header->fsf_status) {
1559 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1560 dev_warn(&req->adapter->ccw_device->dev,
1561 "Opening WKA port 0x%x failed\n", wka_port->d_id);
dceab655 1562 /* fall through */
5ab944f9
SS
1563 case FSF_ADAPTER_STATUS_AVAILABLE:
1564 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
bd0072ec 1565 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9 1566 break;
5ab944f9
SS
1567 case FSF_GOOD:
1568 wka_port->handle = header->port_handle;
27f492cc
SS
1569 /* fall through */
1570 case FSF_PORT_ALREADY_OPEN:
bd0072ec 1571 wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
5ab944f9
SS
1572 }
1573out:
1574 wake_up(&wka_port->completion_wq);
1575}
1576
1577/**
1578 * zfcp_fsf_open_wka_port - create and send open wka-port request
bd0072ec 1579 * @wka_port: pointer to struct zfcp_fc_wka_port
5ab944f9
SS
1580 * Returns: 0 on success, error otherwise
1581 */
bd0072ec 1582int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1583{
564e1c86 1584 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
2dfa6688 1585 struct zfcp_fsf_req *req;
5ab944f9
SS
1586 int retval = -EIO;
1587
44a24cb3 1588 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1589 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1590 goto out;
1591
564e1c86 1592 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
3ec90878 1593 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1594 qdio->adapter->pool.erp_req);
09a46c6e 1595
4e7d7af4 1596 if (IS_ERR(req)) {
5ab944f9
SS
1597 retval = PTR_ERR(req);
1598 goto out;
1599 }
1600
09a46c6e 1601 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1602 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1603
1604 req->handler = zfcp_fsf_open_wka_port_handler;
800c0cad 1605 hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
5ab944f9
SS
1606 req->data = wka_port;
1607
1608 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1609 retval = zfcp_fsf_req_send(req);
1610 if (retval)
1611 zfcp_fsf_req_free(req);
1612out:
44a24cb3 1613 spin_unlock_irq(&qdio->req_q_lock);
2dfa6688 1614 if (!retval)
d27a7cb9 1615 zfcp_dbf_rec_run_wka("fsowp_1", wka_port, req->req_id);
5ab944f9
SS
1616 return retval;
1617}
1618
1619static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1620{
bd0072ec 1621 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1622
1623 if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1624 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
ea4a3a6a 1625 zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1");
5ab944f9
SS
1626 }
1627
bd0072ec 1628 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1629 wake_up(&wka_port->completion_wq);
1630}
1631
1632/**
1633 * zfcp_fsf_close_wka_port - create and send close wka port request
bd0072ec 1634 * @wka_port: WKA port to open
5ab944f9
SS
1635 * Returns: 0 on success, error otherwise
1636 */
bd0072ec 1637int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1638{
564e1c86 1639 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
2dfa6688 1640 struct zfcp_fsf_req *req;
5ab944f9
SS
1641 int retval = -EIO;
1642
44a24cb3 1643 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1644 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1645 goto out;
1646
564e1c86 1647 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
3ec90878 1648 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1649 qdio->adapter->pool.erp_req);
09a46c6e 1650
4e7d7af4 1651 if (IS_ERR(req)) {
5ab944f9
SS
1652 retval = PTR_ERR(req);
1653 goto out;
1654 }
1655
09a46c6e 1656 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1657 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1658
1659 req->handler = zfcp_fsf_close_wka_port_handler;
1660 req->data = wka_port;
1661 req->qtcb->header.port_handle = wka_port->handle;
1662
1663 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1664 retval = zfcp_fsf_req_send(req);
1665 if (retval)
1666 zfcp_fsf_req_free(req);
1667out:
44a24cb3 1668 spin_unlock_irq(&qdio->req_q_lock);
2dfa6688 1669 if (!retval)
d27a7cb9 1670 zfcp_dbf_rec_run_wka("fscwp_1", wka_port, req->req_id);
5ab944f9
SS
1671 return retval;
1672}
1673
c41f8cbd 1674static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1da177e4 1675{
c41f8cbd
SS
1676 struct zfcp_port *port = req->data;
1677 struct fsf_qtcb_header *header = &req->qtcb->header;
b62a8d9b 1678 struct scsi_device *sdev;
1da177e4 1679
c41f8cbd 1680 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a5b11dda 1681 return;
1da177e4 1682
1da177e4 1683 switch (header->fsf_status) {
1da177e4 1684 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 1685 zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1");
c41f8cbd 1686 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1687 break;
1da177e4 1688 case FSF_PORT_BOXED:
5c815d15
CS
1689 /* can't use generic zfcp_erp_modify_port_status because
1690 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
805de8f4 1691 atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
b62a8d9b
CS
1692 shost_for_each_device(sdev, port->adapter->scsi_host)
1693 if (sdev_to_zfcp(sdev)->port == port)
805de8f4 1694 atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
b62a8d9b 1695 &sdev_to_zfcp(sdev)->status);
edaed859
SS
1696 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ACCESS_BOXED);
1697 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 1698 "fscpph2");
4c571c65 1699 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1700 break;
1da177e4 1701 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1702 switch (header->fsf_status_qual.word[0]) {
1703 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
c41f8cbd 1704 /* fall through */
1da177e4 1705 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1706 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1707 break;
1da177e4
LT
1708 }
1709 break;
1da177e4 1710 case FSF_GOOD:
1da177e4
LT
1711 /* can't use generic zfcp_erp_modify_port_status because
1712 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1713 */
805de8f4 1714 atomic_andnot(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
b62a8d9b
CS
1715 shost_for_each_device(sdev, port->adapter->scsi_host)
1716 if (sdev_to_zfcp(sdev)->port == port)
805de8f4 1717 atomic_andnot(ZFCP_STATUS_COMMON_OPEN,
b62a8d9b 1718 &sdev_to_zfcp(sdev)->status);
1da177e4 1719 break;
1da177e4 1720 }
1da177e4
LT
1721}
1722
c41f8cbd
SS
1723/**
1724 * zfcp_fsf_close_physical_port - close physical port
1725 * @erp_action: pointer to struct zfcp_erp_action
1726 * Returns: 0 on success
1da177e4 1727 */
c41f8cbd 1728int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1da177e4 1729{
564e1c86 1730 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1731 struct zfcp_fsf_req *req;
1732 int retval = -EIO;
1733
44a24cb3 1734 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1735 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1736 goto out;
1da177e4 1737
564e1c86 1738 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
3ec90878 1739 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1740 qdio->adapter->pool.erp_req);
09a46c6e 1741
025270f0 1742 if (IS_ERR(req)) {
c41f8cbd
SS
1743 retval = PTR_ERR(req);
1744 goto out;
1745 }
1da177e4 1746
09a46c6e 1747 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1748 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1749
c41f8cbd
SS
1750 req->data = erp_action->port;
1751 req->qtcb->header.port_handle = erp_action->port->handle;
1752 req->erp_action = erp_action;
1753 req->handler = zfcp_fsf_close_physical_port_handler;
e60a6d69 1754 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1755
287ac01a 1756 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1757 retval = zfcp_fsf_req_send(req);
1da177e4 1758 if (retval) {
c41f8cbd 1759 zfcp_fsf_req_free(req);
e60a6d69 1760 erp_action->fsf_req_id = 0;
1da177e4 1761 }
c41f8cbd 1762out:
44a24cb3 1763 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1764 return retval;
1765}
1766
b62a8d9b 1767static void zfcp_fsf_open_lun_handler(struct zfcp_fsf_req *req)
1da177e4 1768{
c41f8cbd 1769 struct zfcp_adapter *adapter = req->adapter;
b62a8d9b 1770 struct scsi_device *sdev = req->data;
d436de8c 1771 struct zfcp_scsi_dev *zfcp_sdev;
c41f8cbd 1772 struct fsf_qtcb_header *header = &req->qtcb->header;
663e0890 1773 union fsf_status_qual *qual = &header->fsf_status_qual;
1da177e4 1774
c41f8cbd 1775 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1776 return;
1da177e4 1777
d436de8c
MP
1778 zfcp_sdev = sdev_to_zfcp(sdev);
1779
805de8f4 1780 atomic_andnot(ZFCP_STATUS_COMMON_ACCESS_DENIED |
663e0890 1781 ZFCP_STATUS_COMMON_ACCESS_BOXED,
b62a8d9b 1782 &zfcp_sdev->status);
1da177e4 1783
1da177e4
LT
1784 switch (header->fsf_status) {
1785
1786 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 1787 zfcp_erp_adapter_reopen(adapter, 0, "fsouh_1");
c41f8cbd 1788 /* fall through */
1da177e4 1789 case FSF_LUN_ALREADY_OPEN:
1da177e4 1790 break;
1da177e4 1791 case FSF_PORT_BOXED:
edaed859
SS
1792 zfcp_erp_set_port_status(zfcp_sdev->port,
1793 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1794 zfcp_erp_port_reopen(zfcp_sdev->port,
ea4a3a6a 1795 ZFCP_STATUS_COMMON_ERP_FAILED, "fsouh_2");
4c571c65 1796 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1797 break;
1da177e4 1798 case FSF_LUN_SHARING_VIOLATION:
663e0890
MP
1799 if (qual->word[0])
1800 dev_warn(&zfcp_sdev->port->adapter->ccw_device->dev,
1801 "LUN 0x%Lx on port 0x%Lx is already in "
1802 "use by CSS%d, MIF Image ID %x\n",
1803 zfcp_scsi_dev_lun(sdev),
1804 (unsigned long long)zfcp_sdev->port->wwpn,
1805 qual->fsf_queue_designator.cssid,
1806 qual->fsf_queue_designator.hla);
1807 zfcp_erp_set_lun_status(sdev,
1808 ZFCP_STATUS_COMMON_ERP_FAILED |
1809 ZFCP_STATUS_COMMON_ACCESS_DENIED);
c41f8cbd 1810 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1811 break;
1da177e4 1812 case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
c41f8cbd 1813 dev_warn(&adapter->ccw_device->dev,
ff3b24fa
CS
1814 "No handle is available for LUN "
1815 "0x%016Lx on port 0x%016Lx\n",
b62a8d9b
CS
1816 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1817 (unsigned long long)zfcp_sdev->port->wwpn);
edaed859 1818 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
c41f8cbd
SS
1819 /* fall through */
1820 case FSF_INVALID_COMMAND_OPTION:
1821 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1822 break;
1da177e4 1823 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1824 switch (header->fsf_status_qual.word[0]) {
1825 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 1826 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 1827 /* fall through */
1da177e4 1828 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1829 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1830 break;
1da177e4
LT
1831 }
1832 break;
1833
1da177e4 1834 case FSF_GOOD:
b62a8d9b 1835 zfcp_sdev->lun_handle = header->lun_handle;
805de8f4 1836 atomic_or(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
1da177e4 1837 break;
1da177e4 1838 }
1da177e4
LT
1839}
1840
c41f8cbd 1841/**
b62a8d9b 1842 * zfcp_fsf_open_lun - open LUN
c41f8cbd
SS
1843 * @erp_action: pointer to struct zfcp_erp_action
1844 * Returns: 0 on success, error otherwise
1da177e4 1845 */
b62a8d9b 1846int zfcp_fsf_open_lun(struct zfcp_erp_action *erp_action)
1da177e4 1847{
c41f8cbd 1848 struct zfcp_adapter *adapter = erp_action->adapter;
564e1c86 1849 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1850 struct zfcp_fsf_req *req;
1851 int retval = -EIO;
1852
44a24cb3 1853 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1854 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1855 goto out;
1da177e4 1856
564e1c86 1857 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
3ec90878 1858 SBAL_SFLAGS0_TYPE_READ,
a4623c46 1859 adapter->pool.erp_req);
09a46c6e 1860
025270f0 1861 if (IS_ERR(req)) {
c41f8cbd
SS
1862 retval = PTR_ERR(req);
1863 goto out;
1864 }
1865
09a46c6e 1866 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1867 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1868
c41f8cbd 1869 req->qtcb->header.port_handle = erp_action->port->handle;
b62a8d9b
CS
1870 req->qtcb->bottom.support.fcp_lun = zfcp_scsi_dev_lun(erp_action->sdev);
1871 req->handler = zfcp_fsf_open_lun_handler;
1872 req->data = erp_action->sdev;
c41f8cbd 1873 req->erp_action = erp_action;
e60a6d69 1874 erp_action->fsf_req_id = req->req_id;
c41f8cbd
SS
1875
1876 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1877 req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1878
287ac01a 1879 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1880 retval = zfcp_fsf_req_send(req);
1da177e4 1881 if (retval) {
c41f8cbd 1882 zfcp_fsf_req_free(req);
e60a6d69 1883 erp_action->fsf_req_id = 0;
1da177e4 1884 }
c41f8cbd 1885out:
44a24cb3 1886 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1887 return retval;
1888}
1889
b62a8d9b 1890static void zfcp_fsf_close_lun_handler(struct zfcp_fsf_req *req)
1da177e4 1891{
b62a8d9b 1892 struct scsi_device *sdev = req->data;
d436de8c 1893 struct zfcp_scsi_dev *zfcp_sdev;
1da177e4 1894
c41f8cbd 1895 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1896 return;
1da177e4 1897
d436de8c
MP
1898 zfcp_sdev = sdev_to_zfcp(sdev);
1899
c41f8cbd 1900 switch (req->qtcb->header.fsf_status) {
1da177e4 1901 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 1902 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fscuh_1");
c41f8cbd 1903 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1904 break;
1da177e4 1905 case FSF_LUN_HANDLE_NOT_VALID:
ea4a3a6a 1906 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fscuh_2");
c41f8cbd 1907 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1908 break;
1da177e4 1909 case FSF_PORT_BOXED:
edaed859
SS
1910 zfcp_erp_set_port_status(zfcp_sdev->port,
1911 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1912 zfcp_erp_port_reopen(zfcp_sdev->port,
ea4a3a6a 1913 ZFCP_STATUS_COMMON_ERP_FAILED, "fscuh_3");
4c571c65 1914 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1915 break;
1da177e4 1916 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 1917 switch (req->qtcb->header.fsf_status_qual.word[0]) {
1da177e4 1918 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 1919 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 1920 /* fall through */
1da177e4 1921 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1922 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1923 break;
1924 }
1925 break;
1da177e4 1926 case FSF_GOOD:
805de8f4 1927 atomic_andnot(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
1da177e4 1928 break;
1da177e4 1929 }
1da177e4
LT
1930}
1931
1932/**
b62a8d9b
CS
1933 * zfcp_fsf_close_LUN - close LUN
1934 * @erp_action: pointer to erp_action triggering the "close LUN"
c41f8cbd 1935 * Returns: 0 on success, error otherwise
1da177e4 1936 */
b62a8d9b 1937int zfcp_fsf_close_lun(struct zfcp_erp_action *erp_action)
1da177e4 1938{
564e1c86 1939 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
b62a8d9b 1940 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
c41f8cbd
SS
1941 struct zfcp_fsf_req *req;
1942 int retval = -EIO;
1da177e4 1943
44a24cb3 1944 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1945 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1946 goto out;
09a46c6e 1947
564e1c86 1948 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
3ec90878 1949 SBAL_SFLAGS0_TYPE_READ,
564e1c86 1950 qdio->adapter->pool.erp_req);
09a46c6e 1951
025270f0 1952 if (IS_ERR(req)) {
c41f8cbd
SS
1953 retval = PTR_ERR(req);
1954 goto out;
1955 }
1da177e4 1956
09a46c6e 1957 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1958 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1959
c41f8cbd 1960 req->qtcb->header.port_handle = erp_action->port->handle;
b62a8d9b
CS
1961 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
1962 req->handler = zfcp_fsf_close_lun_handler;
1963 req->data = erp_action->sdev;
c41f8cbd 1964 req->erp_action = erp_action;
e60a6d69 1965 erp_action->fsf_req_id = req->req_id;
fdf23452 1966
287ac01a 1967 zfcp_fsf_start_erp_timer(req);
c41f8cbd
SS
1968 retval = zfcp_fsf_req_send(req);
1969 if (retval) {
1970 zfcp_fsf_req_free(req);
e60a6d69 1971 erp_action->fsf_req_id = 0;
c41f8cbd
SS
1972 }
1973out:
44a24cb3 1974 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1975 return retval;
1da177e4
LT
1976}
1977
c9615858
CS
1978static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
1979{
1980 lat_rec->sum += lat;
c41f8cbd
SS
1981 lat_rec->min = min(lat_rec->min, lat);
1982 lat_rec->max = max(lat_rec->max, lat);
c9615858
CS
1983}
1984
d9742b42 1985static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
c9615858 1986{
d9742b42
CS
1987 struct fsf_qual_latency_info *lat_in;
1988 struct latency_cont *lat = NULL;
d436de8c 1989 struct zfcp_scsi_dev *zfcp_sdev;
d9742b42
CS
1990 struct zfcp_blk_drv_data blktrc;
1991 int ticks = req->adapter->timer_ticks;
c9615858 1992
d9742b42 1993 lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
c9615858 1994
d9742b42
CS
1995 blktrc.flags = 0;
1996 blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
1997 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1998 blktrc.flags |= ZFCP_BLK_REQ_ERROR;
706eca49 1999 blktrc.inb_usage = 0;
34c2b712 2000 blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
d9742b42 2001
5bbf297c
CS
2002 if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
2003 !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
d436de8c 2004 zfcp_sdev = sdev_to_zfcp(scsi->device);
d9742b42
CS
2005 blktrc.flags |= ZFCP_BLK_LAT_VALID;
2006 blktrc.channel_lat = lat_in->channel_lat * ticks;
2007 blktrc.fabric_lat = lat_in->fabric_lat * ticks;
2008
2009 switch (req->qtcb->bottom.io.data_direction) {
ef3eb71d
FB
2010 case FSF_DATADIR_DIF_READ_STRIP:
2011 case FSF_DATADIR_DIF_READ_CONVERT:
d9742b42 2012 case FSF_DATADIR_READ:
b62a8d9b 2013 lat = &zfcp_sdev->latencies.read;
d9742b42 2014 break;
ef3eb71d
FB
2015 case FSF_DATADIR_DIF_WRITE_INSERT:
2016 case FSF_DATADIR_DIF_WRITE_CONVERT:
d9742b42 2017 case FSF_DATADIR_WRITE:
b62a8d9b 2018 lat = &zfcp_sdev->latencies.write;
d9742b42
CS
2019 break;
2020 case FSF_DATADIR_CMND:
b62a8d9b 2021 lat = &zfcp_sdev->latencies.cmd;
d9742b42
CS
2022 break;
2023 }
1da177e4 2024
d9742b42 2025 if (lat) {
b62a8d9b 2026 spin_lock(&zfcp_sdev->latencies.lock);
d9742b42
CS
2027 zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
2028 zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
2029 lat->counter++;
b62a8d9b 2030 spin_unlock(&zfcp_sdev->latencies.lock);
d9742b42 2031 }
0997f1c5 2032 }
0997f1c5 2033
d9742b42
CS
2034 blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
2035 sizeof(blktrc));
0997f1c5 2036}
0997f1c5 2037
c61b536c 2038static void zfcp_fsf_fcp_handler_common(struct zfcp_fsf_req *req)
c41f8cbd 2039{
b62a8d9b
CS
2040 struct scsi_cmnd *scmnd = req->data;
2041 struct scsi_device *sdev = scmnd->device;
d436de8c 2042 struct zfcp_scsi_dev *zfcp_sdev;
c41f8cbd
SS
2043 struct fsf_qtcb_header *header = &req->qtcb->header;
2044
c41f8cbd 2045 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
c61b536c 2046 return;
1da177e4 2047
d436de8c
MP
2048 zfcp_sdev = sdev_to_zfcp(sdev);
2049
c41f8cbd
SS
2050 switch (header->fsf_status) {
2051 case FSF_HANDLE_MISMATCH:
2052 case FSF_PORT_HANDLE_NOT_VALID:
ea4a3a6a 2053 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fssfch1");
c41f8cbd
SS
2054 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2055 break;
2056 case FSF_FCPLUN_NOT_VALID:
2057 case FSF_LUN_HANDLE_NOT_VALID:
ea4a3a6a 2058 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fssfch2");
c41f8cbd 2059 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2060 break;
c41f8cbd
SS
2061 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2062 zfcp_fsf_class_not_supp(req);
1da177e4 2063 break;
c41f8cbd
SS
2064 case FSF_DIRECTION_INDICATOR_NOT_VALID:
2065 dev_err(&req->adapter->ccw_device->dev,
b62a8d9b 2066 "Incorrect direction %d, LUN 0x%016Lx on port "
ff3b24fa 2067 "0x%016Lx closed\n",
c41f8cbd 2068 req->qtcb->bottom.io.data_direction,
b62a8d9b
CS
2069 (unsigned long long)zfcp_scsi_dev_lun(sdev),
2070 (unsigned long long)zfcp_sdev->port->wwpn);
2071 zfcp_erp_adapter_shutdown(zfcp_sdev->port->adapter, 0,
ea4a3a6a 2072 "fssfch3");
c41f8cbd
SS
2073 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2074 break;
2075 case FSF_CMND_LENGTH_NOT_VALID:
2076 dev_err(&req->adapter->ccw_device->dev,
b62a8d9b 2077 "Incorrect CDB length %d, LUN 0x%016Lx on "
ff3b24fa 2078 "port 0x%016Lx closed\n",
c41f8cbd 2079 req->qtcb->bottom.io.fcp_cmnd_length,
b62a8d9b
CS
2080 (unsigned long long)zfcp_scsi_dev_lun(sdev),
2081 (unsigned long long)zfcp_sdev->port->wwpn);
2082 zfcp_erp_adapter_shutdown(zfcp_sdev->port->adapter, 0,
ea4a3a6a 2083 "fssfch4");
c41f8cbd
SS
2084 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2085 break;
2086 case FSF_PORT_BOXED:
edaed859
SS
2087 zfcp_erp_set_port_status(zfcp_sdev->port,
2088 ZFCP_STATUS_COMMON_ACCESS_BOXED);
2089 zfcp_erp_port_reopen(zfcp_sdev->port,
ea4a3a6a 2090 ZFCP_STATUS_COMMON_ERP_FAILED, "fssfch5");
4c571c65 2091 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2092 break;
2093 case FSF_LUN_BOXED:
edaed859
SS
2094 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
2095 zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
ea4a3a6a 2096 "fssfch6");
4c571c65 2097 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2098 break;
2099 case FSF_ADAPTER_STATUS_AVAILABLE:
2100 if (header->fsf_status_qual.word[0] ==
2101 FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
b62a8d9b 2102 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 2103 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2104 break;
1da177e4 2105 }
c61b536c
CS
2106}
2107
2108static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req)
2109{
2110 struct scsi_cmnd *scpnt;
2111 struct fcp_resp_with_ext *fcp_rsp;
2112 unsigned long flags;
2113
c61b536c
CS
2114 read_lock_irqsave(&req->adapter->abort_lock, flags);
2115
2116 scpnt = req->data;
2117 if (unlikely(!scpnt)) {
2118 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2119 return;
c41f8cbd 2120 }
c61b536c 2121
5bfb2c31
SS
2122 zfcp_fsf_fcp_handler_common(req);
2123
c61b536c
CS
2124 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2125 set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
2126 goto skip_fsfstatus;
2127 }
2128
2129 switch (req->qtcb->header.fsf_status) {
2130 case FSF_INCONSISTENT_PROT_DATA:
2131 case FSF_INVALID_PROT_PARM:
2132 set_host_byte(scpnt, DID_ERROR);
2133 goto skip_fsfstatus;
2134 case FSF_BLOCK_GUARD_CHECK_FAILURE:
2135 zfcp_scsi_dif_sense_error(scpnt, 0x1);
2136 goto skip_fsfstatus;
2137 case FSF_APP_TAG_CHECK_FAILURE:
2138 zfcp_scsi_dif_sense_error(scpnt, 0x2);
2139 goto skip_fsfstatus;
2140 case FSF_REF_TAG_CHECK_FAILURE:
2141 zfcp_scsi_dif_sense_error(scpnt, 0x3);
2142 goto skip_fsfstatus;
2143 }
2144 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2145 zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
2146
2147skip_fsfstatus:
2148 zfcp_fsf_req_trace(req, scpnt);
250a1352 2149 zfcp_dbf_scsi_result(scpnt, req);
c61b536c
CS
2150
2151 scpnt->host_scribble = NULL;
2152 (scpnt->scsi_done) (scpnt);
2153 /*
2154 * We must hold this lock until scsi_done has been called.
2155 * Otherwise we may call scsi_done after abort regarding this
2156 * command has completed.
2157 * Note: scsi_done must not block!
2158 */
2159 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
1da177e4
LT
2160}
2161
ef3eb71d
FB
2162static int zfcp_fsf_set_data_dir(struct scsi_cmnd *scsi_cmnd, u32 *data_dir)
2163{
2164 switch (scsi_get_prot_op(scsi_cmnd)) {
2165 case SCSI_PROT_NORMAL:
2166 switch (scsi_cmnd->sc_data_direction) {
2167 case DMA_NONE:
2168 *data_dir = FSF_DATADIR_CMND;
2169 break;
2170 case DMA_FROM_DEVICE:
2171 *data_dir = FSF_DATADIR_READ;
2172 break;
2173 case DMA_TO_DEVICE:
2174 *data_dir = FSF_DATADIR_WRITE;
2175 break;
2176 case DMA_BIDIRECTIONAL:
2177 return -EINVAL;
2178 }
2179 break;
2180
2181 case SCSI_PROT_READ_STRIP:
2182 *data_dir = FSF_DATADIR_DIF_READ_STRIP;
2183 break;
2184 case SCSI_PROT_WRITE_INSERT:
2185 *data_dir = FSF_DATADIR_DIF_WRITE_INSERT;
2186 break;
2187 case SCSI_PROT_READ_PASS:
2188 *data_dir = FSF_DATADIR_DIF_READ_CONVERT;
2189 break;
2190 case SCSI_PROT_WRITE_PASS:
2191 *data_dir = FSF_DATADIR_DIF_WRITE_CONVERT;
2192 break;
2193 default:
2194 return -EINVAL;
2195 }
2196
2197 return 0;
2198}
2199
c41f8cbd 2200/**
b62a8d9b 2201 * zfcp_fsf_fcp_cmnd - initiate an FCP command (for a SCSI command)
c41f8cbd 2202 * @scsi_cmnd: scsi command to be sent
1da177e4 2203 */
b62a8d9b 2204int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd)
1da177e4 2205{
c41f8cbd 2206 struct zfcp_fsf_req *req;
4318e08c 2207 struct fcp_cmnd *fcp_cmnd;
3ec90878 2208 u8 sbtype = SBAL_SFLAGS0_TYPE_READ;
86a9668a 2209 int retval = -EIO;
b62a8d9b
CS
2210 struct scsi_device *sdev = scsi_cmnd->device;
2211 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2212 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
564e1c86 2213 struct zfcp_qdio *qdio = adapter->qdio;
ef3eb71d 2214 struct fsf_qtcb_bottom_io *io;
e55f8753 2215 unsigned long flags;
1da177e4 2216
b62a8d9b 2217 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
2218 ZFCP_STATUS_COMMON_UNBLOCKED)))
2219 return -EBUSY;
1da177e4 2220
e55f8753 2221 spin_lock_irqsave(&qdio->req_q_lock, flags);
706eca49 2222 if (atomic_read(&qdio->req_q_free) <= 0) {
564e1c86 2223 atomic_inc(&qdio->req_q_full);
c41f8cbd 2224 goto out;
8fdf30d5 2225 }
09a46c6e 2226
1674b405 2227 if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
3ec90878 2228 sbtype = SBAL_SFLAGS0_TYPE_WRITE;
1674b405 2229
564e1c86 2230 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
1674b405 2231 sbtype, adapter->pool.scsi_req);
09a46c6e 2232
025270f0 2233 if (IS_ERR(req)) {
c41f8cbd
SS
2234 retval = PTR_ERR(req);
2235 goto out;
2236 }
2237
ef3eb71d
FB
2238 scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2239
2240 io = &req->qtcb->bottom.io;
09a46c6e 2241 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
c41f8cbd 2242 req->data = scsi_cmnd;
c61b536c 2243 req->handler = zfcp_fsf_fcp_cmnd_handler;
b62a8d9b
CS
2244 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2245 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
ef3eb71d
FB
2246 io->service_class = FSF_CLASS_3;
2247 io->fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2248
ef3eb71d
FB
2249 if (scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) {
2250 io->data_block_length = scsi_cmnd->device->sector_size;
2251 io->ref_tag_value = scsi_get_lba(scsi_cmnd) & 0xFFFFFFFF;
1da177e4
LT
2252 }
2253
cc405ace
SM
2254 if (zfcp_fsf_set_data_dir(scsi_cmnd, &io->data_direction))
2255 goto failed_scsi_cmnd;
ef3eb71d 2256
4318e08c 2257 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
2443c8b2 2258 zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd, 0);
1da177e4 2259
71b8e45d
SM
2260 if ((scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) &&
2261 scsi_prot_sg_count(scsi_cmnd)) {
ef3eb71d
FB
2262 zfcp_qdio_set_data_div(qdio, &req->qdio_req,
2263 scsi_prot_sg_count(scsi_cmnd));
86a9668a
SS
2264 retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2265 scsi_prot_sglist(scsi_cmnd));
2266 if (retval)
2267 goto failed_scsi_cmnd;
2268 io->prot_data_length = zfcp_qdio_real_bytes(
ef3eb71d 2269 scsi_prot_sglist(scsi_cmnd));
ef3eb71d
FB
2270 }
2271
86a9668a
SS
2272 retval = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2273 scsi_sglist(scsi_cmnd));
2274 if (unlikely(retval))
c41f8cbd 2275 goto failed_scsi_cmnd;
1da177e4 2276
ef3eb71d 2277 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
86a9668a
SS
2278 if (zfcp_adapter_multi_buffer_active(adapter))
2279 zfcp_qdio_set_scount(qdio, &req->qdio_req);
ef3eb71d 2280
c41f8cbd
SS
2281 retval = zfcp_fsf_req_send(req);
2282 if (unlikely(retval))
2283 goto failed_scsi_cmnd;
1da177e4 2284
c41f8cbd 2285 goto out;
1da177e4 2286
c41f8cbd 2287failed_scsi_cmnd:
c41f8cbd
SS
2288 zfcp_fsf_req_free(req);
2289 scsi_cmnd->host_scribble = NULL;
2290out:
e55f8753 2291 spin_unlock_irqrestore(&qdio->req_q_lock, flags);
c41f8cbd 2292 return retval;
1da177e4
LT
2293}
2294
c61b536c
CS
2295static void zfcp_fsf_fcp_task_mgmt_handler(struct zfcp_fsf_req *req)
2296{
2297 struct fcp_resp_with_ext *fcp_rsp;
2298 struct fcp_resp_rsp_info *rsp_info;
2299
2300 zfcp_fsf_fcp_handler_common(req);
2301
2302 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2303 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
2304
2305 if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
2306 (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2307 req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2308}
2309
1da177e4 2310/**
b62a8d9b
CS
2311 * zfcp_fsf_fcp_task_mgmt - send SCSI task management command
2312 * @scmnd: SCSI command to send the task management command for
c41f8cbd 2313 * @tm_flags: unsigned byte for task management flags
c41f8cbd 2314 * Returns: on success pointer to struct fsf_req, NULL otherwise
1da177e4 2315 */
b62a8d9b
CS
2316struct zfcp_fsf_req *zfcp_fsf_fcp_task_mgmt(struct scsi_cmnd *scmnd,
2317 u8 tm_flags)
1da177e4 2318{
c41f8cbd 2319 struct zfcp_fsf_req *req = NULL;
4318e08c 2320 struct fcp_cmnd *fcp_cmnd;
b62a8d9b
CS
2321 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scmnd->device);
2322 struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
1da177e4 2323
b62a8d9b 2324 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
2325 ZFCP_STATUS_COMMON_UNBLOCKED)))
2326 return NULL;
1da177e4 2327
44a24cb3 2328 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 2329 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 2330 goto out;
09a46c6e 2331
564e1c86 2332 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
3ec90878 2333 SBAL_SFLAGS0_TYPE_WRITE,
564e1c86 2334 qdio->adapter->pool.scsi_req);
09a46c6e 2335
633528c3
SS
2336 if (IS_ERR(req)) {
2337 req = NULL;
c41f8cbd 2338 goto out;
633528c3 2339 }
1da177e4 2340
b62a8d9b 2341 req->data = scmnd;
c61b536c 2342 req->handler = zfcp_fsf_fcp_task_mgmt_handler;
b62a8d9b
CS
2343 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2344 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
c41f8cbd
SS
2345 req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2346 req->qtcb->bottom.io.service_class = FSF_CLASS_3;
4318e08c 2347 req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2348
1674b405 2349 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 2350
4318e08c 2351 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
2443c8b2 2352 zfcp_fc_scsi_to_fcp(fcp_cmnd, scmnd, tm_flags);
1da177e4 2353
c41f8cbd
SS
2354 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2355 if (!zfcp_fsf_req_send(req))
2356 goto out;
1da177e4 2357
c41f8cbd
SS
2358 zfcp_fsf_req_free(req);
2359 req = NULL;
2360out:
44a24cb3 2361 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd
SS
2362 return req;
2363}
1da177e4 2364
bd63eaf4
SS
2365/**
2366 * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2367 * @adapter: pointer to struct zfcp_adapter
2368 * @sbal_idx: response queue index of SBAL to be processed
2369 */
564e1c86 2370void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
bd63eaf4 2371{
564e1c86 2372 struct zfcp_adapter *adapter = qdio->adapter;
706eca49 2373 struct qdio_buffer *sbal = qdio->res_q[sbal_idx];
bd63eaf4
SS
2374 struct qdio_buffer_element *sbale;
2375 struct zfcp_fsf_req *fsf_req;
b6bd2fb9 2376 unsigned long req_id;
bd63eaf4
SS
2377 int idx;
2378
2379 for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2380
2381 sbale = &sbal->element[idx];
2382 req_id = (unsigned long) sbale->addr;
b6bd2fb9 2383 fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
bd63eaf4 2384
339f4f4e 2385 if (!fsf_req) {
bd63eaf4
SS
2386 /*
2387 * Unknown request means that we have potentially memory
2388 * corruption and must stop the machine immediately.
2389 */
339f4f4e 2390 zfcp_qdio_siosl(adapter);
bd63eaf4
SS
2391 panic("error: unknown req_id (%lx) on adapter %s.\n",
2392 req_id, dev_name(&adapter->ccw_device->dev));
339f4f4e 2393 }
bd63eaf4 2394
34c2b712 2395 fsf_req->qdio_req.sbal_response = sbal_idx;
bd63eaf4
SS
2396 zfcp_fsf_req_complete(fsf_req);
2397
3ec90878 2398 if (likely(sbale->eflags & SBAL_EFLAGS_LAST_ENTRY))
bd63eaf4
SS
2399 break;
2400 }
2401}