xref: /openbmc/linux/drivers/scsi/bfa/bfad_im.c (revision d0b73b48)
1 /*
2  * Copyright (c) 2005-2010 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 <linux/export.h>
23 
24 #include "bfad_drv.h"
25 #include "bfad_im.h"
26 #include "bfa_fcs.h"
27 
28 BFA_TRC_FILE(LDRV, IM);
29 
30 DEFINE_IDR(bfad_im_port_index);
31 struct scsi_transport_template *bfad_im_scsi_transport_template;
32 struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
33 static void bfad_im_itnim_work_handler(struct work_struct *work);
34 static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
35 static int bfad_im_slave_alloc(struct scsi_device *sdev);
36 static void bfad_im_fc_rport_add(struct bfad_im_port_s  *im_port,
37 				struct bfad_itnim_s *itnim);
38 
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)
43 {
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;
49 
50 	switch (io_status) {
51 	case BFI_IOIM_STS_OK:
52 		bfa_trc(bfad, scsi_status);
53 		scsi_set_resid(cmnd, 0);
54 
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);
60 		}
61 
62 		if (residue > 0) {
63 			bfa_trc(bfad, residue);
64 			scsi_set_resid(cmnd, residue);
65 			if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
66 				(scsi_bufflen(cmnd) - residue) <
67 					cmnd->underflow) {
68 				bfa_trc(bfad, 0);
69 				host_status = DID_ERROR;
70 			}
71 		}
72 		cmnd->result = ScsiResult(host_status, scsi_status);
73 
74 		break;
75 
76 	case BFI_IOIM_STS_ABORTED:
77 	case BFI_IOIM_STS_TIMEDOUT:
78 	case BFI_IOIM_STS_PATHTOV:
79 	default:
80 		host_status = DID_ERROR;
81 		cmnd->result = ScsiResult(host_status, 0);
82 	}
83 
84 	/* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
85 	if (cmnd->device->host != NULL)
86 		scsi_dma_unmap(cmnd);
87 
88 	cmnd->host_scribble = NULL;
89 	bfa_trc(bfad, cmnd->result);
90 
91 	itnim_data = cmnd->device->hostdata;
92 	if (itnim_data) {
93 		itnim = itnim_data->itnim;
94 		if (!cmnd->result && itnim &&
95 			 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
96 			/* Queue depth adjustment for good status completion */
97 			bfad_ramp_up_qdepth(itnim, cmnd->device);
98 		} else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
99 			/* qfull handling */
100 			bfad_handle_qfull(itnim, cmnd->device);
101 		}
102 	}
103 
104 	cmnd->scsi_done(cmnd);
105 }
106 
107 void
108 bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
109 {
110 	struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
111 	struct bfad_itnim_data_s *itnim_data;
112 	struct bfad_itnim_s *itnim;
113 
114 	cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
115 
116 	/* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
117 	if (cmnd->device->host != NULL)
118 		scsi_dma_unmap(cmnd);
119 
120 	cmnd->host_scribble = NULL;
121 
122 	/* Queue depth adjustment */
123 	if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
124 		itnim_data = cmnd->device->hostdata;
125 		if (itnim_data) {
126 			itnim = itnim_data->itnim;
127 			if (itnim)
128 				bfad_ramp_up_qdepth(itnim, cmnd->device);
129 		}
130 	}
131 
132 	cmnd->scsi_done(cmnd);
133 }
134 
135 void
136 bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
137 {
138 	struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
139 	struct bfad_s         *bfad = drv;
140 
141 	cmnd->result = ScsiResult(DID_ERROR, 0);
142 
143 	/* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
144 	if (cmnd->device->host != NULL)
145 		scsi_dma_unmap(cmnd);
146 
147 	bfa_trc(bfad, cmnd->result);
148 	cmnd->host_scribble = NULL;
149 }
150 
151 void
152 bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
153 		   enum bfi_tskim_status tsk_status)
154 {
155 	struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
156 	wait_queue_head_t *wq;
157 
158 	cmnd->SCp.Status |= tsk_status << 1;
159 	set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
160 	wq = (wait_queue_head_t *) cmnd->SCp.ptr;
161 	cmnd->SCp.ptr = NULL;
162 
163 	if (wq)
164 		wake_up(wq);
165 }
166 
167 /*
168  *  Scsi_Host_template SCSI host template
169  */
170 /*
171  * Scsi_Host template entry, returns BFAD PCI info.
172  */
173 static const char *
174 bfad_im_info(struct Scsi_Host *shost)
175 {
176 	static char     bfa_buf[256];
177 	struct bfad_im_port_s *im_port =
178 			(struct bfad_im_port_s *) shost->hostdata[0];
179 	struct bfad_s *bfad = im_port->bfad;
180 
181 	memset(bfa_buf, 0, sizeof(bfa_buf));
182 	snprintf(bfa_buf, sizeof(bfa_buf),
183 		"Brocade FC/FCOE Adapter, " "hwpath: %s driver: %s",
184 		bfad->pci_name, BFAD_DRIVER_VERSION);
185 
186 	return bfa_buf;
187 }
188 
189 /*
190  * Scsi_Host template entry, aborts the specified SCSI command.
191  *
192  * Returns: SUCCESS or FAILED.
193  */
194 static int
195 bfad_im_abort_handler(struct scsi_cmnd *cmnd)
196 {
197 	struct Scsi_Host *shost = cmnd->device->host;
198 	struct bfad_im_port_s *im_port =
199 			(struct bfad_im_port_s *) shost->hostdata[0];
200 	struct bfad_s         *bfad = im_port->bfad;
201 	struct bfa_ioim_s *hal_io;
202 	unsigned long   flags;
203 	u32        timeout;
204 	int             rc = FAILED;
205 
206 	spin_lock_irqsave(&bfad->bfad_lock, flags);
207 	hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
208 	if (!hal_io) {
209 		/* IO has been completed, retrun success */
210 		rc = SUCCESS;
211 		goto out;
212 	}
213 	if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
214 		rc = FAILED;
215 		goto out;
216 	}
217 
218 	bfa_trc(bfad, hal_io->iotag);
219 	BFA_LOG(KERN_INFO, bfad, bfa_log_level,
220 		"scsi%d: abort cmnd %p iotag %x\n",
221 		im_port->shost->host_no, cmnd, hal_io->iotag);
222 	(void) bfa_ioim_abort(hal_io);
223 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
224 
225 	/* Need to wait until the command get aborted */
226 	timeout = 10;
227 	while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
228 		set_current_state(TASK_UNINTERRUPTIBLE);
229 		schedule_timeout(timeout);
230 		if (timeout < 4 * HZ)
231 			timeout *= 2;
232 	}
233 
234 	cmnd->scsi_done(cmnd);
235 	bfa_trc(bfad, hal_io->iotag);
236 	BFA_LOG(KERN_INFO, bfad, bfa_log_level,
237 		"scsi%d: complete abort 0x%p iotag 0x%x\n",
238 		im_port->shost->host_no, cmnd, hal_io->iotag);
239 	return SUCCESS;
240 out:
241 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
242 	return rc;
243 }
244 
245 static bfa_status_t
246 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
247 		     struct bfad_itnim_s *itnim)
248 {
249 	struct bfa_tskim_s *tskim;
250 	struct bfa_itnim_s *bfa_itnim;
251 	bfa_status_t    rc = BFA_STATUS_OK;
252 	struct scsi_lun scsilun;
253 
254 	tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
255 	if (!tskim) {
256 		BFA_LOG(KERN_ERR, bfad, bfa_log_level,
257 			"target reset, fail to allocate tskim\n");
258 		rc = BFA_STATUS_FAILED;
259 		goto out;
260 	}
261 
262 	/*
263 	 * Set host_scribble to NULL to avoid aborting a task command if
264 	 * happens.
265 	 */
266 	cmnd->host_scribble = NULL;
267 	cmnd->SCp.Status = 0;
268 	bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
269 	memset(&scsilun, 0, sizeof(scsilun));
270 	bfa_tskim_start(tskim, bfa_itnim, scsilun,
271 			    FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
272 out:
273 	return rc;
274 }
275 
276 /*
277  * Scsi_Host template entry, resets a LUN and abort its all commands.
278  *
279  * Returns: SUCCESS or FAILED.
280  *
281  */
282 static int
283 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
284 {
285 	struct Scsi_Host *shost = cmnd->device->host;
286 	struct bfad_im_port_s *im_port =
287 			(struct bfad_im_port_s *) shost->hostdata[0];
288 	struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
289 	struct bfad_s         *bfad = im_port->bfad;
290 	struct bfa_tskim_s *tskim;
291 	struct bfad_itnim_s   *itnim;
292 	struct bfa_itnim_s *bfa_itnim;
293 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
294 	int             rc = SUCCESS;
295 	unsigned long   flags;
296 	enum bfi_tskim_status task_status;
297 	struct scsi_lun scsilun;
298 
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;
305 	}
306 
307 	tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
308 	if (!tskim) {
309 		BFA_LOG(KERN_ERR, bfad, bfa_log_level,
310 				"LUN reset, fail to allocate tskim");
311 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
312 		rc = FAILED;
313 		goto out;
314 	}
315 
316 	/*
317 	 * Set host_scribble to NULL to avoid aborting a task command
318 	 * if happens.
319 	 */
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 	int_to_scsilun(cmnd->device->lun, &scsilun);
325 	bfa_tskim_start(tskim, bfa_itnim, scsilun,
326 			    FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
327 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
328 
329 	wait_event(wq, test_bit(IO_DONE_BIT,
330 			(unsigned long *)&cmnd->SCp.Status));
331 
332 	task_status = cmnd->SCp.Status >> 1;
333 	if (task_status != BFI_TSKIM_STS_OK) {
334 		BFA_LOG(KERN_ERR, bfad, bfa_log_level,
335 			"LUN reset failure, status: %d\n", task_status);
336 		rc = FAILED;
337 	}
338 
339 out:
340 	return rc;
341 }
342 
343 /*
344  * Scsi_Host template entry, resets the bus and abort all commands.
345  */
346 static int
347 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
348 {
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;
358 
359 	spin_lock_irqsave(&bfad->bfad_lock, flags);
360 	for (i = 0; i < MAX_FCP_TARGET; i++) {
361 		itnim = bfad_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;
368 			}
369 
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);
375 
376 			task_status = cmnd->SCp.Status >> 1;
377 			if (task_status != BFI_TSKIM_STS_OK) {
378 				BFA_LOG(KERN_ERR, bfad, bfa_log_level,
379 					"target reset failure,"
380 					" status: %d\n", task_status);
381 				err_cnt++;
382 			}
383 		}
384 	}
385 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
386 
387 	if (err_cnt)
388 		return FAILED;
389 
390 	return SUCCESS;
391 }
392 
393 /*
394  * Scsi_Host template entry slave_destroy.
395  */
396 static void
397 bfad_im_slave_destroy(struct scsi_device *sdev)
398 {
399 	sdev->hostdata = NULL;
400 	return;
401 }
402 
403 /*
404  *  BFA FCS itnim callbacks
405  */
406 
407 /*
408  * BFA FCS itnim alloc callback, after successful PRLI
409  * Context: Interrupt
410  */
411 void
412 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
413 		    struct bfad_itnim_s **itnim_drv)
414 {
415 	*itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
416 	if (*itnim_drv == NULL)
417 		return;
418 
419 	(*itnim_drv)->im = bfad->im;
420 	*itnim = &(*itnim_drv)->fcs_itnim;
421 	(*itnim_drv)->state = ITNIM_STATE_NONE;
422 
423 	/*
424 	 * Initiaze the itnim_work
425 	 */
426 	INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
427 	bfad->bfad_flags |= BFAD_RPORT_ONLINE;
428 }
429 
430 /*
431  * BFA FCS itnim free callback.
432  * Context: Interrupt. bfad_lock is held
433  */
434 void
435 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
436 {
437 	struct bfad_port_s    *port;
438 	wwn_t wwpn;
439 	u32 fcid;
440 	char wwpn_str[32], fcid_str[16];
441 	struct bfad_im_s	*im = itnim_drv->im;
442 
443 	/* online to free state transtion should not happen */
444 	WARN_ON(itnim_drv->state == ITNIM_STATE_ONLINE);
445 
446 	itnim_drv->queue_work = 1;
447 	/* offline request is not yet done, use the same request to free */
448 	if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
449 		itnim_drv->queue_work = 0;
450 
451 	itnim_drv->state = ITNIM_STATE_FREE;
452 	port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
453 	itnim_drv->im_port = port->im_port;
454 	wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
455 	fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
456 	wwn2str(wwpn_str, wwpn);
457 	fcid2str(fcid_str, fcid);
458 	BFA_LOG(KERN_INFO, bfad, bfa_log_level,
459 		"ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
460 		port->im_port->shost->host_no,
461 		fcid_str, wwpn_str);
462 
463 	/* ITNIM processing */
464 	if (itnim_drv->queue_work)
465 		queue_work(im->drv_workq, &itnim_drv->itnim_work);
466 }
467 
468 /*
469  * BFA FCS itnim online callback.
470  * Context: Interrupt. bfad_lock is held
471  */
472 void
473 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
474 {
475 	struct bfad_port_s    *port;
476 	struct bfad_im_s	*im = itnim_drv->im;
477 
478 	itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
479 	port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
480 	itnim_drv->state = ITNIM_STATE_ONLINE;
481 	itnim_drv->queue_work = 1;
482 	itnim_drv->im_port = port->im_port;
483 
484 	/* ITNIM processing */
485 	if (itnim_drv->queue_work)
486 		queue_work(im->drv_workq, &itnim_drv->itnim_work);
487 }
488 
489 /*
490  * BFA FCS itnim offline callback.
491  * Context: Interrupt. bfad_lock is held
492  */
493 void
494 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
495 {
496 	struct bfad_port_s    *port;
497 	struct bfad_s *bfad;
498 	struct bfad_im_s	*im = itnim_drv->im;
499 
500 	port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
501 	bfad = port->bfad;
502 	if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
503 		 (port->flags & BFAD_PORT_DELETE)) {
504 		itnim_drv->state = ITNIM_STATE_OFFLINE;
505 		return;
506 	}
507 	itnim_drv->im_port = port->im_port;
508 	itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
509 	itnim_drv->queue_work = 1;
510 
511 	/* ITNIM processing */
512 	if (itnim_drv->queue_work)
513 		queue_work(im->drv_workq, &itnim_drv->itnim_work);
514 }
515 
516 /*
517  * Allocate a Scsi_Host for a port.
518  */
519 int
520 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
521 			struct device *dev)
522 {
523 	int error = 1;
524 
525 	mutex_lock(&bfad_mutex);
526 	if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
527 		mutex_unlock(&bfad_mutex);
528 		printk(KERN_WARNING "idr_pre_get failure\n");
529 		goto out;
530 	}
531 
532 	error = idr_get_new(&bfad_im_port_index, im_port,
533 					 &im_port->idr_id);
534 	if (error) {
535 		mutex_unlock(&bfad_mutex);
536 		printk(KERN_WARNING "idr_get_new failure\n");
537 		goto out;
538 	}
539 
540 	mutex_unlock(&bfad_mutex);
541 
542 	im_port->shost = bfad_scsi_host_alloc(im_port, bfad);
543 	if (!im_port->shost) {
544 		error = 1;
545 		goto out_free_idr;
546 	}
547 
548 	im_port->shost->hostdata[0] = (unsigned long)im_port;
549 	im_port->shost->unique_id = im_port->idr_id;
550 	im_port->shost->this_id = -1;
551 	im_port->shost->max_id = MAX_FCP_TARGET;
552 	im_port->shost->max_lun = MAX_FCP_LUN;
553 	im_port->shost->max_cmd_len = 16;
554 	im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
555 	if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
556 		im_port->shost->transportt = bfad_im_scsi_transport_template;
557 	else
558 		im_port->shost->transportt =
559 				bfad_im_scsi_vport_transport_template;
560 
561 	error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
562 	if (error) {
563 		printk(KERN_WARNING "scsi_add_host failure %d\n", error);
564 		goto out_fc_rel;
565 	}
566 
567 	return 0;
568 
569 out_fc_rel:
570 	scsi_host_put(im_port->shost);
571 	im_port->shost = NULL;
572 out_free_idr:
573 	mutex_lock(&bfad_mutex);
574 	idr_remove(&bfad_im_port_index, im_port->idr_id);
575 	mutex_unlock(&bfad_mutex);
576 out:
577 	return error;
578 }
579 
580 void
581 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
582 {
583 	bfa_trc(bfad, bfad->inst_no);
584 	BFA_LOG(KERN_INFO, bfad, bfa_log_level, "Free scsi%d\n",
585 			im_port->shost->host_no);
586 
587 	fc_remove_host(im_port->shost);
588 
589 	scsi_remove_host(im_port->shost);
590 	scsi_host_put(im_port->shost);
591 
592 	mutex_lock(&bfad_mutex);
593 	idr_remove(&bfad_im_port_index, im_port->idr_id);
594 	mutex_unlock(&bfad_mutex);
595 }
596 
597 static void
598 bfad_im_port_delete_handler(struct work_struct *work)
599 {
600 	struct bfad_im_port_s *im_port =
601 		container_of(work, struct bfad_im_port_s, port_delete_work);
602 
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 	}
607 }
608 
609 bfa_status_t
610 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
611 {
612 	int             rc = BFA_STATUS_OK;
613 	struct bfad_im_port_s *im_port;
614 
615 	im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
616 	if (im_port == NULL) {
617 		rc = BFA_STATUS_ENOMEM;
618 		goto ext;
619 	}
620 	port->im_port = im_port;
621 	im_port->port = port;
622 	im_port->bfad = bfad;
623 
624 	INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
625 	INIT_LIST_HEAD(&im_port->itnim_mapped_list);
626 	INIT_LIST_HEAD(&im_port->binding_list);
627 
628 ext:
629 	return rc;
630 }
631 
632 void
633 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
634 {
635 	struct bfad_im_port_s *im_port = port->im_port;
636 
637 	queue_work(bfad->im->drv_workq,
638 				&im_port->port_delete_work);
639 }
640 
641 void
642 bfad_im_port_clean(struct bfad_im_port_s *im_port)
643 {
644 	struct bfad_fcp_binding *bp, *bp_new;
645 	unsigned long flags;
646 	struct bfad_s *bfad =  im_port->bfad;
647 
648 	spin_lock_irqsave(&bfad->bfad_lock, flags);
649 	list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
650 					list_entry) {
651 		list_del(&bp->list_entry);
652 		kfree(bp);
653 	}
654 
655 	/* the itnim_mapped_list must be empty at this time */
656 	WARN_ON(!list_empty(&im_port->itnim_mapped_list));
657 
658 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
659 }
660 
661 static void bfad_aen_im_notify_handler(struct work_struct *work)
662 {
663 	struct bfad_im_s *im =
664 		container_of(work, struct bfad_im_s, aen_im_notify_work);
665 	struct bfa_aen_entry_s *aen_entry;
666 	struct bfad_s *bfad = im->bfad;
667 	struct Scsi_Host *shost = bfad->pport.im_port->shost;
668 	void *event_data;
669 	unsigned long flags;
670 
671 	while (!list_empty(&bfad->active_aen_q)) {
672 		spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
673 		bfa_q_deq(&bfad->active_aen_q, &aen_entry);
674 		spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
675 		event_data = (char *)aen_entry + sizeof(struct list_head);
676 		fc_host_post_vendor_event(shost, fc_get_event_number(),
677 				sizeof(struct bfa_aen_entry_s) -
678 				sizeof(struct list_head),
679 				(char *)event_data, BFAD_NL_VENDOR_ID);
680 		spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
681 		list_add_tail(&aen_entry->qe, &bfad->free_aen_q);
682 		spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
683 	}
684 }
685 
686 bfa_status_t
687 bfad_im_probe(struct bfad_s *bfad)
688 {
689 	struct bfad_im_s      *im;
690 
691 	im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
692 	if (im == NULL)
693 		return BFA_STATUS_ENOMEM;
694 
695 	bfad->im = im;
696 	im->bfad = bfad;
697 
698 	if (bfad_thread_workq(bfad) != BFA_STATUS_OK) {
699 		kfree(im);
700 		return BFA_STATUS_FAILED;
701 	}
702 
703 	INIT_WORK(&im->aen_im_notify_work, bfad_aen_im_notify_handler);
704 	return BFA_STATUS_OK;
705 }
706 
707 void
708 bfad_im_probe_undo(struct bfad_s *bfad)
709 {
710 	if (bfad->im) {
711 		bfad_destroy_workq(bfad->im);
712 		kfree(bfad->im);
713 		bfad->im = NULL;
714 	}
715 }
716 
717 struct Scsi_Host *
718 bfad_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
719 {
720 	struct scsi_host_template *sht;
721 
722 	if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
723 		sht = &bfad_im_scsi_host_template;
724 	else
725 		sht = &bfad_im_vport_template;
726 
727 	if (max_xfer_size != BFAD_MAX_SECTORS >> 1)
728 		sht->max_sectors = max_xfer_size << 1;
729 
730 	sht->sg_tablesize = bfad->cfg_data.io_max_sge;
731 
732 	return scsi_host_alloc(sht, sizeof(unsigned long));
733 }
734 
735 void
736 bfad_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
737 {
738 	if (!(im_port->flags & BFAD_PORT_DELETE))
739 		flush_workqueue(bfad->im->drv_workq);
740 	bfad_im_scsi_host_free(im_port->bfad, im_port);
741 	bfad_im_port_clean(im_port);
742 	kfree(im_port);
743 }
744 
745 void
746 bfad_destroy_workq(struct bfad_im_s *im)
747 {
748 	if (im && im->drv_workq) {
749 		flush_workqueue(im->drv_workq);
750 		destroy_workqueue(im->drv_workq);
751 		im->drv_workq = NULL;
752 	}
753 }
754 
755 bfa_status_t
756 bfad_thread_workq(struct bfad_s *bfad)
757 {
758 	struct bfad_im_s      *im = bfad->im;
759 
760 	bfa_trc(bfad, 0);
761 	snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
762 		 bfad->inst_no);
763 	im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
764 	if (!im->drv_workq)
765 		return BFA_STATUS_FAILED;
766 
767 	return BFA_STATUS_OK;
768 }
769 
770 /*
771  * Scsi_Host template entry.
772  *
773  * Description:
774  * OS entry point to adjust the queue_depths on a per-device basis.
775  * Called once per device during the bus scan.
776  * Return non-zero if fails.
777  */
778 static int
779 bfad_im_slave_configure(struct scsi_device *sdev)
780 {
781 	if (sdev->tagged_supported)
782 		scsi_activate_tcq(sdev, bfa_lun_queue_depth);
783 	else
784 		scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
785 
786 	return 0;
787 }
788 
789 struct scsi_host_template bfad_im_scsi_host_template = {
790 	.module = THIS_MODULE,
791 	.name = BFAD_DRIVER_NAME,
792 	.info = bfad_im_info,
793 	.queuecommand = bfad_im_queuecommand,
794 	.eh_abort_handler = bfad_im_abort_handler,
795 	.eh_device_reset_handler = bfad_im_reset_lun_handler,
796 	.eh_bus_reset_handler = bfad_im_reset_bus_handler,
797 
798 	.slave_alloc = bfad_im_slave_alloc,
799 	.slave_configure = bfad_im_slave_configure,
800 	.slave_destroy = bfad_im_slave_destroy,
801 
802 	.this_id = -1,
803 	.sg_tablesize = BFAD_IO_MAX_SGE,
804 	.cmd_per_lun = 3,
805 	.use_clustering = ENABLE_CLUSTERING,
806 	.shost_attrs = bfad_im_host_attrs,
807 	.max_sectors = BFAD_MAX_SECTORS,
808 	.vendor_id = BFA_PCI_VENDOR_ID_BROCADE,
809 };
810 
811 struct scsi_host_template bfad_im_vport_template = {
812 	.module = THIS_MODULE,
813 	.name = BFAD_DRIVER_NAME,
814 	.info = bfad_im_info,
815 	.queuecommand = bfad_im_queuecommand,
816 	.eh_abort_handler = bfad_im_abort_handler,
817 	.eh_device_reset_handler = bfad_im_reset_lun_handler,
818 	.eh_bus_reset_handler = bfad_im_reset_bus_handler,
819 
820 	.slave_alloc = bfad_im_slave_alloc,
821 	.slave_configure = bfad_im_slave_configure,
822 	.slave_destroy = bfad_im_slave_destroy,
823 
824 	.this_id = -1,
825 	.sg_tablesize = BFAD_IO_MAX_SGE,
826 	.cmd_per_lun = 3,
827 	.use_clustering = ENABLE_CLUSTERING,
828 	.shost_attrs = bfad_im_vport_attrs,
829 	.max_sectors = BFAD_MAX_SECTORS,
830 };
831 
832 bfa_status_t
833 bfad_im_module_init(void)
834 {
835 	bfad_im_scsi_transport_template =
836 		fc_attach_transport(&bfad_im_fc_function_template);
837 	if (!bfad_im_scsi_transport_template)
838 		return BFA_STATUS_ENOMEM;
839 
840 	bfad_im_scsi_vport_transport_template =
841 		fc_attach_transport(&bfad_im_vport_fc_function_template);
842 	if (!bfad_im_scsi_vport_transport_template) {
843 		fc_release_transport(bfad_im_scsi_transport_template);
844 		return BFA_STATUS_ENOMEM;
845 	}
846 
847 	return BFA_STATUS_OK;
848 }
849 
850 void
851 bfad_im_module_exit(void)
852 {
853 	if (bfad_im_scsi_transport_template)
854 		fc_release_transport(bfad_im_scsi_transport_template);
855 
856 	if (bfad_im_scsi_vport_transport_template)
857 		fc_release_transport(bfad_im_scsi_vport_transport_template);
858 }
859 
860 void
861 bfad_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
862 {
863 	struct scsi_device *tmp_sdev;
864 
865 	if (((jiffies - itnim->last_ramp_up_time) >
866 		BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
867 		((jiffies - itnim->last_queue_full_time) >
868 		BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
869 		shost_for_each_device(tmp_sdev, sdev->host) {
870 			if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
871 				if (tmp_sdev->id != sdev->id)
872 					continue;
873 				if (tmp_sdev->ordered_tags)
874 					scsi_adjust_queue_depth(tmp_sdev,
875 						MSG_ORDERED_TAG,
876 						tmp_sdev->queue_depth + 1);
877 				else
878 					scsi_adjust_queue_depth(tmp_sdev,
879 						MSG_SIMPLE_TAG,
880 						tmp_sdev->queue_depth + 1);
881 
882 				itnim->last_ramp_up_time = jiffies;
883 			}
884 		}
885 	}
886 }
887 
888 void
889 bfad_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
890 {
891 	struct scsi_device *tmp_sdev;
892 
893 	itnim->last_queue_full_time = jiffies;
894 
895 	shost_for_each_device(tmp_sdev, sdev->host) {
896 		if (tmp_sdev->id != sdev->id)
897 			continue;
898 		scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
899 	}
900 }
901 
902 struct bfad_itnim_s *
903 bfad_get_itnim(struct bfad_im_port_s *im_port, int id)
904 {
905 	struct bfad_itnim_s   *itnim = NULL;
906 
907 	/* Search the mapped list for this target ID */
908 	list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
909 		if (id == itnim->scsi_tgt_id)
910 			return itnim;
911 	}
912 
913 	return NULL;
914 }
915 
916 /*
917  * Function is invoked from the SCSI Host Template slave_alloc() entry point.
918  * Has the logic to query the LUN Mask database to check if this LUN needs to
919  * be made visible to the SCSI mid-layer or not.
920  *
921  * Returns BFA_STATUS_OK if this LUN needs to be added to the OS stack.
922  * Returns -ENXIO to notify SCSI mid-layer to not add this LUN to the OS stack.
923  */
924 static int
925 bfad_im_check_if_make_lun_visible(struct scsi_device *sdev,
926 				  struct fc_rport *rport)
927 {
928 	struct bfad_itnim_data_s *itnim_data =
929 				(struct bfad_itnim_data_s *) rport->dd_data;
930 	struct bfa_s *bfa = itnim_data->itnim->bfa_itnim->bfa;
931 	struct bfa_rport_s *bfa_rport = itnim_data->itnim->bfa_itnim->rport;
932 	struct bfa_lun_mask_s *lun_list = bfa_get_lun_mask_list(bfa);
933 	int i = 0, ret = -ENXIO;
934 
935 	for (i = 0; i < MAX_LUN_MASK_CFG; i++) {
936 		if (lun_list[i].state == BFA_IOIM_LUN_MASK_ACTIVE &&
937 		    scsilun_to_int(&lun_list[i].lun) == sdev->lun &&
938 		    lun_list[i].rp_tag == bfa_rport->rport_tag &&
939 		    lun_list[i].lp_tag == (u8)bfa_rport->rport_info.lp_tag) {
940 			ret = BFA_STATUS_OK;
941 			break;
942 		}
943 	}
944 	return ret;
945 }
946 
947 /*
948  * Scsi_Host template entry slave_alloc
949  */
950 static int
951 bfad_im_slave_alloc(struct scsi_device *sdev)
952 {
953 	struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
954 	struct bfad_itnim_data_s *itnim_data =
955 				(struct bfad_itnim_data_s *) rport->dd_data;
956 	struct bfa_s *bfa = itnim_data->itnim->bfa_itnim->bfa;
957 
958 	if (!rport || fc_remote_port_chkready(rport))
959 		return -ENXIO;
960 
961 	if (bfa_get_lun_mask_status(bfa) == BFA_LUNMASK_ENABLED) {
962 		/*
963 		 * We should not mask LUN 0 - since this will translate
964 		 * to no LUN / TARGET for SCSI ml resulting no scan.
965 		 */
966 		if (sdev->lun == 0) {
967 			sdev->sdev_bflags |= BLIST_NOREPORTLUN |
968 					     BLIST_SPARSELUN;
969 			goto done;
970 		}
971 
972 		/*
973 		 * Query LUN Mask configuration - to expose this LUN
974 		 * to the SCSI mid-layer or to mask it.
975 		 */
976 		if (bfad_im_check_if_make_lun_visible(sdev, rport) !=
977 							BFA_STATUS_OK)
978 			return -ENXIO;
979 	}
980 done:
981 	sdev->hostdata = rport->dd_data;
982 
983 	return 0;
984 }
985 
986 u32
987 bfad_im_supported_speeds(struct bfa_s *bfa)
988 {
989 	struct bfa_ioc_attr_s *ioc_attr;
990 	u32 supported_speed = 0;
991 
992 	ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
993 	if (!ioc_attr)
994 		return 0;
995 
996 	bfa_ioc_get_attr(&bfa->ioc, ioc_attr);
997 	if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_16GBPS)
998 		supported_speed |=  FC_PORTSPEED_16GBIT | FC_PORTSPEED_8GBIT |
999 				FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT;
1000 	else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
1001 		if (ioc_attr->adapter_attr.is_mezz) {
1002 			supported_speed |= FC_PORTSPEED_8GBIT |
1003 				FC_PORTSPEED_4GBIT |
1004 				FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1005 		} else {
1006 			supported_speed |= FC_PORTSPEED_8GBIT |
1007 				FC_PORTSPEED_4GBIT |
1008 				FC_PORTSPEED_2GBIT;
1009 		}
1010 	} else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
1011 		supported_speed |=  FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
1012 				FC_PORTSPEED_1GBIT;
1013 	} else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
1014 		supported_speed |= FC_PORTSPEED_10GBIT;
1015 	}
1016 	kfree(ioc_attr);
1017 	return supported_speed;
1018 }
1019 
1020 void
1021 bfad_fc_host_init(struct bfad_im_port_s *im_port)
1022 {
1023 	struct Scsi_Host *host = im_port->shost;
1024 	struct bfad_s         *bfad = im_port->bfad;
1025 	struct bfad_port_s    *port = im_port->port;
1026 	char symname[BFA_SYMNAME_MAXLEN];
1027 	struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
1028 
1029 	fc_host_node_name(host) =
1030 		cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
1031 	fc_host_port_name(host) =
1032 		cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
1033 	fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
1034 
1035 	fc_host_supported_classes(host) = FC_COS_CLASS3;
1036 
1037 	memset(fc_host_supported_fc4s(host), 0,
1038 	       sizeof(fc_host_supported_fc4s(host)));
1039 	if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
1040 		/* For FCP type 0x08 */
1041 		fc_host_supported_fc4s(host)[2] = 1;
1042 	/* For fibre channel services type 0x20 */
1043 	fc_host_supported_fc4s(host)[7] = 1;
1044 
1045 	strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
1046 		BFA_SYMNAME_MAXLEN);
1047 	sprintf(fc_host_symbolic_name(host), "%s", symname);
1048 
1049 	fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
1050 	fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
1051 }
1052 
1053 static void
1054 bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
1055 {
1056 	struct fc_rport_identifiers rport_ids;
1057 	struct fc_rport *fc_rport;
1058 	struct bfad_itnim_data_s *itnim_data;
1059 
1060 	rport_ids.node_name =
1061 		cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
1062 	rport_ids.port_name =
1063 		cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1064 	rport_ids.port_id =
1065 		bfa_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
1066 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1067 
1068 	itnim->fc_rport = fc_rport =
1069 		fc_remote_port_add(im_port->shost, 0, &rport_ids);
1070 
1071 	if (!fc_rport)
1072 		return;
1073 
1074 	fc_rport->maxframe_size =
1075 		bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1076 	fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1077 
1078 	itnim_data = fc_rport->dd_data;
1079 	itnim_data->itnim = itnim;
1080 
1081 	rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1082 
1083 	if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1084 		fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1085 
1086 	if ((fc_rport->scsi_target_id != -1)
1087 	    && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1088 		itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1089 
1090 	itnim->channel = fc_rport->channel;
1091 
1092 	return;
1093 }
1094 
1095 /*
1096  * Work queue handler using FC transport service
1097 * Context: kernel
1098  */
1099 static void
1100 bfad_im_itnim_work_handler(struct work_struct *work)
1101 {
1102 	struct bfad_itnim_s   *itnim = container_of(work, struct bfad_itnim_s,
1103 							itnim_work);
1104 	struct bfad_im_s      *im = itnim->im;
1105 	struct bfad_s         *bfad = im->bfad;
1106 	struct bfad_im_port_s *im_port;
1107 	unsigned long   flags;
1108 	struct fc_rport *fc_rport;
1109 	wwn_t wwpn;
1110 	u32 fcid;
1111 	char wwpn_str[32], fcid_str[16];
1112 
1113 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1114 	im_port = itnim->im_port;
1115 	bfa_trc(bfad, itnim->state);
1116 	switch (itnim->state) {
1117 	case ITNIM_STATE_ONLINE:
1118 		if (!itnim->fc_rport) {
1119 			spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1120 			bfad_im_fc_rport_add(im_port, itnim);
1121 			spin_lock_irqsave(&bfad->bfad_lock, flags);
1122 			wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1123 			fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1124 			wwn2str(wwpn_str, wwpn);
1125 			fcid2str(fcid_str, fcid);
1126 			list_add_tail(&itnim->list_entry,
1127 				&im_port->itnim_mapped_list);
1128 			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
1129 				"ITNIM ONLINE Target: %d:0:%d "
1130 				"FCID: %s WWPN: %s\n",
1131 				im_port->shost->host_no,
1132 				itnim->scsi_tgt_id,
1133 				fcid_str, wwpn_str);
1134 		} else {
1135 			printk(KERN_WARNING
1136 				"%s: itnim %llx is already in online state\n",
1137 				__func__,
1138 				bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1139 		}
1140 
1141 		break;
1142 	case ITNIM_STATE_OFFLINE_PENDING:
1143 		itnim->state = ITNIM_STATE_OFFLINE;
1144 		if (itnim->fc_rport) {
1145 			fc_rport = itnim->fc_rport;
1146 			((struct bfad_itnim_data_s *)
1147 				fc_rport->dd_data)->itnim = NULL;
1148 			itnim->fc_rport = NULL;
1149 			if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1150 				spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1151 				fc_rport->dev_loss_tmo =
1152 					bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1153 				fc_remote_port_delete(fc_rport);
1154 				spin_lock_irqsave(&bfad->bfad_lock, flags);
1155 			}
1156 			wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1157 			fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1158 			wwn2str(wwpn_str, wwpn);
1159 			fcid2str(fcid_str, fcid);
1160 			list_del(&itnim->list_entry);
1161 			BFA_LOG(KERN_INFO, bfad, bfa_log_level,
1162 				"ITNIM OFFLINE Target: %d:0:%d "
1163 				"FCID: %s WWPN: %s\n",
1164 				im_port->shost->host_no,
1165 				itnim->scsi_tgt_id,
1166 				fcid_str, wwpn_str);
1167 		}
1168 		break;
1169 	case ITNIM_STATE_FREE:
1170 		if (itnim->fc_rport) {
1171 			fc_rport = itnim->fc_rport;
1172 			((struct bfad_itnim_data_s *)
1173 				fc_rport->dd_data)->itnim = NULL;
1174 			itnim->fc_rport = NULL;
1175 			if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1176 				spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1177 				fc_rport->dev_loss_tmo =
1178 					bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1179 				fc_remote_port_delete(fc_rport);
1180 				spin_lock_irqsave(&bfad->bfad_lock, flags);
1181 			}
1182 			list_del(&itnim->list_entry);
1183 		}
1184 
1185 		kfree(itnim);
1186 		break;
1187 	default:
1188 		WARN_ON(1);
1189 		break;
1190 	}
1191 
1192 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1193 }
1194 
1195 /*
1196  * Scsi_Host template entry, queue a SCSI command to the BFAD.
1197  */
1198 static int
1199 bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1200 {
1201 	struct bfad_im_port_s *im_port =
1202 		(struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1203 	struct bfad_s         *bfad = im_port->bfad;
1204 	struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1205 	struct bfad_itnim_s   *itnim;
1206 	struct bfa_ioim_s *hal_io;
1207 	unsigned long   flags;
1208 	int             rc;
1209 	int       sg_cnt = 0;
1210 	struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1211 
1212 	rc = fc_remote_port_chkready(rport);
1213 	if (rc) {
1214 		cmnd->result = rc;
1215 		done(cmnd);
1216 		return 0;
1217 	}
1218 
1219 	if (bfad->bfad_flags & BFAD_EEH_BUSY) {
1220 		if (bfad->bfad_flags & BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE)
1221 			cmnd->result = DID_NO_CONNECT << 16;
1222 		else
1223 			cmnd->result = DID_REQUEUE << 16;
1224 		done(cmnd);
1225 		return 0;
1226 	}
1227 
1228 	sg_cnt = scsi_dma_map(cmnd);
1229 	if (sg_cnt < 0)
1230 		return SCSI_MLQUEUE_HOST_BUSY;
1231 
1232 	cmnd->scsi_done = done;
1233 
1234 	spin_lock_irqsave(&bfad->bfad_lock, flags);
1235 	if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1236 		printk(KERN_WARNING
1237 			"bfad%d, queuecommand %p %x failed, BFA stopped\n",
1238 		       bfad->inst_no, cmnd, cmnd->cmnd[0]);
1239 		cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1240 		goto out_fail_cmd;
1241 	}
1242 
1243 
1244 	itnim = itnim_data->itnim;
1245 	if (!itnim) {
1246 		cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1247 		goto out_fail_cmd;
1248 	}
1249 
1250 	hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1251 				    itnim->bfa_itnim, sg_cnt);
1252 	if (!hal_io) {
1253 		printk(KERN_WARNING "hal_io failure\n");
1254 		spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1255 		scsi_dma_unmap(cmnd);
1256 		return SCSI_MLQUEUE_HOST_BUSY;
1257 	}
1258 
1259 	cmnd->host_scribble = (char *)hal_io;
1260 	bfa_ioim_start(hal_io);
1261 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1262 
1263 	return 0;
1264 
1265 out_fail_cmd:
1266 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1267 	scsi_dma_unmap(cmnd);
1268 	if (done)
1269 		done(cmnd);
1270 
1271 	return 0;
1272 }
1273 
1274 static DEF_SCSI_QCMD(bfad_im_queuecommand)
1275 
1276 void
1277 bfad_rport_online_wait(struct bfad_s *bfad)
1278 {
1279 	int i;
1280 	int rport_delay = 10;
1281 
1282 	for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1283 		&& i < bfa_linkup_delay; i++) {
1284 		set_current_state(TASK_UNINTERRUPTIBLE);
1285 		schedule_timeout(HZ);
1286 	}
1287 
1288 	if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1289 		rport_delay = rport_delay < bfa_linkup_delay ?
1290 			rport_delay : bfa_linkup_delay;
1291 		for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1292 			&& i < rport_delay; i++) {
1293 			set_current_state(TASK_UNINTERRUPTIBLE);
1294 			schedule_timeout(HZ);
1295 		}
1296 
1297 		if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1298 			set_current_state(TASK_UNINTERRUPTIBLE);
1299 			schedule_timeout(rport_delay * HZ);
1300 		}
1301 	}
1302 }
1303 
1304 int
1305 bfad_get_linkup_delay(struct bfad_s *bfad)
1306 {
1307 	u8		nwwns = 0;
1308 	wwn_t		wwns[BFA_PREBOOT_BOOTLUN_MAX];
1309 	int		linkup_delay;
1310 
1311 	/*
1312 	 * Querying for the boot target port wwns
1313 	 * -- read from boot information in flash.
1314 	 * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1315 	 * else => local boot machine set linkup_delay = 0
1316 	 */
1317 
1318 	bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1319 
1320 	if (nwwns > 0)
1321 		/* If Boot over SAN set linkup_delay = 30sec */
1322 		linkup_delay = 30;
1323 	else
1324 		/* If local boot; no linkup_delay */
1325 		linkup_delay = 0;
1326 
1327 	return linkup_delay;
1328 }
1329