snic_scsi.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672
  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. pci_unmap_single(snic->pdev,
  130. le64_to_cpu(req->u.icmnd.sense_addr),
  131. SCSI_SENSE_BUFFERSIZE,
  132. PCI_DMA_FROMDEVICE);
  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 = pci_map_single(snic->pdev,
  163. sc->sense_buffer,
  164. SCSI_SENSE_BUFFERSIZE,
  165. PCI_DMA_FROMDEVICE);
  166. if (pci_dma_mapping_error(snic->pdev, 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. if (ret)
  1125. goto tmreq_err;
  1126. ret = 0;
  1127. tmreq_err:
  1128. if (ret) {
  1129. SNIC_HOST_ERR(snic->shost,
  1130. "issu_tmreq: Queing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
  1131. tmf, sc, rqi, req_id, tag, ret);
  1132. } else {
  1133. SNIC_SCSI_DBG(snic->shost,
  1134. "issu_tmreq: Queuing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
  1135. tmf, sc, rqi, req_id, tag);
  1136. }
  1137. atomic_dec(&snic->ios_inflight);
  1138. return ret;
  1139. }
  1140. /*
  1141. * snic_queue_abort_req : Queues abort req to WQ
  1142. */
  1143. static int
  1144. snic_queue_abort_req(struct snic *snic,
  1145. struct snic_req_info *rqi,
  1146. struct scsi_cmnd *sc,
  1147. int tmf)
  1148. {
  1149. SNIC_SCSI_DBG(snic->shost, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
  1150. sc, rqi, snic_cmd_tag(sc), tmf);
  1151. /* Add special tag for abort */
  1152. rqi->tm_tag |= SNIC_TAG_ABORT;
  1153. return snic_issue_tm_req(snic, rqi, sc, tmf);
  1154. }
  1155. /*
  1156. * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
  1157. */
  1158. static int
  1159. snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
  1160. {
  1161. struct snic_req_info *rqi = NULL;
  1162. spinlock_t *io_lock = NULL;
  1163. unsigned long flags;
  1164. int ret = 0, tag = snic_cmd_tag(sc);
  1165. io_lock = snic_io_lock_hash(snic, sc);
  1166. spin_lock_irqsave(io_lock, flags);
  1167. rqi = (struct snic_req_info *) CMD_SP(sc);
  1168. if (!rqi) {
  1169. atomic64_inc(&snic->s_stats.io.req_null);
  1170. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  1171. SNIC_SCSI_DBG(snic->shost,
  1172. "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
  1173. tag, sc, CMD_FLAGS(sc));
  1174. ret = FAILED;
  1175. goto abort_fail;
  1176. }
  1177. rqi->abts_done = NULL;
  1178. ret = FAILED;
  1179. /* Check the abort status. */
  1180. switch (CMD_ABTS_STATUS(sc)) {
  1181. case SNIC_INVALID_CODE:
  1182. /* Firmware didn't complete abort req, timedout */
  1183. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TIMEDOUT;
  1184. atomic64_inc(&snic->s_stats.abts.drv_tmo);
  1185. SNIC_SCSI_DBG(snic->shost,
  1186. "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
  1187. sc, snic_cmd_tag(sc), CMD_FLAGS(sc));
  1188. /* do not release snic request in timedout case */
  1189. rqi = NULL;
  1190. goto abort_fail;
  1191. case SNIC_STAT_IO_SUCCESS:
  1192. case SNIC_STAT_IO_NOT_FOUND:
  1193. ret = SUCCESS;
  1194. /*
  1195. * If abort path doesn't call scsi_done(),
  1196. * the # IO timeouts == 2, will cause the LUN offline.
  1197. * Call scsi_done to complete the IO.
  1198. */
  1199. sc->result = (DID_ERROR << 16);
  1200. sc->scsi_done(sc);
  1201. break;
  1202. default:
  1203. /* Firmware completed abort with error */
  1204. ret = FAILED;
  1205. rqi = NULL;
  1206. break;
  1207. }
  1208. CMD_SP(sc) = NULL;
  1209. SNIC_HOST_INFO(snic->shost,
  1210. "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
  1211. tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
  1212. CMD_FLAGS(sc));
  1213. abort_fail:
  1214. spin_unlock_irqrestore(io_lock, flags);
  1215. if (rqi)
  1216. snic_release_req_buf(snic, rqi, sc);
  1217. return ret;
  1218. } /* end of snic_abort_finish */
  1219. /*
  1220. * snic_send_abort_and_wait : Issues Abort, and Waits
  1221. */
  1222. static int
  1223. snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
  1224. {
  1225. struct snic_req_info *rqi = NULL;
  1226. enum snic_ioreq_state sv_state;
  1227. struct snic_tgt *tgt = NULL;
  1228. spinlock_t *io_lock = NULL;
  1229. DECLARE_COMPLETION_ONSTACK(tm_done);
  1230. unsigned long flags;
  1231. int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
  1232. tgt = starget_to_tgt(scsi_target(sc->device));
  1233. if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
  1234. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  1235. else
  1236. tmf = SNIC_ITMF_ABTS_TASK;
  1237. /* stats */
  1238. io_lock = snic_io_lock_hash(snic, sc);
  1239. /*
  1240. * Avoid a race between SCSI issuing the abort and the device
  1241. * completing the command.
  1242. *
  1243. * If the command is already completed by fw_cmpl code,
  1244. * we just return SUCCESS from here. This means that the abort
  1245. * succeeded. In the SCSI ML, since the timeout for command has
  1246. * happend, the completion wont actually complete the command
  1247. * and it will be considered as an aborted command
  1248. *
  1249. * The CMD_SP will not be cleared except while holding io_lock
  1250. */
  1251. spin_lock_irqsave(io_lock, flags);
  1252. rqi = (struct snic_req_info *) CMD_SP(sc);
  1253. if (!rqi) {
  1254. spin_unlock_irqrestore(io_lock, flags);
  1255. SNIC_HOST_ERR(snic->shost,
  1256. "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
  1257. tag, CMD_FLAGS(sc));
  1258. ret = SUCCESS;
  1259. goto send_abts_end;
  1260. }
  1261. rqi->abts_done = &tm_done;
  1262. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
  1263. spin_unlock_irqrestore(io_lock, flags);
  1264. ret = 0;
  1265. goto abts_pending;
  1266. }
  1267. SNIC_BUG_ON(!rqi->abts_done);
  1268. /* Save Command State, should be restored on failed to Queue. */
  1269. sv_state = CMD_STATE(sc);
  1270. /*
  1271. * Command is still pending, need to abort it
  1272. * If the fw completes the command after this point,
  1273. * the completion won't be done till mid-layer, since abot
  1274. * has already started.
  1275. */
  1276. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  1277. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  1278. SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
  1279. spin_unlock_irqrestore(io_lock, flags);
  1280. /* Now Queue the abort command to firmware */
  1281. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  1282. if (ret) {
  1283. atomic64_inc(&snic->s_stats.abts.q_fail);
  1284. SNIC_HOST_ERR(snic->shost,
  1285. "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
  1286. tag, ret, CMD_FLAGS(sc));
  1287. spin_lock_irqsave(io_lock, flags);
  1288. /* Restore Command's previous state */
  1289. CMD_STATE(sc) = sv_state;
  1290. rqi = (struct snic_req_info *) CMD_SP(sc);
  1291. if (rqi)
  1292. rqi->abts_done = NULL;
  1293. spin_unlock_irqrestore(io_lock, flags);
  1294. ret = FAILED;
  1295. goto send_abts_end;
  1296. }
  1297. spin_lock_irqsave(io_lock, flags);
  1298. if (tmf == SNIC_ITMF_ABTS_TASK) {
  1299. CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
  1300. atomic64_inc(&snic->s_stats.abts.num);
  1301. } else {
  1302. /* term stats */
  1303. CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
  1304. }
  1305. spin_unlock_irqrestore(io_lock, flags);
  1306. SNIC_SCSI_DBG(snic->shost,
  1307. "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
  1308. sc, tag, CMD_FLAGS(sc));
  1309. ret = 0;
  1310. abts_pending:
  1311. /*
  1312. * Queued an abort IO, wait for its completion.
  1313. * Once the fw completes the abort command, it will
  1314. * wakeup this thread.
  1315. */
  1316. wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
  1317. send_abts_end:
  1318. return ret;
  1319. } /* end of snic_send_abort_and_wait */
  1320. /*
  1321. * This function is exported to SCSI for sending abort cmnds.
  1322. * A SCSI IO is represent by snic_ioreq in the driver.
  1323. * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
  1324. */
  1325. int
  1326. snic_abort_cmd(struct scsi_cmnd *sc)
  1327. {
  1328. struct snic *snic = shost_priv(sc->device->host);
  1329. int ret = SUCCESS, tag = snic_cmd_tag(sc);
  1330. u32 start_time = jiffies;
  1331. SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
  1332. sc, sc->cmnd[0], sc->request, tag);
  1333. if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
  1334. SNIC_HOST_ERR(snic->shost,
  1335. "abt_cmd: tag %x Parent Devs are not rdy\n",
  1336. tag);
  1337. ret = FAST_IO_FAIL;
  1338. goto abort_end;
  1339. }
  1340. ret = snic_send_abort_and_wait(snic, sc);
  1341. if (ret)
  1342. goto abort_end;
  1343. ret = snic_abort_finish(snic, sc);
  1344. abort_end:
  1345. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  1346. jiffies_to_msecs(jiffies - start_time), 0,
  1347. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1348. SNIC_SCSI_DBG(snic->shost,
  1349. "abts: Abort Req Status = %s\n",
  1350. (ret == SUCCESS) ? "SUCCESS" :
  1351. ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
  1352. return ret;
  1353. }
  1354. static int
  1355. snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
  1356. {
  1357. struct snic_req_info *rqi = NULL;
  1358. struct scsi_cmnd *sc = NULL;
  1359. struct scsi_device *lr_sdev = NULL;
  1360. spinlock_t *io_lock = NULL;
  1361. u32 tag;
  1362. unsigned long flags;
  1363. if (lr_sc)
  1364. lr_sdev = lr_sc->device;
  1365. /* walk through the tag map, an dcheck if IOs are still pending in fw*/
  1366. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1367. io_lock = snic_io_lock_tag(snic, tag);
  1368. spin_lock_irqsave(io_lock, flags);
  1369. sc = scsi_host_find_tag(snic->shost, tag);
  1370. if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
  1371. spin_unlock_irqrestore(io_lock, flags);
  1372. continue;
  1373. }
  1374. rqi = (struct snic_req_info *) CMD_SP(sc);
  1375. if (!rqi) {
  1376. spin_unlock_irqrestore(io_lock, flags);
  1377. continue;
  1378. }
  1379. /*
  1380. * Found IO that is still pending w/ firmware and belongs to
  1381. * the LUN that is under reset, if lr_sc != NULL
  1382. */
  1383. SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
  1384. snic_ioreq_state_to_str(CMD_STATE(sc)));
  1385. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
  1386. spin_unlock_irqrestore(io_lock, flags);
  1387. return 1;
  1388. }
  1389. spin_unlock_irqrestore(io_lock, flags);
  1390. }
  1391. return 0;
  1392. } /* end of snic_is_abts_pending */
  1393. static int
  1394. snic_dr_clean_single_req(struct snic *snic,
  1395. u32 tag,
  1396. struct scsi_device *lr_sdev)
  1397. {
  1398. struct snic_req_info *rqi = NULL;
  1399. struct snic_tgt *tgt = NULL;
  1400. struct scsi_cmnd *sc = NULL;
  1401. spinlock_t *io_lock = NULL;
  1402. u32 sv_state = 0, tmf = 0;
  1403. DECLARE_COMPLETION_ONSTACK(tm_done);
  1404. unsigned long flags;
  1405. int ret = 0;
  1406. io_lock = snic_io_lock_tag(snic, tag);
  1407. spin_lock_irqsave(io_lock, flags);
  1408. sc = scsi_host_find_tag(snic->shost, tag);
  1409. /* Ignore Cmd that don't belong to Lun Reset device */
  1410. if (!sc || sc->device != lr_sdev)
  1411. goto skip_clean;
  1412. rqi = (struct snic_req_info *) CMD_SP(sc);
  1413. if (!rqi)
  1414. goto skip_clean;
  1415. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  1416. goto skip_clean;
  1417. if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
  1418. (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
  1419. SNIC_SCSI_DBG(snic->shost,
  1420. "clean_single_req: devrst is not pending sc 0x%p\n",
  1421. sc);
  1422. goto skip_clean;
  1423. }
  1424. SNIC_SCSI_DBG(snic->shost,
  1425. "clean_single_req: Found IO in %s on lun\n",
  1426. snic_ioreq_state_to_str(CMD_STATE(sc)));
  1427. /* Save Command State */
  1428. sv_state = CMD_STATE(sc);
  1429. /*
  1430. * Any pending IO issued prior to reset is expected to be
  1431. * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
  1432. * to indicate the IO is abort pending.
  1433. * When IO is completed, the IO will be handed over and handled
  1434. * in this function.
  1435. */
  1436. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  1437. SNIC_BUG_ON(rqi->abts_done);
  1438. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
  1439. rqi->tm_tag = SNIC_TAG_DEV_RST;
  1440. SNIC_SCSI_DBG(snic->shost,
  1441. "clean_single_req:devrst sc 0x%p\n", sc);
  1442. }
  1443. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  1444. rqi->abts_done = &tm_done;
  1445. spin_unlock_irqrestore(io_lock, flags);
  1446. tgt = starget_to_tgt(scsi_target(sc->device));
  1447. if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
  1448. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  1449. else
  1450. tmf = SNIC_ITMF_ABTS_TASK;
  1451. /* Now queue the abort command to firmware */
  1452. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  1453. if (ret) {
  1454. SNIC_HOST_ERR(snic->shost,
  1455. "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
  1456. sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
  1457. spin_lock_irqsave(io_lock, flags);
  1458. rqi = (struct snic_req_info *) CMD_SP(sc);
  1459. if (rqi)
  1460. rqi->abts_done = NULL;
  1461. /* Restore Command State */
  1462. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  1463. CMD_STATE(sc) = sv_state;
  1464. ret = 1;
  1465. goto skip_clean;
  1466. }
  1467. spin_lock_irqsave(io_lock, flags);
  1468. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
  1469. CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
  1470. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
  1471. spin_unlock_irqrestore(io_lock, flags);
  1472. wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
  1473. /* Recheck cmd state to check if it now aborted. */
  1474. spin_lock_irqsave(io_lock, flags);
  1475. rqi = (struct snic_req_info *) CMD_SP(sc);
  1476. if (!rqi) {
  1477. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
  1478. goto skip_clean;
  1479. }
  1480. rqi->abts_done = NULL;
  1481. /* if abort is still pending w/ fw, fail */
  1482. if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
  1483. SNIC_HOST_ERR(snic->shost,
  1484. "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
  1485. sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
  1486. CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
  1487. ret = 1;
  1488. goto skip_clean;
  1489. }
  1490. CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
  1491. CMD_SP(sc) = NULL;
  1492. spin_unlock_irqrestore(io_lock, flags);
  1493. snic_release_req_buf(snic, rqi, sc);
  1494. sc->result = (DID_ERROR << 16);
  1495. sc->scsi_done(sc);
  1496. ret = 0;
  1497. return ret;
  1498. skip_clean:
  1499. spin_unlock_irqrestore(io_lock, flags);
  1500. return ret;
  1501. } /* end of snic_dr_clean_single_req */
  1502. static int
  1503. snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
  1504. {
  1505. struct scsi_device *lr_sdev = lr_sc->device;
  1506. u32 tag = 0;
  1507. int ret = FAILED;
  1508. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1509. if (tag == snic_cmd_tag(lr_sc))
  1510. continue;
  1511. ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
  1512. if (ret) {
  1513. SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
  1514. goto clean_err;
  1515. }
  1516. }
  1517. schedule_timeout(msecs_to_jiffies(100));
  1518. /* Walk through all the cmds and check abts status. */
  1519. if (snic_is_abts_pending(snic, lr_sc)) {
  1520. ret = FAILED;
  1521. goto clean_err;
  1522. }
  1523. ret = 0;
  1524. SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
  1525. return ret;
  1526. clean_err:
  1527. ret = FAILED;
  1528. SNIC_HOST_ERR(snic->shost,
  1529. "Failed to Clean Pending IOs on %s device.\n",
  1530. dev_name(&lr_sdev->sdev_gendev));
  1531. return ret;
  1532. } /* end of snic_dr_clean_pending_req */
  1533. /*
  1534. * snic_dr_finish : Called by snic_device_reset
  1535. */
  1536. static int
  1537. snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
  1538. {
  1539. struct snic_req_info *rqi = NULL;
  1540. spinlock_t *io_lock = NULL;
  1541. unsigned long flags;
  1542. int lr_res = 0;
  1543. int ret = FAILED;
  1544. io_lock = snic_io_lock_hash(snic, sc);
  1545. spin_lock_irqsave(io_lock, flags);
  1546. rqi = (struct snic_req_info *) CMD_SP(sc);
  1547. if (!rqi) {
  1548. spin_unlock_irqrestore(io_lock, flags);
  1549. SNIC_SCSI_DBG(snic->shost,
  1550. "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
  1551. snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
  1552. ret = FAILED;
  1553. goto dr_fini_end;
  1554. }
  1555. rqi->dr_done = NULL;
  1556. lr_res = CMD_LR_STATUS(sc);
  1557. switch (lr_res) {
  1558. case SNIC_INVALID_CODE:
  1559. /* stats */
  1560. SNIC_SCSI_DBG(snic->shost,
  1561. "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
  1562. snic_cmd_tag(sc), CMD_FLAGS(sc));
  1563. CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
  1564. ret = FAILED;
  1565. goto dr_failed;
  1566. case SNIC_STAT_IO_SUCCESS:
  1567. SNIC_SCSI_DBG(snic->shost,
  1568. "dr_fini: Tag %x Dev Reset cmpl\n",
  1569. snic_cmd_tag(sc));
  1570. ret = 0;
  1571. break;
  1572. default:
  1573. SNIC_HOST_ERR(snic->shost,
  1574. "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
  1575. snic_cmd_tag(sc),
  1576. snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
  1577. ret = FAILED;
  1578. goto dr_failed;
  1579. }
  1580. spin_unlock_irqrestore(io_lock, flags);
  1581. /*
  1582. * Cleanup any IOs on this LUN that have still not completed.
  1583. * If any of these fail, then LUN Reset fails.
  1584. * Cleanup cleans all commands on this LUN except
  1585. * the lun reset command. If all cmds get cleaned, the LUN Reset
  1586. * succeeds.
  1587. */
  1588. ret = snic_dr_clean_pending_req(snic, sc);
  1589. if (ret) {
  1590. spin_lock_irqsave(io_lock, flags);
  1591. SNIC_SCSI_DBG(snic->shost,
  1592. "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
  1593. snic_cmd_tag(sc));
  1594. rqi = (struct snic_req_info *) CMD_SP(sc);
  1595. goto dr_failed;
  1596. } else {
  1597. /* Cleanup LUN Reset Command */
  1598. spin_lock_irqsave(io_lock, flags);
  1599. rqi = (struct snic_req_info *) CMD_SP(sc);
  1600. if (rqi)
  1601. ret = SUCCESS; /* Completed Successfully */
  1602. else
  1603. ret = FAILED;
  1604. }
  1605. dr_failed:
  1606. SNIC_BUG_ON(!spin_is_locked(io_lock));
  1607. if (rqi)
  1608. CMD_SP(sc) = NULL;
  1609. spin_unlock_irqrestore(io_lock, flags);
  1610. if (rqi)
  1611. snic_release_req_buf(snic, rqi, sc);
  1612. dr_fini_end:
  1613. return ret;
  1614. } /* end of snic_dr_finish */
  1615. static int
  1616. snic_queue_dr_req(struct snic *snic,
  1617. struct snic_req_info *rqi,
  1618. struct scsi_cmnd *sc)
  1619. {
  1620. /* Add special tag for device reset */
  1621. rqi->tm_tag |= SNIC_TAG_DEV_RST;
  1622. return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
  1623. }
  1624. static int
  1625. snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
  1626. {
  1627. struct snic_req_info *rqi = NULL;
  1628. enum snic_ioreq_state sv_state;
  1629. spinlock_t *io_lock = NULL;
  1630. unsigned long flags;
  1631. DECLARE_COMPLETION_ONSTACK(tm_done);
  1632. int ret = FAILED, tag = snic_cmd_tag(sc);
  1633. io_lock = snic_io_lock_hash(snic, sc);
  1634. spin_lock_irqsave(io_lock, flags);
  1635. CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
  1636. rqi = (struct snic_req_info *) CMD_SP(sc);
  1637. if (!rqi) {
  1638. SNIC_HOST_ERR(snic->shost,
  1639. "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
  1640. tag, CMD_FLAGS(sc));
  1641. spin_unlock_irqrestore(io_lock, flags);
  1642. ret = FAILED;
  1643. goto send_dr_end;
  1644. }
  1645. /* Save Command state to restore in case Queuing failed. */
  1646. sv_state = CMD_STATE(sc);
  1647. CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
  1648. CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
  1649. SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
  1650. rqi->dr_done = &tm_done;
  1651. SNIC_BUG_ON(!rqi->dr_done);
  1652. spin_unlock_irqrestore(io_lock, flags);
  1653. /*
  1654. * The Command state is changed to IOREQ_PENDING,
  1655. * in this case, if the command is completed, the icmnd_cmpl will
  1656. * mark the cmd as completed.
  1657. * This logic still makes LUN Reset is inevitable.
  1658. */
  1659. ret = snic_queue_dr_req(snic, rqi, sc);
  1660. if (ret) {
  1661. SNIC_HOST_ERR(snic->shost,
  1662. "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
  1663. tag, ret, CMD_FLAGS(sc));
  1664. spin_lock_irqsave(io_lock, flags);
  1665. /* Restore State */
  1666. CMD_STATE(sc) = sv_state;
  1667. rqi = (struct snic_req_info *) CMD_SP(sc);
  1668. if (rqi)
  1669. rqi->dr_done = NULL;
  1670. /* rqi is freed in caller. */
  1671. spin_unlock_irqrestore(io_lock, flags);
  1672. ret = FAILED;
  1673. goto send_dr_end;
  1674. }
  1675. spin_lock_irqsave(io_lock, flags);
  1676. CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
  1677. spin_unlock_irqrestore(io_lock, flags);
  1678. ret = 0;
  1679. wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
  1680. send_dr_end:
  1681. return ret;
  1682. }
  1683. /*
  1684. * auxillary funciton to check lun reset op is supported or not
  1685. * Not supported if returns 0
  1686. */
  1687. static int
  1688. snic_dev_reset_supported(struct scsi_device *sdev)
  1689. {
  1690. struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
  1691. if (tgt->tdata.typ == SNIC_TGT_DAS)
  1692. return 0;
  1693. return 1;
  1694. }
  1695. static void
  1696. snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
  1697. {
  1698. struct snic_req_info *rqi = NULL;
  1699. spinlock_t *io_lock = NULL;
  1700. unsigned long flags;
  1701. u32 start_time = jiffies;
  1702. io_lock = snic_io_lock_hash(snic, sc);
  1703. spin_lock_irqsave(io_lock, flags);
  1704. rqi = (struct snic_req_info *) CMD_SP(sc);
  1705. if (rqi) {
  1706. start_time = rqi->start_time;
  1707. CMD_SP(sc) = NULL;
  1708. }
  1709. CMD_FLAGS(sc) |= flag;
  1710. spin_unlock_irqrestore(io_lock, flags);
  1711. if (rqi)
  1712. snic_release_req_buf(snic, rqi, sc);
  1713. SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
  1714. jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
  1715. SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1716. }
  1717. /*
  1718. * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
  1719. * fail to get aborted. It calls driver's eh_device_reset with a SCSI
  1720. * command on the LUN.
  1721. */
  1722. int
  1723. snic_device_reset(struct scsi_cmnd *sc)
  1724. {
  1725. struct Scsi_Host *shost = sc->device->host;
  1726. struct snic *snic = shost_priv(shost);
  1727. struct snic_req_info *rqi = NULL;
  1728. int tag = snic_cmd_tag(sc);
  1729. int start_time = jiffies;
  1730. int ret = FAILED;
  1731. int dr_supp = 0;
  1732. SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
  1733. sc, sc->cmnd[0], sc->request,
  1734. snic_cmd_tag(sc));
  1735. dr_supp = snic_dev_reset_supported(sc->device);
  1736. if (!dr_supp) {
  1737. /* device reset op is not supported */
  1738. SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
  1739. snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
  1740. goto dev_rst_end;
  1741. }
  1742. if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
  1743. snic_unlink_and_release_req(snic, sc, 0);
  1744. SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
  1745. goto dev_rst_end;
  1746. }
  1747. /* There is no tag when lun reset is issue through ioctl. */
  1748. if (unlikely(tag <= SNIC_NO_TAG)) {
  1749. SNIC_HOST_INFO(snic->shost,
  1750. "Devrst: LUN Reset Recvd thru IOCTL.\n");
  1751. rqi = snic_req_init(snic, 0);
  1752. if (!rqi)
  1753. goto dev_rst_end;
  1754. memset(scsi_cmd_priv(sc), 0,
  1755. sizeof(struct snic_internal_io_state));
  1756. CMD_SP(sc) = (char *)rqi;
  1757. CMD_FLAGS(sc) = SNIC_NO_FLAGS;
  1758. /* Add special tag for dr coming from user spc */
  1759. rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
  1760. rqi->sc = sc;
  1761. }
  1762. ret = snic_send_dr_and_wait(snic, sc);
  1763. if (ret) {
  1764. SNIC_HOST_ERR(snic->shost,
  1765. "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
  1766. tag, ret);
  1767. snic_unlink_and_release_req(snic, sc, 0);
  1768. goto dev_rst_end;
  1769. }
  1770. ret = snic_dr_finish(snic, sc);
  1771. dev_rst_end:
  1772. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  1773. jiffies_to_msecs(jiffies - start_time),
  1774. 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1775. SNIC_SCSI_DBG(snic->shost,
  1776. "Devrst: Returning from Device Reset : %s\n",
  1777. (ret == SUCCESS) ? "SUCCESS" : "FAILED");
  1778. return ret;
  1779. } /* end of snic_device_reset */
  1780. /*
  1781. * SCSI Error handling calls driver's eh_host_reset if all prior
  1782. * error handling levels return FAILED.
  1783. *
  1784. * Host Reset is the highest level of error recovery. If this fails, then
  1785. * host is offlined by SCSI.
  1786. */
  1787. /*
  1788. * snic_issue_hba_reset : Queues FW Reset Request.
  1789. */
  1790. static int
  1791. snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
  1792. {
  1793. struct snic_req_info *rqi = NULL;
  1794. struct snic_host_req *req = NULL;
  1795. spinlock_t *io_lock = NULL;
  1796. DECLARE_COMPLETION_ONSTACK(wait);
  1797. unsigned long flags;
  1798. int ret = -ENOMEM;
  1799. rqi = snic_req_init(snic, 0);
  1800. if (!rqi) {
  1801. ret = -ENOMEM;
  1802. goto hba_rst_end;
  1803. }
  1804. if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
  1805. memset(scsi_cmd_priv(sc), 0,
  1806. sizeof(struct snic_internal_io_state));
  1807. SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
  1808. rqi->sc = sc;
  1809. }
  1810. req = rqi_to_req(rqi);
  1811. io_lock = snic_io_lock_hash(snic, sc);
  1812. spin_lock_irqsave(io_lock, flags);
  1813. SNIC_BUG_ON(CMD_SP(sc) != NULL);
  1814. CMD_STATE(sc) = SNIC_IOREQ_PENDING;
  1815. CMD_SP(sc) = (char *) rqi;
  1816. CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
  1817. snic->remove_wait = &wait;
  1818. spin_unlock_irqrestore(io_lock, flags);
  1819. /* Initialize Request */
  1820. snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
  1821. snic->config.hid, 0, (ulong) rqi);
  1822. req->u.reset.flags = 0;
  1823. ret = snic_queue_wq_desc(snic, req, sizeof(*req));
  1824. if (ret) {
  1825. SNIC_HOST_ERR(snic->shost,
  1826. "issu_hr:Queuing HBA Reset Failed. w err %d\n",
  1827. ret);
  1828. goto hba_rst_err;
  1829. }
  1830. spin_lock_irqsave(io_lock, flags);
  1831. CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
  1832. spin_unlock_irqrestore(io_lock, flags);
  1833. atomic64_inc(&snic->s_stats.reset.hba_resets);
  1834. SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
  1835. wait_for_completion_timeout(snic->remove_wait,
  1836. SNIC_HOST_RESET_TIMEOUT);
  1837. if (snic_get_state(snic) == SNIC_FWRESET) {
  1838. SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
  1839. ret = -ETIMEDOUT;
  1840. goto hba_rst_err;
  1841. }
  1842. spin_lock_irqsave(io_lock, flags);
  1843. snic->remove_wait = NULL;
  1844. rqi = (struct snic_req_info *) CMD_SP(sc);
  1845. CMD_SP(sc) = NULL;
  1846. spin_unlock_irqrestore(io_lock, flags);
  1847. if (rqi)
  1848. snic_req_free(snic, rqi);
  1849. ret = 0;
  1850. return ret;
  1851. hba_rst_err:
  1852. spin_lock_irqsave(io_lock, flags);
  1853. snic->remove_wait = NULL;
  1854. rqi = (struct snic_req_info *) CMD_SP(sc);
  1855. CMD_SP(sc) = NULL;
  1856. spin_unlock_irqrestore(io_lock, flags);
  1857. if (rqi)
  1858. snic_req_free(snic, rqi);
  1859. hba_rst_end:
  1860. SNIC_HOST_ERR(snic->shost,
  1861. "reset:HBA Reset Failed w/ err = %d.\n",
  1862. ret);
  1863. return ret;
  1864. } /* end of snic_issue_hba_reset */
  1865. int
  1866. snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
  1867. {
  1868. struct snic *snic = shost_priv(shost);
  1869. enum snic_state sv_state;
  1870. unsigned long flags;
  1871. int ret = FAILED;
  1872. /* Set snic state as SNIC_FWRESET*/
  1873. sv_state = snic_get_state(snic);
  1874. spin_lock_irqsave(&snic->snic_lock, flags);
  1875. if (snic_get_state(snic) == SNIC_FWRESET) {
  1876. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1877. SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
  1878. msleep(SNIC_HOST_RESET_TIMEOUT);
  1879. ret = SUCCESS;
  1880. goto reset_end;
  1881. }
  1882. snic_set_state(snic, SNIC_FWRESET);
  1883. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1884. /* Wait for all the IOs that are entered in Qcmd */
  1885. while (atomic_read(&snic->ios_inflight))
  1886. schedule_timeout(msecs_to_jiffies(1));
  1887. ret = snic_issue_hba_reset(snic, sc);
  1888. if (ret) {
  1889. SNIC_HOST_ERR(shost,
  1890. "reset:Host Reset Failed w/ err %d.\n",
  1891. ret);
  1892. spin_lock_irqsave(&snic->snic_lock, flags);
  1893. snic_set_state(snic, sv_state);
  1894. spin_unlock_irqrestore(&snic->snic_lock, flags);
  1895. atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
  1896. ret = FAILED;
  1897. goto reset_end;
  1898. }
  1899. ret = SUCCESS;
  1900. reset_end:
  1901. return ret;
  1902. } /* end of snic_reset */
  1903. /*
  1904. * SCSI Error handling calls driver's eh_host_reset if all prior
  1905. * error handling levels return FAILED.
  1906. *
  1907. * Host Reset is the highest level of error recovery. If this fails, then
  1908. * host is offlined by SCSI.
  1909. */
  1910. int
  1911. snic_host_reset(struct scsi_cmnd *sc)
  1912. {
  1913. struct Scsi_Host *shost = sc->device->host;
  1914. u32 start_time = jiffies;
  1915. int ret = FAILED;
  1916. SNIC_SCSI_DBG(shost,
  1917. "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
  1918. sc, sc->cmnd[0], sc->request,
  1919. snic_cmd_tag(sc), CMD_FLAGS(sc));
  1920. ret = snic_reset(shost, sc);
  1921. SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
  1922. jiffies_to_msecs(jiffies - start_time),
  1923. 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
  1924. return ret;
  1925. } /* end of snic_host_reset */
  1926. /*
  1927. * snic_cmpl_pending_tmreq : Caller should hold io_lock
  1928. */
  1929. static void
  1930. snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
  1931. {
  1932. struct snic_req_info *rqi = NULL;
  1933. SNIC_SCSI_DBG(snic->shost,
  1934. "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
  1935. sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
  1936. /*
  1937. * CASE : FW didn't post itmf completion due to PCIe Errors.
  1938. * Marking the abort status as Success to call scsi completion
  1939. * in snic_abort_finish()
  1940. */
  1941. CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
  1942. rqi = (struct snic_req_info *) CMD_SP(sc);
  1943. if (!rqi)
  1944. return;
  1945. if (rqi->dr_done)
  1946. complete(rqi->dr_done);
  1947. else if (rqi->abts_done)
  1948. complete(rqi->abts_done);
  1949. }
  1950. /*
  1951. * snic_scsi_cleanup: Walks through tag map and releases the reqs
  1952. */
  1953. static void
  1954. snic_scsi_cleanup(struct snic *snic, int ex_tag)
  1955. {
  1956. struct snic_req_info *rqi = NULL;
  1957. struct scsi_cmnd *sc = NULL;
  1958. spinlock_t *io_lock = NULL;
  1959. unsigned long flags;
  1960. int tag;
  1961. u64 st_time = 0;
  1962. SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
  1963. for (tag = 0; tag < snic->max_tag_id; tag++) {
  1964. /* Skip ex_tag */
  1965. if (tag == ex_tag)
  1966. continue;
  1967. io_lock = snic_io_lock_tag(snic, tag);
  1968. spin_lock_irqsave(io_lock, flags);
  1969. sc = scsi_host_find_tag(snic->shost, tag);
  1970. if (!sc) {
  1971. spin_unlock_irqrestore(io_lock, flags);
  1972. continue;
  1973. }
  1974. if (unlikely(snic_tmreq_pending(sc))) {
  1975. /*
  1976. * When FW Completes reset w/o sending completions
  1977. * for outstanding ios.
  1978. */
  1979. snic_cmpl_pending_tmreq(snic, sc);
  1980. spin_unlock_irqrestore(io_lock, flags);
  1981. continue;
  1982. }
  1983. rqi = (struct snic_req_info *) CMD_SP(sc);
  1984. if (!rqi) {
  1985. spin_unlock_irqrestore(io_lock, flags);
  1986. goto cleanup;
  1987. }
  1988. SNIC_SCSI_DBG(snic->shost,
  1989. "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
  1990. sc, rqi, tag, CMD_FLAGS(sc));
  1991. CMD_SP(sc) = NULL;
  1992. CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
  1993. spin_unlock_irqrestore(io_lock, flags);
  1994. st_time = rqi->start_time;
  1995. SNIC_HOST_INFO(snic->shost,
  1996. "sc_clean: Releasing rqi %p : flags 0x%llx\n",
  1997. rqi, CMD_FLAGS(sc));
  1998. snic_release_req_buf(snic, rqi, sc);
  1999. cleanup:
  2000. sc->result = DID_TRANSPORT_DISRUPTED << 16;
  2001. SNIC_HOST_INFO(snic->shost,
  2002. "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
  2003. sc, sc->request->tag, CMD_FLAGS(sc), rqi,
  2004. jiffies_to_msecs(jiffies - st_time));
  2005. /* Update IO stats */
  2006. snic_stats_update_io_cmpl(&snic->s_stats);
  2007. if (sc->scsi_done) {
  2008. SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
  2009. jiffies_to_msecs(jiffies - st_time), 0,
  2010. SNIC_TRC_CMD(sc),
  2011. SNIC_TRC_CMD_STATE_FLAGS(sc));
  2012. sc->scsi_done(sc);
  2013. }
  2014. }
  2015. } /* end of snic_scsi_cleanup */
  2016. void
  2017. snic_shutdown_scsi_cleanup(struct snic *snic)
  2018. {
  2019. SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
  2020. snic_scsi_cleanup(snic, SCSI_NO_TAG);
  2021. } /* end of snic_shutdown_scsi_cleanup */
  2022. /*
  2023. * snic_internal_abort_io
  2024. * called by : snic_tgt_scsi_abort_io
  2025. */
  2026. static int
  2027. snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
  2028. {
  2029. struct snic_req_info *rqi = NULL;
  2030. spinlock_t *io_lock = NULL;
  2031. unsigned long flags;
  2032. u32 sv_state = 0;
  2033. int ret = 0;
  2034. io_lock = snic_io_lock_hash(snic, sc);
  2035. spin_lock_irqsave(io_lock, flags);
  2036. rqi = (struct snic_req_info *) CMD_SP(sc);
  2037. if (!rqi)
  2038. goto skip_internal_abts;
  2039. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  2040. goto skip_internal_abts;
  2041. if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
  2042. (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
  2043. SNIC_SCSI_DBG(snic->shost,
  2044. "internal_abts: dev rst not pending sc 0x%p\n",
  2045. sc);
  2046. goto skip_internal_abts;
  2047. }
  2048. if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
  2049. SNIC_SCSI_DBG(snic->shost,
  2050. "internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
  2051. sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
  2052. goto skip_internal_abts;
  2053. }
  2054. sv_state = CMD_STATE(sc);
  2055. CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
  2056. CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
  2057. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
  2058. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
  2059. /* stats */
  2060. rqi->tm_tag = SNIC_TAG_DEV_RST;
  2061. SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
  2062. }
  2063. SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
  2064. snic_cmd_tag(sc));
  2065. SNIC_BUG_ON(rqi->abts_done);
  2066. spin_unlock_irqrestore(io_lock, flags);
  2067. ret = snic_queue_abort_req(snic, rqi, sc, tmf);
  2068. if (ret) {
  2069. SNIC_HOST_ERR(snic->shost,
  2070. "internal_abts: Tag = %x , Failed w/ err = %d\n",
  2071. snic_cmd_tag(sc), ret);
  2072. spin_lock_irqsave(io_lock, flags);
  2073. if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
  2074. CMD_STATE(sc) = sv_state;
  2075. goto skip_internal_abts;
  2076. }
  2077. spin_lock_irqsave(io_lock, flags);
  2078. if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
  2079. CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
  2080. else
  2081. CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
  2082. ret = SUCCESS;
  2083. skip_internal_abts:
  2084. SNIC_BUG_ON(!spin_is_locked(io_lock));
  2085. spin_unlock_irqrestore(io_lock, flags);
  2086. return ret;
  2087. } /* end of snic_internal_abort_io */
  2088. /*
  2089. * snic_tgt_scsi_abort_io : called by snic_tgt_del
  2090. */
  2091. int
  2092. snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
  2093. {
  2094. struct snic *snic = NULL;
  2095. struct scsi_cmnd *sc = NULL;
  2096. struct snic_tgt *sc_tgt = NULL;
  2097. spinlock_t *io_lock = NULL;
  2098. unsigned long flags;
  2099. int ret = 0, tag, abt_cnt = 0, tmf = 0;
  2100. if (!tgt)
  2101. return -1;
  2102. snic = shost_priv(snic_tgt_to_shost(tgt));
  2103. SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
  2104. if (tgt->tdata.typ == SNIC_TGT_DAS)
  2105. tmf = SNIC_ITMF_ABTS_TASK;
  2106. else
  2107. tmf = SNIC_ITMF_ABTS_TASK_TERM;
  2108. for (tag = 0; tag < snic->max_tag_id; tag++) {
  2109. io_lock = snic_io_lock_tag(snic, tag);
  2110. spin_lock_irqsave(io_lock, flags);
  2111. sc = scsi_host_find_tag(snic->shost, tag);
  2112. if (!sc) {
  2113. spin_unlock_irqrestore(io_lock, flags);
  2114. continue;
  2115. }
  2116. sc_tgt = starget_to_tgt(scsi_target(sc->device));
  2117. if (sc_tgt != tgt) {
  2118. spin_unlock_irqrestore(io_lock, flags);
  2119. continue;
  2120. }
  2121. spin_unlock_irqrestore(io_lock, flags);
  2122. ret = snic_internal_abort_io(snic, sc, tmf);
  2123. if (ret < 0) {
  2124. SNIC_HOST_ERR(snic->shost,
  2125. "tgt_abt_io: Tag %x, Failed w err = %d\n",
  2126. tag, ret);
  2127. continue;
  2128. }
  2129. if (ret == SUCCESS)
  2130. abt_cnt++;
  2131. }
  2132. SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
  2133. return 0;
  2134. } /* end of snic_tgt_scsi_abort_io */