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_Host *h, struct scsi_cmnd *cmnd);
34 static int bfad_im_slave_alloc(struct scsi_device *sdev);
35 static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
36 struct bfad_itnim_s *itnim);
39 bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
40 enum bfi_ioim_status io_status, u8 scsi_status,
41 int sns_len, u8 *sns_info, s32 residue)
43 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
44 struct bfad_s *bfad = drv;
45 struct bfad_itnim_data_s *itnim_data;
46 struct bfad_itnim_s *itnim;
47 u8 host_status = DID_OK;
51 bfa_trc(bfad, scsi_status);
52 scsi_set_resid(cmnd, 0);
55 bfa_trc(bfad, sns_len);
56 if (sns_len > SCSI_SENSE_BUFFERSIZE)
57 sns_len = SCSI_SENSE_BUFFERSIZE;
58 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 host_status = DID_ERROR;
80 cmnd->result = ScsiResult(host_status, 0);
83 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
84 if (cmnd->device->host != NULL)
87 cmnd->host_scribble = NULL;
88 bfa_trc(bfad, cmnd->result);
90 itnim_data = cmnd->device->hostdata;
92 itnim = itnim_data->itnim;
93 if (!cmnd->result && itnim &&
94 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
95 /* Queue depth adjustment for good status completion */
96 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
97 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
99 bfad_os_handle_qfull(itnim, cmnd->device);
103 cmnd->scsi_done(cmnd);
107 bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
109 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
110 struct bfad_itnim_data_s *itnim_data;
111 struct bfad_itnim_s *itnim;
113 cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
115 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
116 if (cmnd->device->host != NULL)
117 scsi_dma_unmap(cmnd);
119 cmnd->host_scribble = NULL;
121 /* Queue depth adjustment */
122 if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
123 itnim_data = cmnd->device->hostdata;
125 itnim = itnim_data->itnim;
127 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
131 cmnd->scsi_done(cmnd);
135 bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
137 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
138 struct bfad_s *bfad = drv;
140 cmnd->result = ScsiResult(DID_ERROR, 0);
142 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
143 if (cmnd->device->host != NULL)
144 scsi_dma_unmap(cmnd);
146 bfa_trc(bfad, cmnd->result);
147 cmnd->host_scribble = NULL;
151 bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
152 enum bfi_tskim_status tsk_status)
154 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
155 wait_queue_head_t *wq;
157 cmnd->SCp.Status |= tsk_status << 1;
158 set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
159 wq = (wait_queue_head_t *) cmnd->SCp.ptr;
160 cmnd->SCp.ptr = NULL;
167 * Scsi_Host_template SCSI host template
170 * Scsi_Host template entry, returns BFAD PCI info.
173 bfad_im_info(struct Scsi_Host *shost)
175 static char bfa_buf[256];
176 struct bfad_im_port_s *im_port =
177 (struct bfad_im_port_s *) shost->hostdata[0];
178 struct bfad_s *bfad = im_port->bfad;
179 struct bfa_s *bfa = &bfad->bfa;
180 struct bfa_ioc_s *ioc = &bfa->ioc;
181 char model[BFA_ADAPTER_MODEL_NAME_LEN];
183 bfa_get_adapter_model(bfa, model);
185 memset(bfa_buf, 0, sizeof(bfa_buf));
187 snprintf(bfa_buf, sizeof(bfa_buf),
188 "Brocade FCOE Adapter, " "model: %s hwpath: %s driver: %s",
189 model, bfad->pci_name, BFAD_DRIVER_VERSION);
191 snprintf(bfa_buf, sizeof(bfa_buf),
192 "Brocade FC Adapter, " "model: %s hwpath: %s driver: %s",
193 model, bfad->pci_name, BFAD_DRIVER_VERSION);
199 * Scsi_Host template entry, aborts the specified SCSI command.
201 * Returns: SUCCESS or FAILED.
204 bfad_im_abort_handler(struct scsi_cmnd *cmnd)
206 struct Scsi_Host *shost = cmnd->device->host;
207 struct bfad_im_port_s *im_port =
208 (struct bfad_im_port_s *) shost->hostdata[0];
209 struct bfad_s *bfad = im_port->bfad;
210 struct bfa_ioim_s *hal_io;
215 spin_lock_irqsave(&bfad->bfad_lock, flags);
216 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
218 /* IO has been completed, retrun success */
222 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
227 bfa_trc(bfad, hal_io->iotag);
228 BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n",
229 im_port->shost->host_no, cmnd, hal_io->iotag);
230 (void) bfa_ioim_abort(hal_io);
231 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
233 /* Need to wait until the command get aborted */
235 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
236 set_current_state(TASK_UNINTERRUPTIBLE);
237 schedule_timeout(timeout);
238 if (timeout < 4 * HZ)
242 cmnd->scsi_done(cmnd);
243 bfa_trc(bfad, hal_io->iotag);
244 BFA_LOG(KERN_INFO, bfad, log_level,
245 "scsi%d: complete abort 0x%p iotag 0x%x\n",
246 im_port->shost->host_no, cmnd, hal_io->iotag);
249 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
254 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
255 struct bfad_itnim_s *itnim)
257 struct bfa_tskim_s *tskim;
258 struct bfa_itnim_s *bfa_itnim;
259 bfa_status_t rc = BFA_STATUS_OK;
261 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
263 BFA_LOG(KERN_ERR, bfad, log_level,
264 "target reset, fail to allocate tskim\n");
265 rc = BFA_STATUS_FAILED;
270 * Set host_scribble to NULL to avoid aborting a task command if
273 cmnd->host_scribble = NULL;
274 cmnd->SCp.Status = 0;
275 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
276 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
277 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
283 * Scsi_Host template entry, resets a LUN and abort its all commands.
285 * Returns: SUCCESS or FAILED.
289 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
291 struct Scsi_Host *shost = cmnd->device->host;
292 struct bfad_im_port_s *im_port =
293 (struct bfad_im_port_s *) shost->hostdata[0];
294 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
295 struct bfad_s *bfad = im_port->bfad;
296 struct bfa_tskim_s *tskim;
297 struct bfad_itnim_s *itnim;
298 struct bfa_itnim_s *bfa_itnim;
299 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
302 enum bfi_tskim_status task_status;
304 spin_lock_irqsave(&bfad->bfad_lock, flags);
305 itnim = itnim_data->itnim;
307 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
312 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
314 BFA_LOG(KERN_ERR, bfad, log_level,
315 "LUN reset, fail to allocate tskim");
316 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
322 * Set host_scribble to NULL to avoid aborting a task command
325 cmnd->host_scribble = NULL;
326 cmnd->SCp.ptr = (char *)&wq;
327 cmnd->SCp.Status = 0;
328 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
329 bfa_tskim_start(tskim, bfa_itnim,
330 bfad_int_to_lun(cmnd->device->lun),
331 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
332 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
334 wait_event(wq, test_bit(IO_DONE_BIT,
335 (unsigned long *)&cmnd->SCp.Status));
337 task_status = cmnd->SCp.Status >> 1;
338 if (task_status != BFI_TSKIM_STS_OK) {
339 BFA_LOG(KERN_ERR, bfad, log_level,
340 "LUN reset failure, status: %d\n", task_status);
349 * Scsi_Host template entry, resets the bus and abort all commands.
352 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
354 struct Scsi_Host *shost = cmnd->device->host;
355 struct bfad_im_port_s *im_port =
356 (struct bfad_im_port_s *) shost->hostdata[0];
357 struct bfad_s *bfad = im_port->bfad;
358 struct bfad_itnim_s *itnim;
360 u32 i, rc, err_cnt = 0;
361 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
362 enum bfi_tskim_status task_status;
364 spin_lock_irqsave(&bfad->bfad_lock, flags);
365 for (i = 0; i < MAX_FCP_TARGET; i++) {
366 itnim = bfad_os_get_itnim(im_port, i);
368 cmnd->SCp.ptr = (char *)&wq;
369 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
370 if (rc != BFA_STATUS_OK) {
375 /* wait target reset to complete */
376 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
377 wait_event(wq, test_bit(IO_DONE_BIT,
378 (unsigned long *)&cmnd->SCp.Status));
379 spin_lock_irqsave(&bfad->bfad_lock, flags);
381 task_status = cmnd->SCp.Status >> 1;
382 if (task_status != BFI_TSKIM_STS_OK) {
383 BFA_LOG(KERN_ERR, bfad, log_level,
384 "target reset failure,"
385 " status: %d\n", task_status);
390 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
399 * Scsi_Host template entry slave_destroy.
402 bfad_im_slave_destroy(struct scsi_device *sdev)
404 sdev->hostdata = NULL;
409 * BFA FCS itnim callbacks
413 * BFA FCS itnim alloc callback, after successful PRLI
417 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
418 struct bfad_itnim_s **itnim_drv)
420 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
421 if (*itnim_drv == NULL)
424 (*itnim_drv)->im = bfad->im;
425 *itnim = &(*itnim_drv)->fcs_itnim;
426 (*itnim_drv)->state = ITNIM_STATE_NONE;
429 * Initiaze the itnim_work
431 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
432 bfad->bfad_flags |= BFAD_RPORT_ONLINE;
436 * BFA FCS itnim free callback.
437 * Context: Interrupt. bfad_lock is held
440 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
442 struct bfad_port_s *port;
445 char wwpn_str[32], fcid_str[16];
446 struct bfad_im_s *im = itnim_drv->im;
448 /* online to free state transtion should not happen */
449 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
451 itnim_drv->queue_work = 1;
452 /* offline request is not yet done, use the same request to free */
453 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
454 itnim_drv->queue_work = 0;
456 itnim_drv->state = ITNIM_STATE_FREE;
457 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
458 itnim_drv->im_port = port->im_port;
459 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
460 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
461 wwn2str(wwpn_str, wwpn);
462 fcid2str(fcid_str, fcid);
463 BFA_LOG(KERN_INFO, bfad, log_level,
464 "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
465 port->im_port->shost->host_no,
468 /* ITNIM processing */
469 if (itnim_drv->queue_work)
470 queue_work(im->drv_workq, &itnim_drv->itnim_work);
474 * BFA FCS itnim online callback.
475 * Context: Interrupt. bfad_lock is held
478 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
480 struct bfad_port_s *port;
481 struct bfad_im_s *im = itnim_drv->im;
483 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
484 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
485 itnim_drv->state = ITNIM_STATE_ONLINE;
486 itnim_drv->queue_work = 1;
487 itnim_drv->im_port = port->im_port;
489 /* ITNIM processing */
490 if (itnim_drv->queue_work)
491 queue_work(im->drv_workq, &itnim_drv->itnim_work);
495 * BFA FCS itnim offline callback.
496 * Context: Interrupt. bfad_lock is held
499 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
501 struct bfad_port_s *port;
503 struct bfad_im_s *im = itnim_drv->im;
505 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
507 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
508 (port->flags & BFAD_PORT_DELETE)) {
509 itnim_drv->state = ITNIM_STATE_OFFLINE;
512 itnim_drv->im_port = port->im_port;
513 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
514 itnim_drv->queue_work = 1;
516 /* ITNIM processing */
517 if (itnim_drv->queue_work)
518 queue_work(im->drv_workq, &itnim_drv->itnim_work);
522 * Allocate a Scsi_Host for a port.
525 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
530 mutex_lock(&bfad_mutex);
531 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
532 mutex_unlock(&bfad_mutex);
533 printk(KERN_WARNING "idr_pre_get failure\n");
537 error = idr_get_new(&bfad_im_port_index, im_port,
540 mutex_unlock(&bfad_mutex);
541 printk(KERN_WARNING "idr_get_new failure\n");
545 mutex_unlock(&bfad_mutex);
547 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
548 if (!im_port->shost) {
553 im_port->shost->hostdata[0] = (unsigned long)im_port;
554 im_port->shost->unique_id = im_port->idr_id;
555 im_port->shost->this_id = -1;
556 im_port->shost->max_id = MAX_FCP_TARGET;
557 im_port->shost->max_lun = MAX_FCP_LUN;
558 im_port->shost->max_cmd_len = 16;
559 im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
560 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
561 im_port->shost->transportt = bfad_im_scsi_transport_template;
563 im_port->shost->transportt =
564 bfad_im_scsi_vport_transport_template;
566 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
568 printk(KERN_WARNING "scsi_add_host failure %d\n", error);
572 /* setup host fixed attribute if the lk supports */
573 bfad_os_fc_host_init(im_port);
578 scsi_host_put(im_port->shost);
579 im_port->shost = NULL;
581 mutex_lock(&bfad_mutex);
582 idr_remove(&bfad_im_port_index, im_port->idr_id);
583 mutex_unlock(&bfad_mutex);
589 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
591 bfa_trc(bfad, bfad->inst_no);
592 BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n",
593 im_port->shost->host_no);
595 fc_remove_host(im_port->shost);
597 scsi_remove_host(im_port->shost);
598 scsi_host_put(im_port->shost);
600 mutex_lock(&bfad_mutex);
601 idr_remove(&bfad_im_port_index, im_port->idr_id);
602 mutex_unlock(&bfad_mutex);
606 bfad_im_port_delete_handler(struct work_struct *work)
608 struct bfad_im_port_s *im_port =
609 container_of(work, struct bfad_im_port_s, port_delete_work);
611 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
612 im_port->flags |= BFAD_PORT_DELETE;
613 fc_vport_terminate(im_port->fc_vport);
618 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
620 int rc = BFA_STATUS_OK;
621 struct bfad_im_port_s *im_port;
623 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
624 if (im_port == NULL) {
625 rc = BFA_STATUS_ENOMEM;
628 port->im_port = im_port;
629 im_port->port = port;
630 im_port->bfad = bfad;
632 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
633 INIT_LIST_HEAD(&im_port->itnim_mapped_list);
634 INIT_LIST_HEAD(&im_port->binding_list);
641 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
643 struct bfad_im_port_s *im_port = port->im_port;
645 queue_work(bfad->im->drv_workq,
646 &im_port->port_delete_work);
650 bfad_im_port_clean(struct bfad_im_port_s *im_port)
652 struct bfad_fcp_binding *bp, *bp_new;
654 struct bfad_s *bfad = im_port->bfad;
656 spin_lock_irqsave(&bfad->bfad_lock, flags);
657 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
659 list_del(&bp->list_entry);
663 /* the itnim_mapped_list must be empty at this time */
664 bfa_assert(list_empty(&im_port->itnim_mapped_list));
666 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
670 bfad_im_probe(struct bfad_s *bfad)
672 struct bfad_im_s *im;
673 bfa_status_t rc = BFA_STATUS_OK;
675 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
677 rc = BFA_STATUS_ENOMEM;
684 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
686 rc = BFA_STATUS_FAILED;
694 bfad_im_probe_undo(struct bfad_s *bfad)
697 bfad_os_destroy_workq(bfad->im);
704 bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
706 struct scsi_host_template *sht;
708 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
709 sht = &bfad_im_scsi_host_template;
711 sht = &bfad_im_vport_template;
713 sht->sg_tablesize = bfad->cfg_data.io_max_sge;
715 return scsi_host_alloc(sht, sizeof(unsigned long));
719 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
721 if (!(im_port->flags & BFAD_PORT_DELETE))
722 flush_workqueue(bfad->im->drv_workq);
723 bfad_im_scsi_host_free(im_port->bfad, im_port);
724 bfad_im_port_clean(im_port);
729 bfad_os_destroy_workq(struct bfad_im_s *im)
731 if (im && im->drv_workq) {
732 flush_workqueue(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, 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_module_init(void)
817 bfad_im_scsi_transport_template =
818 fc_attach_transport(&bfad_im_fc_function_template);
819 if (!bfad_im_scsi_transport_template)
820 return BFA_STATUS_ENOMEM;
822 bfad_im_scsi_vport_transport_template =
823 fc_attach_transport(&bfad_im_vport_fc_function_template);
824 if (!bfad_im_scsi_vport_transport_template) {
825 fc_release_transport(bfad_im_scsi_transport_template);
826 return BFA_STATUS_ENOMEM;
829 return BFA_STATUS_OK;
833 bfad_im_module_exit(void)
835 if (bfad_im_scsi_transport_template)
836 fc_release_transport(bfad_im_scsi_transport_template);
838 if (bfad_im_scsi_vport_transport_template)
839 fc_release_transport(bfad_im_scsi_vport_transport_template);
843 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
845 struct scsi_device *tmp_sdev;
847 if (((jiffies - itnim->last_ramp_up_time) >
848 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
849 ((jiffies - itnim->last_queue_full_time) >
850 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
851 shost_for_each_device(tmp_sdev, sdev->host) {
852 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
853 if (tmp_sdev->id != sdev->id)
855 if (tmp_sdev->ordered_tags)
856 scsi_adjust_queue_depth(tmp_sdev,
858 tmp_sdev->queue_depth + 1);
860 scsi_adjust_queue_depth(tmp_sdev,
862 tmp_sdev->queue_depth + 1);
864 itnim->last_ramp_up_time = jiffies;
871 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
873 struct scsi_device *tmp_sdev;
875 itnim->last_queue_full_time = jiffies;
877 shost_for_each_device(tmp_sdev, sdev->host) {
878 if (tmp_sdev->id != sdev->id)
880 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
884 struct bfad_itnim_s *
885 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
887 struct bfad_itnim_s *itnim = NULL;
889 /* Search the mapped list for this target ID */
890 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
891 if (id == itnim->scsi_tgt_id)
899 * Scsi_Host template entry slave_alloc
902 bfad_im_slave_alloc(struct scsi_device *sdev)
904 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
906 if (!rport || fc_remote_port_chkready(rport))
909 sdev->hostdata = rport->dd_data;
915 bfad_im_supported_speeds(struct bfa_s *bfa)
917 struct bfa_ioc_attr_s *ioc_attr;
918 u32 supported_speed = 0;
920 ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
924 bfa_get_attr(bfa, ioc_attr);
925 if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
926 if (ioc_attr->adapter_attr.is_mezz) {
927 supported_speed |= FC_PORTSPEED_8GBIT |
929 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
931 supported_speed |= FC_PORTSPEED_8GBIT |
935 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
936 supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
938 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
939 supported_speed |= FC_PORTSPEED_10GBIT;
942 return supported_speed;
946 bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
948 struct Scsi_Host *host = im_port->shost;
949 struct bfad_s *bfad = im_port->bfad;
950 struct bfad_port_s *port = im_port->port;
951 char symname[BFA_SYMNAME_MAXLEN];
952 struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
954 fc_host_node_name(host) =
955 cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
956 fc_host_port_name(host) =
957 cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
958 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
960 fc_host_supported_classes(host) = FC_COS_CLASS3;
962 memset(fc_host_supported_fc4s(host), 0,
963 sizeof(fc_host_supported_fc4s(host)));
964 if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
965 /* For FCP type 0x08 */
966 fc_host_supported_fc4s(host)[2] = 1;
967 /* For fibre channel services type 0x20 */
968 fc_host_supported_fc4s(host)[7] = 1;
970 strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
972 sprintf(fc_host_symbolic_name(host), "%s", symname);
974 fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
975 fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
979 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
981 struct fc_rport_identifiers rport_ids;
982 struct fc_rport *fc_rport;
983 struct bfad_itnim_data_s *itnim_data;
985 rport_ids.node_name =
986 cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
987 rport_ids.port_name =
988 cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
990 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
991 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
993 itnim->fc_rport = fc_rport =
994 fc_remote_port_add(im_port->shost, 0, &rport_ids);
999 fc_rport->maxframe_size =
1000 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1001 fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1003 itnim_data = fc_rport->dd_data;
1004 itnim_data->itnim = itnim;
1006 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1008 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1009 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1011 if ((fc_rport->scsi_target_id != -1)
1012 && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1013 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1019 * Work queue handler using FC transport service
1023 bfad_im_itnim_work_handler(struct work_struct *work)
1025 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1027 struct bfad_im_s *im = itnim->im;
1028 struct bfad_s *bfad = im->bfad;
1029 struct bfad_im_port_s *im_port;
1030 unsigned long flags;
1031 struct fc_rport *fc_rport;
1034 char wwpn_str[32], fcid_str[16];
1036 spin_lock_irqsave(&bfad->bfad_lock, flags);
1037 im_port = itnim->im_port;
1038 bfa_trc(bfad, itnim->state);
1039 switch (itnim->state) {
1040 case ITNIM_STATE_ONLINE:
1041 if (!itnim->fc_rport) {
1042 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1043 bfad_im_fc_rport_add(im_port, itnim);
1044 spin_lock_irqsave(&bfad->bfad_lock, flags);
1045 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1046 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1047 wwn2str(wwpn_str, wwpn);
1048 fcid2str(fcid_str, fcid);
1049 list_add_tail(&itnim->list_entry,
1050 &im_port->itnim_mapped_list);
1051 BFA_LOG(KERN_INFO, bfad, log_level,
1052 "ITNIM ONLINE Target: %d:0:%d "
1053 "FCID: %s WWPN: %s\n",
1054 im_port->shost->host_no,
1056 fcid_str, wwpn_str);
1059 "%s: itnim %llx is already in online state\n",
1061 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1065 case ITNIM_STATE_OFFLINE_PENDING:
1066 itnim->state = ITNIM_STATE_OFFLINE;
1067 if (itnim->fc_rport) {
1068 fc_rport = itnim->fc_rport;
1069 ((struct bfad_itnim_data_s *)
1070 fc_rport->dd_data)->itnim = NULL;
1071 itnim->fc_rport = NULL;
1072 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1073 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1074 fc_rport->dev_loss_tmo =
1075 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1076 fc_remote_port_delete(fc_rport);
1077 spin_lock_irqsave(&bfad->bfad_lock, flags);
1079 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1080 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1081 wwn2str(wwpn_str, wwpn);
1082 fcid2str(fcid_str, fcid);
1083 list_del(&itnim->list_entry);
1084 BFA_LOG(KERN_INFO, bfad, log_level,
1085 "ITNIM OFFLINE Target: %d:0:%d "
1086 "FCID: %s WWPN: %s\n",
1087 im_port->shost->host_no,
1089 fcid_str, wwpn_str);
1092 case ITNIM_STATE_FREE:
1093 if (itnim->fc_rport) {
1094 fc_rport = itnim->fc_rport;
1095 ((struct bfad_itnim_data_s *)
1096 fc_rport->dd_data)->itnim = NULL;
1097 itnim->fc_rport = NULL;
1098 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1099 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1100 fc_rport->dev_loss_tmo =
1101 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1102 fc_remote_port_delete(fc_rport);
1103 spin_lock_irqsave(&bfad->bfad_lock, flags);
1105 list_del(&itnim->list_entry);
1115 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1119 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1122 bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1124 struct bfad_im_port_s *im_port =
1125 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1126 struct bfad_s *bfad = im_port->bfad;
1127 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1128 struct bfad_itnim_s *itnim;
1129 struct bfa_ioim_s *hal_io;
1130 unsigned long flags;
1133 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1135 rc = fc_remote_port_chkready(rport);
1142 sg_cnt = scsi_dma_map(cmnd);
1144 return SCSI_MLQUEUE_HOST_BUSY;
1146 cmnd->scsi_done = done;
1148 spin_lock_irqsave(&bfad->bfad_lock, flags);
1149 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1151 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1152 bfad->inst_no, cmnd, cmnd->cmnd[0]);
1153 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1158 itnim = itnim_data->itnim;
1160 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1164 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1165 itnim->bfa_itnim, sg_cnt);
1167 printk(KERN_WARNING "hal_io failure\n");
1168 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1169 scsi_dma_unmap(cmnd);
1170 return SCSI_MLQUEUE_HOST_BUSY;
1173 cmnd->host_scribble = (char *)hal_io;
1174 bfa_trc_fp(bfad, hal_io->iotag);
1175 bfa_ioim_start(hal_io);
1176 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1181 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1182 scsi_dma_unmap(cmnd);
1189 static DEF_SCSI_QCMD(bfad_im_queuecommand)
1192 bfad_os_rport_online_wait(struct bfad_s *bfad)
1195 int rport_delay = 10;
1197 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1198 && i < bfa_linkup_delay; i++) {
1199 set_current_state(TASK_UNINTERRUPTIBLE);
1200 schedule_timeout(HZ);
1203 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1204 rport_delay = rport_delay < bfa_linkup_delay ?
1205 rport_delay : bfa_linkup_delay;
1206 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1207 && i < rport_delay; i++) {
1208 set_current_state(TASK_UNINTERRUPTIBLE);
1209 schedule_timeout(HZ);
1212 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1213 set_current_state(TASK_UNINTERRUPTIBLE);
1214 schedule_timeout(rport_delay * HZ);
1220 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1223 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
1227 * Querying for the boot target port wwns
1228 * -- read from boot information in flash.
1229 * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1230 * else => local boot machine set linkup_delay = 0
1233 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1236 /* If Boot over SAN set linkup_delay = 30sec */
1239 /* If local boot; no linkup_delay */
1242 return linkup_delay;