2 * Networking over Thunderbolt cable using Apple ThunderboltIP protocol
4 * Copyright (C) 2017, Intel Corporation
5 * Authors: Amir Levy <amir.jer.levy@intel.com>
6 * Michael Jamet <michael.jamet@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/atomic.h>
15 #include <linux/highmem.h>
16 #include <linux/if_vlan.h>
17 #include <linux/jhash.h>
18 #include <linux/module.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/sizes.h>
22 #include <linux/thunderbolt.h>
23 #include <linux/uuid.h>
24 #include <linux/workqueue.h>
26 #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 100
33 #define TBNET_RING_SIZE 256
34 #define TBNET_LOCAL_PATH 0xf
35 #define TBNET_LOGIN_RETRIES 60
36 #define TBNET_LOGOUT_RETRIES 5
37 #define TBNET_MATCH_FRAGS_ID BIT(1)
38 #define TBNET_MAX_MTU SZ_64K
39 #define TBNET_FRAME_SIZE SZ_4K
40 #define TBNET_MAX_PAYLOAD_SIZE \
41 (TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
42 /* Rx packets need to hold space for skb_shared_info */
43 #define TBNET_RX_MAX_SIZE \
44 (TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
45 #define TBNET_RX_PAGE_ORDER get_order(TBNET_RX_MAX_SIZE)
46 #define TBNET_RX_PAGE_SIZE (PAGE_SIZE << TBNET_RX_PAGE_ORDER)
48 #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
51 * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
52 * @frame_size: size of the data with the frame
53 * @frame_index: running index on the frames
54 * @frame_id: ID of the frame to match frames to specific packet
55 * @frame_count: how many frames assembles a full packet
57 * Each data frame passed to the high-speed DMA ring has this header. If
58 * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
59 * supported then @frame_id is filled, otherwise it stays %0.
61 struct thunderbolt_ip_frame_header
{
68 enum thunderbolt_ip_frame_pdf
{
69 TBIP_PDF_FRAME_START
= 1,
73 enum thunderbolt_ip_type
{
80 struct thunderbolt_ip_header
{
85 uuid_t initiator_uuid
;
91 #define TBIP_HDR_LENGTH_MASK GENMASK(5, 0)
92 #define TBIP_HDR_SN_MASK GENMASK(28, 27)
93 #define TBIP_HDR_SN_SHIFT 27
95 struct thunderbolt_ip_login
{
96 struct thunderbolt_ip_header hdr
;
102 #define TBIP_LOGIN_PROTO_VERSION 1
104 struct thunderbolt_ip_login_response
{
105 struct thunderbolt_ip_header hdr
;
108 u32 receiver_mac_len
;
112 struct thunderbolt_ip_logout
{
113 struct thunderbolt_ip_header hdr
;
116 struct thunderbolt_ip_status
{
117 struct thunderbolt_ip_header hdr
;
128 u64 rx_length_errors
;
131 u64 rx_missed_errors
;
135 struct net_device
*dev
;
137 struct ring_frame frame
;
141 struct tbnet_frame frames
[TBNET_RING_SIZE
];
144 struct tb_ring
*ring
;
148 * struct tbnet - ThunderboltIP network driver private data
149 * @svc: XDomain service the driver is bound to
150 * @xd: XDomain the service blongs to
151 * @handler: ThunderboltIP configuration protocol handler
152 * @dev: Networking device
153 * @napi: NAPI structure for Rx polling
154 * @stats: Network statistics
155 * @skb: Network packet that is currently processed on Rx path
156 * @command_id: ID used for next configuration protocol packet
157 * @login_sent: ThunderboltIP login message successfully sent
158 * @login_received: ThunderboltIP login message received from the remote
160 * @transmit_path: HopID the other end needs to use building the
161 * opposite side path.
162 * @connection_lock: Lock serializing access to @login_sent,
163 * @login_received and @transmit_path.
164 * @login_retries: Number of login retries currently done
165 * @login_work: Worker to send ThunderboltIP login packets
166 * @connected_work: Worker that finalizes the ThunderboltIP connection
167 * setup and enables DMA paths for high speed data
169 * @rx_hdr: Copy of the currently processed Rx frame. Used when a
170 * network packet consists of multiple Thunderbolt frames.
171 * In host byte order.
172 * @rx_ring: Software ring holding Rx frames
173 * @frame_id: Frame ID use for next Tx packet
174 * (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
175 * @tx_ring: Software ring holding Tx frames
178 const struct tb_service
*svc
;
179 struct tb_xdomain
*xd
;
180 struct tb_protocol_handler handler
;
181 struct net_device
*dev
;
182 struct napi_struct napi
;
183 struct tbnet_stats stats
;
189 struct mutex connection_lock
;
191 struct delayed_work login_work
;
192 struct work_struct connected_work
;
193 struct thunderbolt_ip_frame_header rx_hdr
;
194 struct tbnet_ring rx_ring
;
196 struct tbnet_ring tx_ring
;
199 /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
200 static const uuid_t tbnet_dir_uuid
=
201 UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
202 0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
204 /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
205 static const uuid_t tbnet_svc_uuid
=
206 UUID_INIT(0x798f589e, 0x3616, 0x8a47,
207 0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
209 static struct tb_property_dir
*tbnet_dir
;
211 static void tbnet_fill_header(struct thunderbolt_ip_header
*hdr
, u64 route
,
212 u8 sequence
, const uuid_t
*initiator_uuid
, const uuid_t
*target_uuid
,
213 enum thunderbolt_ip_type type
, size_t size
, u32 command_id
)
217 /* Length does not include route_hi/lo and length_sn fields */
218 length_sn
= (size
- 3 * 4) / 4;
219 length_sn
|= (sequence
<< TBIP_HDR_SN_SHIFT
) & TBIP_HDR_SN_MASK
;
221 hdr
->route_hi
= upper_32_bits(route
);
222 hdr
->route_lo
= lower_32_bits(route
);
223 hdr
->length_sn
= length_sn
;
224 uuid_copy(&hdr
->uuid
, &tbnet_svc_uuid
);
225 uuid_copy(&hdr
->initiator_uuid
, initiator_uuid
);
226 uuid_copy(&hdr
->target_uuid
, target_uuid
);
228 hdr
->command_id
= command_id
;
231 static int tbnet_login_response(struct tbnet
*net
, u64 route
, u8 sequence
,
234 struct thunderbolt_ip_login_response reply
;
235 struct tb_xdomain
*xd
= net
->xd
;
237 memset(&reply
, 0, sizeof(reply
));
238 tbnet_fill_header(&reply
.hdr
, route
, sequence
, xd
->local_uuid
,
239 xd
->remote_uuid
, TBIP_LOGIN_RESPONSE
, sizeof(reply
),
241 memcpy(reply
.receiver_mac
, net
->dev
->dev_addr
, ETH_ALEN
);
242 reply
.receiver_mac_len
= ETH_ALEN
;
244 return tb_xdomain_response(xd
, &reply
, sizeof(reply
),
245 TB_CFG_PKG_XDOMAIN_RESP
);
248 static int tbnet_login_request(struct tbnet
*net
, u8 sequence
)
250 struct thunderbolt_ip_login_response reply
;
251 struct thunderbolt_ip_login request
;
252 struct tb_xdomain
*xd
= net
->xd
;
254 memset(&request
, 0, sizeof(request
));
255 tbnet_fill_header(&request
.hdr
, xd
->route
, sequence
, xd
->local_uuid
,
256 xd
->remote_uuid
, TBIP_LOGIN
, sizeof(request
),
257 atomic_inc_return(&net
->command_id
));
259 request
.proto_version
= TBIP_LOGIN_PROTO_VERSION
;
260 request
.transmit_path
= TBNET_LOCAL_PATH
;
262 return tb_xdomain_request(xd
, &request
, sizeof(request
),
263 TB_CFG_PKG_XDOMAIN_RESP
, &reply
,
264 sizeof(reply
), TB_CFG_PKG_XDOMAIN_RESP
,
265 TBNET_LOGIN_TIMEOUT
);
268 static int tbnet_logout_response(struct tbnet
*net
, u64 route
, u8 sequence
,
271 struct thunderbolt_ip_status reply
;
272 struct tb_xdomain
*xd
= net
->xd
;
274 memset(&reply
, 0, sizeof(reply
));
275 tbnet_fill_header(&reply
.hdr
, route
, sequence
, xd
->local_uuid
,
276 xd
->remote_uuid
, TBIP_STATUS
, sizeof(reply
),
277 atomic_inc_return(&net
->command_id
));
278 return tb_xdomain_response(xd
, &reply
, sizeof(reply
),
279 TB_CFG_PKG_XDOMAIN_RESP
);
282 static int tbnet_logout_request(struct tbnet
*net
)
284 struct thunderbolt_ip_logout request
;
285 struct thunderbolt_ip_status reply
;
286 struct tb_xdomain
*xd
= net
->xd
;
288 memset(&request
, 0, sizeof(request
));
289 tbnet_fill_header(&request
.hdr
, xd
->route
, 0, xd
->local_uuid
,
290 xd
->remote_uuid
, TBIP_LOGOUT
, sizeof(request
),
291 atomic_inc_return(&net
->command_id
));
293 return tb_xdomain_request(xd
, &request
, sizeof(request
),
294 TB_CFG_PKG_XDOMAIN_RESP
, &reply
,
295 sizeof(reply
), TB_CFG_PKG_XDOMAIN_RESP
,
296 TBNET_LOGOUT_TIMEOUT
);
299 static void start_login(struct tbnet
*net
)
301 mutex_lock(&net
->connection_lock
);
302 net
->login_sent
= false;
303 net
->login_received
= false;
304 mutex_unlock(&net
->connection_lock
);
306 queue_delayed_work(system_long_wq
, &net
->login_work
,
307 msecs_to_jiffies(1000));
310 static void stop_login(struct tbnet
*net
)
312 cancel_delayed_work_sync(&net
->login_work
);
313 cancel_work_sync(&net
->connected_work
);
316 static inline unsigned int tbnet_frame_size(const struct tbnet_frame
*tf
)
318 return tf
->frame
.size
? : TBNET_FRAME_SIZE
;
321 static void tbnet_free_buffers(struct tbnet_ring
*ring
)
325 for (i
= 0; i
< TBNET_RING_SIZE
; i
++) {
326 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
327 struct tbnet_frame
*tf
= &ring
->frames
[i
];
328 enum dma_data_direction dir
;
335 if (ring
->ring
->is_tx
) {
338 size
= tbnet_frame_size(tf
);
340 dir
= DMA_FROM_DEVICE
;
341 order
= TBNET_RX_PAGE_ORDER
;
342 size
= TBNET_RX_PAGE_SIZE
;
345 if (tf
->frame
.buffer_phy
)
346 dma_unmap_page(dma_dev
, tf
->frame
.buffer_phy
, size
,
349 __free_pages(tf
->page
, order
);
357 static void tbnet_tear_down(struct tbnet
*net
, bool send_logout
)
359 netif_carrier_off(net
->dev
);
360 netif_stop_queue(net
->dev
);
364 mutex_lock(&net
->connection_lock
);
366 if (net
->login_sent
&& net
->login_received
) {
367 int retries
= TBNET_LOGOUT_RETRIES
;
369 while (send_logout
&& retries
-- > 0) {
370 int ret
= tbnet_logout_request(net
);
371 if (ret
!= -ETIMEDOUT
)
375 tb_ring_stop(net
->rx_ring
.ring
);
376 tb_ring_stop(net
->tx_ring
.ring
);
377 tbnet_free_buffers(&net
->rx_ring
);
378 tbnet_free_buffers(&net
->tx_ring
);
380 if (tb_xdomain_disable_paths(net
->xd
))
381 netdev_warn(net
->dev
, "failed to disable DMA paths\n");
384 net
->login_retries
= 0;
385 net
->login_sent
= false;
386 net
->login_received
= false;
388 mutex_unlock(&net
->connection_lock
);
391 static int tbnet_handle_packet(const void *buf
, size_t size
, void *data
)
393 const struct thunderbolt_ip_login
*pkg
= buf
;
394 struct tbnet
*net
= data
;
400 /* Make sure the packet is for us */
401 if (size
< sizeof(struct thunderbolt_ip_header
))
403 if (!uuid_equal(&pkg
->hdr
.initiator_uuid
, net
->xd
->remote_uuid
))
405 if (!uuid_equal(&pkg
->hdr
.target_uuid
, net
->xd
->local_uuid
))
408 route
= ((u64
)pkg
->hdr
.route_hi
<< 32) | pkg
->hdr
.route_lo
;
409 route
&= ~BIT_ULL(63);
410 if (route
!= net
->xd
->route
)
413 sequence
= pkg
->hdr
.length_sn
& TBIP_HDR_SN_MASK
;
414 sequence
>>= TBIP_HDR_SN_SHIFT
;
415 command_id
= pkg
->hdr
.command_id
;
417 switch (pkg
->hdr
.type
) {
419 if (!netif_running(net
->dev
))
422 ret
= tbnet_login_response(net
, route
, sequence
,
423 pkg
->hdr
.command_id
);
425 mutex_lock(&net
->connection_lock
);
426 net
->login_received
= true;
427 net
->transmit_path
= pkg
->transmit_path
;
429 /* If we reached the number of max retries or
430 * previous logout, schedule another round of
433 if (net
->login_retries
>= TBNET_LOGIN_RETRIES
||
435 net
->login_retries
= 0;
436 queue_delayed_work(system_long_wq
,
437 &net
->login_work
, 0);
439 mutex_unlock(&net
->connection_lock
);
441 queue_work(system_long_wq
, &net
->connected_work
);
446 ret
= tbnet_logout_response(net
, route
, sequence
, command_id
);
448 tbnet_tear_down(net
, false);
456 netdev_warn(net
->dev
, "failed to send ThunderboltIP response\n");
461 static unsigned int tbnet_available_buffers(const struct tbnet_ring
*ring
)
463 return ring
->prod
- ring
->cons
;
466 static int tbnet_alloc_rx_buffers(struct tbnet
*net
, unsigned int nbuffers
)
468 struct tbnet_ring
*ring
= &net
->rx_ring
;
472 struct device
*dma_dev
= tb_ring_dma_device(ring
->ring
);
473 unsigned int index
= ring
->prod
& (TBNET_RING_SIZE
- 1);
474 struct tbnet_frame
*tf
= &ring
->frames
[index
];
480 /* Allocate page (order > 0) so that it can hold maximum
481 * ThunderboltIP frame (4kB) and the additional room for
482 * SKB shared info required by build_skb().
484 tf
->page
= dev_alloc_pages(TBNET_RX_PAGE_ORDER
);
490 dma_addr
= dma_map_page(dma_dev
, tf
->page
, 0,
491 TBNET_RX_PAGE_SIZE
, DMA_FROM_DEVICE
);
492 if (dma_mapping_error(dma_dev
, dma_addr
)) {
497 tf
->frame
.buffer_phy
= dma_addr
;
500 tb_ring_rx(ring
->ring
, &tf
->frame
);
508 tbnet_free_buffers(ring
);
512 static struct tbnet_frame
*tbnet_get_tx_buffer(struct tbnet
*net
)
514 struct tbnet_ring
*ring
= &net
->tx_ring
;
515 struct tbnet_frame
*tf
;
518 if (!tbnet_available_buffers(ring
))
521 index
= ring
->cons
++ & (TBNET_RING_SIZE
- 1);
523 tf
= &ring
->frames
[index
];
525 tf
->frame
.buffer_phy
= 0;
530 static void tbnet_tx_callback(struct tb_ring
*ring
, struct ring_frame
*frame
,
533 struct tbnet_frame
*tf
= container_of(frame
, typeof(*tf
), frame
);
534 struct device
*dma_dev
= tb_ring_dma_device(ring
);
535 struct tbnet
*net
= netdev_priv(tf
->dev
);
537 dma_unmap_page(dma_dev
, tf
->frame
.buffer_phy
, tbnet_frame_size(tf
),
539 tf
->frame
.buffer_phy
= 0;
541 /* Return buffer to the ring */
544 if (tbnet_available_buffers(&net
->tx_ring
) >= TBNET_RING_SIZE
/ 2)
545 netif_wake_queue(net
->dev
);
548 static int tbnet_alloc_tx_buffers(struct tbnet
*net
)
550 struct tbnet_ring
*ring
= &net
->tx_ring
;
553 for (i
= 0; i
< TBNET_RING_SIZE
; i
++) {
554 struct tbnet_frame
*tf
= &ring
->frames
[i
];
556 tf
->page
= alloc_page(GFP_KERNEL
);
558 tbnet_free_buffers(ring
);
563 tf
->frame
.callback
= tbnet_tx_callback
;
564 tf
->frame
.sof
= TBIP_PDF_FRAME_START
;
565 tf
->frame
.eof
= TBIP_PDF_FRAME_END
;
569 ring
->prod
= TBNET_RING_SIZE
- 1;
574 static void tbnet_connected_work(struct work_struct
*work
)
576 struct tbnet
*net
= container_of(work
, typeof(*net
), connected_work
);
580 if (netif_carrier_ok(net
->dev
))
583 mutex_lock(&net
->connection_lock
);
584 connected
= net
->login_sent
&& net
->login_received
;
585 mutex_unlock(&net
->connection_lock
);
590 /* Both logins successful so enable the high-speed DMA paths and
591 * start the network device queue.
593 ret
= tb_xdomain_enable_paths(net
->xd
, TBNET_LOCAL_PATH
,
594 net
->rx_ring
.ring
->hop
,
596 net
->tx_ring
.ring
->hop
);
598 netdev_err(net
->dev
, "failed to enable DMA paths\n");
602 tb_ring_start(net
->tx_ring
.ring
);
603 tb_ring_start(net
->rx_ring
.ring
);
605 ret
= tbnet_alloc_rx_buffers(net
, TBNET_RING_SIZE
);
609 ret
= tbnet_alloc_tx_buffers(net
);
611 goto err_free_rx_buffers
;
613 netif_carrier_on(net
->dev
);
614 netif_start_queue(net
->dev
);
618 tbnet_free_buffers(&net
->rx_ring
);
620 tb_ring_stop(net
->rx_ring
.ring
);
621 tb_ring_stop(net
->tx_ring
.ring
);
624 static void tbnet_login_work(struct work_struct
*work
)
626 struct tbnet
*net
= container_of(work
, typeof(*net
), login_work
.work
);
627 unsigned long delay
= msecs_to_jiffies(TBNET_LOGIN_DELAY
);
630 if (netif_carrier_ok(net
->dev
))
633 ret
= tbnet_login_request(net
, net
->login_retries
% 4);
635 if (net
->login_retries
++ < TBNET_LOGIN_RETRIES
) {
636 queue_delayed_work(system_long_wq
, &net
->login_work
,
639 netdev_info(net
->dev
, "ThunderboltIP login timed out\n");
642 net
->login_retries
= 0;
644 mutex_lock(&net
->connection_lock
);
645 net
->login_sent
= true;
646 mutex_unlock(&net
->connection_lock
);
648 queue_work(system_long_wq
, &net
->connected_work
);
652 static bool tbnet_check_frame(struct tbnet
*net
, const struct tbnet_frame
*tf
,
653 const struct thunderbolt_ip_frame_header
*hdr
)
655 u32 frame_id
, frame_count
, frame_size
, frame_index
;
658 if (tf
->frame
.flags
& RING_DESC_CRC_ERROR
) {
659 net
->stats
.rx_crc_errors
++;
661 } else if (tf
->frame
.flags
& RING_DESC_BUFFER_OVERRUN
) {
662 net
->stats
.rx_over_errors
++;
666 /* Should be greater than just header i.e. contains data */
667 size
= tbnet_frame_size(tf
);
668 if (size
<= sizeof(*hdr
)) {
669 net
->stats
.rx_length_errors
++;
673 frame_count
= le32_to_cpu(hdr
->frame_count
);
674 frame_size
= le32_to_cpu(hdr
->frame_size
);
675 frame_index
= le16_to_cpu(hdr
->frame_index
);
676 frame_id
= le16_to_cpu(hdr
->frame_id
);
678 if ((frame_size
> size
- sizeof(*hdr
)) || !frame_size
) {
679 net
->stats
.rx_length_errors
++;
683 /* In case we're in the middle of packet, validate the frame
684 * header based on first fragment of the packet.
686 if (net
->skb
&& net
->rx_hdr
.frame_count
) {
687 /* Check the frame count fits the count field */
688 if (frame_count
!= net
->rx_hdr
.frame_count
) {
689 net
->stats
.rx_length_errors
++;
693 /* Check the frame identifiers are incremented correctly,
694 * and id is matching.
696 if (frame_index
!= net
->rx_hdr
.frame_index
+ 1 ||
697 frame_id
!= net
->rx_hdr
.frame_id
) {
698 net
->stats
.rx_missed_errors
++;
702 if (net
->skb
->len
+ frame_size
> TBNET_MAX_MTU
) {
703 net
->stats
.rx_length_errors
++;
710 /* Start of packet, validate the frame header */
711 if (frame_count
== 0 || frame_count
> TBNET_RING_SIZE
/ 4) {
712 net
->stats
.rx_length_errors
++;
715 if (frame_index
!= 0) {
716 net
->stats
.rx_missed_errors
++;
723 static int tbnet_poll(struct napi_struct
*napi
, int budget
)
725 struct tbnet
*net
= container_of(napi
, struct tbnet
, napi
);
726 unsigned int cleaned_count
= tbnet_available_buffers(&net
->rx_ring
);
727 struct device
*dma_dev
= tb_ring_dma_device(net
->rx_ring
.ring
);
728 unsigned int rx_packets
= 0;
730 while (rx_packets
< budget
) {
731 const struct thunderbolt_ip_frame_header
*hdr
;
732 unsigned int hdr_size
= sizeof(*hdr
);
733 struct sk_buff
*skb
= NULL
;
734 struct ring_frame
*frame
;
735 struct tbnet_frame
*tf
;
740 /* Return some buffers to hardware, one at a time is too
741 * slow so allocate MAX_SKB_FRAGS buffers at the same
744 if (cleaned_count
>= MAX_SKB_FRAGS
) {
745 tbnet_alloc_rx_buffers(net
, cleaned_count
);
749 frame
= tb_ring_poll(net
->rx_ring
.ring
);
753 dma_unmap_page(dma_dev
, frame
->buffer_phy
,
754 TBNET_RX_PAGE_SIZE
, DMA_FROM_DEVICE
);
756 tf
= container_of(frame
, typeof(*tf
), frame
);
763 hdr
= page_address(page
);
764 if (!tbnet_check_frame(net
, tf
, hdr
)) {
765 __free_pages(page
, TBNET_RX_PAGE_ORDER
);
766 dev_kfree_skb_any(net
->skb
);
771 frame_size
= le32_to_cpu(hdr
->frame_size
);
775 skb
= build_skb(page_address(page
),
778 __free_pages(page
, TBNET_RX_PAGE_ORDER
);
779 net
->stats
.rx_errors
++;
783 skb_reserve(skb
, hdr_size
);
784 skb_put(skb
, frame_size
);
788 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
789 page
, hdr_size
, frame_size
,
790 TBNET_RX_PAGE_SIZE
- hdr_size
);
793 net
->rx_hdr
.frame_size
= frame_size
;
794 net
->rx_hdr
.frame_count
= le32_to_cpu(hdr
->frame_count
);
795 net
->rx_hdr
.frame_index
= le16_to_cpu(hdr
->frame_index
);
796 net
->rx_hdr
.frame_id
= le16_to_cpu(hdr
->frame_id
);
797 last
= net
->rx_hdr
.frame_index
== net
->rx_hdr
.frame_count
- 1;
800 net
->stats
.rx_bytes
+= frame_size
;
803 skb
->protocol
= eth_type_trans(skb
, net
->dev
);
804 napi_gro_receive(&net
->napi
, skb
);
809 net
->stats
.rx_packets
+= rx_packets
;
812 tbnet_alloc_rx_buffers(net
, cleaned_count
);
814 if (rx_packets
>= budget
)
817 napi_complete_done(napi
, rx_packets
);
818 /* Re-enable the ring interrupt */
819 tb_ring_poll_complete(net
->rx_ring
.ring
);
824 static void tbnet_start_poll(void *data
)
826 struct tbnet
*net
= data
;
828 napi_schedule(&net
->napi
);
831 static int tbnet_open(struct net_device
*dev
)
833 struct tbnet
*net
= netdev_priv(dev
);
834 struct tb_xdomain
*xd
= net
->xd
;
835 u16 sof_mask
, eof_mask
;
836 struct tb_ring
*ring
;
838 netif_carrier_off(dev
);
840 ring
= tb_ring_alloc_tx(xd
->tb
->nhi
, -1, TBNET_RING_SIZE
,
843 netdev_err(dev
, "failed to allocate Tx ring\n");
846 net
->tx_ring
.ring
= ring
;
848 sof_mask
= BIT(TBIP_PDF_FRAME_START
);
849 eof_mask
= BIT(TBIP_PDF_FRAME_END
);
851 ring
= tb_ring_alloc_rx(xd
->tb
->nhi
, -1, TBNET_RING_SIZE
,
852 RING_FLAG_FRAME
| RING_FLAG_E2E
, sof_mask
,
853 eof_mask
, tbnet_start_poll
, net
);
855 netdev_err(dev
, "failed to allocate Rx ring\n");
856 tb_ring_free(net
->tx_ring
.ring
);
857 net
->tx_ring
.ring
= NULL
;
860 net
->rx_ring
.ring
= ring
;
862 napi_enable(&net
->napi
);
868 static int tbnet_stop(struct net_device
*dev
)
870 struct tbnet
*net
= netdev_priv(dev
);
872 napi_disable(&net
->napi
);
874 tbnet_tear_down(net
, true);
876 tb_ring_free(net
->rx_ring
.ring
);
877 net
->rx_ring
.ring
= NULL
;
878 tb_ring_free(net
->tx_ring
.ring
);
879 net
->tx_ring
.ring
= NULL
;
884 static bool tbnet_xmit_map(struct device
*dma_dev
, struct tbnet_frame
*tf
)
888 dma_addr
= dma_map_page(dma_dev
, tf
->page
, 0, tbnet_frame_size(tf
),
890 if (dma_mapping_error(dma_dev
, dma_addr
))
893 tf
->frame
.buffer_phy
= dma_addr
;
897 static bool tbnet_xmit_csum_and_map(struct tbnet
*net
, struct sk_buff
*skb
,
898 struct tbnet_frame
**frames
, u32 frame_count
)
900 struct thunderbolt_ip_frame_header
*hdr
= page_address(frames
[0]->page
);
901 struct device
*dma_dev
= tb_ring_dma_device(net
->tx_ring
.ring
);
902 __wsum wsum
= htonl(skb
->len
- skb_transport_offset(skb
));
903 unsigned int i
, len
, offset
= skb_transport_offset(skb
);
904 __be16 protocol
= skb
->protocol
;
905 void *data
= skb
->data
;
906 void *dest
= hdr
+ 1;
909 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
910 /* No need to calculate checksum so we just update the
911 * total frame count and map the frames for DMA.
913 for (i
= 0; i
< frame_count
; i
++) {
914 hdr
= page_address(frames
[i
]->page
);
915 hdr
->frame_count
= cpu_to_le32(frame_count
);
916 if (!tbnet_xmit_map(dma_dev
, frames
[i
]))
923 if (protocol
== htons(ETH_P_8021Q
)) {
924 struct vlan_hdr
*vhdr
, vh
;
926 vhdr
= skb_header_pointer(skb
, ETH_HLEN
, sizeof(vh
), &vh
);
930 protocol
= vhdr
->h_vlan_encapsulated_proto
;
933 /* Data points on the beginning of packet.
934 * Check is the checksum absolute place in the packet.
935 * ipcso will update IP checksum.
936 * tucso will update TCP/UPD checksum.
938 if (protocol
== htons(ETH_P_IP
)) {
939 __sum16
*ipcso
= dest
+ ((void *)&(ip_hdr(skb
)->check
) - data
);
942 *ipcso
= ip_fast_csum(dest
+ skb_network_offset(skb
),
945 if (ip_hdr(skb
)->protocol
== IPPROTO_TCP
)
946 tucso
= dest
+ ((void *)&(tcp_hdr(skb
)->check
) - data
);
947 else if (ip_hdr(skb
)->protocol
== IPPROTO_UDP
)
948 tucso
= dest
+ ((void *)&(udp_hdr(skb
)->check
) - data
);
952 *tucso
= ~csum_tcpudp_magic(ip_hdr(skb
)->saddr
,
953 ip_hdr(skb
)->daddr
, 0,
954 ip_hdr(skb
)->protocol
, 0);
955 } else if (skb_is_gso_v6(skb
)) {
956 tucso
= dest
+ ((void *)&(tcp_hdr(skb
)->check
) - data
);
957 *tucso
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
958 &ipv6_hdr(skb
)->daddr
, 0,
961 } else if (protocol
== htons(ETH_P_IPV6
)) {
962 tucso
= dest
+ skb_checksum_start_offset(skb
) + skb
->csum_offset
;
963 *tucso
= ~csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
964 &ipv6_hdr(skb
)->daddr
, 0,
965 ipv6_hdr(skb
)->nexthdr
, 0);
970 /* First frame was headers, rest of the frames contain data.
971 * Calculate checksum over each frame.
973 for (i
= 0; i
< frame_count
; i
++) {
974 hdr
= page_address(frames
[i
]->page
);
975 dest
= (void *)(hdr
+ 1) + offset
;
976 len
= le32_to_cpu(hdr
->frame_size
) - offset
;
977 wsum
= csum_partial(dest
, len
, wsum
);
978 hdr
->frame_count
= cpu_to_le32(frame_count
);
983 *tucso
= csum_fold(wsum
);
985 /* Checksum is finally calculated and we don't touch the memory
986 * anymore, so DMA map the frames now.
988 for (i
= 0; i
< frame_count
; i
++) {
989 if (!tbnet_xmit_map(dma_dev
, frames
[i
]))
997 dma_unmap_page(dma_dev
, frames
[i
]->frame
.buffer_phy
,
998 tbnet_frame_size(frames
[i
]), DMA_TO_DEVICE
);
1003 static void *tbnet_kmap_frag(struct sk_buff
*skb
, unsigned int frag_num
,
1006 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[frag_num
];
1008 *len
= skb_frag_size(frag
);
1009 return kmap_atomic(skb_frag_page(frag
)) + frag
->page_offset
;
1012 static netdev_tx_t
tbnet_start_xmit(struct sk_buff
*skb
,
1013 struct net_device
*dev
)
1015 struct tbnet
*net
= netdev_priv(dev
);
1016 struct tbnet_frame
*frames
[MAX_SKB_FRAGS
];
1017 u16 frame_id
= atomic_read(&net
->frame_id
);
1018 struct thunderbolt_ip_frame_header
*hdr
;
1019 unsigned int len
= skb_headlen(skb
);
1020 unsigned int data_len
= skb
->len
;
1021 unsigned int nframes
, i
;
1022 unsigned int frag
= 0;
1023 void *src
= skb
->data
;
1024 u32 frame_index
= 0;
1028 nframes
= DIV_ROUND_UP(data_len
, TBNET_MAX_PAYLOAD_SIZE
);
1029 if (tbnet_available_buffers(&net
->tx_ring
) < nframes
) {
1030 netif_stop_queue(net
->dev
);
1031 return NETDEV_TX_BUSY
;
1034 frames
[frame_index
] = tbnet_get_tx_buffer(net
);
1035 if (!frames
[frame_index
])
1038 hdr
= page_address(frames
[frame_index
]->page
);
1041 /* If overall packet is bigger than the frame data size */
1042 while (data_len
> TBNET_MAX_PAYLOAD_SIZE
) {
1043 unsigned int size_left
= TBNET_MAX_PAYLOAD_SIZE
;
1045 hdr
->frame_size
= cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE
);
1046 hdr
->frame_index
= cpu_to_le16(frame_index
);
1047 hdr
->frame_id
= cpu_to_le16(frame_id
);
1050 if (len
> size_left
) {
1051 /* Copy data onto Tx buffer data with
1052 * full frame size then break and go to
1055 memcpy(dest
, src
, size_left
);
1062 memcpy(dest
, src
, len
);
1071 /* Ensure all fragments have been processed */
1072 if (frag
< skb_shinfo(skb
)->nr_frags
) {
1073 /* Map and then unmap quickly */
1074 src
= tbnet_kmap_frag(skb
, frag
++, &len
);
1076 } else if (unlikely(size_left
> 0)) {
1079 } while (size_left
> 0);
1081 data_len
-= TBNET_MAX_PAYLOAD_SIZE
;
1084 frames
[frame_index
] = tbnet_get_tx_buffer(net
);
1085 if (!frames
[frame_index
])
1088 hdr
= page_address(frames
[frame_index
]->page
);
1092 hdr
->frame_size
= cpu_to_le32(data_len
);
1093 hdr
->frame_index
= cpu_to_le16(frame_index
);
1094 hdr
->frame_id
= cpu_to_le16(frame_id
);
1096 frames
[frame_index
]->frame
.size
= data_len
+ sizeof(*hdr
);
1098 /* In case the remaining data_len is smaller than a frame */
1099 while (len
< data_len
) {
1100 memcpy(dest
, src
, len
);
1109 if (frag
< skb_shinfo(skb
)->nr_frags
) {
1110 src
= tbnet_kmap_frag(skb
, frag
++, &len
);
1112 } else if (unlikely(data_len
> 0)) {
1117 memcpy(dest
, src
, data_len
);
1122 if (!tbnet_xmit_csum_and_map(net
, skb
, frames
, frame_index
+ 1))
1125 for (i
= 0; i
< frame_index
+ 1; i
++)
1126 tb_ring_tx(net
->tx_ring
.ring
, &frames
[i
]->frame
);
1128 if (net
->svc
->prtcstns
& TBNET_MATCH_FRAGS_ID
)
1129 atomic_inc(&net
->frame_id
);
1131 net
->stats
.tx_packets
++;
1132 net
->stats
.tx_bytes
+= skb
->len
;
1134 dev_consume_skb_any(skb
);
1136 return NETDEV_TX_OK
;
1139 /* We can re-use the buffers */
1140 net
->tx_ring
.cons
-= frame_index
;
1142 dev_kfree_skb_any(skb
);
1143 net
->stats
.tx_errors
++;
1145 return NETDEV_TX_OK
;
1148 static void tbnet_get_stats64(struct net_device
*dev
,
1149 struct rtnl_link_stats64
*stats
)
1151 struct tbnet
*net
= netdev_priv(dev
);
1153 stats
->tx_packets
= net
->stats
.tx_packets
;
1154 stats
->rx_packets
= net
->stats
.rx_packets
;
1155 stats
->tx_bytes
= net
->stats
.tx_bytes
;
1156 stats
->rx_bytes
= net
->stats
.rx_bytes
;
1157 stats
->rx_errors
= net
->stats
.rx_errors
+ net
->stats
.rx_length_errors
+
1158 net
->stats
.rx_over_errors
+ net
->stats
.rx_crc_errors
+
1159 net
->stats
.rx_missed_errors
;
1160 stats
->tx_errors
= net
->stats
.tx_errors
;
1161 stats
->rx_length_errors
= net
->stats
.rx_length_errors
;
1162 stats
->rx_over_errors
= net
->stats
.rx_over_errors
;
1163 stats
->rx_crc_errors
= net
->stats
.rx_crc_errors
;
1164 stats
->rx_missed_errors
= net
->stats
.rx_missed_errors
;
1167 static const struct net_device_ops tbnet_netdev_ops
= {
1168 .ndo_open
= tbnet_open
,
1169 .ndo_stop
= tbnet_stop
,
1170 .ndo_start_xmit
= tbnet_start_xmit
,
1171 .ndo_get_stats64
= tbnet_get_stats64
,
1174 static void tbnet_generate_mac(struct net_device
*dev
)
1176 const struct tbnet
*net
= netdev_priv(dev
);
1177 const struct tb_xdomain
*xd
= net
->xd
;
1181 phy_port
= tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd
->route
));
1183 /* Unicast and locally administered MAC */
1184 dev
->dev_addr
[0] = phy_port
<< 4 | 0x02;
1185 hash
= jhash2((u32
*)xd
->local_uuid
, 4, 0);
1186 memcpy(dev
->dev_addr
+ 1, &hash
, sizeof(hash
));
1187 hash
= jhash2((u32
*)xd
->local_uuid
, 4, hash
);
1188 dev
->dev_addr
[5] = hash
& 0xff;
1191 static int tbnet_probe(struct tb_service
*svc
, const struct tb_service_id
*id
)
1193 struct tb_xdomain
*xd
= tb_service_parent(svc
);
1194 struct net_device
*dev
;
1198 dev
= alloc_etherdev(sizeof(*net
));
1202 SET_NETDEV_DEV(dev
, &svc
->dev
);
1204 net
= netdev_priv(dev
);
1205 INIT_DELAYED_WORK(&net
->login_work
, tbnet_login_work
);
1206 INIT_WORK(&net
->connected_work
, tbnet_connected_work
);
1207 mutex_init(&net
->connection_lock
);
1208 atomic_set(&net
->command_id
, 0);
1209 atomic_set(&net
->frame_id
, 0);
1214 tbnet_generate_mac(dev
);
1216 strcpy(dev
->name
, "thunderbolt%d");
1217 dev
->netdev_ops
= &tbnet_netdev_ops
;
1219 /* ThunderboltIP takes advantage of TSO packets but instead of
1220 * segmenting them we just split the packet into Thunderbolt
1221 * frames (maximum payload size of each frame is 4084 bytes) and
1222 * calculate checksum over the whole packet here.
1224 * The receiving side does the opposite if the host OS supports
1225 * LRO, otherwise it needs to split the large packet into MTU
1226 * sized smaller packets.
1228 * In order to receive large packets from the networking stack,
1229 * we need to announce support for most of the offloading
1232 dev
->hw_features
= NETIF_F_SG
| NETIF_F_ALL_TSO
| NETIF_F_GRO
|
1233 NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1234 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
;
1235 dev
->hard_header_len
+= sizeof(struct thunderbolt_ip_frame_header
);
1237 netif_napi_add(dev
, &net
->napi
, tbnet_poll
, NAPI_POLL_WEIGHT
);
1239 /* MTU range: 68 - 65522 */
1240 dev
->min_mtu
= ETH_MIN_MTU
;
1241 dev
->max_mtu
= TBNET_MAX_MTU
- ETH_HLEN
;
1243 net
->handler
.uuid
= &tbnet_svc_uuid
;
1244 net
->handler
.callback
= tbnet_handle_packet
,
1245 net
->handler
.data
= net
;
1246 tb_register_protocol_handler(&net
->handler
);
1248 tb_service_set_drvdata(svc
, net
);
1250 ret
= register_netdev(dev
);
1252 tb_unregister_protocol_handler(&net
->handler
);
1260 static void tbnet_remove(struct tb_service
*svc
)
1262 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1264 unregister_netdev(net
->dev
);
1265 tb_unregister_protocol_handler(&net
->handler
);
1266 free_netdev(net
->dev
);
1269 static void tbnet_shutdown(struct tb_service
*svc
)
1271 tbnet_tear_down(tb_service_get_drvdata(svc
), true);
1274 static int __maybe_unused
tbnet_suspend(struct device
*dev
)
1276 struct tb_service
*svc
= tb_to_service(dev
);
1277 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1280 if (netif_running(net
->dev
)) {
1281 netif_device_detach(net
->dev
);
1282 tb_ring_stop(net
->rx_ring
.ring
);
1283 tb_ring_stop(net
->tx_ring
.ring
);
1284 tbnet_free_buffers(&net
->rx_ring
);
1285 tbnet_free_buffers(&net
->tx_ring
);
1291 static int __maybe_unused
tbnet_resume(struct device
*dev
)
1293 struct tb_service
*svc
= tb_to_service(dev
);
1294 struct tbnet
*net
= tb_service_get_drvdata(svc
);
1296 netif_carrier_off(net
->dev
);
1297 if (netif_running(net
->dev
)) {
1298 netif_device_attach(net
->dev
);
1305 static const struct dev_pm_ops tbnet_pm_ops
= {
1306 SET_SYSTEM_SLEEP_PM_OPS(tbnet_suspend
, tbnet_resume
)
1309 static const struct tb_service_id tbnet_ids
[] = {
1310 { TB_SERVICE("network", 1) },
1313 MODULE_DEVICE_TABLE(tbsvc
, tbnet_ids
);
1315 static struct tb_service_driver tbnet_driver
= {
1317 .owner
= THIS_MODULE
,
1318 .name
= "thunderbolt-net",
1319 .pm
= &tbnet_pm_ops
,
1321 .probe
= tbnet_probe
,
1322 .remove
= tbnet_remove
,
1323 .shutdown
= tbnet_shutdown
,
1324 .id_table
= tbnet_ids
,
1327 static int __init
tbnet_init(void)
1331 tbnet_dir
= tb_property_create_dir(&tbnet_dir_uuid
);
1335 tb_property_add_immediate(tbnet_dir
, "prtcid", 1);
1336 tb_property_add_immediate(tbnet_dir
, "prtcvers", 1);
1337 tb_property_add_immediate(tbnet_dir
, "prtcrevs", 1);
1338 tb_property_add_immediate(tbnet_dir
, "prtcstns",
1339 TBNET_MATCH_FRAGS_ID
);
1341 ret
= tb_register_property_dir("network", tbnet_dir
);
1343 tb_property_free_dir(tbnet_dir
);
1347 return tb_register_service_driver(&tbnet_driver
);
1349 module_init(tbnet_init
);
1351 static void __exit
tbnet_exit(void)
1353 tb_unregister_service_driver(&tbnet_driver
);
1354 tb_unregister_property_dir("network", tbnet_dir
);
1355 tb_property_free_dir(tbnet_dir
);
1357 module_exit(tbnet_exit
);
1359 MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
1360 MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
1361 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
1362 MODULE_DESCRIPTION("Thunderbolt network driver");
1363 MODULE_LICENSE("GPL v2");