snic_scsi.c 67 KB

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