qla_iocb.c 21 KB

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