fnic_scsi.c 78 KB

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