fnic_scsi.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  4. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  5. */
  6. #include <linux/mempool.h>
  7. #include <linux/errno.h>
  8. #include <linux/init.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/pci.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/if_ether.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/delay.h>
  18. #include <linux/gfp.h>
  19. #include <scsi/scsi.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_device.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_tcq.h>
  24. #include <scsi/fc/fc_els.h>
  25. #include <scsi/fc/fc_fcoe.h>
  26. #include <scsi/libfc.h>
  27. #include <scsi/fc_frame.h>
  28. #include "fnic_io.h"
  29. #include "fnic.h"
  30. const char *fnic_state_str[] = {
  31. [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
  32. [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
  33. [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
  34. [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
  35. };
  36. static const char *fnic_ioreq_state_str[] = {
  37. [FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
  38. [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
  39. [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
  40. [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
  41. [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
  42. };
  43. static const char *fcpio_status_str[] = {
  44. [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
  45. [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
  46. [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
  47. [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
  48. [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
  49. [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
  50. [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
  51. [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
  52. [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
  53. [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
  54. [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
  55. [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
  56. [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
  57. [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
  58. [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
  59. [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
  60. [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
  61. [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
  62. [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
  63. };
  64. const char *fnic_state_to_str(unsigned int state)
  65. {
  66. if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
  67. return "unknown";
  68. return fnic_state_str[state];
  69. }
  70. static const char *fnic_ioreq_state_to_str(unsigned int state)
  71. {
  72. if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
  73. !fnic_ioreq_state_str[state])
  74. return "unknown";
  75. return fnic_ioreq_state_str[state];
  76. }
  77. static const char *fnic_fcpio_status_to_str(unsigned int status)
  78. {
  79. if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
  80. return "unknown";
  81. return fcpio_status_str[status];
  82. }
  83. static void fnic_cleanup_io(struct fnic *fnic);
  84. static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
  85. struct scsi_cmnd *sc)
  86. {
  87. u32 hash = scsi_cmd_to_rq(sc)->tag & (FNIC_IO_LOCKS - 1);
  88. return &fnic->io_req_lock[hash];
  89. }
  90. static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
  91. int tag)
  92. {
  93. return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
  94. }
  95. /*
  96. * Unmap the data buffer and sense buffer for an io_req,
  97. * also unmap and free the device-private scatter/gather list.
  98. */
  99. static void fnic_release_ioreq_buf(struct fnic *fnic,
  100. struct fnic_io_req *io_req,
  101. struct scsi_cmnd *sc)
  102. {
  103. if (io_req->sgl_list_pa)
  104. dma_unmap_single(&fnic->pdev->dev, io_req->sgl_list_pa,
  105. sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
  106. DMA_TO_DEVICE);
  107. scsi_dma_unmap(sc);
  108. if (io_req->sgl_cnt)
  109. mempool_free(io_req->sgl_list_alloc,
  110. fnic->io_sgl_pool[io_req->sgl_type]);
  111. if (io_req->sense_buf_pa)
  112. dma_unmap_single(&fnic->pdev->dev, io_req->sense_buf_pa,
  113. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  114. }
  115. /* Free up Copy Wq descriptors. Called with copy_wq lock held */
  116. static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
  117. {
  118. /* if no Ack received from firmware, then nothing to clean */
  119. if (!fnic->fw_ack_recd[0])
  120. return 1;
  121. /*
  122. * Update desc_available count based on number of freed descriptors
  123. * Account for wraparound
  124. */
  125. if (wq->to_clean_index <= fnic->fw_ack_index[0])
  126. wq->ring.desc_avail += (fnic->fw_ack_index[0]
  127. - wq->to_clean_index + 1);
  128. else
  129. wq->ring.desc_avail += (wq->ring.desc_count
  130. - wq->to_clean_index
  131. + fnic->fw_ack_index[0] + 1);
  132. /*
  133. * just bump clean index to ack_index+1 accounting for wraparound
  134. * this will essentially free up all descriptors between
  135. * to_clean_index and fw_ack_index, both inclusive
  136. */
  137. wq->to_clean_index =
  138. (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
  139. /* we have processed the acks received so far */
  140. fnic->fw_ack_recd[0] = 0;
  141. return 0;
  142. }
  143. /*
  144. * __fnic_set_state_flags
  145. * Sets/Clears bits in fnic's state_flags
  146. **/
  147. void
  148. __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
  149. unsigned long clearbits)
  150. {
  151. unsigned long flags = 0;
  152. unsigned long host_lock_flags = 0;
  153. spin_lock_irqsave(&fnic->fnic_lock, flags);
  154. spin_lock_irqsave(fnic->lport->host->host_lock, host_lock_flags);
  155. if (clearbits)
  156. fnic->state_flags &= ~st_flags;
  157. else
  158. fnic->state_flags |= st_flags;
  159. spin_unlock_irqrestore(fnic->lport->host->host_lock, host_lock_flags);
  160. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  161. return;
  162. }
  163. /*
  164. * fnic_fw_reset_handler
  165. * Routine to send reset msg to fw
  166. */
  167. int fnic_fw_reset_handler(struct fnic *fnic)
  168. {
  169. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  170. int ret = 0;
  171. unsigned long flags;
  172. /* indicate fwreset to io path */
  173. fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
  174. skb_queue_purge(&fnic->frame_queue);
  175. skb_queue_purge(&fnic->tx_queue);
  176. /* wait for io cmpl */
  177. while (atomic_read(&fnic->in_flight))
  178. schedule_timeout(msecs_to_jiffies(1));
  179. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  180. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  181. free_wq_copy_descs(fnic, wq);
  182. if (!vnic_wq_copy_desc_avail(wq))
  183. ret = -EAGAIN;
  184. else {
  185. fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
  186. atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  187. if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
  188. atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
  189. atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
  190. atomic64_read(
  191. &fnic->fnic_stats.fw_stats.active_fw_reqs));
  192. }
  193. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  194. if (!ret) {
  195. atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
  196. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  197. "Issued fw reset\n");
  198. } else {
  199. fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
  200. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  201. "Failed to issue fw reset\n");
  202. }
  203. return ret;
  204. }
  205. /*
  206. * fnic_flogi_reg_handler
  207. * Routine to send flogi register msg to fw
  208. */
  209. int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
  210. {
  211. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  212. enum fcpio_flogi_reg_format_type format;
  213. struct fc_lport *lp = fnic->lport;
  214. u8 gw_mac[ETH_ALEN];
  215. int ret = 0;
  216. unsigned long flags;
  217. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  218. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  219. free_wq_copy_descs(fnic, wq);
  220. if (!vnic_wq_copy_desc_avail(wq)) {
  221. ret = -EAGAIN;
  222. goto flogi_reg_ioreq_end;
  223. }
  224. if (fnic->ctlr.map_dest) {
  225. eth_broadcast_addr(gw_mac);
  226. format = FCPIO_FLOGI_REG_DEF_DEST;
  227. } else {
  228. memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
  229. format = FCPIO_FLOGI_REG_GW_DEST;
  230. }
  231. if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
  232. fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
  233. fc_id, gw_mac,
  234. fnic->data_src_addr,
  235. lp->r_a_tov, lp->e_d_tov);
  236. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  237. "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
  238. fc_id, fnic->data_src_addr, gw_mac);
  239. } else {
  240. fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
  241. format, fc_id, gw_mac);
  242. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  243. "FLOGI reg issued fcid %x map %d dest %pM\n",
  244. fc_id, fnic->ctlr.map_dest, gw_mac);
  245. }
  246. atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  247. if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
  248. atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
  249. atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
  250. atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
  251. flogi_reg_ioreq_end:
  252. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  253. return ret;
  254. }
  255. /*
  256. * fnic_queue_wq_copy_desc
  257. * Routine to enqueue a wq copy desc
  258. */
  259. static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
  260. struct vnic_wq_copy *wq,
  261. struct fnic_io_req *io_req,
  262. struct scsi_cmnd *sc,
  263. int sg_count)
  264. {
  265. struct scatterlist *sg;
  266. struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
  267. struct fc_rport_libfc_priv *rp = rport->dd_data;
  268. struct host_sg_desc *desc;
  269. struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
  270. unsigned int i;
  271. unsigned long intr_flags;
  272. int flags;
  273. u8 exch_flags;
  274. struct scsi_lun fc_lun;
  275. if (sg_count) {
  276. /* For each SGE, create a device desc entry */
  277. desc = io_req->sgl_list;
  278. for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
  279. desc->addr = cpu_to_le64(sg_dma_address(sg));
  280. desc->len = cpu_to_le32(sg_dma_len(sg));
  281. desc->_resvd = 0;
  282. desc++;
  283. }
  284. io_req->sgl_list_pa = dma_map_single(&fnic->pdev->dev,
  285. io_req->sgl_list,
  286. sizeof(io_req->sgl_list[0]) * sg_count,
  287. DMA_TO_DEVICE);
  288. if (dma_mapping_error(&fnic->pdev->dev, io_req->sgl_list_pa)) {
  289. printk(KERN_ERR "DMA mapping failed\n");
  290. return SCSI_MLQUEUE_HOST_BUSY;
  291. }
  292. }
  293. io_req->sense_buf_pa = dma_map_single(&fnic->pdev->dev,
  294. sc->sense_buffer,
  295. SCSI_SENSE_BUFFERSIZE,
  296. DMA_FROM_DEVICE);
  297. if (dma_mapping_error(&fnic->pdev->dev, io_req->sense_buf_pa)) {
  298. dma_unmap_single(&fnic->pdev->dev, io_req->sgl_list_pa,
  299. sizeof(io_req->sgl_list[0]) * sg_count,
  300. DMA_TO_DEVICE);
  301. printk(KERN_ERR "DMA mapping failed\n");
  302. return SCSI_MLQUEUE_HOST_BUSY;
  303. }
  304. int_to_scsilun(sc->device->lun, &fc_lun);
  305. /* Enqueue the descriptor in the Copy WQ */
  306. spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
  307. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  308. free_wq_copy_descs(fnic, wq);
  309. if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
  310. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  311. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  312. "fnic_queue_wq_copy_desc failure - no descriptors\n");
  313. atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
  314. return SCSI_MLQUEUE_HOST_BUSY;
  315. }
  316. flags = 0;
  317. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  318. flags = FCPIO_ICMND_RDDATA;
  319. else if (sc->sc_data_direction == DMA_TO_DEVICE)
  320. flags = FCPIO_ICMND_WRDATA;
  321. exch_flags = 0;
  322. if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
  323. (rp->flags & FC_RP_FLAGS_RETRY))
  324. exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
  325. fnic_queue_wq_copy_desc_icmnd_16(wq, scsi_cmd_to_rq(sc)->tag,
  326. 0, exch_flags, io_req->sgl_cnt,
  327. SCSI_SENSE_BUFFERSIZE,
  328. io_req->sgl_list_pa,
  329. io_req->sense_buf_pa,
  330. 0, /* scsi cmd ref, always 0 */
  331. FCPIO_ICMND_PTA_SIMPLE,
  332. /* scsi pri and tag */
  333. flags, /* command flags */
  334. sc->cmnd, sc->cmd_len,
  335. scsi_bufflen(sc),
  336. fc_lun.scsi_lun, io_req->port_id,
  337. rport->maxframe_size, rp->r_a_tov,
  338. rp->e_d_tov);
  339. atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  340. if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
  341. atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
  342. atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
  343. atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
  344. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  345. return 0;
  346. }
  347. /*
  348. * fnic_queuecommand
  349. * Routine to send a scsi cdb
  350. * Called with host_lock held and interrupts disabled.
  351. */
  352. static int fnic_queuecommand_lck(struct scsi_cmnd *sc)
  353. {
  354. void (*done)(struct scsi_cmnd *) = scsi_done;
  355. const int tag = scsi_cmd_to_rq(sc)->tag;
  356. struct fc_lport *lp = shost_priv(sc->device->host);
  357. struct fc_rport *rport;
  358. struct fnic_io_req *io_req = NULL;
  359. struct fnic *fnic = lport_priv(lp);
  360. struct fnic_stats *fnic_stats = &fnic->fnic_stats;
  361. struct vnic_wq_copy *wq;
  362. int ret;
  363. u64 cmd_trace;
  364. int sg_count = 0;
  365. unsigned long flags = 0;
  366. unsigned long ptr;
  367. spinlock_t *io_lock = NULL;
  368. int io_lock_acquired = 0;
  369. struct fc_rport_libfc_priv *rp;
  370. if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
  371. return SCSI_MLQUEUE_HOST_BUSY;
  372. if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_FWRESET)))
  373. return SCSI_MLQUEUE_HOST_BUSY;
  374. rport = starget_to_rport(scsi_target(sc->device));
  375. if (!rport) {
  376. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  377. "returning DID_NO_CONNECT for IO as rport is NULL\n");
  378. sc->result = DID_NO_CONNECT << 16;
  379. done(sc);
  380. return 0;
  381. }
  382. ret = fc_remote_port_chkready(rport);
  383. if (ret) {
  384. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  385. "rport is not ready\n");
  386. atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
  387. sc->result = ret;
  388. done(sc);
  389. return 0;
  390. }
  391. rp = rport->dd_data;
  392. if (!rp || rp->rp_state == RPORT_ST_DELETE) {
  393. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  394. "rport 0x%x removed, returning DID_NO_CONNECT\n",
  395. rport->port_id);
  396. atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
  397. sc->result = DID_NO_CONNECT<<16;
  398. done(sc);
  399. return 0;
  400. }
  401. if (rp->rp_state != RPORT_ST_READY) {
  402. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  403. "rport 0x%x in state 0x%x, returning DID_IMM_RETRY\n",
  404. rport->port_id, rp->rp_state);
  405. sc->result = DID_IMM_RETRY << 16;
  406. done(sc);
  407. return 0;
  408. }
  409. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  410. return SCSI_MLQUEUE_HOST_BUSY;
  411. atomic_inc(&fnic->in_flight);
  412. /*
  413. * Release host lock, use driver resource specific locks from here.
  414. * Don't re-enable interrupts in case they were disabled prior to the
  415. * caller disabling them.
  416. */
  417. spin_unlock(lp->host->host_lock);
  418. fnic_priv(sc)->state = FNIC_IOREQ_NOT_INITED;
  419. fnic_priv(sc)->flags = FNIC_NO_FLAGS;
  420. /* Get a new io_req for this SCSI IO */
  421. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  422. if (!io_req) {
  423. atomic64_inc(&fnic_stats->io_stats.alloc_failures);
  424. ret = SCSI_MLQUEUE_HOST_BUSY;
  425. goto out;
  426. }
  427. memset(io_req, 0, sizeof(*io_req));
  428. /* Map the data buffer */
  429. sg_count = scsi_dma_map(sc);
  430. if (sg_count < 0) {
  431. FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
  432. tag, sc, 0, sc->cmnd[0], sg_count, fnic_priv(sc)->state);
  433. mempool_free(io_req, fnic->io_req_pool);
  434. goto out;
  435. }
  436. /* Determine the type of scatter/gather list we need */
  437. io_req->sgl_cnt = sg_count;
  438. io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
  439. if (sg_count > FNIC_DFLT_SG_DESC_CNT)
  440. io_req->sgl_type = FNIC_SGL_CACHE_MAX;
  441. if (sg_count) {
  442. io_req->sgl_list =
  443. mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
  444. GFP_ATOMIC);
  445. if (!io_req->sgl_list) {
  446. atomic64_inc(&fnic_stats->io_stats.alloc_failures);
  447. ret = SCSI_MLQUEUE_HOST_BUSY;
  448. scsi_dma_unmap(sc);
  449. mempool_free(io_req, fnic->io_req_pool);
  450. goto out;
  451. }
  452. /* Cache sgl list allocated address before alignment */
  453. io_req->sgl_list_alloc = io_req->sgl_list;
  454. ptr = (unsigned long) io_req->sgl_list;
  455. if (ptr % FNIC_SG_DESC_ALIGN) {
  456. io_req->sgl_list = (struct host_sg_desc *)
  457. (((unsigned long) ptr
  458. + FNIC_SG_DESC_ALIGN - 1)
  459. & ~(FNIC_SG_DESC_ALIGN - 1));
  460. }
  461. }
  462. /*
  463. * Will acquire lock defore setting to IO initialized.
  464. */
  465. io_lock = fnic_io_lock_hash(fnic, sc);
  466. spin_lock_irqsave(io_lock, flags);
  467. /* initialize rest of io_req */
  468. io_lock_acquired = 1;
  469. io_req->port_id = rport->port_id;
  470. io_req->start_time = jiffies;
  471. fnic_priv(sc)->state = FNIC_IOREQ_CMD_PENDING;
  472. fnic_priv(sc)->io_req = io_req;
  473. fnic_priv(sc)->flags |= FNIC_IO_INITIALIZED;
  474. /* create copy wq desc and enqueue it */
  475. wq = &fnic->wq_copy[0];
  476. ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
  477. if (ret) {
  478. /*
  479. * In case another thread cancelled the request,
  480. * refetch the pointer under the lock.
  481. */
  482. FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
  483. tag, sc, 0, 0, 0, fnic_flags_and_state(sc));
  484. io_req = fnic_priv(sc)->io_req;
  485. fnic_priv(sc)->io_req = NULL;
  486. fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
  487. spin_unlock_irqrestore(io_lock, flags);
  488. if (io_req) {
  489. fnic_release_ioreq_buf(fnic, io_req, sc);
  490. mempool_free(io_req, fnic->io_req_pool);
  491. }
  492. atomic_dec(&fnic->in_flight);
  493. /* acquire host lock before returning to SCSI */
  494. spin_lock(lp->host->host_lock);
  495. return ret;
  496. } else {
  497. atomic64_inc(&fnic_stats->io_stats.active_ios);
  498. atomic64_inc(&fnic_stats->io_stats.num_ios);
  499. if (atomic64_read(&fnic_stats->io_stats.active_ios) >
  500. atomic64_read(&fnic_stats->io_stats.max_active_ios))
  501. atomic64_set(&fnic_stats->io_stats.max_active_ios,
  502. atomic64_read(&fnic_stats->io_stats.active_ios));
  503. /* REVISIT: Use per IO lock in the final code */
  504. fnic_priv(sc)->flags |= FNIC_IO_ISSUED;
  505. }
  506. out:
  507. cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
  508. (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
  509. (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
  510. sc->cmnd[5]);
  511. FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
  512. tag, sc, io_req, sg_count, cmd_trace,
  513. fnic_flags_and_state(sc));
  514. /* if only we issued IO, will we have the io lock */
  515. if (io_lock_acquired)
  516. spin_unlock_irqrestore(io_lock, flags);
  517. atomic_dec(&fnic->in_flight);
  518. /* acquire host lock before returning to SCSI */
  519. spin_lock(lp->host->host_lock);
  520. return ret;
  521. }
  522. DEF_SCSI_QCMD(fnic_queuecommand)
  523. /*
  524. * fnic_fcpio_fw_reset_cmpl_handler
  525. * Routine to handle fw reset completion
  526. */
  527. static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
  528. struct fcpio_fw_req *desc)
  529. {
  530. u8 type;
  531. u8 hdr_status;
  532. struct fcpio_tag tag;
  533. int ret = 0;
  534. unsigned long flags;
  535. struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
  536. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  537. atomic64_inc(&reset_stats->fw_reset_completions);
  538. /* Clean up all outstanding io requests */
  539. fnic_cleanup_io(fnic);
  540. atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
  541. atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
  542. atomic64_set(&fnic->io_cmpl_skip, 0);
  543. spin_lock_irqsave(&fnic->fnic_lock, flags);
  544. /* fnic should be in FC_TRANS_ETH_MODE */
  545. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
  546. /* Check status of reset completion */
  547. if (!hdr_status) {
  548. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  549. "reset cmpl success\n");
  550. /* Ready to send flogi out */
  551. fnic->state = FNIC_IN_ETH_MODE;
  552. } else {
  553. FNIC_SCSI_DBG(KERN_DEBUG,
  554. fnic->lport->host,
  555. "fnic fw_reset : failed %s\n",
  556. fnic_fcpio_status_to_str(hdr_status));
  557. /*
  558. * Unable to change to eth mode, cannot send out flogi
  559. * Change state to fc mode, so that subsequent Flogi
  560. * requests from libFC will cause more attempts to
  561. * reset the firmware. Free the cached flogi
  562. */
  563. fnic->state = FNIC_IN_FC_MODE;
  564. atomic64_inc(&reset_stats->fw_reset_failures);
  565. ret = -1;
  566. }
  567. } else {
  568. FNIC_SCSI_DBG(KERN_DEBUG,
  569. fnic->lport->host,
  570. "Unexpected state %s while processing"
  571. " reset cmpl\n", fnic_state_to_str(fnic->state));
  572. atomic64_inc(&reset_stats->fw_reset_failures);
  573. ret = -1;
  574. }
  575. /* Thread removing device blocks till firmware reset is complete */
  576. if (fnic->remove_wait)
  577. complete(fnic->remove_wait);
  578. /*
  579. * If fnic is being removed, or fw reset failed
  580. * free the flogi frame. Else, send it out
  581. */
  582. if (fnic->remove_wait || ret) {
  583. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  584. skb_queue_purge(&fnic->tx_queue);
  585. goto reset_cmpl_handler_end;
  586. }
  587. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  588. fnic_flush_tx(fnic);
  589. reset_cmpl_handler_end:
  590. fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
  591. return ret;
  592. }
  593. /*
  594. * fnic_fcpio_flogi_reg_cmpl_handler
  595. * Routine to handle flogi register completion
  596. */
  597. static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
  598. struct fcpio_fw_req *desc)
  599. {
  600. u8 type;
  601. u8 hdr_status;
  602. struct fcpio_tag tag;
  603. int ret = 0;
  604. unsigned long flags;
  605. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  606. /* Update fnic state based on status of flogi reg completion */
  607. spin_lock_irqsave(&fnic->fnic_lock, flags);
  608. if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
  609. /* Check flogi registration completion status */
  610. if (!hdr_status) {
  611. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  612. "flog reg succeeded\n");
  613. fnic->state = FNIC_IN_FC_MODE;
  614. } else {
  615. FNIC_SCSI_DBG(KERN_DEBUG,
  616. fnic->lport->host,
  617. "fnic flogi reg :failed %s\n",
  618. fnic_fcpio_status_to_str(hdr_status));
  619. fnic->state = FNIC_IN_ETH_MODE;
  620. ret = -1;
  621. }
  622. } else {
  623. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  624. "Unexpected fnic state %s while"
  625. " processing flogi reg completion\n",
  626. fnic_state_to_str(fnic->state));
  627. ret = -1;
  628. }
  629. if (!ret) {
  630. if (fnic->stop_rx_link_events) {
  631. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  632. goto reg_cmpl_handler_end;
  633. }
  634. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  635. fnic_flush_tx(fnic);
  636. queue_work(fnic_event_queue, &fnic->frame_work);
  637. } else {
  638. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  639. }
  640. reg_cmpl_handler_end:
  641. return ret;
  642. }
  643. static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
  644. u16 request_out)
  645. {
  646. if (wq->to_clean_index <= wq->to_use_index) {
  647. /* out of range, stale request_out index */
  648. if (request_out < wq->to_clean_index ||
  649. request_out >= wq->to_use_index)
  650. return 0;
  651. } else {
  652. /* out of range, stale request_out index */
  653. if (request_out < wq->to_clean_index &&
  654. request_out >= wq->to_use_index)
  655. return 0;
  656. }
  657. /* request_out index is in range */
  658. return 1;
  659. }
  660. /*
  661. * Mark that ack received and store the Ack index. If there are multiple
  662. * acks received before Tx thread cleans it up, the latest value will be
  663. * used which is correct behavior. This state should be in the copy Wq
  664. * instead of in the fnic
  665. */
  666. static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
  667. unsigned int cq_index,
  668. struct fcpio_fw_req *desc)
  669. {
  670. struct vnic_wq_copy *wq;
  671. u16 request_out = desc->u.ack.request_out;
  672. unsigned long flags;
  673. u64 *ox_id_tag = (u64 *)(void *)desc;
  674. /* mark the ack state */
  675. wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
  676. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  677. fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
  678. if (is_ack_index_in_range(wq, request_out)) {
  679. fnic->fw_ack_index[0] = request_out;
  680. fnic->fw_ack_recd[0] = 1;
  681. } else
  682. atomic64_inc(
  683. &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
  684. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  685. FNIC_TRACE(fnic_fcpio_ack_handler,
  686. fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
  687. ox_id_tag[4], ox_id_tag[5]);
  688. }
  689. /*
  690. * fnic_fcpio_icmnd_cmpl_handler
  691. * Routine to handle icmnd completions
  692. */
  693. static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
  694. struct fcpio_fw_req *desc)
  695. {
  696. u8 type;
  697. u8 hdr_status;
  698. struct fcpio_tag tag;
  699. u32 id;
  700. u64 xfer_len = 0;
  701. struct fcpio_icmnd_cmpl *icmnd_cmpl;
  702. struct fnic_io_req *io_req;
  703. struct scsi_cmnd *sc;
  704. struct fnic_stats *fnic_stats = &fnic->fnic_stats;
  705. unsigned long flags;
  706. spinlock_t *io_lock;
  707. u64 cmd_trace;
  708. unsigned long start_time;
  709. unsigned long io_duration_time;
  710. /* Decode the cmpl description to get the io_req id */
  711. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  712. fcpio_tag_id_dec(&tag, &id);
  713. icmnd_cmpl = &desc->u.icmnd_cmpl;
  714. if (id >= fnic->fnic_max_tag_id) {
  715. shost_printk(KERN_ERR, fnic->lport->host,
  716. "Tag out of range tag %x hdr status = %s\n",
  717. id, fnic_fcpio_status_to_str(hdr_status));
  718. return;
  719. }
  720. sc = scsi_host_find_tag(fnic->lport->host, id);
  721. WARN_ON_ONCE(!sc);
  722. if (!sc) {
  723. atomic64_inc(&fnic_stats->io_stats.sc_null);
  724. shost_printk(KERN_ERR, fnic->lport->host,
  725. "icmnd_cmpl sc is null - "
  726. "hdr status = %s tag = 0x%x desc = 0x%p\n",
  727. fnic_fcpio_status_to_str(hdr_status), id, desc);
  728. FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
  729. fnic->lport->host->host_no, id,
  730. ((u64)icmnd_cmpl->_resvd0[1] << 16 |
  731. (u64)icmnd_cmpl->_resvd0[0]),
  732. ((u64)hdr_status << 16 |
  733. (u64)icmnd_cmpl->scsi_status << 8 |
  734. (u64)icmnd_cmpl->flags), desc,
  735. (u64)icmnd_cmpl->residual, 0);
  736. return;
  737. }
  738. io_lock = fnic_io_lock_hash(fnic, sc);
  739. spin_lock_irqsave(io_lock, flags);
  740. io_req = fnic_priv(sc)->io_req;
  741. WARN_ON_ONCE(!io_req);
  742. if (!io_req) {
  743. atomic64_inc(&fnic_stats->io_stats.ioreq_null);
  744. fnic_priv(sc)->flags |= FNIC_IO_REQ_NULL;
  745. spin_unlock_irqrestore(io_lock, flags);
  746. shost_printk(KERN_ERR, fnic->lport->host,
  747. "icmnd_cmpl io_req is null - "
  748. "hdr status = %s tag = 0x%x sc 0x%p\n",
  749. fnic_fcpio_status_to_str(hdr_status), id, sc);
  750. return;
  751. }
  752. start_time = io_req->start_time;
  753. /* firmware completed the io */
  754. io_req->io_completed = 1;
  755. /*
  756. * if SCSI-ML has already issued abort on this command,
  757. * set completion of the IO. The abts path will clean it up
  758. */
  759. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
  760. /*
  761. * set the FNIC_IO_DONE so that this doesn't get
  762. * flagged as 'out of order' if it was not aborted
  763. */
  764. fnic_priv(sc)->flags |= FNIC_IO_DONE;
  765. fnic_priv(sc)->flags |= FNIC_IO_ABTS_PENDING;
  766. spin_unlock_irqrestore(io_lock, flags);
  767. if(FCPIO_ABORTED == hdr_status)
  768. fnic_priv(sc)->flags |= FNIC_IO_ABORTED;
  769. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  770. "icmnd_cmpl abts pending "
  771. "hdr status = %s tag = 0x%x sc = 0x%p "
  772. "scsi_status = %x residual = %d\n",
  773. fnic_fcpio_status_to_str(hdr_status),
  774. id, sc,
  775. icmnd_cmpl->scsi_status,
  776. icmnd_cmpl->residual);
  777. return;
  778. }
  779. /* Mark the IO as complete */
  780. fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
  781. icmnd_cmpl = &desc->u.icmnd_cmpl;
  782. switch (hdr_status) {
  783. case FCPIO_SUCCESS:
  784. sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
  785. xfer_len = scsi_bufflen(sc);
  786. if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER) {
  787. xfer_len -= icmnd_cmpl->residual;
  788. scsi_set_resid(sc, icmnd_cmpl->residual);
  789. }
  790. if (icmnd_cmpl->scsi_status == SAM_STAT_CHECK_CONDITION)
  791. atomic64_inc(&fnic_stats->misc_stats.check_condition);
  792. if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
  793. atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
  794. break;
  795. case FCPIO_TIMEOUT: /* request was timed out */
  796. atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
  797. sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
  798. break;
  799. case FCPIO_ABORTED: /* request was aborted */
  800. atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
  801. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  802. break;
  803. case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
  804. atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
  805. scsi_set_resid(sc, icmnd_cmpl->residual);
  806. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  807. break;
  808. case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
  809. atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
  810. sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
  811. break;
  812. case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
  813. atomic64_inc(&fnic_stats->io_stats.io_not_found);
  814. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  815. break;
  816. case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
  817. atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
  818. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  819. break;
  820. case FCPIO_FW_ERR: /* request was terminated due fw error */
  821. atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
  822. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  823. break;
  824. case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
  825. atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
  826. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  827. break;
  828. case FCPIO_INVALID_HEADER: /* header contains invalid data */
  829. case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
  830. case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
  831. default:
  832. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  833. break;
  834. }
  835. /* Break link with the SCSI command */
  836. fnic_priv(sc)->io_req = NULL;
  837. fnic_priv(sc)->flags |= FNIC_IO_DONE;
  838. if (hdr_status != FCPIO_SUCCESS) {
  839. atomic64_inc(&fnic_stats->io_stats.io_failures);
  840. shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
  841. fnic_fcpio_status_to_str(hdr_status));
  842. }
  843. fnic_release_ioreq_buf(fnic, io_req, sc);
  844. cmd_trace = ((u64)hdr_status << 56) |
  845. (u64)icmnd_cmpl->scsi_status << 48 |
  846. (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
  847. (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
  848. (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
  849. FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
  850. sc->device->host->host_no, id, sc,
  851. ((u64)icmnd_cmpl->_resvd0[1] << 56 |
  852. (u64)icmnd_cmpl->_resvd0[0] << 48 |
  853. jiffies_to_msecs(jiffies - start_time)),
  854. desc, cmd_trace, fnic_flags_and_state(sc));
  855. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  856. fnic->lport->host_stats.fcp_input_requests++;
  857. fnic->fcp_input_bytes += xfer_len;
  858. } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
  859. fnic->lport->host_stats.fcp_output_requests++;
  860. fnic->fcp_output_bytes += xfer_len;
  861. } else
  862. fnic->lport->host_stats.fcp_control_requests++;
  863. /* Call SCSI completion function to complete the IO */
  864. scsi_done(sc);
  865. spin_unlock_irqrestore(io_lock, flags);
  866. mempool_free(io_req, fnic->io_req_pool);
  867. atomic64_dec(&fnic_stats->io_stats.active_ios);
  868. if (atomic64_read(&fnic->io_cmpl_skip))
  869. atomic64_dec(&fnic->io_cmpl_skip);
  870. else
  871. atomic64_inc(&fnic_stats->io_stats.io_completions);
  872. io_duration_time = jiffies_to_msecs(jiffies) -
  873. jiffies_to_msecs(start_time);
  874. if(io_duration_time <= 10)
  875. atomic64_inc(&fnic_stats->io_stats.io_btw_0_to_10_msec);
  876. else if(io_duration_time <= 100)
  877. atomic64_inc(&fnic_stats->io_stats.io_btw_10_to_100_msec);
  878. else if(io_duration_time <= 500)
  879. atomic64_inc(&fnic_stats->io_stats.io_btw_100_to_500_msec);
  880. else if(io_duration_time <= 5000)
  881. atomic64_inc(&fnic_stats->io_stats.io_btw_500_to_5000_msec);
  882. else if(io_duration_time <= 10000)
  883. atomic64_inc(&fnic_stats->io_stats.io_btw_5000_to_10000_msec);
  884. else if(io_duration_time <= 30000)
  885. atomic64_inc(&fnic_stats->io_stats.io_btw_10000_to_30000_msec);
  886. else {
  887. atomic64_inc(&fnic_stats->io_stats.io_greater_than_30000_msec);
  888. if(io_duration_time > atomic64_read(&fnic_stats->io_stats.current_max_io_time))
  889. atomic64_set(&fnic_stats->io_stats.current_max_io_time, io_duration_time);
  890. }
  891. }
  892. /* fnic_fcpio_itmf_cmpl_handler
  893. * Routine to handle itmf completions
  894. */
  895. static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
  896. struct fcpio_fw_req *desc)
  897. {
  898. u8 type;
  899. u8 hdr_status;
  900. struct fcpio_tag tag;
  901. u32 id;
  902. struct scsi_cmnd *sc;
  903. struct fnic_io_req *io_req;
  904. struct fnic_stats *fnic_stats = &fnic->fnic_stats;
  905. struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
  906. struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
  907. struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
  908. unsigned long flags;
  909. spinlock_t *io_lock;
  910. unsigned long start_time;
  911. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  912. fcpio_tag_id_dec(&tag, &id);
  913. if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
  914. shost_printk(KERN_ERR, fnic->lport->host,
  915. "Tag out of range tag %x hdr status = %s\n",
  916. id, fnic_fcpio_status_to_str(hdr_status));
  917. return;
  918. }
  919. sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
  920. WARN_ON_ONCE(!sc);
  921. if (!sc) {
  922. atomic64_inc(&fnic_stats->io_stats.sc_null);
  923. shost_printk(KERN_ERR, fnic->lport->host,
  924. "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
  925. fnic_fcpio_status_to_str(hdr_status), id);
  926. return;
  927. }
  928. io_lock = fnic_io_lock_hash(fnic, sc);
  929. spin_lock_irqsave(io_lock, flags);
  930. io_req = fnic_priv(sc)->io_req;
  931. WARN_ON_ONCE(!io_req);
  932. if (!io_req) {
  933. atomic64_inc(&fnic_stats->io_stats.ioreq_null);
  934. spin_unlock_irqrestore(io_lock, flags);
  935. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
  936. shost_printk(KERN_ERR, fnic->lport->host,
  937. "itmf_cmpl io_req is null - "
  938. "hdr status = %s tag = 0x%x sc 0x%p\n",
  939. fnic_fcpio_status_to_str(hdr_status), id, sc);
  940. return;
  941. }
  942. start_time = io_req->start_time;
  943. if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
  944. /* Abort and terminate completion of device reset req */
  945. /* REVISIT : Add asserts about various flags */
  946. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  947. "dev reset abts cmpl recd. id %x status %s\n",
  948. id, fnic_fcpio_status_to_str(hdr_status));
  949. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
  950. fnic_priv(sc)->abts_status = hdr_status;
  951. fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
  952. if (io_req->abts_done)
  953. complete(io_req->abts_done);
  954. spin_unlock_irqrestore(io_lock, flags);
  955. } else if (id & FNIC_TAG_ABORT) {
  956. /* Completion of abort cmd */
  957. switch (hdr_status) {
  958. case FCPIO_SUCCESS:
  959. break;
  960. case FCPIO_TIMEOUT:
  961. if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
  962. atomic64_inc(&abts_stats->abort_fw_timeouts);
  963. else
  964. atomic64_inc(
  965. &term_stats->terminate_fw_timeouts);
  966. break;
  967. case FCPIO_ITMF_REJECTED:
  968. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  969. "abort reject recd. id %d\n",
  970. (int)(id & FNIC_TAG_MASK));
  971. break;
  972. case FCPIO_IO_NOT_FOUND:
  973. if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
  974. atomic64_inc(&abts_stats->abort_io_not_found);
  975. else
  976. atomic64_inc(
  977. &term_stats->terminate_io_not_found);
  978. break;
  979. default:
  980. if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
  981. atomic64_inc(&abts_stats->abort_failures);
  982. else
  983. atomic64_inc(
  984. &term_stats->terminate_failures);
  985. break;
  986. }
  987. if (fnic_priv(sc)->state != FNIC_IOREQ_ABTS_PENDING) {
  988. /* This is a late completion. Ignore it */
  989. spin_unlock_irqrestore(io_lock, flags);
  990. return;
  991. }
  992. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_DONE;
  993. fnic_priv(sc)->abts_status = hdr_status;
  994. /* If the status is IO not found consider it as success */
  995. if (hdr_status == FCPIO_IO_NOT_FOUND)
  996. fnic_priv(sc)->abts_status = FCPIO_SUCCESS;
  997. if (!(fnic_priv(sc)->flags & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
  998. atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
  999. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1000. "abts cmpl recd. id %d status %s\n",
  1001. (int)(id & FNIC_TAG_MASK),
  1002. fnic_fcpio_status_to_str(hdr_status));
  1003. /*
  1004. * If scsi_eh thread is blocked waiting for abts to complete,
  1005. * signal completion to it. IO will be cleaned in the thread
  1006. * else clean it in this context
  1007. */
  1008. if (io_req->abts_done) {
  1009. complete(io_req->abts_done);
  1010. spin_unlock_irqrestore(io_lock, flags);
  1011. } else {
  1012. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1013. "abts cmpl, completing IO\n");
  1014. fnic_priv(sc)->io_req = NULL;
  1015. sc->result = (DID_ERROR << 16);
  1016. spin_unlock_irqrestore(io_lock, flags);
  1017. fnic_release_ioreq_buf(fnic, io_req, sc);
  1018. mempool_free(io_req, fnic->io_req_pool);
  1019. FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
  1020. sc->device->host->host_no, id,
  1021. sc,
  1022. jiffies_to_msecs(jiffies - start_time),
  1023. desc,
  1024. (((u64)hdr_status << 40) |
  1025. (u64)sc->cmnd[0] << 32 |
  1026. (u64)sc->cmnd[2] << 24 |
  1027. (u64)sc->cmnd[3] << 16 |
  1028. (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
  1029. fnic_flags_and_state(sc));
  1030. scsi_done(sc);
  1031. atomic64_dec(&fnic_stats->io_stats.active_ios);
  1032. if (atomic64_read(&fnic->io_cmpl_skip))
  1033. atomic64_dec(&fnic->io_cmpl_skip);
  1034. else
  1035. atomic64_inc(&fnic_stats->io_stats.io_completions);
  1036. }
  1037. } else if (id & FNIC_TAG_DEV_RST) {
  1038. /* Completion of device reset */
  1039. fnic_priv(sc)->lr_status = hdr_status;
  1040. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
  1041. spin_unlock_irqrestore(io_lock, flags);
  1042. fnic_priv(sc)->flags |= FNIC_DEV_RST_ABTS_PENDING;
  1043. FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
  1044. sc->device->host->host_no, id, sc,
  1045. jiffies_to_msecs(jiffies - start_time),
  1046. desc, 0, fnic_flags_and_state(sc));
  1047. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1048. "Terminate pending "
  1049. "dev reset cmpl recd. id %d status %s\n",
  1050. (int)(id & FNIC_TAG_MASK),
  1051. fnic_fcpio_status_to_str(hdr_status));
  1052. return;
  1053. }
  1054. if (fnic_priv(sc)->flags & FNIC_DEV_RST_TIMED_OUT) {
  1055. /* Need to wait for terminate completion */
  1056. spin_unlock_irqrestore(io_lock, flags);
  1057. FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
  1058. sc->device->host->host_no, id, sc,
  1059. jiffies_to_msecs(jiffies - start_time),
  1060. desc, 0, fnic_flags_and_state(sc));
  1061. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1062. "dev reset cmpl recd after time out. "
  1063. "id %d status %s\n",
  1064. (int)(id & FNIC_TAG_MASK),
  1065. fnic_fcpio_status_to_str(hdr_status));
  1066. return;
  1067. }
  1068. fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
  1069. fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
  1070. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1071. "dev reset cmpl recd. id %d status %s\n",
  1072. (int)(id & FNIC_TAG_MASK),
  1073. fnic_fcpio_status_to_str(hdr_status));
  1074. if (io_req->dr_done)
  1075. complete(io_req->dr_done);
  1076. spin_unlock_irqrestore(io_lock, flags);
  1077. } else {
  1078. shost_printk(KERN_ERR, fnic->lport->host,
  1079. "Unexpected itmf io state %s tag %x\n",
  1080. fnic_ioreq_state_to_str(fnic_priv(sc)->state), id);
  1081. spin_unlock_irqrestore(io_lock, flags);
  1082. }
  1083. }
  1084. /*
  1085. * fnic_fcpio_cmpl_handler
  1086. * Routine to service the cq for wq_copy
  1087. */
  1088. static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
  1089. unsigned int cq_index,
  1090. struct fcpio_fw_req *desc)
  1091. {
  1092. struct fnic *fnic = vnic_dev_priv(vdev);
  1093. switch (desc->hdr.type) {
  1094. case FCPIO_ICMND_CMPL: /* fw completed a command */
  1095. case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
  1096. case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
  1097. case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
  1098. case FCPIO_RESET_CMPL: /* fw completed reset */
  1099. atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  1100. break;
  1101. default:
  1102. break;
  1103. }
  1104. switch (desc->hdr.type) {
  1105. case FCPIO_ACK: /* fw copied copy wq desc to its queue */
  1106. fnic_fcpio_ack_handler(fnic, cq_index, desc);
  1107. break;
  1108. case FCPIO_ICMND_CMPL: /* fw completed a command */
  1109. fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
  1110. break;
  1111. case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
  1112. fnic_fcpio_itmf_cmpl_handler(fnic, desc);
  1113. break;
  1114. case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
  1115. case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
  1116. fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
  1117. break;
  1118. case FCPIO_RESET_CMPL: /* fw completed reset */
  1119. fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
  1120. break;
  1121. default:
  1122. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1123. "firmware completion type %d\n",
  1124. desc->hdr.type);
  1125. break;
  1126. }
  1127. return 0;
  1128. }
  1129. /*
  1130. * fnic_wq_copy_cmpl_handler
  1131. * Routine to process wq copy
  1132. */
  1133. int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
  1134. {
  1135. unsigned int wq_work_done = 0;
  1136. unsigned int i, cq_index;
  1137. unsigned int cur_work_done;
  1138. struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
  1139. u64 start_jiffies = 0;
  1140. u64 end_jiffies = 0;
  1141. u64 delta_jiffies = 0;
  1142. u64 delta_ms = 0;
  1143. for (i = 0; i < fnic->wq_copy_count; i++) {
  1144. cq_index = i + fnic->raw_wq_count + fnic->rq_count;
  1145. start_jiffies = jiffies;
  1146. cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
  1147. fnic_fcpio_cmpl_handler,
  1148. copy_work_to_do);
  1149. end_jiffies = jiffies;
  1150. wq_work_done += cur_work_done;
  1151. delta_jiffies = end_jiffies - start_jiffies;
  1152. if (delta_jiffies >
  1153. (u64) atomic64_read(&misc_stats->max_isr_jiffies)) {
  1154. atomic64_set(&misc_stats->max_isr_jiffies,
  1155. delta_jiffies);
  1156. delta_ms = jiffies_to_msecs(delta_jiffies);
  1157. atomic64_set(&misc_stats->max_isr_time_ms, delta_ms);
  1158. atomic64_set(&misc_stats->corr_work_done,
  1159. cur_work_done);
  1160. }
  1161. }
  1162. return wq_work_done;
  1163. }
  1164. static bool fnic_cleanup_io_iter(struct scsi_cmnd *sc, void *data)
  1165. {
  1166. const int tag = scsi_cmd_to_rq(sc)->tag;
  1167. struct fnic *fnic = data;
  1168. struct fnic_io_req *io_req;
  1169. unsigned long flags = 0;
  1170. spinlock_t *io_lock;
  1171. unsigned long start_time = 0;
  1172. struct fnic_stats *fnic_stats = &fnic->fnic_stats;
  1173. io_lock = fnic_io_lock_tag(fnic, tag);
  1174. spin_lock_irqsave(io_lock, flags);
  1175. io_req = fnic_priv(sc)->io_req;
  1176. if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
  1177. !(fnic_priv(sc)->flags & FNIC_DEV_RST_DONE)) {
  1178. /*
  1179. * We will be here only when FW completes reset
  1180. * without sending completions for outstanding ios.
  1181. */
  1182. fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
  1183. if (io_req && io_req->dr_done)
  1184. complete(io_req->dr_done);
  1185. else if (io_req && io_req->abts_done)
  1186. complete(io_req->abts_done);
  1187. spin_unlock_irqrestore(io_lock, flags);
  1188. return true;
  1189. } else if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
  1190. spin_unlock_irqrestore(io_lock, flags);
  1191. return true;
  1192. }
  1193. if (!io_req) {
  1194. spin_unlock_irqrestore(io_lock, flags);
  1195. goto cleanup_scsi_cmd;
  1196. }
  1197. fnic_priv(sc)->io_req = NULL;
  1198. spin_unlock_irqrestore(io_lock, flags);
  1199. /*
  1200. * If there is a scsi_cmnd associated with this io_req, then
  1201. * free the corresponding state
  1202. */
  1203. start_time = io_req->start_time;
  1204. fnic_release_ioreq_buf(fnic, io_req, sc);
  1205. mempool_free(io_req, fnic->io_req_pool);
  1206. cleanup_scsi_cmd:
  1207. sc->result = DID_TRANSPORT_DISRUPTED << 16;
  1208. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1209. "fnic_cleanup_io: tag:0x%x : sc:0x%p duration = %lu DID_TRANSPORT_DISRUPTED\n",
  1210. tag, sc, jiffies - start_time);
  1211. if (atomic64_read(&fnic->io_cmpl_skip))
  1212. atomic64_dec(&fnic->io_cmpl_skip);
  1213. else
  1214. atomic64_inc(&fnic_stats->io_stats.io_completions);
  1215. /* Complete the command to SCSI */
  1216. if (!(fnic_priv(sc)->flags & FNIC_IO_ISSUED))
  1217. shost_printk(KERN_ERR, fnic->lport->host,
  1218. "Calling done for IO not issued to fw: tag:0x%x sc:0x%p\n",
  1219. tag, sc);
  1220. FNIC_TRACE(fnic_cleanup_io,
  1221. sc->device->host->host_no, tag, sc,
  1222. jiffies_to_msecs(jiffies - start_time),
  1223. 0, ((u64)sc->cmnd[0] << 32 |
  1224. (u64)sc->cmnd[2] << 24 |
  1225. (u64)sc->cmnd[3] << 16 |
  1226. (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
  1227. fnic_flags_and_state(sc));
  1228. scsi_done(sc);
  1229. return true;
  1230. }
  1231. static void fnic_cleanup_io(struct fnic *fnic)
  1232. {
  1233. scsi_host_busy_iter(fnic->lport->host,
  1234. fnic_cleanup_io_iter, fnic);
  1235. }
  1236. void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
  1237. struct fcpio_host_req *desc)
  1238. {
  1239. u32 id;
  1240. struct fnic *fnic = vnic_dev_priv(wq->vdev);
  1241. struct fnic_io_req *io_req;
  1242. struct scsi_cmnd *sc;
  1243. unsigned long flags;
  1244. spinlock_t *io_lock;
  1245. unsigned long start_time = 0;
  1246. /* get the tag reference */
  1247. fcpio_tag_id_dec(&desc->hdr.tag, &id);
  1248. id &= FNIC_TAG_MASK;
  1249. if (id >= fnic->fnic_max_tag_id)
  1250. return;
  1251. sc = scsi_host_find_tag(fnic->lport->host, id);
  1252. if (!sc)
  1253. return;
  1254. io_lock = fnic_io_lock_hash(fnic, sc);
  1255. spin_lock_irqsave(io_lock, flags);
  1256. /* Get the IO context which this desc refers to */
  1257. io_req = fnic_priv(sc)->io_req;
  1258. /* fnic interrupts are turned off by now */
  1259. if (!io_req) {
  1260. spin_unlock_irqrestore(io_lock, flags);
  1261. goto wq_copy_cleanup_scsi_cmd;
  1262. }
  1263. fnic_priv(sc)->io_req = NULL;
  1264. spin_unlock_irqrestore(io_lock, flags);
  1265. start_time = io_req->start_time;
  1266. fnic_release_ioreq_buf(fnic, io_req, sc);
  1267. mempool_free(io_req, fnic->io_req_pool);
  1268. wq_copy_cleanup_scsi_cmd:
  1269. sc->result = DID_NO_CONNECT << 16;
  1270. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
  1271. " DID_NO_CONNECT\n");
  1272. FNIC_TRACE(fnic_wq_copy_cleanup_handler,
  1273. sc->device->host->host_no, id, sc,
  1274. jiffies_to_msecs(jiffies - start_time),
  1275. 0, ((u64)sc->cmnd[0] << 32 |
  1276. (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
  1277. (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
  1278. fnic_flags_and_state(sc));
  1279. scsi_done(sc);
  1280. }
  1281. static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
  1282. u32 task_req, u8 *fc_lun,
  1283. struct fnic_io_req *io_req)
  1284. {
  1285. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  1286. struct Scsi_Host *host = fnic->lport->host;
  1287. struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
  1288. unsigned long flags;
  1289. spin_lock_irqsave(host->host_lock, flags);
  1290. if (unlikely(fnic_chk_state_flags_locked(fnic,
  1291. FNIC_FLAGS_IO_BLOCKED))) {
  1292. spin_unlock_irqrestore(host->host_lock, flags);
  1293. return 1;
  1294. } else
  1295. atomic_inc(&fnic->in_flight);
  1296. spin_unlock_irqrestore(host->host_lock, flags);
  1297. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  1298. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  1299. free_wq_copy_descs(fnic, wq);
  1300. if (!vnic_wq_copy_desc_avail(wq)) {
  1301. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  1302. atomic_dec(&fnic->in_flight);
  1303. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1304. "fnic_queue_abort_io_req: failure: no descriptors\n");
  1305. atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
  1306. return 1;
  1307. }
  1308. fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
  1309. 0, task_req, tag, fc_lun, io_req->port_id,
  1310. fnic->config.ra_tov, fnic->config.ed_tov);
  1311. atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  1312. if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
  1313. atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
  1314. atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
  1315. atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
  1316. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  1317. atomic_dec(&fnic->in_flight);
  1318. return 0;
  1319. }
  1320. struct fnic_rport_abort_io_iter_data {
  1321. struct fnic *fnic;
  1322. u32 port_id;
  1323. int term_cnt;
  1324. };
  1325. static bool fnic_rport_abort_io_iter(struct scsi_cmnd *sc, void *data)
  1326. {
  1327. struct fnic_rport_abort_io_iter_data *iter_data = data;
  1328. struct fnic *fnic = iter_data->fnic;
  1329. int abt_tag = scsi_cmd_to_rq(sc)->tag;
  1330. struct fnic_io_req *io_req;
  1331. spinlock_t *io_lock;
  1332. unsigned long flags;
  1333. struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
  1334. struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
  1335. struct scsi_lun fc_lun;
  1336. enum fnic_ioreq_state old_ioreq_state;
  1337. io_lock = fnic_io_lock_tag(fnic, abt_tag);
  1338. spin_lock_irqsave(io_lock, flags);
  1339. io_req = fnic_priv(sc)->io_req;
  1340. if (!io_req || io_req->port_id != iter_data->port_id) {
  1341. spin_unlock_irqrestore(io_lock, flags);
  1342. return true;
  1343. }
  1344. if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
  1345. !(fnic_priv(sc)->flags & FNIC_DEV_RST_ISSUED)) {
  1346. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1347. "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
  1348. sc);
  1349. spin_unlock_irqrestore(io_lock, flags);
  1350. return true;
  1351. }
  1352. /*
  1353. * Found IO that is still pending with firmware and
  1354. * belongs to rport that went away
  1355. */
  1356. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
  1357. spin_unlock_irqrestore(io_lock, flags);
  1358. return true;
  1359. }
  1360. if (io_req->abts_done) {
  1361. shost_printk(KERN_ERR, fnic->lport->host,
  1362. "fnic_rport_exch_reset: io_req->abts_done is set "
  1363. "state is %s\n",
  1364. fnic_ioreq_state_to_str(fnic_priv(sc)->state));
  1365. }
  1366. if (!(fnic_priv(sc)->flags & FNIC_IO_ISSUED)) {
  1367. shost_printk(KERN_ERR, fnic->lport->host,
  1368. "rport_exch_reset "
  1369. "IO not yet issued %p tag 0x%x flags "
  1370. "%x state %d\n",
  1371. sc, abt_tag, fnic_priv(sc)->flags, fnic_priv(sc)->state);
  1372. }
  1373. old_ioreq_state = fnic_priv(sc)->state;
  1374. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
  1375. fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
  1376. if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
  1377. atomic64_inc(&reset_stats->device_reset_terminates);
  1378. abt_tag |= FNIC_TAG_DEV_RST;
  1379. }
  1380. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1381. "fnic_rport_exch_reset dev rst sc 0x%p\n", sc);
  1382. BUG_ON(io_req->abts_done);
  1383. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1384. "fnic_rport_reset_exch: Issuing abts\n");
  1385. spin_unlock_irqrestore(io_lock, flags);
  1386. /* Now queue the abort command to firmware */
  1387. int_to_scsilun(sc->device->lun, &fc_lun);
  1388. if (fnic_queue_abort_io_req(fnic, abt_tag,
  1389. FCPIO_ITMF_ABT_TASK_TERM,
  1390. fc_lun.scsi_lun, io_req)) {
  1391. /*
  1392. * Revert the cmd state back to old state, if
  1393. * it hasn't changed in between. This cmd will get
  1394. * aborted later by scsi_eh, or cleaned up during
  1395. * lun reset
  1396. */
  1397. spin_lock_irqsave(io_lock, flags);
  1398. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
  1399. fnic_priv(sc)->state = old_ioreq_state;
  1400. spin_unlock_irqrestore(io_lock, flags);
  1401. } else {
  1402. spin_lock_irqsave(io_lock, flags);
  1403. if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET)
  1404. fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
  1405. else
  1406. fnic_priv(sc)->flags |= FNIC_IO_INTERNAL_TERM_ISSUED;
  1407. spin_unlock_irqrestore(io_lock, flags);
  1408. atomic64_inc(&term_stats->terminates);
  1409. iter_data->term_cnt++;
  1410. }
  1411. return true;
  1412. }
  1413. static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
  1414. {
  1415. struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
  1416. struct fnic_rport_abort_io_iter_data iter_data = {
  1417. .fnic = fnic,
  1418. .port_id = port_id,
  1419. .term_cnt = 0,
  1420. };
  1421. FNIC_SCSI_DBG(KERN_DEBUG,
  1422. fnic->lport->host,
  1423. "fnic_rport_exch_reset called portid 0x%06x\n",
  1424. port_id);
  1425. if (fnic->in_remove)
  1426. return;
  1427. scsi_host_busy_iter(fnic->lport->host, fnic_rport_abort_io_iter,
  1428. &iter_data);
  1429. if (iter_data.term_cnt > atomic64_read(&term_stats->max_terminates))
  1430. atomic64_set(&term_stats->max_terminates, iter_data.term_cnt);
  1431. }
  1432. void fnic_terminate_rport_io(struct fc_rport *rport)
  1433. {
  1434. struct fc_rport_libfc_priv *rdata;
  1435. struct fc_lport *lport;
  1436. struct fnic *fnic;
  1437. if (!rport) {
  1438. printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
  1439. return;
  1440. }
  1441. rdata = rport->dd_data;
  1442. if (!rdata) {
  1443. printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
  1444. return;
  1445. }
  1446. lport = rdata->local_port;
  1447. if (!lport) {
  1448. printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
  1449. return;
  1450. }
  1451. fnic = lport_priv(lport);
  1452. FNIC_SCSI_DBG(KERN_DEBUG,
  1453. fnic->lport->host, "fnic_terminate_rport_io called"
  1454. " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
  1455. rport->port_name, rport->node_name, rport,
  1456. rport->port_id);
  1457. if (fnic->in_remove)
  1458. return;
  1459. fnic_rport_exch_reset(fnic, rport->port_id);
  1460. }
  1461. /*
  1462. * This function is exported to SCSI for sending abort cmnds.
  1463. * A SCSI IO is represented by a io_req in the driver.
  1464. * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
  1465. */
  1466. int fnic_abort_cmd(struct scsi_cmnd *sc)
  1467. {
  1468. struct request *const rq = scsi_cmd_to_rq(sc);
  1469. struct fc_lport *lp;
  1470. struct fnic *fnic;
  1471. struct fnic_io_req *io_req = NULL;
  1472. struct fc_rport *rport;
  1473. spinlock_t *io_lock;
  1474. unsigned long flags;
  1475. unsigned long start_time = 0;
  1476. int ret = SUCCESS;
  1477. u32 task_req = 0;
  1478. struct scsi_lun fc_lun;
  1479. struct fnic_stats *fnic_stats;
  1480. struct abort_stats *abts_stats;
  1481. struct terminate_stats *term_stats;
  1482. enum fnic_ioreq_state old_ioreq_state;
  1483. const int tag = rq->tag;
  1484. unsigned long abt_issued_time;
  1485. DECLARE_COMPLETION_ONSTACK(tm_done);
  1486. /* Wait for rport to unblock */
  1487. fc_block_scsi_eh(sc);
  1488. /* Get local-port, check ready and link up */
  1489. lp = shost_priv(sc->device->host);
  1490. fnic = lport_priv(lp);
  1491. fnic_stats = &fnic->fnic_stats;
  1492. abts_stats = &fnic->fnic_stats.abts_stats;
  1493. term_stats = &fnic->fnic_stats.term_stats;
  1494. rport = starget_to_rport(scsi_target(sc->device));
  1495. FNIC_SCSI_DBG(KERN_DEBUG,
  1496. fnic->lport->host,
  1497. "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
  1498. rport->port_id, sc->device->lun, tag, fnic_priv(sc)->flags);
  1499. fnic_priv(sc)->flags = FNIC_NO_FLAGS;
  1500. if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
  1501. ret = FAILED;
  1502. goto fnic_abort_cmd_end;
  1503. }
  1504. /*
  1505. * Avoid a race between SCSI issuing the abort and the device
  1506. * completing the command.
  1507. *
  1508. * If the command is already completed by the fw cmpl code,
  1509. * we just return SUCCESS from here. This means that the abort
  1510. * succeeded. In the SCSI ML, since the timeout for command has
  1511. * happened, the completion wont actually complete the command
  1512. * and it will be considered as an aborted command
  1513. *
  1514. * .io_req will not be cleared except while holding io_req_lock.
  1515. */
  1516. io_lock = fnic_io_lock_hash(fnic, sc);
  1517. spin_lock_irqsave(io_lock, flags);
  1518. io_req = fnic_priv(sc)->io_req;
  1519. if (!io_req) {
  1520. spin_unlock_irqrestore(io_lock, flags);
  1521. goto fnic_abort_cmd_end;
  1522. }
  1523. io_req->abts_done = &tm_done;
  1524. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
  1525. spin_unlock_irqrestore(io_lock, flags);
  1526. goto wait_pending;
  1527. }
  1528. abt_issued_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
  1529. if (abt_issued_time <= 6000)
  1530. atomic64_inc(&abts_stats->abort_issued_btw_0_to_6_sec);
  1531. else if (abt_issued_time > 6000 && abt_issued_time <= 20000)
  1532. atomic64_inc(&abts_stats->abort_issued_btw_6_to_20_sec);
  1533. else if (abt_issued_time > 20000 && abt_issued_time <= 30000)
  1534. atomic64_inc(&abts_stats->abort_issued_btw_20_to_30_sec);
  1535. else if (abt_issued_time > 30000 && abt_issued_time <= 40000)
  1536. atomic64_inc(&abts_stats->abort_issued_btw_30_to_40_sec);
  1537. else if (abt_issued_time > 40000 && abt_issued_time <= 50000)
  1538. atomic64_inc(&abts_stats->abort_issued_btw_40_to_50_sec);
  1539. else if (abt_issued_time > 50000 && abt_issued_time <= 60000)
  1540. atomic64_inc(&abts_stats->abort_issued_btw_50_to_60_sec);
  1541. else
  1542. atomic64_inc(&abts_stats->abort_issued_greater_than_60_sec);
  1543. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  1544. "CBD Opcode: %02x Abort issued time: %lu msec\n", sc->cmnd[0], abt_issued_time);
  1545. /*
  1546. * Command is still pending, need to abort it
  1547. * If the firmware completes the command after this point,
  1548. * the completion wont be done till mid-layer, since abort
  1549. * has already started.
  1550. */
  1551. old_ioreq_state = fnic_priv(sc)->state;
  1552. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
  1553. fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
  1554. spin_unlock_irqrestore(io_lock, flags);
  1555. /*
  1556. * Check readiness of the remote port. If the path to remote
  1557. * port is up, then send abts to the remote port to terminate
  1558. * the IO. Else, just locally terminate the IO in the firmware
  1559. */
  1560. if (fc_remote_port_chkready(rport) == 0)
  1561. task_req = FCPIO_ITMF_ABT_TASK;
  1562. else {
  1563. atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
  1564. task_req = FCPIO_ITMF_ABT_TASK_TERM;
  1565. }
  1566. /* Now queue the abort command to firmware */
  1567. int_to_scsilun(sc->device->lun, &fc_lun);
  1568. if (fnic_queue_abort_io_req(fnic, tag, task_req, fc_lun.scsi_lun,
  1569. io_req)) {
  1570. spin_lock_irqsave(io_lock, flags);
  1571. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
  1572. fnic_priv(sc)->state = old_ioreq_state;
  1573. io_req = fnic_priv(sc)->io_req;
  1574. if (io_req)
  1575. io_req->abts_done = NULL;
  1576. spin_unlock_irqrestore(io_lock, flags);
  1577. ret = FAILED;
  1578. goto fnic_abort_cmd_end;
  1579. }
  1580. if (task_req == FCPIO_ITMF_ABT_TASK) {
  1581. fnic_priv(sc)->flags |= FNIC_IO_ABTS_ISSUED;
  1582. atomic64_inc(&fnic_stats->abts_stats.aborts);
  1583. } else {
  1584. fnic_priv(sc)->flags |= FNIC_IO_TERM_ISSUED;
  1585. atomic64_inc(&fnic_stats->term_stats.terminates);
  1586. }
  1587. /*
  1588. * We queued an abort IO, wait for its completion.
  1589. * Once the firmware completes the abort command, it will
  1590. * wake up this thread.
  1591. */
  1592. wait_pending:
  1593. wait_for_completion_timeout(&tm_done,
  1594. msecs_to_jiffies
  1595. (2 * fnic->config.ra_tov +
  1596. fnic->config.ed_tov));
  1597. /* Check the abort status */
  1598. spin_lock_irqsave(io_lock, flags);
  1599. io_req = fnic_priv(sc)->io_req;
  1600. if (!io_req) {
  1601. atomic64_inc(&fnic_stats->io_stats.ioreq_null);
  1602. spin_unlock_irqrestore(io_lock, flags);
  1603. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
  1604. ret = FAILED;
  1605. goto fnic_abort_cmd_end;
  1606. }
  1607. io_req->abts_done = NULL;
  1608. /* fw did not complete abort, timed out */
  1609. if (fnic_priv(sc)->abts_status == FCPIO_INVALID_CODE) {
  1610. spin_unlock_irqrestore(io_lock, flags);
  1611. if (task_req == FCPIO_ITMF_ABT_TASK) {
  1612. atomic64_inc(&abts_stats->abort_drv_timeouts);
  1613. } else {
  1614. atomic64_inc(&term_stats->terminate_drv_timeouts);
  1615. }
  1616. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_TIMED_OUT;
  1617. ret = FAILED;
  1618. goto fnic_abort_cmd_end;
  1619. }
  1620. /* IO out of order */
  1621. if (!(fnic_priv(sc)->flags & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
  1622. spin_unlock_irqrestore(io_lock, flags);
  1623. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1624. "Issuing Host reset due to out of order IO\n");
  1625. ret = FAILED;
  1626. goto fnic_abort_cmd_end;
  1627. }
  1628. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
  1629. start_time = io_req->start_time;
  1630. /*
  1631. * firmware completed the abort, check the status,
  1632. * free the io_req if successful. If abort fails,
  1633. * Device reset will clean the I/O.
  1634. */
  1635. if (fnic_priv(sc)->abts_status == FCPIO_SUCCESS) {
  1636. fnic_priv(sc)->io_req = NULL;
  1637. } else {
  1638. ret = FAILED;
  1639. spin_unlock_irqrestore(io_lock, flags);
  1640. goto fnic_abort_cmd_end;
  1641. }
  1642. spin_unlock_irqrestore(io_lock, flags);
  1643. fnic_release_ioreq_buf(fnic, io_req, sc);
  1644. mempool_free(io_req, fnic->io_req_pool);
  1645. /* Call SCSI completion function to complete the IO */
  1646. sc->result = DID_ABORT << 16;
  1647. scsi_done(sc);
  1648. atomic64_dec(&fnic_stats->io_stats.active_ios);
  1649. if (atomic64_read(&fnic->io_cmpl_skip))
  1650. atomic64_dec(&fnic->io_cmpl_skip);
  1651. else
  1652. atomic64_inc(&fnic_stats->io_stats.io_completions);
  1653. fnic_abort_cmd_end:
  1654. FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no, tag, sc,
  1655. jiffies_to_msecs(jiffies - start_time),
  1656. 0, ((u64)sc->cmnd[0] << 32 |
  1657. (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
  1658. (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
  1659. fnic_flags_and_state(sc));
  1660. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1661. "Returning from abort cmd type %x %s\n", task_req,
  1662. (ret == SUCCESS) ?
  1663. "SUCCESS" : "FAILED");
  1664. return ret;
  1665. }
  1666. static inline int fnic_queue_dr_io_req(struct fnic *fnic,
  1667. struct scsi_cmnd *sc,
  1668. struct fnic_io_req *io_req)
  1669. {
  1670. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  1671. struct Scsi_Host *host = fnic->lport->host;
  1672. struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
  1673. struct scsi_lun fc_lun;
  1674. int ret = 0;
  1675. unsigned long intr_flags;
  1676. spin_lock_irqsave(host->host_lock, intr_flags);
  1677. if (unlikely(fnic_chk_state_flags_locked(fnic,
  1678. FNIC_FLAGS_IO_BLOCKED))) {
  1679. spin_unlock_irqrestore(host->host_lock, intr_flags);
  1680. return FAILED;
  1681. } else
  1682. atomic_inc(&fnic->in_flight);
  1683. spin_unlock_irqrestore(host->host_lock, intr_flags);
  1684. spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
  1685. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  1686. free_wq_copy_descs(fnic, wq);
  1687. if (!vnic_wq_copy_desc_avail(wq)) {
  1688. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1689. "queue_dr_io_req failure - no descriptors\n");
  1690. atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
  1691. ret = -EAGAIN;
  1692. goto lr_io_req_end;
  1693. }
  1694. /* fill in the lun info */
  1695. int_to_scsilun(sc->device->lun, &fc_lun);
  1696. fnic_queue_wq_copy_desc_itmf(wq, scsi_cmd_to_rq(sc)->tag | FNIC_TAG_DEV_RST,
  1697. 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
  1698. fc_lun.scsi_lun, io_req->port_id,
  1699. fnic->config.ra_tov, fnic->config.ed_tov);
  1700. atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
  1701. if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
  1702. atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
  1703. atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
  1704. atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
  1705. lr_io_req_end:
  1706. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  1707. atomic_dec(&fnic->in_flight);
  1708. return ret;
  1709. }
  1710. struct fnic_pending_aborts_iter_data {
  1711. struct fnic *fnic;
  1712. struct scsi_cmnd *lr_sc;
  1713. struct scsi_device *lun_dev;
  1714. int ret;
  1715. };
  1716. static bool fnic_pending_aborts_iter(struct scsi_cmnd *sc, void *data)
  1717. {
  1718. struct fnic_pending_aborts_iter_data *iter_data = data;
  1719. struct fnic *fnic = iter_data->fnic;
  1720. struct scsi_device *lun_dev = iter_data->lun_dev;
  1721. int abt_tag = scsi_cmd_to_rq(sc)->tag;
  1722. struct fnic_io_req *io_req;
  1723. spinlock_t *io_lock;
  1724. unsigned long flags;
  1725. struct scsi_lun fc_lun;
  1726. DECLARE_COMPLETION_ONSTACK(tm_done);
  1727. enum fnic_ioreq_state old_ioreq_state;
  1728. if (sc == iter_data->lr_sc || sc->device != lun_dev)
  1729. return true;
  1730. io_lock = fnic_io_lock_tag(fnic, abt_tag);
  1731. spin_lock_irqsave(io_lock, flags);
  1732. io_req = fnic_priv(sc)->io_req;
  1733. if (!io_req) {
  1734. spin_unlock_irqrestore(io_lock, flags);
  1735. return true;
  1736. }
  1737. /*
  1738. * Found IO that is still pending with firmware and
  1739. * belongs to the LUN that we are resetting
  1740. */
  1741. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1742. "Found IO in %s on lun\n",
  1743. fnic_ioreq_state_to_str(fnic_priv(sc)->state));
  1744. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
  1745. spin_unlock_irqrestore(io_lock, flags);
  1746. return true;
  1747. }
  1748. if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
  1749. (!(fnic_priv(sc)->flags & FNIC_DEV_RST_ISSUED))) {
  1750. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  1751. "%s dev rst not pending sc 0x%p\n", __func__,
  1752. sc);
  1753. spin_unlock_irqrestore(io_lock, flags);
  1754. return true;
  1755. }
  1756. if (io_req->abts_done)
  1757. shost_printk(KERN_ERR, fnic->lport->host,
  1758. "%s: io_req->abts_done is set state is %s\n",
  1759. __func__, fnic_ioreq_state_to_str(fnic_priv(sc)->state));
  1760. old_ioreq_state = fnic_priv(sc)->state;
  1761. /*
  1762. * Any pending IO issued prior to reset is expected to be
  1763. * in abts pending state, if not we need to set
  1764. * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
  1765. * When IO is completed, the IO will be handed over and
  1766. * handled in this function.
  1767. */
  1768. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
  1769. BUG_ON(io_req->abts_done);
  1770. if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
  1771. abt_tag |= FNIC_TAG_DEV_RST;
  1772. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  1773. "%s: dev rst sc 0x%p\n", __func__, sc);
  1774. }
  1775. fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
  1776. io_req->abts_done = &tm_done;
  1777. spin_unlock_irqrestore(io_lock, flags);
  1778. /* Now queue the abort command to firmware */
  1779. int_to_scsilun(sc->device->lun, &fc_lun);
  1780. if (fnic_queue_abort_io_req(fnic, abt_tag,
  1781. FCPIO_ITMF_ABT_TASK_TERM,
  1782. fc_lun.scsi_lun, io_req)) {
  1783. spin_lock_irqsave(io_lock, flags);
  1784. io_req = fnic_priv(sc)->io_req;
  1785. if (io_req)
  1786. io_req->abts_done = NULL;
  1787. if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
  1788. fnic_priv(sc)->state = old_ioreq_state;
  1789. spin_unlock_irqrestore(io_lock, flags);
  1790. iter_data->ret = FAILED;
  1791. return false;
  1792. } else {
  1793. spin_lock_irqsave(io_lock, flags);
  1794. if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET)
  1795. fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
  1796. spin_unlock_irqrestore(io_lock, flags);
  1797. }
  1798. fnic_priv(sc)->flags |= FNIC_IO_INTERNAL_TERM_ISSUED;
  1799. wait_for_completion_timeout(&tm_done, msecs_to_jiffies
  1800. (fnic->config.ed_tov));
  1801. /* Recheck cmd state to check if it is now aborted */
  1802. spin_lock_irqsave(io_lock, flags);
  1803. io_req = fnic_priv(sc)->io_req;
  1804. if (!io_req) {
  1805. spin_unlock_irqrestore(io_lock, flags);
  1806. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
  1807. return true;
  1808. }
  1809. io_req->abts_done = NULL;
  1810. /* if abort is still pending with fw, fail */
  1811. if (fnic_priv(sc)->abts_status == FCPIO_INVALID_CODE) {
  1812. spin_unlock_irqrestore(io_lock, flags);
  1813. fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_DONE;
  1814. iter_data->ret = FAILED;
  1815. return false;
  1816. }
  1817. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
  1818. /* original sc used for lr is handled by dev reset code */
  1819. if (sc != iter_data->lr_sc)
  1820. fnic_priv(sc)->io_req = NULL;
  1821. spin_unlock_irqrestore(io_lock, flags);
  1822. /* original sc used for lr is handled by dev reset code */
  1823. if (sc != iter_data->lr_sc) {
  1824. fnic_release_ioreq_buf(fnic, io_req, sc);
  1825. mempool_free(io_req, fnic->io_req_pool);
  1826. }
  1827. /*
  1828. * Any IO is returned during reset, it needs to call scsi_done
  1829. * to return the scsi_cmnd to upper layer.
  1830. */
  1831. /* Set result to let upper SCSI layer retry */
  1832. sc->result = DID_RESET << 16;
  1833. scsi_done(sc);
  1834. return true;
  1835. }
  1836. /*
  1837. * Clean up any pending aborts on the lun
  1838. * For each outstanding IO on this lun, whose abort is not completed by fw,
  1839. * issue a local abort. Wait for abort to complete. Return 0 if all commands
  1840. * successfully aborted, 1 otherwise
  1841. */
  1842. static int fnic_clean_pending_aborts(struct fnic *fnic,
  1843. struct scsi_cmnd *lr_sc,
  1844. bool new_sc)
  1845. {
  1846. int ret = 0;
  1847. struct fnic_pending_aborts_iter_data iter_data = {
  1848. .fnic = fnic,
  1849. .lun_dev = lr_sc->device,
  1850. .ret = SUCCESS,
  1851. };
  1852. if (new_sc)
  1853. iter_data.lr_sc = lr_sc;
  1854. scsi_host_busy_iter(fnic->lport->host,
  1855. fnic_pending_aborts_iter, &iter_data);
  1856. if (iter_data.ret == FAILED) {
  1857. ret = iter_data.ret;
  1858. goto clean_pending_aborts_end;
  1859. }
  1860. schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
  1861. /* walk again to check, if IOs are still pending in fw */
  1862. if (fnic_is_abts_pending(fnic, lr_sc))
  1863. ret = 1;
  1864. clean_pending_aborts_end:
  1865. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  1866. "%s: exit status: %d\n", __func__, ret);
  1867. return ret;
  1868. }
  1869. /*
  1870. * fnic_scsi_host_start_tag
  1871. * Allocates tagid from host's tag list
  1872. **/
  1873. static inline int
  1874. fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
  1875. {
  1876. struct request *rq = scsi_cmd_to_rq(sc);
  1877. struct request_queue *q = rq->q;
  1878. struct request *dummy;
  1879. dummy = blk_mq_alloc_request(q, REQ_OP_WRITE, BLK_MQ_REQ_NOWAIT);
  1880. if (IS_ERR(dummy))
  1881. return SCSI_NO_TAG;
  1882. rq->tag = dummy->tag;
  1883. sc->host_scribble = (unsigned char *)dummy;
  1884. return dummy->tag;
  1885. }
  1886. /*
  1887. * fnic_scsi_host_end_tag
  1888. * frees tag allocated by fnic_scsi_host_start_tag.
  1889. **/
  1890. static inline void
  1891. fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
  1892. {
  1893. struct request *dummy = (struct request *)sc->host_scribble;
  1894. blk_mq_free_request(dummy);
  1895. }
  1896. /*
  1897. * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
  1898. * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
  1899. * on the LUN.
  1900. */
  1901. int fnic_device_reset(struct scsi_cmnd *sc)
  1902. {
  1903. struct request *rq = scsi_cmd_to_rq(sc);
  1904. struct fc_lport *lp;
  1905. struct fnic *fnic;
  1906. struct fnic_io_req *io_req = NULL;
  1907. struct fc_rport *rport;
  1908. int status;
  1909. int ret = FAILED;
  1910. spinlock_t *io_lock;
  1911. unsigned long flags;
  1912. unsigned long start_time = 0;
  1913. struct scsi_lun fc_lun;
  1914. struct fnic_stats *fnic_stats;
  1915. struct reset_stats *reset_stats;
  1916. int tag = rq->tag;
  1917. DECLARE_COMPLETION_ONSTACK(tm_done);
  1918. int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
  1919. bool new_sc = 0;
  1920. /* Wait for rport to unblock */
  1921. fc_block_scsi_eh(sc);
  1922. /* Get local-port, check ready and link up */
  1923. lp = shost_priv(sc->device->host);
  1924. fnic = lport_priv(lp);
  1925. fnic_stats = &fnic->fnic_stats;
  1926. reset_stats = &fnic->fnic_stats.reset_stats;
  1927. atomic64_inc(&reset_stats->device_resets);
  1928. rport = starget_to_rport(scsi_target(sc->device));
  1929. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1930. "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
  1931. rport->port_id, sc->device->lun, sc);
  1932. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  1933. goto fnic_device_reset_end;
  1934. /* Check if remote port up */
  1935. if (fc_remote_port_chkready(rport)) {
  1936. atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
  1937. goto fnic_device_reset_end;
  1938. }
  1939. fnic_priv(sc)->flags = FNIC_DEVICE_RESET;
  1940. /* Allocate tag if not present */
  1941. if (unlikely(tag < 0)) {
  1942. /*
  1943. * Really should fix the midlayer to pass in a proper
  1944. * request for ioctls...
  1945. */
  1946. tag = fnic_scsi_host_start_tag(fnic, sc);
  1947. if (unlikely(tag == SCSI_NO_TAG))
  1948. goto fnic_device_reset_end;
  1949. tag_gen_flag = 1;
  1950. new_sc = 1;
  1951. }
  1952. io_lock = fnic_io_lock_hash(fnic, sc);
  1953. spin_lock_irqsave(io_lock, flags);
  1954. io_req = fnic_priv(sc)->io_req;
  1955. /*
  1956. * If there is a io_req attached to this command, then use it,
  1957. * else allocate a new one.
  1958. */
  1959. if (!io_req) {
  1960. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  1961. if (!io_req) {
  1962. spin_unlock_irqrestore(io_lock, flags);
  1963. goto fnic_device_reset_end;
  1964. }
  1965. memset(io_req, 0, sizeof(*io_req));
  1966. io_req->port_id = rport->port_id;
  1967. fnic_priv(sc)->io_req = io_req;
  1968. }
  1969. io_req->dr_done = &tm_done;
  1970. fnic_priv(sc)->state = FNIC_IOREQ_CMD_PENDING;
  1971. fnic_priv(sc)->lr_status = FCPIO_INVALID_CODE;
  1972. spin_unlock_irqrestore(io_lock, flags);
  1973. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
  1974. /*
  1975. * issue the device reset, if enqueue failed, clean up the ioreq
  1976. * and break assoc with scsi cmd
  1977. */
  1978. if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
  1979. spin_lock_irqsave(io_lock, flags);
  1980. io_req = fnic_priv(sc)->io_req;
  1981. if (io_req)
  1982. io_req->dr_done = NULL;
  1983. goto fnic_device_reset_clean;
  1984. }
  1985. spin_lock_irqsave(io_lock, flags);
  1986. fnic_priv(sc)->flags |= FNIC_DEV_RST_ISSUED;
  1987. spin_unlock_irqrestore(io_lock, flags);
  1988. /*
  1989. * Wait on the local completion for LUN reset. The io_req may be
  1990. * freed while we wait since we hold no lock.
  1991. */
  1992. wait_for_completion_timeout(&tm_done,
  1993. msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
  1994. spin_lock_irqsave(io_lock, flags);
  1995. io_req = fnic_priv(sc)->io_req;
  1996. if (!io_req) {
  1997. spin_unlock_irqrestore(io_lock, flags);
  1998. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1999. "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
  2000. goto fnic_device_reset_end;
  2001. }
  2002. io_req->dr_done = NULL;
  2003. status = fnic_priv(sc)->lr_status;
  2004. /*
  2005. * If lun reset not completed, bail out with failed. io_req
  2006. * gets cleaned up during higher levels of EH
  2007. */
  2008. if (status == FCPIO_INVALID_CODE) {
  2009. atomic64_inc(&reset_stats->device_reset_timeouts);
  2010. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2011. "Device reset timed out\n");
  2012. fnic_priv(sc)->flags |= FNIC_DEV_RST_TIMED_OUT;
  2013. spin_unlock_irqrestore(io_lock, flags);
  2014. int_to_scsilun(sc->device->lun, &fc_lun);
  2015. /*
  2016. * Issue abort and terminate on device reset request.
  2017. * If q'ing of terminate fails, retry it after a delay.
  2018. */
  2019. while (1) {
  2020. spin_lock_irqsave(io_lock, flags);
  2021. if (fnic_priv(sc)->flags & FNIC_DEV_RST_TERM_ISSUED) {
  2022. spin_unlock_irqrestore(io_lock, flags);
  2023. break;
  2024. }
  2025. spin_unlock_irqrestore(io_lock, flags);
  2026. if (fnic_queue_abort_io_req(fnic,
  2027. tag | FNIC_TAG_DEV_RST,
  2028. FCPIO_ITMF_ABT_TASK_TERM,
  2029. fc_lun.scsi_lun, io_req)) {
  2030. wait_for_completion_timeout(&tm_done,
  2031. msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
  2032. } else {
  2033. spin_lock_irqsave(io_lock, flags);
  2034. fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
  2035. fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
  2036. io_req->abts_done = &tm_done;
  2037. spin_unlock_irqrestore(io_lock, flags);
  2038. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2039. "Abort and terminate issued on Device reset "
  2040. "tag 0x%x sc 0x%p\n", tag, sc);
  2041. break;
  2042. }
  2043. }
  2044. while (1) {
  2045. spin_lock_irqsave(io_lock, flags);
  2046. if (!(fnic_priv(sc)->flags & FNIC_DEV_RST_DONE)) {
  2047. spin_unlock_irqrestore(io_lock, flags);
  2048. wait_for_completion_timeout(&tm_done,
  2049. msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
  2050. break;
  2051. } else {
  2052. io_req = fnic_priv(sc)->io_req;
  2053. io_req->abts_done = NULL;
  2054. goto fnic_device_reset_clean;
  2055. }
  2056. }
  2057. } else {
  2058. spin_unlock_irqrestore(io_lock, flags);
  2059. }
  2060. /* Completed, but not successful, clean up the io_req, return fail */
  2061. if (status != FCPIO_SUCCESS) {
  2062. spin_lock_irqsave(io_lock, flags);
  2063. FNIC_SCSI_DBG(KERN_DEBUG,
  2064. fnic->lport->host,
  2065. "Device reset completed - failed\n");
  2066. io_req = fnic_priv(sc)->io_req;
  2067. goto fnic_device_reset_clean;
  2068. }
  2069. /*
  2070. * Clean up any aborts on this lun that have still not
  2071. * completed. If any of these fail, then LUN reset fails.
  2072. * clean_pending_aborts cleans all cmds on this lun except
  2073. * the lun reset cmd. If all cmds get cleaned, the lun reset
  2074. * succeeds
  2075. */
  2076. if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
  2077. spin_lock_irqsave(io_lock, flags);
  2078. io_req = fnic_priv(sc)->io_req;
  2079. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2080. "Device reset failed"
  2081. " since could not abort all IOs\n");
  2082. goto fnic_device_reset_clean;
  2083. }
  2084. /* Clean lun reset command */
  2085. spin_lock_irqsave(io_lock, flags);
  2086. io_req = fnic_priv(sc)->io_req;
  2087. if (io_req)
  2088. /* Completed, and successful */
  2089. ret = SUCCESS;
  2090. fnic_device_reset_clean:
  2091. if (io_req)
  2092. fnic_priv(sc)->io_req = NULL;
  2093. spin_unlock_irqrestore(io_lock, flags);
  2094. if (io_req) {
  2095. start_time = io_req->start_time;
  2096. fnic_release_ioreq_buf(fnic, io_req, sc);
  2097. mempool_free(io_req, fnic->io_req_pool);
  2098. }
  2099. fnic_device_reset_end:
  2100. FNIC_TRACE(fnic_device_reset, sc->device->host->host_no, rq->tag, sc,
  2101. jiffies_to_msecs(jiffies - start_time),
  2102. 0, ((u64)sc->cmnd[0] << 32 |
  2103. (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
  2104. (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
  2105. fnic_flags_and_state(sc));
  2106. /* free tag if it is allocated */
  2107. if (unlikely(tag_gen_flag))
  2108. fnic_scsi_host_end_tag(fnic, sc);
  2109. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2110. "Returning from device reset %s\n",
  2111. (ret == SUCCESS) ?
  2112. "SUCCESS" : "FAILED");
  2113. if (ret == FAILED)
  2114. atomic64_inc(&reset_stats->device_reset_failures);
  2115. return ret;
  2116. }
  2117. /* Clean up all IOs, clean up libFC local port */
  2118. int fnic_reset(struct Scsi_Host *shost)
  2119. {
  2120. struct fc_lport *lp;
  2121. struct fnic *fnic;
  2122. int ret = 0;
  2123. struct reset_stats *reset_stats;
  2124. lp = shost_priv(shost);
  2125. fnic = lport_priv(lp);
  2126. reset_stats = &fnic->fnic_stats.reset_stats;
  2127. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2128. "fnic_reset called\n");
  2129. atomic64_inc(&reset_stats->fnic_resets);
  2130. /*
  2131. * Reset local port, this will clean up libFC exchanges,
  2132. * reset remote port sessions, and if link is up, begin flogi
  2133. */
  2134. ret = fc_lport_reset(lp);
  2135. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2136. "Returning from fnic reset %s\n",
  2137. (ret == 0) ?
  2138. "SUCCESS" : "FAILED");
  2139. if (ret == 0)
  2140. atomic64_inc(&reset_stats->fnic_reset_completions);
  2141. else
  2142. atomic64_inc(&reset_stats->fnic_reset_failures);
  2143. return ret;
  2144. }
  2145. /*
  2146. * SCSI Error handling calls driver's eh_host_reset if all prior
  2147. * error handling levels return FAILED. If host reset completes
  2148. * successfully, and if link is up, then Fabric login begins.
  2149. *
  2150. * Host Reset is the highest level of error recovery. If this fails, then
  2151. * host is offlined by SCSI.
  2152. *
  2153. */
  2154. int fnic_host_reset(struct scsi_cmnd *sc)
  2155. {
  2156. int ret;
  2157. unsigned long wait_host_tmo;
  2158. struct Scsi_Host *shost = sc->device->host;
  2159. struct fc_lport *lp = shost_priv(shost);
  2160. struct fnic *fnic = lport_priv(lp);
  2161. unsigned long flags;
  2162. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2163. if (!fnic->internal_reset_inprogress) {
  2164. fnic->internal_reset_inprogress = true;
  2165. } else {
  2166. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2167. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2168. "host reset in progress skipping another host reset\n");
  2169. return SUCCESS;
  2170. }
  2171. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2172. /*
  2173. * If fnic_reset is successful, wait for fabric login to complete
  2174. * scsi-ml tries to send a TUR to every device if host reset is
  2175. * successful, so before returning to scsi, fabric should be up
  2176. */
  2177. ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
  2178. if (ret == SUCCESS) {
  2179. wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
  2180. ret = FAILED;
  2181. while (time_before(jiffies, wait_host_tmo)) {
  2182. if ((lp->state == LPORT_ST_READY) &&
  2183. (lp->link_up)) {
  2184. ret = SUCCESS;
  2185. break;
  2186. }
  2187. ssleep(1);
  2188. }
  2189. }
  2190. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2191. fnic->internal_reset_inprogress = false;
  2192. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2193. return ret;
  2194. }
  2195. /*
  2196. * This fxn is called from libFC when host is removed
  2197. */
  2198. void fnic_scsi_abort_io(struct fc_lport *lp)
  2199. {
  2200. int err = 0;
  2201. unsigned long flags;
  2202. enum fnic_state old_state;
  2203. struct fnic *fnic = lport_priv(lp);
  2204. DECLARE_COMPLETION_ONSTACK(remove_wait);
  2205. /* Issue firmware reset for fnic, wait for reset to complete */
  2206. retry_fw_reset:
  2207. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2208. if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) &&
  2209. fnic->link_events) {
  2210. /* fw reset is in progress, poll for its completion */
  2211. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2212. schedule_timeout(msecs_to_jiffies(100));
  2213. goto retry_fw_reset;
  2214. }
  2215. fnic->remove_wait = &remove_wait;
  2216. old_state = fnic->state;
  2217. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  2218. fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
  2219. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2220. err = fnic_fw_reset_handler(fnic);
  2221. if (err) {
  2222. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2223. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  2224. fnic->state = old_state;
  2225. fnic->remove_wait = NULL;
  2226. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2227. return;
  2228. }
  2229. /* Wait for firmware reset to complete */
  2230. wait_for_completion_timeout(&remove_wait,
  2231. msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
  2232. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2233. fnic->remove_wait = NULL;
  2234. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  2235. "fnic_scsi_abort_io %s\n",
  2236. (fnic->state == FNIC_IN_ETH_MODE) ?
  2237. "SUCCESS" : "FAILED");
  2238. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2239. }
  2240. /*
  2241. * This fxn called from libFC to clean up driver IO state on link down
  2242. */
  2243. void fnic_scsi_cleanup(struct fc_lport *lp)
  2244. {
  2245. unsigned long flags;
  2246. enum fnic_state old_state;
  2247. struct fnic *fnic = lport_priv(lp);
  2248. /* issue fw reset */
  2249. retry_fw_reset:
  2250. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2251. if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
  2252. /* fw reset is in progress, poll for its completion */
  2253. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2254. schedule_timeout(msecs_to_jiffies(100));
  2255. goto retry_fw_reset;
  2256. }
  2257. old_state = fnic->state;
  2258. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  2259. fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
  2260. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2261. if (fnic_fw_reset_handler(fnic)) {
  2262. spin_lock_irqsave(&fnic->fnic_lock, flags);
  2263. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  2264. fnic->state = old_state;
  2265. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  2266. }
  2267. }
  2268. void fnic_empty_scsi_cleanup(struct fc_lport *lp)
  2269. {
  2270. }
  2271. void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
  2272. {
  2273. struct fnic *fnic = lport_priv(lp);
  2274. /* Non-zero sid, nothing to do */
  2275. if (sid)
  2276. goto call_fc_exch_mgr_reset;
  2277. if (did) {
  2278. fnic_rport_exch_reset(fnic, did);
  2279. goto call_fc_exch_mgr_reset;
  2280. }
  2281. /*
  2282. * sid = 0, did = 0
  2283. * link down or device being removed
  2284. */
  2285. if (!fnic->in_remove)
  2286. fnic_scsi_cleanup(lp);
  2287. else
  2288. fnic_scsi_abort_io(lp);
  2289. /* call libFC exch mgr reset to reset its exchanges */
  2290. call_fc_exch_mgr_reset:
  2291. fc_exch_mgr_reset(lp, sid, did);
  2292. }
  2293. static bool fnic_abts_pending_iter(struct scsi_cmnd *sc, void *data)
  2294. {
  2295. struct fnic_pending_aborts_iter_data *iter_data = data;
  2296. struct fnic *fnic = iter_data->fnic;
  2297. int cmd_state;
  2298. struct fnic_io_req *io_req;
  2299. spinlock_t *io_lock;
  2300. unsigned long flags;
  2301. /*
  2302. * ignore this lun reset cmd or cmds that do not belong to
  2303. * this lun
  2304. */
  2305. if (iter_data->lr_sc && sc == iter_data->lr_sc)
  2306. return true;
  2307. if (iter_data->lun_dev && sc->device != iter_data->lun_dev)
  2308. return true;
  2309. io_lock = fnic_io_lock_hash(fnic, sc);
  2310. spin_lock_irqsave(io_lock, flags);
  2311. io_req = fnic_priv(sc)->io_req;
  2312. if (!io_req) {
  2313. spin_unlock_irqrestore(io_lock, flags);
  2314. return true;
  2315. }
  2316. /*
  2317. * Found IO that is still pending with firmware and
  2318. * belongs to the LUN that we are resetting
  2319. */
  2320. FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
  2321. "Found IO in %s on lun\n",
  2322. fnic_ioreq_state_to_str(fnic_priv(sc)->state));
  2323. cmd_state = fnic_priv(sc)->state;
  2324. spin_unlock_irqrestore(io_lock, flags);
  2325. if (cmd_state == FNIC_IOREQ_ABTS_PENDING)
  2326. iter_data->ret = 1;
  2327. return iter_data->ret ? false : true;
  2328. }
  2329. /*
  2330. * fnic_is_abts_pending() is a helper function that
  2331. * walks through tag map to check if there is any IOs pending,if there is one,
  2332. * then it returns 1 (true), otherwise 0 (false)
  2333. * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
  2334. * otherwise, it checks for all IOs.
  2335. */
  2336. int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
  2337. {
  2338. struct fnic_pending_aborts_iter_data iter_data = {
  2339. .fnic = fnic,
  2340. .lun_dev = NULL,
  2341. .ret = 0,
  2342. };
  2343. if (lr_sc) {
  2344. iter_data.lun_dev = lr_sc->device;
  2345. iter_data.lr_sc = lr_sc;
  2346. }
  2347. /* walk again to check, if IOs are still pending in fw */
  2348. scsi_host_busy_iter(fnic->lport->host,
  2349. fnic_abts_pending_iter, &iter_data);
  2350. return iter_data.ret;
  2351. }