Merge commit 'v2.6.37-rc3' into perf/core
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / bfa / bfad_im.c
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 "bfad_drv.h"
23 #include "bfad_im.h"
24 #include "bfa_cb_ioim.h"
25 #include "bfa_fcs.h"
26
27 BFA_TRC_FILE(LDRV, IM);
28
29 DEFINE_IDR(bfad_im_port_index);
30 struct scsi_transport_template *bfad_im_scsi_transport_template;
31 struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
32 static void bfad_im_itnim_work_handler(struct work_struct *work);
33 static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
34 static int bfad_im_slave_alloc(struct scsi_device *sdev);
35 static void bfad_im_fc_rport_add(struct bfad_im_port_s *im_port,
36 struct bfad_itnim_s *itnim);
37
38 void
39 bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
40 enum bfi_ioim_status io_status, u8 scsi_status,
41 int sns_len, u8 *sns_info, s32 residue)
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;
47 u8 host_status = DID_OK;
48
49 switch (io_status) {
50 case BFI_IOIM_STS_OK:
51 bfa_trc(bfad, scsi_status);
52 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 }
60
61 if (residue > 0) {
62 bfa_trc(bfad, residue);
63 scsi_set_resid(cmnd, residue);
64 if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
65 (scsi_bufflen(cmnd) - residue) <
66 cmnd->underflow) {
67 bfa_trc(bfad, 0);
68 host_status = DID_ERROR;
69 }
70 }
71 cmnd->result = ScsiResult(host_status, scsi_status);
72
73 break;
74
75 case BFI_IOIM_STS_ABORTED:
76 case BFI_IOIM_STS_TIMEDOUT:
77 case BFI_IOIM_STS_PATHTOV:
78 default:
79 host_status = DID_ERROR;
80 cmnd->result = ScsiResult(host_status, 0);
81 }
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
106 void
107 bfa_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
134 void
135 bfa_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
150 void
151 bfa_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
166 /*
167 * Scsi_Host_template SCSI host template
168 */
169 /*
170 * Scsi_Host template entry, returns BFAD PCI info.
171 */
172 static const char *
173 bfad_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];
178 struct bfad_s *bfad = im_port->bfad;
179 struct bfa_s *bfa = &bfad->bfa;
180 struct bfa_ioc_s *ioc = &bfa->ioc;
181 char model[BFA_ADAPTER_MODEL_NAME_LEN];
182
183 bfa_get_adapter_model(bfa, model);
184
185 memset(bfa_buf, 0, sizeof(bfa_buf));
186 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",
193 model, bfad->pci_name, BFAD_DRIVER_VERSION);
194
195 return bfa_buf;
196 }
197
198 /*
199 * Scsi_Host template entry, aborts the specified SCSI command.
200 *
201 * Returns: SUCCESS or FAILED.
202 */
203 static int
204 bfad_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);
228 BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n",
229 im_port->shost->host_no, cmnd, hal_io->iotag);
230 (void) bfa_ioim_abort(hal_io);
231 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
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);
244 BFA_LOG(KERN_INFO, bfad, log_level,
245 "scsi%d: complete abort 0x%p iotag 0x%x\n",
246 im_port->shost->host_no, cmnd, hal_io->iotag);
247 return SUCCESS;
248 out:
249 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
250 return rc;
251 }
252
253 static bfa_status_t
254 bfad_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
261 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
262 if (!tskim) {
263 BFA_LOG(KERN_ERR, bfad, log_level,
264 "target reset, fail to allocate tskim\n");
265 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);
278 out:
279 return rc;
280 }
281
282 /*
283 * Scsi_Host template entry, resets a LUN and abort its all commands.
284 *
285 * Returns: SUCCESS or FAILED.
286 *
287 */
288 static int
289 bfad_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;
299 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
300 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) {
314 BFA_LOG(KERN_ERR, bfad, log_level,
315 "LUN reset, fail to allocate tskim");
316 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
317 rc = FAILED;
318 goto out;
319 }
320
321 /*
322 * 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) {
339 BFA_LOG(KERN_ERR, bfad, log_level,
340 "LUN reset failure, status: %d\n", task_status);
341 rc = FAILED;
342 }
343
344 out:
345 return rc;
346 }
347
348 /*
349 * Scsi_Host template entry, resets the bus and abort all commands.
350 */
351 static int
352 bfad_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;
361 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
362 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) {
383 BFA_LOG(KERN_ERR, bfad, log_level,
384 "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
398 /*
399 * Scsi_Host template entry slave_destroy.
400 */
401 static void
402 bfad_im_slave_destroy(struct scsi_device *sdev)
403 {
404 sdev->hostdata = NULL;
405 return;
406 }
407
408 /*
409 * BFA FCS itnim callbacks
410 */
411
412 /*
413 * BFA FCS itnim alloc callback, after successful PRLI
414 * Context: Interrupt
415 */
416 void
417 bfa_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
435 /*
436 * BFA FCS itnim free callback.
437 * Context: Interrupt. bfad_lock is held
438 */
439 void
440 bfa_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];
446 struct bfad_im_s *im = itnim_drv->im;
447
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);
463 BFA_LOG(KERN_INFO, bfad, log_level,
464 "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
465 port->im_port->shost->host_no,
466 fcid_str, wwpn_str);
467
468 /* ITNIM processing */
469 if (itnim_drv->queue_work)
470 queue_work(im->drv_workq, &itnim_drv->itnim_work);
471 }
472
473 /*
474 * BFA FCS itnim online callback.
475 * Context: Interrupt. bfad_lock is held
476 */
477 void
478 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
479 {
480 struct bfad_port_s *port;
481 struct bfad_im_s *im = itnim_drv->im;
482
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;
488
489 /* ITNIM processing */
490 if (itnim_drv->queue_work)
491 queue_work(im->drv_workq, &itnim_drv->itnim_work);
492 }
493
494 /*
495 * BFA FCS itnim offline callback.
496 * Context: Interrupt. bfad_lock is held
497 */
498 void
499 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
500 {
501 struct bfad_port_s *port;
502 struct bfad_s *bfad;
503 struct bfad_im_s *im = itnim_drv->im;
504
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;
515
516 /* ITNIM processing */
517 if (itnim_drv->queue_work)
518 queue_work(im->drv_workq, &itnim_drv->itnim_work);
519 }
520
521 /*
522 * Allocate a Scsi_Host for a port.
523 */
524 int
525 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
526 struct device *dev)
527 {
528 int error = 1;
529
530 mutex_lock(&bfad_mutex);
531 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) {
532 mutex_unlock(&bfad_mutex);
533 printk(KERN_WARNING "idr_pre_get failure\n");
534 goto out;
535 }
536
537 error = idr_get_new(&bfad_im_port_index, im_port,
538 &im_port->idr_id);
539 if (error) {
540 mutex_unlock(&bfad_mutex);
541 printk(KERN_WARNING "idr_get_new failure\n");
542 goto out;
543 }
544
545 mutex_unlock(&bfad_mutex);
546
547 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;
560 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;
565
566 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
567 if (error) {
568 printk(KERN_WARNING "scsi_add_host failure %d\n", error);
569 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
577 out_fc_rel:
578 scsi_host_put(im_port->shost);
579 im_port->shost = NULL;
580 out_free_idr:
581 mutex_lock(&bfad_mutex);
582 idr_remove(&bfad_im_port_index, im_port->idr_id);
583 mutex_unlock(&bfad_mutex);
584 out:
585 return error;
586 }
587
588 void
589 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
590 {
591 bfa_trc(bfad, bfad->inst_no);
592 BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n",
593 im_port->shost->host_no);
594
595 fc_remove_host(im_port->shost);
596
597 scsi_remove_host(im_port->shost);
598 scsi_host_put(im_port->shost);
599
600 mutex_lock(&bfad_mutex);
601 idr_remove(&bfad_im_port_index, im_port->idr_id);
602 mutex_unlock(&bfad_mutex);
603 }
604
605 static void
606 bfad_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
611 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
612 im_port->flags |= BFAD_PORT_DELETE;
613 fc_vport_terminate(im_port->fc_vport);
614 }
615 }
616
617 bfa_status_t
618 bfad_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
636 ext:
637 return rc;
638 }
639
640 void
641 bfad_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
645 queue_work(bfad->im->drv_workq,
646 &im_port->port_delete_work);
647 }
648
649 void
650 bfad_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
669 bfa_status_t
670 bfad_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
689 ext:
690 return rc;
691 }
692
693 void
694 bfad_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
703 struct Scsi_Host *
704 bfad_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
718 void
719 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
720 {
721 if (!(im_port->flags & BFAD_PORT_DELETE))
722 flush_workqueue(bfad->im->drv_workq);
723 bfad_im_scsi_host_free(im_port->bfad, im_port);
724 bfad_im_port_clean(im_port);
725 kfree(im_port);
726 }
727
728 void
729 bfad_os_destroy_workq(struct bfad_im_s *im)
730 {
731 if (im && im->drv_workq) {
732 flush_workqueue(im->drv_workq);
733 destroy_workqueue(im->drv_workq);
734 im->drv_workq = NULL;
735 }
736 }
737
738 bfa_status_t
739 bfad_os_thread_workq(struct bfad_s *bfad)
740 {
741 struct bfad_im_s *im = bfad->im;
742
743 bfa_trc(bfad, 0);
744 snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
745 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
753 /*
754 * 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 */
761 static int
762 bfad_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
772 struct scsi_host_template bfad_im_scsi_host_template = {
773 .module = THIS_MODULE,
774 .name = BFAD_DRIVER_NAME,
775 .info = bfad_im_info,
776 .queuecommand = bfad_im_queuecommand,
777 .eh_abort_handler = bfad_im_abort_handler,
778 .eh_device_reset_handler = bfad_im_reset_lun_handler,
779 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
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
793 struct scsi_host_template bfad_im_vport_template = {
794 .module = THIS_MODULE,
795 .name = BFAD_DRIVER_NAME,
796 .info = bfad_im_info,
797 .queuecommand = bfad_im_queuecommand,
798 .eh_abort_handler = bfad_im_abort_handler,
799 .eh_device_reset_handler = bfad_im_reset_lun_handler,
800 .eh_bus_reset_handler = bfad_im_reset_bus_handler,
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
814 bfa_status_t
815 bfad_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
822 bfad_im_scsi_vport_transport_template =
823 fc_attach_transport(&bfad_im_vport_fc_function_template);
824 if (!bfad_im_scsi_vport_transport_template) {
825 fc_release_transport(bfad_im_scsi_transport_template);
826 return BFA_STATUS_ENOMEM;
827 }
828
829 return BFA_STATUS_OK;
830 }
831
832 void
833 bfad_im_module_exit(void)
834 {
835 if (bfad_im_scsi_transport_template)
836 fc_release_transport(bfad_im_scsi_transport_template);
837
838 if (bfad_im_scsi_vport_transport_template)
839 fc_release_transport(bfad_im_scsi_vport_transport_template);
840 }
841
842 void
843 bfad_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
870 void
871 bfad_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
884 struct bfad_itnim_s *
885 bfad_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
898 /*
899 * Scsi_Host template entry slave_alloc
900 */
901 static int
902 bfad_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
914 static u32
915 bfad_im_supported_speeds(struct bfa_s *bfa)
916 {
917 struct bfa_ioc_attr_s *ioc_attr;
918 u32 supported_speed = 0;
919
920 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) {
927 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 }
935 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
936 supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
937 FC_PORTSPEED_1GBIT;
938 } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
939 supported_speed |= FC_PORTSPEED_10GBIT;
940 }
941 kfree(ioc_attr);
942 return supported_speed;
943 }
944
945 void
946 bfad_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;
951 char symname[BFA_SYMNAME_MAXLEN];
952 struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
953
954 fc_host_node_name(host) =
955 cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
956 fc_host_port_name(host) =
957 cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
958 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
959
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)));
964 if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
965 /* For FCP type 0x08 */
966 fc_host_supported_fc4s(host)[2] = 1;
967 /* For fibre channel services type 0x20 */
968 fc_host_supported_fc4s(host)[7] = 1;
969
970 strncpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
971 BFA_SYMNAME_MAXLEN);
972 sprintf(fc_host_symbolic_name(host), "%s", symname);
973
974 fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
975 fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
976 }
977
978 static void
979 bfad_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 =
986 cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
987 rport_ids.port_name =
988 cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
989 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
1018 /*
1019 * Work queue handler using FC transport service
1020 * Context: kernel
1021 */
1022 static void
1023 bfad_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);
1051 BFA_LOG(KERN_INFO, bfad, log_level,
1052 "ITNIM ONLINE Target: %d:0:%d "
1053 "FCID: %s WWPN: %s\n",
1054 im_port->shost->host_no,
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",
1060 __func__,
1061 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);
1084 BFA_LOG(KERN_INFO, bfad, log_level,
1085 "ITNIM OFFLINE Target: %d:0:%d "
1086 "FCID: %s WWPN: %s\n",
1087 im_port->shost->host_no,
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
1118 /*
1119 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1120 */
1121 static int
1122 bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1123 {
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;
1132 int sg_cnt = 0;
1133 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);
1143 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
1157
1158 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
1180 out_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
1189 static DEF_SCSI_QCMD(bfad_im_queuecommand)
1190
1191 void
1192 bfad_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)
1198 && i < bfa_linkup_delay; i++) {
1199 set_current_state(TASK_UNINTERRUPTIBLE);
1200 schedule_timeout(HZ);
1201 }
1202
1203 if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1204 rport_delay = rport_delay < bfa_linkup_delay ?
1205 rport_delay : bfa_linkup_delay;
1206 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1207 && i < rport_delay; i++) {
1208 set_current_state(TASK_UNINTERRUPTIBLE);
1209 schedule_timeout(HZ);
1210 }
1211
1212 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1213 set_current_state(TASK_UNINTERRUPTIBLE);
1214 schedule_timeout(rport_delay * HZ);
1215 }
1216 }
1217 }
1218
1219 int
1220 bfad_os_get_linkup_delay(struct bfad_s *bfad)
1221 {
1222 u8 nwwns = 0;
1223 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX];
1224 int linkup_delay;
1225
1226 /*
1227 * Querying for the boot target port wwns
1228 * -- read from boot information in flash.
1229 * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1230 * else => local boot machine set linkup_delay = 0
1231 */
1232
1233 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1234
1235 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;
1241
1242 return linkup_delay;
1243 }