nvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. In case
  227. * of error returns ERR_PTR(). Specifically returns %-EOPNOTSUPP if the
  228. * NVM format of the @dev is not known by the kernel.
  229. */
  230. struct tb_nvm *tb_nvm_alloc(struct device *dev)
  231. {
  232. const struct tb_nvm_vendor_ops *vops = NULL;
  233. struct tb_nvm *nvm;
  234. int ret, i;
  235. if (tb_is_switch(dev)) {
  236. const struct tb_switch *sw = tb_to_switch(dev);
  237. for (i = 0; i < ARRAY_SIZE(switch_nvm_vendors); i++) {
  238. const struct tb_nvm_vendor *v = &switch_nvm_vendors[i];
  239. if (v->vendor == sw->config.vendor_id) {
  240. vops = v->vops;
  241. break;
  242. }
  243. }
  244. if (!vops) {
  245. tb_sw_dbg(sw, "router NVM format of vendor %#x unknown\n",
  246. sw->config.vendor_id);
  247. return ERR_PTR(-EOPNOTSUPP);
  248. }
  249. } else if (tb_is_retimer(dev)) {
  250. const struct tb_retimer *rt = tb_to_retimer(dev);
  251. for (i = 0; i < ARRAY_SIZE(retimer_nvm_vendors); i++) {
  252. const struct tb_nvm_vendor *v = &retimer_nvm_vendors[i];
  253. if (v->vendor == rt->vendor) {
  254. vops = v->vops;
  255. break;
  256. }
  257. }
  258. if (!vops) {
  259. dev_dbg(dev, "retimer NVM format of vendor %#x unknown\n",
  260. rt->vendor);
  261. return ERR_PTR(-EOPNOTSUPP);
  262. }
  263. } else {
  264. return ERR_PTR(-EOPNOTSUPP);
  265. }
  266. nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
  267. if (!nvm)
  268. return ERR_PTR(-ENOMEM);
  269. ret = ida_alloc(&nvm_ida, GFP_KERNEL);
  270. if (ret < 0) {
  271. kfree(nvm);
  272. return ERR_PTR(ret);
  273. }
  274. nvm->id = ret;
  275. nvm->dev = dev;
  276. nvm->vops = vops;
  277. return nvm;
  278. }
  279. /**
  280. * tb_nvm_read_version() - Read and populate NVM version
  281. * @nvm: NVM structure
  282. *
  283. * Uses vendor specific means to read out and fill in the existing
  284. * active NVM version. Returns %0 in case of success and negative errno
  285. * otherwise.
  286. */
  287. int tb_nvm_read_version(struct tb_nvm *nvm)
  288. {
  289. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  290. if (vops && vops->read_version)
  291. return vops->read_version(nvm);
  292. return -EOPNOTSUPP;
  293. }
  294. /**
  295. * tb_nvm_validate() - Validate new NVM image
  296. * @nvm: NVM structure
  297. *
  298. * Runs vendor specific validation over the new NVM image and if all
  299. * checks pass returns %0. As side effect updates @nvm->buf_data_start
  300. * and @nvm->buf_data_size fields to match the actual data to be written
  301. * to the NVM.
  302. *
  303. * If the validation does not pass then returns negative errno.
  304. */
  305. int tb_nvm_validate(struct tb_nvm *nvm)
  306. {
  307. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  308. unsigned int image_size;
  309. u8 *buf = nvm->buf;
  310. if (!buf)
  311. return -EINVAL;
  312. if (!vops)
  313. return -EOPNOTSUPP;
  314. /* Just do basic image size checks */
  315. image_size = nvm->buf_data_size;
  316. if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
  317. return -EINVAL;
  318. /*
  319. * Set the default data start in the buffer. The validate method
  320. * below can change this if needed.
  321. */
  322. nvm->buf_data_start = buf;
  323. return vops->validate ? vops->validate(nvm) : 0;
  324. }
  325. /**
  326. * tb_nvm_write_headers() - Write headers before the rest of the image
  327. * @nvm: NVM structure
  328. *
  329. * If the vendor NVM format requires writing headers before the rest of
  330. * the image, this function does that. Can be called even if the device
  331. * does not need this.
  332. *
  333. * Returns %0 in case of success and negative errno otherwise.
  334. */
  335. int tb_nvm_write_headers(struct tb_nvm *nvm)
  336. {
  337. const struct tb_nvm_vendor_ops *vops = nvm->vops;
  338. return vops->write_headers ? vops->write_headers(nvm) : 0;
  339. }
  340. /**
  341. * tb_nvm_add_active() - Adds active NVMem device to NVM
  342. * @nvm: NVM structure
  343. * @reg_read: Pointer to the function to read the NVM (passed directly to the
  344. * NVMem device)
  345. *
  346. * Registers new active NVmem device for @nvm. The @reg_read is called
  347. * directly from NVMem so it must handle possible concurrent access if
  348. * needed. The first parameter passed to @reg_read is @nvm structure.
  349. * Returns %0 in success and negative errno otherwise.
  350. */
  351. int tb_nvm_add_active(struct tb_nvm *nvm, nvmem_reg_read_t reg_read)
  352. {
  353. struct nvmem_config config;
  354. struct nvmem_device *nvmem;
  355. memset(&config, 0, sizeof(config));
  356. config.name = "nvm_active";
  357. config.reg_read = reg_read;
  358. config.read_only = true;
  359. config.id = nvm->id;
  360. config.stride = 4;
  361. config.word_size = 4;
  362. config.size = nvm->active_size;
  363. config.dev = nvm->dev;
  364. config.owner = THIS_MODULE;
  365. config.priv = nvm;
  366. nvmem = nvmem_register(&config);
  367. if (IS_ERR(nvmem))
  368. return PTR_ERR(nvmem);
  369. nvm->active = nvmem;
  370. return 0;
  371. }
  372. /**
  373. * tb_nvm_write_buf() - Write data to @nvm buffer
  374. * @nvm: NVM structure
  375. * @offset: Offset where to write the data
  376. * @val: Data buffer to write
  377. * @bytes: Number of bytes to write
  378. *
  379. * Helper function to cache the new NVM image before it is actually
  380. * written to the flash. Copies @bytes from @val to @nvm->buf starting
  381. * from @offset.
  382. */
  383. int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
  384. size_t bytes)
  385. {
  386. if (!nvm->buf) {
  387. nvm->buf = vmalloc(NVM_MAX_SIZE);
  388. if (!nvm->buf)
  389. return -ENOMEM;
  390. }
  391. nvm->flushed = false;
  392. nvm->buf_data_size = offset + bytes;
  393. memcpy(nvm->buf + offset, val, bytes);
  394. return 0;
  395. }
  396. /**
  397. * tb_nvm_add_non_active() - Adds non-active NVMem device to NVM
  398. * @nvm: NVM structure
  399. * @reg_write: Pointer to the function to write the NVM (passed directly
  400. * to the NVMem device)
  401. *
  402. * Registers new non-active NVmem device for @nvm. The @reg_write is called
  403. * directly from NVMem so it must handle possible concurrent access if
  404. * needed. The first parameter passed to @reg_write is @nvm structure.
  405. * The size of the NVMem device is set to %NVM_MAX_SIZE.
  406. *
  407. * Returns %0 in success and negative errno otherwise.
  408. */
  409. int tb_nvm_add_non_active(struct tb_nvm *nvm, nvmem_reg_write_t reg_write)
  410. {
  411. struct nvmem_config config;
  412. struct nvmem_device *nvmem;
  413. memset(&config, 0, sizeof(config));
  414. config.name = "nvm_non_active";
  415. config.reg_write = reg_write;
  416. config.root_only = true;
  417. config.id = nvm->id;
  418. config.stride = 4;
  419. config.word_size = 4;
  420. config.size = NVM_MAX_SIZE;
  421. config.dev = nvm->dev;
  422. config.owner = THIS_MODULE;
  423. config.priv = nvm;
  424. nvmem = nvmem_register(&config);
  425. if (IS_ERR(nvmem))
  426. return PTR_ERR(nvmem);
  427. nvm->non_active = nvmem;
  428. return 0;
  429. }
  430. /**
  431. * tb_nvm_free() - Release NVM and its resources
  432. * @nvm: NVM structure to release
  433. *
  434. * Releases NVM and the NVMem devices if they were registered.
  435. */
  436. void tb_nvm_free(struct tb_nvm *nvm)
  437. {
  438. if (nvm) {
  439. nvmem_unregister(nvm->non_active);
  440. nvmem_unregister(nvm->active);
  441. vfree(nvm->buf);
  442. ida_free(&nvm_ida, nvm->id);
  443. }
  444. kfree(nvm);
  445. }
  446. /**
  447. * tb_nvm_read_data() - Read data from NVM
  448. * @address: Start address on the flash
  449. * @buf: Buffer where the read data is copied
  450. * @size: Size of the buffer in bytes
  451. * @retries: Number of retries if block read fails
  452. * @read_block: Function that reads block from the flash
  453. * @read_block_data: Data passsed to @read_block
  454. *
  455. * This is a generic function that reads data from NVM or NVM like
  456. * device.
  457. *
  458. * Returns %0 on success and negative errno otherwise.
  459. */
  460. int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
  461. unsigned int retries, read_block_fn read_block,
  462. void *read_block_data)
  463. {
  464. do {
  465. unsigned int dwaddress, dwords, offset;
  466. u8 data[NVM_DATA_DWORDS * 4];
  467. size_t nbytes;
  468. int ret;
  469. offset = address & 3;
  470. nbytes = min_t(size_t, size + offset, NVM_DATA_DWORDS * 4);
  471. dwaddress = address / 4;
  472. dwords = ALIGN(nbytes, 4) / 4;
  473. ret = read_block(read_block_data, dwaddress, data, dwords);
  474. if (ret) {
  475. if (ret != -ENODEV && retries--)
  476. continue;
  477. return ret;
  478. }
  479. nbytes -= offset;
  480. memcpy(buf, data + offset, nbytes);
  481. size -= nbytes;
  482. address += nbytes;
  483. buf += nbytes;
  484. } while (size > 0);
  485. return 0;
  486. }
  487. /**
  488. * tb_nvm_write_data() - Write data to NVM
  489. * @address: Start address on the flash
  490. * @buf: Buffer where the data is copied from
  491. * @size: Size of the buffer in bytes
  492. * @retries: Number of retries if the block write fails
  493. * @write_block: Function that writes block to the flash
  494. * @write_block_data: Data passwd to @write_block
  495. *
  496. * This is generic function that writes data to NVM or NVM like device.
  497. *
  498. * Returns %0 on success and negative errno otherwise.
  499. */
  500. int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
  501. unsigned int retries, write_block_fn write_block,
  502. void *write_block_data)
  503. {
  504. do {
  505. unsigned int offset, dwaddress;
  506. u8 data[NVM_DATA_DWORDS * 4];
  507. size_t nbytes;
  508. int ret;
  509. offset = address & 3;
  510. nbytes = min_t(u32, size + offset, NVM_DATA_DWORDS * 4);
  511. memcpy(data + offset, buf, nbytes);
  512. dwaddress = address / 4;
  513. ret = write_block(write_block_data, dwaddress, data, nbytes / 4);
  514. if (ret) {
  515. if (ret == -ETIMEDOUT) {
  516. if (retries--)
  517. continue;
  518. ret = -EIO;
  519. }
  520. return ret;
  521. }
  522. size -= nbytes;
  523. address += nbytes;
  524. buf += nbytes;
  525. } while (size > 0);
  526. return 0;
  527. }
  528. void tb_nvm_exit(void)
  529. {
  530. ida_destroy(&nvm_ida);
  531. }