fnic_scsi.c 48 KB

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