fnic_scsi.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. */
  18. #include <linux/mempool.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/pci.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/if_vlan.h>
  28. #include <linux/delay.h>
  29. #include <linux/gfp.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/scsi_tcq.h>
  35. #include <scsi/fc/fc_els.h>
  36. #include <scsi/fc/fc_fcoe.h>
  37. #include <scsi/libfc.h>
  38. #include <scsi/fc_frame.h>
  39. #include "fnic_io.h"
  40. #include "fnic.h"
  41. const char *fnic_state_str[] = {
  42. [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
  43. [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
  44. [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
  45. [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
  46. };
  47. static const char *fnic_ioreq_state_str[] = {
  48. [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
  49. [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
  50. [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
  51. [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
  52. };
  53. static const char *fcpio_status_str[] = {
  54. [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
  55. [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
  56. [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
  57. [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
  58. [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
  59. [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
  60. [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
  61. [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
  62. [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
  63. [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
  64. [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
  65. [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
  66. [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
  67. [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
  68. [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
  69. [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
  70. [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
  71. [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
  72. [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
  73. };
  74. const char *fnic_state_to_str(unsigned int state)
  75. {
  76. if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
  77. return "unknown";
  78. return fnic_state_str[state];
  79. }
  80. static const char *fnic_ioreq_state_to_str(unsigned int state)
  81. {
  82. if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
  83. !fnic_ioreq_state_str[state])
  84. return "unknown";
  85. return fnic_ioreq_state_str[state];
  86. }
  87. static const char *fnic_fcpio_status_to_str(unsigned int status)
  88. {
  89. if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
  90. return "unknown";
  91. return fcpio_status_str[status];
  92. }
  93. static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
  94. static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
  95. struct scsi_cmnd *sc)
  96. {
  97. u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
  98. return &fnic->io_req_lock[hash];
  99. }
  100. /*
  101. * Unmap the data buffer and sense buffer for an io_req,
  102. * also unmap and free the device-private scatter/gather list.
  103. */
  104. static void fnic_release_ioreq_buf(struct fnic *fnic,
  105. struct fnic_io_req *io_req,
  106. struct scsi_cmnd *sc)
  107. {
  108. if (io_req->sgl_list_pa)
  109. pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
  110. sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
  111. PCI_DMA_TODEVICE);
  112. scsi_dma_unmap(sc);
  113. if (io_req->sgl_cnt)
  114. mempool_free(io_req->sgl_list_alloc,
  115. fnic->io_sgl_pool[io_req->sgl_type]);
  116. if (io_req->sense_buf_pa)
  117. pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
  118. SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
  119. }
  120. /* Free up Copy Wq descriptors. Called with copy_wq lock held */
  121. static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
  122. {
  123. /* if no Ack received from firmware, then nothing to clean */
  124. if (!fnic->fw_ack_recd[0])
  125. return 1;
  126. /*
  127. * Update desc_available count based on number of freed descriptors
  128. * Account for wraparound
  129. */
  130. if (wq->to_clean_index <= fnic->fw_ack_index[0])
  131. wq->ring.desc_avail += (fnic->fw_ack_index[0]
  132. - wq->to_clean_index + 1);
  133. else
  134. wq->ring.desc_avail += (wq->ring.desc_count
  135. - wq->to_clean_index
  136. + fnic->fw_ack_index[0] + 1);
  137. /*
  138. * just bump clean index to ack_index+1 accounting for wraparound
  139. * this will essentially free up all descriptors between
  140. * to_clean_index and fw_ack_index, both inclusive
  141. */
  142. wq->to_clean_index =
  143. (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
  144. /* we have processed the acks received so far */
  145. fnic->fw_ack_recd[0] = 0;
  146. return 0;
  147. }
  148. /*
  149. * fnic_fw_reset_handler
  150. * Routine to send reset msg to fw
  151. */
  152. int fnic_fw_reset_handler(struct fnic *fnic)
  153. {
  154. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  155. int ret = 0;
  156. unsigned long flags;
  157. skb_queue_purge(&fnic->frame_queue);
  158. skb_queue_purge(&fnic->tx_queue);
  159. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  160. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  161. free_wq_copy_descs(fnic, wq);
  162. if (!vnic_wq_copy_desc_avail(wq))
  163. ret = -EAGAIN;
  164. else
  165. fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
  166. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  167. if (!ret)
  168. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  169. "Issued fw reset\n");
  170. else
  171. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  172. "Failed to issue fw reset\n");
  173. return ret;
  174. }
  175. /*
  176. * fnic_flogi_reg_handler
  177. * Routine to send flogi register msg to fw
  178. */
  179. int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
  180. {
  181. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  182. enum fcpio_flogi_reg_format_type format;
  183. struct fc_lport *lp = fnic->lport;
  184. u8 gw_mac[ETH_ALEN];
  185. int ret = 0;
  186. unsigned long flags;
  187. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  188. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  189. free_wq_copy_descs(fnic, wq);
  190. if (!vnic_wq_copy_desc_avail(wq)) {
  191. ret = -EAGAIN;
  192. goto flogi_reg_ioreq_end;
  193. }
  194. if (fnic->ctlr.map_dest) {
  195. memset(gw_mac, 0xff, ETH_ALEN);
  196. format = FCPIO_FLOGI_REG_DEF_DEST;
  197. } else {
  198. memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
  199. format = FCPIO_FLOGI_REG_GW_DEST;
  200. }
  201. if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
  202. fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
  203. fc_id, gw_mac,
  204. fnic->data_src_addr,
  205. lp->r_a_tov, lp->e_d_tov);
  206. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  207. "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
  208. fc_id, fnic->data_src_addr, gw_mac);
  209. } else {
  210. fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
  211. format, fc_id, gw_mac);
  212. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  213. "FLOGI reg issued fcid %x map %d dest %pM\n",
  214. fc_id, fnic->ctlr.map_dest, gw_mac);
  215. }
  216. flogi_reg_ioreq_end:
  217. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  218. return ret;
  219. }
  220. /*
  221. * fnic_queue_wq_copy_desc
  222. * Routine to enqueue a wq copy desc
  223. */
  224. static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
  225. struct vnic_wq_copy *wq,
  226. struct fnic_io_req *io_req,
  227. struct scsi_cmnd *sc,
  228. int sg_count)
  229. {
  230. struct scatterlist *sg;
  231. struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
  232. struct fc_rport_libfc_priv *rp = rport->dd_data;
  233. struct host_sg_desc *desc;
  234. u8 pri_tag = 0;
  235. unsigned int i;
  236. unsigned long intr_flags;
  237. int flags;
  238. u8 exch_flags;
  239. struct scsi_lun fc_lun;
  240. char msg[2];
  241. if (sg_count) {
  242. /* For each SGE, create a device desc entry */
  243. desc = io_req->sgl_list;
  244. for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
  245. desc->addr = cpu_to_le64(sg_dma_address(sg));
  246. desc->len = cpu_to_le32(sg_dma_len(sg));
  247. desc->_resvd = 0;
  248. desc++;
  249. }
  250. io_req->sgl_list_pa = pci_map_single
  251. (fnic->pdev,
  252. io_req->sgl_list,
  253. sizeof(io_req->sgl_list[0]) * sg_count,
  254. PCI_DMA_TODEVICE);
  255. }
  256. io_req->sense_buf_pa = pci_map_single(fnic->pdev,
  257. sc->sense_buffer,
  258. SCSI_SENSE_BUFFERSIZE,
  259. PCI_DMA_FROMDEVICE);
  260. int_to_scsilun(sc->device->lun, &fc_lun);
  261. pri_tag = FCPIO_ICMND_PTA_SIMPLE;
  262. msg[0] = MSG_SIMPLE_TAG;
  263. scsi_populate_tag_msg(sc, msg);
  264. if (msg[0] == MSG_ORDERED_TAG)
  265. pri_tag = FCPIO_ICMND_PTA_ORDERED;
  266. /* Enqueue the descriptor in the Copy WQ */
  267. spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
  268. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  269. free_wq_copy_descs(fnic, wq);
  270. if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
  271. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  272. return SCSI_MLQUEUE_HOST_BUSY;
  273. }
  274. flags = 0;
  275. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  276. flags = FCPIO_ICMND_RDDATA;
  277. else if (sc->sc_data_direction == DMA_TO_DEVICE)
  278. flags = FCPIO_ICMND_WRDATA;
  279. exch_flags = 0;
  280. if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
  281. (rp->flags & FC_RP_FLAGS_RETRY))
  282. exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
  283. fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
  284. 0, exch_flags, io_req->sgl_cnt,
  285. SCSI_SENSE_BUFFERSIZE,
  286. io_req->sgl_list_pa,
  287. io_req->sense_buf_pa,
  288. 0, /* scsi cmd ref, always 0 */
  289. pri_tag, /* scsi pri and tag */
  290. flags, /* command flags */
  291. sc->cmnd, sc->cmd_len,
  292. scsi_bufflen(sc),
  293. fc_lun.scsi_lun, io_req->port_id,
  294. rport->maxframe_size, rp->r_a_tov,
  295. rp->e_d_tov);
  296. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
  297. return 0;
  298. }
  299. /*
  300. * fnic_queuecommand
  301. * Routine to send a scsi cdb
  302. * Called with host_lock held and interrupts disabled.
  303. */
  304. static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  305. {
  306. struct fc_lport *lp;
  307. struct fc_rport *rport;
  308. struct fnic_io_req *io_req;
  309. struct fnic *fnic;
  310. struct vnic_wq_copy *wq;
  311. int ret;
  312. int sg_count;
  313. unsigned long flags;
  314. unsigned long ptr;
  315. rport = starget_to_rport(scsi_target(sc->device));
  316. ret = fc_remote_port_chkready(rport);
  317. if (ret) {
  318. sc->result = ret;
  319. done(sc);
  320. return 0;
  321. }
  322. lp = shost_priv(sc->device->host);
  323. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  324. return SCSI_MLQUEUE_HOST_BUSY;
  325. /*
  326. * Release host lock, use driver resource specific locks from here.
  327. * Don't re-enable interrupts in case they were disabled prior to the
  328. * caller disabling them.
  329. */
  330. spin_unlock(lp->host->host_lock);
  331. /* Get a new io_req for this SCSI IO */
  332. fnic = lport_priv(lp);
  333. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  334. if (!io_req) {
  335. ret = SCSI_MLQUEUE_HOST_BUSY;
  336. goto out;
  337. }
  338. memset(io_req, 0, sizeof(*io_req));
  339. /* Map the data buffer */
  340. sg_count = scsi_dma_map(sc);
  341. if (sg_count < 0) {
  342. mempool_free(io_req, fnic->io_req_pool);
  343. goto out;
  344. }
  345. /* Determine the type of scatter/gather list we need */
  346. io_req->sgl_cnt = sg_count;
  347. io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
  348. if (sg_count > FNIC_DFLT_SG_DESC_CNT)
  349. io_req->sgl_type = FNIC_SGL_CACHE_MAX;
  350. if (sg_count) {
  351. io_req->sgl_list =
  352. mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
  353. GFP_ATOMIC | GFP_DMA);
  354. if (!io_req->sgl_list) {
  355. ret = SCSI_MLQUEUE_HOST_BUSY;
  356. scsi_dma_unmap(sc);
  357. mempool_free(io_req, fnic->io_req_pool);
  358. goto out;
  359. }
  360. /* Cache sgl list allocated address before alignment */
  361. io_req->sgl_list_alloc = io_req->sgl_list;
  362. ptr = (unsigned long) io_req->sgl_list;
  363. if (ptr % FNIC_SG_DESC_ALIGN) {
  364. io_req->sgl_list = (struct host_sg_desc *)
  365. (((unsigned long) ptr
  366. + FNIC_SG_DESC_ALIGN - 1)
  367. & ~(FNIC_SG_DESC_ALIGN - 1));
  368. }
  369. }
  370. /* initialize rest of io_req */
  371. io_req->port_id = rport->port_id;
  372. CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
  373. CMD_SP(sc) = (char *)io_req;
  374. sc->scsi_done = done;
  375. /* create copy wq desc and enqueue it */
  376. wq = &fnic->wq_copy[0];
  377. ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
  378. if (ret) {
  379. /*
  380. * In case another thread cancelled the request,
  381. * refetch the pointer under the lock.
  382. */
  383. spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
  384. spin_lock_irqsave(io_lock, flags);
  385. io_req = (struct fnic_io_req *)CMD_SP(sc);
  386. CMD_SP(sc) = NULL;
  387. CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
  388. spin_unlock_irqrestore(io_lock, flags);
  389. if (io_req) {
  390. fnic_release_ioreq_buf(fnic, io_req, sc);
  391. mempool_free(io_req, fnic->io_req_pool);
  392. }
  393. }
  394. out:
  395. /* acquire host lock before returning to SCSI */
  396. spin_lock(lp->host->host_lock);
  397. return ret;
  398. }
  399. DEF_SCSI_QCMD(fnic_queuecommand)
  400. /*
  401. * fnic_fcpio_fw_reset_cmpl_handler
  402. * Routine to handle fw reset completion
  403. */
  404. static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
  405. struct fcpio_fw_req *desc)
  406. {
  407. u8 type;
  408. u8 hdr_status;
  409. struct fcpio_tag tag;
  410. int ret = 0;
  411. unsigned long flags;
  412. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  413. /* Clean up all outstanding io requests */
  414. fnic_cleanup_io(fnic, SCSI_NO_TAG);
  415. spin_lock_irqsave(&fnic->fnic_lock, flags);
  416. /* fnic should be in FC_TRANS_ETH_MODE */
  417. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
  418. /* Check status of reset completion */
  419. if (!hdr_status) {
  420. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  421. "reset cmpl success\n");
  422. /* Ready to send flogi out */
  423. fnic->state = FNIC_IN_ETH_MODE;
  424. } else {
  425. FNIC_SCSI_DBG(KERN_DEBUG,
  426. fnic->lport->host,
  427. "fnic fw_reset : failed %s\n",
  428. fnic_fcpio_status_to_str(hdr_status));
  429. /*
  430. * Unable to change to eth mode, cannot send out flogi
  431. * Change state to fc mode, so that subsequent Flogi
  432. * requests from libFC will cause more attempts to
  433. * reset the firmware. Free the cached flogi
  434. */
  435. fnic->state = FNIC_IN_FC_MODE;
  436. ret = -1;
  437. }
  438. } else {
  439. FNIC_SCSI_DBG(KERN_DEBUG,
  440. fnic->lport->host,
  441. "Unexpected state %s while processing"
  442. " reset cmpl\n", fnic_state_to_str(fnic->state));
  443. ret = -1;
  444. }
  445. /* Thread removing device blocks till firmware reset is complete */
  446. if (fnic->remove_wait)
  447. complete(fnic->remove_wait);
  448. /*
  449. * If fnic is being removed, or fw reset failed
  450. * free the flogi frame. Else, send it out
  451. */
  452. if (fnic->remove_wait || ret) {
  453. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  454. skb_queue_purge(&fnic->tx_queue);
  455. goto reset_cmpl_handler_end;
  456. }
  457. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  458. fnic_flush_tx(fnic);
  459. reset_cmpl_handler_end:
  460. return ret;
  461. }
  462. /*
  463. * fnic_fcpio_flogi_reg_cmpl_handler
  464. * Routine to handle flogi register completion
  465. */
  466. static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
  467. struct fcpio_fw_req *desc)
  468. {
  469. u8 type;
  470. u8 hdr_status;
  471. struct fcpio_tag tag;
  472. int ret = 0;
  473. unsigned long flags;
  474. fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
  475. /* Update fnic state based on status of flogi reg completion */
  476. spin_lock_irqsave(&fnic->fnic_lock, flags);
  477. if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
  478. /* Check flogi registration completion status */
  479. if (!hdr_status) {
  480. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  481. "flog reg succeeded\n");
  482. fnic->state = FNIC_IN_FC_MODE;
  483. } else {
  484. FNIC_SCSI_DBG(KERN_DEBUG,
  485. fnic->lport->host,
  486. "fnic flogi reg :failed %s\n",
  487. fnic_fcpio_status_to_str(hdr_status));
  488. fnic->state = FNIC_IN_ETH_MODE;
  489. ret = -1;
  490. }
  491. } else {
  492. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  493. "Unexpected fnic state %s while"
  494. " processing flogi reg completion\n",
  495. fnic_state_to_str(fnic->state));
  496. ret = -1;
  497. }
  498. if (!ret) {
  499. if (fnic->stop_rx_link_events) {
  500. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  501. goto reg_cmpl_handler_end;
  502. }
  503. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  504. fnic_flush_tx(fnic);
  505. queue_work(fnic_event_queue, &fnic->frame_work);
  506. } else {
  507. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  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. case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
  781. ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
  782. break;
  783. case FCPIO_RESET_CMPL: /* fw completed reset */
  784. ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
  785. break;
  786. default:
  787. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  788. "firmware completion type %d\n",
  789. desc->hdr.type);
  790. break;
  791. }
  792. return ret;
  793. }
  794. /*
  795. * fnic_wq_copy_cmpl_handler
  796. * Routine to process wq copy
  797. */
  798. int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
  799. {
  800. unsigned int wq_work_done = 0;
  801. unsigned int i, cq_index;
  802. unsigned int cur_work_done;
  803. for (i = 0; i < fnic->wq_copy_count; i++) {
  804. cq_index = i + fnic->raw_wq_count + fnic->rq_count;
  805. cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
  806. fnic_fcpio_cmpl_handler,
  807. copy_work_to_do);
  808. wq_work_done += cur_work_done;
  809. }
  810. return wq_work_done;
  811. }
  812. static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
  813. {
  814. unsigned int i;
  815. struct fnic_io_req *io_req;
  816. unsigned long flags = 0;
  817. struct scsi_cmnd *sc;
  818. spinlock_t *io_lock;
  819. for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
  820. if (i == exclude_id)
  821. continue;
  822. sc = scsi_host_find_tag(fnic->lport->host, i);
  823. if (!sc)
  824. continue;
  825. io_lock = fnic_io_lock_hash(fnic, sc);
  826. spin_lock_irqsave(io_lock, flags);
  827. io_req = (struct fnic_io_req *)CMD_SP(sc);
  828. if (!io_req) {
  829. spin_unlock_irqrestore(io_lock, flags);
  830. goto cleanup_scsi_cmd;
  831. }
  832. CMD_SP(sc) = NULL;
  833. spin_unlock_irqrestore(io_lock, flags);
  834. /*
  835. * If there is a scsi_cmnd associated with this io_req, then
  836. * free the corresponding state
  837. */
  838. fnic_release_ioreq_buf(fnic, io_req, sc);
  839. mempool_free(io_req, fnic->io_req_pool);
  840. cleanup_scsi_cmd:
  841. sc->result = DID_TRANSPORT_DISRUPTED << 16;
  842. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
  843. " DID_TRANSPORT_DISRUPTED\n");
  844. /* Complete the command to SCSI */
  845. if (sc->scsi_done)
  846. sc->scsi_done(sc);
  847. }
  848. }
  849. void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
  850. struct fcpio_host_req *desc)
  851. {
  852. u32 id;
  853. struct fnic *fnic = vnic_dev_priv(wq->vdev);
  854. struct fnic_io_req *io_req;
  855. struct scsi_cmnd *sc;
  856. unsigned long flags;
  857. spinlock_t *io_lock;
  858. /* get the tag reference */
  859. fcpio_tag_id_dec(&desc->hdr.tag, &id);
  860. id &= FNIC_TAG_MASK;
  861. if (id >= FNIC_MAX_IO_REQ)
  862. return;
  863. sc = scsi_host_find_tag(fnic->lport->host, id);
  864. if (!sc)
  865. return;
  866. io_lock = fnic_io_lock_hash(fnic, sc);
  867. spin_lock_irqsave(io_lock, flags);
  868. /* Get the IO context which this desc refers to */
  869. io_req = (struct fnic_io_req *)CMD_SP(sc);
  870. /* fnic interrupts are turned off by now */
  871. if (!io_req) {
  872. spin_unlock_irqrestore(io_lock, flags);
  873. goto wq_copy_cleanup_scsi_cmd;
  874. }
  875. CMD_SP(sc) = NULL;
  876. spin_unlock_irqrestore(io_lock, flags);
  877. fnic_release_ioreq_buf(fnic, io_req, sc);
  878. mempool_free(io_req, fnic->io_req_pool);
  879. wq_copy_cleanup_scsi_cmd:
  880. sc->result = DID_NO_CONNECT << 16;
  881. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
  882. " DID_NO_CONNECT\n");
  883. if (sc->scsi_done)
  884. sc->scsi_done(sc);
  885. }
  886. static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
  887. u32 task_req, u8 *fc_lun,
  888. struct fnic_io_req *io_req)
  889. {
  890. struct vnic_wq_copy *wq = &fnic->wq_copy[0];
  891. unsigned long flags;
  892. spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
  893. if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
  894. free_wq_copy_descs(fnic, wq);
  895. if (!vnic_wq_copy_desc_avail(wq)) {
  896. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  897. return 1;
  898. }
  899. fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
  900. 0, task_req, tag, fc_lun, io_req->port_id,
  901. fnic->config.ra_tov, fnic->config.ed_tov);
  902. spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
  903. return 0;
  904. }
  905. void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
  906. {
  907. int tag;
  908. struct fnic_io_req *io_req;
  909. spinlock_t *io_lock;
  910. unsigned long flags;
  911. struct scsi_cmnd *sc;
  912. struct scsi_lun fc_lun;
  913. enum fnic_ioreq_state old_ioreq_state;
  914. FNIC_SCSI_DBG(KERN_DEBUG,
  915. fnic->lport->host,
  916. "fnic_rport_reset_exch called portid 0x%06x\n",
  917. port_id);
  918. if (fnic->in_remove)
  919. return;
  920. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  921. sc = scsi_host_find_tag(fnic->lport->host, tag);
  922. if (!sc)
  923. continue;
  924. io_lock = fnic_io_lock_hash(fnic, sc);
  925. spin_lock_irqsave(io_lock, flags);
  926. io_req = (struct fnic_io_req *)CMD_SP(sc);
  927. if (!io_req || io_req->port_id != port_id) {
  928. spin_unlock_irqrestore(io_lock, flags);
  929. continue;
  930. }
  931. /*
  932. * Found IO that is still pending with firmware and
  933. * belongs to rport that went away
  934. */
  935. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  936. spin_unlock_irqrestore(io_lock, flags);
  937. continue;
  938. }
  939. old_ioreq_state = CMD_STATE(sc);
  940. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  941. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  942. BUG_ON(io_req->abts_done);
  943. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  944. "fnic_rport_reset_exch: Issuing abts\n");
  945. spin_unlock_irqrestore(io_lock, flags);
  946. /* Now queue the abort command to firmware */
  947. int_to_scsilun(sc->device->lun, &fc_lun);
  948. if (fnic_queue_abort_io_req(fnic, tag,
  949. FCPIO_ITMF_ABT_TASK_TERM,
  950. fc_lun.scsi_lun, io_req)) {
  951. /*
  952. * Revert the cmd state back to old state, if
  953. * it hasnt changed in between. This cmd will get
  954. * aborted later by scsi_eh, or cleaned up during
  955. * lun reset
  956. */
  957. io_lock = fnic_io_lock_hash(fnic, sc);
  958. spin_lock_irqsave(io_lock, flags);
  959. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
  960. CMD_STATE(sc) = old_ioreq_state;
  961. spin_unlock_irqrestore(io_lock, flags);
  962. }
  963. }
  964. }
  965. void fnic_terminate_rport_io(struct fc_rport *rport)
  966. {
  967. int tag;
  968. struct fnic_io_req *io_req;
  969. spinlock_t *io_lock;
  970. unsigned long flags;
  971. struct scsi_cmnd *sc;
  972. struct scsi_lun fc_lun;
  973. struct fc_rport_libfc_priv *rdata = rport->dd_data;
  974. struct fc_lport *lport = rdata->local_port;
  975. struct fnic *fnic = lport_priv(lport);
  976. struct fc_rport *cmd_rport;
  977. enum fnic_ioreq_state old_ioreq_state;
  978. FNIC_SCSI_DBG(KERN_DEBUG,
  979. fnic->lport->host, "fnic_terminate_rport_io called"
  980. " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
  981. rport->port_name, rport->node_name,
  982. rport->port_id);
  983. if (fnic->in_remove)
  984. return;
  985. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  986. sc = scsi_host_find_tag(fnic->lport->host, tag);
  987. if (!sc)
  988. continue;
  989. cmd_rport = starget_to_rport(scsi_target(sc->device));
  990. if (rport != cmd_rport)
  991. continue;
  992. io_lock = fnic_io_lock_hash(fnic, sc);
  993. spin_lock_irqsave(io_lock, flags);
  994. io_req = (struct fnic_io_req *)CMD_SP(sc);
  995. if (!io_req || rport != cmd_rport) {
  996. spin_unlock_irqrestore(io_lock, flags);
  997. continue;
  998. }
  999. /*
  1000. * Found IO that is still pending with firmware and
  1001. * belongs to rport that went away
  1002. */
  1003. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1004. spin_unlock_irqrestore(io_lock, flags);
  1005. continue;
  1006. }
  1007. old_ioreq_state = CMD_STATE(sc);
  1008. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  1009. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1010. BUG_ON(io_req->abts_done);
  1011. FNIC_SCSI_DBG(KERN_DEBUG,
  1012. fnic->lport->host,
  1013. "fnic_terminate_rport_io: Issuing abts\n");
  1014. spin_unlock_irqrestore(io_lock, flags);
  1015. /* Now queue the abort command to firmware */
  1016. int_to_scsilun(sc->device->lun, &fc_lun);
  1017. if (fnic_queue_abort_io_req(fnic, tag,
  1018. FCPIO_ITMF_ABT_TASK_TERM,
  1019. fc_lun.scsi_lun, io_req)) {
  1020. /*
  1021. * Revert the cmd state back to old state, if
  1022. * it hasnt changed in between. This cmd will get
  1023. * aborted later by scsi_eh, or cleaned up during
  1024. * lun reset
  1025. */
  1026. io_lock = fnic_io_lock_hash(fnic, sc);
  1027. spin_lock_irqsave(io_lock, flags);
  1028. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
  1029. CMD_STATE(sc) = old_ioreq_state;
  1030. spin_unlock_irqrestore(io_lock, flags);
  1031. }
  1032. }
  1033. }
  1034. /*
  1035. * This function is exported to SCSI for sending abort cmnds.
  1036. * A SCSI IO is represented by a io_req in the driver.
  1037. * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
  1038. */
  1039. int fnic_abort_cmd(struct scsi_cmnd *sc)
  1040. {
  1041. struct fc_lport *lp;
  1042. struct fnic *fnic;
  1043. struct fnic_io_req *io_req;
  1044. struct fc_rport *rport;
  1045. spinlock_t *io_lock;
  1046. unsigned long flags;
  1047. int ret = SUCCESS;
  1048. u32 task_req;
  1049. struct scsi_lun fc_lun;
  1050. DECLARE_COMPLETION_ONSTACK(tm_done);
  1051. /* Wait for rport to unblock */
  1052. fc_block_scsi_eh(sc);
  1053. /* Get local-port, check ready and link up */
  1054. lp = shost_priv(sc->device->host);
  1055. fnic = lport_priv(lp);
  1056. rport = starget_to_rport(scsi_target(sc->device));
  1057. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1058. "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
  1059. rport->port_id, sc->device->lun, sc->request->tag);
  1060. if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
  1061. ret = FAILED;
  1062. goto fnic_abort_cmd_end;
  1063. }
  1064. /*
  1065. * Avoid a race between SCSI issuing the abort and the device
  1066. * completing the command.
  1067. *
  1068. * If the command is already completed by the fw cmpl code,
  1069. * we just return SUCCESS from here. This means that the abort
  1070. * succeeded. In the SCSI ML, since the timeout for command has
  1071. * happened, the completion wont actually complete the command
  1072. * and it will be considered as an aborted command
  1073. *
  1074. * The CMD_SP will not be cleared except while holding io_req_lock.
  1075. */
  1076. io_lock = fnic_io_lock_hash(fnic, sc);
  1077. spin_lock_irqsave(io_lock, flags);
  1078. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1079. if (!io_req) {
  1080. spin_unlock_irqrestore(io_lock, flags);
  1081. goto fnic_abort_cmd_end;
  1082. }
  1083. io_req->abts_done = &tm_done;
  1084. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1085. spin_unlock_irqrestore(io_lock, flags);
  1086. goto wait_pending;
  1087. }
  1088. /*
  1089. * Command is still pending, need to abort it
  1090. * If the firmware completes the command after this point,
  1091. * the completion wont be done till mid-layer, since abort
  1092. * has already started.
  1093. */
  1094. CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
  1095. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1096. spin_unlock_irqrestore(io_lock, flags);
  1097. /*
  1098. * Check readiness of the remote port. If the path to remote
  1099. * port is up, then send abts to the remote port to terminate
  1100. * the IO. Else, just locally terminate the IO in the firmware
  1101. */
  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 scsi_lun fc_lun;
  1201. struct scsi_device *lun_dev = lr_sc->device;
  1202. DECLARE_COMPLETION_ONSTACK(tm_done);
  1203. for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
  1204. sc = scsi_host_find_tag(fnic->lport->host, tag);
  1205. /*
  1206. * ignore this lun reset cmd or cmds that do not belong to
  1207. * this lun
  1208. */
  1209. if (!sc || sc == lr_sc || sc->device != lun_dev)
  1210. continue;
  1211. io_lock = fnic_io_lock_hash(fnic, sc);
  1212. spin_lock_irqsave(io_lock, flags);
  1213. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1214. if (!io_req || sc->device != lun_dev) {
  1215. spin_unlock_irqrestore(io_lock, flags);
  1216. continue;
  1217. }
  1218. /*
  1219. * Found IO that is still pending with firmware and
  1220. * belongs to the LUN that we are resetting
  1221. */
  1222. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1223. "Found IO in %s on lun\n",
  1224. fnic_ioreq_state_to_str(CMD_STATE(sc)));
  1225. BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
  1226. CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
  1227. io_req->abts_done = &tm_done;
  1228. spin_unlock_irqrestore(io_lock, flags);
  1229. /* Now queue the abort command to firmware */
  1230. int_to_scsilun(sc->device->lun, &fc_lun);
  1231. if (fnic_queue_abort_io_req(fnic, tag,
  1232. FCPIO_ITMF_ABT_TASK_TERM,
  1233. fc_lun.scsi_lun, io_req)) {
  1234. spin_lock_irqsave(io_lock, flags);
  1235. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1236. if (io_req)
  1237. io_req->abts_done = NULL;
  1238. spin_unlock_irqrestore(io_lock, flags);
  1239. ret = 1;
  1240. goto clean_pending_aborts_end;
  1241. }
  1242. wait_for_completion_timeout(&tm_done,
  1243. msecs_to_jiffies
  1244. (fnic->config.ed_tov));
  1245. /* Recheck cmd state to check if it is now aborted */
  1246. spin_lock_irqsave(io_lock, flags);
  1247. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1248. if (!io_req) {
  1249. spin_unlock_irqrestore(io_lock, flags);
  1250. ret = 1;
  1251. goto clean_pending_aborts_end;
  1252. }
  1253. io_req->abts_done = NULL;
  1254. /* if abort is still pending with fw, fail */
  1255. if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
  1256. spin_unlock_irqrestore(io_lock, flags);
  1257. ret = 1;
  1258. goto clean_pending_aborts_end;
  1259. }
  1260. CMD_SP(sc) = NULL;
  1261. spin_unlock_irqrestore(io_lock, flags);
  1262. fnic_release_ioreq_buf(fnic, io_req, sc);
  1263. mempool_free(io_req, fnic->io_req_pool);
  1264. }
  1265. clean_pending_aborts_end:
  1266. return ret;
  1267. }
  1268. /*
  1269. * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
  1270. * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
  1271. * on the LUN.
  1272. */
  1273. int fnic_device_reset(struct scsi_cmnd *sc)
  1274. {
  1275. struct fc_lport *lp;
  1276. struct fnic *fnic;
  1277. struct fnic_io_req *io_req;
  1278. struct fc_rport *rport;
  1279. int status;
  1280. int ret = FAILED;
  1281. spinlock_t *io_lock;
  1282. unsigned long flags;
  1283. DECLARE_COMPLETION_ONSTACK(tm_done);
  1284. /* Wait for rport to unblock */
  1285. fc_block_scsi_eh(sc);
  1286. /* Get local-port, check ready and link up */
  1287. lp = shost_priv(sc->device->host);
  1288. fnic = lport_priv(lp);
  1289. rport = starget_to_rport(scsi_target(sc->device));
  1290. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1291. "Device reset called FCID 0x%x, LUN 0x%x\n",
  1292. rport->port_id, sc->device->lun);
  1293. if (lp->state != LPORT_ST_READY || !(lp->link_up))
  1294. goto fnic_device_reset_end;
  1295. /* Check if remote port up */
  1296. if (fc_remote_port_chkready(rport))
  1297. goto fnic_device_reset_end;
  1298. io_lock = fnic_io_lock_hash(fnic, sc);
  1299. spin_lock_irqsave(io_lock, flags);
  1300. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1301. /*
  1302. * If there is a io_req attached to this command, then use it,
  1303. * else allocate a new one.
  1304. */
  1305. if (!io_req) {
  1306. io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
  1307. if (!io_req) {
  1308. spin_unlock_irqrestore(io_lock, flags);
  1309. goto fnic_device_reset_end;
  1310. }
  1311. memset(io_req, 0, sizeof(*io_req));
  1312. io_req->port_id = rport->port_id;
  1313. CMD_SP(sc) = (char *)io_req;
  1314. }
  1315. io_req->dr_done = &tm_done;
  1316. CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
  1317. CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
  1318. spin_unlock_irqrestore(io_lock, flags);
  1319. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
  1320. sc->request->tag);
  1321. /*
  1322. * issue the device reset, if enqueue failed, clean up the ioreq
  1323. * and break assoc with scsi cmd
  1324. */
  1325. if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
  1326. spin_lock_irqsave(io_lock, flags);
  1327. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1328. if (io_req)
  1329. io_req->dr_done = NULL;
  1330. goto fnic_device_reset_clean;
  1331. }
  1332. /*
  1333. * Wait on the local completion for LUN reset. The io_req may be
  1334. * freed while we wait since we hold no lock.
  1335. */
  1336. wait_for_completion_timeout(&tm_done,
  1337. msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
  1338. spin_lock_irqsave(io_lock, flags);
  1339. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1340. if (!io_req) {
  1341. spin_unlock_irqrestore(io_lock, flags);
  1342. goto fnic_device_reset_end;
  1343. }
  1344. io_req->dr_done = NULL;
  1345. status = CMD_LR_STATUS(sc);
  1346. spin_unlock_irqrestore(io_lock, flags);
  1347. /*
  1348. * If lun reset not completed, bail out with failed. io_req
  1349. * gets cleaned up during higher levels of EH
  1350. */
  1351. if (status == FCPIO_INVALID_CODE) {
  1352. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1353. "Device reset timed out\n");
  1354. goto fnic_device_reset_end;
  1355. }
  1356. /* Completed, but not successful, clean up the io_req, return fail */
  1357. if (status != FCPIO_SUCCESS) {
  1358. spin_lock_irqsave(io_lock, flags);
  1359. FNIC_SCSI_DBG(KERN_DEBUG,
  1360. fnic->lport->host,
  1361. "Device reset completed - failed\n");
  1362. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1363. goto fnic_device_reset_clean;
  1364. }
  1365. /*
  1366. * Clean up any aborts on this lun that have still not
  1367. * completed. If any of these fail, then LUN reset fails.
  1368. * clean_pending_aborts cleans all cmds on this lun except
  1369. * the lun reset cmd. If all cmds get cleaned, the lun reset
  1370. * succeeds
  1371. */
  1372. if (fnic_clean_pending_aborts(fnic, sc)) {
  1373. spin_lock_irqsave(io_lock, flags);
  1374. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1375. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1376. "Device reset failed"
  1377. " since could not abort all IOs\n");
  1378. goto fnic_device_reset_clean;
  1379. }
  1380. /* Clean lun reset command */
  1381. spin_lock_irqsave(io_lock, flags);
  1382. io_req = (struct fnic_io_req *)CMD_SP(sc);
  1383. if (io_req)
  1384. /* Completed, and successful */
  1385. ret = SUCCESS;
  1386. fnic_device_reset_clean:
  1387. if (io_req)
  1388. CMD_SP(sc) = NULL;
  1389. spin_unlock_irqrestore(io_lock, flags);
  1390. if (io_req) {
  1391. fnic_release_ioreq_buf(fnic, io_req, sc);
  1392. mempool_free(io_req, fnic->io_req_pool);
  1393. }
  1394. fnic_device_reset_end:
  1395. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1396. "Returning from device reset %s\n",
  1397. (ret == SUCCESS) ?
  1398. "SUCCESS" : "FAILED");
  1399. return ret;
  1400. }
  1401. /* Clean up all IOs, clean up libFC local port */
  1402. int fnic_reset(struct Scsi_Host *shost)
  1403. {
  1404. struct fc_lport *lp;
  1405. struct fnic *fnic;
  1406. int ret = SUCCESS;
  1407. lp = shost_priv(shost);
  1408. fnic = lport_priv(lp);
  1409. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1410. "fnic_reset called\n");
  1411. /*
  1412. * Reset local port, this will clean up libFC exchanges,
  1413. * reset remote port sessions, and if link is up, begin flogi
  1414. */
  1415. if (lp->tt.lport_reset(lp))
  1416. ret = FAILED;
  1417. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1418. "Returning from fnic reset %s\n",
  1419. (ret == SUCCESS) ?
  1420. "SUCCESS" : "FAILED");
  1421. return ret;
  1422. }
  1423. /*
  1424. * SCSI Error handling calls driver's eh_host_reset if all prior
  1425. * error handling levels return FAILED. If host reset completes
  1426. * successfully, and if link is up, then Fabric login begins.
  1427. *
  1428. * Host Reset is the highest level of error recovery. If this fails, then
  1429. * host is offlined by SCSI.
  1430. *
  1431. */
  1432. int fnic_host_reset(struct scsi_cmnd *sc)
  1433. {
  1434. int ret;
  1435. unsigned long wait_host_tmo;
  1436. struct Scsi_Host *shost = sc->device->host;
  1437. struct fc_lport *lp = shost_priv(shost);
  1438. /*
  1439. * If fnic_reset is successful, wait for fabric login to complete
  1440. * scsi-ml tries to send a TUR to every device if host reset is
  1441. * successful, so before returning to scsi, fabric should be up
  1442. */
  1443. ret = fnic_reset(shost);
  1444. if (ret == SUCCESS) {
  1445. wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
  1446. ret = FAILED;
  1447. while (time_before(jiffies, wait_host_tmo)) {
  1448. if ((lp->state == LPORT_ST_READY) &&
  1449. (lp->link_up)) {
  1450. ret = SUCCESS;
  1451. break;
  1452. }
  1453. ssleep(1);
  1454. }
  1455. }
  1456. return ret;
  1457. }
  1458. /*
  1459. * This fxn is called from libFC when host is removed
  1460. */
  1461. void fnic_scsi_abort_io(struct fc_lport *lp)
  1462. {
  1463. int err = 0;
  1464. unsigned long flags;
  1465. enum fnic_state old_state;
  1466. struct fnic *fnic = lport_priv(lp);
  1467. DECLARE_COMPLETION_ONSTACK(remove_wait);
  1468. /* Issue firmware reset for fnic, wait for reset to complete */
  1469. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1470. fnic->remove_wait = &remove_wait;
  1471. old_state = fnic->state;
  1472. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  1473. fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
  1474. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1475. err = fnic_fw_reset_handler(fnic);
  1476. if (err) {
  1477. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1478. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  1479. fnic->state = old_state;
  1480. fnic->remove_wait = NULL;
  1481. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1482. return;
  1483. }
  1484. /* Wait for firmware reset to complete */
  1485. wait_for_completion_timeout(&remove_wait,
  1486. msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
  1487. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1488. fnic->remove_wait = NULL;
  1489. FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
  1490. "fnic_scsi_abort_io %s\n",
  1491. (fnic->state == FNIC_IN_ETH_MODE) ?
  1492. "SUCCESS" : "FAILED");
  1493. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1494. }
  1495. /*
  1496. * This fxn called from libFC to clean up driver IO state on link down
  1497. */
  1498. void fnic_scsi_cleanup(struct fc_lport *lp)
  1499. {
  1500. unsigned long flags;
  1501. enum fnic_state old_state;
  1502. struct fnic *fnic = lport_priv(lp);
  1503. /* issue fw reset */
  1504. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1505. old_state = fnic->state;
  1506. fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
  1507. fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
  1508. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1509. if (fnic_fw_reset_handler(fnic)) {
  1510. spin_lock_irqsave(&fnic->fnic_lock, flags);
  1511. if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
  1512. fnic->state = old_state;
  1513. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  1514. }
  1515. }
  1516. void fnic_empty_scsi_cleanup(struct fc_lport *lp)
  1517. {
  1518. }
  1519. void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
  1520. {
  1521. struct fnic *fnic = lport_priv(lp);
  1522. /* Non-zero sid, nothing to do */
  1523. if (sid)
  1524. goto call_fc_exch_mgr_reset;
  1525. if (did) {
  1526. fnic_rport_exch_reset(fnic, did);
  1527. goto call_fc_exch_mgr_reset;
  1528. }
  1529. /*
  1530. * sid = 0, did = 0
  1531. * link down or device being removed
  1532. */
  1533. if (!fnic->in_remove)
  1534. fnic_scsi_cleanup(lp);
  1535. else
  1536. fnic_scsi_abort_io(lp);
  1537. /* call libFC exch mgr reset to reset its exchanges */
  1538. call_fc_exch_mgr_reset:
  1539. fc_exch_mgr_reset(lp, sid, did);
  1540. }