fnic_scsi.c 67 KB

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