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