nvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVM helpers
  4. *
  5. * Copyright (C) 2020, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #include <linux/idr.h>
  9. #include <linux/slab.h>
  10. #include <linux/vmalloc.h>
  11. #include "tb.h"
  12. #define NVM_MIN_SIZE SZ_32K
  13. #define NVM_MAX_SIZE SZ_1M
  14. #define NVM_DATA_DWORDS 16
  15. /* Intel specific NVM offsets */
  16. #define INTEL_NVM_DEVID 0x05
  17. #define INTEL_NVM_VERSION 0x08
  18. #define INTEL_NVM_CSS 0x10
  19. #define INTEL_NVM_FLASH_SIZE 0x45
  20. /* ASMedia specific NVM offsets */
  21. #define ASMEDIA_NVM_DATE 0x1c
  22. #define ASMEDIA_NVM_VERSION 0x28
  23. static DEFINE_IDA(nvm_ida);
  24. /**
  25. * struct tb_nvm_vendor_ops - Vendor specific NVM operations
  26. * @read_version: Reads out NVM version from the flash
  27. * @validate: Validates the NVM image before update (optional)
  28. * @write_headers: Writes headers before the rest of the image (optional)
  29. */
  30. struct tb_nvm_vendor_ops {
  31. int (*read_version)(struct tb_nvm *nvm);
  32. int (*validate)(struct tb_nvm *nvm);
  33. int (*write_headers)(struct tb_nvm *nvm);
  34. };
  35. /**
  36. * struct tb_nvm_vendor - Vendor to &struct tb_nvm_vendor_ops mapping
  37. * @vendor: Vendor ID
  38. * @vops: Vendor specific NVM operations
  39. *
  40. * Maps vendor ID to NVM vendor operations. If there is no mapping then
  41. * NVM firmware upgrade is disabled for the device.
  42. */
  43. struct tb_nvm_vendor {
  44. u16 vendor;
  45. const struct tb_nvm_vendor_ops *vops;
  46. };
  47. static int intel_switch_nvm_version(struct tb_nvm *nvm)
  48. {
  49. struct tb_switch *sw = tb_to_switch(nvm->dev);
  50. u32 val, nvm_size, hdr_size;
  51. int ret;
  52. /*
  53. * If the switch is in safe-mode the only accessible portion of
  54. * the NVM is the non-active one where userspace is expected to
  55. * write new functional NVM.
  56. */
  57. if (sw->safe_mode)
  58. return 0;
  59. ret = tb_switch_nvm_read(sw, INTEL_NVM_FLASH_SIZE, &val, sizeof(val));
  60. if (ret)
  61. return ret;
  62. hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
  63. nvm_size = (SZ_1M << (val & 7)) / 8;
  64. nvm_size = (nvm_size - hdr_size) / 2;
  65. ret = tb_switch_nvm_read(sw, INTEL_NVM_VERSION, &val, sizeof(val));
  66. if (ret)
  67. return ret;
  68. nvm->major = (val >> 16) & 0xff;
  69. nvm->minor = (val >> 8) & 0xff;
  70. nvm->active_size = nvm_size;
  71. return 0;
  72. }
  73. static int intel_switch_nvm_validate(struct tb_nvm *nvm)
  74. {
  75. struct tb_switch *sw = tb_to_switch(nvm->dev);
  76. unsigned int image_size, hdr_size;
  77. u16 ds_size, device_id;
  78. u8 *buf = nvm->buf;
  79. image_size = nvm->buf_data_size;
  80. /*
  81. * FARB pointer must point inside the image and must at least
  82. * contain parts of the digital section we will be reading here.
  83. */
  84. hdr_size = (*(u32 *)buf) & 0xffffff;
  85. if (hdr_size + INTEL_NVM_DEVID + 2 >= image_size)
  86. return -EINVAL;
  87. /* Digital section start should be aligned to 4k page */
  88. if (!IS_ALIGNED(hdr_size, SZ_4K))
  89. return -EINVAL;
  90. /*
  91. * Read digital section size and check that it also fits inside
  92. * the image.
  93. */
  94. ds_size = *(u16 *)(buf + hdr_size);
  95. if (ds_size >= image_size)
  96. return -EINVAL;
  97. if (sw->safe_mode)
  98. return 0;
  99. /*
  100. * Make sure the device ID in the image matches the one
  101. * we read from the switch config space.
  102. */
  103. device_id = *(u16 *)(buf + hdr_size + INTEL_NVM_DEVID);
  104. if (device_id != sw->config.device_id)
  105. return -EINVAL;
  106. /* Skip headers in the image */
  107. nvm->buf_data_start = buf + hdr_size;
  108. nvm->buf_data_size = image_size - hdr_size;
  109. return 0;
  110. }
  111. static int intel_switch_nvm_write_headers(struct tb_nvm *nvm)
  112. {
  113. struct tb_switch *sw = tb_to_switch(nvm->dev);
  114. if (sw->generation < 3) {
  115. int ret;
  116. /* Write CSS headers first */
  117. ret = dma_port_flash_write(sw->dma_port,
  118. DMA_PORT_CSS_ADDRESS, nvm->buf + INTEL_NVM_CSS,
  119. DMA_PORT_CSS_MAX_SIZE);
  120. if (ret)
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static const struct tb_nvm_vendor_ops intel_switch_nvm_ops = {
  126. .read_version = intel_switch_nvm_version,
  127. .validate = intel_switch_nvm_validate,
  128. .write_headers = intel_switch_nvm_write_headers,
  129. };
  130. static int asmedia_switch_nvm_version(struct tb_nvm *nvm)
  131. {
  132. struct tb_switch *sw = tb_to_switch(nvm->dev);
  133. u32 val;
  134. int ret;
  135. ret = tb_switch_nvm_read(sw, ASMEDIA_NVM_VERSION, &val, sizeof(val));
  136. if (ret)
  137. return ret;
  138. nvm->major = (val << 16) & 0xff0000;
  139. nvm->major |= val & 0x00ff00;
  140. nvm->major |= (val >> 16) & 0x0000ff;
  141. ret = tb_switch_nvm_read(sw, ASMEDIA_NVM_DATE, &val, sizeof(val));
  142. if (ret)
  143. return ret;
  144. nvm->minor = (val << 16) & 0xff0000;
  145. nvm->minor |= val & 0x00ff00;
  146. nvm->minor |= (val >> 16) & 0x0000ff;
  147. /* ASMedia NVM size is fixed to 512k */
  148. nvm->active_size = SZ_512K;
  149. return 0;
  150. }
  151. static const struct tb_nvm_vendor_ops asmedia_switch_nvm_ops = {
  152. .read_version = asmedia_switch_nvm_version,
  153. };
  154. /* Router vendor NVM support table */
  155. static const struct tb_nvm_vendor switch_nvm_vendors[] = {
  156. { 0x174c, &asmedia_switch_nvm_ops },
  157. { PCI_VENDOR_ID_INTEL, &intel_switch_nvm_ops },
  158. { 0x8087, &intel_switch_nvm_ops },
  159. };
  160. static int intel_retimer_nvm_version(struct tb_nvm *nvm)
  161. {
  162. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  163. u32 val, nvm_size;
  164. int ret;
  165. ret = tb_retimer_nvm_read(rt, INTEL_NVM_VERSION, &val, sizeof(val));
  166. if (ret)
  167. return ret;
  168. nvm->major = (val >> 16) & 0xff;
  169. nvm->minor = (val >> 8) & 0xff;
  170. ret = tb_retimer_nvm_read(rt, INTEL_NVM_FLASH_SIZE, &val, sizeof(val));
  171. if (ret)
  172. return ret;
  173. nvm_size = (SZ_1M << (val & 7)) / 8;
  174. nvm_size = (nvm_size - SZ_16K) / 2;
  175. nvm->active_size = nvm_size;
  176. return 0;
  177. }
  178. static int intel_retimer_nvm_validate(struct tb_nvm *nvm)
  179. {
  180. struct tb_retimer *rt = tb_to_retimer(nvm->dev);
  181. unsigned int image_size, hdr_size;
  182. u8 *buf = nvm->buf;
  183. u16 ds_size, device;
  184. image_size = nvm->buf_data_size;
  185. /*
  186. * FARB pointer must point inside the image and must at least
  187. * contain parts of the digital section we will be reading here.
  188. */
  189. hdr_size = (*(u32 *)buf) & 0xffffff;
  190. if (hdr_size + INTEL_NVM_DEVID + 2 >= image_size)
  191. return -EINVAL;
  192. /* Digital section start should be aligned to 4k page */
  193. if (!IS_ALIGNED(hdr_size, SZ_4K))
  194. return -EINVAL;
  195. /*
  196. * Read digital section size and check that it also fits inside
  197. * the image.
  198. */
  199. ds_size = *(u16 *)(buf + hdr_size);
  200. if (ds_size >= image_size)
  201. return -EINVAL;
  202. /*
  203. * Make sure the device ID in the image matches the retimer
  204. * hardware.
  205. */
  206. device = *(u16 *)(buf + hdr_size + INTEL_NVM_DEVID);
  207. if (device != rt->device)
  208. return -EINVAL;
  209. /* Skip headers in the image */
  210. nvm->buf_data_start = buf + hdr_size;
  211. nvm->buf_data_size = image_size - hdr_size;
  212. return 0;
  213. }
  214. static const struct tb_nvm_vendor_ops intel_retimer_nvm_ops = {
  215. .read_version = intel_retimer_nvm_version,
  216. .validate = intel_retimer_nvm_validate,
  217. };
  218. /* Retimer vendor NVM support table */
  219. static const struct tb_nvm_vendor retimer_nvm_vendors[] = {
  220. { 0x8087, &intel_retimer_nvm_ops },
  221. };
  222. /**
  223. * tb_nvm_alloc() - Allocate new NVM structure
  224. * @dev: Device owning the NVM
  225. *
  226. * Allocates new NVM structure with unique @id and returns it.
  227. *
  228. * Return:
  229. * * Pointer to &struct tb_nvm - On success.
  230. * * %-EOPNOTSUPP - If the NVM format of the @dev is not known by the
  231. * kernel.
  232. * * %ERR_PTR - In case of failure.
  233. */
  234. struct tb_nvm *tb_nvm_alloc(struct device *dev)
  235. {
  236. const struct tb_nvm_vendor_ops *vops = NULL;
  237. struct tb_nvm *nvm;
  238. int ret, i;
  239. if (tb_is_switch(dev)) {
  240. const struct tb_switch *sw = tb_to_switch(dev);
  241. for (i = 0; i < ARRAY_SIZE(switch_nvm_vendors); i++) {
  242. const struct tb_nvm_vendor *v = &switch_nvm_vendors[i];
  243. if (v->vendor == sw->config.vendor_id) {
  244. vops = v->vops;
  245. break;
  246. }
  247. }
  248. if (!vops) {
  249. tb_sw_dbg(sw, "router NVM format of vendor %#x unknown\n",
  250. sw->config.vendor_id);
  251. return ERR_PTR(-EOPNOTSUPP);
  252. }
  253. } else if (tb_is_retimer(dev)) {
  254. const struct tb_retimer *rt = tb_to_retimer(dev);
  255. for (i = 0; i < ARRAY_SIZE(retimer_nvm_vendors); i++) {
  256. const struct tb_nvm_vendor *v = &retimer_nvm_vendors[i];
  257. if (v->vendor == rt->vendor) {
  258. vops = v->vops;
  259. break;
  260. }
  261. }
  262. if (!vops) {
  263. dev_dbg(dev, "retimer NVM format of vendor %#x unknown\n",
  264. rt->vendor);
  265. return ERR_PTR(-EOPNOTSUPP);
  266. }
  267. } else {
  268. return ERR_PTR(-EOPNOTSUPP);
  269. }
  270. nvm = kzalloc_obj(*nvm);
  271. if (!nvm)
  272. return ERR_PTR(-ENOMEM);
  273. ret = ida_alloc(&nvm_ida, GFP_KERNEL);
  274. if (ret < 0) {
  275. kfree(nvm);
  276. return ERR_PTR(ret);
  277. }
  278. nvm->id = ret;
  279. nvm->dev = dev;
  280. nvm->vops = vops;
  281. return nvm;
  282. }
  283. /**
  284. * tb_nvm_read_version() - Read and populate NVM version
  285. * @nvm: NVM structure
  286. *
  287. * Uses vendor specific means to read and fill out the existing
  288. * active NVM version.
  289. *
  290. * Return: %0 on success, negative errno otherwise.
  291. */
  292. int tb_nvm_read_version(struct tb_nvm *nvm)
  293. {
  294. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  295. if (vops && vops->read_version)
  296. return vops->read_version(nvm);
  297. return -EOPNOTSUPP;
  298. }
  299. /**
  300. * tb_nvm_validate() - Validate new NVM image
  301. * @nvm: NVM structure
  302. *
  303. * Runs vendor specific validation over the new NVM image. As a
  304. * side effect, updates @nvm->buf_data_start and @nvm->buf_data_size
  305. * fields to match the actual data to be written to the NVM.
  306. *
  307. * Return: %0 on successful validation, negative errno otherwise.
  308. */
  309. int tb_nvm_validate(struct tb_nvm *nvm)
  310. {
  311. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  312. unsigned int image_size;
  313. u8 *buf = nvm->buf;
  314. if (!buf)
  315. return -EINVAL;
  316. if (!vops)
  317. return -EOPNOTSUPP;
  318. /* Just do basic image size checks */
  319. image_size = nvm->buf_data_size;
  320. if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
  321. return -EINVAL;
  322. /*
  323. * Set the default data start in the buffer. The validate method
  324. * below can change this if needed.
  325. */
  326. nvm->buf_data_start = buf;
  327. return vops->validate ? vops->validate(nvm) : 0;
  328. }
  329. /**
  330. * tb_nvm_write_headers() - Write headers before the rest of the image
  331. * @nvm: NVM structure
  332. *
  333. * If the vendor NVM format requires writing headers before the rest of
  334. * the image, this function does that. Can be called even if the device
  335. * does not need this.
  336. *
  337. * Return: %0 on success, negative errno otherwise.
  338. */
  339. int tb_nvm_write_headers(struct tb_nvm *nvm)
  340. {
  341. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  342. return vops->write_headers ? vops->write_headers(nvm) : 0;
  343. }
  344. /**
  345. * tb_nvm_add_active() - Adds active NVMem device to NVM
  346. * @nvm: NVM structure
  347. * @reg_read: Pointer to the function to read the NVM (passed directly to the
  348. * NVMem device)
  349. *
  350. * Registers new active NVmem device for @nvm. The @reg_read is called
  351. * directly from NVMem so it must handle possible concurrent access if
  352. * needed. The first parameter passed to @reg_read is @nvm structure.
  353. *
  354. * Return: %0 on success, negative errno otherwise.
  355. */
  356. int tb_nvm_add_active(struct tb_nvm *nvm, nvmem_reg_read_t reg_read)
  357. {
  358. struct nvmem_config config;
  359. struct nvmem_device *nvmem;
  360. memset(&config, 0, sizeof(config));
  361. config.name = "nvm_active";
  362. config.reg_read = reg_read;
  363. config.read_only = true;
  364. config.id = nvm->id;
  365. config.stride = 4;
  366. config.word_size = 4;
  367. config.size = nvm->active_size;
  368. config.dev = nvm->dev;
  369. config.owner = THIS_MODULE;
  370. config.priv = nvm;
  371. nvmem = nvmem_register(&config);
  372. if (IS_ERR(nvmem))
  373. return PTR_ERR(nvmem);
  374. nvm->active = nvmem;
  375. return 0;
  376. }
  377. /**
  378. * tb_nvm_write_buf() - Write data to @nvm buffer
  379. * @nvm: NVM structure
  380. * @offset: Offset where to write the data
  381. * @val: Data buffer to write
  382. * @bytes: Number of bytes to write
  383. *
  384. * Helper function to cache the new NVM image before it is actually
  385. * written to the flash. Copies @bytes from @val to @nvm->buf starting
  386. * from @offset.
  387. *
  388. * Return:
  389. * * %0 - On success.
  390. * * %-ENOMEM - If buffer allocation failed.
  391. * * Negative errno - Another error occurred.
  392. */
  393. int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
  394. size_t bytes)
  395. {
  396. if (!nvm->buf) {
  397. nvm->buf = vmalloc(NVM_MAX_SIZE);
  398. if (!nvm->buf)
  399. return -ENOMEM;
  400. }
  401. nvm->flushed = false;
  402. nvm->buf_data_size = offset + bytes;
  403. memcpy(nvm->buf + offset, val, bytes);
  404. return 0;
  405. }
  406. /**
  407. * tb_nvm_add_non_active() - Adds non-active NVMem device to NVM
  408. * @nvm: NVM structure
  409. * @reg_write: Pointer to the function to write the NVM (passed directly
  410. * to the NVMem device)
  411. *
  412. * Registers new non-active NVmem device for @nvm. The @reg_write is called
  413. * directly from NVMem so it must handle possible concurrent access if
  414. * needed. The first parameter passed to @reg_write is @nvm structure.
  415. * The size of the NVMem device is set to %NVM_MAX_SIZE.
  416. *
  417. * Return: %0 on success, negative errno otherwise.
  418. */
  419. int tb_nvm_add_non_active(struct tb_nvm *nvm, nvmem_reg_write_t reg_write)
  420. {
  421. struct nvmem_config config;
  422. struct nvmem_device *nvmem;
  423. memset(&config, 0, sizeof(config));
  424. config.name = "nvm_non_active";
  425. config.reg_write = reg_write;
  426. config.root_only = true;
  427. config.id = nvm->id;
  428. config.stride = 4;
  429. config.word_size = 4;
  430. config.size = NVM_MAX_SIZE;
  431. config.dev = nvm->dev;
  432. config.owner = THIS_MODULE;
  433. config.priv = nvm;
  434. nvmem = nvmem_register(&config);
  435. if (IS_ERR(nvmem))
  436. return PTR_ERR(nvmem);
  437. nvm->non_active = nvmem;
  438. return 0;
  439. }
  440. /**
  441. * tb_nvm_free() - Release NVM and its resources
  442. * @nvm: NVM structure to release
  443. *
  444. * Releases NVM and the NVMem devices if they were registered.
  445. */
  446. void tb_nvm_free(struct tb_nvm *nvm)
  447. {
  448. if (nvm) {
  449. nvmem_unregister(nvm->non_active);
  450. nvmem_unregister(nvm->active);
  451. vfree(nvm->buf);
  452. ida_free(&nvm_ida, nvm->id);
  453. }
  454. kfree(nvm);
  455. }
  456. /**
  457. * tb_nvm_read_data() - Read data from NVM
  458. * @address: Start address on the flash
  459. * @buf: Buffer where the read data is copied
  460. * @size: Size of the buffer in bytes
  461. * @retries: Number of retries if block read fails
  462. * @read_block: Function that reads block from the flash
  463. * @read_block_data: Data passsed to @read_block
  464. *
  465. * This is a generic function that reads data from NVM or NVM like
  466. * device.
  467. *
  468. * Return: %0 on success, negative errno otherwise.
  469. */
  470. int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
  471. unsigned int retries, read_block_fn read_block,
  472. void *read_block_data)
  473. {
  474. do {
  475. unsigned int dwaddress, dwords, offset;
  476. u8 data[NVM_DATA_DWORDS * 4];
  477. size_t nbytes;
  478. int ret;
  479. offset = address & 3;
  480. nbytes = min_t(size_t, size + offset, NVM_DATA_DWORDS * 4);
  481. dwaddress = address / 4;
  482. dwords = ALIGN(nbytes, 4) / 4;
  483. ret = read_block(read_block_data, dwaddress, data, dwords);
  484. if (ret) {
  485. if (ret != -ENODEV && retries--)
  486. continue;
  487. return ret;
  488. }
  489. nbytes -= offset;
  490. memcpy(buf, data + offset, nbytes);
  491. size -= nbytes;
  492. address += nbytes;
  493. buf += nbytes;
  494. } while (size > 0);
  495. return 0;
  496. }
  497. /**
  498. * tb_nvm_write_data() - Write data to NVM
  499. * @address: Start address on the flash
  500. * @buf: Buffer where the data is copied from
  501. * @size: Size of the buffer in bytes
  502. * @retries: Number of retries if the block write fails
  503. * @write_block: Function that writes block to the flash
  504. * @write_block_data: Data passed to @write_block
  505. *
  506. * This is generic function that writes data to NVM or NVM like device.
  507. *
  508. * Return: %0 on success, negative errno otherwise.
  509. */
  510. int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
  511. unsigned int retries, write_block_fn write_block,
  512. void *write_block_data)
  513. {
  514. do {
  515. unsigned int offset, dwaddress;
  516. u8 data[NVM_DATA_DWORDS * 4];
  517. size_t nbytes;
  518. int ret;
  519. offset = address & 3;
  520. nbytes = min_t(u32, size + offset, NVM_DATA_DWORDS * 4);
  521. memcpy(data + offset, buf, nbytes);
  522. dwaddress = address / 4;
  523. ret = write_block(write_block_data, dwaddress, data, nbytes / 4);
  524. if (ret) {
  525. if (ret == -ETIMEDOUT) {
  526. if (retries--)
  527. continue;
  528. ret = -EIO;
  529. }
  530. return ret;
  531. }
  532. size -= nbytes;
  533. address += nbytes;
  534. buf += nbytes;
  535. } while (size > 0);
  536. return 0;
  537. }
  538. void tb_nvm_exit(void)
  539. {
  540. ida_destroy(&nvm_ida);
  541. }