xref: /linux/drivers/net/thunderbolt/main.c (revision 7d8d6ad659c02ed5d2387777194c22e8e81dbb2b)
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_THROTTLING	128000
38 #define TBNET_E2E		BIT(0)
39 #define TBNET_MATCH_FRAGS_ID	BIT(1)
40 #define TBNET_64K_FRAMES	BIT(2)
41 #define TBNET_MAX_MTU		SZ_64K
42 #define TBNET_FRAME_SIZE	TB_MAX_FRAME_SIZE
43 #define TBNET_MAX_PAYLOAD_SIZE	\
44 	(TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
45 /* Rx packets need to hold space for skb_shared_info */
46 #define TBNET_RX_MAX_SIZE	\
47 	(TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
48 #define TBNET_RX_PAGE_ORDER	get_order(TBNET_RX_MAX_SIZE)
49 #define TBNET_RX_PAGE_SIZE	(PAGE_SIZE << TBNET_RX_PAGE_ORDER)
50 
51 #define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
52 
53 /**
54  * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
55  * @frame_size: size of the data with the frame
56  * @frame_index: running index on the frames
57  * @frame_id: ID of the frame to match frames to specific packet
58  * @frame_count: how many frames assembles a full packet
59  *
60  * Each data frame passed to the high-speed DMA ring has this header. If
61  * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
62  * supported then @frame_id is filled, otherwise it stays %0.
63  */
64 struct thunderbolt_ip_frame_header {
65 	__le32 frame_size;
66 	__le16 frame_index;
67 	__le16 frame_id;
68 	__le32 frame_count;
69 };
70 
71 enum thunderbolt_ip_frame_pdf {
72 	TBIP_PDF_FRAME_START = 1,
73 	TBIP_PDF_FRAME_END,
74 };
75 
76 enum thunderbolt_ip_type {
77 	TBIP_LOGIN,
78 	TBIP_LOGIN_RESPONSE,
79 	TBIP_LOGOUT,
80 	TBIP_STATUS,
81 };
82 
83 struct thunderbolt_ip_header {
84 	u32 route_hi;
85 	u32 route_lo;
86 	u32 length_sn;
87 	uuid_t uuid;
88 	uuid_t initiator_uuid;
89 	uuid_t target_uuid;
90 	u32 type;
91 	u32 command_id;
92 };
93 
94 #define TBIP_HDR_LENGTH_MASK		GENMASK(5, 0)
95 #define TBIP_HDR_SN_MASK		GENMASK(28, 27)
96 #define TBIP_HDR_SN_SHIFT		27
97 
98 struct thunderbolt_ip_login {
99 	struct thunderbolt_ip_header hdr;
100 	u32 proto_version;
101 	u32 transmit_path;
102 	u32 reserved[4];
103 };
104 
105 #define TBIP_LOGIN_PROTO_VERSION	1
106 
107 struct thunderbolt_ip_login_response {
108 	struct thunderbolt_ip_header hdr;
109 	u32 status;
110 	u32 receiver_mac[2];
111 	u32 receiver_mac_len;
112 	u32 reserved[4];
113 };
114 
115 struct thunderbolt_ip_logout {
116 	struct thunderbolt_ip_header hdr;
117 };
118 
119 struct thunderbolt_ip_status {
120 	struct thunderbolt_ip_header hdr;
121 	u32 status;
122 };
123 
124 struct tbnet_stats {
125 	u64 tx_packets;
126 	u64 rx_packets;
127 	u64 tx_bytes;
128 	u64 rx_bytes;
129 	u64 rx_errors;
130 	u64 tx_errors;
131 	u64 rx_length_errors;
132 	u64 rx_over_errors;
133 	u64 rx_crc_errors;
134 	u64 rx_missed_errors;
135 };
136 
137 struct tbnet_frame {
138 	struct net_device *dev;
139 	struct page *page;
140 	struct ring_frame frame;
141 };
142 
143 struct tbnet_ring {
144 	struct tbnet_frame frames[TBNET_RING_SIZE];
145 	unsigned int cons;
146 	unsigned int prod;
147 	struct tb_ring *ring;
148 };
149 
150 /**
151  * struct tbnet - ThunderboltIP network driver private data
152  * @svc: XDomain service the driver is bound to
153  * @xd: XDomain the service belongs to
154  * @handler: ThunderboltIP configuration protocol handler
155  * @dev: Networking device
156  * @napi: NAPI structure for Rx polling
157  * @stats: Network statistics
158  * @skb: Network packet that is currently processed on Rx path
159  * @command_id: ID used for next configuration protocol packet
160  * @login_sent: ThunderboltIP login message successfully sent
161  * @login_received: ThunderboltIP login message received from the remote
162  *		    host
163  * @local_transmit_path: HopID we are using to send out packets
164  * @remote_transmit_path: HopID the other end is using to send packets to us
165  * @connection_lock: Lock serializing access to @login_sent,
166  *		     @login_received and @transmit_path.
167  * @login_retries: Number of login retries currently done
168  * @login_work: Worker to send ThunderboltIP login packets
169  * @connected_work: Worker that finalizes the ThunderboltIP connection
170  *		    setup and enables DMA paths for high speed data
171  *		    transfers
172  * @disconnect_work: Worker that handles tearing down the ThunderboltIP
173  *		     connection
174  * @rx_hdr: Copy of the currently processed Rx frame. Used when a
175  *	    network packet consists of multiple Thunderbolt frames.
176  *	    In host byte order.
177  * @rx_ring: Software ring holding Rx frames
178  * @frame_id: Frame ID use for next Tx packet
179  *            (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
180  * @tx_ring: Software ring holding Tx frames
181  */
182 struct tbnet {
183 	const struct tb_service *svc;
184 	struct tb_xdomain *xd;
185 	struct tb_protocol_handler handler;
186 	struct net_device *dev;
187 	struct napi_struct napi;
188 	struct tbnet_stats stats;
189 	struct sk_buff *skb;
190 	atomic_t command_id;
191 	bool login_sent;
192 	bool login_received;
193 	int local_transmit_path;
194 	int remote_transmit_path;
195 	struct mutex connection_lock;
196 	int login_retries;
197 	struct delayed_work login_work;
198 	struct work_struct connected_work;
199 	struct work_struct disconnect_work;
200 	struct thunderbolt_ip_frame_header rx_hdr;
201 	struct tbnet_ring rx_ring;
202 	atomic_t frame_id;
203 	struct tbnet_ring tx_ring;
204 };
205 
206 /* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
207 static const uuid_t tbnet_dir_uuid =
208 	UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
209 		  0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
210 
211 /* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
212 static const uuid_t tbnet_svc_uuid =
213 	UUID_INIT(0x798f589e, 0x3616, 0x8a47,
214 		  0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
215 
216 static struct tb_property_dir *tbnet_dir;
217 
218 static bool tbnet_e2e = true;
219 module_param_named(e2e, tbnet_e2e, bool, 0444);
220 MODULE_PARM_DESC(e2e, "USB4NET full end-to-end flow control (default: true)");
221 
222 static void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
223 	u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
224 	enum thunderbolt_ip_type type, size_t size, u32 command_id)
225 {
226 	u32 length_sn;
227 
228 	/* Length does not include route_hi/lo and length_sn fields */
229 	length_sn = (size - 3 * 4) / 4;
230 	length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
231 
232 	hdr->route_hi = upper_32_bits(route);
233 	hdr->route_lo = lower_32_bits(route);
234 	hdr->length_sn = length_sn;
235 	uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
236 	uuid_copy(&hdr->initiator_uuid, initiator_uuid);
237 	uuid_copy(&hdr->target_uuid, target_uuid);
238 	hdr->type = type;
239 	hdr->command_id = command_id;
240 }
241 
242 static int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
243 				u32 command_id)
244 {
245 	struct thunderbolt_ip_login_response reply;
246 	struct tb_xdomain *xd = net->xd;
247 
248 	memset(&reply, 0, sizeof(reply));
249 	tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
250 			  xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
251 			  command_id);
252 	memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
253 	reply.receiver_mac_len = ETH_ALEN;
254 
255 	return tb_xdomain_response(xd, &reply, sizeof(reply),
256 				   TB_CFG_PKG_XDOMAIN_RESP);
257 }
258 
259 static int tbnet_login_request(struct tbnet *net, u8 sequence)
260 {
261 	struct thunderbolt_ip_login_response reply;
262 	struct thunderbolt_ip_login request;
263 	struct tb_xdomain *xd = net->xd;
264 
265 	memset(&request, 0, sizeof(request));
266 	tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
267 			  xd->remote_uuid, TBIP_LOGIN, sizeof(request),
268 			  atomic_inc_return(&net->command_id));
269 
270 	request.proto_version = TBIP_LOGIN_PROTO_VERSION;
271 	request.transmit_path = net->local_transmit_path;
272 
273 	return tb_xdomain_request(xd, &request, sizeof(request),
274 				  TB_CFG_PKG_XDOMAIN_RESP, &reply,
275 				  sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
276 				  TBNET_LOGIN_TIMEOUT);
277 }
278 
279 static int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
280 				 u32 command_id)
281 {
282 	struct thunderbolt_ip_status reply;
283 	struct tb_xdomain *xd = net->xd;
284 
285 	memset(&reply, 0, sizeof(reply));
286 	tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
287 			  xd->remote_uuid, TBIP_STATUS, sizeof(reply),
288 			  atomic_inc_return(&net->command_id));
289 	return tb_xdomain_response(xd, &reply, sizeof(reply),
290 				   TB_CFG_PKG_XDOMAIN_RESP);
291 }
292 
293 static int tbnet_logout_request(struct tbnet *net)
294 {
295 	struct thunderbolt_ip_logout request;
296 	struct thunderbolt_ip_status reply;
297 	struct tb_xdomain *xd = net->xd;
298 
299 	memset(&request, 0, sizeof(request));
300 	tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
301 			  xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
302 			  atomic_inc_return(&net->command_id));
303 
304 	return tb_xdomain_request(xd, &request, sizeof(request),
305 				  TB_CFG_PKG_XDOMAIN_RESP, &reply,
306 				  sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
307 				  TBNET_LOGOUT_TIMEOUT);
308 }
309 
310 static void start_login(struct tbnet *net)
311 {
312 	netdev_dbg(net->dev, "login started\n");
313 
314 	mutex_lock(&net->connection_lock);
315 	net->login_sent = false;
316 	net->login_received = false;
317 	mutex_unlock(&net->connection_lock);
318 
319 	queue_delayed_work(system_long_wq, &net->login_work,
320 			   msecs_to_jiffies(1000));
321 }
322 
323 static void stop_login(struct tbnet *net)
324 {
325 	cancel_delayed_work_sync(&net->login_work);
326 	cancel_work_sync(&net->connected_work);
327 
328 	netdev_dbg(net->dev, "login stopped\n");
329 }
330 
331 static void tbnet_free_buffers(struct tbnet_ring *ring)
332 {
333 	unsigned int i;
334 
335 	for (i = 0; i < TBNET_RING_SIZE; i++) {
336 		struct device *dma_dev = tb_ring_dma_device(ring->ring);
337 		struct tbnet_frame *tf = &ring->frames[i];
338 		enum dma_data_direction dir;
339 		unsigned int order;
340 		size_t size;
341 
342 		if (!tf->page)
343 			continue;
344 
345 		if (ring->ring->is_tx) {
346 			dir = DMA_TO_DEVICE;
347 			order = 0;
348 			size = TBNET_FRAME_SIZE;
349 		} else {
350 			dir = DMA_FROM_DEVICE;
351 			order = TBNET_RX_PAGE_ORDER;
352 			size = TBNET_RX_PAGE_SIZE;
353 		}
354 
355 		trace_tbnet_free_frame(i, tf->page, tf->frame.buffer_phy, dir);
356 
357 		if (tf->frame.buffer_phy)
358 			dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
359 				       dir);
360 
361 		__free_pages(tf->page, order);
362 		tf->page = NULL;
363 	}
364 
365 	ring->cons = 0;
366 	ring->prod = 0;
367 }
368 
369 static void tbnet_tear_down(struct tbnet *net, bool send_logout)
370 {
371 	netif_carrier_off(net->dev);
372 	netif_stop_queue(net->dev);
373 
374 	stop_login(net);
375 
376 	mutex_lock(&net->connection_lock);
377 
378 	if (net->login_sent && net->login_received) {
379 		int ret, retries = TBNET_LOGOUT_RETRIES;
380 
381 		while (send_logout && retries-- > 0) {
382 			netdev_dbg(net->dev, "sending logout request %u\n",
383 				   retries);
384 			ret = tbnet_logout_request(net);
385 			if (ret != -ETIMEDOUT)
386 				break;
387 		}
388 
389 		tb_ring_stop(net->rx_ring.ring);
390 		tb_ring_stop(net->tx_ring.ring);
391 		tbnet_free_buffers(&net->rx_ring);
392 		tbnet_free_buffers(&net->tx_ring);
393 
394 		ret = tb_xdomain_disable_paths(net->xd,
395 					       net->local_transmit_path,
396 					       net->tx_ring.ring->hop,
397 					       net->remote_transmit_path,
398 					       net->rx_ring.ring->hop);
399 		if (ret)
400 			netdev_warn(net->dev, "failed to disable DMA paths\n");
401 
402 		tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
403 		net->remote_transmit_path = 0;
404 	}
405 
406 	net->login_retries = 0;
407 	net->login_sent = false;
408 	net->login_received = false;
409 
410 	netdev_dbg(net->dev, "network traffic stopped\n");
411 
412 	mutex_unlock(&net->connection_lock);
413 }
414 
415 static int tbnet_handle_packet(const void *buf, size_t size, void *data)
416 {
417 	const struct thunderbolt_ip_login *pkg = buf;
418 	struct tbnet *net = data;
419 	u32 command_id;
420 	int ret = 0;
421 	u32 sequence;
422 	u64 route;
423 
424 	/* Make sure the packet is for us */
425 	if (size < sizeof(struct thunderbolt_ip_header))
426 		return 0;
427 	if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
428 		return 0;
429 	if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
430 		return 0;
431 
432 	route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
433 	route &= ~BIT_ULL(63);
434 	if (route != net->xd->route)
435 		return 0;
436 
437 	sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
438 	sequence >>= TBIP_HDR_SN_SHIFT;
439 	command_id = pkg->hdr.command_id;
440 
441 	switch (pkg->hdr.type) {
442 	case TBIP_LOGIN:
443 		netdev_dbg(net->dev, "remote login request received\n");
444 		if (!netif_running(net->dev))
445 			break;
446 
447 		ret = tbnet_login_response(net, route, sequence,
448 					   pkg->hdr.command_id);
449 		if (!ret) {
450 			netdev_dbg(net->dev, "remote login response sent\n");
451 
452 			mutex_lock(&net->connection_lock);
453 			net->login_received = true;
454 			net->remote_transmit_path = pkg->transmit_path;
455 
456 			/* If we reached the number of max retries or
457 			 * previous logout, schedule another round of
458 			 * login retries
459 			 */
460 			if (net->login_retries >= TBNET_LOGIN_RETRIES ||
461 			    !net->login_sent) {
462 				net->login_retries = 0;
463 				queue_delayed_work(system_long_wq,
464 						   &net->login_work, 0);
465 			}
466 			mutex_unlock(&net->connection_lock);
467 
468 			queue_work(system_long_wq, &net->connected_work);
469 		}
470 		break;
471 
472 	case TBIP_LOGOUT:
473 		netdev_dbg(net->dev, "remote logout request received\n");
474 		ret = tbnet_logout_response(net, route, sequence, command_id);
475 		if (!ret) {
476 			netdev_dbg(net->dev, "remote logout response sent\n");
477 			queue_work(system_long_wq, &net->disconnect_work);
478 		}
479 		break;
480 
481 	default:
482 		return 0;
483 	}
484 
485 	if (ret)
486 		netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
487 
488 	return 1;
489 }
490 
491 static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
492 {
493 	return ring->prod - ring->cons;
494 }
495 
496 static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
497 {
498 	struct tbnet_ring *ring = &net->rx_ring;
499 	int ret;
500 
501 	while (nbuffers--) {
502 		struct device *dma_dev = tb_ring_dma_device(ring->ring);
503 		unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
504 		struct tbnet_frame *tf = &ring->frames[index];
505 		dma_addr_t dma_addr;
506 
507 		if (tf->page)
508 			break;
509 
510 		/* Allocate page (order > 0) so that it can hold maximum
511 		 * ThunderboltIP frame (4kB) and the additional room for
512 		 * SKB shared info required by build_skb().
513 		 */
514 		tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
515 		if (!tf->page) {
516 			ret = -ENOMEM;
517 			goto err_free;
518 		}
519 
520 		dma_addr = dma_map_page(dma_dev, tf->page, 0,
521 					TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
522 		if (dma_mapping_error(dma_dev, dma_addr)) {
523 			ret = -ENOMEM;
524 			goto err_free;
525 		}
526 
527 		tf->frame.buffer_phy = dma_addr;
528 		tf->dev = net->dev;
529 
530 		trace_tbnet_alloc_rx_frame(index, tf->page, dma_addr,
531 					   DMA_FROM_DEVICE);
532 
533 		tb_ring_rx(ring->ring, &tf->frame);
534 
535 		ring->prod++;
536 	}
537 
538 	return 0;
539 
540 err_free:
541 	tbnet_free_buffers(ring);
542 	return ret;
543 }
544 
545 static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
546 {
547 	struct tbnet_ring *ring = &net->tx_ring;
548 	struct device *dma_dev = tb_ring_dma_device(ring->ring);
549 	struct tbnet_frame *tf;
550 	unsigned int index;
551 
552 	if (!tbnet_available_buffers(ring))
553 		return NULL;
554 
555 	index = ring->cons++ & (TBNET_RING_SIZE - 1);
556 
557 	tf = &ring->frames[index];
558 	tf->frame.size = 0;
559 
560 	dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
561 				tb_ring_frame_size(&tf->frame), DMA_TO_DEVICE);
562 
563 	return tf;
564 }
565 
566 static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
567 			      bool canceled)
568 {
569 	struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
570 	struct tbnet *net = netdev_priv(tf->dev);
571 
572 	/* Return buffer to the ring */
573 	net->tx_ring.prod++;
574 
575 	if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
576 		netif_wake_queue(net->dev);
577 }
578 
579 static int tbnet_alloc_tx_buffers(struct tbnet *net)
580 {
581 	struct tbnet_ring *ring = &net->tx_ring;
582 	struct device *dma_dev = tb_ring_dma_device(ring->ring);
583 	unsigned int i;
584 
585 	for (i = 0; i < TBNET_RING_SIZE; i++) {
586 		struct tbnet_frame *tf = &ring->frames[i];
587 		dma_addr_t dma_addr;
588 
589 		tf->page = alloc_page(GFP_KERNEL);
590 		if (!tf->page) {
591 			tbnet_free_buffers(ring);
592 			return -ENOMEM;
593 		}
594 
595 		dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
596 					DMA_TO_DEVICE);
597 		if (dma_mapping_error(dma_dev, dma_addr)) {
598 			__free_page(tf->page);
599 			tf->page = NULL;
600 			tbnet_free_buffers(ring);
601 			return -ENOMEM;
602 		}
603 
604 		tf->dev = net->dev;
605 		tf->frame.buffer_phy = dma_addr;
606 		tf->frame.callback = tbnet_tx_callback;
607 		tf->frame.sof = TBIP_PDF_FRAME_START;
608 		tf->frame.eof = TBIP_PDF_FRAME_END;
609 
610 		trace_tbnet_alloc_tx_frame(i, tf->page, dma_addr, DMA_TO_DEVICE);
611 	}
612 
613 	ring->cons = 0;
614 	ring->prod = TBNET_RING_SIZE - 1;
615 
616 	return 0;
617 }
618 
619 static void tbnet_connected_work(struct work_struct *work)
620 {
621 	struct tbnet *net = container_of(work, typeof(*net), connected_work);
622 	bool connected;
623 	int ret;
624 
625 	if (netif_carrier_ok(net->dev))
626 		return;
627 
628 	mutex_lock(&net->connection_lock);
629 	connected = net->login_sent && net->login_received;
630 	mutex_unlock(&net->connection_lock);
631 
632 	if (!connected)
633 		return;
634 
635 	netdev_dbg(net->dev, "login successful, enabling paths\n");
636 
637 	ret = tb_xdomain_alloc_in_hopid(net->xd, net->remote_transmit_path);
638 	if (ret != net->remote_transmit_path) {
639 		netdev_err(net->dev, "failed to allocate Rx HopID\n");
640 		return;
641 	}
642 
643 	/* Both logins successful so enable the rings, high-speed DMA
644 	 * paths and start the network device queue.
645 	 *
646 	 * Note we enable the DMA paths last to make sure we have primed
647 	 * the Rx ring before any incoming packets are allowed to
648 	 * arrive.
649 	 */
650 	tb_ring_start(net->tx_ring.ring);
651 	tb_ring_start(net->rx_ring.ring);
652 
653 	ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
654 	if (ret)
655 		goto err_stop_rings;
656 
657 	ret = tbnet_alloc_tx_buffers(net);
658 	if (ret)
659 		goto err_free_rx_buffers;
660 
661 	ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path,
662 				      net->tx_ring.ring->hop,
663 				      net->remote_transmit_path,
664 				      net->rx_ring.ring->hop);
665 	if (ret) {
666 		netdev_err(net->dev, "failed to enable DMA paths\n");
667 		goto err_free_tx_buffers;
668 	}
669 
670 	netif_carrier_on(net->dev);
671 	netif_start_queue(net->dev);
672 
673 	netdev_dbg(net->dev, "network traffic started\n");
674 	return;
675 
676 err_free_tx_buffers:
677 	tbnet_free_buffers(&net->tx_ring);
678 err_free_rx_buffers:
679 	tbnet_free_buffers(&net->rx_ring);
680 err_stop_rings:
681 	tb_ring_stop(net->rx_ring.ring);
682 	tb_ring_stop(net->tx_ring.ring);
683 	tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path);
684 }
685 
686 static void tbnet_login_work(struct work_struct *work)
687 {
688 	struct tbnet *net = container_of(work, typeof(*net), login_work.work);
689 	unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
690 	int ret;
691 
692 	if (netif_carrier_ok(net->dev))
693 		return;
694 
695 	netdev_dbg(net->dev, "sending login request, retries=%u\n",
696 		   net->login_retries);
697 
698 	ret = tbnet_login_request(net, net->login_retries % 4);
699 	if (ret) {
700 		netdev_dbg(net->dev, "sending login request failed, ret=%d\n",
701 			   ret);
702 		if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
703 			queue_delayed_work(system_long_wq, &net->login_work,
704 					   delay);
705 		} else {
706 			netdev_info(net->dev, "ThunderboltIP login timed out\n");
707 		}
708 	} else {
709 		netdev_dbg(net->dev, "received login reply\n");
710 
711 		net->login_retries = 0;
712 
713 		mutex_lock(&net->connection_lock);
714 		net->login_sent = true;
715 		mutex_unlock(&net->connection_lock);
716 
717 		queue_work(system_long_wq, &net->connected_work);
718 	}
719 }
720 
721 static void tbnet_disconnect_work(struct work_struct *work)
722 {
723 	struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
724 
725 	tbnet_tear_down(net, false);
726 }
727 
728 static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
729 			      const struct thunderbolt_ip_frame_header *hdr)
730 {
731 	u32 frame_id, frame_count, frame_size, frame_index;
732 	unsigned int size;
733 
734 	if (tf->frame.flags & RING_DESC_CRC_ERROR) {
735 		net->stats.rx_crc_errors++;
736 		return false;
737 	} else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
738 		net->stats.rx_over_errors++;
739 		return false;
740 	}
741 
742 	/* Should be greater than just header i.e. contains data */
743 	size = tb_ring_frame_size(&tf->frame);
744 	if (size <= sizeof(*hdr)) {
745 		net->stats.rx_length_errors++;
746 		return false;
747 	}
748 
749 	frame_count = le32_to_cpu(hdr->frame_count);
750 	frame_size = le32_to_cpu(hdr->frame_size);
751 	frame_index = le16_to_cpu(hdr->frame_index);
752 	frame_id = le16_to_cpu(hdr->frame_id);
753 
754 	if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
755 		net->stats.rx_length_errors++;
756 		return false;
757 	}
758 
759 	/* In case we're in the middle of packet, validate the frame
760 	 * header based on first fragment of the packet.
761 	 */
762 	if (net->skb && net->rx_hdr.frame_count) {
763 		/* Check the frame count fits the count field */
764 		if (frame_count != le32_to_cpu(net->rx_hdr.frame_count)) {
765 			net->stats.rx_length_errors++;
766 			return false;
767 		}
768 
769 		/* Check the frame identifiers are incremented correctly,
770 		 * and id is matching.
771 		 */
772 		if (frame_index != le16_to_cpu(net->rx_hdr.frame_index) + 1 ||
773 		    frame_id != le16_to_cpu(net->rx_hdr.frame_id)) {
774 			net->stats.rx_missed_errors++;
775 			return false;
776 		}
777 
778 		if (net->skb->len + frame_size > TBNET_MAX_MTU) {
779 			net->stats.rx_length_errors++;
780 			return false;
781 		}
782 
783 		return true;
784 	}
785 
786 	/* Start of packet, validate the frame header. tbnet_poll() puts the
787 	 * first frame in the skb linear area and every further frame in a page
788 	 * fragment, so a packet may not span more than MAX_SKB_FRAGS + 1 frames
789 	 * without overflowing skb_shinfo()->frags[].
790 	 */
791 	if (frame_count == 0 || frame_count > MAX_SKB_FRAGS + 1) {
792 		net->stats.rx_length_errors++;
793 		return false;
794 	}
795 	if (frame_index != 0) {
796 		net->stats.rx_missed_errors++;
797 		return false;
798 	}
799 
800 	return true;
801 }
802 
803 static int tbnet_poll(struct napi_struct *napi, int budget)
804 {
805 	struct tbnet *net = container_of(napi, struct tbnet, napi);
806 	unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
807 	struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
808 	unsigned int rx_packets = 0;
809 
810 	while (rx_packets < budget) {
811 		const struct thunderbolt_ip_frame_header *hdr;
812 		unsigned int hdr_size = sizeof(*hdr);
813 		struct sk_buff *skb = NULL;
814 		struct ring_frame *frame;
815 		struct tbnet_frame *tf;
816 		struct page *page;
817 		bool last = true;
818 		u32 frame_size;
819 
820 		/* Return some buffers to hardware, one at a time is too
821 		 * slow so allocate MAX_SKB_FRAGS buffers at the same
822 		 * time.
823 		 */
824 		if (cleaned_count >= MAX_SKB_FRAGS) {
825 			tbnet_alloc_rx_buffers(net, cleaned_count);
826 			cleaned_count = 0;
827 		}
828 
829 		frame = tb_ring_poll(net->rx_ring.ring);
830 		if (!frame)
831 			break;
832 
833 		dma_unmap_page(dma_dev, frame->buffer_phy,
834 			       TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
835 
836 		tf = container_of(frame, typeof(*tf), frame);
837 
838 		page = tf->page;
839 		tf->page = NULL;
840 		net->rx_ring.cons++;
841 		cleaned_count++;
842 
843 		hdr = page_address(page);
844 		if (!tbnet_check_frame(net, tf, hdr)) {
845 			trace_tbnet_invalid_rx_ip_frame(hdr->frame_size,
846 				hdr->frame_id, hdr->frame_index, hdr->frame_count);
847 			__free_pages(page, TBNET_RX_PAGE_ORDER);
848 			dev_kfree_skb_any(net->skb);
849 			net->skb = NULL;
850 			continue;
851 		}
852 
853 		trace_tbnet_rx_ip_frame(hdr->frame_size, hdr->frame_id,
854 					hdr->frame_index, hdr->frame_count);
855 		frame_size = le32_to_cpu(hdr->frame_size);
856 
857 		skb = net->skb;
858 		if (!skb) {
859 			skb = build_skb(page_address(page),
860 					TBNET_RX_PAGE_SIZE);
861 			if (!skb) {
862 				__free_pages(page, TBNET_RX_PAGE_ORDER);
863 				net->stats.rx_errors++;
864 				break;
865 			}
866 
867 			skb_reserve(skb, hdr_size);
868 			skb_put(skb, frame_size);
869 
870 			net->skb = skb;
871 		} else {
872 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
873 					page, hdr_size, frame_size,
874 					TBNET_RX_PAGE_SIZE - hdr_size);
875 		}
876 
877 		net->rx_hdr.frame_size = hdr->frame_size;
878 		net->rx_hdr.frame_count = hdr->frame_count;
879 		net->rx_hdr.frame_index = hdr->frame_index;
880 		net->rx_hdr.frame_id = hdr->frame_id;
881 		last = le16_to_cpu(net->rx_hdr.frame_index) ==
882 		       le32_to_cpu(net->rx_hdr.frame_count) - 1;
883 
884 		rx_packets++;
885 		net->stats.rx_bytes += frame_size;
886 
887 		if (last) {
888 			skb->protocol = eth_type_trans(skb, net->dev);
889 			trace_tbnet_rx_skb(skb);
890 			napi_gro_receive(&net->napi, skb);
891 			net->skb = NULL;
892 		}
893 	}
894 
895 	net->stats.rx_packets += rx_packets;
896 
897 	if (cleaned_count)
898 		tbnet_alloc_rx_buffers(net, cleaned_count);
899 
900 	if (rx_packets >= budget)
901 		return budget;
902 
903 	napi_complete_done(napi, rx_packets);
904 	/* Re-enable the ring interrupt */
905 	tb_ring_poll_complete(net->rx_ring.ring);
906 
907 	return rx_packets;
908 }
909 
910 static void tbnet_start_poll(void *data)
911 {
912 	struct tbnet *net = data;
913 
914 	napi_schedule(&net->napi);
915 }
916 
917 static int tbnet_open(struct net_device *dev)
918 {
919 	struct tbnet *net = netdev_priv(dev);
920 	struct tb_xdomain *xd = net->xd;
921 	u16 sof_mask, eof_mask;
922 	struct tb_ring *ring;
923 	unsigned int flags;
924 	int hopid;
925 
926 	netif_carrier_off(dev);
927 
928 	flags = RING_FLAG_FRAME;
929 	/* Only enable full E2E if the other end supports it too */
930 	if (tbnet_e2e && net->svc->prtcstns & TBNET_E2E)
931 		flags |= RING_FLAG_E2E;
932 
933 	ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE, flags);
934 	if (!ring) {
935 		netdev_err(dev, "failed to allocate Tx ring\n");
936 		return -ENOMEM;
937 	}
938 	net->tx_ring.ring = ring;
939 
940 	hopid = tb_xdomain_alloc_out_hopid(xd, -1);
941 	if (hopid < 0) {
942 		netdev_err(dev, "failed to allocate Tx HopID\n");
943 		tb_ring_free(net->tx_ring.ring);
944 		net->tx_ring.ring = NULL;
945 		return hopid;
946 	}
947 	net->local_transmit_path = hopid;
948 
949 	sof_mask = BIT(TBIP_PDF_FRAME_START);
950 	eof_mask = BIT(TBIP_PDF_FRAME_END);
951 
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);
955 	if (!ring) {
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;
960 		return -ENOMEM;
961 	}
962 	net->rx_ring.ring = ring;
963 
964 	tb_ring_throttling(net->tx_ring.ring, TBNET_THROTTLING);
965 	tb_ring_throttling(net->rx_ring.ring, TBNET_THROTTLING);
966 
967 	napi_enable(&net->napi);
968 	start_login(net);
969 
970 	return 0;
971 }
972 
973 static int tbnet_stop(struct net_device *dev)
974 {
975 	struct tbnet *net = netdev_priv(dev);
976 
977 	napi_disable(&net->napi);
978 
979 	cancel_work_sync(&net->disconnect_work);
980 	tbnet_tear_down(net, true);
981 
982 	tb_ring_free(net->rx_ring.ring);
983 	net->rx_ring.ring = NULL;
984 
985 	tb_xdomain_release_out_hopid(net->xd, net->local_transmit_path);
986 	tb_ring_free(net->tx_ring.ring);
987 	net->tx_ring.ring = NULL;
988 
989 	return 0;
990 }
991 
992 static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
993 	struct tbnet_frame **frames, u32 frame_count)
994 {
995 	struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
996 	struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
997 	unsigned int i, len, offset = skb_transport_offset(skb);
998 	/* Remove payload length from checksum */
999 	u32 paylen = skb->len - skb_transport_offset(skb);
1000 	__wsum wsum = (__force __wsum)htonl(paylen);
1001 	__be16 protocol = skb->protocol;
1002 	void *data = skb->data;
1003 	void *dest = hdr + 1;
1004 	__sum16 *tucso;
1005 
1006 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
1007 		/* No need to calculate checksum so we just update the
1008 		 * total frame count and sync the frames for DMA.
1009 		 */
1010 		for (i = 0; i < frame_count; i++) {
1011 			hdr = page_address(frames[i]->page);
1012 			hdr->frame_count = cpu_to_le32(frame_count);
1013 			trace_tbnet_tx_ip_frame(hdr->frame_size, hdr->frame_id,
1014 						hdr->frame_index, hdr->frame_count);
1015 			dma_sync_single_for_device(dma_dev,
1016 				frames[i]->frame.buffer_phy,
1017 				tb_ring_frame_size(&frames[i]->frame),
1018 						   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 			tb_ring_frame_size(&frames[i]->frame), 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