1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
5 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) IBM Corporation, 2008
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/kthread.h>
17 #include <linux/slab.h>
20 #include <linux/stringify.h>
21 #include <linux/bsg-lib.h>
22 #include <asm/firmware.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
34 static unsigned int init_timeout
= IBMVFC_INIT_TIMEOUT
;
35 static unsigned int default_timeout
= IBMVFC_DEFAULT_TIMEOUT
;
36 static u64 max_lun
= IBMVFC_MAX_LUN
;
37 static unsigned int max_targets
= IBMVFC_MAX_TARGETS
;
38 static unsigned int max_requests
= IBMVFC_MAX_REQUESTS_DEFAULT
;
39 static unsigned int disc_threads
= IBMVFC_MAX_DISC_THREADS
;
40 static unsigned int ibmvfc_debug
= IBMVFC_DEBUG
;
41 static unsigned int log_level
= IBMVFC_DEFAULT_LOG_LEVEL
;
42 static unsigned int cls3_error
= IBMVFC_CLS3_ERROR
;
43 static LIST_HEAD(ibmvfc_head
);
44 static DEFINE_SPINLOCK(ibmvfc_driver_lock
);
45 static struct scsi_transport_template
*ibmvfc_transport_template
;
47 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
48 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(IBMVFC_DRIVER_VERSION
);
52 module_param_named(init_timeout
, init_timeout
, uint
, S_IRUGO
| S_IWUSR
);
53 MODULE_PARM_DESC(init_timeout
, "Initialization timeout in seconds. "
54 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT
) "]");
55 module_param_named(default_timeout
, default_timeout
, uint
, S_IRUGO
| S_IWUSR
);
56 MODULE_PARM_DESC(default_timeout
,
57 "Default timeout in seconds for initialization and EH commands. "
58 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT
) "]");
59 module_param_named(max_requests
, max_requests
, uint
, S_IRUGO
);
60 MODULE_PARM_DESC(max_requests
, "Maximum requests for this adapter. "
61 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT
) "]");
62 module_param_named(max_lun
, max_lun
, ullong
, S_IRUGO
);
63 MODULE_PARM_DESC(max_lun
, "Maximum allowed LUN. "
64 "[Default=" __stringify(IBMVFC_MAX_LUN
) "]");
65 module_param_named(max_targets
, max_targets
, uint
, S_IRUGO
);
66 MODULE_PARM_DESC(max_targets
, "Maximum allowed targets. "
67 "[Default=" __stringify(IBMVFC_MAX_TARGETS
) "]");
68 module_param_named(disc_threads
, disc_threads
, uint
, S_IRUGO
);
69 MODULE_PARM_DESC(disc_threads
, "Number of device discovery threads to use. "
70 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS
) "]");
71 module_param_named(debug
, ibmvfc_debug
, uint
, S_IRUGO
| S_IWUSR
);
72 MODULE_PARM_DESC(debug
, "Enable driver debug information. "
73 "[Default=" __stringify(IBMVFC_DEBUG
) "]");
74 module_param_named(log_level
, log_level
, uint
, 0);
75 MODULE_PARM_DESC(log_level
, "Set to 0 - 4 for increasing verbosity of device driver. "
76 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL
) "]");
77 module_param_named(cls3_error
, cls3_error
, uint
, 0);
78 MODULE_PARM_DESC(cls3_error
, "Enable FC Class 3 Error Recovery. "
79 "[Default=" __stringify(IBMVFC_CLS3_ERROR
) "]");
89 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_ESTABLISH
, DID_ERROR
, 1, 1, "unable to establish" },
90 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_FAULT
, DID_OK
, 1, 0, "transport fault" },
91 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CMD_TIMEOUT
, DID_TIME_OUT
, 1, 1, "command timeout" },
92 { IBMVFC_FABRIC_MAPPED
, IBMVFC_ENETDOWN
, DID_TRANSPORT_DISRUPTED
, 1, 1, "network down" },
93 { IBMVFC_FABRIC_MAPPED
, IBMVFC_HW_FAILURE
, DID_ERROR
, 1, 1, "hardware failure" },
94 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DOWN_ERR
, DID_REQUEUE
, 0, 0, "link down" },
95 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DEAD_ERR
, DID_ERROR
, 0, 0, "link dead" },
96 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_REGISTER
, DID_ERROR
, 1, 1, "unable to register" },
97 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_BUSY
, DID_BUS_BUSY
, 1, 0, "transport busy" },
98 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_DEAD
, DID_ERROR
, 0, 1, "transport dead" },
99 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CONFIG_ERROR
, DID_ERROR
, 1, 1, "configuration error" },
100 { IBMVFC_FABRIC_MAPPED
, IBMVFC_NAME_SERVER_FAIL
, DID_ERROR
, 1, 1, "name server failure" },
101 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_HALTED
, DID_REQUEUE
, 1, 0, "link halted" },
102 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_GENERAL
, DID_OK
, 1, 0, "general transport error" },
104 { IBMVFC_VIOS_FAILURE
, IBMVFC_CRQ_FAILURE
, DID_REQUEUE
, 1, 1, "CRQ failure" },
105 { IBMVFC_VIOS_FAILURE
, IBMVFC_SW_FAILURE
, DID_ERROR
, 0, 1, "software failure" },
106 { IBMVFC_VIOS_FAILURE
, IBMVFC_INVALID_PARAMETER
, DID_ERROR
, 0, 1, "invalid parameter" },
107 { IBMVFC_VIOS_FAILURE
, IBMVFC_MISSING_PARAMETER
, DID_ERROR
, 0, 1, "missing parameter" },
108 { IBMVFC_VIOS_FAILURE
, IBMVFC_HOST_IO_BUS
, DID_ERROR
, 1, 1, "host I/O bus failure" },
109 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED
, DID_ERROR
, 0, 1, "transaction cancelled" },
110 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED_IMPLICIT
, DID_ERROR
, 0, 1, "transaction cancelled implicit" },
111 { IBMVFC_VIOS_FAILURE
, IBMVFC_INSUFFICIENT_RESOURCE
, DID_REQUEUE
, 1, 1, "insufficient resources" },
112 { IBMVFC_VIOS_FAILURE
, IBMVFC_PLOGI_REQUIRED
, DID_ERROR
, 0, 1, "port login required" },
113 { IBMVFC_VIOS_FAILURE
, IBMVFC_COMMAND_FAILED
, DID_ERROR
, 1, 1, "command failed" },
115 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_ELS_CMD_CODE
, DID_ERROR
, 0, 1, "invalid ELS command code" },
116 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_VERSION
, DID_ERROR
, 0, 1, "invalid version level" },
117 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_ERROR
, DID_ERROR
, 1, 1, "logical error" },
118 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_CT_IU_SIZE
, DID_ERROR
, 0, 1, "invalid CT_IU size" },
119 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_BUSY
, DID_REQUEUE
, 1, 0, "logical busy" },
120 { IBMVFC_FC_FAILURE
, IBMVFC_PROTOCOL_ERROR
, DID_ERROR
, 1, 1, "protocol error" },
121 { IBMVFC_FC_FAILURE
, IBMVFC_UNABLE_TO_PERFORM_REQ
, DID_ERROR
, 1, 1, "unable to perform request" },
122 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_NOT_SUPPORTED
, DID_ERROR
, 0, 0, "command not supported" },
123 { IBMVFC_FC_FAILURE
, IBMVFC_SERVER_NOT_AVAIL
, DID_ERROR
, 0, 1, "server not available" },
124 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_IN_PROGRESS
, DID_ERROR
, 0, 1, "command already in progress" },
125 { IBMVFC_FC_FAILURE
, IBMVFC_VENDOR_SPECIFIC
, DID_ERROR
, 1, 1, "vendor specific" },
127 { IBMVFC_FC_SCSI_ERROR
, 0, DID_OK
, 1, 0, "SCSI error" },
128 { IBMVFC_FC_SCSI_ERROR
, IBMVFC_COMMAND_FAILED
, DID_ERROR
, 0, 1, "PRLI to device failed." },
131 static void ibmvfc_npiv_login(struct ibmvfc_host
*);
132 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*);
133 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*);
134 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*);
135 static void ibmvfc_npiv_logout(struct ibmvfc_host
*);
136 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target
*);
138 static const char *unknown_error
= "unknown error";
140 #ifdef CONFIG_SCSI_IBMVFC_TRACE
142 * ibmvfc_trc_start - Log a start trace entry
143 * @evt: ibmvfc event struct
146 static void ibmvfc_trc_start(struct ibmvfc_event
*evt
)
148 struct ibmvfc_host
*vhost
= evt
->vhost
;
149 struct ibmvfc_cmd
*vfc_cmd
= &evt
->iu
.cmd
;
150 struct ibmvfc_mad_common
*mad
= &evt
->iu
.mad_common
;
151 struct ibmvfc_trace_entry
*entry
;
153 entry
= &vhost
->trace
[vhost
->trace_index
++];
155 entry
->time
= jiffies
;
156 entry
->fmt
= evt
->crq
.format
;
157 entry
->type
= IBMVFC_TRC_START
;
159 switch (entry
->fmt
) {
160 case IBMVFC_CMD_FORMAT
:
161 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
162 entry
->scsi_id
= be64_to_cpu(vfc_cmd
->tgt_scsi_id
);
163 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
164 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
165 entry
->u
.start
.xfer_len
= be32_to_cpu(vfc_cmd
->iu
.xfer_len
);
167 case IBMVFC_MAD_FORMAT
:
168 entry
->op_code
= be32_to_cpu(mad
->opcode
);
176 * ibmvfc_trc_end - Log an end trace entry
177 * @evt: ibmvfc event struct
180 static void ibmvfc_trc_end(struct ibmvfc_event
*evt
)
182 struct ibmvfc_host
*vhost
= evt
->vhost
;
183 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
184 struct ibmvfc_mad_common
*mad
= &evt
->xfer_iu
->mad_common
;
185 struct ibmvfc_trace_entry
*entry
= &vhost
->trace
[vhost
->trace_index
++];
188 entry
->time
= jiffies
;
189 entry
->fmt
= evt
->crq
.format
;
190 entry
->type
= IBMVFC_TRC_END
;
192 switch (entry
->fmt
) {
193 case IBMVFC_CMD_FORMAT
:
194 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
195 entry
->scsi_id
= be64_to_cpu(vfc_cmd
->tgt_scsi_id
);
196 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
197 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
198 entry
->u
.end
.status
= be16_to_cpu(vfc_cmd
->status
);
199 entry
->u
.end
.error
= be16_to_cpu(vfc_cmd
->error
);
200 entry
->u
.end
.fcp_rsp_flags
= vfc_cmd
->rsp
.flags
;
201 entry
->u
.end
.rsp_code
= vfc_cmd
->rsp
.data
.info
.rsp_code
;
202 entry
->u
.end
.scsi_status
= vfc_cmd
->rsp
.scsi_status
;
204 case IBMVFC_MAD_FORMAT
:
205 entry
->op_code
= be32_to_cpu(mad
->opcode
);
206 entry
->u
.end
.status
= be16_to_cpu(mad
->status
);
215 #define ibmvfc_trc_start(evt) do { } while (0)
216 #define ibmvfc_trc_end(evt) do { } while (0)
220 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
221 * @status: status / error class
225 * index into cmd_status / -EINVAL on failure
227 static int ibmvfc_get_err_index(u16 status
, u16 error
)
231 for (i
= 0; i
< ARRAY_SIZE(cmd_status
); i
++)
232 if ((cmd_status
[i
].status
& status
) == cmd_status
[i
].status
&&
233 cmd_status
[i
].error
== error
)
240 * ibmvfc_get_cmd_error - Find the error description for the fcp response
241 * @status: status / error class
245 * error description string
247 static const char *ibmvfc_get_cmd_error(u16 status
, u16 error
)
249 int rc
= ibmvfc_get_err_index(status
, error
);
251 return cmd_status
[rc
].name
;
252 return unknown_error
;
256 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
257 * @vfc_cmd: ibmvfc command struct
260 * SCSI result value to return for completed command
262 static int ibmvfc_get_err_result(struct ibmvfc_cmd
*vfc_cmd
)
265 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
266 int fc_rsp_len
= be32_to_cpu(rsp
->fcp_rsp_len
);
268 if ((rsp
->flags
& FCP_RSP_LEN_VALID
) &&
269 ((fc_rsp_len
&& fc_rsp_len
!= 4 && fc_rsp_len
!= 8) ||
270 rsp
->data
.info
.rsp_code
))
271 return DID_ERROR
<< 16;
273 err
= ibmvfc_get_err_index(be16_to_cpu(vfc_cmd
->status
), be16_to_cpu(vfc_cmd
->error
));
275 return rsp
->scsi_status
| (cmd_status
[err
].result
<< 16);
276 return rsp
->scsi_status
| (DID_ERROR
<< 16);
280 * ibmvfc_retry_cmd - Determine if error status is retryable
281 * @status: status / error class
285 * 1 if error should be retried / 0 if it should not
287 static int ibmvfc_retry_cmd(u16 status
, u16 error
)
289 int rc
= ibmvfc_get_err_index(status
, error
);
292 return cmd_status
[rc
].retry
;
296 static const char *unknown_fc_explain
= "unknown fc explain";
298 static const struct {
302 { 0x00, "no additional explanation" },
303 { 0x01, "service parameter error - options" },
304 { 0x03, "service parameter error - initiator control" },
305 { 0x05, "service parameter error - recipient control" },
306 { 0x07, "service parameter error - received data field size" },
307 { 0x09, "service parameter error - concurrent seq" },
308 { 0x0B, "service parameter error - credit" },
309 { 0x0D, "invalid N_Port/F_Port_Name" },
310 { 0x0E, "invalid node/Fabric Name" },
311 { 0x0F, "invalid common service parameters" },
312 { 0x11, "invalid association header" },
313 { 0x13, "association header required" },
314 { 0x15, "invalid originator S_ID" },
315 { 0x17, "invalid OX_ID-RX-ID combination" },
316 { 0x19, "command (request) already in progress" },
317 { 0x1E, "N_Port Login requested" },
318 { 0x1F, "Invalid N_Port_ID" },
321 static const struct {
325 { 0x00, "no additional explanation" },
326 { 0x01, "port identifier not registered" },
327 { 0x02, "port name not registered" },
328 { 0x03, "node name not registered" },
329 { 0x04, "class of service not registered" },
330 { 0x06, "initial process associator not registered" },
331 { 0x07, "FC-4 TYPEs not registered" },
332 { 0x08, "symbolic port name not registered" },
333 { 0x09, "symbolic node name not registered" },
334 { 0x0A, "port type not registered" },
335 { 0xF0, "authorization exception" },
336 { 0xF1, "authentication exception" },
337 { 0xF2, "data base full" },
338 { 0xF3, "data base empty" },
339 { 0xF4, "processing request" },
340 { 0xF5, "unable to verify connection" },
341 { 0xF6, "devices not in a common zone" },
345 * ibmvfc_get_ls_explain - Return the FC Explain description text
346 * @status: FC Explain status
351 static const char *ibmvfc_get_ls_explain(u16 status
)
355 for (i
= 0; i
< ARRAY_SIZE(ls_explain
); i
++)
356 if (ls_explain
[i
].fc_explain
== status
)
357 return ls_explain
[i
].name
;
359 return unknown_fc_explain
;
363 * ibmvfc_get_gs_explain - Return the FC Explain description text
364 * @status: FC Explain status
369 static const char *ibmvfc_get_gs_explain(u16 status
)
373 for (i
= 0; i
< ARRAY_SIZE(gs_explain
); i
++)
374 if (gs_explain
[i
].fc_explain
== status
)
375 return gs_explain
[i
].name
;
377 return unknown_fc_explain
;
380 static const struct {
381 enum ibmvfc_fc_type fc_type
;
384 { IBMVFC_FABRIC_REJECT
, "fabric reject" },
385 { IBMVFC_PORT_REJECT
, "port reject" },
386 { IBMVFC_LS_REJECT
, "ELS reject" },
387 { IBMVFC_FABRIC_BUSY
, "fabric busy" },
388 { IBMVFC_PORT_BUSY
, "port busy" },
389 { IBMVFC_BASIC_REJECT
, "basic reject" },
392 static const char *unknown_fc_type
= "unknown fc type";
395 * ibmvfc_get_fc_type - Return the FC Type description text
396 * @status: FC Type error status
401 static const char *ibmvfc_get_fc_type(u16 status
)
405 for (i
= 0; i
< ARRAY_SIZE(fc_type
); i
++)
406 if (fc_type
[i
].fc_type
== status
)
407 return fc_type
[i
].name
;
409 return unknown_fc_type
;
413 * ibmvfc_set_tgt_action - Set the next init action for the target
414 * @tgt: ibmvfc target struct
415 * @action: action to perform
418 * 0 if action changed / non-zero if not changed
420 static int ibmvfc_set_tgt_action(struct ibmvfc_target
*tgt
,
421 enum ibmvfc_target_action action
)
425 switch (tgt
->action
) {
426 case IBMVFC_TGT_ACTION_LOGOUT_RPORT
:
427 if (action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT
||
428 action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
429 tgt
->action
= action
;
433 case IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT
:
434 if (action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
435 tgt
->action
= action
;
439 case IBMVFC_TGT_ACTION_DEL_RPORT
:
440 if (action
== IBMVFC_TGT_ACTION_DELETED_RPORT
) {
441 tgt
->action
= action
;
444 case IBMVFC_TGT_ACTION_DELETED_RPORT
:
447 if (action
>= IBMVFC_TGT_ACTION_LOGOUT_RPORT
)
449 tgt
->action
= action
;
458 * ibmvfc_set_host_state - Set the state for the host
459 * @vhost: ibmvfc host struct
460 * @state: state to set host to
463 * 0 if state changed / non-zero if not changed
465 static int ibmvfc_set_host_state(struct ibmvfc_host
*vhost
,
466 enum ibmvfc_host_state state
)
470 switch (vhost
->state
) {
471 case IBMVFC_HOST_OFFLINE
:
475 vhost
->state
= state
;
483 * ibmvfc_set_host_action - Set the next init action for the host
484 * @vhost: ibmvfc host struct
485 * @action: action to perform
488 static void ibmvfc_set_host_action(struct ibmvfc_host
*vhost
,
489 enum ibmvfc_host_action action
)
492 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
493 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
)
494 vhost
->action
= action
;
496 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
497 if (vhost
->action
== IBMVFC_HOST_ACTION_LOGO
)
498 vhost
->action
= action
;
500 case IBMVFC_HOST_ACTION_INIT_WAIT
:
501 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT
)
502 vhost
->action
= action
;
504 case IBMVFC_HOST_ACTION_QUERY
:
505 switch (vhost
->action
) {
506 case IBMVFC_HOST_ACTION_INIT_WAIT
:
507 case IBMVFC_HOST_ACTION_NONE
:
508 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
509 vhost
->action
= action
;
515 case IBMVFC_HOST_ACTION_TGT_INIT
:
516 if (vhost
->action
== IBMVFC_HOST_ACTION_ALLOC_TGTS
)
517 vhost
->action
= action
;
519 case IBMVFC_HOST_ACTION_INIT
:
520 case IBMVFC_HOST_ACTION_TGT_DEL
:
521 switch (vhost
->action
) {
522 case IBMVFC_HOST_ACTION_RESET
:
523 case IBMVFC_HOST_ACTION_REENABLE
:
526 vhost
->action
= action
;
530 case IBMVFC_HOST_ACTION_LOGO
:
531 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
532 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
533 case IBMVFC_HOST_ACTION_NONE
:
534 case IBMVFC_HOST_ACTION_RESET
:
535 case IBMVFC_HOST_ACTION_REENABLE
:
537 vhost
->action
= action
;
543 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
544 * @vhost: ibmvfc host struct
549 static void ibmvfc_reinit_host(struct ibmvfc_host
*vhost
)
551 if (vhost
->action
== IBMVFC_HOST_ACTION_NONE
) {
552 if (!ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
553 scsi_block_requests(vhost
->host
);
554 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
559 wake_up(&vhost
->work_wait_q
);
563 * ibmvfc_del_tgt - Schedule cleanup and removal of the target
564 * @tgt: ibmvfc target struct
565 * @job_step: job step to perform
568 static void ibmvfc_del_tgt(struct ibmvfc_target
*tgt
)
570 if (!ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_LOGOUT_RPORT
))
571 tgt
->job_step
= ibmvfc_tgt_implicit_logout_and_del
;
572 wake_up(&tgt
->vhost
->work_wait_q
);
576 * ibmvfc_link_down - Handle a link down event from the adapter
577 * @vhost: ibmvfc host struct
578 * @state: ibmvfc host state to enter
581 static void ibmvfc_link_down(struct ibmvfc_host
*vhost
,
582 enum ibmvfc_host_state state
)
584 struct ibmvfc_target
*tgt
;
587 scsi_block_requests(vhost
->host
);
588 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
590 ibmvfc_set_host_state(vhost
, state
);
591 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
592 vhost
->events_to_log
|= IBMVFC_AE_LINKDOWN
;
593 wake_up(&vhost
->work_wait_q
);
598 * ibmvfc_init_host - Start host initialization
599 * @vhost: ibmvfc host struct
604 static void ibmvfc_init_host(struct ibmvfc_host
*vhost
)
606 struct ibmvfc_target
*tgt
;
608 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
609 if (++vhost
->init_retries
> IBMVFC_MAX_HOST_INIT_RETRIES
) {
611 "Host initialization retries exceeded. Taking adapter offline\n");
612 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
617 if (!ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
618 memset(vhost
->async_crq
.msgs
, 0, PAGE_SIZE
);
619 vhost
->async_crq
.cur
= 0;
621 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
623 scsi_block_requests(vhost
->host
);
624 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
625 vhost
->job_step
= ibmvfc_npiv_login
;
626 wake_up(&vhost
->work_wait_q
);
631 * ibmvfc_send_crq - Send a CRQ
632 * @vhost: ibmvfc host struct
633 * @word1: the first 64 bits of the data
634 * @word2: the second 64 bits of the data
637 * 0 on success / other on failure
639 static int ibmvfc_send_crq(struct ibmvfc_host
*vhost
, u64 word1
, u64 word2
)
641 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
642 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, word1
, word2
);
646 * ibmvfc_send_crq_init - Send a CRQ init message
647 * @vhost: ibmvfc host struct
650 * 0 on success / other on failure
652 static int ibmvfc_send_crq_init(struct ibmvfc_host
*vhost
)
654 ibmvfc_dbg(vhost
, "Sending CRQ init\n");
655 return ibmvfc_send_crq(vhost
, 0xC001000000000000LL
, 0);
659 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
660 * @vhost: ibmvfc host struct
663 * 0 on success / other on failure
665 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host
*vhost
)
667 ibmvfc_dbg(vhost
, "Sending CRQ init complete\n");
668 return ibmvfc_send_crq(vhost
, 0xC002000000000000LL
, 0);
672 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
673 * @vhost: ibmvfc host struct
675 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
676 * the crq with the hypervisor.
678 static void ibmvfc_release_crq_queue(struct ibmvfc_host
*vhost
)
681 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
682 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
684 ibmvfc_dbg(vhost
, "Releasing CRQ\n");
685 free_irq(vdev
->irq
, vhost
);
686 tasklet_kill(&vhost
->tasklet
);
690 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
691 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
693 vhost
->state
= IBMVFC_NO_CRQ
;
694 vhost
->logged_in
= 0;
695 dma_unmap_single(vhost
->dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
696 free_page((unsigned long)crq
->msgs
);
700 * ibmvfc_reenable_crq_queue - reenables the CRQ
701 * @vhost: ibmvfc host struct
704 * 0 on success / other on failure
706 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host
*vhost
)
709 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
711 /* Re-enable the CRQ */
715 rc
= plpar_hcall_norets(H_ENABLE_CRQ
, vdev
->unit_address
);
716 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
719 dev_err(vhost
->dev
, "Error enabling adapter (rc=%d)\n", rc
);
725 * ibmvfc_reset_crq - resets a crq after a failure
726 * @vhost: ibmvfc host struct
729 * 0 on success / other on failure
731 static int ibmvfc_reset_crq(struct ibmvfc_host
*vhost
)
735 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
736 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
742 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
743 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
745 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
746 vhost
->state
= IBMVFC_NO_CRQ
;
747 vhost
->logged_in
= 0;
749 /* Clean out the queue */
750 memset(crq
->msgs
, 0, PAGE_SIZE
);
753 /* And re-open it again */
754 rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
755 crq
->msg_token
, PAGE_SIZE
);
758 /* Adapter is good, but other end is not ready */
759 dev_warn(vhost
->dev
, "Partner adapter not ready\n");
761 dev_warn(vhost
->dev
, "Couldn't register crq (rc=%d)\n", rc
);
762 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
768 * ibmvfc_valid_event - Determines if event is valid.
769 * @pool: event_pool that contains the event
770 * @evt: ibmvfc event to be checked for validity
773 * 1 if event is valid / 0 if event is not valid
775 static int ibmvfc_valid_event(struct ibmvfc_event_pool
*pool
,
776 struct ibmvfc_event
*evt
)
778 int index
= evt
- pool
->events
;
779 if (index
< 0 || index
>= pool
->size
) /* outside of bounds */
781 if (evt
!= pool
->events
+ index
) /* unaligned */
787 * ibmvfc_free_event - Free the specified event
788 * @evt: ibmvfc_event to be freed
791 static void ibmvfc_free_event(struct ibmvfc_event
*evt
)
793 struct ibmvfc_host
*vhost
= evt
->vhost
;
794 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
796 BUG_ON(!ibmvfc_valid_event(pool
, evt
));
797 BUG_ON(atomic_inc_return(&evt
->free
) != 1);
798 list_add_tail(&evt
->queue
, &vhost
->free
);
802 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
803 * @evt: ibmvfc event struct
805 * This function does not setup any error status, that must be done
806 * before this function gets called.
808 static void ibmvfc_scsi_eh_done(struct ibmvfc_event
*evt
)
810 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
813 scsi_dma_unmap(cmnd
);
814 cmnd
->scsi_done(cmnd
);
818 complete(evt
->eh_comp
);
820 ibmvfc_free_event(evt
);
824 * ibmvfc_fail_request - Fail request with specified error code
825 * @evt: ibmvfc event struct
826 * @error_code: error code to fail request with
831 static void ibmvfc_fail_request(struct ibmvfc_event
*evt
, int error_code
)
834 evt
->cmnd
->result
= (error_code
<< 16);
835 evt
->done
= ibmvfc_scsi_eh_done
;
837 evt
->xfer_iu
->mad_common
.status
= cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED
);
839 list_del(&evt
->queue
);
840 del_timer(&evt
->timer
);
846 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
847 * @vhost: ibmvfc host struct
848 * @error_code: error code to fail requests with
853 static void ibmvfc_purge_requests(struct ibmvfc_host
*vhost
, int error_code
)
855 struct ibmvfc_event
*evt
, *pos
;
857 ibmvfc_dbg(vhost
, "Purging all requests\n");
858 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
)
859 ibmvfc_fail_request(evt
, error_code
);
863 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
864 * @vhost: struct ibmvfc host to reset
866 static void ibmvfc_hard_reset_host(struct ibmvfc_host
*vhost
)
868 ibmvfc_purge_requests(vhost
, DID_ERROR
);
869 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
870 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_RESET
);
874 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
875 * @vhost: struct ibmvfc host to reset
877 static void __ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
879 if (vhost
->logged_in
&& vhost
->action
!= IBMVFC_HOST_ACTION_LOGO_WAIT
&&
880 !ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
881 scsi_block_requests(vhost
->host
);
882 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_LOGO
);
883 vhost
->job_step
= ibmvfc_npiv_logout
;
884 wake_up(&vhost
->work_wait_q
);
886 ibmvfc_hard_reset_host(vhost
);
890 * ibmvfc_reset_host - Reset the connection to the server
891 * @vhost: ibmvfc host struct
893 static void ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
897 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
898 __ibmvfc_reset_host(vhost
);
899 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
903 * ibmvfc_retry_host_init - Retry host initialization if allowed
904 * @vhost: ibmvfc host struct
906 * Returns: 1 if init will be retried / 0 if not
909 static int ibmvfc_retry_host_init(struct ibmvfc_host
*vhost
)
913 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
914 vhost
->delay_init
= 1;
915 if (++vhost
->init_retries
> IBMVFC_MAX_HOST_INIT_RETRIES
) {
917 "Host initialization retries exceeded. Taking adapter offline\n");
918 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
919 } else if (vhost
->init_retries
== IBMVFC_MAX_HOST_INIT_RETRIES
)
920 __ibmvfc_reset_host(vhost
);
922 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
927 wake_up(&vhost
->work_wait_q
);
932 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
933 * @starget: scsi target struct
936 * ibmvfc_target struct / NULL if not found
938 static struct ibmvfc_target
*__ibmvfc_get_target(struct scsi_target
*starget
)
940 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
941 struct ibmvfc_host
*vhost
= shost_priv(shost
);
942 struct ibmvfc_target
*tgt
;
944 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
945 if (tgt
->target_id
== starget
->id
) {
946 kref_get(&tgt
->kref
);
953 * ibmvfc_get_target - Find the specified scsi_target
954 * @starget: scsi target struct
957 * ibmvfc_target struct / NULL if not found
959 static struct ibmvfc_target
*ibmvfc_get_target(struct scsi_target
*starget
)
961 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
962 struct ibmvfc_target
*tgt
;
965 spin_lock_irqsave(shost
->host_lock
, flags
);
966 tgt
= __ibmvfc_get_target(starget
);
967 spin_unlock_irqrestore(shost
->host_lock
, flags
);
972 * ibmvfc_get_host_speed - Get host port speed
973 * @shost: scsi host struct
978 static void ibmvfc_get_host_speed(struct Scsi_Host
*shost
)
980 struct ibmvfc_host
*vhost
= shost_priv(shost
);
983 spin_lock_irqsave(shost
->host_lock
, flags
);
984 if (vhost
->state
== IBMVFC_ACTIVE
) {
985 switch (be64_to_cpu(vhost
->login_buf
->resp
.link_speed
) / 100) {
987 fc_host_speed(shost
) = FC_PORTSPEED_1GBIT
;
990 fc_host_speed(shost
) = FC_PORTSPEED_2GBIT
;
993 fc_host_speed(shost
) = FC_PORTSPEED_4GBIT
;
996 fc_host_speed(shost
) = FC_PORTSPEED_8GBIT
;
999 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
1002 fc_host_speed(shost
) = FC_PORTSPEED_16GBIT
;
1005 ibmvfc_log(vhost
, 3, "Unknown port speed: %lld Gbit\n",
1006 be64_to_cpu(vhost
->login_buf
->resp
.link_speed
) / 100);
1007 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
1011 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
1012 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1016 * ibmvfc_get_host_port_state - Get host port state
1017 * @shost: scsi host struct
1022 static void ibmvfc_get_host_port_state(struct Scsi_Host
*shost
)
1024 struct ibmvfc_host
*vhost
= shost_priv(shost
);
1025 unsigned long flags
;
1027 spin_lock_irqsave(shost
->host_lock
, flags
);
1028 switch (vhost
->state
) {
1029 case IBMVFC_INITIALIZING
:
1031 fc_host_port_state(shost
) = FC_PORTSTATE_ONLINE
;
1033 case IBMVFC_LINK_DOWN
:
1034 fc_host_port_state(shost
) = FC_PORTSTATE_LINKDOWN
;
1036 case IBMVFC_LINK_DEAD
:
1037 case IBMVFC_HOST_OFFLINE
:
1038 fc_host_port_state(shost
) = FC_PORTSTATE_OFFLINE
;
1041 fc_host_port_state(shost
) = FC_PORTSTATE_BLOCKED
;
1044 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
1047 ibmvfc_log(vhost
, 3, "Unknown port state: %d\n", vhost
->state
);
1048 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
1051 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1055 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1056 * @rport: rport struct
1057 * @timeout: timeout value
1062 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
1065 rport
->dev_loss_tmo
= timeout
;
1067 rport
->dev_loss_tmo
= 1;
1071 * ibmvfc_release_tgt - Free memory allocated for a target
1072 * @kref: kref struct
1075 static void ibmvfc_release_tgt(struct kref
*kref
)
1077 struct ibmvfc_target
*tgt
= container_of(kref
, struct ibmvfc_target
, kref
);
1082 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1083 * @starget: scsi target struct
1088 static void ibmvfc_get_starget_node_name(struct scsi_target
*starget
)
1090 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1091 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.node_name
: 0;
1093 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1097 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1098 * @starget: scsi target struct
1103 static void ibmvfc_get_starget_port_name(struct scsi_target
*starget
)
1105 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1106 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.port_name
: 0;
1108 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1112 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1113 * @starget: scsi target struct
1118 static void ibmvfc_get_starget_port_id(struct scsi_target
*starget
)
1120 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1121 fc_starget_port_id(starget
) = tgt
? tgt
->scsi_id
: -1;
1123 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1127 * ibmvfc_wait_while_resetting - Wait while the host resets
1128 * @vhost: ibmvfc host struct
1131 * 0 on success / other on failure
1133 static int ibmvfc_wait_while_resetting(struct ibmvfc_host
*vhost
)
1135 long timeout
= wait_event_timeout(vhost
->init_wait_q
,
1136 ((vhost
->state
== IBMVFC_ACTIVE
||
1137 vhost
->state
== IBMVFC_HOST_OFFLINE
||
1138 vhost
->state
== IBMVFC_LINK_DEAD
) &&
1139 vhost
->action
== IBMVFC_HOST_ACTION_NONE
),
1140 (init_timeout
* HZ
));
1142 return timeout
? 0 : -EIO
;
1146 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1147 * @shost: scsi host struct
1150 * 0 on success / other on failure
1152 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host
*shost
)
1154 struct ibmvfc_host
*vhost
= shost_priv(shost
);
1156 dev_err(vhost
->dev
, "Initiating host LIP. Resetting connection\n");
1157 ibmvfc_reset_host(vhost
);
1158 return ibmvfc_wait_while_resetting(vhost
);
1162 * ibmvfc_gather_partition_info - Gather info about the LPAR
1167 static void ibmvfc_gather_partition_info(struct ibmvfc_host
*vhost
)
1169 struct device_node
*rootdn
;
1171 const unsigned int *num
;
1173 rootdn
= of_find_node_by_path("/");
1177 name
= of_get_property(rootdn
, "ibm,partition-name", NULL
);
1179 strncpy(vhost
->partition_name
, name
, sizeof(vhost
->partition_name
));
1180 num
= of_get_property(rootdn
, "ibm,partition-no", NULL
);
1182 vhost
->partition_number
= *num
;
1183 of_node_put(rootdn
);
1187 * ibmvfc_set_login_info - Setup info for NPIV login
1188 * @vhost: ibmvfc host struct
1193 static void ibmvfc_set_login_info(struct ibmvfc_host
*vhost
)
1195 struct ibmvfc_npiv_login
*login_info
= &vhost
->login_info
;
1196 struct device_node
*of_node
= vhost
->dev
->of_node
;
1197 const char *location
;
1199 memset(login_info
, 0, sizeof(*login_info
));
1201 login_info
->ostype
= cpu_to_be32(IBMVFC_OS_LINUX
);
1202 login_info
->max_dma_len
= cpu_to_be64(IBMVFC_MAX_SECTORS
<< 9);
1203 login_info
->max_payload
= cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu
));
1204 login_info
->max_response
= cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp
));
1205 login_info
->partition_num
= cpu_to_be32(vhost
->partition_number
);
1206 login_info
->vfc_frame_version
= cpu_to_be32(1);
1207 login_info
->fcp_version
= cpu_to_be16(3);
1208 login_info
->flags
= cpu_to_be16(IBMVFC_FLUSH_ON_HALT
);
1209 if (vhost
->client_migrated
)
1210 login_info
->flags
|= cpu_to_be16(IBMVFC_CLIENT_MIGRATED
);
1212 login_info
->max_cmds
= cpu_to_be32(max_requests
+ IBMVFC_NUM_INTERNAL_REQ
);
1213 login_info
->capabilities
= cpu_to_be64(IBMVFC_CAN_MIGRATE
);
1214 login_info
->async
.va
= cpu_to_be64(vhost
->async_crq
.msg_token
);
1215 login_info
->async
.len
= cpu_to_be32(vhost
->async_crq
.size
* sizeof(*vhost
->async_crq
.msgs
));
1216 strncpy(login_info
->partition_name
, vhost
->partition_name
, IBMVFC_MAX_NAME
);
1217 strncpy(login_info
->device_name
,
1218 dev_name(&vhost
->host
->shost_gendev
), IBMVFC_MAX_NAME
);
1220 location
= of_get_property(of_node
, "ibm,loc-code", NULL
);
1221 location
= location
? location
: dev_name(vhost
->dev
);
1222 strncpy(login_info
->drc_name
, location
, IBMVFC_MAX_NAME
);
1226 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1227 * @vhost: ibmvfc host who owns the event pool
1229 * Returns zero on success.
1231 static int ibmvfc_init_event_pool(struct ibmvfc_host
*vhost
)
1234 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1237 pool
->size
= max_requests
+ IBMVFC_NUM_INTERNAL_REQ
;
1238 pool
->events
= kcalloc(pool
->size
, sizeof(*pool
->events
), GFP_KERNEL
);
1242 pool
->iu_storage
= dma_alloc_coherent(vhost
->dev
,
1243 pool
->size
* sizeof(*pool
->iu_storage
),
1244 &pool
->iu_token
, 0);
1246 if (!pool
->iu_storage
) {
1247 kfree(pool
->events
);
1251 for (i
= 0; i
< pool
->size
; ++i
) {
1252 struct ibmvfc_event
*evt
= &pool
->events
[i
];
1253 atomic_set(&evt
->free
, 1);
1254 evt
->crq
.valid
= 0x80;
1255 evt
->crq
.ioba
= cpu_to_be64(pool
->iu_token
+ (sizeof(*evt
->xfer_iu
) * i
));
1256 evt
->xfer_iu
= pool
->iu_storage
+ i
;
1258 evt
->ext_list
= NULL
;
1259 list_add_tail(&evt
->queue
, &vhost
->free
);
1267 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1268 * @vhost: ibmvfc host who owns the event pool
1271 static void ibmvfc_free_event_pool(struct ibmvfc_host
*vhost
)
1274 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1277 for (i
= 0; i
< pool
->size
; ++i
) {
1278 list_del(&pool
->events
[i
].queue
);
1279 BUG_ON(atomic_read(&pool
->events
[i
].free
) != 1);
1280 if (pool
->events
[i
].ext_list
)
1281 dma_pool_free(vhost
->sg_pool
,
1282 pool
->events
[i
].ext_list
,
1283 pool
->events
[i
].ext_list_token
);
1286 kfree(pool
->events
);
1287 dma_free_coherent(vhost
->dev
,
1288 pool
->size
* sizeof(*pool
->iu_storage
),
1289 pool
->iu_storage
, pool
->iu_token
);
1294 * ibmvfc_get_event - Gets the next free event in pool
1295 * @vhost: ibmvfc host struct
1297 * Returns a free event from the pool.
1299 static struct ibmvfc_event
*ibmvfc_get_event(struct ibmvfc_host
*vhost
)
1301 struct ibmvfc_event
*evt
;
1303 BUG_ON(list_empty(&vhost
->free
));
1304 evt
= list_entry(vhost
->free
.next
, struct ibmvfc_event
, queue
);
1305 atomic_set(&evt
->free
, 0);
1306 list_del(&evt
->queue
);
1311 * ibmvfc_init_event - Initialize fields in an event struct that are always
1314 * @done: Routine to call when the event is responded to
1315 * @format: SRP or MAD format
1317 static void ibmvfc_init_event(struct ibmvfc_event
*evt
,
1318 void (*done
) (struct ibmvfc_event
*), u8 format
)
1321 evt
->sync_iu
= NULL
;
1322 evt
->crq
.format
= format
;
1324 evt
->eh_comp
= NULL
;
1328 * ibmvfc_map_sg_list - Initialize scatterlist
1329 * @scmd: scsi command struct
1330 * @nseg: number of scatterlist segments
1331 * @md: memory descriptor list to initialize
1333 static void ibmvfc_map_sg_list(struct scsi_cmnd
*scmd
, int nseg
,
1334 struct srp_direct_buf
*md
)
1337 struct scatterlist
*sg
;
1339 scsi_for_each_sg(scmd
, sg
, nseg
, i
) {
1340 md
[i
].va
= cpu_to_be64(sg_dma_address(sg
));
1341 md
[i
].len
= cpu_to_be32(sg_dma_len(sg
));
1347 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1348 * @scmd: struct scsi_cmnd with the scatterlist
1349 * @evt: ibmvfc event struct
1350 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1351 * @dev: device for which to map dma memory
1354 * 0 on success / non-zero on failure
1356 static int ibmvfc_map_sg_data(struct scsi_cmnd
*scmd
,
1357 struct ibmvfc_event
*evt
,
1358 struct ibmvfc_cmd
*vfc_cmd
, struct device
*dev
)
1362 struct srp_direct_buf
*data
= &vfc_cmd
->ioba
;
1363 struct ibmvfc_host
*vhost
= dev_get_drvdata(dev
);
1366 vfc_cmd
->flags
|= cpu_to_be16(IBMVFC_CLASS_3_ERR
);
1368 sg_mapped
= scsi_dma_map(scmd
);
1370 vfc_cmd
->flags
|= cpu_to_be16(IBMVFC_NO_MEM_DESC
);
1372 } else if (unlikely(sg_mapped
< 0)) {
1373 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1374 scmd_printk(KERN_ERR
, scmd
, "Failed to map DMA buffer for command\n");
1378 if (scmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1379 vfc_cmd
->flags
|= cpu_to_be16(IBMVFC_WRITE
);
1380 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_WRDATA
;
1382 vfc_cmd
->flags
|= cpu_to_be16(IBMVFC_READ
);
1383 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_RDDATA
;
1386 if (sg_mapped
== 1) {
1387 ibmvfc_map_sg_list(scmd
, sg_mapped
, data
);
1391 vfc_cmd
->flags
|= cpu_to_be16(IBMVFC_SCATTERLIST
);
1393 if (!evt
->ext_list
) {
1394 evt
->ext_list
= dma_pool_alloc(vhost
->sg_pool
, GFP_ATOMIC
,
1395 &evt
->ext_list_token
);
1397 if (!evt
->ext_list
) {
1398 scsi_dma_unmap(scmd
);
1399 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1400 scmd_printk(KERN_ERR
, scmd
, "Can't allocate memory for scatterlist\n");
1405 ibmvfc_map_sg_list(scmd
, sg_mapped
, evt
->ext_list
);
1407 data
->va
= cpu_to_be64(evt
->ext_list_token
);
1408 data
->len
= cpu_to_be32(sg_mapped
* sizeof(struct srp_direct_buf
));
1414 * ibmvfc_timeout - Internal command timeout handler
1415 * @evt: struct ibmvfc_event that timed out
1417 * Called when an internally generated command times out
1419 static void ibmvfc_timeout(struct timer_list
*t
)
1421 struct ibmvfc_event
*evt
= from_timer(evt
, t
, timer
);
1422 struct ibmvfc_host
*vhost
= evt
->vhost
;
1423 dev_err(vhost
->dev
, "Command timed out (%p). Resetting connection\n", evt
);
1424 ibmvfc_reset_host(vhost
);
1428 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1429 * @evt: event to be sent
1430 * @vhost: ibmvfc host struct
1431 * @timeout: timeout in seconds - 0 means do not time command
1433 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1435 static int ibmvfc_send_event(struct ibmvfc_event
*evt
,
1436 struct ibmvfc_host
*vhost
, unsigned long timeout
)
1438 __be64
*crq_as_u64
= (__be64
*) &evt
->crq
;
1441 /* Copy the IU into the transfer area */
1442 *evt
->xfer_iu
= evt
->iu
;
1443 if (evt
->crq
.format
== IBMVFC_CMD_FORMAT
)
1444 evt
->xfer_iu
->cmd
.tag
= cpu_to_be64((u64
)evt
);
1445 else if (evt
->crq
.format
== IBMVFC_MAD_FORMAT
)
1446 evt
->xfer_iu
->mad_common
.tag
= cpu_to_be64((u64
)evt
);
1450 list_add_tail(&evt
->queue
, &vhost
->sent
);
1451 timer_setup(&evt
->timer
, ibmvfc_timeout
, 0);
1454 evt
->timer
.expires
= jiffies
+ (timeout
* HZ
);
1455 add_timer(&evt
->timer
);
1460 if ((rc
= ibmvfc_send_crq(vhost
, be64_to_cpu(crq_as_u64
[0]),
1461 be64_to_cpu(crq_as_u64
[1])))) {
1462 list_del(&evt
->queue
);
1463 del_timer(&evt
->timer
);
1465 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1466 * Firmware will send a CRQ with a transport event (0xFF) to
1467 * tell this client what has happened to the transport. This
1468 * will be handled in ibmvfc_handle_crq()
1470 if (rc
== H_CLOSED
) {
1471 if (printk_ratelimit())
1472 dev_warn(vhost
->dev
, "Send warning. Receive queue closed, will retry.\n");
1474 scsi_dma_unmap(evt
->cmnd
);
1475 ibmvfc_free_event(evt
);
1476 return SCSI_MLQUEUE_HOST_BUSY
;
1479 dev_err(vhost
->dev
, "Send error (rc=%d)\n", rc
);
1481 evt
->cmnd
->result
= DID_ERROR
<< 16;
1482 evt
->done
= ibmvfc_scsi_eh_done
;
1484 evt
->xfer_iu
->mad_common
.status
= cpu_to_be16(IBMVFC_MAD_CRQ_ERROR
);
1488 ibmvfc_trc_start(evt
);
1494 * ibmvfc_log_error - Log an error for the failed command if appropriate
1495 * @evt: ibmvfc event to log
1498 static void ibmvfc_log_error(struct ibmvfc_event
*evt
)
1500 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1501 struct ibmvfc_host
*vhost
= evt
->vhost
;
1502 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1503 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1504 const char *err
= unknown_error
;
1505 int index
= ibmvfc_get_err_index(be16_to_cpu(vfc_cmd
->status
), be16_to_cpu(vfc_cmd
->error
));
1510 logerr
= cmd_status
[index
].log
;
1511 err
= cmd_status
[index
].name
;
1514 if (!logerr
&& (vhost
->log_level
<= (IBMVFC_DEFAULT_LOG_LEVEL
+ 1)))
1517 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1518 rsp_code
= rsp
->data
.info
.rsp_code
;
1520 scmd_printk(KERN_ERR
, cmnd
, "Command (%02X) : %s (%x:%x) "
1521 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1522 cmnd
->cmnd
[0], err
, be16_to_cpu(vfc_cmd
->status
), be16_to_cpu(vfc_cmd
->error
),
1523 rsp
->flags
, rsp_code
, scsi_get_resid(cmnd
), rsp
->scsi_status
);
1527 * ibmvfc_relogin - Log back into the specified device
1528 * @sdev: scsi device struct
1531 static void ibmvfc_relogin(struct scsi_device
*sdev
)
1533 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1534 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1535 struct ibmvfc_target
*tgt
;
1537 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
1538 if (rport
== tgt
->rport
) {
1539 ibmvfc_del_tgt(tgt
);
1544 ibmvfc_reinit_host(vhost
);
1548 * ibmvfc_scsi_done - Handle responses from commands
1549 * @evt: ibmvfc event to be handled
1551 * Used as a callback when sending scsi cmds.
1553 static void ibmvfc_scsi_done(struct ibmvfc_event
*evt
)
1555 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1556 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1557 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1559 u32 sense_len
= be32_to_cpu(rsp
->fcp_sense_len
);
1562 if (be16_to_cpu(vfc_cmd
->response_flags
) & IBMVFC_ADAPTER_RESID_VALID
)
1563 scsi_set_resid(cmnd
, be32_to_cpu(vfc_cmd
->adapter_resid
));
1564 else if (rsp
->flags
& FCP_RESID_UNDER
)
1565 scsi_set_resid(cmnd
, be32_to_cpu(rsp
->fcp_resid
));
1567 scsi_set_resid(cmnd
, 0);
1569 if (vfc_cmd
->status
) {
1570 cmnd
->result
= ibmvfc_get_err_result(vfc_cmd
);
1572 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1573 rsp_len
= be32_to_cpu(rsp
->fcp_rsp_len
);
1574 if ((sense_len
+ rsp_len
) > SCSI_SENSE_BUFFERSIZE
)
1575 sense_len
= SCSI_SENSE_BUFFERSIZE
- rsp_len
;
1576 if ((rsp
->flags
& FCP_SNS_LEN_VALID
) && rsp
->fcp_sense_len
&& rsp_len
<= 8)
1577 memcpy(cmnd
->sense_buffer
, rsp
->data
.sense
+ rsp_len
, sense_len
);
1578 if ((be16_to_cpu(vfc_cmd
->status
) & IBMVFC_VIOS_FAILURE
) &&
1579 (be16_to_cpu(vfc_cmd
->error
) == IBMVFC_PLOGI_REQUIRED
))
1580 ibmvfc_relogin(cmnd
->device
);
1582 if (!cmnd
->result
&& (!scsi_get_resid(cmnd
) || (rsp
->flags
& FCP_RESID_OVER
)))
1583 cmnd
->result
= (DID_ERROR
<< 16);
1585 ibmvfc_log_error(evt
);
1588 if (!cmnd
->result
&&
1589 (scsi_bufflen(cmnd
) - scsi_get_resid(cmnd
) < cmnd
->underflow
))
1590 cmnd
->result
= (DID_ERROR
<< 16);
1592 scsi_dma_unmap(cmnd
);
1593 cmnd
->scsi_done(cmnd
);
1597 complete(evt
->eh_comp
);
1599 ibmvfc_free_event(evt
);
1603 * ibmvfc_host_chkready - Check if the host can accept commands
1604 * @vhost: struct ibmvfc host
1607 * 1 if host can accept command / 0 if not
1609 static inline int ibmvfc_host_chkready(struct ibmvfc_host
*vhost
)
1613 switch (vhost
->state
) {
1614 case IBMVFC_LINK_DEAD
:
1615 case IBMVFC_HOST_OFFLINE
:
1616 result
= DID_NO_CONNECT
<< 16;
1619 case IBMVFC_INITIALIZING
:
1621 case IBMVFC_LINK_DOWN
:
1622 result
= DID_REQUEUE
<< 16;
1633 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1634 * @cmnd: struct scsi_cmnd to be executed
1635 * @done: Callback function to be called when cmnd is completed
1638 * 0 on success / other on failure
1640 static int ibmvfc_queuecommand_lck(struct scsi_cmnd
*cmnd
,
1641 void (*done
) (struct scsi_cmnd
*))
1643 struct ibmvfc_host
*vhost
= shost_priv(cmnd
->device
->host
);
1644 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
1645 struct ibmvfc_cmd
*vfc_cmd
;
1646 struct ibmvfc_event
*evt
;
1649 if (unlikely((rc
= fc_remote_port_chkready(rport
))) ||
1650 unlikely((rc
= ibmvfc_host_chkready(vhost
)))) {
1656 cmnd
->result
= (DID_OK
<< 16);
1657 evt
= ibmvfc_get_event(vhost
);
1658 ibmvfc_init_event(evt
, ibmvfc_scsi_done
, IBMVFC_CMD_FORMAT
);
1660 cmnd
->scsi_done
= done
;
1661 vfc_cmd
= &evt
->iu
.cmd
;
1662 memset(vfc_cmd
, 0, sizeof(*vfc_cmd
));
1663 vfc_cmd
->resp
.va
= cpu_to_be64(be64_to_cpu(evt
->crq
.ioba
) + offsetof(struct ibmvfc_cmd
, rsp
));
1664 vfc_cmd
->resp
.len
= cpu_to_be32(sizeof(vfc_cmd
->rsp
));
1665 vfc_cmd
->frame_type
= cpu_to_be32(IBMVFC_SCSI_FCP_TYPE
);
1666 vfc_cmd
->payload_len
= cpu_to_be32(sizeof(vfc_cmd
->iu
));
1667 vfc_cmd
->resp_len
= cpu_to_be32(sizeof(vfc_cmd
->rsp
));
1668 vfc_cmd
->cancel_key
= cpu_to_be32((unsigned long)cmnd
->device
->hostdata
);
1669 vfc_cmd
->tgt_scsi_id
= cpu_to_be64(rport
->port_id
);
1670 vfc_cmd
->iu
.xfer_len
= cpu_to_be32(scsi_bufflen(cmnd
));
1671 int_to_scsilun(cmnd
->device
->lun
, &vfc_cmd
->iu
.lun
);
1672 memcpy(vfc_cmd
->iu
.cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
1674 if (cmnd
->flags
& SCMD_TAGGED
) {
1675 vfc_cmd
->task_tag
= cpu_to_be64(cmnd
->tag
);
1676 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_SIMPLE_TASK
;
1679 if (likely(!(rc
= ibmvfc_map_sg_data(cmnd
, evt
, vfc_cmd
, vhost
->dev
))))
1680 return ibmvfc_send_event(evt
, vhost
, 0);
1682 ibmvfc_free_event(evt
);
1684 return SCSI_MLQUEUE_HOST_BUSY
;
1686 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1687 scmd_printk(KERN_ERR
, cmnd
,
1688 "Failed to map DMA buffer for command. rc=%d\n", rc
);
1690 cmnd
->result
= DID_ERROR
<< 16;
1695 static DEF_SCSI_QCMD(ibmvfc_queuecommand
)
1698 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1699 * @evt: ibmvfc event struct
1702 static void ibmvfc_sync_completion(struct ibmvfc_event
*evt
)
1704 /* copy the response back */
1706 *evt
->sync_iu
= *evt
->xfer_iu
;
1708 complete(&evt
->comp
);
1712 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1713 * @evt: struct ibmvfc_event
1716 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event
*evt
)
1718 struct ibmvfc_host
*vhost
= evt
->vhost
;
1720 ibmvfc_free_event(evt
);
1721 vhost
->aborting_passthru
= 0;
1722 dev_info(vhost
->dev
, "Passthru command cancelled\n");
1726 * ibmvfc_bsg_timeout - Handle a BSG timeout
1727 * @job: struct bsg_job that timed out
1730 * 0 on success / other on failure
1732 static int ibmvfc_bsg_timeout(struct bsg_job
*job
)
1734 struct ibmvfc_host
*vhost
= shost_priv(fc_bsg_to_shost(job
));
1735 unsigned long port_id
= (unsigned long)job
->dd_data
;
1736 struct ibmvfc_event
*evt
;
1737 struct ibmvfc_tmf
*tmf
;
1738 unsigned long flags
;
1742 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1743 if (vhost
->aborting_passthru
|| vhost
->state
!= IBMVFC_ACTIVE
) {
1744 __ibmvfc_reset_host(vhost
);
1745 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1749 vhost
->aborting_passthru
= 1;
1750 evt
= ibmvfc_get_event(vhost
);
1751 ibmvfc_init_event(evt
, ibmvfc_bsg_timeout_done
, IBMVFC_MAD_FORMAT
);
1754 memset(tmf
, 0, sizeof(*tmf
));
1755 tmf
->common
.version
= cpu_to_be32(1);
1756 tmf
->common
.opcode
= cpu_to_be32(IBMVFC_TMF_MAD
);
1757 tmf
->common
.length
= cpu_to_be16(sizeof(*tmf
));
1758 tmf
->scsi_id
= cpu_to_be64(port_id
);
1759 tmf
->cancel_key
= cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY
);
1760 tmf
->my_cancel_key
= cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY
);
1761 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1764 vhost
->aborting_passthru
= 0;
1765 dev_err(vhost
->dev
, "Failed to send cancel event. rc=%d\n", rc
);
1768 dev_info(vhost
->dev
, "Cancelling passthru command to port id 0x%lx\n",
1771 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1778 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1779 * @vhost: struct ibmvfc_host to send command
1780 * @port_id: port ID to send command
1783 * 0 on success / other on failure
1785 static int ibmvfc_bsg_plogi(struct ibmvfc_host
*vhost
, unsigned int port_id
)
1787 struct ibmvfc_port_login
*plogi
;
1788 struct ibmvfc_target
*tgt
;
1789 struct ibmvfc_event
*evt
;
1790 union ibmvfc_iu rsp_iu
;
1791 unsigned long flags
;
1792 int rc
= 0, issue_login
= 1;
1795 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1796 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
1797 if (tgt
->scsi_id
== port_id
) {
1805 if (unlikely((rc
= ibmvfc_host_chkready(vhost
))))
1808 evt
= ibmvfc_get_event(vhost
);
1809 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
1810 plogi
= &evt
->iu
.plogi
;
1811 memset(plogi
, 0, sizeof(*plogi
));
1812 plogi
->common
.version
= cpu_to_be32(1);
1813 plogi
->common
.opcode
= cpu_to_be32(IBMVFC_PORT_LOGIN
);
1814 plogi
->common
.length
= cpu_to_be16(sizeof(*plogi
));
1815 plogi
->scsi_id
= cpu_to_be64(port_id
);
1816 evt
->sync_iu
= &rsp_iu
;
1817 init_completion(&evt
->comp
);
1819 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1820 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1825 wait_for_completion(&evt
->comp
);
1827 if (rsp_iu
.plogi
.common
.status
)
1830 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1831 ibmvfc_free_event(evt
);
1833 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1839 * ibmvfc_bsg_request - Handle a BSG request
1840 * @job: struct bsg_job to be executed
1843 * 0 on success / other on failure
1845 static int ibmvfc_bsg_request(struct bsg_job
*job
)
1847 struct ibmvfc_host
*vhost
= shost_priv(fc_bsg_to_shost(job
));
1848 struct fc_rport
*rport
= fc_bsg_to_rport(job
);
1849 struct ibmvfc_passthru_mad
*mad
;
1850 struct ibmvfc_event
*evt
;
1851 union ibmvfc_iu rsp_iu
;
1852 unsigned long flags
, port_id
= -1;
1853 struct fc_bsg_request
*bsg_request
= job
->request
;
1854 struct fc_bsg_reply
*bsg_reply
= job
->reply
;
1855 unsigned int code
= bsg_request
->msgcode
;
1856 int rc
= 0, req_seg
, rsp_seg
, issue_login
= 0;
1857 u32 fc_flags
, rsp_len
;
1860 bsg_reply
->reply_payload_rcv_len
= 0;
1862 port_id
= rport
->port_id
;
1865 case FC_BSG_HST_ELS_NOLOGIN
:
1866 port_id
= (bsg_request
->rqst_data
.h_els
.port_id
[0] << 16) |
1867 (bsg_request
->rqst_data
.h_els
.port_id
[1] << 8) |
1868 bsg_request
->rqst_data
.h_els
.port_id
[2];
1870 case FC_BSG_RPT_ELS
:
1871 fc_flags
= IBMVFC_FC_ELS
;
1875 port_id
= (bsg_request
->rqst_data
.h_ct
.port_id
[0] << 16) |
1876 (bsg_request
->rqst_data
.h_ct
.port_id
[1] << 8) |
1877 bsg_request
->rqst_data
.h_ct
.port_id
[2];
1880 fc_flags
= IBMVFC_FC_CT_IU
;
1888 if (!mutex_trylock(&vhost
->passthru_mutex
))
1891 job
->dd_data
= (void *)port_id
;
1892 req_seg
= dma_map_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1893 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1896 mutex_unlock(&vhost
->passthru_mutex
);
1900 rsp_seg
= dma_map_sg(vhost
->dev
, job
->reply_payload
.sg_list
,
1901 job
->reply_payload
.sg_cnt
, DMA_FROM_DEVICE
);
1904 dma_unmap_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1905 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1906 mutex_unlock(&vhost
->passthru_mutex
);
1910 if (req_seg
> 1 || rsp_seg
> 1) {
1916 rc
= ibmvfc_bsg_plogi(vhost
, port_id
);
1918 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1920 if (unlikely(rc
|| (rport
&& (rc
= fc_remote_port_chkready(rport
)))) ||
1921 unlikely((rc
= ibmvfc_host_chkready(vhost
)))) {
1922 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1926 evt
= ibmvfc_get_event(vhost
);
1927 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
1928 mad
= &evt
->iu
.passthru
;
1930 memset(mad
, 0, sizeof(*mad
));
1931 mad
->common
.version
= cpu_to_be32(1);
1932 mad
->common
.opcode
= cpu_to_be32(IBMVFC_PASSTHRU
);
1933 mad
->common
.length
= cpu_to_be16(sizeof(*mad
) - sizeof(mad
->fc_iu
) - sizeof(mad
->iu
));
1935 mad
->cmd_ioba
.va
= cpu_to_be64(be64_to_cpu(evt
->crq
.ioba
) +
1936 offsetof(struct ibmvfc_passthru_mad
, iu
));
1937 mad
->cmd_ioba
.len
= cpu_to_be32(sizeof(mad
->iu
));
1939 mad
->iu
.cmd_len
= cpu_to_be32(job
->request_payload
.payload_len
);
1940 mad
->iu
.rsp_len
= cpu_to_be32(job
->reply_payload
.payload_len
);
1941 mad
->iu
.flags
= cpu_to_be32(fc_flags
);
1942 mad
->iu
.cancel_key
= cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY
);
1944 mad
->iu
.cmd
.va
= cpu_to_be64(sg_dma_address(job
->request_payload
.sg_list
));
1945 mad
->iu
.cmd
.len
= cpu_to_be32(sg_dma_len(job
->request_payload
.sg_list
));
1946 mad
->iu
.rsp
.va
= cpu_to_be64(sg_dma_address(job
->reply_payload
.sg_list
));
1947 mad
->iu
.rsp
.len
= cpu_to_be32(sg_dma_len(job
->reply_payload
.sg_list
));
1948 mad
->iu
.scsi_id
= cpu_to_be64(port_id
);
1949 mad
->iu
.tag
= cpu_to_be64((u64
)evt
);
1950 rsp_len
= be32_to_cpu(mad
->iu
.rsp
.len
);
1952 evt
->sync_iu
= &rsp_iu
;
1953 init_completion(&evt
->comp
);
1954 rc
= ibmvfc_send_event(evt
, vhost
, 0);
1955 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1962 wait_for_completion(&evt
->comp
);
1964 if (rsp_iu
.passthru
.common
.status
)
1967 bsg_reply
->reply_payload_rcv_len
= rsp_len
;
1969 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1970 ibmvfc_free_event(evt
);
1971 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1972 bsg_reply
->result
= rc
;
1973 bsg_job_done(job
, bsg_reply
->result
,
1974 bsg_reply
->reply_payload_rcv_len
);
1977 dma_unmap_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1978 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1979 dma_unmap_sg(vhost
->dev
, job
->reply_payload
.sg_list
,
1980 job
->reply_payload
.sg_cnt
, DMA_FROM_DEVICE
);
1981 mutex_unlock(&vhost
->passthru_mutex
);
1987 * ibmvfc_reset_device - Reset the device with the specified reset type
1988 * @sdev: scsi device to reset
1990 * @desc: reset type description for log messages
1993 * 0 on success / other on failure
1995 static int ibmvfc_reset_device(struct scsi_device
*sdev
, int type
, char *desc
)
1997 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1998 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1999 struct ibmvfc_cmd
*tmf
;
2000 struct ibmvfc_event
*evt
= NULL
;
2001 union ibmvfc_iu rsp_iu
;
2002 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
2003 int rsp_rc
= -EBUSY
;
2004 unsigned long flags
;
2007 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2008 if (vhost
->state
== IBMVFC_ACTIVE
) {
2009 evt
= ibmvfc_get_event(vhost
);
2010 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
2013 memset(tmf
, 0, sizeof(*tmf
));
2014 tmf
->resp
.va
= cpu_to_be64(be64_to_cpu(evt
->crq
.ioba
) + offsetof(struct ibmvfc_cmd
, rsp
));
2015 tmf
->resp
.len
= cpu_to_be32(sizeof(tmf
->rsp
));
2016 tmf
->frame_type
= cpu_to_be32(IBMVFC_SCSI_FCP_TYPE
);
2017 tmf
->payload_len
= cpu_to_be32(sizeof(tmf
->iu
));
2018 tmf
->resp_len
= cpu_to_be32(sizeof(tmf
->rsp
));
2019 tmf
->cancel_key
= cpu_to_be32((unsigned long)sdev
->hostdata
);
2020 tmf
->tgt_scsi_id
= cpu_to_be64(rport
->port_id
);
2021 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
2022 tmf
->flags
= cpu_to_be16((IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
));
2023 tmf
->iu
.tmf_flags
= type
;
2024 evt
->sync_iu
= &rsp_iu
;
2026 init_completion(&evt
->comp
);
2027 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
2029 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2032 sdev_printk(KERN_ERR
, sdev
, "Failed to send %s reset event. rc=%d\n",
2037 sdev_printk(KERN_INFO
, sdev
, "Resetting %s\n", desc
);
2038 wait_for_completion(&evt
->comp
);
2040 if (rsp_iu
.cmd
.status
)
2041 rsp_code
= ibmvfc_get_err_result(&rsp_iu
.cmd
);
2044 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
2045 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
2047 sdev_printk(KERN_ERR
, sdev
, "%s reset failed: %s (%x:%x) "
2048 "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc
,
2049 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu
.cmd
.status
), be16_to_cpu(rsp_iu
.cmd
.error
)),
2050 be16_to_cpu(rsp_iu
.cmd
.status
), be16_to_cpu(rsp_iu
.cmd
.error
), fc_rsp
->flags
, rsp_code
,
2051 fc_rsp
->scsi_status
);
2054 sdev_printk(KERN_INFO
, sdev
, "%s reset successful\n", desc
);
2056 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2057 ibmvfc_free_event(evt
);
2058 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2063 * ibmvfc_match_rport - Match function for specified remote port
2064 * @evt: ibmvfc event struct
2065 * @device: device to match (rport)
2068 * 1 if event matches rport / 0 if event does not match rport
2070 static int ibmvfc_match_rport(struct ibmvfc_event
*evt
, void *rport
)
2072 struct fc_rport
*cmd_rport
;
2075 cmd_rport
= starget_to_rport(scsi_target(evt
->cmnd
->device
));
2076 if (cmd_rport
== rport
)
2083 * ibmvfc_match_target - Match function for specified target
2084 * @evt: ibmvfc event struct
2085 * @device: device to match (starget)
2088 * 1 if event matches starget / 0 if event does not match starget
2090 static int ibmvfc_match_target(struct ibmvfc_event
*evt
, void *device
)
2092 if (evt
->cmnd
&& scsi_target(evt
->cmnd
->device
) == device
)
2098 * ibmvfc_match_lun - Match function for specified LUN
2099 * @evt: ibmvfc event struct
2100 * @device: device to match (sdev)
2103 * 1 if event matches sdev / 0 if event does not match sdev
2105 static int ibmvfc_match_lun(struct ibmvfc_event
*evt
, void *device
)
2107 if (evt
->cmnd
&& evt
->cmnd
->device
== device
)
2113 * ibmvfc_wait_for_ops - Wait for ops to complete
2114 * @vhost: ibmvfc host struct
2115 * @device: device to match (starget or sdev)
2116 * @match: match function
2121 static int ibmvfc_wait_for_ops(struct ibmvfc_host
*vhost
, void *device
,
2122 int (*match
) (struct ibmvfc_event
*, void *))
2124 struct ibmvfc_event
*evt
;
2125 DECLARE_COMPLETION_ONSTACK(comp
);
2127 unsigned long flags
;
2128 signed long timeout
= IBMVFC_ABORT_WAIT_TIMEOUT
* HZ
;
2133 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2134 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2135 if (match(evt
, device
)) {
2136 evt
->eh_comp
= &comp
;
2140 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2143 timeout
= wait_for_completion_timeout(&comp
, timeout
);
2147 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2148 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2149 if (match(evt
, device
)) {
2150 evt
->eh_comp
= NULL
;
2154 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2156 dev_err(vhost
->dev
, "Timed out waiting for aborted commands\n");
2158 return wait
? FAILED
: SUCCESS
;
2168 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2169 * @sdev: scsi device to cancel commands
2170 * @type: type of error recovery being performed
2172 * This sends a cancel to the VIOS for the specified device. This does
2173 * NOT send any abort to the actual device. That must be done separately.
2176 * 0 on success / other on failure
2178 static int ibmvfc_cancel_all(struct scsi_device
*sdev
, int type
)
2180 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2181 struct scsi_target
*starget
= scsi_target(sdev
);
2182 struct fc_rport
*rport
= starget_to_rport(starget
);
2183 struct ibmvfc_tmf
*tmf
;
2184 struct ibmvfc_event
*evt
, *found_evt
;
2185 union ibmvfc_iu rsp
;
2186 int rsp_rc
= -EBUSY
;
2187 unsigned long flags
;
2191 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2193 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2194 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
2201 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
2202 sdev_printk(KERN_INFO
, sdev
, "No events found to cancel\n");
2203 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2207 if (vhost
->logged_in
) {
2208 evt
= ibmvfc_get_event(vhost
);
2209 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
2212 memset(tmf
, 0, sizeof(*tmf
));
2213 tmf
->common
.version
= cpu_to_be32(1);
2214 tmf
->common
.opcode
= cpu_to_be32(IBMVFC_TMF_MAD
);
2215 tmf
->common
.length
= cpu_to_be16(sizeof(*tmf
));
2216 tmf
->scsi_id
= cpu_to_be64(rport
->port_id
);
2217 int_to_scsilun(sdev
->lun
, &tmf
->lun
);
2218 if (!(be64_to_cpu(vhost
->login_buf
->resp
.capabilities
) & IBMVFC_CAN_SUPPRESS_ABTS
))
2219 type
&= ~IBMVFC_TMF_SUPPRESS_ABTS
;
2220 if (vhost
->state
== IBMVFC_ACTIVE
)
2221 tmf
->flags
= cpu_to_be32((type
| IBMVFC_TMF_LUA_VALID
));
2223 tmf
->flags
= cpu_to_be32(((type
& IBMVFC_TMF_SUPPRESS_ABTS
) | IBMVFC_TMF_LUA_VALID
));
2224 tmf
->cancel_key
= cpu_to_be32((unsigned long)sdev
->hostdata
);
2225 tmf
->my_cancel_key
= cpu_to_be32((unsigned long)starget
->hostdata
);
2227 evt
->sync_iu
= &rsp
;
2228 init_completion(&evt
->comp
);
2229 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
2232 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2235 sdev_printk(KERN_ERR
, sdev
, "Failed to send cancel event. rc=%d\n", rsp_rc
);
2236 /* If failure is received, the host adapter is most likely going
2237 through reset, return success so the caller will wait for the command
2238 being cancelled to get returned */
2242 sdev_printk(KERN_INFO
, sdev
, "Cancelling outstanding commands.\n");
2244 wait_for_completion(&evt
->comp
);
2245 status
= be16_to_cpu(rsp
.mad_common
.status
);
2246 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2247 ibmvfc_free_event(evt
);
2248 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2250 if (status
!= IBMVFC_MAD_SUCCESS
) {
2251 sdev_printk(KERN_WARNING
, sdev
, "Cancel failed with rc=%x\n", status
);
2253 case IBMVFC_MAD_DRIVER_FAILED
:
2254 case IBMVFC_MAD_CRQ_ERROR
:
2255 /* Host adapter most likely going through reset, return success to
2256 the caller will wait for the command being cancelled to get returned */
2263 sdev_printk(KERN_INFO
, sdev
, "Successfully cancelled outstanding commands\n");
2268 * ibmvfc_match_key - Match function for specified cancel key
2269 * @evt: ibmvfc event struct
2270 * @key: cancel key to match
2273 * 1 if event matches key / 0 if event does not match key
2275 static int ibmvfc_match_key(struct ibmvfc_event
*evt
, void *key
)
2277 unsigned long cancel_key
= (unsigned long)key
;
2279 if (evt
->crq
.format
== IBMVFC_CMD_FORMAT
&&
2280 be32_to_cpu(evt
->iu
.cmd
.cancel_key
) == cancel_key
)
2286 * ibmvfc_match_evt - Match function for specified event
2287 * @evt: ibmvfc event struct
2288 * @match: event to match
2291 * 1 if event matches key / 0 if event does not match key
2293 static int ibmvfc_match_evt(struct ibmvfc_event
*evt
, void *match
)
2301 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2302 * @sdev: scsi device to abort commands
2304 * This sends an Abort Task Set to the VIOS for the specified device. This does
2305 * NOT send any cancel to the VIOS. That must be done separately.
2308 * 0 on success / other on failure
2310 static int ibmvfc_abort_task_set(struct scsi_device
*sdev
)
2312 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2313 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2314 struct ibmvfc_cmd
*tmf
;
2315 struct ibmvfc_event
*evt
, *found_evt
;
2316 union ibmvfc_iu rsp_iu
;
2317 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
2318 int rc
, rsp_rc
= -EBUSY
;
2319 unsigned long flags
, timeout
= IBMVFC_ABORT_TIMEOUT
;
2322 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2324 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2325 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
2332 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
2333 sdev_printk(KERN_INFO
, sdev
, "No events found to abort\n");
2334 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2338 if (vhost
->state
== IBMVFC_ACTIVE
) {
2339 evt
= ibmvfc_get_event(vhost
);
2340 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
2343 memset(tmf
, 0, sizeof(*tmf
));
2344 tmf
->resp
.va
= cpu_to_be64(be64_to_cpu(evt
->crq
.ioba
) + offsetof(struct ibmvfc_cmd
, rsp
));
2345 tmf
->resp
.len
= cpu_to_be32(sizeof(tmf
->rsp
));
2346 tmf
->frame_type
= cpu_to_be32(IBMVFC_SCSI_FCP_TYPE
);
2347 tmf
->payload_len
= cpu_to_be32(sizeof(tmf
->iu
));
2348 tmf
->resp_len
= cpu_to_be32(sizeof(tmf
->rsp
));
2349 tmf
->cancel_key
= cpu_to_be32((unsigned long)sdev
->hostdata
);
2350 tmf
->tgt_scsi_id
= cpu_to_be64(rport
->port_id
);
2351 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
2352 tmf
->flags
= cpu_to_be16((IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
));
2353 tmf
->iu
.tmf_flags
= IBMVFC_ABORT_TASK_SET
;
2354 evt
->sync_iu
= &rsp_iu
;
2356 init_completion(&evt
->comp
);
2357 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
2360 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2363 sdev_printk(KERN_ERR
, sdev
, "Failed to send abort. rc=%d\n", rsp_rc
);
2367 sdev_printk(KERN_INFO
, sdev
, "Aborting outstanding commands\n");
2368 timeout
= wait_for_completion_timeout(&evt
->comp
, timeout
);
2371 rc
= ibmvfc_cancel_all(sdev
, 0);
2373 rc
= ibmvfc_wait_for_ops(vhost
, sdev
->hostdata
, ibmvfc_match_key
);
2379 sdev_printk(KERN_INFO
, sdev
, "Cancel failed, resetting host\n");
2380 ibmvfc_reset_host(vhost
);
2382 rc
= ibmvfc_wait_for_ops(vhost
, sdev
->hostdata
, ibmvfc_match_key
);
2387 rc
= ibmvfc_wait_for_ops(vhost
, evt
, ibmvfc_match_evt
);
2388 if (rc
!= SUCCESS
) {
2389 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2390 ibmvfc_hard_reset_host(vhost
);
2391 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2399 if (rsp_iu
.cmd
.status
)
2400 rsp_code
= ibmvfc_get_err_result(&rsp_iu
.cmd
);
2403 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
2404 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
2406 sdev_printk(KERN_ERR
, sdev
, "Abort failed: %s (%x:%x) "
2407 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2408 ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu
.cmd
.status
), be16_to_cpu(rsp_iu
.cmd
.error
)),
2409 be16_to_cpu(rsp_iu
.cmd
.status
), be16_to_cpu(rsp_iu
.cmd
.error
), fc_rsp
->flags
, rsp_code
,
2410 fc_rsp
->scsi_status
);
2413 sdev_printk(KERN_INFO
, sdev
, "Abort successful\n");
2416 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2417 ibmvfc_free_event(evt
);
2418 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2423 * ibmvfc_eh_abort_handler - Abort a command
2424 * @cmd: scsi command to abort
2427 * SUCCESS / FAST_IO_FAIL / FAILED
2429 static int ibmvfc_eh_abort_handler(struct scsi_cmnd
*cmd
)
2431 struct scsi_device
*sdev
= cmd
->device
;
2432 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2433 int cancel_rc
, block_rc
;
2437 block_rc
= fc_block_scsi_eh(cmd
);
2438 ibmvfc_wait_while_resetting(vhost
);
2439 if (block_rc
!= FAST_IO_FAIL
) {
2440 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_ABORT_TASK_SET
);
2441 ibmvfc_abort_task_set(sdev
);
2443 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_SUPPRESS_ABTS
);
2446 rc
= ibmvfc_wait_for_ops(vhost
, sdev
, ibmvfc_match_lun
);
2448 if (block_rc
== FAST_IO_FAIL
&& rc
!= FAILED
)
2456 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2457 * @cmd: scsi command struct
2460 * SUCCESS / FAST_IO_FAIL / FAILED
2462 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
2464 struct scsi_device
*sdev
= cmd
->device
;
2465 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2466 int cancel_rc
, block_rc
, reset_rc
= 0;
2470 block_rc
= fc_block_scsi_eh(cmd
);
2471 ibmvfc_wait_while_resetting(vhost
);
2472 if (block_rc
!= FAST_IO_FAIL
) {
2473 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_LUN_RESET
);
2474 reset_rc
= ibmvfc_reset_device(sdev
, IBMVFC_LUN_RESET
, "LUN");
2476 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_SUPPRESS_ABTS
);
2478 if (!cancel_rc
&& !reset_rc
)
2479 rc
= ibmvfc_wait_for_ops(vhost
, sdev
, ibmvfc_match_lun
);
2481 if (block_rc
== FAST_IO_FAIL
&& rc
!= FAILED
)
2489 * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
2490 * @sdev: scsi device struct
2491 * @data: return code
2494 static void ibmvfc_dev_cancel_all_noreset(struct scsi_device
*sdev
, void *data
)
2496 unsigned long *rc
= data
;
2497 *rc
|= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_SUPPRESS_ABTS
);
2501 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2502 * @sdev: scsi device struct
2503 * @data: return code
2506 static void ibmvfc_dev_cancel_all_reset(struct scsi_device
*sdev
, void *data
)
2508 unsigned long *rc
= data
;
2509 *rc
|= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_TGT_RESET
);
2513 * ibmvfc_eh_target_reset_handler - Reset the target
2514 * @cmd: scsi command struct
2517 * SUCCESS / FAST_IO_FAIL / FAILED
2519 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd
*cmd
)
2521 struct scsi_device
*sdev
= cmd
->device
;
2522 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2523 struct scsi_target
*starget
= scsi_target(sdev
);
2527 unsigned long cancel_rc
= 0;
2530 block_rc
= fc_block_scsi_eh(cmd
);
2531 ibmvfc_wait_while_resetting(vhost
);
2532 if (block_rc
!= FAST_IO_FAIL
) {
2533 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all_reset
);
2534 reset_rc
= ibmvfc_reset_device(sdev
, IBMVFC_TARGET_RESET
, "target");
2536 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all_noreset
);
2538 if (!cancel_rc
&& !reset_rc
)
2539 rc
= ibmvfc_wait_for_ops(vhost
, starget
, ibmvfc_match_target
);
2541 if (block_rc
== FAST_IO_FAIL
&& rc
!= FAILED
)
2549 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2550 * @cmd: struct scsi_cmnd having problems
2553 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
2556 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
2558 dev_err(vhost
->dev
, "Resetting connection due to error recovery\n");
2559 rc
= ibmvfc_issue_fc_host_lip(vhost
->host
);
2561 return rc
? FAILED
: SUCCESS
;
2565 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2566 * @rport: rport struct
2571 static void ibmvfc_terminate_rport_io(struct fc_rport
*rport
)
2573 struct Scsi_Host
*shost
= rport_to_shost(rport
);
2574 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2575 struct fc_rport
*dev_rport
;
2576 struct scsi_device
*sdev
;
2580 shost_for_each_device(sdev
, shost
) {
2581 dev_rport
= starget_to_rport(scsi_target(sdev
));
2582 if (dev_rport
!= rport
)
2584 ibmvfc_cancel_all(sdev
, IBMVFC_TMF_SUPPRESS_ABTS
);
2587 rc
= ibmvfc_wait_for_ops(vhost
, rport
, ibmvfc_match_rport
);
2590 ibmvfc_issue_fc_host_lip(shost
);
2594 static const struct ibmvfc_async_desc ae_desc
[] = {
2595 { "PLOGI", IBMVFC_AE_ELS_PLOGI
, IBMVFC_DEFAULT_LOG_LEVEL
+ 1 },
2596 { "LOGO", IBMVFC_AE_ELS_LOGO
, IBMVFC_DEFAULT_LOG_LEVEL
+ 1 },
2597 { "PRLO", IBMVFC_AE_ELS_PRLO
, IBMVFC_DEFAULT_LOG_LEVEL
+ 1 },
2598 { "N-Port SCN", IBMVFC_AE_SCN_NPORT
, IBMVFC_DEFAULT_LOG_LEVEL
+ 1 },
2599 { "Group SCN", IBMVFC_AE_SCN_GROUP
, IBMVFC_DEFAULT_LOG_LEVEL
+ 1 },
2600 { "Domain SCN", IBMVFC_AE_SCN_DOMAIN
, IBMVFC_DEFAULT_LOG_LEVEL
},
2601 { "Fabric SCN", IBMVFC_AE_SCN_FABRIC
, IBMVFC_DEFAULT_LOG_LEVEL
},
2602 { "Link Up", IBMVFC_AE_LINK_UP
, IBMVFC_DEFAULT_LOG_LEVEL
},
2603 { "Link Down", IBMVFC_AE_LINK_DOWN
, IBMVFC_DEFAULT_LOG_LEVEL
},
2604 { "Link Dead", IBMVFC_AE_LINK_DEAD
, IBMVFC_DEFAULT_LOG_LEVEL
},
2605 { "Halt", IBMVFC_AE_HALT
, IBMVFC_DEFAULT_LOG_LEVEL
},
2606 { "Resume", IBMVFC_AE_RESUME
, IBMVFC_DEFAULT_LOG_LEVEL
},
2607 { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED
, IBMVFC_DEFAULT_LOG_LEVEL
},
2610 static const struct ibmvfc_async_desc unknown_ae
= {
2611 "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
2615 * ibmvfc_get_ae_desc - Get text description for async event
2619 static const struct ibmvfc_async_desc
*ibmvfc_get_ae_desc(u64 ae
)
2623 for (i
= 0; i
< ARRAY_SIZE(ae_desc
); i
++)
2624 if (ae_desc
[i
].ae
== ae
)
2630 static const struct {
2631 enum ibmvfc_ae_link_state state
;
2634 { IBMVFC_AE_LS_LINK_UP
, " link up" },
2635 { IBMVFC_AE_LS_LINK_BOUNCED
, " link bounced" },
2636 { IBMVFC_AE_LS_LINK_DOWN
, " link down" },
2637 { IBMVFC_AE_LS_LINK_DEAD
, " link dead" },
2641 * ibmvfc_get_link_state - Get text description for link state
2642 * @state: link state
2645 static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state
)
2649 for (i
= 0; i
< ARRAY_SIZE(link_desc
); i
++)
2650 if (link_desc
[i
].state
== state
)
2651 return link_desc
[i
].desc
;
2657 * ibmvfc_handle_async - Handle an async event from the adapter
2658 * @crq: crq to process
2659 * @vhost: ibmvfc host struct
2662 static void ibmvfc_handle_async(struct ibmvfc_async_crq
*crq
,
2663 struct ibmvfc_host
*vhost
)
2665 const struct ibmvfc_async_desc
*desc
= ibmvfc_get_ae_desc(be64_to_cpu(crq
->event
));
2666 struct ibmvfc_target
*tgt
;
2668 ibmvfc_log(vhost
, desc
->log_level
, "%s event received. scsi_id: %llx, wwpn: %llx,"
2669 " node_name: %llx%s\n", desc
->desc
, be64_to_cpu(crq
->scsi_id
),
2670 be64_to_cpu(crq
->wwpn
), be64_to_cpu(crq
->node_name
),
2671 ibmvfc_get_link_state(crq
->link_state
));
2673 switch (be64_to_cpu(crq
->event
)) {
2674 case IBMVFC_AE_RESUME
:
2675 switch (crq
->link_state
) {
2676 case IBMVFC_AE_LS_LINK_DOWN
:
2677 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2679 case IBMVFC_AE_LS_LINK_DEAD
:
2680 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2682 case IBMVFC_AE_LS_LINK_UP
:
2683 case IBMVFC_AE_LS_LINK_BOUNCED
:
2685 vhost
->events_to_log
|= IBMVFC_AE_LINKUP
;
2686 vhost
->delay_init
= 1;
2687 __ibmvfc_reset_host(vhost
);
2692 case IBMVFC_AE_LINK_UP
:
2693 vhost
->events_to_log
|= IBMVFC_AE_LINKUP
;
2694 vhost
->delay_init
= 1;
2695 __ibmvfc_reset_host(vhost
);
2697 case IBMVFC_AE_SCN_FABRIC
:
2698 case IBMVFC_AE_SCN_DOMAIN
:
2699 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2700 if (vhost
->state
< IBMVFC_HALTED
) {
2701 vhost
->delay_init
= 1;
2702 __ibmvfc_reset_host(vhost
);
2705 case IBMVFC_AE_SCN_NPORT
:
2706 case IBMVFC_AE_SCN_GROUP
:
2707 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2708 ibmvfc_reinit_host(vhost
);
2710 case IBMVFC_AE_ELS_LOGO
:
2711 case IBMVFC_AE_ELS_PRLO
:
2712 case IBMVFC_AE_ELS_PLOGI
:
2713 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
2714 if (!crq
->scsi_id
&& !crq
->wwpn
&& !crq
->node_name
)
2716 if (crq
->scsi_id
&& cpu_to_be64(tgt
->scsi_id
) != crq
->scsi_id
)
2718 if (crq
->wwpn
&& cpu_to_be64(tgt
->ids
.port_name
) != crq
->wwpn
)
2720 if (crq
->node_name
&& cpu_to_be64(tgt
->ids
.node_name
) != crq
->node_name
)
2722 if (tgt
->need_login
&& be64_to_cpu(crq
->event
) == IBMVFC_AE_ELS_LOGO
)
2724 if (!tgt
->need_login
|| be64_to_cpu(crq
->event
) == IBMVFC_AE_ELS_PLOGI
) {
2725 ibmvfc_del_tgt(tgt
);
2726 ibmvfc_reinit_host(vhost
);
2730 case IBMVFC_AE_LINK_DOWN
:
2731 case IBMVFC_AE_ADAPTER_FAILED
:
2732 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2734 case IBMVFC_AE_LINK_DEAD
:
2735 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2737 case IBMVFC_AE_HALT
:
2738 ibmvfc_link_down(vhost
, IBMVFC_HALTED
);
2741 dev_err(vhost
->dev
, "Unknown async event received: %lld\n", crq
->event
);
2747 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2748 * @crq: Command/Response queue
2749 * @vhost: ibmvfc host struct
2752 static void ibmvfc_handle_crq(struct ibmvfc_crq
*crq
, struct ibmvfc_host
*vhost
)
2755 struct ibmvfc_event
*evt
= (struct ibmvfc_event
*)be64_to_cpu(crq
->ioba
);
2757 switch (crq
->valid
) {
2758 case IBMVFC_CRQ_INIT_RSP
:
2759 switch (crq
->format
) {
2760 case IBMVFC_CRQ_INIT
:
2761 dev_info(vhost
->dev
, "Partner initialized\n");
2762 /* Send back a response */
2763 rc
= ibmvfc_send_crq_init_complete(vhost
);
2765 ibmvfc_init_host(vhost
);
2767 dev_err(vhost
->dev
, "Unable to send init rsp. rc=%ld\n", rc
);
2769 case IBMVFC_CRQ_INIT_COMPLETE
:
2770 dev_info(vhost
->dev
, "Partner initialization complete\n");
2771 ibmvfc_init_host(vhost
);
2774 dev_err(vhost
->dev
, "Unknown crq message type: %d\n", crq
->format
);
2777 case IBMVFC_CRQ_XPORT_EVENT
:
2778 vhost
->state
= IBMVFC_NO_CRQ
;
2779 vhost
->logged_in
= 0;
2780 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
2781 if (crq
->format
== IBMVFC_PARTITION_MIGRATED
) {
2782 /* We need to re-setup the interpartition connection */
2783 dev_info(vhost
->dev
, "Partition migrated, Re-enabling adapter\n");
2784 vhost
->client_migrated
= 1;
2785 ibmvfc_purge_requests(vhost
, DID_REQUEUE
);
2786 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2787 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_REENABLE
);
2788 } else if (crq
->format
== IBMVFC_PARTNER_FAILED
|| crq
->format
== IBMVFC_PARTNER_DEREGISTER
) {
2789 dev_err(vhost
->dev
, "Host partner adapter deregistered or failed (rc=%d)\n", crq
->format
);
2790 ibmvfc_purge_requests(vhost
, DID_ERROR
);
2791 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2792 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_RESET
);
2794 dev_err(vhost
->dev
, "Received unknown transport event from partner (rc=%d)\n", crq
->format
);
2797 case IBMVFC_CRQ_CMD_RSP
:
2800 dev_err(vhost
->dev
, "Got an invalid message type 0x%02x\n", crq
->valid
);
2804 if (crq
->format
== IBMVFC_ASYNC_EVENT
)
2807 /* The only kind of payload CRQs we should get are responses to
2808 * things we send. Make sure this response is to something we
2811 if (unlikely(!ibmvfc_valid_event(&vhost
->pool
, evt
))) {
2812 dev_err(vhost
->dev
, "Returned correlation_token 0x%08llx is invalid!\n",
2817 if (unlikely(atomic_read(&evt
->free
))) {
2818 dev_err(vhost
->dev
, "Received duplicate correlation_token 0x%08llx!\n",
2823 del_timer(&evt
->timer
);
2824 list_del(&evt
->queue
);
2825 ibmvfc_trc_end(evt
);
2830 * ibmvfc_scan_finished - Check if the device scan is done.
2831 * @shost: scsi host struct
2832 * @time: current elapsed time
2835 * 0 if scan is not done / 1 if scan is done
2837 static int ibmvfc_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
2839 unsigned long flags
;
2840 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2843 spin_lock_irqsave(shost
->host_lock
, flags
);
2844 if (time
>= (init_timeout
* HZ
)) {
2845 dev_info(vhost
->dev
, "Scan taking longer than %d seconds, "
2846 "continuing initialization\n", init_timeout
);
2850 if (vhost
->scan_complete
)
2852 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2857 * ibmvfc_slave_alloc - Setup the device's task set value
2858 * @sdev: struct scsi_device device to configure
2860 * Set the device's task set value so that error handling works as
2864 * 0 on success / -ENXIO if device does not exist
2866 static int ibmvfc_slave_alloc(struct scsi_device
*sdev
)
2868 struct Scsi_Host
*shost
= sdev
->host
;
2869 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2870 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2871 unsigned long flags
= 0;
2873 if (!rport
|| fc_remote_port_chkready(rport
))
2876 spin_lock_irqsave(shost
->host_lock
, flags
);
2877 sdev
->hostdata
= (void *)(unsigned long)vhost
->task_set
++;
2878 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2883 * ibmvfc_target_alloc - Setup the target's task set value
2884 * @starget: struct scsi_target
2886 * Set the target's task set value so that error handling works as
2890 * 0 on success / -ENXIO if device does not exist
2892 static int ibmvfc_target_alloc(struct scsi_target
*starget
)
2894 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2895 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2896 unsigned long flags
= 0;
2898 spin_lock_irqsave(shost
->host_lock
, flags
);
2899 starget
->hostdata
= (void *)(unsigned long)vhost
->task_set
++;
2900 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2905 * ibmvfc_slave_configure - Configure the device
2906 * @sdev: struct scsi_device device to configure
2908 * Enable allow_restart for a device if it is a disk. Adjust the
2909 * queue_depth here also.
2914 static int ibmvfc_slave_configure(struct scsi_device
*sdev
)
2916 struct Scsi_Host
*shost
= sdev
->host
;
2917 unsigned long flags
= 0;
2919 spin_lock_irqsave(shost
->host_lock
, flags
);
2920 if (sdev
->type
== TYPE_DISK
)
2921 sdev
->allow_restart
= 1;
2922 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2927 * ibmvfc_change_queue_depth - Change the device's queue depth
2928 * @sdev: scsi device struct
2929 * @qdepth: depth to set
2930 * @reason: calling context
2935 static int ibmvfc_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2937 if (qdepth
> IBMVFC_MAX_CMDS_PER_LUN
)
2938 qdepth
= IBMVFC_MAX_CMDS_PER_LUN
;
2940 return scsi_change_queue_depth(sdev
, qdepth
);
2943 static ssize_t
ibmvfc_show_host_partition_name(struct device
*dev
,
2944 struct device_attribute
*attr
, char *buf
)
2946 struct Scsi_Host
*shost
= class_to_shost(dev
);
2947 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2949 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2950 vhost
->login_buf
->resp
.partition_name
);
2953 static ssize_t
ibmvfc_show_host_device_name(struct device
*dev
,
2954 struct device_attribute
*attr
, char *buf
)
2956 struct Scsi_Host
*shost
= class_to_shost(dev
);
2957 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2959 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2960 vhost
->login_buf
->resp
.device_name
);
2963 static ssize_t
ibmvfc_show_host_loc_code(struct device
*dev
,
2964 struct device_attribute
*attr
, char *buf
)
2966 struct Scsi_Host
*shost
= class_to_shost(dev
);
2967 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2969 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2970 vhost
->login_buf
->resp
.port_loc_code
);
2973 static ssize_t
ibmvfc_show_host_drc_name(struct device
*dev
,
2974 struct device_attribute
*attr
, char *buf
)
2976 struct Scsi_Host
*shost
= class_to_shost(dev
);
2977 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2979 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2980 vhost
->login_buf
->resp
.drc_name
);
2983 static ssize_t
ibmvfc_show_host_npiv_version(struct device
*dev
,
2984 struct device_attribute
*attr
, char *buf
)
2986 struct Scsi_Host
*shost
= class_to_shost(dev
);
2987 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2988 return snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->login_buf
->resp
.version
);
2991 static ssize_t
ibmvfc_show_host_capabilities(struct device
*dev
,
2992 struct device_attribute
*attr
, char *buf
)
2994 struct Scsi_Host
*shost
= class_to_shost(dev
);
2995 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2996 return snprintf(buf
, PAGE_SIZE
, "%llx\n", vhost
->login_buf
->resp
.capabilities
);
3000 * ibmvfc_show_log_level - Show the adapter's error logging level
3001 * @dev: class device struct
3005 * number of bytes printed to buffer
3007 static ssize_t
ibmvfc_show_log_level(struct device
*dev
,
3008 struct device_attribute
*attr
, char *buf
)
3010 struct Scsi_Host
*shost
= class_to_shost(dev
);
3011 struct ibmvfc_host
*vhost
= shost_priv(shost
);
3012 unsigned long flags
= 0;
3015 spin_lock_irqsave(shost
->host_lock
, flags
);
3016 len
= snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->log_level
);
3017 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3022 * ibmvfc_store_log_level - Change the adapter's error logging level
3023 * @dev: class device struct
3027 * number of bytes printed to buffer
3029 static ssize_t
ibmvfc_store_log_level(struct device
*dev
,
3030 struct device_attribute
*attr
,
3031 const char *buf
, size_t count
)
3033 struct Scsi_Host
*shost
= class_to_shost(dev
);
3034 struct ibmvfc_host
*vhost
= shost_priv(shost
);
3035 unsigned long flags
= 0;
3037 spin_lock_irqsave(shost
->host_lock
, flags
);
3038 vhost
->log_level
= simple_strtoul(buf
, NULL
, 10);
3039 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3043 static DEVICE_ATTR(partition_name
, S_IRUGO
, ibmvfc_show_host_partition_name
, NULL
);
3044 static DEVICE_ATTR(device_name
, S_IRUGO
, ibmvfc_show_host_device_name
, NULL
);
3045 static DEVICE_ATTR(port_loc_code
, S_IRUGO
, ibmvfc_show_host_loc_code
, NULL
);
3046 static DEVICE_ATTR(drc_name
, S_IRUGO
, ibmvfc_show_host_drc_name
, NULL
);
3047 static DEVICE_ATTR(npiv_version
, S_IRUGO
, ibmvfc_show_host_npiv_version
, NULL
);
3048 static DEVICE_ATTR(capabilities
, S_IRUGO
, ibmvfc_show_host_capabilities
, NULL
);
3049 static DEVICE_ATTR(log_level
, S_IRUGO
| S_IWUSR
,
3050 ibmvfc_show_log_level
, ibmvfc_store_log_level
);
3052 #ifdef CONFIG_SCSI_IBMVFC_TRACE
3054 * ibmvfc_read_trace - Dump the adapter trace
3055 * @filp: open sysfs file
3056 * @kobj: kobject struct
3057 * @bin_attr: bin_attribute struct
3060 * @count: buffer size
3063 * number of bytes printed to buffer
3065 static ssize_t
ibmvfc_read_trace(struct file
*filp
, struct kobject
*kobj
,
3066 struct bin_attribute
*bin_attr
,
3067 char *buf
, loff_t off
, size_t count
)
3069 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
3070 struct Scsi_Host
*shost
= class_to_shost(dev
);
3071 struct ibmvfc_host
*vhost
= shost_priv(shost
);
3072 unsigned long flags
= 0;
3073 int size
= IBMVFC_TRACE_SIZE
;
3074 char *src
= (char *)vhost
->trace
;
3078 if (off
+ count
> size
) {
3083 spin_lock_irqsave(shost
->host_lock
, flags
);
3084 memcpy(buf
, &src
[off
], count
);
3085 spin_unlock_irqrestore(shost
->host_lock
, flags
);
3089 static struct bin_attribute ibmvfc_trace_attr
= {
3095 .read
= ibmvfc_read_trace
,
3099 static struct device_attribute
*ibmvfc_attrs
[] = {
3100 &dev_attr_partition_name
,
3101 &dev_attr_device_name
,
3102 &dev_attr_port_loc_code
,
3104 &dev_attr_npiv_version
,
3105 &dev_attr_capabilities
,
3106 &dev_attr_log_level
,
3110 static struct scsi_host_template driver_template
= {
3111 .module
= THIS_MODULE
,
3112 .name
= "IBM POWER Virtual FC Adapter",
3113 .proc_name
= IBMVFC_NAME
,
3114 .queuecommand
= ibmvfc_queuecommand
,
3115 .eh_timed_out
= fc_eh_timed_out
,
3116 .eh_abort_handler
= ibmvfc_eh_abort_handler
,
3117 .eh_device_reset_handler
= ibmvfc_eh_device_reset_handler
,
3118 .eh_target_reset_handler
= ibmvfc_eh_target_reset_handler
,
3119 .eh_host_reset_handler
= ibmvfc_eh_host_reset_handler
,
3120 .slave_alloc
= ibmvfc_slave_alloc
,
3121 .slave_configure
= ibmvfc_slave_configure
,
3122 .target_alloc
= ibmvfc_target_alloc
,
3123 .scan_finished
= ibmvfc_scan_finished
,
3124 .change_queue_depth
= ibmvfc_change_queue_depth
,
3126 .can_queue
= IBMVFC_MAX_REQUESTS_DEFAULT
,
3128 .sg_tablesize
= SG_ALL
,
3129 .max_sectors
= IBMVFC_MAX_SECTORS
,
3130 .shost_attrs
= ibmvfc_attrs
,
3131 .track_queue_depth
= 1,
3135 * ibmvfc_next_async_crq - Returns the next entry in async queue
3136 * @vhost: ibmvfc host struct
3139 * Pointer to next entry in queue / NULL if empty
3141 static struct ibmvfc_async_crq
*ibmvfc_next_async_crq(struct ibmvfc_host
*vhost
)
3143 struct ibmvfc_async_crq_queue
*async_crq
= &vhost
->async_crq
;
3144 struct ibmvfc_async_crq
*crq
;
3146 crq
= &async_crq
->msgs
[async_crq
->cur
];
3147 if (crq
->valid
& 0x80) {
3148 if (++async_crq
->cur
== async_crq
->size
)
3158 * ibmvfc_next_crq - Returns the next entry in message queue
3159 * @vhost: ibmvfc host struct
3162 * Pointer to next entry in queue / NULL if empty
3164 static struct ibmvfc_crq
*ibmvfc_next_crq(struct ibmvfc_host
*vhost
)
3166 struct ibmvfc_crq_queue
*queue
= &vhost
->crq
;
3167 struct ibmvfc_crq
*crq
;
3169 crq
= &queue
->msgs
[queue
->cur
];
3170 if (crq
->valid
& 0x80) {
3171 if (++queue
->cur
== queue
->size
)
3181 * ibmvfc_interrupt - Interrupt handler
3182 * @irq: number of irq to handle, not used
3183 * @dev_instance: ibmvfc_host that received interrupt
3188 static irqreturn_t
ibmvfc_interrupt(int irq
, void *dev_instance
)
3190 struct ibmvfc_host
*vhost
= (struct ibmvfc_host
*)dev_instance
;
3191 unsigned long flags
;
3193 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3194 vio_disable_interrupts(to_vio_dev(vhost
->dev
));
3195 tasklet_schedule(&vhost
->tasklet
);
3196 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3201 * ibmvfc_tasklet - Interrupt handler tasklet
3202 * @data: ibmvfc host struct
3207 static void ibmvfc_tasklet(void *data
)
3209 struct ibmvfc_host
*vhost
= data
;
3210 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
3211 struct ibmvfc_crq
*crq
;
3212 struct ibmvfc_async_crq
*async
;
3213 unsigned long flags
;
3216 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3218 /* Pull all the valid messages off the async CRQ */
3219 while ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
3220 ibmvfc_handle_async(async
, vhost
);
3225 /* Pull all the valid messages off the CRQ */
3226 while ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
3227 ibmvfc_handle_crq(crq
, vhost
);
3232 vio_enable_interrupts(vdev
);
3233 if ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
3234 vio_disable_interrupts(vdev
);
3235 ibmvfc_handle_async(async
, vhost
);
3238 } else if ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
3239 vio_disable_interrupts(vdev
);
3240 ibmvfc_handle_crq(crq
, vhost
);
3247 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3251 * ibmvfc_init_tgt - Set the next init job step for the target
3252 * @tgt: ibmvfc target struct
3253 * @job_step: job step to perform
3256 static void ibmvfc_init_tgt(struct ibmvfc_target
*tgt
,
3257 void (*job_step
) (struct ibmvfc_target
*))
3259 if (!ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT
))
3260 tgt
->job_step
= job_step
;
3261 wake_up(&tgt
->vhost
->work_wait_q
);
3265 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3266 * @tgt: ibmvfc target struct
3267 * @job_step: initialization job step
3269 * Returns: 1 if step will be retried / 0 if not
3272 static int ibmvfc_retry_tgt_init(struct ibmvfc_target
*tgt
,
3273 void (*job_step
) (struct ibmvfc_target
*))
3275 if (++tgt
->init_retries
> IBMVFC_MAX_TGT_INIT_RETRIES
) {
3276 ibmvfc_del_tgt(tgt
);
3277 wake_up(&tgt
->vhost
->work_wait_q
);
3280 ibmvfc_init_tgt(tgt
, job_step
);
3284 /* Defined in FC-LS */
3285 static const struct {
3302 * ibmvfc_get_prli_rsp - Find PRLI response index
3303 * @flags: PRLI response flags
3306 static int ibmvfc_get_prli_rsp(u16 flags
)
3309 int code
= (flags
& 0x0f00) >> 8;
3311 for (i
= 0; i
< ARRAY_SIZE(prli_rsp
); i
++)
3312 if (prli_rsp
[i
].code
== code
)
3319 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3320 * @evt: ibmvfc event struct
3323 static void ibmvfc_tgt_prli_done(struct ibmvfc_event
*evt
)
3325 struct ibmvfc_target
*tgt
= evt
->tgt
;
3326 struct ibmvfc_host
*vhost
= evt
->vhost
;
3327 struct ibmvfc_process_login
*rsp
= &evt
->xfer_iu
->prli
;
3328 struct ibmvfc_prli_svc_parms
*parms
= &rsp
->parms
;
3329 u32 status
= be16_to_cpu(rsp
->common
.status
);
3330 int index
, level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3332 vhost
->discovery_threads
--;
3333 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3335 case IBMVFC_MAD_SUCCESS
:
3336 tgt_dbg(tgt
, "Process Login succeeded: %X %02X %04X\n",
3337 parms
->type
, parms
->flags
, parms
->service_parms
);
3339 if (parms
->type
== IBMVFC_SCSI_FCP_TYPE
) {
3340 index
= ibmvfc_get_prli_rsp(be16_to_cpu(parms
->flags
));
3341 if (prli_rsp
[index
].logged_in
) {
3342 if (be16_to_cpu(parms
->flags
) & IBMVFC_PRLI_EST_IMG_PAIR
) {
3343 tgt
->need_login
= 0;
3345 if (be32_to_cpu(parms
->service_parms
) & IBMVFC_PRLI_TARGET_FUNC
)
3346 tgt
->ids
.roles
|= FC_PORT_ROLE_FCP_TARGET
;
3347 if (be32_to_cpu(parms
->service_parms
) & IBMVFC_PRLI_INITIATOR_FUNC
)
3348 tgt
->ids
.roles
|= FC_PORT_ROLE_FCP_INITIATOR
;
3351 ibmvfc_del_tgt(tgt
);
3352 } else if (prli_rsp
[index
].retry
)
3353 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3355 ibmvfc_del_tgt(tgt
);
3357 ibmvfc_del_tgt(tgt
);
3359 case IBMVFC_MAD_DRIVER_FAILED
:
3361 case IBMVFC_MAD_CRQ_ERROR
:
3362 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3364 case IBMVFC_MAD_FAILED
:
3366 if ((be16_to_cpu(rsp
->status
) & IBMVFC_VIOS_FAILURE
) &&
3367 be16_to_cpu(rsp
->error
) == IBMVFC_PLOGI_REQUIRED
)
3368 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3369 else if (tgt
->logo_rcvd
)
3370 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3371 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)))
3372 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3374 ibmvfc_del_tgt(tgt
);
3376 tgt_log(tgt
, level
, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3377 ibmvfc_get_cmd_error(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)),
3378 be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
), status
);
3382 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3383 ibmvfc_free_event(evt
);
3384 wake_up(&vhost
->work_wait_q
);
3388 * ibmvfc_tgt_send_prli - Send a process login
3389 * @tgt: ibmvfc target struct
3392 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*tgt
)
3394 struct ibmvfc_process_login
*prli
;
3395 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3396 struct ibmvfc_event
*evt
;
3398 if (vhost
->discovery_threads
>= disc_threads
)
3401 kref_get(&tgt
->kref
);
3402 evt
= ibmvfc_get_event(vhost
);
3403 vhost
->discovery_threads
++;
3404 ibmvfc_init_event(evt
, ibmvfc_tgt_prli_done
, IBMVFC_MAD_FORMAT
);
3406 prli
= &evt
->iu
.prli
;
3407 memset(prli
, 0, sizeof(*prli
));
3408 prli
->common
.version
= cpu_to_be32(1);
3409 prli
->common
.opcode
= cpu_to_be32(IBMVFC_PROCESS_LOGIN
);
3410 prli
->common
.length
= cpu_to_be16(sizeof(*prli
));
3411 prli
->scsi_id
= cpu_to_be64(tgt
->scsi_id
);
3413 prli
->parms
.type
= IBMVFC_SCSI_FCP_TYPE
;
3414 prli
->parms
.flags
= cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR
);
3415 prli
->parms
.service_parms
= cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC
);
3416 prli
->parms
.service_parms
|= cpu_to_be32(IBMVFC_PRLI_READ_FCP_XFER_RDY_DISABLED
);
3419 prli
->parms
.service_parms
|= cpu_to_be32(IBMVFC_PRLI_RETRY
);
3421 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3422 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3423 vhost
->discovery_threads
--;
3424 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3425 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3427 tgt_dbg(tgt
, "Sent process login\n");
3431 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3432 * @evt: ibmvfc event struct
3435 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event
*evt
)
3437 struct ibmvfc_target
*tgt
= evt
->tgt
;
3438 struct ibmvfc_host
*vhost
= evt
->vhost
;
3439 struct ibmvfc_port_login
*rsp
= &evt
->xfer_iu
->plogi
;
3440 u32 status
= be16_to_cpu(rsp
->common
.status
);
3441 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3443 vhost
->discovery_threads
--;
3444 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3446 case IBMVFC_MAD_SUCCESS
:
3447 tgt_dbg(tgt
, "Port Login succeeded\n");
3448 if (tgt
->ids
.port_name
&&
3449 tgt
->ids
.port_name
!= wwn_to_u64(rsp
->service_parms
.port_name
)) {
3451 tgt_dbg(tgt
, "Port re-init required\n");
3454 tgt
->ids
.node_name
= wwn_to_u64(rsp
->service_parms
.node_name
);
3455 tgt
->ids
.port_name
= wwn_to_u64(rsp
->service_parms
.port_name
);
3456 tgt
->ids
.port_id
= tgt
->scsi_id
;
3457 memcpy(&tgt
->service_parms
, &rsp
->service_parms
,
3458 sizeof(tgt
->service_parms
));
3459 memcpy(&tgt
->service_parms_change
, &rsp
->service_parms_change
,
3460 sizeof(tgt
->service_parms_change
));
3461 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_prli
);
3463 case IBMVFC_MAD_DRIVER_FAILED
:
3465 case IBMVFC_MAD_CRQ_ERROR
:
3466 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3468 case IBMVFC_MAD_FAILED
:
3470 if (ibmvfc_retry_cmd(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)))
3471 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3473 ibmvfc_del_tgt(tgt
);
3475 tgt_log(tgt
, level
, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3476 ibmvfc_get_cmd_error(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)),
3477 be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
),
3478 ibmvfc_get_fc_type(be16_to_cpu(rsp
->fc_type
)), be16_to_cpu(rsp
->fc_type
),
3479 ibmvfc_get_ls_explain(be16_to_cpu(rsp
->fc_explain
)), be16_to_cpu(rsp
->fc_explain
), status
);
3483 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3484 ibmvfc_free_event(evt
);
3485 wake_up(&vhost
->work_wait_q
);
3489 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3490 * @tgt: ibmvfc target struct
3493 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*tgt
)
3495 struct ibmvfc_port_login
*plogi
;
3496 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3497 struct ibmvfc_event
*evt
;
3499 if (vhost
->discovery_threads
>= disc_threads
)
3502 kref_get(&tgt
->kref
);
3504 evt
= ibmvfc_get_event(vhost
);
3505 vhost
->discovery_threads
++;
3506 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3507 ibmvfc_init_event(evt
, ibmvfc_tgt_plogi_done
, IBMVFC_MAD_FORMAT
);
3509 plogi
= &evt
->iu
.plogi
;
3510 memset(plogi
, 0, sizeof(*plogi
));
3511 plogi
->common
.version
= cpu_to_be32(1);
3512 plogi
->common
.opcode
= cpu_to_be32(IBMVFC_PORT_LOGIN
);
3513 plogi
->common
.length
= cpu_to_be16(sizeof(*plogi
));
3514 plogi
->scsi_id
= cpu_to_be64(tgt
->scsi_id
);
3516 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3517 vhost
->discovery_threads
--;
3518 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3519 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3521 tgt_dbg(tgt
, "Sent port login\n");
3525 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3526 * @evt: ibmvfc event struct
3529 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event
*evt
)
3531 struct ibmvfc_target
*tgt
= evt
->tgt
;
3532 struct ibmvfc_host
*vhost
= evt
->vhost
;
3533 struct ibmvfc_implicit_logout
*rsp
= &evt
->xfer_iu
->implicit_logout
;
3534 u32 status
= be16_to_cpu(rsp
->common
.status
);
3536 vhost
->discovery_threads
--;
3537 ibmvfc_free_event(evt
);
3538 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3541 case IBMVFC_MAD_SUCCESS
:
3542 tgt_dbg(tgt
, "Implicit Logout succeeded\n");
3544 case IBMVFC_MAD_DRIVER_FAILED
:
3545 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3546 wake_up(&vhost
->work_wait_q
);
3548 case IBMVFC_MAD_FAILED
:
3550 tgt_err(tgt
, "Implicit Logout failed: rc=0x%02X\n", status
);
3554 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_plogi
);
3555 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3556 wake_up(&vhost
->work_wait_q
);
3560 * __ibmvfc_tgt_get_implicit_logout_evt - Allocate and init an event for implicit logout
3561 * @tgt: ibmvfc target struct
3564 * Allocated and initialized ibmvfc_event struct
3566 static struct ibmvfc_event
*__ibmvfc_tgt_get_implicit_logout_evt(struct ibmvfc_target
*tgt
,
3567 void (*done
) (struct ibmvfc_event
*))
3569 struct ibmvfc_implicit_logout
*mad
;
3570 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3571 struct ibmvfc_event
*evt
;
3573 kref_get(&tgt
->kref
);
3574 evt
= ibmvfc_get_event(vhost
);
3575 ibmvfc_init_event(evt
, done
, IBMVFC_MAD_FORMAT
);
3577 mad
= &evt
->iu
.implicit_logout
;
3578 memset(mad
, 0, sizeof(*mad
));
3579 mad
->common
.version
= cpu_to_be32(1);
3580 mad
->common
.opcode
= cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT
);
3581 mad
->common
.length
= cpu_to_be16(sizeof(*mad
));
3582 mad
->old_scsi_id
= cpu_to_be64(tgt
->scsi_id
);
3587 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3588 * @tgt: ibmvfc target struct
3591 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target
*tgt
)
3593 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3594 struct ibmvfc_event
*evt
;
3596 if (vhost
->discovery_threads
>= disc_threads
)
3599 vhost
->discovery_threads
++;
3600 evt
= __ibmvfc_tgt_get_implicit_logout_evt(tgt
,
3601 ibmvfc_tgt_implicit_logout_done
);
3603 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3604 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3605 vhost
->discovery_threads
--;
3606 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3607 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3609 tgt_dbg(tgt
, "Sent Implicit Logout\n");
3613 * ibmvfc_tgt_implicit_logout_and_del_done - Completion handler for Implicit Logout MAD
3614 * @evt: ibmvfc event struct
3617 static void ibmvfc_tgt_implicit_logout_and_del_done(struct ibmvfc_event
*evt
)
3619 struct ibmvfc_target
*tgt
= evt
->tgt
;
3620 struct ibmvfc_host
*vhost
= evt
->vhost
;
3621 struct ibmvfc_passthru_mad
*mad
= &evt
->xfer_iu
->passthru
;
3622 u32 status
= be16_to_cpu(mad
->common
.status
);
3624 vhost
->discovery_threads
--;
3625 ibmvfc_free_event(evt
);
3626 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3628 tgt_dbg(tgt
, "Implicit Logout %s\n", (status
== IBMVFC_MAD_SUCCESS
) ? "succeeded" : "failed");
3629 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3630 wake_up(&vhost
->work_wait_q
);
3634 * ibmvfc_tgt_implicit_logout_and_del - Initiate an Implicit Logout for specified target
3635 * @tgt: ibmvfc target struct
3638 static void ibmvfc_tgt_implicit_logout_and_del(struct ibmvfc_target
*tgt
)
3640 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3641 struct ibmvfc_event
*evt
;
3643 if (vhost
->discovery_threads
>= disc_threads
)
3646 vhost
->discovery_threads
++;
3647 evt
= __ibmvfc_tgt_get_implicit_logout_evt(tgt
,
3648 ibmvfc_tgt_implicit_logout_and_del_done
);
3650 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT
);
3651 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3652 vhost
->discovery_threads
--;
3653 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3654 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3656 tgt_dbg(tgt
, "Sent Implicit Logout\n");
3660 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3661 * @mad: ibmvfc passthru mad struct
3662 * @tgt: ibmvfc target struct
3665 * 1 if PLOGI needed / 0 if PLOGI not needed
3667 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad
*mad
,
3668 struct ibmvfc_target
*tgt
)
3670 if (wwn_to_u64((u8
*)&mad
->fc_iu
.response
[2]) != tgt
->ids
.port_name
)
3672 if (wwn_to_u64((u8
*)&mad
->fc_iu
.response
[4]) != tgt
->ids
.node_name
)
3674 if (be32_to_cpu(mad
->fc_iu
.response
[6]) != tgt
->scsi_id
)
3680 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3681 * @evt: ibmvfc event struct
3684 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event
*evt
)
3686 struct ibmvfc_target
*tgt
= evt
->tgt
;
3687 struct ibmvfc_host
*vhost
= evt
->vhost
;
3688 struct ibmvfc_passthru_mad
*mad
= &evt
->xfer_iu
->passthru
;
3689 u32 status
= be16_to_cpu(mad
->common
.status
);
3690 u8 fc_reason
, fc_explain
;
3692 vhost
->discovery_threads
--;
3693 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3694 del_timer(&tgt
->timer
);
3697 case IBMVFC_MAD_SUCCESS
:
3698 tgt_dbg(tgt
, "ADISC succeeded\n");
3699 if (ibmvfc_adisc_needs_plogi(mad
, tgt
))
3700 ibmvfc_del_tgt(tgt
);
3702 case IBMVFC_MAD_DRIVER_FAILED
:
3704 case IBMVFC_MAD_FAILED
:
3706 ibmvfc_del_tgt(tgt
);
3707 fc_reason
= (be32_to_cpu(mad
->fc_iu
.response
[1]) & 0x00ff0000) >> 16;
3708 fc_explain
= (be32_to_cpu(mad
->fc_iu
.response
[1]) & 0x0000ff00) >> 8;
3709 tgt_info(tgt
, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3710 ibmvfc_get_cmd_error(be16_to_cpu(mad
->iu
.status
), be16_to_cpu(mad
->iu
.error
)),
3711 be16_to_cpu(mad
->iu
.status
), be16_to_cpu(mad
->iu
.error
),
3712 ibmvfc_get_fc_type(fc_reason
), fc_reason
,
3713 ibmvfc_get_ls_explain(fc_explain
), fc_explain
, status
);
3717 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3718 ibmvfc_free_event(evt
);
3719 wake_up(&vhost
->work_wait_q
);
3723 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3724 * @evt: ibmvfc event struct
3727 static void ibmvfc_init_passthru(struct ibmvfc_event
*evt
)
3729 struct ibmvfc_passthru_mad
*mad
= &evt
->iu
.passthru
;
3731 memset(mad
, 0, sizeof(*mad
));
3732 mad
->common
.version
= cpu_to_be32(1);
3733 mad
->common
.opcode
= cpu_to_be32(IBMVFC_PASSTHRU
);
3734 mad
->common
.length
= cpu_to_be16(sizeof(*mad
) - sizeof(mad
->fc_iu
) - sizeof(mad
->iu
));
3735 mad
->cmd_ioba
.va
= cpu_to_be64((u64
)be64_to_cpu(evt
->crq
.ioba
) +
3736 offsetof(struct ibmvfc_passthru_mad
, iu
));
3737 mad
->cmd_ioba
.len
= cpu_to_be32(sizeof(mad
->iu
));
3738 mad
->iu
.cmd_len
= cpu_to_be32(sizeof(mad
->fc_iu
.payload
));
3739 mad
->iu
.rsp_len
= cpu_to_be32(sizeof(mad
->fc_iu
.response
));
3740 mad
->iu
.cmd
.va
= cpu_to_be64((u64
)be64_to_cpu(evt
->crq
.ioba
) +
3741 offsetof(struct ibmvfc_passthru_mad
, fc_iu
) +
3742 offsetof(struct ibmvfc_passthru_fc_iu
, payload
));
3743 mad
->iu
.cmd
.len
= cpu_to_be32(sizeof(mad
->fc_iu
.payload
));
3744 mad
->iu
.rsp
.va
= cpu_to_be64((u64
)be64_to_cpu(evt
->crq
.ioba
) +
3745 offsetof(struct ibmvfc_passthru_mad
, fc_iu
) +
3746 offsetof(struct ibmvfc_passthru_fc_iu
, response
));
3747 mad
->iu
.rsp
.len
= cpu_to_be32(sizeof(mad
->fc_iu
.response
));
3751 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3752 * @evt: ibmvfc event struct
3754 * Just cleanup this event struct. Everything else is handled by
3755 * the ADISC completion handler. If the ADISC never actually comes
3756 * back, we still have the timer running on the ADISC event struct
3757 * which will fire and cause the CRQ to get reset.
3760 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event
*evt
)
3762 struct ibmvfc_host
*vhost
= evt
->vhost
;
3763 struct ibmvfc_target
*tgt
= evt
->tgt
;
3765 tgt_dbg(tgt
, "ADISC cancel complete\n");
3766 vhost
->abort_threads
--;
3767 ibmvfc_free_event(evt
);
3768 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3769 wake_up(&vhost
->work_wait_q
);
3773 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3774 * @tgt: ibmvfc target struct
3776 * If an ADISC times out, send a cancel. If the cancel times
3777 * out, reset the CRQ. When the ADISC comes back as cancelled,
3778 * log back into the target.
3780 static void ibmvfc_adisc_timeout(struct timer_list
*t
)
3782 struct ibmvfc_target
*tgt
= from_timer(tgt
, t
, timer
);
3783 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3784 struct ibmvfc_event
*evt
;
3785 struct ibmvfc_tmf
*tmf
;
3786 unsigned long flags
;
3789 tgt_dbg(tgt
, "ADISC timeout\n");
3790 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3791 if (vhost
->abort_threads
>= disc_threads
||
3792 tgt
->action
!= IBMVFC_TGT_ACTION_INIT_WAIT
||
3793 vhost
->state
!= IBMVFC_INITIALIZING
||
3794 vhost
->action
!= IBMVFC_HOST_ACTION_QUERY_TGTS
) {
3795 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3799 vhost
->abort_threads
++;
3800 kref_get(&tgt
->kref
);
3801 evt
= ibmvfc_get_event(vhost
);
3802 ibmvfc_init_event(evt
, ibmvfc_tgt_adisc_cancel_done
, IBMVFC_MAD_FORMAT
);
3806 memset(tmf
, 0, sizeof(*tmf
));
3807 tmf
->common
.version
= cpu_to_be32(1);
3808 tmf
->common
.opcode
= cpu_to_be32(IBMVFC_TMF_MAD
);
3809 tmf
->common
.length
= cpu_to_be16(sizeof(*tmf
));
3810 tmf
->scsi_id
= cpu_to_be64(tgt
->scsi_id
);
3811 tmf
->cancel_key
= cpu_to_be32(tgt
->cancel_key
);
3813 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
3816 tgt_err(tgt
, "Failed to send cancel event for ADISC. rc=%d\n", rc
);
3817 vhost
->abort_threads
--;
3818 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3819 __ibmvfc_reset_host(vhost
);
3821 tgt_dbg(tgt
, "Attempting to cancel ADISC\n");
3822 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3826 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3827 * @tgt: ibmvfc target struct
3829 * When sending an ADISC we end up with two timers running. The
3830 * first timer is the timer in the ibmvfc target struct. If this
3831 * fires, we send a cancel to the target. The second timer is the
3832 * timer on the ibmvfc event for the ADISC, which is longer. If that
3833 * fires, it means the ADISC timed out and our attempt to cancel it
3834 * also failed, so we need to reset the CRQ.
3836 static void ibmvfc_tgt_adisc(struct ibmvfc_target
*tgt
)
3838 struct ibmvfc_passthru_mad
*mad
;
3839 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3840 struct ibmvfc_event
*evt
;
3842 if (vhost
->discovery_threads
>= disc_threads
)
3845 kref_get(&tgt
->kref
);
3846 evt
= ibmvfc_get_event(vhost
);
3847 vhost
->discovery_threads
++;
3848 ibmvfc_init_event(evt
, ibmvfc_tgt_adisc_done
, IBMVFC_MAD_FORMAT
);
3851 ibmvfc_init_passthru(evt
);
3852 mad
= &evt
->iu
.passthru
;
3853 mad
->iu
.flags
= cpu_to_be32(IBMVFC_FC_ELS
);
3854 mad
->iu
.scsi_id
= cpu_to_be64(tgt
->scsi_id
);
3855 mad
->iu
.cancel_key
= cpu_to_be32(tgt
->cancel_key
);
3857 mad
->fc_iu
.payload
[0] = cpu_to_be32(IBMVFC_ADISC
);
3858 memcpy(&mad
->fc_iu
.payload
[2], &vhost
->login_buf
->resp
.port_name
,
3859 sizeof(vhost
->login_buf
->resp
.port_name
));
3860 memcpy(&mad
->fc_iu
.payload
[4], &vhost
->login_buf
->resp
.node_name
,
3861 sizeof(vhost
->login_buf
->resp
.node_name
));
3862 mad
->fc_iu
.payload
[6] = cpu_to_be32(be64_to_cpu(vhost
->login_buf
->resp
.scsi_id
) & 0x00ffffff);
3864 if (timer_pending(&tgt
->timer
))
3865 mod_timer(&tgt
->timer
, jiffies
+ (IBMVFC_ADISC_TIMEOUT
* HZ
));
3867 tgt
->timer
.expires
= jiffies
+ (IBMVFC_ADISC_TIMEOUT
* HZ
);
3868 add_timer(&tgt
->timer
);
3871 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3872 if (ibmvfc_send_event(evt
, vhost
, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT
)) {
3873 vhost
->discovery_threads
--;
3874 del_timer(&tgt
->timer
);
3875 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3876 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3878 tgt_dbg(tgt
, "Sent ADISC\n");
3882 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3883 * @evt: ibmvfc event struct
3886 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event
*evt
)
3888 struct ibmvfc_target
*tgt
= evt
->tgt
;
3889 struct ibmvfc_host
*vhost
= evt
->vhost
;
3890 struct ibmvfc_query_tgt
*rsp
= &evt
->xfer_iu
->query_tgt
;
3891 u32 status
= be16_to_cpu(rsp
->common
.status
);
3892 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3894 vhost
->discovery_threads
--;
3895 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3897 case IBMVFC_MAD_SUCCESS
:
3898 tgt_dbg(tgt
, "Query Target succeeded\n");
3899 if (be64_to_cpu(rsp
->scsi_id
) != tgt
->scsi_id
)
3900 ibmvfc_del_tgt(tgt
);
3902 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_adisc
);
3904 case IBMVFC_MAD_DRIVER_FAILED
:
3906 case IBMVFC_MAD_CRQ_ERROR
:
3907 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
3909 case IBMVFC_MAD_FAILED
:
3911 if ((be16_to_cpu(rsp
->status
) & IBMVFC_FABRIC_MAPPED
) == IBMVFC_FABRIC_MAPPED
&&
3912 be16_to_cpu(rsp
->error
) == IBMVFC_UNABLE_TO_PERFORM_REQ
&&
3913 be16_to_cpu(rsp
->fc_explain
) == IBMVFC_PORT_NAME_NOT_REG
)
3914 ibmvfc_del_tgt(tgt
);
3915 else if (ibmvfc_retry_cmd(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)))
3916 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
3918 ibmvfc_del_tgt(tgt
);
3920 tgt_log(tgt
, level
, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3921 ibmvfc_get_cmd_error(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)),
3922 be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
),
3923 ibmvfc_get_fc_type(be16_to_cpu(rsp
->fc_type
)), be16_to_cpu(rsp
->fc_type
),
3924 ibmvfc_get_gs_explain(be16_to_cpu(rsp
->fc_explain
)), be16_to_cpu(rsp
->fc_explain
),
3929 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3930 ibmvfc_free_event(evt
);
3931 wake_up(&vhost
->work_wait_q
);
3935 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3936 * @tgt: ibmvfc target struct
3939 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*tgt
)
3941 struct ibmvfc_query_tgt
*query_tgt
;
3942 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3943 struct ibmvfc_event
*evt
;
3945 if (vhost
->discovery_threads
>= disc_threads
)
3948 kref_get(&tgt
->kref
);
3949 evt
= ibmvfc_get_event(vhost
);
3950 vhost
->discovery_threads
++;
3952 ibmvfc_init_event(evt
, ibmvfc_tgt_query_target_done
, IBMVFC_MAD_FORMAT
);
3953 query_tgt
= &evt
->iu
.query_tgt
;
3954 memset(query_tgt
, 0, sizeof(*query_tgt
));
3955 query_tgt
->common
.version
= cpu_to_be32(1);
3956 query_tgt
->common
.opcode
= cpu_to_be32(IBMVFC_QUERY_TARGET
);
3957 query_tgt
->common
.length
= cpu_to_be16(sizeof(*query_tgt
));
3958 query_tgt
->wwpn
= cpu_to_be64(tgt
->ids
.port_name
);
3960 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3961 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3962 vhost
->discovery_threads
--;
3963 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3964 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3966 tgt_dbg(tgt
, "Sent Query Target\n");
3970 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3971 * @vhost: ibmvfc host struct
3972 * @scsi_id: SCSI ID to allocate target for
3975 * 0 on success / other on failure
3977 static int ibmvfc_alloc_target(struct ibmvfc_host
*vhost
, u64 scsi_id
)
3979 struct ibmvfc_target
*tgt
;
3980 unsigned long flags
;
3982 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3983 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3984 if (tgt
->scsi_id
== scsi_id
) {
3985 if (tgt
->need_login
)
3986 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3990 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3992 tgt
= mempool_alloc(vhost
->tgt_pool
, GFP_NOIO
);
3993 memset(tgt
, 0, sizeof(*tgt
));
3994 tgt
->scsi_id
= scsi_id
;
3996 tgt
->need_login
= 1;
3997 tgt
->cancel_key
= vhost
->task_set
++;
3998 timer_setup(&tgt
->timer
, ibmvfc_adisc_timeout
, 0);
3999 kref_init(&tgt
->kref
);
4000 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
4001 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4002 list_add_tail(&tgt
->queue
, &vhost
->targets
);
4005 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4010 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
4011 * @vhost: ibmvfc host struct
4014 * 0 on success / other on failure
4016 static int ibmvfc_alloc_targets(struct ibmvfc_host
*vhost
)
4020 for (i
= 0, rc
= 0; !rc
&& i
< vhost
->num_targets
; i
++)
4021 rc
= ibmvfc_alloc_target(vhost
,
4022 be32_to_cpu(vhost
->disc_buf
->scsi_id
[i
]) &
4023 IBMVFC_DISC_TGT_SCSI_ID_MASK
);
4029 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
4030 * @evt: ibmvfc event struct
4033 static void ibmvfc_discover_targets_done(struct ibmvfc_event
*evt
)
4035 struct ibmvfc_host
*vhost
= evt
->vhost
;
4036 struct ibmvfc_discover_targets
*rsp
= &evt
->xfer_iu
->discover_targets
;
4037 u32 mad_status
= be16_to_cpu(rsp
->common
.status
);
4038 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
4040 switch (mad_status
) {
4041 case IBMVFC_MAD_SUCCESS
:
4042 ibmvfc_dbg(vhost
, "Discover Targets succeeded\n");
4043 vhost
->num_targets
= be32_to_cpu(rsp
->num_written
);
4044 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_ALLOC_TGTS
);
4046 case IBMVFC_MAD_FAILED
:
4047 level
+= ibmvfc_retry_host_init(vhost
);
4048 ibmvfc_log(vhost
, level
, "Discover Targets failed: %s (%x:%x)\n",
4049 ibmvfc_get_cmd_error(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)),
4050 be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
));
4052 case IBMVFC_MAD_DRIVER_FAILED
:
4055 dev_err(vhost
->dev
, "Invalid Discover Targets response: 0x%x\n", mad_status
);
4056 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4060 ibmvfc_free_event(evt
);
4061 wake_up(&vhost
->work_wait_q
);
4065 * ibmvfc_discover_targets - Send Discover Targets MAD
4066 * @vhost: ibmvfc host struct
4069 static void ibmvfc_discover_targets(struct ibmvfc_host
*vhost
)
4071 struct ibmvfc_discover_targets
*mad
;
4072 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
4074 ibmvfc_init_event(evt
, ibmvfc_discover_targets_done
, IBMVFC_MAD_FORMAT
);
4075 mad
= &evt
->iu
.discover_targets
;
4076 memset(mad
, 0, sizeof(*mad
));
4077 mad
->common
.version
= cpu_to_be32(1);
4078 mad
->common
.opcode
= cpu_to_be32(IBMVFC_DISC_TARGETS
);
4079 mad
->common
.length
= cpu_to_be16(sizeof(*mad
));
4080 mad
->bufflen
= cpu_to_be32(vhost
->disc_buf_sz
);
4081 mad
->buffer
.va
= cpu_to_be64(vhost
->disc_buf_dma
);
4082 mad
->buffer
.len
= cpu_to_be32(vhost
->disc_buf_sz
);
4083 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
4085 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
4086 ibmvfc_dbg(vhost
, "Sent discover targets\n");
4088 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4092 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
4093 * @evt: ibmvfc event struct
4096 static void ibmvfc_npiv_login_done(struct ibmvfc_event
*evt
)
4098 struct ibmvfc_host
*vhost
= evt
->vhost
;
4099 u32 mad_status
= be16_to_cpu(evt
->xfer_iu
->npiv_login
.common
.status
);
4100 struct ibmvfc_npiv_login_resp
*rsp
= &vhost
->login_buf
->resp
;
4101 unsigned int npiv_max_sectors
;
4102 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
4104 switch (mad_status
) {
4105 case IBMVFC_MAD_SUCCESS
:
4106 ibmvfc_free_event(evt
);
4108 case IBMVFC_MAD_FAILED
:
4109 if (ibmvfc_retry_cmd(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)))
4110 level
+= ibmvfc_retry_host_init(vhost
);
4112 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4113 ibmvfc_log(vhost
, level
, "NPIV Login failed: %s (%x:%x)\n",
4114 ibmvfc_get_cmd_error(be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
)),
4115 be16_to_cpu(rsp
->status
), be16_to_cpu(rsp
->error
));
4116 ibmvfc_free_event(evt
);
4118 case IBMVFC_MAD_CRQ_ERROR
:
4119 ibmvfc_retry_host_init(vhost
);
4121 case IBMVFC_MAD_DRIVER_FAILED
:
4122 ibmvfc_free_event(evt
);
4125 dev_err(vhost
->dev
, "Invalid NPIV Login response: 0x%x\n", mad_status
);
4126 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4127 ibmvfc_free_event(evt
);
4131 vhost
->client_migrated
= 0;
4133 if (!(be32_to_cpu(rsp
->flags
) & IBMVFC_NATIVE_FC
)) {
4134 dev_err(vhost
->dev
, "Virtual adapter does not support FC. %x\n",
4136 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4137 wake_up(&vhost
->work_wait_q
);
4141 if (be32_to_cpu(rsp
->max_cmds
) <= IBMVFC_NUM_INTERNAL_REQ
) {
4142 dev_err(vhost
->dev
, "Virtual adapter supported queue depth too small: %d\n",
4144 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4145 wake_up(&vhost
->work_wait_q
);
4149 vhost
->logged_in
= 1;
4150 npiv_max_sectors
= min((uint
)(be64_to_cpu(rsp
->max_dma_len
) >> 9), IBMVFC_MAX_SECTORS
);
4151 dev_info(vhost
->dev
, "Host partition: %s, device: %s %s %s max sectors %u\n",
4152 rsp
->partition_name
, rsp
->device_name
, rsp
->port_loc_code
,
4153 rsp
->drc_name
, npiv_max_sectors
);
4155 fc_host_fabric_name(vhost
->host
) = be64_to_cpu(rsp
->node_name
);
4156 fc_host_node_name(vhost
->host
) = be64_to_cpu(rsp
->node_name
);
4157 fc_host_port_name(vhost
->host
) = be64_to_cpu(rsp
->port_name
);
4158 fc_host_port_id(vhost
->host
) = be64_to_cpu(rsp
->scsi_id
);
4159 fc_host_port_type(vhost
->host
) = FC_PORTTYPE_NPIV
;
4160 fc_host_supported_classes(vhost
->host
) = 0;
4161 if (be32_to_cpu(rsp
->service_parms
.class1_parms
[0]) & 0x80000000)
4162 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS1
;
4163 if (be32_to_cpu(rsp
->service_parms
.class2_parms
[0]) & 0x80000000)
4164 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS2
;
4165 if (be32_to_cpu(rsp
->service_parms
.class3_parms
[0]) & 0x80000000)
4166 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS3
;
4167 fc_host_maxframe_size(vhost
->host
) =
4168 be16_to_cpu(rsp
->service_parms
.common
.bb_rcv_sz
) & 0x0fff;
4170 vhost
->host
->can_queue
= be32_to_cpu(rsp
->max_cmds
) - IBMVFC_NUM_INTERNAL_REQ
;
4171 vhost
->host
->max_sectors
= npiv_max_sectors
;
4172 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
4173 wake_up(&vhost
->work_wait_q
);
4177 * ibmvfc_npiv_login - Sends NPIV login
4178 * @vhost: ibmvfc host struct
4181 static void ibmvfc_npiv_login(struct ibmvfc_host
*vhost
)
4183 struct ibmvfc_npiv_login_mad
*mad
;
4184 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
4186 ibmvfc_gather_partition_info(vhost
);
4187 ibmvfc_set_login_info(vhost
);
4188 ibmvfc_init_event(evt
, ibmvfc_npiv_login_done
, IBMVFC_MAD_FORMAT
);
4190 memcpy(vhost
->login_buf
, &vhost
->login_info
, sizeof(vhost
->login_info
));
4191 mad
= &evt
->iu
.npiv_login
;
4192 memset(mad
, 0, sizeof(struct ibmvfc_npiv_login_mad
));
4193 mad
->common
.version
= cpu_to_be32(1);
4194 mad
->common
.opcode
= cpu_to_be32(IBMVFC_NPIV_LOGIN
);
4195 mad
->common
.length
= cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad
));
4196 mad
->buffer
.va
= cpu_to_be64(vhost
->login_buf_dma
);
4197 mad
->buffer
.len
= cpu_to_be32(sizeof(*vhost
->login_buf
));
4199 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
4201 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
4202 ibmvfc_dbg(vhost
, "Sent NPIV login\n");
4204 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4208 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4209 * @vhost: ibmvfc host struct
4212 static void ibmvfc_npiv_logout_done(struct ibmvfc_event
*evt
)
4214 struct ibmvfc_host
*vhost
= evt
->vhost
;
4215 u32 mad_status
= be16_to_cpu(evt
->xfer_iu
->npiv_logout
.common
.status
);
4217 ibmvfc_free_event(evt
);
4219 switch (mad_status
) {
4220 case IBMVFC_MAD_SUCCESS
:
4221 if (list_empty(&vhost
->sent
) &&
4222 vhost
->action
== IBMVFC_HOST_ACTION_LOGO_WAIT
) {
4223 ibmvfc_init_host(vhost
);
4227 case IBMVFC_MAD_FAILED
:
4228 case IBMVFC_MAD_NOT_SUPPORTED
:
4229 case IBMVFC_MAD_CRQ_ERROR
:
4230 case IBMVFC_MAD_DRIVER_FAILED
:
4232 ibmvfc_dbg(vhost
, "NPIV Logout failed. 0x%X\n", mad_status
);
4236 ibmvfc_hard_reset_host(vhost
);
4240 * ibmvfc_npiv_logout - Issue an NPIV Logout
4241 * @vhost: ibmvfc host struct
4244 static void ibmvfc_npiv_logout(struct ibmvfc_host
*vhost
)
4246 struct ibmvfc_npiv_logout_mad
*mad
;
4247 struct ibmvfc_event
*evt
;
4249 evt
= ibmvfc_get_event(vhost
);
4250 ibmvfc_init_event(evt
, ibmvfc_npiv_logout_done
, IBMVFC_MAD_FORMAT
);
4252 mad
= &evt
->iu
.npiv_logout
;
4253 memset(mad
, 0, sizeof(*mad
));
4254 mad
->common
.version
= cpu_to_be32(1);
4255 mad
->common
.opcode
= cpu_to_be32(IBMVFC_NPIV_LOGOUT
);
4256 mad
->common
.length
= cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad
));
4258 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_LOGO_WAIT
);
4260 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
4261 ibmvfc_dbg(vhost
, "Sent NPIV logout\n");
4263 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4267 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4268 * @vhost: ibmvfc host struct
4271 * 1 if work to do / 0 if not
4273 static int ibmvfc_dev_init_to_do(struct ibmvfc_host
*vhost
)
4275 struct ibmvfc_target
*tgt
;
4277 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4278 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
||
4279 tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
4287 * ibmvfc_dev_logo_to_do - Is there target logout work to do?
4288 * @vhost: ibmvfc host struct
4291 * 1 if work to do / 0 if not
4293 static int ibmvfc_dev_logo_to_do(struct ibmvfc_host
*vhost
)
4295 struct ibmvfc_target
*tgt
;
4297 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4298 if (tgt
->action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT
||
4299 tgt
->action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT
)
4306 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4307 * @vhost: ibmvfc host struct
4310 * 1 if work to do / 0 if not
4312 static int __ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
4314 struct ibmvfc_target
*tgt
;
4316 if (kthread_should_stop())
4318 switch (vhost
->action
) {
4319 case IBMVFC_HOST_ACTION_NONE
:
4320 case IBMVFC_HOST_ACTION_INIT_WAIT
:
4321 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
4323 case IBMVFC_HOST_ACTION_TGT_INIT
:
4324 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
4325 if (vhost
->discovery_threads
== disc_threads
)
4327 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4328 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
)
4330 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4331 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
4334 case IBMVFC_HOST_ACTION_TGT_DEL
:
4335 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
4336 if (vhost
->discovery_threads
== disc_threads
)
4338 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4339 if (tgt
->action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT
)
4341 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4342 if (tgt
->action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT_WAIT
)
4345 case IBMVFC_HOST_ACTION_LOGO
:
4346 case IBMVFC_HOST_ACTION_INIT
:
4347 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
4348 case IBMVFC_HOST_ACTION_QUERY
:
4349 case IBMVFC_HOST_ACTION_RESET
:
4350 case IBMVFC_HOST_ACTION_REENABLE
:
4359 * ibmvfc_work_to_do - Is there task level work to do?
4360 * @vhost: ibmvfc host struct
4363 * 1 if work to do / 0 if not
4365 static int ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
4367 unsigned long flags
;
4370 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4371 rc
= __ibmvfc_work_to_do(vhost
);
4372 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4377 * ibmvfc_log_ae - Log async events if necessary
4378 * @vhost: ibmvfc host struct
4379 * @events: events to log
4382 static void ibmvfc_log_ae(struct ibmvfc_host
*vhost
, int events
)
4384 if (events
& IBMVFC_AE_RSCN
)
4385 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_RSCN
, 0);
4386 if ((events
& IBMVFC_AE_LINKDOWN
) &&
4387 vhost
->state
>= IBMVFC_HALTED
)
4388 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKDOWN
, 0);
4389 if ((events
& IBMVFC_AE_LINKUP
) &&
4390 vhost
->state
== IBMVFC_INITIALIZING
)
4391 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKUP
, 0);
4395 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4396 * @tgt: ibmvfc target struct
4399 static void ibmvfc_tgt_add_rport(struct ibmvfc_target
*tgt
)
4401 struct ibmvfc_host
*vhost
= tgt
->vhost
;
4402 struct fc_rport
*rport
;
4403 unsigned long flags
;
4405 tgt_dbg(tgt
, "Adding rport\n");
4406 rport
= fc_remote_port_add(vhost
->host
, 0, &tgt
->ids
);
4407 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4409 if (rport
&& tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
4410 tgt_dbg(tgt
, "Deleting rport\n");
4411 list_del(&tgt
->queue
);
4412 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DELETED_RPORT
);
4413 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4414 fc_remote_port_delete(rport
);
4415 del_timer_sync(&tgt
->timer
);
4416 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4418 } else if (rport
&& tgt
->action
== IBMVFC_TGT_ACTION_DELETED_RPORT
) {
4419 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4424 tgt_dbg(tgt
, "rport add succeeded\n");
4426 rport
->maxframe_size
= be16_to_cpu(tgt
->service_parms
.common
.bb_rcv_sz
) & 0x0fff;
4427 rport
->supported_classes
= 0;
4428 tgt
->target_id
= rport
->scsi_target_id
;
4429 if (be32_to_cpu(tgt
->service_parms
.class1_parms
[0]) & 0x80000000)
4430 rport
->supported_classes
|= FC_COS_CLASS1
;
4431 if (be32_to_cpu(tgt
->service_parms
.class2_parms
[0]) & 0x80000000)
4432 rport
->supported_classes
|= FC_COS_CLASS2
;
4433 if (be32_to_cpu(tgt
->service_parms
.class3_parms
[0]) & 0x80000000)
4434 rport
->supported_classes
|= FC_COS_CLASS3
;
4436 blk_queue_max_segments(rport
->rqst_q
, 1);
4438 tgt_dbg(tgt
, "rport add failed\n");
4439 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4443 * ibmvfc_do_work - Do task level work
4444 * @vhost: ibmvfc host struct
4447 static void ibmvfc_do_work(struct ibmvfc_host
*vhost
)
4449 struct ibmvfc_target
*tgt
;
4450 unsigned long flags
;
4451 struct fc_rport
*rport
;
4454 ibmvfc_log_ae(vhost
, vhost
->events_to_log
);
4455 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4456 vhost
->events_to_log
= 0;
4457 switch (vhost
->action
) {
4458 case IBMVFC_HOST_ACTION_NONE
:
4459 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
4460 case IBMVFC_HOST_ACTION_INIT_WAIT
:
4462 case IBMVFC_HOST_ACTION_RESET
:
4463 vhost
->action
= IBMVFC_HOST_ACTION_TGT_DEL
;
4464 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4465 rc
= ibmvfc_reset_crq(vhost
);
4466 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4468 vio_enable_interrupts(to_vio_dev(vhost
->dev
));
4469 if (rc
|| (rc
= ibmvfc_send_crq_init(vhost
)) ||
4470 (rc
= vio_enable_interrupts(to_vio_dev(vhost
->dev
)))) {
4471 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4472 dev_err(vhost
->dev
, "Error after reset (rc=%d)\n", rc
);
4475 case IBMVFC_HOST_ACTION_REENABLE
:
4476 vhost
->action
= IBMVFC_HOST_ACTION_TGT_DEL
;
4477 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4478 rc
= ibmvfc_reenable_crq_queue(vhost
);
4479 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4480 if (rc
|| (rc
= ibmvfc_send_crq_init(vhost
))) {
4481 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4482 dev_err(vhost
->dev
, "Error after enable (rc=%d)\n", rc
);
4485 case IBMVFC_HOST_ACTION_LOGO
:
4486 vhost
->job_step(vhost
);
4488 case IBMVFC_HOST_ACTION_INIT
:
4489 BUG_ON(vhost
->state
!= IBMVFC_INITIALIZING
);
4490 if (vhost
->delay_init
) {
4491 vhost
->delay_init
= 0;
4492 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4496 vhost
->job_step(vhost
);
4498 case IBMVFC_HOST_ACTION_QUERY
:
4499 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4500 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_query_target
);
4501 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY_TGTS
);
4503 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
4504 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4505 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
4511 if (!ibmvfc_dev_init_to_do(vhost
))
4512 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
4514 case IBMVFC_HOST_ACTION_TGT_DEL
:
4515 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
4516 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4517 if (tgt
->action
== IBMVFC_TGT_ACTION_LOGOUT_RPORT
) {
4523 if (ibmvfc_dev_logo_to_do(vhost
)) {
4524 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4528 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4529 if (tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
4530 tgt_dbg(tgt
, "Deleting rport\n");
4533 list_del(&tgt
->queue
);
4534 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DELETED_RPORT
);
4535 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4537 fc_remote_port_delete(rport
);
4538 del_timer_sync(&tgt
->timer
);
4539 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4544 if (vhost
->state
== IBMVFC_INITIALIZING
) {
4545 if (vhost
->action
== IBMVFC_HOST_ACTION_TGT_DEL_FAILED
) {
4546 if (vhost
->reinit
) {
4548 scsi_block_requests(vhost
->host
);
4549 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
4550 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4552 ibmvfc_set_host_state(vhost
, IBMVFC_ACTIVE
);
4553 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
4554 wake_up(&vhost
->init_wait_q
);
4555 schedule_work(&vhost
->rport_add_work_q
);
4556 vhost
->init_retries
= 0;
4557 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4558 scsi_unblock_requests(vhost
->host
);
4563 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
4564 vhost
->job_step
= ibmvfc_discover_targets
;
4567 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
4568 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4569 scsi_unblock_requests(vhost
->host
);
4570 wake_up(&vhost
->init_wait_q
);
4574 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
4575 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_INIT
);
4576 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4577 ibmvfc_alloc_targets(vhost
);
4578 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4580 case IBMVFC_HOST_ACTION_TGT_INIT
:
4581 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4582 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
4588 if (!ibmvfc_dev_init_to_do(vhost
))
4589 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL_FAILED
);
4595 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4599 * ibmvfc_work - Do task level work
4600 * @data: ibmvfc host struct
4605 static int ibmvfc_work(void *data
)
4607 struct ibmvfc_host
*vhost
= data
;
4610 set_user_nice(current
, MIN_NICE
);
4613 rc
= wait_event_interruptible(vhost
->work_wait_q
,
4614 ibmvfc_work_to_do(vhost
));
4618 if (kthread_should_stop())
4621 ibmvfc_do_work(vhost
);
4624 ibmvfc_dbg(vhost
, "ibmvfc kthread exiting...\n");
4629 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4630 * @vhost: ibmvfc host struct
4632 * Allocates a page for messages, maps it for dma, and registers
4633 * the crq with the hypervisor.
4636 * zero on success / other on failure
4638 static int ibmvfc_init_crq(struct ibmvfc_host
*vhost
)
4640 int rc
, retrc
= -ENOMEM
;
4641 struct device
*dev
= vhost
->dev
;
4642 struct vio_dev
*vdev
= to_vio_dev(dev
);
4643 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
4646 crq
->msgs
= (struct ibmvfc_crq
*)get_zeroed_page(GFP_KERNEL
);
4651 crq
->size
= PAGE_SIZE
/ sizeof(*crq
->msgs
);
4652 crq
->msg_token
= dma_map_single(dev
, crq
->msgs
,
4653 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
4655 if (dma_mapping_error(dev
, crq
->msg_token
))
4658 retrc
= rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
4659 crq
->msg_token
, PAGE_SIZE
);
4661 if (rc
== H_RESOURCE
)
4662 /* maybe kexecing and resource is busy. try a reset */
4663 retrc
= rc
= ibmvfc_reset_crq(vhost
);
4666 dev_warn(dev
, "Partner adapter not ready\n");
4668 dev_warn(dev
, "Error %d opening adapter\n", rc
);
4669 goto reg_crq_failed
;
4674 tasklet_init(&vhost
->tasklet
, (void *)ibmvfc_tasklet
, (unsigned long)vhost
);
4676 if ((rc
= request_irq(vdev
->irq
, ibmvfc_interrupt
, 0, IBMVFC_NAME
, vhost
))) {
4677 dev_err(dev
, "Couldn't register irq 0x%x. rc=%d\n", vdev
->irq
, rc
);
4678 goto req_irq_failed
;
4681 if ((rc
= vio_enable_interrupts(vdev
))) {
4682 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
4683 goto req_irq_failed
;
4691 tasklet_kill(&vhost
->tasklet
);
4693 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
4694 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
4696 dma_unmap_single(dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
4698 free_page((unsigned long)crq
->msgs
);
4703 * ibmvfc_free_mem - Free memory for vhost
4704 * @vhost: ibmvfc host struct
4709 static void ibmvfc_free_mem(struct ibmvfc_host
*vhost
)
4711 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
4714 mempool_destroy(vhost
->tgt_pool
);
4715 kfree(vhost
->trace
);
4716 dma_free_coherent(vhost
->dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
4717 vhost
->disc_buf_dma
);
4718 dma_free_coherent(vhost
->dev
, sizeof(*vhost
->login_buf
),
4719 vhost
->login_buf
, vhost
->login_buf_dma
);
4720 dma_pool_destroy(vhost
->sg_pool
);
4721 dma_unmap_single(vhost
->dev
, async_q
->msg_token
,
4722 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
4723 free_page((unsigned long)async_q
->msgs
);
4728 * ibmvfc_alloc_mem - Allocate memory for vhost
4729 * @vhost: ibmvfc host struct
4732 * 0 on success / non-zero on failure
4734 static int ibmvfc_alloc_mem(struct ibmvfc_host
*vhost
)
4736 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
4737 struct device
*dev
= vhost
->dev
;
4740 async_q
->msgs
= (struct ibmvfc_async_crq
*)get_zeroed_page(GFP_KERNEL
);
4741 if (!async_q
->msgs
) {
4742 dev_err(dev
, "Couldn't allocate async queue.\n");
4746 async_q
->size
= PAGE_SIZE
/ sizeof(struct ibmvfc_async_crq
);
4747 async_q
->msg_token
= dma_map_single(dev
, async_q
->msgs
,
4748 async_q
->size
* sizeof(*async_q
->msgs
),
4751 if (dma_mapping_error(dev
, async_q
->msg_token
)) {
4752 dev_err(dev
, "Failed to map async queue\n");
4753 goto free_async_crq
;
4756 vhost
->sg_pool
= dma_pool_create(IBMVFC_NAME
, dev
,
4757 SG_ALL
* sizeof(struct srp_direct_buf
),
4758 sizeof(struct srp_direct_buf
), 0);
4760 if (!vhost
->sg_pool
) {
4761 dev_err(dev
, "Failed to allocate sg pool\n");
4762 goto unmap_async_crq
;
4765 vhost
->login_buf
= dma_alloc_coherent(dev
, sizeof(*vhost
->login_buf
),
4766 &vhost
->login_buf_dma
, GFP_KERNEL
);
4768 if (!vhost
->login_buf
) {
4769 dev_err(dev
, "Couldn't allocate NPIV login buffer\n");
4773 vhost
->disc_buf_sz
= sizeof(vhost
->disc_buf
->scsi_id
[0]) * max_targets
;
4774 vhost
->disc_buf
= dma_alloc_coherent(dev
, vhost
->disc_buf_sz
,
4775 &vhost
->disc_buf_dma
, GFP_KERNEL
);
4777 if (!vhost
->disc_buf
) {
4778 dev_err(dev
, "Couldn't allocate Discover Targets buffer\n");
4779 goto free_login_buffer
;
4782 vhost
->trace
= kcalloc(IBMVFC_NUM_TRACE_ENTRIES
,
4783 sizeof(struct ibmvfc_trace_entry
), GFP_KERNEL
);
4786 goto free_disc_buffer
;
4788 vhost
->tgt_pool
= mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ
,
4789 sizeof(struct ibmvfc_target
));
4791 if (!vhost
->tgt_pool
) {
4792 dev_err(dev
, "Couldn't allocate target memory pool\n");
4800 kfree(vhost
->trace
);
4802 dma_free_coherent(dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
4803 vhost
->disc_buf_dma
);
4805 dma_free_coherent(dev
, sizeof(*vhost
->login_buf
),
4806 vhost
->login_buf
, vhost
->login_buf_dma
);
4808 dma_pool_destroy(vhost
->sg_pool
);
4810 dma_unmap_single(dev
, async_q
->msg_token
,
4811 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
4813 free_page((unsigned long)async_q
->msgs
);
4820 * ibmvfc_rport_add_thread - Worker thread for rport adds
4821 * @work: work struct
4824 static void ibmvfc_rport_add_thread(struct work_struct
*work
)
4826 struct ibmvfc_host
*vhost
= container_of(work
, struct ibmvfc_host
,
4828 struct ibmvfc_target
*tgt
;
4829 struct fc_rport
*rport
;
4830 unsigned long flags
;
4834 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4837 if (vhost
->state
!= IBMVFC_ACTIVE
)
4840 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4841 if (tgt
->add_rport
) {
4844 kref_get(&tgt
->kref
);
4847 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4848 ibmvfc_tgt_add_rport(tgt
);
4849 } else if (get_device(&rport
->dev
)) {
4850 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4851 tgt_dbg(tgt
, "Setting rport roles\n");
4852 fc_remote_port_rolechg(rport
, tgt
->ids
.roles
);
4853 put_device(&rport
->dev
);
4855 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4858 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4859 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4865 if (vhost
->state
== IBMVFC_ACTIVE
)
4866 vhost
->scan_complete
= 1;
4867 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4872 * ibmvfc_probe - Adapter hot plug add entry point
4873 * @vdev: vio device struct
4874 * @id: vio device id struct
4877 * 0 on success / non-zero on failure
4879 static int ibmvfc_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
4881 struct ibmvfc_host
*vhost
;
4882 struct Scsi_Host
*shost
;
4883 struct device
*dev
= &vdev
->dev
;
4887 shost
= scsi_host_alloc(&driver_template
, sizeof(*vhost
));
4889 dev_err(dev
, "Couldn't allocate host data\n");
4893 shost
->transportt
= ibmvfc_transport_template
;
4894 shost
->can_queue
= max_requests
;
4895 shost
->max_lun
= max_lun
;
4896 shost
->max_id
= max_targets
;
4897 shost
->max_sectors
= IBMVFC_MAX_SECTORS
;
4898 shost
->max_cmd_len
= IBMVFC_MAX_CDB_LEN
;
4899 shost
->unique_id
= shost
->host_no
;
4901 vhost
= shost_priv(shost
);
4902 INIT_LIST_HEAD(&vhost
->sent
);
4903 INIT_LIST_HEAD(&vhost
->free
);
4904 INIT_LIST_HEAD(&vhost
->targets
);
4905 sprintf(vhost
->name
, IBMVFC_NAME
);
4906 vhost
->host
= shost
;
4908 vhost
->partition_number
= -1;
4909 vhost
->log_level
= log_level
;
4910 vhost
->task_set
= 1;
4911 strcpy(vhost
->partition_name
, "UNKNOWN");
4912 init_waitqueue_head(&vhost
->work_wait_q
);
4913 init_waitqueue_head(&vhost
->init_wait_q
);
4914 INIT_WORK(&vhost
->rport_add_work_q
, ibmvfc_rport_add_thread
);
4915 mutex_init(&vhost
->passthru_mutex
);
4917 if ((rc
= ibmvfc_alloc_mem(vhost
)))
4918 goto free_scsi_host
;
4920 vhost
->work_thread
= kthread_run(ibmvfc_work
, vhost
, "%s_%d", IBMVFC_NAME
,
4923 if (IS_ERR(vhost
->work_thread
)) {
4924 dev_err(dev
, "Couldn't create kernel thread: %ld\n",
4925 PTR_ERR(vhost
->work_thread
));
4929 if ((rc
= ibmvfc_init_crq(vhost
))) {
4930 dev_err(dev
, "Couldn't initialize crq. rc=%d\n", rc
);
4934 if ((rc
= ibmvfc_init_event_pool(vhost
))) {
4935 dev_err(dev
, "Couldn't initialize event pool. rc=%d\n", rc
);
4939 if ((rc
= scsi_add_host(shost
, dev
)))
4940 goto release_event_pool
;
4942 fc_host_dev_loss_tmo(shost
) = IBMVFC_DEV_LOSS_TMO
;
4944 if ((rc
= ibmvfc_create_trace_file(&shost
->shost_dev
.kobj
,
4945 &ibmvfc_trace_attr
))) {
4946 dev_err(dev
, "Failed to create trace file. rc=%d\n", rc
);
4950 if (shost_to_fc_host(shost
)->rqst_q
)
4951 blk_queue_max_segments(shost_to_fc_host(shost
)->rqst_q
, 1);
4952 dev_set_drvdata(dev
, vhost
);
4953 spin_lock(&ibmvfc_driver_lock
);
4954 list_add_tail(&vhost
->queue
, &ibmvfc_head
);
4955 spin_unlock(&ibmvfc_driver_lock
);
4957 ibmvfc_send_crq_init(vhost
);
4958 scsi_scan_host(shost
);
4962 scsi_remove_host(shost
);
4964 ibmvfc_free_event_pool(vhost
);
4966 ibmvfc_release_crq_queue(vhost
);
4968 kthread_stop(vhost
->work_thread
);
4970 ibmvfc_free_mem(vhost
);
4972 scsi_host_put(shost
);
4979 * ibmvfc_remove - Adapter hot plug remove entry point
4980 * @vdev: vio device struct
4985 static int ibmvfc_remove(struct vio_dev
*vdev
)
4987 struct ibmvfc_host
*vhost
= dev_get_drvdata(&vdev
->dev
);
4988 unsigned long flags
;
4991 ibmvfc_remove_trace_file(&vhost
->host
->shost_dev
.kobj
, &ibmvfc_trace_attr
);
4993 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4994 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
4995 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4997 ibmvfc_wait_while_resetting(vhost
);
4998 ibmvfc_release_crq_queue(vhost
);
4999 kthread_stop(vhost
->work_thread
);
5000 fc_remove_host(vhost
->host
);
5001 scsi_remove_host(vhost
->host
);
5003 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
5004 ibmvfc_purge_requests(vhost
, DID_ERROR
);
5005 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
5006 ibmvfc_free_event_pool(vhost
);
5008 ibmvfc_free_mem(vhost
);
5009 spin_lock(&ibmvfc_driver_lock
);
5010 list_del(&vhost
->queue
);
5011 spin_unlock(&ibmvfc_driver_lock
);
5012 scsi_host_put(vhost
->host
);
5018 * ibmvfc_resume - Resume from suspend
5019 * @dev: device struct
5021 * We may have lost an interrupt across suspend/resume, so kick the
5025 static int ibmvfc_resume(struct device
*dev
)
5027 unsigned long flags
;
5028 struct ibmvfc_host
*vhost
= dev_get_drvdata(dev
);
5029 struct vio_dev
*vdev
= to_vio_dev(dev
);
5031 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
5032 vio_disable_interrupts(vdev
);
5033 tasklet_schedule(&vhost
->tasklet
);
5034 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
5039 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
5040 * @vdev: vio device struct
5043 * Number of bytes the driver will need to DMA map at the same time in
5044 * order to perform well.
5046 static unsigned long ibmvfc_get_desired_dma(struct vio_dev
*vdev
)
5048 unsigned long pool_dma
= max_requests
* sizeof(union ibmvfc_iu
);
5049 return pool_dma
+ ((512 * 1024) * driver_template
.cmd_per_lun
);
5052 static const struct vio_device_id ibmvfc_device_table
[] = {
5053 {"fcp", "IBM,vfc-client"},
5056 MODULE_DEVICE_TABLE(vio
, ibmvfc_device_table
);
5058 static const struct dev_pm_ops ibmvfc_pm_ops
= {
5059 .resume
= ibmvfc_resume
5062 static struct vio_driver ibmvfc_driver
= {
5063 .id_table
= ibmvfc_device_table
,
5064 .probe
= ibmvfc_probe
,
5065 .remove
= ibmvfc_remove
,
5066 .get_desired_dma
= ibmvfc_get_desired_dma
,
5067 .name
= IBMVFC_NAME
,
5068 .pm
= &ibmvfc_pm_ops
,
5071 static struct fc_function_template ibmvfc_transport_functions
= {
5072 .show_host_fabric_name
= 1,
5073 .show_host_node_name
= 1,
5074 .show_host_port_name
= 1,
5075 .show_host_supported_classes
= 1,
5076 .show_host_port_type
= 1,
5077 .show_host_port_id
= 1,
5078 .show_host_maxframe_size
= 1,
5080 .get_host_port_state
= ibmvfc_get_host_port_state
,
5081 .show_host_port_state
= 1,
5083 .get_host_speed
= ibmvfc_get_host_speed
,
5084 .show_host_speed
= 1,
5086 .issue_fc_host_lip
= ibmvfc_issue_fc_host_lip
,
5087 .terminate_rport_io
= ibmvfc_terminate_rport_io
,
5089 .show_rport_maxframe_size
= 1,
5090 .show_rport_supported_classes
= 1,
5092 .set_rport_dev_loss_tmo
= ibmvfc_set_rport_dev_loss_tmo
,
5093 .show_rport_dev_loss_tmo
= 1,
5095 .get_starget_node_name
= ibmvfc_get_starget_node_name
,
5096 .show_starget_node_name
= 1,
5098 .get_starget_port_name
= ibmvfc_get_starget_port_name
,
5099 .show_starget_port_name
= 1,
5101 .get_starget_port_id
= ibmvfc_get_starget_port_id
,
5102 .show_starget_port_id
= 1,
5104 .bsg_request
= ibmvfc_bsg_request
,
5105 .bsg_timeout
= ibmvfc_bsg_timeout
,
5109 * ibmvfc_module_init - Initialize the ibmvfc module
5112 * 0 on success / other on failure
5114 static int __init
ibmvfc_module_init(void)
5118 if (!firmware_has_feature(FW_FEATURE_VIO
))
5121 printk(KERN_INFO IBMVFC_NAME
": IBM Virtual Fibre Channel Driver version: %s %s\n",
5122 IBMVFC_DRIVER_VERSION
, IBMVFC_DRIVER_DATE
);
5124 ibmvfc_transport_template
= fc_attach_transport(&ibmvfc_transport_functions
);
5125 if (!ibmvfc_transport_template
)
5128 rc
= vio_register_driver(&ibmvfc_driver
);
5130 fc_release_transport(ibmvfc_transport_template
);
5135 * ibmvfc_module_exit - Teardown the ibmvfc module
5140 static void __exit
ibmvfc_module_exit(void)
5142 vio_unregister_driver(&ibmvfc_driver
);
5143 fc_release_transport(ibmvfc_transport_template
);
5146 module_init(ibmvfc_module_init
);
5147 module_exit(ibmvfc_module_exit
);