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