qla_iocb.c 22 KB

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