2 * Copyright (c) 2005-2010 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 "bfa_cb_ioim.h"
27 BFA_TRC_FILE(LDRV, IM);
29 DEFINE_IDR(bfad_im_port_index);
30 struct scsi_transport_template *bfad_im_scsi_transport_template;
31 struct scsi_transport_template *bfad_im_scsi_vport_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);
36 static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
37 struct bfad_itnim_s *itnim);
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);
63 bfa_trc(bfad, residue);
64 scsi_set_resid(cmnd, residue);
65 if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
66 (scsi_bufflen(cmnd) - residue) <
69 host_status = DID_ERROR;
72 cmnd->result = ScsiResult(host_status, scsi_status);
76 case BFI_IOIM_STS_ABORTED:
77 case BFI_IOIM_STS_TIMEDOUT:
78 case BFI_IOIM_STS_PATHTOV:
80 host_status = DID_ERROR;
81 cmnd->result = ScsiResult(host_status, 0);
84 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
85 if (cmnd->device->host != NULL)
88 cmnd->host_scribble = NULL;
89 bfa_trc(bfad, cmnd->result);
91 itnim_data = cmnd->device->hostdata;
93 itnim = itnim_data->itnim;
94 if (!cmnd->result && itnim &&
95 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
96 /* Queue depth adjustment for good status completion */
97 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
98 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
100 bfad_os_handle_qfull(itnim, cmnd->device);
104 cmnd->scsi_done(cmnd);
108 bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
110 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
111 struct bfad_itnim_data_s *itnim_data;
112 struct bfad_itnim_s *itnim;
114 cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
116 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
117 if (cmnd->device->host != NULL)
118 scsi_dma_unmap(cmnd);
120 cmnd->host_scribble = NULL;
122 /* Queue depth adjustment */
123 if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
124 itnim_data = cmnd->device->hostdata;
126 itnim = itnim_data->itnim;
128 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
132 cmnd->scsi_done(cmnd);
136 bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
138 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
139 struct bfad_s *bfad = drv;
141 cmnd->result = ScsiResult(DID_ERROR, 0);
143 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
144 if (cmnd->device->host != NULL)
145 scsi_dma_unmap(cmnd);
147 bfa_trc(bfad, cmnd->result);
148 cmnd->host_scribble = NULL;
152 bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
153 enum bfi_tskim_status tsk_status)
155 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
156 wait_queue_head_t *wq;
158 cmnd->SCp.Status |= tsk_status << 1;
159 set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
160 wq = (wait_queue_head_t *) cmnd->SCp.ptr;
161 cmnd->SCp.ptr = NULL;
168 * Scsi_Host_template SCSI host template
171 * Scsi_Host template entry, returns BFAD PCI info.
174 bfad_im_info(struct Scsi_Host *shost)
176 static char bfa_buf[256];
177 struct bfad_im_port_s *im_port =
178 (struct bfad_im_port_s *) shost->hostdata[0];
179 struct bfad_s *bfad = im_port->bfad;
180 struct bfa_s *bfa = &bfad->bfa;
181 struct bfa_ioc_s *ioc = &bfa->ioc;
182 char model[BFA_ADAPTER_MODEL_NAME_LEN];
184 bfa_get_adapter_model(bfa, model);
186 memset(bfa_buf, 0, sizeof(bfa_buf));
188 snprintf(bfa_buf, sizeof(bfa_buf),
189 "Brocade FCOE Adapter, " "model: %s hwpath: %s driver: %s",
190 model, bfad->pci_name, BFAD_DRIVER_VERSION);
192 snprintf(bfa_buf, sizeof(bfa_buf),
193 "Brocade FC Adapter, " "model: %s hwpath: %s driver: %s",
194 model, bfad->pci_name, BFAD_DRIVER_VERSION);
200 * Scsi_Host template entry, aborts the specified SCSI command.
202 * Returns: SUCCESS or FAILED.
205 bfad_im_abort_handler(struct scsi_cmnd *cmnd)
207 struct Scsi_Host *shost = cmnd->device->host;
208 struct bfad_im_port_s *im_port =
209 (struct bfad_im_port_s *) shost->hostdata[0];
210 struct bfad_s *bfad = im_port->bfad;
211 struct bfa_ioim_s *hal_io;
216 spin_lock_irqsave(&bfad->bfad_lock, flags);
217 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
219 /* IO has been completed, retrun success */
223 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
228 bfa_trc(bfad, hal_io->iotag);
229 BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n",
230 im_port->shost->host_no, cmnd, hal_io->iotag);
231 (void) bfa_ioim_abort(hal_io);
232 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
234 /* Need to wait until the command get aborted */
236 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
237 set_current_state(TASK_UNINTERRUPTIBLE);
238 schedule_timeout(timeout);
239 if (timeout < 4 * HZ)
243 cmnd->scsi_done(cmnd);
244 bfa_trc(bfad, hal_io->iotag);
245 BFA_LOG(KERN_INFO, bfad, log_level,
246 "scsi%d: complete abort 0x%p iotag 0x%x\n",
247 im_port->shost->host_no, cmnd, hal_io->iotag);
250 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
255 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
256 struct bfad_itnim_s *itnim)
258 struct bfa_tskim_s *tskim;
259 struct bfa_itnim_s *bfa_itnim;
260 bfa_status_t rc = BFA_STATUS_OK;
262 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
264 BFA_LOG(KERN_ERR, bfad, log_level,
265 "target reset, fail to allocate tskim\n");
266 rc = BFA_STATUS_FAILED;
271 * Set host_scribble to NULL to avoid aborting a task command if
274 cmnd->host_scribble = NULL;
275 cmnd->SCp.Status = 0;
276 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
277 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
278 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
284 * Scsi_Host template entry, resets a LUN and abort its all commands.
286 * Returns: SUCCESS or FAILED.
290 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
292 struct Scsi_Host *shost = cmnd->device->host;
293 struct bfad_im_port_s *im_port =
294 (struct bfad_im_port_s *) shost->hostdata[0];
295 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
296 struct bfad_s *bfad = im_port->bfad;
297 struct bfa_tskim_s *tskim;
298 struct bfad_itnim_s *itnim;
299 struct bfa_itnim_s *bfa_itnim;
300 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
303 enum bfi_tskim_status task_status;
305 spin_lock_irqsave(&bfad->bfad_lock, flags);
306 itnim = itnim_data->itnim;
308 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
313 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
315 BFA_LOG(KERN_ERR, bfad, log_level,
316 "LUN reset, fail to allocate tskim");
317 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
323 * Set host_scribble to NULL to avoid aborting a task command
326 cmnd->host_scribble = NULL;
327 cmnd->SCp.ptr = (char *)&wq;
328 cmnd->SCp.Status = 0;
329 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
330 bfa_tskim_start(tskim, bfa_itnim,
331 bfad_int_to_lun(cmnd->device->lun),
332 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
333 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
335 wait_event(wq, test_bit(IO_DONE_BIT,
336 (unsigned long *)&cmnd->SCp.Status));
338 task_status = cmnd->SCp.Status >> 1;
339 if (task_status != BFI_TSKIM_STS_OK) {
340 BFA_LOG(KERN_ERR, bfad, log_level,
341 "LUN reset failure, status: %d\n", task_status);
350 * Scsi_Host template entry, resets the bus and abort all commands.
353 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
355 struct Scsi_Host *shost = cmnd->device->host;
356 struct bfad_im_port_s *im_port =
357 (struct bfad_im_port_s *) shost->hostdata[0];
358 struct bfad_s *bfad = im_port->bfad;
359 struct bfad_itnim_s *itnim;
361 u32 i, rc, err_cnt = 0;
362 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
363 enum bfi_tskim_status task_status;
365 spin_lock_irqsave(&bfad->bfad_lock, flags);
366 for (i = 0; i < MAX_FCP_TARGET; i++) {
367 itnim = bfad_os_get_itnim(im_port, i);
369 cmnd->SCp.ptr = (char *)&wq;
370 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
371 if (rc != BFA_STATUS_OK) {
376 /* wait target reset to complete */
377 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
378 wait_event(wq, test_bit(IO_DONE_BIT,
379 (unsigned long *)&cmnd->SCp.Status));
380 spin_lock_irqsave(&bfad->bfad_lock, flags);
382 task_status = cmnd->SCp.Status >> 1;
383 if (task_status != BFI_TSKIM_STS_OK) {
384 BFA_LOG(KERN_ERR, bfad, log_level,
385 "target reset failure,"
386 " status: %d\n", task_status);
391 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
400 * Scsi_Host template entry slave_destroy.
403 bfad_im_slave_destroy(struct scsi_device *sdev)
405 sdev->hostdata = NULL;
410 * BFA FCS itnim callbacks
414 * BFA FCS itnim alloc callback, after successful PRLI
418 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
419 struct bfad_itnim_s **itnim_drv)
421 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
422 if (*itnim_drv == NULL)
425 (*itnim_drv)->im = bfad->im;
426 *itnim = &(*itnim_drv)->fcs_itnim;
427 (*itnim_drv)->state = ITNIM_STATE_NONE;
430 * Initiaze the itnim_work
432 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
433 bfad->bfad_flags |= BFAD_RPORT_ONLINE;
437 * BFA FCS itnim free callback.
438 * Context: Interrupt. bfad_lock is held
441 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
443 struct bfad_port_s *port;
446 char wwpn_str[32], fcid_str[16];
447 struct bfad_im_s *im = itnim_drv->im;
449 /* online to free state transtion should not happen */
450 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
452 itnim_drv->queue_work = 1;
453 /* offline request is not yet done, use the same request to free */
454 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
455 itnim_drv->queue_work = 0;
457 itnim_drv->state = ITNIM_STATE_FREE;
458 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
459 itnim_drv->im_port = port->im_port;
460 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
461 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
462 wwn2str(wwpn_str, wwpn);
463 fcid2str(fcid_str, fcid);
464 BFA_LOG(KERN_INFO, bfad, log_level,
465 "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
466 port->im_port->shost->host_no,
469 /* ITNIM processing */
470 if (itnim_drv->queue_work)
471 queue_work(im->drv_workq, &itnim_drv->itnim_work);
475 * BFA FCS itnim online callback.
476 * Context: Interrupt. bfad_lock is held
479 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
481 struct bfad_port_s *port;
482 struct bfad_im_s *im = itnim_drv->im;
484 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
485 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
486 itnim_drv->state = ITNIM_STATE_ONLINE;
487 itnim_drv->queue_work = 1;
488 itnim_drv->im_port = port->im_port;
490 /* ITNIM processing */
491 if (itnim_drv->queue_work)
492 queue_work(im->drv_workq, &itnim_drv->itnim_work);
496 * BFA FCS itnim offline callback.
497 * Context: Interrupt. bfad_lock is held
500 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
502 struct bfad_port_s *port;
504 struct bfad_im_s *im = itnim_drv->im;
506 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
508 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
509 (port->flags & BFAD_PORT_DELETE)) {
510 itnim_drv->state = ITNIM_STATE_OFFLINE;
513 itnim_drv->im_port = port->im_port;
514 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
515 itnim_drv->queue_work = 1;
517 /* ITNIM processing */
518 if (itnim_drv->queue_work)
519 queue_work(im->drv_workq, &itnim_drv->itnim_work);
523 * Allocate a Scsi_Host for a port.
526 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
531 mutex_lock(&bfad_mutex);
532 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
533 mutex_unlock(&bfad_mutex);
534 printk(KERN_WARNING "idr_pre_get failure\n");
538 error = idr_get_new(&bfad_im_port_index, im_port,
541 mutex_unlock(&bfad_mutex);
542 printk(KERN_WARNING "idr_get_new failure\n");
546 mutex_unlock(&bfad_mutex);
548 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
549 if (!im_port->shost) {
554 im_port->shost->hostdata[0] = (unsigned long)im_port;
555 im_port->shost->unique_id = im_port->idr_id;
556 im_port->shost->this_id = -1;
557 im_port->shost->max_id = MAX_FCP_TARGET;
558 im_port->shost->max_lun = MAX_FCP_LUN;
559 im_port->shost->max_cmd_len = 16;
560 im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
561 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
562 im_port->shost->transportt = bfad_im_scsi_transport_template;
564 im_port->shost->transportt =
565 bfad_im_scsi_vport_transport_template;
567 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
569 printk(KERN_WARNING "scsi_add_host failure %d\n", error);
573 /* setup host fixed attribute if the lk supports */
574 bfad_os_fc_host_init(im_port);
579 scsi_host_put(im_port->shost);
580 im_port->shost = NULL;
582 mutex_lock(&bfad_mutex);
583 idr_remove(&bfad_im_port_index, im_port->idr_id);
584 mutex_unlock(&bfad_mutex);
590 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
592 bfa_trc(bfad, bfad->inst_no);
593 BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n",
594 im_port->shost->host_no);
596 fc_remove_host(im_port->shost);
598 scsi_remove_host(im_port->shost);
599 scsi_host_put(im_port->shost);
601 mutex_lock(&bfad_mutex);
602 idr_remove(&bfad_im_port_index, im_port->idr_id);
603 mutex_unlock(&bfad_mutex);
607 bfad_im_port_delete_handler(struct work_struct *work)
609 struct bfad_im_port_s *im_port =
610 container_of(work, struct bfad_im_port_s, port_delete_work);
612 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
613 im_port->flags |= BFAD_PORT_DELETE;
614 fc_vport_terminate(im_port->fc_vport);
619 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
621 int rc = BFA_STATUS_OK;
622 struct bfad_im_port_s *im_port;
624 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
625 if (im_port == NULL) {
626 rc = BFA_STATUS_ENOMEM;
629 port->im_port = im_port;
630 im_port->port = port;
631 im_port->bfad = bfad;
633 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
634 INIT_LIST_HEAD(&im_port->itnim_mapped_list);
635 INIT_LIST_HEAD(&im_port->binding_list);
642 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
644 struct bfad_im_port_s *im_port = port->im_port;
646 queue_work(bfad->im->drv_workq,
647 &im_port->port_delete_work);
651 bfad_im_port_clean(struct bfad_im_port_s *im_port)
653 struct bfad_fcp_binding *bp, *bp_new;
655 struct bfad_s *bfad = im_port->bfad;
657 spin_lock_irqsave(&bfad->bfad_lock, flags);
658 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
660 list_del(&bp->list_entry);
664 /* the itnim_mapped_list must be empty at this time */
665 bfa_assert(list_empty(&im_port->itnim_mapped_list));
667 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
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 flush_workqueue(im->drv_workq);
734 destroy_workqueue(im->drv_workq);
735 im->drv_workq = NULL;
740 bfad_os_thread_workq(struct bfad_s *bfad)
742 struct bfad_im_s *im = bfad->im;
745 snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
747 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
749 return BFA_STATUS_FAILED;
751 return BFA_STATUS_OK;
755 * Scsi_Host template entry.
758 * OS entry point to adjust the queue_depths on a per-device basis.
759 * Called once per device during the bus scan.
760 * Return non-zero if fails.
763 bfad_im_slave_configure(struct scsi_device *sdev)
765 if (sdev->tagged_supported)
766 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
768 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
773 struct scsi_host_template bfad_im_scsi_host_template = {
774 .module = THIS_MODULE,
775 .name = BFAD_DRIVER_NAME,
776 .info = bfad_im_info,
777 .queuecommand = bfad_im_queuecommand,
778 .eh_abort_handler = bfad_im_abort_handler,
779 .eh_device_reset_handler = bfad_im_reset_lun_handler,
780 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
782 .slave_alloc = bfad_im_slave_alloc,
783 .slave_configure = bfad_im_slave_configure,
784 .slave_destroy = bfad_im_slave_destroy,
787 .sg_tablesize = BFAD_IO_MAX_SGE,
789 .use_clustering = ENABLE_CLUSTERING,
790 .shost_attrs = bfad_im_host_attrs,
791 .max_sectors = 0xFFFF,
794 struct scsi_host_template bfad_im_vport_template = {
795 .module = THIS_MODULE,
796 .name = BFAD_DRIVER_NAME,
797 .info = bfad_im_info,
798 .queuecommand = bfad_im_queuecommand,
799 .eh_abort_handler = bfad_im_abort_handler,
800 .eh_device_reset_handler = bfad_im_reset_lun_handler,
801 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
803 .slave_alloc = bfad_im_slave_alloc,
804 .slave_configure = bfad_im_slave_configure,
805 .slave_destroy = bfad_im_slave_destroy,
808 .sg_tablesize = BFAD_IO_MAX_SGE,
810 .use_clustering = ENABLE_CLUSTERING,
811 .shost_attrs = bfad_im_vport_attrs,
812 .max_sectors = 0xFFFF,
816 bfad_im_module_init(void)
818 bfad_im_scsi_transport_template =
819 fc_attach_transport(&bfad_im_fc_function_template);
820 if (!bfad_im_scsi_transport_template)
821 return BFA_STATUS_ENOMEM;
823 bfad_im_scsi_vport_transport_template =
824 fc_attach_transport(&bfad_im_vport_fc_function_template);
825 if (!bfad_im_scsi_vport_transport_template) {
826 fc_release_transport(bfad_im_scsi_transport_template);
827 return BFA_STATUS_ENOMEM;
830 return BFA_STATUS_OK;
834 bfad_im_module_exit(void)
836 if (bfad_im_scsi_transport_template)
837 fc_release_transport(bfad_im_scsi_transport_template);
839 if (bfad_im_scsi_vport_transport_template)
840 fc_release_transport(bfad_im_scsi_vport_transport_template);
844 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
846 struct scsi_device *tmp_sdev;
848 if (((jiffies - itnim->last_ramp_up_time) >
849 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
850 ((jiffies - itnim->last_queue_full_time) >
851 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
852 shost_for_each_device(tmp_sdev, sdev->host) {
853 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
854 if (tmp_sdev->id != sdev->id)
856 if (tmp_sdev->ordered_tags)
857 scsi_adjust_queue_depth(tmp_sdev,
859 tmp_sdev->queue_depth + 1);
861 scsi_adjust_queue_depth(tmp_sdev,
863 tmp_sdev->queue_depth + 1);
865 itnim->last_ramp_up_time = jiffies;
872 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
874 struct scsi_device *tmp_sdev;
876 itnim->last_queue_full_time = jiffies;
878 shost_for_each_device(tmp_sdev, sdev->host) {
879 if (tmp_sdev->id != sdev->id)
881 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
885 struct bfad_itnim_s *
886 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
888 struct bfad_itnim_s *itnim = NULL;
890 /* Search the mapped list for this target ID */
891 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
892 if (id == itnim->scsi_tgt_id)
900 * Scsi_Host template entry slave_alloc
903 bfad_im_slave_alloc(struct scsi_device *sdev)
905 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
907 if (!rport || fc_remote_port_chkready(rport))
910 sdev->hostdata = rport->dd_data;
916 bfad_im_supported_speeds(struct bfa_s *bfa)
918 struct bfa_ioc_attr_s *ioc_attr;
919 u32 supported_speed = 0;
921 ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
925 bfa_get_attr(bfa, ioc_attr);
926 if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
927 if (ioc_attr->adapter_attr.is_mezz) {
928 supported_speed |= FC_PORTSPEED_8GBIT |
930 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
932 supported_speed |= FC_PORTSPEED_8GBIT |
936 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
937 supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
939 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
940 supported_speed |= FC_PORTSPEED_10GBIT;
943 return supported_speed;
947 bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
949 struct Scsi_Host *host = im_port->shost;
950 struct bfad_s *bfad = im_port->bfad;
951 struct bfad_port_s *port = im_port->port;
952 char symname[BFA_SYMNAME_MAXLEN];
953 struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
955 fc_host_node_name(host) =
956 cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
957 fc_host_port_name(host) =
958 cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
959 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
961 fc_host_supported_classes(host) = FC_COS_CLASS3;
963 memset(fc_host_supported_fc4s(host), 0,
964 sizeof(fc_host_supported_fc4s(host)));
965 if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
966 /* For FCP type 0x08 */
967 fc_host_supported_fc4s(host)[2] = 1;
968 /* For fibre channel services type 0x20 */
969 fc_host_supported_fc4s(host)[7] = 1;
971 strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
973 sprintf(fc_host_symbolic_name(host), "%s", symname);
975 fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
976 fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
980 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
982 struct fc_rport_identifiers rport_ids;
983 struct fc_rport *fc_rport;
984 struct bfad_itnim_data_s *itnim_data;
986 rport_ids.node_name =
987 cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
988 rport_ids.port_name =
989 cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
991 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
992 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
994 itnim->fc_rport = fc_rport =
995 fc_remote_port_add(im_port->shost, 0, &rport_ids);
1000 fc_rport->maxframe_size =
1001 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1002 fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1004 itnim_data = fc_rport->dd_data;
1005 itnim_data->itnim = itnim;
1007 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1009 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1010 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1012 if ((fc_rport->scsi_target_id != -1)
1013 && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1014 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1020 * Work queue handler using FC transport service
1024 bfad_im_itnim_work_handler(struct work_struct *work)
1026 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1028 struct bfad_im_s *im = itnim->im;
1029 struct bfad_s *bfad = im->bfad;
1030 struct bfad_im_port_s *im_port;
1031 unsigned long flags;
1032 struct fc_rport *fc_rport;
1035 char wwpn_str[32], fcid_str[16];
1037 spin_lock_irqsave(&bfad->bfad_lock, flags);
1038 im_port = itnim->im_port;
1039 bfa_trc(bfad, itnim->state);
1040 switch (itnim->state) {
1041 case ITNIM_STATE_ONLINE:
1042 if (!itnim->fc_rport) {
1043 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1044 bfad_im_fc_rport_add(im_port, itnim);
1045 spin_lock_irqsave(&bfad->bfad_lock, flags);
1046 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1047 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1048 wwn2str(wwpn_str, wwpn);
1049 fcid2str(fcid_str, fcid);
1050 list_add_tail(&itnim->list_entry,
1051 &im_port->itnim_mapped_list);
1052 BFA_LOG(KERN_INFO, bfad, log_level,
1053 "ITNIM ONLINE Target: %d:0:%d "
1054 "FCID: %s WWPN: %s\n",
1055 im_port->shost->host_no,
1057 fcid_str, wwpn_str);
1060 "%s: itnim %llx is already in online state\n",
1062 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1066 case ITNIM_STATE_OFFLINE_PENDING:
1067 itnim->state = ITNIM_STATE_OFFLINE;
1068 if (itnim->fc_rport) {
1069 fc_rport = itnim->fc_rport;
1070 ((struct bfad_itnim_data_s *)
1071 fc_rport->dd_data)->itnim = NULL;
1072 itnim->fc_rport = NULL;
1073 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1074 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1075 fc_rport->dev_loss_tmo =
1076 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1077 fc_remote_port_delete(fc_rport);
1078 spin_lock_irqsave(&bfad->bfad_lock, flags);
1080 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1081 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1082 wwn2str(wwpn_str, wwpn);
1083 fcid2str(fcid_str, fcid);
1084 list_del(&itnim->list_entry);
1085 BFA_LOG(KERN_INFO, bfad, log_level,
1086 "ITNIM OFFLINE Target: %d:0:%d "
1087 "FCID: %s WWPN: %s\n",
1088 im_port->shost->host_no,
1090 fcid_str, wwpn_str);
1093 case ITNIM_STATE_FREE:
1094 if (itnim->fc_rport) {
1095 fc_rport = itnim->fc_rport;
1096 ((struct bfad_itnim_data_s *)
1097 fc_rport->dd_data)->itnim = NULL;
1098 itnim->fc_rport = NULL;
1099 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1100 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1101 fc_rport->dev_loss_tmo =
1102 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1103 fc_remote_port_delete(fc_rport);
1104 spin_lock_irqsave(&bfad->bfad_lock, flags);
1106 list_del(&itnim->list_entry);
1116 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1120 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1123 bfad_im_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1125 struct bfad_im_port_s *im_port =
1126 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1127 struct bfad_s *bfad = im_port->bfad;
1128 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1129 struct bfad_itnim_s *itnim;
1130 struct bfa_ioim_s *hal_io;
1131 unsigned long flags;
1134 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1136 rc = fc_remote_port_chkready(rport);
1143 sg_cnt = scsi_dma_map(cmnd);
1145 return SCSI_MLQUEUE_HOST_BUSY;
1147 cmnd->scsi_done = done;
1149 spin_lock_irqsave(&bfad->bfad_lock, flags);
1150 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1152 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1153 bfad->inst_no, cmnd, cmnd->cmnd[0]);
1154 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1159 itnim = itnim_data->itnim;
1161 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1165 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1166 itnim->bfa_itnim, sg_cnt);
1168 printk(KERN_WARNING "hal_io failure\n");
1169 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1170 scsi_dma_unmap(cmnd);
1171 return SCSI_MLQUEUE_HOST_BUSY;
1174 cmnd->host_scribble = (char *)hal_io;
1175 bfa_trc_fp(bfad, hal_io->iotag);
1176 bfa_ioim_start(hal_io);
1177 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1182 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1183 scsi_dma_unmap(cmnd);
1191 bfad_os_rport_online_wait(struct bfad_s *bfad)
1194 int rport_delay = 10;
1196 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1197 && i < bfa_linkup_delay; i++) {
1198 set_current_state(TASK_UNINTERRUPTIBLE);
1199 schedule_timeout(HZ);
1202 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1203 rport_delay = rport_delay < bfa_linkup_delay ?
1204 rport_delay : bfa_linkup_delay;
1205 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1206 && i < rport_delay; i++) {
1207 set_current_state(TASK_UNINTERRUPTIBLE);
1208 schedule_timeout(HZ);
1211 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1212 set_current_state(TASK_UNINTERRUPTIBLE);
1213 schedule_timeout(rport_delay * HZ);
1219 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1222 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
1226 * Querying for the boot target port wwns
1227 * -- read from boot information in flash.
1228 * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1229 * else => local boot machine set linkup_delay = 0
1232 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1235 /* If Boot over SAN set linkup_delay = 30sec */
1238 /* If local boot; no linkup_delay */
1241 return linkup_delay;