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