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