fnic_scsi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  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 <scsi/scsi.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/fc/fc_els.h>
  35. #include <scsi/fc/fc_fcoe.h>
  36. #include <scsi/libfc.h>
  37. #include <scsi/fc_frame.h>
  38. #include "fnic_io.h"
  39. #include "fnic.h"
  40. const char *fnic_state_str[] = {
  41. [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
  42. [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
  43. [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
  44. [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
  45. };
  46. static const char *fnic_ioreq_state_str[] = {
  47. [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
  48. [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
  49. [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
  50. [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
  51. };
  52. static const char *fcpio_status_str[] = {
  53. [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
  54. [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
  55. [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
  56. [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
  57. [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
  58. [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
  59. [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
  60. [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
  61. [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
  62. [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
  63. [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
  64. [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
  65. [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
  66. [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
  67. [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
  68. [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
  69. [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
  70. [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
  71. [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
  72. };
  73. const char *fnic_state_to_str(unsigned int state)
  74. {
  75. if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
  76. return "unknown";
  77. return fnic_state_str[state];
  78. }
  79. static const char *fnic_ioreq_state_to_str(unsigned int state)
  80. {
  81. if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
  82. !fnic_ioreq_state_str[state])
  83. return "unknown";
  84. return fnic_ioreq_state_str[state];
  85. }
  86. static const char *fnic_fcpio_status_to_str(unsigned int status)
  87. {
  88. if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
  89. return "unknown";
  90. return fcpio_status_str[status];
  91. }
  92. static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
  93. static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
  94. struct scsi_cmnd *sc)
  95. {
  96. u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
  97. return &fnic->io_req_lock[hash];
  98. }
  99. /*
  100. * Unmap the data buffer and sense buffer for an io_req,
  101. * also unmap and free the device-private scatter/gather list.
  102. */
  103. static void fnic_release_ioreq_buf(struct fnic *fnic,
  104. struct fnic_io_req *io_req,
  105. struct scsi_cmnd *sc)
  106. {
  107. if (io_req->sgl_list_pa)
  108. pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
  109. sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
  110. PCI_DMA_TODEVICE);
  111. scsi_dma_unmap(sc);
  112. if (io_req->sgl_cnt)
  113. mempool_free(io_req->sgl_list_alloc,
  114. fnic->io_sgl_pool[io_req->sgl_type]);
  115. if (io_req->sense_buf_pa)
  116. pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
  117. SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
  118. }
  119. /* Free up Copy Wq descriptors. Called with copy_wq lock held */
  120. static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
  121. {
  122. /* if no Ack received from firmware, then nothing to clean */
  123. if (!fnic->fw_ack_recd[0])
  124. return 1;
  125. /*
  126. * Update desc_available count based on number of freed descriptors
  127. * Account for wraparound
  128. */
  129. if (wq->to_clean_index <= fnic->fw_ack_index[0])
  130. wq->ring.desc_avail += (fnic->fw_ack_index[0]
  131. - wq->to_clean_index + 1);
  132. else
  133. wq->ring.desc_avail += (wq->ring.desc_count
  134. - wq->to_clean_index
  135. + fnic->fw_ack_index[0] + 1);
  136. /*
  137. * just bump clean index to ack_index+1 accounting for wraparound
  138. * this will essentially free up all descriptors between
  139. * to_clean_index and fw_ack_index, both inclusive
  140. */
  141. wq->to_clean_index =
  142. (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
  143. /* we have processed the acks received so far */
  144. fnic->fw_ack_recd[0] = 0;
  145. return 0;
  146. }
  147. /*
  148. * fnic_fw_reset_handler
  149. * Routine to send reset msg to fw
  150. */
  151. int fnic_fw_reset_handler(struct fnic *fnic)
  152. {
  153. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  154. int ret = 0;
  155. unsigned long flags;
  156. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  157. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  158. free_wq_copy_descs(fnic, wq);
  159. if (!vnic_wq_copy_desc_avail(wq))
  160. ret = -EAGAIN;
  161. else
  162. fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
  163. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  164. if (!ret)
  165. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  166. "Issued fw reset\n");
  167. else
  168. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  169. "Failed to issue fw reset\n");
  170. return ret;
  171. }
  172. /*
  173. * fnic_flogi_reg_handler
  174. * Routine to send flogi register msg to fw
  175. */
  176. int fnic_flogi_reg_handler(struct fnic *fnic)
  177. {
  178. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  179. u8 gw_mac[ETH_ALEN];
  180. int ret = 0;
  181. unsigned long flags;
  182. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  183. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  184. free_wq_copy_descs(fnic, wq);
  185. if (!vnic_wq_copy_desc_avail(wq)) {
  186. ret = -EAGAIN;
  187. goto flogi_reg_ioreq_end;
  188. }
  189. if (fnic->fcoui_mode)
  190. memset(gw_mac, 0xff, ETH_ALEN);
  191. else
  192. memcpy(gw_mac, fnic->dest_addr, ETH_ALEN);
  193. fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
  194. FCPIO_FLOGI_REG_GW_DEST,
  195. fnic->s_id,
  196. gw_mac);
  197. flogi_reg_ioreq_end:
  198. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  199. if (!ret)
  200. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  201. "flog reg issued\n");
  202. return ret;
  203. }
  204. /*
  205. * fnic_queue_wq_copy_desc
  206. * Routine to enqueue a wq copy desc
  207. */
  208. static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
  209. struct vnic_wq_copy *wq,
  210. struct fnic_io_req *io_req,
  211. struct scsi_cmnd *sc,
  212. int sg_count)
  213. {
  214. struct scatterlist *sg;
  215. struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
  216. struct fc_rport_libfc_priv *rp = rport->dd_data;
  217. struct host_sg_desc *desc;
  218. u8 pri_tag = 0;
  219. unsigned int i;
  220. unsigned long intr_flags;
  221. int flags;
  222. u8 exch_flags;
  223. struct scsi_lun fc_lun;
  224. char msg[2];
  225. if (sg_count) {
  226. /* For each SGE, create a device desc entry */
  227. desc = io_req->sgl_list;
  228. for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
  229. desc->addr = cpu_to_le64(sg_dma_address(sg));
  230. desc->len = cpu_to_le32(sg_dma_len(sg));
  231. desc->_resvd = 0;
  232. desc++;
  233. }
  234. io_req->sgl_list_pa = pci_map_single
  235. (fnic->pdev,
  236. io_req->sgl_list,
  237. sizeof(io_req->sgl_list[0]) * sg_count,
  238. PCI_DMA_TODEVICE);
  239. }
  240. io_req->sense_buf_pa = pci_map_single(fnic->pdev,
  241. sc->sense_buffer,
  242. SCSI_SENSE_BUFFERSIZE,
  243. PCI_DMA_FROMDEVICE);
  244. int_to_scsilun(sc->device->lun, &fc_lun);
  245. pri_tag = FCPIO_ICMND_PTA_SIMPLE;
  246. msg[0] = MSG_SIMPLE_TAG;
  247. scsi_populate_tag_msg(sc, msg);
  248. if (msg[0] == MSG_ORDERED_TAG)
  249. pri_tag = FCPIO_ICMND_PTA_ORDERED;
  250. /* Enqueue the descriptor in the Copy WQ */
  251. spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
  252. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  253. free_wq_copy_descs(fnic, wq);
  254. if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
  255. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  256. return SCSI_MLQUEUE_HOST_BUSY;
  257. }
  258. flags = 0;
  259. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  260. flags = FCPIO_ICMND_RDDATA;
  261. else if (sc->sc_data_direction == DMA_TO_DEVICE)
  262. flags = FCPIO_ICMND_WRDATA;
  263. exch_flags = 0;
  264. if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
  265. (rp->flags & FC_RP_FLAGS_RETRY))
  266. exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
  267. fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
  268. 0, exch_flags, io_req->sgl_cnt,
  269. SCSI_SENSE_BUFFERSIZE,
  270. io_req->sgl_list_pa,
  271. io_req->sense_buf_pa,
  272. 0, /* scsi cmd ref, always 0 */
  273. pri_tag, /* scsi pri and tag */
  274. flags, /* command flags */
  275. sc->cmnd, sc->cmd_len,
  276. scsi_bufflen(sc),
  277. fc_lun.scsi_lun, io_req->port_id,
  278. rport->maxframe_size, rp->r_a_tov,
  279. rp->e_d_tov);
  280. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  281. return 0;
  282. }
  283. /*
  284. * fnic_queuecommand
  285. * Routine to send a scsi cdb
  286. * Called with host_lock held and interrupts disabled.
  287. */
  288. int fnic_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  289. {
  290. struct fc_lport *lp;
  291. struct fc_rport *rport;
  292. struct fnic_io_req *io_req;
  293. struct fnic *fnic;
  294. struct vnic_wq_copy *wq;
  295. int ret;
  296. int sg_count;
  297. unsigned long flags;
  298. unsigned long ptr;
  299. rport = starget_to_rport(scsi_target(sc->device));
  300. ret = fc_remote_port_chkready(rport);
  301. if (ret) {
  302. sc->result = ret;
  303. done(sc);
  304. return 0;
  305. }
  306. lp = shost_priv(sc->device->host);
  307. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  308. return SCSI_MLQUEUE_HOST_BUSY;
  309. /*
  310. * Release host lock, use driver resource specific locks from here.
  311. * Don't re-enable interrupts in case they were disabled prior to the
  312. * caller disabling them.
  313. */
  314. spin_unlock(lp->host->host_lock);
  315. /* Get a new io_req for this SCSI IO */
  316. fnic = lport_priv(lp);
  317. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  318. if (!io_req) {
  319. ret = SCSI_MLQUEUE_HOST_BUSY;
  320. goto out;
  321. }
  322. memset(io_req, 0, sizeof(*io_req));
  323. /* Map the data buffer */
  324. sg_count = scsi_dma_map(sc);
  325. if (sg_count < 0) {
  326. mempool_free(io_req, fnic->io_req_pool);
  327. goto out;
  328. }
  329. /* Determine the type of scatter/gather list we need */
  330. io_req->sgl_cnt = sg_count;
  331. io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
  332. if (sg_count > FNIC_DFLT_SG_DESC_CNT)
  333. io_req->sgl_type = FNIC_SGL_CACHE_MAX;
  334. if (sg_count) {
  335. io_req->sgl_list =
  336. mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
  337. GFP_ATOMIC | GFP_DMA);
  338. if (!io_req->sgl_list) {
  339. ret = SCSI_MLQUEUE_HOST_BUSY;
  340. scsi_dma_unmap(sc);
  341. mempool_free(io_req, fnic->io_req_pool);
  342. goto out;
  343. }
  344. /* Cache sgl list allocated address before alignment */
  345. io_req->sgl_list_alloc = io_req->sgl_list;
  346. ptr = (unsigned long) io_req->sgl_list;
  347. if (ptr % FNIC_SG_DESC_ALIGN) {
  348. io_req->sgl_list = (struct host_sg_desc *)
  349. (((unsigned long) ptr
  350. + FNIC_SG_DESC_ALIGN - 1)
  351. & ~(FNIC_SG_DESC_ALIGN - 1));
  352. }
  353. }
  354. /* initialize rest of io_req */
  355. io_req->port_id = rport->port_id;
  356. CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
  357. CMD_SP(sc) = (char *)io_req;
  358. sc->scsi_done = done;
  359. /* create copy wq desc and enqueue it */
  360. wq = &fnic->wq_copy[0];
  361. ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
  362. if (ret) {
  363. /*
  364. * In case another thread cancelled the request,
  365. * refetch the pointer under the lock.
  366. */
  367. spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
  368. spin_lock_irqsave(io_lock, flags);
  369. io_req = (struct fnic_io_req *)CMD_SP(sc);
  370. CMD_SP(sc) = NULL;
  371. CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
  372. spin_unlock_irqrestore(io_lock, flags);
  373. if (io_req) {
  374. fnic_release_ioreq_buf(fnic, io_req, sc);
  375. mempool_free(io_req, fnic->io_req_pool);
  376. }
  377. }
  378. out:
  379. /* acquire host lock before returning to SCSI */
  380. spin_lock(lp->host->host_lock);
  381. return ret;
  382. }
  383. /*
  384. * fnic_fcpio_fw_reset_cmpl_handler
  385. * Routine to handle fw reset completion
  386. */
  387. static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
  388. struct fcpio_fw_req *desc)
  389. {
  390. u8 type;
  391. u8 hdr_status;
  392. struct fcpio_tag tag;
  393. int ret = 0;
  394. struct fc_frame *flogi;
  395. unsigned long flags;
  396. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  397. /* Clean up all outstanding io requests */
  398. fnic_cleanup_io(fnic, SCSI_NO_TAG);
  399. spin_lock_irqsave(&fnic->fnic_lock, flags);
  400. flogi = fnic->flogi;
  401. fnic->flogi = NULL;
  402. /* fnic should be in FC_TRANS_ETH_MODE */
  403. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
  404. /* Check status of reset completion */
  405. if (!hdr_status) {
  406. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  407. "reset cmpl success\n");
  408. /* Ready to send flogi out */
  409. fnic->state = FNIC_IN_ETH_MODE;
  410. } else {
  411. FNIC_SCSI_DBG(KERN_DEBUG,
  412. fnic->lport->host,
  413. "fnic fw_reset : failed %s\n",
  414. fnic_fcpio_status_to_str(hdr_status));
  415. /*
  416. * Unable to change to eth mode, cannot send out flogi
  417. * Change state to fc mode, so that subsequent Flogi
  418. * requests from libFC will cause more attempts to
  419. * reset the firmware. Free the cached flogi
  420. */
  421. fnic->state = FNIC_IN_FC_MODE;
  422. ret = -1;
  423. }
  424. } else {
  425. FNIC_SCSI_DBG(KERN_DEBUG,
  426. fnic->lport->host,
  427. "Unexpected state %s while processing"
  428. " reset cmpl\n", fnic_state_to_str(fnic->state));
  429. ret = -1;
  430. }
  431. /* Thread removing device blocks till firmware reset is complete */
  432. if (fnic->remove_wait)
  433. complete(fnic->remove_wait);
  434. /*
  435. * If fnic is being removed, or fw reset failed
  436. * free the flogi frame. Else, send it out
  437. */
  438. if (fnic->remove_wait || ret) {
  439. fnic->flogi_oxid = FC_XID_UNKNOWN;
  440. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  441. if (flogi)
  442. dev_kfree_skb_irq(fp_skb(flogi));
  443. goto reset_cmpl_handler_end;
  444. }
  445. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  446. if (flogi)
  447. ret = fnic_send_frame(fnic, flogi);
  448. reset_cmpl_handler_end:
  449. return ret;
  450. }
  451. /*
  452. * fnic_fcpio_flogi_reg_cmpl_handler
  453. * Routine to handle flogi register completion
  454. */
  455. static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
  456. struct fcpio_fw_req *desc)
  457. {
  458. u8 type;
  459. u8 hdr_status;
  460. struct fcpio_tag tag;
  461. int ret = 0;
  462. struct fc_frame *flogi_resp = NULL;
  463. unsigned long flags;
  464. struct sk_buff *skb;
  465. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  466. /* Update fnic state based on status of flogi reg completion */
  467. spin_lock_irqsave(&fnic->fnic_lock, flags);
  468. flogi_resp = fnic->flogi_resp;
  469. fnic->flogi_resp = NULL;
  470. if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
  471. /* Check flogi registration completion status */
  472. if (!hdr_status) {
  473. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  474. "flog reg succeeded\n");
  475. fnic->state = FNIC_IN_FC_MODE;
  476. } else {
  477. FNIC_SCSI_DBG(KERN_DEBUG,
  478. fnic->lport->host,
  479. "fnic flogi reg :failed %s\n",
  480. fnic_fcpio_status_to_str(hdr_status));
  481. fnic->state = FNIC_IN_ETH_MODE;
  482. ret = -1;
  483. }
  484. } else {
  485. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  486. "Unexpected fnic state %s while"
  487. " processing flogi reg completion\n",
  488. fnic_state_to_str(fnic->state));
  489. ret = -1;
  490. }
  491. /* Successful flogi reg cmpl, pass frame to LibFC */
  492. if (!ret && flogi_resp) {
  493. if (fnic->stop_rx_link_events) {
  494. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  495. goto reg_cmpl_handler_end;
  496. }
  497. skb = (struct sk_buff *)flogi_resp;
  498. /* Use fr_flags to indicate whether flogi resp or not */
  499. fr_flags(flogi_resp) = 1;
  500. fr_dev(flogi_resp) = fnic->lport;
  501. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  502. skb_queue_tail(&fnic->frame_queue, skb);
  503. queue_work(fnic_event_queue, &fnic->frame_work);
  504. } else {
  505. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  506. if (flogi_resp)
  507. dev_kfree_skb_irq(fp_skb(flogi_resp));
  508. }
  509. reg_cmpl_handler_end:
  510. return ret;
  511. }
  512. static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
  513. u16 request_out)
  514. {
  515. if (wq->to_clean_index <= wq->to_use_index) {
  516. /* out of range, stale request_out index */
  517. if (request_out < wq->to_clean_index ||
  518. request_out >= wq->to_use_index)
  519. return 0;
  520. } else {
  521. /* out of range, stale request_out index */
  522. if (request_out < wq->to_clean_index &&
  523. request_out >= wq->to_use_index)
  524. return 0;
  525. }
  526. /* request_out index is in range */
  527. return 1;
  528. }
  529. /*
  530. * Mark that ack received and store the Ack index. If there are multiple
  531. * acks received before Tx thread cleans it up, the latest value will be
  532. * used which is correct behavior. This state should be in the copy Wq
  533. * instead of in the fnic
  534. */
  535. static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
  536. unsigned int cq_index,
  537. struct fcpio_fw_req *desc)
  538. {
  539. struct vnic_wq_copy *wq;
  540. u16 request_out = desc->u.ack.request_out;
  541. unsigned long flags;
  542. /* mark the ack state */
  543. wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
  544. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  545. if (is_ack_index_in_range(wq, request_out)) {
  546. fnic->fw_ack_index[0] = request_out;
  547. fnic->fw_ack_recd[0] = 1;
  548. }
  549. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  550. }
  551. /*
  552. * fnic_fcpio_icmnd_cmpl_handler
  553. * Routine to handle icmnd completions
  554. */
  555. static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
  556. struct fcpio_fw_req *desc)
  557. {
  558. u8 type;
  559. u8 hdr_status;
  560. struct fcpio_tag tag;
  561. u32 id;
  562. u64 xfer_len = 0;
  563. struct fcpio_icmnd_cmpl *icmnd_cmpl;
  564. struct fnic_io_req *io_req;
  565. struct scsi_cmnd *sc;
  566. unsigned long flags;
  567. spinlock_t *io_lock;
  568. /* Decode the cmpl description to get the io_req id */
  569. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  570. fcpio_tag_id_dec(&tag, &id);
  571. if (id >= FNIC_MAX_IO_REQ)
  572. return;
  573. sc = scsi_host_find_tag(fnic->lport->host, id);
  574. WARN_ON_ONCE(!sc);
  575. if (!sc)
  576. return;
  577. io_lock = fnic_io_lock_hash(fnic, sc);
  578. spin_lock_irqsave(io_lock, flags);
  579. io_req = (struct fnic_io_req *)CMD_SP(sc);
  580. WARN_ON_ONCE(!io_req);
  581. if (!io_req) {
  582. spin_unlock_irqrestore(io_lock, flags);
  583. return;
  584. }
  585. /* firmware completed the io */
  586. io_req->io_completed = 1;
  587. /*
  588. * if SCSI-ML has already issued abort on this command,
  589. * ignore completion of the IO. The abts path will clean it up
  590. */
  591. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  592. spin_unlock_irqrestore(io_lock, flags);
  593. return;
  594. }
  595. /* Mark the IO as complete */
  596. CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
  597. icmnd_cmpl = &desc->u.icmnd_cmpl;
  598. switch (hdr_status) {
  599. case FCPIO_SUCCESS:
  600. sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
  601. xfer_len = scsi_bufflen(sc);
  602. scsi_set_resid(sc, icmnd_cmpl->residual);
  603. if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
  604. xfer_len -= icmnd_cmpl->residual;
  605. /*
  606. * If queue_full, then try to reduce queue depth for all
  607. * LUNS on the target. Todo: this should be accompanied
  608. * by a periodic queue_depth rampup based on successful
  609. * IO completion.
  610. */
  611. if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
  612. struct scsi_device *t_sdev;
  613. int qd = 0;
  614. shost_for_each_device(t_sdev, sc->device->host) {
  615. if (t_sdev->id != sc->device->id)
  616. continue;
  617. if (t_sdev->queue_depth > 1) {
  618. qd = scsi_track_queue_full
  619. (t_sdev,
  620. t_sdev->queue_depth - 1);
  621. if (qd == -1)
  622. qd = t_sdev->host->cmd_per_lun;
  623. shost_printk(KERN_INFO,
  624. fnic->lport->host,
  625. "scsi[%d:%d:%d:%d"
  626. "] queue full detected,"
  627. "new depth = %d\n",
  628. t_sdev->host->host_no,
  629. t_sdev->channel,
  630. t_sdev->id, t_sdev->lun,
  631. t_sdev->queue_depth);
  632. }
  633. }
  634. }
  635. break;
  636. case FCPIO_TIMEOUT: /* request was timed out */
  637. sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
  638. break;
  639. case FCPIO_ABORTED: /* request was aborted */
  640. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  641. break;
  642. case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
  643. scsi_set_resid(sc, icmnd_cmpl->residual);
  644. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  645. break;
  646. case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
  647. sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
  648. break;
  649. case FCPIO_INVALID_HEADER: /* header contains invalid data */
  650. case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
  651. case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
  652. case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
  653. case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
  654. case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
  655. case FCPIO_FW_ERR: /* request was terminated due fw error */
  656. default:
  657. shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
  658. fnic_fcpio_status_to_str(hdr_status));
  659. sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
  660. break;
  661. }
  662. /* Break link with the SCSI command */
  663. CMD_SP(sc) = NULL;
  664. spin_unlock_irqrestore(io_lock, flags);
  665. fnic_release_ioreq_buf(fnic, io_req, sc);
  666. mempool_free(io_req, fnic->io_req_pool);
  667. if (sc->sc_data_direction == DMA_FROM_DEVICE) {
  668. fnic->lport->host_stats.fcp_input_requests++;
  669. fnic->fcp_input_bytes += xfer_len;
  670. } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
  671. fnic->lport->host_stats.fcp_output_requests++;
  672. fnic->fcp_output_bytes += xfer_len;
  673. } else
  674. fnic->lport->host_stats.fcp_control_requests++;
  675. /* Call SCSI completion function to complete the IO */
  676. if (sc->scsi_done)
  677. sc->scsi_done(sc);
  678. }
  679. /* fnic_fcpio_itmf_cmpl_handler
  680. * Routine to handle itmf completions
  681. */
  682. static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
  683. struct fcpio_fw_req *desc)
  684. {
  685. u8 type;
  686. u8 hdr_status;
  687. struct fcpio_tag tag;
  688. u32 id;
  689. struct scsi_cmnd *sc;
  690. struct fnic_io_req *io_req;
  691. unsigned long flags;
  692. spinlock_t *io_lock;
  693. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  694. fcpio_tag_id_dec(&tag, &id);
  695. if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ)
  696. return;
  697. sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
  698. WARN_ON_ONCE(!sc);
  699. if (!sc)
  700. return;
  701. io_lock = fnic_io_lock_hash(fnic, sc);
  702. spin_lock_irqsave(io_lock, flags);
  703. io_req = (struct fnic_io_req *)CMD_SP(sc);
  704. WARN_ON_ONCE(!io_req);
  705. if (!io_req) {
  706. spin_unlock_irqrestore(io_lock, flags);
  707. return;
  708. }
  709. if (id & FNIC_TAG_ABORT) {
  710. /* Completion of abort cmd */
  711. if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
  712. /* This is a late completion. Ignore it */
  713. spin_unlock_irqrestore(io_lock, flags);
  714. return;
  715. }
  716. CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
  717. CMD_ABTS_STATUS(sc) = hdr_status;
  718. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  719. "abts cmpl recd. id %d status %s\n",
  720. (int)(id & FNIC_TAG_MASK),
  721. fnic_fcpio_status_to_str(hdr_status));
  722. /*
  723. * If scsi_eh thread is blocked waiting for abts to complete,
  724. * signal completion to it. IO will be cleaned in the thread
  725. * else clean it in this context
  726. */
  727. if (io_req->abts_done) {
  728. complete(io_req->abts_done);
  729. spin_unlock_irqrestore(io_lock, flags);
  730. } else {
  731. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  732. "abts cmpl, completing IO\n");
  733. CMD_SP(sc) = NULL;
  734. sc->result = (DID_ERROR << 16);
  735. spin_unlock_irqrestore(io_lock, flags);
  736. fnic_release_ioreq_buf(fnic, io_req, sc);
  737. mempool_free(io_req, fnic->io_req_pool);
  738. if (sc->scsi_done)
  739. sc->scsi_done(sc);
  740. }
  741. } else if (id & FNIC_TAG_DEV_RST) {
  742. /* Completion of device reset */
  743. CMD_LR_STATUS(sc) = hdr_status;
  744. CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
  745. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  746. "dev reset cmpl recd. id %d status %s\n",
  747. (int)(id & FNIC_TAG_MASK),
  748. fnic_fcpio_status_to_str(hdr_status));
  749. if (io_req->dr_done)
  750. complete(io_req->dr_done);
  751. spin_unlock_irqrestore(io_lock, flags);
  752. } else {
  753. shost_printk(KERN_ERR, fnic->lport->host,
  754. "Unexpected itmf io state %s tag %x\n",
  755. fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
  756. spin_unlock_irqrestore(io_lock, flags);
  757. }
  758. }
  759. /*
  760. * fnic_fcpio_cmpl_handler
  761. * Routine to service the cq for wq_copy
  762. */
  763. static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
  764. unsigned int cq_index,
  765. struct fcpio_fw_req *desc)
  766. {
  767. struct fnic *fnic = vnic_dev_priv(vdev);
  768. int ret = 0;
  769. switch (desc->hdr.type) {
  770. case FCPIO_ACK: /* fw copied copy wq desc to its queue */
  771. fnic_fcpio_ack_handler(fnic, cq_index, desc);
  772. break;
  773. case FCPIO_ICMND_CMPL: /* fw completed a command */
  774. fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
  775. break;
  776. case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
  777. fnic_fcpio_itmf_cmpl_handler(fnic, desc);
  778. break;
  779. case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
  780. ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
  781. break;
  782. case FCPIO_RESET_CMPL: /* fw completed reset */
  783. ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
  784. break;
  785. default:
  786. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  787. "firmware completion type %d\n",
  788. desc->hdr.type);
  789. break;
  790. }
  791. return ret;
  792. }
  793. /*
  794. * fnic_wq_copy_cmpl_handler
  795. * Routine to process wq copy
  796. */
  797. int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
  798. {
  799. unsigned int wq_work_done = 0;
  800. unsigned int i, cq_index;
  801. unsigned int cur_work_done;
  802. for (i = 0; i < fnic->wq_copy_count; i++) {
  803. cq_index = i + fnic->raw_wq_count + fnic->rq_count;
  804. cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
  805. fnic_fcpio_cmpl_handler,
  806. copy_work_to_do);
  807. wq_work_done += cur_work_done;
  808. }
  809. return wq_work_done;
  810. }
  811. static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
  812. {
  813. unsigned int i;
  814. struct fnic_io_req *io_req;
  815. unsigned long flags = 0;
  816. struct scsi_cmnd *sc;
  817. spinlock_t *io_lock;
  818. for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
  819. if (i == exclude_id)
  820. continue;
  821. sc = scsi_host_find_tag(fnic->lport->host, i);
  822. if (!sc)
  823. continue;
  824. io_lock = fnic_io_lock_hash(fnic, sc);
  825. spin_lock_irqsave(io_lock, flags);
  826. io_req = (struct fnic_io_req *)CMD_SP(sc);
  827. if (!io_req) {
  828. spin_unlock_irqrestore(io_lock, flags);
  829. goto cleanup_scsi_cmd;
  830. }
  831. CMD_SP(sc) = NULL;
  832. spin_unlock_irqrestore(io_lock, flags);
  833. /*
  834. * If there is a scsi_cmnd associated with this io_req, then
  835. * free the corresponding state
  836. */
  837. fnic_release_ioreq_buf(fnic, io_req, sc);
  838. mempool_free(io_req, fnic->io_req_pool);
  839. cleanup_scsi_cmd:
  840. sc->result = DID_TRANSPORT_DISRUPTED << 16;
  841. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
  842. " DID_TRANSPORT_DISRUPTED\n");
  843. /* Complete the command to SCSI */
  844. if (sc->scsi_done)
  845. sc->scsi_done(sc);
  846. }
  847. }
  848. void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
  849. struct fcpio_host_req *desc)
  850. {
  851. u32 id;
  852. struct fnic *fnic = vnic_dev_priv(wq->vdev);
  853. struct fnic_io_req *io_req;
  854. struct scsi_cmnd *sc;
  855. unsigned long flags;
  856. spinlock_t *io_lock;
  857. /* get the tag reference */
  858. fcpio_tag_id_dec(&desc->hdr.tag, &id);
  859. id &= FNIC_TAG_MASK;
  860. if (id >= FNIC_MAX_IO_REQ)
  861. return;
  862. sc = scsi_host_find_tag(fnic->lport->host, id);
  863. if (!sc)
  864. return;
  865. io_lock = fnic_io_lock_hash(fnic, sc);
  866. spin_lock_irqsave(io_lock, flags);
  867. /* Get the IO context which this desc refers to */
  868. io_req = (struct fnic_io_req *)CMD_SP(sc);
  869. /* fnic interrupts are turned off by now */
  870. if (!io_req) {
  871. spin_unlock_irqrestore(io_lock, flags);
  872. goto wq_copy_cleanup_scsi_cmd;
  873. }
  874. CMD_SP(sc) = NULL;
  875. spin_unlock_irqrestore(io_lock, flags);
  876. fnic_release_ioreq_buf(fnic, io_req, sc);
  877. mempool_free(io_req, fnic->io_req_pool);
  878. wq_copy_cleanup_scsi_cmd:
  879. sc->result = DID_NO_CONNECT << 16;
  880. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
  881. " DID_NO_CONNECT\n");
  882. if (sc->scsi_done)
  883. sc->scsi_done(sc);
  884. }
  885. static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
  886. u32 task_req, u8 *fc_lun,
  887. struct fnic_io_req *io_req)
  888. {
  889. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  890. unsigned long flags;
  891. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  892. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  893. free_wq_copy_descs(fnic, wq);
  894. if (!vnic_wq_copy_desc_avail(wq)) {
  895. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  896. return 1;
  897. }
  898. fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
  899. 0, task_req, tag, fc_lun, io_req->port_id,
  900. fnic->config.ra_tov, fnic->config.ed_tov);
  901. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  902. return 0;
  903. }
  904. void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
  905. {
  906. int tag;
  907. struct fnic_io_req *io_req;
  908. spinlock_t *io_lock;
  909. unsigned long flags;
  910. struct scsi_cmnd *sc;
  911. struct scsi_lun fc_lun;
  912. enum fnic_ioreq_state old_ioreq_state;
  913. FNIC_SCSI_DBG(KERN_DEBUG,
  914. fnic->lport->host,
  915. "fnic_rport_reset_exch called portid 0x%06x\n",
  916. port_id);
  917. if (fnic->in_remove)
  918. return;
  919. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  920. sc = scsi_host_find_tag(fnic->lport->host, tag);
  921. if (!sc)
  922. continue;
  923. io_lock = fnic_io_lock_hash(fnic, sc);
  924. spin_lock_irqsave(io_lock, flags);
  925. io_req = (struct fnic_io_req *)CMD_SP(sc);
  926. if (!io_req || io_req->port_id != port_id) {
  927. spin_unlock_irqrestore(io_lock, flags);
  928. continue;
  929. }
  930. /*
  931. * Found IO that is still pending with firmware and
  932. * belongs to rport that went away
  933. */
  934. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  935. spin_unlock_irqrestore(io_lock, flags);
  936. continue;
  937. }
  938. old_ioreq_state = CMD_STATE(sc);
  939. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  940. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  941. BUG_ON(io_req->abts_done);
  942. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  943. "fnic_rport_reset_exch: Issuing abts\n");
  944. spin_unlock_irqrestore(io_lock, flags);
  945. /* Now queue the abort command to firmware */
  946. int_to_scsilun(sc->device->lun, &fc_lun);
  947. if (fnic_queue_abort_io_req(fnic, tag,
  948. FCPIO_ITMF_ABT_TASK_TERM,
  949. fc_lun.scsi_lun, io_req)) {
  950. /*
  951. * Revert the cmd state back to old state, if
  952. * it hasnt changed in between. This cmd will get
  953. * aborted later by scsi_eh, or cleaned up during
  954. * lun reset
  955. */
  956. io_lock = fnic_io_lock_hash(fnic, sc);
  957. spin_lock_irqsave(io_lock, flags);
  958. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
  959. CMD_STATE(sc) = old_ioreq_state;
  960. spin_unlock_irqrestore(io_lock, flags);
  961. }
  962. }
  963. }
  964. void fnic_terminate_rport_io(struct fc_rport *rport)
  965. {
  966. int tag;
  967. struct fnic_io_req *io_req;
  968. spinlock_t *io_lock;
  969. unsigned long flags;
  970. struct scsi_cmnd *sc;
  971. struct scsi_lun fc_lun;
  972. struct fc_rport_libfc_priv *rdata = rport->dd_data;
  973. struct fc_lport *lport = rdata->local_port;
  974. struct fnic *fnic = lport_priv(lport);
  975. struct fc_rport *cmd_rport;
  976. enum fnic_ioreq_state old_ioreq_state;
  977. FNIC_SCSI_DBG(KERN_DEBUG,
  978. fnic->lport->host, "fnic_terminate_rport_io called"
  979. " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
  980. rport->port_name, rport->node_name,
  981. rport->port_id);
  982. if (fnic->in_remove)
  983. return;
  984. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  985. sc = scsi_host_find_tag(fnic->lport->host, tag);
  986. if (!sc)
  987. continue;
  988. cmd_rport = starget_to_rport(scsi_target(sc->device));
  989. if (rport != cmd_rport)
  990. continue;
  991. io_lock = fnic_io_lock_hash(fnic, sc);
  992. spin_lock_irqsave(io_lock, flags);
  993. io_req = (struct fnic_io_req *)CMD_SP(sc);
  994. if (!io_req || rport != cmd_rport) {
  995. spin_unlock_irqrestore(io_lock, flags);
  996. continue;
  997. }
  998. /*
  999. * Found IO that is still pending with firmware and
  1000. * belongs to rport that went away
  1001. */
  1002. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1003. spin_unlock_irqrestore(io_lock, flags);
  1004. continue;
  1005. }
  1006. old_ioreq_state = CMD_STATE(sc);
  1007. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  1008. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1009. BUG_ON(io_req->abts_done);
  1010. FNIC_SCSI_DBG(KERN_DEBUG,
  1011. fnic->lport->host,
  1012. "fnic_terminate_rport_io: Issuing abts\n");
  1013. spin_unlock_irqrestore(io_lock, flags);
  1014. /* Now queue the abort command to firmware */
  1015. int_to_scsilun(sc->device->lun, &fc_lun);
  1016. if (fnic_queue_abort_io_req(fnic, tag,
  1017. FCPIO_ITMF_ABT_TASK_TERM,
  1018. fc_lun.scsi_lun, io_req)) {
  1019. /*
  1020. * Revert the cmd state back to old state, if
  1021. * it hasnt changed in between. This cmd will get
  1022. * aborted later by scsi_eh, or cleaned up during
  1023. * lun reset
  1024. */
  1025. io_lock = fnic_io_lock_hash(fnic, sc);
  1026. spin_lock_irqsave(io_lock, flags);
  1027. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
  1028. CMD_STATE(sc) = old_ioreq_state;
  1029. spin_unlock_irqrestore(io_lock, flags);
  1030. }
  1031. }
  1032. }
  1033. static void fnic_block_error_handler(struct scsi_cmnd *sc)
  1034. {
  1035. struct Scsi_Host *shost = sc->device->host;
  1036. struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
  1037. unsigned long flags;
  1038. spin_lock_irqsave(shost->host_lock, flags);
  1039. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  1040. spin_unlock_irqrestore(shost->host_lock, flags);
  1041. msleep(1000);
  1042. spin_lock_irqsave(shost->host_lock, flags);
  1043. }
  1044. spin_unlock_irqrestore(shost->host_lock, flags);
  1045. }
  1046. /*
  1047. * This function is exported to SCSI for sending abort cmnds.
  1048. * A SCSI IO is represented by a io_req in the driver.
  1049. * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
  1050. */
  1051. int fnic_abort_cmd(struct scsi_cmnd *sc)
  1052. {
  1053. struct fc_lport *lp;
  1054. struct fnic *fnic;
  1055. struct fnic_io_req *io_req;
  1056. struct fc_rport *rport;
  1057. spinlock_t *io_lock;
  1058. unsigned long flags;
  1059. int ret = SUCCESS;
  1060. u32 task_req;
  1061. struct scsi_lun fc_lun;
  1062. DECLARE_COMPLETION_ONSTACK(tm_done);
  1063. /* Wait for rport to unblock */
  1064. fnic_block_error_handler(sc);
  1065. /* Get local-port, check ready and link up */
  1066. lp = shost_priv(sc->device->host);
  1067. fnic = lport_priv(lp);
  1068. FNIC_SCSI_DBG(KERN_DEBUG,
  1069. fnic->lport->host,
  1070. "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
  1071. (starget_to_rport(scsi_target(sc->device)))->port_id,
  1072. sc->device->lun, sc->request->tag);
  1073. if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
  1074. ret = FAILED;
  1075. goto fnic_abort_cmd_end;
  1076. }
  1077. /*
  1078. * Avoid a race between SCSI issuing the abort and the device
  1079. * completing the command.
  1080. *
  1081. * If the command is already completed by the fw cmpl code,
  1082. * we just return SUCCESS from here. This means that the abort
  1083. * succeeded. In the SCSI ML, since the timeout for command has
  1084. * happened, the completion wont actually complete the command
  1085. * and it will be considered as an aborted command
  1086. *
  1087. * The CMD_SP will not be cleared except while holding io_req_lock.
  1088. */
  1089. io_lock = fnic_io_lock_hash(fnic, sc);
  1090. spin_lock_irqsave(io_lock, flags);
  1091. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1092. if (!io_req) {
  1093. spin_unlock_irqrestore(io_lock, flags);
  1094. goto fnic_abort_cmd_end;
  1095. }
  1096. io_req->abts_done = &tm_done;
  1097. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1098. spin_unlock_irqrestore(io_lock, flags);
  1099. goto wait_pending;
  1100. }
  1101. /*
  1102. * Command is still pending, need to abort it
  1103. * If the firmware completes the command after this point,
  1104. * the completion wont be done till mid-layer, since abort
  1105. * has already started.
  1106. */
  1107. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  1108. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1109. spin_unlock_irqrestore(io_lock, flags);
  1110. /*
  1111. * Check readiness of the remote port. If the path to remote
  1112. * port is up, then send abts to the remote port to terminate
  1113. * the IO. Else, just locally terminate the IO in the firmware
  1114. */
  1115. rport = starget_to_rport(scsi_target(sc->device));
  1116. if (fc_remote_port_chkready(rport) == 0)
  1117. task_req = FCPIO_ITMF_ABT_TASK;
  1118. else
  1119. task_req = FCPIO_ITMF_ABT_TASK_TERM;
  1120. /* Now queue the abort command to firmware */
  1121. int_to_scsilun(sc->device->lun, &fc_lun);
  1122. if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
  1123. fc_lun.scsi_lun, io_req)) {
  1124. spin_lock_irqsave(io_lock, flags);
  1125. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1126. if (io_req)
  1127. io_req->abts_done = NULL;
  1128. spin_unlock_irqrestore(io_lock, flags);
  1129. ret = FAILED;
  1130. goto fnic_abort_cmd_end;
  1131. }
  1132. /*
  1133. * We queued an abort IO, wait for its completion.
  1134. * Once the firmware completes the abort command, it will
  1135. * wake up this thread.
  1136. */
  1137. wait_pending:
  1138. wait_for_completion_timeout(&tm_done,
  1139. msecs_to_jiffies
  1140. (2 * fnic->config.ra_tov +
  1141. fnic->config.ed_tov));
  1142. /* Check the abort status */
  1143. spin_lock_irqsave(io_lock, flags);
  1144. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1145. if (!io_req) {
  1146. spin_unlock_irqrestore(io_lock, flags);
  1147. ret = FAILED;
  1148. goto fnic_abort_cmd_end;
  1149. }
  1150. io_req->abts_done = NULL;
  1151. /* fw did not complete abort, timed out */
  1152. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1153. spin_unlock_irqrestore(io_lock, flags);
  1154. ret = FAILED;
  1155. goto fnic_abort_cmd_end;
  1156. }
  1157. /*
  1158. * firmware completed the abort, check the status,
  1159. * free the io_req irrespective of failure or success
  1160. */
  1161. if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
  1162. ret = FAILED;
  1163. CMD_SP(sc) = NULL;
  1164. spin_unlock_irqrestore(io_lock, flags);
  1165. fnic_release_ioreq_buf(fnic, io_req, sc);
  1166. mempool_free(io_req, fnic->io_req_pool);
  1167. fnic_abort_cmd_end:
  1168. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1169. "Returning from abort cmd %s\n",
  1170. (ret == SUCCESS) ?
  1171. "SUCCESS" : "FAILED");
  1172. return ret;
  1173. }
  1174. static inline int fnic_queue_dr_io_req(struct fnic *fnic,
  1175. struct scsi_cmnd *sc,
  1176. struct fnic_io_req *io_req)
  1177. {
  1178. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  1179. struct scsi_lun fc_lun;
  1180. int ret = 0;
  1181. unsigned long intr_flags;
  1182. spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
  1183. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  1184. free_wq_copy_descs(fnic, wq);
  1185. if (!vnic_wq_copy_desc_avail(wq)) {
  1186. ret = -EAGAIN;
  1187. goto lr_io_req_end;
  1188. }
  1189. /* fill in the lun info */
  1190. int_to_scsilun(sc->device->lun, &fc_lun);
  1191. fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
  1192. 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
  1193. fc_lun.scsi_lun, io_req->port_id,
  1194. fnic->config.ra_tov, fnic->config.ed_tov);
  1195. lr_io_req_end:
  1196. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  1197. return ret;
  1198. }
  1199. /*
  1200. * Clean up any pending aborts on the lun
  1201. * For each outstanding IO on this lun, whose abort is not completed by fw,
  1202. * issue a local abort. Wait for abort to complete. Return 0 if all commands
  1203. * successfully aborted, 1 otherwise
  1204. */
  1205. static int fnic_clean_pending_aborts(struct fnic *fnic,
  1206. struct scsi_cmnd *lr_sc)
  1207. {
  1208. int tag;
  1209. struct fnic_io_req *io_req;
  1210. spinlock_t *io_lock;
  1211. unsigned long flags;
  1212. int ret = 0;
  1213. struct scsi_cmnd *sc;
  1214. struct fc_rport *rport;
  1215. struct scsi_lun fc_lun;
  1216. struct scsi_device *lun_dev = lr_sc->device;
  1217. DECLARE_COMPLETION_ONSTACK(tm_done);
  1218. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  1219. sc = scsi_host_find_tag(fnic->lport->host, tag);
  1220. /*
  1221. * ignore this lun reset cmd or cmds that do not belong to
  1222. * this lun
  1223. */
  1224. if (!sc || sc == lr_sc || sc->device != lun_dev)
  1225. continue;
  1226. io_lock = fnic_io_lock_hash(fnic, sc);
  1227. spin_lock_irqsave(io_lock, flags);
  1228. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1229. if (!io_req || sc->device != lun_dev) {
  1230. spin_unlock_irqrestore(io_lock, flags);
  1231. continue;
  1232. }
  1233. /*
  1234. * Found IO that is still pending with firmware and
  1235. * belongs to the LUN that we are resetting
  1236. */
  1237. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1238. "Found IO in %s on lun\n",
  1239. fnic_ioreq_state_to_str(CMD_STATE(sc)));
  1240. BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
  1241. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1242. io_req->abts_done = &tm_done;
  1243. spin_unlock_irqrestore(io_lock, flags);
  1244. /* Now queue the abort command to firmware */
  1245. int_to_scsilun(sc->device->lun, &fc_lun);
  1246. rport = starget_to_rport(scsi_target(sc->device));
  1247. if (fnic_queue_abort_io_req(fnic, tag,
  1248. FCPIO_ITMF_ABT_TASK_TERM,
  1249. fc_lun.scsi_lun, io_req)) {
  1250. spin_lock_irqsave(io_lock, flags);
  1251. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1252. if (io_req)
  1253. io_req->abts_done = NULL;
  1254. spin_unlock_irqrestore(io_lock, flags);
  1255. ret = 1;
  1256. goto clean_pending_aborts_end;
  1257. }
  1258. wait_for_completion_timeout(&tm_done,
  1259. msecs_to_jiffies
  1260. (fnic->config.ed_tov));
  1261. /* Recheck cmd state to check if it is now aborted */
  1262. spin_lock_irqsave(io_lock, flags);
  1263. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1264. if (!io_req) {
  1265. spin_unlock_irqrestore(io_lock, flags);
  1266. ret = 1;
  1267. goto clean_pending_aborts_end;
  1268. }
  1269. io_req->abts_done = NULL;
  1270. /* if abort is still pending with fw, fail */
  1271. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1272. spin_unlock_irqrestore(io_lock, flags);
  1273. ret = 1;
  1274. goto clean_pending_aborts_end;
  1275. }
  1276. CMD_SP(sc) = NULL;
  1277. spin_unlock_irqrestore(io_lock, flags);
  1278. fnic_release_ioreq_buf(fnic, io_req, sc);
  1279. mempool_free(io_req, fnic->io_req_pool);
  1280. }
  1281. clean_pending_aborts_end:
  1282. return ret;
  1283. }
  1284. /*
  1285. * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
  1286. * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
  1287. * on the LUN.
  1288. */
  1289. int fnic_device_reset(struct scsi_cmnd *sc)
  1290. {
  1291. struct fc_lport *lp;
  1292. struct fnic *fnic;
  1293. struct fnic_io_req *io_req;
  1294. struct fc_rport *rport;
  1295. int status;
  1296. int ret = FAILED;
  1297. spinlock_t *io_lock;
  1298. unsigned long flags;
  1299. DECLARE_COMPLETION_ONSTACK(tm_done);
  1300. /* Wait for rport to unblock */
  1301. fnic_block_error_handler(sc);
  1302. /* Get local-port, check ready and link up */
  1303. lp = shost_priv(sc->device->host);
  1304. fnic = lport_priv(lp);
  1305. FNIC_SCSI_DBG(KERN_DEBUG,
  1306. fnic->lport->host,
  1307. "Device reset called FCID 0x%x, LUN 0x%x\n",
  1308. (starget_to_rport(scsi_target(sc->device)))->port_id,
  1309. sc->device->lun);
  1310. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  1311. goto fnic_device_reset_end;
  1312. /* Check if remote port up */
  1313. rport = starget_to_rport(scsi_target(sc->device));
  1314. if (fc_remote_port_chkready(rport))
  1315. goto fnic_device_reset_end;
  1316. io_lock = fnic_io_lock_hash(fnic, sc);
  1317. spin_lock_irqsave(io_lock, flags);
  1318. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1319. /*
  1320. * If there is a io_req attached to this command, then use it,
  1321. * else allocate a new one.
  1322. */
  1323. if (!io_req) {
  1324. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  1325. if (!io_req) {
  1326. spin_unlock_irqrestore(io_lock, flags);
  1327. goto fnic_device_reset_end;
  1328. }
  1329. memset(io_req, 0, sizeof(*io_req));
  1330. io_req->port_id = rport->port_id;
  1331. CMD_SP(sc) = (char *)io_req;
  1332. }
  1333. io_req->dr_done = &tm_done;
  1334. CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
  1335. CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
  1336. spin_unlock_irqrestore(io_lock, flags);
  1337. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
  1338. sc->request->tag);
  1339. /*
  1340. * issue the device reset, if enqueue failed, clean up the ioreq
  1341. * and break assoc with scsi cmd
  1342. */
  1343. if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
  1344. spin_lock_irqsave(io_lock, flags);
  1345. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1346. if (io_req)
  1347. io_req->dr_done = NULL;
  1348. goto fnic_device_reset_clean;
  1349. }
  1350. /*
  1351. * Wait on the local completion for LUN reset. The io_req may be
  1352. * freed while we wait since we hold no lock.
  1353. */
  1354. wait_for_completion_timeout(&tm_done,
  1355. msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
  1356. spin_lock_irqsave(io_lock, flags);
  1357. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1358. if (!io_req) {
  1359. spin_unlock_irqrestore(io_lock, flags);
  1360. goto fnic_device_reset_end;
  1361. }
  1362. io_req->dr_done = NULL;
  1363. status = CMD_LR_STATUS(sc);
  1364. spin_unlock_irqrestore(io_lock, flags);
  1365. /*
  1366. * If lun reset not completed, bail out with failed. io_req
  1367. * gets cleaned up during higher levels of EH
  1368. */
  1369. if (status == FCPIO_INVALID_CODE) {
  1370. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1371. "Device reset timed out\n");
  1372. goto fnic_device_reset_end;
  1373. }
  1374. /* Completed, but not successful, clean up the io_req, return fail */
  1375. if (status != FCPIO_SUCCESS) {
  1376. spin_lock_irqsave(io_lock, flags);
  1377. FNIC_SCSI_DBG(KERN_DEBUG,
  1378. fnic->lport->host,
  1379. "Device reset completed - failed\n");
  1380. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1381. goto fnic_device_reset_clean;
  1382. }
  1383. /*
  1384. * Clean up any aborts on this lun that have still not
  1385. * completed. If any of these fail, then LUN reset fails.
  1386. * clean_pending_aborts cleans all cmds on this lun except
  1387. * the lun reset cmd. If all cmds get cleaned, the lun reset
  1388. * succeeds
  1389. */
  1390. if (fnic_clean_pending_aborts(fnic, sc)) {
  1391. spin_lock_irqsave(io_lock, flags);
  1392. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1393. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1394. "Device reset failed"
  1395. " since could not abort all IOs\n");
  1396. goto fnic_device_reset_clean;
  1397. }
  1398. /* Clean lun reset command */
  1399. spin_lock_irqsave(io_lock, flags);
  1400. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1401. if (io_req)
  1402. /* Completed, and successful */
  1403. ret = SUCCESS;
  1404. fnic_device_reset_clean:
  1405. if (io_req)
  1406. CMD_SP(sc) = NULL;
  1407. spin_unlock_irqrestore(io_lock, flags);
  1408. if (io_req) {
  1409. fnic_release_ioreq_buf(fnic, io_req, sc);
  1410. mempool_free(io_req, fnic->io_req_pool);
  1411. }
  1412. fnic_device_reset_end:
  1413. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1414. "Returning from device reset %s\n",
  1415. (ret == SUCCESS) ?
  1416. "SUCCESS" : "FAILED");
  1417. return ret;
  1418. }
  1419. /* Clean up all IOs, clean up libFC local port */
  1420. int fnic_reset(struct Scsi_Host *shost)
  1421. {
  1422. struct fc_lport *lp;
  1423. struct fnic *fnic;
  1424. int ret = SUCCESS;
  1425. lp = shost_priv(shost);
  1426. fnic = lport_priv(lp);
  1427. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1428. "fnic_reset called\n");
  1429. /*
  1430. * Reset local port, this will clean up libFC exchanges,
  1431. * reset remote port sessions, and if link is up, begin flogi
  1432. */
  1433. if (lp->tt.lport_reset(lp))
  1434. ret = FAILED;
  1435. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1436. "Returning from fnic reset %s\n",
  1437. (ret == SUCCESS) ?
  1438. "SUCCESS" : "FAILED");
  1439. return ret;
  1440. }
  1441. /*
  1442. * SCSI Error handling calls driver's eh_host_reset if all prior
  1443. * error handling levels return FAILED. If host reset completes
  1444. * successfully, and if link is up, then Fabric login begins.
  1445. *
  1446. * Host Reset is the highest level of error recovery. If this fails, then
  1447. * host is offlined by SCSI.
  1448. *
  1449. */
  1450. int fnic_host_reset(struct scsi_cmnd *sc)
  1451. {
  1452. int ret;
  1453. unsigned long wait_host_tmo;
  1454. struct Scsi_Host *shost = sc->device->host;
  1455. struct fc_lport *lp = shost_priv(shost);
  1456. /*
  1457. * If fnic_reset is successful, wait for fabric login to complete
  1458. * scsi-ml tries to send a TUR to every device if host reset is
  1459. * successful, so before returning to scsi, fabric should be up
  1460. */
  1461. ret = fnic_reset(shost);
  1462. if (ret == SUCCESS) {
  1463. wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
  1464. ret = FAILED;
  1465. while (time_before(jiffies, wait_host_tmo)) {
  1466. if ((lp->state == LPORT_ST_READY) &&
  1467. (lp->link_up)) {
  1468. ret = SUCCESS;
  1469. break;
  1470. }
  1471. ssleep(1);
  1472. }
  1473. }
  1474. return ret;
  1475. }
  1476. /*
  1477. * This fxn is called from libFC when host is removed
  1478. */
  1479. void fnic_scsi_abort_io(struct fc_lport *lp)
  1480. {
  1481. int err = 0;
  1482. unsigned long flags;
  1483. enum fnic_state old_state;
  1484. struct fnic *fnic = lport_priv(lp);
  1485. DECLARE_COMPLETION_ONSTACK(remove_wait);
  1486. /* Issue firmware reset for fnic, wait for reset to complete */
  1487. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1488. fnic->remove_wait = &remove_wait;
  1489. old_state = fnic->state;
  1490. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  1491. vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
  1492. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1493. err = fnic_fw_reset_handler(fnic);
  1494. if (err) {
  1495. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1496. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  1497. fnic->state = old_state;
  1498. fnic->remove_wait = NULL;
  1499. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1500. return;
  1501. }
  1502. /* Wait for firmware reset to complete */
  1503. wait_for_completion_timeout(&remove_wait,
  1504. msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
  1505. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1506. fnic->remove_wait = NULL;
  1507. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1508. "fnic_scsi_abort_io %s\n",
  1509. (fnic->state == FNIC_IN_ETH_MODE) ?
  1510. "SUCCESS" : "FAILED");
  1511. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1512. }
  1513. /*
  1514. * This fxn called from libFC to clean up driver IO state on link down
  1515. */
  1516. void fnic_scsi_cleanup(struct fc_lport *lp)
  1517. {
  1518. unsigned long flags;
  1519. enum fnic_state old_state;
  1520. struct fnic *fnic = lport_priv(lp);
  1521. /* issue fw reset */
  1522. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1523. old_state = fnic->state;
  1524. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  1525. vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
  1526. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1527. if (fnic_fw_reset_handler(fnic)) {
  1528. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1529. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  1530. fnic->state = old_state;
  1531. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1532. }
  1533. }
  1534. void fnic_empty_scsi_cleanup(struct fc_lport *lp)
  1535. {
  1536. }
  1537. void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
  1538. {
  1539. struct fnic *fnic = lport_priv(lp);
  1540. /* Non-zero sid, nothing to do */
  1541. if (sid)
  1542. goto call_fc_exch_mgr_reset;
  1543. if (did) {
  1544. fnic_rport_exch_reset(fnic, did);
  1545. goto call_fc_exch_mgr_reset;
  1546. }
  1547. /*
  1548. * sid = 0, did = 0
  1549. * link down or device being removed
  1550. */
  1551. if (!fnic->in_remove)
  1552. fnic_scsi_cleanup(lp);
  1553. else
  1554. fnic_scsi_abort_io(lp);
  1555. /* call libFC exch mgr reset to reset its exchanges */
  1556. call_fc_exch_mgr_reset:
  1557. fc_exch_mgr_reset(lp, sid, did);
  1558. }