blob: cee3d89d14127454771cc304d70709c9e25f081d [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3 * All rights reserved
4 * www.brocade.com
5 *
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7 *
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
11 *
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.
16 */
17
18/**
19 * bfad_im.c Linux driver IM module.
20 */
21
22#include "bfad_drv.h"
23#include "bfad_im.h"
24#include "bfad_trcmod.h"
25#include "bfa_cb_ioim_macros.h"
26#include <fcb/bfa_fcb_fcpim.h>
27
28BFA_TRC_FILE(LDRV, IM);
29
30DEFINE_IDR(bfad_im_port_index);
31struct scsi_transport_template *bfad_im_scsi_transport_template;
32static void bfad_im_itnim_work_handler(struct work_struct *work);
33static int bfad_im_queuecommand(struct scsi_cmnd *cmnd,
34 void (*done)(struct scsi_cmnd *));
35static int bfad_im_slave_alloc(struct scsi_device *sdev);
36
37void
38bfa_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)
41{
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
47 switch (io_status) {
48 case BFI_IOIM_STS_OK:
49 bfa_trc(bfad, scsi_status);
50 cmnd->result = ScsiResult(DID_OK, scsi_status);
51 scsi_set_resid(cmnd, 0);
52
53 if (sns_len > 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);
58 }
59 if (residue > 0)
60 scsi_set_resid(cmnd, residue);
61 break;
62
63 case BFI_IOIM_STS_ABORTED:
64 case BFI_IOIM_STS_TIMEDOUT:
65 case BFI_IOIM_STS_PATHTOV:
66 default:
67 cmnd->result = ScsiResult(DID_ERROR, 0);
68 }
69
70 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
71 if (cmnd->device->host != NULL)
72 scsi_dma_unmap(cmnd);
73
74 cmnd->host_scribble = NULL;
75 bfa_trc(bfad, cmnd->result);
76
77 itnim_data = cmnd->device->hostdata;
78 if (itnim_data) {
79 itnim = itnim_data->itnim;
80 if (!cmnd->result && itnim &&
81 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
82 /* Queue depth adjustment for good status completion */
83 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
84 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
85 /* qfull handling */
86 bfad_os_handle_qfull(itnim, cmnd->device);
87 }
88 }
89
90 cmnd->scsi_done(cmnd);
91}
92
93void
94bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
95{
96 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
97 struct bfad_itnim_data_s *itnim_data;
98 struct bfad_itnim_s *itnim;
99
100 cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
101
102 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
103 if (cmnd->device->host != NULL)
104 scsi_dma_unmap(cmnd);
105
106 cmnd->host_scribble = NULL;
107
108 /* Queue depth adjustment */
109 if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
110 itnim_data = cmnd->device->hostdata;
111 if (itnim_data) {
112 itnim = itnim_data->itnim;
113 if (itnim)
114 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
115 }
116 }
117
118 cmnd->scsi_done(cmnd);
119}
120
121void
122bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
123{
124 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
125 struct bfad_s *bfad = drv;
126
127 cmnd->result = ScsiResult(DID_ERROR, 0);
128
129 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
130 if (cmnd->device->host != NULL)
131 scsi_dma_unmap(cmnd);
132
133 bfa_trc(bfad, cmnd->result);
134 cmnd->host_scribble = NULL;
135}
136
137void
138bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
139 enum bfi_tskim_status tsk_status)
140{
141 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
142 wait_queue_head_t *wq;
143
144 cmnd->SCp.Status |= tsk_status << 1;
145 set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
146 wq = (wait_queue_head_t *) cmnd->SCp.ptr;
147 cmnd->SCp.ptr = NULL;
148
149 if (wq)
150 wake_up(wq);
151}
152
153void
154bfa_cb_ioim_resfree(void *drv)
155{
156}
157
158/**
159 * Scsi_Host_template SCSI host template
160 */
161/**
162 * Scsi_Host template entry, returns BFAD PCI info.
163 */
164static const char *
165bfad_im_info(struct Scsi_Host *shost)
166{
167 static char bfa_buf[256];
168 struct bfad_im_port_s *im_port =
169 (struct bfad_im_port_s *) shost->hostdata[0];
Jing Huang7725ccf2009-09-23 17:46:15 -0700170 struct bfad_s *bfad = im_port->bfad;
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800171 char model[BFA_ADAPTER_MODEL_NAME_LEN];
Jing Huang7725ccf2009-09-23 17:46:15 -0700172
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800173 bfa_get_adapter_model(&bfad->bfa, model);
Jing Huang7725ccf2009-09-23 17:46:15 -0700174
175 memset(bfa_buf, 0, sizeof(bfa_buf));
176 snprintf(bfa_buf, sizeof(bfa_buf),
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800177 "Brocade FC/FCOE Adapter, " "model: %s hwpath: %s driver: %s",
178 model, bfad->pci_name, BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -0700179 return bfa_buf;
180}
181
182/**
183 * Scsi_Host template entry, aborts the specified SCSI command.
184 *
185 * Returns: SUCCESS or FAILED.
186 */
187static int
188bfad_im_abort_handler(struct scsi_cmnd *cmnd)
189{
190 struct Scsi_Host *shost = cmnd->device->host;
191 struct bfad_im_port_s *im_port =
192 (struct bfad_im_port_s *) shost->hostdata[0];
193 struct bfad_s *bfad = im_port->bfad;
194 struct bfa_ioim_s *hal_io;
195 unsigned long flags;
196 u32 timeout;
197 int rc = FAILED;
198
199 spin_lock_irqsave(&bfad->bfad_lock, flags);
200 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
201 if (!hal_io) {
202 /* IO has been completed, retrun success */
203 rc = SUCCESS;
204 goto out;
205 }
206 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
207 rc = FAILED;
208 goto out;
209 }
210
211 bfa_trc(bfad, hal_io->iotag);
212 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT,
213 im_port->shost->host_no, cmnd, hal_io->iotag);
214 bfa_ioim_abort(hal_io);
215 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
216
217 /* Need to wait until the command get aborted */
218 timeout = 10;
219 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
220 set_current_state(TASK_UNINTERRUPTIBLE);
221 schedule_timeout(timeout);
222 if (timeout < 4 * HZ)
223 timeout *= 2;
224 }
225
226 cmnd->scsi_done(cmnd);
227 bfa_trc(bfad, hal_io->iotag);
228 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_ABORT_COMP,
229 im_port->shost->host_no, cmnd, hal_io->iotag);
230 return SUCCESS;
231out:
232 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
233 return rc;
234}
235
236static bfa_status_t
237bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
238 struct bfad_itnim_s *itnim)
239{
240 struct bfa_tskim_s *tskim;
241 struct bfa_itnim_s *bfa_itnim;
242 bfa_status_t rc = BFA_STATUS_OK;
243
244 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
245 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
246 if (!tskim) {
247 BFA_DEV_PRINTF(bfad, BFA_ERR,
248 "target reset, fail to allocate tskim\n");
249 rc = BFA_STATUS_FAILED;
250 goto out;
251 }
252
253 /*
254 * Set host_scribble to NULL to avoid aborting a task command if
255 * happens.
256 */
257 cmnd->host_scribble = NULL;
258 cmnd->SCp.Status = 0;
259 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
260 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
261 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
262out:
263 return rc;
264}
265
266/**
267 * Scsi_Host template entry, resets a LUN and abort its all commands.
268 *
269 * Returns: SUCCESS or FAILED.
270 *
271 */
272static int
273bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
274{
275 struct Scsi_Host *shost = cmnd->device->host;
276 struct bfad_im_port_s *im_port =
277 (struct bfad_im_port_s *) shost->hostdata[0];
278 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
279 struct bfad_s *bfad = im_port->bfad;
280 struct bfa_tskim_s *tskim;
281 struct bfad_itnim_s *itnim;
282 struct bfa_itnim_s *bfa_itnim;
283 DECLARE_WAIT_QUEUE_HEAD(wq);
284 int rc = SUCCESS;
285 unsigned long flags;
286 enum bfi_tskim_status task_status;
287
288 spin_lock_irqsave(&bfad->bfad_lock, flags);
289 itnim = itnim_data->itnim;
290 if (!itnim) {
291 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
292 rc = FAILED;
293 goto out;
294 }
295
296 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
297 if (!tskim) {
298 BFA_DEV_PRINTF(bfad, BFA_ERR,
299 "LUN reset, fail to allocate tskim");
300 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
301 rc = FAILED;
302 goto out;
303 }
304
305 /**
306 * Set host_scribble to NULL to avoid aborting a task command
307 * if happens.
308 */
309 cmnd->host_scribble = NULL;
310 cmnd->SCp.ptr = (char *)&wq;
311 cmnd->SCp.Status = 0;
312 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
313 bfa_tskim_start(tskim, bfa_itnim,
314 bfad_int_to_lun(cmnd->device->lun),
315 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
316 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
317
318 wait_event(wq, test_bit(IO_DONE_BIT,
319 (unsigned long *)&cmnd->SCp.Status));
320
321 task_status = cmnd->SCp.Status >> 1;
322 if (task_status != BFI_TSKIM_STS_OK) {
323 BFA_DEV_PRINTF(bfad, BFA_ERR, "LUN reset failure, status: %d\n",
324 task_status);
325 rc = FAILED;
326 }
327
328out:
329 return rc;
330}
331
332/**
333 * Scsi_Host template entry, resets the bus and abort all commands.
334 */
335static int
336bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
337{
338 struct Scsi_Host *shost = cmnd->device->host;
339 struct bfad_im_port_s *im_port =
340 (struct bfad_im_port_s *) shost->hostdata[0];
341 struct bfad_s *bfad = im_port->bfad;
342 struct bfad_itnim_s *itnim;
343 unsigned long flags;
344 u32 i, rc, err_cnt = 0;
345 DECLARE_WAIT_QUEUE_HEAD(wq);
346 enum bfi_tskim_status task_status;
347
348 spin_lock_irqsave(&bfad->bfad_lock, flags);
349 for (i = 0; i < MAX_FCP_TARGET; i++) {
350 itnim = bfad_os_get_itnim(im_port, i);
351 if (itnim) {
352 cmnd->SCp.ptr = (char *)&wq;
353 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
354 if (rc != BFA_STATUS_OK) {
355 err_cnt++;
356 continue;
357 }
358
359 /* wait target reset to complete */
360 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
361 wait_event(wq, test_bit(IO_DONE_BIT,
362 (unsigned long *)&cmnd->SCp.Status));
363 spin_lock_irqsave(&bfad->bfad_lock, flags);
364
365 task_status = cmnd->SCp.Status >> 1;
366 if (task_status != BFI_TSKIM_STS_OK) {
367 BFA_DEV_PRINTF(bfad, BFA_ERR,
368 "target reset failure,"
369 " status: %d\n", task_status);
370 err_cnt++;
371 }
372 }
373 }
374 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
375
376 if (err_cnt)
377 return FAILED;
378
379 return SUCCESS;
380}
381
382/**
383 * Scsi_Host template entry slave_destroy.
384 */
385static void
386bfad_im_slave_destroy(struct scsi_device *sdev)
387{
388 sdev->hostdata = NULL;
389 return;
390}
391
392/**
393 * BFA FCS itnim callbacks
394 */
395
396/**
397 * BFA FCS itnim alloc callback, after successful PRLI
398 * Context: Interrupt
399 */
400void
401bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
402 struct bfad_itnim_s **itnim_drv)
403{
404 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
405 if (*itnim_drv == NULL)
406 return;
407
408 (*itnim_drv)->im = bfad->im;
409 *itnim = &(*itnim_drv)->fcs_itnim;
410 (*itnim_drv)->state = ITNIM_STATE_NONE;
411
412 /*
413 * Initiaze the itnim_work
414 */
415 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
416 bfad->bfad_flags |= BFAD_RPORT_ONLINE;
417}
418
419/**
420 * BFA FCS itnim free callback.
421 * Context: Interrupt. bfad_lock is held
422 */
423void
424bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
425{
426 struct bfad_port_s *port;
427 wwn_t wwpn;
428 u32 fcid;
429 char wwpn_str[32], fcid_str[16];
430
431 /* online to free state transtion should not happen */
432 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
433
434 itnim_drv->queue_work = 1;
435 /* offline request is not yet done, use the same request to free */
436 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
437 itnim_drv->queue_work = 0;
438
439 itnim_drv->state = ITNIM_STATE_FREE;
440 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
441 itnim_drv->im_port = port->im_port;
442 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
443 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
444 wwn2str(wwpn_str, wwpn);
445 fcid2str(fcid_str, fcid);
446 bfa_log(bfad->logmod, BFA_LOG_LINUX_ITNIM_FREE,
447 port->im_port->shost->host_no,
448 fcid_str, wwpn_str);
449 bfad_os_itnim_process(itnim_drv);
450}
451
452/**
453 * BFA FCS itnim online callback.
454 * Context: Interrupt. bfad_lock is held
455 */
456void
457bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
458{
459 struct bfad_port_s *port;
460
461 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
462 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
463 itnim_drv->state = ITNIM_STATE_ONLINE;
464 itnim_drv->queue_work = 1;
465 itnim_drv->im_port = port->im_port;
466 bfad_os_itnim_process(itnim_drv);
467}
468
469/**
470 * BFA FCS itnim offline callback.
471 * Context: Interrupt. bfad_lock is held
472 */
473void
474bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
475{
476 struct bfad_port_s *port;
477 struct bfad_s *bfad;
478
479 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
480 bfad = port->bfad;
481 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
482 (port->flags & BFAD_PORT_DELETE)) {
483 itnim_drv->state = ITNIM_STATE_OFFLINE;
484 return;
485 }
486 itnim_drv->im_port = port->im_port;
487 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
488 itnim_drv->queue_work = 1;
489 bfad_os_itnim_process(itnim_drv);
490}
491
492/**
493 * BFA FCS itnim timeout callback.
494 * Context: Interrupt. bfad_lock is held
495 */
496void bfa_fcb_itnim_tov(struct bfad_itnim_s *itnim)
497{
498 itnim->state = ITNIM_STATE_TIMEOUT;
499}
500
501/**
502 * Path TOV processing begin notification -- dummy for linux
503 */
504void
505bfa_fcb_itnim_tov_begin(struct bfad_itnim_s *itnim)
506{
507}
508
509
510
511/**
512 * Allocate a Scsi_Host for a port.
513 */
514int
515bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
516{
517 int error = 1;
518
519 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
520 printk(KERN_WARNING "idr_pre_get failure\n");
521 goto out;
522 }
523
524 error = idr_get_new(&bfad_im_port_index, im_port,
525 &im_port->idr_id);
526 if (error) {
527 printk(KERN_WARNING "idr_get_new failure\n");
528 goto out;
529 }
530
531 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
532 if (!im_port->shost) {
533 error = 1;
534 goto out_free_idr;
535 }
536
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;
545
546 error = bfad_os_scsi_add_host(im_port->shost, im_port, bfad);
547 if (error) {
548 printk(KERN_WARNING "bfad_os_scsi_add_host failure %d\n",
549 error);
550 goto out_fc_rel;
551 }
552
553 /* setup host fixed attribute if the lk supports */
554 bfad_os_fc_host_init(im_port);
555
556 return 0;
557
558out_fc_rel:
559 scsi_host_put(im_port->shost);
560out_free_idr:
561 idr_remove(&bfad_im_port_index, im_port->idr_id);
562out:
563 return error;
564}
565
566void
567bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
568{
569 unsigned long flags;
570
571 bfa_trc(bfad, bfad->inst_no);
572 bfa_log(bfad->logmod, BFA_LOG_LINUX_SCSI_HOST_FREE,
573 im_port->shost->host_no);
574
575 fc_remove_host(im_port->shost);
576
577 scsi_remove_host(im_port->shost);
578 scsi_host_put(im_port->shost);
579
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);
583}
584
585static void
586bfad_im_port_delete_handler(struct work_struct *work)
587{
588 struct bfad_im_port_s *im_port =
589 container_of(work, struct bfad_im_port_s, port_delete_work);
590
591 bfad_im_scsi_host_free(im_port->bfad, im_port);
592 bfad_im_port_clean(im_port);
593 kfree(im_port);
594}
595
596bfa_status_t
597bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
598{
599 int rc = BFA_STATUS_OK;
600 struct bfad_im_port_s *im_port;
601
602 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
603 if (im_port == NULL) {
604 rc = BFA_STATUS_ENOMEM;
605 goto ext;
606 }
607 port->im_port = im_port;
608 im_port->port = port;
609 im_port->bfad = bfad;
610
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);
614
615ext:
616 return rc;
617}
618
619void
620bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
621{
622 struct bfad_im_port_s *im_port = port->im_port;
623
624 queue_work(bfad->im->drv_workq,
625 &im_port->port_delete_work);
626}
627
628void
629bfad_im_port_clean(struct bfad_im_port_s *im_port)
630{
631 struct bfad_fcp_binding *bp, *bp_new;
632 unsigned long flags;
633 struct bfad_s *bfad = im_port->bfad;
634
635 spin_lock_irqsave(&bfad->bfad_lock, flags);
636 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
637 list_entry) {
638 list_del(&bp->list_entry);
639 kfree(bp);
640 }
641
642 /* the itnim_mapped_list must be empty at this time */
643 bfa_assert(list_empty(&im_port->itnim_mapped_list));
644
645 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
646}
647
648void
649bfad_im_port_online(struct bfad_s *bfad, struct bfad_port_s *port)
650{
651}
652
653void
654bfad_im_port_offline(struct bfad_s *bfad, struct bfad_port_s *port)
655{
656}
657
658bfa_status_t
659bfad_im_probe(struct bfad_s *bfad)
660{
661 struct bfad_im_s *im;
662 bfa_status_t rc = BFA_STATUS_OK;
663
664 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
665 if (im == NULL) {
666 rc = BFA_STATUS_ENOMEM;
667 goto ext;
668 }
669
670 bfad->im = im;
671 im->bfad = bfad;
672
673 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
674 kfree(im);
675 rc = BFA_STATUS_FAILED;
676 }
677
678ext:
679 return rc;
680}
681
682void
683bfad_im_probe_undo(struct bfad_s *bfad)
684{
685 if (bfad->im) {
686 bfad_os_destroy_workq(bfad->im);
687 kfree(bfad->im);
688 bfad->im = NULL;
689 }
690}
691
692
693
694
695int
696bfad_os_scsi_add_host(struct Scsi_Host *shost, struct bfad_im_port_s *im_port,
697 struct bfad_s *bfad)
698{
699 struct device *dev;
700
701 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
702 dev = &bfad->pcidev->dev;
703 else
704 dev = &bfad->pport.im_port->shost->shost_gendev;
705
706 return scsi_add_host(shost, dev);
707}
708
709struct Scsi_Host *
710bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
711{
712 struct scsi_host_template *sht;
713
714 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
715 sht = &bfad_im_scsi_host_template;
716 else
717 sht = &bfad_im_vport_template;
718
719 sht->sg_tablesize = bfad->cfg_data.io_max_sge;
720
721 return scsi_host_alloc(sht, sizeof(unsigned long));
722}
723
724void
725bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
726{
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);
730 kfree(im_port);
731}
732
733void
734bfad_os_destroy_workq(struct bfad_im_s *im)
735{
736 if (im && im->drv_workq) {
737 destroy_workqueue(im->drv_workq);
738 im->drv_workq = NULL;
739 }
740}
741
742bfa_status_t
743bfad_os_thread_workq(struct bfad_s *bfad)
744{
745 struct bfad_im_s *im = bfad->im;
746
747 bfa_trc(bfad, 0);
748 snprintf(im->drv_workq_name, BFAD_KOBJ_NAME_LEN, "bfad_wq_%d",
749 bfad->inst_no);
750 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
751 if (!im->drv_workq)
752 return BFA_STATUS_FAILED;
753
754 return BFA_STATUS_OK;
755}
756
757/**
758 * Scsi_Host template entry.
759 *
760 * Description:
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.
764 */
765static int
766bfad_im_slave_configure(struct scsi_device *sdev)
767{
768 if (sdev->tagged_supported)
769 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
770 else
771 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
772
773 return 0;
774}
775
776struct 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,
784
785 .slave_alloc = bfad_im_slave_alloc,
786 .slave_configure = bfad_im_slave_configure,
787 .slave_destroy = bfad_im_slave_destroy,
788
789 .this_id = -1,
790 .sg_tablesize = BFAD_IO_MAX_SGE,
791 .cmd_per_lun = 3,
792 .use_clustering = ENABLE_CLUSTERING,
793 .shost_attrs = bfad_im_host_attrs,
794 .max_sectors = 0xFFFF,
795};
796
797struct 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,
805
806 .slave_alloc = bfad_im_slave_alloc,
807 .slave_configure = bfad_im_slave_configure,
808 .slave_destroy = bfad_im_slave_destroy,
809
810 .this_id = -1,
811 .sg_tablesize = BFAD_IO_MAX_SGE,
812 .cmd_per_lun = 3,
813 .use_clustering = ENABLE_CLUSTERING,
814 .shost_attrs = bfad_im_vport_attrs,
815 .max_sectors = 0xFFFF,
816};
817
818void
819bfad_im_probe_post(struct bfad_im_s *im)
820{
821 flush_workqueue(im->drv_workq);
822}
823
824bfa_status_t
825bfad_im_module_init(void)
826{
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;
831
832 return BFA_STATUS_OK;
833}
834
835void
836bfad_im_module_exit(void)
837{
838 if (bfad_im_scsi_transport_template)
839 fc_release_transport(bfad_im_scsi_transport_template);
840}
841
842void
843bfad_os_itnim_process(struct bfad_itnim_s *itnim_drv)
844{
845 struct bfad_im_s *im = itnim_drv->im;
846
847 if (itnim_drv->queue_work)
848 queue_work(im->drv_workq, &itnim_drv->itnim_work);
849}
850
851void
852bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
853{
854 struct scsi_device *tmp_sdev;
855
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)
863 continue;
864 if (tmp_sdev->ordered_tags)
865 scsi_adjust_queue_depth(tmp_sdev,
866 MSG_ORDERED_TAG,
867 tmp_sdev->queue_depth + 1);
868 else
869 scsi_adjust_queue_depth(tmp_sdev,
870 MSG_SIMPLE_TAG,
871 tmp_sdev->queue_depth + 1);
872
873 itnim->last_ramp_up_time = jiffies;
874 }
875 }
876 }
877}
878
879void
880bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
881{
882 struct scsi_device *tmp_sdev;
883
884 itnim->last_queue_full_time = jiffies;
885
886 shost_for_each_device(tmp_sdev, sdev->host) {
887 if (tmp_sdev->id != sdev->id)
888 continue;
889 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
890 }
891}
892
893
894
895
896struct bfad_itnim_s *
897bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
898{
899 struct bfad_itnim_s *itnim = NULL;
900
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)
904 return itnim;
905 }
906
907 return NULL;
908}
909
910/**
911 * Scsi_Host template entry slave_alloc
912 */
913static int
914bfad_im_slave_alloc(struct scsi_device *sdev)
915{
916 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
917
918 if (!rport || fc_remote_port_chkready(rport))
919 return -ENXIO;
920
921 sdev->hostdata = rport->dd_data;
922
923 return 0;
924}
925
926void
927bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
928{
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;
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800932 struct bfa_pport_attr_s pattr;
933 char model[BFA_ADAPTER_MODEL_NAME_LEN];
934 char fw_ver[BFA_VERSION_LEN];
Jing Huang7725ccf2009-09-23 17:46:15 -0700935
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)));
940
941 fc_host_supported_classes(host) = FC_COS_CLASS3;
942
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;
Roel Kluin7542fa72009-10-15 20:15:17 +0200948 if (bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IPFC)
Jing Huang7725ccf2009-09-23 17:46:15 -0700949 /* 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;
953
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800954 bfa_get_adapter_model(&bfad->bfa, model);
955 bfa_get_adapter_fw_ver(&bfad->bfa, fw_ver);
Jing Huang7725ccf2009-09-23 17:46:15 -0700956 sprintf(fc_host_symbolic_name(host), "Brocade %s FV%s DV%s",
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800957 model, fw_ver, BFAD_DRIVER_VERSION);
Jing Huang7725ccf2009-09-23 17:46:15 -0700958
959 fc_host_supported_speeds(host) = 0;
960 fc_host_supported_speeds(host) |=
961 FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
962 FC_PORTSPEED_1GBIT;
963
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800964 bfa_fcport_get_attr(&bfad->bfa, &pattr);
965 fc_host_maxframe_size(host) = pattr.pport_cfg.maxfrsize;
Jing Huang7725ccf2009-09-23 17:46:15 -0700966}
967
968static void
969bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
970{
971 struct fc_rport_identifiers rport_ids;
972 struct fc_rport *fc_rport;
973 struct bfad_itnim_data_s *itnim_data;
974
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));
979 rport_ids.port_id =
980 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
981 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
982
983 itnim->fc_rport = fc_rport =
984 fc_remote_port_add(im_port->shost, 0, &rport_ids);
985
986 if (!fc_rport)
987 return;
988
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);
992
993 itnim_data = fc_rport->dd_data;
994 itnim_data->itnim = itnim;
995
996 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
997
998 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
999 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1000
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;
1004
1005 return;
1006}
1007
1008/**
1009 * Work queue handler using FC transport service
1010* Context: kernel
1011 */
1012static void
1013bfad_im_itnim_work_handler(struct work_struct *work)
1014{
1015 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1016 itnim_work);
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;
1022 wwn_t wwpn;
1023 u32 fcid;
1024 char wwpn_str[32], fcid_str[16];
1025
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,
1043 itnim->scsi_tgt_id,
1044 fcid_str, wwpn_str);
1045 } else {
1046 printk(KERN_WARNING
1047 "%s: itnim %llx is already in online state\n",
Jing Huangf8ceafd2009-09-25 12:29:54 -07001048 __func__,
Jing Huang7725ccf2009-09-23 17:46:15 -07001049 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1050 }
1051
1052 break;
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);
1066 }
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,
1074 itnim->scsi_tgt_id,
1075 fcid_str, wwpn_str);
1076 }
1077 break;
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);
1090 }
1091 list_del(&itnim->list_entry);
1092 }
1093
1094 kfree(itnim);
1095 break;
1096 default:
1097 bfa_assert(0);
1098 break;
1099 }
1100
1101 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1102}
1103
1104/**
1105 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1106 */
1107static int
1108bfad_im_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1109{
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;
1117 int rc;
1118 s16 sg_cnt = 0;
1119 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1120
1121 rc = fc_remote_port_chkready(rport);
1122 if (rc) {
1123 cmnd->result = rc;
1124 done(cmnd);
1125 return 0;
1126 }
1127
1128 sg_cnt = scsi_dma_map(cmnd);
1129
1130 if (sg_cnt < 0)
1131 return SCSI_MLQUEUE_HOST_BUSY;
1132
1133 cmnd->scsi_done = done;
1134
1135 spin_lock_irqsave(&bfad->bfad_lock, flags);
1136 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1137 printk(KERN_WARNING
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);
1141 goto out_fail_cmd;
1142 }
1143
1144 itnim = itnim_data->itnim;
1145 if (!itnim) {
1146 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1147 goto out_fail_cmd;
1148 }
1149
1150 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1151 itnim->bfa_itnim, sg_cnt);
1152 if (!hal_io) {
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;
1157 }
1158
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);
1163
1164 return 0;
1165
1166out_fail_cmd:
1167 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1168 scsi_dma_unmap(cmnd);
1169 if (done)
1170 done(cmnd);
1171
1172 return 0;
1173}
1174
1175void
1176bfad_os_rport_online_wait(struct bfad_s *bfad)
1177{
1178 int i;
1179 int rport_delay = 10;
1180
1181 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1182 && i < bfa_linkup_delay; i++)
1183 schedule_timeout_uninterruptible(HZ);
1184
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);
1191
1192 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE))
1193 schedule_timeout_uninterruptible(rport_delay * HZ);
1194 }
1195}
1196
1197int
1198bfad_os_get_linkup_delay(struct bfad_s *bfad)
1199{
1200
1201 u8 nwwns = 0;
1202 wwn_t *wwns;
1203 int ldelay;
1204
1205 /*
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
1210 */
1211
1212 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, &wwns);
1213
1214 if (nwwns > 0) {
1215 /* If boot over SAN; linkup_delay = 30sec */
1216 ldelay = 30;
1217 } else {
1218 /* If local boot; linkup_delay = 10sec */
1219 ldelay = 0;
1220 }
1221
1222 return ldelay;
1223}
1224
1225