fnic_scsi.c 66 KB

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