fnic_scsi.c 78 KB

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