18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Networking over Thunderbolt cable using Apple ThunderboltIP protocol
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017, Intel Corporation
68c2ecf20Sopenharmony_ci * Authors: Amir Levy <amir.jer.levy@intel.com>
78c2ecf20Sopenharmony_ci *          Michael Jamet <michael.jamet@intel.com>
88c2ecf20Sopenharmony_ci *          Mika Westerberg <mika.westerberg@linux.intel.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/atomic.h>
128c2ecf20Sopenharmony_ci#include <linux/highmem.h>
138c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
148c2ecf20Sopenharmony_ci#include <linux/jhash.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
178c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
188c2ecf20Sopenharmony_ci#include <linux/sizes.h>
198c2ecf20Sopenharmony_ci#include <linux/thunderbolt.h>
208c2ecf20Sopenharmony_ci#include <linux/uuid.h>
218c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <net/ip6_checksum.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Protocol timeouts in ms */
268c2ecf20Sopenharmony_ci#define TBNET_LOGIN_DELAY	4500
278c2ecf20Sopenharmony_ci#define TBNET_LOGIN_TIMEOUT	500
288c2ecf20Sopenharmony_ci#define TBNET_LOGOUT_TIMEOUT	100
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define TBNET_RING_SIZE		256
318c2ecf20Sopenharmony_ci#define TBNET_LOCAL_PATH	0xf
328c2ecf20Sopenharmony_ci#define TBNET_LOGIN_RETRIES	60
338c2ecf20Sopenharmony_ci#define TBNET_LOGOUT_RETRIES	5
348c2ecf20Sopenharmony_ci#define TBNET_MATCH_FRAGS_ID	BIT(1)
358c2ecf20Sopenharmony_ci#define TBNET_MAX_MTU		SZ_64K
368c2ecf20Sopenharmony_ci#define TBNET_FRAME_SIZE	SZ_4K
378c2ecf20Sopenharmony_ci#define TBNET_MAX_PAYLOAD_SIZE	\
388c2ecf20Sopenharmony_ci	(TBNET_FRAME_SIZE - sizeof(struct thunderbolt_ip_frame_header))
398c2ecf20Sopenharmony_ci/* Rx packets need to hold space for skb_shared_info */
408c2ecf20Sopenharmony_ci#define TBNET_RX_MAX_SIZE	\
418c2ecf20Sopenharmony_ci	(TBNET_FRAME_SIZE + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
428c2ecf20Sopenharmony_ci#define TBNET_RX_PAGE_ORDER	get_order(TBNET_RX_MAX_SIZE)
438c2ecf20Sopenharmony_ci#define TBNET_RX_PAGE_SIZE	(PAGE_SIZE << TBNET_RX_PAGE_ORDER)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define TBNET_L0_PORT_NUM(route) ((route) & GENMASK(5, 0))
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/**
488c2ecf20Sopenharmony_ci * struct thunderbolt_ip_frame_header - Header for each Thunderbolt frame
498c2ecf20Sopenharmony_ci * @frame_size: size of the data with the frame
508c2ecf20Sopenharmony_ci * @frame_index: running index on the frames
518c2ecf20Sopenharmony_ci * @frame_id: ID of the frame to match frames to specific packet
528c2ecf20Sopenharmony_ci * @frame_count: how many frames assembles a full packet
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Each data frame passed to the high-speed DMA ring has this header. If
558c2ecf20Sopenharmony_ci * the XDomain network directory announces that %TBNET_MATCH_FRAGS_ID is
568c2ecf20Sopenharmony_ci * supported then @frame_id is filled, otherwise it stays %0.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_cistruct thunderbolt_ip_frame_header {
598c2ecf20Sopenharmony_ci	u32 frame_size;
608c2ecf20Sopenharmony_ci	u16 frame_index;
618c2ecf20Sopenharmony_ci	u16 frame_id;
628c2ecf20Sopenharmony_ci	u32 frame_count;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cienum thunderbolt_ip_frame_pdf {
668c2ecf20Sopenharmony_ci	TBIP_PDF_FRAME_START = 1,
678c2ecf20Sopenharmony_ci	TBIP_PDF_FRAME_END,
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cienum thunderbolt_ip_type {
718c2ecf20Sopenharmony_ci	TBIP_LOGIN,
728c2ecf20Sopenharmony_ci	TBIP_LOGIN_RESPONSE,
738c2ecf20Sopenharmony_ci	TBIP_LOGOUT,
748c2ecf20Sopenharmony_ci	TBIP_STATUS,
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct thunderbolt_ip_header {
788c2ecf20Sopenharmony_ci	u32 route_hi;
798c2ecf20Sopenharmony_ci	u32 route_lo;
808c2ecf20Sopenharmony_ci	u32 length_sn;
818c2ecf20Sopenharmony_ci	uuid_t uuid;
828c2ecf20Sopenharmony_ci	uuid_t initiator_uuid;
838c2ecf20Sopenharmony_ci	uuid_t target_uuid;
848c2ecf20Sopenharmony_ci	u32 type;
858c2ecf20Sopenharmony_ci	u32 command_id;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define TBIP_HDR_LENGTH_MASK		GENMASK(5, 0)
898c2ecf20Sopenharmony_ci#define TBIP_HDR_SN_MASK		GENMASK(28, 27)
908c2ecf20Sopenharmony_ci#define TBIP_HDR_SN_SHIFT		27
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct thunderbolt_ip_login {
938c2ecf20Sopenharmony_ci	struct thunderbolt_ip_header hdr;
948c2ecf20Sopenharmony_ci	u32 proto_version;
958c2ecf20Sopenharmony_ci	u32 transmit_path;
968c2ecf20Sopenharmony_ci	u32 reserved[4];
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define TBIP_LOGIN_PROTO_VERSION	1
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistruct thunderbolt_ip_login_response {
1028c2ecf20Sopenharmony_ci	struct thunderbolt_ip_header hdr;
1038c2ecf20Sopenharmony_ci	u32 status;
1048c2ecf20Sopenharmony_ci	u32 receiver_mac[2];
1058c2ecf20Sopenharmony_ci	u32 receiver_mac_len;
1068c2ecf20Sopenharmony_ci	u32 reserved[4];
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistruct thunderbolt_ip_logout {
1108c2ecf20Sopenharmony_ci	struct thunderbolt_ip_header hdr;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistruct thunderbolt_ip_status {
1148c2ecf20Sopenharmony_ci	struct thunderbolt_ip_header hdr;
1158c2ecf20Sopenharmony_ci	u32 status;
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistruct tbnet_stats {
1198c2ecf20Sopenharmony_ci	u64 tx_packets;
1208c2ecf20Sopenharmony_ci	u64 rx_packets;
1218c2ecf20Sopenharmony_ci	u64 tx_bytes;
1228c2ecf20Sopenharmony_ci	u64 rx_bytes;
1238c2ecf20Sopenharmony_ci	u64 rx_errors;
1248c2ecf20Sopenharmony_ci	u64 tx_errors;
1258c2ecf20Sopenharmony_ci	u64 rx_length_errors;
1268c2ecf20Sopenharmony_ci	u64 rx_over_errors;
1278c2ecf20Sopenharmony_ci	u64 rx_crc_errors;
1288c2ecf20Sopenharmony_ci	u64 rx_missed_errors;
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistruct tbnet_frame {
1328c2ecf20Sopenharmony_ci	struct net_device *dev;
1338c2ecf20Sopenharmony_ci	struct page *page;
1348c2ecf20Sopenharmony_ci	struct ring_frame frame;
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistruct tbnet_ring {
1388c2ecf20Sopenharmony_ci	struct tbnet_frame frames[TBNET_RING_SIZE];
1398c2ecf20Sopenharmony_ci	unsigned int cons;
1408c2ecf20Sopenharmony_ci	unsigned int prod;
1418c2ecf20Sopenharmony_ci	struct tb_ring *ring;
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci * struct tbnet - ThunderboltIP network driver private data
1468c2ecf20Sopenharmony_ci * @svc: XDomain service the driver is bound to
1478c2ecf20Sopenharmony_ci * @xd: XDomain the service blongs to
1488c2ecf20Sopenharmony_ci * @handler: ThunderboltIP configuration protocol handler
1498c2ecf20Sopenharmony_ci * @dev: Networking device
1508c2ecf20Sopenharmony_ci * @napi: NAPI structure for Rx polling
1518c2ecf20Sopenharmony_ci * @stats: Network statistics
1528c2ecf20Sopenharmony_ci * @skb: Network packet that is currently processed on Rx path
1538c2ecf20Sopenharmony_ci * @command_id: ID used for next configuration protocol packet
1548c2ecf20Sopenharmony_ci * @login_sent: ThunderboltIP login message successfully sent
1558c2ecf20Sopenharmony_ci * @login_received: ThunderboltIP login message received from the remote
1568c2ecf20Sopenharmony_ci *		    host
1578c2ecf20Sopenharmony_ci * @transmit_path: HopID the other end needs to use building the
1588c2ecf20Sopenharmony_ci *		   opposite side path.
1598c2ecf20Sopenharmony_ci * @connection_lock: Lock serializing access to @login_sent,
1608c2ecf20Sopenharmony_ci *		     @login_received and @transmit_path.
1618c2ecf20Sopenharmony_ci * @login_retries: Number of login retries currently done
1628c2ecf20Sopenharmony_ci * @login_work: Worker to send ThunderboltIP login packets
1638c2ecf20Sopenharmony_ci * @connected_work: Worker that finalizes the ThunderboltIP connection
1648c2ecf20Sopenharmony_ci *		    setup and enables DMA paths for high speed data
1658c2ecf20Sopenharmony_ci *		    transfers
1668c2ecf20Sopenharmony_ci * @disconnect_work: Worker that handles tearing down the ThunderboltIP
1678c2ecf20Sopenharmony_ci *		     connection
1688c2ecf20Sopenharmony_ci * @rx_hdr: Copy of the currently processed Rx frame. Used when a
1698c2ecf20Sopenharmony_ci *	    network packet consists of multiple Thunderbolt frames.
1708c2ecf20Sopenharmony_ci *	    In host byte order.
1718c2ecf20Sopenharmony_ci * @rx_ring: Software ring holding Rx frames
1728c2ecf20Sopenharmony_ci * @frame_id: Frame ID use for next Tx packet
1738c2ecf20Sopenharmony_ci *            (if %TBNET_MATCH_FRAGS_ID is supported in both ends)
1748c2ecf20Sopenharmony_ci * @tx_ring: Software ring holding Tx frames
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistruct tbnet {
1778c2ecf20Sopenharmony_ci	const struct tb_service *svc;
1788c2ecf20Sopenharmony_ci	struct tb_xdomain *xd;
1798c2ecf20Sopenharmony_ci	struct tb_protocol_handler handler;
1808c2ecf20Sopenharmony_ci	struct net_device *dev;
1818c2ecf20Sopenharmony_ci	struct napi_struct napi;
1828c2ecf20Sopenharmony_ci	struct tbnet_stats stats;
1838c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1848c2ecf20Sopenharmony_ci	atomic_t command_id;
1858c2ecf20Sopenharmony_ci	bool login_sent;
1868c2ecf20Sopenharmony_ci	bool login_received;
1878c2ecf20Sopenharmony_ci	u32 transmit_path;
1888c2ecf20Sopenharmony_ci	struct mutex connection_lock;
1898c2ecf20Sopenharmony_ci	int login_retries;
1908c2ecf20Sopenharmony_ci	struct delayed_work login_work;
1918c2ecf20Sopenharmony_ci	struct work_struct connected_work;
1928c2ecf20Sopenharmony_ci	struct work_struct disconnect_work;
1938c2ecf20Sopenharmony_ci	struct thunderbolt_ip_frame_header rx_hdr;
1948c2ecf20Sopenharmony_ci	struct tbnet_ring rx_ring;
1958c2ecf20Sopenharmony_ci	atomic_t frame_id;
1968c2ecf20Sopenharmony_ci	struct tbnet_ring tx_ring;
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* Network property directory UUID: c66189ca-1cce-4195-bdb8-49592e5f5a4f */
2008c2ecf20Sopenharmony_cistatic const uuid_t tbnet_dir_uuid =
2018c2ecf20Sopenharmony_ci	UUID_INIT(0xc66189ca, 0x1cce, 0x4195,
2028c2ecf20Sopenharmony_ci		  0xbd, 0xb8, 0x49, 0x59, 0x2e, 0x5f, 0x5a, 0x4f);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* ThunderboltIP protocol UUID: 798f589e-3616-8a47-97c6-5664a920c8dd */
2058c2ecf20Sopenharmony_cistatic const uuid_t tbnet_svc_uuid =
2068c2ecf20Sopenharmony_ci	UUID_INIT(0x798f589e, 0x3616, 0x8a47,
2078c2ecf20Sopenharmony_ci		  0x97, 0xc6, 0x56, 0x64, 0xa9, 0x20, 0xc8, 0xdd);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic struct tb_property_dir *tbnet_dir;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void tbnet_fill_header(struct thunderbolt_ip_header *hdr, u64 route,
2128c2ecf20Sopenharmony_ci	u8 sequence, const uuid_t *initiator_uuid, const uuid_t *target_uuid,
2138c2ecf20Sopenharmony_ci	enum thunderbolt_ip_type type, size_t size, u32 command_id)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	u32 length_sn;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Length does not include route_hi/lo and length_sn fields */
2188c2ecf20Sopenharmony_ci	length_sn = (size - 3 * 4) / 4;
2198c2ecf20Sopenharmony_ci	length_sn |= (sequence << TBIP_HDR_SN_SHIFT) & TBIP_HDR_SN_MASK;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	hdr->route_hi = upper_32_bits(route);
2228c2ecf20Sopenharmony_ci	hdr->route_lo = lower_32_bits(route);
2238c2ecf20Sopenharmony_ci	hdr->length_sn = length_sn;
2248c2ecf20Sopenharmony_ci	uuid_copy(&hdr->uuid, &tbnet_svc_uuid);
2258c2ecf20Sopenharmony_ci	uuid_copy(&hdr->initiator_uuid, initiator_uuid);
2268c2ecf20Sopenharmony_ci	uuid_copy(&hdr->target_uuid, target_uuid);
2278c2ecf20Sopenharmony_ci	hdr->type = type;
2288c2ecf20Sopenharmony_ci	hdr->command_id = command_id;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic int tbnet_login_response(struct tbnet *net, u64 route, u8 sequence,
2328c2ecf20Sopenharmony_ci				u32 command_id)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	struct thunderbolt_ip_login_response reply;
2358c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = net->xd;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	memset(&reply, 0, sizeof(reply));
2388c2ecf20Sopenharmony_ci	tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
2398c2ecf20Sopenharmony_ci			  xd->remote_uuid, TBIP_LOGIN_RESPONSE, sizeof(reply),
2408c2ecf20Sopenharmony_ci			  command_id);
2418c2ecf20Sopenharmony_ci	memcpy(reply.receiver_mac, net->dev->dev_addr, ETH_ALEN);
2428c2ecf20Sopenharmony_ci	reply.receiver_mac_len = ETH_ALEN;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	return tb_xdomain_response(xd, &reply, sizeof(reply),
2458c2ecf20Sopenharmony_ci				   TB_CFG_PKG_XDOMAIN_RESP);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic int tbnet_login_request(struct tbnet *net, u8 sequence)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	struct thunderbolt_ip_login_response reply;
2518c2ecf20Sopenharmony_ci	struct thunderbolt_ip_login request;
2528c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = net->xd;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	memset(&request, 0, sizeof(request));
2558c2ecf20Sopenharmony_ci	tbnet_fill_header(&request.hdr, xd->route, sequence, xd->local_uuid,
2568c2ecf20Sopenharmony_ci			  xd->remote_uuid, TBIP_LOGIN, sizeof(request),
2578c2ecf20Sopenharmony_ci			  atomic_inc_return(&net->command_id));
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	request.proto_version = TBIP_LOGIN_PROTO_VERSION;
2608c2ecf20Sopenharmony_ci	request.transmit_path = TBNET_LOCAL_PATH;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	return tb_xdomain_request(xd, &request, sizeof(request),
2638c2ecf20Sopenharmony_ci				  TB_CFG_PKG_XDOMAIN_RESP, &reply,
2648c2ecf20Sopenharmony_ci				  sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
2658c2ecf20Sopenharmony_ci				  TBNET_LOGIN_TIMEOUT);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic int tbnet_logout_response(struct tbnet *net, u64 route, u8 sequence,
2698c2ecf20Sopenharmony_ci				 u32 command_id)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct thunderbolt_ip_status reply;
2728c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = net->xd;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	memset(&reply, 0, sizeof(reply));
2758c2ecf20Sopenharmony_ci	tbnet_fill_header(&reply.hdr, route, sequence, xd->local_uuid,
2768c2ecf20Sopenharmony_ci			  xd->remote_uuid, TBIP_STATUS, sizeof(reply),
2778c2ecf20Sopenharmony_ci			  atomic_inc_return(&net->command_id));
2788c2ecf20Sopenharmony_ci	return tb_xdomain_response(xd, &reply, sizeof(reply),
2798c2ecf20Sopenharmony_ci				   TB_CFG_PKG_XDOMAIN_RESP);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int tbnet_logout_request(struct tbnet *net)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct thunderbolt_ip_logout request;
2858c2ecf20Sopenharmony_ci	struct thunderbolt_ip_status reply;
2868c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = net->xd;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	memset(&request, 0, sizeof(request));
2898c2ecf20Sopenharmony_ci	tbnet_fill_header(&request.hdr, xd->route, 0, xd->local_uuid,
2908c2ecf20Sopenharmony_ci			  xd->remote_uuid, TBIP_LOGOUT, sizeof(request),
2918c2ecf20Sopenharmony_ci			  atomic_inc_return(&net->command_id));
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return tb_xdomain_request(xd, &request, sizeof(request),
2948c2ecf20Sopenharmony_ci				  TB_CFG_PKG_XDOMAIN_RESP, &reply,
2958c2ecf20Sopenharmony_ci				  sizeof(reply), TB_CFG_PKG_XDOMAIN_RESP,
2968c2ecf20Sopenharmony_ci				  TBNET_LOGOUT_TIMEOUT);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic void start_login(struct tbnet *net)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	mutex_lock(&net->connection_lock);
3028c2ecf20Sopenharmony_ci	net->login_sent = false;
3038c2ecf20Sopenharmony_ci	net->login_received = false;
3048c2ecf20Sopenharmony_ci	mutex_unlock(&net->connection_lock);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	queue_delayed_work(system_long_wq, &net->login_work,
3078c2ecf20Sopenharmony_ci			   msecs_to_jiffies(1000));
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic void stop_login(struct tbnet *net)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&net->login_work);
3138c2ecf20Sopenharmony_ci	cancel_work_sync(&net->connected_work);
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic inline unsigned int tbnet_frame_size(const struct tbnet_frame *tf)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	return tf->frame.size ? : TBNET_FRAME_SIZE;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic void tbnet_free_buffers(struct tbnet_ring *ring)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	unsigned int i;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	for (i = 0; i < TBNET_RING_SIZE; i++) {
3268c2ecf20Sopenharmony_ci		struct device *dma_dev = tb_ring_dma_device(ring->ring);
3278c2ecf20Sopenharmony_ci		struct tbnet_frame *tf = &ring->frames[i];
3288c2ecf20Sopenharmony_ci		enum dma_data_direction dir;
3298c2ecf20Sopenharmony_ci		unsigned int order;
3308c2ecf20Sopenharmony_ci		size_t size;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci		if (!tf->page)
3338c2ecf20Sopenharmony_ci			continue;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		if (ring->ring->is_tx) {
3368c2ecf20Sopenharmony_ci			dir = DMA_TO_DEVICE;
3378c2ecf20Sopenharmony_ci			order = 0;
3388c2ecf20Sopenharmony_ci			size = TBNET_FRAME_SIZE;
3398c2ecf20Sopenharmony_ci		} else {
3408c2ecf20Sopenharmony_ci			dir = DMA_FROM_DEVICE;
3418c2ecf20Sopenharmony_ci			order = TBNET_RX_PAGE_ORDER;
3428c2ecf20Sopenharmony_ci			size = TBNET_RX_PAGE_SIZE;
3438c2ecf20Sopenharmony_ci		}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci		if (tf->frame.buffer_phy)
3468c2ecf20Sopenharmony_ci			dma_unmap_page(dma_dev, tf->frame.buffer_phy, size,
3478c2ecf20Sopenharmony_ci				       dir);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		__free_pages(tf->page, order);
3508c2ecf20Sopenharmony_ci		tf->page = NULL;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	ring->cons = 0;
3548c2ecf20Sopenharmony_ci	ring->prod = 0;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic void tbnet_tear_down(struct tbnet *net, bool send_logout)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	netif_carrier_off(net->dev);
3608c2ecf20Sopenharmony_ci	netif_stop_queue(net->dev);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	stop_login(net);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	mutex_lock(&net->connection_lock);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (net->login_sent && net->login_received) {
3678c2ecf20Sopenharmony_ci		int retries = TBNET_LOGOUT_RETRIES;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci		while (send_logout && retries-- > 0) {
3708c2ecf20Sopenharmony_ci			int ret = tbnet_logout_request(net);
3718c2ecf20Sopenharmony_ci			if (ret != -ETIMEDOUT)
3728c2ecf20Sopenharmony_ci				break;
3738c2ecf20Sopenharmony_ci		}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		tb_ring_stop(net->rx_ring.ring);
3768c2ecf20Sopenharmony_ci		tb_ring_stop(net->tx_ring.ring);
3778c2ecf20Sopenharmony_ci		tbnet_free_buffers(&net->rx_ring);
3788c2ecf20Sopenharmony_ci		tbnet_free_buffers(&net->tx_ring);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci		if (tb_xdomain_disable_paths(net->xd))
3818c2ecf20Sopenharmony_ci			netdev_warn(net->dev, "failed to disable DMA paths\n");
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	net->login_retries = 0;
3858c2ecf20Sopenharmony_ci	net->login_sent = false;
3868c2ecf20Sopenharmony_ci	net->login_received = false;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	mutex_unlock(&net->connection_lock);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int tbnet_handle_packet(const void *buf, size_t size, void *data)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	const struct thunderbolt_ip_login *pkg = buf;
3948c2ecf20Sopenharmony_ci	struct tbnet *net = data;
3958c2ecf20Sopenharmony_ci	u32 command_id;
3968c2ecf20Sopenharmony_ci	int ret = 0;
3978c2ecf20Sopenharmony_ci	u32 sequence;
3988c2ecf20Sopenharmony_ci	u64 route;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* Make sure the packet is for us */
4018c2ecf20Sopenharmony_ci	if (size < sizeof(struct thunderbolt_ip_header))
4028c2ecf20Sopenharmony_ci		return 0;
4038c2ecf20Sopenharmony_ci	if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid))
4048c2ecf20Sopenharmony_ci		return 0;
4058c2ecf20Sopenharmony_ci	if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid))
4068c2ecf20Sopenharmony_ci		return 0;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo;
4098c2ecf20Sopenharmony_ci	route &= ~BIT_ULL(63);
4108c2ecf20Sopenharmony_ci	if (route != net->xd->route)
4118c2ecf20Sopenharmony_ci		return 0;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK;
4148c2ecf20Sopenharmony_ci	sequence >>= TBIP_HDR_SN_SHIFT;
4158c2ecf20Sopenharmony_ci	command_id = pkg->hdr.command_id;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	switch (pkg->hdr.type) {
4188c2ecf20Sopenharmony_ci	case TBIP_LOGIN:
4198c2ecf20Sopenharmony_ci		if (!netif_running(net->dev))
4208c2ecf20Sopenharmony_ci			break;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		ret = tbnet_login_response(net, route, sequence,
4238c2ecf20Sopenharmony_ci					   pkg->hdr.command_id);
4248c2ecf20Sopenharmony_ci		if (!ret) {
4258c2ecf20Sopenharmony_ci			mutex_lock(&net->connection_lock);
4268c2ecf20Sopenharmony_ci			net->login_received = true;
4278c2ecf20Sopenharmony_ci			net->transmit_path = pkg->transmit_path;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci			/* If we reached the number of max retries or
4308c2ecf20Sopenharmony_ci			 * previous logout, schedule another round of
4318c2ecf20Sopenharmony_ci			 * login retries
4328c2ecf20Sopenharmony_ci			 */
4338c2ecf20Sopenharmony_ci			if (net->login_retries >= TBNET_LOGIN_RETRIES ||
4348c2ecf20Sopenharmony_ci			    !net->login_sent) {
4358c2ecf20Sopenharmony_ci				net->login_retries = 0;
4368c2ecf20Sopenharmony_ci				queue_delayed_work(system_long_wq,
4378c2ecf20Sopenharmony_ci						   &net->login_work, 0);
4388c2ecf20Sopenharmony_ci			}
4398c2ecf20Sopenharmony_ci			mutex_unlock(&net->connection_lock);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci			queue_work(system_long_wq, &net->connected_work);
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci		break;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	case TBIP_LOGOUT:
4468c2ecf20Sopenharmony_ci		ret = tbnet_logout_response(net, route, sequence, command_id);
4478c2ecf20Sopenharmony_ci		if (!ret)
4488c2ecf20Sopenharmony_ci			queue_work(system_long_wq, &net->disconnect_work);
4498c2ecf20Sopenharmony_ci		break;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	default:
4528c2ecf20Sopenharmony_ci		return 0;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (ret)
4568c2ecf20Sopenharmony_ci		netdev_warn(net->dev, "failed to send ThunderboltIP response\n");
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return 1;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic unsigned int tbnet_available_buffers(const struct tbnet_ring *ring)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	return ring->prod - ring->cons;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	struct tbnet_ring *ring = &net->rx_ring;
4698c2ecf20Sopenharmony_ci	int ret;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	while (nbuffers--) {
4728c2ecf20Sopenharmony_ci		struct device *dma_dev = tb_ring_dma_device(ring->ring);
4738c2ecf20Sopenharmony_ci		unsigned int index = ring->prod & (TBNET_RING_SIZE - 1);
4748c2ecf20Sopenharmony_ci		struct tbnet_frame *tf = &ring->frames[index];
4758c2ecf20Sopenharmony_ci		dma_addr_t dma_addr;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci		if (tf->page)
4788c2ecf20Sopenharmony_ci			break;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci		/* Allocate page (order > 0) so that it can hold maximum
4818c2ecf20Sopenharmony_ci		 * ThunderboltIP frame (4kB) and the additional room for
4828c2ecf20Sopenharmony_ci		 * SKB shared info required by build_skb().
4838c2ecf20Sopenharmony_ci		 */
4848c2ecf20Sopenharmony_ci		tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER);
4858c2ecf20Sopenharmony_ci		if (!tf->page) {
4868c2ecf20Sopenharmony_ci			ret = -ENOMEM;
4878c2ecf20Sopenharmony_ci			goto err_free;
4888c2ecf20Sopenharmony_ci		}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		dma_addr = dma_map_page(dma_dev, tf->page, 0,
4918c2ecf20Sopenharmony_ci					TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
4928c2ecf20Sopenharmony_ci		if (dma_mapping_error(dma_dev, dma_addr)) {
4938c2ecf20Sopenharmony_ci			ret = -ENOMEM;
4948c2ecf20Sopenharmony_ci			goto err_free;
4958c2ecf20Sopenharmony_ci		}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci		tf->frame.buffer_phy = dma_addr;
4988c2ecf20Sopenharmony_ci		tf->dev = net->dev;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci		tb_ring_rx(ring->ring, &tf->frame);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci		ring->prod++;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return 0;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_cierr_free:
5088c2ecf20Sopenharmony_ci	tbnet_free_buffers(ring);
5098c2ecf20Sopenharmony_ci	return ret;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	struct tbnet_ring *ring = &net->tx_ring;
5158c2ecf20Sopenharmony_ci	struct device *dma_dev = tb_ring_dma_device(ring->ring);
5168c2ecf20Sopenharmony_ci	struct tbnet_frame *tf;
5178c2ecf20Sopenharmony_ci	unsigned int index;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (!tbnet_available_buffers(ring))
5208c2ecf20Sopenharmony_ci		return NULL;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	index = ring->cons++ & (TBNET_RING_SIZE - 1);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	tf = &ring->frames[index];
5258c2ecf20Sopenharmony_ci	tf->frame.size = 0;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy,
5288c2ecf20Sopenharmony_ci				tbnet_frame_size(tf), DMA_TO_DEVICE);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	return tf;
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
5348c2ecf20Sopenharmony_ci			      bool canceled)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame);
5378c2ecf20Sopenharmony_ci	struct tbnet *net = netdev_priv(tf->dev);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Return buffer to the ring */
5408c2ecf20Sopenharmony_ci	net->tx_ring.prod++;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2)
5438c2ecf20Sopenharmony_ci		netif_wake_queue(net->dev);
5448c2ecf20Sopenharmony_ci}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_cistatic int tbnet_alloc_tx_buffers(struct tbnet *net)
5478c2ecf20Sopenharmony_ci{
5488c2ecf20Sopenharmony_ci	struct tbnet_ring *ring = &net->tx_ring;
5498c2ecf20Sopenharmony_ci	struct device *dma_dev = tb_ring_dma_device(ring->ring);
5508c2ecf20Sopenharmony_ci	unsigned int i;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	for (i = 0; i < TBNET_RING_SIZE; i++) {
5538c2ecf20Sopenharmony_ci		struct tbnet_frame *tf = &ring->frames[i];
5548c2ecf20Sopenharmony_ci		dma_addr_t dma_addr;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		tf->page = alloc_page(GFP_KERNEL);
5578c2ecf20Sopenharmony_ci		if (!tf->page) {
5588c2ecf20Sopenharmony_ci			tbnet_free_buffers(ring);
5598c2ecf20Sopenharmony_ci			return -ENOMEM;
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci		dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE,
5638c2ecf20Sopenharmony_ci					DMA_TO_DEVICE);
5648c2ecf20Sopenharmony_ci		if (dma_mapping_error(dma_dev, dma_addr)) {
5658c2ecf20Sopenharmony_ci			__free_page(tf->page);
5668c2ecf20Sopenharmony_ci			tf->page = NULL;
5678c2ecf20Sopenharmony_ci			tbnet_free_buffers(ring);
5688c2ecf20Sopenharmony_ci			return -ENOMEM;
5698c2ecf20Sopenharmony_ci		}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci		tf->dev = net->dev;
5728c2ecf20Sopenharmony_ci		tf->frame.buffer_phy = dma_addr;
5738c2ecf20Sopenharmony_ci		tf->frame.callback = tbnet_tx_callback;
5748c2ecf20Sopenharmony_ci		tf->frame.sof = TBIP_PDF_FRAME_START;
5758c2ecf20Sopenharmony_ci		tf->frame.eof = TBIP_PDF_FRAME_END;
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	ring->cons = 0;
5798c2ecf20Sopenharmony_ci	ring->prod = TBNET_RING_SIZE - 1;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return 0;
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic void tbnet_connected_work(struct work_struct *work)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct tbnet *net = container_of(work, typeof(*net), connected_work);
5878c2ecf20Sopenharmony_ci	bool connected;
5888c2ecf20Sopenharmony_ci	int ret;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	if (netif_carrier_ok(net->dev))
5918c2ecf20Sopenharmony_ci		return;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	mutex_lock(&net->connection_lock);
5948c2ecf20Sopenharmony_ci	connected = net->login_sent && net->login_received;
5958c2ecf20Sopenharmony_ci	mutex_unlock(&net->connection_lock);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	if (!connected)
5988c2ecf20Sopenharmony_ci		return;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	/* Both logins successful so enable the high-speed DMA paths and
6018c2ecf20Sopenharmony_ci	 * start the network device queue.
6028c2ecf20Sopenharmony_ci	 */
6038c2ecf20Sopenharmony_ci	ret = tb_xdomain_enable_paths(net->xd, TBNET_LOCAL_PATH,
6048c2ecf20Sopenharmony_ci				      net->rx_ring.ring->hop,
6058c2ecf20Sopenharmony_ci				      net->transmit_path,
6068c2ecf20Sopenharmony_ci				      net->tx_ring.ring->hop);
6078c2ecf20Sopenharmony_ci	if (ret) {
6088c2ecf20Sopenharmony_ci		netdev_err(net->dev, "failed to enable DMA paths\n");
6098c2ecf20Sopenharmony_ci		return;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	tb_ring_start(net->tx_ring.ring);
6138c2ecf20Sopenharmony_ci	tb_ring_start(net->rx_ring.ring);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE);
6168c2ecf20Sopenharmony_ci	if (ret)
6178c2ecf20Sopenharmony_ci		goto err_stop_rings;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	ret = tbnet_alloc_tx_buffers(net);
6208c2ecf20Sopenharmony_ci	if (ret)
6218c2ecf20Sopenharmony_ci		goto err_free_rx_buffers;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	netif_carrier_on(net->dev);
6248c2ecf20Sopenharmony_ci	netif_start_queue(net->dev);
6258c2ecf20Sopenharmony_ci	return;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cierr_free_rx_buffers:
6288c2ecf20Sopenharmony_ci	tbnet_free_buffers(&net->rx_ring);
6298c2ecf20Sopenharmony_cierr_stop_rings:
6308c2ecf20Sopenharmony_ci	tb_ring_stop(net->rx_ring.ring);
6318c2ecf20Sopenharmony_ci	tb_ring_stop(net->tx_ring.ring);
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic void tbnet_login_work(struct work_struct *work)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	struct tbnet *net = container_of(work, typeof(*net), login_work.work);
6378c2ecf20Sopenharmony_ci	unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY);
6388c2ecf20Sopenharmony_ci	int ret;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	if (netif_carrier_ok(net->dev))
6418c2ecf20Sopenharmony_ci		return;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	ret = tbnet_login_request(net, net->login_retries % 4);
6448c2ecf20Sopenharmony_ci	if (ret) {
6458c2ecf20Sopenharmony_ci		if (net->login_retries++ < TBNET_LOGIN_RETRIES) {
6468c2ecf20Sopenharmony_ci			queue_delayed_work(system_long_wq, &net->login_work,
6478c2ecf20Sopenharmony_ci					   delay);
6488c2ecf20Sopenharmony_ci		} else {
6498c2ecf20Sopenharmony_ci			netdev_info(net->dev, "ThunderboltIP login timed out\n");
6508c2ecf20Sopenharmony_ci		}
6518c2ecf20Sopenharmony_ci	} else {
6528c2ecf20Sopenharmony_ci		net->login_retries = 0;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci		mutex_lock(&net->connection_lock);
6558c2ecf20Sopenharmony_ci		net->login_sent = true;
6568c2ecf20Sopenharmony_ci		mutex_unlock(&net->connection_lock);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci		queue_work(system_long_wq, &net->connected_work);
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic void tbnet_disconnect_work(struct work_struct *work)
6638c2ecf20Sopenharmony_ci{
6648c2ecf20Sopenharmony_ci	struct tbnet *net = container_of(work, typeof(*net), disconnect_work);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	tbnet_tear_down(net, false);
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf,
6708c2ecf20Sopenharmony_ci			      const struct thunderbolt_ip_frame_header *hdr)
6718c2ecf20Sopenharmony_ci{
6728c2ecf20Sopenharmony_ci	u32 frame_id, frame_count, frame_size, frame_index;
6738c2ecf20Sopenharmony_ci	unsigned int size;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	if (tf->frame.flags & RING_DESC_CRC_ERROR) {
6768c2ecf20Sopenharmony_ci		net->stats.rx_crc_errors++;
6778c2ecf20Sopenharmony_ci		return false;
6788c2ecf20Sopenharmony_ci	} else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) {
6798c2ecf20Sopenharmony_ci		net->stats.rx_over_errors++;
6808c2ecf20Sopenharmony_ci		return false;
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/* Should be greater than just header i.e. contains data */
6848c2ecf20Sopenharmony_ci	size = tbnet_frame_size(tf);
6858c2ecf20Sopenharmony_ci	if (size <= sizeof(*hdr)) {
6868c2ecf20Sopenharmony_ci		net->stats.rx_length_errors++;
6878c2ecf20Sopenharmony_ci		return false;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	frame_count = le32_to_cpu(hdr->frame_count);
6918c2ecf20Sopenharmony_ci	frame_size = le32_to_cpu(hdr->frame_size);
6928c2ecf20Sopenharmony_ci	frame_index = le16_to_cpu(hdr->frame_index);
6938c2ecf20Sopenharmony_ci	frame_id = le16_to_cpu(hdr->frame_id);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if ((frame_size > size - sizeof(*hdr)) || !frame_size) {
6968c2ecf20Sopenharmony_ci		net->stats.rx_length_errors++;
6978c2ecf20Sopenharmony_ci		return false;
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	/* In case we're in the middle of packet, validate the frame
7018c2ecf20Sopenharmony_ci	 * header based on first fragment of the packet.
7028c2ecf20Sopenharmony_ci	 */
7038c2ecf20Sopenharmony_ci	if (net->skb && net->rx_hdr.frame_count) {
7048c2ecf20Sopenharmony_ci		/* Check the frame count fits the count field */
7058c2ecf20Sopenharmony_ci		if (frame_count != net->rx_hdr.frame_count) {
7068c2ecf20Sopenharmony_ci			net->stats.rx_length_errors++;
7078c2ecf20Sopenharmony_ci			return false;
7088c2ecf20Sopenharmony_ci		}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		/* Check the frame identifiers are incremented correctly,
7118c2ecf20Sopenharmony_ci		 * and id is matching.
7128c2ecf20Sopenharmony_ci		 */
7138c2ecf20Sopenharmony_ci		if (frame_index != net->rx_hdr.frame_index + 1 ||
7148c2ecf20Sopenharmony_ci		    frame_id != net->rx_hdr.frame_id) {
7158c2ecf20Sopenharmony_ci			net->stats.rx_missed_errors++;
7168c2ecf20Sopenharmony_ci			return false;
7178c2ecf20Sopenharmony_ci		}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		if (net->skb->len + frame_size > TBNET_MAX_MTU) {
7208c2ecf20Sopenharmony_ci			net->stats.rx_length_errors++;
7218c2ecf20Sopenharmony_ci			return false;
7228c2ecf20Sopenharmony_ci		}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci		return true;
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	/* Start of packet, validate the frame header */
7288c2ecf20Sopenharmony_ci	if (frame_count == 0 || frame_count > TBNET_RING_SIZE / 4) {
7298c2ecf20Sopenharmony_ci		net->stats.rx_length_errors++;
7308c2ecf20Sopenharmony_ci		return false;
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci	if (frame_index != 0) {
7338c2ecf20Sopenharmony_ci		net->stats.rx_missed_errors++;
7348c2ecf20Sopenharmony_ci		return false;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	return true;
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cistatic int tbnet_poll(struct napi_struct *napi, int budget)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct tbnet *net = container_of(napi, struct tbnet, napi);
7438c2ecf20Sopenharmony_ci	unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring);
7448c2ecf20Sopenharmony_ci	struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring);
7458c2ecf20Sopenharmony_ci	unsigned int rx_packets = 0;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	while (rx_packets < budget) {
7488c2ecf20Sopenharmony_ci		const struct thunderbolt_ip_frame_header *hdr;
7498c2ecf20Sopenharmony_ci		unsigned int hdr_size = sizeof(*hdr);
7508c2ecf20Sopenharmony_ci		struct sk_buff *skb = NULL;
7518c2ecf20Sopenharmony_ci		struct ring_frame *frame;
7528c2ecf20Sopenharmony_ci		struct tbnet_frame *tf;
7538c2ecf20Sopenharmony_ci		struct page *page;
7548c2ecf20Sopenharmony_ci		bool last = true;
7558c2ecf20Sopenharmony_ci		u32 frame_size;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci		/* Return some buffers to hardware, one at a time is too
7588c2ecf20Sopenharmony_ci		 * slow so allocate MAX_SKB_FRAGS buffers at the same
7598c2ecf20Sopenharmony_ci		 * time.
7608c2ecf20Sopenharmony_ci		 */
7618c2ecf20Sopenharmony_ci		if (cleaned_count >= MAX_SKB_FRAGS) {
7628c2ecf20Sopenharmony_ci			tbnet_alloc_rx_buffers(net, cleaned_count);
7638c2ecf20Sopenharmony_ci			cleaned_count = 0;
7648c2ecf20Sopenharmony_ci		}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci		frame = tb_ring_poll(net->rx_ring.ring);
7678c2ecf20Sopenharmony_ci		if (!frame)
7688c2ecf20Sopenharmony_ci			break;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		dma_unmap_page(dma_dev, frame->buffer_phy,
7718c2ecf20Sopenharmony_ci			       TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		tf = container_of(frame, typeof(*tf), frame);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci		page = tf->page;
7768c2ecf20Sopenharmony_ci		tf->page = NULL;
7778c2ecf20Sopenharmony_ci		net->rx_ring.cons++;
7788c2ecf20Sopenharmony_ci		cleaned_count++;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		hdr = page_address(page);
7818c2ecf20Sopenharmony_ci		if (!tbnet_check_frame(net, tf, hdr)) {
7828c2ecf20Sopenharmony_ci			__free_pages(page, TBNET_RX_PAGE_ORDER);
7838c2ecf20Sopenharmony_ci			dev_kfree_skb_any(net->skb);
7848c2ecf20Sopenharmony_ci			net->skb = NULL;
7858c2ecf20Sopenharmony_ci			continue;
7868c2ecf20Sopenharmony_ci		}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci		frame_size = le32_to_cpu(hdr->frame_size);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci		skb = net->skb;
7918c2ecf20Sopenharmony_ci		if (!skb) {
7928c2ecf20Sopenharmony_ci			skb = build_skb(page_address(page),
7938c2ecf20Sopenharmony_ci					TBNET_RX_PAGE_SIZE);
7948c2ecf20Sopenharmony_ci			if (!skb) {
7958c2ecf20Sopenharmony_ci				__free_pages(page, TBNET_RX_PAGE_ORDER);
7968c2ecf20Sopenharmony_ci				net->stats.rx_errors++;
7978c2ecf20Sopenharmony_ci				break;
7988c2ecf20Sopenharmony_ci			}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci			skb_reserve(skb, hdr_size);
8018c2ecf20Sopenharmony_ci			skb_put(skb, frame_size);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci			net->skb = skb;
8048c2ecf20Sopenharmony_ci		} else {
8058c2ecf20Sopenharmony_ci			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
8068c2ecf20Sopenharmony_ci					page, hdr_size, frame_size,
8078c2ecf20Sopenharmony_ci					TBNET_RX_PAGE_SIZE - hdr_size);
8088c2ecf20Sopenharmony_ci		}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci		net->rx_hdr.frame_size = frame_size;
8118c2ecf20Sopenharmony_ci		net->rx_hdr.frame_count = le32_to_cpu(hdr->frame_count);
8128c2ecf20Sopenharmony_ci		net->rx_hdr.frame_index = le16_to_cpu(hdr->frame_index);
8138c2ecf20Sopenharmony_ci		net->rx_hdr.frame_id = le16_to_cpu(hdr->frame_id);
8148c2ecf20Sopenharmony_ci		last = net->rx_hdr.frame_index == net->rx_hdr.frame_count - 1;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		rx_packets++;
8178c2ecf20Sopenharmony_ci		net->stats.rx_bytes += frame_size;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci		if (last) {
8208c2ecf20Sopenharmony_ci			skb->protocol = eth_type_trans(skb, net->dev);
8218c2ecf20Sopenharmony_ci			napi_gro_receive(&net->napi, skb);
8228c2ecf20Sopenharmony_ci			net->skb = NULL;
8238c2ecf20Sopenharmony_ci		}
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	net->stats.rx_packets += rx_packets;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	if (cleaned_count)
8298c2ecf20Sopenharmony_ci		tbnet_alloc_rx_buffers(net, cleaned_count);
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	if (rx_packets >= budget)
8328c2ecf20Sopenharmony_ci		return budget;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	napi_complete_done(napi, rx_packets);
8358c2ecf20Sopenharmony_ci	/* Re-enable the ring interrupt */
8368c2ecf20Sopenharmony_ci	tb_ring_poll_complete(net->rx_ring.ring);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	return rx_packets;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic void tbnet_start_poll(void *data)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	struct tbnet *net = data;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	napi_schedule(&net->napi);
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic int tbnet_open(struct net_device *dev)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	struct tbnet *net = netdev_priv(dev);
8518c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = net->xd;
8528c2ecf20Sopenharmony_ci	u16 sof_mask, eof_mask;
8538c2ecf20Sopenharmony_ci	struct tb_ring *ring;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE,
8588c2ecf20Sopenharmony_ci				RING_FLAG_FRAME);
8598c2ecf20Sopenharmony_ci	if (!ring) {
8608c2ecf20Sopenharmony_ci		netdev_err(dev, "failed to allocate Tx ring\n");
8618c2ecf20Sopenharmony_ci		return -ENOMEM;
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci	net->tx_ring.ring = ring;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	sof_mask = BIT(TBIP_PDF_FRAME_START);
8668c2ecf20Sopenharmony_ci	eof_mask = BIT(TBIP_PDF_FRAME_END);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE,
8698c2ecf20Sopenharmony_ci				RING_FLAG_FRAME, sof_mask, eof_mask,
8708c2ecf20Sopenharmony_ci				tbnet_start_poll, net);
8718c2ecf20Sopenharmony_ci	if (!ring) {
8728c2ecf20Sopenharmony_ci		netdev_err(dev, "failed to allocate Rx ring\n");
8738c2ecf20Sopenharmony_ci		tb_ring_free(net->tx_ring.ring);
8748c2ecf20Sopenharmony_ci		net->tx_ring.ring = NULL;
8758c2ecf20Sopenharmony_ci		return -ENOMEM;
8768c2ecf20Sopenharmony_ci	}
8778c2ecf20Sopenharmony_ci	net->rx_ring.ring = ring;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	napi_enable(&net->napi);
8808c2ecf20Sopenharmony_ci	start_login(net);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	return 0;
8838c2ecf20Sopenharmony_ci}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_cistatic int tbnet_stop(struct net_device *dev)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	struct tbnet *net = netdev_priv(dev);
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	napi_disable(&net->napi);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	cancel_work_sync(&net->disconnect_work);
8928c2ecf20Sopenharmony_ci	tbnet_tear_down(net, true);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	tb_ring_free(net->rx_ring.ring);
8958c2ecf20Sopenharmony_ci	net->rx_ring.ring = NULL;
8968c2ecf20Sopenharmony_ci	tb_ring_free(net->tx_ring.ring);
8978c2ecf20Sopenharmony_ci	net->tx_ring.ring = NULL;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	return 0;
9008c2ecf20Sopenharmony_ci}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_cistatic bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb,
9038c2ecf20Sopenharmony_ci	struct tbnet_frame **frames, u32 frame_count)
9048c2ecf20Sopenharmony_ci{
9058c2ecf20Sopenharmony_ci	struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page);
9068c2ecf20Sopenharmony_ci	struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring);
9078c2ecf20Sopenharmony_ci	__wsum wsum = htonl(skb->len - skb_transport_offset(skb));
9088c2ecf20Sopenharmony_ci	unsigned int i, len, offset = skb_transport_offset(skb);
9098c2ecf20Sopenharmony_ci	__be16 protocol = skb->protocol;
9108c2ecf20Sopenharmony_ci	void *data = skb->data;
9118c2ecf20Sopenharmony_ci	void *dest = hdr + 1;
9128c2ecf20Sopenharmony_ci	__sum16 *tucso;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	if (skb->ip_summed != CHECKSUM_PARTIAL) {
9158c2ecf20Sopenharmony_ci		/* No need to calculate checksum so we just update the
9168c2ecf20Sopenharmony_ci		 * total frame count and sync the frames for DMA.
9178c2ecf20Sopenharmony_ci		 */
9188c2ecf20Sopenharmony_ci		for (i = 0; i < frame_count; i++) {
9198c2ecf20Sopenharmony_ci			hdr = page_address(frames[i]->page);
9208c2ecf20Sopenharmony_ci			hdr->frame_count = cpu_to_le32(frame_count);
9218c2ecf20Sopenharmony_ci			dma_sync_single_for_device(dma_dev,
9228c2ecf20Sopenharmony_ci				frames[i]->frame.buffer_phy,
9238c2ecf20Sopenharmony_ci				tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
9248c2ecf20Sopenharmony_ci		}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci		return true;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	if (protocol == htons(ETH_P_8021Q)) {
9308c2ecf20Sopenharmony_ci		struct vlan_hdr *vhdr, vh;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci		vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh);
9338c2ecf20Sopenharmony_ci		if (!vhdr)
9348c2ecf20Sopenharmony_ci			return false;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci		protocol = vhdr->h_vlan_encapsulated_proto;
9378c2ecf20Sopenharmony_ci	}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	/* Data points on the beginning of packet.
9408c2ecf20Sopenharmony_ci	 * Check is the checksum absolute place in the packet.
9418c2ecf20Sopenharmony_ci	 * ipcso will update IP checksum.
9428c2ecf20Sopenharmony_ci	 * tucso will update TCP/UPD checksum.
9438c2ecf20Sopenharmony_ci	 */
9448c2ecf20Sopenharmony_ci	if (protocol == htons(ETH_P_IP)) {
9458c2ecf20Sopenharmony_ci		__sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci		*ipcso = 0;
9488c2ecf20Sopenharmony_ci		*ipcso = ip_fast_csum(dest + skb_network_offset(skb),
9498c2ecf20Sopenharmony_ci				      ip_hdr(skb)->ihl);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
9528c2ecf20Sopenharmony_ci			tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
9538c2ecf20Sopenharmony_ci		else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
9548c2ecf20Sopenharmony_ci			tucso = dest + ((void *)&(udp_hdr(skb)->check) - data);
9558c2ecf20Sopenharmony_ci		else
9568c2ecf20Sopenharmony_ci			return false;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci		*tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
9598c2ecf20Sopenharmony_ci					    ip_hdr(skb)->daddr, 0,
9608c2ecf20Sopenharmony_ci					    ip_hdr(skb)->protocol, 0);
9618c2ecf20Sopenharmony_ci	} else if (skb_is_gso(skb) && skb_is_gso_v6(skb)) {
9628c2ecf20Sopenharmony_ci		tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data);
9638c2ecf20Sopenharmony_ci		*tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
9648c2ecf20Sopenharmony_ci					  &ipv6_hdr(skb)->daddr, 0,
9658c2ecf20Sopenharmony_ci					  IPPROTO_TCP, 0);
9668c2ecf20Sopenharmony_ci	} else if (protocol == htons(ETH_P_IPV6)) {
9678c2ecf20Sopenharmony_ci		tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset;
9688c2ecf20Sopenharmony_ci		*tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
9698c2ecf20Sopenharmony_ci					  &ipv6_hdr(skb)->daddr, 0,
9708c2ecf20Sopenharmony_ci					  ipv6_hdr(skb)->nexthdr, 0);
9718c2ecf20Sopenharmony_ci	} else {
9728c2ecf20Sopenharmony_ci		return false;
9738c2ecf20Sopenharmony_ci	}
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	/* First frame was headers, rest of the frames contain data.
9768c2ecf20Sopenharmony_ci	 * Calculate checksum over each frame.
9778c2ecf20Sopenharmony_ci	 */
9788c2ecf20Sopenharmony_ci	for (i = 0; i < frame_count; i++) {
9798c2ecf20Sopenharmony_ci		hdr = page_address(frames[i]->page);
9808c2ecf20Sopenharmony_ci		dest = (void *)(hdr + 1) + offset;
9818c2ecf20Sopenharmony_ci		len = le32_to_cpu(hdr->frame_size) - offset;
9828c2ecf20Sopenharmony_ci		wsum = csum_partial(dest, len, wsum);
9838c2ecf20Sopenharmony_ci		hdr->frame_count = cpu_to_le32(frame_count);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci		offset = 0;
9868c2ecf20Sopenharmony_ci	}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	*tucso = csum_fold(wsum);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	/* Checksum is finally calculated and we don't touch the memory
9918c2ecf20Sopenharmony_ci	 * anymore, so DMA sync the frames now.
9928c2ecf20Sopenharmony_ci	 */
9938c2ecf20Sopenharmony_ci	for (i = 0; i < frame_count; i++) {
9948c2ecf20Sopenharmony_ci		dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy,
9958c2ecf20Sopenharmony_ci			tbnet_frame_size(frames[i]), DMA_TO_DEVICE);
9968c2ecf20Sopenharmony_ci	}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	return true;
9998c2ecf20Sopenharmony_ci}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_cistatic void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num,
10028c2ecf20Sopenharmony_ci			     unsigned int *len)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num];
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	*len = skb_frag_size(frag);
10078c2ecf20Sopenharmony_ci	return kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
10088c2ecf20Sopenharmony_ci}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cistatic netdev_tx_t tbnet_start_xmit(struct sk_buff *skb,
10118c2ecf20Sopenharmony_ci				    struct net_device *dev)
10128c2ecf20Sopenharmony_ci{
10138c2ecf20Sopenharmony_ci	struct tbnet *net = netdev_priv(dev);
10148c2ecf20Sopenharmony_ci	struct tbnet_frame *frames[MAX_SKB_FRAGS];
10158c2ecf20Sopenharmony_ci	u16 frame_id = atomic_read(&net->frame_id);
10168c2ecf20Sopenharmony_ci	struct thunderbolt_ip_frame_header *hdr;
10178c2ecf20Sopenharmony_ci	unsigned int len = skb_headlen(skb);
10188c2ecf20Sopenharmony_ci	unsigned int data_len = skb->len;
10198c2ecf20Sopenharmony_ci	unsigned int nframes, i;
10208c2ecf20Sopenharmony_ci	unsigned int frag = 0;
10218c2ecf20Sopenharmony_ci	void *src = skb->data;
10228c2ecf20Sopenharmony_ci	u32 frame_index = 0;
10238c2ecf20Sopenharmony_ci	bool unmap = false;
10248c2ecf20Sopenharmony_ci	void *dest;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE);
10278c2ecf20Sopenharmony_ci	if (tbnet_available_buffers(&net->tx_ring) < nframes) {
10288c2ecf20Sopenharmony_ci		netif_stop_queue(net->dev);
10298c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
10308c2ecf20Sopenharmony_ci	}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	frames[frame_index] = tbnet_get_tx_buffer(net);
10338c2ecf20Sopenharmony_ci	if (!frames[frame_index])
10348c2ecf20Sopenharmony_ci		goto err_drop;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	hdr = page_address(frames[frame_index]->page);
10378c2ecf20Sopenharmony_ci	dest = hdr + 1;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	/* If overall packet is bigger than the frame data size */
10408c2ecf20Sopenharmony_ci	while (data_len > TBNET_MAX_PAYLOAD_SIZE) {
10418c2ecf20Sopenharmony_ci		unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE;
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci		hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE);
10448c2ecf20Sopenharmony_ci		hdr->frame_index = cpu_to_le16(frame_index);
10458c2ecf20Sopenharmony_ci		hdr->frame_id = cpu_to_le16(frame_id);
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci		do {
10488c2ecf20Sopenharmony_ci			if (len > size_left) {
10498c2ecf20Sopenharmony_ci				/* Copy data onto Tx buffer data with
10508c2ecf20Sopenharmony_ci				 * full frame size then break and go to
10518c2ecf20Sopenharmony_ci				 * next frame
10528c2ecf20Sopenharmony_ci				 */
10538c2ecf20Sopenharmony_ci				memcpy(dest, src, size_left);
10548c2ecf20Sopenharmony_ci				len -= size_left;
10558c2ecf20Sopenharmony_ci				dest += size_left;
10568c2ecf20Sopenharmony_ci				src += size_left;
10578c2ecf20Sopenharmony_ci				break;
10588c2ecf20Sopenharmony_ci			}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci			memcpy(dest, src, len);
10618c2ecf20Sopenharmony_ci			size_left -= len;
10628c2ecf20Sopenharmony_ci			dest += len;
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci			if (unmap) {
10658c2ecf20Sopenharmony_ci				kunmap_atomic(src);
10668c2ecf20Sopenharmony_ci				unmap = false;
10678c2ecf20Sopenharmony_ci			}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci			/* Ensure all fragments have been processed */
10708c2ecf20Sopenharmony_ci			if (frag < skb_shinfo(skb)->nr_frags) {
10718c2ecf20Sopenharmony_ci				/* Map and then unmap quickly */
10728c2ecf20Sopenharmony_ci				src = tbnet_kmap_frag(skb, frag++, &len);
10738c2ecf20Sopenharmony_ci				unmap = true;
10748c2ecf20Sopenharmony_ci			} else if (unlikely(size_left > 0)) {
10758c2ecf20Sopenharmony_ci				goto err_drop;
10768c2ecf20Sopenharmony_ci			}
10778c2ecf20Sopenharmony_ci		} while (size_left > 0);
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci		data_len -= TBNET_MAX_PAYLOAD_SIZE;
10808c2ecf20Sopenharmony_ci		frame_index++;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci		frames[frame_index] = tbnet_get_tx_buffer(net);
10838c2ecf20Sopenharmony_ci		if (!frames[frame_index])
10848c2ecf20Sopenharmony_ci			goto err_drop;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci		hdr = page_address(frames[frame_index]->page);
10878c2ecf20Sopenharmony_ci		dest = hdr + 1;
10888c2ecf20Sopenharmony_ci	}
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	hdr->frame_size = cpu_to_le32(data_len);
10918c2ecf20Sopenharmony_ci	hdr->frame_index = cpu_to_le16(frame_index);
10928c2ecf20Sopenharmony_ci	hdr->frame_id = cpu_to_le16(frame_id);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	frames[frame_index]->frame.size = data_len + sizeof(*hdr);
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	/* In case the remaining data_len is smaller than a frame */
10978c2ecf20Sopenharmony_ci	while (len < data_len) {
10988c2ecf20Sopenharmony_ci		memcpy(dest, src, len);
10998c2ecf20Sopenharmony_ci		data_len -= len;
11008c2ecf20Sopenharmony_ci		dest += len;
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci		if (unmap) {
11038c2ecf20Sopenharmony_ci			kunmap_atomic(src);
11048c2ecf20Sopenharmony_ci			unmap = false;
11058c2ecf20Sopenharmony_ci		}
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci		if (frag < skb_shinfo(skb)->nr_frags) {
11088c2ecf20Sopenharmony_ci			src = tbnet_kmap_frag(skb, frag++, &len);
11098c2ecf20Sopenharmony_ci			unmap = true;
11108c2ecf20Sopenharmony_ci		} else if (unlikely(data_len > 0)) {
11118c2ecf20Sopenharmony_ci			goto err_drop;
11128c2ecf20Sopenharmony_ci		}
11138c2ecf20Sopenharmony_ci	}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	memcpy(dest, src, data_len);
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	if (unmap)
11188c2ecf20Sopenharmony_ci		kunmap_atomic(src);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1))
11218c2ecf20Sopenharmony_ci		goto err_drop;
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	for (i = 0; i < frame_index + 1; i++)
11248c2ecf20Sopenharmony_ci		tb_ring_tx(net->tx_ring.ring, &frames[i]->frame);
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID)
11278c2ecf20Sopenharmony_ci		atomic_inc(&net->frame_id);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	net->stats.tx_packets++;
11308c2ecf20Sopenharmony_ci	net->stats.tx_bytes += skb->len;
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	dev_consume_skb_any(skb);
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_cierr_drop:
11378c2ecf20Sopenharmony_ci	/* We can re-use the buffers */
11388c2ecf20Sopenharmony_ci	net->tx_ring.cons -= frame_index;
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
11418c2ecf20Sopenharmony_ci	net->stats.tx_errors++;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
11448c2ecf20Sopenharmony_ci}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_cistatic void tbnet_get_stats64(struct net_device *dev,
11478c2ecf20Sopenharmony_ci			      struct rtnl_link_stats64 *stats)
11488c2ecf20Sopenharmony_ci{
11498c2ecf20Sopenharmony_ci	struct tbnet *net = netdev_priv(dev);
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	stats->tx_packets = net->stats.tx_packets;
11528c2ecf20Sopenharmony_ci	stats->rx_packets = net->stats.rx_packets;
11538c2ecf20Sopenharmony_ci	stats->tx_bytes = net->stats.tx_bytes;
11548c2ecf20Sopenharmony_ci	stats->rx_bytes = net->stats.rx_bytes;
11558c2ecf20Sopenharmony_ci	stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors +
11568c2ecf20Sopenharmony_ci		net->stats.rx_over_errors + net->stats.rx_crc_errors +
11578c2ecf20Sopenharmony_ci		net->stats.rx_missed_errors;
11588c2ecf20Sopenharmony_ci	stats->tx_errors = net->stats.tx_errors;
11598c2ecf20Sopenharmony_ci	stats->rx_length_errors = net->stats.rx_length_errors;
11608c2ecf20Sopenharmony_ci	stats->rx_over_errors = net->stats.rx_over_errors;
11618c2ecf20Sopenharmony_ci	stats->rx_crc_errors = net->stats.rx_crc_errors;
11628c2ecf20Sopenharmony_ci	stats->rx_missed_errors = net->stats.rx_missed_errors;
11638c2ecf20Sopenharmony_ci}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_cistatic const struct net_device_ops tbnet_netdev_ops = {
11668c2ecf20Sopenharmony_ci	.ndo_open = tbnet_open,
11678c2ecf20Sopenharmony_ci	.ndo_stop = tbnet_stop,
11688c2ecf20Sopenharmony_ci	.ndo_start_xmit = tbnet_start_xmit,
11698c2ecf20Sopenharmony_ci	.ndo_get_stats64 = tbnet_get_stats64,
11708c2ecf20Sopenharmony_ci};
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_cistatic void tbnet_generate_mac(struct net_device *dev)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	const struct tbnet *net = netdev_priv(dev);
11758c2ecf20Sopenharmony_ci	const struct tb_xdomain *xd = net->xd;
11768c2ecf20Sopenharmony_ci	u8 phy_port;
11778c2ecf20Sopenharmony_ci	u32 hash;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route));
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	/* Unicast and locally administered MAC */
11828c2ecf20Sopenharmony_ci	dev->dev_addr[0] = phy_port << 4 | 0x02;
11838c2ecf20Sopenharmony_ci	hash = jhash2((u32 *)xd->local_uuid, 4, 0);
11848c2ecf20Sopenharmony_ci	memcpy(dev->dev_addr + 1, &hash, sizeof(hash));
11858c2ecf20Sopenharmony_ci	hash = jhash2((u32 *)xd->local_uuid, 4, hash);
11868c2ecf20Sopenharmony_ci	dev->dev_addr[5] = hash & 0xff;
11878c2ecf20Sopenharmony_ci}
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_cistatic int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id)
11908c2ecf20Sopenharmony_ci{
11918c2ecf20Sopenharmony_ci	struct tb_xdomain *xd = tb_service_parent(svc);
11928c2ecf20Sopenharmony_ci	struct net_device *dev;
11938c2ecf20Sopenharmony_ci	struct tbnet *net;
11948c2ecf20Sopenharmony_ci	int ret;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(*net));
11978c2ecf20Sopenharmony_ci	if (!dev)
11988c2ecf20Sopenharmony_ci		return -ENOMEM;
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, &svc->dev);
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	net = netdev_priv(dev);
12038c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&net->login_work, tbnet_login_work);
12048c2ecf20Sopenharmony_ci	INIT_WORK(&net->connected_work, tbnet_connected_work);
12058c2ecf20Sopenharmony_ci	INIT_WORK(&net->disconnect_work, tbnet_disconnect_work);
12068c2ecf20Sopenharmony_ci	mutex_init(&net->connection_lock);
12078c2ecf20Sopenharmony_ci	atomic_set(&net->command_id, 0);
12088c2ecf20Sopenharmony_ci	atomic_set(&net->frame_id, 0);
12098c2ecf20Sopenharmony_ci	net->svc = svc;
12108c2ecf20Sopenharmony_ci	net->dev = dev;
12118c2ecf20Sopenharmony_ci	net->xd = xd;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	tbnet_generate_mac(dev);
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	strcpy(dev->name, "thunderbolt%d");
12168c2ecf20Sopenharmony_ci	dev->netdev_ops = &tbnet_netdev_ops;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	/* ThunderboltIP takes advantage of TSO packets but instead of
12198c2ecf20Sopenharmony_ci	 * segmenting them we just split the packet into Thunderbolt
12208c2ecf20Sopenharmony_ci	 * frames (maximum payload size of each frame is 4084 bytes) and
12218c2ecf20Sopenharmony_ci	 * calculate checksum over the whole packet here.
12228c2ecf20Sopenharmony_ci	 *
12238c2ecf20Sopenharmony_ci	 * The receiving side does the opposite if the host OS supports
12248c2ecf20Sopenharmony_ci	 * LRO, otherwise it needs to split the large packet into MTU
12258c2ecf20Sopenharmony_ci	 * sized smaller packets.
12268c2ecf20Sopenharmony_ci	 *
12278c2ecf20Sopenharmony_ci	 * In order to receive large packets from the networking stack,
12288c2ecf20Sopenharmony_ci	 * we need to announce support for most of the offloading
12298c2ecf20Sopenharmony_ci	 * features here.
12308c2ecf20Sopenharmony_ci	 */
12318c2ecf20Sopenharmony_ci	dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO |
12328c2ecf20Sopenharmony_ci			   NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
12338c2ecf20Sopenharmony_ci	dev->features = dev->hw_features | NETIF_F_HIGHDMA;
12348c2ecf20Sopenharmony_ci	dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	netif_napi_add(dev, &net->napi, tbnet_poll, NAPI_POLL_WEIGHT);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	/* MTU range: 68 - 65522 */
12398c2ecf20Sopenharmony_ci	dev->min_mtu = ETH_MIN_MTU;
12408c2ecf20Sopenharmony_ci	dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	net->handler.uuid = &tbnet_svc_uuid;
12438c2ecf20Sopenharmony_ci	net->handler.callback = tbnet_handle_packet,
12448c2ecf20Sopenharmony_ci	net->handler.data = net;
12458c2ecf20Sopenharmony_ci	tb_register_protocol_handler(&net->handler);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	tb_service_set_drvdata(svc, net);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	ret = register_netdev(dev);
12508c2ecf20Sopenharmony_ci	if (ret) {
12518c2ecf20Sopenharmony_ci		tb_unregister_protocol_handler(&net->handler);
12528c2ecf20Sopenharmony_ci		free_netdev(dev);
12538c2ecf20Sopenharmony_ci		return ret;
12548c2ecf20Sopenharmony_ci	}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	return 0;
12578c2ecf20Sopenharmony_ci}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_cistatic void tbnet_remove(struct tb_service *svc)
12608c2ecf20Sopenharmony_ci{
12618c2ecf20Sopenharmony_ci	struct tbnet *net = tb_service_get_drvdata(svc);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	unregister_netdev(net->dev);
12648c2ecf20Sopenharmony_ci	tb_unregister_protocol_handler(&net->handler);
12658c2ecf20Sopenharmony_ci	free_netdev(net->dev);
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_cistatic void tbnet_shutdown(struct tb_service *svc)
12698c2ecf20Sopenharmony_ci{
12708c2ecf20Sopenharmony_ci	tbnet_tear_down(tb_service_get_drvdata(svc), true);
12718c2ecf20Sopenharmony_ci}
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_cistatic int __maybe_unused tbnet_suspend(struct device *dev)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	struct tb_service *svc = tb_to_service(dev);
12768c2ecf20Sopenharmony_ci	struct tbnet *net = tb_service_get_drvdata(svc);
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	stop_login(net);
12798c2ecf20Sopenharmony_ci	if (netif_running(net->dev)) {
12808c2ecf20Sopenharmony_ci		netif_device_detach(net->dev);
12818c2ecf20Sopenharmony_ci		tbnet_tear_down(net, true);
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	tb_unregister_protocol_handler(&net->handler);
12858c2ecf20Sopenharmony_ci	return 0;
12868c2ecf20Sopenharmony_ci}
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_cistatic int __maybe_unused tbnet_resume(struct device *dev)
12898c2ecf20Sopenharmony_ci{
12908c2ecf20Sopenharmony_ci	struct tb_service *svc = tb_to_service(dev);
12918c2ecf20Sopenharmony_ci	struct tbnet *net = tb_service_get_drvdata(svc);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	tb_register_protocol_handler(&net->handler);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	netif_carrier_off(net->dev);
12968c2ecf20Sopenharmony_ci	if (netif_running(net->dev)) {
12978c2ecf20Sopenharmony_ci		netif_device_attach(net->dev);
12988c2ecf20Sopenharmony_ci		start_login(net);
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	return 0;
13028c2ecf20Sopenharmony_ci}
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_cistatic const struct dev_pm_ops tbnet_pm_ops = {
13058c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(tbnet_suspend, tbnet_resume)
13068c2ecf20Sopenharmony_ci};
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_cistatic const struct tb_service_id tbnet_ids[] = {
13098c2ecf20Sopenharmony_ci	{ TB_SERVICE("network", 1) },
13108c2ecf20Sopenharmony_ci	{ },
13118c2ecf20Sopenharmony_ci};
13128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(tbsvc, tbnet_ids);
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_cistatic struct tb_service_driver tbnet_driver = {
13158c2ecf20Sopenharmony_ci	.driver = {
13168c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
13178c2ecf20Sopenharmony_ci		.name = "thunderbolt-net",
13188c2ecf20Sopenharmony_ci		.pm = &tbnet_pm_ops,
13198c2ecf20Sopenharmony_ci	},
13208c2ecf20Sopenharmony_ci	.probe = tbnet_probe,
13218c2ecf20Sopenharmony_ci	.remove = tbnet_remove,
13228c2ecf20Sopenharmony_ci	.shutdown = tbnet_shutdown,
13238c2ecf20Sopenharmony_ci	.id_table = tbnet_ids,
13248c2ecf20Sopenharmony_ci};
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_cistatic int __init tbnet_init(void)
13278c2ecf20Sopenharmony_ci{
13288c2ecf20Sopenharmony_ci	int ret;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid);
13318c2ecf20Sopenharmony_ci	if (!tbnet_dir)
13328c2ecf20Sopenharmony_ci		return -ENOMEM;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	tb_property_add_immediate(tbnet_dir, "prtcid", 1);
13358c2ecf20Sopenharmony_ci	tb_property_add_immediate(tbnet_dir, "prtcvers", 1);
13368c2ecf20Sopenharmony_ci	tb_property_add_immediate(tbnet_dir, "prtcrevs", 1);
13378c2ecf20Sopenharmony_ci	/* Currently only announce support for match frags ID (bit 1). Bit 0
13388c2ecf20Sopenharmony_ci	 * is reserved for full E2E flow control which we do not support at
13398c2ecf20Sopenharmony_ci	 * the moment.
13408c2ecf20Sopenharmony_ci	 */
13418c2ecf20Sopenharmony_ci	tb_property_add_immediate(tbnet_dir, "prtcstns",
13428c2ecf20Sopenharmony_ci				  TBNET_MATCH_FRAGS_ID);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	ret = tb_register_property_dir("network", tbnet_dir);
13458c2ecf20Sopenharmony_ci	if (ret)
13468c2ecf20Sopenharmony_ci		goto err_free_dir;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	ret = tb_register_service_driver(&tbnet_driver);
13498c2ecf20Sopenharmony_ci	if (ret)
13508c2ecf20Sopenharmony_ci		goto err_unregister;
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	return 0;
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_cierr_unregister:
13558c2ecf20Sopenharmony_ci	tb_unregister_property_dir("network", tbnet_dir);
13568c2ecf20Sopenharmony_cierr_free_dir:
13578c2ecf20Sopenharmony_ci	tb_property_free_dir(tbnet_dir);
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	return ret;
13608c2ecf20Sopenharmony_ci}
13618c2ecf20Sopenharmony_cimodule_init(tbnet_init);
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_cistatic void __exit tbnet_exit(void)
13648c2ecf20Sopenharmony_ci{
13658c2ecf20Sopenharmony_ci	tb_unregister_service_driver(&tbnet_driver);
13668c2ecf20Sopenharmony_ci	tb_unregister_property_dir("network", tbnet_dir);
13678c2ecf20Sopenharmony_ci	tb_property_free_dir(tbnet_dir);
13688c2ecf20Sopenharmony_ci}
13698c2ecf20Sopenharmony_cimodule_exit(tbnet_exit);
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ciMODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>");
13728c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>");
13738c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
13748c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Thunderbolt network driver");
13758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1376