firmware.c 65 KB

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