qla_iocb.c 22 KB

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