lpfc_hbadisc.c 78 KB

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