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_cmnd *cmnd, 34 void (*done)(struct scsi_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_os_ramp_up_qdepth(itnim, cmnd->device); 98 } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) { 99 /* qfull handling */ 100 bfad_os_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_os_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 struct bfa_s *bfa = &bfad->bfa; 181 struct bfa_ioc_s *ioc = &bfa->ioc; 182 char model[BFA_ADAPTER_MODEL_NAME_LEN]; 183 184 bfa_get_adapter_model(bfa, model); 185 186 memset(bfa_buf, 0, sizeof(bfa_buf)); 187 if (ioc->ctdev) 188 snprintf(bfa_buf, sizeof(bfa_buf), 189 "Brocade FCOE Adapter, " "model: %s hwpath: %s driver: %s", 190 model, bfad->pci_name, BFAD_DRIVER_VERSION); 191 else 192 snprintf(bfa_buf, sizeof(bfa_buf), 193 "Brocade FC Adapter, " "model: %s hwpath: %s driver: %s", 194 model, bfad->pci_name, BFAD_DRIVER_VERSION); 195 196 return bfa_buf; 197 } 198 199 /** 200 * Scsi_Host template entry, aborts the specified SCSI command. 201 * 202 * Returns: SUCCESS or FAILED. 203 */ 204 static int 205 bfad_im_abort_handler(struct scsi_cmnd *cmnd) 206 { 207 struct Scsi_Host *shost = cmnd->device->host; 208 struct bfad_im_port_s *im_port = 209 (struct bfad_im_port_s *) shost->hostdata[0]; 210 struct bfad_s *bfad = im_port->bfad; 211 struct bfa_ioim_s *hal_io; 212 unsigned long flags; 213 u32 timeout; 214 int rc = FAILED; 215 216 spin_lock_irqsave(&bfad->bfad_lock, flags); 217 hal_io = (struct bfa_ioim_s *) cmnd->host_scribble; 218 if (!hal_io) { 219 /* IO has been completed, retrun success */ 220 rc = SUCCESS; 221 goto out; 222 } 223 if (hal_io->dio != (struct bfad_ioim_s *) cmnd) { 224 rc = FAILED; 225 goto out; 226 } 227 228 bfa_trc(bfad, hal_io->iotag); 229 BFA_LOG(KERN_INFO, bfad, log_level, "scsi%d: abort cmnd %p iotag %x\n", 230 im_port->shost->host_no, cmnd, hal_io->iotag); 231 (void) bfa_ioim_abort(hal_io); 232 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 233 234 /* Need to wait until the command get aborted */ 235 timeout = 10; 236 while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) { 237 set_current_state(TASK_UNINTERRUPTIBLE); 238 schedule_timeout(timeout); 239 if (timeout < 4 * HZ) 240 timeout *= 2; 241 } 242 243 cmnd->scsi_done(cmnd); 244 bfa_trc(bfad, hal_io->iotag); 245 BFA_LOG(KERN_INFO, bfad, log_level, 246 "scsi%d: complete abort 0x%p iotag 0x%x\n", 247 im_port->shost->host_no, cmnd, hal_io->iotag); 248 return SUCCESS; 249 out: 250 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 251 return rc; 252 } 253 254 static bfa_status_t 255 bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd, 256 struct bfad_itnim_s *itnim) 257 { 258 struct bfa_tskim_s *tskim; 259 struct bfa_itnim_s *bfa_itnim; 260 bfa_status_t rc = BFA_STATUS_OK; 261 262 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd); 263 if (!tskim) { 264 BFA_LOG(KERN_ERR, bfad, log_level, 265 "target reset, fail to allocate tskim\n"); 266 rc = BFA_STATUS_FAILED; 267 goto out; 268 } 269 270 /* 271 * Set host_scribble to NULL to avoid aborting a task command if 272 * happens. 273 */ 274 cmnd->host_scribble = NULL; 275 cmnd->SCp.Status = 0; 276 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim); 277 bfa_tskim_start(tskim, bfa_itnim, (lun_t)0, 278 FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO); 279 out: 280 return rc; 281 } 282 283 /** 284 * Scsi_Host template entry, resets a LUN and abort its all commands. 285 * 286 * Returns: SUCCESS or FAILED. 287 * 288 */ 289 static int 290 bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd) 291 { 292 struct Scsi_Host *shost = cmnd->device->host; 293 struct bfad_im_port_s *im_port = 294 (struct bfad_im_port_s *) shost->hostdata[0]; 295 struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata; 296 struct bfad_s *bfad = im_port->bfad; 297 struct bfa_tskim_s *tskim; 298 struct bfad_itnim_s *itnim; 299 struct bfa_itnim_s *bfa_itnim; 300 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 301 int rc = SUCCESS; 302 unsigned long flags; 303 enum bfi_tskim_status task_status; 304 305 spin_lock_irqsave(&bfad->bfad_lock, flags); 306 itnim = itnim_data->itnim; 307 if (!itnim) { 308 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 309 rc = FAILED; 310 goto out; 311 } 312 313 tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd); 314 if (!tskim) { 315 BFA_LOG(KERN_ERR, bfad, log_level, 316 "LUN reset, fail to allocate tskim"); 317 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 318 rc = FAILED; 319 goto out; 320 } 321 322 /** 323 * Set host_scribble to NULL to avoid aborting a task command 324 * if happens. 325 */ 326 cmnd->host_scribble = NULL; 327 cmnd->SCp.ptr = (char *)&wq; 328 cmnd->SCp.Status = 0; 329 bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim); 330 bfa_tskim_start(tskim, bfa_itnim, 331 bfad_int_to_lun(cmnd->device->lun), 332 FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO); 333 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 334 335 wait_event(wq, test_bit(IO_DONE_BIT, 336 (unsigned long *)&cmnd->SCp.Status)); 337 338 task_status = cmnd->SCp.Status >> 1; 339 if (task_status != BFI_TSKIM_STS_OK) { 340 BFA_LOG(KERN_ERR, bfad, log_level, 341 "LUN reset failure, status: %d\n", task_status); 342 rc = FAILED; 343 } 344 345 out: 346 return rc; 347 } 348 349 /** 350 * Scsi_Host template entry, resets the bus and abort all commands. 351 */ 352 static int 353 bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd) 354 { 355 struct Scsi_Host *shost = cmnd->device->host; 356 struct bfad_im_port_s *im_port = 357 (struct bfad_im_port_s *) shost->hostdata[0]; 358 struct bfad_s *bfad = im_port->bfad; 359 struct bfad_itnim_s *itnim; 360 unsigned long flags; 361 u32 i, rc, err_cnt = 0; 362 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 363 enum bfi_tskim_status task_status; 364 365 spin_lock_irqsave(&bfad->bfad_lock, flags); 366 for (i = 0; i < MAX_FCP_TARGET; i++) { 367 itnim = bfad_os_get_itnim(im_port, i); 368 if (itnim) { 369 cmnd->SCp.ptr = (char *)&wq; 370 rc = bfad_im_target_reset_send(bfad, cmnd, itnim); 371 if (rc != BFA_STATUS_OK) { 372 err_cnt++; 373 continue; 374 } 375 376 /* wait target reset to complete */ 377 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 378 wait_event(wq, test_bit(IO_DONE_BIT, 379 (unsigned long *)&cmnd->SCp.Status)); 380 spin_lock_irqsave(&bfad->bfad_lock, flags); 381 382 task_status = cmnd->SCp.Status >> 1; 383 if (task_status != BFI_TSKIM_STS_OK) { 384 BFA_LOG(KERN_ERR, bfad, log_level, 385 "target reset failure," 386 " status: %d\n", task_status); 387 err_cnt++; 388 } 389 } 390 } 391 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 392 393 if (err_cnt) 394 return FAILED; 395 396 return SUCCESS; 397 } 398 399 /** 400 * Scsi_Host template entry slave_destroy. 401 */ 402 static void 403 bfad_im_slave_destroy(struct scsi_device *sdev) 404 { 405 sdev->hostdata = NULL; 406 return; 407 } 408 409 /** 410 * BFA FCS itnim callbacks 411 */ 412 413 /** 414 * BFA FCS itnim alloc callback, after successful PRLI 415 * Context: Interrupt 416 */ 417 void 418 bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim, 419 struct bfad_itnim_s **itnim_drv) 420 { 421 *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC); 422 if (*itnim_drv == NULL) 423 return; 424 425 (*itnim_drv)->im = bfad->im; 426 *itnim = &(*itnim_drv)->fcs_itnim; 427 (*itnim_drv)->state = ITNIM_STATE_NONE; 428 429 /* 430 * Initiaze the itnim_work 431 */ 432 INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler); 433 bfad->bfad_flags |= BFAD_RPORT_ONLINE; 434 } 435 436 /** 437 * BFA FCS itnim free callback. 438 * Context: Interrupt. bfad_lock is held 439 */ 440 void 441 bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv) 442 { 443 struct bfad_port_s *port; 444 wwn_t wwpn; 445 u32 fcid; 446 char wwpn_str[32], fcid_str[16]; 447 struct bfad_im_s *im = itnim_drv->im; 448 449 /* online to free state transtion should not happen */ 450 bfa_assert(itnim_drv->state != ITNIM_STATE_ONLINE); 451 452 itnim_drv->queue_work = 1; 453 /* offline request is not yet done, use the same request to free */ 454 if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING) 455 itnim_drv->queue_work = 0; 456 457 itnim_drv->state = ITNIM_STATE_FREE; 458 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim); 459 itnim_drv->im_port = port->im_port; 460 wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim); 461 fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim); 462 wwn2str(wwpn_str, wwpn); 463 fcid2str(fcid_str, fcid); 464 BFA_LOG(KERN_INFO, bfad, log_level, 465 "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n", 466 port->im_port->shost->host_no, 467 fcid_str, wwpn_str); 468 469 /* ITNIM processing */ 470 if (itnim_drv->queue_work) 471 queue_work(im->drv_workq, &itnim_drv->itnim_work); 472 } 473 474 /** 475 * BFA FCS itnim online callback. 476 * Context: Interrupt. bfad_lock is held 477 */ 478 void 479 bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv) 480 { 481 struct bfad_port_s *port; 482 struct bfad_im_s *im = itnim_drv->im; 483 484 itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim); 485 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim); 486 itnim_drv->state = ITNIM_STATE_ONLINE; 487 itnim_drv->queue_work = 1; 488 itnim_drv->im_port = port->im_port; 489 490 /* ITNIM processing */ 491 if (itnim_drv->queue_work) 492 queue_work(im->drv_workq, &itnim_drv->itnim_work); 493 } 494 495 /** 496 * BFA FCS itnim offline callback. 497 * Context: Interrupt. bfad_lock is held 498 */ 499 void 500 bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv) 501 { 502 struct bfad_port_s *port; 503 struct bfad_s *bfad; 504 struct bfad_im_s *im = itnim_drv->im; 505 506 port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim); 507 bfad = port->bfad; 508 if ((bfad->pport.flags & BFAD_PORT_DELETE) || 509 (port->flags & BFAD_PORT_DELETE)) { 510 itnim_drv->state = ITNIM_STATE_OFFLINE; 511 return; 512 } 513 itnim_drv->im_port = port->im_port; 514 itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING; 515 itnim_drv->queue_work = 1; 516 517 /* ITNIM processing */ 518 if (itnim_drv->queue_work) 519 queue_work(im->drv_workq, &itnim_drv->itnim_work); 520 } 521 522 /** 523 * Allocate a Scsi_Host for a port. 524 */ 525 int 526 bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port, 527 struct device *dev) 528 { 529 int error = 1; 530 531 mutex_lock(&bfad_mutex); 532 if (!idr_pre_get(&bfad_im_port_index, GFP_KERNEL)) { 533 mutex_unlock(&bfad_mutex); 534 printk(KERN_WARNING "idr_pre_get failure\n"); 535 goto out; 536 } 537 538 error = idr_get_new(&bfad_im_port_index, im_port, 539 &im_port->idr_id); 540 if (error) { 541 mutex_unlock(&bfad_mutex); 542 printk(KERN_WARNING "idr_get_new failure\n"); 543 goto out; 544 } 545 546 mutex_unlock(&bfad_mutex); 547 548 im_port->shost = bfad_os_scsi_host_alloc(im_port, bfad); 549 if (!im_port->shost) { 550 error = 1; 551 goto out_free_idr; 552 } 553 554 im_port->shost->hostdata[0] = (unsigned long)im_port; 555 im_port->shost->unique_id = im_port->idr_id; 556 im_port->shost->this_id = -1; 557 im_port->shost->max_id = MAX_FCP_TARGET; 558 im_port->shost->max_lun = MAX_FCP_LUN; 559 im_port->shost->max_cmd_len = 16; 560 im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth; 561 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE) 562 im_port->shost->transportt = bfad_im_scsi_transport_template; 563 else 564 im_port->shost->transportt = 565 bfad_im_scsi_vport_transport_template; 566 567 error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev); 568 if (error) { 569 printk(KERN_WARNING "scsi_add_host failure %d\n", error); 570 goto out_fc_rel; 571 } 572 573 /* setup host fixed attribute if the lk supports */ 574 bfad_os_fc_host_init(im_port); 575 576 return 0; 577 578 out_fc_rel: 579 scsi_host_put(im_port->shost); 580 im_port->shost = NULL; 581 out_free_idr: 582 mutex_lock(&bfad_mutex); 583 idr_remove(&bfad_im_port_index, im_port->idr_id); 584 mutex_unlock(&bfad_mutex); 585 out: 586 return error; 587 } 588 589 void 590 bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port) 591 { 592 bfa_trc(bfad, bfad->inst_no); 593 BFA_LOG(KERN_INFO, bfad, log_level, "Free scsi%d\n", 594 im_port->shost->host_no); 595 596 fc_remove_host(im_port->shost); 597 598 scsi_remove_host(im_port->shost); 599 scsi_host_put(im_port->shost); 600 601 mutex_lock(&bfad_mutex); 602 idr_remove(&bfad_im_port_index, im_port->idr_id); 603 mutex_unlock(&bfad_mutex); 604 } 605 606 static void 607 bfad_im_port_delete_handler(struct work_struct *work) 608 { 609 struct bfad_im_port_s *im_port = 610 container_of(work, struct bfad_im_port_s, port_delete_work); 611 612 if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) { 613 im_port->flags |= BFAD_PORT_DELETE; 614 fc_vport_terminate(im_port->fc_vport); 615 } 616 } 617 618 bfa_status_t 619 bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port) 620 { 621 int rc = BFA_STATUS_OK; 622 struct bfad_im_port_s *im_port; 623 624 im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC); 625 if (im_port == NULL) { 626 rc = BFA_STATUS_ENOMEM; 627 goto ext; 628 } 629 port->im_port = im_port; 630 im_port->port = port; 631 im_port->bfad = bfad; 632 633 INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler); 634 INIT_LIST_HEAD(&im_port->itnim_mapped_list); 635 INIT_LIST_HEAD(&im_port->binding_list); 636 637 ext: 638 return rc; 639 } 640 641 void 642 bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port) 643 { 644 struct bfad_im_port_s *im_port = port->im_port; 645 646 queue_work(bfad->im->drv_workq, 647 &im_port->port_delete_work); 648 } 649 650 void 651 bfad_im_port_clean(struct bfad_im_port_s *im_port) 652 { 653 struct bfad_fcp_binding *bp, *bp_new; 654 unsigned long flags; 655 struct bfad_s *bfad = im_port->bfad; 656 657 spin_lock_irqsave(&bfad->bfad_lock, flags); 658 list_for_each_entry_safe(bp, bp_new, &im_port->binding_list, 659 list_entry) { 660 list_del(&bp->list_entry); 661 kfree(bp); 662 } 663 664 /* the itnim_mapped_list must be empty at this time */ 665 bfa_assert(list_empty(&im_port->itnim_mapped_list)); 666 667 spin_unlock_irqrestore(&bfad->bfad_lock, flags); 668 } 669 670 bfa_status_t 671 bfad_im_probe(struct bfad_s *bfad) 672 { 673 struct bfad_im_s *im; 674 bfa_status_t rc = BFA_STATUS_OK; 675 676 im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL); 677 if (im == NULL) { 678 rc = BFA_STATUS_ENOMEM; 679 goto ext; 680 } 681 682 bfad->im = im; 683 im->bfad = bfad; 684 685 if (bfad_os_thread_workq(bfad) != BFA_STATUS_OK) { 686 kfree(im); 687 rc = BFA_STATUS_FAILED; 688 } 689 690 ext: 691 return rc; 692 } 693 694 void 695 bfad_im_probe_undo(struct bfad_s *bfad) 696 { 697 if (bfad->im) { 698 bfad_os_destroy_workq(bfad->im); 699 kfree(bfad->im); 700 bfad->im = NULL; 701 } 702 } 703 704 struct Scsi_Host * 705 bfad_os_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad) 706 { 707 struct scsi_host_template *sht; 708 709 if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE) 710 sht = &bfad_im_scsi_host_template; 711 else 712 sht = &bfad_im_vport_template; 713 714 sht->sg_tablesize = bfad->cfg_data.io_max_sge; 715 716 return scsi_host_alloc(sht, sizeof(unsigned long)); 717 } 718 719 void 720 bfad_os_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port) 721 { 722 if (!(im_port->flags & BFAD_PORT_DELETE)) 723 flush_workqueue(bfad->im->drv_workq); 724 bfad_im_scsi_host_free(im_port->bfad, im_port); 725 bfad_im_port_clean(im_port); 726 kfree(im_port); 727 } 728 729 void 730 bfad_os_destroy_workq(struct bfad_im_s *im) 731 { 732 if (im && im->drv_workq) { 733 flush_workqueue(im->drv_workq); 734 destroy_workqueue(im->drv_workq); 735 im->drv_workq = NULL; 736 } 737 } 738 739 bfa_status_t 740 bfad_os_thread_workq(struct bfad_s *bfad) 741 { 742 struct bfad_im_s *im = bfad->im; 743 744 bfa_trc(bfad, 0); 745 snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d", 746 bfad->inst_no); 747 im->drv_workq = create_singlethread_workqueue(im->drv_workq_name); 748 if (!im->drv_workq) 749 return BFA_STATUS_FAILED; 750 751 return BFA_STATUS_OK; 752 } 753 754 /** 755 * Scsi_Host template entry. 756 * 757 * Description: 758 * OS entry point to adjust the queue_depths on a per-device basis. 759 * Called once per device during the bus scan. 760 * Return non-zero if fails. 761 */ 762 static int 763 bfad_im_slave_configure(struct scsi_device *sdev) 764 { 765 if (sdev->tagged_supported) 766 scsi_activate_tcq(sdev, bfa_lun_queue_depth); 767 else 768 scsi_deactivate_tcq(sdev, bfa_lun_queue_depth); 769 770 return 0; 771 } 772 773 struct scsi_host_template bfad_im_scsi_host_template = { 774 .module = THIS_MODULE, 775 .name = BFAD_DRIVER_NAME, 776 .info = bfad_im_info, 777 .queuecommand = bfad_im_queuecommand, 778 .eh_abort_handler = bfad_im_abort_handler, 779 .eh_device_reset_handler = bfad_im_reset_lun_handler, 780 .eh_bus_reset_handler = bfad_im_reset_bus_handler, 781 782 .slave_alloc = bfad_im_slave_alloc, 783 .slave_configure = bfad_im_slave_configure, 784 .slave_destroy = bfad_im_slave_destroy, 785 786 .this_id = -1, 787 .sg_tablesize = BFAD_IO_MAX_SGE, 788 .cmd_per_lun = 3, 789 .use_clustering = ENABLE_CLUSTERING, 790 .shost_attrs = bfad_im_host_attrs, 791 .max_sectors = 0xFFFF, 792 }; 793 794 struct scsi_host_template bfad_im_vport_template = { 795 .module = THIS_MODULE, 796 .name = BFAD_DRIVER_NAME, 797 .info = bfad_im_info, 798 .queuecommand = bfad_im_queuecommand, 799 .eh_abort_handler = bfad_im_abort_handler, 800 .eh_device_reset_handler = bfad_im_reset_lun_handler, 801 .eh_bus_reset_handler = bfad_im_reset_bus_handler, 802 803 .slave_alloc = bfad_im_slave_alloc, 804 .slave_configure = bfad_im_slave_configure, 805 .slave_destroy = bfad_im_slave_destroy, 806 807 .this_id = -1, 808 .sg_tablesize = BFAD_IO_MAX_SGE, 809 .cmd_per_lun = 3, 810 .use_clustering = ENABLE_CLUSTERING, 811 .shost_attrs = bfad_im_vport_attrs, 812 .max_sectors = 0xFFFF, 813 }; 814 815 bfa_status_t 816 bfad_im_module_init(void) 817 { 818 bfad_im_scsi_transport_template = 819 fc_attach_transport(&bfad_im_fc_function_template); 820 if (!bfad_im_scsi_transport_template) 821 return BFA_STATUS_ENOMEM; 822 823 bfad_im_scsi_vport_transport_template = 824 fc_attach_transport(&bfad_im_vport_fc_function_template); 825 if (!bfad_im_scsi_vport_transport_template) { 826 fc_release_transport(bfad_im_scsi_transport_template); 827 return BFA_STATUS_ENOMEM; 828 } 829 830 return BFA_STATUS_OK; 831 } 832 833 void 834 bfad_im_module_exit(void) 835 { 836 if (bfad_im_scsi_transport_template) 837 fc_release_transport(bfad_im_scsi_transport_template); 838 839 if (bfad_im_scsi_vport_transport_template) 840 fc_release_transport(bfad_im_scsi_vport_transport_template); 841 } 842 843 void 844 bfad_os_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev) 845 { 846 struct scsi_device *tmp_sdev; 847 848 if (((jiffies - itnim->last_ramp_up_time) > 849 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) && 850 ((jiffies - itnim->last_queue_full_time) > 851 BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) { 852 shost_for_each_device(tmp_sdev, sdev->host) { 853 if (bfa_lun_queue_depth > tmp_sdev->queue_depth) { 854 if (tmp_sdev->id != sdev->id) 855 continue; 856 if (tmp_sdev->ordered_tags) 857 scsi_adjust_queue_depth(tmp_sdev, 858 MSG_ORDERED_TAG, 859 tmp_sdev->queue_depth + 1); 860 else 861 scsi_adjust_queue_depth(tmp_sdev, 862 MSG_SIMPLE_TAG, 863 tmp_sdev->queue_depth + 1); 864 865 itnim->last_ramp_up_time = jiffies; 866 } 867 } 868 } 869 } 870 871 void 872 bfad_os_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev) 873 { 874 struct scsi_device *tmp_sdev; 875 876 itnim->last_queue_full_time = jiffies; 877 878 shost_for_each_device(tmp_sdev, sdev->host) { 879 if (tmp_sdev->id != sdev->id) 880 continue; 881 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1); 882 } 883 } 884 885 struct bfad_itnim_s * 886 bfad_os_get_itnim(struct bfad_im_port_s *im_port, int id) 887 { 888 struct bfad_itnim_s *itnim = NULL; 889 890 /* Search the mapped list for this target ID */ 891 list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) { 892 if (id == itnim->scsi_tgt_id) 893 return itnim; 894 } 895 896 return NULL; 897 } 898 899 /** 900 * Scsi_Host template entry slave_alloc 901 */ 902 static int 903 bfad_im_slave_alloc(struct scsi_device *sdev) 904 { 905 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 906 907 if (!rport || fc_remote_port_chkready(rport)) 908 return -ENXIO; 909 910 sdev->hostdata = rport->dd_data; 911 912 return 0; 913 } 914 915 static u32 916 bfad_im_supported_speeds(struct bfa_s *bfa) 917 { 918 struct bfa_ioc_attr_s ioc_attr; 919 u32 supported_speed = 0; 920 921 bfa_get_attr(bfa, &ioc_attr); 922 if (ioc_attr.adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) { 923 if (ioc_attr.adapter_attr.is_mezz) { 924 supported_speed |= FC_PORTSPEED_8GBIT | 925 FC_PORTSPEED_4GBIT | 926 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT; 927 } else { 928 supported_speed |= FC_PORTSPEED_8GBIT | 929 FC_PORTSPEED_4GBIT | 930 FC_PORTSPEED_2GBIT; 931 } 932 } else if (ioc_attr.adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) { 933 supported_speed |= FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT | 934 FC_PORTSPEED_1GBIT; 935 } else if (ioc_attr.adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) { 936 supported_speed |= FC_PORTSPEED_10GBIT; 937 } 938 return supported_speed; 939 } 940 941 void 942 bfad_os_fc_host_init(struct bfad_im_port_s *im_port) 943 { 944 struct Scsi_Host *host = im_port->shost; 945 struct bfad_s *bfad = im_port->bfad; 946 struct bfad_port_s *port = im_port->port; 947 struct bfa_port_attr_s pattr; 948 struct bfa_lport_attr_s port_attr; 949 char symname[BFA_SYMNAME_MAXLEN]; 950 951 fc_host_node_name(host) = 952 bfa_os_htonll((bfa_fcs_lport_get_nwwn(port->fcs_port))); 953 fc_host_port_name(host) = 954 bfa_os_htonll((bfa_fcs_lport_get_pwwn(port->fcs_port))); 955 fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa); 956 957 fc_host_supported_classes(host) = FC_COS_CLASS3; 958 959 memset(fc_host_supported_fc4s(host), 0, 960 sizeof(fc_host_supported_fc4s(host))); 961 if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM) 962 /* For FCP type 0x08 */ 963 fc_host_supported_fc4s(host)[2] = 1; 964 /* For fibre channel services type 0x20 */ 965 fc_host_supported_fc4s(host)[7] = 1; 966 967 bfa_fcs_lport_get_attr(&bfad->bfa_fcs.fabric.bport, &port_attr); 968 strncpy(symname, port_attr.port_cfg.sym_name.symname, 969 BFA_SYMNAME_MAXLEN); 970 sprintf(fc_host_symbolic_name(host), "%s", symname); 971 972 fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa); 973 974 bfa_fcport_get_attr(&bfad->bfa, &pattr); 975 fc_host_maxframe_size(host) = pattr.pport_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 bfa_os_htonll(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim)); 987 rport_ids.port_name = 988 bfa_os_htonll(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(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 void 1190 bfad_os_rport_online_wait(struct bfad_s *bfad) 1191 { 1192 int i; 1193 int rport_delay = 10; 1194 1195 for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE) 1196 && i < bfa_linkup_delay; i++) { 1197 set_current_state(TASK_UNINTERRUPTIBLE); 1198 schedule_timeout(HZ); 1199 } 1200 1201 if (bfad->bfad_flags & BFAD_PORT_ONLINE) { 1202 rport_delay = rport_delay < bfa_linkup_delay ? 1203 rport_delay : bfa_linkup_delay; 1204 for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE) 1205 && i < rport_delay; i++) { 1206 set_current_state(TASK_UNINTERRUPTIBLE); 1207 schedule_timeout(HZ); 1208 } 1209 1210 if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) { 1211 set_current_state(TASK_UNINTERRUPTIBLE); 1212 schedule_timeout(rport_delay * HZ); 1213 } 1214 } 1215 } 1216 1217 int 1218 bfad_os_get_linkup_delay(struct bfad_s *bfad) 1219 { 1220 u8 nwwns = 0; 1221 wwn_t wwns[BFA_PREBOOT_BOOTLUN_MAX]; 1222 int linkup_delay; 1223 1224 /* 1225 * Querying for the boot target port wwns 1226 * -- read from boot information in flash. 1227 * If nwwns > 0 => boot over SAN and set linkup_delay = 30 1228 * else => local boot machine set linkup_delay = 0 1229 */ 1230 1231 bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns); 1232 1233 if (nwwns > 0) 1234 /* If Boot over SAN set linkup_delay = 30sec */ 1235 linkup_delay = 30; 1236 else 1237 /* If local boot; no linkup_delay */ 1238 linkup_delay = 0; 1239 1240 return linkup_delay; 1241 } 1242