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