snic_scsi.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  1. /*
  2. * Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/mempool.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/pci.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/delay.h>
  24. #include <linux/gfp.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_host.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_dbg.h>
  31. #include "snic_io.h"
  32. #include "snic.h"
  33. #define snic_cmd_tag(sc) (((struct scsi_cmnd *) sc)->request->tag)
  34. const char *snic_state_str[] = {
  35. [SNIC_INIT] = "SNIC_INIT",
  36. [SNIC_ERROR] = "SNIC_ERROR",
  37. [SNIC_ONLINE] = "SNIC_ONLINE",
  38. [SNIC_OFFLINE] = "SNIC_OFFLINE",
  39. [SNIC_FWRESET] = "SNIC_FWRESET",
  40. };
  41. static const char * const snic_req_state_str[] = {
  42. [SNIC_IOREQ_NOT_INITED] = "SNIC_IOREQ_NOT_INITED",
  43. [SNIC_IOREQ_PENDING] = "SNIC_IOREQ_PENDING",
  44. [SNIC_IOREQ_ABTS_PENDING] = "SNIC_IOREQ_ABTS_PENDING",
  45. [SNIC_IOREQ_ABTS_COMPLETE] = "SNIC_IOREQ_ABTS_COMPLETE",
  46. [SNIC_IOREQ_LR_PENDING] = "SNIC_IOREQ_LR_PENDING",
  47. [SNIC_IOREQ_LR_COMPLETE] = "SNIC_IOREQ_LR_COMPLETE",
  48. [SNIC_IOREQ_COMPLETE] = "SNIC_IOREQ_CMD_COMPLETE",
  49. };
  50. /* snic cmd status strings */
  51. static const char * const snic_io_status_str[] = {
  52. [SNIC_STAT_IO_SUCCESS] = "SNIC_STAT_IO_SUCCESS", /* 0x0 */
  53. [SNIC_STAT_INVALID_HDR] = "SNIC_STAT_INVALID_HDR",
  54. [SNIC_STAT_OUT_OF_RES] = "SNIC_STAT_OUT_OF_RES",
  55. [SNIC_STAT_INVALID_PARM] = "SNIC_STAT_INVALID_PARM",
  56. [SNIC_STAT_REQ_NOT_SUP] = "SNIC_STAT_REQ_NOT_SUP",
  57. [SNIC_STAT_IO_NOT_FOUND] = "SNIC_STAT_IO_NOT_FOUND",
  58. [SNIC_STAT_ABORTED] = "SNIC_STAT_ABORTED",
  59. [SNIC_STAT_TIMEOUT] = "SNIC_STAT_TIMEOUT",
  60. [SNIC_STAT_SGL_INVALID] = "SNIC_STAT_SGL_INVALID",
  61. [SNIC_STAT_DATA_CNT_MISMATCH] = "SNIC_STAT_DATA_CNT_MISMATCH",
  62. [SNIC_STAT_FW_ERR] = "SNIC_STAT_FW_ERR",
  63. [SNIC_STAT_ITMF_REJECT] = "SNIC_STAT_ITMF_REJECT",
  64. [SNIC_STAT_ITMF_FAIL] = "SNIC_STAT_ITMF_FAIL",
  65. [SNIC_STAT_ITMF_INCORRECT_LUN] = "SNIC_STAT_ITMF_INCORRECT_LUN",
  66. [SNIC_STAT_CMND_REJECT] = "SNIC_STAT_CMND_REJECT",
  67. [SNIC_STAT_DEV_OFFLINE] = "SNIC_STAT_DEV_OFFLINE",
  68. [SNIC_STAT_NO_BOOTLUN] = "SNIC_STAT_NO_BOOTLUN",
  69. [SNIC_STAT_SCSI_ERR] = "SNIC_STAT_SCSI_ERR",
  70. [SNIC_STAT_NOT_READY] = "SNIC_STAT_NOT_READY",
  71. [SNIC_STAT_FATAL_ERROR] = "SNIC_STAT_FATAL_ERROR",
  72. };
  73. static void snic_scsi_cleanup(struct snic *, int);
  74. const char *
  75. snic_state_to_str(unsigned int state)
  76. {
  77. if (state >= ARRAY_SIZE(snic_state_str) || !snic_state_str[state])
  78. return "Unknown";
  79. return snic_state_str[state];
  80. }
  81. static const char *
  82. snic_io_status_to_str(unsigned int state)
  83. {
  84. if ((state >= ARRAY_SIZE(snic_io_status_str)) ||
  85. (!snic_io_status_str[state]))
  86. return "Unknown";
  87. return snic_io_status_str[state];
  88. }
  89. static const char *
  90. snic_ioreq_state_to_str(unsigned int state)
  91. {
  92. if (state >= ARRAY_SIZE(snic_req_state_str) ||
  93. !snic_req_state_str[state])
  94. return "Unknown";
  95. return snic_req_state_str[state];
  96. }
  97. static inline spinlock_t *
  98. snic_io_lock_hash(struct snic *snic, struct scsi_cmnd *sc)
  99. {
  100. u32 hash = snic_cmd_tag(sc) & (SNIC_IO_LOCKS - 1);
  101. return &snic->io_req_lock[hash];
  102. }
  103. static inline spinlock_t *
  104. snic_io_lock_tag(struct snic *snic, int tag)
  105. {
  106. return &snic->io_req_lock[tag & (SNIC_IO_LOCKS - 1)];
  107. }
  108. /* snic_release_req_buf : Releases snic_req_info */
  109. static void
  110. snic_release_req_buf(struct snic *snic,
  111. struct snic_req_info *rqi,
  112. struct scsi_cmnd *sc)
  113. {
  114. struct snic_host_req *req = rqi_to_req(rqi);
  115. /* Freeing cmd without marking completion, not okay */
  116. SNIC_BUG_ON(!((CMD_STATE(sc) == SNIC_IOREQ_COMPLETE) ||
  117. (CMD_STATE(sc) == SNIC_IOREQ_ABTS_COMPLETE) ||
  118. (CMD_FLAGS(sc) & SNIC_DEV_RST_NOTSUP) ||
  119. (CMD_FLAGS(sc) & SNIC_IO_INTERNAL_TERM_ISSUED) ||
  120. (CMD_FLAGS(sc) & SNIC_DEV_RST_TERM_ISSUED) ||
  121. (CMD_FLAGS(sc) & SNIC_SCSI_CLEANUP) ||
  122. (CMD_STATE(sc) == SNIC_IOREQ_LR_COMPLETE)));
  123. SNIC_SCSI_DBG(snic->shost,
  124. "Rel_req:sc %p:tag %x:rqi %p:ioreq %p:abt %p:dr %p: state %s:flags 0x%llx\n",
  125. sc, snic_cmd_tag(sc), rqi, rqi->req, rqi->abort_req,
  126. rqi->dr_req, snic_ioreq_state_to_str(CMD_STATE(sc)),
  127. CMD_FLAGS(sc));
  128. if (req->u.icmnd.sense_addr)
  129. dma_unmap_single(&snic->pdev->dev,
  130. le64_to_cpu(req->u.icmnd.sense_addr),
  131. SCSI_SENSE_BUFFERSIZE,
  132. DMA_FROM_DEVICE);
  133. scsi_dma_unmap(sc);
  134. snic_req_free(snic, rqi);
  135. } /* end of snic_release_req_buf */
  136. /*
  137. * snic_queue_icmnd_req : Queues snic_icmnd request
  138. */
  139. static int
  140. snic_queue_icmnd_req(struct snic *snic,
  141. struct snic_req_info *rqi,
  142. struct scsi_cmnd *sc,
  143. int sg_cnt)
  144. {
  145. struct scatterlist *sg;
  146. struct snic_sg_desc *sgd;
  147. dma_addr_t pa = 0;
  148. struct scsi_lun lun;
  149. u16 flags = 0;
  150. int ret = 0;
  151. unsigned int i;
  152. if (sg_cnt) {
  153. flags = SNIC_ICMND_ESGL;
  154. sgd = (struct snic_sg_desc *) req_to_sgl(rqi->req);
  155. for_each_sg(scsi_sglist(sc), sg, sg_cnt, i) {
  156. sgd->addr = cpu_to_le64(sg_dma_address(sg));
  157. sgd->len = cpu_to_le32(sg_dma_len(sg));
  158. sgd->_resvd = 0;
  159. sgd++;
  160. }
  161. }
  162. pa = dma_map_single(&snic->pdev->dev,
  163. sc->sense_buffer,
  164. SCSI_SENSE_BUFFERSIZE,
  165. DMA_FROM_DEVICE);
  166. if (dma_mapping_error(&snic->pdev->dev, pa)) {
  167. SNIC_HOST_ERR(snic->shost,
  168. "QIcmnd:PCI Map Failed for sns buf %p tag %x\n",
  169. sc->sense_buffer, snic_cmd_tag(sc));
  170. ret = -ENOMEM;
  171. return ret;
  172. }
  173. int_to_scsilun(sc->device->lun, &lun);
  174. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  175. flags |= SNIC_ICMND_RD;
  176. if (sc->sc_data_direction == DMA_TO_DEVICE)
  177. flags |= SNIC_ICMND_WR;
  178. /* Initialize icmnd */
  179. snic_icmnd_init(rqi->req,
  180. snic_cmd_tag(sc),
  181. snic->config.hid, /* hid */
  182. (ulong) rqi,
  183. flags, /* command flags */
  184. rqi->tgt_id,
  185. lun.scsi_lun,
  186. sc->cmnd,
  187. sc->cmd_len,
  188. scsi_bufflen(sc),
  189. sg_cnt,
  190. (ulong) req_to_sgl(rqi->req),
  191. pa, /* sense buffer pa */
  192. SCSI_SENSE_BUFFERSIZE);
  193. atomic64_inc(&snic->s_stats.io.active);
  194. ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
  195. if (ret) {
  196. atomic64_dec(&snic->s_stats.io.active);
  197. SNIC_HOST_ERR(snic->shost,
  198. "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
  199. ret);
  200. } else
  201. snic_stats_update_active_ios(&snic->s_stats);
  202. return ret;
  203. } /* end of snic_queue_icmnd_req */
  204. /*
  205. * snic_issue_scsi_req : Prepares IO request and Issues to FW.
  206. */
  207. static int
  208. snic_issue_scsi_req(struct snic *snic,
  209. struct snic_tgt *tgt,
  210. struct scsi_cmnd *sc)
  211. {
  212. struct snic_req_info *rqi = NULL;
  213. int sg_cnt = 0;
  214. int ret = 0;
  215. u32 tag = snic_cmd_tag(sc);
  216. u64 cmd_trc = 0, cmd_st_flags = 0;
  217. spinlock_t *io_lock = NULL;
  218. unsigned long flags;
  219. CMD_STATE(sc) = SNIC_IOREQ_NOT_INITED;
  220. CMD_FLAGS(sc) = SNIC_NO_FLAGS;
  221. sg_cnt = scsi_dma_map(sc);
  222. if (sg_cnt < 0) {
  223. SNIC_TRC((u16)snic->shost->host_no, tag, (ulong) sc, 0,
  224. sc->cmnd[0], sg_cnt, CMD_STATE(sc));
  225. SNIC_HOST_ERR(snic->shost, "issue_sc:Failed to map SG List.\n");
  226. ret = -ENOMEM;
  227. goto issue_sc_end;
  228. }
  229. rqi = snic_req_init(snic, sg_cnt);
  230. if (!rqi) {
  231. scsi_dma_unmap(sc);
  232. ret = -ENOMEM;
  233. goto issue_sc_end;
  234. }
  235. rqi->tgt_id = tgt->id;
  236. rqi->sc = sc;
  237. CMD_STATE(sc) = SNIC_IOREQ_PENDING;
  238. CMD_SP(sc) = (char *) rqi;
  239. cmd_trc = SNIC_TRC_CMD(sc);
  240. CMD_FLAGS(sc) |= (SNIC_IO_INITIALIZED | SNIC_IO_ISSUED);
  241. cmd_st_flags = SNIC_TRC_CMD_STATE_FLAGS(sc);
  242. io_lock = snic_io_lock_hash(snic, sc);
  243. /* create wq desc and enqueue it */
  244. ret = snic_queue_icmnd_req(snic, rqi, sc, sg_cnt);
  245. if (ret) {
  246. SNIC_HOST_ERR(snic->shost,
  247. "issue_sc: icmnd qing Failed for sc %p, err %d\n",
  248. sc, ret);
  249. spin_lock_irqsave(io_lock, flags);
  250. rqi = (struct snic_req_info *) CMD_SP(sc);
  251. CMD_SP(sc) = NULL;
  252. CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
  253. CMD_FLAGS(sc) &= ~SNIC_IO_ISSUED; /* turn off the flag */
  254. spin_unlock_irqrestore(io_lock, flags);
  255. if (rqi)
  256. snic_release_req_buf(snic, rqi, sc);
  257. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, 0, 0, 0,
  258. SNIC_TRC_CMD_STATE_FLAGS(sc));
  259. } else {
  260. u32 io_sz = scsi_bufflen(sc) >> 9;
  261. u32 qtime = jiffies - rqi->start_time;
  262. struct snic_io_stats *iostats = &snic->s_stats.io;
  263. if (io_sz > atomic64_read(&iostats->max_io_sz))
  264. atomic64_set(&iostats->max_io_sz, io_sz);
  265. if (qtime > atomic64_read(&iostats->max_qtime))
  266. atomic64_set(&iostats->max_qtime, qtime);
  267. SNIC_SCSI_DBG(snic->shost,
  268. "issue_sc:sc %p, tag %d queued to WQ.\n",
  269. sc, tag);
  270. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, (ulong) rqi,
  271. sg_cnt, cmd_trc, cmd_st_flags);
  272. }
  273. issue_sc_end:
  274. return ret;
  275. } /* end of snic_issue_scsi_req */
  276. /*
  277. * snic_queuecommand
  278. * Routine to send a scsi cdb to LLD
  279. * Called with host_lock held and interrupts disabled
  280. */
  281. int
  282. snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)
  283. {
  284. struct snic_tgt *tgt = NULL;
  285. struct snic *snic = shost_priv(shost);
  286. int ret;
  287. tgt = starget_to_tgt(scsi_target(sc->device));
  288. ret = snic_tgt_chkready(tgt);
  289. if (ret) {
  290. SNIC_HOST_ERR(shost, "Tgt %p id %d Not Ready.\n", tgt, tgt->id);
  291. atomic64_inc(&snic->s_stats.misc.tgt_not_rdy);
  292. sc->result = ret;
  293. sc->scsi_done(sc);
  294. return 0;
  295. }
  296. if (snic_get_state(snic) != SNIC_ONLINE) {
  297. SNIC_HOST_ERR(shost, "snic state is %s\n",
  298. snic_state_str[snic_get_state(snic)]);
  299. return SCSI_MLQUEUE_HOST_BUSY;
  300. }
  301. atomic_inc(&snic->ios_inflight);
  302. SNIC_SCSI_DBG(shost, "sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n",
  303. sc, snic_cmd_tag(sc), sc->cmnd[0], sc->device->lun);
  304. ret = snic_issue_scsi_req(snic, tgt, sc);
  305. if (ret) {
  306. SNIC_HOST_ERR(shost, "Failed to Q, Scsi Req w/ err %d.\n", ret);
  307. ret = SCSI_MLQUEUE_HOST_BUSY;
  308. }
  309. atomic_dec(&snic->ios_inflight);
  310. return ret;
  311. } /* end of snic_queuecommand */
  312. /*
  313. * snic_process_abts_pending_state:
  314. * caller should hold IO lock
  315. */
  316. static void
  317. snic_proc_tmreq_pending_state(struct snic *snic,
  318. struct scsi_cmnd *sc,
  319. u8 cmpl_status)
  320. {
  321. int state = CMD_STATE(sc);
  322. if (state == SNIC_IOREQ_ABTS_PENDING)
  323. CMD_FLAGS(sc) |= SNIC_IO_ABTS_PENDING;
  324. else if (state == SNIC_IOREQ_LR_PENDING)
  325. CMD_FLAGS(sc) |= SNIC_DEV_RST_PENDING;
  326. else
  327. SNIC_BUG_ON(1);
  328. switch (cmpl_status) {
  329. case SNIC_STAT_IO_SUCCESS:
  330. CMD_FLAGS(sc) |= SNIC_IO_DONE;
  331. break;
  332. case SNIC_STAT_ABORTED:
  333. CMD_FLAGS(sc) |= SNIC_IO_ABORTED;
  334. break;
  335. default:
  336. SNIC_BUG_ON(1);
  337. }
  338. }
  339. /*
  340. * snic_process_io_failed_state:
  341. * Processes IO's error states
  342. */
  343. static void
  344. snic_process_io_failed_state(struct snic *snic,
  345. struct snic_icmnd_cmpl *icmnd_cmpl,
  346. struct scsi_cmnd *sc,
  347. u8 cmpl_stat)
  348. {
  349. int res = 0;
  350. switch (cmpl_stat) {
  351. case SNIC_STAT_TIMEOUT: /* Req was timedout */
  352. atomic64_inc(&snic->s_stats.misc.io_tmo);
  353. res = DID_TIME_OUT;
  354. break;
  355. case SNIC_STAT_ABORTED: /* Req was aborted */
  356. atomic64_inc(&snic->s_stats.misc.io_aborted);
  357. res = DID_ABORT;
  358. break;
  359. case SNIC_STAT_DATA_CNT_MISMATCH:/* Recv/Sent more/less data than exp */
  360. atomic64_inc(&snic->s_stats.misc.data_cnt_mismat);
  361. scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
  362. res = DID_ERROR;
  363. break;
  364. case SNIC_STAT_OUT_OF_RES: /* Out of resources to complete request */
  365. atomic64_inc(&snic->s_stats.fw.out_of_res);
  366. res = DID_REQUEUE;
  367. break;
  368. case SNIC_STAT_IO_NOT_FOUND: /* Requested I/O was not found */
  369. atomic64_inc(&snic->s_stats.io.io_not_found);
  370. res = DID_ERROR;
  371. break;
  372. case SNIC_STAT_SGL_INVALID: /* Req was aborted to due to sgl error*/
  373. atomic64_inc(&snic->s_stats.misc.sgl_inval);
  374. res = DID_ERROR;
  375. break;
  376. case SNIC_STAT_FW_ERR: /* Req terminated due to FW Error */
  377. atomic64_inc(&snic->s_stats.fw.io_errs);
  378. res = DID_ERROR;
  379. break;
  380. case SNIC_STAT_SCSI_ERR: /* FW hits SCSI Error */
  381. atomic64_inc(&snic->s_stats.fw.scsi_errs);
  382. break;
  383. case SNIC_STAT_NOT_READY: /* XPT yet to initialize */
  384. case SNIC_STAT_DEV_OFFLINE: /* Device offline */
  385. res = DID_NO_CONNECT;
  386. break;
  387. case SNIC_STAT_INVALID_HDR: /* Hdr contains invalid data */
  388. case SNIC_STAT_INVALID_PARM: /* Some param in req is invalid */
  389. case SNIC_STAT_REQ_NOT_SUP: /* Req type is not supported */
  390. case SNIC_STAT_CMND_REJECT: /* Req rejected */
  391. case SNIC_STAT_FATAL_ERROR: /* XPT Error */
  392. default:
  393. SNIC_SCSI_DBG(snic->shost,
  394. "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
  395. res = DID_ERROR;
  396. break;
  397. }
  398. SNIC_HOST_ERR(snic->shost, "fw returns failed status %s flags 0x%llx\n",
  399. snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
  400. /* Set sc->result */
  401. sc->result = (res << 16) | icmnd_cmpl->scsi_status;
  402. } /* end of snic_process_io_failed_state */
  403. /*
  404. * snic_tmreq_pending : is task management in progress.
  405. */
  406. static int
  407. snic_tmreq_pending(struct scsi_cmnd *sc)
  408. {
  409. int state = CMD_STATE(sc);
  410. return ((state == SNIC_IOREQ_ABTS_PENDING) ||
  411. (state == SNIC_IOREQ_LR_PENDING));
  412. }
  413. /*
  414. * snic_process_icmnd_cmpl_status:
  415. * Caller should hold io_lock
  416. */
  417. static int
  418. snic_process_icmnd_cmpl_status(struct snic *snic,
  419. struct snic_icmnd_cmpl *icmnd_cmpl,
  420. u8 cmpl_stat,
  421. struct scsi_cmnd *sc)
  422. {
  423. u8 scsi_stat = icmnd_cmpl->scsi_status;
  424. u64 xfer_len = 0;
  425. int ret = 0;
  426. /* Mark the IO as complete */
  427. CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
  428. if (likely(cmpl_stat == SNIC_STAT_IO_SUCCESS)) {
  429. sc->result = (DID_OK << 16) | scsi_stat;
  430. xfer_len = scsi_bufflen(sc);
  431. /* Update SCSI Cmd with resid value */
  432. scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
  433. if (icmnd_cmpl->flags & SNIC_ICMND_CMPL_UNDR_RUN) {
  434. xfer_len -= le32_to_cpu(icmnd_cmpl->resid);
  435. atomic64_inc(&snic->s_stats.misc.io_under_run);
  436. }
  437. if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
  438. atomic64_inc(&snic->s_stats.misc.qfull);
  439. ret = 0;
  440. } else {
  441. snic_process_io_failed_state(snic, icmnd_cmpl, sc, cmpl_stat);
  442. atomic64_inc(&snic->s_stats.io.fail);
  443. SNIC_HOST_ERR(snic->shost,
  444. "icmnd_cmpl: IO Failed : Hdr Status %s flags 0x%llx\n",
  445. snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
  446. ret = 1;
  447. }
  448. return ret;
  449. } /* end of snic_process_icmnd_cmpl_status */
  450. /*
  451. * snic_icmnd_cmpl_handler
  452. * Routine to handle icmnd completions
  453. */
  454. static void
  455. snic_icmnd_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
  456. {
  457. u8 typ, hdr_stat;
  458. u32 cmnd_id, hid;
  459. ulong ctx;
  460. struct scsi_cmnd *sc = NULL;
  461. struct snic_icmnd_cmpl *icmnd_cmpl = NULL;
  462. struct snic_host_req *req = NULL;
  463. struct snic_req_info *rqi = NULL;
  464. unsigned long flags, start_time;
  465. spinlock_t *io_lock;
  466. u8 sc_stat = 0;
  467. snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
  468. icmnd_cmpl = &fwreq->u.icmnd_cmpl;
  469. sc_stat = icmnd_cmpl->scsi_status;
  470. SNIC_SCSI_DBG(snic->shost,
  471. "Icmnd_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,i ctx = %lx\n",
  472. typ, hdr_stat, cmnd_id, hid, ctx);
  473. if (cmnd_id >= snic->max_tag_id) {
  474. SNIC_HOST_ERR(snic->shost,
  475. "Icmnd_cmpl:Tag Error:Out of Range Tag %d, hdr status = %s\n",
  476. cmnd_id, snic_io_status_to_str(hdr_stat));
  477. return;
  478. }
  479. sc = scsi_host_find_tag(snic->shost, cmnd_id);
  480. WARN_ON_ONCE(!sc);
  481. if (!sc) {
  482. atomic64_inc(&snic->s_stats.io.sc_null);
  483. SNIC_HOST_ERR(snic->shost,
  484. "Icmnd_cmpl: Scsi Cmnd Not found, sc = NULL Hdr Status = %s tag = 0x%x fwreq = 0x%p\n",
  485. snic_io_status_to_str(hdr_stat),
  486. cmnd_id,
  487. fwreq);
  488. SNIC_TRC(snic->shost->host_no, cmnd_id, 0,
  489. ((u64)hdr_stat << 16 |
  490. (u64)sc_stat << 8 | (u64)icmnd_cmpl->flags),
  491. (ulong) fwreq, le32_to_cpu(icmnd_cmpl->resid), ctx);
  492. return;
  493. }
  494. io_lock = snic_io_lock_hash(snic, sc);
  495. spin_lock_irqsave(io_lock, flags);
  496. rqi = (struct snic_req_info *) CMD_SP(sc);
  497. SNIC_SCSI_DBG(snic->shost,
  498. "Icmnd_cmpl:lun %lld sc %p cmd %xtag %d flags 0x%llx rqi %p\n",
  499. sc->device->lun, sc, sc->cmnd[0], snic_cmd_tag(sc),
  500. CMD_FLAGS(sc), rqi);
  501. if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
  502. spin_unlock_irqrestore(io_lock, flags);
  503. return;
  504. }
  505. SNIC_BUG_ON(rqi != (struct snic_req_info *)ctx);
  506. WARN_ON_ONCE(req);
  507. if (!rqi) {
  508. atomic64_inc(&snic->s_stats.io.req_null);
  509. CMD_FLAGS(sc) |= SNIC_IO_REQ_NULL;
  510. spin_unlock_irqrestore(io_lock, flags);
  511. SNIC_HOST_ERR(snic->shost,
  512. "Icmnd_cmpl:Host Req Not Found(null), Hdr Status %s, Tag 0x%x, sc 0x%p flags 0x%llx\n",
  513. snic_io_status_to_str(hdr_stat),
  514. cmnd_id, sc, CMD_FLAGS(sc));
  515. return;
  516. }
  517. rqi = (struct snic_req_info *) ctx;
  518. start_time = rqi->start_time;
  519. /* firmware completed the io */
  520. rqi->io_cmpl = 1;
  521. /*
  522. * if SCSI-ML has already issued abort on this command,
  523. * ignore completion of the IO. The abts path will clean it up
  524. */
  525. if (unlikely(snic_tmreq_pending(sc))) {
  526. snic_proc_tmreq_pending_state(snic, sc, hdr_stat);
  527. spin_unlock_irqrestore(io_lock, flags);
  528. snic_stats_update_io_cmpl(&snic->s_stats);
  529. /* Expected value is SNIC_STAT_ABORTED */
  530. if (likely(hdr_stat == SNIC_STAT_ABORTED))
  531. return;
  532. SNIC_SCSI_DBG(snic->shost,
  533. "icmnd_cmpl:TM Req Pending(%s), Hdr Status %s sc 0x%p scsi status %x resid %d flags 0x%llx\n",
  534. snic_ioreq_state_to_str(CMD_STATE(sc)),
  535. snic_io_status_to_str(hdr_stat),
  536. sc, sc_stat, le32_to_cpu(icmnd_cmpl->resid),
  537. CMD_FLAGS(sc));
  538. SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
  539. jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
  540. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  541. return;
  542. }
  543. if (snic_process_icmnd_cmpl_status(snic, icmnd_cmpl, hdr_stat, sc)) {
  544. scsi_print_command(sc);
  545. SNIC_HOST_ERR(snic->shost,
  546. "icmnd_cmpl:IO Failed, sc 0x%p Tag %d Cmd %x Hdr Status %s flags 0x%llx\n",
  547. sc, sc->cmnd[0], cmnd_id,
  548. snic_io_status_to_str(hdr_stat), CMD_FLAGS(sc));
  549. }
  550. /* Break link with the SCSI Command */
  551. CMD_SP(sc) = NULL;
  552. CMD_FLAGS(sc) |= SNIC_IO_DONE;
  553. spin_unlock_irqrestore(io_lock, flags);
  554. /* For now, consider only successful IO. */
  555. snic_calc_io_process_time(snic, rqi);
  556. snic_release_req_buf(snic, rqi, sc);
  557. SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
  558. jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
  559. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  560. if (sc->scsi_done)
  561. sc->scsi_done(sc);
  562. snic_stats_update_io_cmpl(&snic->s_stats);
  563. } /* end of snic_icmnd_cmpl_handler */
  564. static void
  565. snic_proc_dr_cmpl_locked(struct snic *snic,
  566. struct snic_fw_req *fwreq,
  567. u8 cmpl_stat,
  568. u32 cmnd_id,
  569. struct scsi_cmnd *sc)
  570. {
  571. struct snic_req_info *rqi = (struct snic_req_info *) CMD_SP(sc);
  572. u32 start_time = rqi->start_time;
  573. CMD_LR_STATUS(sc) = cmpl_stat;
  574. SNIC_SCSI_DBG(snic->shost, "itmf_cmpl: Cmd State = %s\n",
  575. snic_ioreq_state_to_str(CMD_STATE(sc)));
  576. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
  577. CMD_FLAGS(sc) |= SNIC_DEV_RST_ABTS_PENDING;
  578. SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
  579. jiffies_to_msecs(jiffies - start_time),
  580. (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
  581. SNIC_SCSI_DBG(snic->shost,
  582. "itmf_cmpl: Terminate Pending Dev Reset Cmpl Recvd.id %x, status %s flags 0x%llx\n",
  583. (int)(cmnd_id & SNIC_TAG_MASK),
  584. snic_io_status_to_str(cmpl_stat),
  585. CMD_FLAGS(sc));
  586. return;
  587. }
  588. if (CMD_FLAGS(sc) & SNIC_DEV_RST_TIMEDOUT) {
  589. SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
  590. jiffies_to_msecs(jiffies - start_time),
  591. (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
  592. SNIC_SCSI_DBG(snic->shost,
  593. "itmf_cmpl:Dev Reset Completion Received after timeout. id %d cmpl status %s flags 0x%llx\n",
  594. (int)(cmnd_id & SNIC_TAG_MASK),
  595. snic_io_status_to_str(cmpl_stat),
  596. CMD_FLAGS(sc));
  597. return;
  598. }
  599. CMD_STATE(sc) = SNIC_IOREQ_LR_COMPLETE;
  600. CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
  601. SNIC_SCSI_DBG(snic->shost,
  602. "itmf_cmpl:Dev Reset Cmpl Recvd id %d cmpl status %s flags 0x%llx\n",
  603. (int)(cmnd_id & SNIC_TAG_MASK),
  604. snic_io_status_to_str(cmpl_stat),
  605. CMD_FLAGS(sc));
  606. if (rqi->dr_done)
  607. complete(rqi->dr_done);
  608. } /* end of snic_proc_dr_cmpl_locked */
  609. /*
  610. * snic_update_abort_stats : Updates abort stats based on completion status.
  611. */
  612. static void
  613. snic_update_abort_stats(struct snic *snic, u8 cmpl_stat)
  614. {
  615. struct snic_abort_stats *abt_stats = &snic->s_stats.abts;
  616. SNIC_SCSI_DBG(snic->shost, "Updating Abort stats.\n");
  617. switch (cmpl_stat) {
  618. case SNIC_STAT_IO_SUCCESS:
  619. break;
  620. case SNIC_STAT_TIMEOUT:
  621. atomic64_inc(&abt_stats->fw_tmo);
  622. break;
  623. case SNIC_STAT_IO_NOT_FOUND:
  624. atomic64_inc(&abt_stats->io_not_found);
  625. break;
  626. default:
  627. atomic64_inc(&abt_stats->fail);
  628. break;
  629. }
  630. }
  631. static int
  632. snic_process_itmf_cmpl(struct snic *snic,
  633. struct snic_fw_req *fwreq,
  634. u32 cmnd_id,
  635. u8 cmpl_stat,
  636. struct scsi_cmnd *sc)
  637. {
  638. struct snic_req_info *rqi = NULL;
  639. u32 tm_tags = 0;
  640. spinlock_t *io_lock = NULL;
  641. unsigned long flags;
  642. u32 start_time = 0;
  643. int ret = 0;
  644. io_lock = snic_io_lock_hash(snic, sc);
  645. spin_lock_irqsave(io_lock, flags);
  646. if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
  647. spin_unlock_irqrestore(io_lock, flags);
  648. return ret;
  649. }
  650. rqi = (struct snic_req_info *) CMD_SP(sc);
  651. WARN_ON_ONCE(!rqi);
  652. if (!rqi) {
  653. atomic64_inc(&snic->s_stats.io.req_null);
  654. spin_unlock_irqrestore(io_lock, flags);
  655. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  656. SNIC_HOST_ERR(snic->shost,
  657. "itmf_cmpl: rqi is null,Hdr stat = %s Tag = 0x%x sc = 0x%p flags 0x%llx\n",
  658. snic_io_status_to_str(cmpl_stat), cmnd_id, sc,
  659. CMD_FLAGS(sc));
  660. return ret;
  661. }
  662. /* Extract task management flags */
  663. tm_tags = cmnd_id & ~(SNIC_TAG_MASK);
  664. start_time = rqi->start_time;
  665. cmnd_id &= (SNIC_TAG_MASK);
  666. switch (tm_tags) {
  667. case SNIC_TAG_ABORT:
  668. /* Abort only issued on cmd */
  669. snic_update_abort_stats(snic, cmpl_stat);
  670. if (CMD_STATE(sc) != SNIC_IOREQ_ABTS_PENDING) {
  671. /* This is a late completion. Ignore it. */
  672. ret = -1;
  673. spin_unlock_irqrestore(io_lock, flags);
  674. break;
  675. }
  676. CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
  677. CMD_ABTS_STATUS(sc) = cmpl_stat;
  678. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
  679. SNIC_SCSI_DBG(snic->shost,
  680. "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
  681. cmnd_id,
  682. snic_io_status_to_str(cmpl_stat),
  683. CMD_FLAGS(sc));
  684. /*
  685. * If scsi_eh thread is blocked waiting for abts complete,
  686. * signal completion to it. IO will be cleaned in the thread,
  687. * else clean it in this context.
  688. */
  689. if (rqi->abts_done) {
  690. complete(rqi->abts_done);
  691. spin_unlock_irqrestore(io_lock, flags);
  692. break; /* jump out */
  693. }
  694. CMD_SP(sc) = NULL;
  695. sc->result = (DID_ERROR << 16);
  696. SNIC_SCSI_DBG(snic->shost,
  697. "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
  698. sc, CMD_FLAGS(sc));
  699. spin_unlock_irqrestore(io_lock, flags);
  700. snic_release_req_buf(snic, rqi, sc);
  701. if (sc->scsi_done) {
  702. SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
  703. jiffies_to_msecs(jiffies - start_time),
  704. (ulong) fwreq, SNIC_TRC_CMD(sc),
  705. SNIC_TRC_CMD_STATE_FLAGS(sc));
  706. sc->scsi_done(sc);
  707. }
  708. break;
  709. case SNIC_TAG_DEV_RST:
  710. case SNIC_TAG_DEV_RST | SNIC_TAG_IOCTL_DEV_RST:
  711. snic_proc_dr_cmpl_locked(snic, fwreq, cmpl_stat, cmnd_id, sc);
  712. spin_unlock_irqrestore(io_lock, flags);
  713. ret = 0;
  714. break;
  715. case SNIC_TAG_ABORT | SNIC_TAG_DEV_RST:
  716. /* Abort and terminate completion of device reset req */
  717. CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
  718. CMD_ABTS_STATUS(sc) = cmpl_stat;
  719. CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
  720. SNIC_SCSI_DBG(snic->shost,
  721. "itmf_cmpl:dev reset abts cmpl recvd. id %d status %s flags 0x%llx\n",
  722. cmnd_id, snic_io_status_to_str(cmpl_stat),
  723. CMD_FLAGS(sc));
  724. if (rqi->abts_done)
  725. complete(rqi->abts_done);
  726. spin_unlock_irqrestore(io_lock, flags);
  727. break;
  728. default:
  729. spin_unlock_irqrestore(io_lock, flags);
  730. SNIC_HOST_ERR(snic->shost,
  731. "itmf_cmpl: Unknown TM tag bit 0x%x\n", tm_tags);
  732. SNIC_HOST_ERR(snic->shost,
  733. "itmf_cmpl:Unexpected itmf io stat %s Tag = 0x%x flags 0x%llx\n",
  734. snic_ioreq_state_to_str(CMD_STATE(sc)),
  735. cmnd_id,
  736. CMD_FLAGS(sc));
  737. ret = -1;
  738. SNIC_BUG_ON(1);
  739. break;
  740. }
  741. return ret;
  742. } /* end of snic_process_itmf_cmpl_status */
  743. /*
  744. * snic_itmf_cmpl_handler.
  745. * Routine to handle itmf completions.
  746. */
  747. static void
  748. snic_itmf_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
  749. {
  750. struct scsi_cmnd *sc = NULL;
  751. struct snic_req_info *rqi = NULL;
  752. struct snic_itmf_cmpl *itmf_cmpl = NULL;
  753. ulong ctx;
  754. u32 cmnd_id;
  755. u32 hid;
  756. u8 typ;
  757. u8 hdr_stat;
  758. snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
  759. SNIC_SCSI_DBG(snic->shost,
  760. "Itmf_cmpl: %s: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,ctx = %lx\n",
  761. __func__, typ, hdr_stat, cmnd_id, hid, ctx);
  762. itmf_cmpl = &fwreq->u.itmf_cmpl;
  763. SNIC_SCSI_DBG(snic->shost,
  764. "Itmf_cmpl: nterm %u , flags 0x%x\n",
  765. le32_to_cpu(itmf_cmpl->nterminated), itmf_cmpl->flags);
  766. /* spl case, dev reset issued through ioctl */
  767. if (cmnd_id & SNIC_TAG_IOCTL_DEV_RST) {
  768. rqi = (struct snic_req_info *) ctx;
  769. sc = rqi->sc;
  770. goto ioctl_dev_rst;
  771. }
  772. if ((cmnd_id & SNIC_TAG_MASK) >= snic->max_tag_id) {
  773. SNIC_HOST_ERR(snic->shost,
  774. "Itmf_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
  775. cmnd_id, snic_io_status_to_str(hdr_stat));
  776. SNIC_BUG_ON(1);
  777. return;
  778. }
  779. sc = scsi_host_find_tag(snic->shost, cmnd_id & SNIC_TAG_MASK);
  780. WARN_ON_ONCE(!sc);
  781. ioctl_dev_rst:
  782. if (!sc) {
  783. atomic64_inc(&snic->s_stats.io.sc_null);
  784. SNIC_HOST_ERR(snic->shost,
  785. "Itmf_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
  786. snic_io_status_to_str(hdr_stat), cmnd_id);
  787. return;
  788. }
  789. snic_process_itmf_cmpl(snic, fwreq, cmnd_id, hdr_stat, sc);
  790. } /* end of snic_itmf_cmpl_handler */
  791. static void
  792. snic_hba_reset_scsi_cleanup(struct snic *snic, struct scsi_cmnd *sc)
  793. {
  794. struct snic_stats *st = &snic->s_stats;
  795. long act_ios = 0, act_fwreqs = 0;
  796. SNIC_SCSI_DBG(snic->shost, "HBA Reset scsi cleanup.\n");
  797. snic_scsi_cleanup(snic, snic_cmd_tag(sc));
  798. /* Update stats on pending IOs */
  799. act_ios = atomic64_read(&st->io.active);
  800. atomic64_add(act_ios, &st->io.compl);
  801. atomic64_sub(act_ios, &st->io.active);
  802. act_fwreqs = atomic64_read(&st->fw.actv_reqs);
  803. atomic64_sub(act_fwreqs, &st->fw.actv_reqs);
  804. }
  805. /*
  806. * snic_hba_reset_cmpl_handler :
  807. *
  808. * Notes :
  809. * 1. Cleanup all the scsi cmds, release all snic specific cmds
  810. * 2. Issue Report Targets in case of SAN targets
  811. */
  812. static int
  813. snic_hba_reset_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
  814. {
  815. ulong ctx;
  816. u32 cmnd_id;
  817. u32 hid;
  818. u8 typ;
  819. u8 hdr_stat;
  820. struct scsi_cmnd *sc = NULL;
  821. struct snic_req_info *rqi = NULL;
  822. spinlock_t *io_lock = NULL;
  823. unsigned long flags, gflags;
  824. int ret = 0;
  825. snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
  826. SNIC_HOST_INFO(snic->shost,
  827. "reset_cmpl:Tag %d ctx %lx cmpl status %s HBA Reset Completion received.\n",
  828. cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
  829. SNIC_SCSI_DBG(snic->shost,
  830. "reset_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
  831. typ, hdr_stat, cmnd_id, hid, ctx);
  832. /* spl case, host reset issued through ioctl */
  833. if (cmnd_id == SCSI_NO_TAG) {
  834. rqi = (struct snic_req_info *) ctx;
  835. SNIC_HOST_INFO(snic->shost,
  836. "reset_cmpl:Tag %d ctx %lx cmpl stat %s\n",
  837. cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
  838. sc = rqi->sc;
  839. goto ioctl_hba_rst;
  840. }
  841. if (cmnd_id >= snic->max_tag_id) {
  842. SNIC_HOST_ERR(snic->shost,
  843. "reset_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
  844. cmnd_id, snic_io_status_to_str(hdr_stat));
  845. SNIC_BUG_ON(1);
  846. return 1;
  847. }
  848. sc = scsi_host_find_tag(snic->shost, cmnd_id);
  849. ioctl_hba_rst:
  850. if (!sc) {
  851. atomic64_inc(&snic->s_stats.io.sc_null);
  852. SNIC_HOST_ERR(snic->shost,
  853. "reset_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
  854. snic_io_status_to_str(hdr_stat), cmnd_id);
  855. ret = 1;
  856. return ret;
  857. }
  858. SNIC_HOST_INFO(snic->shost,
  859. "reset_cmpl: sc %p rqi %p Tag %d flags 0x%llx\n",
  860. sc, rqi, cmnd_id, CMD_FLAGS(sc));
  861. io_lock = snic_io_lock_hash(snic, sc);
  862. spin_lock_irqsave(io_lock, flags);
  863. if (!snic->remove_wait) {
  864. spin_unlock_irqrestore(io_lock, flags);
  865. SNIC_HOST_ERR(snic->shost,
  866. "reset_cmpl:host reset completed after timeout\n");
  867. ret = 1;
  868. return ret;
  869. }
  870. rqi = (struct snic_req_info *) CMD_SP(sc);
  871. WARN_ON_ONCE(!rqi);
  872. if (!rqi) {
  873. atomic64_inc(&snic->s_stats.io.req_null);
  874. spin_unlock_irqrestore(io_lock, flags);
  875. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  876. SNIC_HOST_ERR(snic->shost,
  877. "reset_cmpl: rqi is null,Hdr stat %s Tag 0x%x sc 0x%p flags 0x%llx\n",
  878. snic_io_status_to_str(hdr_stat), cmnd_id, sc,
  879. CMD_FLAGS(sc));
  880. ret = 1;
  881. return ret;
  882. }
  883. /* stats */
  884. spin_unlock_irqrestore(io_lock, flags);
  885. /* scsi cleanup */
  886. snic_hba_reset_scsi_cleanup(snic, sc);
  887. SNIC_BUG_ON(snic_get_state(snic) != SNIC_OFFLINE &&
  888. snic_get_state(snic) != SNIC_FWRESET);
  889. /* Careful locking between snic_lock and io lock */
  890. spin_lock_irqsave(io_lock, flags);
  891. spin_lock_irqsave(&snic->snic_lock, gflags);
  892. if (snic_get_state(snic) == SNIC_FWRESET)
  893. snic_set_state(snic, SNIC_ONLINE);
  894. spin_unlock_irqrestore(&snic->snic_lock, gflags);
  895. if (snic->remove_wait)
  896. complete(snic->remove_wait);
  897. spin_unlock_irqrestore(io_lock, flags);
  898. atomic64_inc(&snic->s_stats.reset.hba_reset_cmpl);
  899. ret = 0;
  900. /* Rediscovery is for SAN */
  901. if (snic->config.xpt_type == SNIC_DAS)
  902. return ret;
  903. SNIC_SCSI_DBG(snic->shost, "reset_cmpl: Queuing discovery work.\n");
  904. queue_work(snic_glob->event_q, &snic->disc_work);
  905. return ret;
  906. }
  907. static void
  908. snic_msg_ack_handler(struct snic *snic, struct snic_fw_req *fwreq)
  909. {
  910. SNIC_HOST_INFO(snic->shost, "Message Ack Received.\n");
  911. SNIC_ASSERT_NOT_IMPL(1);
  912. }
  913. static void
  914. snic_aen_handler(struct snic *snic, struct snic_fw_req *fwreq)
  915. {
  916. u8 typ, hdr_stat;
  917. u32 cmnd_id, hid;
  918. ulong ctx;
  919. struct snic_async_evnotify *aen = &fwreq->u.async_ev;
  920. u32 event_id = 0;
  921. snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
  922. SNIC_SCSI_DBG(snic->shost,
  923. "aen: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
  924. typ, hdr_stat, cmnd_id, hid, ctx);
  925. event_id = le32_to_cpu(aen->ev_id);
  926. switch (event_id) {
  927. case SNIC_EV_TGT_OFFLINE:
  928. SNIC_HOST_INFO(snic->shost, "aen:TGT_OFFLINE Event Recvd.\n");
  929. break;
  930. case SNIC_EV_TGT_ONLINE:
  931. SNIC_HOST_INFO(snic->shost, "aen:TGT_ONLINE Event Recvd.\n");
  932. break;
  933. case SNIC_EV_LUN_OFFLINE:
  934. SNIC_HOST_INFO(snic->shost, "aen:LUN_OFFLINE Event Recvd.\n");
  935. break;
  936. case SNIC_EV_LUN_ONLINE:
  937. SNIC_HOST_INFO(snic->shost, "aen:LUN_ONLINE Event Recvd.\n");
  938. break;
  939. case SNIC_EV_CONF_CHG:
  940. SNIC_HOST_INFO(snic->shost, "aen:Config Change Event Recvd.\n");
  941. break;
  942. case SNIC_EV_TGT_ADDED:
  943. SNIC_HOST_INFO(snic->shost, "aen:TGT_ADD Event Recvd.\n");
  944. break;
  945. case SNIC_EV_TGT_DELTD:
  946. SNIC_HOST_INFO(snic->shost, "aen:TGT_DEL Event Recvd.\n");
  947. break;
  948. case SNIC_EV_LUN_ADDED:
  949. SNIC_HOST_INFO(snic->shost, "aen:LUN_ADD Event Recvd.\n");
  950. break;
  951. case SNIC_EV_LUN_DELTD:
  952. SNIC_HOST_INFO(snic->shost, "aen:LUN_DEL Event Recvd.\n");
  953. break;
  954. case SNIC_EV_DISC_CMPL:
  955. SNIC_HOST_INFO(snic->shost, "aen:DISC_CMPL Event Recvd.\n");
  956. break;
  957. default:
  958. SNIC_HOST_INFO(snic->shost, "aen:Unknown Event Recvd.\n");
  959. SNIC_BUG_ON(1);
  960. break;
  961. }
  962. SNIC_ASSERT_NOT_IMPL(1);
  963. } /* end of snic_aen_handler */
  964. /*
  965. * snic_io_cmpl_handler
  966. * Routine to process CQ entries(IO Completions) posted by fw.
  967. */
  968. static int
  969. snic_io_cmpl_handler(struct vnic_dev *vdev,
  970. unsigned int cq_idx,
  971. struct snic_fw_req *fwreq)
  972. {
  973. struct snic *snic = svnic_dev_priv(vdev);
  974. u64 start = jiffies, cmpl_time;
  975. snic_print_desc(__func__, (char *)fwreq, sizeof(*fwreq));
  976. /* Update FW Stats */
  977. if ((fwreq->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL) &&
  978. (fwreq->hdr.type <= SNIC_RSP_BOOT_LUNS_CMPL))
  979. atomic64_dec(&snic->s_stats.fw.actv_reqs);
  980. SNIC_BUG_ON((fwreq->hdr.type > SNIC_RSP_BOOT_LUNS_CMPL) &&
  981. (fwreq->hdr.type < SNIC_MSG_ASYNC_EVNOTIFY));
  982. /* Check for snic subsys errors */
  983. switch (fwreq->hdr.status) {
  984. case SNIC_STAT_NOT_READY: /* XPT yet to initialize */
  985. SNIC_HOST_ERR(snic->shost,
  986. "sNIC SubSystem is NOT Ready.\n");
  987. break;
  988. case SNIC_STAT_FATAL_ERROR: /* XPT Error */
  989. SNIC_HOST_ERR(snic->shost,
  990. "sNIC SubSystem in Unrecoverable State.\n");
  991. break;
  992. }
  993. switch (fwreq->hdr.type) {
  994. case SNIC_RSP_EXCH_VER_CMPL:
  995. snic_io_exch_ver_cmpl_handler(snic, fwreq);
  996. break;
  997. case SNIC_RSP_REPORT_TGTS_CMPL:
  998. snic_report_tgt_cmpl_handler(snic, fwreq);
  999. break;
  1000. case SNIC_RSP_ICMND_CMPL:
  1001. snic_icmnd_cmpl_handler(snic, fwreq);
  1002. break;
  1003. case SNIC_RSP_ITMF_CMPL:
  1004. snic_itmf_cmpl_handler(snic, fwreq);
  1005. break;
  1006. case SNIC_RSP_HBA_RESET_CMPL:
  1007. snic_hba_reset_cmpl_handler(snic, fwreq);
  1008. break;
  1009. case SNIC_MSG_ACK:
  1010. snic_msg_ack_handler(snic, fwreq);
  1011. break;
  1012. case SNIC_MSG_ASYNC_EVNOTIFY:
  1013. snic_aen_handler(snic, fwreq);
  1014. break;
  1015. default:
  1016. SNIC_BUG_ON(1);
  1017. SNIC_SCSI_DBG(snic->shost,
  1018. "Unknown Firmware completion request type %d\n",
  1019. fwreq->hdr.type);
  1020. break;
  1021. }
  1022. /* Update Stats */
  1023. cmpl_time = jiffies - start;
  1024. if (cmpl_time > atomic64_read(&snic->s_stats.io.max_cmpl_time))
  1025. atomic64_set(&snic->s_stats.io.max_cmpl_time, cmpl_time);
  1026. return 0;
  1027. } /* end of snic_io_cmpl_handler */
  1028. /*
  1029. * snic_fwcq_cmpl_handler
  1030. * Routine to process fwCQ
  1031. * This CQ is independent, and not associated with wq/rq/wq_copy queues
  1032. */
  1033. int
  1034. snic_fwcq_cmpl_handler(struct snic *snic, int io_cmpl_work)
  1035. {
  1036. unsigned int num_ent = 0; /* number cq entries processed */
  1037. unsigned int cq_idx;
  1038. unsigned int nent_per_cq;
  1039. struct snic_misc_stats *misc_stats = &snic->s_stats.misc;
  1040. for (cq_idx = snic->wq_count; cq_idx < snic->cq_count; cq_idx++) {
  1041. nent_per_cq = vnic_cq_fw_service(&snic->cq[cq_idx],
  1042. snic_io_cmpl_handler,
  1043. io_cmpl_work);
  1044. num_ent += nent_per_cq;
  1045. if (nent_per_cq > atomic64_read(&misc_stats->max_cq_ents))
  1046. atomic64_set(&misc_stats->max_cq_ents, nent_per_cq);
  1047. }
  1048. return num_ent;
  1049. } /* end of snic_fwcq_cmpl_handler */
  1050. /*
  1051. * snic_queue_itmf_req: Common API to queue Task Management requests.
  1052. * Use rqi->tm_tag for passing special tags.
  1053. * @req_id : aborted request's tag, -1 for lun reset.
  1054. */
  1055. static int
  1056. snic_queue_itmf_req(struct snic *snic,
  1057. struct snic_host_req *tmreq,
  1058. struct scsi_cmnd *sc,
  1059. u32 tmf,
  1060. u32 req_id)
  1061. {
  1062. struct snic_req_info *rqi = req_to_rqi(tmreq);
  1063. struct scsi_lun lun;
  1064. int tm_tag = snic_cmd_tag(sc) | rqi->tm_tag;
  1065. int ret = 0;
  1066. SNIC_BUG_ON(!rqi);
  1067. SNIC_BUG_ON(!rqi->tm_tag);
  1068. /* fill in lun info */
  1069. int_to_scsilun(sc->device->lun, &lun);
  1070. /* Initialize snic_host_req: itmf */
  1071. snic_itmf_init(tmreq,
  1072. tm_tag,
  1073. snic->config.hid,
  1074. (ulong) rqi,
  1075. 0 /* flags */,
  1076. req_id, /* Command to be aborted. */
  1077. rqi->tgt_id,
  1078. lun.scsi_lun,
  1079. tmf);
  1080. /*
  1081. * In case of multiple aborts on same cmd,
  1082. * use try_wait_for_completion and completion_done() to check
  1083. * whether it queues aborts even after completion of abort issued
  1084. * prior.SNIC_BUG_ON(completion_done(&rqi->done));
  1085. */
  1086. ret = snic_queue_wq_desc(snic, tmreq, sizeof(*tmreq));
  1087. if (ret)
  1088. SNIC_HOST_ERR(snic->shost,
  1089. "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d tag %d Failed, ret = %d\n",
  1090. tmf, sc, rqi, req_id, snic_cmd_tag(sc), ret);
  1091. else
  1092. SNIC_SCSI_DBG(snic->shost,
  1093. "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d, tag %d (req_id)- Success.",
  1094. tmf, sc, rqi, req_id, snic_cmd_tag(sc));
  1095. return ret;
  1096. } /* end of snic_queue_itmf_req */
  1097. static int
  1098. snic_issue_tm_req(struct snic *snic,
  1099. struct snic_req_info *rqi,
  1100. struct scsi_cmnd *sc,
  1101. int tmf)
  1102. {
  1103. struct snic_host_req *tmreq = NULL;
  1104. int req_id = 0, tag = snic_cmd_tag(sc);
  1105. int ret = 0;
  1106. if (snic_get_state(snic) == SNIC_FWRESET)
  1107. return -EBUSY;
  1108. atomic_inc(&snic->ios_inflight);
  1109. SNIC_SCSI_DBG(snic->shost,
  1110. "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
  1111. tmf, rqi, tag);
  1112. if (tmf == SNIC_ITMF_LUN_RESET) {
  1113. tmreq = snic_dr_req_init(snic, rqi);
  1114. req_id = SCSI_NO_TAG;
  1115. } else {
  1116. tmreq = snic_abort_req_init(snic, rqi);
  1117. req_id = tag;
  1118. }
  1119. if (!tmreq) {
  1120. ret = -ENOMEM;
  1121. goto tmreq_err;
  1122. }
  1123. ret = snic_queue_itmf_req(snic, tmreq, sc, tmf, req_id);
  1124. tmreq_err:
  1125. if (ret) {
  1126. SNIC_HOST_ERR(snic->shost,
  1127. "issu_tmreq: Queueing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
  1128. tmf, sc, rqi, req_id, tag, ret);
  1129. } else {
  1130. SNIC_SCSI_DBG(snic->shost,
  1131. "issu_tmreq: Queueing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
  1132. tmf, sc, rqi, req_id, tag);
  1133. }
  1134. atomic_dec(&snic->ios_inflight);
  1135. return ret;
  1136. }
  1137. /*
  1138. * snic_queue_abort_req : Queues abort req to WQ
  1139. */
  1140. static int
  1141. snic_queue_abort_req(struct snic *snic,
  1142. struct snic_req_info *rqi,
  1143. struct scsi_cmnd *sc,
  1144. int tmf)
  1145. {
  1146. SNIC_SCSI_DBG(snic->shost, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
  1147. sc, rqi, snic_cmd_tag(sc), tmf);
  1148. /* Add special tag for abort */
  1149. rqi->tm_tag |= SNIC_TAG_ABORT;
  1150. return snic_issue_tm_req(snic, rqi, sc, tmf);
  1151. }
  1152. /*
  1153. * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
  1154. */
  1155. static int
  1156. snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
  1157. {
  1158. struct snic_req_info *rqi = NULL;
  1159. spinlock_t *io_lock = NULL;
  1160. unsigned long flags;
  1161. int ret = 0, tag = snic_cmd_tag(sc);
  1162. io_lock = snic_io_lock_hash(snic, sc);
  1163. spin_lock_irqsave(io_lock, flags);
  1164. rqi = (struct snic_req_info *) CMD_SP(sc);
  1165. if (!rqi) {
  1166. atomic64_inc(&snic->s_stats.io.req_null);
  1167. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  1168. SNIC_SCSI_DBG(snic->shost,
  1169. "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
  1170. tag, sc, CMD_FLAGS(sc));
  1171. ret = FAILED;
  1172. goto abort_fail;
  1173. }
  1174. rqi->abts_done = NULL;
  1175. ret = FAILED;
  1176. /* Check the abort status. */
  1177. switch (CMD_ABTS_STATUS(sc)) {
  1178. case SNIC_INVALID_CODE:
  1179. /* Firmware didn't complete abort req, timedout */
  1180. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TIMEDOUT;
  1181. atomic64_inc(&snic->s_stats.abts.drv_tmo);
  1182. SNIC_SCSI_DBG(snic->shost,
  1183. "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
  1184. sc, snic_cmd_tag(sc), CMD_FLAGS(sc));
  1185. /* do not release snic request in timedout case */
  1186. rqi = NULL;
  1187. goto abort_fail;
  1188. case SNIC_STAT_IO_SUCCESS:
  1189. case SNIC_STAT_IO_NOT_FOUND:
  1190. ret = SUCCESS;
  1191. /*
  1192. * If abort path doesn't call scsi_done(),
  1193. * the # IO timeouts == 2, will cause the LUN offline.
  1194. * Call scsi_done to complete the IO.
  1195. */
  1196. sc->result = (DID_ERROR << 16);
  1197. sc->scsi_done(sc);
  1198. break;
  1199. default:
  1200. /* Firmware completed abort with error */
  1201. ret = FAILED;
  1202. rqi = NULL;
  1203. break;
  1204. }
  1205. CMD_SP(sc) = NULL;
  1206. SNIC_HOST_INFO(snic->shost,
  1207. "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
  1208. tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
  1209. CMD_FLAGS(sc));
  1210. abort_fail:
  1211. spin_unlock_irqrestore(io_lock, flags);
  1212. if (rqi)
  1213. snic_release_req_buf(snic, rqi, sc);
  1214. return ret;
  1215. } /* end of snic_abort_finish */
  1216. /*
  1217. * snic_send_abort_and_wait : Issues Abort, and Waits
  1218. */
  1219. static int
  1220. snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
  1221. {
  1222. struct snic_req_info *rqi = NULL;
  1223. enum snic_ioreq_state sv_state;
  1224. struct snic_tgt *tgt = NULL;
  1225. spinlock_t *io_lock = NULL;
  1226. DECLARE_COMPLETION_ONSTACK(tm_done);
  1227. unsigned long flags;
  1228. int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
  1229. tgt = starget_to_tgt(scsi_target(sc->device));
  1230. if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
  1231. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  1232. else
  1233. tmf = SNIC_ITMF_ABTS_TASK;
  1234. /* stats */
  1235. io_lock = snic_io_lock_hash(snic, sc);
  1236. /*
  1237. * Avoid a race between SCSI issuing the abort and the device
  1238. * completing the command.
  1239. *
  1240. * If the command is already completed by fw_cmpl code,
  1241. * we just return SUCCESS from here. This means that the abort
  1242. * succeeded. In the SCSI ML, since the timeout for command has
  1243. * happend, the completion wont actually complete the command
  1244. * and it will be considered as an aborted command
  1245. *
  1246. * The CMD_SP will not be cleared except while holding io_lock
  1247. */
  1248. spin_lock_irqsave(io_lock, flags);
  1249. rqi = (struct snic_req_info *) CMD_SP(sc);
  1250. if (!rqi) {
  1251. spin_unlock_irqrestore(io_lock, flags);
  1252. SNIC_HOST_ERR(snic->shost,
  1253. "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
  1254. tag, CMD_FLAGS(sc));
  1255. ret = SUCCESS;
  1256. goto send_abts_end;
  1257. }
  1258. rqi->abts_done = &tm_done;
  1259. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
  1260. spin_unlock_irqrestore(io_lock, flags);
  1261. ret = 0;
  1262. goto abts_pending;
  1263. }
  1264. SNIC_BUG_ON(!rqi->abts_done);
  1265. /* Save Command State, should be restored on failed to Queue. */
  1266. sv_state = CMD_STATE(sc);
  1267. /*
  1268. * Command is still pending, need to abort it
  1269. * If the fw completes the command after this point,
  1270. * the completion won't be done till mid-layer, since abot
  1271. * has already started.
  1272. */
  1273. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  1274. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  1275. SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
  1276. spin_unlock_irqrestore(io_lock, flags);
  1277. /* Now Queue the abort command to firmware */
  1278. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  1279. if (ret) {
  1280. atomic64_inc(&snic->s_stats.abts.q_fail);
  1281. SNIC_HOST_ERR(snic->shost,
  1282. "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
  1283. tag, ret, CMD_FLAGS(sc));
  1284. spin_lock_irqsave(io_lock, flags);
  1285. /* Restore Command's previous state */
  1286. CMD_STATE(sc) = sv_state;
  1287. rqi = (struct snic_req_info *) CMD_SP(sc);
  1288. if (rqi)
  1289. rqi->abts_done = NULL;
  1290. spin_unlock_irqrestore(io_lock, flags);
  1291. ret = FAILED;
  1292. goto send_abts_end;
  1293. }
  1294. spin_lock_irqsave(io_lock, flags);
  1295. if (tmf == SNIC_ITMF_ABTS_TASK) {
  1296. CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
  1297. atomic64_inc(&snic->s_stats.abts.num);
  1298. } else {
  1299. /* term stats */
  1300. CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
  1301. }
  1302. spin_unlock_irqrestore(io_lock, flags);
  1303. SNIC_SCSI_DBG(snic->shost,
  1304. "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
  1305. sc, tag, CMD_FLAGS(sc));
  1306. ret = 0;
  1307. abts_pending:
  1308. /*
  1309. * Queued an abort IO, wait for its completion.
  1310. * Once the fw completes the abort command, it will
  1311. * wakeup this thread.
  1312. */
  1313. wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
  1314. send_abts_end:
  1315. return ret;
  1316. } /* end of snic_send_abort_and_wait */
  1317. /*
  1318. * This function is exported to SCSI for sending abort cmnds.
  1319. * A SCSI IO is represent by snic_ioreq in the driver.
  1320. * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
  1321. */
  1322. int
  1323. snic_abort_cmd(struct scsi_cmnd *sc)
  1324. {
  1325. struct snic *snic = shost_priv(sc->device->host);
  1326. int ret = SUCCESS, tag = snic_cmd_tag(sc);
  1327. u32 start_time = jiffies;
  1328. SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
  1329. sc, sc->cmnd[0], sc->request, tag);
  1330. if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
  1331. SNIC_HOST_ERR(snic->shost,
  1332. "abt_cmd: tag %x Parent Devs are not rdy\n",
  1333. tag);
  1334. ret = FAST_IO_FAIL;
  1335. goto abort_end;
  1336. }
  1337. ret = snic_send_abort_and_wait(snic, sc);
  1338. if (ret)
  1339. goto abort_end;
  1340. ret = snic_abort_finish(snic, sc);
  1341. abort_end:
  1342. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  1343. jiffies_to_msecs(jiffies - start_time), 0,
  1344. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1345. SNIC_SCSI_DBG(snic->shost,
  1346. "abts: Abort Req Status = %s\n",
  1347. (ret == SUCCESS) ? "SUCCESS" :
  1348. ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
  1349. return ret;
  1350. }
  1351. static int
  1352. snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
  1353. {
  1354. struct snic_req_info *rqi = NULL;
  1355. struct scsi_cmnd *sc = NULL;
  1356. struct scsi_device *lr_sdev = NULL;
  1357. spinlock_t *io_lock = NULL;
  1358. u32 tag;
  1359. unsigned long flags;
  1360. if (lr_sc)
  1361. lr_sdev = lr_sc->device;
  1362. /* walk through the tag map, an dcheck if IOs are still pending in fw*/
  1363. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1364. io_lock = snic_io_lock_tag(snic, tag);
  1365. spin_lock_irqsave(io_lock, flags);
  1366. sc = scsi_host_find_tag(snic->shost, tag);
  1367. if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
  1368. spin_unlock_irqrestore(io_lock, flags);
  1369. continue;
  1370. }
  1371. rqi = (struct snic_req_info *) CMD_SP(sc);
  1372. if (!rqi) {
  1373. spin_unlock_irqrestore(io_lock, flags);
  1374. continue;
  1375. }
  1376. /*
  1377. * Found IO that is still pending w/ firmware and belongs to
  1378. * the LUN that is under reset, if lr_sc != NULL
  1379. */
  1380. SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
  1381. snic_ioreq_state_to_str(CMD_STATE(sc)));
  1382. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
  1383. spin_unlock_irqrestore(io_lock, flags);
  1384. return 1;
  1385. }
  1386. spin_unlock_irqrestore(io_lock, flags);
  1387. }
  1388. return 0;
  1389. } /* end of snic_is_abts_pending */
  1390. static int
  1391. snic_dr_clean_single_req(struct snic *snic,
  1392. u32 tag,
  1393. struct scsi_device *lr_sdev)
  1394. {
  1395. struct snic_req_info *rqi = NULL;
  1396. struct snic_tgt *tgt = NULL;
  1397. struct scsi_cmnd *sc = NULL;
  1398. spinlock_t *io_lock = NULL;
  1399. u32 sv_state = 0, tmf = 0;
  1400. DECLARE_COMPLETION_ONSTACK(tm_done);
  1401. unsigned long flags;
  1402. int ret = 0;
  1403. io_lock = snic_io_lock_tag(snic, tag);
  1404. spin_lock_irqsave(io_lock, flags);
  1405. sc = scsi_host_find_tag(snic->shost, tag);
  1406. /* Ignore Cmd that don't belong to Lun Reset device */
  1407. if (!sc || sc->device != lr_sdev)
  1408. goto skip_clean;
  1409. rqi = (struct snic_req_info *) CMD_SP(sc);
  1410. if (!rqi)
  1411. goto skip_clean;
  1412. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  1413. goto skip_clean;
  1414. if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
  1415. (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
  1416. SNIC_SCSI_DBG(snic->shost,
  1417. "clean_single_req: devrst is not pending sc 0x%p\n",
  1418. sc);
  1419. goto skip_clean;
  1420. }
  1421. SNIC_SCSI_DBG(snic->shost,
  1422. "clean_single_req: Found IO in %s on lun\n",
  1423. snic_ioreq_state_to_str(CMD_STATE(sc)));
  1424. /* Save Command State */
  1425. sv_state = CMD_STATE(sc);
  1426. /*
  1427. * Any pending IO issued prior to reset is expected to be
  1428. * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
  1429. * to indicate the IO is abort pending.
  1430. * When IO is completed, the IO will be handed over and handled
  1431. * in this function.
  1432. */
  1433. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  1434. SNIC_BUG_ON(rqi->abts_done);
  1435. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
  1436. rqi->tm_tag = SNIC_TAG_DEV_RST;
  1437. SNIC_SCSI_DBG(snic->shost,
  1438. "clean_single_req:devrst sc 0x%p\n", sc);
  1439. }
  1440. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  1441. rqi->abts_done = &tm_done;
  1442. spin_unlock_irqrestore(io_lock, flags);
  1443. tgt = starget_to_tgt(scsi_target(sc->device));
  1444. if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
  1445. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  1446. else
  1447. tmf = SNIC_ITMF_ABTS_TASK;
  1448. /* Now queue the abort command to firmware */
  1449. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  1450. if (ret) {
  1451. SNIC_HOST_ERR(snic->shost,
  1452. "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
  1453. sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
  1454. spin_lock_irqsave(io_lock, flags);
  1455. rqi = (struct snic_req_info *) CMD_SP(sc);
  1456. if (rqi)
  1457. rqi->abts_done = NULL;
  1458. /* Restore Command State */
  1459. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  1460. CMD_STATE(sc) = sv_state;
  1461. ret = 1;
  1462. goto skip_clean;
  1463. }
  1464. spin_lock_irqsave(io_lock, flags);
  1465. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
  1466. CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
  1467. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
  1468. spin_unlock_irqrestore(io_lock, flags);
  1469. wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
  1470. /* Recheck cmd state to check if it now aborted. */
  1471. spin_lock_irqsave(io_lock, flags);
  1472. rqi = (struct snic_req_info *) CMD_SP(sc);
  1473. if (!rqi) {
  1474. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  1475. goto skip_clean;
  1476. }
  1477. rqi->abts_done = NULL;
  1478. /* if abort is still pending w/ fw, fail */
  1479. if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
  1480. SNIC_HOST_ERR(snic->shost,
  1481. "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
  1482. sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
  1483. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
  1484. ret = 1;
  1485. goto skip_clean;
  1486. }
  1487. CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
  1488. CMD_SP(sc) = NULL;
  1489. spin_unlock_irqrestore(io_lock, flags);
  1490. snic_release_req_buf(snic, rqi, sc);
  1491. sc->result = (DID_ERROR << 16);
  1492. sc->scsi_done(sc);
  1493. ret = 0;
  1494. return ret;
  1495. skip_clean:
  1496. spin_unlock_irqrestore(io_lock, flags);
  1497. return ret;
  1498. } /* end of snic_dr_clean_single_req */
  1499. static int
  1500. snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
  1501. {
  1502. struct scsi_device *lr_sdev = lr_sc->device;
  1503. u32 tag = 0;
  1504. int ret = FAILED;
  1505. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1506. if (tag == snic_cmd_tag(lr_sc))
  1507. continue;
  1508. ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
  1509. if (ret) {
  1510. SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
  1511. goto clean_err;
  1512. }
  1513. }
  1514. schedule_timeout(msecs_to_jiffies(100));
  1515. /* Walk through all the cmds and check abts status. */
  1516. if (snic_is_abts_pending(snic, lr_sc)) {
  1517. ret = FAILED;
  1518. goto clean_err;
  1519. }
  1520. ret = 0;
  1521. SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
  1522. return ret;
  1523. clean_err:
  1524. ret = FAILED;
  1525. SNIC_HOST_ERR(snic->shost,
  1526. "Failed to Clean Pending IOs on %s device.\n",
  1527. dev_name(&lr_sdev->sdev_gendev));
  1528. return ret;
  1529. } /* end of snic_dr_clean_pending_req */
  1530. /*
  1531. * snic_dr_finish : Called by snic_device_reset
  1532. */
  1533. static int
  1534. snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
  1535. {
  1536. struct snic_req_info *rqi = NULL;
  1537. spinlock_t *io_lock = NULL;
  1538. unsigned long flags;
  1539. int lr_res = 0;
  1540. int ret = FAILED;
  1541. io_lock = snic_io_lock_hash(snic, sc);
  1542. spin_lock_irqsave(io_lock, flags);
  1543. rqi = (struct snic_req_info *) CMD_SP(sc);
  1544. if (!rqi) {
  1545. spin_unlock_irqrestore(io_lock, flags);
  1546. SNIC_SCSI_DBG(snic->shost,
  1547. "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
  1548. snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
  1549. ret = FAILED;
  1550. goto dr_fini_end;
  1551. }
  1552. rqi->dr_done = NULL;
  1553. lr_res = CMD_LR_STATUS(sc);
  1554. switch (lr_res) {
  1555. case SNIC_INVALID_CODE:
  1556. /* stats */
  1557. SNIC_SCSI_DBG(snic->shost,
  1558. "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
  1559. snic_cmd_tag(sc), CMD_FLAGS(sc));
  1560. CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
  1561. ret = FAILED;
  1562. goto dr_failed;
  1563. case SNIC_STAT_IO_SUCCESS:
  1564. SNIC_SCSI_DBG(snic->shost,
  1565. "dr_fini: Tag %x Dev Reset cmpl\n",
  1566. snic_cmd_tag(sc));
  1567. ret = 0;
  1568. break;
  1569. default:
  1570. SNIC_HOST_ERR(snic->shost,
  1571. "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
  1572. snic_cmd_tag(sc),
  1573. snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
  1574. ret = FAILED;
  1575. goto dr_failed;
  1576. }
  1577. spin_unlock_irqrestore(io_lock, flags);
  1578. /*
  1579. * Cleanup any IOs on this LUN that have still not completed.
  1580. * If any of these fail, then LUN Reset fails.
  1581. * Cleanup cleans all commands on this LUN except
  1582. * the lun reset command. If all cmds get cleaned, the LUN Reset
  1583. * succeeds.
  1584. */
  1585. ret = snic_dr_clean_pending_req(snic, sc);
  1586. if (ret) {
  1587. spin_lock_irqsave(io_lock, flags);
  1588. SNIC_SCSI_DBG(snic->shost,
  1589. "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
  1590. snic_cmd_tag(sc));
  1591. rqi = (struct snic_req_info *) CMD_SP(sc);
  1592. goto dr_failed;
  1593. } else {
  1594. /* Cleanup LUN Reset Command */
  1595. spin_lock_irqsave(io_lock, flags);
  1596. rqi = (struct snic_req_info *) CMD_SP(sc);
  1597. if (rqi)
  1598. ret = SUCCESS; /* Completed Successfully */
  1599. else
  1600. ret = FAILED;
  1601. }
  1602. dr_failed:
  1603. lockdep_assert_held(io_lock);
  1604. if (rqi)
  1605. CMD_SP(sc) = NULL;
  1606. spin_unlock_irqrestore(io_lock, flags);
  1607. if (rqi)
  1608. snic_release_req_buf(snic, rqi, sc);
  1609. dr_fini_end:
  1610. return ret;
  1611. } /* end of snic_dr_finish */
  1612. static int
  1613. snic_queue_dr_req(struct snic *snic,
  1614. struct snic_req_info *rqi,
  1615. struct scsi_cmnd *sc)
  1616. {
  1617. /* Add special tag for device reset */
  1618. rqi->tm_tag |= SNIC_TAG_DEV_RST;
  1619. return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
  1620. }
  1621. static int
  1622. snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
  1623. {
  1624. struct snic_req_info *rqi = NULL;
  1625. enum snic_ioreq_state sv_state;
  1626. spinlock_t *io_lock = NULL;
  1627. unsigned long flags;
  1628. DECLARE_COMPLETION_ONSTACK(tm_done);
  1629. int ret = FAILED, tag = snic_cmd_tag(sc);
  1630. io_lock = snic_io_lock_hash(snic, sc);
  1631. spin_lock_irqsave(io_lock, flags);
  1632. CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
  1633. rqi = (struct snic_req_info *) CMD_SP(sc);
  1634. if (!rqi) {
  1635. SNIC_HOST_ERR(snic->shost,
  1636. "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
  1637. tag, CMD_FLAGS(sc));
  1638. spin_unlock_irqrestore(io_lock, flags);
  1639. ret = FAILED;
  1640. goto send_dr_end;
  1641. }
  1642. /* Save Command state to restore in case Queuing failed. */
  1643. sv_state = CMD_STATE(sc);
  1644. CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
  1645. CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
  1646. SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
  1647. rqi->dr_done = &tm_done;
  1648. SNIC_BUG_ON(!rqi->dr_done);
  1649. spin_unlock_irqrestore(io_lock, flags);
  1650. /*
  1651. * The Command state is changed to IOREQ_PENDING,
  1652. * in this case, if the command is completed, the icmnd_cmpl will
  1653. * mark the cmd as completed.
  1654. * This logic still makes LUN Reset is inevitable.
  1655. */
  1656. ret = snic_queue_dr_req(snic, rqi, sc);
  1657. if (ret) {
  1658. SNIC_HOST_ERR(snic->shost,
  1659. "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
  1660. tag, ret, CMD_FLAGS(sc));
  1661. spin_lock_irqsave(io_lock, flags);
  1662. /* Restore State */
  1663. CMD_STATE(sc) = sv_state;
  1664. rqi = (struct snic_req_info *) CMD_SP(sc);
  1665. if (rqi)
  1666. rqi->dr_done = NULL;
  1667. /* rqi is freed in caller. */
  1668. spin_unlock_irqrestore(io_lock, flags);
  1669. ret = FAILED;
  1670. goto send_dr_end;
  1671. }
  1672. spin_lock_irqsave(io_lock, flags);
  1673. CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
  1674. spin_unlock_irqrestore(io_lock, flags);
  1675. ret = 0;
  1676. wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
  1677. send_dr_end:
  1678. return ret;
  1679. }
  1680. /*
  1681. * auxillary funciton to check lun reset op is supported or not
  1682. * Not supported if returns 0
  1683. */
  1684. static int
  1685. snic_dev_reset_supported(struct scsi_device *sdev)
  1686. {
  1687. struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
  1688. if (tgt->tdata.typ == SNIC_TGT_DAS)
  1689. return 0;
  1690. return 1;
  1691. }
  1692. static void
  1693. snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
  1694. {
  1695. struct snic_req_info *rqi = NULL;
  1696. spinlock_t *io_lock = NULL;
  1697. unsigned long flags;
  1698. u32 start_time = jiffies;
  1699. io_lock = snic_io_lock_hash(snic, sc);
  1700. spin_lock_irqsave(io_lock, flags);
  1701. rqi = (struct snic_req_info *) CMD_SP(sc);
  1702. if (rqi) {
  1703. start_time = rqi->start_time;
  1704. CMD_SP(sc) = NULL;
  1705. }
  1706. CMD_FLAGS(sc) |= flag;
  1707. spin_unlock_irqrestore(io_lock, flags);
  1708. if (rqi)
  1709. snic_release_req_buf(snic, rqi, sc);
  1710. SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
  1711. jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
  1712. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1713. }
  1714. /*
  1715. * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
  1716. * fail to get aborted. It calls driver's eh_device_reset with a SCSI
  1717. * command on the LUN.
  1718. */
  1719. int
  1720. snic_device_reset(struct scsi_cmnd *sc)
  1721. {
  1722. struct Scsi_Host *shost = sc->device->host;
  1723. struct snic *snic = shost_priv(shost);
  1724. struct snic_req_info *rqi = NULL;
  1725. int tag = snic_cmd_tag(sc);
  1726. int start_time = jiffies;
  1727. int ret = FAILED;
  1728. int dr_supp = 0;
  1729. SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
  1730. sc, sc->cmnd[0], sc->request,
  1731. snic_cmd_tag(sc));
  1732. dr_supp = snic_dev_reset_supported(sc->device);
  1733. if (!dr_supp) {
  1734. /* device reset op is not supported */
  1735. SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
  1736. snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
  1737. goto dev_rst_end;
  1738. }
  1739. if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
  1740. snic_unlink_and_release_req(snic, sc, 0);
  1741. SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
  1742. goto dev_rst_end;
  1743. }
  1744. /* There is no tag when lun reset is issue through ioctl. */
  1745. if (unlikely(tag <= SNIC_NO_TAG)) {
  1746. SNIC_HOST_INFO(snic->shost,
  1747. "Devrst: LUN Reset Recvd thru IOCTL.\n");
  1748. rqi = snic_req_init(snic, 0);
  1749. if (!rqi)
  1750. goto dev_rst_end;
  1751. memset(scsi_cmd_priv(sc), 0,
  1752. sizeof(struct snic_internal_io_state));
  1753. CMD_SP(sc) = (char *)rqi;
  1754. CMD_FLAGS(sc) = SNIC_NO_FLAGS;
  1755. /* Add special tag for dr coming from user spc */
  1756. rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
  1757. rqi->sc = sc;
  1758. }
  1759. ret = snic_send_dr_and_wait(snic, sc);
  1760. if (ret) {
  1761. SNIC_HOST_ERR(snic->shost,
  1762. "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
  1763. tag, ret);
  1764. snic_unlink_and_release_req(snic, sc, 0);
  1765. goto dev_rst_end;
  1766. }
  1767. ret = snic_dr_finish(snic, sc);
  1768. dev_rst_end:
  1769. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  1770. jiffies_to_msecs(jiffies - start_time),
  1771. 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1772. SNIC_SCSI_DBG(snic->shost,
  1773. "Devrst: Returning from Device Reset : %s\n",
  1774. (ret == SUCCESS) ? "SUCCESS" : "FAILED");
  1775. return ret;
  1776. } /* end of snic_device_reset */
  1777. /*
  1778. * SCSI Error handling calls driver's eh_host_reset if all prior
  1779. * error handling levels return FAILED.
  1780. *
  1781. * Host Reset is the highest level of error recovery. If this fails, then
  1782. * host is offlined by SCSI.
  1783. */
  1784. /*
  1785. * snic_issue_hba_reset : Queues FW Reset Request.
  1786. */
  1787. static int
  1788. snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
  1789. {
  1790. struct snic_req_info *rqi = NULL;
  1791. struct snic_host_req *req = NULL;
  1792. spinlock_t *io_lock = NULL;
  1793. DECLARE_COMPLETION_ONSTACK(wait);
  1794. unsigned long flags;
  1795. int ret = -ENOMEM;
  1796. rqi = snic_req_init(snic, 0);
  1797. if (!rqi) {
  1798. ret = -ENOMEM;
  1799. goto hba_rst_end;
  1800. }
  1801. if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
  1802. memset(scsi_cmd_priv(sc), 0,
  1803. sizeof(struct snic_internal_io_state));
  1804. SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
  1805. rqi->sc = sc;
  1806. }
  1807. req = rqi_to_req(rqi);
  1808. io_lock = snic_io_lock_hash(snic, sc);
  1809. spin_lock_irqsave(io_lock, flags);
  1810. SNIC_BUG_ON(CMD_SP(sc) != NULL);
  1811. CMD_STATE(sc) = SNIC_IOREQ_PENDING;
  1812. CMD_SP(sc) = (char *) rqi;
  1813. CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
  1814. snic->remove_wait = &wait;
  1815. spin_unlock_irqrestore(io_lock, flags);
  1816. /* Initialize Request */
  1817. snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
  1818. snic->config.hid, 0, (ulong) rqi);
  1819. req->u.reset.flags = 0;
  1820. ret = snic_queue_wq_desc(snic, req, sizeof(*req));
  1821. if (ret) {
  1822. SNIC_HOST_ERR(snic->shost,
  1823. "issu_hr:Queuing HBA Reset Failed. w err %d\n",
  1824. ret);
  1825. goto hba_rst_err;
  1826. }
  1827. spin_lock_irqsave(io_lock, flags);
  1828. CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
  1829. spin_unlock_irqrestore(io_lock, flags);
  1830. atomic64_inc(&snic->s_stats.reset.hba_resets);
  1831. SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
  1832. wait_for_completion_timeout(snic->remove_wait,
  1833. SNIC_HOST_RESET_TIMEOUT);
  1834. if (snic_get_state(snic) == SNIC_FWRESET) {
  1835. SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
  1836. ret = -ETIMEDOUT;
  1837. goto hba_rst_err;
  1838. }
  1839. spin_lock_irqsave(io_lock, flags);
  1840. snic->remove_wait = NULL;
  1841. rqi = (struct snic_req_info *) CMD_SP(sc);
  1842. CMD_SP(sc) = NULL;
  1843. spin_unlock_irqrestore(io_lock, flags);
  1844. if (rqi)
  1845. snic_req_free(snic, rqi);
  1846. ret = 0;
  1847. return ret;
  1848. hba_rst_err:
  1849. spin_lock_irqsave(io_lock, flags);
  1850. snic->remove_wait = NULL;
  1851. rqi = (struct snic_req_info *) CMD_SP(sc);
  1852. CMD_SP(sc) = NULL;
  1853. spin_unlock_irqrestore(io_lock, flags);
  1854. if (rqi)
  1855. snic_req_free(snic, rqi);
  1856. hba_rst_end:
  1857. SNIC_HOST_ERR(snic->shost,
  1858. "reset:HBA Reset Failed w/ err = %d.\n",
  1859. ret);
  1860. return ret;
  1861. } /* end of snic_issue_hba_reset */
  1862. int
  1863. snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
  1864. {
  1865. struct snic *snic = shost_priv(shost);
  1866. enum snic_state sv_state;
  1867. unsigned long flags;
  1868. int ret = FAILED;
  1869. /* Set snic state as SNIC_FWRESET*/
  1870. sv_state = snic_get_state(snic);
  1871. spin_lock_irqsave(&snic->snic_lock, flags);
  1872. if (snic_get_state(snic) == SNIC_FWRESET) {
  1873. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1874. SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
  1875. msleep(SNIC_HOST_RESET_TIMEOUT);
  1876. ret = SUCCESS;
  1877. goto reset_end;
  1878. }
  1879. snic_set_state(snic, SNIC_FWRESET);
  1880. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1881. /* Wait for all the IOs that are entered in Qcmd */
  1882. while (atomic_read(&snic->ios_inflight))
  1883. schedule_timeout(msecs_to_jiffies(1));
  1884. ret = snic_issue_hba_reset(snic, sc);
  1885. if (ret) {
  1886. SNIC_HOST_ERR(shost,
  1887. "reset:Host Reset Failed w/ err %d.\n",
  1888. ret);
  1889. spin_lock_irqsave(&snic->snic_lock, flags);
  1890. snic_set_state(snic, sv_state);
  1891. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1892. atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
  1893. ret = FAILED;
  1894. goto reset_end;
  1895. }
  1896. ret = SUCCESS;
  1897. reset_end:
  1898. return ret;
  1899. } /* end of snic_reset */
  1900. /*
  1901. * SCSI Error handling calls driver's eh_host_reset if all prior
  1902. * error handling levels return FAILED.
  1903. *
  1904. * Host Reset is the highest level of error recovery. If this fails, then
  1905. * host is offlined by SCSI.
  1906. */
  1907. int
  1908. snic_host_reset(struct scsi_cmnd *sc)
  1909. {
  1910. struct Scsi_Host *shost = sc->device->host;
  1911. u32 start_time = jiffies;
  1912. int ret = FAILED;
  1913. SNIC_SCSI_DBG(shost,
  1914. "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
  1915. sc, sc->cmnd[0], sc->request,
  1916. snic_cmd_tag(sc), CMD_FLAGS(sc));
  1917. ret = snic_reset(shost, sc);
  1918. SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
  1919. jiffies_to_msecs(jiffies - start_time),
  1920. 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1921. return ret;
  1922. } /* end of snic_host_reset */
  1923. /*
  1924. * snic_cmpl_pending_tmreq : Caller should hold io_lock
  1925. */
  1926. static void
  1927. snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
  1928. {
  1929. struct snic_req_info *rqi = NULL;
  1930. SNIC_SCSI_DBG(snic->shost,
  1931. "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
  1932. sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
  1933. /*
  1934. * CASE : FW didn't post itmf completion due to PCIe Errors.
  1935. * Marking the abort status as Success to call scsi completion
  1936. * in snic_abort_finish()
  1937. */
  1938. CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
  1939. rqi = (struct snic_req_info *) CMD_SP(sc);
  1940. if (!rqi)
  1941. return;
  1942. if (rqi->dr_done)
  1943. complete(rqi->dr_done);
  1944. else if (rqi->abts_done)
  1945. complete(rqi->abts_done);
  1946. }
  1947. /*
  1948. * snic_scsi_cleanup: Walks through tag map and releases the reqs
  1949. */
  1950. static void
  1951. snic_scsi_cleanup(struct snic *snic, int ex_tag)
  1952. {
  1953. struct snic_req_info *rqi = NULL;
  1954. struct scsi_cmnd *sc = NULL;
  1955. spinlock_t *io_lock = NULL;
  1956. unsigned long flags;
  1957. int tag;
  1958. u64 st_time = 0;
  1959. SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
  1960. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1961. /* Skip ex_tag */
  1962. if (tag == ex_tag)
  1963. continue;
  1964. io_lock = snic_io_lock_tag(snic, tag);
  1965. spin_lock_irqsave(io_lock, flags);
  1966. sc = scsi_host_find_tag(snic->shost, tag);
  1967. if (!sc) {
  1968. spin_unlock_irqrestore(io_lock, flags);
  1969. continue;
  1970. }
  1971. if (unlikely(snic_tmreq_pending(sc))) {
  1972. /*
  1973. * When FW Completes reset w/o sending completions
  1974. * for outstanding ios.
  1975. */
  1976. snic_cmpl_pending_tmreq(snic, sc);
  1977. spin_unlock_irqrestore(io_lock, flags);
  1978. continue;
  1979. }
  1980. rqi = (struct snic_req_info *) CMD_SP(sc);
  1981. if (!rqi) {
  1982. spin_unlock_irqrestore(io_lock, flags);
  1983. goto cleanup;
  1984. }
  1985. SNIC_SCSI_DBG(snic->shost,
  1986. "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
  1987. sc, rqi, tag, CMD_FLAGS(sc));
  1988. CMD_SP(sc) = NULL;
  1989. CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
  1990. spin_unlock_irqrestore(io_lock, flags);
  1991. st_time = rqi->start_time;
  1992. SNIC_HOST_INFO(snic->shost,
  1993. "sc_clean: Releasing rqi %p : flags 0x%llx\n",
  1994. rqi, CMD_FLAGS(sc));
  1995. snic_release_req_buf(snic, rqi, sc);
  1996. cleanup:
  1997. sc->result = DID_TRANSPORT_DISRUPTED << 16;
  1998. SNIC_HOST_INFO(snic->shost,
  1999. "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
  2000. sc, sc->request->tag, CMD_FLAGS(sc), rqi,
  2001. jiffies_to_msecs(jiffies - st_time));
  2002. /* Update IO stats */
  2003. snic_stats_update_io_cmpl(&snic->s_stats);
  2004. if (sc->scsi_done) {
  2005. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  2006. jiffies_to_msecs(jiffies - st_time), 0,
  2007. SNIC_TRC_CMD(sc),
  2008. SNIC_TRC_CMD_STATE_FLAGS(sc));
  2009. sc->scsi_done(sc);
  2010. }
  2011. }
  2012. } /* end of snic_scsi_cleanup */
  2013. void
  2014. snic_shutdown_scsi_cleanup(struct snic *snic)
  2015. {
  2016. SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
  2017. snic_scsi_cleanup(snic, SCSI_NO_TAG);
  2018. } /* end of snic_shutdown_scsi_cleanup */
  2019. /*
  2020. * snic_internal_abort_io
  2021. * called by : snic_tgt_scsi_abort_io
  2022. */
  2023. static int
  2024. snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
  2025. {
  2026. struct snic_req_info *rqi = NULL;
  2027. spinlock_t *io_lock = NULL;
  2028. unsigned long flags;
  2029. u32 sv_state = 0;
  2030. int ret = 0;
  2031. io_lock = snic_io_lock_hash(snic, sc);
  2032. spin_lock_irqsave(io_lock, flags);
  2033. rqi = (struct snic_req_info *) CMD_SP(sc);
  2034. if (!rqi)
  2035. goto skip_internal_abts;
  2036. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  2037. goto skip_internal_abts;
  2038. if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
  2039. (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
  2040. SNIC_SCSI_DBG(snic->shost,
  2041. "internal_abts: dev rst not pending sc 0x%p\n",
  2042. sc);
  2043. goto skip_internal_abts;
  2044. }
  2045. if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
  2046. SNIC_SCSI_DBG(snic->shost,
  2047. "internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
  2048. sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
  2049. goto skip_internal_abts;
  2050. }
  2051. sv_state = CMD_STATE(sc);
  2052. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  2053. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  2054. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
  2055. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
  2056. /* stats */
  2057. rqi->tm_tag = SNIC_TAG_DEV_RST;
  2058. SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
  2059. }
  2060. SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
  2061. snic_cmd_tag(sc));
  2062. SNIC_BUG_ON(rqi->abts_done);
  2063. spin_unlock_irqrestore(io_lock, flags);
  2064. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  2065. if (ret) {
  2066. SNIC_HOST_ERR(snic->shost,
  2067. "internal_abts: Tag = %x , Failed w/ err = %d\n",
  2068. snic_cmd_tag(sc), ret);
  2069. spin_lock_irqsave(io_lock, flags);
  2070. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  2071. CMD_STATE(sc) = sv_state;
  2072. goto skip_internal_abts;
  2073. }
  2074. spin_lock_irqsave(io_lock, flags);
  2075. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
  2076. CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
  2077. else
  2078. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
  2079. ret = SUCCESS;
  2080. skip_internal_abts:
  2081. lockdep_assert_held(io_lock);
  2082. spin_unlock_irqrestore(io_lock, flags);
  2083. return ret;
  2084. } /* end of snic_internal_abort_io */
  2085. /*
  2086. * snic_tgt_scsi_abort_io : called by snic_tgt_del
  2087. */
  2088. int
  2089. snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
  2090. {
  2091. struct snic *snic = NULL;
  2092. struct scsi_cmnd *sc = NULL;
  2093. struct snic_tgt *sc_tgt = NULL;
  2094. spinlock_t *io_lock = NULL;
  2095. unsigned long flags;
  2096. int ret = 0, tag, abt_cnt = 0, tmf = 0;
  2097. if (!tgt)
  2098. return -1;
  2099. snic = shost_priv(snic_tgt_to_shost(tgt));
  2100. SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
  2101. if (tgt->tdata.typ == SNIC_TGT_DAS)
  2102. tmf = SNIC_ITMF_ABTS_TASK;
  2103. else
  2104. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  2105. for (tag = 0; tag < snic->max_tag_id; tag++) {
  2106. io_lock = snic_io_lock_tag(snic, tag);
  2107. spin_lock_irqsave(io_lock, flags);
  2108. sc = scsi_host_find_tag(snic->shost, tag);
  2109. if (!sc) {
  2110. spin_unlock_irqrestore(io_lock, flags);
  2111. continue;
  2112. }
  2113. sc_tgt = starget_to_tgt(scsi_target(sc->device));
  2114. if (sc_tgt != tgt) {
  2115. spin_unlock_irqrestore(io_lock, flags);
  2116. continue;
  2117. }
  2118. spin_unlock_irqrestore(io_lock, flags);
  2119. ret = snic_internal_abort_io(snic, sc, tmf);
  2120. if (ret < 0) {
  2121. SNIC_HOST_ERR(snic->shost,
  2122. "tgt_abt_io: Tag %x, Failed w err = %d\n",
  2123. tag, ret);
  2124. continue;
  2125. }
  2126. if (ret == SUCCESS)
  2127. abt_cnt++;
  2128. }
  2129. SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
  2130. return 0;
  2131. } /* end of snic_tgt_scsi_abort_io */