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