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