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.
22 #include <linux/slab.h>
25 #include "bfad_trcmod.h"
26 #include "bfa_cb_ioim_macros.h"
27 #include <fcb/bfa_fcb_fcpim.h>
29 BFA_TRC_FILE(LDRV
, IM
);
31 DEFINE_IDR(bfad_im_port_index
);
32 struct scsi_transport_template
*bfad_im_scsi_transport_template
;
33 struct scsi_transport_template
*bfad_im_scsi_vport_transport_template
;
34 static void bfad_im_itnim_work_handler(struct work_struct
*work
);
35 static int bfad_im_queuecommand(struct scsi_cmnd
*cmnd
,
36 void (*done
)(struct scsi_cmnd
*));
37 static int bfad_im_slave_alloc(struct scsi_device
*sdev
);
40 bfa_cb_ioim_done(void *drv
, struct bfad_ioim_s
*dio
,
41 enum bfi_ioim_status io_status
, u8 scsi_status
,
42 int sns_len
, u8
*sns_info
, s32 residue
)
44 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
45 struct bfad_s
*bfad
= drv
;
46 struct bfad_itnim_data_s
*itnim_data
;
47 struct bfad_itnim_s
*itnim
;
48 u8 host_status
= DID_OK
;
52 bfa_trc(bfad
, scsi_status
);
53 scsi_set_resid(cmnd
, 0);
56 bfa_trc(bfad
, sns_len
);
57 if (sns_len
> SCSI_SENSE_BUFFERSIZE
)
58 sns_len
= SCSI_SENSE_BUFFERSIZE
;
59 memcpy(cmnd
->sense_buffer
, sns_info
, sns_len
);
62 bfa_trc(bfad
, residue
);
63 scsi_set_resid(cmnd
, residue
);
64 if (!sns_len
&& (scsi_status
== SAM_STAT_GOOD
) &&
65 (scsi_bufflen(cmnd
) - residue
) <
68 host_status
= DID_ERROR
;
71 cmnd
->result
= ScsiResult(host_status
, scsi_status
);
75 case BFI_IOIM_STS_ABORTED
:
76 case BFI_IOIM_STS_TIMEDOUT
:
77 case BFI_IOIM_STS_PATHTOV
:
79 cmnd
->result
= ScsiResult(DID_ERROR
, 0);
82 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
83 if (cmnd
->device
->host
!= NULL
)
86 cmnd
->host_scribble
= NULL
;
87 bfa_trc(bfad
, cmnd
->result
);
89 itnim_data
= cmnd
->device
->hostdata
;
91 itnim
= itnim_data
->itnim
;
92 if (!cmnd
->result
&& itnim
&&
93 (bfa_lun_queue_depth
> cmnd
->device
->queue_depth
)) {
94 /* Queue depth adjustment for good status completion */
95 bfad_os_ramp_up_qdepth(itnim
, cmnd
->device
);
96 } else if (cmnd
->result
== SAM_STAT_TASK_SET_FULL
&& itnim
) {
98 bfad_os_handle_qfull(itnim
, cmnd
->device
);
102 cmnd
->scsi_done(cmnd
);
106 bfa_cb_ioim_good_comp(void *drv
, struct bfad_ioim_s
*dio
)
108 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
109 struct bfad_itnim_data_s
*itnim_data
;
110 struct bfad_itnim_s
*itnim
;
112 cmnd
->result
= ScsiResult(DID_OK
, SCSI_STATUS_GOOD
);
114 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
115 if (cmnd
->device
->host
!= NULL
)
116 scsi_dma_unmap(cmnd
);
118 cmnd
->host_scribble
= NULL
;
120 /* Queue depth adjustment */
121 if (bfa_lun_queue_depth
> cmnd
->device
->queue_depth
) {
122 itnim_data
= cmnd
->device
->hostdata
;
124 itnim
= itnim_data
->itnim
;
126 bfad_os_ramp_up_qdepth(itnim
, cmnd
->device
);
130 cmnd
->scsi_done(cmnd
);
134 bfa_cb_ioim_abort(void *drv
, struct bfad_ioim_s
*dio
)
136 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dio
;
137 struct bfad_s
*bfad
= drv
;
139 cmnd
->result
= ScsiResult(DID_ERROR
, 0);
141 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
142 if (cmnd
->device
->host
!= NULL
)
143 scsi_dma_unmap(cmnd
);
145 bfa_trc(bfad
, cmnd
->result
);
146 cmnd
->host_scribble
= NULL
;
150 bfa_cb_tskim_done(void *bfad
, struct bfad_tskim_s
*dtsk
,
151 enum bfi_tskim_status tsk_status
)
153 struct scsi_cmnd
*cmnd
= (struct scsi_cmnd
*)dtsk
;
154 wait_queue_head_t
*wq
;
156 cmnd
->SCp
.Status
|= tsk_status
<< 1;
157 set_bit(IO_DONE_BIT
, (unsigned long *)&cmnd
->SCp
.Status
);
158 wq
= (wait_queue_head_t
*) cmnd
->SCp
.ptr
;
159 cmnd
->SCp
.ptr
= NULL
;
166 bfa_cb_ioim_resfree(void *drv
)
171 * Scsi_Host_template SCSI host template
174 * Scsi_Host template entry, returns BFAD PCI info.
177 bfad_im_info(struct Scsi_Host
*shost
)
179 static char bfa_buf
[256];
180 struct bfad_im_port_s
*im_port
=
181 (struct bfad_im_port_s
*) shost
->hostdata
[0];
182 struct bfad_s
*bfad
= im_port
->bfad
;
183 char model
[BFA_ADAPTER_MODEL_NAME_LEN
];
185 bfa_get_adapter_model(&bfad
->bfa
, model
);
187 memset(bfa_buf
, 0, sizeof(bfa_buf
));
188 snprintf(bfa_buf
, sizeof(bfa_buf
),
189 "Brocade FC/FCOE Adapter, " "model: %s hwpath: %s driver: %s",
190 model
, bfad
->pci_name
, BFAD_DRIVER_VERSION
);
195 * Scsi_Host template entry, aborts the specified SCSI command.
197 * Returns: SUCCESS or FAILED.
200 bfad_im_abort_handler(struct scsi_cmnd
*cmnd
)
202 struct Scsi_Host
*shost
= cmnd
->device
->host
;
203 struct bfad_im_port_s
*im_port
=
204 (struct bfad_im_port_s
*) shost
->hostdata
[0];
205 struct bfad_s
*bfad
= im_port
->bfad
;
206 struct bfa_ioim_s
*hal_io
;
211 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
212 hal_io
= (struct bfa_ioim_s
*) cmnd
->host_scribble
;
214 /* IO has been completed, retrun success */
218 if (hal_io
->dio
!= (struct bfad_ioim_s
*) cmnd
) {
223 bfa_trc(bfad
, hal_io
->iotag
);
224 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_ABORT
,
225 im_port
->shost
->host_no
, cmnd
, hal_io
->iotag
);
226 bfa_ioim_abort(hal_io
);
227 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
229 /* Need to wait until the command get aborted */
231 while ((struct bfa_ioim_s
*) cmnd
->host_scribble
== hal_io
) {
232 set_current_state(TASK_UNINTERRUPTIBLE
);
233 schedule_timeout(timeout
);
234 if (timeout
< 4 * HZ
)
238 cmnd
->scsi_done(cmnd
);
239 bfa_trc(bfad
, hal_io
->iotag
);
240 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_ABORT_COMP
,
241 im_port
->shost
->host_no
, cmnd
, hal_io
->iotag
);
244 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
249 bfad_im_target_reset_send(struct bfad_s
*bfad
, struct scsi_cmnd
*cmnd
,
250 struct bfad_itnim_s
*itnim
)
252 struct bfa_tskim_s
*tskim
;
253 struct bfa_itnim_s
*bfa_itnim
;
254 bfa_status_t rc
= BFA_STATUS_OK
;
256 tskim
= bfa_tskim_alloc(&bfad
->bfa
, (struct bfad_tskim_s
*) cmnd
);
258 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
259 "target reset, fail to allocate tskim\n");
260 rc
= BFA_STATUS_FAILED
;
265 * Set host_scribble to NULL to avoid aborting a task command if
268 cmnd
->host_scribble
= NULL
;
269 cmnd
->SCp
.Status
= 0;
270 bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim
->fcs_itnim
);
271 bfa_tskim_start(tskim
, bfa_itnim
, (lun_t
)0,
272 FCP_TM_TARGET_RESET
, BFAD_TARGET_RESET_TMO
);
278 * Scsi_Host template entry, resets a LUN and abort its all commands.
280 * Returns: SUCCESS or FAILED.
284 bfad_im_reset_lun_handler(struct scsi_cmnd
*cmnd
)
286 struct Scsi_Host
*shost
= cmnd
->device
->host
;
287 struct bfad_im_port_s
*im_port
=
288 (struct bfad_im_port_s
*) shost
->hostdata
[0];
289 struct bfad_itnim_data_s
*itnim_data
= cmnd
->device
->hostdata
;
290 struct bfad_s
*bfad
= im_port
->bfad
;
291 struct bfa_tskim_s
*tskim
;
292 struct bfad_itnim_s
*itnim
;
293 struct bfa_itnim_s
*bfa_itnim
;
294 DECLARE_WAIT_QUEUE_HEAD(wq
);
297 enum bfi_tskim_status task_status
;
299 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
300 itnim
= itnim_data
->itnim
;
302 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
307 tskim
= bfa_tskim_alloc(&bfad
->bfa
, (struct bfad_tskim_s
*) cmnd
);
309 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
310 "LUN reset, fail to allocate tskim");
311 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
317 * Set host_scribble to NULL to avoid aborting a task command
320 cmnd
->host_scribble
= NULL
;
321 cmnd
->SCp
.ptr
= (char *)&wq
;
322 cmnd
->SCp
.Status
= 0;
323 bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim
->fcs_itnim
);
324 bfa_tskim_start(tskim
, bfa_itnim
,
325 bfad_int_to_lun(cmnd
->device
->lun
),
326 FCP_TM_LUN_RESET
, BFAD_LUN_RESET_TMO
);
327 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
329 wait_event(wq
, test_bit(IO_DONE_BIT
,
330 (unsigned long *)&cmnd
->SCp
.Status
));
332 task_status
= cmnd
->SCp
.Status
>> 1;
333 if (task_status
!= BFI_TSKIM_STS_OK
) {
334 BFA_DEV_PRINTF(bfad
, BFA_ERR
, "LUN reset failure, status: %d\n",
344 * Scsi_Host template entry, resets the bus and abort all commands.
347 bfad_im_reset_bus_handler(struct scsi_cmnd
*cmnd
)
349 struct Scsi_Host
*shost
= cmnd
->device
->host
;
350 struct bfad_im_port_s
*im_port
=
351 (struct bfad_im_port_s
*) shost
->hostdata
[0];
352 struct bfad_s
*bfad
= im_port
->bfad
;
353 struct bfad_itnim_s
*itnim
;
355 u32 i
, rc
, err_cnt
= 0;
356 DECLARE_WAIT_QUEUE_HEAD(wq
);
357 enum bfi_tskim_status task_status
;
359 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
360 for (i
= 0; i
< MAX_FCP_TARGET
; i
++) {
361 itnim
= bfad_os_get_itnim(im_port
, i
);
363 cmnd
->SCp
.ptr
= (char *)&wq
;
364 rc
= bfad_im_target_reset_send(bfad
, cmnd
, itnim
);
365 if (rc
!= BFA_STATUS_OK
) {
370 /* wait target reset to complete */
371 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
372 wait_event(wq
, test_bit(IO_DONE_BIT
,
373 (unsigned long *)&cmnd
->SCp
.Status
));
374 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
376 task_status
= cmnd
->SCp
.Status
>> 1;
377 if (task_status
!= BFI_TSKIM_STS_OK
) {
378 BFA_DEV_PRINTF(bfad
, BFA_ERR
,
379 "target reset failure,"
380 " status: %d\n", task_status
);
385 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
394 * Scsi_Host template entry slave_destroy.
397 bfad_im_slave_destroy(struct scsi_device
*sdev
)
399 sdev
->hostdata
= NULL
;
404 * BFA FCS itnim callbacks
408 * BFA FCS itnim alloc callback, after successful PRLI
412 bfa_fcb_itnim_alloc(struct bfad_s
*bfad
, struct bfa_fcs_itnim_s
**itnim
,
413 struct bfad_itnim_s
**itnim_drv
)
415 *itnim_drv
= kzalloc(sizeof(struct bfad_itnim_s
), GFP_ATOMIC
);
416 if (*itnim_drv
== NULL
)
419 (*itnim_drv
)->im
= bfad
->im
;
420 *itnim
= &(*itnim_drv
)->fcs_itnim
;
421 (*itnim_drv
)->state
= ITNIM_STATE_NONE
;
424 * Initiaze the itnim_work
426 INIT_WORK(&(*itnim_drv
)->itnim_work
, bfad_im_itnim_work_handler
);
427 bfad
->bfad_flags
|= BFAD_RPORT_ONLINE
;
431 * BFA FCS itnim free callback.
432 * Context: Interrupt. bfad_lock is held
435 bfa_fcb_itnim_free(struct bfad_s
*bfad
, struct bfad_itnim_s
*itnim_drv
)
437 struct bfad_port_s
*port
;
440 char wwpn_str
[32], fcid_str
[16];
442 /* online to free state transtion should not happen */
443 bfa_assert(itnim_drv
->state
!= ITNIM_STATE_ONLINE
);
445 itnim_drv
->queue_work
= 1;
446 /* offline request is not yet done, use the same request to free */
447 if (itnim_drv
->state
== ITNIM_STATE_OFFLINE_PENDING
)
448 itnim_drv
->queue_work
= 0;
450 itnim_drv
->state
= ITNIM_STATE_FREE
;
451 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
452 itnim_drv
->im_port
= port
->im_port
;
453 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim_drv
->fcs_itnim
);
454 fcid
= bfa_fcs_itnim_get_fcid(&itnim_drv
->fcs_itnim
);
455 wwn2str(wwpn_str
, wwpn
);
456 fcid2str(fcid_str
, fcid
);
457 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_FREE
,
458 port
->im_port
->shost
->host_no
,
460 bfad_os_itnim_process(itnim_drv
);
464 * BFA FCS itnim online callback.
465 * Context: Interrupt. bfad_lock is held
468 bfa_fcb_itnim_online(struct bfad_itnim_s
*itnim_drv
)
470 struct bfad_port_s
*port
;
472 itnim_drv
->bfa_itnim
= bfa_fcs_itnim_get_halitn(&itnim_drv
->fcs_itnim
);
473 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
474 itnim_drv
->state
= ITNIM_STATE_ONLINE
;
475 itnim_drv
->queue_work
= 1;
476 itnim_drv
->im_port
= port
->im_port
;
477 bfad_os_itnim_process(itnim_drv
);
481 * BFA FCS itnim offline callback.
482 * Context: Interrupt. bfad_lock is held
485 bfa_fcb_itnim_offline(struct bfad_itnim_s
*itnim_drv
)
487 struct bfad_port_s
*port
;
490 port
= bfa_fcs_itnim_get_drvport(&itnim_drv
->fcs_itnim
);
492 if ((bfad
->pport
.flags
& BFAD_PORT_DELETE
) ||
493 (port
->flags
& BFAD_PORT_DELETE
)) {
494 itnim_drv
->state
= ITNIM_STATE_OFFLINE
;
497 itnim_drv
->im_port
= port
->im_port
;
498 itnim_drv
->state
= ITNIM_STATE_OFFLINE_PENDING
;
499 itnim_drv
->queue_work
= 1;
500 bfad_os_itnim_process(itnim_drv
);
504 * BFA FCS itnim timeout callback.
505 * Context: Interrupt. bfad_lock is held
507 void bfa_fcb_itnim_tov(struct bfad_itnim_s
*itnim
)
509 itnim
->state
= ITNIM_STATE_TIMEOUT
;
513 * Allocate a Scsi_Host for a port.
516 bfad_im_scsi_host_alloc(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
,
521 mutex_lock(&bfad_mutex
);
522 if (!idr_pre_get(&bfad_im_port_index
, GFP_KERNEL
)) {
523 mutex_unlock(&bfad_mutex
);
524 printk(KERN_WARNING
"idr_pre_get failure\n");
528 error
= idr_get_new(&bfad_im_port_index
, im_port
,
531 mutex_unlock(&bfad_mutex
);
532 printk(KERN_WARNING
"idr_get_new failure\n");
536 mutex_unlock(&bfad_mutex
);
538 im_port
->shost
= bfad_os_scsi_host_alloc(im_port
, bfad
);
539 if (!im_port
->shost
) {
544 im_port
->shost
->hostdata
[0] = (unsigned long)im_port
;
545 im_port
->shost
->unique_id
= im_port
->idr_id
;
546 im_port
->shost
->this_id
= -1;
547 im_port
->shost
->max_id
= MAX_FCP_TARGET
;
548 im_port
->shost
->max_lun
= MAX_FCP_LUN
;
549 im_port
->shost
->max_cmd_len
= 16;
550 im_port
->shost
->can_queue
= bfad
->cfg_data
.ioc_queue_depth
;
551 if (im_port
->port
->pvb_type
== BFAD_PORT_PHYS_BASE
)
552 im_port
->shost
->transportt
= bfad_im_scsi_transport_template
;
554 im_port
->shost
->transportt
=
555 bfad_im_scsi_vport_transport_template
;
557 error
= scsi_add_host(im_port
->shost
, dev
);
559 printk(KERN_WARNING
"scsi_add_host failure %d\n", error
);
563 /* setup host fixed attribute if the lk supports */
564 bfad_os_fc_host_init(im_port
);
569 scsi_host_put(im_port
->shost
);
571 mutex_lock(&bfad_mutex
);
572 idr_remove(&bfad_im_port_index
, im_port
->idr_id
);
573 mutex_unlock(&bfad_mutex
);
579 bfad_im_scsi_host_free(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
)
581 bfa_trc(bfad
, bfad
->inst_no
);
582 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_SCSI_HOST_FREE
,
583 im_port
->shost
->host_no
);
585 fc_remove_host(im_port
->shost
);
587 scsi_remove_host(im_port
->shost
);
588 scsi_host_put(im_port
->shost
);
590 mutex_lock(&bfad_mutex
);
591 idr_remove(&bfad_im_port_index
, im_port
->idr_id
);
592 mutex_unlock(&bfad_mutex
);
596 bfad_im_port_delete_handler(struct work_struct
*work
)
598 struct bfad_im_port_s
*im_port
=
599 container_of(work
, struct bfad_im_port_s
, port_delete_work
);
601 if (im_port
->port
->pvb_type
!= BFAD_PORT_PHYS_BASE
) {
602 im_port
->flags
|= BFAD_PORT_DELETE
;
603 fc_vport_terminate(im_port
->fc_vport
);
609 bfad_im_port_new(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
611 int rc
= BFA_STATUS_OK
;
612 struct bfad_im_port_s
*im_port
;
614 im_port
= kzalloc(sizeof(struct bfad_im_port_s
), GFP_ATOMIC
);
615 if (im_port
== NULL
) {
616 rc
= BFA_STATUS_ENOMEM
;
619 port
->im_port
= im_port
;
620 im_port
->port
= port
;
621 im_port
->bfad
= bfad
;
623 INIT_WORK(&im_port
->port_delete_work
, bfad_im_port_delete_handler
);
624 INIT_LIST_HEAD(&im_port
->itnim_mapped_list
);
625 INIT_LIST_HEAD(&im_port
->binding_list
);
632 bfad_im_port_delete(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
634 struct bfad_im_port_s
*im_port
= port
->im_port
;
636 queue_work(bfad
->im
->drv_workq
,
637 &im_port
->port_delete_work
);
641 bfad_im_port_clean(struct bfad_im_port_s
*im_port
)
643 struct bfad_fcp_binding
*bp
, *bp_new
;
645 struct bfad_s
*bfad
= im_port
->bfad
;
647 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
648 list_for_each_entry_safe(bp
, bp_new
, &im_port
->binding_list
,
650 list_del(&bp
->list_entry
);
654 /* the itnim_mapped_list must be empty at this time */
655 bfa_assert(list_empty(&im_port
->itnim_mapped_list
));
657 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
661 bfad_im_port_online(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
666 bfad_im_port_offline(struct bfad_s
*bfad
, struct bfad_port_s
*port
)
671 bfad_im_probe(struct bfad_s
*bfad
)
673 struct bfad_im_s
*im
;
674 bfa_status_t rc
= BFA_STATUS_OK
;
676 im
= kzalloc(sizeof(struct bfad_im_s
), GFP_KERNEL
);
678 rc
= BFA_STATUS_ENOMEM
;
685 if (bfad_os_thread_workq(bfad
) != BFA_STATUS_OK
) {
687 rc
= BFA_STATUS_FAILED
;
695 bfad_im_probe_undo(struct bfad_s
*bfad
)
698 bfad_os_destroy_workq(bfad
->im
);
705 bfad_os_scsi_host_alloc(struct bfad_im_port_s
*im_port
, struct bfad_s
*bfad
)
707 struct scsi_host_template
*sht
;
709 if (im_port
->port
->pvb_type
== BFAD_PORT_PHYS_BASE
)
710 sht
= &bfad_im_scsi_host_template
;
712 sht
= &bfad_im_vport_template
;
714 sht
->sg_tablesize
= bfad
->cfg_data
.io_max_sge
;
716 return scsi_host_alloc(sht
, sizeof(unsigned long));
720 bfad_os_scsi_host_free(struct bfad_s
*bfad
, struct bfad_im_port_s
*im_port
)
722 if (!(im_port
->flags
& BFAD_PORT_DELETE
))
723 flush_workqueue(bfad
->im
->drv_workq
);
724 bfad_im_scsi_host_free(im_port
->bfad
, im_port
);
725 bfad_im_port_clean(im_port
);
730 bfad_os_destroy_workq(struct bfad_im_s
*im
)
732 if (im
&& im
->drv_workq
) {
733 destroy_workqueue(im
->drv_workq
);
734 im
->drv_workq
= NULL
;
739 bfad_os_thread_workq(struct bfad_s
*bfad
)
741 struct bfad_im_s
*im
= bfad
->im
;
744 snprintf(im
->drv_workq_name
, BFAD_KOBJ_NAME_LEN
, "bfad_wq_%d",
746 im
->drv_workq
= create_singlethread_workqueue(im
->drv_workq_name
);
748 return BFA_STATUS_FAILED
;
750 return BFA_STATUS_OK
;
754 * Scsi_Host template entry.
757 * OS entry point to adjust the queue_depths on a per-device basis.
758 * Called once per device during the bus scan.
759 * Return non-zero if fails.
762 bfad_im_slave_configure(struct scsi_device
*sdev
)
764 if (sdev
->tagged_supported
)
765 scsi_activate_tcq(sdev
, bfa_lun_queue_depth
);
767 scsi_deactivate_tcq(sdev
, bfa_lun_queue_depth
);
772 struct scsi_host_template bfad_im_scsi_host_template
= {
773 .module
= THIS_MODULE
,
774 .name
= BFAD_DRIVER_NAME
,
775 .info
= bfad_im_info
,
776 .queuecommand
= bfad_im_queuecommand
,
777 .eh_abort_handler
= bfad_im_abort_handler
,
778 .eh_device_reset_handler
= bfad_im_reset_lun_handler
,
779 .eh_bus_reset_handler
= bfad_im_reset_bus_handler
,
781 .slave_alloc
= bfad_im_slave_alloc
,
782 .slave_configure
= bfad_im_slave_configure
,
783 .slave_destroy
= bfad_im_slave_destroy
,
786 .sg_tablesize
= BFAD_IO_MAX_SGE
,
788 .use_clustering
= ENABLE_CLUSTERING
,
789 .shost_attrs
= bfad_im_host_attrs
,
790 .max_sectors
= 0xFFFF,
793 struct scsi_host_template bfad_im_vport_template
= {
794 .module
= THIS_MODULE
,
795 .name
= BFAD_DRIVER_NAME
,
796 .info
= bfad_im_info
,
797 .queuecommand
= bfad_im_queuecommand
,
798 .eh_abort_handler
= bfad_im_abort_handler
,
799 .eh_device_reset_handler
= bfad_im_reset_lun_handler
,
800 .eh_bus_reset_handler
= bfad_im_reset_bus_handler
,
802 .slave_alloc
= bfad_im_slave_alloc
,
803 .slave_configure
= bfad_im_slave_configure
,
804 .slave_destroy
= bfad_im_slave_destroy
,
807 .sg_tablesize
= BFAD_IO_MAX_SGE
,
809 .use_clustering
= ENABLE_CLUSTERING
,
810 .shost_attrs
= bfad_im_vport_attrs
,
811 .max_sectors
= 0xFFFF,
815 bfad_im_probe_post(struct bfad_im_s
*im
)
817 flush_workqueue(im
->drv_workq
);
821 bfad_im_module_init(void)
823 bfad_im_scsi_transport_template
=
824 fc_attach_transport(&bfad_im_fc_function_template
);
825 if (!bfad_im_scsi_transport_template
)
826 return BFA_STATUS_ENOMEM
;
828 bfad_im_scsi_vport_transport_template
=
829 fc_attach_transport(&bfad_im_vport_fc_function_template
);
830 if (!bfad_im_scsi_vport_transport_template
) {
831 fc_release_transport(bfad_im_scsi_transport_template
);
832 return BFA_STATUS_ENOMEM
;
835 return BFA_STATUS_OK
;
839 bfad_im_module_exit(void)
841 if (bfad_im_scsi_transport_template
)
842 fc_release_transport(bfad_im_scsi_transport_template
);
843 if (bfad_im_scsi_vport_transport_template
)
844 fc_release_transport(bfad_im_scsi_vport_transport_template
);
848 bfad_os_itnim_process(struct bfad_itnim_s
*itnim_drv
)
850 struct bfad_im_s
*im
= itnim_drv
->im
;
852 if (itnim_drv
->queue_work
)
853 queue_work(im
->drv_workq
, &itnim_drv
->itnim_work
);
857 bfad_os_ramp_up_qdepth(struct bfad_itnim_s
*itnim
, struct scsi_device
*sdev
)
859 struct scsi_device
*tmp_sdev
;
861 if (((jiffies
- itnim
->last_ramp_up_time
) >
862 BFA_QUEUE_FULL_RAMP_UP_TIME
* HZ
) &&
863 ((jiffies
- itnim
->last_queue_full_time
) >
864 BFA_QUEUE_FULL_RAMP_UP_TIME
* HZ
)) {
865 shost_for_each_device(tmp_sdev
, sdev
->host
) {
866 if (bfa_lun_queue_depth
> tmp_sdev
->queue_depth
) {
867 if (tmp_sdev
->id
!= sdev
->id
)
869 if (tmp_sdev
->ordered_tags
)
870 scsi_adjust_queue_depth(tmp_sdev
,
872 tmp_sdev
->queue_depth
+ 1);
874 scsi_adjust_queue_depth(tmp_sdev
,
876 tmp_sdev
->queue_depth
+ 1);
878 itnim
->last_ramp_up_time
= jiffies
;
885 bfad_os_handle_qfull(struct bfad_itnim_s
*itnim
, struct scsi_device
*sdev
)
887 struct scsi_device
*tmp_sdev
;
889 itnim
->last_queue_full_time
= jiffies
;
891 shost_for_each_device(tmp_sdev
, sdev
->host
) {
892 if (tmp_sdev
->id
!= sdev
->id
)
894 scsi_track_queue_full(tmp_sdev
, tmp_sdev
->queue_depth
- 1);
901 struct bfad_itnim_s
*
902 bfad_os_get_itnim(struct bfad_im_port_s
*im_port
, int id
)
904 struct bfad_itnim_s
*itnim
= NULL
;
906 /* Search the mapped list for this target ID */
907 list_for_each_entry(itnim
, &im_port
->itnim_mapped_list
, list_entry
) {
908 if (id
== itnim
->scsi_tgt_id
)
916 * Scsi_Host template entry slave_alloc
919 bfad_im_slave_alloc(struct scsi_device
*sdev
)
921 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
923 if (!rport
|| fc_remote_port_chkready(rport
))
926 sdev
->hostdata
= rport
->dd_data
;
932 bfad_os_fc_host_init(struct bfad_im_port_s
*im_port
)
934 struct Scsi_Host
*host
= im_port
->shost
;
935 struct bfad_s
*bfad
= im_port
->bfad
;
936 struct bfad_port_s
*port
= im_port
->port
;
937 struct bfa_pport_attr_s pattr
;
938 char model
[BFA_ADAPTER_MODEL_NAME_LEN
];
939 char fw_ver
[BFA_VERSION_LEN
];
941 fc_host_node_name(host
) =
942 bfa_os_htonll((bfa_fcs_port_get_nwwn(port
->fcs_port
)));
943 fc_host_port_name(host
) =
944 bfa_os_htonll((bfa_fcs_port_get_pwwn(port
->fcs_port
)));
945 fc_host_max_npiv_vports(host
) = bfa_lps_get_max_vport(&bfad
->bfa
);
947 fc_host_supported_classes(host
) = FC_COS_CLASS3
;
949 memset(fc_host_supported_fc4s(host
), 0,
950 sizeof(fc_host_supported_fc4s(host
)));
951 if (bfad_supported_fc4s
& (BFA_PORT_ROLE_FCP_IM
| BFA_PORT_ROLE_FCP_TM
))
952 /* For FCP type 0x08 */
953 fc_host_supported_fc4s(host
)[2] = 1;
954 if (bfad_supported_fc4s
& BFA_PORT_ROLE_FCP_IPFC
)
955 /* For LLC/SNAP type 0x05 */
956 fc_host_supported_fc4s(host
)[3] = 0x20;
957 /* For fibre channel services type 0x20 */
958 fc_host_supported_fc4s(host
)[7] = 1;
960 bfa_get_adapter_model(&bfad
->bfa
, model
);
961 bfa_get_adapter_fw_ver(&bfad
->bfa
, fw_ver
);
962 sprintf(fc_host_symbolic_name(host
), "Brocade %s FV%s DV%s",
963 model
, fw_ver
, BFAD_DRIVER_VERSION
);
965 fc_host_supported_speeds(host
) = 0;
966 fc_host_supported_speeds(host
) |=
967 FC_PORTSPEED_8GBIT
| FC_PORTSPEED_4GBIT
| FC_PORTSPEED_2GBIT
|
970 bfa_fcport_get_attr(&bfad
->bfa
, &pattr
);
971 fc_host_maxframe_size(host
) = pattr
.pport_cfg
.maxfrsize
;
975 bfad_im_fc_rport_add(struct bfad_im_port_s
*im_port
, struct bfad_itnim_s
*itnim
)
977 struct fc_rport_identifiers rport_ids
;
978 struct fc_rport
*fc_rport
;
979 struct bfad_itnim_data_s
*itnim_data
;
981 rport_ids
.node_name
=
982 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim
->fcs_itnim
));
983 rport_ids
.port_name
=
984 bfa_os_htonll(bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
));
986 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
));
987 rport_ids
.roles
= FC_RPORT_ROLE_UNKNOWN
;
989 itnim
->fc_rport
= fc_rport
=
990 fc_remote_port_add(im_port
->shost
, 0, &rport_ids
);
995 fc_rport
->maxframe_size
=
996 bfa_fcs_itnim_get_maxfrsize(&itnim
->fcs_itnim
);
997 fc_rport
->supported_classes
= bfa_fcs_itnim_get_cos(&itnim
->fcs_itnim
);
999 itnim_data
= fc_rport
->dd_data
;
1000 itnim_data
->itnim
= itnim
;
1002 rport_ids
.roles
|= FC_RPORT_ROLE_FCP_TARGET
;
1004 if (rport_ids
.roles
!= FC_RPORT_ROLE_UNKNOWN
)
1005 fc_remote_port_rolechg(fc_rport
, rport_ids
.roles
);
1007 if ((fc_rport
->scsi_target_id
!= -1)
1008 && (fc_rport
->scsi_target_id
< MAX_FCP_TARGET
))
1009 itnim
->scsi_tgt_id
= fc_rport
->scsi_target_id
;
1015 * Work queue handler using FC transport service
1019 bfad_im_itnim_work_handler(struct work_struct
*work
)
1021 struct bfad_itnim_s
*itnim
= container_of(work
, struct bfad_itnim_s
,
1023 struct bfad_im_s
*im
= itnim
->im
;
1024 struct bfad_s
*bfad
= im
->bfad
;
1025 struct bfad_im_port_s
*im_port
;
1026 unsigned long flags
;
1027 struct fc_rport
*fc_rport
;
1030 char wwpn_str
[32], fcid_str
[16];
1032 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1033 im_port
= itnim
->im_port
;
1034 bfa_trc(bfad
, itnim
->state
);
1035 switch (itnim
->state
) {
1036 case ITNIM_STATE_ONLINE
:
1037 if (!itnim
->fc_rport
) {
1038 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1039 bfad_im_fc_rport_add(im_port
, itnim
);
1040 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1041 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
);
1042 fcid
= bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
);
1043 wwn2str(wwpn_str
, wwpn
);
1044 fcid2str(fcid_str
, fcid
);
1045 list_add_tail(&itnim
->list_entry
,
1046 &im_port
->itnim_mapped_list
);
1047 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_ONLINE
,
1048 im_port
->shost
->host_no
,
1050 fcid_str
, wwpn_str
);
1053 "%s: itnim %llx is already in online state\n",
1055 bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
));
1059 case ITNIM_STATE_OFFLINE_PENDING
:
1060 itnim
->state
= ITNIM_STATE_OFFLINE
;
1061 if (itnim
->fc_rport
) {
1062 fc_rport
= itnim
->fc_rport
;
1063 ((struct bfad_itnim_data_s
*)
1064 fc_rport
->dd_data
)->itnim
= NULL
;
1065 itnim
->fc_rport
= NULL
;
1066 if (!(im_port
->port
->flags
& BFAD_PORT_DELETE
)) {
1067 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1068 fc_rport
->dev_loss_tmo
=
1069 bfa_fcpim_path_tov_get(&bfad
->bfa
) + 1;
1070 fc_remote_port_delete(fc_rport
);
1071 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1073 wwpn
= bfa_fcs_itnim_get_pwwn(&itnim
->fcs_itnim
);
1074 fcid
= bfa_fcs_itnim_get_fcid(&itnim
->fcs_itnim
);
1075 wwn2str(wwpn_str
, wwpn
);
1076 fcid2str(fcid_str
, fcid
);
1077 list_del(&itnim
->list_entry
);
1078 bfa_log(bfad
->logmod
, BFA_LOG_LINUX_ITNIM_OFFLINE
,
1079 im_port
->shost
->host_no
,
1081 fcid_str
, wwpn_str
);
1084 case ITNIM_STATE_FREE
:
1085 if (itnim
->fc_rport
) {
1086 fc_rport
= itnim
->fc_rport
;
1087 ((struct bfad_itnim_data_s
*)
1088 fc_rport
->dd_data
)->itnim
= NULL
;
1089 itnim
->fc_rport
= NULL
;
1090 if (!(im_port
->port
->flags
& BFAD_PORT_DELETE
)) {
1091 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1092 fc_rport
->dev_loss_tmo
=
1093 bfa_fcpim_path_tov_get(&bfad
->bfa
) + 1;
1094 fc_remote_port_delete(fc_rport
);
1095 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1097 list_del(&itnim
->list_entry
);
1107 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1111 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1114 bfad_im_queuecommand(struct scsi_cmnd
*cmnd
, void (*done
) (struct scsi_cmnd
*))
1116 struct bfad_im_port_s
*im_port
=
1117 (struct bfad_im_port_s
*) cmnd
->device
->host
->hostdata
[0];
1118 struct bfad_s
*bfad
= im_port
->bfad
;
1119 struct bfad_itnim_data_s
*itnim_data
= cmnd
->device
->hostdata
;
1120 struct bfad_itnim_s
*itnim
;
1121 struct bfa_ioim_s
*hal_io
;
1122 unsigned long flags
;
1125 struct fc_rport
*rport
= starget_to_rport(scsi_target(cmnd
->device
));
1127 rc
= fc_remote_port_chkready(rport
);
1134 sg_cnt
= scsi_dma_map(cmnd
);
1137 return SCSI_MLQUEUE_HOST_BUSY
;
1139 cmnd
->scsi_done
= done
;
1141 spin_lock_irqsave(&bfad
->bfad_lock
, flags
);
1142 if (!(bfad
->bfad_flags
& BFAD_HAL_START_DONE
)) {
1144 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1145 bfad
->inst_no
, cmnd
, cmnd
->cmnd
[0]);
1146 cmnd
->result
= ScsiResult(DID_NO_CONNECT
, 0);
1150 itnim
= itnim_data
->itnim
;
1152 cmnd
->result
= ScsiResult(DID_IMM_RETRY
, 0);
1156 hal_io
= bfa_ioim_alloc(&bfad
->bfa
, (struct bfad_ioim_s
*) cmnd
,
1157 itnim
->bfa_itnim
, sg_cnt
);
1159 printk(KERN_WARNING
"hal_io failure\n");
1160 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1161 scsi_dma_unmap(cmnd
);
1162 return SCSI_MLQUEUE_HOST_BUSY
;
1165 cmnd
->host_scribble
= (char *)hal_io
;
1166 bfa_trc_fp(bfad
, hal_io
->iotag
);
1167 bfa_ioim_start(hal_io
);
1168 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1173 spin_unlock_irqrestore(&bfad
->bfad_lock
, flags
);
1174 scsi_dma_unmap(cmnd
);
1182 bfad_os_rport_online_wait(struct bfad_s
*bfad
)
1185 int rport_delay
= 10;
1187 for (i
= 0; !(bfad
->bfad_flags
& BFAD_PORT_ONLINE
)
1188 && i
< bfa_linkup_delay
; i
++)
1189 schedule_timeout_uninterruptible(HZ
);
1191 if (bfad
->bfad_flags
& BFAD_PORT_ONLINE
) {
1192 rport_delay
= rport_delay
< bfa_linkup_delay
?
1193 rport_delay
: bfa_linkup_delay
;
1194 for (i
= 0; !(bfad
->bfad_flags
& BFAD_RPORT_ONLINE
)
1195 && i
< rport_delay
; i
++)
1196 schedule_timeout_uninterruptible(HZ
);
1198 if (rport_delay
> 0 && (bfad
->bfad_flags
& BFAD_RPORT_ONLINE
))
1199 schedule_timeout_uninterruptible(rport_delay
* HZ
);
1204 bfad_os_get_linkup_delay(struct bfad_s
*bfad
)
1212 * Querying for the boot target port wwns
1213 * -- read from boot information in flash.
1214 * If nwwns > 0 => boot over SAN and set bfa_linkup_delay = 30
1215 * else => local boot machine set bfa_linkup_delay = 10
1218 bfa_iocfc_get_bootwwns(&bfad
->bfa
, &nwwns
, &wwns
);
1221 /* If boot over SAN; linkup_delay = 30sec */
1224 /* If local boot; linkup_delay = 10sec */