qla_iocb.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2010 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. static 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. if (!ql2xenablehba_err_chk)
  619. break;
  620. if (scsi_prot_sg_count(cmd)) {
  621. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  622. scsi_prot_sglist(cmd)[0].offset;
  623. pkt->app_tag = swab32(spt->app_tag);
  624. pkt->app_tag_mask[0] = 0xff;
  625. pkt->app_tag_mask[1] = 0xff;
  626. }
  627. pkt->ref_tag = cpu_to_le32((uint32_t)
  628. (0xffffffff & scsi_get_lba(cmd)));
  629. /* enable ALL bytes of the ref tag */
  630. pkt->ref_tag_mask[0] = 0xff;
  631. pkt->ref_tag_mask[1] = 0xff;
  632. pkt->ref_tag_mask[2] = 0xff;
  633. pkt->ref_tag_mask[3] = 0xff;
  634. break;
  635. /* For Type 3 protection: 16 bit GUARD only */
  636. case SCSI_PROT_DIF_TYPE3:
  637. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  638. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  639. 0x00;
  640. break;
  641. /*
  642. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  643. * 16 bit app tag.
  644. */
  645. case SCSI_PROT_DIF_TYPE1:
  646. if (!ql2xenablehba_err_chk)
  647. break;
  648. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  649. op == SCSI_PROT_WRITE_PASS)) {
  650. spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
  651. scsi_prot_sglist(cmd)[0].offset;
  652. DEBUG18(printk(KERN_DEBUG
  653. "%s(): LBA from user %p, lba = 0x%x\n",
  654. __func__, spt, (int)spt->ref_tag));
  655. pkt->ref_tag = swab32(spt->ref_tag);
  656. pkt->app_tag_mask[0] = 0x0;
  657. pkt->app_tag_mask[1] = 0x0;
  658. } else {
  659. pkt->ref_tag = cpu_to_le32((uint32_t)
  660. (0xffffffff & scsi_get_lba(cmd)));
  661. pkt->app_tag = __constant_cpu_to_le16(0);
  662. pkt->app_tag_mask[0] = 0x0;
  663. pkt->app_tag_mask[1] = 0x0;
  664. }
  665. /* enable ALL bytes of the ref tag */
  666. pkt->ref_tag_mask[0] = 0xff;
  667. pkt->ref_tag_mask[1] = 0xff;
  668. pkt->ref_tag_mask[2] = 0xff;
  669. pkt->ref_tag_mask[3] = 0xff;
  670. break;
  671. }
  672. DEBUG18(printk(KERN_DEBUG
  673. "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
  674. " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
  675. " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
  676. (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
  677. }
  678. static int
  679. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  680. uint16_t tot_dsds)
  681. {
  682. void *next_dsd;
  683. uint8_t avail_dsds = 0;
  684. uint32_t dsd_list_len;
  685. struct dsd_dma *dsd_ptr;
  686. struct scatterlist *sg;
  687. uint32_t *cur_dsd = dsd;
  688. int i;
  689. uint16_t used_dsds = tot_dsds;
  690. uint8_t *cp;
  691. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  692. dma_addr_t sle_dma;
  693. /* Allocate additional continuation packets? */
  694. if (avail_dsds == 0) {
  695. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  696. QLA_DSDS_PER_IOCB : used_dsds;
  697. dsd_list_len = (avail_dsds + 1) * 12;
  698. used_dsds -= avail_dsds;
  699. /* allocate tracking DS */
  700. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  701. if (!dsd_ptr)
  702. return 1;
  703. /* allocate new list */
  704. dsd_ptr->dsd_addr = next_dsd =
  705. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  706. &dsd_ptr->dsd_list_dma);
  707. if (!next_dsd) {
  708. /*
  709. * Need to cleanup only this dsd_ptr, rest
  710. * will be done by sp_free_dma()
  711. */
  712. kfree(dsd_ptr);
  713. return 1;
  714. }
  715. list_add_tail(&dsd_ptr->list,
  716. &((struct crc_context *)sp->ctx)->dsd_list);
  717. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  718. /* add new list to cmd iocb or last list */
  719. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  720. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  721. *cur_dsd++ = dsd_list_len;
  722. cur_dsd = (uint32_t *)next_dsd;
  723. }
  724. sle_dma = sg_dma_address(sg);
  725. DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
  726. " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
  727. MSD(sle_dma), sg_dma_len(sg)));
  728. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  729. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  730. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  731. avail_dsds--;
  732. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  733. cp = page_address(sg_page(sg)) + sg->offset;
  734. DEBUG18(printk("%s(): User Data buffer= %p:\n",
  735. __func__ , cp));
  736. }
  737. }
  738. /* Null termination */
  739. *cur_dsd++ = 0;
  740. *cur_dsd++ = 0;
  741. *cur_dsd++ = 0;
  742. return 0;
  743. }
  744. static int
  745. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  746. uint32_t *dsd,
  747. uint16_t tot_dsds)
  748. {
  749. void *next_dsd;
  750. uint8_t avail_dsds = 0;
  751. uint32_t dsd_list_len;
  752. struct dsd_dma *dsd_ptr;
  753. struct scatterlist *sg;
  754. int i;
  755. struct scsi_cmnd *cmd;
  756. uint32_t *cur_dsd = dsd;
  757. uint16_t used_dsds = tot_dsds;
  758. uint8_t *cp;
  759. cmd = sp->cmd;
  760. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  761. dma_addr_t sle_dma;
  762. /* Allocate additional continuation packets? */
  763. if (avail_dsds == 0) {
  764. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  765. QLA_DSDS_PER_IOCB : used_dsds;
  766. dsd_list_len = (avail_dsds + 1) * 12;
  767. used_dsds -= avail_dsds;
  768. /* allocate tracking DS */
  769. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  770. if (!dsd_ptr)
  771. return 1;
  772. /* allocate new list */
  773. dsd_ptr->dsd_addr = next_dsd =
  774. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  775. &dsd_ptr->dsd_list_dma);
  776. if (!next_dsd) {
  777. /*
  778. * Need to cleanup only this dsd_ptr, rest
  779. * will be done by sp_free_dma()
  780. */
  781. kfree(dsd_ptr);
  782. return 1;
  783. }
  784. list_add_tail(&dsd_ptr->list,
  785. &((struct crc_context *)sp->ctx)->dsd_list);
  786. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  787. /* add new list to cmd iocb or last list */
  788. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  789. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  790. *cur_dsd++ = dsd_list_len;
  791. cur_dsd = (uint32_t *)next_dsd;
  792. }
  793. sle_dma = sg_dma_address(sg);
  794. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  795. DEBUG18(printk(KERN_DEBUG
  796. "%s(): %p, sg entry %d - addr =0x%x"
  797. "0x%x, len =%d\n", __func__ , cur_dsd, i,
  798. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
  799. }
  800. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  801. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  802. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  803. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  804. cp = page_address(sg_page(sg)) + sg->offset;
  805. DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
  806. __func__ , cp));
  807. }
  808. avail_dsds--;
  809. }
  810. /* Null termination */
  811. *cur_dsd++ = 0;
  812. *cur_dsd++ = 0;
  813. *cur_dsd++ = 0;
  814. return 0;
  815. }
  816. /**
  817. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  818. * Type 6 IOCB types.
  819. *
  820. * @sp: SRB command to process
  821. * @cmd_pkt: Command type 3 IOCB
  822. * @tot_dsds: Total number of segments to transfer
  823. */
  824. static inline int
  825. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  826. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  827. {
  828. uint32_t *cur_dsd, *fcp_dl;
  829. scsi_qla_host_t *vha;
  830. struct scsi_cmnd *cmd;
  831. struct scatterlist *cur_seg;
  832. int sgc;
  833. uint32_t total_bytes;
  834. uint32_t data_bytes;
  835. uint32_t dif_bytes;
  836. uint8_t bundling = 1;
  837. uint16_t blk_size;
  838. uint8_t *clr_ptr;
  839. struct crc_context *crc_ctx_pkt = NULL;
  840. struct qla_hw_data *ha;
  841. uint8_t additional_fcpcdb_len;
  842. uint16_t fcp_cmnd_len;
  843. struct fcp_cmnd *fcp_cmnd;
  844. dma_addr_t crc_ctx_dma;
  845. cmd = sp->cmd;
  846. sgc = 0;
  847. /* Update entry type to indicate Command Type CRC_2 IOCB */
  848. *((uint32_t *)(&cmd_pkt->entry_type)) =
  849. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  850. /* No data transfer */
  851. data_bytes = scsi_bufflen(cmd);
  852. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  853. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  854. __func__, data_bytes));
  855. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  856. return QLA_SUCCESS;
  857. }
  858. vha = sp->fcport->vha;
  859. ha = vha->hw;
  860. DEBUG18(printk(KERN_DEBUG
  861. "%s(%ld): Executing cmd sp %p, pid=%ld, prot_op=%u.\n", __func__,
  862. vha->host_no, sp, cmd->serial_number, scsi_get_prot_op(sp->cmd)));
  863. cmd_pkt->vp_index = sp->fcport->vp_idx;
  864. /* Set transfer direction */
  865. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  866. cmd_pkt->control_flags =
  867. __constant_cpu_to_le16(CF_WRITE_DATA);
  868. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  869. cmd_pkt->control_flags =
  870. __constant_cpu_to_le16(CF_READ_DATA);
  871. }
  872. tot_prot_dsds = scsi_prot_sg_count(cmd);
  873. if (!tot_prot_dsds)
  874. bundling = 0;
  875. /* Allocate CRC context from global pool */
  876. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  877. GFP_ATOMIC, &crc_ctx_dma);
  878. if (!crc_ctx_pkt)
  879. goto crc_queuing_error;
  880. /* Zero out CTX area. */
  881. clr_ptr = (uint8_t *)crc_ctx_pkt;
  882. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  883. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  884. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  885. /* Set handle */
  886. crc_ctx_pkt->handle = cmd_pkt->handle;
  887. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  888. qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
  889. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  890. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  891. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  892. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  893. /* Determine SCSI command length -- align to 4 byte boundary */
  894. if (cmd->cmd_len > 16) {
  895. DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
  896. __func__));
  897. additional_fcpcdb_len = cmd->cmd_len - 16;
  898. if ((cmd->cmd_len % 4) != 0) {
  899. /* SCSI cmd > 16 bytes must be multiple of 4 */
  900. goto crc_queuing_error;
  901. }
  902. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  903. } else {
  904. additional_fcpcdb_len = 0;
  905. fcp_cmnd_len = 12 + 16 + 4;
  906. }
  907. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  908. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  909. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  910. fcp_cmnd->additional_cdb_len |= 1;
  911. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  912. fcp_cmnd->additional_cdb_len |= 2;
  913. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  914. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  915. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  916. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  917. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  918. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  919. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  920. fcp_cmnd->task_attribute = 0;
  921. fcp_cmnd->task_management = 0;
  922. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  923. DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
  924. "entries %d, data bytes %d, Protection entries %d\n",
  925. __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
  926. data_bytes, tot_prot_dsds));
  927. /* Compute dif len and adjust data len to incude protection */
  928. total_bytes = data_bytes;
  929. dif_bytes = 0;
  930. blk_size = cmd->device->sector_size;
  931. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  932. dif_bytes = (data_bytes / blk_size) * 8;
  933. total_bytes += dif_bytes;
  934. }
  935. if (!ql2xenablehba_err_chk)
  936. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  937. if (!bundling) {
  938. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  939. } else {
  940. /*
  941. * Configure Bundling if we need to fetch interlaving
  942. * protection PCI accesses
  943. */
  944. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  945. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  946. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  947. tot_prot_dsds);
  948. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  949. }
  950. /* Finish the common fields of CRC pkt */
  951. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  952. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  953. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  954. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  955. /* Fibre channel byte count */
  956. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  957. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  958. additional_fcpcdb_len);
  959. *fcp_dl = htonl(total_bytes);
  960. DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
  961. " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
  962. vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
  963. crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
  964. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  965. DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
  966. __func__, data_bytes));
  967. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  968. return QLA_SUCCESS;
  969. }
  970. /* Walks data segments */
  971. cmd_pkt->control_flags |=
  972. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  973. if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  974. (tot_dsds - tot_prot_dsds)))
  975. goto crc_queuing_error;
  976. if (bundling && tot_prot_dsds) {
  977. /* Walks dif segments */
  978. cur_seg = scsi_prot_sglist(cmd);
  979. cmd_pkt->control_flags |=
  980. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  981. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  982. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  983. tot_prot_dsds))
  984. goto crc_queuing_error;
  985. }
  986. return QLA_SUCCESS;
  987. crc_queuing_error:
  988. DEBUG18(qla_printk(KERN_INFO, ha,
  989. "CMD sent FAILED crc_q error:sp = %p\n", sp));
  990. /* Cleanup will be performed by the caller */
  991. return QLA_FUNCTION_FAILED;
  992. }
  993. /**
  994. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  995. * @sp: command to send to the ISP
  996. *
  997. * Returns non-zero if a failure occurred, else zero.
  998. */
  999. int
  1000. qla24xx_start_scsi(srb_t *sp)
  1001. {
  1002. int ret, nseg;
  1003. unsigned long flags;
  1004. uint32_t *clr_ptr;
  1005. uint32_t index;
  1006. uint32_t handle;
  1007. struct cmd_type_7 *cmd_pkt;
  1008. uint16_t cnt;
  1009. uint16_t req_cnt;
  1010. uint16_t tot_dsds;
  1011. struct req_que *req = NULL;
  1012. struct rsp_que *rsp = NULL;
  1013. struct scsi_cmnd *cmd = sp->cmd;
  1014. struct scsi_qla_host *vha = sp->fcport->vha;
  1015. struct qla_hw_data *ha = vha->hw;
  1016. /* Setup device pointers. */
  1017. ret = 0;
  1018. qla25xx_set_que(sp, &rsp);
  1019. req = vha->req;
  1020. /* So we know we haven't pci_map'ed anything yet */
  1021. tot_dsds = 0;
  1022. /* Send marker if required */
  1023. if (vha->marker_needed != 0) {
  1024. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
  1025. != QLA_SUCCESS)
  1026. return QLA_FUNCTION_FAILED;
  1027. vha->marker_needed = 0;
  1028. }
  1029. /* Acquire ring specific lock */
  1030. spin_lock_irqsave(&ha->hardware_lock, flags);
  1031. /* Check for room in outstanding command list. */
  1032. handle = req->current_outstanding_cmd;
  1033. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1034. handle++;
  1035. if (handle == MAX_OUTSTANDING_COMMANDS)
  1036. handle = 1;
  1037. if (!req->outstanding_cmds[handle])
  1038. break;
  1039. }
  1040. if (index == MAX_OUTSTANDING_COMMANDS)
  1041. goto queuing_error;
  1042. /* Map the sg table so we have an accurate count of sg entries needed */
  1043. if (scsi_sg_count(cmd)) {
  1044. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1045. scsi_sg_count(cmd), cmd->sc_data_direction);
  1046. if (unlikely(!nseg))
  1047. goto queuing_error;
  1048. } else
  1049. nseg = 0;
  1050. tot_dsds = nseg;
  1051. req_cnt = qla24xx_calc_iocbs(tot_dsds);
  1052. if (req->cnt < (req_cnt + 2)) {
  1053. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1054. if (req->ring_index < cnt)
  1055. req->cnt = cnt - req->ring_index;
  1056. else
  1057. req->cnt = req->length -
  1058. (req->ring_index - cnt);
  1059. }
  1060. if (req->cnt < (req_cnt + 2))
  1061. goto queuing_error;
  1062. /* Build command packet. */
  1063. req->current_outstanding_cmd = handle;
  1064. req->outstanding_cmds[handle] = sp;
  1065. sp->handle = handle;
  1066. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1067. req->cnt -= req_cnt;
  1068. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1069. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1070. /* Zero out remaining portion of packet. */
  1071. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1072. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1073. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1074. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1075. /* Set NPORT-ID and LUN number*/
  1076. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1077. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1078. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1079. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1080. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1081. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1082. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1083. /* Load SCSI command packet. */
  1084. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1085. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1086. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1087. /* Build IOCB segments */
  1088. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1089. /* Set total data segment count. */
  1090. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1091. /* Specify response queue number where completion should happen */
  1092. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1093. wmb();
  1094. /* Adjust ring index. */
  1095. req->ring_index++;
  1096. if (req->ring_index == req->length) {
  1097. req->ring_index = 0;
  1098. req->ring_ptr = req->ring;
  1099. } else
  1100. req->ring_ptr++;
  1101. sp->flags |= SRB_DMA_VALID;
  1102. /* Set chip new ring index. */
  1103. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1104. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1105. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1106. if (vha->flags.process_response_queue &&
  1107. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1108. qla24xx_process_response_queue(vha, rsp);
  1109. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1110. return QLA_SUCCESS;
  1111. queuing_error:
  1112. if (tot_dsds)
  1113. scsi_dma_unmap(cmd);
  1114. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1115. return QLA_FUNCTION_FAILED;
  1116. }
  1117. /**
  1118. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1119. * @sp: command to send to the ISP
  1120. *
  1121. * Returns non-zero if a failure occurred, else zero.
  1122. */
  1123. int
  1124. qla24xx_dif_start_scsi(srb_t *sp)
  1125. {
  1126. int nseg;
  1127. unsigned long flags;
  1128. uint32_t *clr_ptr;
  1129. uint32_t index;
  1130. uint32_t handle;
  1131. uint16_t cnt;
  1132. uint16_t req_cnt = 0;
  1133. uint16_t tot_dsds;
  1134. uint16_t tot_prot_dsds;
  1135. uint16_t fw_prot_opts = 0;
  1136. struct req_que *req = NULL;
  1137. struct rsp_que *rsp = NULL;
  1138. struct scsi_cmnd *cmd = sp->cmd;
  1139. struct scsi_qla_host *vha = sp->fcport->vha;
  1140. struct qla_hw_data *ha = vha->hw;
  1141. struct cmd_type_crc_2 *cmd_pkt;
  1142. uint32_t status = 0;
  1143. #define QDSS_GOT_Q_SPACE BIT_0
  1144. /* Only process protection or >16 cdb in this routine */
  1145. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1146. if (cmd->cmd_len <= 16)
  1147. return qla24xx_start_scsi(sp);
  1148. }
  1149. /* Setup device pointers. */
  1150. qla25xx_set_que(sp, &rsp);
  1151. req = vha->req;
  1152. /* So we know we haven't pci_map'ed anything yet */
  1153. tot_dsds = 0;
  1154. /* Send marker if required */
  1155. if (vha->marker_needed != 0) {
  1156. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1157. QLA_SUCCESS)
  1158. return QLA_FUNCTION_FAILED;
  1159. vha->marker_needed = 0;
  1160. }
  1161. /* Acquire ring specific lock */
  1162. spin_lock_irqsave(&ha->hardware_lock, flags);
  1163. /* Check for room in outstanding command list. */
  1164. handle = req->current_outstanding_cmd;
  1165. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1166. handle++;
  1167. if (handle == MAX_OUTSTANDING_COMMANDS)
  1168. handle = 1;
  1169. if (!req->outstanding_cmds[handle])
  1170. break;
  1171. }
  1172. if (index == MAX_OUTSTANDING_COMMANDS)
  1173. goto queuing_error;
  1174. /* Compute number of required data segments */
  1175. /* Map the sg table so we have an accurate count of sg entries needed */
  1176. if (scsi_sg_count(cmd)) {
  1177. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1178. scsi_sg_count(cmd), cmd->sc_data_direction);
  1179. if (unlikely(!nseg))
  1180. goto queuing_error;
  1181. else
  1182. sp->flags |= SRB_DMA_VALID;
  1183. } else
  1184. nseg = 0;
  1185. /* number of required data segments */
  1186. tot_dsds = nseg;
  1187. /* Compute number of required protection segments */
  1188. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1189. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1190. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1191. if (unlikely(!nseg))
  1192. goto queuing_error;
  1193. else
  1194. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1195. } else {
  1196. nseg = 0;
  1197. }
  1198. req_cnt = 1;
  1199. /* Total Data and protection sg segment(s) */
  1200. tot_prot_dsds = nseg;
  1201. tot_dsds += nseg;
  1202. if (req->cnt < (req_cnt + 2)) {
  1203. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1204. if (req->ring_index < cnt)
  1205. req->cnt = cnt - req->ring_index;
  1206. else
  1207. req->cnt = req->length -
  1208. (req->ring_index - cnt);
  1209. }
  1210. if (req->cnt < (req_cnt + 2))
  1211. goto queuing_error;
  1212. status |= QDSS_GOT_Q_SPACE;
  1213. /* Build header part of command packet (excluding the OPCODE). */
  1214. req->current_outstanding_cmd = handle;
  1215. req->outstanding_cmds[handle] = sp;
  1216. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1217. req->cnt -= req_cnt;
  1218. /* Fill-in common area */
  1219. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1220. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1221. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1222. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1223. /* Set NPORT-ID and LUN number*/
  1224. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1225. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1226. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1227. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1228. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1229. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1230. /* Total Data and protection segment(s) */
  1231. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1232. /* Build IOCB segments and adjust for data protection segments */
  1233. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1234. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1235. QLA_SUCCESS)
  1236. goto queuing_error;
  1237. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1238. /* Specify response queue number where completion should happen */
  1239. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1240. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1241. wmb();
  1242. /* Adjust ring index. */
  1243. req->ring_index++;
  1244. if (req->ring_index == req->length) {
  1245. req->ring_index = 0;
  1246. req->ring_ptr = req->ring;
  1247. } else
  1248. req->ring_ptr++;
  1249. /* Set chip new ring index. */
  1250. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1251. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1252. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1253. if (vha->flags.process_response_queue &&
  1254. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1255. qla24xx_process_response_queue(vha, rsp);
  1256. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1257. return QLA_SUCCESS;
  1258. queuing_error:
  1259. if (status & QDSS_GOT_Q_SPACE) {
  1260. req->outstanding_cmds[handle] = NULL;
  1261. req->cnt += req_cnt;
  1262. }
  1263. /* Cleanup will be performed by the caller (queuecommand) */
  1264. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1265. DEBUG18(qla_printk(KERN_INFO, ha,
  1266. "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
  1267. return QLA_FUNCTION_FAILED;
  1268. }
  1269. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1270. {
  1271. struct scsi_cmnd *cmd = sp->cmd;
  1272. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1273. int affinity = cmd->request->cpu;
  1274. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1275. affinity < ha->max_rsp_queues - 1)
  1276. *rsp = ha->rsp_q_map[affinity + 1];
  1277. else
  1278. *rsp = ha->rsp_q_map[0];
  1279. }
  1280. /* Generic Control-SRB manipulation functions. */
  1281. void *
  1282. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1283. {
  1284. struct qla_hw_data *ha = vha->hw;
  1285. struct req_que *req = ha->req_q_map[0];
  1286. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1287. uint32_t index, handle;
  1288. request_t *pkt;
  1289. uint16_t cnt, req_cnt;
  1290. pkt = NULL;
  1291. req_cnt = 1;
  1292. handle = 0;
  1293. if (!sp)
  1294. goto skip_cmd_array;
  1295. /* Check for room in outstanding command list. */
  1296. handle = req->current_outstanding_cmd;
  1297. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1298. handle++;
  1299. if (handle == MAX_OUTSTANDING_COMMANDS)
  1300. handle = 1;
  1301. if (!req->outstanding_cmds[handle])
  1302. break;
  1303. }
  1304. if (index == MAX_OUTSTANDING_COMMANDS)
  1305. goto queuing_error;
  1306. /* Prep command array. */
  1307. req->current_outstanding_cmd = handle;
  1308. req->outstanding_cmds[handle] = sp;
  1309. sp->handle = handle;
  1310. skip_cmd_array:
  1311. /* Check for room on request queue. */
  1312. if (req->cnt < req_cnt) {
  1313. if (ha->mqenable)
  1314. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1315. else if (IS_QLA82XX(ha))
  1316. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1317. else if (IS_FWI2_CAPABLE(ha))
  1318. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1319. else
  1320. cnt = qla2x00_debounce_register(
  1321. ISP_REQ_Q_OUT(ha, &reg->isp));
  1322. if (req->ring_index < cnt)
  1323. req->cnt = cnt - req->ring_index;
  1324. else
  1325. req->cnt = req->length -
  1326. (req->ring_index - cnt);
  1327. }
  1328. if (req->cnt < req_cnt)
  1329. goto queuing_error;
  1330. /* Prep packet */
  1331. req->cnt -= req_cnt;
  1332. pkt = req->ring_ptr;
  1333. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1334. pkt->entry_count = req_cnt;
  1335. pkt->handle = handle;
  1336. queuing_error:
  1337. return pkt;
  1338. }
  1339. static void
  1340. qla2x00_start_iocbs(srb_t *sp)
  1341. {
  1342. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1343. struct req_que *req = ha->req_q_map[0];
  1344. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1345. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1346. if (IS_QLA82XX(ha)) {
  1347. qla82xx_start_iocbs(sp);
  1348. } else {
  1349. /* Adjust ring index. */
  1350. req->ring_index++;
  1351. if (req->ring_index == req->length) {
  1352. req->ring_index = 0;
  1353. req->ring_ptr = req->ring;
  1354. } else
  1355. req->ring_ptr++;
  1356. /* Set chip new ring index. */
  1357. if (ha->mqenable) {
  1358. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1359. RD_REG_DWORD(&ioreg->hccr);
  1360. } else if (IS_QLA82XX(ha)) {
  1361. qla82xx_start_iocbs(sp);
  1362. } else if (IS_FWI2_CAPABLE(ha)) {
  1363. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1364. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1365. } else {
  1366. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1367. req->ring_index);
  1368. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1369. }
  1370. }
  1371. }
  1372. static void
  1373. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1374. {
  1375. struct srb_ctx *ctx = sp->ctx;
  1376. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1377. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1378. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1379. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1380. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1381. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1382. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1383. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1384. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1385. logio->port_id[1] = sp->fcport->d_id.b.area;
  1386. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1387. logio->vp_index = sp->fcport->vp_idx;
  1388. }
  1389. static void
  1390. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1391. {
  1392. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1393. struct srb_ctx *ctx = sp->ctx;
  1394. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1395. uint16_t opts;
  1396. mbx->entry_type = MBX_IOCB_TYPE;
  1397. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1398. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1399. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1400. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1401. if (HAS_EXTENDED_IDS(ha)) {
  1402. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1403. mbx->mb10 = cpu_to_le16(opts);
  1404. } else {
  1405. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1406. }
  1407. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1408. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1409. sp->fcport->d_id.b.al_pa);
  1410. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1411. }
  1412. static void
  1413. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1414. {
  1415. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1416. logio->control_flags =
  1417. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1418. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1419. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1420. logio->port_id[1] = sp->fcport->d_id.b.area;
  1421. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1422. logio->vp_index = sp->fcport->vp_idx;
  1423. }
  1424. static void
  1425. qla2x00_logout_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_LOGOUT_FABRIC_PORT);
  1431. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1432. cpu_to_le16(sp->fcport->loop_id):
  1433. cpu_to_le16(sp->fcport->loop_id << 8);
  1434. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1435. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1436. sp->fcport->d_id.b.al_pa);
  1437. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1438. /* Implicit: mbx->mbx10 = 0. */
  1439. }
  1440. static void
  1441. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1442. {
  1443. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1444. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1445. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1446. logio->vp_index = sp->fcport->vp_idx;
  1447. }
  1448. static void
  1449. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1450. {
  1451. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1452. mbx->entry_type = MBX_IOCB_TYPE;
  1453. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1454. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1455. if (HAS_EXTENDED_IDS(ha)) {
  1456. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1457. mbx->mb10 = cpu_to_le16(BIT_0);
  1458. } else {
  1459. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1460. }
  1461. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1462. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1463. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1464. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1465. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1466. }
  1467. static void
  1468. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1469. {
  1470. uint32_t flags;
  1471. unsigned int lun;
  1472. struct fc_port *fcport = sp->fcport;
  1473. scsi_qla_host_t *vha = fcport->vha;
  1474. struct qla_hw_data *ha = vha->hw;
  1475. struct srb_ctx *ctx = sp->ctx;
  1476. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1477. struct req_que *req = vha->req;
  1478. flags = iocb->u.tmf.flags;
  1479. lun = iocb->u.tmf.lun;
  1480. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1481. tsk->entry_count = 1;
  1482. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1483. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1484. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1485. tsk->control_flags = cpu_to_le32(flags);
  1486. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1487. tsk->port_id[1] = fcport->d_id.b.area;
  1488. tsk->port_id[2] = fcport->d_id.b.domain;
  1489. tsk->vp_index = fcport->vp_idx;
  1490. if (flags == TCF_LUN_RESET) {
  1491. int_to_scsilun(lun, &tsk->lun);
  1492. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1493. sizeof(tsk->lun));
  1494. }
  1495. }
  1496. static void
  1497. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1498. {
  1499. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1500. els_iocb->entry_type = ELS_IOCB_TYPE;
  1501. els_iocb->entry_count = 1;
  1502. els_iocb->sys_define = 0;
  1503. els_iocb->entry_status = 0;
  1504. els_iocb->handle = sp->handle;
  1505. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1506. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1507. els_iocb->vp_index = sp->fcport->vp_idx;
  1508. els_iocb->sof_type = EST_SOFI3;
  1509. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1510. els_iocb->opcode =
  1511. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1512. bsg_job->request->rqst_data.r_els.els_code :
  1513. bsg_job->request->rqst_data.h_els.command_code;
  1514. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1515. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1516. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1517. els_iocb->control_flags = 0;
  1518. els_iocb->rx_byte_count =
  1519. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1520. els_iocb->tx_byte_count =
  1521. cpu_to_le32(bsg_job->request_payload.payload_len);
  1522. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1523. (bsg_job->request_payload.sg_list)));
  1524. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1525. (bsg_job->request_payload.sg_list)));
  1526. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1527. (bsg_job->request_payload.sg_list));
  1528. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1529. (bsg_job->reply_payload.sg_list)));
  1530. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1531. (bsg_job->reply_payload.sg_list)));
  1532. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1533. (bsg_job->reply_payload.sg_list));
  1534. }
  1535. static void
  1536. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1537. {
  1538. uint16_t avail_dsds;
  1539. uint32_t *cur_dsd;
  1540. struct scatterlist *sg;
  1541. int index;
  1542. uint16_t tot_dsds;
  1543. scsi_qla_host_t *vha = sp->fcport->vha;
  1544. struct qla_hw_data *ha = vha->hw;
  1545. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1546. int loop_iterartion = 0;
  1547. int cont_iocb_prsnt = 0;
  1548. int entry_count = 1;
  1549. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1550. ct_iocb->entry_type = CT_IOCB_TYPE;
  1551. ct_iocb->entry_status = 0;
  1552. ct_iocb->handle1 = sp->handle;
  1553. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1554. ct_iocb->status = __constant_cpu_to_le16(0);
  1555. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1556. ct_iocb->timeout = 0;
  1557. ct_iocb->cmd_dsd_count =
  1558. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1559. ct_iocb->total_dsd_count =
  1560. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1561. ct_iocb->req_bytecount =
  1562. cpu_to_le32(bsg_job->request_payload.payload_len);
  1563. ct_iocb->rsp_bytecount =
  1564. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1565. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1566. (bsg_job->request_payload.sg_list)));
  1567. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1568. (bsg_job->request_payload.sg_list)));
  1569. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1570. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1571. (bsg_job->reply_payload.sg_list)));
  1572. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1573. (bsg_job->reply_payload.sg_list)));
  1574. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1575. avail_dsds = 1;
  1576. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1577. index = 0;
  1578. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1579. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1580. dma_addr_t sle_dma;
  1581. cont_a64_entry_t *cont_pkt;
  1582. /* Allocate additional continuation packets? */
  1583. if (avail_dsds == 0) {
  1584. /*
  1585. * Five DSDs are available in the Cont.
  1586. * Type 1 IOCB.
  1587. */
  1588. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1589. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1590. avail_dsds = 5;
  1591. cont_iocb_prsnt = 1;
  1592. entry_count++;
  1593. }
  1594. sle_dma = sg_dma_address(sg);
  1595. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1596. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1597. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1598. loop_iterartion++;
  1599. avail_dsds--;
  1600. }
  1601. ct_iocb->entry_count = entry_count;
  1602. }
  1603. static void
  1604. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1605. {
  1606. uint16_t avail_dsds;
  1607. uint32_t *cur_dsd;
  1608. struct scatterlist *sg;
  1609. int index;
  1610. uint16_t tot_dsds;
  1611. scsi_qla_host_t *vha = sp->fcport->vha;
  1612. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1613. int loop_iterartion = 0;
  1614. int cont_iocb_prsnt = 0;
  1615. int entry_count = 1;
  1616. ct_iocb->entry_type = CT_IOCB_TYPE;
  1617. ct_iocb->entry_status = 0;
  1618. ct_iocb->sys_define = 0;
  1619. ct_iocb->handle = sp->handle;
  1620. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1621. ct_iocb->vp_index = sp->fcport->vp_idx;
  1622. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1623. ct_iocb->cmd_dsd_count =
  1624. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1625. ct_iocb->timeout = 0;
  1626. ct_iocb->rsp_dsd_count =
  1627. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1628. ct_iocb->rsp_byte_count =
  1629. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1630. ct_iocb->cmd_byte_count =
  1631. cpu_to_le32(bsg_job->request_payload.payload_len);
  1632. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1633. (bsg_job->request_payload.sg_list)));
  1634. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1635. (bsg_job->request_payload.sg_list)));
  1636. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1637. (bsg_job->request_payload.sg_list));
  1638. avail_dsds = 1;
  1639. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1640. index = 0;
  1641. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1642. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1643. dma_addr_t sle_dma;
  1644. cont_a64_entry_t *cont_pkt;
  1645. /* Allocate additional continuation packets? */
  1646. if (avail_dsds == 0) {
  1647. /*
  1648. * Five DSDs are available in the Cont.
  1649. * Type 1 IOCB.
  1650. */
  1651. cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
  1652. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1653. avail_dsds = 5;
  1654. cont_iocb_prsnt = 1;
  1655. entry_count++;
  1656. }
  1657. sle_dma = sg_dma_address(sg);
  1658. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1659. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1660. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1661. loop_iterartion++;
  1662. avail_dsds--;
  1663. }
  1664. ct_iocb->entry_count = entry_count;
  1665. }
  1666. int
  1667. qla2x00_start_sp(srb_t *sp)
  1668. {
  1669. int rval;
  1670. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1671. void *pkt;
  1672. struct srb_ctx *ctx = sp->ctx;
  1673. unsigned long flags;
  1674. rval = QLA_FUNCTION_FAILED;
  1675. spin_lock_irqsave(&ha->hardware_lock, flags);
  1676. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1677. if (!pkt)
  1678. goto done;
  1679. rval = QLA_SUCCESS;
  1680. switch (ctx->type) {
  1681. case SRB_LOGIN_CMD:
  1682. IS_FWI2_CAPABLE(ha) ?
  1683. qla24xx_login_iocb(sp, pkt) :
  1684. qla2x00_login_iocb(sp, pkt);
  1685. break;
  1686. case SRB_LOGOUT_CMD:
  1687. IS_FWI2_CAPABLE(ha) ?
  1688. qla24xx_logout_iocb(sp, pkt) :
  1689. qla2x00_logout_iocb(sp, pkt);
  1690. break;
  1691. case SRB_ELS_CMD_RPT:
  1692. case SRB_ELS_CMD_HST:
  1693. qla24xx_els_iocb(sp, pkt);
  1694. break;
  1695. case SRB_CT_CMD:
  1696. IS_FWI2_CAPABLE(ha) ?
  1697. qla24xx_ct_iocb(sp, pkt) :
  1698. qla2x00_ct_iocb(sp, pkt);
  1699. break;
  1700. case SRB_ADISC_CMD:
  1701. IS_FWI2_CAPABLE(ha) ?
  1702. qla24xx_adisc_iocb(sp, pkt) :
  1703. qla2x00_adisc_iocb(sp, pkt);
  1704. break;
  1705. case SRB_TM_CMD:
  1706. qla24xx_tm_iocb(sp, pkt);
  1707. break;
  1708. default:
  1709. break;
  1710. }
  1711. wmb();
  1712. qla2x00_start_iocbs(sp);
  1713. done:
  1714. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1715. return rval;
  1716. }