fnic_scsi.c 78 KB

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