octeon_console.c 23 KB

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