firmware.c 64 KB

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