csio_scsi.c 67 KB

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