qla_iocb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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;
  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. reg = &ha->iobase->isp;
  245. cmd = sp->cmd;
  246. /* So we know we haven't pci_map'ed anything yet */
  247. tot_dsds = 0;
  248. /* Send marker if required */
  249. if (ha->marker_needed != 0) {
  250. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  251. return (QLA_FUNCTION_FAILED);
  252. }
  253. ha->marker_needed = 0;
  254. }
  255. /* Acquire ring specific lock */
  256. spin_lock_irqsave(&ha->hardware_lock, flags);
  257. /* Check for room in outstanding command list. */
  258. handle = ha->current_outstanding_cmd;
  259. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  260. handle++;
  261. if (handle == MAX_OUTSTANDING_COMMANDS)
  262. handle = 1;
  263. if (!ha->outstanding_cmds[handle])
  264. break;
  265. }
  266. if (index == MAX_OUTSTANDING_COMMANDS)
  267. goto queuing_error;
  268. /* Map the sg table so we have an accurate count of sg entries needed */
  269. if (scsi_sg_count(cmd)) {
  270. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  271. scsi_sg_count(cmd), cmd->sc_data_direction);
  272. if (unlikely(!nseg))
  273. goto queuing_error;
  274. } else
  275. nseg = 0;
  276. tot_dsds = nseg;
  277. /* Calculate the number of request entries needed. */
  278. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  279. if (ha->req_q_cnt < (req_cnt + 2)) {
  280. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  281. if (ha->req_ring_index < cnt)
  282. ha->req_q_cnt = cnt - ha->req_ring_index;
  283. else
  284. ha->req_q_cnt = ha->request_q_length -
  285. (ha->req_ring_index - cnt);
  286. }
  287. if (ha->req_q_cnt < (req_cnt + 2))
  288. goto queuing_error;
  289. /* Build command packet */
  290. ha->current_outstanding_cmd = handle;
  291. ha->outstanding_cmds[handle] = sp;
  292. sp->ha = ha;
  293. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  294. ha->req_q_cnt -= req_cnt;
  295. cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
  296. cmd_pkt->handle = handle;
  297. /* Zero out remaining portion of packet. */
  298. clr_ptr = (uint32_t *)cmd_pkt + 2;
  299. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  300. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  301. /* Set target ID and LUN number*/
  302. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  303. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  304. /* Update tagged queuing modifier */
  305. cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
  306. /* Load SCSI command packet. */
  307. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  308. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  309. /* Build IOCB segments */
  310. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  311. /* Set total data segment count. */
  312. cmd_pkt->entry_count = (uint8_t)req_cnt;
  313. wmb();
  314. /* Adjust ring index. */
  315. ha->req_ring_index++;
  316. if (ha->req_ring_index == ha->request_q_length) {
  317. ha->req_ring_index = 0;
  318. ha->request_ring_ptr = ha->request_ring;
  319. } else
  320. ha->request_ring_ptr++;
  321. sp->flags |= SRB_DMA_VALID;
  322. /* Set chip new ring index. */
  323. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
  324. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  325. /* Manage unprocessed RIO/ZIO commands in response queue. */
  326. if (ha->flags.process_response_queue &&
  327. ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
  328. qla2x00_process_response_queue(ha);
  329. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  330. return (QLA_SUCCESS);
  331. queuing_error:
  332. if (tot_dsds)
  333. scsi_dma_unmap(cmd);
  334. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  335. return (QLA_FUNCTION_FAILED);
  336. }
  337. /**
  338. * qla2x00_marker() - Send a marker IOCB to the firmware.
  339. * @ha: HA context
  340. * @loop_id: loop ID
  341. * @lun: LUN
  342. * @type: marker modifier
  343. *
  344. * Can be called from both normal and interrupt context.
  345. *
  346. * Returns non-zero if a failure occured, else zero.
  347. */
  348. int
  349. __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  350. uint8_t type)
  351. {
  352. mrk_entry_t *mrk;
  353. struct mrk_entry_24xx *mrk24;
  354. scsi_qla_host_t *pha = to_qla_parent(ha);
  355. mrk24 = NULL;
  356. mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
  357. if (mrk == NULL) {
  358. DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
  359. __func__, ha->host_no));
  360. return (QLA_FUNCTION_FAILED);
  361. }
  362. mrk->entry_type = MARKER_TYPE;
  363. mrk->modifier = type;
  364. if (type != MK_SYNC_ALL) {
  365. if (IS_FWI2_CAPABLE(ha)) {
  366. mrk24 = (struct mrk_entry_24xx *) mrk;
  367. mrk24->nport_handle = cpu_to_le16(loop_id);
  368. mrk24->lun[1] = LSB(lun);
  369. mrk24->lun[2] = MSB(lun);
  370. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  371. mrk24->vp_index = ha->vp_idx;
  372. } else {
  373. SET_TARGET_ID(ha, mrk->target, loop_id);
  374. mrk->lun = cpu_to_le16(lun);
  375. }
  376. }
  377. wmb();
  378. qla2x00_isp_cmd(pha);
  379. return (QLA_SUCCESS);
  380. }
  381. int
  382. qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
  383. uint8_t type)
  384. {
  385. int ret;
  386. unsigned long flags = 0;
  387. scsi_qla_host_t *pha = to_qla_parent(ha);
  388. spin_lock_irqsave(&pha->hardware_lock, flags);
  389. ret = __qla2x00_marker(ha, loop_id, lun, type);
  390. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  391. return (ret);
  392. }
  393. /**
  394. * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
  395. * @ha: HA context
  396. *
  397. * Note: The caller must hold the hardware lock before calling this routine.
  398. *
  399. * Returns NULL if function failed, else, a pointer to the request packet.
  400. */
  401. static request_t *
  402. qla2x00_req_pkt(scsi_qla_host_t *ha)
  403. {
  404. device_reg_t __iomem *reg = ha->iobase;
  405. request_t *pkt = NULL;
  406. uint16_t cnt;
  407. uint32_t *dword_ptr;
  408. uint32_t timer;
  409. uint16_t req_cnt = 1;
  410. /* Wait 1 second for slot. */
  411. for (timer = HZ; timer; timer--) {
  412. if ((req_cnt + 2) >= ha->req_q_cnt) {
  413. /* Calculate number of free request entries. */
  414. if (IS_FWI2_CAPABLE(ha))
  415. cnt = (uint16_t)RD_REG_DWORD(
  416. &reg->isp24.req_q_out);
  417. else
  418. cnt = qla2x00_debounce_register(
  419. ISP_REQ_Q_OUT(ha, &reg->isp));
  420. if (ha->req_ring_index < cnt)
  421. ha->req_q_cnt = cnt - ha->req_ring_index;
  422. else
  423. ha->req_q_cnt = ha->request_q_length -
  424. (ha->req_ring_index - cnt);
  425. }
  426. /* If room for request in request ring. */
  427. if ((req_cnt + 2) < ha->req_q_cnt) {
  428. ha->req_q_cnt--;
  429. pkt = ha->request_ring_ptr;
  430. /* Zero out packet. */
  431. dword_ptr = (uint32_t *)pkt;
  432. for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
  433. *dword_ptr++ = 0;
  434. /* Set system defined field. */
  435. pkt->sys_define = (uint8_t)ha->req_ring_index;
  436. /* Set entry count. */
  437. pkt->entry_count = 1;
  438. break;
  439. }
  440. /* Release ring specific lock */
  441. spin_unlock(&ha->hardware_lock);
  442. udelay(2); /* 2 us */
  443. /* Check for pending interrupts. */
  444. /* During init we issue marker directly */
  445. if (!ha->marker_needed && !ha->flags.init_done)
  446. qla2x00_poll(ha);
  447. spin_lock_irq(&ha->hardware_lock);
  448. }
  449. if (!pkt) {
  450. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  451. }
  452. return (pkt);
  453. }
  454. /**
  455. * qla2x00_isp_cmd() - Modify the request ring pointer.
  456. * @ha: HA context
  457. *
  458. * Note: The caller must hold the hardware lock before calling this routine.
  459. */
  460. static void
  461. qla2x00_isp_cmd(scsi_qla_host_t *ha)
  462. {
  463. device_reg_t __iomem *reg = ha->iobase;
  464. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  465. DEBUG5(qla2x00_dump_buffer(
  466. (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
  467. /* Adjust ring index. */
  468. ha->req_ring_index++;
  469. if (ha->req_ring_index == ha->request_q_length) {
  470. ha->req_ring_index = 0;
  471. ha->request_ring_ptr = ha->request_ring;
  472. } else
  473. ha->request_ring_ptr++;
  474. /* Set chip new ring index. */
  475. if (IS_FWI2_CAPABLE(ha)) {
  476. WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
  477. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  478. } else {
  479. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
  480. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  481. }
  482. }
  483. /**
  484. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  485. * Continuation Type 1 IOCBs to allocate.
  486. *
  487. * @dsds: number of data segment decriptors needed
  488. *
  489. * Returns the number of IOCB entries needed to store @dsds.
  490. */
  491. static inline uint16_t
  492. qla24xx_calc_iocbs(uint16_t dsds)
  493. {
  494. uint16_t iocbs;
  495. iocbs = 1;
  496. if (dsds > 1) {
  497. iocbs += (dsds - 1) / 5;
  498. if ((dsds - 1) % 5)
  499. iocbs++;
  500. }
  501. return iocbs;
  502. }
  503. /**
  504. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  505. * IOCB types.
  506. *
  507. * @sp: SRB command to process
  508. * @cmd_pkt: Command type 3 IOCB
  509. * @tot_dsds: Total number of segments to transfer
  510. */
  511. static inline void
  512. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  513. uint16_t tot_dsds)
  514. {
  515. uint16_t avail_dsds;
  516. uint32_t *cur_dsd;
  517. scsi_qla_host_t *ha;
  518. struct scsi_cmnd *cmd;
  519. struct scatterlist *sg;
  520. int i;
  521. cmd = sp->cmd;
  522. /* Update entry type to indicate Command Type 3 IOCB */
  523. *((uint32_t *)(&cmd_pkt->entry_type)) =
  524. __constant_cpu_to_le32(COMMAND_TYPE_7);
  525. /* No data transfer */
  526. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  527. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  528. return;
  529. }
  530. ha = sp->ha;
  531. /* Set transfer direction */
  532. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  533. cmd_pkt->task_mgmt_flags =
  534. __constant_cpu_to_le16(TMF_WRITE_DATA);
  535. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  536. cmd_pkt->task_mgmt_flags =
  537. __constant_cpu_to_le16(TMF_READ_DATA);
  538. /* One DSD is available in the Command Type 3 IOCB */
  539. avail_dsds = 1;
  540. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  541. /* Load data segments */
  542. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  543. dma_addr_t sle_dma;
  544. cont_a64_entry_t *cont_pkt;
  545. /* Allocate additional continuation packets? */
  546. if (avail_dsds == 0) {
  547. /*
  548. * Five DSDs are available in the Continuation
  549. * Type 1 IOCB.
  550. */
  551. cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
  552. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  553. avail_dsds = 5;
  554. }
  555. sle_dma = sg_dma_address(sg);
  556. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  557. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  558. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  559. avail_dsds--;
  560. }
  561. }
  562. /**
  563. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  564. * @sp: command to send to the ISP
  565. *
  566. * Returns non-zero if a failure occured, else zero.
  567. */
  568. int
  569. qla24xx_start_scsi(srb_t *sp)
  570. {
  571. int ret, nseg;
  572. unsigned long flags;
  573. scsi_qla_host_t *ha, *pha;
  574. struct scsi_cmnd *cmd;
  575. uint32_t *clr_ptr;
  576. uint32_t index;
  577. uint32_t handle;
  578. struct cmd_type_7 *cmd_pkt;
  579. uint16_t cnt;
  580. uint16_t req_cnt;
  581. uint16_t tot_dsds;
  582. struct device_reg_24xx __iomem *reg;
  583. /* Setup device pointers. */
  584. ret = 0;
  585. ha = sp->ha;
  586. pha = to_qla_parent(ha);
  587. reg = &ha->iobase->isp24;
  588. cmd = sp->cmd;
  589. /* So we know we haven't pci_map'ed anything yet */
  590. tot_dsds = 0;
  591. /* Send marker if required */
  592. if (ha->marker_needed != 0) {
  593. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  594. return QLA_FUNCTION_FAILED;
  595. }
  596. ha->marker_needed = 0;
  597. }
  598. /* Acquire ring specific lock */
  599. spin_lock_irqsave(&pha->hardware_lock, flags);
  600. /* Check for room in outstanding command list. */
  601. handle = ha->current_outstanding_cmd;
  602. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  603. handle++;
  604. if (handle == MAX_OUTSTANDING_COMMANDS)
  605. handle = 1;
  606. if (!ha->outstanding_cmds[handle])
  607. break;
  608. }
  609. if (index == MAX_OUTSTANDING_COMMANDS)
  610. goto queuing_error;
  611. /* Map the sg table so we have an accurate count of sg entries needed */
  612. if (scsi_sg_count(cmd)) {
  613. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  614. scsi_sg_count(cmd), cmd->sc_data_direction);
  615. if (unlikely(!nseg))
  616. goto queuing_error;
  617. } else
  618. nseg = 0;
  619. tot_dsds = nseg;
  620. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  621. if (ha->req_q_cnt < (req_cnt + 2)) {
  622. cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
  623. if (ha->req_ring_index < cnt)
  624. ha->req_q_cnt = cnt - ha->req_ring_index;
  625. else
  626. ha->req_q_cnt = ha->request_q_length -
  627. (ha->req_ring_index - cnt);
  628. }
  629. if (ha->req_q_cnt < (req_cnt + 2))
  630. goto queuing_error;
  631. /* Build command packet. */
  632. ha->current_outstanding_cmd = handle;
  633. ha->outstanding_cmds[handle] = sp;
  634. sp->ha = ha;
  635. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  636. ha->req_q_cnt -= req_cnt;
  637. cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
  638. cmd_pkt->handle = handle;
  639. /* Zero out remaining portion of packet. */
  640. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  641. clr_ptr = (uint32_t *)cmd_pkt + 2;
  642. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  643. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  644. /* Set NPORT-ID and LUN number*/
  645. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  646. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  647. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  648. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  649. cmd_pkt->vp_index = sp->fcport->vp_idx;
  650. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  651. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  652. /* Load SCSI command packet. */
  653. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  654. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  655. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  656. /* Build IOCB segments */
  657. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  658. /* Set total data segment count. */
  659. cmd_pkt->entry_count = (uint8_t)req_cnt;
  660. wmb();
  661. /* Adjust ring index. */
  662. ha->req_ring_index++;
  663. if (ha->req_ring_index == ha->request_q_length) {
  664. ha->req_ring_index = 0;
  665. ha->request_ring_ptr = ha->request_ring;
  666. } else
  667. ha->request_ring_ptr++;
  668. sp->flags |= SRB_DMA_VALID;
  669. /* Set chip new ring index. */
  670. WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
  671. RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
  672. /* Manage unprocessed RIO/ZIO commands in response queue. */
  673. if (ha->flags.process_response_queue &&
  674. ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
  675. qla24xx_process_response_queue(ha);
  676. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  677. return QLA_SUCCESS;
  678. queuing_error:
  679. if (tot_dsds)
  680. scsi_dma_unmap(cmd);
  681. spin_unlock_irqrestore(&pha->hardware_lock, flags);
  682. return QLA_FUNCTION_FAILED;
  683. }