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