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