fnic_scsi.c 48 KB

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