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