firmware.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /*
  2. * Copyright(c) 2015 - 2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/firmware.h>
  48. #include <linux/mutex.h>
  49. #include <linux/module.h>
  50. #include <linux/delay.h>
  51. #include <linux/crc32.h>
  52. #include "hfi.h"
  53. #include "trace.h"
  54. /*
  55. * Make it easy to toggle firmware file name and if it gets loaded by
  56. * editing the following. This may be something we do while in development
  57. * but not necessarily something a user would ever need to use.
  58. */
  59. #define DEFAULT_FW_8051_NAME_FPGA "hfi_dc8051.bin"
  60. #define DEFAULT_FW_8051_NAME_ASIC "hfi1_dc8051.fw"
  61. #define DEFAULT_FW_FABRIC_NAME "hfi1_fabric.fw"
  62. #define DEFAULT_FW_SBUS_NAME "hfi1_sbus.fw"
  63. #define DEFAULT_FW_PCIE_NAME "hfi1_pcie.fw"
  64. #define DEFAULT_PLATFORM_CONFIG_NAME "hfi1_platform.dat"
  65. #define ALT_FW_8051_NAME_ASIC "hfi1_dc8051_d.fw"
  66. #define ALT_FW_FABRIC_NAME "hfi1_fabric_d.fw"
  67. #define ALT_FW_SBUS_NAME "hfi1_sbus_d.fw"
  68. #define ALT_FW_PCIE_NAME "hfi1_pcie_d.fw"
  69. static uint fw_8051_load = 1;
  70. static uint fw_fabric_serdes_load = 1;
  71. static uint fw_pcie_serdes_load = 1;
  72. static uint fw_sbus_load = 1;
  73. /*
  74. * Access required in platform.c
  75. * Maintains state of whether the platform config was fetched via the
  76. * fallback option
  77. */
  78. uint platform_config_load;
  79. /* Firmware file names get set in hfi1_firmware_init() based on the above */
  80. static char *fw_8051_name;
  81. static char *fw_fabric_serdes_name;
  82. static char *fw_sbus_name;
  83. static char *fw_pcie_serdes_name;
  84. static char *platform_config_name;
  85. #define SBUS_MAX_POLL_COUNT 100
  86. #define SBUS_COUNTER(reg, name) \
  87. (((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
  88. ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
  89. /*
  90. * Firmware security header.
  91. */
  92. struct css_header {
  93. u32 module_type;
  94. u32 header_len;
  95. u32 header_version;
  96. u32 module_id;
  97. u32 module_vendor;
  98. u32 date; /* BCD yyyymmdd */
  99. u32 size; /* in DWORDs */
  100. u32 key_size; /* in DWORDs */
  101. u32 modulus_size; /* in DWORDs */
  102. u32 exponent_size; /* in DWORDs */
  103. u32 reserved[22];
  104. };
  105. /* expected field values */
  106. #define CSS_MODULE_TYPE 0x00000006
  107. #define CSS_HEADER_LEN 0x000000a1
  108. #define CSS_HEADER_VERSION 0x00010000
  109. #define CSS_MODULE_VENDOR 0x00008086
  110. #define KEY_SIZE 256
  111. #define MU_SIZE 8
  112. #define EXPONENT_SIZE 4
  113. /* the file itself */
  114. struct firmware_file {
  115. struct css_header css_header;
  116. u8 modulus[KEY_SIZE];
  117. u8 exponent[EXPONENT_SIZE];
  118. u8 signature[KEY_SIZE];
  119. u8 firmware[];
  120. };
  121. struct augmented_firmware_file {
  122. struct css_header css_header;
  123. u8 modulus[KEY_SIZE];
  124. u8 exponent[EXPONENT_SIZE];
  125. u8 signature[KEY_SIZE];
  126. u8 r2[KEY_SIZE];
  127. u8 mu[MU_SIZE];
  128. u8 firmware[];
  129. };
  130. /* augmented file size difference */
  131. #define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
  132. sizeof(struct firmware_file))
  133. struct firmware_details {
  134. /* Linux core piece */
  135. const struct firmware *fw;
  136. struct css_header *css_header;
  137. u8 *firmware_ptr; /* pointer to binary data */
  138. u32 firmware_len; /* length in bytes */
  139. u8 *modulus; /* pointer to the modulus */
  140. u8 *exponent; /* pointer to the exponent */
  141. u8 *signature; /* pointer to the signature */
  142. u8 *r2; /* pointer to r2 */
  143. u8 *mu; /* pointer to mu */
  144. struct augmented_firmware_file dummy_header;
  145. };
  146. /*
  147. * The mutex protects fw_state, fw_err, and all of the firmware_details
  148. * variables.
  149. */
  150. static DEFINE_MUTEX(fw_mutex);
  151. enum fw_state {
  152. FW_EMPTY,
  153. FW_TRY,
  154. FW_FINAL,
  155. FW_ERR
  156. };
  157. static enum fw_state fw_state = FW_EMPTY;
  158. static int fw_err;
  159. static struct firmware_details fw_8051;
  160. static struct firmware_details fw_fabric;
  161. static struct firmware_details fw_pcie;
  162. static struct firmware_details fw_sbus;
  163. static const struct firmware *platform_config;
  164. /* flags for turn_off_spicos() */
  165. #define SPICO_SBUS 0x1
  166. #define SPICO_FABRIC 0x2
  167. #define ENABLE_SPICO_SMASK 0x1
  168. /* security block commands */
  169. #define RSA_CMD_INIT 0x1
  170. #define RSA_CMD_START 0x2
  171. /* security block status */
  172. #define RSA_STATUS_IDLE 0x0
  173. #define RSA_STATUS_ACTIVE 0x1
  174. #define RSA_STATUS_DONE 0x2
  175. #define RSA_STATUS_FAILED 0x3
  176. /* RSA engine timeout, in ms */
  177. #define RSA_ENGINE_TIMEOUT 100 /* ms */
  178. /* hardware mutex timeout, in ms */
  179. #define HM_TIMEOUT 10 /* ms */
  180. /* 8051 memory access timeout, in us */
  181. #define DC8051_ACCESS_TIMEOUT 100 /* us */
  182. /* the number of fabric SerDes on the SBus */
  183. #define NUM_FABRIC_SERDES 4
  184. /* ASIC_STS_SBUS_RESULT.RESULT_CODE value */
  185. #define SBUS_READ_COMPLETE 0x4
  186. /* SBus fabric SerDes addresses, one set per HFI */
  187. static const u8 fabric_serdes_addrs[2][NUM_FABRIC_SERDES] = {
  188. { 0x01, 0x02, 0x03, 0x04 },
  189. { 0x28, 0x29, 0x2a, 0x2b }
  190. };
  191. /* SBus PCIe SerDes addresses, one set per HFI */
  192. static const u8 pcie_serdes_addrs[2][NUM_PCIE_SERDES] = {
  193. { 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
  194. 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
  195. { 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
  196. 0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
  197. };
  198. /* SBus PCIe PCS addresses, one set per HFI */
  199. const u8 pcie_pcs_addrs[2][NUM_PCIE_SERDES] = {
  200. { 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
  201. 0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
  202. { 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
  203. 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
  204. };
  205. /* SBus fabric SerDes broadcast addresses, one per HFI */
  206. static const u8 fabric_serdes_broadcast[2] = { 0xe4, 0xe5 };
  207. static const u8 all_fabric_serdes_broadcast = 0xe1;
  208. /* SBus PCIe SerDes broadcast addresses, one per HFI */
  209. const u8 pcie_serdes_broadcast[2] = { 0xe2, 0xe3 };
  210. static const u8 all_pcie_serdes_broadcast = 0xe0;
  211. static const u32 platform_config_table_limits[PLATFORM_CONFIG_TABLE_MAX] = {
  212. 0,
  213. SYSTEM_TABLE_MAX,
  214. PORT_TABLE_MAX,
  215. RX_PRESET_TABLE_MAX,
  216. TX_PRESET_TABLE_MAX,
  217. QSFP_ATTEN_TABLE_MAX,
  218. VARIABLE_SETTINGS_TABLE_MAX
  219. };
  220. /* forwards */
  221. static void dispose_one_firmware(struct firmware_details *fdet);
  222. static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
  223. struct firmware_details *fdet);
  224. static void dump_fw_version(struct hfi1_devdata *dd);
  225. /*
  226. * Read a single 64-bit value from 8051 data memory.
  227. *
  228. * Expects:
  229. * o caller to have already set up data read, no auto increment
  230. * o caller to turn off read enable when finished
  231. *
  232. * The address argument is a byte offset. Bits 0:2 in the address are
  233. * ignored - i.e. the hardware will always do aligned 8-byte reads as if
  234. * the lower bits are zero.
  235. *
  236. * Return 0 on success, -ENXIO on a read error (timeout).
  237. */
  238. static int __read_8051_data(struct hfi1_devdata *dd, u32 addr, u64 *result)
  239. {
  240. u64 reg;
  241. int count;
  242. /* step 1: set the address, clear enable */
  243. reg = (addr & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
  244. << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT;
  245. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
  246. /* step 2: enable */
  247. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL,
  248. reg | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK);
  249. /* wait until ACCESS_COMPLETED is set */
  250. count = 0;
  251. while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
  252. & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
  253. == 0) {
  254. count++;
  255. if (count > DC8051_ACCESS_TIMEOUT) {
  256. dd_dev_err(dd, "timeout reading 8051 data\n");
  257. return -ENXIO;
  258. }
  259. ndelay(10);
  260. }
  261. /* gather the data */
  262. *result = read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_RD_DATA);
  263. return 0;
  264. }
  265. /*
  266. * Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
  267. * Return 0 on success, -errno on error.
  268. */
  269. int read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result)
  270. {
  271. unsigned long flags;
  272. u32 done;
  273. int ret = 0;
  274. spin_lock_irqsave(&dd->dc8051_memlock, flags);
  275. /* data read set-up, no auto-increment */
  276. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
  277. for (done = 0; done < len; addr += 8, done += 8, result++) {
  278. ret = __read_8051_data(dd, addr, result);
  279. if (ret)
  280. break;
  281. }
  282. /* turn off read enable */
  283. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
  284. spin_unlock_irqrestore(&dd->dc8051_memlock, flags);
  285. return ret;
  286. }
  287. /*
  288. * Write data or code to the 8051 code or data RAM.
  289. */
  290. static int write_8051(struct hfi1_devdata *dd, int code, u32 start,
  291. const u8 *data, u32 len)
  292. {
  293. u64 reg;
  294. u32 offset;
  295. int aligned, count;
  296. /* check alignment */
  297. aligned = ((unsigned long)data & 0x7) == 0;
  298. /* write set-up */
  299. reg = (code ? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK : 0ull)
  300. | DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK;
  301. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, reg);
  302. reg = ((start & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
  303. << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
  304. | DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK;
  305. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
  306. /* write */
  307. for (offset = 0; offset < len; offset += 8) {
  308. int bytes = len - offset;
  309. if (bytes < 8) {
  310. reg = 0;
  311. memcpy(&reg, &data[offset], bytes);
  312. } else if (aligned) {
  313. reg = *(u64 *)&data[offset];
  314. } else {
  315. memcpy(&reg, &data[offset], 8);
  316. }
  317. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_WR_DATA, reg);
  318. /* wait until ACCESS_COMPLETED is set */
  319. count = 0;
  320. while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
  321. & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
  322. == 0) {
  323. count++;
  324. if (count > DC8051_ACCESS_TIMEOUT) {
  325. dd_dev_err(dd, "timeout writing 8051 data\n");
  326. return -ENXIO;
  327. }
  328. udelay(1);
  329. }
  330. }
  331. /* turn off write access, auto increment (also sets to data access) */
  332. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
  333. write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
  334. return 0;
  335. }
  336. /* return 0 if values match, non-zero and complain otherwise */
  337. static int invalid_header(struct hfi1_devdata *dd, const char *what,
  338. u32 actual, u32 expected)
  339. {
  340. if (actual == expected)
  341. return 0;
  342. dd_dev_err(dd,
  343. "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
  344. what, expected, actual);
  345. return 1;
  346. }
  347. /*
  348. * Verify that the static fields in the CSS header match.
  349. */
  350. static int verify_css_header(struct hfi1_devdata *dd, struct css_header *css)
  351. {
  352. /* verify CSS header fields (most sizes are in DW, so add /4) */
  353. if (invalid_header(dd, "module_type", css->module_type,
  354. CSS_MODULE_TYPE) ||
  355. invalid_header(dd, "header_len", css->header_len,
  356. (sizeof(struct firmware_file) / 4)) ||
  357. invalid_header(dd, "header_version", css->header_version,
  358. CSS_HEADER_VERSION) ||
  359. invalid_header(dd, "module_vendor", css->module_vendor,
  360. CSS_MODULE_VENDOR) ||
  361. invalid_header(dd, "key_size", css->key_size, KEY_SIZE / 4) ||
  362. invalid_header(dd, "modulus_size", css->modulus_size,
  363. KEY_SIZE / 4) ||
  364. invalid_header(dd, "exponent_size", css->exponent_size,
  365. EXPONENT_SIZE / 4)) {
  366. return -EINVAL;
  367. }
  368. return 0;
  369. }
  370. /*
  371. * Make sure there are at least some bytes after the prefix.
  372. */
  373. static int payload_check(struct hfi1_devdata *dd, const char *name,
  374. long file_size, long prefix_size)
  375. {
  376. /* make sure we have some payload */
  377. if (prefix_size >= file_size) {
  378. dd_dev_err(dd,
  379. "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
  380. name, file_size, prefix_size);
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. /*
  386. * Request the firmware from the system. Extract the pieces and fill in
  387. * fdet. If successful, the caller will need to call dispose_one_firmware().
  388. * Returns 0 on success, -ERRNO on error.
  389. */
  390. static int obtain_one_firmware(struct hfi1_devdata *dd, const char *name,
  391. struct firmware_details *fdet)
  392. {
  393. struct css_header *css;
  394. int ret;
  395. memset(fdet, 0, sizeof(*fdet));
  396. ret = request_firmware(&fdet->fw, name, &dd->pcidev->dev);
  397. if (ret) {
  398. dd_dev_warn(dd, "cannot find firmware \"%s\", err %d\n",
  399. name, ret);
  400. return ret;
  401. }
  402. /* verify the firmware */
  403. if (fdet->fw->size < sizeof(struct css_header)) {
  404. dd_dev_err(dd, "firmware \"%s\" is too small\n", name);
  405. ret = -EINVAL;
  406. goto done;
  407. }
  408. css = (struct css_header *)fdet->fw->data;
  409. hfi1_cdbg(FIRMWARE, "Firmware %s details:", name);
  410. hfi1_cdbg(FIRMWARE, "file size: 0x%lx bytes", fdet->fw->size);
  411. hfi1_cdbg(FIRMWARE, "CSS structure:");
  412. hfi1_cdbg(FIRMWARE, " module_type 0x%x", css->module_type);
  413. hfi1_cdbg(FIRMWARE, " header_len 0x%03x (0x%03x bytes)",
  414. css->header_len, 4 * css->header_len);
  415. hfi1_cdbg(FIRMWARE, " header_version 0x%x", css->header_version);
  416. hfi1_cdbg(FIRMWARE, " module_id 0x%x", css->module_id);
  417. hfi1_cdbg(FIRMWARE, " module_vendor 0x%x", css->module_vendor);
  418. hfi1_cdbg(FIRMWARE, " date 0x%x", css->date);
  419. hfi1_cdbg(FIRMWARE, " size 0x%03x (0x%03x bytes)",
  420. css->size, 4 * css->size);
  421. hfi1_cdbg(FIRMWARE, " key_size 0x%03x (0x%03x bytes)",
  422. css->key_size, 4 * css->key_size);
  423. hfi1_cdbg(FIRMWARE, " modulus_size 0x%03x (0x%03x bytes)",
  424. css->modulus_size, 4 * css->modulus_size);
  425. hfi1_cdbg(FIRMWARE, " exponent_size 0x%03x (0x%03x bytes)",
  426. css->exponent_size, 4 * css->exponent_size);
  427. hfi1_cdbg(FIRMWARE, "firmware size: 0x%lx bytes",
  428. fdet->fw->size - sizeof(struct firmware_file));
  429. /*
  430. * If the file does not have a valid CSS header, fail.
  431. * Otherwise, check the CSS size field for an expected size.
  432. * The augmented file has r2 and mu inserted after the header
  433. * was generated, so there will be a known difference between
  434. * the CSS header size and the actual file size. Use this
  435. * difference to identify an augmented file.
  436. *
  437. * Note: css->size is in DWORDs, multiply by 4 to get bytes.
  438. */
  439. ret = verify_css_header(dd, css);
  440. if (ret) {
  441. dd_dev_info(dd, "Invalid CSS header for \"%s\"\n", name);
  442. } else if ((css->size * 4) == fdet->fw->size) {
  443. /* non-augmented firmware file */
  444. struct firmware_file *ff = (struct firmware_file *)
  445. fdet->fw->data;
  446. /* make sure there are bytes in the payload */
  447. ret = payload_check(dd, name, fdet->fw->size,
  448. sizeof(struct firmware_file));
  449. if (ret == 0) {
  450. fdet->css_header = css;
  451. fdet->modulus = ff->modulus;
  452. fdet->exponent = ff->exponent;
  453. fdet->signature = ff->signature;
  454. fdet->r2 = fdet->dummy_header.r2; /* use dummy space */
  455. fdet->mu = fdet->dummy_header.mu; /* use dummy space */
  456. fdet->firmware_ptr = ff->firmware;
  457. fdet->firmware_len = fdet->fw->size -
  458. sizeof(struct firmware_file);
  459. /*
  460. * Header does not include r2 and mu - generate here.
  461. * For now, fail.
  462. */
  463. dd_dev_err(dd, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
  464. ret = -EINVAL;
  465. }
  466. } else if ((css->size * 4) + AUGMENT_SIZE == fdet->fw->size) {
  467. /* augmented firmware file */
  468. struct augmented_firmware_file *aff =
  469. (struct augmented_firmware_file *)fdet->fw->data;
  470. /* make sure there are bytes in the payload */
  471. ret = payload_check(dd, name, fdet->fw->size,
  472. sizeof(struct augmented_firmware_file));
  473. if (ret == 0) {
  474. fdet->css_header = css;
  475. fdet->modulus = aff->modulus;
  476. fdet->exponent = aff->exponent;
  477. fdet->signature = aff->signature;
  478. fdet->r2 = aff->r2;
  479. fdet->mu = aff->mu;
  480. fdet->firmware_ptr = aff->firmware;
  481. fdet->firmware_len = fdet->fw->size -
  482. sizeof(struct augmented_firmware_file);
  483. }
  484. } else {
  485. /* css->size check failed */
  486. dd_dev_err(dd,
  487. "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
  488. fdet->fw->size / 4,
  489. (fdet->fw->size - AUGMENT_SIZE) / 4,
  490. css->size);
  491. ret = -EINVAL;
  492. }
  493. done:
  494. /* if returning an error, clean up after ourselves */
  495. if (ret)
  496. dispose_one_firmware(fdet);
  497. return ret;
  498. }
  499. static void dispose_one_firmware(struct firmware_details *fdet)
  500. {
  501. release_firmware(fdet->fw);
  502. /* erase all previous information */
  503. memset(fdet, 0, sizeof(*fdet));
  504. }
  505. /*
  506. * Obtain the 4 firmwares from the OS. All must be obtained at once or not
  507. * at all. If called with the firmware state in FW_TRY, use alternate names.
  508. * On exit, this routine will have set the firmware state to one of FW_TRY,
  509. * FW_FINAL, or FW_ERR.
  510. *
  511. * Must be holding fw_mutex.
  512. */
  513. static void __obtain_firmware(struct hfi1_devdata *dd)
  514. {
  515. int err = 0;
  516. if (fw_state == FW_FINAL) /* nothing more to obtain */
  517. return;
  518. if (fw_state == FW_ERR) /* already in error */
  519. return;
  520. /* fw_state is FW_EMPTY or FW_TRY */
  521. retry:
  522. if (fw_state == FW_TRY) {
  523. /*
  524. * We tried the original and it failed. Move to the
  525. * alternate.
  526. */
  527. dd_dev_warn(dd, "using alternate firmware names\n");
  528. /*
  529. * Let others run. Some systems, when missing firmware, does
  530. * something that holds for 30 seconds. If we do that twice
  531. * in a row it triggers task blocked warning.
  532. */
  533. cond_resched();
  534. if (fw_8051_load)
  535. dispose_one_firmware(&fw_8051);
  536. if (fw_fabric_serdes_load)
  537. dispose_one_firmware(&fw_fabric);
  538. if (fw_sbus_load)
  539. dispose_one_firmware(&fw_sbus);
  540. if (fw_pcie_serdes_load)
  541. dispose_one_firmware(&fw_pcie);
  542. fw_8051_name = ALT_FW_8051_NAME_ASIC;
  543. fw_fabric_serdes_name = ALT_FW_FABRIC_NAME;
  544. fw_sbus_name = ALT_FW_SBUS_NAME;
  545. fw_pcie_serdes_name = ALT_FW_PCIE_NAME;
  546. }
  547. if (fw_sbus_load) {
  548. err = obtain_one_firmware(dd, fw_sbus_name, &fw_sbus);
  549. if (err)
  550. goto done;
  551. }
  552. if (fw_pcie_serdes_load) {
  553. err = obtain_one_firmware(dd, fw_pcie_serdes_name, &fw_pcie);
  554. if (err)
  555. goto done;
  556. }
  557. if (fw_fabric_serdes_load) {
  558. err = obtain_one_firmware(dd, fw_fabric_serdes_name,
  559. &fw_fabric);
  560. if (err)
  561. goto done;
  562. }
  563. if (fw_8051_load) {
  564. err = obtain_one_firmware(dd, fw_8051_name, &fw_8051);
  565. if (err)
  566. goto done;
  567. }
  568. done:
  569. if (err) {
  570. /* oops, had problems obtaining a firmware */
  571. if (fw_state == FW_EMPTY && dd->icode == ICODE_RTL_SILICON) {
  572. /* retry with alternate (RTL only) */
  573. fw_state = FW_TRY;
  574. goto retry;
  575. }
  576. dd_dev_err(dd, "unable to obtain working firmware\n");
  577. fw_state = FW_ERR;
  578. fw_err = -ENOENT;
  579. } else {
  580. /* success */
  581. if (fw_state == FW_EMPTY &&
  582. dd->icode != ICODE_FUNCTIONAL_SIMULATOR)
  583. fw_state = FW_TRY; /* may retry later */
  584. else
  585. fw_state = FW_FINAL; /* cannot try again */
  586. }
  587. }
  588. /*
  589. * Called by all HFIs when loading their firmware - i.e. device probe time.
  590. * The first one will do the actual firmware load. Use a mutex to resolve
  591. * any possible race condition.
  592. *
  593. * The call to this routine cannot be moved to driver load because the kernel
  594. * call request_firmware() requires a device which is only available after
  595. * the first device probe.
  596. */
  597. static int obtain_firmware(struct hfi1_devdata *dd)
  598. {
  599. unsigned long timeout;
  600. int err = 0;
  601. mutex_lock(&fw_mutex);
  602. /* 40s delay due to long delay on missing firmware on some systems */
  603. timeout = jiffies + msecs_to_jiffies(40000);
  604. while (fw_state == FW_TRY) {
  605. /*
  606. * Another device is trying the firmware. Wait until it
  607. * decides what works (or not).
  608. */
  609. if (time_after(jiffies, timeout)) {
  610. /* waited too long */
  611. dd_dev_err(dd, "Timeout waiting for firmware try");
  612. fw_state = FW_ERR;
  613. fw_err = -ETIMEDOUT;
  614. break;
  615. }
  616. mutex_unlock(&fw_mutex);
  617. msleep(20); /* arbitrary delay */
  618. mutex_lock(&fw_mutex);
  619. }
  620. /* not in FW_TRY state */
  621. if (fw_state == FW_FINAL) {
  622. if (platform_config) {
  623. dd->platform_config.data = platform_config->data;
  624. dd->platform_config.size = platform_config->size;
  625. }
  626. goto done; /* already acquired */
  627. } else if (fw_state == FW_ERR) {
  628. goto done; /* already tried and failed */
  629. }
  630. /* fw_state is FW_EMPTY */
  631. /* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
  632. __obtain_firmware(dd);
  633. if (platform_config_load) {
  634. platform_config = NULL;
  635. err = request_firmware(&platform_config, platform_config_name,
  636. &dd->pcidev->dev);
  637. if (err) {
  638. platform_config = NULL;
  639. dd_dev_err(dd,
  640. "%s: No default platform config file found\n",
  641. __func__);
  642. goto done;
  643. }
  644. dd->platform_config.data = platform_config->data;
  645. dd->platform_config.size = platform_config->size;
  646. }
  647. done:
  648. mutex_unlock(&fw_mutex);
  649. return fw_err;
  650. }
  651. /*
  652. * Called when the driver unloads. The timing is asymmetric with its
  653. * counterpart, obtain_firmware(). If called at device remove time,
  654. * then it is conceivable that another device could probe while the
  655. * firmware is being disposed. The mutexes can be moved to do that
  656. * safely, but then the firmware would be requested from the OS multiple
  657. * times.
  658. *
  659. * No mutex is needed as the driver is unloading and there cannot be any
  660. * other callers.
  661. */
  662. void dispose_firmware(void)
  663. {
  664. dispose_one_firmware(&fw_8051);
  665. dispose_one_firmware(&fw_fabric);
  666. dispose_one_firmware(&fw_pcie);
  667. dispose_one_firmware(&fw_sbus);
  668. release_firmware(platform_config);
  669. platform_config = NULL;
  670. /* retain the error state, otherwise revert to empty */
  671. if (fw_state != FW_ERR)
  672. fw_state = FW_EMPTY;
  673. }
  674. /*
  675. * Called with the result of a firmware download.
  676. *
  677. * Return 1 to retry loading the firmware, 0 to stop.
  678. */
  679. static int retry_firmware(struct hfi1_devdata *dd, int load_result)
  680. {
  681. int retry;
  682. mutex_lock(&fw_mutex);
  683. if (load_result == 0) {
  684. /*
  685. * The load succeeded, so expect all others to do the same.
  686. * Do not retry again.
  687. */
  688. if (fw_state == FW_TRY)
  689. fw_state = FW_FINAL;
  690. retry = 0; /* do NOT retry */
  691. } else if (fw_state == FW_TRY) {
  692. /* load failed, obtain alternate firmware */
  693. __obtain_firmware(dd);
  694. retry = (fw_state == FW_FINAL);
  695. } else {
  696. /* else in FW_FINAL or FW_ERR, no retry in either case */
  697. retry = 0;
  698. }
  699. mutex_unlock(&fw_mutex);
  700. return retry;
  701. }
  702. /*
  703. * Write a block of data to a given array CSR. All calls will be in
  704. * multiples of 8 bytes.
  705. */
  706. static void write_rsa_data(struct hfi1_devdata *dd, int what,
  707. const u8 *data, int nbytes)
  708. {
  709. int qw_size = nbytes / 8;
  710. int i;
  711. if (((unsigned long)data & 0x7) == 0) {
  712. /* aligned */
  713. u64 *ptr = (u64 *)data;
  714. for (i = 0; i < qw_size; i++, ptr++)
  715. write_csr(dd, what + (8 * i), *ptr);
  716. } else {
  717. /* not aligned */
  718. for (i = 0; i < qw_size; i++, data += 8) {
  719. u64 value;
  720. memcpy(&value, data, 8);
  721. write_csr(dd, what + (8 * i), value);
  722. }
  723. }
  724. }
  725. /*
  726. * Write a block of data to a given CSR as a stream of writes. All calls will
  727. * be in multiples of 8 bytes.
  728. */
  729. static void write_streamed_rsa_data(struct hfi1_devdata *dd, int what,
  730. const u8 *data, int nbytes)
  731. {
  732. u64 *ptr = (u64 *)data;
  733. int qw_size = nbytes / 8;
  734. for (; qw_size > 0; qw_size--, ptr++)
  735. write_csr(dd, what, *ptr);
  736. }
  737. /*
  738. * Download the signature and start the RSA mechanism. Wait for
  739. * RSA_ENGINE_TIMEOUT before giving up.
  740. */
  741. static int run_rsa(struct hfi1_devdata *dd, const char *who,
  742. const u8 *signature)
  743. {
  744. unsigned long timeout;
  745. u64 reg;
  746. u32 status;
  747. int ret = 0;
  748. /* write the signature */
  749. write_rsa_data(dd, MISC_CFG_RSA_SIGNATURE, signature, KEY_SIZE);
  750. /* initialize RSA */
  751. write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_INIT);
  752. /*
  753. * Make sure the engine is idle and insert a delay between the two
  754. * writes to MISC_CFG_RSA_CMD.
  755. */
  756. status = (read_csr(dd, MISC_CFG_FW_CTRL)
  757. & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
  758. >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
  759. if (status != RSA_STATUS_IDLE) {
  760. dd_dev_err(dd, "%s security engine not idle - giving up\n",
  761. who);
  762. return -EBUSY;
  763. }
  764. /* start RSA */
  765. write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_START);
  766. /*
  767. * Look for the result.
  768. *
  769. * The RSA engine is hooked up to two MISC errors. The driver
  770. * masks these errors as they do not respond to the standard
  771. * error "clear down" mechanism. Look for these errors here and
  772. * clear them when possible. This routine will exit with the
  773. * errors of the current run still set.
  774. *
  775. * MISC_FW_AUTH_FAILED_ERR
  776. * Firmware authorization failed. This can be cleared by
  777. * re-initializing the RSA engine, then clearing the status bit.
  778. * Do not re-init the RSA angine immediately after a successful
  779. * run - this will reset the current authorization.
  780. *
  781. * MISC_KEY_MISMATCH_ERR
  782. * Key does not match. The only way to clear this is to load
  783. * a matching key then clear the status bit. If this error
  784. * is raised, it will persist outside of this routine until a
  785. * matching key is loaded.
  786. */
  787. timeout = msecs_to_jiffies(RSA_ENGINE_TIMEOUT) + jiffies;
  788. while (1) {
  789. status = (read_csr(dd, MISC_CFG_FW_CTRL)
  790. & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
  791. >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
  792. if (status == RSA_STATUS_IDLE) {
  793. /* should not happen */
  794. dd_dev_err(dd, "%s firmware security bad idle state\n",
  795. who);
  796. ret = -EINVAL;
  797. break;
  798. } else if (status == RSA_STATUS_DONE) {
  799. /* finished successfully */
  800. break;
  801. } else if (status == RSA_STATUS_FAILED) {
  802. /* finished unsuccessfully */
  803. ret = -EINVAL;
  804. break;
  805. }
  806. /* else still active */
  807. if (time_after(jiffies, timeout)) {
  808. /*
  809. * Timed out while active. We can't reset the engine
  810. * if it is stuck active, but run through the
  811. * error code to see what error bits are set.
  812. */
  813. dd_dev_err(dd, "%s firmware security time out\n", who);
  814. ret = -ETIMEDOUT;
  815. break;
  816. }
  817. msleep(20);
  818. }
  819. /*
  820. * Arrive here on success or failure. Clear all RSA engine
  821. * errors. All current errors will stick - the RSA logic is keeping
  822. * error high. All previous errors will clear - the RSA logic
  823. * is not keeping the error high.
  824. */
  825. write_csr(dd, MISC_ERR_CLEAR,
  826. MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK |
  827. MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK);
  828. /*
  829. * All that is left are the current errors. Print warnings on
  830. * authorization failure details, if any. Firmware authorization
  831. * can be retried, so these are only warnings.
  832. */
  833. reg = read_csr(dd, MISC_ERR_STATUS);
  834. if (ret) {
  835. if (reg & MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK)
  836. dd_dev_warn(dd, "%s firmware authorization failed\n",
  837. who);
  838. if (reg & MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK)
  839. dd_dev_warn(dd, "%s firmware key mismatch\n", who);
  840. }
  841. return ret;
  842. }
  843. static void load_security_variables(struct hfi1_devdata *dd,
  844. struct firmware_details *fdet)
  845. {
  846. /* Security variables a. Write the modulus */
  847. write_rsa_data(dd, MISC_CFG_RSA_MODULUS, fdet->modulus, KEY_SIZE);
  848. /* Security variables b. Write the r2 */
  849. write_rsa_data(dd, MISC_CFG_RSA_R2, fdet->r2, KEY_SIZE);
  850. /* Security variables c. Write the mu */
  851. write_rsa_data(dd, MISC_CFG_RSA_MU, fdet->mu, MU_SIZE);
  852. /* Security variables d. Write the header */
  853. write_streamed_rsa_data(dd, MISC_CFG_SHA_PRELOAD,
  854. (u8 *)fdet->css_header,
  855. sizeof(struct css_header));
  856. }
  857. /* return the 8051 firmware state */
  858. static inline u32 get_firmware_state(struct hfi1_devdata *dd)
  859. {
  860. u64 reg = read_csr(dd, DC_DC8051_STS_CUR_STATE);
  861. return (reg >> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT)
  862. & DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK;
  863. }
  864. /*
  865. * Wait until the firmware is up and ready to take host requests.
  866. * Return 0 on success, -ETIMEDOUT on timeout.
  867. */
  868. int wait_fm_ready(struct hfi1_devdata *dd, u32 mstimeout)
  869. {
  870. unsigned long timeout;
  871. /* in the simulator, the fake 8051 is always ready */
  872. if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
  873. return 0;
  874. timeout = msecs_to_jiffies(mstimeout) + jiffies;
  875. while (1) {
  876. if (get_firmware_state(dd) == 0xa0) /* ready */
  877. return 0;
  878. if (time_after(jiffies, timeout)) /* timed out */
  879. return -ETIMEDOUT;
  880. usleep_range(1950, 2050); /* sleep 2ms-ish */
  881. }
  882. }
  883. /*
  884. * Load the 8051 firmware.
  885. */
  886. static int load_8051_firmware(struct hfi1_devdata *dd,
  887. struct firmware_details *fdet)
  888. {
  889. u64 reg;
  890. int ret;
  891. u8 ver_major;
  892. u8 ver_minor;
  893. u8 ver_patch;
  894. /*
  895. * DC Reset sequence
  896. * Load DC 8051 firmware
  897. */
  898. /*
  899. * DC reset step 1: Reset DC8051
  900. */
  901. reg = DC_DC8051_CFG_RST_M8051W_SMASK
  902. | DC_DC8051_CFG_RST_CRAM_SMASK
  903. | DC_DC8051_CFG_RST_DRAM_SMASK
  904. | DC_DC8051_CFG_RST_IRAM_SMASK
  905. | DC_DC8051_CFG_RST_SFR_SMASK;
  906. write_csr(dd, DC_DC8051_CFG_RST, reg);
  907. /*
  908. * DC reset step 2 (optional): Load 8051 data memory with link
  909. * configuration
  910. */
  911. /*
  912. * DC reset step 3: Load DC8051 firmware
  913. */
  914. /* release all but the core reset */
  915. reg = DC_DC8051_CFG_RST_M8051W_SMASK;
  916. write_csr(dd, DC_DC8051_CFG_RST, reg);
  917. /* Firmware load step 1 */
  918. load_security_variables(dd, fdet);
  919. /*
  920. * Firmware load step 2. Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
  921. */
  922. write_csr(dd, MISC_CFG_FW_CTRL, 0);
  923. /* Firmware load steps 3-5 */
  924. ret = write_8051(dd, 1/*code*/, 0, fdet->firmware_ptr,
  925. fdet->firmware_len);
  926. if (ret)
  927. return ret;
  928. /*
  929. * DC reset step 4. Host starts the DC8051 firmware
  930. */
  931. /*
  932. * Firmware load step 6. Set MISC_CFG_FW_CTRL.FW_8051_LOADED
  933. */
  934. write_csr(dd, MISC_CFG_FW_CTRL, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK);
  935. /* Firmware load steps 7-10 */
  936. ret = run_rsa(dd, "8051", fdet->signature);
  937. if (ret)
  938. return ret;
  939. /* clear all reset bits, releasing the 8051 */
  940. write_csr(dd, DC_DC8051_CFG_RST, 0ull);
  941. /*
  942. * DC reset step 5. Wait for firmware to be ready to accept host
  943. * requests.
  944. */
  945. ret = wait_fm_ready(dd, TIMEOUT_8051_START);
  946. if (ret) { /* timed out */
  947. dd_dev_err(dd, "8051 start timeout, current state 0x%x\n",
  948. get_firmware_state(dd));
  949. return -ETIMEDOUT;
  950. }
  951. read_misc_status(dd, &ver_major, &ver_minor, &ver_patch);
  952. dd_dev_info(dd, "8051 firmware version %d.%d.%d\n",
  953. (int)ver_major, (int)ver_minor, (int)ver_patch);
  954. dd->dc8051_ver = dc8051_ver(ver_major, ver_minor, ver_patch);
  955. return 0;
  956. }
  957. /*
  958. * Write the SBus request register
  959. *
  960. * No need for masking - the arguments are sized exactly.
  961. */
  962. void sbus_request(struct hfi1_devdata *dd,
  963. u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
  964. {
  965. write_csr(dd, ASIC_CFG_SBUS_REQUEST,
  966. ((u64)data_in << ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT) |
  967. ((u64)command << ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT) |
  968. ((u64)data_addr << ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT) |
  969. ((u64)receiver_addr <<
  970. ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT));
  971. }
  972. /*
  973. * Read a value from the SBus.
  974. *
  975. * Requires the caller to be in fast mode
  976. */
  977. static u32 sbus_read(struct hfi1_devdata *dd, u8 receiver_addr, u8 data_addr,
  978. u32 data_in)
  979. {
  980. u64 reg;
  981. int retries;
  982. int success = 0;
  983. u32 result = 0;
  984. u32 result_code = 0;
  985. sbus_request(dd, receiver_addr, data_addr, READ_SBUS_RECEIVER, data_in);
  986. for (retries = 0; retries < 100; retries++) {
  987. usleep_range(1000, 1200); /* arbitrary */
  988. reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
  989. result_code = (reg >> ASIC_STS_SBUS_RESULT_RESULT_CODE_SHIFT)
  990. & ASIC_STS_SBUS_RESULT_RESULT_CODE_MASK;
  991. if (result_code != SBUS_READ_COMPLETE)
  992. continue;
  993. success = 1;
  994. result = (reg >> ASIC_STS_SBUS_RESULT_DATA_OUT_SHIFT)
  995. & ASIC_STS_SBUS_RESULT_DATA_OUT_MASK;
  996. break;
  997. }
  998. if (!success) {
  999. dd_dev_err(dd, "%s: read failed, result code 0x%x\n", __func__,
  1000. result_code);
  1001. }
  1002. return result;
  1003. }
  1004. /*
  1005. * Turn off the SBus and fabric serdes spicos.
  1006. *
  1007. * + Must be called with Sbus fast mode turned on.
  1008. * + Must be called after fabric serdes broadcast is set up.
  1009. * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
  1010. * when using MISC_CFG_FW_CTRL.
  1011. */
  1012. static void turn_off_spicos(struct hfi1_devdata *dd, int flags)
  1013. {
  1014. /* only needed on A0 */
  1015. if (!is_ax(dd))
  1016. return;
  1017. dd_dev_info(dd, "Turning off spicos:%s%s\n",
  1018. flags & SPICO_SBUS ? " SBus" : "",
  1019. flags & SPICO_FABRIC ? " fabric" : "");
  1020. write_csr(dd, MISC_CFG_FW_CTRL, ENABLE_SPICO_SMASK);
  1021. /* disable SBus spico */
  1022. if (flags & SPICO_SBUS)
  1023. sbus_request(dd, SBUS_MASTER_BROADCAST, 0x01,
  1024. WRITE_SBUS_RECEIVER, 0x00000040);
  1025. /* disable the fabric serdes spicos */
  1026. if (flags & SPICO_FABRIC)
  1027. sbus_request(dd, fabric_serdes_broadcast[dd->hfi1_id],
  1028. 0x07, WRITE_SBUS_RECEIVER, 0x00000000);
  1029. write_csr(dd, MISC_CFG_FW_CTRL, 0);
  1030. }
  1031. /*
  1032. * Reset all of the fabric serdes for this HFI in preparation to take the
  1033. * link to Polling.
  1034. *
  1035. * To do a reset, we need to write to to the serdes registers. Unfortunately,
  1036. * the fabric serdes download to the other HFI on the ASIC will have turned
  1037. * off the firmware validation on this HFI. This means we can't write to the
  1038. * registers to reset the serdes. Work around this by performing a complete
  1039. * re-download and validation of the fabric serdes firmware. This, as a
  1040. * by-product, will reset the serdes. NOTE: the re-download requires that
  1041. * the 8051 be in the Offline state. I.e. not actively trying to use the
  1042. * serdes. This routine is called at the point where the link is Offline and
  1043. * is getting ready to go to Polling.
  1044. */
  1045. void fabric_serdes_reset(struct hfi1_devdata *dd)
  1046. {
  1047. int ret;
  1048. if (!fw_fabric_serdes_load)
  1049. return;
  1050. ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
  1051. if (ret) {
  1052. dd_dev_err(dd,
  1053. "Cannot acquire SBus resource to reset fabric SerDes - perhaps you should reboot\n");
  1054. return;
  1055. }
  1056. set_sbus_fast_mode(dd);
  1057. if (is_ax(dd)) {
  1058. /* A0 serdes do not work with a re-download */
  1059. u8 ra = fabric_serdes_broadcast[dd->hfi1_id];
  1060. /* place SerDes in reset and disable SPICO */
  1061. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
  1062. /* wait 100 refclk cycles @ 156.25MHz => 640ns */
  1063. udelay(1);
  1064. /* remove SerDes reset */
  1065. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
  1066. /* turn SPICO enable on */
  1067. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
  1068. } else {
  1069. turn_off_spicos(dd, SPICO_FABRIC);
  1070. /*
  1071. * No need for firmware retry - what to download has already
  1072. * been decided.
  1073. * No need to pay attention to the load return - the only
  1074. * failure is a validation failure, which has already been
  1075. * checked by the initial download.
  1076. */
  1077. (void)load_fabric_serdes_firmware(dd, &fw_fabric);
  1078. }
  1079. clear_sbus_fast_mode(dd);
  1080. release_chip_resource(dd, CR_SBUS);
  1081. }
  1082. /* Access to the SBus in this routine should probably be serialized */
  1083. int sbus_request_slow(struct hfi1_devdata *dd,
  1084. u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
  1085. {
  1086. u64 reg, count = 0;
  1087. /* make sure fast mode is clear */
  1088. clear_sbus_fast_mode(dd);
  1089. sbus_request(dd, receiver_addr, data_addr, command, data_in);
  1090. write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
  1091. ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK);
  1092. /* Wait for both DONE and RCV_DATA_VALID to go high */
  1093. reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
  1094. while (!((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
  1095. (reg & ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK))) {
  1096. if (count++ >= SBUS_MAX_POLL_COUNT) {
  1097. u64 counts = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
  1098. /*
  1099. * If the loop has timed out, we are OK if DONE bit
  1100. * is set and RCV_DATA_VALID and EXECUTE counters
  1101. * are the same. If not, we cannot proceed.
  1102. */
  1103. if ((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
  1104. (SBUS_COUNTER(counts, RCV_DATA_VALID) ==
  1105. SBUS_COUNTER(counts, EXECUTE)))
  1106. break;
  1107. return -ETIMEDOUT;
  1108. }
  1109. udelay(1);
  1110. reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
  1111. }
  1112. count = 0;
  1113. write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
  1114. /* Wait for DONE to clear after EXECUTE is cleared */
  1115. reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
  1116. while (reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) {
  1117. if (count++ >= SBUS_MAX_POLL_COUNT)
  1118. return -ETIME;
  1119. udelay(1);
  1120. reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
  1121. }
  1122. return 0;
  1123. }
  1124. static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
  1125. struct firmware_details *fdet)
  1126. {
  1127. int i, err;
  1128. const u8 ra = fabric_serdes_broadcast[dd->hfi1_id]; /* receiver addr */
  1129. dd_dev_info(dd, "Downloading fabric firmware\n");
  1130. /* step 1: load security variables */
  1131. load_security_variables(dd, fdet);
  1132. /* step 2: place SerDes in reset and disable SPICO */
  1133. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
  1134. /* wait 100 refclk cycles @ 156.25MHz => 640ns */
  1135. udelay(1);
  1136. /* step 3: remove SerDes reset */
  1137. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
  1138. /* step 4: assert IMEM override */
  1139. sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x40000000);
  1140. /* step 5: download SerDes machine code */
  1141. for (i = 0; i < fdet->firmware_len; i += 4) {
  1142. sbus_request(dd, ra, 0x0a, WRITE_SBUS_RECEIVER,
  1143. *(u32 *)&fdet->firmware_ptr[i]);
  1144. }
  1145. /* step 6: IMEM override off */
  1146. sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x00000000);
  1147. /* step 7: turn ECC on */
  1148. sbus_request(dd, ra, 0x0b, WRITE_SBUS_RECEIVER, 0x000c0000);
  1149. /* steps 8-11: run the RSA engine */
  1150. err = run_rsa(dd, "fabric serdes", fdet->signature);
  1151. if (err)
  1152. return err;
  1153. /* step 12: turn SPICO enable on */
  1154. sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
  1155. /* step 13: enable core hardware interrupts */
  1156. sbus_request(dd, ra, 0x08, WRITE_SBUS_RECEIVER, 0x00000000);
  1157. return 0;
  1158. }
  1159. static int load_sbus_firmware(struct hfi1_devdata *dd,
  1160. struct firmware_details *fdet)
  1161. {
  1162. int i, err;
  1163. const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
  1164. dd_dev_info(dd, "Downloading SBus firmware\n");
  1165. /* step 1: load security variables */
  1166. load_security_variables(dd, fdet);
  1167. /* step 2: place SPICO into reset and enable off */
  1168. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x000000c0);
  1169. /* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
  1170. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000240);
  1171. /* step 4: set starting IMEM address for burst download */
  1172. sbus_request(dd, ra, 0x03, WRITE_SBUS_RECEIVER, 0x80000000);
  1173. /* step 5: download the SBus Master machine code */
  1174. for (i = 0; i < fdet->firmware_len; i += 4) {
  1175. sbus_request(dd, ra, 0x14, WRITE_SBUS_RECEIVER,
  1176. *(u32 *)&fdet->firmware_ptr[i]);
  1177. }
  1178. /* step 6: set IMEM_CNTL_EN off */
  1179. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000040);
  1180. /* step 7: turn ECC on */
  1181. sbus_request(dd, ra, 0x16, WRITE_SBUS_RECEIVER, 0x000c0000);
  1182. /* steps 8-11: run the RSA engine */
  1183. err = run_rsa(dd, "SBus", fdet->signature);
  1184. if (err)
  1185. return err;
  1186. /* step 12: set SPICO_ENABLE on */
  1187. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
  1188. return 0;
  1189. }
  1190. static int load_pcie_serdes_firmware(struct hfi1_devdata *dd,
  1191. struct firmware_details *fdet)
  1192. {
  1193. int i;
  1194. const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
  1195. dd_dev_info(dd, "Downloading PCIe firmware\n");
  1196. /* step 1: load security variables */
  1197. load_security_variables(dd, fdet);
  1198. /* step 2: assert single step (halts the SBus Master spico) */
  1199. sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000001);
  1200. /* step 3: enable XDMEM access */
  1201. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000d40);
  1202. /* step 4: load firmware into SBus Master XDMEM */
  1203. /*
  1204. * NOTE: the dmem address, write_en, and wdata are all pre-packed,
  1205. * we only need to pick up the bytes and write them
  1206. */
  1207. for (i = 0; i < fdet->firmware_len; i += 4) {
  1208. sbus_request(dd, ra, 0x04, WRITE_SBUS_RECEIVER,
  1209. *(u32 *)&fdet->firmware_ptr[i]);
  1210. }
  1211. /* step 5: disable XDMEM access */
  1212. sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
  1213. /* step 6: allow SBus Spico to run */
  1214. sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000000);
  1215. /*
  1216. * steps 7-11: run RSA, if it succeeds, firmware is available to
  1217. * be swapped
  1218. */
  1219. return run_rsa(dd, "PCIe serdes", fdet->signature);
  1220. }
  1221. /*
  1222. * Set the given broadcast values on the given list of devices.
  1223. */
  1224. static void set_serdes_broadcast(struct hfi1_devdata *dd, u8 bg1, u8 bg2,
  1225. const u8 *addrs, int count)
  1226. {
  1227. while (--count >= 0) {
  1228. /*
  1229. * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
  1230. * defaults for everything else. Do not read-modify-write,
  1231. * per instruction from the manufacturer.
  1232. *
  1233. * Register 0xfd:
  1234. * bits what
  1235. * ----- ---------------------------------
  1236. * 0 IGNORE_BROADCAST (default 0)
  1237. * 11:4 BROADCAST_GROUP_1 (default 0xff)
  1238. * 23:16 BROADCAST_GROUP_2 (default 0xff)
  1239. */
  1240. sbus_request(dd, addrs[count], 0xfd, WRITE_SBUS_RECEIVER,
  1241. (u32)bg1 << 4 | (u32)bg2 << 16);
  1242. }
  1243. }
  1244. int acquire_hw_mutex(struct hfi1_devdata *dd)
  1245. {
  1246. unsigned long timeout;
  1247. int try = 0;
  1248. u8 mask = 1 << dd->hfi1_id;
  1249. u8 user;
  1250. retry:
  1251. timeout = msecs_to_jiffies(HM_TIMEOUT) + jiffies;
  1252. while (1) {
  1253. write_csr(dd, ASIC_CFG_MUTEX, mask);
  1254. user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
  1255. if (user == mask)
  1256. return 0; /* success */
  1257. if (time_after(jiffies, timeout))
  1258. break; /* timed out */
  1259. msleep(20);
  1260. }
  1261. /* timed out */
  1262. dd_dev_err(dd,
  1263. "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
  1264. (u32)user, (u32)mask, (try == 0) ? "retrying" : "giving up");
  1265. if (try == 0) {
  1266. /* break mutex and retry */
  1267. write_csr(dd, ASIC_CFG_MUTEX, 0);
  1268. try++;
  1269. goto retry;
  1270. }
  1271. return -EBUSY;
  1272. }
  1273. void release_hw_mutex(struct hfi1_devdata *dd)
  1274. {
  1275. write_csr(dd, ASIC_CFG_MUTEX, 0);
  1276. }
  1277. /* return the given resource bit(s) as a mask for the given HFI */
  1278. static inline u64 resource_mask(u32 hfi1_id, u32 resource)
  1279. {
  1280. return ((u64)resource) << (hfi1_id ? CR_DYN_SHIFT : 0);
  1281. }
  1282. static void fail_mutex_acquire_message(struct hfi1_devdata *dd,
  1283. const char *func)
  1284. {
  1285. dd_dev_err(dd,
  1286. "%s: hardware mutex stuck - suggest rebooting the machine\n",
  1287. func);
  1288. }
  1289. /*
  1290. * Acquire access to a chip resource.
  1291. *
  1292. * Return 0 on success, -EBUSY if resource busy, -EIO if mutex acquire failed.
  1293. */
  1294. static int __acquire_chip_resource(struct hfi1_devdata *dd, u32 resource)
  1295. {
  1296. u64 scratch0, all_bits, my_bit;
  1297. int ret;
  1298. if (resource & CR_DYN_MASK) {
  1299. /* a dynamic resource is in use if either HFI has set the bit */
  1300. if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0 &&
  1301. (resource & (CR_I2C1 | CR_I2C2))) {
  1302. /* discrete devices must serialize across both chains */
  1303. all_bits = resource_mask(0, CR_I2C1 | CR_I2C2) |
  1304. resource_mask(1, CR_I2C1 | CR_I2C2);
  1305. } else {
  1306. all_bits = resource_mask(0, resource) |
  1307. resource_mask(1, resource);
  1308. }
  1309. my_bit = resource_mask(dd->hfi1_id, resource);
  1310. } else {
  1311. /* non-dynamic resources are not split between HFIs */
  1312. all_bits = resource;
  1313. my_bit = resource;
  1314. }
  1315. /* lock against other callers within the driver wanting a resource */
  1316. mutex_lock(&dd->asic_data->asic_resource_mutex);
  1317. ret = acquire_hw_mutex(dd);
  1318. if (ret) {
  1319. fail_mutex_acquire_message(dd, __func__);
  1320. ret = -EIO;
  1321. goto done;
  1322. }
  1323. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  1324. if (scratch0 & all_bits) {
  1325. ret = -EBUSY;
  1326. } else {
  1327. write_csr(dd, ASIC_CFG_SCRATCH, scratch0 | my_bit);
  1328. /* force write to be visible to other HFI on another OS */
  1329. (void)read_csr(dd, ASIC_CFG_SCRATCH);
  1330. }
  1331. release_hw_mutex(dd);
  1332. done:
  1333. mutex_unlock(&dd->asic_data->asic_resource_mutex);
  1334. return ret;
  1335. }
  1336. /*
  1337. * Acquire access to a chip resource, wait up to mswait milliseconds for
  1338. * the resource to become available.
  1339. *
  1340. * Return 0 on success, -EBUSY if busy (even after wait), -EIO if mutex
  1341. * acquire failed.
  1342. */
  1343. int acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait)
  1344. {
  1345. unsigned long timeout;
  1346. int ret;
  1347. timeout = jiffies + msecs_to_jiffies(mswait);
  1348. while (1) {
  1349. ret = __acquire_chip_resource(dd, resource);
  1350. if (ret != -EBUSY)
  1351. return ret;
  1352. /* resource is busy, check our timeout */
  1353. if (time_after_eq(jiffies, timeout))
  1354. return -EBUSY;
  1355. usleep_range(80, 120); /* arbitrary delay */
  1356. }
  1357. }
  1358. /*
  1359. * Release access to a chip resource
  1360. */
  1361. void release_chip_resource(struct hfi1_devdata *dd, u32 resource)
  1362. {
  1363. u64 scratch0, bit;
  1364. /* only dynamic resources should ever be cleared */
  1365. if (!(resource & CR_DYN_MASK)) {
  1366. dd_dev_err(dd, "%s: invalid resource 0x%x\n", __func__,
  1367. resource);
  1368. return;
  1369. }
  1370. bit = resource_mask(dd->hfi1_id, resource);
  1371. /* lock against other callers within the driver wanting a resource */
  1372. mutex_lock(&dd->asic_data->asic_resource_mutex);
  1373. if (acquire_hw_mutex(dd)) {
  1374. fail_mutex_acquire_message(dd, __func__);
  1375. goto done;
  1376. }
  1377. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  1378. if ((scratch0 & bit) != 0) {
  1379. scratch0 &= ~bit;
  1380. write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
  1381. /* force write to be visible to other HFI on another OS */
  1382. (void)read_csr(dd, ASIC_CFG_SCRATCH);
  1383. } else {
  1384. dd_dev_warn(dd, "%s: id %d, resource 0x%x: bit not set\n",
  1385. __func__, dd->hfi1_id, resource);
  1386. }
  1387. release_hw_mutex(dd);
  1388. done:
  1389. mutex_unlock(&dd->asic_data->asic_resource_mutex);
  1390. }
  1391. /*
  1392. * Return true if resource is set, false otherwise. Print a warning
  1393. * if not set and a function is supplied.
  1394. */
  1395. bool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
  1396. const char *func)
  1397. {
  1398. u64 scratch0, bit;
  1399. if (resource & CR_DYN_MASK)
  1400. bit = resource_mask(dd->hfi1_id, resource);
  1401. else
  1402. bit = resource;
  1403. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  1404. if ((scratch0 & bit) == 0) {
  1405. if (func)
  1406. dd_dev_warn(dd,
  1407. "%s: id %d, resource 0x%x, not acquired!\n",
  1408. func, dd->hfi1_id, resource);
  1409. return false;
  1410. }
  1411. return true;
  1412. }
  1413. static void clear_chip_resources(struct hfi1_devdata *dd, const char *func)
  1414. {
  1415. u64 scratch0;
  1416. /* lock against other callers within the driver wanting a resource */
  1417. mutex_lock(&dd->asic_data->asic_resource_mutex);
  1418. if (acquire_hw_mutex(dd)) {
  1419. fail_mutex_acquire_message(dd, func);
  1420. goto done;
  1421. }
  1422. /* clear all dynamic access bits for this HFI */
  1423. scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
  1424. scratch0 &= ~resource_mask(dd->hfi1_id, CR_DYN_MASK);
  1425. write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
  1426. /* force write to be visible to other HFI on another OS */
  1427. (void)read_csr(dd, ASIC_CFG_SCRATCH);
  1428. release_hw_mutex(dd);
  1429. done:
  1430. mutex_unlock(&dd->asic_data->asic_resource_mutex);
  1431. }
  1432. void init_chip_resources(struct hfi1_devdata *dd)
  1433. {
  1434. /* clear any holds left by us */
  1435. clear_chip_resources(dd, __func__);
  1436. }
  1437. void finish_chip_resources(struct hfi1_devdata *dd)
  1438. {
  1439. /* clear any holds left by us */
  1440. clear_chip_resources(dd, __func__);
  1441. }
  1442. void set_sbus_fast_mode(struct hfi1_devdata *dd)
  1443. {
  1444. write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
  1445. ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK);
  1446. }
  1447. void clear_sbus_fast_mode(struct hfi1_devdata *dd)
  1448. {
  1449. u64 reg, count = 0;
  1450. reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
  1451. while (SBUS_COUNTER(reg, EXECUTE) !=
  1452. SBUS_COUNTER(reg, RCV_DATA_VALID)) {
  1453. if (count++ >= SBUS_MAX_POLL_COUNT)
  1454. break;
  1455. udelay(1);
  1456. reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
  1457. }
  1458. write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
  1459. }
  1460. int load_firmware(struct hfi1_devdata *dd)
  1461. {
  1462. int ret;
  1463. if (fw_fabric_serdes_load) {
  1464. ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
  1465. if (ret)
  1466. return ret;
  1467. set_sbus_fast_mode(dd);
  1468. set_serdes_broadcast(dd, all_fabric_serdes_broadcast,
  1469. fabric_serdes_broadcast[dd->hfi1_id],
  1470. fabric_serdes_addrs[dd->hfi1_id],
  1471. NUM_FABRIC_SERDES);
  1472. turn_off_spicos(dd, SPICO_FABRIC);
  1473. do {
  1474. ret = load_fabric_serdes_firmware(dd, &fw_fabric);
  1475. } while (retry_firmware(dd, ret));
  1476. clear_sbus_fast_mode(dd);
  1477. release_chip_resource(dd, CR_SBUS);
  1478. if (ret)
  1479. return ret;
  1480. }
  1481. if (fw_8051_load) {
  1482. do {
  1483. ret = load_8051_firmware(dd, &fw_8051);
  1484. } while (retry_firmware(dd, ret));
  1485. if (ret)
  1486. return ret;
  1487. }
  1488. dump_fw_version(dd);
  1489. return 0;
  1490. }
  1491. int hfi1_firmware_init(struct hfi1_devdata *dd)
  1492. {
  1493. /* only RTL can use these */
  1494. if (dd->icode != ICODE_RTL_SILICON) {
  1495. fw_fabric_serdes_load = 0;
  1496. fw_pcie_serdes_load = 0;
  1497. fw_sbus_load = 0;
  1498. }
  1499. /* no 8051 or QSFP on simulator */
  1500. if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR) {
  1501. fw_8051_load = 0;
  1502. platform_config_load = 0;
  1503. }
  1504. if (!fw_8051_name) {
  1505. if (dd->icode == ICODE_RTL_SILICON)
  1506. fw_8051_name = DEFAULT_FW_8051_NAME_ASIC;
  1507. else
  1508. fw_8051_name = DEFAULT_FW_8051_NAME_FPGA;
  1509. }
  1510. if (!fw_fabric_serdes_name)
  1511. fw_fabric_serdes_name = DEFAULT_FW_FABRIC_NAME;
  1512. if (!fw_sbus_name)
  1513. fw_sbus_name = DEFAULT_FW_SBUS_NAME;
  1514. if (!fw_pcie_serdes_name)
  1515. fw_pcie_serdes_name = DEFAULT_FW_PCIE_NAME;
  1516. if (!platform_config_name)
  1517. platform_config_name = DEFAULT_PLATFORM_CONFIG_NAME;
  1518. return obtain_firmware(dd);
  1519. }
  1520. /*
  1521. * This function is a helper function for parse_platform_config(...) and
  1522. * does not check for validity of the platform configuration cache
  1523. * (because we know it is invalid as we are building up the cache).
  1524. * As such, this should not be called from anywhere other than
  1525. * parse_platform_config
  1526. */
  1527. static int check_meta_version(struct hfi1_devdata *dd, u32 *system_table)
  1528. {
  1529. u32 meta_ver, meta_ver_meta, ver_start, ver_len, mask;
  1530. struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
  1531. if (!system_table)
  1532. return -EINVAL;
  1533. meta_ver_meta =
  1534. *(pcfgcache->config_tables[PLATFORM_CONFIG_SYSTEM_TABLE].table_metadata
  1535. + SYSTEM_TABLE_META_VERSION);
  1536. mask = ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
  1537. ver_start = meta_ver_meta & mask;
  1538. meta_ver_meta >>= METADATA_TABLE_FIELD_LEN_SHIFT;
  1539. mask = ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
  1540. ver_len = meta_ver_meta & mask;
  1541. ver_start /= 8;
  1542. meta_ver = *((u8 *)system_table + ver_start) & ((1 << ver_len) - 1);
  1543. if (meta_ver < 5) {
  1544. dd_dev_info(
  1545. dd, "%s:Please update platform config\n", __func__);
  1546. return -EINVAL;
  1547. }
  1548. return 0;
  1549. }
  1550. int parse_platform_config(struct hfi1_devdata *dd)
  1551. {
  1552. struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
  1553. u32 *ptr = NULL;
  1554. u32 header1 = 0, header2 = 0, magic_num = 0, crc = 0, file_length = 0;
  1555. u32 record_idx = 0, table_type = 0, table_length_dwords = 0;
  1556. int ret = -EINVAL; /* assume failure */
  1557. /*
  1558. * For integrated devices that did not fall back to the default file,
  1559. * the SI tuning information for active channels is acquired from the
  1560. * scratch register bitmap, thus there is no platform config to parse.
  1561. * Skip parsing in these situations.
  1562. */
  1563. if (is_integrated(dd) && !platform_config_load)
  1564. return 0;
  1565. if (!dd->platform_config.data) {
  1566. dd_dev_err(dd, "%s: Missing config file\n", __func__);
  1567. goto bail;
  1568. }
  1569. ptr = (u32 *)dd->platform_config.data;
  1570. magic_num = *ptr;
  1571. ptr++;
  1572. if (magic_num != PLATFORM_CONFIG_MAGIC_NUM) {
  1573. dd_dev_err(dd, "%s: Bad config file\n", __func__);
  1574. goto bail;
  1575. }
  1576. /* Field is file size in DWORDs */
  1577. file_length = (*ptr) * 4;
  1578. ptr++;
  1579. if (file_length > dd->platform_config.size) {
  1580. dd_dev_info(dd, "%s:File claims to be larger than read size\n",
  1581. __func__);
  1582. goto bail;
  1583. } else if (file_length < dd->platform_config.size) {
  1584. dd_dev_info(dd,
  1585. "%s:File claims to be smaller than read size, continuing\n",
  1586. __func__);
  1587. }
  1588. /* exactly equal, perfection */
  1589. /*
  1590. * In both cases where we proceed, using the self-reported file length
  1591. * is the safer option
  1592. */
  1593. while (ptr < (u32 *)(dd->platform_config.data + file_length)) {
  1594. header1 = *ptr;
  1595. header2 = *(ptr + 1);
  1596. if (header1 != ~header2) {
  1597. dd_dev_err(dd, "%s: Failed validation at offset %ld\n",
  1598. __func__, (ptr - (u32 *)
  1599. dd->platform_config.data));
  1600. goto bail;
  1601. }
  1602. record_idx = *ptr &
  1603. ((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS) - 1);
  1604. table_length_dwords = (*ptr >>
  1605. PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT) &
  1606. ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS) - 1);
  1607. table_type = (*ptr >> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT) &
  1608. ((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS) - 1);
  1609. /* Done with this set of headers */
  1610. ptr += 2;
  1611. if (record_idx) {
  1612. /* data table */
  1613. switch (table_type) {
  1614. case PLATFORM_CONFIG_SYSTEM_TABLE:
  1615. pcfgcache->config_tables[table_type].num_table =
  1616. 1;
  1617. ret = check_meta_version(dd, ptr);
  1618. if (ret)
  1619. goto bail;
  1620. break;
  1621. case PLATFORM_CONFIG_PORT_TABLE:
  1622. pcfgcache->config_tables[table_type].num_table =
  1623. 2;
  1624. break;
  1625. case PLATFORM_CONFIG_RX_PRESET_TABLE:
  1626. /* fall through */
  1627. case PLATFORM_CONFIG_TX_PRESET_TABLE:
  1628. /* fall through */
  1629. case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
  1630. /* fall through */
  1631. case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
  1632. pcfgcache->config_tables[table_type].num_table =
  1633. table_length_dwords;
  1634. break;
  1635. default:
  1636. dd_dev_err(dd,
  1637. "%s: Unknown data table %d, offset %ld\n",
  1638. __func__, table_type,
  1639. (ptr - (u32 *)
  1640. dd->platform_config.data));
  1641. goto bail; /* We don't trust this file now */
  1642. }
  1643. pcfgcache->config_tables[table_type].table = ptr;
  1644. } else {
  1645. /* metadata table */
  1646. switch (table_type) {
  1647. case PLATFORM_CONFIG_SYSTEM_TABLE:
  1648. /* fall through */
  1649. case PLATFORM_CONFIG_PORT_TABLE:
  1650. /* fall through */
  1651. case PLATFORM_CONFIG_RX_PRESET_TABLE:
  1652. /* fall through */
  1653. case PLATFORM_CONFIG_TX_PRESET_TABLE:
  1654. /* fall through */
  1655. case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
  1656. /* fall through */
  1657. case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
  1658. break;
  1659. default:
  1660. dd_dev_err(dd,
  1661. "%s: Unknown meta table %d, offset %ld\n",
  1662. __func__, table_type,
  1663. (ptr -
  1664. (u32 *)dd->platform_config.data));
  1665. goto bail; /* We don't trust this file now */
  1666. }
  1667. pcfgcache->config_tables[table_type].table_metadata =
  1668. ptr;
  1669. }
  1670. /* Calculate and check table crc */
  1671. crc = crc32_le(~(u32)0, (unsigned char const *)ptr,
  1672. (table_length_dwords * 4));
  1673. crc ^= ~(u32)0;
  1674. /* Jump the table */
  1675. ptr += table_length_dwords;
  1676. if (crc != *ptr) {
  1677. dd_dev_err(dd, "%s: Failed CRC check at offset %ld\n",
  1678. __func__, (ptr -
  1679. (u32 *)dd->platform_config.data));
  1680. goto bail;
  1681. }
  1682. /* Jump the CRC DWORD */
  1683. ptr++;
  1684. }
  1685. pcfgcache->cache_valid = 1;
  1686. return 0;
  1687. bail:
  1688. memset(pcfgcache, 0, sizeof(struct platform_config_cache));
  1689. return ret;
  1690. }
  1691. static void get_integrated_platform_config_field(
  1692. struct hfi1_devdata *dd,
  1693. enum platform_config_table_type_encoding table_type,
  1694. int field_index, u32 *data)
  1695. {
  1696. struct hfi1_pportdata *ppd = dd->pport;
  1697. u8 *cache = ppd->qsfp_info.cache;
  1698. u32 tx_preset = 0;
  1699. switch (table_type) {
  1700. case PLATFORM_CONFIG_SYSTEM_TABLE:
  1701. if (field_index == SYSTEM_TABLE_QSFP_POWER_CLASS_MAX)
  1702. *data = ppd->max_power_class;
  1703. else if (field_index == SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_25G)
  1704. *data = ppd->default_atten;
  1705. break;
  1706. case PLATFORM_CONFIG_PORT_TABLE:
  1707. if (field_index == PORT_TABLE_PORT_TYPE)
  1708. *data = ppd->port_type;
  1709. else if (field_index == PORT_TABLE_LOCAL_ATTEN_25G)
  1710. *data = ppd->local_atten;
  1711. else if (field_index == PORT_TABLE_REMOTE_ATTEN_25G)
  1712. *data = ppd->remote_atten;
  1713. break;
  1714. case PLATFORM_CONFIG_RX_PRESET_TABLE:
  1715. if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR_APPLY)
  1716. *data = (ppd->rx_preset & QSFP_RX_CDR_APPLY_SMASK) >>
  1717. QSFP_RX_CDR_APPLY_SHIFT;
  1718. else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP_APPLY)
  1719. *data = (ppd->rx_preset & QSFP_RX_EMP_APPLY_SMASK) >>
  1720. QSFP_RX_EMP_APPLY_SHIFT;
  1721. else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP_APPLY)
  1722. *data = (ppd->rx_preset & QSFP_RX_AMP_APPLY_SMASK) >>
  1723. QSFP_RX_AMP_APPLY_SHIFT;
  1724. else if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR)
  1725. *data = (ppd->rx_preset & QSFP_RX_CDR_SMASK) >>
  1726. QSFP_RX_CDR_SHIFT;
  1727. else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP)
  1728. *data = (ppd->rx_preset & QSFP_RX_EMP_SMASK) >>
  1729. QSFP_RX_EMP_SHIFT;
  1730. else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP)
  1731. *data = (ppd->rx_preset & QSFP_RX_AMP_SMASK) >>
  1732. QSFP_RX_AMP_SHIFT;
  1733. break;
  1734. case PLATFORM_CONFIG_TX_PRESET_TABLE:
  1735. if (cache[QSFP_EQ_INFO_OFFS] & 0x4)
  1736. tx_preset = ppd->tx_preset_eq;
  1737. else
  1738. tx_preset = ppd->tx_preset_noeq;
  1739. if (field_index == TX_PRESET_TABLE_PRECUR)
  1740. *data = (tx_preset & TX_PRECUR_SMASK) >>
  1741. TX_PRECUR_SHIFT;
  1742. else if (field_index == TX_PRESET_TABLE_ATTN)
  1743. *data = (tx_preset & TX_ATTN_SMASK) >>
  1744. TX_ATTN_SHIFT;
  1745. else if (field_index == TX_PRESET_TABLE_POSTCUR)
  1746. *data = (tx_preset & TX_POSTCUR_SMASK) >>
  1747. TX_POSTCUR_SHIFT;
  1748. else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR_APPLY)
  1749. *data = (tx_preset & QSFP_TX_CDR_APPLY_SMASK) >>
  1750. QSFP_TX_CDR_APPLY_SHIFT;
  1751. else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ_APPLY)
  1752. *data = (tx_preset & QSFP_TX_EQ_APPLY_SMASK) >>
  1753. QSFP_TX_EQ_APPLY_SHIFT;
  1754. else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR)
  1755. *data = (tx_preset & QSFP_TX_CDR_SMASK) >>
  1756. QSFP_TX_CDR_SHIFT;
  1757. else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ)
  1758. *data = (tx_preset & QSFP_TX_EQ_SMASK) >>
  1759. QSFP_TX_EQ_SHIFT;
  1760. break;
  1761. case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
  1762. case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
  1763. default:
  1764. break;
  1765. }
  1766. }
  1767. static int get_platform_fw_field_metadata(struct hfi1_devdata *dd, int table,
  1768. int field, u32 *field_len_bits,
  1769. u32 *field_start_bits)
  1770. {
  1771. struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
  1772. u32 *src_ptr = NULL;
  1773. if (!pcfgcache->cache_valid)
  1774. return -EINVAL;
  1775. switch (table) {
  1776. case PLATFORM_CONFIG_SYSTEM_TABLE:
  1777. /* fall through */
  1778. case PLATFORM_CONFIG_PORT_TABLE:
  1779. /* fall through */
  1780. case PLATFORM_CONFIG_RX_PRESET_TABLE:
  1781. /* fall through */
  1782. case PLATFORM_CONFIG_TX_PRESET_TABLE:
  1783. /* fall through */
  1784. case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
  1785. /* fall through */
  1786. case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
  1787. if (field && field < platform_config_table_limits[table])
  1788. src_ptr =
  1789. pcfgcache->config_tables[table].table_metadata + field;
  1790. break;
  1791. default:
  1792. dd_dev_info(dd, "%s: Unknown table\n", __func__);
  1793. break;
  1794. }
  1795. if (!src_ptr)
  1796. return -EINVAL;
  1797. if (field_start_bits)
  1798. *field_start_bits = *src_ptr &
  1799. ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
  1800. if (field_len_bits)
  1801. *field_len_bits = (*src_ptr >> METADATA_TABLE_FIELD_LEN_SHIFT)
  1802. & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
  1803. return 0;
  1804. }
  1805. /* This is the central interface to getting data out of the platform config
  1806. * file. It depends on parse_platform_config() having populated the
  1807. * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
  1808. * validate the sanity of the cache.
  1809. *
  1810. * The non-obvious parameters:
  1811. * @table_index: Acts as a look up key into which instance of the tables the
  1812. * relevant field is fetched from.
  1813. *
  1814. * This applies to the data tables that have multiple instances. The port table
  1815. * is an exception to this rule as each HFI only has one port and thus the
  1816. * relevant table can be distinguished by hfi_id.
  1817. *
  1818. * @data: pointer to memory that will be populated with the field requested.
  1819. * @len: length of memory pointed by @data in bytes.
  1820. */
  1821. int get_platform_config_field(struct hfi1_devdata *dd,
  1822. enum platform_config_table_type_encoding
  1823. table_type, int table_index, int field_index,
  1824. u32 *data, u32 len)
  1825. {
  1826. int ret = 0, wlen = 0, seek = 0;
  1827. u32 field_len_bits = 0, field_start_bits = 0, *src_ptr = NULL;
  1828. struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
  1829. if (data)
  1830. memset(data, 0, len);
  1831. else
  1832. return -EINVAL;
  1833. if (is_integrated(dd) && !platform_config_load) {
  1834. /*
  1835. * Use saved configuration from ppd for integrated platforms
  1836. */
  1837. get_integrated_platform_config_field(dd, table_type,
  1838. field_index, data);
  1839. return 0;
  1840. }
  1841. ret = get_platform_fw_field_metadata(dd, table_type, field_index,
  1842. &field_len_bits,
  1843. &field_start_bits);
  1844. if (ret)
  1845. return -EINVAL;
  1846. /* Convert length to bits */
  1847. len *= 8;
  1848. /* Our metadata function checked cache_valid and field_index for us */
  1849. switch (table_type) {
  1850. case PLATFORM_CONFIG_SYSTEM_TABLE:
  1851. src_ptr = pcfgcache->config_tables[table_type].table;
  1852. if (field_index != SYSTEM_TABLE_QSFP_POWER_CLASS_MAX) {
  1853. if (len < field_len_bits)
  1854. return -EINVAL;
  1855. seek = field_start_bits / 8;
  1856. wlen = field_len_bits / 8;
  1857. src_ptr = (u32 *)((u8 *)src_ptr + seek);
  1858. /*
  1859. * We expect the field to be byte aligned and whole byte
  1860. * lengths if we are here
  1861. */
  1862. memcpy(data, src_ptr, wlen);
  1863. return 0;
  1864. }
  1865. break;
  1866. case PLATFORM_CONFIG_PORT_TABLE:
  1867. /* Port table is 4 DWORDS */
  1868. src_ptr = dd->hfi1_id ?
  1869. pcfgcache->config_tables[table_type].table + 4 :
  1870. pcfgcache->config_tables[table_type].table;
  1871. break;
  1872. case PLATFORM_CONFIG_RX_PRESET_TABLE:
  1873. /* fall through */
  1874. case PLATFORM_CONFIG_TX_PRESET_TABLE:
  1875. /* fall through */
  1876. case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
  1877. /* fall through */
  1878. case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
  1879. src_ptr = pcfgcache->config_tables[table_type].table;
  1880. if (table_index <
  1881. pcfgcache->config_tables[table_type].num_table)
  1882. src_ptr += table_index;
  1883. else
  1884. src_ptr = NULL;
  1885. break;
  1886. default:
  1887. dd_dev_info(dd, "%s: Unknown table\n", __func__);
  1888. break;
  1889. }
  1890. if (!src_ptr || len < field_len_bits)
  1891. return -EINVAL;
  1892. src_ptr += (field_start_bits / 32);
  1893. *data = (*src_ptr >> (field_start_bits % 32)) &
  1894. ((1 << field_len_bits) - 1);
  1895. return 0;
  1896. }
  1897. /*
  1898. * Download the firmware needed for the Gen3 PCIe SerDes. An update
  1899. * to the SBus firmware is needed before updating the PCIe firmware.
  1900. *
  1901. * Note: caller must be holding the SBus resource.
  1902. */
  1903. int load_pcie_firmware(struct hfi1_devdata *dd)
  1904. {
  1905. int ret = 0;
  1906. /* both firmware loads below use the SBus */
  1907. set_sbus_fast_mode(dd);
  1908. if (fw_sbus_load) {
  1909. turn_off_spicos(dd, SPICO_SBUS);
  1910. do {
  1911. ret = load_sbus_firmware(dd, &fw_sbus);
  1912. } while (retry_firmware(dd, ret));
  1913. if (ret)
  1914. goto done;
  1915. }
  1916. if (fw_pcie_serdes_load) {
  1917. dd_dev_info(dd, "Setting PCIe SerDes broadcast\n");
  1918. set_serdes_broadcast(dd, all_pcie_serdes_broadcast,
  1919. pcie_serdes_broadcast[dd->hfi1_id],
  1920. pcie_serdes_addrs[dd->hfi1_id],
  1921. NUM_PCIE_SERDES);
  1922. do {
  1923. ret = load_pcie_serdes_firmware(dd, &fw_pcie);
  1924. } while (retry_firmware(dd, ret));
  1925. if (ret)
  1926. goto done;
  1927. }
  1928. done:
  1929. clear_sbus_fast_mode(dd);
  1930. return ret;
  1931. }
  1932. /*
  1933. * Read the GUID from the hardware, store it in dd.
  1934. */
  1935. void read_guid(struct hfi1_devdata *dd)
  1936. {
  1937. /* Take the DC out of reset to get a valid GUID value */
  1938. write_csr(dd, CCE_DC_CTRL, 0);
  1939. (void)read_csr(dd, CCE_DC_CTRL);
  1940. dd->base_guid = read_csr(dd, DC_DC8051_CFG_LOCAL_GUID);
  1941. dd_dev_info(dd, "GUID %llx",
  1942. (unsigned long long)dd->base_guid);
  1943. }
  1944. /* read and display firmware version info */
  1945. static void dump_fw_version(struct hfi1_devdata *dd)
  1946. {
  1947. u32 pcie_vers[NUM_PCIE_SERDES];
  1948. u32 fabric_vers[NUM_FABRIC_SERDES];
  1949. u32 sbus_vers;
  1950. int i;
  1951. int all_same;
  1952. int ret;
  1953. u8 rcv_addr;
  1954. ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
  1955. if (ret) {
  1956. dd_dev_err(dd, "Unable to acquire SBus to read firmware versions\n");
  1957. return;
  1958. }
  1959. /* set fast mode */
  1960. set_sbus_fast_mode(dd);
  1961. /* read version for SBus Master */
  1962. sbus_request(dd, SBUS_MASTER_BROADCAST, 0x02, WRITE_SBUS_RECEIVER, 0);
  1963. sbus_request(dd, SBUS_MASTER_BROADCAST, 0x07, WRITE_SBUS_RECEIVER, 0x1);
  1964. /* wait for interrupt to be processed */
  1965. usleep_range(10000, 11000);
  1966. sbus_vers = sbus_read(dd, SBUS_MASTER_BROADCAST, 0x08, 0x1);
  1967. dd_dev_info(dd, "SBus Master firmware version 0x%08x\n", sbus_vers);
  1968. /* read version for PCIe SerDes */
  1969. all_same = 1;
  1970. pcie_vers[0] = 0;
  1971. for (i = 0; i < NUM_PCIE_SERDES; i++) {
  1972. rcv_addr = pcie_serdes_addrs[dd->hfi1_id][i];
  1973. sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
  1974. /* wait for interrupt to be processed */
  1975. usleep_range(10000, 11000);
  1976. pcie_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
  1977. if (i > 0 && pcie_vers[0] != pcie_vers[i])
  1978. all_same = 0;
  1979. }
  1980. if (all_same) {
  1981. dd_dev_info(dd, "PCIe SerDes firmware version 0x%x\n",
  1982. pcie_vers[0]);
  1983. } else {
  1984. dd_dev_warn(dd, "PCIe SerDes do not have the same firmware version\n");
  1985. for (i = 0; i < NUM_PCIE_SERDES; i++) {
  1986. dd_dev_info(dd,
  1987. "PCIe SerDes lane %d firmware version 0x%x\n",
  1988. i, pcie_vers[i]);
  1989. }
  1990. }
  1991. /* read version for fabric SerDes */
  1992. all_same = 1;
  1993. fabric_vers[0] = 0;
  1994. for (i = 0; i < NUM_FABRIC_SERDES; i++) {
  1995. rcv_addr = fabric_serdes_addrs[dd->hfi1_id][i];
  1996. sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
  1997. /* wait for interrupt to be processed */
  1998. usleep_range(10000, 11000);
  1999. fabric_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
  2000. if (i > 0 && fabric_vers[0] != fabric_vers[i])
  2001. all_same = 0;
  2002. }
  2003. if (all_same) {
  2004. dd_dev_info(dd, "Fabric SerDes firmware version 0x%x\n",
  2005. fabric_vers[0]);
  2006. } else {
  2007. dd_dev_warn(dd, "Fabric SerDes do not have the same firmware version\n");
  2008. for (i = 0; i < NUM_FABRIC_SERDES; i++) {
  2009. dd_dev_info(dd,
  2010. "Fabric SerDes lane %d firmware version 0x%x\n",
  2011. i, fabric_vers[i]);
  2012. }
  2013. }
  2014. clear_sbus_fast_mode(dd);
  2015. release_chip_resource(dd, CR_SBUS);
  2016. }