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 /* Tear the paths down before stopping the rings. This mirrors 390 * tbnet_connected_work(), which enables the paths last so the 391 * Rx ring is primed before packets can arrive. Stopping a 392 * ring zeroes its descriptor base and tbnet_free_buffers() 393 * unmaps and frees the frame buffers, leaving anything still 394 * in flight with nowhere to drain to; 395 * __tb_path_deactivate_hop() then waits for the hop's 396 * 'pending' bit, which on some host routers never clears in 397 * that state. 398 */ 399 ret = tb_xdomain_disable_paths(net->xd, 400 net->local_transmit_path, 401 net->tx_ring.ring->hop, 402 net->remote_transmit_path, 403 net->rx_ring.ring->hop); 404 if (ret) 405 netdev_warn(net->dev, "failed to disable DMA paths\n"); 406 407 tb_ring_stop(net->rx_ring.ring); 408 tb_ring_stop(net->tx_ring.ring); 409 tbnet_free_buffers(&net->rx_ring); 410 tbnet_free_buffers(&net->tx_ring); 411 412 tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path); 413 net->remote_transmit_path = 0; 414 } 415 416 net->login_retries = 0; 417 net->login_sent = false; 418 net->login_received = false; 419 420 netdev_dbg(net->dev, "network traffic stopped\n"); 421 422 mutex_unlock(&net->connection_lock); 423 } 424 425 static int tbnet_handle_packet(const void *buf, size_t size, void *data) 426 { 427 const struct thunderbolt_ip_login *pkg = buf; 428 struct tbnet *net = data; 429 u32 command_id; 430 int ret = 0; 431 u32 sequence; 432 u64 route; 433 434 /* Make sure the packet is for us */ 435 if (size < sizeof(struct thunderbolt_ip_header)) 436 return 0; 437 if (!uuid_equal(&pkg->hdr.initiator_uuid, net->xd->remote_uuid)) 438 return 0; 439 if (!uuid_equal(&pkg->hdr.target_uuid, net->xd->local_uuid)) 440 return 0; 441 442 route = ((u64)pkg->hdr.route_hi << 32) | pkg->hdr.route_lo; 443 route &= ~BIT_ULL(63); 444 if (route != net->xd->route) 445 return 0; 446 447 sequence = pkg->hdr.length_sn & TBIP_HDR_SN_MASK; 448 sequence >>= TBIP_HDR_SN_SHIFT; 449 command_id = pkg->hdr.command_id; 450 451 switch (pkg->hdr.type) { 452 case TBIP_LOGIN: 453 netdev_dbg(net->dev, "remote login request received\n"); 454 if (!netif_running(net->dev)) 455 break; 456 457 ret = tbnet_login_response(net, route, sequence, 458 pkg->hdr.command_id); 459 if (!ret) { 460 netdev_dbg(net->dev, "remote login response sent\n"); 461 462 mutex_lock(&net->connection_lock); 463 net->login_received = true; 464 net->remote_transmit_path = pkg->transmit_path; 465 466 /* If we reached the number of max retries or 467 * previous logout, schedule another round of 468 * login retries 469 */ 470 if (net->login_retries >= TBNET_LOGIN_RETRIES || 471 !net->login_sent) { 472 net->login_retries = 0; 473 queue_delayed_work(system_long_wq, 474 &net->login_work, 0); 475 } 476 mutex_unlock(&net->connection_lock); 477 478 queue_work(system_long_wq, &net->connected_work); 479 } 480 break; 481 482 case TBIP_LOGOUT: 483 netdev_dbg(net->dev, "remote logout request received\n"); 484 ret = tbnet_logout_response(net, route, sequence, command_id); 485 if (!ret) { 486 netdev_dbg(net->dev, "remote logout response sent\n"); 487 queue_work(system_long_wq, &net->disconnect_work); 488 } 489 break; 490 491 default: 492 return 0; 493 } 494 495 if (ret) 496 netdev_warn(net->dev, "failed to send ThunderboltIP response\n"); 497 498 return 1; 499 } 500 501 static unsigned int tbnet_available_buffers(const struct tbnet_ring *ring) 502 { 503 return ring->prod - ring->cons; 504 } 505 506 static int tbnet_alloc_rx_buffers(struct tbnet *net, unsigned int nbuffers) 507 { 508 struct tbnet_ring *ring = &net->rx_ring; 509 int ret; 510 511 while (nbuffers--) { 512 struct device *dma_dev = tb_ring_dma_device(ring->ring); 513 unsigned int index = ring->prod & (TBNET_RING_SIZE - 1); 514 struct tbnet_frame *tf = &ring->frames[index]; 515 dma_addr_t dma_addr; 516 517 if (tf->page) 518 break; 519 520 /* Allocate page (order > 0) so that it can hold maximum 521 * ThunderboltIP frame (4kB) and the additional room for 522 * SKB shared info required by build_skb(). 523 */ 524 tf->page = dev_alloc_pages(TBNET_RX_PAGE_ORDER); 525 if (!tf->page) { 526 ret = -ENOMEM; 527 goto err_free; 528 } 529 530 dma_addr = dma_map_page(dma_dev, tf->page, 0, 531 TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE); 532 if (dma_mapping_error(dma_dev, dma_addr)) { 533 ret = -ENOMEM; 534 goto err_free; 535 } 536 537 tf->frame.buffer_phy = dma_addr; 538 tf->dev = net->dev; 539 540 trace_tbnet_alloc_rx_frame(index, tf->page, dma_addr, 541 DMA_FROM_DEVICE); 542 543 tb_ring_rx(ring->ring, &tf->frame); 544 545 ring->prod++; 546 } 547 548 return 0; 549 550 err_free: 551 tbnet_free_buffers(ring); 552 return ret; 553 } 554 555 static struct tbnet_frame *tbnet_get_tx_buffer(struct tbnet *net) 556 { 557 struct tbnet_ring *ring = &net->tx_ring; 558 struct device *dma_dev = tb_ring_dma_device(ring->ring); 559 struct tbnet_frame *tf; 560 unsigned int index; 561 562 if (!tbnet_available_buffers(ring)) 563 return NULL; 564 565 index = ring->cons++ & (TBNET_RING_SIZE - 1); 566 567 tf = &ring->frames[index]; 568 tf->frame.size = 0; 569 570 dma_sync_single_for_cpu(dma_dev, tf->frame.buffer_phy, 571 tb_ring_frame_size(&tf->frame), DMA_TO_DEVICE); 572 573 return tf; 574 } 575 576 static void tbnet_tx_callback(struct tb_ring *ring, struct ring_frame *frame, 577 bool canceled) 578 { 579 struct tbnet_frame *tf = container_of(frame, typeof(*tf), frame); 580 struct tbnet *net = netdev_priv(tf->dev); 581 582 /* Return buffer to the ring */ 583 net->tx_ring.prod++; 584 585 if (tbnet_available_buffers(&net->tx_ring) >= TBNET_RING_SIZE / 2) 586 netif_wake_queue(net->dev); 587 } 588 589 static int tbnet_alloc_tx_buffers(struct tbnet *net) 590 { 591 struct tbnet_ring *ring = &net->tx_ring; 592 struct device *dma_dev = tb_ring_dma_device(ring->ring); 593 unsigned int i; 594 595 for (i = 0; i < TBNET_RING_SIZE; i++) { 596 struct tbnet_frame *tf = &ring->frames[i]; 597 dma_addr_t dma_addr; 598 599 tf->page = alloc_page(GFP_KERNEL); 600 if (!tf->page) { 601 tbnet_free_buffers(ring); 602 return -ENOMEM; 603 } 604 605 dma_addr = dma_map_page(dma_dev, tf->page, 0, TBNET_FRAME_SIZE, 606 DMA_TO_DEVICE); 607 if (dma_mapping_error(dma_dev, dma_addr)) { 608 __free_page(tf->page); 609 tf->page = NULL; 610 tbnet_free_buffers(ring); 611 return -ENOMEM; 612 } 613 614 tf->dev = net->dev; 615 tf->frame.buffer_phy = dma_addr; 616 tf->frame.callback = tbnet_tx_callback; 617 tf->frame.sof = TBIP_PDF_FRAME_START; 618 tf->frame.eof = TBIP_PDF_FRAME_END; 619 620 trace_tbnet_alloc_tx_frame(i, tf->page, dma_addr, DMA_TO_DEVICE); 621 } 622 623 ring->cons = 0; 624 ring->prod = TBNET_RING_SIZE - 1; 625 626 return 0; 627 } 628 629 static void tbnet_connect_failed(struct tbnet *net) 630 { 631 /* Leave login_received set: only the peer can make it true again. */ 632 mutex_lock(&net->connection_lock); 633 net->login_sent = false; 634 mutex_unlock(&net->connection_lock); 635 } 636 637 static void tbnet_connected_work(struct work_struct *work) 638 { 639 struct tbnet *net = container_of(work, typeof(*net), connected_work); 640 bool connected; 641 int ret; 642 643 if (netif_carrier_ok(net->dev)) 644 return; 645 646 mutex_lock(&net->connection_lock); 647 connected = net->login_sent && net->login_received; 648 mutex_unlock(&net->connection_lock); 649 650 if (!connected) 651 return; 652 653 netdev_dbg(net->dev, "login successful, enabling paths\n"); 654 655 ret = tb_xdomain_alloc_in_hopid(net->xd, net->remote_transmit_path); 656 if (ret != net->remote_transmit_path) { 657 netdev_err(net->dev, "failed to allocate Rx HopID\n"); 658 if (ret >= 0) 659 tb_xdomain_release_in_hopid(net->xd, ret); 660 tbnet_connect_failed(net); 661 return; 662 } 663 664 /* Both logins successful so enable the rings, high-speed DMA 665 * paths and start the network device queue. 666 * 667 * Note we enable the DMA paths last to make sure we have primed 668 * the Rx ring before any incoming packets are allowed to 669 * arrive. 670 */ 671 tb_ring_start(net->tx_ring.ring); 672 tb_ring_start(net->rx_ring.ring); 673 674 ret = tbnet_alloc_rx_buffers(net, TBNET_RING_SIZE); 675 if (ret) 676 goto err_stop_rings; 677 678 ret = tbnet_alloc_tx_buffers(net); 679 if (ret) 680 goto err_free_rx_buffers; 681 682 ret = tb_xdomain_enable_paths(net->xd, net->local_transmit_path, 683 net->tx_ring.ring->hop, 684 net->remote_transmit_path, 685 net->rx_ring.ring->hop); 686 if (ret) { 687 netdev_err(net->dev, "failed to enable DMA paths\n"); 688 goto err_free_tx_buffers; 689 } 690 691 netif_carrier_on(net->dev); 692 netif_start_queue(net->dev); 693 694 netdev_dbg(net->dev, "network traffic started\n"); 695 return; 696 697 err_free_tx_buffers: 698 tbnet_free_buffers(&net->tx_ring); 699 err_free_rx_buffers: 700 tbnet_free_buffers(&net->rx_ring); 701 err_stop_rings: 702 tb_ring_stop(net->rx_ring.ring); 703 tb_ring_stop(net->tx_ring.ring); 704 tb_xdomain_release_in_hopid(net->xd, net->remote_transmit_path); 705 tbnet_connect_failed(net); 706 } 707 708 static void tbnet_login_work(struct work_struct *work) 709 { 710 struct tbnet *net = container_of(work, typeof(*net), login_work.work); 711 unsigned long delay = msecs_to_jiffies(TBNET_LOGIN_DELAY); 712 int ret; 713 714 if (netif_carrier_ok(net->dev)) 715 return; 716 717 netdev_dbg(net->dev, "sending login request, retries=%u\n", 718 net->login_retries); 719 720 ret = tbnet_login_request(net, net->login_retries % 4); 721 if (ret) { 722 netdev_dbg(net->dev, "sending login request failed, ret=%d\n", 723 ret); 724 if (net->login_retries++ < TBNET_LOGIN_RETRIES) { 725 queue_delayed_work(system_long_wq, &net->login_work, 726 delay); 727 } else { 728 netdev_info(net->dev, "ThunderboltIP login timed out\n"); 729 } 730 } else { 731 netdev_dbg(net->dev, "received login reply\n"); 732 733 net->login_retries = 0; 734 735 mutex_lock(&net->connection_lock); 736 net->login_sent = true; 737 mutex_unlock(&net->connection_lock); 738 739 queue_work(system_long_wq, &net->connected_work); 740 } 741 } 742 743 static void tbnet_disconnect_work(struct work_struct *work) 744 { 745 struct tbnet *net = container_of(work, typeof(*net), disconnect_work); 746 747 tbnet_tear_down(net, false); 748 } 749 750 static bool tbnet_check_frame(struct tbnet *net, const struct tbnet_frame *tf, 751 const struct thunderbolt_ip_frame_header *hdr) 752 { 753 u32 frame_id, frame_count, frame_size, frame_index; 754 unsigned int size; 755 756 if (tf->frame.flags & RING_DESC_CRC_ERROR) { 757 net->stats.rx_crc_errors++; 758 return false; 759 } else if (tf->frame.flags & RING_DESC_BUFFER_OVERRUN) { 760 net->stats.rx_over_errors++; 761 return false; 762 } 763 764 /* Should be greater than just header i.e. contains data */ 765 size = tb_ring_frame_size(&tf->frame); 766 if (size <= sizeof(*hdr)) { 767 net->stats.rx_length_errors++; 768 return false; 769 } 770 771 frame_count = le32_to_cpu(hdr->frame_count); 772 frame_size = le32_to_cpu(hdr->frame_size); 773 frame_index = le16_to_cpu(hdr->frame_index); 774 frame_id = le16_to_cpu(hdr->frame_id); 775 776 if ((frame_size > size - sizeof(*hdr)) || !frame_size) { 777 net->stats.rx_length_errors++; 778 return false; 779 } 780 781 /* In case we're in the middle of packet, validate the frame 782 * header based on first fragment of the packet. 783 */ 784 if (net->skb && net->rx_hdr.frame_count) { 785 /* Check the frame count fits the count field */ 786 if (frame_count != le32_to_cpu(net->rx_hdr.frame_count)) { 787 net->stats.rx_length_errors++; 788 return false; 789 } 790 791 /* Check the frame identifiers are incremented correctly, 792 * and id is matching. 793 */ 794 if (frame_index != le16_to_cpu(net->rx_hdr.frame_index) + 1 || 795 frame_id != le16_to_cpu(net->rx_hdr.frame_id)) { 796 net->stats.rx_missed_errors++; 797 return false; 798 } 799 800 if (net->skb->len + frame_size > TBNET_MAX_MTU) { 801 net->stats.rx_length_errors++; 802 return false; 803 } 804 805 return true; 806 } 807 808 /* Start of packet, validate the frame header. tbnet_poll() puts the 809 * first frame in the skb linear area and every further frame in a page 810 * fragment, so a packet may not span more than MAX_SKB_FRAGS + 1 frames 811 * without overflowing skb_shinfo()->frags[]. 812 */ 813 if (frame_count == 0 || frame_count > MAX_SKB_FRAGS + 1) { 814 net->stats.rx_length_errors++; 815 return false; 816 } 817 if (frame_index != 0) { 818 net->stats.rx_missed_errors++; 819 return false; 820 } 821 822 return true; 823 } 824 825 static int tbnet_poll(struct napi_struct *napi, int budget) 826 { 827 struct tbnet *net = container_of(napi, struct tbnet, napi); 828 unsigned int cleaned_count = tbnet_available_buffers(&net->rx_ring); 829 struct device *dma_dev = tb_ring_dma_device(net->rx_ring.ring); 830 unsigned int rx_packets = 0; 831 832 while (rx_packets < budget) { 833 const struct thunderbolt_ip_frame_header *hdr; 834 unsigned int hdr_size = sizeof(*hdr); 835 struct sk_buff *skb = NULL; 836 struct ring_frame *frame; 837 struct tbnet_frame *tf; 838 struct page *page; 839 bool last = true; 840 u32 frame_size; 841 842 /* Return some buffers to hardware, one at a time is too 843 * slow so allocate MAX_SKB_FRAGS buffers at the same 844 * time. 845 */ 846 if (cleaned_count >= MAX_SKB_FRAGS) { 847 tbnet_alloc_rx_buffers(net, cleaned_count); 848 cleaned_count = 0; 849 } 850 851 frame = tb_ring_poll(net->rx_ring.ring); 852 if (!frame) 853 break; 854 855 dma_unmap_page(dma_dev, frame->buffer_phy, 856 TBNET_RX_PAGE_SIZE, DMA_FROM_DEVICE); 857 858 tf = container_of(frame, typeof(*tf), frame); 859 860 page = tf->page; 861 tf->page = NULL; 862 net->rx_ring.cons++; 863 cleaned_count++; 864 865 hdr = page_address(page); 866 if (!tbnet_check_frame(net, tf, hdr)) { 867 trace_tbnet_invalid_rx_ip_frame(hdr->frame_size, 868 hdr->frame_id, hdr->frame_index, hdr->frame_count); 869 __free_pages(page, TBNET_RX_PAGE_ORDER); 870 dev_kfree_skb_any(net->skb); 871 net->skb = NULL; 872 continue; 873 } 874 875 trace_tbnet_rx_ip_frame(hdr->frame_size, hdr->frame_id, 876 hdr->frame_index, hdr->frame_count); 877 frame_size = le32_to_cpu(hdr->frame_size); 878 879 skb = net->skb; 880 if (!skb) { 881 skb = build_skb(page_address(page), 882 TBNET_RX_PAGE_SIZE); 883 if (!skb) { 884 __free_pages(page, TBNET_RX_PAGE_ORDER); 885 net->stats.rx_errors++; 886 break; 887 } 888 889 skb_reserve(skb, hdr_size); 890 skb_put(skb, frame_size); 891 892 net->skb = skb; 893 } else { 894 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 895 page, hdr_size, frame_size, 896 TBNET_RX_PAGE_SIZE - hdr_size); 897 } 898 899 net->rx_hdr.frame_size = hdr->frame_size; 900 net->rx_hdr.frame_count = hdr->frame_count; 901 net->rx_hdr.frame_index = hdr->frame_index; 902 net->rx_hdr.frame_id = hdr->frame_id; 903 last = le16_to_cpu(net->rx_hdr.frame_index) == 904 le32_to_cpu(net->rx_hdr.frame_count) - 1; 905 906 rx_packets++; 907 net->stats.rx_bytes += frame_size; 908 909 if (last) { 910 skb->protocol = eth_type_trans(skb, net->dev); 911 trace_tbnet_rx_skb(skb); 912 napi_gro_receive(&net->napi, skb); 913 net->skb = NULL; 914 } 915 } 916 917 net->stats.rx_packets += rx_packets; 918 919 if (cleaned_count) 920 tbnet_alloc_rx_buffers(net, cleaned_count); 921 922 if (rx_packets >= budget) 923 return budget; 924 925 napi_complete_done(napi, rx_packets); 926 /* Re-enable the ring interrupt */ 927 tb_ring_poll_complete(net->rx_ring.ring); 928 929 return rx_packets; 930 } 931 932 static void tbnet_start_poll(void *data) 933 { 934 struct tbnet *net = data; 935 936 napi_schedule(&net->napi); 937 } 938 939 static int tbnet_open(struct net_device *dev) 940 { 941 struct tbnet *net = netdev_priv(dev); 942 struct tb_xdomain *xd = net->xd; 943 u16 sof_mask, eof_mask; 944 struct tb_ring *ring; 945 unsigned int flags; 946 int hopid; 947 948 netif_carrier_off(dev); 949 950 ring = tb_ring_alloc_tx(xd->tb->nhi, -1, TBNET_RING_SIZE, 951 RING_FLAG_FRAME); 952 if (!ring) { 953 netdev_err(dev, "failed to allocate Tx ring\n"); 954 return -ENOMEM; 955 } 956 net->tx_ring.ring = ring; 957 958 hopid = tb_xdomain_alloc_out_hopid(xd, -1); 959 if (hopid < 0) { 960 netdev_err(dev, "failed to allocate Tx HopID\n"); 961 tb_ring_free(net->tx_ring.ring); 962 net->tx_ring.ring = NULL; 963 return hopid; 964 } 965 net->local_transmit_path = hopid; 966 967 sof_mask = BIT(TBIP_PDF_FRAME_START); 968 eof_mask = BIT(TBIP_PDF_FRAME_END); 969 970 flags = RING_FLAG_FRAME; 971 /* Only enable full E2E if the other end supports it too */ 972 if (tbnet_e2e && net->svc->prtcstns & TBNET_E2E) 973 flags |= RING_FLAG_E2E; 974 975 ring = tb_ring_alloc_rx(xd->tb->nhi, -1, TBNET_RING_SIZE, flags, 976 net->tx_ring.ring->hop, sof_mask, 977 eof_mask, tbnet_start_poll, net); 978 if (!ring) { 979 netdev_err(dev, "failed to allocate Rx ring\n"); 980 tb_xdomain_release_out_hopid(xd, hopid); 981 tb_ring_free(net->tx_ring.ring); 982 net->tx_ring.ring = NULL; 983 return -ENOMEM; 984 } 985 net->rx_ring.ring = ring; 986 987 tb_ring_throttling(net->tx_ring.ring, TBNET_THROTTLING); 988 tb_ring_throttling(net->rx_ring.ring, TBNET_THROTTLING); 989 990 napi_enable(&net->napi); 991 start_login(net); 992 993 return 0; 994 } 995 996 static int tbnet_stop(struct net_device *dev) 997 { 998 struct tbnet *net = netdev_priv(dev); 999 1000 napi_disable(&net->napi); 1001 1002 cancel_work_sync(&net->disconnect_work); 1003 tbnet_tear_down(net, true); 1004 1005 tb_ring_free(net->rx_ring.ring); 1006 net->rx_ring.ring = NULL; 1007 1008 tb_xdomain_release_out_hopid(net->xd, net->local_transmit_path); 1009 tb_ring_free(net->tx_ring.ring); 1010 net->tx_ring.ring = NULL; 1011 1012 return 0; 1013 } 1014 1015 static bool tbnet_xmit_csum_and_map(struct tbnet *net, struct sk_buff *skb, 1016 struct tbnet_frame **frames, u32 frame_count) 1017 { 1018 struct thunderbolt_ip_frame_header *hdr = page_address(frames[0]->page); 1019 struct device *dma_dev = tb_ring_dma_device(net->tx_ring.ring); 1020 unsigned int i, len, offset = skb_transport_offset(skb); 1021 /* Remove payload length from checksum */ 1022 u32 paylen = skb->len - skb_transport_offset(skb); 1023 __wsum wsum = (__force __wsum)htonl(paylen); 1024 __be16 protocol = skb->protocol; 1025 void *data = skb->data; 1026 void *dest = hdr + 1; 1027 __sum16 *tucso; 1028 1029 if (skb->ip_summed != CHECKSUM_PARTIAL) { 1030 /* No need to calculate checksum so we just update the 1031 * total frame count and sync the frames for DMA. 1032 */ 1033 for (i = 0; i < frame_count; i++) { 1034 hdr = page_address(frames[i]->page); 1035 hdr->frame_count = cpu_to_le32(frame_count); 1036 trace_tbnet_tx_ip_frame(hdr->frame_size, hdr->frame_id, 1037 hdr->frame_index, hdr->frame_count); 1038 dma_sync_single_for_device(dma_dev, 1039 frames[i]->frame.buffer_phy, 1040 tb_ring_frame_size(&frames[i]->frame), 1041 DMA_TO_DEVICE); 1042 } 1043 1044 return true; 1045 } 1046 1047 if (protocol == htons(ETH_P_8021Q)) { 1048 struct vlan_hdr *vhdr, vh; 1049 1050 vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(vh), &vh); 1051 if (!vhdr) 1052 return false; 1053 1054 protocol = vhdr->h_vlan_encapsulated_proto; 1055 } 1056 1057 /* Data points on the beginning of packet. 1058 * Check is the checksum absolute place in the packet. 1059 * ipcso will update IP checksum. 1060 * tucso will update TCP/UDP checksum. 1061 */ 1062 if (protocol == htons(ETH_P_IP)) { 1063 __sum16 *ipcso = dest + ((void *)&(ip_hdr(skb)->check) - data); 1064 1065 *ipcso = 0; 1066 *ipcso = ip_fast_csum(dest + skb_network_offset(skb), 1067 ip_hdr(skb)->ihl); 1068 1069 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 1070 tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data); 1071 else if (ip_hdr(skb)->protocol == IPPROTO_UDP) 1072 tucso = dest + ((void *)&(udp_hdr(skb)->check) - data); 1073 else 1074 return false; 1075 1076 *tucso = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 1077 ip_hdr(skb)->daddr, 0, 1078 ip_hdr(skb)->protocol, 0); 1079 } else if (skb_is_gso(skb) && skb_is_gso_v6(skb)) { 1080 tucso = dest + ((void *)&(tcp_hdr(skb)->check) - data); 1081 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 1082 &ipv6_hdr(skb)->daddr, 0, 1083 IPPROTO_TCP, 0); 1084 } else if (protocol == htons(ETH_P_IPV6)) { 1085 tucso = dest + skb_checksum_start_offset(skb) + skb->csum_offset; 1086 *tucso = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 1087 &ipv6_hdr(skb)->daddr, 0, 1088 ipv6_hdr(skb)->nexthdr, 0); 1089 } else { 1090 return false; 1091 } 1092 1093 /* First frame was headers, rest of the frames contain data. 1094 * Calculate checksum over each frame. 1095 */ 1096 for (i = 0; i < frame_count; i++) { 1097 hdr = page_address(frames[i]->page); 1098 dest = (void *)(hdr + 1) + offset; 1099 len = le32_to_cpu(hdr->frame_size) - offset; 1100 wsum = csum_partial(dest, len, wsum); 1101 hdr->frame_count = cpu_to_le32(frame_count); 1102 trace_tbnet_tx_ip_frame(hdr->frame_size, hdr->frame_id, 1103 hdr->frame_index, hdr->frame_count); 1104 1105 offset = 0; 1106 } 1107 1108 *tucso = csum_fold(wsum); 1109 1110 /* Checksum is finally calculated and we don't touch the memory 1111 * anymore, so DMA sync the frames now. 1112 */ 1113 for (i = 0; i < frame_count; i++) { 1114 dma_sync_single_for_device(dma_dev, frames[i]->frame.buffer_phy, 1115 tb_ring_frame_size(&frames[i]->frame), DMA_TO_DEVICE); 1116 } 1117 1118 return true; 1119 } 1120 1121 static void *tbnet_kmap_frag(struct sk_buff *skb, unsigned int frag_num, 1122 unsigned int *len) 1123 { 1124 const skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_num]; 1125 1126 *len = skb_frag_size(frag); 1127 return kmap_local_page(skb_frag_page(frag)) + skb_frag_off(frag); 1128 } 1129 1130 static netdev_tx_t tbnet_start_xmit(struct sk_buff *skb, 1131 struct net_device *dev) 1132 { 1133 struct tbnet *net = netdev_priv(dev); 1134 struct tbnet_frame *frames[MAX_SKB_FRAGS]; 1135 u16 frame_id = atomic_read(&net->frame_id); 1136 struct thunderbolt_ip_frame_header *hdr; 1137 unsigned int len = skb_headlen(skb); 1138 unsigned int data_len = skb->len; 1139 unsigned int nframes, i; 1140 unsigned int frag = 0; 1141 void *src = skb->data; 1142 u32 frame_index = 0; 1143 bool unmap = false; 1144 void *dest; 1145 1146 trace_tbnet_tx_skb(skb); 1147 1148 nframes = DIV_ROUND_UP(data_len, TBNET_MAX_PAYLOAD_SIZE); 1149 if (tbnet_available_buffers(&net->tx_ring) < nframes) { 1150 netif_stop_queue(net->dev); 1151 return NETDEV_TX_BUSY; 1152 } 1153 1154 frames[frame_index] = tbnet_get_tx_buffer(net); 1155 if (!frames[frame_index]) 1156 goto err_drop; 1157 1158 hdr = page_address(frames[frame_index]->page); 1159 dest = hdr + 1; 1160 1161 /* If overall packet is bigger than the frame data size */ 1162 while (data_len > TBNET_MAX_PAYLOAD_SIZE) { 1163 unsigned int size_left = TBNET_MAX_PAYLOAD_SIZE; 1164 1165 hdr->frame_size = cpu_to_le32(TBNET_MAX_PAYLOAD_SIZE); 1166 hdr->frame_index = cpu_to_le16(frame_index); 1167 hdr->frame_id = cpu_to_le16(frame_id); 1168 1169 do { 1170 if (len > size_left) { 1171 /* Copy data onto Tx buffer data with 1172 * full frame size then break and go to 1173 * next frame 1174 */ 1175 memcpy(dest, src, size_left); 1176 len -= size_left; 1177 dest += size_left; 1178 src += size_left; 1179 break; 1180 } 1181 1182 memcpy(dest, src, len); 1183 size_left -= len; 1184 dest += len; 1185 1186 if (unmap) { 1187 kunmap_local(src); 1188 unmap = false; 1189 } 1190 1191 /* Ensure all fragments have been processed */ 1192 if (frag < skb_shinfo(skb)->nr_frags) { 1193 /* Map and then unmap quickly */ 1194 src = tbnet_kmap_frag(skb, frag++, &len); 1195 unmap = true; 1196 } else if (unlikely(size_left > 0)) { 1197 goto err_drop; 1198 } 1199 } while (size_left > 0); 1200 1201 data_len -= TBNET_MAX_PAYLOAD_SIZE; 1202 frame_index++; 1203 1204 frames[frame_index] = tbnet_get_tx_buffer(net); 1205 if (!frames[frame_index]) 1206 goto err_drop; 1207 1208 hdr = page_address(frames[frame_index]->page); 1209 dest = hdr + 1; 1210 } 1211 1212 hdr->frame_size = cpu_to_le32(data_len); 1213 hdr->frame_index = cpu_to_le16(frame_index); 1214 hdr->frame_id = cpu_to_le16(frame_id); 1215 1216 frames[frame_index]->frame.size = data_len + sizeof(*hdr); 1217 1218 /* In case the remaining data_len is smaller than a frame */ 1219 while (len < data_len) { 1220 memcpy(dest, src, len); 1221 data_len -= len; 1222 dest += len; 1223 1224 if (unmap) { 1225 kunmap_local(src); 1226 unmap = false; 1227 } 1228 1229 if (frag < skb_shinfo(skb)->nr_frags) { 1230 src = tbnet_kmap_frag(skb, frag++, &len); 1231 unmap = true; 1232 } else if (unlikely(data_len > 0)) { 1233 goto err_drop; 1234 } 1235 } 1236 1237 memcpy(dest, src, data_len); 1238 1239 if (unmap) 1240 kunmap_local(src); 1241 1242 if (!tbnet_xmit_csum_and_map(net, skb, frames, frame_index + 1)) 1243 goto err_drop; 1244 1245 for (i = 0; i < frame_index + 1; i++) 1246 tb_ring_tx(net->tx_ring.ring, &frames[i]->frame); 1247 1248 if (net->svc->prtcstns & TBNET_MATCH_FRAGS_ID) 1249 atomic_inc(&net->frame_id); 1250 1251 net->stats.tx_packets++; 1252 net->stats.tx_bytes += skb->len; 1253 1254 trace_tbnet_consume_skb(skb); 1255 dev_consume_skb_any(skb); 1256 1257 return NETDEV_TX_OK; 1258 1259 err_drop: 1260 /* We can re-use the buffers */ 1261 net->tx_ring.cons -= frame_index; 1262 1263 dev_kfree_skb_any(skb); 1264 net->stats.tx_errors++; 1265 1266 return NETDEV_TX_OK; 1267 } 1268 1269 static void tbnet_get_stats64(struct net_device *dev, 1270 struct rtnl_link_stats64 *stats) 1271 { 1272 struct tbnet *net = netdev_priv(dev); 1273 1274 stats->tx_packets = net->stats.tx_packets; 1275 stats->rx_packets = net->stats.rx_packets; 1276 stats->tx_bytes = net->stats.tx_bytes; 1277 stats->rx_bytes = net->stats.rx_bytes; 1278 stats->rx_errors = net->stats.rx_errors + net->stats.rx_length_errors + 1279 net->stats.rx_over_errors + net->stats.rx_crc_errors + 1280 net->stats.rx_missed_errors; 1281 stats->tx_errors = net->stats.tx_errors; 1282 stats->rx_length_errors = net->stats.rx_length_errors; 1283 stats->rx_over_errors = net->stats.rx_over_errors; 1284 stats->rx_crc_errors = net->stats.rx_crc_errors; 1285 stats->rx_missed_errors = net->stats.rx_missed_errors; 1286 } 1287 1288 static const struct net_device_ops tbnet_netdev_ops = { 1289 .ndo_open = tbnet_open, 1290 .ndo_stop = tbnet_stop, 1291 .ndo_start_xmit = tbnet_start_xmit, 1292 .ndo_set_mac_address = eth_mac_addr, 1293 .ndo_get_stats64 = tbnet_get_stats64, 1294 }; 1295 1296 static int tbnet_get_link_ksettings(struct net_device *dev, 1297 struct ethtool_link_ksettings *cmd) 1298 { 1299 const struct tbnet *net = netdev_priv(dev); 1300 const struct tb_xdomain *xd = net->xd; 1301 int speed; 1302 1303 ethtool_link_ksettings_zero_link_mode(cmd, supported); 1304 ethtool_link_ksettings_zero_link_mode(cmd, advertising); 1305 1306 /* Figure out the current link speed and width */ 1307 switch (xd->link_speed) { 1308 case 40: 1309 speed = SPEED_80000; 1310 break; 1311 1312 case 20: 1313 if (xd->link_width == 2) 1314 speed = SPEED_40000; 1315 else 1316 speed = SPEED_20000; 1317 break; 1318 1319 case 10: 1320 if (xd->link_width == 2) { 1321 speed = SPEED_20000; 1322 break; 1323 } 1324 fallthrough; 1325 1326 default: 1327 speed = SPEED_10000; 1328 break; 1329 } 1330 1331 cmd->base.speed = speed; 1332 cmd->base.duplex = DUPLEX_FULL; 1333 cmd->base.autoneg = AUTONEG_DISABLE; 1334 cmd->base.port = PORT_OTHER; 1335 1336 return 0; 1337 } 1338 1339 static const struct ethtool_ops tbnet_ethtool_ops = { 1340 .get_link_ksettings = tbnet_get_link_ksettings, 1341 }; 1342 1343 static void tbnet_generate_mac(struct net_device *dev) 1344 { 1345 const struct tbnet *net = netdev_priv(dev); 1346 const struct tb_xdomain *xd = net->xd; 1347 u8 addr[ETH_ALEN]; 1348 u8 phy_port; 1349 u32 hash; 1350 1351 phy_port = tb_phy_port_from_link(TBNET_L0_PORT_NUM(xd->route)); 1352 1353 /* Unicast and locally administered MAC */ 1354 addr[0] = phy_port << 4 | 0x02; 1355 hash = jhash2((u32 *)xd->local_uuid, 4, 0); 1356 memcpy(addr + 1, &hash, sizeof(hash)); 1357 hash = jhash2((u32 *)xd->local_uuid, 4, hash); 1358 addr[5] = hash & 0xff; 1359 eth_hw_addr_set(dev, addr); 1360 1361 /* Allow changing it if needed */ 1362 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1363 } 1364 1365 static int tbnet_probe(struct tb_service *svc, const struct tb_service_id *id) 1366 { 1367 struct tb_xdomain *xd = tb_service_parent(svc); 1368 struct net_device *dev; 1369 struct tbnet *net; 1370 int ret; 1371 1372 dev = alloc_etherdev(sizeof(*net)); 1373 if (!dev) 1374 return -ENOMEM; 1375 1376 SET_NETDEV_DEV(dev, &svc->dev); 1377 1378 net = netdev_priv(dev); 1379 INIT_DELAYED_WORK(&net->login_work, tbnet_login_work); 1380 INIT_WORK(&net->connected_work, tbnet_connected_work); 1381 INIT_WORK(&net->disconnect_work, tbnet_disconnect_work); 1382 mutex_init(&net->connection_lock); 1383 atomic_set(&net->command_id, 0); 1384 atomic_set(&net->frame_id, 0); 1385 net->svc = svc; 1386 net->dev = dev; 1387 net->xd = xd; 1388 1389 tbnet_generate_mac(dev); 1390 1391 strcpy(dev->name, "thunderbolt%d"); 1392 dev->netdev_ops = &tbnet_netdev_ops; 1393 dev->ethtool_ops = &tbnet_ethtool_ops; 1394 1395 /* ThunderboltIP takes advantage of TSO packets but instead of 1396 * segmenting them we just split the packet into Thunderbolt 1397 * frames (maximum payload size of each frame is 4084 bytes) and 1398 * calculate checksum over the whole packet here. 1399 * 1400 * The receiving side does the opposite if the host OS supports 1401 * LRO, otherwise it needs to split the large packet into MTU 1402 * sized smaller packets. 1403 * 1404 * In order to receive large packets from the networking stack, 1405 * we need to announce support for most of the offloading 1406 * features here. 1407 */ 1408 dev->hw_features = NETIF_F_SG | NETIF_F_ALL_TSO | NETIF_F_GRO | 1409 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 1410 dev->features = dev->hw_features | NETIF_F_HIGHDMA; 1411 dev->hard_header_len += sizeof(struct thunderbolt_ip_frame_header); 1412 1413 netif_napi_add(dev, &net->napi, tbnet_poll); 1414 1415 /* MTU range: 68 - 65522 */ 1416 dev->min_mtu = ETH_MIN_MTU; 1417 dev->max_mtu = TBNET_MAX_MTU - ETH_HLEN; 1418 1419 net->handler.uuid = &tbnet_svc_uuid; 1420 net->handler.callback = tbnet_handle_packet; 1421 net->handler.data = net; 1422 tb_register_protocol_handler(&net->handler); 1423 1424 tb_service_set_drvdata(svc, net); 1425 1426 ret = register_netdev(dev); 1427 if (ret) { 1428 tb_unregister_protocol_handler(&net->handler); 1429 free_netdev(dev); 1430 return ret; 1431 } 1432 1433 return 0; 1434 } 1435 1436 static void tbnet_remove(struct tb_service *svc) 1437 { 1438 struct tbnet *net = tb_service_get_drvdata(svc); 1439 1440 unregister_netdev(net->dev); 1441 tb_unregister_protocol_handler(&net->handler); 1442 free_netdev(net->dev); 1443 } 1444 1445 static void tbnet_shutdown(struct tb_service *svc) 1446 { 1447 tbnet_tear_down(tb_service_get_drvdata(svc), true); 1448 } 1449 1450 static int tbnet_suspend(struct device *dev) 1451 { 1452 struct tb_service *svc = tb_to_service(dev); 1453 struct tbnet *net = tb_service_get_drvdata(svc); 1454 1455 stop_login(net); 1456 if (netif_running(net->dev)) { 1457 netif_device_detach(net->dev); 1458 tbnet_tear_down(net, true); 1459 } 1460 1461 tb_unregister_protocol_handler(&net->handler); 1462 return 0; 1463 } 1464 1465 static int tbnet_resume(struct device *dev) 1466 { 1467 struct tb_service *svc = tb_to_service(dev); 1468 struct tbnet *net = tb_service_get_drvdata(svc); 1469 1470 tb_register_protocol_handler(&net->handler); 1471 1472 netif_carrier_off(net->dev); 1473 if (netif_running(net->dev)) { 1474 netif_device_attach(net->dev); 1475 start_login(net); 1476 } 1477 1478 return 0; 1479 } 1480 1481 static DEFINE_SIMPLE_DEV_PM_OPS(tbnet_pm_ops, tbnet_suspend, tbnet_resume); 1482 1483 static const struct tb_service_id tbnet_ids[] = { 1484 { TB_SERVICE("network", 1) }, 1485 { }, 1486 }; 1487 MODULE_DEVICE_TABLE(tbsvc, tbnet_ids); 1488 1489 static struct tb_service_driver tbnet_driver = { 1490 .driver = { 1491 .owner = THIS_MODULE, 1492 .name = "thunderbolt-net", 1493 .pm = pm_sleep_ptr(&tbnet_pm_ops), 1494 }, 1495 .probe = tbnet_probe, 1496 .remove = tbnet_remove, 1497 .shutdown = tbnet_shutdown, 1498 .id_table = tbnet_ids, 1499 }; 1500 1501 static int __init tbnet_init(void) 1502 { 1503 unsigned int flags; 1504 int ret; 1505 1506 tbnet_dir = tb_property_create_dir(&tbnet_dir_uuid); 1507 if (!tbnet_dir) 1508 return -ENOMEM; 1509 1510 tb_property_add_immediate(tbnet_dir, "prtcid", 1); 1511 tb_property_add_immediate(tbnet_dir, "prtcvers", 1); 1512 tb_property_add_immediate(tbnet_dir, "prtcrevs", 1); 1513 1514 flags = TBNET_MATCH_FRAGS_ID | TBNET_64K_FRAMES; 1515 if (tbnet_e2e) 1516 flags |= TBNET_E2E; 1517 tb_property_add_immediate(tbnet_dir, "prtcstns", flags); 1518 1519 ret = tb_register_property_dir("network", tbnet_dir); 1520 if (ret) 1521 goto err_free_dir; 1522 1523 ret = tb_register_service_driver(&tbnet_driver); 1524 if (ret) 1525 goto err_unregister; 1526 1527 return 0; 1528 1529 err_unregister: 1530 tb_unregister_property_dir("network", tbnet_dir); 1531 err_free_dir: 1532 tb_property_free_dir(tbnet_dir); 1533 1534 return ret; 1535 } 1536 module_init(tbnet_init); 1537 1538 static void __exit tbnet_exit(void) 1539 { 1540 tb_unregister_service_driver(&tbnet_driver); 1541 tb_unregister_property_dir("network", tbnet_dir); 1542 tb_property_free_dir(tbnet_dir); 1543 } 1544 module_exit(tbnet_exit); 1545 1546 MODULE_AUTHOR("Amir Levy <amir.jer.levy@intel.com>"); 1547 MODULE_AUTHOR("Michael Jamet <michael.jamet@intel.com>"); 1548 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 1549 MODULE_DESCRIPTION("Thunderbolt/USB4 network driver"); 1550 MODULE_LICENSE("GPL v2"); 1551