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>
42 #include <scsi/scsi_bsg_fc.h>
45 static unsigned int init_timeout
= IBMVFC_INIT_TIMEOUT
;
46 static unsigned int default_timeout
= IBMVFC_DEFAULT_TIMEOUT
;
47 static unsigned int max_lun
= IBMVFC_MAX_LUN
;
48 static unsigned int max_targets
= IBMVFC_MAX_TARGETS
;
49 static unsigned int max_requests
= IBMVFC_MAX_REQUESTS_DEFAULT
;
50 static unsigned int disc_threads
= IBMVFC_MAX_DISC_THREADS
;
51 static unsigned int dev_loss_tmo
= IBMVFC_DEV_LOSS_TMO
;
52 static unsigned int ibmvfc_debug
= IBMVFC_DEBUG
;
53 static unsigned int log_level
= IBMVFC_DEFAULT_LOG_LEVEL
;
54 static LIST_HEAD(ibmvfc_head
);
55 static DEFINE_SPINLOCK(ibmvfc_driver_lock
);
56 static struct scsi_transport_template
*ibmvfc_transport_template
;
58 MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
59 MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(IBMVFC_DRIVER_VERSION
);
63 module_param_named(init_timeout
, init_timeout
, uint
, S_IRUGO
| S_IWUSR
);
64 MODULE_PARM_DESC(init_timeout
, "Initialization timeout in seconds. "
65 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT
) "]");
66 module_param_named(default_timeout
, default_timeout
, uint
, S_IRUGO
| S_IWUSR
);
67 MODULE_PARM_DESC(default_timeout
,
68 "Default timeout in seconds for initialization and EH commands. "
69 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT
) "]");
70 module_param_named(max_requests
, max_requests
, uint
, S_IRUGO
);
71 MODULE_PARM_DESC(max_requests
, "Maximum requests for this adapter. "
72 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT
) "]");
73 module_param_named(max_lun
, max_lun
, uint
, S_IRUGO
);
74 MODULE_PARM_DESC(max_lun
, "Maximum allowed LUN. "
75 "[Default=" __stringify(IBMVFC_MAX_LUN
) "]");
76 module_param_named(max_targets
, max_targets
, uint
, S_IRUGO
);
77 MODULE_PARM_DESC(max_targets
, "Maximum allowed targets. "
78 "[Default=" __stringify(IBMVFC_MAX_TARGETS
) "]");
79 module_param_named(disc_threads
, disc_threads
, uint
, S_IRUGO
);
80 MODULE_PARM_DESC(disc_threads
, "Number of device discovery threads to use. "
81 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS
) "]");
82 module_param_named(debug
, ibmvfc_debug
, uint
, S_IRUGO
| S_IWUSR
);
83 MODULE_PARM_DESC(debug
, "Enable driver debug information. "
84 "[Default=" __stringify(IBMVFC_DEBUG
) "]");
85 module_param_named(dev_loss_tmo
, dev_loss_tmo
, uint
, S_IRUGO
| S_IWUSR
);
86 MODULE_PARM_DESC(dev_loss_tmo
, "Maximum number of seconds that the FC "
87 "transport should insulate the loss of a remote port. Once this "
88 "value is exceeded, the scsi target is removed. "
89 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO
) "]");
90 module_param_named(log_level
, log_level
, uint
, 0);
91 MODULE_PARM_DESC(log_level
, "Set to 0 - 4 for increasing verbosity of device driver. "
92 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL
) "]");
102 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_ESTABLISH
, DID_ERROR
, 1, 1, "unable to establish" },
103 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_FAULT
, DID_OK
, 1, 0, "transport fault" },
104 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CMD_TIMEOUT
, DID_TIME_OUT
, 1, 1, "command timeout" },
105 { IBMVFC_FABRIC_MAPPED
, IBMVFC_ENETDOWN
, DID_TRANSPORT_DISRUPTED
, 1, 1, "network down" },
106 { IBMVFC_FABRIC_MAPPED
, IBMVFC_HW_FAILURE
, DID_ERROR
, 1, 1, "hardware failure" },
107 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DOWN_ERR
, DID_REQUEUE
, 0, 0, "link down" },
108 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_DEAD_ERR
, DID_ERROR
, 0, 0, "link dead" },
109 { IBMVFC_FABRIC_MAPPED
, IBMVFC_UNABLE_TO_REGISTER
, DID_ERROR
, 1, 1, "unable to register" },
110 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_BUSY
, DID_BUS_BUSY
, 1, 0, "transport busy" },
111 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_DEAD
, DID_ERROR
, 0, 1, "transport dead" },
112 { IBMVFC_FABRIC_MAPPED
, IBMVFC_CONFIG_ERROR
, DID_ERROR
, 1, 1, "configuration error" },
113 { IBMVFC_FABRIC_MAPPED
, IBMVFC_NAME_SERVER_FAIL
, DID_ERROR
, 1, 1, "name server failure" },
114 { IBMVFC_FABRIC_MAPPED
, IBMVFC_LINK_HALTED
, DID_REQUEUE
, 1, 0, "link halted" },
115 { IBMVFC_FABRIC_MAPPED
, IBMVFC_XPORT_GENERAL
, DID_OK
, 1, 0, "general transport error" },
117 { IBMVFC_VIOS_FAILURE
, IBMVFC_CRQ_FAILURE
, DID_REQUEUE
, 1, 1, "CRQ failure" },
118 { IBMVFC_VIOS_FAILURE
, IBMVFC_SW_FAILURE
, DID_ERROR
, 0, 1, "software failure" },
119 { IBMVFC_VIOS_FAILURE
, IBMVFC_INVALID_PARAMETER
, DID_ERROR
, 0, 1, "invalid parameter" },
120 { IBMVFC_VIOS_FAILURE
, IBMVFC_MISSING_PARAMETER
, DID_ERROR
, 0, 1, "missing parameter" },
121 { IBMVFC_VIOS_FAILURE
, IBMVFC_HOST_IO_BUS
, DID_ERROR
, 1, 1, "host I/O bus failure" },
122 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED
, DID_ERROR
, 0, 1, "transaction cancelled" },
123 { IBMVFC_VIOS_FAILURE
, IBMVFC_TRANS_CANCELLED_IMPLICIT
, DID_ERROR
, 0, 1, "transaction cancelled implicit" },
124 { IBMVFC_VIOS_FAILURE
, IBMVFC_INSUFFICIENT_RESOURCE
, DID_REQUEUE
, 1, 1, "insufficient resources" },
125 { IBMVFC_VIOS_FAILURE
, IBMVFC_PLOGI_REQUIRED
, DID_ERROR
, 0, 1, "port login required" },
126 { IBMVFC_VIOS_FAILURE
, IBMVFC_COMMAND_FAILED
, DID_ERROR
, 1, 1, "command failed" },
128 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_ELS_CMD_CODE
, DID_ERROR
, 0, 1, "invalid ELS command code" },
129 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_VERSION
, DID_ERROR
, 0, 1, "invalid version level" },
130 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_ERROR
, DID_ERROR
, 1, 1, "logical error" },
131 { IBMVFC_FC_FAILURE
, IBMVFC_INVALID_CT_IU_SIZE
, DID_ERROR
, 0, 1, "invalid CT_IU size" },
132 { IBMVFC_FC_FAILURE
, IBMVFC_LOGICAL_BUSY
, DID_REQUEUE
, 1, 0, "logical busy" },
133 { IBMVFC_FC_FAILURE
, IBMVFC_PROTOCOL_ERROR
, DID_ERROR
, 1, 1, "protocol error" },
134 { IBMVFC_FC_FAILURE
, IBMVFC_UNABLE_TO_PERFORM_REQ
, DID_ERROR
, 1, 1, "unable to perform request" },
135 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_NOT_SUPPORTED
, DID_ERROR
, 0, 0, "command not supported" },
136 { IBMVFC_FC_FAILURE
, IBMVFC_SERVER_NOT_AVAIL
, DID_ERROR
, 0, 1, "server not available" },
137 { IBMVFC_FC_FAILURE
, IBMVFC_CMD_IN_PROGRESS
, DID_ERROR
, 0, 1, "command already in progress" },
138 { IBMVFC_FC_FAILURE
, IBMVFC_VENDOR_SPECIFIC
, DID_ERROR
, 1, 1, "vendor specific" },
140 { IBMVFC_FC_SCSI_ERROR
, 0, DID_OK
, 1, 0, "SCSI error" },
143 static void ibmvfc_npiv_login(struct ibmvfc_host
*);
144 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*);
145 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*);
146 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*);
147 static void ibmvfc_npiv_logout(struct ibmvfc_host
*);
149 static const char *unknown_error
= "unknown error";
151 #ifdef CONFIG_SCSI_IBMVFC_TRACE
153 * ibmvfc_trc_start - Log a start trace entry
154 * @evt: ibmvfc event struct
157 static void ibmvfc_trc_start(struct ibmvfc_event
*evt
)
159 struct ibmvfc_host
*vhost
= evt
->vhost
;
160 struct ibmvfc_cmd
*vfc_cmd
= &evt
->iu
.cmd
;
161 struct ibmvfc_mad_common
*mad
= &evt
->iu
.mad_common
;
162 struct ibmvfc_trace_entry
*entry
;
164 entry
= &vhost
->trace
[vhost
->trace_index
++];
166 entry
->time
= jiffies
;
167 entry
->fmt
= evt
->crq
.format
;
168 entry
->type
= IBMVFC_TRC_START
;
170 switch (entry
->fmt
) {
171 case IBMVFC_CMD_FORMAT
:
172 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
173 entry
->scsi_id
= vfc_cmd
->tgt_scsi_id
;
174 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
175 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
176 entry
->u
.start
.xfer_len
= vfc_cmd
->iu
.xfer_len
;
178 case IBMVFC_MAD_FORMAT
:
179 entry
->op_code
= mad
->opcode
;
187 * ibmvfc_trc_end - Log an end trace entry
188 * @evt: ibmvfc event struct
191 static void ibmvfc_trc_end(struct ibmvfc_event
*evt
)
193 struct ibmvfc_host
*vhost
= evt
->vhost
;
194 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
195 struct ibmvfc_mad_common
*mad
= &evt
->xfer_iu
->mad_common
;
196 struct ibmvfc_trace_entry
*entry
= &vhost
->trace
[vhost
->trace_index
++];
199 entry
->time
= jiffies
;
200 entry
->fmt
= evt
->crq
.format
;
201 entry
->type
= IBMVFC_TRC_END
;
203 switch (entry
->fmt
) {
204 case IBMVFC_CMD_FORMAT
:
205 entry
->op_code
= vfc_cmd
->iu
.cdb
[0];
206 entry
->scsi_id
= vfc_cmd
->tgt_scsi_id
;
207 entry
->lun
= scsilun_to_int(&vfc_cmd
->iu
.lun
);
208 entry
->tmf_flags
= vfc_cmd
->iu
.tmf_flags
;
209 entry
->u
.end
.status
= vfc_cmd
->status
;
210 entry
->u
.end
.error
= vfc_cmd
->error
;
211 entry
->u
.end
.fcp_rsp_flags
= vfc_cmd
->rsp
.flags
;
212 entry
->u
.end
.rsp_code
= vfc_cmd
->rsp
.data
.info
.rsp_code
;
213 entry
->u
.end
.scsi_status
= vfc_cmd
->rsp
.scsi_status
;
215 case IBMVFC_MAD_FORMAT
:
216 entry
->op_code
= mad
->opcode
;
217 entry
->u
.end
.status
= mad
->status
;
226 #define ibmvfc_trc_start(evt) do { } while (0)
227 #define ibmvfc_trc_end(evt) do { } while (0)
231 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
232 * @status: status / error class
236 * index into cmd_status / -EINVAL on failure
238 static int ibmvfc_get_err_index(u16 status
, u16 error
)
242 for (i
= 0; i
< ARRAY_SIZE(cmd_status
); i
++)
243 if ((cmd_status
[i
].status
& status
) == cmd_status
[i
].status
&&
244 cmd_status
[i
].error
== error
)
251 * ibmvfc_get_cmd_error - Find the error description for the fcp response
252 * @status: status / error class
256 * error description string
258 static const char *ibmvfc_get_cmd_error(u16 status
, u16 error
)
260 int rc
= ibmvfc_get_err_index(status
, error
);
262 return cmd_status
[rc
].name
;
263 return unknown_error
;
267 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
268 * @vfc_cmd: ibmvfc command struct
271 * SCSI result value to return for completed command
273 static int ibmvfc_get_err_result(struct ibmvfc_cmd
*vfc_cmd
)
276 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
277 int fc_rsp_len
= rsp
->fcp_rsp_len
;
279 if ((rsp
->flags
& FCP_RSP_LEN_VALID
) &&
280 ((fc_rsp_len
&& fc_rsp_len
!= 4 && fc_rsp_len
!= 8) ||
281 rsp
->data
.info
.rsp_code
))
282 return DID_ERROR
<< 16;
284 err
= ibmvfc_get_err_index(vfc_cmd
->status
, vfc_cmd
->error
);
286 return rsp
->scsi_status
| (cmd_status
[err
].result
<< 16);
287 return rsp
->scsi_status
| (DID_ERROR
<< 16);
291 * ibmvfc_retry_cmd - Determine if error status is retryable
292 * @status: status / error class
296 * 1 if error should be retried / 0 if it should not
298 static int ibmvfc_retry_cmd(u16 status
, u16 error
)
300 int rc
= ibmvfc_get_err_index(status
, error
);
303 return cmd_status
[rc
].retry
;
307 static const char *unknown_fc_explain
= "unknown fc explain";
309 static const struct {
313 { 0x00, "no additional explanation" },
314 { 0x01, "service parameter error - options" },
315 { 0x03, "service parameter error - initiator control" },
316 { 0x05, "service parameter error - recipient control" },
317 { 0x07, "service parameter error - received data field size" },
318 { 0x09, "service parameter error - concurrent seq" },
319 { 0x0B, "service parameter error - credit" },
320 { 0x0D, "invalid N_Port/F_Port_Name" },
321 { 0x0E, "invalid node/Fabric Name" },
322 { 0x0F, "invalid common service parameters" },
323 { 0x11, "invalid association header" },
324 { 0x13, "association header required" },
325 { 0x15, "invalid originator S_ID" },
326 { 0x17, "invalid OX_ID-RX-ID combination" },
327 { 0x19, "command (request) already in progress" },
328 { 0x1E, "N_Port Login requested" },
329 { 0x1F, "Invalid N_Port_ID" },
332 static const struct {
336 { 0x00, "no additional explanation" },
337 { 0x01, "port identifier not registered" },
338 { 0x02, "port name not registered" },
339 { 0x03, "node name not registered" },
340 { 0x04, "class of service not registered" },
341 { 0x06, "initial process associator not registered" },
342 { 0x07, "FC-4 TYPEs not registered" },
343 { 0x08, "symbolic port name not registered" },
344 { 0x09, "symbolic node name not registered" },
345 { 0x0A, "port type not registered" },
346 { 0xF0, "authorization exception" },
347 { 0xF1, "authentication exception" },
348 { 0xF2, "data base full" },
349 { 0xF3, "data base empty" },
350 { 0xF4, "processing request" },
351 { 0xF5, "unable to verify connection" },
352 { 0xF6, "devices not in a common zone" },
356 * ibmvfc_get_ls_explain - Return the FC Explain description text
357 * @status: FC Explain status
362 static const char *ibmvfc_get_ls_explain(u16 status
)
366 for (i
= 0; i
< ARRAY_SIZE(ls_explain
); i
++)
367 if (ls_explain
[i
].fc_explain
== status
)
368 return ls_explain
[i
].name
;
370 return unknown_fc_explain
;
374 * ibmvfc_get_gs_explain - Return the FC Explain description text
375 * @status: FC Explain status
380 static const char *ibmvfc_get_gs_explain(u16 status
)
384 for (i
= 0; i
< ARRAY_SIZE(gs_explain
); i
++)
385 if (gs_explain
[i
].fc_explain
== status
)
386 return gs_explain
[i
].name
;
388 return unknown_fc_explain
;
391 static const struct {
392 enum ibmvfc_fc_type fc_type
;
395 { IBMVFC_FABRIC_REJECT
, "fabric reject" },
396 { IBMVFC_PORT_REJECT
, "port reject" },
397 { IBMVFC_LS_REJECT
, "ELS reject" },
398 { IBMVFC_FABRIC_BUSY
, "fabric busy" },
399 { IBMVFC_PORT_BUSY
, "port busy" },
400 { IBMVFC_BASIC_REJECT
, "basic reject" },
403 static const char *unknown_fc_type
= "unknown fc type";
406 * ibmvfc_get_fc_type - Return the FC Type description text
407 * @status: FC Type error status
412 static const char *ibmvfc_get_fc_type(u16 status
)
416 for (i
= 0; i
< ARRAY_SIZE(fc_type
); i
++)
417 if (fc_type
[i
].fc_type
== status
)
418 return fc_type
[i
].name
;
420 return unknown_fc_type
;
424 * ibmvfc_set_tgt_action - Set the next init action for the target
425 * @tgt: ibmvfc target struct
426 * @action: action to perform
429 static void ibmvfc_set_tgt_action(struct ibmvfc_target
*tgt
,
430 enum ibmvfc_target_action action
)
432 switch (tgt
->action
) {
433 case IBMVFC_TGT_ACTION_DEL_RPORT
:
436 if (action
== IBMVFC_TGT_ACTION_DEL_RPORT
)
438 tgt
->action
= action
;
444 * ibmvfc_set_host_state - Set the state for the host
445 * @vhost: ibmvfc host struct
446 * @state: state to set host to
449 * 0 if state changed / non-zero if not changed
451 static int ibmvfc_set_host_state(struct ibmvfc_host
*vhost
,
452 enum ibmvfc_host_state state
)
456 switch (vhost
->state
) {
457 case IBMVFC_HOST_OFFLINE
:
461 vhost
->state
= state
;
469 * ibmvfc_set_host_action - Set the next init action for the host
470 * @vhost: ibmvfc host struct
471 * @action: action to perform
474 static void ibmvfc_set_host_action(struct ibmvfc_host
*vhost
,
475 enum ibmvfc_host_action action
)
478 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
479 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
)
480 vhost
->action
= action
;
482 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
483 if (vhost
->action
== IBMVFC_HOST_ACTION_LOGO
)
484 vhost
->action
= action
;
486 case IBMVFC_HOST_ACTION_INIT_WAIT
:
487 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT
)
488 vhost
->action
= action
;
490 case IBMVFC_HOST_ACTION_QUERY
:
491 switch (vhost
->action
) {
492 case IBMVFC_HOST_ACTION_INIT_WAIT
:
493 case IBMVFC_HOST_ACTION_NONE
:
494 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
495 vhost
->action
= action
;
501 case IBMVFC_HOST_ACTION_TGT_INIT
:
502 if (vhost
->action
== IBMVFC_HOST_ACTION_ALLOC_TGTS
)
503 vhost
->action
= action
;
505 case IBMVFC_HOST_ACTION_LOGO
:
506 case IBMVFC_HOST_ACTION_INIT
:
507 case IBMVFC_HOST_ACTION_TGT_DEL
:
508 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
509 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
510 case IBMVFC_HOST_ACTION_NONE
:
512 vhost
->action
= action
;
518 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
519 * @vhost: ibmvfc host struct
524 static void ibmvfc_reinit_host(struct ibmvfc_host
*vhost
)
526 if (vhost
->action
== IBMVFC_HOST_ACTION_NONE
) {
527 if (!ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
528 scsi_block_requests(vhost
->host
);
529 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
534 wake_up(&vhost
->work_wait_q
);
538 * ibmvfc_link_down - Handle a link down event from the adapter
539 * @vhost: ibmvfc host struct
540 * @state: ibmvfc host state to enter
543 static void ibmvfc_link_down(struct ibmvfc_host
*vhost
,
544 enum ibmvfc_host_state state
)
546 struct ibmvfc_target
*tgt
;
549 scsi_block_requests(vhost
->host
);
550 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
551 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
552 ibmvfc_set_host_state(vhost
, state
);
553 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
554 vhost
->events_to_log
|= IBMVFC_AE_LINKDOWN
;
555 wake_up(&vhost
->work_wait_q
);
560 * ibmvfc_init_host - Start host initialization
561 * @vhost: ibmvfc host struct
566 static void ibmvfc_init_host(struct ibmvfc_host
*vhost
)
568 struct ibmvfc_target
*tgt
;
570 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
571 if (++vhost
->init_retries
> IBMVFC_MAX_HOST_INIT_RETRIES
) {
573 "Host initialization retries exceeded. Taking adapter offline\n");
574 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
579 if (!ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
580 memset(vhost
->async_crq
.msgs
, 0, PAGE_SIZE
);
581 vhost
->async_crq
.cur
= 0;
583 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
584 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
585 scsi_block_requests(vhost
->host
);
586 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
587 vhost
->job_step
= ibmvfc_npiv_login
;
588 wake_up(&vhost
->work_wait_q
);
593 * ibmvfc_send_crq - Send a CRQ
594 * @vhost: ibmvfc host struct
595 * @word1: the first 64 bits of the data
596 * @word2: the second 64 bits of the data
599 * 0 on success / other on failure
601 static int ibmvfc_send_crq(struct ibmvfc_host
*vhost
, u64 word1
, u64 word2
)
603 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
604 return plpar_hcall_norets(H_SEND_CRQ
, vdev
->unit_address
, word1
, word2
);
608 * ibmvfc_send_crq_init - Send a CRQ init message
609 * @vhost: ibmvfc host struct
612 * 0 on success / other on failure
614 static int ibmvfc_send_crq_init(struct ibmvfc_host
*vhost
)
616 ibmvfc_dbg(vhost
, "Sending CRQ init\n");
617 return ibmvfc_send_crq(vhost
, 0xC001000000000000LL
, 0);
621 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
622 * @vhost: ibmvfc host struct
625 * 0 on success / other on failure
627 static int ibmvfc_send_crq_init_complete(struct ibmvfc_host
*vhost
)
629 ibmvfc_dbg(vhost
, "Sending CRQ init complete\n");
630 return ibmvfc_send_crq(vhost
, 0xC002000000000000LL
, 0);
634 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
635 * @vhost: ibmvfc host struct
637 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
638 * the crq with the hypervisor.
640 static void ibmvfc_release_crq_queue(struct ibmvfc_host
*vhost
)
643 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
644 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
646 ibmvfc_dbg(vhost
, "Releasing CRQ\n");
647 free_irq(vdev
->irq
, vhost
);
648 tasklet_kill(&vhost
->tasklet
);
650 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
651 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
653 vhost
->state
= IBMVFC_NO_CRQ
;
654 vhost
->logged_in
= 0;
655 dma_unmap_single(vhost
->dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
656 free_page((unsigned long)crq
->msgs
);
660 * ibmvfc_reenable_crq_queue - reenables the CRQ
661 * @vhost: ibmvfc host struct
664 * 0 on success / other on failure
666 static int ibmvfc_reenable_crq_queue(struct ibmvfc_host
*vhost
)
669 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
671 /* Re-enable the CRQ */
673 rc
= plpar_hcall_norets(H_ENABLE_CRQ
, vdev
->unit_address
);
674 } while (rc
== H_IN_PROGRESS
|| rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
677 dev_err(vhost
->dev
, "Error enabling adapter (rc=%d)\n", rc
);
683 * ibmvfc_reset_crq - resets a crq after a failure
684 * @vhost: ibmvfc host struct
687 * 0 on success / other on failure
689 static int ibmvfc_reset_crq(struct ibmvfc_host
*vhost
)
692 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
693 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
697 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
698 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
700 vhost
->state
= IBMVFC_NO_CRQ
;
701 vhost
->logged_in
= 0;
702 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
704 /* Clean out the queue */
705 memset(crq
->msgs
, 0, PAGE_SIZE
);
708 /* And re-open it again */
709 rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
710 crq
->msg_token
, PAGE_SIZE
);
713 /* Adapter is good, but other end is not ready */
714 dev_warn(vhost
->dev
, "Partner adapter not ready\n");
716 dev_warn(vhost
->dev
, "Couldn't register crq (rc=%d)\n", rc
);
722 * ibmvfc_valid_event - Determines if event is valid.
723 * @pool: event_pool that contains the event
724 * @evt: ibmvfc event to be checked for validity
727 * 1 if event is valid / 0 if event is not valid
729 static int ibmvfc_valid_event(struct ibmvfc_event_pool
*pool
,
730 struct ibmvfc_event
*evt
)
732 int index
= evt
- pool
->events
;
733 if (index
< 0 || index
>= pool
->size
) /* outside of bounds */
735 if (evt
!= pool
->events
+ index
) /* unaligned */
741 * ibmvfc_free_event - Free the specified event
742 * @evt: ibmvfc_event to be freed
745 static void ibmvfc_free_event(struct ibmvfc_event
*evt
)
747 struct ibmvfc_host
*vhost
= evt
->vhost
;
748 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
750 BUG_ON(!ibmvfc_valid_event(pool
, evt
));
751 BUG_ON(atomic_inc_return(&evt
->free
) != 1);
752 list_add_tail(&evt
->queue
, &vhost
->free
);
756 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
757 * @evt: ibmvfc event struct
759 * This function does not setup any error status, that must be done
760 * before this function gets called.
762 static void ibmvfc_scsi_eh_done(struct ibmvfc_event
*evt
)
764 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
767 scsi_dma_unmap(cmnd
);
768 cmnd
->scsi_done(cmnd
);
772 complete(evt
->eh_comp
);
774 ibmvfc_free_event(evt
);
778 * ibmvfc_fail_request - Fail request with specified error code
779 * @evt: ibmvfc event struct
780 * @error_code: error code to fail request with
785 static void ibmvfc_fail_request(struct ibmvfc_event
*evt
, int error_code
)
788 evt
->cmnd
->result
= (error_code
<< 16);
789 evt
->done
= ibmvfc_scsi_eh_done
;
791 evt
->xfer_iu
->mad_common
.status
= IBMVFC_MAD_DRIVER_FAILED
;
793 list_del(&evt
->queue
);
794 del_timer(&evt
->timer
);
800 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
801 * @vhost: ibmvfc host struct
802 * @error_code: error code to fail requests with
807 static void ibmvfc_purge_requests(struct ibmvfc_host
*vhost
, int error_code
)
809 struct ibmvfc_event
*evt
, *pos
;
811 ibmvfc_dbg(vhost
, "Purging all requests\n");
812 list_for_each_entry_safe(evt
, pos
, &vhost
->sent
, queue
)
813 ibmvfc_fail_request(evt
, error_code
);
817 * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
818 * @vhost: struct ibmvfc host to reset
820 static void ibmvfc_hard_reset_host(struct ibmvfc_host
*vhost
)
824 scsi_block_requests(vhost
->host
);
825 ibmvfc_purge_requests(vhost
, DID_ERROR
);
826 if ((rc
= ibmvfc_reset_crq(vhost
)) ||
827 (rc
= ibmvfc_send_crq_init(vhost
)) ||
828 (rc
= vio_enable_interrupts(to_vio_dev(vhost
->dev
)))) {
829 dev_err(vhost
->dev
, "Error after reset rc=%d\n", rc
);
830 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
832 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
836 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
837 * @vhost: struct ibmvfc host to reset
839 static void __ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
841 if (vhost
->logged_in
&& vhost
->action
!= IBMVFC_HOST_ACTION_LOGO_WAIT
&&
842 !ibmvfc_set_host_state(vhost
, IBMVFC_INITIALIZING
)) {
843 scsi_block_requests(vhost
->host
);
844 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_LOGO
);
845 vhost
->job_step
= ibmvfc_npiv_logout
;
846 wake_up(&vhost
->work_wait_q
);
848 ibmvfc_hard_reset_host(vhost
);
852 * ibmvfc_reset_host - Reset the connection to the server
853 * @vhost: ibmvfc host struct
855 static void ibmvfc_reset_host(struct ibmvfc_host
*vhost
)
859 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
860 __ibmvfc_reset_host(vhost
);
861 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
865 * ibmvfc_retry_host_init - Retry host initialization if allowed
866 * @vhost: ibmvfc host struct
868 * Returns: 1 if init will be retried / 0 if not
871 static int ibmvfc_retry_host_init(struct ibmvfc_host
*vhost
)
875 if (vhost
->action
== IBMVFC_HOST_ACTION_INIT_WAIT
) {
876 vhost
->delay_init
= 1;
877 if (++vhost
->init_retries
> IBMVFC_MAX_HOST_INIT_RETRIES
) {
879 "Host initialization retries exceeded. Taking adapter offline\n");
880 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
881 } else if (vhost
->init_retries
== IBMVFC_MAX_HOST_INIT_RETRIES
)
882 __ibmvfc_reset_host(vhost
);
884 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
889 wake_up(&vhost
->work_wait_q
);
894 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
895 * @starget: scsi target struct
898 * ibmvfc_target struct / NULL if not found
900 static struct ibmvfc_target
*__ibmvfc_get_target(struct scsi_target
*starget
)
902 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
903 struct ibmvfc_host
*vhost
= shost_priv(shost
);
904 struct ibmvfc_target
*tgt
;
906 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
907 if (tgt
->target_id
== starget
->id
) {
908 kref_get(&tgt
->kref
);
915 * ibmvfc_get_target - Find the specified scsi_target
916 * @starget: scsi target struct
919 * ibmvfc_target struct / NULL if not found
921 static struct ibmvfc_target
*ibmvfc_get_target(struct scsi_target
*starget
)
923 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
924 struct ibmvfc_target
*tgt
;
927 spin_lock_irqsave(shost
->host_lock
, flags
);
928 tgt
= __ibmvfc_get_target(starget
);
929 spin_unlock_irqrestore(shost
->host_lock
, flags
);
934 * ibmvfc_get_host_speed - Get host port speed
935 * @shost: scsi host struct
940 static void ibmvfc_get_host_speed(struct Scsi_Host
*shost
)
942 struct ibmvfc_host
*vhost
= shost_priv(shost
);
945 spin_lock_irqsave(shost
->host_lock
, flags
);
946 if (vhost
->state
== IBMVFC_ACTIVE
) {
947 switch (vhost
->login_buf
->resp
.link_speed
/ 100) {
949 fc_host_speed(shost
) = FC_PORTSPEED_1GBIT
;
952 fc_host_speed(shost
) = FC_PORTSPEED_2GBIT
;
955 fc_host_speed(shost
) = FC_PORTSPEED_4GBIT
;
958 fc_host_speed(shost
) = FC_PORTSPEED_8GBIT
;
961 fc_host_speed(shost
) = FC_PORTSPEED_10GBIT
;
964 fc_host_speed(shost
) = FC_PORTSPEED_16GBIT
;
967 ibmvfc_log(vhost
, 3, "Unknown port speed: %lld Gbit\n",
968 vhost
->login_buf
->resp
.link_speed
/ 100);
969 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
973 fc_host_speed(shost
) = FC_PORTSPEED_UNKNOWN
;
974 spin_unlock_irqrestore(shost
->host_lock
, flags
);
978 * ibmvfc_get_host_port_state - Get host port state
979 * @shost: scsi host struct
984 static void ibmvfc_get_host_port_state(struct Scsi_Host
*shost
)
986 struct ibmvfc_host
*vhost
= shost_priv(shost
);
989 spin_lock_irqsave(shost
->host_lock
, flags
);
990 switch (vhost
->state
) {
991 case IBMVFC_INITIALIZING
:
993 fc_host_port_state(shost
) = FC_PORTSTATE_ONLINE
;
995 case IBMVFC_LINK_DOWN
:
996 fc_host_port_state(shost
) = FC_PORTSTATE_LINKDOWN
;
998 case IBMVFC_LINK_DEAD
:
999 case IBMVFC_HOST_OFFLINE
:
1000 fc_host_port_state(shost
) = FC_PORTSTATE_OFFLINE
;
1003 fc_host_port_state(shost
) = FC_PORTSTATE_BLOCKED
;
1006 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
1009 ibmvfc_log(vhost
, 3, "Unknown port state: %d\n", vhost
->state
);
1010 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
1013 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1017 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
1018 * @rport: rport struct
1019 * @timeout: timeout value
1024 static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport
*rport
, u32 timeout
)
1027 rport
->dev_loss_tmo
= timeout
;
1029 rport
->dev_loss_tmo
= 1;
1033 * ibmvfc_release_tgt - Free memory allocated for a target
1034 * @kref: kref struct
1037 static void ibmvfc_release_tgt(struct kref
*kref
)
1039 struct ibmvfc_target
*tgt
= container_of(kref
, struct ibmvfc_target
, kref
);
1044 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1045 * @starget: scsi target struct
1050 static void ibmvfc_get_starget_node_name(struct scsi_target
*starget
)
1052 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1053 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.node_name
: 0;
1055 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1059 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1060 * @starget: scsi target struct
1065 static void ibmvfc_get_starget_port_name(struct scsi_target
*starget
)
1067 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1068 fc_starget_port_name(starget
) = tgt
? tgt
->ids
.port_name
: 0;
1070 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1074 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1075 * @starget: scsi target struct
1080 static void ibmvfc_get_starget_port_id(struct scsi_target
*starget
)
1082 struct ibmvfc_target
*tgt
= ibmvfc_get_target(starget
);
1083 fc_starget_port_id(starget
) = tgt
? tgt
->scsi_id
: -1;
1085 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
1089 * ibmvfc_wait_while_resetting - Wait while the host resets
1090 * @vhost: ibmvfc host struct
1093 * 0 on success / other on failure
1095 static int ibmvfc_wait_while_resetting(struct ibmvfc_host
*vhost
)
1097 long timeout
= wait_event_timeout(vhost
->init_wait_q
,
1098 ((vhost
->state
== IBMVFC_ACTIVE
||
1099 vhost
->state
== IBMVFC_HOST_OFFLINE
||
1100 vhost
->state
== IBMVFC_LINK_DEAD
) &&
1101 vhost
->action
== IBMVFC_HOST_ACTION_NONE
),
1102 (init_timeout
* HZ
));
1104 return timeout
? 0 : -EIO
;
1108 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1109 * @shost: scsi host struct
1112 * 0 on success / other on failure
1114 static int ibmvfc_issue_fc_host_lip(struct Scsi_Host
*shost
)
1116 struct ibmvfc_host
*vhost
= shost_priv(shost
);
1118 dev_err(vhost
->dev
, "Initiating host LIP. Resetting connection\n");
1119 ibmvfc_reset_host(vhost
);
1120 return ibmvfc_wait_while_resetting(vhost
);
1124 * ibmvfc_gather_partition_info - Gather info about the LPAR
1129 static void ibmvfc_gather_partition_info(struct ibmvfc_host
*vhost
)
1131 struct device_node
*rootdn
;
1133 const unsigned int *num
;
1135 rootdn
= of_find_node_by_path("/");
1139 name
= of_get_property(rootdn
, "ibm,partition-name", NULL
);
1141 strncpy(vhost
->partition_name
, name
, sizeof(vhost
->partition_name
));
1142 num
= of_get_property(rootdn
, "ibm,partition-no", NULL
);
1144 vhost
->partition_number
= *num
;
1145 of_node_put(rootdn
);
1149 * ibmvfc_set_login_info - Setup info for NPIV login
1150 * @vhost: ibmvfc host struct
1155 static void ibmvfc_set_login_info(struct ibmvfc_host
*vhost
)
1157 struct ibmvfc_npiv_login
*login_info
= &vhost
->login_info
;
1158 struct device_node
*of_node
= vhost
->dev
->archdata
.of_node
;
1159 const char *location
;
1161 memset(login_info
, 0, sizeof(*login_info
));
1163 login_info
->ostype
= IBMVFC_OS_LINUX
;
1164 login_info
->max_dma_len
= IBMVFC_MAX_SECTORS
<< 9;
1165 login_info
->max_payload
= sizeof(struct ibmvfc_fcp_cmd_iu
);
1166 login_info
->max_response
= sizeof(struct ibmvfc_fcp_rsp
);
1167 login_info
->partition_num
= vhost
->partition_number
;
1168 login_info
->vfc_frame_version
= 1;
1169 login_info
->fcp_version
= 3;
1170 login_info
->flags
= IBMVFC_FLUSH_ON_HALT
;
1171 if (vhost
->client_migrated
)
1172 login_info
->flags
|= IBMVFC_CLIENT_MIGRATED
;
1174 login_info
->max_cmds
= max_requests
+ IBMVFC_NUM_INTERNAL_REQ
;
1175 login_info
->capabilities
= IBMVFC_CAN_MIGRATE
;
1176 login_info
->async
.va
= vhost
->async_crq
.msg_token
;
1177 login_info
->async
.len
= vhost
->async_crq
.size
* sizeof(*vhost
->async_crq
.msgs
);
1178 strncpy(login_info
->partition_name
, vhost
->partition_name
, IBMVFC_MAX_NAME
);
1179 strncpy(login_info
->device_name
,
1180 dev_name(&vhost
->host
->shost_gendev
), IBMVFC_MAX_NAME
);
1182 location
= of_get_property(of_node
, "ibm,loc-code", NULL
);
1183 location
= location
? location
: dev_name(vhost
->dev
);
1184 strncpy(login_info
->drc_name
, location
, IBMVFC_MAX_NAME
);
1188 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1189 * @vhost: ibmvfc host who owns the event pool
1191 * Returns zero on success.
1193 static int ibmvfc_init_event_pool(struct ibmvfc_host
*vhost
)
1196 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1199 pool
->size
= max_requests
+ IBMVFC_NUM_INTERNAL_REQ
;
1200 pool
->events
= kcalloc(pool
->size
, sizeof(*pool
->events
), GFP_KERNEL
);
1204 pool
->iu_storage
= dma_alloc_coherent(vhost
->dev
,
1205 pool
->size
* sizeof(*pool
->iu_storage
),
1206 &pool
->iu_token
, 0);
1208 if (!pool
->iu_storage
) {
1209 kfree(pool
->events
);
1213 for (i
= 0; i
< pool
->size
; ++i
) {
1214 struct ibmvfc_event
*evt
= &pool
->events
[i
];
1215 atomic_set(&evt
->free
, 1);
1216 evt
->crq
.valid
= 0x80;
1217 evt
->crq
.ioba
= pool
->iu_token
+ (sizeof(*evt
->xfer_iu
) * i
);
1218 evt
->xfer_iu
= pool
->iu_storage
+ i
;
1220 evt
->ext_list
= NULL
;
1221 list_add_tail(&evt
->queue
, &vhost
->free
);
1229 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1230 * @vhost: ibmvfc host who owns the event pool
1233 static void ibmvfc_free_event_pool(struct ibmvfc_host
*vhost
)
1236 struct ibmvfc_event_pool
*pool
= &vhost
->pool
;
1239 for (i
= 0; i
< pool
->size
; ++i
) {
1240 list_del(&pool
->events
[i
].queue
);
1241 BUG_ON(atomic_read(&pool
->events
[i
].free
) != 1);
1242 if (pool
->events
[i
].ext_list
)
1243 dma_pool_free(vhost
->sg_pool
,
1244 pool
->events
[i
].ext_list
,
1245 pool
->events
[i
].ext_list_token
);
1248 kfree(pool
->events
);
1249 dma_free_coherent(vhost
->dev
,
1250 pool
->size
* sizeof(*pool
->iu_storage
),
1251 pool
->iu_storage
, pool
->iu_token
);
1256 * ibmvfc_get_event - Gets the next free event in pool
1257 * @vhost: ibmvfc host struct
1259 * Returns a free event from the pool.
1261 static struct ibmvfc_event
*ibmvfc_get_event(struct ibmvfc_host
*vhost
)
1263 struct ibmvfc_event
*evt
;
1265 BUG_ON(list_empty(&vhost
->free
));
1266 evt
= list_entry(vhost
->free
.next
, struct ibmvfc_event
, queue
);
1267 atomic_set(&evt
->free
, 0);
1268 list_del(&evt
->queue
);
1273 * ibmvfc_init_event - Initialize fields in an event struct that are always
1276 * @done: Routine to call when the event is responded to
1277 * @format: SRP or MAD format
1279 static void ibmvfc_init_event(struct ibmvfc_event
*evt
,
1280 void (*done
) (struct ibmvfc_event
*), u8 format
)
1283 evt
->sync_iu
= NULL
;
1284 evt
->crq
.format
= format
;
1286 evt
->eh_comp
= NULL
;
1290 * ibmvfc_map_sg_list - Initialize scatterlist
1291 * @scmd: scsi command struct
1292 * @nseg: number of scatterlist segments
1293 * @md: memory descriptor list to initialize
1295 static void ibmvfc_map_sg_list(struct scsi_cmnd
*scmd
, int nseg
,
1296 struct srp_direct_buf
*md
)
1299 struct scatterlist
*sg
;
1301 scsi_for_each_sg(scmd
, sg
, nseg
, i
) {
1302 md
[i
].va
= sg_dma_address(sg
);
1303 md
[i
].len
= sg_dma_len(sg
);
1309 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1310 * @scmd: Scsi_Cmnd with the scatterlist
1311 * @evt: ibmvfc event struct
1312 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1313 * @dev: device for which to map dma memory
1316 * 0 on success / non-zero on failure
1318 static int ibmvfc_map_sg_data(struct scsi_cmnd
*scmd
,
1319 struct ibmvfc_event
*evt
,
1320 struct ibmvfc_cmd
*vfc_cmd
, struct device
*dev
)
1324 struct srp_direct_buf
*data
= &vfc_cmd
->ioba
;
1325 struct ibmvfc_host
*vhost
= dev_get_drvdata(dev
);
1327 sg_mapped
= scsi_dma_map(scmd
);
1329 vfc_cmd
->flags
|= IBMVFC_NO_MEM_DESC
;
1331 } else if (unlikely(sg_mapped
< 0)) {
1332 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1333 scmd_printk(KERN_ERR
, scmd
, "Failed to map DMA buffer for command\n");
1337 if (scmd
->sc_data_direction
== DMA_TO_DEVICE
) {
1338 vfc_cmd
->flags
|= IBMVFC_WRITE
;
1339 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_WRDATA
;
1341 vfc_cmd
->flags
|= IBMVFC_READ
;
1342 vfc_cmd
->iu
.add_cdb_len
|= IBMVFC_RDDATA
;
1345 if (sg_mapped
== 1) {
1346 ibmvfc_map_sg_list(scmd
, sg_mapped
, data
);
1350 vfc_cmd
->flags
|= IBMVFC_SCATTERLIST
;
1352 if (!evt
->ext_list
) {
1353 evt
->ext_list
= dma_pool_alloc(vhost
->sg_pool
, GFP_ATOMIC
,
1354 &evt
->ext_list_token
);
1356 if (!evt
->ext_list
) {
1357 scsi_dma_unmap(scmd
);
1358 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1359 scmd_printk(KERN_ERR
, scmd
, "Can't allocate memory for scatterlist\n");
1364 ibmvfc_map_sg_list(scmd
, sg_mapped
, evt
->ext_list
);
1366 data
->va
= evt
->ext_list_token
;
1367 data
->len
= sg_mapped
* sizeof(struct srp_direct_buf
);
1373 * ibmvfc_timeout - Internal command timeout handler
1374 * @evt: struct ibmvfc_event that timed out
1376 * Called when an internally generated command times out
1378 static void ibmvfc_timeout(struct ibmvfc_event
*evt
)
1380 struct ibmvfc_host
*vhost
= evt
->vhost
;
1381 dev_err(vhost
->dev
, "Command timed out (%p). Resetting connection\n", evt
);
1382 ibmvfc_reset_host(vhost
);
1386 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1387 * @evt: event to be sent
1388 * @vhost: ibmvfc host struct
1389 * @timeout: timeout in seconds - 0 means do not time command
1391 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1393 static int ibmvfc_send_event(struct ibmvfc_event
*evt
,
1394 struct ibmvfc_host
*vhost
, unsigned long timeout
)
1396 u64
*crq_as_u64
= (u64
*) &evt
->crq
;
1399 /* Copy the IU into the transfer area */
1400 *evt
->xfer_iu
= evt
->iu
;
1401 if (evt
->crq
.format
== IBMVFC_CMD_FORMAT
)
1402 evt
->xfer_iu
->cmd
.tag
= (u64
)evt
;
1403 else if (evt
->crq
.format
== IBMVFC_MAD_FORMAT
)
1404 evt
->xfer_iu
->mad_common
.tag
= (u64
)evt
;
1408 list_add_tail(&evt
->queue
, &vhost
->sent
);
1409 init_timer(&evt
->timer
);
1412 evt
->timer
.data
= (unsigned long) evt
;
1413 evt
->timer
.expires
= jiffies
+ (timeout
* HZ
);
1414 evt
->timer
.function
= (void (*)(unsigned long))ibmvfc_timeout
;
1415 add_timer(&evt
->timer
);
1420 if ((rc
= ibmvfc_send_crq(vhost
, crq_as_u64
[0], crq_as_u64
[1]))) {
1421 list_del(&evt
->queue
);
1422 del_timer(&evt
->timer
);
1424 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1425 * Firmware will send a CRQ with a transport event (0xFF) to
1426 * tell this client what has happened to the transport. This
1427 * will be handled in ibmvfc_handle_crq()
1429 if (rc
== H_CLOSED
) {
1430 if (printk_ratelimit())
1431 dev_warn(vhost
->dev
, "Send warning. Receive queue closed, will retry.\n");
1433 scsi_dma_unmap(evt
->cmnd
);
1434 ibmvfc_free_event(evt
);
1435 return SCSI_MLQUEUE_HOST_BUSY
;
1438 dev_err(vhost
->dev
, "Send error (rc=%d)\n", rc
);
1440 evt
->cmnd
->result
= DID_ERROR
<< 16;
1441 evt
->done
= ibmvfc_scsi_eh_done
;
1443 evt
->xfer_iu
->mad_common
.status
= IBMVFC_MAD_CRQ_ERROR
;
1447 ibmvfc_trc_start(evt
);
1453 * ibmvfc_log_error - Log an error for the failed command if appropriate
1454 * @evt: ibmvfc event to log
1457 static void ibmvfc_log_error(struct ibmvfc_event
*evt
)
1459 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1460 struct ibmvfc_host
*vhost
= evt
->vhost
;
1461 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1462 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1463 const char *err
= unknown_error
;
1464 int index
= ibmvfc_get_err_index(vfc_cmd
->status
, vfc_cmd
->error
);
1469 logerr
= cmd_status
[index
].log
;
1470 err
= cmd_status
[index
].name
;
1473 if (!logerr
&& (vhost
->log_level
<= (IBMVFC_DEFAULT_LOG_LEVEL
+ 1)))
1476 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1477 rsp_code
= rsp
->data
.info
.rsp_code
;
1479 scmd_printk(KERN_ERR
, cmnd
, "Command (%02X) failed: %s (%x:%x) "
1480 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1481 cmnd
->cmnd
[0], err
, vfc_cmd
->status
, vfc_cmd
->error
,
1482 rsp
->flags
, rsp_code
, scsi_get_resid(cmnd
), rsp
->scsi_status
);
1486 * ibmvfc_relogin - Log back into the specified device
1487 * @sdev: scsi device struct
1490 static void ibmvfc_relogin(struct scsi_device
*sdev
)
1492 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1493 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1494 struct ibmvfc_target
*tgt
;
1496 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
1497 if (rport
== tgt
->rport
) {
1498 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
1503 ibmvfc_reinit_host(vhost
);
1507 * ibmvfc_scsi_done - Handle responses from commands
1508 * @evt: ibmvfc event to be handled
1510 * Used as a callback when sending scsi cmds.
1512 static void ibmvfc_scsi_done(struct ibmvfc_event
*evt
)
1514 struct ibmvfc_cmd
*vfc_cmd
= &evt
->xfer_iu
->cmd
;
1515 struct ibmvfc_fcp_rsp
*rsp
= &vfc_cmd
->rsp
;
1516 struct scsi_cmnd
*cmnd
= evt
->cmnd
;
1518 u32 sense_len
= rsp
->fcp_sense_len
;
1521 if (vfc_cmd
->response_flags
& IBMVFC_ADAPTER_RESID_VALID
)
1522 scsi_set_resid(cmnd
, vfc_cmd
->adapter_resid
);
1523 else if (rsp
->flags
& FCP_RESID_UNDER
)
1524 scsi_set_resid(cmnd
, rsp
->fcp_resid
);
1526 scsi_set_resid(cmnd
, 0);
1528 if (vfc_cmd
->status
) {
1529 cmnd
->result
= ibmvfc_get_err_result(vfc_cmd
);
1531 if (rsp
->flags
& FCP_RSP_LEN_VALID
)
1532 rsp_len
= rsp
->fcp_rsp_len
;
1533 if ((sense_len
+ rsp_len
) > SCSI_SENSE_BUFFERSIZE
)
1534 sense_len
= SCSI_SENSE_BUFFERSIZE
- rsp_len
;
1535 if ((rsp
->flags
& FCP_SNS_LEN_VALID
) && rsp
->fcp_sense_len
&& rsp_len
<= 8)
1536 memcpy(cmnd
->sense_buffer
, rsp
->data
.sense
+ rsp_len
, sense_len
);
1537 if ((vfc_cmd
->status
& IBMVFC_VIOS_FAILURE
) && (vfc_cmd
->error
== IBMVFC_PLOGI_REQUIRED
))
1538 ibmvfc_relogin(cmnd
->device
);
1540 if (!cmnd
->result
&& (!scsi_get_resid(cmnd
) || (rsp
->flags
& FCP_RESID_OVER
)))
1541 cmnd
->result
= (DID_ERROR
<< 16);
1543 ibmvfc_log_error(evt
);
1546 if (!cmnd
->result
&&
1547 (scsi_bufflen(cmnd
) - scsi_get_resid(cmnd
) < cmnd
->underflow
))
1548 cmnd
->result
= (DID_ERROR
<< 16);
1550 scsi_dma_unmap(cmnd
);
1551 cmnd
->scsi_done(cmnd
);
1555 complete(evt
->eh_comp
);
1557 ibmvfc_free_event(evt
);
1561 * ibmvfc_host_chkready - Check if the host can accept commands
1562 * @vhost: struct ibmvfc host
1565 * 1 if host can accept command / 0 if not
1567 static inline int ibmvfc_host_chkready(struct ibmvfc_host
*vhost
)
1571 switch (vhost
->state
) {
1572 case IBMVFC_LINK_DEAD
:
1573 case IBMVFC_HOST_OFFLINE
:
1574 result
= DID_NO_CONNECT
<< 16;
1577 case IBMVFC_INITIALIZING
:
1579 case IBMVFC_LINK_DOWN
:
1580 result
= DID_REQUEUE
<< 16;
1591 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1592 * @cmnd: struct scsi_cmnd to be executed
1593 * @done: Callback function to be called when cmnd is completed
1596 * 0 on success / other on failure
1598 static int ibmvfc_queuecommand(struct scsi_cmnd
*cmnd
,
1599 void (*done
) (struct scsi_cmnd
*))
1601 struct ibmvfc_host
*vhost
= shost_priv(cmnd
->device
->host
);
1602 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
1603 struct ibmvfc_cmd
*vfc_cmd
;
1604 struct ibmvfc_event
*evt
;
1608 if (unlikely((rc
= fc_remote_port_chkready(rport
))) ||
1609 unlikely((rc
= ibmvfc_host_chkready(vhost
)))) {
1615 cmnd
->result
= (DID_OK
<< 16);
1616 evt
= ibmvfc_get_event(vhost
);
1617 ibmvfc_init_event(evt
, ibmvfc_scsi_done
, IBMVFC_CMD_FORMAT
);
1619 cmnd
->scsi_done
= done
;
1620 vfc_cmd
= &evt
->iu
.cmd
;
1621 memset(vfc_cmd
, 0, sizeof(*vfc_cmd
));
1622 vfc_cmd
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
1623 vfc_cmd
->resp
.len
= sizeof(vfc_cmd
->rsp
);
1624 vfc_cmd
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
1625 vfc_cmd
->payload_len
= sizeof(vfc_cmd
->iu
);
1626 vfc_cmd
->resp_len
= sizeof(vfc_cmd
->rsp
);
1627 vfc_cmd
->cancel_key
= (unsigned long)cmnd
->device
->hostdata
;
1628 vfc_cmd
->tgt_scsi_id
= rport
->port_id
;
1629 vfc_cmd
->iu
.xfer_len
= scsi_bufflen(cmnd
);
1630 int_to_scsilun(cmnd
->device
->lun
, &vfc_cmd
->iu
.lun
);
1631 memcpy(vfc_cmd
->iu
.cdb
, cmnd
->cmnd
, cmnd
->cmd_len
);
1633 if (scsi_populate_tag_msg(cmnd
, tag
)) {
1634 vfc_cmd
->task_tag
= tag
[1];
1636 case MSG_SIMPLE_TAG
:
1637 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_SIMPLE_TASK
;
1640 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_HEAD_OF_QUEUE
;
1642 case MSG_ORDERED_TAG
:
1643 vfc_cmd
->iu
.pri_task_attr
= IBMVFC_ORDERED_TASK
;
1648 if (likely(!(rc
= ibmvfc_map_sg_data(cmnd
, evt
, vfc_cmd
, vhost
->dev
))))
1649 return ibmvfc_send_event(evt
, vhost
, 0);
1651 ibmvfc_free_event(evt
);
1653 return SCSI_MLQUEUE_HOST_BUSY
;
1655 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
1656 scmd_printk(KERN_ERR
, cmnd
,
1657 "Failed to map DMA buffer for command. rc=%d\n", rc
);
1659 cmnd
->result
= DID_ERROR
<< 16;
1665 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1666 * @evt: ibmvfc event struct
1669 static void ibmvfc_sync_completion(struct ibmvfc_event
*evt
)
1671 /* copy the response back */
1673 *evt
->sync_iu
= *evt
->xfer_iu
;
1675 complete(&evt
->comp
);
1679 * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
1680 * @evt: struct ibmvfc_event
1683 static void ibmvfc_bsg_timeout_done(struct ibmvfc_event
*evt
)
1685 struct ibmvfc_host
*vhost
= evt
->vhost
;
1687 ibmvfc_free_event(evt
);
1688 vhost
->aborting_passthru
= 0;
1689 dev_info(vhost
->dev
, "Passthru command cancelled\n");
1693 * ibmvfc_bsg_timeout - Handle a BSG timeout
1694 * @job: struct fc_bsg_job that timed out
1697 * 0 on success / other on failure
1699 static int ibmvfc_bsg_timeout(struct fc_bsg_job
*job
)
1701 struct ibmvfc_host
*vhost
= shost_priv(job
->shost
);
1702 unsigned long port_id
= (unsigned long)job
->dd_data
;
1703 struct ibmvfc_event
*evt
;
1704 struct ibmvfc_tmf
*tmf
;
1705 unsigned long flags
;
1709 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1710 if (vhost
->aborting_passthru
|| vhost
->state
!= IBMVFC_ACTIVE
) {
1711 __ibmvfc_reset_host(vhost
);
1712 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1716 vhost
->aborting_passthru
= 1;
1717 evt
= ibmvfc_get_event(vhost
);
1718 ibmvfc_init_event(evt
, ibmvfc_bsg_timeout_done
, IBMVFC_MAD_FORMAT
);
1721 memset(tmf
, 0, sizeof(*tmf
));
1722 tmf
->common
.version
= 1;
1723 tmf
->common
.opcode
= IBMVFC_TMF_MAD
;
1724 tmf
->common
.length
= sizeof(*tmf
);
1725 tmf
->scsi_id
= port_id
;
1726 tmf
->cancel_key
= IBMVFC_PASSTHRU_CANCEL_KEY
;
1727 tmf
->my_cancel_key
= IBMVFC_INTERNAL_CANCEL_KEY
;
1728 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1731 vhost
->aborting_passthru
= 0;
1732 dev_err(vhost
->dev
, "Failed to send cancel event. rc=%d\n", rc
);
1735 dev_info(vhost
->dev
, "Cancelling passthru command to port id 0x%lx\n",
1738 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1745 * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
1746 * @vhost: struct ibmvfc_host to send command
1747 * @port_id: port ID to send command
1750 * 0 on success / other on failure
1752 static int ibmvfc_bsg_plogi(struct ibmvfc_host
*vhost
, unsigned int port_id
)
1754 struct ibmvfc_port_login
*plogi
;
1755 struct ibmvfc_target
*tgt
;
1756 struct ibmvfc_event
*evt
;
1757 union ibmvfc_iu rsp_iu
;
1758 unsigned long flags
;
1759 int rc
= 0, issue_login
= 1;
1762 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1763 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
1764 if (tgt
->scsi_id
== port_id
) {
1772 if (unlikely((rc
= ibmvfc_host_chkready(vhost
))))
1775 evt
= ibmvfc_get_event(vhost
);
1776 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
1777 plogi
= &evt
->iu
.plogi
;
1778 memset(plogi
, 0, sizeof(*plogi
));
1779 plogi
->common
.version
= 1;
1780 plogi
->common
.opcode
= IBMVFC_PORT_LOGIN
;
1781 plogi
->common
.length
= sizeof(*plogi
);
1782 plogi
->scsi_id
= port_id
;
1783 evt
->sync_iu
= &rsp_iu
;
1784 init_completion(&evt
->comp
);
1786 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1787 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1792 wait_for_completion(&evt
->comp
);
1794 if (rsp_iu
.plogi
.common
.status
)
1797 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1798 ibmvfc_free_event(evt
);
1800 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1806 * ibmvfc_bsg_request - Handle a BSG request
1807 * @job: struct fc_bsg_job to be executed
1810 * 0 on success / other on failure
1812 static int ibmvfc_bsg_request(struct fc_bsg_job
*job
)
1814 struct ibmvfc_host
*vhost
= shost_priv(job
->shost
);
1815 struct fc_rport
*rport
= job
->rport
;
1816 struct ibmvfc_passthru_mad
*mad
;
1817 struct ibmvfc_event
*evt
;
1818 union ibmvfc_iu rsp_iu
;
1819 unsigned long flags
, port_id
= -1;
1820 unsigned int code
= job
->request
->msgcode
;
1821 int rc
= 0, req_seg
, rsp_seg
, issue_login
= 0;
1822 u32 fc_flags
, rsp_len
;
1825 job
->reply
->reply_payload_rcv_len
= 0;
1827 port_id
= rport
->port_id
;
1830 case FC_BSG_HST_ELS_NOLOGIN
:
1831 port_id
= (job
->request
->rqst_data
.h_els
.port_id
[0] << 16) |
1832 (job
->request
->rqst_data
.h_els
.port_id
[1] << 8) |
1833 job
->request
->rqst_data
.h_els
.port_id
[2];
1834 case FC_BSG_RPT_ELS
:
1835 fc_flags
= IBMVFC_FC_ELS
;
1839 port_id
= (job
->request
->rqst_data
.h_ct
.port_id
[0] << 16) |
1840 (job
->request
->rqst_data
.h_ct
.port_id
[1] << 8) |
1841 job
->request
->rqst_data
.h_ct
.port_id
[2];
1843 fc_flags
= IBMVFC_FC_CT_IU
;
1851 if (!mutex_trylock(&vhost
->passthru_mutex
))
1854 job
->dd_data
= (void *)port_id
;
1855 req_seg
= dma_map_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1856 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1859 mutex_unlock(&vhost
->passthru_mutex
);
1863 rsp_seg
= dma_map_sg(vhost
->dev
, job
->reply_payload
.sg_list
,
1864 job
->reply_payload
.sg_cnt
, DMA_FROM_DEVICE
);
1867 dma_unmap_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1868 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1869 mutex_unlock(&vhost
->passthru_mutex
);
1873 if (req_seg
> 1 || rsp_seg
> 1) {
1879 rc
= ibmvfc_bsg_plogi(vhost
, port_id
);
1881 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1883 if (unlikely(rc
|| (rport
&& (rc
= fc_remote_port_chkready(rport
)))) ||
1884 unlikely((rc
= ibmvfc_host_chkready(vhost
)))) {
1885 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1889 evt
= ibmvfc_get_event(vhost
);
1890 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
1891 mad
= &evt
->iu
.passthru
;
1893 memset(mad
, 0, sizeof(*mad
));
1894 mad
->common
.version
= 1;
1895 mad
->common
.opcode
= IBMVFC_PASSTHRU
;
1896 mad
->common
.length
= sizeof(*mad
) - sizeof(mad
->fc_iu
) - sizeof(mad
->iu
);
1898 mad
->cmd_ioba
.va
= (u64
)evt
->crq
.ioba
+
1899 offsetof(struct ibmvfc_passthru_mad
, iu
);
1900 mad
->cmd_ioba
.len
= sizeof(mad
->iu
);
1902 mad
->iu
.cmd_len
= job
->request_payload
.payload_len
;
1903 mad
->iu
.rsp_len
= job
->reply_payload
.payload_len
;
1904 mad
->iu
.flags
= fc_flags
;
1905 mad
->iu
.cancel_key
= IBMVFC_PASSTHRU_CANCEL_KEY
;
1907 mad
->iu
.cmd
.va
= sg_dma_address(job
->request_payload
.sg_list
);
1908 mad
->iu
.cmd
.len
= sg_dma_len(job
->request_payload
.sg_list
);
1909 mad
->iu
.rsp
.va
= sg_dma_address(job
->reply_payload
.sg_list
);
1910 mad
->iu
.rsp
.len
= sg_dma_len(job
->reply_payload
.sg_list
);
1911 mad
->iu
.scsi_id
= port_id
;
1912 mad
->iu
.tag
= (u64
)evt
;
1913 rsp_len
= mad
->iu
.rsp
.len
;
1915 evt
->sync_iu
= &rsp_iu
;
1916 init_completion(&evt
->comp
);
1917 rc
= ibmvfc_send_event(evt
, vhost
, 0);
1918 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1925 wait_for_completion(&evt
->comp
);
1927 if (rsp_iu
.passthru
.common
.status
)
1930 job
->reply
->reply_payload_rcv_len
= rsp_len
;
1932 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1933 ibmvfc_free_event(evt
);
1934 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1935 job
->reply
->result
= rc
;
1939 dma_unmap_sg(vhost
->dev
, job
->request_payload
.sg_list
,
1940 job
->request_payload
.sg_cnt
, DMA_TO_DEVICE
);
1941 dma_unmap_sg(vhost
->dev
, job
->reply_payload
.sg_list
,
1942 job
->reply_payload
.sg_cnt
, DMA_FROM_DEVICE
);
1943 mutex_unlock(&vhost
->passthru_mutex
);
1949 * ibmvfc_reset_device - Reset the device with the specified reset type
1950 * @sdev: scsi device to reset
1952 * @desc: reset type description for log messages
1955 * 0 on success / other on failure
1957 static int ibmvfc_reset_device(struct scsi_device
*sdev
, int type
, char *desc
)
1959 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
1960 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
1961 struct ibmvfc_cmd
*tmf
;
1962 struct ibmvfc_event
*evt
= NULL
;
1963 union ibmvfc_iu rsp_iu
;
1964 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
1965 int rsp_rc
= -EBUSY
;
1966 unsigned long flags
;
1969 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
1970 if (vhost
->state
== IBMVFC_ACTIVE
) {
1971 evt
= ibmvfc_get_event(vhost
);
1972 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
1975 memset(tmf
, 0, sizeof(*tmf
));
1976 tmf
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
1977 tmf
->resp
.len
= sizeof(tmf
->rsp
);
1978 tmf
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
1979 tmf
->payload_len
= sizeof(tmf
->iu
);
1980 tmf
->resp_len
= sizeof(tmf
->rsp
);
1981 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
1982 tmf
->tgt_scsi_id
= rport
->port_id
;
1983 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
1984 tmf
->flags
= (IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
);
1985 tmf
->iu
.tmf_flags
= type
;
1986 evt
->sync_iu
= &rsp_iu
;
1988 init_completion(&evt
->comp
);
1989 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
1991 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
1994 sdev_printk(KERN_ERR
, sdev
, "Failed to send %s reset event. rc=%d\n",
1999 sdev_printk(KERN_INFO
, sdev
, "Resetting %s\n", desc
);
2000 wait_for_completion(&evt
->comp
);
2002 if (rsp_iu
.cmd
.status
)
2003 rsp_code
= ibmvfc_get_err_result(&rsp_iu
.cmd
);
2006 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
2007 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
2009 sdev_printk(KERN_ERR
, sdev
, "%s reset failed: %s (%x:%x) "
2010 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2011 desc
, ibmvfc_get_cmd_error(rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
),
2012 rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
, fc_rsp
->flags
, rsp_code
,
2013 fc_rsp
->scsi_status
);
2016 sdev_printk(KERN_INFO
, sdev
, "%s reset successful\n", desc
);
2018 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2019 ibmvfc_free_event(evt
);
2020 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2025 * ibmvfc_abort_task_set - Abort outstanding commands to the device
2026 * @sdev: scsi device to abort commands
2028 * This sends an Abort Task Set to the VIOS for the specified device. This does
2029 * NOT send any cancel to the VIOS. That must be done separately.
2032 * 0 on success / other on failure
2034 static int ibmvfc_abort_task_set(struct scsi_device
*sdev
)
2036 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2037 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2038 struct ibmvfc_cmd
*tmf
;
2039 struct ibmvfc_event
*evt
, *found_evt
;
2040 union ibmvfc_iu rsp_iu
;
2041 struct ibmvfc_fcp_rsp
*fc_rsp
= &rsp_iu
.cmd
.rsp
;
2042 int rsp_rc
= -EBUSY
;
2043 unsigned long flags
;
2046 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2048 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2049 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
2056 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
2057 sdev_printk(KERN_INFO
, sdev
, "No events found to abort\n");
2058 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2062 if (vhost
->state
== IBMVFC_ACTIVE
) {
2063 evt
= ibmvfc_get_event(vhost
);
2064 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_CMD_FORMAT
);
2067 memset(tmf
, 0, sizeof(*tmf
));
2068 tmf
->resp
.va
= (u64
)evt
->crq
.ioba
+ offsetof(struct ibmvfc_cmd
, rsp
);
2069 tmf
->resp
.len
= sizeof(tmf
->rsp
);
2070 tmf
->frame_type
= IBMVFC_SCSI_FCP_TYPE
;
2071 tmf
->payload_len
= sizeof(tmf
->iu
);
2072 tmf
->resp_len
= sizeof(tmf
->rsp
);
2073 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
2074 tmf
->tgt_scsi_id
= rport
->port_id
;
2075 int_to_scsilun(sdev
->lun
, &tmf
->iu
.lun
);
2076 tmf
->flags
= (IBMVFC_NO_MEM_DESC
| IBMVFC_TMF
);
2077 tmf
->iu
.tmf_flags
= IBMVFC_ABORT_TASK_SET
;
2078 evt
->sync_iu
= &rsp_iu
;
2080 init_completion(&evt
->comp
);
2081 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
2084 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2087 sdev_printk(KERN_ERR
, sdev
, "Failed to send abort. rc=%d\n", rsp_rc
);
2091 sdev_printk(KERN_INFO
, sdev
, "Aborting outstanding commands\n");
2092 wait_for_completion(&evt
->comp
);
2094 if (rsp_iu
.cmd
.status
)
2095 rsp_code
= ibmvfc_get_err_result(&rsp_iu
.cmd
);
2098 if (fc_rsp
->flags
& FCP_RSP_LEN_VALID
)
2099 rsp_code
= fc_rsp
->data
.info
.rsp_code
;
2101 sdev_printk(KERN_ERR
, sdev
, "Abort failed: %s (%x:%x) "
2102 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
2103 ibmvfc_get_cmd_error(rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
),
2104 rsp_iu
.cmd
.status
, rsp_iu
.cmd
.error
, fc_rsp
->flags
, rsp_code
,
2105 fc_rsp
->scsi_status
);
2108 sdev_printk(KERN_INFO
, sdev
, "Abort successful\n");
2110 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2111 ibmvfc_free_event(evt
);
2112 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2117 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
2118 * @sdev: scsi device to cancel commands
2119 * @type: type of error recovery being performed
2121 * This sends a cancel to the VIOS for the specified device. This does
2122 * NOT send any abort to the actual device. That must be done separately.
2125 * 0 on success / other on failure
2127 static int ibmvfc_cancel_all(struct scsi_device
*sdev
, int type
)
2129 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2130 struct scsi_target
*starget
= scsi_target(sdev
);
2131 struct fc_rport
*rport
= starget_to_rport(starget
);
2132 struct ibmvfc_tmf
*tmf
;
2133 struct ibmvfc_event
*evt
, *found_evt
;
2134 union ibmvfc_iu rsp
;
2135 int rsp_rc
= -EBUSY
;
2136 unsigned long flags
;
2140 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2142 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2143 if (evt
->cmnd
&& evt
->cmnd
->device
== sdev
) {
2150 if (vhost
->log_level
> IBMVFC_DEFAULT_LOG_LEVEL
)
2151 sdev_printk(KERN_INFO
, sdev
, "No events found to cancel\n");
2152 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2156 if (vhost
->state
== IBMVFC_ACTIVE
) {
2157 evt
= ibmvfc_get_event(vhost
);
2158 ibmvfc_init_event(evt
, ibmvfc_sync_completion
, IBMVFC_MAD_FORMAT
);
2161 memset(tmf
, 0, sizeof(*tmf
));
2162 tmf
->common
.version
= 1;
2163 tmf
->common
.opcode
= IBMVFC_TMF_MAD
;
2164 tmf
->common
.length
= sizeof(*tmf
);
2165 tmf
->scsi_id
= rport
->port_id
;
2166 int_to_scsilun(sdev
->lun
, &tmf
->lun
);
2167 tmf
->flags
= (type
| IBMVFC_TMF_LUA_VALID
);
2168 tmf
->cancel_key
= (unsigned long)sdev
->hostdata
;
2169 tmf
->my_cancel_key
= (unsigned long)starget
->hostdata
;
2171 evt
->sync_iu
= &rsp
;
2172 init_completion(&evt
->comp
);
2173 rsp_rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
2176 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2179 sdev_printk(KERN_ERR
, sdev
, "Failed to send cancel event. rc=%d\n", rsp_rc
);
2183 sdev_printk(KERN_INFO
, sdev
, "Cancelling outstanding commands.\n");
2185 wait_for_completion(&evt
->comp
);
2186 status
= rsp
.mad_common
.status
;
2187 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2188 ibmvfc_free_event(evt
);
2189 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2191 if (status
!= IBMVFC_MAD_SUCCESS
) {
2192 sdev_printk(KERN_WARNING
, sdev
, "Cancel failed with rc=%x\n", status
);
2196 sdev_printk(KERN_INFO
, sdev
, "Successfully cancelled outstanding commands\n");
2201 * ibmvfc_match_target - Match function for specified target
2202 * @evt: ibmvfc event struct
2203 * @device: device to match (starget)
2206 * 1 if event matches starget / 0 if event does not match starget
2208 static int ibmvfc_match_target(struct ibmvfc_event
*evt
, void *device
)
2210 if (evt
->cmnd
&& scsi_target(evt
->cmnd
->device
) == device
)
2216 * ibmvfc_match_lun - Match function for specified LUN
2217 * @evt: ibmvfc event struct
2218 * @device: device to match (sdev)
2221 * 1 if event matches sdev / 0 if event does not match sdev
2223 static int ibmvfc_match_lun(struct ibmvfc_event
*evt
, void *device
)
2225 if (evt
->cmnd
&& evt
->cmnd
->device
== device
)
2231 * ibmvfc_wait_for_ops - Wait for ops to complete
2232 * @vhost: ibmvfc host struct
2233 * @device: device to match (starget or sdev)
2234 * @match: match function
2239 static int ibmvfc_wait_for_ops(struct ibmvfc_host
*vhost
, void *device
,
2240 int (*match
) (struct ibmvfc_event
*, void *))
2242 struct ibmvfc_event
*evt
;
2243 DECLARE_COMPLETION_ONSTACK(comp
);
2245 unsigned long flags
;
2246 signed long timeout
= init_timeout
* HZ
;
2251 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2252 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2253 if (match(evt
, device
)) {
2254 evt
->eh_comp
= &comp
;
2258 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2261 timeout
= wait_for_completion_timeout(&comp
, timeout
);
2265 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
2266 list_for_each_entry(evt
, &vhost
->sent
, queue
) {
2267 if (match(evt
, device
)) {
2268 evt
->eh_comp
= NULL
;
2272 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
2274 dev_err(vhost
->dev
, "Timed out waiting for aborted commands\n");
2276 return wait
? FAILED
: SUCCESS
;
2286 * ibmvfc_eh_abort_handler - Abort a command
2287 * @cmd: scsi command to abort
2292 static int ibmvfc_eh_abort_handler(struct scsi_cmnd
*cmd
)
2294 struct scsi_device
*sdev
= cmd
->device
;
2295 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2296 int cancel_rc
, abort_rc
;
2300 ibmvfc_wait_while_resetting(vhost
);
2301 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_ABORT_TASK_SET
);
2302 abort_rc
= ibmvfc_abort_task_set(sdev
);
2304 if (!cancel_rc
&& !abort_rc
)
2305 rc
= ibmvfc_wait_for_ops(vhost
, sdev
, ibmvfc_match_lun
);
2312 * ibmvfc_eh_device_reset_handler - Reset a single LUN
2313 * @cmd: scsi command struct
2318 static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
2320 struct scsi_device
*sdev
= cmd
->device
;
2321 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2322 int cancel_rc
, reset_rc
;
2326 ibmvfc_wait_while_resetting(vhost
);
2327 cancel_rc
= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_LUN_RESET
);
2328 reset_rc
= ibmvfc_reset_device(sdev
, IBMVFC_LUN_RESET
, "LUN");
2330 if (!cancel_rc
&& !reset_rc
)
2331 rc
= ibmvfc_wait_for_ops(vhost
, sdev
, ibmvfc_match_lun
);
2338 * ibmvfc_dev_cancel_all_abts - Device iterated cancel all function
2339 * @sdev: scsi device struct
2340 * @data: return code
2343 static void ibmvfc_dev_cancel_all_abts(struct scsi_device
*sdev
, void *data
)
2345 unsigned long *rc
= data
;
2346 *rc
|= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_ABORT_TASK_SET
);
2350 * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
2351 * @sdev: scsi device struct
2352 * @data: return code
2355 static void ibmvfc_dev_cancel_all_reset(struct scsi_device
*sdev
, void *data
)
2357 unsigned long *rc
= data
;
2358 *rc
|= ibmvfc_cancel_all(sdev
, IBMVFC_TMF_TGT_RESET
);
2362 * ibmvfc_dev_abort_all - Device iterated abort task set function
2363 * @sdev: scsi device struct
2364 * @data: return code
2367 static void ibmvfc_dev_abort_all(struct scsi_device
*sdev
, void *data
)
2369 unsigned long *rc
= data
;
2370 *rc
|= ibmvfc_abort_task_set(sdev
);
2374 * ibmvfc_eh_target_reset_handler - Reset the target
2375 * @cmd: scsi command struct
2380 static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd
*cmd
)
2382 struct scsi_device
*sdev
= cmd
->device
;
2383 struct ibmvfc_host
*vhost
= shost_priv(sdev
->host
);
2384 struct scsi_target
*starget
= scsi_target(sdev
);
2387 unsigned long cancel_rc
= 0;
2390 ibmvfc_wait_while_resetting(vhost
);
2391 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all_reset
);
2392 reset_rc
= ibmvfc_reset_device(sdev
, IBMVFC_TARGET_RESET
, "target");
2394 if (!cancel_rc
&& !reset_rc
)
2395 rc
= ibmvfc_wait_for_ops(vhost
, starget
, ibmvfc_match_target
);
2402 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
2403 * @cmd: struct scsi_cmnd having problems
2406 static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
2409 struct ibmvfc_host
*vhost
= shost_priv(cmd
->device
->host
);
2411 dev_err(vhost
->dev
, "Resetting connection due to error recovery\n");
2412 rc
= ibmvfc_issue_fc_host_lip(vhost
->host
);
2413 return rc
? FAILED
: SUCCESS
;
2417 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2418 * @rport: rport struct
2423 static void ibmvfc_terminate_rport_io(struct fc_rport
*rport
)
2425 struct scsi_target
*starget
= to_scsi_target(&rport
->dev
);
2426 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2427 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2428 unsigned long cancel_rc
= 0;
2429 unsigned long abort_rc
= 0;
2433 starget_for_each_device(starget
, &cancel_rc
, ibmvfc_dev_cancel_all_abts
);
2434 starget_for_each_device(starget
, &abort_rc
, ibmvfc_dev_abort_all
);
2436 if (!cancel_rc
&& !abort_rc
)
2437 rc
= ibmvfc_wait_for_ops(vhost
, starget
, ibmvfc_match_target
);
2440 ibmvfc_issue_fc_host_lip(shost
);
2444 static const struct {
2445 enum ibmvfc_async_event ae
;
2448 { IBMVFC_AE_ELS_PLOGI
, "PLOGI" },
2449 { IBMVFC_AE_ELS_LOGO
, "LOGO" },
2450 { IBMVFC_AE_ELS_PRLO
, "PRLO" },
2451 { IBMVFC_AE_SCN_NPORT
, "N-Port SCN" },
2452 { IBMVFC_AE_SCN_GROUP
, "Group SCN" },
2453 { IBMVFC_AE_SCN_DOMAIN
, "Domain SCN" },
2454 { IBMVFC_AE_SCN_FABRIC
, "Fabric SCN" },
2455 { IBMVFC_AE_LINK_UP
, "Link Up" },
2456 { IBMVFC_AE_LINK_DOWN
, "Link Down" },
2457 { IBMVFC_AE_LINK_DEAD
, "Link Dead" },
2458 { IBMVFC_AE_HALT
, "Halt" },
2459 { IBMVFC_AE_RESUME
, "Resume" },
2460 { IBMVFC_AE_ADAPTER_FAILED
, "Adapter Failed" },
2463 static const char *unknown_ae
= "Unknown async";
2466 * ibmvfc_get_ae_desc - Get text description for async event
2470 static const char *ibmvfc_get_ae_desc(u64 ae
)
2474 for (i
= 0; i
< ARRAY_SIZE(ae_desc
); i
++)
2475 if (ae_desc
[i
].ae
== ae
)
2476 return ae_desc
[i
].desc
;
2482 * ibmvfc_handle_async - Handle an async event from the adapter
2483 * @crq: crq to process
2484 * @vhost: ibmvfc host struct
2487 static void ibmvfc_handle_async(struct ibmvfc_async_crq
*crq
,
2488 struct ibmvfc_host
*vhost
)
2490 const char *desc
= ibmvfc_get_ae_desc(crq
->event
);
2491 struct ibmvfc_target
*tgt
;
2493 ibmvfc_log(vhost
, 3, "%s event received. scsi_id: %llx, wwpn: %llx,"
2494 " node_name: %llx\n", desc
, crq
->scsi_id
, crq
->wwpn
, crq
->node_name
);
2496 switch (crq
->event
) {
2497 case IBMVFC_AE_RESUME
:
2498 switch (crq
->link_state
) {
2499 case IBMVFC_AE_LS_LINK_DOWN
:
2500 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2502 case IBMVFC_AE_LS_LINK_DEAD
:
2503 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2505 case IBMVFC_AE_LS_LINK_UP
:
2506 case IBMVFC_AE_LS_LINK_BOUNCED
:
2508 vhost
->events_to_log
|= IBMVFC_AE_LINKUP
;
2509 vhost
->delay_init
= 1;
2510 __ibmvfc_reset_host(vhost
);
2515 case IBMVFC_AE_LINK_UP
:
2516 vhost
->events_to_log
|= IBMVFC_AE_LINKUP
;
2517 vhost
->delay_init
= 1;
2518 __ibmvfc_reset_host(vhost
);
2520 case IBMVFC_AE_SCN_FABRIC
:
2521 case IBMVFC_AE_SCN_DOMAIN
:
2522 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2523 vhost
->delay_init
= 1;
2524 __ibmvfc_reset_host(vhost
);
2526 case IBMVFC_AE_SCN_NPORT
:
2527 case IBMVFC_AE_SCN_GROUP
:
2528 vhost
->events_to_log
|= IBMVFC_AE_RSCN
;
2529 ibmvfc_reinit_host(vhost
);
2531 case IBMVFC_AE_ELS_LOGO
:
2532 case IBMVFC_AE_ELS_PRLO
:
2533 case IBMVFC_AE_ELS_PLOGI
:
2534 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
2535 if (!crq
->scsi_id
&& !crq
->wwpn
&& !crq
->node_name
)
2537 if (crq
->scsi_id
&& tgt
->scsi_id
!= crq
->scsi_id
)
2539 if (crq
->wwpn
&& tgt
->ids
.port_name
!= crq
->wwpn
)
2541 if (crq
->node_name
&& tgt
->ids
.node_name
!= crq
->node_name
)
2543 if (tgt
->need_login
&& crq
->event
== IBMVFC_AE_ELS_LOGO
)
2545 if (!tgt
->need_login
|| crq
->event
== IBMVFC_AE_ELS_PLOGI
) {
2546 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
2547 ibmvfc_reinit_host(vhost
);
2551 case IBMVFC_AE_LINK_DOWN
:
2552 case IBMVFC_AE_ADAPTER_FAILED
:
2553 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2555 case IBMVFC_AE_LINK_DEAD
:
2556 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2558 case IBMVFC_AE_HALT
:
2559 ibmvfc_link_down(vhost
, IBMVFC_HALTED
);
2562 dev_err(vhost
->dev
, "Unknown async event received: %lld\n", crq
->event
);
2568 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2569 * @crq: Command/Response queue
2570 * @vhost: ibmvfc host struct
2573 static void ibmvfc_handle_crq(struct ibmvfc_crq
*crq
, struct ibmvfc_host
*vhost
)
2576 struct ibmvfc_event
*evt
= (struct ibmvfc_event
*)crq
->ioba
;
2578 switch (crq
->valid
) {
2579 case IBMVFC_CRQ_INIT_RSP
:
2580 switch (crq
->format
) {
2581 case IBMVFC_CRQ_INIT
:
2582 dev_info(vhost
->dev
, "Partner initialized\n");
2583 /* Send back a response */
2584 rc
= ibmvfc_send_crq_init_complete(vhost
);
2586 ibmvfc_init_host(vhost
);
2588 dev_err(vhost
->dev
, "Unable to send init rsp. rc=%ld\n", rc
);
2590 case IBMVFC_CRQ_INIT_COMPLETE
:
2591 dev_info(vhost
->dev
, "Partner initialization complete\n");
2592 ibmvfc_init_host(vhost
);
2595 dev_err(vhost
->dev
, "Unknown crq message type: %d\n", crq
->format
);
2598 case IBMVFC_CRQ_XPORT_EVENT
:
2599 vhost
->state
= IBMVFC_NO_CRQ
;
2600 vhost
->logged_in
= 0;
2601 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
2602 if (crq
->format
== IBMVFC_PARTITION_MIGRATED
) {
2603 /* We need to re-setup the interpartition connection */
2604 dev_info(vhost
->dev
, "Re-enabling adapter\n");
2605 vhost
->client_migrated
= 1;
2606 ibmvfc_purge_requests(vhost
, DID_REQUEUE
);
2607 if ((rc
= ibmvfc_reenable_crq_queue(vhost
)) ||
2608 (rc
= ibmvfc_send_crq_init(vhost
))) {
2609 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2610 dev_err(vhost
->dev
, "Error after enable (rc=%ld)\n", rc
);
2612 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2614 dev_err(vhost
->dev
, "Virtual adapter failed (rc=%d)\n", crq
->format
);
2616 ibmvfc_purge_requests(vhost
, DID_ERROR
);
2617 if ((rc
= ibmvfc_reset_crq(vhost
)) ||
2618 (rc
= ibmvfc_send_crq_init(vhost
))) {
2619 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
2620 dev_err(vhost
->dev
, "Error after reset (rc=%ld)\n", rc
);
2622 ibmvfc_link_down(vhost
, IBMVFC_LINK_DOWN
);
2625 case IBMVFC_CRQ_CMD_RSP
:
2628 dev_err(vhost
->dev
, "Got an invalid message type 0x%02x\n", crq
->valid
);
2632 if (crq
->format
== IBMVFC_ASYNC_EVENT
)
2635 /* The only kind of payload CRQs we should get are responses to
2636 * things we send. Make sure this response is to something we
2639 if (unlikely(!ibmvfc_valid_event(&vhost
->pool
, evt
))) {
2640 dev_err(vhost
->dev
, "Returned correlation_token 0x%08llx is invalid!\n",
2645 if (unlikely(atomic_read(&evt
->free
))) {
2646 dev_err(vhost
->dev
, "Received duplicate correlation_token 0x%08llx!\n",
2651 del_timer(&evt
->timer
);
2652 list_del(&evt
->queue
);
2653 ibmvfc_trc_end(evt
);
2658 * ibmvfc_scan_finished - Check if the device scan is done.
2659 * @shost: scsi host struct
2660 * @time: current elapsed time
2663 * 0 if scan is not done / 1 if scan is done
2665 static int ibmvfc_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
2667 unsigned long flags
;
2668 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2671 spin_lock_irqsave(shost
->host_lock
, flags
);
2672 if (time
>= (init_timeout
* HZ
)) {
2673 dev_info(vhost
->dev
, "Scan taking longer than %d seconds, "
2674 "continuing initialization\n", init_timeout
);
2678 if (vhost
->scan_complete
)
2680 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2685 * ibmvfc_slave_alloc - Setup the device's task set value
2686 * @sdev: struct scsi_device device to configure
2688 * Set the device's task set value so that error handling works as
2692 * 0 on success / -ENXIO if device does not exist
2694 static int ibmvfc_slave_alloc(struct scsi_device
*sdev
)
2696 struct Scsi_Host
*shost
= sdev
->host
;
2697 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
2698 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2699 unsigned long flags
= 0;
2701 if (!rport
|| fc_remote_port_chkready(rport
))
2704 spin_lock_irqsave(shost
->host_lock
, flags
);
2705 sdev
->hostdata
= (void *)(unsigned long)vhost
->task_set
++;
2706 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2711 * ibmvfc_target_alloc - Setup the target's task set value
2712 * @starget: struct scsi_target
2714 * Set the target's task set value so that error handling works as
2718 * 0 on success / -ENXIO if device does not exist
2720 static int ibmvfc_target_alloc(struct scsi_target
*starget
)
2722 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
2723 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2724 unsigned long flags
= 0;
2726 spin_lock_irqsave(shost
->host_lock
, flags
);
2727 starget
->hostdata
= (void *)(unsigned long)vhost
->task_set
++;
2728 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2733 * ibmvfc_slave_configure - Configure the device
2734 * @sdev: struct scsi_device device to configure
2736 * Enable allow_restart for a device if it is a disk. Adjust the
2737 * queue_depth here also.
2742 static int ibmvfc_slave_configure(struct scsi_device
*sdev
)
2744 struct Scsi_Host
*shost
= sdev
->host
;
2745 struct fc_rport
*rport
= starget_to_rport(sdev
->sdev_target
);
2746 unsigned long flags
= 0;
2748 spin_lock_irqsave(shost
->host_lock
, flags
);
2749 if (sdev
->type
== TYPE_DISK
)
2750 sdev
->allow_restart
= 1;
2752 if (sdev
->tagged_supported
) {
2753 scsi_set_tag_type(sdev
, MSG_SIMPLE_TAG
);
2754 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2756 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2758 rport
->dev_loss_tmo
= dev_loss_tmo
;
2759 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2764 * ibmvfc_change_queue_depth - Change the device's queue depth
2765 * @sdev: scsi device struct
2766 * @qdepth: depth to set
2767 * @reason: calling context
2772 static int ibmvfc_change_queue_depth(struct scsi_device
*sdev
, int qdepth
,
2775 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2778 if (qdepth
> IBMVFC_MAX_CMDS_PER_LUN
)
2779 qdepth
= IBMVFC_MAX_CMDS_PER_LUN
;
2781 scsi_adjust_queue_depth(sdev
, 0, qdepth
);
2782 return sdev
->queue_depth
;
2786 * ibmvfc_change_queue_type - Change the device's queue type
2787 * @sdev: scsi device struct
2788 * @tag_type: type of tags to use
2791 * actual queue type set
2793 static int ibmvfc_change_queue_type(struct scsi_device
*sdev
, int tag_type
)
2795 if (sdev
->tagged_supported
) {
2796 scsi_set_tag_type(sdev
, tag_type
);
2799 scsi_activate_tcq(sdev
, sdev
->queue_depth
);
2801 scsi_deactivate_tcq(sdev
, sdev
->queue_depth
);
2808 static ssize_t
ibmvfc_show_host_partition_name(struct device
*dev
,
2809 struct device_attribute
*attr
, char *buf
)
2811 struct Scsi_Host
*shost
= class_to_shost(dev
);
2812 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2814 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2815 vhost
->login_buf
->resp
.partition_name
);
2818 static ssize_t
ibmvfc_show_host_device_name(struct device
*dev
,
2819 struct device_attribute
*attr
, char *buf
)
2821 struct Scsi_Host
*shost
= class_to_shost(dev
);
2822 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2824 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2825 vhost
->login_buf
->resp
.device_name
);
2828 static ssize_t
ibmvfc_show_host_loc_code(struct device
*dev
,
2829 struct device_attribute
*attr
, char *buf
)
2831 struct Scsi_Host
*shost
= class_to_shost(dev
);
2832 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2834 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2835 vhost
->login_buf
->resp
.port_loc_code
);
2838 static ssize_t
ibmvfc_show_host_drc_name(struct device
*dev
,
2839 struct device_attribute
*attr
, char *buf
)
2841 struct Scsi_Host
*shost
= class_to_shost(dev
);
2842 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2844 return snprintf(buf
, PAGE_SIZE
, "%s\n",
2845 vhost
->login_buf
->resp
.drc_name
);
2848 static ssize_t
ibmvfc_show_host_npiv_version(struct device
*dev
,
2849 struct device_attribute
*attr
, char *buf
)
2851 struct Scsi_Host
*shost
= class_to_shost(dev
);
2852 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2853 return snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->login_buf
->resp
.version
);
2856 static ssize_t
ibmvfc_show_host_capabilities(struct device
*dev
,
2857 struct device_attribute
*attr
, char *buf
)
2859 struct Scsi_Host
*shost
= class_to_shost(dev
);
2860 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2861 return snprintf(buf
, PAGE_SIZE
, "%llx\n", vhost
->login_buf
->resp
.capabilities
);
2865 * ibmvfc_show_log_level - Show the adapter's error logging level
2866 * @dev: class device struct
2870 * number of bytes printed to buffer
2872 static ssize_t
ibmvfc_show_log_level(struct device
*dev
,
2873 struct device_attribute
*attr
, char *buf
)
2875 struct Scsi_Host
*shost
= class_to_shost(dev
);
2876 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2877 unsigned long flags
= 0;
2880 spin_lock_irqsave(shost
->host_lock
, flags
);
2881 len
= snprintf(buf
, PAGE_SIZE
, "%d\n", vhost
->log_level
);
2882 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2887 * ibmvfc_store_log_level - Change the adapter's error logging level
2888 * @dev: class device struct
2892 * number of bytes printed to buffer
2894 static ssize_t
ibmvfc_store_log_level(struct device
*dev
,
2895 struct device_attribute
*attr
,
2896 const char *buf
, size_t count
)
2898 struct Scsi_Host
*shost
= class_to_shost(dev
);
2899 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2900 unsigned long flags
= 0;
2902 spin_lock_irqsave(shost
->host_lock
, flags
);
2903 vhost
->log_level
= simple_strtoul(buf
, NULL
, 10);
2904 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2908 static DEVICE_ATTR(partition_name
, S_IRUGO
, ibmvfc_show_host_partition_name
, NULL
);
2909 static DEVICE_ATTR(device_name
, S_IRUGO
, ibmvfc_show_host_device_name
, NULL
);
2910 static DEVICE_ATTR(port_loc_code
, S_IRUGO
, ibmvfc_show_host_loc_code
, NULL
);
2911 static DEVICE_ATTR(drc_name
, S_IRUGO
, ibmvfc_show_host_drc_name
, NULL
);
2912 static DEVICE_ATTR(npiv_version
, S_IRUGO
, ibmvfc_show_host_npiv_version
, NULL
);
2913 static DEVICE_ATTR(capabilities
, S_IRUGO
, ibmvfc_show_host_capabilities
, NULL
);
2914 static DEVICE_ATTR(log_level
, S_IRUGO
| S_IWUSR
,
2915 ibmvfc_show_log_level
, ibmvfc_store_log_level
);
2917 #ifdef CONFIG_SCSI_IBMVFC_TRACE
2919 * ibmvfc_read_trace - Dump the adapter trace
2920 * @kobj: kobject struct
2921 * @bin_attr: bin_attribute struct
2924 * @count: buffer size
2927 * number of bytes printed to buffer
2929 static ssize_t
ibmvfc_read_trace(struct kobject
*kobj
,
2930 struct bin_attribute
*bin_attr
,
2931 char *buf
, loff_t off
, size_t count
)
2933 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
2934 struct Scsi_Host
*shost
= class_to_shost(dev
);
2935 struct ibmvfc_host
*vhost
= shost_priv(shost
);
2936 unsigned long flags
= 0;
2937 int size
= IBMVFC_TRACE_SIZE
;
2938 char *src
= (char *)vhost
->trace
;
2942 if (off
+ count
> size
) {
2947 spin_lock_irqsave(shost
->host_lock
, flags
);
2948 memcpy(buf
, &src
[off
], count
);
2949 spin_unlock_irqrestore(shost
->host_lock
, flags
);
2953 static struct bin_attribute ibmvfc_trace_attr
= {
2959 .read
= ibmvfc_read_trace
,
2963 static struct device_attribute
*ibmvfc_attrs
[] = {
2964 &dev_attr_partition_name
,
2965 &dev_attr_device_name
,
2966 &dev_attr_port_loc_code
,
2968 &dev_attr_npiv_version
,
2969 &dev_attr_capabilities
,
2970 &dev_attr_log_level
,
2974 static struct scsi_host_template driver_template
= {
2975 .module
= THIS_MODULE
,
2976 .name
= "IBM POWER Virtual FC Adapter",
2977 .proc_name
= IBMVFC_NAME
,
2978 .queuecommand
= ibmvfc_queuecommand
,
2979 .eh_abort_handler
= ibmvfc_eh_abort_handler
,
2980 .eh_device_reset_handler
= ibmvfc_eh_device_reset_handler
,
2981 .eh_target_reset_handler
= ibmvfc_eh_target_reset_handler
,
2982 .eh_host_reset_handler
= ibmvfc_eh_host_reset_handler
,
2983 .slave_alloc
= ibmvfc_slave_alloc
,
2984 .slave_configure
= ibmvfc_slave_configure
,
2985 .target_alloc
= ibmvfc_target_alloc
,
2986 .scan_finished
= ibmvfc_scan_finished
,
2987 .change_queue_depth
= ibmvfc_change_queue_depth
,
2988 .change_queue_type
= ibmvfc_change_queue_type
,
2990 .can_queue
= IBMVFC_MAX_REQUESTS_DEFAULT
,
2992 .sg_tablesize
= SG_ALL
,
2993 .max_sectors
= IBMVFC_MAX_SECTORS
,
2994 .use_clustering
= ENABLE_CLUSTERING
,
2995 .shost_attrs
= ibmvfc_attrs
,
2999 * ibmvfc_next_async_crq - Returns the next entry in async queue
3000 * @vhost: ibmvfc host struct
3003 * Pointer to next entry in queue / NULL if empty
3005 static struct ibmvfc_async_crq
*ibmvfc_next_async_crq(struct ibmvfc_host
*vhost
)
3007 struct ibmvfc_async_crq_queue
*async_crq
= &vhost
->async_crq
;
3008 struct ibmvfc_async_crq
*crq
;
3010 crq
= &async_crq
->msgs
[async_crq
->cur
];
3011 if (crq
->valid
& 0x80) {
3012 if (++async_crq
->cur
== async_crq
->size
)
3021 * ibmvfc_next_crq - Returns the next entry in message queue
3022 * @vhost: ibmvfc host struct
3025 * Pointer to next entry in queue / NULL if empty
3027 static struct ibmvfc_crq
*ibmvfc_next_crq(struct ibmvfc_host
*vhost
)
3029 struct ibmvfc_crq_queue
*queue
= &vhost
->crq
;
3030 struct ibmvfc_crq
*crq
;
3032 crq
= &queue
->msgs
[queue
->cur
];
3033 if (crq
->valid
& 0x80) {
3034 if (++queue
->cur
== queue
->size
)
3043 * ibmvfc_interrupt - Interrupt handler
3044 * @irq: number of irq to handle, not used
3045 * @dev_instance: ibmvfc_host that received interrupt
3050 static irqreturn_t
ibmvfc_interrupt(int irq
, void *dev_instance
)
3052 struct ibmvfc_host
*vhost
= (struct ibmvfc_host
*)dev_instance
;
3053 unsigned long flags
;
3055 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3056 vio_disable_interrupts(to_vio_dev(vhost
->dev
));
3057 tasklet_schedule(&vhost
->tasklet
);
3058 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3063 * ibmvfc_tasklet - Interrupt handler tasklet
3064 * @data: ibmvfc host struct
3069 static void ibmvfc_tasklet(void *data
)
3071 struct ibmvfc_host
*vhost
= data
;
3072 struct vio_dev
*vdev
= to_vio_dev(vhost
->dev
);
3073 struct ibmvfc_crq
*crq
;
3074 struct ibmvfc_async_crq
*async
;
3075 unsigned long flags
;
3078 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3080 /* Pull all the valid messages off the async CRQ */
3081 while ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
3082 ibmvfc_handle_async(async
, vhost
);
3086 /* Pull all the valid messages off the CRQ */
3087 while ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
3088 ibmvfc_handle_crq(crq
, vhost
);
3092 vio_enable_interrupts(vdev
);
3093 if ((async
= ibmvfc_next_async_crq(vhost
)) != NULL
) {
3094 vio_disable_interrupts(vdev
);
3095 ibmvfc_handle_async(async
, vhost
);
3097 } else if ((crq
= ibmvfc_next_crq(vhost
)) != NULL
) {
3098 vio_disable_interrupts(vdev
);
3099 ibmvfc_handle_crq(crq
, vhost
);
3105 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3109 * ibmvfc_init_tgt - Set the next init job step for the target
3110 * @tgt: ibmvfc target struct
3111 * @job_step: job step to perform
3114 static void ibmvfc_init_tgt(struct ibmvfc_target
*tgt
,
3115 void (*job_step
) (struct ibmvfc_target
*))
3117 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT
);
3118 tgt
->job_step
= job_step
;
3119 wake_up(&tgt
->vhost
->work_wait_q
);
3123 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
3124 * @tgt: ibmvfc target struct
3125 * @job_step: initialization job step
3127 * Returns: 1 if step will be retried / 0 if not
3130 static int ibmvfc_retry_tgt_init(struct ibmvfc_target
*tgt
,
3131 void (*job_step
) (struct ibmvfc_target
*))
3133 if (++tgt
->init_retries
> IBMVFC_MAX_TGT_INIT_RETRIES
) {
3134 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3135 wake_up(&tgt
->vhost
->work_wait_q
);
3138 ibmvfc_init_tgt(tgt
, job_step
);
3142 /* Defined in FC-LS */
3143 static const struct {
3160 * ibmvfc_get_prli_rsp - Find PRLI response index
3161 * @flags: PRLI response flags
3164 static int ibmvfc_get_prli_rsp(u16 flags
)
3167 int code
= (flags
& 0x0f00) >> 8;
3169 for (i
= 0; i
< ARRAY_SIZE(prli_rsp
); i
++)
3170 if (prli_rsp
[i
].code
== code
)
3177 * ibmvfc_tgt_prli_done - Completion handler for Process Login
3178 * @evt: ibmvfc event struct
3181 static void ibmvfc_tgt_prli_done(struct ibmvfc_event
*evt
)
3183 struct ibmvfc_target
*tgt
= evt
->tgt
;
3184 struct ibmvfc_host
*vhost
= evt
->vhost
;
3185 struct ibmvfc_process_login
*rsp
= &evt
->xfer_iu
->prli
;
3186 struct ibmvfc_prli_svc_parms
*parms
= &rsp
->parms
;
3187 u32 status
= rsp
->common
.status
;
3188 int index
, level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3190 vhost
->discovery_threads
--;
3191 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3193 case IBMVFC_MAD_SUCCESS
:
3194 tgt_dbg(tgt
, "Process Login succeeded: %X %02X %04X\n",
3195 parms
->type
, parms
->flags
, parms
->service_parms
);
3197 if (parms
->type
== IBMVFC_SCSI_FCP_TYPE
) {
3198 index
= ibmvfc_get_prli_rsp(parms
->flags
);
3199 if (prli_rsp
[index
].logged_in
) {
3200 if (parms
->flags
& IBMVFC_PRLI_EST_IMG_PAIR
) {
3201 tgt
->need_login
= 0;
3203 if (parms
->service_parms
& IBMVFC_PRLI_TARGET_FUNC
)
3204 tgt
->ids
.roles
|= FC_PORT_ROLE_FCP_TARGET
;
3205 if (parms
->service_parms
& IBMVFC_PRLI_INITIATOR_FUNC
)
3206 tgt
->ids
.roles
|= FC_PORT_ROLE_FCP_INITIATOR
;
3209 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3210 } else if (prli_rsp
[index
].retry
)
3211 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3213 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3215 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3217 case IBMVFC_MAD_DRIVER_FAILED
:
3219 case IBMVFC_MAD_CRQ_ERROR
:
3220 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3222 case IBMVFC_MAD_FAILED
:
3224 if ((rsp
->status
& IBMVFC_VIOS_FAILURE
) && rsp
->error
== IBMVFC_PLOGI_REQUIRED
)
3225 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3226 else if (tgt
->logo_rcvd
)
3227 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3228 else if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
3229 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_prli
);
3231 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3233 tgt_log(tgt
, level
, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
3234 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
),
3235 rsp
->status
, rsp
->error
, status
);
3239 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3240 ibmvfc_free_event(evt
);
3241 wake_up(&vhost
->work_wait_q
);
3245 * ibmvfc_tgt_send_prli - Send a process login
3246 * @tgt: ibmvfc target struct
3249 static void ibmvfc_tgt_send_prli(struct ibmvfc_target
*tgt
)
3251 struct ibmvfc_process_login
*prli
;
3252 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3253 struct ibmvfc_event
*evt
;
3255 if (vhost
->discovery_threads
>= disc_threads
)
3258 kref_get(&tgt
->kref
);
3259 evt
= ibmvfc_get_event(vhost
);
3260 vhost
->discovery_threads
++;
3261 ibmvfc_init_event(evt
, ibmvfc_tgt_prli_done
, IBMVFC_MAD_FORMAT
);
3263 prli
= &evt
->iu
.prli
;
3264 memset(prli
, 0, sizeof(*prli
));
3265 prli
->common
.version
= 1;
3266 prli
->common
.opcode
= IBMVFC_PROCESS_LOGIN
;
3267 prli
->common
.length
= sizeof(*prli
);
3268 prli
->scsi_id
= tgt
->scsi_id
;
3270 prli
->parms
.type
= IBMVFC_SCSI_FCP_TYPE
;
3271 prli
->parms
.flags
= IBMVFC_PRLI_EST_IMG_PAIR
;
3272 prli
->parms
.service_parms
= IBMVFC_PRLI_INITIATOR_FUNC
;
3274 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3275 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3276 vhost
->discovery_threads
--;
3277 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3278 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3280 tgt_dbg(tgt
, "Sent process login\n");
3284 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
3285 * @evt: ibmvfc event struct
3288 static void ibmvfc_tgt_plogi_done(struct ibmvfc_event
*evt
)
3290 struct ibmvfc_target
*tgt
= evt
->tgt
;
3291 struct ibmvfc_host
*vhost
= evt
->vhost
;
3292 struct ibmvfc_port_login
*rsp
= &evt
->xfer_iu
->plogi
;
3293 u32 status
= rsp
->common
.status
;
3294 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3296 vhost
->discovery_threads
--;
3297 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3299 case IBMVFC_MAD_SUCCESS
:
3300 tgt_dbg(tgt
, "Port Login succeeded\n");
3301 if (tgt
->ids
.port_name
&&
3302 tgt
->ids
.port_name
!= wwn_to_u64(rsp
->service_parms
.port_name
)) {
3304 tgt_dbg(tgt
, "Port re-init required\n");
3307 tgt
->ids
.node_name
= wwn_to_u64(rsp
->service_parms
.node_name
);
3308 tgt
->ids
.port_name
= wwn_to_u64(rsp
->service_parms
.port_name
);
3309 tgt
->ids
.port_id
= tgt
->scsi_id
;
3310 memcpy(&tgt
->service_parms
, &rsp
->service_parms
,
3311 sizeof(tgt
->service_parms
));
3312 memcpy(&tgt
->service_parms_change
, &rsp
->service_parms_change
,
3313 sizeof(tgt
->service_parms_change
));
3314 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_prli
);
3316 case IBMVFC_MAD_DRIVER_FAILED
:
3318 case IBMVFC_MAD_CRQ_ERROR
:
3319 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3321 case IBMVFC_MAD_FAILED
:
3323 if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
3324 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_send_plogi
);
3326 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3328 tgt_log(tgt
, level
, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3329 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
,
3330 ibmvfc_get_fc_type(rsp
->fc_type
), rsp
->fc_type
,
3331 ibmvfc_get_ls_explain(rsp
->fc_explain
), rsp
->fc_explain
, status
);
3335 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3336 ibmvfc_free_event(evt
);
3337 wake_up(&vhost
->work_wait_q
);
3341 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
3342 * @tgt: ibmvfc target struct
3345 static void ibmvfc_tgt_send_plogi(struct ibmvfc_target
*tgt
)
3347 struct ibmvfc_port_login
*plogi
;
3348 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3349 struct ibmvfc_event
*evt
;
3351 if (vhost
->discovery_threads
>= disc_threads
)
3354 kref_get(&tgt
->kref
);
3356 evt
= ibmvfc_get_event(vhost
);
3357 vhost
->discovery_threads
++;
3358 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3359 ibmvfc_init_event(evt
, ibmvfc_tgt_plogi_done
, IBMVFC_MAD_FORMAT
);
3361 plogi
= &evt
->iu
.plogi
;
3362 memset(plogi
, 0, sizeof(*plogi
));
3363 plogi
->common
.version
= 1;
3364 plogi
->common
.opcode
= IBMVFC_PORT_LOGIN
;
3365 plogi
->common
.length
= sizeof(*plogi
);
3366 plogi
->scsi_id
= tgt
->scsi_id
;
3368 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3369 vhost
->discovery_threads
--;
3370 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3371 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3373 tgt_dbg(tgt
, "Sent port login\n");
3377 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
3378 * @evt: ibmvfc event struct
3381 static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event
*evt
)
3383 struct ibmvfc_target
*tgt
= evt
->tgt
;
3384 struct ibmvfc_host
*vhost
= evt
->vhost
;
3385 struct ibmvfc_implicit_logout
*rsp
= &evt
->xfer_iu
->implicit_logout
;
3386 u32 status
= rsp
->common
.status
;
3388 vhost
->discovery_threads
--;
3389 ibmvfc_free_event(evt
);
3390 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3393 case IBMVFC_MAD_SUCCESS
:
3394 tgt_dbg(tgt
, "Implicit Logout succeeded\n");
3396 case IBMVFC_MAD_DRIVER_FAILED
:
3397 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3398 wake_up(&vhost
->work_wait_q
);
3400 case IBMVFC_MAD_FAILED
:
3402 tgt_err(tgt
, "Implicit Logout failed: rc=0x%02X\n", status
);
3406 if (vhost
->action
== IBMVFC_HOST_ACTION_TGT_INIT
)
3407 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_send_plogi
);
3408 else if (vhost
->action
== IBMVFC_HOST_ACTION_QUERY_TGTS
&&
3409 tgt
->scsi_id
!= tgt
->new_scsi_id
)
3410 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3411 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3412 wake_up(&vhost
->work_wait_q
);
3416 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
3417 * @tgt: ibmvfc target struct
3420 static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target
*tgt
)
3422 struct ibmvfc_implicit_logout
*mad
;
3423 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3424 struct ibmvfc_event
*evt
;
3426 if (vhost
->discovery_threads
>= disc_threads
)
3429 kref_get(&tgt
->kref
);
3430 evt
= ibmvfc_get_event(vhost
);
3431 vhost
->discovery_threads
++;
3432 ibmvfc_init_event(evt
, ibmvfc_tgt_implicit_logout_done
, IBMVFC_MAD_FORMAT
);
3434 mad
= &evt
->iu
.implicit_logout
;
3435 memset(mad
, 0, sizeof(*mad
));
3436 mad
->common
.version
= 1;
3437 mad
->common
.opcode
= IBMVFC_IMPLICIT_LOGOUT
;
3438 mad
->common
.length
= sizeof(*mad
);
3439 mad
->old_scsi_id
= tgt
->scsi_id
;
3441 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3442 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3443 vhost
->discovery_threads
--;
3444 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3445 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3447 tgt_dbg(tgt
, "Sent Implicit Logout\n");
3451 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
3452 * @mad: ibmvfc passthru mad struct
3453 * @tgt: ibmvfc target struct
3456 * 1 if PLOGI needed / 0 if PLOGI not needed
3458 static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad
*mad
,
3459 struct ibmvfc_target
*tgt
)
3461 if (memcmp(&mad
->fc_iu
.response
[2], &tgt
->ids
.port_name
,
3462 sizeof(tgt
->ids
.port_name
)))
3464 if (memcmp(&mad
->fc_iu
.response
[4], &tgt
->ids
.node_name
,
3465 sizeof(tgt
->ids
.node_name
)))
3467 if (mad
->fc_iu
.response
[6] != tgt
->scsi_id
)
3473 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
3474 * @evt: ibmvfc event struct
3477 static void ibmvfc_tgt_adisc_done(struct ibmvfc_event
*evt
)
3479 struct ibmvfc_target
*tgt
= evt
->tgt
;
3480 struct ibmvfc_host
*vhost
= evt
->vhost
;
3481 struct ibmvfc_passthru_mad
*mad
= &evt
->xfer_iu
->passthru
;
3482 u32 status
= mad
->common
.status
;
3483 u8 fc_reason
, fc_explain
;
3485 vhost
->discovery_threads
--;
3486 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3487 del_timer(&tgt
->timer
);
3490 case IBMVFC_MAD_SUCCESS
:
3491 tgt_dbg(tgt
, "ADISC succeeded\n");
3492 if (ibmvfc_adisc_needs_plogi(mad
, tgt
))
3493 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3495 case IBMVFC_MAD_DRIVER_FAILED
:
3497 case IBMVFC_MAD_FAILED
:
3499 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3500 fc_reason
= (mad
->fc_iu
.response
[1] & 0x00ff0000) >> 16;
3501 fc_explain
= (mad
->fc_iu
.response
[1] & 0x0000ff00) >> 8;
3502 tgt_info(tgt
, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3503 ibmvfc_get_cmd_error(mad
->iu
.status
, mad
->iu
.error
),
3504 mad
->iu
.status
, mad
->iu
.error
,
3505 ibmvfc_get_fc_type(fc_reason
), fc_reason
,
3506 ibmvfc_get_ls_explain(fc_explain
), fc_explain
, status
);
3510 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3511 ibmvfc_free_event(evt
);
3512 wake_up(&vhost
->work_wait_q
);
3516 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
3517 * @evt: ibmvfc event struct
3520 static void ibmvfc_init_passthru(struct ibmvfc_event
*evt
)
3522 struct ibmvfc_passthru_mad
*mad
= &evt
->iu
.passthru
;
3524 memset(mad
, 0, sizeof(*mad
));
3525 mad
->common
.version
= 1;
3526 mad
->common
.opcode
= IBMVFC_PASSTHRU
;
3527 mad
->common
.length
= sizeof(*mad
) - sizeof(mad
->fc_iu
) - sizeof(mad
->iu
);
3528 mad
->cmd_ioba
.va
= (u64
)evt
->crq
.ioba
+
3529 offsetof(struct ibmvfc_passthru_mad
, iu
);
3530 mad
->cmd_ioba
.len
= sizeof(mad
->iu
);
3531 mad
->iu
.cmd_len
= sizeof(mad
->fc_iu
.payload
);
3532 mad
->iu
.rsp_len
= sizeof(mad
->fc_iu
.response
);
3533 mad
->iu
.cmd
.va
= (u64
)evt
->crq
.ioba
+
3534 offsetof(struct ibmvfc_passthru_mad
, fc_iu
) +
3535 offsetof(struct ibmvfc_passthru_fc_iu
, payload
);
3536 mad
->iu
.cmd
.len
= sizeof(mad
->fc_iu
.payload
);
3537 mad
->iu
.rsp
.va
= (u64
)evt
->crq
.ioba
+
3538 offsetof(struct ibmvfc_passthru_mad
, fc_iu
) +
3539 offsetof(struct ibmvfc_passthru_fc_iu
, response
);
3540 mad
->iu
.rsp
.len
= sizeof(mad
->fc_iu
.response
);
3544 * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
3545 * @evt: ibmvfc event struct
3547 * Just cleanup this event struct. Everything else is handled by
3548 * the ADISC completion handler. If the ADISC never actually comes
3549 * back, we still have the timer running on the ADISC event struct
3550 * which will fire and cause the CRQ to get reset.
3553 static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event
*evt
)
3555 struct ibmvfc_host
*vhost
= evt
->vhost
;
3556 struct ibmvfc_target
*tgt
= evt
->tgt
;
3558 tgt_dbg(tgt
, "ADISC cancel complete\n");
3559 vhost
->abort_threads
--;
3560 ibmvfc_free_event(evt
);
3561 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3562 wake_up(&vhost
->work_wait_q
);
3566 * ibmvfc_adisc_timeout - Handle an ADISC timeout
3567 * @tgt: ibmvfc target struct
3569 * If an ADISC times out, send a cancel. If the cancel times
3570 * out, reset the CRQ. When the ADISC comes back as cancelled,
3571 * log back into the target.
3573 static void ibmvfc_adisc_timeout(struct ibmvfc_target
*tgt
)
3575 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3576 struct ibmvfc_event
*evt
;
3577 struct ibmvfc_tmf
*tmf
;
3578 unsigned long flags
;
3581 tgt_dbg(tgt
, "ADISC timeout\n");
3582 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3583 if (vhost
->abort_threads
>= disc_threads
||
3584 tgt
->action
!= IBMVFC_TGT_ACTION_INIT_WAIT
||
3585 vhost
->state
!= IBMVFC_INITIALIZING
||
3586 vhost
->action
!= IBMVFC_HOST_ACTION_QUERY_TGTS
) {
3587 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3591 vhost
->abort_threads
++;
3592 kref_get(&tgt
->kref
);
3593 evt
= ibmvfc_get_event(vhost
);
3594 ibmvfc_init_event(evt
, ibmvfc_tgt_adisc_cancel_done
, IBMVFC_MAD_FORMAT
);
3598 memset(tmf
, 0, sizeof(*tmf
));
3599 tmf
->common
.version
= 1;
3600 tmf
->common
.opcode
= IBMVFC_TMF_MAD
;
3601 tmf
->common
.length
= sizeof(*tmf
);
3602 tmf
->scsi_id
= tgt
->scsi_id
;
3603 tmf
->cancel_key
= tgt
->cancel_key
;
3605 rc
= ibmvfc_send_event(evt
, vhost
, default_timeout
);
3608 tgt_err(tgt
, "Failed to send cancel event for ADISC. rc=%d\n", rc
);
3609 vhost
->abort_threads
--;
3610 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3611 __ibmvfc_reset_host(vhost
);
3613 tgt_dbg(tgt
, "Attempting to cancel ADISC\n");
3614 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3618 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3619 * @tgt: ibmvfc target struct
3621 * When sending an ADISC we end up with two timers running. The
3622 * first timer is the timer in the ibmvfc target struct. If this
3623 * fires, we send a cancel to the target. The second timer is the
3624 * timer on the ibmvfc event for the ADISC, which is longer. If that
3625 * fires, it means the ADISC timed out and our attempt to cancel it
3626 * also failed, so we need to reset the CRQ.
3628 static void ibmvfc_tgt_adisc(struct ibmvfc_target
*tgt
)
3630 struct ibmvfc_passthru_mad
*mad
;
3631 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3632 struct ibmvfc_event
*evt
;
3634 if (vhost
->discovery_threads
>= disc_threads
)
3637 kref_get(&tgt
->kref
);
3638 evt
= ibmvfc_get_event(vhost
);
3639 vhost
->discovery_threads
++;
3640 ibmvfc_init_event(evt
, ibmvfc_tgt_adisc_done
, IBMVFC_MAD_FORMAT
);
3643 ibmvfc_init_passthru(evt
);
3644 mad
= &evt
->iu
.passthru
;
3645 mad
->iu
.flags
= IBMVFC_FC_ELS
;
3646 mad
->iu
.scsi_id
= tgt
->scsi_id
;
3647 mad
->iu
.cancel_key
= tgt
->cancel_key
;
3649 mad
->fc_iu
.payload
[0] = IBMVFC_ADISC
;
3650 memcpy(&mad
->fc_iu
.payload
[2], &vhost
->login_buf
->resp
.port_name
,
3651 sizeof(vhost
->login_buf
->resp
.port_name
));
3652 memcpy(&mad
->fc_iu
.payload
[4], &vhost
->login_buf
->resp
.node_name
,
3653 sizeof(vhost
->login_buf
->resp
.node_name
));
3654 mad
->fc_iu
.payload
[6] = vhost
->login_buf
->resp
.scsi_id
& 0x00ffffff;
3656 if (timer_pending(&tgt
->timer
))
3657 mod_timer(&tgt
->timer
, jiffies
+ (IBMVFC_ADISC_TIMEOUT
* HZ
));
3659 tgt
->timer
.data
= (unsigned long) tgt
;
3660 tgt
->timer
.expires
= jiffies
+ (IBMVFC_ADISC_TIMEOUT
* HZ
);
3661 tgt
->timer
.function
= (void (*)(unsigned long))ibmvfc_adisc_timeout
;
3662 add_timer(&tgt
->timer
);
3665 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3666 if (ibmvfc_send_event(evt
, vhost
, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT
)) {
3667 vhost
->discovery_threads
--;
3668 del_timer(&tgt
->timer
);
3669 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3670 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3672 tgt_dbg(tgt
, "Sent ADISC\n");
3676 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3677 * @evt: ibmvfc event struct
3680 static void ibmvfc_tgt_query_target_done(struct ibmvfc_event
*evt
)
3682 struct ibmvfc_target
*tgt
= evt
->tgt
;
3683 struct ibmvfc_host
*vhost
= evt
->vhost
;
3684 struct ibmvfc_query_tgt
*rsp
= &evt
->xfer_iu
->query_tgt
;
3685 u32 status
= rsp
->common
.status
;
3686 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3688 vhost
->discovery_threads
--;
3689 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3691 case IBMVFC_MAD_SUCCESS
:
3692 tgt_dbg(tgt
, "Query Target succeeded\n");
3693 tgt
->new_scsi_id
= rsp
->scsi_id
;
3694 if (rsp
->scsi_id
!= tgt
->scsi_id
)
3695 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3697 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_adisc
);
3699 case IBMVFC_MAD_DRIVER_FAILED
:
3701 case IBMVFC_MAD_CRQ_ERROR
:
3702 ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
3704 case IBMVFC_MAD_FAILED
:
3706 if ((rsp
->status
& IBMVFC_FABRIC_MAPPED
) == IBMVFC_FABRIC_MAPPED
&&
3707 rsp
->error
== IBMVFC_UNABLE_TO_PERFORM_REQ
&&
3708 rsp
->fc_explain
== IBMVFC_PORT_NAME_NOT_REG
)
3709 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3710 else if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
3711 level
+= ibmvfc_retry_tgt_init(tgt
, ibmvfc_tgt_query_target
);
3713 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_DEL_RPORT
);
3715 tgt_log(tgt
, level
, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3716 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
,
3717 ibmvfc_get_fc_type(rsp
->fc_type
), rsp
->fc_type
,
3718 ibmvfc_get_gs_explain(rsp
->fc_explain
), rsp
->fc_explain
, status
);
3722 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3723 ibmvfc_free_event(evt
);
3724 wake_up(&vhost
->work_wait_q
);
3728 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3729 * @tgt: ibmvfc target struct
3732 static void ibmvfc_tgt_query_target(struct ibmvfc_target
*tgt
)
3734 struct ibmvfc_query_tgt
*query_tgt
;
3735 struct ibmvfc_host
*vhost
= tgt
->vhost
;
3736 struct ibmvfc_event
*evt
;
3738 if (vhost
->discovery_threads
>= disc_threads
)
3741 kref_get(&tgt
->kref
);
3742 evt
= ibmvfc_get_event(vhost
);
3743 vhost
->discovery_threads
++;
3745 ibmvfc_init_event(evt
, ibmvfc_tgt_query_target_done
, IBMVFC_MAD_FORMAT
);
3746 query_tgt
= &evt
->iu
.query_tgt
;
3747 memset(query_tgt
, 0, sizeof(*query_tgt
));
3748 query_tgt
->common
.version
= 1;
3749 query_tgt
->common
.opcode
= IBMVFC_QUERY_TARGET
;
3750 query_tgt
->common
.length
= sizeof(*query_tgt
);
3751 query_tgt
->wwpn
= tgt
->ids
.port_name
;
3753 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_INIT_WAIT
);
3754 if (ibmvfc_send_event(evt
, vhost
, default_timeout
)) {
3755 vhost
->discovery_threads
--;
3756 ibmvfc_set_tgt_action(tgt
, IBMVFC_TGT_ACTION_NONE
);
3757 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
3759 tgt_dbg(tgt
, "Sent Query Target\n");
3763 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3764 * @vhost: ibmvfc host struct
3765 * @scsi_id: SCSI ID to allocate target for
3768 * 0 on success / other on failure
3770 static int ibmvfc_alloc_target(struct ibmvfc_host
*vhost
, u64 scsi_id
)
3772 struct ibmvfc_target
*tgt
;
3773 unsigned long flags
;
3775 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3776 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
3777 if (tgt
->scsi_id
== scsi_id
) {
3778 if (tgt
->need_login
)
3779 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3783 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3785 tgt
= mempool_alloc(vhost
->tgt_pool
, GFP_NOIO
);
3787 dev_err(vhost
->dev
, "Target allocation failure for scsi id %08llx\n",
3792 memset(tgt
, 0, sizeof(*tgt
));
3793 tgt
->scsi_id
= scsi_id
;
3794 tgt
->new_scsi_id
= scsi_id
;
3796 tgt
->need_login
= 1;
3797 tgt
->cancel_key
= vhost
->task_set
++;
3798 init_timer(&tgt
->timer
);
3799 kref_init(&tgt
->kref
);
3800 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_implicit_logout
);
3801 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
3802 list_add_tail(&tgt
->queue
, &vhost
->targets
);
3805 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
3810 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3811 * @vhost: ibmvfc host struct
3814 * 0 on success / other on failure
3816 static int ibmvfc_alloc_targets(struct ibmvfc_host
*vhost
)
3820 for (i
= 0, rc
= 0; !rc
&& i
< vhost
->num_targets
; i
++)
3821 rc
= ibmvfc_alloc_target(vhost
,
3822 vhost
->disc_buf
->scsi_id
[i
] & IBMVFC_DISC_TGT_SCSI_ID_MASK
);
3828 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3829 * @evt: ibmvfc event struct
3832 static void ibmvfc_discover_targets_done(struct ibmvfc_event
*evt
)
3834 struct ibmvfc_host
*vhost
= evt
->vhost
;
3835 struct ibmvfc_discover_targets
*rsp
= &evt
->xfer_iu
->discover_targets
;
3836 u32 mad_status
= rsp
->common
.status
;
3837 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3839 switch (mad_status
) {
3840 case IBMVFC_MAD_SUCCESS
:
3841 ibmvfc_dbg(vhost
, "Discover Targets succeeded\n");
3842 vhost
->num_targets
= rsp
->num_written
;
3843 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_ALLOC_TGTS
);
3845 case IBMVFC_MAD_FAILED
:
3846 level
+= ibmvfc_retry_host_init(vhost
);
3847 ibmvfc_log(vhost
, level
, "Discover Targets failed: %s (%x:%x)\n",
3848 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
);
3850 case IBMVFC_MAD_DRIVER_FAILED
:
3853 dev_err(vhost
->dev
, "Invalid Discover Targets response: 0x%x\n", mad_status
);
3854 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3858 ibmvfc_free_event(evt
);
3859 wake_up(&vhost
->work_wait_q
);
3863 * ibmvfc_discover_targets - Send Discover Targets MAD
3864 * @vhost: ibmvfc host struct
3867 static void ibmvfc_discover_targets(struct ibmvfc_host
*vhost
)
3869 struct ibmvfc_discover_targets
*mad
;
3870 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
3872 ibmvfc_init_event(evt
, ibmvfc_discover_targets_done
, IBMVFC_MAD_FORMAT
);
3873 mad
= &evt
->iu
.discover_targets
;
3874 memset(mad
, 0, sizeof(*mad
));
3875 mad
->common
.version
= 1;
3876 mad
->common
.opcode
= IBMVFC_DISC_TARGETS
;
3877 mad
->common
.length
= sizeof(*mad
);
3878 mad
->bufflen
= vhost
->disc_buf_sz
;
3879 mad
->buffer
.va
= vhost
->disc_buf_dma
;
3880 mad
->buffer
.len
= vhost
->disc_buf_sz
;
3881 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
3883 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
3884 ibmvfc_dbg(vhost
, "Sent discover targets\n");
3886 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3890 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3891 * @evt: ibmvfc event struct
3894 static void ibmvfc_npiv_login_done(struct ibmvfc_event
*evt
)
3896 struct ibmvfc_host
*vhost
= evt
->vhost
;
3897 u32 mad_status
= evt
->xfer_iu
->npiv_login
.common
.status
;
3898 struct ibmvfc_npiv_login_resp
*rsp
= &vhost
->login_buf
->resp
;
3899 unsigned int npiv_max_sectors
;
3900 int level
= IBMVFC_DEFAULT_LOG_LEVEL
;
3902 switch (mad_status
) {
3903 case IBMVFC_MAD_SUCCESS
:
3904 ibmvfc_free_event(evt
);
3906 case IBMVFC_MAD_FAILED
:
3907 if (ibmvfc_retry_cmd(rsp
->status
, rsp
->error
))
3908 level
+= ibmvfc_retry_host_init(vhost
);
3910 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3911 ibmvfc_log(vhost
, level
, "NPIV Login failed: %s (%x:%x)\n",
3912 ibmvfc_get_cmd_error(rsp
->status
, rsp
->error
), rsp
->status
, rsp
->error
);
3913 ibmvfc_free_event(evt
);
3915 case IBMVFC_MAD_CRQ_ERROR
:
3916 ibmvfc_retry_host_init(vhost
);
3917 case IBMVFC_MAD_DRIVER_FAILED
:
3918 ibmvfc_free_event(evt
);
3921 dev_err(vhost
->dev
, "Invalid NPIV Login response: 0x%x\n", mad_status
);
3922 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3923 ibmvfc_free_event(evt
);
3927 vhost
->client_migrated
= 0;
3929 if (!(rsp
->flags
& IBMVFC_NATIVE_FC
)) {
3930 dev_err(vhost
->dev
, "Virtual adapter does not support FC. %x\n",
3932 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3933 wake_up(&vhost
->work_wait_q
);
3937 if (rsp
->max_cmds
<= IBMVFC_NUM_INTERNAL_REQ
) {
3938 dev_err(vhost
->dev
, "Virtual adapter supported queue depth too small: %d\n",
3940 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
3941 wake_up(&vhost
->work_wait_q
);
3945 vhost
->logged_in
= 1;
3946 npiv_max_sectors
= min((uint
)(rsp
->max_dma_len
>> 9), IBMVFC_MAX_SECTORS
);
3947 dev_info(vhost
->dev
, "Host partition: %s, device: %s %s %s max sectors %u\n",
3948 rsp
->partition_name
, rsp
->device_name
, rsp
->port_loc_code
,
3949 rsp
->drc_name
, npiv_max_sectors
);
3951 fc_host_fabric_name(vhost
->host
) = rsp
->node_name
;
3952 fc_host_node_name(vhost
->host
) = rsp
->node_name
;
3953 fc_host_port_name(vhost
->host
) = rsp
->port_name
;
3954 fc_host_port_id(vhost
->host
) = rsp
->scsi_id
;
3955 fc_host_port_type(vhost
->host
) = FC_PORTTYPE_NPIV
;
3956 fc_host_supported_classes(vhost
->host
) = 0;
3957 if (rsp
->service_parms
.class1_parms
[0] & 0x80000000)
3958 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS1
;
3959 if (rsp
->service_parms
.class2_parms
[0] & 0x80000000)
3960 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS2
;
3961 if (rsp
->service_parms
.class3_parms
[0] & 0x80000000)
3962 fc_host_supported_classes(vhost
->host
) |= FC_COS_CLASS3
;
3963 fc_host_maxframe_size(vhost
->host
) =
3964 rsp
->service_parms
.common
.bb_rcv_sz
& 0x0fff;
3966 vhost
->host
->can_queue
= rsp
->max_cmds
- IBMVFC_NUM_INTERNAL_REQ
;
3967 vhost
->host
->max_sectors
= npiv_max_sectors
;
3968 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
3969 wake_up(&vhost
->work_wait_q
);
3973 * ibmvfc_npiv_login - Sends NPIV login
3974 * @vhost: ibmvfc host struct
3977 static void ibmvfc_npiv_login(struct ibmvfc_host
*vhost
)
3979 struct ibmvfc_npiv_login_mad
*mad
;
3980 struct ibmvfc_event
*evt
= ibmvfc_get_event(vhost
);
3982 ibmvfc_gather_partition_info(vhost
);
3983 ibmvfc_set_login_info(vhost
);
3984 ibmvfc_init_event(evt
, ibmvfc_npiv_login_done
, IBMVFC_MAD_FORMAT
);
3986 memcpy(vhost
->login_buf
, &vhost
->login_info
, sizeof(vhost
->login_info
));
3987 mad
= &evt
->iu
.npiv_login
;
3988 memset(mad
, 0, sizeof(struct ibmvfc_npiv_login_mad
));
3989 mad
->common
.version
= 1;
3990 mad
->common
.opcode
= IBMVFC_NPIV_LOGIN
;
3991 mad
->common
.length
= sizeof(struct ibmvfc_npiv_login_mad
);
3992 mad
->buffer
.va
= vhost
->login_buf_dma
;
3993 mad
->buffer
.len
= sizeof(*vhost
->login_buf
);
3995 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT_WAIT
);
3997 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
3998 ibmvfc_dbg(vhost
, "Sent NPIV login\n");
4000 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4004 * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
4005 * @vhost: ibmvfc host struct
4008 static void ibmvfc_npiv_logout_done(struct ibmvfc_event
*evt
)
4010 struct ibmvfc_host
*vhost
= evt
->vhost
;
4011 u32 mad_status
= evt
->xfer_iu
->npiv_logout
.common
.status
;
4013 ibmvfc_free_event(evt
);
4015 switch (mad_status
) {
4016 case IBMVFC_MAD_SUCCESS
:
4017 if (list_empty(&vhost
->sent
) &&
4018 vhost
->action
== IBMVFC_HOST_ACTION_LOGO_WAIT
) {
4019 ibmvfc_init_host(vhost
);
4023 case IBMVFC_MAD_FAILED
:
4024 case IBMVFC_MAD_NOT_SUPPORTED
:
4025 case IBMVFC_MAD_CRQ_ERROR
:
4026 case IBMVFC_MAD_DRIVER_FAILED
:
4028 ibmvfc_dbg(vhost
, "NPIV Logout failed. 0x%X\n", mad_status
);
4032 ibmvfc_hard_reset_host(vhost
);
4036 * ibmvfc_npiv_logout - Issue an NPIV Logout
4037 * @vhost: ibmvfc host struct
4040 static void ibmvfc_npiv_logout(struct ibmvfc_host
*vhost
)
4042 struct ibmvfc_npiv_logout_mad
*mad
;
4043 struct ibmvfc_event
*evt
;
4045 evt
= ibmvfc_get_event(vhost
);
4046 ibmvfc_init_event(evt
, ibmvfc_npiv_logout_done
, IBMVFC_MAD_FORMAT
);
4048 mad
= &evt
->iu
.npiv_logout
;
4049 memset(mad
, 0, sizeof(*mad
));
4050 mad
->common
.version
= 1;
4051 mad
->common
.opcode
= IBMVFC_NPIV_LOGOUT
;
4052 mad
->common
.length
= sizeof(struct ibmvfc_npiv_logout_mad
);
4054 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_LOGO_WAIT
);
4056 if (!ibmvfc_send_event(evt
, vhost
, default_timeout
))
4057 ibmvfc_dbg(vhost
, "Sent NPIV logout\n");
4059 ibmvfc_link_down(vhost
, IBMVFC_LINK_DEAD
);
4063 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
4064 * @vhost: ibmvfc host struct
4067 * 1 if work to do / 0 if not
4069 static int ibmvfc_dev_init_to_do(struct ibmvfc_host
*vhost
)
4071 struct ibmvfc_target
*tgt
;
4073 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4074 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
||
4075 tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
4083 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
4084 * @vhost: ibmvfc host struct
4087 * 1 if work to do / 0 if not
4089 static int __ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
4091 struct ibmvfc_target
*tgt
;
4093 if (kthread_should_stop())
4095 switch (vhost
->action
) {
4096 case IBMVFC_HOST_ACTION_NONE
:
4097 case IBMVFC_HOST_ACTION_INIT_WAIT
:
4098 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
4100 case IBMVFC_HOST_ACTION_TGT_INIT
:
4101 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
4102 if (vhost
->discovery_threads
== disc_threads
)
4104 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4105 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
)
4107 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4108 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT_WAIT
)
4111 case IBMVFC_HOST_ACTION_LOGO
:
4112 case IBMVFC_HOST_ACTION_INIT
:
4113 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
4114 case IBMVFC_HOST_ACTION_TGT_DEL
:
4115 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
4116 case IBMVFC_HOST_ACTION_QUERY
:
4125 * ibmvfc_work_to_do - Is there task level work to do?
4126 * @vhost: ibmvfc host struct
4129 * 1 if work to do / 0 if not
4131 static int ibmvfc_work_to_do(struct ibmvfc_host
*vhost
)
4133 unsigned long flags
;
4136 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4137 rc
= __ibmvfc_work_to_do(vhost
);
4138 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4143 * ibmvfc_log_ae - Log async events if necessary
4144 * @vhost: ibmvfc host struct
4145 * @events: events to log
4148 static void ibmvfc_log_ae(struct ibmvfc_host
*vhost
, int events
)
4150 if (events
& IBMVFC_AE_RSCN
)
4151 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_RSCN
, 0);
4152 if ((events
& IBMVFC_AE_LINKDOWN
) &&
4153 vhost
->state
>= IBMVFC_HALTED
)
4154 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKDOWN
, 0);
4155 if ((events
& IBMVFC_AE_LINKUP
) &&
4156 vhost
->state
== IBMVFC_INITIALIZING
)
4157 fc_host_post_event(vhost
->host
, fc_get_event_number(), FCH_EVT_LINKUP
, 0);
4161 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
4162 * @tgt: ibmvfc target struct
4165 static void ibmvfc_tgt_add_rport(struct ibmvfc_target
*tgt
)
4167 struct ibmvfc_host
*vhost
= tgt
->vhost
;
4168 struct fc_rport
*rport
;
4169 unsigned long flags
;
4171 tgt_dbg(tgt
, "Adding rport\n");
4172 rport
= fc_remote_port_add(vhost
->host
, 0, &tgt
->ids
);
4173 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4175 if (rport
&& tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
4176 tgt_dbg(tgt
, "Deleting rport\n");
4177 list_del(&tgt
->queue
);
4178 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4179 fc_remote_port_delete(rport
);
4180 del_timer_sync(&tgt
->timer
);
4181 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4186 tgt_dbg(tgt
, "rport add succeeded\n");
4188 rport
->maxframe_size
= tgt
->service_parms
.common
.bb_rcv_sz
& 0x0fff;
4189 rport
->supported_classes
= 0;
4190 tgt
->target_id
= rport
->scsi_target_id
;
4191 if (tgt
->service_parms
.class1_parms
[0] & 0x80000000)
4192 rport
->supported_classes
|= FC_COS_CLASS1
;
4193 if (tgt
->service_parms
.class2_parms
[0] & 0x80000000)
4194 rport
->supported_classes
|= FC_COS_CLASS2
;
4195 if (tgt
->service_parms
.class3_parms
[0] & 0x80000000)
4196 rport
->supported_classes
|= FC_COS_CLASS3
;
4198 blk_queue_max_segments(rport
->rqst_q
, 1);
4200 tgt_dbg(tgt
, "rport add failed\n");
4201 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4205 * ibmvfc_do_work - Do task level work
4206 * @vhost: ibmvfc host struct
4209 static void ibmvfc_do_work(struct ibmvfc_host
*vhost
)
4211 struct ibmvfc_target
*tgt
;
4212 unsigned long flags
;
4213 struct fc_rport
*rport
;
4215 ibmvfc_log_ae(vhost
, vhost
->events_to_log
);
4216 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4217 vhost
->events_to_log
= 0;
4218 switch (vhost
->action
) {
4219 case IBMVFC_HOST_ACTION_NONE
:
4220 case IBMVFC_HOST_ACTION_LOGO_WAIT
:
4221 case IBMVFC_HOST_ACTION_INIT_WAIT
:
4223 case IBMVFC_HOST_ACTION_LOGO
:
4224 vhost
->job_step(vhost
);
4226 case IBMVFC_HOST_ACTION_INIT
:
4227 BUG_ON(vhost
->state
!= IBMVFC_INITIALIZING
);
4228 if (vhost
->delay_init
) {
4229 vhost
->delay_init
= 0;
4230 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4234 vhost
->job_step(vhost
);
4236 case IBMVFC_HOST_ACTION_QUERY
:
4237 list_for_each_entry(tgt
, &vhost
->targets
, queue
)
4238 ibmvfc_init_tgt(tgt
, ibmvfc_tgt_query_target
);
4239 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY_TGTS
);
4241 case IBMVFC_HOST_ACTION_QUERY_TGTS
:
4242 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4243 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
4249 if (!ibmvfc_dev_init_to_do(vhost
))
4250 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL
);
4252 case IBMVFC_HOST_ACTION_TGT_DEL
:
4253 case IBMVFC_HOST_ACTION_TGT_DEL_FAILED
:
4254 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4255 if (tgt
->action
== IBMVFC_TGT_ACTION_DEL_RPORT
) {
4256 tgt_dbg(tgt
, "Deleting rport\n");
4259 list_del(&tgt
->queue
);
4260 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4262 fc_remote_port_delete(rport
);
4263 del_timer_sync(&tgt
->timer
);
4264 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4269 if (vhost
->state
== IBMVFC_INITIALIZING
) {
4270 if (vhost
->action
== IBMVFC_HOST_ACTION_TGT_DEL_FAILED
) {
4271 if (vhost
->reinit
) {
4273 scsi_block_requests(vhost
->host
);
4274 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_QUERY
);
4275 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4277 ibmvfc_set_host_state(vhost
, IBMVFC_ACTIVE
);
4278 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
4279 wake_up(&vhost
->init_wait_q
);
4280 schedule_work(&vhost
->rport_add_work_q
);
4281 vhost
->init_retries
= 0;
4282 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4283 scsi_unblock_requests(vhost
->host
);
4288 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_INIT
);
4289 vhost
->job_step
= ibmvfc_discover_targets
;
4292 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_NONE
);
4293 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4294 scsi_unblock_requests(vhost
->host
);
4295 wake_up(&vhost
->init_wait_q
);
4299 case IBMVFC_HOST_ACTION_ALLOC_TGTS
:
4300 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_INIT
);
4301 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4302 ibmvfc_alloc_targets(vhost
);
4303 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4305 case IBMVFC_HOST_ACTION_TGT_INIT
:
4306 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4307 if (tgt
->action
== IBMVFC_TGT_ACTION_INIT
) {
4313 if (!ibmvfc_dev_init_to_do(vhost
))
4314 ibmvfc_set_host_action(vhost
, IBMVFC_HOST_ACTION_TGT_DEL_FAILED
);
4320 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4324 * ibmvfc_work - Do task level work
4325 * @data: ibmvfc host struct
4330 static int ibmvfc_work(void *data
)
4332 struct ibmvfc_host
*vhost
= data
;
4335 set_user_nice(current
, -20);
4338 rc
= wait_event_interruptible(vhost
->work_wait_q
,
4339 ibmvfc_work_to_do(vhost
));
4343 if (kthread_should_stop())
4346 ibmvfc_do_work(vhost
);
4349 ibmvfc_dbg(vhost
, "ibmvfc kthread exiting...\n");
4354 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
4355 * @vhost: ibmvfc host struct
4357 * Allocates a page for messages, maps it for dma, and registers
4358 * the crq with the hypervisor.
4361 * zero on success / other on failure
4363 static int ibmvfc_init_crq(struct ibmvfc_host
*vhost
)
4365 int rc
, retrc
= -ENOMEM
;
4366 struct device
*dev
= vhost
->dev
;
4367 struct vio_dev
*vdev
= to_vio_dev(dev
);
4368 struct ibmvfc_crq_queue
*crq
= &vhost
->crq
;
4371 crq
->msgs
= (struct ibmvfc_crq
*)get_zeroed_page(GFP_KERNEL
);
4376 crq
->size
= PAGE_SIZE
/ sizeof(*crq
->msgs
);
4377 crq
->msg_token
= dma_map_single(dev
, crq
->msgs
,
4378 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
4380 if (dma_mapping_error(dev
, crq
->msg_token
))
4383 retrc
= rc
= plpar_hcall_norets(H_REG_CRQ
, vdev
->unit_address
,
4384 crq
->msg_token
, PAGE_SIZE
);
4386 if (rc
== H_RESOURCE
)
4387 /* maybe kexecing and resource is busy. try a reset */
4388 retrc
= rc
= ibmvfc_reset_crq(vhost
);
4391 dev_warn(dev
, "Partner adapter not ready\n");
4393 dev_warn(dev
, "Error %d opening adapter\n", rc
);
4394 goto reg_crq_failed
;
4399 tasklet_init(&vhost
->tasklet
, (void *)ibmvfc_tasklet
, (unsigned long)vhost
);
4401 if ((rc
= request_irq(vdev
->irq
, ibmvfc_interrupt
, 0, IBMVFC_NAME
, vhost
))) {
4402 dev_err(dev
, "Couldn't register irq 0x%x. rc=%d\n", vdev
->irq
, rc
);
4403 goto req_irq_failed
;
4406 if ((rc
= vio_enable_interrupts(vdev
))) {
4407 dev_err(dev
, "Error %d enabling interrupts\n", rc
);
4408 goto req_irq_failed
;
4416 tasklet_kill(&vhost
->tasklet
);
4418 rc
= plpar_hcall_norets(H_FREE_CRQ
, vdev
->unit_address
);
4419 } while (rc
== H_BUSY
|| H_IS_LONG_BUSY(rc
));
4421 dma_unmap_single(dev
, crq
->msg_token
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
4423 free_page((unsigned long)crq
->msgs
);
4428 * ibmvfc_free_mem - Free memory for vhost
4429 * @vhost: ibmvfc host struct
4434 static void ibmvfc_free_mem(struct ibmvfc_host
*vhost
)
4436 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
4439 mempool_destroy(vhost
->tgt_pool
);
4440 kfree(vhost
->trace
);
4441 dma_free_coherent(vhost
->dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
4442 vhost
->disc_buf_dma
);
4443 dma_free_coherent(vhost
->dev
, sizeof(*vhost
->login_buf
),
4444 vhost
->login_buf
, vhost
->login_buf_dma
);
4445 dma_pool_destroy(vhost
->sg_pool
);
4446 dma_unmap_single(vhost
->dev
, async_q
->msg_token
,
4447 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
4448 free_page((unsigned long)async_q
->msgs
);
4453 * ibmvfc_alloc_mem - Allocate memory for vhost
4454 * @vhost: ibmvfc host struct
4457 * 0 on success / non-zero on failure
4459 static int ibmvfc_alloc_mem(struct ibmvfc_host
*vhost
)
4461 struct ibmvfc_async_crq_queue
*async_q
= &vhost
->async_crq
;
4462 struct device
*dev
= vhost
->dev
;
4465 async_q
->msgs
= (struct ibmvfc_async_crq
*)get_zeroed_page(GFP_KERNEL
);
4466 if (!async_q
->msgs
) {
4467 dev_err(dev
, "Couldn't allocate async queue.\n");
4471 async_q
->size
= PAGE_SIZE
/ sizeof(struct ibmvfc_async_crq
);
4472 async_q
->msg_token
= dma_map_single(dev
, async_q
->msgs
,
4473 async_q
->size
* sizeof(*async_q
->msgs
),
4476 if (dma_mapping_error(dev
, async_q
->msg_token
)) {
4477 dev_err(dev
, "Failed to map async queue\n");
4478 goto free_async_crq
;
4481 vhost
->sg_pool
= dma_pool_create(IBMVFC_NAME
, dev
,
4482 SG_ALL
* sizeof(struct srp_direct_buf
),
4483 sizeof(struct srp_direct_buf
), 0);
4485 if (!vhost
->sg_pool
) {
4486 dev_err(dev
, "Failed to allocate sg pool\n");
4487 goto unmap_async_crq
;
4490 vhost
->login_buf
= dma_alloc_coherent(dev
, sizeof(*vhost
->login_buf
),
4491 &vhost
->login_buf_dma
, GFP_KERNEL
);
4493 if (!vhost
->login_buf
) {
4494 dev_err(dev
, "Couldn't allocate NPIV login buffer\n");
4498 vhost
->disc_buf_sz
= sizeof(vhost
->disc_buf
->scsi_id
[0]) * max_targets
;
4499 vhost
->disc_buf
= dma_alloc_coherent(dev
, vhost
->disc_buf_sz
,
4500 &vhost
->disc_buf_dma
, GFP_KERNEL
);
4502 if (!vhost
->disc_buf
) {
4503 dev_err(dev
, "Couldn't allocate Discover Targets buffer\n");
4504 goto free_login_buffer
;
4507 vhost
->trace
= kcalloc(IBMVFC_NUM_TRACE_ENTRIES
,
4508 sizeof(struct ibmvfc_trace_entry
), GFP_KERNEL
);
4511 goto free_disc_buffer
;
4513 vhost
->tgt_pool
= mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ
,
4514 sizeof(struct ibmvfc_target
));
4516 if (!vhost
->tgt_pool
) {
4517 dev_err(dev
, "Couldn't allocate target memory pool\n");
4525 kfree(vhost
->trace
);
4527 dma_free_coherent(dev
, vhost
->disc_buf_sz
, vhost
->disc_buf
,
4528 vhost
->disc_buf_dma
);
4530 dma_free_coherent(dev
, sizeof(*vhost
->login_buf
),
4531 vhost
->login_buf
, vhost
->login_buf_dma
);
4533 dma_pool_destroy(vhost
->sg_pool
);
4535 dma_unmap_single(dev
, async_q
->msg_token
,
4536 async_q
->size
* sizeof(*async_q
->msgs
), DMA_BIDIRECTIONAL
);
4538 free_page((unsigned long)async_q
->msgs
);
4545 * ibmvfc_rport_add_thread - Worker thread for rport adds
4546 * @work: work struct
4549 static void ibmvfc_rport_add_thread(struct work_struct
*work
)
4551 struct ibmvfc_host
*vhost
= container_of(work
, struct ibmvfc_host
,
4553 struct ibmvfc_target
*tgt
;
4554 struct fc_rport
*rport
;
4555 unsigned long flags
;
4559 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4562 if (vhost
->state
!= IBMVFC_ACTIVE
)
4565 list_for_each_entry(tgt
, &vhost
->targets
, queue
) {
4566 if (tgt
->add_rport
) {
4569 kref_get(&tgt
->kref
);
4572 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4573 ibmvfc_tgt_add_rport(tgt
);
4574 } else if (get_device(&rport
->dev
)) {
4575 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4576 tgt_dbg(tgt
, "Setting rport roles\n");
4577 fc_remote_port_rolechg(rport
, tgt
->ids
.roles
);
4578 put_device(&rport
->dev
);
4581 kref_put(&tgt
->kref
, ibmvfc_release_tgt
);
4582 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4588 if (vhost
->state
== IBMVFC_ACTIVE
)
4589 vhost
->scan_complete
= 1;
4590 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4595 * ibmvfc_probe - Adapter hot plug add entry point
4596 * @vdev: vio device struct
4597 * @id: vio device id struct
4600 * 0 on success / non-zero on failure
4602 static int ibmvfc_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
4604 struct ibmvfc_host
*vhost
;
4605 struct Scsi_Host
*shost
;
4606 struct device
*dev
= &vdev
->dev
;
4610 shost
= scsi_host_alloc(&driver_template
, sizeof(*vhost
));
4612 dev_err(dev
, "Couldn't allocate host data\n");
4616 shost
->transportt
= ibmvfc_transport_template
;
4617 shost
->can_queue
= max_requests
;
4618 shost
->max_lun
= max_lun
;
4619 shost
->max_id
= max_targets
;
4620 shost
->max_sectors
= IBMVFC_MAX_SECTORS
;
4621 shost
->max_cmd_len
= IBMVFC_MAX_CDB_LEN
;
4622 shost
->unique_id
= shost
->host_no
;
4624 vhost
= shost_priv(shost
);
4625 INIT_LIST_HEAD(&vhost
->sent
);
4626 INIT_LIST_HEAD(&vhost
->free
);
4627 INIT_LIST_HEAD(&vhost
->targets
);
4628 sprintf(vhost
->name
, IBMVFC_NAME
);
4629 vhost
->host
= shost
;
4631 vhost
->partition_number
= -1;
4632 vhost
->log_level
= log_level
;
4633 vhost
->task_set
= 1;
4634 strcpy(vhost
->partition_name
, "UNKNOWN");
4635 init_waitqueue_head(&vhost
->work_wait_q
);
4636 init_waitqueue_head(&vhost
->init_wait_q
);
4637 INIT_WORK(&vhost
->rport_add_work_q
, ibmvfc_rport_add_thread
);
4638 mutex_init(&vhost
->passthru_mutex
);
4640 if ((rc
= ibmvfc_alloc_mem(vhost
)))
4641 goto free_scsi_host
;
4643 vhost
->work_thread
= kthread_run(ibmvfc_work
, vhost
, "%s_%d", IBMVFC_NAME
,
4646 if (IS_ERR(vhost
->work_thread
)) {
4647 dev_err(dev
, "Couldn't create kernel thread: %ld\n",
4648 PTR_ERR(vhost
->work_thread
));
4652 if ((rc
= ibmvfc_init_crq(vhost
))) {
4653 dev_err(dev
, "Couldn't initialize crq. rc=%d\n", rc
);
4657 if ((rc
= ibmvfc_init_event_pool(vhost
))) {
4658 dev_err(dev
, "Couldn't initialize event pool. rc=%d\n", rc
);
4662 if ((rc
= scsi_add_host(shost
, dev
)))
4663 goto release_event_pool
;
4665 if ((rc
= ibmvfc_create_trace_file(&shost
->shost_dev
.kobj
,
4666 &ibmvfc_trace_attr
))) {
4667 dev_err(dev
, "Failed to create trace file. rc=%d\n", rc
);
4671 if (shost_to_fc_host(shost
)->rqst_q
)
4672 blk_queue_max_segments(shost_to_fc_host(shost
)->rqst_q
, 1);
4673 dev_set_drvdata(dev
, vhost
);
4674 spin_lock(&ibmvfc_driver_lock
);
4675 list_add_tail(&vhost
->queue
, &ibmvfc_head
);
4676 spin_unlock(&ibmvfc_driver_lock
);
4678 ibmvfc_send_crq_init(vhost
);
4679 scsi_scan_host(shost
);
4683 scsi_remove_host(shost
);
4685 ibmvfc_free_event_pool(vhost
);
4687 ibmvfc_release_crq_queue(vhost
);
4689 kthread_stop(vhost
->work_thread
);
4691 ibmvfc_free_mem(vhost
);
4693 scsi_host_put(shost
);
4700 * ibmvfc_remove - Adapter hot plug remove entry point
4701 * @vdev: vio device struct
4706 static int ibmvfc_remove(struct vio_dev
*vdev
)
4708 struct ibmvfc_host
*vhost
= dev_get_drvdata(&vdev
->dev
);
4709 unsigned long flags
;
4712 ibmvfc_remove_trace_file(&vhost
->host
->shost_dev
.kobj
, &ibmvfc_trace_attr
);
4714 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4715 ibmvfc_link_down(vhost
, IBMVFC_HOST_OFFLINE
);
4716 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4718 ibmvfc_wait_while_resetting(vhost
);
4719 ibmvfc_release_crq_queue(vhost
);
4720 kthread_stop(vhost
->work_thread
);
4721 fc_remove_host(vhost
->host
);
4722 scsi_remove_host(vhost
->host
);
4724 spin_lock_irqsave(vhost
->host
->host_lock
, flags
);
4725 ibmvfc_purge_requests(vhost
, DID_ERROR
);
4726 ibmvfc_free_event_pool(vhost
);
4727 spin_unlock_irqrestore(vhost
->host
->host_lock
, flags
);
4729 ibmvfc_free_mem(vhost
);
4730 spin_lock(&ibmvfc_driver_lock
);
4731 list_del(&vhost
->queue
);
4732 spin_unlock(&ibmvfc_driver_lock
);
4733 scsi_host_put(vhost
->host
);
4739 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
4740 * @vdev: vio device struct
4743 * Number of bytes the driver will need to DMA map at the same time in
4744 * order to perform well.
4746 static unsigned long ibmvfc_get_desired_dma(struct vio_dev
*vdev
)
4748 unsigned long pool_dma
= max_requests
* sizeof(union ibmvfc_iu
);
4749 return pool_dma
+ ((512 * 1024) * driver_template
.cmd_per_lun
);
4752 static struct vio_device_id ibmvfc_device_table
[] __devinitdata
= {
4753 {"fcp", "IBM,vfc-client"},
4756 MODULE_DEVICE_TABLE(vio
, ibmvfc_device_table
);
4758 static struct vio_driver ibmvfc_driver
= {
4759 .id_table
= ibmvfc_device_table
,
4760 .probe
= ibmvfc_probe
,
4761 .remove
= ibmvfc_remove
,
4762 .get_desired_dma
= ibmvfc_get_desired_dma
,
4764 .name
= IBMVFC_NAME
,
4765 .owner
= THIS_MODULE
,
4769 static struct fc_function_template ibmvfc_transport_functions
= {
4770 .show_host_fabric_name
= 1,
4771 .show_host_node_name
= 1,
4772 .show_host_port_name
= 1,
4773 .show_host_supported_classes
= 1,
4774 .show_host_port_type
= 1,
4775 .show_host_port_id
= 1,
4776 .show_host_maxframe_size
= 1,
4778 .get_host_port_state
= ibmvfc_get_host_port_state
,
4779 .show_host_port_state
= 1,
4781 .get_host_speed
= ibmvfc_get_host_speed
,
4782 .show_host_speed
= 1,
4784 .issue_fc_host_lip
= ibmvfc_issue_fc_host_lip
,
4785 .terminate_rport_io
= ibmvfc_terminate_rport_io
,
4787 .show_rport_maxframe_size
= 1,
4788 .show_rport_supported_classes
= 1,
4790 .set_rport_dev_loss_tmo
= ibmvfc_set_rport_dev_loss_tmo
,
4791 .show_rport_dev_loss_tmo
= 1,
4793 .get_starget_node_name
= ibmvfc_get_starget_node_name
,
4794 .show_starget_node_name
= 1,
4796 .get_starget_port_name
= ibmvfc_get_starget_port_name
,
4797 .show_starget_port_name
= 1,
4799 .get_starget_port_id
= ibmvfc_get_starget_port_id
,
4800 .show_starget_port_id
= 1,
4802 .bsg_request
= ibmvfc_bsg_request
,
4803 .bsg_timeout
= ibmvfc_bsg_timeout
,
4807 * ibmvfc_module_init - Initialize the ibmvfc module
4810 * 0 on success / other on failure
4812 static int __init
ibmvfc_module_init(void)
4816 if (!firmware_has_feature(FW_FEATURE_VIO
))
4819 printk(KERN_INFO IBMVFC_NAME
": IBM Virtual Fibre Channel Driver version: %s %s\n",
4820 IBMVFC_DRIVER_VERSION
, IBMVFC_DRIVER_DATE
);
4822 ibmvfc_transport_template
= fc_attach_transport(&ibmvfc_transport_functions
);
4823 if (!ibmvfc_transport_template
)
4826 rc
= vio_register_driver(&ibmvfc_driver
);
4828 fc_release_transport(ibmvfc_transport_template
);
4833 * ibmvfc_module_exit - Teardown the ibmvfc module
4838 static void __exit
ibmvfc_module_exit(void)
4840 vio_unregister_driver(&ibmvfc_driver
);
4841 fc_release_transport(ibmvfc_transport_template
);
4844 module_init(ibmvfc_module_init
);
4845 module_exit(ibmvfc_module_exit
);