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