| 1 | |
| 2 | |
| 3 | * Thunderbolt DMA configuration based mailbox support
|
| 4 | *
|
| 5 | * Copyright (C) 2017, Intel Corporation
|
| 6 | * Authors: Michael Jamet <[email protected]>
|
| 7 | * Mika Westerberg <[email protected]>
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/delay.h>
|
| 11 | #include <linux/slab.h>
|
| 12 |
|
| 13 | #include "dma_port.h"
|
| 14 | #include "tb_regs.h"
|
| 15 |
|
| 16 | #define DMA_PORT_CAP 0x3e
|
| 17 |
|
| 18 | #define MAIL_DATA 1
|
| 19 | #define MAIL_DATA_DWORDS 16
|
| 20 |
|
| 21 | #define MAIL_IN 17
|
| 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)
|
| 34 |
|
| 35 | #define MAIL_OUT 18
|
| 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
|
| 43 |
|
| 44 | #define DMA_PORT_TIMEOUT 5000
|
| 45 | #define DMA_PORT_RETRIES 3
|
| 46 |
|
| 47 | |
| 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
|
| 53 |
|
| 54 | struct tb_dma_port {
|
| 55 | struct tb_switch *sw;
|
| 56 | u8 port;
|
| 57 | u32 base;
|
| 58 | u8 buf[];
|
| 59 | };
|
| 60 |
|
| 61 | |
| 62 | * When the switch is in safe mode it supports very little functionality
|
| 63 | * so we don't validate that much here.
|
| 64 |
|
| 65 | static bool dma_port_match(const struct tb_cfg_request *req,
|
| 66 | const struct ctl_pkg *pkg)
|
| 67 | {
|
| 68 | u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
|
| 69 |
|
| 70 | if (pkg->frame.eof == TB_CFG_PKG_ERROR)
|
| 71 | return true;
|
| 72 | if (pkg->frame.eof != req->response_type)
|
| 73 | return false;
|
| 74 | if (route != tb_cfg_get_route(req->request))
|
| 75 | return false;
|
| 76 | if (pkg->frame.size != req->response_size)
|
| 77 | return false;
|
| 78 |
|
| 79 | return true;
|
| 80 | }
|
| 81 |
|
| 82 | static bool dma_port_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
|
| 83 | {
|
| 84 | memcpy(req->response, pkg->buffer, req->response_size);
|
| 85 | return true;
|
| 86 | }
|
| 87 |
|
| 88 | static int dma_port_read(struct tb_ctl *ctl, void *buffer, u64 route,
|
| 89 | u32 port, u32 offset, u32 length, int timeout_msec)
|
| 90 | {
|
| 91 | struct cfg_read_pkg request = {
|
| 92 | .header = tb_cfg_make_header(route),
|
| 93 | .addr = {
|
| 94 | .seq = 1,
|
| 95 | .port = port,
|
| 96 | .space = TB_CFG_PORT,
|
| 97 | .offset = offset,
|
| 98 | .length = length,
|
| 99 | },
|
| 100 | };
|
| 101 | struct tb_cfg_request *req;
|
| 102 | struct cfg_write_pkg reply;
|
| 103 | struct tb_cfg_result res;
|
| 104 |
|
| 105 | req = tb_cfg_request_alloc();
|
| 106 | if (!req)
|
| 107 | return -ENOMEM;
|
| 108 |
|
| 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;
|
| 117 |
|
| 118 | res = tb_cfg_request_sync(ctl, req, timeout_msec);
|
| 119 |
|
| 120 | tb_cfg_request_put(req);
|
| 121 |
|
| 122 | if (res.err)
|
| 123 | return res.err;
|
| 124 |
|
| 125 | memcpy(buffer, &reply.data, 4 * length);
|
| 126 | return 0;
|
| 127 | }
|
| 128 |
|
| 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)
|
| 131 | {
|
| 132 | struct cfg_write_pkg request = {
|
| 133 | .header = tb_cfg_make_header(route),
|
| 134 | .addr = {
|
| 135 | .seq = 1,
|
| 136 | .port = port,
|
| 137 | .space = TB_CFG_PORT,
|
| 138 | .offset = offset,
|
| 139 | .length = length,
|
| 140 | },
|
| 141 | };
|
| 142 | struct tb_cfg_request *req;
|
| 143 | struct cfg_read_pkg reply;
|
| 144 | struct tb_cfg_result res;
|
| 145 |
|
| 146 | memcpy(&request.data, buffer, length * 4);
|
| 147 |
|
| 148 | req = tb_cfg_request_alloc();
|
| 149 | if (!req)
|
| 150 | return -ENOMEM;
|
| 151 |
|
| 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;
|
| 160 |
|
| 161 | res = tb_cfg_request_sync(ctl, req, timeout_msec);
|
| 162 |
|
| 163 | tb_cfg_request_put(req);
|
| 164 |
|
| 165 | return res.err;
|
| 166 | }
|
| 167 |
|
| 168 | static int dma_find_port(struct tb_switch *sw)
|
| 169 | {
|
| 170 | static const int ports[] = { 3, 5, 7 };
|
| 171 | int i;
|
| 172 |
|
| 173 | |
| 174 | * The DMA (NHI) port is either 3, 5 or 7 depending on the
|
| 175 | * controller. Try all of them.
|
| 176 |
|
| 177 | for (i = 0; i < ARRAY_SIZE(ports); i++) {
|
| 178 | u32 type;
|
| 179 | int ret;
|
| 180 |
|
| 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)
|
| 184 | return ports[i];
|
| 185 | }
|
| 186 |
|
| 187 | return -ENODEV;
|
| 188 | }
|
| 189 |
|
| 190 | |
| 191 | * dma_port_alloc() - Finds DMA control port from a switch pointed by route
|
| 192 | * @sw: Switch from where find the DMA port
|
| 193 | *
|
| 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.
|
| 197 | *
|
| 198 | * The DMA control port is functional also when the switch is in safe
|
| 199 | * mode.
|
| 200 | *
|
| 201 | * Return: &struct tb_dma_port on success, %NULL otherwise.
|
| 202 |
|
| 203 | struct tb_dma_port *dma_port_alloc(struct tb_switch *sw)
|
| 204 | {
|
| 205 | struct tb_dma_port *dma;
|
| 206 | int port;
|
| 207 |
|
| 208 | port = dma_find_port(sw);
|
| 209 | if (port < 0)
|
| 210 | return NULL;
|
| 211 |
|
| 212 | dma = kzalloc_flex(*dma, buf, MAIL_DATA_DWORDS);
|
| 213 | if (!dma)
|
| 214 | return NULL;
|
| 215 |
|
| 216 | dma->sw = sw;
|
| 217 | dma->port = port;
|
| 218 | dma->base = DMA_PORT_CAP;
|
| 219 |
|
| 220 | return dma;
|
| 221 | }
|
| 222 |
|
| 223 | |
| 224 | * dma_port_free() - Release DMA control port structure
|
| 225 | * @dma: DMA control port
|
| 226 |
|
| 227 | void dma_port_free(struct tb_dma_port *dma)
|
| 228 | {
|
| 229 | kfree(dma);
|
| 230 | }
|
| 231 |
|
| 232 | static int dma_port_wait_for_completion(struct tb_dma_port *dma,
|
| 233 | unsigned int timeout)
|
| 234 | {
|
| 235 | unsigned long end = jiffies + msecs_to_jiffies(timeout);
|
| 236 | struct tb_switch *sw = dma->sw;
|
| 237 |
|
| 238 | do {
|
| 239 | int ret;
|
| 240 | u32 in;
|
| 241 |
|
| 242 | ret = dma_port_read(sw->tb->ctl, &in, tb_route(sw), dma->port,
|
| 243 | dma->base + MAIL_IN, 1, 50);
|
| 244 | if (ret) {
|
| 245 | if (ret != -ETIMEDOUT)
|
| 246 | return ret;
|
| 247 | } else if (!(in & MAIL_IN_OP_REQUEST)) {
|
| 248 | return 0;
|
| 249 | }
|
| 250 |
|
| 251 | usleep_range(50, 100);
|
| 252 | } while (time_before(jiffies, end));
|
| 253 |
|
| 254 | return -ETIMEDOUT;
|
| 255 | }
|
| 256 |
|
| 257 | static int status_to_errno(u32 status)
|
| 258 | {
|
| 259 | switch (status & MAIL_OUT_STATUS_MASK) {
|
| 260 | case MAIL_OUT_STATUS_COMPLETED:
|
| 261 | return 0;
|
| 262 | case MAIL_OUT_STATUS_ERR_AUTH:
|
| 263 | return -EINVAL;
|
| 264 | case MAIL_OUT_STATUS_ERR_ACCESS:
|
| 265 | return -EACCES;
|
| 266 | }
|
| 267 |
|
| 268 | return -EIO;
|
| 269 | }
|
| 270 |
|
| 271 | static int dma_port_request(struct tb_dma_port *dma, u32 in,
|
| 272 | unsigned int timeout)
|
| 273 | {
|
| 274 | struct tb_switch *sw = dma->sw;
|
| 275 | u32 out;
|
| 276 | int ret;
|
| 277 |
|
| 278 | ret = dma_port_write(sw->tb->ctl, &in, tb_route(sw), dma->port,
|
| 279 | dma->base + MAIL_IN, 1, DMA_PORT_TIMEOUT);
|
| 280 | if (ret)
|
| 281 | return ret;
|
| 282 |
|
| 283 | ret = dma_port_wait_for_completion(dma, timeout);
|
| 284 | if (ret)
|
| 285 | return ret;
|
| 286 |
|
| 287 | ret = dma_port_read(sw->tb->ctl, &out, tb_route(sw), dma->port,
|
| 288 | dma->base + MAIL_OUT, 1, DMA_PORT_TIMEOUT);
|
| 289 | if (ret)
|
| 290 | return ret;
|
| 291 |
|
| 292 | return status_to_errno(out);
|
| 293 | }
|
| 294 |
|
| 295 | static int dma_port_flash_read_block(void *data, unsigned int dwaddress,
|
| 296 | void *buf, size_t dwords)
|
| 297 | {
|
| 298 | struct tb_dma_port *dma = data;
|
| 299 | struct tb_switch *sw = dma->sw;
|
| 300 | int ret;
|
| 301 | u32 in;
|
| 302 |
|
| 303 | in = MAIL_IN_CMD_FLASH_READ << MAIL_IN_CMD_SHIFT;
|
| 304 | if (dwords < MAIL_DATA_DWORDS)
|
| 305 | in |= (dwords << MAIL_IN_DWORDS_SHIFT) & MAIL_IN_DWORDS_MASK;
|
| 306 | in |= (dwaddress << MAIL_IN_ADDRESS_SHIFT) & MAIL_IN_ADDRESS_MASK;
|
| 307 | in |= MAIL_IN_OP_REQUEST;
|
| 308 |
|
| 309 | ret = dma_port_request(dma, in, DMA_PORT_TIMEOUT);
|
| 310 | if (ret)
|
| 311 | return ret;
|
| 312 |
|
| 313 | return dma_port_read(sw->tb->ctl, buf, tb_route(sw), dma->port,
|
| 314 | dma->base + MAIL_DATA, dwords, DMA_PORT_TIMEOUT);
|
| 315 | }
|
| 316 |
|
| 317 | static int dma_port_flash_write_block(void *data, unsigned int dwaddress,
|
| 318 | const void *buf, size_t dwords)
|
| 319 | {
|
| 320 | struct tb_dma_port *dma = data;
|
| 321 | struct tb_switch *sw = dma->sw;
|
| 322 | int ret;
|
| 323 | u32 in;
|
| 324 |
|
| 325 |
|
| 326 | ret = dma_port_write(sw->tb->ctl, buf, tb_route(sw), dma->port,
|
| 327 | dma->base + MAIL_DATA, dwords, DMA_PORT_TIMEOUT);
|
| 328 | if (ret)
|
| 329 | return ret;
|
| 330 |
|
| 331 | in = MAIL_IN_CMD_FLASH_WRITE << MAIL_IN_CMD_SHIFT;
|
| 332 |
|
| 333 |
|
| 334 | if (dwaddress >= DMA_PORT_CSS_ADDRESS)
|
| 335 | in |= MAIL_IN_CSS;
|
| 336 |
|
| 337 | in |= ((dwords - 1) << MAIL_IN_DWORDS_SHIFT) & MAIL_IN_DWORDS_MASK;
|
| 338 | in |= (dwaddress << MAIL_IN_ADDRESS_SHIFT) & MAIL_IN_ADDRESS_MASK;
|
| 339 | in |= MAIL_IN_OP_REQUEST;
|
| 340 |
|
| 341 | return dma_port_request(dma, in, DMA_PORT_TIMEOUT);
|
| 342 | }
|
| 343 |
|
| 344 | |
| 345 | * dma_port_flash_read() - Read from active flash region
|
| 346 | * @dma: DMA control port
|
| 347 | * @address: Address relative to the start of active region
|
| 348 | * @buf: Buffer where the data is read
|
| 349 | * @size: Size of the buffer
|
| 350 | *
|
| 351 | * Return: %0 on success, negative errno otherwise.
|
| 352 |
|
| 353 | int dma_port_flash_read(struct tb_dma_port *dma, unsigned int address,
|
| 354 | void *buf, size_t size)
|
| 355 | {
|
| 356 | return tb_nvm_read_data(address, buf, size, DMA_PORT_RETRIES,
|
| 357 | dma_port_flash_read_block, dma);
|
| 358 | }
|
| 359 |
|
| 360 | |
| 361 | * dma_port_flash_write() - Write to non-active flash region
|
| 362 | * @dma: DMA control port
|
| 363 | * @address: Address relative to the start of non-active region
|
| 364 | * @buf: Data to write
|
| 365 | * @size: Size of the buffer
|
| 366 | *
|
| 367 | * Writes block of data to the non-active flash region of the switch. If
|
| 368 | * the address is given as %DMA_PORT_CSS_ADDRESS the block is written
|
| 369 | * using CSS command.
|
| 370 | *
|
| 371 | * Return: %0 on success, negative errno otherwise.
|
| 372 |
|
| 373 | int dma_port_flash_write(struct tb_dma_port *dma, unsigned int address,
|
| 374 | const void *buf, size_t size)
|
| 375 | {
|
| 376 | if (address >= DMA_PORT_CSS_ADDRESS && size > DMA_PORT_CSS_MAX_SIZE)
|
| 377 | return -E2BIG;
|
| 378 |
|
| 379 | return tb_nvm_write_data(address, buf, size, DMA_PORT_RETRIES,
|
| 380 | dma_port_flash_write_block, dma);
|
| 381 | }
|
| 382 |
|
| 383 | |
| 384 | * dma_port_flash_update_auth() - Starts flash authenticate cycle
|
| 385 | * @dma: DMA control port
|
| 386 | *
|
| 387 | * Starts the flash update authentication cycle. If the image in the
|
| 388 | * non-active area was valid, the switch starts upgrade process where
|
| 389 | * active and non-active area get swapped in the end. Caller should call
|
| 390 | * dma_port_flash_update_auth_status() to get status of this command.
|
| 391 | * This is because if the switch in question is root switch the
|
| 392 | * thunderbolt host controller gets reset as well.
|
| 393 | *
|
| 394 | * Return: %0 on success, negative errno otherwise.
|
| 395 |
|
| 396 | int dma_port_flash_update_auth(struct tb_dma_port *dma)
|
| 397 | {
|
| 398 | u32 in;
|
| 399 |
|
| 400 | in = MAIL_IN_CMD_FLASH_UPDATE_AUTH << MAIL_IN_CMD_SHIFT;
|
| 401 | in |= MAIL_IN_OP_REQUEST;
|
| 402 |
|
| 403 | return dma_port_request(dma, in, 150);
|
| 404 | }
|
| 405 |
|
| 406 | |
| 407 | * dma_port_flash_update_auth_status() - Reads status of update auth command
|
| 408 | * @dma: DMA control port
|
| 409 | * @status: Status code of the operation
|
| 410 | *
|
| 411 | * The function checks if there is status available from the last update
|
| 412 | * auth command.
|
| 413 | *
|
| 414 | * Return:
|
| 415 | * * %0 - If there is no status and no further action is required.
|
| 416 | * * %1 - If there is some status. @status holds the failure code.
|
| 417 | * * Negative errno - An error occurred when reading status from the
|
| 418 | * switch.
|
| 419 |
|
| 420 | int dma_port_flash_update_auth_status(struct tb_dma_port *dma, u32 *status)
|
| 421 | {
|
| 422 | struct tb_switch *sw = dma->sw;
|
| 423 | u32 out, cmd;
|
| 424 | int ret;
|
| 425 |
|
| 426 | ret = dma_port_read(sw->tb->ctl, &out, tb_route(sw), dma->port,
|
| 427 | dma->base + MAIL_OUT, 1, DMA_PORT_TIMEOUT);
|
| 428 | if (ret)
|
| 429 | return ret;
|
| 430 |
|
| 431 |
|
| 432 | cmd = (out & MAIL_OUT_STATUS_CMD_MASK) >> MAIL_OUT_STATUS_CMD_SHIFT;
|
| 433 | if (cmd == MAIL_IN_CMD_FLASH_UPDATE_AUTH) {
|
| 434 | if (status)
|
| 435 | *status = out & MAIL_OUT_STATUS_MASK;
|
| 436 |
|
| 437 |
|
| 438 | return 1;
|
| 439 | }
|
| 440 |
|
| 441 | return 0;
|
| 442 | }
|
| 443 |
|
| 444 | |
| 445 | * dma_port_power_cycle() - Power cycles the switch
|
| 446 | * @dma: DMA control port
|
| 447 | *
|
| 448 | * Triggers power cycle to the switch.
|
| 449 | *
|
| 450 | * Return: %0 on success, negative errno otherwise.
|
| 451 |
|
| 452 | int dma_port_power_cycle(struct tb_dma_port *dma)
|
| 453 | {
|
| 454 | u32 in;
|
| 455 |
|
| 456 | in = MAIL_IN_CMD_POWER_CYCLE << MAIL_IN_CMD_SHIFT;
|
| 457 | in |= MAIL_IN_OP_REQUEST;
|
| 458 |
|
| 459 | return dma_port_request(dma, in, 150);
|
| 460 | }
|