fnic_scsi.c 66 KB

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