1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt DMA configuration based mailbox support
5 * Copyright (C) 2017, Intel Corporation
6 * Authors: Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
16 #define DMA_PORT_CAP 0x3e
19 #define MAIL_DATA_DWORDS 16
22 #define MAIL_IN_CMD_SHIFT 28
23 #define MAIL_IN_CMD_MASK GENMASK(31, 28)
24 #define MAIL_IN_CMD_FLASH_WRITE 0x0
25 #define MAIL_IN_CMD_FLASH_UPDATE_AUTH 0x1
26 #define MAIL_IN_CMD_FLASH_READ 0x2
27 #define MAIL_IN_CMD_POWER_CYCLE 0x4
28 #define MAIL_IN_DWORDS_SHIFT 24
29 #define MAIL_IN_DWORDS_MASK GENMASK(27, 24)
30 #define MAIL_IN_ADDRESS_SHIFT 2
31 #define MAIL_IN_ADDRESS_MASK GENMASK(23, 2)
32 #define MAIL_IN_CSS BIT(1)
33 #define MAIL_IN_OP_REQUEST BIT(0)
36 #define MAIL_OUT_STATUS_RESPONSE BIT(29)
37 #define MAIL_OUT_STATUS_CMD_SHIFT 4
38 #define MAIL_OUT_STATUS_CMD_MASK GENMASK(7, 4)
39 #define MAIL_OUT_STATUS_MASK GENMASK(3, 0)
40 #define MAIL_OUT_STATUS_COMPLETED 0
41 #define MAIL_OUT_STATUS_ERR_AUTH 1
42 #define MAIL_OUT_STATUS_ERR_ACCESS 2
44 #define DMA_PORT_TIMEOUT 5000 /* ms */
45 #define DMA_PORT_RETRIES 3
48 * struct tb_dma_port - DMA control port
49 * @sw: Switch the DMA port belongs to
50 * @port: Switch port number where DMA capability is found
51 * @base: Start offset of the mailbox registers
52 * @buf: Temporary buffer to store a single block
62 * When the switch is in safe mode it supports very little functionality
63 * so we don't validate that much here.
65 static bool dma_port_match(const struct tb_cfg_request *req,
66 const struct ctl_pkg *pkg)
68 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
70 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
72 if (pkg->frame.eof != req->response_type)
74 if (route != tb_cfg_get_route(req->request))
76 if (pkg->frame.size != req->response_size)
82 static bool dma_port_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
84 memcpy(req->response, pkg->buffer, req->response_size);
88 static int dma_port_read(struct tb_ctl *ctl, void *buffer, u64 route,
89 u32 port, u32 offset, u32 length, int timeout_msec)
91 struct cfg_read_pkg request = {
92 .header = tb_cfg_make_header(route),
101 struct tb_cfg_request *req;
102 struct cfg_write_pkg reply;
103 struct tb_cfg_result res;
105 req = tb_cfg_request_alloc();
109 req->match = dma_port_match;
110 req->copy = dma_port_copy;
111 req->request = &request;
112 req->request_size = sizeof(request);
113 req->request_type = TB_CFG_PKG_READ;
114 req->response = &reply;
115 req->response_size = 12 + 4 * length;
116 req->response_type = TB_CFG_PKG_READ;
118 res = tb_cfg_request_sync(ctl, req, timeout_msec);
120 tb_cfg_request_put(req);
125 memcpy(buffer, &reply.data, 4 * length);
129 static int dma_port_write(struct tb_ctl *ctl, const void *buffer, u64 route,
130 u32 port, u32 offset, u32 length, int timeout_msec)
132 struct cfg_write_pkg request = {
133 .header = tb_cfg_make_header(route),
137 .space = TB_CFG_PORT,
142 struct tb_cfg_request *req;
143 struct cfg_read_pkg reply;
144 struct tb_cfg_result res;
146 memcpy(&request.data, buffer, length * 4);
148 req = tb_cfg_request_alloc();
152 req->match = dma_port_match;
153 req->copy = dma_port_copy;
154 req->request = &request;
155 req->request_size = 12 + 4 * length;
156 req->request_type = TB_CFG_PKG_WRITE;
157 req->response = &reply;
158 req->response_size = sizeof(reply);
159 req->response_type = TB_CFG_PKG_WRITE;
161 res = tb_cfg_request_sync(ctl, req, timeout_msec);
163 tb_cfg_request_put(req);
168 static int dma_find_port(struct tb_switch *sw)
170 static const int ports[] = { 3, 5, 7 };
174 * The DMA (NHI) port is either 3, 5 or 7 depending on the
175 * controller. Try all of them.
177 for (i = 0; i < ARRAY_SIZE(ports); i++) {
181 ret = dma_port_read(sw->tb->ctl, &type, tb_route(sw), ports[i],
182 2, 1, DMA_PORT_TIMEOUT);
183 if (!ret && (type & 0xffffff) == TB_TYPE_NHI)
191 * dma_port_alloc() - Finds DMA control port from a switch pointed by route
192 * @sw: Switch from where find the DMA port
194 * Function checks if the switch NHI port supports DMA configuration
195 * based mailbox capability and if it does, allocates and initializes
196 * DMA port structure. Returns %NULL if the capabity was not found.
198 * The DMA control port is functional also when the switch is in safe
201 struct tb_dma_port *dma_port_alloc(struct tb_switch *sw)
203 struct tb_dma_port *dma;
206 port = dma_find_port(sw);
210 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
214 dma->buf = kmalloc_array(MAIL_DATA_DWORDS, sizeof(u32), GFP_KERNEL);
222 dma->base = DMA_PORT_CAP;
228 * dma_port_free() - Release DMA control port structure
229 * @dma: DMA control port
231 void dma_port_free(struct tb_dma_port *dma)
239 static int dma_port_wait_for_completion(struct tb_dma_port *dma,
240 unsigned int timeout)
242 unsigned long end = jiffies + msecs_to_jiffies(timeout);
243 struct tb_switch *sw = dma->sw;
249 ret = dma_port_read(sw->tb->ctl, &in, tb_route(sw), dma->port,
250 dma->base + MAIL_IN, 1, 50);
252 if (ret != -ETIMEDOUT)
254 } else if (!(in & MAIL_IN_OP_REQUEST)) {
258 usleep_range(50, 100);
259 } while (time_before(jiffies, end));
264 static int status_to_errno(u32 status)
266 switch (status & MAIL_OUT_STATUS_MASK) {
267 case MAIL_OUT_STATUS_COMPLETED:
269 case MAIL_OUT_STATUS_ERR_AUTH:
271 case MAIL_OUT_STATUS_ERR_ACCESS:
278 static int dma_port_request(struct tb_dma_port *dma, u32 in,
279 unsigned int timeout)
281 struct tb_switch *sw = dma->sw;
285 ret = dma_port_write(sw->tb->ctl, &in, tb_route(sw), dma->port,
286 dma->base + MAIL_IN, 1, DMA_PORT_TIMEOUT);
290 ret = dma_port_wait_for_completion(dma, timeout);
294 ret = dma_port_read(sw->tb->ctl, &out, tb_route(sw), dma->port,
295 dma->base + MAIL_OUT, 1, DMA_PORT_TIMEOUT);
299 return status_to_errno(out);
302 static int dma_port_flash_read_block(struct tb_dma_port *dma, u32 address,
305 struct tb_switch *sw = dma->sw;
306 u32 in, dwaddress, dwords;
309 dwaddress = address / 4;
312 in = MAIL_IN_CMD_FLASH_READ << MAIL_IN_CMD_SHIFT;
313 if (dwords < MAIL_DATA_DWORDS)
314 in |= (dwords << MAIL_IN_DWORDS_SHIFT) & MAIL_IN_DWORDS_MASK;
315 in |= (dwaddress << MAIL_IN_ADDRESS_SHIFT) & MAIL_IN_ADDRESS_MASK;
316 in |= MAIL_IN_OP_REQUEST;
318 ret = dma_port_request(dma, in, DMA_PORT_TIMEOUT);
322 return dma_port_read(sw->tb->ctl, buf, tb_route(sw), dma->port,
323 dma->base + MAIL_DATA, dwords, DMA_PORT_TIMEOUT);
326 static int dma_port_flash_write_block(struct tb_dma_port *dma, u32 address,
327 const void *buf, u32 size)
329 struct tb_switch *sw = dma->sw;
330 u32 in, dwaddress, dwords;
335 /* Write the block to MAIL_DATA registers */
336 ret = dma_port_write(sw->tb->ctl, buf, tb_route(sw), dma->port,
337 dma->base + MAIL_DATA, dwords, DMA_PORT_TIMEOUT);
339 in = MAIL_IN_CMD_FLASH_WRITE << MAIL_IN_CMD_SHIFT;
341 /* CSS header write is always done to the same magic address */
342 if (address >= DMA_PORT_CSS_ADDRESS) {
343 dwaddress = DMA_PORT_CSS_ADDRESS;
346 dwaddress = address / 4;
349 in |= ((dwords - 1) << MAIL_IN_DWORDS_SHIFT) & MAIL_IN_DWORDS_MASK;
350 in |= (dwaddress << MAIL_IN_ADDRESS_SHIFT) & MAIL_IN_ADDRESS_MASK;
351 in |= MAIL_IN_OP_REQUEST;
353 return dma_port_request(dma, in, DMA_PORT_TIMEOUT);
357 * dma_port_flash_read() - Read from active flash region
358 * @dma: DMA control port
359 * @address: Address relative to the start of active region
360 * @buf: Buffer where the data is read
361 * @size: Size of the buffer
363 int dma_port_flash_read(struct tb_dma_port *dma, unsigned int address,
364 void *buf, size_t size)
366 unsigned int retries = DMA_PORT_RETRIES;
369 offset = address & 3;
370 address = address & ~3;
373 u32 nbytes = min_t(u32, size, MAIL_DATA_DWORDS * 4);
376 ret = dma_port_flash_read_block(dma, address, dma->buf,
379 if (ret == -ETIMEDOUT) {
387 memcpy(buf, dma->buf + offset, nbytes);
398 * dma_port_flash_write() - Write to non-active flash region
399 * @dma: DMA control port
400 * @address: Address relative to the start of non-active region
401 * @buf: Data to write
402 * @size: Size of the buffer
404 * Writes block of data to the non-active flash region of the switch. If
405 * the address is given as %DMA_PORT_CSS_ADDRESS the block is written
408 int dma_port_flash_write(struct tb_dma_port *dma, unsigned int address,
409 const void *buf, size_t size)
411 unsigned int retries = DMA_PORT_RETRIES;
414 if (address >= DMA_PORT_CSS_ADDRESS) {
416 if (size > DMA_PORT_CSS_MAX_SIZE)
419 offset = address & 3;
420 address = address & ~3;
424 u32 nbytes = min_t(u32, size, MAIL_DATA_DWORDS * 4);
427 memcpy(dma->buf + offset, buf, nbytes);
429 ret = dma_port_flash_write_block(dma, address, buf, nbytes);
431 if (ret == -ETIMEDOUT) {
448 * dma_port_flash_update_auth() - Starts flash authenticate cycle
449 * @dma: DMA control port
451 * Starts the flash update authentication cycle. If the image in the
452 * non-active area was valid, the switch starts upgrade process where
453 * active and non-active area get swapped in the end. Caller should call
454 * dma_port_flash_update_auth_status() to get status of this command.
455 * This is because if the switch in question is root switch the
456 * thunderbolt host controller gets reset as well.
458 int dma_port_flash_update_auth(struct tb_dma_port *dma)
462 in = MAIL_IN_CMD_FLASH_UPDATE_AUTH << MAIL_IN_CMD_SHIFT;
463 in |= MAIL_IN_OP_REQUEST;
465 return dma_port_request(dma, in, 150);
469 * dma_port_flash_update_auth_status() - Reads status of update auth command
470 * @dma: DMA control port
471 * @status: Status code of the operation
473 * The function checks if there is status available from the last update
474 * auth command. Returns %0 if there is no status and no further
475 * action is required. If there is status, %1 is returned instead and
476 * @status holds the failure code.
478 * Negative return means there was an error reading status from the
481 int dma_port_flash_update_auth_status(struct tb_dma_port *dma, u32 *status)
483 struct tb_switch *sw = dma->sw;
487 ret = dma_port_read(sw->tb->ctl, &out, tb_route(sw), dma->port,
488 dma->base + MAIL_OUT, 1, DMA_PORT_TIMEOUT);
492 /* Check if the status relates to flash update auth */
493 cmd = (out & MAIL_OUT_STATUS_CMD_MASK) >> MAIL_OUT_STATUS_CMD_SHIFT;
494 if (cmd == MAIL_IN_CMD_FLASH_UPDATE_AUTH) {
496 *status = out & MAIL_OUT_STATUS_MASK;
498 /* Reset is needed in any case */
506 * dma_port_power_cycle() - Power cycles the switch
507 * @dma: DMA control port
509 * Triggers power cycle to the switch.
511 int dma_port_power_cycle(struct tb_dma_port *dma)
515 in = MAIL_IN_CMD_POWER_CYCLE << MAIL_IN_CMD_SHIFT;
516 in |= MAIL_IN_OP_REQUEST;
518 return dma_port_request(dma, in, 150);