GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / scsi / ibmvscsi / ibmvfc.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
4  *
5  * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
6  *
7  * Copyright (C) IBM Corporation, 2008
8  */
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/kthread.h>
17 #include <linux/slab.h>
18 #include <linux/of.h>
19 #include <linux/pm.h>
20 #include <linux/stringify.h>
21 #include <linux/bsg-lib.h>
22 #include <asm/firmware.h>
23 #include <asm/irq.h>
24 #include <asm/vio.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
32 #include "ibmvfc.h"
33
34 static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
35 static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
36 static u64 max_lun = IBMVFC_MAX_LUN;
37 static unsigned int max_targets = IBMVFC_MAX_TARGETS;
38 static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
39 static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
40 static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
41 static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
42 static unsigned int cls3_error = IBMVFC_CLS3_ERROR;
43 static LIST_HEAD(ibmvfc_head);
44 static DEFINE_SPINLOCK(ibmvfc_driver_lock);
45 static struct scsi_transport_template *ibmvfc_transport_template;
46
47 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
48 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(IBMVFC_DRIVER_VERSION);
51
52 module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
54                  "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
55 module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(default_timeout,
57                  "Default timeout in seconds for initialization and EH commands. "
58                  "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
59 module_param_named(max_requests, max_requests, uint, S_IRUGO);
60 MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
61                  "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
62 module_param_named(max_lun, max_lun, ullong, S_IRUGO);
63 MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
64                  "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
65 module_param_named(max_targets, max_targets, uint, S_IRUGO);
66 MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
67                  "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
68 module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
69 MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
70                  "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
71 module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
72 MODULE_PARM_DESC(debug, "Enable driver debug information. "
73                  "[Default=" __stringify(IBMVFC_DEBUG) "]");
74 module_param_named(log_level, log_level, uint, 0);
75 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
76                  "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
77 module_param_named(cls3_error, cls3_error, uint, 0);
78 MODULE_PARM_DESC(cls3_error, "Enable FC Class 3 Error Recovery. "
79                  "[Default=" __stringify(IBMVFC_CLS3_ERROR) "]");
80
81 static const struct {
82         u16 status;
83         u16 error;
84         u8 result;
85         u8 retry;
86         int log;
87         char *name;
88 } cmd_status [] = {
89         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
90         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
91         { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
92         { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
93         { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
94         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
95         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
96         { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
97         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
98         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
99         { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
100         { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
101         { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
102         { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
103
104         { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
105         { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
106         { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
107         { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
108         { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
109         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
110         { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
111         { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
112         { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
113         { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
114
115         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
116         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
117         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
118         { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
119         { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
120         { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
121         { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
122         { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
123         { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
124         { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
125         { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
126
127         { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
128         { IBMVFC_FC_SCSI_ERROR, IBMVFC_COMMAND_FAILED, DID_ERROR, 0, 1, "PRLI to device failed." },
129 };
130
131 static void ibmvfc_npiv_login(struct ibmvfc_host *);
132 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
133 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
134 static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
135 static void ibmvfc_npiv_logout(struct ibmvfc_host *);
136 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *);
137 static void ibmvfc_tgt_move_login(struct ibmvfc_target *);
138
139 static const char *unknown_error = "unknown error";
140
141 #ifdef CONFIG_SCSI_IBMVFC_TRACE
142 /**
143  * ibmvfc_trc_start - Log a start trace entry
144  * @evt:                ibmvfc event struct
145  *
146  **/
147 static void ibmvfc_trc_start(struct ibmvfc_event *evt)
148 {
149         struct ibmvfc_host *vhost = evt->vhost;
150         struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
151         struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
152         struct ibmvfc_trace_entry *entry;
153
154         entry = &vhost->trace[vhost->trace_index++];
155         entry->evt = evt;
156         entry->time = jiffies;
157         entry->fmt = evt->crq.format;
158         entry->type = IBMVFC_TRC_START;
159
160         switch (entry->fmt) {
161         case IBMVFC_CMD_FORMAT:
162                 entry->op_code = vfc_cmd->iu.cdb[0];
163                 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
164                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
165                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
166                 entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
167                 break;
168         case IBMVFC_MAD_FORMAT:
169                 entry->op_code = be32_to_cpu(mad->opcode);
170                 break;
171         default:
172                 break;
173         }
174 }
175
176 /**
177  * ibmvfc_trc_end - Log an end trace entry
178  * @evt:                ibmvfc event struct
179  *
180  **/
181 static void ibmvfc_trc_end(struct ibmvfc_event *evt)
182 {
183         struct ibmvfc_host *vhost = evt->vhost;
184         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
185         struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
186         struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
187
188         entry->evt = evt;
189         entry->time = jiffies;
190         entry->fmt = evt->crq.format;
191         entry->type = IBMVFC_TRC_END;
192
193         switch (entry->fmt) {
194         case IBMVFC_CMD_FORMAT:
195                 entry->op_code = vfc_cmd->iu.cdb[0];
196                 entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
197                 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
198                 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
199                 entry->u.end.status = be16_to_cpu(vfc_cmd->status);
200                 entry->u.end.error = be16_to_cpu(vfc_cmd->error);
201                 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
202                 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
203                 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
204                 break;
205         case IBMVFC_MAD_FORMAT:
206                 entry->op_code = be32_to_cpu(mad->opcode);
207                 entry->u.end.status = be16_to_cpu(mad->status);
208                 break;
209         default:
210                 break;
211
212         }
213 }
214
215 #else
216 #define ibmvfc_trc_start(evt) do { } while (0)
217 #define ibmvfc_trc_end(evt) do { } while (0)
218 #endif
219
220 /**
221  * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
222  * @status:             status / error class
223  * @error:              error
224  *
225  * Return value:
226  *      index into cmd_status / -EINVAL on failure
227  **/
228 static int ibmvfc_get_err_index(u16 status, u16 error)
229 {
230         int i;
231
232         for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
233                 if ((cmd_status[i].status & status) == cmd_status[i].status &&
234                     cmd_status[i].error == error)
235                         return i;
236
237         return -EINVAL;
238 }
239
240 /**
241  * ibmvfc_get_cmd_error - Find the error description for the fcp response
242  * @status:             status / error class
243  * @error:              error
244  *
245  * Return value:
246  *      error description string
247  **/
248 static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
249 {
250         int rc = ibmvfc_get_err_index(status, error);
251         if (rc >= 0)
252                 return cmd_status[rc].name;
253         return unknown_error;
254 }
255
256 /**
257  * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
258  * @vfc_cmd:    ibmvfc command struct
259  *
260  * Return value:
261  *      SCSI result value to return for completed command
262  **/
263 static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
264 {
265         int err;
266         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
267         int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
268
269         if ((rsp->flags & FCP_RSP_LEN_VALID) &&
270             ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
271              rsp->data.info.rsp_code))
272                 return DID_ERROR << 16;
273
274         err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
275         if (err >= 0)
276                 return rsp->scsi_status | (cmd_status[err].result << 16);
277         return rsp->scsi_status | (DID_ERROR << 16);
278 }
279
280 /**
281  * ibmvfc_retry_cmd - Determine if error status is retryable
282  * @status:             status / error class
283  * @error:              error
284  *
285  * Return value:
286  *      1 if error should be retried / 0 if it should not
287  **/
288 static int ibmvfc_retry_cmd(u16 status, u16 error)
289 {
290         int rc = ibmvfc_get_err_index(status, error);
291
292         if (rc >= 0)
293                 return cmd_status[rc].retry;
294         return 1;
295 }
296
297 static const char *unknown_fc_explain = "unknown fc explain";
298
299 static const struct {
300         u16 fc_explain;
301         char *name;
302 } ls_explain [] = {
303         { 0x00, "no additional explanation" },
304         { 0x01, "service parameter error - options" },
305         { 0x03, "service parameter error - initiator control" },
306         { 0x05, "service parameter error - recipient control" },
307         { 0x07, "service parameter error - received data field size" },
308         { 0x09, "service parameter error - concurrent seq" },
309         { 0x0B, "service parameter error - credit" },
310         { 0x0D, "invalid N_Port/F_Port_Name" },
311         { 0x0E, "invalid node/Fabric Name" },
312         { 0x0F, "invalid common service parameters" },
313         { 0x11, "invalid association header" },
314         { 0x13, "association header required" },
315         { 0x15, "invalid originator S_ID" },
316         { 0x17, "invalid OX_ID-RX-ID combination" },
317         { 0x19, "command (request) already in progress" },
318         { 0x1E, "N_Port Login requested" },
319         { 0x1F, "Invalid N_Port_ID" },
320 };
321
322 static const struct {
323         u16 fc_explain;
324         char *name;
325 } gs_explain [] = {
326         { 0x00, "no additional explanation" },
327         { 0x01, "port identifier not registered" },
328         { 0x02, "port name not registered" },
329         { 0x03, "node name not registered" },
330         { 0x04, "class of service not registered" },
331         { 0x06, "initial process associator not registered" },
332         { 0x07, "FC-4 TYPEs not registered" },
333         { 0x08, "symbolic port name not registered" },
334         { 0x09, "symbolic node name not registered" },
335         { 0x0A, "port type not registered" },
336         { 0xF0, "authorization exception" },
337         { 0xF1, "authentication exception" },
338         { 0xF2, "data base full" },
339         { 0xF3, "data base empty" },
340         { 0xF4, "processing request" },
341         { 0xF5, "unable to verify connection" },
342         { 0xF6, "devices not in a common zone" },
343 };
344
345 /**
346  * ibmvfc_get_ls_explain - Return the FC Explain description text
347  * @status:     FC Explain status
348  *
349  * Returns:
350  *      error string
351  **/
352 static const char *ibmvfc_get_ls_explain(u16 status)
353 {
354         int i;
355
356         for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
357                 if (ls_explain[i].fc_explain == status)
358                         return ls_explain[i].name;
359
360         return unknown_fc_explain;
361 }
362
363 /**
364  * ibmvfc_get_gs_explain - Return the FC Explain description text
365  * @status:     FC Explain status
366  *
367  * Returns:
368  *      error string
369  **/
370 static const char *ibmvfc_get_gs_explain(u16 status)
371 {
372         int i;
373
374         for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
375                 if (gs_explain[i].fc_explain == status)
376                         return gs_explain[i].name;
377
378         return unknown_fc_explain;
379 }
380
381 static const struct {
382         enum ibmvfc_fc_type fc_type;
383         char *name;
384 } fc_type [] = {
385         { IBMVFC_FABRIC_REJECT, "fabric reject" },
386         { IBMVFC_PORT_REJECT, "port reject" },
387         { IBMVFC_LS_REJECT, "ELS reject" },
388         { IBMVFC_FABRIC_BUSY, "fabric busy" },
389         { IBMVFC_PORT_BUSY, "port busy" },
390         { IBMVFC_BASIC_REJECT, "basic reject" },
391 };
392
393 static const char *unknown_fc_type = "unknown fc type";
394
395 /**
396  * ibmvfc_get_fc_type - Return the FC Type description text
397  * @status:     FC Type error status
398  *
399  * Returns:
400  *      error string
401  **/
402 static const char *ibmvfc_get_fc_type(u16 status)
403 {
404         int i;
405
406         for (i = 0; i < ARRAY_SIZE(fc_type); i++)
407                 if (fc_type[i].fc_type == status)
408                         return fc_type[i].name;
409
410         return unknown_fc_type;
411 }
412
413 /**
414  * ibmvfc_set_tgt_action - Set the next init action for the target
415  * @tgt:                ibmvfc target struct
416  * @action:             action to perform
417  *
418  * Returns:
419  *      0 if action changed / non-zero if not changed
420  **/
421 static int ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
422                                   enum ibmvfc_target_action action)
423 {
424         int rc = -EINVAL;
425
426         switch (tgt->action) {
427         case IBMVFC_TGT_ACTION_LOGOUT_RPORT:
428                 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT ||
429                     action == IBMVFC_TGT_ACTION_DEL_RPORT) {
430                         tgt->action = action;
431                         rc = 0;
432                 }
433                 break;
434         case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT:
435                 if (action == IBMVFC_TGT_ACTION_DEL_RPORT ||
436                     action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
437                         tgt->action = action;
438                         rc = 0;
439                 }
440                 break;
441         case IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT:
442                 if (action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
443                         tgt->action = action;
444                         rc = 0;
445                 }
446                 break;
447         case IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT:
448                 if (action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
449                         tgt->action = action;
450                         rc = 0;
451                 }
452                 break;
453         case IBMVFC_TGT_ACTION_DEL_RPORT:
454                 if (action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
455                         tgt->action = action;
456                         rc = 0;
457                 }
458                 break;
459         case IBMVFC_TGT_ACTION_DELETED_RPORT:
460                 break;
461         default:
462                 tgt->action = action;
463                 rc = 0;
464                 break;
465         }
466
467         if (action >= IBMVFC_TGT_ACTION_LOGOUT_RPORT)
468                 tgt->add_rport = 0;
469
470         return rc;
471 }
472
473 /**
474  * ibmvfc_set_host_state - Set the state for the host
475  * @vhost:              ibmvfc host struct
476  * @state:              state to set host to
477  *
478  * Returns:
479  *      0 if state changed / non-zero if not changed
480  **/
481 static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
482                                   enum ibmvfc_host_state state)
483 {
484         int rc = 0;
485
486         switch (vhost->state) {
487         case IBMVFC_HOST_OFFLINE:
488                 rc = -EINVAL;
489                 break;
490         default:
491                 vhost->state = state;
492                 break;
493         }
494
495         return rc;
496 }
497
498 /**
499  * ibmvfc_set_host_action - Set the next init action for the host
500  * @vhost:              ibmvfc host struct
501  * @action:             action to perform
502  *
503  **/
504 static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
505                                    enum ibmvfc_host_action action)
506 {
507         switch (action) {
508         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
509                 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
510                         vhost->action = action;
511                 break;
512         case IBMVFC_HOST_ACTION_LOGO_WAIT:
513                 if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
514                         vhost->action = action;
515                 break;
516         case IBMVFC_HOST_ACTION_INIT_WAIT:
517                 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
518                         vhost->action = action;
519                 break;
520         case IBMVFC_HOST_ACTION_QUERY:
521                 switch (vhost->action) {
522                 case IBMVFC_HOST_ACTION_INIT_WAIT:
523                 case IBMVFC_HOST_ACTION_NONE:
524                 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
525                         vhost->action = action;
526                         break;
527                 default:
528                         break;
529                 }
530                 break;
531         case IBMVFC_HOST_ACTION_TGT_INIT:
532                 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
533                         vhost->action = action;
534                 break;
535         case IBMVFC_HOST_ACTION_REENABLE:
536         case IBMVFC_HOST_ACTION_RESET:
537                 vhost->action = action;
538                 break;
539         case IBMVFC_HOST_ACTION_INIT:
540         case IBMVFC_HOST_ACTION_TGT_DEL:
541         case IBMVFC_HOST_ACTION_LOGO:
542         case IBMVFC_HOST_ACTION_QUERY_TGTS:
543         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
544         case IBMVFC_HOST_ACTION_NONE:
545         default:
546                 switch (vhost->action) {
547                 case IBMVFC_HOST_ACTION_RESET:
548                 case IBMVFC_HOST_ACTION_REENABLE:
549                         break;
550                 default:
551                         vhost->action = action;
552                         break;
553                 }
554                 break;
555         }
556 }
557
558 /**
559  * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
560  * @vhost:              ibmvfc host struct
561  *
562  * Return value:
563  *      nothing
564  **/
565 static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
566 {
567         if (vhost->action == IBMVFC_HOST_ACTION_NONE &&
568             vhost->state == IBMVFC_ACTIVE) {
569                 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
570                         scsi_block_requests(vhost->host);
571                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
572                 }
573         } else
574                 vhost->reinit = 1;
575
576         wake_up(&vhost->work_wait_q);
577 }
578
579 /**
580  * ibmvfc_del_tgt - Schedule cleanup and removal of the target
581  * @tgt:                ibmvfc target struct
582  * @job_step:   job step to perform
583  *
584  **/
585 static void ibmvfc_del_tgt(struct ibmvfc_target *tgt)
586 {
587         if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT))
588                 tgt->job_step = ibmvfc_tgt_implicit_logout_and_del;
589         wake_up(&tgt->vhost->work_wait_q);
590 }
591
592 /**
593  * ibmvfc_link_down - Handle a link down event from the adapter
594  * @vhost:      ibmvfc host struct
595  * @state:      ibmvfc host state to enter
596  *
597  **/
598 static void ibmvfc_link_down(struct ibmvfc_host *vhost,
599                              enum ibmvfc_host_state state)
600 {
601         struct ibmvfc_target *tgt;
602
603         ENTER;
604         scsi_block_requests(vhost->host);
605         list_for_each_entry(tgt, &vhost->targets, queue)
606                 ibmvfc_del_tgt(tgt);
607         ibmvfc_set_host_state(vhost, state);
608         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
609         vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
610         wake_up(&vhost->work_wait_q);
611         LEAVE;
612 }
613
614 /**
615  * ibmvfc_init_host - Start host initialization
616  * @vhost:              ibmvfc host struct
617  *
618  * Return value:
619  *      nothing
620  **/
621 static void ibmvfc_init_host(struct ibmvfc_host *vhost)
622 {
623         struct ibmvfc_target *tgt;
624
625         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
626                 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
627                         dev_err(vhost->dev,
628                                 "Host initialization retries exceeded. Taking adapter offline\n");
629                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
630                         return;
631                 }
632         }
633
634         if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
635                 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
636                 vhost->async_crq.cur = 0;
637
638                 list_for_each_entry(tgt, &vhost->targets, queue) {
639                         if (vhost->client_migrated)
640                                 tgt->need_login = 1;
641                         else
642                                 ibmvfc_del_tgt(tgt);
643                 }
644
645                 scsi_block_requests(vhost->host);
646                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
647                 vhost->job_step = ibmvfc_npiv_login;
648                 wake_up(&vhost->work_wait_q);
649         }
650 }
651
652 /**
653  * ibmvfc_send_crq - Send a CRQ
654  * @vhost:      ibmvfc host struct
655  * @word1:      the first 64 bits of the data
656  * @word2:      the second 64 bits of the data
657  *
658  * Return value:
659  *      0 on success / other on failure
660  **/
661 static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
662 {
663         struct vio_dev *vdev = to_vio_dev(vhost->dev);
664         return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
665 }
666
667 /**
668  * ibmvfc_send_crq_init - Send a CRQ init message
669  * @vhost:      ibmvfc host struct
670  *
671  * Return value:
672  *      0 on success / other on failure
673  **/
674 static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
675 {
676         ibmvfc_dbg(vhost, "Sending CRQ init\n");
677         return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
678 }
679
680 /**
681  * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
682  * @vhost:      ibmvfc host struct
683  *
684  * Return value:
685  *      0 on success / other on failure
686  **/
687 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
688 {
689         ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
690         return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
691 }
692
693 /**
694  * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
695  * @vhost:      ibmvfc host struct
696  *
697  * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
698  * the crq with the hypervisor.
699  **/
700 static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
701 {
702         long rc = 0;
703         struct vio_dev *vdev = to_vio_dev(vhost->dev);
704         struct ibmvfc_crq_queue *crq = &vhost->crq;
705
706         ibmvfc_dbg(vhost, "Releasing CRQ\n");
707         free_irq(vdev->irq, vhost);
708         tasklet_kill(&vhost->tasklet);
709         do {
710                 if (rc)
711                         msleep(100);
712                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
713         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
714
715         vhost->state = IBMVFC_NO_CRQ;
716         vhost->logged_in = 0;
717         dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
718         free_page((unsigned long)crq->msgs);
719 }
720
721 /**
722  * ibmvfc_reenable_crq_queue - reenables the CRQ
723  * @vhost:      ibmvfc host struct
724  *
725  * Return value:
726  *      0 on success / other on failure
727  **/
728 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
729 {
730         int rc = 0;
731         struct vio_dev *vdev = to_vio_dev(vhost->dev);
732
733         /* Re-enable the CRQ */
734         do {
735                 if (rc)
736                         msleep(100);
737                 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
738         } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
739
740         if (rc)
741                 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
742
743         return rc;
744 }
745
746 /**
747  * ibmvfc_reset_crq - resets a crq after a failure
748  * @vhost:      ibmvfc host struct
749  *
750  * Return value:
751  *      0 on success / other on failure
752  **/
753 static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
754 {
755         int rc = 0;
756         unsigned long flags;
757         struct vio_dev *vdev = to_vio_dev(vhost->dev);
758         struct ibmvfc_crq_queue *crq = &vhost->crq;
759
760         /* Close the CRQ */
761         do {
762                 if (rc)
763                         msleep(100);
764                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
765         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
766
767         spin_lock_irqsave(vhost->host->host_lock, flags);
768         vhost->state = IBMVFC_NO_CRQ;
769         vhost->logged_in = 0;
770
771         /* Clean out the queue */
772         memset(crq->msgs, 0, PAGE_SIZE);
773         crq->cur = 0;
774
775         /* And re-open it again */
776         rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
777                                 crq->msg_token, PAGE_SIZE);
778
779         if (rc == H_CLOSED)
780                 /* Adapter is good, but other end is not ready */
781                 dev_warn(vhost->dev, "Partner adapter not ready\n");
782         else if (rc != 0)
783                 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
784         spin_unlock_irqrestore(vhost->host->host_lock, flags);
785
786         return rc;
787 }
788
789 /**
790  * ibmvfc_valid_event - Determines if event is valid.
791  * @pool:       event_pool that contains the event
792  * @evt:        ibmvfc event to be checked for validity
793  *
794  * Return value:
795  *      1 if event is valid / 0 if event is not valid
796  **/
797 static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
798                               struct ibmvfc_event *evt)
799 {
800         int index = evt - pool->events;
801         if (index < 0 || index >= pool->size)   /* outside of bounds */
802                 return 0;
803         if (evt != pool->events + index)        /* unaligned */
804                 return 0;
805         return 1;
806 }
807
808 /**
809  * ibmvfc_free_event - Free the specified event
810  * @evt:        ibmvfc_event to be freed
811  *
812  **/
813 static void ibmvfc_free_event(struct ibmvfc_event *evt)
814 {
815         struct ibmvfc_host *vhost = evt->vhost;
816         struct ibmvfc_event_pool *pool = &vhost->pool;
817
818         BUG_ON(!ibmvfc_valid_event(pool, evt));
819         BUG_ON(atomic_inc_return(&evt->free) != 1);
820         list_add_tail(&evt->queue, &vhost->free);
821 }
822
823 /**
824  * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
825  * @evt:        ibmvfc event struct
826  *
827  * This function does not setup any error status, that must be done
828  * before this function gets called.
829  **/
830 static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
831 {
832         struct scsi_cmnd *cmnd = evt->cmnd;
833
834         if (cmnd) {
835                 scsi_dma_unmap(cmnd);
836                 cmnd->scsi_done(cmnd);
837         }
838
839         if (evt->eh_comp)
840                 complete(evt->eh_comp);
841
842         ibmvfc_free_event(evt);
843 }
844
845 /**
846  * ibmvfc_fail_request - Fail request with specified error code
847  * @evt:                ibmvfc event struct
848  * @error_code: error code to fail request with
849  *
850  * Return value:
851  *      none
852  **/
853 static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
854 {
855         if (evt->cmnd) {
856                 evt->cmnd->result = (error_code << 16);
857                 evt->done = ibmvfc_scsi_eh_done;
858         } else
859                 evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
860
861         list_del(&evt->queue);
862         del_timer(&evt->timer);
863         ibmvfc_trc_end(evt);
864         evt->done(evt);
865 }
866
867 /**
868  * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
869  * @vhost:              ibmvfc host struct
870  * @error_code: error code to fail requests with
871  *
872  * Return value:
873  *      none
874  **/
875 static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
876 {
877         struct ibmvfc_event *evt, *pos;
878
879         ibmvfc_dbg(vhost, "Purging all requests\n");
880         list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
881                 ibmvfc_fail_request(evt, error_code);
882 }
883
884 /**
885  * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
886  * @vhost:      struct ibmvfc host to reset
887  **/
888 static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
889 {
890         ibmvfc_purge_requests(vhost, DID_ERROR);
891         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
892         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
893 }
894
895 /**
896  * __ibmvfc_reset_host - Reset the connection to the server (no locking)
897  * @vhost:      struct ibmvfc host to reset
898  **/
899 static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
900 {
901         if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
902             !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
903                 scsi_block_requests(vhost->host);
904                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
905                 vhost->job_step = ibmvfc_npiv_logout;
906                 wake_up(&vhost->work_wait_q);
907         } else
908                 ibmvfc_hard_reset_host(vhost);
909 }
910
911 /**
912  * ibmvfc_reset_host - Reset the connection to the server
913  * @vhost:      ibmvfc host struct
914  **/
915 static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
916 {
917         unsigned long flags;
918
919         spin_lock_irqsave(vhost->host->host_lock, flags);
920         __ibmvfc_reset_host(vhost);
921         spin_unlock_irqrestore(vhost->host->host_lock, flags);
922 }
923
924 /**
925  * ibmvfc_retry_host_init - Retry host initialization if allowed
926  * @vhost:      ibmvfc host struct
927  *
928  * Returns: 1 if init will be retried / 0 if not
929  *
930  **/
931 static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
932 {
933         int retry = 0;
934
935         if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
936                 vhost->delay_init = 1;
937                 if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
938                         dev_err(vhost->dev,
939                                 "Host initialization retries exceeded. Taking adapter offline\n");
940                         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
941                 } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
942                         __ibmvfc_reset_host(vhost);
943                 else {
944                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
945                         retry = 1;
946                 }
947         }
948
949         wake_up(&vhost->work_wait_q);
950         return retry;
951 }
952
953 /**
954  * __ibmvfc_get_target - Find the specified scsi_target (no locking)
955  * @starget:    scsi target struct
956  *
957  * Return value:
958  *      ibmvfc_target struct / NULL if not found
959  **/
960 static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
961 {
962         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
963         struct ibmvfc_host *vhost = shost_priv(shost);
964         struct ibmvfc_target *tgt;
965
966         list_for_each_entry(tgt, &vhost->targets, queue)
967                 if (tgt->target_id == starget->id) {
968                         kref_get(&tgt->kref);
969                         return tgt;
970                 }
971         return NULL;
972 }
973
974 /**
975  * ibmvfc_get_target - Find the specified scsi_target
976  * @starget:    scsi target struct
977  *
978  * Return value:
979  *      ibmvfc_target struct / NULL if not found
980  **/
981 static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
982 {
983         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
984         struct ibmvfc_target *tgt;
985         unsigned long flags;
986
987         spin_lock_irqsave(shost->host_lock, flags);
988         tgt = __ibmvfc_get_target(starget);
989         spin_unlock_irqrestore(shost->host_lock, flags);
990         return tgt;
991 }
992
993 /**
994  * ibmvfc_get_host_speed - Get host port speed
995  * @shost:              scsi host struct
996  *
997  * Return value:
998  *      none
999  **/
1000 static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
1001 {
1002         struct ibmvfc_host *vhost = shost_priv(shost);
1003         unsigned long flags;
1004
1005         spin_lock_irqsave(shost->host_lock, flags);
1006         if (vhost->state == IBMVFC_ACTIVE) {
1007                 switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
1008                 case 1:
1009                         fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
1010                         break;
1011                 case 2:
1012                         fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
1013                         break;
1014                 case 4:
1015                         fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
1016                         break;
1017                 case 8:
1018                         fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
1019                         break;
1020                 case 10:
1021                         fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
1022                         break;
1023                 case 16:
1024                         fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
1025                         break;
1026                 default:
1027                         ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
1028                                    be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
1029                         fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1030                         break;
1031                 }
1032         } else
1033                 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
1034         spin_unlock_irqrestore(shost->host_lock, flags);
1035 }
1036
1037 /**
1038  * ibmvfc_get_host_port_state - Get host port state
1039  * @shost:              scsi host struct
1040  *
1041  * Return value:
1042  *      none
1043  **/
1044 static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
1045 {
1046         struct ibmvfc_host *vhost = shost_priv(shost);
1047         unsigned long flags;
1048
1049         spin_lock_irqsave(shost->host_lock, flags);
1050         switch (vhost->state) {
1051         case IBMVFC_INITIALIZING:
1052         case IBMVFC_ACTIVE:
1053                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1054                 break;
1055         case IBMVFC_LINK_DOWN:
1056                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1057                 break;
1058         case IBMVFC_LINK_DEAD:
1059         case IBMVFC_HOST_OFFLINE:
1060                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1061                 break;
1062         case IBMVFC_HALTED:
1063                 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
1064                 break;
1065         case IBMVFC_NO_CRQ:
1066                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1067                 break;
1068         default:
1069                 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
1070                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1071                 break;
1072         }
1073         spin_unlock_irqrestore(shost->host_lock, flags);
1074 }
1075
1076 /**
1077  * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1078  * @rport:              rport struct
1079  * @timeout:    timeout value
1080  *
1081  * Return value:
1082  *      none
1083  **/
1084 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
1085 {
1086         if (timeout)
1087                 rport->dev_loss_tmo = timeout;
1088         else
1089                 rport->dev_loss_tmo = 1;
1090 }
1091
1092 /**
1093  * ibmvfc_release_tgt - Free memory allocated for a target
1094  * @kref:               kref struct
1095  *
1096  **/
1097 static void ibmvfc_release_tgt(struct kref *kref)
1098 {
1099         struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1100         kfree(tgt);
1101 }
1102
1103 /**
1104  * ibmvfc_get_starget_node_name - Get SCSI target's node name
1105  * @starget:    scsi target struct
1106  *
1107  * Return value:
1108  *      none
1109  **/
1110 static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1111 {
1112         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1113         fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
1114         if (tgt)
1115                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1116 }
1117
1118 /**
1119  * ibmvfc_get_starget_port_name - Get SCSI target's port name
1120  * @starget:    scsi target struct
1121  *
1122  * Return value:
1123  *      none
1124  **/
1125 static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1126 {
1127         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1128         fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
1129         if (tgt)
1130                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1131 }
1132
1133 /**
1134  * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1135  * @starget:    scsi target struct
1136  *
1137  * Return value:
1138  *      none
1139  **/
1140 static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1141 {
1142         struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
1143         fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
1144         if (tgt)
1145                 kref_put(&tgt->kref, ibmvfc_release_tgt);
1146 }
1147
1148 /**
1149  * ibmvfc_wait_while_resetting - Wait while the host resets
1150  * @vhost:              ibmvfc host struct
1151  *
1152  * Return value:
1153  *      0 on success / other on failure
1154  **/
1155 static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1156 {
1157         long timeout = wait_event_timeout(vhost->init_wait_q,
1158                                           ((vhost->state == IBMVFC_ACTIVE ||
1159                                             vhost->state == IBMVFC_HOST_OFFLINE ||
1160                                             vhost->state == IBMVFC_LINK_DEAD) &&
1161                                            vhost->action == IBMVFC_HOST_ACTION_NONE),
1162                                           (init_timeout * HZ));
1163
1164         return timeout ? 0 : -EIO;
1165 }
1166
1167 /**
1168  * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1169  * @shost:              scsi host struct
1170  *
1171  * Return value:
1172  *      0 on success / other on failure
1173  **/
1174 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1175 {
1176         struct ibmvfc_host *vhost = shost_priv(shost);
1177
1178         dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1179         ibmvfc_reset_host(vhost);
1180         return ibmvfc_wait_while_resetting(vhost);
1181 }
1182
1183 /**
1184  * ibmvfc_gather_partition_info - Gather info about the LPAR
1185  *
1186  * Return value:
1187  *      none
1188  **/
1189 static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1190 {
1191         struct device_node *rootdn;
1192         const char *name;
1193         const unsigned int *num;
1194
1195         rootdn = of_find_node_by_path("/");
1196         if (!rootdn)
1197                 return;
1198
1199         name = of_get_property(rootdn, "ibm,partition-name", NULL);
1200         if (name)
1201                 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1202         num = of_get_property(rootdn, "ibm,partition-no", NULL);
1203         if (num)
1204                 vhost->partition_number = *num;
1205         of_node_put(rootdn);
1206 }
1207
1208 /**
1209  * ibmvfc_set_login_info - Setup info for NPIV login
1210  * @vhost:      ibmvfc host struct
1211  *
1212  * Return value:
1213  *      none
1214  **/
1215 static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1216 {
1217         struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1218         struct device_node *of_node = vhost->dev->of_node;
1219         const char *location;
1220
1221         memset(login_info, 0, sizeof(*login_info));
1222
1223         login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
1224         login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
1225         login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
1226         login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
1227         login_info->partition_num = cpu_to_be32(vhost->partition_number);
1228         login_info->vfc_frame_version = cpu_to_be32(1);
1229         login_info->fcp_version = cpu_to_be16(3);
1230         login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
1231         if (vhost->client_migrated)
1232                 login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
1233
1234         login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
1235         login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
1236         login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
1237         login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
1238         strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1239         strncpy(login_info->device_name,
1240                 dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
1241
1242         location = of_get_property(of_node, "ibm,loc-code", NULL);
1243         location = location ? location : dev_name(vhost->dev);
1244         strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1245 }
1246
1247 /**
1248  * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1249  * @vhost:      ibmvfc host who owns the event pool
1250  *
1251  * Returns zero on success.
1252  **/
1253 static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1254 {
1255         int i;
1256         struct ibmvfc_event_pool *pool = &vhost->pool;
1257
1258         ENTER;
1259         pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1260         pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1261         if (!pool->events)
1262                 return -ENOMEM;
1263
1264         pool->iu_storage = dma_alloc_coherent(vhost->dev,
1265                                               pool->size * sizeof(*pool->iu_storage),
1266                                               &pool->iu_token, 0);
1267
1268         if (!pool->iu_storage) {
1269                 kfree(pool->events);
1270                 return -ENOMEM;
1271         }
1272
1273         for (i = 0; i < pool->size; ++i) {
1274                 struct ibmvfc_event *evt = &pool->events[i];
1275                 atomic_set(&evt->free, 1);
1276                 evt->crq.valid = 0x80;
1277                 evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
1278                 evt->xfer_iu = pool->iu_storage + i;
1279                 evt->vhost = vhost;
1280                 evt->ext_list = NULL;
1281                 list_add_tail(&evt->queue, &vhost->free);
1282         }
1283
1284         LEAVE;
1285         return 0;
1286 }
1287
1288 /**
1289  * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1290  * @vhost:      ibmvfc host who owns the event pool
1291  *
1292  **/
1293 static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1294 {
1295         int i;
1296         struct ibmvfc_event_pool *pool = &vhost->pool;
1297
1298         ENTER;
1299         for (i = 0; i < pool->size; ++i) {
1300                 list_del(&pool->events[i].queue);
1301                 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1302                 if (pool->events[i].ext_list)
1303                         dma_pool_free(vhost->sg_pool,
1304                                       pool->events[i].ext_list,
1305                                       pool->events[i].ext_list_token);
1306         }
1307
1308         kfree(pool->events);
1309         dma_free_coherent(vhost->dev,
1310                           pool->size * sizeof(*pool->iu_storage),
1311                           pool->iu_storage, pool->iu_token);
1312         LEAVE;
1313 }
1314
1315 /**
1316  * ibmvfc_get_event - Gets the next free event in pool
1317  * @vhost:      ibmvfc host struct
1318  *
1319  * Returns a free event from the pool.
1320  **/
1321 static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1322 {
1323         struct ibmvfc_event *evt;
1324
1325         BUG_ON(list_empty(&vhost->free));
1326         evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1327         atomic_set(&evt->free, 0);
1328         list_del(&evt->queue);
1329         return evt;
1330 }
1331
1332 /**
1333  * ibmvfc_init_event - Initialize fields in an event struct that are always
1334  *                              required.
1335  * @evt:        The event
1336  * @done:       Routine to call when the event is responded to
1337  * @format:     SRP or MAD format
1338  **/
1339 static void ibmvfc_init_event(struct ibmvfc_event *evt,
1340                               void (*done) (struct ibmvfc_event *), u8 format)
1341 {
1342         evt->cmnd = NULL;
1343         evt->sync_iu = NULL;
1344         evt->crq.format = format;
1345         evt->done = done;
1346         evt->eh_comp = NULL;
1347 }
1348
1349 /**
1350  * ibmvfc_map_sg_list - Initialize scatterlist
1351  * @scmd:       scsi command struct
1352  * @nseg:       number of scatterlist segments
1353  * @md: memory descriptor list to initialize
1354  **/
1355 static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1356                                struct srp_direct_buf *md)
1357 {
1358         int i;
1359         struct scatterlist *sg;
1360
1361         scsi_for_each_sg(scmd, sg, nseg, i) {
1362                 md[i].va = cpu_to_be64(sg_dma_address(sg));
1363                 md[i].len = cpu_to_be32(sg_dma_len(sg));
1364                 md[i].key = 0;
1365         }
1366 }
1367
1368 /**
1369  * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes descriptor fields
1370  * @scmd:               struct scsi_cmnd with the scatterlist
1371  * @evt:                ibmvfc event struct
1372  * @vfc_cmd:    vfc_cmd that contains the memory descriptor
1373  * @dev:                device for which to map dma memory
1374  *
1375  * Returns:
1376  *      0 on success / non-zero on failure
1377  **/
1378 static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1379                               struct ibmvfc_event *evt,
1380                               struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1381 {
1382
1383         int sg_mapped;
1384         struct srp_direct_buf *data = &vfc_cmd->ioba;
1385         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1386
1387         if (cls3_error)
1388                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_CLASS_3_ERR);
1389
1390         sg_mapped = scsi_dma_map(scmd);
1391         if (!sg_mapped) {
1392                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
1393                 return 0;
1394         } else if (unlikely(sg_mapped < 0)) {
1395                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1396                         scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1397                 return sg_mapped;
1398         }
1399
1400         if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1401                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
1402                 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1403         } else {
1404                 vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
1405                 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1406         }
1407
1408         if (sg_mapped == 1) {
1409                 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1410                 return 0;
1411         }
1412
1413         vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
1414
1415         if (!evt->ext_list) {
1416                 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1417                                                &evt->ext_list_token);
1418
1419                 if (!evt->ext_list) {
1420                         scsi_dma_unmap(scmd);
1421                         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1422                                 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1423                         return -ENOMEM;
1424                 }
1425         }
1426
1427         ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1428
1429         data->va = cpu_to_be64(evt->ext_list_token);
1430         data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
1431         data->key = 0;
1432         return 0;
1433 }
1434
1435 /**
1436  * ibmvfc_timeout - Internal command timeout handler
1437  * @evt:        struct ibmvfc_event that timed out
1438  *
1439  * Called when an internally generated command times out
1440  **/
1441 static void ibmvfc_timeout(struct timer_list *t)
1442 {
1443         struct ibmvfc_event *evt = from_timer(evt, t, timer);
1444         struct ibmvfc_host *vhost = evt->vhost;
1445         dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1446         ibmvfc_reset_host(vhost);
1447 }
1448
1449 /**
1450  * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1451  * @evt:                event to be sent
1452  * @vhost:              ibmvfc host struct
1453  * @timeout:    timeout in seconds - 0 means do not time command
1454  *
1455  * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1456  **/
1457 static int ibmvfc_send_event(struct ibmvfc_event *evt,
1458                              struct ibmvfc_host *vhost, unsigned long timeout)
1459 {
1460         __be64 *crq_as_u64 = (__be64 *) &evt->crq;
1461         int rc;
1462
1463         /* Copy the IU into the transfer area */
1464         *evt->xfer_iu = evt->iu;
1465         if (evt->crq.format == IBMVFC_CMD_FORMAT)
1466                 evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
1467         else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1468                 evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
1469         else
1470                 BUG();
1471
1472         list_add_tail(&evt->queue, &vhost->sent);
1473         timer_setup(&evt->timer, ibmvfc_timeout, 0);
1474
1475         if (timeout) {
1476                 evt->timer.expires = jiffies + (timeout * HZ);
1477                 add_timer(&evt->timer);
1478         }
1479
1480         mb();
1481
1482         if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
1483                                   be64_to_cpu(crq_as_u64[1])))) {
1484                 list_del(&evt->queue);
1485                 del_timer(&evt->timer);
1486
1487                 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1488                  * Firmware will send a CRQ with a transport event (0xFF) to
1489                  * tell this client what has happened to the transport. This
1490                  * will be handled in ibmvfc_handle_crq()
1491                  */
1492                 if (rc == H_CLOSED) {
1493                         if (printk_ratelimit())
1494                                 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1495                         if (evt->cmnd)
1496                                 scsi_dma_unmap(evt->cmnd);
1497                         ibmvfc_free_event(evt);
1498                         return SCSI_MLQUEUE_HOST_BUSY;
1499                 }
1500
1501                 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1502                 if (evt->cmnd) {
1503                         evt->cmnd->result = DID_ERROR << 16;
1504                         evt->done = ibmvfc_scsi_eh_done;
1505                 } else
1506                         evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
1507
1508                 evt->done(evt);
1509         } else
1510                 ibmvfc_trc_start(evt);
1511
1512         return 0;
1513 }
1514
1515 /**
1516  * ibmvfc_log_error - Log an error for the failed command if appropriate
1517  * @evt:        ibmvfc event to log
1518  *
1519  **/
1520 static void ibmvfc_log_error(struct ibmvfc_event *evt)
1521 {
1522         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1523         struct ibmvfc_host *vhost = evt->vhost;
1524         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1525         struct scsi_cmnd *cmnd = evt->cmnd;
1526         const char *err = unknown_error;
1527         int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
1528         int logerr = 0;
1529         int rsp_code = 0;
1530
1531         if (index >= 0) {
1532                 logerr = cmd_status[index].log;
1533                 err = cmd_status[index].name;
1534         }
1535
1536         if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
1537                 return;
1538
1539         if (rsp->flags & FCP_RSP_LEN_VALID)
1540                 rsp_code = rsp->data.info.rsp_code;
1541
1542         scmd_printk(KERN_ERR, cmnd, "Command (%02X) : %s (%x:%x) "
1543                     "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1544                     cmnd->cmnd[0], err, be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error),
1545                     rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1546 }
1547
1548 /**
1549  * ibmvfc_relogin - Log back into the specified device
1550  * @sdev:       scsi device struct
1551  *
1552  **/
1553 static void ibmvfc_relogin(struct scsi_device *sdev)
1554 {
1555         struct ibmvfc_host *vhost = shost_priv(sdev->host);
1556         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1557         struct ibmvfc_target *tgt;
1558
1559         list_for_each_entry(tgt, &vhost->targets, queue) {
1560                 if (rport == tgt->rport) {
1561                         ibmvfc_del_tgt(tgt);
1562                         break;
1563                 }
1564         }
1565
1566         ibmvfc_reinit_host(vhost);
1567 }
1568
1569 /**
1570  * ibmvfc_scsi_done - Handle responses from commands
1571  * @evt:        ibmvfc event to be handled
1572  *
1573  * Used as a callback when sending scsi cmds.
1574  **/
1575 static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1576 {
1577         struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1578         struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1579         struct scsi_cmnd *cmnd = evt->cmnd;
1580         u32 rsp_len = 0;
1581         u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
1582
1583         if (cmnd) {
1584                 if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
1585                         scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
1586                 else if (rsp->flags & FCP_RESID_UNDER)
1587                         scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
1588                 else
1589                         scsi_set_resid(cmnd, 0);
1590
1591                 if (vfc_cmd->status) {
1592                         cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1593
1594                         if (rsp->flags & FCP_RSP_LEN_VALID)
1595                                 rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
1596                         if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1597                                 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
1598                         if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
1599                                 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1600                         if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
1601                             (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
1602                                 ibmvfc_relogin(cmnd->device);
1603
1604                         if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1605                                 cmnd->result = (DID_ERROR << 16);
1606
1607                         ibmvfc_log_error(evt);
1608                 }
1609
1610                 if (!cmnd->result &&
1611                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1612                         cmnd->result = (DID_ERROR << 16);
1613
1614                 scsi_dma_unmap(cmnd);
1615                 cmnd->scsi_done(cmnd);
1616         }
1617
1618         if (evt->eh_comp)
1619                 complete(evt->eh_comp);
1620
1621         ibmvfc_free_event(evt);
1622 }
1623
1624 /**
1625  * ibmvfc_host_chkready - Check if the host can accept commands
1626  * @vhost:       struct ibmvfc host
1627  *
1628  * Returns:
1629  *      1 if host can accept command / 0 if not
1630  **/
1631 static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1632 {
1633         int result = 0;
1634
1635         switch (vhost->state) {
1636         case IBMVFC_LINK_DEAD:
1637         case IBMVFC_HOST_OFFLINE:
1638                 result = DID_NO_CONNECT << 16;
1639                 break;
1640         case IBMVFC_NO_CRQ:
1641         case IBMVFC_INITIALIZING:
1642         case IBMVFC_HALTED:
1643         case IBMVFC_LINK_DOWN:
1644                 result = DID_REQUEUE << 16;
1645                 break;
1646         case IBMVFC_ACTIVE:
1647                 result = 0;
1648                 break;
1649         }
1650
1651         return result;
1652 }
1653
1654 /**
1655  * ibmvfc_queuecommand - The queuecommand function of the scsi template
1656  * @cmnd:       struct scsi_cmnd to be executed
1657  * @done:       Callback function to be called when cmnd is completed
1658  *
1659  * Returns:
1660  *      0 on success / other on failure
1661  **/
1662 static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
1663                                void (*done) (struct scsi_cmnd *))
1664 {
1665         struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1666         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1667         struct ibmvfc_cmd *vfc_cmd;
1668         struct ibmvfc_event *evt;
1669         int rc;
1670
1671         if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1672             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1673                 cmnd->result = rc;
1674                 done(cmnd);
1675                 return 0;
1676         }
1677
1678         cmnd->result = (DID_OK << 16);
1679         evt = ibmvfc_get_event(vhost);
1680         ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1681         evt->cmnd = cmnd;
1682         cmnd->scsi_done = done;
1683         vfc_cmd = &evt->iu.cmd;
1684         memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1685         vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
1686         vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1687         vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
1688         vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
1689         vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
1690         vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
1691         vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
1692         vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
1693         int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1694         memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1695
1696         if (cmnd->flags & SCMD_TAGGED) {
1697                 vfc_cmd->task_tag = cpu_to_be64(cmnd->tag);
1698                 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1699         }
1700
1701         if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1702                 return ibmvfc_send_event(evt, vhost, 0);
1703
1704         ibmvfc_free_event(evt);
1705         if (rc == -ENOMEM)
1706                 return SCSI_MLQUEUE_HOST_BUSY;
1707
1708         if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1709                 scmd_printk(KERN_ERR, cmnd,
1710                             "Failed to map DMA buffer for command. rc=%d\n", rc);
1711
1712         cmnd->result = DID_ERROR << 16;
1713         done(cmnd);
1714         return 0;
1715 }
1716
1717 static DEF_SCSI_QCMD(ibmvfc_queuecommand)
1718
1719 /**
1720  * ibmvfc_sync_completion - Signal that a synchronous command has completed
1721  * @evt:        ibmvfc event struct
1722  *
1723  **/
1724 static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1725 {
1726         /* copy the response back */
1727         if (evt->sync_iu)
1728                 *evt->sync_iu = *evt->xfer_iu;
1729
1730         complete(&evt->comp);
1731 }
1732
1733 /**
1734  * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1735  * @evt:        struct ibmvfc_event
1736  *
1737  **/
1738 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
1739 {
1740         struct ibmvfc_host *vhost = evt->vhost;
1741
1742         ibmvfc_free_event(evt);
1743         vhost->aborting_passthru = 0;
1744         dev_info(vhost->dev, "Passthru command cancelled\n");
1745 }
1746
1747 /**
1748  * ibmvfc_bsg_timeout - Handle a BSG timeout
1749  * @job:        struct bsg_job that timed out
1750  *
1751  * Returns:
1752  *      0 on success / other on failure
1753  **/
1754 static int ibmvfc_bsg_timeout(struct bsg_job *job)
1755 {
1756         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1757         unsigned long port_id = (unsigned long)job->dd_data;
1758         struct ibmvfc_event *evt;
1759         struct ibmvfc_tmf *tmf;
1760         unsigned long flags;
1761         int rc;
1762
1763         ENTER;
1764         spin_lock_irqsave(vhost->host->host_lock, flags);
1765         if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
1766                 __ibmvfc_reset_host(vhost);
1767                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1768                 return 0;
1769         }
1770
1771         vhost->aborting_passthru = 1;
1772         evt = ibmvfc_get_event(vhost);
1773         ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
1774
1775         tmf = &evt->iu.tmf;
1776         memset(tmf, 0, sizeof(*tmf));
1777         tmf->common.version = cpu_to_be32(1);
1778         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
1779         tmf->common.length = cpu_to_be16(sizeof(*tmf));
1780         tmf->scsi_id = cpu_to_be64(port_id);
1781         tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1782         tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
1783         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1784
1785         if (rc != 0) {
1786                 vhost->aborting_passthru = 0;
1787                 dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
1788                 rc = -EIO;
1789         } else
1790                 dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
1791                          port_id);
1792
1793         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1794
1795         LEAVE;
1796         return rc;
1797 }
1798
1799 /**
1800  * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1801  * @vhost:              struct ibmvfc_host to send command
1802  * @port_id:    port ID to send command
1803  *
1804  * Returns:
1805  *      0 on success / other on failure
1806  **/
1807 static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
1808 {
1809         struct ibmvfc_port_login *plogi;
1810         struct ibmvfc_target *tgt;
1811         struct ibmvfc_event *evt;
1812         union ibmvfc_iu rsp_iu;
1813         unsigned long flags;
1814         int rc = 0, issue_login = 1;
1815
1816         ENTER;
1817         spin_lock_irqsave(vhost->host->host_lock, flags);
1818         list_for_each_entry(tgt, &vhost->targets, queue) {
1819                 if (tgt->scsi_id == port_id) {
1820                         issue_login = 0;
1821                         break;
1822                 }
1823         }
1824
1825         if (!issue_login)
1826                 goto unlock_out;
1827         if (unlikely((rc = ibmvfc_host_chkready(vhost))))
1828                 goto unlock_out;
1829
1830         evt = ibmvfc_get_event(vhost);
1831         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1832         plogi = &evt->iu.plogi;
1833         memset(plogi, 0, sizeof(*plogi));
1834         plogi->common.version = cpu_to_be32(1);
1835         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
1836         plogi->common.length = cpu_to_be16(sizeof(*plogi));
1837         plogi->scsi_id = cpu_to_be64(port_id);
1838         evt->sync_iu = &rsp_iu;
1839         init_completion(&evt->comp);
1840
1841         rc = ibmvfc_send_event(evt, vhost, default_timeout);
1842         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1843
1844         if (rc)
1845                 return -EIO;
1846
1847         wait_for_completion(&evt->comp);
1848
1849         if (rsp_iu.plogi.common.status)
1850                 rc = -EIO;
1851
1852         spin_lock_irqsave(vhost->host->host_lock, flags);
1853         ibmvfc_free_event(evt);
1854 unlock_out:
1855         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1856         LEAVE;
1857         return rc;
1858 }
1859
1860 /**
1861  * ibmvfc_bsg_request - Handle a BSG request
1862  * @job:        struct bsg_job to be executed
1863  *
1864  * Returns:
1865  *      0 on success / other on failure
1866  **/
1867 static int ibmvfc_bsg_request(struct bsg_job *job)
1868 {
1869         struct ibmvfc_host *vhost = shost_priv(fc_bsg_to_shost(job));
1870         struct fc_rport *rport = fc_bsg_to_rport(job);
1871         struct ibmvfc_passthru_mad *mad;
1872         struct ibmvfc_event *evt;
1873         union ibmvfc_iu rsp_iu;
1874         unsigned long flags, port_id = -1;
1875         struct fc_bsg_request *bsg_request = job->request;
1876         struct fc_bsg_reply *bsg_reply = job->reply;
1877         unsigned int code = bsg_request->msgcode;
1878         int rc = 0, req_seg, rsp_seg, issue_login = 0;
1879         u32 fc_flags, rsp_len;
1880
1881         ENTER;
1882         bsg_reply->reply_payload_rcv_len = 0;
1883         if (rport)
1884                 port_id = rport->port_id;
1885
1886         switch (code) {
1887         case FC_BSG_HST_ELS_NOLOGIN:
1888                 port_id = (bsg_request->rqst_data.h_els.port_id[0] << 16) |
1889                         (bsg_request->rqst_data.h_els.port_id[1] << 8) |
1890                         bsg_request->rqst_data.h_els.port_id[2];
1891                 fallthrough;
1892         case FC_BSG_RPT_ELS:
1893                 fc_flags = IBMVFC_FC_ELS;
1894                 break;
1895         case FC_BSG_HST_CT:
1896                 issue_login = 1;
1897                 port_id = (bsg_request->rqst_data.h_ct.port_id[0] << 16) |
1898                         (bsg_request->rqst_data.h_ct.port_id[1] << 8) |
1899                         bsg_request->rqst_data.h_ct.port_id[2];
1900                 fallthrough;
1901         case FC_BSG_RPT_CT:
1902                 fc_flags = IBMVFC_FC_CT_IU;
1903                 break;
1904         default:
1905                 return -ENOTSUPP;
1906         }
1907
1908         if (port_id == -1)
1909                 return -EINVAL;
1910         if (!mutex_trylock(&vhost->passthru_mutex))
1911                 return -EBUSY;
1912
1913         job->dd_data = (void *)port_id;
1914         req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
1915                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1916
1917         if (!req_seg) {
1918                 mutex_unlock(&vhost->passthru_mutex);
1919                 return -ENOMEM;
1920         }
1921
1922         rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
1923                              job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1924
1925         if (!rsp_seg) {
1926                 dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
1927                              job->request_payload.sg_cnt, DMA_TO_DEVICE);
1928                 mutex_unlock(&vhost->passthru_mutex);
1929                 return -ENOMEM;
1930         }
1931
1932         if (req_seg > 1 || rsp_seg > 1) {
1933                 rc = -EINVAL;
1934                 goto out;
1935         }
1936
1937         if (issue_login)
1938                 rc = ibmvfc_bsg_plogi(vhost, port_id);
1939
1940         spin_lock_irqsave(vhost->host->host_lock, flags);
1941
1942         if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
1943             unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1944                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1945                 goto out;
1946         }
1947
1948         evt = ibmvfc_get_event(vhost);
1949         ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1950         mad = &evt->iu.passthru;
1951
1952         memset(mad, 0, sizeof(*mad));
1953         mad->common.version = cpu_to_be32(1);
1954         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
1955         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
1956
1957         mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
1958                 offsetof(struct ibmvfc_passthru_mad, iu));
1959         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
1960
1961         mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
1962         mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
1963         mad->iu.flags = cpu_to_be32(fc_flags);
1964         mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
1965
1966         mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
1967         mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
1968         mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
1969         mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
1970         mad->iu.scsi_id = cpu_to_be64(port_id);
1971         mad->iu.tag = cpu_to_be64((u64)evt);
1972         rsp_len = be32_to_cpu(mad->iu.rsp.len);
1973
1974         evt->sync_iu = &rsp_iu;
1975         init_completion(&evt->comp);
1976         rc = ibmvfc_send_event(evt, vhost, 0);
1977         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1978
1979         if (rc) {
1980                 rc = -EIO;
1981                 goto out;
1982         }
1983
1984         wait_for_completion(&evt->comp);
1985
1986         if (rsp_iu.passthru.common.status)
1987                 rc = -EIO;
1988         else
1989                 bsg_reply->reply_payload_rcv_len = rsp_len;
1990
1991         spin_lock_irqsave(vhost->host->host_lock, flags);
1992         ibmvfc_free_event(evt);
1993         spin_unlock_irqrestore(vhost->host->host_lock, flags);
1994         bsg_reply->result = rc;
1995         bsg_job_done(job, bsg_reply->result,
1996                        bsg_reply->reply_payload_rcv_len);
1997         rc = 0;
1998 out:
1999         dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
2000                      job->request_payload.sg_cnt, DMA_TO_DEVICE);
2001         dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
2002                      job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2003         mutex_unlock(&vhost->passthru_mutex);
2004         LEAVE;
2005         return rc;
2006 }
2007
2008 /**
2009  * ibmvfc_reset_device - Reset the device with the specified reset type
2010  * @sdev:       scsi device to reset
2011  * @type:       reset type
2012  * @desc:       reset type description for log messages
2013  *
2014  * Returns:
2015  *      0 on success / other on failure
2016  **/
2017 static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
2018 {
2019         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2020         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2021         struct ibmvfc_cmd *tmf;
2022         struct ibmvfc_event *evt = NULL;
2023         union ibmvfc_iu rsp_iu;
2024         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2025         int rsp_rc = -EBUSY;
2026         unsigned long flags;
2027         int rsp_code = 0;
2028
2029         spin_lock_irqsave(vhost->host->host_lock, flags);
2030         if (vhost->state == IBMVFC_ACTIVE) {
2031                 evt = ibmvfc_get_event(vhost);
2032                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2033
2034                 tmf = &evt->iu.cmd;
2035                 memset(tmf, 0, sizeof(*tmf));
2036                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2037                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2038                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2039                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2040                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2041                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2042                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2043                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2044                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2045                 tmf->iu.tmf_flags = type;
2046                 evt->sync_iu = &rsp_iu;
2047
2048                 init_completion(&evt->comp);
2049                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2050         }
2051         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2052
2053         if (rsp_rc != 0) {
2054                 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
2055                             desc, rsp_rc);
2056                 return -EIO;
2057         }
2058
2059         sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
2060         wait_for_completion(&evt->comp);
2061
2062         if (rsp_iu.cmd.status)
2063                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2064
2065         if (rsp_code) {
2066                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2067                         rsp_code = fc_rsp->data.info.rsp_code;
2068
2069                 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
2070                             "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
2071                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2072                             be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2073                             fc_rsp->scsi_status);
2074                 rsp_rc = -EIO;
2075         } else
2076                 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
2077
2078         spin_lock_irqsave(vhost->host->host_lock, flags);
2079         ibmvfc_free_event(evt);
2080         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2081         return rsp_rc;
2082 }
2083
2084 /**
2085  * ibmvfc_match_rport - Match function for specified remote port
2086  * @evt:        ibmvfc event struct
2087  * @device:     device to match (rport)
2088  *
2089  * Returns:
2090  *      1 if event matches rport / 0 if event does not match rport
2091  **/
2092 static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
2093 {
2094         struct fc_rport *cmd_rport;
2095
2096         if (evt->cmnd) {
2097                 cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
2098                 if (cmd_rport == rport)
2099                         return 1;
2100         }
2101         return 0;
2102 }
2103
2104 /**
2105  * ibmvfc_match_target - Match function for specified target
2106  * @evt:        ibmvfc event struct
2107  * @device:     device to match (starget)
2108  *
2109  * Returns:
2110  *      1 if event matches starget / 0 if event does not match starget
2111  **/
2112 static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
2113 {
2114         if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
2115                 return 1;
2116         return 0;
2117 }
2118
2119 /**
2120  * ibmvfc_match_lun - Match function for specified LUN
2121  * @evt:        ibmvfc event struct
2122  * @device:     device to match (sdev)
2123  *
2124  * Returns:
2125  *      1 if event matches sdev / 0 if event does not match sdev
2126  **/
2127 static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
2128 {
2129         if (evt->cmnd && evt->cmnd->device == device)
2130                 return 1;
2131         return 0;
2132 }
2133
2134 /**
2135  * ibmvfc_wait_for_ops - Wait for ops to complete
2136  * @vhost:      ibmvfc host struct
2137  * @device:     device to match (starget or sdev)
2138  * @match:      match function
2139  *
2140  * Returns:
2141  *      SUCCESS / FAILED
2142  **/
2143 static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
2144                                int (*match) (struct ibmvfc_event *, void *))
2145 {
2146         struct ibmvfc_event *evt;
2147         DECLARE_COMPLETION_ONSTACK(comp);
2148         int wait;
2149         unsigned long flags;
2150         signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
2151
2152         ENTER;
2153         do {
2154                 wait = 0;
2155                 spin_lock_irqsave(vhost->host->host_lock, flags);
2156                 list_for_each_entry(evt, &vhost->sent, queue) {
2157                         if (match(evt, device)) {
2158                                 evt->eh_comp = &comp;
2159                                 wait++;
2160                         }
2161                 }
2162                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2163
2164                 if (wait) {
2165                         timeout = wait_for_completion_timeout(&comp, timeout);
2166
2167                         if (!timeout) {
2168                                 wait = 0;
2169                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2170                                 list_for_each_entry(evt, &vhost->sent, queue) {
2171                                         if (match(evt, device)) {
2172                                                 evt->eh_comp = NULL;
2173                                                 wait++;
2174                                         }
2175                                 }
2176                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2177                                 if (wait)
2178                                         dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
2179                                 LEAVE;
2180                                 return wait ? FAILED : SUCCESS;
2181                         }
2182                 }
2183         } while (wait);
2184
2185         LEAVE;
2186         return SUCCESS;
2187 }
2188
2189 /**
2190  * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2191  * @sdev:       scsi device to cancel commands
2192  * @type:       type of error recovery being performed
2193  *
2194  * This sends a cancel to the VIOS for the specified device. This does
2195  * NOT send any abort to the actual device. That must be done separately.
2196  *
2197  * Returns:
2198  *      0 on success / other on failure
2199  **/
2200 static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
2201 {
2202         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2203         struct scsi_target *starget = scsi_target(sdev);
2204         struct fc_rport *rport = starget_to_rport(starget);
2205         struct ibmvfc_tmf *tmf;
2206         struct ibmvfc_event *evt, *found_evt;
2207         union ibmvfc_iu rsp;
2208         int rsp_rc = -EBUSY;
2209         unsigned long flags;
2210         u16 status;
2211
2212         ENTER;
2213         spin_lock_irqsave(vhost->host->host_lock, flags);
2214         found_evt = NULL;
2215         list_for_each_entry(evt, &vhost->sent, queue) {
2216                 if (evt->cmnd && evt->cmnd->device == sdev) {
2217                         found_evt = evt;
2218                         break;
2219                 }
2220         }
2221
2222         if (!found_evt) {
2223                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2224                         sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
2225                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2226                 return 0;
2227         }
2228
2229         if (vhost->logged_in) {
2230                 evt = ibmvfc_get_event(vhost);
2231                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
2232
2233                 tmf = &evt->iu.tmf;
2234                 memset(tmf, 0, sizeof(*tmf));
2235                 tmf->common.version = cpu_to_be32(1);
2236                 tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
2237                 tmf->common.length = cpu_to_be16(sizeof(*tmf));
2238                 tmf->scsi_id = cpu_to_be64(rport->port_id);
2239                 int_to_scsilun(sdev->lun, &tmf->lun);
2240                 if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
2241                         type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
2242                 if (vhost->state == IBMVFC_ACTIVE)
2243                         tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
2244                 else
2245                         tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
2246                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2247                 tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
2248
2249                 evt->sync_iu = &rsp;
2250                 init_completion(&evt->comp);
2251                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2252         }
2253
2254         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2255
2256         if (rsp_rc != 0) {
2257                 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
2258                 /* If failure is received, the host adapter is most likely going
2259                  through reset, return success so the caller will wait for the command
2260                  being cancelled to get returned */
2261                 return 0;
2262         }
2263
2264         sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
2265
2266         wait_for_completion(&evt->comp);
2267         status = be16_to_cpu(rsp.mad_common.status);
2268         spin_lock_irqsave(vhost->host->host_lock, flags);
2269         ibmvfc_free_event(evt);
2270         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2271
2272         if (status != IBMVFC_MAD_SUCCESS) {
2273                 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
2274                 switch (status) {
2275                 case IBMVFC_MAD_DRIVER_FAILED:
2276                 case IBMVFC_MAD_CRQ_ERROR:
2277                         /* Host adapter most likely going through reset, return success to
2278                          the caller will wait for the command being cancelled to get returned */
2279                         return 0;
2280                 default:
2281                         return -EIO;
2282                 };
2283         }
2284
2285         sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
2286         return 0;
2287 }
2288
2289 /**
2290  * ibmvfc_match_key - Match function for specified cancel key
2291  * @evt:        ibmvfc event struct
2292  * @key:        cancel key to match
2293  *
2294  * Returns:
2295  *      1 if event matches key / 0 if event does not match key
2296  **/
2297 static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
2298 {
2299         unsigned long cancel_key = (unsigned long)key;
2300
2301         if (evt->crq.format == IBMVFC_CMD_FORMAT &&
2302             be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
2303                 return 1;
2304         return 0;
2305 }
2306
2307 /**
2308  * ibmvfc_match_evt - Match function for specified event
2309  * @evt:        ibmvfc event struct
2310  * @match:      event to match
2311  *
2312  * Returns:
2313  *      1 if event matches key / 0 if event does not match key
2314  **/
2315 static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
2316 {
2317         if (evt == match)
2318                 return 1;
2319         return 0;
2320 }
2321
2322 /**
2323  * ibmvfc_abort_task_set - Abort outstanding commands to the device
2324  * @sdev:       scsi device to abort commands
2325  *
2326  * This sends an Abort Task Set to the VIOS for the specified device. This does
2327  * NOT send any cancel to the VIOS. That must be done separately.
2328  *
2329  * Returns:
2330  *      0 on success / other on failure
2331  **/
2332 static int ibmvfc_abort_task_set(struct scsi_device *sdev)
2333 {
2334         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2335         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2336         struct ibmvfc_cmd *tmf;
2337         struct ibmvfc_event *evt, *found_evt;
2338         union ibmvfc_iu rsp_iu;
2339         struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
2340         int rc, rsp_rc = -EBUSY;
2341         unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
2342         int rsp_code = 0;
2343
2344         spin_lock_irqsave(vhost->host->host_lock, flags);
2345         found_evt = NULL;
2346         list_for_each_entry(evt, &vhost->sent, queue) {
2347                 if (evt->cmnd && evt->cmnd->device == sdev) {
2348                         found_evt = evt;
2349                         break;
2350                 }
2351         }
2352
2353         if (!found_evt) {
2354                 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
2355                         sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
2356                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2357                 return 0;
2358         }
2359
2360         if (vhost->state == IBMVFC_ACTIVE) {
2361                 evt = ibmvfc_get_event(vhost);
2362                 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
2363
2364                 tmf = &evt->iu.cmd;
2365                 memset(tmf, 0, sizeof(*tmf));
2366                 tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
2367                 tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
2368                 tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
2369                 tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
2370                 tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
2371                 tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
2372                 tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
2373                 int_to_scsilun(sdev->lun, &tmf->iu.lun);
2374                 tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
2375                 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
2376                 evt->sync_iu = &rsp_iu;
2377
2378                 init_completion(&evt->comp);
2379                 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
2380         }
2381
2382         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2383
2384         if (rsp_rc != 0) {
2385                 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
2386                 return -EIO;
2387         }
2388
2389         sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
2390         timeout = wait_for_completion_timeout(&evt->comp, timeout);
2391
2392         if (!timeout) {
2393                 rc = ibmvfc_cancel_all(sdev, 0);
2394                 if (!rc) {
2395                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2396                         if (rc == SUCCESS)
2397                                 rc = 0;
2398                 }
2399
2400                 if (rc) {
2401                         sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
2402                         ibmvfc_reset_host(vhost);
2403                         rsp_rc = -EIO;
2404                         rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
2405
2406                         if (rc == SUCCESS)
2407                                 rsp_rc = 0;
2408
2409                         rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
2410                         if (rc != SUCCESS) {
2411                                 spin_lock_irqsave(vhost->host->host_lock, flags);
2412                                 ibmvfc_hard_reset_host(vhost);
2413                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2414                                 rsp_rc = 0;
2415                         }
2416
2417                         goto out;
2418                 }
2419         }
2420
2421         if (rsp_iu.cmd.status)
2422                 rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
2423
2424         if (rsp_code) {
2425                 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
2426                         rsp_code = fc_rsp->data.info.rsp_code;
2427
2428                 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
2429                             "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2430                             ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
2431                             be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error), fc_rsp->flags, rsp_code,
2432                             fc_rsp->scsi_status);
2433                 rsp_rc = -EIO;
2434         } else
2435                 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
2436
2437 out:
2438         spin_lock_irqsave(vhost->host->host_lock, flags);
2439         ibmvfc_free_event(evt);
2440         spin_unlock_irqrestore(vhost->host->host_lock, flags);
2441         return rsp_rc;
2442 }
2443
2444 /**
2445  * ibmvfc_eh_abort_handler - Abort a command
2446  * @cmd:        scsi command to abort
2447  *
2448  * Returns:
2449  *      SUCCESS / FAST_IO_FAIL / FAILED
2450  **/
2451 static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
2452 {
2453         struct scsi_device *sdev = cmd->device;
2454         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2455         int cancel_rc, block_rc;
2456         int rc = FAILED;
2457
2458         ENTER;
2459         block_rc = fc_block_scsi_eh(cmd);
2460         ibmvfc_wait_while_resetting(vhost);
2461         if (block_rc != FAST_IO_FAIL) {
2462                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
2463                 ibmvfc_abort_task_set(sdev);
2464         } else
2465                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2466
2467         if (!cancel_rc)
2468                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2469
2470         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2471                 rc = FAST_IO_FAIL;
2472
2473         LEAVE;
2474         return rc;
2475 }
2476
2477 /**
2478  * ibmvfc_eh_device_reset_handler - Reset a single LUN
2479  * @cmd:        scsi command struct
2480  *
2481  * Returns:
2482  *      SUCCESS / FAST_IO_FAIL / FAILED
2483  **/
2484 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
2485 {
2486         struct scsi_device *sdev = cmd->device;
2487         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2488         int cancel_rc, block_rc, reset_rc = 0;
2489         int rc = FAILED;
2490
2491         ENTER;
2492         block_rc = fc_block_scsi_eh(cmd);
2493         ibmvfc_wait_while_resetting(vhost);
2494         if (block_rc != FAST_IO_FAIL) {
2495                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
2496                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
2497         } else
2498                 cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2499
2500         if (!cancel_rc && !reset_rc)
2501                 rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
2502
2503         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2504                 rc = FAST_IO_FAIL;
2505
2506         LEAVE;
2507         return rc;
2508 }
2509
2510 /**
2511  * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2512  * @sdev:       scsi device struct
2513  * @data:       return code
2514  *
2515  **/
2516 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
2517 {
2518         unsigned long *rc = data;
2519         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2520 }
2521
2522 /**
2523  * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2524  * @sdev:       scsi device struct
2525  * @data:       return code
2526  *
2527  **/
2528 static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
2529 {
2530         unsigned long *rc = data;
2531         *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
2532 }
2533
2534 /**
2535  * ibmvfc_eh_target_reset_handler - Reset the target
2536  * @cmd:        scsi command struct
2537  *
2538  * Returns:
2539  *      SUCCESS / FAST_IO_FAIL / FAILED
2540  **/
2541 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
2542 {
2543         struct scsi_device *sdev = cmd->device;
2544         struct ibmvfc_host *vhost = shost_priv(sdev->host);
2545         struct scsi_target *starget = scsi_target(sdev);
2546         int block_rc;
2547         int reset_rc = 0;
2548         int rc = FAILED;
2549         unsigned long cancel_rc = 0;
2550
2551         ENTER;
2552         block_rc = fc_block_scsi_eh(cmd);
2553         ibmvfc_wait_while_resetting(vhost);
2554         if (block_rc != FAST_IO_FAIL) {
2555                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
2556                 reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
2557         } else
2558                 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
2559
2560         if (!cancel_rc && !reset_rc)
2561                 rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
2562
2563         if (block_rc == FAST_IO_FAIL && rc != FAILED)
2564                 rc = FAST_IO_FAIL;
2565
2566         LEAVE;
2567         return rc;
2568 }
2569
2570 /**
2571  * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2572  * @cmd:        struct scsi_cmnd having problems
2573  *
2574  **/
2575 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
2576 {
2577         int rc;
2578         struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
2579
2580         dev_err(vhost->dev, "Resetting connection due to error recovery\n");
2581         rc = ibmvfc_issue_fc_host_lip(vhost->host);
2582
2583         return rc ? FAILED : SUCCESS;
2584 }
2585
2586 /**
2587  * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2588  * @rport:              rport struct
2589  *
2590  * Return value:
2591  *      none
2592  **/
2593 static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2594 {
2595         struct Scsi_Host *shost = rport_to_shost(rport);
2596         struct ibmvfc_host *vhost = shost_priv(shost);
2597         struct fc_rport *dev_rport;
2598         struct scsi_device *sdev;
2599         struct ibmvfc_target *tgt;
2600         unsigned long rc, flags;
2601         unsigned int found;
2602
2603         ENTER;
2604         shost_for_each_device(sdev, shost) {
2605                 dev_rport = starget_to_rport(scsi_target(sdev));
2606                 if (dev_rport != rport)
2607                         continue;
2608                 ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
2609         }
2610
2611         rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
2612
2613         if (rc == FAILED)
2614                 ibmvfc_issue_fc_host_lip(shost);
2615
2616         spin_lock_irqsave(shost->host_lock, flags);
2617         found = 0;
2618         list_for_each_entry(tgt, &vhost->targets, queue) {
2619                 if (tgt->scsi_id == rport->port_id) {
2620                         found++;
2621                         break;
2622                 }
2623         }
2624
2625         if (found && tgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
2626                 /*
2627                  * If we get here, that means we previously attempted to send
2628                  * an implicit logout to the target but it failed, most likely
2629                  * due to I/O being pending, so we need to send it again
2630                  */
2631                 ibmvfc_del_tgt(tgt);
2632                 ibmvfc_reinit_host(vhost);
2633         }
2634
2635         spin_unlock_irqrestore(shost->host_lock, flags);
2636         LEAVE;
2637 }
2638
2639 static const struct ibmvfc_async_desc ae_desc [] = {
2640         { "PLOGI",      IBMVFC_AE_ELS_PLOGI,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2641         { "LOGO",       IBMVFC_AE_ELS_LOGO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2642         { "PRLO",       IBMVFC_AE_ELS_PRLO,     IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2643         { "N-Port SCN", IBMVFC_AE_SCN_NPORT,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2644         { "Group SCN",  IBMVFC_AE_SCN_GROUP,    IBMVFC_DEFAULT_LOG_LEVEL + 1 },
2645         { "Domain SCN", IBMVFC_AE_SCN_DOMAIN,   IBMVFC_DEFAULT_LOG_LEVEL },
2646         { "Fabric SCN", IBMVFC_AE_SCN_FABRIC,   IBMVFC_DEFAULT_LOG_LEVEL },
2647         { "Link Up",    IBMVFC_AE_LINK_UP,      IBMVFC_DEFAULT_LOG_LEVEL },
2648         { "Link Down",  IBMVFC_AE_LINK_DOWN,    IBMVFC_DEFAULT_LOG_LEVEL },
2649         { "Link Dead",  IBMVFC_AE_LINK_DEAD,    IBMVFC_DEFAULT_LOG_LEVEL },
2650         { "Halt",       IBMVFC_AE_HALT,         IBMVFC_DEFAULT_LOG_LEVEL },
2651         { "Resume",     IBMVFC_AE_RESUME,       IBMVFC_DEFAULT_LOG_LEVEL },
2652         { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
2653 };
2654
2655 static const struct ibmvfc_async_desc unknown_ae = {
2656         "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2657 };
2658
2659 /**
2660  * ibmvfc_get_ae_desc - Get text description for async event
2661  * @ae: async event
2662  *
2663  **/
2664 static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
2665 {
2666         int i;
2667
2668         for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2669                 if (ae_desc[i].ae == ae)
2670                         return &ae_desc[i];
2671
2672         return &unknown_ae;
2673 }
2674
2675 static const struct {
2676         enum ibmvfc_ae_link_state state;
2677         const char *desc;
2678 } link_desc [] = {
2679         { IBMVFC_AE_LS_LINK_UP,         " link up" },
2680         { IBMVFC_AE_LS_LINK_BOUNCED,    " link bounced" },
2681         { IBMVFC_AE_LS_LINK_DOWN,       " link down" },
2682         { IBMVFC_AE_LS_LINK_DEAD,       " link dead" },
2683 };
2684
2685 /**
2686  * ibmvfc_get_link_state - Get text description for link state
2687  * @state:      link state
2688  *
2689  **/
2690 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
2691 {
2692         int i;
2693
2694         for (i = 0; i < ARRAY_SIZE(link_desc); i++)
2695                 if (link_desc[i].state == state)
2696                         return link_desc[i].desc;
2697
2698         return "";
2699 }
2700
2701 /**
2702  * ibmvfc_handle_async - Handle an async event from the adapter
2703  * @crq:        crq to process
2704  * @vhost:      ibmvfc host struct
2705  *
2706  **/
2707 static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2708                                 struct ibmvfc_host *vhost)
2709 {
2710         const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
2711         struct ibmvfc_target *tgt;
2712
2713         ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
2714                    " node_name: %llx%s\n", desc->desc, be64_to_cpu(crq->scsi_id),
2715                    be64_to_cpu(crq->wwpn), be64_to_cpu(crq->node_name),
2716                    ibmvfc_get_link_state(crq->link_state));
2717
2718         switch (be64_to_cpu(crq->event)) {
2719         case IBMVFC_AE_RESUME:
2720                 switch (crq->link_state) {
2721                 case IBMVFC_AE_LS_LINK_DOWN:
2722                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2723                         break;
2724                 case IBMVFC_AE_LS_LINK_DEAD:
2725                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2726                         break;
2727                 case IBMVFC_AE_LS_LINK_UP:
2728                 case IBMVFC_AE_LS_LINK_BOUNCED:
2729                 default:
2730                         vhost->events_to_log |= IBMVFC_AE_LINKUP;
2731                         vhost->delay_init = 1;
2732                         __ibmvfc_reset_host(vhost);
2733                         break;
2734                 }
2735
2736                 break;
2737         case IBMVFC_AE_LINK_UP:
2738                 vhost->events_to_log |= IBMVFC_AE_LINKUP;
2739                 vhost->delay_init = 1;
2740                 __ibmvfc_reset_host(vhost);
2741                 break;
2742         case IBMVFC_AE_SCN_FABRIC:
2743         case IBMVFC_AE_SCN_DOMAIN:
2744                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2745                 if (vhost->state < IBMVFC_HALTED) {
2746                         vhost->delay_init = 1;
2747                         __ibmvfc_reset_host(vhost);
2748                 }
2749                 break;
2750         case IBMVFC_AE_SCN_NPORT:
2751         case IBMVFC_AE_SCN_GROUP:
2752                 vhost->events_to_log |= IBMVFC_AE_RSCN;
2753                 ibmvfc_reinit_host(vhost);
2754                 break;
2755         case IBMVFC_AE_ELS_LOGO:
2756         case IBMVFC_AE_ELS_PRLO:
2757         case IBMVFC_AE_ELS_PLOGI:
2758                 list_for_each_entry(tgt, &vhost->targets, queue) {
2759                         if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
2760                                 break;
2761                         if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
2762                                 continue;
2763                         if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
2764                                 continue;
2765                         if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
2766                                 continue;
2767                         if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
2768                                 tgt->logo_rcvd = 1;
2769                         if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
2770                                 ibmvfc_del_tgt(tgt);
2771                                 ibmvfc_reinit_host(vhost);
2772                         }
2773                 }
2774                 break;
2775         case IBMVFC_AE_LINK_DOWN:
2776         case IBMVFC_AE_ADAPTER_FAILED:
2777                 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2778                 break;
2779         case IBMVFC_AE_LINK_DEAD:
2780                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2781                 break;
2782         case IBMVFC_AE_HALT:
2783                 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2784                 break;
2785         default:
2786                 dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
2787                 break;
2788         }
2789 }
2790
2791 /**
2792  * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2793  * @crq:        Command/Response queue
2794  * @vhost:      ibmvfc host struct
2795  *
2796  **/
2797 static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2798 {
2799         long rc;
2800         struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
2801
2802         switch (crq->valid) {
2803         case IBMVFC_CRQ_INIT_RSP:
2804                 switch (crq->format) {
2805                 case IBMVFC_CRQ_INIT:
2806                         dev_info(vhost->dev, "Partner initialized\n");
2807                         /* Send back a response */
2808                         rc = ibmvfc_send_crq_init_complete(vhost);
2809                         if (rc == 0)
2810                                 ibmvfc_init_host(vhost);
2811                         else
2812                                 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2813                         break;
2814                 case IBMVFC_CRQ_INIT_COMPLETE:
2815                         dev_info(vhost->dev, "Partner initialization complete\n");
2816                         ibmvfc_init_host(vhost);
2817                         break;
2818                 default:
2819                         dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2820                 }
2821                 return;
2822         case IBMVFC_CRQ_XPORT_EVENT:
2823                 vhost->state = IBMVFC_NO_CRQ;
2824                 vhost->logged_in = 0;
2825                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2826                 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2827                         /* We need to re-setup the interpartition connection */
2828                         dev_info(vhost->dev, "Partition migrated, Re-enabling adapter\n");
2829                         vhost->client_migrated = 1;
2830
2831                         scsi_block_requests(vhost->host);
2832                         ibmvfc_purge_requests(vhost, DID_REQUEUE);
2833                         ibmvfc_set_host_state(vhost, IBMVFC_LINK_DOWN);
2834                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
2835                         wake_up(&vhost->work_wait_q);
2836                 } else if (crq->format == IBMVFC_PARTNER_FAILED || crq->format == IBMVFC_PARTNER_DEREGISTER) {
2837                         dev_err(vhost->dev, "Host partner adapter deregistered or failed (rc=%d)\n", crq->format);
2838                         ibmvfc_purge_requests(vhost, DID_ERROR);
2839                         ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2840                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
2841                 } else {
2842                         dev_err(vhost->dev, "Received unknown transport event from partner (rc=%d)\n", crq->format);
2843                 }
2844                 return;
2845         case IBMVFC_CRQ_CMD_RSP:
2846                 break;
2847         default:
2848                 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2849                 return;
2850         }
2851
2852         if (crq->format == IBMVFC_ASYNC_EVENT)
2853                 return;
2854
2855         /* The only kind of payload CRQs we should get are responses to
2856          * things we send. Make sure this response is to something we
2857          * actually sent
2858          */
2859         if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2860                 dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
2861                         crq->ioba);
2862                 return;
2863         }
2864
2865         if (unlikely(atomic_read(&evt->free))) {
2866                 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
2867                         crq->ioba);
2868                 return;
2869         }
2870
2871         del_timer(&evt->timer);
2872         list_del(&evt->queue);
2873         ibmvfc_trc_end(evt);
2874         evt->done(evt);
2875 }
2876
2877 /**
2878  * ibmvfc_scan_finished - Check if the device scan is done.
2879  * @shost:      scsi host struct
2880  * @time:       current elapsed time
2881  *
2882  * Returns:
2883  *      0 if scan is not done / 1 if scan is done
2884  **/
2885 static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2886 {
2887         unsigned long flags;
2888         struct ibmvfc_host *vhost = shost_priv(shost);
2889         int done = 0;
2890
2891         spin_lock_irqsave(shost->host_lock, flags);
2892         if (time >= (init_timeout * HZ)) {
2893                 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2894                          "continuing initialization\n", init_timeout);
2895                 done = 1;
2896         }
2897
2898         if (vhost->scan_complete)
2899                 done = 1;
2900         spin_unlock_irqrestore(shost->host_lock, flags);
2901         return done;
2902 }
2903
2904 /**
2905  * ibmvfc_slave_alloc - Setup the device's task set value
2906  * @sdev:       struct scsi_device device to configure
2907  *
2908  * Set the device's task set value so that error handling works as
2909  * expected.
2910  *
2911  * Returns:
2912  *      0 on success / -ENXIO if device does not exist
2913  **/
2914 static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2915 {
2916         struct Scsi_Host *shost = sdev->host;
2917         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2918         struct ibmvfc_host *vhost = shost_priv(shost);
2919         unsigned long flags = 0;
2920
2921         if (!rport || fc_remote_port_chkready(rport))
2922                 return -ENXIO;
2923
2924         spin_lock_irqsave(shost->host_lock, flags);
2925         sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2926         spin_unlock_irqrestore(shost->host_lock, flags);
2927         return 0;
2928 }
2929
2930 /**
2931  * ibmvfc_target_alloc - Setup the target's task set value
2932  * @starget:    struct scsi_target
2933  *
2934  * Set the target's task set value so that error handling works as
2935  * expected.
2936  *
2937  * Returns:
2938  *      0 on success / -ENXIO if device does not exist
2939  **/
2940 static int ibmvfc_target_alloc(struct scsi_target *starget)
2941 {
2942         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2943         struct ibmvfc_host *vhost = shost_priv(shost);
2944         unsigned long flags = 0;
2945
2946         spin_lock_irqsave(shost->host_lock, flags);
2947         starget->hostdata = (void *)(unsigned long)vhost->task_set++;
2948         spin_unlock_irqrestore(shost->host_lock, flags);
2949         return 0;
2950 }
2951
2952 /**
2953  * ibmvfc_slave_configure - Configure the device
2954  * @sdev:       struct scsi_device device to configure
2955  *
2956  * Enable allow_restart for a device if it is a disk. Adjust the
2957  * queue_depth here also.
2958  *
2959  * Returns:
2960  *      0
2961  **/
2962 static int ibmvfc_slave_configure(struct scsi_device *sdev)
2963 {
2964         struct Scsi_Host *shost = sdev->host;
2965         unsigned long flags = 0;
2966
2967         spin_lock_irqsave(shost->host_lock, flags);
2968         if (sdev->type == TYPE_DISK) {
2969                 sdev->allow_restart = 1;
2970                 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
2971         }
2972         spin_unlock_irqrestore(shost->host_lock, flags);
2973         return 0;
2974 }
2975
2976 /**
2977  * ibmvfc_change_queue_depth - Change the device's queue depth
2978  * @sdev:       scsi device struct
2979  * @qdepth:     depth to set
2980  * @reason:     calling context
2981  *
2982  * Return value:
2983  *      actual depth set
2984  **/
2985 static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2986 {
2987         if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2988                 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2989
2990         return scsi_change_queue_depth(sdev, qdepth);
2991 }
2992
2993 static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2994                                                  struct device_attribute *attr, char *buf)
2995 {
2996         struct Scsi_Host *shost = class_to_shost(dev);
2997         struct ibmvfc_host *vhost = shost_priv(shost);
2998
2999         return snprintf(buf, PAGE_SIZE, "%s\n",
3000                         vhost->login_buf->resp.partition_name);
3001 }
3002
3003 static ssize_t ibmvfc_show_host_device_name(struct device *dev,
3004                                             struct device_attribute *attr, char *buf)
3005 {
3006         struct Scsi_Host *shost = class_to_shost(dev);
3007         struct ibmvfc_host *vhost = shost_priv(shost);
3008
3009         return snprintf(buf, PAGE_SIZE, "%s\n",
3010                         vhost->login_buf->resp.device_name);
3011 }
3012
3013 static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
3014                                          struct device_attribute *attr, char *buf)
3015 {
3016         struct Scsi_Host *shost = class_to_shost(dev);
3017         struct ibmvfc_host *vhost = shost_priv(shost);
3018
3019         return snprintf(buf, PAGE_SIZE, "%s\n",
3020                         vhost->login_buf->resp.port_loc_code);
3021 }
3022
3023 static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
3024                                          struct device_attribute *attr, char *buf)
3025 {
3026         struct Scsi_Host *shost = class_to_shost(dev);
3027         struct ibmvfc_host *vhost = shost_priv(shost);
3028
3029         return snprintf(buf, PAGE_SIZE, "%s\n",
3030                         vhost->login_buf->resp.drc_name);
3031 }
3032
3033 static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
3034                                              struct device_attribute *attr, char *buf)
3035 {
3036         struct Scsi_Host *shost = class_to_shost(dev);
3037         struct ibmvfc_host *vhost = shost_priv(shost);
3038         return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
3039 }
3040
3041 static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
3042                                              struct device_attribute *attr, char *buf)
3043 {
3044         struct Scsi_Host *shost = class_to_shost(dev);
3045         struct ibmvfc_host *vhost = shost_priv(shost);
3046         return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
3047 }
3048
3049 /**
3050  * ibmvfc_show_log_level - Show the adapter's error logging level
3051  * @dev:        class device struct
3052  * @buf:        buffer
3053  *
3054  * Return value:
3055  *      number of bytes printed to buffer
3056  **/
3057 static ssize_t ibmvfc_show_log_level(struct device *dev,
3058                                      struct device_attribute *attr, char *buf)
3059 {
3060         struct Scsi_Host *shost = class_to_shost(dev);
3061         struct ibmvfc_host *vhost = shost_priv(shost);
3062         unsigned long flags = 0;
3063         int len;
3064
3065         spin_lock_irqsave(shost->host_lock, flags);
3066         len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
3067         spin_unlock_irqrestore(shost->host_lock, flags);
3068         return len;
3069 }
3070
3071 /**
3072  * ibmvfc_store_log_level - Change the adapter's error logging level
3073  * @dev:        class device struct
3074  * @buf:        buffer
3075  *
3076  * Return value:
3077  *      number of bytes printed to buffer
3078  **/
3079 static ssize_t ibmvfc_store_log_level(struct device *dev,
3080                                       struct device_attribute *attr,
3081                                       const char *buf, size_t count)
3082 {
3083         struct Scsi_Host *shost = class_to_shost(dev);
3084         struct ibmvfc_host *vhost = shost_priv(shost);
3085         unsigned long flags = 0;
3086
3087         spin_lock_irqsave(shost->host_lock, flags);
3088         vhost->log_level = simple_strtoul(buf, NULL, 10);
3089         spin_unlock_irqrestore(shost->host_lock, flags);
3090         return strlen(buf);
3091 }
3092
3093 static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
3094 static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
3095 static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
3096 static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
3097 static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
3098 static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
3099 static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
3100                    ibmvfc_show_log_level, ibmvfc_store_log_level);
3101
3102 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3103 /**
3104  * ibmvfc_read_trace - Dump the adapter trace
3105  * @filp:               open sysfs file
3106  * @kobj:               kobject struct
3107  * @bin_attr:   bin_attribute struct
3108  * @buf:                buffer
3109  * @off:                offset
3110  * @count:              buffer size
3111  *
3112  * Return value:
3113  *      number of bytes printed to buffer
3114  **/
3115 static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
3116                                  struct bin_attribute *bin_attr,
3117                                  char *buf, loff_t off, size_t count)
3118 {
3119         struct device *dev = container_of(kobj, struct device, kobj);
3120         struct Scsi_Host *shost = class_to_shost(dev);
3121         struct ibmvfc_host *vhost = shost_priv(shost);
3122         unsigned long flags = 0;
3123         int size = IBMVFC_TRACE_SIZE;
3124         char *src = (char *)vhost->trace;
3125
3126         if (off > size)
3127                 return 0;
3128         if (off + count > size) {
3129                 size -= off;
3130                 count = size;
3131         }
3132
3133         spin_lock_irqsave(shost->host_lock, flags);
3134         memcpy(buf, &src[off], count);
3135         spin_unlock_irqrestore(shost->host_lock, flags);
3136         return count;
3137 }
3138
3139 static struct bin_attribute ibmvfc_trace_attr = {
3140         .attr = {
3141                 .name = "trace",
3142                 .mode = S_IRUGO,
3143         },
3144         .size = 0,
3145         .read = ibmvfc_read_trace,
3146 };
3147 #endif
3148
3149 static struct device_attribute *ibmvfc_attrs[] = {
3150         &dev_attr_partition_name,
3151         &dev_attr_device_name,
3152         &dev_attr_port_loc_code,
3153         &dev_attr_drc_name,
3154         &dev_attr_npiv_version,
3155         &dev_attr_capabilities,
3156         &dev_attr_log_level,
3157         NULL
3158 };
3159
3160 static struct scsi_host_template driver_template = {
3161         .module = THIS_MODULE,
3162         .name = "IBM POWER Virtual FC Adapter",
3163         .proc_name = IBMVFC_NAME,
3164         .queuecommand = ibmvfc_queuecommand,
3165         .eh_timed_out = fc_eh_timed_out,
3166         .eh_abort_handler = ibmvfc_eh_abort_handler,
3167         .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
3168         .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
3169         .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
3170         .slave_alloc = ibmvfc_slave_alloc,
3171         .slave_configure = ibmvfc_slave_configure,
3172         .target_alloc = ibmvfc_target_alloc,
3173         .scan_finished = ibmvfc_scan_finished,
3174         .change_queue_depth = ibmvfc_change_queue_depth,
3175         .cmd_per_lun = 16,
3176         .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
3177         .this_id = -1,
3178         .sg_tablesize = SG_ALL,
3179         .max_sectors = IBMVFC_MAX_SECTORS,
3180         .shost_attrs = ibmvfc_attrs,
3181         .track_queue_depth = 1,
3182 };
3183
3184 /**
3185  * ibmvfc_next_async_crq - Returns the next entry in async queue
3186  * @vhost:      ibmvfc host struct
3187  *
3188  * Returns:
3189  *      Pointer to next entry in queue / NULL if empty
3190  **/
3191 static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
3192 {
3193         struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
3194         struct ibmvfc_async_crq *crq;
3195
3196         crq = &async_crq->msgs[async_crq->cur];
3197         if (crq->valid & 0x80) {
3198                 if (++async_crq->cur == async_crq->size)
3199                         async_crq->cur = 0;
3200                 rmb();
3201         } else
3202                 crq = NULL;
3203
3204         return crq;
3205 }
3206
3207 /**
3208  * ibmvfc_next_crq - Returns the next entry in message queue
3209  * @vhost:      ibmvfc host struct
3210  *
3211  * Returns:
3212  *      Pointer to next entry in queue / NULL if empty
3213  **/
3214 static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
3215 {
3216         struct ibmvfc_crq_queue *queue = &vhost->crq;
3217         struct ibmvfc_crq *crq;
3218
3219         crq = &queue->msgs[queue->cur];
3220         if (crq->valid & 0x80) {
3221                 if (++queue->cur == queue->size)
3222                         queue->cur = 0;
3223                 rmb();
3224         } else
3225                 crq = NULL;
3226
3227         return crq;
3228 }
3229
3230 /**
3231  * ibmvfc_interrupt - Interrupt handler
3232  * @irq:                number of irq to handle, not used
3233  * @dev_instance: ibmvfc_host that received interrupt
3234  *
3235  * Returns:
3236  *      IRQ_HANDLED
3237  **/
3238 static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
3239 {
3240         struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
3241         unsigned long flags;
3242
3243         spin_lock_irqsave(vhost->host->host_lock, flags);
3244         vio_disable_interrupts(to_vio_dev(vhost->dev));
3245         tasklet_schedule(&vhost->tasklet);
3246         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3247         return IRQ_HANDLED;
3248 }
3249
3250 /**
3251  * ibmvfc_tasklet - Interrupt handler tasklet
3252  * @data:               ibmvfc host struct
3253  *
3254  * Returns:
3255  *      Nothing
3256  **/
3257 static void ibmvfc_tasklet(void *data)
3258 {
3259         struct ibmvfc_host *vhost = data;
3260         struct vio_dev *vdev = to_vio_dev(vhost->dev);
3261         struct ibmvfc_crq *crq;
3262         struct ibmvfc_async_crq *async;
3263         unsigned long flags;
3264         int done = 0;
3265
3266         spin_lock_irqsave(vhost->host->host_lock, flags);
3267         while (!done) {
3268                 /* Pull all the valid messages off the async CRQ */
3269                 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3270                         ibmvfc_handle_async(async, vhost);
3271                         async->valid = 0;
3272                         wmb();
3273                 }
3274
3275                 /* Pull all the valid messages off the CRQ */
3276                 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3277                         ibmvfc_handle_crq(crq, vhost);
3278                         crq->valid = 0;
3279                         wmb();
3280                 }
3281
3282                 vio_enable_interrupts(vdev);
3283                 if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
3284                         vio_disable_interrupts(vdev);
3285                         ibmvfc_handle_async(async, vhost);
3286                         async->valid = 0;
3287                         wmb();
3288                 } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
3289                         vio_disable_interrupts(vdev);
3290                         ibmvfc_handle_crq(crq, vhost);
3291                         crq->valid = 0;
3292                         wmb();
3293                 } else
3294                         done = 1;
3295         }
3296
3297         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3298 }
3299
3300 /**
3301  * ibmvfc_init_tgt - Set the next init job step for the target
3302  * @tgt:                ibmvfc target struct
3303  * @job_step:   job step to perform
3304  *
3305  **/
3306 static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
3307                             void (*job_step) (struct ibmvfc_target *))
3308 {
3309         if (!ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT))
3310                 tgt->job_step = job_step;
3311         wake_up(&tgt->vhost->work_wait_q);
3312 }
3313
3314 /**
3315  * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3316  * @tgt:                ibmvfc target struct
3317  * @job_step:   initialization job step
3318  *
3319  * Returns: 1 if step will be retried / 0 if not
3320  *
3321  **/
3322 static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
3323                                   void (*job_step) (struct ibmvfc_target *))
3324 {
3325         if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
3326                 ibmvfc_del_tgt(tgt);
3327                 wake_up(&tgt->vhost->work_wait_q);
3328                 return 0;
3329         } else
3330                 ibmvfc_init_tgt(tgt, job_step);
3331         return 1;
3332 }
3333
3334 /* Defined in FC-LS */
3335 static const struct {
3336         int code;
3337         int retry;
3338         int logged_in;
3339 } prli_rsp [] = {
3340         { 0, 1, 0 },
3341         { 1, 0, 1 },
3342         { 2, 1, 0 },
3343         { 3, 1, 0 },
3344         { 4, 0, 0 },
3345         { 5, 0, 0 },
3346         { 6, 0, 1 },
3347         { 7, 0, 0 },
3348         { 8, 1, 0 },
3349 };
3350
3351 /**
3352  * ibmvfc_get_prli_rsp - Find PRLI response index
3353  * @flags:      PRLI response flags
3354  *
3355  **/
3356 static int ibmvfc_get_prli_rsp(u16 flags)
3357 {
3358         int i;
3359         int code = (flags & 0x0f00) >> 8;
3360
3361         for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
3362                 if (prli_rsp[i].code == code)
3363                         return i;
3364
3365         return 0;
3366 }
3367
3368 /**
3369  * ibmvfc_tgt_prli_done - Completion handler for Process Login
3370  * @evt:        ibmvfc event struct
3371  *
3372  **/
3373 static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
3374 {
3375         struct ibmvfc_target *tgt = evt->tgt;
3376         struct ibmvfc_host *vhost = evt->vhost;
3377         struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
3378         struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
3379         u32 status = be16_to_cpu(rsp->common.status);
3380         int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
3381
3382         vhost->discovery_threads--;
3383         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3384         switch (status) {
3385         case IBMVFC_MAD_SUCCESS:
3386                 tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
3387                         parms->type, parms->flags, parms->service_parms);
3388
3389                 if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
3390                         index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
3391                         if (prli_rsp[index].logged_in) {
3392                                 if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
3393                                         tgt->need_login = 0;
3394                                         tgt->ids.roles = 0;
3395                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
3396                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
3397                                         if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
3398                                                 tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
3399                                         tgt->add_rport = 1;
3400                                 } else
3401                                         ibmvfc_del_tgt(tgt);
3402                         } else if (prli_rsp[index].retry)
3403                                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3404                         else
3405                                 ibmvfc_del_tgt(tgt);
3406                 } else
3407                         ibmvfc_del_tgt(tgt);
3408                 break;
3409         case IBMVFC_MAD_DRIVER_FAILED:
3410                 break;
3411         case IBMVFC_MAD_CRQ_ERROR:
3412                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3413                 break;
3414         case IBMVFC_MAD_FAILED:
3415         default:
3416                 if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
3417                      be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
3418                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3419                 else if (tgt->logo_rcvd)
3420                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3421                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3422                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
3423                 else
3424                         ibmvfc_del_tgt(tgt);
3425
3426                 tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3427                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3428                         be16_to_cpu(rsp->status), be16_to_cpu(rsp->error), status);
3429                 break;
3430         }
3431
3432         kref_put(&tgt->kref, ibmvfc_release_tgt);
3433         ibmvfc_free_event(evt);
3434         wake_up(&vhost->work_wait_q);
3435 }
3436
3437 /**
3438  * ibmvfc_tgt_send_prli - Send a process login
3439  * @tgt:        ibmvfc target struct
3440  *
3441  **/
3442 static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
3443 {
3444         struct ibmvfc_process_login *prli;
3445         struct ibmvfc_host *vhost = tgt->vhost;
3446         struct ibmvfc_event *evt;
3447
3448         if (vhost->discovery_threads >= disc_threads)
3449                 return;
3450
3451         kref_get(&tgt->kref);
3452         evt = ibmvfc_get_event(vhost);
3453         vhost->discovery_threads++;
3454         ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
3455         evt->tgt = tgt;
3456         prli = &evt->iu.prli;
3457         memset(prli, 0, sizeof(*prli));
3458         prli->common.version = cpu_to_be32(1);
3459         prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
3460         prli->common.length = cpu_to_be16(sizeof(*prli));
3461         prli->scsi_id = cpu_to_be64(tgt->scsi_id);
3462
3463         prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
3464         prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
3465         prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
3466         prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED);
3467
3468         if (cls3_error)
3469                 prli->parms.service_parms |= cpu_to_be32(IBMVFC_PRLI_RETRY);
3470
3471         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3472         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3473                 vhost->discovery_threads--;
3474                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3475                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3476         } else
3477                 tgt_dbg(tgt, "Sent process login\n");
3478 }
3479
3480 /**
3481  * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3482  * @evt:        ibmvfc event struct
3483  *
3484  **/
3485 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
3486 {
3487         struct ibmvfc_target *tgt = evt->tgt;
3488         struct ibmvfc_host *vhost = evt->vhost;
3489         struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
3490         u32 status = be16_to_cpu(rsp->common.status);
3491         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3492
3493         vhost->discovery_threads--;
3494         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3495         switch (status) {
3496         case IBMVFC_MAD_SUCCESS:
3497                 tgt_dbg(tgt, "Port Login succeeded\n");
3498                 if (tgt->ids.port_name &&
3499                     tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
3500                         vhost->reinit = 1;
3501                         tgt_dbg(tgt, "Port re-init required\n");
3502                         break;
3503                 }
3504                 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3505                 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3506                 tgt->ids.port_id = tgt->scsi_id;
3507                 memcpy(&tgt->service_parms, &rsp->service_parms,
3508                        sizeof(tgt->service_parms));
3509                 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3510                        sizeof(tgt->service_parms_change));
3511                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3512                 break;
3513         case IBMVFC_MAD_DRIVER_FAILED:
3514                 break;
3515         case IBMVFC_MAD_CRQ_ERROR:
3516                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3517                 break;
3518         case IBMVFC_MAD_FAILED:
3519         default:
3520                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
3521                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
3522                 else
3523                         ibmvfc_del_tgt(tgt);
3524
3525                 tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3526                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
3527                                              be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
3528                         ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
3529                         ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain), status);
3530                 break;
3531         }
3532
3533         kref_put(&tgt->kref, ibmvfc_release_tgt);
3534         ibmvfc_free_event(evt);
3535         wake_up(&vhost->work_wait_q);
3536 }
3537
3538 /**
3539  * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3540  * @tgt:        ibmvfc target struct
3541  *
3542  **/
3543 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
3544 {
3545         struct ibmvfc_port_login *plogi;
3546         struct ibmvfc_host *vhost = tgt->vhost;
3547         struct ibmvfc_event *evt;
3548
3549         if (vhost->discovery_threads >= disc_threads)
3550                 return;
3551
3552         kref_get(&tgt->kref);
3553         tgt->logo_rcvd = 0;
3554         evt = ibmvfc_get_event(vhost);
3555         vhost->discovery_threads++;
3556         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3557         ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
3558         evt->tgt = tgt;
3559         plogi = &evt->iu.plogi;
3560         memset(plogi, 0, sizeof(*plogi));
3561         plogi->common.version = cpu_to_be32(1);
3562         plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
3563         plogi->common.length = cpu_to_be16(sizeof(*plogi));
3564         plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
3565
3566         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3567                 vhost->discovery_threads--;
3568                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3569                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3570         } else
3571                 tgt_dbg(tgt, "Sent port login\n");
3572 }
3573
3574 /**
3575  * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3576  * @evt:        ibmvfc event struct
3577  *
3578  **/
3579 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
3580 {
3581         struct ibmvfc_target *tgt = evt->tgt;
3582         struct ibmvfc_host *vhost = evt->vhost;
3583         struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
3584         u32 status = be16_to_cpu(rsp->common.status);
3585
3586         vhost->discovery_threads--;
3587         ibmvfc_free_event(evt);
3588         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3589
3590         switch (status) {
3591         case IBMVFC_MAD_SUCCESS:
3592                 tgt_dbg(tgt, "Implicit Logout succeeded\n");
3593                 break;
3594         case IBMVFC_MAD_DRIVER_FAILED:
3595                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3596                 wake_up(&vhost->work_wait_q);
3597                 return;
3598         case IBMVFC_MAD_FAILED:
3599         default:
3600                 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
3601                 break;
3602         }
3603
3604         ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
3605         kref_put(&tgt->kref, ibmvfc_release_tgt);
3606         wake_up(&vhost->work_wait_q);
3607 }
3608
3609 /**
3610  * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
3611  * @tgt:                ibmvfc target struct
3612  *
3613  * Returns:
3614  *      Allocated and initialized ibmvfc_event struct
3615  **/
3616 static struct ibmvfc_event *__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target *tgt,
3617                                                                  void (*done) (struct ibmvfc_event *))
3618 {
3619         struct ibmvfc_implicit_logout *mad;
3620         struct ibmvfc_host *vhost = tgt->vhost;
3621         struct ibmvfc_event *evt;
3622
3623         kref_get(&tgt->kref);
3624         evt = ibmvfc_get_event(vhost);
3625         ibmvfc_init_event(evt, done, IBMVFC_MAD_FORMAT);
3626         evt->tgt = tgt;
3627         mad = &evt->iu.implicit_logout;
3628         memset(mad, 0, sizeof(*mad));
3629         mad->common.version = cpu_to_be32(1);
3630         mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
3631         mad->common.length = cpu_to_be16(sizeof(*mad));
3632         mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
3633         return evt;
3634 }
3635
3636 /**
3637  * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3638  * @tgt:                ibmvfc target struct
3639  *
3640  **/
3641 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
3642 {
3643         struct ibmvfc_host *vhost = tgt->vhost;
3644         struct ibmvfc_event *evt;
3645
3646         if (vhost->discovery_threads >= disc_threads)
3647                 return;
3648
3649         vhost->discovery_threads++;
3650         evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3651                                                    ibmvfc_tgt_implicit_logout_done);
3652
3653         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3654         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3655                 vhost->discovery_threads--;
3656                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3657                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3658         } else
3659                 tgt_dbg(tgt, "Sent Implicit Logout\n");
3660 }
3661
3662 /**
3663  * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
3664  * @evt:        ibmvfc event struct
3665  *
3666  **/
3667 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event *evt)
3668 {
3669         struct ibmvfc_target *tgt = evt->tgt;
3670         struct ibmvfc_host *vhost = evt->vhost;
3671         struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3672         u32 status = be16_to_cpu(mad->common.status);
3673
3674         vhost->discovery_threads--;
3675         ibmvfc_free_event(evt);
3676
3677         /*
3678          * If our state is IBMVFC_HOST_OFFLINE, we could be unloading the
3679          * driver in which case we need to free up all the targets. If we are
3680          * not unloading, we will still go through a hard reset to get out of
3681          * offline state, so there is no need to track the old targets in that
3682          * case.
3683          */
3684         if (status == IBMVFC_MAD_SUCCESS || vhost->state == IBMVFC_HOST_OFFLINE)
3685                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3686         else
3687                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT);
3688
3689         tgt_dbg(tgt, "Implicit Logout %s\n", (status == IBMVFC_MAD_SUCCESS) ? "succeeded" : "failed");
3690         kref_put(&tgt->kref, ibmvfc_release_tgt);
3691         wake_up(&vhost->work_wait_q);
3692 }
3693
3694 /**
3695  * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
3696  * @tgt:                ibmvfc target struct
3697  *
3698  **/
3699 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target *tgt)
3700 {
3701         struct ibmvfc_host *vhost = tgt->vhost;
3702         struct ibmvfc_event *evt;
3703
3704         if (!vhost->logged_in) {
3705                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3706                 return;
3707         }
3708
3709         if (vhost->discovery_threads >= disc_threads)
3710                 return;
3711
3712         vhost->discovery_threads++;
3713         evt = __ibmvfc_tgt_get_implicit_logout_evt(tgt,
3714                                                    ibmvfc_tgt_implicit_logout_and_del_done);
3715
3716         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT);
3717         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3718                 vhost->discovery_threads--;
3719                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3720                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3721         } else
3722                 tgt_dbg(tgt, "Sent Implicit Logout\n");
3723 }
3724
3725 /**
3726  * ibmvfc_tgt_move_login_done - Completion handler for Move Login
3727  * @evt:        ibmvfc event struct
3728  *
3729  **/
3730 static void ibmvfc_tgt_move_login_done(struct ibmvfc_event *evt)
3731 {
3732         struct ibmvfc_target *tgt = evt->tgt;
3733         struct ibmvfc_host *vhost = evt->vhost;
3734         struct ibmvfc_move_login *rsp = &evt->xfer_iu->move_login;
3735         u32 status = be16_to_cpu(rsp->common.status);
3736         int level = IBMVFC_DEFAULT_LOG_LEVEL;
3737
3738         vhost->discovery_threads--;
3739         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3740         switch (status) {
3741         case IBMVFC_MAD_SUCCESS:
3742                 tgt_dbg(tgt, "Move Login succeeded for old scsi_id: %llX\n", tgt->old_scsi_id);
3743                 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
3744                 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
3745                 tgt->ids.port_id = tgt->scsi_id;
3746                 memcpy(&tgt->service_parms, &rsp->service_parms,
3747                        sizeof(tgt->service_parms));
3748                 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
3749                        sizeof(tgt->service_parms_change));
3750                 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
3751                 break;
3752         case IBMVFC_MAD_DRIVER_FAILED:
3753                 break;
3754         case IBMVFC_MAD_CRQ_ERROR:
3755                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3756                 break;
3757         case IBMVFC_MAD_FAILED:
3758         default:
3759                 level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_move_login);
3760
3761                 tgt_log(tgt, level,
3762                         "Move Login failed: old scsi_id: %llX, flags:%x, vios_flags:%x, rc=0x%02X\n",
3763                         tgt->old_scsi_id, be32_to_cpu(rsp->flags), be16_to_cpu(rsp->vios_flags),
3764                         status);
3765                 break;
3766         }
3767
3768         kref_put(&tgt->kref, ibmvfc_release_tgt);
3769         ibmvfc_free_event(evt);
3770         wake_up(&vhost->work_wait_q);
3771 }
3772
3773
3774 /**
3775  * ibmvfc_tgt_move_login - Initiate a move login for specified target
3776  * @tgt:                ibmvfc target struct
3777  *
3778  **/
3779 static void ibmvfc_tgt_move_login(struct ibmvfc_target *tgt)
3780 {
3781         struct ibmvfc_host *vhost = tgt->vhost;
3782         struct ibmvfc_move_login *move;
3783         struct ibmvfc_event *evt;
3784
3785         if (vhost->discovery_threads >= disc_threads)
3786                 return;
3787
3788         kref_get(&tgt->kref);
3789         evt = ibmvfc_get_event(vhost);
3790         vhost->discovery_threads++;
3791         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3792         ibmvfc_init_event(evt, ibmvfc_tgt_move_login_done, IBMVFC_MAD_FORMAT);
3793         evt->tgt = tgt;
3794         move = &evt->iu.move_login;
3795         memset(move, 0, sizeof(*move));
3796         move->common.version = cpu_to_be32(1);
3797         move->common.opcode = cpu_to_be32(IBMVFC_MOVE_LOGIN);
3798         move->common.length = cpu_to_be16(sizeof(*move));
3799
3800         move->old_scsi_id = cpu_to_be64(tgt->old_scsi_id);
3801         move->new_scsi_id = cpu_to_be64(tgt->scsi_id);
3802         move->wwpn = cpu_to_be64(tgt->wwpn);
3803         move->node_name = cpu_to_be64(tgt->ids.node_name);
3804
3805         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3806                 vhost->discovery_threads--;
3807                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3808                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3809         } else
3810                 tgt_dbg(tgt, "Sent Move Login for old scsi_id: %llX\n", tgt->old_scsi_id);
3811 }
3812
3813 /**
3814  * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3815  * @mad:        ibmvfc passthru mad struct
3816  * @tgt:        ibmvfc target struct
3817  *
3818  * Returns:
3819  *      1 if PLOGI needed / 0 if PLOGI not needed
3820  **/
3821 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
3822                                     struct ibmvfc_target *tgt)
3823 {
3824         if (wwn_to_u64((u8 *)&mad->fc_iu.response[2]) != tgt->ids.port_name)
3825                 return 1;
3826         if (wwn_to_u64((u8 *)&mad->fc_iu.response[4]) != tgt->ids.node_name)
3827                 return 1;
3828         if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
3829                 return 1;
3830         return 0;
3831 }
3832
3833 /**
3834  * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3835  * @evt:        ibmvfc event struct
3836  *
3837  **/
3838 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
3839 {
3840         struct ibmvfc_target *tgt = evt->tgt;
3841         struct ibmvfc_host *vhost = evt->vhost;
3842         struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
3843         u32 status = be16_to_cpu(mad->common.status);
3844         u8 fc_reason, fc_explain;
3845
3846         vhost->discovery_threads--;
3847         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3848         del_timer(&tgt->timer);
3849
3850         switch (status) {
3851         case IBMVFC_MAD_SUCCESS:
3852                 tgt_dbg(tgt, "ADISC succeeded\n");
3853                 if (ibmvfc_adisc_needs_plogi(mad, tgt))
3854                         ibmvfc_del_tgt(tgt);
3855                 break;
3856         case IBMVFC_MAD_DRIVER_FAILED:
3857                 break;
3858         case IBMVFC_MAD_FAILED:
3859         default:
3860                 ibmvfc_del_tgt(tgt);
3861                 fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
3862                 fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
3863                 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3864                          ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
3865                          be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error),
3866                          ibmvfc_get_fc_type(fc_reason), fc_reason,
3867                          ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
3868                 break;
3869         }
3870
3871         kref_put(&tgt->kref, ibmvfc_release_tgt);
3872         ibmvfc_free_event(evt);
3873         wake_up(&vhost->work_wait_q);
3874 }
3875
3876 /**
3877  * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3878  * @evt:                ibmvfc event struct
3879  *
3880  **/
3881 static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
3882 {
3883         struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
3884
3885         memset(mad, 0, sizeof(*mad));
3886         mad->common.version = cpu_to_be32(1);
3887         mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
3888         mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
3889         mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3890                 offsetof(struct ibmvfc_passthru_mad, iu));
3891         mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
3892         mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3893         mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
3894         mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3895                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3896                 offsetof(struct ibmvfc_passthru_fc_iu, payload));
3897         mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
3898         mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
3899                 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3900                 offsetof(struct ibmvfc_passthru_fc_iu, response));
3901         mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
3902 }
3903
3904 /**
3905  * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3906  * @evt:                ibmvfc event struct
3907  *
3908  * Just cleanup this event struct. Everything else is handled by
3909  * the ADISC completion handler. If the ADISC never actually comes
3910  * back, we still have the timer running on the ADISC event struct
3911  * which will fire and cause the CRQ to get reset.
3912  *
3913  **/
3914 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
3915 {
3916         struct ibmvfc_host *vhost = evt->vhost;
3917         struct ibmvfc_target *tgt = evt->tgt;
3918
3919         tgt_dbg(tgt, "ADISC cancel complete\n");
3920         vhost->abort_threads--;
3921         ibmvfc_free_event(evt);
3922         kref_put(&tgt->kref, ibmvfc_release_tgt);
3923         wake_up(&vhost->work_wait_q);
3924 }
3925
3926 /**
3927  * ibmvfc_adisc_timeout - Handle an ADISC timeout
3928  * @tgt:                ibmvfc target struct
3929  *
3930  * If an ADISC times out, send a cancel. If the cancel times
3931  * out, reset the CRQ. When the ADISC comes back as cancelled,
3932  * log back into the target.
3933  **/
3934 static void ibmvfc_adisc_timeout(struct timer_list *t)
3935 {
3936         struct ibmvfc_target *tgt = from_timer(tgt, t, timer);
3937         struct ibmvfc_host *vhost = tgt->vhost;
3938         struct ibmvfc_event *evt;
3939         struct ibmvfc_tmf *tmf;
3940         unsigned long flags;
3941         int rc;
3942
3943         tgt_dbg(tgt, "ADISC timeout\n");
3944         spin_lock_irqsave(vhost->host->host_lock, flags);
3945         if (vhost->abort_threads >= disc_threads ||
3946             tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
3947             vhost->state != IBMVFC_INITIALIZING ||
3948             vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
3949                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3950                 return;
3951         }
3952
3953         vhost->abort_threads++;
3954         kref_get(&tgt->kref);
3955         evt = ibmvfc_get_event(vhost);
3956         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
3957
3958         evt->tgt = tgt;
3959         tmf = &evt->iu.tmf;
3960         memset(tmf, 0, sizeof(*tmf));
3961         tmf->common.version = cpu_to_be32(1);
3962         tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
3963         tmf->common.length = cpu_to_be16(sizeof(*tmf));
3964         tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
3965         tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
3966
3967         rc = ibmvfc_send_event(evt, vhost, default_timeout);
3968
3969         if (rc) {
3970                 tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
3971                 vhost->abort_threads--;
3972                 kref_put(&tgt->kref, ibmvfc_release_tgt);
3973                 __ibmvfc_reset_host(vhost);
3974         } else
3975                 tgt_dbg(tgt, "Attempting to cancel ADISC\n");
3976         spin_unlock_irqrestore(vhost->host->host_lock, flags);
3977 }
3978
3979 /**
3980  * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3981  * @tgt:                ibmvfc target struct
3982  *
3983  * When sending an ADISC we end up with two timers running. The
3984  * first timer is the timer in the ibmvfc target struct. If this
3985  * fires, we send a cancel to the target. The second timer is the
3986  * timer on the ibmvfc event for the ADISC, which is longer. If that
3987  * fires, it means the ADISC timed out and our attempt to cancel it
3988  * also failed, so we need to reset the CRQ.
3989  **/
3990 static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3991 {
3992         struct ibmvfc_passthru_mad *mad;
3993         struct ibmvfc_host *vhost = tgt->vhost;
3994         struct ibmvfc_event *evt;
3995
3996         if (vhost->discovery_threads >= disc_threads)
3997                 return;
3998
3999         kref_get(&tgt->kref);
4000         evt = ibmvfc_get_event(vhost);
4001         vhost->discovery_threads++;
4002         ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
4003         evt->tgt = tgt;
4004
4005         ibmvfc_init_passthru(evt);
4006         mad = &evt->iu.passthru;
4007         mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
4008         mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
4009         mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
4010
4011         mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
4012         memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
4013                sizeof(vhost->login_buf->resp.port_name));
4014         memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
4015                sizeof(vhost->login_buf->resp.node_name));
4016         mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
4017
4018         if (timer_pending(&tgt->timer))
4019                 mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
4020         else {
4021                 tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
4022                 add_timer(&tgt->timer);
4023         }
4024
4025         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4026         if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
4027                 vhost->discovery_threads--;
4028                 del_timer(&tgt->timer);
4029                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4030                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4031         } else
4032                 tgt_dbg(tgt, "Sent ADISC\n");
4033 }
4034
4035 /**
4036  * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
4037  * @evt:        ibmvfc event struct
4038  *
4039  **/
4040 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
4041 {
4042         struct ibmvfc_target *tgt = evt->tgt;
4043         struct ibmvfc_host *vhost = evt->vhost;
4044         struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
4045         u32 status = be16_to_cpu(rsp->common.status);
4046         int level = IBMVFC_DEFAULT_LOG_LEVEL;
4047
4048         vhost->discovery_threads--;
4049         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4050         switch (status) {
4051         case IBMVFC_MAD_SUCCESS:
4052                 tgt_dbg(tgt, "Query Target succeeded\n");
4053                 if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
4054                         ibmvfc_del_tgt(tgt);
4055                 else
4056                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
4057                 break;
4058         case IBMVFC_MAD_DRIVER_FAILED:
4059                 break;
4060         case IBMVFC_MAD_CRQ_ERROR:
4061                 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4062                 break;
4063         case IBMVFC_MAD_FAILED:
4064         default:
4065                 if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
4066                     be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
4067                     be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
4068                         ibmvfc_del_tgt(tgt);
4069                 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4070                         level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
4071                 else
4072                         ibmvfc_del_tgt(tgt);
4073
4074                 tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
4075                         ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4076                         be16_to_cpu(rsp->status), be16_to_cpu(rsp->error),
4077                         ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), be16_to_cpu(rsp->fc_type),
4078                         ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)), be16_to_cpu(rsp->fc_explain),
4079                         status);
4080                 break;
4081         }
4082
4083         kref_put(&tgt->kref, ibmvfc_release_tgt);
4084         ibmvfc_free_event(evt);
4085         wake_up(&vhost->work_wait_q);
4086 }
4087
4088 /**
4089  * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
4090  * @tgt:        ibmvfc target struct
4091  *
4092  **/
4093 static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
4094 {
4095         struct ibmvfc_query_tgt *query_tgt;
4096         struct ibmvfc_host *vhost = tgt->vhost;
4097         struct ibmvfc_event *evt;
4098
4099         if (vhost->discovery_threads >= disc_threads)
4100                 return;
4101
4102         kref_get(&tgt->kref);
4103         evt = ibmvfc_get_event(vhost);
4104         vhost->discovery_threads++;
4105         evt->tgt = tgt;
4106         ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
4107         query_tgt = &evt->iu.query_tgt;
4108         memset(query_tgt, 0, sizeof(*query_tgt));
4109         query_tgt->common.version = cpu_to_be32(1);
4110         query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
4111         query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
4112         query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
4113
4114         ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
4115         if (ibmvfc_send_event(evt, vhost, default_timeout)) {
4116                 vhost->discovery_threads--;
4117                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
4118                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4119         } else
4120                 tgt_dbg(tgt, "Sent Query Target\n");
4121 }
4122
4123 /**
4124  * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
4125  * @vhost:              ibmvfc host struct
4126  * @scsi_id:    SCSI ID to allocate target for
4127  *
4128  * Returns:
4129  *      0 on success / other on failure
4130  **/
4131 static int ibmvfc_alloc_target(struct ibmvfc_host *vhost,
4132                                struct ibmvfc_discover_targets_entry *target)
4133 {
4134         struct ibmvfc_target *stgt = NULL;
4135         struct ibmvfc_target *wtgt = NULL;
4136         struct ibmvfc_target *tgt;
4137         unsigned long flags;
4138         u64 scsi_id = be32_to_cpu(target->scsi_id) & IBMVFC_DISC_TGT_SCSI_ID_MASK;
4139         u64 wwpn = be64_to_cpu(target->wwpn);
4140
4141         /* Look to see if we already have a target allocated for this SCSI ID or WWPN */
4142         spin_lock_irqsave(vhost->host->host_lock, flags);
4143         list_for_each_entry(tgt, &vhost->targets, queue) {
4144                 if (tgt->wwpn == wwpn) {
4145                         wtgt = tgt;
4146                         break;
4147                 }
4148         }
4149
4150         list_for_each_entry(tgt, &vhost->targets, queue) {
4151                 if (tgt->scsi_id == scsi_id) {
4152                         stgt = tgt;
4153                         break;
4154                 }
4155         }
4156
4157         if (wtgt && !stgt) {
4158                 /*
4159                  * A WWPN target has moved and we still are tracking the old
4160                  * SCSI ID.  The only way we should be able to get here is if
4161                  * we attempted to send an implicit logout for the old SCSI ID
4162                  * and it failed for some reason, such as there being I/O
4163                  * pending to the target. In this case, we will have already
4164                  * deleted the rport from the FC transport so we do a move
4165                  * login, which works even with I/O pending, as it will cancel
4166                  * any active commands.
4167                  */
4168                 if (wtgt->action == IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT) {
4169                         /*
4170                          * Do a move login here. The old target is no longer
4171                          * known to the transport layer We don't use the
4172                          * normal ibmvfc_set_tgt_action to set this, as we
4173                          * don't normally want to allow this state change.
4174                          */
4175                         wtgt->old_scsi_id = wtgt->scsi_id;
4176                         wtgt->scsi_id = scsi_id;
4177                         wtgt->action = IBMVFC_TGT_ACTION_INIT;
4178                         ibmvfc_init_tgt(wtgt, ibmvfc_tgt_move_login);
4179                         goto unlock_out;
4180                 } else {
4181                         tgt_err(wtgt, "Unexpected target state: %d, %p\n",
4182                                 wtgt->action, wtgt->rport);
4183                 }
4184         } else if (stgt) {
4185                 if (tgt->need_login)
4186                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4187                 goto unlock_out;
4188         }
4189         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4190
4191         tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
4192         memset(tgt, 0, sizeof(*tgt));
4193         tgt->scsi_id = scsi_id;
4194         tgt->wwpn = wwpn;
4195         tgt->vhost = vhost;
4196         tgt->need_login = 1;
4197         timer_setup(&tgt->timer, ibmvfc_adisc_timeout, 0);
4198         kref_init(&tgt->kref);
4199         ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
4200         spin_lock_irqsave(vhost->host->host_lock, flags);
4201         tgt->cancel_key = vhost->task_set++;
4202         list_add_tail(&tgt->queue, &vhost->targets);
4203
4204 unlock_out:
4205         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4206         return 0;
4207 }
4208
4209 /**
4210  * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4211  * @vhost:              ibmvfc host struct
4212  *
4213  * Returns:
4214  *      0 on success / other on failure
4215  **/
4216 static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
4217 {
4218         int i, rc;
4219
4220         for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
4221                 rc = ibmvfc_alloc_target(vhost, &vhost->disc_buf[i]);
4222
4223         return rc;
4224 }
4225
4226 /**
4227  * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4228  * @evt:        ibmvfc event struct
4229  *
4230  **/
4231 static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
4232 {
4233         struct ibmvfc_host *vhost = evt->vhost;
4234         struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
4235         u32 mad_status = be16_to_cpu(rsp->common.status);
4236         int level = IBMVFC_DEFAULT_LOG_LEVEL;
4237
4238         switch (mad_status) {
4239         case IBMVFC_MAD_SUCCESS:
4240                 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
4241                 vhost->num_targets = be32_to_cpu(rsp->num_written);
4242                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
4243                 break;
4244         case IBMVFC_MAD_FAILED:
4245                 level += ibmvfc_retry_host_init(vhost);
4246                 ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
4247                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4248                            be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4249                 break;
4250         case IBMVFC_MAD_DRIVER_FAILED:
4251                 break;
4252         default:
4253                 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
4254                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4255                 break;
4256         }
4257
4258         ibmvfc_free_event(evt);
4259         wake_up(&vhost->work_wait_q);
4260 }
4261
4262 /**
4263  * ibmvfc_discover_targets - Send Discover Targets MAD
4264  * @vhost:      ibmvfc host struct
4265  *
4266  **/
4267 static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
4268 {
4269         struct ibmvfc_discover_targets *mad;
4270         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4271
4272         ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
4273         mad = &evt->iu.discover_targets;
4274         memset(mad, 0, sizeof(*mad));
4275         mad->common.version = cpu_to_be32(1);
4276         mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
4277         mad->common.length = cpu_to_be16(sizeof(*mad));
4278         mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
4279         mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
4280         mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
4281         mad->flags = cpu_to_be32(IBMVFC_DISC_TGT_PORT_ID_WWPN_LIST);
4282         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4283
4284         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4285                 ibmvfc_dbg(vhost, "Sent discover targets\n");
4286         else
4287                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4288 }
4289
4290 /**
4291  * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4292  * @evt:        ibmvfc event struct
4293  *
4294  **/
4295 static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
4296 {
4297         struct ibmvfc_host *vhost = evt->vhost;
4298         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
4299         struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
4300         unsigned int npiv_max_sectors;
4301         int level = IBMVFC_DEFAULT_LOG_LEVEL;
4302
4303         switch (mad_status) {
4304         case IBMVFC_MAD_SUCCESS:
4305                 ibmvfc_free_event(evt);
4306                 break;
4307         case IBMVFC_MAD_FAILED:
4308                 if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
4309                         level += ibmvfc_retry_host_init(vhost);
4310                 else
4311                         ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4312                 ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
4313                            ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
4314                                                 be16_to_cpu(rsp->status), be16_to_cpu(rsp->error));
4315                 ibmvfc_free_event(evt);
4316                 return;
4317         case IBMVFC_MAD_CRQ_ERROR:
4318                 ibmvfc_retry_host_init(vhost);
4319                 fallthrough;
4320         case IBMVFC_MAD_DRIVER_FAILED:
4321                 ibmvfc_free_event(evt);
4322                 return;
4323         default:
4324                 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
4325                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4326                 ibmvfc_free_event(evt);
4327                 return;
4328         }
4329
4330         vhost->client_migrated = 0;
4331
4332         if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
4333                 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
4334                         rsp->flags);
4335                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4336                 wake_up(&vhost->work_wait_q);
4337                 return;
4338         }
4339
4340         if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
4341                 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
4342                         rsp->max_cmds);
4343                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4344                 wake_up(&vhost->work_wait_q);
4345                 return;
4346         }
4347
4348         vhost->logged_in = 1;
4349         npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
4350         dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
4351                  rsp->partition_name, rsp->device_name, rsp->port_loc_code,
4352                  rsp->drc_name, npiv_max_sectors);
4353
4354         fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
4355         fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
4356         fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
4357         fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
4358         fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
4359         fc_host_supported_classes(vhost->host) = 0;
4360         if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
4361                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
4362         if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
4363                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
4364         if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
4365                 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
4366         fc_host_maxframe_size(vhost->host) =
4367                 be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
4368
4369         vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
4370         vhost->host->max_sectors = npiv_max_sectors;
4371         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4372         wake_up(&vhost->work_wait_q);
4373 }
4374
4375 /**
4376  * ibmvfc_npiv_login - Sends NPIV login
4377  * @vhost:      ibmvfc host struct
4378  *
4379  **/
4380 static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
4381 {
4382         struct ibmvfc_npiv_login_mad *mad;
4383         struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
4384
4385         ibmvfc_gather_partition_info(vhost);
4386         ibmvfc_set_login_info(vhost);
4387         ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
4388
4389         memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
4390         mad = &evt->iu.npiv_login;
4391         memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
4392         mad->common.version = cpu_to_be32(1);
4393         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
4394         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
4395         mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
4396         mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
4397
4398         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
4399
4400         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4401                 ibmvfc_dbg(vhost, "Sent NPIV login\n");
4402         else
4403                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4404 };
4405
4406 /**
4407  * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4408  * @vhost:              ibmvfc host struct
4409  *
4410  **/
4411 static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
4412 {
4413         struct ibmvfc_host *vhost = evt->vhost;
4414         u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
4415
4416         ibmvfc_free_event(evt);
4417
4418         switch (mad_status) {
4419         case IBMVFC_MAD_SUCCESS:
4420                 if (list_empty(&vhost->sent) &&
4421                     vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
4422                         ibmvfc_init_host(vhost);
4423                         return;
4424                 }
4425                 break;
4426         case IBMVFC_MAD_FAILED:
4427         case IBMVFC_MAD_NOT_SUPPORTED:
4428         case IBMVFC_MAD_CRQ_ERROR:
4429         case IBMVFC_MAD_DRIVER_FAILED:
4430         default:
4431                 ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
4432                 break;
4433         }
4434
4435         ibmvfc_hard_reset_host(vhost);
4436 }
4437
4438 /**
4439  * ibmvfc_npiv_logout - Issue an NPIV Logout
4440  * @vhost:              ibmvfc host struct
4441  *
4442  **/
4443 static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
4444 {
4445         struct ibmvfc_npiv_logout_mad *mad;
4446         struct ibmvfc_event *evt;
4447
4448         evt = ibmvfc_get_event(vhost);
4449         ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
4450
4451         mad = &evt->iu.npiv_logout;
4452         memset(mad, 0, sizeof(*mad));
4453         mad->common.version = cpu_to_be32(1);
4454         mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
4455         mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
4456
4457         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
4458
4459         if (!ibmvfc_send_event(evt, vhost, default_timeout))
4460                 ibmvfc_dbg(vhost, "Sent NPIV logout\n");
4461         else
4462                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4463 }
4464
4465 /**
4466  * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4467  * @vhost:              ibmvfc host struct
4468  *
4469  * Returns:
4470  *      1 if work to do / 0 if not
4471  **/
4472 static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
4473 {
4474         struct ibmvfc_target *tgt;
4475
4476         list_for_each_entry(tgt, &vhost->targets, queue) {
4477                 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
4478                     tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4479                         return 1;
4480         }
4481
4482         return 0;
4483 }
4484
4485 /**
4486  * ibmvfc_dev_logo_to_do - Is there target logout work to do?
4487  * @vhost:              ibmvfc host struct
4488  *
4489  * Returns:
4490  *      1 if work to do / 0 if not
4491  **/
4492 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host *vhost)
4493 {
4494         struct ibmvfc_target *tgt;
4495
4496         list_for_each_entry(tgt, &vhost->targets, queue) {
4497                 if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT ||
4498                     tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4499                         return 1;
4500         }
4501         return 0;
4502 }
4503
4504 /**
4505  * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4506  * @vhost:              ibmvfc host struct
4507  *
4508  * Returns:
4509  *      1 if work to do / 0 if not
4510  **/
4511 static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4512 {
4513         struct ibmvfc_target *tgt;
4514
4515         if (kthread_should_stop())
4516                 return 1;
4517         switch (vhost->action) {
4518         case IBMVFC_HOST_ACTION_NONE:
4519         case IBMVFC_HOST_ACTION_INIT_WAIT:
4520         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4521                 return 0;
4522         case IBMVFC_HOST_ACTION_TGT_INIT:
4523         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4524                 if (vhost->discovery_threads == disc_threads)
4525                         return 0;
4526                 list_for_each_entry(tgt, &vhost->targets, queue)
4527                         if (tgt->action == IBMVFC_TGT_ACTION_INIT)
4528                                 return 1;
4529                 list_for_each_entry(tgt, &vhost->targets, queue)
4530                         if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
4531                                 return 0;
4532                 return 1;
4533         case IBMVFC_HOST_ACTION_TGT_DEL:
4534         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4535                 if (vhost->discovery_threads == disc_threads)
4536                         return 0;
4537                 list_for_each_entry(tgt, &vhost->targets, queue)
4538                         if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT)
4539                                 return 1;
4540                 list_for_each_entry(tgt, &vhost->targets, queue)
4541                         if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT)
4542                                 return 0;
4543                 return 1;
4544         case IBMVFC_HOST_ACTION_LOGO:
4545         case IBMVFC_HOST_ACTION_INIT:
4546         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4547         case IBMVFC_HOST_ACTION_QUERY:
4548         case IBMVFC_HOST_ACTION_RESET:
4549         case IBMVFC_HOST_ACTION_REENABLE:
4550         default:
4551                 break;
4552         }
4553
4554         return 1;
4555 }
4556
4557 /**
4558  * ibmvfc_work_to_do - Is there task level work to do?
4559  * @vhost:              ibmvfc host struct
4560  *
4561  * Returns:
4562  *      1 if work to do / 0 if not
4563  **/
4564 static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
4565 {
4566         unsigned long flags;
4567         int rc;
4568
4569         spin_lock_irqsave(vhost->host->host_lock, flags);
4570         rc = __ibmvfc_work_to_do(vhost);
4571         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4572         return rc;
4573 }
4574
4575 /**
4576  * ibmvfc_log_ae - Log async events if necessary
4577  * @vhost:              ibmvfc host struct
4578  * @events:             events to log
4579  *
4580  **/
4581 static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
4582 {
4583         if (events & IBMVFC_AE_RSCN)
4584                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
4585         if ((events & IBMVFC_AE_LINKDOWN) &&
4586             vhost->state >= IBMVFC_HALTED)
4587                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
4588         if ((events & IBMVFC_AE_LINKUP) &&
4589             vhost->state == IBMVFC_INITIALIZING)
4590                 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
4591 }
4592
4593 /**
4594  * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4595  * @tgt:                ibmvfc target struct
4596  *
4597  **/
4598 static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
4599 {
4600         struct ibmvfc_host *vhost = tgt->vhost;
4601         struct fc_rport *rport;
4602         unsigned long flags;
4603
4604         tgt_dbg(tgt, "Adding rport\n");
4605         rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
4606         spin_lock_irqsave(vhost->host->host_lock, flags);
4607
4608         if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4609                 tgt_dbg(tgt, "Deleting rport\n");
4610                 list_del(&tgt->queue);
4611                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4612                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4613                 fc_remote_port_delete(rport);
4614                 del_timer_sync(&tgt->timer);
4615                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4616                 return;
4617         } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4618                 tgt_dbg(tgt, "Deleting rport with outstanding I/O\n");
4619                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4620                 tgt->rport = NULL;
4621                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4622                 fc_remote_port_delete(rport);
4623                 return;
4624         } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
4625                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4626                 return;
4627         }
4628
4629         if (rport) {
4630                 tgt_dbg(tgt, "rport add succeeded\n");
4631                 tgt->rport = rport;
4632                 rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
4633                 rport->supported_classes = 0;
4634                 tgt->target_id = rport->scsi_target_id;
4635                 if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
4636                         rport->supported_classes |= FC_COS_CLASS1;
4637                 if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
4638                         rport->supported_classes |= FC_COS_CLASS2;
4639                 if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
4640                         rport->supported_classes |= FC_COS_CLASS3;
4641                 if (rport->rqst_q)
4642                         blk_queue_max_segments(rport->rqst_q, 1);
4643         } else
4644                 tgt_dbg(tgt, "rport add failed\n");
4645         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4646 }
4647
4648 /**
4649  * ibmvfc_do_work - Do task level work
4650  * @vhost:              ibmvfc host struct
4651  *
4652  **/
4653 static void ibmvfc_do_work(struct ibmvfc_host *vhost)
4654 {
4655         struct ibmvfc_target *tgt;
4656         unsigned long flags;
4657         struct fc_rport *rport;
4658         int rc;
4659
4660         ibmvfc_log_ae(vhost, vhost->events_to_log);
4661         spin_lock_irqsave(vhost->host->host_lock, flags);
4662         vhost->events_to_log = 0;
4663         switch (vhost->action) {
4664         case IBMVFC_HOST_ACTION_NONE:
4665         case IBMVFC_HOST_ACTION_LOGO_WAIT:
4666         case IBMVFC_HOST_ACTION_INIT_WAIT:
4667                 break;
4668         case IBMVFC_HOST_ACTION_RESET:
4669                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4670                 rc = ibmvfc_reset_crq(vhost);
4671
4672                 spin_lock_irqsave(vhost->host->host_lock, flags);
4673                 if (!rc || rc == H_CLOSED)
4674                         vio_enable_interrupts(to_vio_dev(vhost->dev));
4675                 if (vhost->action == IBMVFC_HOST_ACTION_RESET) {
4676                         /*
4677                          * The only action we could have changed to would have
4678                          * been reenable, in which case, we skip the rest of
4679                          * this path and wait until we've done the re-enable
4680                          * before sending the crq init.
4681                          */
4682                         vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4683
4684                         if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
4685                             (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
4686                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4687                                 dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
4688                         }
4689                 }
4690                 break;
4691         case IBMVFC_HOST_ACTION_REENABLE:
4692                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4693                 rc = ibmvfc_reenable_crq_queue(vhost);
4694
4695                 spin_lock_irqsave(vhost->host->host_lock, flags);
4696                 if (vhost->action == IBMVFC_HOST_ACTION_REENABLE) {
4697                         /*
4698                          * The only action we could have changed to would have
4699                          * been reset, in which case, we skip the rest of this
4700                          * path and wait until we've done the reset before
4701                          * sending the crq init.
4702                          */
4703                         vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
4704                         if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
4705                                 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
4706                                 dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
4707                         }
4708                 }
4709                 break;
4710         case IBMVFC_HOST_ACTION_LOGO:
4711                 vhost->job_step(vhost);
4712                 break;
4713         case IBMVFC_HOST_ACTION_INIT:
4714                 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
4715                 if (vhost->delay_init) {
4716                         vhost->delay_init = 0;
4717                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4718                         ssleep(15);
4719                         return;
4720                 } else
4721                         vhost->job_step(vhost);
4722                 break;
4723         case IBMVFC_HOST_ACTION_QUERY:
4724                 list_for_each_entry(tgt, &vhost->targets, queue)
4725                         ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
4726                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
4727                 break;
4728         case IBMVFC_HOST_ACTION_QUERY_TGTS:
4729                 list_for_each_entry(tgt, &vhost->targets, queue) {
4730                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4731                                 tgt->job_step(tgt);
4732                                 break;
4733                         }
4734                 }
4735
4736                 if (!ibmvfc_dev_init_to_do(vhost))
4737                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
4738                 break;
4739         case IBMVFC_HOST_ACTION_TGT_DEL:
4740         case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
4741                 list_for_each_entry(tgt, &vhost->targets, queue) {
4742                         if (tgt->action == IBMVFC_TGT_ACTION_LOGOUT_RPORT) {
4743                                 tgt->job_step(tgt);
4744                                 break;
4745                         }
4746                 }
4747
4748                 if (ibmvfc_dev_logo_to_do(vhost)) {
4749                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4750                         return;
4751                 }
4752
4753                 list_for_each_entry(tgt, &vhost->targets, queue) {
4754                         if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
4755                                 tgt_dbg(tgt, "Deleting rport\n");
4756                                 rport = tgt->rport;
4757                                 tgt->rport = NULL;
4758                                 list_del(&tgt->queue);
4759                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
4760                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4761                                 if (rport)
4762                                         fc_remote_port_delete(rport);
4763                                 del_timer_sync(&tgt->timer);
4764                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
4765                                 return;
4766                         } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_AND_LOGOUT_RPORT) {
4767                                 tgt_dbg(tgt, "Deleting rport with I/O outstanding\n");
4768                                 rport = tgt->rport;
4769                                 tgt->rport = NULL;
4770                                 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_LOGOUT_DELETED_RPORT);
4771                                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4772                                 if (rport)
4773                                         fc_remote_port_delete(rport);
4774                                 return;
4775                         }
4776                 }
4777
4778                 if (vhost->state == IBMVFC_INITIALIZING) {
4779                         if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
4780                                 if (vhost->reinit) {
4781                                         vhost->reinit = 0;
4782                                         scsi_block_requests(vhost->host);
4783                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
4784                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4785                                 } else {
4786                                         ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
4787                                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4788                                         wake_up(&vhost->init_wait_q);
4789                                         schedule_work(&vhost->rport_add_work_q);
4790                                         vhost->init_retries = 0;
4791                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4792                                         scsi_unblock_requests(vhost->host);
4793                                 }
4794
4795                                 return;
4796                         } else {
4797                                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
4798                                 vhost->job_step = ibmvfc_discover_targets;
4799                         }
4800                 } else {
4801                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
4802                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4803                         scsi_unblock_requests(vhost->host);
4804                         wake_up(&vhost->init_wait_q);
4805                         return;
4806                 }
4807                 break;
4808         case IBMVFC_HOST_ACTION_ALLOC_TGTS:
4809                 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
4810                 spin_unlock_irqrestore(vhost->host->host_lock, flags);
4811                 ibmvfc_alloc_targets(vhost);
4812                 spin_lock_irqsave(vhost->host->host_lock, flags);
4813                 break;
4814         case IBMVFC_HOST_ACTION_TGT_INIT:
4815                 list_for_each_entry(tgt, &vhost->targets, queue) {
4816                         if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
4817                                 tgt->job_step(tgt);
4818                                 break;
4819                         }
4820                 }
4821
4822                 if (!ibmvfc_dev_init_to_do(vhost))
4823                         ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
4824                 break;
4825         default:
4826                 break;
4827         }
4828
4829         spin_unlock_irqrestore(vhost->host->host_lock, flags);
4830 }
4831
4832 /**
4833  * ibmvfc_work - Do task level work
4834  * @data:               ibmvfc host struct
4835  *
4836  * Returns:
4837  *      zero
4838  **/
4839 static int ibmvfc_work(void *data)
4840 {
4841         struct ibmvfc_host *vhost = data;
4842         int rc;
4843
4844         set_user_nice(current, MIN_NICE);
4845
4846         while (1) {
4847                 rc = wait_event_interruptible(vhost->work_wait_q,
4848                                               ibmvfc_work_to_do(vhost));
4849
4850                 BUG_ON(rc);
4851
4852                 if (kthread_should_stop())
4853                         break;
4854
4855                 ibmvfc_do_work(vhost);
4856         }
4857
4858         ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
4859         return 0;
4860 }
4861
4862 /**
4863  * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4864  * @vhost:      ibmvfc host struct
4865  *
4866  * Allocates a page for messages, maps it for dma, and registers
4867  * the crq with the hypervisor.
4868  *
4869  * Return value:
4870  *      zero on success / other on failure
4871  **/
4872 static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
4873 {
4874         int rc, retrc = -ENOMEM;
4875         struct device *dev = vhost->dev;
4876         struct vio_dev *vdev = to_vio_dev(dev);
4877         struct ibmvfc_crq_queue *crq = &vhost->crq;
4878
4879         ENTER;
4880         crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
4881
4882         if (!crq->msgs)
4883                 return -ENOMEM;
4884
4885         crq->size = PAGE_SIZE / sizeof(*crq->msgs);
4886         crq->msg_token = dma_map_single(dev, crq->msgs,
4887                                         PAGE_SIZE, DMA_BIDIRECTIONAL);
4888
4889         if (dma_mapping_error(dev, crq->msg_token))
4890                 goto map_failed;
4891
4892         retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
4893                                         crq->msg_token, PAGE_SIZE);
4894
4895         if (rc == H_RESOURCE)
4896                 /* maybe kexecing and resource is busy. try a reset */
4897                 retrc = rc = ibmvfc_reset_crq(vhost);
4898
4899         if (rc == H_CLOSED)
4900                 dev_warn(dev, "Partner adapter not ready\n");
4901         else if (rc) {
4902                 dev_warn(dev, "Error %d opening adapter\n", rc);
4903                 goto reg_crq_failed;
4904         }
4905
4906         retrc = 0;
4907
4908         tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
4909
4910         if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
4911                 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
4912                 goto req_irq_failed;
4913         }
4914
4915         if ((rc = vio_enable_interrupts(vdev))) {
4916                 dev_err(dev, "Error %d enabling interrupts\n", rc);
4917                 goto req_irq_failed;
4918         }
4919
4920         crq->cur = 0;
4921         LEAVE;
4922         return retrc;
4923
4924 req_irq_failed:
4925         tasklet_kill(&vhost->tasklet);
4926         do {
4927                 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
4928         } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
4929 reg_crq_failed:
4930         dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
4931 map_failed:
4932         free_page((unsigned long)crq->msgs);
4933         return retrc;
4934 }
4935
4936 /**
4937  * ibmvfc_free_mem - Free memory for vhost
4938  * @vhost:      ibmvfc host struct
4939  *
4940  * Return value:
4941  *      none
4942  **/
4943 static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
4944 {
4945         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4946
4947         ENTER;
4948         mempool_destroy(vhost->tgt_pool);
4949         kfree(vhost->trace);
4950         dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
4951                           vhost->disc_buf_dma);
4952         dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
4953                           vhost->login_buf, vhost->login_buf_dma);
4954         dma_pool_destroy(vhost->sg_pool);
4955         dma_unmap_single(vhost->dev, async_q->msg_token,
4956                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
4957         free_page((unsigned long)async_q->msgs);
4958         LEAVE;
4959 }
4960
4961 /**
4962  * ibmvfc_alloc_mem - Allocate memory for vhost
4963  * @vhost:      ibmvfc host struct
4964  *
4965  * Return value:
4966  *      0 on success / non-zero on failure
4967  **/
4968 static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
4969 {
4970         struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
4971         struct device *dev = vhost->dev;
4972
4973         ENTER;
4974         async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
4975         if (!async_q->msgs) {
4976                 dev_err(dev, "Couldn't allocate async queue.\n");
4977                 goto nomem;
4978         }
4979
4980         async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
4981         async_q->msg_token = dma_map_single(dev, async_q->msgs,
4982                                             async_q->size * sizeof(*async_q->msgs),
4983                                             DMA_BIDIRECTIONAL);
4984
4985         if (dma_mapping_error(dev, async_q->msg_token)) {
4986                 dev_err(dev, "Failed to map async queue\n");
4987                 goto free_async_crq;
4988         }
4989
4990         vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
4991                                          SG_ALL * sizeof(struct srp_direct_buf),
4992                                          sizeof(struct srp_direct_buf), 0);
4993
4994         if (!vhost->sg_pool) {
4995                 dev_err(dev, "Failed to allocate sg pool\n");
4996                 goto unmap_async_crq;
4997         }
4998
4999         vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
5000                                               &vhost->login_buf_dma, GFP_KERNEL);
5001
5002         if (!vhost->login_buf) {
5003                 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
5004                 goto free_sg_pool;
5005         }
5006
5007         vhost->disc_buf_sz = sizeof(*vhost->disc_buf) * max_targets;
5008         vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
5009                                              &vhost->disc_buf_dma, GFP_KERNEL);
5010
5011         if (!vhost->disc_buf) {
5012                 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
5013                 goto free_login_buffer;
5014         }
5015
5016         vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
5017                                sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
5018
5019         if (!vhost->trace)
5020                 goto free_disc_buffer;
5021
5022         vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
5023                                                       sizeof(struct ibmvfc_target));
5024
5025         if (!vhost->tgt_pool) {
5026                 dev_err(dev, "Couldn't allocate target memory pool\n");
5027                 goto free_trace;
5028         }
5029
5030         LEAVE;
5031         return 0;
5032
5033 free_trace:
5034         kfree(vhost->trace);
5035 free_disc_buffer:
5036         dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
5037                           vhost->disc_buf_dma);
5038 free_login_buffer:
5039         dma_free_coherent(dev, sizeof(*vhost->login_buf),
5040                           vhost->login_buf, vhost->login_buf_dma);
5041 free_sg_pool:
5042         dma_pool_destroy(vhost->sg_pool);
5043 unmap_async_crq:
5044         dma_unmap_single(dev, async_q->msg_token,
5045                          async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
5046 free_async_crq:
5047         free_page((unsigned long)async_q->msgs);
5048 nomem:
5049         LEAVE;
5050         return -ENOMEM;
5051 }
5052
5053 /**
5054  * ibmvfc_rport_add_thread - Worker thread for rport adds
5055  * @work:       work struct
5056  *
5057  **/
5058 static void ibmvfc_rport_add_thread(struct work_struct *work)
5059 {
5060         struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
5061                                                  rport_add_work_q);
5062         struct ibmvfc_target *tgt;
5063         struct fc_rport *rport;
5064         unsigned long flags;
5065         int did_work;
5066
5067         ENTER;
5068         spin_lock_irqsave(vhost->host->host_lock, flags);
5069         do {
5070                 did_work = 0;
5071                 if (vhost->state != IBMVFC_ACTIVE)
5072                         break;
5073
5074                 list_for_each_entry(tgt, &vhost->targets, queue) {
5075                         if (tgt->add_rport) {
5076                                 did_work = 1;
5077                                 tgt->add_rport = 0;
5078                                 kref_get(&tgt->kref);
5079                                 rport = tgt->rport;
5080                                 if (!rport) {
5081                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5082                                         ibmvfc_tgt_add_rport(tgt);
5083                                 } else if (get_device(&rport->dev)) {
5084                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5085                                         tgt_dbg(tgt, "Setting rport roles\n");
5086                                         fc_remote_port_rolechg(rport, tgt->ids.roles);
5087                                         put_device(&rport->dev);
5088                                 } else {
5089                                         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5090                                 }
5091
5092                                 kref_put(&tgt->kref, ibmvfc_release_tgt);
5093                                 spin_lock_irqsave(vhost->host->host_lock, flags);
5094                                 break;
5095                         }
5096                 }
5097         } while(did_work);
5098
5099         if (vhost->state == IBMVFC_ACTIVE)
5100                 vhost->scan_complete = 1;
5101         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5102         LEAVE;
5103 }
5104
5105 /**
5106  * ibmvfc_probe - Adapter hot plug add entry point
5107  * @vdev:       vio device struct
5108  * @id: vio device id struct
5109  *
5110  * Return value:
5111  *      0 on success / non-zero on failure
5112  **/
5113 static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
5114 {
5115         struct ibmvfc_host *vhost;
5116         struct Scsi_Host *shost;
5117         struct device *dev = &vdev->dev;
5118         int rc = -ENOMEM;
5119
5120         ENTER;
5121         shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
5122         if (!shost) {
5123                 dev_err(dev, "Couldn't allocate host data\n");
5124                 goto out;
5125         }
5126
5127         shost->transportt = ibmvfc_transport_template;
5128         shost->can_queue = max_requests;
5129         shost->max_lun = max_lun;
5130         shost->max_id = max_targets;
5131         shost->max_sectors = IBMVFC_MAX_SECTORS;
5132         shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
5133         shost->unique_id = shost->host_no;
5134
5135         vhost = shost_priv(shost);
5136         INIT_LIST_HEAD(&vhost->sent);
5137         INIT_LIST_HEAD(&vhost->free);
5138         INIT_LIST_HEAD(&vhost->targets);
5139         sprintf(vhost->name, IBMVFC_NAME);
5140         vhost->host = shost;
5141         vhost->dev = dev;
5142         vhost->partition_number = -1;
5143         vhost->log_level = log_level;
5144         vhost->task_set = 1;
5145         strcpy(vhost->partition_name, "UNKNOWN");
5146         init_waitqueue_head(&vhost->work_wait_q);
5147         init_waitqueue_head(&vhost->init_wait_q);
5148         INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
5149         mutex_init(&vhost->passthru_mutex);
5150
5151         if ((rc = ibmvfc_alloc_mem(vhost)))
5152                 goto free_scsi_host;
5153
5154         vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
5155                                          shost->host_no);
5156
5157         if (IS_ERR(vhost->work_thread)) {
5158                 dev_err(dev, "Couldn't create kernel thread: %ld\n",
5159                         PTR_ERR(vhost->work_thread));
5160                 rc = PTR_ERR(vhost->work_thread);
5161                 goto free_host_mem;
5162         }
5163
5164         if ((rc = ibmvfc_init_crq(vhost))) {
5165                 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
5166                 goto kill_kthread;
5167         }
5168
5169         if ((rc = ibmvfc_init_event_pool(vhost))) {
5170                 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
5171                 goto release_crq;
5172         }
5173
5174         if ((rc = scsi_add_host(shost, dev)))
5175                 goto release_event_pool;
5176
5177         fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
5178
5179         if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
5180                                            &ibmvfc_trace_attr))) {
5181                 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
5182                 goto remove_shost;
5183         }
5184
5185         if (shost_to_fc_host(shost)->rqst_q)
5186                 blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
5187         dev_set_drvdata(dev, vhost);
5188         spin_lock(&ibmvfc_driver_lock);
5189         list_add_tail(&vhost->queue, &ibmvfc_head);
5190         spin_unlock(&ibmvfc_driver_lock);
5191
5192         ibmvfc_send_crq_init(vhost);
5193         scsi_scan_host(shost);
5194         return 0;
5195
5196 remove_shost:
5197         scsi_remove_host(shost);
5198 release_event_pool:
5199         ibmvfc_free_event_pool(vhost);
5200 release_crq:
5201         ibmvfc_release_crq_queue(vhost);
5202 kill_kthread:
5203         kthread_stop(vhost->work_thread);
5204 free_host_mem:
5205         ibmvfc_free_mem(vhost);
5206 free_scsi_host:
5207         scsi_host_put(shost);
5208 out:
5209         LEAVE;
5210         return rc;
5211 }
5212
5213 /**
5214  * ibmvfc_remove - Adapter hot plug remove entry point
5215  * @vdev:       vio device struct
5216  *
5217  * Return value:
5218  *      0
5219  **/
5220 static int ibmvfc_remove(struct vio_dev *vdev)
5221 {
5222         struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
5223         unsigned long flags;
5224
5225         ENTER;
5226         ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
5227
5228         spin_lock_irqsave(vhost->host->host_lock, flags);
5229         ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
5230         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5231
5232         ibmvfc_wait_while_resetting(vhost);
5233         ibmvfc_release_crq_queue(vhost);
5234         kthread_stop(vhost->work_thread);
5235         fc_remove_host(vhost->host);
5236         scsi_remove_host(vhost->host);
5237
5238         spin_lock_irqsave(vhost->host->host_lock, flags);
5239         ibmvfc_purge_requests(vhost, DID_ERROR);
5240         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5241         ibmvfc_free_event_pool(vhost);
5242
5243         ibmvfc_free_mem(vhost);
5244         spin_lock(&ibmvfc_driver_lock);
5245         list_del(&vhost->queue);
5246         spin_unlock(&ibmvfc_driver_lock);
5247         scsi_host_put(vhost->host);
5248         LEAVE;
5249         return 0;
5250 }
5251
5252 /**
5253  * ibmvfc_resume - Resume from suspend
5254  * @dev:        device struct
5255  *
5256  * We may have lost an interrupt across suspend/resume, so kick the
5257  * interrupt handler
5258  *
5259  */
5260 static int ibmvfc_resume(struct device *dev)
5261 {
5262         unsigned long flags;
5263         struct ibmvfc_host *vhost = dev_get_drvdata(dev);
5264         struct vio_dev *vdev = to_vio_dev(dev);
5265
5266         spin_lock_irqsave(vhost->host->host_lock, flags);
5267         vio_disable_interrupts(vdev);
5268         tasklet_schedule(&vhost->tasklet);
5269         spin_unlock_irqrestore(vhost->host->host_lock, flags);
5270         return 0;
5271 }
5272
5273 /**
5274  * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
5275  * @vdev:       vio device struct
5276  *
5277  * Return value:
5278  *      Number of bytes the driver will need to DMA map at the same time in
5279  *      order to perform well.
5280  */
5281 static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
5282 {
5283         unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
5284         return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
5285 }
5286
5287 static const struct vio_device_id ibmvfc_device_table[] = {
5288         {"fcp", "IBM,vfc-client"},
5289         { "", "" }
5290 };
5291 MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
5292
5293 static const struct dev_pm_ops ibmvfc_pm_ops = {
5294         .resume = ibmvfc_resume
5295 };
5296
5297 static struct vio_driver ibmvfc_driver = {
5298         .id_table = ibmvfc_device_table,
5299         .probe = ibmvfc_probe,
5300         .remove = ibmvfc_remove,
5301         .get_desired_dma = ibmvfc_get_desired_dma,
5302         .name = IBMVFC_NAME,
5303         .pm = &ibmvfc_pm_ops,
5304 };
5305
5306 static struct fc_function_template ibmvfc_transport_functions = {
5307         .show_host_fabric_name = 1,
5308         .show_host_node_name = 1,
5309         .show_host_port_name = 1,
5310         .show_host_supported_classes = 1,
5311         .show_host_port_type = 1,
5312         .show_host_port_id = 1,
5313         .show_host_maxframe_size = 1,
5314
5315         .get_host_port_state = ibmvfc_get_host_port_state,
5316         .show_host_port_state = 1,
5317
5318         .get_host_speed = ibmvfc_get_host_speed,
5319         .show_host_speed = 1,
5320
5321         .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
5322         .terminate_rport_io = ibmvfc_terminate_rport_io,
5323
5324         .show_rport_maxframe_size = 1,
5325         .show_rport_supported_classes = 1,
5326
5327         .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
5328         .show_rport_dev_loss_tmo = 1,
5329
5330         .get_starget_node_name = ibmvfc_get_starget_node_name,
5331         .show_starget_node_name = 1,
5332
5333         .get_starget_port_name = ibmvfc_get_starget_port_name,
5334         .show_starget_port_name = 1,
5335
5336         .get_starget_port_id = ibmvfc_get_starget_port_id,
5337         .show_starget_port_id = 1,
5338
5339         .bsg_request = ibmvfc_bsg_request,
5340         .bsg_timeout = ibmvfc_bsg_timeout,
5341 };
5342
5343 /**
5344  * ibmvfc_module_init - Initialize the ibmvfc module
5345  *
5346  * Return value:
5347  *      0 on success / other on failure
5348  **/
5349 static int __init ibmvfc_module_init(void)
5350 {
5351         int rc;
5352
5353         if (!firmware_has_feature(FW_FEATURE_VIO))
5354                 return -ENODEV;
5355
5356         printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
5357                IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
5358
5359         ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
5360         if (!ibmvfc_transport_template)
5361                 return -ENOMEM;
5362
5363         rc = vio_register_driver(&ibmvfc_driver);
5364         if (rc)
5365                 fc_release_transport(ibmvfc_transport_template);
5366         return rc;
5367 }
5368
5369 /**
5370  * ibmvfc_module_exit - Teardown the ibmvfc module
5371  *
5372  * Return value:
5373  *      nothing
5374  **/
5375 static void __exit ibmvfc_module_exit(void)
5376 {
5377         vio_unregister_driver(&ibmvfc_driver);
5378         fc_release_transport(ibmvfc_transport_template);
5379 }
5380
5381 module_init(ibmvfc_module_init);
5382 module_exit(ibmvfc_module_exit);