Linux Audio

Check our new training course

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