octeon_console.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. /**
  19. * @file octeon_console.c
  20. */
  21. #include <linux/moduleparam.h>
  22. #include <linux/pci.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/crc32.h>
  25. #include "liquidio_common.h"
  26. #include "octeon_droq.h"
  27. #include "octeon_iq.h"
  28. #include "response_manager.h"
  29. #include "octeon_device.h"
  30. #include "liquidio_image.h"
  31. #include "octeon_mem_ops.h"
  32. static void octeon_remote_lock(void);
  33. static void octeon_remote_unlock(void);
  34. static u64 cvmx_bootmem_phy_named_block_find(struct octeon_device *oct,
  35. const char *name,
  36. u32 flags);
  37. static int octeon_console_read(struct octeon_device *oct, u32 console_num,
  38. char *buffer, u32 buf_size);
  39. #define BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR 0x0006c008
  40. #define BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR 0x0006c004
  41. #define BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR 0x0006c000
  42. #define BOOTLOADER_PCI_READ_DESC_ADDR 0x0006c100
  43. #define BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN 248
  44. #define OCTEON_PCI_IO_BUF_OWNER_OCTEON 0x00000001
  45. #define OCTEON_PCI_IO_BUF_OWNER_HOST 0x00000002
  46. /** Can change without breaking ABI */
  47. #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64
  48. /** minimum alignment of bootmem alloced blocks */
  49. #define CVMX_BOOTMEM_ALIGNMENT_SIZE (16ull)
  50. /** CVMX bootmem descriptor major version */
  51. #define CVMX_BOOTMEM_DESC_MAJ_VER 3
  52. /* CVMX bootmem descriptor minor version */
  53. #define CVMX_BOOTMEM_DESC_MIN_VER 0
  54. /* Current versions */
  55. #define OCTEON_PCI_CONSOLE_MAJOR_VERSION 1
  56. #define OCTEON_PCI_CONSOLE_MINOR_VERSION 0
  57. #define OCTEON_PCI_CONSOLE_BLOCK_NAME "__pci_console"
  58. #define OCTEON_CONSOLE_POLL_INTERVAL_MS 100 /* 10 times per second */
  59. /* First three members of cvmx_bootmem_desc are left in original
  60. * positions for backwards compatibility.
  61. * Assumes big endian target
  62. */
  63. struct cvmx_bootmem_desc {
  64. /** spinlock to control access to list */
  65. u32 lock;
  66. /** flags for indicating various conditions */
  67. u32 flags;
  68. u64 head_addr;
  69. /** incremented changed when incompatible changes made */
  70. u32 major_version;
  71. /** incremented changed when compatible changes made,
  72. * reset to zero when major incremented
  73. */
  74. u32 minor_version;
  75. u64 app_data_addr;
  76. u64 app_data_size;
  77. /** number of elements in named blocks array */
  78. u32 nb_num_blocks;
  79. /** length of name array in bootmem blocks */
  80. u32 named_block_name_len;
  81. /** address of named memory block descriptors */
  82. u64 named_block_array_addr;
  83. };
  84. /* Structure that defines a single console.
  85. *
  86. * Note: when read_index == write_index, the buffer is empty.
  87. * The actual usable size of each console is console_buf_size -1;
  88. */
  89. struct octeon_pci_console {
  90. u64 input_base_addr;
  91. u32 input_read_index;
  92. u32 input_write_index;
  93. u64 output_base_addr;
  94. u32 output_read_index;
  95. u32 output_write_index;
  96. u32 lock;
  97. u32 buf_size;
  98. };
  99. /* This is the main container structure that contains all the information
  100. * about all PCI consoles. The address of this structure is passed to various
  101. * routines that operation on PCI consoles.
  102. */
  103. struct octeon_pci_console_desc {
  104. u32 major_version;
  105. u32 minor_version;
  106. u32 lock;
  107. u32 flags;
  108. u32 num_consoles;
  109. u32 pad;
  110. /* must be 64 bit aligned here... */
  111. /* Array of addresses of octeon_pci_console structures */
  112. u64 console_addr_array[0];
  113. /* Implicit storage for console_addr_array */
  114. };
  115. /**
  116. * This function is the implementation of the get macros defined
  117. * for individual structure members. The argument are generated
  118. * by the macros inorder to read only the needed memory.
  119. *
  120. * @param oct Pointer to current octeon device
  121. * @param base 64bit physical address of the complete structure
  122. * @param offset Offset from the beginning of the structure to the member being
  123. * accessed.
  124. * @param size Size of the structure member.
  125. *
  126. * @return Value of the structure member promoted into a u64.
  127. */
  128. static inline u64 __cvmx_bootmem_desc_get(struct octeon_device *oct,
  129. u64 base,
  130. u32 offset,
  131. u32 size)
  132. {
  133. base = (1ull << 63) | (base + offset);
  134. switch (size) {
  135. case 4:
  136. return octeon_read_device_mem32(oct, base);
  137. case 8:
  138. return octeon_read_device_mem64(oct, base);
  139. default:
  140. return 0;
  141. }
  142. }
  143. /**
  144. * This function retrieves the string name of a named block. It is
  145. * more complicated than a simple memcpy() since the named block
  146. * descriptor may not be directly accessible.
  147. *
  148. * @param addr Physical address of the named block descriptor
  149. * @param str String to receive the named block string name
  150. * @param len Length of the string buffer, which must match the length
  151. * stored in the bootmem descriptor.
  152. */
  153. static void CVMX_BOOTMEM_NAMED_GET_NAME(struct octeon_device *oct,
  154. u64 addr,
  155. char *str,
  156. u32 len)
  157. {
  158. addr += offsetof(struct cvmx_bootmem_named_block_desc, name);
  159. octeon_pci_read_core_mem(oct, addr, (u8 *)str, len);
  160. str[len] = 0;
  161. }
  162. /* See header file for descriptions of functions */
  163. /**
  164. * Check the version information on the bootmem descriptor
  165. *
  166. * @param exact_match
  167. * Exact major version to check against. A zero means
  168. * check that the version supports named blocks.
  169. *
  170. * @return Zero if the version is correct. Negative if the version is
  171. * incorrect. Failures also cause a message to be displayed.
  172. */
  173. static int __cvmx_bootmem_check_version(struct octeon_device *oct,
  174. u32 exact_match)
  175. {
  176. u32 major_version;
  177. u32 minor_version;
  178. if (!oct->bootmem_desc_addr)
  179. oct->bootmem_desc_addr =
  180. octeon_read_device_mem64(oct,
  181. BOOTLOADER_PCI_READ_DESC_ADDR);
  182. major_version = (u32)__cvmx_bootmem_desc_get(
  183. oct, oct->bootmem_desc_addr,
  184. offsetof(struct cvmx_bootmem_desc, major_version),
  185. FIELD_SIZEOF(struct cvmx_bootmem_desc, major_version));
  186. minor_version = (u32)__cvmx_bootmem_desc_get(
  187. oct, oct->bootmem_desc_addr,
  188. offsetof(struct cvmx_bootmem_desc, minor_version),
  189. FIELD_SIZEOF(struct cvmx_bootmem_desc, minor_version));
  190. dev_dbg(&oct->pci_dev->dev, "%s: major_version=%d\n", __func__,
  191. major_version);
  192. if ((major_version > 3) ||
  193. (exact_match && major_version != exact_match)) {
  194. dev_err(&oct->pci_dev->dev, "bootmem ver mismatch %d.%d addr:0x%llx\n",
  195. major_version, minor_version,
  196. (long long)oct->bootmem_desc_addr);
  197. return -1;
  198. } else {
  199. return 0;
  200. }
  201. }
  202. static const struct cvmx_bootmem_named_block_desc
  203. *__cvmx_bootmem_find_named_block_flags(struct octeon_device *oct,
  204. const char *name, u32 flags)
  205. {
  206. struct cvmx_bootmem_named_block_desc *desc =
  207. &oct->bootmem_named_block_desc;
  208. u64 named_addr = cvmx_bootmem_phy_named_block_find(oct, name, flags);
  209. if (named_addr) {
  210. desc->base_addr = __cvmx_bootmem_desc_get(
  211. oct, named_addr,
  212. offsetof(struct cvmx_bootmem_named_block_desc,
  213. base_addr),
  214. FIELD_SIZEOF(
  215. struct cvmx_bootmem_named_block_desc,
  216. base_addr));
  217. desc->size = __cvmx_bootmem_desc_get(oct, named_addr,
  218. offsetof(struct cvmx_bootmem_named_block_desc,
  219. size),
  220. FIELD_SIZEOF(
  221. struct cvmx_bootmem_named_block_desc,
  222. size));
  223. strncpy(desc->name, name, sizeof(desc->name));
  224. desc->name[sizeof(desc->name) - 1] = 0;
  225. return &oct->bootmem_named_block_desc;
  226. } else {
  227. return NULL;
  228. }
  229. }
  230. static u64 cvmx_bootmem_phy_named_block_find(struct octeon_device *oct,
  231. const char *name,
  232. u32 flags)
  233. {
  234. u64 result = 0;
  235. if (!__cvmx_bootmem_check_version(oct, 3)) {
  236. u32 i;
  237. u64 named_block_array_addr = __cvmx_bootmem_desc_get(
  238. oct, oct->bootmem_desc_addr,
  239. offsetof(struct cvmx_bootmem_desc,
  240. named_block_array_addr),
  241. FIELD_SIZEOF(struct cvmx_bootmem_desc,
  242. named_block_array_addr));
  243. u32 num_blocks = (u32)__cvmx_bootmem_desc_get(
  244. oct, oct->bootmem_desc_addr,
  245. offsetof(struct cvmx_bootmem_desc,
  246. nb_num_blocks),
  247. FIELD_SIZEOF(struct cvmx_bootmem_desc,
  248. nb_num_blocks));
  249. u32 name_length = (u32)__cvmx_bootmem_desc_get(
  250. oct, oct->bootmem_desc_addr,
  251. offsetof(struct cvmx_bootmem_desc,
  252. named_block_name_len),
  253. FIELD_SIZEOF(struct cvmx_bootmem_desc,
  254. named_block_name_len));
  255. u64 named_addr = named_block_array_addr;
  256. for (i = 0; i < num_blocks; i++) {
  257. u64 named_size = __cvmx_bootmem_desc_get(
  258. oct, named_addr,
  259. offsetof(
  260. struct cvmx_bootmem_named_block_desc,
  261. size),
  262. FIELD_SIZEOF(
  263. struct cvmx_bootmem_named_block_desc,
  264. size));
  265. if (name && named_size) {
  266. char *name_tmp =
  267. kmalloc(name_length + 1, GFP_KERNEL);
  268. if (!name_tmp)
  269. break;
  270. CVMX_BOOTMEM_NAMED_GET_NAME(oct, named_addr,
  271. name_tmp,
  272. name_length);
  273. if (!strncmp(name, name_tmp, name_length)) {
  274. result = named_addr;
  275. kfree(name_tmp);
  276. break;
  277. }
  278. kfree(name_tmp);
  279. } else if (!name && !named_size) {
  280. result = named_addr;
  281. break;
  282. }
  283. named_addr +=
  284. sizeof(struct cvmx_bootmem_named_block_desc);
  285. }
  286. }
  287. return result;
  288. }
  289. /**
  290. * Find a named block on the remote Octeon
  291. *
  292. * @param name Name of block to find
  293. * @param base_addr Address the block is at (OUTPUT)
  294. * @param size The size of the block (OUTPUT)
  295. *
  296. * @return Zero on success, One on failure.
  297. */
  298. static int octeon_named_block_find(struct octeon_device *oct, const char *name,
  299. u64 *base_addr, u64 *size)
  300. {
  301. const struct cvmx_bootmem_named_block_desc *named_block;
  302. octeon_remote_lock();
  303. named_block = __cvmx_bootmem_find_named_block_flags(oct, name, 0);
  304. octeon_remote_unlock();
  305. if (named_block) {
  306. *base_addr = named_block->base_addr;
  307. *size = named_block->size;
  308. return 0;
  309. }
  310. return 1;
  311. }
  312. static void octeon_remote_lock(void)
  313. {
  314. /* fill this in if any sharing is needed */
  315. }
  316. static void octeon_remote_unlock(void)
  317. {
  318. /* fill this in if any sharing is needed */
  319. }
  320. int octeon_console_send_cmd(struct octeon_device *oct, char *cmd_str,
  321. u32 wait_hundredths)
  322. {
  323. u32 len = (u32)strlen(cmd_str);
  324. dev_dbg(&oct->pci_dev->dev, "sending \"%s\" to bootloader\n", cmd_str);
  325. if (len > BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN - 1) {
  326. dev_err(&oct->pci_dev->dev, "Command string too long, max length is: %d\n",
  327. BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN - 1);
  328. return -1;
  329. }
  330. if (octeon_wait_for_bootloader(oct, wait_hundredths) != 0) {
  331. dev_err(&oct->pci_dev->dev, "Bootloader not ready for command.\n");
  332. return -1;
  333. }
  334. /* Write command to bootloader */
  335. octeon_remote_lock();
  336. octeon_pci_write_core_mem(oct, BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR,
  337. (u8 *)cmd_str, len);
  338. octeon_write_device_mem32(oct, BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR,
  339. len);
  340. octeon_write_device_mem32(oct, BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR,
  341. OCTEON_PCI_IO_BUF_OWNER_OCTEON);
  342. /* Bootloader should accept command very quickly
  343. * if it really was ready
  344. */
  345. if (octeon_wait_for_bootloader(oct, 200) != 0) {
  346. octeon_remote_unlock();
  347. dev_err(&oct->pci_dev->dev, "Bootloader did not accept command.\n");
  348. return -1;
  349. }
  350. octeon_remote_unlock();
  351. return 0;
  352. }
  353. int octeon_wait_for_bootloader(struct octeon_device *oct,
  354. u32 wait_time_hundredths)
  355. {
  356. dev_dbg(&oct->pci_dev->dev, "waiting %d0 ms for bootloader\n",
  357. wait_time_hundredths);
  358. if (octeon_mem_access_ok(oct))
  359. return -1;
  360. while (wait_time_hundredths > 0 &&
  361. octeon_read_device_mem32(oct,
  362. BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR)
  363. != OCTEON_PCI_IO_BUF_OWNER_HOST) {
  364. if (--wait_time_hundredths <= 0)
  365. return -1;
  366. schedule_timeout_uninterruptible(HZ / 100);
  367. }
  368. return 0;
  369. }
  370. static void octeon_console_handle_result(struct octeon_device *oct,
  371. size_t console_num)
  372. {
  373. struct octeon_console *console;
  374. console = &oct->console[console_num];
  375. console->waiting = 0;
  376. }
  377. static char console_buffer[OCTEON_CONSOLE_MAX_READ_BYTES];
  378. static void output_console_line(struct octeon_device *oct,
  379. struct octeon_console *console,
  380. size_t console_num,
  381. char *console_buffer,
  382. s32 bytes_read)
  383. {
  384. char *line;
  385. s32 i;
  386. size_t len;
  387. line = console_buffer;
  388. for (i = 0; i < bytes_read; i++) {
  389. /* Output a line at a time, prefixed */
  390. if (console_buffer[i] == '\n') {
  391. console_buffer[i] = '\0';
  392. /* We need to output 'line', prefaced by 'leftover'.
  393. * However, it is possible we're being called to
  394. * output 'leftover' by itself (in the case of nothing
  395. * having been read from the console).
  396. *
  397. * To avoid duplication, check for this condition.
  398. */
  399. if (console->leftover[0] &&
  400. (line != console->leftover)) {
  401. if (console->print)
  402. (*console->print)(oct, (u32)console_num,
  403. console->leftover,
  404. line);
  405. console->leftover[0] = '\0';
  406. } else {
  407. if (console->print)
  408. (*console->print)(oct, (u32)console_num,
  409. line, NULL);
  410. }
  411. line = &console_buffer[i + 1];
  412. }
  413. }
  414. /* Save off any leftovers */
  415. if (line != &console_buffer[bytes_read]) {
  416. console_buffer[bytes_read] = '\0';
  417. len = strlen(console->leftover);
  418. strncpy(&console->leftover[len], line,
  419. sizeof(console->leftover) - len);
  420. }
  421. }
  422. static void check_console(struct work_struct *work)
  423. {
  424. s32 bytes_read, tries, total_read;
  425. size_t len;
  426. struct octeon_console *console;
  427. struct cavium_wk *wk = (struct cavium_wk *)work;
  428. struct octeon_device *oct = (struct octeon_device *)wk->ctxptr;
  429. u32 console_num = (u32)wk->ctxul;
  430. u32 delay;
  431. console = &oct->console[console_num];
  432. tries = 0;
  433. total_read = 0;
  434. do {
  435. /* Take console output regardless of whether it will
  436. * be logged
  437. */
  438. bytes_read =
  439. octeon_console_read(oct, console_num, console_buffer,
  440. sizeof(console_buffer) - 1);
  441. if (bytes_read > 0) {
  442. total_read += bytes_read;
  443. if (console->waiting)
  444. octeon_console_handle_result(oct, console_num);
  445. if (console->print) {
  446. output_console_line(oct, console, console_num,
  447. console_buffer, bytes_read);
  448. }
  449. } else if (bytes_read < 0) {
  450. dev_err(&oct->pci_dev->dev, "Error reading console %u, ret=%d\n",
  451. console_num, bytes_read);
  452. }
  453. tries++;
  454. } while ((bytes_read > 0) && (tries < 16));
  455. /* If nothing is read after polling the console,
  456. * output any leftovers if any
  457. */
  458. if (console->print && (total_read == 0) &&
  459. (console->leftover[0])) {
  460. /* append '\n' as terminator for 'output_console_line' */
  461. len = strlen(console->leftover);
  462. console->leftover[len] = '\n';
  463. output_console_line(oct, console, console_num,
  464. console->leftover, (s32)(len + 1));
  465. console->leftover[0] = '\0';
  466. }
  467. delay = OCTEON_CONSOLE_POLL_INTERVAL_MS;
  468. schedule_delayed_work(&wk->work, msecs_to_jiffies(delay));
  469. }
  470. int octeon_init_consoles(struct octeon_device *oct)
  471. {
  472. int ret = 0;
  473. u64 addr, size;
  474. ret = octeon_mem_access_ok(oct);
  475. if (ret) {
  476. dev_err(&oct->pci_dev->dev, "Memory access not okay'\n");
  477. return ret;
  478. }
  479. ret = octeon_named_block_find(oct, OCTEON_PCI_CONSOLE_BLOCK_NAME, &addr,
  480. &size);
  481. if (ret) {
  482. dev_err(&oct->pci_dev->dev, "Could not find console '%s'\n",
  483. OCTEON_PCI_CONSOLE_BLOCK_NAME);
  484. return ret;
  485. }
  486. /* Dedicate one of Octeon's BAR1 index registers to create a static
  487. * mapping to a region of Octeon DRAM that contains the PCI console
  488. * named block.
  489. */
  490. oct->console_nb_info.bar1_index = BAR1_INDEX_STATIC_MAP;
  491. oct->fn_list.bar1_idx_setup(oct, addr, oct->console_nb_info.bar1_index,
  492. true);
  493. oct->console_nb_info.dram_region_base = addr
  494. & ~(OCTEON_BAR1_ENTRY_SIZE - 1ULL);
  495. /* num_consoles > 0, is an indication that the consoles
  496. * are accessible
  497. */
  498. oct->num_consoles = octeon_read_device_mem32(oct,
  499. addr + offsetof(struct octeon_pci_console_desc,
  500. num_consoles));
  501. oct->console_desc_addr = addr;
  502. dev_dbg(&oct->pci_dev->dev, "Initialized consoles. %d available\n",
  503. oct->num_consoles);
  504. return ret;
  505. }
  506. static void octeon_get_uboot_version(struct octeon_device *oct)
  507. {
  508. s32 bytes_read, tries, total_read;
  509. struct octeon_console *console;
  510. u32 console_num = 0;
  511. char *uboot_ver;
  512. char *buf;
  513. char *p;
  514. #define OCTEON_UBOOT_VER_BUF_SIZE 512
  515. buf = kmalloc(OCTEON_UBOOT_VER_BUF_SIZE, GFP_KERNEL);
  516. if (!buf)
  517. return;
  518. if (octeon_console_send_cmd(oct, "setenv stdout pci\n", 50)) {
  519. kfree(buf);
  520. return;
  521. }
  522. if (octeon_console_send_cmd(oct, "version\n", 1)) {
  523. kfree(buf);
  524. return;
  525. }
  526. console = &oct->console[console_num];
  527. tries = 0;
  528. total_read = 0;
  529. do {
  530. /* Take console output regardless of whether it will
  531. * be logged
  532. */
  533. bytes_read =
  534. octeon_console_read(oct,
  535. console_num, buf + total_read,
  536. OCTEON_UBOOT_VER_BUF_SIZE - 1 -
  537. total_read);
  538. if (bytes_read > 0) {
  539. buf[bytes_read] = '\0';
  540. total_read += bytes_read;
  541. if (console->waiting)
  542. octeon_console_handle_result(oct, console_num);
  543. } else if (bytes_read < 0) {
  544. dev_err(&oct->pci_dev->dev, "Error reading console %u, ret=%d\n",
  545. console_num, bytes_read);
  546. }
  547. tries++;
  548. } while ((bytes_read > 0) && (tries < 16));
  549. /* If nothing is read after polling the console,
  550. * output any leftovers if any
  551. */
  552. if ((total_read == 0) && (console->leftover[0])) {
  553. dev_dbg(&oct->pci_dev->dev, "%u: %s\n",
  554. console_num, console->leftover);
  555. console->leftover[0] = '\0';
  556. }
  557. buf[OCTEON_UBOOT_VER_BUF_SIZE - 1] = '\0';
  558. uboot_ver = strstr(buf, "U-Boot");
  559. if (uboot_ver) {
  560. p = strstr(uboot_ver, "mips");
  561. if (p) {
  562. p--;
  563. *p = '\0';
  564. dev_info(&oct->pci_dev->dev, "%s\n", uboot_ver);
  565. }
  566. }
  567. kfree(buf);
  568. octeon_console_send_cmd(oct, "setenv stdout serial\n", 50);
  569. }
  570. int octeon_add_console(struct octeon_device *oct, u32 console_num,
  571. char *dbg_enb)
  572. {
  573. int ret = 0;
  574. u32 delay;
  575. u64 coreaddr;
  576. struct delayed_work *work;
  577. struct octeon_console *console;
  578. if (console_num >= oct->num_consoles) {
  579. dev_err(&oct->pci_dev->dev,
  580. "trying to read from console number %d when only 0 to %d exist\n",
  581. console_num, oct->num_consoles);
  582. } else {
  583. console = &oct->console[console_num];
  584. console->waiting = 0;
  585. coreaddr = oct->console_desc_addr + console_num * 8 +
  586. offsetof(struct octeon_pci_console_desc,
  587. console_addr_array);
  588. console->addr = octeon_read_device_mem64(oct, coreaddr);
  589. coreaddr = console->addr + offsetof(struct octeon_pci_console,
  590. buf_size);
  591. console->buffer_size = octeon_read_device_mem32(oct, coreaddr);
  592. coreaddr = console->addr + offsetof(struct octeon_pci_console,
  593. input_base_addr);
  594. console->input_base_addr =
  595. octeon_read_device_mem64(oct, coreaddr);
  596. coreaddr = console->addr + offsetof(struct octeon_pci_console,
  597. output_base_addr);
  598. console->output_base_addr =
  599. octeon_read_device_mem64(oct, coreaddr);
  600. console->leftover[0] = '\0';
  601. work = &oct->console_poll_work[console_num].work;
  602. octeon_get_uboot_version(oct);
  603. INIT_DELAYED_WORK(work, check_console);
  604. oct->console_poll_work[console_num].ctxptr = (void *)oct;
  605. oct->console_poll_work[console_num].ctxul = console_num;
  606. delay = OCTEON_CONSOLE_POLL_INTERVAL_MS;
  607. schedule_delayed_work(work, msecs_to_jiffies(delay));
  608. /* an empty string means use default debug console enablement */
  609. if (dbg_enb && !dbg_enb[0])
  610. dbg_enb = "setenv pci_console_active 1";
  611. if (dbg_enb)
  612. ret = octeon_console_send_cmd(oct, dbg_enb, 2000);
  613. console->active = 1;
  614. }
  615. return ret;
  616. }
  617. /**
  618. * Removes all consoles
  619. *
  620. * @param oct octeon device
  621. */
  622. void octeon_remove_consoles(struct octeon_device *oct)
  623. {
  624. u32 i;
  625. struct octeon_console *console;
  626. for (i = 0; i < oct->num_consoles; i++) {
  627. console = &oct->console[i];
  628. if (!console->active)
  629. continue;
  630. cancel_delayed_work_sync(&oct->console_poll_work[i].
  631. work);
  632. console->addr = 0;
  633. console->buffer_size = 0;
  634. console->input_base_addr = 0;
  635. console->output_base_addr = 0;
  636. }
  637. oct->num_consoles = 0;
  638. }
  639. static inline int octeon_console_free_bytes(u32 buffer_size,
  640. u32 wr_idx,
  641. u32 rd_idx)
  642. {
  643. if (rd_idx >= buffer_size || wr_idx >= buffer_size)
  644. return -1;
  645. return ((buffer_size - 1) - (wr_idx - rd_idx)) % buffer_size;
  646. }
  647. static inline int octeon_console_avail_bytes(u32 buffer_size,
  648. u32 wr_idx,
  649. u32 rd_idx)
  650. {
  651. if (rd_idx >= buffer_size || wr_idx >= buffer_size)
  652. return -1;
  653. return buffer_size - 1 -
  654. octeon_console_free_bytes(buffer_size, wr_idx, rd_idx);
  655. }
  656. static int octeon_console_read(struct octeon_device *oct, u32 console_num,
  657. char *buffer, u32 buf_size)
  658. {
  659. int bytes_to_read;
  660. u32 rd_idx, wr_idx;
  661. struct octeon_console *console;
  662. if (console_num >= oct->num_consoles) {
  663. dev_err(&oct->pci_dev->dev, "Attempted to read from disabled console %d\n",
  664. console_num);
  665. return 0;
  666. }
  667. console = &oct->console[console_num];
  668. /* Check to see if any data is available.
  669. * Maybe optimize this with 64-bit read.
  670. */
  671. rd_idx = octeon_read_device_mem32(oct, console->addr +
  672. offsetof(struct octeon_pci_console, output_read_index));
  673. wr_idx = octeon_read_device_mem32(oct, console->addr +
  674. offsetof(struct octeon_pci_console, output_write_index));
  675. bytes_to_read = octeon_console_avail_bytes(console->buffer_size,
  676. wr_idx, rd_idx);
  677. if (bytes_to_read <= 0)
  678. return bytes_to_read;
  679. bytes_to_read = min_t(s32, bytes_to_read, buf_size);
  680. /* Check to see if what we want to read is not contiguous, and limit
  681. * ourselves to the contiguous block
  682. */
  683. if (rd_idx + bytes_to_read >= console->buffer_size)
  684. bytes_to_read = console->buffer_size - rd_idx;
  685. octeon_pci_read_core_mem(oct, console->output_base_addr + rd_idx,
  686. (u8 *)buffer, bytes_to_read);
  687. octeon_write_device_mem32(oct, console->addr +
  688. offsetof(struct octeon_pci_console,
  689. output_read_index),
  690. (rd_idx + bytes_to_read) %
  691. console->buffer_size);
  692. return bytes_to_read;
  693. }
  694. #define FBUF_SIZE (4 * 1024 * 1024)
  695. #define MAX_BOOTTIME_SIZE 80
  696. int octeon_download_firmware(struct octeon_device *oct, const u8 *data,
  697. size_t size)
  698. {
  699. struct octeon_firmware_file_header *h;
  700. char boottime[MAX_BOOTTIME_SIZE];
  701. struct timespec64 ts;
  702. u32 crc32_result;
  703. u64 load_addr;
  704. u32 image_len;
  705. int ret = 0;
  706. u32 i, rem;
  707. if (size < sizeof(struct octeon_firmware_file_header)) {
  708. dev_err(&oct->pci_dev->dev, "Firmware file too small (%d < %d).\n",
  709. (u32)size,
  710. (u32)sizeof(struct octeon_firmware_file_header));
  711. return -EINVAL;
  712. }
  713. h = (struct octeon_firmware_file_header *)data;
  714. if (be32_to_cpu(h->magic) != LIO_NIC_MAGIC) {
  715. dev_err(&oct->pci_dev->dev, "Unrecognized firmware file.\n");
  716. return -EINVAL;
  717. }
  718. crc32_result = crc32((unsigned int)~0, data,
  719. sizeof(struct octeon_firmware_file_header) -
  720. sizeof(u32)) ^ ~0U;
  721. if (crc32_result != be32_to_cpu(h->crc32)) {
  722. dev_err(&oct->pci_dev->dev, "Firmware CRC mismatch (0x%08x != 0x%08x).\n",
  723. crc32_result, be32_to_cpu(h->crc32));
  724. return -EINVAL;
  725. }
  726. if (strncmp(LIQUIDIO_PACKAGE, h->version, strlen(LIQUIDIO_PACKAGE))) {
  727. dev_err(&oct->pci_dev->dev, "Unmatched firmware package type. Expected %s, got %s.\n",
  728. LIQUIDIO_PACKAGE, h->version);
  729. return -EINVAL;
  730. }
  731. if (memcmp(LIQUIDIO_BASE_VERSION, h->version + strlen(LIQUIDIO_PACKAGE),
  732. strlen(LIQUIDIO_BASE_VERSION))) {
  733. dev_err(&oct->pci_dev->dev, "Unmatched firmware version. Expected %s.x, got %s.\n",
  734. LIQUIDIO_BASE_VERSION,
  735. h->version + strlen(LIQUIDIO_PACKAGE));
  736. return -EINVAL;
  737. }
  738. if (be32_to_cpu(h->num_images) > LIO_MAX_IMAGES) {
  739. dev_err(&oct->pci_dev->dev, "Too many images in firmware file (%d).\n",
  740. be32_to_cpu(h->num_images));
  741. return -EINVAL;
  742. }
  743. dev_info(&oct->pci_dev->dev, "Firmware version: %s\n", h->version);
  744. snprintf(oct->fw_info.liquidio_firmware_version, 32, "LIQUIDIO: %s",
  745. h->version);
  746. data += sizeof(struct octeon_firmware_file_header);
  747. dev_info(&oct->pci_dev->dev, "%s: Loading %d images\n", __func__,
  748. be32_to_cpu(h->num_images));
  749. /* load all images */
  750. for (i = 0; i < be32_to_cpu(h->num_images); i++) {
  751. load_addr = be64_to_cpu(h->desc[i].addr);
  752. image_len = be32_to_cpu(h->desc[i].len);
  753. dev_info(&oct->pci_dev->dev, "Loading firmware %d at %llx\n",
  754. image_len, load_addr);
  755. /* Write in 4MB chunks*/
  756. rem = image_len;
  757. while (rem) {
  758. if (rem < FBUF_SIZE)
  759. size = rem;
  760. else
  761. size = FBUF_SIZE;
  762. /* download the image */
  763. octeon_pci_write_core_mem(oct, load_addr, data, (u32)size);
  764. data += size;
  765. rem -= (u32)size;
  766. load_addr += size;
  767. }
  768. }
  769. /* Pass date and time information to NIC at the time of loading
  770. * firmware and periodically update the host time to NIC firmware.
  771. * This is to make NIC firmware use the same time reference as Host,
  772. * so that it is easy to correlate logs from firmware and host for
  773. * debugging.
  774. *
  775. * Octeon always uses UTC time. so timezone information is not sent.
  776. */
  777. ktime_get_real_ts64(&ts);
  778. ret = snprintf(boottime, MAX_BOOTTIME_SIZE,
  779. " time_sec=%lld time_nsec=%ld",
  780. (s64)ts.tv_sec, ts.tv_nsec);
  781. if ((sizeof(h->bootcmd) - strnlen(h->bootcmd, sizeof(h->bootcmd))) <
  782. ret) {
  783. dev_err(&oct->pci_dev->dev, "Boot command buffer too small\n");
  784. return -EINVAL;
  785. }
  786. strncat(h->bootcmd, boottime,
  787. sizeof(h->bootcmd) - strnlen(h->bootcmd, sizeof(h->bootcmd)));
  788. dev_info(&oct->pci_dev->dev, "Writing boot command: %s\n",
  789. h->bootcmd);
  790. /* Invoke the bootcmd */
  791. ret = octeon_console_send_cmd(oct, h->bootcmd, 50);
  792. if (ret)
  793. dev_info(&oct->pci_dev->dev, "Boot command send failed\n");
  794. return ret;
  795. }