lpfc_hbadisc.c 78 KB

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