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