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>
28 /* Protocol timeouts in ms */
29 #define TBNET_LOGIN_DELAY 4500
30 #define TBNET_LOGIN_TIMEOUT 500
31 #define TBNET_LOGOUT_TIMEOUT 1000
33 #define TBNET_RING_SIZE 256
34 #define TBNET_LOGIN_RETRIES 60
35 #define TBNET_LOGOUT_RETRIES 10
36 #define TBNET_E2E BIT(0)
37 #define TBNET_MATCH_FRAGS_ID BIT(1)
38 #define TBNET_64K_FRAMES BIT(2)
39 #define TBNET_MAX_MTU SZ_64K
40 #define TBNET_FRAME_SIZE SZ_4K
41 #define TBNET_MAX_PAYLOAD_SIZE \
42 (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
43 /* Rx packets need to hold space for skb_shared_info */
44 #define TBNET_RX_MAX_SIZE \
45 (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
46 #define TBNET_RX_PAGE_ORDER get_order(TBNET_RX_MAX_SIZE)
47 #define TBNET_RX_PAGE_SIZE (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
49 #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
52 * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
53 * @frame_size: size of the data with the frame
54 * @frame_index: running index on the frames
55 * @frame_id: ID of the frame to match frames to specific packet
56 * @frame_count: how many frames assembles a full packet
58 * Each data frame passed to the high-speed DMA ring has this header. If
59 * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
60 * supported then @frame_id is filled, otherwise it stays %0.
62 struct thunderbolt_ip_frame_header
{
69 enum thunderbolt_ip_frame_pdf
{
70 TBIP_PDF_FRAME_START
= 1,
74 enum thunderbolt_ip_type
{
81 struct thunderbolt_ip_header
{
86 uuid_t initiator_uuid
;
92 #define TBIP_HDR_LENGTH_MASK GENMASK(5, 0)
93 #define TBIP_HDR_SN_MASK GENMASK(28, 27)
94 #define TBIP_HDR_SN_SHIFT 27
96 struct thunderbolt_ip_login
{
97 struct thunderbolt_ip_header hdr
;
103 #define TBIP_LOGIN_PROTO_VERSION 1
105 struct thunderbolt_ip_login_response
{
106 struct thunderbolt_ip_header hdr
;
109 u32 receiver_mac_len
;
113 struct thunderbolt_ip_logout
{
114 struct thunderbolt_ip_header hdr
;
117 struct thunderbolt_ip_status
{
118 struct thunderbolt_ip_header hdr
;
129 u64 rx_length_errors
;
132 u64 rx_missed_errors
;
136 struct net_device
*dev
;
138 struct ring_frame frame
;
142 struct tbnet_frame frames
[TBNET_RING_SIZE
];
145 struct tb_ring
*ring
;
149 * struct tbnet - ThunderboltIP network driver private data
150 * @svc: XDomain service the driver is bound to
151 * @xd: XDomain the service belongs to
152 * @handler: ThunderboltIP configuration protocol handler
153 * @dev: Networking device
154 * @napi: NAPI structure for Rx polling
155 * @stats: Network statistics
156 * @skb: Network packet that is currently processed on Rx path
157 * @command_id: ID used for next configuration protocol packet
158 * @login_sent: ThunderboltIP login message successfully sent
159 * @login_received: ThunderboltIP login message received from the remote
161 * @local_transmit_path: HopID we are using to send out packets
162 * @remote_transmit_path: HopID the other end is using to send packets to us
163 * @connection_lock: Lock serializing access to @login_sent,
164 * @login_received and @transmit_path.
165 * @login_retries: Number of login retries currently done
166 * @login_work: Worker to send ThunderboltIP login packets
167 * @connected_work: Worker that finalizes the ThunderboltIP connection
168 * setup and enables DMA paths for high speed data
170 * @disconnect_work: Worker that handles tearing down the ThunderboltIP
172 * @rx_hdr: Copy of the currently processed Rx frame. Used when a
173 * network packet consists of multiple Thunderbolt frames.
174 * In host byte order.
175 * @rx_ring: Software ring holding Rx frames
176 * @frame_id: Frame ID use for next Tx packet
177 * (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
178 * @tx_ring: Software ring holding Tx frames
181 const struct tb_service
*svc
;
182 struct tb_xdomain
*xd
;
183 struct tb_protocol_handler handler
;
184 struct net_device
*dev
;
185 struct napi_struct napi
;
186 struct tbnet_stats stats
;
191 int local_transmit_path
;
192 int remote_transmit_path
;
193 struct mutex connection_lock
;
195 struct delayed_work login_work
;
196 struct work_struct connected_work
;
197 struct work_struct disconnect_work
;
198 struct thunderbolt_ip_frame_header rx_hdr
;
199 struct tbnet_ring rx_ring
;
201 struct tbnet_ring tx_ring
;
204 /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
205 static const uuid_t tbnet_dir_uuid
=
206 UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
207 0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
209 /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
210 static const uuid_t tbnet_svc_uuid
=
211 UUID_INIT(0x798f589e, 0x3616, 0x8a47,
212 0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
214 static struct tb_property_dir
*tbnet_dir
;
216 static bool tbnet_e2e
= true;
217 module_param_named(e2e
, tbnet_e2e
, bool, 0444);
218 MODULE_PARM_DESC(e2e
, "USB4NET full end-to-end flow control (default: true)");
220 static void tbnet_fill_header(struct thunderbolt_ip_header
*hdr
, u64 route
,
221 u8 sequence
, const uuid_t
*initiator_uuid
, const uuid_t
*target_uuid
,
222 enum thunderbolt_ip_type type
, size_t size
, u32 command_id
)
226 /* Length does not include route_hi/lo and length_sn fields */
227 length_sn
= (size
- 3 * 4) / 4;
228 length_sn
|= (sequence
<< TBIP_HDR_SN_SHIFT
) & TBIP_HDR_SN_MASK
;
230 hdr
->route_hi
= upper_32_bits(route
);
231 hdr
->route_lo
= lower_32_bits(route
);
232 hdr
->length_sn
= length_sn
;
233 uuid_copy(&hdr
->uuid
, &tbnet_svc_uuid
);
234 uuid_copy(&hdr
->initiator_uuid
, initiator_uuid
);
235 uuid_copy(&hdr
->target_uuid
, target_uuid
);
237 hdr
->command_id
= command_id
;
240 static int tbnet_login_response(struct tbnet
*net
, u64 route
, u8 sequence
,
243 struct thunderbolt_ip_login_response reply
;
244 struct tb_xdomain
*xd
= net
->xd
;
246 memset(&reply
, 0, sizeof(reply
));
247 tbnet_fill_header(&reply
.hdr
, route
, sequence
, xd
->local_uuid
,
248 xd
->remote_uuid
, TBIP_LOGIN_RESPONSE
, sizeof(reply
),
250 memcpy(reply
.receiver_mac
, net
->dev
->dev_addr
, ETH_ALEN
);
251 reply
.receiver_mac_len
= ETH_ALEN
;
253 return tb_xdomain_response(xd
, &reply
, sizeof(reply
),
254 TB_CFG_PKG_XDOMAIN_RESP
);
257 static int tbnet_login_request(struct tbnet
*net
, u8 sequence
)
259 struct thunderbolt_ip_login_response reply
;
260 struct thunderbolt_ip_login request
;
261 struct tb_xdomain
*xd
= net
->xd
;
263 memset(&request
, 0, sizeof(request
));
264 tbnet_fill_header(&request
.hdr
, xd
->route
, sequence
, xd
->local_uuid
,
265 xd
->remote_uuid
, TBIP_LOGIN
, sizeof(request
),
266 atomic_inc_return(&net
->command_id
));
268 request
.proto_version
= TBIP_LOGIN_PROTO_VERSION
;
269 request
.transmit_path
= net
->local_transmit_path
;
271 return tb_xdomain_request(xd
, &request
, sizeof(request
),
272 TB_CFG_PKG_XDOMAIN_RESP
, &reply
,
273 sizeof(reply
), TB_CFG_PKG_XDOMAIN_RESP
,
274 TBNET_LOGIN_TIMEOUT
);
277 static int tbnet_logout_response(struct tbnet
*net
, u64 route
, u8 sequence
,
280 struct thunderbolt_ip_status reply
;
281 struct tb_xdomain
*xd
= net
->xd
;
283 memset(&reply
, 0, sizeof(reply
));
284 tbnet_fill_header(&reply
.hdr
, route
, sequence
, xd
->local_uuid
,
285 xd
->remote_uuid
, TBIP_STATUS
, sizeof(reply
),
286 atomic_inc_return(&net
->command_id
));
287 return tb_xdomain_response(xd
, &reply
, sizeof(reply
),
288 TB_CFG_PKG_XDOMAIN_RESP
);
291 static int tbnet_logout_request(struct tbnet
*net
)
293 struct thunderbolt_ip_logout request
;
294 struct thunderbolt_ip_status reply
;
295 struct tb_xdomain
*xd
= net
->xd
;
297 memset(&request
, 0, sizeof(request
));
298 tbnet_fill_header(&request
.hdr
, xd
->route
, 0, xd
->local_uuid
,
299 xd
->remote_uuid
, TBIP_LOGOUT
, sizeof(request
),
300 atomic_inc_return(&net
->command_id
));
302 return tb_xdomain_request(xd
, &request
, sizeof(request
),
303 TB_CFG_PKG_XDOMAIN_RESP
, &reply
,
304 sizeof(reply
), TB_CFG_PKG_XDOMAIN_RESP
,
305 TBNET_LOGOUT_TIMEOUT
);
308 static void start_login(struct tbnet
*net
)
310 netdev_dbg(net
->dev
, "login started\n");
312 mutex_lock(&net
->connection_lock
);
313 net
->login_sent
= false;
314 net
->login_received
= false;
315 mutex_unlock(&net
->connection_lock
);
317 queue_delayed_work(system_long_wq
, &net
->login_work
,
318 msecs_to_jiffies(1000));
321 static void stop_login(struct tbnet
*net
)
323 cancel_delayed_work_sync(&net
->login_work
);
324 cancel_work_sync(&net
->connected_work
);
326 netdev_dbg(net
->dev
, "login stopped\n");
329 static inline unsigned int tbnet_frame_size(const struct tbnet_frame
*tf
)
331 return tf
->frame
.size
? : TBNET_FRAME_SIZE
;
334 static void tbnet_free_buffers(struct tbnet_ring
*ring
)
338 for (i
= 0; i
< TBNET_RING_SIZE
; i
++) {
339 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
340 struct tbnet_frame
*tf
= &ring
->frames
[i
];
341 enum dma_data_direction dir
;
348 if (ring
->ring
->is_tx
) {
351 size
= TBNET_FRAME_SIZE
;
353 dir
= DMA_FROM_DEVICE
;
354 order
= TBNET_RX_PAGE_ORDER
;
355 size
= TBNET_RX_PAGE_SIZE
;
358 trace_tbnet_free_frame(i
, tf
->page
, tf
->frame
.buffer_phy
, dir
);
360 if (tf
->frame
.buffer_phy
)
361 dma_unmap_page(dma_dev
, tf
->frame
.buffer_phy
, size
,
364 __free_pages(tf
->page
, order
);
372 static void tbnet_tear_down(struct tbnet
*net
, bool send_logout
)
374 netif_carrier_off(net
->dev
);
375 netif_stop_queue(net
->dev
);
379 mutex_lock(&net
->connection_lock
);
381 if (net
->login_sent
&& net
->login_received
) {
382 int ret
, retries
= TBNET_LOGOUT_RETRIES
;
384 while (send_logout
&& retries
-- > 0) {
385 netdev_dbg(net
->dev
, "sending logout request %u\n",
387 ret
= tbnet_logout_request(net
);
388 if (ret
!= -ETIMEDOUT
)
392 tb_ring_stop(net
->rx_ring
.ring
);
393 tb_ring_stop(net
->tx_ring
.ring
);
394 tbnet_free_buffers(&net
->rx_ring
);
395 tbnet_free_buffers(&net
->tx_ring
);
397 ret
= tb_xdomain_disable_paths(net
->xd
,
398 net
->local_transmit_path
,
399 net
->rx_ring
.ring
->hop
,
400 net
->remote_transmit_path
,
401 net
->tx_ring
.ring
->hop
);
403 netdev_warn(net
->dev
, "failed to disable DMA paths\n");
405 tb_xdomain_release_in_hopid(net
->xd
, net
->remote_transmit_path
);
406 net
->remote_transmit_path
= 0;
409 net
->login_retries
= 0;
410 net
->login_sent
= false;
411 net
->login_received
= false;
413 netdev_dbg(net
->dev
, "network traffic stopped\n");
415 mutex_unlock(&net
->connection_lock
);
418 static int tbnet_handle_packet(const void *buf
, size_t size
, void *data
)
420 const struct thunderbolt_ip_login
*pkg
= buf
;
421 struct tbnet
*net
= data
;
427 /* Make sure the packet is for us */
428 if (size
< sizeof(struct thunderbolt_ip_header
))
430 if (!uuid_equal(&pkg
->hdr
.initiator_uuid
, net
->xd
->remote_uuid
))
432 if (!uuid_equal(&pkg
->hdr
.target_uuid
, net
->xd
->local_uuid
))
435 route
= ((u64
)pkg
->hdr
.route_hi
<< 32) | pkg
->hdr
.route_lo
;
436 route
&= ~BIT_ULL(63);
437 if (route
!= net
->xd
->route
)
440 sequence
= pkg
->hdr
.length_sn
& TBIP_HDR_SN_MASK
;
441 sequence
>>= TBIP_HDR_SN_SHIFT
;
442 command_id
= pkg
->hdr
.command_id
;
444 switch (pkg
->hdr
.type
) {
446 netdev_dbg(net
->dev
, "remote login request received\n");
447 if (!netif_running(net
->dev
))
450 ret
= tbnet_login_response(net
, route
, sequence
,
451 pkg
->hdr
.command_id
);
453 netdev_dbg(net
->dev
, "remote login response sent\n");
455 mutex_lock(&net
->connection_lock
);
456 net
->login_received
= true;
457 net
->remote_transmit_path
= pkg
->transmit_path
;
459 /* If we reached the number of max retries or
460 * previous logout, schedule another round of
463 if (net
->login_retries
>= TBNET_LOGIN_RETRIES
||
465 net
->login_retries
= 0;
466 queue_delayed_work(system_long_wq
,
467 &net
->login_work
, 0);
469 mutex_unlock(&net
->connection_lock
);
471 queue_work(system_long_wq
, &net
->connected_work
);
476 netdev_dbg(net
->dev
, "remote logout request received\n");
477 ret
= tbnet_logout_response(net
, route
, sequence
, command_id
);
479 netdev_dbg(net
->dev
, "remote logout response sent\n");
480 queue_work(system_long_wq
, &net
->disconnect_work
);
489 netdev_warn(net
->dev
, "failed to send ThunderboltIP response\n");
494 static unsigned int tbnet_available_buffers(const struct tbnet_ring
*ring
)
496 return ring
->prod
- ring
->cons
;
499 static int tbnet_alloc_rx_buffers(struct tbnet
*net
, unsigned int nbuffers
)
501 struct tbnet_ring
*ring
= &net
->rx_ring
;
505 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
506 unsigned int index
= ring
->prod
& (TBNET_RING_SIZE
- 1);
507 struct tbnet_frame
*tf
= &ring
->frames
[index
];
513 /* Allocate page (order > 0) so that it can hold maximum
514 * ThunderboltIP frame (4kB) and the additional room for
515 * SKB shared info required by build_skb().
517 tf
->page
= dev_alloc_pages(TBNET_RX_PAGE_ORDER
);
523 dma_addr
= dma_map_page(dma_dev
, tf
->page
, 0,
524 TBNET_RX_PAGE_SIZE
, DMA_FROM_DEVICE
);
525 if (dma_mapping_error(dma_dev
, dma_addr
)) {
530 tf
->frame
.buffer_phy
= dma_addr
;
533 trace_tbnet_alloc_rx_frame(index
, tf
->page
, dma_addr
,
536 tb_ring_rx(ring
->ring
, &tf
->frame
);
544 tbnet_free_buffers(ring
);
548 static struct tbnet_frame
*tbnet_get_tx_buffer(struct tbnet
*net
)
550 struct tbnet_ring
*ring
= &net
->tx_ring
;
551 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
552 struct tbnet_frame
*tf
;
555 if (!tbnet_available_buffers(ring
))
558 index
= ring
->cons
++ & (TBNET_RING_SIZE
- 1);
560 tf
= &ring
->frames
[index
];
563 dma_sync_single_for_cpu(dma_dev
, tf
->frame
.buffer_phy
,
564 tbnet_frame_size(tf
), DMA_TO_DEVICE
);
569 static void tbnet_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
572 struct tbnet_frame
*tf
= container_of(frame
, typeof(*tf
), frame
);
573 struct tbnet
*net
= netdev_priv(tf
->dev
);
575 /* Return buffer to the ring */
578 if (tbnet_available_buffers(&net
->tx_ring
) >= TBNET_RING_SIZE
/ 2)
579 netif_wake_queue(net
->dev
);
582 static int tbnet_alloc_tx_buffers(struct tbnet
*net
)
584 struct tbnet_ring
*ring
= &net
->tx_ring
;
585 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
588 for (i
= 0; i
< TBNET_RING_SIZE
; i
++) {
589 struct tbnet_frame
*tf
= &ring
->frames
[i
];
592 tf
->page
= alloc_page(GFP_KERNEL
);
594 tbnet_free_buffers(ring
);
598 dma_addr
= dma_map_page(dma_dev
, tf
->page
, 0, TBNET_FRAME_SIZE
,
600 if (dma_mapping_error(dma_dev
, dma_addr
)) {
601 __free_page(tf
->page
);
603 tbnet_free_buffers(ring
);
608 tf
->frame
.buffer_phy
= dma_addr
;
609 tf
->frame
.callback
= tbnet_tx_callback
;
610 tf
->frame
.sof
= TBIP_PDF_FRAME_START
;
611 tf
->frame
.eof
= TBIP_PDF_FRAME_END
;
613 trace_tbnet_alloc_tx_frame(i
, tf
->page
, dma_addr
, DMA_TO_DEVICE
);
617 ring
->prod
= TBNET_RING_SIZE
- 1;
622 static void tbnet_connected_work(struct work_struct
*work
)
624 struct tbnet
*net
= container_of(work
, typeof(*net
), connected_work
);
628 if (netif_carrier_ok(net
->dev
))
631 mutex_lock(&net
->connection_lock
);
632 connected
= net
->login_sent
&& net
->login_received
;
633 mutex_unlock(&net
->connection_lock
);
638 netdev_dbg(net
->dev
, "login successful, enabling paths\n");
640 ret
= tb_xdomain_alloc_in_hopid(net
->xd
, net
->remote_transmit_path
);
641 if (ret
!= net
->remote_transmit_path
) {
642 netdev_err(net
->dev
, "failed to allocate Rx HopID\n");
646 /* Both logins successful so enable the rings, high-speed DMA
647 * paths and start the network device queue.
649 * Note we enable the DMA paths last to make sure we have primed
650 * the Rx ring before any incoming packets are allowed to
653 tb_ring_start(net
->tx_ring
.ring
);
654 tb_ring_start(net
->rx_ring
.ring
);
656 ret
= tbnet_alloc_rx_buffers(net
, TBNET_RING_SIZE
);
660 ret
= tbnet_alloc_tx_buffers(net
);
662 goto err_free_rx_buffers
;
664 ret
= tb_xdomain_enable_paths(net
->xd
, net
->local_transmit_path
,
665 net
->rx_ring
.ring
->hop
,
666 net
->remote_transmit_path
,
667 net
->tx_ring
.ring
->hop
);
669 netdev_err(net
->dev
, "failed to enable DMA paths\n");
670 goto err_free_tx_buffers
;
673 netif_carrier_on(net
->dev
);
674 netif_start_queue(net
->dev
);
676 netdev_dbg(net
->dev
, "network traffic started\n");
680 tbnet_free_buffers(&net
->tx_ring
);
682 tbnet_free_buffers(&net
->rx_ring
);
684 tb_ring_stop(net
->rx_ring
.ring
);
685 tb_ring_stop(net
->tx_ring
.ring
);
686 tb_xdomain_release_in_hopid(net
->xd
, net
->remote_transmit_path
);
689 static void tbnet_login_work(struct work_struct
*work
)
691 struct tbnet
*net
= container_of(work
, typeof(*net
), login_work
.work
);
692 unsigned long delay
= msecs_to_jiffies(TBNET_LOGIN_DELAY
);
695 if (netif_carrier_ok(net
->dev
))
698 netdev_dbg(net
->dev
, "sending login request, retries=%u\n",
701 ret
= tbnet_login_request(net
, net
->login_retries
% 4);
703 netdev_dbg(net
->dev
, "sending login request failed, ret=%d\n",
705 if (net
->login_retries
++ < TBNET_LOGIN_RETRIES
) {
706 queue_delayed_work(system_long_wq
, &net
->login_work
,
709 netdev_info(net
->dev
, "ThunderboltIP login timed out\n");
712 netdev_dbg(net
->dev
, "received login reply\n");
714 net
->login_retries
= 0;
716 mutex_lock(&net
->connection_lock
);
717 net
->login_sent
= true;
718 mutex_unlock(&net
->connection_lock
);
720 queue_work(system_long_wq
, &net
->connected_work
);
724 static void tbnet_disconnect_work(struct work_struct
*work
)
726 struct tbnet
*net
= container_of(work
, typeof(*net
), disconnect_work
);
728 tbnet_tear_down(net
, false);
731 static bool tbnet_check_frame(struct tbnet
*net
, const struct tbnet_frame
*tf
,
732 const struct thunderbolt_ip_frame_header
*hdr
)
734 u32 frame_id
, frame_count
, frame_size
, frame_index
;
737 if (tf
->frame
.flags
& RING_DESC_CRC_ERROR
) {
738 net
->stats
.rx_crc_errors
++;
740 } else if (tf
->frame
.flags
& RING_DESC_BUFFER_OVERRUN
) {
741 net
->stats
.rx_over_errors
++;
745 /* Should be greater than just header i.e. contains data */
746 size
= tbnet_frame_size(tf
);
747 if (size
<= sizeof(*hdr
)) {
748 net
->stats
.rx_length_errors
++;
752 frame_count
= le32_to_cpu(hdr
->frame_count
);
753 frame_size
= le32_to_cpu(hdr
->frame_size
);
754 frame_index
= le16_to_cpu(hdr
->frame_index
);
755 frame_id
= le16_to_cpu(hdr
->frame_id
);
757 if ((frame_size
> size
- sizeof(*hdr
)) || !frame_size
) {
758 net
->stats
.rx_length_errors
++;
762 /* In case we're in the middle of packet, validate the frame
763 * header based on first fragment of the packet.
765 if (net
->skb
&& net
->rx_hdr
.frame_count
) {
766 /* Check the frame count fits the count field */
767 if (frame_count
!= le32_to_cpu(net
->rx_hdr
.frame_count
)) {
768 net
->stats
.rx_length_errors
++;
772 /* Check the frame identifiers are incremented correctly,
773 * and id is matching.
775 if (frame_index
!= le16_to_cpu(net
->rx_hdr
.frame_index
) + 1 ||
776 frame_id
!= le16_to_cpu(net
->rx_hdr
.frame_id
)) {
777 net
->stats
.rx_missed_errors
++;
781 if (net
->skb
->len
+ frame_size
> TBNET_MAX_MTU
) {
782 net
->stats
.rx_length_errors
++;
789 /* Start of packet, validate the frame header */
790 if (frame_count
== 0 || frame_count
> TBNET_RING_SIZE
/ 4) {
791 net
->stats
.rx_length_errors
++;
794 if (frame_index
!= 0) {
795 net
->stats
.rx_missed_errors
++;
802 static int tbnet_poll(struct napi_struct
*napi
, int budget
)
804 struct tbnet
*net
= container_of(napi
, struct tbnet
, napi
);
805 unsigned int cleaned_count
= tbnet_available_buffers(&net
->rx_ring
);
806 struct device
*dma_dev
= tb_ring_dma_device(net
->rx_ring
.ring
);
807 unsigned int rx_packets
= 0;
809 while (rx_packets
< budget
) {
810 const struct thunderbolt_ip_frame_header
*hdr
;
811 unsigned int hdr_size
= sizeof(*hdr
);
812 struct sk_buff
*skb
= NULL
;
813 struct ring_frame
*frame
;
814 struct tbnet_frame
*tf
;
819 /* Return some buffers to hardware, one at a time is too
820 * slow so allocate MAX_SKB_FRAGS buffers at the same
823 if (cleaned_count
>= MAX_SKB_FRAGS
) {
824 tbnet_alloc_rx_buffers(net
, cleaned_count
);
828 frame
= tb_ring_poll(net
->rx_ring
.ring
);
832 dma_unmap_page(dma_dev
, frame
->buffer_phy
,
833 TBNET_RX_PAGE_SIZE
, DMA_FROM_DEVICE
);
835 tf
= container_of(frame
, typeof(*tf
), frame
);
842 hdr
= page_address(page
);
843 if (!tbnet_check_frame(net
, tf
, hdr
)) {
844 trace_tbnet_invalid_rx_ip_frame(hdr
->frame_size
,
845 hdr
->frame_id
, hdr
->frame_index
, hdr
->frame_count
);
846 __free_pages(page
, TBNET_RX_PAGE_ORDER
);
847 dev_kfree_skb_any(net
->skb
);
852 trace_tbnet_rx_ip_frame(hdr
->frame_size
, hdr
->frame_id
,
853 hdr
->frame_index
, hdr
->frame_count
);
854 frame_size
= le32_to_cpu(hdr
->frame_size
);
858 skb
= build_skb(page_address(page
),
861 __free_pages(page
, TBNET_RX_PAGE_ORDER
);
862 net
->stats
.rx_errors
++;
866 skb_reserve(skb
, hdr_size
);
867 skb_put(skb
, frame_size
);
871 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
872 page
, hdr_size
, frame_size
,
873 TBNET_RX_PAGE_SIZE
- hdr_size
);
876 net
->rx_hdr
.frame_size
= hdr
->frame_size
;
877 net
->rx_hdr
.frame_count
= hdr
->frame_count
;
878 net
->rx_hdr
.frame_index
= hdr
->frame_index
;
879 net
->rx_hdr
.frame_id
= hdr
->frame_id
;
880 last
= le16_to_cpu(net
->rx_hdr
.frame_index
) ==
881 le32_to_cpu(net
->rx_hdr
.frame_count
) - 1;
884 net
->stats
.rx_bytes
+= frame_size
;
887 skb
->protocol
= eth_type_trans(skb
, net
->dev
);
888 trace_tbnet_rx_skb(skb
);
889 napi_gro_receive(&net
->napi
, skb
);
894 net
->stats
.rx_packets
+= rx_packets
;
897 tbnet_alloc_rx_buffers(net
, cleaned_count
);
899 if (rx_packets
>= budget
)
902 napi_complete_done(napi
, rx_packets
);
903 /* Re-enable the ring interrupt */
904 tb_ring_poll_complete(net
->rx_ring
.ring
);
909 static void tbnet_start_poll(void *data
)
911 struct tbnet
*net
= data
;
913 napi_schedule(&net
->napi
);
916 static int tbnet_open(struct net_device
*dev
)
918 struct tbnet
*net
= netdev_priv(dev
);
919 struct tb_xdomain
*xd
= net
->xd
;
920 u16 sof_mask
, eof_mask
;
921 struct tb_ring
*ring
;
925 netif_carrier_off(dev
);
927 ring
= tb_ring_alloc_tx(xd
->tb
->nhi
, -1, TBNET_RING_SIZE
,
930 netdev_err(dev
, "failed to allocate Tx ring\n");
933 net
->tx_ring
.ring
= ring
;
935 hopid
= tb_xdomain_alloc_out_hopid(xd
, -1);
937 netdev_err(dev
, "failed to allocate Tx HopID\n");
938 tb_ring_free(net
->tx_ring
.ring
);
939 net
->tx_ring
.ring
= NULL
;
942 net
->local_transmit_path
= hopid
;
944 sof_mask
= BIT(TBIP_PDF_FRAME_START
);
945 eof_mask
= BIT(TBIP_PDF_FRAME_END
);
947 flags
= RING_FLAG_FRAME
;
948 /* Only enable full E2E if the other end supports it too */
949 if (tbnet_e2e
&& net
->svc
->prtcstns
& TBNET_E2E
)
950 flags
|= RING_FLAG_E2E
;
952 ring
= tb_ring_alloc_rx(xd
->tb
->nhi
, -1, TBNET_RING_SIZE
, flags
,
953 net
->tx_ring
.ring
->hop
, sof_mask
,
954 eof_mask
, tbnet_start_poll
, net
);
956 netdev_err(dev
, "failed to allocate Rx ring\n");
957 tb_xdomain_release_out_hopid(xd
, hopid
);
958 tb_ring_free(net
->tx_ring
.ring
);
959 net
->tx_ring
.ring
= NULL
;
962 net
->rx_ring
.ring
= ring
;
964 napi_enable(&net
->napi
);
970 static int tbnet_stop(struct net_device
*dev
)
972 struct tbnet
*net
= netdev_priv(dev
);
974 napi_disable(&net
->napi
);
976 cancel_work_sync(&net
->disconnect_work
);
977 tbnet_tear_down(net
, true);
979 tb_ring_free(net
->rx_ring
.ring
);
980 net
->rx_ring
.ring
= NULL
;
982 tb_xdomain_release_out_hopid(net
->xd
, net
->local_transmit_path
);
983 tb_ring_free(net
->tx_ring
.ring
);
984 net
->tx_ring
.ring
= NULL
;
989 static bool tbnet_xmit_csum_and_map(struct tbnet
*net
, struct sk_buff
*skb
,
990 struct tbnet_frame
**frames
, u32 frame_count
)
992 struct thunderbolt_ip_frame_header
*hdr
= page_address(frames
[0]->page
);
993 struct device
*dma_dev
= tb_ring_dma_device(net
->tx_ring
.ring
);
994 unsigned int i
, len
, offset
= skb_transport_offset(skb
);
995 /* Remove payload length from checksum */
996 u32 paylen
= skb
->len
- skb_transport_offset(skb
);
997 __wsum wsum
= (__force __wsum
)htonl(paylen
);
998 __be16 protocol
= skb
->protocol
;
999 void *data
= skb
->data
;
1000 void *dest
= hdr
+ 1;
1003 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
1004 /* No need to calculate checksum so we just update the
1005 * total frame count and sync the frames for DMA.
1007 for (i
= 0; i
< frame_count
; i
++) {
1008 hdr
= page_address(frames
[i
]->page
);
1009 hdr
->frame_count
= cpu_to_le32(frame_count
);
1010 trace_tbnet_tx_ip_frame(hdr
->frame_size
, hdr
->frame_id
,
1011 hdr
->frame_index
, hdr
->frame_count
);
1012 dma_sync_single_for_device(dma_dev
,
1013 frames
[i
]->frame
.buffer_phy
,
1014 tbnet_frame_size(frames
[i
]), DMA_TO_DEVICE
);
1020 if (protocol
== htons(ETH_P_8021Q
)) {
1021 struct vlan_hdr
*vhdr
, vh
;
1023 vhdr
= skb_header_pointer(skb
, ETH_HLEN
, sizeof(vh
), &vh
);
1027 protocol
= vhdr
->h_vlan_encapsulated_proto
;
1030 /* Data points on the beginning of packet.
1031 * Check is the checksum absolute place in the packet.
1032 * ipcso will update IP checksum.
1033 * tucso will update TCP/UDP checksum.
1035 if (protocol
== htons(ETH_P_IP
)) {
1036 __sum16
*ipcso
= dest
+ ((void *)&(ip_hdr(skb
)->check
) - data
);
1039 *ipcso
= ip_fast_csum(dest
+ skb_network_offset(skb
),
1042 if (ip_hdr(skb
)->protocol
== IPPROTO_TCP
)
1043 tucso
= dest
+ ((void *)&(tcp_hdr(skb
)->check
) - data
);
1044 else if (ip_hdr(skb
)->protocol
== IPPROTO_UDP
)
1045 tucso
= dest
+ ((void *)&(udp_hdr(skb
)->check
) - data
);
1049 *tucso
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
1050 ip_hdr(skb
)->daddr
, 0,
1051 ip_hdr(skb
)->protocol
, 0);
1052 } else if (skb_is_gso(skb
) && skb_is_gso_v6(skb
)) {
1053 tucso
= dest
+ ((void *)&(tcp_hdr(skb
)->check
) - data
);
1054 *tucso
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
1055 &ipv6_hdr(skb
)->daddr
, 0,
1057 } else if (protocol
== htons(ETH_P_IPV6
)) {
1058 tucso
= dest
+ skb_checksum_start_offset(skb
) + skb
->csum_offset
;
1059 *tucso
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
1060 &ipv6_hdr(skb
)->daddr
, 0,
1061 ipv6_hdr(skb
)->nexthdr
, 0);
1066 /* First frame was headers, rest of the frames contain data.
1067 * Calculate checksum over each frame.
1069 for (i
= 0; i
< frame_count
; i
++) {
1070 hdr
= page_address(frames
[i
]->page
);
1071 dest
= (void *)(hdr
+ 1) + offset
;
1072 len
= le32_to_cpu(hdr
->frame_size
) - offset
;
1073 wsum
= csum_partial(dest
, len
, wsum
);
1074 hdr
->frame_count
= cpu_to_le32(frame_count
);
1075 trace_tbnet_tx_ip_frame(hdr
->frame_size
, hdr
->frame_id
,
1076 hdr
->frame_index
, hdr
->frame_count
);
1081 *tucso
= csum_fold(wsum
);
1083 /* Checksum is finally calculated and we don't touch the memory
1084 * anymore, so DMA sync the frames now.
1086 for (i
= 0; i
< frame_count
; i
++) {
1087 dma_sync_single_for_device(dma_dev
, frames
[i
]->frame
.buffer_phy
,
1088 tbnet_frame_size(frames
[i
]), DMA_TO_DEVICE
);
1094 static void *tbnet_kmap_frag(struct sk_buff
*skb
, unsigned int frag_num
,
1097 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[frag_num
];
1099 *len
= skb_frag_size(frag
);
1100 return kmap_local_page(skb_frag_page(frag
)) + skb_frag_off(frag
);
1103 static netdev_tx_t
tbnet_start_xmit(struct sk_buff
*skb
,
1104 struct net_device
*dev
)
1106 struct tbnet
*net
= netdev_priv(dev
);
1107 struct tbnet_frame
*frames
[MAX_SKB_FRAGS
];
1108 u16 frame_id
= atomic_read(&net
->frame_id
);
1109 struct thunderbolt_ip_frame_header
*hdr
;
1110 unsigned int len
= skb_headlen(skb
);
1111 unsigned int data_len
= skb
->len
;
1112 unsigned int nframes
, i
;
1113 unsigned int frag
= 0;
1114 void *src
= skb
->data
;
1115 u32 frame_index
= 0;
1119 trace_tbnet_tx_skb(skb
);
1121 nframes
= DIV_ROUND_UP(data_len
, TBNET_MAX_PAYLOAD_SIZE
);
1122 if (tbnet_available_buffers(&net
->tx_ring
) < nframes
) {
1123 netif_stop_queue(net
->dev
);
1124 return NETDEV_TX_BUSY
;
1127 frames
[frame_index
] = tbnet_get_tx_buffer(net
);
1128 if (!frames
[frame_index
])
1131 hdr
= page_address(frames
[frame_index
]->page
);
1134 /* If overall packet is bigger than the frame data size */
1135 while (data_len
> TBNET_MAX_PAYLOAD_SIZE
) {
1136 unsigned int size_left
= TBNET_MAX_PAYLOAD_SIZE
;
1138 hdr
->frame_size
= cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE
);
1139 hdr
->frame_index
= cpu_to_le16(frame_index
);
1140 hdr
->frame_id
= cpu_to_le16(frame_id
);
1143 if (len
> size_left
) {
1144 /* Copy data onto Tx buffer data with
1145 * full frame size then break and go to
1148 memcpy(dest
, src
, size_left
);
1155 memcpy(dest
, src
, len
);
1164 /* Ensure all fragments have been processed */
1165 if (frag
< skb_shinfo(skb
)->nr_frags
) {
1166 /* Map and then unmap quickly */
1167 src
= tbnet_kmap_frag(skb
, frag
++, &len
);
1169 } else if (unlikely(size_left
> 0)) {
1172 } while (size_left
> 0);
1174 data_len
-= TBNET_MAX_PAYLOAD_SIZE
;
1177 frames
[frame_index
] = tbnet_get_tx_buffer(net
);
1178 if (!frames
[frame_index
])
1181 hdr
= page_address(frames
[frame_index
]->page
);
1185 hdr
->frame_size
= cpu_to_le32(data_len
);
1186 hdr
->frame_index
= cpu_to_le16(frame_index
);
1187 hdr
->frame_id
= cpu_to_le16(frame_id
);
1189 frames
[frame_index
]->frame
.size
= data_len
+ sizeof(*hdr
);
1191 /* In case the remaining data_len is smaller than a frame */
1192 while (len
< data_len
) {
1193 memcpy(dest
, src
, len
);
1202 if (frag
< skb_shinfo(skb
)->nr_frags
) {
1203 src
= tbnet_kmap_frag(skb
, frag
++, &len
);
1205 } else if (unlikely(data_len
> 0)) {
1210 memcpy(dest
, src
, data_len
);
1215 if (!tbnet_xmit_csum_and_map(net
, skb
, frames
, frame_index
+ 1))
1218 for (i
= 0; i
< frame_index
+ 1; i
++)
1219 tb_ring_tx(net
->tx_ring
.ring
, &frames
[i
]->frame
);
1221 if (net
->svc
->prtcstns
& TBNET_MATCH_FRAGS_ID
)
1222 atomic_inc(&net
->frame_id
);
1224 net
->stats
.tx_packets
++;
1225 net
->stats
.tx_bytes
+= skb
->len
;
1227 trace_tbnet_consume_skb(skb
);
1228 dev_consume_skb_any(skb
);
1230 return NETDEV_TX_OK
;
1233 /* We can re-use the buffers */
1234 net
->tx_ring
.cons
-= frame_index
;
1236 dev_kfree_skb_any(skb
);
1237 net
->stats
.tx_errors
++;
1239 return NETDEV_TX_OK
;
1242 static void tbnet_get_stats64(struct net_device
*dev
,
1243 struct rtnl_link_stats64
*stats
)
1245 struct tbnet
*net
= netdev_priv(dev
);
1247 stats
->tx_packets
= net
->stats
.tx_packets
;
1248 stats
->rx_packets
= net
->stats
.rx_packets
;
1249 stats
->tx_bytes
= net
->stats
.tx_bytes
;
1250 stats
->rx_bytes
= net
->stats
.rx_bytes
;
1251 stats
->rx_errors
= net
->stats
.rx_errors
+ net
->stats
.rx_length_errors
+
1252 net
->stats
.rx_over_errors
+ net
->stats
.rx_crc_errors
+
1253 net
->stats
.rx_missed_errors
;
1254 stats
->tx_errors
= net
->stats
.tx_errors
;
1255 stats
->rx_length_errors
= net
->stats
.rx_length_errors
;
1256 stats
->rx_over_errors
= net
->stats
.rx_over_errors
;
1257 stats
->rx_crc_errors
= net
->stats
.rx_crc_errors
;
1258 stats
->rx_missed_errors
= net
->stats
.rx_missed_errors
;
1261 static const struct net_device_ops tbnet_netdev_ops
= {
1262 .ndo_open
= tbnet_open
,
1263 .ndo_stop
= tbnet_stop
,
1264 .ndo_start_xmit
= tbnet_start_xmit
,
1265 .ndo_get_stats64
= tbnet_get_stats64
,
1268 static void tbnet_generate_mac(struct net_device
*dev
)
1270 const struct tbnet
*net
= netdev_priv(dev
);
1271 const struct tb_xdomain
*xd
= net
->xd
;
1276 phy_port
= tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd
->route
));
1278 /* Unicast and locally administered MAC */
1279 addr
[0] = phy_port
<< 4 | 0x02;
1280 hash
= jhash2((u32
*)xd
->local_uuid
, 4, 0);
1281 memcpy(addr
+ 1, &hash
, sizeof(hash
));
1282 hash
= jhash2((u32
*)xd
->local_uuid
, 4, hash
);
1283 addr
[5] = hash
& 0xff;
1284 eth_hw_addr_set(dev
, addr
);
1287 static int tbnet_probe(struct tb_service
*svc
, const struct tb_service_id
*id
)
1289 struct tb_xdomain
*xd
= tb_service_parent(svc
);
1290 struct net_device
*dev
;
1294 dev
= alloc_etherdev(sizeof(*net
));
1298 SET_NETDEV_DEV(dev
, &svc
->dev
);
1300 net
= netdev_priv(dev
);
1301 INIT_DELAYED_WORK(&net
->login_work
, tbnet_login_work
);
1302 INIT_WORK(&net
->connected_work
, tbnet_connected_work
);
1303 INIT_WORK(&net
->disconnect_work
, tbnet_disconnect_work
);
1304 mutex_init(&net
->connection_lock
);
1305 atomic_set(&net
->command_id
, 0);
1306 atomic_set(&net
->frame_id
, 0);
1311 tbnet_generate_mac(dev
);
1313 strcpy(dev
->name
, "thunderbolt%d");
1314 dev
->netdev_ops
= &tbnet_netdev_ops
;
1316 /* ThunderboltIP takes advantage of TSO packets but instead of
1317 * segmenting them we just split the packet into Thunderbolt
1318 * frames (maximum payload size of each frame is 4084 bytes) and
1319 * calculate checksum over the whole packet here.
1321 * The receiving side does the opposite if the host OS supports
1322 * LRO, otherwise it needs to split the large packet into MTU
1323 * sized smaller packets.
1325 * In order to receive large packets from the networking stack,
1326 * we need to announce support for most of the offloading
1329 dev
->hw_features
= NETIF_F_SG
| NETIF_F_ALL_TSO
| NETIF_F_GRO
|
1330 NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1331 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
;
1332 dev
->hard_header_len
+= sizeof(struct thunderbolt_ip_frame_header
);
1334 netif_napi_add(dev
, &net
->napi
, tbnet_poll
);
1336 /* MTU range: 68 - 65522 */
1337 dev
->min_mtu
= ETH_MIN_MTU
;
1338 dev
->max_mtu
= TBNET_MAX_MTU
- ETH_HLEN
;
1340 net
->handler
.uuid
= &tbnet_svc_uuid
;
1341 net
->handler
.callback
= tbnet_handle_packet
;
1342 net
->handler
.data
= net
;
1343 tb_register_protocol_handler(&net
->handler
);
1345 tb_service_set_drvdata(svc
, net
);
1347 ret
= register_netdev(dev
);
1349 tb_unregister_protocol_handler(&net
->handler
);
1357 static void tbnet_remove(struct tb_service
*svc
)
1359 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1361 unregister_netdev(net
->dev
);
1362 tb_unregister_protocol_handler(&net
->handler
);
1363 free_netdev(net
->dev
);
1366 static void tbnet_shutdown(struct tb_service
*svc
)
1368 tbnet_tear_down(tb_service_get_drvdata(svc
), true);
1371 static int tbnet_suspend(struct device
*dev
)
1373 struct tb_service
*svc
= tb_to_service(dev
);
1374 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1377 if (netif_running(net
->dev
)) {
1378 netif_device_detach(net
->dev
);
1379 tbnet_tear_down(net
, true);
1382 tb_unregister_protocol_handler(&net
->handler
);
1386 static int tbnet_resume(struct device
*dev
)
1388 struct tb_service
*svc
= tb_to_service(dev
);
1389 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1391 tb_register_protocol_handler(&net
->handler
);
1393 netif_carrier_off(net
->dev
);
1394 if (netif_running(net
->dev
)) {
1395 netif_device_attach(net
->dev
);
1402 static DEFINE_SIMPLE_DEV_PM_OPS(tbnet_pm_ops
, tbnet_suspend
, tbnet_resume
);
1404 static const struct tb_service_id tbnet_ids
[] = {
1405 { TB_SERVICE("network", 1) },
1408 MODULE_DEVICE_TABLE(tbsvc
, tbnet_ids
);
1410 static struct tb_service_driver tbnet_driver
= {
1412 .owner
= THIS_MODULE
,
1413 .name
= "thunderbolt-net",
1414 .pm
= pm_sleep_ptr(&tbnet_pm_ops
),
1416 .probe
= tbnet_probe
,
1417 .remove
= tbnet_remove
,
1418 .shutdown
= tbnet_shutdown
,
1419 .id_table
= tbnet_ids
,
1422 static int __init
tbnet_init(void)
1427 tbnet_dir
= tb_property_create_dir(&tbnet_dir_uuid
);
1431 tb_property_add_immediate(tbnet_dir
, "prtcid", 1);
1432 tb_property_add_immediate(tbnet_dir
, "prtcvers", 1);
1433 tb_property_add_immediate(tbnet_dir
, "prtcrevs", 1);
1435 flags
= TBNET_MATCH_FRAGS_ID
| TBNET_64K_FRAMES
;
1438 tb_property_add_immediate(tbnet_dir
, "prtcstns", flags
);
1440 ret
= tb_register_property_dir("network", tbnet_dir
);
1444 ret
= tb_register_service_driver(&tbnet_driver
);
1446 goto err_unregister
;
1451 tb_unregister_property_dir("network", tbnet_dir
);
1453 tb_property_free_dir(tbnet_dir
);
1457 module_init(tbnet_init
);
1459 static void __exit
tbnet_exit(void)
1461 tb_unregister_service_driver(&tbnet_driver
);
1462 tb_unregister_property_dir("network", tbnet_dir
);
1463 tb_property_free_dir(tbnet_dir
);
1465 module_exit(tbnet_exit
);
1467 MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
1468 MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
1469 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1470 MODULE_DESCRIPTION("Thunderbolt/USB4 network driver");
1471 MODULE_LICENSE("GPL v2");