fnic_scsi.c 76 KB

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