1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Management Controller Transport Protocol (MCTP) 4 * Implements DMTF specification 5 * "DSP0237 Management Component Transport Protocol (MCTP) SMBus/I2C 6 * Transport Binding" 7 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0237_1.2.0.pdf 8 * 9 * A netdev is created for each I2C bus that handles MCTP. In the case of an I2C 10 * mux topology a single I2C client is attached to the root of the mux topology, 11 * shared between all mux I2C busses underneath. For non-mux cases an I2C client 12 * is attached per netdev. 13 * 14 * mctp-i2c-controller.yml devicetree binding has further details. 15 * 16 * Copyright (c) 2022 Code Construct 17 * Copyright (c) 2022 Google 18 */ 19 20 #include <linux/module.h> 21 #include <linux/netdevice.h> 22 #include <linux/i2c.h> 23 #include <linux/i2c-mux.h> 24 #include <linux/if_arp.h> 25 #include <net/mctp.h> 26 #include <net/mctpdevice.h> 27 28 /* byte_count is limited to u8 */ 29 #define MCTP_I2C_MAXBLOCK 255 30 /* One byte is taken by source_slave */ 31 #define MCTP_I2C_MAXMTU (MCTP_I2C_MAXBLOCK - 1) 32 #define MCTP_I2C_MINMTU (64 + 4) 33 /* Allow space for dest_address, command, byte_count, data, PEC */ 34 #define MCTP_I2C_BUFSZ (3 + MCTP_I2C_MAXBLOCK + 1) 35 #define MCTP_I2C_MINLEN 8 36 #define MCTP_I2C_COMMANDCODE 0x0f 37 #define MCTP_I2C_TX_WORK_LEN 100 38 /* Sufficient for 64kB at min mtu */ 39 #define MCTP_I2C_TX_QUEUE_LEN 1100 40 41 #define MCTP_I2C_OF_PROP "mctp-controller" 42 43 enum { 44 MCTP_I2C_FLOW_STATE_NEW = 0, 45 MCTP_I2C_FLOW_STATE_ACTIVE, 46 MCTP_I2C_FLOW_STATE_INVALID, 47 }; 48 49 /* List of all struct mctp_i2c_client 50 * Lock protects driver_clients and also prevents adding/removing adapters 51 * during mctp_i2c_client probe/remove. 52 */ 53 static DEFINE_MUTEX(driver_clients_lock); 54 static LIST_HEAD(driver_clients); 55 56 struct mctp_i2c_client; 57 58 /* The netdev structure. One of these per I2C adapter. */ 59 struct mctp_i2c_dev { 60 struct net_device *ndev; 61 struct i2c_adapter *adapter; 62 struct mctp_i2c_client *client; 63 struct list_head list; /* For mctp_i2c_client.devs */ 64 65 size_t rx_pos; 66 u8 rx_buffer[MCTP_I2C_BUFSZ]; 67 struct completion rx_done; 68 69 struct task_struct *tx_thread; 70 wait_queue_head_t tx_wq; 71 struct sk_buff_head tx_queue; 72 u8 tx_scratch[MCTP_I2C_BUFSZ]; 73 74 /* A fake entry in our tx queue to perform an unlock operation */ 75 struct sk_buff unlock_marker; 76 77 /* Spinlock protects i2c_lock_count, release_count, allow_rx */ 78 spinlock_t lock; 79 int i2c_lock_count; 80 int release_count; 81 /* Indicates that the netif is ready to receive incoming packets */ 82 bool allow_rx; 83 84 }; 85 86 /* The i2c client structure. One per hardware i2c bus at the top of the 87 * mux tree, shared by multiple netdevs 88 */ 89 struct mctp_i2c_client { 90 struct i2c_client *client; 91 u8 lladdr; 92 93 struct mctp_i2c_dev *sel; 94 struct list_head devs; 95 spinlock_t sel_lock; /* Protects sel and devs */ 96 97 struct list_head list; /* For driver_clients */ 98 }; 99 100 /* Header on the wire. */ 101 struct mctp_i2c_hdr { 102 u8 dest_slave; 103 u8 command; 104 /* Count of bytes following byte_count, excluding PEC */ 105 u8 byte_count; 106 u8 source_slave; 107 }; 108 109 static int mctp_i2c_recv(struct mctp_i2c_dev *midev); 110 static int mctp_i2c_slave_cb(struct i2c_client *client, 111 enum i2c_slave_event event, u8 *val); 112 static void mctp_i2c_ndo_uninit(struct net_device *dev); 113 static int mctp_i2c_ndo_open(struct net_device *dev); 114 115 static struct i2c_adapter *mux_root_adapter(struct i2c_adapter *adap) 116 { 117 #if IS_ENABLED(CONFIG_I2C_MUX) 118 return i2c_root_adapter(&adap->dev); 119 #else 120 /* In non-mux config all i2c adapters are root adapters */ 121 return adap; 122 #endif 123 } 124 125 /* Creates a new i2c slave device attached to the root adapter. 126 * Sets up the slave callback. 127 * Must be called with a client on a root adapter. 128 */ 129 static struct mctp_i2c_client *mctp_i2c_new_client(struct i2c_client *client) 130 { 131 struct mctp_i2c_client *mcli = NULL; 132 struct i2c_adapter *root = NULL; 133 int rc; 134 135 if (client->flags & I2C_CLIENT_TEN) { 136 dev_err(&client->dev, "failed, MCTP requires a 7-bit I2C address, addr=0x%x\n", 137 client->addr); 138 rc = -EINVAL; 139 goto err; 140 } 141 142 root = mux_root_adapter(client->adapter); 143 if (!root) { 144 dev_err(&client->dev, "failed to find root adapter\n"); 145 rc = -ENOENT; 146 goto err; 147 } 148 if (root != client->adapter) { 149 dev_err(&client->dev, 150 "A mctp-i2c-controller client cannot be placed on an I2C mux adapter.\n" 151 " It should be placed on the mux tree root adapter\n" 152 " then set mctp-controller property on adapters to attach\n"); 153 rc = -EINVAL; 154 goto err; 155 } 156 157 mcli = kzalloc(sizeof(*mcli), GFP_KERNEL); 158 if (!mcli) { 159 rc = -ENOMEM; 160 goto err; 161 } 162 spin_lock_init(&mcli->sel_lock); 163 INIT_LIST_HEAD(&mcli->devs); 164 INIT_LIST_HEAD(&mcli->list); 165 mcli->lladdr = client->addr & 0xff; 166 mcli->client = client; 167 i2c_set_clientdata(client, mcli); 168 169 rc = i2c_slave_register(mcli->client, mctp_i2c_slave_cb); 170 if (rc < 0) { 171 dev_err(&client->dev, "i2c register failed %d\n", rc); 172 mcli->client = NULL; 173 i2c_set_clientdata(client, NULL); 174 goto err; 175 } 176 177 return mcli; 178 err: 179 if (mcli) { 180 i2c_unregister_device(mcli->client); 181 kfree(mcli); 182 } 183 return ERR_PTR(rc); 184 } 185 186 static void mctp_i2c_free_client(struct mctp_i2c_client *mcli) 187 { 188 int rc; 189 190 WARN_ON(!mutex_is_locked(&driver_clients_lock)); 191 WARN_ON(!list_empty(&mcli->devs)); 192 WARN_ON(mcli->sel); /* sanity check, no locking */ 193 194 rc = i2c_slave_unregister(mcli->client); 195 /* Leak if it fails, we can't propagate errors upwards */ 196 if (rc < 0) 197 dev_err(&mcli->client->dev, "i2c unregister failed %d\n", rc); 198 else 199 kfree(mcli); 200 } 201 202 /* Switch the mctp i2c device to receive responses. 203 * Call with sel_lock held 204 */ 205 static void __mctp_i2c_device_select(struct mctp_i2c_client *mcli, 206 struct mctp_i2c_dev *midev) 207 { 208 assert_spin_locked(&mcli->sel_lock); 209 if (midev) 210 dev_hold(midev->ndev); 211 if (mcli->sel) 212 dev_put(mcli->sel->ndev); 213 mcli->sel = midev; 214 } 215 216 /* Switch the mctp i2c device to receive responses */ 217 static void mctp_i2c_device_select(struct mctp_i2c_client *mcli, 218 struct mctp_i2c_dev *midev) 219 { 220 unsigned long flags; 221 222 spin_lock_irqsave(&mcli->sel_lock, flags); 223 __mctp_i2c_device_select(mcli, midev); 224 spin_unlock_irqrestore(&mcli->sel_lock, flags); 225 } 226 227 static int mctp_i2c_slave_cb(struct i2c_client *client, 228 enum i2c_slave_event event, u8 *val) 229 { 230 struct mctp_i2c_client *mcli = i2c_get_clientdata(client); 231 struct mctp_i2c_dev *midev = NULL; 232 unsigned long flags; 233 int rc = 0; 234 235 spin_lock_irqsave(&mcli->sel_lock, flags); 236 midev = mcli->sel; 237 if (midev) 238 dev_hold(midev->ndev); 239 spin_unlock_irqrestore(&mcli->sel_lock, flags); 240 241 if (!midev) 242 return 0; 243 244 switch (event) { 245 case I2C_SLAVE_WRITE_RECEIVED: 246 if (midev->rx_pos < MCTP_I2C_BUFSZ) { 247 midev->rx_buffer[midev->rx_pos] = *val; 248 midev->rx_pos++; 249 } else { 250 midev->ndev->stats.rx_over_errors++; 251 } 252 253 break; 254 case I2C_SLAVE_WRITE_REQUESTED: 255 /* dest_slave as first byte */ 256 midev->rx_buffer[0] = mcli->lladdr << 1; 257 midev->rx_pos = 1; 258 break; 259 case I2C_SLAVE_STOP: 260 rc = mctp_i2c_recv(midev); 261 break; 262 default: 263 break; 264 } 265 266 dev_put(midev->ndev); 267 return rc; 268 } 269 270 /* Processes incoming data that has been accumulated by the slave cb */ 271 static int mctp_i2c_recv(struct mctp_i2c_dev *midev) 272 { 273 struct net_device *ndev = midev->ndev; 274 struct mctp_i2c_hdr *hdr; 275 struct mctp_skb_cb *cb; 276 struct sk_buff *skb; 277 unsigned long flags; 278 u8 pec, calc_pec; 279 size_t recvlen; 280 int status; 281 282 /* + 1 for the PEC */ 283 if (midev->rx_pos < MCTP_I2C_MINLEN + 1) { 284 ndev->stats.rx_length_errors++; 285 return -EINVAL; 286 } 287 /* recvlen excludes PEC */ 288 recvlen = midev->rx_pos - 1; 289 290 hdr = (void *)midev->rx_buffer; 291 if (hdr->command != MCTP_I2C_COMMANDCODE) { 292 ndev->stats.rx_dropped++; 293 return -EINVAL; 294 } 295 296 if (hdr->byte_count + offsetof(struct mctp_i2c_hdr, source_slave) != recvlen) { 297 ndev->stats.rx_length_errors++; 298 return -EINVAL; 299 } 300 301 pec = midev->rx_buffer[midev->rx_pos - 1]; 302 calc_pec = i2c_smbus_pec(0, midev->rx_buffer, recvlen); 303 if (pec != calc_pec) { 304 ndev->stats.rx_crc_errors++; 305 return -EINVAL; 306 } 307 308 skb = netdev_alloc_skb(ndev, recvlen); 309 if (!skb) { 310 ndev->stats.rx_dropped++; 311 return -ENOMEM; 312 } 313 314 skb->protocol = htons(ETH_P_MCTP); 315 skb_put_data(skb, midev->rx_buffer, recvlen); 316 skb_reset_mac_header(skb); 317 skb_pull(skb, sizeof(struct mctp_i2c_hdr)); 318 skb_reset_network_header(skb); 319 320 cb = __mctp_cb(skb); 321 cb->halen = 1; 322 cb->haddr[0] = hdr->source_slave >> 1; 323 324 /* We need to ensure that the netif is not used once netdev 325 * unregister occurs 326 */ 327 spin_lock_irqsave(&midev->lock, flags); 328 if (midev->allow_rx) { 329 reinit_completion(&midev->rx_done); 330 spin_unlock_irqrestore(&midev->lock, flags); 331 332 status = netif_rx(skb); 333 complete(&midev->rx_done); 334 } else { 335 status = NET_RX_DROP; 336 spin_unlock_irqrestore(&midev->lock, flags); 337 } 338 339 if (status == NET_RX_SUCCESS) { 340 ndev->stats.rx_packets++; 341 ndev->stats.rx_bytes += recvlen; 342 } else { 343 ndev->stats.rx_dropped++; 344 } 345 return 0; 346 } 347 348 enum mctp_i2c_flow_state { 349 MCTP_I2C_TX_FLOW_INVALID, 350 MCTP_I2C_TX_FLOW_NONE, 351 MCTP_I2C_TX_FLOW_NEW, 352 MCTP_I2C_TX_FLOW_EXISTING, 353 }; 354 355 static enum mctp_i2c_flow_state 356 mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev *midev, struct sk_buff *skb) 357 { 358 enum mctp_i2c_flow_state state; 359 struct mctp_sk_key *key; 360 struct mctp_flow *flow; 361 unsigned long flags; 362 363 flow = skb_ext_find(skb, SKB_EXT_MCTP); 364 if (!flow) 365 return MCTP_I2C_TX_FLOW_NONE; 366 367 key = flow->key; 368 if (!key) 369 return MCTP_I2C_TX_FLOW_NONE; 370 371 spin_lock_irqsave(&key->lock, flags); 372 /* If the key is present but invalid, we're unlikely to be able 373 * to handle the flow at all; just drop now 374 */ 375 if (!key->valid) { 376 state = MCTP_I2C_TX_FLOW_INVALID; 377 } else { 378 switch (key->dev_flow_state) { 379 case MCTP_I2C_FLOW_STATE_NEW: 380 key->dev_flow_state = MCTP_I2C_FLOW_STATE_ACTIVE; 381 state = MCTP_I2C_TX_FLOW_NEW; 382 break; 383 case MCTP_I2C_FLOW_STATE_ACTIVE: 384 state = MCTP_I2C_TX_FLOW_EXISTING; 385 break; 386 default: 387 state = MCTP_I2C_TX_FLOW_INVALID; 388 } 389 } 390 391 spin_unlock_irqrestore(&key->lock, flags); 392 393 return state; 394 } 395 396 /* We're not contending with ourselves here; we only need to exclude other 397 * i2c clients from using the bus. refcounts are simply to prevent 398 * recursive locking. 399 */ 400 static void mctp_i2c_lock_nest(struct mctp_i2c_dev *midev) 401 { 402 unsigned long flags; 403 bool lock; 404 405 spin_lock_irqsave(&midev->lock, flags); 406 lock = midev->i2c_lock_count == 0; 407 midev->i2c_lock_count++; 408 spin_unlock_irqrestore(&midev->lock, flags); 409 410 if (lock) 411 i2c_lock_bus(midev->adapter, I2C_LOCK_SEGMENT); 412 } 413 414 static void mctp_i2c_unlock_nest(struct mctp_i2c_dev *midev) 415 { 416 unsigned long flags; 417 bool unlock; 418 419 spin_lock_irqsave(&midev->lock, flags); 420 if (!WARN_ONCE(midev->i2c_lock_count == 0, "lock count underflow!")) 421 midev->i2c_lock_count--; 422 unlock = midev->i2c_lock_count == 0; 423 spin_unlock_irqrestore(&midev->lock, flags); 424 425 if (unlock) 426 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); 427 } 428 429 /* Unlocks the bus if was previously locked, used for cleanup */ 430 static void mctp_i2c_unlock_reset(struct mctp_i2c_dev *midev) 431 { 432 unsigned long flags; 433 bool unlock; 434 435 spin_lock_irqsave(&midev->lock, flags); 436 unlock = midev->i2c_lock_count > 0; 437 midev->i2c_lock_count = 0; 438 spin_unlock_irqrestore(&midev->lock, flags); 439 440 if (unlock) 441 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); 442 } 443 444 static void mctp_i2c_invalidate_tx_flow(struct mctp_i2c_dev *midev, 445 struct sk_buff *skb) 446 { 447 struct mctp_sk_key *key; 448 struct mctp_flow *flow; 449 unsigned long flags; 450 bool release; 451 452 flow = skb_ext_find(skb, SKB_EXT_MCTP); 453 if (!flow) 454 return; 455 456 key = flow->key; 457 if (!key) 458 return; 459 460 spin_lock_irqsave(&key->lock, flags); 461 if (key->manual_alloc) { 462 /* we don't have control over lifetimes for manually-allocated 463 * keys, so cannot assume we can invalidate all future flows 464 * that would use this key. 465 */ 466 release = false; 467 } else { 468 release = key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE; 469 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID; 470 } 471 spin_unlock_irqrestore(&key->lock, flags); 472 473 /* if we have changed state from active, the flow held a reference on 474 * the lock; release that now. 475 */ 476 if (release) 477 mctp_i2c_unlock_nest(midev); 478 } 479 480 static void mctp_i2c_xmit(struct mctp_i2c_dev *midev, struct sk_buff *skb) 481 { 482 struct net_device_stats *stats = &midev->ndev->stats; 483 enum mctp_i2c_flow_state fs; 484 struct mctp_i2c_hdr *hdr; 485 struct i2c_msg msg = {0}; 486 u8 *pecp; 487 int rc; 488 489 fs = mctp_i2c_get_tx_flow_state(midev, skb); 490 491 hdr = (void *)skb_mac_header(skb); 492 /* Sanity check that packet contents matches skb length, 493 * and can't exceed MCTP_I2C_BUFSZ 494 */ 495 if (skb->len != hdr->byte_count + 3) { 496 dev_warn_ratelimited(&midev->adapter->dev, 497 "Bad tx length %d vs skb %u\n", 498 hdr->byte_count + 3, skb->len); 499 return; 500 } 501 502 if (skb_tailroom(skb) >= 1) { 503 /* Linear case with space, we can just append the PEC */ 504 skb_put(skb, 1); 505 } else { 506 /* Otherwise need to copy the buffer */ 507 skb_copy_bits(skb, 0, midev->tx_scratch, skb->len); 508 hdr = (void *)midev->tx_scratch; 509 } 510 511 pecp = (void *)&hdr->source_slave + hdr->byte_count; 512 *pecp = i2c_smbus_pec(0, (u8 *)hdr, hdr->byte_count + 3); 513 msg.buf = (void *)&hdr->command; 514 /* command, bytecount, data, pec */ 515 msg.len = 2 + hdr->byte_count + 1; 516 msg.addr = hdr->dest_slave >> 1; 517 518 switch (fs) { 519 case MCTP_I2C_TX_FLOW_NONE: 520 /* no flow: full lock & unlock */ 521 mctp_i2c_lock_nest(midev); 522 mctp_i2c_device_select(midev->client, midev); 523 rc = __i2c_transfer(midev->adapter, &msg, 1); 524 mctp_i2c_unlock_nest(midev); 525 break; 526 527 case MCTP_I2C_TX_FLOW_NEW: 528 /* new flow: lock, tx, but don't unlock; that will happen 529 * on flow release 530 */ 531 mctp_i2c_lock_nest(midev); 532 mctp_i2c_device_select(midev->client, midev); 533 fallthrough; 534 535 case MCTP_I2C_TX_FLOW_EXISTING: 536 /* existing flow: we already have the lock; just tx */ 537 rc = __i2c_transfer(midev->adapter, &msg, 1); 538 539 /* on tx errors, the flow can no longer be considered valid */ 540 if (rc) 541 mctp_i2c_invalidate_tx_flow(midev, skb); 542 543 break; 544 545 case MCTP_I2C_TX_FLOW_INVALID: 546 return; 547 } 548 549 if (rc < 0) { 550 dev_warn_ratelimited(&midev->adapter->dev, 551 "__i2c_transfer failed %d\n", rc); 552 stats->tx_errors++; 553 } else { 554 stats->tx_bytes += skb->len; 555 stats->tx_packets++; 556 } 557 } 558 559 static void mctp_i2c_flow_release(struct mctp_i2c_dev *midev) 560 { 561 unsigned long flags; 562 bool unlock; 563 564 spin_lock_irqsave(&midev->lock, flags); 565 if (midev->release_count > midev->i2c_lock_count) { 566 WARN_ONCE(1, "release count overflow"); 567 midev->release_count = midev->i2c_lock_count; 568 } 569 570 midev->i2c_lock_count -= midev->release_count; 571 unlock = midev->i2c_lock_count == 0 && midev->release_count > 0; 572 midev->release_count = 0; 573 spin_unlock_irqrestore(&midev->lock, flags); 574 575 if (unlock) 576 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); 577 } 578 579 static int mctp_i2c_header_create(struct sk_buff *skb, struct net_device *dev, 580 unsigned short type, const void *daddr, 581 const void *saddr, unsigned int len) 582 { 583 struct mctp_i2c_hdr *hdr; 584 struct mctp_hdr *mhdr; 585 u8 lldst, llsrc; 586 int rc; 587 588 if (len > MCTP_I2C_MAXMTU) 589 return -EMSGSIZE; 590 591 if (!daddr || !saddr) 592 return -EINVAL; 593 594 lldst = *((u8 *)daddr); 595 llsrc = *((u8 *)saddr); 596 597 rc = skb_cow_head(skb, sizeof(struct mctp_i2c_hdr)); 598 if (rc) 599 return rc; 600 601 skb_push(skb, sizeof(struct mctp_i2c_hdr)); 602 skb_reset_mac_header(skb); 603 hdr = (void *)skb_mac_header(skb); 604 mhdr = mctp_hdr(skb); 605 hdr->dest_slave = (lldst << 1) & 0xff; 606 hdr->command = MCTP_I2C_COMMANDCODE; 607 hdr->byte_count = len + 1; 608 hdr->source_slave = ((llsrc << 1) & 0xff) | 0x01; 609 mhdr->ver = 0x01; 610 611 return sizeof(struct mctp_i2c_hdr); 612 } 613 614 static int mctp_i2c_tx_thread(void *data) 615 { 616 struct mctp_i2c_dev *midev = data; 617 struct sk_buff *skb; 618 unsigned long flags; 619 620 for (;;) { 621 if (kthread_should_stop()) 622 break; 623 624 spin_lock_irqsave(&midev->tx_queue.lock, flags); 625 skb = __skb_dequeue(&midev->tx_queue); 626 if (netif_queue_stopped(midev->ndev)) 627 netif_wake_queue(midev->ndev); 628 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); 629 630 if (skb == &midev->unlock_marker) { 631 mctp_i2c_flow_release(midev); 632 633 } else if (skb) { 634 mctp_i2c_xmit(midev, skb); 635 kfree_skb(skb); 636 637 } else { 638 wait_event_idle(midev->tx_wq, 639 !skb_queue_empty(&midev->tx_queue) || 640 kthread_should_stop()); 641 } 642 } 643 644 return 0; 645 } 646 647 static netdev_tx_t mctp_i2c_start_xmit(struct sk_buff *skb, 648 struct net_device *dev) 649 { 650 struct mctp_i2c_dev *midev = netdev_priv(dev); 651 unsigned long flags; 652 653 spin_lock_irqsave(&midev->tx_queue.lock, flags); 654 if (skb_queue_len(&midev->tx_queue) >= MCTP_I2C_TX_WORK_LEN) { 655 netif_stop_queue(dev); 656 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); 657 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n"); 658 return NETDEV_TX_BUSY; 659 } 660 661 __skb_queue_tail(&midev->tx_queue, skb); 662 if (skb_queue_len(&midev->tx_queue) == MCTP_I2C_TX_WORK_LEN) 663 netif_stop_queue(dev); 664 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); 665 666 wake_up(&midev->tx_wq); 667 return NETDEV_TX_OK; 668 } 669 670 static void mctp_i2c_release_flow(struct mctp_dev *mdev, 671 struct mctp_sk_key *key) 672 673 { 674 struct mctp_i2c_dev *midev = netdev_priv(mdev->dev); 675 bool queue_release = false; 676 unsigned long flags; 677 678 spin_lock_irqsave(&midev->lock, flags); 679 /* if we have seen the flow/key previously, we need to pair the 680 * original lock with a release 681 */ 682 if (key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE) { 683 midev->release_count++; 684 queue_release = true; 685 } 686 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID; 687 spin_unlock_irqrestore(&midev->lock, flags); 688 689 if (queue_release) { 690 /* Ensure we have a release operation queued, through the fake 691 * marker skb 692 */ 693 spin_lock(&midev->tx_queue.lock); 694 if (!midev->unlock_marker.next) 695 __skb_queue_tail(&midev->tx_queue, 696 &midev->unlock_marker); 697 spin_unlock(&midev->tx_queue.lock); 698 wake_up(&midev->tx_wq); 699 } 700 } 701 702 static const struct net_device_ops mctp_i2c_ops = { 703 .ndo_start_xmit = mctp_i2c_start_xmit, 704 .ndo_uninit = mctp_i2c_ndo_uninit, 705 .ndo_open = mctp_i2c_ndo_open, 706 }; 707 708 static const struct header_ops mctp_i2c_headops = { 709 .create = mctp_i2c_header_create, 710 }; 711 712 static const struct mctp_netdev_ops mctp_i2c_mctp_ops = { 713 .release_flow = mctp_i2c_release_flow, 714 }; 715 716 static void mctp_i2c_net_setup(struct net_device *dev) 717 { 718 dev->type = ARPHRD_MCTP; 719 720 dev->mtu = MCTP_I2C_MAXMTU; 721 dev->min_mtu = MCTP_I2C_MINMTU; 722 dev->max_mtu = MCTP_I2C_MAXMTU; 723 dev->tx_queue_len = MCTP_I2C_TX_QUEUE_LEN; 724 725 dev->hard_header_len = sizeof(struct mctp_i2c_hdr); 726 dev->addr_len = 1; 727 728 dev->netdev_ops = &mctp_i2c_ops; 729 dev->header_ops = &mctp_i2c_headops; 730 } 731 732 /* Populates the mctp_i2c_dev priv struct for a netdev. 733 * Returns an error pointer on failure. 734 */ 735 static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev, 736 struct mctp_i2c_client *mcli, 737 struct i2c_adapter *adap) 738 { 739 struct mctp_i2c_dev *midev = netdev_priv(dev); 740 unsigned long flags; 741 742 midev->tx_thread = kthread_create(mctp_i2c_tx_thread, midev, 743 "%s/tx", dev->name); 744 if (IS_ERR(midev->tx_thread)) 745 return ERR_CAST(midev->tx_thread); 746 747 midev->ndev = dev; 748 get_device(&adap->dev); 749 midev->adapter = adap; 750 get_device(&mcli->client->dev); 751 midev->client = mcli; 752 INIT_LIST_HEAD(&midev->list); 753 spin_lock_init(&midev->lock); 754 midev->i2c_lock_count = 0; 755 midev->release_count = 0; 756 init_completion(&midev->rx_done); 757 complete(&midev->rx_done); 758 init_waitqueue_head(&midev->tx_wq); 759 skb_queue_head_init(&midev->tx_queue); 760 761 /* Add to the parent mcli */ 762 spin_lock_irqsave(&mcli->sel_lock, flags); 763 list_add(&midev->list, &mcli->devs); 764 /* Select a device by default */ 765 if (!mcli->sel) 766 __mctp_i2c_device_select(mcli, midev); 767 spin_unlock_irqrestore(&mcli->sel_lock, flags); 768 769 /* Start the worker thread */ 770 wake_up_process(midev->tx_thread); 771 772 return midev; 773 } 774 775 /* Counterpart of mctp_i2c_midev_init */ 776 static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev) 777 { 778 struct mctp_i2c_client *mcli = midev->client; 779 unsigned long flags; 780 781 if (midev->tx_thread) { 782 kthread_stop(midev->tx_thread); 783 midev->tx_thread = NULL; 784 } 785 786 /* Unconditionally unlock on close */ 787 mctp_i2c_unlock_reset(midev); 788 789 /* Remove the netdev from the parent i2c client. */ 790 spin_lock_irqsave(&mcli->sel_lock, flags); 791 list_del(&midev->list); 792 if (mcli->sel == midev) { 793 struct mctp_i2c_dev *first; 794 795 first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list); 796 __mctp_i2c_device_select(mcli, first); 797 } 798 spin_unlock_irqrestore(&mcli->sel_lock, flags); 799 800 skb_queue_purge(&midev->tx_queue); 801 put_device(&midev->adapter->dev); 802 put_device(&mcli->client->dev); 803 } 804 805 /* Stops, unregisters, and frees midev */ 806 static void mctp_i2c_unregister(struct mctp_i2c_dev *midev) 807 { 808 unsigned long flags; 809 810 /* Stop tx thread prior to unregister, it uses netif_() functions */ 811 kthread_stop(midev->tx_thread); 812 midev->tx_thread = NULL; 813 814 /* Prevent any new rx in mctp_i2c_recv(), let any pending work finish */ 815 spin_lock_irqsave(&midev->lock, flags); 816 midev->allow_rx = false; 817 spin_unlock_irqrestore(&midev->lock, flags); 818 wait_for_completion(&midev->rx_done); 819 820 mctp_unregister_netdev(midev->ndev); 821 /* midev has been freed now by mctp_i2c_ndo_uninit callback */ 822 823 free_netdev(midev->ndev); 824 } 825 826 static void mctp_i2c_ndo_uninit(struct net_device *dev) 827 { 828 struct mctp_i2c_dev *midev = netdev_priv(dev); 829 830 /* Perform cleanup here to ensure that mcli->sel isn't holding 831 * a reference that would prevent unregister_netdevice() 832 * from completing. 833 */ 834 mctp_i2c_midev_free(midev); 835 } 836 837 static int mctp_i2c_ndo_open(struct net_device *dev) 838 { 839 struct mctp_i2c_dev *midev = netdev_priv(dev); 840 unsigned long flags; 841 842 /* i2c rx handler can only pass packets once the netdev is registered */ 843 spin_lock_irqsave(&midev->lock, flags); 844 midev->allow_rx = true; 845 spin_unlock_irqrestore(&midev->lock, flags); 846 847 return 0; 848 } 849 850 static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli, 851 struct i2c_adapter *adap) 852 { 853 struct mctp_i2c_dev *midev = NULL; 854 struct net_device *ndev = NULL; 855 struct i2c_adapter *root; 856 unsigned long flags; 857 char namebuf[30]; 858 int rc; 859 860 root = mux_root_adapter(adap); 861 if (root != mcli->client->adapter) { 862 dev_err(&mcli->client->dev, 863 "I2C adapter %s is not a child bus of %s\n", 864 mcli->client->adapter->name, root->name); 865 return -EINVAL; 866 } 867 868 WARN_ON(!mutex_is_locked(&driver_clients_lock)); 869 snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr); 870 ndev = alloc_netdev(sizeof(*midev), namebuf, NET_NAME_ENUM, mctp_i2c_net_setup); 871 if (!ndev) { 872 dev_err(&mcli->client->dev, "alloc netdev failed\n"); 873 rc = -ENOMEM; 874 goto err; 875 } 876 dev_net_set(ndev, current->nsproxy->net_ns); 877 SET_NETDEV_DEV(ndev, &adap->dev); 878 dev_addr_set(ndev, &mcli->lladdr); 879 880 midev = mctp_i2c_midev_init(ndev, mcli, adap); 881 if (IS_ERR(midev)) { 882 rc = PTR_ERR(midev); 883 midev = NULL; 884 goto err; 885 } 886 887 rc = mctp_register_netdev(ndev, &mctp_i2c_mctp_ops, 888 MCTP_PHYS_BINDING_SMBUS); 889 if (rc < 0) { 890 dev_err(&mcli->client->dev, 891 "register netdev \"%s\" failed %d\n", 892 ndev->name, rc); 893 goto err; 894 } 895 896 spin_lock_irqsave(&midev->lock, flags); 897 midev->allow_rx = false; 898 spin_unlock_irqrestore(&midev->lock, flags); 899 900 return 0; 901 err: 902 if (midev) 903 mctp_i2c_midev_free(midev); 904 if (ndev) 905 free_netdev(ndev); 906 return rc; 907 } 908 909 /* Removes any netdev for adap. mcli is the parent root i2c client */ 910 static void mctp_i2c_remove_netdev(struct mctp_i2c_client *mcli, 911 struct i2c_adapter *adap) 912 { 913 struct mctp_i2c_dev *midev = NULL, *m = NULL; 914 unsigned long flags; 915 916 WARN_ON(!mutex_is_locked(&driver_clients_lock)); 917 spin_lock_irqsave(&mcli->sel_lock, flags); 918 /* List size is limited by number of MCTP netdevs on a single hardware bus */ 919 list_for_each_entry(m, &mcli->devs, list) 920 if (m->adapter == adap) { 921 midev = m; 922 break; 923 } 924 spin_unlock_irqrestore(&mcli->sel_lock, flags); 925 926 if (midev) 927 mctp_i2c_unregister(midev); 928 } 929 930 /* Determines whether a device is an i2c adapter. 931 * Optionally returns the root i2c_adapter 932 */ 933 static struct i2c_adapter *mctp_i2c_get_adapter(struct device *dev, 934 struct i2c_adapter **ret_root) 935 { 936 struct i2c_adapter *root, *adap; 937 938 if (dev->type != &i2c_adapter_type) 939 return NULL; 940 adap = to_i2c_adapter(dev); 941 root = mux_root_adapter(adap); 942 WARN_ONCE(!root, "MCTP I2C failed to find root adapter for %s\n", 943 dev_name(dev)); 944 if (!root) 945 return NULL; 946 if (ret_root) 947 *ret_root = root; 948 return adap; 949 } 950 951 /* Determines whether a device is an i2c adapter with the "mctp-controller" 952 * devicetree property set. If adap is not an OF node, returns match_no_of 953 */ 954 static bool mctp_i2c_adapter_match(struct i2c_adapter *adap, bool match_no_of) 955 { 956 if (!adap->dev.of_node) 957 return match_no_of; 958 return of_property_read_bool(adap->dev.of_node, MCTP_I2C_OF_PROP); 959 } 960 961 /* Called for each existing i2c device (adapter or client) when a 962 * new mctp-i2c client is probed. 963 */ 964 static int mctp_i2c_client_try_attach(struct device *dev, void *data) 965 { 966 struct i2c_adapter *adap = NULL, *root = NULL; 967 struct mctp_i2c_client *mcli = data; 968 969 adap = mctp_i2c_get_adapter(dev, &root); 970 if (!adap) 971 return 0; 972 if (mcli->client->adapter != root) 973 return 0; 974 /* Must either have mctp-controller property on the adapter, or 975 * be a root adapter if it's non-devicetree 976 */ 977 if (!mctp_i2c_adapter_match(adap, adap == root)) 978 return 0; 979 980 return mctp_i2c_add_netdev(mcli, adap); 981 } 982 983 static void mctp_i2c_notify_add(struct device *dev) 984 { 985 struct mctp_i2c_client *mcli = NULL, *m = NULL; 986 struct i2c_adapter *root = NULL, *adap = NULL; 987 int rc; 988 989 adap = mctp_i2c_get_adapter(dev, &root); 990 if (!adap) 991 return; 992 /* Check for mctp-controller property on the adapter */ 993 if (!mctp_i2c_adapter_match(adap, false)) 994 return; 995 996 /* Find an existing mcli for adap's root */ 997 mutex_lock(&driver_clients_lock); 998 list_for_each_entry(m, &driver_clients, list) { 999 if (m->client->adapter == root) { 1000 mcli = m; 1001 break; 1002 } 1003 } 1004 1005 if (mcli) { 1006 rc = mctp_i2c_add_netdev(mcli, adap); 1007 if (rc < 0) 1008 dev_warn(dev, "Failed adding mctp-i2c net device\n"); 1009 } 1010 mutex_unlock(&driver_clients_lock); 1011 } 1012 1013 static void mctp_i2c_notify_del(struct device *dev) 1014 { 1015 struct i2c_adapter *root = NULL, *adap = NULL; 1016 struct mctp_i2c_client *mcli = NULL; 1017 1018 adap = mctp_i2c_get_adapter(dev, &root); 1019 if (!adap) 1020 return; 1021 1022 mutex_lock(&driver_clients_lock); 1023 list_for_each_entry(mcli, &driver_clients, list) { 1024 if (mcli->client->adapter == root) { 1025 mctp_i2c_remove_netdev(mcli, adap); 1026 break; 1027 } 1028 } 1029 mutex_unlock(&driver_clients_lock); 1030 } 1031 1032 static int mctp_i2c_probe(struct i2c_client *client) 1033 { 1034 struct mctp_i2c_client *mcli = NULL; 1035 int rc; 1036 1037 mutex_lock(&driver_clients_lock); 1038 mcli = mctp_i2c_new_client(client); 1039 if (IS_ERR(mcli)) { 1040 rc = PTR_ERR(mcli); 1041 mcli = NULL; 1042 goto out; 1043 } else { 1044 list_add(&mcli->list, &driver_clients); 1045 } 1046 1047 /* Add a netdev for adapters that have a 'mctp-controller' property */ 1048 i2c_for_each_dev(mcli, mctp_i2c_client_try_attach); 1049 rc = 0; 1050 out: 1051 mutex_unlock(&driver_clients_lock); 1052 return rc; 1053 } 1054 1055 static void mctp_i2c_remove(struct i2c_client *client) 1056 { 1057 struct mctp_i2c_client *mcli = i2c_get_clientdata(client); 1058 struct mctp_i2c_dev *midev = NULL, *tmp = NULL; 1059 1060 mutex_lock(&driver_clients_lock); 1061 list_del(&mcli->list); 1062 /* Remove all child adapter netdevs */ 1063 list_for_each_entry_safe(midev, tmp, &mcli->devs, list) 1064 mctp_i2c_unregister(midev); 1065 1066 mctp_i2c_free_client(mcli); 1067 mutex_unlock(&driver_clients_lock); 1068 } 1069 1070 /* We look for a 'mctp-controller' property on I2C busses as they are 1071 * added/deleted, creating/removing netdevs as required. 1072 */ 1073 static int mctp_i2c_notifier_call(struct notifier_block *nb, 1074 unsigned long action, void *data) 1075 { 1076 struct device *dev = data; 1077 1078 switch (action) { 1079 case BUS_NOTIFY_ADD_DEVICE: 1080 mctp_i2c_notify_add(dev); 1081 break; 1082 case BUS_NOTIFY_DEL_DEVICE: 1083 mctp_i2c_notify_del(dev); 1084 break; 1085 } 1086 return NOTIFY_DONE; 1087 } 1088 1089 static struct notifier_block mctp_i2c_notifier = { 1090 .notifier_call = mctp_i2c_notifier_call, 1091 }; 1092 1093 static const struct i2c_device_id mctp_i2c_id[] = { 1094 { "mctp-i2c-interface" }, 1095 {} 1096 }; 1097 MODULE_DEVICE_TABLE(i2c, mctp_i2c_id); 1098 1099 static const struct of_device_id mctp_i2c_of_match[] = { 1100 { .compatible = "mctp-i2c-controller" }, 1101 {}, 1102 }; 1103 MODULE_DEVICE_TABLE(of, mctp_i2c_of_match); 1104 1105 static struct i2c_driver mctp_i2c_driver = { 1106 .driver = { 1107 .name = "mctp-i2c-interface", 1108 .of_match_table = mctp_i2c_of_match, 1109 }, 1110 .probe = mctp_i2c_probe, 1111 .remove = mctp_i2c_remove, 1112 .id_table = mctp_i2c_id, 1113 }; 1114 1115 static __init int mctp_i2c_mod_init(void) 1116 { 1117 int rc; 1118 1119 pr_info("MCTP I2C interface driver\n"); 1120 rc = i2c_add_driver(&mctp_i2c_driver); 1121 if (rc < 0) 1122 return rc; 1123 rc = bus_register_notifier(&i2c_bus_type, &mctp_i2c_notifier); 1124 if (rc < 0) { 1125 i2c_del_driver(&mctp_i2c_driver); 1126 return rc; 1127 } 1128 return 0; 1129 } 1130 1131 static __exit void mctp_i2c_mod_exit(void) 1132 { 1133 int rc; 1134 1135 rc = bus_unregister_notifier(&i2c_bus_type, &mctp_i2c_notifier); 1136 if (rc < 0) 1137 pr_warn("MCTP I2C could not unregister notifier, %d\n", rc); 1138 i2c_del_driver(&mctp_i2c_driver); 1139 } 1140 1141 module_init(mctp_i2c_mod_init); 1142 module_exit(mctp_i2c_mod_exit); 1143 1144 MODULE_DESCRIPTION("MCTP I2C device"); 1145 MODULE_LICENSE("GPL v2"); 1146 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>"); 1147