1 /****************************************************************************** 2 * QLOGIC LINUX SOFTWARE 3 * 4 * QLogic ISP2x00 device driver for Linux 2.6.x 5 * Copyright (C) 2003-2005 QLogic Corporation 6 * (www.qlogic.com) 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms of the GNU General Public License as published by the 10 * Free Software Foundation; either version 2, or (at your option) any 11 * later version. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 ******************************************************************************/ 19 20 #include "qla_def.h" 21 22 #include <linux/blkdev.h> 23 #include <linux/delay.h> 24 25 #include <scsi/scsi_tcq.h> 26 27 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd); 28 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *); 29 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *); 30 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha); 31 32 /** 33 * qla2x00_get_cmd_direction() - Determine control_flag data direction. 34 * @cmd: SCSI command 35 * 36 * Returns the proper CF_* direction based on CDB. 37 */ 38 static inline uint16_t 39 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd) 40 { 41 uint16_t cflags; 42 43 cflags = 0; 44 45 /* Set transfer direction */ 46 if (cmd->sc_data_direction == DMA_TO_DEVICE) 47 cflags = CF_WRITE; 48 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 49 cflags = CF_READ; 50 return (cflags); 51 } 52 53 /** 54 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and 55 * Continuation Type 0 IOCBs to allocate. 56 * 57 * @dsds: number of data segment decriptors needed 58 * 59 * Returns the number of IOCB entries needed to store @dsds. 60 */ 61 uint16_t 62 qla2x00_calc_iocbs_32(uint16_t dsds) 63 { 64 uint16_t iocbs; 65 66 iocbs = 1; 67 if (dsds > 3) { 68 iocbs += (dsds - 3) / 7; 69 if ((dsds - 3) % 7) 70 iocbs++; 71 } 72 return (iocbs); 73 } 74 75 /** 76 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and 77 * Continuation Type 1 IOCBs to allocate. 78 * 79 * @dsds: number of data segment decriptors needed 80 * 81 * Returns the number of IOCB entries needed to store @dsds. 82 */ 83 uint16_t 84 qla2x00_calc_iocbs_64(uint16_t dsds) 85 { 86 uint16_t iocbs; 87 88 iocbs = 1; 89 if (dsds > 2) { 90 iocbs += (dsds - 2) / 5; 91 if ((dsds - 2) % 5) 92 iocbs++; 93 } 94 return (iocbs); 95 } 96 97 /** 98 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB. 99 * @ha: HA context 100 * 101 * Returns a pointer to the Continuation Type 0 IOCB packet. 102 */ 103 static inline cont_entry_t * 104 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha) 105 { 106 cont_entry_t *cont_pkt; 107 108 /* Adjust ring index. */ 109 ha->req_ring_index++; 110 if (ha->req_ring_index == ha->request_q_length) { 111 ha->req_ring_index = 0; 112 ha->request_ring_ptr = ha->request_ring; 113 } else { 114 ha->request_ring_ptr++; 115 } 116 117 cont_pkt = (cont_entry_t *)ha->request_ring_ptr; 118 119 /* Load packet defaults. */ 120 *((uint32_t *)(&cont_pkt->entry_type)) = 121 __constant_cpu_to_le32(CONTINUE_TYPE); 122 123 return (cont_pkt); 124 } 125 126 /** 127 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB. 128 * @ha: HA context 129 * 130 * Returns a pointer to the continuation type 1 IOCB packet. 131 */ 132 static inline cont_a64_entry_t * 133 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha) 134 { 135 cont_a64_entry_t *cont_pkt; 136 137 /* Adjust ring index. */ 138 ha->req_ring_index++; 139 if (ha->req_ring_index == ha->request_q_length) { 140 ha->req_ring_index = 0; 141 ha->request_ring_ptr = ha->request_ring; 142 } else { 143 ha->request_ring_ptr++; 144 } 145 146 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr; 147 148 /* Load packet defaults. */ 149 *((uint32_t *)(&cont_pkt->entry_type)) = 150 __constant_cpu_to_le32(CONTINUE_A64_TYPE); 151 152 return (cont_pkt); 153 } 154 155 /** 156 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit 157 * capable IOCB types. 158 * 159 * @sp: SRB command to process 160 * @cmd_pkt: Command type 2 IOCB 161 * @tot_dsds: Total number of segments to transfer 162 */ 163 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt, 164 uint16_t tot_dsds) 165 { 166 uint16_t avail_dsds; 167 uint32_t *cur_dsd; 168 scsi_qla_host_t *ha; 169 struct scsi_cmnd *cmd; 170 171 cmd = sp->cmd; 172 173 /* Update entry type to indicate Command Type 2 IOCB */ 174 *((uint32_t *)(&cmd_pkt->entry_type)) = 175 __constant_cpu_to_le32(COMMAND_TYPE); 176 177 /* No data transfer */ 178 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) { 179 cmd_pkt->byte_count = __constant_cpu_to_le32(0); 180 return; 181 } 182 183 ha = sp->ha; 184 185 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd)); 186 187 /* Three DSDs are available in the Command Type 2 IOCB */ 188 avail_dsds = 3; 189 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 190 191 /* Load data segments */ 192 if (cmd->use_sg != 0) { 193 struct scatterlist *cur_seg; 194 struct scatterlist *end_seg; 195 196 cur_seg = (struct scatterlist *)cmd->request_buffer; 197 end_seg = cur_seg + tot_dsds; 198 while (cur_seg < end_seg) { 199 cont_entry_t *cont_pkt; 200 201 /* Allocate additional continuation packets? */ 202 if (avail_dsds == 0) { 203 /* 204 * Seven DSDs are available in the Continuation 205 * Type 0 IOCB. 206 */ 207 cont_pkt = qla2x00_prep_cont_type0_iocb(ha); 208 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address; 209 avail_dsds = 7; 210 } 211 212 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg)); 213 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg)); 214 avail_dsds--; 215 216 cur_seg++; 217 } 218 } else { 219 *cur_dsd++ = cpu_to_le32(sp->dma_handle); 220 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen); 221 } 222 } 223 224 /** 225 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit 226 * capable IOCB types. 227 * 228 * @sp: SRB command to process 229 * @cmd_pkt: Command type 3 IOCB 230 * @tot_dsds: Total number of segments to transfer 231 */ 232 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt, 233 uint16_t tot_dsds) 234 { 235 uint16_t avail_dsds; 236 uint32_t *cur_dsd; 237 scsi_qla_host_t *ha; 238 struct scsi_cmnd *cmd; 239 240 cmd = sp->cmd; 241 242 /* Update entry type to indicate Command Type 3 IOCB */ 243 *((uint32_t *)(&cmd_pkt->entry_type)) = 244 __constant_cpu_to_le32(COMMAND_A64_TYPE); 245 246 /* No data transfer */ 247 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) { 248 cmd_pkt->byte_count = __constant_cpu_to_le32(0); 249 return; 250 } 251 252 ha = sp->ha; 253 254 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd)); 255 256 /* Two DSDs are available in the Command Type 3 IOCB */ 257 avail_dsds = 2; 258 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 259 260 /* Load data segments */ 261 if (cmd->use_sg != 0) { 262 struct scatterlist *cur_seg; 263 struct scatterlist *end_seg; 264 265 cur_seg = (struct scatterlist *)cmd->request_buffer; 266 end_seg = cur_seg + tot_dsds; 267 while (cur_seg < end_seg) { 268 dma_addr_t sle_dma; 269 cont_a64_entry_t *cont_pkt; 270 271 /* Allocate additional continuation packets? */ 272 if (avail_dsds == 0) { 273 /* 274 * Five DSDs are available in the Continuation 275 * Type 1 IOCB. 276 */ 277 cont_pkt = qla2x00_prep_cont_type1_iocb(ha); 278 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; 279 avail_dsds = 5; 280 } 281 282 sle_dma = sg_dma_address(cur_seg); 283 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 284 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 285 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg)); 286 avail_dsds--; 287 288 cur_seg++; 289 } 290 } else { 291 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle)); 292 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle)); 293 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen); 294 } 295 } 296 297 /** 298 * qla2x00_start_scsi() - Send a SCSI command to the ISP 299 * @sp: command to send to the ISP 300 * 301 * Returns non-zero if a failure occured, else zero. 302 */ 303 int 304 qla2x00_start_scsi(srb_t *sp) 305 { 306 int ret; 307 unsigned long flags; 308 scsi_qla_host_t *ha; 309 struct scsi_cmnd *cmd; 310 uint32_t *clr_ptr; 311 uint32_t index; 312 uint32_t handle; 313 cmd_entry_t *cmd_pkt; 314 struct scatterlist *sg; 315 uint16_t cnt; 316 uint16_t req_cnt; 317 uint16_t tot_dsds; 318 struct device_reg_2xxx __iomem *reg; 319 char tag[2]; 320 321 /* Setup device pointers. */ 322 ret = 0; 323 ha = sp->ha; 324 reg = &ha->iobase->isp; 325 cmd = sp->cmd; 326 /* So we know we haven't pci_map'ed anything yet */ 327 tot_dsds = 0; 328 329 /* Send marker if required */ 330 if (ha->marker_needed != 0) { 331 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) { 332 return (QLA_FUNCTION_FAILED); 333 } 334 ha->marker_needed = 0; 335 } 336 337 /* Acquire ring specific lock */ 338 spin_lock_irqsave(&ha->hardware_lock, flags); 339 340 /* Check for room in outstanding command list. */ 341 handle = ha->current_outstanding_cmd; 342 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { 343 handle++; 344 if (handle == MAX_OUTSTANDING_COMMANDS) 345 handle = 1; 346 if (ha->outstanding_cmds[handle] == 0) 347 break; 348 } 349 if (index == MAX_OUTSTANDING_COMMANDS) 350 goto queuing_error; 351 352 /* Map the sg table so we have an accurate count of sg entries needed */ 353 if (cmd->use_sg) { 354 sg = (struct scatterlist *) cmd->request_buffer; 355 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg, 356 cmd->sc_data_direction); 357 if (tot_dsds == 0) 358 goto queuing_error; 359 } else if (cmd->request_bufflen) { 360 dma_addr_t req_dma; 361 362 req_dma = pci_map_single(ha->pdev, cmd->request_buffer, 363 cmd->request_bufflen, cmd->sc_data_direction); 364 if (dma_mapping_error(req_dma)) 365 goto queuing_error; 366 367 sp->dma_handle = req_dma; 368 tot_dsds = 1; 369 } 370 371 /* Calculate the number of request entries needed. */ 372 req_cnt = ha->isp_ops.calc_req_entries(tot_dsds); 373 if (ha->req_q_cnt < (req_cnt + 2)) { 374 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg)); 375 if (ha->req_ring_index < cnt) 376 ha->req_q_cnt = cnt - ha->req_ring_index; 377 else 378 ha->req_q_cnt = ha->request_q_length - 379 (ha->req_ring_index - cnt); 380 } 381 if (ha->req_q_cnt < (req_cnt + 2)) 382 goto queuing_error; 383 384 /* Build command packet */ 385 ha->current_outstanding_cmd = handle; 386 ha->outstanding_cmds[handle] = sp; 387 sp->ha = ha; 388 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; 389 ha->req_q_cnt -= req_cnt; 390 391 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr; 392 cmd_pkt->handle = handle; 393 /* Zero out remaining portion of packet. */ 394 clr_ptr = (uint32_t *)cmd_pkt + 2; 395 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 396 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 397 398 /* Set target ID and LUN number*/ 399 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id); 400 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun); 401 402 /* Update tagged queuing modifier */ 403 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG); 404 if (scsi_populate_tag_msg(cmd, tag)) { 405 switch (tag[0]) { 406 case MSG_HEAD_TAG: 407 cmd_pkt->control_flags = 408 __constant_cpu_to_le16(CF_HEAD_TAG); 409 break; 410 case MSG_ORDERED_TAG: 411 cmd_pkt->control_flags = 412 __constant_cpu_to_le16(CF_ORDERED_TAG); 413 break; 414 } 415 } 416 417 /* Load SCSI command packet. */ 418 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len); 419 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen); 420 421 /* Build IOCB segments */ 422 ha->isp_ops.build_iocbs(sp, cmd_pkt, tot_dsds); 423 424 /* Set total data segment count. */ 425 cmd_pkt->entry_count = (uint8_t)req_cnt; 426 wmb(); 427 428 /* Adjust ring index. */ 429 ha->req_ring_index++; 430 if (ha->req_ring_index == ha->request_q_length) { 431 ha->req_ring_index = 0; 432 ha->request_ring_ptr = ha->request_ring; 433 } else 434 ha->request_ring_ptr++; 435 436 sp->flags |= SRB_DMA_VALID; 437 sp->state = SRB_ACTIVE_STATE; 438 439 /* Set chip new ring index. */ 440 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index); 441 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */ 442 443 spin_unlock_irqrestore(&ha->hardware_lock, flags); 444 return (QLA_SUCCESS); 445 446 queuing_error: 447 if (cmd->use_sg && tot_dsds) { 448 sg = (struct scatterlist *) cmd->request_buffer; 449 pci_unmap_sg(ha->pdev, sg, cmd->use_sg, 450 cmd->sc_data_direction); 451 } else if (tot_dsds) { 452 pci_unmap_single(ha->pdev, sp->dma_handle, 453 cmd->request_bufflen, cmd->sc_data_direction); 454 } 455 spin_unlock_irqrestore(&ha->hardware_lock, flags); 456 457 return (QLA_FUNCTION_FAILED); 458 } 459 460 /** 461 * qla2x00_marker() - Send a marker IOCB to the firmware. 462 * @ha: HA context 463 * @loop_id: loop ID 464 * @lun: LUN 465 * @type: marker modifier 466 * 467 * Can be called from both normal and interrupt context. 468 * 469 * Returns non-zero if a failure occured, else zero. 470 */ 471 int 472 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun, 473 uint8_t type) 474 { 475 mrk_entry_t *mrk; 476 struct mrk_entry_24xx *mrk24; 477 478 mrk24 = NULL; 479 mrk = (mrk_entry_t *)qla2x00_req_pkt(ha); 480 if (mrk == NULL) { 481 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n", 482 __func__, ha->host_no)); 483 484 return (QLA_FUNCTION_FAILED); 485 } 486 487 mrk->entry_type = MARKER_TYPE; 488 mrk->modifier = type; 489 if (type != MK_SYNC_ALL) { 490 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) { 491 mrk24 = (struct mrk_entry_24xx *) mrk; 492 mrk24->nport_handle = cpu_to_le16(loop_id); 493 mrk24->lun[1] = LSB(lun); 494 mrk24->lun[2] = MSB(lun); 495 } else { 496 SET_TARGET_ID(ha, mrk->target, loop_id); 497 mrk->lun = cpu_to_le16(lun); 498 } 499 } 500 wmb(); 501 502 qla2x00_isp_cmd(ha); 503 504 return (QLA_SUCCESS); 505 } 506 507 int 508 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun, 509 uint8_t type) 510 { 511 int ret; 512 unsigned long flags = 0; 513 514 spin_lock_irqsave(&ha->hardware_lock, flags); 515 ret = __qla2x00_marker(ha, loop_id, lun, type); 516 spin_unlock_irqrestore(&ha->hardware_lock, flags); 517 518 return (ret); 519 } 520 521 /** 522 * qla2x00_req_pkt() - Retrieve a request packet from the request ring. 523 * @ha: HA context 524 * 525 * Note: The caller must hold the hardware lock before calling this routine. 526 * 527 * Returns NULL if function failed, else, a pointer to the request packet. 528 */ 529 static request_t * 530 qla2x00_req_pkt(scsi_qla_host_t *ha) 531 { 532 device_reg_t __iomem *reg = ha->iobase; 533 request_t *pkt = NULL; 534 uint16_t cnt; 535 uint32_t *dword_ptr; 536 uint32_t timer; 537 uint16_t req_cnt = 1; 538 539 /* Wait 1 second for slot. */ 540 for (timer = HZ; timer; timer--) { 541 if ((req_cnt + 2) >= ha->req_q_cnt) { 542 /* Calculate number of free request entries. */ 543 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) 544 cnt = (uint16_t)RD_REG_DWORD( 545 ®->isp24.req_q_out); 546 else 547 cnt = qla2x00_debounce_register( 548 ISP_REQ_Q_OUT(ha, ®->isp)); 549 if (ha->req_ring_index < cnt) 550 ha->req_q_cnt = cnt - ha->req_ring_index; 551 else 552 ha->req_q_cnt = ha->request_q_length - 553 (ha->req_ring_index - cnt); 554 } 555 /* If room for request in request ring. */ 556 if ((req_cnt + 2) < ha->req_q_cnt) { 557 ha->req_q_cnt--; 558 pkt = ha->request_ring_ptr; 559 560 /* Zero out packet. */ 561 dword_ptr = (uint32_t *)pkt; 562 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++) 563 *dword_ptr++ = 0; 564 565 /* Set system defined field. */ 566 pkt->sys_define = (uint8_t)ha->req_ring_index; 567 568 /* Set entry count. */ 569 pkt->entry_count = 1; 570 571 break; 572 } 573 574 /* Release ring specific lock */ 575 spin_unlock(&ha->hardware_lock); 576 577 udelay(2); /* 2 us */ 578 579 /* Check for pending interrupts. */ 580 /* During init we issue marker directly */ 581 if (!ha->marker_needed) 582 qla2x00_poll(ha); 583 584 spin_lock_irq(&ha->hardware_lock); 585 } 586 if (!pkt) { 587 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__)); 588 } 589 590 return (pkt); 591 } 592 593 /** 594 * qla2x00_isp_cmd() - Modify the request ring pointer. 595 * @ha: HA context 596 * 597 * Note: The caller must hold the hardware lock before calling this routine. 598 */ 599 void 600 qla2x00_isp_cmd(scsi_qla_host_t *ha) 601 { 602 device_reg_t __iomem *reg = ha->iobase; 603 604 DEBUG5(printk("%s(): IOCB data:\n", __func__)); 605 DEBUG5(qla2x00_dump_buffer( 606 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE)); 607 608 /* Adjust ring index. */ 609 ha->req_ring_index++; 610 if (ha->req_ring_index == ha->request_q_length) { 611 ha->req_ring_index = 0; 612 ha->request_ring_ptr = ha->request_ring; 613 } else 614 ha->request_ring_ptr++; 615 616 /* Set chip new ring index. */ 617 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) { 618 WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index); 619 RD_REG_DWORD_RELAXED(®->isp24.req_q_in); 620 } else { 621 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index); 622 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp)); 623 } 624 625 } 626 627 /** 628 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and 629 * Continuation Type 1 IOCBs to allocate. 630 * 631 * @dsds: number of data segment decriptors needed 632 * 633 * Returns the number of IOCB entries needed to store @dsds. 634 */ 635 static inline uint16_t 636 qla24xx_calc_iocbs(uint16_t dsds) 637 { 638 uint16_t iocbs; 639 640 iocbs = 1; 641 if (dsds > 1) { 642 iocbs += (dsds - 1) / 5; 643 if ((dsds - 1) % 5) 644 iocbs++; 645 } 646 return iocbs; 647 } 648 649 /** 650 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7 651 * IOCB types. 652 * 653 * @sp: SRB command to process 654 * @cmd_pkt: Command type 3 IOCB 655 * @tot_dsds: Total number of segments to transfer 656 */ 657 static inline void 658 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt, 659 uint16_t tot_dsds) 660 { 661 uint16_t avail_dsds; 662 uint32_t *cur_dsd; 663 scsi_qla_host_t *ha; 664 struct scsi_cmnd *cmd; 665 666 cmd = sp->cmd; 667 668 /* Update entry type to indicate Command Type 3 IOCB */ 669 *((uint32_t *)(&cmd_pkt->entry_type)) = 670 __constant_cpu_to_le32(COMMAND_TYPE_7); 671 672 /* No data transfer */ 673 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) { 674 cmd_pkt->byte_count = __constant_cpu_to_le32(0); 675 return; 676 } 677 678 ha = sp->ha; 679 680 /* Set transfer direction */ 681 if (cmd->sc_data_direction == DMA_TO_DEVICE) 682 cmd_pkt->task_mgmt_flags = 683 __constant_cpu_to_le16(TMF_WRITE_DATA); 684 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 685 cmd_pkt->task_mgmt_flags = 686 __constant_cpu_to_le16(TMF_READ_DATA); 687 688 /* One DSD is available in the Command Type 3 IOCB */ 689 avail_dsds = 1; 690 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address; 691 692 /* Load data segments */ 693 if (cmd->use_sg != 0) { 694 struct scatterlist *cur_seg; 695 struct scatterlist *end_seg; 696 697 cur_seg = (struct scatterlist *)cmd->request_buffer; 698 end_seg = cur_seg + tot_dsds; 699 while (cur_seg < end_seg) { 700 dma_addr_t sle_dma; 701 cont_a64_entry_t *cont_pkt; 702 703 /* Allocate additional continuation packets? */ 704 if (avail_dsds == 0) { 705 /* 706 * Five DSDs are available in the Continuation 707 * Type 1 IOCB. 708 */ 709 cont_pkt = qla2x00_prep_cont_type1_iocb(ha); 710 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address; 711 avail_dsds = 5; 712 } 713 714 sle_dma = sg_dma_address(cur_seg); 715 *cur_dsd++ = cpu_to_le32(LSD(sle_dma)); 716 *cur_dsd++ = cpu_to_le32(MSD(sle_dma)); 717 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg)); 718 avail_dsds--; 719 720 cur_seg++; 721 } 722 } else { 723 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle)); 724 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle)); 725 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen); 726 } 727 } 728 729 730 /** 731 * qla24xx_start_scsi() - Send a SCSI command to the ISP 732 * @sp: command to send to the ISP 733 * 734 * Returns non-zero if a failure occured, else zero. 735 */ 736 int 737 qla24xx_start_scsi(srb_t *sp) 738 { 739 int ret; 740 unsigned long flags; 741 scsi_qla_host_t *ha; 742 struct scsi_cmnd *cmd; 743 uint32_t *clr_ptr; 744 uint32_t index; 745 uint32_t handle; 746 struct cmd_type_7 *cmd_pkt; 747 struct scatterlist *sg; 748 uint16_t cnt; 749 uint16_t req_cnt; 750 uint16_t tot_dsds; 751 struct device_reg_24xx __iomem *reg; 752 char tag[2]; 753 754 /* Setup device pointers. */ 755 ret = 0; 756 ha = sp->ha; 757 reg = &ha->iobase->isp24; 758 cmd = sp->cmd; 759 /* So we know we haven't pci_map'ed anything yet */ 760 tot_dsds = 0; 761 762 /* Send marker if required */ 763 if (ha->marker_needed != 0) { 764 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) { 765 return QLA_FUNCTION_FAILED; 766 } 767 ha->marker_needed = 0; 768 } 769 770 /* Acquire ring specific lock */ 771 spin_lock_irqsave(&ha->hardware_lock, flags); 772 773 /* Check for room in outstanding command list. */ 774 handle = ha->current_outstanding_cmd; 775 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) { 776 handle++; 777 if (handle == MAX_OUTSTANDING_COMMANDS) 778 handle = 1; 779 if (ha->outstanding_cmds[handle] == 0) 780 break; 781 } 782 if (index == MAX_OUTSTANDING_COMMANDS) 783 goto queuing_error; 784 785 /* Map the sg table so we have an accurate count of sg entries needed */ 786 if (cmd->use_sg) { 787 sg = (struct scatterlist *) cmd->request_buffer; 788 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg, 789 cmd->sc_data_direction); 790 if (tot_dsds == 0) 791 goto queuing_error; 792 } else if (cmd->request_bufflen) { 793 dma_addr_t req_dma; 794 795 req_dma = pci_map_single(ha->pdev, cmd->request_buffer, 796 cmd->request_bufflen, cmd->sc_data_direction); 797 if (dma_mapping_error(req_dma)) 798 goto queuing_error; 799 800 sp->dma_handle = req_dma; 801 tot_dsds = 1; 802 } 803 804 req_cnt = qla24xx_calc_iocbs(tot_dsds); 805 if (ha->req_q_cnt < (req_cnt + 2)) { 806 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out); 807 if (ha->req_ring_index < cnt) 808 ha->req_q_cnt = cnt - ha->req_ring_index; 809 else 810 ha->req_q_cnt = ha->request_q_length - 811 (ha->req_ring_index - cnt); 812 } 813 if (ha->req_q_cnt < (req_cnt + 2)) 814 goto queuing_error; 815 816 /* Build command packet. */ 817 ha->current_outstanding_cmd = handle; 818 ha->outstanding_cmds[handle] = sp; 819 sp->ha = ha; 820 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle; 821 ha->req_q_cnt -= req_cnt; 822 823 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr; 824 cmd_pkt->handle = handle; 825 826 /* Zero out remaining portion of packet. */ 827 clr_ptr = (uint32_t *)cmd_pkt + 2; 828 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8); 829 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds); 830 831 /* Set NPORT-ID and LUN number*/ 832 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id); 833 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa; 834 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area; 835 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain; 836 837 cmd_pkt->lun[1] = LSB(sp->cmd->device->lun); 838 cmd_pkt->lun[2] = MSB(sp->cmd->device->lun); 839 840 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */ 841 if (scsi_populate_tag_msg(cmd, tag)) { 842 switch (tag[0]) { 843 case MSG_HEAD_TAG: 844 cmd_pkt->task = TSK_HEAD_OF_QUEUE; 845 break; 846 case MSG_ORDERED_TAG: 847 cmd_pkt->task = TSK_ORDERED; 848 break; 849 } 850 } 851 852 /* Load SCSI command packet. */ 853 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len); 854 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb)); 855 856 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen); 857 858 /* Build IOCB segments */ 859 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds); 860 861 /* Set total data segment count. */ 862 cmd_pkt->entry_count = (uint8_t)req_cnt; 863 wmb(); 864 865 /* Adjust ring index. */ 866 ha->req_ring_index++; 867 if (ha->req_ring_index == ha->request_q_length) { 868 ha->req_ring_index = 0; 869 ha->request_ring_ptr = ha->request_ring; 870 } else 871 ha->request_ring_ptr++; 872 873 sp->flags |= SRB_DMA_VALID; 874 sp->state = SRB_ACTIVE_STATE; 875 876 /* Set chip new ring index. */ 877 WRT_REG_DWORD(®->req_q_in, ha->req_ring_index); 878 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */ 879 880 spin_unlock_irqrestore(&ha->hardware_lock, flags); 881 return QLA_SUCCESS; 882 883 queuing_error: 884 if (cmd->use_sg && tot_dsds) { 885 sg = (struct scatterlist *) cmd->request_buffer; 886 pci_unmap_sg(ha->pdev, sg, cmd->use_sg, 887 cmd->sc_data_direction); 888 } else if (tot_dsds) { 889 pci_unmap_single(ha->pdev, sp->dma_handle, 890 cmd->request_bufflen, cmd->sc_data_direction); 891 } 892 spin_unlock_irqrestore(&ha->hardware_lock, flags); 893 894 return QLA_FUNCTION_FAILED; 895 } 896