1 /* 2 * QLogic Fibre Channel HBA Driver 3 * Copyright (c) 2003-2014 QLogic Corporation 4 * 5 * See LICENSE.qla2xxx for copyright and licensing details. 6 */ 7 #include "qla_def.h" 8 #include "qla_target.h" 9 10 #include <linux/blkdev.h> 11 #include <linux/delay.h> 12 13 #include <scsi/scsi_tcq.h> 14 15 /** 16 * qla2x00_get_cmd_direction() - Determine control_flag data direction. 17 * @sp: SCSI command 18 * 19 * Returns the proper CF_* direction based on CDB. 20 */ 21 static inline uint16_t 22 qla2x00_get_cmd_direction(srb_t *sp) 23 { 24 uint16_t cflags; 25 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 26 struct scsi_qla_host *vha = sp->vha; 27 28 cflags = 0; 29 30 /* Set transfer direction */ 31 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 32 cflags = CF_WRITE; 33 vha->qla_stats.output_bytes += scsi_bufflen(cmd); 34 vha->qla_stats.output_requests++; 35 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 36 cflags = CF_READ; 37 vha->qla_stats.input_bytes += scsi_bufflen(cmd); 38 vha->qla_stats.input_requests++; 39 } 40 return (cflags); 41 } 42 43 /** 44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and 45 * Continuation Type 0 IOCBs to allocate. 46 * 47 * @dsds: number of data segment decriptors needed 48 * 49 * Returns the number of IOCB entries needed to store @dsds. 50 */ 51 uint16_t 52 qla2x00_calc_iocbs_32(uint16_t dsds) 53 { 54 uint16_t iocbs; 55 56 iocbs = 1; 57 if (dsds > 3) { 58 iocbs += (dsds - 3) / 7; 59 if ((dsds - 3) % 7) 60 iocbs++; 61 } 62 return (iocbs); 63 } 64 65 /** 66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and 67 * Continuation Type 1 IOCBs to allocate. 68 * 69 * @dsds: number of data segment decriptors needed 70 * 71 * Returns the number of IOCB entries needed to store @dsds. 72 */ 73 uint16_t 74 qla2x00_calc_iocbs_64(uint16_t dsds) 75 { 76 uint16_t iocbs; 77 78 iocbs = 1; 79 if (dsds > 2) { 80 iocbs += (dsds - 2) / 5; 81 if ((dsds - 2) % 5) 82 iocbs++; 83 } 84 return (iocbs); 85 } 86 87 /** 88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB. 89 * @vha: HA context 90 * 91 * Returns a pointer to the Continuation Type 0 IOCB packet. 92 */ 93 static inline cont_entry_t * 94 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha) 95 { 96 cont_entry_t *cont_pkt; 97 struct req_que *req = vha->req; 98 /* Adjust ring index. */ 99 req->ring_index++; 100 if (req->ring_index == req->length) { 101 req->ring_index = 0; 102 req->ring_ptr = req->ring; 103 } else { 104 req->ring_ptr++; 105 } 106 107 cont_pkt = (cont_entry_t *)req->ring_ptr; 108 109 /* Load packet defaults. */ 110 *((uint32_t *)(&cont_pkt->entry_type)) = cpu_to_le32(CONTINUE_TYPE); 111 112 return (cont_pkt); 113 } 114 115 /** 116 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB. 117 * @vha: HA context 118 * @req: request queue 119 * 120 * Returns a pointer to the continuation type 1 IOCB packet. 121 */ 122 static inline cont_a64_entry_t * 123 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req) 124 { 125 cont_a64_entry_t *cont_pkt; 126 127 /* Adjust ring index. */ 128 req->ring_index++; 129 if (req->ring_index == req->length) { 130 req->ring_index = 0; 131 req->ring_ptr = req->ring; 132 } else { 133 req->ring_ptr++; 134 } 135 136 cont_pkt = (cont_a64_entry_t *)req->ring_ptr; 137 138 /* Load packet defaults. */ 139 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ? 140 cpu_to_le32(CONTINUE_A64_TYPE_FX00) : 141 cpu_to_le32(CONTINUE_A64_TYPE); 142 143 return (cont_pkt); 144 } 145 146 inline int 147 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts) 148 { 149 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 150 uint8_t guard = scsi_host_get_guard(cmd->device->host); 151 152 /* We always use DIFF Bundling for best performance */ 153 *fw_prot_opts = 0; 154 155 /* Translate SCSI opcode to a protection opcode */ 156 switch (scsi_get_prot_op(cmd)) { 157 case SCSI_PROT_READ_STRIP: 158 *fw_prot_opts |= PO_MODE_DIF_REMOVE; 159 break; 160 case SCSI_PROT_WRITE_INSERT: 161 *fw_prot_opts |= PO_MODE_DIF_INSERT; 162 break; 163 case SCSI_PROT_READ_INSERT: 164 *fw_prot_opts |= PO_MODE_DIF_INSERT; 165 break; 166 case SCSI_PROT_WRITE_STRIP: 167 *fw_prot_opts |= PO_MODE_DIF_REMOVE; 168 break; 169 case SCSI_PROT_READ_PASS: 170 case SCSI_PROT_WRITE_PASS: 171 if (guard & SHOST_DIX_GUARD_IP) 172 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM; 173 else 174 *fw_prot_opts |= PO_MODE_DIF_PASS; 175 break; 176 default: /* Normal Request */ 177 *fw_prot_opts |= PO_MODE_DIF_PASS; 178 break; 179 } 180 181 return scsi_prot_sg_count(cmd); 182 } 183 184 /* 185 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit 186 * capable IOCB types. 187 * 188 * @sp: SRB command to process 189 * @cmd_pkt: Command type 2 IOCB 190 * @tot_dsds: Total number of segments to transfer 191 */ 192 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt, 193 uint16_t tot_dsds) 194 { 195 uint16_t avail_dsds; 196 uint32_t *cur_dsd; 197 scsi_qla_host_t *vha; 198 struct scsi_cmnd *cmd; 199 struct scatterlist *sg; 200 int i; 201 202 cmd = GET_CMD_SP(sp); 203 204 /* Update entry type to indicate Command Type 2 IOCB */ 205 *((uint32_t *)(&cmd_pkt->entry_type)) = 206 cpu_to_le32(COMMAND_TYPE); 207 208 /* No data transfer */ 209 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { 210 cmd_pkt->byte_count = cpu_to_le32(0); 211 return; 212 } 213 214 vha = sp->vha; 215 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp)); 216 217 /* Three DSDs are available in the Command Type 2 IOCB */ 218 avail_dsds = 3; 219 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 220 221 /* Load data segments */ 222 scsi_for_each_sg(cmd, sg, tot_dsds, i) { 223 cont_entry_t *cont_pkt; 224 225 /* Allocate additional continuation packets? */ 226 if (avail_dsds == 0) { 227 /* 228 * Seven DSDs are available in the Continuation 229 * Type 0 IOCB. 230 */ 231 cont_pkt = qla2x00_prep_cont_type0_iocb(vha); 232 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address; 233 avail_dsds = 7; 234 } 235 236 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg)); 237 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 238 avail_dsds--; 239 } 240 } 241 242 /** 243 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit 244 * capable IOCB types. 245 * 246 * @sp: SRB command to process 247 * @cmd_pkt: Command type 3 IOCB 248 * @tot_dsds: Total number of segments to transfer 249 */ 250 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt, 251 uint16_t tot_dsds) 252 { 253 uint16_t avail_dsds; 254 uint32_t *cur_dsd; 255 scsi_qla_host_t *vha; 256 struct scsi_cmnd *cmd; 257 struct scatterlist *sg; 258 int i; 259 260 cmd = GET_CMD_SP(sp); 261 262 /* Update entry type to indicate Command Type 3 IOCB */ 263 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_A64_TYPE); 264 265 /* No data transfer */ 266 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { 267 cmd_pkt->byte_count = cpu_to_le32(0); 268 return; 269 } 270 271 vha = sp->vha; 272 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp)); 273 274 /* Two DSDs are available in the Command Type 3 IOCB */ 275 avail_dsds = 2; 276 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 277 278 /* Load data segments */ 279 scsi_for_each_sg(cmd, sg, tot_dsds, i) { 280 dma_addr_t sle_dma; 281 cont_a64_entry_t *cont_pkt; 282 283 /* Allocate additional continuation packets? */ 284 if (avail_dsds == 0) { 285 /* 286 * Five DSDs are available in the Continuation 287 * Type 1 IOCB. 288 */ 289 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req); 290 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; 291 avail_dsds = 5; 292 } 293 294 sle_dma = sg_dma_address(sg); 295 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 296 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 297 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 298 avail_dsds--; 299 } 300 } 301 302 /** 303 * qla2x00_start_scsi() - Send a SCSI command to the ISP 304 * @sp: command to send to the ISP 305 * 306 * Returns non-zero if a failure occurred, else zero. 307 */ 308 int 309 qla2x00_start_scsi(srb_t *sp) 310 { 311 int nseg; 312 unsigned long flags; 313 scsi_qla_host_t *vha; 314 struct scsi_cmnd *cmd; 315 uint32_t *clr_ptr; 316 uint32_t index; 317 uint32_t handle; 318 cmd_entry_t *cmd_pkt; 319 uint16_t cnt; 320 uint16_t req_cnt; 321 uint16_t tot_dsds; 322 struct device_reg_2xxx __iomem *reg; 323 struct qla_hw_data *ha; 324 struct req_que *req; 325 struct rsp_que *rsp; 326 327 /* Setup device pointers. */ 328 vha = sp->vha; 329 ha = vha->hw; 330 reg = &ha->iobase->isp; 331 cmd = GET_CMD_SP(sp); 332 req = ha->req_q_map[0]; 333 rsp = ha->rsp_q_map[0]; 334 /* So we know we haven't pci_map'ed anything yet */ 335 tot_dsds = 0; 336 337 /* Send marker if required */ 338 if (vha->marker_needed != 0) { 339 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) != 340 QLA_SUCCESS) { 341 return (QLA_FUNCTION_FAILED); 342 } 343 vha->marker_needed = 0; 344 } 345 346 /* Acquire ring specific lock */ 347 spin_lock_irqsave(&ha->hardware_lock, flags); 348 349 /* Check for room in outstanding command list. */ 350 handle = req->current_outstanding_cmd; 351 for (index = 1; index < req->num_outstanding_cmds; index++) { 352 handle++; 353 if (handle == req->num_outstanding_cmds) 354 handle = 1; 355 if (!req->outstanding_cmds[handle]) 356 break; 357 } 358 if (index == req->num_outstanding_cmds) 359 goto queuing_error; 360 361 /* Map the sg table so we have an accurate count of sg entries needed */ 362 if (scsi_sg_count(cmd)) { 363 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 364 scsi_sg_count(cmd), cmd->sc_data_direction); 365 if (unlikely(!nseg)) 366 goto queuing_error; 367 } else 368 nseg = 0; 369 370 tot_dsds = nseg; 371 372 /* Calculate the number of request entries needed. */ 373 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds); 374 if (req->cnt < (req_cnt + 2)) { 375 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg)); 376 if (req->ring_index < cnt) 377 req->cnt = cnt - req->ring_index; 378 else 379 req->cnt = req->length - 380 (req->ring_index - cnt); 381 /* If still no head room then bail out */ 382 if (req->cnt < (req_cnt + 2)) 383 goto queuing_error; 384 } 385 386 /* Build command packet */ 387 req->current_outstanding_cmd = handle; 388 req->outstanding_cmds[handle] = sp; 389 sp->handle = handle; 390 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 391 req->cnt -= req_cnt; 392 393 cmd_pkt = (cmd_entry_t *)req->ring_ptr; 394 cmd_pkt->handle = handle; 395 /* Zero out remaining portion of packet. */ 396 clr_ptr = (uint32_t *)cmd_pkt + 2; 397 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 398 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 399 400 /* Set target ID and LUN number*/ 401 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id); 402 cmd_pkt->lun = cpu_to_le16(cmd->device->lun); 403 cmd_pkt->control_flags = cpu_to_le16(CF_SIMPLE_TAG); 404 405 /* Load SCSI command packet. */ 406 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len); 407 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); 408 409 /* Build IOCB segments */ 410 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds); 411 412 /* Set total data segment count. */ 413 cmd_pkt->entry_count = (uint8_t)req_cnt; 414 wmb(); 415 416 /* Adjust ring index. */ 417 req->ring_index++; 418 if (req->ring_index == req->length) { 419 req->ring_index = 0; 420 req->ring_ptr = req->ring; 421 } else 422 req->ring_ptr++; 423 424 sp->flags |= SRB_DMA_VALID; 425 426 /* Set chip new ring index. */ 427 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index); 428 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */ 429 430 /* Manage unprocessed RIO/ZIO commands in response queue. */ 431 if (vha->flags.process_response_queue && 432 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 433 qla2x00_process_response_queue(rsp); 434 435 spin_unlock_irqrestore(&ha->hardware_lock, flags); 436 return (QLA_SUCCESS); 437 438 queuing_error: 439 if (tot_dsds) 440 scsi_dma_unmap(cmd); 441 442 spin_unlock_irqrestore(&ha->hardware_lock, flags); 443 444 return (QLA_FUNCTION_FAILED); 445 } 446 447 /** 448 * qla2x00_start_iocbs() - Execute the IOCB command 449 * @vha: HA context 450 * @req: request queue 451 */ 452 void 453 qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req) 454 { 455 struct qla_hw_data *ha = vha->hw; 456 device_reg_t *reg = ISP_QUE_REG(ha, req->id); 457 458 if (IS_P3P_TYPE(ha)) { 459 qla82xx_start_iocbs(vha); 460 } else { 461 /* Adjust ring index. */ 462 req->ring_index++; 463 if (req->ring_index == req->length) { 464 req->ring_index = 0; 465 req->ring_ptr = req->ring; 466 } else 467 req->ring_ptr++; 468 469 /* Set chip new ring index. */ 470 if (ha->mqenable || IS_QLA27XX(ha)) { 471 WRT_REG_DWORD(req->req_q_in, req->ring_index); 472 } else if (IS_QLA83XX(ha)) { 473 WRT_REG_DWORD(req->req_q_in, req->ring_index); 474 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr); 475 } else if (IS_QLAFX00(ha)) { 476 WRT_REG_DWORD(®->ispfx00.req_q_in, req->ring_index); 477 RD_REG_DWORD_RELAXED(®->ispfx00.req_q_in); 478 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code); 479 } else if (IS_FWI2_CAPABLE(ha)) { 480 WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index); 481 RD_REG_DWORD_RELAXED(®->isp24.req_q_in); 482 } else { 483 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), 484 req->ring_index); 485 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp)); 486 } 487 } 488 } 489 490 /** 491 * qla2x00_marker() - Send a marker IOCB to the firmware. 492 * @vha: HA context 493 * @req: request queue 494 * @rsp: response queue 495 * @loop_id: loop ID 496 * @lun: LUN 497 * @type: marker modifier 498 * 499 * Can be called from both normal and interrupt context. 500 * 501 * Returns non-zero if a failure occurred, else zero. 502 */ 503 static int 504 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req, 505 struct rsp_que *rsp, uint16_t loop_id, 506 uint64_t lun, uint8_t type) 507 { 508 mrk_entry_t *mrk; 509 struct mrk_entry_24xx *mrk24 = NULL; 510 511 struct qla_hw_data *ha = vha->hw; 512 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev); 513 514 req = ha->req_q_map[0]; 515 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL); 516 if (mrk == NULL) { 517 ql_log(ql_log_warn, base_vha, 0x3026, 518 "Failed to allocate Marker IOCB.\n"); 519 520 return (QLA_FUNCTION_FAILED); 521 } 522 523 mrk->entry_type = MARKER_TYPE; 524 mrk->modifier = type; 525 if (type != MK_SYNC_ALL) { 526 if (IS_FWI2_CAPABLE(ha)) { 527 mrk24 = (struct mrk_entry_24xx *) mrk; 528 mrk24->nport_handle = cpu_to_le16(loop_id); 529 int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun); 530 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun)); 531 mrk24->vp_index = vha->vp_idx; 532 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle); 533 } else { 534 SET_TARGET_ID(ha, mrk->target, loop_id); 535 mrk->lun = cpu_to_le16((uint16_t)lun); 536 } 537 } 538 wmb(); 539 540 qla2x00_start_iocbs(vha, req); 541 542 return (QLA_SUCCESS); 543 } 544 545 int 546 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req, 547 struct rsp_que *rsp, uint16_t loop_id, uint64_t lun, 548 uint8_t type) 549 { 550 int ret; 551 unsigned long flags = 0; 552 553 spin_lock_irqsave(&vha->hw->hardware_lock, flags); 554 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type); 555 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags); 556 557 return (ret); 558 } 559 560 /* 561 * qla2x00_issue_marker 562 * 563 * Issue marker 564 * Caller CAN have hardware lock held as specified by ha_locked parameter. 565 * Might release it, then reaquire. 566 */ 567 int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked) 568 { 569 if (ha_locked) { 570 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0, 571 MK_SYNC_ALL) != QLA_SUCCESS) 572 return QLA_FUNCTION_FAILED; 573 } else { 574 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0, 575 MK_SYNC_ALL) != QLA_SUCCESS) 576 return QLA_FUNCTION_FAILED; 577 } 578 vha->marker_needed = 0; 579 580 return QLA_SUCCESS; 581 } 582 583 static inline int 584 qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt, 585 uint16_t tot_dsds) 586 { 587 uint32_t *cur_dsd = NULL; 588 scsi_qla_host_t *vha; 589 struct qla_hw_data *ha; 590 struct scsi_cmnd *cmd; 591 struct scatterlist *cur_seg; 592 uint32_t *dsd_seg; 593 void *next_dsd; 594 uint8_t avail_dsds; 595 uint8_t first_iocb = 1; 596 uint32_t dsd_list_len; 597 struct dsd_dma *dsd_ptr; 598 struct ct6_dsd *ctx; 599 600 cmd = GET_CMD_SP(sp); 601 602 /* Update entry type to indicate Command Type 3 IOCB */ 603 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_6); 604 605 /* No data transfer */ 606 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { 607 cmd_pkt->byte_count = cpu_to_le32(0); 608 return 0; 609 } 610 611 vha = sp->vha; 612 ha = vha->hw; 613 614 /* Set transfer direction */ 615 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 616 cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA); 617 vha->qla_stats.output_bytes += scsi_bufflen(cmd); 618 vha->qla_stats.output_requests++; 619 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 620 cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA); 621 vha->qla_stats.input_bytes += scsi_bufflen(cmd); 622 vha->qla_stats.input_requests++; 623 } 624 625 cur_seg = scsi_sglist(cmd); 626 ctx = GET_CMD_CTX_SP(sp); 627 628 while (tot_dsds) { 629 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ? 630 QLA_DSDS_PER_IOCB : tot_dsds; 631 tot_dsds -= avail_dsds; 632 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE; 633 634 dsd_ptr = list_first_entry(&ha->gbl_dsd_list, 635 struct dsd_dma, list); 636 next_dsd = dsd_ptr->dsd_addr; 637 list_del(&dsd_ptr->list); 638 ha->gbl_dsd_avail--; 639 list_add_tail(&dsd_ptr->list, &ctx->dsd_list); 640 ctx->dsd_use_cnt++; 641 ha->gbl_dsd_inuse++; 642 643 if (first_iocb) { 644 first_iocb = 0; 645 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address; 646 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma)); 647 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma)); 648 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len); 649 } else { 650 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma)); 651 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma)); 652 *cur_dsd++ = cpu_to_le32(dsd_list_len); 653 } 654 cur_dsd = (uint32_t *)next_dsd; 655 while (avail_dsds) { 656 dma_addr_t sle_dma; 657 658 sle_dma = sg_dma_address(cur_seg); 659 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 660 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 661 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg)); 662 cur_seg = sg_next(cur_seg); 663 avail_dsds--; 664 } 665 } 666 667 /* Null termination */ 668 *cur_dsd++ = 0; 669 *cur_dsd++ = 0; 670 *cur_dsd++ = 0; 671 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE; 672 return 0; 673 } 674 675 /* 676 * qla24xx_calc_dsd_lists() - Determine number of DSD list required 677 * for Command Type 6. 678 * 679 * @dsds: number of data segment decriptors needed 680 * 681 * Returns the number of dsd list needed to store @dsds. 682 */ 683 static inline uint16_t 684 qla24xx_calc_dsd_lists(uint16_t dsds) 685 { 686 uint16_t dsd_lists = 0; 687 688 dsd_lists = (dsds/QLA_DSDS_PER_IOCB); 689 if (dsds % QLA_DSDS_PER_IOCB) 690 dsd_lists++; 691 return dsd_lists; 692 } 693 694 695 /** 696 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7 697 * IOCB types. 698 * 699 * @sp: SRB command to process 700 * @cmd_pkt: Command type 3 IOCB 701 * @tot_dsds: Total number of segments to transfer 702 * @req: pointer to request queue 703 */ 704 inline void 705 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt, 706 uint16_t tot_dsds, struct req_que *req) 707 { 708 uint16_t avail_dsds; 709 uint32_t *cur_dsd; 710 scsi_qla_host_t *vha; 711 struct scsi_cmnd *cmd; 712 struct scatterlist *sg; 713 int i; 714 715 cmd = GET_CMD_SP(sp); 716 717 /* Update entry type to indicate Command Type 3 IOCB */ 718 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_7); 719 720 /* No data transfer */ 721 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) { 722 cmd_pkt->byte_count = cpu_to_le32(0); 723 return; 724 } 725 726 vha = sp->vha; 727 728 /* Set transfer direction */ 729 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 730 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_WRITE_DATA); 731 vha->qla_stats.output_bytes += scsi_bufflen(cmd); 732 vha->qla_stats.output_requests++; 733 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 734 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_READ_DATA); 735 vha->qla_stats.input_bytes += scsi_bufflen(cmd); 736 vha->qla_stats.input_requests++; 737 } 738 739 /* One DSD is available in the Command Type 3 IOCB */ 740 avail_dsds = 1; 741 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 742 743 /* Load data segments */ 744 745 scsi_for_each_sg(cmd, sg, tot_dsds, i) { 746 dma_addr_t sle_dma; 747 cont_a64_entry_t *cont_pkt; 748 749 /* Allocate additional continuation packets? */ 750 if (avail_dsds == 0) { 751 /* 752 * Five DSDs are available in the Continuation 753 * Type 1 IOCB. 754 */ 755 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req); 756 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; 757 avail_dsds = 5; 758 } 759 760 sle_dma = sg_dma_address(sg); 761 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 762 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 763 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 764 avail_dsds--; 765 } 766 } 767 768 struct fw_dif_context { 769 uint32_t ref_tag; 770 uint16_t app_tag; 771 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/ 772 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/ 773 }; 774 775 /* 776 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command 777 * 778 */ 779 static inline void 780 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt, 781 unsigned int protcnt) 782 { 783 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 784 785 switch (scsi_get_prot_type(cmd)) { 786 case SCSI_PROT_DIF_TYPE0: 787 /* 788 * No check for ql2xenablehba_err_chk, as it would be an 789 * I/O error if hba tag generation is not done. 790 */ 791 pkt->ref_tag = cpu_to_le32((uint32_t) 792 (0xffffffff & scsi_get_lba(cmd))); 793 794 if (!qla2x00_hba_err_chk_enabled(sp)) 795 break; 796 797 pkt->ref_tag_mask[0] = 0xff; 798 pkt->ref_tag_mask[1] = 0xff; 799 pkt->ref_tag_mask[2] = 0xff; 800 pkt->ref_tag_mask[3] = 0xff; 801 break; 802 803 /* 804 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to 805 * match LBA in CDB + N 806 */ 807 case SCSI_PROT_DIF_TYPE2: 808 pkt->app_tag = cpu_to_le16(0); 809 pkt->app_tag_mask[0] = 0x0; 810 pkt->app_tag_mask[1] = 0x0; 811 812 pkt->ref_tag = cpu_to_le32((uint32_t) 813 (0xffffffff & scsi_get_lba(cmd))); 814 815 if (!qla2x00_hba_err_chk_enabled(sp)) 816 break; 817 818 /* enable ALL bytes of the ref tag */ 819 pkt->ref_tag_mask[0] = 0xff; 820 pkt->ref_tag_mask[1] = 0xff; 821 pkt->ref_tag_mask[2] = 0xff; 822 pkt->ref_tag_mask[3] = 0xff; 823 break; 824 825 /* For Type 3 protection: 16 bit GUARD only */ 826 case SCSI_PROT_DIF_TYPE3: 827 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] = 828 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] = 829 0x00; 830 break; 831 832 /* 833 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and 834 * 16 bit app tag. 835 */ 836 case SCSI_PROT_DIF_TYPE1: 837 pkt->ref_tag = cpu_to_le32((uint32_t) 838 (0xffffffff & scsi_get_lba(cmd))); 839 pkt->app_tag = cpu_to_le16(0); 840 pkt->app_tag_mask[0] = 0x0; 841 pkt->app_tag_mask[1] = 0x0; 842 843 if (!qla2x00_hba_err_chk_enabled(sp)) 844 break; 845 846 /* enable ALL bytes of the ref tag */ 847 pkt->ref_tag_mask[0] = 0xff; 848 pkt->ref_tag_mask[1] = 0xff; 849 pkt->ref_tag_mask[2] = 0xff; 850 pkt->ref_tag_mask[3] = 0xff; 851 break; 852 } 853 } 854 855 int 856 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx, 857 uint32_t *partial) 858 { 859 struct scatterlist *sg; 860 uint32_t cumulative_partial, sg_len; 861 dma_addr_t sg_dma_addr; 862 863 if (sgx->num_bytes == sgx->tot_bytes) 864 return 0; 865 866 sg = sgx->cur_sg; 867 cumulative_partial = sgx->tot_partial; 868 869 sg_dma_addr = sg_dma_address(sg); 870 sg_len = sg_dma_len(sg); 871 872 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed; 873 874 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) { 875 sgx->dma_len = (blk_sz - cumulative_partial); 876 sgx->tot_partial = 0; 877 sgx->num_bytes += blk_sz; 878 *partial = 0; 879 } else { 880 sgx->dma_len = sg_len - sgx->bytes_consumed; 881 sgx->tot_partial += sgx->dma_len; 882 *partial = 1; 883 } 884 885 sgx->bytes_consumed += sgx->dma_len; 886 887 if (sg_len == sgx->bytes_consumed) { 888 sg = sg_next(sg); 889 sgx->num_sg++; 890 sgx->cur_sg = sg; 891 sgx->bytes_consumed = 0; 892 } 893 894 return 1; 895 } 896 897 int 898 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp, 899 uint32_t *dsd, uint16_t tot_dsds, struct qla_tc_param *tc) 900 { 901 void *next_dsd; 902 uint8_t avail_dsds = 0; 903 uint32_t dsd_list_len; 904 struct dsd_dma *dsd_ptr; 905 struct scatterlist *sg_prot; 906 uint32_t *cur_dsd = dsd; 907 uint16_t used_dsds = tot_dsds; 908 uint32_t prot_int; /* protection interval */ 909 uint32_t partial; 910 struct qla2_sgx sgx; 911 dma_addr_t sle_dma; 912 uint32_t sle_dma_len, tot_prot_dma_len = 0; 913 struct scsi_cmnd *cmd; 914 915 memset(&sgx, 0, sizeof(struct qla2_sgx)); 916 if (sp) { 917 cmd = GET_CMD_SP(sp); 918 prot_int = cmd->device->sector_size; 919 920 sgx.tot_bytes = scsi_bufflen(cmd); 921 sgx.cur_sg = scsi_sglist(cmd); 922 sgx.sp = sp; 923 924 sg_prot = scsi_prot_sglist(cmd); 925 } else if (tc) { 926 prot_int = tc->blk_sz; 927 sgx.tot_bytes = tc->bufflen; 928 sgx.cur_sg = tc->sg; 929 sg_prot = tc->prot_sg; 930 } else { 931 BUG(); 932 return 1; 933 } 934 935 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) { 936 937 sle_dma = sgx.dma_addr; 938 sle_dma_len = sgx.dma_len; 939 alloc_and_fill: 940 /* Allocate additional continuation packets? */ 941 if (avail_dsds == 0) { 942 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ? 943 QLA_DSDS_PER_IOCB : used_dsds; 944 dsd_list_len = (avail_dsds + 1) * 12; 945 used_dsds -= avail_dsds; 946 947 /* allocate tracking DS */ 948 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC); 949 if (!dsd_ptr) 950 return 1; 951 952 /* allocate new list */ 953 dsd_ptr->dsd_addr = next_dsd = 954 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, 955 &dsd_ptr->dsd_list_dma); 956 957 if (!next_dsd) { 958 /* 959 * Need to cleanup only this dsd_ptr, rest 960 * will be done by sp_free_dma() 961 */ 962 kfree(dsd_ptr); 963 return 1; 964 } 965 966 if (sp) { 967 list_add_tail(&dsd_ptr->list, 968 &((struct crc_context *) 969 sp->u.scmd.ctx)->dsd_list); 970 971 sp->flags |= SRB_CRC_CTX_DSD_VALID; 972 } else { 973 list_add_tail(&dsd_ptr->list, 974 &(tc->ctx->dsd_list)); 975 *tc->ctx_dsd_alloced = 1; 976 } 977 978 979 /* add new list to cmd iocb or last list */ 980 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma)); 981 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma)); 982 *cur_dsd++ = dsd_list_len; 983 cur_dsd = (uint32_t *)next_dsd; 984 } 985 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 986 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 987 *cur_dsd++ = cpu_to_le32(sle_dma_len); 988 avail_dsds--; 989 990 if (partial == 0) { 991 /* Got a full protection interval */ 992 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len; 993 sle_dma_len = 8; 994 995 tot_prot_dma_len += sle_dma_len; 996 if (tot_prot_dma_len == sg_dma_len(sg_prot)) { 997 tot_prot_dma_len = 0; 998 sg_prot = sg_next(sg_prot); 999 } 1000 1001 partial = 1; /* So as to not re-enter this block */ 1002 goto alloc_and_fill; 1003 } 1004 } 1005 /* Null termination */ 1006 *cur_dsd++ = 0; 1007 *cur_dsd++ = 0; 1008 *cur_dsd++ = 0; 1009 return 0; 1010 } 1011 1012 int 1013 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd, 1014 uint16_t tot_dsds, struct qla_tc_param *tc) 1015 { 1016 void *next_dsd; 1017 uint8_t avail_dsds = 0; 1018 uint32_t dsd_list_len; 1019 struct dsd_dma *dsd_ptr; 1020 struct scatterlist *sg, *sgl; 1021 uint32_t *cur_dsd = dsd; 1022 int i; 1023 uint16_t used_dsds = tot_dsds; 1024 struct scsi_cmnd *cmd; 1025 1026 if (sp) { 1027 cmd = GET_CMD_SP(sp); 1028 sgl = scsi_sglist(cmd); 1029 } else if (tc) { 1030 sgl = tc->sg; 1031 } else { 1032 BUG(); 1033 return 1; 1034 } 1035 1036 1037 for_each_sg(sgl, sg, tot_dsds, i) { 1038 dma_addr_t sle_dma; 1039 1040 /* Allocate additional continuation packets? */ 1041 if (avail_dsds == 0) { 1042 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ? 1043 QLA_DSDS_PER_IOCB : used_dsds; 1044 dsd_list_len = (avail_dsds + 1) * 12; 1045 used_dsds -= avail_dsds; 1046 1047 /* allocate tracking DS */ 1048 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC); 1049 if (!dsd_ptr) 1050 return 1; 1051 1052 /* allocate new list */ 1053 dsd_ptr->dsd_addr = next_dsd = 1054 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, 1055 &dsd_ptr->dsd_list_dma); 1056 1057 if (!next_dsd) { 1058 /* 1059 * Need to cleanup only this dsd_ptr, rest 1060 * will be done by sp_free_dma() 1061 */ 1062 kfree(dsd_ptr); 1063 return 1; 1064 } 1065 1066 if (sp) { 1067 list_add_tail(&dsd_ptr->list, 1068 &((struct crc_context *) 1069 sp->u.scmd.ctx)->dsd_list); 1070 1071 sp->flags |= SRB_CRC_CTX_DSD_VALID; 1072 } else { 1073 list_add_tail(&dsd_ptr->list, 1074 &(tc->ctx->dsd_list)); 1075 *tc->ctx_dsd_alloced = 1; 1076 } 1077 1078 /* add new list to cmd iocb or last list */ 1079 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma)); 1080 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma)); 1081 *cur_dsd++ = dsd_list_len; 1082 cur_dsd = (uint32_t *)next_dsd; 1083 } 1084 sle_dma = sg_dma_address(sg); 1085 1086 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 1087 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 1088 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 1089 avail_dsds--; 1090 1091 } 1092 /* Null termination */ 1093 *cur_dsd++ = 0; 1094 *cur_dsd++ = 0; 1095 *cur_dsd++ = 0; 1096 return 0; 1097 } 1098 1099 int 1100 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp, 1101 uint32_t *dsd, uint16_t tot_dsds, struct qla_tc_param *tc) 1102 { 1103 void *next_dsd; 1104 uint8_t avail_dsds = 0; 1105 uint32_t dsd_list_len; 1106 struct dsd_dma *dsd_ptr; 1107 struct scatterlist *sg, *sgl; 1108 int i; 1109 struct scsi_cmnd *cmd; 1110 uint32_t *cur_dsd = dsd; 1111 uint16_t used_dsds = tot_dsds; 1112 struct scsi_qla_host *vha; 1113 1114 if (sp) { 1115 cmd = GET_CMD_SP(sp); 1116 sgl = scsi_prot_sglist(cmd); 1117 vha = sp->vha; 1118 } else if (tc) { 1119 vha = tc->vha; 1120 sgl = tc->prot_sg; 1121 } else { 1122 BUG(); 1123 return 1; 1124 } 1125 1126 ql_dbg(ql_dbg_tgt, vha, 0xe021, 1127 "%s: enter\n", __func__); 1128 1129 for_each_sg(sgl, sg, tot_dsds, i) { 1130 dma_addr_t sle_dma; 1131 1132 /* Allocate additional continuation packets? */ 1133 if (avail_dsds == 0) { 1134 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ? 1135 QLA_DSDS_PER_IOCB : used_dsds; 1136 dsd_list_len = (avail_dsds + 1) * 12; 1137 used_dsds -= avail_dsds; 1138 1139 /* allocate tracking DS */ 1140 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC); 1141 if (!dsd_ptr) 1142 return 1; 1143 1144 /* allocate new list */ 1145 dsd_ptr->dsd_addr = next_dsd = 1146 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, 1147 &dsd_ptr->dsd_list_dma); 1148 1149 if (!next_dsd) { 1150 /* 1151 * Need to cleanup only this dsd_ptr, rest 1152 * will be done by sp_free_dma() 1153 */ 1154 kfree(dsd_ptr); 1155 return 1; 1156 } 1157 1158 if (sp) { 1159 list_add_tail(&dsd_ptr->list, 1160 &((struct crc_context *) 1161 sp->u.scmd.ctx)->dsd_list); 1162 1163 sp->flags |= SRB_CRC_CTX_DSD_VALID; 1164 } else { 1165 list_add_tail(&dsd_ptr->list, 1166 &(tc->ctx->dsd_list)); 1167 *tc->ctx_dsd_alloced = 1; 1168 } 1169 1170 /* add new list to cmd iocb or last list */ 1171 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma)); 1172 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma)); 1173 *cur_dsd++ = dsd_list_len; 1174 cur_dsd = (uint32_t *)next_dsd; 1175 } 1176 sle_dma = sg_dma_address(sg); 1177 1178 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 1179 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 1180 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 1181 1182 avail_dsds--; 1183 } 1184 /* Null termination */ 1185 *cur_dsd++ = 0; 1186 *cur_dsd++ = 0; 1187 *cur_dsd++ = 0; 1188 return 0; 1189 } 1190 1191 /** 1192 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command 1193 * Type 6 IOCB types. 1194 * 1195 * @sp: SRB command to process 1196 * @cmd_pkt: Command type 3 IOCB 1197 * @tot_dsds: Total number of segments to transfer 1198 * @tot_prot_dsds: 1199 * @fw_prot_opts: 1200 */ 1201 inline int 1202 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt, 1203 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts) 1204 { 1205 uint32_t *cur_dsd, *fcp_dl; 1206 scsi_qla_host_t *vha; 1207 struct scsi_cmnd *cmd; 1208 uint32_t total_bytes = 0; 1209 uint32_t data_bytes; 1210 uint32_t dif_bytes; 1211 uint8_t bundling = 1; 1212 uint16_t blk_size; 1213 struct crc_context *crc_ctx_pkt = NULL; 1214 struct qla_hw_data *ha; 1215 uint8_t additional_fcpcdb_len; 1216 uint16_t fcp_cmnd_len; 1217 struct fcp_cmnd *fcp_cmnd; 1218 dma_addr_t crc_ctx_dma; 1219 1220 cmd = GET_CMD_SP(sp); 1221 1222 /* Update entry type to indicate Command Type CRC_2 IOCB */ 1223 *((uint32_t *)(&cmd_pkt->entry_type)) = cpu_to_le32(COMMAND_TYPE_CRC_2); 1224 1225 vha = sp->vha; 1226 ha = vha->hw; 1227 1228 /* No data transfer */ 1229 data_bytes = scsi_bufflen(cmd); 1230 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) { 1231 cmd_pkt->byte_count = cpu_to_le32(0); 1232 return QLA_SUCCESS; 1233 } 1234 1235 cmd_pkt->vp_index = sp->vha->vp_idx; 1236 1237 /* Set transfer direction */ 1238 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 1239 cmd_pkt->control_flags = 1240 cpu_to_le16(CF_WRITE_DATA); 1241 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 1242 cmd_pkt->control_flags = 1243 cpu_to_le16(CF_READ_DATA); 1244 } 1245 1246 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) || 1247 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) || 1248 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) || 1249 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT)) 1250 bundling = 0; 1251 1252 /* Allocate CRC context from global pool */ 1253 crc_ctx_pkt = sp->u.scmd.ctx = 1254 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma); 1255 1256 if (!crc_ctx_pkt) 1257 goto crc_queuing_error; 1258 1259 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma; 1260 1261 sp->flags |= SRB_CRC_CTX_DMA_VALID; 1262 1263 /* Set handle */ 1264 crc_ctx_pkt->handle = cmd_pkt->handle; 1265 1266 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list); 1267 1268 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *) 1269 &crc_ctx_pkt->ref_tag, tot_prot_dsds); 1270 1271 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma)); 1272 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma)); 1273 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW; 1274 1275 /* Determine SCSI command length -- align to 4 byte boundary */ 1276 if (cmd->cmd_len > 16) { 1277 additional_fcpcdb_len = cmd->cmd_len - 16; 1278 if ((cmd->cmd_len % 4) != 0) { 1279 /* SCSI cmd > 16 bytes must be multiple of 4 */ 1280 goto crc_queuing_error; 1281 } 1282 fcp_cmnd_len = 12 + cmd->cmd_len + 4; 1283 } else { 1284 additional_fcpcdb_len = 0; 1285 fcp_cmnd_len = 12 + 16 + 4; 1286 } 1287 1288 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd; 1289 1290 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len; 1291 if (cmd->sc_data_direction == DMA_TO_DEVICE) 1292 fcp_cmnd->additional_cdb_len |= 1; 1293 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 1294 fcp_cmnd->additional_cdb_len |= 2; 1295 1296 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun); 1297 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len); 1298 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len); 1299 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32( 1300 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF)); 1301 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32( 1302 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF)); 1303 fcp_cmnd->task_management = 0; 1304 fcp_cmnd->task_attribute = TSK_SIMPLE; 1305 1306 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */ 1307 1308 /* Compute dif len and adjust data len to incude protection */ 1309 dif_bytes = 0; 1310 blk_size = cmd->device->sector_size; 1311 dif_bytes = (data_bytes / blk_size) * 8; 1312 1313 switch (scsi_get_prot_op(GET_CMD_SP(sp))) { 1314 case SCSI_PROT_READ_INSERT: 1315 case SCSI_PROT_WRITE_STRIP: 1316 total_bytes = data_bytes; 1317 data_bytes += dif_bytes; 1318 break; 1319 1320 case SCSI_PROT_READ_STRIP: 1321 case SCSI_PROT_WRITE_INSERT: 1322 case SCSI_PROT_READ_PASS: 1323 case SCSI_PROT_WRITE_PASS: 1324 total_bytes = data_bytes + dif_bytes; 1325 break; 1326 default: 1327 BUG(); 1328 } 1329 1330 if (!qla2x00_hba_err_chk_enabled(sp)) 1331 fw_prot_opts |= 0x10; /* Disable Guard tag checking */ 1332 /* HBA error checking enabled */ 1333 else if (IS_PI_UNINIT_CAPABLE(ha)) { 1334 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1) 1335 || (scsi_get_prot_type(GET_CMD_SP(sp)) == 1336 SCSI_PROT_DIF_TYPE2)) 1337 fw_prot_opts |= BIT_10; 1338 else if (scsi_get_prot_type(GET_CMD_SP(sp)) == 1339 SCSI_PROT_DIF_TYPE3) 1340 fw_prot_opts |= BIT_11; 1341 } 1342 1343 if (!bundling) { 1344 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address; 1345 } else { 1346 /* 1347 * Configure Bundling if we need to fetch interlaving 1348 * protection PCI accesses 1349 */ 1350 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING; 1351 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes); 1352 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds - 1353 tot_prot_dsds); 1354 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address; 1355 } 1356 1357 /* Finish the common fields of CRC pkt */ 1358 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size); 1359 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts); 1360 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes); 1361 crc_ctx_pkt->guard_seed = cpu_to_le16(0); 1362 /* Fibre channel byte count */ 1363 cmd_pkt->byte_count = cpu_to_le32(total_bytes); 1364 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 + 1365 additional_fcpcdb_len); 1366 *fcp_dl = htonl(total_bytes); 1367 1368 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) { 1369 cmd_pkt->byte_count = cpu_to_le32(0); 1370 return QLA_SUCCESS; 1371 } 1372 /* Walks data segments */ 1373 1374 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE); 1375 1376 if (!bundling && tot_prot_dsds) { 1377 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp, 1378 cur_dsd, tot_dsds, NULL)) 1379 goto crc_queuing_error; 1380 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd, 1381 (tot_dsds - tot_prot_dsds), NULL)) 1382 goto crc_queuing_error; 1383 1384 if (bundling && tot_prot_dsds) { 1385 /* Walks dif segments */ 1386 cmd_pkt->control_flags |= cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE); 1387 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address; 1388 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd, 1389 tot_prot_dsds, NULL)) 1390 goto crc_queuing_error; 1391 } 1392 return QLA_SUCCESS; 1393 1394 crc_queuing_error: 1395 /* Cleanup will be performed by the caller */ 1396 1397 return QLA_FUNCTION_FAILED; 1398 } 1399 1400 /** 1401 * qla24xx_start_scsi() - Send a SCSI command to the ISP 1402 * @sp: command to send to the ISP 1403 * 1404 * Returns non-zero if a failure occurred, else zero. 1405 */ 1406 int 1407 qla24xx_start_scsi(srb_t *sp) 1408 { 1409 int nseg; 1410 unsigned long flags; 1411 uint32_t *clr_ptr; 1412 uint32_t index; 1413 uint32_t handle; 1414 struct cmd_type_7 *cmd_pkt; 1415 uint16_t cnt; 1416 uint16_t req_cnt; 1417 uint16_t tot_dsds; 1418 struct req_que *req = NULL; 1419 struct rsp_que *rsp = NULL; 1420 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 1421 struct scsi_qla_host *vha = sp->vha; 1422 struct qla_hw_data *ha = vha->hw; 1423 1424 /* Setup device pointers. */ 1425 req = vha->req; 1426 rsp = req->rsp; 1427 1428 /* So we know we haven't pci_map'ed anything yet */ 1429 tot_dsds = 0; 1430 1431 /* Send marker if required */ 1432 if (vha->marker_needed != 0) { 1433 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) != 1434 QLA_SUCCESS) 1435 return QLA_FUNCTION_FAILED; 1436 vha->marker_needed = 0; 1437 } 1438 1439 /* Acquire ring specific lock */ 1440 spin_lock_irqsave(&ha->hardware_lock, flags); 1441 1442 /* Check for room in outstanding command list. */ 1443 handle = req->current_outstanding_cmd; 1444 for (index = 1; index < req->num_outstanding_cmds; index++) { 1445 handle++; 1446 if (handle == req->num_outstanding_cmds) 1447 handle = 1; 1448 if (!req->outstanding_cmds[handle]) 1449 break; 1450 } 1451 if (index == req->num_outstanding_cmds) 1452 goto queuing_error; 1453 1454 /* Map the sg table so we have an accurate count of sg entries needed */ 1455 if (scsi_sg_count(cmd)) { 1456 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 1457 scsi_sg_count(cmd), cmd->sc_data_direction); 1458 if (unlikely(!nseg)) 1459 goto queuing_error; 1460 } else 1461 nseg = 0; 1462 1463 tot_dsds = nseg; 1464 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds); 1465 if (req->cnt < (req_cnt + 2)) { 1466 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr : 1467 RD_REG_DWORD_RELAXED(req->req_q_out); 1468 if (req->ring_index < cnt) 1469 req->cnt = cnt - req->ring_index; 1470 else 1471 req->cnt = req->length - 1472 (req->ring_index - cnt); 1473 if (req->cnt < (req_cnt + 2)) 1474 goto queuing_error; 1475 } 1476 1477 /* Build command packet. */ 1478 req->current_outstanding_cmd = handle; 1479 req->outstanding_cmds[handle] = sp; 1480 sp->handle = handle; 1481 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 1482 req->cnt -= req_cnt; 1483 1484 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr; 1485 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 1486 1487 /* Zero out remaining portion of packet. */ 1488 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */ 1489 clr_ptr = (uint32_t *)cmd_pkt + 2; 1490 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 1491 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 1492 1493 /* Set NPORT-ID and LUN number*/ 1494 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 1495 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 1496 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 1497 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 1498 cmd_pkt->vp_index = sp->vha->vp_idx; 1499 1500 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 1501 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); 1502 1503 cmd_pkt->task = TSK_SIMPLE; 1504 1505 /* Load SCSI command packet. */ 1506 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); 1507 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); 1508 1509 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); 1510 1511 /* Build IOCB segments */ 1512 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req); 1513 1514 /* Set total data segment count. */ 1515 cmd_pkt->entry_count = (uint8_t)req_cnt; 1516 wmb(); 1517 /* Adjust ring index. */ 1518 req->ring_index++; 1519 if (req->ring_index == req->length) { 1520 req->ring_index = 0; 1521 req->ring_ptr = req->ring; 1522 } else 1523 req->ring_ptr++; 1524 1525 sp->flags |= SRB_DMA_VALID; 1526 1527 /* Set chip new ring index. */ 1528 WRT_REG_DWORD(req->req_q_in, req->ring_index); 1529 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr); 1530 1531 /* Manage unprocessed RIO/ZIO commands in response queue. */ 1532 if (vha->flags.process_response_queue && 1533 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 1534 qla24xx_process_response_queue(vha, rsp); 1535 1536 spin_unlock_irqrestore(&ha->hardware_lock, flags); 1537 return QLA_SUCCESS; 1538 1539 queuing_error: 1540 if (tot_dsds) 1541 scsi_dma_unmap(cmd); 1542 1543 spin_unlock_irqrestore(&ha->hardware_lock, flags); 1544 1545 return QLA_FUNCTION_FAILED; 1546 } 1547 1548 /** 1549 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP 1550 * @sp: command to send to the ISP 1551 * 1552 * Returns non-zero if a failure occurred, else zero. 1553 */ 1554 int 1555 qla24xx_dif_start_scsi(srb_t *sp) 1556 { 1557 int nseg; 1558 unsigned long flags; 1559 uint32_t *clr_ptr; 1560 uint32_t index; 1561 uint32_t handle; 1562 uint16_t cnt; 1563 uint16_t req_cnt = 0; 1564 uint16_t tot_dsds; 1565 uint16_t tot_prot_dsds; 1566 uint16_t fw_prot_opts = 0; 1567 struct req_que *req = NULL; 1568 struct rsp_que *rsp = NULL; 1569 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 1570 struct scsi_qla_host *vha = sp->vha; 1571 struct qla_hw_data *ha = vha->hw; 1572 struct cmd_type_crc_2 *cmd_pkt; 1573 uint32_t status = 0; 1574 1575 #define QDSS_GOT_Q_SPACE BIT_0 1576 1577 /* Only process protection or >16 cdb in this routine */ 1578 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) { 1579 if (cmd->cmd_len <= 16) 1580 return qla24xx_start_scsi(sp); 1581 } 1582 1583 /* Setup device pointers. */ 1584 req = vha->req; 1585 rsp = req->rsp; 1586 1587 /* So we know we haven't pci_map'ed anything yet */ 1588 tot_dsds = 0; 1589 1590 /* Send marker if required */ 1591 if (vha->marker_needed != 0) { 1592 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) != 1593 QLA_SUCCESS) 1594 return QLA_FUNCTION_FAILED; 1595 vha->marker_needed = 0; 1596 } 1597 1598 /* Acquire ring specific lock */ 1599 spin_lock_irqsave(&ha->hardware_lock, flags); 1600 1601 /* Check for room in outstanding command list. */ 1602 handle = req->current_outstanding_cmd; 1603 for (index = 1; index < req->num_outstanding_cmds; index++) { 1604 handle++; 1605 if (handle == req->num_outstanding_cmds) 1606 handle = 1; 1607 if (!req->outstanding_cmds[handle]) 1608 break; 1609 } 1610 1611 if (index == req->num_outstanding_cmds) 1612 goto queuing_error; 1613 1614 /* Compute number of required data segments */ 1615 /* Map the sg table so we have an accurate count of sg entries needed */ 1616 if (scsi_sg_count(cmd)) { 1617 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 1618 scsi_sg_count(cmd), cmd->sc_data_direction); 1619 if (unlikely(!nseg)) 1620 goto queuing_error; 1621 else 1622 sp->flags |= SRB_DMA_VALID; 1623 1624 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) || 1625 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) { 1626 struct qla2_sgx sgx; 1627 uint32_t partial; 1628 1629 memset(&sgx, 0, sizeof(struct qla2_sgx)); 1630 sgx.tot_bytes = scsi_bufflen(cmd); 1631 sgx.cur_sg = scsi_sglist(cmd); 1632 sgx.sp = sp; 1633 1634 nseg = 0; 1635 while (qla24xx_get_one_block_sg( 1636 cmd->device->sector_size, &sgx, &partial)) 1637 nseg++; 1638 } 1639 } else 1640 nseg = 0; 1641 1642 /* number of required data segments */ 1643 tot_dsds = nseg; 1644 1645 /* Compute number of required protection segments */ 1646 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) { 1647 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd), 1648 scsi_prot_sg_count(cmd), cmd->sc_data_direction); 1649 if (unlikely(!nseg)) 1650 goto queuing_error; 1651 else 1652 sp->flags |= SRB_CRC_PROT_DMA_VALID; 1653 1654 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) || 1655 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) { 1656 nseg = scsi_bufflen(cmd) / cmd->device->sector_size; 1657 } 1658 } else { 1659 nseg = 0; 1660 } 1661 1662 req_cnt = 1; 1663 /* Total Data and protection sg segment(s) */ 1664 tot_prot_dsds = nseg; 1665 tot_dsds += nseg; 1666 if (req->cnt < (req_cnt + 2)) { 1667 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr : 1668 RD_REG_DWORD_RELAXED(req->req_q_out); 1669 if (req->ring_index < cnt) 1670 req->cnt = cnt - req->ring_index; 1671 else 1672 req->cnt = req->length - 1673 (req->ring_index - cnt); 1674 if (req->cnt < (req_cnt + 2)) 1675 goto queuing_error; 1676 } 1677 1678 status |= QDSS_GOT_Q_SPACE; 1679 1680 /* Build header part of command packet (excluding the OPCODE). */ 1681 req->current_outstanding_cmd = handle; 1682 req->outstanding_cmds[handle] = sp; 1683 sp->handle = handle; 1684 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 1685 req->cnt -= req_cnt; 1686 1687 /* Fill-in common area */ 1688 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr; 1689 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 1690 1691 clr_ptr = (uint32_t *)cmd_pkt + 2; 1692 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 1693 1694 /* Set NPORT-ID and LUN number*/ 1695 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 1696 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 1697 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 1698 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 1699 1700 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 1701 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); 1702 1703 /* Total Data and protection segment(s) */ 1704 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 1705 1706 /* Build IOCB segments and adjust for data protection segments */ 1707 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *) 1708 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) != 1709 QLA_SUCCESS) 1710 goto queuing_error; 1711 1712 cmd_pkt->entry_count = (uint8_t)req_cnt; 1713 /* Specify response queue number where completion should happen */ 1714 cmd_pkt->entry_status = (uint8_t) rsp->id; 1715 cmd_pkt->timeout = cpu_to_le16(0); 1716 wmb(); 1717 1718 /* Adjust ring index. */ 1719 req->ring_index++; 1720 if (req->ring_index == req->length) { 1721 req->ring_index = 0; 1722 req->ring_ptr = req->ring; 1723 } else 1724 req->ring_ptr++; 1725 1726 /* Set chip new ring index. */ 1727 WRT_REG_DWORD(req->req_q_in, req->ring_index); 1728 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr); 1729 1730 /* Manage unprocessed RIO/ZIO commands in response queue. */ 1731 if (vha->flags.process_response_queue && 1732 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 1733 qla24xx_process_response_queue(vha, rsp); 1734 1735 spin_unlock_irqrestore(&ha->hardware_lock, flags); 1736 1737 return QLA_SUCCESS; 1738 1739 queuing_error: 1740 if (status & QDSS_GOT_Q_SPACE) { 1741 req->outstanding_cmds[handle] = NULL; 1742 req->cnt += req_cnt; 1743 } 1744 /* Cleanup will be performed by the caller (queuecommand) */ 1745 1746 spin_unlock_irqrestore(&ha->hardware_lock, flags); 1747 return QLA_FUNCTION_FAILED; 1748 } 1749 1750 /** 1751 * qla2xxx_start_scsi_mq() - Send a SCSI command to the ISP 1752 * @sp: command to send to the ISP 1753 * 1754 * Returns non-zero if a failure occurred, else zero. 1755 */ 1756 static int 1757 qla2xxx_start_scsi_mq(srb_t *sp) 1758 { 1759 int nseg; 1760 unsigned long flags; 1761 uint32_t *clr_ptr; 1762 uint32_t index; 1763 uint32_t handle; 1764 struct cmd_type_7 *cmd_pkt; 1765 uint16_t cnt; 1766 uint16_t req_cnt; 1767 uint16_t tot_dsds; 1768 struct req_que *req = NULL; 1769 struct rsp_que *rsp = NULL; 1770 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 1771 struct scsi_qla_host *vha = sp->fcport->vha; 1772 struct qla_hw_data *ha = vha->hw; 1773 struct qla_qpair *qpair = sp->qpair; 1774 1775 /* Acquire qpair specific lock */ 1776 spin_lock_irqsave(&qpair->qp_lock, flags); 1777 1778 /* Setup qpair pointers */ 1779 rsp = qpair->rsp; 1780 req = qpair->req; 1781 1782 /* So we know we haven't pci_map'ed anything yet */ 1783 tot_dsds = 0; 1784 1785 /* Send marker if required */ 1786 if (vha->marker_needed != 0) { 1787 if (__qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) != 1788 QLA_SUCCESS) { 1789 spin_unlock_irqrestore(&qpair->qp_lock, flags); 1790 return QLA_FUNCTION_FAILED; 1791 } 1792 vha->marker_needed = 0; 1793 } 1794 1795 /* Check for room in outstanding command list. */ 1796 handle = req->current_outstanding_cmd; 1797 for (index = 1; index < req->num_outstanding_cmds; index++) { 1798 handle++; 1799 if (handle == req->num_outstanding_cmds) 1800 handle = 1; 1801 if (!req->outstanding_cmds[handle]) 1802 break; 1803 } 1804 if (index == req->num_outstanding_cmds) 1805 goto queuing_error; 1806 1807 /* Map the sg table so we have an accurate count of sg entries needed */ 1808 if (scsi_sg_count(cmd)) { 1809 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 1810 scsi_sg_count(cmd), cmd->sc_data_direction); 1811 if (unlikely(!nseg)) 1812 goto queuing_error; 1813 } else 1814 nseg = 0; 1815 1816 tot_dsds = nseg; 1817 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds); 1818 if (req->cnt < (req_cnt + 2)) { 1819 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr : 1820 RD_REG_DWORD_RELAXED(req->req_q_out); 1821 if (req->ring_index < cnt) 1822 req->cnt = cnt - req->ring_index; 1823 else 1824 req->cnt = req->length - 1825 (req->ring_index - cnt); 1826 if (req->cnt < (req_cnt + 2)) 1827 goto queuing_error; 1828 } 1829 1830 /* Build command packet. */ 1831 req->current_outstanding_cmd = handle; 1832 req->outstanding_cmds[handle] = sp; 1833 sp->handle = handle; 1834 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 1835 req->cnt -= req_cnt; 1836 1837 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr; 1838 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 1839 1840 /* Zero out remaining portion of packet. */ 1841 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */ 1842 clr_ptr = (uint32_t *)cmd_pkt + 2; 1843 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 1844 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 1845 1846 /* Set NPORT-ID and LUN number*/ 1847 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 1848 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 1849 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 1850 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 1851 cmd_pkt->vp_index = sp->fcport->vha->vp_idx; 1852 1853 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 1854 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); 1855 1856 cmd_pkt->task = TSK_SIMPLE; 1857 1858 /* Load SCSI command packet. */ 1859 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); 1860 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); 1861 1862 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); 1863 1864 /* Build IOCB segments */ 1865 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req); 1866 1867 /* Set total data segment count. */ 1868 cmd_pkt->entry_count = (uint8_t)req_cnt; 1869 wmb(); 1870 /* Adjust ring index. */ 1871 req->ring_index++; 1872 if (req->ring_index == req->length) { 1873 req->ring_index = 0; 1874 req->ring_ptr = req->ring; 1875 } else 1876 req->ring_ptr++; 1877 1878 sp->flags |= SRB_DMA_VALID; 1879 1880 /* Set chip new ring index. */ 1881 WRT_REG_DWORD(req->req_q_in, req->ring_index); 1882 1883 /* Manage unprocessed RIO/ZIO commands in response queue. */ 1884 if (vha->flags.process_response_queue && 1885 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 1886 qla24xx_process_response_queue(vha, rsp); 1887 1888 spin_unlock_irqrestore(&qpair->qp_lock, flags); 1889 return QLA_SUCCESS; 1890 1891 queuing_error: 1892 if (tot_dsds) 1893 scsi_dma_unmap(cmd); 1894 1895 spin_unlock_irqrestore(&qpair->qp_lock, flags); 1896 1897 return QLA_FUNCTION_FAILED; 1898 } 1899 1900 1901 /** 1902 * qla2xxx_dif_start_scsi_mq() - Send a SCSI command to the ISP 1903 * @sp: command to send to the ISP 1904 * 1905 * Returns non-zero if a failure occurred, else zero. 1906 */ 1907 int 1908 qla2xxx_dif_start_scsi_mq(srb_t *sp) 1909 { 1910 int nseg; 1911 unsigned long flags; 1912 uint32_t *clr_ptr; 1913 uint32_t index; 1914 uint32_t handle; 1915 uint16_t cnt; 1916 uint16_t req_cnt = 0; 1917 uint16_t tot_dsds; 1918 uint16_t tot_prot_dsds; 1919 uint16_t fw_prot_opts = 0; 1920 struct req_que *req = NULL; 1921 struct rsp_que *rsp = NULL; 1922 struct scsi_cmnd *cmd = GET_CMD_SP(sp); 1923 struct scsi_qla_host *vha = sp->fcport->vha; 1924 struct qla_hw_data *ha = vha->hw; 1925 struct cmd_type_crc_2 *cmd_pkt; 1926 uint32_t status = 0; 1927 struct qla_qpair *qpair = sp->qpair; 1928 1929 #define QDSS_GOT_Q_SPACE BIT_0 1930 1931 /* Check for host side state */ 1932 if (!qpair->online) { 1933 cmd->result = DID_NO_CONNECT << 16; 1934 return QLA_INTERFACE_ERROR; 1935 } 1936 1937 if (!qpair->difdix_supported && 1938 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) { 1939 cmd->result = DID_NO_CONNECT << 16; 1940 return QLA_INTERFACE_ERROR; 1941 } 1942 1943 /* Only process protection or >16 cdb in this routine */ 1944 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) { 1945 if (cmd->cmd_len <= 16) 1946 return qla2xxx_start_scsi_mq(sp); 1947 } 1948 1949 spin_lock_irqsave(&qpair->qp_lock, flags); 1950 1951 /* Setup qpair pointers */ 1952 rsp = qpair->rsp; 1953 req = qpair->req; 1954 1955 /* So we know we haven't pci_map'ed anything yet */ 1956 tot_dsds = 0; 1957 1958 /* Send marker if required */ 1959 if (vha->marker_needed != 0) { 1960 if (__qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) != 1961 QLA_SUCCESS) { 1962 spin_unlock_irqrestore(&qpair->qp_lock, flags); 1963 return QLA_FUNCTION_FAILED; 1964 } 1965 vha->marker_needed = 0; 1966 } 1967 1968 /* Check for room in outstanding command list. */ 1969 handle = req->current_outstanding_cmd; 1970 for (index = 1; index < req->num_outstanding_cmds; index++) { 1971 handle++; 1972 if (handle == req->num_outstanding_cmds) 1973 handle = 1; 1974 if (!req->outstanding_cmds[handle]) 1975 break; 1976 } 1977 1978 if (index == req->num_outstanding_cmds) 1979 goto queuing_error; 1980 1981 /* Compute number of required data segments */ 1982 /* Map the sg table so we have an accurate count of sg entries needed */ 1983 if (scsi_sg_count(cmd)) { 1984 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 1985 scsi_sg_count(cmd), cmd->sc_data_direction); 1986 if (unlikely(!nseg)) 1987 goto queuing_error; 1988 else 1989 sp->flags |= SRB_DMA_VALID; 1990 1991 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) || 1992 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) { 1993 struct qla2_sgx sgx; 1994 uint32_t partial; 1995 1996 memset(&sgx, 0, sizeof(struct qla2_sgx)); 1997 sgx.tot_bytes = scsi_bufflen(cmd); 1998 sgx.cur_sg = scsi_sglist(cmd); 1999 sgx.sp = sp; 2000 2001 nseg = 0; 2002 while (qla24xx_get_one_block_sg( 2003 cmd->device->sector_size, &sgx, &partial)) 2004 nseg++; 2005 } 2006 } else 2007 nseg = 0; 2008 2009 /* number of required data segments */ 2010 tot_dsds = nseg; 2011 2012 /* Compute number of required protection segments */ 2013 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) { 2014 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd), 2015 scsi_prot_sg_count(cmd), cmd->sc_data_direction); 2016 if (unlikely(!nseg)) 2017 goto queuing_error; 2018 else 2019 sp->flags |= SRB_CRC_PROT_DMA_VALID; 2020 2021 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) || 2022 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) { 2023 nseg = scsi_bufflen(cmd) / cmd->device->sector_size; 2024 } 2025 } else { 2026 nseg = 0; 2027 } 2028 2029 req_cnt = 1; 2030 /* Total Data and protection sg segment(s) */ 2031 tot_prot_dsds = nseg; 2032 tot_dsds += nseg; 2033 if (req->cnt < (req_cnt + 2)) { 2034 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr : 2035 RD_REG_DWORD_RELAXED(req->req_q_out); 2036 if (req->ring_index < cnt) 2037 req->cnt = cnt - req->ring_index; 2038 else 2039 req->cnt = req->length - 2040 (req->ring_index - cnt); 2041 if (req->cnt < (req_cnt + 2)) 2042 goto queuing_error; 2043 } 2044 2045 status |= QDSS_GOT_Q_SPACE; 2046 2047 /* Build header part of command packet (excluding the OPCODE). */ 2048 req->current_outstanding_cmd = handle; 2049 req->outstanding_cmds[handle] = sp; 2050 sp->handle = handle; 2051 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 2052 req->cnt -= req_cnt; 2053 2054 /* Fill-in common area */ 2055 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr; 2056 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 2057 2058 clr_ptr = (uint32_t *)cmd_pkt + 2; 2059 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 2060 2061 /* Set NPORT-ID and LUN number*/ 2062 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2063 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 2064 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 2065 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 2066 2067 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 2068 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); 2069 2070 /* Total Data and protection segment(s) */ 2071 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 2072 2073 /* Build IOCB segments and adjust for data protection segments */ 2074 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *) 2075 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) != 2076 QLA_SUCCESS) 2077 goto queuing_error; 2078 2079 cmd_pkt->entry_count = (uint8_t)req_cnt; 2080 cmd_pkt->timeout = cpu_to_le16(0); 2081 wmb(); 2082 2083 /* Adjust ring index. */ 2084 req->ring_index++; 2085 if (req->ring_index == req->length) { 2086 req->ring_index = 0; 2087 req->ring_ptr = req->ring; 2088 } else 2089 req->ring_ptr++; 2090 2091 /* Set chip new ring index. */ 2092 WRT_REG_DWORD(req->req_q_in, req->ring_index); 2093 2094 /* Manage unprocessed RIO/ZIO commands in response queue. */ 2095 if (vha->flags.process_response_queue && 2096 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 2097 qla24xx_process_response_queue(vha, rsp); 2098 2099 spin_unlock_irqrestore(&qpair->qp_lock, flags); 2100 2101 return QLA_SUCCESS; 2102 2103 queuing_error: 2104 if (status & QDSS_GOT_Q_SPACE) { 2105 req->outstanding_cmds[handle] = NULL; 2106 req->cnt += req_cnt; 2107 } 2108 /* Cleanup will be performed by the caller (queuecommand) */ 2109 2110 spin_unlock_irqrestore(&qpair->qp_lock, flags); 2111 return QLA_FUNCTION_FAILED; 2112 } 2113 2114 /* Generic Control-SRB manipulation functions. */ 2115 2116 /* hardware_lock assumed to be held. */ 2117 2118 void * 2119 __qla2x00_alloc_iocbs(struct qla_qpair *qpair, srb_t *sp) 2120 { 2121 scsi_qla_host_t *vha = qpair->vha; 2122 struct qla_hw_data *ha = vha->hw; 2123 struct req_que *req = qpair->req; 2124 device_reg_t *reg = ISP_QUE_REG(ha, req->id); 2125 uint32_t index, handle; 2126 request_t *pkt; 2127 uint16_t cnt, req_cnt; 2128 2129 pkt = NULL; 2130 req_cnt = 1; 2131 handle = 0; 2132 2133 if (sp && (sp->type != SRB_SCSI_CMD)) { 2134 /* Adjust entry-counts as needed. */ 2135 req_cnt = sp->iocbs; 2136 } 2137 2138 /* Check for room on request queue. */ 2139 if (req->cnt < req_cnt + 2) { 2140 if (qpair->use_shadow_reg) 2141 cnt = *req->out_ptr; 2142 else if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) 2143 cnt = RD_REG_DWORD(®->isp25mq.req_q_out); 2144 else if (IS_P3P_TYPE(ha)) 2145 cnt = RD_REG_DWORD(®->isp82.req_q_out); 2146 else if (IS_FWI2_CAPABLE(ha)) 2147 cnt = RD_REG_DWORD(®->isp24.req_q_out); 2148 else if (IS_QLAFX00(ha)) 2149 cnt = RD_REG_DWORD(®->ispfx00.req_q_out); 2150 else 2151 cnt = qla2x00_debounce_register( 2152 ISP_REQ_Q_OUT(ha, ®->isp)); 2153 2154 if (req->ring_index < cnt) 2155 req->cnt = cnt - req->ring_index; 2156 else 2157 req->cnt = req->length - 2158 (req->ring_index - cnt); 2159 } 2160 if (req->cnt < req_cnt + 2) 2161 goto queuing_error; 2162 2163 if (sp) { 2164 /* Check for room in outstanding command list. */ 2165 handle = req->current_outstanding_cmd; 2166 for (index = 1; index < req->num_outstanding_cmds; index++) { 2167 handle++; 2168 if (handle == req->num_outstanding_cmds) 2169 handle = 1; 2170 if (!req->outstanding_cmds[handle]) 2171 break; 2172 } 2173 if (index == req->num_outstanding_cmds) { 2174 ql_log(ql_log_warn, vha, 0x700b, 2175 "No room on outstanding cmd array.\n"); 2176 goto queuing_error; 2177 } 2178 2179 /* Prep command array. */ 2180 req->current_outstanding_cmd = handle; 2181 req->outstanding_cmds[handle] = sp; 2182 sp->handle = handle; 2183 } 2184 2185 /* Prep packet */ 2186 req->cnt -= req_cnt; 2187 pkt = req->ring_ptr; 2188 memset(pkt, 0, REQUEST_ENTRY_SIZE); 2189 if (IS_QLAFX00(ha)) { 2190 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt); 2191 WRT_REG_WORD((void __iomem *)&pkt->handle, handle); 2192 } else { 2193 pkt->entry_count = req_cnt; 2194 pkt->handle = handle; 2195 } 2196 2197 return pkt; 2198 2199 queuing_error: 2200 qpair->tgt_counters.num_alloc_iocb_failed++; 2201 return pkt; 2202 } 2203 2204 void * 2205 qla2x00_alloc_iocbs_ready(struct qla_qpair *qpair, srb_t *sp) 2206 { 2207 scsi_qla_host_t *vha = qpair->vha; 2208 2209 if (qla2x00_reset_active(vha)) 2210 return NULL; 2211 2212 return __qla2x00_alloc_iocbs(qpair, sp); 2213 } 2214 2215 void * 2216 qla2x00_alloc_iocbs(struct scsi_qla_host *vha, srb_t *sp) 2217 { 2218 return __qla2x00_alloc_iocbs(vha->hw->base_qpair, sp); 2219 } 2220 2221 static void 2222 qla24xx_prli_iocb(srb_t *sp, struct logio_entry_24xx *logio) 2223 { 2224 struct srb_iocb *lio = &sp->u.iocb_cmd; 2225 2226 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE; 2227 logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI); 2228 if (lio->u.logio.flags & SRB_LOGIN_NVME_PRLI) 2229 logio->control_flags |= LCF_NVME_PRLI; 2230 2231 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2232 logio->port_id[0] = sp->fcport->d_id.b.al_pa; 2233 logio->port_id[1] = sp->fcport->d_id.b.area; 2234 logio->port_id[2] = sp->fcport->d_id.b.domain; 2235 logio->vp_index = sp->vha->vp_idx; 2236 } 2237 2238 static void 2239 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio) 2240 { 2241 struct srb_iocb *lio = &sp->u.iocb_cmd; 2242 2243 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE; 2244 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI); 2245 2246 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI) 2247 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI); 2248 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI) 2249 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI); 2250 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2251 logio->port_id[0] = sp->fcport->d_id.b.al_pa; 2252 logio->port_id[1] = sp->fcport->d_id.b.area; 2253 logio->port_id[2] = sp->fcport->d_id.b.domain; 2254 logio->vp_index = sp->vha->vp_idx; 2255 } 2256 2257 static void 2258 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx) 2259 { 2260 struct qla_hw_data *ha = sp->vha->hw; 2261 struct srb_iocb *lio = &sp->u.iocb_cmd; 2262 uint16_t opts; 2263 2264 mbx->entry_type = MBX_IOCB_TYPE; 2265 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id); 2266 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT); 2267 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0; 2268 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0; 2269 if (HAS_EXTENDED_IDS(ha)) { 2270 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id); 2271 mbx->mb10 = cpu_to_le16(opts); 2272 } else { 2273 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts); 2274 } 2275 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain); 2276 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 | 2277 sp->fcport->d_id.b.al_pa); 2278 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx); 2279 } 2280 2281 static void 2282 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio) 2283 { 2284 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE; 2285 logio->control_flags = 2286 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO); 2287 if (!sp->fcport->se_sess || 2288 !sp->fcport->keep_nport_handle) 2289 logio->control_flags |= cpu_to_le16(LCF_FREE_NPORT); 2290 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2291 logio->port_id[0] = sp->fcport->d_id.b.al_pa; 2292 logio->port_id[1] = sp->fcport->d_id.b.area; 2293 logio->port_id[2] = sp->fcport->d_id.b.domain; 2294 logio->vp_index = sp->vha->vp_idx; 2295 } 2296 2297 static void 2298 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx) 2299 { 2300 struct qla_hw_data *ha = sp->vha->hw; 2301 2302 mbx->entry_type = MBX_IOCB_TYPE; 2303 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id); 2304 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT); 2305 mbx->mb1 = HAS_EXTENDED_IDS(ha) ? 2306 cpu_to_le16(sp->fcport->loop_id): 2307 cpu_to_le16(sp->fcport->loop_id << 8); 2308 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain); 2309 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 | 2310 sp->fcport->d_id.b.al_pa); 2311 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx); 2312 /* Implicit: mbx->mbx10 = 0. */ 2313 } 2314 2315 static void 2316 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio) 2317 { 2318 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE; 2319 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC); 2320 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2321 logio->vp_index = sp->vha->vp_idx; 2322 } 2323 2324 static void 2325 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx) 2326 { 2327 struct qla_hw_data *ha = sp->vha->hw; 2328 2329 mbx->entry_type = MBX_IOCB_TYPE; 2330 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id); 2331 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE); 2332 if (HAS_EXTENDED_IDS(ha)) { 2333 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id); 2334 mbx->mb10 = cpu_to_le16(BIT_0); 2335 } else { 2336 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0); 2337 } 2338 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma)); 2339 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma)); 2340 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma))); 2341 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma))); 2342 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx); 2343 } 2344 2345 static void 2346 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk) 2347 { 2348 uint32_t flags; 2349 uint64_t lun; 2350 struct fc_port *fcport = sp->fcport; 2351 scsi_qla_host_t *vha = fcport->vha; 2352 struct qla_hw_data *ha = vha->hw; 2353 struct srb_iocb *iocb = &sp->u.iocb_cmd; 2354 struct req_que *req = vha->req; 2355 2356 flags = iocb->u.tmf.flags; 2357 lun = iocb->u.tmf.lun; 2358 2359 tsk->entry_type = TSK_MGMT_IOCB_TYPE; 2360 tsk->entry_count = 1; 2361 tsk->handle = MAKE_HANDLE(req->id, tsk->handle); 2362 tsk->nport_handle = cpu_to_le16(fcport->loop_id); 2363 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2); 2364 tsk->control_flags = cpu_to_le32(flags); 2365 tsk->port_id[0] = fcport->d_id.b.al_pa; 2366 tsk->port_id[1] = fcport->d_id.b.area; 2367 tsk->port_id[2] = fcport->d_id.b.domain; 2368 tsk->vp_index = fcport->vha->vp_idx; 2369 2370 if (flags == TCF_LUN_RESET) { 2371 int_to_scsilun(lun, &tsk->lun); 2372 host_to_fcp_swap((uint8_t *)&tsk->lun, 2373 sizeof(tsk->lun)); 2374 } 2375 } 2376 2377 static void 2378 qla2x00_els_dcmd_sp_free(void *data) 2379 { 2380 srb_t *sp = data; 2381 struct srb_iocb *elsio = &sp->u.iocb_cmd; 2382 2383 kfree(sp->fcport); 2384 2385 if (elsio->u.els_logo.els_logo_pyld) 2386 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE, 2387 elsio->u.els_logo.els_logo_pyld, 2388 elsio->u.els_logo.els_logo_pyld_dma); 2389 2390 del_timer(&elsio->timer); 2391 qla2x00_rel_sp(sp); 2392 } 2393 2394 static void 2395 qla2x00_els_dcmd_iocb_timeout(void *data) 2396 { 2397 srb_t *sp = data; 2398 fc_port_t *fcport = sp->fcport; 2399 struct scsi_qla_host *vha = sp->vha; 2400 struct srb_iocb *lio = &sp->u.iocb_cmd; 2401 2402 ql_dbg(ql_dbg_io, vha, 0x3069, 2403 "%s Timeout, hdl=%x, portid=%02x%02x%02x\n", 2404 sp->name, sp->handle, fcport->d_id.b.domain, fcport->d_id.b.area, 2405 fcport->d_id.b.al_pa); 2406 2407 complete(&lio->u.els_logo.comp); 2408 } 2409 2410 static void 2411 qla2x00_els_dcmd_sp_done(void *ptr, int res) 2412 { 2413 srb_t *sp = ptr; 2414 fc_port_t *fcport = sp->fcport; 2415 struct srb_iocb *lio = &sp->u.iocb_cmd; 2416 struct scsi_qla_host *vha = sp->vha; 2417 2418 ql_dbg(ql_dbg_io, vha, 0x3072, 2419 "%s hdl=%x, portid=%02x%02x%02x done\n", 2420 sp->name, sp->handle, fcport->d_id.b.domain, 2421 fcport->d_id.b.area, fcport->d_id.b.al_pa); 2422 2423 complete(&lio->u.els_logo.comp); 2424 } 2425 2426 int 2427 qla24xx_els_dcmd_iocb(scsi_qla_host_t *vha, int els_opcode, 2428 port_id_t remote_did) 2429 { 2430 srb_t *sp; 2431 fc_port_t *fcport = NULL; 2432 struct srb_iocb *elsio = NULL; 2433 struct qla_hw_data *ha = vha->hw; 2434 struct els_logo_payload logo_pyld; 2435 int rval = QLA_SUCCESS; 2436 2437 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL); 2438 if (!fcport) { 2439 ql_log(ql_log_info, vha, 0x70e5, "fcport allocation failed\n"); 2440 return -ENOMEM; 2441 } 2442 2443 /* Alloc SRB structure */ 2444 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL); 2445 if (!sp) { 2446 kfree(fcport); 2447 ql_log(ql_log_info, vha, 0x70e6, 2448 "SRB allocation failed\n"); 2449 return -ENOMEM; 2450 } 2451 2452 elsio = &sp->u.iocb_cmd; 2453 fcport->loop_id = 0xFFFF; 2454 fcport->d_id.b.domain = remote_did.b.domain; 2455 fcport->d_id.b.area = remote_did.b.area; 2456 fcport->d_id.b.al_pa = remote_did.b.al_pa; 2457 2458 ql_dbg(ql_dbg_io, vha, 0x3073, "portid=%02x%02x%02x done\n", 2459 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa); 2460 2461 sp->type = SRB_ELS_DCMD; 2462 sp->name = "ELS_DCMD"; 2463 sp->fcport = fcport; 2464 elsio->timeout = qla2x00_els_dcmd_iocb_timeout; 2465 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT); 2466 sp->done = qla2x00_els_dcmd_sp_done; 2467 sp->free = qla2x00_els_dcmd_sp_free; 2468 2469 elsio->u.els_logo.els_logo_pyld = dma_alloc_coherent(&ha->pdev->dev, 2470 DMA_POOL_SIZE, &elsio->u.els_logo.els_logo_pyld_dma, 2471 GFP_KERNEL); 2472 2473 if (!elsio->u.els_logo.els_logo_pyld) { 2474 sp->free(sp); 2475 return QLA_FUNCTION_FAILED; 2476 } 2477 2478 memset(&logo_pyld, 0, sizeof(struct els_logo_payload)); 2479 2480 elsio->u.els_logo.els_cmd = els_opcode; 2481 logo_pyld.opcode = els_opcode; 2482 logo_pyld.s_id[0] = vha->d_id.b.al_pa; 2483 logo_pyld.s_id[1] = vha->d_id.b.area; 2484 logo_pyld.s_id[2] = vha->d_id.b.domain; 2485 host_to_fcp_swap(logo_pyld.s_id, sizeof(uint32_t)); 2486 memcpy(&logo_pyld.wwpn, vha->port_name, WWN_SIZE); 2487 2488 memcpy(elsio->u.els_logo.els_logo_pyld, &logo_pyld, 2489 sizeof(struct els_logo_payload)); 2490 2491 rval = qla2x00_start_sp(sp); 2492 if (rval != QLA_SUCCESS) { 2493 sp->free(sp); 2494 return QLA_FUNCTION_FAILED; 2495 } 2496 2497 ql_dbg(ql_dbg_io, vha, 0x3074, 2498 "%s LOGO sent, hdl=%x, loopid=%x, portid=%02x%02x%02x.\n", 2499 sp->name, sp->handle, fcport->loop_id, fcport->d_id.b.domain, 2500 fcport->d_id.b.area, fcport->d_id.b.al_pa); 2501 2502 wait_for_completion(&elsio->u.els_logo.comp); 2503 2504 sp->free(sp); 2505 return rval; 2506 } 2507 2508 static void 2509 qla24xx_els_logo_iocb(srb_t *sp, struct els_entry_24xx *els_iocb) 2510 { 2511 scsi_qla_host_t *vha = sp->vha; 2512 struct srb_iocb *elsio = &sp->u.iocb_cmd; 2513 uint32_t dsd_len = 24; 2514 2515 els_iocb->entry_type = ELS_IOCB_TYPE; 2516 els_iocb->entry_count = 1; 2517 els_iocb->sys_define = 0; 2518 els_iocb->entry_status = 0; 2519 els_iocb->handle = sp->handle; 2520 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2521 els_iocb->tx_dsd_count = 1; 2522 els_iocb->vp_index = vha->vp_idx; 2523 els_iocb->sof_type = EST_SOFI3; 2524 els_iocb->rx_dsd_count = 0; 2525 els_iocb->opcode = elsio->u.els_logo.els_cmd; 2526 2527 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa; 2528 els_iocb->port_id[1] = sp->fcport->d_id.b.area; 2529 els_iocb->port_id[2] = sp->fcport->d_id.b.domain; 2530 els_iocb->s_id[0] = vha->d_id.b.al_pa; 2531 els_iocb->s_id[1] = vha->d_id.b.area; 2532 els_iocb->s_id[2] = vha->d_id.b.domain; 2533 els_iocb->control_flags = 0; 2534 2535 if (elsio->u.els_logo.els_cmd == ELS_DCMD_PLOGI) { 2536 els_iocb->tx_byte_count = sizeof(struct els_plogi_payload); 2537 els_iocb->tx_address[0] = 2538 cpu_to_le32(LSD(elsio->u.els_plogi.els_plogi_pyld_dma)); 2539 els_iocb->tx_address[1] = 2540 cpu_to_le32(MSD(elsio->u.els_plogi.els_plogi_pyld_dma)); 2541 els_iocb->tx_len = dsd_len; 2542 2543 els_iocb->rx_dsd_count = 1; 2544 els_iocb->rx_byte_count = sizeof(struct els_plogi_payload); 2545 els_iocb->rx_address[0] = 2546 cpu_to_le32(LSD(elsio->u.els_plogi.els_resp_pyld_dma)); 2547 els_iocb->rx_address[1] = 2548 cpu_to_le32(MSD(elsio->u.els_plogi.els_resp_pyld_dma)); 2549 els_iocb->rx_len = dsd_len; 2550 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073, 2551 "PLOGI ELS IOCB:\n"); 2552 ql_dump_buffer(ql_log_info, vha, 0x0109, 2553 (uint8_t *)els_iocb, 0x70); 2554 } else { 2555 els_iocb->tx_byte_count = sizeof(struct els_logo_payload); 2556 els_iocb->tx_address[0] = 2557 cpu_to_le32(LSD(elsio->u.els_logo.els_logo_pyld_dma)); 2558 els_iocb->tx_address[1] = 2559 cpu_to_le32(MSD(elsio->u.els_logo.els_logo_pyld_dma)); 2560 els_iocb->tx_len = cpu_to_le32(sizeof(struct els_logo_payload)); 2561 2562 els_iocb->rx_byte_count = 0; 2563 els_iocb->rx_address[0] = 0; 2564 els_iocb->rx_address[1] = 0; 2565 els_iocb->rx_len = 0; 2566 } 2567 2568 sp->vha->qla_stats.control_requests++; 2569 } 2570 2571 static void 2572 qla2x00_els_dcmd2_sp_free(void *data) 2573 { 2574 srb_t *sp = data; 2575 struct srb_iocb *elsio = &sp->u.iocb_cmd; 2576 2577 if (elsio->u.els_plogi.els_plogi_pyld) 2578 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE, 2579 elsio->u.els_plogi.els_plogi_pyld, 2580 elsio->u.els_plogi.els_plogi_pyld_dma); 2581 2582 if (elsio->u.els_plogi.els_resp_pyld) 2583 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE, 2584 elsio->u.els_plogi.els_resp_pyld, 2585 elsio->u.els_plogi.els_resp_pyld_dma); 2586 2587 del_timer(&elsio->timer); 2588 qla2x00_rel_sp(sp); 2589 } 2590 2591 static void 2592 qla2x00_els_dcmd2_iocb_timeout(void *data) 2593 { 2594 srb_t *sp = data; 2595 fc_port_t *fcport = sp->fcport; 2596 struct scsi_qla_host *vha = sp->vha; 2597 struct qla_hw_data *ha = vha->hw; 2598 struct srb_iocb *lio = &sp->u.iocb_cmd; 2599 unsigned long flags = 0; 2600 int res; 2601 2602 ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3069, 2603 "%s hdl=%x ELS Timeout, %8phC portid=%06x\n", 2604 sp->name, sp->handle, fcport->port_name, fcport->d_id.b24); 2605 2606 /* Abort the exchange */ 2607 spin_lock_irqsave(&ha->hardware_lock, flags); 2608 res = ha->isp_ops->abort_command(sp); 2609 ql_dbg(ql_dbg_io, vha, 0x3070, 2610 "mbx abort_command %s\n", 2611 (res == QLA_SUCCESS) ? "successful" : "failed"); 2612 spin_unlock_irqrestore(&ha->hardware_lock, flags); 2613 2614 complete(&lio->u.els_plogi.comp); 2615 } 2616 2617 static void 2618 qla2x00_els_dcmd2_sp_done(void *ptr, int res) 2619 { 2620 srb_t *sp = ptr; 2621 fc_port_t *fcport = sp->fcport; 2622 struct srb_iocb *lio = &sp->u.iocb_cmd; 2623 struct scsi_qla_host *vha = sp->vha; 2624 2625 ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3072, 2626 "%s ELS hdl=%x, portid=%06x done %8phC\n", 2627 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name); 2628 2629 complete(&lio->u.els_plogi.comp); 2630 } 2631 2632 int 2633 qla24xx_els_dcmd2_iocb(scsi_qla_host_t *vha, int els_opcode, 2634 fc_port_t *fcport, port_id_t remote_did) 2635 { 2636 srb_t *sp; 2637 struct srb_iocb *elsio = NULL; 2638 struct qla_hw_data *ha = vha->hw; 2639 int rval = QLA_SUCCESS; 2640 void *ptr, *resp_ptr; 2641 dma_addr_t ptr_dma; 2642 2643 /* Alloc SRB structure */ 2644 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL); 2645 if (!sp) { 2646 ql_log(ql_log_info, vha, 0x70e6, 2647 "SRB allocation failed\n"); 2648 return -ENOMEM; 2649 } 2650 2651 elsio = &sp->u.iocb_cmd; 2652 fcport->d_id.b.domain = remote_did.b.domain; 2653 fcport->d_id.b.area = remote_did.b.area; 2654 fcport->d_id.b.al_pa = remote_did.b.al_pa; 2655 2656 ql_dbg(ql_dbg_io, vha, 0x3073, 2657 "Enter: PLOGI portid=%06x\n", fcport->d_id.b24); 2658 2659 sp->type = SRB_ELS_DCMD; 2660 sp->name = "ELS_DCMD"; 2661 sp->fcport = fcport; 2662 2663 elsio->timeout = qla2x00_els_dcmd2_iocb_timeout; 2664 init_completion(&elsio->u.els_plogi.comp); 2665 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT); 2666 2667 sp->done = qla2x00_els_dcmd2_sp_done; 2668 sp->free = qla2x00_els_dcmd2_sp_free; 2669 2670 ptr = elsio->u.els_plogi.els_plogi_pyld = 2671 dma_alloc_coherent(&ha->pdev->dev, DMA_POOL_SIZE, 2672 &elsio->u.els_plogi.els_plogi_pyld_dma, GFP_KERNEL); 2673 ptr_dma = elsio->u.els_plogi.els_plogi_pyld_dma; 2674 2675 if (!elsio->u.els_plogi.els_plogi_pyld) { 2676 rval = QLA_FUNCTION_FAILED; 2677 goto out; 2678 } 2679 2680 resp_ptr = elsio->u.els_plogi.els_resp_pyld = 2681 dma_alloc_coherent(&ha->pdev->dev, DMA_POOL_SIZE, 2682 &elsio->u.els_plogi.els_resp_pyld_dma, GFP_KERNEL); 2683 2684 if (!elsio->u.els_plogi.els_resp_pyld) { 2685 rval = QLA_FUNCTION_FAILED; 2686 goto out; 2687 } 2688 2689 ql_dbg(ql_dbg_io, vha, 0x3073, "PLOGI %p %p\n", ptr, resp_ptr); 2690 2691 memset(ptr, 0, sizeof(struct els_plogi_payload)); 2692 memset(resp_ptr, 0, sizeof(struct els_plogi_payload)); 2693 elsio->u.els_plogi.els_cmd = els_opcode; 2694 elsio->u.els_plogi.els_plogi_pyld->opcode = els_opcode; 2695 qla24xx_get_port_login_templ(vha, ptr_dma + 4, 2696 &elsio->u.els_plogi.els_plogi_pyld->data[0], 2697 sizeof(struct els_plogi_payload)); 2698 2699 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073, "PLOGI buffer:\n"); 2700 ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x0109, 2701 (uint8_t *)elsio->u.els_plogi.els_plogi_pyld, 0x70); 2702 2703 rval = qla2x00_start_sp(sp); 2704 if (rval != QLA_SUCCESS) { 2705 rval = QLA_FUNCTION_FAILED; 2706 goto out; 2707 } 2708 2709 ql_dbg(ql_dbg_io, vha, 0x3074, 2710 "%s PLOGI sent, hdl=%x, loopid=%x, portid=%06x\n", 2711 sp->name, sp->handle, fcport->loop_id, fcport->d_id.b24); 2712 2713 wait_for_completion(&elsio->u.els_plogi.comp); 2714 2715 if (elsio->u.els_plogi.comp_status != CS_COMPLETE) 2716 rval = QLA_FUNCTION_FAILED; 2717 2718 out: 2719 sp->free(sp); 2720 return rval; 2721 } 2722 2723 static void 2724 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb) 2725 { 2726 struct bsg_job *bsg_job = sp->u.bsg_job; 2727 struct fc_bsg_request *bsg_request = bsg_job->request; 2728 2729 els_iocb->entry_type = ELS_IOCB_TYPE; 2730 els_iocb->entry_count = 1; 2731 els_iocb->sys_define = 0; 2732 els_iocb->entry_status = 0; 2733 els_iocb->handle = sp->handle; 2734 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2735 els_iocb->tx_dsd_count = cpu_to_le16(bsg_job->request_payload.sg_cnt); 2736 els_iocb->vp_index = sp->vha->vp_idx; 2737 els_iocb->sof_type = EST_SOFI3; 2738 els_iocb->rx_dsd_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt); 2739 2740 els_iocb->opcode = 2741 sp->type == SRB_ELS_CMD_RPT ? 2742 bsg_request->rqst_data.r_els.els_code : 2743 bsg_request->rqst_data.h_els.command_code; 2744 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa; 2745 els_iocb->port_id[1] = sp->fcport->d_id.b.area; 2746 els_iocb->port_id[2] = sp->fcport->d_id.b.domain; 2747 els_iocb->control_flags = 0; 2748 els_iocb->rx_byte_count = 2749 cpu_to_le32(bsg_job->reply_payload.payload_len); 2750 els_iocb->tx_byte_count = 2751 cpu_to_le32(bsg_job->request_payload.payload_len); 2752 2753 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address 2754 (bsg_job->request_payload.sg_list))); 2755 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address 2756 (bsg_job->request_payload.sg_list))); 2757 els_iocb->tx_len = cpu_to_le32(sg_dma_len 2758 (bsg_job->request_payload.sg_list)); 2759 2760 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address 2761 (bsg_job->reply_payload.sg_list))); 2762 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address 2763 (bsg_job->reply_payload.sg_list))); 2764 els_iocb->rx_len = cpu_to_le32(sg_dma_len 2765 (bsg_job->reply_payload.sg_list)); 2766 2767 sp->vha->qla_stats.control_requests++; 2768 } 2769 2770 static void 2771 qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb) 2772 { 2773 uint16_t avail_dsds; 2774 uint32_t *cur_dsd; 2775 struct scatterlist *sg; 2776 int index; 2777 uint16_t tot_dsds; 2778 scsi_qla_host_t *vha = sp->vha; 2779 struct qla_hw_data *ha = vha->hw; 2780 struct bsg_job *bsg_job = sp->u.bsg_job; 2781 int loop_iterartion = 0; 2782 int entry_count = 1; 2783 2784 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t)); 2785 ct_iocb->entry_type = CT_IOCB_TYPE; 2786 ct_iocb->entry_status = 0; 2787 ct_iocb->handle1 = sp->handle; 2788 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id); 2789 ct_iocb->status = cpu_to_le16(0); 2790 ct_iocb->control_flags = cpu_to_le16(0); 2791 ct_iocb->timeout = 0; 2792 ct_iocb->cmd_dsd_count = 2793 cpu_to_le16(bsg_job->request_payload.sg_cnt); 2794 ct_iocb->total_dsd_count = 2795 cpu_to_le16(bsg_job->request_payload.sg_cnt + 1); 2796 ct_iocb->req_bytecount = 2797 cpu_to_le32(bsg_job->request_payload.payload_len); 2798 ct_iocb->rsp_bytecount = 2799 cpu_to_le32(bsg_job->reply_payload.payload_len); 2800 2801 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address 2802 (bsg_job->request_payload.sg_list))); 2803 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address 2804 (bsg_job->request_payload.sg_list))); 2805 ct_iocb->dseg_req_length = ct_iocb->req_bytecount; 2806 2807 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address 2808 (bsg_job->reply_payload.sg_list))); 2809 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address 2810 (bsg_job->reply_payload.sg_list))); 2811 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount; 2812 2813 avail_dsds = 1; 2814 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address; 2815 index = 0; 2816 tot_dsds = bsg_job->reply_payload.sg_cnt; 2817 2818 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) { 2819 dma_addr_t sle_dma; 2820 cont_a64_entry_t *cont_pkt; 2821 2822 /* Allocate additional continuation packets? */ 2823 if (avail_dsds == 0) { 2824 /* 2825 * Five DSDs are available in the Cont. 2826 * Type 1 IOCB. 2827 */ 2828 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, 2829 vha->hw->req_q_map[0]); 2830 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address; 2831 avail_dsds = 5; 2832 entry_count++; 2833 } 2834 2835 sle_dma = sg_dma_address(sg); 2836 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 2837 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 2838 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 2839 loop_iterartion++; 2840 avail_dsds--; 2841 } 2842 ct_iocb->entry_count = entry_count; 2843 2844 sp->vha->qla_stats.control_requests++; 2845 } 2846 2847 static void 2848 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb) 2849 { 2850 uint16_t avail_dsds; 2851 uint32_t *cur_dsd; 2852 struct scatterlist *sg; 2853 int index; 2854 uint16_t cmd_dsds, rsp_dsds; 2855 scsi_qla_host_t *vha = sp->vha; 2856 struct qla_hw_data *ha = vha->hw; 2857 struct bsg_job *bsg_job = sp->u.bsg_job; 2858 int entry_count = 1; 2859 cont_a64_entry_t *cont_pkt = NULL; 2860 2861 ct_iocb->entry_type = CT_IOCB_TYPE; 2862 ct_iocb->entry_status = 0; 2863 ct_iocb->sys_define = 0; 2864 ct_iocb->handle = sp->handle; 2865 2866 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id); 2867 ct_iocb->vp_index = sp->vha->vp_idx; 2868 ct_iocb->comp_status = cpu_to_le16(0); 2869 2870 cmd_dsds = bsg_job->request_payload.sg_cnt; 2871 rsp_dsds = bsg_job->reply_payload.sg_cnt; 2872 2873 ct_iocb->cmd_dsd_count = cpu_to_le16(cmd_dsds); 2874 ct_iocb->timeout = 0; 2875 ct_iocb->rsp_dsd_count = cpu_to_le16(rsp_dsds); 2876 ct_iocb->cmd_byte_count = 2877 cpu_to_le32(bsg_job->request_payload.payload_len); 2878 2879 avail_dsds = 2; 2880 cur_dsd = (uint32_t *)ct_iocb->dseg_0_address; 2881 index = 0; 2882 2883 for_each_sg(bsg_job->request_payload.sg_list, sg, cmd_dsds, index) { 2884 dma_addr_t sle_dma; 2885 2886 /* Allocate additional continuation packets? */ 2887 if (avail_dsds == 0) { 2888 /* 2889 * Five DSDs are available in the Cont. 2890 * Type 1 IOCB. 2891 */ 2892 cont_pkt = qla2x00_prep_cont_type1_iocb( 2893 vha, ha->req_q_map[0]); 2894 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address; 2895 avail_dsds = 5; 2896 entry_count++; 2897 } 2898 2899 sle_dma = sg_dma_address(sg); 2900 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 2901 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 2902 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 2903 avail_dsds--; 2904 } 2905 2906 index = 0; 2907 2908 for_each_sg(bsg_job->reply_payload.sg_list, sg, rsp_dsds, index) { 2909 dma_addr_t sle_dma; 2910 2911 /* Allocate additional continuation packets? */ 2912 if (avail_dsds == 0) { 2913 /* 2914 * Five DSDs are available in the Cont. 2915 * Type 1 IOCB. 2916 */ 2917 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, 2918 ha->req_q_map[0]); 2919 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address; 2920 avail_dsds = 5; 2921 entry_count++; 2922 } 2923 2924 sle_dma = sg_dma_address(sg); 2925 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 2926 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 2927 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 2928 avail_dsds--; 2929 } 2930 ct_iocb->entry_count = entry_count; 2931 } 2932 2933 /* 2934 * qla82xx_start_scsi() - Send a SCSI command to the ISP 2935 * @sp: command to send to the ISP 2936 * 2937 * Returns non-zero if a failure occurred, else zero. 2938 */ 2939 int 2940 qla82xx_start_scsi(srb_t *sp) 2941 { 2942 int nseg; 2943 unsigned long flags; 2944 struct scsi_cmnd *cmd; 2945 uint32_t *clr_ptr; 2946 uint32_t index; 2947 uint32_t handle; 2948 uint16_t cnt; 2949 uint16_t req_cnt; 2950 uint16_t tot_dsds; 2951 struct device_reg_82xx __iomem *reg; 2952 uint32_t dbval; 2953 uint32_t *fcp_dl; 2954 uint8_t additional_cdb_len; 2955 struct ct6_dsd *ctx; 2956 struct scsi_qla_host *vha = sp->vha; 2957 struct qla_hw_data *ha = vha->hw; 2958 struct req_que *req = NULL; 2959 struct rsp_que *rsp = NULL; 2960 2961 /* Setup device pointers. */ 2962 reg = &ha->iobase->isp82; 2963 cmd = GET_CMD_SP(sp); 2964 req = vha->req; 2965 rsp = ha->rsp_q_map[0]; 2966 2967 /* So we know we haven't pci_map'ed anything yet */ 2968 tot_dsds = 0; 2969 2970 dbval = 0x04 | (ha->portnum << 5); 2971 2972 /* Send marker if required */ 2973 if (vha->marker_needed != 0) { 2974 if (qla2x00_marker(vha, req, 2975 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) { 2976 ql_log(ql_log_warn, vha, 0x300c, 2977 "qla2x00_marker failed for cmd=%p.\n", cmd); 2978 return QLA_FUNCTION_FAILED; 2979 } 2980 vha->marker_needed = 0; 2981 } 2982 2983 /* Acquire ring specific lock */ 2984 spin_lock_irqsave(&ha->hardware_lock, flags); 2985 2986 /* Check for room in outstanding command list. */ 2987 handle = req->current_outstanding_cmd; 2988 for (index = 1; index < req->num_outstanding_cmds; index++) { 2989 handle++; 2990 if (handle == req->num_outstanding_cmds) 2991 handle = 1; 2992 if (!req->outstanding_cmds[handle]) 2993 break; 2994 } 2995 if (index == req->num_outstanding_cmds) 2996 goto queuing_error; 2997 2998 /* Map the sg table so we have an accurate count of sg entries needed */ 2999 if (scsi_sg_count(cmd)) { 3000 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd), 3001 scsi_sg_count(cmd), cmd->sc_data_direction); 3002 if (unlikely(!nseg)) 3003 goto queuing_error; 3004 } else 3005 nseg = 0; 3006 3007 tot_dsds = nseg; 3008 3009 if (tot_dsds > ql2xshiftctondsd) { 3010 struct cmd_type_6 *cmd_pkt; 3011 uint16_t more_dsd_lists = 0; 3012 struct dsd_dma *dsd_ptr; 3013 uint16_t i; 3014 3015 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds); 3016 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) { 3017 ql_dbg(ql_dbg_io, vha, 0x300d, 3018 "Num of DSD list %d is than %d for cmd=%p.\n", 3019 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN, 3020 cmd); 3021 goto queuing_error; 3022 } 3023 3024 if (more_dsd_lists <= ha->gbl_dsd_avail) 3025 goto sufficient_dsds; 3026 else 3027 more_dsd_lists -= ha->gbl_dsd_avail; 3028 3029 for (i = 0; i < more_dsd_lists; i++) { 3030 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC); 3031 if (!dsd_ptr) { 3032 ql_log(ql_log_fatal, vha, 0x300e, 3033 "Failed to allocate memory for dsd_dma " 3034 "for cmd=%p.\n", cmd); 3035 goto queuing_error; 3036 } 3037 3038 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool, 3039 GFP_ATOMIC, &dsd_ptr->dsd_list_dma); 3040 if (!dsd_ptr->dsd_addr) { 3041 kfree(dsd_ptr); 3042 ql_log(ql_log_fatal, vha, 0x300f, 3043 "Failed to allocate memory for dsd_addr " 3044 "for cmd=%p.\n", cmd); 3045 goto queuing_error; 3046 } 3047 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list); 3048 ha->gbl_dsd_avail++; 3049 } 3050 3051 sufficient_dsds: 3052 req_cnt = 1; 3053 3054 if (req->cnt < (req_cnt + 2)) { 3055 cnt = (uint16_t)RD_REG_DWORD_RELAXED( 3056 ®->req_q_out[0]); 3057 if (req->ring_index < cnt) 3058 req->cnt = cnt - req->ring_index; 3059 else 3060 req->cnt = req->length - 3061 (req->ring_index - cnt); 3062 if (req->cnt < (req_cnt + 2)) 3063 goto queuing_error; 3064 } 3065 3066 ctx = sp->u.scmd.ctx = 3067 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC); 3068 if (!ctx) { 3069 ql_log(ql_log_fatal, vha, 0x3010, 3070 "Failed to allocate ctx for cmd=%p.\n", cmd); 3071 goto queuing_error; 3072 } 3073 3074 memset(ctx, 0, sizeof(struct ct6_dsd)); 3075 ctx->fcp_cmnd = dma_pool_zalloc(ha->fcp_cmnd_dma_pool, 3076 GFP_ATOMIC, &ctx->fcp_cmnd_dma); 3077 if (!ctx->fcp_cmnd) { 3078 ql_log(ql_log_fatal, vha, 0x3011, 3079 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd); 3080 goto queuing_error; 3081 } 3082 3083 /* Initialize the DSD list and dma handle */ 3084 INIT_LIST_HEAD(&ctx->dsd_list); 3085 ctx->dsd_use_cnt = 0; 3086 3087 if (cmd->cmd_len > 16) { 3088 additional_cdb_len = cmd->cmd_len - 16; 3089 if ((cmd->cmd_len % 4) != 0) { 3090 /* SCSI command bigger than 16 bytes must be 3091 * multiple of 4 3092 */ 3093 ql_log(ql_log_warn, vha, 0x3012, 3094 "scsi cmd len %d not multiple of 4 " 3095 "for cmd=%p.\n", cmd->cmd_len, cmd); 3096 goto queuing_error_fcp_cmnd; 3097 } 3098 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4; 3099 } else { 3100 additional_cdb_len = 0; 3101 ctx->fcp_cmnd_len = 12 + 16 + 4; 3102 } 3103 3104 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr; 3105 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 3106 3107 /* Zero out remaining portion of packet. */ 3108 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */ 3109 clr_ptr = (uint32_t *)cmd_pkt + 2; 3110 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 3111 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 3112 3113 /* Set NPORT-ID and LUN number*/ 3114 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 3115 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 3116 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 3117 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 3118 cmd_pkt->vp_index = sp->vha->vp_idx; 3119 3120 /* Build IOCB segments */ 3121 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds)) 3122 goto queuing_error_fcp_cmnd; 3123 3124 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 3125 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun)); 3126 3127 /* build FCP_CMND IU */ 3128 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun); 3129 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len; 3130 3131 if (cmd->sc_data_direction == DMA_TO_DEVICE) 3132 ctx->fcp_cmnd->additional_cdb_len |= 1; 3133 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 3134 ctx->fcp_cmnd->additional_cdb_len |= 2; 3135 3136 /* Populate the FCP_PRIO. */ 3137 if (ha->flags.fcp_prio_enabled) 3138 ctx->fcp_cmnd->task_attribute |= 3139 sp->fcport->fcp_prio << 3; 3140 3141 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len); 3142 3143 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 + 3144 additional_cdb_len); 3145 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd)); 3146 3147 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len); 3148 cmd_pkt->fcp_cmnd_dseg_address[0] = 3149 cpu_to_le32(LSD(ctx->fcp_cmnd_dma)); 3150 cmd_pkt->fcp_cmnd_dseg_address[1] = 3151 cpu_to_le32(MSD(ctx->fcp_cmnd_dma)); 3152 3153 sp->flags |= SRB_FCP_CMND_DMA_VALID; 3154 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); 3155 /* Set total data segment count. */ 3156 cmd_pkt->entry_count = (uint8_t)req_cnt; 3157 /* Specify response queue number where 3158 * completion should happen 3159 */ 3160 cmd_pkt->entry_status = (uint8_t) rsp->id; 3161 } else { 3162 struct cmd_type_7 *cmd_pkt; 3163 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds); 3164 if (req->cnt < (req_cnt + 2)) { 3165 cnt = (uint16_t)RD_REG_DWORD_RELAXED( 3166 ®->req_q_out[0]); 3167 if (req->ring_index < cnt) 3168 req->cnt = cnt - req->ring_index; 3169 else 3170 req->cnt = req->length - 3171 (req->ring_index - cnt); 3172 } 3173 if (req->cnt < (req_cnt + 2)) 3174 goto queuing_error; 3175 3176 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr; 3177 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 3178 3179 /* Zero out remaining portion of packet. */ 3180 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/ 3181 clr_ptr = (uint32_t *)cmd_pkt + 2; 3182 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 3183 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 3184 3185 /* Set NPORT-ID and LUN number*/ 3186 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 3187 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 3188 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 3189 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 3190 cmd_pkt->vp_index = sp->vha->vp_idx; 3191 3192 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun); 3193 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, 3194 sizeof(cmd_pkt->lun)); 3195 3196 /* Populate the FCP_PRIO. */ 3197 if (ha->flags.fcp_prio_enabled) 3198 cmd_pkt->task |= sp->fcport->fcp_prio << 3; 3199 3200 /* Load SCSI command packet. */ 3201 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); 3202 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); 3203 3204 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd)); 3205 3206 /* Build IOCB segments */ 3207 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req); 3208 3209 /* Set total data segment count. */ 3210 cmd_pkt->entry_count = (uint8_t)req_cnt; 3211 /* Specify response queue number where 3212 * completion should happen. 3213 */ 3214 cmd_pkt->entry_status = (uint8_t) rsp->id; 3215 3216 } 3217 /* Build command packet. */ 3218 req->current_outstanding_cmd = handle; 3219 req->outstanding_cmds[handle] = sp; 3220 sp->handle = handle; 3221 cmd->host_scribble = (unsigned char *)(unsigned long)handle; 3222 req->cnt -= req_cnt; 3223 wmb(); 3224 3225 /* Adjust ring index. */ 3226 req->ring_index++; 3227 if (req->ring_index == req->length) { 3228 req->ring_index = 0; 3229 req->ring_ptr = req->ring; 3230 } else 3231 req->ring_ptr++; 3232 3233 sp->flags |= SRB_DMA_VALID; 3234 3235 /* Set chip new ring index. */ 3236 /* write, read and verify logic */ 3237 dbval = dbval | (req->id << 8) | (req->ring_index << 16); 3238 if (ql2xdbwr) 3239 qla82xx_wr_32(ha, (uintptr_t __force)ha->nxdb_wr_ptr, dbval); 3240 else { 3241 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval); 3242 wmb(); 3243 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) { 3244 WRT_REG_DWORD(ha->nxdb_wr_ptr, dbval); 3245 wmb(); 3246 } 3247 } 3248 3249 /* Manage unprocessed RIO/ZIO commands in response queue. */ 3250 if (vha->flags.process_response_queue && 3251 rsp->ring_ptr->signature != RESPONSE_PROCESSED) 3252 qla24xx_process_response_queue(vha, rsp); 3253 3254 spin_unlock_irqrestore(&ha->hardware_lock, flags); 3255 return QLA_SUCCESS; 3256 3257 queuing_error_fcp_cmnd: 3258 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma); 3259 queuing_error: 3260 if (tot_dsds) 3261 scsi_dma_unmap(cmd); 3262 3263 if (sp->u.scmd.ctx) { 3264 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool); 3265 sp->u.scmd.ctx = NULL; 3266 } 3267 spin_unlock_irqrestore(&ha->hardware_lock, flags); 3268 3269 return QLA_FUNCTION_FAILED; 3270 } 3271 3272 static void 3273 qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb) 3274 { 3275 struct srb_iocb *aio = &sp->u.iocb_cmd; 3276 scsi_qla_host_t *vha = sp->vha; 3277 struct req_que *req = vha->req; 3278 3279 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx)); 3280 abt_iocb->entry_type = ABORT_IOCB_TYPE; 3281 abt_iocb->entry_count = 1; 3282 abt_iocb->handle = cpu_to_le32(MAKE_HANDLE(req->id, sp->handle)); 3283 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id); 3284 abt_iocb->handle_to_abort = 3285 cpu_to_le32(MAKE_HANDLE(aio->u.abt.req_que_no, 3286 aio->u.abt.cmd_hndl)); 3287 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa; 3288 abt_iocb->port_id[1] = sp->fcport->d_id.b.area; 3289 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain; 3290 abt_iocb->vp_index = vha->vp_idx; 3291 abt_iocb->req_que_no = cpu_to_le16(aio->u.abt.req_que_no); 3292 /* Send the command to the firmware */ 3293 wmb(); 3294 } 3295 3296 static void 3297 qla2x00_mb_iocb(srb_t *sp, struct mbx_24xx_entry *mbx) 3298 { 3299 int i, sz; 3300 3301 mbx->entry_type = MBX_IOCB_TYPE; 3302 mbx->handle = sp->handle; 3303 sz = min(ARRAY_SIZE(mbx->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.out_mb)); 3304 3305 for (i = 0; i < sz; i++) 3306 mbx->mb[i] = cpu_to_le16(sp->u.iocb_cmd.u.mbx.out_mb[i]); 3307 } 3308 3309 static void 3310 qla2x00_ctpthru_cmd_iocb(srb_t *sp, struct ct_entry_24xx *ct_pkt) 3311 { 3312 sp->u.iocb_cmd.u.ctarg.iocb = ct_pkt; 3313 qla24xx_prep_ms_iocb(sp->vha, &sp->u.iocb_cmd.u.ctarg); 3314 ct_pkt->handle = sp->handle; 3315 } 3316 3317 static void qla2x00_send_notify_ack_iocb(srb_t *sp, 3318 struct nack_to_isp *nack) 3319 { 3320 struct imm_ntfy_from_isp *ntfy = sp->u.iocb_cmd.u.nack.ntfy; 3321 3322 nack->entry_type = NOTIFY_ACK_TYPE; 3323 nack->entry_count = 1; 3324 nack->ox_id = ntfy->ox_id; 3325 3326 nack->u.isp24.handle = sp->handle; 3327 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle; 3328 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) { 3329 nack->u.isp24.flags = ntfy->u.isp24.flags & 3330 cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB); 3331 } 3332 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id; 3333 nack->u.isp24.status = ntfy->u.isp24.status; 3334 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode; 3335 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle; 3336 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address; 3337 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs; 3338 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui; 3339 nack->u.isp24.srr_flags = 0; 3340 nack->u.isp24.srr_reject_code = 0; 3341 nack->u.isp24.srr_reject_code_expl = 0; 3342 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index; 3343 } 3344 3345 /* 3346 * Build NVME LS request 3347 */ 3348 static int 3349 qla_nvme_ls(srb_t *sp, struct pt_ls4_request *cmd_pkt) 3350 { 3351 struct srb_iocb *nvme; 3352 int rval = QLA_SUCCESS; 3353 3354 nvme = &sp->u.iocb_cmd; 3355 cmd_pkt->entry_type = PT_LS4_REQUEST; 3356 cmd_pkt->entry_count = 1; 3357 cmd_pkt->control_flags = CF_LS4_ORIGINATOR << CF_LS4_SHIFT; 3358 3359 cmd_pkt->timeout = cpu_to_le16(nvme->u.nvme.timeout_sec); 3360 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 3361 cmd_pkt->vp_index = sp->fcport->vha->vp_idx; 3362 3363 cmd_pkt->tx_dseg_count = 1; 3364 cmd_pkt->tx_byte_count = nvme->u.nvme.cmd_len; 3365 cmd_pkt->dseg0_len = nvme->u.nvme.cmd_len; 3366 cmd_pkt->dseg0_address[0] = cpu_to_le32(LSD(nvme->u.nvme.cmd_dma)); 3367 cmd_pkt->dseg0_address[1] = cpu_to_le32(MSD(nvme->u.nvme.cmd_dma)); 3368 3369 cmd_pkt->rx_dseg_count = 1; 3370 cmd_pkt->rx_byte_count = nvme->u.nvme.rsp_len; 3371 cmd_pkt->dseg1_len = nvme->u.nvme.rsp_len; 3372 cmd_pkt->dseg1_address[0] = cpu_to_le32(LSD(nvme->u.nvme.rsp_dma)); 3373 cmd_pkt->dseg1_address[1] = cpu_to_le32(MSD(nvme->u.nvme.rsp_dma)); 3374 3375 return rval; 3376 } 3377 3378 static void 3379 qla25xx_ctrlvp_iocb(srb_t *sp, struct vp_ctrl_entry_24xx *vce) 3380 { 3381 int map, pos; 3382 3383 vce->entry_type = VP_CTRL_IOCB_TYPE; 3384 vce->handle = sp->handle; 3385 vce->entry_count = 1; 3386 vce->command = cpu_to_le16(sp->u.iocb_cmd.u.ctrlvp.cmd); 3387 vce->vp_count = cpu_to_le16(1); 3388 3389 /* 3390 * index map in firmware starts with 1; decrement index 3391 * this is ok as we never use index 0 3392 */ 3393 map = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) / 8; 3394 pos = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) & 7; 3395 vce->vp_idx_map[map] |= 1 << pos; 3396 } 3397 3398 static void 3399 qla24xx_prlo_iocb(srb_t *sp, struct logio_entry_24xx *logio) 3400 { 3401 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE; 3402 logio->control_flags = 3403 cpu_to_le16(LCF_COMMAND_PRLO|LCF_IMPL_PRLO); 3404 3405 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id); 3406 logio->port_id[0] = sp->fcport->d_id.b.al_pa; 3407 logio->port_id[1] = sp->fcport->d_id.b.area; 3408 logio->port_id[2] = sp->fcport->d_id.b.domain; 3409 logio->vp_index = sp->fcport->vha->vp_idx; 3410 } 3411 3412 int 3413 qla2x00_start_sp(srb_t *sp) 3414 { 3415 int rval; 3416 scsi_qla_host_t *vha = sp->vha; 3417 struct qla_hw_data *ha = vha->hw; 3418 void *pkt; 3419 unsigned long flags; 3420 3421 rval = QLA_FUNCTION_FAILED; 3422 spin_lock_irqsave(&ha->hardware_lock, flags); 3423 pkt = qla2x00_alloc_iocbs(vha, sp); 3424 if (!pkt) { 3425 ql_log(ql_log_warn, vha, 0x700c, 3426 "qla2x00_alloc_iocbs failed.\n"); 3427 goto done; 3428 } 3429 3430 rval = QLA_SUCCESS; 3431 switch (sp->type) { 3432 case SRB_LOGIN_CMD: 3433 IS_FWI2_CAPABLE(ha) ? 3434 qla24xx_login_iocb(sp, pkt) : 3435 qla2x00_login_iocb(sp, pkt); 3436 break; 3437 case SRB_PRLI_CMD: 3438 qla24xx_prli_iocb(sp, pkt); 3439 break; 3440 case SRB_LOGOUT_CMD: 3441 IS_FWI2_CAPABLE(ha) ? 3442 qla24xx_logout_iocb(sp, pkt) : 3443 qla2x00_logout_iocb(sp, pkt); 3444 break; 3445 case SRB_ELS_CMD_RPT: 3446 case SRB_ELS_CMD_HST: 3447 qla24xx_els_iocb(sp, pkt); 3448 break; 3449 case SRB_CT_CMD: 3450 IS_FWI2_CAPABLE(ha) ? 3451 qla24xx_ct_iocb(sp, pkt) : 3452 qla2x00_ct_iocb(sp, pkt); 3453 break; 3454 case SRB_ADISC_CMD: 3455 IS_FWI2_CAPABLE(ha) ? 3456 qla24xx_adisc_iocb(sp, pkt) : 3457 qla2x00_adisc_iocb(sp, pkt); 3458 break; 3459 case SRB_TM_CMD: 3460 IS_QLAFX00(ha) ? 3461 qlafx00_tm_iocb(sp, pkt) : 3462 qla24xx_tm_iocb(sp, pkt); 3463 break; 3464 case SRB_FXIOCB_DCMD: 3465 case SRB_FXIOCB_BCMD: 3466 qlafx00_fxdisc_iocb(sp, pkt); 3467 break; 3468 case SRB_NVME_LS: 3469 qla_nvme_ls(sp, pkt); 3470 break; 3471 case SRB_ABT_CMD: 3472 IS_QLAFX00(ha) ? 3473 qlafx00_abort_iocb(sp, pkt) : 3474 qla24xx_abort_iocb(sp, pkt); 3475 break; 3476 case SRB_ELS_DCMD: 3477 qla24xx_els_logo_iocb(sp, pkt); 3478 break; 3479 case SRB_CT_PTHRU_CMD: 3480 qla2x00_ctpthru_cmd_iocb(sp, pkt); 3481 break; 3482 case SRB_MB_IOCB: 3483 qla2x00_mb_iocb(sp, pkt); 3484 break; 3485 case SRB_NACK_PLOGI: 3486 case SRB_NACK_PRLI: 3487 case SRB_NACK_LOGO: 3488 qla2x00_send_notify_ack_iocb(sp, pkt); 3489 break; 3490 case SRB_CTRL_VP: 3491 qla25xx_ctrlvp_iocb(sp, pkt); 3492 break; 3493 case SRB_PRLO_CMD: 3494 qla24xx_prlo_iocb(sp, pkt); 3495 break; 3496 default: 3497 break; 3498 } 3499 3500 wmb(); 3501 qla2x00_start_iocbs(vha, ha->req_q_map[0]); 3502 done: 3503 spin_unlock_irqrestore(&ha->hardware_lock, flags); 3504 return rval; 3505 } 3506 3507 static void 3508 qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha, 3509 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds) 3510 { 3511 uint16_t avail_dsds; 3512 uint32_t *cur_dsd; 3513 uint32_t req_data_len = 0; 3514 uint32_t rsp_data_len = 0; 3515 struct scatterlist *sg; 3516 int index; 3517 int entry_count = 1; 3518 struct bsg_job *bsg_job = sp->u.bsg_job; 3519 3520 /*Update entry type to indicate bidir command */ 3521 *((uint32_t *)(&cmd_pkt->entry_type)) = 3522 cpu_to_le32(COMMAND_BIDIRECTIONAL); 3523 3524 /* Set the transfer direction, in this set both flags 3525 * Also set the BD_WRAP_BACK flag, firmware will take care 3526 * assigning DID=SID for outgoing pkts. 3527 */ 3528 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt); 3529 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt); 3530 cmd_pkt->control_flags = cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA | 3531 BD_WRAP_BACK); 3532 3533 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len; 3534 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len); 3535 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len); 3536 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2); 3537 3538 vha->bidi_stats.transfer_bytes += req_data_len; 3539 vha->bidi_stats.io_count++; 3540 3541 vha->qla_stats.output_bytes += req_data_len; 3542 vha->qla_stats.output_requests++; 3543 3544 /* Only one dsd is available for bidirectional IOCB, remaining dsds 3545 * are bundled in continuation iocb 3546 */ 3547 avail_dsds = 1; 3548 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address; 3549 3550 index = 0; 3551 3552 for_each_sg(bsg_job->request_payload.sg_list, sg, 3553 bsg_job->request_payload.sg_cnt, index) { 3554 dma_addr_t sle_dma; 3555 cont_a64_entry_t *cont_pkt; 3556 3557 /* Allocate additional continuation packets */ 3558 if (avail_dsds == 0) { 3559 /* Continuation type 1 IOCB can accomodate 3560 * 5 DSDS 3561 */ 3562 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req); 3563 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address; 3564 avail_dsds = 5; 3565 entry_count++; 3566 } 3567 sle_dma = sg_dma_address(sg); 3568 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 3569 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 3570 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 3571 avail_dsds--; 3572 } 3573 /* For read request DSD will always goes to continuation IOCB 3574 * and follow the write DSD. If there is room on the current IOCB 3575 * then it is added to that IOCB else new continuation IOCB is 3576 * allocated. 3577 */ 3578 for_each_sg(bsg_job->reply_payload.sg_list, sg, 3579 bsg_job->reply_payload.sg_cnt, index) { 3580 dma_addr_t sle_dma; 3581 cont_a64_entry_t *cont_pkt; 3582 3583 /* Allocate additional continuation packets */ 3584 if (avail_dsds == 0) { 3585 /* Continuation type 1 IOCB can accomodate 3586 * 5 DSDS 3587 */ 3588 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req); 3589 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address; 3590 avail_dsds = 5; 3591 entry_count++; 3592 } 3593 sle_dma = sg_dma_address(sg); 3594 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 3595 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 3596 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg)); 3597 avail_dsds--; 3598 } 3599 /* This value should be same as number of IOCB required for this cmd */ 3600 cmd_pkt->entry_count = entry_count; 3601 } 3602 3603 int 3604 qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds) 3605 { 3606 3607 struct qla_hw_data *ha = vha->hw; 3608 unsigned long flags; 3609 uint32_t handle; 3610 uint32_t index; 3611 uint16_t req_cnt; 3612 uint16_t cnt; 3613 uint32_t *clr_ptr; 3614 struct cmd_bidir *cmd_pkt = NULL; 3615 struct rsp_que *rsp; 3616 struct req_que *req; 3617 int rval = EXT_STATUS_OK; 3618 3619 rval = QLA_SUCCESS; 3620 3621 rsp = ha->rsp_q_map[0]; 3622 req = vha->req; 3623 3624 /* Send marker if required */ 3625 if (vha->marker_needed != 0) { 3626 if (qla2x00_marker(vha, req, 3627 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) 3628 return EXT_STATUS_MAILBOX; 3629 vha->marker_needed = 0; 3630 } 3631 3632 /* Acquire ring specific lock */ 3633 spin_lock_irqsave(&ha->hardware_lock, flags); 3634 3635 /* Check for room in outstanding command list. */ 3636 handle = req->current_outstanding_cmd; 3637 for (index = 1; index < req->num_outstanding_cmds; index++) { 3638 handle++; 3639 if (handle == req->num_outstanding_cmds) 3640 handle = 1; 3641 if (!req->outstanding_cmds[handle]) 3642 break; 3643 } 3644 3645 if (index == req->num_outstanding_cmds) { 3646 rval = EXT_STATUS_BUSY; 3647 goto queuing_error; 3648 } 3649 3650 /* Calculate number of IOCB required */ 3651 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds); 3652 3653 /* Check for room on request queue. */ 3654 if (req->cnt < req_cnt + 2) { 3655 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr : 3656 RD_REG_DWORD_RELAXED(req->req_q_out); 3657 if (req->ring_index < cnt) 3658 req->cnt = cnt - req->ring_index; 3659 else 3660 req->cnt = req->length - 3661 (req->ring_index - cnt); 3662 } 3663 if (req->cnt < req_cnt + 2) { 3664 rval = EXT_STATUS_BUSY; 3665 goto queuing_error; 3666 } 3667 3668 cmd_pkt = (struct cmd_bidir *)req->ring_ptr; 3669 cmd_pkt->handle = MAKE_HANDLE(req->id, handle); 3670 3671 /* Zero out remaining portion of packet. */ 3672 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/ 3673 clr_ptr = (uint32_t *)cmd_pkt + 2; 3674 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 3675 3676 /* Set NPORT-ID (of vha)*/ 3677 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id); 3678 cmd_pkt->port_id[0] = vha->d_id.b.al_pa; 3679 cmd_pkt->port_id[1] = vha->d_id.b.area; 3680 cmd_pkt->port_id[2] = vha->d_id.b.domain; 3681 3682 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds); 3683 cmd_pkt->entry_status = (uint8_t) rsp->id; 3684 /* Build command packet. */ 3685 req->current_outstanding_cmd = handle; 3686 req->outstanding_cmds[handle] = sp; 3687 sp->handle = handle; 3688 req->cnt -= req_cnt; 3689 3690 /* Send the command to the firmware */ 3691 wmb(); 3692 qla2x00_start_iocbs(vha, req); 3693 queuing_error: 3694 spin_unlock_irqrestore(&ha->hardware_lock, flags); 3695 return rval; 3696 } 3697