lpfc_hbadisc.c 84 KB

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