lpfc_hbadisc.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2007 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/kthread.h>
  24. #include <linux/interrupt.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_transport_fc.h>
  29. #include "lpfc_hw.h"
  30. #include "lpfc_disc.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_scsi.h"
  33. #include "lpfc.h"
  34. #include "lpfc_logmsg.h"
  35. #include "lpfc_crtn.h"
  36. #include "lpfc_vport.h"
  37. #include "lpfc_debugfs.h"
  38. /* AlpaArray for assignment of scsid for scan-down and bind_method */
  39. static uint8_t lpfcAlpaArray[] = {
  40. 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
  41. 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
  42. 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
  43. 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
  44. 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
  45. 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
  46. 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
  47. 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
  48. 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
  49. 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
  50. 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
  51. 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
  52. 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
  53. };
  54. static void lpfc_disc_timeout_handler(struct lpfc_vport *);
  55. void
  56. lpfc_terminate_rport_io(struct fc_rport *rport)
  57. {
  58. struct lpfc_rport_data *rdata;
  59. struct lpfc_nodelist * ndlp;
  60. struct lpfc_hba *phba;
  61. rdata = rport->dd_data;
  62. ndlp = rdata->pnode;
  63. if (!ndlp) {
  64. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  65. printk(KERN_ERR "Cannot find remote node"
  66. " to terminate I/O Data x%x\n",
  67. rport->port_id);
  68. return;
  69. }
  70. phba = ndlp->vport->phba;
  71. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  72. "rport terminate: sid:x%x did:x%x flg:x%x",
  73. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  74. if (ndlp->nlp_sid != NLP_NO_SID) {
  75. lpfc_sli_abort_iocb(ndlp->vport,
  76. &phba->sli.ring[phba->sli.fcp_ring],
  77. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  78. }
  79. /*
  80. * A device is normally blocked for rediscovery and unblocked when
  81. * devloss timeout happens. In case a vport is removed or driver
  82. * unloaded before devloss timeout happens, we need to unblock here.
  83. */
  84. scsi_target_unblock(&rport->dev);
  85. return;
  86. }
  87. /*
  88. * This function will be called when dev_loss_tmo fire.
  89. */
  90. void
  91. lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
  92. {
  93. struct lpfc_rport_data *rdata;
  94. struct lpfc_nodelist * ndlp;
  95. struct lpfc_vport *vport;
  96. struct lpfc_hba *phba;
  97. struct lpfc_work_evt *evtp;
  98. rdata = rport->dd_data;
  99. ndlp = rdata->pnode;
  100. if (!ndlp) {
  101. if (rport->scsi_target_id != -1) {
  102. printk(KERN_ERR "Cannot find remote node"
  103. " for rport in dev_loss_tmo_callbk x%x\n",
  104. rport->port_id);
  105. }
  106. return;
  107. }
  108. vport = ndlp->vport;
  109. phba = vport->phba;
  110. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  111. "rport devlosscb: sid:x%x did:x%x flg:x%x",
  112. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  113. evtp = &ndlp->dev_loss_evt;
  114. if (!list_empty(&evtp->evt_listp))
  115. return;
  116. spin_lock_irq(&phba->hbalock);
  117. evtp->evt_arg1 = ndlp;
  118. evtp->evt = LPFC_EVT_DEV_LOSS;
  119. list_add_tail(&evtp->evt_listp, &phba->work_list);
  120. if (phba->work_wait)
  121. wake_up(phba->work_wait);
  122. spin_unlock_irq(&phba->hbalock);
  123. return;
  124. }
  125. /*
  126. * This function is called from the worker thread when dev_loss_tmo
  127. * expire.
  128. */
  129. void
  130. lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
  131. {
  132. struct lpfc_rport_data *rdata;
  133. struct fc_rport *rport;
  134. struct lpfc_vport *vport;
  135. struct lpfc_hba *phba;
  136. uint8_t *name;
  137. int put_node;
  138. int put_rport;
  139. int warn_on = 0;
  140. rport = ndlp->rport;
  141. if (!rport)
  142. return;
  143. rdata = rport->dd_data;
  144. name = (uint8_t *) &ndlp->nlp_portname;
  145. vport = ndlp->vport;
  146. phba = vport->phba;
  147. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  148. "rport devlosstmo:did:x%x type:x%x id:x%x",
  149. ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
  150. if (!(vport->load_flag & FC_UNLOADING) &&
  151. ndlp->nlp_state == NLP_STE_MAPPED_NODE)
  152. return;
  153. if (ndlp->nlp_type & NLP_FABRIC) {
  154. /* We will clean up these Nodes in linkup */
  155. put_node = rdata->pnode != NULL;
  156. put_rport = ndlp->rport != NULL;
  157. rdata->pnode = NULL;
  158. ndlp->rport = NULL;
  159. if (put_node)
  160. lpfc_nlp_put(ndlp);
  161. if (put_rport)
  162. put_device(&rport->dev);
  163. return;
  164. }
  165. if (ndlp->nlp_sid != NLP_NO_SID) {
  166. warn_on = 1;
  167. /* flush the target */
  168. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  169. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  170. }
  171. if (vport->load_flag & FC_UNLOADING)
  172. warn_on = 0;
  173. if (warn_on) {
  174. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  175. "0203 Devloss timeout on "
  176. "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
  177. "NPort x%x Data: x%x x%x x%x\n",
  178. *name, *(name+1), *(name+2), *(name+3),
  179. *(name+4), *(name+5), *(name+6), *(name+7),
  180. ndlp->nlp_DID, ndlp->nlp_flag,
  181. ndlp->nlp_state, ndlp->nlp_rpi);
  182. } else {
  183. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  184. "0204 Devloss timeout on "
  185. "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
  186. "NPort x%x Data: x%x x%x x%x\n",
  187. *name, *(name+1), *(name+2), *(name+3),
  188. *(name+4), *(name+5), *(name+6), *(name+7),
  189. ndlp->nlp_DID, ndlp->nlp_flag,
  190. ndlp->nlp_state, ndlp->nlp_rpi);
  191. }
  192. put_node = rdata->pnode != NULL;
  193. put_rport = ndlp->rport != NULL;
  194. rdata->pnode = NULL;
  195. ndlp->rport = NULL;
  196. if (put_node)
  197. lpfc_nlp_put(ndlp);
  198. if (put_rport)
  199. put_device(&rport->dev);
  200. if (!(vport->load_flag & FC_UNLOADING) &&
  201. !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
  202. !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
  203. (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)) {
  204. lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
  205. }
  206. }
  207. void
  208. lpfc_worker_wake_up(struct lpfc_hba *phba)
  209. {
  210. wake_up(phba->work_wait);
  211. return;
  212. }
  213. static void
  214. lpfc_work_list_done(struct lpfc_hba *phba)
  215. {
  216. struct lpfc_work_evt *evtp = NULL;
  217. struct lpfc_nodelist *ndlp;
  218. int free_evt;
  219. spin_lock_irq(&phba->hbalock);
  220. while (!list_empty(&phba->work_list)) {
  221. list_remove_head((&phba->work_list), evtp, typeof(*evtp),
  222. evt_listp);
  223. spin_unlock_irq(&phba->hbalock);
  224. free_evt = 1;
  225. switch (evtp->evt) {
  226. case LPFC_EVT_ELS_RETRY:
  227. ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
  228. lpfc_els_retry_delay_handler(ndlp);
  229. free_evt = 0; /* evt is part of ndlp */
  230. break;
  231. case LPFC_EVT_DEV_LOSS:
  232. ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
  233. lpfc_nlp_get(ndlp);
  234. lpfc_dev_loss_tmo_handler(ndlp);
  235. free_evt = 0;
  236. lpfc_nlp_put(ndlp);
  237. break;
  238. case LPFC_EVT_ONLINE:
  239. if (phba->link_state < LPFC_LINK_DOWN)
  240. *(int *) (evtp->evt_arg1) = lpfc_online(phba);
  241. else
  242. *(int *) (evtp->evt_arg1) = 0;
  243. complete((struct completion *)(evtp->evt_arg2));
  244. break;
  245. case LPFC_EVT_OFFLINE_PREP:
  246. if (phba->link_state >= LPFC_LINK_DOWN)
  247. lpfc_offline_prep(phba);
  248. *(int *)(evtp->evt_arg1) = 0;
  249. complete((struct completion *)(evtp->evt_arg2));
  250. break;
  251. case LPFC_EVT_OFFLINE:
  252. lpfc_offline(phba);
  253. lpfc_sli_brdrestart(phba);
  254. *(int *)(evtp->evt_arg1) =
  255. lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
  256. lpfc_unblock_mgmt_io(phba);
  257. complete((struct completion *)(evtp->evt_arg2));
  258. break;
  259. case LPFC_EVT_WARM_START:
  260. lpfc_offline(phba);
  261. lpfc_reset_barrier(phba);
  262. lpfc_sli_brdreset(phba);
  263. lpfc_hba_down_post(phba);
  264. *(int *)(evtp->evt_arg1) =
  265. lpfc_sli_brdready(phba, HS_MBRDY);
  266. lpfc_unblock_mgmt_io(phba);
  267. complete((struct completion *)(evtp->evt_arg2));
  268. break;
  269. case LPFC_EVT_KILL:
  270. lpfc_offline(phba);
  271. *(int *)(evtp->evt_arg1)
  272. = (phba->pport->stopped)
  273. ? 0 : lpfc_sli_brdkill(phba);
  274. lpfc_unblock_mgmt_io(phba);
  275. complete((struct completion *)(evtp->evt_arg2));
  276. break;
  277. }
  278. if (free_evt)
  279. kfree(evtp);
  280. spin_lock_irq(&phba->hbalock);
  281. }
  282. spin_unlock_irq(&phba->hbalock);
  283. }
  284. static void
  285. lpfc_work_done(struct lpfc_hba *phba)
  286. {
  287. struct lpfc_sli_ring *pring;
  288. uint32_t ha_copy, status, control, work_port_events;
  289. struct lpfc_vport **vports;
  290. struct lpfc_vport *vport;
  291. int i;
  292. spin_lock_irq(&phba->hbalock);
  293. ha_copy = phba->work_ha;
  294. phba->work_ha = 0;
  295. spin_unlock_irq(&phba->hbalock);
  296. if (ha_copy & HA_ERATT)
  297. lpfc_handle_eratt(phba);
  298. if (ha_copy & HA_MBATT)
  299. lpfc_sli_handle_mb_event(phba);
  300. if (ha_copy & HA_LATT)
  301. lpfc_handle_latt(phba);
  302. vports = lpfc_create_vport_work_array(phba);
  303. if (vports != NULL)
  304. for(i = 0; i < LPFC_MAX_VPORTS; i++) {
  305. /*
  306. * We could have no vports in array if unloading, so if
  307. * this happens then just use the pport
  308. */
  309. if (vports[i] == NULL && i == 0)
  310. vport = phba->pport;
  311. else
  312. vport = vports[i];
  313. if (vport == NULL)
  314. break;
  315. work_port_events = vport->work_port_events;
  316. if (work_port_events & WORKER_DISC_TMO)
  317. lpfc_disc_timeout_handler(vport);
  318. if (work_port_events & WORKER_ELS_TMO)
  319. lpfc_els_timeout_handler(vport);
  320. if (work_port_events & WORKER_HB_TMO)
  321. lpfc_hb_timeout_handler(phba);
  322. if (work_port_events & WORKER_MBOX_TMO)
  323. lpfc_mbox_timeout_handler(phba);
  324. if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
  325. lpfc_unblock_fabric_iocbs(phba);
  326. if (work_port_events & WORKER_FDMI_TMO)
  327. lpfc_fdmi_timeout_handler(vport);
  328. if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
  329. lpfc_ramp_down_queue_handler(phba);
  330. if (work_port_events & WORKER_RAMP_UP_QUEUE)
  331. lpfc_ramp_up_queue_handler(phba);
  332. spin_lock_irq(&vport->work_port_lock);
  333. vport->work_port_events &= ~work_port_events;
  334. spin_unlock_irq(&vport->work_port_lock);
  335. }
  336. lpfc_destroy_vport_work_array(vports);
  337. pring = &phba->sli.ring[LPFC_ELS_RING];
  338. status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  339. status >>= (4*LPFC_ELS_RING);
  340. if ((status & HA_RXMASK)
  341. || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
  342. if (pring->flag & LPFC_STOP_IOCB_EVENT) {
  343. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  344. } else {
  345. lpfc_sli_handle_slow_ring_event(phba, pring,
  346. (status &
  347. HA_RXMASK));
  348. pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
  349. }
  350. /*
  351. * Turn on Ring interrupts
  352. */
  353. spin_lock_irq(&phba->hbalock);
  354. control = readl(phba->HCregaddr);
  355. if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
  356. lpfc_debugfs_slow_ring_trc(phba,
  357. "WRK Enable ring: cntl:x%x hacopy:x%x",
  358. control, ha_copy, 0);
  359. control |= (HC_R0INT_ENA << LPFC_ELS_RING);
  360. writel(control, phba->HCregaddr);
  361. readl(phba->HCregaddr); /* flush */
  362. }
  363. else {
  364. lpfc_debugfs_slow_ring_trc(phba,
  365. "WRK Ring ok: cntl:x%x hacopy:x%x",
  366. control, ha_copy, 0);
  367. }
  368. spin_unlock_irq(&phba->hbalock);
  369. }
  370. lpfc_work_list_done(phba);
  371. }
  372. static int
  373. check_work_wait_done(struct lpfc_hba *phba)
  374. {
  375. struct lpfc_vport *vport;
  376. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  377. int rc = 0;
  378. spin_lock_irq(&phba->hbalock);
  379. list_for_each_entry(vport, &phba->port_list, listentry) {
  380. if (vport->work_port_events) {
  381. rc = 1;
  382. break;
  383. }
  384. }
  385. if (rc || phba->work_ha || (!list_empty(&phba->work_list)) ||
  386. kthread_should_stop() || pring->flag & LPFC_DEFERRED_RING_EVENT) {
  387. rc = 1;
  388. phba->work_found++;
  389. } else
  390. phba->work_found = 0;
  391. spin_unlock_irq(&phba->hbalock);
  392. return rc;
  393. }
  394. int
  395. lpfc_do_work(void *p)
  396. {
  397. struct lpfc_hba *phba = p;
  398. int rc;
  399. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
  400. set_user_nice(current, -20);
  401. phba->work_wait = &work_waitq;
  402. phba->work_found = 0;
  403. while (1) {
  404. rc = wait_event_interruptible(work_waitq,
  405. check_work_wait_done(phba));
  406. BUG_ON(rc);
  407. if (kthread_should_stop())
  408. break;
  409. lpfc_work_done(phba);
  410. /* If there is alot of slow ring work, like during link up
  411. * check_work_wait_done() may cause this thread to not give
  412. * up the CPU for very long periods of time. This may cause
  413. * soft lockups or other problems. To avoid these situations
  414. * give up the CPU here after LPFC_MAX_WORKER_ITERATION
  415. * consecutive iterations.
  416. */
  417. if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
  418. phba->work_found = 0;
  419. schedule();
  420. }
  421. }
  422. phba->work_wait = NULL;
  423. return 0;
  424. }
  425. /*
  426. * This is only called to handle FC worker events. Since this a rare
  427. * occurance, we allocate a struct lpfc_work_evt structure here instead of
  428. * embedding it in the IOCB.
  429. */
  430. int
  431. lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
  432. uint32_t evt)
  433. {
  434. struct lpfc_work_evt *evtp;
  435. unsigned long flags;
  436. /*
  437. * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
  438. * be queued to worker thread for processing
  439. */
  440. evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
  441. if (!evtp)
  442. return 0;
  443. evtp->evt_arg1 = arg1;
  444. evtp->evt_arg2 = arg2;
  445. evtp->evt = evt;
  446. spin_lock_irqsave(&phba->hbalock, flags);
  447. list_add_tail(&evtp->evt_listp, &phba->work_list);
  448. if (phba->work_wait)
  449. lpfc_worker_wake_up(phba);
  450. spin_unlock_irqrestore(&phba->hbalock, flags);
  451. return 1;
  452. }
  453. void
  454. lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
  455. {
  456. struct lpfc_hba *phba = vport->phba;
  457. struct lpfc_nodelist *ndlp, *next_ndlp;
  458. int rc;
  459. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  460. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  461. continue;
  462. if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
  463. ((vport->port_type == LPFC_NPIV_PORT) &&
  464. (ndlp->nlp_DID == NameServer_DID)))
  465. lpfc_unreg_rpi(vport, ndlp);
  466. /* Leave Fabric nodes alone on link down */
  467. if (!remove && ndlp->nlp_type & NLP_FABRIC)
  468. continue;
  469. rc = lpfc_disc_state_machine(vport, ndlp, NULL,
  470. remove
  471. ? NLP_EVT_DEVICE_RM
  472. : NLP_EVT_DEVICE_RECOVERY);
  473. }
  474. if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
  475. lpfc_mbx_unreg_vpi(vport);
  476. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  477. }
  478. }
  479. void
  480. lpfc_port_link_failure(struct lpfc_vport *vport)
  481. {
  482. /* Cleanup any outstanding RSCN activity */
  483. lpfc_els_flush_rscn(vport);
  484. /* Cleanup any outstanding ELS commands */
  485. lpfc_els_flush_cmd(vport);
  486. lpfc_cleanup_rpis(vport, 0);
  487. /* Turn off discovery timer if its running */
  488. lpfc_can_disctmo(vport);
  489. }
  490. static void
  491. lpfc_linkdown_port(struct lpfc_vport *vport)
  492. {
  493. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  494. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  495. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  496. "Link Down: state:x%x rtry:x%x flg:x%x",
  497. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  498. lpfc_port_link_failure(vport);
  499. }
  500. int
  501. lpfc_linkdown(struct lpfc_hba *phba)
  502. {
  503. struct lpfc_vport *vport = phba->pport;
  504. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  505. struct lpfc_vport **vports;
  506. LPFC_MBOXQ_t *mb;
  507. int i;
  508. if (phba->link_state == LPFC_LINK_DOWN) {
  509. return 0;
  510. }
  511. spin_lock_irq(&phba->hbalock);
  512. if (phba->link_state > LPFC_LINK_DOWN) {
  513. phba->link_state = LPFC_LINK_DOWN;
  514. phba->pport->fc_flag &= ~FC_LBIT;
  515. }
  516. spin_unlock_irq(&phba->hbalock);
  517. vports = lpfc_create_vport_work_array(phba);
  518. if (vports != NULL)
  519. for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
  520. /* Issue a LINK DOWN event to all nodes */
  521. lpfc_linkdown_port(vports[i]);
  522. }
  523. lpfc_destroy_vport_work_array(vports);
  524. /* Clean up any firmware default rpi's */
  525. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  526. if (mb) {
  527. lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
  528. mb->vport = vport;
  529. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  530. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  531. == MBX_NOT_FINISHED) {
  532. mempool_free(mb, phba->mbox_mem_pool);
  533. }
  534. }
  535. /* Setup myDID for link up if we are in pt2pt mode */
  536. if (phba->pport->fc_flag & FC_PT2PT) {
  537. phba->pport->fc_myDID = 0;
  538. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  539. if (mb) {
  540. lpfc_config_link(phba, mb);
  541. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  542. mb->vport = vport;
  543. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  544. == MBX_NOT_FINISHED) {
  545. mempool_free(mb, phba->mbox_mem_pool);
  546. }
  547. }
  548. spin_lock_irq(shost->host_lock);
  549. phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
  550. spin_unlock_irq(shost->host_lock);
  551. }
  552. return 0;
  553. }
  554. static void
  555. lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
  556. {
  557. struct lpfc_nodelist *ndlp;
  558. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  559. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  560. continue;
  561. if (ndlp->nlp_type & NLP_FABRIC) {
  562. /* On Linkup its safe to clean up the ndlp
  563. * from Fabric connections.
  564. */
  565. if (ndlp->nlp_DID != Fabric_DID)
  566. lpfc_unreg_rpi(vport, ndlp);
  567. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  568. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  569. /* Fail outstanding IO now since device is
  570. * marked for PLOGI.
  571. */
  572. lpfc_unreg_rpi(vport, ndlp);
  573. }
  574. }
  575. }
  576. static void
  577. lpfc_linkup_port(struct lpfc_vport *vport)
  578. {
  579. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  580. struct lpfc_hba *phba = vport->phba;
  581. if ((vport->load_flag & FC_UNLOADING) != 0)
  582. return;
  583. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  584. "Link Up: top:x%x speed:x%x flg:x%x",
  585. phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
  586. /* If NPIV is not enabled, only bring the physical port up */
  587. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  588. (vport != phba->pport))
  589. return;
  590. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  591. spin_lock_irq(shost->host_lock);
  592. vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
  593. FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
  594. vport->fc_flag |= FC_NDISC_ACTIVE;
  595. vport->fc_ns_retry = 0;
  596. spin_unlock_irq(shost->host_lock);
  597. if (vport->fc_flag & FC_LBIT)
  598. lpfc_linkup_cleanup_nodes(vport);
  599. }
  600. static int
  601. lpfc_linkup(struct lpfc_hba *phba)
  602. {
  603. struct lpfc_vport **vports;
  604. int i;
  605. phba->link_state = LPFC_LINK_UP;
  606. /* Unblock fabric iocbs if they are blocked */
  607. clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
  608. del_timer_sync(&phba->fabric_block_timer);
  609. vports = lpfc_create_vport_work_array(phba);
  610. if (vports != NULL)
  611. for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++)
  612. lpfc_linkup_port(vports[i]);
  613. lpfc_destroy_vport_work_array(vports);
  614. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  615. lpfc_issue_clear_la(phba, phba->pport);
  616. return 0;
  617. }
  618. /*
  619. * This routine handles processing a CLEAR_LA mailbox
  620. * command upon completion. It is setup in the LPFC_MBOXQ
  621. * as the completion routine when the command is
  622. * handed off to the SLI layer.
  623. */
  624. void
  625. lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  626. {
  627. struct lpfc_vport *vport = pmb->vport;
  628. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  629. struct lpfc_sli *psli = &phba->sli;
  630. MAILBOX_t *mb = &pmb->mb;
  631. uint32_t control;
  632. /* Since we don't do discovery right now, turn these off here */
  633. psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  634. psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  635. psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  636. /* Check for error */
  637. if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
  638. /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
  639. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  640. "0320 CLEAR_LA mbxStatus error x%x hba "
  641. "state x%x\n",
  642. mb->mbxStatus, vport->port_state);
  643. phba->link_state = LPFC_HBA_ERROR;
  644. goto out;
  645. }
  646. if (vport->port_type == LPFC_PHYSICAL_PORT)
  647. phba->link_state = LPFC_HBA_READY;
  648. spin_lock_irq(&phba->hbalock);
  649. psli->sli_flag |= LPFC_PROCESS_LA;
  650. control = readl(phba->HCregaddr);
  651. control |= HC_LAINT_ENA;
  652. writel(control, phba->HCregaddr);
  653. readl(phba->HCregaddr); /* flush */
  654. spin_unlock_irq(&phba->hbalock);
  655. return;
  656. vport->num_disc_nodes = 0;
  657. /* go thru NPR nodes and issue ELS PLOGIs */
  658. if (vport->fc_npr_cnt)
  659. lpfc_els_disc_plogi(vport);
  660. if (!vport->num_disc_nodes) {
  661. spin_lock_irq(shost->host_lock);
  662. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  663. spin_unlock_irq(shost->host_lock);
  664. }
  665. vport->port_state = LPFC_VPORT_READY;
  666. out:
  667. /* Device Discovery completes */
  668. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  669. "0225 Device Discovery completes\n");
  670. mempool_free(pmb, phba->mbox_mem_pool);
  671. spin_lock_irq(shost->host_lock);
  672. vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
  673. spin_unlock_irq(shost->host_lock);
  674. del_timer_sync(&phba->fc_estabtmo);
  675. lpfc_can_disctmo(vport);
  676. /* turn on Link Attention interrupts */
  677. spin_lock_irq(&phba->hbalock);
  678. psli->sli_flag |= LPFC_PROCESS_LA;
  679. control = readl(phba->HCregaddr);
  680. control |= HC_LAINT_ENA;
  681. writel(control, phba->HCregaddr);
  682. readl(phba->HCregaddr); /* flush */
  683. spin_unlock_irq(&phba->hbalock);
  684. return;
  685. }
  686. static void
  687. lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  688. {
  689. struct lpfc_vport *vport = pmb->vport;
  690. if (pmb->mb.mbxStatus)
  691. goto out;
  692. mempool_free(pmb, phba->mbox_mem_pool);
  693. if (phba->fc_topology == TOPOLOGY_LOOP &&
  694. vport->fc_flag & FC_PUBLIC_LOOP &&
  695. !(vport->fc_flag & FC_LBIT)) {
  696. /* Need to wait for FAN - use discovery timer
  697. * for timeout. port_state is identically
  698. * LPFC_LOCAL_CFG_LINK while waiting for FAN
  699. */
  700. lpfc_set_disctmo(vport);
  701. return;
  702. }
  703. /* Start discovery by sending a FLOGI. port_state is identically
  704. * LPFC_FLOGI while waiting for FLOGI cmpl
  705. */
  706. if (vport->port_state != LPFC_FLOGI) {
  707. lpfc_initial_flogi(vport);
  708. }
  709. return;
  710. out:
  711. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  712. "0306 CONFIG_LINK mbxStatus error x%x "
  713. "HBA state x%x\n",
  714. pmb->mb.mbxStatus, vport->port_state);
  715. mempool_free(pmb, phba->mbox_mem_pool);
  716. lpfc_linkdown(phba);
  717. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  718. "0200 CONFIG_LINK bad hba state x%x\n",
  719. vport->port_state);
  720. lpfc_issue_clear_la(phba, vport);
  721. return;
  722. }
  723. static void
  724. lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  725. {
  726. MAILBOX_t *mb = &pmb->mb;
  727. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
  728. struct lpfc_vport *vport = pmb->vport;
  729. /* Check for error */
  730. if (mb->mbxStatus) {
  731. /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
  732. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  733. "0319 READ_SPARAM mbxStatus error x%x "
  734. "hba state x%x>\n",
  735. mb->mbxStatus, vport->port_state);
  736. lpfc_linkdown(phba);
  737. goto out;
  738. }
  739. memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
  740. sizeof (struct serv_parm));
  741. if (phba->cfg_soft_wwnn)
  742. u64_to_wwn(phba->cfg_soft_wwnn,
  743. vport->fc_sparam.nodeName.u.wwn);
  744. if (phba->cfg_soft_wwpn)
  745. u64_to_wwn(phba->cfg_soft_wwpn,
  746. vport->fc_sparam.portName.u.wwn);
  747. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  748. sizeof(vport->fc_nodename));
  749. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  750. sizeof(vport->fc_portname));
  751. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  752. memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
  753. memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
  754. }
  755. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  756. kfree(mp);
  757. mempool_free(pmb, phba->mbox_mem_pool);
  758. return;
  759. out:
  760. pmb->context1 = NULL;
  761. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  762. kfree(mp);
  763. lpfc_issue_clear_la(phba, vport);
  764. mempool_free(pmb, phba->mbox_mem_pool);
  765. return;
  766. }
  767. static void
  768. lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
  769. {
  770. struct lpfc_vport *vport = phba->pport;
  771. LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
  772. int i;
  773. struct lpfc_dmabuf *mp;
  774. int rc;
  775. sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  776. cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  777. spin_lock_irq(&phba->hbalock);
  778. switch (la->UlnkSpeed) {
  779. case LA_1GHZ_LINK:
  780. phba->fc_linkspeed = LA_1GHZ_LINK;
  781. break;
  782. case LA_2GHZ_LINK:
  783. phba->fc_linkspeed = LA_2GHZ_LINK;
  784. break;
  785. case LA_4GHZ_LINK:
  786. phba->fc_linkspeed = LA_4GHZ_LINK;
  787. break;
  788. case LA_8GHZ_LINK:
  789. phba->fc_linkspeed = LA_8GHZ_LINK;
  790. break;
  791. default:
  792. phba->fc_linkspeed = LA_UNKNW_LINK;
  793. break;
  794. }
  795. phba->fc_topology = la->topology;
  796. phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  797. if (phba->fc_topology == TOPOLOGY_LOOP) {
  798. phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  799. /* Get Loop Map information */
  800. if (la->il)
  801. vport->fc_flag |= FC_LBIT;
  802. vport->fc_myDID = la->granted_AL_PA;
  803. i = la->un.lilpBde64.tus.f.bdeSize;
  804. if (i == 0) {
  805. phba->alpa_map[0] = 0;
  806. } else {
  807. if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
  808. int numalpa, j, k;
  809. union {
  810. uint8_t pamap[16];
  811. struct {
  812. uint32_t wd1;
  813. uint32_t wd2;
  814. uint32_t wd3;
  815. uint32_t wd4;
  816. } pa;
  817. } un;
  818. numalpa = phba->alpa_map[0];
  819. j = 0;
  820. while (j < numalpa) {
  821. memset(un.pamap, 0, 16);
  822. for (k = 1; j < numalpa; k++) {
  823. un.pamap[k - 1] =
  824. phba->alpa_map[j + 1];
  825. j++;
  826. if (k == 16)
  827. break;
  828. }
  829. /* Link Up Event ALPA map */
  830. lpfc_printf_log(phba,
  831. KERN_WARNING,
  832. LOG_LINK_EVENT,
  833. "1304 Link Up Event "
  834. "ALPA map Data: x%x "
  835. "x%x x%x x%x\n",
  836. un.pa.wd1, un.pa.wd2,
  837. un.pa.wd3, un.pa.wd4);
  838. }
  839. }
  840. }
  841. } else {
  842. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
  843. if (phba->max_vpi && phba->cfg_enable_npiv &&
  844. (phba->sli_rev == 3))
  845. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  846. }
  847. vport->fc_myDID = phba->fc_pref_DID;
  848. vport->fc_flag |= FC_LBIT;
  849. }
  850. spin_unlock_irq(&phba->hbalock);
  851. lpfc_linkup(phba);
  852. if (sparam_mbox) {
  853. lpfc_read_sparam(phba, sparam_mbox, 0);
  854. sparam_mbox->vport = vport;
  855. sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
  856. rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
  857. if (rc == MBX_NOT_FINISHED) {
  858. mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
  859. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  860. kfree(mp);
  861. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  862. if (cfglink_mbox)
  863. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  864. goto out;
  865. }
  866. }
  867. if (cfglink_mbox) {
  868. vport->port_state = LPFC_LOCAL_CFG_LINK;
  869. lpfc_config_link(phba, cfglink_mbox);
  870. cfglink_mbox->vport = vport;
  871. cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  872. rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
  873. if (rc != MBX_NOT_FINISHED)
  874. return;
  875. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  876. }
  877. out:
  878. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  879. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  880. "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
  881. vport->port_state, sparam_mbox, cfglink_mbox);
  882. lpfc_issue_clear_la(phba, vport);
  883. return;
  884. }
  885. static void
  886. lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
  887. {
  888. uint32_t control;
  889. struct lpfc_sli *psli = &phba->sli;
  890. lpfc_linkdown(phba);
  891. /* turn on Link Attention interrupts - no CLEAR_LA needed */
  892. spin_lock_irq(&phba->hbalock);
  893. psli->sli_flag |= LPFC_PROCESS_LA;
  894. control = readl(phba->HCregaddr);
  895. control |= HC_LAINT_ENA;
  896. writel(control, phba->HCregaddr);
  897. readl(phba->HCregaddr); /* flush */
  898. spin_unlock_irq(&phba->hbalock);
  899. }
  900. /*
  901. * This routine handles processing a READ_LA mailbox
  902. * command upon completion. It is setup in the LPFC_MBOXQ
  903. * as the completion routine when the command is
  904. * handed off to the SLI layer.
  905. */
  906. void
  907. lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  908. {
  909. struct lpfc_vport *vport = pmb->vport;
  910. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  911. READ_LA_VAR *la;
  912. MAILBOX_t *mb = &pmb->mb;
  913. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  914. /* Check for error */
  915. if (mb->mbxStatus) {
  916. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  917. "1307 READ_LA mbox error x%x state x%x\n",
  918. mb->mbxStatus, vport->port_state);
  919. lpfc_mbx_issue_link_down(phba);
  920. phba->link_state = LPFC_HBA_ERROR;
  921. goto lpfc_mbx_cmpl_read_la_free_mbuf;
  922. }
  923. la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
  924. memcpy(&phba->alpa_map[0], mp->virt, 128);
  925. spin_lock_irq(shost->host_lock);
  926. if (la->pb)
  927. vport->fc_flag |= FC_BYPASSED_MODE;
  928. else
  929. vport->fc_flag &= ~FC_BYPASSED_MODE;
  930. spin_unlock_irq(shost->host_lock);
  931. if (((phba->fc_eventTag + 1) < la->eventTag) ||
  932. (phba->fc_eventTag == la->eventTag)) {
  933. phba->fc_stat.LinkMultiEvent++;
  934. if (la->attType == AT_LINK_UP)
  935. if (phba->fc_eventTag != 0)
  936. lpfc_linkdown(phba);
  937. }
  938. phba->fc_eventTag = la->eventTag;
  939. if (la->attType == AT_LINK_UP) {
  940. phba->fc_stat.LinkUp++;
  941. if (phba->link_flag & LS_LOOPBACK_MODE) {
  942. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  943. "1306 Link Up Event in loop back mode "
  944. "x%x received Data: x%x x%x x%x x%x\n",
  945. la->eventTag, phba->fc_eventTag,
  946. la->granted_AL_PA, la->UlnkSpeed,
  947. phba->alpa_map[0]);
  948. } else {
  949. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  950. "1303 Link Up Event x%x received "
  951. "Data: x%x x%x x%x x%x\n",
  952. la->eventTag, phba->fc_eventTag,
  953. la->granted_AL_PA, la->UlnkSpeed,
  954. phba->alpa_map[0]);
  955. }
  956. lpfc_mbx_process_link_up(phba, la);
  957. } else {
  958. phba->fc_stat.LinkDown++;
  959. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  960. "1305 Link Down Event x%x received "
  961. "Data: x%x x%x x%x\n",
  962. la->eventTag, phba->fc_eventTag,
  963. phba->pport->port_state, vport->fc_flag);
  964. lpfc_mbx_issue_link_down(phba);
  965. }
  966. lpfc_mbx_cmpl_read_la_free_mbuf:
  967. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  968. kfree(mp);
  969. mempool_free(pmb, phba->mbox_mem_pool);
  970. return;
  971. }
  972. /*
  973. * This routine handles processing a REG_LOGIN mailbox
  974. * command upon completion. It is setup in the LPFC_MBOXQ
  975. * as the completion routine when the command is
  976. * handed off to the SLI layer.
  977. */
  978. void
  979. lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  980. {
  981. struct lpfc_vport *vport = pmb->vport;
  982. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  983. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  984. pmb->context1 = NULL;
  985. /* Good status, call state machine */
  986. lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
  987. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  988. kfree(mp);
  989. mempool_free(pmb, phba->mbox_mem_pool);
  990. lpfc_nlp_put(ndlp);
  991. return;
  992. }
  993. static void
  994. lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  995. {
  996. MAILBOX_t *mb = &pmb->mb;
  997. struct lpfc_vport *vport = pmb->vport;
  998. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  999. switch (mb->mbxStatus) {
  1000. case 0x0011:
  1001. case 0x0020:
  1002. case 0x9700:
  1003. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1004. "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
  1005. mb->mbxStatus);
  1006. break;
  1007. }
  1008. vport->unreg_vpi_cmpl = VPORT_OK;
  1009. mempool_free(pmb, phba->mbox_mem_pool);
  1010. /*
  1011. * This shost reference might have been taken at the beginning of
  1012. * lpfc_vport_delete()
  1013. */
  1014. if (vport->load_flag & FC_UNLOADING)
  1015. scsi_host_put(shost);
  1016. }
  1017. void
  1018. lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
  1019. {
  1020. struct lpfc_hba *phba = vport->phba;
  1021. LPFC_MBOXQ_t *mbox;
  1022. int rc;
  1023. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1024. if (!mbox)
  1025. return;
  1026. lpfc_unreg_vpi(phba, vport->vpi, mbox);
  1027. mbox->vport = vport;
  1028. mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
  1029. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1030. if (rc == MBX_NOT_FINISHED) {
  1031. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  1032. "1800 Could not issue unreg_vpi\n");
  1033. mempool_free(mbox, phba->mbox_mem_pool);
  1034. vport->unreg_vpi_cmpl = VPORT_ERROR;
  1035. }
  1036. }
  1037. static void
  1038. lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1039. {
  1040. struct lpfc_vport *vport = pmb->vport;
  1041. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1042. MAILBOX_t *mb = &pmb->mb;
  1043. switch (mb->mbxStatus) {
  1044. case 0x0011:
  1045. case 0x9601:
  1046. case 0x9602:
  1047. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1048. "0912 cmpl_reg_vpi, mb status = 0x%x\n",
  1049. mb->mbxStatus);
  1050. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1051. spin_lock_irq(shost->host_lock);
  1052. vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
  1053. spin_unlock_irq(shost->host_lock);
  1054. vport->fc_myDID = 0;
  1055. goto out;
  1056. }
  1057. vport->num_disc_nodes = 0;
  1058. /* go thru NPR list and issue ELS PLOGIs */
  1059. if (vport->fc_npr_cnt)
  1060. lpfc_els_disc_plogi(vport);
  1061. if (!vport->num_disc_nodes) {
  1062. spin_lock_irq(shost->host_lock);
  1063. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  1064. spin_unlock_irq(shost->host_lock);
  1065. lpfc_can_disctmo(vport);
  1066. }
  1067. vport->port_state = LPFC_VPORT_READY;
  1068. out:
  1069. mempool_free(pmb, phba->mbox_mem_pool);
  1070. return;
  1071. }
  1072. /*
  1073. * This routine handles processing a Fabric REG_LOGIN mailbox
  1074. * command upon completion. It is setup in the LPFC_MBOXQ
  1075. * as the completion routine when the command is
  1076. * handed off to the SLI layer.
  1077. */
  1078. void
  1079. lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1080. {
  1081. struct lpfc_vport *vport = pmb->vport;
  1082. MAILBOX_t *mb = &pmb->mb;
  1083. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  1084. struct lpfc_nodelist *ndlp;
  1085. struct lpfc_vport **vports;
  1086. int i;
  1087. ndlp = (struct lpfc_nodelist *) pmb->context2;
  1088. pmb->context1 = NULL;
  1089. pmb->context2 = NULL;
  1090. if (mb->mbxStatus) {
  1091. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1092. kfree(mp);
  1093. mempool_free(pmb, phba->mbox_mem_pool);
  1094. lpfc_nlp_put(ndlp);
  1095. if (phba->fc_topology == TOPOLOGY_LOOP) {
  1096. /* FLOGI failed, use loop map to make discovery list */
  1097. lpfc_disc_list_loopmap(vport);
  1098. /* Start discovery */
  1099. lpfc_disc_start(vport);
  1100. return;
  1101. }
  1102. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1103. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  1104. "0258 Register Fabric login error: 0x%x\n",
  1105. mb->mbxStatus);
  1106. return;
  1107. }
  1108. ndlp->nlp_rpi = mb->un.varWords[0];
  1109. ndlp->nlp_type |= NLP_FABRIC;
  1110. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  1111. lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
  1112. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  1113. vports = lpfc_create_vport_work_array(phba);
  1114. if (vports != NULL)
  1115. for(i = 0;
  1116. i < LPFC_MAX_VPORTS && vports[i] != NULL;
  1117. i++) {
  1118. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  1119. continue;
  1120. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  1121. lpfc_initial_fdisc(vports[i]);
  1122. else if (phba->sli3_options &
  1123. LPFC_SLI3_NPIV_ENABLED) {
  1124. lpfc_vport_set_state(vports[i],
  1125. FC_VPORT_NO_FABRIC_SUPP);
  1126. lpfc_printf_vlog(vport, KERN_ERR,
  1127. LOG_ELS,
  1128. "0259 No NPIV "
  1129. "Fabric support\n");
  1130. }
  1131. }
  1132. lpfc_destroy_vport_work_array(vports);
  1133. lpfc_do_scr_ns_plogi(phba, vport);
  1134. }
  1135. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1136. kfree(mp);
  1137. mempool_free(pmb, phba->mbox_mem_pool);
  1138. return;
  1139. }
  1140. /*
  1141. * This routine handles processing a NameServer REG_LOGIN mailbox
  1142. * command upon completion. It is setup in the LPFC_MBOXQ
  1143. * as the completion routine when the command is
  1144. * handed off to the SLI layer.
  1145. */
  1146. void
  1147. lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1148. {
  1149. MAILBOX_t *mb = &pmb->mb;
  1150. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  1151. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  1152. struct lpfc_vport *vport = pmb->vport;
  1153. if (mb->mbxStatus) {
  1154. out:
  1155. lpfc_nlp_put(ndlp);
  1156. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1157. kfree(mp);
  1158. mempool_free(pmb, phba->mbox_mem_pool);
  1159. /* If no other thread is using the ndlp, free it */
  1160. lpfc_nlp_not_used(ndlp);
  1161. if (phba->fc_topology == TOPOLOGY_LOOP) {
  1162. /*
  1163. * RegLogin failed, use loop map to make discovery
  1164. * list
  1165. */
  1166. lpfc_disc_list_loopmap(vport);
  1167. /* Start discovery */
  1168. lpfc_disc_start(vport);
  1169. return;
  1170. }
  1171. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1172. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  1173. "0260 Register NameServer error: 0x%x\n",
  1174. mb->mbxStatus);
  1175. return;
  1176. }
  1177. pmb->context1 = NULL;
  1178. ndlp->nlp_rpi = mb->un.varWords[0];
  1179. ndlp->nlp_type |= NLP_FABRIC;
  1180. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  1181. if (vport->port_state < LPFC_VPORT_READY) {
  1182. /* Link up discovery requires Fabric registration. */
  1183. lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
  1184. lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
  1185. lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
  1186. lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
  1187. lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
  1188. /* Issue SCR just before NameServer GID_FT Query */
  1189. lpfc_issue_els_scr(vport, SCR_DID, 0);
  1190. }
  1191. vport->fc_ns_retry = 0;
  1192. /* Good status, issue CT Request to NameServer */
  1193. if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
  1194. /* Cannot issue NameServer Query, so finish up discovery */
  1195. goto out;
  1196. }
  1197. lpfc_nlp_put(ndlp);
  1198. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1199. kfree(mp);
  1200. mempool_free(pmb, phba->mbox_mem_pool);
  1201. return;
  1202. }
  1203. static void
  1204. lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1205. {
  1206. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1207. struct fc_rport *rport;
  1208. struct lpfc_rport_data *rdata;
  1209. struct fc_rport_identifiers rport_ids;
  1210. struct lpfc_hba *phba = vport->phba;
  1211. /* Remote port has reappeared. Re-register w/ FC transport */
  1212. rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
  1213. rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
  1214. rport_ids.port_id = ndlp->nlp_DID;
  1215. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  1216. /*
  1217. * We leave our node pointer in rport->dd_data when we unregister a
  1218. * FCP target port. But fc_remote_port_add zeros the space to which
  1219. * rport->dd_data points. So, if we're reusing a previously
  1220. * registered port, drop the reference that we took the last time we
  1221. * registered the port.
  1222. */
  1223. if (ndlp->rport && ndlp->rport->dd_data &&
  1224. ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
  1225. lpfc_nlp_put(ndlp);
  1226. }
  1227. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  1228. "rport add: did:x%x flg:x%x type x%x",
  1229. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  1230. ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
  1231. if (!rport || !get_device(&rport->dev)) {
  1232. dev_printk(KERN_WARNING, &phba->pcidev->dev,
  1233. "Warning: fc_remote_port_add failed\n");
  1234. return;
  1235. }
  1236. /* initialize static port data */
  1237. rport->maxframe_size = ndlp->nlp_maxframe;
  1238. rport->supported_classes = ndlp->nlp_class_sup;
  1239. rdata = rport->dd_data;
  1240. rdata->pnode = lpfc_nlp_get(ndlp);
  1241. if (ndlp->nlp_type & NLP_FCP_TARGET)
  1242. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  1243. if (ndlp->nlp_type & NLP_FCP_INITIATOR)
  1244. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  1245. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  1246. fc_remote_port_rolechg(rport, rport_ids.roles);
  1247. if ((rport->scsi_target_id != -1) &&
  1248. (rport->scsi_target_id < LPFC_MAX_TARGET)) {
  1249. ndlp->nlp_sid = rport->scsi_target_id;
  1250. }
  1251. return;
  1252. }
  1253. static void
  1254. lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
  1255. {
  1256. struct fc_rport *rport = ndlp->rport;
  1257. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  1258. "rport delete: did:x%x flg:x%x type x%x",
  1259. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  1260. fc_remote_port_delete(rport);
  1261. return;
  1262. }
  1263. static void
  1264. lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
  1265. {
  1266. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1267. spin_lock_irq(shost->host_lock);
  1268. switch (state) {
  1269. case NLP_STE_UNUSED_NODE:
  1270. vport->fc_unused_cnt += count;
  1271. break;
  1272. case NLP_STE_PLOGI_ISSUE:
  1273. vport->fc_plogi_cnt += count;
  1274. break;
  1275. case NLP_STE_ADISC_ISSUE:
  1276. vport->fc_adisc_cnt += count;
  1277. break;
  1278. case NLP_STE_REG_LOGIN_ISSUE:
  1279. vport->fc_reglogin_cnt += count;
  1280. break;
  1281. case NLP_STE_PRLI_ISSUE:
  1282. vport->fc_prli_cnt += count;
  1283. break;
  1284. case NLP_STE_UNMAPPED_NODE:
  1285. vport->fc_unmap_cnt += count;
  1286. break;
  1287. case NLP_STE_MAPPED_NODE:
  1288. vport->fc_map_cnt += count;
  1289. break;
  1290. case NLP_STE_NPR_NODE:
  1291. vport->fc_npr_cnt += count;
  1292. break;
  1293. }
  1294. spin_unlock_irq(shost->host_lock);
  1295. }
  1296. static void
  1297. lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  1298. int old_state, int new_state)
  1299. {
  1300. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1301. if (new_state == NLP_STE_UNMAPPED_NODE) {
  1302. ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
  1303. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  1304. ndlp->nlp_type |= NLP_FC_NODE;
  1305. }
  1306. if (new_state == NLP_STE_MAPPED_NODE)
  1307. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  1308. if (new_state == NLP_STE_NPR_NODE)
  1309. ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
  1310. /* Transport interface */
  1311. if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
  1312. old_state == NLP_STE_UNMAPPED_NODE)) {
  1313. vport->phba->nport_event_cnt++;
  1314. lpfc_unregister_remote_port(ndlp);
  1315. }
  1316. if (new_state == NLP_STE_MAPPED_NODE ||
  1317. new_state == NLP_STE_UNMAPPED_NODE) {
  1318. vport->phba->nport_event_cnt++;
  1319. /*
  1320. * Tell the fc transport about the port, if we haven't
  1321. * already. If we have, and it's a scsi entity, be
  1322. * sure to unblock any attached scsi devices
  1323. */
  1324. lpfc_register_remote_port(vport, ndlp);
  1325. }
  1326. /*
  1327. * if we added to Mapped list, but the remote port
  1328. * registration failed or assigned a target id outside
  1329. * our presentable range - move the node to the
  1330. * Unmapped List
  1331. */
  1332. if (new_state == NLP_STE_MAPPED_NODE &&
  1333. (!ndlp->rport ||
  1334. ndlp->rport->scsi_target_id == -1 ||
  1335. ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
  1336. spin_lock_irq(shost->host_lock);
  1337. ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
  1338. spin_unlock_irq(shost->host_lock);
  1339. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  1340. }
  1341. }
  1342. static char *
  1343. lpfc_nlp_state_name(char *buffer, size_t size, int state)
  1344. {
  1345. static char *states[] = {
  1346. [NLP_STE_UNUSED_NODE] = "UNUSED",
  1347. [NLP_STE_PLOGI_ISSUE] = "PLOGI",
  1348. [NLP_STE_ADISC_ISSUE] = "ADISC",
  1349. [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
  1350. [NLP_STE_PRLI_ISSUE] = "PRLI",
  1351. [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
  1352. [NLP_STE_MAPPED_NODE] = "MAPPED",
  1353. [NLP_STE_NPR_NODE] = "NPR",
  1354. };
  1355. if (state < NLP_STE_MAX_STATE && states[state])
  1356. strlcpy(buffer, states[state], size);
  1357. else
  1358. snprintf(buffer, size, "unknown (%d)", state);
  1359. return buffer;
  1360. }
  1361. void
  1362. lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  1363. int state)
  1364. {
  1365. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1366. int old_state = ndlp->nlp_state;
  1367. char name1[16], name2[16];
  1368. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1369. "0904 NPort state transition x%06x, %s -> %s\n",
  1370. ndlp->nlp_DID,
  1371. lpfc_nlp_state_name(name1, sizeof(name1), old_state),
  1372. lpfc_nlp_state_name(name2, sizeof(name2), state));
  1373. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  1374. "node statechg did:x%x old:%d ste:%d",
  1375. ndlp->nlp_DID, old_state, state);
  1376. if (old_state == NLP_STE_NPR_NODE &&
  1377. (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
  1378. state != NLP_STE_NPR_NODE)
  1379. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  1380. if (old_state == NLP_STE_UNMAPPED_NODE) {
  1381. ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
  1382. ndlp->nlp_type &= ~NLP_FC_NODE;
  1383. }
  1384. if (list_empty(&ndlp->nlp_listp)) {
  1385. spin_lock_irq(shost->host_lock);
  1386. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  1387. spin_unlock_irq(shost->host_lock);
  1388. } else if (old_state)
  1389. lpfc_nlp_counters(vport, old_state, -1);
  1390. ndlp->nlp_state = state;
  1391. lpfc_nlp_counters(vport, state, 1);
  1392. lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
  1393. }
  1394. void
  1395. lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1396. {
  1397. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1398. if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
  1399. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  1400. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  1401. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  1402. spin_lock_irq(shost->host_lock);
  1403. list_del_init(&ndlp->nlp_listp);
  1404. spin_unlock_irq(shost->host_lock);
  1405. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  1406. NLP_STE_UNUSED_NODE);
  1407. }
  1408. void
  1409. lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1410. {
  1411. /*
  1412. * Use of lpfc_drop_node and UNUSED list. lpfc_drop_node should
  1413. * be used if we wish to issue the "last" lpfc_nlp_put() to remove
  1414. * the ndlp from the vport. The ndlp resides on the UNUSED list
  1415. * until ALL other outstanding threads have completed. Thus, if a
  1416. * ndlp is on the UNUSED list already, we should never do another
  1417. * lpfc_drop_node() on it.
  1418. */
  1419. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
  1420. lpfc_nlp_put(ndlp);
  1421. return;
  1422. }
  1423. /*
  1424. * Start / ReStart rescue timer for Discovery / RSCN handling
  1425. */
  1426. void
  1427. lpfc_set_disctmo(struct lpfc_vport *vport)
  1428. {
  1429. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1430. struct lpfc_hba *phba = vport->phba;
  1431. uint32_t tmo;
  1432. if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
  1433. /* For FAN, timeout should be greater then edtov */
  1434. tmo = (((phba->fc_edtov + 999) / 1000) + 1);
  1435. } else {
  1436. /* Normal discovery timeout should be > then ELS/CT timeout
  1437. * FC spec states we need 3 * ratov for CT requests
  1438. */
  1439. tmo = ((phba->fc_ratov * 3) + 3);
  1440. }
  1441. if (!timer_pending(&vport->fc_disctmo)) {
  1442. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  1443. "set disc timer: tmo:x%x state:x%x flg:x%x",
  1444. tmo, vport->port_state, vport->fc_flag);
  1445. }
  1446. mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
  1447. spin_lock_irq(shost->host_lock);
  1448. vport->fc_flag |= FC_DISC_TMO;
  1449. spin_unlock_irq(shost->host_lock);
  1450. /* Start Discovery Timer state <hba_state> */
  1451. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  1452. "0247 Start Discovery Timer state x%x "
  1453. "Data: x%x x%lx x%x x%x\n",
  1454. vport->port_state, tmo,
  1455. (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
  1456. vport->fc_adisc_cnt);
  1457. return;
  1458. }
  1459. /*
  1460. * Cancel rescue timer for Discovery / RSCN handling
  1461. */
  1462. int
  1463. lpfc_can_disctmo(struct lpfc_vport *vport)
  1464. {
  1465. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1466. unsigned long iflags;
  1467. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  1468. "can disc timer: state:x%x rtry:x%x flg:x%x",
  1469. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  1470. /* Turn off discovery timer if its running */
  1471. if (vport->fc_flag & FC_DISC_TMO) {
  1472. spin_lock_irqsave(shost->host_lock, iflags);
  1473. vport->fc_flag &= ~FC_DISC_TMO;
  1474. spin_unlock_irqrestore(shost->host_lock, iflags);
  1475. del_timer_sync(&vport->fc_disctmo);
  1476. spin_lock_irqsave(&vport->work_port_lock, iflags);
  1477. vport->work_port_events &= ~WORKER_DISC_TMO;
  1478. spin_unlock_irqrestore(&vport->work_port_lock, iflags);
  1479. }
  1480. /* Cancel Discovery Timer state <hba_state> */
  1481. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  1482. "0248 Cancel Discovery Timer state x%x "
  1483. "Data: x%x x%x x%x\n",
  1484. vport->port_state, vport->fc_flag,
  1485. vport->fc_plogi_cnt, vport->fc_adisc_cnt);
  1486. return 0;
  1487. }
  1488. /*
  1489. * Check specified ring for outstanding IOCB on the SLI queue
  1490. * Return true if iocb matches the specified nport
  1491. */
  1492. int
  1493. lpfc_check_sli_ndlp(struct lpfc_hba *phba,
  1494. struct lpfc_sli_ring *pring,
  1495. struct lpfc_iocbq *iocb,
  1496. struct lpfc_nodelist *ndlp)
  1497. {
  1498. struct lpfc_sli *psli = &phba->sli;
  1499. IOCB_t *icmd = &iocb->iocb;
  1500. struct lpfc_vport *vport = ndlp->vport;
  1501. if (iocb->vport != vport)
  1502. return 0;
  1503. if (pring->ringno == LPFC_ELS_RING) {
  1504. switch (icmd->ulpCommand) {
  1505. case CMD_GEN_REQUEST64_CR:
  1506. if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
  1507. return 1;
  1508. case CMD_ELS_REQUEST64_CR:
  1509. if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
  1510. return 1;
  1511. case CMD_XMIT_ELS_RSP64_CX:
  1512. if (iocb->context1 == (uint8_t *) ndlp)
  1513. return 1;
  1514. }
  1515. } else if (pring->ringno == psli->extra_ring) {
  1516. } else if (pring->ringno == psli->fcp_ring) {
  1517. /* Skip match check if waiting to relogin to FCP target */
  1518. if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
  1519. (ndlp->nlp_flag & NLP_DELAY_TMO)) {
  1520. return 0;
  1521. }
  1522. if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
  1523. return 1;
  1524. }
  1525. } else if (pring->ringno == psli->next_ring) {
  1526. }
  1527. return 0;
  1528. }
  1529. /*
  1530. * Free resources / clean up outstanding I/Os
  1531. * associated with nlp_rpi in the LPFC_NODELIST entry.
  1532. */
  1533. static int
  1534. lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  1535. {
  1536. LIST_HEAD(completions);
  1537. struct lpfc_sli *psli;
  1538. struct lpfc_sli_ring *pring;
  1539. struct lpfc_iocbq *iocb, *next_iocb;
  1540. IOCB_t *icmd;
  1541. uint32_t rpi, i;
  1542. lpfc_fabric_abort_nport(ndlp);
  1543. /*
  1544. * Everything that matches on txcmplq will be returned
  1545. * by firmware with a no rpi error.
  1546. */
  1547. psli = &phba->sli;
  1548. rpi = ndlp->nlp_rpi;
  1549. if (rpi) {
  1550. /* Now process each ring */
  1551. for (i = 0; i < psli->num_rings; i++) {
  1552. pring = &psli->ring[i];
  1553. spin_lock_irq(&phba->hbalock);
  1554. list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
  1555. list) {
  1556. /*
  1557. * Check to see if iocb matches the nport we are
  1558. * looking for
  1559. */
  1560. if ((lpfc_check_sli_ndlp(phba, pring, iocb,
  1561. ndlp))) {
  1562. /* It matches, so deque and call compl
  1563. with an error */
  1564. list_move_tail(&iocb->list,
  1565. &completions);
  1566. pring->txq_cnt--;
  1567. }
  1568. }
  1569. spin_unlock_irq(&phba->hbalock);
  1570. }
  1571. }
  1572. while (!list_empty(&completions)) {
  1573. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  1574. list_del_init(&iocb->list);
  1575. if (!iocb->iocb_cmpl)
  1576. lpfc_sli_release_iocbq(phba, iocb);
  1577. else {
  1578. icmd = &iocb->iocb;
  1579. icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  1580. icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  1581. (iocb->iocb_cmpl)(phba, iocb, iocb);
  1582. }
  1583. }
  1584. return 0;
  1585. }
  1586. /*
  1587. * Free rpi associated with LPFC_NODELIST entry.
  1588. * This routine is called from lpfc_freenode(), when we are removing
  1589. * a LPFC_NODELIST entry. It is also called if the driver initiates a
  1590. * LOGO that completes successfully, and we are waiting to PLOGI back
  1591. * to the remote NPort. In addition, it is called after we receive
  1592. * and unsolicated ELS cmd, send back a rsp, the rsp completes and
  1593. * we are waiting to PLOGI back to the remote NPort.
  1594. */
  1595. int
  1596. lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1597. {
  1598. struct lpfc_hba *phba = vport->phba;
  1599. LPFC_MBOXQ_t *mbox;
  1600. int rc;
  1601. if (ndlp->nlp_rpi) {
  1602. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1603. if (mbox) {
  1604. lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
  1605. mbox->vport = vport;
  1606. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1607. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1608. if (rc == MBX_NOT_FINISHED)
  1609. mempool_free(mbox, phba->mbox_mem_pool);
  1610. }
  1611. lpfc_no_rpi(phba, ndlp);
  1612. ndlp->nlp_rpi = 0;
  1613. return 1;
  1614. }
  1615. return 0;
  1616. }
  1617. void
  1618. lpfc_unreg_all_rpis(struct lpfc_vport *vport)
  1619. {
  1620. struct lpfc_hba *phba = vport->phba;
  1621. LPFC_MBOXQ_t *mbox;
  1622. int rc;
  1623. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1624. if (mbox) {
  1625. lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
  1626. mbox->vport = vport;
  1627. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1628. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1629. if (rc == MBX_NOT_FINISHED) {
  1630. mempool_free(mbox, phba->mbox_mem_pool);
  1631. }
  1632. }
  1633. }
  1634. void
  1635. lpfc_unreg_default_rpis(struct lpfc_vport *vport)
  1636. {
  1637. struct lpfc_hba *phba = vport->phba;
  1638. LPFC_MBOXQ_t *mbox;
  1639. int rc;
  1640. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1641. if (mbox) {
  1642. lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
  1643. mbox->vport = vport;
  1644. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1645. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1646. if (rc == MBX_NOT_FINISHED) {
  1647. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  1648. "1815 Could not issue "
  1649. "unreg_did (default rpis)\n");
  1650. mempool_free(mbox, phba->mbox_mem_pool);
  1651. }
  1652. }
  1653. }
  1654. /*
  1655. * Free resources associated with LPFC_NODELIST entry
  1656. * so it can be freed.
  1657. */
  1658. static int
  1659. lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1660. {
  1661. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1662. struct lpfc_hba *phba = vport->phba;
  1663. LPFC_MBOXQ_t *mb, *nextmb;
  1664. struct lpfc_dmabuf *mp;
  1665. /* Cleanup node for NPort <nlp_DID> */
  1666. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1667. "0900 Cleanup node for NPort x%x "
  1668. "Data: x%x x%x x%x\n",
  1669. ndlp->nlp_DID, ndlp->nlp_flag,
  1670. ndlp->nlp_state, ndlp->nlp_rpi);
  1671. lpfc_dequeue_node(vport, ndlp);
  1672. /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
  1673. if ((mb = phba->sli.mbox_active)) {
  1674. if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
  1675. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  1676. mb->context2 = NULL;
  1677. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1678. }
  1679. }
  1680. spin_lock_irq(&phba->hbalock);
  1681. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  1682. if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
  1683. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  1684. mp = (struct lpfc_dmabuf *) (mb->context1);
  1685. if (mp) {
  1686. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1687. kfree(mp);
  1688. }
  1689. list_del(&mb->list);
  1690. mempool_free(mb, phba->mbox_mem_pool);
  1691. lpfc_nlp_put(ndlp);
  1692. }
  1693. }
  1694. spin_unlock_irq(&phba->hbalock);
  1695. lpfc_els_abort(phba,ndlp);
  1696. spin_lock_irq(shost->host_lock);
  1697. ndlp->nlp_flag &= ~NLP_DELAY_TMO;
  1698. spin_unlock_irq(shost->host_lock);
  1699. ndlp->nlp_last_elscmd = 0;
  1700. del_timer_sync(&ndlp->nlp_delayfunc);
  1701. if (!list_empty(&ndlp->els_retry_evt.evt_listp))
  1702. list_del_init(&ndlp->els_retry_evt.evt_listp);
  1703. if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
  1704. list_del_init(&ndlp->dev_loss_evt.evt_listp);
  1705. lpfc_unreg_rpi(vport, ndlp);
  1706. return 0;
  1707. }
  1708. /*
  1709. * Check to see if we can free the nlp back to the freelist.
  1710. * If we are in the middle of using the nlp in the discovery state
  1711. * machine, defer the free till we reach the end of the state machine.
  1712. */
  1713. static void
  1714. lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  1715. {
  1716. struct lpfc_rport_data *rdata;
  1717. if (ndlp->nlp_flag & NLP_DELAY_TMO) {
  1718. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  1719. }
  1720. lpfc_cleanup_node(vport, ndlp);
  1721. /*
  1722. * We can get here with a non-NULL ndlp->rport because when we
  1723. * unregister a rport we don't break the rport/node linkage. So if we
  1724. * do, make sure we don't leaving any dangling pointers behind.
  1725. */
  1726. if (ndlp->rport) {
  1727. rdata = ndlp->rport->dd_data;
  1728. rdata->pnode = NULL;
  1729. ndlp->rport = NULL;
  1730. }
  1731. }
  1732. static int
  1733. lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  1734. uint32_t did)
  1735. {
  1736. D_ID mydid, ndlpdid, matchdid;
  1737. if (did == Bcast_DID)
  1738. return 0;
  1739. if (ndlp->nlp_DID == 0) {
  1740. return 0;
  1741. }
  1742. /* First check for Direct match */
  1743. if (ndlp->nlp_DID == did)
  1744. return 1;
  1745. /* Next check for area/domain identically equals 0 match */
  1746. mydid.un.word = vport->fc_myDID;
  1747. if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
  1748. return 0;
  1749. }
  1750. matchdid.un.word = did;
  1751. ndlpdid.un.word = ndlp->nlp_DID;
  1752. if (matchdid.un.b.id == ndlpdid.un.b.id) {
  1753. if ((mydid.un.b.domain == matchdid.un.b.domain) &&
  1754. (mydid.un.b.area == matchdid.un.b.area)) {
  1755. if ((ndlpdid.un.b.domain == 0) &&
  1756. (ndlpdid.un.b.area == 0)) {
  1757. if (ndlpdid.un.b.id)
  1758. return 1;
  1759. }
  1760. return 0;
  1761. }
  1762. matchdid.un.word = ndlp->nlp_DID;
  1763. if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
  1764. (mydid.un.b.area == ndlpdid.un.b.area)) {
  1765. if ((matchdid.un.b.domain == 0) &&
  1766. (matchdid.un.b.area == 0)) {
  1767. if (matchdid.un.b.id)
  1768. return 1;
  1769. }
  1770. }
  1771. }
  1772. return 0;
  1773. }
  1774. /* Search for a nodelist entry */
  1775. static struct lpfc_nodelist *
  1776. __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  1777. {
  1778. struct lpfc_nodelist *ndlp;
  1779. uint32_t data1;
  1780. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  1781. if (lpfc_matchdid(vport, ndlp, did)) {
  1782. data1 = (((uint32_t) ndlp->nlp_state << 24) |
  1783. ((uint32_t) ndlp->nlp_xri << 16) |
  1784. ((uint32_t) ndlp->nlp_type << 8) |
  1785. ((uint32_t) ndlp->nlp_rpi & 0xff));
  1786. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1787. "0929 FIND node DID "
  1788. "Data: x%p x%x x%x x%x\n",
  1789. ndlp, ndlp->nlp_DID,
  1790. ndlp->nlp_flag, data1);
  1791. return ndlp;
  1792. }
  1793. }
  1794. /* FIND node did <did> NOT FOUND */
  1795. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1796. "0932 FIND node did x%x NOT FOUND.\n", did);
  1797. return NULL;
  1798. }
  1799. struct lpfc_nodelist *
  1800. lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  1801. {
  1802. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1803. struct lpfc_nodelist *ndlp;
  1804. spin_lock_irq(shost->host_lock);
  1805. ndlp = __lpfc_findnode_did(vport, did);
  1806. spin_unlock_irq(shost->host_lock);
  1807. return ndlp;
  1808. }
  1809. struct lpfc_nodelist *
  1810. lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
  1811. {
  1812. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1813. struct lpfc_nodelist *ndlp;
  1814. ndlp = lpfc_findnode_did(vport, did);
  1815. if (!ndlp) {
  1816. if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
  1817. lpfc_rscn_payload_check(vport, did) == 0)
  1818. return NULL;
  1819. ndlp = (struct lpfc_nodelist *)
  1820. mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
  1821. if (!ndlp)
  1822. return NULL;
  1823. lpfc_nlp_init(vport, ndlp, did);
  1824. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  1825. spin_lock_irq(shost->host_lock);
  1826. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  1827. spin_unlock_irq(shost->host_lock);
  1828. return ndlp;
  1829. }
  1830. if (vport->fc_flag & FC_RSCN_MODE) {
  1831. if (lpfc_rscn_payload_check(vport, did)) {
  1832. /* If we've already recieved a PLOGI from this NPort
  1833. * we don't need to try to discover it again.
  1834. */
  1835. if (ndlp->nlp_flag & NLP_RCV_PLOGI)
  1836. return NULL;
  1837. spin_lock_irq(shost->host_lock);
  1838. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  1839. spin_unlock_irq(shost->host_lock);
  1840. /* Since this node is marked for discovery,
  1841. * delay timeout is not needed.
  1842. */
  1843. if (ndlp->nlp_flag & NLP_DELAY_TMO)
  1844. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  1845. } else
  1846. ndlp = NULL;
  1847. } else {
  1848. /* If we've already recieved a PLOGI from this NPort,
  1849. * or we are already in the process of discovery on it,
  1850. * we don't need to try to discover it again.
  1851. */
  1852. if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
  1853. ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  1854. ndlp->nlp_flag & NLP_RCV_PLOGI)
  1855. return NULL;
  1856. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  1857. spin_lock_irq(shost->host_lock);
  1858. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  1859. spin_unlock_irq(shost->host_lock);
  1860. }
  1861. return ndlp;
  1862. }
  1863. /* Build a list of nodes to discover based on the loopmap */
  1864. void
  1865. lpfc_disc_list_loopmap(struct lpfc_vport *vport)
  1866. {
  1867. struct lpfc_hba *phba = vport->phba;
  1868. int j;
  1869. uint32_t alpa, index;
  1870. if (!lpfc_is_link_up(phba))
  1871. return;
  1872. if (phba->fc_topology != TOPOLOGY_LOOP)
  1873. return;
  1874. /* Check for loop map present or not */
  1875. if (phba->alpa_map[0]) {
  1876. for (j = 1; j <= phba->alpa_map[0]; j++) {
  1877. alpa = phba->alpa_map[j];
  1878. if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
  1879. continue;
  1880. lpfc_setup_disc_node(vport, alpa);
  1881. }
  1882. } else {
  1883. /* No alpamap, so try all alpa's */
  1884. for (j = 0; j < FC_MAXLOOP; j++) {
  1885. /* If cfg_scan_down is set, start from highest
  1886. * ALPA (0xef) to lowest (0x1).
  1887. */
  1888. if (vport->cfg_scan_down)
  1889. index = j;
  1890. else
  1891. index = FC_MAXLOOP - j - 1;
  1892. alpa = lpfcAlpaArray[index];
  1893. if ((vport->fc_myDID & 0xff) == alpa)
  1894. continue;
  1895. lpfc_setup_disc_node(vport, alpa);
  1896. }
  1897. }
  1898. return;
  1899. }
  1900. void
  1901. lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
  1902. {
  1903. LPFC_MBOXQ_t *mbox;
  1904. struct lpfc_sli *psli = &phba->sli;
  1905. struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
  1906. struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
  1907. struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
  1908. int rc;
  1909. /*
  1910. * if it's not a physical port or if we already send
  1911. * clear_la then don't send it.
  1912. */
  1913. if ((phba->link_state >= LPFC_CLEAR_LA) ||
  1914. (vport->port_type != LPFC_PHYSICAL_PORT))
  1915. return;
  1916. /* Link up discovery */
  1917. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
  1918. phba->link_state = LPFC_CLEAR_LA;
  1919. lpfc_clear_la(phba, mbox);
  1920. mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
  1921. mbox->vport = vport;
  1922. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1923. if (rc == MBX_NOT_FINISHED) {
  1924. mempool_free(mbox, phba->mbox_mem_pool);
  1925. lpfc_disc_flush_list(vport);
  1926. extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  1927. fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  1928. next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  1929. phba->link_state = LPFC_HBA_ERROR;
  1930. }
  1931. }
  1932. }
  1933. /* Reg_vpi to tell firmware to resume normal operations */
  1934. void
  1935. lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
  1936. {
  1937. LPFC_MBOXQ_t *regvpimbox;
  1938. regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1939. if (regvpimbox) {
  1940. lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
  1941. regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
  1942. regvpimbox->vport = vport;
  1943. if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
  1944. == MBX_NOT_FINISHED) {
  1945. mempool_free(regvpimbox, phba->mbox_mem_pool);
  1946. }
  1947. }
  1948. }
  1949. /* Start Link up / RSCN discovery on NPR nodes */
  1950. void
  1951. lpfc_disc_start(struct lpfc_vport *vport)
  1952. {
  1953. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1954. struct lpfc_hba *phba = vport->phba;
  1955. uint32_t num_sent;
  1956. uint32_t clear_la_pending;
  1957. int did_changed;
  1958. if (!lpfc_is_link_up(phba))
  1959. return;
  1960. if (phba->link_state == LPFC_CLEAR_LA)
  1961. clear_la_pending = 1;
  1962. else
  1963. clear_la_pending = 0;
  1964. if (vport->port_state < LPFC_VPORT_READY)
  1965. vport->port_state = LPFC_DISC_AUTH;
  1966. lpfc_set_disctmo(vport);
  1967. if (vport->fc_prevDID == vport->fc_myDID)
  1968. did_changed = 0;
  1969. else
  1970. did_changed = 1;
  1971. vport->fc_prevDID = vport->fc_myDID;
  1972. vport->num_disc_nodes = 0;
  1973. /* Start Discovery state <hba_state> */
  1974. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  1975. "0202 Start Discovery hba state x%x "
  1976. "Data: x%x x%x x%x\n",
  1977. vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
  1978. vport->fc_adisc_cnt);
  1979. /* First do ADISCs - if any */
  1980. num_sent = lpfc_els_disc_adisc(vport);
  1981. if (num_sent)
  1982. return;
  1983. /*
  1984. * For SLI3, cmpl_reg_vpi will set port_state to READY, and
  1985. * continue discovery.
  1986. */
  1987. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  1988. !(vport->fc_flag & FC_RSCN_MODE)) {
  1989. lpfc_issue_reg_vpi(phba, vport);
  1990. return;
  1991. }
  1992. /*
  1993. * For SLI2, we need to set port_state to READY and continue
  1994. * discovery.
  1995. */
  1996. if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
  1997. /* If we get here, there is nothing to ADISC */
  1998. if (vport->port_type == LPFC_PHYSICAL_PORT)
  1999. lpfc_issue_clear_la(phba, vport);
  2000. if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
  2001. vport->num_disc_nodes = 0;
  2002. /* go thru NPR nodes and issue ELS PLOGIs */
  2003. if (vport->fc_npr_cnt)
  2004. lpfc_els_disc_plogi(vport);
  2005. if (!vport->num_disc_nodes) {
  2006. spin_lock_irq(shost->host_lock);
  2007. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  2008. spin_unlock_irq(shost->host_lock);
  2009. lpfc_can_disctmo(vport);
  2010. }
  2011. }
  2012. vport->port_state = LPFC_VPORT_READY;
  2013. } else {
  2014. /* Next do PLOGIs - if any */
  2015. num_sent = lpfc_els_disc_plogi(vport);
  2016. if (num_sent)
  2017. return;
  2018. if (vport->fc_flag & FC_RSCN_MODE) {
  2019. /* Check to see if more RSCNs came in while we
  2020. * were processing this one.
  2021. */
  2022. if ((vport->fc_rscn_id_cnt == 0) &&
  2023. (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
  2024. spin_lock_irq(shost->host_lock);
  2025. vport->fc_flag &= ~FC_RSCN_MODE;
  2026. spin_unlock_irq(shost->host_lock);
  2027. lpfc_can_disctmo(vport);
  2028. } else
  2029. lpfc_els_handle_rscn(vport);
  2030. }
  2031. }
  2032. return;
  2033. }
  2034. /*
  2035. * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
  2036. * ring the match the sppecified nodelist.
  2037. */
  2038. static void
  2039. lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  2040. {
  2041. LIST_HEAD(completions);
  2042. struct lpfc_sli *psli;
  2043. IOCB_t *icmd;
  2044. struct lpfc_iocbq *iocb, *next_iocb;
  2045. struct lpfc_sli_ring *pring;
  2046. psli = &phba->sli;
  2047. pring = &psli->ring[LPFC_ELS_RING];
  2048. /* Error matching iocb on txq or txcmplq
  2049. * First check the txq.
  2050. */
  2051. spin_lock_irq(&phba->hbalock);
  2052. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  2053. if (iocb->context1 != ndlp) {
  2054. continue;
  2055. }
  2056. icmd = &iocb->iocb;
  2057. if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
  2058. (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
  2059. list_move_tail(&iocb->list, &completions);
  2060. pring->txq_cnt--;
  2061. }
  2062. }
  2063. /* Next check the txcmplq */
  2064. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  2065. if (iocb->context1 != ndlp) {
  2066. continue;
  2067. }
  2068. icmd = &iocb->iocb;
  2069. if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
  2070. icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
  2071. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2072. }
  2073. }
  2074. spin_unlock_irq(&phba->hbalock);
  2075. while (!list_empty(&completions)) {
  2076. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  2077. list_del_init(&iocb->list);
  2078. if (!iocb->iocb_cmpl)
  2079. lpfc_sli_release_iocbq(phba, iocb);
  2080. else {
  2081. icmd = &iocb->iocb;
  2082. icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2083. icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  2084. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2085. }
  2086. }
  2087. }
  2088. void
  2089. lpfc_disc_flush_list(struct lpfc_vport *vport)
  2090. {
  2091. struct lpfc_nodelist *ndlp, *next_ndlp;
  2092. struct lpfc_hba *phba = vport->phba;
  2093. if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
  2094. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  2095. nlp_listp) {
  2096. if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  2097. ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
  2098. lpfc_free_tx(phba, ndlp);
  2099. }
  2100. }
  2101. }
  2102. }
  2103. void
  2104. lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
  2105. {
  2106. lpfc_els_flush_rscn(vport);
  2107. lpfc_els_flush_cmd(vport);
  2108. lpfc_disc_flush_list(vport);
  2109. }
  2110. /*****************************************************************************/
  2111. /*
  2112. * NAME: lpfc_disc_timeout
  2113. *
  2114. * FUNCTION: Fibre Channel driver discovery timeout routine.
  2115. *
  2116. * EXECUTION ENVIRONMENT: interrupt only
  2117. *
  2118. * CALLED FROM:
  2119. * Timer function
  2120. *
  2121. * RETURNS:
  2122. * none
  2123. */
  2124. /*****************************************************************************/
  2125. void
  2126. lpfc_disc_timeout(unsigned long ptr)
  2127. {
  2128. struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
  2129. struct lpfc_hba *phba = vport->phba;
  2130. unsigned long flags = 0;
  2131. if (unlikely(!phba))
  2132. return;
  2133. if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
  2134. spin_lock_irqsave(&vport->work_port_lock, flags);
  2135. vport->work_port_events |= WORKER_DISC_TMO;
  2136. spin_unlock_irqrestore(&vport->work_port_lock, flags);
  2137. spin_lock_irqsave(&phba->hbalock, flags);
  2138. if (phba->work_wait)
  2139. lpfc_worker_wake_up(phba);
  2140. spin_unlock_irqrestore(&phba->hbalock, flags);
  2141. }
  2142. return;
  2143. }
  2144. static void
  2145. lpfc_disc_timeout_handler(struct lpfc_vport *vport)
  2146. {
  2147. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2148. struct lpfc_hba *phba = vport->phba;
  2149. struct lpfc_sli *psli = &phba->sli;
  2150. struct lpfc_nodelist *ndlp, *next_ndlp;
  2151. LPFC_MBOXQ_t *initlinkmbox;
  2152. int rc, clrlaerr = 0;
  2153. if (!(vport->fc_flag & FC_DISC_TMO))
  2154. return;
  2155. spin_lock_irq(shost->host_lock);
  2156. vport->fc_flag &= ~FC_DISC_TMO;
  2157. spin_unlock_irq(shost->host_lock);
  2158. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  2159. "disc timeout: state:x%x rtry:x%x flg:x%x",
  2160. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  2161. switch (vport->port_state) {
  2162. case LPFC_LOCAL_CFG_LINK:
  2163. /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
  2164. * FAN
  2165. */
  2166. /* FAN timeout */
  2167. lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
  2168. "0221 FAN timeout\n");
  2169. /* Start discovery by sending FLOGI, clean up old rpis */
  2170. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  2171. nlp_listp) {
  2172. if (ndlp->nlp_state != NLP_STE_NPR_NODE)
  2173. continue;
  2174. if (ndlp->nlp_type & NLP_FABRIC) {
  2175. /* Clean up the ndlp on Fabric connections */
  2176. lpfc_drop_node(vport, ndlp);
  2177. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  2178. /* Fail outstanding IO now since device
  2179. * is marked for PLOGI.
  2180. */
  2181. lpfc_unreg_rpi(vport, ndlp);
  2182. }
  2183. }
  2184. if (vport->port_state != LPFC_FLOGI) {
  2185. lpfc_initial_flogi(vport);
  2186. }
  2187. break;
  2188. case LPFC_FDISC:
  2189. case LPFC_FLOGI:
  2190. /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
  2191. /* Initial FLOGI timeout */
  2192. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2193. "0222 Initial %s timeout\n",
  2194. vport->vpi ? "FDISC" : "FLOGI");
  2195. /* Assume no Fabric and go on with discovery.
  2196. * Check for outstanding ELS FLOGI to abort.
  2197. */
  2198. /* FLOGI failed, so just use loop map to make discovery list */
  2199. lpfc_disc_list_loopmap(vport);
  2200. /* Start discovery */
  2201. lpfc_disc_start(vport);
  2202. break;
  2203. case LPFC_FABRIC_CFG_LINK:
  2204. /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
  2205. NameServer login */
  2206. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2207. "0223 Timeout while waiting for "
  2208. "NameServer login\n");
  2209. /* Next look for NameServer ndlp */
  2210. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  2211. if (ndlp)
  2212. lpfc_els_abort(phba, ndlp);
  2213. /* ReStart discovery */
  2214. goto restart_disc;
  2215. case LPFC_NS_QRY:
  2216. /* Check for wait for NameServer Rsp timeout */
  2217. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2218. "0224 NameServer Query timeout "
  2219. "Data: x%x x%x\n",
  2220. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  2221. if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
  2222. /* Try it one more time */
  2223. vport->fc_ns_retry++;
  2224. rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
  2225. vport->fc_ns_retry, 0);
  2226. if (rc == 0)
  2227. break;
  2228. }
  2229. vport->fc_ns_retry = 0;
  2230. restart_disc:
  2231. /*
  2232. * Discovery is over.
  2233. * set port_state to PORT_READY if SLI2.
  2234. * cmpl_reg_vpi will set port_state to READY for SLI3.
  2235. */
  2236. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  2237. lpfc_issue_reg_vpi(phba, vport);
  2238. else { /* NPIV Not enabled */
  2239. lpfc_issue_clear_la(phba, vport);
  2240. vport->port_state = LPFC_VPORT_READY;
  2241. }
  2242. /* Setup and issue mailbox INITIALIZE LINK command */
  2243. initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2244. if (!initlinkmbox) {
  2245. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2246. "0206 Device Discovery "
  2247. "completion error\n");
  2248. phba->link_state = LPFC_HBA_ERROR;
  2249. break;
  2250. }
  2251. lpfc_linkdown(phba);
  2252. lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
  2253. phba->cfg_link_speed);
  2254. initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
  2255. initlinkmbox->vport = vport;
  2256. initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2257. rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
  2258. lpfc_set_loopback_flag(phba);
  2259. if (rc == MBX_NOT_FINISHED)
  2260. mempool_free(initlinkmbox, phba->mbox_mem_pool);
  2261. break;
  2262. case LPFC_DISC_AUTH:
  2263. /* Node Authentication timeout */
  2264. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2265. "0227 Node Authentication timeout\n");
  2266. lpfc_disc_flush_list(vport);
  2267. /*
  2268. * set port_state to PORT_READY if SLI2.
  2269. * cmpl_reg_vpi will set port_state to READY for SLI3.
  2270. */
  2271. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  2272. lpfc_issue_reg_vpi(phba, vport);
  2273. else { /* NPIV Not enabled */
  2274. lpfc_issue_clear_la(phba, vport);
  2275. vport->port_state = LPFC_VPORT_READY;
  2276. }
  2277. break;
  2278. case LPFC_VPORT_READY:
  2279. if (vport->fc_flag & FC_RSCN_MODE) {
  2280. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2281. "0231 RSCN timeout Data: x%x "
  2282. "x%x\n",
  2283. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  2284. /* Cleanup any outstanding ELS commands */
  2285. lpfc_els_flush_cmd(vport);
  2286. lpfc_els_flush_rscn(vport);
  2287. lpfc_disc_flush_list(vport);
  2288. }
  2289. break;
  2290. default:
  2291. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2292. "0229 Unexpected discovery timeout, "
  2293. "vport State x%x\n", vport->port_state);
  2294. break;
  2295. }
  2296. switch (phba->link_state) {
  2297. case LPFC_CLEAR_LA:
  2298. /* CLEAR LA timeout */
  2299. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2300. "0228 CLEAR LA timeout\n");
  2301. clrlaerr = 1;
  2302. break;
  2303. case LPFC_LINK_UNKNOWN:
  2304. case LPFC_WARM_START:
  2305. case LPFC_INIT_START:
  2306. case LPFC_INIT_MBX_CMDS:
  2307. case LPFC_LINK_DOWN:
  2308. case LPFC_LINK_UP:
  2309. case LPFC_HBA_ERROR:
  2310. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  2311. "0230 Unexpected timeout, hba link "
  2312. "state x%x\n", phba->link_state);
  2313. clrlaerr = 1;
  2314. break;
  2315. case LPFC_HBA_READY:
  2316. break;
  2317. }
  2318. if (clrlaerr) {
  2319. lpfc_disc_flush_list(vport);
  2320. psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  2321. psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  2322. psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  2323. vport->port_state = LPFC_VPORT_READY;
  2324. }
  2325. return;
  2326. }
  2327. /*
  2328. * This routine handles processing a NameServer REG_LOGIN mailbox
  2329. * command upon completion. It is setup in the LPFC_MBOXQ
  2330. * as the completion routine when the command is
  2331. * handed off to the SLI layer.
  2332. */
  2333. void
  2334. lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2335. {
  2336. MAILBOX_t *mb = &pmb->mb;
  2337. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2338. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2339. struct lpfc_vport *vport = pmb->vport;
  2340. pmb->context1 = NULL;
  2341. ndlp->nlp_rpi = mb->un.varWords[0];
  2342. ndlp->nlp_type |= NLP_FABRIC;
  2343. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2344. /*
  2345. * Start issuing Fabric-Device Management Interface (FDMI) command to
  2346. * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
  2347. * fdmi-on=2 (supporting RPA/hostnmae)
  2348. */
  2349. if (vport->cfg_fdmi_on == 1)
  2350. lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
  2351. else
  2352. mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
  2353. /* Mailbox took a reference to the node */
  2354. lpfc_nlp_put(ndlp);
  2355. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2356. kfree(mp);
  2357. mempool_free(pmb, phba->mbox_mem_pool);
  2358. return;
  2359. }
  2360. static int
  2361. lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
  2362. {
  2363. uint16_t *rpi = param;
  2364. return ndlp->nlp_rpi == *rpi;
  2365. }
  2366. static int
  2367. lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
  2368. {
  2369. return memcmp(&ndlp->nlp_portname, param,
  2370. sizeof(ndlp->nlp_portname)) == 0;
  2371. }
  2372. struct lpfc_nodelist *
  2373. __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  2374. {
  2375. struct lpfc_nodelist *ndlp;
  2376. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  2377. if (filter(ndlp, param))
  2378. return ndlp;
  2379. }
  2380. return NULL;
  2381. }
  2382. /*
  2383. * Search node lists for a remote port matching filter criteria
  2384. * Caller needs to hold host_lock before calling this routine.
  2385. */
  2386. struct lpfc_nodelist *
  2387. lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  2388. {
  2389. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2390. struct lpfc_nodelist *ndlp;
  2391. spin_lock_irq(shost->host_lock);
  2392. ndlp = __lpfc_find_node(vport, filter, param);
  2393. spin_unlock_irq(shost->host_lock);
  2394. return ndlp;
  2395. }
  2396. /*
  2397. * This routine looks up the ndlp lists for the given RPI. If rpi found it
  2398. * returns the node list element pointer else return NULL.
  2399. */
  2400. struct lpfc_nodelist *
  2401. __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  2402. {
  2403. return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
  2404. }
  2405. struct lpfc_nodelist *
  2406. lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  2407. {
  2408. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2409. struct lpfc_nodelist *ndlp;
  2410. spin_lock_irq(shost->host_lock);
  2411. ndlp = __lpfc_findnode_rpi(vport, rpi);
  2412. spin_unlock_irq(shost->host_lock);
  2413. return ndlp;
  2414. }
  2415. /*
  2416. * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
  2417. * returns the node element list pointer else return NULL.
  2418. */
  2419. struct lpfc_nodelist *
  2420. lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
  2421. {
  2422. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2423. struct lpfc_nodelist *ndlp;
  2424. spin_lock_irq(shost->host_lock);
  2425. ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
  2426. spin_unlock_irq(shost->host_lock);
  2427. return ndlp;
  2428. }
  2429. void
  2430. lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2431. uint32_t did)
  2432. {
  2433. memset(ndlp, 0, sizeof (struct lpfc_nodelist));
  2434. INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
  2435. INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
  2436. init_timer(&ndlp->nlp_delayfunc);
  2437. ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
  2438. ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
  2439. ndlp->nlp_DID = did;
  2440. ndlp->vport = vport;
  2441. ndlp->nlp_sid = NLP_NO_SID;
  2442. INIT_LIST_HEAD(&ndlp->nlp_listp);
  2443. kref_init(&ndlp->kref);
  2444. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  2445. "node init: did:x%x",
  2446. ndlp->nlp_DID, 0, 0);
  2447. return;
  2448. }
  2449. /* This routine releases all resources associated with a specifc NPort's ndlp
  2450. * and mempool_free's the nodelist.
  2451. */
  2452. static void
  2453. lpfc_nlp_release(struct kref *kref)
  2454. {
  2455. struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
  2456. kref);
  2457. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  2458. "node release: did:x%x flg:x%x type:x%x",
  2459. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  2460. lpfc_nlp_remove(ndlp->vport, ndlp);
  2461. mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
  2462. }
  2463. /* This routine bumps the reference count for a ndlp structure to ensure
  2464. * that one discovery thread won't free a ndlp while another discovery thread
  2465. * is using it.
  2466. */
  2467. struct lpfc_nodelist *
  2468. lpfc_nlp_get(struct lpfc_nodelist *ndlp)
  2469. {
  2470. if (ndlp) {
  2471. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  2472. "node get: did:x%x flg:x%x refcnt:x%x",
  2473. ndlp->nlp_DID, ndlp->nlp_flag,
  2474. atomic_read(&ndlp->kref.refcount));
  2475. kref_get(&ndlp->kref);
  2476. }
  2477. return ndlp;
  2478. }
  2479. /* This routine decrements the reference count for a ndlp structure. If the
  2480. * count goes to 0, this indicates the the associated nodelist should be freed.
  2481. */
  2482. int
  2483. lpfc_nlp_put(struct lpfc_nodelist *ndlp)
  2484. {
  2485. if (ndlp) {
  2486. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  2487. "node put: did:x%x flg:x%x refcnt:x%x",
  2488. ndlp->nlp_DID, ndlp->nlp_flag,
  2489. atomic_read(&ndlp->kref.refcount));
  2490. }
  2491. return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
  2492. }
  2493. /* This routine free's the specified nodelist if it is not in use
  2494. * by any other discovery thread. This routine returns 1 if the ndlp
  2495. * is not being used by anyone and has been freed. A return value of
  2496. * 0 indicates it is being used by another discovery thread and the
  2497. * refcount is left unchanged.
  2498. */
  2499. int
  2500. lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
  2501. {
  2502. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  2503. "node not used: did:x%x flg:x%x refcnt:x%x",
  2504. ndlp->nlp_DID, ndlp->nlp_flag,
  2505. atomic_read(&ndlp->kref.refcount));
  2506. if (atomic_read(&ndlp->kref.refcount) == 1) {
  2507. lpfc_nlp_put(ndlp);
  2508. return 1;
  2509. }
  2510. return 0;
  2511. }