qla_iocb.c 80 KB

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