fnic_scsi.c 48 KB

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