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