lpfc_hbadisc.c 90 KB

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