cxgb4vf: shut down the adapter when t4vf_update_port_info() failed in cxgb4vf_open()
[platform/kernel/linux-rpi.git] / drivers / net / thunderbolt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Networking over Thunderbolt cable using Apple ThunderboltIP protocol
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Authors: Amir Levy <amir.jer.levy@intel.com>
7  *          Michael Jamet <michael.jamet@intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  */
10
11 #include <linux/atomic.h>
12 #include <linux/highmem.h>
13 #include <linux/if_vlan.h>
14 #include <linux/jhash.h>
15 #include <linux/module.h>
16 #include <linux/etherdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/sizes.h>
19 #include <linux/thunderbolt.h>
20 #include <linux/uuid.h>
21 #include <linux/workqueue.h>
22
23 #include <net/ip6_checksum.h>
24
25 /* Protocol timeouts in ms */
26 #define TBNET_LOGIN_DELAY       4500
27 #define TBNET_LOGIN_TIMEOUT     500
28 #define TBNET_LOGOUT_TIMEOUT    1000
29
30 #define TBNET_RING_SIZE         256
31 #define TBNET_LOGIN_RETRIES     60
32 #define TBNET_LOGOUT_RETRIES    10
33 #define TBNET_MATCH_FRAGS_ID    BIT(1)
34 #define TBNET_64K_FRAMES        BIT(2)
35 #define TBNET_MAX_MTU           SZ_64K
36 #define TBNET_FRAME_SIZE        SZ_4K
37 #define TBNET_MAX_PAYLOAD_SIZE  \
38         (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
39 /* Rx packets need to hold space for skb_shared_info */
40 #define TBNET_RX_MAX_SIZE       \
41         (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
42 #define TBNET_RX_PAGE_ORDER     get_order(TBNET_RX_MAX_SIZE)
43 #define TBNET_RX_PAGE_SIZE      (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
44
45 #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
46
47 /**
48  * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
49  * @frame_size: size of the data with the frame
50  * @frame_index: running index on the frames
51  * @frame_id: ID of the frame to match frames to specific packet
52  * @frame_count: how many frames assembles a full packet
53  *
54  * Each data frame passed to the high-speed DMA ring has this header. If
55  * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
56  * supported then @frame_id is filled, otherwise it stays %0.
57  */
58 struct thunderbolt_ip_frame_header {
59         u32 frame_size;
60         u16 frame_index;
61         u16 frame_id;
62         u32 frame_count;
63 };
64
65 enum thunderbolt_ip_frame_pdf {
66         TBIP_PDF_FRAME_START = 1,
67         TBIP_PDF_FRAME_END,
68 };
69
70 enum thunderbolt_ip_type {
71         TBIP_LOGIN,
72         TBIP_LOGIN_RESPONSE,
73         TBIP_LOGOUT,
74         TBIP_STATUS,
75 };
76
77 struct thunderbolt_ip_header {
78         u32 route_hi;
79         u32 route_lo;
80         u32 length_sn;
81         uuid_t uuid;
82         uuid_t initiator_uuid;
83         uuid_t target_uuid;
84         u32 type;
85         u32 command_id;
86 };
87
88 #define TBIP_HDR_LENGTH_MASK            GENMASK(5, 0)
89 #define TBIP_HDR_SN_MASK                GENMASK(28, 27)
90 #define TBIP_HDR_SN_SHIFT               27
91
92 struct thunderbolt_ip_login {
93         struct thunderbolt_ip_header hdr;
94         u32 proto_version;
95         u32 transmit_path;
96         u32 reserved[4];
97 };
98
99 #define TBIP_LOGIN_PROTO_VERSION        1
100
101 struct thunderbolt_ip_login_response {
102         struct thunderbolt_ip_header hdr;
103         u32 status;
104         u32 receiver_mac[2];
105         u32 receiver_mac_len;
106         u32 reserved[4];
107 };
108
109 struct thunderbolt_ip_logout {
110         struct thunderbolt_ip_header hdr;
111 };
112
113 struct thunderbolt_ip_status {
114         struct thunderbolt_ip_header hdr;
115         u32 status;
116 };
117
118 struct tbnet_stats {
119         u64 tx_packets;
120         u64 rx_packets;
121         u64 tx_bytes;
122         u64 rx_bytes;
123         u64 rx_errors;
124         u64 tx_errors;
125         u64 rx_length_errors;
126         u64 rx_over_errors;
127         u64 rx_crc_errors;
128         u64 rx_missed_errors;
129 };
130
131 struct tbnet_frame {
132         struct net_device *dev;
133         struct page *page;
134         struct ring_frame frame;
135 };
136
137 struct tbnet_ring {
138         struct tbnet_frame frames[TBNET_RING_SIZE];
139         unsigned int cons;
140         unsigned int prod;
141         struct tb_ring *ring;
142 };
143
144 /**
145  * struct tbnet - ThunderboltIP network driver private data
146  * @svc: XDomain service the driver is bound to
147  * @xd: XDomain the service blongs to
148  * @handler: ThunderboltIP configuration protocol handler
149  * @dev: Networking device
150  * @napi: NAPI structure for Rx polling
151  * @stats: Network statistics
152  * @skb: Network packet that is currently processed on Rx path
153  * @command_id: ID used for next configuration protocol packet
154  * @login_sent: ThunderboltIP login message successfully sent
155  * @login_received: ThunderboltIP login message received from the remote
156  *                  host
157  * @local_transmit_path: HopID we are using to send out packets
158  * @remote_transmit_path: HopID the other end is using to send packets to us
159  * @connection_lock: Lock serializing access to @login_sent,
160  *                   @login_received and @transmit_path.
161  * @login_retries: Number of login retries currently done
162  * @login_work: Worker to send ThunderboltIP login packets
163  * @connected_work: Worker that finalizes the ThunderboltIP connection
164  *                  setup and enables DMA paths for high speed data
165  *                  transfers
166  * @disconnect_work: Worker that handles tearing down the ThunderboltIP
167  *                   connection
168  * @rx_hdr: Copy of the currently processed Rx frame. Used when a
169  *          network packet consists of multiple Thunderbolt frames.
170  *          In host byte order.
171  * @rx_ring: Software ring holding Rx frames
172  * @frame_id: Frame ID use for next Tx packet
173  *            (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
174  * @tx_ring: Software ring holding Tx frames
175  */
176 struct tbnet {
177         const struct tb_service *svc;
178         struct tb_xdomain *xd;
179         struct tb_protocol_handler handler;
180         struct net_device *dev;
181         struct napi_struct napi;
182         struct tbnet_stats stats;
183         struct sk_buff *skb;
184         atomic_t command_id;
185         bool login_sent;
186         bool login_received;
187         int local_transmit_path;
188         int remote_transmit_path;
189         struct mutex connection_lock;
190         int login_retries;
191         struct delayed_work login_work;
192         struct work_struct connected_work;
193         struct work_struct disconnect_work;
194         struct thunderbolt_ip_frame_header rx_hdr;
195         struct tbnet_ring rx_ring;
196         atomic_t frame_id;
197         struct tbnet_ring tx_ring;
198 };
199
200 /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
201 static const uuid_t tbnet_dir_uuid =
202         UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
203                   0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
204
205 /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
206 static const uuid_t tbnet_svc_uuid =
207         UUID_INIT(0x798f589e, 0x3616, 0x8a47,
208                   0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
209
210 static struct tb_property_dir *tbnet_dir;
211
212 static void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
213         u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
214         enum thunderbolt_ip_type type, size_t size, u32 command_id)
215 {
216         u32 length_sn;
217
218         /* Length does not include route_hi/lo and length_sn fields */
219         length_sn = (size - 3 * 4) / 4;
220         length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
221
222         hdr->route_hi = upper_32_bits(route);
223         hdr->route_lo = lower_32_bits(route);
224         hdr->length_sn = length_sn;
225         uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
226         uuid_copy(&hdr->initiator_uuid, initiator_uuid);
227         uuid_copy(&hdr->target_uuid, target_uuid);
228         hdr->type = type;
229         hdr->command_id = command_id;
230 }
231
232 static int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
233                                 u32 command_id)
234 {
235         struct thunderbolt_ip_login_response reply;
236         struct tb_xdomain *xd = net->xd;
237
238         memset(&reply, 0, sizeof(reply));
239         tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
240                           xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
241                           command_id);
242         memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
243         reply.receiver_mac_len = ETH_ALEN;
244
245         return tb_xdomain_response(xd, &reply, sizeof(reply),
246                                    TB_CFG_PKG_XDOMAIN_RESP);
247 }
248
249 static int tbnet_login_request(struct tbnet *net, u8 sequence)
250 {
251         struct thunderbolt_ip_login_response reply;
252         struct thunderbolt_ip_login request;
253         struct tb_xdomain *xd = net->xd;
254
255         memset(&request, 0, sizeof(request));
256         tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
257                           xd->remote_uuid, TBIP_LOGIN, sizeof(request),
258                           atomic_inc_return(&net->command_id));
259
260         request.proto_version = TBIP_LOGIN_PROTO_VERSION;
261         request.transmit_path = net->local_transmit_path;
262
263         return tb_xdomain_request(xd, &request, sizeof(request),
264                                   TB_CFG_PKG_XDOMAIN_RESP, &reply,
265                                   sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
266                                   TBNET_LOGIN_TIMEOUT);
267 }
268
269 static int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
270                                  u32 command_id)
271 {
272         struct thunderbolt_ip_status reply;
273         struct tb_xdomain *xd = net->xd;
274
275         memset(&reply, 0, sizeof(reply));
276         tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
277                           xd->remote_uuid, TBIP_STATUS, sizeof(reply),
278                           atomic_inc_return(&net->command_id));
279         return tb_xdomain_response(xd, &reply, sizeof(reply),
280                                    TB_CFG_PKG_XDOMAIN_RESP);
281 }
282
283 static int tbnet_logout_request(struct tbnet *net)
284 {
285         struct thunderbolt_ip_logout request;
286         struct thunderbolt_ip_status reply;
287         struct tb_xdomain *xd = net->xd;
288
289         memset(&request, 0, sizeof(request));
290         tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
291                           xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
292                           atomic_inc_return(&net->command_id));
293
294         return tb_xdomain_request(xd, &request, sizeof(request),
295                                   TB_CFG_PKG_XDOMAIN_RESP, &reply,
296                                   sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
297                                   TBNET_LOGOUT_TIMEOUT);
298 }
299
300 static void start_login(struct tbnet *net)
301 {
302         mutex_lock(&net->connection_lock);
303         net->login_sent = false;
304         net->login_received = false;
305         mutex_unlock(&net->connection_lock);
306
307         queue_delayed_work(system_long_wq, &net->login_work,
308                            msecs_to_jiffies(1000));
309 }
310
311 static void stop_login(struct tbnet *net)
312 {
313         cancel_delayed_work_sync(&net->login_work);
314         cancel_work_sync(&net->connected_work);
315 }
316
317 static inline unsigned int tbnet_frame_size(const struct tbnet_frame *tf)
318 {
319         return tf->frame.size ? : TBNET_FRAME_SIZE;
320 }
321
322 static void tbnet_free_buffers(struct tbnet_ring *ring)
323 {
324         unsigned int i;
325
326         for (i = 0; i < TBNET_RING_SIZE; i++) {
327                 struct device *dma_dev = tb_ring_dma_device(ring->ring);
328                 struct tbnet_frame *tf = &ring->frames[i];
329                 enum dma_data_direction dir;
330                 unsigned int order;
331                 size_t size;
332
333                 if (!tf->page)
334                         continue;
335
336                 if (ring->ring->is_tx) {
337                         dir = DMA_TO_DEVICE;
338                         order = 0;
339                         size = TBNET_FRAME_SIZE;
340                 } else {
341                         dir = DMA_FROM_DEVICE;
342                         order = TBNET_RX_PAGE_ORDER;
343                         size = TBNET_RX_PAGE_SIZE;
344                 }
345
346                 if (tf->frame.buffer_phy)
347                         dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
348                                        dir);
349
350                 __free_pages(tf->page, order);
351                 tf->page = NULL;
352         }
353
354         ring->cons = 0;
355         ring->prod = 0;
356 }
357
358 static void tbnet_tear_down(struct tbnet *net, bool send_logout)
359 {
360         netif_carrier_off(net->dev);
361         netif_stop_queue(net->dev);
362
363         stop_login(net);
364
365         mutex_lock(&net->connection_lock);
366
367         if (net->login_sent && net->login_received) {
368                 int ret, retries = TBNET_LOGOUT_RETRIES;
369
370                 while (send_logout && retries-- > 0) {
371                         ret = tbnet_logout_request(net);
372                         if (ret != -ETIMEDOUT)
373                                 break;
374                 }
375
376                 tb_ring_stop(net->rx_ring.ring);
377                 tb_ring_stop(net->tx_ring.ring);
378                 tbnet_free_buffers(&net->rx_ring);
379                 tbnet_free_buffers(&net->tx_ring);
380
381                 ret = tb_xdomain_disable_paths(net->xd,
382                                                net->local_transmit_path,
383                                                net->rx_ring.ring->hop,
384                                                net->remote_transmit_path,
385                                                net->tx_ring.ring->hop);
386                 if (ret)
387                         netdev_warn(net->dev, "failed to disable DMA paths\n");
388
389                 tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
390                 net->remote_transmit_path = 0;
391         }
392
393         net->login_retries = 0;
394         net->login_sent = false;
395         net->login_received = false;
396
397         mutex_unlock(&net->connection_lock);
398 }
399
400 static int tbnet_handle_packet(const void *buf, size_t size, void *data)
401 {
402         const struct thunderbolt_ip_login *pkg = buf;
403         struct tbnet *net = data;
404         u32 command_id;
405         int ret = 0;
406         u32 sequence;
407         u64 route;
408
409         /* Make sure the packet is for us */
410         if (size < sizeof(struct thunderbolt_ip_header))
411                 return 0;
412         if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
413                 return 0;
414         if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
415                 return 0;
416
417         route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
418         route &= ~BIT_ULL(63);
419         if (route != net->xd->route)
420                 return 0;
421
422         sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
423         sequence >>= TBIP_HDR_SN_SHIFT;
424         command_id = pkg->hdr.command_id;
425
426         switch (pkg->hdr.type) {
427         case TBIP_LOGIN:
428                 if (!netif_running(net->dev))
429                         break;
430
431                 ret = tbnet_login_response(net, route, sequence,
432                                            pkg->hdr.command_id);
433                 if (!ret) {
434                         mutex_lock(&net->connection_lock);
435                         net->login_received = true;
436                         net->remote_transmit_path = pkg->transmit_path;
437
438                         /* If we reached the number of max retries or
439                          * previous logout, schedule another round of
440                          * login retries
441                          */
442                         if (net->login_retries >= TBNET_LOGIN_RETRIES ||
443                             !net->login_sent) {
444                                 net->login_retries = 0;
445                                 queue_delayed_work(system_long_wq,
446                                                    &net->login_work, 0);
447                         }
448                         mutex_unlock(&net->connection_lock);
449
450                         queue_work(system_long_wq, &net->connected_work);
451                 }
452                 break;
453
454         case TBIP_LOGOUT:
455                 ret = tbnet_logout_response(net, route, sequence, command_id);
456                 if (!ret)
457                         queue_work(system_long_wq, &net->disconnect_work);
458                 break;
459
460         default:
461                 return 0;
462         }
463
464         if (ret)
465                 netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
466
467         return 1;
468 }
469
470 static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
471 {
472         return ring->prod - ring->cons;
473 }
474
475 static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
476 {
477         struct tbnet_ring *ring = &net->rx_ring;
478         int ret;
479
480         while (nbuffers--) {
481                 struct device *dma_dev = tb_ring_dma_device(ring->ring);
482                 unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
483                 struct tbnet_frame *tf = &ring->frames[index];
484                 dma_addr_t dma_addr;
485
486                 if (tf->page)
487                         break;
488
489                 /* Allocate page (order > 0) so that it can hold maximum
490                  * ThunderboltIP frame (4kB) and the additional room for
491                  * SKB shared info required by build_skb().
492                  */
493                 tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
494                 if (!tf->page) {
495                         ret = -ENOMEM;
496                         goto err_free;
497                 }
498
499                 dma_addr = dma_map_page(dma_dev, tf->page, 0,
500                                         TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
501                 if (dma_mapping_error(dma_dev, dma_addr)) {
502                         ret = -ENOMEM;
503                         goto err_free;
504                 }
505
506                 tf->frame.buffer_phy = dma_addr;
507                 tf->dev = net->dev;
508
509                 tb_ring_rx(ring->ring, &tf->frame);
510
511                 ring->prod++;
512         }
513
514         return 0;
515
516 err_free:
517         tbnet_free_buffers(ring);
518         return ret;
519 }
520
521 static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
522 {
523         struct tbnet_ring *ring = &net->tx_ring;
524         struct device *dma_dev = tb_ring_dma_device(ring->ring);
525         struct tbnet_frame *tf;
526         unsigned int index;
527
528         if (!tbnet_available_buffers(ring))
529                 return NULL;
530
531         index = ring->cons++ & (TBNET_RING_SIZE - 1);
532
533         tf = &ring->frames[index];
534         tf->frame.size = 0;
535
536         dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
537                                 tbnet_frame_size(tf), DMA_TO_DEVICE);
538
539         return tf;
540 }
541
542 static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
543                               bool canceled)
544 {
545         struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
546         struct tbnet *net = netdev_priv(tf->dev);
547
548         /* Return buffer to the ring */
549         net->tx_ring.prod++;
550
551         if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
552                 netif_wake_queue(net->dev);
553 }
554
555 static int tbnet_alloc_tx_buffers(struct tbnet *net)
556 {
557         struct tbnet_ring *ring = &net->tx_ring;
558         struct device *dma_dev = tb_ring_dma_device(ring->ring);
559         unsigned int i;
560
561         for (i = 0; i < TBNET_RING_SIZE; i++) {
562                 struct tbnet_frame *tf = &ring->frames[i];
563                 dma_addr_t dma_addr;
564
565                 tf->page = alloc_page(GFP_KERNEL);
566                 if (!tf->page) {
567                         tbnet_free_buffers(ring);
568                         return -ENOMEM;
569                 }
570
571                 dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
572                                         DMA_TO_DEVICE);
573                 if (dma_mapping_error(dma_dev, dma_addr)) {
574                         __free_page(tf->page);
575                         tf->page = NULL;
576                         tbnet_free_buffers(ring);
577                         return -ENOMEM;
578                 }
579
580                 tf->dev = net->dev;
581                 tf->frame.buffer_phy = dma_addr;
582                 tf->frame.callback = tbnet_tx_callback;
583                 tf->frame.sof = TBIP_PDF_FRAME_START;
584                 tf->frame.eof = TBIP_PDF_FRAME_END;
585         }
586
587         ring->cons = 0;
588         ring->prod = TBNET_RING_SIZE - 1;
589
590         return 0;
591 }
592
593 static void tbnet_connected_work(struct work_struct *work)
594 {
595         struct tbnet *net = container_of(work, typeof(*net), connected_work);
596         bool connected;
597         int ret;
598
599         if (netif_carrier_ok(net->dev))
600                 return;
601
602         mutex_lock(&net->connection_lock);
603         connected = net->login_sent && net->login_received;
604         mutex_unlock(&net->connection_lock);
605
606         if (!connected)
607                 return;
608
609         ret = tb_xdomain_alloc_in_hopid(net->xd, net->remote_transmit_path);
610         if (ret != net->remote_transmit_path) {
611                 netdev_err(net->dev, "failed to allocate Rx HopID\n");
612                 return;
613         }
614
615         /* Both logins successful so enable the rings, high-speed DMA
616          * paths and start the network device queue.
617          *
618          * Note we enable the DMA paths last to make sure we have primed
619          * the Rx ring before any incoming packets are allowed to
620          * arrive.
621          */
622         tb_ring_start(net->tx_ring.ring);
623         tb_ring_start(net->rx_ring.ring);
624
625         ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
626         if (ret)
627                 goto err_stop_rings;
628
629         ret = tbnet_alloc_tx_buffers(net);
630         if (ret)
631                 goto err_free_rx_buffers;
632
633         ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
634                                       net->rx_ring.ring->hop,
635                                       net->remote_transmit_path,
636                                       net->tx_ring.ring->hop);
637         if (ret) {
638                 netdev_err(net->dev, "failed to enable DMA paths\n");
639                 goto err_free_tx_buffers;
640         }
641
642         netif_carrier_on(net->dev);
643         netif_start_queue(net->dev);
644         return;
645
646 err_free_tx_buffers:
647         tbnet_free_buffers(&net->tx_ring);
648 err_free_rx_buffers:
649         tbnet_free_buffers(&net->rx_ring);
650 err_stop_rings:
651         tb_ring_stop(net->rx_ring.ring);
652         tb_ring_stop(net->tx_ring.ring);
653         tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
654 }
655
656 static void tbnet_login_work(struct work_struct *work)
657 {
658         struct tbnet *net = container_of(work, typeof(*net), login_work.work);
659         unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
660         int ret;
661
662         if (netif_carrier_ok(net->dev))
663                 return;
664
665         ret = tbnet_login_request(net, net->login_retries % 4);
666         if (ret) {
667                 if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
668                         queue_delayed_work(system_long_wq, &net->login_work,
669                                            delay);
670                 } else {
671                         netdev_info(net->dev, "ThunderboltIP login timed out\n");
672                 }
673         } else {
674                 net->login_retries = 0;
675
676                 mutex_lock(&net->connection_lock);
677                 net->login_sent = true;
678                 mutex_unlock(&net->connection_lock);
679
680                 queue_work(system_long_wq, &net->connected_work);
681         }
682 }
683
684 static void tbnet_disconnect_work(struct work_struct *work)
685 {
686         struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
687
688         tbnet_tear_down(net, false);
689 }
690
691 static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
692                               const struct thunderbolt_ip_frame_header *hdr)
693 {
694         u32 frame_id, frame_count, frame_size, frame_index;
695         unsigned int size;
696
697         if (tf->frame.flags & RING_DESC_CRC_ERROR) {
698                 net->stats.rx_crc_errors++;
699                 return false;
700         } else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
701                 net->stats.rx_over_errors++;
702                 return false;
703         }
704
705         /* Should be greater than just header i.e. contains data */
706         size = tbnet_frame_size(tf);
707         if (size <= sizeof(*hdr)) {
708                 net->stats.rx_length_errors++;
709                 return false;
710         }
711
712         frame_count = le32_to_cpu(hdr->frame_count);
713         frame_size = le32_to_cpu(hdr->frame_size);
714         frame_index = le16_to_cpu(hdr->frame_index);
715         frame_id = le16_to_cpu(hdr->frame_id);
716
717         if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
718                 net->stats.rx_length_errors++;
719                 return false;
720         }
721
722         /* In case we're in the middle of packet, validate the frame
723          * header based on first fragment of the packet.
724          */
725         if (net->skb && net->rx_hdr.frame_count) {
726                 /* Check the frame count fits the count field */
727                 if (frame_count != net->rx_hdr.frame_count) {
728                         net->stats.rx_length_errors++;
729                         return false;
730                 }
731
732                 /* Check the frame identifiers are incremented correctly,
733                  * and id is matching.
734                  */
735                 if (frame_index != net->rx_hdr.frame_index + 1 ||
736                     frame_id != net->rx_hdr.frame_id) {
737                         net->stats.rx_missed_errors++;
738                         return false;
739                 }
740
741                 if (net->skb->len + frame_size > TBNET_MAX_MTU) {
742                         net->stats.rx_length_errors++;
743                         return false;
744                 }
745
746                 return true;
747         }
748
749         /* Start of packet, validate the frame header */
750         if (frame_count == 0 || frame_count > TBNET_RING_SIZE / 4) {
751                 net->stats.rx_length_errors++;
752                 return false;
753         }
754         if (frame_index != 0) {
755                 net->stats.rx_missed_errors++;
756                 return false;
757         }
758
759         return true;
760 }
761
762 static int tbnet_poll(struct napi_struct *napi, int budget)
763 {
764         struct tbnet *net = container_of(napi, struct tbnet, napi);
765         unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
766         struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
767         unsigned int rx_packets = 0;
768
769         while (rx_packets < budget) {
770                 const struct thunderbolt_ip_frame_header *hdr;
771                 unsigned int hdr_size = sizeof(*hdr);
772                 struct sk_buff *skb = NULL;
773                 struct ring_frame *frame;
774                 struct tbnet_frame *tf;
775                 struct page *page;
776                 bool last = true;
777                 u32 frame_size;
778
779                 /* Return some buffers to hardware, one at a time is too
780                  * slow so allocate MAX_SKB_FRAGS buffers at the same
781                  * time.
782                  */
783                 if (cleaned_count >= MAX_SKB_FRAGS) {
784                         tbnet_alloc_rx_buffers(net, cleaned_count);
785                         cleaned_count = 0;
786                 }
787
788                 frame = tb_ring_poll(net->rx_ring.ring);
789                 if (!frame)
790                         break;
791
792                 dma_unmap_page(dma_dev, frame->buffer_phy,
793                                TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
794
795                 tf = container_of(frame, typeof(*tf), frame);
796
797                 page = tf->page;
798                 tf->page = NULL;
799                 net->rx_ring.cons++;
800                 cleaned_count++;
801
802                 hdr = page_address(page);
803                 if (!tbnet_check_frame(net, tf, hdr)) {
804                         __free_pages(page, TBNET_RX_PAGE_ORDER);
805                         dev_kfree_skb_any(net->skb);
806                         net->skb = NULL;
807                         continue;
808                 }
809
810                 frame_size = le32_to_cpu(hdr->frame_size);
811
812                 skb = net->skb;
813                 if (!skb) {
814                         skb = build_skb(page_address(page),
815                                         TBNET_RX_PAGE_SIZE);
816                         if (!skb) {
817                                 __free_pages(page, TBNET_RX_PAGE_ORDER);
818                                 net->stats.rx_errors++;
819                                 break;
820                         }
821
822                         skb_reserve(skb, hdr_size);
823                         skb_put(skb, frame_size);
824
825                         net->skb = skb;
826                 } else {
827                         skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
828                                         page, hdr_size, frame_size,
829                                         TBNET_RX_PAGE_SIZE - hdr_size);
830                 }
831
832                 net->rx_hdr.frame_size = frame_size;
833                 net->rx_hdr.frame_count = le32_to_cpu(hdr->frame_count);
834                 net->rx_hdr.frame_index = le16_to_cpu(hdr->frame_index);
835                 net->rx_hdr.frame_id = le16_to_cpu(hdr->frame_id);
836                 last = net->rx_hdr.frame_index == net->rx_hdr.frame_count - 1;
837
838                 rx_packets++;
839                 net->stats.rx_bytes += frame_size;
840
841                 if (last) {
842                         skb->protocol = eth_type_trans(skb, net->dev);
843                         napi_gro_receive(&net->napi, skb);
844                         net->skb = NULL;
845                 }
846         }
847
848         net->stats.rx_packets += rx_packets;
849
850         if (cleaned_count)
851                 tbnet_alloc_rx_buffers(net, cleaned_count);
852
853         if (rx_packets >= budget)
854                 return budget;
855
856         napi_complete_done(napi, rx_packets);
857         /* Re-enable the ring interrupt */
858         tb_ring_poll_complete(net->rx_ring.ring);
859
860         return rx_packets;
861 }
862
863 static void tbnet_start_poll(void *data)
864 {
865         struct tbnet *net = data;
866
867         napi_schedule(&net->napi);
868 }
869
870 static int tbnet_open(struct net_device *dev)
871 {
872         struct tbnet *net = netdev_priv(dev);
873         struct tb_xdomain *xd = net->xd;
874         u16 sof_mask, eof_mask;
875         struct tb_ring *ring;
876         int hopid;
877
878         netif_carrier_off(dev);
879
880         ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE,
881                                 RING_FLAG_FRAME);
882         if (!ring) {
883                 netdev_err(dev, "failed to allocate Tx ring\n");
884                 return -ENOMEM;
885         }
886         net->tx_ring.ring = ring;
887
888         hopid = tb_xdomain_alloc_out_hopid(xd, -1);
889         if (hopid < 0) {
890                 netdev_err(dev, "failed to allocate Tx HopID\n");
891                 tb_ring_free(net->tx_ring.ring);
892                 net->tx_ring.ring = NULL;
893                 return hopid;
894         }
895         net->local_transmit_path = hopid;
896
897         sof_mask = BIT(TBIP_PDF_FRAME_START);
898         eof_mask = BIT(TBIP_PDF_FRAME_END);
899
900         ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE,
901                                 RING_FLAG_FRAME, 0, sof_mask, eof_mask,
902                                 tbnet_start_poll, net);
903         if (!ring) {
904                 netdev_err(dev, "failed to allocate Rx ring\n");
905                 tb_ring_free(net->tx_ring.ring);
906                 net->tx_ring.ring = NULL;
907                 return -ENOMEM;
908         }
909         net->rx_ring.ring = ring;
910
911         napi_enable(&net->napi);
912         start_login(net);
913
914         return 0;
915 }
916
917 static int tbnet_stop(struct net_device *dev)
918 {
919         struct tbnet *net = netdev_priv(dev);
920
921         napi_disable(&net->napi);
922
923         cancel_work_sync(&net->disconnect_work);
924         tbnet_tear_down(net, true);
925
926         tb_ring_free(net->rx_ring.ring);
927         net->rx_ring.ring = NULL;
928
929         tb_xdomain_release_out_hopid(net->xd, net->local_transmit_path);
930         tb_ring_free(net->tx_ring.ring);
931         net->tx_ring.ring = NULL;
932
933         return 0;
934 }
935
936 static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
937         struct tbnet_frame **frames, u32 frame_count)
938 {
939         struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
940         struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
941         __wsum wsum = htonl(skb->len - skb_transport_offset(skb));
942         unsigned int i, len, offset = skb_transport_offset(skb);
943         __be16 protocol = skb->protocol;
944         void *data = skb->data;
945         void *dest = hdr + 1;
946         __sum16 *tucso;
947
948         if (skb->ip_summed != CHECKSUM_PARTIAL) {
949                 /* No need to calculate checksum so we just update the
950                  * total frame count and sync the frames for DMA.
951                  */
952                 for (i = 0; i < frame_count; i++) {
953                         hdr = page_address(frames[i]->page);
954                         hdr->frame_count = cpu_to_le32(frame_count);
955                         dma_sync_single_for_device(dma_dev,
956                                 frames[i]->frame.buffer_phy,
957                                 tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
958                 }
959
960                 return true;
961         }
962
963         if (protocol == htons(ETH_P_8021Q)) {
964                 struct vlan_hdr *vhdr, vh;
965
966                 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh);
967                 if (!vhdr)
968                         return false;
969
970                 protocol = vhdr->h_vlan_encapsulated_proto;
971         }
972
973         /* Data points on the beginning of packet.
974          * Check is the checksum absolute place in the packet.
975          * ipcso will update IP checksum.
976          * tucso will update TCP/UPD checksum.
977          */
978         if (protocol == htons(ETH_P_IP)) {
979                 __sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data);
980
981                 *ipcso = 0;
982                 *ipcso = ip_fast_csum(dest + skb_network_offset(skb),
983                                       ip_hdr(skb)->ihl);
984
985                 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
986                         tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
987                 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
988                         tucso = dest + ((void *)&(udp_hdr(skb)->check) - data);
989                 else
990                         return false;
991
992                 *tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
993                                             ip_hdr(skb)->daddr, 0,
994                                             ip_hdr(skb)->protocol, 0);
995         } else if (skb_is_gso_v6(skb)) {
996                 tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
997                 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
998                                           &ipv6_hdr(skb)->daddr, 0,
999                                           IPPROTO_TCP, 0);
1000                 return false;
1001         } else if (protocol == htons(ETH_P_IPV6)) {
1002                 tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset;
1003                 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1004                                           &ipv6_hdr(skb)->daddr, 0,
1005                                           ipv6_hdr(skb)->nexthdr, 0);
1006         } else {
1007                 return false;
1008         }
1009
1010         /* First frame was headers, rest of the frames contain data.
1011          * Calculate checksum over each frame.
1012          */
1013         for (i = 0; i < frame_count; i++) {
1014                 hdr = page_address(frames[i]->page);
1015                 dest = (void *)(hdr + 1) + offset;
1016                 len = le32_to_cpu(hdr->frame_size) - offset;
1017                 wsum = csum_partial(dest, len, wsum);
1018                 hdr->frame_count = cpu_to_le32(frame_count);
1019
1020                 offset = 0;
1021         }
1022
1023         *tucso = csum_fold(wsum);
1024
1025         /* Checksum is finally calculated and we don't touch the memory
1026          * anymore, so DMA sync the frames now.
1027          */
1028         for (i = 0; i < frame_count; i++) {
1029                 dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy,
1030                         tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
1031         }
1032
1033         return true;
1034 }
1035
1036 static void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num,
1037                              unsigned int *len)
1038 {
1039         const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
1040
1041         *len = skb_frag_size(frag);
1042         return kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1043 }
1044
1045 static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
1046                                     struct net_device *dev)
1047 {
1048         struct tbnet *net = netdev_priv(dev);
1049         struct tbnet_frame *frames[MAX_SKB_FRAGS];
1050         u16 frame_id = atomic_read(&net->frame_id);
1051         struct thunderbolt_ip_frame_header *hdr;
1052         unsigned int len = skb_headlen(skb);
1053         unsigned int data_len = skb->len;
1054         unsigned int nframes, i;
1055         unsigned int frag = 0;
1056         void *src = skb->data;
1057         u32 frame_index = 0;
1058         bool unmap = false;
1059         void *dest;
1060
1061         nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE);
1062         if (tbnet_available_buffers(&net->tx_ring) < nframes) {
1063                 netif_stop_queue(net->dev);
1064                 return NETDEV_TX_BUSY;
1065         }
1066
1067         frames[frame_index] = tbnet_get_tx_buffer(net);
1068         if (!frames[frame_index])
1069                 goto err_drop;
1070
1071         hdr = page_address(frames[frame_index]->page);
1072         dest = hdr + 1;
1073
1074         /* If overall packet is bigger than the frame data size */
1075         while (data_len > TBNET_MAX_PAYLOAD_SIZE) {
1076                 unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE;
1077
1078                 hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE);
1079                 hdr->frame_index = cpu_to_le16(frame_index);
1080                 hdr->frame_id = cpu_to_le16(frame_id);
1081
1082                 do {
1083                         if (len > size_left) {
1084                                 /* Copy data onto Tx buffer data with
1085                                  * full frame size then break and go to
1086                                  * next frame
1087                                  */
1088                                 memcpy(dest, src, size_left);
1089                                 len -= size_left;
1090                                 dest += size_left;
1091                                 src += size_left;
1092                                 break;
1093                         }
1094
1095                         memcpy(dest, src, len);
1096                         size_left -= len;
1097                         dest += len;
1098
1099                         if (unmap) {
1100                                 kunmap_atomic(src);
1101                                 unmap = false;
1102                         }
1103
1104                         /* Ensure all fragments have been processed */
1105                         if (frag < skb_shinfo(skb)->nr_frags) {
1106                                 /* Map and then unmap quickly */
1107                                 src = tbnet_kmap_frag(skb, frag++, &len);
1108                                 unmap = true;
1109                         } else if (unlikely(size_left > 0)) {
1110                                 goto err_drop;
1111                         }
1112                 } while (size_left > 0);
1113
1114                 data_len -= TBNET_MAX_PAYLOAD_SIZE;
1115                 frame_index++;
1116
1117                 frames[frame_index] = tbnet_get_tx_buffer(net);
1118                 if (!frames[frame_index])
1119                         goto err_drop;
1120
1121                 hdr = page_address(frames[frame_index]->page);
1122                 dest = hdr + 1;
1123         }
1124
1125         hdr->frame_size = cpu_to_le32(data_len);
1126         hdr->frame_index = cpu_to_le16(frame_index);
1127         hdr->frame_id = cpu_to_le16(frame_id);
1128
1129         frames[frame_index]->frame.size = data_len + sizeof(*hdr);
1130
1131         /* In case the remaining data_len is smaller than a frame */
1132         while (len < data_len) {
1133                 memcpy(dest, src, len);
1134                 data_len -= len;
1135                 dest += len;
1136
1137                 if (unmap) {
1138                         kunmap_atomic(src);
1139                         unmap = false;
1140                 }
1141
1142                 if (frag < skb_shinfo(skb)->nr_frags) {
1143                         src = tbnet_kmap_frag(skb, frag++, &len);
1144                         unmap = true;
1145                 } else if (unlikely(data_len > 0)) {
1146                         goto err_drop;
1147                 }
1148         }
1149
1150         memcpy(dest, src, data_len);
1151
1152         if (unmap)
1153                 kunmap_atomic(src);
1154
1155         if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
1156                 goto err_drop;
1157
1158         for (i = 0; i < frame_index + 1; i++)
1159                 tb_ring_tx(net->tx_ring.ring, &frames[i]->frame);
1160
1161         if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID)
1162                 atomic_inc(&net->frame_id);
1163
1164         net->stats.tx_packets++;
1165         net->stats.tx_bytes += skb->len;
1166
1167         dev_consume_skb_any(skb);
1168
1169         return NETDEV_TX_OK;
1170
1171 err_drop:
1172         /* We can re-use the buffers */
1173         net->tx_ring.cons -= frame_index;
1174
1175         dev_kfree_skb_any(skb);
1176         net->stats.tx_errors++;
1177
1178         return NETDEV_TX_OK;
1179 }
1180
1181 static void tbnet_get_stats64(struct net_device *dev,
1182                               struct rtnl_link_stats64 *stats)
1183 {
1184         struct tbnet *net = netdev_priv(dev);
1185
1186         stats->tx_packets = net->stats.tx_packets;
1187         stats->rx_packets = net->stats.rx_packets;
1188         stats->tx_bytes = net->stats.tx_bytes;
1189         stats->rx_bytes = net->stats.rx_bytes;
1190         stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors +
1191                 net->stats.rx_over_errors + net->stats.rx_crc_errors +
1192                 net->stats.rx_missed_errors;
1193         stats->tx_errors = net->stats.tx_errors;
1194         stats->rx_length_errors = net->stats.rx_length_errors;
1195         stats->rx_over_errors = net->stats.rx_over_errors;
1196         stats->rx_crc_errors = net->stats.rx_crc_errors;
1197         stats->rx_missed_errors = net->stats.rx_missed_errors;
1198 }
1199
1200 static const struct net_device_ops tbnet_netdev_ops = {
1201         .ndo_open = tbnet_open,
1202         .ndo_stop = tbnet_stop,
1203         .ndo_start_xmit = tbnet_start_xmit,
1204         .ndo_get_stats64 = tbnet_get_stats64,
1205 };
1206
1207 static void tbnet_generate_mac(struct net_device *dev)
1208 {
1209         const struct tbnet *net = netdev_priv(dev);
1210         const struct tb_xdomain *xd = net->xd;
1211         u8 phy_port;
1212         u32 hash;
1213
1214         phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route));
1215
1216         /* Unicast and locally administered MAC */
1217         dev->dev_addr[0] = phy_port << 4 | 0x02;
1218         hash = jhash2((u32 *)xd->local_uuid, 4, 0);
1219         memcpy(dev->dev_addr + 1, &hash, sizeof(hash));
1220         hash = jhash2((u32 *)xd->local_uuid, 4, hash);
1221         dev->dev_addr[5] = hash & 0xff;
1222 }
1223
1224 static int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id)
1225 {
1226         struct tb_xdomain *xd = tb_service_parent(svc);
1227         struct net_device *dev;
1228         struct tbnet *net;
1229         int ret;
1230
1231         dev = alloc_etherdev(sizeof(*net));
1232         if (!dev)
1233                 return -ENOMEM;
1234
1235         SET_NETDEV_DEV(dev, &svc->dev);
1236
1237         net = netdev_priv(dev);
1238         INIT_DELAYED_WORK(&net->login_work, tbnet_login_work);
1239         INIT_WORK(&net->connected_work, tbnet_connected_work);
1240         INIT_WORK(&net->disconnect_work, tbnet_disconnect_work);
1241         mutex_init(&net->connection_lock);
1242         atomic_set(&net->command_id, 0);
1243         atomic_set(&net->frame_id, 0);
1244         net->svc = svc;
1245         net->dev = dev;
1246         net->xd = xd;
1247
1248         tbnet_generate_mac(dev);
1249
1250         strcpy(dev->name, "thunderbolt%d");
1251         dev->netdev_ops = &tbnet_netdev_ops;
1252
1253         /* ThunderboltIP takes advantage of TSO packets but instead of
1254          * segmenting them we just split the packet into Thunderbolt
1255          * frames (maximum payload size of each frame is 4084 bytes) and
1256          * calculate checksum over the whole packet here.
1257          *
1258          * The receiving side does the opposite if the host OS supports
1259          * LRO, otherwise it needs to split the large packet into MTU
1260          * sized smaller packets.
1261          *
1262          * In order to receive large packets from the networking stack,
1263          * we need to announce support for most of the offloading
1264          * features here.
1265          */
1266         dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO |
1267                            NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1268         dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1269         dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header);
1270
1271         netif_napi_add(dev, &net->napi, tbnet_poll, NAPI_POLL_WEIGHT);
1272
1273         /* MTU range: 68 - 65522 */
1274         dev->min_mtu = ETH_MIN_MTU;
1275         dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN;
1276
1277         net->handler.uuid = &tbnet_svc_uuid;
1278         net->handler.callback = tbnet_handle_packet;
1279         net->handler.data = net;
1280         tb_register_protocol_handler(&net->handler);
1281
1282         tb_service_set_drvdata(svc, net);
1283
1284         ret = register_netdev(dev);
1285         if (ret) {
1286                 tb_unregister_protocol_handler(&net->handler);
1287                 free_netdev(dev);
1288                 return ret;
1289         }
1290
1291         return 0;
1292 }
1293
1294 static void tbnet_remove(struct tb_service *svc)
1295 {
1296         struct tbnet *net = tb_service_get_drvdata(svc);
1297
1298         unregister_netdev(net->dev);
1299         tb_unregister_protocol_handler(&net->handler);
1300         free_netdev(net->dev);
1301 }
1302
1303 static void tbnet_shutdown(struct tb_service *svc)
1304 {
1305         tbnet_tear_down(tb_service_get_drvdata(svc), true);
1306 }
1307
1308 static int __maybe_unused tbnet_suspend(struct device *dev)
1309 {
1310         struct tb_service *svc = tb_to_service(dev);
1311         struct tbnet *net = tb_service_get_drvdata(svc);
1312
1313         stop_login(net);
1314         if (netif_running(net->dev)) {
1315                 netif_device_detach(net->dev);
1316                 tbnet_tear_down(net, true);
1317         }
1318
1319         tb_unregister_protocol_handler(&net->handler);
1320         return 0;
1321 }
1322
1323 static int __maybe_unused tbnet_resume(struct device *dev)
1324 {
1325         struct tb_service *svc = tb_to_service(dev);
1326         struct tbnet *net = tb_service_get_drvdata(svc);
1327
1328         tb_register_protocol_handler(&net->handler);
1329
1330         netif_carrier_off(net->dev);
1331         if (netif_running(net->dev)) {
1332                 netif_device_attach(net->dev);
1333                 start_login(net);
1334         }
1335
1336         return 0;
1337 }
1338
1339 static const struct dev_pm_ops tbnet_pm_ops = {
1340         SET_SYSTEM_SLEEP_PM_OPS(tbnet_suspend, tbnet_resume)
1341 };
1342
1343 static const struct tb_service_id tbnet_ids[] = {
1344         { TB_SERVICE("network", 1) },
1345         { },
1346 };
1347 MODULE_DEVICE_TABLE(tbsvc, tbnet_ids);
1348
1349 static struct tb_service_driver tbnet_driver = {
1350         .driver = {
1351                 .owner = THIS_MODULE,
1352                 .name = "thunderbolt-net",
1353                 .pm = &tbnet_pm_ops,
1354         },
1355         .probe = tbnet_probe,
1356         .remove = tbnet_remove,
1357         .shutdown = tbnet_shutdown,
1358         .id_table = tbnet_ids,
1359 };
1360
1361 static int __init tbnet_init(void)
1362 {
1363         int ret;
1364
1365         tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid);
1366         if (!tbnet_dir)
1367                 return -ENOMEM;
1368
1369         tb_property_add_immediate(tbnet_dir, "prtcid", 1);
1370         tb_property_add_immediate(tbnet_dir, "prtcvers", 1);
1371         tb_property_add_immediate(tbnet_dir, "prtcrevs", 1);
1372         /* Currently only announce support for match frags ID (bit 1). Bit 0
1373          * is reserved for full E2E flow control which we do not support at
1374          * the moment.
1375          */
1376         tb_property_add_immediate(tbnet_dir, "prtcstns",
1377                                   TBNET_MATCH_FRAGS_ID | TBNET_64K_FRAMES);
1378
1379         ret = tb_register_property_dir("network", tbnet_dir);
1380         if (ret) {
1381                 tb_property_free_dir(tbnet_dir);
1382                 return ret;
1383         }
1384
1385         return tb_register_service_driver(&tbnet_driver);
1386 }
1387 module_init(tbnet_init);
1388
1389 static void __exit tbnet_exit(void)
1390 {
1391         tb_unregister_service_driver(&tbnet_driver);
1392         tb_unregister_property_dir("network", tbnet_dir);
1393         tb_property_free_dir(tbnet_dir);
1394 }
1395 module_exit(tbnet_exit);
1396
1397 MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
1398 MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
1399 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1400 MODULE_DESCRIPTION("Thunderbolt network driver");
1401 MODULE_LICENSE("GPL v2");