firmware.c 63 KB

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