octeon_console.c 23 KB

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