firmware.c 60 KB

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