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