[SCSI] zfcp: Replace fsf_req wait_queue with completion
[safe/jmp/linux-2.6] / drivers / s390 / scsi / zfcp_fsf.c
1 /*
2  * zfcp device driver
3  *
4  * Implementation of FSF commands.
5  *
6  * Copyright IBM Corporation 2002, 2009
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/blktrace_api.h>
13 #include "zfcp_ext.h"
14 #include "zfcp_dbf.h"
15
16 #define ZFCP_REQ_AUTO_CLEANUP   0x00000002
17 #define ZFCP_REQ_NO_QTCB        0x00000008
18
19 static void zfcp_fsf_request_timeout_handler(unsigned long data)
20 {
21         struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
22         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
23                                 "fsrth_1", NULL);
24 }
25
26 static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
27                                  unsigned long timeout)
28 {
29         fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
30         fsf_req->timer.data = (unsigned long) fsf_req->adapter;
31         fsf_req->timer.expires = jiffies + timeout;
32         add_timer(&fsf_req->timer);
33 }
34
35 static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
36 {
37         BUG_ON(!fsf_req->erp_action);
38         fsf_req->timer.function = zfcp_erp_timeout_handler;
39         fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
40         fsf_req->timer.expires = jiffies + 30 * HZ;
41         add_timer(&fsf_req->timer);
42 }
43
44 /* association between FSF command and FSF QTCB type */
45 static u32 fsf_qtcb_type[] = {
46         [FSF_QTCB_FCP_CMND] =             FSF_IO_COMMAND,
47         [FSF_QTCB_ABORT_FCP_CMND] =       FSF_SUPPORT_COMMAND,
48         [FSF_QTCB_OPEN_PORT_WITH_DID] =   FSF_SUPPORT_COMMAND,
49         [FSF_QTCB_OPEN_LUN] =             FSF_SUPPORT_COMMAND,
50         [FSF_QTCB_CLOSE_LUN] =            FSF_SUPPORT_COMMAND,
51         [FSF_QTCB_CLOSE_PORT] =           FSF_SUPPORT_COMMAND,
52         [FSF_QTCB_CLOSE_PHYSICAL_PORT] =  FSF_SUPPORT_COMMAND,
53         [FSF_QTCB_SEND_ELS] =             FSF_SUPPORT_COMMAND,
54         [FSF_QTCB_SEND_GENERIC] =         FSF_SUPPORT_COMMAND,
55         [FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
56         [FSF_QTCB_EXCHANGE_PORT_DATA] =   FSF_PORT_COMMAND,
57         [FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
58         [FSF_QTCB_UPLOAD_CONTROL_FILE] =  FSF_SUPPORT_COMMAND
59 };
60
61 static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
62 {
63         u16 subtable = table >> 16;
64         u16 rule = table & 0xffff;
65         const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
66
67         if (subtable && subtable < ARRAY_SIZE(act_type))
68                 dev_warn(&adapter->ccw_device->dev,
69                          "Access denied according to ACT rule type %s, "
70                          "rule %d\n", act_type[subtable], rule);
71 }
72
73 static void zfcp_fsf_access_denied_port(struct zfcp_fsf_req *req,
74                                         struct zfcp_port *port)
75 {
76         struct fsf_qtcb_header *header = &req->qtcb->header;
77         dev_warn(&req->adapter->ccw_device->dev,
78                  "Access denied to port 0x%016Lx\n",
79                  (unsigned long long)port->wwpn);
80         zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
81         zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
82         zfcp_erp_port_access_denied(port, "fspad_1", req);
83         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
84 }
85
86 static void zfcp_fsf_access_denied_unit(struct zfcp_fsf_req *req,
87                                         struct zfcp_unit *unit)
88 {
89         struct fsf_qtcb_header *header = &req->qtcb->header;
90         dev_warn(&req->adapter->ccw_device->dev,
91                  "Access denied to unit 0x%016Lx on port 0x%016Lx\n",
92                  (unsigned long long)unit->fcp_lun,
93                  (unsigned long long)unit->port->wwpn);
94         zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
95         zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
96         zfcp_erp_unit_access_denied(unit, "fsuad_1", req);
97         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
98 }
99
100 static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
101 {
102         dev_err(&req->adapter->ccw_device->dev, "FCP device not "
103                 "operational because of an unsupported FC class\n");
104         zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1", req);
105         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
106 }
107
108 /**
109  * zfcp_fsf_req_free - free memory used by fsf request
110  * @fsf_req: pointer to struct zfcp_fsf_req
111  */
112 void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
113 {
114         if (likely(req->pool)) {
115                 mempool_free(req, req->pool);
116                 return;
117         }
118
119         if (req->qtcb) {
120                 kmem_cache_free(zfcp_data.fsf_req_qtcb_cache, req);
121                 return;
122         }
123 }
124
125 static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
126 {
127         struct fsf_status_read_buffer *sr_buf = req->data;
128         struct zfcp_adapter *adapter = req->adapter;
129         struct zfcp_port *port;
130         int d_id = sr_buf->d_id & ZFCP_DID_MASK;
131         unsigned long flags;
132
133         read_lock_irqsave(&zfcp_data.config_lock, flags);
134         list_for_each_entry(port, &adapter->port_list_head, list)
135                 if (port->d_id == d_id) {
136                         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
137                         zfcp_erp_port_reopen(port, 0, "fssrpc1", req);
138                         return;
139                 }
140         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
141 }
142
143 static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req, char *id,
144                                          struct fsf_link_down_info *link_down)
145 {
146         struct zfcp_adapter *adapter = req->adapter;
147         unsigned long flags;
148
149         if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
150                 return;
151
152         atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
153
154         read_lock_irqsave(&zfcp_data.config_lock, flags);
155         zfcp_scsi_schedule_rports_block(adapter);
156         read_unlock_irqrestore(&zfcp_data.config_lock, flags);
157
158         if (!link_down)
159                 goto out;
160
161         switch (link_down->error_code) {
162         case FSF_PSQ_LINK_NO_LIGHT:
163                 dev_warn(&req->adapter->ccw_device->dev,
164                          "There is no light signal from the local "
165                          "fibre channel cable\n");
166                 break;
167         case FSF_PSQ_LINK_WRAP_PLUG:
168                 dev_warn(&req->adapter->ccw_device->dev,
169                          "There is a wrap plug instead of a fibre "
170                          "channel cable\n");
171                 break;
172         case FSF_PSQ_LINK_NO_FCP:
173                 dev_warn(&req->adapter->ccw_device->dev,
174                          "The adjacent fibre channel node does not "
175                          "support FCP\n");
176                 break;
177         case FSF_PSQ_LINK_FIRMWARE_UPDATE:
178                 dev_warn(&req->adapter->ccw_device->dev,
179                          "The FCP device is suspended because of a "
180                          "firmware update\n");
181                 break;
182         case FSF_PSQ_LINK_INVALID_WWPN:
183                 dev_warn(&req->adapter->ccw_device->dev,
184                          "The FCP device detected a WWPN that is "
185                          "duplicate or not valid\n");
186                 break;
187         case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
188                 dev_warn(&req->adapter->ccw_device->dev,
189                          "The fibre channel fabric does not support NPIV\n");
190                 break;
191         case FSF_PSQ_LINK_NO_FCP_RESOURCES:
192                 dev_warn(&req->adapter->ccw_device->dev,
193                          "The FCP adapter cannot support more NPIV ports\n");
194                 break;
195         case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
196                 dev_warn(&req->adapter->ccw_device->dev,
197                          "The adjacent switch cannot support "
198                          "more NPIV ports\n");
199                 break;
200         case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
201                 dev_warn(&req->adapter->ccw_device->dev,
202                          "The FCP adapter could not log in to the "
203                          "fibre channel fabric\n");
204                 break;
205         case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
206                 dev_warn(&req->adapter->ccw_device->dev,
207                          "The WWPN assignment file on the FCP adapter "
208                          "has been damaged\n");
209                 break;
210         case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
211                 dev_warn(&req->adapter->ccw_device->dev,
212                          "The mode table on the FCP adapter "
213                          "has been damaged\n");
214                 break;
215         case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
216                 dev_warn(&req->adapter->ccw_device->dev,
217                          "All NPIV ports on the FCP adapter have "
218                          "been assigned\n");
219                 break;
220         default:
221                 dev_warn(&req->adapter->ccw_device->dev,
222                          "The link between the FCP adapter and "
223                          "the FC fabric is down\n");
224         }
225 out:
226         zfcp_erp_adapter_failed(adapter, id, req);
227 }
228
229 static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
230 {
231         struct fsf_status_read_buffer *sr_buf = req->data;
232         struct fsf_link_down_info *ldi =
233                 (struct fsf_link_down_info *) &sr_buf->payload;
234
235         switch (sr_buf->status_subtype) {
236         case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
237                 zfcp_fsf_link_down_info_eval(req, "fssrld1", ldi);
238                 break;
239         case FSF_STATUS_READ_SUB_FDISC_FAILED:
240                 zfcp_fsf_link_down_info_eval(req, "fssrld2", ldi);
241                 break;
242         case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
243                 zfcp_fsf_link_down_info_eval(req, "fssrld3", NULL);
244         };
245 }
246
247 static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
248 {
249         struct zfcp_adapter *adapter = req->adapter;
250         struct fsf_status_read_buffer *sr_buf = req->data;
251
252         if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
253                 zfcp_hba_dbf_event_fsf_unsol("dism", adapter, sr_buf);
254                 mempool_free(sr_buf, adapter->pool.data_status_read);
255                 zfcp_fsf_req_free(req);
256                 return;
257         }
258
259         zfcp_hba_dbf_event_fsf_unsol("read", adapter, sr_buf);
260
261         switch (sr_buf->status_type) {
262         case FSF_STATUS_READ_PORT_CLOSED:
263                 zfcp_fsf_status_read_port_closed(req);
264                 break;
265         case FSF_STATUS_READ_INCOMING_ELS:
266                 zfcp_fc_incoming_els(req);
267                 break;
268         case FSF_STATUS_READ_SENSE_DATA_AVAIL:
269                 break;
270         case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
271                 dev_warn(&adapter->ccw_device->dev,
272                          "The error threshold for checksum statistics "
273                          "has been exceeded\n");
274                 zfcp_hba_dbf_event_berr(adapter, req);
275                 break;
276         case FSF_STATUS_READ_LINK_DOWN:
277                 zfcp_fsf_status_read_link_down(req);
278                 break;
279         case FSF_STATUS_READ_LINK_UP:
280                 dev_info(&adapter->ccw_device->dev,
281                          "The local link has been restored\n");
282                 /* All ports should be marked as ready to run again */
283                 zfcp_erp_modify_adapter_status(adapter, "fssrh_1", NULL,
284                                                ZFCP_STATUS_COMMON_RUNNING,
285                                                ZFCP_SET);
286                 zfcp_erp_adapter_reopen(adapter,
287                                         ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
288                                         ZFCP_STATUS_COMMON_ERP_FAILED,
289                                         "fssrh_2", req);
290                 break;
291         case FSF_STATUS_READ_NOTIFICATION_LOST:
292                 if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_ACT_UPDATED)
293                         zfcp_erp_adapter_access_changed(adapter, "fssrh_3",
294                                                         req);
295                 if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
296                         schedule_work(&adapter->scan_work);
297                 break;
298         case FSF_STATUS_READ_CFDC_UPDATED:
299                 zfcp_erp_adapter_access_changed(adapter, "fssrh_4", req);
300                 break;
301         case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
302                 adapter->adapter_features = sr_buf->payload.word[0];
303                 break;
304         }
305
306         mempool_free(sr_buf, adapter->pool.data_status_read);
307         zfcp_fsf_req_free(req);
308
309         atomic_inc(&adapter->stat_miss);
310         queue_work(zfcp_data.work_queue, &adapter->stat_work);
311 }
312
313 static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
314 {
315         switch (req->qtcb->header.fsf_status_qual.word[0]) {
316         case FSF_SQ_FCP_RSP_AVAILABLE:
317         case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
318         case FSF_SQ_NO_RETRY_POSSIBLE:
319         case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
320                 return;
321         case FSF_SQ_COMMAND_ABORTED:
322                 req->status |= ZFCP_STATUS_FSFREQ_ABORTED;
323                 break;
324         case FSF_SQ_NO_RECOM:
325                 dev_err(&req->adapter->ccw_device->dev,
326                         "The FCP adapter reported a problem "
327                         "that cannot be recovered\n");
328                 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1", req);
329                 break;
330         }
331         /* all non-return stats set FSFREQ_ERROR*/
332         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
333 }
334
335 static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
336 {
337         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
338                 return;
339
340         switch (req->qtcb->header.fsf_status) {
341         case FSF_UNKNOWN_COMMAND:
342                 dev_err(&req->adapter->ccw_device->dev,
343                         "The FCP adapter does not recognize the command 0x%x\n",
344                         req->qtcb->header.fsf_command);
345                 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1", req);
346                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
347                 break;
348         case FSF_ADAPTER_STATUS_AVAILABLE:
349                 zfcp_fsf_fsfstatus_qual_eval(req);
350                 break;
351         }
352 }
353
354 static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
355 {
356         struct zfcp_adapter *adapter = req->adapter;
357         struct fsf_qtcb *qtcb = req->qtcb;
358         union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
359
360         zfcp_hba_dbf_event_fsf_response(req);
361
362         if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
363                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
364                         ZFCP_STATUS_FSFREQ_RETRY; /* only for SCSI cmnds. */
365                 return;
366         }
367
368         switch (qtcb->prefix.prot_status) {
369         case FSF_PROT_GOOD:
370         case FSF_PROT_FSF_STATUS_PRESENTED:
371                 return;
372         case FSF_PROT_QTCB_VERSION_ERROR:
373                 dev_err(&adapter->ccw_device->dev,
374                         "QTCB version 0x%x not supported by FCP adapter "
375                         "(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
376                         psq->word[0], psq->word[1]);
377                 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1", req);
378                 break;
379         case FSF_PROT_ERROR_STATE:
380         case FSF_PROT_SEQ_NUMB_ERROR:
381                 zfcp_erp_adapter_reopen(adapter, 0, "fspse_2", req);
382                 req->status |= ZFCP_STATUS_FSFREQ_RETRY;
383                 break;
384         case FSF_PROT_UNSUPP_QTCB_TYPE:
385                 dev_err(&adapter->ccw_device->dev,
386                         "The QTCB type is not supported by the FCP adapter\n");
387                 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3", req);
388                 break;
389         case FSF_PROT_HOST_CONNECTION_INITIALIZING:
390                 atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
391                                 &adapter->status);
392                 break;
393         case FSF_PROT_DUPLICATE_REQUEST_ID:
394                 dev_err(&adapter->ccw_device->dev,
395                         "0x%Lx is an ambiguous request identifier\n",
396                         (unsigned long long)qtcb->bottom.support.req_handle);
397                 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4", req);
398                 break;
399         case FSF_PROT_LINK_DOWN:
400                 zfcp_fsf_link_down_info_eval(req, "fspse_5",
401                                              &psq->link_down_info);
402                 /* FIXME: reopening adapter now? better wait for link up */
403                 zfcp_erp_adapter_reopen(adapter, 0, "fspse_6", req);
404                 break;
405         case FSF_PROT_REEST_QUEUE:
406                 /* All ports should be marked as ready to run again */
407                 zfcp_erp_modify_adapter_status(adapter, "fspse_7", NULL,
408                                                ZFCP_STATUS_COMMON_RUNNING,
409                                                ZFCP_SET);
410                 zfcp_erp_adapter_reopen(adapter,
411                                         ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
412                                         ZFCP_STATUS_COMMON_ERP_FAILED,
413                                         "fspse_8", req);
414                 break;
415         default:
416                 dev_err(&adapter->ccw_device->dev,
417                         "0x%x is not a valid transfer protocol status\n",
418                         qtcb->prefix.prot_status);
419                 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9", req);
420         }
421         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
422 }
423
424 /**
425  * zfcp_fsf_req_complete - process completion of a FSF request
426  * @fsf_req: The FSF request that has been completed.
427  *
428  * When a request has been completed either from the FCP adapter,
429  * or it has been dismissed due to a queue shutdown, this function
430  * is called to process the completion status and trigger further
431  * events related to the FSF request.
432  */
433 static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
434 {
435         if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
436                 zfcp_fsf_status_read_handler(req);
437                 return;
438         }
439
440         del_timer(&req->timer);
441         zfcp_fsf_protstatus_eval(req);
442         zfcp_fsf_fsfstatus_eval(req);
443         req->handler(req);
444
445         if (req->erp_action)
446                 zfcp_erp_notify(req->erp_action, 0);
447
448         if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
449                 zfcp_fsf_req_free(req);
450         else
451                 complete(&req->completion);
452 }
453
454 /**
455  * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
456  * @adapter: pointer to struct zfcp_adapter
457  *
458  * Never ever call this without shutting down the adapter first.
459  * Otherwise the adapter would continue using and corrupting s390 storage.
460  * Included BUG_ON() call to ensure this is done.
461  * ERP is supposed to be the only user of this function.
462  */
463 void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
464 {
465         struct zfcp_fsf_req *req, *tmp;
466         unsigned long flags;
467         LIST_HEAD(remove_queue);
468         unsigned int i;
469
470         BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
471         spin_lock_irqsave(&adapter->req_list_lock, flags);
472         for (i = 0; i < REQUEST_LIST_SIZE; i++)
473                 list_splice_init(&adapter->req_list[i], &remove_queue);
474         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
475
476         list_for_each_entry_safe(req, tmp, &remove_queue, list) {
477                 list_del(&req->list);
478                 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
479                 zfcp_fsf_req_complete(req);
480         }
481 }
482
483 static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
484 {
485         struct fsf_qtcb_bottom_config *bottom;
486         struct zfcp_adapter *adapter = req->adapter;
487         struct Scsi_Host *shost = adapter->scsi_host;
488
489         bottom = &req->qtcb->bottom.config;
490
491         if (req->data)
492                 memcpy(req->data, bottom, sizeof(*bottom));
493
494         fc_host_node_name(shost) = bottom->nport_serv_param.wwnn;
495         fc_host_port_name(shost) = bottom->nport_serv_param.wwpn;
496         fc_host_port_id(shost) = bottom->s_id & ZFCP_DID_MASK;
497         fc_host_speed(shost) = bottom->fc_link_speed;
498         fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
499
500         adapter->hydra_version = bottom->adapter_type;
501         adapter->timer_ticks = bottom->timer_interval;
502
503         if (fc_host_permanent_port_name(shost) == -1)
504                 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
505
506         switch (bottom->fc_topology) {
507         case FSF_TOPO_P2P:
508                 adapter->peer_d_id = bottom->peer_d_id & ZFCP_DID_MASK;
509                 adapter->peer_wwpn = bottom->plogi_payload.wwpn;
510                 adapter->peer_wwnn = bottom->plogi_payload.wwnn;
511                 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
512                 break;
513         case FSF_TOPO_FABRIC:
514                 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
515                 break;
516         case FSF_TOPO_AL:
517                 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
518                 /* fall through */
519         default:
520                 dev_err(&adapter->ccw_device->dev,
521                         "Unknown or unsupported arbitrated loop "
522                         "fibre channel topology detected\n");
523                 zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1", req);
524                 return -EIO;
525         }
526
527         return 0;
528 }
529
530 static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
531 {
532         struct zfcp_adapter *adapter = req->adapter;
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;
536
537         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
538                 return;
539
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;
546
547         switch (qtcb->header.fsf_status) {
548         case FSF_GOOD:
549                 if (zfcp_fsf_exchange_config_evaluate(req))
550                         return;
551
552                 if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
553                         dev_err(&adapter->ccw_device->dev,
554                                 "FCP adapter maximum QTCB size (%d bytes) "
555                                 "is too small\n",
556                                 bottom->max_qtcb_size);
557                         zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
558                         return;
559                 }
560                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
561                                 &adapter->status);
562                 break;
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;
570
571                 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
572                                 &adapter->status);
573
574                 zfcp_fsf_link_down_info_eval(req, "fsecdh2",
575                         &qtcb->header.fsf_status_qual.link_down_info);
576                 break;
577         default:
578                 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3", req);
579                 return;
580         }
581
582         if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
583                 adapter->hardware_version = bottom->hardware_version;
584                 memcpy(fc_host_serial_number(shost), bottom->serial_number,
585                        min(FC_SERIAL_NUMBER_SIZE, 17));
586                 EBCASC(fc_host_serial_number(shost),
587                        min(FC_SERIAL_NUMBER_SIZE, 17));
588         }
589
590         if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
591                 dev_err(&adapter->ccw_device->dev,
592                         "The FCP adapter only supports newer "
593                         "control block versions\n");
594                 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4", req);
595                 return;
596         }
597         if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
598                 dev_err(&adapter->ccw_device->dev,
599                         "The FCP adapter only supports older "
600                         "control block versions\n");
601                 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5", req);
602         }
603 }
604
605 static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
606 {
607         struct zfcp_adapter *adapter = req->adapter;
608         struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
609         struct Scsi_Host *shost = adapter->scsi_host;
610
611         if (req->data)
612                 memcpy(req->data, bottom, sizeof(*bottom));
613
614         if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
615                 fc_host_permanent_port_name(shost) = bottom->wwpn;
616                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
617         } else
618                 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
619         fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
620         fc_host_supported_speeds(shost) = bottom->supported_speed;
621 }
622
623 static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
624 {
625         struct fsf_qtcb *qtcb = req->qtcb;
626
627         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
628                 return;
629
630         switch (qtcb->header.fsf_status) {
631         case FSF_GOOD:
632                 zfcp_fsf_exchange_port_evaluate(req);
633                 break;
634         case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
635                 zfcp_fsf_exchange_port_evaluate(req);
636                 zfcp_fsf_link_down_info_eval(req, "fsepdh1",
637                         &qtcb->header.fsf_status_qual.link_down_info);
638                 break;
639         }
640 }
641
642 static int zfcp_fsf_sbal_check(struct zfcp_adapter *adapter)
643 {
644         struct zfcp_qdio_queue *req_q = &adapter->req_q;
645
646         spin_lock_bh(&adapter->req_q_lock);
647         if (atomic_read(&req_q->count))
648                 return 1;
649         spin_unlock_bh(&adapter->req_q_lock);
650         return 0;
651 }
652
653 static int zfcp_fsf_req_sbal_get(struct zfcp_adapter *adapter)
654 {
655         long ret;
656
657         spin_unlock_bh(&adapter->req_q_lock);
658         ret = wait_event_interruptible_timeout(adapter->request_wq,
659                                zfcp_fsf_sbal_check(adapter), 5 * HZ);
660         if (ret > 0)
661                 return 0;
662         if (!ret) {
663                 atomic_inc(&adapter->qdio_outb_full);
664                 /* assume hanging outbound queue, try queue recovery */
665                 zfcp_erp_adapter_reopen(adapter, 0, "fsrsg_1", NULL);
666         }
667
668         spin_lock_bh(&adapter->req_q_lock);
669         return -EIO;
670 }
671
672 static struct zfcp_fsf_req *zfcp_fsf_alloc_noqtcb(mempool_t *pool)
673 {
674         struct zfcp_fsf_req *req;
675         req = mempool_alloc(pool, GFP_ATOMIC);
676         if (!req)
677                 return NULL;
678         memset(req, 0, sizeof(*req));
679         req->pool = pool;
680         return req;
681 }
682
683 static struct zfcp_fsf_req *zfcp_fsf_alloc_qtcb(mempool_t *pool)
684 {
685         struct zfcp_fsf_req_qtcb *qtcb;
686
687         if (likely(pool))
688                 qtcb = mempool_alloc(pool, GFP_ATOMIC);
689         else
690                 qtcb = kmem_cache_alloc(zfcp_data.fsf_req_qtcb_cache,
691                                         GFP_ATOMIC);
692         if (unlikely(!qtcb))
693                 return NULL;
694
695         memset(qtcb, 0, sizeof(*qtcb));
696         qtcb->fsf_req.qtcb = &qtcb->qtcb;
697         qtcb->fsf_req.pool = pool;
698
699         return &qtcb->fsf_req;
700 }
701
702 static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_adapter *adapter,
703                                                 u32 fsf_cmd, int req_flags,
704                                                 mempool_t *pool)
705 {
706         struct qdio_buffer_element *sbale;
707
708         struct zfcp_fsf_req *req;
709         struct zfcp_qdio_queue *req_q = &adapter->req_q;
710
711         if (req_flags & ZFCP_REQ_NO_QTCB)
712                 req = zfcp_fsf_alloc_noqtcb(pool);
713         else
714                 req = zfcp_fsf_alloc_qtcb(pool);
715
716         if (unlikely(!req))
717                 return ERR_PTR(-ENOMEM);
718
719         if (adapter->req_no == 0)
720                 adapter->req_no++;
721
722         INIT_LIST_HEAD(&req->list);
723         init_timer(&req->timer);
724         init_completion(&req->completion);
725
726         req->adapter = adapter;
727         req->fsf_command = fsf_cmd;
728         req->req_id = adapter->req_no;
729         req->sbal_number = 1;
730         req->sbal_first = req_q->first;
731         req->sbal_last = req_q->first;
732         req->sbale_curr = 1;
733
734         sbale = zfcp_qdio_sbale_req(req);
735         sbale[0].addr = (void *) req->req_id;
736         sbale[0].flags |= SBAL_FLAGS0_COMMAND;
737
738         if (likely(req->qtcb)) {
739                 req->qtcb->prefix.req_seq_no = req->adapter->fsf_req_seq_no;
740                 req->qtcb->prefix.req_id = req->req_id;
741                 req->qtcb->prefix.ulp_info = 26;
742                 req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
743                 req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
744                 req->qtcb->header.req_handle = req->req_id;
745                 req->qtcb->header.fsf_command = req->fsf_command;
746                 req->seq_no = adapter->fsf_req_seq_no;
747                 req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
748                 sbale[1].addr = (void *) req->qtcb;
749                 sbale[1].length = sizeof(struct fsf_qtcb);
750         }
751
752         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) {
753                 zfcp_fsf_req_free(req);
754                 return ERR_PTR(-EIO);
755         }
756
757         if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP))
758                 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
759
760         return req;
761 }
762
763 static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
764 {
765         struct zfcp_adapter *adapter = req->adapter;
766         unsigned long        flags;
767         int                  idx;
768         int                  with_qtcb = (req->qtcb != NULL);
769
770         /* put allocated FSF request into hash table */
771         spin_lock_irqsave(&adapter->req_list_lock, flags);
772         idx = zfcp_reqlist_hash(req->req_id);
773         list_add_tail(&req->list, &adapter->req_list[idx]);
774         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
775
776         req->qdio_outb_usage = atomic_read(&adapter->req_q.count);
777         req->issued = get_clock();
778         if (zfcp_qdio_send(req)) {
779                 del_timer(&req->timer);
780                 spin_lock_irqsave(&adapter->req_list_lock, flags);
781                 /* lookup request again, list might have changed */
782                 if (zfcp_reqlist_find_safe(adapter, req))
783                         zfcp_reqlist_remove(adapter, req);
784                 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
785                 zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
786                 return -EIO;
787         }
788
789         /* Don't increase for unsolicited status */
790         if (with_qtcb)
791                 adapter->fsf_req_seq_no++;
792         adapter->req_no++;
793
794         return 0;
795 }
796
797 /**
798  * zfcp_fsf_status_read - send status read request
799  * @adapter: pointer to struct zfcp_adapter
800  * @req_flags: request flags
801  * Returns: 0 on success, ERROR otherwise
802  */
803 int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
804 {
805         struct zfcp_fsf_req *req;
806         struct fsf_status_read_buffer *sr_buf;
807         struct qdio_buffer_element *sbale;
808         int retval = -EIO;
809
810         spin_lock_bh(&adapter->req_q_lock);
811         if (zfcp_fsf_req_sbal_get(adapter))
812                 goto out;
813
814         req = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
815                                   ZFCP_REQ_NO_QTCB,
816                                   adapter->pool.fsf_req_status_read);
817         if (IS_ERR(req)) {
818                 retval = PTR_ERR(req);
819                 goto out;
820         }
821
822         sbale = zfcp_qdio_sbale_req(req);
823         sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
824         req->sbale_curr = 2;
825
826         sr_buf = mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
827         if (!sr_buf) {
828                 retval = -ENOMEM;
829                 goto failed_buf;
830         }
831         memset(sr_buf, 0, sizeof(*sr_buf));
832         req->data = sr_buf;
833         sbale = zfcp_qdio_sbale_curr(req);
834         sbale->addr = (void *) sr_buf;
835         sbale->length = sizeof(*sr_buf);
836
837         retval = zfcp_fsf_req_send(req);
838         if (retval)
839                 goto failed_req_send;
840
841         goto out;
842
843 failed_req_send:
844         mempool_free(sr_buf, adapter->pool.data_status_read);
845 failed_buf:
846         zfcp_fsf_req_free(req);
847         zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
848 out:
849         spin_unlock_bh(&adapter->req_q_lock);
850         return retval;
851 }
852
853 static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
854 {
855         struct zfcp_unit *unit = req->data;
856         union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
857
858         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
859                 return;
860
861         switch (req->qtcb->header.fsf_status) {
862         case FSF_PORT_HANDLE_NOT_VALID:
863                 if (fsq->word[0] == fsq->word[1]) {
864                         zfcp_erp_adapter_reopen(unit->port->adapter, 0,
865                                                 "fsafch1", req);
866                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
867                 }
868                 break;
869         case FSF_LUN_HANDLE_NOT_VALID:
870                 if (fsq->word[0] == fsq->word[1]) {
871                         zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
872                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
873                 }
874                 break;
875         case FSF_FCP_COMMAND_DOES_NOT_EXIST:
876                 req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
877                 break;
878         case FSF_PORT_BOXED:
879                 zfcp_erp_port_boxed(unit->port, "fsafch3", req);
880                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
881                                ZFCP_STATUS_FSFREQ_RETRY;
882                 break;
883         case FSF_LUN_BOXED:
884                 zfcp_erp_unit_boxed(unit, "fsafch4", req);
885                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
886                                ZFCP_STATUS_FSFREQ_RETRY;
887                 break;
888         case FSF_ADAPTER_STATUS_AVAILABLE:
889                 switch (fsq->word[0]) {
890                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
891                         zfcp_test_link(unit->port);
892                         /* fall through */
893                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
894                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
895                         break;
896                 }
897                 break;
898         case FSF_GOOD:
899                 req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
900                 break;
901         }
902 }
903
904 /**
905  * zfcp_fsf_abort_fcp_command - abort running SCSI command
906  * @old_req_id: unsigned long
907  * @unit: pointer to struct zfcp_unit
908  * Returns: pointer to struct zfcp_fsf_req
909  */
910
911 struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
912                                                 struct zfcp_unit *unit)
913 {
914         struct qdio_buffer_element *sbale;
915         struct zfcp_fsf_req *req = NULL;
916         struct zfcp_adapter *adapter = unit->port->adapter;
917
918         spin_lock_bh(&adapter->req_q_lock);
919         if (zfcp_fsf_req_sbal_get(adapter))
920                 goto out;
921         req = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
922                                   0, adapter->pool.fsf_req_abort);
923         if (IS_ERR(req)) {
924                 req = NULL;
925                 goto out;
926         }
927
928         if (unlikely(!(atomic_read(&unit->status) &
929                        ZFCP_STATUS_COMMON_UNBLOCKED)))
930                 goto out_error_free;
931
932         sbale = zfcp_qdio_sbale_req(req);
933         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
934         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
935
936         req->data = unit;
937         req->handler = zfcp_fsf_abort_fcp_command_handler;
938         req->qtcb->header.lun_handle = unit->handle;
939         req->qtcb->header.port_handle = unit->port->handle;
940         req->qtcb->bottom.support.req_handle = (u64) old_req_id;
941
942         zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
943         if (!zfcp_fsf_req_send(req))
944                 goto out;
945
946 out_error_free:
947         zfcp_fsf_req_free(req);
948         req = NULL;
949 out:
950         spin_unlock_bh(&adapter->req_q_lock);
951         return req;
952 }
953
954 static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
955 {
956         struct zfcp_adapter *adapter = req->adapter;
957         struct zfcp_send_ct *send_ct = req->data;
958         struct fsf_qtcb_header *header = &req->qtcb->header;
959
960         send_ct->status = -EINVAL;
961
962         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
963                 goto skip_fsfstatus;
964
965         switch (header->fsf_status) {
966         case FSF_GOOD:
967                 zfcp_san_dbf_event_ct_response(req);
968                 send_ct->status = 0;
969                 break;
970         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
971                 zfcp_fsf_class_not_supp(req);
972                 break;
973         case FSF_ADAPTER_STATUS_AVAILABLE:
974                 switch (header->fsf_status_qual.word[0]){
975                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
976                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
977                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
978                         break;
979                 }
980                 break;
981         case FSF_ACCESS_DENIED:
982                 break;
983         case FSF_PORT_BOXED:
984                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
985                                ZFCP_STATUS_FSFREQ_RETRY;
986                 break;
987         case FSF_PORT_HANDLE_NOT_VALID:
988                 zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
989                 /* fall through */
990         case FSF_GENERIC_COMMAND_REJECTED:
991         case FSF_PAYLOAD_SIZE_MISMATCH:
992         case FSF_REQUEST_SIZE_TOO_LARGE:
993         case FSF_RESPONSE_SIZE_TOO_LARGE:
994         case FSF_SBAL_MISMATCH:
995                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
996                 break;
997         }
998
999 skip_fsfstatus:
1000         if (send_ct->handler)
1001                 send_ct->handler(send_ct->handler_data);
1002 }
1003
1004 static void zfcp_fsf_setup_ct_els_unchained(struct qdio_buffer_element *sbale,
1005                                             struct scatterlist *sg_req,
1006                                             struct scatterlist *sg_resp)
1007 {
1008         sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1009         sbale[2].addr   = sg_virt(sg_req);
1010         sbale[2].length = sg_req->length;
1011         sbale[3].addr   = sg_virt(sg_resp);
1012         sbale[3].length = sg_resp->length;
1013         sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1014 }
1015
1016 static int zfcp_fsf_one_sbal(struct scatterlist *sg)
1017 {
1018         return sg_is_last(sg) && sg->length <= PAGE_SIZE;
1019 }
1020
1021 static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
1022                                        struct scatterlist *sg_req,
1023                                        struct scatterlist *sg_resp,
1024                                        int max_sbals)
1025 {
1026         struct qdio_buffer_element *sbale = zfcp_qdio_sbale_req(req);
1027         u32 feat = req->adapter->adapter_features;
1028         int bytes;
1029
1030         if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1031                 if (!zfcp_fsf_one_sbal(sg_req) || !zfcp_fsf_one_sbal(sg_resp))
1032                         return -EOPNOTSUPP;
1033
1034                 zfcp_fsf_setup_ct_els_unchained(sbale, sg_req, sg_resp);
1035                 return 0;
1036         }
1037
1038         /* use single, unchained SBAL if it can hold the request */
1039         if (zfcp_fsf_one_sbal(sg_req) && zfcp_fsf_one_sbal(sg_resp)) {
1040                 zfcp_fsf_setup_ct_els_unchained(sbale, sg_req, sg_resp);
1041                 return 0;
1042         }
1043
1044         bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
1045                                         sg_req, max_sbals);
1046         if (bytes <= 0)
1047                 return -EIO;
1048         req->qtcb->bottom.support.req_buf_length = bytes;
1049         req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1050
1051         bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
1052                                         sg_resp, max_sbals);
1053         if (bytes <= 0)
1054                 return -EIO;
1055         req->qtcb->bottom.support.resp_buf_length = bytes;
1056
1057         return 0;
1058 }
1059
1060 /**
1061  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1062  * @ct: pointer to struct zfcp_send_ct with data for request
1063  * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1064  * @erp_action: if non-null the Generic Service request sent within ERP
1065  */
1066 int zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
1067                      struct zfcp_erp_action *erp_action)
1068 {
1069         struct zfcp_wka_port *wka_port = ct->wka_port;
1070         struct zfcp_adapter *adapter = wka_port->adapter;
1071         struct zfcp_fsf_req *req;
1072         int ret = -EIO;
1073
1074         spin_lock_bh(&adapter->req_q_lock);
1075         if (zfcp_fsf_req_sbal_get(adapter))
1076                 goto out;
1077
1078         req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
1079                                   ZFCP_REQ_AUTO_CLEANUP, pool);
1080         if (IS_ERR(req)) {
1081                 ret = PTR_ERR(req);
1082                 goto out;
1083         }
1084
1085         ret = zfcp_fsf_setup_ct_els_sbals(req, ct->req, ct->resp,
1086                                           FSF_MAX_SBALS_PER_REQ);
1087         if (ret)
1088                 goto failed_send;
1089
1090         req->handler = zfcp_fsf_send_ct_handler;
1091         req->qtcb->header.port_handle = wka_port->handle;
1092         req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1093         req->qtcb->bottom.support.timeout = ct->timeout;
1094         req->data = ct;
1095
1096         zfcp_san_dbf_event_ct_request(req);
1097
1098         if (erp_action) {
1099                 erp_action->fsf_req = req;
1100                 req->erp_action = erp_action;
1101                 zfcp_fsf_start_erp_timer(req);
1102         } else
1103                 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1104
1105         ret = zfcp_fsf_req_send(req);
1106         if (ret)
1107                 goto failed_send;
1108
1109         goto out;
1110
1111 failed_send:
1112         zfcp_fsf_req_free(req);
1113         if (erp_action)
1114                 erp_action->fsf_req = NULL;
1115 out:
1116         spin_unlock_bh(&adapter->req_q_lock);
1117         return ret;
1118 }
1119
1120 static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1121 {
1122         struct zfcp_send_els *send_els = req->data;
1123         struct zfcp_port *port = send_els->port;
1124         struct fsf_qtcb_header *header = &req->qtcb->header;
1125
1126         send_els->status = -EINVAL;
1127
1128         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1129                 goto skip_fsfstatus;
1130
1131         switch (header->fsf_status) {
1132         case FSF_GOOD:
1133                 zfcp_san_dbf_event_els_response(req);
1134                 send_els->status = 0;
1135                 break;
1136         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1137                 zfcp_fsf_class_not_supp(req);
1138                 break;
1139         case FSF_ADAPTER_STATUS_AVAILABLE:
1140                 switch (header->fsf_status_qual.word[0]){
1141                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1142                         if (port && (send_els->ls_code != ZFCP_LS_ADISC))
1143                                 zfcp_test_link(port);
1144                         /*fall through */
1145                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1146                 case FSF_SQ_RETRY_IF_POSSIBLE:
1147                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1148                         break;
1149                 }
1150                 break;
1151         case FSF_ELS_COMMAND_REJECTED:
1152         case FSF_PAYLOAD_SIZE_MISMATCH:
1153         case FSF_REQUEST_SIZE_TOO_LARGE:
1154         case FSF_RESPONSE_SIZE_TOO_LARGE:
1155                 break;
1156         case FSF_ACCESS_DENIED:
1157                 if (port)
1158                         zfcp_fsf_access_denied_port(req, port);
1159                 break;
1160         case FSF_SBAL_MISMATCH:
1161                 /* should never occure, avoided in zfcp_fsf_send_els */
1162                 /* fall through */
1163         default:
1164                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1165                 break;
1166         }
1167 skip_fsfstatus:
1168         if (send_els->handler)
1169                 send_els->handler(send_els->handler_data);
1170 }
1171
1172 /**
1173  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1174  * @els: pointer to struct zfcp_send_els with data for the command
1175  */
1176 int zfcp_fsf_send_els(struct zfcp_send_els *els)
1177 {
1178         struct zfcp_fsf_req *req;
1179         struct zfcp_adapter *adapter = els->adapter;
1180         struct fsf_qtcb_bottom_support *bottom;
1181         int ret = -EIO;
1182
1183         spin_lock_bh(&adapter->req_q_lock);
1184         if (zfcp_fsf_req_sbal_get(adapter))
1185                 goto out;
1186         req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
1187                                   ZFCP_REQ_AUTO_CLEANUP, NULL);
1188         if (IS_ERR(req)) {
1189                 ret = PTR_ERR(req);
1190                 goto out;
1191         }
1192
1193         ret = zfcp_fsf_setup_ct_els_sbals(req, els->req, els->resp, 2);
1194
1195         if (ret)
1196                 goto failed_send;
1197
1198         bottom = &req->qtcb->bottom.support;
1199         req->handler = zfcp_fsf_send_els_handler;
1200         bottom->d_id = els->d_id;
1201         bottom->service_class = FSF_CLASS_3;
1202         bottom->timeout = 2 * R_A_TOV;
1203         req->data = els;
1204
1205         zfcp_san_dbf_event_els_request(req);
1206
1207         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1208         ret = zfcp_fsf_req_send(req);
1209         if (ret)
1210                 goto failed_send;
1211
1212         goto out;
1213
1214 failed_send:
1215         zfcp_fsf_req_free(req);
1216 out:
1217         spin_unlock_bh(&adapter->req_q_lock);
1218         return ret;
1219 }
1220
1221 int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1222 {
1223         struct qdio_buffer_element *sbale;
1224         struct zfcp_fsf_req *req;
1225         struct zfcp_adapter *adapter = erp_action->adapter;
1226         int retval = -EIO;
1227
1228         spin_lock_bh(&adapter->req_q_lock);
1229         if (zfcp_fsf_req_sbal_get(adapter))
1230                 goto out;
1231         req = zfcp_fsf_req_create(adapter,
1232                                   FSF_QTCB_EXCHANGE_CONFIG_DATA,
1233                                   ZFCP_REQ_AUTO_CLEANUP,
1234                                   adapter->pool.fsf_req_erp);
1235         if (IS_ERR(req)) {
1236                 retval = PTR_ERR(req);
1237                 goto out;
1238         }
1239
1240         sbale = zfcp_qdio_sbale_req(req);
1241         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1242         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1243
1244         req->qtcb->bottom.config.feature_selection =
1245                         FSF_FEATURE_CFDC |
1246                         FSF_FEATURE_LUN_SHARING |
1247                         FSF_FEATURE_NOTIFICATION_LOST |
1248                         FSF_FEATURE_UPDATE_ALERT;
1249         req->erp_action = erp_action;
1250         req->handler = zfcp_fsf_exchange_config_data_handler;
1251         erp_action->fsf_req = req;
1252
1253         zfcp_fsf_start_erp_timer(req);
1254         retval = zfcp_fsf_req_send(req);
1255         if (retval) {
1256                 zfcp_fsf_req_free(req);
1257                 erp_action->fsf_req = NULL;
1258         }
1259 out:
1260         spin_unlock_bh(&adapter->req_q_lock);
1261         return retval;
1262 }
1263
1264 int zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
1265                                        struct fsf_qtcb_bottom_config *data)
1266 {
1267         struct qdio_buffer_element *sbale;
1268         struct zfcp_fsf_req *req = NULL;
1269         int retval = -EIO;
1270
1271         spin_lock_bh(&adapter->req_q_lock);
1272         if (zfcp_fsf_req_sbal_get(adapter))
1273                 goto out_unlock;
1274
1275         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1276                                   0, NULL);
1277         if (IS_ERR(req)) {
1278                 retval = PTR_ERR(req);
1279                 goto out_unlock;
1280         }
1281
1282         sbale = zfcp_qdio_sbale_req(req);
1283         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1284         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1285         req->handler = zfcp_fsf_exchange_config_data_handler;
1286
1287         req->qtcb->bottom.config.feature_selection =
1288                         FSF_FEATURE_CFDC |
1289                         FSF_FEATURE_LUN_SHARING |
1290                         FSF_FEATURE_NOTIFICATION_LOST |
1291                         FSF_FEATURE_UPDATE_ALERT;
1292
1293         if (data)
1294                 req->data = data;
1295
1296         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1297         retval = zfcp_fsf_req_send(req);
1298         spin_unlock_bh(&adapter->req_q_lock);
1299         if (!retval)
1300                 wait_for_completion(&req->completion);
1301
1302         zfcp_fsf_req_free(req);
1303         return retval;
1304
1305 out_unlock:
1306         spin_unlock_bh(&adapter->req_q_lock);
1307         return retval;
1308 }
1309
1310 /**
1311  * zfcp_fsf_exchange_port_data - request information about local port
1312  * @erp_action: ERP action for the adapter for which port data is requested
1313  * Returns: 0 on success, error otherwise
1314  */
1315 int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1316 {
1317         struct qdio_buffer_element *sbale;
1318         struct zfcp_fsf_req *req;
1319         struct zfcp_adapter *adapter = erp_action->adapter;
1320         int retval = -EIO;
1321
1322         if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1323                 return -EOPNOTSUPP;
1324
1325         spin_lock_bh(&adapter->req_q_lock);
1326         if (zfcp_fsf_req_sbal_get(adapter))
1327                 goto out;
1328         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
1329                                   ZFCP_REQ_AUTO_CLEANUP,
1330                                   adapter->pool.fsf_req_erp);
1331         if (IS_ERR(req)) {
1332                 retval = PTR_ERR(req);
1333                 goto out;
1334         }
1335
1336         sbale = zfcp_qdio_sbale_req(req);
1337         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1338         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1339
1340         req->handler = zfcp_fsf_exchange_port_data_handler;
1341         req->erp_action = erp_action;
1342         erp_action->fsf_req = req;
1343
1344         zfcp_fsf_start_erp_timer(req);
1345         retval = zfcp_fsf_req_send(req);
1346         if (retval) {
1347                 zfcp_fsf_req_free(req);
1348                 erp_action->fsf_req = NULL;
1349         }
1350 out:
1351         spin_unlock_bh(&adapter->req_q_lock);
1352         return retval;
1353 }
1354
1355 /**
1356  * zfcp_fsf_exchange_port_data_sync - request information about local port
1357  * @adapter: pointer to struct zfcp_adapter
1358  * @data: pointer to struct fsf_qtcb_bottom_port
1359  * Returns: 0 on success, error otherwise
1360  */
1361 int zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
1362                                      struct fsf_qtcb_bottom_port *data)
1363 {
1364         struct qdio_buffer_element *sbale;
1365         struct zfcp_fsf_req *req = NULL;
1366         int retval = -EIO;
1367
1368         if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1369                 return -EOPNOTSUPP;
1370
1371         spin_lock_bh(&adapter->req_q_lock);
1372         if (zfcp_fsf_req_sbal_get(adapter))
1373                 goto out_unlock;
1374
1375         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA, 0,
1376                                   NULL);
1377         if (IS_ERR(req)) {
1378                 retval = PTR_ERR(req);
1379                 goto out_unlock;
1380         }
1381
1382         if (data)
1383                 req->data = data;
1384
1385         sbale = zfcp_qdio_sbale_req(req);
1386         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1387         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1388
1389         req->handler = zfcp_fsf_exchange_port_data_handler;
1390         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1391         retval = zfcp_fsf_req_send(req);
1392         spin_unlock_bh(&adapter->req_q_lock);
1393
1394         if (!retval)
1395                 wait_for_completion(&req->completion);
1396
1397         zfcp_fsf_req_free(req);
1398
1399         return retval;
1400
1401 out_unlock:
1402         spin_unlock_bh(&adapter->req_q_lock);
1403         return retval;
1404 }
1405
1406 static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1407 {
1408         struct zfcp_port *port = req->data;
1409         struct fsf_qtcb_header *header = &req->qtcb->header;
1410         struct fsf_plogi *plogi;
1411
1412         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1413                 goto out;
1414
1415         switch (header->fsf_status) {
1416         case FSF_PORT_ALREADY_OPEN:
1417                 break;
1418         case FSF_ACCESS_DENIED:
1419                 zfcp_fsf_access_denied_port(req, port);
1420                 break;
1421         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1422                 dev_warn(&req->adapter->ccw_device->dev,
1423                          "Not enough FCP adapter resources to open "
1424                          "remote port 0x%016Lx\n",
1425                          (unsigned long long)port->wwpn);
1426                 zfcp_erp_port_failed(port, "fsoph_1", req);
1427                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1428                 break;
1429         case FSF_ADAPTER_STATUS_AVAILABLE:
1430                 switch (header->fsf_status_qual.word[0]) {
1431                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1432                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1433                 case FSF_SQ_NO_RETRY_POSSIBLE:
1434                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1435                         break;
1436                 }
1437                 break;
1438         case FSF_GOOD:
1439                 port->handle = header->port_handle;
1440                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
1441                                 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1442                 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1443                                   ZFCP_STATUS_COMMON_ACCESS_BOXED,
1444                                   &port->status);
1445                 /* check whether D_ID has changed during open */
1446                 /*
1447                  * FIXME: This check is not airtight, as the FCP channel does
1448                  * not monitor closures of target port connections caused on
1449                  * the remote side. Thus, they might miss out on invalidating
1450                  * locally cached WWPNs (and other N_Port parameters) of gone
1451                  * target ports. So, our heroic attempt to make things safe
1452                  * could be undermined by 'open port' response data tagged with
1453                  * obsolete WWPNs. Another reason to monitor potential
1454                  * connection closures ourself at least (by interpreting
1455                  * incoming ELS' and unsolicited status). It just crosses my
1456                  * mind that one should be able to cross-check by means of
1457                  * another GID_PN straight after a port has been opened.
1458                  * Alternately, an ADISC/PDISC ELS should suffice, as well.
1459                  */
1460                 plogi = (struct fsf_plogi *) req->qtcb->bottom.support.els;
1461                 if (req->qtcb->bottom.support.els1_length >=
1462                     FSF_PLOGI_MIN_LEN) {
1463                         if (plogi->serv_param.wwpn != port->wwpn)
1464                                 port->d_id = 0;
1465                         else {
1466                                 port->wwnn = plogi->serv_param.wwnn;
1467                                 zfcp_fc_plogi_evaluate(port, plogi);
1468                         }
1469                 }
1470                 break;
1471         case FSF_UNKNOWN_OP_SUBTYPE:
1472                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1473                 break;
1474         }
1475
1476 out:
1477         zfcp_port_put(port);
1478 }
1479
1480 /**
1481  * zfcp_fsf_open_port - create and send open port request
1482  * @erp_action: pointer to struct zfcp_erp_action
1483  * Returns: 0 on success, error otherwise
1484  */
1485 int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1486 {
1487         struct qdio_buffer_element *sbale;
1488         struct zfcp_adapter *adapter = erp_action->adapter;
1489         struct zfcp_fsf_req *req;
1490         struct zfcp_port *port = erp_action->port;
1491         int retval = -EIO;
1492
1493         spin_lock_bh(&adapter->req_q_lock);
1494         if (zfcp_fsf_req_sbal_get(adapter))
1495                 goto out;
1496
1497         req = zfcp_fsf_req_create(adapter,
1498                                   FSF_QTCB_OPEN_PORT_WITH_DID,
1499                                   ZFCP_REQ_AUTO_CLEANUP,
1500                                   adapter->pool.fsf_req_erp);
1501         if (IS_ERR(req)) {
1502                 retval = PTR_ERR(req);
1503                 goto out;
1504         }
1505
1506         sbale = zfcp_qdio_sbale_req(req);
1507         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1508         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1509
1510         req->handler = zfcp_fsf_open_port_handler;
1511         req->qtcb->bottom.support.d_id = port->d_id;
1512         req->data = port;
1513         req->erp_action = erp_action;
1514         erp_action->fsf_req = req;
1515         zfcp_port_get(port);
1516
1517         zfcp_fsf_start_erp_timer(req);
1518         retval = zfcp_fsf_req_send(req);
1519         if (retval) {
1520                 zfcp_fsf_req_free(req);
1521                 erp_action->fsf_req = NULL;
1522                 zfcp_port_put(port);
1523         }
1524 out:
1525         spin_unlock_bh(&adapter->req_q_lock);
1526         return retval;
1527 }
1528
1529 static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1530 {
1531         struct zfcp_port *port = req->data;
1532
1533         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1534                 return;
1535
1536         switch (req->qtcb->header.fsf_status) {
1537         case FSF_PORT_HANDLE_NOT_VALID:
1538                 zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
1539                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1540                 break;
1541         case FSF_ADAPTER_STATUS_AVAILABLE:
1542                 break;
1543         case FSF_GOOD:
1544                 zfcp_erp_modify_port_status(port, "fscph_2", req,
1545                                             ZFCP_STATUS_COMMON_OPEN,
1546                                             ZFCP_CLEAR);
1547                 break;
1548         }
1549 }
1550
1551 /**
1552  * zfcp_fsf_close_port - create and send close port request
1553  * @erp_action: pointer to struct zfcp_erp_action
1554  * Returns: 0 on success, error otherwise
1555  */
1556 int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1557 {
1558         struct qdio_buffer_element *sbale;
1559         struct zfcp_adapter *adapter = erp_action->adapter;
1560         struct zfcp_fsf_req *req;
1561         int retval = -EIO;
1562
1563         spin_lock_bh(&adapter->req_q_lock);
1564         if (zfcp_fsf_req_sbal_get(adapter))
1565                 goto out;
1566
1567         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
1568                                   ZFCP_REQ_AUTO_CLEANUP,
1569                                   adapter->pool.fsf_req_erp);
1570         if (IS_ERR(req)) {
1571                 retval = PTR_ERR(req);
1572                 goto out;
1573         }
1574
1575         sbale = zfcp_qdio_sbale_req(req);
1576         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1577         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1578
1579         req->handler = zfcp_fsf_close_port_handler;
1580         req->data = erp_action->port;
1581         req->erp_action = erp_action;
1582         req->qtcb->header.port_handle = erp_action->port->handle;
1583         erp_action->fsf_req = req;
1584
1585         zfcp_fsf_start_erp_timer(req);
1586         retval = zfcp_fsf_req_send(req);
1587         if (retval) {
1588                 zfcp_fsf_req_free(req);
1589                 erp_action->fsf_req = NULL;
1590         }
1591 out:
1592         spin_unlock_bh(&adapter->req_q_lock);
1593         return retval;
1594 }
1595
1596 static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1597 {
1598         struct zfcp_wka_port *wka_port = req->data;
1599         struct fsf_qtcb_header *header = &req->qtcb->header;
1600
1601         if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1602                 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1603                 goto out;
1604         }
1605
1606         switch (header->fsf_status) {
1607         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1608                 dev_warn(&req->adapter->ccw_device->dev,
1609                          "Opening WKA port 0x%x failed\n", wka_port->d_id);
1610                 /* fall through */
1611         case FSF_ADAPTER_STATUS_AVAILABLE:
1612                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1613                 /* fall through */
1614         case FSF_ACCESS_DENIED:
1615                 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1616                 break;
1617         case FSF_GOOD:
1618                 wka_port->handle = header->port_handle;
1619                 /* fall through */
1620         case FSF_PORT_ALREADY_OPEN:
1621                 wka_port->status = ZFCP_WKA_PORT_ONLINE;
1622         }
1623 out:
1624         wake_up(&wka_port->completion_wq);
1625 }
1626
1627 /**
1628  * zfcp_fsf_open_wka_port - create and send open wka-port request
1629  * @wka_port: pointer to struct zfcp_wka_port
1630  * Returns: 0 on success, error otherwise
1631  */
1632 int zfcp_fsf_open_wka_port(struct zfcp_wka_port *wka_port)
1633 {
1634         struct qdio_buffer_element *sbale;
1635         struct zfcp_adapter *adapter = wka_port->adapter;
1636         struct zfcp_fsf_req *req;
1637         int retval = -EIO;
1638
1639         spin_lock_bh(&adapter->req_q_lock);
1640         if (zfcp_fsf_req_sbal_get(adapter))
1641                 goto out;
1642
1643         req = zfcp_fsf_req_create(adapter,
1644                                   FSF_QTCB_OPEN_PORT_WITH_DID,
1645                                   ZFCP_REQ_AUTO_CLEANUP,
1646                                   adapter->pool.fsf_req_erp);
1647         if (unlikely(IS_ERR(req))) {
1648                 retval = PTR_ERR(req);
1649                 goto out;
1650         }
1651
1652         sbale = zfcp_qdio_sbale_req(req);
1653         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1654         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1655
1656         req->handler = zfcp_fsf_open_wka_port_handler;
1657         req->qtcb->bottom.support.d_id = wka_port->d_id;
1658         req->data = wka_port;
1659
1660         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1661         retval = zfcp_fsf_req_send(req);
1662         if (retval)
1663                 zfcp_fsf_req_free(req);
1664 out:
1665         spin_unlock_bh(&adapter->req_q_lock);
1666         return retval;
1667 }
1668
1669 static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1670 {
1671         struct zfcp_wka_port *wka_port = req->data;
1672
1673         if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1674                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1675                 zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1", req);
1676         }
1677
1678         wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1679         wake_up(&wka_port->completion_wq);
1680 }
1681
1682 /**
1683  * zfcp_fsf_close_wka_port - create and send close wka port request
1684  * @erp_action: pointer to struct zfcp_erp_action
1685  * Returns: 0 on success, error otherwise
1686  */
1687 int zfcp_fsf_close_wka_port(struct zfcp_wka_port *wka_port)
1688 {
1689         struct qdio_buffer_element *sbale;
1690         struct zfcp_adapter *adapter = wka_port->adapter;
1691         struct zfcp_fsf_req *req;
1692         int retval = -EIO;
1693
1694         spin_lock_bh(&adapter->req_q_lock);
1695         if (zfcp_fsf_req_sbal_get(adapter))
1696                 goto out;
1697
1698         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
1699                                   ZFCP_REQ_AUTO_CLEANUP,
1700                                   adapter->pool.fsf_req_erp);
1701         if (unlikely(IS_ERR(req))) {
1702                 retval = PTR_ERR(req);
1703                 goto out;
1704         }
1705
1706         sbale = zfcp_qdio_sbale_req(req);
1707         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1708         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1709
1710         req->handler = zfcp_fsf_close_wka_port_handler;
1711         req->data = wka_port;
1712         req->qtcb->header.port_handle = wka_port->handle;
1713
1714         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1715         retval = zfcp_fsf_req_send(req);
1716         if (retval)
1717                 zfcp_fsf_req_free(req);
1718 out:
1719         spin_unlock_bh(&adapter->req_q_lock);
1720         return retval;
1721 }
1722
1723 static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1724 {
1725         struct zfcp_port *port = req->data;
1726         struct fsf_qtcb_header *header = &req->qtcb->header;
1727         struct zfcp_unit *unit;
1728
1729         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1730                 return;
1731
1732         switch (header->fsf_status) {
1733         case FSF_PORT_HANDLE_NOT_VALID:
1734                 zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
1735                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1736                 break;
1737         case FSF_ACCESS_DENIED:
1738                 zfcp_fsf_access_denied_port(req, port);
1739                 break;
1740         case FSF_PORT_BOXED:
1741                 /* can't use generic zfcp_erp_modify_port_status because
1742                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
1743                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1744                 list_for_each_entry(unit, &port->unit_list_head, list)
1745                         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1746                                           &unit->status);
1747                 zfcp_erp_port_boxed(port, "fscpph2", req);
1748                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
1749                                ZFCP_STATUS_FSFREQ_RETRY;
1750
1751                 break;
1752         case FSF_ADAPTER_STATUS_AVAILABLE:
1753                 switch (header->fsf_status_qual.word[0]) {
1754                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1755                         /* fall through */
1756                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1757                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1758                         break;
1759                 }
1760                 break;
1761         case FSF_GOOD:
1762                 /* can't use generic zfcp_erp_modify_port_status because
1763                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1764                  */
1765                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1766                 list_for_each_entry(unit, &port->unit_list_head, list)
1767                         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1768                                           &unit->status);
1769                 break;
1770         }
1771 }
1772
1773 /**
1774  * zfcp_fsf_close_physical_port - close physical port
1775  * @erp_action: pointer to struct zfcp_erp_action
1776  * Returns: 0 on success
1777  */
1778 int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1779 {
1780         struct qdio_buffer_element *sbale;
1781         struct zfcp_adapter *adapter = erp_action->adapter;
1782         struct zfcp_fsf_req *req;
1783         int retval = -EIO;
1784
1785         spin_lock_bh(&adapter->req_q_lock);
1786         if (zfcp_fsf_req_sbal_get(adapter))
1787                 goto out;
1788
1789         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1790                                   ZFCP_REQ_AUTO_CLEANUP,
1791                                   adapter->pool.fsf_req_erp);
1792         if (IS_ERR(req)) {
1793                 retval = PTR_ERR(req);
1794                 goto out;
1795         }
1796
1797         sbale = zfcp_qdio_sbale_req(req);
1798         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1799         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1800
1801         req->data = erp_action->port;
1802         req->qtcb->header.port_handle = erp_action->port->handle;
1803         req->erp_action = erp_action;
1804         req->handler = zfcp_fsf_close_physical_port_handler;
1805         erp_action->fsf_req = req;
1806
1807         zfcp_fsf_start_erp_timer(req);
1808         retval = zfcp_fsf_req_send(req);
1809         if (retval) {
1810                 zfcp_fsf_req_free(req);
1811                 erp_action->fsf_req = NULL;
1812         }
1813 out:
1814         spin_unlock_bh(&adapter->req_q_lock);
1815         return retval;
1816 }
1817
1818 static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
1819 {
1820         struct zfcp_adapter *adapter = req->adapter;
1821         struct zfcp_unit *unit = req->data;
1822         struct fsf_qtcb_header *header = &req->qtcb->header;
1823         struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
1824         struct fsf_queue_designator *queue_designator =
1825                                 &header->fsf_status_qual.fsf_queue_designator;
1826         int exclusive, readwrite;
1827
1828         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1829                 return;
1830
1831         atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1832                           ZFCP_STATUS_COMMON_ACCESS_BOXED |
1833                           ZFCP_STATUS_UNIT_SHARED |
1834                           ZFCP_STATUS_UNIT_READONLY,
1835                           &unit->status);
1836
1837         switch (header->fsf_status) {
1838
1839         case FSF_PORT_HANDLE_NOT_VALID:
1840                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fsouh_1", req);
1841                 /* fall through */
1842         case FSF_LUN_ALREADY_OPEN:
1843                 break;
1844         case FSF_ACCESS_DENIED:
1845                 zfcp_fsf_access_denied_unit(req, unit);
1846                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
1847                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
1848                 break;
1849         case FSF_PORT_BOXED:
1850                 zfcp_erp_port_boxed(unit->port, "fsouh_2", req);
1851                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
1852                                ZFCP_STATUS_FSFREQ_RETRY;
1853                 break;
1854         case FSF_LUN_SHARING_VIOLATION:
1855                 if (header->fsf_status_qual.word[0])
1856                         dev_warn(&adapter->ccw_device->dev,
1857                                  "LUN 0x%Lx on port 0x%Lx is already in "
1858                                  "use by CSS%d, MIF Image ID %x\n",
1859                                  (unsigned long long)unit->fcp_lun,
1860                                  (unsigned long long)unit->port->wwpn,
1861                                  queue_designator->cssid,
1862                                  queue_designator->hla);
1863                 else
1864                         zfcp_act_eval_err(adapter,
1865                                           header->fsf_status_qual.word[2]);
1866                 zfcp_erp_unit_access_denied(unit, "fsouh_3", req);
1867                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
1868                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
1869                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1870                 break;
1871         case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
1872                 dev_warn(&adapter->ccw_device->dev,
1873                          "No handle is available for LUN "
1874                          "0x%016Lx on port 0x%016Lx\n",
1875                          (unsigned long long)unit->fcp_lun,
1876                          (unsigned long long)unit->port->wwpn);
1877                 zfcp_erp_unit_failed(unit, "fsouh_4", req);
1878                 /* fall through */
1879         case FSF_INVALID_COMMAND_OPTION:
1880                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1881                 break;
1882         case FSF_ADAPTER_STATUS_AVAILABLE:
1883                 switch (header->fsf_status_qual.word[0]) {
1884                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1885                         zfcp_test_link(unit->port);
1886                         /* fall through */
1887                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1888                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1889                         break;
1890                 }
1891                 break;
1892
1893         case FSF_GOOD:
1894                 unit->handle = header->lun_handle;
1895                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1896
1897                 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
1898                     (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1899                     !zfcp_ccw_priv_sch(adapter)) {
1900                         exclusive = (bottom->lun_access_info &
1901                                         FSF_UNIT_ACCESS_EXCLUSIVE);
1902                         readwrite = (bottom->lun_access_info &
1903                                         FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
1904
1905                         if (!exclusive)
1906                                 atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
1907                                                 &unit->status);
1908
1909                         if (!readwrite) {
1910                                 atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
1911                                                 &unit->status);
1912                                 dev_info(&adapter->ccw_device->dev,
1913                                          "SCSI device at LUN 0x%016Lx on port "
1914                                          "0x%016Lx opened read-only\n",
1915                                          (unsigned long long)unit->fcp_lun,
1916                                          (unsigned long long)unit->port->wwpn);
1917                         }
1918
1919                         if (exclusive && !readwrite) {
1920                                 dev_err(&adapter->ccw_device->dev,
1921                                         "Exclusive read-only access not "
1922                                         "supported (unit 0x%016Lx, "
1923                                         "port 0x%016Lx)\n",
1924                                         (unsigned long long)unit->fcp_lun,
1925                                         (unsigned long long)unit->port->wwpn);
1926                                 zfcp_erp_unit_failed(unit, "fsouh_5", req);
1927                                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1928                                 zfcp_erp_unit_shutdown(unit, 0, "fsouh_6", req);
1929                         } else if (!exclusive && readwrite) {
1930                                 dev_err(&adapter->ccw_device->dev,
1931                                         "Shared read-write access not "
1932                                         "supported (unit 0x%016Lx, port "
1933                                         "0x%016Lx)\n",
1934                                         (unsigned long long)unit->fcp_lun,
1935                                         (unsigned long long)unit->port->wwpn);
1936                                 zfcp_erp_unit_failed(unit, "fsouh_7", req);
1937                                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1938                                 zfcp_erp_unit_shutdown(unit, 0, "fsouh_8", req);
1939                         }
1940                 }
1941                 break;
1942         }
1943 }
1944
1945 /**
1946  * zfcp_fsf_open_unit - open unit
1947  * @erp_action: pointer to struct zfcp_erp_action
1948  * Returns: 0 on success, error otherwise
1949  */
1950 int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
1951 {
1952         struct qdio_buffer_element *sbale;
1953         struct zfcp_adapter *adapter = erp_action->adapter;
1954         struct zfcp_fsf_req *req;
1955         int retval = -EIO;
1956
1957         spin_lock_bh(&adapter->req_q_lock);
1958         if (zfcp_fsf_req_sbal_get(adapter))
1959                 goto out;
1960
1961         req = zfcp_fsf_req_create(adapter, FSF_QTCB_OPEN_LUN,
1962                                   ZFCP_REQ_AUTO_CLEANUP,
1963                                   adapter->pool.fsf_req_erp);
1964         if (IS_ERR(req)) {
1965                 retval = PTR_ERR(req);
1966                 goto out;
1967         }
1968
1969         sbale = zfcp_qdio_sbale_req(req);
1970         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1971         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1972
1973         req->qtcb->header.port_handle = erp_action->port->handle;
1974         req->qtcb->bottom.support.fcp_lun = erp_action->unit->fcp_lun;
1975         req->handler = zfcp_fsf_open_unit_handler;
1976         req->data = erp_action->unit;
1977         req->erp_action = erp_action;
1978         erp_action->fsf_req = req;
1979
1980         if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1981                 req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1982
1983         zfcp_fsf_start_erp_timer(req);
1984         retval = zfcp_fsf_req_send(req);
1985         if (retval) {
1986                 zfcp_fsf_req_free(req);
1987                 erp_action->fsf_req = NULL;
1988         }
1989 out:
1990         spin_unlock_bh(&adapter->req_q_lock);
1991         return retval;
1992 }
1993
1994 static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
1995 {
1996         struct zfcp_unit *unit = req->data;
1997
1998         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1999                 return;
2000
2001         switch (req->qtcb->header.fsf_status) {
2002         case FSF_PORT_HANDLE_NOT_VALID:
2003                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fscuh_1", req);
2004                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2005                 break;
2006         case FSF_LUN_HANDLE_NOT_VALID:
2007                 zfcp_erp_port_reopen(unit->port, 0, "fscuh_2", req);
2008                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2009                 break;
2010         case FSF_PORT_BOXED:
2011                 zfcp_erp_port_boxed(unit->port, "fscuh_3", req);
2012                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2013                                ZFCP_STATUS_FSFREQ_RETRY;
2014                 break;
2015         case FSF_ADAPTER_STATUS_AVAILABLE:
2016                 switch (req->qtcb->header.fsf_status_qual.word[0]) {
2017                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
2018                         zfcp_test_link(unit->port);
2019                         /* fall through */
2020                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
2021                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2022                         break;
2023                 }
2024                 break;
2025         case FSF_GOOD:
2026                 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
2027                 break;
2028         }
2029 }
2030
2031 /**
2032  * zfcp_fsf_close_unit - close zfcp unit
2033  * @erp_action: pointer to struct zfcp_unit
2034  * Returns: 0 on success, error otherwise
2035  */
2036 int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
2037 {
2038         struct qdio_buffer_element *sbale;
2039         struct zfcp_adapter *adapter = erp_action->adapter;
2040         struct zfcp_fsf_req *req;
2041         int retval = -EIO;
2042
2043         spin_lock_bh(&adapter->req_q_lock);
2044         if (zfcp_fsf_req_sbal_get(adapter))
2045                 goto out;
2046         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_LUN,
2047                                   ZFCP_REQ_AUTO_CLEANUP,
2048                                   adapter->pool.fsf_req_erp);
2049         if (IS_ERR(req)) {
2050                 retval = PTR_ERR(req);
2051                 goto out;
2052         }
2053
2054         sbale = zfcp_qdio_sbale_req(req);
2055         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2056         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2057
2058         req->qtcb->header.port_handle = erp_action->port->handle;
2059         req->qtcb->header.lun_handle = erp_action->unit->handle;
2060         req->handler = zfcp_fsf_close_unit_handler;
2061         req->data = erp_action->unit;
2062         req->erp_action = erp_action;
2063         erp_action->fsf_req = req;
2064
2065         zfcp_fsf_start_erp_timer(req);
2066         retval = zfcp_fsf_req_send(req);
2067         if (retval) {
2068                 zfcp_fsf_req_free(req);
2069                 erp_action->fsf_req = NULL;
2070         }
2071 out:
2072         spin_unlock_bh(&adapter->req_q_lock);
2073         return retval;
2074 }
2075
2076 static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
2077 {
2078         lat_rec->sum += lat;
2079         lat_rec->min = min(lat_rec->min, lat);
2080         lat_rec->max = max(lat_rec->max, lat);
2081 }
2082
2083 static void zfcp_fsf_req_latency(struct zfcp_fsf_req *req)
2084 {
2085         struct fsf_qual_latency_info *lat_inf;
2086         struct latency_cont *lat;
2087         struct zfcp_unit *unit = req->unit;
2088
2089         lat_inf = &req->qtcb->prefix.prot_status_qual.latency_info;
2090
2091         switch (req->qtcb->bottom.io.data_direction) {
2092         case FSF_DATADIR_READ:
2093                 lat = &unit->latencies.read;
2094                 break;
2095         case FSF_DATADIR_WRITE:
2096                 lat = &unit->latencies.write;
2097                 break;
2098         case FSF_DATADIR_CMND:
2099                 lat = &unit->latencies.cmd;
2100                 break;
2101         default:
2102                 return;
2103         }
2104
2105         spin_lock(&unit->latencies.lock);
2106         zfcp_fsf_update_lat(&lat->channel, lat_inf->channel_lat);
2107         zfcp_fsf_update_lat(&lat->fabric, lat_inf->fabric_lat);
2108         lat->counter++;
2109         spin_unlock(&unit->latencies.lock);
2110 }
2111
2112 #ifdef CONFIG_BLK_DEV_IO_TRACE
2113 static void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
2114 {
2115         struct fsf_qual_latency_info *lat_inf;
2116         struct scsi_cmnd *scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
2117         struct request *req = scsi_cmnd->request;
2118         struct zfcp_blk_drv_data trace;
2119         int ticks = fsf_req->adapter->timer_ticks;
2120
2121         trace.flags = 0;
2122         trace.magic = ZFCP_BLK_DRV_DATA_MAGIC;
2123         if (fsf_req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA) {
2124                 trace.flags |= ZFCP_BLK_LAT_VALID;
2125                 lat_inf = &fsf_req->qtcb->prefix.prot_status_qual.latency_info;
2126                 trace.channel_lat = lat_inf->channel_lat * ticks;
2127                 trace.fabric_lat = lat_inf->fabric_lat * ticks;
2128         }
2129         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2130                 trace.flags |= ZFCP_BLK_REQ_ERROR;
2131         trace.inb_usage = fsf_req->qdio_inb_usage;
2132         trace.outb_usage = fsf_req->qdio_outb_usage;
2133
2134         blk_add_driver_data(req->q, req, &trace, sizeof(trace));
2135 }
2136 #else
2137 static inline void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
2138 {
2139 }
2140 #endif
2141
2142 static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
2143 {
2144         struct scsi_cmnd *scpnt;
2145         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
2146             &(req->qtcb->bottom.io.fcp_rsp);
2147         u32 sns_len;
2148         char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
2149         unsigned long flags;
2150
2151         read_lock_irqsave(&req->adapter->abort_lock, flags);
2152
2153         scpnt = req->data;
2154         if (unlikely(!scpnt)) {
2155                 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2156                 return;
2157         }
2158
2159         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
2160                 set_host_byte(scpnt, DID_SOFT_ERROR);
2161                 goto skip_fsfstatus;
2162         }
2163
2164         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2165                 set_host_byte(scpnt, DID_ERROR);
2166                 goto skip_fsfstatus;
2167         }
2168
2169         set_msg_byte(scpnt, COMMAND_COMPLETE);
2170
2171         scpnt->result |= fcp_rsp_iu->scsi_status;
2172
2173         if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
2174                 zfcp_fsf_req_latency(req);
2175
2176         zfcp_fsf_trace_latency(req);
2177
2178         if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
2179                 if (fcp_rsp_info[3] == RSP_CODE_GOOD)
2180                         set_host_byte(scpnt, DID_OK);
2181                 else {
2182                         set_host_byte(scpnt, DID_ERROR);
2183                         goto skip_fsfstatus;
2184                 }
2185         }
2186
2187         if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
2188                 sns_len = FSF_FCP_RSP_SIZE - sizeof(struct fcp_rsp_iu) +
2189                           fcp_rsp_iu->fcp_rsp_len;
2190                 sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
2191                 sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);
2192
2193                 memcpy(scpnt->sense_buffer,
2194                        zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
2195         }
2196
2197         if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
2198                 scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
2199                 if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
2200                     scpnt->underflow)
2201                         set_host_byte(scpnt, DID_ERROR);
2202         }
2203 skip_fsfstatus:
2204         if (scpnt->result != 0)
2205                 zfcp_scsi_dbf_event_result("erro", 3, req->adapter, scpnt, req);
2206         else if (scpnt->retries > 0)
2207                 zfcp_scsi_dbf_event_result("retr", 4, req->adapter, scpnt, req);
2208         else
2209                 zfcp_scsi_dbf_event_result("norm", 6, req->adapter, scpnt, req);
2210
2211         scpnt->host_scribble = NULL;
2212         (scpnt->scsi_done) (scpnt);
2213         /*
2214          * We must hold this lock until scsi_done has been called.
2215          * Otherwise we may call scsi_done after abort regarding this
2216          * command has completed.
2217          * Note: scsi_done must not block!
2218          */
2219         read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2220 }
2221
2222 static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
2223 {
2224         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
2225             &(req->qtcb->bottom.io.fcp_rsp);
2226         char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
2227
2228         if ((fcp_rsp_info[3] != RSP_CODE_GOOD) ||
2229              (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2230                 req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2231 }
2232
2233
2234 static void zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *req)
2235 {
2236         struct zfcp_unit *unit;
2237         struct fsf_qtcb_header *header = &req->qtcb->header;
2238
2239         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
2240                 unit = req->data;
2241         else
2242                 unit = req->unit;
2243
2244         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
2245                 goto skip_fsfstatus;
2246
2247         switch (header->fsf_status) {
2248         case FSF_HANDLE_MISMATCH:
2249         case FSF_PORT_HANDLE_NOT_VALID:
2250                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fssfch1", req);
2251                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2252                 break;
2253         case FSF_FCPLUN_NOT_VALID:
2254         case FSF_LUN_HANDLE_NOT_VALID:
2255                 zfcp_erp_port_reopen(unit->port, 0, "fssfch2", req);
2256                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2257                 break;
2258         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2259                 zfcp_fsf_class_not_supp(req);
2260                 break;
2261         case FSF_ACCESS_DENIED:
2262                 zfcp_fsf_access_denied_unit(req, unit);
2263                 break;
2264         case FSF_DIRECTION_INDICATOR_NOT_VALID:
2265                 dev_err(&req->adapter->ccw_device->dev,
2266                         "Incorrect direction %d, unit 0x%016Lx on port "
2267                         "0x%016Lx closed\n",
2268                         req->qtcb->bottom.io.data_direction,
2269                         (unsigned long long)unit->fcp_lun,
2270                         (unsigned long long)unit->port->wwpn);
2271                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch3",
2272                                           req);
2273                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2274                 break;
2275         case FSF_CMND_LENGTH_NOT_VALID:
2276                 dev_err(&req->adapter->ccw_device->dev,
2277                         "Incorrect CDB length %d, unit 0x%016Lx on "
2278                         "port 0x%016Lx closed\n",
2279                         req->qtcb->bottom.io.fcp_cmnd_length,
2280                         (unsigned long long)unit->fcp_lun,
2281                         (unsigned long long)unit->port->wwpn);
2282                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch4",
2283                                           req);
2284                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2285                 break;
2286         case FSF_PORT_BOXED:
2287                 zfcp_erp_port_boxed(unit->port, "fssfch5", req);
2288                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2289                                ZFCP_STATUS_FSFREQ_RETRY;
2290                 break;
2291         case FSF_LUN_BOXED:
2292                 zfcp_erp_unit_boxed(unit, "fssfch6", req);
2293                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2294                                ZFCP_STATUS_FSFREQ_RETRY;
2295                 break;
2296         case FSF_ADAPTER_STATUS_AVAILABLE:
2297                 if (header->fsf_status_qual.word[0] ==
2298                     FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2299                         zfcp_test_link(unit->port);
2300                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2301                 break;
2302         }
2303 skip_fsfstatus:
2304         if (req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
2305                 zfcp_fsf_send_fcp_ctm_handler(req);
2306         else {
2307                 zfcp_fsf_send_fcp_command_task_handler(req);
2308                 req->unit = NULL;
2309                 zfcp_unit_put(unit);
2310         }
2311 }
2312
2313 static void zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, u32 fcp_dl)
2314 {
2315         u32 *fcp_dl_ptr;
2316
2317         /*
2318          * fcp_dl_addr = start address of fcp_cmnd structure +
2319          * size of fixed part + size of dynamically sized add_dcp_cdb field
2320          * SEE FCP-2 documentation
2321          */
2322         fcp_dl_ptr = (u32 *) ((unsigned char *) &fcp_cmd[1] +
2323                         (fcp_cmd->add_fcp_cdb_length << 2));
2324         *fcp_dl_ptr = fcp_dl;
2325 }
2326
2327 /**
2328  * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
2329  * @unit: unit where command is sent to
2330  * @scsi_cmnd: scsi command to be sent
2331  */
2332 int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
2333                                    struct scsi_cmnd *scsi_cmnd)
2334 {
2335         struct zfcp_fsf_req *req;
2336         struct fcp_cmnd_iu *fcp_cmnd_iu;
2337         unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
2338         int real_bytes, retval = -EIO;
2339         struct zfcp_adapter *adapter = unit->port->adapter;
2340
2341         if (unlikely(!(atomic_read(&unit->status) &
2342                        ZFCP_STATUS_COMMON_UNBLOCKED)))
2343                 return -EBUSY;
2344
2345         spin_lock(&adapter->req_q_lock);
2346         if (atomic_read(&adapter->req_q.count) <= 0) {
2347                 atomic_inc(&adapter->qdio_outb_full);
2348                 goto out;
2349         }
2350         req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND,
2351                                   ZFCP_REQ_AUTO_CLEANUP,
2352                                   adapter->pool.fsf_req_scsi);
2353         if (IS_ERR(req)) {
2354                 retval = PTR_ERR(req);
2355                 goto out;
2356         }
2357
2358         zfcp_unit_get(unit);
2359         req->unit = unit;
2360         req->data = scsi_cmnd;
2361         req->handler = zfcp_fsf_send_fcp_command_handler;
2362         req->qtcb->header.lun_handle = unit->handle;
2363         req->qtcb->header.port_handle = unit->port->handle;
2364         req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2365
2366         scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2367
2368         fcp_cmnd_iu = (struct fcp_cmnd_iu *) &(req->qtcb->bottom.io.fcp_cmnd);
2369         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
2370         /*
2371          * set depending on data direction:
2372          *      data direction bits in SBALE (SB Type)
2373          *      data direction bits in QTCB
2374          *      data direction bits in FCP_CMND IU
2375          */
2376         switch (scsi_cmnd->sc_data_direction) {
2377         case DMA_NONE:
2378                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2379                 break;
2380         case DMA_FROM_DEVICE:
2381                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
2382                 fcp_cmnd_iu->rddata = 1;
2383                 break;
2384         case DMA_TO_DEVICE:
2385                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
2386                 sbtype = SBAL_FLAGS0_TYPE_WRITE;
2387                 fcp_cmnd_iu->wddata = 1;
2388                 break;
2389         case DMA_BIDIRECTIONAL:
2390                 goto failed_scsi_cmnd;
2391         }
2392
2393         if (likely((scsi_cmnd->device->simple_tags) ||
2394                    ((atomic_read(&unit->status) & ZFCP_STATUS_UNIT_READONLY) &&
2395                     (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_SHARED))))
2396                 fcp_cmnd_iu->task_attribute = SIMPLE_Q;
2397         else
2398                 fcp_cmnd_iu->task_attribute = UNTAGGED;
2399
2400         if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH))
2401                 fcp_cmnd_iu->add_fcp_cdb_length =
2402                         (scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
2403
2404         memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
2405
2406         req->qtcb->bottom.io.fcp_cmnd_length = sizeof(struct fcp_cmnd_iu) +
2407                 fcp_cmnd_iu->add_fcp_cdb_length + sizeof(u32);
2408
2409         real_bytes = zfcp_qdio_sbals_from_sg(req, sbtype,
2410                                              scsi_sglist(scsi_cmnd),
2411                                              FSF_MAX_SBALS_PER_REQ);
2412         if (unlikely(real_bytes < 0)) {
2413                 if (req->sbal_number >= FSF_MAX_SBALS_PER_REQ) {
2414                         dev_err(&adapter->ccw_device->dev,
2415                                 "Oversize data package, unit 0x%016Lx "
2416                                 "on port 0x%016Lx closed\n",
2417                                 (unsigned long long)unit->fcp_lun,
2418                                 (unsigned long long)unit->port->wwpn);
2419                         zfcp_erp_unit_shutdown(unit, 0, "fssfct1", req);
2420                         retval = -EINVAL;
2421                 }
2422                 goto failed_scsi_cmnd;
2423         }
2424
2425         zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);
2426
2427         retval = zfcp_fsf_req_send(req);
2428         if (unlikely(retval))
2429                 goto failed_scsi_cmnd;
2430
2431         goto out;
2432
2433 failed_scsi_cmnd:
2434         zfcp_unit_put(unit);
2435         zfcp_fsf_req_free(req);
2436         scsi_cmnd->host_scribble = NULL;
2437 out:
2438         spin_unlock(&adapter->req_q_lock);
2439         return retval;
2440 }
2441
2442 /**
2443  * zfcp_fsf_send_fcp_ctm - send SCSI task management command
2444  * @unit: pointer to struct zfcp_unit
2445  * @tm_flags: unsigned byte for task management flags
2446  * Returns: on success pointer to struct fsf_req, NULL otherwise
2447  */
2448 struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
2449 {
2450         struct qdio_buffer_element *sbale;
2451         struct zfcp_fsf_req *req = NULL;
2452         struct fcp_cmnd_iu *fcp_cmnd_iu;
2453         struct zfcp_adapter *adapter = unit->port->adapter;
2454
2455         if (unlikely(!(atomic_read(&unit->status) &
2456                        ZFCP_STATUS_COMMON_UNBLOCKED)))
2457                 return NULL;
2458
2459         spin_lock_bh(&adapter->req_q_lock);
2460         if (zfcp_fsf_req_sbal_get(adapter))
2461                 goto out;
2462         req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, 0,
2463                                   adapter->pool.fsf_req_scsi);
2464         if (IS_ERR(req)) {
2465                 req = NULL;
2466                 goto out;
2467         }
2468
2469         req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
2470         req->data = unit;
2471         req->handler = zfcp_fsf_send_fcp_command_handler;
2472         req->qtcb->header.lun_handle = unit->handle;
2473         req->qtcb->header.port_handle = unit->port->handle;
2474         req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2475         req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2476         req->qtcb->bottom.io.fcp_cmnd_length =  sizeof(struct fcp_cmnd_iu) +
2477                                                 sizeof(u32);
2478
2479         sbale = zfcp_qdio_sbale_req(req);
2480         sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
2481         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2482
2483         fcp_cmnd_iu = (struct fcp_cmnd_iu *) &req->qtcb->bottom.io.fcp_cmnd;
2484         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
2485         fcp_cmnd_iu->task_management_flags = tm_flags;
2486
2487         zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2488         if (!zfcp_fsf_req_send(req))
2489                 goto out;
2490
2491         zfcp_fsf_req_free(req);
2492         req = NULL;
2493 out:
2494         spin_unlock_bh(&adapter->req_q_lock);
2495         return req;
2496 }
2497
2498 static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
2499 {
2500 }
2501
2502 /**
2503  * zfcp_fsf_control_file - control file upload/download
2504  * @adapter: pointer to struct zfcp_adapter
2505  * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
2506  * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
2507  */
2508 struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
2509                                            struct zfcp_fsf_cfdc *fsf_cfdc)
2510 {
2511         struct qdio_buffer_element *sbale;
2512         struct zfcp_fsf_req *req = NULL;
2513         struct fsf_qtcb_bottom_support *bottom;
2514         int direction, retval = -EIO, bytes;
2515
2516         if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
2517                 return ERR_PTR(-EOPNOTSUPP);
2518
2519         switch (fsf_cfdc->command) {
2520         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
2521                 direction = SBAL_FLAGS0_TYPE_WRITE;
2522                 break;
2523         case FSF_QTCB_UPLOAD_CONTROL_FILE:
2524                 direction = SBAL_FLAGS0_TYPE_READ;
2525                 break;
2526         default:
2527                 return ERR_PTR(-EINVAL);
2528         }
2529
2530         spin_lock_bh(&adapter->req_q_lock);
2531         if (zfcp_fsf_req_sbal_get(adapter))
2532                 goto out;
2533
2534         req = zfcp_fsf_req_create(adapter, fsf_cfdc->command, 0, NULL);
2535         if (IS_ERR(req)) {
2536                 retval = -EPERM;
2537                 goto out;
2538         }
2539
2540         req->handler = zfcp_fsf_control_file_handler;
2541
2542         sbale = zfcp_qdio_sbale_req(req);
2543         sbale[0].flags |= direction;
2544
2545         bottom = &req->qtcb->bottom.support;
2546         bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
2547         bottom->option = fsf_cfdc->option;
2548
2549         bytes = zfcp_qdio_sbals_from_sg(req, direction, fsf_cfdc->sg,
2550                                         FSF_MAX_SBALS_PER_REQ);
2551         if (bytes != ZFCP_CFDC_MAX_SIZE) {
2552                 zfcp_fsf_req_free(req);
2553                 goto out;
2554         }
2555
2556         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
2557         retval = zfcp_fsf_req_send(req);
2558 out:
2559         spin_unlock_bh(&adapter->req_q_lock);
2560
2561         if (!retval) {
2562                 wait_for_completion(&req->completion);
2563                 return req;
2564         }
2565         return ERR_PTR(retval);
2566 }
2567
2568 /**
2569  * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2570  * @adapter: pointer to struct zfcp_adapter
2571  * @sbal_idx: response queue index of SBAL to be processed
2572  */
2573 void zfcp_fsf_reqid_check(struct zfcp_adapter *adapter, int sbal_idx)
2574 {
2575         struct qdio_buffer *sbal = adapter->resp_q.sbal[sbal_idx];
2576         struct qdio_buffer_element *sbale;
2577         struct zfcp_fsf_req *fsf_req;
2578         unsigned long flags, req_id;
2579         int idx;
2580
2581         for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2582
2583                 sbale = &sbal->element[idx];
2584                 req_id = (unsigned long) sbale->addr;
2585                 spin_lock_irqsave(&adapter->req_list_lock, flags);
2586                 fsf_req = zfcp_reqlist_find(adapter, req_id);
2587
2588                 if (!fsf_req)
2589                         /*
2590                          * Unknown request means that we have potentially memory
2591                          * corruption and must stop the machine immediately.
2592                          */
2593                         panic("error: unknown req_id (%lx) on adapter %s.\n",
2594                               req_id, dev_name(&adapter->ccw_device->dev));
2595
2596                 list_del(&fsf_req->list);
2597                 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
2598
2599                 fsf_req->sbal_response = sbal_idx;
2600                 fsf_req->qdio_inb_usage = atomic_read(&adapter->resp_q.count);
2601                 zfcp_fsf_req_complete(fsf_req);
2602
2603                 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
2604                         break;
2605         }
2606 }