GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / bfa / bfad_im.c
blob630f9d31ff8310642469706ab6d27c0d8475e3fd
1 /*
2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3 * All rights reserved
4 * www.brocade.com
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.
18 /**
19 * bfad_im.c Linux driver IM module.
22 #include <linux/slab.h>
23 #include "bfad_drv.h"
24 #include "bfad_im.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);
39 void
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;
50 switch (io_status) {
51 case BFI_IOIM_STS_OK:
52 bfa_trc(bfad, scsi_status);
53 scsi_set_resid(cmnd, 0);
55 if (sns_len > 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);
61 if (residue > 0) {
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) <
66 cmnd->underflow) {
67 bfa_trc(bfad, 0);
68 host_status = DID_ERROR;
71 cmnd->result = ScsiResult(host_status, scsi_status);
73 break;
75 case BFI_IOIM_STS_ABORTED:
76 case BFI_IOIM_STS_TIMEDOUT:
77 case BFI_IOIM_STS_PATHTOV:
78 default:
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)
84 scsi_dma_unmap(cmnd);
86 cmnd->host_scribble = NULL;
87 bfa_trc(bfad, cmnd->result);
89 itnim_data = cmnd->device->hostdata;
90 if (itnim_data) {
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) {
97 /* qfull handling */
98 bfad_os_handle_qfull(itnim, cmnd->device);
102 cmnd->scsi_done(cmnd);
105 void
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;
123 if (itnim_data) {
124 itnim = itnim_data->itnim;
125 if (itnim)
126 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
130 cmnd->scsi_done(cmnd);
133 void
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;
149 void
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;
161 if (wq)
162 wake_up(wq);
165 void
166 bfa_cb_ioim_resfree(void *drv)
171 * Scsi_Host_template SCSI host template
174 * Scsi_Host template entry, returns BFAD PCI info.
176 static const char *
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);
191 return bfa_buf;
195 * Scsi_Host template entry, aborts the specified SCSI command.
197 * Returns: SUCCESS or FAILED.
199 static int
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;
207 unsigned long flags;
208 u32 timeout;
209 int rc = FAILED;
211 spin_lock_irqsave(&bfad->bfad_lock, flags);
212 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
213 if (!hal_io) {
214 /* IO has been completed, retrun success */
215 rc = SUCCESS;
216 goto out;
218 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
219 rc = FAILED;
220 goto out;
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 */
230 timeout = 10;
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)
235 timeout *= 2;
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);
242 return SUCCESS;
243 out:
244 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
245 return rc;
248 static bfa_status_t
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);
257 if (!tskim) {
258 BFA_DEV_PRINTF(bfad, BFA_ERR,
259 "target reset, fail to allocate tskim\n");
260 rc = BFA_STATUS_FAILED;
261 goto out;
265 * Set host_scribble to NULL to avoid aborting a task command if
266 * happens.
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);
273 out:
274 return rc;
278 * Scsi_Host template entry, resets a LUN and abort its all commands.
280 * Returns: SUCCESS or FAILED.
283 static int
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_ONSTACK(wq);
295 int rc = SUCCESS;
296 unsigned long flags;
297 enum bfi_tskim_status task_status;
299 spin_lock_irqsave(&bfad->bfad_lock, flags);
300 itnim = itnim_data->itnim;
301 if (!itnim) {
302 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
303 rc = FAILED;
304 goto out;
307 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
308 if (!tskim) {
309 BFA_DEV_PRINTF(bfad, BFA_ERR,
310 "LUN reset, fail to allocate tskim");
311 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
312 rc = FAILED;
313 goto out;
317 * Set host_scribble to NULL to avoid aborting a task command
318 * if happens.
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",
335 task_status);
336 rc = FAILED;
339 out:
340 return rc;
344 * Scsi_Host template entry, resets the bus and abort all commands.
346 static int
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;
354 unsigned long flags;
355 u32 i, rc, err_cnt = 0;
356 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(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);
362 if (itnim) {
363 cmnd->SCp.ptr = (char *)&wq;
364 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
365 if (rc != BFA_STATUS_OK) {
366 err_cnt++;
367 continue;
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);
381 err_cnt++;
385 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
387 if (err_cnt)
388 return FAILED;
390 return SUCCESS;
394 * Scsi_Host template entry slave_destroy.
396 static void
397 bfad_im_slave_destroy(struct scsi_device *sdev)
399 sdev->hostdata = NULL;
400 return;
404 * BFA FCS itnim callbacks
408 * BFA FCS itnim alloc callback, after successful PRLI
409 * Context: Interrupt
411 void
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)
417 return;
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
434 void
435 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
437 struct bfad_port_s *port;
438 wwn_t wwpn;
439 u32 fcid;
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,
459 fcid_str, wwpn_str);
460 bfad_os_itnim_process(itnim_drv);
464 * BFA FCS itnim online callback.
465 * Context: Interrupt. bfad_lock is held
467 void
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
484 void
485 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
487 struct bfad_port_s *port;
488 struct bfad_s *bfad;
490 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
491 bfad = port->bfad;
492 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
493 (port->flags & BFAD_PORT_DELETE)) {
494 itnim_drv->state = ITNIM_STATE_OFFLINE;
495 return;
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,
517 struct device *dev)
519 int error = 1;
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");
525 goto out;
528 error = idr_get_new(&bfad_im_port_index, im_port,
529 &im_port->idr_id);
530 if (error) {
531 mutex_unlock(&bfad_mutex);
532 printk(KERN_WARNING "idr_get_new failure\n");
533 goto out;
536 mutex_unlock(&bfad_mutex);
538 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
539 if (!im_port->shost) {
540 error = 1;
541 goto out_free_idr;
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;
553 else
554 im_port->shost->transportt =
555 bfad_im_scsi_vport_transport_template;
557 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
558 if (error) {
559 printk(KERN_WARNING "scsi_add_host failure %d\n", error);
560 goto out_fc_rel;
563 /* setup host fixed attribute if the lk supports */
564 bfad_os_fc_host_init(im_port);
566 return 0;
568 out_fc_rel:
569 scsi_host_put(im_port->shost);
570 im_port->shost = NULL;
571 out_free_idr:
572 mutex_lock(&bfad_mutex);
573 idr_remove(&bfad_im_port_index, im_port->idr_id);
574 mutex_unlock(&bfad_mutex);
575 out:
576 return error;
579 void
580 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
582 bfa_trc(bfad, bfad->inst_no);
583 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_HOST_FREE,
584 im_port->shost->host_no);
586 fc_remove_host(im_port->shost);
588 scsi_remove_host(im_port->shost);
589 scsi_host_put(im_port->shost);
591 mutex_lock(&bfad_mutex);
592 idr_remove(&bfad_im_port_index, im_port->idr_id);
593 mutex_unlock(&bfad_mutex);
596 static void
597 bfad_im_port_delete_handler(struct work_struct *work)
599 struct bfad_im_port_s *im_port =
600 container_of(work, struct bfad_im_port_s, port_delete_work);
601 struct bfad_s *bfad = im_port->bfad;
603 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
604 im_port->flags |= BFAD_PORT_DELETE;
605 fc_vport_terminate(im_port->fc_vport);
606 atomic_dec(&bfad->wq_reqcnt);
611 bfa_status_t
612 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
614 int rc = BFA_STATUS_OK;
615 struct bfad_im_port_s *im_port;
617 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
618 if (im_port == NULL) {
619 rc = BFA_STATUS_ENOMEM;
620 goto ext;
622 port->im_port = im_port;
623 im_port->port = port;
624 im_port->bfad = bfad;
626 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
627 INIT_LIST_HEAD(&im_port->itnim_mapped_list);
628 INIT_LIST_HEAD(&im_port->binding_list);
630 ext:
631 return rc;
634 void
635 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
637 struct bfad_im_port_s *im_port = port->im_port;
639 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
640 atomic_inc(&bfad->wq_reqcnt);
641 queue_work(bfad->im->drv_workq,
642 &im_port->port_delete_work);
646 void
647 bfad_im_port_clean(struct bfad_im_port_s *im_port)
649 struct bfad_fcp_binding *bp, *bp_new;
650 unsigned long flags;
651 struct bfad_s *bfad = im_port->bfad;
653 spin_lock_irqsave(&bfad->bfad_lock, flags);
654 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
655 list_entry) {
656 list_del(&bp->list_entry);
657 kfree(bp);
660 /* the itnim_mapped_list must be empty at this time */
661 bfa_assert(list_empty(&im_port->itnim_mapped_list));
663 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
666 void
667 bfad_im_port_online(struct bfad_s *bfad, struct bfad_port_s *port)
671 void
672 bfad_im_port_offline(struct bfad_s *bfad, struct bfad_port_s *port)
676 bfa_status_t
677 bfad_im_probe(struct bfad_s *bfad)
679 struct bfad_im_s *im;
680 bfa_status_t rc = BFA_STATUS_OK;
682 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
683 if (im == NULL) {
684 rc = BFA_STATUS_ENOMEM;
685 goto ext;
688 bfad->im = im;
689 im->bfad = bfad;
691 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
692 kfree(im);
693 rc = BFA_STATUS_FAILED;
696 ext:
697 return rc;
700 void
701 bfad_im_probe_undo(struct bfad_s *bfad)
703 if (bfad->im) {
704 while (atomic_read(&bfad->wq_reqcnt)) {
705 printk(KERN_INFO "bfa %s: waiting workq processing,"
706 " wq_reqcnt:%x\n", bfad->pci_name,
707 atomic_read(&bfad->wq_reqcnt));
708 schedule_timeout_uninterruptible(HZ);
710 bfad_os_destroy_workq(bfad->im);
711 kfree(bfad->im);
712 bfad->im = NULL;
717 * Call back function to handle IO redirection state change
719 void
720 bfa_cb_ioredirect_state_change(void *hcb_bfad, bfa_boolean_t ioredirect)
722 /* Do nothing */
725 struct Scsi_Host *
726 bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
728 struct scsi_host_template *sht;
730 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
731 sht = &bfad_im_scsi_host_template;
732 else
733 sht = &bfad_im_vport_template;
735 sht->sg_tablesize = bfad->cfg_data.io_max_sge;
737 return scsi_host_alloc(sht, sizeof(unsigned long));
740 void
741 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
743 if (!(im_port->flags & BFAD_PORT_DELETE))
744 flush_workqueue(bfad->im->drv_workq);
745 bfad_im_scsi_host_free(im_port->bfad, im_port);
746 bfad_im_port_clean(im_port);
747 kfree(im_port);
750 void
751 bfad_os_destroy_workq(struct bfad_im_s *im)
753 if (im && im->drv_workq) {
754 destroy_workqueue(im->drv_workq);
755 im->drv_workq = NULL;
759 bfa_status_t
760 bfad_os_thread_workq(struct bfad_s *bfad)
762 struct bfad_im_s *im = bfad->im;
764 bfa_trc(bfad, 0);
765 snprintf(im->drv_workq_name, BFAD_KOBJ_NAME_LEN, "bfad_wq_%d",
766 bfad->inst_no);
767 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
768 if (!im->drv_workq)
769 return BFA_STATUS_FAILED;
771 return BFA_STATUS_OK;
775 * Scsi_Host template entry.
777 * Description:
778 * OS entry point to adjust the queue_depths on a per-device basis.
779 * Called once per device during the bus scan.
780 * Return non-zero if fails.
782 static int
783 bfad_im_slave_configure(struct scsi_device *sdev)
785 if (sdev->tagged_supported)
786 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
787 else
788 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
790 return 0;
793 struct scsi_host_template bfad_im_scsi_host_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,
806 .this_id = -1,
807 .sg_tablesize = BFAD_IO_MAX_SGE,
808 .cmd_per_lun = 3,
809 .use_clustering = ENABLE_CLUSTERING,
810 .shost_attrs = bfad_im_host_attrs,
811 .max_sectors = 0xFFFF,
814 struct scsi_host_template bfad_im_vport_template = {
815 .module = THIS_MODULE,
816 .name = BFAD_DRIVER_NAME,
817 .info = bfad_im_info,
818 .queuecommand = bfad_im_queuecommand,
819 .eh_abort_handler = bfad_im_abort_handler,
820 .eh_device_reset_handler = bfad_im_reset_lun_handler,
821 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
823 .slave_alloc = bfad_im_slave_alloc,
824 .slave_configure = bfad_im_slave_configure,
825 .slave_destroy = bfad_im_slave_destroy,
827 .this_id = -1,
828 .sg_tablesize = BFAD_IO_MAX_SGE,
829 .cmd_per_lun = 3,
830 .use_clustering = ENABLE_CLUSTERING,
831 .shost_attrs = bfad_im_vport_attrs,
832 .max_sectors = 0xFFFF,
835 void
836 bfad_im_probe_post(struct bfad_im_s *im)
838 flush_workqueue(im->drv_workq);
841 bfa_status_t
842 bfad_im_module_init(void)
844 bfad_im_scsi_transport_template =
845 fc_attach_transport(&bfad_im_fc_function_template);
846 if (!bfad_im_scsi_transport_template)
847 return BFA_STATUS_ENOMEM;
849 bfad_im_scsi_vport_transport_template =
850 fc_attach_transport(&bfad_im_vport_fc_function_template);
851 if (!bfad_im_scsi_vport_transport_template) {
852 fc_release_transport(bfad_im_scsi_transport_template);
853 return BFA_STATUS_ENOMEM;
856 return BFA_STATUS_OK;
859 void
860 bfad_im_module_exit(void)
862 if (bfad_im_scsi_transport_template)
863 fc_release_transport(bfad_im_scsi_transport_template);
864 if (bfad_im_scsi_vport_transport_template)
865 fc_release_transport(bfad_im_scsi_vport_transport_template);
868 void
869 bfad_os_itnim_process(struct bfad_itnim_s *itnim_drv)
871 struct bfad_im_s *im = itnim_drv->im;
873 if (itnim_drv->queue_work)
874 queue_work(im->drv_workq, &itnim_drv->itnim_work);
877 void
878 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
880 struct scsi_device *tmp_sdev;
882 if (((jiffies - itnim->last_ramp_up_time) >
883 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
884 ((jiffies - itnim->last_queue_full_time) >
885 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
886 shost_for_each_device(tmp_sdev, sdev->host) {
887 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
888 if (tmp_sdev->id != sdev->id)
889 continue;
890 if (tmp_sdev->ordered_tags)
891 scsi_adjust_queue_depth(tmp_sdev,
892 MSG_ORDERED_TAG,
893 tmp_sdev->queue_depth + 1);
894 else
895 scsi_adjust_queue_depth(tmp_sdev,
896 MSG_SIMPLE_TAG,
897 tmp_sdev->queue_depth + 1);
899 itnim->last_ramp_up_time = jiffies;
905 void
906 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
908 struct scsi_device *tmp_sdev;
910 itnim->last_queue_full_time = jiffies;
912 shost_for_each_device(tmp_sdev, sdev->host) {
913 if (tmp_sdev->id != sdev->id)
914 continue;
915 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
922 struct bfad_itnim_s *
923 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
925 struct bfad_itnim_s *itnim = NULL;
927 /* Search the mapped list for this target ID */
928 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
929 if (id == itnim->scsi_tgt_id)
930 return itnim;
933 return NULL;
937 * Scsi_Host template entry slave_alloc
939 static int
940 bfad_im_slave_alloc(struct scsi_device *sdev)
942 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
944 if (!rport || fc_remote_port_chkready(rport))
945 return -ENXIO;
947 sdev->hostdata = rport->dd_data;
949 return 0;
952 void
953 bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
955 struct Scsi_Host *host = im_port->shost;
956 struct bfad_s *bfad = im_port->bfad;
957 struct bfad_port_s *port = im_port->port;
958 struct bfa_pport_attr_s pattr;
959 char model[BFA_ADAPTER_MODEL_NAME_LEN];
960 char fw_ver[BFA_VERSION_LEN];
962 fc_host_node_name(host) =
963 bfa_os_htonll((bfa_fcs_port_get_nwwn(port->fcs_port)));
964 fc_host_port_name(host) =
965 bfa_os_htonll((bfa_fcs_port_get_pwwn(port->fcs_port)));
966 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
968 fc_host_supported_classes(host) = FC_COS_CLASS3;
970 memset(fc_host_supported_fc4s(host), 0,
971 sizeof(fc_host_supported_fc4s(host)));
972 if (bfad_supported_fc4s & (BFA_PORT_ROLE_FCP_IM | BFA_PORT_ROLE_FCP_TM))
973 /* For FCP type 0x08 */
974 fc_host_supported_fc4s(host)[2] = 1;
975 if (bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IPFC)
976 /* For LLC/SNAP type 0x05 */
977 fc_host_supported_fc4s(host)[3] = 0x20;
978 /* For fibre channel services type 0x20 */
979 fc_host_supported_fc4s(host)[7] = 1;
981 bfa_get_adapter_model(&bfad->bfa, model);
982 bfa_get_adapter_fw_ver(&bfad->bfa, fw_ver);
983 sprintf(fc_host_symbolic_name(host), "Brocade %s FV%s DV%s",
984 model, fw_ver, BFAD_DRIVER_VERSION);
986 fc_host_supported_speeds(host) = 0;
987 fc_host_supported_speeds(host) |=
988 FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
989 FC_PORTSPEED_1GBIT;
991 bfa_fcport_get_attr(&bfad->bfa, &pattr);
992 fc_host_maxframe_size(host) = pattr.pport_cfg.maxfrsize;
995 static void
996 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
998 struct fc_rport_identifiers rport_ids;
999 struct fc_rport *fc_rport;
1000 struct bfad_itnim_data_s *itnim_data;
1002 rport_ids.node_name =
1003 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
1004 rport_ids.port_name =
1005 bfa_os_htonll(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1006 rport_ids.port_id =
1007 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
1008 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1010 itnim->fc_rport = fc_rport =
1011 fc_remote_port_add(im_port->shost, 0, &rport_ids);
1013 if (!fc_rport)
1014 return;
1016 fc_rport->maxframe_size =
1017 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1018 fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1020 itnim_data = fc_rport->dd_data;
1021 itnim_data->itnim = itnim;
1023 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1025 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1026 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1028 if ((fc_rport->scsi_target_id != -1)
1029 && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1030 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1032 return;
1036 * Work queue handler using FC transport service
1037 * Context: kernel
1039 static void
1040 bfad_im_itnim_work_handler(struct work_struct *work)
1042 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1043 itnim_work);
1044 struct bfad_im_s *im = itnim->im;
1045 struct bfad_s *bfad = im->bfad;
1046 struct bfad_im_port_s *im_port;
1047 unsigned long flags;
1048 struct fc_rport *fc_rport;
1049 wwn_t wwpn;
1050 u32 fcid;
1051 char wwpn_str[32], fcid_str[16];
1053 spin_lock_irqsave(&bfad->bfad_lock, flags);
1054 im_port = itnim->im_port;
1055 bfa_trc(bfad, itnim->state);
1056 switch (itnim->state) {
1057 case ITNIM_STATE_ONLINE:
1058 if (!itnim->fc_rport) {
1059 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1060 bfad_im_fc_rport_add(im_port, itnim);
1061 spin_lock_irqsave(&bfad->bfad_lock, flags);
1062 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1063 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1064 wwn2str(wwpn_str, wwpn);
1065 fcid2str(fcid_str, fcid);
1066 list_add_tail(&itnim->list_entry,
1067 &im_port->itnim_mapped_list);
1068 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_ONLINE,
1069 im_port->shost->host_no,
1070 itnim->scsi_tgt_id,
1071 fcid_str, wwpn_str);
1072 } else {
1073 printk(KERN_WARNING
1074 "%s: itnim %llx is already in online state\n",
1075 __func__,
1076 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1079 break;
1080 case ITNIM_STATE_OFFLINE_PENDING:
1081 itnim->state = ITNIM_STATE_OFFLINE;
1082 if (itnim->fc_rport) {
1083 fc_rport = itnim->fc_rport;
1084 ((struct bfad_itnim_data_s *)
1085 fc_rport->dd_data)->itnim = NULL;
1086 itnim->fc_rport = NULL;
1087 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1088 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1089 fc_rport->dev_loss_tmo =
1090 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1091 fc_remote_port_delete(fc_rport);
1092 spin_lock_irqsave(&bfad->bfad_lock, flags);
1094 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1095 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1096 wwn2str(wwpn_str, wwpn);
1097 fcid2str(fcid_str, fcid);
1098 list_del(&itnim->list_entry);
1099 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_OFFLINE,
1100 im_port->shost->host_no,
1101 itnim->scsi_tgt_id,
1102 fcid_str, wwpn_str);
1104 break;
1105 case ITNIM_STATE_FREE:
1106 if (itnim->fc_rport) {
1107 fc_rport = itnim->fc_rport;
1108 ((struct bfad_itnim_data_s *)
1109 fc_rport->dd_data)->itnim = NULL;
1110 itnim->fc_rport = NULL;
1111 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1112 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1113 fc_rport->dev_loss_tmo =
1114 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1115 fc_remote_port_delete(fc_rport);
1116 spin_lock_irqsave(&bfad->bfad_lock, flags);
1118 list_del(&itnim->list_entry);
1121 kfree(itnim);
1122 break;
1123 default:
1124 bfa_assert(0);
1125 break;
1128 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1132 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1134 static int
1135 bfad_im_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1137 struct bfad_im_port_s *im_port =
1138 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1139 struct bfad_s *bfad = im_port->bfad;
1140 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1141 struct bfad_itnim_s *itnim;
1142 struct bfa_ioim_s *hal_io;
1143 unsigned long flags;
1144 int rc;
1145 s16 sg_cnt = 0;
1146 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1148 rc = fc_remote_port_chkready(rport);
1149 if (rc) {
1150 cmnd->result = rc;
1151 done(cmnd);
1152 return 0;
1155 sg_cnt = scsi_dma_map(cmnd);
1157 if (sg_cnt < 0)
1158 return SCSI_MLQUEUE_HOST_BUSY;
1160 cmnd->scsi_done = done;
1162 spin_lock_irqsave(&bfad->bfad_lock, flags);
1163 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1164 printk(KERN_WARNING
1165 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1166 bfad->inst_no, cmnd, cmnd->cmnd[0]);
1167 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1168 goto out_fail_cmd;
1171 itnim = itnim_data->itnim;
1172 if (!itnim) {
1173 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1174 goto out_fail_cmd;
1177 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1178 itnim->bfa_itnim, sg_cnt);
1179 if (!hal_io) {
1180 printk(KERN_WARNING "hal_io failure\n");
1181 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1182 scsi_dma_unmap(cmnd);
1183 return SCSI_MLQUEUE_HOST_BUSY;
1186 cmnd->host_scribble = (char *)hal_io;
1187 bfa_trc_fp(bfad, hal_io->iotag);
1188 bfa_ioim_start(hal_io);
1189 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1191 return 0;
1193 out_fail_cmd:
1194 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1195 scsi_dma_unmap(cmnd);
1196 if (done)
1197 done(cmnd);
1199 return 0;
1202 void
1203 bfad_os_rport_online_wait(struct bfad_s *bfad)
1205 int i;
1206 int rport_delay = 10;
1208 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1209 && i < bfa_linkup_delay; i++)
1210 schedule_timeout_uninterruptible(HZ);
1212 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1213 rport_delay = rport_delay < bfa_linkup_delay ?
1214 rport_delay : bfa_linkup_delay;
1215 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1216 && i < rport_delay; i++)
1217 schedule_timeout_uninterruptible(HZ);
1219 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE))
1220 schedule_timeout_uninterruptible(rport_delay * HZ);
1225 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1228 u8 nwwns = 0;
1229 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
1230 int ldelay;
1233 * Querying for the boot target port wwns
1234 * -- read from boot information in flash.
1235 * If nwwns > 0 => boot over SAN and set bfa_linkup_delay = 30
1236 * else => local boot machine set bfa_linkup_delay = 10
1239 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1241 if (nwwns > 0) {
1242 /* If boot over SAN; linkup_delay = 30sec */
1243 ldelay = 30;
1244 } else {
1245 /* If local boot; linkup_delay = 10sec */
1246 ldelay = 0;
1249 return ldelay;