lpfc_hbadisc.c 91 KB

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