[SCSI] zfcp: Send ELS ADISC from workqueue
[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_req_sbal_get(struct zfcp_adapter *adapter)
651         __releases(&adapter->req_q_lock)
652         __acquires(&adapter->req_q_lock)
653 {
654         struct zfcp_qdio_queue *req_q = &adapter->req_q;
655         long ret;
656
657         if (atomic_read(&req_q->count) <= -REQUEST_LIST_SIZE)
658                 return -EIO;
659         if (atomic_read(&req_q->count) > 0)
660                 return 0;
661
662         atomic_dec(&req_q->count);
663         spin_unlock_bh(&adapter->req_q_lock);
664         ret = wait_event_interruptible_timeout(adapter->request_wq,
665                                         atomic_read(&req_q->count) >= 0,
666                                         5 * HZ);
667         spin_lock_bh(&adapter->req_q_lock);
668         atomic_inc(&req_q->count);
669
670         if (ret > 0)
671                 return 0;
672         if (!ret)
673                 atomic_inc(&adapter->qdio_outb_full);
674         return -EIO;
675 }
676
677 static struct zfcp_fsf_req *zfcp_fsf_alloc_noqtcb(mempool_t *pool)
678 {
679         struct zfcp_fsf_req *req;
680         req = mempool_alloc(pool, GFP_ATOMIC);
681         if (!req)
682                 return NULL;
683         memset(req, 0, sizeof(*req));
684         req->pool = pool;
685         return req;
686 }
687
688 static struct zfcp_fsf_req *zfcp_fsf_alloc_qtcb(mempool_t *pool)
689 {
690         struct zfcp_fsf_req_qtcb *qtcb;
691
692         if (likely(pool))
693                 qtcb = mempool_alloc(pool, GFP_ATOMIC);
694         else
695                 qtcb = kmem_cache_alloc(zfcp_data.fsf_req_qtcb_cache,
696                                         GFP_ATOMIC);
697         if (unlikely(!qtcb))
698                 return NULL;
699
700         memset(qtcb, 0, sizeof(*qtcb));
701         qtcb->fsf_req.qtcb = &qtcb->qtcb;
702         qtcb->fsf_req.pool = pool;
703
704         return &qtcb->fsf_req;
705 }
706
707 static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_adapter *adapter,
708                                                 u32 fsf_cmd, int req_flags,
709                                                 mempool_t *pool)
710 {
711         struct qdio_buffer_element *sbale;
712
713         struct zfcp_fsf_req *req;
714         struct zfcp_qdio_queue *req_q = &adapter->req_q;
715
716         if (req_flags & ZFCP_REQ_NO_QTCB)
717                 req = zfcp_fsf_alloc_noqtcb(pool);
718         else
719                 req = zfcp_fsf_alloc_qtcb(pool);
720
721         if (unlikely(!req))
722                 return ERR_PTR(-EIO);
723
724         if (adapter->req_no == 0)
725                 adapter->req_no++;
726
727         INIT_LIST_HEAD(&req->list);
728         init_timer(&req->timer);
729         init_waitqueue_head(&req->completion_wq);
730
731         req->adapter = adapter;
732         req->fsf_command = fsf_cmd;
733         req->req_id = adapter->req_no;
734         req->sbal_number = 1;
735         req->sbal_first = req_q->first;
736         req->sbal_last = req_q->first;
737         req->sbale_curr = 1;
738
739         sbale = zfcp_qdio_sbale_req(req);
740         sbale[0].addr = (void *) req->req_id;
741         sbale[0].flags |= SBAL_FLAGS0_COMMAND;
742
743         if (likely(req->qtcb)) {
744                 req->qtcb->prefix.req_seq_no = req->adapter->fsf_req_seq_no;
745                 req->qtcb->prefix.req_id = req->req_id;
746                 req->qtcb->prefix.ulp_info = 26;
747                 req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
748                 req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
749                 req->qtcb->header.req_handle = req->req_id;
750                 req->qtcb->header.fsf_command = req->fsf_command;
751                 req->seq_no = adapter->fsf_req_seq_no;
752                 req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
753                 sbale[1].addr = (void *) req->qtcb;
754                 sbale[1].length = sizeof(struct fsf_qtcb);
755         }
756
757         if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) {
758                 zfcp_fsf_req_free(req);
759                 return ERR_PTR(-EIO);
760         }
761
762         if (likely(req_flags & ZFCP_REQ_AUTO_CLEANUP))
763                 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
764
765         return req;
766 }
767
768 static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
769 {
770         struct zfcp_adapter *adapter = req->adapter;
771         unsigned long flags;
772         int idx;
773
774         /* put allocated FSF request into hash table */
775         spin_lock_irqsave(&adapter->req_list_lock, flags);
776         idx = zfcp_reqlist_hash(req->req_id);
777         list_add_tail(&req->list, &adapter->req_list[idx]);
778         spin_unlock_irqrestore(&adapter->req_list_lock, flags);
779
780         req->qdio_outb_usage = atomic_read(&adapter->req_q.count);
781         req->issued = get_clock();
782         if (zfcp_qdio_send(req)) {
783                 del_timer(&req->timer);
784                 spin_lock_irqsave(&adapter->req_list_lock, flags);
785                 /* lookup request again, list might have changed */
786                 if (zfcp_reqlist_find_safe(adapter, req))
787                         zfcp_reqlist_remove(adapter, req);
788                 spin_unlock_irqrestore(&adapter->req_list_lock, flags);
789                 zfcp_erp_adapter_reopen(adapter, 0, 116, req);
790                 return -EIO;
791         }
792
793         /* Don't increase for unsolicited status */
794         if (req->qtcb)
795                 adapter->fsf_req_seq_no++;
796         adapter->req_no++;
797
798         return 0;
799 }
800
801 /**
802  * zfcp_fsf_status_read - send status read request
803  * @adapter: pointer to struct zfcp_adapter
804  * @req_flags: request flags
805  * Returns: 0 on success, ERROR otherwise
806  */
807 int zfcp_fsf_status_read(struct zfcp_adapter *adapter)
808 {
809         struct zfcp_fsf_req *req;
810         struct fsf_status_read_buffer *sr_buf;
811         struct qdio_buffer_element *sbale;
812         int retval = -EIO;
813
814         spin_lock_bh(&adapter->req_q_lock);
815         if (zfcp_fsf_req_sbal_get(adapter))
816                 goto out;
817
818         req = zfcp_fsf_req_create(adapter, FSF_QTCB_UNSOLICITED_STATUS,
819                                   ZFCP_REQ_NO_QTCB,
820                                   adapter->pool.fsf_req_status_read);
821         if (IS_ERR(req)) {
822                 retval = PTR_ERR(req);
823                 goto out;
824         }
825
826         sbale = zfcp_qdio_sbale_req(req);
827         sbale[0].flags |= SBAL_FLAGS0_TYPE_STATUS;
828         sbale[2].flags |= SBAL_FLAGS_LAST_ENTRY;
829         req->sbale_curr = 2;
830
831         sr_buf = mempool_alloc(adapter->pool.data_status_read, GFP_ATOMIC);
832         if (!sr_buf) {
833                 retval = -ENOMEM;
834                 goto failed_buf;
835         }
836         memset(sr_buf, 0, sizeof(*sr_buf));
837         req->data = sr_buf;
838         sbale = zfcp_qdio_sbale_curr(req);
839         sbale->addr = (void *) sr_buf;
840         sbale->length = sizeof(*sr_buf);
841
842         retval = zfcp_fsf_req_send(req);
843         if (retval)
844                 goto failed_req_send;
845
846         goto out;
847
848 failed_req_send:
849         mempool_free(sr_buf, adapter->pool.data_status_read);
850 failed_buf:
851         zfcp_fsf_req_free(req);
852         zfcp_hba_dbf_event_fsf_unsol("fail", adapter, NULL);
853 out:
854         spin_unlock_bh(&adapter->req_q_lock);
855         return retval;
856 }
857
858 static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
859 {
860         struct zfcp_unit *unit = req->data;
861         union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
862
863         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
864                 return;
865
866         switch (req->qtcb->header.fsf_status) {
867         case FSF_PORT_HANDLE_NOT_VALID:
868                 if (fsq->word[0] == fsq->word[1]) {
869                         zfcp_erp_adapter_reopen(unit->port->adapter, 0, 104,
870                                                 req);
871                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
872                 }
873                 break;
874         case FSF_LUN_HANDLE_NOT_VALID:
875                 if (fsq->word[0] == fsq->word[1]) {
876                         zfcp_erp_port_reopen(unit->port, 0, 105, req);
877                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
878                 }
879                 break;
880         case FSF_FCP_COMMAND_DOES_NOT_EXIST:
881                 req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
882                 break;
883         case FSF_PORT_BOXED:
884                 zfcp_erp_port_boxed(unit->port, 47, req);
885                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
886                                ZFCP_STATUS_FSFREQ_RETRY;
887                 break;
888         case FSF_LUN_BOXED:
889                 zfcp_erp_unit_boxed(unit, 48, req);
890                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
891                                ZFCP_STATUS_FSFREQ_RETRY;
892                 break;
893         case FSF_ADAPTER_STATUS_AVAILABLE:
894                 switch (fsq->word[0]) {
895                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
896                         zfcp_test_link(unit->port);
897                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
898                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
899                         break;
900                 }
901                 break;
902         case FSF_GOOD:
903                 req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
904                 break;
905         }
906 }
907
908 /**
909  * zfcp_fsf_abort_fcp_command - abort running SCSI command
910  * @old_req_id: unsigned long
911  * @unit: pointer to struct zfcp_unit
912  * Returns: pointer to struct zfcp_fsf_req
913  */
914
915 struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
916                                                 struct zfcp_unit *unit)
917 {
918         struct qdio_buffer_element *sbale;
919         struct zfcp_fsf_req *req = NULL;
920         struct zfcp_adapter *adapter = unit->port->adapter;
921
922         spin_lock_bh(&adapter->req_q_lock);
923         if (zfcp_fsf_req_sbal_get(adapter))
924                 goto out;
925         req = zfcp_fsf_req_create(adapter, FSF_QTCB_ABORT_FCP_CMND,
926                                   0, adapter->pool.fsf_req_abort);
927         if (IS_ERR(req)) {
928                 req = NULL;
929                 goto out;
930         }
931
932         if (unlikely(!(atomic_read(&unit->status) &
933                        ZFCP_STATUS_COMMON_UNBLOCKED)))
934                 goto out_error_free;
935
936         sbale = zfcp_qdio_sbale_req(req);
937         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
938         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
939
940         req->data = unit;
941         req->handler = zfcp_fsf_abort_fcp_command_handler;
942         req->qtcb->header.lun_handle = unit->handle;
943         req->qtcb->header.port_handle = unit->port->handle;
944         req->qtcb->bottom.support.req_handle = (u64) old_req_id;
945
946         zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
947         if (!zfcp_fsf_req_send(req))
948                 goto out;
949
950 out_error_free:
951         zfcp_fsf_req_free(req);
952         req = NULL;
953 out:
954         spin_unlock_bh(&adapter->req_q_lock);
955         return req;
956 }
957
958 static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
959 {
960         struct zfcp_adapter *adapter = req->adapter;
961         struct zfcp_send_ct *send_ct = req->data;
962         struct fsf_qtcb_header *header = &req->qtcb->header;
963
964         send_ct->status = -EINVAL;
965
966         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
967                 goto skip_fsfstatus;
968
969         switch (header->fsf_status) {
970         case FSF_GOOD:
971                 zfcp_san_dbf_event_ct_response(req);
972                 send_ct->status = 0;
973                 break;
974         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
975                 zfcp_fsf_class_not_supp(req);
976                 break;
977         case FSF_ADAPTER_STATUS_AVAILABLE:
978                 switch (header->fsf_status_qual.word[0]){
979                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
980                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
981                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
982                         break;
983                 }
984                 break;
985         case FSF_ACCESS_DENIED:
986                 break;
987         case FSF_PORT_BOXED:
988                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
989                                ZFCP_STATUS_FSFREQ_RETRY;
990                 break;
991         case FSF_PORT_HANDLE_NOT_VALID:
992                 zfcp_erp_adapter_reopen(adapter, 0, 106, req);
993         case FSF_GENERIC_COMMAND_REJECTED:
994         case FSF_PAYLOAD_SIZE_MISMATCH:
995         case FSF_REQUEST_SIZE_TOO_LARGE:
996         case FSF_RESPONSE_SIZE_TOO_LARGE:
997         case FSF_SBAL_MISMATCH:
998                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
999                 break;
1000         }
1001
1002 skip_fsfstatus:
1003         if (send_ct->handler)
1004                 send_ct->handler(send_ct->handler_data);
1005 }
1006
1007 static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
1008                                        struct scatterlist *sg_req,
1009                                        struct scatterlist *sg_resp,
1010                                        int max_sbals)
1011 {
1012         struct qdio_buffer_element *sbale = zfcp_qdio_sbale_req(req);
1013         u32 feat = req->adapter->adapter_features;
1014         int bytes;
1015
1016         if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1017                 if (sg_req->length > PAGE_SIZE || sg_resp->length > PAGE_SIZE ||
1018                     !sg_is_last(sg_req) || !sg_is_last(sg_resp))
1019                         return -EOPNOTSUPP;
1020
1021                 sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE_READ;
1022                 sbale[2].addr   = sg_virt(sg_req);
1023                 sbale[2].length = sg_req->length;
1024                 sbale[3].addr   = sg_virt(sg_resp);
1025                 sbale[3].length = sg_resp->length;
1026                 sbale[3].flags |= SBAL_FLAGS_LAST_ENTRY;
1027                 return 0;
1028         }
1029
1030         bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
1031                                         sg_req, max_sbals);
1032         if (bytes <= 0)
1033                 return -ENOMEM;
1034         req->qtcb->bottom.support.req_buf_length = bytes;
1035         req->sbale_curr = ZFCP_LAST_SBALE_PER_SBAL;
1036
1037         bytes = zfcp_qdio_sbals_from_sg(req, SBAL_FLAGS0_TYPE_WRITE_READ,
1038                                         sg_resp, max_sbals);
1039         if (bytes <= 0)
1040                 return -ENOMEM;
1041         req->qtcb->bottom.support.resp_buf_length = bytes;
1042
1043         return 0;
1044 }
1045
1046 /**
1047  * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1048  * @ct: pointer to struct zfcp_send_ct with data for request
1049  * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1050  * @erp_action: if non-null the Generic Service request sent within ERP
1051  */
1052 int zfcp_fsf_send_ct(struct zfcp_send_ct *ct, mempool_t *pool,
1053                      struct zfcp_erp_action *erp_action)
1054 {
1055         struct zfcp_wka_port *wka_port = ct->wka_port;
1056         struct zfcp_adapter *adapter = wka_port->adapter;
1057         struct zfcp_fsf_req *req;
1058         int ret = -EIO;
1059
1060         spin_lock_bh(&adapter->req_q_lock);
1061         if (zfcp_fsf_req_sbal_get(adapter))
1062                 goto out;
1063
1064         req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_GENERIC,
1065                                   ZFCP_REQ_AUTO_CLEANUP, pool);
1066         if (IS_ERR(req)) {
1067                 ret = PTR_ERR(req);
1068                 goto out;
1069         }
1070
1071         ret = zfcp_fsf_setup_ct_els_sbals(req, ct->req, ct->resp,
1072                                           FSF_MAX_SBALS_PER_REQ);
1073         if (ret)
1074                 goto failed_send;
1075
1076         req->handler = zfcp_fsf_send_ct_handler;
1077         req->qtcb->header.port_handle = wka_port->handle;
1078         req->qtcb->bottom.support.service_class = FSF_CLASS_3;
1079         req->qtcb->bottom.support.timeout = ct->timeout;
1080         req->data = ct;
1081
1082         zfcp_san_dbf_event_ct_request(req);
1083
1084         if (erp_action) {
1085                 erp_action->fsf_req = req;
1086                 req->erp_action = erp_action;
1087                 zfcp_fsf_start_erp_timer(req);
1088         } else
1089                 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1090
1091         ret = zfcp_fsf_req_send(req);
1092         if (ret)
1093                 goto failed_send;
1094
1095         goto out;
1096
1097 failed_send:
1098         zfcp_fsf_req_free(req);
1099         if (erp_action)
1100                 erp_action->fsf_req = NULL;
1101 out:
1102         spin_unlock_bh(&adapter->req_q_lock);
1103         return ret;
1104 }
1105
1106 static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1107 {
1108         struct zfcp_send_els *send_els = req->data;
1109         struct zfcp_port *port = send_els->port;
1110         struct fsf_qtcb_header *header = &req->qtcb->header;
1111
1112         send_els->status = -EINVAL;
1113
1114         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1115                 goto skip_fsfstatus;
1116
1117         switch (header->fsf_status) {
1118         case FSF_GOOD:
1119                 zfcp_san_dbf_event_els_response(req);
1120                 send_els->status = 0;
1121                 break;
1122         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
1123                 zfcp_fsf_class_not_supp(req);
1124                 break;
1125         case FSF_ADAPTER_STATUS_AVAILABLE:
1126                 switch (header->fsf_status_qual.word[0]){
1127                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1128                         if (port && (send_els->ls_code != ZFCP_LS_ADISC))
1129                                 zfcp_test_link(port);
1130                         /*fall through */
1131                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1132                 case FSF_SQ_RETRY_IF_POSSIBLE:
1133                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1134                         break;
1135                 }
1136                 break;
1137         case FSF_ELS_COMMAND_REJECTED:
1138         case FSF_PAYLOAD_SIZE_MISMATCH:
1139         case FSF_REQUEST_SIZE_TOO_LARGE:
1140         case FSF_RESPONSE_SIZE_TOO_LARGE:
1141                 break;
1142         case FSF_ACCESS_DENIED:
1143                 zfcp_fsf_access_denied_port(req, port);
1144                 break;
1145         case FSF_SBAL_MISMATCH:
1146                 /* should never occure, avoided in zfcp_fsf_send_els */
1147                 /* fall through */
1148         default:
1149                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1150                 break;
1151         }
1152 skip_fsfstatus:
1153         if (send_els->handler)
1154                 send_els->handler(send_els->handler_data);
1155 }
1156
1157 /**
1158  * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1159  * @els: pointer to struct zfcp_send_els with data for the command
1160  */
1161 int zfcp_fsf_send_els(struct zfcp_send_els *els)
1162 {
1163         struct zfcp_fsf_req *req;
1164         struct zfcp_adapter *adapter = els->adapter;
1165         struct fsf_qtcb_bottom_support *bottom;
1166         int ret = -EIO;
1167
1168         if (unlikely(!(atomic_read(&els->port->status) &
1169                        ZFCP_STATUS_COMMON_UNBLOCKED)))
1170                 return -EBUSY;
1171
1172         spin_lock_bh(&adapter->req_q_lock);
1173         if (zfcp_fsf_req_sbal_get(adapter))
1174                 goto out;
1175         req = zfcp_fsf_req_create(adapter, FSF_QTCB_SEND_ELS,
1176                                   ZFCP_REQ_AUTO_CLEANUP, NULL);
1177         if (IS_ERR(req)) {
1178                 ret = PTR_ERR(req);
1179                 goto out;
1180         }
1181
1182         ret = zfcp_fsf_setup_ct_els_sbals(req, els->req, els->resp, 2);
1183
1184         if (ret)
1185                 goto failed_send;
1186
1187         bottom = &req->qtcb->bottom.support;
1188         req->handler = zfcp_fsf_send_els_handler;
1189         bottom->d_id = els->d_id;
1190         bottom->service_class = FSF_CLASS_3;
1191         bottom->timeout = 2 * R_A_TOV;
1192         req->data = els;
1193
1194         zfcp_san_dbf_event_els_request(req);
1195
1196         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1197         ret = zfcp_fsf_req_send(req);
1198         if (ret)
1199                 goto failed_send;
1200
1201         goto out;
1202
1203 failed_send:
1204         zfcp_fsf_req_free(req);
1205 out:
1206         spin_unlock_bh(&adapter->req_q_lock);
1207         return ret;
1208 }
1209
1210 int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1211 {
1212         struct qdio_buffer_element *sbale;
1213         struct zfcp_fsf_req *req;
1214         struct zfcp_adapter *adapter = erp_action->adapter;
1215         int retval = -EIO;
1216
1217         spin_lock_bh(&adapter->req_q_lock);
1218         if (zfcp_fsf_req_sbal_get(adapter))
1219                 goto out;
1220         req = zfcp_fsf_req_create(adapter,
1221                                   FSF_QTCB_EXCHANGE_CONFIG_DATA,
1222                                   ZFCP_REQ_AUTO_CLEANUP,
1223                                   adapter->pool.fsf_req_erp);
1224         if (IS_ERR(req)) {
1225                 retval = PTR_ERR(req);
1226                 goto out;
1227         }
1228
1229         sbale = zfcp_qdio_sbale_req(req);
1230         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1231         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1232
1233         req->qtcb->bottom.config.feature_selection =
1234                         FSF_FEATURE_CFDC |
1235                         FSF_FEATURE_LUN_SHARING |
1236                         FSF_FEATURE_NOTIFICATION_LOST |
1237                         FSF_FEATURE_UPDATE_ALERT;
1238         req->erp_action = erp_action;
1239         req->handler = zfcp_fsf_exchange_config_data_handler;
1240         erp_action->fsf_req = req;
1241
1242         zfcp_fsf_start_erp_timer(req);
1243         retval = zfcp_fsf_req_send(req);
1244         if (retval) {
1245                 zfcp_fsf_req_free(req);
1246                 erp_action->fsf_req = NULL;
1247         }
1248 out:
1249         spin_unlock_bh(&adapter->req_q_lock);
1250         return retval;
1251 }
1252
1253 int zfcp_fsf_exchange_config_data_sync(struct zfcp_adapter *adapter,
1254                                        struct fsf_qtcb_bottom_config *data)
1255 {
1256         struct qdio_buffer_element *sbale;
1257         struct zfcp_fsf_req *req = NULL;
1258         int retval = -EIO;
1259
1260         spin_lock_bh(&adapter->req_q_lock);
1261         if (zfcp_fsf_req_sbal_get(adapter))
1262                 goto out;
1263
1264         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1265                                   0, NULL);
1266         if (IS_ERR(req)) {
1267                 retval = PTR_ERR(req);
1268                 goto out;
1269         }
1270
1271         sbale = zfcp_qdio_sbale_req(req);
1272         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1273         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1274         req->handler = zfcp_fsf_exchange_config_data_handler;
1275
1276         req->qtcb->bottom.config.feature_selection =
1277                         FSF_FEATURE_CFDC |
1278                         FSF_FEATURE_LUN_SHARING |
1279                         FSF_FEATURE_NOTIFICATION_LOST |
1280                         FSF_FEATURE_UPDATE_ALERT;
1281
1282         if (data)
1283                 req->data = data;
1284
1285         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1286         retval = zfcp_fsf_req_send(req);
1287 out:
1288         spin_unlock_bh(&adapter->req_q_lock);
1289         if (!retval)
1290                 wait_event(req->completion_wq,
1291                            req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
1292
1293         zfcp_fsf_req_free(req);
1294
1295         return retval;
1296 }
1297
1298 /**
1299  * zfcp_fsf_exchange_port_data - request information about local port
1300  * @erp_action: ERP action for the adapter for which port data is requested
1301  * Returns: 0 on success, error otherwise
1302  */
1303 int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1304 {
1305         struct qdio_buffer_element *sbale;
1306         struct zfcp_fsf_req *req;
1307         struct zfcp_adapter *adapter = erp_action->adapter;
1308         int retval = -EIO;
1309
1310         if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1311                 return -EOPNOTSUPP;
1312
1313         spin_lock_bh(&adapter->req_q_lock);
1314         if (zfcp_fsf_req_sbal_get(adapter))
1315                 goto out;
1316         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA,
1317                                   ZFCP_REQ_AUTO_CLEANUP,
1318                                   adapter->pool.fsf_req_erp);
1319         if (IS_ERR(req)) {
1320                 retval = PTR_ERR(req);
1321                 goto out;
1322         }
1323
1324         sbale = zfcp_qdio_sbale_req(req);
1325         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1326         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1327
1328         req->handler = zfcp_fsf_exchange_port_data_handler;
1329         req->erp_action = erp_action;
1330         erp_action->fsf_req = req;
1331
1332         zfcp_fsf_start_erp_timer(req);
1333         retval = zfcp_fsf_req_send(req);
1334         if (retval) {
1335                 zfcp_fsf_req_free(req);
1336                 erp_action->fsf_req = NULL;
1337         }
1338 out:
1339         spin_unlock_bh(&adapter->req_q_lock);
1340         return retval;
1341 }
1342
1343 /**
1344  * zfcp_fsf_exchange_port_data_sync - request information about local port
1345  * @adapter: pointer to struct zfcp_adapter
1346  * @data: pointer to struct fsf_qtcb_bottom_port
1347  * Returns: 0 on success, error otherwise
1348  */
1349 int zfcp_fsf_exchange_port_data_sync(struct zfcp_adapter *adapter,
1350                                      struct fsf_qtcb_bottom_port *data)
1351 {
1352         struct qdio_buffer_element *sbale;
1353         struct zfcp_fsf_req *req = NULL;
1354         int retval = -EIO;
1355
1356         if (!(adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
1357                 return -EOPNOTSUPP;
1358
1359         spin_lock_bh(&adapter->req_q_lock);
1360         if (zfcp_fsf_req_sbal_get(adapter))
1361                 goto out;
1362
1363         req = zfcp_fsf_req_create(adapter, FSF_QTCB_EXCHANGE_PORT_DATA, 0,
1364                                   NULL);
1365         if (IS_ERR(req)) {
1366                 retval = PTR_ERR(req);
1367                 goto out;
1368         }
1369
1370         if (data)
1371                 req->data = data;
1372
1373         sbale = zfcp_qdio_sbale_req(req);
1374         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1375         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1376
1377         req->handler = zfcp_fsf_exchange_port_data_handler;
1378         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1379         retval = zfcp_fsf_req_send(req);
1380 out:
1381         spin_unlock_bh(&adapter->req_q_lock);
1382         if (!retval)
1383                 wait_event(req->completion_wq,
1384                            req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
1385         zfcp_fsf_req_free(req);
1386
1387         return retval;
1388 }
1389
1390 static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1391 {
1392         struct zfcp_port *port = req->data;
1393         struct fsf_qtcb_header *header = &req->qtcb->header;
1394         struct fsf_plogi *plogi;
1395
1396         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1397                 return;
1398
1399         switch (header->fsf_status) {
1400         case FSF_PORT_ALREADY_OPEN:
1401                 break;
1402         case FSF_ACCESS_DENIED:
1403                 zfcp_fsf_access_denied_port(req, port);
1404                 break;
1405         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1406                 dev_warn(&req->adapter->ccw_device->dev,
1407                          "Not enough FCP adapter resources to open "
1408                          "remote port 0x%016Lx\n",
1409                          (unsigned long long)port->wwpn);
1410                 zfcp_erp_port_failed(port, 31, req);
1411                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1412                 break;
1413         case FSF_ADAPTER_STATUS_AVAILABLE:
1414                 switch (header->fsf_status_qual.word[0]) {
1415                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1416                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1417                 case FSF_SQ_NO_RETRY_POSSIBLE:
1418                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1419                         break;
1420                 }
1421                 break;
1422         case FSF_GOOD:
1423                 port->handle = header->port_handle;
1424                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
1425                                 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1426                 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1427                                   ZFCP_STATUS_COMMON_ACCESS_BOXED,
1428                                   &port->status);
1429                 /* check whether D_ID has changed during open */
1430                 /*
1431                  * FIXME: This check is not airtight, as the FCP channel does
1432                  * not monitor closures of target port connections caused on
1433                  * the remote side. Thus, they might miss out on invalidating
1434                  * locally cached WWPNs (and other N_Port parameters) of gone
1435                  * target ports. So, our heroic attempt to make things safe
1436                  * could be undermined by 'open port' response data tagged with
1437                  * obsolete WWPNs. Another reason to monitor potential
1438                  * connection closures ourself at least (by interpreting
1439                  * incoming ELS' and unsolicited status). It just crosses my
1440                  * mind that one should be able to cross-check by means of
1441                  * another GID_PN straight after a port has been opened.
1442                  * Alternately, an ADISC/PDISC ELS should suffice, as well.
1443                  */
1444                 plogi = (struct fsf_plogi *) req->qtcb->bottom.support.els;
1445                 if (req->qtcb->bottom.support.els1_length >=
1446                     FSF_PLOGI_MIN_LEN) {
1447                         if (plogi->serv_param.wwpn != port->wwpn)
1448                                 port->d_id = 0;
1449                         else {
1450                                 port->wwnn = plogi->serv_param.wwnn;
1451                                 zfcp_fc_plogi_evaluate(port, plogi);
1452                         }
1453                 }
1454                 break;
1455         case FSF_UNKNOWN_OP_SUBTYPE:
1456                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1457                 break;
1458         }
1459 }
1460
1461 /**
1462  * zfcp_fsf_open_port - create and send open port request
1463  * @erp_action: pointer to struct zfcp_erp_action
1464  * Returns: 0 on success, error otherwise
1465  */
1466 int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1467 {
1468         struct qdio_buffer_element *sbale;
1469         struct zfcp_adapter *adapter = erp_action->adapter;
1470         struct zfcp_fsf_req *req;
1471         int retval = -EIO;
1472
1473         spin_lock_bh(&adapter->req_q_lock);
1474         if (zfcp_fsf_req_sbal_get(adapter))
1475                 goto out;
1476
1477         req = zfcp_fsf_req_create(adapter,
1478                                   FSF_QTCB_OPEN_PORT_WITH_DID,
1479                                   ZFCP_REQ_AUTO_CLEANUP,
1480                                   adapter->pool.fsf_req_erp);
1481         if (IS_ERR(req)) {
1482                 retval = PTR_ERR(req);
1483                 goto out;
1484         }
1485
1486         sbale = zfcp_qdio_sbale_req(req);
1487         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1488         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1489
1490         req->handler = zfcp_fsf_open_port_handler;
1491         req->qtcb->bottom.support.d_id = erp_action->port->d_id;
1492         req->data = erp_action->port;
1493         req->erp_action = erp_action;
1494         erp_action->fsf_req = req;
1495
1496         zfcp_fsf_start_erp_timer(req);
1497         retval = zfcp_fsf_req_send(req);
1498         if (retval) {
1499                 zfcp_fsf_req_free(req);
1500                 erp_action->fsf_req = NULL;
1501         }
1502 out:
1503         spin_unlock_bh(&adapter->req_q_lock);
1504         return retval;
1505 }
1506
1507 static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1508 {
1509         struct zfcp_port *port = req->data;
1510
1511         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1512                 return;
1513
1514         switch (req->qtcb->header.fsf_status) {
1515         case FSF_PORT_HANDLE_NOT_VALID:
1516                 zfcp_erp_adapter_reopen(port->adapter, 0, 107, req);
1517                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1518                 break;
1519         case FSF_ADAPTER_STATUS_AVAILABLE:
1520                 break;
1521         case FSF_GOOD:
1522                 zfcp_erp_modify_port_status(port, 33, req,
1523                                             ZFCP_STATUS_COMMON_OPEN,
1524                                             ZFCP_CLEAR);
1525                 break;
1526         }
1527 }
1528
1529 /**
1530  * zfcp_fsf_close_port - create and send close port request
1531  * @erp_action: pointer to struct zfcp_erp_action
1532  * Returns: 0 on success, error otherwise
1533  */
1534 int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1535 {
1536         struct qdio_buffer_element *sbale;
1537         struct zfcp_adapter *adapter = erp_action->adapter;
1538         struct zfcp_fsf_req *req;
1539         int retval = -EIO;
1540
1541         spin_lock_bh(&adapter->req_q_lock);
1542         if (zfcp_fsf_req_sbal_get(adapter))
1543                 goto out;
1544
1545         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
1546                                   ZFCP_REQ_AUTO_CLEANUP,
1547                                   adapter->pool.fsf_req_erp);
1548         if (IS_ERR(req)) {
1549                 retval = PTR_ERR(req);
1550                 goto out;
1551         }
1552
1553         sbale = zfcp_qdio_sbale_req(req);
1554         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1555         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1556
1557         req->handler = zfcp_fsf_close_port_handler;
1558         req->data = erp_action->port;
1559         req->erp_action = erp_action;
1560         req->qtcb->header.port_handle = erp_action->port->handle;
1561         erp_action->fsf_req = req;
1562
1563         zfcp_fsf_start_erp_timer(req);
1564         retval = zfcp_fsf_req_send(req);
1565         if (retval) {
1566                 zfcp_fsf_req_free(req);
1567                 erp_action->fsf_req = NULL;
1568         }
1569 out:
1570         spin_unlock_bh(&adapter->req_q_lock);
1571         return retval;
1572 }
1573
1574 static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1575 {
1576         struct zfcp_wka_port *wka_port = req->data;
1577         struct fsf_qtcb_header *header = &req->qtcb->header;
1578
1579         if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
1580                 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1581                 goto out;
1582         }
1583
1584         switch (header->fsf_status) {
1585         case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1586                 dev_warn(&req->adapter->ccw_device->dev,
1587                          "Opening WKA port 0x%x failed\n", wka_port->d_id);
1588         case FSF_ADAPTER_STATUS_AVAILABLE:
1589                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1590         case FSF_ACCESS_DENIED:
1591                 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1592                 break;
1593         case FSF_PORT_ALREADY_OPEN:
1594                 break;
1595         case FSF_GOOD:
1596                 wka_port->handle = header->port_handle;
1597                 wka_port->status = ZFCP_WKA_PORT_ONLINE;
1598         }
1599 out:
1600         wake_up(&wka_port->completion_wq);
1601 }
1602
1603 /**
1604  * zfcp_fsf_open_wka_port - create and send open wka-port request
1605  * @wka_port: pointer to struct zfcp_wka_port
1606  * Returns: 0 on success, error otherwise
1607  */
1608 int zfcp_fsf_open_wka_port(struct zfcp_wka_port *wka_port)
1609 {
1610         struct qdio_buffer_element *sbale;
1611         struct zfcp_adapter *adapter = wka_port->adapter;
1612         struct zfcp_fsf_req *req;
1613         int retval = -EIO;
1614
1615         spin_lock_bh(&adapter->req_q_lock);
1616         if (zfcp_fsf_req_sbal_get(adapter))
1617                 goto out;
1618
1619         req = zfcp_fsf_req_create(adapter,
1620                                   FSF_QTCB_OPEN_PORT_WITH_DID,
1621                                   ZFCP_REQ_AUTO_CLEANUP,
1622                                   adapter->pool.fsf_req_erp);
1623         if (unlikely(IS_ERR(req))) {
1624                 retval = PTR_ERR(req);
1625                 goto out;
1626         }
1627
1628         sbale = zfcp_qdio_sbale_req(req);
1629         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1630         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1631
1632         req->handler = zfcp_fsf_open_wka_port_handler;
1633         req->qtcb->bottom.support.d_id = wka_port->d_id;
1634         req->data = wka_port;
1635
1636         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1637         retval = zfcp_fsf_req_send(req);
1638         if (retval)
1639                 zfcp_fsf_req_free(req);
1640 out:
1641         spin_unlock_bh(&adapter->req_q_lock);
1642         return retval;
1643 }
1644
1645 static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1646 {
1647         struct zfcp_wka_port *wka_port = req->data;
1648
1649         if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1650                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1651                 zfcp_erp_adapter_reopen(wka_port->adapter, 0, 84, req);
1652         }
1653
1654         wka_port->status = ZFCP_WKA_PORT_OFFLINE;
1655         wake_up(&wka_port->completion_wq);
1656 }
1657
1658 /**
1659  * zfcp_fsf_close_wka_port - create and send close wka port request
1660  * @erp_action: pointer to struct zfcp_erp_action
1661  * Returns: 0 on success, error otherwise
1662  */
1663 int zfcp_fsf_close_wka_port(struct zfcp_wka_port *wka_port)
1664 {
1665         struct qdio_buffer_element *sbale;
1666         struct zfcp_adapter *adapter = wka_port->adapter;
1667         struct zfcp_fsf_req *req;
1668         int retval = -EIO;
1669
1670         spin_lock_bh(&adapter->req_q_lock);
1671         if (zfcp_fsf_req_sbal_get(adapter))
1672                 goto out;
1673
1674         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PORT,
1675                                   ZFCP_REQ_AUTO_CLEANUP,
1676                                   adapter->pool.fsf_req_erp);
1677         if (unlikely(IS_ERR(req))) {
1678                 retval = PTR_ERR(req);
1679                 goto out;
1680         }
1681
1682         sbale = zfcp_qdio_sbale_req(req);
1683         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1684         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1685
1686         req->handler = zfcp_fsf_close_wka_port_handler;
1687         req->data = wka_port;
1688         req->qtcb->header.port_handle = wka_port->handle;
1689
1690         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1691         retval = zfcp_fsf_req_send(req);
1692         if (retval)
1693                 zfcp_fsf_req_free(req);
1694 out:
1695         spin_unlock_bh(&adapter->req_q_lock);
1696         return retval;
1697 }
1698
1699 static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1700 {
1701         struct zfcp_port *port = req->data;
1702         struct fsf_qtcb_header *header = &req->qtcb->header;
1703         struct zfcp_unit *unit;
1704
1705         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1706                 return;
1707
1708         switch (header->fsf_status) {
1709         case FSF_PORT_HANDLE_NOT_VALID:
1710                 zfcp_erp_adapter_reopen(port->adapter, 0, 108, req);
1711                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1712                 break;
1713         case FSF_ACCESS_DENIED:
1714                 zfcp_fsf_access_denied_port(req, port);
1715                 break;
1716         case FSF_PORT_BOXED:
1717                 zfcp_erp_port_boxed(port, 50, req);
1718                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
1719                                ZFCP_STATUS_FSFREQ_RETRY;
1720                 /* can't use generic zfcp_erp_modify_port_status because
1721                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
1722                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1723                 list_for_each_entry(unit, &port->unit_list_head, list)
1724                         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1725                                           &unit->status);
1726                 break;
1727         case FSF_ADAPTER_STATUS_AVAILABLE:
1728                 switch (header->fsf_status_qual.word[0]) {
1729                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1730                         /* fall through */
1731                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1732                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1733                         break;
1734                 }
1735                 break;
1736         case FSF_GOOD:
1737                 /* can't use generic zfcp_erp_modify_port_status because
1738                  * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1739                  */
1740                 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
1741                 list_for_each_entry(unit, &port->unit_list_head, list)
1742                         atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1743                                           &unit->status);
1744                 break;
1745         }
1746 }
1747
1748 /**
1749  * zfcp_fsf_close_physical_port - close physical port
1750  * @erp_action: pointer to struct zfcp_erp_action
1751  * Returns: 0 on success
1752  */
1753 int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1754 {
1755         struct qdio_buffer_element *sbale;
1756         struct zfcp_adapter *adapter = erp_action->adapter;
1757         struct zfcp_fsf_req *req;
1758         int retval = -EIO;
1759
1760         spin_lock_bh(&adapter->req_q_lock);
1761         if (zfcp_fsf_req_sbal_get(adapter))
1762                 goto out;
1763
1764         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1765                                   ZFCP_REQ_AUTO_CLEANUP,
1766                                   adapter->pool.fsf_req_erp);
1767         if (IS_ERR(req)) {
1768                 retval = PTR_ERR(req);
1769                 goto out;
1770         }
1771
1772         sbale = zfcp_qdio_sbale_req(req);
1773         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1774         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1775
1776         req->data = erp_action->port;
1777         req->qtcb->header.port_handle = erp_action->port->handle;
1778         req->erp_action = erp_action;
1779         req->handler = zfcp_fsf_close_physical_port_handler;
1780         erp_action->fsf_req = req;
1781
1782         zfcp_fsf_start_erp_timer(req);
1783         retval = zfcp_fsf_req_send(req);
1784         if (retval) {
1785                 zfcp_fsf_req_free(req);
1786                 erp_action->fsf_req = NULL;
1787         }
1788 out:
1789         spin_unlock_bh(&adapter->req_q_lock);
1790         return retval;
1791 }
1792
1793 static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
1794 {
1795         struct zfcp_adapter *adapter = req->adapter;
1796         struct zfcp_unit *unit = req->data;
1797         struct fsf_qtcb_header *header = &req->qtcb->header;
1798         struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
1799         struct fsf_queue_designator *queue_designator =
1800                                 &header->fsf_status_qual.fsf_queue_designator;
1801         int exclusive, readwrite;
1802
1803         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1804                 return;
1805
1806         atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1807                           ZFCP_STATUS_COMMON_ACCESS_BOXED |
1808                           ZFCP_STATUS_UNIT_SHARED |
1809                           ZFCP_STATUS_UNIT_READONLY,
1810                           &unit->status);
1811
1812         switch (header->fsf_status) {
1813
1814         case FSF_PORT_HANDLE_NOT_VALID:
1815                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, 109, req);
1816                 /* fall through */
1817         case FSF_LUN_ALREADY_OPEN:
1818                 break;
1819         case FSF_ACCESS_DENIED:
1820                 zfcp_fsf_access_denied_unit(req, unit);
1821                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
1822                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
1823                 break;
1824         case FSF_PORT_BOXED:
1825                 zfcp_erp_port_boxed(unit->port, 51, req);
1826                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
1827                                ZFCP_STATUS_FSFREQ_RETRY;
1828                 break;
1829         case FSF_LUN_SHARING_VIOLATION:
1830                 if (header->fsf_status_qual.word[0])
1831                         dev_warn(&adapter->ccw_device->dev,
1832                                  "LUN 0x%Lx on port 0x%Lx is already in "
1833                                  "use by CSS%d, MIF Image ID %x\n",
1834                                  (unsigned long long)unit->fcp_lun,
1835                                  (unsigned long long)unit->port->wwpn,
1836                                  queue_designator->cssid,
1837                                  queue_designator->hla);
1838                 else
1839                         zfcp_act_eval_err(adapter,
1840                                           header->fsf_status_qual.word[2]);
1841                 zfcp_erp_unit_access_denied(unit, 60, req);
1842                 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
1843                 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
1844                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1845                 break;
1846         case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
1847                 dev_warn(&adapter->ccw_device->dev,
1848                          "No handle is available for LUN "
1849                          "0x%016Lx on port 0x%016Lx\n",
1850                          (unsigned long long)unit->fcp_lun,
1851                          (unsigned long long)unit->port->wwpn);
1852                 zfcp_erp_unit_failed(unit, 34, req);
1853                 /* fall through */
1854         case FSF_INVALID_COMMAND_OPTION:
1855                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1856                 break;
1857         case FSF_ADAPTER_STATUS_AVAILABLE:
1858                 switch (header->fsf_status_qual.word[0]) {
1859                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1860                         zfcp_test_link(unit->port);
1861                         /* fall through */
1862                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1863                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1864                         break;
1865                 }
1866                 break;
1867
1868         case FSF_GOOD:
1869                 unit->handle = header->lun_handle;
1870                 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1871
1872                 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
1873                     (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
1874                     (adapter->ccw_device->id.dev_model != ZFCP_DEVICE_MODEL_PRIV)) {
1875                         exclusive = (bottom->lun_access_info &
1876                                         FSF_UNIT_ACCESS_EXCLUSIVE);
1877                         readwrite = (bottom->lun_access_info &
1878                                         FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
1879
1880                         if (!exclusive)
1881                                 atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
1882                                                 &unit->status);
1883
1884                         if (!readwrite) {
1885                                 atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
1886                                                 &unit->status);
1887                                 dev_info(&adapter->ccw_device->dev,
1888                                          "SCSI device at LUN 0x%016Lx on port "
1889                                          "0x%016Lx opened read-only\n",
1890                                          (unsigned long long)unit->fcp_lun,
1891                                          (unsigned long long)unit->port->wwpn);
1892                         }
1893
1894                         if (exclusive && !readwrite) {
1895                                 dev_err(&adapter->ccw_device->dev,
1896                                         "Exclusive read-only access not "
1897                                         "supported (unit 0x%016Lx, "
1898                                         "port 0x%016Lx)\n",
1899                                         (unsigned long long)unit->fcp_lun,
1900                                         (unsigned long long)unit->port->wwpn);
1901                                 zfcp_erp_unit_failed(unit, 35, req);
1902                                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1903                                 zfcp_erp_unit_shutdown(unit, 0, 80, req);
1904                         } else if (!exclusive && readwrite) {
1905                                 dev_err(&adapter->ccw_device->dev,
1906                                         "Shared read-write access not "
1907                                         "supported (unit 0x%016Lx, port "
1908                                         "0x%016Lx)\n",
1909                                         (unsigned long long)unit->fcp_lun,
1910                                         (unsigned long long)unit->port->wwpn);
1911                                 zfcp_erp_unit_failed(unit, 36, req);
1912                                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1913                                 zfcp_erp_unit_shutdown(unit, 0, 81, req);
1914                         }
1915                 }
1916                 break;
1917         }
1918 }
1919
1920 /**
1921  * zfcp_fsf_open_unit - open unit
1922  * @erp_action: pointer to struct zfcp_erp_action
1923  * Returns: 0 on success, error otherwise
1924  */
1925 int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
1926 {
1927         struct qdio_buffer_element *sbale;
1928         struct zfcp_adapter *adapter = erp_action->adapter;
1929         struct zfcp_fsf_req *req;
1930         int retval = -EIO;
1931
1932         spin_lock_bh(&adapter->req_q_lock);
1933         if (zfcp_fsf_req_sbal_get(adapter))
1934                 goto out;
1935
1936         req = zfcp_fsf_req_create(adapter, FSF_QTCB_OPEN_LUN,
1937                                   ZFCP_REQ_AUTO_CLEANUP,
1938                                   adapter->pool.fsf_req_erp);
1939         if (IS_ERR(req)) {
1940                 retval = PTR_ERR(req);
1941                 goto out;
1942         }
1943
1944         sbale = zfcp_qdio_sbale_req(req);
1945         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
1946         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
1947
1948         req->qtcb->header.port_handle = erp_action->port->handle;
1949         req->qtcb->bottom.support.fcp_lun = erp_action->unit->fcp_lun;
1950         req->handler = zfcp_fsf_open_unit_handler;
1951         req->data = erp_action->unit;
1952         req->erp_action = erp_action;
1953         erp_action->fsf_req = req;
1954
1955         if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1956                 req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1957
1958         zfcp_fsf_start_erp_timer(req);
1959         retval = zfcp_fsf_req_send(req);
1960         if (retval) {
1961                 zfcp_fsf_req_free(req);
1962                 erp_action->fsf_req = NULL;
1963         }
1964 out:
1965         spin_unlock_bh(&adapter->req_q_lock);
1966         return retval;
1967 }
1968
1969 static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
1970 {
1971         struct zfcp_unit *unit = req->data;
1972
1973         if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1974                 return;
1975
1976         switch (req->qtcb->header.fsf_status) {
1977         case FSF_PORT_HANDLE_NOT_VALID:
1978                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, 110, req);
1979                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1980                 break;
1981         case FSF_LUN_HANDLE_NOT_VALID:
1982                 zfcp_erp_port_reopen(unit->port, 0, 111, req);
1983                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1984                 break;
1985         case FSF_PORT_BOXED:
1986                 zfcp_erp_port_boxed(unit->port, 52, req);
1987                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
1988                                ZFCP_STATUS_FSFREQ_RETRY;
1989                 break;
1990         case FSF_ADAPTER_STATUS_AVAILABLE:
1991                 switch (req->qtcb->header.fsf_status_qual.word[0]) {
1992                 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1993                         zfcp_test_link(unit->port);
1994                         /* fall through */
1995                 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1996                         req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1997                         break;
1998                 }
1999                 break;
2000         case FSF_GOOD:
2001                 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
2002                 break;
2003         }
2004 }
2005
2006 /**
2007  * zfcp_fsf_close_unit - close zfcp unit
2008  * @erp_action: pointer to struct zfcp_unit
2009  * Returns: 0 on success, error otherwise
2010  */
2011 int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
2012 {
2013         struct qdio_buffer_element *sbale;
2014         struct zfcp_adapter *adapter = erp_action->adapter;
2015         struct zfcp_fsf_req *req;
2016         int retval = -EIO;
2017
2018         spin_lock_bh(&adapter->req_q_lock);
2019         if (zfcp_fsf_req_sbal_get(adapter))
2020                 goto out;
2021         req = zfcp_fsf_req_create(adapter, FSF_QTCB_CLOSE_LUN,
2022                                   ZFCP_REQ_AUTO_CLEANUP,
2023                                   adapter->pool.fsf_req_erp);
2024         if (IS_ERR(req)) {
2025                 retval = PTR_ERR(req);
2026                 goto out;
2027         }
2028
2029         sbale = zfcp_qdio_sbale_req(req);
2030         sbale[0].flags |= SBAL_FLAGS0_TYPE_READ;
2031         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2032
2033         req->qtcb->header.port_handle = erp_action->port->handle;
2034         req->qtcb->header.lun_handle = erp_action->unit->handle;
2035         req->handler = zfcp_fsf_close_unit_handler;
2036         req->data = erp_action->unit;
2037         req->erp_action = erp_action;
2038         erp_action->fsf_req = req;
2039
2040         zfcp_fsf_start_erp_timer(req);
2041         retval = zfcp_fsf_req_send(req);
2042         if (retval) {
2043                 zfcp_fsf_req_free(req);
2044                 erp_action->fsf_req = NULL;
2045         }
2046 out:
2047         spin_unlock_bh(&adapter->req_q_lock);
2048         return retval;
2049 }
2050
2051 static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
2052 {
2053         lat_rec->sum += lat;
2054         lat_rec->min = min(lat_rec->min, lat);
2055         lat_rec->max = max(lat_rec->max, lat);
2056 }
2057
2058 static void zfcp_fsf_req_latency(struct zfcp_fsf_req *req)
2059 {
2060         struct fsf_qual_latency_info *lat_inf;
2061         struct latency_cont *lat;
2062         struct zfcp_unit *unit = req->unit;
2063
2064         lat_inf = &req->qtcb->prefix.prot_status_qual.latency_info;
2065
2066         switch (req->qtcb->bottom.io.data_direction) {
2067         case FSF_DATADIR_READ:
2068                 lat = &unit->latencies.read;
2069                 break;
2070         case FSF_DATADIR_WRITE:
2071                 lat = &unit->latencies.write;
2072                 break;
2073         case FSF_DATADIR_CMND:
2074                 lat = &unit->latencies.cmd;
2075                 break;
2076         default:
2077                 return;
2078         }
2079
2080         spin_lock(&unit->latencies.lock);
2081         zfcp_fsf_update_lat(&lat->channel, lat_inf->channel_lat);
2082         zfcp_fsf_update_lat(&lat->fabric, lat_inf->fabric_lat);
2083         lat->counter++;
2084         spin_unlock(&unit->latencies.lock);
2085 }
2086
2087 #ifdef CONFIG_BLK_DEV_IO_TRACE
2088 static void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
2089 {
2090         struct fsf_qual_latency_info *lat_inf;
2091         struct scsi_cmnd *scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
2092         struct request *req = scsi_cmnd->request;
2093         struct zfcp_blk_drv_data trace;
2094         int ticks = fsf_req->adapter->timer_ticks;
2095
2096         trace.flags = 0;
2097         trace.magic = ZFCP_BLK_DRV_DATA_MAGIC;
2098         if (fsf_req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA) {
2099                 trace.flags |= ZFCP_BLK_LAT_VALID;
2100                 lat_inf = &fsf_req->qtcb->prefix.prot_status_qual.latency_info;
2101                 trace.channel_lat = lat_inf->channel_lat * ticks;
2102                 trace.fabric_lat = lat_inf->fabric_lat * ticks;
2103         }
2104         if (fsf_req->status & ZFCP_STATUS_FSFREQ_ERROR)
2105                 trace.flags |= ZFCP_BLK_REQ_ERROR;
2106         trace.inb_usage = fsf_req->qdio_inb_usage;
2107         trace.outb_usage = fsf_req->qdio_outb_usage;
2108
2109         blk_add_driver_data(req->q, req, &trace, sizeof(trace));
2110 }
2111 #else
2112 static inline void zfcp_fsf_trace_latency(struct zfcp_fsf_req *fsf_req)
2113 {
2114 }
2115 #endif
2116
2117 static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
2118 {
2119         struct scsi_cmnd *scpnt;
2120         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
2121             &(req->qtcb->bottom.io.fcp_rsp);
2122         u32 sns_len;
2123         char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
2124         unsigned long flags;
2125
2126         read_lock_irqsave(&req->adapter->abort_lock, flags);
2127
2128         scpnt = req->data;
2129         if (unlikely(!scpnt)) {
2130                 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2131                 return;
2132         }
2133
2134         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ABORTED)) {
2135                 set_host_byte(scpnt, DID_SOFT_ERROR);
2136                 goto skip_fsfstatus;
2137         }
2138
2139         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2140                 set_host_byte(scpnt, DID_ERROR);
2141                 goto skip_fsfstatus;
2142         }
2143
2144         set_msg_byte(scpnt, COMMAND_COMPLETE);
2145
2146         scpnt->result |= fcp_rsp_iu->scsi_status;
2147
2148         if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA)
2149                 zfcp_fsf_req_latency(req);
2150
2151         zfcp_fsf_trace_latency(req);
2152
2153         if (unlikely(fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)) {
2154                 if (fcp_rsp_info[3] == RSP_CODE_GOOD)
2155                         set_host_byte(scpnt, DID_OK);
2156                 else {
2157                         set_host_byte(scpnt, DID_ERROR);
2158                         goto skip_fsfstatus;
2159                 }
2160         }
2161
2162         if (unlikely(fcp_rsp_iu->validity.bits.fcp_sns_len_valid)) {
2163                 sns_len = FSF_FCP_RSP_SIZE - sizeof(struct fcp_rsp_iu) +
2164                           fcp_rsp_iu->fcp_rsp_len;
2165                 sns_len = min(sns_len, (u32) SCSI_SENSE_BUFFERSIZE);
2166                 sns_len = min(sns_len, fcp_rsp_iu->fcp_sns_len);
2167
2168                 memcpy(scpnt->sense_buffer,
2169                        zfcp_get_fcp_sns_info_ptr(fcp_rsp_iu), sns_len);
2170         }
2171
2172         if (unlikely(fcp_rsp_iu->validity.bits.fcp_resid_under)) {
2173                 scsi_set_resid(scpnt, fcp_rsp_iu->fcp_resid);
2174                 if (scsi_bufflen(scpnt) - scsi_get_resid(scpnt) <
2175                     scpnt->underflow)
2176                         set_host_byte(scpnt, DID_ERROR);
2177         }
2178 skip_fsfstatus:
2179         if (scpnt->result != 0)
2180                 zfcp_scsi_dbf_event_result("erro", 3, req->adapter, scpnt, req);
2181         else if (scpnt->retries > 0)
2182                 zfcp_scsi_dbf_event_result("retr", 4, req->adapter, scpnt, req);
2183         else
2184                 zfcp_scsi_dbf_event_result("norm", 6, req->adapter, scpnt, req);
2185
2186         scpnt->host_scribble = NULL;
2187         (scpnt->scsi_done) (scpnt);
2188         /*
2189          * We must hold this lock until scsi_done has been called.
2190          * Otherwise we may call scsi_done after abort regarding this
2191          * command has completed.
2192          * Note: scsi_done must not block!
2193          */
2194         read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2195 }
2196
2197 static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
2198 {
2199         struct fcp_rsp_iu *fcp_rsp_iu = (struct fcp_rsp_iu *)
2200             &(req->qtcb->bottom.io.fcp_rsp);
2201         char *fcp_rsp_info = (unsigned char *) &fcp_rsp_iu[1];
2202
2203         if ((fcp_rsp_info[3] != RSP_CODE_GOOD) ||
2204              (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2205                 req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2206 }
2207
2208
2209 static void zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *req)
2210 {
2211         struct zfcp_unit *unit;
2212         struct fsf_qtcb_header *header = &req->qtcb->header;
2213
2214         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
2215                 unit = req->data;
2216         else
2217                 unit = req->unit;
2218
2219         if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
2220                 goto skip_fsfstatus;
2221
2222         switch (header->fsf_status) {
2223         case FSF_HANDLE_MISMATCH:
2224         case FSF_PORT_HANDLE_NOT_VALID:
2225                 zfcp_erp_adapter_reopen(unit->port->adapter, 0, 112, req);
2226                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2227                 break;
2228         case FSF_FCPLUN_NOT_VALID:
2229         case FSF_LUN_HANDLE_NOT_VALID:
2230                 zfcp_erp_port_reopen(unit->port, 0, 113, req);
2231                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2232                 break;
2233         case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2234                 zfcp_fsf_class_not_supp(req);
2235                 break;
2236         case FSF_ACCESS_DENIED:
2237                 zfcp_fsf_access_denied_unit(req, unit);
2238                 break;
2239         case FSF_DIRECTION_INDICATOR_NOT_VALID:
2240                 dev_err(&req->adapter->ccw_device->dev,
2241                         "Incorrect direction %d, unit 0x%016Lx on port "
2242                         "0x%016Lx closed\n",
2243                         req->qtcb->bottom.io.data_direction,
2244                         (unsigned long long)unit->fcp_lun,
2245                         (unsigned long long)unit->port->wwpn);
2246                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 133, req);
2247                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2248                 break;
2249         case FSF_CMND_LENGTH_NOT_VALID:
2250                 dev_err(&req->adapter->ccw_device->dev,
2251                         "Incorrect CDB length %d, unit 0x%016Lx on "
2252                         "port 0x%016Lx closed\n",
2253                         req->qtcb->bottom.io.fcp_cmnd_length,
2254                         (unsigned long long)unit->fcp_lun,
2255                         (unsigned long long)unit->port->wwpn);
2256                 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, 134, req);
2257                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2258                 break;
2259         case FSF_PORT_BOXED:
2260                 zfcp_erp_port_boxed(unit->port, 53, req);
2261                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2262                                ZFCP_STATUS_FSFREQ_RETRY;
2263                 break;
2264         case FSF_LUN_BOXED:
2265                 zfcp_erp_unit_boxed(unit, 54, req);
2266                 req->status |= ZFCP_STATUS_FSFREQ_ERROR |
2267                                ZFCP_STATUS_FSFREQ_RETRY;
2268                 break;
2269         case FSF_ADAPTER_STATUS_AVAILABLE:
2270                 if (header->fsf_status_qual.word[0] ==
2271                     FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
2272                         zfcp_test_link(unit->port);
2273                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2274                 break;
2275         }
2276 skip_fsfstatus:
2277         if (req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
2278                 zfcp_fsf_send_fcp_ctm_handler(req);
2279         else {
2280                 zfcp_fsf_send_fcp_command_task_handler(req);
2281                 req->unit = NULL;
2282                 zfcp_unit_put(unit);
2283         }
2284 }
2285
2286 static void zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, u32 fcp_dl)
2287 {
2288         u32 *fcp_dl_ptr;
2289
2290         /*
2291          * fcp_dl_addr = start address of fcp_cmnd structure +
2292          * size of fixed part + size of dynamically sized add_dcp_cdb field
2293          * SEE FCP-2 documentation
2294          */
2295         fcp_dl_ptr = (u32 *) ((unsigned char *) &fcp_cmd[1] +
2296                         (fcp_cmd->add_fcp_cdb_length << 2));
2297         *fcp_dl_ptr = fcp_dl;
2298 }
2299
2300 /**
2301  * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
2302  * @unit: unit where command is sent to
2303  * @scsi_cmnd: scsi command to be sent
2304  */
2305 int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
2306                                    struct scsi_cmnd *scsi_cmnd)
2307 {
2308         struct zfcp_fsf_req *req;
2309         struct fcp_cmnd_iu *fcp_cmnd_iu;
2310         unsigned int sbtype;
2311         int real_bytes, retval = -EIO;
2312         struct zfcp_adapter *adapter = unit->port->adapter;
2313
2314         if (unlikely(!(atomic_read(&unit->status) &
2315                        ZFCP_STATUS_COMMON_UNBLOCKED)))
2316                 return -EBUSY;
2317
2318         spin_lock(&adapter->req_q_lock);
2319         if (atomic_read(&adapter->req_q.count) <= 0) {
2320                 atomic_inc(&adapter->qdio_outb_full);
2321                 goto out;
2322         }
2323         req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND,
2324                                   ZFCP_REQ_AUTO_CLEANUP,
2325                                   adapter->pool.fsf_req_scsi);
2326         if (IS_ERR(req)) {
2327                 retval = PTR_ERR(req);
2328                 goto out;
2329         }
2330
2331         zfcp_unit_get(unit);
2332         req->unit = unit;
2333         req->data = scsi_cmnd;
2334         req->handler = zfcp_fsf_send_fcp_command_handler;
2335         req->qtcb->header.lun_handle = unit->handle;
2336         req->qtcb->header.port_handle = unit->port->handle;
2337         req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2338
2339         scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2340
2341         fcp_cmnd_iu = (struct fcp_cmnd_iu *) &(req->qtcb->bottom.io.fcp_cmnd);
2342         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
2343         /*
2344          * set depending on data direction:
2345          *      data direction bits in SBALE (SB Type)
2346          *      data direction bits in QTCB
2347          *      data direction bits in FCP_CMND IU
2348          */
2349         switch (scsi_cmnd->sc_data_direction) {
2350         case DMA_NONE:
2351                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2352                 sbtype = SBAL_FLAGS0_TYPE_READ;
2353                 break;
2354         case DMA_FROM_DEVICE:
2355                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_READ;
2356                 sbtype = SBAL_FLAGS0_TYPE_READ;
2357                 fcp_cmnd_iu->rddata = 1;
2358                 break;
2359         case DMA_TO_DEVICE:
2360                 req->qtcb->bottom.io.data_direction = FSF_DATADIR_WRITE;
2361                 sbtype = SBAL_FLAGS0_TYPE_WRITE;
2362                 fcp_cmnd_iu->wddata = 1;
2363                 break;
2364         case DMA_BIDIRECTIONAL:
2365         default:
2366                 retval = -EIO;
2367                 goto failed_scsi_cmnd;
2368         }
2369
2370         if (likely((scsi_cmnd->device->simple_tags) ||
2371                    ((atomic_read(&unit->status) & ZFCP_STATUS_UNIT_READONLY) &&
2372                     (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_SHARED))))
2373                 fcp_cmnd_iu->task_attribute = SIMPLE_Q;
2374         else
2375                 fcp_cmnd_iu->task_attribute = UNTAGGED;
2376
2377         if (unlikely(scsi_cmnd->cmd_len > FCP_CDB_LENGTH))
2378                 fcp_cmnd_iu->add_fcp_cdb_length =
2379                         (scsi_cmnd->cmd_len - FCP_CDB_LENGTH) >> 2;
2380
2381         memcpy(fcp_cmnd_iu->fcp_cdb, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
2382
2383         req->qtcb->bottom.io.fcp_cmnd_length = sizeof(struct fcp_cmnd_iu) +
2384                 fcp_cmnd_iu->add_fcp_cdb_length + sizeof(u32);
2385
2386         real_bytes = zfcp_qdio_sbals_from_sg(req, sbtype,
2387                                              scsi_sglist(scsi_cmnd),
2388                                              FSF_MAX_SBALS_PER_REQ);
2389         if (unlikely(real_bytes < 0)) {
2390                 if (req->sbal_number < FSF_MAX_SBALS_PER_REQ)
2391                         retval = -EIO;
2392                 else {
2393                         dev_err(&adapter->ccw_device->dev,
2394                                 "Oversize data package, unit 0x%016Lx "
2395                                 "on port 0x%016Lx closed\n",
2396                                 (unsigned long long)unit->fcp_lun,
2397                                 (unsigned long long)unit->port->wwpn);
2398                         zfcp_erp_unit_shutdown(unit, 0, 131, req);
2399                         retval = -EINVAL;
2400                 }
2401                 goto failed_scsi_cmnd;
2402         }
2403
2404         zfcp_set_fcp_dl(fcp_cmnd_iu, real_bytes);
2405
2406         retval = zfcp_fsf_req_send(req);
2407         if (unlikely(retval))
2408                 goto failed_scsi_cmnd;
2409
2410         goto out;
2411
2412 failed_scsi_cmnd:
2413         zfcp_unit_put(unit);
2414         zfcp_fsf_req_free(req);
2415         scsi_cmnd->host_scribble = NULL;
2416 out:
2417         spin_unlock(&adapter->req_q_lock);
2418         return retval;
2419 }
2420
2421 /**
2422  * zfcp_fsf_send_fcp_ctm - send SCSI task management command
2423  * @unit: pointer to struct zfcp_unit
2424  * @tm_flags: unsigned byte for task management flags
2425  * Returns: on success pointer to struct fsf_req, NULL otherwise
2426  */
2427 struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
2428 {
2429         struct qdio_buffer_element *sbale;
2430         struct zfcp_fsf_req *req = NULL;
2431         struct fcp_cmnd_iu *fcp_cmnd_iu;
2432         struct zfcp_adapter *adapter = unit->port->adapter;
2433
2434         if (unlikely(!(atomic_read(&unit->status) &
2435                        ZFCP_STATUS_COMMON_UNBLOCKED)))
2436                 return NULL;
2437
2438         spin_lock_bh(&adapter->req_q_lock);
2439         if (zfcp_fsf_req_sbal_get(adapter))
2440                 goto out;
2441         req = zfcp_fsf_req_create(adapter, FSF_QTCB_FCP_CMND, 0,
2442                                   adapter->pool.fsf_req_scsi);
2443         if (IS_ERR(req)) {
2444                 req = NULL;
2445                 goto out;
2446         }
2447
2448         req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
2449         req->data = unit;
2450         req->handler = zfcp_fsf_send_fcp_command_handler;
2451         req->qtcb->header.lun_handle = unit->handle;
2452         req->qtcb->header.port_handle = unit->port->handle;
2453         req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2454         req->qtcb->bottom.io.service_class = FSF_CLASS_3;
2455         req->qtcb->bottom.io.fcp_cmnd_length =  sizeof(struct fcp_cmnd_iu) +
2456                                                 sizeof(u32);
2457
2458         sbale = zfcp_qdio_sbale_req(req);
2459         sbale[0].flags |= SBAL_FLAGS0_TYPE_WRITE;
2460         sbale[1].flags |= SBAL_FLAGS_LAST_ENTRY;
2461
2462         fcp_cmnd_iu = (struct fcp_cmnd_iu *) &req->qtcb->bottom.io.fcp_cmnd;
2463         fcp_cmnd_iu->fcp_lun = unit->fcp_lun;
2464         fcp_cmnd_iu->task_management_flags = tm_flags;
2465
2466         zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2467         if (!zfcp_fsf_req_send(req))
2468                 goto out;
2469
2470         zfcp_fsf_req_free(req);
2471         req = NULL;
2472 out:
2473         spin_unlock_bh(&adapter->req_q_lock);
2474         return req;
2475 }
2476
2477 static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
2478 {
2479         if (req->qtcb->header.fsf_status != FSF_GOOD)
2480                 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2481 }
2482
2483 /**
2484  * zfcp_fsf_control_file - control file upload/download
2485  * @adapter: pointer to struct zfcp_adapter
2486  * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
2487  * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
2488  */
2489 struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
2490                                            struct zfcp_fsf_cfdc *fsf_cfdc)
2491 {
2492         struct qdio_buffer_element *sbale;
2493         struct zfcp_fsf_req *req = NULL;
2494         struct fsf_qtcb_bottom_support *bottom;
2495         int direction, retval = -EIO, bytes;
2496
2497         if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
2498                 return ERR_PTR(-EOPNOTSUPP);
2499
2500         switch (fsf_cfdc->command) {
2501         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
2502                 direction = SBAL_FLAGS0_TYPE_WRITE;
2503                 break;
2504         case FSF_QTCB_UPLOAD_CONTROL_FILE:
2505                 direction = SBAL_FLAGS0_TYPE_READ;
2506                 break;
2507         default:
2508                 return ERR_PTR(-EINVAL);
2509         }
2510
2511         spin_lock_bh(&adapter->req_q_lock);
2512         if (zfcp_fsf_req_sbal_get(adapter))
2513                 goto out;
2514
2515         req = zfcp_fsf_req_create(adapter, fsf_cfdc->command, 0, NULL);
2516         if (IS_ERR(req)) {
2517                 retval = -EPERM;
2518                 goto out;
2519         }
2520
2521         req->handler = zfcp_fsf_control_file_handler;
2522
2523         sbale = zfcp_qdio_sbale_req(req);
2524         sbale[0].flags |= direction;
2525
2526         bottom = &req->qtcb->bottom.support;
2527         bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
2528         bottom->option = fsf_cfdc->option;
2529
2530         bytes = zfcp_qdio_sbals_from_sg(req, direction, fsf_cfdc->sg,
2531                                         FSF_MAX_SBALS_PER_REQ);
2532         if (bytes != ZFCP_CFDC_MAX_SIZE) {
2533                 retval = -ENOMEM;
2534                 zfcp_fsf_req_free(req);
2535                 goto out;
2536         }
2537
2538         zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
2539         retval = zfcp_fsf_req_send(req);
2540 out:
2541         spin_unlock_bh(&adapter->req_q_lock);
2542
2543         if (!retval) {
2544                 wait_event(req->completion_wq,
2545                            req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
2546                 return req;
2547         }
2548         return ERR_PTR(retval);
2549 }