qla_iocb.c 75 KB

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