1 // SPDX-License-Identifier: GPL-2.0
3 * Networking over Thunderbolt/USB4 cables using USB4NET protocol
4 * (formerly Apple ThunderboltIP).
6 * Copyright (C) 2017, Intel Corporation
7 * Authors: Amir Levy <amir.jer.levy@intel.com>
8 * Michael Jamet <michael.jamet@intel.com>
9 * Mika Westerberg <mika.westerberg@linux.intel.com>
12 #include <linux/atomic.h>
13 #include <linux/highmem.h>
14 #include <linux/if_vlan.h>
15 #include <linux/jhash.h>
16 #include <linux/module.h>
17 #include <linux/etherdevice.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/sizes.h>
20 #include <linux/thunderbolt.h>
21 #include <linux/uuid.h>
22 #include <linux/workqueue.h>
24 #include <net/ip6_checksum.h>
26 /* Protocol timeouts in ms */
27 #define TBNET_LOGIN_DELAY 4500
28 #define TBNET_LOGIN_TIMEOUT 500
29 #define TBNET_LOGOUT_TIMEOUT 1000
31 #define TBNET_RING_SIZE 256
32 #define TBNET_LOGIN_RETRIES 60
33 #define TBNET_LOGOUT_RETRIES 10
34 #define TBNET_E2E BIT(0)
35 #define TBNET_MATCH_FRAGS_ID BIT(1)
36 #define TBNET_64K_FRAMES BIT(2)
37 #define TBNET_MAX_MTU SZ_64K
38 #define TBNET_FRAME_SIZE SZ_4K
39 #define TBNET_MAX_PAYLOAD_SIZE \
40 (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
41 /* Rx packets need to hold space for skb_shared_info */
42 #define TBNET_RX_MAX_SIZE \
43 (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
44 #define TBNET_RX_PAGE_ORDER get_order(TBNET_RX_MAX_SIZE)
45 #define TBNET_RX_PAGE_SIZE (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
47 #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
50 * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
51 * @frame_size: size of the data with the frame
52 * @frame_index: running index on the frames
53 * @frame_id: ID of the frame to match frames to specific packet
54 * @frame_count: how many frames assembles a full packet
56 * Each data frame passed to the high-speed DMA ring has this header. If
57 * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
58 * supported then @frame_id is filled, otherwise it stays %0.
60 struct thunderbolt_ip_frame_header {
67 enum thunderbolt_ip_frame_pdf {
68 TBIP_PDF_FRAME_START = 1,
72 enum thunderbolt_ip_type {
79 struct thunderbolt_ip_header {
84 uuid_t initiator_uuid;
90 #define TBIP_HDR_LENGTH_MASK GENMASK(5, 0)
91 #define TBIP_HDR_SN_MASK GENMASK(28, 27)
92 #define TBIP_HDR_SN_SHIFT 27
94 struct thunderbolt_ip_login {
95 struct thunderbolt_ip_header hdr;
101 #define TBIP_LOGIN_PROTO_VERSION 1
103 struct thunderbolt_ip_login_response {
104 struct thunderbolt_ip_header hdr;
107 u32 receiver_mac_len;
111 struct thunderbolt_ip_logout {
112 struct thunderbolt_ip_header hdr;
115 struct thunderbolt_ip_status {
116 struct thunderbolt_ip_header hdr;
127 u64 rx_length_errors;
130 u64 rx_missed_errors;
134 struct net_device *dev;
136 struct ring_frame frame;
140 struct tbnet_frame frames[TBNET_RING_SIZE];
143 struct tb_ring *ring;
147 * struct tbnet - ThunderboltIP network driver private data
148 * @svc: XDomain service the driver is bound to
149 * @xd: XDomain the service blongs to
150 * @handler: ThunderboltIP configuration protocol handler
151 * @dev: Networking device
152 * @napi: NAPI structure for Rx polling
153 * @stats: Network statistics
154 * @skb: Network packet that is currently processed on Rx path
155 * @command_id: ID used for next configuration protocol packet
156 * @login_sent: ThunderboltIP login message successfully sent
157 * @login_received: ThunderboltIP login message received from the remote
159 * @local_transmit_path: HopID we are using to send out packets
160 * @remote_transmit_path: HopID the other end is using to send packets to us
161 * @connection_lock: Lock serializing access to @login_sent,
162 * @login_received and @transmit_path.
163 * @login_retries: Number of login retries currently done
164 * @login_work: Worker to send ThunderboltIP login packets
165 * @connected_work: Worker that finalizes the ThunderboltIP connection
166 * setup and enables DMA paths for high speed data
168 * @disconnect_work: Worker that handles tearing down the ThunderboltIP
170 * @rx_hdr: Copy of the currently processed Rx frame. Used when a
171 * network packet consists of multiple Thunderbolt frames.
172 * In host byte order.
173 * @rx_ring: Software ring holding Rx frames
174 * @frame_id: Frame ID use for next Tx packet
175 * (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
176 * @tx_ring: Software ring holding Tx frames
179 const struct tb_service *svc;
180 struct tb_xdomain *xd;
181 struct tb_protocol_handler handler;
182 struct net_device *dev;
183 struct napi_struct napi;
184 struct tbnet_stats stats;
189 int local_transmit_path;
190 int remote_transmit_path;
191 struct mutex connection_lock;
193 struct delayed_work login_work;
194 struct work_struct connected_work;
195 struct work_struct disconnect_work;
196 struct thunderbolt_ip_frame_header rx_hdr;
197 struct tbnet_ring rx_ring;
199 struct tbnet_ring tx_ring;
202 /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
203 static const uuid_t tbnet_dir_uuid =
204 UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
205 0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
207 /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
208 static const uuid_t tbnet_svc_uuid =
209 UUID_INIT(0x798f589e, 0x3616, 0x8a47,
210 0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
212 static struct tb_property_dir *tbnet_dir;
214 static bool tbnet_e2e = true;
215 module_param_named(e2e, tbnet_e2e, bool, 0444);
216 MODULE_PARM_DESC(e2e, "USB4NET full end-to-end flow control (default: true)");
218 static void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
219 u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
220 enum thunderbolt_ip_type type, size_t size, u32 command_id)
224 /* Length does not include route_hi/lo and length_sn fields */
225 length_sn = (size - 3 * 4) / 4;
226 length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
228 hdr->route_hi = upper_32_bits(route);
229 hdr->route_lo = lower_32_bits(route);
230 hdr->length_sn = length_sn;
231 uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
232 uuid_copy(&hdr->initiator_uuid, initiator_uuid);
233 uuid_copy(&hdr->target_uuid, target_uuid);
235 hdr->command_id = command_id;
238 static int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
241 struct thunderbolt_ip_login_response reply;
242 struct tb_xdomain *xd = net->xd;
244 memset(&reply, 0, sizeof(reply));
245 tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
246 xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
248 memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
249 reply.receiver_mac_len = ETH_ALEN;
251 return tb_xdomain_response(xd, &reply, sizeof(reply),
252 TB_CFG_PKG_XDOMAIN_RESP);
255 static int tbnet_login_request(struct tbnet *net, u8 sequence)
257 struct thunderbolt_ip_login_response reply;
258 struct thunderbolt_ip_login request;
259 struct tb_xdomain *xd = net->xd;
261 memset(&request, 0, sizeof(request));
262 tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
263 xd->remote_uuid, TBIP_LOGIN, sizeof(request),
264 atomic_inc_return(&net->command_id));
266 request.proto_version = TBIP_LOGIN_PROTO_VERSION;
267 request.transmit_path = net->local_transmit_path;
269 return tb_xdomain_request(xd, &request, sizeof(request),
270 TB_CFG_PKG_XDOMAIN_RESP, &reply,
271 sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
272 TBNET_LOGIN_TIMEOUT);
275 static int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
278 struct thunderbolt_ip_status reply;
279 struct tb_xdomain *xd = net->xd;
281 memset(&reply, 0, sizeof(reply));
282 tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
283 xd->remote_uuid, TBIP_STATUS, sizeof(reply),
284 atomic_inc_return(&net->command_id));
285 return tb_xdomain_response(xd, &reply, sizeof(reply),
286 TB_CFG_PKG_XDOMAIN_RESP);
289 static int tbnet_logout_request(struct tbnet *net)
291 struct thunderbolt_ip_logout request;
292 struct thunderbolt_ip_status reply;
293 struct tb_xdomain *xd = net->xd;
295 memset(&request, 0, sizeof(request));
296 tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
297 xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
298 atomic_inc_return(&net->command_id));
300 return tb_xdomain_request(xd, &request, sizeof(request),
301 TB_CFG_PKG_XDOMAIN_RESP, &reply,
302 sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
303 TBNET_LOGOUT_TIMEOUT);
306 static void start_login(struct tbnet *net)
308 mutex_lock(&net->connection_lock);
309 net->login_sent = false;
310 net->login_received = false;
311 mutex_unlock(&net->connection_lock);
313 queue_delayed_work(system_long_wq, &net->login_work,
314 msecs_to_jiffies(1000));
317 static void stop_login(struct tbnet *net)
319 cancel_delayed_work_sync(&net->login_work);
320 cancel_work_sync(&net->connected_work);
323 static inline unsigned int tbnet_frame_size(const struct tbnet_frame *tf)
325 return tf->frame.size ? : TBNET_FRAME_SIZE;
328 static void tbnet_free_buffers(struct tbnet_ring *ring)
332 for (i = 0; i < TBNET_RING_SIZE; i++) {
333 struct device *dma_dev = tb_ring_dma_device(ring->ring);
334 struct tbnet_frame *tf = &ring->frames[i];
335 enum dma_data_direction dir;
342 if (ring->ring->is_tx) {
345 size = TBNET_FRAME_SIZE;
347 dir = DMA_FROM_DEVICE;
348 order = TBNET_RX_PAGE_ORDER;
349 size = TBNET_RX_PAGE_SIZE;
352 if (tf->frame.buffer_phy)
353 dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
356 __free_pages(tf->page, order);
364 static void tbnet_tear_down(struct tbnet *net, bool send_logout)
366 netif_carrier_off(net->dev);
367 netif_stop_queue(net->dev);
371 mutex_lock(&net->connection_lock);
373 if (net->login_sent && net->login_received) {
374 int ret, retries = TBNET_LOGOUT_RETRIES;
376 while (send_logout && retries-- > 0) {
377 ret = tbnet_logout_request(net);
378 if (ret != -ETIMEDOUT)
382 tb_ring_stop(net->rx_ring.ring);
383 tb_ring_stop(net->tx_ring.ring);
384 tbnet_free_buffers(&net->rx_ring);
385 tbnet_free_buffers(&net->tx_ring);
387 ret = tb_xdomain_disable_paths(net->xd,
388 net->local_transmit_path,
389 net->rx_ring.ring->hop,
390 net->remote_transmit_path,
391 net->tx_ring.ring->hop);
393 netdev_warn(net->dev, "failed to disable DMA paths\n");
395 tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
396 net->remote_transmit_path = 0;
399 net->login_retries = 0;
400 net->login_sent = false;
401 net->login_received = false;
403 mutex_unlock(&net->connection_lock);
406 static int tbnet_handle_packet(const void *buf, size_t size, void *data)
408 const struct thunderbolt_ip_login *pkg = buf;
409 struct tbnet *net = data;
415 /* Make sure the packet is for us */
416 if (size < sizeof(struct thunderbolt_ip_header))
418 if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
420 if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
423 route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
424 route &= ~BIT_ULL(63);
425 if (route != net->xd->route)
428 sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
429 sequence >>= TBIP_HDR_SN_SHIFT;
430 command_id = pkg->hdr.command_id;
432 switch (pkg->hdr.type) {
434 if (!netif_running(net->dev))
437 ret = tbnet_login_response(net, route, sequence,
438 pkg->hdr.command_id);
440 mutex_lock(&net->connection_lock);
441 net->login_received = true;
442 net->remote_transmit_path = pkg->transmit_path;
444 /* If we reached the number of max retries or
445 * previous logout, schedule another round of
448 if (net->login_retries >= TBNET_LOGIN_RETRIES ||
450 net->login_retries = 0;
451 queue_delayed_work(system_long_wq,
452 &net->login_work, 0);
454 mutex_unlock(&net->connection_lock);
456 queue_work(system_long_wq, &net->connected_work);
461 ret = tbnet_logout_response(net, route, sequence, command_id);
463 queue_work(system_long_wq, &net->disconnect_work);
471 netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
476 static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
478 return ring->prod - ring->cons;
481 static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
483 struct tbnet_ring *ring = &net->rx_ring;
487 struct device *dma_dev = tb_ring_dma_device(ring->ring);
488 unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
489 struct tbnet_frame *tf = &ring->frames[index];
495 /* Allocate page (order > 0) so that it can hold maximum
496 * ThunderboltIP frame (4kB) and the additional room for
497 * SKB shared info required by build_skb().
499 tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
505 dma_addr = dma_map_page(dma_dev, tf->page, 0,
506 TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
507 if (dma_mapping_error(dma_dev, dma_addr)) {
512 tf->frame.buffer_phy = dma_addr;
515 tb_ring_rx(ring->ring, &tf->frame);
523 tbnet_free_buffers(ring);
527 static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
529 struct tbnet_ring *ring = &net->tx_ring;
530 struct device *dma_dev = tb_ring_dma_device(ring->ring);
531 struct tbnet_frame *tf;
534 if (!tbnet_available_buffers(ring))
537 index = ring->cons++ & (TBNET_RING_SIZE - 1);
539 tf = &ring->frames[index];
542 dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
543 tbnet_frame_size(tf), DMA_TO_DEVICE);
548 static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
551 struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
552 struct tbnet *net = netdev_priv(tf->dev);
554 /* Return buffer to the ring */
557 if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
558 netif_wake_queue(net->dev);
561 static int tbnet_alloc_tx_buffers(struct tbnet *net)
563 struct tbnet_ring *ring = &net->tx_ring;
564 struct device *dma_dev = tb_ring_dma_device(ring->ring);
567 for (i = 0; i < TBNET_RING_SIZE; i++) {
568 struct tbnet_frame *tf = &ring->frames[i];
571 tf->page = alloc_page(GFP_KERNEL);
573 tbnet_free_buffers(ring);
577 dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
579 if (dma_mapping_error(dma_dev, dma_addr)) {
580 __free_page(tf->page);
582 tbnet_free_buffers(ring);
587 tf->frame.buffer_phy = dma_addr;
588 tf->frame.callback = tbnet_tx_callback;
589 tf->frame.sof = TBIP_PDF_FRAME_START;
590 tf->frame.eof = TBIP_PDF_FRAME_END;
594 ring->prod = TBNET_RING_SIZE - 1;
599 static void tbnet_connected_work(struct work_struct *work)
601 struct tbnet *net = container_of(work, typeof(*net), connected_work);
605 if (netif_carrier_ok(net->dev))
608 mutex_lock(&net->connection_lock);
609 connected = net->login_sent && net->login_received;
610 mutex_unlock(&net->connection_lock);
615 ret = tb_xdomain_alloc_in_hopid(net->xd, net->remote_transmit_path);
616 if (ret != net->remote_transmit_path) {
617 netdev_err(net->dev, "failed to allocate Rx HopID\n");
621 /* Both logins successful so enable the rings, high-speed DMA
622 * paths and start the network device queue.
624 * Note we enable the DMA paths last to make sure we have primed
625 * the Rx ring before any incoming packets are allowed to
628 tb_ring_start(net->tx_ring.ring);
629 tb_ring_start(net->rx_ring.ring);
631 ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
635 ret = tbnet_alloc_tx_buffers(net);
637 goto err_free_rx_buffers;
639 ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
640 net->rx_ring.ring->hop,
641 net->remote_transmit_path,
642 net->tx_ring.ring->hop);
644 netdev_err(net->dev, "failed to enable DMA paths\n");
645 goto err_free_tx_buffers;
648 netif_carrier_on(net->dev);
649 netif_start_queue(net->dev);
653 tbnet_free_buffers(&net->tx_ring);
655 tbnet_free_buffers(&net->rx_ring);
657 tb_ring_stop(net->rx_ring.ring);
658 tb_ring_stop(net->tx_ring.ring);
659 tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
662 static void tbnet_login_work(struct work_struct *work)
664 struct tbnet *net = container_of(work, typeof(*net), login_work.work);
665 unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
668 if (netif_carrier_ok(net->dev))
671 ret = tbnet_login_request(net, net->login_retries % 4);
673 if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
674 queue_delayed_work(system_long_wq, &net->login_work,
677 netdev_info(net->dev, "ThunderboltIP login timed out\n");
680 net->login_retries = 0;
682 mutex_lock(&net->connection_lock);
683 net->login_sent = true;
684 mutex_unlock(&net->connection_lock);
686 queue_work(system_long_wq, &net->connected_work);
690 static void tbnet_disconnect_work(struct work_struct *work)
692 struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
694 tbnet_tear_down(net, false);
697 static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
698 const struct thunderbolt_ip_frame_header *hdr)
700 u32 frame_id, frame_count, frame_size, frame_index;
703 if (tf->frame.flags & RING_DESC_CRC_ERROR) {
704 net->stats.rx_crc_errors++;
706 } else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
707 net->stats.rx_over_errors++;
711 /* Should be greater than just header i.e. contains data */
712 size = tbnet_frame_size(tf);
713 if (size <= sizeof(*hdr)) {
714 net->stats.rx_length_errors++;
718 frame_count = le32_to_cpu(hdr->frame_count);
719 frame_size = le32_to_cpu(hdr->frame_size);
720 frame_index = le16_to_cpu(hdr->frame_index);
721 frame_id = le16_to_cpu(hdr->frame_id);
723 if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
724 net->stats.rx_length_errors++;
728 /* In case we're in the middle of packet, validate the frame
729 * header based on first fragment of the packet.
731 if (net->skb && net->rx_hdr.frame_count) {
732 /* Check the frame count fits the count field */
733 if (frame_count != net->rx_hdr.frame_count) {
734 net->stats.rx_length_errors++;
738 /* Check the frame identifiers are incremented correctly,
739 * and id is matching.
741 if (frame_index != net->rx_hdr.frame_index + 1 ||
742 frame_id != net->rx_hdr.frame_id) {
743 net->stats.rx_missed_errors++;
747 if (net->skb->len + frame_size > TBNET_MAX_MTU) {
748 net->stats.rx_length_errors++;
755 /* Start of packet, validate the frame header */
756 if (frame_count == 0 || frame_count > TBNET_RING_SIZE / 4) {
757 net->stats.rx_length_errors++;
760 if (frame_index != 0) {
761 net->stats.rx_missed_errors++;
768 static int tbnet_poll(struct napi_struct *napi, int budget)
770 struct tbnet *net = container_of(napi, struct tbnet, napi);
771 unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
772 struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
773 unsigned int rx_packets = 0;
775 while (rx_packets < budget) {
776 const struct thunderbolt_ip_frame_header *hdr;
777 unsigned int hdr_size = sizeof(*hdr);
778 struct sk_buff *skb = NULL;
779 struct ring_frame *frame;
780 struct tbnet_frame *tf;
785 /* Return some buffers to hardware, one at a time is too
786 * slow so allocate MAX_SKB_FRAGS buffers at the same
789 if (cleaned_count >= MAX_SKB_FRAGS) {
790 tbnet_alloc_rx_buffers(net, cleaned_count);
794 frame = tb_ring_poll(net->rx_ring.ring);
798 dma_unmap_page(dma_dev, frame->buffer_phy,
799 TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
801 tf = container_of(frame, typeof(*tf), frame);
808 hdr = page_address(page);
809 if (!tbnet_check_frame(net, tf, hdr)) {
810 __free_pages(page, TBNET_RX_PAGE_ORDER);
811 dev_kfree_skb_any(net->skb);
816 frame_size = le32_to_cpu(hdr->frame_size);
820 skb = build_skb(page_address(page),
823 __free_pages(page, TBNET_RX_PAGE_ORDER);
824 net->stats.rx_errors++;
828 skb_reserve(skb, hdr_size);
829 skb_put(skb, frame_size);
833 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
834 page, hdr_size, frame_size,
835 TBNET_RX_PAGE_SIZE - hdr_size);
838 net->rx_hdr.frame_size = frame_size;
839 net->rx_hdr.frame_count = le32_to_cpu(hdr->frame_count);
840 net->rx_hdr.frame_index = le16_to_cpu(hdr->frame_index);
841 net->rx_hdr.frame_id = le16_to_cpu(hdr->frame_id);
842 last = net->rx_hdr.frame_index == net->rx_hdr.frame_count - 1;
845 net->stats.rx_bytes += frame_size;
848 skb->protocol = eth_type_trans(skb, net->dev);
849 napi_gro_receive(&net->napi, skb);
854 net->stats.rx_packets += rx_packets;
857 tbnet_alloc_rx_buffers(net, cleaned_count);
859 if (rx_packets >= budget)
862 napi_complete_done(napi, rx_packets);
863 /* Re-enable the ring interrupt */
864 tb_ring_poll_complete(net->rx_ring.ring);
869 static void tbnet_start_poll(void *data)
871 struct tbnet *net = data;
873 napi_schedule(&net->napi);
876 static int tbnet_open(struct net_device *dev)
878 struct tbnet *net = netdev_priv(dev);
879 struct tb_xdomain *xd = net->xd;
880 u16 sof_mask, eof_mask;
881 struct tb_ring *ring;
885 netif_carrier_off(dev);
887 ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE,
890 netdev_err(dev, "failed to allocate Tx ring\n");
893 net->tx_ring.ring = ring;
895 hopid = tb_xdomain_alloc_out_hopid(xd, -1);
897 netdev_err(dev, "failed to allocate Tx HopID\n");
898 tb_ring_free(net->tx_ring.ring);
899 net->tx_ring.ring = NULL;
902 net->local_transmit_path = hopid;
904 sof_mask = BIT(TBIP_PDF_FRAME_START);
905 eof_mask = BIT(TBIP_PDF_FRAME_END);
907 flags = RING_FLAG_FRAME;
908 /* Only enable full E2E if the other end supports it too */
909 if (tbnet_e2e && net->svc->prtcstns & TBNET_E2E)
910 flags |= RING_FLAG_E2E;
912 ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE, flags,
913 net->tx_ring.ring->hop, sof_mask,
914 eof_mask, tbnet_start_poll, net);
916 netdev_err(dev, "failed to allocate Rx ring\n");
917 tb_xdomain_release_out_hopid(xd, hopid);
918 tb_ring_free(net->tx_ring.ring);
919 net->tx_ring.ring = NULL;
922 net->rx_ring.ring = ring;
924 napi_enable(&net->napi);
930 static int tbnet_stop(struct net_device *dev)
932 struct tbnet *net = netdev_priv(dev);
934 napi_disable(&net->napi);
936 cancel_work_sync(&net->disconnect_work);
937 tbnet_tear_down(net, true);
939 tb_ring_free(net->rx_ring.ring);
940 net->rx_ring.ring = NULL;
942 tb_xdomain_release_out_hopid(net->xd, net->local_transmit_path);
943 tb_ring_free(net->tx_ring.ring);
944 net->tx_ring.ring = NULL;
949 static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
950 struct tbnet_frame **frames, u32 frame_count)
952 struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
953 struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
954 __wsum wsum = htonl(skb->len - skb_transport_offset(skb));
955 unsigned int i, len, offset = skb_transport_offset(skb);
956 __be16 protocol = skb->protocol;
957 void *data = skb->data;
958 void *dest = hdr + 1;
961 if (skb->ip_summed != CHECKSUM_PARTIAL) {
962 /* No need to calculate checksum so we just update the
963 * total frame count and sync the frames for DMA.
965 for (i = 0; i < frame_count; i++) {
966 hdr = page_address(frames[i]->page);
967 hdr->frame_count = cpu_to_le32(frame_count);
968 dma_sync_single_for_device(dma_dev,
969 frames[i]->frame.buffer_phy,
970 tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
976 if (protocol == htons(ETH_P_8021Q)) {
977 struct vlan_hdr *vhdr, vh;
979 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh);
983 protocol = vhdr->h_vlan_encapsulated_proto;
986 /* Data points on the beginning of packet.
987 * Check is the checksum absolute place in the packet.
988 * ipcso will update IP checksum.
989 * tucso will update TCP/UPD checksum.
991 if (protocol == htons(ETH_P_IP)) {
992 __sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data);
995 *ipcso = ip_fast_csum(dest + skb_network_offset(skb),
998 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
999 tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
1000 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1001 tucso = dest + ((void *)&(udp_hdr(skb)->check) - data);
1005 *tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1006 ip_hdr(skb)->daddr, 0,
1007 ip_hdr(skb)->protocol, 0);
1008 } else if (skb_is_gso_v6(skb)) {
1009 tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
1010 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1011 &ipv6_hdr(skb)->daddr, 0,
1014 } else if (protocol == htons(ETH_P_IPV6)) {
1015 tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset;
1016 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1017 &ipv6_hdr(skb)->daddr, 0,
1018 ipv6_hdr(skb)->nexthdr, 0);
1023 /* First frame was headers, rest of the frames contain data.
1024 * Calculate checksum over each frame.
1026 for (i = 0; i < frame_count; i++) {
1027 hdr = page_address(frames[i]->page);
1028 dest = (void *)(hdr + 1) + offset;
1029 len = le32_to_cpu(hdr->frame_size) - offset;
1030 wsum = csum_partial(dest, len, wsum);
1031 hdr->frame_count = cpu_to_le32(frame_count);
1036 *tucso = csum_fold(wsum);
1038 /* Checksum is finally calculated and we don't touch the memory
1039 * anymore, so DMA sync the frames now.
1041 for (i = 0; i < frame_count; i++) {
1042 dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy,
1043 tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
1049 static void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num,
1052 const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
1054 *len = skb_frag_size(frag);
1055 return kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
1058 static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
1059 struct net_device *dev)
1061 struct tbnet *net = netdev_priv(dev);
1062 struct tbnet_frame *frames[MAX_SKB_FRAGS];
1063 u16 frame_id = atomic_read(&net->frame_id);
1064 struct thunderbolt_ip_frame_header *hdr;
1065 unsigned int len = skb_headlen(skb);
1066 unsigned int data_len = skb->len;
1067 unsigned int nframes, i;
1068 unsigned int frag = 0;
1069 void *src = skb->data;
1070 u32 frame_index = 0;
1074 nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE);
1075 if (tbnet_available_buffers(&net->tx_ring) < nframes) {
1076 netif_stop_queue(net->dev);
1077 return NETDEV_TX_BUSY;
1080 frames[frame_index] = tbnet_get_tx_buffer(net);
1081 if (!frames[frame_index])
1084 hdr = page_address(frames[frame_index]->page);
1087 /* If overall packet is bigger than the frame data size */
1088 while (data_len > TBNET_MAX_PAYLOAD_SIZE) {
1089 unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE;
1091 hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE);
1092 hdr->frame_index = cpu_to_le16(frame_index);
1093 hdr->frame_id = cpu_to_le16(frame_id);
1096 if (len > size_left) {
1097 /* Copy data onto Tx buffer data with
1098 * full frame size then break and go to
1101 memcpy(dest, src, size_left);
1108 memcpy(dest, src, len);
1117 /* Ensure all fragments have been processed */
1118 if (frag < skb_shinfo(skb)->nr_frags) {
1119 /* Map and then unmap quickly */
1120 src = tbnet_kmap_frag(skb, frag++, &len);
1122 } else if (unlikely(size_left > 0)) {
1125 } while (size_left > 0);
1127 data_len -= TBNET_MAX_PAYLOAD_SIZE;
1130 frames[frame_index] = tbnet_get_tx_buffer(net);
1131 if (!frames[frame_index])
1134 hdr = page_address(frames[frame_index]->page);
1138 hdr->frame_size = cpu_to_le32(data_len);
1139 hdr->frame_index = cpu_to_le16(frame_index);
1140 hdr->frame_id = cpu_to_le16(frame_id);
1142 frames[frame_index]->frame.size = data_len + sizeof(*hdr);
1144 /* In case the remaining data_len is smaller than a frame */
1145 while (len < data_len) {
1146 memcpy(dest, src, len);
1155 if (frag < skb_shinfo(skb)->nr_frags) {
1156 src = tbnet_kmap_frag(skb, frag++, &len);
1158 } else if (unlikely(data_len > 0)) {
1163 memcpy(dest, src, data_len);
1168 if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
1171 for (i = 0; i < frame_index + 1; i++)
1172 tb_ring_tx(net->tx_ring.ring, &frames[i]->frame);
1174 if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID)
1175 atomic_inc(&net->frame_id);
1177 net->stats.tx_packets++;
1178 net->stats.tx_bytes += skb->len;
1180 dev_consume_skb_any(skb);
1182 return NETDEV_TX_OK;
1185 /* We can re-use the buffers */
1186 net->tx_ring.cons -= frame_index;
1188 dev_kfree_skb_any(skb);
1189 net->stats.tx_errors++;
1191 return NETDEV_TX_OK;
1194 static void tbnet_get_stats64(struct net_device *dev,
1195 struct rtnl_link_stats64 *stats)
1197 struct tbnet *net = netdev_priv(dev);
1199 stats->tx_packets = net->stats.tx_packets;
1200 stats->rx_packets = net->stats.rx_packets;
1201 stats->tx_bytes = net->stats.tx_bytes;
1202 stats->rx_bytes = net->stats.rx_bytes;
1203 stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors +
1204 net->stats.rx_over_errors + net->stats.rx_crc_errors +
1205 net->stats.rx_missed_errors;
1206 stats->tx_errors = net->stats.tx_errors;
1207 stats->rx_length_errors = net->stats.rx_length_errors;
1208 stats->rx_over_errors = net->stats.rx_over_errors;
1209 stats->rx_crc_errors = net->stats.rx_crc_errors;
1210 stats->rx_missed_errors = net->stats.rx_missed_errors;
1213 static const struct net_device_ops tbnet_netdev_ops = {
1214 .ndo_open = tbnet_open,
1215 .ndo_stop = tbnet_stop,
1216 .ndo_start_xmit = tbnet_start_xmit,
1217 .ndo_get_stats64 = tbnet_get_stats64,
1220 static void tbnet_generate_mac(struct net_device *dev)
1222 const struct tbnet *net = netdev_priv(dev);
1223 const struct tb_xdomain *xd = net->xd;
1228 phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route));
1230 /* Unicast and locally administered MAC */
1231 addr[0] = phy_port << 4 | 0x02;
1232 hash = jhash2((u32 *)xd->local_uuid, 4, 0);
1233 memcpy(addr + 1, &hash, sizeof(hash));
1234 hash = jhash2((u32 *)xd->local_uuid, 4, hash);
1235 addr[5] = hash & 0xff;
1236 eth_hw_addr_set(dev, addr);
1239 static int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id)
1241 struct tb_xdomain *xd = tb_service_parent(svc);
1242 struct net_device *dev;
1246 dev = alloc_etherdev(sizeof(*net));
1250 SET_NETDEV_DEV(dev, &svc->dev);
1252 net = netdev_priv(dev);
1253 INIT_DELAYED_WORK(&net->login_work, tbnet_login_work);
1254 INIT_WORK(&net->connected_work, tbnet_connected_work);
1255 INIT_WORK(&net->disconnect_work, tbnet_disconnect_work);
1256 mutex_init(&net->connection_lock);
1257 atomic_set(&net->command_id, 0);
1258 atomic_set(&net->frame_id, 0);
1263 tbnet_generate_mac(dev);
1265 strcpy(dev->name, "thunderbolt%d");
1266 dev->netdev_ops = &tbnet_netdev_ops;
1268 /* ThunderboltIP takes advantage of TSO packets but instead of
1269 * segmenting them we just split the packet into Thunderbolt
1270 * frames (maximum payload size of each frame is 4084 bytes) and
1271 * calculate checksum over the whole packet here.
1273 * The receiving side does the opposite if the host OS supports
1274 * LRO, otherwise it needs to split the large packet into MTU
1275 * sized smaller packets.
1277 * In order to receive large packets from the networking stack,
1278 * we need to announce support for most of the offloading
1281 dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO |
1282 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1283 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1284 dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header);
1286 netif_napi_add(dev, &net->napi, tbnet_poll);
1288 /* MTU range: 68 - 65522 */
1289 dev->min_mtu = ETH_MIN_MTU;
1290 dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN;
1292 net->handler.uuid = &tbnet_svc_uuid;
1293 net->handler.callback = tbnet_handle_packet;
1294 net->handler.data = net;
1295 tb_register_protocol_handler(&net->handler);
1297 tb_service_set_drvdata(svc, net);
1299 ret = register_netdev(dev);
1301 tb_unregister_protocol_handler(&net->handler);
1309 static void tbnet_remove(struct tb_service *svc)
1311 struct tbnet *net = tb_service_get_drvdata(svc);
1313 unregister_netdev(net->dev);
1314 tb_unregister_protocol_handler(&net->handler);
1315 free_netdev(net->dev);
1318 static void tbnet_shutdown(struct tb_service *svc)
1320 tbnet_tear_down(tb_service_get_drvdata(svc), true);
1323 static int __maybe_unused tbnet_suspend(struct device *dev)
1325 struct tb_service *svc = tb_to_service(dev);
1326 struct tbnet *net = tb_service_get_drvdata(svc);
1329 if (netif_running(net->dev)) {
1330 netif_device_detach(net->dev);
1331 tbnet_tear_down(net, true);
1334 tb_unregister_protocol_handler(&net->handler);
1338 static int __maybe_unused tbnet_resume(struct device *dev)
1340 struct tb_service *svc = tb_to_service(dev);
1341 struct tbnet *net = tb_service_get_drvdata(svc);
1343 tb_register_protocol_handler(&net->handler);
1345 netif_carrier_off(net->dev);
1346 if (netif_running(net->dev)) {
1347 netif_device_attach(net->dev);
1354 static const struct dev_pm_ops tbnet_pm_ops = {
1355 SET_SYSTEM_SLEEP_PM_OPS(tbnet_suspend, tbnet_resume)
1358 static const struct tb_service_id tbnet_ids[] = {
1359 { TB_SERVICE("network", 1) },
1362 MODULE_DEVICE_TABLE(tbsvc, tbnet_ids);
1364 static struct tb_service_driver tbnet_driver = {
1366 .owner = THIS_MODULE,
1367 .name = "thunderbolt-net",
1368 .pm = &tbnet_pm_ops,
1370 .probe = tbnet_probe,
1371 .remove = tbnet_remove,
1372 .shutdown = tbnet_shutdown,
1373 .id_table = tbnet_ids,
1376 static int __init tbnet_init(void)
1381 tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid);
1385 tb_property_add_immediate(tbnet_dir, "prtcid", 1);
1386 tb_property_add_immediate(tbnet_dir, "prtcvers", 1);
1387 tb_property_add_immediate(tbnet_dir, "prtcrevs", 1);
1389 flags = TBNET_MATCH_FRAGS_ID | TBNET_64K_FRAMES;
1392 tb_property_add_immediate(tbnet_dir, "prtcstns", flags);
1394 ret = tb_register_property_dir("network", tbnet_dir);
1398 ret = tb_register_service_driver(&tbnet_driver);
1400 goto err_unregister;
1405 tb_unregister_property_dir("network", tbnet_dir);
1407 tb_property_free_dir(tbnet_dir);
1411 module_init(tbnet_init);
1413 static void __exit tbnet_exit(void)
1415 tb_unregister_service_driver(&tbnet_driver);
1416 tb_unregister_property_dir("network", tbnet_dir);
1417 tb_property_free_dir(tbnet_dir);
1419 module_exit(tbnet_exit);
1421 MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
1422 MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
1423 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1424 MODULE_DESCRIPTION("Thunderbolt/USB4 network driver");
1425 MODULE_LICENSE("GPL v2");