snic_scsi.c 66 KB

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