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