lpfc_hbadisc.c 78 KB

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