lpfc_hbadisc.c 90 KB

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