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