qla_iocb.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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 void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
  12. static void qla25xx_set_que(srb_t *, struct rsp_que **);
  13. /**
  14. * qla2x00_get_cmd_direction() - Determine control_flag data direction.
  15. * @cmd: SCSI command
  16. *
  17. * Returns the proper CF_* direction based on CDB.
  18. */
  19. static inline uint16_t
  20. qla2x00_get_cmd_direction(srb_t *sp)
  21. {
  22. uint16_t cflags;
  23. cflags = 0;
  24. /* Set transfer direction */
  25. if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
  26. cflags = CF_WRITE;
  27. sp->fcport->vha->hw->qla_stats.output_bytes +=
  28. scsi_bufflen(sp->cmd);
  29. } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
  30. cflags = CF_READ;
  31. sp->fcport->vha->hw->qla_stats.input_bytes +=
  32. scsi_bufflen(sp->cmd);
  33. }
  34. return (cflags);
  35. }
  36. /**
  37. * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
  38. * Continuation Type 0 IOCBs to allocate.
  39. *
  40. * @dsds: number of data segment decriptors needed
  41. *
  42. * Returns the number of IOCB entries needed to store @dsds.
  43. */
  44. uint16_t
  45. qla2x00_calc_iocbs_32(uint16_t dsds)
  46. {
  47. uint16_t iocbs;
  48. iocbs = 1;
  49. if (dsds > 3) {
  50. iocbs += (dsds - 3) / 7;
  51. if ((dsds - 3) % 7)
  52. iocbs++;
  53. }
  54. return (iocbs);
  55. }
  56. /**
  57. * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
  58. * Continuation Type 1 IOCBs to allocate.
  59. *
  60. * @dsds: number of data segment decriptors needed
  61. *
  62. * Returns the number of IOCB entries needed to store @dsds.
  63. */
  64. uint16_t
  65. qla2x00_calc_iocbs_64(uint16_t dsds)
  66. {
  67. uint16_t iocbs;
  68. iocbs = 1;
  69. if (dsds > 2) {
  70. iocbs += (dsds - 2) / 5;
  71. if ((dsds - 2) % 5)
  72. iocbs++;
  73. }
  74. return (iocbs);
  75. }
  76. /**
  77. * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
  78. * @ha: HA context
  79. *
  80. * Returns a pointer to the Continuation Type 0 IOCB packet.
  81. */
  82. static inline cont_entry_t *
  83. qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
  84. {
  85. cont_entry_t *cont_pkt;
  86. struct req_que *req = vha->req;
  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(scsi_qla_host_t *vha)
  109. {
  110. cont_a64_entry_t *cont_pkt;
  111. struct req_que *req = vha->req;
  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. static inline int
  127. qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
  128. {
  129. uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
  130. /* We only support T10 DIF right now */
  131. if (guard != SHOST_DIX_GUARD_CRC) {
  132. DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
  133. return 0;
  134. }
  135. /* We always use DIFF Bundling for best performance */
  136. *fw_prot_opts = 0;
  137. /* Translate SCSI opcode to a protection opcode */
  138. switch (scsi_get_prot_op(sp->cmd)) {
  139. case SCSI_PROT_READ_STRIP:
  140. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  141. break;
  142. case SCSI_PROT_WRITE_INSERT:
  143. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  144. break;
  145. case SCSI_PROT_READ_INSERT:
  146. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  147. break;
  148. case SCSI_PROT_WRITE_STRIP:
  149. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  150. break;
  151. case SCSI_PROT_READ_PASS:
  152. *fw_prot_opts |= PO_MODE_DIF_PASS;
  153. break;
  154. case SCSI_PROT_WRITE_PASS:
  155. *fw_prot_opts |= PO_MODE_DIF_PASS;
  156. break;
  157. default: /* Normal Request */
  158. *fw_prot_opts |= PO_MODE_DIF_PASS;
  159. break;
  160. }
  161. return scsi_prot_sg_count(sp->cmd);
  162. }
  163. /*
  164. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  165. * capable IOCB types.
  166. *
  167. * @sp: SRB command to process
  168. * @cmd_pkt: Command type 2 IOCB
  169. * @tot_dsds: Total number of segments to transfer
  170. */
  171. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  172. uint16_t tot_dsds)
  173. {
  174. uint16_t avail_dsds;
  175. uint32_t *cur_dsd;
  176. scsi_qla_host_t *vha;
  177. struct scsi_cmnd *cmd;
  178. struct scatterlist *sg;
  179. int i;
  180. cmd = sp->cmd;
  181. /* Update entry type to indicate Command Type 2 IOCB */
  182. *((uint32_t *)(&cmd_pkt->entry_type)) =
  183. __constant_cpu_to_le32(COMMAND_TYPE);
  184. /* No data transfer */
  185. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  186. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  187. return;
  188. }
  189. vha = sp->fcport->vha;
  190. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  191. /* Three DSDs are available in the Command Type 2 IOCB */
  192. avail_dsds = 3;
  193. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  194. /* Load data segments */
  195. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  196. cont_entry_t *cont_pkt;
  197. /* Allocate additional continuation packets? */
  198. if (avail_dsds == 0) {
  199. /*
  200. * Seven DSDs are available in the Continuation
  201. * Type 0 IOCB.
  202. */
  203. cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
  204. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  205. avail_dsds = 7;
  206. }
  207. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  208. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  209. avail_dsds--;
  210. }
  211. }
  212. /**
  213. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  214. * capable IOCB types.
  215. *
  216. * @sp: SRB command to process
  217. * @cmd_pkt: Command type 3 IOCB
  218. * @tot_dsds: Total number of segments to transfer
  219. */
  220. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  221. uint16_t tot_dsds)
  222. {
  223. uint16_t avail_dsds;
  224. uint32_t *cur_dsd;
  225. scsi_qla_host_t *vha;
  226. struct scsi_cmnd *cmd;
  227. struct scatterlist *sg;
  228. int i;
  229. cmd = sp->cmd;
  230. /* Update entry type to indicate Command Type 3 IOCB */
  231. *((uint32_t *)(&cmd_pkt->entry_type)) =
  232. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  233. /* No data transfer */
  234. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  235. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  236. return;
  237. }
  238. vha = sp->fcport->vha;
  239. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  240. /* Two DSDs are available in the Command Type 3 IOCB */
  241. avail_dsds = 2;
  242. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  243. /* Load data segments */
  244. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  245. dma_addr_t sle_dma;
  246. cont_a64_entry_t *cont_pkt;
  247. /* Allocate additional continuation packets? */
  248. if (avail_dsds == 0) {
  249. /*
  250. * Five DSDs are available in the Continuation
  251. * Type 1 IOCB.
  252. */
  253. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  254. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  255. avail_dsds = 5;
  256. }
  257. sle_dma = sg_dma_address(sg);
  258. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  259. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  260. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  261. avail_dsds--;
  262. }
  263. }
  264. /**
  265. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  266. * @sp: command to send to the ISP
  267. *
  268. * Returns non-zero if a failure occurred, else zero.
  269. */
  270. int
  271. qla2x00_start_scsi(srb_t *sp)
  272. {
  273. int ret, nseg;
  274. unsigned long flags;
  275. scsi_qla_host_t *vha;
  276. struct scsi_cmnd *cmd;
  277. uint32_t *clr_ptr;
  278. uint32_t index;
  279. uint32_t handle;
  280. cmd_entry_t *cmd_pkt;
  281. uint16_t cnt;
  282. uint16_t req_cnt;
  283. uint16_t tot_dsds;
  284. struct device_reg_2xxx __iomem *reg;
  285. struct qla_hw_data *ha;
  286. struct req_que *req;
  287. struct rsp_que *rsp;
  288. /* Setup device pointers. */
  289. ret = 0;
  290. vha = sp->fcport->vha;
  291. ha = vha->hw;
  292. reg = &ha->iobase->isp;
  293. cmd = sp->cmd;
  294. req = ha->req_q_map[0];
  295. rsp = ha->rsp_q_map[0];
  296. /* So we know we haven't pci_map'ed anything yet */
  297. tot_dsds = 0;
  298. /* Send marker if required */
  299. if (vha->marker_needed != 0) {
  300. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  301. != QLA_SUCCESS)
  302. return (QLA_FUNCTION_FAILED);
  303. vha->marker_needed = 0;
  304. }
  305. /* Acquire ring specific lock */
  306. spin_lock_irqsave(&ha->hardware_lock, flags);
  307. /* Check for room in outstanding command list. */
  308. handle = req->current_outstanding_cmd;
  309. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  310. handle++;
  311. if (handle == MAX_OUTSTANDING_COMMANDS)
  312. handle = 1;
  313. if (!req->outstanding_cmds[handle])
  314. break;
  315. }
  316. if (index == MAX_OUTSTANDING_COMMANDS)
  317. goto queuing_error;
  318. /* Map the sg table so we have an accurate count of sg entries needed */
  319. if (scsi_sg_count(cmd)) {
  320. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  321. scsi_sg_count(cmd), cmd->sc_data_direction);
  322. if (unlikely(!nseg))
  323. goto queuing_error;
  324. } else
  325. nseg = 0;
  326. tot_dsds = nseg;
  327. /* Calculate the number of request entries needed. */
  328. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  329. if (req->cnt < (req_cnt + 2)) {
  330. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  331. if (req->ring_index < cnt)
  332. req->cnt = cnt - req->ring_index;
  333. else
  334. req->cnt = req->length -
  335. (req->ring_index - cnt);
  336. }
  337. if (req->cnt < (req_cnt + 2))
  338. goto queuing_error;
  339. /* Build command packet */
  340. req->current_outstanding_cmd = handle;
  341. req->outstanding_cmds[handle] = sp;
  342. sp->handle = handle;
  343. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  344. req->cnt -= req_cnt;
  345. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  346. cmd_pkt->handle = handle;
  347. /* Zero out remaining portion of packet. */
  348. clr_ptr = (uint32_t *)cmd_pkt + 2;
  349. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  350. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  351. /* Set target ID and LUN number*/
  352. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  353. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  354. /* Update tagged queuing modifier */
  355. cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
  356. /* Load SCSI command packet. */
  357. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  358. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  359. /* Build IOCB segments */
  360. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  361. /* Set total data segment count. */
  362. cmd_pkt->entry_count = (uint8_t)req_cnt;
  363. wmb();
  364. /* Adjust ring index. */
  365. req->ring_index++;
  366. if (req->ring_index == req->length) {
  367. req->ring_index = 0;
  368. req->ring_ptr = req->ring;
  369. } else
  370. req->ring_ptr++;
  371. sp->flags |= SRB_DMA_VALID;
  372. /* Set chip new ring index. */
  373. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  374. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  375. /* Manage unprocessed RIO/ZIO commands in response queue. */
  376. if (vha->flags.process_response_queue &&
  377. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  378. qla2x00_process_response_queue(rsp);
  379. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  380. return (QLA_SUCCESS);
  381. queuing_error:
  382. if (tot_dsds)
  383. scsi_dma_unmap(cmd);
  384. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  385. return (QLA_FUNCTION_FAILED);
  386. }
  387. /**
  388. * qla2x00_marker() - Send a marker IOCB to the firmware.
  389. * @ha: HA context
  390. * @loop_id: loop ID
  391. * @lun: LUN
  392. * @type: marker modifier
  393. *
  394. * Can be called from both normal and interrupt context.
  395. *
  396. * Returns non-zero if a failure occurred, else zero.
  397. */
  398. int
  399. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  400. struct rsp_que *rsp, uint16_t loop_id,
  401. uint16_t lun, uint8_t type)
  402. {
  403. mrk_entry_t *mrk;
  404. struct mrk_entry_24xx *mrk24;
  405. struct qla_hw_data *ha = vha->hw;
  406. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  407. mrk24 = NULL;
  408. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
  409. if (mrk == NULL) {
  410. DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
  411. __func__, base_vha->host_no));
  412. return (QLA_FUNCTION_FAILED);
  413. }
  414. mrk->entry_type = MARKER_TYPE;
  415. mrk->modifier = type;
  416. if (type != MK_SYNC_ALL) {
  417. if (IS_FWI2_CAPABLE(ha)) {
  418. mrk24 = (struct mrk_entry_24xx *) mrk;
  419. mrk24->nport_handle = cpu_to_le16(loop_id);
  420. mrk24->lun[1] = LSB(lun);
  421. mrk24->lun[2] = MSB(lun);
  422. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  423. mrk24->vp_index = vha->vp_idx;
  424. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  425. } else {
  426. SET_TARGET_ID(ha, mrk->target, loop_id);
  427. mrk->lun = cpu_to_le16(lun);
  428. }
  429. }
  430. wmb();
  431. qla2x00_isp_cmd(vha, req);
  432. return (QLA_SUCCESS);
  433. }
  434. int
  435. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  436. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  437. uint8_t type)
  438. {
  439. int ret;
  440. unsigned long flags = 0;
  441. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  442. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  443. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  444. return (ret);
  445. }
  446. /**
  447. * qla2x00_isp_cmd() - Modify the request ring pointer.
  448. * @ha: HA context
  449. *
  450. * Note: The caller must hold the hardware lock before calling this routine.
  451. */
  452. static void
  453. qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
  454. {
  455. struct qla_hw_data *ha = vha->hw;
  456. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  457. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  458. DEBUG5(printk("%s(): IOCB data:\n", __func__));
  459. DEBUG5(qla2x00_dump_buffer(
  460. (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
  461. /* Adjust ring index. */
  462. req->ring_index++;
  463. if (req->ring_index == req->length) {
  464. req->ring_index = 0;
  465. req->ring_ptr = req->ring;
  466. } else
  467. req->ring_ptr++;
  468. /* Set chip new ring index. */
  469. if (IS_QLA82XX(ha)) {
  470. uint32_t dbval = 0x04 | (ha->portnum << 5);
  471. /* write, read and verify logic */
  472. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  473. if (ql2xdbwr)
  474. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  475. else {
  476. WRT_REG_DWORD(
  477. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  478. dbval);
  479. wmb();
  480. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  481. WRT_REG_DWORD((unsigned long __iomem *)
  482. ha->nxdb_wr_ptr, dbval);
  483. wmb();
  484. }
  485. }
  486. } else if (ha->mqenable) {
  487. /* Set chip new ring index. */
  488. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  489. RD_REG_DWORD(&ioreg->hccr);
  490. } else {
  491. if (IS_FWI2_CAPABLE(ha)) {
  492. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  493. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  494. } else {
  495. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  496. req->ring_index);
  497. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  498. }
  499. }
  500. }
  501. /**
  502. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  503. * Continuation Type 1 IOCBs to allocate.
  504. *
  505. * @dsds: number of data segment decriptors needed
  506. *
  507. * Returns the number of IOCB entries needed to store @dsds.
  508. */
  509. inline uint16_t
  510. qla24xx_calc_iocbs(uint16_t dsds)
  511. {
  512. uint16_t iocbs;
  513. iocbs = 1;
  514. if (dsds > 1) {
  515. iocbs += (dsds - 1) / 5;
  516. if ((dsds - 1) % 5)
  517. iocbs++;
  518. }
  519. DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
  520. __func__, iocbs));
  521. return iocbs;
  522. }
  523. /**
  524. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  525. * IOCB types.
  526. *
  527. * @sp: SRB command to process
  528. * @cmd_pkt: Command type 3 IOCB
  529. * @tot_dsds: Total number of segments to transfer
  530. */
  531. inline void
  532. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  533. uint16_t tot_dsds)
  534. {
  535. uint16_t avail_dsds;
  536. uint32_t *cur_dsd;
  537. scsi_qla_host_t *vha;
  538. struct scsi_cmnd *cmd;
  539. struct scatterlist *sg;
  540. int i;
  541. struct req_que *req;
  542. cmd = sp->cmd;
  543. /* Update entry type to indicate Command Type 3 IOCB */
  544. *((uint32_t *)(&cmd_pkt->entry_type)) =
  545. __constant_cpu_to_le32(COMMAND_TYPE_7);
  546. /* No data transfer */
  547. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  548. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  549. return;
  550. }
  551. vha = sp->fcport->vha;
  552. req = vha->req;
  553. /* Set transfer direction */
  554. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  555. cmd_pkt->task_mgmt_flags =
  556. __constant_cpu_to_le16(TMF_WRITE_DATA);
  557. sp->fcport->vha->hw->qla_stats.output_bytes +=
  558. scsi_bufflen(sp->cmd);
  559. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  560. cmd_pkt->task_mgmt_flags =
  561. __constant_cpu_to_le16(TMF_READ_DATA);
  562. sp->fcport->vha->hw->qla_stats.input_bytes +=
  563. scsi_bufflen(sp->cmd);
  564. }
  565. /* One DSD is available in the Command Type 3 IOCB */
  566. avail_dsds = 1;
  567. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  568. /* Load data segments */
  569. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  570. dma_addr_t sle_dma;
  571. cont_a64_entry_t *cont_pkt;
  572. /* Allocate additional continuation packets? */
  573. if (avail_dsds == 0) {
  574. /*
  575. * Five DSDs are available in the Continuation
  576. * Type 1 IOCB.
  577. */
  578. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  579. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  580. avail_dsds = 5;
  581. }
  582. sle_dma = sg_dma_address(sg);
  583. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  584. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  585. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  586. avail_dsds--;
  587. }
  588. }
  589. struct fw_dif_context {
  590. uint32_t ref_tag;
  591. uint16_t app_tag;
  592. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  593. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  594. };
  595. /*
  596. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  597. *
  598. */
  599. static inline void
  600. qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
  601. unsigned int protcnt)
  602. {
  603. struct sd_dif_tuple *spt;
  604. unsigned char op = scsi_get_prot_op(cmd);
  605. switch (scsi_get_prot_type(cmd)) {
  606. /* For TYPE 0 protection: no checking */
  607. case SCSI_PROT_DIF_TYPE0:
  608. pkt->ref_tag_mask[0] = 0x00;
  609. pkt->ref_tag_mask[1] = 0x00;
  610. pkt->ref_tag_mask[2] = 0x00;
  611. pkt->ref_tag_mask[3] = 0x00;
  612. break;
  613. /*
  614. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  615. * match LBA in CDB + N
  616. */
  617. case SCSI_PROT_DIF_TYPE2:
  618. break;
  619. /* For Type 3 protection: 16 bit GUARD only */
  620. case SCSI_PROT_DIF_TYPE3:
  621. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  622. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  623. 0x00;
  624. break;
  625. /*
  626. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  627. * 16 bit app tag.
  628. */
  629. case SCSI_PROT_DIF_TYPE1:
  630. if (!ql2xenablehba_err_chk)
  631. break;
  632. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  633. op == SCSI_PROT_WRITE_PASS)) {
  634. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  635. scsi_prot_sglist(cmd)[0].offset;
  636. DEBUG18(printk(KERN_DEBUG
  637. "%s(): LBA from user %p, lba = 0x%x\n",
  638. __func__, spt, (int)spt->ref_tag));
  639. pkt->ref_tag = swab32(spt->ref_tag);
  640. pkt->app_tag_mask[0] = 0x0;
  641. pkt->app_tag_mask[1] = 0x0;
  642. } else {
  643. pkt->ref_tag = cpu_to_le32((uint32_t)
  644. (0xffffffff & scsi_get_lba(cmd)));
  645. pkt->app_tag = __constant_cpu_to_le16(0);
  646. pkt->app_tag_mask[0] = 0x0;
  647. pkt->app_tag_mask[1] = 0x0;
  648. }
  649. /* enable ALL bytes of the ref tag */
  650. pkt->ref_tag_mask[0] = 0xff;
  651. pkt->ref_tag_mask[1] = 0xff;
  652. pkt->ref_tag_mask[2] = 0xff;
  653. pkt->ref_tag_mask[3] = 0xff;
  654. break;
  655. }
  656. DEBUG18(printk(KERN_DEBUG
  657. "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
  658. " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
  659. " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
  660. (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
  661. }
  662. static int
  663. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  664. uint16_t tot_dsds)
  665. {
  666. void *next_dsd;
  667. uint8_t avail_dsds = 0;
  668. uint32_t dsd_list_len;
  669. struct dsd_dma *dsd_ptr;
  670. struct scatterlist *sg;
  671. uint32_t *cur_dsd = dsd;
  672. int i;
  673. uint16_t used_dsds = tot_dsds;
  674. uint8_t *cp;
  675. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  676. dma_addr_t sle_dma;
  677. /* Allocate additional continuation packets? */
  678. if (avail_dsds == 0) {
  679. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  680. QLA_DSDS_PER_IOCB : used_dsds;
  681. dsd_list_len = (avail_dsds + 1) * 12;
  682. used_dsds -= avail_dsds;
  683. /* allocate tracking DS */
  684. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  685. if (!dsd_ptr)
  686. return 1;
  687. /* allocate new list */
  688. dsd_ptr->dsd_addr = next_dsd =
  689. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  690. &dsd_ptr->dsd_list_dma);
  691. if (!next_dsd) {
  692. /*
  693. * Need to cleanup only this dsd_ptr, rest
  694. * will be done by sp_free_dma()
  695. */
  696. kfree(dsd_ptr);
  697. return 1;
  698. }
  699. list_add_tail(&dsd_ptr->list,
  700. &((struct crc_context *)sp->ctx)->dsd_list);
  701. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  702. /* add new list to cmd iocb or last list */
  703. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  704. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  705. *cur_dsd++ = dsd_list_len;
  706. cur_dsd = (uint32_t *)next_dsd;
  707. }
  708. sle_dma = sg_dma_address(sg);
  709. DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
  710. " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
  711. MSD(sle_dma), sg_dma_len(sg)));
  712. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  713. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  714. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  715. avail_dsds--;
  716. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  717. cp = page_address(sg_page(sg)) + sg->offset;
  718. DEBUG18(printk("%s(): User Data buffer= %p:\n",
  719. __func__ , cp));
  720. }
  721. }
  722. /* Null termination */
  723. *cur_dsd++ = 0;
  724. *cur_dsd++ = 0;
  725. *cur_dsd++ = 0;
  726. return 0;
  727. }
  728. static int
  729. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  730. uint32_t *dsd,
  731. uint16_t tot_dsds)
  732. {
  733. void *next_dsd;
  734. uint8_t avail_dsds = 0;
  735. uint32_t dsd_list_len;
  736. struct dsd_dma *dsd_ptr;
  737. struct scatterlist *sg;
  738. int i;
  739. struct scsi_cmnd *cmd;
  740. uint32_t *cur_dsd = dsd;
  741. uint16_t used_dsds = tot_dsds;
  742. uint8_t *cp;
  743. cmd = sp->cmd;
  744. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  745. dma_addr_t sle_dma;
  746. /* Allocate additional continuation packets? */
  747. if (avail_dsds == 0) {
  748. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  749. QLA_DSDS_PER_IOCB : used_dsds;
  750. dsd_list_len = (avail_dsds + 1) * 12;
  751. used_dsds -= avail_dsds;
  752. /* allocate tracking DS */
  753. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  754. if (!dsd_ptr)
  755. return 1;
  756. /* allocate new list */
  757. dsd_ptr->dsd_addr = next_dsd =
  758. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  759. &dsd_ptr->dsd_list_dma);
  760. if (!next_dsd) {
  761. /*
  762. * Need to cleanup only this dsd_ptr, rest
  763. * will be done by sp_free_dma()
  764. */
  765. kfree(dsd_ptr);
  766. return 1;
  767. }
  768. list_add_tail(&dsd_ptr->list,
  769. &((struct crc_context *)sp->ctx)->dsd_list);
  770. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  771. /* add new list to cmd iocb or last list */
  772. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  773. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  774. *cur_dsd++ = dsd_list_len;
  775. cur_dsd = (uint32_t *)next_dsd;
  776. }
  777. sle_dma = sg_dma_address(sg);
  778. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  779. DEBUG18(printk(KERN_DEBUG
  780. "%s(): %p, sg entry %d - addr =0x%x"
  781. "0x%x, len =%d\n", __func__ , cur_dsd, i,
  782. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
  783. }
  784. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  785. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  786. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  787. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  788. cp = page_address(sg_page(sg)) + sg->offset;
  789. DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
  790. __func__ , cp));
  791. }
  792. avail_dsds--;
  793. }
  794. /* Null termination */
  795. *cur_dsd++ = 0;
  796. *cur_dsd++ = 0;
  797. *cur_dsd++ = 0;
  798. return 0;
  799. }
  800. /**
  801. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  802. * Type 6 IOCB types.
  803. *
  804. * @sp: SRB command to process
  805. * @cmd_pkt: Command type 3 IOCB
  806. * @tot_dsds: Total number of segments to transfer
  807. */
  808. static inline int
  809. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  810. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  811. {
  812. uint32_t *cur_dsd, *fcp_dl;
  813. scsi_qla_host_t *vha;
  814. struct scsi_cmnd *cmd;
  815. struct scatterlist *cur_seg;
  816. int sgc;
  817. uint32_t total_bytes;
  818. uint32_t data_bytes;
  819. uint32_t dif_bytes;
  820. uint8_t bundling = 1;
  821. uint16_t blk_size;
  822. uint8_t *clr_ptr;
  823. struct crc_context *crc_ctx_pkt = NULL;
  824. struct qla_hw_data *ha;
  825. uint8_t additional_fcpcdb_len;
  826. uint16_t fcp_cmnd_len;
  827. struct fcp_cmnd *fcp_cmnd;
  828. dma_addr_t crc_ctx_dma;
  829. cmd = sp->cmd;
  830. sgc = 0;
  831. /* Update entry type to indicate Command Type CRC_2 IOCB */
  832. *((uint32_t *)(&cmd_pkt->entry_type)) =
  833. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  834. /* No data transfer */
  835. data_bytes = scsi_bufflen(cmd);
  836. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  837. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  838. __func__, data_bytes));
  839. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  840. return QLA_SUCCESS;
  841. }
  842. vha = sp->fcport->vha;
  843. ha = vha->hw;
  844. DEBUG18(printk(KERN_DEBUG
  845. "%s(%ld): Executing cmd sp %p, pid=%ld, prot_op=%u.\n", __func__,
  846. vha->host_no, sp, cmd->serial_number, scsi_get_prot_op(sp->cmd)));
  847. cmd_pkt->vp_index = sp->fcport->vp_idx;
  848. /* Set transfer direction */
  849. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  850. cmd_pkt->control_flags =
  851. __constant_cpu_to_le16(CF_WRITE_DATA);
  852. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  853. cmd_pkt->control_flags =
  854. __constant_cpu_to_le16(CF_READ_DATA);
  855. }
  856. tot_prot_dsds = scsi_prot_sg_count(cmd);
  857. if (!tot_prot_dsds)
  858. bundling = 0;
  859. /* Allocate CRC context from global pool */
  860. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  861. GFP_ATOMIC, &crc_ctx_dma);
  862. if (!crc_ctx_pkt)
  863. goto crc_queuing_error;
  864. /* Zero out CTX area. */
  865. clr_ptr = (uint8_t *)crc_ctx_pkt;
  866. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  867. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  868. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  869. /* Set handle */
  870. crc_ctx_pkt->handle = cmd_pkt->handle;
  871. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  872. qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
  873. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  874. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  875. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  876. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  877. /* Determine SCSI command length -- align to 4 byte boundary */
  878. if (cmd->cmd_len > 16) {
  879. DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
  880. __func__));
  881. additional_fcpcdb_len = cmd->cmd_len - 16;
  882. if ((cmd->cmd_len % 4) != 0) {
  883. /* SCSI cmd > 16 bytes must be multiple of 4 */
  884. goto crc_queuing_error;
  885. }
  886. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  887. } else {
  888. additional_fcpcdb_len = 0;
  889. fcp_cmnd_len = 12 + 16 + 4;
  890. }
  891. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  892. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  893. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  894. fcp_cmnd->additional_cdb_len |= 1;
  895. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  896. fcp_cmnd->additional_cdb_len |= 2;
  897. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  898. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  899. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  900. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  901. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  902. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  903. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  904. fcp_cmnd->task_attribute = 0;
  905. fcp_cmnd->task_managment = 0;
  906. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  907. DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
  908. "entries %d, data bytes %d, Protection entries %d\n",
  909. __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
  910. data_bytes, tot_prot_dsds));
  911. /* Compute dif len and adjust data len to incude protection */
  912. total_bytes = data_bytes;
  913. dif_bytes = 0;
  914. blk_size = cmd->device->sector_size;
  915. if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE1) {
  916. dif_bytes = (data_bytes / blk_size) * 8;
  917. total_bytes += dif_bytes;
  918. }
  919. if (!ql2xenablehba_err_chk)
  920. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  921. if (!bundling) {
  922. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  923. } else {
  924. /*
  925. * Configure Bundling if we need to fetch interlaving
  926. * protection PCI accesses
  927. */
  928. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  929. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  930. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  931. tot_prot_dsds);
  932. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  933. }
  934. /* Finish the common fields of CRC pkt */
  935. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  936. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  937. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  938. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  939. /* Fibre channel byte count */
  940. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  941. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  942. additional_fcpcdb_len);
  943. *fcp_dl = htonl(total_bytes);
  944. DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
  945. " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
  946. vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
  947. crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
  948. /* Walks data segments */
  949. cmd_pkt->control_flags |=
  950. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  951. if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  952. (tot_dsds - tot_prot_dsds)))
  953. goto crc_queuing_error;
  954. if (bundling && tot_prot_dsds) {
  955. /* Walks dif segments */
  956. cur_seg = scsi_prot_sglist(cmd);
  957. cmd_pkt->control_flags |=
  958. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  959. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  960. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  961. tot_prot_dsds))
  962. goto crc_queuing_error;
  963. }
  964. return QLA_SUCCESS;
  965. crc_queuing_error:
  966. DEBUG18(qla_printk(KERN_INFO, ha,
  967. "CMD sent FAILED crc_q error:sp = %p\n", sp));
  968. /* Cleanup will be performed by the caller */
  969. return QLA_FUNCTION_FAILED;
  970. }
  971. /**
  972. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  973. * @sp: command to send to the ISP
  974. *
  975. * Returns non-zero if a failure occurred, else zero.
  976. */
  977. int
  978. qla24xx_start_scsi(srb_t *sp)
  979. {
  980. int ret, nseg;
  981. unsigned long flags;
  982. uint32_t *clr_ptr;
  983. uint32_t index;
  984. uint32_t handle;
  985. struct cmd_type_7 *cmd_pkt;
  986. uint16_t cnt;
  987. uint16_t req_cnt;
  988. uint16_t tot_dsds;
  989. struct req_que *req = NULL;
  990. struct rsp_que *rsp = NULL;
  991. struct scsi_cmnd *cmd = sp->cmd;
  992. struct scsi_qla_host *vha = sp->fcport->vha;
  993. struct qla_hw_data *ha = vha->hw;
  994. /* Setup device pointers. */
  995. ret = 0;
  996. qla25xx_set_que(sp, &rsp);
  997. req = vha->req;
  998. /* So we know we haven't pci_map'ed anything yet */
  999. tot_dsds = 0;
  1000. /* Send marker if required */
  1001. if (vha->marker_needed != 0) {
  1002. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  1003. != QLA_SUCCESS)
  1004. return QLA_FUNCTION_FAILED;
  1005. vha->marker_needed = 0;
  1006. }
  1007. /* Acquire ring specific lock */
  1008. spin_lock_irqsave(&ha->hardware_lock, flags);
  1009. /* Check for room in outstanding command list. */
  1010. handle = req->current_outstanding_cmd;
  1011. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1012. handle++;
  1013. if (handle == MAX_OUTSTANDING_COMMANDS)
  1014. handle = 1;
  1015. if (!req->outstanding_cmds[handle])
  1016. break;
  1017. }
  1018. if (index == MAX_OUTSTANDING_COMMANDS)
  1019. goto queuing_error;
  1020. /* Map the sg table so we have an accurate count of sg entries needed */
  1021. if (scsi_sg_count(cmd)) {
  1022. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1023. scsi_sg_count(cmd), cmd->sc_data_direction);
  1024. if (unlikely(!nseg))
  1025. goto queuing_error;
  1026. } else
  1027. nseg = 0;
  1028. tot_dsds = nseg;
  1029. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  1030. if (req->cnt < (req_cnt + 2)) {
  1031. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1032. if (req->ring_index < cnt)
  1033. req->cnt = cnt - req->ring_index;
  1034. else
  1035. req->cnt = req->length -
  1036. (req->ring_index - cnt);
  1037. }
  1038. if (req->cnt < (req_cnt + 2))
  1039. goto queuing_error;
  1040. /* Build command packet. */
  1041. req->current_outstanding_cmd = handle;
  1042. req->outstanding_cmds[handle] = sp;
  1043. sp->handle = handle;
  1044. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1045. req->cnt -= req_cnt;
  1046. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1047. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1048. /* Zero out remaining portion of packet. */
  1049. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1050. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1051. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1052. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1053. /* Set NPORT-ID and LUN number*/
  1054. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1055. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1056. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1057. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1058. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1059. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1060. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1061. /* Load SCSI command packet. */
  1062. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1063. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1064. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1065. /* Build IOCB segments */
  1066. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1067. /* Set total data segment count. */
  1068. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1069. /* Specify response queue number where completion should happen */
  1070. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1071. wmb();
  1072. /* Adjust ring index. */
  1073. req->ring_index++;
  1074. if (req->ring_index == req->length) {
  1075. req->ring_index = 0;
  1076. req->ring_ptr = req->ring;
  1077. } else
  1078. req->ring_ptr++;
  1079. sp->flags |= SRB_DMA_VALID;
  1080. /* Set chip new ring index. */
  1081. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1082. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1083. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1084. if (vha->flags.process_response_queue &&
  1085. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1086. qla24xx_process_response_queue(vha, rsp);
  1087. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1088. return QLA_SUCCESS;
  1089. queuing_error:
  1090. if (tot_dsds)
  1091. scsi_dma_unmap(cmd);
  1092. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1093. return QLA_FUNCTION_FAILED;
  1094. }
  1095. /**
  1096. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1097. * @sp: command to send to the ISP
  1098. *
  1099. * Returns non-zero if a failure occurred, else zero.
  1100. */
  1101. int
  1102. qla24xx_dif_start_scsi(srb_t *sp)
  1103. {
  1104. int nseg;
  1105. unsigned long flags;
  1106. uint32_t *clr_ptr;
  1107. uint32_t index;
  1108. uint32_t handle;
  1109. uint16_t cnt;
  1110. uint16_t req_cnt = 0;
  1111. uint16_t tot_dsds;
  1112. uint16_t tot_prot_dsds;
  1113. uint16_t fw_prot_opts = 0;
  1114. struct req_que *req = NULL;
  1115. struct rsp_que *rsp = NULL;
  1116. struct scsi_cmnd *cmd = sp->cmd;
  1117. struct scsi_qla_host *vha = sp->fcport->vha;
  1118. struct qla_hw_data *ha = vha->hw;
  1119. struct cmd_type_crc_2 *cmd_pkt;
  1120. uint32_t status = 0;
  1121. #define QDSS_GOT_Q_SPACE BIT_0
  1122. /* Only process protection in this routine */
  1123. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL)
  1124. return qla24xx_start_scsi(sp);
  1125. /* Setup device pointers. */
  1126. qla25xx_set_que(sp, &rsp);
  1127. req = vha->req;
  1128. /* So we know we haven't pci_map'ed anything yet */
  1129. tot_dsds = 0;
  1130. /* Send marker if required */
  1131. if (vha->marker_needed != 0) {
  1132. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1133. QLA_SUCCESS)
  1134. return QLA_FUNCTION_FAILED;
  1135. vha->marker_needed = 0;
  1136. }
  1137. /* Acquire ring specific lock */
  1138. spin_lock_irqsave(&ha->hardware_lock, flags);
  1139. /* Check for room in outstanding command list. */
  1140. handle = req->current_outstanding_cmd;
  1141. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1142. handle++;
  1143. if (handle == MAX_OUTSTANDING_COMMANDS)
  1144. handle = 1;
  1145. if (!req->outstanding_cmds[handle])
  1146. break;
  1147. }
  1148. if (index == MAX_OUTSTANDING_COMMANDS)
  1149. goto queuing_error;
  1150. /* Compute number of required data segments */
  1151. /* Map the sg table so we have an accurate count of sg entries needed */
  1152. if (scsi_sg_count(cmd)) {
  1153. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1154. scsi_sg_count(cmd), cmd->sc_data_direction);
  1155. if (unlikely(!nseg))
  1156. goto queuing_error;
  1157. else
  1158. sp->flags |= SRB_DMA_VALID;
  1159. } else
  1160. nseg = 0;
  1161. /* number of required data segments */
  1162. tot_dsds = nseg;
  1163. /* Compute number of required protection segments */
  1164. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1165. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1166. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1167. if (unlikely(!nseg))
  1168. goto queuing_error;
  1169. else
  1170. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1171. } else {
  1172. nseg = 0;
  1173. }
  1174. req_cnt = 1;
  1175. /* Total Data and protection sg segment(s) */
  1176. tot_prot_dsds = nseg;
  1177. tot_dsds += nseg;
  1178. if (req->cnt < (req_cnt + 2)) {
  1179. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1180. if (req->ring_index < cnt)
  1181. req->cnt = cnt - req->ring_index;
  1182. else
  1183. req->cnt = req->length -
  1184. (req->ring_index - cnt);
  1185. }
  1186. if (req->cnt < (req_cnt + 2))
  1187. goto queuing_error;
  1188. status |= QDSS_GOT_Q_SPACE;
  1189. /* Build header part of command packet (excluding the OPCODE). */
  1190. req->current_outstanding_cmd = handle;
  1191. req->outstanding_cmds[handle] = sp;
  1192. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1193. req->cnt -= req_cnt;
  1194. /* Fill-in common area */
  1195. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1196. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1197. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1198. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1199. /* Set NPORT-ID and LUN number*/
  1200. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1201. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1202. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1203. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1204. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1205. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1206. /* Total Data and protection segment(s) */
  1207. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1208. /* Build IOCB segments and adjust for data protection segments */
  1209. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1210. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1211. QLA_SUCCESS)
  1212. goto queuing_error;
  1213. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1214. /* Specify response queue number where completion should happen */
  1215. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1216. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1217. wmb();
  1218. /* Adjust ring index. */
  1219. req->ring_index++;
  1220. if (req->ring_index == req->length) {
  1221. req->ring_index = 0;
  1222. req->ring_ptr = req->ring;
  1223. } else
  1224. req->ring_ptr++;
  1225. /* Set chip new ring index. */
  1226. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1227. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1228. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1229. if (vha->flags.process_response_queue &&
  1230. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1231. qla24xx_process_response_queue(vha, rsp);
  1232. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1233. return QLA_SUCCESS;
  1234. queuing_error:
  1235. if (status & QDSS_GOT_Q_SPACE) {
  1236. req->outstanding_cmds[handle] = NULL;
  1237. req->cnt += req_cnt;
  1238. }
  1239. /* Cleanup will be performed by the caller (queuecommand) */
  1240. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1241. DEBUG18(qla_printk(KERN_INFO, ha,
  1242. "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
  1243. return QLA_FUNCTION_FAILED;
  1244. }
  1245. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1246. {
  1247. struct scsi_cmnd *cmd = sp->cmd;
  1248. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1249. int affinity = cmd->request->cpu;
  1250. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1251. affinity < ha->max_rsp_queues - 1)
  1252. *rsp = ha->rsp_q_map[affinity + 1];
  1253. else
  1254. *rsp = ha->rsp_q_map[0];
  1255. }
  1256. /* Generic Control-SRB manipulation functions. */
  1257. void *
  1258. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1259. {
  1260. struct qla_hw_data *ha = vha->hw;
  1261. struct req_que *req = ha->req_q_map[0];
  1262. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1263. uint32_t index, handle;
  1264. request_t *pkt;
  1265. uint16_t cnt, req_cnt;
  1266. pkt = NULL;
  1267. req_cnt = 1;
  1268. handle = 0;
  1269. if (!sp)
  1270. goto skip_cmd_array;
  1271. /* Check for room in outstanding command list. */
  1272. handle = req->current_outstanding_cmd;
  1273. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1274. handle++;
  1275. if (handle == MAX_OUTSTANDING_COMMANDS)
  1276. handle = 1;
  1277. if (!req->outstanding_cmds[handle])
  1278. break;
  1279. }
  1280. if (index == MAX_OUTSTANDING_COMMANDS)
  1281. goto queuing_error;
  1282. /* Prep command array. */
  1283. req->current_outstanding_cmd = handle;
  1284. req->outstanding_cmds[handle] = sp;
  1285. sp->handle = handle;
  1286. skip_cmd_array:
  1287. /* Check for room on request queue. */
  1288. if (req->cnt < req_cnt) {
  1289. if (ha->mqenable)
  1290. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1291. else if (IS_QLA82XX(ha))
  1292. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1293. else if (IS_FWI2_CAPABLE(ha))
  1294. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1295. else
  1296. cnt = qla2x00_debounce_register(
  1297. ISP_REQ_Q_OUT(ha, &reg->isp));
  1298. if (req->ring_index < cnt)
  1299. req->cnt = cnt - req->ring_index;
  1300. else
  1301. req->cnt = req->length -
  1302. (req->ring_index - cnt);
  1303. }
  1304. if (req->cnt < req_cnt)
  1305. goto queuing_error;
  1306. /* Prep packet */
  1307. req->cnt -= req_cnt;
  1308. pkt = req->ring_ptr;
  1309. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1310. pkt->entry_count = req_cnt;
  1311. pkt->handle = handle;
  1312. queuing_error:
  1313. return pkt;
  1314. }
  1315. static void
  1316. qla2x00_start_iocbs(srb_t *sp)
  1317. {
  1318. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1319. struct req_que *req = ha->req_q_map[0];
  1320. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1321. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1322. if (IS_QLA82XX(ha)) {
  1323. qla82xx_start_iocbs(sp);
  1324. } else {
  1325. /* Adjust ring index. */
  1326. req->ring_index++;
  1327. if (req->ring_index == req->length) {
  1328. req->ring_index = 0;
  1329. req->ring_ptr = req->ring;
  1330. } else
  1331. req->ring_ptr++;
  1332. /* Set chip new ring index. */
  1333. if (ha->mqenable) {
  1334. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1335. RD_REG_DWORD(&ioreg->hccr);
  1336. } else if (IS_QLA82XX(ha)) {
  1337. qla82xx_start_iocbs(sp);
  1338. } else if (IS_FWI2_CAPABLE(ha)) {
  1339. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1340. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1341. } else {
  1342. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1343. req->ring_index);
  1344. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1345. }
  1346. }
  1347. }
  1348. static void
  1349. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1350. {
  1351. struct srb_ctx *ctx = sp->ctx;
  1352. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1353. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1354. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1355. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1356. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1357. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1358. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1359. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1360. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1361. logio->port_id[1] = sp->fcport->d_id.b.area;
  1362. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1363. logio->vp_index = sp->fcport->vp_idx;
  1364. }
  1365. static void
  1366. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1367. {
  1368. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1369. struct srb_ctx *ctx = sp->ctx;
  1370. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1371. uint16_t opts;
  1372. mbx->entry_type = MBX_IOCB_TYPE;
  1373. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1374. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1375. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1376. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1377. if (HAS_EXTENDED_IDS(ha)) {
  1378. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1379. mbx->mb10 = cpu_to_le16(opts);
  1380. } else {
  1381. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1382. }
  1383. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1384. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1385. sp->fcport->d_id.b.al_pa);
  1386. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1387. }
  1388. static void
  1389. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1390. {
  1391. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1392. logio->control_flags =
  1393. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1394. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1395. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1396. logio->port_id[1] = sp->fcport->d_id.b.area;
  1397. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1398. logio->vp_index = sp->fcport->vp_idx;
  1399. }
  1400. static void
  1401. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1402. {
  1403. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1404. mbx->entry_type = MBX_IOCB_TYPE;
  1405. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1406. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1407. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1408. cpu_to_le16(sp->fcport->loop_id):
  1409. cpu_to_le16(sp->fcport->loop_id << 8);
  1410. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1411. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1412. sp->fcport->d_id.b.al_pa);
  1413. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1414. /* Implicit: mbx->mbx10 = 0. */
  1415. }
  1416. static void
  1417. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1418. {
  1419. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1420. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1421. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1422. logio->vp_index = sp->fcport->vp_idx;
  1423. }
  1424. static void
  1425. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1426. {
  1427. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1428. mbx->entry_type = MBX_IOCB_TYPE;
  1429. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1430. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1431. if (HAS_EXTENDED_IDS(ha)) {
  1432. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1433. mbx->mb10 = cpu_to_le16(BIT_0);
  1434. } else {
  1435. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1436. }
  1437. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1438. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1439. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1440. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1441. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1442. }
  1443. static void
  1444. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1445. {
  1446. uint32_t flags;
  1447. unsigned int lun;
  1448. struct fc_port *fcport = sp->fcport;
  1449. scsi_qla_host_t *vha = fcport->vha;
  1450. struct qla_hw_data *ha = vha->hw;
  1451. struct srb_ctx *ctx = sp->ctx;
  1452. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1453. struct req_que *req = vha->req;
  1454. flags = iocb->u.tmf.flags;
  1455. lun = iocb->u.tmf.lun;
  1456. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1457. tsk->entry_count = 1;
  1458. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1459. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1460. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1461. tsk->control_flags = cpu_to_le32(flags);
  1462. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1463. tsk->port_id[1] = fcport->d_id.b.area;
  1464. tsk->port_id[2] = fcport->d_id.b.domain;
  1465. tsk->vp_index = fcport->vp_idx;
  1466. if (flags == TCF_LUN_RESET) {
  1467. int_to_scsilun(lun, &tsk->lun);
  1468. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1469. sizeof(tsk->lun));
  1470. }
  1471. }
  1472. static void
  1473. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1474. {
  1475. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1476. els_iocb->entry_type = ELS_IOCB_TYPE;
  1477. els_iocb->entry_count = 1;
  1478. els_iocb->sys_define = 0;
  1479. els_iocb->entry_status = 0;
  1480. els_iocb->handle = sp->handle;
  1481. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1482. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1483. els_iocb->vp_index = sp->fcport->vp_idx;
  1484. els_iocb->sof_type = EST_SOFI3;
  1485. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1486. els_iocb->opcode =
  1487. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1488. bsg_job->request->rqst_data.r_els.els_code :
  1489. bsg_job->request->rqst_data.h_els.command_code;
  1490. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1491. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1492. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1493. els_iocb->control_flags = 0;
  1494. els_iocb->rx_byte_count =
  1495. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1496. els_iocb->tx_byte_count =
  1497. cpu_to_le32(bsg_job->request_payload.payload_len);
  1498. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1499. (bsg_job->request_payload.sg_list)));
  1500. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1501. (bsg_job->request_payload.sg_list)));
  1502. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1503. (bsg_job->request_payload.sg_list));
  1504. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1505. (bsg_job->reply_payload.sg_list)));
  1506. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1507. (bsg_job->reply_payload.sg_list)));
  1508. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1509. (bsg_job->reply_payload.sg_list));
  1510. }
  1511. static void
  1512. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1513. {
  1514. uint16_t avail_dsds;
  1515. uint32_t *cur_dsd;
  1516. struct scatterlist *sg;
  1517. int index;
  1518. uint16_t tot_dsds;
  1519. scsi_qla_host_t *vha = sp->fcport->vha;
  1520. struct qla_hw_data *ha = vha->hw;
  1521. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1522. int loop_iterartion = 0;
  1523. int cont_iocb_prsnt = 0;
  1524. int entry_count = 1;
  1525. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1526. ct_iocb->entry_type = CT_IOCB_TYPE;
  1527. ct_iocb->entry_status = 0;
  1528. ct_iocb->handle1 = sp->handle;
  1529. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1530. ct_iocb->status = __constant_cpu_to_le16(0);
  1531. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1532. ct_iocb->timeout = 0;
  1533. ct_iocb->cmd_dsd_count =
  1534. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1535. ct_iocb->total_dsd_count =
  1536. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1537. ct_iocb->req_bytecount =
  1538. cpu_to_le32(bsg_job->request_payload.payload_len);
  1539. ct_iocb->rsp_bytecount =
  1540. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1541. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1542. (bsg_job->request_payload.sg_list)));
  1543. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1544. (bsg_job->request_payload.sg_list)));
  1545. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1546. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1547. (bsg_job->reply_payload.sg_list)));
  1548. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1549. (bsg_job->reply_payload.sg_list)));
  1550. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1551. avail_dsds = 1;
  1552. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1553. index = 0;
  1554. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1555. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1556. dma_addr_t sle_dma;
  1557. cont_a64_entry_t *cont_pkt;
  1558. /* Allocate additional continuation packets? */
  1559. if (avail_dsds == 0) {
  1560. /*
  1561. * Five DSDs are available in the Cont.
  1562. * Type 1 IOCB.
  1563. */
  1564. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1565. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1566. avail_dsds = 5;
  1567. cont_iocb_prsnt = 1;
  1568. entry_count++;
  1569. }
  1570. sle_dma = sg_dma_address(sg);
  1571. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1572. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1573. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1574. loop_iterartion++;
  1575. avail_dsds--;
  1576. }
  1577. ct_iocb->entry_count = entry_count;
  1578. }
  1579. static void
  1580. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1581. {
  1582. uint16_t avail_dsds;
  1583. uint32_t *cur_dsd;
  1584. struct scatterlist *sg;
  1585. int index;
  1586. uint16_t tot_dsds;
  1587. scsi_qla_host_t *vha = sp->fcport->vha;
  1588. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1589. int loop_iterartion = 0;
  1590. int cont_iocb_prsnt = 0;
  1591. int entry_count = 1;
  1592. ct_iocb->entry_type = CT_IOCB_TYPE;
  1593. ct_iocb->entry_status = 0;
  1594. ct_iocb->sys_define = 0;
  1595. ct_iocb->handle = sp->handle;
  1596. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1597. ct_iocb->vp_index = sp->fcport->vp_idx;
  1598. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1599. ct_iocb->cmd_dsd_count =
  1600. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1601. ct_iocb->timeout = 0;
  1602. ct_iocb->rsp_dsd_count =
  1603. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1604. ct_iocb->rsp_byte_count =
  1605. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1606. ct_iocb->cmd_byte_count =
  1607. cpu_to_le32(bsg_job->request_payload.payload_len);
  1608. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1609. (bsg_job->request_payload.sg_list)));
  1610. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1611. (bsg_job->request_payload.sg_list)));
  1612. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1613. (bsg_job->request_payload.sg_list));
  1614. avail_dsds = 1;
  1615. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1616. index = 0;
  1617. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1618. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1619. dma_addr_t sle_dma;
  1620. cont_a64_entry_t *cont_pkt;
  1621. /* Allocate additional continuation packets? */
  1622. if (avail_dsds == 0) {
  1623. /*
  1624. * Five DSDs are available in the Cont.
  1625. * Type 1 IOCB.
  1626. */
  1627. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1628. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1629. avail_dsds = 5;
  1630. cont_iocb_prsnt = 1;
  1631. entry_count++;
  1632. }
  1633. sle_dma = sg_dma_address(sg);
  1634. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1635. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1636. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1637. loop_iterartion++;
  1638. avail_dsds--;
  1639. }
  1640. ct_iocb->entry_count = entry_count;
  1641. }
  1642. int
  1643. qla2x00_start_sp(srb_t *sp)
  1644. {
  1645. int rval;
  1646. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1647. void *pkt;
  1648. struct srb_ctx *ctx = sp->ctx;
  1649. unsigned long flags;
  1650. rval = QLA_FUNCTION_FAILED;
  1651. spin_lock_irqsave(&ha->hardware_lock, flags);
  1652. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1653. if (!pkt)
  1654. goto done;
  1655. rval = QLA_SUCCESS;
  1656. switch (ctx->type) {
  1657. case SRB_LOGIN_CMD:
  1658. IS_FWI2_CAPABLE(ha) ?
  1659. qla24xx_login_iocb(sp, pkt) :
  1660. qla2x00_login_iocb(sp, pkt);
  1661. break;
  1662. case SRB_LOGOUT_CMD:
  1663. IS_FWI2_CAPABLE(ha) ?
  1664. qla24xx_logout_iocb(sp, pkt) :
  1665. qla2x00_logout_iocb(sp, pkt);
  1666. break;
  1667. case SRB_ELS_CMD_RPT:
  1668. case SRB_ELS_CMD_HST:
  1669. qla24xx_els_iocb(sp, pkt);
  1670. break;
  1671. case SRB_CT_CMD:
  1672. IS_FWI2_CAPABLE(ha) ?
  1673. qla24xx_ct_iocb(sp, pkt) :
  1674. qla2x00_ct_iocb(sp, pkt);
  1675. break;
  1676. case SRB_ADISC_CMD:
  1677. IS_FWI2_CAPABLE(ha) ?
  1678. qla24xx_adisc_iocb(sp, pkt) :
  1679. qla2x00_adisc_iocb(sp, pkt);
  1680. break;
  1681. case SRB_TM_CMD:
  1682. qla24xx_tm_iocb(sp, pkt);
  1683. break;
  1684. default:
  1685. break;
  1686. }
  1687. wmb();
  1688. qla2x00_start_iocbs(sp);
  1689. done:
  1690. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1691. return rval;
  1692. }