fnic_scsi.c 75 KB

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