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