1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2015 Cavium, Inc.
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.
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
19 * This file may also be available under a different license from Cavium.
20 * Contact Cavium, Inc. for more information
21 **********************************************************************/
24 * @file octeon_console.c
26 #include <linux/pci.h>
27 #include <linux/netdevice.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 "octeon_main.h"
34 #include "octeon_mem_ops.h"
36 static void octeon_remote_lock(void);
37 static void octeon_remote_unlock(void);
38 static u64
cvmx_bootmem_phy_named_block_find(struct octeon_device
*oct
,
41 static int octeon_console_read(struct octeon_device
*oct
, u32 console_num
,
42 char *buffer
, u32 buf_size
);
44 #define MIN(a, b) min((a), (b))
45 #define CAST_ULL(v) ((u64)(v))
47 #define BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR 0x0006c008
48 #define BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR 0x0006c004
49 #define BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR 0x0006c000
50 #define BOOTLOADER_PCI_READ_DESC_ADDR 0x0006c100
51 #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
56 /** Can change without breaking ABI */
57 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64
59 /** minimum alignment of bootmem alloced blocks */
60 #define CVMX_BOOTMEM_ALIGNMENT_SIZE (16ull)
62 /** CVMX bootmem descriptor major version */
63 #define CVMX_BOOTMEM_DESC_MAJ_VER 3
64 /* CVMX bootmem descriptor minor version */
65 #define CVMX_BOOTMEM_DESC_MIN_VER 0
67 /* Current versions */
68 #define OCTEON_PCI_CONSOLE_MAJOR_VERSION 1
69 #define OCTEON_PCI_CONSOLE_MINOR_VERSION 0
70 #define OCTEON_PCI_CONSOLE_BLOCK_NAME "__pci_console"
71 #define OCTEON_CONSOLE_POLL_INTERVAL_MS 100 /* 10 times per second */
73 /* First three members of cvmx_bootmem_desc are left in original
74 ** positions for backwards compatibility.
75 ** Assumes big endian target
77 struct cvmx_bootmem_desc
{
78 /** spinlock to control access to list */
81 /** flags for indicating various conditions */
86 /** incremented changed when incompatible changes made */
89 /** incremented changed when compatible changes made,
90 * reset to zero when major incremented
97 /** number of elements in named blocks array */
100 /** length of name array in bootmem blocks */
101 u32 named_block_name_len
;
103 /** address of named memory block descriptors */
104 u64 named_block_array_addr
;
107 /* Structure that defines a single console.
109 * Note: when read_index == write_index, the buffer is empty.
110 * The actual usable size of each console is console_buf_size -1;
112 struct octeon_pci_console
{
114 u32 input_read_index
;
115 u32 input_write_index
;
116 u64 output_base_addr
;
117 u32 output_read_index
;
118 u32 output_write_index
;
123 /* This is the main container structure that contains all the information
124 * about all PCI consoles. The address of this structure is passed to various
125 * routines that operation on PCI consoles.
127 struct octeon_pci_console_desc
{
134 /* must be 64 bit aligned here... */
135 /* Array of addresses of octeon_pci_console structures */
136 u64 console_addr_array
[0];
137 /* Implicit storage for console_addr_array */
141 * This macro returns the size of a member of a structure.
142 * Logically it is the same as "sizeof(s::field)" in C++, but
143 * C lacks the "::" operator.
145 #define SIZEOF_FIELD(s, field) sizeof(((s *)NULL)->field)
148 * This macro returns a member of the cvmx_bootmem_desc
149 * structure. These members can't be directly addressed as
150 * they might be in memory not directly reachable. In the case
151 * where bootmem is compiled with LINUX_HOST, the structure
152 * itself might be located on a remote Octeon. The argument
153 * "field" is the member name of the cvmx_bootmem_desc to read.
154 * Regardless of the type of the field, the return type is always
157 #define CVMX_BOOTMEM_DESC_GET_FIELD(oct, field) \
158 __cvmx_bootmem_desc_get(oct, oct->bootmem_desc_addr, \
159 offsetof(struct cvmx_bootmem_desc, field), \
160 SIZEOF_FIELD(struct cvmx_bootmem_desc, field))
162 #define __cvmx_bootmem_lock(flags) (flags = flags)
163 #define __cvmx_bootmem_unlock(flags) (flags = flags)
166 * This macro returns a member of the
167 * cvmx_bootmem_named_block_desc structure. These members can't
168 * be directly addressed as they might be in memory not directly
169 * reachable. In the case where bootmem is compiled with
170 * LINUX_HOST, the structure itself might be located on a remote
171 * Octeon. The argument "field" is the member name of the
172 * cvmx_bootmem_named_block_desc to read. Regardless of the type
173 * of the field, the return type is always a u64. The "addr"
174 * parameter is the physical address of the structure.
176 #define CVMX_BOOTMEM_NAMED_GET_FIELD(oct, addr, field) \
177 __cvmx_bootmem_desc_get(oct, addr, \
178 offsetof(struct cvmx_bootmem_named_block_desc, field), \
179 SIZEOF_FIELD(struct cvmx_bootmem_named_block_desc, field))
182 * This function is the implementation of the get macros defined
183 * for individual structure members. The argument are generated
184 * by the macros inorder to read only the needed memory.
186 * @param oct Pointer to current octeon device
187 * @param base 64bit physical address of the complete structure
188 * @param offset Offset from the beginning of the structure to the member being
190 * @param size Size of the structure member.
192 * @return Value of the structure member promoted into a u64.
194 static inline u64
__cvmx_bootmem_desc_get(struct octeon_device
*oct
,
199 base
= (1ull << 63) | (base
+ offset
);
202 return octeon_read_device_mem32(oct
, base
);
204 return octeon_read_device_mem64(oct
, base
);
211 * This function retrieves the string name of a named block. It is
212 * more complicated than a simple memcpy() since the named block
213 * descriptor may not be directly accessible.
215 * @param addr Physical address of the named block descriptor
216 * @param str String to receive the named block string name
217 * @param len Length of the string buffer, which must match the length
218 * stored in the bootmem descriptor.
220 static void CVMX_BOOTMEM_NAMED_GET_NAME(struct octeon_device
*oct
,
225 addr
+= offsetof(struct cvmx_bootmem_named_block_desc
, name
);
226 octeon_pci_read_core_mem(oct
, addr
, (u8
*)str
, len
);
230 /* See header file for descriptions of functions */
233 * Check the version information on the bootmem descriptor
236 * Exact major version to check against. A zero means
237 * check that the version supports named blocks.
239 * @return Zero if the version is correct. Negative if the version is
240 * incorrect. Failures also cause a message to be displayed.
242 static int __cvmx_bootmem_check_version(struct octeon_device
*oct
,
248 if (!oct
->bootmem_desc_addr
)
249 oct
->bootmem_desc_addr
=
250 octeon_read_device_mem64(oct
,
251 BOOTLOADER_PCI_READ_DESC_ADDR
);
253 (u32
)CVMX_BOOTMEM_DESC_GET_FIELD(oct
, major_version
);
255 (u32
)CVMX_BOOTMEM_DESC_GET_FIELD(oct
, minor_version
);
256 dev_dbg(&oct
->pci_dev
->dev
, "%s: major_version=%d\n", __func__
,
258 if ((major_version
> 3) ||
259 (exact_match
&& major_version
!= exact_match
)) {
260 dev_err(&oct
->pci_dev
->dev
, "bootmem ver mismatch %d.%d addr:0x%llx\n",
261 major_version
, minor_version
,
262 CAST_ULL(oct
->bootmem_desc_addr
));
269 static const struct cvmx_bootmem_named_block_desc
270 *__cvmx_bootmem_find_named_block_flags(struct octeon_device
*oct
,
271 const char *name
, u32 flags
)
273 struct cvmx_bootmem_named_block_desc
*desc
=
274 &oct
->bootmem_named_block_desc
;
275 u64 named_addr
= cvmx_bootmem_phy_named_block_find(oct
, name
, flags
);
278 desc
->base_addr
= CVMX_BOOTMEM_NAMED_GET_FIELD(oct
, named_addr
,
281 CVMX_BOOTMEM_NAMED_GET_FIELD(oct
, named_addr
, size
);
282 strncpy(desc
->name
, name
, sizeof(desc
->name
));
283 desc
->name
[sizeof(desc
->name
) - 1] = 0;
284 return &oct
->bootmem_named_block_desc
;
290 static u64
cvmx_bootmem_phy_named_block_find(struct octeon_device
*oct
,
296 __cvmx_bootmem_lock(flags
);
297 if (!__cvmx_bootmem_check_version(oct
, 3)) {
299 u64 named_block_array_addr
=
300 CVMX_BOOTMEM_DESC_GET_FIELD(oct
,
301 named_block_array_addr
);
302 u32 num_blocks
= (u32
)
303 CVMX_BOOTMEM_DESC_GET_FIELD(oct
, nb_num_blocks
);
304 u32 name_length
= (u32
)
305 CVMX_BOOTMEM_DESC_GET_FIELD(oct
, named_block_name_len
);
306 u64 named_addr
= named_block_array_addr
;
308 for (i
= 0; i
< num_blocks
; i
++) {
310 CVMX_BOOTMEM_NAMED_GET_FIELD(oct
, named_addr
,
312 if (name
&& named_size
) {
314 kmalloc(name_length
+ 1, GFP_KERNEL
);
318 CVMX_BOOTMEM_NAMED_GET_NAME(oct
, named_addr
,
321 if (!strncmp(name
, name_tmp
, name_length
)) {
327 } else if (!name
&& !named_size
) {
333 sizeof(struct cvmx_bootmem_named_block_desc
);
336 __cvmx_bootmem_unlock(flags
);
341 * Find a named block on the remote Octeon
343 * @param name Name of block to find
344 * @param base_addr Address the block is at (OUTPUT)
345 * @param size The size of the block (OUTPUT)
347 * @return Zero on success, One on failure.
349 static int octeon_named_block_find(struct octeon_device
*oct
, const char *name
,
350 u64
*base_addr
, u64
*size
)
352 const struct cvmx_bootmem_named_block_desc
*named_block
;
354 octeon_remote_lock();
355 named_block
= __cvmx_bootmem_find_named_block_flags(oct
, name
, 0);
356 octeon_remote_unlock();
358 *base_addr
= named_block
->base_addr
;
359 *size
= named_block
->size
;
365 static void octeon_remote_lock(void)
367 /* fill this in if any sharing is needed */
370 static void octeon_remote_unlock(void)
372 /* fill this in if any sharing is needed */
375 int octeon_console_send_cmd(struct octeon_device
*oct
, char *cmd_str
,
378 u32 len
= (u32
)strlen(cmd_str
);
380 dev_dbg(&oct
->pci_dev
->dev
, "sending \"%s\" to bootloader\n", cmd_str
);
382 if (len
> BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN
- 1) {
383 dev_err(&oct
->pci_dev
->dev
, "Command string too long, max length is: %d\n",
384 BOOTLOADER_PCI_WRITE_BUFFER_STR_LEN
- 1);
388 if (octeon_wait_for_bootloader(oct
, wait_hundredths
) != 0) {
389 dev_err(&oct
->pci_dev
->dev
, "Bootloader not ready for command.\n");
393 /* Write command to bootloader */
394 octeon_remote_lock();
395 octeon_pci_write_core_mem(oct
, BOOTLOADER_PCI_READ_BUFFER_DATA_ADDR
,
397 octeon_write_device_mem32(oct
, BOOTLOADER_PCI_READ_BUFFER_LEN_ADDR
,
399 octeon_write_device_mem32(oct
, BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR
,
400 OCTEON_PCI_IO_BUF_OWNER_OCTEON
);
402 /* Bootloader should accept command very quickly
403 * if it really was ready
405 if (octeon_wait_for_bootloader(oct
, 200) != 0) {
406 octeon_remote_unlock();
407 dev_err(&oct
->pci_dev
->dev
, "Bootloader did not accept command.\n");
410 octeon_remote_unlock();
414 int octeon_wait_for_bootloader(struct octeon_device
*oct
,
415 u32 wait_time_hundredths
)
417 dev_dbg(&oct
->pci_dev
->dev
, "waiting %d0 ms for bootloader\n",
418 wait_time_hundredths
);
420 if (octeon_mem_access_ok(oct
))
423 while (wait_time_hundredths
> 0 &&
424 octeon_read_device_mem32(oct
,
425 BOOTLOADER_PCI_READ_BUFFER_OWNER_ADDR
)
426 != OCTEON_PCI_IO_BUF_OWNER_HOST
) {
427 if (--wait_time_hundredths
<= 0)
429 schedule_timeout_uninterruptible(HZ
/ 100);
434 static void octeon_console_handle_result(struct octeon_device
*oct
,
437 struct octeon_console
*console
;
439 console
= &oct
->console
[console_num
];
441 console
->waiting
= 0;
444 static char console_buffer
[OCTEON_CONSOLE_MAX_READ_BYTES
];
446 static void output_console_line(struct octeon_device
*oct
,
447 struct octeon_console
*console
,
449 char *console_buffer
,
455 line
= console_buffer
;
456 for (i
= 0; i
< bytes_read
; i
++) {
457 /* Output a line at a time, prefixed */
458 if (console_buffer
[i
] == '\n') {
459 console_buffer
[i
] = '\0';
460 if (console
->leftover
[0]) {
461 dev_info(&oct
->pci_dev
->dev
, "%lu: %s%s\n",
462 console_num
, console
->leftover
,
464 console
->leftover
[0] = '\0';
466 dev_info(&oct
->pci_dev
->dev
, "%lu: %s\n",
469 line
= &console_buffer
[i
+ 1];
473 /* Save off any leftovers */
474 if (line
!= &console_buffer
[bytes_read
]) {
475 console_buffer
[bytes_read
] = '\0';
476 strcpy(console
->leftover
, line
);
480 static void check_console(struct work_struct
*work
)
482 s32 bytes_read
, tries
, total_read
;
483 struct octeon_console
*console
;
484 struct cavium_wk
*wk
= (struct cavium_wk
*)work
;
485 struct octeon_device
*oct
= (struct octeon_device
*)wk
->ctxptr
;
486 u32 console_num
= (u32
)wk
->ctxul
;
489 console
= &oct
->console
[console_num
];
494 /* Take console output regardless of whether it will
498 octeon_console_read(oct
, console_num
, console_buffer
,
499 sizeof(console_buffer
) - 1);
500 if (bytes_read
> 0) {
501 total_read
+= bytes_read
;
502 if (console
->waiting
)
503 octeon_console_handle_result(oct
, console_num
);
504 if (octeon_console_debug_enabled(console_num
)) {
505 output_console_line(oct
, console
, console_num
,
506 console_buffer
, bytes_read
);
508 } else if (bytes_read
< 0) {
509 dev_err(&oct
->pci_dev
->dev
, "Error reading console %u, ret=%d\n",
510 console_num
, bytes_read
);
514 } while ((bytes_read
> 0) && (tries
< 16));
516 /* If nothing is read after polling the console,
517 * output any leftovers if any
519 if (octeon_console_debug_enabled(console_num
) &&
520 (total_read
== 0) && (console
->leftover
[0])) {
521 dev_info(&oct
->pci_dev
->dev
, "%u: %s\n",
522 console_num
, console
->leftover
);
523 console
->leftover
[0] = '\0';
526 delay
= OCTEON_CONSOLE_POLL_INTERVAL_MS
;
528 schedule_delayed_work(&wk
->work
, msecs_to_jiffies(delay
));
531 int octeon_init_consoles(struct octeon_device
*oct
)
536 ret
= octeon_mem_access_ok(oct
);
538 dev_err(&oct
->pci_dev
->dev
, "Memory access not okay'\n");
542 ret
= octeon_named_block_find(oct
, OCTEON_PCI_CONSOLE_BLOCK_NAME
, &addr
,
545 dev_err(&oct
->pci_dev
->dev
, "Could not find console '%s'\n",
546 OCTEON_PCI_CONSOLE_BLOCK_NAME
);
550 /* num_consoles > 0, is an indication that the consoles
553 oct
->num_consoles
= octeon_read_device_mem32(oct
,
554 addr
+ offsetof(struct octeon_pci_console_desc
,
556 oct
->console_desc_addr
= addr
;
558 dev_dbg(&oct
->pci_dev
->dev
, "Initialized consoles. %d available\n",
564 int octeon_add_console(struct octeon_device
*oct
, u32 console_num
)
569 struct delayed_work
*work
;
570 struct octeon_console
*console
;
572 if (console_num
>= oct
->num_consoles
) {
573 dev_err(&oct
->pci_dev
->dev
,
574 "trying to read from console number %d when only 0 to %d exist\n",
575 console_num
, oct
->num_consoles
);
577 console
= &oct
->console
[console_num
];
579 console
->waiting
= 0;
581 coreaddr
= oct
->console_desc_addr
+ console_num
* 8 +
582 offsetof(struct octeon_pci_console_desc
,
584 console
->addr
= octeon_read_device_mem64(oct
, coreaddr
);
585 coreaddr
= console
->addr
+ offsetof(struct octeon_pci_console
,
587 console
->buffer_size
= octeon_read_device_mem32(oct
, coreaddr
);
588 coreaddr
= console
->addr
+ offsetof(struct octeon_pci_console
,
590 console
->input_base_addr
=
591 octeon_read_device_mem64(oct
, coreaddr
);
592 coreaddr
= console
->addr
+ offsetof(struct octeon_pci_console
,
594 console
->output_base_addr
=
595 octeon_read_device_mem64(oct
, coreaddr
);
596 console
->leftover
[0] = '\0';
598 work
= &oct
->console_poll_work
[console_num
].work
;
600 INIT_DELAYED_WORK(work
, check_console
);
601 oct
->console_poll_work
[console_num
].ctxptr
= (void *)oct
;
602 oct
->console_poll_work
[console_num
].ctxul
= console_num
;
603 delay
= OCTEON_CONSOLE_POLL_INTERVAL_MS
;
604 schedule_delayed_work(work
, msecs_to_jiffies(delay
));
606 if (octeon_console_debug_enabled(console_num
)) {
607 ret
= octeon_console_send_cmd(oct
,
608 "setenv pci_console_active 1",
619 * Removes all consoles
621 * @param oct octeon device
623 void octeon_remove_consoles(struct octeon_device
*oct
)
626 struct octeon_console
*console
;
628 for (i
= 0; i
< oct
->num_consoles
; i
++) {
629 console
= &oct
->console
[i
];
631 if (!console
->active
)
634 cancel_delayed_work_sync(&oct
->console_poll_work
[i
].
637 console
->buffer_size
= 0;
638 console
->input_base_addr
= 0;
639 console
->output_base_addr
= 0;
642 oct
->num_consoles
= 0;
645 static inline int octeon_console_free_bytes(u32 buffer_size
,
649 if (rd_idx
>= buffer_size
|| wr_idx
>= buffer_size
)
652 return ((buffer_size
- 1) - (wr_idx
- rd_idx
)) % buffer_size
;
655 static inline int octeon_console_avail_bytes(u32 buffer_size
,
659 if (rd_idx
>= buffer_size
|| wr_idx
>= buffer_size
)
662 return buffer_size
- 1 -
663 octeon_console_free_bytes(buffer_size
, wr_idx
, rd_idx
);
666 static int octeon_console_read(struct octeon_device
*oct
, u32 console_num
,
667 char *buffer
, u32 buf_size
)
671 struct octeon_console
*console
;
673 if (console_num
>= oct
->num_consoles
) {
674 dev_err(&oct
->pci_dev
->dev
, "Attempted to read from disabled console %d\n",
679 console
= &oct
->console
[console_num
];
681 /* Check to see if any data is available.
682 * Maybe optimize this with 64-bit read.
684 rd_idx
= octeon_read_device_mem32(oct
, console
->addr
+
685 offsetof(struct octeon_pci_console
, output_read_index
));
686 wr_idx
= octeon_read_device_mem32(oct
, console
->addr
+
687 offsetof(struct octeon_pci_console
, output_write_index
));
689 bytes_to_read
= octeon_console_avail_bytes(console
->buffer_size
,
691 if (bytes_to_read
<= 0)
692 return bytes_to_read
;
694 bytes_to_read
= MIN(bytes_to_read
, (s32
)buf_size
);
696 /* Check to see if what we want to read is not contiguous, and limit
697 * ourselves to the contiguous block
699 if (rd_idx
+ bytes_to_read
>= console
->buffer_size
)
700 bytes_to_read
= console
->buffer_size
- rd_idx
;
702 octeon_pci_read_core_mem(oct
, console
->output_base_addr
+ rd_idx
,
703 (u8
*)buffer
, bytes_to_read
);
704 octeon_write_device_mem32(oct
, console
->addr
+
705 offsetof(struct octeon_pci_console
,
707 (rd_idx
+ bytes_to_read
) %
708 console
->buffer_size
);
710 return bytes_to_read
;