Commit | Line | Data |
---|---|---|
f25bf6fc AN |
1 | /* |
2 | * Thunderbolt Cactus Ridge driver - control channel and configuration commands | |
3 | * | |
4 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> | |
5 | */ | |
6 | ||
7 | #include <linux/crc32.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/pci.h> | |
10 | #include <linux/dmapool.h> | |
11 | #include <linux/workqueue.h> | |
12 | #include <linux/kfifo.h> | |
13 | ||
14 | #include "ctl.h" | |
15 | ||
16 | ||
17 | struct ctl_pkg { | |
18 | struct tb_ctl *ctl; | |
19 | void *buffer; | |
20 | struct ring_frame frame; | |
21 | }; | |
22 | ||
23 | #define TB_CTL_RX_PKG_COUNT 10 | |
24 | ||
25 | /** | |
26 | * struct tb_cfg - thunderbolt control channel | |
27 | */ | |
28 | struct tb_ctl { | |
29 | struct tb_nhi *nhi; | |
30 | struct tb_ring *tx; | |
31 | struct tb_ring *rx; | |
32 | ||
33 | struct dma_pool *frame_pool; | |
34 | struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT]; | |
35 | DECLARE_KFIFO(response_fifo, struct ctl_pkg*, 16); | |
36 | struct completion response_ready; | |
37 | ||
38 | hotplug_cb callback; | |
39 | void *callback_data; | |
40 | }; | |
41 | ||
42 | ||
43 | #define tb_ctl_WARN(ctl, format, arg...) \ | |
44 | dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg) | |
45 | ||
46 | #define tb_ctl_err(ctl, format, arg...) \ | |
47 | dev_err(&(ctl)->nhi->pdev->dev, format, ## arg) | |
48 | ||
49 | #define tb_ctl_warn(ctl, format, arg...) \ | |
50 | dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg) | |
51 | ||
52 | #define tb_ctl_info(ctl, format, arg...) \ | |
53 | dev_info(&(ctl)->nhi->pdev->dev, format, ## arg) | |
54 | ||
f25bf6fc AN |
55 | /* utility functions */ |
56 | ||
f25bf6fc AN |
57 | static int check_header(struct ctl_pkg *pkg, u32 len, enum tb_cfg_pkg_type type, |
58 | u64 route) | |
59 | { | |
60 | struct tb_cfg_header *header = pkg->buffer; | |
61 | ||
62 | /* check frame, TODO: frame flags */ | |
63 | if (WARN(len != pkg->frame.size, | |
64 | "wrong framesize (expected %#x, got %#x)\n", | |
65 | len, pkg->frame.size)) | |
66 | return -EIO; | |
67 | if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n", | |
68 | type, pkg->frame.eof)) | |
69 | return -EIO; | |
70 | if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n", | |
71 | pkg->frame.sof)) | |
72 | return -EIO; | |
73 | ||
74 | /* check header */ | |
75 | if (WARN(header->unknown != 1 << 9, | |
76 | "header->unknown is %#x\n", header->unknown)) | |
77 | return -EIO; | |
ac6c44de | 78 | if (WARN(route != tb_cfg_get_route(header), |
f25bf6fc | 79 | "wrong route (expected %llx, got %llx)", |
ac6c44de | 80 | route, tb_cfg_get_route(header))) |
f25bf6fc AN |
81 | return -EIO; |
82 | return 0; | |
83 | } | |
84 | ||
85 | static int check_config_address(struct tb_cfg_address addr, | |
86 | enum tb_cfg_space space, u32 offset, | |
87 | u32 length) | |
88 | { | |
89 | if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero)) | |
90 | return -EIO; | |
91 | if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)", | |
92 | space, addr.space)) | |
93 | return -EIO; | |
94 | if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)", | |
95 | offset, addr.offset)) | |
96 | return -EIO; | |
97 | if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)", | |
98 | length, addr.length)) | |
99 | return -EIO; | |
100 | if (WARN(addr.seq, "addr.seq is %#x\n", addr.seq)) | |
101 | return -EIO; | |
102 | /* | |
103 | * We cannot check addr->port as it is set to the upstream port of the | |
104 | * sender. | |
105 | */ | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static struct tb_cfg_result decode_error(struct ctl_pkg *response) | |
110 | { | |
111 | struct cfg_error_pkg *pkg = response->buffer; | |
112 | struct tb_cfg_result res = { 0 }; | |
ac6c44de | 113 | res.response_route = tb_cfg_get_route(&pkg->header); |
f25bf6fc AN |
114 | res.response_port = 0; |
115 | res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR, | |
ac6c44de | 116 | tb_cfg_get_route(&pkg->header)); |
f25bf6fc AN |
117 | if (res.err) |
118 | return res; | |
119 | ||
120 | WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1); | |
121 | WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1); | |
122 | WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1); | |
123 | res.err = 1; | |
124 | res.tb_error = pkg->error; | |
125 | res.response_port = pkg->port; | |
126 | return res; | |
127 | ||
128 | } | |
129 | ||
130 | static struct tb_cfg_result parse_header(struct ctl_pkg *pkg, u32 len, | |
131 | enum tb_cfg_pkg_type type, u64 route) | |
132 | { | |
133 | struct tb_cfg_header *header = pkg->buffer; | |
134 | struct tb_cfg_result res = { 0 }; | |
135 | ||
136 | if (pkg->frame.eof == TB_CFG_PKG_ERROR) | |
137 | return decode_error(pkg); | |
138 | ||
139 | res.response_port = 0; /* will be updated later for cfg_read/write */ | |
ac6c44de | 140 | res.response_route = tb_cfg_get_route(header); |
f25bf6fc AN |
141 | res.err = check_header(pkg, len, type, route); |
142 | return res; | |
143 | } | |
144 | ||
145 | static void tb_cfg_print_error(struct tb_ctl *ctl, | |
146 | const struct tb_cfg_result *res) | |
147 | { | |
148 | WARN_ON(res->err != 1); | |
149 | switch (res->tb_error) { | |
150 | case TB_CFG_ERROR_PORT_NOT_CONNECTED: | |
151 | /* Port is not connected. This can happen during surprise | |
152 | * removal. Do not warn. */ | |
153 | return; | |
154 | case TB_CFG_ERROR_INVALID_CONFIG_SPACE: | |
155 | /* | |
156 | * Invalid cfg_space/offset/length combination in | |
157 | * cfg_read/cfg_write. | |
158 | */ | |
159 | tb_ctl_WARN(ctl, | |
aae20bb6 | 160 | "CFG_ERROR(%llx:%x): Invalid config space or offset\n", |
f25bf6fc AN |
161 | res->response_route, res->response_port); |
162 | return; | |
163 | case TB_CFG_ERROR_NO_SUCH_PORT: | |
164 | /* | |
165 | * - The route contains a non-existent port. | |
166 | * - The route contains a non-PHY port (e.g. PCIe). | |
167 | * - The port in cfg_read/cfg_write does not exist. | |
168 | */ | |
169 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n", | |
170 | res->response_route, res->response_port); | |
171 | return; | |
172 | case TB_CFG_ERROR_LOOP: | |
173 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n", | |
174 | res->response_route, res->response_port); | |
175 | return; | |
176 | default: | |
177 | /* 5,6,7,9 and 11 are also valid error codes */ | |
178 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n", | |
179 | res->response_route, res->response_port); | |
180 | return; | |
181 | } | |
182 | } | |
183 | ||
16a1258a | 184 | static void cpu_to_be32_array(__be32 *dst, const u32 *src, size_t len) |
f25bf6fc AN |
185 | { |
186 | int i; | |
187 | for (i = 0; i < len; i++) | |
188 | dst[i] = cpu_to_be32(src[i]); | |
189 | } | |
190 | ||
191 | static void be32_to_cpu_array(u32 *dst, __be32 *src, size_t len) | |
192 | { | |
193 | int i; | |
194 | for (i = 0; i < len; i++) | |
195 | dst[i] = be32_to_cpu(src[i]); | |
196 | } | |
197 | ||
198 | static __be32 tb_crc(void *data, size_t len) | |
199 | { | |
200 | return cpu_to_be32(~__crc32c_le(~0, data, len)); | |
201 | } | |
202 | ||
203 | static void tb_ctl_pkg_free(struct ctl_pkg *pkg) | |
204 | { | |
205 | if (pkg) { | |
206 | dma_pool_free(pkg->ctl->frame_pool, | |
207 | pkg->buffer, pkg->frame.buffer_phy); | |
208 | kfree(pkg); | |
209 | } | |
210 | } | |
211 | ||
212 | static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl) | |
213 | { | |
214 | struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL); | |
215 | if (!pkg) | |
8db353bd | 216 | return NULL; |
f25bf6fc AN |
217 | pkg->ctl = ctl; |
218 | pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, | |
219 | &pkg->frame.buffer_phy); | |
220 | if (!pkg->buffer) { | |
221 | kfree(pkg); | |
8db353bd | 222 | return NULL; |
f25bf6fc AN |
223 | } |
224 | return pkg; | |
225 | } | |
226 | ||
227 | ||
228 | /* RX/TX handling */ | |
229 | ||
230 | static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, | |
231 | bool canceled) | |
232 | { | |
233 | struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); | |
234 | tb_ctl_pkg_free(pkg); | |
235 | } | |
236 | ||
237 | /** | |
238 | * tb_cfg_tx() - transmit a packet on the control channel | |
239 | * | |
240 | * len must be a multiple of four. | |
241 | * | |
242 | * Return: Returns 0 on success or an error code on failure. | |
243 | */ | |
16a1258a | 244 | static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len, |
f25bf6fc AN |
245 | enum tb_cfg_pkg_type type) |
246 | { | |
247 | int res; | |
248 | struct ctl_pkg *pkg; | |
249 | if (len % 4 != 0) { /* required for le->be conversion */ | |
250 | tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len); | |
251 | return -EINVAL; | |
252 | } | |
253 | if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */ | |
254 | tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n", | |
255 | len, TB_FRAME_SIZE - 4); | |
256 | return -EINVAL; | |
257 | } | |
258 | pkg = tb_ctl_pkg_alloc(ctl); | |
259 | if (!pkg) | |
260 | return -ENOMEM; | |
261 | pkg->frame.callback = tb_ctl_tx_callback; | |
262 | pkg->frame.size = len + 4; | |
263 | pkg->frame.sof = type; | |
264 | pkg->frame.eof = type; | |
265 | cpu_to_be32_array(pkg->buffer, data, len / 4); | |
801dba53 | 266 | *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len); |
f25bf6fc AN |
267 | |
268 | res = ring_tx(ctl->tx, &pkg->frame); | |
269 | if (res) /* ring is stopped */ | |
270 | tb_ctl_pkg_free(pkg); | |
271 | return res; | |
272 | } | |
273 | ||
274 | /** | |
275 | * tb_ctl_handle_plug_event() - acknowledge a plug event, invoke ctl->callback | |
276 | */ | |
277 | static void tb_ctl_handle_plug_event(struct tb_ctl *ctl, | |
278 | struct ctl_pkg *response) | |
279 | { | |
280 | struct cfg_event_pkg *pkg = response->buffer; | |
ac6c44de | 281 | u64 route = tb_cfg_get_route(&pkg->header); |
f25bf6fc AN |
282 | |
283 | if (check_header(response, sizeof(*pkg), TB_CFG_PKG_EVENT, route)) { | |
284 | tb_ctl_warn(ctl, "malformed TB_CFG_PKG_EVENT\n"); | |
285 | return; | |
286 | } | |
287 | ||
288 | if (tb_cfg_error(ctl, route, pkg->port, TB_CFG_ERROR_ACK_PLUG_EVENT)) | |
289 | tb_ctl_warn(ctl, "could not ack plug event on %llx:%x\n", | |
290 | route, pkg->port); | |
291 | WARN(pkg->zero, "pkg->zero is %#x\n", pkg->zero); | |
292 | ctl->callback(ctl->callback_data, route, pkg->port, pkg->unplug); | |
293 | } | |
294 | ||
295 | static void tb_ctl_rx_submit(struct ctl_pkg *pkg) | |
296 | { | |
297 | ring_rx(pkg->ctl->rx, &pkg->frame); /* | |
298 | * We ignore failures during stop. | |
299 | * All rx packets are referenced | |
300 | * from ctl->rx_packets, so we do | |
301 | * not loose them. | |
302 | */ | |
303 | } | |
304 | ||
305 | static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, | |
306 | bool canceled) | |
307 | { | |
308 | struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); | |
309 | ||
310 | if (canceled) | |
311 | return; /* | |
312 | * ring is stopped, packet is referenced from | |
313 | * ctl->rx_packets. | |
314 | */ | |
315 | ||
316 | if (frame->size < 4 || frame->size % 4 != 0) { | |
317 | tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", | |
318 | frame->size); | |
319 | goto rx; | |
320 | } | |
321 | ||
322 | frame->size -= 4; /* remove checksum */ | |
801dba53 | 323 | if (*(__be32 *) (pkg->buffer + frame->size) |
f25bf6fc AN |
324 | != tb_crc(pkg->buffer, frame->size)) { |
325 | tb_ctl_err(pkg->ctl, | |
326 | "RX: checksum mismatch, dropping packet\n"); | |
327 | goto rx; | |
328 | } | |
329 | be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4); | |
330 | ||
331 | if (frame->eof == TB_CFG_PKG_EVENT) { | |
332 | tb_ctl_handle_plug_event(pkg->ctl, pkg); | |
333 | goto rx; | |
334 | } | |
335 | if (!kfifo_put(&pkg->ctl->response_fifo, pkg)) { | |
336 | tb_ctl_err(pkg->ctl, "RX: fifo is full\n"); | |
337 | goto rx; | |
338 | } | |
339 | complete(&pkg->ctl->response_ready); | |
340 | return; | |
341 | rx: | |
342 | tb_ctl_rx_submit(pkg); | |
343 | } | |
344 | ||
345 | /** | |
346 | * tb_ctl_rx() - receive a packet from the control channel | |
347 | */ | |
348 | static struct tb_cfg_result tb_ctl_rx(struct tb_ctl *ctl, void *buffer, | |
349 | size_t length, int timeout_msec, | |
343fcb8c | 350 | u64 route, enum tb_cfg_pkg_type type) |
f25bf6fc AN |
351 | { |
352 | struct tb_cfg_result res; | |
353 | struct ctl_pkg *pkg; | |
354 | ||
355 | if (!wait_for_completion_timeout(&ctl->response_ready, | |
356 | msecs_to_jiffies(timeout_msec))) { | |
357 | tb_ctl_WARN(ctl, "RX: timeout\n"); | |
358 | return (struct tb_cfg_result) { .err = -ETIMEDOUT }; | |
359 | } | |
360 | if (!kfifo_get(&ctl->response_fifo, &pkg)) { | |
361 | tb_ctl_WARN(ctl, "empty kfifo\n"); | |
362 | return (struct tb_cfg_result) { .err = -EIO }; | |
363 | } | |
364 | ||
365 | res = parse_header(pkg, length, type, route); | |
366 | if (!res.err) | |
367 | memcpy(buffer, pkg->buffer, length); | |
368 | tb_ctl_rx_submit(pkg); | |
369 | return res; | |
370 | } | |
371 | ||
372 | ||
373 | /* public interface, alloc/start/stop/free */ | |
374 | ||
375 | /** | |
376 | * tb_ctl_alloc() - allocate a control channel | |
377 | * | |
378 | * cb will be invoked once for every hot plug event. | |
379 | * | |
380 | * Return: Returns a pointer on success or NULL on failure. | |
381 | */ | |
382 | struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, hotplug_cb cb, void *cb_data) | |
383 | { | |
384 | int i; | |
385 | struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); | |
386 | if (!ctl) | |
387 | return NULL; | |
388 | ctl->nhi = nhi; | |
389 | ctl->callback = cb; | |
390 | ctl->callback_data = cb_data; | |
391 | ||
392 | init_completion(&ctl->response_ready); | |
393 | INIT_KFIFO(ctl->response_fifo); | |
394 | ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, | |
395 | TB_FRAME_SIZE, 4, 0); | |
396 | if (!ctl->frame_pool) | |
397 | goto err; | |
398 | ||
046bee1f | 399 | ctl->tx = ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); |
f25bf6fc AN |
400 | if (!ctl->tx) |
401 | goto err; | |
402 | ||
046bee1f | 403 | ctl->rx = ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); |
f25bf6fc AN |
404 | if (!ctl->rx) |
405 | goto err; | |
406 | ||
407 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) { | |
408 | ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); | |
409 | if (!ctl->rx_packets[i]) | |
410 | goto err; | |
411 | ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; | |
412 | } | |
413 | ||
414 | tb_ctl_info(ctl, "control channel created\n"); | |
415 | return ctl; | |
416 | err: | |
417 | tb_ctl_free(ctl); | |
418 | return NULL; | |
419 | } | |
420 | ||
421 | /** | |
422 | * tb_ctl_free() - free a control channel | |
423 | * | |
424 | * Must be called after tb_ctl_stop. | |
425 | * | |
426 | * Must NOT be called from ctl->callback. | |
427 | */ | |
428 | void tb_ctl_free(struct tb_ctl *ctl) | |
429 | { | |
430 | int i; | |
c9843ebb MW |
431 | |
432 | if (!ctl) | |
433 | return; | |
434 | ||
f25bf6fc AN |
435 | if (ctl->rx) |
436 | ring_free(ctl->rx); | |
437 | if (ctl->tx) | |
438 | ring_free(ctl->tx); | |
439 | ||
440 | /* free RX packets */ | |
441 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) | |
442 | tb_ctl_pkg_free(ctl->rx_packets[i]); | |
443 | ||
444 | ||
445 | if (ctl->frame_pool) | |
446 | dma_pool_destroy(ctl->frame_pool); | |
447 | kfree(ctl); | |
448 | } | |
449 | ||
450 | /** | |
451 | * tb_cfg_start() - start/resume the control channel | |
452 | */ | |
453 | void tb_ctl_start(struct tb_ctl *ctl) | |
454 | { | |
455 | int i; | |
456 | tb_ctl_info(ctl, "control channel starting...\n"); | |
457 | ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ | |
458 | ring_start(ctl->rx); | |
459 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) | |
460 | tb_ctl_rx_submit(ctl->rx_packets[i]); | |
461 | } | |
462 | ||
463 | /** | |
464 | * control() - pause the control channel | |
465 | * | |
466 | * All invocations of ctl->callback will have finished after this method | |
467 | * returns. | |
468 | * | |
469 | * Must NOT be called from ctl->callback. | |
470 | */ | |
471 | void tb_ctl_stop(struct tb_ctl *ctl) | |
472 | { | |
473 | ring_stop(ctl->rx); | |
474 | ring_stop(ctl->tx); | |
475 | ||
476 | if (!kfifo_is_empty(&ctl->response_fifo)) | |
477 | tb_ctl_WARN(ctl, "dangling response in response_fifo\n"); | |
478 | kfifo_reset(&ctl->response_fifo); | |
479 | tb_ctl_info(ctl, "control channel stopped\n"); | |
480 | } | |
481 | ||
482 | /* public interface, commands */ | |
483 | ||
484 | /** | |
485 | * tb_cfg_error() - send error packet | |
486 | * | |
487 | * Return: Returns 0 on success or an error code on failure. | |
488 | */ | |
489 | int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port, | |
490 | enum tb_cfg_error error) | |
491 | { | |
492 | struct cfg_error_pkg pkg = { | |
05c242e9 | 493 | .header = tb_cfg_make_header(route), |
f25bf6fc AN |
494 | .port = port, |
495 | .error = error, | |
496 | }; | |
497 | tb_ctl_info(ctl, "resetting error on %llx:%x.\n", route, port); | |
498 | return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR); | |
499 | } | |
500 | ||
501 | /** | |
502 | * tb_cfg_reset() - send a reset packet and wait for a response | |
503 | * | |
504 | * If the switch at route is incorrectly configured then we will not receive a | |
505 | * reply (even though the switch will reset). The caller should check for | |
506 | * -ETIMEDOUT and attempt to reconfigure the switch. | |
507 | */ | |
508 | struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route, | |
509 | int timeout_msec) | |
510 | { | |
511 | int err; | |
05c242e9 | 512 | struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) }; |
f25bf6fc AN |
513 | struct tb_cfg_header reply; |
514 | ||
515 | err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_RESET); | |
516 | if (err) | |
517 | return (struct tb_cfg_result) { .err = err }; | |
518 | ||
519 | return tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route, | |
520 | TB_CFG_PKG_RESET); | |
521 | } | |
522 | ||
523 | /** | |
524 | * tb_cfg_read() - read from config space into buffer | |
525 | * | |
526 | * Offset and length are in dwords. | |
527 | */ | |
528 | struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, | |
529 | u64 route, u32 port, enum tb_cfg_space space, | |
530 | u32 offset, u32 length, int timeout_msec) | |
531 | { | |
532 | struct tb_cfg_result res = { 0 }; | |
533 | struct cfg_read_pkg request = { | |
05c242e9 | 534 | .header = tb_cfg_make_header(route), |
f25bf6fc AN |
535 | .addr = { |
536 | .port = port, | |
537 | .space = space, | |
538 | .offset = offset, | |
539 | .length = length, | |
540 | }, | |
541 | }; | |
542 | struct cfg_write_pkg reply; | |
543 | ||
544 | res.err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_READ); | |
545 | if (res.err) | |
546 | return res; | |
547 | ||
548 | res = tb_ctl_rx(ctl, &reply, 12 + 4 * length, timeout_msec, route, | |
549 | TB_CFG_PKG_READ); | |
550 | if (res.err) | |
551 | return res; | |
552 | ||
553 | res.response_port = reply.addr.port; | |
554 | res.err = check_config_address(reply.addr, space, offset, length); | |
555 | if (!res.err) | |
556 | memcpy(buffer, &reply.data, 4 * length); | |
557 | return res; | |
558 | } | |
559 | ||
560 | /** | |
561 | * tb_cfg_write() - write from buffer into config space | |
562 | * | |
563 | * Offset and length are in dwords. | |
564 | */ | |
16a1258a | 565 | struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, |
f25bf6fc AN |
566 | u64 route, u32 port, enum tb_cfg_space space, |
567 | u32 offset, u32 length, int timeout_msec) | |
568 | { | |
569 | struct tb_cfg_result res = { 0 }; | |
570 | struct cfg_write_pkg request = { | |
05c242e9 | 571 | .header = tb_cfg_make_header(route), |
f25bf6fc AN |
572 | .addr = { |
573 | .port = port, | |
574 | .space = space, | |
575 | .offset = offset, | |
576 | .length = length, | |
577 | }, | |
578 | }; | |
579 | struct cfg_read_pkg reply; | |
580 | ||
581 | memcpy(&request.data, buffer, length * 4); | |
582 | ||
583 | res.err = tb_ctl_tx(ctl, &request, 12 + 4 * length, TB_CFG_PKG_WRITE); | |
584 | if (res.err) | |
585 | return res; | |
586 | ||
587 | res = tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route, | |
588 | TB_CFG_PKG_WRITE); | |
589 | if (res.err) | |
590 | return res; | |
591 | ||
592 | res.response_port = reply.addr.port; | |
593 | res.err = check_config_address(reply.addr, space, offset, length); | |
594 | return res; | |
595 | } | |
596 | ||
597 | int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, | |
598 | enum tb_cfg_space space, u32 offset, u32 length) | |
599 | { | |
600 | struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, | |
601 | space, offset, length, TB_CFG_DEFAULT_TIMEOUT); | |
602 | if (res.err == 1) { | |
603 | tb_cfg_print_error(ctl, &res); | |
604 | return -EIO; | |
605 | } | |
606 | WARN(res.err, "tb_cfg_read: %d\n", res.err); | |
607 | return res.err; | |
608 | } | |
609 | ||
16a1258a | 610 | int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, |
f25bf6fc AN |
611 | enum tb_cfg_space space, u32 offset, u32 length) |
612 | { | |
613 | struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, | |
614 | space, offset, length, TB_CFG_DEFAULT_TIMEOUT); | |
615 | if (res.err == 1) { | |
616 | tb_cfg_print_error(ctl, &res); | |
617 | return -EIO; | |
618 | } | |
619 | WARN(res.err, "tb_cfg_write: %d\n", res.err); | |
620 | return res.err; | |
621 | } | |
622 | ||
623 | /** | |
624 | * tb_cfg_get_upstream_port() - get upstream port number of switch at route | |
625 | * | |
626 | * Reads the first dword from the switches TB_CFG_SWITCH config area and | |
627 | * returns the port number from which the reply originated. | |
628 | * | |
629 | * Return: Returns the upstream port number on success or an error code on | |
630 | * failure. | |
631 | */ | |
632 | int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route) | |
633 | { | |
634 | u32 dummy; | |
635 | struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0, | |
636 | TB_CFG_SWITCH, 0, 1, | |
637 | TB_CFG_DEFAULT_TIMEOUT); | |
638 | if (res.err == 1) | |
639 | return -EIO; | |
640 | if (res.err) | |
641 | return res.err; | |
642 | return res.response_port; | |
643 | } |