qla_iocb.c 21 KB

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