fnic_scsi.c 81 KB

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