2 * Thunderbolt DMA configuration based mailbox support
4 * Copyright (C) 2017, Intel Corporation
5 * Authors: Michael Jamet <michael.jamet@intel.com>
6 * Mika Westerberg <mika.westerberg@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/slab.h>
19 #define DMA_PORT_CAP 0x3e
22 #define MAIL_DATA_DWORDS 16
25 #define MAIL_IN_CMD_SHIFT 28
26 #define MAIL_IN_CMD_MASK GENMASK(31, 28)
27 #define MAIL_IN_CMD_FLASH_WRITE 0x0
28 #define MAIL_IN_CMD_FLASH_UPDATE_AUTH 0x1
29 #define MAIL_IN_CMD_FLASH_READ 0x2
30 #define MAIL_IN_CMD_POWER_CYCLE 0x4
31 #define MAIL_IN_DWORDS_SHIFT 24
32 #define MAIL_IN_DWORDS_MASK GENMASK(27, 24)
33 #define MAIL_IN_ADDRESS_SHIFT 2
34 #define MAIL_IN_ADDRESS_MASK GENMASK(23, 2)
35 #define MAIL_IN_CSS BIT(1)
36 #define MAIL_IN_OP_REQUEST BIT(0)
39 #define MAIL_OUT_STATUS_RESPONSE BIT(29)
40 #define MAIL_OUT_STATUS_CMD_SHIFT 4
41 #define MAIL_OUT_STATUS_CMD_MASK GENMASK(7, 4)
42 #define MAIL_OUT_STATUS_MASK GENMASK(3, 0)
43 #define MAIL_OUT_STATUS_COMPLETED 0
44 #define MAIL_OUT_STATUS_ERR_AUTH 1
45 #define MAIL_OUT_STATUS_ERR_ACCESS 2
47 #define DMA_PORT_TIMEOUT 5000 /* ms */
48 #define DMA_PORT_RETRIES 3
51 * struct tb_dma_port - DMA control port
52 * @sw: Switch the DMA port belongs to
53 * @port: Switch port number where DMA capability is found
54 * @base: Start offset of the mailbox registers
55 * @buf: Temporary buffer to store a single block
65 * When the switch is in safe mode it supports very little functionality
66 * so we don't validate that much here.
68 static bool dma_port_match(const struct tb_cfg_request
*req
,
69 const struct ctl_pkg
*pkg
)
71 u64 route
= tb_cfg_get_route(pkg
->buffer
) & ~BIT_ULL(63);
73 if (pkg
->frame
.eof
== TB_CFG_PKG_ERROR
)
75 if (pkg
->frame
.eof
!= req
->response_type
)
77 if (route
!= tb_cfg_get_route(req
->request
))
79 if (pkg
->frame
.size
!= req
->response_size
)
85 static bool dma_port_copy(struct tb_cfg_request
*req
, const struct ctl_pkg
*pkg
)
87 memcpy(req
->response
, pkg
->buffer
, req
->response_size
);
91 static int dma_port_read(struct tb_ctl
*ctl
, void *buffer
, u64 route
,
92 u32 port
, u32 offset
, u32 length
, int timeout_msec
)
94 struct cfg_read_pkg request
= {
95 .header
= tb_cfg_make_header(route
),
104 struct tb_cfg_request
*req
;
105 struct cfg_write_pkg reply
;
106 struct tb_cfg_result res
;
108 req
= tb_cfg_request_alloc();
112 req
->match
= dma_port_match
;
113 req
->copy
= dma_port_copy
;
114 req
->request
= &request
;
115 req
->request_size
= sizeof(request
);
116 req
->request_type
= TB_CFG_PKG_READ
;
117 req
->response
= &reply
;
118 req
->response_size
= 12 + 4 * length
;
119 req
->response_type
= TB_CFG_PKG_READ
;
121 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
123 tb_cfg_request_put(req
);
128 memcpy(buffer
, &reply
.data
, 4 * length
);
132 static int dma_port_write(struct tb_ctl
*ctl
, const void *buffer
, u64 route
,
133 u32 port
, u32 offset
, u32 length
, int timeout_msec
)
135 struct cfg_write_pkg request
= {
136 .header
= tb_cfg_make_header(route
),
140 .space
= TB_CFG_PORT
,
145 struct tb_cfg_request
*req
;
146 struct cfg_read_pkg reply
;
147 struct tb_cfg_result res
;
149 memcpy(&request
.data
, buffer
, length
* 4);
151 req
= tb_cfg_request_alloc();
155 req
->match
= dma_port_match
;
156 req
->copy
= dma_port_copy
;
157 req
->request
= &request
;
158 req
->request_size
= 12 + 4 * length
;
159 req
->request_type
= TB_CFG_PKG_WRITE
;
160 req
->response
= &reply
;
161 req
->response_size
= sizeof(reply
);
162 req
->response_type
= TB_CFG_PKG_WRITE
;
164 res
= tb_cfg_request_sync(ctl
, req
, timeout_msec
);
166 tb_cfg_request_put(req
);
171 static int dma_find_port(struct tb_switch
*sw
)
177 * The DMA (NHI) port is either 3 or 5 depending on the
178 * controller. Try both starting from 5 which is more common.
181 ret
= dma_port_read(sw
->tb
->ctl
, &type
, tb_route(sw
), port
, 2, 1,
183 if (!ret
&& (type
& 0xffffff) == TB_TYPE_NHI
)
187 ret
= dma_port_read(sw
->tb
->ctl
, &type
, tb_route(sw
), port
, 2, 1,
189 if (!ret
&& (type
& 0xffffff) == TB_TYPE_NHI
)
196 * dma_port_alloc() - Finds DMA control port from a switch pointed by route
197 * @sw: Switch from where find the DMA port
199 * Function checks if the switch NHI port supports DMA configuration
200 * based mailbox capability and if it does, allocates and initializes
201 * DMA port structure. Returns %NULL if the capabity was not found.
203 * The DMA control port is functional also when the switch is in safe
206 struct tb_dma_port
*dma_port_alloc(struct tb_switch
*sw
)
208 struct tb_dma_port
*dma
;
211 port
= dma_find_port(sw
);
215 dma
= kzalloc(sizeof(*dma
), GFP_KERNEL
);
219 dma
->buf
= kmalloc_array(MAIL_DATA_DWORDS
, sizeof(u32
), GFP_KERNEL
);
227 dma
->base
= DMA_PORT_CAP
;
233 * dma_port_free() - Release DMA control port structure
234 * @dma: DMA control port
236 void dma_port_free(struct tb_dma_port
*dma
)
244 static int dma_port_wait_for_completion(struct tb_dma_port
*dma
,
245 unsigned int timeout
)
247 unsigned long end
= jiffies
+ msecs_to_jiffies(timeout
);
248 struct tb_switch
*sw
= dma
->sw
;
254 ret
= dma_port_read(sw
->tb
->ctl
, &in
, tb_route(sw
), dma
->port
,
255 dma
->base
+ MAIL_IN
, 1, 50);
257 if (ret
!= -ETIMEDOUT
)
259 } else if (!(in
& MAIL_IN_OP_REQUEST
)) {
263 usleep_range(50, 100);
264 } while (time_before(jiffies
, end
));
269 static int status_to_errno(u32 status
)
271 switch (status
& MAIL_OUT_STATUS_MASK
) {
272 case MAIL_OUT_STATUS_COMPLETED
:
274 case MAIL_OUT_STATUS_ERR_AUTH
:
276 case MAIL_OUT_STATUS_ERR_ACCESS
:
283 static int dma_port_request(struct tb_dma_port
*dma
, u32 in
,
284 unsigned int timeout
)
286 struct tb_switch
*sw
= dma
->sw
;
290 ret
= dma_port_write(sw
->tb
->ctl
, &in
, tb_route(sw
), dma
->port
,
291 dma
->base
+ MAIL_IN
, 1, DMA_PORT_TIMEOUT
);
295 ret
= dma_port_wait_for_completion(dma
, timeout
);
299 ret
= dma_port_read(sw
->tb
->ctl
, &out
, tb_route(sw
), dma
->port
,
300 dma
->base
+ MAIL_OUT
, 1, DMA_PORT_TIMEOUT
);
304 return status_to_errno(out
);
307 static int dma_port_flash_read_block(struct tb_dma_port
*dma
, u32 address
,
310 struct tb_switch
*sw
= dma
->sw
;
311 u32 in
, dwaddress
, dwords
;
314 dwaddress
= address
/ 4;
317 in
= MAIL_IN_CMD_FLASH_READ
<< MAIL_IN_CMD_SHIFT
;
318 if (dwords
< MAIL_DATA_DWORDS
)
319 in
|= (dwords
<< MAIL_IN_DWORDS_SHIFT
) & MAIL_IN_DWORDS_MASK
;
320 in
|= (dwaddress
<< MAIL_IN_ADDRESS_SHIFT
) & MAIL_IN_ADDRESS_MASK
;
321 in
|= MAIL_IN_OP_REQUEST
;
323 ret
= dma_port_request(dma
, in
, DMA_PORT_TIMEOUT
);
327 return dma_port_read(sw
->tb
->ctl
, buf
, tb_route(sw
), dma
->port
,
328 dma
->base
+ MAIL_DATA
, dwords
, DMA_PORT_TIMEOUT
);
331 static int dma_port_flash_write_block(struct tb_dma_port
*dma
, u32 address
,
332 const void *buf
, u32 size
)
334 struct tb_switch
*sw
= dma
->sw
;
335 u32 in
, dwaddress
, dwords
;
340 /* Write the block to MAIL_DATA registers */
341 ret
= dma_port_write(sw
->tb
->ctl
, buf
, tb_route(sw
), dma
->port
,
342 dma
->base
+ MAIL_DATA
, dwords
, DMA_PORT_TIMEOUT
);
344 in
= MAIL_IN_CMD_FLASH_WRITE
<< MAIL_IN_CMD_SHIFT
;
346 /* CSS header write is always done to the same magic address */
347 if (address
>= DMA_PORT_CSS_ADDRESS
) {
348 dwaddress
= DMA_PORT_CSS_ADDRESS
;
351 dwaddress
= address
/ 4;
354 in
|= ((dwords
- 1) << MAIL_IN_DWORDS_SHIFT
) & MAIL_IN_DWORDS_MASK
;
355 in
|= (dwaddress
<< MAIL_IN_ADDRESS_SHIFT
) & MAIL_IN_ADDRESS_MASK
;
356 in
|= MAIL_IN_OP_REQUEST
;
358 return dma_port_request(dma
, in
, DMA_PORT_TIMEOUT
);
362 * dma_port_flash_read() - Read from active flash region
363 * @dma: DMA control port
364 * @address: Address relative to the start of active region
365 * @buf: Buffer where the data is read
366 * @size: Size of the buffer
368 int dma_port_flash_read(struct tb_dma_port
*dma
, unsigned int address
,
369 void *buf
, size_t size
)
371 unsigned int retries
= DMA_PORT_RETRIES
;
374 offset
= address
& 3;
375 address
= address
& ~3;
378 u32 nbytes
= min_t(u32
, size
, MAIL_DATA_DWORDS
* 4);
381 ret
= dma_port_flash_read_block(dma
, address
, dma
->buf
,
384 if (ret
== -ETIMEDOUT
) {
392 memcpy(buf
, dma
->buf
+ offset
, nbytes
);
403 * dma_port_flash_write() - Write to non-active flash region
404 * @dma: DMA control port
405 * @address: Address relative to the start of non-active region
406 * @buf: Data to write
407 * @size: Size of the buffer
409 * Writes block of data to the non-active flash region of the switch. If
410 * the address is given as %DMA_PORT_CSS_ADDRESS the block is written
413 int dma_port_flash_write(struct tb_dma_port
*dma
, unsigned int address
,
414 const void *buf
, size_t size
)
416 unsigned int retries
= DMA_PORT_RETRIES
;
419 if (address
>= DMA_PORT_CSS_ADDRESS
) {
421 if (size
> DMA_PORT_CSS_MAX_SIZE
)
424 offset
= address
& 3;
425 address
= address
& ~3;
429 u32 nbytes
= min_t(u32
, size
, MAIL_DATA_DWORDS
* 4);
432 memcpy(dma
->buf
+ offset
, buf
, nbytes
);
434 ret
= dma_port_flash_write_block(dma
, address
, buf
, nbytes
);
436 if (ret
== -ETIMEDOUT
) {
453 * dma_port_flash_update_auth() - Starts flash authenticate cycle
454 * @dma: DMA control port
456 * Starts the flash update authentication cycle. If the image in the
457 * non-active area was valid, the switch starts upgrade process where
458 * active and non-active area get swapped in the end. Caller should call
459 * dma_port_flash_update_auth_status() to get status of this command.
460 * This is because if the switch in question is root switch the
461 * thunderbolt host controller gets reset as well.
463 int dma_port_flash_update_auth(struct tb_dma_port
*dma
)
467 in
= MAIL_IN_CMD_FLASH_UPDATE_AUTH
<< MAIL_IN_CMD_SHIFT
;
468 in
|= MAIL_IN_OP_REQUEST
;
470 return dma_port_request(dma
, in
, 150);
474 * dma_port_flash_update_auth_status() - Reads status of update auth command
475 * @dma: DMA control port
476 * @status: Status code of the operation
478 * The function checks if there is status available from the last update
479 * auth command. Returns %0 if there is no status and no further
480 * action is required. If there is status, %1 is returned instead and
481 * @status holds the failure code.
483 * Negative return means there was an error reading status from the
486 int dma_port_flash_update_auth_status(struct tb_dma_port
*dma
, u32
*status
)
488 struct tb_switch
*sw
= dma
->sw
;
492 ret
= dma_port_read(sw
->tb
->ctl
, &out
, tb_route(sw
), dma
->port
,
493 dma
->base
+ MAIL_OUT
, 1, DMA_PORT_TIMEOUT
);
497 /* Check if the status relates to flash update auth */
498 cmd
= (out
& MAIL_OUT_STATUS_CMD_MASK
) >> MAIL_OUT_STATUS_CMD_SHIFT
;
499 if (cmd
== MAIL_IN_CMD_FLASH_UPDATE_AUTH
) {
501 *status
= out
& MAIL_OUT_STATUS_MASK
;
503 /* Reset is needed in any case */
511 * dma_port_power_cycle() - Power cycles the switch
512 * @dma: DMA control port
514 * Triggers power cycle to the switch.
516 int dma_port_power_cycle(struct tb_dma_port
*dma
)
520 in
= MAIL_IN_CMD_POWER_CYCLE
<< MAIL_IN_CMD_SHIFT
;
521 in
|= MAIL_IN_OP_REQUEST
;
523 return dma_port_request(dma
, in
, 150);