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