fnic_scsi.c 63 KB

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