fnic_scsi.c 77 KB

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