2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 * bfad_im.c Linux driver IM module.
24 #include "bfad_trcmod.h"
25 #include "bfa_cb_ioim_macros.h"
26 #include <fcb/bfa_fcb_fcpim.h>
28 BFA_TRC_FILE(LDRV
, IM
);
30 DEFINE_IDR(bfad_im_port_index
);
31 struct scsi_transport_template
*bfad_im_scsi_transport_template
;
32 static void bfad_im_itnim_work_handler(struct work_struct
*work
);
33 static int bfad_im_queuecommand(struct scsi_cmnd
*cmnd
,
34 void (*done
)(struct scsi_cmnd
*));
35 static int bfad_im_slave_alloc(struct scsi_device
*sdev
);
38 bfa_cb_ioim_done(void *drv
, struct bfad_ioim_s
*dio
,
39 enum bfi_ioim_status io_status
, u8 scsi_status
,
40 int sns_len
, u8
*sns_info
, s32 residue
)
42 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
43 struct bfad_s
*bfad
= drv
;
44 struct bfad_itnim_data_s
*itnim_data
;
45 struct bfad_itnim_s
*itnim
;
49 bfa_trc(bfad
, scsi_status
);
50 cmnd
->result
= ScsiResult(DID_OK
, scsi_status
);
51 scsi_set_resid(cmnd
, 0);
54 bfa_trc(bfad
, sns_len
);
55 if (sns_len
> SCSI_SENSE_BUFFERSIZE
)
56 sns_len
= SCSI_SENSE_BUFFERSIZE
;
57 memcpy(cmnd
->sense_buffer
, sns_info
, sns_len
);
60 scsi_set_resid(cmnd
, residue
);
63 case BFI_IOIM_STS_ABORTED
:
64 case BFI_IOIM_STS_TIMEDOUT
:
65 case BFI_IOIM_STS_PATHTOV
:
67 cmnd
->result
= ScsiResult(DID_ERROR
, 0);
70 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
71 if (cmnd
->device
->host
!= NULL
)
74 cmnd
->host_scribble
= NULL
;
75 bfa_trc(bfad
, cmnd
->result
);
77 itnim_data
= cmnd
->device
->hostdata
;
79 itnim
= itnim_data
->itnim
;
80 if (!cmnd
->result
&& itnim
&&
81 (bfa_lun_queue_depth
> cmnd
->device
->queue_depth
)) {
82 /* Queue depth adjustment for good status completion */
83 bfad_os_ramp_up_qdepth(itnim
, cmnd
->device
);
84 } else if (cmnd
->result
== SAM_STAT_TASK_SET_FULL
&& itnim
) {
86 bfad_os_handle_qfull(itnim
, cmnd
->device
);
90 cmnd
->scsi_done(cmnd
);
94 bfa_cb_ioim_good_comp(void *drv
, struct bfad_ioim_s
*dio
)
96 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
97 struct bfad_itnim_data_s
*itnim_data
;
98 struct bfad_itnim_s
*itnim
;
100 cmnd
->result
= ScsiResult(DID_OK
, SCSI_STATUS_GOOD
);
102 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
103 if (cmnd
->device
->host
!= NULL
)
104 scsi_dma_unmap(cmnd
);
106 cmnd
->host_scribble
= NULL
;
108 /* Queue depth adjustment */
109 if (bfa_lun_queue_depth
> cmnd
->device
->queue_depth
) {
110 itnim_data
= cmnd
->device
->hostdata
;
112 itnim
= itnim_data
->itnim
;
114 bfad_os_ramp_up_qdepth(itnim
, cmnd
->device
);
118 cmnd
->scsi_done(cmnd
);
122 bfa_cb_ioim_abort(void *drv
, struct bfad_ioim_s
*dio
)
124 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
125 struct bfad_s
*bfad
= drv
;
127 cmnd
->result
= ScsiResult(DID_ERROR
, 0);
129 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
130 if (cmnd
->device
->host
!= NULL
)
131 scsi_dma_unmap(cmnd
);
133 bfa_trc(bfad
, cmnd
->result
);
134 cmnd
->host_scribble
= NULL
;
138 bfa_cb_tskim_done(void *bfad
, struct bfad_tskim_s
*dtsk
,
139 enum bfi_tskim_status tsk_status
)
141 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dtsk
;
142 wait_queue_head_t
*wq
;
144 cmnd
->SCp
.Status
|= tsk_status
<< 1;
145 set_bit(IO_DONE_BIT
, (unsigned long *)&cmnd
->SCp
.Status
);
146 wq
= (wait_queue_head_t
*) cmnd
->SCp
.ptr
;
147 cmnd
->SCp
.ptr
= NULL
;
154 bfa_cb_ioim_resfree(void *drv
)
159 * Scsi_Host_template SCSI host template
162 * Scsi_Host template entry, returns BFAD PCI info.
165 bfad_im_info(struct Scsi_Host
*shost
)
167 static char bfa_buf
[256];
168 struct bfad_im_port_s
*im_port
=
169 (struct bfad_im_port_s
*) shost
->hostdata
[0];
170 struct bfa_ioc_attr_s ioc_attr
;
171 struct bfad_s
*bfad
= im_port
->bfad
;
173 memset(&ioc_attr
, 0, sizeof(ioc_attr
));
174 bfa_get_attr(&bfad
->bfa
, &ioc_attr
);
176 memset(bfa_buf
, 0, sizeof(bfa_buf
));
177 snprintf(bfa_buf
, sizeof(bfa_buf
),
178 "Brocade FC/FCOE Adapter, " "model: %s hwpath: %s driver: %s",
179 ioc_attr
.adapter_attr
.model
, bfad
->pci_name
,
180 BFAD_DRIVER_VERSION
);
185 * Scsi_Host template entry, aborts the specified SCSI command.
187 * Returns: SUCCESS or FAILED.
190 bfad_im_abort_handler(struct scsi_cmnd
*cmnd
)
192 struct Scsi_Host
*shost
= cmnd
->device
->host
;
193 struct bfad_im_port_s
*im_port
=
194 (struct bfad_im_port_s
*) shost
->hostdata
[0];
195 struct bfad_s
*bfad
= im_port
->bfad
;
196 struct bfa_ioim_s
*hal_io
;
201 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
202 hal_io
= (struct bfa_ioim_s
*) cmnd
->host_scribble
;
204 /* IO has been completed, retrun success */
208 if (hal_io
->dio
!= (struct bfad_ioim_s
*) cmnd
) {
213 bfa_trc(bfad
, hal_io
->iotag
);
214 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_ABORT
,
215 im_port
->shost
->host_no
, cmnd
, hal_io
->iotag
);
216 bfa_ioim_abort(hal_io
);
217 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
219 /* Need to wait until the command get aborted */
221 while ((struct bfa_ioim_s
*) cmnd
->host_scribble
== hal_io
) {
222 set_current_state(TASK_UNINTERRUPTIBLE
);
223 schedule_timeout(timeout
);
224 if (timeout
< 4 * HZ
)
228 cmnd
->scsi_done(cmnd
);
229 bfa_trc(bfad
, hal_io
->iotag
);
230 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_ABORT_COMP
,
231 im_port
->shost
->host_no
, cmnd
, hal_io
->iotag
);
234 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
239 bfad_im_target_reset_send(struct bfad_s
*bfad
, struct scsi_cmnd
*cmnd
,
240 struct bfad_itnim_s
*itnim
)
242 struct bfa_tskim_s
*tskim
;
243 struct bfa_itnim_s
*bfa_itnim
;
244 bfa_status_t rc
= BFA_STATUS_OK
;
246 bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim
->fcs_itnim
);
247 tskim
= bfa_tskim_alloc(&bfad
->bfa
, (struct bfad_tskim_s
*) cmnd
);
249 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
250 "target reset, fail to allocate tskim\n");
251 rc
= BFA_STATUS_FAILED
;
256 * Set host_scribble to NULL to avoid aborting a task command if
259 cmnd
->host_scribble
= NULL
;
260 cmnd
->SCp
.Status
= 0;
261 bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim
->fcs_itnim
);
262 bfa_tskim_start(tskim
, bfa_itnim
, (lun_t
)0,
263 FCP_TM_TARGET_RESET
, BFAD_TARGET_RESET_TMO
);
269 * Scsi_Host template entry, resets a LUN and abort its all commands.
271 * Returns: SUCCESS or FAILED.
275 bfad_im_reset_lun_handler(struct scsi_cmnd
*cmnd
)
277 struct Scsi_Host
*shost
= cmnd
->device
->host
;
278 struct bfad_im_port_s
*im_port
=
279 (struct bfad_im_port_s
*) shost
->hostdata
[0];
280 struct bfad_itnim_data_s
*itnim_data
= cmnd
->device
->hostdata
;
281 struct bfad_s
*bfad
= im_port
->bfad
;
282 struct bfa_tskim_s
*tskim
;
283 struct bfad_itnim_s
*itnim
;
284 struct bfa_itnim_s
*bfa_itnim
;
285 DECLARE_WAIT_QUEUE_HEAD(wq
);
288 enum bfi_tskim_status task_status
;
290 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
291 itnim
= itnim_data
->itnim
;
293 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
298 tskim
= bfa_tskim_alloc(&bfad
->bfa
, (struct bfad_tskim_s
*) cmnd
);
300 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
301 "LUN reset, fail to allocate tskim");
302 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
308 * Set host_scribble to NULL to avoid aborting a task command
311 cmnd
->host_scribble
= NULL
;
312 cmnd
->SCp
.ptr
= (char *)&wq
;
313 cmnd
->SCp
.Status
= 0;
314 bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim
->fcs_itnim
);
315 bfa_tskim_start(tskim
, bfa_itnim
,
316 bfad_int_to_lun(cmnd
->device
->lun
),
317 FCP_TM_LUN_RESET
, BFAD_LUN_RESET_TMO
);
318 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
320 wait_event(wq
, test_bit(IO_DONE_BIT
,
321 (unsigned long *)&cmnd
->SCp
.Status
));
323 task_status
= cmnd
->SCp
.Status
>> 1;
324 if (task_status
!= BFI_TSKIM_STS_OK
) {
325 BFA_DEV_PRINTF(bfad
, BFA_ERR
, "LUN reset failure, status: %d\n",
335 * Scsi_Host template entry, resets the bus and abort all commands.
338 bfad_im_reset_bus_handler(struct scsi_cmnd
*cmnd
)
340 struct Scsi_Host
*shost
= cmnd
->device
->host
;
341 struct bfad_im_port_s
*im_port
=
342 (struct bfad_im_port_s
*) shost
->hostdata
[0];
343 struct bfad_s
*bfad
= im_port
->bfad
;
344 struct bfad_itnim_s
*itnim
;
346 u32 i
, rc
, err_cnt
= 0;
347 DECLARE_WAIT_QUEUE_HEAD(wq
);
348 enum bfi_tskim_status task_status
;
350 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
351 for (i
= 0; i
< MAX_FCP_TARGET
; i
++) {
352 itnim
= bfad_os_get_itnim(im_port
, i
);
354 cmnd
->SCp
.ptr
= (char *)&wq
;
355 rc
= bfad_im_target_reset_send(bfad
, cmnd
, itnim
);
356 if (rc
!= BFA_STATUS_OK
) {
361 /* wait target reset to complete */
362 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
363 wait_event(wq
, test_bit(IO_DONE_BIT
,
364 (unsigned long *)&cmnd
->SCp
.Status
));
365 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
367 task_status
= cmnd
->SCp
.Status
>> 1;
368 if (task_status
!= BFI_TSKIM_STS_OK
) {
369 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
370 "target reset failure,"
371 " status: %d\n", task_status
);
376 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
385 * Scsi_Host template entry slave_destroy.
388 bfad_im_slave_destroy(struct scsi_device
*sdev
)
390 sdev
->hostdata
= NULL
;
395 * BFA FCS itnim callbacks
399 * BFA FCS itnim alloc callback, after successful PRLI
403 bfa_fcb_itnim_alloc(struct bfad_s
*bfad
, struct bfa_fcs_itnim_s
**itnim
,
404 struct bfad_itnim_s
**itnim_drv
)
406 *itnim_drv
= kzalloc(sizeof(struct bfad_itnim_s
), GFP_ATOMIC
);
407 if (*itnim_drv
== NULL
)
410 (*itnim_drv
)->im
= bfad
->im
;
411 *itnim
= &(*itnim_drv
)->fcs_itnim
;
412 (*itnim_drv
)->state
= ITNIM_STATE_NONE
;
415 * Initiaze the itnim_work
417 INIT_WORK(&(*itnim_drv
)->itnim_work
, bfad_im_itnim_work_handler
);
418 bfad
->bfad_flags
|= BFAD_RPORT_ONLINE
;
422 * BFA FCS itnim free callback.
423 * Context: Interrupt. bfad_lock is held
426 bfa_fcb_itnim_free(struct bfad_s
*bfad
, struct bfad_itnim_s
*itnim_drv
)
428 struct bfad_port_s
*port
;
431 char wwpn_str
[32], fcid_str
[16];
433 /* online to free state transtion should not happen */
434 bfa_assert(itnim_drv
->state
!= ITNIM_STATE_ONLINE
);
436 itnim_drv
->queue_work
= 1;
437 /* offline request is not yet done, use the same request to free */
438 if (itnim_drv
->state
== ITNIM_STATE_OFFLINE_PENDING
)
439 itnim_drv
->queue_work
= 0;
441 itnim_drv
->state
= ITNIM_STATE_FREE
;
442 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
443 itnim_drv
->im_port
= port
->im_port
;
444 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim_drv
->fcs_itnim
);
445 fcid
= bfa_fcs_itnim_get_fcid(&itnim_drv
->fcs_itnim
);
446 wwn2str(wwpn_str
, wwpn
);
447 fcid2str(fcid_str
, fcid
);
448 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_FREE
,
449 port
->im_port
->shost
->host_no
,
451 bfad_os_itnim_process(itnim_drv
);
455 * BFA FCS itnim online callback.
456 * Context: Interrupt. bfad_lock is held
459 bfa_fcb_itnim_online(struct bfad_itnim_s
*itnim_drv
)
461 struct bfad_port_s
*port
;
463 itnim_drv
->bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim_drv
->fcs_itnim
);
464 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
465 itnim_drv
->state
= ITNIM_STATE_ONLINE
;
466 itnim_drv
->queue_work
= 1;
467 itnim_drv
->im_port
= port
->im_port
;
468 bfad_os_itnim_process(itnim_drv
);
472 * BFA FCS itnim offline callback.
473 * Context: Interrupt. bfad_lock is held
476 bfa_fcb_itnim_offline(struct bfad_itnim_s
*itnim_drv
)
478 struct bfad_port_s
*port
;
481 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
483 if ((bfad
->pport
.flags
& BFAD_PORT_DELETE
) ||
484 (port
->flags
& BFAD_PORT_DELETE
)) {
485 itnim_drv
->state
= ITNIM_STATE_OFFLINE
;
488 itnim_drv
->im_port
= port
->im_port
;
489 itnim_drv
->state
= ITNIM_STATE_OFFLINE_PENDING
;
490 itnim_drv
->queue_work
= 1;
491 bfad_os_itnim_process(itnim_drv
);
495 * BFA FCS itnim timeout callback.
496 * Context: Interrupt. bfad_lock is held
498 void bfa_fcb_itnim_tov(struct bfad_itnim_s
*itnim
)
500 itnim
->state
= ITNIM_STATE_TIMEOUT
;
504 * Path TOV processing begin notification -- dummy for linux
507 bfa_fcb_itnim_tov_begin(struct bfad_itnim_s
*itnim
)
514 * Allocate a Scsi_Host for a port.
517 bfad_im_scsi_host_alloc(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
)
521 if (!idr_pre_get(&bfad_im_port_index
, GFP_KERNEL
)) {
522 printk(KERN_WARNING
"idr_pre_get failure\n");
526 error
= idr_get_new(&bfad_im_port_index
, im_port
,
529 printk(KERN_WARNING
"idr_get_new failure\n");
533 im_port
->shost
= bfad_os_scsi_host_alloc(im_port
, bfad
);
534 if (!im_port
->shost
) {
539 im_port
->shost
->hostdata
[0] = (unsigned long)im_port
;
540 im_port
->shost
->unique_id
= im_port
->idr_id
;
541 im_port
->shost
->this_id
= -1;
542 im_port
->shost
->max_id
= MAX_FCP_TARGET
;
543 im_port
->shost
->max_lun
= MAX_FCP_LUN
;
544 im_port
->shost
->max_cmd_len
= 16;
545 im_port
->shost
->can_queue
= bfad
->cfg_data
.ioc_queue_depth
;
546 im_port
->shost
->transportt
= bfad_im_scsi_transport_template
;
548 error
= bfad_os_scsi_add_host(im_port
->shost
, im_port
, bfad
);
550 printk(KERN_WARNING
"bfad_os_scsi_add_host failure %d\n",
555 /* setup host fixed attribute if the lk supports */
556 bfad_os_fc_host_init(im_port
);
561 scsi_host_put(im_port
->shost
);
563 idr_remove(&bfad_im_port_index
, im_port
->idr_id
);
569 bfad_im_scsi_host_free(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
)
573 bfa_trc(bfad
, bfad
->inst_no
);
574 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_HOST_FREE
,
575 im_port
->shost
->host_no
);
577 fc_remove_host(im_port
->shost
);
579 scsi_remove_host(im_port
->shost
);
580 scsi_host_put(im_port
->shost
);
582 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
583 idr_remove(&bfad_im_port_index
, im_port
->idr_id
);
584 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
588 bfad_im_port_delete_handler(struct work_struct
*work
)
590 struct bfad_im_port_s
*im_port
=
591 container_of(work
, struct bfad_im_port_s
, port_delete_work
);
593 bfad_im_scsi_host_free(im_port
->bfad
, im_port
);
594 bfad_im_port_clean(im_port
);
599 bfad_im_port_new(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
601 int rc
= BFA_STATUS_OK
;
602 struct bfad_im_port_s
*im_port
;
604 im_port
= kzalloc(sizeof(struct bfad_im_port_s
), GFP_ATOMIC
);
605 if (im_port
== NULL
) {
606 rc
= BFA_STATUS_ENOMEM
;
609 port
->im_port
= im_port
;
610 im_port
->port
= port
;
611 im_port
->bfad
= bfad
;
613 INIT_WORK(&im_port
->port_delete_work
, bfad_im_port_delete_handler
);
614 INIT_LIST_HEAD(&im_port
->itnim_mapped_list
);
615 INIT_LIST_HEAD(&im_port
->binding_list
);
622 bfad_im_port_delete(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
624 struct bfad_im_port_s
*im_port
= port
->im_port
;
626 queue_work(bfad
->im
->drv_workq
,
627 &im_port
->port_delete_work
);
631 bfad_im_port_clean(struct bfad_im_port_s
*im_port
)
633 struct bfad_fcp_binding
*bp
, *bp_new
;
635 struct bfad_s
*bfad
= im_port
->bfad
;
637 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
638 list_for_each_entry_safe(bp
, bp_new
, &im_port
->binding_list
,
640 list_del(&bp
->list_entry
);
644 /* the itnim_mapped_list must be empty at this time */
645 bfa_assert(list_empty(&im_port
->itnim_mapped_list
));
647 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
651 bfad_im_port_online(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
656 bfad_im_port_offline(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
661 bfad_im_probe(struct bfad_s
*bfad
)
663 struct bfad_im_s
*im
;
664 bfa_status_t rc
= BFA_STATUS_OK
;
666 im
= kzalloc(sizeof(struct bfad_im_s
), GFP_KERNEL
);
668 rc
= BFA_STATUS_ENOMEM
;
675 if (bfad_os_thread_workq(bfad
) != BFA_STATUS_OK
) {
677 rc
= BFA_STATUS_FAILED
;
685 bfad_im_probe_undo(struct bfad_s
*bfad
)
688 bfad_os_destroy_workq(bfad
->im
);
698 bfad_os_scsi_add_host(struct Scsi_Host
*shost
, struct bfad_im_port_s
*im_port
,
703 if (im_port
->port
->pvb_type
== BFAD_PORT_PHYS_BASE
)
704 dev
= &bfad
->pcidev
->dev
;
706 dev
= &bfad
->pport
.im_port
->shost
->shost_gendev
;
708 return scsi_add_host(shost
, dev
);
712 bfad_os_scsi_host_alloc(struct bfad_im_port_s
*im_port
, struct bfad_s
*bfad
)
714 struct scsi_host_template
*sht
;
716 if (im_port
->port
->pvb_type
== BFAD_PORT_PHYS_BASE
)
717 sht
= &bfad_im_scsi_host_template
;
719 sht
= &bfad_im_vport_template
;
721 sht
->sg_tablesize
= bfad
->cfg_data
.io_max_sge
;
723 return scsi_host_alloc(sht
, sizeof(unsigned long));
727 bfad_os_scsi_host_free(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
)
729 flush_workqueue(bfad
->im
->drv_workq
);
730 bfad_im_scsi_host_free(im_port
->bfad
, im_port
);
731 bfad_im_port_clean(im_port
);
736 bfad_os_destroy_workq(struct bfad_im_s
*im
)
738 if (im
&& im
->drv_workq
) {
739 destroy_workqueue(im
->drv_workq
);
740 im
->drv_workq
= NULL
;
745 bfad_os_thread_workq(struct bfad_s
*bfad
)
747 struct bfad_im_s
*im
= bfad
->im
;
750 snprintf(im
->drv_workq_name
, BFAD_KOBJ_NAME_LEN
, "bfad_wq_%d",
752 im
->drv_workq
= create_singlethread_workqueue(im
->drv_workq_name
);
754 return BFA_STATUS_FAILED
;
756 return BFA_STATUS_OK
;
760 * Scsi_Host template entry.
763 * OS entry point to adjust the queue_depths on a per-device basis.
764 * Called once per device during the bus scan.
765 * Return non-zero if fails.
768 bfad_im_slave_configure(struct scsi_device
*sdev
)
770 if (sdev
->tagged_supported
)
771 scsi_activate_tcq(sdev
, bfa_lun_queue_depth
);
773 scsi_deactivate_tcq(sdev
, bfa_lun_queue_depth
);
778 struct scsi_host_template bfad_im_scsi_host_template
= {
779 .module
= THIS_MODULE
,
780 .name
= BFAD_DRIVER_NAME
,
781 .info
= bfad_im_info
,
782 .queuecommand
= bfad_im_queuecommand
,
783 .eh_abort_handler
= bfad_im_abort_handler
,
784 .eh_device_reset_handler
= bfad_im_reset_lun_handler
,
785 .eh_bus_reset_handler
= bfad_im_reset_bus_handler
,
787 .slave_alloc
= bfad_im_slave_alloc
,
788 .slave_configure
= bfad_im_slave_configure
,
789 .slave_destroy
= bfad_im_slave_destroy
,
792 .sg_tablesize
= BFAD_IO_MAX_SGE
,
794 .use_clustering
= ENABLE_CLUSTERING
,
795 .shost_attrs
= bfad_im_host_attrs
,
796 .max_sectors
= 0xFFFF,
799 struct scsi_host_template bfad_im_vport_template
= {
800 .module
= THIS_MODULE
,
801 .name
= BFAD_DRIVER_NAME
,
802 .info
= bfad_im_info
,
803 .queuecommand
= bfad_im_queuecommand
,
804 .eh_abort_handler
= bfad_im_abort_handler
,
805 .eh_device_reset_handler
= bfad_im_reset_lun_handler
,
806 .eh_bus_reset_handler
= bfad_im_reset_bus_handler
,
808 .slave_alloc
= bfad_im_slave_alloc
,
809 .slave_configure
= bfad_im_slave_configure
,
810 .slave_destroy
= bfad_im_slave_destroy
,
813 .sg_tablesize
= BFAD_IO_MAX_SGE
,
815 .use_clustering
= ENABLE_CLUSTERING
,
816 .shost_attrs
= bfad_im_vport_attrs
,
817 .max_sectors
= 0xFFFF,
821 bfad_im_probe_post(struct bfad_im_s
*im
)
823 flush_workqueue(im
->drv_workq
);
827 bfad_im_module_init(void)
829 bfad_im_scsi_transport_template
=
830 fc_attach_transport(&bfad_im_fc_function_template
);
831 if (!bfad_im_scsi_transport_template
)
832 return BFA_STATUS_ENOMEM
;
834 return BFA_STATUS_OK
;
838 bfad_im_module_exit(void)
840 if (bfad_im_scsi_transport_template
)
841 fc_release_transport(bfad_im_scsi_transport_template
);
845 bfad_os_itnim_process(struct bfad_itnim_s
*itnim_drv
)
847 struct bfad_im_s
*im
= itnim_drv
->im
;
849 if (itnim_drv
->queue_work
)
850 queue_work(im
->drv_workq
, &itnim_drv
->itnim_work
);
854 bfad_os_ramp_up_qdepth(struct bfad_itnim_s
*itnim
, struct scsi_device
*sdev
)
856 struct scsi_device
*tmp_sdev
;
858 if (((jiffies
- itnim
->last_ramp_up_time
) >
859 BFA_QUEUE_FULL_RAMP_UP_TIME
* HZ
) &&
860 ((jiffies
- itnim
->last_queue_full_time
) >
861 BFA_QUEUE_FULL_RAMP_UP_TIME
* HZ
)) {
862 shost_for_each_device(tmp_sdev
, sdev
->host
) {
863 if (bfa_lun_queue_depth
> tmp_sdev
->queue_depth
) {
864 if (tmp_sdev
->id
!= sdev
->id
)
866 if (tmp_sdev
->ordered_tags
)
867 scsi_adjust_queue_depth(tmp_sdev
,
869 tmp_sdev
->queue_depth
+ 1);
871 scsi_adjust_queue_depth(tmp_sdev
,
873 tmp_sdev
->queue_depth
+ 1);
875 itnim
->last_ramp_up_time
= jiffies
;
882 bfad_os_handle_qfull(struct bfad_itnim_s
*itnim
, struct scsi_device
*sdev
)
884 struct scsi_device
*tmp_sdev
;
886 itnim
->last_queue_full_time
= jiffies
;
888 shost_for_each_device(tmp_sdev
, sdev
->host
) {
889 if (tmp_sdev
->id
!= sdev
->id
)
891 scsi_track_queue_full(tmp_sdev
, tmp_sdev
->queue_depth
- 1);
898 struct bfad_itnim_s
*
899 bfad_os_get_itnim(struct bfad_im_port_s
*im_port
, int id
)
901 struct bfad_itnim_s
*itnim
= NULL
;
903 /* Search the mapped list for this target ID */
904 list_for_each_entry(itnim
, &im_port
->itnim_mapped_list
, list_entry
) {
905 if (id
== itnim
->scsi_tgt_id
)
913 * Scsi_Host template entry slave_alloc
916 bfad_im_slave_alloc(struct scsi_device
*sdev
)
918 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
920 if (!rport
|| fc_remote_port_chkready(rport
))
923 sdev
->hostdata
= rport
->dd_data
;
929 bfad_os_fc_host_init(struct bfad_im_port_s
*im_port
)
931 struct Scsi_Host
*host
= im_port
->shost
;
932 struct bfad_s
*bfad
= im_port
->bfad
;
933 struct bfad_port_s
*port
= im_port
->port
;
935 struct bfa_pport_attr_s pattr
;
936 struct bfa_ioc_attr_s ioc_attr
;
939 fc_host_node_name(host
) =
940 bfa_os_htonll((bfa_fcs_port_get_nwwn(port
->fcs_port
)));
941 fc_host_port_name(host
) =
942 bfa_os_htonll((bfa_fcs_port_get_pwwn(port
->fcs_port
)));
944 fc_host_supported_classes(host
) = FC_COS_CLASS3
;
946 memset(fc_host_supported_fc4s(host
), 0,
947 sizeof(fc_host_supported_fc4s(host
)));
948 if (bfad_supported_fc4s
& (BFA_PORT_ROLE_FCP_IM
| BFA_PORT_ROLE_FCP_TM
))
949 /* For FCP type 0x08 */
950 fc_host_supported_fc4s(host
)[2] = 1;
951 if (bfad_supported_fc4s
| BFA_PORT_ROLE_FCP_IPFC
)
952 /* For LLC/SNAP type 0x05 */
953 fc_host_supported_fc4s(host
)[3] = 0x20;
954 /* For fibre channel services type 0x20 */
955 fc_host_supported_fc4s(host
)[7] = 1;
957 memset(&attr
.ioc_attr
, 0, sizeof(attr
.ioc_attr
));
958 bfa_get_attr(&bfad
->bfa
, &attr
.ioc_attr
);
959 sprintf(fc_host_symbolic_name(host
), "Brocade %s FV%s DV%s",
960 attr
.ioc_attr
.adapter_attr
.model
,
961 attr
.ioc_attr
.adapter_attr
.fw_ver
, BFAD_DRIVER_VERSION
);
963 fc_host_supported_speeds(host
) = 0;
964 fc_host_supported_speeds(host
) |=
965 FC_PORTSPEED_8GBIT
| FC_PORTSPEED_4GBIT
| FC_PORTSPEED_2GBIT
|
968 memset(&attr
.pattr
, 0, sizeof(attr
.pattr
));
969 bfa_pport_get_attr(&bfad
->bfa
, &attr
.pattr
);
970 fc_host_maxframe_size(host
) = attr
.pattr
.pport_cfg
.maxfrsize
;
974 bfad_im_fc_rport_add(struct bfad_im_port_s
*im_port
, struct bfad_itnim_s
*itnim
)
976 struct fc_rport_identifiers rport_ids
;
977 struct fc_rport
*fc_rport
;
978 struct bfad_itnim_data_s
*itnim_data
;
980 rport_ids
.node_name
=
981 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim
->fcs_itnim
));
982 rport_ids
.port_name
=
983 bfa_os_htonll(bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
));
985 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
));
986 rport_ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
988 itnim
->fc_rport
= fc_rport
=
989 fc_remote_port_add(im_port
->shost
, 0, &rport_ids
);
994 fc_rport
->maxframe_size
=
995 bfa_fcs_itnim_get_maxfrsize(&itnim
->fcs_itnim
);
996 fc_rport
->supported_classes
= bfa_fcs_itnim_get_cos(&itnim
->fcs_itnim
);
998 itnim_data
= fc_rport
->dd_data
;
999 itnim_data
->itnim
= itnim
;
1001 rport_ids
.roles
|= FC_RPORT_ROLE_FCP_TARGET
;
1003 if (rport_ids
.roles
!= FC_RPORT_ROLE_UNKNOWN
)
1004 fc_remote_port_rolechg(fc_rport
, rport_ids
.roles
);
1006 if ((fc_rport
->scsi_target_id
!= -1)
1007 && (fc_rport
->scsi_target_id
< MAX_FCP_TARGET
))
1008 itnim
->scsi_tgt_id
= fc_rport
->scsi_target_id
;
1014 * Work queue handler using FC transport service
1018 bfad_im_itnim_work_handler(struct work_struct
*work
)
1020 struct bfad_itnim_s
*itnim
= container_of(work
, struct bfad_itnim_s
,
1022 struct bfad_im_s
*im
= itnim
->im
;
1023 struct bfad_s
*bfad
= im
->bfad
;
1024 struct bfad_im_port_s
*im_port
;
1025 unsigned long flags
;
1026 struct fc_rport
*fc_rport
;
1029 char wwpn_str
[32], fcid_str
[16];
1031 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1032 im_port
= itnim
->im_port
;
1033 bfa_trc(bfad
, itnim
->state
);
1034 switch (itnim
->state
) {
1035 case ITNIM_STATE_ONLINE
:
1036 if (!itnim
->fc_rport
) {
1037 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1038 bfad_im_fc_rport_add(im_port
, itnim
);
1039 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1040 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
);
1041 fcid
= bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
);
1042 wwn2str(wwpn_str
, wwpn
);
1043 fcid2str(fcid_str
, fcid
);
1044 list_add_tail(&itnim
->list_entry
,
1045 &im_port
->itnim_mapped_list
);
1046 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_ONLINE
,
1047 im_port
->shost
->host_no
,
1049 fcid_str
, wwpn_str
);
1052 "%s: itnim %llx is already in online state\n",
1054 bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
));
1058 case ITNIM_STATE_OFFLINE_PENDING
:
1059 itnim
->state
= ITNIM_STATE_OFFLINE
;
1060 if (itnim
->fc_rport
) {
1061 fc_rport
= itnim
->fc_rport
;
1062 ((struct bfad_itnim_data_s
*)
1063 fc_rport
->dd_data
)->itnim
= NULL
;
1064 itnim
->fc_rport
= NULL
;
1065 if (!(im_port
->port
->flags
& BFAD_PORT_DELETE
)) {
1066 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1067 fc_rport
->dev_loss_tmo
=
1068 bfa_fcpim_path_tov_get(&bfad
->bfa
) + 1;
1069 fc_remote_port_delete(fc_rport
);
1070 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1072 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
);
1073 fcid
= bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
);
1074 wwn2str(wwpn_str
, wwpn
);
1075 fcid2str(fcid_str
, fcid
);
1076 list_del(&itnim
->list_entry
);
1077 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_OFFLINE
,
1078 im_port
->shost
->host_no
,
1080 fcid_str
, wwpn_str
);
1083 case ITNIM_STATE_FREE
:
1084 if (itnim
->fc_rport
) {
1085 fc_rport
= itnim
->fc_rport
;
1086 ((struct bfad_itnim_data_s
*)
1087 fc_rport
->dd_data
)->itnim
= NULL
;
1088 itnim
->fc_rport
= NULL
;
1089 if (!(im_port
->port
->flags
& BFAD_PORT_DELETE
)) {
1090 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1091 fc_rport
->dev_loss_tmo
=
1092 bfa_fcpim_path_tov_get(&bfad
->bfa
) + 1;
1093 fc_remote_port_delete(fc_rport
);
1094 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1096 list_del(&itnim
->list_entry
);
1106 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1110 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1113 bfad_im_queuecommand(struct scsi_cmnd
*cmnd
, void (*done
) (struct scsi_cmnd
*))
1115 struct bfad_im_port_s
*im_port
=
1116 (struct bfad_im_port_s
*) cmnd
->device
->host
->hostdata
[0];
1117 struct bfad_s
*bfad
= im_port
->bfad
;
1118 struct bfad_itnim_data_s
*itnim_data
= cmnd
->device
->hostdata
;
1119 struct bfad_itnim_s
*itnim
;
1120 struct bfa_ioim_s
*hal_io
;
1121 unsigned long flags
;
1124 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
1126 rc
= fc_remote_port_chkready(rport
);
1133 sg_cnt
= scsi_dma_map(cmnd
);
1136 return SCSI_MLQUEUE_HOST_BUSY
;
1138 cmnd
->scsi_done
= done
;
1140 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1141 if (!(bfad
->bfad_flags
& BFAD_HAL_START_DONE
)) {
1143 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1144 bfad
->inst_no
, cmnd
, cmnd
->cmnd
[0]);
1145 cmnd
->result
= ScsiResult(DID_NO_CONNECT
, 0);
1149 itnim
= itnim_data
->itnim
;
1151 cmnd
->result
= ScsiResult(DID_IMM_RETRY
, 0);
1155 hal_io
= bfa_ioim_alloc(&bfad
->bfa
, (struct bfad_ioim_s
*) cmnd
,
1156 itnim
->bfa_itnim
, sg_cnt
);
1158 printk(KERN_WARNING
"hal_io failure\n");
1159 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1160 scsi_dma_unmap(cmnd
);
1161 return SCSI_MLQUEUE_HOST_BUSY
;
1164 cmnd
->host_scribble
= (char *)hal_io
;
1165 bfa_trc_fp(bfad
, hal_io
->iotag
);
1166 bfa_ioim_start(hal_io
);
1167 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1172 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1173 scsi_dma_unmap(cmnd
);
1181 bfad_os_rport_online_wait(struct bfad_s
*bfad
)
1184 int rport_delay
= 10;
1186 for (i
= 0; !(bfad
->bfad_flags
& BFAD_PORT_ONLINE
)
1187 && i
< bfa_linkup_delay
; i
++)
1188 schedule_timeout_uninterruptible(HZ
);
1190 if (bfad
->bfad_flags
& BFAD_PORT_ONLINE
) {
1191 rport_delay
= rport_delay
< bfa_linkup_delay
?
1192 rport_delay
: bfa_linkup_delay
;
1193 for (i
= 0; !(bfad
->bfad_flags
& BFAD_RPORT_ONLINE
)
1194 && i
< rport_delay
; i
++)
1195 schedule_timeout_uninterruptible(HZ
);
1197 if (rport_delay
> 0 && (bfad
->bfad_flags
& BFAD_RPORT_ONLINE
))
1198 schedule_timeout_uninterruptible(rport_delay
* HZ
);
1203 bfad_os_get_linkup_delay(struct bfad_s
*bfad
)
1211 * Querying for the boot target port wwns
1212 * -- read from boot information in flash.
1213 * If nwwns > 0 => boot over SAN and set bfa_linkup_delay = 30
1214 * else => local boot machine set bfa_linkup_delay = 10
1217 bfa_iocfc_get_bootwwns(&bfad
->bfa
, &nwwns
, &wwns
);
1220 /* If boot over SAN; linkup_delay = 30sec */
1223 /* If local boot; linkup_delay = 10sec */