qla_iocb.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2013 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include "qla_target.h"
  9. #include <linux/blkdev.h>
  10. #include <linux/delay.h>
  11. #include <scsi/scsi_tcq.h>
  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. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  24. struct scsi_qla_host *vha = sp->fcport->vha;
  25. cflags = 0;
  26. /* Set transfer direction */
  27. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  28. cflags = CF_WRITE;
  29. vha->qla_stats.output_bytes += scsi_bufflen(cmd);
  30. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  31. cflags = CF_READ;
  32. vha->qla_stats.input_bytes += scsi_bufflen(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. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  129. uint8_t guard = scsi_host_get_guard(cmd->device->host);
  130. /* We always use DIFF Bundling for best performance */
  131. *fw_prot_opts = 0;
  132. /* Translate SCSI opcode to a protection opcode */
  133. switch (scsi_get_prot_op(cmd)) {
  134. case SCSI_PROT_READ_STRIP:
  135. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  136. break;
  137. case SCSI_PROT_WRITE_INSERT:
  138. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  139. break;
  140. case SCSI_PROT_READ_INSERT:
  141. *fw_prot_opts |= PO_MODE_DIF_INSERT;
  142. break;
  143. case SCSI_PROT_WRITE_STRIP:
  144. *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  145. break;
  146. case SCSI_PROT_READ_PASS:
  147. case SCSI_PROT_WRITE_PASS:
  148. if (guard & SHOST_DIX_GUARD_IP)
  149. *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
  150. else
  151. *fw_prot_opts |= PO_MODE_DIF_PASS;
  152. break;
  153. default: /* Normal Request */
  154. *fw_prot_opts |= PO_MODE_DIF_PASS;
  155. break;
  156. }
  157. return scsi_prot_sg_count(cmd);
  158. }
  159. /*
  160. * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
  161. * capable IOCB types.
  162. *
  163. * @sp: SRB command to process
  164. * @cmd_pkt: Command type 2 IOCB
  165. * @tot_dsds: Total number of segments to transfer
  166. */
  167. void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
  168. uint16_t tot_dsds)
  169. {
  170. uint16_t avail_dsds;
  171. uint32_t *cur_dsd;
  172. scsi_qla_host_t *vha;
  173. struct scsi_cmnd *cmd;
  174. struct scatterlist *sg;
  175. int i;
  176. cmd = GET_CMD_SP(sp);
  177. /* Update entry type to indicate Command Type 2 IOCB */
  178. *((uint32_t *)(&cmd_pkt->entry_type)) =
  179. __constant_cpu_to_le32(COMMAND_TYPE);
  180. /* No data transfer */
  181. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  182. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  183. return;
  184. }
  185. vha = sp->fcport->vha;
  186. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  187. /* Three DSDs are available in the Command Type 2 IOCB */
  188. avail_dsds = 3;
  189. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  190. /* Load data segments */
  191. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  192. cont_entry_t *cont_pkt;
  193. /* Allocate additional continuation packets? */
  194. if (avail_dsds == 0) {
  195. /*
  196. * Seven DSDs are available in the Continuation
  197. * Type 0 IOCB.
  198. */
  199. cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
  200. cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
  201. avail_dsds = 7;
  202. }
  203. *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
  204. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  205. avail_dsds--;
  206. }
  207. }
  208. /**
  209. * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
  210. * capable IOCB types.
  211. *
  212. * @sp: SRB command to process
  213. * @cmd_pkt: Command type 3 IOCB
  214. * @tot_dsds: Total number of segments to transfer
  215. */
  216. void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
  217. uint16_t tot_dsds)
  218. {
  219. uint16_t avail_dsds;
  220. uint32_t *cur_dsd;
  221. scsi_qla_host_t *vha;
  222. struct scsi_cmnd *cmd;
  223. struct scatterlist *sg;
  224. int i;
  225. cmd = GET_CMD_SP(sp);
  226. /* Update entry type to indicate Command Type 3 IOCB */
  227. *((uint32_t *)(&cmd_pkt->entry_type)) =
  228. __constant_cpu_to_le32(COMMAND_A64_TYPE);
  229. /* No data transfer */
  230. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  231. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  232. return;
  233. }
  234. vha = sp->fcport->vha;
  235. cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
  236. /* Two DSDs are available in the Command Type 3 IOCB */
  237. avail_dsds = 2;
  238. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  239. /* Load data segments */
  240. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  241. dma_addr_t sle_dma;
  242. cont_a64_entry_t *cont_pkt;
  243. /* Allocate additional continuation packets? */
  244. if (avail_dsds == 0) {
  245. /*
  246. * Five DSDs are available in the Continuation
  247. * Type 1 IOCB.
  248. */
  249. cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
  250. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  251. avail_dsds = 5;
  252. }
  253. sle_dma = sg_dma_address(sg);
  254. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  255. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  256. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  257. avail_dsds--;
  258. }
  259. }
  260. /**
  261. * qla2x00_start_scsi() - Send a SCSI command to the ISP
  262. * @sp: command to send to the ISP
  263. *
  264. * Returns non-zero if a failure occurred, else zero.
  265. */
  266. int
  267. qla2x00_start_scsi(srb_t *sp)
  268. {
  269. int ret, nseg;
  270. unsigned long flags;
  271. scsi_qla_host_t *vha;
  272. struct scsi_cmnd *cmd;
  273. uint32_t *clr_ptr;
  274. uint32_t index;
  275. uint32_t handle;
  276. cmd_entry_t *cmd_pkt;
  277. uint16_t cnt;
  278. uint16_t req_cnt;
  279. uint16_t tot_dsds;
  280. struct device_reg_2xxx __iomem *reg;
  281. struct qla_hw_data *ha;
  282. struct req_que *req;
  283. struct rsp_que *rsp;
  284. char tag[2];
  285. /* Setup device pointers. */
  286. ret = 0;
  287. vha = sp->fcport->vha;
  288. ha = vha->hw;
  289. reg = &ha->iobase->isp;
  290. cmd = GET_CMD_SP(sp);
  291. req = ha->req_q_map[0];
  292. rsp = ha->rsp_q_map[0];
  293. /* So we know we haven't pci_map'ed anything yet */
  294. tot_dsds = 0;
  295. /* Send marker if required */
  296. if (vha->marker_needed != 0) {
  297. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  298. QLA_SUCCESS) {
  299. return (QLA_FUNCTION_FAILED);
  300. }
  301. vha->marker_needed = 0;
  302. }
  303. /* Acquire ring specific lock */
  304. spin_lock_irqsave(&ha->hardware_lock, flags);
  305. /* Check for room in outstanding command list. */
  306. handle = req->current_outstanding_cmd;
  307. for (index = 1; index < req->num_outstanding_cmds; index++) {
  308. handle++;
  309. if (handle == req->num_outstanding_cmds)
  310. handle = 1;
  311. if (!req->outstanding_cmds[handle])
  312. break;
  313. }
  314. if (index == req->num_outstanding_cmds)
  315. goto queuing_error;
  316. /* Map the sg table so we have an accurate count of sg entries needed */
  317. if (scsi_sg_count(cmd)) {
  318. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  319. scsi_sg_count(cmd), cmd->sc_data_direction);
  320. if (unlikely(!nseg))
  321. goto queuing_error;
  322. } else
  323. nseg = 0;
  324. tot_dsds = nseg;
  325. /* Calculate the number of request entries needed. */
  326. req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
  327. if (req->cnt < (req_cnt + 2)) {
  328. cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
  329. if (req->ring_index < cnt)
  330. req->cnt = cnt - req->ring_index;
  331. else
  332. req->cnt = req->length -
  333. (req->ring_index - cnt);
  334. /* If still no head room then bail out */
  335. if (req->cnt < (req_cnt + 2))
  336. goto queuing_error;
  337. }
  338. /* Build command packet */
  339. req->current_outstanding_cmd = handle;
  340. req->outstanding_cmds[handle] = sp;
  341. sp->handle = handle;
  342. cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  343. req->cnt -= req_cnt;
  344. cmd_pkt = (cmd_entry_t *)req->ring_ptr;
  345. cmd_pkt->handle = handle;
  346. /* Zero out remaining portion of packet. */
  347. clr_ptr = (uint32_t *)cmd_pkt + 2;
  348. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  349. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  350. /* Set target ID and LUN number*/
  351. SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
  352. cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
  353. /* Update tagged queuing modifier */
  354. if (scsi_populate_tag_msg(cmd, tag)) {
  355. switch (tag[0]) {
  356. case HEAD_OF_QUEUE_TAG:
  357. cmd_pkt->control_flags =
  358. __constant_cpu_to_le16(CF_HEAD_TAG);
  359. break;
  360. case ORDERED_QUEUE_TAG:
  361. cmd_pkt->control_flags =
  362. __constant_cpu_to_le16(CF_ORDERED_TAG);
  363. break;
  364. default:
  365. cmd_pkt->control_flags =
  366. __constant_cpu_to_le16(CF_SIMPLE_TAG);
  367. break;
  368. }
  369. }
  370. /* Load SCSI command packet. */
  371. memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
  372. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  373. /* Build IOCB segments */
  374. ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
  375. /* Set total data segment count. */
  376. cmd_pkt->entry_count = (uint8_t)req_cnt;
  377. wmb();
  378. /* Adjust ring index. */
  379. req->ring_index++;
  380. if (req->ring_index == req->length) {
  381. req->ring_index = 0;
  382. req->ring_ptr = req->ring;
  383. } else
  384. req->ring_ptr++;
  385. sp->flags |= SRB_DMA_VALID;
  386. /* Set chip new ring index. */
  387. WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
  388. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
  389. /* Manage unprocessed RIO/ZIO commands in response queue. */
  390. if (vha->flags.process_response_queue &&
  391. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  392. qla2x00_process_response_queue(rsp);
  393. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  394. return (QLA_SUCCESS);
  395. queuing_error:
  396. if (tot_dsds)
  397. scsi_dma_unmap(cmd);
  398. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  399. return (QLA_FUNCTION_FAILED);
  400. }
  401. /**
  402. * qla2x00_start_iocbs() - Execute the IOCB command
  403. */
  404. void
  405. qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
  406. {
  407. struct qla_hw_data *ha = vha->hw;
  408. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  409. if (IS_QLA82XX(ha)) {
  410. qla82xx_start_iocbs(vha);
  411. } else {
  412. /* Adjust ring index. */
  413. req->ring_index++;
  414. if (req->ring_index == req->length) {
  415. req->ring_index = 0;
  416. req->ring_ptr = req->ring;
  417. } else
  418. req->ring_ptr++;
  419. /* Set chip new ring index. */
  420. if (ha->mqenable || IS_QLA83XX(ha)) {
  421. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  422. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  423. } else if (IS_FWI2_CAPABLE(ha)) {
  424. WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
  425. RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
  426. } else {
  427. WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
  428. req->ring_index);
  429. RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
  430. }
  431. }
  432. }
  433. /**
  434. * qla2x00_marker() - Send a marker IOCB to the firmware.
  435. * @ha: HA context
  436. * @loop_id: loop ID
  437. * @lun: LUN
  438. * @type: marker modifier
  439. *
  440. * Can be called from both normal and interrupt context.
  441. *
  442. * Returns non-zero if a failure occurred, else zero.
  443. */
  444. static int
  445. __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  446. struct rsp_que *rsp, uint16_t loop_id,
  447. uint16_t lun, uint8_t type)
  448. {
  449. mrk_entry_t *mrk;
  450. struct mrk_entry_24xx *mrk24;
  451. struct qla_hw_data *ha = vha->hw;
  452. scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
  453. mrk24 = NULL;
  454. req = ha->req_q_map[0];
  455. mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
  456. if (mrk == NULL) {
  457. ql_log(ql_log_warn, base_vha, 0x3026,
  458. "Failed to allocate Marker IOCB.\n");
  459. return (QLA_FUNCTION_FAILED);
  460. }
  461. mrk->entry_type = MARKER_TYPE;
  462. mrk->modifier = type;
  463. if (type != MK_SYNC_ALL) {
  464. if (IS_FWI2_CAPABLE(ha)) {
  465. mrk24 = (struct mrk_entry_24xx *) mrk;
  466. mrk24->nport_handle = cpu_to_le16(loop_id);
  467. mrk24->lun[1] = LSB(lun);
  468. mrk24->lun[2] = MSB(lun);
  469. host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
  470. mrk24->vp_index = vha->vp_idx;
  471. mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
  472. } else {
  473. SET_TARGET_ID(ha, mrk->target, loop_id);
  474. mrk->lun = cpu_to_le16(lun);
  475. }
  476. }
  477. wmb();
  478. qla2x00_start_iocbs(vha, req);
  479. return (QLA_SUCCESS);
  480. }
  481. int
  482. qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
  483. struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
  484. uint8_t type)
  485. {
  486. int ret;
  487. unsigned long flags = 0;
  488. spin_lock_irqsave(&vha->hw->hardware_lock, flags);
  489. ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
  490. spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
  491. return (ret);
  492. }
  493. /*
  494. * qla2x00_issue_marker
  495. *
  496. * Issue marker
  497. * Caller CAN have hardware lock held as specified by ha_locked parameter.
  498. * Might release it, then reaquire.
  499. */
  500. int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
  501. {
  502. if (ha_locked) {
  503. if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
  504. MK_SYNC_ALL) != QLA_SUCCESS)
  505. return QLA_FUNCTION_FAILED;
  506. } else {
  507. if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
  508. MK_SYNC_ALL) != QLA_SUCCESS)
  509. return QLA_FUNCTION_FAILED;
  510. }
  511. vha->marker_needed = 0;
  512. return QLA_SUCCESS;
  513. }
  514. /**
  515. * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
  516. * Continuation Type 1 IOCBs to allocate.
  517. *
  518. * @dsds: number of data segment decriptors needed
  519. *
  520. * Returns the number of IOCB entries needed to store @dsds.
  521. */
  522. inline uint16_t
  523. qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
  524. {
  525. uint16_t iocbs;
  526. iocbs = 1;
  527. if (dsds > 1) {
  528. iocbs += (dsds - 1) / 5;
  529. if ((dsds - 1) % 5)
  530. iocbs++;
  531. }
  532. return iocbs;
  533. }
  534. static inline int
  535. qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
  536. uint16_t tot_dsds)
  537. {
  538. uint32_t *cur_dsd = NULL;
  539. scsi_qla_host_t *vha;
  540. struct qla_hw_data *ha;
  541. struct scsi_cmnd *cmd;
  542. struct scatterlist *cur_seg;
  543. uint32_t *dsd_seg;
  544. void *next_dsd;
  545. uint8_t avail_dsds;
  546. uint8_t first_iocb = 1;
  547. uint32_t dsd_list_len;
  548. struct dsd_dma *dsd_ptr;
  549. struct ct6_dsd *ctx;
  550. cmd = GET_CMD_SP(sp);
  551. /* Update entry type to indicate Command Type 3 IOCB */
  552. *((uint32_t *)(&cmd_pkt->entry_type)) =
  553. __constant_cpu_to_le32(COMMAND_TYPE_6);
  554. /* No data transfer */
  555. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  556. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  557. return 0;
  558. }
  559. vha = sp->fcport->vha;
  560. ha = vha->hw;
  561. /* Set transfer direction */
  562. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  563. cmd_pkt->control_flags =
  564. __constant_cpu_to_le16(CF_WRITE_DATA);
  565. vha->qla_stats.output_bytes += scsi_bufflen(cmd);
  566. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  567. cmd_pkt->control_flags =
  568. __constant_cpu_to_le16(CF_READ_DATA);
  569. vha->qla_stats.input_bytes += scsi_bufflen(cmd);
  570. }
  571. cur_seg = scsi_sglist(cmd);
  572. ctx = GET_CMD_CTX_SP(sp);
  573. while (tot_dsds) {
  574. avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
  575. QLA_DSDS_PER_IOCB : tot_dsds;
  576. tot_dsds -= avail_dsds;
  577. dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
  578. dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
  579. struct dsd_dma, list);
  580. next_dsd = dsd_ptr->dsd_addr;
  581. list_del(&dsd_ptr->list);
  582. ha->gbl_dsd_avail--;
  583. list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
  584. ctx->dsd_use_cnt++;
  585. ha->gbl_dsd_inuse++;
  586. if (first_iocb) {
  587. first_iocb = 0;
  588. dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
  589. *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  590. *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  591. cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
  592. } else {
  593. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  594. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  595. *cur_dsd++ = cpu_to_le32(dsd_list_len);
  596. }
  597. cur_dsd = (uint32_t *)next_dsd;
  598. while (avail_dsds) {
  599. dma_addr_t sle_dma;
  600. sle_dma = sg_dma_address(cur_seg);
  601. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  602. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  603. *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
  604. cur_seg = sg_next(cur_seg);
  605. avail_dsds--;
  606. }
  607. }
  608. /* Null termination */
  609. *cur_dsd++ = 0;
  610. *cur_dsd++ = 0;
  611. *cur_dsd++ = 0;
  612. cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
  613. return 0;
  614. }
  615. /*
  616. * qla24xx_calc_dsd_lists() - Determine number of DSD list required
  617. * for Command Type 6.
  618. *
  619. * @dsds: number of data segment decriptors needed
  620. *
  621. * Returns the number of dsd list needed to store @dsds.
  622. */
  623. inline uint16_t
  624. qla24xx_calc_dsd_lists(uint16_t dsds)
  625. {
  626. uint16_t dsd_lists = 0;
  627. dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
  628. if (dsds % QLA_DSDS_PER_IOCB)
  629. dsd_lists++;
  630. return dsd_lists;
  631. }
  632. /**
  633. * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
  634. * IOCB types.
  635. *
  636. * @sp: SRB command to process
  637. * @cmd_pkt: Command type 3 IOCB
  638. * @tot_dsds: Total number of segments to transfer
  639. */
  640. inline void
  641. qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
  642. uint16_t tot_dsds)
  643. {
  644. uint16_t avail_dsds;
  645. uint32_t *cur_dsd;
  646. scsi_qla_host_t *vha;
  647. struct scsi_cmnd *cmd;
  648. struct scatterlist *sg;
  649. int i;
  650. struct req_que *req;
  651. cmd = GET_CMD_SP(sp);
  652. /* Update entry type to indicate Command Type 3 IOCB */
  653. *((uint32_t *)(&cmd_pkt->entry_type)) =
  654. __constant_cpu_to_le32(COMMAND_TYPE_7);
  655. /* No data transfer */
  656. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  657. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  658. return;
  659. }
  660. vha = sp->fcport->vha;
  661. req = vha->req;
  662. /* Set transfer direction */
  663. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  664. cmd_pkt->task_mgmt_flags =
  665. __constant_cpu_to_le16(TMF_WRITE_DATA);
  666. vha->qla_stats.output_bytes += scsi_bufflen(cmd);
  667. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  668. cmd_pkt->task_mgmt_flags =
  669. __constant_cpu_to_le16(TMF_READ_DATA);
  670. vha->qla_stats.input_bytes += scsi_bufflen(cmd);
  671. }
  672. /* One DSD is available in the Command Type 3 IOCB */
  673. avail_dsds = 1;
  674. cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
  675. /* Load data segments */
  676. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  677. dma_addr_t sle_dma;
  678. cont_a64_entry_t *cont_pkt;
  679. /* Allocate additional continuation packets? */
  680. if (avail_dsds == 0) {
  681. /*
  682. * Five DSDs are available in the Continuation
  683. * Type 1 IOCB.
  684. */
  685. cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
  686. cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
  687. avail_dsds = 5;
  688. }
  689. sle_dma = sg_dma_address(sg);
  690. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  691. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  692. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  693. avail_dsds--;
  694. }
  695. }
  696. struct fw_dif_context {
  697. uint32_t ref_tag;
  698. uint16_t app_tag;
  699. uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
  700. uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
  701. };
  702. /*
  703. * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
  704. *
  705. */
  706. static inline void
  707. qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
  708. unsigned int protcnt)
  709. {
  710. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  711. switch (scsi_get_prot_type(cmd)) {
  712. case SCSI_PROT_DIF_TYPE0:
  713. /*
  714. * No check for ql2xenablehba_err_chk, as it would be an
  715. * I/O error if hba tag generation is not done.
  716. */
  717. pkt->ref_tag = cpu_to_le32((uint32_t)
  718. (0xffffffff & scsi_get_lba(cmd)));
  719. if (!qla2x00_hba_err_chk_enabled(sp))
  720. break;
  721. pkt->ref_tag_mask[0] = 0xff;
  722. pkt->ref_tag_mask[1] = 0xff;
  723. pkt->ref_tag_mask[2] = 0xff;
  724. pkt->ref_tag_mask[3] = 0xff;
  725. break;
  726. /*
  727. * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
  728. * match LBA in CDB + N
  729. */
  730. case SCSI_PROT_DIF_TYPE2:
  731. pkt->app_tag = __constant_cpu_to_le16(0);
  732. pkt->app_tag_mask[0] = 0x0;
  733. pkt->app_tag_mask[1] = 0x0;
  734. pkt->ref_tag = cpu_to_le32((uint32_t)
  735. (0xffffffff & scsi_get_lba(cmd)));
  736. if (!qla2x00_hba_err_chk_enabled(sp))
  737. break;
  738. /* enable ALL bytes of the ref tag */
  739. pkt->ref_tag_mask[0] = 0xff;
  740. pkt->ref_tag_mask[1] = 0xff;
  741. pkt->ref_tag_mask[2] = 0xff;
  742. pkt->ref_tag_mask[3] = 0xff;
  743. break;
  744. /* For Type 3 protection: 16 bit GUARD only */
  745. case SCSI_PROT_DIF_TYPE3:
  746. pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
  747. pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
  748. 0x00;
  749. break;
  750. /*
  751. * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
  752. * 16 bit app tag.
  753. */
  754. case SCSI_PROT_DIF_TYPE1:
  755. pkt->ref_tag = cpu_to_le32((uint32_t)
  756. (0xffffffff & scsi_get_lba(cmd)));
  757. pkt->app_tag = __constant_cpu_to_le16(0);
  758. pkt->app_tag_mask[0] = 0x0;
  759. pkt->app_tag_mask[1] = 0x0;
  760. if (!qla2x00_hba_err_chk_enabled(sp))
  761. break;
  762. /* enable ALL bytes of the ref tag */
  763. pkt->ref_tag_mask[0] = 0xff;
  764. pkt->ref_tag_mask[1] = 0xff;
  765. pkt->ref_tag_mask[2] = 0xff;
  766. pkt->ref_tag_mask[3] = 0xff;
  767. break;
  768. }
  769. }
  770. struct qla2_sgx {
  771. dma_addr_t dma_addr; /* OUT */
  772. uint32_t dma_len; /* OUT */
  773. uint32_t tot_bytes; /* IN */
  774. struct scatterlist *cur_sg; /* IN */
  775. /* for book keeping, bzero on initial invocation */
  776. uint32_t bytes_consumed;
  777. uint32_t num_bytes;
  778. uint32_t tot_partial;
  779. /* for debugging */
  780. uint32_t num_sg;
  781. srb_t *sp;
  782. };
  783. static int
  784. qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
  785. uint32_t *partial)
  786. {
  787. struct scatterlist *sg;
  788. uint32_t cumulative_partial, sg_len;
  789. dma_addr_t sg_dma_addr;
  790. if (sgx->num_bytes == sgx->tot_bytes)
  791. return 0;
  792. sg = sgx->cur_sg;
  793. cumulative_partial = sgx->tot_partial;
  794. sg_dma_addr = sg_dma_address(sg);
  795. sg_len = sg_dma_len(sg);
  796. sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
  797. if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
  798. sgx->dma_len = (blk_sz - cumulative_partial);
  799. sgx->tot_partial = 0;
  800. sgx->num_bytes += blk_sz;
  801. *partial = 0;
  802. } else {
  803. sgx->dma_len = sg_len - sgx->bytes_consumed;
  804. sgx->tot_partial += sgx->dma_len;
  805. *partial = 1;
  806. }
  807. sgx->bytes_consumed += sgx->dma_len;
  808. if (sg_len == sgx->bytes_consumed) {
  809. sg = sg_next(sg);
  810. sgx->num_sg++;
  811. sgx->cur_sg = sg;
  812. sgx->bytes_consumed = 0;
  813. }
  814. return 1;
  815. }
  816. static int
  817. qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
  818. uint32_t *dsd, uint16_t tot_dsds)
  819. {
  820. void *next_dsd;
  821. uint8_t avail_dsds = 0;
  822. uint32_t dsd_list_len;
  823. struct dsd_dma *dsd_ptr;
  824. struct scatterlist *sg_prot;
  825. uint32_t *cur_dsd = dsd;
  826. uint16_t used_dsds = tot_dsds;
  827. uint32_t prot_int;
  828. uint32_t partial;
  829. struct qla2_sgx sgx;
  830. dma_addr_t sle_dma;
  831. uint32_t sle_dma_len, tot_prot_dma_len = 0;
  832. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  833. prot_int = cmd->device->sector_size;
  834. memset(&sgx, 0, sizeof(struct qla2_sgx));
  835. sgx.tot_bytes = scsi_bufflen(cmd);
  836. sgx.cur_sg = scsi_sglist(cmd);
  837. sgx.sp = sp;
  838. sg_prot = scsi_prot_sglist(cmd);
  839. while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
  840. sle_dma = sgx.dma_addr;
  841. sle_dma_len = sgx.dma_len;
  842. alloc_and_fill:
  843. /* Allocate additional continuation packets? */
  844. if (avail_dsds == 0) {
  845. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  846. QLA_DSDS_PER_IOCB : used_dsds;
  847. dsd_list_len = (avail_dsds + 1) * 12;
  848. used_dsds -= avail_dsds;
  849. /* allocate tracking DS */
  850. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  851. if (!dsd_ptr)
  852. return 1;
  853. /* allocate new list */
  854. dsd_ptr->dsd_addr = next_dsd =
  855. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  856. &dsd_ptr->dsd_list_dma);
  857. if (!next_dsd) {
  858. /*
  859. * Need to cleanup only this dsd_ptr, rest
  860. * will be done by sp_free_dma()
  861. */
  862. kfree(dsd_ptr);
  863. return 1;
  864. }
  865. list_add_tail(&dsd_ptr->list,
  866. &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
  867. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  868. /* add new list to cmd iocb or last list */
  869. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  870. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  871. *cur_dsd++ = dsd_list_len;
  872. cur_dsd = (uint32_t *)next_dsd;
  873. }
  874. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  875. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  876. *cur_dsd++ = cpu_to_le32(sle_dma_len);
  877. avail_dsds--;
  878. if (partial == 0) {
  879. /* Got a full protection interval */
  880. sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
  881. sle_dma_len = 8;
  882. tot_prot_dma_len += sle_dma_len;
  883. if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
  884. tot_prot_dma_len = 0;
  885. sg_prot = sg_next(sg_prot);
  886. }
  887. partial = 1; /* So as to not re-enter this block */
  888. goto alloc_and_fill;
  889. }
  890. }
  891. /* Null termination */
  892. *cur_dsd++ = 0;
  893. *cur_dsd++ = 0;
  894. *cur_dsd++ = 0;
  895. return 0;
  896. }
  897. static int
  898. qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
  899. uint16_t tot_dsds)
  900. {
  901. void *next_dsd;
  902. uint8_t avail_dsds = 0;
  903. uint32_t dsd_list_len;
  904. struct dsd_dma *dsd_ptr;
  905. struct scatterlist *sg;
  906. uint32_t *cur_dsd = dsd;
  907. int i;
  908. uint16_t used_dsds = tot_dsds;
  909. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  910. scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  911. dma_addr_t sle_dma;
  912. /* Allocate additional continuation packets? */
  913. if (avail_dsds == 0) {
  914. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  915. QLA_DSDS_PER_IOCB : used_dsds;
  916. dsd_list_len = (avail_dsds + 1) * 12;
  917. used_dsds -= avail_dsds;
  918. /* allocate tracking DS */
  919. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  920. if (!dsd_ptr)
  921. return 1;
  922. /* allocate new list */
  923. dsd_ptr->dsd_addr = next_dsd =
  924. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  925. &dsd_ptr->dsd_list_dma);
  926. if (!next_dsd) {
  927. /*
  928. * Need to cleanup only this dsd_ptr, rest
  929. * will be done by sp_free_dma()
  930. */
  931. kfree(dsd_ptr);
  932. return 1;
  933. }
  934. list_add_tail(&dsd_ptr->list,
  935. &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
  936. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  937. /* add new list to cmd iocb or last list */
  938. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  939. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  940. *cur_dsd++ = dsd_list_len;
  941. cur_dsd = (uint32_t *)next_dsd;
  942. }
  943. sle_dma = sg_dma_address(sg);
  944. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  945. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  946. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  947. avail_dsds--;
  948. }
  949. /* Null termination */
  950. *cur_dsd++ = 0;
  951. *cur_dsd++ = 0;
  952. *cur_dsd++ = 0;
  953. return 0;
  954. }
  955. static int
  956. qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
  957. uint32_t *dsd,
  958. uint16_t tot_dsds)
  959. {
  960. void *next_dsd;
  961. uint8_t avail_dsds = 0;
  962. uint32_t dsd_list_len;
  963. struct dsd_dma *dsd_ptr;
  964. struct scatterlist *sg;
  965. int i;
  966. struct scsi_cmnd *cmd;
  967. uint32_t *cur_dsd = dsd;
  968. uint16_t used_dsds = tot_dsds;
  969. cmd = GET_CMD_SP(sp);
  970. scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
  971. dma_addr_t sle_dma;
  972. /* Allocate additional continuation packets? */
  973. if (avail_dsds == 0) {
  974. avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
  975. QLA_DSDS_PER_IOCB : used_dsds;
  976. dsd_list_len = (avail_dsds + 1) * 12;
  977. used_dsds -= avail_dsds;
  978. /* allocate tracking DS */
  979. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  980. if (!dsd_ptr)
  981. return 1;
  982. /* allocate new list */
  983. dsd_ptr->dsd_addr = next_dsd =
  984. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
  985. &dsd_ptr->dsd_list_dma);
  986. if (!next_dsd) {
  987. /*
  988. * Need to cleanup only this dsd_ptr, rest
  989. * will be done by sp_free_dma()
  990. */
  991. kfree(dsd_ptr);
  992. return 1;
  993. }
  994. list_add_tail(&dsd_ptr->list,
  995. &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
  996. sp->flags |= SRB_CRC_CTX_DSD_VALID;
  997. /* add new list to cmd iocb or last list */
  998. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  999. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  1000. *cur_dsd++ = dsd_list_len;
  1001. cur_dsd = (uint32_t *)next_dsd;
  1002. }
  1003. sle_dma = sg_dma_address(sg);
  1004. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1005. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1006. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1007. avail_dsds--;
  1008. }
  1009. /* Null termination */
  1010. *cur_dsd++ = 0;
  1011. *cur_dsd++ = 0;
  1012. *cur_dsd++ = 0;
  1013. return 0;
  1014. }
  1015. /**
  1016. * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
  1017. * Type 6 IOCB types.
  1018. *
  1019. * @sp: SRB command to process
  1020. * @cmd_pkt: Command type 3 IOCB
  1021. * @tot_dsds: Total number of segments to transfer
  1022. */
  1023. static inline int
  1024. qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
  1025. uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
  1026. {
  1027. uint32_t *cur_dsd, *fcp_dl;
  1028. scsi_qla_host_t *vha;
  1029. struct scsi_cmnd *cmd;
  1030. struct scatterlist *cur_seg;
  1031. int sgc;
  1032. uint32_t total_bytes = 0;
  1033. uint32_t data_bytes;
  1034. uint32_t dif_bytes;
  1035. uint8_t bundling = 1;
  1036. uint16_t blk_size;
  1037. uint8_t *clr_ptr;
  1038. struct crc_context *crc_ctx_pkt = NULL;
  1039. struct qla_hw_data *ha;
  1040. uint8_t additional_fcpcdb_len;
  1041. uint16_t fcp_cmnd_len;
  1042. struct fcp_cmnd *fcp_cmnd;
  1043. dma_addr_t crc_ctx_dma;
  1044. char tag[2];
  1045. cmd = GET_CMD_SP(sp);
  1046. sgc = 0;
  1047. /* Update entry type to indicate Command Type CRC_2 IOCB */
  1048. *((uint32_t *)(&cmd_pkt->entry_type)) =
  1049. __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
  1050. vha = sp->fcport->vha;
  1051. ha = vha->hw;
  1052. /* No data transfer */
  1053. data_bytes = scsi_bufflen(cmd);
  1054. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  1055. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  1056. return QLA_SUCCESS;
  1057. }
  1058. cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
  1059. /* Set transfer direction */
  1060. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1061. cmd_pkt->control_flags =
  1062. __constant_cpu_to_le16(CF_WRITE_DATA);
  1063. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1064. cmd_pkt->control_flags =
  1065. __constant_cpu_to_le16(CF_READ_DATA);
  1066. }
  1067. if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
  1068. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
  1069. (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
  1070. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
  1071. bundling = 0;
  1072. /* Allocate CRC context from global pool */
  1073. crc_ctx_pkt = sp->u.scmd.ctx =
  1074. dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
  1075. if (!crc_ctx_pkt)
  1076. goto crc_queuing_error;
  1077. /* Zero out CTX area. */
  1078. clr_ptr = (uint8_t *)crc_ctx_pkt;
  1079. memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
  1080. crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
  1081. sp->flags |= SRB_CRC_CTX_DMA_VALID;
  1082. /* Set handle */
  1083. crc_ctx_pkt->handle = cmd_pkt->handle;
  1084. INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
  1085. qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
  1086. &crc_ctx_pkt->ref_tag, tot_prot_dsds);
  1087. cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
  1088. cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
  1089. cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
  1090. /* Determine SCSI command length -- align to 4 byte boundary */
  1091. if (cmd->cmd_len > 16) {
  1092. additional_fcpcdb_len = cmd->cmd_len - 16;
  1093. if ((cmd->cmd_len % 4) != 0) {
  1094. /* SCSI cmd > 16 bytes must be multiple of 4 */
  1095. goto crc_queuing_error;
  1096. }
  1097. fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  1098. } else {
  1099. additional_fcpcdb_len = 0;
  1100. fcp_cmnd_len = 12 + 16 + 4;
  1101. }
  1102. fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
  1103. fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
  1104. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1105. fcp_cmnd->additional_cdb_len |= 1;
  1106. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  1107. fcp_cmnd->additional_cdb_len |= 2;
  1108. int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
  1109. memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  1110. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
  1111. cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
  1112. LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  1113. cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
  1114. MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
  1115. fcp_cmnd->task_management = 0;
  1116. /*
  1117. * Update tagged queuing modifier if using command tag queuing
  1118. */
  1119. if (scsi_populate_tag_msg(cmd, tag)) {
  1120. switch (tag[0]) {
  1121. case HEAD_OF_QUEUE_TAG:
  1122. fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
  1123. break;
  1124. case ORDERED_QUEUE_TAG:
  1125. fcp_cmnd->task_attribute = TSK_ORDERED;
  1126. break;
  1127. default:
  1128. fcp_cmnd->task_attribute = 0;
  1129. break;
  1130. }
  1131. } else {
  1132. fcp_cmnd->task_attribute = 0;
  1133. }
  1134. cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
  1135. /* Compute dif len and adjust data len to incude protection */
  1136. dif_bytes = 0;
  1137. blk_size = cmd->device->sector_size;
  1138. dif_bytes = (data_bytes / blk_size) * 8;
  1139. switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
  1140. case SCSI_PROT_READ_INSERT:
  1141. case SCSI_PROT_WRITE_STRIP:
  1142. total_bytes = data_bytes;
  1143. data_bytes += dif_bytes;
  1144. break;
  1145. case SCSI_PROT_READ_STRIP:
  1146. case SCSI_PROT_WRITE_INSERT:
  1147. case SCSI_PROT_READ_PASS:
  1148. case SCSI_PROT_WRITE_PASS:
  1149. total_bytes = data_bytes + dif_bytes;
  1150. break;
  1151. default:
  1152. BUG();
  1153. }
  1154. if (!qla2x00_hba_err_chk_enabled(sp))
  1155. fw_prot_opts |= 0x10; /* Disable Guard tag checking */
  1156. /* HBA error checking enabled */
  1157. else if (IS_PI_UNINIT_CAPABLE(ha)) {
  1158. if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
  1159. || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
  1160. SCSI_PROT_DIF_TYPE2))
  1161. fw_prot_opts |= BIT_10;
  1162. else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
  1163. SCSI_PROT_DIF_TYPE3)
  1164. fw_prot_opts |= BIT_11;
  1165. }
  1166. if (!bundling) {
  1167. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
  1168. } else {
  1169. /*
  1170. * Configure Bundling if we need to fetch interlaving
  1171. * protection PCI accesses
  1172. */
  1173. fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  1174. crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  1175. crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
  1176. tot_prot_dsds);
  1177. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
  1178. }
  1179. /* Finish the common fields of CRC pkt */
  1180. crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
  1181. crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
  1182. crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
  1183. crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
  1184. /* Fibre channel byte count */
  1185. cmd_pkt->byte_count = cpu_to_le32(total_bytes);
  1186. fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
  1187. additional_fcpcdb_len);
  1188. *fcp_dl = htonl(total_bytes);
  1189. if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  1190. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  1191. return QLA_SUCCESS;
  1192. }
  1193. /* Walks data segments */
  1194. cmd_pkt->control_flags |=
  1195. __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
  1196. if (!bundling && tot_prot_dsds) {
  1197. if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
  1198. cur_dsd, tot_dsds))
  1199. goto crc_queuing_error;
  1200. } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
  1201. (tot_dsds - tot_prot_dsds)))
  1202. goto crc_queuing_error;
  1203. if (bundling && tot_prot_dsds) {
  1204. /* Walks dif segments */
  1205. cur_seg = scsi_prot_sglist(cmd);
  1206. cmd_pkt->control_flags |=
  1207. __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  1208. cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
  1209. if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
  1210. tot_prot_dsds))
  1211. goto crc_queuing_error;
  1212. }
  1213. return QLA_SUCCESS;
  1214. crc_queuing_error:
  1215. /* Cleanup will be performed by the caller */
  1216. return QLA_FUNCTION_FAILED;
  1217. }
  1218. /**
  1219. * qla24xx_start_scsi() - Send a SCSI command to the ISP
  1220. * @sp: command to send to the ISP
  1221. *
  1222. * Returns non-zero if a failure occurred, else zero.
  1223. */
  1224. int
  1225. qla24xx_start_scsi(srb_t *sp)
  1226. {
  1227. int ret, nseg;
  1228. unsigned long flags;
  1229. uint32_t *clr_ptr;
  1230. uint32_t index;
  1231. uint32_t handle;
  1232. struct cmd_type_7 *cmd_pkt;
  1233. uint16_t cnt;
  1234. uint16_t req_cnt;
  1235. uint16_t tot_dsds;
  1236. struct req_que *req = NULL;
  1237. struct rsp_que *rsp = NULL;
  1238. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  1239. struct scsi_qla_host *vha = sp->fcport->vha;
  1240. struct qla_hw_data *ha = vha->hw;
  1241. char tag[2];
  1242. /* Setup device pointers. */
  1243. ret = 0;
  1244. qla25xx_set_que(sp, &rsp);
  1245. req = vha->req;
  1246. /* So we know we haven't pci_map'ed anything yet */
  1247. tot_dsds = 0;
  1248. /* Send marker if required */
  1249. if (vha->marker_needed != 0) {
  1250. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1251. QLA_SUCCESS)
  1252. return QLA_FUNCTION_FAILED;
  1253. vha->marker_needed = 0;
  1254. }
  1255. /* Acquire ring specific lock */
  1256. spin_lock_irqsave(&ha->hardware_lock, flags);
  1257. /* Check for room in outstanding command list. */
  1258. handle = req->current_outstanding_cmd;
  1259. for (index = 1; index < req->num_outstanding_cmds; index++) {
  1260. handle++;
  1261. if (handle == req->num_outstanding_cmds)
  1262. handle = 1;
  1263. if (!req->outstanding_cmds[handle])
  1264. break;
  1265. }
  1266. if (index == req->num_outstanding_cmds)
  1267. goto queuing_error;
  1268. /* Map the sg table so we have an accurate count of sg entries needed */
  1269. if (scsi_sg_count(cmd)) {
  1270. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1271. scsi_sg_count(cmd), cmd->sc_data_direction);
  1272. if (unlikely(!nseg))
  1273. goto queuing_error;
  1274. } else
  1275. nseg = 0;
  1276. tot_dsds = nseg;
  1277. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  1278. if (req->cnt < (req_cnt + 2)) {
  1279. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1280. if (req->ring_index < cnt)
  1281. req->cnt = cnt - req->ring_index;
  1282. else
  1283. req->cnt = req->length -
  1284. (req->ring_index - cnt);
  1285. if (req->cnt < (req_cnt + 2))
  1286. goto queuing_error;
  1287. }
  1288. /* Build command packet. */
  1289. req->current_outstanding_cmd = handle;
  1290. req->outstanding_cmds[handle] = sp;
  1291. sp->handle = handle;
  1292. cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1293. req->cnt -= req_cnt;
  1294. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  1295. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1296. /* Zero out remaining portion of packet. */
  1297. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1298. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1299. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1300. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1301. /* Set NPORT-ID and LUN number*/
  1302. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1303. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1304. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1305. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1306. cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
  1307. int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
  1308. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1309. /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
  1310. if (scsi_populate_tag_msg(cmd, tag)) {
  1311. switch (tag[0]) {
  1312. case HEAD_OF_QUEUE_TAG:
  1313. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  1314. break;
  1315. case ORDERED_QUEUE_TAG:
  1316. cmd_pkt->task = TSK_ORDERED;
  1317. break;
  1318. }
  1319. }
  1320. /* Load SCSI command packet. */
  1321. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  1322. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  1323. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  1324. /* Build IOCB segments */
  1325. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  1326. /* Set total data segment count. */
  1327. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1328. /* Specify response queue number where completion should happen */
  1329. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1330. wmb();
  1331. /* Adjust ring index. */
  1332. req->ring_index++;
  1333. if (req->ring_index == req->length) {
  1334. req->ring_index = 0;
  1335. req->ring_ptr = req->ring;
  1336. } else
  1337. req->ring_ptr++;
  1338. sp->flags |= SRB_DMA_VALID;
  1339. /* Set chip new ring index. */
  1340. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1341. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1342. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1343. if (vha->flags.process_response_queue &&
  1344. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1345. qla24xx_process_response_queue(vha, rsp);
  1346. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1347. return QLA_SUCCESS;
  1348. queuing_error:
  1349. if (tot_dsds)
  1350. scsi_dma_unmap(cmd);
  1351. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1352. return QLA_FUNCTION_FAILED;
  1353. }
  1354. /**
  1355. * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
  1356. * @sp: command to send to the ISP
  1357. *
  1358. * Returns non-zero if a failure occurred, else zero.
  1359. */
  1360. int
  1361. qla24xx_dif_start_scsi(srb_t *sp)
  1362. {
  1363. int nseg;
  1364. unsigned long flags;
  1365. uint32_t *clr_ptr;
  1366. uint32_t index;
  1367. uint32_t handle;
  1368. uint16_t cnt;
  1369. uint16_t req_cnt = 0;
  1370. uint16_t tot_dsds;
  1371. uint16_t tot_prot_dsds;
  1372. uint16_t fw_prot_opts = 0;
  1373. struct req_que *req = NULL;
  1374. struct rsp_que *rsp = NULL;
  1375. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  1376. struct scsi_qla_host *vha = sp->fcport->vha;
  1377. struct qla_hw_data *ha = vha->hw;
  1378. struct cmd_type_crc_2 *cmd_pkt;
  1379. uint32_t status = 0;
  1380. #define QDSS_GOT_Q_SPACE BIT_0
  1381. /* Only process protection or >16 cdb in this routine */
  1382. if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  1383. if (cmd->cmd_len <= 16)
  1384. return qla24xx_start_scsi(sp);
  1385. }
  1386. /* Setup device pointers. */
  1387. qla25xx_set_que(sp, &rsp);
  1388. req = vha->req;
  1389. /* So we know we haven't pci_map'ed anything yet */
  1390. tot_dsds = 0;
  1391. /* Send marker if required */
  1392. if (vha->marker_needed != 0) {
  1393. if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
  1394. QLA_SUCCESS)
  1395. return QLA_FUNCTION_FAILED;
  1396. vha->marker_needed = 0;
  1397. }
  1398. /* Acquire ring specific lock */
  1399. spin_lock_irqsave(&ha->hardware_lock, flags);
  1400. /* Check for room in outstanding command list. */
  1401. handle = req->current_outstanding_cmd;
  1402. for (index = 1; index < req->num_outstanding_cmds; index++) {
  1403. handle++;
  1404. if (handle == req->num_outstanding_cmds)
  1405. handle = 1;
  1406. if (!req->outstanding_cmds[handle])
  1407. break;
  1408. }
  1409. if (index == req->num_outstanding_cmds)
  1410. goto queuing_error;
  1411. /* Compute number of required data segments */
  1412. /* Map the sg table so we have an accurate count of sg entries needed */
  1413. if (scsi_sg_count(cmd)) {
  1414. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1415. scsi_sg_count(cmd), cmd->sc_data_direction);
  1416. if (unlikely(!nseg))
  1417. goto queuing_error;
  1418. else
  1419. sp->flags |= SRB_DMA_VALID;
  1420. if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
  1421. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
  1422. struct qla2_sgx sgx;
  1423. uint32_t partial;
  1424. memset(&sgx, 0, sizeof(struct qla2_sgx));
  1425. sgx.tot_bytes = scsi_bufflen(cmd);
  1426. sgx.cur_sg = scsi_sglist(cmd);
  1427. sgx.sp = sp;
  1428. nseg = 0;
  1429. while (qla24xx_get_one_block_sg(
  1430. cmd->device->sector_size, &sgx, &partial))
  1431. nseg++;
  1432. }
  1433. } else
  1434. nseg = 0;
  1435. /* number of required data segments */
  1436. tot_dsds = nseg;
  1437. /* Compute number of required protection segments */
  1438. if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  1439. nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
  1440. scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  1441. if (unlikely(!nseg))
  1442. goto queuing_error;
  1443. else
  1444. sp->flags |= SRB_CRC_PROT_DMA_VALID;
  1445. if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
  1446. (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
  1447. nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
  1448. }
  1449. } else {
  1450. nseg = 0;
  1451. }
  1452. req_cnt = 1;
  1453. /* Total Data and protection sg segment(s) */
  1454. tot_prot_dsds = nseg;
  1455. tot_dsds += nseg;
  1456. if (req->cnt < (req_cnt + 2)) {
  1457. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  1458. if (req->ring_index < cnt)
  1459. req->cnt = cnt - req->ring_index;
  1460. else
  1461. req->cnt = req->length -
  1462. (req->ring_index - cnt);
  1463. if (req->cnt < (req_cnt + 2))
  1464. goto queuing_error;
  1465. }
  1466. status |= QDSS_GOT_Q_SPACE;
  1467. /* Build header part of command packet (excluding the OPCODE). */
  1468. req->current_outstanding_cmd = handle;
  1469. req->outstanding_cmds[handle] = sp;
  1470. sp->handle = handle;
  1471. cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  1472. req->cnt -= req_cnt;
  1473. /* Fill-in common area */
  1474. cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
  1475. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  1476. clr_ptr = (uint32_t *)cmd_pkt + 2;
  1477. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  1478. /* Set NPORT-ID and LUN number*/
  1479. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1480. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  1481. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  1482. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  1483. int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
  1484. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  1485. /* Total Data and protection segment(s) */
  1486. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  1487. /* Build IOCB segments and adjust for data protection segments */
  1488. if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
  1489. req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  1490. QLA_SUCCESS)
  1491. goto queuing_error;
  1492. cmd_pkt->entry_count = (uint8_t)req_cnt;
  1493. /* Specify response queue number where completion should happen */
  1494. cmd_pkt->entry_status = (uint8_t) rsp->id;
  1495. cmd_pkt->timeout = __constant_cpu_to_le16(0);
  1496. wmb();
  1497. /* Adjust ring index. */
  1498. req->ring_index++;
  1499. if (req->ring_index == req->length) {
  1500. req->ring_index = 0;
  1501. req->ring_ptr = req->ring;
  1502. } else
  1503. req->ring_ptr++;
  1504. /* Set chip new ring index. */
  1505. WRT_REG_DWORD(req->req_q_in, req->ring_index);
  1506. RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
  1507. /* Manage unprocessed RIO/ZIO commands in response queue. */
  1508. if (vha->flags.process_response_queue &&
  1509. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  1510. qla24xx_process_response_queue(vha, rsp);
  1511. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1512. return QLA_SUCCESS;
  1513. queuing_error:
  1514. if (status & QDSS_GOT_Q_SPACE) {
  1515. req->outstanding_cmds[handle] = NULL;
  1516. req->cnt += req_cnt;
  1517. }
  1518. /* Cleanup will be performed by the caller (queuecommand) */
  1519. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1520. return QLA_FUNCTION_FAILED;
  1521. }
  1522. static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
  1523. {
  1524. struct scsi_cmnd *cmd = GET_CMD_SP(sp);
  1525. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1526. int affinity = cmd->request->cpu;
  1527. if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
  1528. affinity < ha->max_rsp_queues - 1)
  1529. *rsp = ha->rsp_q_map[affinity + 1];
  1530. else
  1531. *rsp = ha->rsp_q_map[0];
  1532. }
  1533. /* Generic Control-SRB manipulation functions. */
  1534. void *
  1535. qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
  1536. {
  1537. struct qla_hw_data *ha = vha->hw;
  1538. struct req_que *req = ha->req_q_map[0];
  1539. device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
  1540. uint32_t index, handle;
  1541. request_t *pkt;
  1542. uint16_t cnt, req_cnt;
  1543. pkt = NULL;
  1544. req_cnt = 1;
  1545. handle = 0;
  1546. if (!sp)
  1547. goto skip_cmd_array;
  1548. /* Check for room in outstanding command list. */
  1549. handle = req->current_outstanding_cmd;
  1550. for (index = 1; req->num_outstanding_cmds; index++) {
  1551. handle++;
  1552. if (handle == req->num_outstanding_cmds)
  1553. handle = 1;
  1554. if (!req->outstanding_cmds[handle])
  1555. break;
  1556. }
  1557. if (index == req->num_outstanding_cmds) {
  1558. ql_log(ql_log_warn, vha, 0x700b,
  1559. "No room on outstanding cmd array.\n");
  1560. goto queuing_error;
  1561. }
  1562. /* Prep command array. */
  1563. req->current_outstanding_cmd = handle;
  1564. req->outstanding_cmds[handle] = sp;
  1565. sp->handle = handle;
  1566. /* Adjust entry-counts as needed. */
  1567. if (sp->type != SRB_SCSI_CMD)
  1568. req_cnt = sp->iocbs;
  1569. skip_cmd_array:
  1570. /* Check for room on request queue. */
  1571. if (req->cnt < req_cnt) {
  1572. if (ha->mqenable || IS_QLA83XX(ha))
  1573. cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
  1574. else if (IS_QLA82XX(ha))
  1575. cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
  1576. else if (IS_FWI2_CAPABLE(ha))
  1577. cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
  1578. else
  1579. cnt = qla2x00_debounce_register(
  1580. ISP_REQ_Q_OUT(ha, &reg->isp));
  1581. if (req->ring_index < cnt)
  1582. req->cnt = cnt - req->ring_index;
  1583. else
  1584. req->cnt = req->length -
  1585. (req->ring_index - cnt);
  1586. }
  1587. if (req->cnt < req_cnt)
  1588. goto queuing_error;
  1589. /* Prep packet */
  1590. req->cnt -= req_cnt;
  1591. pkt = req->ring_ptr;
  1592. memset(pkt, 0, REQUEST_ENTRY_SIZE);
  1593. pkt->entry_count = req_cnt;
  1594. pkt->handle = handle;
  1595. queuing_error:
  1596. return pkt;
  1597. }
  1598. static void
  1599. qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1600. {
  1601. struct srb_iocb *lio = &sp->u.iocb_cmd;
  1602. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1603. logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  1604. if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
  1605. logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  1606. if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
  1607. logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  1608. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1609. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1610. logio->port_id[1] = sp->fcport->d_id.b.area;
  1611. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1612. logio->vp_index = sp->fcport->vha->vp_idx;
  1613. }
  1614. static void
  1615. qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
  1616. {
  1617. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1618. struct srb_iocb *lio = &sp->u.iocb_cmd;
  1619. uint16_t opts;
  1620. mbx->entry_type = MBX_IOCB_TYPE;
  1621. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1622. mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
  1623. opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
  1624. opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
  1625. if (HAS_EXTENDED_IDS(ha)) {
  1626. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1627. mbx->mb10 = cpu_to_le16(opts);
  1628. } else {
  1629. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
  1630. }
  1631. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1632. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1633. sp->fcport->d_id.b.al_pa);
  1634. mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
  1635. }
  1636. static void
  1637. qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1638. {
  1639. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1640. logio->control_flags =
  1641. cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
  1642. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1643. logio->port_id[0] = sp->fcport->d_id.b.al_pa;
  1644. logio->port_id[1] = sp->fcport->d_id.b.area;
  1645. logio->port_id[2] = sp->fcport->d_id.b.domain;
  1646. logio->vp_index = sp->fcport->vha->vp_idx;
  1647. }
  1648. static void
  1649. qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
  1650. {
  1651. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1652. mbx->entry_type = MBX_IOCB_TYPE;
  1653. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1654. mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
  1655. mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
  1656. cpu_to_le16(sp->fcport->loop_id):
  1657. cpu_to_le16(sp->fcport->loop_id << 8);
  1658. mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
  1659. mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
  1660. sp->fcport->d_id.b.al_pa);
  1661. mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
  1662. /* Implicit: mbx->mbx10 = 0. */
  1663. }
  1664. static void
  1665. qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
  1666. {
  1667. logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
  1668. logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
  1669. logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1670. logio->vp_index = sp->fcport->vha->vp_idx;
  1671. }
  1672. static void
  1673. qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
  1674. {
  1675. struct qla_hw_data *ha = sp->fcport->vha->hw;
  1676. mbx->entry_type = MBX_IOCB_TYPE;
  1677. SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
  1678. mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
  1679. if (HAS_EXTENDED_IDS(ha)) {
  1680. mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  1681. mbx->mb10 = cpu_to_le16(BIT_0);
  1682. } else {
  1683. mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
  1684. }
  1685. mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
  1686. mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
  1687. mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
  1688. mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
  1689. mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
  1690. }
  1691. static void
  1692. qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
  1693. {
  1694. uint32_t flags;
  1695. unsigned int lun;
  1696. struct fc_port *fcport = sp->fcport;
  1697. scsi_qla_host_t *vha = fcport->vha;
  1698. struct qla_hw_data *ha = vha->hw;
  1699. struct srb_iocb *iocb = &sp->u.iocb_cmd;
  1700. struct req_que *req = vha->req;
  1701. flags = iocb->u.tmf.flags;
  1702. lun = iocb->u.tmf.lun;
  1703. tsk->entry_type = TSK_MGMT_IOCB_TYPE;
  1704. tsk->entry_count = 1;
  1705. tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
  1706. tsk->nport_handle = cpu_to_le16(fcport->loop_id);
  1707. tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
  1708. tsk->control_flags = cpu_to_le32(flags);
  1709. tsk->port_id[0] = fcport->d_id.b.al_pa;
  1710. tsk->port_id[1] = fcport->d_id.b.area;
  1711. tsk->port_id[2] = fcport->d_id.b.domain;
  1712. tsk->vp_index = fcport->vha->vp_idx;
  1713. if (flags == TCF_LUN_RESET) {
  1714. int_to_scsilun(lun, &tsk->lun);
  1715. host_to_fcp_swap((uint8_t *)&tsk->lun,
  1716. sizeof(tsk->lun));
  1717. }
  1718. }
  1719. static void
  1720. qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
  1721. {
  1722. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  1723. els_iocb->entry_type = ELS_IOCB_TYPE;
  1724. els_iocb->entry_count = 1;
  1725. els_iocb->sys_define = 0;
  1726. els_iocb->entry_status = 0;
  1727. els_iocb->handle = sp->handle;
  1728. els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1729. els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1730. els_iocb->vp_index = sp->fcport->vha->vp_idx;
  1731. els_iocb->sof_type = EST_SOFI3;
  1732. els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1733. els_iocb->opcode =
  1734. sp->type == SRB_ELS_CMD_RPT ?
  1735. bsg_job->request->rqst_data.r_els.els_code :
  1736. bsg_job->request->rqst_data.h_els.command_code;
  1737. els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
  1738. els_iocb->port_id[1] = sp->fcport->d_id.b.area;
  1739. els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
  1740. els_iocb->control_flags = 0;
  1741. els_iocb->rx_byte_count =
  1742. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1743. els_iocb->tx_byte_count =
  1744. cpu_to_le32(bsg_job->request_payload.payload_len);
  1745. els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1746. (bsg_job->request_payload.sg_list)));
  1747. els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1748. (bsg_job->request_payload.sg_list)));
  1749. els_iocb->tx_len = cpu_to_le32(sg_dma_len
  1750. (bsg_job->request_payload.sg_list));
  1751. els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
  1752. (bsg_job->reply_payload.sg_list)));
  1753. els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
  1754. (bsg_job->reply_payload.sg_list)));
  1755. els_iocb->rx_len = cpu_to_le32(sg_dma_len
  1756. (bsg_job->reply_payload.sg_list));
  1757. }
  1758. static void
  1759. qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
  1760. {
  1761. uint16_t avail_dsds;
  1762. uint32_t *cur_dsd;
  1763. struct scatterlist *sg;
  1764. int index;
  1765. uint16_t tot_dsds;
  1766. scsi_qla_host_t *vha = sp->fcport->vha;
  1767. struct qla_hw_data *ha = vha->hw;
  1768. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  1769. int loop_iterartion = 0;
  1770. int cont_iocb_prsnt = 0;
  1771. int entry_count = 1;
  1772. memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
  1773. ct_iocb->entry_type = CT_IOCB_TYPE;
  1774. ct_iocb->entry_status = 0;
  1775. ct_iocb->handle1 = sp->handle;
  1776. SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
  1777. ct_iocb->status = __constant_cpu_to_le16(0);
  1778. ct_iocb->control_flags = __constant_cpu_to_le16(0);
  1779. ct_iocb->timeout = 0;
  1780. ct_iocb->cmd_dsd_count =
  1781. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1782. ct_iocb->total_dsd_count =
  1783. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
  1784. ct_iocb->req_bytecount =
  1785. cpu_to_le32(bsg_job->request_payload.payload_len);
  1786. ct_iocb->rsp_bytecount =
  1787. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1788. ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
  1789. (bsg_job->request_payload.sg_list)));
  1790. ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
  1791. (bsg_job->request_payload.sg_list)));
  1792. ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
  1793. ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
  1794. (bsg_job->reply_payload.sg_list)));
  1795. ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
  1796. (bsg_job->reply_payload.sg_list)));
  1797. ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
  1798. avail_dsds = 1;
  1799. cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
  1800. index = 0;
  1801. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1802. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1803. dma_addr_t sle_dma;
  1804. cont_a64_entry_t *cont_pkt;
  1805. /* Allocate additional continuation packets? */
  1806. if (avail_dsds == 0) {
  1807. /*
  1808. * Five DSDs are available in the Cont.
  1809. * Type 1 IOCB.
  1810. */
  1811. cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
  1812. vha->hw->req_q_map[0]);
  1813. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1814. avail_dsds = 5;
  1815. cont_iocb_prsnt = 1;
  1816. entry_count++;
  1817. }
  1818. sle_dma = sg_dma_address(sg);
  1819. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1820. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1821. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1822. loop_iterartion++;
  1823. avail_dsds--;
  1824. }
  1825. ct_iocb->entry_count = entry_count;
  1826. }
  1827. static void
  1828. qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
  1829. {
  1830. uint16_t avail_dsds;
  1831. uint32_t *cur_dsd;
  1832. struct scatterlist *sg;
  1833. int index;
  1834. uint16_t tot_dsds;
  1835. scsi_qla_host_t *vha = sp->fcport->vha;
  1836. struct qla_hw_data *ha = vha->hw;
  1837. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  1838. int loop_iterartion = 0;
  1839. int cont_iocb_prsnt = 0;
  1840. int entry_count = 1;
  1841. ct_iocb->entry_type = CT_IOCB_TYPE;
  1842. ct_iocb->entry_status = 0;
  1843. ct_iocb->sys_define = 0;
  1844. ct_iocb->handle = sp->handle;
  1845. ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  1846. ct_iocb->vp_index = sp->fcport->vha->vp_idx;
  1847. ct_iocb->comp_status = __constant_cpu_to_le16(0);
  1848. ct_iocb->cmd_dsd_count =
  1849. __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
  1850. ct_iocb->timeout = 0;
  1851. ct_iocb->rsp_dsd_count =
  1852. __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  1853. ct_iocb->rsp_byte_count =
  1854. cpu_to_le32(bsg_job->reply_payload.payload_len);
  1855. ct_iocb->cmd_byte_count =
  1856. cpu_to_le32(bsg_job->request_payload.payload_len);
  1857. ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
  1858. (bsg_job->request_payload.sg_list)));
  1859. ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
  1860. (bsg_job->request_payload.sg_list)));
  1861. ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
  1862. (bsg_job->request_payload.sg_list));
  1863. avail_dsds = 1;
  1864. cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
  1865. index = 0;
  1866. tot_dsds = bsg_job->reply_payload.sg_cnt;
  1867. for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
  1868. dma_addr_t sle_dma;
  1869. cont_a64_entry_t *cont_pkt;
  1870. /* Allocate additional continuation packets? */
  1871. if (avail_dsds == 0) {
  1872. /*
  1873. * Five DSDs are available in the Cont.
  1874. * Type 1 IOCB.
  1875. */
  1876. cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
  1877. ha->req_q_map[0]);
  1878. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  1879. avail_dsds = 5;
  1880. cont_iocb_prsnt = 1;
  1881. entry_count++;
  1882. }
  1883. sle_dma = sg_dma_address(sg);
  1884. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  1885. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  1886. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  1887. loop_iterartion++;
  1888. avail_dsds--;
  1889. }
  1890. ct_iocb->entry_count = entry_count;
  1891. }
  1892. /*
  1893. * qla82xx_start_scsi() - Send a SCSI command to the ISP
  1894. * @sp: command to send to the ISP
  1895. *
  1896. * Returns non-zero if a failure occurred, else zero.
  1897. */
  1898. int
  1899. qla82xx_start_scsi(srb_t *sp)
  1900. {
  1901. int ret, nseg;
  1902. unsigned long flags;
  1903. struct scsi_cmnd *cmd;
  1904. uint32_t *clr_ptr;
  1905. uint32_t index;
  1906. uint32_t handle;
  1907. uint16_t cnt;
  1908. uint16_t req_cnt;
  1909. uint16_t tot_dsds;
  1910. struct device_reg_82xx __iomem *reg;
  1911. uint32_t dbval;
  1912. uint32_t *fcp_dl;
  1913. uint8_t additional_cdb_len;
  1914. struct ct6_dsd *ctx;
  1915. struct scsi_qla_host *vha = sp->fcport->vha;
  1916. struct qla_hw_data *ha = vha->hw;
  1917. struct req_que *req = NULL;
  1918. struct rsp_que *rsp = NULL;
  1919. char tag[2];
  1920. /* Setup device pointers. */
  1921. ret = 0;
  1922. reg = &ha->iobase->isp82;
  1923. cmd = GET_CMD_SP(sp);
  1924. req = vha->req;
  1925. rsp = ha->rsp_q_map[0];
  1926. /* So we know we haven't pci_map'ed anything yet */
  1927. tot_dsds = 0;
  1928. dbval = 0x04 | (ha->portnum << 5);
  1929. /* Send marker if required */
  1930. if (vha->marker_needed != 0) {
  1931. if (qla2x00_marker(vha, req,
  1932. rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  1933. ql_log(ql_log_warn, vha, 0x300c,
  1934. "qla2x00_marker failed for cmd=%p.\n", cmd);
  1935. return QLA_FUNCTION_FAILED;
  1936. }
  1937. vha->marker_needed = 0;
  1938. }
  1939. /* Acquire ring specific lock */
  1940. spin_lock_irqsave(&ha->hardware_lock, flags);
  1941. /* Check for room in outstanding command list. */
  1942. handle = req->current_outstanding_cmd;
  1943. for (index = 1; index < req->num_outstanding_cmds; index++) {
  1944. handle++;
  1945. if (handle == req->num_outstanding_cmds)
  1946. handle = 1;
  1947. if (!req->outstanding_cmds[handle])
  1948. break;
  1949. }
  1950. if (index == req->num_outstanding_cmds)
  1951. goto queuing_error;
  1952. /* Map the sg table so we have an accurate count of sg entries needed */
  1953. if (scsi_sg_count(cmd)) {
  1954. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  1955. scsi_sg_count(cmd), cmd->sc_data_direction);
  1956. if (unlikely(!nseg))
  1957. goto queuing_error;
  1958. } else
  1959. nseg = 0;
  1960. tot_dsds = nseg;
  1961. if (tot_dsds > ql2xshiftctondsd) {
  1962. struct cmd_type_6 *cmd_pkt;
  1963. uint16_t more_dsd_lists = 0;
  1964. struct dsd_dma *dsd_ptr;
  1965. uint16_t i;
  1966. more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
  1967. if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
  1968. ql_dbg(ql_dbg_io, vha, 0x300d,
  1969. "Num of DSD list %d is than %d for cmd=%p.\n",
  1970. more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
  1971. cmd);
  1972. goto queuing_error;
  1973. }
  1974. if (more_dsd_lists <= ha->gbl_dsd_avail)
  1975. goto sufficient_dsds;
  1976. else
  1977. more_dsd_lists -= ha->gbl_dsd_avail;
  1978. for (i = 0; i < more_dsd_lists; i++) {
  1979. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  1980. if (!dsd_ptr) {
  1981. ql_log(ql_log_fatal, vha, 0x300e,
  1982. "Failed to allocate memory for dsd_dma "
  1983. "for cmd=%p.\n", cmd);
  1984. goto queuing_error;
  1985. }
  1986. dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
  1987. GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
  1988. if (!dsd_ptr->dsd_addr) {
  1989. kfree(dsd_ptr);
  1990. ql_log(ql_log_fatal, vha, 0x300f,
  1991. "Failed to allocate memory for dsd_addr "
  1992. "for cmd=%p.\n", cmd);
  1993. goto queuing_error;
  1994. }
  1995. list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
  1996. ha->gbl_dsd_avail++;
  1997. }
  1998. sufficient_dsds:
  1999. req_cnt = 1;
  2000. if (req->cnt < (req_cnt + 2)) {
  2001. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2002. &reg->req_q_out[0]);
  2003. if (req->ring_index < cnt)
  2004. req->cnt = cnt - req->ring_index;
  2005. else
  2006. req->cnt = req->length -
  2007. (req->ring_index - cnt);
  2008. if (req->cnt < (req_cnt + 2))
  2009. goto queuing_error;
  2010. }
  2011. ctx = sp->u.scmd.ctx =
  2012. mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
  2013. if (!ctx) {
  2014. ql_log(ql_log_fatal, vha, 0x3010,
  2015. "Failed to allocate ctx for cmd=%p.\n", cmd);
  2016. goto queuing_error;
  2017. }
  2018. memset(ctx, 0, sizeof(struct ct6_dsd));
  2019. ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
  2020. GFP_ATOMIC, &ctx->fcp_cmnd_dma);
  2021. if (!ctx->fcp_cmnd) {
  2022. ql_log(ql_log_fatal, vha, 0x3011,
  2023. "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
  2024. goto queuing_error;
  2025. }
  2026. /* Initialize the DSD list and dma handle */
  2027. INIT_LIST_HEAD(&ctx->dsd_list);
  2028. ctx->dsd_use_cnt = 0;
  2029. if (cmd->cmd_len > 16) {
  2030. additional_cdb_len = cmd->cmd_len - 16;
  2031. if ((cmd->cmd_len % 4) != 0) {
  2032. /* SCSI command bigger than 16 bytes must be
  2033. * multiple of 4
  2034. */
  2035. ql_log(ql_log_warn, vha, 0x3012,
  2036. "scsi cmd len %d not multiple of 4 "
  2037. "for cmd=%p.\n", cmd->cmd_len, cmd);
  2038. goto queuing_error_fcp_cmnd;
  2039. }
  2040. ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  2041. } else {
  2042. additional_cdb_len = 0;
  2043. ctx->fcp_cmnd_len = 12 + 16 + 4;
  2044. }
  2045. cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
  2046. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2047. /* Zero out remaining portion of packet. */
  2048. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  2049. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2050. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2051. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2052. /* Set NPORT-ID and LUN number*/
  2053. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2054. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2055. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2056. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2057. cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
  2058. /* Build IOCB segments */
  2059. if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
  2060. goto queuing_error_fcp_cmnd;
  2061. int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
  2062. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  2063. /* build FCP_CMND IU */
  2064. memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2065. int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
  2066. ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
  2067. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  2068. ctx->fcp_cmnd->additional_cdb_len |= 1;
  2069. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  2070. ctx->fcp_cmnd->additional_cdb_len |= 2;
  2071. /*
  2072. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2073. */
  2074. if (scsi_populate_tag_msg(cmd, tag)) {
  2075. switch (tag[0]) {
  2076. case HEAD_OF_QUEUE_TAG:
  2077. ctx->fcp_cmnd->task_attribute =
  2078. TSK_HEAD_OF_QUEUE;
  2079. break;
  2080. case ORDERED_QUEUE_TAG:
  2081. ctx->fcp_cmnd->task_attribute =
  2082. TSK_ORDERED;
  2083. break;
  2084. }
  2085. }
  2086. /* Populate the FCP_PRIO. */
  2087. if (ha->flags.fcp_prio_enabled)
  2088. ctx->fcp_cmnd->task_attribute |=
  2089. sp->fcport->fcp_prio << 3;
  2090. memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  2091. fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
  2092. additional_cdb_len);
  2093. *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
  2094. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
  2095. cmd_pkt->fcp_cmnd_dseg_address[0] =
  2096. cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
  2097. cmd_pkt->fcp_cmnd_dseg_address[1] =
  2098. cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
  2099. sp->flags |= SRB_FCP_CMND_DMA_VALID;
  2100. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2101. /* Set total data segment count. */
  2102. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2103. /* Specify response queue number where
  2104. * completion should happen
  2105. */
  2106. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2107. } else {
  2108. struct cmd_type_7 *cmd_pkt;
  2109. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  2110. if (req->cnt < (req_cnt + 2)) {
  2111. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2112. &reg->req_q_out[0]);
  2113. if (req->ring_index < cnt)
  2114. req->cnt = cnt - req->ring_index;
  2115. else
  2116. req->cnt = req->length -
  2117. (req->ring_index - cnt);
  2118. }
  2119. if (req->cnt < (req_cnt + 2))
  2120. goto queuing_error;
  2121. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  2122. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2123. /* Zero out remaining portion of packet. */
  2124. /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
  2125. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2126. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2127. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2128. /* Set NPORT-ID and LUN number*/
  2129. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2130. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2131. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2132. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2133. cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
  2134. int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
  2135. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
  2136. sizeof(cmd_pkt->lun));
  2137. /*
  2138. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2139. */
  2140. if (scsi_populate_tag_msg(cmd, tag)) {
  2141. switch (tag[0]) {
  2142. case HEAD_OF_QUEUE_TAG:
  2143. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  2144. break;
  2145. case ORDERED_QUEUE_TAG:
  2146. cmd_pkt->task = TSK_ORDERED;
  2147. break;
  2148. }
  2149. }
  2150. /* Populate the FCP_PRIO. */
  2151. if (ha->flags.fcp_prio_enabled)
  2152. cmd_pkt->task |= sp->fcport->fcp_prio << 3;
  2153. /* Load SCSI command packet. */
  2154. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  2155. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  2156. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2157. /* Build IOCB segments */
  2158. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  2159. /* Set total data segment count. */
  2160. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2161. /* Specify response queue number where
  2162. * completion should happen.
  2163. */
  2164. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2165. }
  2166. /* Build command packet. */
  2167. req->current_outstanding_cmd = handle;
  2168. req->outstanding_cmds[handle] = sp;
  2169. sp->handle = handle;
  2170. cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  2171. req->cnt -= req_cnt;
  2172. wmb();
  2173. /* Adjust ring index. */
  2174. req->ring_index++;
  2175. if (req->ring_index == req->length) {
  2176. req->ring_index = 0;
  2177. req->ring_ptr = req->ring;
  2178. } else
  2179. req->ring_ptr++;
  2180. sp->flags |= SRB_DMA_VALID;
  2181. /* Set chip new ring index. */
  2182. /* write, read and verify logic */
  2183. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  2184. if (ql2xdbwr)
  2185. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  2186. else {
  2187. WRT_REG_DWORD(
  2188. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2189. dbval);
  2190. wmb();
  2191. while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
  2192. WRT_REG_DWORD(
  2193. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2194. dbval);
  2195. wmb();
  2196. }
  2197. }
  2198. /* Manage unprocessed RIO/ZIO commands in response queue. */
  2199. if (vha->flags.process_response_queue &&
  2200. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  2201. qla24xx_process_response_queue(vha, rsp);
  2202. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2203. return QLA_SUCCESS;
  2204. queuing_error_fcp_cmnd:
  2205. dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
  2206. queuing_error:
  2207. if (tot_dsds)
  2208. scsi_dma_unmap(cmd);
  2209. if (sp->u.scmd.ctx) {
  2210. mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
  2211. sp->u.scmd.ctx = NULL;
  2212. }
  2213. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2214. return QLA_FUNCTION_FAILED;
  2215. }
  2216. int
  2217. qla2x00_start_sp(srb_t *sp)
  2218. {
  2219. int rval;
  2220. struct qla_hw_data *ha = sp->fcport->vha->hw;
  2221. void *pkt;
  2222. unsigned long flags;
  2223. rval = QLA_FUNCTION_FAILED;
  2224. spin_lock_irqsave(&ha->hardware_lock, flags);
  2225. pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
  2226. if (!pkt) {
  2227. ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
  2228. "qla2x00_alloc_iocbs failed.\n");
  2229. goto done;
  2230. }
  2231. rval = QLA_SUCCESS;
  2232. switch (sp->type) {
  2233. case SRB_LOGIN_CMD:
  2234. IS_FWI2_CAPABLE(ha) ?
  2235. qla24xx_login_iocb(sp, pkt) :
  2236. qla2x00_login_iocb(sp, pkt);
  2237. break;
  2238. case SRB_LOGOUT_CMD:
  2239. IS_FWI2_CAPABLE(ha) ?
  2240. qla24xx_logout_iocb(sp, pkt) :
  2241. qla2x00_logout_iocb(sp, pkt);
  2242. break;
  2243. case SRB_ELS_CMD_RPT:
  2244. case SRB_ELS_CMD_HST:
  2245. qla24xx_els_iocb(sp, pkt);
  2246. break;
  2247. case SRB_CT_CMD:
  2248. IS_FWI2_CAPABLE(ha) ?
  2249. qla24xx_ct_iocb(sp, pkt) :
  2250. qla2x00_ct_iocb(sp, pkt);
  2251. break;
  2252. case SRB_ADISC_CMD:
  2253. IS_FWI2_CAPABLE(ha) ?
  2254. qla24xx_adisc_iocb(sp, pkt) :
  2255. qla2x00_adisc_iocb(sp, pkt);
  2256. break;
  2257. case SRB_TM_CMD:
  2258. qla24xx_tm_iocb(sp, pkt);
  2259. break;
  2260. default:
  2261. break;
  2262. }
  2263. wmb();
  2264. qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
  2265. done:
  2266. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2267. return rval;
  2268. }
  2269. static void
  2270. qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
  2271. struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
  2272. {
  2273. uint16_t avail_dsds;
  2274. uint32_t *cur_dsd;
  2275. uint32_t req_data_len = 0;
  2276. uint32_t rsp_data_len = 0;
  2277. struct scatterlist *sg;
  2278. int index;
  2279. int entry_count = 1;
  2280. struct fc_bsg_job *bsg_job = sp->u.bsg_job;
  2281. /*Update entry type to indicate bidir command */
  2282. *((uint32_t *)(&cmd_pkt->entry_type)) =
  2283. __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
  2284. /* Set the transfer direction, in this set both flags
  2285. * Also set the BD_WRAP_BACK flag, firmware will take care
  2286. * assigning DID=SID for outgoing pkts.
  2287. */
  2288. cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
  2289. cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
  2290. cmd_pkt->control_flags =
  2291. __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
  2292. BD_WRAP_BACK);
  2293. req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
  2294. cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
  2295. cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
  2296. cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
  2297. vha->bidi_stats.transfer_bytes += req_data_len;
  2298. vha->bidi_stats.io_count++;
  2299. /* Only one dsd is available for bidirectional IOCB, remaining dsds
  2300. * are bundled in continuation iocb
  2301. */
  2302. avail_dsds = 1;
  2303. cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
  2304. index = 0;
  2305. for_each_sg(bsg_job->request_payload.sg_list, sg,
  2306. bsg_job->request_payload.sg_cnt, index) {
  2307. dma_addr_t sle_dma;
  2308. cont_a64_entry_t *cont_pkt;
  2309. /* Allocate additional continuation packets */
  2310. if (avail_dsds == 0) {
  2311. /* Continuation type 1 IOCB can accomodate
  2312. * 5 DSDS
  2313. */
  2314. cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
  2315. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  2316. avail_dsds = 5;
  2317. entry_count++;
  2318. }
  2319. sle_dma = sg_dma_address(sg);
  2320. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  2321. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  2322. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  2323. avail_dsds--;
  2324. }
  2325. /* For read request DSD will always goes to continuation IOCB
  2326. * and follow the write DSD. If there is room on the current IOCB
  2327. * then it is added to that IOCB else new continuation IOCB is
  2328. * allocated.
  2329. */
  2330. for_each_sg(bsg_job->reply_payload.sg_list, sg,
  2331. bsg_job->reply_payload.sg_cnt, index) {
  2332. dma_addr_t sle_dma;
  2333. cont_a64_entry_t *cont_pkt;
  2334. /* Allocate additional continuation packets */
  2335. if (avail_dsds == 0) {
  2336. /* Continuation type 1 IOCB can accomodate
  2337. * 5 DSDS
  2338. */
  2339. cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
  2340. cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
  2341. avail_dsds = 5;
  2342. entry_count++;
  2343. }
  2344. sle_dma = sg_dma_address(sg);
  2345. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  2346. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  2347. *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
  2348. avail_dsds--;
  2349. }
  2350. /* This value should be same as number of IOCB required for this cmd */
  2351. cmd_pkt->entry_count = entry_count;
  2352. }
  2353. int
  2354. qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
  2355. {
  2356. struct qla_hw_data *ha = vha->hw;
  2357. unsigned long flags;
  2358. uint32_t handle;
  2359. uint32_t index;
  2360. uint16_t req_cnt;
  2361. uint16_t cnt;
  2362. uint32_t *clr_ptr;
  2363. struct cmd_bidir *cmd_pkt = NULL;
  2364. struct rsp_que *rsp;
  2365. struct req_que *req;
  2366. int rval = EXT_STATUS_OK;
  2367. rval = QLA_SUCCESS;
  2368. rsp = ha->rsp_q_map[0];
  2369. req = vha->req;
  2370. /* Send marker if required */
  2371. if (vha->marker_needed != 0) {
  2372. if (qla2x00_marker(vha, req,
  2373. rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
  2374. return EXT_STATUS_MAILBOX;
  2375. vha->marker_needed = 0;
  2376. }
  2377. /* Acquire ring specific lock */
  2378. spin_lock_irqsave(&ha->hardware_lock, flags);
  2379. /* Check for room in outstanding command list. */
  2380. handle = req->current_outstanding_cmd;
  2381. for (index = 1; index < req->num_outstanding_cmds; index++) {
  2382. handle++;
  2383. if (handle == req->num_outstanding_cmds)
  2384. handle = 1;
  2385. if (!req->outstanding_cmds[handle])
  2386. break;
  2387. }
  2388. if (index == req->num_outstanding_cmds) {
  2389. rval = EXT_STATUS_BUSY;
  2390. goto queuing_error;
  2391. }
  2392. /* Calculate number of IOCB required */
  2393. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  2394. /* Check for room on request queue. */
  2395. if (req->cnt < req_cnt + 2) {
  2396. cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
  2397. if (req->ring_index < cnt)
  2398. req->cnt = cnt - req->ring_index;
  2399. else
  2400. req->cnt = req->length -
  2401. (req->ring_index - cnt);
  2402. }
  2403. if (req->cnt < req_cnt + 2) {
  2404. rval = EXT_STATUS_BUSY;
  2405. goto queuing_error;
  2406. }
  2407. cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
  2408. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2409. /* Zero out remaining portion of packet. */
  2410. /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
  2411. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2412. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2413. /* Set NPORT-ID (of vha)*/
  2414. cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
  2415. cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
  2416. cmd_pkt->port_id[1] = vha->d_id.b.area;
  2417. cmd_pkt->port_id[2] = vha->d_id.b.domain;
  2418. qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
  2419. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2420. /* Build command packet. */
  2421. req->current_outstanding_cmd = handle;
  2422. req->outstanding_cmds[handle] = sp;
  2423. sp->handle = handle;
  2424. req->cnt -= req_cnt;
  2425. /* Send the command to the firmware */
  2426. wmb();
  2427. qla2x00_start_iocbs(vha, req);
  2428. queuing_error:
  2429. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2430. return rval;
  2431. }