fnic_scsi.c 48 KB

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