qla_iocb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. spin_lock_irqsave(&ha->hardware_lock, flags);
  388. ret = __qla2x00_marker(ha, loop_id, lun, type);
  389. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  390. return (ret);
  391. }
  392. /**
  393. * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
  394. * @ha: HA context
  395. *
  396. * Note: The caller must hold the hardware lock before calling this routine.
  397. *
  398. * Returns NULL if function failed, else, a pointer to the request packet.
  399. */
  400. static request_t *
  401. qla2x00_req_pkt(scsi_qla_host_t *ha)
  402. {
  403. device_reg_t __iomem *reg = ha->iobase;
  404. request_t *pkt = NULL;
  405. uint16_t cnt;
  406. uint32_t *dword_ptr;
  407. uint32_t timer;
  408. uint16_t req_cnt = 1;
  409. /* Wait 1 second for slot. */
  410. for (timer = HZ; timer; timer--) {
  411. if ((req_cnt + 2) >= ha->req_q_cnt) {
  412. /* Calculate number of free request entries. */
  413. if (IS_FWI2_CAPABLE(ha))
  414. cnt = (uint16_t)RD_REG_DWORD(
  415. &reg->isp24.req_q_out);
  416. else
  417. cnt = qla2x00_debounce_register(
  418. ISP_REQ_Q_OUT(ha, &reg->isp));
  419. if (ha->req_ring_index < cnt)
  420. ha->req_q_cnt = cnt - ha->req_ring_index;
  421. else
  422. ha->req_q_cnt = ha->request_q_length -
  423. (ha->req_ring_index - cnt);
  424. }
  425. /* If room for request in request ring. */
  426. if ((req_cnt + 2) < ha->req_q_cnt) {
  427. ha->req_q_cnt--;
  428. pkt = ha->request_ring_ptr;
  429. /* Zero out packet. */
  430. dword_ptr = (uint32_t *)pkt;
  431. for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
  432. *dword_ptr++ = 0;
  433. /* Set system defined field. */
  434. pkt->sys_define = (uint8_t)ha->req_ring_index;
  435. /* Set entry count. */
  436. pkt->entry_count = 1;
  437. break;
  438. }
  439. /* Release ring specific lock */
  440. spin_unlock(&ha->hardware_lock);
  441. udelay(2); /* 2 us */
  442. /* Check for pending interrupts. */
  443. /* During init we issue marker directly */
  444. if (!ha->marker_needed && !ha->flags.init_done)
  445. qla2x00_poll(ha);
  446. spin_lock_irq(&ha->hardware_lock);
  447. }
  448. if (!pkt) {
  449. DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
  450. }
  451. return (pkt);
  452. }
  453. /**
  454. * qla2x00_isp_cmd() - Modify the request ring pointer.
  455. * @ha: HA context
  456. *
  457. * Note: The caller must hold the hardware lock before calling this routine.
  458. */
  459. static void
  460. qla2x00_isp_cmd(scsi_qla_host_t *ha)
  461. {
  462. device_reg_t __iomem *reg = ha->iobase;
  463. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  464. DEBUG5(qla2x00_dump_buffer(
  465. (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
  466. /* Adjust ring index. */
  467. ha->req_ring_index++;
  468. if (ha->req_ring_index == ha->request_q_length) {
  469. ha->req_ring_index = 0;
  470. ha->request_ring_ptr = ha->request_ring;
  471. } else
  472. ha->request_ring_ptr++;
  473. /* Set chip new ring index. */
  474. if (IS_FWI2_CAPABLE(ha)) {
  475. WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
  476. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  477. } else {
  478. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
  479. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  480. }
  481. }
  482. /**
  483. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  484. * Continuation Type 1 IOCBs to allocate.
  485. *
  486. * @dsds: number of data segment decriptors needed
  487. *
  488. * Returns the number of IOCB entries needed to store @dsds.
  489. */
  490. static inline uint16_t
  491. qla24xx_calc_iocbs(uint16_t dsds)
  492. {
  493. uint16_t iocbs;
  494. iocbs = 1;
  495. if (dsds > 1) {
  496. iocbs += (dsds - 1) / 5;
  497. if ((dsds - 1) % 5)
  498. iocbs++;
  499. }
  500. return iocbs;
  501. }
  502. /**
  503. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  504. * IOCB types.
  505. *
  506. * @sp: SRB command to process
  507. * @cmd_pkt: Command type 3 IOCB
  508. * @tot_dsds: Total number of segments to transfer
  509. */
  510. static inline void
  511. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  512. uint16_t tot_dsds)
  513. {
  514. uint16_t avail_dsds;
  515. uint32_t *cur_dsd;
  516. scsi_qla_host_t *ha;
  517. struct scsi_cmnd *cmd;
  518. struct scatterlist *sg;
  519. int i;
  520. cmd = sp->cmd;
  521. /* Update entry type to indicate Command Type 3 IOCB */
  522. *((uint32_t *)(&cmd_pkt->entry_type)) =
  523. __constant_cpu_to_le32(COMMAND_TYPE_7);
  524. /* No data transfer */
  525. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  526. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  527. return;
  528. }
  529. ha = sp->ha;
  530. /* Set transfer direction */
  531. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  532. cmd_pkt->task_mgmt_flags =
  533. __constant_cpu_to_le16(TMF_WRITE_DATA);
  534. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  535. cmd_pkt->task_mgmt_flags =
  536. __constant_cpu_to_le16(TMF_READ_DATA);
  537. /* One DSD is available in the Command Type 3 IOCB */
  538. avail_dsds = 1;
  539. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  540. /* Load data segments */
  541. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  542. dma_addr_t sle_dma;
  543. cont_a64_entry_t *cont_pkt;
  544. /* Allocate additional continuation packets? */
  545. if (avail_dsds == 0) {
  546. /*
  547. * Five DSDs are available in the Continuation
  548. * Type 1 IOCB.
  549. */
  550. cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
  551. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  552. avail_dsds = 5;
  553. }
  554. sle_dma = sg_dma_address(sg);
  555. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  556. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  557. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  558. avail_dsds--;
  559. }
  560. }
  561. /**
  562. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  563. * @sp: command to send to the ISP
  564. *
  565. * Returns non-zero if a failure occured, else zero.
  566. */
  567. int
  568. qla24xx_start_scsi(srb_t *sp)
  569. {
  570. int ret, nseg;
  571. unsigned long flags;
  572. scsi_qla_host_t *ha;
  573. struct scsi_cmnd *cmd;
  574. uint32_t *clr_ptr;
  575. uint32_t index;
  576. uint32_t handle;
  577. struct cmd_type_7 *cmd_pkt;
  578. uint16_t cnt;
  579. uint16_t req_cnt;
  580. uint16_t tot_dsds;
  581. struct device_reg_24xx __iomem *reg;
  582. /* Setup device pointers. */
  583. ret = 0;
  584. ha = sp->ha;
  585. reg = &ha->iobase->isp24;
  586. cmd = sp->cmd;
  587. /* So we know we haven't pci_map'ed anything yet */
  588. tot_dsds = 0;
  589. /* Send marker if required */
  590. if (ha->marker_needed != 0) {
  591. if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  592. return QLA_FUNCTION_FAILED;
  593. }
  594. ha->marker_needed = 0;
  595. }
  596. /* Acquire ring specific lock */
  597. spin_lock_irqsave(&ha->hardware_lock, flags);
  598. /* Check for room in outstanding command list. */
  599. handle = ha->current_outstanding_cmd;
  600. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  601. handle++;
  602. if (handle == MAX_OUTSTANDING_COMMANDS)
  603. handle = 1;
  604. if (!ha->outstanding_cmds[handle])
  605. break;
  606. }
  607. if (index == MAX_OUTSTANDING_COMMANDS)
  608. goto queuing_error;
  609. /* Map the sg table so we have an accurate count of sg entries needed */
  610. if (scsi_sg_count(cmd)) {
  611. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  612. scsi_sg_count(cmd), cmd->sc_data_direction);
  613. if (unlikely(!nseg))
  614. goto queuing_error;
  615. } else
  616. nseg = 0;
  617. tot_dsds = nseg;
  618. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  619. if (ha->req_q_cnt < (req_cnt + 2)) {
  620. cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
  621. if (ha->req_ring_index < cnt)
  622. ha->req_q_cnt = cnt - ha->req_ring_index;
  623. else
  624. ha->req_q_cnt = ha->request_q_length -
  625. (ha->req_ring_index - cnt);
  626. }
  627. if (ha->req_q_cnt < (req_cnt + 2))
  628. goto queuing_error;
  629. /* Build command packet. */
  630. ha->current_outstanding_cmd = handle;
  631. ha->outstanding_cmds[handle] = sp;
  632. sp->ha = ha;
  633. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  634. ha->req_q_cnt -= req_cnt;
  635. cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
  636. cmd_pkt->handle = handle;
  637. /* Zero out remaining portion of packet. */
  638. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  639. clr_ptr = (uint32_t *)cmd_pkt + 2;
  640. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  641. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  642. /* Set NPORT-ID and LUN number*/
  643. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  644. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  645. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  646. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  647. cmd_pkt->vp_index = sp->fcport->vp_idx;
  648. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  649. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  650. /* Load SCSI command packet. */
  651. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  652. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  653. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  654. /* Build IOCB segments */
  655. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  656. /* Set total data segment count. */
  657. cmd_pkt->entry_count = (uint8_t)req_cnt;
  658. wmb();
  659. /* Adjust ring index. */
  660. ha->req_ring_index++;
  661. if (ha->req_ring_index == ha->request_q_length) {
  662. ha->req_ring_index = 0;
  663. ha->request_ring_ptr = ha->request_ring;
  664. } else
  665. ha->request_ring_ptr++;
  666. sp->flags |= SRB_DMA_VALID;
  667. /* Set chip new ring index. */
  668. WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
  669. RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
  670. /* Manage unprocessed RIO/ZIO commands in response queue. */
  671. if (ha->flags.process_response_queue &&
  672. ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
  673. qla24xx_process_response_queue(ha);
  674. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  675. return QLA_SUCCESS;
  676. queuing_error:
  677. if (tot_dsds)
  678. scsi_dma_unmap(cmd);
  679. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  680. return QLA_FUNCTION_FAILED;
  681. }