csio_scsi.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/device.h>
  35. #include <linux/delay.h>
  36. #include <linux/ctype.h>
  37. #include <linux/kernel.h>
  38. #include <linux/slab.h>
  39. #include <linux/string.h>
  40. #include <linux/compiler.h>
  41. #include <linux/export.h>
  42. #include <linux/module.h>
  43. #include <asm/unaligned.h>
  44. #include <asm/page.h>
  45. #include <scsi/scsi.h>
  46. #include <scsi/scsi_device.h>
  47. #include <scsi/scsi_transport_fc.h>
  48. #include "csio_hw.h"
  49. #include "csio_lnode.h"
  50. #include "csio_rnode.h"
  51. #include "csio_scsi.h"
  52. #include "csio_init.h"
  53. int csio_scsi_eqsize = 65536;
  54. int csio_scsi_iqlen = 128;
  55. int csio_scsi_ioreqs = 2048;
  56. uint32_t csio_max_scan_tmo;
  57. uint32_t csio_delta_scan_tmo = 5;
  58. int csio_lun_qdepth = 32;
  59. static int csio_ddp_descs = 128;
  60. static int csio_do_abrt_cls(struct csio_hw *,
  61. struct csio_ioreq *, bool);
  62. static void csio_scsis_uninit(struct csio_ioreq *, enum csio_scsi_ev);
  63. static void csio_scsis_io_active(struct csio_ioreq *, enum csio_scsi_ev);
  64. static void csio_scsis_tm_active(struct csio_ioreq *, enum csio_scsi_ev);
  65. static void csio_scsis_aborting(struct csio_ioreq *, enum csio_scsi_ev);
  66. static void csio_scsis_closing(struct csio_ioreq *, enum csio_scsi_ev);
  67. static void csio_scsis_shost_cmpl_await(struct csio_ioreq *, enum csio_scsi_ev);
  68. /*
  69. * csio_scsi_match_io - Match an ioreq with the given SCSI level data.
  70. * @ioreq: The I/O request
  71. * @sld: Level information
  72. *
  73. * Should be called with lock held.
  74. *
  75. */
  76. static bool
  77. csio_scsi_match_io(struct csio_ioreq *ioreq, struct csio_scsi_level_data *sld)
  78. {
  79. struct scsi_cmnd *scmnd = csio_scsi_cmnd(ioreq);
  80. switch (sld->level) {
  81. case CSIO_LEV_LUN:
  82. if (scmnd == NULL)
  83. return false;
  84. return ((ioreq->lnode == sld->lnode) &&
  85. (ioreq->rnode == sld->rnode) &&
  86. ((uint64_t)scmnd->device->lun == sld->oslun));
  87. case CSIO_LEV_RNODE:
  88. return ((ioreq->lnode == sld->lnode) &&
  89. (ioreq->rnode == sld->rnode));
  90. case CSIO_LEV_LNODE:
  91. return (ioreq->lnode == sld->lnode);
  92. case CSIO_LEV_ALL:
  93. return true;
  94. default:
  95. return false;
  96. }
  97. }
  98. /*
  99. * csio_scsi_gather_active_ios - Gather active I/Os based on level
  100. * @scm: SCSI module
  101. * @sld: Level information
  102. * @dest: The queue where these I/Os have to be gathered.
  103. *
  104. * Should be called with lock held.
  105. */
  106. static void
  107. csio_scsi_gather_active_ios(struct csio_scsim *scm,
  108. struct csio_scsi_level_data *sld,
  109. struct list_head *dest)
  110. {
  111. struct list_head *tmp, *next;
  112. if (list_empty(&scm->active_q))
  113. return;
  114. /* Just splice the entire active_q into dest */
  115. if (sld->level == CSIO_LEV_ALL) {
  116. list_splice_tail_init(&scm->active_q, dest);
  117. return;
  118. }
  119. list_for_each_safe(tmp, next, &scm->active_q) {
  120. if (csio_scsi_match_io((struct csio_ioreq *)tmp, sld)) {
  121. list_del_init(tmp);
  122. list_add_tail(tmp, dest);
  123. }
  124. }
  125. }
  126. static inline bool
  127. csio_scsi_itnexus_loss_error(uint16_t error)
  128. {
  129. switch (error) {
  130. case FW_ERR_LINK_DOWN:
  131. case FW_RDEV_NOT_READY:
  132. case FW_ERR_RDEV_LOST:
  133. case FW_ERR_RDEV_LOGO:
  134. case FW_ERR_RDEV_IMPL_LOGO:
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. static inline void
  140. csio_scsi_tag(struct scsi_cmnd *scmnd, uint8_t *tag, uint8_t hq,
  141. uint8_t oq, uint8_t sq)
  142. {
  143. char stag[2];
  144. if (scsi_populate_tag_msg(scmnd, stag)) {
  145. switch (stag[0]) {
  146. case HEAD_OF_QUEUE_TAG:
  147. *tag = hq;
  148. break;
  149. case ORDERED_QUEUE_TAG:
  150. *tag = oq;
  151. break;
  152. default:
  153. *tag = sq;
  154. break;
  155. }
  156. } else
  157. *tag = 0;
  158. }
  159. /*
  160. * csio_scsi_fcp_cmnd - Frame the SCSI FCP command paylod.
  161. * @req: IO req structure.
  162. * @addr: DMA location to place the payload.
  163. *
  164. * This routine is shared between FCP_WRITE, FCP_READ and FCP_CMD requests.
  165. */
  166. static inline void
  167. csio_scsi_fcp_cmnd(struct csio_ioreq *req, void *addr)
  168. {
  169. struct fcp_cmnd *fcp_cmnd = (struct fcp_cmnd *)addr;
  170. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  171. /* Check for Task Management */
  172. if (likely(scmnd->SCp.Message == 0)) {
  173. int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
  174. fcp_cmnd->fc_tm_flags = 0;
  175. fcp_cmnd->fc_cmdref = 0;
  176. fcp_cmnd->fc_pri_ta = 0;
  177. memcpy(fcp_cmnd->fc_cdb, scmnd->cmnd, 16);
  178. csio_scsi_tag(scmnd, &fcp_cmnd->fc_pri_ta,
  179. FCP_PTA_HEADQ, FCP_PTA_ORDERED, FCP_PTA_SIMPLE);
  180. fcp_cmnd->fc_dl = cpu_to_be32(scsi_bufflen(scmnd));
  181. if (req->nsge)
  182. if (req->datadir == DMA_TO_DEVICE)
  183. fcp_cmnd->fc_flags = FCP_CFL_WRDATA;
  184. else
  185. fcp_cmnd->fc_flags = FCP_CFL_RDDATA;
  186. else
  187. fcp_cmnd->fc_flags = 0;
  188. } else {
  189. memset(fcp_cmnd, 0, sizeof(*fcp_cmnd));
  190. int_to_scsilun(scmnd->device->lun, &fcp_cmnd->fc_lun);
  191. fcp_cmnd->fc_tm_flags = (uint8_t)scmnd->SCp.Message;
  192. }
  193. }
  194. /*
  195. * csio_scsi_init_cmd_wr - Initialize the SCSI CMD WR.
  196. * @req: IO req structure.
  197. * @addr: DMA location to place the payload.
  198. * @size: Size of WR (including FW WR + immed data + rsp SG entry
  199. *
  200. * Wrapper for populating fw_scsi_cmd_wr.
  201. */
  202. static inline void
  203. csio_scsi_init_cmd_wr(struct csio_ioreq *req, void *addr, uint32_t size)
  204. {
  205. struct csio_hw *hw = req->lnode->hwp;
  206. struct csio_rnode *rn = req->rnode;
  207. struct fw_scsi_cmd_wr *wr = (struct fw_scsi_cmd_wr *)addr;
  208. struct csio_dma_buf *dma_buf;
  209. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  210. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_CMD_WR) |
  211. FW_SCSI_CMD_WR_IMMDLEN(imm));
  212. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  213. FW_WR_LEN16(
  214. DIV_ROUND_UP(size, 16)));
  215. wr->cookie = (uintptr_t) req;
  216. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  217. wr->tmo_val = (uint8_t) req->tmo;
  218. wr->r3 = 0;
  219. memset(&wr->r5, 0, 8);
  220. /* Get RSP DMA buffer */
  221. dma_buf = &req->dma_buf;
  222. /* Prepare RSP SGL */
  223. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  224. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  225. wr->r6 = 0;
  226. wr->u.fcoe.ctl_pri = 0;
  227. wr->u.fcoe.cp_en_class = 0;
  228. wr->u.fcoe.r4_lo[0] = 0;
  229. wr->u.fcoe.r4_lo[1] = 0;
  230. /* Frame a FCP command */
  231. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)addr +
  232. sizeof(struct fw_scsi_cmd_wr)));
  233. }
  234. #define CSIO_SCSI_CMD_WR_SZ(_imm) \
  235. (sizeof(struct fw_scsi_cmd_wr) + /* WR size */ \
  236. ALIGN((_imm), 16)) /* Immed data */
  237. #define CSIO_SCSI_CMD_WR_SZ_16(_imm) \
  238. (ALIGN(CSIO_SCSI_CMD_WR_SZ((_imm)), 16))
  239. /*
  240. * csio_scsi_cmd - Create a SCSI CMD WR.
  241. * @req: IO req structure.
  242. *
  243. * Gets a WR slot in the ingress queue and initializes it with SCSI CMD WR.
  244. *
  245. */
  246. static inline void
  247. csio_scsi_cmd(struct csio_ioreq *req)
  248. {
  249. struct csio_wr_pair wrp;
  250. struct csio_hw *hw = req->lnode->hwp;
  251. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  252. uint32_t size = CSIO_SCSI_CMD_WR_SZ_16(scsim->proto_cmd_len);
  253. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  254. if (unlikely(req->drv_status != 0))
  255. return;
  256. if (wrp.size1 >= size) {
  257. /* Initialize WR in one shot */
  258. csio_scsi_init_cmd_wr(req, wrp.addr1, size);
  259. } else {
  260. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  261. /*
  262. * Make a temporary copy of the WR and write back
  263. * the copy into the WR pair.
  264. */
  265. csio_scsi_init_cmd_wr(req, (void *)tmpwr, size);
  266. memcpy(wrp.addr1, tmpwr, wrp.size1);
  267. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  268. }
  269. }
  270. /*
  271. * csio_scsi_init_ulptx_dsgl - Fill in a ULP_TX_SC_DSGL
  272. * @hw: HW module
  273. * @req: IO request
  274. * @sgl: ULP TX SGL pointer.
  275. *
  276. */
  277. static inline void
  278. csio_scsi_init_ultptx_dsgl(struct csio_hw *hw, struct csio_ioreq *req,
  279. struct ulptx_sgl *sgl)
  280. {
  281. struct ulptx_sge_pair *sge_pair = NULL;
  282. struct scatterlist *sgel;
  283. uint32_t i = 0;
  284. uint32_t xfer_len;
  285. struct list_head *tmp;
  286. struct csio_dma_buf *dma_buf;
  287. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  288. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_MORE |
  289. ULPTX_NSGE(req->nsge));
  290. /* Now add the data SGLs */
  291. if (likely(!req->dcopy)) {
  292. scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
  293. if (i == 0) {
  294. sgl->addr0 = cpu_to_be64(sg_dma_address(sgel));
  295. sgl->len0 = cpu_to_be32(sg_dma_len(sgel));
  296. sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
  297. continue;
  298. }
  299. if ((i - 1) & 0x1) {
  300. sge_pair->addr[1] = cpu_to_be64(
  301. sg_dma_address(sgel));
  302. sge_pair->len[1] = cpu_to_be32(
  303. sg_dma_len(sgel));
  304. sge_pair++;
  305. } else {
  306. sge_pair->addr[0] = cpu_to_be64(
  307. sg_dma_address(sgel));
  308. sge_pair->len[0] = cpu_to_be32(
  309. sg_dma_len(sgel));
  310. }
  311. }
  312. } else {
  313. /* Program sg elements with driver's DDP buffer */
  314. xfer_len = scsi_bufflen(scmnd);
  315. list_for_each(tmp, &req->gen_list) {
  316. dma_buf = (struct csio_dma_buf *)tmp;
  317. if (i == 0) {
  318. sgl->addr0 = cpu_to_be64(dma_buf->paddr);
  319. sgl->len0 = cpu_to_be32(
  320. min(xfer_len, dma_buf->len));
  321. sge_pair = (struct ulptx_sge_pair *)(sgl + 1);
  322. } else if ((i - 1) & 0x1) {
  323. sge_pair->addr[1] = cpu_to_be64(dma_buf->paddr);
  324. sge_pair->len[1] = cpu_to_be32(
  325. min(xfer_len, dma_buf->len));
  326. sge_pair++;
  327. } else {
  328. sge_pair->addr[0] = cpu_to_be64(dma_buf->paddr);
  329. sge_pair->len[0] = cpu_to_be32(
  330. min(xfer_len, dma_buf->len));
  331. }
  332. xfer_len -= min(xfer_len, dma_buf->len);
  333. i++;
  334. }
  335. }
  336. }
  337. /*
  338. * csio_scsi_init_read_wr - Initialize the READ SCSI WR.
  339. * @req: IO req structure.
  340. * @wrp: DMA location to place the payload.
  341. * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
  342. *
  343. * Wrapper for populating fw_scsi_read_wr.
  344. */
  345. static inline void
  346. csio_scsi_init_read_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
  347. {
  348. struct csio_hw *hw = req->lnode->hwp;
  349. struct csio_rnode *rn = req->rnode;
  350. struct fw_scsi_read_wr *wr = (struct fw_scsi_read_wr *)wrp;
  351. struct ulptx_sgl *sgl;
  352. struct csio_dma_buf *dma_buf;
  353. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  354. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  355. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_READ_WR) |
  356. FW_SCSI_READ_WR_IMMDLEN(imm));
  357. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  358. FW_WR_LEN16(DIV_ROUND_UP(size, 16)));
  359. wr->cookie = (uintptr_t)req;
  360. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  361. wr->tmo_val = (uint8_t)(req->tmo);
  362. wr->use_xfer_cnt = 1;
  363. wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  364. wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  365. /* Get RSP DMA buffer */
  366. dma_buf = &req->dma_buf;
  367. /* Prepare RSP SGL */
  368. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  369. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  370. wr->r4 = 0;
  371. wr->u.fcoe.ctl_pri = 0;
  372. wr->u.fcoe.cp_en_class = 0;
  373. wr->u.fcoe.r3_lo[0] = 0;
  374. wr->u.fcoe.r3_lo[1] = 0;
  375. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
  376. sizeof(struct fw_scsi_read_wr)));
  377. /* Move WR pointer past command and immediate data */
  378. sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
  379. sizeof(struct fw_scsi_read_wr) + ALIGN(imm, 16));
  380. /* Fill in the DSGL */
  381. csio_scsi_init_ultptx_dsgl(hw, req, sgl);
  382. }
  383. /*
  384. * csio_scsi_init_write_wr - Initialize the WRITE SCSI WR.
  385. * @req: IO req structure.
  386. * @wrp: DMA location to place the payload.
  387. * @size: Size of WR (including FW WR + immed data + rsp SG entry + data SGL
  388. *
  389. * Wrapper for populating fw_scsi_write_wr.
  390. */
  391. static inline void
  392. csio_scsi_init_write_wr(struct csio_ioreq *req, void *wrp, uint32_t size)
  393. {
  394. struct csio_hw *hw = req->lnode->hwp;
  395. struct csio_rnode *rn = req->rnode;
  396. struct fw_scsi_write_wr *wr = (struct fw_scsi_write_wr *)wrp;
  397. struct ulptx_sgl *sgl;
  398. struct csio_dma_buf *dma_buf;
  399. uint8_t imm = csio_hw_to_scsim(hw)->proto_cmd_len;
  400. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  401. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_WRITE_WR) |
  402. FW_SCSI_WRITE_WR_IMMDLEN(imm));
  403. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  404. FW_WR_LEN16(DIV_ROUND_UP(size, 16)));
  405. wr->cookie = (uintptr_t)req;
  406. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  407. wr->tmo_val = (uint8_t)(req->tmo);
  408. wr->use_xfer_cnt = 1;
  409. wr->xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  410. wr->ini_xfer_cnt = cpu_to_be32(scsi_bufflen(scmnd));
  411. /* Get RSP DMA buffer */
  412. dma_buf = &req->dma_buf;
  413. /* Prepare RSP SGL */
  414. wr->rsp_dmalen = cpu_to_be32(dma_buf->len);
  415. wr->rsp_dmaaddr = cpu_to_be64(dma_buf->paddr);
  416. wr->r4 = 0;
  417. wr->u.fcoe.ctl_pri = 0;
  418. wr->u.fcoe.cp_en_class = 0;
  419. wr->u.fcoe.r3_lo[0] = 0;
  420. wr->u.fcoe.r3_lo[1] = 0;
  421. csio_scsi_fcp_cmnd(req, (void *)((uintptr_t)wrp +
  422. sizeof(struct fw_scsi_write_wr)));
  423. /* Move WR pointer past command and immediate data */
  424. sgl = (struct ulptx_sgl *)((uintptr_t)wrp +
  425. sizeof(struct fw_scsi_write_wr) + ALIGN(imm, 16));
  426. /* Fill in the DSGL */
  427. csio_scsi_init_ultptx_dsgl(hw, req, sgl);
  428. }
  429. /* Calculate WR size needed for fw_scsi_read_wr/fw_scsi_write_wr */
  430. #define CSIO_SCSI_DATA_WRSZ(req, oper, sz, imm) \
  431. do { \
  432. (sz) = sizeof(struct fw_scsi_##oper##_wr) + /* WR size */ \
  433. ALIGN((imm), 16) + /* Immed data */ \
  434. sizeof(struct ulptx_sgl); /* ulptx_sgl */ \
  435. \
  436. if (unlikely((req)->nsge > 1)) \
  437. (sz) += (sizeof(struct ulptx_sge_pair) * \
  438. (ALIGN(((req)->nsge - 1), 2) / 2)); \
  439. /* Data SGE */ \
  440. } while (0)
  441. /*
  442. * csio_scsi_read - Create a SCSI READ WR.
  443. * @req: IO req structure.
  444. *
  445. * Gets a WR slot in the ingress queue and initializes it with
  446. * SCSI READ WR.
  447. *
  448. */
  449. static inline void
  450. csio_scsi_read(struct csio_ioreq *req)
  451. {
  452. struct csio_wr_pair wrp;
  453. uint32_t size;
  454. struct csio_hw *hw = req->lnode->hwp;
  455. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  456. CSIO_SCSI_DATA_WRSZ(req, read, size, scsim->proto_cmd_len);
  457. size = ALIGN(size, 16);
  458. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  459. if (likely(req->drv_status == 0)) {
  460. if (likely(wrp.size1 >= size)) {
  461. /* Initialize WR in one shot */
  462. csio_scsi_init_read_wr(req, wrp.addr1, size);
  463. } else {
  464. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  465. /*
  466. * Make a temporary copy of the WR and write back
  467. * the copy into the WR pair.
  468. */
  469. csio_scsi_init_read_wr(req, (void *)tmpwr, size);
  470. memcpy(wrp.addr1, tmpwr, wrp.size1);
  471. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  472. }
  473. }
  474. }
  475. /*
  476. * csio_scsi_write - Create a SCSI WRITE WR.
  477. * @req: IO req structure.
  478. *
  479. * Gets a WR slot in the ingress queue and initializes it with
  480. * SCSI WRITE WR.
  481. *
  482. */
  483. static inline void
  484. csio_scsi_write(struct csio_ioreq *req)
  485. {
  486. struct csio_wr_pair wrp;
  487. uint32_t size;
  488. struct csio_hw *hw = req->lnode->hwp;
  489. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  490. CSIO_SCSI_DATA_WRSZ(req, write, size, scsim->proto_cmd_len);
  491. size = ALIGN(size, 16);
  492. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  493. if (likely(req->drv_status == 0)) {
  494. if (likely(wrp.size1 >= size)) {
  495. /* Initialize WR in one shot */
  496. csio_scsi_init_write_wr(req, wrp.addr1, size);
  497. } else {
  498. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  499. /*
  500. * Make a temporary copy of the WR and write back
  501. * the copy into the WR pair.
  502. */
  503. csio_scsi_init_write_wr(req, (void *)tmpwr, size);
  504. memcpy(wrp.addr1, tmpwr, wrp.size1);
  505. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  506. }
  507. }
  508. }
  509. /*
  510. * csio_setup_ddp - Setup DDP buffers for Read request.
  511. * @req: IO req structure.
  512. *
  513. * Checks SGLs/Data buffers are virtually contiguous required for DDP.
  514. * If contiguous,driver posts SGLs in the WR otherwise post internal
  515. * buffers for such request for DDP.
  516. */
  517. static inline void
  518. csio_setup_ddp(struct csio_scsim *scsim, struct csio_ioreq *req)
  519. {
  520. #ifdef __CSIO_DEBUG__
  521. struct csio_hw *hw = req->lnode->hwp;
  522. #endif
  523. struct scatterlist *sgel = NULL;
  524. struct scsi_cmnd *scmnd = csio_scsi_cmnd(req);
  525. uint64_t sg_addr = 0;
  526. uint32_t ddp_pagesz = 4096;
  527. uint32_t buf_off;
  528. struct csio_dma_buf *dma_buf = NULL;
  529. uint32_t alloc_len = 0;
  530. uint32_t xfer_len = 0;
  531. uint32_t sg_len = 0;
  532. uint32_t i;
  533. scsi_for_each_sg(scmnd, sgel, req->nsge, i) {
  534. sg_addr = sg_dma_address(sgel);
  535. sg_len = sg_dma_len(sgel);
  536. buf_off = sg_addr & (ddp_pagesz - 1);
  537. /* Except 1st buffer,all buffer addr have to be Page aligned */
  538. if (i != 0 && buf_off) {
  539. csio_dbg(hw, "SGL addr not DDP aligned (%llx:%d)\n",
  540. sg_addr, sg_len);
  541. goto unaligned;
  542. }
  543. /* Except last buffer,all buffer must end on page boundary */
  544. if ((i != (req->nsge - 1)) &&
  545. ((buf_off + sg_len) & (ddp_pagesz - 1))) {
  546. csio_dbg(hw,
  547. "SGL addr not ending on page boundary"
  548. "(%llx:%d)\n", sg_addr, sg_len);
  549. goto unaligned;
  550. }
  551. }
  552. /* SGL's are virtually contiguous. HW will DDP to SGLs */
  553. req->dcopy = 0;
  554. csio_scsi_read(req);
  555. return;
  556. unaligned:
  557. CSIO_INC_STATS(scsim, n_unaligned);
  558. /*
  559. * For unaligned SGLs, driver will allocate internal DDP buffer.
  560. * Once command is completed data from DDP buffer copied to SGLs
  561. */
  562. req->dcopy = 1;
  563. /* Use gen_list to store the DDP buffers */
  564. INIT_LIST_HEAD(&req->gen_list);
  565. xfer_len = scsi_bufflen(scmnd);
  566. i = 0;
  567. /* Allocate ddp buffers for this request */
  568. while (alloc_len < xfer_len) {
  569. dma_buf = csio_get_scsi_ddp(scsim);
  570. if (dma_buf == NULL || i > scsim->max_sge) {
  571. req->drv_status = -EBUSY;
  572. break;
  573. }
  574. alloc_len += dma_buf->len;
  575. /* Added to IO req */
  576. list_add_tail(&dma_buf->list, &req->gen_list);
  577. i++;
  578. }
  579. if (!req->drv_status) {
  580. /* set number of ddp bufs used */
  581. req->nsge = i;
  582. csio_scsi_read(req);
  583. return;
  584. }
  585. /* release dma descs */
  586. if (i > 0)
  587. csio_put_scsi_ddp_list(scsim, &req->gen_list, i);
  588. }
  589. /*
  590. * csio_scsi_init_abrt_cls_wr - Initialize an ABORT/CLOSE WR.
  591. * @req: IO req structure.
  592. * @addr: DMA location to place the payload.
  593. * @size: Size of WR
  594. * @abort: abort OR close
  595. *
  596. * Wrapper for populating fw_scsi_cmd_wr.
  597. */
  598. static inline void
  599. csio_scsi_init_abrt_cls_wr(struct csio_ioreq *req, void *addr, uint32_t size,
  600. bool abort)
  601. {
  602. struct csio_hw *hw = req->lnode->hwp;
  603. struct csio_rnode *rn = req->rnode;
  604. struct fw_scsi_abrt_cls_wr *wr = (struct fw_scsi_abrt_cls_wr *)addr;
  605. wr->op_immdlen = cpu_to_be32(FW_WR_OP(FW_SCSI_ABRT_CLS_WR));
  606. wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID(rn->flowid) |
  607. FW_WR_LEN16(
  608. DIV_ROUND_UP(size, 16)));
  609. wr->cookie = (uintptr_t) req;
  610. wr->iqid = cpu_to_be16(csio_q_physiqid(hw, req->iq_idx));
  611. wr->tmo_val = (uint8_t) req->tmo;
  612. /* 0 for CHK_ALL_IO tells FW to look up t_cookie */
  613. wr->sub_opcode_to_chk_all_io =
  614. (FW_SCSI_ABRT_CLS_WR_SUB_OPCODE(abort) |
  615. FW_SCSI_ABRT_CLS_WR_CHK_ALL_IO(0));
  616. wr->r3[0] = 0;
  617. wr->r3[1] = 0;
  618. wr->r3[2] = 0;
  619. wr->r3[3] = 0;
  620. /* Since we re-use the same ioreq for abort as well */
  621. wr->t_cookie = (uintptr_t) req;
  622. }
  623. static inline void
  624. csio_scsi_abrt_cls(struct csio_ioreq *req, bool abort)
  625. {
  626. struct csio_wr_pair wrp;
  627. struct csio_hw *hw = req->lnode->hwp;
  628. uint32_t size = ALIGN(sizeof(struct fw_scsi_abrt_cls_wr), 16);
  629. req->drv_status = csio_wr_get(hw, req->eq_idx, size, &wrp);
  630. if (req->drv_status != 0)
  631. return;
  632. if (wrp.size1 >= size) {
  633. /* Initialize WR in one shot */
  634. csio_scsi_init_abrt_cls_wr(req, wrp.addr1, size, abort);
  635. } else {
  636. uint8_t *tmpwr = csio_q_eq_wrap(hw, req->eq_idx);
  637. /*
  638. * Make a temporary copy of the WR and write back
  639. * the copy into the WR pair.
  640. */
  641. csio_scsi_init_abrt_cls_wr(req, (void *)tmpwr, size, abort);
  642. memcpy(wrp.addr1, tmpwr, wrp.size1);
  643. memcpy(wrp.addr2, tmpwr + wrp.size1, size - wrp.size1);
  644. }
  645. }
  646. /*****************************************************************************/
  647. /* START: SCSI SM */
  648. /*****************************************************************************/
  649. static void
  650. csio_scsis_uninit(struct csio_ioreq *req, enum csio_scsi_ev evt)
  651. {
  652. struct csio_hw *hw = req->lnode->hwp;
  653. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  654. switch (evt) {
  655. case CSIO_SCSIE_START_IO:
  656. if (req->nsge) {
  657. if (req->datadir == DMA_TO_DEVICE) {
  658. req->dcopy = 0;
  659. csio_scsi_write(req);
  660. } else
  661. csio_setup_ddp(scsim, req);
  662. } else {
  663. csio_scsi_cmd(req);
  664. }
  665. if (likely(req->drv_status == 0)) {
  666. /* change state and enqueue on active_q */
  667. csio_set_state(&req->sm, csio_scsis_io_active);
  668. list_add_tail(&req->sm.sm_list, &scsim->active_q);
  669. csio_wr_issue(hw, req->eq_idx, false);
  670. CSIO_INC_STATS(scsim, n_active);
  671. return;
  672. }
  673. break;
  674. case CSIO_SCSIE_START_TM:
  675. csio_scsi_cmd(req);
  676. if (req->drv_status == 0) {
  677. /*
  678. * NOTE: We collect the affected I/Os prior to issuing
  679. * LUN reset, and not after it. This is to prevent
  680. * aborting I/Os that get issued after the LUN reset,
  681. * but prior to LUN reset completion (in the event that
  682. * the host stack has not blocked I/Os to a LUN that is
  683. * being reset.
  684. */
  685. csio_set_state(&req->sm, csio_scsis_tm_active);
  686. list_add_tail(&req->sm.sm_list, &scsim->active_q);
  687. csio_wr_issue(hw, req->eq_idx, false);
  688. CSIO_INC_STATS(scsim, n_tm_active);
  689. }
  690. return;
  691. case CSIO_SCSIE_ABORT:
  692. case CSIO_SCSIE_CLOSE:
  693. /*
  694. * NOTE:
  695. * We could get here due to :
  696. * - a window in the cleanup path of the SCSI module
  697. * (csio_scsi_abort_io()). Please see NOTE in this function.
  698. * - a window in the time we tried to issue an abort/close
  699. * of a request to FW, and the FW completed the request
  700. * itself.
  701. * Print a message for now, and return INVAL either way.
  702. */
  703. req->drv_status = -EINVAL;
  704. csio_warn(hw, "Trying to abort/close completed IO:%p!\n", req);
  705. break;
  706. default:
  707. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  708. CSIO_DB_ASSERT(0);
  709. }
  710. }
  711. static void
  712. csio_scsis_io_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
  713. {
  714. struct csio_hw *hw = req->lnode->hwp;
  715. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  716. struct csio_rnode *rn;
  717. switch (evt) {
  718. case CSIO_SCSIE_COMPLETED:
  719. CSIO_DEC_STATS(scm, n_active);
  720. list_del_init(&req->sm.sm_list);
  721. csio_set_state(&req->sm, csio_scsis_uninit);
  722. /*
  723. * In MSIX mode, with multiple queues, the SCSI compeltions
  724. * could reach us sooner than the FW events sent to indicate
  725. * I-T nexus loss (link down, remote device logo etc). We
  726. * dont want to be returning such I/Os to the upper layer
  727. * immediately, since we wouldnt have reported the I-T nexus
  728. * loss itself. This forces us to serialize such completions
  729. * with the reporting of the I-T nexus loss. Therefore, we
  730. * internally queue up such up such completions in the rnode.
  731. * The reporting of I-T nexus loss to the upper layer is then
  732. * followed by the returning of I/Os in this internal queue.
  733. * Having another state alongwith another queue helps us take
  734. * actions for events such as ABORT received while we are
  735. * in this rnode queue.
  736. */
  737. if (unlikely(req->wr_status != FW_SUCCESS)) {
  738. rn = req->rnode;
  739. /*
  740. * FW says remote device is lost, but rnode
  741. * doesnt reflect it.
  742. */
  743. if (csio_scsi_itnexus_loss_error(req->wr_status) &&
  744. csio_is_rnode_ready(rn)) {
  745. csio_set_state(&req->sm,
  746. csio_scsis_shost_cmpl_await);
  747. list_add_tail(&req->sm.sm_list,
  748. &rn->host_cmpl_q);
  749. }
  750. }
  751. break;
  752. case CSIO_SCSIE_ABORT:
  753. csio_scsi_abrt_cls(req, SCSI_ABORT);
  754. if (req->drv_status == 0) {
  755. csio_wr_issue(hw, req->eq_idx, false);
  756. csio_set_state(&req->sm, csio_scsis_aborting);
  757. }
  758. break;
  759. case CSIO_SCSIE_CLOSE:
  760. csio_scsi_abrt_cls(req, SCSI_CLOSE);
  761. if (req->drv_status == 0) {
  762. csio_wr_issue(hw, req->eq_idx, false);
  763. csio_set_state(&req->sm, csio_scsis_closing);
  764. }
  765. break;
  766. case CSIO_SCSIE_DRVCLEANUP:
  767. req->wr_status = FW_HOSTERROR;
  768. CSIO_DEC_STATS(scm, n_active);
  769. csio_set_state(&req->sm, csio_scsis_uninit);
  770. break;
  771. default:
  772. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  773. CSIO_DB_ASSERT(0);
  774. }
  775. }
  776. static void
  777. csio_scsis_tm_active(struct csio_ioreq *req, enum csio_scsi_ev evt)
  778. {
  779. struct csio_hw *hw = req->lnode->hwp;
  780. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  781. switch (evt) {
  782. case CSIO_SCSIE_COMPLETED:
  783. CSIO_DEC_STATS(scm, n_tm_active);
  784. list_del_init(&req->sm.sm_list);
  785. csio_set_state(&req->sm, csio_scsis_uninit);
  786. break;
  787. case CSIO_SCSIE_ABORT:
  788. csio_scsi_abrt_cls(req, SCSI_ABORT);
  789. if (req->drv_status == 0) {
  790. csio_wr_issue(hw, req->eq_idx, false);
  791. csio_set_state(&req->sm, csio_scsis_aborting);
  792. }
  793. break;
  794. case CSIO_SCSIE_CLOSE:
  795. csio_scsi_abrt_cls(req, SCSI_CLOSE);
  796. if (req->drv_status == 0) {
  797. csio_wr_issue(hw, req->eq_idx, false);
  798. csio_set_state(&req->sm, csio_scsis_closing);
  799. }
  800. break;
  801. case CSIO_SCSIE_DRVCLEANUP:
  802. req->wr_status = FW_HOSTERROR;
  803. CSIO_DEC_STATS(scm, n_tm_active);
  804. csio_set_state(&req->sm, csio_scsis_uninit);
  805. break;
  806. default:
  807. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  808. CSIO_DB_ASSERT(0);
  809. }
  810. }
  811. static void
  812. csio_scsis_aborting(struct csio_ioreq *req, enum csio_scsi_ev evt)
  813. {
  814. struct csio_hw *hw = req->lnode->hwp;
  815. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  816. switch (evt) {
  817. case CSIO_SCSIE_COMPLETED:
  818. csio_dbg(hw,
  819. "ioreq %p recvd cmpltd (wr_status:%d) "
  820. "in aborting st\n", req, req->wr_status);
  821. /*
  822. * Use -ECANCELED to explicitly tell the ABORTED event that
  823. * the original I/O was returned to driver by FW.
  824. * We dont really care if the I/O was returned with success by
  825. * FW (because the ABORT and completion of the I/O crossed each
  826. * other), or any other return value. Once we are in aborting
  827. * state, the success or failure of the I/O is unimportant to
  828. * us.
  829. */
  830. req->drv_status = -ECANCELED;
  831. break;
  832. case CSIO_SCSIE_ABORT:
  833. CSIO_INC_STATS(scm, n_abrt_dups);
  834. break;
  835. case CSIO_SCSIE_ABORTED:
  836. csio_dbg(hw, "abort of %p return status:0x%x drv_status:%x\n",
  837. req, req->wr_status, req->drv_status);
  838. /*
  839. * Check if original I/O WR completed before the Abort
  840. * completion.
  841. */
  842. if (req->drv_status != -ECANCELED) {
  843. csio_warn(hw,
  844. "Abort completed before original I/O,"
  845. " req:%p\n", req);
  846. CSIO_DB_ASSERT(0);
  847. }
  848. /*
  849. * There are the following possible scenarios:
  850. * 1. The abort completed successfully, FW returned FW_SUCCESS.
  851. * 2. The completion of an I/O and the receipt of
  852. * abort for that I/O by the FW crossed each other.
  853. * The FW returned FW_EINVAL. The original I/O would have
  854. * returned with FW_SUCCESS or any other SCSI error.
  855. * 3. The FW couldnt sent the abort out on the wire, as there
  856. * was an I-T nexus loss (link down, remote device logged
  857. * out etc). FW sent back an appropriate IT nexus loss status
  858. * for the abort.
  859. * 4. FW sent an abort, but abort timed out (remote device
  860. * didnt respond). FW replied back with
  861. * FW_SCSI_ABORT_TIMEDOUT.
  862. * 5. FW couldnt genuinely abort the request for some reason,
  863. * and sent us an error.
  864. *
  865. * The first 3 scenarios are treated as succesful abort
  866. * operations by the host, while the last 2 are failed attempts
  867. * to abort. Manipulate the return value of the request
  868. * appropriately, so that host can convey these results
  869. * back to the upper layer.
  870. */
  871. if ((req->wr_status == FW_SUCCESS) ||
  872. (req->wr_status == FW_EINVAL) ||
  873. csio_scsi_itnexus_loss_error(req->wr_status))
  874. req->wr_status = FW_SCSI_ABORT_REQUESTED;
  875. CSIO_DEC_STATS(scm, n_active);
  876. list_del_init(&req->sm.sm_list);
  877. csio_set_state(&req->sm, csio_scsis_uninit);
  878. break;
  879. case CSIO_SCSIE_DRVCLEANUP:
  880. req->wr_status = FW_HOSTERROR;
  881. CSIO_DEC_STATS(scm, n_active);
  882. csio_set_state(&req->sm, csio_scsis_uninit);
  883. break;
  884. case CSIO_SCSIE_CLOSE:
  885. /*
  886. * We can receive this event from the module
  887. * cleanup paths, if the FW forgot to reply to the ABORT WR
  888. * and left this ioreq in this state. For now, just ignore
  889. * the event. The CLOSE event is sent to this state, as
  890. * the LINK may have already gone down.
  891. */
  892. break;
  893. default:
  894. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  895. CSIO_DB_ASSERT(0);
  896. }
  897. }
  898. static void
  899. csio_scsis_closing(struct csio_ioreq *req, enum csio_scsi_ev evt)
  900. {
  901. struct csio_hw *hw = req->lnode->hwp;
  902. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  903. switch (evt) {
  904. case CSIO_SCSIE_COMPLETED:
  905. csio_dbg(hw,
  906. "ioreq %p recvd cmpltd (wr_status:%d) "
  907. "in closing st\n", req, req->wr_status);
  908. /*
  909. * Use -ECANCELED to explicitly tell the CLOSED event that
  910. * the original I/O was returned to driver by FW.
  911. * We dont really care if the I/O was returned with success by
  912. * FW (because the CLOSE and completion of the I/O crossed each
  913. * other), or any other return value. Once we are in aborting
  914. * state, the success or failure of the I/O is unimportant to
  915. * us.
  916. */
  917. req->drv_status = -ECANCELED;
  918. break;
  919. case CSIO_SCSIE_CLOSED:
  920. /*
  921. * Check if original I/O WR completed before the Close
  922. * completion.
  923. */
  924. if (req->drv_status != -ECANCELED) {
  925. csio_fatal(hw,
  926. "Close completed before original I/O,"
  927. " req:%p\n", req);
  928. CSIO_DB_ASSERT(0);
  929. }
  930. /*
  931. * Either close succeeded, or we issued close to FW at the
  932. * same time FW compelted it to us. Either way, the I/O
  933. * is closed.
  934. */
  935. CSIO_DB_ASSERT((req->wr_status == FW_SUCCESS) ||
  936. (req->wr_status == FW_EINVAL));
  937. req->wr_status = FW_SCSI_CLOSE_REQUESTED;
  938. CSIO_DEC_STATS(scm, n_active);
  939. list_del_init(&req->sm.sm_list);
  940. csio_set_state(&req->sm, csio_scsis_uninit);
  941. break;
  942. case CSIO_SCSIE_CLOSE:
  943. break;
  944. case CSIO_SCSIE_DRVCLEANUP:
  945. req->wr_status = FW_HOSTERROR;
  946. CSIO_DEC_STATS(scm, n_active);
  947. csio_set_state(&req->sm, csio_scsis_uninit);
  948. break;
  949. default:
  950. csio_dbg(hw, "Unhandled event:%d sent to req:%p\n", evt, req);
  951. CSIO_DB_ASSERT(0);
  952. }
  953. }
  954. static void
  955. csio_scsis_shost_cmpl_await(struct csio_ioreq *req, enum csio_scsi_ev evt)
  956. {
  957. switch (evt) {
  958. case CSIO_SCSIE_ABORT:
  959. case CSIO_SCSIE_CLOSE:
  960. /*
  961. * Just succeed the abort request, and hope that
  962. * the remote device unregister path will cleanup
  963. * this I/O to the upper layer within a sane
  964. * amount of time.
  965. */
  966. /*
  967. * A close can come in during a LINK DOWN. The FW would have
  968. * returned us the I/O back, but not the remote device lost
  969. * FW event. In this interval, if the I/O times out at the upper
  970. * layer, a close can come in. Take the same action as abort:
  971. * return success, and hope that the remote device unregister
  972. * path will cleanup this I/O. If the FW still doesnt send
  973. * the msg, the close times out, and the upper layer resorts
  974. * to the next level of error recovery.
  975. */
  976. req->drv_status = 0;
  977. break;
  978. case CSIO_SCSIE_DRVCLEANUP:
  979. csio_set_state(&req->sm, csio_scsis_uninit);
  980. break;
  981. default:
  982. csio_dbg(req->lnode->hwp, "Unhandled event:%d sent to req:%p\n",
  983. evt, req);
  984. CSIO_DB_ASSERT(0);
  985. }
  986. }
  987. /*
  988. * csio_scsi_cmpl_handler - WR completion handler for SCSI.
  989. * @hw: HW module.
  990. * @wr: The completed WR from the ingress queue.
  991. * @len: Length of the WR.
  992. * @flb: Freelist buffer array.
  993. * @priv: Private object
  994. * @scsiwr: Pointer to SCSI WR.
  995. *
  996. * This is the WR completion handler called per completion from the
  997. * ISR. It is called with lock held. It walks past the RSS and CPL message
  998. * header where the actual WR is present.
  999. * It then gets the status, WR handle (ioreq pointer) and the len of
  1000. * the WR, based on WR opcode. Only on a non-good status is the entire
  1001. * WR copied into the WR cache (ioreq->fw_wr).
  1002. * The ioreq corresponding to the WR is returned to the caller.
  1003. * NOTE: The SCSI queue doesnt allocate a freelist today, hence
  1004. * no freelist buffer is expected.
  1005. */
  1006. struct csio_ioreq *
  1007. csio_scsi_cmpl_handler(struct csio_hw *hw, void *wr, uint32_t len,
  1008. struct csio_fl_dma_buf *flb, void *priv, uint8_t **scsiwr)
  1009. {
  1010. struct csio_ioreq *ioreq = NULL;
  1011. struct cpl_fw6_msg *cpl;
  1012. uint8_t *tempwr;
  1013. uint8_t status;
  1014. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  1015. /* skip RSS header */
  1016. cpl = (struct cpl_fw6_msg *)((uintptr_t)wr + sizeof(__be64));
  1017. if (unlikely(cpl->opcode != CPL_FW6_MSG)) {
  1018. csio_warn(hw, "Error: Invalid CPL msg %x recvd on SCSI q\n",
  1019. cpl->opcode);
  1020. CSIO_INC_STATS(scm, n_inval_cplop);
  1021. return NULL;
  1022. }
  1023. tempwr = (uint8_t *)(cpl->data);
  1024. status = csio_wr_status(tempwr);
  1025. *scsiwr = tempwr;
  1026. if (likely((*tempwr == FW_SCSI_READ_WR) ||
  1027. (*tempwr == FW_SCSI_WRITE_WR) ||
  1028. (*tempwr == FW_SCSI_CMD_WR))) {
  1029. ioreq = (struct csio_ioreq *)((uintptr_t)
  1030. (((struct fw_scsi_read_wr *)tempwr)->cookie));
  1031. CSIO_DB_ASSERT(virt_addr_valid(ioreq));
  1032. ioreq->wr_status = status;
  1033. return ioreq;
  1034. }
  1035. if (*tempwr == FW_SCSI_ABRT_CLS_WR) {
  1036. ioreq = (struct csio_ioreq *)((uintptr_t)
  1037. (((struct fw_scsi_abrt_cls_wr *)tempwr)->cookie));
  1038. CSIO_DB_ASSERT(virt_addr_valid(ioreq));
  1039. ioreq->wr_status = status;
  1040. return ioreq;
  1041. }
  1042. csio_warn(hw, "WR with invalid opcode in SCSI IQ: %x\n", *tempwr);
  1043. CSIO_INC_STATS(scm, n_inval_scsiop);
  1044. return NULL;
  1045. }
  1046. /*
  1047. * csio_scsi_cleanup_io_q - Cleanup the given queue.
  1048. * @scm: SCSI module.
  1049. * @q: Queue to be cleaned up.
  1050. *
  1051. * Called with lock held. Has to exit with lock held.
  1052. */
  1053. void
  1054. csio_scsi_cleanup_io_q(struct csio_scsim *scm, struct list_head *q)
  1055. {
  1056. struct csio_hw *hw = scm->hw;
  1057. struct csio_ioreq *ioreq;
  1058. struct list_head *tmp, *next;
  1059. struct scsi_cmnd *scmnd;
  1060. /* Call back the completion routines of the active_q */
  1061. list_for_each_safe(tmp, next, q) {
  1062. ioreq = (struct csio_ioreq *)tmp;
  1063. csio_scsi_drvcleanup(ioreq);
  1064. list_del_init(&ioreq->sm.sm_list);
  1065. scmnd = csio_scsi_cmnd(ioreq);
  1066. spin_unlock_irq(&hw->lock);
  1067. /*
  1068. * Upper layers may have cleared this command, hence this
  1069. * check to avoid accessing stale references.
  1070. */
  1071. if (scmnd != NULL)
  1072. ioreq->io_cbfn(hw, ioreq);
  1073. spin_lock_irq(&scm->freelist_lock);
  1074. csio_put_scsi_ioreq(scm, ioreq);
  1075. spin_unlock_irq(&scm->freelist_lock);
  1076. spin_lock_irq(&hw->lock);
  1077. }
  1078. }
  1079. #define CSIO_SCSI_ABORT_Q_POLL_MS 2000
  1080. static void
  1081. csio_abrt_cls(struct csio_ioreq *ioreq, struct scsi_cmnd *scmnd)
  1082. {
  1083. struct csio_lnode *ln = ioreq->lnode;
  1084. struct csio_hw *hw = ln->hwp;
  1085. int ready = 0;
  1086. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1087. int rv;
  1088. if (csio_scsi_cmnd(ioreq) != scmnd) {
  1089. CSIO_INC_STATS(scsim, n_abrt_race_comp);
  1090. return;
  1091. }
  1092. ready = csio_is_lnode_ready(ln);
  1093. rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
  1094. if (rv != 0) {
  1095. if (ready)
  1096. CSIO_INC_STATS(scsim, n_abrt_busy_error);
  1097. else
  1098. CSIO_INC_STATS(scsim, n_cls_busy_error);
  1099. }
  1100. }
  1101. /*
  1102. * csio_scsi_abort_io_q - Abort all I/Os on given queue
  1103. * @scm: SCSI module.
  1104. * @q: Queue to abort.
  1105. * @tmo: Timeout in ms
  1106. *
  1107. * Attempt to abort all I/Os on given queue, and wait for a max
  1108. * of tmo milliseconds for them to complete. Returns success
  1109. * if all I/Os are aborted. Else returns -ETIMEDOUT.
  1110. * Should be entered with lock held. Exits with lock held.
  1111. * NOTE:
  1112. * Lock has to be held across the loop that aborts I/Os, since dropping the lock
  1113. * in between can cause the list to be corrupted. As a result, the caller
  1114. * of this function has to ensure that the number of I/os to be aborted
  1115. * is finite enough to not cause lock-held-for-too-long issues.
  1116. */
  1117. static int
  1118. csio_scsi_abort_io_q(struct csio_scsim *scm, struct list_head *q, uint32_t tmo)
  1119. {
  1120. struct csio_hw *hw = scm->hw;
  1121. struct list_head *tmp, *next;
  1122. int count = DIV_ROUND_UP(tmo, CSIO_SCSI_ABORT_Q_POLL_MS);
  1123. struct scsi_cmnd *scmnd;
  1124. if (list_empty(q))
  1125. return 0;
  1126. csio_dbg(hw, "Aborting SCSI I/Os\n");
  1127. /* Now abort/close I/Os in the queue passed */
  1128. list_for_each_safe(tmp, next, q) {
  1129. scmnd = csio_scsi_cmnd((struct csio_ioreq *)tmp);
  1130. csio_abrt_cls((struct csio_ioreq *)tmp, scmnd);
  1131. }
  1132. /* Wait till all active I/Os are completed/aborted/closed */
  1133. while (!list_empty(q) && count--) {
  1134. spin_unlock_irq(&hw->lock);
  1135. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1136. spin_lock_irq(&hw->lock);
  1137. }
  1138. /* all aborts completed */
  1139. if (list_empty(q))
  1140. return 0;
  1141. return -ETIMEDOUT;
  1142. }
  1143. /*
  1144. * csio_scsim_cleanup_io - Cleanup all I/Os in SCSI module.
  1145. * @scm: SCSI module.
  1146. * @abort: abort required.
  1147. * Called with lock held, should exit with lock held.
  1148. * Can sleep when waiting for I/Os to complete.
  1149. */
  1150. int
  1151. csio_scsim_cleanup_io(struct csio_scsim *scm, bool abort)
  1152. {
  1153. struct csio_hw *hw = scm->hw;
  1154. int rv = 0;
  1155. int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
  1156. /* No I/Os pending */
  1157. if (list_empty(&scm->active_q))
  1158. return 0;
  1159. /* Wait until all active I/Os are completed */
  1160. while (!list_empty(&scm->active_q) && count--) {
  1161. spin_unlock_irq(&hw->lock);
  1162. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1163. spin_lock_irq(&hw->lock);
  1164. }
  1165. /* all I/Os completed */
  1166. if (list_empty(&scm->active_q))
  1167. return 0;
  1168. /* Else abort */
  1169. if (abort) {
  1170. rv = csio_scsi_abort_io_q(scm, &scm->active_q, 30000);
  1171. if (rv == 0)
  1172. return rv;
  1173. csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
  1174. }
  1175. csio_scsi_cleanup_io_q(scm, &scm->active_q);
  1176. CSIO_DB_ASSERT(list_empty(&scm->active_q));
  1177. return rv;
  1178. }
  1179. /*
  1180. * csio_scsim_cleanup_io_lnode - Cleanup all I/Os of given lnode.
  1181. * @scm: SCSI module.
  1182. * @lnode: lnode
  1183. *
  1184. * Called with lock held, should exit with lock held.
  1185. * Can sleep (with dropped lock) when waiting for I/Os to complete.
  1186. */
  1187. int
  1188. csio_scsim_cleanup_io_lnode(struct csio_scsim *scm, struct csio_lnode *ln)
  1189. {
  1190. struct csio_hw *hw = scm->hw;
  1191. struct csio_scsi_level_data sld;
  1192. int rv;
  1193. int count = DIV_ROUND_UP(60 * 1000, CSIO_SCSI_ABORT_Q_POLL_MS);
  1194. csio_dbg(hw, "Gathering all SCSI I/Os on lnode %p\n", ln);
  1195. sld.level = CSIO_LEV_LNODE;
  1196. sld.lnode = ln;
  1197. INIT_LIST_HEAD(&ln->cmpl_q);
  1198. csio_scsi_gather_active_ios(scm, &sld, &ln->cmpl_q);
  1199. /* No I/Os pending on this lnode */
  1200. if (list_empty(&ln->cmpl_q))
  1201. return 0;
  1202. /* Wait until all active I/Os on this lnode are completed */
  1203. while (!list_empty(&ln->cmpl_q) && count--) {
  1204. spin_unlock_irq(&hw->lock);
  1205. msleep(CSIO_SCSI_ABORT_Q_POLL_MS);
  1206. spin_lock_irq(&hw->lock);
  1207. }
  1208. /* all I/Os completed */
  1209. if (list_empty(&ln->cmpl_q))
  1210. return 0;
  1211. csio_dbg(hw, "Some I/Os pending on ln:%p, aborting them..\n", ln);
  1212. /* I/Os are pending, abort them */
  1213. rv = csio_scsi_abort_io_q(scm, &ln->cmpl_q, 30000);
  1214. if (rv != 0) {
  1215. csio_dbg(hw, "Some I/O aborts timed out, cleaning up..\n");
  1216. csio_scsi_cleanup_io_q(scm, &ln->cmpl_q);
  1217. }
  1218. CSIO_DB_ASSERT(list_empty(&ln->cmpl_q));
  1219. return rv;
  1220. }
  1221. static ssize_t
  1222. csio_show_hw_state(struct device *dev,
  1223. struct device_attribute *attr, char *buf)
  1224. {
  1225. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1226. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1227. if (csio_is_hw_ready(hw))
  1228. return snprintf(buf, PAGE_SIZE, "ready\n");
  1229. else
  1230. return snprintf(buf, PAGE_SIZE, "not ready\n");
  1231. }
  1232. /* Device reset */
  1233. static ssize_t
  1234. csio_device_reset(struct device *dev,
  1235. struct device_attribute *attr, const char *buf, size_t count)
  1236. {
  1237. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1238. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1239. if (*buf != '1')
  1240. return -EINVAL;
  1241. /* Delete NPIV lnodes */
  1242. csio_lnodes_exit(hw, 1);
  1243. /* Block upper IOs */
  1244. csio_lnodes_block_request(hw);
  1245. spin_lock_irq(&hw->lock);
  1246. csio_hw_reset(hw);
  1247. spin_unlock_irq(&hw->lock);
  1248. /* Unblock upper IOs */
  1249. csio_lnodes_unblock_request(hw);
  1250. return count;
  1251. }
  1252. /* disable port */
  1253. static ssize_t
  1254. csio_disable_port(struct device *dev,
  1255. struct device_attribute *attr, const char *buf, size_t count)
  1256. {
  1257. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1258. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1259. bool disable;
  1260. if (*buf == '1' || *buf == '0')
  1261. disable = (*buf == '1') ? true : false;
  1262. else
  1263. return -EINVAL;
  1264. /* Block upper IOs */
  1265. csio_lnodes_block_by_port(hw, ln->portid);
  1266. spin_lock_irq(&hw->lock);
  1267. csio_disable_lnodes(hw, ln->portid, disable);
  1268. spin_unlock_irq(&hw->lock);
  1269. /* Unblock upper IOs */
  1270. csio_lnodes_unblock_by_port(hw, ln->portid);
  1271. return count;
  1272. }
  1273. /* Show debug level */
  1274. static ssize_t
  1275. csio_show_dbg_level(struct device *dev,
  1276. struct device_attribute *attr, char *buf)
  1277. {
  1278. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1279. return snprintf(buf, PAGE_SIZE, "%x\n", ln->params.log_level);
  1280. }
  1281. /* Store debug level */
  1282. static ssize_t
  1283. csio_store_dbg_level(struct device *dev,
  1284. struct device_attribute *attr, const char *buf, size_t count)
  1285. {
  1286. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1287. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1288. uint32_t dbg_level = 0;
  1289. if (!isdigit(buf[0]))
  1290. return -EINVAL;
  1291. if (sscanf(buf, "%i", &dbg_level))
  1292. return -EINVAL;
  1293. ln->params.log_level = dbg_level;
  1294. hw->params.log_level = dbg_level;
  1295. return 0;
  1296. }
  1297. static DEVICE_ATTR(hw_state, S_IRUGO, csio_show_hw_state, NULL);
  1298. static DEVICE_ATTR(device_reset, S_IRUGO | S_IWUSR, NULL, csio_device_reset);
  1299. static DEVICE_ATTR(disable_port, S_IRUGO | S_IWUSR, NULL, csio_disable_port);
  1300. static DEVICE_ATTR(dbg_level, S_IRUGO | S_IWUSR, csio_show_dbg_level,
  1301. csio_store_dbg_level);
  1302. static struct device_attribute *csio_fcoe_lport_attrs[] = {
  1303. &dev_attr_hw_state,
  1304. &dev_attr_device_reset,
  1305. &dev_attr_disable_port,
  1306. &dev_attr_dbg_level,
  1307. NULL,
  1308. };
  1309. static ssize_t
  1310. csio_show_num_reg_rnodes(struct device *dev,
  1311. struct device_attribute *attr, char *buf)
  1312. {
  1313. struct csio_lnode *ln = shost_priv(class_to_shost(dev));
  1314. return snprintf(buf, PAGE_SIZE, "%d\n", ln->num_reg_rnodes);
  1315. }
  1316. static DEVICE_ATTR(num_reg_rnodes, S_IRUGO, csio_show_num_reg_rnodes, NULL);
  1317. static struct device_attribute *csio_fcoe_vport_attrs[] = {
  1318. &dev_attr_num_reg_rnodes,
  1319. &dev_attr_dbg_level,
  1320. NULL,
  1321. };
  1322. static inline uint32_t
  1323. csio_scsi_copy_to_sgl(struct csio_hw *hw, struct csio_ioreq *req)
  1324. {
  1325. struct scsi_cmnd *scmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1326. struct scatterlist *sg;
  1327. uint32_t bytes_left;
  1328. uint32_t bytes_copy;
  1329. uint32_t buf_off = 0;
  1330. uint32_t start_off = 0;
  1331. uint32_t sg_off = 0;
  1332. void *sg_addr;
  1333. void *buf_addr;
  1334. struct csio_dma_buf *dma_buf;
  1335. bytes_left = scsi_bufflen(scmnd);
  1336. sg = scsi_sglist(scmnd);
  1337. dma_buf = (struct csio_dma_buf *)csio_list_next(&req->gen_list);
  1338. /* Copy data from driver buffer to SGs of SCSI CMD */
  1339. while (bytes_left > 0 && sg && dma_buf) {
  1340. if (buf_off >= dma_buf->len) {
  1341. buf_off = 0;
  1342. dma_buf = (struct csio_dma_buf *)
  1343. csio_list_next(dma_buf);
  1344. continue;
  1345. }
  1346. if (start_off >= sg->length) {
  1347. start_off -= sg->length;
  1348. sg = sg_next(sg);
  1349. continue;
  1350. }
  1351. buf_addr = dma_buf->vaddr + buf_off;
  1352. sg_off = sg->offset + start_off;
  1353. bytes_copy = min((dma_buf->len - buf_off),
  1354. sg->length - start_off);
  1355. bytes_copy = min((uint32_t)(PAGE_SIZE - (sg_off & ~PAGE_MASK)),
  1356. bytes_copy);
  1357. sg_addr = kmap_atomic(sg_page(sg) + (sg_off >> PAGE_SHIFT));
  1358. if (!sg_addr) {
  1359. csio_err(hw, "failed to kmap sg:%p of ioreq:%p\n",
  1360. sg, req);
  1361. break;
  1362. }
  1363. csio_dbg(hw, "copy_to_sgl:sg_addr %p sg_off %d buf %p len %d\n",
  1364. sg_addr, sg_off, buf_addr, bytes_copy);
  1365. memcpy(sg_addr + (sg_off & ~PAGE_MASK), buf_addr, bytes_copy);
  1366. kunmap_atomic(sg_addr);
  1367. start_off += bytes_copy;
  1368. buf_off += bytes_copy;
  1369. bytes_left -= bytes_copy;
  1370. }
  1371. if (bytes_left > 0)
  1372. return DID_ERROR;
  1373. else
  1374. return DID_OK;
  1375. }
  1376. /*
  1377. * csio_scsi_err_handler - SCSI error handler.
  1378. * @hw: HW module.
  1379. * @req: IO request.
  1380. *
  1381. */
  1382. static inline void
  1383. csio_scsi_err_handler(struct csio_hw *hw, struct csio_ioreq *req)
  1384. {
  1385. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1386. struct csio_scsim *scm = csio_hw_to_scsim(hw);
  1387. struct fcp_resp_with_ext *fcp_resp;
  1388. struct fcp_resp_rsp_info *rsp_info;
  1389. struct csio_dma_buf *dma_buf;
  1390. uint8_t flags, scsi_status = 0;
  1391. uint32_t host_status = DID_OK;
  1392. uint32_t rsp_len = 0, sns_len = 0;
  1393. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1394. switch (req->wr_status) {
  1395. case FW_HOSTERROR:
  1396. if (unlikely(!csio_is_hw_ready(hw)))
  1397. return;
  1398. host_status = DID_ERROR;
  1399. CSIO_INC_STATS(scm, n_hosterror);
  1400. break;
  1401. case FW_SCSI_RSP_ERR:
  1402. dma_buf = &req->dma_buf;
  1403. fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
  1404. rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
  1405. flags = fcp_resp->resp.fr_flags;
  1406. scsi_status = fcp_resp->resp.fr_status;
  1407. if (flags & FCP_RSP_LEN_VAL) {
  1408. rsp_len = be32_to_cpu(fcp_resp->ext.fr_rsp_len);
  1409. if ((rsp_len != 0 && rsp_len != 4 && rsp_len != 8) ||
  1410. (rsp_info->rsp_code != FCP_TMF_CMPL)) {
  1411. host_status = DID_ERROR;
  1412. goto out;
  1413. }
  1414. }
  1415. if ((flags & FCP_SNS_LEN_VAL) && fcp_resp->ext.fr_sns_len) {
  1416. sns_len = be32_to_cpu(fcp_resp->ext.fr_sns_len);
  1417. if (sns_len > SCSI_SENSE_BUFFERSIZE)
  1418. sns_len = SCSI_SENSE_BUFFERSIZE;
  1419. memcpy(cmnd->sense_buffer,
  1420. &rsp_info->_fr_resvd[0] + rsp_len, sns_len);
  1421. CSIO_INC_STATS(scm, n_autosense);
  1422. }
  1423. scsi_set_resid(cmnd, 0);
  1424. /* Under run */
  1425. if (flags & FCP_RESID_UNDER) {
  1426. scsi_set_resid(cmnd,
  1427. be32_to_cpu(fcp_resp->ext.fr_resid));
  1428. if (!(flags & FCP_SNS_LEN_VAL) &&
  1429. (scsi_status == SAM_STAT_GOOD) &&
  1430. ((scsi_bufflen(cmnd) - scsi_get_resid(cmnd))
  1431. < cmnd->underflow))
  1432. host_status = DID_ERROR;
  1433. } else if (flags & FCP_RESID_OVER)
  1434. host_status = DID_ERROR;
  1435. CSIO_INC_STATS(scm, n_rsperror);
  1436. break;
  1437. case FW_SCSI_OVER_FLOW_ERR:
  1438. csio_warn(hw,
  1439. "Over-flow error,cmnd:0x%x expected len:0x%x"
  1440. " resid:0x%x\n", cmnd->cmnd[0],
  1441. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  1442. host_status = DID_ERROR;
  1443. CSIO_INC_STATS(scm, n_ovflerror);
  1444. break;
  1445. case FW_SCSI_UNDER_FLOW_ERR:
  1446. csio_warn(hw,
  1447. "Under-flow error,cmnd:0x%x expected"
  1448. " len:0x%x resid:0x%x lun:0x%x ssn:0x%x\n",
  1449. cmnd->cmnd[0], scsi_bufflen(cmnd),
  1450. scsi_get_resid(cmnd), cmnd->device->lun,
  1451. rn->flowid);
  1452. host_status = DID_ERROR;
  1453. CSIO_INC_STATS(scm, n_unflerror);
  1454. break;
  1455. case FW_SCSI_ABORT_REQUESTED:
  1456. case FW_SCSI_ABORTED:
  1457. case FW_SCSI_CLOSE_REQUESTED:
  1458. csio_dbg(hw, "Req %p cmd:%p op:%x %s\n", req, cmnd,
  1459. cmnd->cmnd[0],
  1460. (req->wr_status == FW_SCSI_CLOSE_REQUESTED) ?
  1461. "closed" : "aborted");
  1462. /*
  1463. * csio_eh_abort_handler checks this value to
  1464. * succeed or fail the abort request.
  1465. */
  1466. host_status = DID_REQUEUE;
  1467. if (req->wr_status == FW_SCSI_CLOSE_REQUESTED)
  1468. CSIO_INC_STATS(scm, n_closed);
  1469. else
  1470. CSIO_INC_STATS(scm, n_aborted);
  1471. break;
  1472. case FW_SCSI_ABORT_TIMEDOUT:
  1473. /* FW timed out the abort itself */
  1474. csio_dbg(hw, "FW timed out abort req:%p cmnd:%p status:%x\n",
  1475. req, cmnd, req->wr_status);
  1476. host_status = DID_ERROR;
  1477. CSIO_INC_STATS(scm, n_abrt_timedout);
  1478. break;
  1479. case FW_RDEV_NOT_READY:
  1480. /*
  1481. * In firmware, a RDEV can get into this state
  1482. * temporarily, before moving into dissapeared/lost
  1483. * state. So, the driver should complete the request equivalent
  1484. * to device-disappeared!
  1485. */
  1486. CSIO_INC_STATS(scm, n_rdev_nr_error);
  1487. host_status = DID_ERROR;
  1488. break;
  1489. case FW_ERR_RDEV_LOST:
  1490. CSIO_INC_STATS(scm, n_rdev_lost_error);
  1491. host_status = DID_ERROR;
  1492. break;
  1493. case FW_ERR_RDEV_LOGO:
  1494. CSIO_INC_STATS(scm, n_rdev_logo_error);
  1495. host_status = DID_ERROR;
  1496. break;
  1497. case FW_ERR_RDEV_IMPL_LOGO:
  1498. host_status = DID_ERROR;
  1499. break;
  1500. case FW_ERR_LINK_DOWN:
  1501. CSIO_INC_STATS(scm, n_link_down_error);
  1502. host_status = DID_ERROR;
  1503. break;
  1504. case FW_FCOE_NO_XCHG:
  1505. CSIO_INC_STATS(scm, n_no_xchg_error);
  1506. host_status = DID_ERROR;
  1507. break;
  1508. default:
  1509. csio_err(hw, "Unknown SCSI FW WR status:%d req:%p cmnd:%p\n",
  1510. req->wr_status, req, cmnd);
  1511. CSIO_DB_ASSERT(0);
  1512. CSIO_INC_STATS(scm, n_unknown_error);
  1513. host_status = DID_ERROR;
  1514. break;
  1515. }
  1516. out:
  1517. if (req->nsge > 0)
  1518. scsi_dma_unmap(cmnd);
  1519. cmnd->result = (((host_status) << 16) | scsi_status);
  1520. cmnd->scsi_done(cmnd);
  1521. /* Wake up waiting threads */
  1522. csio_scsi_cmnd(req) = NULL;
  1523. complete_all(&req->cmplobj);
  1524. }
  1525. /*
  1526. * csio_scsi_cbfn - SCSI callback function.
  1527. * @hw: HW module.
  1528. * @req: IO request.
  1529. *
  1530. */
  1531. static void
  1532. csio_scsi_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
  1533. {
  1534. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1535. uint8_t scsi_status = SAM_STAT_GOOD;
  1536. uint32_t host_status = DID_OK;
  1537. if (likely(req->wr_status == FW_SUCCESS)) {
  1538. if (req->nsge > 0) {
  1539. scsi_dma_unmap(cmnd);
  1540. if (req->dcopy)
  1541. host_status = csio_scsi_copy_to_sgl(hw, req);
  1542. }
  1543. cmnd->result = (((host_status) << 16) | scsi_status);
  1544. cmnd->scsi_done(cmnd);
  1545. csio_scsi_cmnd(req) = NULL;
  1546. CSIO_INC_STATS(csio_hw_to_scsim(hw), n_tot_success);
  1547. } else {
  1548. /* Error handling */
  1549. csio_scsi_err_handler(hw, req);
  1550. }
  1551. }
  1552. /**
  1553. * csio_queuecommand - Entry point to kickstart an I/O request.
  1554. * @host: The scsi_host pointer.
  1555. * @cmnd: The I/O request from ML.
  1556. *
  1557. * This routine does the following:
  1558. * - Checks for HW and Rnode module readiness.
  1559. * - Gets a free ioreq structure (which is already initialized
  1560. * to uninit during its allocation).
  1561. * - Maps SG elements.
  1562. * - Initializes ioreq members.
  1563. * - Kicks off the SCSI state machine for this IO.
  1564. * - Returns busy status on error.
  1565. */
  1566. static int
  1567. csio_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmnd)
  1568. {
  1569. struct csio_lnode *ln = shost_priv(host);
  1570. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1571. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1572. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1573. struct csio_ioreq *ioreq = NULL;
  1574. unsigned long flags;
  1575. int nsge = 0;
  1576. int rv = SCSI_MLQUEUE_HOST_BUSY, nr;
  1577. int retval;
  1578. int cpu;
  1579. struct csio_scsi_qset *sqset;
  1580. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1581. if (!blk_rq_cpu_valid(cmnd->request))
  1582. cpu = smp_processor_id();
  1583. else
  1584. cpu = cmnd->request->cpu;
  1585. sqset = &hw->sqset[ln->portid][cpu];
  1586. nr = fc_remote_port_chkready(rport);
  1587. if (nr) {
  1588. cmnd->result = nr;
  1589. CSIO_INC_STATS(scsim, n_rn_nr_error);
  1590. goto err_done;
  1591. }
  1592. if (unlikely(!csio_is_hw_ready(hw))) {
  1593. cmnd->result = (DID_REQUEUE << 16);
  1594. CSIO_INC_STATS(scsim, n_hw_nr_error);
  1595. goto err_done;
  1596. }
  1597. /* Get req->nsge, if there are SG elements to be mapped */
  1598. nsge = scsi_dma_map(cmnd);
  1599. if (unlikely(nsge < 0)) {
  1600. CSIO_INC_STATS(scsim, n_dmamap_error);
  1601. goto err;
  1602. }
  1603. /* Do we support so many mappings? */
  1604. if (unlikely(nsge > scsim->max_sge)) {
  1605. csio_warn(hw,
  1606. "More SGEs than can be supported."
  1607. " SGEs: %d, Max SGEs: %d\n", nsge, scsim->max_sge);
  1608. CSIO_INC_STATS(scsim, n_unsupp_sge_error);
  1609. goto err_dma_unmap;
  1610. }
  1611. /* Get a free ioreq structure - SM is already set to uninit */
  1612. ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
  1613. if (!ioreq) {
  1614. csio_err(hw, "Out of I/O request elements. Active #:%d\n",
  1615. scsim->stats.n_active);
  1616. CSIO_INC_STATS(scsim, n_no_req_error);
  1617. goto err_dma_unmap;
  1618. }
  1619. ioreq->nsge = nsge;
  1620. ioreq->lnode = ln;
  1621. ioreq->rnode = rn;
  1622. ioreq->iq_idx = sqset->iq_idx;
  1623. ioreq->eq_idx = sqset->eq_idx;
  1624. ioreq->wr_status = 0;
  1625. ioreq->drv_status = 0;
  1626. csio_scsi_cmnd(ioreq) = (void *)cmnd;
  1627. ioreq->tmo = 0;
  1628. ioreq->datadir = cmnd->sc_data_direction;
  1629. if (cmnd->sc_data_direction == DMA_TO_DEVICE) {
  1630. CSIO_INC_STATS(ln, n_output_requests);
  1631. ln->stats.n_output_bytes += scsi_bufflen(cmnd);
  1632. } else if (cmnd->sc_data_direction == DMA_FROM_DEVICE) {
  1633. CSIO_INC_STATS(ln, n_input_requests);
  1634. ln->stats.n_input_bytes += scsi_bufflen(cmnd);
  1635. } else
  1636. CSIO_INC_STATS(ln, n_control_requests);
  1637. /* Set cbfn */
  1638. ioreq->io_cbfn = csio_scsi_cbfn;
  1639. /* Needed during abort */
  1640. cmnd->host_scribble = (unsigned char *)ioreq;
  1641. cmnd->SCp.Message = 0;
  1642. /* Kick off SCSI IO SM on the ioreq */
  1643. spin_lock_irqsave(&hw->lock, flags);
  1644. retval = csio_scsi_start_io(ioreq);
  1645. spin_unlock_irqrestore(&hw->lock, flags);
  1646. if (retval != 0) {
  1647. csio_err(hw, "ioreq: %p couldnt be started, status:%d\n",
  1648. ioreq, retval);
  1649. CSIO_INC_STATS(scsim, n_busy_error);
  1650. goto err_put_req;
  1651. }
  1652. return 0;
  1653. err_put_req:
  1654. csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
  1655. err_dma_unmap:
  1656. if (nsge > 0)
  1657. scsi_dma_unmap(cmnd);
  1658. err:
  1659. return rv;
  1660. err_done:
  1661. cmnd->scsi_done(cmnd);
  1662. return 0;
  1663. }
  1664. static int
  1665. csio_do_abrt_cls(struct csio_hw *hw, struct csio_ioreq *ioreq, bool abort)
  1666. {
  1667. int rv;
  1668. int cpu = smp_processor_id();
  1669. struct csio_lnode *ln = ioreq->lnode;
  1670. struct csio_scsi_qset *sqset = &hw->sqset[ln->portid][cpu];
  1671. ioreq->tmo = CSIO_SCSI_ABRT_TMO_MS;
  1672. /*
  1673. * Use current processor queue for posting the abort/close, but retain
  1674. * the ingress queue ID of the original I/O being aborted/closed - we
  1675. * need the abort/close completion to be received on the same queue
  1676. * as the original I/O.
  1677. */
  1678. ioreq->eq_idx = sqset->eq_idx;
  1679. if (abort == SCSI_ABORT)
  1680. rv = csio_scsi_abort(ioreq);
  1681. else
  1682. rv = csio_scsi_close(ioreq);
  1683. return rv;
  1684. }
  1685. static int
  1686. csio_eh_abort_handler(struct scsi_cmnd *cmnd)
  1687. {
  1688. struct csio_ioreq *ioreq;
  1689. struct csio_lnode *ln = shost_priv(cmnd->device->host);
  1690. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1691. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1692. int ready = 0, ret;
  1693. unsigned long tmo = 0;
  1694. int rv;
  1695. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1696. ret = fc_block_scsi_eh(cmnd);
  1697. if (ret)
  1698. return ret;
  1699. ioreq = (struct csio_ioreq *)cmnd->host_scribble;
  1700. if (!ioreq)
  1701. return SUCCESS;
  1702. if (!rn)
  1703. return FAILED;
  1704. csio_dbg(hw,
  1705. "Request to abort ioreq:%p cmd:%p cdb:%08llx"
  1706. " ssni:0x%x lun:%d iq:0x%x\n",
  1707. ioreq, cmnd, *((uint64_t *)cmnd->cmnd), rn->flowid,
  1708. cmnd->device->lun, csio_q_physiqid(hw, ioreq->iq_idx));
  1709. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) != cmnd) {
  1710. CSIO_INC_STATS(scsim, n_abrt_race_comp);
  1711. return SUCCESS;
  1712. }
  1713. ready = csio_is_lnode_ready(ln);
  1714. tmo = CSIO_SCSI_ABRT_TMO_MS;
  1715. spin_lock_irq(&hw->lock);
  1716. rv = csio_do_abrt_cls(hw, ioreq, (ready ? SCSI_ABORT : SCSI_CLOSE));
  1717. spin_unlock_irq(&hw->lock);
  1718. if (rv != 0) {
  1719. if (rv == -EINVAL) {
  1720. /* Return success, if abort/close request issued on
  1721. * already completed IO
  1722. */
  1723. return SUCCESS;
  1724. }
  1725. if (ready)
  1726. CSIO_INC_STATS(scsim, n_abrt_busy_error);
  1727. else
  1728. CSIO_INC_STATS(scsim, n_cls_busy_error);
  1729. goto inval_scmnd;
  1730. }
  1731. /* Wait for completion */
  1732. init_completion(&ioreq->cmplobj);
  1733. wait_for_completion_timeout(&ioreq->cmplobj, msecs_to_jiffies(tmo));
  1734. /* FW didnt respond to abort within our timeout */
  1735. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
  1736. csio_err(hw, "Abort timed out -- req: %p\n", ioreq);
  1737. CSIO_INC_STATS(scsim, n_abrt_timedout);
  1738. inval_scmnd:
  1739. if (ioreq->nsge > 0)
  1740. scsi_dma_unmap(cmnd);
  1741. spin_lock_irq(&hw->lock);
  1742. csio_scsi_cmnd(ioreq) = NULL;
  1743. spin_unlock_irq(&hw->lock);
  1744. cmnd->result = (DID_ERROR << 16);
  1745. cmnd->scsi_done(cmnd);
  1746. return FAILED;
  1747. }
  1748. /* FW successfully aborted the request */
  1749. if (host_byte(cmnd->result) == DID_REQUEUE) {
  1750. csio_info(hw,
  1751. "Aborted SCSI command to (%d:%d) serial#:0x%lx\n",
  1752. cmnd->device->id, cmnd->device->lun,
  1753. cmnd->serial_number);
  1754. return SUCCESS;
  1755. } else {
  1756. csio_info(hw,
  1757. "Failed to abort SCSI command, (%d:%d) serial#:0x%lx\n",
  1758. cmnd->device->id, cmnd->device->lun,
  1759. cmnd->serial_number);
  1760. return FAILED;
  1761. }
  1762. }
  1763. /*
  1764. * csio_tm_cbfn - TM callback function.
  1765. * @hw: HW module.
  1766. * @req: IO request.
  1767. *
  1768. * Cache the result in 'cmnd', since ioreq will be freed soon
  1769. * after we return from here, and the waiting thread shouldnt trust
  1770. * the ioreq contents.
  1771. */
  1772. static void
  1773. csio_tm_cbfn(struct csio_hw *hw, struct csio_ioreq *req)
  1774. {
  1775. struct scsi_cmnd *cmnd = (struct scsi_cmnd *)csio_scsi_cmnd(req);
  1776. struct csio_dma_buf *dma_buf;
  1777. uint8_t flags = 0;
  1778. struct fcp_resp_with_ext *fcp_resp;
  1779. struct fcp_resp_rsp_info *rsp_info;
  1780. csio_dbg(hw, "req: %p in csio_tm_cbfn status: %d\n",
  1781. req, req->wr_status);
  1782. /* Cache FW return status */
  1783. cmnd->SCp.Status = req->wr_status;
  1784. /* Special handling based on FCP response */
  1785. /*
  1786. * FW returns us this error, if flags were set. FCP4 says
  1787. * FCP_RSP_LEN_VAL in flags shall be set for TM completions.
  1788. * So if a target were to set this bit, we expect that the
  1789. * rsp_code is set to FCP_TMF_CMPL for a successful TM
  1790. * completion. Any other rsp_code means TM operation failed.
  1791. * If a target were to just ignore setting flags, we treat
  1792. * the TM operation as success, and FW returns FW_SUCCESS.
  1793. */
  1794. if (req->wr_status == FW_SCSI_RSP_ERR) {
  1795. dma_buf = &req->dma_buf;
  1796. fcp_resp = (struct fcp_resp_with_ext *)dma_buf->vaddr;
  1797. rsp_info = (struct fcp_resp_rsp_info *)(fcp_resp + 1);
  1798. flags = fcp_resp->resp.fr_flags;
  1799. /* Modify return status if flags indicate success */
  1800. if (flags & FCP_RSP_LEN_VAL)
  1801. if (rsp_info->rsp_code == FCP_TMF_CMPL)
  1802. cmnd->SCp.Status = FW_SUCCESS;
  1803. csio_dbg(hw, "TM FCP rsp code: %d\n", rsp_info->rsp_code);
  1804. }
  1805. /* Wake up the TM handler thread */
  1806. csio_scsi_cmnd(req) = NULL;
  1807. }
  1808. static int
  1809. csio_eh_lun_reset_handler(struct scsi_cmnd *cmnd)
  1810. {
  1811. struct csio_lnode *ln = shost_priv(cmnd->device->host);
  1812. struct csio_hw *hw = csio_lnode_to_hw(ln);
  1813. struct csio_scsim *scsim = csio_hw_to_scsim(hw);
  1814. struct csio_rnode *rn = (struct csio_rnode *)(cmnd->device->hostdata);
  1815. struct csio_ioreq *ioreq = NULL;
  1816. struct csio_scsi_qset *sqset;
  1817. unsigned long flags;
  1818. int retval;
  1819. int count, ret;
  1820. LIST_HEAD(local_q);
  1821. struct csio_scsi_level_data sld;
  1822. if (!rn)
  1823. goto fail;
  1824. csio_dbg(hw, "Request to reset LUN:%d (ssni:0x%x tgtid:%d)\n",
  1825. cmnd->device->lun, rn->flowid, rn->scsi_id);
  1826. if (!csio_is_lnode_ready(ln)) {
  1827. csio_err(hw,
  1828. "LUN reset cannot be issued on non-ready"
  1829. " local node vnpi:0x%x (LUN:%d)\n",
  1830. ln->vnp_flowid, cmnd->device->lun);
  1831. goto fail;
  1832. }
  1833. /* Lnode is ready, now wait on rport node readiness */
  1834. ret = fc_block_scsi_eh(cmnd);
  1835. if (ret)
  1836. return ret;
  1837. /*
  1838. * If we have blocked in the previous call, at this point, either the
  1839. * remote node has come back online, or device loss timer has fired
  1840. * and the remote node is destroyed. Allow the LUN reset only for
  1841. * the former case, since LUN reset is a TMF I/O on the wire, and we
  1842. * need a valid session to issue it.
  1843. */
  1844. if (fc_remote_port_chkready(rn->rport)) {
  1845. csio_err(hw,
  1846. "LUN reset cannot be issued on non-ready"
  1847. " remote node ssni:0x%x (LUN:%d)\n",
  1848. rn->flowid, cmnd->device->lun);
  1849. goto fail;
  1850. }
  1851. /* Get a free ioreq structure - SM is already set to uninit */
  1852. ioreq = csio_get_scsi_ioreq_lock(hw, scsim);
  1853. if (!ioreq) {
  1854. csio_err(hw, "Out of IO request elements. Active # :%d\n",
  1855. scsim->stats.n_active);
  1856. goto fail;
  1857. }
  1858. sqset = &hw->sqset[ln->portid][smp_processor_id()];
  1859. ioreq->nsge = 0;
  1860. ioreq->lnode = ln;
  1861. ioreq->rnode = rn;
  1862. ioreq->iq_idx = sqset->iq_idx;
  1863. ioreq->eq_idx = sqset->eq_idx;
  1864. csio_scsi_cmnd(ioreq) = cmnd;
  1865. cmnd->host_scribble = (unsigned char *)ioreq;
  1866. cmnd->SCp.Status = 0;
  1867. cmnd->SCp.Message = FCP_TMF_LUN_RESET;
  1868. ioreq->tmo = CSIO_SCSI_LUNRST_TMO_MS / 1000;
  1869. /*
  1870. * FW times the LUN reset for ioreq->tmo, so we got to wait a little
  1871. * longer (10s for now) than that to allow FW to return the timed
  1872. * out command.
  1873. */
  1874. count = DIV_ROUND_UP((ioreq->tmo + 10) * 1000, CSIO_SCSI_TM_POLL_MS);
  1875. /* Set cbfn */
  1876. ioreq->io_cbfn = csio_tm_cbfn;
  1877. /* Save of the ioreq info for later use */
  1878. sld.level = CSIO_LEV_LUN;
  1879. sld.lnode = ioreq->lnode;
  1880. sld.rnode = ioreq->rnode;
  1881. sld.oslun = (uint64_t)cmnd->device->lun;
  1882. spin_lock_irqsave(&hw->lock, flags);
  1883. /* Kick off TM SM on the ioreq */
  1884. retval = csio_scsi_start_tm(ioreq);
  1885. spin_unlock_irqrestore(&hw->lock, flags);
  1886. if (retval != 0) {
  1887. csio_err(hw, "Failed to issue LUN reset, req:%p, status:%d\n",
  1888. ioreq, retval);
  1889. goto fail_ret_ioreq;
  1890. }
  1891. csio_dbg(hw, "Waiting max %d secs for LUN reset completion\n",
  1892. count * (CSIO_SCSI_TM_POLL_MS / 1000));
  1893. /* Wait for completion */
  1894. while ((((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd)
  1895. && count--)
  1896. msleep(CSIO_SCSI_TM_POLL_MS);
  1897. /* LUN reset timed-out */
  1898. if (((struct scsi_cmnd *)csio_scsi_cmnd(ioreq)) == cmnd) {
  1899. csio_err(hw, "LUN reset (%d:%d) timed out\n",
  1900. cmnd->device->id, cmnd->device->lun);
  1901. spin_lock_irq(&hw->lock);
  1902. csio_scsi_drvcleanup(ioreq);
  1903. list_del_init(&ioreq->sm.sm_list);
  1904. spin_unlock_irq(&hw->lock);
  1905. goto fail_ret_ioreq;
  1906. }
  1907. /* LUN reset returned, check cached status */
  1908. if (cmnd->SCp.Status != FW_SUCCESS) {
  1909. csio_err(hw, "LUN reset failed (%d:%d), status: %d\n",
  1910. cmnd->device->id, cmnd->device->lun, cmnd->SCp.Status);
  1911. goto fail;
  1912. }
  1913. /* LUN reset succeeded, Start aborting affected I/Os */
  1914. /*
  1915. * Since the host guarantees during LUN reset that there
  1916. * will not be any more I/Os to that LUN, until the LUN reset
  1917. * completes, we gather pending I/Os after the LUN reset.
  1918. */
  1919. spin_lock_irq(&hw->lock);
  1920. csio_scsi_gather_active_ios(scsim, &sld, &local_q);
  1921. retval = csio_scsi_abort_io_q(scsim, &local_q, 30000);
  1922. spin_unlock_irq(&hw->lock);
  1923. /* Aborts may have timed out */
  1924. if (retval != 0) {
  1925. csio_err(hw,
  1926. "Attempt to abort I/Os during LUN reset of %d"
  1927. " returned %d\n", cmnd->device->lun, retval);
  1928. /* Return I/Os back to active_q */
  1929. spin_lock_irq(&hw->lock);
  1930. list_splice_tail_init(&local_q, &scsim->active_q);
  1931. spin_unlock_irq(&hw->lock);
  1932. goto fail;
  1933. }
  1934. CSIO_INC_STATS(rn, n_lun_rst);
  1935. csio_info(hw, "LUN reset occurred (%d:%d)\n",
  1936. cmnd->device->id, cmnd->device->lun);
  1937. return SUCCESS;
  1938. fail_ret_ioreq:
  1939. csio_put_scsi_ioreq_lock(hw, scsim, ioreq);
  1940. fail:
  1941. CSIO_INC_STATS(rn, n_lun_rst_fail);
  1942. return FAILED;
  1943. }
  1944. static int
  1945. csio_slave_alloc(struct scsi_device *sdev)
  1946. {
  1947. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1948. if (!rport || fc_remote_port_chkready(rport))
  1949. return -ENXIO;
  1950. sdev->hostdata = *((struct csio_lnode **)(rport->dd_data));
  1951. return 0;
  1952. }
  1953. static int
  1954. csio_slave_configure(struct scsi_device *sdev)
  1955. {
  1956. if (sdev->tagged_supported)
  1957. scsi_activate_tcq(sdev, csio_lun_qdepth);
  1958. else
  1959. scsi_deactivate_tcq(sdev, csio_lun_qdepth);
  1960. return 0;
  1961. }
  1962. static void
  1963. csio_slave_destroy(struct scsi_device *sdev)
  1964. {
  1965. sdev->hostdata = NULL;
  1966. }
  1967. static int
  1968. csio_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1969. {
  1970. struct csio_lnode *ln = shost_priv(shost);
  1971. int rv = 1;
  1972. spin_lock_irq(shost->host_lock);
  1973. if (!ln->hwp || csio_list_deleted(&ln->sm.sm_list))
  1974. goto out;
  1975. rv = csio_scan_done(ln, jiffies, time, csio_max_scan_tmo * HZ,
  1976. csio_delta_scan_tmo * HZ);
  1977. out:
  1978. spin_unlock_irq(shost->host_lock);
  1979. return rv;
  1980. }
  1981. struct scsi_host_template csio_fcoe_shost_template = {
  1982. .module = THIS_MODULE,
  1983. .name = CSIO_DRV_DESC,
  1984. .proc_name = KBUILD_MODNAME,
  1985. .queuecommand = csio_queuecommand,
  1986. .eh_abort_handler = csio_eh_abort_handler,
  1987. .eh_device_reset_handler = csio_eh_lun_reset_handler,
  1988. .slave_alloc = csio_slave_alloc,
  1989. .slave_configure = csio_slave_configure,
  1990. .slave_destroy = csio_slave_destroy,
  1991. .scan_finished = csio_scan_finished,
  1992. .this_id = -1,
  1993. .sg_tablesize = CSIO_SCSI_MAX_SGE,
  1994. .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
  1995. .use_clustering = ENABLE_CLUSTERING,
  1996. .shost_attrs = csio_fcoe_lport_attrs,
  1997. .max_sectors = CSIO_MAX_SECTOR_SIZE,
  1998. };
  1999. struct scsi_host_template csio_fcoe_shost_vport_template = {
  2000. .module = THIS_MODULE,
  2001. .name = CSIO_DRV_DESC,
  2002. .proc_name = KBUILD_MODNAME,
  2003. .queuecommand = csio_queuecommand,
  2004. .eh_abort_handler = csio_eh_abort_handler,
  2005. .eh_device_reset_handler = csio_eh_lun_reset_handler,
  2006. .slave_alloc = csio_slave_alloc,
  2007. .slave_configure = csio_slave_configure,
  2008. .slave_destroy = csio_slave_destroy,
  2009. .scan_finished = csio_scan_finished,
  2010. .this_id = -1,
  2011. .sg_tablesize = CSIO_SCSI_MAX_SGE,
  2012. .cmd_per_lun = CSIO_MAX_CMD_PER_LUN,
  2013. .use_clustering = ENABLE_CLUSTERING,
  2014. .shost_attrs = csio_fcoe_vport_attrs,
  2015. .max_sectors = CSIO_MAX_SECTOR_SIZE,
  2016. };
  2017. /*
  2018. * csio_scsi_alloc_ddp_bufs - Allocate buffers for DDP of unaligned SGLs.
  2019. * @scm: SCSI Module
  2020. * @hw: HW device.
  2021. * @buf_size: buffer size
  2022. * @num_buf : Number of buffers.
  2023. *
  2024. * This routine allocates DMA buffers required for SCSI Data xfer, if
  2025. * each SGL buffer for a SCSI Read request posted by SCSI midlayer are
  2026. * not virtually contiguous.
  2027. */
  2028. static int
  2029. csio_scsi_alloc_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw,
  2030. int buf_size, int num_buf)
  2031. {
  2032. int n = 0;
  2033. struct list_head *tmp;
  2034. struct csio_dma_buf *ddp_desc = NULL;
  2035. uint32_t unit_size = 0;
  2036. if (!num_buf)
  2037. return 0;
  2038. if (!buf_size)
  2039. return -EINVAL;
  2040. INIT_LIST_HEAD(&scm->ddp_freelist);
  2041. /* Align buf size to page size */
  2042. buf_size = (buf_size + PAGE_SIZE - 1) & PAGE_MASK;
  2043. /* Initialize dma descriptors */
  2044. for (n = 0; n < num_buf; n++) {
  2045. /* Set unit size to request size */
  2046. unit_size = buf_size;
  2047. ddp_desc = kzalloc(sizeof(struct csio_dma_buf), GFP_KERNEL);
  2048. if (!ddp_desc) {
  2049. csio_err(hw,
  2050. "Failed to allocate ddp descriptors,"
  2051. " Num allocated = %d.\n",
  2052. scm->stats.n_free_ddp);
  2053. goto no_mem;
  2054. }
  2055. /* Allocate Dma buffers for DDP */
  2056. ddp_desc->vaddr = pci_alloc_consistent(hw->pdev, unit_size,
  2057. &ddp_desc->paddr);
  2058. if (!ddp_desc->vaddr) {
  2059. csio_err(hw,
  2060. "SCSI response DMA buffer (ddp) allocation"
  2061. " failed!\n");
  2062. kfree(ddp_desc);
  2063. goto no_mem;
  2064. }
  2065. ddp_desc->len = unit_size;
  2066. /* Added it to scsi ddp freelist */
  2067. list_add_tail(&ddp_desc->list, &scm->ddp_freelist);
  2068. CSIO_INC_STATS(scm, n_free_ddp);
  2069. }
  2070. return 0;
  2071. no_mem:
  2072. /* release dma descs back to freelist and free dma memory */
  2073. list_for_each(tmp, &scm->ddp_freelist) {
  2074. ddp_desc = (struct csio_dma_buf *) tmp;
  2075. tmp = csio_list_prev(tmp);
  2076. pci_free_consistent(hw->pdev, ddp_desc->len, ddp_desc->vaddr,
  2077. ddp_desc->paddr);
  2078. list_del_init(&ddp_desc->list);
  2079. kfree(ddp_desc);
  2080. }
  2081. scm->stats.n_free_ddp = 0;
  2082. return -ENOMEM;
  2083. }
  2084. /*
  2085. * csio_scsi_free_ddp_bufs - free DDP buffers of unaligned SGLs.
  2086. * @scm: SCSI Module
  2087. * @hw: HW device.
  2088. *
  2089. * This routine frees ddp buffers.
  2090. */
  2091. static void
  2092. csio_scsi_free_ddp_bufs(struct csio_scsim *scm, struct csio_hw *hw)
  2093. {
  2094. struct list_head *tmp;
  2095. struct csio_dma_buf *ddp_desc;
  2096. /* release dma descs back to freelist and free dma memory */
  2097. list_for_each(tmp, &scm->ddp_freelist) {
  2098. ddp_desc = (struct csio_dma_buf *) tmp;
  2099. tmp = csio_list_prev(tmp);
  2100. pci_free_consistent(hw->pdev, ddp_desc->len, ddp_desc->vaddr,
  2101. ddp_desc->paddr);
  2102. list_del_init(&ddp_desc->list);
  2103. kfree(ddp_desc);
  2104. }
  2105. scm->stats.n_free_ddp = 0;
  2106. }
  2107. /**
  2108. * csio_scsim_init - Initialize SCSI Module
  2109. * @scm: SCSI Module
  2110. * @hw: HW module
  2111. *
  2112. */
  2113. int
  2114. csio_scsim_init(struct csio_scsim *scm, struct csio_hw *hw)
  2115. {
  2116. int i;
  2117. struct csio_ioreq *ioreq;
  2118. struct csio_dma_buf *dma_buf;
  2119. INIT_LIST_HEAD(&scm->active_q);
  2120. scm->hw = hw;
  2121. scm->proto_cmd_len = sizeof(struct fcp_cmnd);
  2122. scm->proto_rsp_len = CSIO_SCSI_RSP_LEN;
  2123. scm->max_sge = CSIO_SCSI_MAX_SGE;
  2124. spin_lock_init(&scm->freelist_lock);
  2125. /* Pre-allocate ioreqs and initialize them */
  2126. INIT_LIST_HEAD(&scm->ioreq_freelist);
  2127. for (i = 0; i < csio_scsi_ioreqs; i++) {
  2128. ioreq = kzalloc(sizeof(struct csio_ioreq), GFP_KERNEL);
  2129. if (!ioreq) {
  2130. csio_err(hw,
  2131. "I/O request element allocation failed, "
  2132. " Num allocated = %d.\n",
  2133. scm->stats.n_free_ioreq);
  2134. goto free_ioreq;
  2135. }
  2136. /* Allocate Dma buffers for Response Payload */
  2137. dma_buf = &ioreq->dma_buf;
  2138. dma_buf->vaddr = pci_pool_alloc(hw->scsi_pci_pool, GFP_KERNEL,
  2139. &dma_buf->paddr);
  2140. if (!dma_buf->vaddr) {
  2141. csio_err(hw,
  2142. "SCSI response DMA buffer allocation"
  2143. " failed!\n");
  2144. kfree(ioreq);
  2145. goto free_ioreq;
  2146. }
  2147. dma_buf->len = scm->proto_rsp_len;
  2148. /* Set state to uninit */
  2149. csio_init_state(&ioreq->sm, csio_scsis_uninit);
  2150. INIT_LIST_HEAD(&ioreq->gen_list);
  2151. init_completion(&ioreq->cmplobj);
  2152. list_add_tail(&ioreq->sm.sm_list, &scm->ioreq_freelist);
  2153. CSIO_INC_STATS(scm, n_free_ioreq);
  2154. }
  2155. if (csio_scsi_alloc_ddp_bufs(scm, hw, PAGE_SIZE, csio_ddp_descs))
  2156. goto free_ioreq;
  2157. return 0;
  2158. free_ioreq:
  2159. /*
  2160. * Free up existing allocations, since an error
  2161. * from here means we are returning for good
  2162. */
  2163. while (!list_empty(&scm->ioreq_freelist)) {
  2164. struct csio_sm *tmp;
  2165. tmp = list_first_entry(&scm->ioreq_freelist,
  2166. struct csio_sm, sm_list);
  2167. list_del_init(&tmp->sm_list);
  2168. ioreq = (struct csio_ioreq *)tmp;
  2169. dma_buf = &ioreq->dma_buf;
  2170. pci_pool_free(hw->scsi_pci_pool, dma_buf->vaddr,
  2171. dma_buf->paddr);
  2172. kfree(ioreq);
  2173. }
  2174. scm->stats.n_free_ioreq = 0;
  2175. return -ENOMEM;
  2176. }
  2177. /**
  2178. * csio_scsim_exit: Uninitialize SCSI Module
  2179. * @scm: SCSI Module
  2180. *
  2181. */
  2182. void
  2183. csio_scsim_exit(struct csio_scsim *scm)
  2184. {
  2185. struct csio_ioreq *ioreq;
  2186. struct csio_dma_buf *dma_buf;
  2187. while (!list_empty(&scm->ioreq_freelist)) {
  2188. struct csio_sm *tmp;
  2189. tmp = list_first_entry(&scm->ioreq_freelist,
  2190. struct csio_sm, sm_list);
  2191. list_del_init(&tmp->sm_list);
  2192. ioreq = (struct csio_ioreq *)tmp;
  2193. dma_buf = &ioreq->dma_buf;
  2194. pci_pool_free(scm->hw->scsi_pci_pool, dma_buf->vaddr,
  2195. dma_buf->paddr);
  2196. kfree(ioreq);
  2197. }
  2198. scm->stats.n_free_ioreq = 0;
  2199. csio_scsi_free_ddp_bufs(scm, scm->hw);
  2200. }