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