lpfc_hbadisc.c 90 KB

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