csio_scsi.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/device.h>
  35. #include <linux/delay.h>
  36. #include <linux/ctype.h>
  37. #include <linux/kernel.h>
  38. #include <linux/slab.h>
  39. #include <linux/string.h>
  40. #include <linux/compiler.h>
  41. #include <linux/export.h>
  42. #include <linux/module.h>
  43. #include <asm/unaligned.h>
  44. #include <asm/page.h>
  45. #include <scsi/scsi.h>
  46. #include <scsi/scsi_device.h>
  47. #include <scsi/scsi_transport_fc.h>
  48. #include "csio_hw.h"
  49. #include "csio_lnode.h"
  50. #include "csio_rnode.h"
  51. #include "csio_scsi.h"
  52. #include "csio_init.h"
  53. int csio_scsi_eqsize = 65536;
  54. int csio_scsi_iqlen = 128;
  55. int csio_scsi_ioreqs = 2048;
  56. uint32_t csio_max_scan_tmo;
  57. uint32_t csio_delta_scan_tmo = 5;
  58. int csio_lun_qdepth = 32;
  59. static int csio_ddp_descs = 128;
  60. static int csio_do_abrt_cls(struct csio_hw *,
  61. struct csio_ioreq *, bool);
  62. static void csio_scsis_uninit(struct csio_ioreq *, enum csio_scsi_ev);
  63. static void csio_scsis_io_active(struct csio_ioreq *, enum csio_scsi_ev);
  64. static void csio_scsis_tm_active(struct csio_ioreq *, enum csio_scsi_ev);
  65. static void csio_scsis_aborting(struct csio_ioreq *, enum csio_scsi_ev);
  66. static void csio_scsis_closing(struct csio_ioreq *, enum csio_scsi_ev);
  67. static void csio_scsis_shost_cmpl_await(struct csio_ioreq *, enum csio_scsi_ev);
  68. /*
  69. * csio_scsi_match_io - Match an ioreq with the given SCSI level data.
  70. * @ioreq: The I/O request
  71. * @sld: Level information
  72. *
  73. * Should be called with lock held.
  74. *
  75. */
  76. static bool
  77. csio_scsi_match_io(struct csio_ioreq *ioreq, struct csio_scsi_level_data *sld)
  78. {
  79. struct scsi_cmnd *scmnd = csio_scsi_cmnd(ioreq);
  80. switch (sld->level) {
  81. case CSIO_LEV_LUN:
  82. if (scmnd == NULL)
  83. return false;
  84. return ((ioreq->lnode == sld->lnode) &&
  85. (ioreq->rnode == sld->rnode) &&
  86. ((uint64_t)scmnd->device->lun == sld->oslun));
  87. case CSIO_LEV_RNODE:
  88. return ((ioreq->lnode == sld->lnode) &&
  89. (ioreq->rnode == sld->rnode));
  90. case CSIO_LEV_LNODE:
  91. return (ioreq->lnode == sld->lnode);
  92. case CSIO_LEV_ALL:
  93. return true;
  94. default:
  95. return false;
  96. }
  97. }
  98. /*
  99. * csio_scsi_gather_active_ios - Gather active I/Os based on level
  100. * @scm: SCSI module
  101. * @sld: Level information
  102. * @dest: The queue where these I/Os have to be gathered.
  103. *
  104. * Should be called with lock held.
  105. */
  106. static void
  107. csio_scsi_gather_active_ios(struct csio_scsim *scm,
  108. struct csio_scsi_level_data *sld,
  109. struct list_head *dest)
  110. {
  111. struct list_head *tmp, *next;
  112. if (list_empty(&scm->active_q))
  113. return;
  114. /* Just splice the entire active_q into dest */
  115. if (sld->level == CSIO_LEV_ALL) {
  116. list_splice_tail_init(&scm->active_q, dest);
  117. return;
  118. }
  119. list_for_each_safe(tmp, next, &scm->active_q) {
  120. if (csio_scsi_match_io((struct csio_ioreq *)tmp, sld)) {
  121. list_del_init(tmp);
  122. list_add_tail(tmp, dest);
  123. }
  124. }
  125. }
  126. static inline bool
  127. csio_scsi_itnexus_loss_error(uint16_t error)
  128. {
  129. switch (error) {
  130. case FW_ERR_LINK_DOWN:
  131. case FW_RDEV_NOT_READY:
  132. case FW_ERR_RDEV_LOST:
  133. case FW_ERR_RDEV_LOGO:
  134. case FW_ERR_RDEV_IMPL_LOGO:
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. /*
  140. * csio_scsi_fcp_cmnd - Frame the SCSI FCP command paylod.
  141. * @req: IO req structure.
  142. * @addr: DMA location to place the payload.
  143. *
  144. * This routine is shared between FCP_WRITE, FCP_READ and FCP_CMD requests.
  145. */
  146. static inline void
  147. csio_scsi_fcp_cmnd(struct csio_ioreq *req, void *addr)
  148. {
  149. struct fcp_cmnd *fcp_cmnd = (struct fcp_cmnd *)addr;
  150. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  151. /* Check for Task Management */
  152. if (likely(scmnd->SCp.Message == 0)) {
  153. int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
  154. fcp_cmnd->fc_tm_flags = 0;
  155. fcp_cmnd->fc_cmdref = 0;
  156. memcpy(fcp_cmnd->fc_cdb, scmnd->cmnd, 16);
  157. if (scmnd->flags & SCMD_TAGGED)
  158. fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
  159. else
  160. fcp_cmnd->fc_pri_ta = 0;
  161. fcp_cmnd->fc_dl = cpu_to_be32(scsi_bufflen(scmnd));
  162. if (req->nsge)
  163. if (req->datadir == DMA_TO_DEVICE)
  164. fcp_cmnd->fc_flags = FCP_CFL_WRDATA;
  165. else
  166. fcp_cmnd->fc_flags = FCP_CFL_RDDATA;
  167. else
  168. fcp_cmnd->fc_flags = 0;
  169. } else {
  170. memset(fcp_cmnd, 0, sizeof(*fcp_cmnd));
  171. int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
  172. fcp_cmnd->fc_tm_flags = (uint8_t)scmnd->SCp.Message;
  173. }
  174. }
  175. /*
  176. * csio_scsi_init_cmd_wr - Initialize the SCSI CMD WR.
  177. * @req: IO req structure.
  178. * @addr: DMA location to place the payload.
  179. * @size: Size of WR (including FW WR + immed data + rsp SG entry
  180. *
  181. * Wrapper for populating fw_scsi_cmd_wr.
  182. */
  183. static inline void
  184. csio_scsi_init_cmd_wr(struct csio_ioreq *req, void *addr, uint32_t size)
  185. {
  186. struct csio_hw *hw = req->lnode->hwp;
  187. struct csio_rnode *rn = req->rnode;
  188. struct fw_scsi_cmd_wr *wr = (struct fw_scsi_cmd_wr *)addr;
  189. struct csio_dma_buf *dma_buf;
  190. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  191. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_CMD_WR) |
  192. FW_SCSI_CMD_WR_IMMDLEN(imm));
  193. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  194. FW_WR_LEN16(
  195. DIV_ROUND_UP(size, 16)));
  196. wr->cookie = (uintptr_t) req;
  197. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  198. wr->tmo_val = (uint8_t) req->tmo;
  199. wr->r3 = 0;
  200. memset(&wr->r5, 0, 8);
  201. /* Get RSP DMA buffer */
  202. dma_buf = &req->dma_buf;
  203. /* Prepare RSP SGL */
  204. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  205. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  206. wr->r6 = 0;
  207. wr->u.fcoe.ctl_pri = 0;
  208. wr->u.fcoe.cp_en_class = 0;
  209. wr->u.fcoe.r4_lo[0] = 0;
  210. wr->u.fcoe.r4_lo[1] = 0;
  211. /* Frame a FCP command */
  212. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)addr +
  213. sizeof(struct fw_scsi_cmd_wr)));
  214. }
  215. #define CSIO_SCSI_CMD_WR_SZ(_imm) \
  216. (sizeof(struct fw_scsi_cmd_wr) + /* WR size */ \
  217. ALIGN((_imm), 16)) /* Immed data */
  218. #define CSIO_SCSI_CMD_WR_SZ_16(_imm) \
  219. (ALIGN(CSIO_SCSI_CMD_WR_SZ((_imm)), 16))
  220. /*
  221. * csio_scsi_cmd - Create a SCSI CMD WR.
  222. * @req: IO req structure.
  223. *
  224. * Gets a WR slot in the ingress queue and initializes it with SCSI CMD WR.
  225. *
  226. */
  227. static inline void
  228. csio_scsi_cmd(struct csio_ioreq *req)
  229. {
  230. struct csio_wr_pair wrp;
  231. struct csio_hw *hw = req->lnode->hwp;
  232. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  233. uint32_t size = CSIO_SCSI_CMD_WR_SZ_16(scsim->proto_cmd_len);
  234. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  235. if (unlikely(req->drv_status != 0))
  236. return;
  237. if (wrp.size1 >= size) {
  238. /* Initialize WR in one shot */
  239. csio_scsi_init_cmd_wr(req, wrp.addr1, size);
  240. } else {
  241. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  242. /*
  243. * Make a temporary copy of the WR and write back
  244. * the copy into the WR pair.
  245. */
  246. csio_scsi_init_cmd_wr(req, (void *)tmpwr, size);
  247. memcpy(wrp.addr1, tmpwr, wrp.size1);
  248. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  249. }
  250. }
  251. /*
  252. * csio_scsi_init_ulptx_dsgl - Fill in a ULP_TX_SC_DSGL
  253. * @hw: HW module
  254. * @req: IO request
  255. * @sgl: ULP TX SGL pointer.
  256. *
  257. */
  258. static inline void
  259. csio_scsi_init_ultptx_dsgl(struct csio_hw *hw, struct csio_ioreq *req,
  260. struct ulptx_sgl *sgl)
  261. {
  262. struct ulptx_sge_pair *sge_pair = NULL;
  263. struct scatterlist *sgel;
  264. uint32_t i = 0;
  265. uint32_t xfer_len;
  266. struct list_head *tmp;
  267. struct csio_dma_buf *dma_buf;
  268. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  269. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_MORE |
  270. ULPTX_NSGE(req->nsge));
  271. /* Now add the data SGLs */
  272. if (likely(!req->dcopy)) {
  273. scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
  274. if (i == 0) {
  275. sgl->addr0 = cpu_to_be64(sg_dma_address(sgel));
  276. sgl->len0 = cpu_to_be32(sg_dma_len(sgel));
  277. sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
  278. continue;
  279. }
  280. if ((i - 1) & 0x1) {
  281. sge_pair->addr[1] = cpu_to_be64(
  282. sg_dma_address(sgel));
  283. sge_pair->len[1] = cpu_to_be32(
  284. sg_dma_len(sgel));
  285. sge_pair++;
  286. } else {
  287. sge_pair->addr[0] = cpu_to_be64(
  288. sg_dma_address(sgel));
  289. sge_pair->len[0] = cpu_to_be32(
  290. sg_dma_len(sgel));
  291. }
  292. }
  293. } else {
  294. /* Program sg elements with driver's DDP buffer */
  295. xfer_len = scsi_bufflen(scmnd);
  296. list_for_each(tmp, &req->gen_list) {
  297. dma_buf = (struct csio_dma_buf *)tmp;
  298. if (i == 0) {
  299. sgl->addr0 = cpu_to_be64(dma_buf->paddr);
  300. sgl->len0 = cpu_to_be32(
  301. min(xfer_len, dma_buf->len));
  302. sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
  303. } else if ((i - 1) & 0x1) {
  304. sge_pair->addr[1] = cpu_to_be64(dma_buf->paddr);
  305. sge_pair->len[1] = cpu_to_be32(
  306. min(xfer_len, dma_buf->len));
  307. sge_pair++;
  308. } else {
  309. sge_pair->addr[0] = cpu_to_be64(dma_buf->paddr);
  310. sge_pair->len[0] = cpu_to_be32(
  311. min(xfer_len, dma_buf->len));
  312. }
  313. xfer_len -= min(xfer_len, dma_buf->len);
  314. i++;
  315. }
  316. }
  317. }
  318. /*
  319. * csio_scsi_init_read_wr - Initialize the READ SCSI WR.
  320. * @req: IO req structure.
  321. * @wrp: DMA location to place the payload.
  322. * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
  323. *
  324. * Wrapper for populating fw_scsi_read_wr.
  325. */
  326. static inline void
  327. csio_scsi_init_read_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
  328. {
  329. struct csio_hw *hw = req->lnode->hwp;
  330. struct csio_rnode *rn = req->rnode;
  331. struct fw_scsi_read_wr *wr = (struct fw_scsi_read_wr *)wrp;
  332. struct ulptx_sgl *sgl;
  333. struct csio_dma_buf *dma_buf;
  334. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  335. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  336. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_READ_WR) |
  337. FW_SCSI_READ_WR_IMMDLEN(imm));
  338. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  339. FW_WR_LEN16(DIV_ROUND_UP(size, 16)));
  340. wr->cookie = (uintptr_t)req;
  341. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  342. wr->tmo_val = (uint8_t)(req->tmo);
  343. wr->use_xfer_cnt = 1;
  344. wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  345. wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  346. /* Get RSP DMA buffer */
  347. dma_buf = &req->dma_buf;
  348. /* Prepare RSP SGL */
  349. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  350. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  351. wr->r4 = 0;
  352. wr->u.fcoe.ctl_pri = 0;
  353. wr->u.fcoe.cp_en_class = 0;
  354. wr->u.fcoe.r3_lo[0] = 0;
  355. wr->u.fcoe.r3_lo[1] = 0;
  356. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
  357. sizeof(struct fw_scsi_read_wr)));
  358. /* Move WR pointer past command and immediate data */
  359. sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
  360. sizeof(struct fw_scsi_read_wr) + ALIGN(imm, 16));
  361. /* Fill in the DSGL */
  362. csio_scsi_init_ultptx_dsgl(hw, req, sgl);
  363. }
  364. /*
  365. * csio_scsi_init_write_wr - Initialize the WRITE SCSI WR.
  366. * @req: IO req structure.
  367. * @wrp: DMA location to place the payload.
  368. * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
  369. *
  370. * Wrapper for populating fw_scsi_write_wr.
  371. */
  372. static inline void
  373. csio_scsi_init_write_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
  374. {
  375. struct csio_hw *hw = req->lnode->hwp;
  376. struct csio_rnode *rn = req->rnode;
  377. struct fw_scsi_write_wr *wr = (struct fw_scsi_write_wr *)wrp;
  378. struct ulptx_sgl *sgl;
  379. struct csio_dma_buf *dma_buf;
  380. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  381. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  382. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_WRITE_WR) |
  383. FW_SCSI_WRITE_WR_IMMDLEN(imm));
  384. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  385. FW_WR_LEN16(DIV_ROUND_UP(size, 16)));
  386. wr->cookie = (uintptr_t)req;
  387. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  388. wr->tmo_val = (uint8_t)(req->tmo);
  389. wr->use_xfer_cnt = 1;
  390. wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  391. wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  392. /* Get RSP DMA buffer */
  393. dma_buf = &req->dma_buf;
  394. /* Prepare RSP SGL */
  395. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  396. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  397. wr->r4 = 0;
  398. wr->u.fcoe.ctl_pri = 0;
  399. wr->u.fcoe.cp_en_class = 0;
  400. wr->u.fcoe.r3_lo[0] = 0;
  401. wr->u.fcoe.r3_lo[1] = 0;
  402. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
  403. sizeof(struct fw_scsi_write_wr)));
  404. /* Move WR pointer past command and immediate data */
  405. sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
  406. sizeof(struct fw_scsi_write_wr) + ALIGN(imm, 16));
  407. /* Fill in the DSGL */
  408. csio_scsi_init_ultptx_dsgl(hw, req, sgl);
  409. }
  410. /* Calculate WR size needed for fw_scsi_read_wr/fw_scsi_write_wr */
  411. #define CSIO_SCSI_DATA_WRSZ(req, oper, sz, imm) \
  412. do { \
  413. (sz) = sizeof(struct fw_scsi_##oper##_wr) + /* WR size */ \
  414. ALIGN((imm), 16) + /* Immed data */ \
  415. sizeof(struct ulptx_sgl); /* ulptx_sgl */ \
  416. \
  417. if (unlikely((req)->nsge > 1)) \
  418. (sz) += (sizeof(struct ulptx_sge_pair) * \
  419. (ALIGN(((req)->nsge - 1), 2) / 2)); \
  420. /* Data SGE */ \
  421. } while (0)
  422. /*
  423. * csio_scsi_read - Create a SCSI READ WR.
  424. * @req: IO req structure.
  425. *
  426. * Gets a WR slot in the ingress queue and initializes it with
  427. * SCSI READ WR.
  428. *
  429. */
  430. static inline void
  431. csio_scsi_read(struct csio_ioreq *req)
  432. {
  433. struct csio_wr_pair wrp;
  434. uint32_t size;
  435. struct csio_hw *hw = req->lnode->hwp;
  436. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  437. CSIO_SCSI_DATA_WRSZ(req, read, size, scsim->proto_cmd_len);
  438. size = ALIGN(size, 16);
  439. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  440. if (likely(req->drv_status == 0)) {
  441. if (likely(wrp.size1 >= size)) {
  442. /* Initialize WR in one shot */
  443. csio_scsi_init_read_wr(req, wrp.addr1, size);
  444. } else {
  445. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  446. /*
  447. * Make a temporary copy of the WR and write back
  448. * the copy into the WR pair.
  449. */
  450. csio_scsi_init_read_wr(req, (void *)tmpwr, size);
  451. memcpy(wrp.addr1, tmpwr, wrp.size1);
  452. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  453. }
  454. }
  455. }
  456. /*
  457. * csio_scsi_write - Create a SCSI WRITE WR.
  458. * @req: IO req structure.
  459. *
  460. * Gets a WR slot in the ingress queue and initializes it with
  461. * SCSI WRITE WR.
  462. *
  463. */
  464. static inline void
  465. csio_scsi_write(struct csio_ioreq *req)
  466. {
  467. struct csio_wr_pair wrp;
  468. uint32_t size;
  469. struct csio_hw *hw = req->lnode->hwp;
  470. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  471. CSIO_SCSI_DATA_WRSZ(req, write, size, scsim->proto_cmd_len);
  472. size = ALIGN(size, 16);
  473. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  474. if (likely(req->drv_status == 0)) {
  475. if (likely(wrp.size1 >= size)) {
  476. /* Initialize WR in one shot */
  477. csio_scsi_init_write_wr(req, wrp.addr1, size);
  478. } else {
  479. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  480. /*
  481. * Make a temporary copy of the WR and write back
  482. * the copy into the WR pair.
  483. */
  484. csio_scsi_init_write_wr(req, (void *)tmpwr, size);
  485. memcpy(wrp.addr1, tmpwr, wrp.size1);
  486. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  487. }
  488. }
  489. }
  490. /*
  491. * csio_setup_ddp - Setup DDP buffers for Read request.
  492. * @req: IO req structure.
  493. *
  494. * Checks SGLs/Data buffers are virtually contiguous required for DDP.
  495. * If contiguous,driver posts SGLs in the WR otherwise post internal
  496. * buffers for such request for DDP.
  497. */
  498. static inline void
  499. csio_setup_ddp(struct csio_scsim *scsim, struct csio_ioreq *req)
  500. {
  501. #ifdef __CSIO_DEBUG__
  502. struct csio_hw *hw = req->lnode->hwp;
  503. #endif
  504. struct scatterlist *sgel = NULL;
  505. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  506. uint64_t sg_addr = 0;
  507. uint32_t ddp_pagesz = 4096;
  508. uint32_t buf_off;
  509. struct csio_dma_buf *dma_buf = NULL;
  510. uint32_t alloc_len = 0;
  511. uint32_t xfer_len = 0;
  512. uint32_t sg_len = 0;
  513. uint32_t i;
  514. scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
  515. sg_addr = sg_dma_address(sgel);
  516. sg_len = sg_dma_len(sgel);
  517. buf_off = sg_addr & (ddp_pagesz - 1);
  518. /* Except 1st buffer,all buffer addr have to be Page aligned */
  519. if (i != 0 && buf_off) {
  520. csio_dbg(hw, "SGL addr not DDP aligned (%llx:%d)\n",
  521. sg_addr, sg_len);
  522. goto unaligned;
  523. }
  524. /* Except last buffer,all buffer must end on page boundary */
  525. if ((i != (req->nsge - 1)) &&
  526. ((buf_off + sg_len) & (ddp_pagesz - 1))) {
  527. csio_dbg(hw,
  528. "SGL addr not ending on page boundary"
  529. "(%llx:%d)\n", sg_addr, sg_len);
  530. goto unaligned;
  531. }
  532. }
  533. /* SGL's are virtually contiguous. HW will DDP to SGLs */
  534. req->dcopy = 0;
  535. csio_scsi_read(req);
  536. return;
  537. unaligned:
  538. CSIO_INC_STATS(scsim, n_unaligned);
  539. /*
  540. * For unaligned SGLs, driver will allocate internal DDP buffer.
  541. * Once command is completed data from DDP buffer copied to SGLs
  542. */
  543. req->dcopy = 1;
  544. /* Use gen_list to store the DDP buffers */
  545. INIT_LIST_HEAD(&req->gen_list);
  546. xfer_len = scsi_bufflen(scmnd);
  547. i = 0;
  548. /* Allocate ddp buffers for this request */
  549. while (alloc_len < xfer_len) {
  550. dma_buf = csio_get_scsi_ddp(scsim);
  551. if (dma_buf == NULL || i > scsim->max_sge) {
  552. req->drv_status = -EBUSY;
  553. break;
  554. }
  555. alloc_len += dma_buf->len;
  556. /* Added to IO req */
  557. list_add_tail(&dma_buf->list, &req->gen_list);
  558. i++;
  559. }
  560. if (!req->drv_status) {
  561. /* set number of ddp bufs used */
  562. req->nsge = i;
  563. csio_scsi_read(req);
  564. return;
  565. }
  566. /* release dma descs */
  567. if (i > 0)
  568. csio_put_scsi_ddp_list(scsim, &req->gen_list, i);
  569. }
  570. /*
  571. * csio_scsi_init_abrt_cls_wr - Initialize an ABORT/CLOSE WR.
  572. * @req: IO req structure.
  573. * @addr: DMA location to place the payload.
  574. * @size: Size of WR
  575. * @abort: abort OR close
  576. *
  577. * Wrapper for populating fw_scsi_cmd_wr.
  578. */
  579. static inline void
  580. csio_scsi_init_abrt_cls_wr(struct csio_ioreq *req, void *addr, uint32_t size,
  581. bool abort)
  582. {
  583. struct csio_hw *hw = req->lnode->hwp;
  584. struct csio_rnode *rn = req->rnode;
  585. struct fw_scsi_abrt_cls_wr *wr = (struct fw_scsi_abrt_cls_wr *)addr;
  586. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_ABRT_CLS_WR));
  587. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  588. FW_WR_LEN16(
  589. DIV_ROUND_UP(size, 16)));
  590. wr->cookie = (uintptr_t) req;
  591. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  592. wr->tmo_val = (uint8_t) req->tmo;
  593. /* 0 for CHK_ALL_IO tells FW to look up t_cookie */
  594. wr->sub_opcode_to_chk_all_io =
  595. (FW_SCSI_ABRT_CLS_WR_SUB_OPCODE(abort) |
  596. FW_SCSI_ABRT_CLS_WR_CHK_ALL_IO(0));
  597. wr->r3[0] = 0;
  598. wr->r3[1] = 0;
  599. wr->r3[2] = 0;
  600. wr->r3[3] = 0;
  601. /* Since we re-use the same ioreq for abort as well */
  602. wr->t_cookie = (uintptr_t) req;
  603. }
  604. static inline void
  605. csio_scsi_abrt_cls(struct csio_ioreq *req, bool abort)
  606. {
  607. struct csio_wr_pair wrp;
  608. struct csio_hw *hw = req->lnode->hwp;
  609. uint32_t size = ALIGN(sizeof(struct fw_scsi_abrt_cls_wr), 16);
  610. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  611. if (req->drv_status != 0)
  612. return;
  613. if (wrp.size1 >= size) {
  614. /* Initialize WR in one shot */
  615. csio_scsi_init_abrt_cls_wr(req, wrp.addr1, size, abort);
  616. } else {
  617. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  618. /*
  619. * Make a temporary copy of the WR and write back
  620. * the copy into the WR pair.
  621. */
  622. csio_scsi_init_abrt_cls_wr(req, (void *)tmpwr, size, abort);
  623. memcpy(wrp.addr1, tmpwr, wrp.size1);
  624. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  625. }
  626. }
  627. /*****************************************************************************/
  628. /* START: SCSI SM */
  629. /*****************************************************************************/
  630. static void
  631. csio_scsis_uninit(struct csio_ioreq *req, enum csio_scsi_ev evt)
  632. {
  633. struct csio_hw *hw = req->lnode->hwp;
  634. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  635. switch (evt) {
  636. case CSIO_SCSIE_START_IO:
  637. if (req->nsge) {
  638. if (req->datadir == DMA_TO_DEVICE) {
  639. req->dcopy = 0;
  640. csio_scsi_write(req);
  641. } else
  642. csio_setup_ddp(scsim, req);
  643. } else {
  644. csio_scsi_cmd(req);
  645. }
  646. if (likely(req->drv_status == 0)) {
  647. /* change state and enqueue on active_q */
  648. csio_set_state(&req->sm, csio_scsis_io_active);
  649. list_add_tail(&req->sm.sm_list, &scsim->active_q);
  650. csio_wr_issue(hw, req->eq_idx, false);
  651. CSIO_INC_STATS(scsim, n_active);
  652. return;
  653. }
  654. break;
  655. case CSIO_SCSIE_START_TM:
  656. csio_scsi_cmd(req);
  657. if (req->drv_status == 0) {
  658. /*
  659. * NOTE: We collect the affected I/Os prior to issuing
  660. * LUN reset, and not after it. This is to prevent
  661. * aborting I/Os that get issued after the LUN reset,
  662. * but prior to LUN reset completion (in the event that
  663. * the host stack has not blocked I/Os to a LUN that is
  664. * being reset.
  665. */
  666. csio_set_state(&req->sm, csio_scsis_tm_active);
  667. list_add_tail(&req->sm.sm_list, &scsim->active_q);
  668. csio_wr_issue(hw, req->eq_idx, false);
  669. CSIO_INC_STATS(scsim, n_tm_active);
  670. }
  671. return;
  672. case CSIO_SCSIE_ABORT:
  673. case CSIO_SCSIE_CLOSE:
  674. /*
  675. * NOTE:
  676. * We could get here due to :
  677. * - a window in the cleanup path of the SCSI module
  678. * (csio_scsi_abort_io()). Please see NOTE in this function.
  679. * - a window in the time we tried to issue an abort/close
  680. * of a request to FW, and the FW completed the request
  681. * itself.
  682. * Print a message for now, and return INVAL either way.
  683. */
  684. req->drv_status = -EINVAL;
  685. csio_warn(hw, "Trying to abort/close completed IO:%p!\n", req);
  686. break;
  687. default:
  688. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  689. CSIO_DB_ASSERT(0);
  690. }
  691. }
  692. static void
  693. csio_scsis_io_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
  694. {
  695. struct csio_hw *hw = req->lnode->hwp;
  696. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  697. struct csio_rnode *rn;
  698. switch (evt) {
  699. case CSIO_SCSIE_COMPLETED:
  700. CSIO_DEC_STATS(scm, n_active);
  701. list_del_init(&req->sm.sm_list);
  702. csio_set_state(&req->sm, csio_scsis_uninit);
  703. /*
  704. * In MSIX mode, with multiple queues, the SCSI compeltions
  705. * could reach us sooner than the FW events sent to indicate
  706. * I-T nexus loss (link down, remote device logo etc). We
  707. * dont want to be returning such I/Os to the upper layer
  708. * immediately, since we wouldnt have reported the I-T nexus
  709. * loss itself. This forces us to serialize such completions
  710. * with the reporting of the I-T nexus loss. Therefore, we
  711. * internally queue up such up such completions in the rnode.
  712. * The reporting of I-T nexus loss to the upper layer is then
  713. * followed by the returning of I/Os in this internal queue.
  714. * Having another state alongwith another queue helps us take
  715. * actions for events such as ABORT received while we are
  716. * in this rnode queue.
  717. */
  718. if (unlikely(req->wr_status != FW_SUCCESS)) {
  719. rn = req->rnode;
  720. /*
  721. * FW says remote device is lost, but rnode
  722. * doesnt reflect it.
  723. */
  724. if (csio_scsi_itnexus_loss_error(req->wr_status) &&
  725. csio_is_rnode_ready(rn)) {
  726. csio_set_state(&req->sm,
  727. csio_scsis_shost_cmpl_await);
  728. list_add_tail(&req->sm.sm_list,
  729. &rn->host_cmpl_q);
  730. }
  731. }
  732. break;
  733. case CSIO_SCSIE_ABORT:
  734. csio_scsi_abrt_cls(req, SCSI_ABORT);
  735. if (req->drv_status == 0) {
  736. csio_wr_issue(hw, req->eq_idx, false);
  737. csio_set_state(&req->sm, csio_scsis_aborting);
  738. }
  739. break;
  740. case CSIO_SCSIE_CLOSE:
  741. csio_scsi_abrt_cls(req, SCSI_CLOSE);
  742. if (req->drv_status == 0) {
  743. csio_wr_issue(hw, req->eq_idx, false);
  744. csio_set_state(&req->sm, csio_scsis_closing);
  745. }
  746. break;
  747. case CSIO_SCSIE_DRVCLEANUP:
  748. req->wr_status = FW_HOSTERROR;
  749. CSIO_DEC_STATS(scm, n_active);
  750. csio_set_state(&req->sm, csio_scsis_uninit);
  751. break;
  752. default:
  753. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  754. CSIO_DB_ASSERT(0);
  755. }
  756. }
  757. static void
  758. csio_scsis_tm_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
  759. {
  760. struct csio_hw *hw = req->lnode->hwp;
  761. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  762. switch (evt) {
  763. case CSIO_SCSIE_COMPLETED:
  764. CSIO_DEC_STATS(scm, n_tm_active);
  765. list_del_init(&req->sm.sm_list);
  766. csio_set_state(&req->sm, csio_scsis_uninit);
  767. break;
  768. case CSIO_SCSIE_ABORT:
  769. csio_scsi_abrt_cls(req, SCSI_ABORT);
  770. if (req->drv_status == 0) {
  771. csio_wr_issue(hw, req->eq_idx, false);
  772. csio_set_state(&req->sm, csio_scsis_aborting);
  773. }
  774. break;
  775. case CSIO_SCSIE_CLOSE:
  776. csio_scsi_abrt_cls(req, SCSI_CLOSE);
  777. if (req->drv_status == 0) {
  778. csio_wr_issue(hw, req->eq_idx, false);
  779. csio_set_state(&req->sm, csio_scsis_closing);
  780. }
  781. break;
  782. case CSIO_SCSIE_DRVCLEANUP:
  783. req->wr_status = FW_HOSTERROR;
  784. CSIO_DEC_STATS(scm, n_tm_active);
  785. csio_set_state(&req->sm, csio_scsis_uninit);
  786. break;
  787. default:
  788. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  789. CSIO_DB_ASSERT(0);
  790. }
  791. }
  792. static void
  793. csio_scsis_aborting(struct csio_ioreq *req, enum csio_scsi_ev evt)
  794. {
  795. struct csio_hw *hw = req->lnode->hwp;
  796. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  797. switch (evt) {
  798. case CSIO_SCSIE_COMPLETED:
  799. csio_dbg(hw,
  800. "ioreq %p recvd cmpltd (wr_status:%d) "
  801. "in aborting st\n", req, req->wr_status);
  802. /*
  803. * Use -ECANCELED to explicitly tell the ABORTED event that
  804. * the original I/O was returned to driver by FW.
  805. * We dont really care if the I/O was returned with success by
  806. * FW (because the ABORT and completion of the I/O crossed each
  807. * other), or any other return value. Once we are in aborting
  808. * state, the success or failure of the I/O is unimportant to
  809. * us.
  810. */
  811. req->drv_status = -ECANCELED;
  812. break;
  813. case CSIO_SCSIE_ABORT:
  814. CSIO_INC_STATS(scm, n_abrt_dups);
  815. break;
  816. case CSIO_SCSIE_ABORTED:
  817. csio_dbg(hw, "abort of %p return status:0x%x drv_status:%x\n",
  818. req, req->wr_status, req->drv_status);
  819. /*
  820. * Check if original I/O WR completed before the Abort
  821. * completion.
  822. */
  823. if (req->drv_status != -ECANCELED) {
  824. csio_warn(hw,
  825. "Abort completed before original I/O,"
  826. " req:%p\n", req);
  827. CSIO_DB_ASSERT(0);
  828. }
  829. /*
  830. * There are the following possible scenarios:
  831. * 1. The abort completed successfully, FW returned FW_SUCCESS.
  832. * 2. The completion of an I/O and the receipt of
  833. * abort for that I/O by the FW crossed each other.
  834. * The FW returned FW_EINVAL. The original I/O would have
  835. * returned with FW_SUCCESS or any other SCSI error.
  836. * 3. The FW couldnt sent the abort out on the wire, as there
  837. * was an I-T nexus loss (link down, remote device logged
  838. * out etc). FW sent back an appropriate IT nexus loss status
  839. * for the abort.
  840. * 4. FW sent an abort, but abort timed out (remote device
  841. * didnt respond). FW replied back with
  842. * FW_SCSI_ABORT_TIMEDOUT.
  843. * 5. FW couldnt genuinely abort the request for some reason,
  844. * and sent us an error.
  845. *
  846. * The first 3 scenarios are treated as succesful abort
  847. * operations by the host, while the last 2 are failed attempts
  848. * to abort. Manipulate the return value of the request
  849. * appropriately, so that host can convey these results
  850. * back to the upper layer.
  851. */
  852. if ((req->wr_status == FW_SUCCESS) ||
  853. (req->wr_status == FW_EINVAL) ||
  854. csio_scsi_itnexus_loss_error(req->wr_status))
  855. req->wr_status = FW_SCSI_ABORT_REQUESTED;
  856. CSIO_DEC_STATS(scm, n_active);
  857. list_del_init(&req->sm.sm_list);
  858. csio_set_state(&req->sm, csio_scsis_uninit);
  859. break;
  860. case CSIO_SCSIE_DRVCLEANUP:
  861. req->wr_status = FW_HOSTERROR;
  862. CSIO_DEC_STATS(scm, n_active);
  863. csio_set_state(&req->sm, csio_scsis_uninit);
  864. break;
  865. case CSIO_SCSIE_CLOSE:
  866. /*
  867. * We can receive this event from the module
  868. * cleanup paths, if the FW forgot to reply to the ABORT WR
  869. * and left this ioreq in this state. For now, just ignore
  870. * the event. The CLOSE event is sent to this state, as
  871. * the LINK may have already gone down.
  872. */
  873. break;
  874. default:
  875. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  876. CSIO_DB_ASSERT(0);
  877. }
  878. }
  879. static void
  880. csio_scsis_closing(struct csio_ioreq *req, enum csio_scsi_ev evt)
  881. {
  882. struct csio_hw *hw = req->lnode->hwp;
  883. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  884. switch (evt) {
  885. case CSIO_SCSIE_COMPLETED:
  886. csio_dbg(hw,
  887. "ioreq %p recvd cmpltd (wr_status:%d) "
  888. "in closing st\n", req, req->wr_status);
  889. /*
  890. * Use -ECANCELED to explicitly tell the CLOSED event that
  891. * the original I/O was returned to driver by FW.
  892. * We dont really care if the I/O was returned with success by
  893. * FW (because the CLOSE and completion of the I/O crossed each
  894. * other), or any other return value. Once we are in aborting
  895. * state, the success or failure of the I/O is unimportant to
  896. * us.
  897. */
  898. req->drv_status = -ECANCELED;
  899. break;
  900. case CSIO_SCSIE_CLOSED:
  901. /*
  902. * Check if original I/O WR completed before the Close
  903. * completion.
  904. */
  905. if (req->drv_status != -ECANCELED) {
  906. csio_fatal(hw,
  907. "Close completed before original I/O,"
  908. " req:%p\n", req);
  909. CSIO_DB_ASSERT(0);
  910. }
  911. /*
  912. * Either close succeeded, or we issued close to FW at the
  913. * same time FW compelted it to us. Either way, the I/O
  914. * is closed.
  915. */
  916. CSIO_DB_ASSERT((req->wr_status == FW_SUCCESS) ||
  917. (req->wr_status == FW_EINVAL));
  918. req->wr_status = FW_SCSI_CLOSE_REQUESTED;
  919. CSIO_DEC_STATS(scm, n_active);
  920. list_del_init(&req->sm.sm_list);
  921. csio_set_state(&req->sm, csio_scsis_uninit);
  922. break;
  923. case CSIO_SCSIE_CLOSE:
  924. break;
  925. case CSIO_SCSIE_DRVCLEANUP:
  926. req->wr_status = FW_HOSTERROR;
  927. CSIO_DEC_STATS(scm, n_active);
  928. csio_set_state(&req->sm, csio_scsis_uninit);
  929. break;
  930. default:
  931. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  932. CSIO_DB_ASSERT(0);
  933. }
  934. }
  935. static void
  936. csio_scsis_shost_cmpl_await(struct csio_ioreq *req, enum csio_scsi_ev evt)
  937. {
  938. switch (evt) {
  939. case CSIO_SCSIE_ABORT:
  940. case CSIO_SCSIE_CLOSE:
  941. /*
  942. * Just succeed the abort request, and hope that
  943. * the remote device unregister path will cleanup
  944. * this I/O to the upper layer within a sane
  945. * amount of time.
  946. */
  947. /*
  948. * A close can come in during a LINK DOWN. The FW would have
  949. * returned us the I/O back, but not the remote device lost
  950. * FW event. In this interval, if the I/O times out at the upper
  951. * layer, a close can come in. Take the same action as abort:
  952. * return success, and hope that the remote device unregister
  953. * path will cleanup this I/O. If the FW still doesnt send
  954. * the msg, the close times out, and the upper layer resorts
  955. * to the next level of error recovery.
  956. */
  957. req->drv_status = 0;
  958. break;
  959. case CSIO_SCSIE_DRVCLEANUP:
  960. csio_set_state(&req->sm, csio_scsis_uninit);
  961. break;
  962. default:
  963. csio_dbg(req->lnode->hwp, "Unhandled event:%d sent to req:%p\n",
  964. evt, req);
  965. CSIO_DB_ASSERT(0);
  966. }
  967. }
  968. /*
  969. * csio_scsi_cmpl_handler - WR completion handler for SCSI.
  970. * @hw: HW module.
  971. * @wr: The completed WR from the ingress queue.
  972. * @len: Length of the WR.
  973. * @flb: Freelist buffer array.
  974. * @priv: Private object
  975. * @scsiwr: Pointer to SCSI WR.
  976. *
  977. * This is the WR completion handler called per completion from the
  978. * ISR. It is called with lock held. It walks past the RSS and CPL message
  979. * header where the actual WR is present.
  980. * It then gets the status, WR handle (ioreq pointer) and the len of
  981. * the WR, based on WR opcode. Only on a non-good status is the entire
  982. * WR copied into the WR cache (ioreq->fw_wr).
  983. * The ioreq corresponding to the WR is returned to the caller.
  984. * NOTE: The SCSI queue doesnt allocate a freelist today, hence
  985. * no freelist buffer is expected.
  986. */
  987. struct csio_ioreq *
  988. csio_scsi_cmpl_handler(struct csio_hw *hw, void *wr, uint32_t len,
  989. struct csio_fl_dma_buf *flb, void *priv, uint8_t **scsiwr)
  990. {
  991. struct csio_ioreq *ioreq = NULL;
  992. struct cpl_fw6_msg *cpl;
  993. uint8_t *tempwr;
  994. uint8_t status;
  995. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  996. /* skip RSS header */
  997. cpl = (struct cpl_fw6_msg *)((uintptr_t)wr + sizeof(__be64));
  998. if (unlikely(cpl->opcode != CPL_FW6_MSG)) {
  999. csio_warn(hw, "Error: Invalid CPL msg %x recvd on SCSI q\n",
  1000. cpl->opcode);
  1001. CSIO_INC_STATS(scm, n_inval_cplop);
  1002. return NULL;
  1003. }
  1004. tempwr = (uint8_t *)(cpl->data);
  1005. status = csio_wr_status(tempwr);
  1006. *scsiwr = tempwr;
  1007. if (likely((*tempwr == FW_SCSI_READ_WR) ||
  1008. (*tempwr == FW_SCSI_WRITE_WR) ||
  1009. (*tempwr == FW_SCSI_CMD_WR))) {
  1010. ioreq = (struct csio_ioreq *)((uintptr_t)
  1011. (((struct fw_scsi_read_wr *)tempwr)->cookie));
  1012. CSIO_DB_ASSERT(virt_addr_valid(ioreq));
  1013. ioreq->wr_status = status;
  1014. return ioreq;
  1015. }
  1016. if (*tempwr == FW_SCSI_ABRT_CLS_WR) {
  1017. ioreq = (struct csio_ioreq *)((uintptr_t)
  1018. (((struct fw_scsi_abrt_cls_wr *)tempwr)->cookie));
  1019. CSIO_DB_ASSERT(virt_addr_valid(ioreq));
  1020. ioreq->wr_status = status;
  1021. return ioreq;
  1022. }
  1023. csio_warn(hw, "WR with invalid opcode in SCSI IQ: %x\n", *tempwr);
  1024. CSIO_INC_STATS(scm, n_inval_scsiop);
  1025. return NULL;
  1026. }
  1027. /*
  1028. * csio_scsi_cleanup_io_q - Cleanup the given queue.
  1029. * @scm: SCSI module.
  1030. * @q: Queue to be cleaned up.
  1031. *
  1032. * Called with lock held. Has to exit with lock held.
  1033. */
  1034. void
  1035. csio_scsi_cleanup_io_q(struct csio_scsim *scm, struct list_head *q)
  1036. {
  1037. struct csio_hw *hw = scm->hw;
  1038. struct csio_ioreq *ioreq;
  1039. struct list_head *tmp, *next;
  1040. struct scsi_cmnd *scmnd;
  1041. /* Call back the completion routines of the active_q */
  1042. list_for_each_safe(tmp, next, q) {
  1043. ioreq = (struct csio_ioreq *)tmp;
  1044. csio_scsi_drvcleanup(ioreq);
  1045. list_del_init(&ioreq->sm.sm_list);
  1046. scmnd = csio_scsi_cmnd(ioreq);
  1047. spin_unlock_irq(&hw->lock);
  1048. /*
  1049. * Upper layers may have cleared this command, hence this
  1050. * check to avoid accessing stale references.
  1051. */
  1052. if (scmnd != NULL)
  1053. ioreq->io_cbfn(hw, ioreq);
  1054. spin_lock_irq(&scm->freelist_lock);
  1055. csio_put_scsi_ioreq(scm, ioreq);
  1056. spin_unlock_irq(&scm->freelist_lock);
  1057. spin_lock_irq(&hw->lock);
  1058. }
  1059. }
  1060. #define CSIO_SCSI_ABORT_Q_POLL_MS 2000
  1061. static void
  1062. csio_abrt_cls(struct csio_ioreq *ioreq, struct scsi_cmnd *scmnd)
  1063. {
  1064. struct csio_lnode *ln = ioreq->lnode;
  1065. struct csio_hw *hw = ln->hwp;
  1066. int ready = 0;
  1067. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1068. int rv;
  1069. if (csio_scsi_cmnd(ioreq) != scmnd) {
  1070. CSIO_INC_STATS(scsim, n_abrt_race_comp);
  1071. return;
  1072. }
  1073. ready = csio_is_lnode_ready(ln);
  1074. rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
  1075. if (rv != 0) {
  1076. if (ready)
  1077. CSIO_INC_STATS(scsim, n_abrt_busy_error);
  1078. else
  1079. CSIO_INC_STATS(scsim, n_cls_busy_error);
  1080. }
  1081. }
  1082. /*
  1083. * csio_scsi_abort_io_q - Abort all I/Os on given queue
  1084. * @scm: SCSI module.
  1085. * @q: Queue to abort.
  1086. * @tmo: Timeout in ms
  1087. *
  1088. * Attempt to abort all I/Os on given queue, and wait for a max
  1089. * of tmo milliseconds for them to complete. Returns success
  1090. * if all I/Os are aborted. Else returns -ETIMEDOUT.
  1091. * Should be entered with lock held. Exits with lock held.
  1092. * NOTE:
  1093. * Lock has to be held across the loop that aborts I/Os, since dropping the lock
  1094. * in between can cause the list to be corrupted. As a result, the caller
  1095. * of this function has to ensure that the number of I/os to be aborted
  1096. * is finite enough to not cause lock-held-for-too-long issues.
  1097. */
  1098. static int
  1099. csio_scsi_abort_io_q(struct csio_scsim *scm, struct list_head *q, uint32_t tmo)
  1100. {
  1101. struct csio_hw *hw = scm->hw;
  1102. struct list_head *tmp, *next;
  1103. int count = DIV_ROUND_UP(tmo, CSIO_SCSI_ABORT_Q_POLL_MS);
  1104. struct scsi_cmnd *scmnd;
  1105. if (list_empty(q))
  1106. return 0;
  1107. csio_dbg(hw, "Aborting SCSI I/Os\n");
  1108. /* Now abort/close I/Os in the queue passed */
  1109. list_for_each_safe(tmp, next, q) {
  1110. scmnd = csio_scsi_cmnd((struct csio_ioreq *)tmp);
  1111. csio_abrt_cls((struct csio_ioreq *)tmp, scmnd);
  1112. }
  1113. /* Wait till all active I/Os are completed/aborted/closed */
  1114. while (!list_empty(q) && count--) {
  1115. spin_unlock_irq(&hw->lock);
  1116. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1117. spin_lock_irq(&hw->lock);
  1118. }
  1119. /* all aborts completed */
  1120. if (list_empty(q))
  1121. return 0;
  1122. return -ETIMEDOUT;
  1123. }
  1124. /*
  1125. * csio_scsim_cleanup_io - Cleanup all I/Os in SCSI module.
  1126. * @scm: SCSI module.
  1127. * @abort: abort required.
  1128. * Called with lock held, should exit with lock held.
  1129. * Can sleep when waiting for I/Os to complete.
  1130. */
  1131. int
  1132. csio_scsim_cleanup_io(struct csio_scsim *scm, bool abort)
  1133. {
  1134. struct csio_hw *hw = scm->hw;
  1135. int rv = 0;
  1136. int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
  1137. /* No I/Os pending */
  1138. if (list_empty(&scm->active_q))
  1139. return 0;
  1140. /* Wait until all active I/Os are completed */
  1141. while (!list_empty(&scm->active_q) && count--) {
  1142. spin_unlock_irq(&hw->lock);
  1143. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1144. spin_lock_irq(&hw->lock);
  1145. }
  1146. /* all I/Os completed */
  1147. if (list_empty(&scm->active_q))
  1148. return 0;
  1149. /* Else abort */
  1150. if (abort) {
  1151. rv = csio_scsi_abort_io_q(scm, &scm->active_q, 30000);
  1152. if (rv == 0)
  1153. return rv;
  1154. csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
  1155. }
  1156. csio_scsi_cleanup_io_q(scm, &scm->active_q);
  1157. CSIO_DB_ASSERT(list_empty(&scm->active_q));
  1158. return rv;
  1159. }
  1160. /*
  1161. * csio_scsim_cleanup_io_lnode - Cleanup all I/Os of given lnode.
  1162. * @scm: SCSI module.
  1163. * @lnode: lnode
  1164. *
  1165. * Called with lock held, should exit with lock held.
  1166. * Can sleep (with dropped lock) when waiting for I/Os to complete.
  1167. */
  1168. int
  1169. csio_scsim_cleanup_io_lnode(struct csio_scsim *scm, struct csio_lnode *ln)
  1170. {
  1171. struct csio_hw *hw = scm->hw;
  1172. struct csio_scsi_level_data sld;
  1173. int rv;
  1174. int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
  1175. csio_dbg(hw, "Gathering all SCSI I/Os on lnode %p\n", ln);
  1176. sld.level = CSIO_LEV_LNODE;
  1177. sld.lnode = ln;
  1178. INIT_LIST_HEAD(&ln->cmpl_q);
  1179. csio_scsi_gather_active_ios(scm, &sld, &ln->cmpl_q);
  1180. /* No I/Os pending on this lnode */
  1181. if (list_empty(&ln->cmpl_q))
  1182. return 0;
  1183. /* Wait until all active I/Os on this lnode are completed */
  1184. while (!list_empty(&ln->cmpl_q) && count--) {
  1185. spin_unlock_irq(&hw->lock);
  1186. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1187. spin_lock_irq(&hw->lock);
  1188. }
  1189. /* all I/Os completed */
  1190. if (list_empty(&ln->cmpl_q))
  1191. return 0;
  1192. csio_dbg(hw, "Some I/Os pending on ln:%p, aborting them..\n", ln);
  1193. /* I/Os are pending, abort them */
  1194. rv = csio_scsi_abort_io_q(scm, &ln->cmpl_q, 30000);
  1195. if (rv != 0) {
  1196. csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
  1197. csio_scsi_cleanup_io_q(scm, &ln->cmpl_q);
  1198. }
  1199. CSIO_DB_ASSERT(list_empty(&ln->cmpl_q));
  1200. return rv;
  1201. }
  1202. static ssize_t
  1203. csio_show_hw_state(struct device *dev,
  1204. struct device_attribute *attr, char *buf)
  1205. {
  1206. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1207. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1208. if (csio_is_hw_ready(hw))
  1209. return snprintf(buf, PAGE_SIZE, "ready\n");
  1210. else
  1211. return snprintf(buf, PAGE_SIZE, "not ready\n");
  1212. }
  1213. /* Device reset */
  1214. static ssize_t
  1215. csio_device_reset(struct device *dev,
  1216. struct device_attribute *attr, const char *buf, size_t count)
  1217. {
  1218. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1219. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1220. if (*buf != '1')
  1221. return -EINVAL;
  1222. /* Delete NPIV lnodes */
  1223. csio_lnodes_exit(hw, 1);
  1224. /* Block upper IOs */
  1225. csio_lnodes_block_request(hw);
  1226. spin_lock_irq(&hw->lock);
  1227. csio_hw_reset(hw);
  1228. spin_unlock_irq(&hw->lock);
  1229. /* Unblock upper IOs */
  1230. csio_lnodes_unblock_request(hw);
  1231. return count;
  1232. }
  1233. /* disable port */
  1234. static ssize_t
  1235. csio_disable_port(struct device *dev,
  1236. struct device_attribute *attr, const char *buf, size_t count)
  1237. {
  1238. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1239. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1240. bool disable;
  1241. if (*buf == '1' || *buf == '0')
  1242. disable = (*buf == '1') ? true : false;
  1243. else
  1244. return -EINVAL;
  1245. /* Block upper IOs */
  1246. csio_lnodes_block_by_port(hw, ln->portid);
  1247. spin_lock_irq(&hw->lock);
  1248. csio_disable_lnodes(hw, ln->portid, disable);
  1249. spin_unlock_irq(&hw->lock);
  1250. /* Unblock upper IOs */
  1251. csio_lnodes_unblock_by_port(hw, ln->portid);
  1252. return count;
  1253. }
  1254. /* Show debug level */
  1255. static ssize_t
  1256. csio_show_dbg_level(struct device *dev,
  1257. struct device_attribute *attr, char *buf)
  1258. {
  1259. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1260. return snprintf(buf, PAGE_SIZE, "%x\n", ln->params.log_level);
  1261. }
  1262. /* Store debug level */
  1263. static ssize_t
  1264. csio_store_dbg_level(struct device *dev,
  1265. struct device_attribute *attr, const char *buf, size_t count)
  1266. {
  1267. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1268. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1269. uint32_t dbg_level = 0;
  1270. if (!isdigit(buf[0]))
  1271. return -EINVAL;
  1272. if (sscanf(buf, "%i", &dbg_level))
  1273. return -EINVAL;
  1274. ln->params.log_level = dbg_level;
  1275. hw->params.log_level = dbg_level;
  1276. return 0;
  1277. }
  1278. static DEVICE_ATTR(hw_state, S_IRUGO, csio_show_hw_state, NULL);
  1279. static DEVICE_ATTR(device_reset, S_IWUSR, NULL, csio_device_reset);
  1280. static DEVICE_ATTR(disable_port, S_IWUSR, NULL, csio_disable_port);
  1281. static DEVICE_ATTR(dbg_level, S_IRUGO | S_IWUSR, csio_show_dbg_level,
  1282. csio_store_dbg_level);
  1283. static struct device_attribute *csio_fcoe_lport_attrs[] = {
  1284. &dev_attr_hw_state,
  1285. &dev_attr_device_reset,
  1286. &dev_attr_disable_port,
  1287. &dev_attr_dbg_level,
  1288. NULL,
  1289. };
  1290. static ssize_t
  1291. csio_show_num_reg_rnodes(struct device *dev,
  1292. struct device_attribute *attr, char *buf)
  1293. {
  1294. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1295. return snprintf(buf, PAGE_SIZE, "%d\n", ln->num_reg_rnodes);
  1296. }
  1297. static DEVICE_ATTR(num_reg_rnodes, S_IRUGO, csio_show_num_reg_rnodes, NULL);
  1298. static struct device_attribute *csio_fcoe_vport_attrs[] = {
  1299. &dev_attr_num_reg_rnodes,
  1300. &dev_attr_dbg_level,
  1301. NULL,
  1302. };
  1303. static inline uint32_t
  1304. csio_scsi_copy_to_sgl(struct csio_hw *hw, struct csio_ioreq *req)
  1305. {
  1306. struct scsi_cmnd *scmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1307. struct scatterlist *sg;
  1308. uint32_t bytes_left;
  1309. uint32_t bytes_copy;
  1310. uint32_t buf_off = 0;
  1311. uint32_t start_off = 0;
  1312. uint32_t sg_off = 0;
  1313. void *sg_addr;
  1314. void *buf_addr;
  1315. struct csio_dma_buf *dma_buf;
  1316. bytes_left = scsi_bufflen(scmnd);
  1317. sg = scsi_sglist(scmnd);
  1318. dma_buf = (struct csio_dma_buf *)csio_list_next(&req->gen_list);
  1319. /* Copy data from driver buffer to SGs of SCSI CMD */
  1320. while (bytes_left > 0 && sg && dma_buf) {
  1321. if (buf_off >= dma_buf->len) {
  1322. buf_off = 0;
  1323. dma_buf = (struct csio_dma_buf *)
  1324. csio_list_next(dma_buf);
  1325. continue;
  1326. }
  1327. if (start_off >= sg->length) {
  1328. start_off -= sg->length;
  1329. sg = sg_next(sg);
  1330. continue;
  1331. }
  1332. buf_addr = dma_buf->vaddr + buf_off;
  1333. sg_off = sg->offset + start_off;
  1334. bytes_copy = min((dma_buf->len - buf_off),
  1335. sg->length - start_off);
  1336. bytes_copy = min((uint32_t)(PAGE_SIZE - (sg_off & ~PAGE_MASK)),
  1337. bytes_copy);
  1338. sg_addr = kmap_atomic(sg_page(sg) + (sg_off >> PAGE_SHIFT));
  1339. if (!sg_addr) {
  1340. csio_err(hw, "failed to kmap sg:%p of ioreq:%p\n",
  1341. sg, req);
  1342. break;
  1343. }
  1344. csio_dbg(hw, "copy_to_sgl:sg_addr %p sg_off %d buf %p len %d\n",
  1345. sg_addr, sg_off, buf_addr, bytes_copy);
  1346. memcpy(sg_addr + (sg_off & ~PAGE_MASK), buf_addr, bytes_copy);
  1347. kunmap_atomic(sg_addr);
  1348. start_off += bytes_copy;
  1349. buf_off += bytes_copy;
  1350. bytes_left -= bytes_copy;
  1351. }
  1352. if (bytes_left > 0)
  1353. return DID_ERROR;
  1354. else
  1355. return DID_OK;
  1356. }
  1357. /*
  1358. * csio_scsi_err_handler - SCSI error handler.
  1359. * @hw: HW module.
  1360. * @req: IO request.
  1361. *
  1362. */
  1363. static inline void
  1364. csio_scsi_err_handler(struct csio_hw *hw, struct csio_ioreq *req)
  1365. {
  1366. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1367. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  1368. struct fcp_resp_with_ext *fcp_resp;
  1369. struct fcp_resp_rsp_info *rsp_info;
  1370. struct csio_dma_buf *dma_buf;
  1371. uint8_t flags, scsi_status = 0;
  1372. uint32_t host_status = DID_OK;
  1373. uint32_t rsp_len = 0, sns_len = 0;
  1374. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1375. switch (req->wr_status) {
  1376. case FW_HOSTERROR:
  1377. if (unlikely(!csio_is_hw_ready(hw)))
  1378. return;
  1379. host_status = DID_ERROR;
  1380. CSIO_INC_STATS(scm, n_hosterror);
  1381. break;
  1382. case FW_SCSI_RSP_ERR:
  1383. dma_buf = &req->dma_buf;
  1384. fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
  1385. rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
  1386. flags = fcp_resp->resp.fr_flags;
  1387. scsi_status = fcp_resp->resp.fr_status;
  1388. if (flags & FCP_RSP_LEN_VAL) {
  1389. rsp_len = be32_to_cpu(fcp_resp->ext.fr_rsp_len);
  1390. if ((rsp_len != 0 && rsp_len != 4 && rsp_len != 8) ||
  1391. (rsp_info->rsp_code != FCP_TMF_CMPL)) {
  1392. host_status = DID_ERROR;
  1393. goto out;
  1394. }
  1395. }
  1396. if ((flags & FCP_SNS_LEN_VAL) && fcp_resp->ext.fr_sns_len) {
  1397. sns_len = be32_to_cpu(fcp_resp->ext.fr_sns_len);
  1398. if (sns_len > SCSI_SENSE_BUFFERSIZE)
  1399. sns_len = SCSI_SENSE_BUFFERSIZE;
  1400. memcpy(cmnd->sense_buffer,
  1401. &rsp_info->_fr_resvd[0] + rsp_len, sns_len);
  1402. CSIO_INC_STATS(scm, n_autosense);
  1403. }
  1404. scsi_set_resid(cmnd, 0);
  1405. /* Under run */
  1406. if (flags & FCP_RESID_UNDER) {
  1407. scsi_set_resid(cmnd,
  1408. be32_to_cpu(fcp_resp->ext.fr_resid));
  1409. if (!(flags & FCP_SNS_LEN_VAL) &&
  1410. (scsi_status == SAM_STAT_GOOD) &&
  1411. ((scsi_bufflen(cmnd) - scsi_get_resid(cmnd))
  1412. < cmnd->underflow))
  1413. host_status = DID_ERROR;
  1414. } else if (flags & FCP_RESID_OVER)
  1415. host_status = DID_ERROR;
  1416. CSIO_INC_STATS(scm, n_rsperror);
  1417. break;
  1418. case FW_SCSI_OVER_FLOW_ERR:
  1419. csio_warn(hw,
  1420. "Over-flow error,cmnd:0x%x expected len:0x%x"
  1421. " resid:0x%x\n", cmnd->cmnd[0],
  1422. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  1423. host_status = DID_ERROR;
  1424. CSIO_INC_STATS(scm, n_ovflerror);
  1425. break;
  1426. case FW_SCSI_UNDER_FLOW_ERR:
  1427. csio_warn(hw,
  1428. "Under-flow error,cmnd:0x%x expected"
  1429. " len:0x%x resid:0x%x lun:0x%llx ssn:0x%x\n",
  1430. cmnd->cmnd[0], scsi_bufflen(cmnd),
  1431. scsi_get_resid(cmnd), cmnd->device->lun,
  1432. rn->flowid);
  1433. host_status = DID_ERROR;
  1434. CSIO_INC_STATS(scm, n_unflerror);
  1435. break;
  1436. case FW_SCSI_ABORT_REQUESTED:
  1437. case FW_SCSI_ABORTED:
  1438. case FW_SCSI_CLOSE_REQUESTED:
  1439. csio_dbg(hw, "Req %p cmd:%p op:%x %s\n", req, cmnd,
  1440. cmnd->cmnd[0],
  1441. (req->wr_status == FW_SCSI_CLOSE_REQUESTED) ?
  1442. "closed" : "aborted");
  1443. /*
  1444. * csio_eh_abort_handler checks this value to
  1445. * succeed or fail the abort request.
  1446. */
  1447. host_status = DID_REQUEUE;
  1448. if (req->wr_status == FW_SCSI_CLOSE_REQUESTED)
  1449. CSIO_INC_STATS(scm, n_closed);
  1450. else
  1451. CSIO_INC_STATS(scm, n_aborted);
  1452. break;
  1453. case FW_SCSI_ABORT_TIMEDOUT:
  1454. /* FW timed out the abort itself */
  1455. csio_dbg(hw, "FW timed out abort req:%p cmnd:%p status:%x\n",
  1456. req, cmnd, req->wr_status);
  1457. host_status = DID_ERROR;
  1458. CSIO_INC_STATS(scm, n_abrt_timedout);
  1459. break;
  1460. case FW_RDEV_NOT_READY:
  1461. /*
  1462. * In firmware, a RDEV can get into this state
  1463. * temporarily, before moving into dissapeared/lost
  1464. * state. So, the driver should complete the request equivalent
  1465. * to device-disappeared!
  1466. */
  1467. CSIO_INC_STATS(scm, n_rdev_nr_error);
  1468. host_status = DID_ERROR;
  1469. break;
  1470. case FW_ERR_RDEV_LOST:
  1471. CSIO_INC_STATS(scm, n_rdev_lost_error);
  1472. host_status = DID_ERROR;
  1473. break;
  1474. case FW_ERR_RDEV_LOGO:
  1475. CSIO_INC_STATS(scm, n_rdev_logo_error);
  1476. host_status = DID_ERROR;
  1477. break;
  1478. case FW_ERR_RDEV_IMPL_LOGO:
  1479. host_status = DID_ERROR;
  1480. break;
  1481. case FW_ERR_LINK_DOWN:
  1482. CSIO_INC_STATS(scm, n_link_down_error);
  1483. host_status = DID_ERROR;
  1484. break;
  1485. case FW_FCOE_NO_XCHG:
  1486. CSIO_INC_STATS(scm, n_no_xchg_error);
  1487. host_status = DID_ERROR;
  1488. break;
  1489. default:
  1490. csio_err(hw, "Unknown SCSI FW WR status:%d req:%p cmnd:%p\n",
  1491. req->wr_status, req, cmnd);
  1492. CSIO_DB_ASSERT(0);
  1493. CSIO_INC_STATS(scm, n_unknown_error);
  1494. host_status = DID_ERROR;
  1495. break;
  1496. }
  1497. out:
  1498. if (req->nsge > 0)
  1499. scsi_dma_unmap(cmnd);
  1500. cmnd->result = (((host_status) << 16) | scsi_status);
  1501. cmnd->scsi_done(cmnd);
  1502. /* Wake up waiting threads */
  1503. csio_scsi_cmnd(req) = NULL;
  1504. complete_all(&req->cmplobj);
  1505. }
  1506. /*
  1507. * csio_scsi_cbfn - SCSI callback function.
  1508. * @hw: HW module.
  1509. * @req: IO request.
  1510. *
  1511. */
  1512. static void
  1513. csio_scsi_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
  1514. {
  1515. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1516. uint8_t scsi_status = SAM_STAT_GOOD;
  1517. uint32_t host_status = DID_OK;
  1518. if (likely(req->wr_status == FW_SUCCESS)) {
  1519. if (req->nsge > 0) {
  1520. scsi_dma_unmap(cmnd);
  1521. if (req->dcopy)
  1522. host_status = csio_scsi_copy_to_sgl(hw, req);
  1523. }
  1524. cmnd->result = (((host_status) << 16) | scsi_status);
  1525. cmnd->scsi_done(cmnd);
  1526. csio_scsi_cmnd(req) = NULL;
  1527. CSIO_INC_STATS(csio_hw_to_scsim(hw), n_tot_success);
  1528. } else {
  1529. /* Error handling */
  1530. csio_scsi_err_handler(hw, req);
  1531. }
  1532. }
  1533. /**
  1534. * csio_queuecommand - Entry point to kickstart an I/O request.
  1535. * @host: The scsi_host pointer.
  1536. * @cmnd: The I/O request from ML.
  1537. *
  1538. * This routine does the following:
  1539. * - Checks for HW and Rnode module readiness.
  1540. * - Gets a free ioreq structure (which is already initialized
  1541. * to uninit during its allocation).
  1542. * - Maps SG elements.
  1543. * - Initializes ioreq members.
  1544. * - Kicks off the SCSI state machine for this IO.
  1545. * - Returns busy status on error.
  1546. */
  1547. static int
  1548. csio_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmnd)
  1549. {
  1550. struct csio_lnode *ln = shost_priv(host);
  1551. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1552. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1553. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1554. struct csio_ioreq *ioreq = NULL;
  1555. unsigned long flags;
  1556. int nsge = 0;
  1557. int rv = SCSI_MLQUEUE_HOST_BUSY, nr;
  1558. int retval;
  1559. int cpu;
  1560. struct csio_scsi_qset *sqset;
  1561. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1562. if (!blk_rq_cpu_valid(cmnd->request))
  1563. cpu = smp_processor_id();
  1564. else
  1565. cpu = cmnd->request->cpu;
  1566. sqset = &hw->sqset[ln->portid][cpu];
  1567. nr = fc_remote_port_chkready(rport);
  1568. if (nr) {
  1569. cmnd->result = nr;
  1570. CSIO_INC_STATS(scsim, n_rn_nr_error);
  1571. goto err_done;
  1572. }
  1573. if (unlikely(!csio_is_hw_ready(hw))) {
  1574. cmnd->result = (DID_REQUEUE << 16);
  1575. CSIO_INC_STATS(scsim, n_hw_nr_error);
  1576. goto err_done;
  1577. }
  1578. /* Get req->nsge, if there are SG elements to be mapped */
  1579. nsge = scsi_dma_map(cmnd);
  1580. if (unlikely(nsge < 0)) {
  1581. CSIO_INC_STATS(scsim, n_dmamap_error);
  1582. goto err;
  1583. }
  1584. /* Do we support so many mappings? */
  1585. if (unlikely(nsge > scsim->max_sge)) {
  1586. csio_warn(hw,
  1587. "More SGEs than can be supported."
  1588. " SGEs: %d, Max SGEs: %d\n", nsge, scsim->max_sge);
  1589. CSIO_INC_STATS(scsim, n_unsupp_sge_error);
  1590. goto err_dma_unmap;
  1591. }
  1592. /* Get a free ioreq structure - SM is already set to uninit */
  1593. ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
  1594. if (!ioreq) {
  1595. csio_err(hw, "Out of I/O request elements. Active #:%d\n",
  1596. scsim->stats.n_active);
  1597. CSIO_INC_STATS(scsim, n_no_req_error);
  1598. goto err_dma_unmap;
  1599. }
  1600. ioreq->nsge = nsge;
  1601. ioreq->lnode = ln;
  1602. ioreq->rnode = rn;
  1603. ioreq->iq_idx = sqset->iq_idx;
  1604. ioreq->eq_idx = sqset->eq_idx;
  1605. ioreq->wr_status = 0;
  1606. ioreq->drv_status = 0;
  1607. csio_scsi_cmnd(ioreq) = (void *)cmnd;
  1608. ioreq->tmo = 0;
  1609. ioreq->datadir = cmnd->sc_data_direction;
  1610. if (cmnd->sc_data_direction == DMA_TO_DEVICE) {
  1611. CSIO_INC_STATS(ln, n_output_requests);
  1612. ln->stats.n_output_bytes += scsi_bufflen(cmnd);
  1613. } else if (cmnd->sc_data_direction == DMA_FROM_DEVICE) {
  1614. CSIO_INC_STATS(ln, n_input_requests);
  1615. ln->stats.n_input_bytes += scsi_bufflen(cmnd);
  1616. } else
  1617. CSIO_INC_STATS(ln, n_control_requests);
  1618. /* Set cbfn */
  1619. ioreq->io_cbfn = csio_scsi_cbfn;
  1620. /* Needed during abort */
  1621. cmnd->host_scribble = (unsigned char *)ioreq;
  1622. cmnd->SCp.Message = 0;
  1623. /* Kick off SCSI IO SM on the ioreq */
  1624. spin_lock_irqsave(&hw->lock, flags);
  1625. retval = csio_scsi_start_io(ioreq);
  1626. spin_unlock_irqrestore(&hw->lock, flags);
  1627. if (retval != 0) {
  1628. csio_err(hw, "ioreq: %p couldnt be started, status:%d\n",
  1629. ioreq, retval);
  1630. CSIO_INC_STATS(scsim, n_busy_error);
  1631. goto err_put_req;
  1632. }
  1633. return 0;
  1634. err_put_req:
  1635. csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
  1636. err_dma_unmap:
  1637. if (nsge > 0)
  1638. scsi_dma_unmap(cmnd);
  1639. err:
  1640. return rv;
  1641. err_done:
  1642. cmnd->scsi_done(cmnd);
  1643. return 0;
  1644. }
  1645. static int
  1646. csio_do_abrt_cls(struct csio_hw *hw, struct csio_ioreq *ioreq, bool abort)
  1647. {
  1648. int rv;
  1649. int cpu = smp_processor_id();
  1650. struct csio_lnode *ln = ioreq->lnode;
  1651. struct csio_scsi_qset *sqset = &hw->sqset[ln->portid][cpu];
  1652. ioreq->tmo = CSIO_SCSI_ABRT_TMO_MS;
  1653. /*
  1654. * Use current processor queue for posting the abort/close, but retain
  1655. * the ingress queue ID of the original I/O being aborted/closed - we
  1656. * need the abort/close completion to be received on the same queue
  1657. * as the original I/O.
  1658. */
  1659. ioreq->eq_idx = sqset->eq_idx;
  1660. if (abort == SCSI_ABORT)
  1661. rv = csio_scsi_abort(ioreq);
  1662. else
  1663. rv = csio_scsi_close(ioreq);
  1664. return rv;
  1665. }
  1666. static int
  1667. csio_eh_abort_handler(struct scsi_cmnd *cmnd)
  1668. {
  1669. struct csio_ioreq *ioreq;
  1670. struct csio_lnode *ln = shost_priv(cmnd->device->host);
  1671. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1672. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1673. int ready = 0, ret;
  1674. unsigned long tmo = 0;
  1675. int rv;
  1676. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1677. ret = fc_block_scsi_eh(cmnd);
  1678. if (ret)
  1679. return ret;
  1680. ioreq = (struct csio_ioreq *)cmnd->host_scribble;
  1681. if (!ioreq)
  1682. return SUCCESS;
  1683. if (!rn)
  1684. return FAILED;
  1685. csio_dbg(hw,
  1686. "Request to abort ioreq:%p cmd:%p cdb:%08llx"
  1687. " ssni:0x%x lun:%llu iq:0x%x\n",
  1688. ioreq, cmnd, *((uint64_t *)cmnd->cmnd), rn->flowid,
  1689. cmnd->device->lun, csio_q_physiqid(hw, ioreq->iq_idx));
  1690. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) != cmnd) {
  1691. CSIO_INC_STATS(scsim, n_abrt_race_comp);
  1692. return SUCCESS;
  1693. }
  1694. ready = csio_is_lnode_ready(ln);
  1695. tmo = CSIO_SCSI_ABRT_TMO_MS;
  1696. spin_lock_irq(&hw->lock);
  1697. rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
  1698. spin_unlock_irq(&hw->lock);
  1699. if (rv != 0) {
  1700. if (rv == -EINVAL) {
  1701. /* Return success, if abort/close request issued on
  1702. * already completed IO
  1703. */
  1704. return SUCCESS;
  1705. }
  1706. if (ready)
  1707. CSIO_INC_STATS(scsim, n_abrt_busy_error);
  1708. else
  1709. CSIO_INC_STATS(scsim, n_cls_busy_error);
  1710. goto inval_scmnd;
  1711. }
  1712. /* Wait for completion */
  1713. init_completion(&ioreq->cmplobj);
  1714. wait_for_completion_timeout(&ioreq->cmplobj, msecs_to_jiffies(tmo));
  1715. /* FW didnt respond to abort within our timeout */
  1716. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
  1717. csio_err(hw, "Abort timed out -- req: %p\n", ioreq);
  1718. CSIO_INC_STATS(scsim, n_abrt_timedout);
  1719. inval_scmnd:
  1720. if (ioreq->nsge > 0)
  1721. scsi_dma_unmap(cmnd);
  1722. spin_lock_irq(&hw->lock);
  1723. csio_scsi_cmnd(ioreq) = NULL;
  1724. spin_unlock_irq(&hw->lock);
  1725. cmnd->result = (DID_ERROR << 16);
  1726. cmnd->scsi_done(cmnd);
  1727. return FAILED;
  1728. }
  1729. /* FW successfully aborted the request */
  1730. if (host_byte(cmnd->result) == DID_REQUEUE) {
  1731. csio_info(hw,
  1732. "Aborted SCSI command to (%d:%llu) serial#:0x%lx\n",
  1733. cmnd->device->id, cmnd->device->lun,
  1734. cmnd->serial_number);
  1735. return SUCCESS;
  1736. } else {
  1737. csio_info(hw,
  1738. "Failed to abort SCSI command, (%d:%llu) serial#:0x%lx\n",
  1739. cmnd->device->id, cmnd->device->lun,
  1740. cmnd->serial_number);
  1741. return FAILED;
  1742. }
  1743. }
  1744. /*
  1745. * csio_tm_cbfn - TM callback function.
  1746. * @hw: HW module.
  1747. * @req: IO request.
  1748. *
  1749. * Cache the result in 'cmnd', since ioreq will be freed soon
  1750. * after we return from here, and the waiting thread shouldnt trust
  1751. * the ioreq contents.
  1752. */
  1753. static void
  1754. csio_tm_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
  1755. {
  1756. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1757. struct csio_dma_buf *dma_buf;
  1758. uint8_t flags = 0;
  1759. struct fcp_resp_with_ext *fcp_resp;
  1760. struct fcp_resp_rsp_info *rsp_info;
  1761. csio_dbg(hw, "req: %p in csio_tm_cbfn status: %d\n",
  1762. req, req->wr_status);
  1763. /* Cache FW return status */
  1764. cmnd->SCp.Status = req->wr_status;
  1765. /* Special handling based on FCP response */
  1766. /*
  1767. * FW returns us this error, if flags were set. FCP4 says
  1768. * FCP_RSP_LEN_VAL in flags shall be set for TM completions.
  1769. * So if a target were to set this bit, we expect that the
  1770. * rsp_code is set to FCP_TMF_CMPL for a successful TM
  1771. * completion. Any other rsp_code means TM operation failed.
  1772. * If a target were to just ignore setting flags, we treat
  1773. * the TM operation as success, and FW returns FW_SUCCESS.
  1774. */
  1775. if (req->wr_status == FW_SCSI_RSP_ERR) {
  1776. dma_buf = &req->dma_buf;
  1777. fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
  1778. rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
  1779. flags = fcp_resp->resp.fr_flags;
  1780. /* Modify return status if flags indicate success */
  1781. if (flags & FCP_RSP_LEN_VAL)
  1782. if (rsp_info->rsp_code == FCP_TMF_CMPL)
  1783. cmnd->SCp.Status = FW_SUCCESS;
  1784. csio_dbg(hw, "TM FCP rsp code: %d\n", rsp_info->rsp_code);
  1785. }
  1786. /* Wake up the TM handler thread */
  1787. csio_scsi_cmnd(req) = NULL;
  1788. }
  1789. static int
  1790. csio_eh_lun_reset_handler(struct scsi_cmnd *cmnd)
  1791. {
  1792. struct csio_lnode *ln = shost_priv(cmnd->device->host);
  1793. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1794. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1795. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1796. struct csio_ioreq *ioreq = NULL;
  1797. struct csio_scsi_qset *sqset;
  1798. unsigned long flags;
  1799. int retval;
  1800. int count, ret;
  1801. LIST_HEAD(local_q);
  1802. struct csio_scsi_level_data sld;
  1803. if (!rn)
  1804. goto fail;
  1805. csio_dbg(hw, "Request to reset LUN:%llu (ssni:0x%x tgtid:%d)\n",
  1806. cmnd->device->lun, rn->flowid, rn->scsi_id);
  1807. if (!csio_is_lnode_ready(ln)) {
  1808. csio_err(hw,
  1809. "LUN reset cannot be issued on non-ready"
  1810. " local node vnpi:0x%x (LUN:%llu)\n",
  1811. ln->vnp_flowid, cmnd->device->lun);
  1812. goto fail;
  1813. }
  1814. /* Lnode is ready, now wait on rport node readiness */
  1815. ret = fc_block_scsi_eh(cmnd);
  1816. if (ret)
  1817. return ret;
  1818. /*
  1819. * If we have blocked in the previous call, at this point, either the
  1820. * remote node has come back online, or device loss timer has fired
  1821. * and the remote node is destroyed. Allow the LUN reset only for
  1822. * the former case, since LUN reset is a TMF I/O on the wire, and we
  1823. * need a valid session to issue it.
  1824. */
  1825. if (fc_remote_port_chkready(rn->rport)) {
  1826. csio_err(hw,
  1827. "LUN reset cannot be issued on non-ready"
  1828. " remote node ssni:0x%x (LUN:%llu)\n",
  1829. rn->flowid, cmnd->device->lun);
  1830. goto fail;
  1831. }
  1832. /* Get a free ioreq structure - SM is already set to uninit */
  1833. ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
  1834. if (!ioreq) {
  1835. csio_err(hw, "Out of IO request elements. Active # :%d\n",
  1836. scsim->stats.n_active);
  1837. goto fail;
  1838. }
  1839. sqset = &hw->sqset[ln->portid][smp_processor_id()];
  1840. ioreq->nsge = 0;
  1841. ioreq->lnode = ln;
  1842. ioreq->rnode = rn;
  1843. ioreq->iq_idx = sqset->iq_idx;
  1844. ioreq->eq_idx = sqset->eq_idx;
  1845. csio_scsi_cmnd(ioreq) = cmnd;
  1846. cmnd->host_scribble = (unsigned char *)ioreq;
  1847. cmnd->SCp.Status = 0;
  1848. cmnd->SCp.Message = FCP_TMF_LUN_RESET;
  1849. ioreq->tmo = CSIO_SCSI_LUNRST_TMO_MS / 1000;
  1850. /*
  1851. * FW times the LUN reset for ioreq->tmo, so we got to wait a little
  1852. * longer (10s for now) than that to allow FW to return the timed
  1853. * out command.
  1854. */
  1855. count = DIV_ROUND_UP((ioreq->tmo + 10) * 1000, CSIO_SCSI_TM_POLL_MS);
  1856. /* Set cbfn */
  1857. ioreq->io_cbfn = csio_tm_cbfn;
  1858. /* Save of the ioreq info for later use */
  1859. sld.level = CSIO_LEV_LUN;
  1860. sld.lnode = ioreq->lnode;
  1861. sld.rnode = ioreq->rnode;
  1862. sld.oslun = cmnd->device->lun;
  1863. spin_lock_irqsave(&hw->lock, flags);
  1864. /* Kick off TM SM on the ioreq */
  1865. retval = csio_scsi_start_tm(ioreq);
  1866. spin_unlock_irqrestore(&hw->lock, flags);
  1867. if (retval != 0) {
  1868. csio_err(hw, "Failed to issue LUN reset, req:%p, status:%d\n",
  1869. ioreq, retval);
  1870. goto fail_ret_ioreq;
  1871. }
  1872. csio_dbg(hw, "Waiting max %d secs for LUN reset completion\n",
  1873. count * (CSIO_SCSI_TM_POLL_MS / 1000));
  1874. /* Wait for completion */
  1875. while ((((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd)
  1876. && count--)
  1877. msleep(CSIO_SCSI_TM_POLL_MS);
  1878. /* LUN reset timed-out */
  1879. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
  1880. csio_err(hw, "LUN reset (%d:%llu) timed out\n",
  1881. cmnd->device->id, cmnd->device->lun);
  1882. spin_lock_irq(&hw->lock);
  1883. csio_scsi_drvcleanup(ioreq);
  1884. list_del_init(&ioreq->sm.sm_list);
  1885. spin_unlock_irq(&hw->lock);
  1886. goto fail_ret_ioreq;
  1887. }
  1888. /* LUN reset returned, check cached status */
  1889. if (cmnd->SCp.Status != FW_SUCCESS) {
  1890. csio_err(hw, "LUN reset failed (%d:%llu), status: %d\n",
  1891. cmnd->device->id, cmnd->device->lun, cmnd->SCp.Status);
  1892. goto fail;
  1893. }
  1894. /* LUN reset succeeded, Start aborting affected I/Os */
  1895. /*
  1896. * Since the host guarantees during LUN reset that there
  1897. * will not be any more I/Os to that LUN, until the LUN reset
  1898. * completes, we gather pending I/Os after the LUN reset.
  1899. */
  1900. spin_lock_irq(&hw->lock);
  1901. csio_scsi_gather_active_ios(scsim, &sld, &local_q);
  1902. retval = csio_scsi_abort_io_q(scsim, &local_q, 30000);
  1903. spin_unlock_irq(&hw->lock);
  1904. /* Aborts may have timed out */
  1905. if (retval != 0) {
  1906. csio_err(hw,
  1907. "Attempt to abort I/Os during LUN reset of %llu"
  1908. " returned %d\n", cmnd->device->lun, retval);
  1909. /* Return I/Os back to active_q */
  1910. spin_lock_irq(&hw->lock);
  1911. list_splice_tail_init(&local_q, &scsim->active_q);
  1912. spin_unlock_irq(&hw->lock);
  1913. goto fail;
  1914. }
  1915. CSIO_INC_STATS(rn, n_lun_rst);
  1916. csio_info(hw, "LUN reset occurred (%d:%llu)\n",
  1917. cmnd->device->id, cmnd->device->lun);
  1918. return SUCCESS;
  1919. fail_ret_ioreq:
  1920. csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
  1921. fail:
  1922. CSIO_INC_STATS(rn, n_lun_rst_fail);
  1923. return FAILED;
  1924. }
  1925. static int
  1926. csio_slave_alloc(struct scsi_device *sdev)
  1927. {
  1928. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1929. if (!rport || fc_remote_port_chkready(rport))
  1930. return -ENXIO;
  1931. sdev->hostdata = *((struct csio_lnode **)(rport->dd_data));
  1932. return 0;
  1933. }
  1934. static int
  1935. csio_slave_configure(struct scsi_device *sdev)
  1936. {
  1937. scsi_change_queue_depth(sdev, csio_lun_qdepth);
  1938. return 0;
  1939. }
  1940. static void
  1941. csio_slave_destroy(struct scsi_device *sdev)
  1942. {
  1943. sdev->hostdata = NULL;
  1944. }
  1945. static int
  1946. csio_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1947. {
  1948. struct csio_lnode *ln = shost_priv(shost);
  1949. int rv = 1;
  1950. spin_lock_irq(shost->host_lock);
  1951. if (!ln->hwp || csio_list_deleted(&ln->sm.sm_list))
  1952. goto out;
  1953. rv = csio_scan_done(ln, jiffies, time, csio_max_scan_tmo * HZ,
  1954. csio_delta_scan_tmo * HZ);
  1955. out:
  1956. spin_unlock_irq(shost->host_lock);
  1957. return rv;
  1958. }
  1959. struct scsi_host_template csio_fcoe_shost_template = {
  1960. .module = THIS_MODULE,
  1961. .name = CSIO_DRV_DESC,
  1962. .proc_name = KBUILD_MODNAME,
  1963. .queuecommand = csio_queuecommand,
  1964. .eh_abort_handler = csio_eh_abort_handler,
  1965. .eh_device_reset_handler = csio_eh_lun_reset_handler,
  1966. .slave_alloc = csio_slave_alloc,
  1967. .slave_configure = csio_slave_configure,
  1968. .slave_destroy = csio_slave_destroy,
  1969. .scan_finished = csio_scan_finished,
  1970. .this_id = -1,
  1971. .sg_tablesize = CSIO_SCSI_MAX_SGE,
  1972. .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
  1973. .use_clustering = ENABLE_CLUSTERING,
  1974. .shost_attrs = csio_fcoe_lport_attrs,
  1975. .max_sectors = CSIO_MAX_SECTOR_SIZE,
  1976. .use_blk_tags = 1,
  1977. };
  1978. struct scsi_host_template csio_fcoe_shost_vport_template = {
  1979. .module = THIS_MODULE,
  1980. .name = CSIO_DRV_DESC,
  1981. .proc_name = KBUILD_MODNAME,
  1982. .queuecommand = csio_queuecommand,
  1983. .eh_abort_handler = csio_eh_abort_handler,
  1984. .eh_device_reset_handler = csio_eh_lun_reset_handler,
  1985. .slave_alloc = csio_slave_alloc,
  1986. .slave_configure = csio_slave_configure,
  1987. .slave_destroy = csio_slave_destroy,
  1988. .scan_finished = csio_scan_finished,
  1989. .this_id = -1,
  1990. .sg_tablesize = CSIO_SCSI_MAX_SGE,
  1991. .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
  1992. .use_clustering = ENABLE_CLUSTERING,
  1993. .shost_attrs = csio_fcoe_vport_attrs,
  1994. .max_sectors = CSIO_MAX_SECTOR_SIZE,
  1995. .use_blk_tags = 1,
  1996. };
  1997. /*
  1998. * csio_scsi_alloc_ddp_bufs - Allocate buffers for DDP of unaligned SGLs.
  1999. * @scm: SCSI Module
  2000. * @hw: HW device.
  2001. * @buf_size: buffer size
  2002. * @num_buf : Number of buffers.
  2003. *
  2004. * This routine allocates DMA buffers required for SCSI Data xfer, if
  2005. * each SGL buffer for a SCSI Read request posted by SCSI midlayer are
  2006. * not virtually contiguous.
  2007. */
  2008. static int
  2009. csio_scsi_alloc_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw,
  2010. int buf_size, int num_buf)
  2011. {
  2012. int n = 0;
  2013. struct list_head *tmp;
  2014. struct csio_dma_buf *ddp_desc = NULL;
  2015. uint32_t unit_size = 0;
  2016. if (!num_buf)
  2017. return 0;
  2018. if (!buf_size)
  2019. return -EINVAL;
  2020. INIT_LIST_HEAD(&scm->ddp_freelist);
  2021. /* Align buf size to page size */
  2022. buf_size = (buf_size + PAGE_SIZE - 1) & PAGE_MASK;
  2023. /* Initialize dma descriptors */
  2024. for (n = 0; n < num_buf; n++) {
  2025. /* Set unit size to request size */
  2026. unit_size = buf_size;
  2027. ddp_desc = kzalloc(sizeof(struct csio_dma_buf), GFP_KERNEL);
  2028. if (!ddp_desc) {
  2029. csio_err(hw,
  2030. "Failed to allocate ddp descriptors,"
  2031. " Num allocated = %d.\n",
  2032. scm->stats.n_free_ddp);
  2033. goto no_mem;
  2034. }
  2035. /* Allocate Dma buffers for DDP */
  2036. ddp_desc->vaddr = pci_alloc_consistent(hw->pdev, unit_size,
  2037. &ddp_desc->paddr);
  2038. if (!ddp_desc->vaddr) {
  2039. csio_err(hw,
  2040. "SCSI response DMA buffer (ddp) allocation"
  2041. " failed!\n");
  2042. kfree(ddp_desc);
  2043. goto no_mem;
  2044. }
  2045. ddp_desc->len = unit_size;
  2046. /* Added it to scsi ddp freelist */
  2047. list_add_tail(&ddp_desc->list, &scm->ddp_freelist);
  2048. CSIO_INC_STATS(scm, n_free_ddp);
  2049. }
  2050. return 0;
  2051. no_mem:
  2052. /* release dma descs back to freelist and free dma memory */
  2053. list_for_each(tmp, &scm->ddp_freelist) {
  2054. ddp_desc = (struct csio_dma_buf *) tmp;
  2055. tmp = csio_list_prev(tmp);
  2056. pci_free_consistent(hw->pdev, ddp_desc->len, ddp_desc->vaddr,
  2057. ddp_desc->paddr);
  2058. list_del_init(&ddp_desc->list);
  2059. kfree(ddp_desc);
  2060. }
  2061. scm->stats.n_free_ddp = 0;
  2062. return -ENOMEM;
  2063. }
  2064. /*
  2065. * csio_scsi_free_ddp_bufs - free DDP buffers of unaligned SGLs.
  2066. * @scm: SCSI Module
  2067. * @hw: HW device.
  2068. *
  2069. * This routine frees ddp buffers.
  2070. */
  2071. static void
  2072. csio_scsi_free_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw)
  2073. {
  2074. struct list_head *tmp;
  2075. struct csio_dma_buf *ddp_desc;
  2076. /* release dma descs back to freelist and free dma memory */
  2077. list_for_each(tmp, &scm->ddp_freelist) {
  2078. ddp_desc = (struct csio_dma_buf *) tmp;
  2079. tmp = csio_list_prev(tmp);
  2080. pci_free_consistent(hw->pdev, ddp_desc->len, ddp_desc->vaddr,
  2081. ddp_desc->paddr);
  2082. list_del_init(&ddp_desc->list);
  2083. kfree(ddp_desc);
  2084. }
  2085. scm->stats.n_free_ddp = 0;
  2086. }
  2087. /**
  2088. * csio_scsim_init - Initialize SCSI Module
  2089. * @scm: SCSI Module
  2090. * @hw: HW module
  2091. *
  2092. */
  2093. int
  2094. csio_scsim_init(struct csio_scsim *scm, struct csio_hw *hw)
  2095. {
  2096. int i;
  2097. struct csio_ioreq *ioreq;
  2098. struct csio_dma_buf *dma_buf;
  2099. INIT_LIST_HEAD(&scm->active_q);
  2100. scm->hw = hw;
  2101. scm->proto_cmd_len = sizeof(struct fcp_cmnd);
  2102. scm->proto_rsp_len = CSIO_SCSI_RSP_LEN;
  2103. scm->max_sge = CSIO_SCSI_MAX_SGE;
  2104. spin_lock_init(&scm->freelist_lock);
  2105. /* Pre-allocate ioreqs and initialize them */
  2106. INIT_LIST_HEAD(&scm->ioreq_freelist);
  2107. for (i = 0; i < csio_scsi_ioreqs; i++) {
  2108. ioreq = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
  2109. if (!ioreq) {
  2110. csio_err(hw,
  2111. "I/O request element allocation failed, "
  2112. " Num allocated = %d.\n",
  2113. scm->stats.n_free_ioreq);
  2114. goto free_ioreq;
  2115. }
  2116. /* Allocate Dma buffers for Response Payload */
  2117. dma_buf = &ioreq->dma_buf;
  2118. dma_buf->vaddr = pci_pool_alloc(hw->scsi_pci_pool, GFP_KERNEL,
  2119. &dma_buf->paddr);
  2120. if (!dma_buf->vaddr) {
  2121. csio_err(hw,
  2122. "SCSI response DMA buffer allocation"
  2123. " failed!\n");
  2124. kfree(ioreq);
  2125. goto free_ioreq;
  2126. }
  2127. dma_buf->len = scm->proto_rsp_len;
  2128. /* Set state to uninit */
  2129. csio_init_state(&ioreq->sm, csio_scsis_uninit);
  2130. INIT_LIST_HEAD(&ioreq->gen_list);
  2131. init_completion(&ioreq->cmplobj);
  2132. list_add_tail(&ioreq->sm.sm_list, &scm->ioreq_freelist);
  2133. CSIO_INC_STATS(scm, n_free_ioreq);
  2134. }
  2135. if (csio_scsi_alloc_ddp_bufs(scm, hw, PAGE_SIZE, csio_ddp_descs))
  2136. goto free_ioreq;
  2137. return 0;
  2138. free_ioreq:
  2139. /*
  2140. * Free up existing allocations, since an error
  2141. * from here means we are returning for good
  2142. */
  2143. while (!list_empty(&scm->ioreq_freelist)) {
  2144. struct csio_sm *tmp;
  2145. tmp = list_first_entry(&scm->ioreq_freelist,
  2146. struct csio_sm, sm_list);
  2147. list_del_init(&tmp->sm_list);
  2148. ioreq = (struct csio_ioreq *)tmp;
  2149. dma_buf = &ioreq->dma_buf;
  2150. pci_pool_free(hw->scsi_pci_pool, dma_buf->vaddr,
  2151. dma_buf->paddr);
  2152. kfree(ioreq);
  2153. }
  2154. scm->stats.n_free_ioreq = 0;
  2155. return -ENOMEM;
  2156. }
  2157. /**
  2158. * csio_scsim_exit: Uninitialize SCSI Module
  2159. * @scm: SCSI Module
  2160. *
  2161. */
  2162. void
  2163. csio_scsim_exit(struct csio_scsim *scm)
  2164. {
  2165. struct csio_ioreq *ioreq;
  2166. struct csio_dma_buf *dma_buf;
  2167. while (!list_empty(&scm->ioreq_freelist)) {
  2168. struct csio_sm *tmp;
  2169. tmp = list_first_entry(&scm->ioreq_freelist,
  2170. struct csio_sm, sm_list);
  2171. list_del_init(&tmp->sm_list);
  2172. ioreq = (struct csio_ioreq *)tmp;
  2173. dma_buf = &ioreq->dma_buf;
  2174. pci_pool_free(scm->hw->scsi_pci_pool, dma_buf->vaddr,
  2175. dma_buf->paddr);
  2176. kfree(ioreq);
  2177. }
  2178. scm->stats.n_free_ioreq = 0;
  2179. csio_scsi_free_ddp_bufs(scm, scm->hw);
  2180. }