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;
46 u8 host_status = DID_OK;
50 bfa_trc(bfad, 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 bfa_trc(bfad, residue);
61 scsi_set_resid(cmnd, residue);
62 if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
63 (scsi_bufflen(cmnd) - residue) <
66 host_status = DID_ERROR;
69 cmnd->result = ScsiResult(host_status, scsi_status);
73 case BFI_IOIM_STS_ABORTED:
74 case BFI_IOIM_STS_TIMEDOUT:
75 case BFI_IOIM_STS_PATHTOV:
77 cmnd->result = ScsiResult(DID_ERROR, 0);
80 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
81 if (cmnd->device->host != NULL)
84 cmnd->host_scribble = NULL;
85 bfa_trc(bfad, cmnd->result);
87 itnim_data = cmnd->device->hostdata;
89 itnim = itnim_data->itnim;
90 if (!cmnd->result && itnim &&
91 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
92 /* Queue depth adjustment for good status completion */
93 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
94 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
96 bfad_os_handle_qfull(itnim, cmnd->device);
100 cmnd->scsi_done(cmnd);
104 bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
106 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
107 struct bfad_itnim_data_s *itnim_data;
108 struct bfad_itnim_s *itnim;
110 cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
112 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
113 if (cmnd->device->host != NULL)
114 scsi_dma_unmap(cmnd);
116 cmnd->host_scribble = NULL;
118 /* Queue depth adjustment */
119 if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
120 itnim_data = cmnd->device->hostdata;
122 itnim = itnim_data->itnim;
124 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
128 cmnd->scsi_done(cmnd);
132 bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
134 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
135 struct bfad_s *bfad = drv;
137 cmnd->result = ScsiResult(DID_ERROR, 0);
139 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
140 if (cmnd->device->host != NULL)
141 scsi_dma_unmap(cmnd);
143 bfa_trc(bfad, cmnd->result);
144 cmnd->host_scribble = NULL;
148 bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
149 enum bfi_tskim_status tsk_status)
151 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
152 wait_queue_head_t *wq;
154 cmnd->SCp.Status |= tsk_status << 1;
155 set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
156 wq = (wait_queue_head_t *) cmnd->SCp.ptr;
157 cmnd->SCp.ptr = NULL;
164 bfa_cb_ioim_resfree(void *drv)
169 * Scsi_Host_template SCSI host template
172 * Scsi_Host template entry, returns BFAD PCI info.
175 bfad_im_info(struct Scsi_Host *shost)
177 static char bfa_buf[256];
178 struct bfad_im_port_s *im_port =
179 (struct bfad_im_port_s *) shost->hostdata[0];
180 struct bfad_s *bfad = im_port->bfad;
181 char model[BFA_ADAPTER_MODEL_NAME_LEN];
183 bfa_get_adapter_model(&bfad->bfa, model);
185 memset(bfa_buf, 0, sizeof(bfa_buf));
186 snprintf(bfa_buf, sizeof(bfa_buf),
187 "Brocade FC/FCOE Adapter, " "model: %s hwpath: %s driver: %s",
188 model, bfad->pci_name, BFAD_DRIVER_VERSION);
193 * Scsi_Host template entry, aborts the specified SCSI command.
195 * Returns: SUCCESS or FAILED.
198 bfad_im_abort_handler(struct scsi_cmnd *cmnd)
200 struct Scsi_Host *shost = cmnd->device->host;
201 struct bfad_im_port_s *im_port =
202 (struct bfad_im_port_s *) shost->hostdata[0];
203 struct bfad_s *bfad = im_port->bfad;
204 struct bfa_ioim_s *hal_io;
209 spin_lock_irqsave(&bfad->bfad_lock, flags);
210 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
212 /* IO has been completed, retrun success */
216 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
221 bfa_trc(bfad, hal_io->iotag);
222 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT,
223 im_port->shost->host_no, cmnd, hal_io->iotag);
224 bfa_ioim_abort(hal_io);
225 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
227 /* Need to wait until the command get aborted */
229 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
230 set_current_state(TASK_UNINTERRUPTIBLE);
231 schedule_timeout(timeout);
232 if (timeout < 4 * HZ)
236 cmnd->scsi_done(cmnd);
237 bfa_trc(bfad, hal_io->iotag);
238 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT_COMP,
239 im_port->shost->host_no, cmnd, hal_io->iotag);
242 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
247 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
248 struct bfad_itnim_s *itnim)
250 struct bfa_tskim_s *tskim;
251 struct bfa_itnim_s *bfa_itnim;
252 bfa_status_t rc = BFA_STATUS_OK;
254 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
255 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
257 BFA_DEV_PRINTF(bfad, BFA_ERR,
258 "target reset, fail to allocate tskim\n");
259 rc = BFA_STATUS_FAILED;
264 * Set host_scribble to NULL to avoid aborting a task command if
267 cmnd->host_scribble = NULL;
268 cmnd->SCp.Status = 0;
269 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
270 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
271 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
277 * Scsi_Host template entry, resets a LUN and abort its all commands.
279 * Returns: SUCCESS or FAILED.
283 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
285 struct Scsi_Host *shost = cmnd->device->host;
286 struct bfad_im_port_s *im_port =
287 (struct bfad_im_port_s *) shost->hostdata[0];
288 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
289 struct bfad_s *bfad = im_port->bfad;
290 struct bfa_tskim_s *tskim;
291 struct bfad_itnim_s *itnim;
292 struct bfa_itnim_s *bfa_itnim;
293 DECLARE_WAIT_QUEUE_HEAD(wq);
296 enum bfi_tskim_status task_status;
298 spin_lock_irqsave(&bfad->bfad_lock, flags);
299 itnim = itnim_data->itnim;
301 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
306 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
308 BFA_DEV_PRINTF(bfad, BFA_ERR,
309 "LUN reset, fail to allocate tskim");
310 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
316 * Set host_scribble to NULL to avoid aborting a task command
319 cmnd->host_scribble = NULL;
320 cmnd->SCp.ptr = (char *)&wq;
321 cmnd->SCp.Status = 0;
322 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
323 bfa_tskim_start(tskim, bfa_itnim,
324 bfad_int_to_lun(cmnd->device->lun),
325 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
326 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
328 wait_event(wq, test_bit(IO_DONE_BIT,
329 (unsigned long *)&cmnd->SCp.Status));
331 task_status = cmnd->SCp.Status >> 1;
332 if (task_status != BFI_TSKIM_STS_OK) {
333 BFA_DEV_PRINTF(bfad, BFA_ERR, "LUN reset failure, status: %d\n",
343 * Scsi_Host template entry, resets the bus and abort all commands.
346 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
348 struct Scsi_Host *shost = cmnd->device->host;
349 struct bfad_im_port_s *im_port =
350 (struct bfad_im_port_s *) shost->hostdata[0];
351 struct bfad_s *bfad = im_port->bfad;
352 struct bfad_itnim_s *itnim;
354 u32 i, rc, err_cnt = 0;
355 DECLARE_WAIT_QUEUE_HEAD(wq);
356 enum bfi_tskim_status task_status;
358 spin_lock_irqsave(&bfad->bfad_lock, flags);
359 for (i = 0; i < MAX_FCP_TARGET; i++) {
360 itnim = bfad_os_get_itnim(im_port, i);
362 cmnd->SCp.ptr = (char *)&wq;
363 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
364 if (rc != BFA_STATUS_OK) {
369 /* wait target reset to complete */
370 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
371 wait_event(wq, test_bit(IO_DONE_BIT,
372 (unsigned long *)&cmnd->SCp.Status));
373 spin_lock_irqsave(&bfad->bfad_lock, flags);
375 task_status = cmnd->SCp.Status >> 1;
376 if (task_status != BFI_TSKIM_STS_OK) {
377 BFA_DEV_PRINTF(bfad, BFA_ERR,
378 "target reset failure,"
379 " status: %d\n", task_status);
384 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
393 * Scsi_Host template entry slave_destroy.
396 bfad_im_slave_destroy(struct scsi_device *sdev)
398 sdev->hostdata = NULL;
403 * BFA FCS itnim callbacks
407 * BFA FCS itnim alloc callback, after successful PRLI
411 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
412 struct bfad_itnim_s **itnim_drv)
414 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
415 if (*itnim_drv == NULL)
418 (*itnim_drv)->im = bfad->im;
419 *itnim = &(*itnim_drv)->fcs_itnim;
420 (*itnim_drv)->state = ITNIM_STATE_NONE;
423 * Initiaze the itnim_work
425 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
426 bfad->bfad_flags |= BFAD_RPORT_ONLINE;
430 * BFA FCS itnim free callback.
431 * Context: Interrupt. bfad_lock is held
434 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
436 struct bfad_port_s *port;
439 char wwpn_str[32], fcid_str[16];
441 /* online to free state transtion should not happen */
442 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
444 itnim_drv->queue_work = 1;
445 /* offline request is not yet done, use the same request to free */
446 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
447 itnim_drv->queue_work = 0;
449 itnim_drv->state = ITNIM_STATE_FREE;
450 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
451 itnim_drv->im_port = port->im_port;
452 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
453 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
454 wwn2str(wwpn_str, wwpn);
455 fcid2str(fcid_str, fcid);
456 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_FREE,
457 port->im_port->shost->host_no,
459 bfad_os_itnim_process(itnim_drv);
463 * BFA FCS itnim online callback.
464 * Context: Interrupt. bfad_lock is held
467 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
469 struct bfad_port_s *port;
471 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
472 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
473 itnim_drv->state = ITNIM_STATE_ONLINE;
474 itnim_drv->queue_work = 1;
475 itnim_drv->im_port = port->im_port;
476 bfad_os_itnim_process(itnim_drv);
480 * BFA FCS itnim offline callback.
481 * Context: Interrupt. bfad_lock is held
484 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
486 struct bfad_port_s *port;
489 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
491 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
492 (port->flags & BFAD_PORT_DELETE)) {
493 itnim_drv->state = ITNIM_STATE_OFFLINE;
496 itnim_drv->im_port = port->im_port;
497 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
498 itnim_drv->queue_work = 1;
499 bfad_os_itnim_process(itnim_drv);
503 * BFA FCS itnim timeout callback.
504 * Context: Interrupt. bfad_lock is held
506 void bfa_fcb_itnim_tov(struct bfad_itnim_s *itnim)
508 itnim->state = ITNIM_STATE_TIMEOUT;
512 * Allocate a Scsi_Host for a port.
515 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
519 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
520 printk(KERN_WARNING "idr_pre_get failure\n");
524 error = idr_get_new(&bfad_im_port_index, im_port,
527 printk(KERN_WARNING "idr_get_new failure\n");
531 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
532 if (!im_port->shost) {
537 im_port->shost->hostdata[0] = (unsigned long)im_port;
538 im_port->shost->unique_id = im_port->idr_id;
539 im_port->shost->this_id = -1;
540 im_port->shost->max_id = MAX_FCP_TARGET;
541 im_port->shost->max_lun = MAX_FCP_LUN;
542 im_port->shost->max_cmd_len = 16;
543 im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
544 im_port->shost->transportt = bfad_im_scsi_transport_template;
546 error = bfad_os_scsi_add_host(im_port->shost, im_port, bfad);
548 printk(KERN_WARNING "bfad_os_scsi_add_host failure %d\n",
553 /* setup host fixed attribute if the lk supports */
554 bfad_os_fc_host_init(im_port);
559 scsi_host_put(im_port->shost);
561 idr_remove(&bfad_im_port_index, im_port->idr_id);
567 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
571 bfa_trc(bfad, bfad->inst_no);
572 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_HOST_FREE,
573 im_port->shost->host_no);
575 fc_remove_host(im_port->shost);
577 scsi_remove_host(im_port->shost);
578 scsi_host_put(im_port->shost);
580 spin_lock_irqsave(&bfad->bfad_lock, flags);
581 idr_remove(&bfad_im_port_index, im_port->idr_id);
582 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
586 bfad_im_port_delete_handler(struct work_struct *work)
588 struct bfad_im_port_s *im_port =
589 container_of(work, struct bfad_im_port_s, port_delete_work);
591 bfad_im_scsi_host_free(im_port->bfad, im_port);
592 bfad_im_port_clean(im_port);
597 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
599 int rc = BFA_STATUS_OK;
600 struct bfad_im_port_s *im_port;
602 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
603 if (im_port == NULL) {
604 rc = BFA_STATUS_ENOMEM;
607 port->im_port = im_port;
608 im_port->port = port;
609 im_port->bfad = bfad;
611 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
612 INIT_LIST_HEAD(&im_port->itnim_mapped_list);
613 INIT_LIST_HEAD(&im_port->binding_list);
620 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
622 struct bfad_im_port_s *im_port = port->im_port;
624 queue_work(bfad->im->drv_workq,
625 &im_port->port_delete_work);
629 bfad_im_port_clean(struct bfad_im_port_s *im_port)
631 struct bfad_fcp_binding *bp, *bp_new;
633 struct bfad_s *bfad = im_port->bfad;
635 spin_lock_irqsave(&bfad->bfad_lock, flags);
636 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
638 list_del(&bp->list_entry);
642 /* the itnim_mapped_list must be empty at this time */
643 bfa_assert(list_empty(&im_port->itnim_mapped_list));
645 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
649 bfad_im_port_online(struct bfad_s *bfad, struct bfad_port_s *port)
654 bfad_im_port_offline(struct bfad_s *bfad, struct bfad_port_s *port)
659 bfad_im_probe(struct bfad_s *bfad)
661 struct bfad_im_s *im;
662 bfa_status_t rc = BFA_STATUS_OK;
664 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
666 rc = BFA_STATUS_ENOMEM;
673 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
675 rc = BFA_STATUS_FAILED;
683 bfad_im_probe_undo(struct bfad_s *bfad)
686 bfad_os_destroy_workq(bfad->im);
696 bfad_os_scsi_add_host(struct Scsi_Host *shost, struct bfad_im_port_s *im_port,
701 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
702 dev = &bfad->pcidev->dev;
704 dev = &bfad->pport.im_port->shost->shost_gendev;
706 return scsi_add_host(shost, dev);
710 bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
712 struct scsi_host_template *sht;
714 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
715 sht = &bfad_im_scsi_host_template;
717 sht = &bfad_im_vport_template;
719 sht->sg_tablesize = bfad->cfg_data.io_max_sge;
721 return scsi_host_alloc(sht, sizeof(unsigned long));
725 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
727 flush_workqueue(bfad->im->drv_workq);
728 bfad_im_scsi_host_free(im_port->bfad, im_port);
729 bfad_im_port_clean(im_port);
734 bfad_os_destroy_workq(struct bfad_im_s *im)
736 if (im && im->drv_workq) {
737 destroy_workqueue(im->drv_workq);
738 im->drv_workq = NULL;
743 bfad_os_thread_workq(struct bfad_s *bfad)
745 struct bfad_im_s *im = bfad->im;
748 snprintf(im->drv_workq_name, BFAD_KOBJ_NAME_LEN, "bfad_wq_%d",
750 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
752 return BFA_STATUS_FAILED;
754 return BFA_STATUS_OK;
758 * Scsi_Host template entry.
761 * OS entry point to adjust the queue_depths on a per-device basis.
762 * Called once per device during the bus scan.
763 * Return non-zero if fails.
766 bfad_im_slave_configure(struct scsi_device *sdev)
768 if (sdev->tagged_supported)
769 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
771 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
776 struct scsi_host_template bfad_im_scsi_host_template = {
777 .module = THIS_MODULE,
778 .name = BFAD_DRIVER_NAME,
779 .info = bfad_im_info,
780 .queuecommand = bfad_im_queuecommand,
781 .eh_abort_handler = bfad_im_abort_handler,
782 .eh_device_reset_handler = bfad_im_reset_lun_handler,
783 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
785 .slave_alloc = bfad_im_slave_alloc,
786 .slave_configure = bfad_im_slave_configure,
787 .slave_destroy = bfad_im_slave_destroy,
790 .sg_tablesize = BFAD_IO_MAX_SGE,
792 .use_clustering = ENABLE_CLUSTERING,
793 .shost_attrs = bfad_im_host_attrs,
794 .max_sectors = 0xFFFF,
797 struct scsi_host_template bfad_im_vport_template = {
798 .module = THIS_MODULE,
799 .name = BFAD_DRIVER_NAME,
800 .info = bfad_im_info,
801 .queuecommand = bfad_im_queuecommand,
802 .eh_abort_handler = bfad_im_abort_handler,
803 .eh_device_reset_handler = bfad_im_reset_lun_handler,
804 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
806 .slave_alloc = bfad_im_slave_alloc,
807 .slave_configure = bfad_im_slave_configure,
808 .slave_destroy = bfad_im_slave_destroy,
811 .sg_tablesize = BFAD_IO_MAX_SGE,
813 .use_clustering = ENABLE_CLUSTERING,
814 .shost_attrs = bfad_im_vport_attrs,
815 .max_sectors = 0xFFFF,
819 bfad_im_probe_post(struct bfad_im_s *im)
821 flush_workqueue(im->drv_workq);
825 bfad_im_module_init(void)
827 bfad_im_scsi_transport_template =
828 fc_attach_transport(&bfad_im_fc_function_template);
829 if (!bfad_im_scsi_transport_template)
830 return BFA_STATUS_ENOMEM;
832 return BFA_STATUS_OK;
836 bfad_im_module_exit(void)
838 if (bfad_im_scsi_transport_template)
839 fc_release_transport(bfad_im_scsi_transport_template);
843 bfad_os_itnim_process(struct bfad_itnim_s *itnim_drv)
845 struct bfad_im_s *im = itnim_drv->im;
847 if (itnim_drv->queue_work)
848 queue_work(im->drv_workq, &itnim_drv->itnim_work);
852 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
854 struct scsi_device *tmp_sdev;
856 if (((jiffies - itnim->last_ramp_up_time) >
857 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
858 ((jiffies - itnim->last_queue_full_time) >
859 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
860 shost_for_each_device(tmp_sdev, sdev->host) {
861 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
862 if (tmp_sdev->id != sdev->id)
864 if (tmp_sdev->ordered_tags)
865 scsi_adjust_queue_depth(tmp_sdev,
867 tmp_sdev->queue_depth + 1);
869 scsi_adjust_queue_depth(tmp_sdev,
871 tmp_sdev->queue_depth + 1);
873 itnim->last_ramp_up_time = jiffies;
880 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
882 struct scsi_device *tmp_sdev;
884 itnim->last_queue_full_time = jiffies;
886 shost_for_each_device(tmp_sdev, sdev->host) {
887 if (tmp_sdev->id != sdev->id)
889 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
896 struct bfad_itnim_s *
897 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
899 struct bfad_itnim_s *itnim = NULL;
901 /* Search the mapped list for this target ID */
902 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
903 if (id == itnim->scsi_tgt_id)
911 * Scsi_Host template entry slave_alloc
914 bfad_im_slave_alloc(struct scsi_device *sdev)
916 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
918 if (!rport || fc_remote_port_chkready(rport))
921 sdev->hostdata = rport->dd_data;
927 bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
929 struct Scsi_Host *host = im_port->shost;
930 struct bfad_s *bfad = im_port->bfad;
931 struct bfad_port_s *port = im_port->port;
932 struct bfa_pport_attr_s pattr;
933 char model[BFA_ADAPTER_MODEL_NAME_LEN];
934 char fw_ver[BFA_VERSION_LEN];
936 fc_host_node_name(host) =
937 bfa_os_htonll((bfa_fcs_port_get_nwwn(port->fcs_port)));
938 fc_host_port_name(host) =
939 bfa_os_htonll((bfa_fcs_port_get_pwwn(port->fcs_port)));
941 fc_host_supported_classes(host) = FC_COS_CLASS3;
943 memset(fc_host_supported_fc4s(host), 0,
944 sizeof(fc_host_supported_fc4s(host)));
945 if (bfad_supported_fc4s & (BFA_PORT_ROLE_FCP_IM | BFA_PORT_ROLE_FCP_TM))
946 /* For FCP type 0x08 */
947 fc_host_supported_fc4s(host)[2] = 1;
948 if (bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IPFC)
949 /* For LLC/SNAP type 0x05 */
950 fc_host_supported_fc4s(host)[3] = 0x20;
951 /* For fibre channel services type 0x20 */
952 fc_host_supported_fc4s(host)[7] = 1;
954 bfa_get_adapter_model(&bfad->bfa, model);
955 bfa_get_adapter_fw_ver(&bfad->bfa, fw_ver);
956 sprintf(fc_host_symbolic_name(host), "Brocade %s FV%s DV%s",
957 model, fw_ver, BFAD_DRIVER_VERSION);
959 fc_host_supported_speeds(host) = 0;
960 fc_host_supported_speeds(host) |=
961 FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
964 bfa_fcport_get_attr(&bfad->bfa, &pattr);
965 fc_host_maxframe_size(host) = pattr.pport_cfg.maxfrsize;
969 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
971 struct fc_rport_identifiers rport_ids;
972 struct fc_rport *fc_rport;
973 struct bfad_itnim_data_s *itnim_data;
975 rport_ids.node_name =
976 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
977 rport_ids.port_name =
978 bfa_os_htonll(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
980 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
981 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
983 itnim->fc_rport = fc_rport =
984 fc_remote_port_add(im_port->shost, 0, &rport_ids);
989 fc_rport->maxframe_size =
990 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
991 fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
993 itnim_data = fc_rport->dd_data;
994 itnim_data->itnim = itnim;
996 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
998 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
999 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1001 if ((fc_rport->scsi_target_id != -1)
1002 && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1003 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1009 * Work queue handler using FC transport service
1013 bfad_im_itnim_work_handler(struct work_struct *work)
1015 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1017 struct bfad_im_s *im = itnim->im;
1018 struct bfad_s *bfad = im->bfad;
1019 struct bfad_im_port_s *im_port;
1020 unsigned long flags;
1021 struct fc_rport *fc_rport;
1024 char wwpn_str[32], fcid_str[16];
1026 spin_lock_irqsave(&bfad->bfad_lock, flags);
1027 im_port = itnim->im_port;
1028 bfa_trc(bfad, itnim->state);
1029 switch (itnim->state) {
1030 case ITNIM_STATE_ONLINE:
1031 if (!itnim->fc_rport) {
1032 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1033 bfad_im_fc_rport_add(im_port, itnim);
1034 spin_lock_irqsave(&bfad->bfad_lock, flags);
1035 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1036 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1037 wwn2str(wwpn_str, wwpn);
1038 fcid2str(fcid_str, fcid);
1039 list_add_tail(&itnim->list_entry,
1040 &im_port->itnim_mapped_list);
1041 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_ONLINE,
1042 im_port->shost->host_no,
1044 fcid_str, wwpn_str);
1047 "%s: itnim %llx is already in online state\n",
1049 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1053 case ITNIM_STATE_OFFLINE_PENDING:
1054 itnim->state = ITNIM_STATE_OFFLINE;
1055 if (itnim->fc_rport) {
1056 fc_rport = itnim->fc_rport;
1057 ((struct bfad_itnim_data_s *)
1058 fc_rport->dd_data)->itnim = NULL;
1059 itnim->fc_rport = NULL;
1060 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1061 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1062 fc_rport->dev_loss_tmo =
1063 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1064 fc_remote_port_delete(fc_rport);
1065 spin_lock_irqsave(&bfad->bfad_lock, flags);
1067 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1068 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1069 wwn2str(wwpn_str, wwpn);
1070 fcid2str(fcid_str, fcid);
1071 list_del(&itnim->list_entry);
1072 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_OFFLINE,
1073 im_port->shost->host_no,
1075 fcid_str, wwpn_str);
1078 case ITNIM_STATE_FREE:
1079 if (itnim->fc_rport) {
1080 fc_rport = itnim->fc_rport;
1081 ((struct bfad_itnim_data_s *)
1082 fc_rport->dd_data)->itnim = NULL;
1083 itnim->fc_rport = NULL;
1084 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1085 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1086 fc_rport->dev_loss_tmo =
1087 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1088 fc_remote_port_delete(fc_rport);
1089 spin_lock_irqsave(&bfad->bfad_lock, flags);
1091 list_del(&itnim->list_entry);
1101 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1105 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1108 bfad_im_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1110 struct bfad_im_port_s *im_port =
1111 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1112 struct bfad_s *bfad = im_port->bfad;
1113 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1114 struct bfad_itnim_s *itnim;
1115 struct bfa_ioim_s *hal_io;
1116 unsigned long flags;
1119 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1121 rc = fc_remote_port_chkready(rport);
1128 sg_cnt = scsi_dma_map(cmnd);
1131 return SCSI_MLQUEUE_HOST_BUSY;
1133 cmnd->scsi_done = done;
1135 spin_lock_irqsave(&bfad->bfad_lock, flags);
1136 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1138 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1139 bfad->inst_no, cmnd, cmnd->cmnd[0]);
1140 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1144 itnim = itnim_data->itnim;
1146 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1150 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1151 itnim->bfa_itnim, sg_cnt);
1153 printk(KERN_WARNING "hal_io failure\n");
1154 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1155 scsi_dma_unmap(cmnd);
1156 return SCSI_MLQUEUE_HOST_BUSY;
1159 cmnd->host_scribble = (char *)hal_io;
1160 bfa_trc_fp(bfad, hal_io->iotag);
1161 bfa_ioim_start(hal_io);
1162 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1167 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1168 scsi_dma_unmap(cmnd);
1176 bfad_os_rport_online_wait(struct bfad_s *bfad)
1179 int rport_delay = 10;
1181 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1182 && i < bfa_linkup_delay; i++)
1183 schedule_timeout_uninterruptible(HZ);
1185 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1186 rport_delay = rport_delay < bfa_linkup_delay ?
1187 rport_delay : bfa_linkup_delay;
1188 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1189 && i < rport_delay; i++)
1190 schedule_timeout_uninterruptible(HZ);
1192 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE))
1193 schedule_timeout_uninterruptible(rport_delay * HZ);
1198 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1206 * Querying for the boot target port wwns
1207 * -- read from boot information in flash.
1208 * If nwwns > 0 => boot over SAN and set bfa_linkup_delay = 30
1209 * else => local boot machine set bfa_linkup_delay = 10
1212 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, &wwns);
1215 /* If boot over SAN; linkup_delay = 30sec */
1218 /* If local boot; linkup_delay = 10sec */