qla_iocb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /******************************************************************************
  2. * QLOGIC LINUX SOFTWARE
  3. *
  4. * QLogic ISP2x00 device driver for Linux 2.6.x
  5. * Copyright (C) 2003-2004 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. #include "qla_def.h"
  20. #include <linux/blkdev.h>
  21. #include <linux/delay.h>
  22. #include <scsi/scsi_tcq.h>
  23. static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
  24. static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
  25. static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
  26. static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
  27. /**
  28. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  29. * @cmd: SCSI command
  30. *
  31. * Returns the proper CF_* direction based on CDB.
  32. */
  33. static inline uint16_t
  34. qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
  35. {
  36. uint16_t cflags;
  37. cflags = 0;
  38. /* Set transfer direction */
  39. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  40. cflags = CF_WRITE;
  41. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  42. cflags = CF_READ;
  43. return (cflags);
  44. }
  45. /**
  46. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  47. * Continuation Type 0 IOCBs to allocate.
  48. *
  49. * @dsds: number of data segment decriptors needed
  50. *
  51. * Returns the number of IOCB entries needed to store @dsds.
  52. */
  53. uint16_t
  54. qla2x00_calc_iocbs_32(uint16_t dsds)
  55. {
  56. uint16_t iocbs;
  57. iocbs = 1;
  58. if (dsds > 3) {
  59. iocbs += (dsds - 3) / 7;
  60. if ((dsds - 3) % 7)
  61. iocbs++;
  62. }
  63. return (iocbs);
  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. iocbs = 1;
  78. if (dsds > 2) {
  79. iocbs += (dsds - 2) / 5;
  80. if ((dsds - 2) % 5)
  81. iocbs++;
  82. }
  83. return (iocbs);
  84. }
  85. /**
  86. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  87. * @ha: HA context
  88. *
  89. * Returns a pointer to the Continuation Type 0 IOCB packet.
  90. */
  91. static inline cont_entry_t *
  92. qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
  93. {
  94. cont_entry_t *cont_pkt;
  95. /* Adjust ring index. */
  96. ha->req_ring_index++;
  97. if (ha->req_ring_index == ha->request_q_length) {
  98. ha->req_ring_index = 0;
  99. ha->request_ring_ptr = ha->request_ring;
  100. } else {
  101. ha->request_ring_ptr++;
  102. }
  103. cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
  104. /* Load packet defaults. */
  105. *((uint32_t *)(&cont_pkt->entry_type)) =
  106. __constant_cpu_to_le32(CONTINUE_TYPE);
  107. return (cont_pkt);
  108. }
  109. /**
  110. * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
  111. * @ha: HA context
  112. *
  113. * Returns a pointer to the continuation type 1 IOCB packet.
  114. */
  115. static inline cont_a64_entry_t *
  116. qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
  117. {
  118. cont_a64_entry_t *cont_pkt;
  119. /* Adjust ring index. */
  120. ha->req_ring_index++;
  121. if (ha->req_ring_index == ha->request_q_length) {
  122. ha->req_ring_index = 0;
  123. ha->request_ring_ptr = ha->request_ring;
  124. } else {
  125. ha->request_ring_ptr++;
  126. }
  127. cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
  128. /* Load packet defaults. */
  129. *((uint32_t *)(&cont_pkt->entry_type)) =
  130. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  131. return (cont_pkt);
  132. }
  133. /**
  134. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  135. * capable IOCB types.
  136. *
  137. * @sp: SRB command to process
  138. * @cmd_pkt: Command type 2 IOCB
  139. * @tot_dsds: Total number of segments to transfer
  140. */
  141. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  142. uint16_t tot_dsds)
  143. {
  144. uint16_t avail_dsds;
  145. uint32_t *cur_dsd;
  146. scsi_qla_host_t *ha;
  147. struct scsi_cmnd *cmd;
  148. cmd = sp->cmd;
  149. /* Update entry type to indicate Command Type 2 IOCB */
  150. *((uint32_t *)(&cmd_pkt->entry_type)) =
  151. __constant_cpu_to_le32(COMMAND_TYPE);
  152. /* No data transfer */
  153. if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
  154. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  155. return;
  156. }
  157. ha = sp->ha;
  158. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
  159. /* Three DSDs are available in the Command Type 2 IOCB */
  160. avail_dsds = 3;
  161. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  162. /* Load data segments */
  163. if (cmd->use_sg != 0) {
  164. struct scatterlist *cur_seg;
  165. struct scatterlist *end_seg;
  166. cur_seg = (struct scatterlist *)cmd->request_buffer;
  167. end_seg = cur_seg + tot_dsds;
  168. while (cur_seg < end_seg) {
  169. cont_entry_t *cont_pkt;
  170. /* Allocate additional continuation packets? */
  171. if (avail_dsds == 0) {
  172. /*
  173. * Seven DSDs are available in the Continuation
  174. * Type 0 IOCB.
  175. */
  176. cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
  177. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  178. avail_dsds = 7;
  179. }
  180. *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
  181. *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
  182. avail_dsds--;
  183. cur_seg++;
  184. }
  185. } else {
  186. *cur_dsd++ = cpu_to_le32(sp->dma_handle);
  187. *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
  188. }
  189. }
  190. /**
  191. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  192. * capable IOCB types.
  193. *
  194. * @sp: SRB command to process
  195. * @cmd_pkt: Command type 3 IOCB
  196. * @tot_dsds: Total number of segments to transfer
  197. */
  198. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  199. uint16_t tot_dsds)
  200. {
  201. uint16_t avail_dsds;
  202. uint32_t *cur_dsd;
  203. scsi_qla_host_t *ha;
  204. struct scsi_cmnd *cmd;
  205. cmd = sp->cmd;
  206. /* Update entry type to indicate Command Type 3 IOCB */
  207. *((uint32_t *)(&cmd_pkt->entry_type)) =
  208. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  209. /* No data transfer */
  210. if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
  211. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  212. return;
  213. }
  214. ha = sp->ha;
  215. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
  216. /* Two DSDs are available in the Command Type 3 IOCB */
  217. avail_dsds = 2;
  218. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  219. /* Load data segments */
  220. if (cmd->use_sg != 0) {
  221. struct scatterlist *cur_seg;
  222. struct scatterlist *end_seg;
  223. cur_seg = (struct scatterlist *)cmd->request_buffer;
  224. end_seg = cur_seg + tot_dsds;
  225. while (cur_seg < end_seg) {
  226. dma_addr_t sle_dma;
  227. cont_a64_entry_t *cont_pkt;
  228. /* Allocate additional continuation packets? */
  229. if (avail_dsds == 0) {
  230. /*
  231. * Five DSDs are available in the Continuation
  232. * Type 1 IOCB.
  233. */
  234. cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
  235. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  236. avail_dsds = 5;
  237. }
  238. sle_dma = sg_dma_address(cur_seg);
  239. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  240. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  241. *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
  242. avail_dsds--;
  243. cur_seg++;
  244. }
  245. } else {
  246. *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
  247. *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
  248. *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
  249. }
  250. }
  251. /**
  252. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  253. * @sp: command to send to the ISP
  254. *
  255. * Returns non-zero if a failure occured, else zero.
  256. */
  257. int
  258. qla2x00_start_scsi(srb_t *sp)
  259. {
  260. int ret;
  261. unsigned long flags;
  262. scsi_qla_host_t *ha;
  263. struct scsi_cmnd *cmd;
  264. uint32_t *clr_ptr;
  265. uint32_t index;
  266. uint32_t handle;
  267. cmd_entry_t *cmd_pkt;
  268. struct scatterlist *sg;
  269. uint16_t cnt;
  270. uint16_t req_cnt;
  271. uint16_t tot_dsds;
  272. device_reg_t __iomem *reg;
  273. char tag[2];
  274. /* Setup device pointers. */
  275. ret = 0;
  276. ha = sp->ha;
  277. reg = ha->iobase;
  278. cmd = sp->cmd;
  279. /* So we know we haven't pci_map'ed anything yet */
  280. tot_dsds = 0;
  281. /* Send marker if required */
  282. if (ha->marker_needed != 0) {
  283. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  284. return (QLA_FUNCTION_FAILED);
  285. }
  286. ha->marker_needed = 0;
  287. }
  288. /* Acquire ring specific lock */
  289. spin_lock_irqsave(&ha->hardware_lock, flags);
  290. /* Check for room in outstanding command list. */
  291. handle = ha->current_outstanding_cmd;
  292. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  293. handle++;
  294. if (handle == MAX_OUTSTANDING_COMMANDS)
  295. handle = 1;
  296. if (ha->outstanding_cmds[handle] == 0)
  297. break;
  298. }
  299. if (index == MAX_OUTSTANDING_COMMANDS)
  300. goto queuing_error;
  301. /* Map the sg table so we have an accurate count of sg entries needed */
  302. if (cmd->use_sg) {
  303. sg = (struct scatterlist *) cmd->request_buffer;
  304. tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
  305. cmd->sc_data_direction);
  306. if (tot_dsds == 0)
  307. goto queuing_error;
  308. } else if (cmd->request_bufflen) {
  309. dma_addr_t req_dma;
  310. req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
  311. cmd->request_bufflen, cmd->sc_data_direction);
  312. if (dma_mapping_error(req_dma))
  313. goto queuing_error;
  314. sp->dma_handle = req_dma;
  315. tot_dsds = 1;
  316. }
  317. /* Calculate the number of request entries needed. */
  318. req_cnt = (ha->calc_request_entries)(tot_dsds);
  319. if (ha->req_q_cnt < (req_cnt + 2)) {
  320. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  321. if (ha->req_ring_index < cnt)
  322. ha->req_q_cnt = cnt - ha->req_ring_index;
  323. else
  324. ha->req_q_cnt = ha->request_q_length -
  325. (ha->req_ring_index - cnt);
  326. }
  327. if (ha->req_q_cnt < (req_cnt + 2))
  328. goto queuing_error;
  329. /* Build command packet */
  330. ha->current_outstanding_cmd = handle;
  331. ha->outstanding_cmds[handle] = sp;
  332. sp->ha = ha;
  333. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  334. ha->req_q_cnt -= req_cnt;
  335. cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
  336. cmd_pkt->handle = handle;
  337. /* Zero out remaining portion of packet. */
  338. clr_ptr = (uint32_t *)cmd_pkt + 2;
  339. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  340. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  341. /* Set target ID and LUN number*/
  342. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  343. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  344. /* Update tagged queuing modifier */
  345. cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
  346. if (scsi_populate_tag_msg(cmd, tag)) {
  347. switch (tag[0]) {
  348. case MSG_HEAD_TAG:
  349. cmd_pkt->control_flags =
  350. __constant_cpu_to_le16(CF_HEAD_TAG);
  351. break;
  352. case MSG_ORDERED_TAG:
  353. cmd_pkt->control_flags =
  354. __constant_cpu_to_le16(CF_ORDERED_TAG);
  355. break;
  356. }
  357. }
  358. /* Load SCSI command packet. */
  359. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  360. cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
  361. /* Build IOCB segments */
  362. (ha->build_scsi_iocbs)(sp, cmd_pkt, tot_dsds);
  363. /* Set total data segment count. */
  364. cmd_pkt->entry_count = (uint8_t)req_cnt;
  365. wmb();
  366. /* Adjust ring index. */
  367. ha->req_ring_index++;
  368. if (ha->req_ring_index == ha->request_q_length) {
  369. ha->req_ring_index = 0;
  370. ha->request_ring_ptr = ha->request_ring;
  371. } else
  372. ha->request_ring_ptr++;
  373. sp->flags |= SRB_DMA_VALID;
  374. sp->state = SRB_ACTIVE_STATE;
  375. /* Set chip new ring index. */
  376. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
  377. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  378. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  379. return (QLA_SUCCESS);
  380. queuing_error:
  381. if (cmd->use_sg && tot_dsds) {
  382. sg = (struct scatterlist *) cmd->request_buffer;
  383. pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
  384. cmd->sc_data_direction);
  385. } else if (tot_dsds) {
  386. pci_unmap_single(ha->pdev, sp->dma_handle,
  387. cmd->request_bufflen, cmd->sc_data_direction);
  388. }
  389. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  390. return (QLA_FUNCTION_FAILED);
  391. }
  392. /**
  393. * qla2x00_marker() - Send a marker IOCB to the firmware.
  394. * @ha: HA context
  395. * @loop_id: loop ID
  396. * @lun: LUN
  397. * @type: marker modifier
  398. *
  399. * Can be called from both normal and interrupt context.
  400. *
  401. * Returns non-zero if a failure occured, else zero.
  402. */
  403. int
  404. __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  405. uint8_t type)
  406. {
  407. mrk_entry_t *pkt;
  408. pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
  409. if (pkt == NULL) {
  410. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  411. return (QLA_FUNCTION_FAILED);
  412. }
  413. pkt->entry_type = MARKER_TYPE;
  414. pkt->modifier = type;
  415. if (type != MK_SYNC_ALL) {
  416. pkt->lun = cpu_to_le16(lun);
  417. SET_TARGET_ID(ha, pkt->target, loop_id);
  418. }
  419. wmb();
  420. /* Issue command to ISP */
  421. qla2x00_isp_cmd(ha);
  422. return (QLA_SUCCESS);
  423. }
  424. int
  425. qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  426. uint8_t type)
  427. {
  428. int ret;
  429. unsigned long flags = 0;
  430. spin_lock_irqsave(&ha->hardware_lock, flags);
  431. ret = __qla2x00_marker(ha, loop_id, lun, type);
  432. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  433. return (ret);
  434. }
  435. /**
  436. * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
  437. * @ha: HA context
  438. *
  439. * Note: The caller must hold the hardware lock before calling this routine.
  440. *
  441. * Returns NULL if function failed, else, a pointer to the request packet.
  442. */
  443. static request_t *
  444. qla2x00_req_pkt(scsi_qla_host_t *ha)
  445. {
  446. device_reg_t __iomem *reg = ha->iobase;
  447. request_t *pkt = NULL;
  448. uint16_t cnt;
  449. uint32_t *dword_ptr;
  450. uint32_t timer;
  451. uint16_t req_cnt = 1;
  452. /* Wait 1 second for slot. */
  453. for (timer = HZ; timer; timer--) {
  454. if ((req_cnt + 2) >= ha->req_q_cnt) {
  455. /* Calculate number of free request entries. */
  456. cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
  457. if (ha->req_ring_index < cnt)
  458. ha->req_q_cnt = cnt - ha->req_ring_index;
  459. else
  460. ha->req_q_cnt = ha->request_q_length -
  461. (ha->req_ring_index - cnt);
  462. }
  463. /* If room for request in request ring. */
  464. if ((req_cnt + 2) < ha->req_q_cnt) {
  465. ha->req_q_cnt--;
  466. pkt = ha->request_ring_ptr;
  467. /* Zero out packet. */
  468. dword_ptr = (uint32_t *)pkt;
  469. for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
  470. *dword_ptr++ = 0;
  471. /* Set system defined field. */
  472. pkt->sys_define = (uint8_t)ha->req_ring_index;
  473. /* Set entry count. */
  474. pkt->entry_count = 1;
  475. break;
  476. }
  477. /* Release ring specific lock */
  478. spin_unlock(&ha->hardware_lock);
  479. udelay(2); /* 2 us */
  480. /* Check for pending interrupts. */
  481. /* During init we issue marker directly */
  482. if (!ha->marker_needed)
  483. qla2x00_poll(ha);
  484. spin_lock_irq(&ha->hardware_lock);
  485. }
  486. if (!pkt) {
  487. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  488. }
  489. return (pkt);
  490. }
  491. /**
  492. * qla2x00_isp_cmd() - Modify the request ring pointer.
  493. * @ha: HA context
  494. *
  495. * Note: The caller must hold the hardware lock before calling this routine.
  496. */
  497. void
  498. qla2x00_isp_cmd(scsi_qla_host_t *ha)
  499. {
  500. device_reg_t __iomem *reg = ha->iobase;
  501. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  502. DEBUG5(qla2x00_dump_buffer(
  503. (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
  504. /* Adjust ring index. */
  505. ha->req_ring_index++;
  506. if (ha->req_ring_index == ha->request_q_length) {
  507. ha->req_ring_index = 0;
  508. ha->request_ring_ptr = ha->request_ring;
  509. } else
  510. ha->request_ring_ptr++;
  511. /* Set chip new ring index. */
  512. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
  513. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  514. }