qla_iocb.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2011 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, struct req_que *req)
  109. {
  110. cont_a64_entry_t *cont_pkt;
  111. /* Adjust ring index. */
  112. req->ring_index++;
  113. if (req->ring_index == req->length) {
  114. req->ring_index = 0;
  115. req->ring_ptr = req->ring;
  116. } else {
  117. req->ring_ptr++;
  118. }
  119. cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
  120. /* Load packet defaults. */
  121. *((uint32_t *)(&cont_pkt->entry_type)) =
  122. __constant_cpu_to_le32(CONTINUE_A64_TYPE);
  123. return (cont_pkt);
  124. }
  125. static inline int
  126. qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
  127. {
  128. uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
  129. /* We only support T10 DIF right now */
  130. if (guard != SHOST_DIX_GUARD_CRC) {
  131. ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
  132. "Unsupported guard: %d for cmd=%p.\n", guard, sp->cmd);
  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, vha->req);
  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. char tag[2];
  289. /* Setup device pointers. */
  290. ret = 0;
  291. vha = sp->fcport->vha;
  292. ha = vha->hw;
  293. reg = &ha->iobase->isp;
  294. cmd = sp->cmd;
  295. req = ha->req_q_map[0];
  296. rsp = ha->rsp_q_map[0];
  297. /* So we know we haven't pci_map'ed anything yet */
  298. tot_dsds = 0;
  299. /* Send marker if required */
  300. if (vha->marker_needed != 0) {
  301. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  302. QLA_SUCCESS) {
  303. return (QLA_FUNCTION_FAILED);
  304. }
  305. vha->marker_needed = 0;
  306. }
  307. /* Acquire ring specific lock */
  308. spin_lock_irqsave(&ha->hardware_lock, flags);
  309. /* Check for room in outstanding command list. */
  310. handle = req->current_outstanding_cmd;
  311. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  312. handle++;
  313. if (handle == MAX_OUTSTANDING_COMMANDS)
  314. handle = 1;
  315. if (!req->outstanding_cmds[handle])
  316. break;
  317. }
  318. if (index == MAX_OUTSTANDING_COMMANDS)
  319. goto queuing_error;
  320. /* Map the sg table so we have an accurate count of sg entries needed */
  321. if (scsi_sg_count(cmd)) {
  322. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  323. scsi_sg_count(cmd), cmd->sc_data_direction);
  324. if (unlikely(!nseg))
  325. goto queuing_error;
  326. } else
  327. nseg = 0;
  328. tot_dsds = nseg;
  329. /* Calculate the number of request entries needed. */
  330. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  331. if (req->cnt < (req_cnt + 2)) {
  332. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  333. if (req->ring_index < cnt)
  334. req->cnt = cnt - req->ring_index;
  335. else
  336. req->cnt = req->length -
  337. (req->ring_index - cnt);
  338. }
  339. if (req->cnt < (req_cnt + 2))
  340. goto queuing_error;
  341. /* Build command packet */
  342. req->current_outstanding_cmd = handle;
  343. req->outstanding_cmds[handle] = sp;
  344. sp->handle = handle;
  345. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  346. req->cnt -= req_cnt;
  347. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  348. cmd_pkt->handle = handle;
  349. /* Zero out remaining portion of packet. */
  350. clr_ptr = (uint32_t *)cmd_pkt + 2;
  351. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  352. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  353. /* Set target ID and LUN number*/
  354. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  355. cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
  356. /* Update tagged queuing modifier */
  357. if (scsi_populate_tag_msg(cmd, tag)) {
  358. switch (tag[0]) {
  359. case HEAD_OF_QUEUE_TAG:
  360. cmd_pkt->control_flags =
  361. __constant_cpu_to_le16(CF_HEAD_TAG);
  362. break;
  363. case ORDERED_QUEUE_TAG:
  364. cmd_pkt->control_flags =
  365. __constant_cpu_to_le16(CF_ORDERED_TAG);
  366. break;
  367. default:
  368. cmd_pkt->control_flags =
  369. __constant_cpu_to_le16(CF_SIMPLE_TAG);
  370. break;
  371. }
  372. }
  373. /* Load SCSI command packet. */
  374. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  375. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  376. /* Build IOCB segments */
  377. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  378. /* Set total data segment count. */
  379. cmd_pkt->entry_count = (uint8_t)req_cnt;
  380. wmb();
  381. /* Adjust ring index. */
  382. req->ring_index++;
  383. if (req->ring_index == req->length) {
  384. req->ring_index = 0;
  385. req->ring_ptr = req->ring;
  386. } else
  387. req->ring_ptr++;
  388. sp->flags |= SRB_DMA_VALID;
  389. /* Set chip new ring index. */
  390. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  391. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  392. /* Manage unprocessed RIO/ZIO commands in response queue. */
  393. if (vha->flags.process_response_queue &&
  394. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  395. qla2x00_process_response_queue(rsp);
  396. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  397. return (QLA_SUCCESS);
  398. queuing_error:
  399. if (tot_dsds)
  400. scsi_dma_unmap(cmd);
  401. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  402. return (QLA_FUNCTION_FAILED);
  403. }
  404. /**
  405. * qla2x00_marker() - Send a marker IOCB to the firmware.
  406. * @ha: HA context
  407. * @loop_id: loop ID
  408. * @lun: LUN
  409. * @type: marker modifier
  410. *
  411. * Can be called from both normal and interrupt context.
  412. *
  413. * Returns non-zero if a failure occurred, else zero.
  414. */
  415. static int
  416. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  417. struct rsp_que *rsp, uint16_t loop_id,
  418. uint16_t lun, uint8_t type)
  419. {
  420. mrk_entry_t *mrk;
  421. struct mrk_entry_24xx *mrk24;
  422. struct qla_hw_data *ha = vha->hw;
  423. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  424. mrk24 = NULL;
  425. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
  426. if (mrk == NULL) {
  427. ql_log(ql_log_warn, base_vha, 0x3026,
  428. "Failed to allocate Marker IOCB.\n");
  429. return (QLA_FUNCTION_FAILED);
  430. }
  431. mrk->entry_type = MARKER_TYPE;
  432. mrk->modifier = type;
  433. if (type != MK_SYNC_ALL) {
  434. if (IS_FWI2_CAPABLE(ha)) {
  435. mrk24 = (struct mrk_entry_24xx *) mrk;
  436. mrk24->nport_handle = cpu_to_le16(loop_id);
  437. mrk24->lun[1] = LSB(lun);
  438. mrk24->lun[2] = MSB(lun);
  439. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  440. mrk24->vp_index = vha->vp_idx;
  441. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  442. } else {
  443. SET_TARGET_ID(ha, mrk->target, loop_id);
  444. mrk->lun = cpu_to_le16(lun);
  445. }
  446. }
  447. wmb();
  448. qla2x00_isp_cmd(vha, req);
  449. return (QLA_SUCCESS);
  450. }
  451. int
  452. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  453. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  454. uint8_t type)
  455. {
  456. int ret;
  457. unsigned long flags = 0;
  458. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  459. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  460. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  461. return (ret);
  462. }
  463. /**
  464. * qla2x00_isp_cmd() - Modify the request ring pointer.
  465. * @ha: HA context
  466. *
  467. * Note: The caller must hold the hardware lock before calling this routine.
  468. */
  469. static void
  470. qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
  471. {
  472. struct qla_hw_data *ha = vha->hw;
  473. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  474. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  475. ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x302d,
  476. "IOCB data:\n");
  477. ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302e,
  478. (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE);
  479. /* Adjust ring index. */
  480. req->ring_index++;
  481. if (req->ring_index == req->length) {
  482. req->ring_index = 0;
  483. req->ring_ptr = req->ring;
  484. } else
  485. req->ring_ptr++;
  486. /* Set chip new ring index. */
  487. if (IS_QLA82XX(ha)) {
  488. uint32_t dbval = 0x04 | (ha->portnum << 5);
  489. /* write, read and verify logic */
  490. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  491. if (ql2xdbwr)
  492. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  493. else {
  494. WRT_REG_DWORD(
  495. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  496. dbval);
  497. wmb();
  498. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  499. WRT_REG_DWORD((unsigned long __iomem *)
  500. ha->nxdb_wr_ptr, dbval);
  501. wmb();
  502. }
  503. }
  504. } else if (ha->mqenable) {
  505. /* Set chip new ring index. */
  506. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  507. RD_REG_DWORD(&ioreg->hccr);
  508. } else {
  509. if (IS_FWI2_CAPABLE(ha)) {
  510. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  511. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  512. } else {
  513. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  514. req->ring_index);
  515. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  516. }
  517. }
  518. }
  519. /**
  520. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  521. * Continuation Type 1 IOCBs to allocate.
  522. *
  523. * @dsds: number of data segment decriptors needed
  524. *
  525. * Returns the number of IOCB entries needed to store @dsds.
  526. */
  527. inline uint16_t
  528. qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
  529. {
  530. uint16_t iocbs;
  531. iocbs = 1;
  532. if (dsds > 1) {
  533. iocbs += (dsds - 1) / 5;
  534. if ((dsds - 1) % 5)
  535. iocbs++;
  536. }
  537. return iocbs;
  538. }
  539. /**
  540. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  541. * IOCB types.
  542. *
  543. * @sp: SRB command to process
  544. * @cmd_pkt: Command type 3 IOCB
  545. * @tot_dsds: Total number of segments to transfer
  546. */
  547. inline void
  548. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  549. uint16_t tot_dsds)
  550. {
  551. uint16_t avail_dsds;
  552. uint32_t *cur_dsd;
  553. scsi_qla_host_t *vha;
  554. struct scsi_cmnd *cmd;
  555. struct scatterlist *sg;
  556. int i;
  557. struct req_que *req;
  558. cmd = sp->cmd;
  559. /* Update entry type to indicate Command Type 3 IOCB */
  560. *((uint32_t *)(&cmd_pkt->entry_type)) =
  561. __constant_cpu_to_le32(COMMAND_TYPE_7);
  562. /* No data transfer */
  563. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  564. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  565. return;
  566. }
  567. vha = sp->fcport->vha;
  568. req = vha->req;
  569. /* Set transfer direction */
  570. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  571. cmd_pkt->task_mgmt_flags =
  572. __constant_cpu_to_le16(TMF_WRITE_DATA);
  573. sp->fcport->vha->hw->qla_stats.output_bytes +=
  574. scsi_bufflen(sp->cmd);
  575. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  576. cmd_pkt->task_mgmt_flags =
  577. __constant_cpu_to_le16(TMF_READ_DATA);
  578. sp->fcport->vha->hw->qla_stats.input_bytes +=
  579. scsi_bufflen(sp->cmd);
  580. }
  581. /* One DSD is available in the Command Type 3 IOCB */
  582. avail_dsds = 1;
  583. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  584. /* Load data segments */
  585. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  586. dma_addr_t sle_dma;
  587. cont_a64_entry_t *cont_pkt;
  588. /* Allocate additional continuation packets? */
  589. if (avail_dsds == 0) {
  590. /*
  591. * Five DSDs are available in the Continuation
  592. * Type 1 IOCB.
  593. */
  594. cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
  595. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  596. avail_dsds = 5;
  597. }
  598. sle_dma = sg_dma_address(sg);
  599. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  600. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  601. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  602. avail_dsds--;
  603. }
  604. }
  605. struct fw_dif_context {
  606. uint32_t ref_tag;
  607. uint16_t app_tag;
  608. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  609. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  610. };
  611. /*
  612. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  613. *
  614. */
  615. static inline void
  616. qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
  617. unsigned int protcnt)
  618. {
  619. struct scsi_cmnd *cmd = sp->cmd;
  620. scsi_qla_host_t *vha = shost_priv(cmd->device->host);
  621. switch (scsi_get_prot_type(cmd)) {
  622. case SCSI_PROT_DIF_TYPE0:
  623. /*
  624. * No check for ql2xenablehba_err_chk, as it would be an
  625. * I/O error if hba tag generation is not done.
  626. */
  627. pkt->ref_tag = cpu_to_le32((uint32_t)
  628. (0xffffffff & scsi_get_lba(cmd)));
  629. if (!qla2x00_hba_err_chk_enabled(sp))
  630. break;
  631. pkt->ref_tag_mask[0] = 0xff;
  632. pkt->ref_tag_mask[1] = 0xff;
  633. pkt->ref_tag_mask[2] = 0xff;
  634. pkt->ref_tag_mask[3] = 0xff;
  635. break;
  636. /*
  637. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  638. * match LBA in CDB + N
  639. */
  640. case SCSI_PROT_DIF_TYPE2:
  641. pkt->app_tag = __constant_cpu_to_le16(0);
  642. pkt->app_tag_mask[0] = 0x0;
  643. pkt->app_tag_mask[1] = 0x0;
  644. pkt->ref_tag = cpu_to_le32((uint32_t)
  645. (0xffffffff & scsi_get_lba(cmd)));
  646. if (!qla2x00_hba_err_chk_enabled(sp))
  647. break;
  648. /* enable ALL bytes of the ref tag */
  649. pkt->ref_tag_mask[0] = 0xff;
  650. pkt->ref_tag_mask[1] = 0xff;
  651. pkt->ref_tag_mask[2] = 0xff;
  652. pkt->ref_tag_mask[3] = 0xff;
  653. break;
  654. /* For Type 3 protection: 16 bit GUARD only */
  655. case SCSI_PROT_DIF_TYPE3:
  656. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  657. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  658. 0x00;
  659. break;
  660. /*
  661. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  662. * 16 bit app tag.
  663. */
  664. case SCSI_PROT_DIF_TYPE1:
  665. pkt->ref_tag = cpu_to_le32((uint32_t)
  666. (0xffffffff & scsi_get_lba(cmd)));
  667. pkt->app_tag = __constant_cpu_to_le16(0);
  668. pkt->app_tag_mask[0] = 0x0;
  669. pkt->app_tag_mask[1] = 0x0;
  670. if (!qla2x00_hba_err_chk_enabled(sp))
  671. break;
  672. /* enable ALL bytes of the ref tag */
  673. pkt->ref_tag_mask[0] = 0xff;
  674. pkt->ref_tag_mask[1] = 0xff;
  675. pkt->ref_tag_mask[2] = 0xff;
  676. pkt->ref_tag_mask[3] = 0xff;
  677. break;
  678. }
  679. ql_dbg(ql_dbg_io, vha, 0x3009,
  680. "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
  681. "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
  682. pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
  683. scsi_get_prot_type(cmd), cmd);
  684. }
  685. struct qla2_sgx {
  686. dma_addr_t dma_addr; /* OUT */
  687. uint32_t dma_len; /* OUT */
  688. uint32_t tot_bytes; /* IN */
  689. struct scatterlist *cur_sg; /* IN */
  690. /* for book keeping, bzero on initial invocation */
  691. uint32_t bytes_consumed;
  692. uint32_t num_bytes;
  693. uint32_t tot_partial;
  694. /* for debugging */
  695. uint32_t num_sg;
  696. srb_t *sp;
  697. };
  698. static int
  699. qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
  700. uint32_t *partial)
  701. {
  702. struct scatterlist *sg;
  703. uint32_t cumulative_partial, sg_len;
  704. dma_addr_t sg_dma_addr;
  705. if (sgx->num_bytes == sgx->tot_bytes)
  706. return 0;
  707. sg = sgx->cur_sg;
  708. cumulative_partial = sgx->tot_partial;
  709. sg_dma_addr = sg_dma_address(sg);
  710. sg_len = sg_dma_len(sg);
  711. sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
  712. if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
  713. sgx->dma_len = (blk_sz - cumulative_partial);
  714. sgx->tot_partial = 0;
  715. sgx->num_bytes += blk_sz;
  716. *partial = 0;
  717. } else {
  718. sgx->dma_len = sg_len - sgx->bytes_consumed;
  719. sgx->tot_partial += sgx->dma_len;
  720. *partial = 1;
  721. }
  722. sgx->bytes_consumed += sgx->dma_len;
  723. if (sg_len == sgx->bytes_consumed) {
  724. sg = sg_next(sg);
  725. sgx->num_sg++;
  726. sgx->cur_sg = sg;
  727. sgx->bytes_consumed = 0;
  728. }
  729. return 1;
  730. }
  731. static int
  732. qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
  733. uint32_t *dsd, uint16_t tot_dsds)
  734. {
  735. void *next_dsd;
  736. uint8_t avail_dsds = 0;
  737. uint32_t dsd_list_len;
  738. struct dsd_dma *dsd_ptr;
  739. struct scatterlist *sg_prot;
  740. uint32_t *cur_dsd = dsd;
  741. uint16_t used_dsds = tot_dsds;
  742. uint32_t prot_int;
  743. uint32_t partial;
  744. struct qla2_sgx sgx;
  745. dma_addr_t sle_dma;
  746. uint32_t sle_dma_len, tot_prot_dma_len = 0;
  747. struct scsi_cmnd *cmd = sp->cmd;
  748. prot_int = cmd->device->sector_size;
  749. memset(&sgx, 0, sizeof(struct qla2_sgx));
  750. sgx.tot_bytes = scsi_bufflen(sp->cmd);
  751. sgx.cur_sg = scsi_sglist(sp->cmd);
  752. sgx.sp = sp;
  753. sg_prot = scsi_prot_sglist(sp->cmd);
  754. while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
  755. sle_dma = sgx.dma_addr;
  756. sle_dma_len = sgx.dma_len;
  757. alloc_and_fill:
  758. /* Allocate additional continuation packets? */
  759. if (avail_dsds == 0) {
  760. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  761. QLA_DSDS_PER_IOCB : used_dsds;
  762. dsd_list_len = (avail_dsds + 1) * 12;
  763. used_dsds -= avail_dsds;
  764. /* allocate tracking DS */
  765. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  766. if (!dsd_ptr)
  767. return 1;
  768. /* allocate new list */
  769. dsd_ptr->dsd_addr = next_dsd =
  770. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  771. &dsd_ptr->dsd_list_dma);
  772. if (!next_dsd) {
  773. /*
  774. * Need to cleanup only this dsd_ptr, rest
  775. * will be done by sp_free_dma()
  776. */
  777. kfree(dsd_ptr);
  778. return 1;
  779. }
  780. list_add_tail(&dsd_ptr->list,
  781. &((struct crc_context *)sp->ctx)->dsd_list);
  782. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  783. /* add new list to cmd iocb or last list */
  784. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  785. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  786. *cur_dsd++ = dsd_list_len;
  787. cur_dsd = (uint32_t *)next_dsd;
  788. }
  789. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  790. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  791. *cur_dsd++ = cpu_to_le32(sle_dma_len);
  792. avail_dsds--;
  793. if (partial == 0) {
  794. /* Got a full protection interval */
  795. sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
  796. sle_dma_len = 8;
  797. tot_prot_dma_len += sle_dma_len;
  798. if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
  799. tot_prot_dma_len = 0;
  800. sg_prot = sg_next(sg_prot);
  801. }
  802. partial = 1; /* So as to not re-enter this block */
  803. goto alloc_and_fill;
  804. }
  805. }
  806. /* Null termination */
  807. *cur_dsd++ = 0;
  808. *cur_dsd++ = 0;
  809. *cur_dsd++ = 0;
  810. return 0;
  811. }
  812. static int
  813. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  814. uint16_t tot_dsds)
  815. {
  816. void *next_dsd;
  817. uint8_t avail_dsds = 0;
  818. uint32_t dsd_list_len;
  819. struct dsd_dma *dsd_ptr;
  820. struct scatterlist *sg;
  821. uint32_t *cur_dsd = dsd;
  822. int i;
  823. uint16_t used_dsds = tot_dsds;
  824. scsi_qla_host_t *vha = shost_priv(sp->cmd->device->host);
  825. uint8_t *cp;
  826. scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
  827. dma_addr_t sle_dma;
  828. /* Allocate additional continuation packets? */
  829. if (avail_dsds == 0) {
  830. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  831. QLA_DSDS_PER_IOCB : used_dsds;
  832. dsd_list_len = (avail_dsds + 1) * 12;
  833. used_dsds -= avail_dsds;
  834. /* allocate tracking DS */
  835. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  836. if (!dsd_ptr)
  837. return 1;
  838. /* allocate new list */
  839. dsd_ptr->dsd_addr = next_dsd =
  840. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  841. &dsd_ptr->dsd_list_dma);
  842. if (!next_dsd) {
  843. /*
  844. * Need to cleanup only this dsd_ptr, rest
  845. * will be done by sp_free_dma()
  846. */
  847. kfree(dsd_ptr);
  848. return 1;
  849. }
  850. list_add_tail(&dsd_ptr->list,
  851. &((struct crc_context *)sp->ctx)->dsd_list);
  852. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  853. /* add new list to cmd iocb or last list */
  854. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  855. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  856. *cur_dsd++ = dsd_list_len;
  857. cur_dsd = (uint32_t *)next_dsd;
  858. }
  859. sle_dma = sg_dma_address(sg);
  860. ql_dbg(ql_dbg_io, vha, 0x300a,
  861. "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
  862. i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg),
  863. sp->cmd);
  864. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  865. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  866. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  867. avail_dsds--;
  868. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  869. cp = page_address(sg_page(sg)) + sg->offset;
  870. ql_dbg(ql_dbg_io, vha, 0x300b,
  871. "User data buffer=%p for cmd=%p.\n", cp, sp->cmd);
  872. }
  873. }
  874. /* Null termination */
  875. *cur_dsd++ = 0;
  876. *cur_dsd++ = 0;
  877. *cur_dsd++ = 0;
  878. return 0;
  879. }
  880. static int
  881. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  882. uint32_t *dsd,
  883. uint16_t tot_dsds)
  884. {
  885. void *next_dsd;
  886. uint8_t avail_dsds = 0;
  887. uint32_t dsd_list_len;
  888. struct dsd_dma *dsd_ptr;
  889. struct scatterlist *sg;
  890. int i;
  891. struct scsi_cmnd *cmd;
  892. uint32_t *cur_dsd = dsd;
  893. uint16_t used_dsds = tot_dsds;
  894. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  895. uint8_t *cp;
  896. cmd = sp->cmd;
  897. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  898. dma_addr_t sle_dma;
  899. /* Allocate additional continuation packets? */
  900. if (avail_dsds == 0) {
  901. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  902. QLA_DSDS_PER_IOCB : used_dsds;
  903. dsd_list_len = (avail_dsds + 1) * 12;
  904. used_dsds -= avail_dsds;
  905. /* allocate tracking DS */
  906. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  907. if (!dsd_ptr)
  908. return 1;
  909. /* allocate new list */
  910. dsd_ptr->dsd_addr = next_dsd =
  911. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  912. &dsd_ptr->dsd_list_dma);
  913. if (!next_dsd) {
  914. /*
  915. * Need to cleanup only this dsd_ptr, rest
  916. * will be done by sp_free_dma()
  917. */
  918. kfree(dsd_ptr);
  919. return 1;
  920. }
  921. list_add_tail(&dsd_ptr->list,
  922. &((struct crc_context *)sp->ctx)->dsd_list);
  923. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  924. /* add new list to cmd iocb or last list */
  925. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  926. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  927. *cur_dsd++ = dsd_list_len;
  928. cur_dsd = (uint32_t *)next_dsd;
  929. }
  930. sle_dma = sg_dma_address(sg);
  931. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  932. ql_dbg(ql_dbg_io, vha, 0x3027,
  933. "%s(): %p, sg_entry %d - "
  934. "addr=0x%x0x%x, len=%d.\n",
  935. __func__, cur_dsd, i,
  936. LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
  937. }
  938. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  939. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  940. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  941. if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
  942. cp = page_address(sg_page(sg)) + sg->offset;
  943. ql_dbg(ql_dbg_io, vha, 0x3028,
  944. "%s(): Protection Data buffer = %p.\n", __func__,
  945. cp);
  946. }
  947. avail_dsds--;
  948. }
  949. /* Null termination */
  950. *cur_dsd++ = 0;
  951. *cur_dsd++ = 0;
  952. *cur_dsd++ = 0;
  953. return 0;
  954. }
  955. /**
  956. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  957. * Type 6 IOCB types.
  958. *
  959. * @sp: SRB command to process
  960. * @cmd_pkt: Command type 3 IOCB
  961. * @tot_dsds: Total number of segments to transfer
  962. */
  963. static inline int
  964. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  965. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  966. {
  967. uint32_t *cur_dsd, *fcp_dl;
  968. scsi_qla_host_t *vha;
  969. struct scsi_cmnd *cmd;
  970. struct scatterlist *cur_seg;
  971. int sgc;
  972. uint32_t total_bytes = 0;
  973. uint32_t data_bytes;
  974. uint32_t dif_bytes;
  975. uint8_t bundling = 1;
  976. uint16_t blk_size;
  977. uint8_t *clr_ptr;
  978. struct crc_context *crc_ctx_pkt = NULL;
  979. struct qla_hw_data *ha;
  980. uint8_t additional_fcpcdb_len;
  981. uint16_t fcp_cmnd_len;
  982. struct fcp_cmnd *fcp_cmnd;
  983. dma_addr_t crc_ctx_dma;
  984. char tag[2];
  985. cmd = sp->cmd;
  986. sgc = 0;
  987. /* Update entry type to indicate Command Type CRC_2 IOCB */
  988. *((uint32_t *)(&cmd_pkt->entry_type)) =
  989. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  990. vha = sp->fcport->vha;
  991. ha = vha->hw;
  992. /* No data transfer */
  993. data_bytes = scsi_bufflen(cmd);
  994. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  995. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  996. return QLA_SUCCESS;
  997. }
  998. cmd_pkt->vp_index = sp->fcport->vp_idx;
  999. /* Set transfer direction */
  1000. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1001. cmd_pkt->control_flags =
  1002. __constant_cpu_to_le16(CF_WRITE_DATA);
  1003. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1004. cmd_pkt->control_flags =
  1005. __constant_cpu_to_le16(CF_READ_DATA);
  1006. }
  1007. if ((scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_INSERT) ||
  1008. (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_STRIP) ||
  1009. (scsi_get_prot_op(sp->cmd) == SCSI_PROT_READ_STRIP) ||
  1010. (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_INSERT))
  1011. bundling = 0;
  1012. /* Allocate CRC context from global pool */
  1013. crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
  1014. GFP_ATOMIC, &crc_ctx_dma);
  1015. if (!crc_ctx_pkt)
  1016. goto crc_queuing_error;
  1017. /* Zero out CTX area. */
  1018. clr_ptr = (uint8_t *)crc_ctx_pkt;
  1019. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  1020. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  1021. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  1022. /* Set handle */
  1023. crc_ctx_pkt->handle = cmd_pkt->handle;
  1024. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  1025. qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
  1026. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  1027. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  1028. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  1029. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  1030. /* Determine SCSI command length -- align to 4 byte boundary */
  1031. if (cmd->cmd_len > 16) {
  1032. additional_fcpcdb_len = cmd->cmd_len - 16;
  1033. if ((cmd->cmd_len % 4) != 0) {
  1034. /* SCSI cmd > 16 bytes must be multiple of 4 */
  1035. goto crc_queuing_error;
  1036. }
  1037. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  1038. } else {
  1039. additional_fcpcdb_len = 0;
  1040. fcp_cmnd_len = 12 + 16 + 4;
  1041. }
  1042. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  1043. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  1044. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1045. fcp_cmnd->additional_cdb_len |= 1;
  1046. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  1047. fcp_cmnd->additional_cdb_len |= 2;
  1048. int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
  1049. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  1050. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  1051. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  1052. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  1053. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  1054. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  1055. fcp_cmnd->task_management = 0;
  1056. /*
  1057. * Update tagged queuing modifier if using command tag queuing
  1058. */
  1059. if (scsi_populate_tag_msg(cmd, tag)) {
  1060. switch (tag[0]) {
  1061. case HEAD_OF_QUEUE_TAG:
  1062. fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
  1063. break;
  1064. case ORDERED_QUEUE_TAG:
  1065. fcp_cmnd->task_attribute = TSK_ORDERED;
  1066. break;
  1067. default:
  1068. fcp_cmnd->task_attribute = 0;
  1069. break;
  1070. }
  1071. } else {
  1072. fcp_cmnd->task_attribute = 0;
  1073. }
  1074. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  1075. /* Compute dif len and adjust data len to incude protection */
  1076. dif_bytes = 0;
  1077. blk_size = cmd->device->sector_size;
  1078. dif_bytes = (data_bytes / blk_size) * 8;
  1079. switch (scsi_get_prot_op(sp->cmd)) {
  1080. case SCSI_PROT_READ_INSERT:
  1081. case SCSI_PROT_WRITE_STRIP:
  1082. total_bytes = data_bytes;
  1083. data_bytes += dif_bytes;
  1084. break;
  1085. case SCSI_PROT_READ_STRIP:
  1086. case SCSI_PROT_WRITE_INSERT:
  1087. case SCSI_PROT_READ_PASS:
  1088. case SCSI_PROT_WRITE_PASS:
  1089. total_bytes = data_bytes + dif_bytes;
  1090. break;
  1091. default:
  1092. BUG();
  1093. }
  1094. if (!qla2x00_hba_err_chk_enabled(sp))
  1095. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  1096. if (!bundling) {
  1097. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  1098. } else {
  1099. /*
  1100. * Configure Bundling if we need to fetch interlaving
  1101. * protection PCI accesses
  1102. */
  1103. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  1104. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  1105. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  1106. tot_prot_dsds);
  1107. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  1108. }
  1109. /* Finish the common fields of CRC pkt */
  1110. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  1111. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  1112. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  1113. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  1114. /* Fibre channel byte count */
  1115. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  1116. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  1117. additional_fcpcdb_len);
  1118. *fcp_dl = htonl(total_bytes);
  1119. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  1120. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  1121. return QLA_SUCCESS;
  1122. }
  1123. /* Walks data segments */
  1124. cmd_pkt->control_flags |=
  1125. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  1126. if (!bundling && tot_prot_dsds) {
  1127. if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
  1128. cur_dsd, tot_dsds))
  1129. goto crc_queuing_error;
  1130. } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  1131. (tot_dsds - tot_prot_dsds)))
  1132. goto crc_queuing_error;
  1133. if (bundling && tot_prot_dsds) {
  1134. /* Walks dif segments */
  1135. cur_seg = scsi_prot_sglist(cmd);
  1136. cmd_pkt->control_flags |=
  1137. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  1138. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  1139. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  1140. tot_prot_dsds))
  1141. goto crc_queuing_error;
  1142. }
  1143. return QLA_SUCCESS;
  1144. crc_queuing_error:
  1145. /* Cleanup will be performed by the caller */
  1146. return QLA_FUNCTION_FAILED;
  1147. }
  1148. /**
  1149. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  1150. * @sp: command to send to the ISP
  1151. *
  1152. * Returns non-zero if a failure occurred, else zero.
  1153. */
  1154. int
  1155. qla24xx_start_scsi(srb_t *sp)
  1156. {
  1157. int ret, nseg;
  1158. unsigned long flags;
  1159. uint32_t *clr_ptr;
  1160. uint32_t index;
  1161. uint32_t handle;
  1162. struct cmd_type_7 *cmd_pkt;
  1163. uint16_t cnt;
  1164. uint16_t req_cnt;
  1165. uint16_t tot_dsds;
  1166. struct req_que *req = NULL;
  1167. struct rsp_que *rsp = NULL;
  1168. struct scsi_cmnd *cmd = sp->cmd;
  1169. struct scsi_qla_host *vha = sp->fcport->vha;
  1170. struct qla_hw_data *ha = vha->hw;
  1171. char tag[2];
  1172. /* Setup device pointers. */
  1173. ret = 0;
  1174. qla25xx_set_que(sp, &rsp);
  1175. req = vha->req;
  1176. /* So we know we haven't pci_map'ed anything yet */
  1177. tot_dsds = 0;
  1178. /* Send marker if required */
  1179. if (vha->marker_needed != 0) {
  1180. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1181. QLA_SUCCESS)
  1182. return QLA_FUNCTION_FAILED;
  1183. vha->marker_needed = 0;
  1184. }
  1185. /* Acquire ring specific lock */
  1186. spin_lock_irqsave(&ha->hardware_lock, flags);
  1187. /* Check for room in outstanding command list. */
  1188. handle = req->current_outstanding_cmd;
  1189. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1190. handle++;
  1191. if (handle == MAX_OUTSTANDING_COMMANDS)
  1192. handle = 1;
  1193. if (!req->outstanding_cmds[handle])
  1194. break;
  1195. }
  1196. if (index == MAX_OUTSTANDING_COMMANDS) {
  1197. goto queuing_error;
  1198. }
  1199. /* Map the sg table so we have an accurate count of sg entries needed */
  1200. if (scsi_sg_count(cmd)) {
  1201. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1202. scsi_sg_count(cmd), cmd->sc_data_direction);
  1203. if (unlikely(!nseg))
  1204. goto queuing_error;
  1205. } else
  1206. nseg = 0;
  1207. tot_dsds = nseg;
  1208. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  1209. if (req->cnt < (req_cnt + 2)) {
  1210. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1211. if (req->ring_index < cnt)
  1212. req->cnt = cnt - req->ring_index;
  1213. else
  1214. req->cnt = req->length -
  1215. (req->ring_index - cnt);
  1216. }
  1217. if (req->cnt < (req_cnt + 2))
  1218. goto queuing_error;
  1219. /* Build command packet. */
  1220. req->current_outstanding_cmd = handle;
  1221. req->outstanding_cmds[handle] = sp;
  1222. sp->handle = handle;
  1223. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1224. req->cnt -= req_cnt;
  1225. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1226. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1227. /* Zero out remaining portion of packet. */
  1228. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1229. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1230. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1231. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1232. /* Set NPORT-ID and LUN number*/
  1233. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1234. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1235. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1236. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1237. cmd_pkt->vp_index = sp->fcport->vp_idx;
  1238. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1239. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1240. /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1241. if (scsi_populate_tag_msg(cmd, tag)) {
  1242. switch (tag[0]) {
  1243. case HEAD_OF_QUEUE_TAG:
  1244. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  1245. break;
  1246. case ORDERED_QUEUE_TAG:
  1247. cmd_pkt->task = TSK_ORDERED;
  1248. break;
  1249. }
  1250. }
  1251. /* Load SCSI command packet. */
  1252. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1253. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1254. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1255. /* Build IOCB segments */
  1256. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1257. /* Set total data segment count. */
  1258. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1259. /* Specify response queue number where completion should happen */
  1260. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1261. wmb();
  1262. /* Adjust ring index. */
  1263. req->ring_index++;
  1264. if (req->ring_index == req->length) {
  1265. req->ring_index = 0;
  1266. req->ring_ptr = req->ring;
  1267. } else
  1268. req->ring_ptr++;
  1269. sp->flags |= SRB_DMA_VALID;
  1270. /* Set chip new ring index. */
  1271. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1272. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1273. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1274. if (vha->flags.process_response_queue &&
  1275. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1276. qla24xx_process_response_queue(vha, rsp);
  1277. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1278. return QLA_SUCCESS;
  1279. queuing_error:
  1280. if (tot_dsds)
  1281. scsi_dma_unmap(cmd);
  1282. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1283. return QLA_FUNCTION_FAILED;
  1284. }
  1285. /**
  1286. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1287. * @sp: command to send to the ISP
  1288. *
  1289. * Returns non-zero if a failure occurred, else zero.
  1290. */
  1291. int
  1292. qla24xx_dif_start_scsi(srb_t *sp)
  1293. {
  1294. int nseg;
  1295. unsigned long flags;
  1296. uint32_t *clr_ptr;
  1297. uint32_t index;
  1298. uint32_t handle;
  1299. uint16_t cnt;
  1300. uint16_t req_cnt = 0;
  1301. uint16_t tot_dsds;
  1302. uint16_t tot_prot_dsds;
  1303. uint16_t fw_prot_opts = 0;
  1304. struct req_que *req = NULL;
  1305. struct rsp_que *rsp = NULL;
  1306. struct scsi_cmnd *cmd = sp->cmd;
  1307. struct scsi_qla_host *vha = sp->fcport->vha;
  1308. struct qla_hw_data *ha = vha->hw;
  1309. struct cmd_type_crc_2 *cmd_pkt;
  1310. uint32_t status = 0;
  1311. #define QDSS_GOT_Q_SPACE BIT_0
  1312. /* Only process protection or >16 cdb in this routine */
  1313. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1314. if (cmd->cmd_len <= 16)
  1315. return qla24xx_start_scsi(sp);
  1316. }
  1317. /* Setup device pointers. */
  1318. qla25xx_set_que(sp, &rsp);
  1319. req = vha->req;
  1320. /* So we know we haven't pci_map'ed anything yet */
  1321. tot_dsds = 0;
  1322. /* Send marker if required */
  1323. if (vha->marker_needed != 0) {
  1324. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1325. QLA_SUCCESS)
  1326. return QLA_FUNCTION_FAILED;
  1327. vha->marker_needed = 0;
  1328. }
  1329. /* Acquire ring specific lock */
  1330. spin_lock_irqsave(&ha->hardware_lock, flags);
  1331. /* Check for room in outstanding command list. */
  1332. handle = req->current_outstanding_cmd;
  1333. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1334. handle++;
  1335. if (handle == MAX_OUTSTANDING_COMMANDS)
  1336. handle = 1;
  1337. if (!req->outstanding_cmds[handle])
  1338. break;
  1339. }
  1340. if (index == MAX_OUTSTANDING_COMMANDS)
  1341. goto queuing_error;
  1342. /* Compute number of required data segments */
  1343. /* Map the sg table so we have an accurate count of sg entries needed */
  1344. if (scsi_sg_count(cmd)) {
  1345. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1346. scsi_sg_count(cmd), cmd->sc_data_direction);
  1347. if (unlikely(!nseg))
  1348. goto queuing_error;
  1349. else
  1350. sp->flags |= SRB_DMA_VALID;
  1351. if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
  1352. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
  1353. struct qla2_sgx sgx;
  1354. uint32_t partial;
  1355. memset(&sgx, 0, sizeof(struct qla2_sgx));
  1356. sgx.tot_bytes = scsi_bufflen(cmd);
  1357. sgx.cur_sg = scsi_sglist(cmd);
  1358. sgx.sp = sp;
  1359. nseg = 0;
  1360. while (qla24xx_get_one_block_sg(
  1361. cmd->device->sector_size, &sgx, &partial))
  1362. nseg++;
  1363. }
  1364. } else
  1365. nseg = 0;
  1366. /* number of required data segments */
  1367. tot_dsds = nseg;
  1368. /* Compute number of required protection segments */
  1369. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1370. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1371. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1372. if (unlikely(!nseg))
  1373. goto queuing_error;
  1374. else
  1375. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1376. if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
  1377. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
  1378. nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
  1379. }
  1380. } else {
  1381. nseg = 0;
  1382. }
  1383. req_cnt = 1;
  1384. /* Total Data and protection sg segment(s) */
  1385. tot_prot_dsds = nseg;
  1386. tot_dsds += nseg;
  1387. if (req->cnt < (req_cnt + 2)) {
  1388. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1389. if (req->ring_index < cnt)
  1390. req->cnt = cnt - req->ring_index;
  1391. else
  1392. req->cnt = req->length -
  1393. (req->ring_index - cnt);
  1394. }
  1395. if (req->cnt < (req_cnt + 2))
  1396. goto queuing_error;
  1397. status |= QDSS_GOT_Q_SPACE;
  1398. /* Build header part of command packet (excluding the OPCODE). */
  1399. req->current_outstanding_cmd = handle;
  1400. req->outstanding_cmds[handle] = sp;
  1401. sp->handle = handle;
  1402. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1403. req->cnt -= req_cnt;
  1404. /* Fill-in common area */
  1405. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1406. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1407. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1408. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1409. /* Set NPORT-ID and LUN number*/
  1410. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1411. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1412. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1413. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1414. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  1415. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1416. /* Total Data and protection segment(s) */
  1417. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1418. /* Build IOCB segments and adjust for data protection segments */
  1419. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1420. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1421. QLA_SUCCESS)
  1422. goto queuing_error;
  1423. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1424. /* Specify response queue number where completion should happen */
  1425. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1426. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1427. wmb();
  1428. /* Adjust ring index. */
  1429. req->ring_index++;
  1430. if (req->ring_index == req->length) {
  1431. req->ring_index = 0;
  1432. req->ring_ptr = req->ring;
  1433. } else
  1434. req->ring_ptr++;
  1435. /* Set chip new ring index. */
  1436. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1437. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1438. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1439. if (vha->flags.process_response_queue &&
  1440. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1441. qla24xx_process_response_queue(vha, rsp);
  1442. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1443. return QLA_SUCCESS;
  1444. queuing_error:
  1445. if (status & QDSS_GOT_Q_SPACE) {
  1446. req->outstanding_cmds[handle] = NULL;
  1447. req->cnt += req_cnt;
  1448. }
  1449. /* Cleanup will be performed by the caller (queuecommand) */
  1450. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1451. return QLA_FUNCTION_FAILED;
  1452. }
  1453. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1454. {
  1455. struct scsi_cmnd *cmd = sp->cmd;
  1456. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1457. int affinity = cmd->request->cpu;
  1458. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1459. affinity < ha->max_rsp_queues - 1)
  1460. *rsp = ha->rsp_q_map[affinity + 1];
  1461. else
  1462. *rsp = ha->rsp_q_map[0];
  1463. }
  1464. /* Generic Control-SRB manipulation functions. */
  1465. void *
  1466. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1467. {
  1468. struct qla_hw_data *ha = vha->hw;
  1469. struct req_que *req = ha->req_q_map[0];
  1470. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1471. uint32_t index, handle;
  1472. request_t *pkt;
  1473. uint16_t cnt, req_cnt;
  1474. pkt = NULL;
  1475. req_cnt = 1;
  1476. handle = 0;
  1477. if (!sp)
  1478. goto skip_cmd_array;
  1479. /* Check for room in outstanding command list. */
  1480. handle = req->current_outstanding_cmd;
  1481. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  1482. handle++;
  1483. if (handle == MAX_OUTSTANDING_COMMANDS)
  1484. handle = 1;
  1485. if (!req->outstanding_cmds[handle])
  1486. break;
  1487. }
  1488. if (index == MAX_OUTSTANDING_COMMANDS) {
  1489. ql_log(ql_log_warn, vha, 0x700b,
  1490. "No room on oustanding cmd array.\n");
  1491. goto queuing_error;
  1492. }
  1493. /* Prep command array. */
  1494. req->current_outstanding_cmd = handle;
  1495. req->outstanding_cmds[handle] = sp;
  1496. sp->handle = handle;
  1497. skip_cmd_array:
  1498. /* Check for room on request queue. */
  1499. if (req->cnt < req_cnt) {
  1500. if (ha->mqenable)
  1501. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1502. else if (IS_QLA82XX(ha))
  1503. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1504. else if (IS_FWI2_CAPABLE(ha))
  1505. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1506. else
  1507. cnt = qla2x00_debounce_register(
  1508. ISP_REQ_Q_OUT(ha, &reg->isp));
  1509. if (req->ring_index < cnt)
  1510. req->cnt = cnt - req->ring_index;
  1511. else
  1512. req->cnt = req->length -
  1513. (req->ring_index - cnt);
  1514. }
  1515. if (req->cnt < req_cnt)
  1516. goto queuing_error;
  1517. /* Prep packet */
  1518. req->cnt -= req_cnt;
  1519. pkt = req->ring_ptr;
  1520. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1521. pkt->entry_count = req_cnt;
  1522. pkt->handle = handle;
  1523. queuing_error:
  1524. return pkt;
  1525. }
  1526. static void
  1527. qla2x00_start_iocbs(srb_t *sp)
  1528. {
  1529. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1530. struct req_que *req = ha->req_q_map[0];
  1531. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1532. struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
  1533. if (IS_QLA82XX(ha)) {
  1534. qla82xx_start_iocbs(sp);
  1535. } else {
  1536. /* Adjust ring index. */
  1537. req->ring_index++;
  1538. if (req->ring_index == req->length) {
  1539. req->ring_index = 0;
  1540. req->ring_ptr = req->ring;
  1541. } else
  1542. req->ring_ptr++;
  1543. /* Set chip new ring index. */
  1544. if (ha->mqenable) {
  1545. WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
  1546. RD_REG_DWORD(&ioreg->hccr);
  1547. } else if (IS_QLA82XX(ha)) {
  1548. qla82xx_start_iocbs(sp);
  1549. } else if (IS_FWI2_CAPABLE(ha)) {
  1550. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  1551. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  1552. } else {
  1553. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  1554. req->ring_index);
  1555. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  1556. }
  1557. }
  1558. }
  1559. static void
  1560. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1561. {
  1562. struct srb_ctx *ctx = sp->ctx;
  1563. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1564. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1565. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1566. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1567. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1568. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1569. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1570. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1571. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1572. logio->port_id[1] = sp->fcport->d_id.b.area;
  1573. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1574. logio->vp_index = sp->fcport->vp_idx;
  1575. }
  1576. static void
  1577. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1578. {
  1579. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1580. struct srb_ctx *ctx = sp->ctx;
  1581. struct srb_iocb *lio = ctx->u.iocb_cmd;
  1582. uint16_t opts;
  1583. mbx->entry_type = MBX_IOCB_TYPE;
  1584. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1585. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1586. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1587. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1588. if (HAS_EXTENDED_IDS(ha)) {
  1589. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1590. mbx->mb10 = cpu_to_le16(opts);
  1591. } else {
  1592. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1593. }
  1594. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1595. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1596. sp->fcport->d_id.b.al_pa);
  1597. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1598. }
  1599. static void
  1600. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1601. {
  1602. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1603. logio->control_flags =
  1604. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1605. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1606. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1607. logio->port_id[1] = sp->fcport->d_id.b.area;
  1608. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1609. logio->vp_index = sp->fcport->vp_idx;
  1610. }
  1611. static void
  1612. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1613. {
  1614. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1615. mbx->entry_type = MBX_IOCB_TYPE;
  1616. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1617. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1618. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1619. cpu_to_le16(sp->fcport->loop_id):
  1620. cpu_to_le16(sp->fcport->loop_id << 8);
  1621. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1622. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1623. sp->fcport->d_id.b.al_pa);
  1624. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1625. /* Implicit: mbx->mbx10 = 0. */
  1626. }
  1627. static void
  1628. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1629. {
  1630. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1631. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1632. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1633. logio->vp_index = sp->fcport->vp_idx;
  1634. }
  1635. static void
  1636. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1637. {
  1638. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1639. mbx->entry_type = MBX_IOCB_TYPE;
  1640. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1641. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1642. if (HAS_EXTENDED_IDS(ha)) {
  1643. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1644. mbx->mb10 = cpu_to_le16(BIT_0);
  1645. } else {
  1646. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1647. }
  1648. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1649. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1650. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1651. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1652. mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
  1653. }
  1654. static void
  1655. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1656. {
  1657. uint32_t flags;
  1658. unsigned int lun;
  1659. struct fc_port *fcport = sp->fcport;
  1660. scsi_qla_host_t *vha = fcport->vha;
  1661. struct qla_hw_data *ha = vha->hw;
  1662. struct srb_ctx *ctx = sp->ctx;
  1663. struct srb_iocb *iocb = ctx->u.iocb_cmd;
  1664. struct req_que *req = vha->req;
  1665. flags = iocb->u.tmf.flags;
  1666. lun = iocb->u.tmf.lun;
  1667. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1668. tsk->entry_count = 1;
  1669. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1670. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1671. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1672. tsk->control_flags = cpu_to_le32(flags);
  1673. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1674. tsk->port_id[1] = fcport->d_id.b.area;
  1675. tsk->port_id[2] = fcport->d_id.b.domain;
  1676. tsk->vp_index = fcport->vp_idx;
  1677. if (flags == TCF_LUN_RESET) {
  1678. int_to_scsilun(lun, &tsk->lun);
  1679. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1680. sizeof(tsk->lun));
  1681. }
  1682. }
  1683. static void
  1684. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1685. {
  1686. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1687. els_iocb->entry_type = ELS_IOCB_TYPE;
  1688. els_iocb->entry_count = 1;
  1689. els_iocb->sys_define = 0;
  1690. els_iocb->entry_status = 0;
  1691. els_iocb->handle = sp->handle;
  1692. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1693. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1694. els_iocb->vp_index = sp->fcport->vp_idx;
  1695. els_iocb->sof_type = EST_SOFI3;
  1696. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1697. els_iocb->opcode =
  1698. (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
  1699. bsg_job->request->rqst_data.r_els.els_code :
  1700. bsg_job->request->rqst_data.h_els.command_code;
  1701. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1702. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1703. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1704. els_iocb->control_flags = 0;
  1705. els_iocb->rx_byte_count =
  1706. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1707. els_iocb->tx_byte_count =
  1708. cpu_to_le32(bsg_job->request_payload.payload_len);
  1709. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1710. (bsg_job->request_payload.sg_list)));
  1711. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1712. (bsg_job->request_payload.sg_list)));
  1713. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1714. (bsg_job->request_payload.sg_list));
  1715. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1716. (bsg_job->reply_payload.sg_list)));
  1717. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1718. (bsg_job->reply_payload.sg_list)));
  1719. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1720. (bsg_job->reply_payload.sg_list));
  1721. }
  1722. static void
  1723. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1724. {
  1725. uint16_t avail_dsds;
  1726. uint32_t *cur_dsd;
  1727. struct scatterlist *sg;
  1728. int index;
  1729. uint16_t tot_dsds;
  1730. scsi_qla_host_t *vha = sp->fcport->vha;
  1731. struct qla_hw_data *ha = vha->hw;
  1732. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1733. int loop_iterartion = 0;
  1734. int cont_iocb_prsnt = 0;
  1735. int entry_count = 1;
  1736. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1737. ct_iocb->entry_type = CT_IOCB_TYPE;
  1738. ct_iocb->entry_status = 0;
  1739. ct_iocb->handle1 = sp->handle;
  1740. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1741. ct_iocb->status = __constant_cpu_to_le16(0);
  1742. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1743. ct_iocb->timeout = 0;
  1744. ct_iocb->cmd_dsd_count =
  1745. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1746. ct_iocb->total_dsd_count =
  1747. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1748. ct_iocb->req_bytecount =
  1749. cpu_to_le32(bsg_job->request_payload.payload_len);
  1750. ct_iocb->rsp_bytecount =
  1751. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1752. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1753. (bsg_job->request_payload.sg_list)));
  1754. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1755. (bsg_job->request_payload.sg_list)));
  1756. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1757. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1758. (bsg_job->reply_payload.sg_list)));
  1759. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1760. (bsg_job->reply_payload.sg_list)));
  1761. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1762. avail_dsds = 1;
  1763. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1764. index = 0;
  1765. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1766. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1767. dma_addr_t sle_dma;
  1768. cont_a64_entry_t *cont_pkt;
  1769. /* Allocate additional continuation packets? */
  1770. if (avail_dsds == 0) {
  1771. /*
  1772. * Five DSDs are available in the Cont.
  1773. * Type 1 IOCB.
  1774. */
  1775. cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
  1776. vha->hw->req_q_map[0]);
  1777. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1778. avail_dsds = 5;
  1779. cont_iocb_prsnt = 1;
  1780. entry_count++;
  1781. }
  1782. sle_dma = sg_dma_address(sg);
  1783. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1784. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1785. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1786. loop_iterartion++;
  1787. avail_dsds--;
  1788. }
  1789. ct_iocb->entry_count = entry_count;
  1790. }
  1791. static void
  1792. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1793. {
  1794. uint16_t avail_dsds;
  1795. uint32_t *cur_dsd;
  1796. struct scatterlist *sg;
  1797. int index;
  1798. uint16_t tot_dsds;
  1799. scsi_qla_host_t *vha = sp->fcport->vha;
  1800. struct qla_hw_data *ha = vha->hw;
  1801. struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
  1802. int loop_iterartion = 0;
  1803. int cont_iocb_prsnt = 0;
  1804. int entry_count = 1;
  1805. ct_iocb->entry_type = CT_IOCB_TYPE;
  1806. ct_iocb->entry_status = 0;
  1807. ct_iocb->sys_define = 0;
  1808. ct_iocb->handle = sp->handle;
  1809. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1810. ct_iocb->vp_index = sp->fcport->vp_idx;
  1811. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1812. ct_iocb->cmd_dsd_count =
  1813. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1814. ct_iocb->timeout = 0;
  1815. ct_iocb->rsp_dsd_count =
  1816. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1817. ct_iocb->rsp_byte_count =
  1818. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1819. ct_iocb->cmd_byte_count =
  1820. cpu_to_le32(bsg_job->request_payload.payload_len);
  1821. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1822. (bsg_job->request_payload.sg_list)));
  1823. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1824. (bsg_job->request_payload.sg_list)));
  1825. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1826. (bsg_job->request_payload.sg_list));
  1827. avail_dsds = 1;
  1828. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1829. index = 0;
  1830. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1831. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1832. dma_addr_t sle_dma;
  1833. cont_a64_entry_t *cont_pkt;
  1834. /* Allocate additional continuation packets? */
  1835. if (avail_dsds == 0) {
  1836. /*
  1837. * Five DSDs are available in the Cont.
  1838. * Type 1 IOCB.
  1839. */
  1840. cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
  1841. ha->req_q_map[0]);
  1842. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1843. avail_dsds = 5;
  1844. cont_iocb_prsnt = 1;
  1845. entry_count++;
  1846. }
  1847. sle_dma = sg_dma_address(sg);
  1848. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1849. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1850. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1851. loop_iterartion++;
  1852. avail_dsds--;
  1853. }
  1854. ct_iocb->entry_count = entry_count;
  1855. }
  1856. int
  1857. qla2x00_start_sp(srb_t *sp)
  1858. {
  1859. int rval;
  1860. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1861. void *pkt;
  1862. struct srb_ctx *ctx = sp->ctx;
  1863. unsigned long flags;
  1864. rval = QLA_FUNCTION_FAILED;
  1865. spin_lock_irqsave(&ha->hardware_lock, flags);
  1866. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  1867. if (!pkt) {
  1868. ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
  1869. "qla2x00_alloc_iocbs failed.\n");
  1870. goto done;
  1871. }
  1872. rval = QLA_SUCCESS;
  1873. switch (ctx->type) {
  1874. case SRB_LOGIN_CMD:
  1875. IS_FWI2_CAPABLE(ha) ?
  1876. qla24xx_login_iocb(sp, pkt) :
  1877. qla2x00_login_iocb(sp, pkt);
  1878. break;
  1879. case SRB_LOGOUT_CMD:
  1880. IS_FWI2_CAPABLE(ha) ?
  1881. qla24xx_logout_iocb(sp, pkt) :
  1882. qla2x00_logout_iocb(sp, pkt);
  1883. break;
  1884. case SRB_ELS_CMD_RPT:
  1885. case SRB_ELS_CMD_HST:
  1886. qla24xx_els_iocb(sp, pkt);
  1887. break;
  1888. case SRB_CT_CMD:
  1889. IS_FWI2_CAPABLE(ha) ?
  1890. qla24xx_ct_iocb(sp, pkt) :
  1891. qla2x00_ct_iocb(sp, pkt);
  1892. break;
  1893. case SRB_ADISC_CMD:
  1894. IS_FWI2_CAPABLE(ha) ?
  1895. qla24xx_adisc_iocb(sp, pkt) :
  1896. qla2x00_adisc_iocb(sp, pkt);
  1897. break;
  1898. case SRB_TM_CMD:
  1899. qla24xx_tm_iocb(sp, pkt);
  1900. break;
  1901. default:
  1902. break;
  1903. }
  1904. wmb();
  1905. qla2x00_start_iocbs(sp);
  1906. done:
  1907. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1908. return rval;
  1909. }