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