snic_scsi.c 65 KB

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