lpfc_hbadisc.c 79 KB

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