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