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