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