fnic_scsi.c 79 KB

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