blob: 8ca967dee66d4eac1b551c82ff5416454e0ffcb6 [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07002 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
Jing Huang7725ccf2009-09-23 17:46:15 -07003 * 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
Jing Huang5fbe25c2010-10-18 17:17:23 -070018/*
Jing Huang7725ccf2009-09-23 17:46:15 -070019 * bfad_im.c Linux driver IM module.
20 */
21
22#include "bfad_drv.h"
23#include "bfad_im.h"
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070024#include "bfa_cb_ioim.h"
25#include "bfa_fcs.h"
Jing Huang7725ccf2009-09-23 17:46:15 -070026
27BFA_TRC_FILE(LDRV, IM);
28
29DEFINE_IDR(bfad_im_port_index);
30struct scsi_transport_template *bfad_im_scsi_transport_template;
Jing Huangb5042932010-03-19 11:05:39 -070031struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
Jing Huang7725ccf2009-09-23 17:46:15 -070032static void bfad_im_itnim_work_handler(struct work_struct *work);
Jeff Garzikf2812332010-11-16 02:10:29 -050033static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
Jing Huang7725ccf2009-09-23 17:46:15 -070034static int bfad_im_slave_alloc(struct scsi_device *sdev);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070035static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
36 struct bfad_itnim_s *itnim);
Jing Huang7725ccf2009-09-23 17:46:15 -070037
38void
39bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
40 enum bfi_ioim_status io_status, u8 scsi_status,
41 int sns_len, u8 *sns_info, s32 residue)
42{
43 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
44 struct bfad_s *bfad = drv;
45 struct bfad_itnim_data_s *itnim_data;
46 struct bfad_itnim_s *itnim;
Krishna Gudipati95aa0602010-03-05 19:38:27 -080047 u8 host_status = DID_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -070048
49 switch (io_status) {
50 case BFI_IOIM_STS_OK:
51 bfa_trc(bfad, scsi_status);
Jing Huang7725ccf2009-09-23 17:46:15 -070052 scsi_set_resid(cmnd, 0);
53
54 if (sns_len > 0) {
55 bfa_trc(bfad, sns_len);
56 if (sns_len > SCSI_SENSE_BUFFERSIZE)
57 sns_len = SCSI_SENSE_BUFFERSIZE;
58 memcpy(cmnd->sense_buffer, sns_info, sns_len);
59 }
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070060
Krishna Gudipati95aa0602010-03-05 19:38:27 -080061 if (residue > 0) {
62 bfa_trc(bfad, residue);
Jing Huang7725ccf2009-09-23 17:46:15 -070063 scsi_set_resid(cmnd, residue);
Krishna Gudipati95aa0602010-03-05 19:38:27 -080064 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;
69 }
70 }
71 cmnd->result = ScsiResult(host_status, scsi_status);
72
Jing Huang7725ccf2009-09-23 17:46:15 -070073 break;
74
75 case BFI_IOIM_STS_ABORTED:
76 case BFI_IOIM_STS_TIMEDOUT:
77 case BFI_IOIM_STS_PATHTOV:
78 default:
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070079 host_status = DID_ERROR;
80 cmnd->result = ScsiResult(host_status, 0);
Jing Huang7725ccf2009-09-23 17:46:15 -070081 }
82
83 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
84 if (cmnd->device->host != NULL)
85 scsi_dma_unmap(cmnd);
86
87 cmnd->host_scribble = NULL;
88 bfa_trc(bfad, cmnd->result);
89
90 itnim_data = cmnd->device->hostdata;
91 if (itnim_data) {
92 itnim = itnim_data->itnim;
93 if (!cmnd->result && itnim &&
94 (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
95 /* Queue depth adjustment for good status completion */
96 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
97 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
98 /* qfull handling */
99 bfad_os_handle_qfull(itnim, cmnd->device);
100 }
101 }
102
103 cmnd->scsi_done(cmnd);
104}
105
106void
107bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
108{
109 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
110 struct bfad_itnim_data_s *itnim_data;
111 struct bfad_itnim_s *itnim;
112
113 cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
114
115 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
116 if (cmnd->device->host != NULL)
117 scsi_dma_unmap(cmnd);
118
119 cmnd->host_scribble = NULL;
120
121 /* Queue depth adjustment */
122 if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
123 itnim_data = cmnd->device->hostdata;
124 if (itnim_data) {
125 itnim = itnim_data->itnim;
126 if (itnim)
127 bfad_os_ramp_up_qdepth(itnim, cmnd->device);
128 }
129 }
130
131 cmnd->scsi_done(cmnd);
132}
133
134void
135bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
136{
137 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
138 struct bfad_s *bfad = drv;
139
140 cmnd->result = ScsiResult(DID_ERROR, 0);
141
142 /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
143 if (cmnd->device->host != NULL)
144 scsi_dma_unmap(cmnd);
145
146 bfa_trc(bfad, cmnd->result);
147 cmnd->host_scribble = NULL;
148}
149
150void
151bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
152 enum bfi_tskim_status tsk_status)
153{
154 struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
155 wait_queue_head_t *wq;
156
157 cmnd->SCp.Status |= tsk_status << 1;
158 set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
159 wq = (wait_queue_head_t *) cmnd->SCp.ptr;
160 cmnd->SCp.ptr = NULL;
161
162 if (wq)
163 wake_up(wq);
164}
165
Jing Huang5fbe25c2010-10-18 17:17:23 -0700166/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700167 * Scsi_Host_template SCSI host template
168 */
Jing Huang5fbe25c2010-10-18 17:17:23 -0700169/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700170 * Scsi_Host template entry, returns BFAD PCI info.
171 */
172static const char *
173bfad_im_info(struct Scsi_Host *shost)
174{
175 static char bfa_buf[256];
176 struct bfad_im_port_s *im_port =
177 (struct bfad_im_port_s *) shost->hostdata[0];
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700178 struct bfad_s *bfad = im_port->bfad;
179 struct bfa_s *bfa = &bfad->bfa;
180 struct bfa_ioc_s *ioc = &bfa->ioc;
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800181 char model[BFA_ADAPTER_MODEL_NAME_LEN];
Jing Huang7725ccf2009-09-23 17:46:15 -0700182
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700183 bfa_get_adapter_model(bfa, model);
Jing Huang7725ccf2009-09-23 17:46:15 -0700184
185 memset(bfa_buf, 0, sizeof(bfa_buf));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700186 if (ioc->ctdev)
187 snprintf(bfa_buf, sizeof(bfa_buf),
188 "Brocade FCOE Adapter, " "model: %s hwpath: %s driver: %s",
189 model, bfad->pci_name, BFAD_DRIVER_VERSION);
190 else
191 snprintf(bfa_buf, sizeof(bfa_buf),
192 "Brocade FC Adapter, " "model: %s hwpath: %s driver: %s",
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -0800193 model, bfad->pci_name, BFAD_DRIVER_VERSION);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700194
Jing Huang7725ccf2009-09-23 17:46:15 -0700195 return bfa_buf;
196}
197
Jing Huang5fbe25c2010-10-18 17:17:23 -0700198/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700199 * Scsi_Host template entry, aborts the specified SCSI command.
200 *
201 * Returns: SUCCESS or FAILED.
202 */
203static int
204bfad_im_abort_handler(struct scsi_cmnd *cmnd)
205{
206 struct Scsi_Host *shost = cmnd->device->host;
207 struct bfad_im_port_s *im_port =
208 (struct bfad_im_port_s *) shost->hostdata[0];
209 struct bfad_s *bfad = im_port->bfad;
210 struct bfa_ioim_s *hal_io;
211 unsigned long flags;
212 u32 timeout;
213 int rc = FAILED;
214
215 spin_lock_irqsave(&bfad->bfad_lock, flags);
216 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
217 if (!hal_io) {
218 /* IO has been completed, retrun success */
219 rc = SUCCESS;
220 goto out;
221 }
222 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
223 rc = FAILED;
224 goto out;
225 }
226
227 bfa_trc(bfad, hal_io->iotag);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700228 BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700229 im_port->shost->host_no, cmnd, hal_io->iotag);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700230 (void) bfa_ioim_abort(hal_io);
Jing Huang7725ccf2009-09-23 17:46:15 -0700231 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
232
233 /* Need to wait until the command get aborted */
234 timeout = 10;
235 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
236 set_current_state(TASK_UNINTERRUPTIBLE);
237 schedule_timeout(timeout);
238 if (timeout < 4 * HZ)
239 timeout *= 2;
240 }
241
242 cmnd->scsi_done(cmnd);
243 bfa_trc(bfad, hal_io->iotag);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700244 BFA_LOG(KERN_INFO, bfad, log_level,
245 "scsi%d: complete abort 0x%p iotag 0x%x\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700246 im_port->shost->host_no, cmnd, hal_io->iotag);
247 return SUCCESS;
248out:
249 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
250 return rc;
251}
252
253static bfa_status_t
254bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
255 struct bfad_itnim_s *itnim)
256{
257 struct bfa_tskim_s *tskim;
258 struct bfa_itnim_s *bfa_itnim;
259 bfa_status_t rc = BFA_STATUS_OK;
260
Jing Huang7725ccf2009-09-23 17:46:15 -0700261 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
262 if (!tskim) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700263 BFA_LOG(KERN_ERR, bfad, log_level,
264 "target reset, fail to allocate tskim\n");
Jing Huang7725ccf2009-09-23 17:46:15 -0700265 rc = BFA_STATUS_FAILED;
266 goto out;
267 }
268
269 /*
270 * Set host_scribble to NULL to avoid aborting a task command if
271 * happens.
272 */
273 cmnd->host_scribble = NULL;
274 cmnd->SCp.Status = 0;
275 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
276 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0,
277 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
278out:
279 return rc;
280}
281
Jing Huang5fbe25c2010-10-18 17:17:23 -0700282/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700283 * Scsi_Host template entry, resets a LUN and abort its all commands.
284 *
285 * Returns: SUCCESS or FAILED.
286 *
287 */
288static int
289bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
290{
291 struct Scsi_Host *shost = cmnd->device->host;
292 struct bfad_im_port_s *im_port =
293 (struct bfad_im_port_s *) shost->hostdata[0];
294 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
295 struct bfad_s *bfad = im_port->bfad;
296 struct bfa_tskim_s *tskim;
297 struct bfad_itnim_s *itnim;
298 struct bfa_itnim_s *bfa_itnim;
Yong Zhang6d154db2010-08-10 18:01:22 -0700299 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700300 int rc = SUCCESS;
301 unsigned long flags;
302 enum bfi_tskim_status task_status;
303
304 spin_lock_irqsave(&bfad->bfad_lock, flags);
305 itnim = itnim_data->itnim;
306 if (!itnim) {
307 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
308 rc = FAILED;
309 goto out;
310 }
311
312 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
313 if (!tskim) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700314 BFA_LOG(KERN_ERR, bfad, log_level,
Jing Huang7725ccf2009-09-23 17:46:15 -0700315 "LUN reset, fail to allocate tskim");
316 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
317 rc = FAILED;
318 goto out;
319 }
320
Jing Huang5fbe25c2010-10-18 17:17:23 -0700321 /*
Jing Huang7725ccf2009-09-23 17:46:15 -0700322 * Set host_scribble to NULL to avoid aborting a task command
323 * if happens.
324 */
325 cmnd->host_scribble = NULL;
326 cmnd->SCp.ptr = (char *)&wq;
327 cmnd->SCp.Status = 0;
328 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
329 bfa_tskim_start(tskim, bfa_itnim,
330 bfad_int_to_lun(cmnd->device->lun),
331 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
332 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
333
334 wait_event(wq, test_bit(IO_DONE_BIT,
335 (unsigned long *)&cmnd->SCp.Status));
336
337 task_status = cmnd->SCp.Status >> 1;
338 if (task_status != BFI_TSKIM_STS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700339 BFA_LOG(KERN_ERR, bfad, log_level,
340 "LUN reset failure, status: %d\n", task_status);
Jing Huang7725ccf2009-09-23 17:46:15 -0700341 rc = FAILED;
342 }
343
344out:
345 return rc;
346}
347
Jing Huang5fbe25c2010-10-18 17:17:23 -0700348/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700349 * Scsi_Host template entry, resets the bus and abort all commands.
350 */
351static int
352bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
353{
354 struct Scsi_Host *shost = cmnd->device->host;
355 struct bfad_im_port_s *im_port =
356 (struct bfad_im_port_s *) shost->hostdata[0];
357 struct bfad_s *bfad = im_port->bfad;
358 struct bfad_itnim_s *itnim;
359 unsigned long flags;
360 u32 i, rc, err_cnt = 0;
Yong Zhang6d154db2010-08-10 18:01:22 -0700361 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700362 enum bfi_tskim_status task_status;
363
364 spin_lock_irqsave(&bfad->bfad_lock, flags);
365 for (i = 0; i < MAX_FCP_TARGET; i++) {
366 itnim = bfad_os_get_itnim(im_port, i);
367 if (itnim) {
368 cmnd->SCp.ptr = (char *)&wq;
369 rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
370 if (rc != BFA_STATUS_OK) {
371 err_cnt++;
372 continue;
373 }
374
375 /* wait target reset to complete */
376 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
377 wait_event(wq, test_bit(IO_DONE_BIT,
378 (unsigned long *)&cmnd->SCp.Status));
379 spin_lock_irqsave(&bfad->bfad_lock, flags);
380
381 task_status = cmnd->SCp.Status >> 1;
382 if (task_status != BFI_TSKIM_STS_OK) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700383 BFA_LOG(KERN_ERR, bfad, log_level,
Jing Huang7725ccf2009-09-23 17:46:15 -0700384 "target reset failure,"
385 " status: %d\n", task_status);
386 err_cnt++;
387 }
388 }
389 }
390 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
391
392 if (err_cnt)
393 return FAILED;
394
395 return SUCCESS;
396}
397
Jing Huang5fbe25c2010-10-18 17:17:23 -0700398/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700399 * Scsi_Host template entry slave_destroy.
400 */
401static void
402bfad_im_slave_destroy(struct scsi_device *sdev)
403{
404 sdev->hostdata = NULL;
405 return;
406}
407
Jing Huang5fbe25c2010-10-18 17:17:23 -0700408/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700409 * BFA FCS itnim callbacks
410 */
411
Jing Huang5fbe25c2010-10-18 17:17:23 -0700412/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700413 * BFA FCS itnim alloc callback, after successful PRLI
414 * Context: Interrupt
415 */
416void
417bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
418 struct bfad_itnim_s **itnim_drv)
419{
420 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
421 if (*itnim_drv == NULL)
422 return;
423
424 (*itnim_drv)->im = bfad->im;
425 *itnim = &(*itnim_drv)->fcs_itnim;
426 (*itnim_drv)->state = ITNIM_STATE_NONE;
427
428 /*
429 * Initiaze the itnim_work
430 */
431 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
432 bfad->bfad_flags |= BFAD_RPORT_ONLINE;
433}
434
Jing Huang5fbe25c2010-10-18 17:17:23 -0700435/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700436 * BFA FCS itnim free callback.
437 * Context: Interrupt. bfad_lock is held
438 */
439void
440bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
441{
442 struct bfad_port_s *port;
443 wwn_t wwpn;
444 u32 fcid;
445 char wwpn_str[32], fcid_str[16];
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700446 struct bfad_im_s *im = itnim_drv->im;
Jing Huang7725ccf2009-09-23 17:46:15 -0700447
448 /* online to free state transtion should not happen */
449 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE);
450
451 itnim_drv->queue_work = 1;
452 /* offline request is not yet done, use the same request to free */
453 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
454 itnim_drv->queue_work = 0;
455
456 itnim_drv->state = ITNIM_STATE_FREE;
457 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
458 itnim_drv->im_port = port->im_port;
459 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
460 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
461 wwn2str(wwpn_str, wwpn);
462 fcid2str(fcid_str, fcid);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700463 BFA_LOG(KERN_INFO, bfad, log_level,
464 "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700465 port->im_port->shost->host_no,
466 fcid_str, wwpn_str);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700467
468 /* ITNIM processing */
469 if (itnim_drv->queue_work)
470 queue_work(im->drv_workq, &itnim_drv->itnim_work);
Jing Huang7725ccf2009-09-23 17:46:15 -0700471}
472
Jing Huang5fbe25c2010-10-18 17:17:23 -0700473/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700474 * BFA FCS itnim online callback.
475 * Context: Interrupt. bfad_lock is held
476 */
477void
478bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
479{
480 struct bfad_port_s *port;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700481 struct bfad_im_s *im = itnim_drv->im;
Jing Huang7725ccf2009-09-23 17:46:15 -0700482
483 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
484 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
485 itnim_drv->state = ITNIM_STATE_ONLINE;
486 itnim_drv->queue_work = 1;
487 itnim_drv->im_port = port->im_port;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700488
489 /* ITNIM processing */
490 if (itnim_drv->queue_work)
491 queue_work(im->drv_workq, &itnim_drv->itnim_work);
Jing Huang7725ccf2009-09-23 17:46:15 -0700492}
493
Jing Huang5fbe25c2010-10-18 17:17:23 -0700494/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700495 * BFA FCS itnim offline callback.
496 * Context: Interrupt. bfad_lock is held
497 */
498void
499bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
500{
501 struct bfad_port_s *port;
502 struct bfad_s *bfad;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700503 struct bfad_im_s *im = itnim_drv->im;
Jing Huang7725ccf2009-09-23 17:46:15 -0700504
505 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
506 bfad = port->bfad;
507 if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
508 (port->flags & BFAD_PORT_DELETE)) {
509 itnim_drv->state = ITNIM_STATE_OFFLINE;
510 return;
511 }
512 itnim_drv->im_port = port->im_port;
513 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
514 itnim_drv->queue_work = 1;
Jing Huang7725ccf2009-09-23 17:46:15 -0700515
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700516 /* ITNIM processing */
517 if (itnim_drv->queue_work)
518 queue_work(im->drv_workq, &itnim_drv->itnim_work);
Jing Huang7725ccf2009-09-23 17:46:15 -0700519}
520
Jing Huang5fbe25c2010-10-18 17:17:23 -0700521/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700522 * Allocate a Scsi_Host for a port.
523 */
524int
Jing Huangb5042932010-03-19 11:05:39 -0700525bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700526 struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700527{
528 int error = 1;
529
Jing Huang42b426e2010-03-19 11:07:09 -0700530 mutex_lock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700531 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
Jing Huang42b426e2010-03-19 11:07:09 -0700532 mutex_unlock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700533 printk(KERN_WARNING "idr_pre_get failure\n");
534 goto out;
535 }
536
537 error = idr_get_new(&bfad_im_port_index, im_port,
538 &im_port->idr_id);
539 if (error) {
Jing Huang42b426e2010-03-19 11:07:09 -0700540 mutex_unlock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700541 printk(KERN_WARNING "idr_get_new failure\n");
542 goto out;
543 }
544
Jing Huang42b426e2010-03-19 11:07:09 -0700545 mutex_unlock(&bfad_mutex);
546
Jing Huang7725ccf2009-09-23 17:46:15 -0700547 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad);
548 if (!im_port->shost) {
549 error = 1;
550 goto out_free_idr;
551 }
552
553 im_port->shost->hostdata[0] = (unsigned long)im_port;
554 im_port->shost->unique_id = im_port->idr_id;
555 im_port->shost->this_id = -1;
556 im_port->shost->max_id = MAX_FCP_TARGET;
557 im_port->shost->max_lun = MAX_FCP_LUN;
558 im_port->shost->max_cmd_len = 16;
559 im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
Jing Huangb5042932010-03-19 11:05:39 -0700560 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
561 im_port->shost->transportt = bfad_im_scsi_transport_template;
562 else
563 im_port->shost->transportt =
564 bfad_im_scsi_vport_transport_template;
Jing Huang7725ccf2009-09-23 17:46:15 -0700565
Jing Huangc54d5572010-07-08 20:01:49 -0700566 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700567 if (error) {
Jing Huangb5042932010-03-19 11:05:39 -0700568 printk(KERN_WARNING "scsi_add_host failure %d\n", error);
Jing Huang7725ccf2009-09-23 17:46:15 -0700569 goto out_fc_rel;
570 }
571
572 /* setup host fixed attribute if the lk supports */
573 bfad_os_fc_host_init(im_port);
574
575 return 0;
576
577out_fc_rel:
578 scsi_host_put(im_port->shost);
Jing Huangd9883542010-07-08 19:46:26 -0700579 im_port->shost = NULL;
Jing Huang7725ccf2009-09-23 17:46:15 -0700580out_free_idr:
Jing Huang42b426e2010-03-19 11:07:09 -0700581 mutex_lock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700582 idr_remove(&bfad_im_port_index, im_port->idr_id);
Jing Huang42b426e2010-03-19 11:07:09 -0700583 mutex_unlock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700584out:
585 return error;
586}
587
588void
589bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
590{
Jing Huang7725ccf2009-09-23 17:46:15 -0700591 bfa_trc(bfad, bfad->inst_no);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700592 BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n",
Jing Huang7725ccf2009-09-23 17:46:15 -0700593 im_port->shost->host_no);
594
595 fc_remove_host(im_port->shost);
596
597 scsi_remove_host(im_port->shost);
598 scsi_host_put(im_port->shost);
599
Jing Huang42b426e2010-03-19 11:07:09 -0700600 mutex_lock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700601 idr_remove(&bfad_im_port_index, im_port->idr_id);
Jing Huang42b426e2010-03-19 11:07:09 -0700602 mutex_unlock(&bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -0700603}
604
605static void
606bfad_im_port_delete_handler(struct work_struct *work)
607{
608 struct bfad_im_port_s *im_port =
609 container_of(work, struct bfad_im_port_s, port_delete_work);
610
Jing Huangb5042932010-03-19 11:05:39 -0700611 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
612 im_port->flags |= BFAD_PORT_DELETE;
613 fc_vport_terminate(im_port->fc_vport);
614 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700615}
616
617bfa_status_t
618bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
619{
620 int rc = BFA_STATUS_OK;
621 struct bfad_im_port_s *im_port;
622
623 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
624 if (im_port == NULL) {
625 rc = BFA_STATUS_ENOMEM;
626 goto ext;
627 }
628 port->im_port = im_port;
629 im_port->port = port;
630 im_port->bfad = bfad;
631
632 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
633 INIT_LIST_HEAD(&im_port->itnim_mapped_list);
634 INIT_LIST_HEAD(&im_port->binding_list);
635
636ext:
637 return rc;
638}
639
640void
641bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
642{
643 struct bfad_im_port_s *im_port = port->im_port;
644
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700645 queue_work(bfad->im->drv_workq,
Jing Huang7725ccf2009-09-23 17:46:15 -0700646 &im_port->port_delete_work);
647}
648
649void
650bfad_im_port_clean(struct bfad_im_port_s *im_port)
651{
652 struct bfad_fcp_binding *bp, *bp_new;
653 unsigned long flags;
654 struct bfad_s *bfad = im_port->bfad;
655
656 spin_lock_irqsave(&bfad->bfad_lock, flags);
657 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
658 list_entry) {
659 list_del(&bp->list_entry);
660 kfree(bp);
661 }
662
663 /* the itnim_mapped_list must be empty at this time */
664 bfa_assert(list_empty(&im_port->itnim_mapped_list));
665
666 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
667}
668
Jing Huang7725ccf2009-09-23 17:46:15 -0700669bfa_status_t
670bfad_im_probe(struct bfad_s *bfad)
671{
672 struct bfad_im_s *im;
673 bfa_status_t rc = BFA_STATUS_OK;
674
675 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
676 if (im == NULL) {
677 rc = BFA_STATUS_ENOMEM;
678 goto ext;
679 }
680
681 bfad->im = im;
682 im->bfad = bfad;
683
684 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) {
685 kfree(im);
686 rc = BFA_STATUS_FAILED;
687 }
688
689ext:
690 return rc;
691}
692
693void
694bfad_im_probe_undo(struct bfad_s *bfad)
695{
696 if (bfad->im) {
697 bfad_os_destroy_workq(bfad->im);
698 kfree(bfad->im);
699 bfad->im = NULL;
700 }
701}
702
Jing Huang7725ccf2009-09-23 17:46:15 -0700703struct Scsi_Host *
704bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
705{
706 struct scsi_host_template *sht;
707
708 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
709 sht = &bfad_im_scsi_host_template;
710 else
711 sht = &bfad_im_vport_template;
712
713 sht->sg_tablesize = bfad->cfg_data.io_max_sge;
714
715 return scsi_host_alloc(sht, sizeof(unsigned long));
716}
717
718void
719bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
720{
Jing Huangb5042932010-03-19 11:05:39 -0700721 if (!(im_port->flags & BFAD_PORT_DELETE))
722 flush_workqueue(bfad->im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700723 bfad_im_scsi_host_free(im_port->bfad, im_port);
724 bfad_im_port_clean(im_port);
725 kfree(im_port);
726}
727
728void
729bfad_os_destroy_workq(struct bfad_im_s *im)
730{
731 if (im && im->drv_workq) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700732 flush_workqueue(im->drv_workq);
Jing Huang7725ccf2009-09-23 17:46:15 -0700733 destroy_workqueue(im->drv_workq);
734 im->drv_workq = NULL;
735 }
736}
737
738bfa_status_t
739bfad_os_thread_workq(struct bfad_s *bfad)
740{
741 struct bfad_im_s *im = bfad->im;
742
743 bfa_trc(bfad, 0);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700744 snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
Jing Huang7725ccf2009-09-23 17:46:15 -0700745 bfad->inst_no);
746 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
747 if (!im->drv_workq)
748 return BFA_STATUS_FAILED;
749
750 return BFA_STATUS_OK;
751}
752
Jing Huang5fbe25c2010-10-18 17:17:23 -0700753/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700754 * Scsi_Host template entry.
755 *
756 * Description:
757 * OS entry point to adjust the queue_depths on a per-device basis.
758 * Called once per device during the bus scan.
759 * Return non-zero if fails.
760 */
761static int
762bfad_im_slave_configure(struct scsi_device *sdev)
763{
764 if (sdev->tagged_supported)
765 scsi_activate_tcq(sdev, bfa_lun_queue_depth);
766 else
767 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
768
769 return 0;
770}
771
772struct scsi_host_template bfad_im_scsi_host_template = {
773 .module = THIS_MODULE,
774 .name = BFAD_DRIVER_NAME,
775 .info = bfad_im_info,
776 .queuecommand = bfad_im_queuecommand,
777 .eh_abort_handler = bfad_im_abort_handler,
778 .eh_device_reset_handler = bfad_im_reset_lun_handler,
779 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
780
781 .slave_alloc = bfad_im_slave_alloc,
782 .slave_configure = bfad_im_slave_configure,
783 .slave_destroy = bfad_im_slave_destroy,
784
785 .this_id = -1,
786 .sg_tablesize = BFAD_IO_MAX_SGE,
787 .cmd_per_lun = 3,
788 .use_clustering = ENABLE_CLUSTERING,
789 .shost_attrs = bfad_im_host_attrs,
790 .max_sectors = 0xFFFF,
791};
792
793struct scsi_host_template bfad_im_vport_template = {
794 .module = THIS_MODULE,
795 .name = BFAD_DRIVER_NAME,
796 .info = bfad_im_info,
797 .queuecommand = bfad_im_queuecommand,
798 .eh_abort_handler = bfad_im_abort_handler,
799 .eh_device_reset_handler = bfad_im_reset_lun_handler,
800 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
801
802 .slave_alloc = bfad_im_slave_alloc,
803 .slave_configure = bfad_im_slave_configure,
804 .slave_destroy = bfad_im_slave_destroy,
805
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_vport_attrs,
811 .max_sectors = 0xFFFF,
812};
813
Jing Huang7725ccf2009-09-23 17:46:15 -0700814bfa_status_t
815bfad_im_module_init(void)
816{
817 bfad_im_scsi_transport_template =
818 fc_attach_transport(&bfad_im_fc_function_template);
819 if (!bfad_im_scsi_transport_template)
820 return BFA_STATUS_ENOMEM;
821
Jing Huangb5042932010-03-19 11:05:39 -0700822 bfad_im_scsi_vport_transport_template =
823 fc_attach_transport(&bfad_im_vport_fc_function_template);
824 if (!bfad_im_scsi_vport_transport_template) {
825 fc_release_transport(bfad_im_scsi_transport_template);
826 return BFA_STATUS_ENOMEM;
827 }
828
Jing Huang7725ccf2009-09-23 17:46:15 -0700829 return BFA_STATUS_OK;
830}
831
832void
833bfad_im_module_exit(void)
834{
835 if (bfad_im_scsi_transport_template)
836 fc_release_transport(bfad_im_scsi_transport_template);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700837
Jing Huangb5042932010-03-19 11:05:39 -0700838 if (bfad_im_scsi_vport_transport_template)
839 fc_release_transport(bfad_im_scsi_vport_transport_template);
Jing Huang7725ccf2009-09-23 17:46:15 -0700840}
841
842void
Jing Huang7725ccf2009-09-23 17:46:15 -0700843bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
844{
845 struct scsi_device *tmp_sdev;
846
847 if (((jiffies - itnim->last_ramp_up_time) >
848 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
849 ((jiffies - itnim->last_queue_full_time) >
850 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
851 shost_for_each_device(tmp_sdev, sdev->host) {
852 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
853 if (tmp_sdev->id != sdev->id)
854 continue;
855 if (tmp_sdev->ordered_tags)
856 scsi_adjust_queue_depth(tmp_sdev,
857 MSG_ORDERED_TAG,
858 tmp_sdev->queue_depth + 1);
859 else
860 scsi_adjust_queue_depth(tmp_sdev,
861 MSG_SIMPLE_TAG,
862 tmp_sdev->queue_depth + 1);
863
864 itnim->last_ramp_up_time = jiffies;
865 }
866 }
867 }
868}
869
870void
871bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
872{
873 struct scsi_device *tmp_sdev;
874
875 itnim->last_queue_full_time = jiffies;
876
877 shost_for_each_device(tmp_sdev, sdev->host) {
878 if (tmp_sdev->id != sdev->id)
879 continue;
880 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
881 }
882}
883
Jing Huang7725ccf2009-09-23 17:46:15 -0700884struct bfad_itnim_s *
885bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id)
886{
887 struct bfad_itnim_s *itnim = NULL;
888
889 /* Search the mapped list for this target ID */
890 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
891 if (id == itnim->scsi_tgt_id)
892 return itnim;
893 }
894
895 return NULL;
896}
897
Jing Huang5fbe25c2010-10-18 17:17:23 -0700898/*
Jing Huang7725ccf2009-09-23 17:46:15 -0700899 * Scsi_Host template entry slave_alloc
900 */
901static int
902bfad_im_slave_alloc(struct scsi_device *sdev)
903{
904 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
905
906 if (!rport || fc_remote_port_chkready(rport))
907 return -ENXIO;
908
909 sdev->hostdata = rport->dd_data;
910
911 return 0;
912}
913
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700914static u32
915bfad_im_supported_speeds(struct bfa_s *bfa)
916{
Jing Huange0a08a32010-10-18 17:14:01 -0700917 struct bfa_ioc_attr_s *ioc_attr;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700918 u32 supported_speed = 0;
919
Jing Huange0a08a32010-10-18 17:14:01 -0700920 ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
921 if (!ioc_attr)
922 return 0;
923
924 bfa_get_attr(bfa, ioc_attr);
925 if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
926 if (ioc_attr->adapter_attr.is_mezz) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700927 supported_speed |= FC_PORTSPEED_8GBIT |
928 FC_PORTSPEED_4GBIT |
929 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
930 } else {
931 supported_speed |= FC_PORTSPEED_8GBIT |
932 FC_PORTSPEED_4GBIT |
933 FC_PORTSPEED_2GBIT;
934 }
Jing Huange0a08a32010-10-18 17:14:01 -0700935 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700936 supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
937 FC_PORTSPEED_1GBIT;
Jing Huange0a08a32010-10-18 17:14:01 -0700938 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700939 supported_speed |= FC_PORTSPEED_10GBIT;
940 }
Jing Huange0a08a32010-10-18 17:14:01 -0700941 kfree(ioc_attr);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700942 return supported_speed;
943}
944
Jing Huang7725ccf2009-09-23 17:46:15 -0700945void
946bfad_os_fc_host_init(struct bfad_im_port_s *im_port)
947{
948 struct Scsi_Host *host = im_port->shost;
949 struct bfad_s *bfad = im_port->bfad;
950 struct bfad_port_s *port = im_port->port;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700951 char symname[BFA_SYMNAME_MAXLEN];
Jing Huange0a08a32010-10-18 17:14:01 -0700952 struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700953
954 fc_host_node_name(host) =
Jing Huangba816ea2010-10-18 17:10:50 -0700955 cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
Jing Huang7725ccf2009-09-23 17:46:15 -0700956 fc_host_port_name(host) =
Jing Huangba816ea2010-10-18 17:10:50 -0700957 cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
Jing Huangb5042932010-03-19 11:05:39 -0700958 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
Jing Huang7725ccf2009-09-23 17:46:15 -0700959
960 fc_host_supported_classes(host) = FC_COS_CLASS3;
961
962 memset(fc_host_supported_fc4s(host), 0,
963 sizeof(fc_host_supported_fc4s(host)));
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700964 if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
Jing Huang7725ccf2009-09-23 17:46:15 -0700965 /* For FCP type 0x08 */
966 fc_host_supported_fc4s(host)[2] = 1;
Jing Huang7725ccf2009-09-23 17:46:15 -0700967 /* For fibre channel services type 0x20 */
968 fc_host_supported_fc4s(host)[7] = 1;
969
Jing Huange0a08a32010-10-18 17:14:01 -0700970 strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700971 BFA_SYMNAME_MAXLEN);
972 sprintf(fc_host_symbolic_name(host), "%s", symname);
Jing Huang7725ccf2009-09-23 17:46:15 -0700973
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700974 fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
Jing Huang5fbe25c2010-10-18 17:17:23 -0700975 fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
Jing Huang7725ccf2009-09-23 17:46:15 -0700976}
977
978static void
979bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
980{
981 struct fc_rport_identifiers rport_ids;
982 struct fc_rport *fc_rport;
983 struct bfad_itnim_data_s *itnim_data;
984
985 rport_ids.node_name =
Jing Huangba816ea2010-10-18 17:10:50 -0700986 cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
Jing Huang7725ccf2009-09-23 17:46:15 -0700987 rport_ids.port_name =
Jing Huangba816ea2010-10-18 17:10:50 -0700988 cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
Jing Huang7725ccf2009-09-23 17:46:15 -0700989 rport_ids.port_id =
990 bfa_os_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
991 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
992
993 itnim->fc_rport = fc_rport =
994 fc_remote_port_add(im_port->shost, 0, &rport_ids);
995
996 if (!fc_rport)
997 return;
998
999 fc_rport->maxframe_size =
1000 bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1001 fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1002
1003 itnim_data = fc_rport->dd_data;
1004 itnim_data->itnim = itnim;
1005
1006 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1007
1008 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1009 fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1010
1011 if ((fc_rport->scsi_target_id != -1)
1012 && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1013 itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1014
1015 return;
1016}
1017
Jing Huang5fbe25c2010-10-18 17:17:23 -07001018/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001019 * Work queue handler using FC transport service
1020* Context: kernel
1021 */
1022static void
1023bfad_im_itnim_work_handler(struct work_struct *work)
1024{
1025 struct bfad_itnim_s *itnim = container_of(work, struct bfad_itnim_s,
1026 itnim_work);
1027 struct bfad_im_s *im = itnim->im;
1028 struct bfad_s *bfad = im->bfad;
1029 struct bfad_im_port_s *im_port;
1030 unsigned long flags;
1031 struct fc_rport *fc_rport;
1032 wwn_t wwpn;
1033 u32 fcid;
1034 char wwpn_str[32], fcid_str[16];
1035
1036 spin_lock_irqsave(&bfad->bfad_lock, flags);
1037 im_port = itnim->im_port;
1038 bfa_trc(bfad, itnim->state);
1039 switch (itnim->state) {
1040 case ITNIM_STATE_ONLINE:
1041 if (!itnim->fc_rport) {
1042 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1043 bfad_im_fc_rport_add(im_port, itnim);
1044 spin_lock_irqsave(&bfad->bfad_lock, flags);
1045 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1046 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1047 wwn2str(wwpn_str, wwpn);
1048 fcid2str(fcid_str, fcid);
1049 list_add_tail(&itnim->list_entry,
1050 &im_port->itnim_mapped_list);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001051 BFA_LOG(KERN_INFO, bfad, log_level,
1052 "ITNIM ONLINE Target: %d:0:%d "
1053 "FCID: %s WWPN: %s\n",
Jing Huang7725ccf2009-09-23 17:46:15 -07001054 im_port->shost->host_no,
1055 itnim->scsi_tgt_id,
1056 fcid_str, wwpn_str);
1057 } else {
1058 printk(KERN_WARNING
1059 "%s: itnim %llx is already in online state\n",
Jing Huangf8ceafd2009-09-25 12:29:54 -07001060 __func__,
Jing Huang7725ccf2009-09-23 17:46:15 -07001061 bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1062 }
1063
1064 break;
1065 case ITNIM_STATE_OFFLINE_PENDING:
1066 itnim->state = ITNIM_STATE_OFFLINE;
1067 if (itnim->fc_rport) {
1068 fc_rport = itnim->fc_rport;
1069 ((struct bfad_itnim_data_s *)
1070 fc_rport->dd_data)->itnim = NULL;
1071 itnim->fc_rport = NULL;
1072 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1073 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1074 fc_rport->dev_loss_tmo =
1075 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1076 fc_remote_port_delete(fc_rport);
1077 spin_lock_irqsave(&bfad->bfad_lock, flags);
1078 }
1079 wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1080 fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1081 wwn2str(wwpn_str, wwpn);
1082 fcid2str(fcid_str, fcid);
1083 list_del(&itnim->list_entry);
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001084 BFA_LOG(KERN_INFO, bfad, log_level,
1085 "ITNIM OFFLINE Target: %d:0:%d "
1086 "FCID: %s WWPN: %s\n",
Jing Huang7725ccf2009-09-23 17:46:15 -07001087 im_port->shost->host_no,
1088 itnim->scsi_tgt_id,
1089 fcid_str, wwpn_str);
1090 }
1091 break;
1092 case ITNIM_STATE_FREE:
1093 if (itnim->fc_rport) {
1094 fc_rport = itnim->fc_rport;
1095 ((struct bfad_itnim_data_s *)
1096 fc_rport->dd_data)->itnim = NULL;
1097 itnim->fc_rport = NULL;
1098 if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1099 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1100 fc_rport->dev_loss_tmo =
1101 bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1102 fc_remote_port_delete(fc_rport);
1103 spin_lock_irqsave(&bfad->bfad_lock, flags);
1104 }
1105 list_del(&itnim->list_entry);
1106 }
1107
1108 kfree(itnim);
1109 break;
1110 default:
1111 bfa_assert(0);
1112 break;
1113 }
1114
1115 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1116}
1117
Jing Huang5fbe25c2010-10-18 17:17:23 -07001118/*
Jing Huang7725ccf2009-09-23 17:46:15 -07001119 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1120 */
1121static int
Jeff Garzikf2812332010-11-16 02:10:29 -05001122bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
Jing Huang7725ccf2009-09-23 17:46:15 -07001123{
1124 struct bfad_im_port_s *im_port =
1125 (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1126 struct bfad_s *bfad = im_port->bfad;
1127 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1128 struct bfad_itnim_s *itnim;
1129 struct bfa_ioim_s *hal_io;
1130 unsigned long flags;
1131 int rc;
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001132 int sg_cnt = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001133 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1134
1135 rc = fc_remote_port_chkready(rport);
1136 if (rc) {
1137 cmnd->result = rc;
1138 done(cmnd);
1139 return 0;
1140 }
1141
1142 sg_cnt = scsi_dma_map(cmnd);
Jing Huang7725ccf2009-09-23 17:46:15 -07001143 if (sg_cnt < 0)
1144 return SCSI_MLQUEUE_HOST_BUSY;
1145
1146 cmnd->scsi_done = done;
1147
1148 spin_lock_irqsave(&bfad->bfad_lock, flags);
1149 if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1150 printk(KERN_WARNING
1151 "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1152 bfad->inst_no, cmnd, cmnd->cmnd[0]);
1153 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1154 goto out_fail_cmd;
1155 }
1156
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001157
Jing Huang7725ccf2009-09-23 17:46:15 -07001158 itnim = itnim_data->itnim;
1159 if (!itnim) {
1160 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1161 goto out_fail_cmd;
1162 }
1163
1164 hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1165 itnim->bfa_itnim, sg_cnt);
1166 if (!hal_io) {
1167 printk(KERN_WARNING "hal_io failure\n");
1168 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1169 scsi_dma_unmap(cmnd);
1170 return SCSI_MLQUEUE_HOST_BUSY;
1171 }
1172
1173 cmnd->host_scribble = (char *)hal_io;
1174 bfa_trc_fp(bfad, hal_io->iotag);
1175 bfa_ioim_start(hal_io);
1176 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1177
1178 return 0;
1179
1180out_fail_cmd:
1181 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1182 scsi_dma_unmap(cmnd);
1183 if (done)
1184 done(cmnd);
1185
1186 return 0;
1187}
1188
Jeff Garzikf2812332010-11-16 02:10:29 -05001189static DEF_SCSI_QCMD(bfad_im_queuecommand)
1190
Jing Huang7725ccf2009-09-23 17:46:15 -07001191void
1192bfad_os_rport_online_wait(struct bfad_s *bfad)
1193{
1194 int i;
1195 int rport_delay = 10;
1196
1197 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001198 && i < bfa_linkup_delay; i++) {
1199 set_current_state(TASK_UNINTERRUPTIBLE);
1200 schedule_timeout(HZ);
1201 }
Jing Huang7725ccf2009-09-23 17:46:15 -07001202
1203 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1204 rport_delay = rport_delay < bfa_linkup_delay ?
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001205 rport_delay : bfa_linkup_delay;
Jing Huang7725ccf2009-09-23 17:46:15 -07001206 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001207 && i < rport_delay; i++) {
1208 set_current_state(TASK_UNINTERRUPTIBLE);
1209 schedule_timeout(HZ);
1210 }
Jing Huang7725ccf2009-09-23 17:46:15 -07001211
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001212 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1213 set_current_state(TASK_UNINTERRUPTIBLE);
1214 schedule_timeout(rport_delay * HZ);
1215 }
Jing Huang7725ccf2009-09-23 17:46:15 -07001216 }
1217}
1218
1219int
1220bfad_os_get_linkup_delay(struct bfad_s *bfad)
1221{
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001222 u8 nwwns = 0;
1223 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
1224 int linkup_delay;
Jing Huang7725ccf2009-09-23 17:46:15 -07001225
1226 /*
1227 * Querying for the boot target port wwns
1228 * -- read from boot information in flash.
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001229 * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1230 * else => local boot machine set linkup_delay = 0
Jing Huang7725ccf2009-09-23 17:46:15 -07001231 */
1232
Jing Huang15b64a82010-07-08 19:48:12 -07001233 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
Jing Huang7725ccf2009-09-23 17:46:15 -07001234
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001235 if (nwwns > 0)
1236 /* If Boot over SAN set linkup_delay = 30sec */
1237 linkup_delay = 30;
1238 else
1239 /* If local boot; no linkup_delay */
1240 linkup_delay = 0;
Jing Huang7725ccf2009-09-23 17:46:15 -07001241
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07001242 return linkup_delay;
Jing Huang7725ccf2009-09-23 17:46:15 -07001243}