xref: /freebsd/sys/dev/liquidio/base/lio_console.c (revision 069ac184)
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * @file lio_console.c
36  */
37 
38 #include "lio_bsd.h"
39 #include "lio_common.h"
40 #include "lio_droq.h"
41 #include "lio_iq.h"
42 #include "lio_response_manager.h"
43 #include "lio_device.h"
44 #include "lio_image.h"
45 #include "lio_mem_ops.h"
46 #include "lio_main.h"
47 
48 static void	lio_get_uboot_version(struct octeon_device *oct);
49 static void	lio_remote_lock(void);
50 static void	lio_remote_unlock(void);
51 static uint64_t	cvmx_bootmem_phy_named_block_find(struct octeon_device *oct,
52 						  const char *name,
53 						  uint32_t flags);
54 static int	lio_console_read(struct octeon_device *oct,
55 				 uint32_t console_num, char *buffer,
56 				 uint32_t buf_size);
57 
58 #define CAST_ULL(v)	((unsigned long long)(v))
59 
60 #define LIO_BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR	0x0006c008
61 #define LIO_BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR		0x0006c004
62 #define LIO_BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR	0x0006c000
63 #define LIO_BOOTLOADER_PCI_READ_DESC_ADDR		0x0006c100
64 #define LIO_BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN		248
65 
66 #define LIO_PCI_IO_BUF_OWNER_OCTEON	0x00000001
67 #define LIO_PCI_IO_BUF_OWNER_HOST	0x00000002
68 
69 #define LIO_PCI_CONSOLE_BLOCK_NAME	"__pci_console"
70 #define LIO_CONSOLE_POLL_INTERVAL_MS	100	/* 10 times per second */
71 
72 /*
73  * First three members of cvmx_bootmem_desc are left in original positions
74  * for backwards compatibility. Assumes big endian target
75  */
76 struct cvmx_bootmem_desc {
77 	/* lock to control access to list */
78 	uint32_t	lock;
79 
80 	/* flags for indicating various conditions */
81 	uint32_t	flags;
82 
83 	uint64_t	head_addr;
84 
85 	/* incremented changed when incompatible changes made */
86 	uint32_t	major_version;
87 
88 	/*
89 	 * incremented changed when compatible changes made, reset to zero
90 	 * when major incremented
91 	 */
92 	uint32_t	minor_version;
93 
94 	uint64_t	app_data_addr;
95 	uint64_t	app_data_size;
96 
97 	/* number of elements in named blocks array */
98 	uint32_t	nb_num_blocks;
99 
100 	/* length of name array in bootmem blocks */
101 	uint32_t	named_block_name_len;
102 
103 	/* address of named memory block descriptors */
104 	uint64_t	named_block_array_addr;
105 };
106 
107 /*
108  * Structure that defines a single console.
109  *
110  * Note: when read_index == write_index, the buffer is empty. The actual usable
111  * size of each console is console_buf_size -1;
112  */
113 struct lio_pci_console {
114 	uint64_t	input_base_addr;
115 	uint32_t	input_read_index;
116 	uint32_t	input_write_index;
117 	uint64_t	output_base_addr;
118 	uint32_t	output_read_index;
119 	uint32_t	output_write_index;
120 	uint32_t	lock;
121 	uint32_t	buf_size;
122 };
123 
124 /*
125  * This is the main container structure that contains all the information
126  * about all PCI consoles.  The address of this structure is passed to
127  * various routines that operation on PCI consoles.
128  */
129 struct lio_pci_console_desc {
130 	uint32_t	major_version;
131 	uint32_t	minor_version;
132 	uint32_t	lock;
133 	uint32_t	flags;
134 	uint32_t	num_consoles;
135 	uint32_t	pad;
136 	/* must be 64 bit aligned here... */
137 	/* Array of addresses of octeon_pci_console structures */
138 	uint64_t	console_addr_array[1];
139 	/* Implicit storage for console_addr_array */
140 };
141 
142 /*
143  * This macro returns the size of a member of a structure. Logically it is
144  * the same as "sizeof(s::field)" in C++, but C lacks the "::" operator.
145  */
146 #define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field)
147 /*
148  * This function is the implementation of the get macros defined
149  * for individual structure members. The argument are generated
150  * by the macros inorder to read only the needed memory.
151  *
152  * @param oct    Pointer to current octeon device
153  * @param base   64bit physical address of the complete structure
154  * @param offset Offset from the beginning of the structure to the member being
155  *		 accessed.
156  * @param size   Size of the structure member.
157  *
158  * @return Value of the structure member promoted into a uint64_t.
159  */
160 static inline uint64_t
161 __cvmx_bootmem_desc_get(struct octeon_device *oct, uint64_t base,
162 			uint32_t offset, uint32_t size)
163 {
164 
165 	base = (1ull << 63) | (base + offset);
166 	switch (size) {
167 	case 4:
168 		return (lio_read_device_mem32(oct, base));
169 	case 8:
170 		return (lio_read_device_mem64(oct, base));
171 	default:
172 		return (0);
173 	}
174 }
175 
176 /*
177  * This function retrieves the string name of a named block. It is
178  * more complicated than a simple memcpy() since the named block
179  * descriptor may not be directly accessible.
180  *
181  * @param oct    Pointer to current octeon device
182  * @param addr   Physical address of the named block descriptor
183  * @param str    String to receive the named block string name
184  * @param len    Length of the string buffer, which must match the length
185  *		 stored in the bootmem descriptor.
186  */
187 static void
188 lio_bootmem_named_get_name(struct octeon_device *oct, uint64_t addr, char *str,
189 			   uint32_t len)
190 {
191 
192 	addr += offsetof(struct cvmx_bootmem_named_block_desc, name);
193 	lio_pci_read_core_mem(oct, addr, (uint8_t *) str, len);
194 	str[len] = 0;
195 }
196 
197 /* See header file for descriptions of functions */
198 
199 /*
200  * Check the version information on the bootmem descriptor
201  *
202  * @param oct    Pointer to current octeon device
203  * @param exact_match
204  *		Exact major version to check against. A zero means
205  *		check that the version supports named blocks.
206  *
207  * @return Zero if the version is correct. Negative if the version is
208  *	   incorrect. Failures also cause a message to be displayed.
209  */
210 static int
211 __cvmx_bootmem_check_version(struct octeon_device *oct, uint32_t exact_match)
212 {
213 	uint32_t	major_version;
214 	uint32_t	minor_version;
215 
216 	if (!oct->bootmem_desc_addr)
217 		oct->bootmem_desc_addr =
218 			lio_read_device_mem64(oct,
219 					LIO_BOOTLOADER_PCI_READ_DESC_ADDR);
220 
221 	major_version = (uint32_t) __cvmx_bootmem_desc_get(oct,
222 			oct->bootmem_desc_addr,
223 			offsetof(struct cvmx_bootmem_desc, major_version),
224 			SIZEOF_FIELD(struct cvmx_bootmem_desc, major_version));
225 	minor_version = (uint32_t) __cvmx_bootmem_desc_get(oct,
226 			oct->bootmem_desc_addr,
227 			offsetof(struct cvmx_bootmem_desc, minor_version),
228 			SIZEOF_FIELD(struct cvmx_bootmem_desc, minor_version));
229 
230 	lio_dev_dbg(oct, "%s: major_version=%d\n", __func__, major_version);
231 	if ((major_version > 3) ||
232 	    (exact_match && major_version != exact_match)) {
233 		lio_dev_err(oct, "bootmem ver mismatch %d.%d addr:0x%llx\n",
234 			    major_version, minor_version,
235 			    CAST_ULL(oct->bootmem_desc_addr));
236 		return (-1);
237 	} else {
238 		return (0);
239 	}
240 }
241 
242 static const struct cvmx_bootmem_named_block_desc *
243 __cvmx_bootmem_find_named_block_flags(struct octeon_device *oct,
244 				      const char *name, uint32_t flags)
245 {
246 	struct cvmx_bootmem_named_block_desc	*desc =
247 		&oct->bootmem_named_block_desc;
248 	uint64_t	named_addr;
249 
250 	named_addr = cvmx_bootmem_phy_named_block_find(oct, name,
251 						       flags);
252 	if (named_addr) {
253 		desc->base_addr = __cvmx_bootmem_desc_get(oct, named_addr,
254 			offsetof(struct cvmx_bootmem_named_block_desc,
255 				 base_addr),
256 			SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc,
257 				     base_addr));
258 
259 		desc->size = __cvmx_bootmem_desc_get(oct, named_addr,
260 			 offsetof(struct cvmx_bootmem_named_block_desc, size),
261 			 SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc,
262 				      size));
263 
264 		strncpy(desc->name, name, sizeof(desc->name));
265 		desc->name[sizeof(desc->name) - 1] = 0;
266 
267 		return (&oct->bootmem_named_block_desc);
268 	} else {
269 		return (NULL);
270 	}
271 }
272 
273 static uint64_t
274 cvmx_bootmem_phy_named_block_find(struct octeon_device *oct, const char *name,
275 				  uint32_t flags)
276 {
277 	uint64_t	result = 0;
278 
279 	if (!__cvmx_bootmem_check_version(oct, 3)) {
280 		uint32_t i;
281 
282 		uint64_t named_block_array_addr =
283 			__cvmx_bootmem_desc_get(oct, oct->bootmem_desc_addr,
284 					offsetof(struct cvmx_bootmem_desc,
285 						 named_block_array_addr),
286 					SIZEOF_FIELD(struct cvmx_bootmem_desc,
287 						     named_block_array_addr));
288 		uint32_t num_blocks =
289 			(uint32_t) __cvmx_bootmem_desc_get(oct,
290 					oct->bootmem_desc_addr,
291 					offsetof(struct cvmx_bootmem_desc,
292 						 nb_num_blocks),
293 					SIZEOF_FIELD(struct cvmx_bootmem_desc,
294 						     nb_num_blocks));
295 
296 		uint32_t name_length =
297 			(uint32_t) __cvmx_bootmem_desc_get(oct,
298 					oct->bootmem_desc_addr,
299 					offsetof(struct cvmx_bootmem_desc,
300 						 named_block_name_len),
301 					SIZEOF_FIELD(struct cvmx_bootmem_desc,
302 						     named_block_name_len));
303 
304 		uint64_t named_addr = named_block_array_addr;
305 
306 		for (i = 0; i < num_blocks; i++) {
307 			uint64_t named_size =
308 			  __cvmx_bootmem_desc_get(oct, named_addr,
309 			    offsetof(struct cvmx_bootmem_named_block_desc,
310 				     size),
311 			    SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc,
312 					 size));
313 
314 			if (name && named_size) {
315 				char	*name_tmp = malloc(name_length + 1,
316 							   M_DEVBUF, M_NOWAIT |
317 							   M_ZERO);
318 				if (!name_tmp)
319 					break;
320 
321 				lio_bootmem_named_get_name(oct, named_addr,
322 							   name_tmp,
323 							   name_length);
324 
325 				if (!strncmp(name, name_tmp, name_length)) {
326 					result = named_addr;
327 					free(name_tmp, M_DEVBUF);
328 					break;
329 				}
330 
331 				free(name_tmp, M_DEVBUF);
332 
333 			} else if (!name && !named_size) {
334 				result = named_addr;
335 				break;
336 			}
337 
338 			named_addr +=
339 				sizeof(struct cvmx_bootmem_named_block_desc);
340 		}
341 	}
342 	return (result);
343 }
344 
345 /*
346  * Find a named block on the remote Octeon
347  *
348  * @param oct       Pointer to current octeon device
349  * @param name      Name of block to find
350  * @param base_addr Address the block is at (OUTPUT)
351  * @param size      The size of the block (OUTPUT)
352  *
353  * @return Zero on success, One on failure.
354  */
355 static int
356 lio_named_block_find(struct octeon_device *oct, const char *name,
357 		     uint64_t * base_addr, uint64_t * size)
358 {
359 	const struct cvmx_bootmem_named_block_desc	*named_block;
360 
361 	lio_remote_lock();
362 	named_block = __cvmx_bootmem_find_named_block_flags(oct, name, 0);
363 	lio_remote_unlock();
364 	if (named_block != NULL) {
365 		*base_addr = named_block->base_addr;
366 		*size = named_block->size;
367 		return (0);
368 	}
369 
370 	return (1);
371 }
372 
373 
374 static void
375 lio_remote_lock(void)
376 {
377 
378 	/* fill this in if any sharing is needed */
379 }
380 
381 static void
382 lio_remote_unlock(void)
383 {
384 
385 	/* fill this in if any sharing is needed */
386 }
387 
388 int
389 lio_console_send_cmd(struct octeon_device *oct, char *cmd_str,
390 		     uint32_t wait_hundredths)
391 {
392 	uint32_t	len = (uint32_t) strlen(cmd_str);
393 
394 	lio_dev_dbg(oct, "sending \"%s\" to bootloader\n", cmd_str);
395 
396 	if (len > LIO_BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN - 1) {
397 		lio_dev_err(oct, "Command string too long, max length is: %d\n",
398 			    LIO_BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN - 1);
399 		return (-1);
400 	}
401 
402 	if (lio_wait_for_bootloader(oct, wait_hundredths)) {
403 		lio_dev_err(oct, "Bootloader not ready for command.\n");
404 		return (-1);
405 	}
406 
407 	/* Write command to bootloader */
408 	lio_remote_lock();
409 	lio_pci_write_core_mem(oct, LIO_BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR,
410 			       (uint8_t *) cmd_str, len);
411 	lio_write_device_mem32(oct, LIO_BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR,
412 			       len);
413 	lio_write_device_mem32(oct, LIO_BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR,
414 			       LIO_PCI_IO_BUF_OWNER_OCTEON);
415 
416 	/*
417 	 * Bootloader should accept command very quickly if it really was
418 	 * ready
419 	 */
420 	if (lio_wait_for_bootloader(oct, 200)) {
421 		lio_remote_unlock();
422 		lio_dev_err(oct, "Bootloader did not accept command.\n");
423 		return (-1);
424 	}
425 
426 	lio_remote_unlock();
427 	return (0);
428 }
429 
430 int
431 lio_wait_for_bootloader(struct octeon_device *oct,
432 			uint32_t wait_time_hundredths)
433 {
434 	lio_dev_dbg(oct, "waiting %d0 ms for bootloader\n",
435 		    wait_time_hundredths);
436 
437 	if (lio_mem_access_ok(oct))
438 		return (-1);
439 
440 	while (wait_time_hundredths > 0 &&
441 	       lio_read_device_mem32(oct,
442 				LIO_BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR) !=
443 	       LIO_PCI_IO_BUF_OWNER_HOST) {
444 		if (--wait_time_hundredths <= 0)
445 			return (-1);
446 
447 		lio_sleep_timeout(10);
448 	}
449 
450 	return (0);
451 }
452 
453 static void
454 lio_console_handle_result(struct octeon_device *oct, size_t console_num)
455 {
456 	struct lio_console	*console;
457 
458 	console = &oct->console[console_num];
459 
460 	console->waiting = 0;
461 }
462 
463 static char	console_buffer[LIO_MAX_CONSOLE_READ_BYTES];
464 
465 static void
466 lio_output_console_line(struct octeon_device *oct, struct lio_console *console,
467 			size_t console_num, char *console_buffer,
468 			int32_t bytes_read)
469 {
470 	size_t		len;
471 	int32_t		i;
472 	char           *line;
473 
474 	line = console_buffer;
475 	for (i = 0; i < bytes_read; i++) {
476 		/* Output a line at a time, prefixed */
477 		if (console_buffer[i] == '\n') {
478 			console_buffer[i] = '\0';
479 			/* We need to output 'line', prefaced by 'leftover'.
480 			 * However, it is possible we're being called to
481 			 * output 'leftover' by itself (in the case of nothing
482 			 * having been read from the console).
483 			 *
484 			 * To avoid duplication, check for this condition.
485 			 */
486 			if (console->leftover[0] &&
487 			    (line != console->leftover)) {
488 				if (console->print)
489 					(*console->print)(oct,
490 							  (uint32_t)console_num,
491 							console->leftover,line);
492 				console->leftover[0] = '\0';
493 			} else {
494 				if (console->print)
495 					(*console->print)(oct,
496 							  (uint32_t)console_num,
497 							  line, NULL);
498 			}
499 
500 			line = &console_buffer[i + 1];
501 		}
502 	}
503 
504 	/* Save off any leftovers */
505 	if (line != &console_buffer[bytes_read]) {
506 		console_buffer[bytes_read] = '\0';
507 		len = strlen(console->leftover);
508 		strncpy(&console->leftover[len], line,
509 			sizeof(console->leftover) - len);
510 	}
511 }
512 
513 static void
514 lio_check_console(void *arg)
515 {
516 	struct lio_console *console;
517 	struct lio_callout *console_callout = arg;
518 	struct octeon_device *oct =
519 		(struct octeon_device *)console_callout->ctxptr;
520 	size_t		len;
521 	uint32_t	console_num = (uint32_t) console_callout->ctxul;
522 	int32_t		bytes_read, total_read, tries;
523 
524 	console = &oct->console[console_num];
525 	tries = 0;
526 	total_read = 0;
527 
528 	if (callout_pending(&console_callout->timer) ||
529 	    (callout_active(&console_callout->timer) == 0))
530 		return;
531 
532 	do {
533 		/*
534 		 * Take console output regardless of whether it will be
535 		 * logged
536 		 */
537 		bytes_read = lio_console_read(oct, console_num, console_buffer,
538 					      sizeof(console_buffer) - 1);
539 		if (bytes_read > 0) {
540 			total_read += bytes_read;
541 			if (console->waiting)
542 				lio_console_handle_result(oct, console_num);
543 
544 			if (console->print) {
545 				lio_output_console_line(oct, console,
546 							console_num,
547 							console_buffer,
548 							bytes_read);
549 			}
550 
551 		} else if (bytes_read < 0) {
552 			lio_dev_err(oct, "Error reading console %u, ret=%d\n",
553 				    console_num, bytes_read);
554 		}
555 
556 		tries++;
557 	} while ((bytes_read > 0) && (tries < 16));
558 
559 	/*
560 	 * If nothing is read after polling the console, output any leftovers
561 	 * if any
562 	 */
563 	if (console->print && (total_read == 0) && (console->leftover[0])) {
564 		/* append '\n' as terminator for 'output_console_line' */
565 		len = strlen(console->leftover);
566 		console->leftover[len] = '\n';
567 		lio_output_console_line(oct, console, console_num,
568 					console->leftover, (int32_t)(len + 1));
569 		console->leftover[0] = '\0';
570 	}
571 	callout_schedule(&oct->console_timer[console_num].timer,
572 			 lio_ms_to_ticks(LIO_CONSOLE_POLL_INTERVAL_MS));
573 }
574 
575 
576 int
577 lio_init_consoles(struct octeon_device *oct)
578 {
579 	uint64_t	addr, size;
580 	int		ret = 0;
581 
582 	ret = lio_mem_access_ok(oct);
583 	if (ret) {
584 		lio_dev_err(oct, "Memory access not okay'\n");
585 		return (ret);
586 	}
587 	ret = lio_named_block_find(oct, LIO_PCI_CONSOLE_BLOCK_NAME, &addr,
588 				   &size);
589 	if (ret) {
590 		lio_dev_err(oct, "Could not find console '%s'\n",
591 			    LIO_PCI_CONSOLE_BLOCK_NAME);
592 		return (ret);
593 	}
594 
595 	/*
596 	 * Use BAR1_INDEX15 to create a static mapping to a region of
597 	 * Octeon's DRAM that contains the PCI console named block.
598 	 */
599 	oct->console_nb_info.bar1_index = 15;
600 	oct->fn_list.bar1_idx_setup(oct, addr, oct->console_nb_info.bar1_index,
601 				    1);
602 	oct->console_nb_info.dram_region_base = addr & 0xFFFFFFFFFFC00000ULL;
603 
604 	/*
605 	 * num_consoles > 0, is an indication that the consoles are
606 	 * accessible
607 	 */
608 	oct->num_consoles = lio_read_device_mem32(oct,
609 				addr + offsetof(struct lio_pci_console_desc,
610 						num_consoles));
611 	oct->console_desc_addr = addr;
612 
613 	lio_dev_dbg(oct, "Initialized consoles. %d available\n",
614 		    oct->num_consoles);
615 
616 	return (ret);
617 }
618 
619 int
620 lio_add_console(struct octeon_device *oct, uint32_t console_num, char *dbg_enb)
621 {
622 	struct callout *timer;
623 	struct lio_console *console;
624 	uint64_t	coreaddr;
625 	int		ret = 0;
626 
627 	if (console_num >= oct->num_consoles) {
628 		lio_dev_err(oct, "trying to read from console number %d when only 0 to %d exist\n",
629 			    console_num, oct->num_consoles);
630 	} else {
631 		console = &oct->console[console_num];
632 
633 		console->waiting = 0;
634 
635 		coreaddr = oct->console_desc_addr + console_num * 8 +
636 			offsetof(struct lio_pci_console_desc,
637 				 console_addr_array);
638 		console->addr = lio_read_device_mem64(oct, coreaddr);
639 		coreaddr = console->addr + offsetof(struct lio_pci_console,
640 						    buf_size);
641 		console->buffer_size = lio_read_device_mem32(oct, coreaddr);
642 		coreaddr = console->addr + offsetof(struct lio_pci_console,
643 						    input_base_addr);
644 		console->input_base_addr = lio_read_device_mem64(oct, coreaddr);
645 		coreaddr = console->addr + offsetof(struct lio_pci_console,
646 						    output_base_addr);
647 		console->output_base_addr =
648 			lio_read_device_mem64(oct, coreaddr);
649 		console->leftover[0] = '\0';
650 
651 		timer = &oct->console_timer[console_num].timer;
652 
653 		if (oct->uboot_len == 0)
654 			lio_get_uboot_version(oct);
655 
656 		callout_init(timer, 0);
657 		oct->console_timer[console_num].ctxptr = (void *)oct;
658 		oct->console_timer[console_num].ctxul = console_num;
659 		callout_reset(timer,
660 			      lio_ms_to_ticks(LIO_CONSOLE_POLL_INTERVAL_MS),
661 			      lio_check_console, timer);
662 		/* an empty string means use default debug console enablement */
663 		if (dbg_enb && !dbg_enb[0])
664 			dbg_enb = "setenv pci_console_active 1";
665 
666 		if (dbg_enb)
667 			ret = lio_console_send_cmd(oct, dbg_enb, 2000);
668 
669 		console->active = 1;
670 	}
671 
672 	return (ret);
673 }
674 
675 /*
676  * Removes all consoles
677  *
678  * @param oct         octeon device
679  */
680 void
681 lio_remove_consoles(struct octeon_device *oct)
682 {
683 	struct lio_console	*console;
684 	uint32_t		i;
685 
686 	for (i = 0; i < oct->num_consoles; i++) {
687 		console = &oct->console[i];
688 
689 		if (!console->active)
690 			continue;
691 
692 		callout_stop(&oct->console_timer[i].timer);
693 		console->addr = 0;
694 		console->buffer_size = 0;
695 		console->input_base_addr = 0;
696 		console->output_base_addr = 0;
697 	}
698 
699 	oct->num_consoles = 0;
700 }
701 
702 static inline int
703 lio_console_free_bytes(uint32_t buffer_size, uint32_t wr_idx, uint32_t rd_idx)
704 {
705 
706 	if (rd_idx >= buffer_size || wr_idx >= buffer_size)
707 		return (-1);
708 
709 	return (((buffer_size - 1) - (wr_idx - rd_idx)) % buffer_size);
710 }
711 
712 static inline int
713 lio_console_avail_bytes(uint32_t buffer_size, uint32_t wr_idx, uint32_t rd_idx)
714 {
715 
716 	if (rd_idx >= buffer_size || wr_idx >= buffer_size)
717 		return (-1);
718 
719 	return (buffer_size - 1 -
720 		lio_console_free_bytes(buffer_size, wr_idx, rd_idx));
721 }
722 
723 static int
724 lio_console_read(struct octeon_device *oct, uint32_t console_num, char *buffer,
725 		 uint32_t buf_size)
726 {
727 	struct lio_console	*console;
728 	int			bytes_to_read;
729 	uint32_t		rd_idx, wr_idx;
730 
731 	if (console_num >= oct->num_consoles) {
732 		lio_dev_err(oct, "Attempted to read from disabled console %d\n",
733 			    console_num);
734 		return (0);
735 	}
736 
737 	console = &oct->console[console_num];
738 
739 	/*
740 	 * Check to see if any data is available. Maybe optimize this with
741 	 * 64-bit read.
742 	 */
743 	rd_idx = lio_read_device_mem32(oct, console->addr +
744 		       offsetof(struct lio_pci_console, output_read_index));
745 	wr_idx = lio_read_device_mem32(oct, console->addr +
746 		      offsetof(struct lio_pci_console, output_write_index));
747 
748 	bytes_to_read = lio_console_avail_bytes(console->buffer_size,
749 						wr_idx, rd_idx);
750 	if (bytes_to_read <= 0)
751 		return (bytes_to_read);
752 
753 	bytes_to_read = min(bytes_to_read, buf_size);
754 
755 	/*
756 	 * Check to see if what we want to read is not contiguous, and limit
757 	 * ourselves to the contiguous block
758 	 */
759 	if (rd_idx + bytes_to_read >= console->buffer_size)
760 		bytes_to_read = console->buffer_size - rd_idx;
761 
762 	lio_pci_read_core_mem(oct, console->output_base_addr + rd_idx,
763 			      (uint8_t *) buffer, bytes_to_read);
764 	lio_write_device_mem32(oct, console->addr +
765 			       offsetof(struct lio_pci_console,
766 					output_read_index),
767 			       (rd_idx + bytes_to_read) % console->buffer_size);
768 
769 	return (bytes_to_read);
770 }
771 
772 static void
773 lio_get_uboot_version(struct octeon_device *oct)
774 {
775 	struct lio_console *console;
776 	int32_t		bytes_read, total_read, tries;
777 	uint32_t	console_num = 0;
778 	int		i, ret __unused = 0;
779 
780 	ret = lio_console_send_cmd(oct, "setenv stdout pci", 50);
781 
782 	console = &oct->console[console_num];
783 	tries = 0;
784 	total_read = 0;
785 
786 	ret = lio_console_send_cmd(oct, "version", 1);
787 
788 	do {
789 		/*
790 		 * Take console output regardless of whether it will be
791 		 * logged
792 		 */
793 		bytes_read = lio_console_read(oct,
794 					      console_num, oct->uboot_version +
795 					      total_read,
796 					      OCTEON_UBOOT_BUFFER_SIZE - 1 -
797 					      total_read);
798 		if (bytes_read > 0) {
799 			oct->uboot_version[bytes_read] = 0x0;
800 
801 			total_read += bytes_read;
802 			if (console->waiting)
803 				lio_console_handle_result(oct, console_num);
804 
805 		} else if (bytes_read < 0) {
806 			lio_dev_err(oct, "Error reading console %u, ret=%d\n",
807 				    console_num, bytes_read);
808 		}
809 
810 		tries++;
811 	} while ((bytes_read > 0) && (tries < 16));
812 
813 	/*
814 	 * If nothing is read after polling the console, output any leftovers
815 	 * if any
816 	 */
817 	if ((total_read == 0) && (console->leftover[0])) {
818 		lio_dev_dbg(oct, "%u: %s\n", console_num, console->leftover);
819 		console->leftover[0] = '\0';
820 	}
821 
822 	ret = lio_console_send_cmd(oct, "setenv stdout serial", 50);
823 
824 	/* U-Boot */
825 	for (i = 0; i < (OCTEON_UBOOT_BUFFER_SIZE - 9); i++) {
826 		if (oct->uboot_version[i] == 'U' &&
827 		    oct->uboot_version[i + 2] == 'B' &&
828 		    oct->uboot_version[i + 3] == 'o' &&
829 		    oct->uboot_version[i + 4] == 'o' &&
830 		    oct->uboot_version[i + 5] == 't') {
831 			oct->uboot_sidx = i;
832 			i++;
833 			for (; oct->uboot_version[i] != 0x0; i++) {
834 				if (oct->uboot_version[i] == 'm' &&
835 				    oct->uboot_version[i + 1] == 'i' &&
836 				    oct->uboot_version[i + 2] == 'p' &&
837 				    oct->uboot_version[i + 3] == 's') {
838 					oct->uboot_eidx = i - 1;
839 					oct->uboot_version[i - 1] = 0x0;
840 					oct->uboot_len = oct->uboot_eidx -
841 						oct->uboot_sidx + 1;
842 					lio_dev_info(oct, "%s\n",
843 						     &oct->uboot_version
844 						     [oct->uboot_sidx]);
845 					return;
846 				}
847 			}
848 		}
849 	}
850 }
851 
852 
853 #define FBUF_SIZE	(4 * 1024 * 1024)
854 
855 int
856 lio_download_firmware(struct octeon_device *oct, const uint8_t * data,
857 		      size_t size)
858 {
859 	struct lio_firmware_file_header *h;
860 	uint64_t	load_addr;
861 	uint32_t	crc32_result, i, image_len, rem;
862 
863 	if (size < sizeof(struct lio_firmware_file_header)) {
864 		lio_dev_err(oct, "Firmware file too small (%d < %d).\n",
865 			    (uint32_t) size,
866 			    (uint32_t) sizeof(struct lio_firmware_file_header));
867 		return (-EINVAL);
868 	}
869 
870 	h = __DECONST(struct lio_firmware_file_header *, data);
871 
872 	if (be32toh(h->magic) != LIO_NIC_MAGIC) {
873 		lio_dev_err(oct, "Unrecognized firmware file.\n");
874 		return (-EINVAL);
875 	}
876 
877 	crc32_result = crc32(data, sizeof(struct lio_firmware_file_header) -
878 			     sizeof(uint32_t));
879 	if (crc32_result != be32toh(h->crc32)) {
880 		lio_dev_err(oct, "Firmware CRC mismatch (0x%08x != 0x%08x).\n",
881 			    crc32_result, be32toh(h->crc32));
882 		return (-EINVAL);
883 	}
884 
885 	if (memcmp(LIO_BASE_VERSION, h->version,
886 		   strlen(LIO_BASE_VERSION))) {
887 		lio_dev_err(oct, "Unmatched firmware version. Expected %s.x, got %s.\n",
888 			    LIO_BASE_VERSION, h->version);
889 		return (-EINVAL);
890 	}
891 
892 	if (be32toh(h->num_images) > LIO_MAX_IMAGES) {
893 		lio_dev_err(oct, "Too many images in firmware file (%d).\n",
894 			    be32toh(h->num_images));
895 		return (-EINVAL);
896 	}
897 
898 	lio_dev_info(oct, "Firmware version: %s\n", h->version);
899 	snprintf(oct->fw_info.lio_firmware_version, 32, "LIQUIDIO: %s",
900 		 h->version);
901 
902 	data += sizeof(struct lio_firmware_file_header);
903 
904 	lio_dev_info(oct, "Loading %d image(s)\n", be32toh(h->num_images));
905 
906 	/* load all images */
907 	for (i = 0; i < be32toh(h->num_images); i++) {
908 		load_addr = be64toh(h->desc[i].addr);
909 		image_len = be32toh(h->desc[i].len);
910 
911 		lio_dev_info(oct, "Loading firmware %d at %llx\n", image_len,
912 			     (unsigned long long)load_addr);
913 
914 		/* Write in 4MB chunks */
915 		rem = image_len;
916 
917 		while (rem) {
918 			if (rem < FBUF_SIZE)
919 				size = rem;
920 			else
921 				size = FBUF_SIZE;
922 
923 			/* download the image */
924 			lio_pci_write_core_mem(oct, load_addr,
925 					       __DECONST(uint8_t *, data),
926 					       (uint32_t) size);
927 
928 			data += size;
929 			rem -= (uint32_t) size;
930 			load_addr += size;
931 		}
932 	}
933 
934 	lio_dev_info(oct, "Writing boot command: %s\n", h->bootcmd);
935 
936 	/* Invoke the bootcmd */
937 	lio_console_send_cmd(oct, h->bootcmd, 50);
938 	return (0);
939 }
940