lpfc_hbadisc.c 90 KB

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