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