qla_iocb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2008 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/blkdev.h>
  9. #include <linux/delay.h>
  10. #include <scsi/scsi_tcq.h>
  11. static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
  12. static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
  13. /**
  14. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  15. * @cmd: SCSI command
  16. *
  17. * Returns the proper CF_* direction based on CDB.
  18. */
  19. static inline uint16_t
  20. qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
  21. {
  22. uint16_t cflags;
  23. cflags = 0;
  24. /* Set transfer direction */
  25. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  26. cflags = CF_WRITE;
  27. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  28. cflags = CF_READ;
  29. return (cflags);
  30. }
  31. /**
  32. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  33. * Continuation Type 0 IOCBs to allocate.
  34. *
  35. * @dsds: number of data segment decriptors needed
  36. *
  37. * Returns the number of IOCB entries needed to store @dsds.
  38. */
  39. uint16_t
  40. qla2x00_calc_iocbs_32(uint16_t dsds)
  41. {
  42. uint16_t iocbs;
  43. iocbs = 1;
  44. if (dsds > 3) {
  45. iocbs += (dsds - 3) / 7;
  46. if ((dsds - 3) % 7)
  47. iocbs++;
  48. }
  49. return (iocbs);
  50. }
  51. /**
  52. * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
  53. * Continuation Type 1 IOCBs to allocate.
  54. *
  55. * @dsds: number of data segment decriptors needed
  56. *
  57. * Returns the number of IOCB entries needed to store @dsds.
  58. */
  59. uint16_t
  60. qla2x00_calc_iocbs_64(uint16_t dsds)
  61. {
  62. uint16_t iocbs;
  63. iocbs = 1;
  64. if (dsds > 2) {
  65. iocbs += (dsds - 2) / 5;
  66. if ((dsds - 2) % 5)
  67. iocbs++;
  68. }
  69. return (iocbs);
  70. }
  71. /**
  72. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  73. * @ha: HA context
  74. *
  75. * Returns a pointer to the Continuation Type 0 IOCB packet.
  76. */
  77. static inline cont_entry_t *
  78. qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
  79. {
  80. cont_entry_t *cont_pkt;
  81. /* Adjust ring index. */
  82. ha->req_ring_index++;
  83. if (ha->req_ring_index == ha->request_q_length) {
  84. ha->req_ring_index = 0;
  85. ha->request_ring_ptr = ha->request_ring;
  86. } else {
  87. ha->request_ring_ptr++;
  88. }
  89. cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
  90. /* Load packet defaults. */
  91. *((uint32_t *)(&cont_pkt->entry_type)) =
  92. __constant_cpu_to_le32(CONTINUE_TYPE);
  93. return (cont_pkt);
  94. }
  95. /**
  96. * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
  97. * @ha: HA context
  98. *
  99. * Returns a pointer to the continuation type 1 IOCB packet.
  100. */
  101. static inline cont_a64_entry_t *
  102. qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
  103. {
  104. cont_a64_entry_t *cont_pkt;
  105. /* Adjust ring index. */
  106. ha->req_ring_index++;
  107. if (ha->req_ring_index == ha->request_q_length) {
  108. ha->req_ring_index = 0;
  109. ha->request_ring_ptr = ha->request_ring;
  110. } else {
  111. ha->request_ring_ptr++;
  112. }
  113. cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
  114. /* Load packet defaults. */
  115. *((uint32_t *)(&cont_pkt->entry_type)) =
  116. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  117. return (cont_pkt);
  118. }
  119. /**
  120. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  121. * capable IOCB types.
  122. *
  123. * @sp: SRB command to process
  124. * @cmd_pkt: Command type 2 IOCB
  125. * @tot_dsds: Total number of segments to transfer
  126. */
  127. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  128. uint16_t tot_dsds)
  129. {
  130. uint16_t avail_dsds;
  131. uint32_t *cur_dsd;
  132. scsi_qla_host_t *ha;
  133. struct scsi_cmnd *cmd;
  134. struct scatterlist *sg;
  135. int i;
  136. cmd = sp->cmd;
  137. /* Update entry type to indicate Command Type 2 IOCB */
  138. *((uint32_t *)(&cmd_pkt->entry_type)) =
  139. __constant_cpu_to_le32(COMMAND_TYPE);
  140. /* No data transfer */
  141. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  142. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  143. return;
  144. }
  145. ha = sp->ha;
  146. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
  147. /* Three DSDs are available in the Command Type 2 IOCB */
  148. avail_dsds = 3;
  149. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  150. /* Load data segments */
  151. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  152. cont_entry_t *cont_pkt;
  153. /* Allocate additional continuation packets? */
  154. if (avail_dsds == 0) {
  155. /*
  156. * Seven DSDs are available in the Continuation
  157. * Type 0 IOCB.
  158. */
  159. cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
  160. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  161. avail_dsds = 7;
  162. }
  163. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  164. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  165. avail_dsds--;
  166. }
  167. }
  168. /**
  169. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  170. * capable IOCB types.
  171. *
  172. * @sp: SRB command to process
  173. * @cmd_pkt: Command type 3 IOCB
  174. * @tot_dsds: Total number of segments to transfer
  175. */
  176. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  177. uint16_t tot_dsds)
  178. {
  179. uint16_t avail_dsds;
  180. uint32_t *cur_dsd;
  181. scsi_qla_host_t *ha;
  182. struct scsi_cmnd *cmd;
  183. struct scatterlist *sg;
  184. int i;
  185. cmd = sp->cmd;
  186. /* Update entry type to indicate Command Type 3 IOCB */
  187. *((uint32_t *)(&cmd_pkt->entry_type)) =
  188. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  189. /* No data transfer */
  190. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  191. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  192. return;
  193. }
  194. ha = sp->ha;
  195. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
  196. /* Two DSDs are available in the Command Type 3 IOCB */
  197. avail_dsds = 2;
  198. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  199. /* Load data segments */
  200. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  201. dma_addr_t sle_dma;
  202. cont_a64_entry_t *cont_pkt;
  203. /* Allocate additional continuation packets? */
  204. if (avail_dsds == 0) {
  205. /*
  206. * Five DSDs are available in the Continuation
  207. * Type 1 IOCB.
  208. */
  209. cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
  210. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  211. avail_dsds = 5;
  212. }
  213. sle_dma = sg_dma_address(sg);
  214. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  215. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  216. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  217. avail_dsds--;
  218. }
  219. }
  220. /**
  221. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  222. * @sp: command to send to the ISP
  223. *
  224. * Returns non-zero if a failure occured, else zero.
  225. */
  226. int
  227. qla2x00_start_scsi(srb_t *sp)
  228. {
  229. int ret, nseg;
  230. unsigned long flags;
  231. scsi_qla_host_t *ha, *pha;
  232. struct scsi_cmnd *cmd;
  233. uint32_t *clr_ptr;
  234. uint32_t index;
  235. uint32_t handle;
  236. cmd_entry_t *cmd_pkt;
  237. uint16_t cnt;
  238. uint16_t req_cnt;
  239. uint16_t tot_dsds;
  240. struct device_reg_2xxx __iomem *reg;
  241. /* Setup device pointers. */
  242. ret = 0;
  243. ha = sp->ha;
  244. pha = to_qla_parent(ha);
  245. reg = &ha->iobase->isp;
  246. cmd = sp->cmd;
  247. /* So we know we haven't pci_map'ed anything yet */
  248. tot_dsds = 0;
  249. /* Send marker if required */
  250. if (ha->marker_needed != 0) {
  251. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  252. return (QLA_FUNCTION_FAILED);
  253. }
  254. ha->marker_needed = 0;
  255. }
  256. /* Acquire ring specific lock */
  257. spin_lock_irqsave(&pha->hardware_lock, flags);
  258. /* Check for room in outstanding command list. */
  259. handle = ha->current_outstanding_cmd;
  260. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  261. handle++;
  262. if (handle == MAX_OUTSTANDING_COMMANDS)
  263. handle = 1;
  264. if (!ha->outstanding_cmds[handle])
  265. break;
  266. }
  267. if (index == MAX_OUTSTANDING_COMMANDS)
  268. goto queuing_error;
  269. /* Map the sg table so we have an accurate count of sg entries needed */
  270. if (scsi_sg_count(cmd)) {
  271. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  272. scsi_sg_count(cmd), cmd->sc_data_direction);
  273. if (unlikely(!nseg))
  274. goto queuing_error;
  275. } else
  276. nseg = 0;
  277. tot_dsds = nseg;
  278. /* Calculate the number of request entries needed. */
  279. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  280. if (ha->req_q_cnt < (req_cnt + 2)) {
  281. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  282. if (ha->req_ring_index < cnt)
  283. ha->req_q_cnt = cnt - ha->req_ring_index;
  284. else
  285. ha->req_q_cnt = ha->request_q_length -
  286. (ha->req_ring_index - cnt);
  287. }
  288. if (ha->req_q_cnt < (req_cnt + 2))
  289. goto queuing_error;
  290. /* Build command packet */
  291. ha->current_outstanding_cmd = handle;
  292. ha->outstanding_cmds[handle] = sp;
  293. sp->ha = ha;
  294. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  295. ha->req_q_cnt -= req_cnt;
  296. cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
  297. cmd_pkt->handle = handle;
  298. /* Zero out remaining portion of packet. */
  299. clr_ptr = (uint32_t *)cmd_pkt + 2;
  300. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  301. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  302. /* Set target ID and LUN number*/
  303. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  304. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  305. /* Update tagged queuing modifier */
  306. cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
  307. /* Load SCSI command packet. */
  308. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  309. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  310. /* Build IOCB segments */
  311. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  312. /* Set total data segment count. */
  313. cmd_pkt->entry_count = (uint8_t)req_cnt;
  314. wmb();
  315. /* Adjust ring index. */
  316. ha->req_ring_index++;
  317. if (ha->req_ring_index == ha->request_q_length) {
  318. ha->req_ring_index = 0;
  319. ha->request_ring_ptr = ha->request_ring;
  320. } else
  321. ha->request_ring_ptr++;
  322. sp->flags |= SRB_DMA_VALID;
  323. /* Set chip new ring index. */
  324. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
  325. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  326. /* Manage unprocessed RIO/ZIO commands in response queue. */
  327. if (ha->flags.process_response_queue &&
  328. ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
  329. qla2x00_process_response_queue(ha);
  330. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  331. return (QLA_SUCCESS);
  332. queuing_error:
  333. if (tot_dsds)
  334. scsi_dma_unmap(cmd);
  335. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  336. return (QLA_FUNCTION_FAILED);
  337. }
  338. /**
  339. * qla2x00_marker() - Send a marker IOCB to the firmware.
  340. * @ha: HA context
  341. * @loop_id: loop ID
  342. * @lun: LUN
  343. * @type: marker modifier
  344. *
  345. * Can be called from both normal and interrupt context.
  346. *
  347. * Returns non-zero if a failure occured, else zero.
  348. */
  349. int
  350. __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  351. uint8_t type)
  352. {
  353. mrk_entry_t *mrk;
  354. struct mrk_entry_24xx *mrk24;
  355. scsi_qla_host_t *pha = to_qla_parent(ha);
  356. mrk24 = NULL;
  357. mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
  358. if (mrk == NULL) {
  359. DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
  360. __func__, ha->host_no));
  361. return (QLA_FUNCTION_FAILED);
  362. }
  363. mrk->entry_type = MARKER_TYPE;
  364. mrk->modifier = type;
  365. if (type != MK_SYNC_ALL) {
  366. if (IS_FWI2_CAPABLE(ha)) {
  367. mrk24 = (struct mrk_entry_24xx *) mrk;
  368. mrk24->nport_handle = cpu_to_le16(loop_id);
  369. mrk24->lun[1] = LSB(lun);
  370. mrk24->lun[2] = MSB(lun);
  371. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  372. mrk24->vp_index = ha->vp_idx;
  373. } else {
  374. SET_TARGET_ID(ha, mrk->target, loop_id);
  375. mrk->lun = cpu_to_le16(lun);
  376. }
  377. }
  378. wmb();
  379. qla2x00_isp_cmd(pha);
  380. return (QLA_SUCCESS);
  381. }
  382. int
  383. qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  384. uint8_t type)
  385. {
  386. int ret;
  387. unsigned long flags = 0;
  388. scsi_qla_host_t *pha = to_qla_parent(ha);
  389. spin_lock_irqsave(&pha->hardware_lock, flags);
  390. ret = __qla2x00_marker(ha, loop_id, lun, type);
  391. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  392. return (ret);
  393. }
  394. /**
  395. * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
  396. * @ha: HA context
  397. *
  398. * Note: The caller must hold the hardware lock before calling this routine.
  399. *
  400. * Returns NULL if function failed, else, a pointer to the request packet.
  401. */
  402. static request_t *
  403. qla2x00_req_pkt(scsi_qla_host_t *ha)
  404. {
  405. device_reg_t __iomem *reg = ha->iobase;
  406. request_t *pkt = NULL;
  407. uint16_t cnt;
  408. uint32_t *dword_ptr;
  409. uint32_t timer;
  410. uint16_t req_cnt = 1;
  411. /* Wait 1 second for slot. */
  412. for (timer = HZ; timer; timer--) {
  413. if ((req_cnt + 2) >= ha->req_q_cnt) {
  414. /* Calculate number of free request entries. */
  415. if (IS_FWI2_CAPABLE(ha))
  416. cnt = (uint16_t)RD_REG_DWORD(
  417. &reg->isp24.req_q_out);
  418. else
  419. cnt = qla2x00_debounce_register(
  420. ISP_REQ_Q_OUT(ha, &reg->isp));
  421. if (ha->req_ring_index < cnt)
  422. ha->req_q_cnt = cnt - ha->req_ring_index;
  423. else
  424. ha->req_q_cnt = ha->request_q_length -
  425. (ha->req_ring_index - cnt);
  426. }
  427. /* If room for request in request ring. */
  428. if ((req_cnt + 2) < ha->req_q_cnt) {
  429. ha->req_q_cnt--;
  430. pkt = ha->request_ring_ptr;
  431. /* Zero out packet. */
  432. dword_ptr = (uint32_t *)pkt;
  433. for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
  434. *dword_ptr++ = 0;
  435. /* Set system defined field. */
  436. pkt->sys_define = (uint8_t)ha->req_ring_index;
  437. /* Set entry count. */
  438. pkt->entry_count = 1;
  439. break;
  440. }
  441. /* Release ring specific lock */
  442. spin_unlock(&ha->hardware_lock);
  443. udelay(2); /* 2 us */
  444. /* Check for pending interrupts. */
  445. /* During init we issue marker directly */
  446. if (!ha->marker_needed && !ha->flags.init_done)
  447. qla2x00_poll(ha);
  448. spin_lock_irq(&ha->hardware_lock);
  449. }
  450. if (!pkt) {
  451. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  452. }
  453. return (pkt);
  454. }
  455. /**
  456. * qla2x00_isp_cmd() - Modify the request ring pointer.
  457. * @ha: HA context
  458. *
  459. * Note: The caller must hold the hardware lock before calling this routine.
  460. */
  461. static void
  462. qla2x00_isp_cmd(scsi_qla_host_t *ha)
  463. {
  464. device_reg_t __iomem *reg = ha->iobase;
  465. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  466. DEBUG5(qla2x00_dump_buffer(
  467. (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
  468. /* Adjust ring index. */
  469. ha->req_ring_index++;
  470. if (ha->req_ring_index == ha->request_q_length) {
  471. ha->req_ring_index = 0;
  472. ha->request_ring_ptr = ha->request_ring;
  473. } else
  474. ha->request_ring_ptr++;
  475. /* Set chip new ring index. */
  476. if (IS_FWI2_CAPABLE(ha)) {
  477. WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
  478. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  479. } else {
  480. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
  481. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  482. }
  483. }
  484. /**
  485. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  486. * Continuation Type 1 IOCBs to allocate.
  487. *
  488. * @dsds: number of data segment decriptors needed
  489. *
  490. * Returns the number of IOCB entries needed to store @dsds.
  491. */
  492. static inline uint16_t
  493. qla24xx_calc_iocbs(uint16_t dsds)
  494. {
  495. uint16_t iocbs;
  496. iocbs = 1;
  497. if (dsds > 1) {
  498. iocbs += (dsds - 1) / 5;
  499. if ((dsds - 1) % 5)
  500. iocbs++;
  501. }
  502. return iocbs;
  503. }
  504. /**
  505. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  506. * IOCB types.
  507. *
  508. * @sp: SRB command to process
  509. * @cmd_pkt: Command type 3 IOCB
  510. * @tot_dsds: Total number of segments to transfer
  511. */
  512. static inline void
  513. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  514. uint16_t tot_dsds)
  515. {
  516. uint16_t avail_dsds;
  517. uint32_t *cur_dsd;
  518. scsi_qla_host_t *ha;
  519. struct scsi_cmnd *cmd;
  520. struct scatterlist *sg;
  521. int i;
  522. cmd = sp->cmd;
  523. /* Update entry type to indicate Command Type 3 IOCB */
  524. *((uint32_t *)(&cmd_pkt->entry_type)) =
  525. __constant_cpu_to_le32(COMMAND_TYPE_7);
  526. /* No data transfer */
  527. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  528. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  529. return;
  530. }
  531. ha = sp->ha;
  532. /* Set transfer direction */
  533. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  534. cmd_pkt->task_mgmt_flags =
  535. __constant_cpu_to_le16(TMF_WRITE_DATA);
  536. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  537. cmd_pkt->task_mgmt_flags =
  538. __constant_cpu_to_le16(TMF_READ_DATA);
  539. /* One DSD is available in the Command Type 3 IOCB */
  540. avail_dsds = 1;
  541. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  542. /* Load data segments */
  543. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  544. dma_addr_t sle_dma;
  545. cont_a64_entry_t *cont_pkt;
  546. /* Allocate additional continuation packets? */
  547. if (avail_dsds == 0) {
  548. /*
  549. * Five DSDs are available in the Continuation
  550. * Type 1 IOCB.
  551. */
  552. cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
  553. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  554. avail_dsds = 5;
  555. }
  556. sle_dma = sg_dma_address(sg);
  557. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  558. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  559. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  560. avail_dsds--;
  561. }
  562. }
  563. /**
  564. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  565. * @sp: command to send to the ISP
  566. *
  567. * Returns non-zero if a failure occured, else zero.
  568. */
  569. int
  570. qla24xx_start_scsi(srb_t *sp)
  571. {
  572. int ret, nseg;
  573. unsigned long flags;
  574. scsi_qla_host_t *ha, *pha;
  575. struct scsi_cmnd *cmd;
  576. uint32_t *clr_ptr;
  577. uint32_t index;
  578. uint32_t handle;
  579. struct cmd_type_7 *cmd_pkt;
  580. uint16_t cnt;
  581. uint16_t req_cnt;
  582. uint16_t tot_dsds;
  583. struct device_reg_24xx __iomem *reg;
  584. /* Setup device pointers. */
  585. ret = 0;
  586. ha = sp->ha;
  587. pha = to_qla_parent(ha);
  588. reg = &ha->iobase->isp24;
  589. cmd = sp->cmd;
  590. /* So we know we haven't pci_map'ed anything yet */
  591. tot_dsds = 0;
  592. /* Send marker if required */
  593. if (ha->marker_needed != 0) {
  594. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  595. return QLA_FUNCTION_FAILED;
  596. }
  597. ha->marker_needed = 0;
  598. }
  599. /* Acquire ring specific lock */
  600. spin_lock_irqsave(&pha->hardware_lock, flags);
  601. /* Check for room in outstanding command list. */
  602. handle = ha->current_outstanding_cmd;
  603. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  604. handle++;
  605. if (handle == MAX_OUTSTANDING_COMMANDS)
  606. handle = 1;
  607. if (!ha->outstanding_cmds[handle])
  608. break;
  609. }
  610. if (index == MAX_OUTSTANDING_COMMANDS)
  611. goto queuing_error;
  612. /* Map the sg table so we have an accurate count of sg entries needed */
  613. if (scsi_sg_count(cmd)) {
  614. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  615. scsi_sg_count(cmd), cmd->sc_data_direction);
  616. if (unlikely(!nseg))
  617. goto queuing_error;
  618. } else
  619. nseg = 0;
  620. tot_dsds = nseg;
  621. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  622. if (ha->req_q_cnt < (req_cnt + 2)) {
  623. cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
  624. if (ha->req_ring_index < cnt)
  625. ha->req_q_cnt = cnt - ha->req_ring_index;
  626. else
  627. ha->req_q_cnt = ha->request_q_length -
  628. (ha->req_ring_index - cnt);
  629. }
  630. if (ha->req_q_cnt < (req_cnt + 2))
  631. goto queuing_error;
  632. /* Build command packet. */
  633. ha->current_outstanding_cmd = handle;
  634. ha->outstanding_cmds[handle] = sp;
  635. sp->ha = ha;
  636. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  637. ha->req_q_cnt -= req_cnt;
  638. cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
  639. cmd_pkt->handle = handle;
  640. /* Zero out remaining portion of packet. */
  641. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  642. clr_ptr = (uint32_t *)cmd_pkt + 2;
  643. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  644. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  645. /* Set NPORT-ID and LUN number*/
  646. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  647. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  648. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  649. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  650. cmd_pkt->vp_index = sp->fcport->vp_idx;
  651. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  652. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  653. /* Load SCSI command packet. */
  654. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  655. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  656. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  657. /* Build IOCB segments */
  658. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  659. /* Set total data segment count. */
  660. cmd_pkt->entry_count = (uint8_t)req_cnt;
  661. wmb();
  662. /* Adjust ring index. */
  663. ha->req_ring_index++;
  664. if (ha->req_ring_index == ha->request_q_length) {
  665. ha->req_ring_index = 0;
  666. ha->request_ring_ptr = ha->request_ring;
  667. } else
  668. ha->request_ring_ptr++;
  669. sp->flags |= SRB_DMA_VALID;
  670. /* Set chip new ring index. */
  671. WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
  672. RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
  673. /* Manage unprocessed RIO/ZIO commands in response queue. */
  674. if (ha->flags.process_response_queue &&
  675. ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
  676. qla24xx_process_response_queue(ha);
  677. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  678. return QLA_SUCCESS;
  679. queuing_error:
  680. if (tot_dsds)
  681. scsi_dma_unmap(cmd);
  682. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  683. return QLA_FUNCTION_FAILED;
  684. }