octeon_console.c 22 KB

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