bfa_ioc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * Linux network driver for Brocade Converged Network Adapter.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License (GPL) Version 2 as
  6. * published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. /*
  14. * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
  15. * All rights reserved
  16. * www.brocade.com
  17. */
  18. #include "bfa_ioc.h"
  19. #include "cna.h"
  20. #include "bfi.h"
  21. #include "bfi_ctreg.h"
  22. #include "bfa_defs.h"
  23. /**
  24. * IOC local definitions
  25. */
  26. #define bfa_ioc_timer_start(__ioc) \
  27. mod_timer(&(__ioc)->ioc_timer, jiffies + \
  28. msecs_to_jiffies(BFA_IOC_TOV))
  29. #define bfa_ioc_timer_stop(__ioc) del_timer(&(__ioc)->ioc_timer)
  30. #define bfa_ioc_recovery_timer_start(__ioc) \
  31. mod_timer(&(__ioc)->ioc_timer, jiffies + \
  32. msecs_to_jiffies(BFA_IOC_TOV_RECOVER))
  33. #define bfa_sem_timer_start(__ioc) \
  34. mod_timer(&(__ioc)->sem_timer, jiffies + \
  35. msecs_to_jiffies(BFA_IOC_HWSEM_TOV))
  36. #define bfa_sem_timer_stop(__ioc) del_timer(&(__ioc)->sem_timer)
  37. #define bfa_hb_timer_start(__ioc) \
  38. mod_timer(&(__ioc)->hb_timer, jiffies + \
  39. msecs_to_jiffies(BFA_IOC_HB_TOV))
  40. #define bfa_hb_timer_stop(__ioc) del_timer(&(__ioc)->hb_timer)
  41. /**
  42. * Asic specific macros : see bfa_hw_cb.c and bfa_hw_ct.c for details.
  43. */
  44. #define bfa_ioc_firmware_lock(__ioc) \
  45. ((__ioc)->ioc_hwif->ioc_firmware_lock(__ioc))
  46. #define bfa_ioc_firmware_unlock(__ioc) \
  47. ((__ioc)->ioc_hwif->ioc_firmware_unlock(__ioc))
  48. #define bfa_ioc_reg_init(__ioc) ((__ioc)->ioc_hwif->ioc_reg_init(__ioc))
  49. #define bfa_ioc_map_port(__ioc) ((__ioc)->ioc_hwif->ioc_map_port(__ioc))
  50. #define bfa_ioc_notify_hbfail(__ioc) \
  51. ((__ioc)->ioc_hwif->ioc_notify_hbfail(__ioc))
  52. #define bfa_ioc_is_optrom(__ioc) \
  53. (bfa_cb_image_get_size(BFA_IOC_FWIMG_TYPE(__ioc)) < BFA_IOC_FWIMG_MINSZ)
  54. #define bfa_ioc_mbox_cmd_pending(__ioc) \
  55. (!list_empty(&((__ioc)->mbox_mod.cmd_q)) || \
  56. readl((__ioc)->ioc_regs.hfn_mbox_cmd))
  57. static bool bfa_nw_auto_recover = true;
  58. /*
  59. * forward declarations
  60. */
  61. static void bfa_ioc_hw_sem_get(struct bfa_ioc *ioc);
  62. static void bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc);
  63. static void bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force);
  64. static void bfa_ioc_send_enable(struct bfa_ioc *ioc);
  65. static void bfa_ioc_send_disable(struct bfa_ioc *ioc);
  66. static void bfa_ioc_send_getattr(struct bfa_ioc *ioc);
  67. static void bfa_ioc_hb_monitor(struct bfa_ioc *ioc);
  68. static void bfa_ioc_hb_stop(struct bfa_ioc *ioc);
  69. static void bfa_ioc_reset(struct bfa_ioc *ioc, bool force);
  70. static void bfa_ioc_mbox_poll(struct bfa_ioc *ioc);
  71. static void bfa_ioc_mbox_hbfail(struct bfa_ioc *ioc);
  72. static void bfa_ioc_recover(struct bfa_ioc *ioc);
  73. static void bfa_ioc_check_attr_wwns(struct bfa_ioc *ioc);
  74. static void bfa_ioc_disable_comp(struct bfa_ioc *ioc);
  75. static void bfa_ioc_lpu_stop(struct bfa_ioc *ioc);
  76. static void bfa_ioc_boot(struct bfa_ioc *ioc, u32 boot_type,
  77. u32 boot_param);
  78. static u32 bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr);
  79. static u32 bfa_ioc_smem_pgoff(struct bfa_ioc *ioc, u32 fmaddr);
  80. static void bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc,
  81. char *serial_num);
  82. static void bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc,
  83. char *fw_ver);
  84. static void bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc,
  85. char *chip_rev);
  86. static void bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc,
  87. char *optrom_ver);
  88. static void bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc,
  89. char *manufacturer);
  90. static void bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model);
  91. static u64 bfa_ioc_get_pwwn(struct bfa_ioc *ioc);
  92. static mac_t bfa_ioc_get_mfg_mac(struct bfa_ioc *ioc);
  93. /**
  94. * IOC state machine events
  95. */
  96. enum ioc_event {
  97. IOC_E_ENABLE = 1, /*!< IOC enable request */
  98. IOC_E_DISABLE = 2, /*!< IOC disable request */
  99. IOC_E_TIMEOUT = 3, /*!< f/w response timeout */
  100. IOC_E_FWREADY = 4, /*!< f/w initialization done */
  101. IOC_E_FWRSP_GETATTR = 5, /*!< IOC get attribute response */
  102. IOC_E_FWRSP_ENABLE = 6, /*!< enable f/w response */
  103. IOC_E_FWRSP_DISABLE = 7, /*!< disable f/w response */
  104. IOC_E_HBFAIL = 8, /*!< heartbeat failure */
  105. IOC_E_HWERROR = 9, /*!< hardware error interrupt */
  106. IOC_E_SEMLOCKED = 10, /*!< h/w semaphore is locked */
  107. IOC_E_DETACH = 11, /*!< driver detach cleanup */
  108. };
  109. bfa_fsm_state_decl(bfa_ioc, reset, struct bfa_ioc, enum ioc_event);
  110. bfa_fsm_state_decl(bfa_ioc, fwcheck, struct bfa_ioc, enum ioc_event);
  111. bfa_fsm_state_decl(bfa_ioc, mismatch, struct bfa_ioc, enum ioc_event);
  112. bfa_fsm_state_decl(bfa_ioc, semwait, struct bfa_ioc, enum ioc_event);
  113. bfa_fsm_state_decl(bfa_ioc, hwinit, struct bfa_ioc, enum ioc_event);
  114. bfa_fsm_state_decl(bfa_ioc, enabling, struct bfa_ioc, enum ioc_event);
  115. bfa_fsm_state_decl(bfa_ioc, getattr, struct bfa_ioc, enum ioc_event);
  116. bfa_fsm_state_decl(bfa_ioc, op, struct bfa_ioc, enum ioc_event);
  117. bfa_fsm_state_decl(bfa_ioc, initfail, struct bfa_ioc, enum ioc_event);
  118. bfa_fsm_state_decl(bfa_ioc, hbfail, struct bfa_ioc, enum ioc_event);
  119. bfa_fsm_state_decl(bfa_ioc, disabling, struct bfa_ioc, enum ioc_event);
  120. bfa_fsm_state_decl(bfa_ioc, disabled, struct bfa_ioc, enum ioc_event);
  121. static struct bfa_sm_table ioc_sm_table[] = {
  122. {BFA_SM(bfa_ioc_sm_reset), BFA_IOC_RESET},
  123. {BFA_SM(bfa_ioc_sm_fwcheck), BFA_IOC_FWMISMATCH},
  124. {BFA_SM(bfa_ioc_sm_mismatch), BFA_IOC_FWMISMATCH},
  125. {BFA_SM(bfa_ioc_sm_semwait), BFA_IOC_SEMWAIT},
  126. {BFA_SM(bfa_ioc_sm_hwinit), BFA_IOC_HWINIT},
  127. {BFA_SM(bfa_ioc_sm_enabling), BFA_IOC_HWINIT},
  128. {BFA_SM(bfa_ioc_sm_getattr), BFA_IOC_GETATTR},
  129. {BFA_SM(bfa_ioc_sm_op), BFA_IOC_OPERATIONAL},
  130. {BFA_SM(bfa_ioc_sm_initfail), BFA_IOC_INITFAIL},
  131. {BFA_SM(bfa_ioc_sm_hbfail), BFA_IOC_HBFAIL},
  132. {BFA_SM(bfa_ioc_sm_disabling), BFA_IOC_DISABLING},
  133. {BFA_SM(bfa_ioc_sm_disabled), BFA_IOC_DISABLED},
  134. };
  135. /**
  136. * Reset entry actions -- initialize state machine
  137. */
  138. static void
  139. bfa_ioc_sm_reset_entry(struct bfa_ioc *ioc)
  140. {
  141. ioc->retry_count = 0;
  142. ioc->auto_recover = bfa_nw_auto_recover;
  143. }
  144. /**
  145. * Beginning state. IOC is in reset state.
  146. */
  147. static void
  148. bfa_ioc_sm_reset(struct bfa_ioc *ioc, enum ioc_event event)
  149. {
  150. switch (event) {
  151. case IOC_E_ENABLE:
  152. bfa_fsm_set_state(ioc, bfa_ioc_sm_fwcheck);
  153. break;
  154. case IOC_E_DISABLE:
  155. bfa_ioc_disable_comp(ioc);
  156. break;
  157. case IOC_E_DETACH:
  158. break;
  159. default:
  160. bfa_sm_fault(ioc, event);
  161. }
  162. }
  163. /**
  164. * Semaphore should be acquired for version check.
  165. */
  166. static void
  167. bfa_ioc_sm_fwcheck_entry(struct bfa_ioc *ioc)
  168. {
  169. bfa_ioc_hw_sem_get(ioc);
  170. }
  171. /**
  172. * Awaiting h/w semaphore to continue with version check.
  173. */
  174. static void
  175. bfa_ioc_sm_fwcheck(struct bfa_ioc *ioc, enum ioc_event event)
  176. {
  177. switch (event) {
  178. case IOC_E_SEMLOCKED:
  179. if (bfa_ioc_firmware_lock(ioc)) {
  180. ioc->retry_count = 0;
  181. bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
  182. } else {
  183. bfa_nw_ioc_hw_sem_release(ioc);
  184. bfa_fsm_set_state(ioc, bfa_ioc_sm_mismatch);
  185. }
  186. break;
  187. case IOC_E_DISABLE:
  188. bfa_ioc_disable_comp(ioc);
  189. /* fall through */
  190. case IOC_E_DETACH:
  191. bfa_ioc_hw_sem_get_cancel(ioc);
  192. bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
  193. break;
  194. case IOC_E_FWREADY:
  195. break;
  196. default:
  197. bfa_sm_fault(ioc, event);
  198. }
  199. }
  200. /**
  201. * Notify enable completion callback and generate mismatch AEN.
  202. */
  203. static void
  204. bfa_ioc_sm_mismatch_entry(struct bfa_ioc *ioc)
  205. {
  206. /**
  207. * Provide enable completion callback and AEN notification only once.
  208. */
  209. if (ioc->retry_count == 0)
  210. ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
  211. ioc->retry_count++;
  212. bfa_ioc_timer_start(ioc);
  213. }
  214. /**
  215. * Awaiting firmware version match.
  216. */
  217. static void
  218. bfa_ioc_sm_mismatch(struct bfa_ioc *ioc, enum ioc_event event)
  219. {
  220. switch (event) {
  221. case IOC_E_TIMEOUT:
  222. bfa_fsm_set_state(ioc, bfa_ioc_sm_fwcheck);
  223. break;
  224. case IOC_E_DISABLE:
  225. bfa_ioc_disable_comp(ioc);
  226. /* fall through */
  227. case IOC_E_DETACH:
  228. bfa_ioc_timer_stop(ioc);
  229. bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
  230. break;
  231. case IOC_E_FWREADY:
  232. break;
  233. default:
  234. bfa_sm_fault(ioc, event);
  235. }
  236. }
  237. /**
  238. * Request for semaphore.
  239. */
  240. static void
  241. bfa_ioc_sm_semwait_entry(struct bfa_ioc *ioc)
  242. {
  243. bfa_ioc_hw_sem_get(ioc);
  244. }
  245. /**
  246. * Awaiting semaphore for h/w initialzation.
  247. */
  248. static void
  249. bfa_ioc_sm_semwait(struct bfa_ioc *ioc, enum ioc_event event)
  250. {
  251. switch (event) {
  252. case IOC_E_SEMLOCKED:
  253. ioc->retry_count = 0;
  254. bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
  255. break;
  256. case IOC_E_DISABLE:
  257. bfa_ioc_hw_sem_get_cancel(ioc);
  258. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  259. break;
  260. default:
  261. bfa_sm_fault(ioc, event);
  262. }
  263. }
  264. static void
  265. bfa_ioc_sm_hwinit_entry(struct bfa_ioc *ioc)
  266. {
  267. bfa_ioc_timer_start(ioc);
  268. bfa_ioc_reset(ioc, false);
  269. }
  270. /**
  271. * @brief
  272. * Hardware is being initialized. Interrupts are enabled.
  273. * Holding hardware semaphore lock.
  274. */
  275. static void
  276. bfa_ioc_sm_hwinit(struct bfa_ioc *ioc, enum ioc_event event)
  277. {
  278. switch (event) {
  279. case IOC_E_FWREADY:
  280. bfa_ioc_timer_stop(ioc);
  281. bfa_fsm_set_state(ioc, bfa_ioc_sm_enabling);
  282. break;
  283. case IOC_E_HWERROR:
  284. bfa_ioc_timer_stop(ioc);
  285. /* fall through */
  286. case IOC_E_TIMEOUT:
  287. ioc->retry_count++;
  288. if (ioc->retry_count < BFA_IOC_HWINIT_MAX) {
  289. bfa_ioc_timer_start(ioc);
  290. bfa_ioc_reset(ioc, true);
  291. break;
  292. }
  293. bfa_nw_ioc_hw_sem_release(ioc);
  294. bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
  295. break;
  296. case IOC_E_DISABLE:
  297. bfa_nw_ioc_hw_sem_release(ioc);
  298. bfa_ioc_timer_stop(ioc);
  299. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  300. break;
  301. default:
  302. bfa_sm_fault(ioc, event);
  303. }
  304. }
  305. static void
  306. bfa_ioc_sm_enabling_entry(struct bfa_ioc *ioc)
  307. {
  308. bfa_ioc_timer_start(ioc);
  309. bfa_ioc_send_enable(ioc);
  310. }
  311. /**
  312. * Host IOC function is being enabled, awaiting response from firmware.
  313. * Semaphore is acquired.
  314. */
  315. static void
  316. bfa_ioc_sm_enabling(struct bfa_ioc *ioc, enum ioc_event event)
  317. {
  318. switch (event) {
  319. case IOC_E_FWRSP_ENABLE:
  320. bfa_ioc_timer_stop(ioc);
  321. bfa_nw_ioc_hw_sem_release(ioc);
  322. bfa_fsm_set_state(ioc, bfa_ioc_sm_getattr);
  323. break;
  324. case IOC_E_HWERROR:
  325. bfa_ioc_timer_stop(ioc);
  326. /* fall through */
  327. case IOC_E_TIMEOUT:
  328. ioc->retry_count++;
  329. if (ioc->retry_count < BFA_IOC_HWINIT_MAX) {
  330. writel(BFI_IOC_UNINIT,
  331. ioc->ioc_regs.ioc_fwstate);
  332. bfa_fsm_set_state(ioc, bfa_ioc_sm_hwinit);
  333. break;
  334. }
  335. bfa_nw_ioc_hw_sem_release(ioc);
  336. bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
  337. break;
  338. case IOC_E_DISABLE:
  339. bfa_ioc_timer_stop(ioc);
  340. bfa_nw_ioc_hw_sem_release(ioc);
  341. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  342. break;
  343. case IOC_E_FWREADY:
  344. bfa_ioc_send_enable(ioc);
  345. break;
  346. default:
  347. bfa_sm_fault(ioc, event);
  348. }
  349. }
  350. static void
  351. bfa_ioc_sm_getattr_entry(struct bfa_ioc *ioc)
  352. {
  353. bfa_ioc_timer_start(ioc);
  354. bfa_ioc_send_getattr(ioc);
  355. }
  356. /**
  357. * @brief
  358. * IOC configuration in progress. Timer is active.
  359. */
  360. static void
  361. bfa_ioc_sm_getattr(struct bfa_ioc *ioc, enum ioc_event event)
  362. {
  363. switch (event) {
  364. case IOC_E_FWRSP_GETATTR:
  365. bfa_ioc_timer_stop(ioc);
  366. bfa_ioc_check_attr_wwns(ioc);
  367. bfa_fsm_set_state(ioc, bfa_ioc_sm_op);
  368. break;
  369. case IOC_E_HWERROR:
  370. bfa_ioc_timer_stop(ioc);
  371. /* fall through */
  372. case IOC_E_TIMEOUT:
  373. bfa_fsm_set_state(ioc, bfa_ioc_sm_initfail);
  374. break;
  375. case IOC_E_DISABLE:
  376. bfa_ioc_timer_stop(ioc);
  377. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  378. break;
  379. default:
  380. bfa_sm_fault(ioc, event);
  381. }
  382. }
  383. static void
  384. bfa_ioc_sm_op_entry(struct bfa_ioc *ioc)
  385. {
  386. ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_OK);
  387. bfa_ioc_hb_monitor(ioc);
  388. }
  389. static void
  390. bfa_ioc_sm_op(struct bfa_ioc *ioc, enum ioc_event event)
  391. {
  392. switch (event) {
  393. case IOC_E_ENABLE:
  394. break;
  395. case IOC_E_DISABLE:
  396. bfa_ioc_hb_stop(ioc);
  397. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabling);
  398. break;
  399. case IOC_E_HWERROR:
  400. case IOC_E_FWREADY:
  401. /**
  402. * Hard error or IOC recovery by other function.
  403. * Treat it same as heartbeat failure.
  404. */
  405. bfa_ioc_hb_stop(ioc);
  406. /* !!! fall through !!! */
  407. case IOC_E_HBFAIL:
  408. bfa_fsm_set_state(ioc, bfa_ioc_sm_hbfail);
  409. break;
  410. default:
  411. bfa_sm_fault(ioc, event);
  412. }
  413. }
  414. static void
  415. bfa_ioc_sm_disabling_entry(struct bfa_ioc *ioc)
  416. {
  417. bfa_ioc_timer_start(ioc);
  418. bfa_ioc_send_disable(ioc);
  419. }
  420. /**
  421. * IOC is being disabled
  422. */
  423. static void
  424. bfa_ioc_sm_disabling(struct bfa_ioc *ioc, enum ioc_event event)
  425. {
  426. switch (event) {
  427. case IOC_E_FWRSP_DISABLE:
  428. bfa_ioc_timer_stop(ioc);
  429. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  430. break;
  431. case IOC_E_HWERROR:
  432. bfa_ioc_timer_stop(ioc);
  433. /*
  434. * !!! fall through !!!
  435. */
  436. case IOC_E_TIMEOUT:
  437. writel(BFI_IOC_FAIL, ioc->ioc_regs.ioc_fwstate);
  438. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  439. break;
  440. default:
  441. bfa_sm_fault(ioc, event);
  442. }
  443. }
  444. /**
  445. * IOC disable completion entry.
  446. */
  447. static void
  448. bfa_ioc_sm_disabled_entry(struct bfa_ioc *ioc)
  449. {
  450. bfa_ioc_disable_comp(ioc);
  451. }
  452. static void
  453. bfa_ioc_sm_disabled(struct bfa_ioc *ioc, enum ioc_event event)
  454. {
  455. switch (event) {
  456. case IOC_E_ENABLE:
  457. bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
  458. break;
  459. case IOC_E_DISABLE:
  460. ioc->cbfn->disable_cbfn(ioc->bfa);
  461. break;
  462. case IOC_E_FWREADY:
  463. break;
  464. case IOC_E_DETACH:
  465. bfa_ioc_firmware_unlock(ioc);
  466. bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
  467. break;
  468. default:
  469. bfa_sm_fault(ioc, event);
  470. }
  471. }
  472. static void
  473. bfa_ioc_sm_initfail_entry(struct bfa_ioc *ioc)
  474. {
  475. ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
  476. bfa_ioc_timer_start(ioc);
  477. }
  478. /**
  479. * @brief
  480. * Hardware initialization failed.
  481. */
  482. static void
  483. bfa_ioc_sm_initfail(struct bfa_ioc *ioc, enum ioc_event event)
  484. {
  485. switch (event) {
  486. case IOC_E_DISABLE:
  487. bfa_ioc_timer_stop(ioc);
  488. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  489. break;
  490. case IOC_E_DETACH:
  491. bfa_ioc_timer_stop(ioc);
  492. bfa_ioc_firmware_unlock(ioc);
  493. bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
  494. break;
  495. case IOC_E_TIMEOUT:
  496. bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
  497. break;
  498. default:
  499. bfa_sm_fault(ioc, event);
  500. }
  501. }
  502. static void
  503. bfa_ioc_sm_hbfail_entry(struct bfa_ioc *ioc)
  504. {
  505. struct list_head *qe;
  506. struct bfa_ioc_hbfail_notify *notify;
  507. /**
  508. * Mark IOC as failed in hardware and stop firmware.
  509. */
  510. bfa_ioc_lpu_stop(ioc);
  511. writel(BFI_IOC_FAIL, ioc->ioc_regs.ioc_fwstate);
  512. /**
  513. * Notify other functions on HB failure.
  514. */
  515. bfa_ioc_notify_hbfail(ioc);
  516. /**
  517. * Notify driver and common modules registered for notification.
  518. */
  519. ioc->cbfn->hbfail_cbfn(ioc->bfa);
  520. list_for_each(qe, &ioc->hb_notify_q) {
  521. notify = (struct bfa_ioc_hbfail_notify *) qe;
  522. notify->cbfn(notify->cbarg);
  523. }
  524. /**
  525. * Flush any queued up mailbox requests.
  526. */
  527. bfa_ioc_mbox_hbfail(ioc);
  528. /**
  529. * Trigger auto-recovery after a delay.
  530. */
  531. if (ioc->auto_recover)
  532. mod_timer(&ioc->ioc_timer, jiffies +
  533. msecs_to_jiffies(BFA_IOC_TOV_RECOVER));
  534. }
  535. /**
  536. * @brief
  537. * IOC heartbeat failure.
  538. */
  539. static void
  540. bfa_ioc_sm_hbfail(struct bfa_ioc *ioc, enum ioc_event event)
  541. {
  542. switch (event) {
  543. case IOC_E_ENABLE:
  544. ioc->cbfn->enable_cbfn(ioc->bfa, BFA_STATUS_IOC_FAILURE);
  545. break;
  546. case IOC_E_DISABLE:
  547. if (ioc->auto_recover)
  548. bfa_ioc_timer_stop(ioc);
  549. bfa_fsm_set_state(ioc, bfa_ioc_sm_disabled);
  550. break;
  551. case IOC_E_TIMEOUT:
  552. bfa_fsm_set_state(ioc, bfa_ioc_sm_semwait);
  553. break;
  554. case IOC_E_FWREADY:
  555. /**
  556. * Recovery is already initiated by other function.
  557. */
  558. break;
  559. case IOC_E_HWERROR:
  560. /*
  561. * HB failure notification, ignore.
  562. */
  563. break;
  564. default:
  565. bfa_sm_fault(ioc, event);
  566. }
  567. }
  568. /**
  569. * BFA IOC private functions
  570. */
  571. static void
  572. bfa_ioc_disable_comp(struct bfa_ioc *ioc)
  573. {
  574. struct list_head *qe;
  575. struct bfa_ioc_hbfail_notify *notify;
  576. ioc->cbfn->disable_cbfn(ioc->bfa);
  577. /**
  578. * Notify common modules registered for notification.
  579. */
  580. list_for_each(qe, &ioc->hb_notify_q) {
  581. notify = (struct bfa_ioc_hbfail_notify *) qe;
  582. notify->cbfn(notify->cbarg);
  583. }
  584. }
  585. void
  586. bfa_nw_ioc_sem_timeout(void *ioc_arg)
  587. {
  588. struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
  589. bfa_ioc_hw_sem_get(ioc);
  590. }
  591. bool
  592. bfa_nw_ioc_sem_get(void __iomem *sem_reg)
  593. {
  594. u32 r32;
  595. int cnt = 0;
  596. #define BFA_SEM_SPINCNT 3000
  597. r32 = readl(sem_reg);
  598. while (r32 && (cnt < BFA_SEM_SPINCNT)) {
  599. cnt++;
  600. udelay(2);
  601. r32 = readl(sem_reg);
  602. }
  603. if (r32 == 0)
  604. return true;
  605. BUG_ON(!(cnt < BFA_SEM_SPINCNT));
  606. return false;
  607. }
  608. void
  609. bfa_nw_ioc_sem_release(void __iomem *sem_reg)
  610. {
  611. writel(1, sem_reg);
  612. }
  613. static void
  614. bfa_ioc_hw_sem_get(struct bfa_ioc *ioc)
  615. {
  616. u32 r32;
  617. /**
  618. * First read to the semaphore register will return 0, subsequent reads
  619. * will return 1. Semaphore is released by writing 1 to the register
  620. */
  621. r32 = readl(ioc->ioc_regs.ioc_sem_reg);
  622. if (r32 == 0) {
  623. bfa_fsm_send_event(ioc, IOC_E_SEMLOCKED);
  624. return;
  625. }
  626. mod_timer(&ioc->sem_timer, jiffies +
  627. msecs_to_jiffies(BFA_IOC_HWSEM_TOV));
  628. }
  629. void
  630. bfa_nw_ioc_hw_sem_release(struct bfa_ioc *ioc)
  631. {
  632. writel(1, ioc->ioc_regs.ioc_sem_reg);
  633. }
  634. static void
  635. bfa_ioc_hw_sem_get_cancel(struct bfa_ioc *ioc)
  636. {
  637. del_timer(&ioc->sem_timer);
  638. }
  639. /**
  640. * @brief
  641. * Initialize LPU local memory (aka secondary memory / SRAM)
  642. */
  643. static void
  644. bfa_ioc_lmem_init(struct bfa_ioc *ioc)
  645. {
  646. u32 pss_ctl;
  647. int i;
  648. #define PSS_LMEM_INIT_TIME 10000
  649. pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
  650. pss_ctl &= ~__PSS_LMEM_RESET;
  651. pss_ctl |= __PSS_LMEM_INIT_EN;
  652. /*
  653. * i2c workaround 12.5khz clock
  654. */
  655. pss_ctl |= __PSS_I2C_CLK_DIV(3UL);
  656. writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
  657. /**
  658. * wait for memory initialization to be complete
  659. */
  660. i = 0;
  661. do {
  662. pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
  663. i++;
  664. } while (!(pss_ctl & __PSS_LMEM_INIT_DONE) && (i < PSS_LMEM_INIT_TIME));
  665. /**
  666. * If memory initialization is not successful, IOC timeout will catch
  667. * such failures.
  668. */
  669. BUG_ON(!(pss_ctl & __PSS_LMEM_INIT_DONE));
  670. pss_ctl &= ~(__PSS_LMEM_INIT_DONE | __PSS_LMEM_INIT_EN);
  671. writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
  672. }
  673. static void
  674. bfa_ioc_lpu_start(struct bfa_ioc *ioc)
  675. {
  676. u32 pss_ctl;
  677. /**
  678. * Take processor out of reset.
  679. */
  680. pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
  681. pss_ctl &= ~__PSS_LPU0_RESET;
  682. writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
  683. }
  684. static void
  685. bfa_ioc_lpu_stop(struct bfa_ioc *ioc)
  686. {
  687. u32 pss_ctl;
  688. /**
  689. * Put processors in reset.
  690. */
  691. pss_ctl = readl(ioc->ioc_regs.pss_ctl_reg);
  692. pss_ctl |= (__PSS_LPU0_RESET | __PSS_LPU1_RESET);
  693. writel(pss_ctl, ioc->ioc_regs.pss_ctl_reg);
  694. }
  695. /**
  696. * Get driver and firmware versions.
  697. */
  698. void
  699. bfa_nw_ioc_fwver_get(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
  700. {
  701. u32 pgnum, pgoff;
  702. u32 loff = 0;
  703. int i;
  704. u32 *fwsig = (u32 *) fwhdr;
  705. pgnum = bfa_ioc_smem_pgnum(ioc, loff);
  706. pgoff = bfa_ioc_smem_pgoff(ioc, loff);
  707. writel(pgnum, ioc->ioc_regs.host_page_num_fn);
  708. for (i = 0; i < (sizeof(struct bfi_ioc_image_hdr) / sizeof(u32));
  709. i++) {
  710. fwsig[i] =
  711. swab32(readl((loff) + (ioc->ioc_regs.smem_page_start)));
  712. loff += sizeof(u32);
  713. }
  714. }
  715. /**
  716. * Returns TRUE if same.
  717. */
  718. bool
  719. bfa_nw_ioc_fwver_cmp(struct bfa_ioc *ioc, struct bfi_ioc_image_hdr *fwhdr)
  720. {
  721. struct bfi_ioc_image_hdr *drv_fwhdr;
  722. int i;
  723. drv_fwhdr = (struct bfi_ioc_image_hdr *)
  724. bfa_cb_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), 0);
  725. for (i = 0; i < BFI_IOC_MD5SUM_SZ; i++) {
  726. if (fwhdr->md5sum[i] != drv_fwhdr->md5sum[i])
  727. return false;
  728. }
  729. return true;
  730. }
  731. /**
  732. * Return true if current running version is valid. Firmware signature and
  733. * execution context (driver/bios) must match.
  734. */
  735. static bool
  736. bfa_ioc_fwver_valid(struct bfa_ioc *ioc)
  737. {
  738. struct bfi_ioc_image_hdr fwhdr, *drv_fwhdr;
  739. /**
  740. * If bios/efi boot (flash based) -- return true
  741. */
  742. if (bfa_ioc_is_optrom(ioc))
  743. return true;
  744. bfa_nw_ioc_fwver_get(ioc, &fwhdr);
  745. drv_fwhdr = (struct bfi_ioc_image_hdr *)
  746. bfa_cb_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), 0);
  747. if (fwhdr.signature != drv_fwhdr->signature)
  748. return false;
  749. if (fwhdr.exec != drv_fwhdr->exec)
  750. return false;
  751. return bfa_nw_ioc_fwver_cmp(ioc, &fwhdr);
  752. }
  753. /**
  754. * Conditionally flush any pending message from firmware at start.
  755. */
  756. static void
  757. bfa_ioc_msgflush(struct bfa_ioc *ioc)
  758. {
  759. u32 r32;
  760. r32 = readl(ioc->ioc_regs.lpu_mbox_cmd);
  761. if (r32)
  762. writel(1, ioc->ioc_regs.lpu_mbox_cmd);
  763. }
  764. /**
  765. * @img ioc_init_logic.jpg
  766. */
  767. static void
  768. bfa_ioc_hwinit(struct bfa_ioc *ioc, bool force)
  769. {
  770. enum bfi_ioc_state ioc_fwstate;
  771. bool fwvalid;
  772. ioc_fwstate = readl(ioc->ioc_regs.ioc_fwstate);
  773. if (force)
  774. ioc_fwstate = BFI_IOC_UNINIT;
  775. /**
  776. * check if firmware is valid
  777. */
  778. fwvalid = (ioc_fwstate == BFI_IOC_UNINIT) ?
  779. false : bfa_ioc_fwver_valid(ioc);
  780. if (!fwvalid) {
  781. bfa_ioc_boot(ioc, BFI_BOOT_TYPE_NORMAL, ioc->pcidev.device_id);
  782. return;
  783. }
  784. /**
  785. * If hardware initialization is in progress (initialized by other IOC),
  786. * just wait for an initialization completion interrupt.
  787. */
  788. if (ioc_fwstate == BFI_IOC_INITING) {
  789. ioc->cbfn->reset_cbfn(ioc->bfa);
  790. return;
  791. }
  792. /**
  793. * If IOC function is disabled and firmware version is same,
  794. * just re-enable IOC.
  795. *
  796. * If option rom, IOC must not be in operational state. With
  797. * convergence, IOC will be in operational state when 2nd driver
  798. * is loaded.
  799. */
  800. if (ioc_fwstate == BFI_IOC_DISABLED ||
  801. (!bfa_ioc_is_optrom(ioc) && ioc_fwstate == BFI_IOC_OP)) {
  802. /**
  803. * When using MSI-X any pending firmware ready event should
  804. * be flushed. Otherwise MSI-X interrupts are not delivered.
  805. */
  806. bfa_ioc_msgflush(ioc);
  807. ioc->cbfn->reset_cbfn(ioc->bfa);
  808. bfa_fsm_send_event(ioc, IOC_E_FWREADY);
  809. return;
  810. }
  811. /**
  812. * Initialize the h/w for any other states.
  813. */
  814. bfa_ioc_boot(ioc, BFI_BOOT_TYPE_NORMAL, ioc->pcidev.device_id);
  815. }
  816. void
  817. bfa_nw_ioc_timeout(void *ioc_arg)
  818. {
  819. struct bfa_ioc *ioc = (struct bfa_ioc *) ioc_arg;
  820. bfa_fsm_send_event(ioc, IOC_E_TIMEOUT);
  821. }
  822. static void
  823. bfa_ioc_mbox_send(struct bfa_ioc *ioc, void *ioc_msg, int len)
  824. {
  825. u32 *msgp = (u32 *) ioc_msg;
  826. u32 i;
  827. BUG_ON(!(len <= BFI_IOC_MSGLEN_MAX));
  828. /*
  829. * first write msg to mailbox registers
  830. */
  831. for (i = 0; i < len / sizeof(u32); i++)
  832. writel(cpu_to_le32(msgp[i]),
  833. ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
  834. for (; i < BFI_IOC_MSGLEN_MAX / sizeof(u32); i++)
  835. writel(0, ioc->ioc_regs.hfn_mbox + i * sizeof(u32));
  836. /*
  837. * write 1 to mailbox CMD to trigger LPU event
  838. */
  839. writel(1, ioc->ioc_regs.hfn_mbox_cmd);
  840. (void) readl(ioc->ioc_regs.hfn_mbox_cmd);
  841. }
  842. static void
  843. bfa_ioc_send_enable(struct bfa_ioc *ioc)
  844. {
  845. struct bfi_ioc_ctrl_req enable_req;
  846. struct timeval tv;
  847. bfi_h2i_set(enable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_ENABLE_REQ,
  848. bfa_ioc_portid(ioc));
  849. enable_req.ioc_class = ioc->ioc_mc;
  850. do_gettimeofday(&tv);
  851. enable_req.tv_sec = ntohl(tv.tv_sec);
  852. bfa_ioc_mbox_send(ioc, &enable_req, sizeof(struct bfi_ioc_ctrl_req));
  853. }
  854. static void
  855. bfa_ioc_send_disable(struct bfa_ioc *ioc)
  856. {
  857. struct bfi_ioc_ctrl_req disable_req;
  858. bfi_h2i_set(disable_req.mh, BFI_MC_IOC, BFI_IOC_H2I_DISABLE_REQ,
  859. bfa_ioc_portid(ioc));
  860. bfa_ioc_mbox_send(ioc, &disable_req, sizeof(struct bfi_ioc_ctrl_req));
  861. }
  862. static void
  863. bfa_ioc_send_getattr(struct bfa_ioc *ioc)
  864. {
  865. struct bfi_ioc_getattr_req attr_req;
  866. bfi_h2i_set(attr_req.mh, BFI_MC_IOC, BFI_IOC_H2I_GETATTR_REQ,
  867. bfa_ioc_portid(ioc));
  868. bfa_dma_be_addr_set(attr_req.attr_addr, ioc->attr_dma.pa);
  869. bfa_ioc_mbox_send(ioc, &attr_req, sizeof(attr_req));
  870. }
  871. void
  872. bfa_nw_ioc_hb_check(void *cbarg)
  873. {
  874. struct bfa_ioc *ioc = cbarg;
  875. u32 hb_count;
  876. hb_count = readl(ioc->ioc_regs.heartbeat);
  877. if (ioc->hb_count == hb_count) {
  878. pr_crit("Firmware heartbeat failure at %d", hb_count);
  879. bfa_ioc_recover(ioc);
  880. return;
  881. } else {
  882. ioc->hb_count = hb_count;
  883. }
  884. bfa_ioc_mbox_poll(ioc);
  885. mod_timer(&ioc->hb_timer, jiffies +
  886. msecs_to_jiffies(BFA_IOC_HB_TOV));
  887. }
  888. static void
  889. bfa_ioc_hb_monitor(struct bfa_ioc *ioc)
  890. {
  891. ioc->hb_count = readl(ioc->ioc_regs.heartbeat);
  892. mod_timer(&ioc->hb_timer, jiffies +
  893. msecs_to_jiffies(BFA_IOC_HB_TOV));
  894. }
  895. static void
  896. bfa_ioc_hb_stop(struct bfa_ioc *ioc)
  897. {
  898. del_timer(&ioc->hb_timer);
  899. }
  900. /**
  901. * @brief
  902. * Initiate a full firmware download.
  903. */
  904. static void
  905. bfa_ioc_download_fw(struct bfa_ioc *ioc, u32 boot_type,
  906. u32 boot_param)
  907. {
  908. u32 *fwimg;
  909. u32 pgnum, pgoff;
  910. u32 loff = 0;
  911. u32 chunkno = 0;
  912. u32 i;
  913. /**
  914. * Initialize LMEM first before code download
  915. */
  916. bfa_ioc_lmem_init(ioc);
  917. /**
  918. * Flash based firmware boot
  919. */
  920. if (bfa_ioc_is_optrom(ioc))
  921. boot_type = BFI_BOOT_TYPE_FLASH;
  922. fwimg = bfa_cb_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc), chunkno);
  923. pgnum = bfa_ioc_smem_pgnum(ioc, loff);
  924. pgoff = bfa_ioc_smem_pgoff(ioc, loff);
  925. writel(pgnum, ioc->ioc_regs.host_page_num_fn);
  926. for (i = 0; i < bfa_cb_image_get_size(BFA_IOC_FWIMG_TYPE(ioc)); i++) {
  927. if (BFA_IOC_FLASH_CHUNK_NO(i) != chunkno) {
  928. chunkno = BFA_IOC_FLASH_CHUNK_NO(i);
  929. fwimg = bfa_cb_image_get_chunk(BFA_IOC_FWIMG_TYPE(ioc),
  930. BFA_IOC_FLASH_CHUNK_ADDR(chunkno));
  931. }
  932. /**
  933. * write smem
  934. */
  935. writel((swab32(fwimg[BFA_IOC_FLASH_OFFSET_IN_CHUNK(i)])),
  936. ((ioc->ioc_regs.smem_page_start) + (loff)));
  937. loff += sizeof(u32);
  938. /**
  939. * handle page offset wrap around
  940. */
  941. loff = PSS_SMEM_PGOFF(loff);
  942. if (loff == 0) {
  943. pgnum++;
  944. writel(pgnum,
  945. ioc->ioc_regs.host_page_num_fn);
  946. }
  947. }
  948. writel(bfa_ioc_smem_pgnum(ioc, 0),
  949. ioc->ioc_regs.host_page_num_fn);
  950. /*
  951. * Set boot type and boot param at the end.
  952. */
  953. writel((swab32(swab32(boot_type))), ((ioc->ioc_regs.smem_page_start)
  954. + (BFI_BOOT_TYPE_OFF)));
  955. writel((swab32(swab32(boot_param))), ((ioc->ioc_regs.smem_page_start)
  956. + (BFI_BOOT_PARAM_OFF)));
  957. }
  958. static void
  959. bfa_ioc_reset(struct bfa_ioc *ioc, bool force)
  960. {
  961. bfa_ioc_hwinit(ioc, force);
  962. }
  963. /**
  964. * @brief
  965. * Update BFA configuration from firmware configuration.
  966. */
  967. static void
  968. bfa_ioc_getattr_reply(struct bfa_ioc *ioc)
  969. {
  970. struct bfi_ioc_attr *attr = ioc->attr;
  971. attr->adapter_prop = ntohl(attr->adapter_prop);
  972. attr->card_type = ntohl(attr->card_type);
  973. attr->maxfrsize = ntohs(attr->maxfrsize);
  974. bfa_fsm_send_event(ioc, IOC_E_FWRSP_GETATTR);
  975. }
  976. /**
  977. * Attach time initialization of mbox logic.
  978. */
  979. static void
  980. bfa_ioc_mbox_attach(struct bfa_ioc *ioc)
  981. {
  982. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  983. int mc;
  984. INIT_LIST_HEAD(&mod->cmd_q);
  985. for (mc = 0; mc < BFI_MC_MAX; mc++) {
  986. mod->mbhdlr[mc].cbfn = NULL;
  987. mod->mbhdlr[mc].cbarg = ioc->bfa;
  988. }
  989. }
  990. /**
  991. * Mbox poll timer -- restarts any pending mailbox requests.
  992. */
  993. static void
  994. bfa_ioc_mbox_poll(struct bfa_ioc *ioc)
  995. {
  996. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  997. struct bfa_mbox_cmd *cmd;
  998. u32 stat;
  999. /**
  1000. * If no command pending, do nothing
  1001. */
  1002. if (list_empty(&mod->cmd_q))
  1003. return;
  1004. /**
  1005. * If previous command is not yet fetched by firmware, do nothing
  1006. */
  1007. stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
  1008. if (stat)
  1009. return;
  1010. /**
  1011. * Enqueue command to firmware.
  1012. */
  1013. bfa_q_deq(&mod->cmd_q, &cmd);
  1014. bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
  1015. }
  1016. /**
  1017. * Cleanup any pending requests.
  1018. */
  1019. static void
  1020. bfa_ioc_mbox_hbfail(struct bfa_ioc *ioc)
  1021. {
  1022. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  1023. struct bfa_mbox_cmd *cmd;
  1024. while (!list_empty(&mod->cmd_q))
  1025. bfa_q_deq(&mod->cmd_q, &cmd);
  1026. }
  1027. /**
  1028. * IOC public
  1029. */
  1030. static enum bfa_status
  1031. bfa_ioc_pll_init(struct bfa_ioc *ioc)
  1032. {
  1033. /*
  1034. * Hold semaphore so that nobody can access the chip during init.
  1035. */
  1036. bfa_nw_ioc_sem_get(ioc->ioc_regs.ioc_init_sem_reg);
  1037. bfa_ioc_pll_init_asic(ioc);
  1038. ioc->pllinit = true;
  1039. /*
  1040. * release semaphore.
  1041. */
  1042. bfa_nw_ioc_sem_release(ioc->ioc_regs.ioc_init_sem_reg);
  1043. return BFA_STATUS_OK;
  1044. }
  1045. /**
  1046. * Interface used by diag module to do firmware boot with memory test
  1047. * as the entry vector.
  1048. */
  1049. static void
  1050. bfa_ioc_boot(struct bfa_ioc *ioc, u32 boot_type, u32 boot_param)
  1051. {
  1052. void __iomem *rb;
  1053. bfa_ioc_stats(ioc, ioc_boots);
  1054. if (bfa_ioc_pll_init(ioc) != BFA_STATUS_OK)
  1055. return;
  1056. /**
  1057. * Initialize IOC state of all functions on a chip reset.
  1058. */
  1059. rb = ioc->pcidev.pci_bar_kva;
  1060. if (boot_param == BFI_BOOT_TYPE_MEMTEST) {
  1061. writel(BFI_IOC_MEMTEST, (rb + BFA_IOC0_STATE_REG));
  1062. writel(BFI_IOC_MEMTEST, (rb + BFA_IOC1_STATE_REG));
  1063. } else {
  1064. writel(BFI_IOC_INITING, (rb + BFA_IOC0_STATE_REG));
  1065. writel(BFI_IOC_INITING, (rb + BFA_IOC1_STATE_REG));
  1066. }
  1067. bfa_ioc_msgflush(ioc);
  1068. bfa_ioc_download_fw(ioc, boot_type, boot_param);
  1069. /**
  1070. * Enable interrupts just before starting LPU
  1071. */
  1072. ioc->cbfn->reset_cbfn(ioc->bfa);
  1073. bfa_ioc_lpu_start(ioc);
  1074. }
  1075. /**
  1076. * Enable/disable IOC failure auto recovery.
  1077. */
  1078. void
  1079. bfa_nw_ioc_auto_recover(bool auto_recover)
  1080. {
  1081. bfa_nw_auto_recover = auto_recover;
  1082. }
  1083. static void
  1084. bfa_ioc_msgget(struct bfa_ioc *ioc, void *mbmsg)
  1085. {
  1086. u32 *msgp = mbmsg;
  1087. u32 r32;
  1088. int i;
  1089. /**
  1090. * read the MBOX msg
  1091. */
  1092. for (i = 0; i < (sizeof(union bfi_ioc_i2h_msg_u) / sizeof(u32));
  1093. i++) {
  1094. r32 = readl(ioc->ioc_regs.lpu_mbox +
  1095. i * sizeof(u32));
  1096. msgp[i] = htonl(r32);
  1097. }
  1098. /**
  1099. * turn off mailbox interrupt by clearing mailbox status
  1100. */
  1101. writel(1, ioc->ioc_regs.lpu_mbox_cmd);
  1102. readl(ioc->ioc_regs.lpu_mbox_cmd);
  1103. }
  1104. static void
  1105. bfa_ioc_isr(struct bfa_ioc *ioc, struct bfi_mbmsg *m)
  1106. {
  1107. union bfi_ioc_i2h_msg_u *msg;
  1108. msg = (union bfi_ioc_i2h_msg_u *) m;
  1109. bfa_ioc_stats(ioc, ioc_isrs);
  1110. switch (msg->mh.msg_id) {
  1111. case BFI_IOC_I2H_HBEAT:
  1112. break;
  1113. case BFI_IOC_I2H_READY_EVENT:
  1114. bfa_fsm_send_event(ioc, IOC_E_FWREADY);
  1115. break;
  1116. case BFI_IOC_I2H_ENABLE_REPLY:
  1117. bfa_fsm_send_event(ioc, IOC_E_FWRSP_ENABLE);
  1118. break;
  1119. case BFI_IOC_I2H_DISABLE_REPLY:
  1120. bfa_fsm_send_event(ioc, IOC_E_FWRSP_DISABLE);
  1121. break;
  1122. case BFI_IOC_I2H_GETATTR_REPLY:
  1123. bfa_ioc_getattr_reply(ioc);
  1124. break;
  1125. default:
  1126. BUG_ON(1);
  1127. }
  1128. }
  1129. /**
  1130. * IOC attach time initialization and setup.
  1131. *
  1132. * @param[in] ioc memory for IOC
  1133. * @param[in] bfa driver instance structure
  1134. */
  1135. void
  1136. bfa_nw_ioc_attach(struct bfa_ioc *ioc, void *bfa, struct bfa_ioc_cbfn *cbfn)
  1137. {
  1138. ioc->bfa = bfa;
  1139. ioc->cbfn = cbfn;
  1140. ioc->fcmode = false;
  1141. ioc->pllinit = false;
  1142. ioc->dbg_fwsave_once = true;
  1143. bfa_ioc_mbox_attach(ioc);
  1144. INIT_LIST_HEAD(&ioc->hb_notify_q);
  1145. bfa_fsm_set_state(ioc, bfa_ioc_sm_reset);
  1146. }
  1147. /**
  1148. * Driver detach time IOC cleanup.
  1149. */
  1150. void
  1151. bfa_nw_ioc_detach(struct bfa_ioc *ioc)
  1152. {
  1153. bfa_fsm_send_event(ioc, IOC_E_DETACH);
  1154. }
  1155. /**
  1156. * Setup IOC PCI properties.
  1157. *
  1158. * @param[in] pcidev PCI device information for this IOC
  1159. */
  1160. void
  1161. bfa_nw_ioc_pci_init(struct bfa_ioc *ioc, struct bfa_pcidev *pcidev,
  1162. enum bfi_mclass mc)
  1163. {
  1164. ioc->ioc_mc = mc;
  1165. ioc->pcidev = *pcidev;
  1166. ioc->ctdev = bfa_asic_id_ct(ioc->pcidev.device_id);
  1167. ioc->cna = ioc->ctdev && !ioc->fcmode;
  1168. bfa_nw_ioc_set_ct_hwif(ioc);
  1169. bfa_ioc_map_port(ioc);
  1170. bfa_ioc_reg_init(ioc);
  1171. }
  1172. /**
  1173. * Initialize IOC dma memory
  1174. *
  1175. * @param[in] dm_kva kernel virtual address of IOC dma memory
  1176. * @param[in] dm_pa physical address of IOC dma memory
  1177. */
  1178. void
  1179. bfa_nw_ioc_mem_claim(struct bfa_ioc *ioc, u8 *dm_kva, u64 dm_pa)
  1180. {
  1181. /**
  1182. * dma memory for firmware attribute
  1183. */
  1184. ioc->attr_dma.kva = dm_kva;
  1185. ioc->attr_dma.pa = dm_pa;
  1186. ioc->attr = (struct bfi_ioc_attr *) dm_kva;
  1187. }
  1188. /**
  1189. * Return size of dma memory required.
  1190. */
  1191. u32
  1192. bfa_nw_ioc_meminfo(void)
  1193. {
  1194. return roundup(sizeof(struct bfi_ioc_attr), BFA_DMA_ALIGN_SZ);
  1195. }
  1196. void
  1197. bfa_nw_ioc_enable(struct bfa_ioc *ioc)
  1198. {
  1199. bfa_ioc_stats(ioc, ioc_enables);
  1200. ioc->dbg_fwsave_once = true;
  1201. bfa_fsm_send_event(ioc, IOC_E_ENABLE);
  1202. }
  1203. void
  1204. bfa_nw_ioc_disable(struct bfa_ioc *ioc)
  1205. {
  1206. bfa_ioc_stats(ioc, ioc_disables);
  1207. bfa_fsm_send_event(ioc, IOC_E_DISABLE);
  1208. }
  1209. static u32
  1210. bfa_ioc_smem_pgnum(struct bfa_ioc *ioc, u32 fmaddr)
  1211. {
  1212. return PSS_SMEM_PGNUM(ioc->ioc_regs.smem_pg0, fmaddr);
  1213. }
  1214. static u32
  1215. bfa_ioc_smem_pgoff(struct bfa_ioc *ioc, u32 fmaddr)
  1216. {
  1217. return PSS_SMEM_PGOFF(fmaddr);
  1218. }
  1219. /**
  1220. * Register mailbox message handler function, to be called by common modules
  1221. */
  1222. void
  1223. bfa_nw_ioc_mbox_regisr(struct bfa_ioc *ioc, enum bfi_mclass mc,
  1224. bfa_ioc_mbox_mcfunc_t cbfn, void *cbarg)
  1225. {
  1226. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  1227. mod->mbhdlr[mc].cbfn = cbfn;
  1228. mod->mbhdlr[mc].cbarg = cbarg;
  1229. }
  1230. /**
  1231. * Queue a mailbox command request to firmware. Waits if mailbox is busy.
  1232. * Responsibility of caller to serialize
  1233. *
  1234. * @param[in] ioc IOC instance
  1235. * @param[i] cmd Mailbox command
  1236. */
  1237. void
  1238. bfa_nw_ioc_mbox_queue(struct bfa_ioc *ioc, struct bfa_mbox_cmd *cmd)
  1239. {
  1240. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  1241. u32 stat;
  1242. /**
  1243. * If a previous command is pending, queue new command
  1244. */
  1245. if (!list_empty(&mod->cmd_q)) {
  1246. list_add_tail(&cmd->qe, &mod->cmd_q);
  1247. return;
  1248. }
  1249. /**
  1250. * If mailbox is busy, queue command for poll timer
  1251. */
  1252. stat = readl(ioc->ioc_regs.hfn_mbox_cmd);
  1253. if (stat) {
  1254. list_add_tail(&cmd->qe, &mod->cmd_q);
  1255. return;
  1256. }
  1257. /**
  1258. * mailbox is free -- queue command to firmware
  1259. */
  1260. bfa_ioc_mbox_send(ioc, cmd->msg, sizeof(cmd->msg));
  1261. }
  1262. /**
  1263. * Handle mailbox interrupts
  1264. */
  1265. void
  1266. bfa_nw_ioc_mbox_isr(struct bfa_ioc *ioc)
  1267. {
  1268. struct bfa_ioc_mbox_mod *mod = &ioc->mbox_mod;
  1269. struct bfi_mbmsg m;
  1270. int mc;
  1271. bfa_ioc_msgget(ioc, &m);
  1272. /**
  1273. * Treat IOC message class as special.
  1274. */
  1275. mc = m.mh.msg_class;
  1276. if (mc == BFI_MC_IOC) {
  1277. bfa_ioc_isr(ioc, &m);
  1278. return;
  1279. }
  1280. if ((mc >= BFI_MC_MAX) || (mod->mbhdlr[mc].cbfn == NULL))
  1281. return;
  1282. mod->mbhdlr[mc].cbfn(mod->mbhdlr[mc].cbarg, &m);
  1283. }
  1284. void
  1285. bfa_nw_ioc_error_isr(struct bfa_ioc *ioc)
  1286. {
  1287. bfa_fsm_send_event(ioc, IOC_E_HWERROR);
  1288. }
  1289. /**
  1290. * Add to IOC heartbeat failure notification queue. To be used by common
  1291. * modules such as cee, port, diag.
  1292. */
  1293. void
  1294. bfa_nw_ioc_hbfail_register(struct bfa_ioc *ioc,
  1295. struct bfa_ioc_hbfail_notify *notify)
  1296. {
  1297. list_add_tail(&notify->qe, &ioc->hb_notify_q);
  1298. }
  1299. #define BFA_MFG_NAME "Brocade"
  1300. static void
  1301. bfa_ioc_get_adapter_attr(struct bfa_ioc *ioc,
  1302. struct bfa_adapter_attr *ad_attr)
  1303. {
  1304. struct bfi_ioc_attr *ioc_attr;
  1305. ioc_attr = ioc->attr;
  1306. bfa_ioc_get_adapter_serial_num(ioc, ad_attr->serial_num);
  1307. bfa_ioc_get_adapter_fw_ver(ioc, ad_attr->fw_ver);
  1308. bfa_ioc_get_adapter_optrom_ver(ioc, ad_attr->optrom_ver);
  1309. bfa_ioc_get_adapter_manufacturer(ioc, ad_attr->manufacturer);
  1310. memcpy(&ad_attr->vpd, &ioc_attr->vpd,
  1311. sizeof(struct bfa_mfg_vpd));
  1312. ad_attr->nports = bfa_ioc_get_nports(ioc);
  1313. ad_attr->max_speed = bfa_ioc_speed_sup(ioc);
  1314. bfa_ioc_get_adapter_model(ioc, ad_attr->model);
  1315. /* For now, model descr uses same model string */
  1316. bfa_ioc_get_adapter_model(ioc, ad_attr->model_descr);
  1317. ad_attr->card_type = ioc_attr->card_type;
  1318. ad_attr->is_mezz = bfa_mfg_is_mezz(ioc_attr->card_type);
  1319. if (BFI_ADAPTER_IS_SPECIAL(ioc_attr->adapter_prop))
  1320. ad_attr->prototype = 1;
  1321. else
  1322. ad_attr->prototype = 0;
  1323. ad_attr->pwwn = bfa_ioc_get_pwwn(ioc);
  1324. ad_attr->mac = bfa_nw_ioc_get_mac(ioc);
  1325. ad_attr->pcie_gen = ioc_attr->pcie_gen;
  1326. ad_attr->pcie_lanes = ioc_attr->pcie_lanes;
  1327. ad_attr->pcie_lanes_orig = ioc_attr->pcie_lanes_orig;
  1328. ad_attr->asic_rev = ioc_attr->asic_rev;
  1329. bfa_ioc_get_pci_chip_rev(ioc, ad_attr->hw_ver);
  1330. ad_attr->cna_capable = ioc->cna;
  1331. ad_attr->trunk_capable = (ad_attr->nports > 1) && !ioc->cna;
  1332. }
  1333. static enum bfa_ioc_type
  1334. bfa_ioc_get_type(struct bfa_ioc *ioc)
  1335. {
  1336. if (!ioc->ctdev || ioc->fcmode)
  1337. return BFA_IOC_TYPE_FC;
  1338. else if (ioc->ioc_mc == BFI_MC_IOCFC)
  1339. return BFA_IOC_TYPE_FCoE;
  1340. else if (ioc->ioc_mc == BFI_MC_LL)
  1341. return BFA_IOC_TYPE_LL;
  1342. else {
  1343. BUG_ON(!(ioc->ioc_mc == BFI_MC_LL));
  1344. return BFA_IOC_TYPE_LL;
  1345. }
  1346. }
  1347. static void
  1348. bfa_ioc_get_adapter_serial_num(struct bfa_ioc *ioc, char *serial_num)
  1349. {
  1350. memset(serial_num, 0, BFA_ADAPTER_SERIAL_NUM_LEN);
  1351. memcpy(serial_num,
  1352. (void *)ioc->attr->brcd_serialnum,
  1353. BFA_ADAPTER_SERIAL_NUM_LEN);
  1354. }
  1355. static void
  1356. bfa_ioc_get_adapter_fw_ver(struct bfa_ioc *ioc, char *fw_ver)
  1357. {
  1358. memset(fw_ver, 0, BFA_VERSION_LEN);
  1359. memcpy(fw_ver, ioc->attr->fw_version, BFA_VERSION_LEN);
  1360. }
  1361. static void
  1362. bfa_ioc_get_pci_chip_rev(struct bfa_ioc *ioc, char *chip_rev)
  1363. {
  1364. BUG_ON(!(chip_rev));
  1365. memset(chip_rev, 0, BFA_IOC_CHIP_REV_LEN);
  1366. chip_rev[0] = 'R';
  1367. chip_rev[1] = 'e';
  1368. chip_rev[2] = 'v';
  1369. chip_rev[3] = '-';
  1370. chip_rev[4] = ioc->attr->asic_rev;
  1371. chip_rev[5] = '\0';
  1372. }
  1373. static void
  1374. bfa_ioc_get_adapter_optrom_ver(struct bfa_ioc *ioc, char *optrom_ver)
  1375. {
  1376. memset(optrom_ver, 0, BFA_VERSION_LEN);
  1377. memcpy(optrom_ver, ioc->attr->optrom_version,
  1378. BFA_VERSION_LEN);
  1379. }
  1380. static void
  1381. bfa_ioc_get_adapter_manufacturer(struct bfa_ioc *ioc, char *manufacturer)
  1382. {
  1383. memset(manufacturer, 0, BFA_ADAPTER_MFG_NAME_LEN);
  1384. memcpy(manufacturer, BFA_MFG_NAME, BFA_ADAPTER_MFG_NAME_LEN);
  1385. }
  1386. static void
  1387. bfa_ioc_get_adapter_model(struct bfa_ioc *ioc, char *model)
  1388. {
  1389. struct bfi_ioc_attr *ioc_attr;
  1390. BUG_ON(!(model));
  1391. memset(model, 0, BFA_ADAPTER_MODEL_NAME_LEN);
  1392. ioc_attr = ioc->attr;
  1393. /**
  1394. * model name
  1395. */
  1396. snprintf(model, BFA_ADAPTER_MODEL_NAME_LEN, "%s-%u",
  1397. BFA_MFG_NAME, ioc_attr->card_type);
  1398. }
  1399. static enum bfa_ioc_state
  1400. bfa_ioc_get_state(struct bfa_ioc *ioc)
  1401. {
  1402. return bfa_sm_to_state(ioc_sm_table, ioc->fsm);
  1403. }
  1404. void
  1405. bfa_nw_ioc_get_attr(struct bfa_ioc *ioc, struct bfa_ioc_attr *ioc_attr)
  1406. {
  1407. memset((void *)ioc_attr, 0, sizeof(struct bfa_ioc_attr));
  1408. ioc_attr->state = bfa_ioc_get_state(ioc);
  1409. ioc_attr->port_id = ioc->port_id;
  1410. ioc_attr->ioc_type = bfa_ioc_get_type(ioc);
  1411. bfa_ioc_get_adapter_attr(ioc, &ioc_attr->adapter_attr);
  1412. ioc_attr->pci_attr.device_id = ioc->pcidev.device_id;
  1413. ioc_attr->pci_attr.pcifn = ioc->pcidev.pci_func;
  1414. bfa_ioc_get_pci_chip_rev(ioc, ioc_attr->pci_attr.chip_rev);
  1415. }
  1416. /**
  1417. * WWN public
  1418. */
  1419. static u64
  1420. bfa_ioc_get_pwwn(struct bfa_ioc *ioc)
  1421. {
  1422. return ioc->attr->pwwn;
  1423. }
  1424. mac_t
  1425. bfa_nw_ioc_get_mac(struct bfa_ioc *ioc)
  1426. {
  1427. /*
  1428. * Currently mfg mac is used as FCoE enode mac (not configured by PBC)
  1429. */
  1430. if (bfa_ioc_get_type(ioc) == BFA_IOC_TYPE_FCoE)
  1431. return bfa_ioc_get_mfg_mac(ioc);
  1432. else
  1433. return ioc->attr->mac;
  1434. }
  1435. static mac_t
  1436. bfa_ioc_get_mfg_mac(struct bfa_ioc *ioc)
  1437. {
  1438. mac_t m;
  1439. m = ioc->attr->mfg_mac;
  1440. if (bfa_mfg_is_old_wwn_mac_model(ioc->attr->card_type))
  1441. m.mac[MAC_ADDRLEN - 1] += bfa_ioc_pcifn(ioc);
  1442. else
  1443. bfa_mfg_increment_wwn_mac(&(m.mac[MAC_ADDRLEN-3]),
  1444. bfa_ioc_pcifn(ioc));
  1445. return m;
  1446. }
  1447. /**
  1448. * Firmware failure detected. Start recovery actions.
  1449. */
  1450. static void
  1451. bfa_ioc_recover(struct bfa_ioc *ioc)
  1452. {
  1453. bfa_ioc_stats(ioc, ioc_hbfails);
  1454. bfa_fsm_send_event(ioc, IOC_E_HBFAIL);
  1455. }
  1456. static void
  1457. bfa_ioc_check_attr_wwns(struct bfa_ioc *ioc)
  1458. {
  1459. if (bfa_ioc_get_type(ioc) == BFA_IOC_TYPE_LL)
  1460. return;
  1461. }