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