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