2 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
4 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
6 * Copyright (C) IBM Corporation, 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmapool.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/kthread.h>
32 #include <linux/stringify.h>
33 #include <asm/firmware.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_tcq.h>
41 #include <scsi/scsi_transport_fc.h>
44 static unsigned int init_timeout
= IBMVFC_INIT_TIMEOUT
;
45 static unsigned int default_timeout
= IBMVFC_DEFAULT_TIMEOUT
;
46 static unsigned int max_lun
= IBMVFC_MAX_LUN
;
47 static unsigned int max_targets
= IBMVFC_MAX_TARGETS
;
48 static unsigned int max_requests
= IBMVFC_MAX_REQUESTS_DEFAULT
;
49 static unsigned int disc_threads
= IBMVFC_MAX_DISC_THREADS
;
50 static unsigned int dev_loss_tmo
= IBMVFC_DEV_LOSS_TMO
;
51 static unsigned int ibmvfc_debug
= IBMVFC_DEBUG
;
52 static unsigned int log_level
= IBMVFC_DEFAULT_LOG_LEVEL
;
53 static LIST_HEAD(ibmvfc_head
);
54 static DEFINE_SPINLOCK(ibmvfc_driver_lock
);
55 static struct scsi_transport_template
*ibmvfc_transport_template
;
57 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(IBMVFC_DRIVER_VERSION
);
62 module_param_named(init_timeout
, init_timeout
, uint
, S_IRUGO
| S_IWUSR
);
63 MODULE_PARM_DESC(init_timeout
, "Initialization timeout in seconds. "
64 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT
) "]");
65 module_param_named(default_timeout
, default_timeout
, uint
, S_IRUGO
| S_IWUSR
);
66 MODULE_PARM_DESC(default_timeout
,
67 "Default timeout in seconds for initialization and EH commands. "
68 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT
) "]");
69 module_param_named(max_requests
, max_requests
, uint
, S_IRUGO
);
70 MODULE_PARM_DESC(max_requests
, "Maximum requests for this adapter. "
71 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT
) "]");
72 module_param_named(max_lun
, max_lun
, uint
, S_IRUGO
);
73 MODULE_PARM_DESC(max_lun
, "Maximum allowed LUN. "
74 "[Default=" __stringify(IBMVFC_MAX_LUN
) "]");
75 module_param_named(max_targets
, max_targets
, uint
, S_IRUGO
);
76 MODULE_PARM_DESC(max_targets
, "Maximum allowed targets. "
77 "[Default=" __stringify(IBMVFC_MAX_TARGETS
) "]");
78 module_param_named(disc_threads
, disc_threads
, uint
, S_IRUGO
| S_IWUSR
);
79 MODULE_PARM_DESC(disc_threads
, "Number of device discovery threads to use. "
80 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS
) "]");
81 module_param_named(debug
, ibmvfc_debug
, uint
, S_IRUGO
| S_IWUSR
);
82 MODULE_PARM_DESC(debug
, "Enable driver debug information. "
83 "[Default=" __stringify(IBMVFC_DEBUG
) "]");
84 module_param_named(dev_loss_tmo
, dev_loss_tmo
, uint
, S_IRUGO
| S_IWUSR
);
85 MODULE_PARM_DESC(dev_loss_tmo
, "Maximum number of seconds that the FC "
86 "transport should insulate the loss of a remote port. Once this "
87 "value is exceeded, the scsi target is removed. "
88 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO
) "]");
89 module_param_named(log_level
, log_level
, uint
, 0);
90 MODULE_PARM_DESC(log_level
, "Set to 0 - 4 for increasing verbosity of device driver. "
91 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL
) "]");
101 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_ESTABLISH
, DID_ERROR
, 1, 1, "unable to establish" },
102 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_FAULT
, DID_OK
, 1, 0, "transport fault" },
103 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CMD_TIMEOUT
, DID_TIME_OUT
, 1, 1, "command timeout" },
104 { IBMVFC_FABRIC_MAPPED
, IBMVFC_ENETDOWN
, DID_NO_CONNECT
, 1, 1, "network down" },
105 { IBMVFC_FABRIC_MAPPED
, IBMVFC_HW_FAILURE
, DID_ERROR
, 1, 1, "hardware failure" },
106 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DOWN_ERR
, DID_REQUEUE
, 0, 0, "link down" },
107 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DEAD_ERR
, DID_ERROR
, 0, 0, "link dead" },
108 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_REGISTER
, DID_ERROR
, 1, 1, "unable to register" },
109 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_BUSY
, DID_BUS_BUSY
, 1, 0, "transport busy" },
110 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_DEAD
, DID_ERROR
, 0, 1, "transport dead" },
111 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CONFIG_ERROR
, DID_ERROR
, 1, 1, "configuration error" },
112 { IBMVFC_FABRIC_MAPPED
, IBMVFC_NAME_SERVER_FAIL
, DID_ERROR
, 1, 1, "name server failure" },
113 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_HALTED
, DID_REQUEUE
, 0, 0, "link halted" },
114 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_GENERAL
, DID_OK
, 1, 0, "general transport error" },
116 { IBMVFC_VIOS_FAILURE
, IBMVFC_CRQ_FAILURE
, DID_REQUEUE
, 1, 1, "CRQ failure" },
117 { IBMVFC_VIOS_FAILURE
, IBMVFC_SW_FAILURE
, DID_ERROR
, 0, 1, "software failure" },
118 { IBMVFC_VIOS_FAILURE
, IBMVFC_INVALID_PARAMETER
, DID_ABORT
, 0, 1, "invalid parameter" },
119 { IBMVFC_VIOS_FAILURE
, IBMVFC_MISSING_PARAMETER
, DID_ABORT
, 0, 1, "missing parameter" },
120 { IBMVFC_VIOS_FAILURE
, IBMVFC_HOST_IO_BUS
, DID_ERROR
, 1, 1, "host I/O bus failure" },
121 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED
, DID_ABORT
, 0, 1, "transaction cancelled" },
122 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED_IMPLICIT
, DID_ABORT
, 0, 1, "transaction cancelled implicit" },
123 { IBMVFC_VIOS_FAILURE
, IBMVFC_INSUFFICIENT_RESOURCE
, DID_REQUEUE
, 1, 1, "insufficient resources" },
124 { IBMVFC_VIOS_FAILURE
, IBMVFC_COMMAND_FAILED
, DID_ERROR
, 1, 1, "command failed" },
126 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_ELS_CMD_CODE
, DID_ERROR
, 0, 1, "invalid ELS command code" },
127 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_VERSION
, DID_ERROR
, 0, 1, "invalid version level" },
128 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_ERROR
, DID_ERROR
, 1, 1, "logical error" },
129 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_CT_IU_SIZE
, DID_ERROR
, 0, 1, "invalid CT_IU size" },
130 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_BUSY
, DID_REQUEUE
, 1, 0, "logical busy" },
131 { IBMVFC_FC_FAILURE
, IBMVFC_PROTOCOL_ERROR
, DID_ERROR
, 1, 1, "protocol error" },
132 { IBMVFC_FC_FAILURE
, IBMVFC_UNABLE_TO_PERFORM_REQ
, DID_ERROR
, 1, 1, "unable to perform request" },
133 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_NOT_SUPPORTED
, DID_ERROR
, 0, 0, "command not supported" },
134 { IBMVFC_FC_FAILURE
, IBMVFC_SERVER_NOT_AVAIL
, DID_ERROR
, 0, 1, "server not available" },
135 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_IN_PROGRESS
, DID_ERROR
, 0, 1, "command already in progress" },
136 { IBMVFC_FC_FAILURE
, IBMVFC_VENDOR_SPECIFIC
, DID_ERROR
, 1, 1, "vendor specific" },
138 { IBMVFC_FC_SCSI_ERROR
, 0, DID_OK
, 1, 0, "SCSI error" },
141 static void ibmvfc_npiv_login(struct ibmvfc_host
*);
142 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*);
143 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*);
144 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*);
146 static const char *unknown_error
= "unknown error";
148 #ifdef CONFIG_SCSI_IBMVFC_TRACE
150 * ibmvfc_trc_start - Log a start trace entry
151 * @evt: ibmvfc event struct
154 static void ibmvfc_trc_start(struct ibmvfc_event
*evt
)
156 struct ibmvfc_host
*vhost
= evt
->vhost
;
157 struct ibmvfc_cmd
*vfc_cmd
= &evt
->iu
.cmd
;
158 struct ibmvfc_mad_common
*mad
= &evt
->iu
.mad_common
;
159 struct ibmvfc_trace_entry
*entry
;
161 entry
= &vhost
->trace
[vhost
->trace_index
++];
163 entry
->time
= jiffies
;
164 entry
->fmt
= evt
->crq
.format
;
165 entry
->type
= IBMVFC_TRC_START
;
167 switch (entry
->fmt
) {
168 case IBMVFC_CMD_FORMAT
:
169 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
170 entry
->scsi_id
= vfc_cmd
->tgt_scsi_id
;
171 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
172 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
173 entry
->u
.start
.xfer_len
= vfc_cmd
->iu
.xfer_len
;
175 case IBMVFC_MAD_FORMAT
:
176 entry
->op_code
= mad
->opcode
;
184 * ibmvfc_trc_end - Log an end trace entry
185 * @evt: ibmvfc event struct
188 static void ibmvfc_trc_end(struct ibmvfc_event
*evt
)
190 struct ibmvfc_host
*vhost
= evt
->vhost
;
191 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
192 struct ibmvfc_mad_common
*mad
= &evt
->xfer_iu
->mad_common
;
193 struct ibmvfc_trace_entry
*entry
= &vhost
->trace
[vhost
->trace_index
++];
196 entry
->time
= jiffies
;
197 entry
->fmt
= evt
->crq
.format
;
198 entry
->type
= IBMVFC_TRC_END
;
200 switch (entry
->fmt
) {
201 case IBMVFC_CMD_FORMAT
:
202 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
203 entry
->scsi_id
= vfc_cmd
->tgt_scsi_id
;
204 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
205 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
206 entry
->u
.end
.status
= vfc_cmd
->status
;
207 entry
->u
.end
.error
= vfc_cmd
->error
;
208 entry
->u
.end
.fcp_rsp_flags
= vfc_cmd
->rsp
.flags
;
209 entry
->u
.end
.rsp_code
= vfc_cmd
->rsp
.data
.info
.rsp_code
;
210 entry
->u
.end
.scsi_status
= vfc_cmd
->rsp
.scsi_status
;
212 case IBMVFC_MAD_FORMAT
:
213 entry
->op_code
= mad
->opcode
;
214 entry
->u
.end
.status
= mad
->status
;
223 #define ibmvfc_trc_start(evt) do { } while (0)
224 #define ibmvfc_trc_end(evt) do { } while (0)
228 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
229 * @status: status / error class
233 * index into cmd_status / -EINVAL on failure
235 static int ibmvfc_get_err_index(u16 status
, u16 error
)
239 for (i
= 0; i
< ARRAY_SIZE(cmd_status
); i
++)
240 if ((cmd_status
[i
].status
& status
) == cmd_status
[i
].status
&&
241 cmd_status
[i
].error
== error
)
248 * ibmvfc_get_cmd_error - Find the error description for the fcp response
249 * @status: status / error class
253 * error description string
255 static const char *ibmvfc_get_cmd_error(u16 status
, u16 error
)
257 int rc
= ibmvfc_get_err_index(status
, error
);
259 return cmd_status
[rc
].name
;
260 return unknown_error
;
264 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
265 * @vfc_cmd: ibmvfc command struct
268 * SCSI result value to return for completed command
270 static int ibmvfc_get_err_result(struct ibmvfc_cmd
*vfc_cmd
)
273 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
274 int fc_rsp_len
= rsp
->fcp_rsp_len
;
276 if ((rsp
->flags
& FCP_RSP_LEN_VALID
) &&
277 ((!fc_rsp_len
&& fc_rsp_len
!= 4 && fc_rsp_len
!= 8) ||
278 rsp
->data
.info
.rsp_code
))
279 return DID_ERROR
<< 16;
281 if (!vfc_cmd
->status
) {
282 if (rsp
->flags
& FCP_RESID_OVER
)
283 return rsp
->scsi_status
| (DID_ERROR
<< 16);
285 return rsp
->scsi_status
| (DID_OK
<< 16);
288 err
= ibmvfc_get_err_index(vfc_cmd
->status
, vfc_cmd
->error
);
290 return rsp
->scsi_status
| (cmd_status
[err
].result
<< 16);
291 return rsp
->scsi_status
| (DID_ERROR
<< 16);
295 * ibmvfc_retry_cmd - Determine if error status is retryable
296 * @status: status / error class
300 * 1 if error should be retried / 0 if it should not
302 static int ibmvfc_retry_cmd(u16 status
, u16 error
)
304 int rc
= ibmvfc_get_err_index(status
, error
);
307 return cmd_status
[rc
].retry
;
311 static const char *unknown_fc_explain
= "unknown fc explain";
313 static const struct {
317 { 0x00, "no additional explanation" },
318 { 0x01, "service parameter error - options" },
319 { 0x03, "service parameter error - initiator control" },
320 { 0x05, "service parameter error - recipient control" },
321 { 0x07, "service parameter error - received data field size" },
322 { 0x09, "service parameter error - concurrent seq" },
323 { 0x0B, "service parameter error - credit" },
324 { 0x0D, "invalid N_Port/F_Port_Name" },
325 { 0x0E, "invalid node/Fabric Name" },
326 { 0x0F, "invalid common service parameters" },
327 { 0x11, "invalid association header" },
328 { 0x13, "association header required" },
329 { 0x15, "invalid originator S_ID" },
330 { 0x17, "invalid OX_ID-RX-ID combination" },
331 { 0x19, "command (request) already in progress" },
332 { 0x1E, "N_Port Login requested" },
333 { 0x1F, "Invalid N_Port_ID" },
336 static const struct {
340 { 0x00, "no additional explanation" },
341 { 0x01, "port identifier not registered" },
342 { 0x02, "port name not registered" },
343 { 0x03, "node name not registered" },
344 { 0x04, "class of service not registered" },
345 { 0x06, "initial process associator not registered" },
346 { 0x07, "FC-4 TYPEs not registered" },
347 { 0x08, "symbolic port name not registered" },
348 { 0x09, "symbolic node name not registered" },
349 { 0x0A, "port type not registered" },
350 { 0xF0, "authorization exception" },
351 { 0xF1, "authentication exception" },
352 { 0xF2, "data base full" },
353 { 0xF3, "data base empty" },
354 { 0xF4, "processing request" },
355 { 0xF5, "unable to verify connection" },
356 { 0xF6, "devices not in a common zone" },
360 * ibmvfc_get_ls_explain - Return the FC Explain description text
361 * @status: FC Explain status
366 static const char *ibmvfc_get_ls_explain(u16 status
)
370 for (i
= 0; i
< ARRAY_SIZE(ls_explain
); i
++)
371 if (ls_explain
[i
].fc_explain
== status
)
372 return ls_explain
[i
].name
;
374 return unknown_fc_explain
;
378 * ibmvfc_get_gs_explain - Return the FC Explain description text
379 * @status: FC Explain status
384 static const char *ibmvfc_get_gs_explain(u16 status
)
388 for (i
= 0; i
< ARRAY_SIZE(gs_explain
); i
++)
389 if (gs_explain
[i
].fc_explain
== status
)
390 return gs_explain
[i
].name
;
392 return unknown_fc_explain
;
395 static const struct {
396 enum ibmvfc_fc_type fc_type
;
399 { IBMVFC_FABRIC_REJECT
, "fabric reject" },
400 { IBMVFC_PORT_REJECT
, "port reject" },
401 { IBMVFC_LS_REJECT
, "ELS reject" },
402 { IBMVFC_FABRIC_BUSY
, "fabric busy" },
403 { IBMVFC_PORT_BUSY
, "port busy" },
404 { IBMVFC_BASIC_REJECT
, "basic reject" },
407 static const char *unknown_fc_type
= "unknown fc type";
410 * ibmvfc_get_fc_type - Return the FC Type description text
411 * @status: FC Type error status
416 static const char *ibmvfc_get_fc_type(u16 status
)
420 for (i
= 0; i
< ARRAY_SIZE(fc_type
); i
++)
421 if (fc_type
[i
].fc_type
== status
)
422 return fc_type
[i
].name
;
424 return unknown_fc_type
;
428 * ibmvfc_set_tgt_action - Set the next init action for the target
429 * @tgt: ibmvfc target struct
430 * @action: action to perform
433 static void ibmvfc_set_tgt_action(struct ibmvfc_target
*tgt
,
434 enum ibmvfc_target_action action
)
436 switch (tgt
->action
) {
437 case IBMVFC_TGT_ACTION_DEL_RPORT
:
440 tgt
->action
= action
;
446 * ibmvfc_set_host_state - Set the state for the host
447 * @vhost: ibmvfc host struct
448 * @state: state to set host to
451 * 0 if state changed / non-zero if not changed
453 static int ibmvfc_set_host_state(struct ibmvfc_host
*vhost
,
454 enum ibmvfc_host_state state
)
458 switch (vhost
->state
) {
459 case IBMVFC_HOST_OFFLINE
:
463 vhost
->state
= state
;
471 * ibmvfc_set_host_action - Set the next init action for the host
472 * @vhost: ibmvfc host struct
473 * @action: action to perform
476 static void ibmvfc_set_host_action(struct ibmvfc_host
*vhost
,
477 enum ibmvfc_host_action action
)
480 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
481 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
)
482 vhost
->action
= action
;
484 case IBMVFC_HOST_ACTION_INIT_WAIT
:
485 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT
)
486 vhost
->action
= action
;
488 case IBMVFC_HOST_ACTION_QUERY
:
489 switch (vhost
->action
) {
490 case IBMVFC_HOST_ACTION_INIT_WAIT
:
491 case IBMVFC_HOST_ACTION_NONE
:
492 case IBMVFC_HOST_ACTION_TGT_ADD
:
493 vhost
->action
= action
;
499 case IBMVFC_HOST_ACTION_TGT_INIT
:
500 if (vhost
->action
== IBMVFC_HOST_ACTION_ALLOC_TGTS
)
501 vhost
->action
= action
;
503 case IBMVFC_HOST_ACTION_INIT
:
504 case IBMVFC_HOST_ACTION_TGT_DEL
:
505 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
506 case IBMVFC_HOST_ACTION_TGT_ADD
:
507 case IBMVFC_HOST_ACTION_NONE
:
509 vhost
->action
= action
;
515 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
516 * @vhost: ibmvfc host struct
521 static void ibmvfc_reinit_host(struct ibmvfc_host
*vhost
)
523 if (vhost
->action
== IBMVFC_HOST_ACTION_NONE
) {
524 scsi_block_requests(vhost
->host
);
525 ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
);
526 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
530 wake_up(&vhost
->work_wait_q
);
534 * ibmvfc_link_down - Handle a link down event from the adapter
535 * @vhost: ibmvfc host struct
536 * @state: ibmvfc host state to enter
539 static void ibmvfc_link_down(struct ibmvfc_host
*vhost
,
540 enum ibmvfc_host_state state
)
542 struct ibmvfc_target
*tgt
;
545 scsi_block_requests(vhost
->host
);
546 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
547 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
548 ibmvfc_set_host_state(vhost
, state
);
549 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
550 vhost
->events_to_log
|= IBMVFC_AE_LINKDOWN
;
551 wake_up(&vhost
->work_wait_q
);
556 * ibmvfc_init_host - Start host initialization
557 * @vhost: ibmvfc host struct
562 static void ibmvfc_init_host(struct ibmvfc_host
*vhost
)
564 struct ibmvfc_target
*tgt
;
566 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
567 if (++vhost
->init_retries
> IBMVFC_MAX_INIT_RETRIES
) {
569 "Host initialization retries exceeded. Taking adapter offline\n");
570 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
575 if (!ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
576 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
578 scsi_block_requests(vhost
->host
);
579 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
580 vhost
->job_step
= ibmvfc_npiv_login
;
581 wake_up(&vhost
->work_wait_q
);
586 * ibmvfc_send_crq - Send a CRQ
587 * @vhost: ibmvfc host struct
588 * @word1: the first 64 bits of the data
589 * @word2: the second 64 bits of the data
592 * 0 on success / other on failure
594 static int ibmvfc_send_crq(struct ibmvfc_host
*vhost
, u64 word1
, u64 word2
)
596 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
597 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, word1
, word2
);
601 * ibmvfc_send_crq_init - Send a CRQ init message
602 * @vhost: ibmvfc host struct
605 * 0 on success / other on failure
607 static int ibmvfc_send_crq_init(struct ibmvfc_host
*vhost
)
609 ibmvfc_dbg(vhost
, "Sending CRQ init\n");
610 return ibmvfc_send_crq(vhost
, 0xC001000000000000LL
, 0);
614 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
615 * @vhost: ibmvfc host struct
618 * 0 on success / other on failure
620 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host
*vhost
)
622 ibmvfc_dbg(vhost
, "Sending CRQ init complete\n");
623 return ibmvfc_send_crq(vhost
, 0xC002000000000000LL
, 0);
627 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
628 * @vhost: ibmvfc host struct
630 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
631 * the crq with the hypervisor.
633 static void ibmvfc_release_crq_queue(struct ibmvfc_host
*vhost
)
636 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
637 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
639 ibmvfc_dbg(vhost
, "Releasing CRQ\n");
640 free_irq(vdev
->irq
, vhost
);
642 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
643 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
645 vhost
->state
= IBMVFC_NO_CRQ
;
646 dma_unmap_single(vhost
->dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
647 free_page((unsigned long)crq
->msgs
);
651 * ibmvfc_reenable_crq_queue - reenables the CRQ
652 * @vhost: ibmvfc host struct
655 * 0 on success / other on failure
657 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host
*vhost
)
660 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
662 /* Re-enable the CRQ */
664 rc
= plpar_hcall_norets(H_ENABLE_CRQ
, vdev
->unit_address
);
665 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
668 dev_err(vhost
->dev
, "Error enabling adapter (rc=%d)\n", rc
);
674 * ibmvfc_reset_crq - resets a crq after a failure
675 * @vhost: ibmvfc host struct
678 * 0 on success / other on failure
680 static int ibmvfc_reset_crq(struct ibmvfc_host
*vhost
)
683 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
684 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
688 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
689 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
691 vhost
->state
= IBMVFC_NO_CRQ
;
692 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
694 /* Clean out the queue */
695 memset(crq
->msgs
, 0, PAGE_SIZE
);
698 /* And re-open it again */
699 rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
700 crq
->msg_token
, PAGE_SIZE
);
703 /* Adapter is good, but other end is not ready */
704 dev_warn(vhost
->dev
, "Partner adapter not ready\n");
706 dev_warn(vhost
->dev
, "Couldn't register crq (rc=%d)\n", rc
);
712 * ibmvfc_valid_event - Determines if event is valid.
713 * @pool: event_pool that contains the event
714 * @evt: ibmvfc event to be checked for validity
717 * 1 if event is valid / 0 if event is not valid
719 static int ibmvfc_valid_event(struct ibmvfc_event_pool
*pool
,
720 struct ibmvfc_event
*evt
)
722 int index
= evt
- pool
->events
;
723 if (index
< 0 || index
>= pool
->size
) /* outside of bounds */
725 if (evt
!= pool
->events
+ index
) /* unaligned */
731 * ibmvfc_free_event - Free the specified event
732 * @evt: ibmvfc_event to be freed
735 static void ibmvfc_free_event(struct ibmvfc_event
*evt
)
737 struct ibmvfc_host
*vhost
= evt
->vhost
;
738 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
740 BUG_ON(!ibmvfc_valid_event(pool
, evt
));
741 BUG_ON(atomic_inc_return(&evt
->free
) != 1);
742 list_add_tail(&evt
->queue
, &vhost
->free
);
746 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
747 * @evt: ibmvfc event struct
749 * This function does not setup any error status, that must be done
750 * before this function gets called.
752 static void ibmvfc_scsi_eh_done(struct ibmvfc_event
*evt
)
754 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
757 scsi_dma_unmap(cmnd
);
758 cmnd
->scsi_done(cmnd
);
761 ibmvfc_free_event(evt
);
765 * ibmvfc_fail_request - Fail request with specified error code
766 * @evt: ibmvfc event struct
767 * @error_code: error code to fail request with
772 static void ibmvfc_fail_request(struct ibmvfc_event
*evt
, int error_code
)
775 evt
->cmnd
->result
= (error_code
<< 16);
776 evt
->done
= ibmvfc_scsi_eh_done
;
778 evt
->xfer_iu
->mad_common
.status
= IBMVFC_MAD_DRIVER_FAILED
;
780 list_del(&evt
->queue
);
781 del_timer(&evt
->timer
);
787 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
788 * @vhost: ibmvfc host struct
789 * @error_code: error code to fail requests with
794 static void ibmvfc_purge_requests(struct ibmvfc_host
*vhost
, int error_code
)
796 struct ibmvfc_event
*evt
, *pos
;
798 ibmvfc_dbg(vhost
, "Purging all requests\n");
799 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
)
800 ibmvfc_fail_request(evt
, error_code
);
804 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
805 * @vhost: struct ibmvfc host to reset
807 static void __ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
811 scsi_block_requests(vhost
->host
);
812 ibmvfc_purge_requests(vhost
, DID_ERROR
);
813 if ((rc
= ibmvfc_reset_crq(vhost
)) ||
814 (rc
= ibmvfc_send_crq_init(vhost
)) ||
815 (rc
= vio_enable_interrupts(to_vio_dev(vhost
->dev
)))) {
816 dev_err(vhost
->dev
, "Error after reset rc=%d\n", rc
);
817 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
819 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
823 * ibmvfc_reset_host - Reset the connection to the server
824 * @vhost: struct ibmvfc host to reset
826 static void ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
830 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
831 __ibmvfc_reset_host(vhost
);
832 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
836 * ibmvfc_retry_host_init - Retry host initialization if allowed
837 * @vhost: ibmvfc host struct
840 static void ibmvfc_retry_host_init(struct ibmvfc_host
*vhost
)
842 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
843 if (++vhost
->init_retries
> IBMVFC_MAX_INIT_RETRIES
) {
845 "Host initialization retries exceeded. Taking adapter offline\n");
846 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
847 } else if (vhost
->init_retries
== IBMVFC_MAX_INIT_RETRIES
)
848 __ibmvfc_reset_host(vhost
);
850 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
853 wake_up(&vhost
->work_wait_q
);
857 * __ibmvfc_find_target - Find the specified scsi_target (no locking)
858 * @starget: scsi target struct
861 * ibmvfc_target struct / NULL if not found
863 static struct ibmvfc_target
*__ibmvfc_find_target(struct scsi_target
*starget
)
865 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
866 struct ibmvfc_host
*vhost
= shost_priv(shost
);
867 struct ibmvfc_target
*tgt
;
869 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
870 if (tgt
->target_id
== starget
->id
)
876 * ibmvfc_find_target - Find the specified scsi_target
877 * @starget: scsi target struct
880 * ibmvfc_target struct / NULL if not found
882 static struct ibmvfc_target
*ibmvfc_find_target(struct scsi_target
*starget
)
884 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
885 struct ibmvfc_target
*tgt
;
888 spin_lock_irqsave(shost
->host_lock
, flags
);
889 tgt
= __ibmvfc_find_target(starget
);
890 spin_unlock_irqrestore(shost
->host_lock
, flags
);
895 * ibmvfc_get_host_speed - Get host port speed
896 * @shost: scsi host struct
901 static void ibmvfc_get_host_speed(struct Scsi_Host
*shost
)
903 struct ibmvfc_host
*vhost
= shost_priv(shost
);
906 spin_lock_irqsave(shost
->host_lock
, flags
);
907 if (vhost
->state
== IBMVFC_ACTIVE
) {
908 switch (vhost
->login_buf
->resp
.link_speed
/ 100) {
910 fc_host_speed(shost
) = FC_PORTSPEED_1GBIT
;
913 fc_host_speed(shost
) = FC_PORTSPEED_2GBIT
;
916 fc_host_speed(shost
) = FC_PORTSPEED_4GBIT
;
919 fc_host_speed(shost
) = FC_PORTSPEED_8GBIT
;
922 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
925 fc_host_speed(shost
) = FC_PORTSPEED_16GBIT
;
928 ibmvfc_log(vhost
, 3, "Unknown port speed: %ld Gbit\n",
929 vhost
->login_buf
->resp
.link_speed
/ 100);
930 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
934 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
935 spin_unlock_irqrestore(shost
->host_lock
, flags
);
939 * ibmvfc_get_host_port_state - Get host port state
940 * @shost: scsi host struct
945 static void ibmvfc_get_host_port_state(struct Scsi_Host
*shost
)
947 struct ibmvfc_host
*vhost
= shost_priv(shost
);
950 spin_lock_irqsave(shost
->host_lock
, flags
);
951 switch (vhost
->state
) {
952 case IBMVFC_INITIALIZING
:
954 fc_host_port_state(shost
) = FC_PORTSTATE_ONLINE
;
956 case IBMVFC_LINK_DOWN
:
957 fc_host_port_state(shost
) = FC_PORTSTATE_LINKDOWN
;
959 case IBMVFC_LINK_DEAD
:
960 case IBMVFC_HOST_OFFLINE
:
961 fc_host_port_state(shost
) = FC_PORTSTATE_OFFLINE
;
964 fc_host_port_state(shost
) = FC_PORTSTATE_BLOCKED
;
967 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
970 ibmvfc_log(vhost
, 3, "Unknown port state: %d\n", vhost
->state
);
971 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
974 spin_unlock_irqrestore(shost
->host_lock
, flags
);
978 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
979 * @rport: rport struct
980 * @timeout: timeout value
985 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
988 rport
->dev_loss_tmo
= timeout
;
990 rport
->dev_loss_tmo
= 1;
994 * ibmvfc_get_starget_node_name - Get SCSI target's node name
995 * @starget: scsi target struct
1000 static void ibmvfc_get_starget_node_name(struct scsi_target
*starget
)
1002 struct ibmvfc_target
*tgt
= ibmvfc_find_target(starget
);
1003 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.node_name
: 0;
1007 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1008 * @starget: scsi target struct
1013 static void ibmvfc_get_starget_port_name(struct scsi_target
*starget
)
1015 struct ibmvfc_target
*tgt
= ibmvfc_find_target(starget
);
1016 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.port_name
: 0;
1020 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1021 * @starget: scsi target struct
1026 static void ibmvfc_get_starget_port_id(struct scsi_target
*starget
)
1028 struct ibmvfc_target
*tgt
= ibmvfc_find_target(starget
);
1029 fc_starget_port_id(starget
) = tgt
? tgt
->scsi_id
: -1;
1033 * ibmvfc_wait_while_resetting - Wait while the host resets
1034 * @vhost: ibmvfc host struct
1037 * 0 on success / other on failure
1039 static int ibmvfc_wait_while_resetting(struct ibmvfc_host
*vhost
)
1041 long timeout
= wait_event_timeout(vhost
->init_wait_q
,
1042 (vhost
->state
== IBMVFC_ACTIVE
||
1043 vhost
->state
== IBMVFC_HOST_OFFLINE
||
1044 vhost
->state
== IBMVFC_LINK_DEAD
),
1045 (init_timeout
* HZ
));
1047 return timeout
? 0 : -EIO
;
1051 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1052 * @shost: scsi host struct
1055 * 0 on success / other on failure
1057 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host
*shost
)
1059 struct ibmvfc_host
*vhost
= shost_priv(shost
);
1061 dev_err(vhost
->dev
, "Initiating host LIP. Resetting connection\n");
1062 ibmvfc_reset_host(vhost
);
1063 return ibmvfc_wait_while_resetting(vhost
);
1067 * ibmvfc_gather_partition_info - Gather info about the LPAR
1072 static void ibmvfc_gather_partition_info(struct ibmvfc_host
*vhost
)
1074 struct device_node
*rootdn
;
1076 const unsigned int *num
;
1078 rootdn
= of_find_node_by_path("/");
1082 name
= of_get_property(rootdn
, "ibm,partition-name", NULL
);
1084 strncpy(vhost
->partition_name
, name
, sizeof(vhost
->partition_name
));
1085 num
= of_get_property(rootdn
, "ibm,partition-no", NULL
);
1087 vhost
->partition_number
= *num
;
1088 of_node_put(rootdn
);
1092 * ibmvfc_set_login_info - Setup info for NPIV login
1093 * @vhost: ibmvfc host struct
1098 static void ibmvfc_set_login_info(struct ibmvfc_host
*vhost
)
1100 struct ibmvfc_npiv_login
*login_info
= &vhost
->login_info
;
1101 struct device_node
*of_node
= vhost
->dev
->archdata
.of_node
;
1102 const char *location
;
1104 memset(login_info
, 0, sizeof(*login_info
));
1106 login_info
->ostype
= IBMVFC_OS_LINUX
;
1107 login_info
->max_dma_len
= IBMVFC_MAX_SECTORS
<< 9;
1108 login_info
->max_payload
= sizeof(struct ibmvfc_fcp_cmd_iu
);
1109 login_info
->max_response
= sizeof(struct ibmvfc_fcp_rsp
);
1110 login_info
->partition_num
= vhost
->partition_number
;
1111 login_info
->vfc_frame_version
= 1;
1112 login_info
->fcp_version
= 3;
1113 if (vhost
->client_migrated
)
1114 login_info
->flags
= IBMVFC_CLIENT_MIGRATED
;
1116 login_info
->max_cmds
= max_requests
+ IBMVFC_NUM_INTERNAL_REQ
;
1117 login_info
->capabilities
= IBMVFC_CAN_MIGRATE
;
1118 login_info
->async
.va
= vhost
->async_crq
.msg_token
;
1119 login_info
->async
.len
= vhost
->async_crq
.size
;
1120 strncpy(login_info
->partition_name
, vhost
->partition_name
, IBMVFC_MAX_NAME
);
1121 strncpy(login_info
->device_name
,
1122 vhost
->host
->shost_gendev
.bus_id
, IBMVFC_MAX_NAME
);
1124 location
= of_get_property(of_node
, "ibm,loc-code", NULL
);
1125 location
= location
? location
: vhost
->dev
->bus_id
;
1126 strncpy(login_info
->drc_name
, location
, IBMVFC_MAX_NAME
);
1130 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1131 * @vhost: ibmvfc host who owns the event pool
1133 * Returns zero on success.
1135 static int ibmvfc_init_event_pool(struct ibmvfc_host
*vhost
)
1138 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1141 pool
->size
= max_requests
+ IBMVFC_NUM_INTERNAL_REQ
;
1142 pool
->events
= kcalloc(pool
->size
, sizeof(*pool
->events
), GFP_KERNEL
);
1146 pool
->iu_storage
= dma_alloc_coherent(vhost
->dev
,
1147 pool
->size
* sizeof(*pool
->iu_storage
),
1148 &pool
->iu_token
, 0);
1150 if (!pool
->iu_storage
) {
1151 kfree(pool
->events
);
1155 for (i
= 0; i
< pool
->size
; ++i
) {
1156 struct ibmvfc_event
*evt
= &pool
->events
[i
];
1157 atomic_set(&evt
->free
, 1);
1158 evt
->crq
.valid
= 0x80;
1159 evt
->crq
.ioba
= pool
->iu_token
+ (sizeof(*evt
->xfer_iu
) * i
);
1160 evt
->xfer_iu
= pool
->iu_storage
+ i
;
1162 evt
->ext_list
= NULL
;
1163 list_add_tail(&evt
->queue
, &vhost
->free
);
1171 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1172 * @vhost: ibmvfc host who owns the event pool
1175 static void ibmvfc_free_event_pool(struct ibmvfc_host
*vhost
)
1178 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1181 for (i
= 0; i
< pool
->size
; ++i
) {
1182 list_del(&pool
->events
[i
].queue
);
1183 BUG_ON(atomic_read(&pool
->events
[i
].free
) != 1);
1184 if (pool
->events
[i
].ext_list
)
1185 dma_pool_free(vhost
->sg_pool
,
1186 pool
->events
[i
].ext_list
,
1187 pool
->events
[i
].ext_list_token
);
1190 kfree(pool
->events
);
1191 dma_free_coherent(vhost
->dev
,
1192 pool
->size
* sizeof(*pool
->iu_storage
),
1193 pool
->iu_storage
, pool
->iu_token
);
1198 * ibmvfc_get_event - Gets the next free event in pool
1199 * @vhost: ibmvfc host struct
1201 * Returns a free event from the pool.
1203 static struct ibmvfc_event
*ibmvfc_get_event(struct ibmvfc_host
*vhost
)
1205 struct ibmvfc_event
*evt
;
1207 BUG_ON(list_empty(&vhost
->free
));
1208 evt
= list_entry(vhost
->free
.next
, struct ibmvfc_event
, queue
);
1209 atomic_set(&evt
->free
, 0);
1210 list_del(&evt
->queue
);
1215 * ibmvfc_init_event - Initialize fields in an event struct that are always
1218 * @done: Routine to call when the event is responded to
1219 * @format: SRP or MAD format
1221 static void ibmvfc_init_event(struct ibmvfc_event
*evt
,
1222 void (*done
) (struct ibmvfc_event
*), u8 format
)
1225 evt
->sync_iu
= NULL
;
1226 evt
->crq
.format
= format
;
1231 * ibmvfc_map_sg_list - Initialize scatterlist
1232 * @scmd: scsi command struct
1233 * @nseg: number of scatterlist segments
1234 * @md: memory descriptor list to initialize
1236 static void ibmvfc_map_sg_list(struct scsi_cmnd
*scmd
, int nseg
,
1237 struct srp_direct_buf
*md
)
1240 struct scatterlist
*sg
;
1242 scsi_for_each_sg(scmd
, sg
, nseg
, i
) {
1243 md
[i
].va
= sg_dma_address(sg
);
1244 md
[i
].len
= sg_dma_len(sg
);
1250 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1251 * @scmd: Scsi_Cmnd with the scatterlist
1252 * @evt: ibmvfc event struct
1253 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1254 * @dev: device for which to map dma memory
1257 * 0 on success / non-zero on failure
1259 static int ibmvfc_map_sg_data(struct scsi_cmnd
*scmd
,
1260 struct ibmvfc_event
*evt
,
1261 struct ibmvfc_cmd
*vfc_cmd
, struct device
*dev
)
1265 struct srp_direct_buf
*data
= &vfc_cmd
->ioba
;
1266 struct ibmvfc_host
*vhost
= dev_get_drvdata(dev
);
1268 sg_mapped
= scsi_dma_map(scmd
);
1270 vfc_cmd
->flags
|= IBMVFC_NO_MEM_DESC
;
1272 } else if (unlikely(sg_mapped
< 0)) {
1273 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1274 scmd_printk(KERN_ERR
, scmd
, "Failed to map DMA buffer for command\n");
1278 if (scmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1279 vfc_cmd
->flags
|= IBMVFC_WRITE
;
1280 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_WRDATA
;
1282 vfc_cmd
->flags
|= IBMVFC_READ
;
1283 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_RDDATA
;
1286 if (sg_mapped
== 1) {
1287 ibmvfc_map_sg_list(scmd
, sg_mapped
, data
);
1291 vfc_cmd
->flags
|= IBMVFC_SCATTERLIST
;
1293 if (!evt
->ext_list
) {
1294 evt
->ext_list
= dma_pool_alloc(vhost
->sg_pool
, GFP_ATOMIC
,
1295 &evt
->ext_list_token
);
1297 if (!evt
->ext_list
) {
1298 scmd_printk(KERN_ERR
, scmd
, "Can't allocate memory for scatterlist\n");
1303 ibmvfc_map_sg_list(scmd
, sg_mapped
, evt
->ext_list
);
1305 data
->va
= evt
->ext_list_token
;
1306 data
->len
= sg_mapped
* sizeof(struct srp_direct_buf
);
1312 * ibmvfc_timeout - Internal command timeout handler
1313 * @evt: struct ibmvfc_event that timed out
1315 * Called when an internally generated command times out
1317 static void ibmvfc_timeout(struct ibmvfc_event
*evt
)
1319 struct ibmvfc_host
*vhost
= evt
->vhost
;
1320 dev_err(vhost
->dev
, "Command timed out (%p). Resetting connection\n", evt
);
1321 ibmvfc_reset_host(vhost
);
1325 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1326 * @evt: event to be sent
1327 * @vhost: ibmvfc host struct
1328 * @timeout: timeout in seconds - 0 means do not time command
1330 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1332 static int ibmvfc_send_event(struct ibmvfc_event
*evt
,
1333 struct ibmvfc_host
*vhost
, unsigned long timeout
)
1335 u64
*crq_as_u64
= (u64
*) &evt
->crq
;
1338 /* Copy the IU into the transfer area */
1339 *evt
->xfer_iu
= evt
->iu
;
1340 if (evt
->crq
.format
== IBMVFC_CMD_FORMAT
)
1341 evt
->xfer_iu
->cmd
.tag
= (u64
)evt
;
1342 else if (evt
->crq
.format
== IBMVFC_MAD_FORMAT
)
1343 evt
->xfer_iu
->mad_common
.tag
= (u64
)evt
;
1347 list_add_tail(&evt
->queue
, &vhost
->sent
);
1348 init_timer(&evt
->timer
);
1351 evt
->timer
.data
= (unsigned long) evt
;
1352 evt
->timer
.expires
= jiffies
+ (timeout
* HZ
);
1353 evt
->timer
.function
= (void (*)(unsigned long))ibmvfc_timeout
;
1354 add_timer(&evt
->timer
);
1357 if ((rc
= ibmvfc_send_crq(vhost
, crq_as_u64
[0], crq_as_u64
[1]))) {
1358 list_del(&evt
->queue
);
1359 del_timer(&evt
->timer
);
1361 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1362 * Firmware will send a CRQ with a transport event (0xFF) to
1363 * tell this client what has happened to the transport. This
1364 * will be handled in ibmvfc_handle_crq()
1366 if (rc
== H_CLOSED
) {
1367 if (printk_ratelimit())
1368 dev_warn(vhost
->dev
, "Send warning. Receive queue closed, will retry.\n");
1370 scsi_dma_unmap(evt
->cmnd
);
1371 ibmvfc_free_event(evt
);
1372 return SCSI_MLQUEUE_HOST_BUSY
;
1375 dev_err(vhost
->dev
, "Send error (rc=%d)\n", rc
);
1377 evt
->cmnd
->result
= DID_ERROR
<< 16;
1378 evt
->done
= ibmvfc_scsi_eh_done
;
1380 evt
->xfer_iu
->mad_common
.status
= IBMVFC_MAD_CRQ_ERROR
;
1384 ibmvfc_trc_start(evt
);
1390 * ibmvfc_log_error - Log an error for the failed command if appropriate
1391 * @evt: ibmvfc event to log
1394 static void ibmvfc_log_error(struct ibmvfc_event
*evt
)
1396 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1397 struct ibmvfc_host
*vhost
= evt
->vhost
;
1398 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1399 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1400 const char *err
= unknown_error
;
1401 int index
= ibmvfc_get_err_index(vfc_cmd
->status
, vfc_cmd
->error
);
1406 logerr
= cmd_status
[index
].log
;
1407 err
= cmd_status
[index
].name
;
1410 if (!logerr
&& (vhost
->log_level
<= (IBMVFC_DEFAULT_LOG_LEVEL
+ 1)))
1413 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1414 rsp_code
= rsp
->data
.info
.rsp_code
;
1416 scmd_printk(KERN_ERR
, cmnd
, "Command (%02X) failed: %s (%x:%x) "
1417 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1418 cmnd
->cmnd
[0], err
, vfc_cmd
->status
, vfc_cmd
->error
,
1419 rsp
->flags
, rsp_code
, scsi_get_resid(cmnd
), rsp
->scsi_status
);
1423 * ibmvfc_scsi_done - Handle responses from commands
1424 * @evt: ibmvfc event to be handled
1426 * Used as a callback when sending scsi cmds.
1428 static void ibmvfc_scsi_done(struct ibmvfc_event
*evt
)
1430 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1431 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1432 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1434 int sense_len
= rsp
->fcp_sense_len
;
1437 if (vfc_cmd
->response_flags
& IBMVFC_ADAPTER_RESID_VALID
)
1438 scsi_set_resid(cmnd
, vfc_cmd
->adapter_resid
);
1439 else if (rsp
->flags
& FCP_RESID_UNDER
)
1440 scsi_set_resid(cmnd
, rsp
->fcp_resid
);
1442 scsi_set_resid(cmnd
, 0);
1444 if (vfc_cmd
->status
) {
1445 cmnd
->result
= ibmvfc_get_err_result(vfc_cmd
);
1447 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1448 rsp_len
= rsp
->fcp_rsp_len
;
1449 if ((sense_len
+ rsp_len
) > SCSI_SENSE_BUFFERSIZE
)
1450 sense_len
= SCSI_SENSE_BUFFERSIZE
- rsp_len
;
1451 if ((rsp
->flags
& FCP_SNS_LEN_VALID
) && rsp
->fcp_sense_len
)
1452 memcpy(cmnd
->sense_buffer
, rsp
->data
.sense
+ rsp_len
, sense_len
);
1454 ibmvfc_log_error(evt
);
1457 if (!cmnd
->result
&&
1458 (scsi_bufflen(cmnd
) - scsi_get_resid(cmnd
) < cmnd
->underflow
))
1459 cmnd
->result
= (DID_ERROR
<< 16);
1461 scsi_dma_unmap(cmnd
);
1462 cmnd
->scsi_done(cmnd
);
1465 ibmvfc_free_event(evt
);
1469 * ibmvfc_host_chkready - Check if the host can accept commands
1470 * @vhost: struct ibmvfc host
1473 * 1 if host can accept command / 0 if not
1475 static inline int ibmvfc_host_chkready(struct ibmvfc_host
*vhost
)
1479 switch (vhost
->state
) {
1480 case IBMVFC_LINK_DEAD
:
1481 case IBMVFC_HOST_OFFLINE
:
1482 result
= DID_NO_CONNECT
<< 16;
1485 case IBMVFC_INITIALIZING
:
1487 case IBMVFC_LINK_DOWN
:
1488 result
= DID_REQUEUE
<< 16;
1499 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1500 * @cmnd: struct scsi_cmnd to be executed
1501 * @done: Callback function to be called when cmnd is completed
1504 * 0 on success / other on failure
1506 static int ibmvfc_queuecommand(struct scsi_cmnd
*cmnd
,
1507 void (*done
) (struct scsi_cmnd
*))
1509 struct ibmvfc_host
*vhost
= shost_priv(cmnd
->device
->host
);
1510 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
1511 struct ibmvfc_cmd
*vfc_cmd
;
1512 struct ibmvfc_event
*evt
;
1516 if (unlikely((rc
= fc_remote_port_chkready(rport
))) ||
1517 unlikely((rc
= ibmvfc_host_chkready(vhost
)))) {
1523 cmnd
->result
= (DID_OK
<< 16);
1524 evt
= ibmvfc_get_event(vhost
);
1525 ibmvfc_init_event(evt
, ibmvfc_scsi_done
, IBMVFC_CMD_FORMAT
);
1527 cmnd
->scsi_done
= done
;
1528 vfc_cmd
= &evt
->iu
.cmd
;
1529 memset(vfc_cmd
, 0, sizeof(*vfc_cmd
));
1530 vfc_cmd
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
1531 vfc_cmd
->resp
.len
= sizeof(vfc_cmd
->rsp
);
1532 vfc_cmd
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
1533 vfc_cmd
->payload_len
= sizeof(vfc_cmd
->iu
);
1534 vfc_cmd
->resp_len
= sizeof(vfc_cmd
->rsp
);
1535 vfc_cmd
->cancel_key
= (unsigned long)cmnd
->device
->hostdata
;
1536 vfc_cmd
->tgt_scsi_id
= rport
->port_id
;
1537 if ((rport
->supported_classes
& FC_COS_CLASS3
) &&
1538 (fc_host_supported_classes(vhost
->host
) & FC_COS_CLASS3
))
1539 vfc_cmd
->flags
= IBMVFC_CLASS_3_ERR
;
1540 vfc_cmd
->iu
.xfer_len
= scsi_bufflen(cmnd
);
1541 int_to_scsilun(cmnd
->device
->lun
, &vfc_cmd
->iu
.lun
);
1542 memcpy(vfc_cmd
->iu
.cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
1544 if (scsi_populate_tag_msg(cmnd
, tag
)) {
1545 vfc_cmd
->task_tag
= tag
[1];
1547 case MSG_SIMPLE_TAG
:
1548 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_SIMPLE_TASK
;
1551 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_HEAD_OF_QUEUE
;
1553 case MSG_ORDERED_TAG
:
1554 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_ORDERED_TASK
;
1559 if (likely(!(rc
= ibmvfc_map_sg_data(cmnd
, evt
, vfc_cmd
, vhost
->dev
))))
1560 return ibmvfc_send_event(evt
, vhost
, 0);
1562 ibmvfc_free_event(evt
);
1564 return SCSI_MLQUEUE_HOST_BUSY
;
1566 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1567 scmd_printk(KERN_ERR
, cmnd
,
1568 "Failed to map DMA buffer for command. rc=%d\n", rc
);
1570 cmnd
->result
= DID_ERROR
<< 16;
1576 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1577 * @evt: ibmvfc event struct
1580 static void ibmvfc_sync_completion(struct ibmvfc_event
*evt
)
1582 /* copy the response back */
1584 *evt
->sync_iu
= *evt
->xfer_iu
;
1586 complete(&evt
->comp
);
1590 * ibmvfc_reset_device - Reset the device with the specified reset type
1591 * @sdev: scsi device to reset
1593 * @desc: reset type description for log messages
1596 * 0 on success / other on failure
1598 static int ibmvfc_reset_device(struct scsi_device
*sdev
, int type
, char *desc
)
1600 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1601 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1602 struct ibmvfc_cmd
*tmf
;
1603 struct ibmvfc_event
*evt
;
1604 union ibmvfc_iu rsp_iu
;
1605 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
1606 int rsp_rc
= -EBUSY
;
1607 unsigned long flags
;
1610 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1611 if (vhost
->state
== IBMVFC_ACTIVE
) {
1612 evt
= ibmvfc_get_event(vhost
);
1613 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
1616 memset(tmf
, 0, sizeof(*tmf
));
1617 tmf
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
1618 tmf
->resp
.len
= sizeof(tmf
->rsp
);
1619 tmf
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
1620 tmf
->payload_len
= sizeof(tmf
->iu
);
1621 tmf
->resp_len
= sizeof(tmf
->rsp
);
1622 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
1623 tmf
->tgt_scsi_id
= rport
->port_id
;
1624 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
1625 tmf
->flags
= (IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
);
1626 tmf
->iu
.tmf_flags
= type
;
1627 evt
->sync_iu
= &rsp_iu
;
1629 init_completion(&evt
->comp
);
1630 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1632 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1635 sdev_printk(KERN_ERR
, sdev
, "Failed to send %s reset event. rc=%d\n",
1640 sdev_printk(KERN_INFO
, sdev
, "Resetting %s\n", desc
);
1641 wait_for_completion(&evt
->comp
);
1643 if (rsp_iu
.cmd
.status
) {
1644 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
1645 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
1647 sdev_printk(KERN_ERR
, sdev
, "%s reset failed: %s (%x:%x) "
1648 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1649 desc
, ibmvfc_get_cmd_error(rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
),
1650 rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
, fc_rsp
->flags
, rsp_code
,
1651 fc_rsp
->scsi_status
);
1654 sdev_printk(KERN_INFO
, sdev
, "%s reset successful\n", desc
);
1656 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1657 ibmvfc_free_event(evt
);
1658 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1663 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1664 * @sdev: scsi device to abort commands
1666 * This sends an Abort Task Set to the VIOS for the specified device. This does
1667 * NOT send any cancel to the VIOS. That must be done separately.
1670 * 0 on success / other on failure
1672 static int ibmvfc_abort_task_set(struct scsi_device
*sdev
)
1674 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1675 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1676 struct ibmvfc_cmd
*tmf
;
1677 struct ibmvfc_event
*evt
, *found_evt
;
1678 union ibmvfc_iu rsp_iu
;
1679 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
1680 int rsp_rc
= -EBUSY
;
1681 unsigned long flags
;
1684 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1686 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
1687 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
1694 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1695 sdev_printk(KERN_INFO
, sdev
, "No events found to abort\n");
1696 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1700 if (vhost
->state
== IBMVFC_ACTIVE
) {
1701 evt
= ibmvfc_get_event(vhost
);
1702 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
1705 memset(tmf
, 0, sizeof(*tmf
));
1706 tmf
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
1707 tmf
->resp
.len
= sizeof(tmf
->rsp
);
1708 tmf
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
1709 tmf
->payload_len
= sizeof(tmf
->iu
);
1710 tmf
->resp_len
= sizeof(tmf
->rsp
);
1711 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
1712 tmf
->tgt_scsi_id
= rport
->port_id
;
1713 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
1714 tmf
->flags
= (IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
);
1715 tmf
->iu
.tmf_flags
= IBMVFC_ABORT_TASK_SET
;
1716 evt
->sync_iu
= &rsp_iu
;
1718 init_completion(&evt
->comp
);
1719 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1722 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1725 sdev_printk(KERN_ERR
, sdev
, "Failed to send abort. rc=%d\n", rsp_rc
);
1729 sdev_printk(KERN_INFO
, sdev
, "Aborting outstanding commands\n");
1730 wait_for_completion(&evt
->comp
);
1732 if (rsp_iu
.cmd
.status
) {
1733 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
1734 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
1736 sdev_printk(KERN_ERR
, sdev
, "Abort failed: %s (%x:%x) "
1737 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1738 ibmvfc_get_cmd_error(rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
),
1739 rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
, fc_rsp
->flags
, rsp_code
,
1740 fc_rsp
->scsi_status
);
1743 sdev_printk(KERN_INFO
, sdev
, "Abort successful\n");
1745 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1746 ibmvfc_free_event(evt
);
1747 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1752 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1753 * @sdev: scsi device to cancel commands
1754 * @type: type of error recovery being performed
1756 * This sends a cancel to the VIOS for the specified device. This does
1757 * NOT send any abort to the actual device. That must be done separately.
1760 * 0 on success / other on failure
1762 static int ibmvfc_cancel_all(struct scsi_device
*sdev
, int type
)
1764 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1765 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1766 struct ibmvfc_tmf
*tmf
;
1767 struct ibmvfc_event
*evt
, *found_evt
;
1768 union ibmvfc_iu rsp
;
1769 int rsp_rc
= -EBUSY
;
1770 unsigned long flags
;
1774 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1776 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
1777 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
1784 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1785 sdev_printk(KERN_INFO
, sdev
, "No events found to cancel\n");
1786 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1790 if (vhost
->state
== IBMVFC_ACTIVE
) {
1791 evt
= ibmvfc_get_event(vhost
);
1792 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
1795 memset(tmf
, 0, sizeof(*tmf
));
1796 tmf
->common
.version
= 1;
1797 tmf
->common
.opcode
= IBMVFC_TMF_MAD
;
1798 tmf
->common
.length
= sizeof(*tmf
);
1799 tmf
->scsi_id
= rport
->port_id
;
1800 int_to_scsilun(sdev
->lun
, &tmf
->lun
);
1801 tmf
->flags
= (type
| IBMVFC_TMF_LUA_VALID
);
1802 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
1803 tmf
->my_cancel_key
= (IBMVFC_TMF_CANCEL_KEY
| (unsigned long)sdev
->hostdata
);
1805 evt
->sync_iu
= &rsp
;
1806 init_completion(&evt
->comp
);
1807 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1810 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1813 sdev_printk(KERN_ERR
, sdev
, "Failed to send cancel event. rc=%d\n", rsp_rc
);
1817 sdev_printk(KERN_INFO
, sdev
, "Cancelling outstanding commands.\n");
1819 wait_for_completion(&evt
->comp
);
1820 status
= rsp
.mad_common
.status
;
1821 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1822 ibmvfc_free_event(evt
);
1823 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1825 if (status
!= IBMVFC_MAD_SUCCESS
) {
1826 sdev_printk(KERN_WARNING
, sdev
, "Cancel failed with rc=%x\n", status
);
1830 sdev_printk(KERN_INFO
, sdev
, "Successfully cancelled outstanding commands\n");
1835 * ibmvfc_eh_abort_handler - Abort a command
1836 * @cmd: scsi command to abort
1841 static int ibmvfc_eh_abort_handler(struct scsi_cmnd
*cmd
)
1843 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
1844 struct ibmvfc_event
*evt
, *pos
;
1845 int cancel_rc
, abort_rc
;
1846 unsigned long flags
;
1849 ibmvfc_wait_while_resetting(vhost
);
1850 cancel_rc
= ibmvfc_cancel_all(cmd
->device
, IBMVFC_TMF_ABORT_TASK_SET
);
1851 abort_rc
= ibmvfc_abort_task_set(cmd
->device
);
1853 if (!cancel_rc
&& !abort_rc
) {
1854 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1855 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
) {
1856 if (evt
->cmnd
&& evt
->cmnd
->device
== cmd
->device
)
1857 ibmvfc_fail_request(evt
, DID_ABORT
);
1859 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1869 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1870 * @cmd: scsi command struct
1875 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
1877 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
1878 struct ibmvfc_event
*evt
, *pos
;
1879 int cancel_rc
, reset_rc
;
1880 unsigned long flags
;
1883 ibmvfc_wait_while_resetting(vhost
);
1884 cancel_rc
= ibmvfc_cancel_all(cmd
->device
, IBMVFC_TMF_LUN_RESET
);
1885 reset_rc
= ibmvfc_reset_device(cmd
->device
, IBMVFC_LUN_RESET
, "LUN");
1887 if (!cancel_rc
&& !reset_rc
) {
1888 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1889 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
) {
1890 if (evt
->cmnd
&& evt
->cmnd
->device
== cmd
->device
)
1891 ibmvfc_fail_request(evt
, DID_ABORT
);
1893 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1903 * ibmvfc_dev_cancel_all - Device iterated cancel all function
1904 * @sdev: scsi device struct
1905 * @data: return code
1908 static void ibmvfc_dev_cancel_all(struct scsi_device
*sdev
, void *data
)
1910 unsigned long *rc
= data
;
1911 *rc
|= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_TGT_RESET
);
1915 * ibmvfc_dev_abort_all - Device iterated abort task set function
1916 * @sdev: scsi device struct
1917 * @data: return code
1920 static void ibmvfc_dev_abort_all(struct scsi_device
*sdev
, void *data
)
1922 unsigned long *rc
= data
;
1923 *rc
|= ibmvfc_abort_task_set(sdev
);
1927 * ibmvfc_eh_target_reset_handler - Reset the target
1928 * @cmd: scsi command struct
1933 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd
*cmd
)
1935 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
1936 struct scsi_target
*starget
= scsi_target(cmd
->device
);
1937 struct ibmvfc_event
*evt
, *pos
;
1939 unsigned long cancel_rc
= 0;
1940 unsigned long flags
;
1943 ibmvfc_wait_while_resetting(vhost
);
1944 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all
);
1945 reset_rc
= ibmvfc_reset_device(cmd
->device
, IBMVFC_TARGET_RESET
, "target");
1947 if (!cancel_rc
&& !reset_rc
) {
1948 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1949 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
) {
1950 if (evt
->cmnd
&& scsi_target(evt
->cmnd
->device
) == starget
)
1951 ibmvfc_fail_request(evt
, DID_ABORT
);
1953 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1963 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
1964 * @cmd: struct scsi_cmnd having problems
1967 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
1970 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
1972 dev_err(vhost
->dev
, "Resetting connection due to error recovery\n");
1973 rc
= ibmvfc_issue_fc_host_lip(vhost
->host
);
1974 return rc
? FAILED
: SUCCESS
;
1978 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
1979 * @rport: rport struct
1984 static void ibmvfc_terminate_rport_io(struct fc_rport
*rport
)
1986 struct scsi_target
*starget
= to_scsi_target(&rport
->dev
);
1987 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
1988 struct ibmvfc_host
*vhost
= shost_priv(shost
);
1989 struct ibmvfc_event
*evt
, *pos
;
1990 unsigned long cancel_rc
= 0;
1991 unsigned long abort_rc
= 0;
1992 unsigned long flags
;
1995 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all
);
1996 starget_for_each_device(starget
, &abort_rc
, ibmvfc_dev_abort_all
);
1998 if (!cancel_rc
&& !abort_rc
) {
1999 spin_lock_irqsave(shost
->host_lock
, flags
);
2000 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
) {
2001 if (evt
->cmnd
&& scsi_target(evt
->cmnd
->device
) == starget
)
2002 ibmvfc_fail_request(evt
, DID_ABORT
);
2004 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2006 ibmvfc_issue_fc_host_lip(shost
);
2008 scsi_target_unblock(&rport
->dev
);
2012 static const struct {
2013 enum ibmvfc_async_event ae
;
2016 { IBMVFC_AE_ELS_PLOGI
, "PLOGI" },
2017 { IBMVFC_AE_ELS_LOGO
, "LOGO" },
2018 { IBMVFC_AE_ELS_PRLO
, "PRLO" },
2019 { IBMVFC_AE_SCN_NPORT
, "N-Port SCN" },
2020 { IBMVFC_AE_SCN_GROUP
, "Group SCN" },
2021 { IBMVFC_AE_SCN_DOMAIN
, "Domain SCN" },
2022 { IBMVFC_AE_SCN_FABRIC
, "Fabric SCN" },
2023 { IBMVFC_AE_LINK_UP
, "Link Up" },
2024 { IBMVFC_AE_LINK_DOWN
, "Link Down" },
2025 { IBMVFC_AE_LINK_DEAD
, "Link Dead" },
2026 { IBMVFC_AE_HALT
, "Halt" },
2027 { IBMVFC_AE_RESUME
, "Resume" },
2028 { IBMVFC_AE_ADAPTER_FAILED
, "Adapter Failed" },
2031 static const char *unknown_ae
= "Unknown async";
2034 * ibmvfc_get_ae_desc - Get text description for async event
2038 static const char *ibmvfc_get_ae_desc(u64 ae
)
2042 for (i
= 0; i
< ARRAY_SIZE(ae_desc
); i
++)
2043 if (ae_desc
[i
].ae
== ae
)
2044 return ae_desc
[i
].desc
;
2050 * ibmvfc_handle_async - Handle an async event from the adapter
2051 * @crq: crq to process
2052 * @vhost: ibmvfc host struct
2055 static void ibmvfc_handle_async(struct ibmvfc_async_crq
*crq
,
2056 struct ibmvfc_host
*vhost
)
2058 const char *desc
= ibmvfc_get_ae_desc(crq
->event
);
2060 ibmvfc_log(vhost
, 3, "%s event received\n", desc
);
2062 switch (crq
->event
) {
2063 case IBMVFC_AE_LINK_UP
:
2064 case IBMVFC_AE_RESUME
:
2065 vhost
->events_to_log
|= IBMVFC_AE_LINKUP
;
2066 ibmvfc_init_host(vhost
);
2068 case IBMVFC_AE_SCN_FABRIC
:
2069 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2070 ibmvfc_init_host(vhost
);
2072 case IBMVFC_AE_SCN_NPORT
:
2073 case IBMVFC_AE_SCN_GROUP
:
2074 case IBMVFC_AE_SCN_DOMAIN
:
2075 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2076 case IBMVFC_AE_ELS_LOGO
:
2077 case IBMVFC_AE_ELS_PRLO
:
2078 case IBMVFC_AE_ELS_PLOGI
:
2079 ibmvfc_reinit_host(vhost
);
2081 case IBMVFC_AE_LINK_DOWN
:
2082 case IBMVFC_AE_ADAPTER_FAILED
:
2083 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2085 case IBMVFC_AE_LINK_DEAD
:
2086 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2088 case IBMVFC_AE_HALT
:
2089 ibmvfc_link_down(vhost
, IBMVFC_HALTED
);
2092 dev_err(vhost
->dev
, "Unknown async event received: %ld\n", crq
->event
);
2098 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2099 * @crq: Command/Response queue
2100 * @vhost: ibmvfc host struct
2103 static void ibmvfc_handle_crq(struct ibmvfc_crq
*crq
, struct ibmvfc_host
*vhost
)
2106 struct ibmvfc_event
*evt
= (struct ibmvfc_event
*)crq
->ioba
;
2108 switch (crq
->valid
) {
2109 case IBMVFC_CRQ_INIT_RSP
:
2110 switch (crq
->format
) {
2111 case IBMVFC_CRQ_INIT
:
2112 dev_info(vhost
->dev
, "Partner initialized\n");
2113 /* Send back a response */
2114 rc
= ibmvfc_send_crq_init_complete(vhost
);
2116 ibmvfc_init_host(vhost
);
2118 dev_err(vhost
->dev
, "Unable to send init rsp. rc=%ld\n", rc
);
2120 case IBMVFC_CRQ_INIT_COMPLETE
:
2121 dev_info(vhost
->dev
, "Partner initialization complete\n");
2122 ibmvfc_init_host(vhost
);
2125 dev_err(vhost
->dev
, "Unknown crq message type: %d\n", crq
->format
);
2128 case IBMVFC_CRQ_XPORT_EVENT
:
2129 vhost
->state
= IBMVFC_NO_CRQ
;
2130 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
2131 if (crq
->format
== IBMVFC_PARTITION_MIGRATED
) {
2132 /* We need to re-setup the interpartition connection */
2133 dev_info(vhost
->dev
, "Re-enabling adapter\n");
2134 vhost
->client_migrated
= 1;
2135 ibmvfc_purge_requests(vhost
, DID_REQUEUE
);
2136 if ((rc
= ibmvfc_reenable_crq_queue(vhost
)) ||
2137 (rc
= ibmvfc_send_crq_init(vhost
))) {
2138 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2139 dev_err(vhost
->dev
, "Error after enable (rc=%ld)\n", rc
);
2141 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2143 dev_err(vhost
->dev
, "Virtual adapter failed (rc=%d)\n", crq
->format
);
2145 ibmvfc_purge_requests(vhost
, DID_ERROR
);
2146 if ((rc
= ibmvfc_reset_crq(vhost
)) ||
2147 (rc
= ibmvfc_send_crq_init(vhost
))) {
2148 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2149 dev_err(vhost
->dev
, "Error after reset (rc=%ld)\n", rc
);
2151 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2154 case IBMVFC_CRQ_CMD_RSP
:
2157 dev_err(vhost
->dev
, "Got an invalid message type 0x%02x\n", crq
->valid
);
2161 if (crq
->format
== IBMVFC_ASYNC_EVENT
)
2164 /* The only kind of payload CRQs we should get are responses to
2165 * things we send. Make sure this response is to something we
2168 if (unlikely(!ibmvfc_valid_event(&vhost
->pool
, evt
))) {
2169 dev_err(vhost
->dev
, "Returned correlation_token 0x%08lx is invalid!\n",
2174 if (unlikely(atomic_read(&evt
->free
))) {
2175 dev_err(vhost
->dev
, "Received duplicate correlation_token 0x%08lx!\n",
2180 del_timer(&evt
->timer
);
2181 list_del(&evt
->queue
);
2182 ibmvfc_trc_end(evt
);
2187 * ibmvfc_scan_finished - Check if the device scan is done.
2188 * @shost: scsi host struct
2189 * @time: current elapsed time
2192 * 0 if scan is not done / 1 if scan is done
2194 static int ibmvfc_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
2196 unsigned long flags
;
2197 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2200 spin_lock_irqsave(shost
->host_lock
, flags
);
2201 if (time
>= (init_timeout
* HZ
)) {
2202 dev_info(vhost
->dev
, "Scan taking longer than %d seconds, "
2203 "continuing initialization\n", init_timeout
);
2207 if (vhost
->state
!= IBMVFC_NO_CRQ
&& vhost
->action
== IBMVFC_HOST_ACTION_NONE
)
2209 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2214 * ibmvfc_slave_alloc - Setup the device's task set value
2215 * @sdev: struct scsi_device device to configure
2217 * Set the device's task set value so that error handling works as
2221 * 0 on success / -ENXIO if device does not exist
2223 static int ibmvfc_slave_alloc(struct scsi_device
*sdev
)
2225 struct Scsi_Host
*shost
= sdev
->host
;
2226 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2227 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2228 unsigned long flags
= 0;
2230 if (!rport
|| fc_remote_port_chkready(rport
))
2233 spin_lock_irqsave(shost
->host_lock
, flags
);
2234 sdev
->hostdata
= (void *)(unsigned long)vhost
->task_set
++;
2235 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2240 * ibmvfc_slave_configure - Configure the device
2241 * @sdev: struct scsi_device device to configure
2243 * Enable allow_restart for a device if it is a disk. Adjust the
2244 * queue_depth here also.
2249 static int ibmvfc_slave_configure(struct scsi_device
*sdev
)
2251 struct Scsi_Host
*shost
= sdev
->host
;
2252 struct fc_rport
*rport
= starget_to_rport(sdev
->sdev_target
);
2253 unsigned long flags
= 0;
2255 spin_lock_irqsave(shost
->host_lock
, flags
);
2256 if (sdev
->type
== TYPE_DISK
)
2257 sdev
->allow_restart
= 1;
2259 if (sdev
->tagged_supported
) {
2260 scsi_set_tag_type(sdev
, MSG_SIMPLE_TAG
);
2261 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2263 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2265 rport
->dev_loss_tmo
= dev_loss_tmo
;
2266 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2271 * ibmvfc_change_queue_depth - Change the device's queue depth
2272 * @sdev: scsi device struct
2273 * @qdepth: depth to set
2278 static int ibmvfc_change_queue_depth(struct scsi_device
*sdev
, int qdepth
)
2280 if (qdepth
> IBMVFC_MAX_CMDS_PER_LUN
)
2281 qdepth
= IBMVFC_MAX_CMDS_PER_LUN
;
2283 scsi_adjust_queue_depth(sdev
, 0, qdepth
);
2284 return sdev
->queue_depth
;
2288 * ibmvfc_change_queue_type - Change the device's queue type
2289 * @sdev: scsi device struct
2290 * @tag_type: type of tags to use
2293 * actual queue type set
2295 static int ibmvfc_change_queue_type(struct scsi_device
*sdev
, int tag_type
)
2297 if (sdev
->tagged_supported
) {
2298 scsi_set_tag_type(sdev
, tag_type
);
2301 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2303 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2310 static ssize_t
ibmvfc_show_host_partition_name(struct device
*dev
,
2311 struct device_attribute
*attr
, char *buf
)
2313 struct Scsi_Host
*shost
= class_to_shost(dev
);
2314 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2316 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2317 vhost
->login_buf
->resp
.partition_name
);
2320 static struct device_attribute ibmvfc_host_partition_name
= {
2322 .name
= "partition_name",
2325 .show
= ibmvfc_show_host_partition_name
,
2328 static ssize_t
ibmvfc_show_host_device_name(struct device
*dev
,
2329 struct device_attribute
*attr
, char *buf
)
2331 struct Scsi_Host
*shost
= class_to_shost(dev
);
2332 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2334 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2335 vhost
->login_buf
->resp
.device_name
);
2338 static struct device_attribute ibmvfc_host_device_name
= {
2340 .name
= "device_name",
2343 .show
= ibmvfc_show_host_device_name
,
2346 static ssize_t
ibmvfc_show_host_loc_code(struct device
*dev
,
2347 struct device_attribute
*attr
, char *buf
)
2349 struct Scsi_Host
*shost
= class_to_shost(dev
);
2350 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2352 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2353 vhost
->login_buf
->resp
.port_loc_code
);
2356 static struct device_attribute ibmvfc_host_loc_code
= {
2358 .name
= "port_loc_code",
2361 .show
= ibmvfc_show_host_loc_code
,
2364 static ssize_t
ibmvfc_show_host_drc_name(struct device
*dev
,
2365 struct device_attribute
*attr
, char *buf
)
2367 struct Scsi_Host
*shost
= class_to_shost(dev
);
2368 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2370 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2371 vhost
->login_buf
->resp
.drc_name
);
2374 static struct device_attribute ibmvfc_host_drc_name
= {
2379 .show
= ibmvfc_show_host_drc_name
,
2382 static ssize_t
ibmvfc_show_host_npiv_version(struct device
*dev
,
2383 struct device_attribute
*attr
, char *buf
)
2385 struct Scsi_Host
*shost
= class_to_shost(dev
);
2386 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2387 return snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->login_buf
->resp
.version
);
2390 static struct device_attribute ibmvfc_host_npiv_version
= {
2392 .name
= "npiv_version",
2395 .show
= ibmvfc_show_host_npiv_version
,
2399 * ibmvfc_show_log_level - Show the adapter's error logging level
2400 * @dev: class device struct
2404 * number of bytes printed to buffer
2406 static ssize_t
ibmvfc_show_log_level(struct device
*dev
,
2407 struct device_attribute
*attr
, char *buf
)
2409 struct Scsi_Host
*shost
= class_to_shost(dev
);
2410 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2411 unsigned long flags
= 0;
2414 spin_lock_irqsave(shost
->host_lock
, flags
);
2415 len
= snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->log_level
);
2416 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2421 * ibmvfc_store_log_level - Change the adapter's error logging level
2422 * @dev: class device struct
2426 * number of bytes printed to buffer
2428 static ssize_t
ibmvfc_store_log_level(struct device
*dev
,
2429 struct device_attribute
*attr
,
2430 const char *buf
, size_t count
)
2432 struct Scsi_Host
*shost
= class_to_shost(dev
);
2433 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2434 unsigned long flags
= 0;
2436 spin_lock_irqsave(shost
->host_lock
, flags
);
2437 vhost
->log_level
= simple_strtoul(buf
, NULL
, 10);
2438 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2442 static struct device_attribute ibmvfc_log_level_attr
= {
2444 .name
= "log_level",
2445 .mode
= S_IRUGO
| S_IWUSR
,
2447 .show
= ibmvfc_show_log_level
,
2448 .store
= ibmvfc_store_log_level
2451 #ifdef CONFIG_SCSI_IBMVFC_TRACE
2453 * ibmvfc_read_trace - Dump the adapter trace
2454 * @kobj: kobject struct
2455 * @bin_attr: bin_attribute struct
2458 * @count: buffer size
2461 * number of bytes printed to buffer
2463 static ssize_t
ibmvfc_read_trace(struct kobject
*kobj
,
2464 struct bin_attribute
*bin_attr
,
2465 char *buf
, loff_t off
, size_t count
)
2467 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2468 struct Scsi_Host
*shost
= class_to_shost(dev
);
2469 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2470 unsigned long flags
= 0;
2471 int size
= IBMVFC_TRACE_SIZE
;
2472 char *src
= (char *)vhost
->trace
;
2476 if (off
+ count
> size
) {
2481 spin_lock_irqsave(shost
->host_lock
, flags
);
2482 memcpy(buf
, &src
[off
], count
);
2483 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2487 static struct bin_attribute ibmvfc_trace_attr
= {
2493 .read
= ibmvfc_read_trace
,
2497 static struct device_attribute
*ibmvfc_attrs
[] = {
2498 &ibmvfc_host_partition_name
,
2499 &ibmvfc_host_device_name
,
2500 &ibmvfc_host_loc_code
,
2501 &ibmvfc_host_drc_name
,
2502 &ibmvfc_host_npiv_version
,
2503 &ibmvfc_log_level_attr
,
2507 static struct scsi_host_template driver_template
= {
2508 .module
= THIS_MODULE
,
2509 .name
= "IBM POWER Virtual FC Adapter",
2510 .proc_name
= IBMVFC_NAME
,
2511 .queuecommand
= ibmvfc_queuecommand
,
2512 .eh_abort_handler
= ibmvfc_eh_abort_handler
,
2513 .eh_device_reset_handler
= ibmvfc_eh_device_reset_handler
,
2514 .eh_target_reset_handler
= ibmvfc_eh_target_reset_handler
,
2515 .eh_host_reset_handler
= ibmvfc_eh_host_reset_handler
,
2516 .slave_alloc
= ibmvfc_slave_alloc
,
2517 .slave_configure
= ibmvfc_slave_configure
,
2518 .scan_finished
= ibmvfc_scan_finished
,
2519 .change_queue_depth
= ibmvfc_change_queue_depth
,
2520 .change_queue_type
= ibmvfc_change_queue_type
,
2522 .can_queue
= IBMVFC_MAX_REQUESTS_DEFAULT
,
2524 .sg_tablesize
= SG_ALL
,
2525 .max_sectors
= IBMVFC_MAX_SECTORS
,
2526 .use_clustering
= ENABLE_CLUSTERING
,
2527 .shost_attrs
= ibmvfc_attrs
,
2531 * ibmvfc_next_async_crq - Returns the next entry in async queue
2532 * @vhost: ibmvfc host struct
2535 * Pointer to next entry in queue / NULL if empty
2537 static struct ibmvfc_async_crq
*ibmvfc_next_async_crq(struct ibmvfc_host
*vhost
)
2539 struct ibmvfc_async_crq_queue
*async_crq
= &vhost
->async_crq
;
2540 struct ibmvfc_async_crq
*crq
;
2542 crq
= &async_crq
->msgs
[async_crq
->cur
];
2543 if (crq
->valid
& 0x80) {
2544 if (++async_crq
->cur
== async_crq
->size
)
2553 * ibmvfc_next_crq - Returns the next entry in message queue
2554 * @vhost: ibmvfc host struct
2557 * Pointer to next entry in queue / NULL if empty
2559 static struct ibmvfc_crq
*ibmvfc_next_crq(struct ibmvfc_host
*vhost
)
2561 struct ibmvfc_crq_queue
*queue
= &vhost
->crq
;
2562 struct ibmvfc_crq
*crq
;
2564 crq
= &queue
->msgs
[queue
->cur
];
2565 if (crq
->valid
& 0x80) {
2566 if (++queue
->cur
== queue
->size
)
2575 * ibmvfc_interrupt - Interrupt handler
2576 * @irq: number of irq to handle, not used
2577 * @dev_instance: ibmvfc_host that received interrupt
2582 static irqreturn_t
ibmvfc_interrupt(int irq
, void *dev_instance
)
2584 struct ibmvfc_host
*vhost
= (struct ibmvfc_host
*)dev_instance
;
2585 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
2586 struct ibmvfc_crq
*crq
;
2587 struct ibmvfc_async_crq
*async
;
2588 unsigned long flags
;
2591 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2592 vio_disable_interrupts(to_vio_dev(vhost
->dev
));
2594 /* Pull all the valid messages off the CRQ */
2595 while ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
2596 ibmvfc_handle_crq(crq
, vhost
);
2600 /* Pull all the valid messages off the async CRQ */
2601 while ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
2602 ibmvfc_handle_async(async
, vhost
);
2606 vio_enable_interrupts(vdev
);
2607 if ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
2608 vio_disable_interrupts(vdev
);
2609 ibmvfc_handle_crq(crq
, vhost
);
2611 } else if ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
2612 vio_disable_interrupts(vdev
);
2613 ibmvfc_handle_async(async
, vhost
);
2619 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2624 * ibmvfc_init_tgt - Set the next init job step for the target
2625 * @tgt: ibmvfc target struct
2626 * @job_step: job step to perform
2629 static void ibmvfc_init_tgt(struct ibmvfc_target
*tgt
,
2630 void (*job_step
) (struct ibmvfc_target
*))
2632 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT
);
2633 tgt
->job_step
= job_step
;
2634 wake_up(&tgt
->vhost
->work_wait_q
);
2638 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2639 * @tgt: ibmvfc target struct
2640 * @job_step: initialization job step
2643 static void ibmvfc_retry_tgt_init(struct ibmvfc_target
*tgt
,
2644 void (*job_step
) (struct ibmvfc_target
*))
2646 if (++tgt
->init_retries
> IBMVFC_MAX_INIT_RETRIES
) {
2647 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
2648 wake_up(&tgt
->vhost
->work_wait_q
);
2650 ibmvfc_init_tgt(tgt
, job_step
);
2654 * ibmvfc_release_tgt - Free memory allocated for a target
2655 * @kref: kref struct
2658 static void ibmvfc_release_tgt(struct kref
*kref
)
2660 struct ibmvfc_target
*tgt
= container_of(kref
, struct ibmvfc_target
, kref
);
2665 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2666 * @evt: ibmvfc event struct
2669 static void ibmvfc_tgt_prli_done(struct ibmvfc_event
*evt
)
2671 struct ibmvfc_target
*tgt
= evt
->tgt
;
2672 struct ibmvfc_host
*vhost
= evt
->vhost
;
2673 struct ibmvfc_process_login
*rsp
= &evt
->xfer_iu
->prli
;
2674 u32 status
= rsp
->common
.status
;
2676 vhost
->discovery_threads
--;
2677 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2679 case IBMVFC_MAD_SUCCESS
:
2680 tgt_dbg(tgt
, "Process Login succeeded\n");
2681 tgt
->need_login
= 0;
2682 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_ADD_RPORT
);
2684 case IBMVFC_MAD_DRIVER_FAILED
:
2686 case IBMVFC_MAD_CRQ_ERROR
:
2687 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
2689 case IBMVFC_MAD_FAILED
:
2691 tgt_err(tgt
, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2692 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
),
2693 rsp
->status
, rsp
->error
, status
);
2694 if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
2695 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
2699 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2700 ibmvfc_free_event(evt
);
2701 wake_up(&vhost
->work_wait_q
);
2705 * ibmvfc_tgt_send_prli - Send a process login
2706 * @tgt: ibmvfc target struct
2709 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*tgt
)
2711 struct ibmvfc_process_login
*prli
;
2712 struct ibmvfc_host
*vhost
= tgt
->vhost
;
2713 struct ibmvfc_event
*evt
;
2715 if (vhost
->discovery_threads
>= disc_threads
)
2718 kref_get(&tgt
->kref
);
2719 evt
= ibmvfc_get_event(vhost
);
2720 vhost
->discovery_threads
++;
2721 ibmvfc_init_event(evt
, ibmvfc_tgt_prli_done
, IBMVFC_MAD_FORMAT
);
2723 prli
= &evt
->iu
.prli
;
2724 memset(prli
, 0, sizeof(*prli
));
2725 prli
->common
.version
= 1;
2726 prli
->common
.opcode
= IBMVFC_PROCESS_LOGIN
;
2727 prli
->common
.length
= sizeof(*prli
);
2728 prli
->scsi_id
= tgt
->scsi_id
;
2730 prli
->parms
.type
= IBMVFC_SCSI_FCP_TYPE
;
2731 prli
->parms
.flags
= IBMVFC_PRLI_EST_IMG_PAIR
;
2732 prli
->parms
.service_parms
= IBMVFC_PRLI_INITIATOR_FUNC
;
2734 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
2735 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
2736 vhost
->discovery_threads
--;
2737 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2738 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2740 tgt_dbg(tgt
, "Sent process login\n");
2744 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2745 * @evt: ibmvfc event struct
2748 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event
*evt
)
2750 struct ibmvfc_target
*tgt
= evt
->tgt
;
2751 struct ibmvfc_host
*vhost
= evt
->vhost
;
2752 struct ibmvfc_port_login
*rsp
= &evt
->xfer_iu
->plogi
;
2753 u32 status
= rsp
->common
.status
;
2755 vhost
->discovery_threads
--;
2756 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2758 case IBMVFC_MAD_SUCCESS
:
2759 tgt_dbg(tgt
, "Port Login succeeded\n");
2760 if (tgt
->ids
.port_name
&&
2761 tgt
->ids
.port_name
!= wwn_to_u64(rsp
->service_parms
.port_name
)) {
2763 tgt_dbg(tgt
, "Port re-init required\n");
2766 tgt
->ids
.node_name
= wwn_to_u64(rsp
->service_parms
.node_name
);
2767 tgt
->ids
.port_name
= wwn_to_u64(rsp
->service_parms
.port_name
);
2768 tgt
->ids
.port_id
= tgt
->scsi_id
;
2769 tgt
->ids
.roles
= FC_PORT_ROLE_FCP_TARGET
;
2770 memcpy(&tgt
->service_parms
, &rsp
->service_parms
,
2771 sizeof(tgt
->service_parms
));
2772 memcpy(&tgt
->service_parms_change
, &rsp
->service_parms_change
,
2773 sizeof(tgt
->service_parms_change
));
2774 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_prli
);
2776 case IBMVFC_MAD_DRIVER_FAILED
:
2778 case IBMVFC_MAD_CRQ_ERROR
:
2779 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
2781 case IBMVFC_MAD_FAILED
:
2783 tgt_err(tgt
, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2784 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
,
2785 ibmvfc_get_fc_type(rsp
->fc_type
), rsp
->fc_type
,
2786 ibmvfc_get_ls_explain(rsp
->fc_explain
), rsp
->fc_explain
, status
);
2788 if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
2789 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
2793 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2794 ibmvfc_free_event(evt
);
2795 wake_up(&vhost
->work_wait_q
);
2799 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2800 * @tgt: ibmvfc target struct
2803 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*tgt
)
2805 struct ibmvfc_port_login
*plogi
;
2806 struct ibmvfc_host
*vhost
= tgt
->vhost
;
2807 struct ibmvfc_event
*evt
;
2809 if (vhost
->discovery_threads
>= disc_threads
)
2812 kref_get(&tgt
->kref
);
2813 evt
= ibmvfc_get_event(vhost
);
2814 vhost
->discovery_threads
++;
2815 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
2816 ibmvfc_init_event(evt
, ibmvfc_tgt_plogi_done
, IBMVFC_MAD_FORMAT
);
2818 plogi
= &evt
->iu
.plogi
;
2819 memset(plogi
, 0, sizeof(*plogi
));
2820 plogi
->common
.version
= 1;
2821 plogi
->common
.opcode
= IBMVFC_PORT_LOGIN
;
2822 plogi
->common
.length
= sizeof(*plogi
);
2823 plogi
->scsi_id
= tgt
->scsi_id
;
2825 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
2826 vhost
->discovery_threads
--;
2827 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2828 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2830 tgt_dbg(tgt
, "Sent port login\n");
2834 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2835 * @evt: ibmvfc event struct
2838 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event
*evt
)
2840 struct ibmvfc_target
*tgt
= evt
->tgt
;
2841 struct ibmvfc_host
*vhost
= evt
->vhost
;
2842 struct ibmvfc_implicit_logout
*rsp
= &evt
->xfer_iu
->implicit_logout
;
2843 u32 status
= rsp
->common
.status
;
2845 vhost
->discovery_threads
--;
2846 ibmvfc_free_event(evt
);
2847 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2850 case IBMVFC_MAD_SUCCESS
:
2851 tgt_dbg(tgt
, "Implicit Logout succeeded\n");
2853 case IBMVFC_MAD_DRIVER_FAILED
:
2854 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2855 wake_up(&vhost
->work_wait_q
);
2857 case IBMVFC_MAD_FAILED
:
2859 tgt_err(tgt
, "Implicit Logout failed: rc=0x%02X\n", status
);
2863 if (vhost
->action
== IBMVFC_HOST_ACTION_TGT_INIT
)
2864 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_plogi
);
2865 else if (vhost
->action
== IBMVFC_HOST_ACTION_QUERY_TGTS
&&
2866 tgt
->scsi_id
!= tgt
->new_scsi_id
)
2867 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
2868 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2869 wake_up(&vhost
->work_wait_q
);
2873 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2874 * @tgt: ibmvfc target struct
2877 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target
*tgt
)
2879 struct ibmvfc_implicit_logout
*mad
;
2880 struct ibmvfc_host
*vhost
= tgt
->vhost
;
2881 struct ibmvfc_event
*evt
;
2883 if (vhost
->discovery_threads
>= disc_threads
)
2886 kref_get(&tgt
->kref
);
2887 evt
= ibmvfc_get_event(vhost
);
2888 vhost
->discovery_threads
++;
2889 ibmvfc_init_event(evt
, ibmvfc_tgt_implicit_logout_done
, IBMVFC_MAD_FORMAT
);
2891 mad
= &evt
->iu
.implicit_logout
;
2892 memset(mad
, 0, sizeof(*mad
));
2893 mad
->common
.version
= 1;
2894 mad
->common
.opcode
= IBMVFC_IMPLICIT_LOGOUT
;
2895 mad
->common
.length
= sizeof(*mad
);
2896 mad
->old_scsi_id
= tgt
->scsi_id
;
2898 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
2899 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
2900 vhost
->discovery_threads
--;
2901 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2902 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2904 tgt_dbg(tgt
, "Sent Implicit Logout\n");
2908 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
2909 * @evt: ibmvfc event struct
2912 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event
*evt
)
2914 struct ibmvfc_target
*tgt
= evt
->tgt
;
2915 struct ibmvfc_host
*vhost
= evt
->vhost
;
2916 struct ibmvfc_query_tgt
*rsp
= &evt
->xfer_iu
->query_tgt
;
2917 u32 status
= rsp
->common
.status
;
2919 vhost
->discovery_threads
--;
2920 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2922 case IBMVFC_MAD_SUCCESS
:
2923 tgt_dbg(tgt
, "Query Target succeeded\n");
2924 tgt
->new_scsi_id
= rsp
->scsi_id
;
2925 if (rsp
->scsi_id
!= tgt
->scsi_id
)
2926 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
2928 case IBMVFC_MAD_DRIVER_FAILED
:
2930 case IBMVFC_MAD_CRQ_ERROR
:
2931 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
2933 case IBMVFC_MAD_FAILED
:
2935 tgt_err(tgt
, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2936 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
,
2937 ibmvfc_get_fc_type(rsp
->fc_type
), rsp
->fc_type
,
2938 ibmvfc_get_gs_explain(rsp
->fc_explain
), rsp
->fc_explain
, status
);
2940 if ((rsp
->status
& IBMVFC_FABRIC_MAPPED
) == IBMVFC_FABRIC_MAPPED
&&
2941 rsp
->error
== IBMVFC_UNABLE_TO_PERFORM_REQ
&&
2942 rsp
->fc_explain
== IBMVFC_PORT_NAME_NOT_REG
)
2943 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
2944 else if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
2945 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
2949 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2950 ibmvfc_free_event(evt
);
2951 wake_up(&vhost
->work_wait_q
);
2955 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
2956 * @tgt: ibmvfc target struct
2959 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*tgt
)
2961 struct ibmvfc_query_tgt
*query_tgt
;
2962 struct ibmvfc_host
*vhost
= tgt
->vhost
;
2963 struct ibmvfc_event
*evt
;
2965 if (vhost
->discovery_threads
>= disc_threads
)
2968 kref_get(&tgt
->kref
);
2969 evt
= ibmvfc_get_event(vhost
);
2970 vhost
->discovery_threads
++;
2972 ibmvfc_init_event(evt
, ibmvfc_tgt_query_target_done
, IBMVFC_MAD_FORMAT
);
2973 query_tgt
= &evt
->iu
.query_tgt
;
2974 memset(query_tgt
, 0, sizeof(*query_tgt
));
2975 query_tgt
->common
.version
= 1;
2976 query_tgt
->common
.opcode
= IBMVFC_QUERY_TARGET
;
2977 query_tgt
->common
.length
= sizeof(*query_tgt
);
2978 query_tgt
->wwpn
= tgt
->ids
.port_name
;
2980 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
2981 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
2982 vhost
->discovery_threads
--;
2983 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
2984 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
2986 tgt_dbg(tgt
, "Sent Query Target\n");
2990 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
2991 * @vhost: ibmvfc host struct
2992 * @scsi_id: SCSI ID to allocate target for
2995 * 0 on success / other on failure
2997 static int ibmvfc_alloc_target(struct ibmvfc_host
*vhost
, u64 scsi_id
)
2999 struct ibmvfc_target
*tgt
;
3000 unsigned long flags
;
3002 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3003 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3004 if (tgt
->scsi_id
== scsi_id
) {
3005 if (tgt
->need_login
)
3006 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3010 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3012 tgt
= mempool_alloc(vhost
->tgt_pool
, GFP_KERNEL
);
3014 dev_err(vhost
->dev
, "Target allocation failure for scsi id %08lx\n",
3019 tgt
->scsi_id
= scsi_id
;
3020 tgt
->new_scsi_id
= scsi_id
;
3022 tgt
->need_login
= 1;
3023 kref_init(&tgt
->kref
);
3024 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3025 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3026 list_add_tail(&tgt
->queue
, &vhost
->targets
);
3029 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3034 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3035 * @vhost: ibmvfc host struct
3038 * 0 on success / other on failure
3040 static int ibmvfc_alloc_targets(struct ibmvfc_host
*vhost
)
3044 for (i
= 0, rc
= 0; !rc
&& i
< vhost
->num_targets
; i
++)
3045 rc
= ibmvfc_alloc_target(vhost
,
3046 vhost
->disc_buf
->scsi_id
[i
] & IBMVFC_DISC_TGT_SCSI_ID_MASK
);
3052 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3053 * @evt: ibmvfc event struct
3056 static void ibmvfc_discover_targets_done(struct ibmvfc_event
*evt
)
3058 struct ibmvfc_host
*vhost
= evt
->vhost
;
3059 struct ibmvfc_discover_targets
*rsp
= &evt
->xfer_iu
->discover_targets
;
3060 u32 mad_status
= rsp
->common
.status
;
3062 switch (mad_status
) {
3063 case IBMVFC_MAD_SUCCESS
:
3064 ibmvfc_dbg(vhost
, "Discover Targets succeeded\n");
3065 vhost
->num_targets
= rsp
->num_written
;
3066 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_ALLOC_TGTS
);
3068 case IBMVFC_MAD_FAILED
:
3069 dev_err(vhost
->dev
, "Discover Targets failed: %s (%x:%x)\n",
3070 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
);
3071 ibmvfc_retry_host_init(vhost
);
3073 case IBMVFC_MAD_DRIVER_FAILED
:
3076 dev_err(vhost
->dev
, "Invalid Discover Targets response: 0x%x\n", mad_status
);
3077 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3081 ibmvfc_free_event(evt
);
3082 wake_up(&vhost
->work_wait_q
);
3086 * ibmvfc_discover_targets - Send Discover Targets MAD
3087 * @vhost: ibmvfc host struct
3090 static void ibmvfc_discover_targets(struct ibmvfc_host
*vhost
)
3092 struct ibmvfc_discover_targets
*mad
;
3093 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
3095 ibmvfc_init_event(evt
, ibmvfc_discover_targets_done
, IBMVFC_MAD_FORMAT
);
3096 mad
= &evt
->iu
.discover_targets
;
3097 memset(mad
, 0, sizeof(*mad
));
3098 mad
->common
.version
= 1;
3099 mad
->common
.opcode
= IBMVFC_DISC_TARGETS
;
3100 mad
->common
.length
= sizeof(*mad
);
3101 mad
->bufflen
= vhost
->disc_buf_sz
;
3102 mad
->buffer
.va
= vhost
->disc_buf_dma
;
3103 mad
->buffer
.len
= vhost
->disc_buf_sz
;
3104 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
3106 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
3107 ibmvfc_dbg(vhost
, "Sent discover targets\n");
3109 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3113 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3114 * @evt: ibmvfc event struct
3117 static void ibmvfc_npiv_login_done(struct ibmvfc_event
*evt
)
3119 struct ibmvfc_host
*vhost
= evt
->vhost
;
3120 u32 mad_status
= evt
->xfer_iu
->npiv_login
.common
.status
;
3121 struct ibmvfc_npiv_login_resp
*rsp
= &vhost
->login_buf
->resp
;
3122 unsigned int npiv_max_sectors
;
3124 switch (mad_status
) {
3125 case IBMVFC_MAD_SUCCESS
:
3126 ibmvfc_free_event(evt
);
3128 case IBMVFC_MAD_FAILED
:
3129 dev_err(vhost
->dev
, "NPIV Login failed: %s (%x:%x)\n",
3130 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
);
3131 if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
3132 ibmvfc_retry_host_init(vhost
);
3134 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3135 ibmvfc_free_event(evt
);
3137 case IBMVFC_MAD_CRQ_ERROR
:
3138 ibmvfc_retry_host_init(vhost
);
3139 case IBMVFC_MAD_DRIVER_FAILED
:
3140 ibmvfc_free_event(evt
);
3143 dev_err(vhost
->dev
, "Invalid NPIV Login response: 0x%x\n", mad_status
);
3144 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3145 ibmvfc_free_event(evt
);
3149 vhost
->client_migrated
= 0;
3151 if (!(rsp
->flags
& IBMVFC_NATIVE_FC
)) {
3152 dev_err(vhost
->dev
, "Virtual adapter does not support FC. %x\n",
3154 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3155 wake_up(&vhost
->work_wait_q
);
3159 if (rsp
->max_cmds
<= IBMVFC_NUM_INTERNAL_REQ
) {
3160 dev_err(vhost
->dev
, "Virtual adapter supported queue depth too small: %d\n",
3162 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3163 wake_up(&vhost
->work_wait_q
);
3167 npiv_max_sectors
= min((uint
)(rsp
->max_dma_len
>> 9), IBMVFC_MAX_SECTORS
);
3168 dev_info(vhost
->dev
, "Host partition: %s, device: %s %s %s max sectors %u\n",
3169 rsp
->partition_name
, rsp
->device_name
, rsp
->port_loc_code
,
3170 rsp
->drc_name
, npiv_max_sectors
);
3172 fc_host_fabric_name(vhost
->host
) = rsp
->node_name
;
3173 fc_host_node_name(vhost
->host
) = rsp
->node_name
;
3174 fc_host_port_name(vhost
->host
) = rsp
->port_name
;
3175 fc_host_port_id(vhost
->host
) = rsp
->scsi_id
;
3176 fc_host_port_type(vhost
->host
) = FC_PORTTYPE_NPIV
;
3177 fc_host_supported_classes(vhost
->host
) = 0;
3178 if (rsp
->service_parms
.class1_parms
[0] & 0x80000000)
3179 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS1
;
3180 if (rsp
->service_parms
.class2_parms
[0] & 0x80000000)
3181 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS2
;
3182 if (rsp
->service_parms
.class3_parms
[0] & 0x80000000)
3183 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS3
;
3184 fc_host_maxframe_size(vhost
->host
) =
3185 rsp
->service_parms
.common
.bb_rcv_sz
& 0x0fff;
3187 vhost
->host
->can_queue
= rsp
->max_cmds
- IBMVFC_NUM_INTERNAL_REQ
;
3188 vhost
->host
->max_sectors
= npiv_max_sectors
;
3189 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
3190 wake_up(&vhost
->work_wait_q
);
3194 * ibmvfc_npiv_login - Sends NPIV login
3195 * @vhost: ibmvfc host struct
3198 static void ibmvfc_npiv_login(struct ibmvfc_host
*vhost
)
3200 struct ibmvfc_npiv_login_mad
*mad
;
3201 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
3203 ibmvfc_gather_partition_info(vhost
);
3204 ibmvfc_set_login_info(vhost
);
3205 ibmvfc_init_event(evt
, ibmvfc_npiv_login_done
, IBMVFC_MAD_FORMAT
);
3207 memcpy(vhost
->login_buf
, &vhost
->login_info
, sizeof(vhost
->login_info
));
3208 mad
= &evt
->iu
.npiv_login
;
3209 memset(mad
, 0, sizeof(struct ibmvfc_npiv_login_mad
));
3210 mad
->common
.version
= 1;
3211 mad
->common
.opcode
= IBMVFC_NPIV_LOGIN
;
3212 mad
->common
.length
= sizeof(struct ibmvfc_npiv_login_mad
);
3213 mad
->buffer
.va
= vhost
->login_buf_dma
;
3214 mad
->buffer
.len
= sizeof(*vhost
->login_buf
);
3216 memset(vhost
->async_crq
.msgs
, 0, PAGE_SIZE
);
3217 vhost
->async_crq
.cur
= 0;
3218 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
3220 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
3221 ibmvfc_dbg(vhost
, "Sent NPIV login\n");
3223 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3227 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3228 * @vhost: ibmvfc host struct
3231 * 1 if work to do / 0 if not
3233 static int ibmvfc_dev_init_to_do(struct ibmvfc_host
*vhost
)
3235 struct ibmvfc_target
*tgt
;
3237 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3238 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
||
3239 tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
3247 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3248 * @vhost: ibmvfc host struct
3251 * 1 if work to do / 0 if not
3253 static int __ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
3255 struct ibmvfc_target
*tgt
;
3257 if (kthread_should_stop())
3259 switch (vhost
->action
) {
3260 case IBMVFC_HOST_ACTION_NONE
:
3261 case IBMVFC_HOST_ACTION_INIT_WAIT
:
3263 case IBMVFC_HOST_ACTION_TGT_INIT
:
3264 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
3265 if (vhost
->discovery_threads
== disc_threads
)
3267 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
3268 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
)
3270 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
3271 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
3274 case IBMVFC_HOST_ACTION_INIT
:
3275 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
3276 case IBMVFC_HOST_ACTION_TGT_ADD
:
3277 case IBMVFC_HOST_ACTION_TGT_DEL
:
3278 case IBMVFC_HOST_ACTION_QUERY
:
3287 * ibmvfc_work_to_do - Is there task level work to do?
3288 * @vhost: ibmvfc host struct
3291 * 1 if work to do / 0 if not
3293 static int ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
3295 unsigned long flags
;
3298 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3299 rc
= __ibmvfc_work_to_do(vhost
);
3300 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3305 * ibmvfc_log_ae - Log async events if necessary
3306 * @vhost: ibmvfc host struct
3307 * @events: events to log
3310 static void ibmvfc_log_ae(struct ibmvfc_host
*vhost
, int events
)
3312 if (events
& IBMVFC_AE_RSCN
)
3313 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_RSCN
, 0);
3314 if ((events
& IBMVFC_AE_LINKDOWN
) &&
3315 vhost
->state
>= IBMVFC_HALTED
)
3316 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKDOWN
, 0);
3317 if ((events
& IBMVFC_AE_LINKUP
) &&
3318 vhost
->state
== IBMVFC_INITIALIZING
)
3319 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKUP
, 0);
3323 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3324 * @tgt: ibmvfc target struct
3327 static void ibmvfc_tgt_add_rport(struct ibmvfc_target
*tgt
)
3329 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3330 struct fc_rport
*rport
;
3331 unsigned long flags
;
3333 tgt_dbg(tgt
, "Adding rport\n");
3334 rport
= fc_remote_port_add(vhost
->host
, 0, &tgt
->ids
);
3335 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3337 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3339 tgt_dbg(tgt
, "rport add succeeded\n");
3340 rport
->maxframe_size
= tgt
->service_parms
.common
.bb_rcv_sz
& 0x0fff;
3341 rport
->supported_classes
= 0;
3342 if (tgt
->service_parms
.class1_parms
[0] & 0x80000000)
3343 rport
->supported_classes
|= FC_COS_CLASS1
;
3344 if (tgt
->service_parms
.class2_parms
[0] & 0x80000000)
3345 rport
->supported_classes
|= FC_COS_CLASS2
;
3346 if (tgt
->service_parms
.class3_parms
[0] & 0x80000000)
3347 rport
->supported_classes
|= FC_COS_CLASS3
;
3349 tgt_dbg(tgt
, "rport add failed\n");
3350 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3354 * ibmvfc_do_work - Do task level work
3355 * @vhost: ibmvfc host struct
3358 static void ibmvfc_do_work(struct ibmvfc_host
*vhost
)
3360 struct ibmvfc_target
*tgt
;
3361 unsigned long flags
;
3362 struct fc_rport
*rport
;
3364 ibmvfc_log_ae(vhost
, vhost
->events_to_log
);
3365 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3366 vhost
->events_to_log
= 0;
3367 switch (vhost
->action
) {
3368 case IBMVFC_HOST_ACTION_NONE
:
3369 case IBMVFC_HOST_ACTION_INIT_WAIT
:
3371 case IBMVFC_HOST_ACTION_INIT
:
3372 BUG_ON(vhost
->state
!= IBMVFC_INITIALIZING
);
3373 vhost
->job_step(vhost
);
3375 case IBMVFC_HOST_ACTION_QUERY
:
3376 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
3377 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_query_target
);
3378 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY_TGTS
);
3380 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
3381 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3382 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
3388 if (!ibmvfc_dev_init_to_do(vhost
))
3389 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
3391 case IBMVFC_HOST_ACTION_TGT_DEL
:
3392 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3393 if (tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
3394 tgt_dbg(tgt
, "Deleting rport\n");
3397 list_del(&tgt
->queue
);
3398 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3400 fc_remote_port_delete(rport
);
3401 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3406 if (vhost
->state
== IBMVFC_INITIALIZING
) {
3407 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
3408 vhost
->job_step
= ibmvfc_discover_targets
;
3410 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
3411 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3412 scsi_unblock_requests(vhost
->host
);
3413 wake_up(&vhost
->init_wait_q
);
3417 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
3418 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_INIT
);
3419 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3420 ibmvfc_alloc_targets(vhost
);
3421 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3423 case IBMVFC_HOST_ACTION_TGT_INIT
:
3424 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3425 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
3431 if (!ibmvfc_dev_init_to_do(vhost
)) {
3432 ibmvfc_set_host_state(vhost
, IBMVFC_ACTIVE
);
3433 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_ADD
);
3434 vhost
->init_retries
= 0;
3435 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3436 scsi_unblock_requests(vhost
->host
);
3440 case IBMVFC_HOST_ACTION_TGT_ADD
:
3441 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3442 if (tgt
->action
== IBMVFC_TGT_ACTION_ADD_RPORT
) {
3443 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3444 ibmvfc_tgt_add_rport(tgt
);
3446 } else if (tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
3447 tgt_dbg(tgt
, "Deleting rport\n");
3450 list_del(&tgt
->queue
);
3451 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3453 fc_remote_port_delete(rport
);
3454 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3459 if (vhost
->reinit
) {
3461 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
3463 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
3464 wake_up(&vhost
->init_wait_q
);
3471 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3475 * ibmvfc_work - Do task level work
3476 * @data: ibmvfc host struct
3481 static int ibmvfc_work(void *data
)
3483 struct ibmvfc_host
*vhost
= data
;
3486 set_user_nice(current
, -20);
3489 rc
= wait_event_interruptible(vhost
->work_wait_q
,
3490 ibmvfc_work_to_do(vhost
));
3494 if (kthread_should_stop())
3497 ibmvfc_do_work(vhost
);
3500 ibmvfc_dbg(vhost
, "ibmvfc kthread exiting...\n");
3505 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3506 * @vhost: ibmvfc host struct
3508 * Allocates a page for messages, maps it for dma, and registers
3509 * the crq with the hypervisor.
3512 * zero on success / other on failure
3514 static int ibmvfc_init_crq(struct ibmvfc_host
*vhost
)
3516 int rc
, retrc
= -ENOMEM
;
3517 struct device
*dev
= vhost
->dev
;
3518 struct vio_dev
*vdev
= to_vio_dev(dev
);
3519 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
3522 crq
->msgs
= (struct ibmvfc_crq
*)get_zeroed_page(GFP_KERNEL
);
3527 crq
->size
= PAGE_SIZE
/ sizeof(*crq
->msgs
);
3528 crq
->msg_token
= dma_map_single(dev
, crq
->msgs
,
3529 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
3531 if (dma_mapping_error(crq
->msg_token
))
3534 retrc
= rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
3535 crq
->msg_token
, PAGE_SIZE
);
3537 if (rc
== H_RESOURCE
)
3538 /* maybe kexecing and resource is busy. try a reset */
3539 retrc
= rc
= ibmvfc_reset_crq(vhost
);
3542 dev_warn(dev
, "Partner adapter not ready\n");
3544 dev_warn(dev
, "Error %d opening adapter\n", rc
);
3545 goto reg_crq_failed
;
3550 if ((rc
= request_irq(vdev
->irq
, ibmvfc_interrupt
, 0, IBMVFC_NAME
, vhost
))) {
3551 dev_err(dev
, "Couldn't register irq 0x%x. rc=%d\n", vdev
->irq
, rc
);
3552 goto req_irq_failed
;
3555 if ((rc
= vio_enable_interrupts(vdev
))) {
3556 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
3557 goto req_irq_failed
;
3566 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
3567 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
3569 dma_unmap_single(dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
3571 free_page((unsigned long)crq
->msgs
);
3576 * ibmvfc_free_mem - Free memory for vhost
3577 * @vhost: ibmvfc host struct
3582 static void ibmvfc_free_mem(struct ibmvfc_host
*vhost
)
3584 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
3587 mempool_destroy(vhost
->tgt_pool
);
3588 kfree(vhost
->trace
);
3589 dma_free_coherent(vhost
->dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
3590 vhost
->disc_buf_dma
);
3591 dma_free_coherent(vhost
->dev
, sizeof(*vhost
->login_buf
),
3592 vhost
->login_buf
, vhost
->login_buf_dma
);
3593 dma_pool_destroy(vhost
->sg_pool
);
3594 dma_unmap_single(vhost
->dev
, async_q
->msg_token
,
3595 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
3596 free_page((unsigned long)async_q
->msgs
);
3601 * ibmvfc_alloc_mem - Allocate memory for vhost
3602 * @vhost: ibmvfc host struct
3605 * 0 on success / non-zero on failure
3607 static int ibmvfc_alloc_mem(struct ibmvfc_host
*vhost
)
3609 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
3610 struct device
*dev
= vhost
->dev
;
3613 async_q
->msgs
= (struct ibmvfc_async_crq
*)get_zeroed_page(GFP_KERNEL
);
3614 if (!async_q
->msgs
) {
3615 dev_err(dev
, "Couldn't allocate async queue.\n");
3619 async_q
->size
= PAGE_SIZE
/ sizeof(struct ibmvfc_async_crq
);
3620 async_q
->msg_token
= dma_map_single(dev
, async_q
->msgs
,
3621 async_q
->size
* sizeof(*async_q
->msgs
),
3624 if (dma_mapping_error(async_q
->msg_token
)) {
3625 dev_err(dev
, "Failed to map async queue\n");
3626 goto free_async_crq
;
3629 vhost
->sg_pool
= dma_pool_create(IBMVFC_NAME
, dev
,
3630 SG_ALL
* sizeof(struct srp_direct_buf
),
3631 sizeof(struct srp_direct_buf
), 0);
3633 if (!vhost
->sg_pool
) {
3634 dev_err(dev
, "Failed to allocate sg pool\n");
3635 goto unmap_async_crq
;
3638 vhost
->login_buf
= dma_alloc_coherent(dev
, sizeof(*vhost
->login_buf
),
3639 &vhost
->login_buf_dma
, GFP_KERNEL
);
3641 if (!vhost
->login_buf
) {
3642 dev_err(dev
, "Couldn't allocate NPIV login buffer\n");
3646 vhost
->disc_buf_sz
= sizeof(vhost
->disc_buf
->scsi_id
[0]) * max_targets
;
3647 vhost
->disc_buf
= dma_alloc_coherent(dev
, vhost
->disc_buf_sz
,
3648 &vhost
->disc_buf_dma
, GFP_KERNEL
);
3650 if (!vhost
->disc_buf
) {
3651 dev_err(dev
, "Couldn't allocate Discover Targets buffer\n");
3652 goto free_login_buffer
;
3655 vhost
->trace
= kcalloc(IBMVFC_NUM_TRACE_ENTRIES
,
3656 sizeof(struct ibmvfc_trace_entry
), GFP_KERNEL
);
3659 goto free_disc_buffer
;
3661 vhost
->tgt_pool
= mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ
,
3662 sizeof(struct ibmvfc_target
));
3664 if (!vhost
->tgt_pool
) {
3665 dev_err(dev
, "Couldn't allocate target memory pool\n");
3673 kfree(vhost
->trace
);
3675 dma_free_coherent(dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
3676 vhost
->disc_buf_dma
);
3678 dma_free_coherent(dev
, sizeof(*vhost
->login_buf
),
3679 vhost
->login_buf
, vhost
->login_buf_dma
);
3681 dma_pool_destroy(vhost
->sg_pool
);
3683 dma_unmap_single(dev
, async_q
->msg_token
,
3684 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
3686 free_page((unsigned long)async_q
->msgs
);
3693 * ibmvfc_probe - Adapter hot plug add entry point
3694 * @vdev: vio device struct
3695 * @id: vio device id struct
3698 * 0 on success / non-zero on failure
3700 static int ibmvfc_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
3702 struct ibmvfc_host
*vhost
;
3703 struct Scsi_Host
*shost
;
3704 struct device
*dev
= &vdev
->dev
;
3708 shost
= scsi_host_alloc(&driver_template
, sizeof(*vhost
));
3710 dev_err(dev
, "Couldn't allocate host data\n");
3714 shost
->transportt
= ibmvfc_transport_template
;
3715 shost
->can_queue
= max_requests
;
3716 shost
->max_lun
= max_lun
;
3717 shost
->max_id
= max_targets
;
3718 shost
->max_sectors
= IBMVFC_MAX_SECTORS
;
3719 shost
->max_cmd_len
= IBMVFC_MAX_CDB_LEN
;
3720 shost
->unique_id
= shost
->host_no
;
3722 vhost
= shost_priv(shost
);
3723 INIT_LIST_HEAD(&vhost
->sent
);
3724 INIT_LIST_HEAD(&vhost
->free
);
3725 INIT_LIST_HEAD(&vhost
->targets
);
3726 sprintf(vhost
->name
, IBMVFC_NAME
);
3727 vhost
->host
= shost
;
3729 vhost
->partition_number
= -1;
3730 vhost
->log_level
= log_level
;
3731 strcpy(vhost
->partition_name
, "UNKNOWN");
3732 init_waitqueue_head(&vhost
->work_wait_q
);
3733 init_waitqueue_head(&vhost
->init_wait_q
);
3735 if ((rc
= ibmvfc_alloc_mem(vhost
)))
3736 goto free_scsi_host
;
3738 vhost
->work_thread
= kthread_run(ibmvfc_work
, vhost
, "%s_%d", IBMVFC_NAME
,
3741 if (IS_ERR(vhost
->work_thread
)) {
3742 dev_err(dev
, "Couldn't create kernel thread: %ld\n",
3743 PTR_ERR(vhost
->work_thread
));
3747 if ((rc
= ibmvfc_init_crq(vhost
))) {
3748 dev_err(dev
, "Couldn't initialize crq. rc=%d\n", rc
);
3752 if ((rc
= ibmvfc_init_event_pool(vhost
))) {
3753 dev_err(dev
, "Couldn't initialize event pool. rc=%d\n", rc
);
3757 if ((rc
= scsi_add_host(shost
, dev
)))
3758 goto release_event_pool
;
3760 if ((rc
= ibmvfc_create_trace_file(&shost
->shost_dev
.kobj
,
3761 &ibmvfc_trace_attr
))) {
3762 dev_err(dev
, "Failed to create trace file. rc=%d\n", rc
);
3766 dev_set_drvdata(dev
, vhost
);
3767 spin_lock(&ibmvfc_driver_lock
);
3768 list_add_tail(&vhost
->queue
, &ibmvfc_head
);
3769 spin_unlock(&ibmvfc_driver_lock
);
3771 ibmvfc_send_crq_init(vhost
);
3772 scsi_scan_host(shost
);
3776 scsi_remove_host(shost
);
3778 ibmvfc_free_event_pool(vhost
);
3780 ibmvfc_release_crq_queue(vhost
);
3782 kthread_stop(vhost
->work_thread
);
3784 ibmvfc_free_mem(vhost
);
3786 scsi_host_put(shost
);
3793 * ibmvfc_remove - Adapter hot plug remove entry point
3794 * @vdev: vio device struct
3799 static int ibmvfc_remove(struct vio_dev
*vdev
)
3801 struct ibmvfc_host
*vhost
= dev_get_drvdata(&vdev
->dev
);
3802 unsigned long flags
;
3805 ibmvfc_remove_trace_file(&vhost
->host
->shost_dev
.kobj
, &ibmvfc_trace_attr
);
3806 kthread_stop(vhost
->work_thread
);
3807 fc_remove_host(vhost
->host
);
3808 scsi_remove_host(vhost
->host
);
3809 ibmvfc_release_crq_queue(vhost
);
3811 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3812 ibmvfc_purge_requests(vhost
, DID_ERROR
);
3813 ibmvfc_free_event_pool(vhost
);
3814 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3816 ibmvfc_free_mem(vhost
);
3817 spin_lock(&ibmvfc_driver_lock
);
3818 list_del(&vhost
->queue
);
3819 spin_unlock(&ibmvfc_driver_lock
);
3820 scsi_host_put(vhost
->host
);
3826 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
3827 * @vdev: vio device struct
3830 * Number of bytes the driver will need to DMA map at the same time in
3831 * order to perform well.
3833 static unsigned long ibmvfc_get_desired_dma(struct vio_dev
*vdev
)
3835 unsigned long pool_dma
= max_requests
* sizeof(union ibmvfc_iu
);
3836 return pool_dma
+ ((512 * 1024) * driver_template
.cmd_per_lun
);
3839 static struct vio_device_id ibmvfc_device_table
[] __devinitdata
= {
3840 {"fcp", "IBM,vfc-client"},
3843 MODULE_DEVICE_TABLE(vio
, ibmvfc_device_table
);
3845 static struct vio_driver ibmvfc_driver
= {
3846 .id_table
= ibmvfc_device_table
,
3847 .probe
= ibmvfc_probe
,
3848 .remove
= ibmvfc_remove
,
3849 .get_desired_dma
= ibmvfc_get_desired_dma
,
3851 .name
= IBMVFC_NAME
,
3852 .owner
= THIS_MODULE
,
3856 static struct fc_function_template ibmvfc_transport_functions
= {
3857 .show_host_fabric_name
= 1,
3858 .show_host_node_name
= 1,
3859 .show_host_port_name
= 1,
3860 .show_host_supported_classes
= 1,
3861 .show_host_port_type
= 1,
3862 .show_host_port_id
= 1,
3864 .get_host_port_state
= ibmvfc_get_host_port_state
,
3865 .show_host_port_state
= 1,
3867 .get_host_speed
= ibmvfc_get_host_speed
,
3868 .show_host_speed
= 1,
3870 .issue_fc_host_lip
= ibmvfc_issue_fc_host_lip
,
3871 .terminate_rport_io
= ibmvfc_terminate_rport_io
,
3873 .show_rport_maxframe_size
= 1,
3874 .show_rport_supported_classes
= 1,
3876 .set_rport_dev_loss_tmo
= ibmvfc_set_rport_dev_loss_tmo
,
3877 .show_rport_dev_loss_tmo
= 1,
3879 .get_starget_node_name
= ibmvfc_get_starget_node_name
,
3880 .show_starget_node_name
= 1,
3882 .get_starget_port_name
= ibmvfc_get_starget_port_name
,
3883 .show_starget_port_name
= 1,
3885 .get_starget_port_id
= ibmvfc_get_starget_port_id
,
3886 .show_starget_port_id
= 1,
3890 * ibmvfc_module_init - Initialize the ibmvfc module
3893 * 0 on success / other on failure
3895 static int __init
ibmvfc_module_init(void)
3899 if (!firmware_has_feature(FW_FEATURE_VIO
))
3902 printk(KERN_INFO IBMVFC_NAME
": IBM Virtual Fibre Channel Driver version: %s %s\n",
3903 IBMVFC_DRIVER_VERSION
, IBMVFC_DRIVER_DATE
);
3905 ibmvfc_transport_template
= fc_attach_transport(&ibmvfc_transport_functions
);
3906 if (!ibmvfc_transport_template
)
3909 rc
= vio_register_driver(&ibmvfc_driver
);
3911 fc_release_transport(ibmvfc_transport_template
);
3916 * ibmvfc_module_exit - Teardown the ibmvfc module
3921 static void __exit
ibmvfc_module_exit(void)
3923 vio_unregister_driver(&ibmvfc_driver
);
3924 fc_release_transport(ibmvfc_transport_template
);
3927 module_init(ibmvfc_module_init
);
3928 module_exit(ibmvfc_module_exit
);