Lines Matching +full:mctp +full:- +full:controller
1 // SPDX-License-Identifier: GPL-2.0
3 * Management Controller Transport Protocol (MCTP)
5 * "DSP0237 Management Component Transport Protocol (MCTP) SMBus/I2C
9 * A netdev is created for each I2C bus that handles MCTP. In the case of an I2C
11 * shared between all mux I2C busses underneath. For non-mux cases an I2C client
14 * mctp-i2c-controller.yml devicetree binding has further details.
23 #include <linux/i2c-mux.h>
25 #include <net/mctp.h>
31 #define MCTP_I2C_MAXMTU (MCTP_I2C_MAXBLOCK - 1)
41 #define MCTP_I2C_OF_PROP "mctp-controller"
118 return i2c_root_adapter(&adap->dev); in mux_root_adapter()
120 /* In non-mux config all i2c adapters are root adapters */ in mux_root_adapter()
135 if (client->flags & I2C_CLIENT_TEN) { in mctp_i2c_new_client()
136 dev_err(&client->dev, "failed, MCTP requires a 7-bit I2C address, addr=0x%x\n", in mctp_i2c_new_client()
137 client->addr); in mctp_i2c_new_client()
138 rc = -EINVAL; in mctp_i2c_new_client()
142 root = mux_root_adapter(client->adapter); in mctp_i2c_new_client()
144 dev_err(&client->dev, "failed to find root adapter\n"); in mctp_i2c_new_client()
145 rc = -ENOENT; in mctp_i2c_new_client()
148 if (root != client->adapter) { in mctp_i2c_new_client()
149 dev_err(&client->dev, in mctp_i2c_new_client()
150 "A mctp-i2c-controller client cannot be placed on an I2C mux adapter.\n" in mctp_i2c_new_client()
152 " then set mctp-controller property on adapters to attach\n"); in mctp_i2c_new_client()
153 rc = -EINVAL; in mctp_i2c_new_client()
159 rc = -ENOMEM; in mctp_i2c_new_client()
162 spin_lock_init(&mcli->sel_lock); in mctp_i2c_new_client()
163 INIT_LIST_HEAD(&mcli->devs); in mctp_i2c_new_client()
164 INIT_LIST_HEAD(&mcli->list); in mctp_i2c_new_client()
165 mcli->lladdr = client->addr & 0xff; in mctp_i2c_new_client()
166 mcli->client = client; in mctp_i2c_new_client()
169 rc = i2c_slave_register(mcli->client, mctp_i2c_slave_cb); in mctp_i2c_new_client()
171 dev_err(&client->dev, "i2c register failed %d\n", rc); in mctp_i2c_new_client()
172 mcli->client = NULL; in mctp_i2c_new_client()
180 if (mcli->client) in mctp_i2c_new_client()
181 i2c_unregister_device(mcli->client); in mctp_i2c_new_client()
192 WARN_ON(!list_empty(&mcli->devs)); in mctp_i2c_free_client()
193 WARN_ON(mcli->sel); /* sanity check, no locking */ in mctp_i2c_free_client()
195 rc = i2c_slave_unregister(mcli->client); in mctp_i2c_free_client()
198 dev_err(&mcli->client->dev, "i2c unregister failed %d\n", rc); in mctp_i2c_free_client()
203 /* Switch the mctp i2c device to receive responses.
209 assert_spin_locked(&mcli->sel_lock); in __mctp_i2c_device_select()
211 dev_hold(midev->ndev); in __mctp_i2c_device_select()
212 if (mcli->sel) in __mctp_i2c_device_select()
213 dev_put(mcli->sel->ndev); in __mctp_i2c_device_select()
214 mcli->sel = midev; in __mctp_i2c_device_select()
217 /* Switch the mctp i2c device to receive responses */
223 spin_lock_irqsave(&mcli->sel_lock, flags); in mctp_i2c_device_select()
225 spin_unlock_irqrestore(&mcli->sel_lock, flags); in mctp_i2c_device_select()
236 spin_lock_irqsave(&mcli->sel_lock, flags); in mctp_i2c_slave_cb()
237 midev = mcli->sel; in mctp_i2c_slave_cb()
239 dev_hold(midev->ndev); in mctp_i2c_slave_cb()
240 spin_unlock_irqrestore(&mcli->sel_lock, flags); in mctp_i2c_slave_cb()
247 if (midev->rx_pos < MCTP_I2C_BUFSZ) { in mctp_i2c_slave_cb()
248 midev->rx_buffer[midev->rx_pos] = *val; in mctp_i2c_slave_cb()
249 midev->rx_pos++; in mctp_i2c_slave_cb()
251 midev->ndev->stats.rx_over_errors++; in mctp_i2c_slave_cb()
257 midev->rx_buffer[0] = mcli->lladdr << 1; in mctp_i2c_slave_cb()
258 midev->rx_pos = 1; in mctp_i2c_slave_cb()
267 dev_put(midev->ndev); in mctp_i2c_slave_cb()
274 struct net_device *ndev = midev->ndev; in mctp_i2c_recv()
284 if (midev->rx_pos < MCTP_I2C_MINLEN + 1) { in mctp_i2c_recv()
285 ndev->stats.rx_length_errors++; in mctp_i2c_recv()
286 return -EINVAL; in mctp_i2c_recv()
289 recvlen = midev->rx_pos - 1; in mctp_i2c_recv()
291 hdr = (void *)midev->rx_buffer; in mctp_i2c_recv()
292 if (hdr->command != MCTP_I2C_COMMANDCODE) { in mctp_i2c_recv()
293 ndev->stats.rx_dropped++; in mctp_i2c_recv()
294 return -EINVAL; in mctp_i2c_recv()
297 if (hdr->byte_count + offsetof(struct mctp_i2c_hdr, source_slave) != recvlen) { in mctp_i2c_recv()
298 ndev->stats.rx_length_errors++; in mctp_i2c_recv()
299 return -EINVAL; in mctp_i2c_recv()
302 pec = midev->rx_buffer[midev->rx_pos - 1]; in mctp_i2c_recv()
303 calc_pec = i2c_smbus_pec(0, midev->rx_buffer, recvlen); in mctp_i2c_recv()
305 ndev->stats.rx_crc_errors++; in mctp_i2c_recv()
306 return -EINVAL; in mctp_i2c_recv()
311 ndev->stats.rx_dropped++; in mctp_i2c_recv()
312 return -ENOMEM; in mctp_i2c_recv()
315 skb->protocol = htons(ETH_P_MCTP); in mctp_i2c_recv()
316 skb_put_data(skb, midev->rx_buffer, recvlen); in mctp_i2c_recv()
322 cb->halen = 1; in mctp_i2c_recv()
323 cb->haddr[0] = hdr->source_slave >> 1; in mctp_i2c_recv()
328 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_recv()
329 if (midev->allow_rx) { in mctp_i2c_recv()
330 reinit_completion(&midev->rx_done); in mctp_i2c_recv()
331 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_recv()
334 complete(&midev->rx_done); in mctp_i2c_recv()
337 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_recv()
341 ndev->stats.rx_packets++; in mctp_i2c_recv()
342 ndev->stats.rx_bytes += recvlen; in mctp_i2c_recv()
344 ndev->stats.rx_dropped++; in mctp_i2c_recv()
368 key = flow->key; in mctp_i2c_get_tx_flow_state()
372 spin_lock_irqsave(&key->lock, flags); in mctp_i2c_get_tx_flow_state()
376 if (!key->valid) { in mctp_i2c_get_tx_flow_state()
379 switch (key->dev_flow_state) { in mctp_i2c_get_tx_flow_state()
381 key->dev_flow_state = MCTP_I2C_FLOW_STATE_ACTIVE; in mctp_i2c_get_tx_flow_state()
392 spin_unlock_irqrestore(&key->lock, flags); in mctp_i2c_get_tx_flow_state()
406 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_lock_nest()
407 lock = midev->i2c_lock_count == 0; in mctp_i2c_lock_nest()
408 midev->i2c_lock_count++; in mctp_i2c_lock_nest()
409 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_lock_nest()
412 i2c_lock_bus(midev->adapter, I2C_LOCK_SEGMENT); in mctp_i2c_lock_nest()
420 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_unlock_nest()
421 if (!WARN_ONCE(midev->i2c_lock_count == 0, "lock count underflow!")) in mctp_i2c_unlock_nest()
422 midev->i2c_lock_count--; in mctp_i2c_unlock_nest()
423 unlock = midev->i2c_lock_count == 0; in mctp_i2c_unlock_nest()
424 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_unlock_nest()
427 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); in mctp_i2c_unlock_nest()
436 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_unlock_reset()
437 unlock = midev->i2c_lock_count > 0; in mctp_i2c_unlock_reset()
438 midev->i2c_lock_count = 0; in mctp_i2c_unlock_reset()
439 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_unlock_reset()
442 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); in mctp_i2c_unlock_reset()
457 key = flow->key; in mctp_i2c_invalidate_tx_flow()
461 spin_lock_irqsave(&key->lock, flags); in mctp_i2c_invalidate_tx_flow()
462 if (key->manual_alloc) { in mctp_i2c_invalidate_tx_flow()
463 /* we don't have control over lifetimes for manually-allocated in mctp_i2c_invalidate_tx_flow()
469 release = key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE; in mctp_i2c_invalidate_tx_flow()
470 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID; in mctp_i2c_invalidate_tx_flow()
472 spin_unlock_irqrestore(&key->lock, flags); in mctp_i2c_invalidate_tx_flow()
483 struct net_device_stats *stats = &midev->ndev->stats; in mctp_i2c_xmit()
496 if (skb->len != hdr->byte_count + 3) { in mctp_i2c_xmit()
497 dev_warn_ratelimited(&midev->adapter->dev, in mctp_i2c_xmit()
499 hdr->byte_count + 3, skb->len); in mctp_i2c_xmit()
508 skb_copy_bits(skb, 0, midev->tx_scratch, skb->len); in mctp_i2c_xmit()
509 hdr = (void *)midev->tx_scratch; in mctp_i2c_xmit()
512 pecp = (void *)&hdr->source_slave + hdr->byte_count; in mctp_i2c_xmit()
513 *pecp = i2c_smbus_pec(0, (u8 *)hdr, hdr->byte_count + 3); in mctp_i2c_xmit()
514 msg.buf = (void *)&hdr->command; in mctp_i2c_xmit()
516 msg.len = 2 + hdr->byte_count + 1; in mctp_i2c_xmit()
517 msg.addr = hdr->dest_slave >> 1; in mctp_i2c_xmit()
523 mctp_i2c_device_select(midev->client, midev); in mctp_i2c_xmit()
524 rc = __i2c_transfer(midev->adapter, &msg, 1); in mctp_i2c_xmit()
533 mctp_i2c_device_select(midev->client, midev); in mctp_i2c_xmit()
538 rc = __i2c_transfer(midev->adapter, &msg, 1); in mctp_i2c_xmit()
551 dev_warn_ratelimited(&midev->adapter->dev, in mctp_i2c_xmit()
553 stats->tx_errors++; in mctp_i2c_xmit()
555 stats->tx_bytes += skb->len; in mctp_i2c_xmit()
556 stats->tx_packets++; in mctp_i2c_xmit()
565 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_flow_release()
566 if (midev->release_count > midev->i2c_lock_count) { in mctp_i2c_flow_release()
568 midev->release_count = midev->i2c_lock_count; in mctp_i2c_flow_release()
571 midev->i2c_lock_count -= midev->release_count; in mctp_i2c_flow_release()
572 unlock = midev->i2c_lock_count == 0 && midev->release_count > 0; in mctp_i2c_flow_release()
573 midev->release_count = 0; in mctp_i2c_flow_release()
574 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_flow_release()
577 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT); in mctp_i2c_flow_release()
589 return -EMSGSIZE; in mctp_i2c_header_create()
592 return -EINVAL; in mctp_i2c_header_create()
601 hdr->dest_slave = (lldst << 1) & 0xff; in mctp_i2c_header_create()
602 hdr->command = MCTP_I2C_COMMANDCODE; in mctp_i2c_header_create()
603 hdr->byte_count = len + 1; in mctp_i2c_header_create()
604 hdr->source_slave = ((llsrc << 1) & 0xff) | 0x01; in mctp_i2c_header_create()
605 mhdr->ver = 0x01; in mctp_i2c_header_create()
620 spin_lock_irqsave(&midev->tx_queue.lock, flags); in mctp_i2c_tx_thread()
621 skb = __skb_dequeue(&midev->tx_queue); in mctp_i2c_tx_thread()
622 if (netif_queue_stopped(midev->ndev)) in mctp_i2c_tx_thread()
623 netif_wake_queue(midev->ndev); in mctp_i2c_tx_thread()
624 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); in mctp_i2c_tx_thread()
626 if (skb == &midev->unlock_marker) { in mctp_i2c_tx_thread()
634 wait_event_idle(midev->tx_wq, in mctp_i2c_tx_thread()
635 !skb_queue_empty(&midev->tx_queue) || in mctp_i2c_tx_thread()
649 spin_lock_irqsave(&midev->tx_queue.lock, flags); in mctp_i2c_start_xmit()
650 if (skb_queue_len(&midev->tx_queue) >= MCTP_I2C_TX_WORK_LEN) { in mctp_i2c_start_xmit()
652 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); in mctp_i2c_start_xmit()
657 __skb_queue_tail(&midev->tx_queue, skb); in mctp_i2c_start_xmit()
658 if (skb_queue_len(&midev->tx_queue) == MCTP_I2C_TX_WORK_LEN) in mctp_i2c_start_xmit()
660 spin_unlock_irqrestore(&midev->tx_queue.lock, flags); in mctp_i2c_start_xmit()
662 wake_up(&midev->tx_wq); in mctp_i2c_start_xmit()
670 struct mctp_i2c_dev *midev = netdev_priv(mdev->dev); in mctp_i2c_release_flow()
674 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_release_flow()
678 if (key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE) { in mctp_i2c_release_flow()
679 midev->release_count++; in mctp_i2c_release_flow()
682 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID; in mctp_i2c_release_flow()
683 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_release_flow()
689 spin_lock(&midev->tx_queue.lock); in mctp_i2c_release_flow()
690 if (!midev->unlock_marker.next) in mctp_i2c_release_flow()
691 __skb_queue_tail(&midev->tx_queue, in mctp_i2c_release_flow()
692 &midev->unlock_marker); in mctp_i2c_release_flow()
693 spin_unlock(&midev->tx_queue.lock); in mctp_i2c_release_flow()
694 wake_up(&midev->tx_wq); in mctp_i2c_release_flow()
714 dev->type = ARPHRD_MCTP; in mctp_i2c_net_setup()
716 dev->mtu = MCTP_I2C_MAXMTU; in mctp_i2c_net_setup()
717 dev->min_mtu = MCTP_I2C_MINMTU; in mctp_i2c_net_setup()
718 dev->max_mtu = MCTP_I2C_MAXMTU; in mctp_i2c_net_setup()
719 dev->tx_queue_len = MCTP_I2C_TX_QUEUE_LEN; in mctp_i2c_net_setup()
721 dev->hard_header_len = sizeof(struct mctp_i2c_hdr); in mctp_i2c_net_setup()
722 dev->addr_len = 1; in mctp_i2c_net_setup()
724 dev->netdev_ops = &mctp_i2c_ops; in mctp_i2c_net_setup()
725 dev->header_ops = &mctp_i2c_headops; in mctp_i2c_net_setup()
738 midev->tx_thread = kthread_create(mctp_i2c_tx_thread, midev, in mctp_i2c_midev_init()
739 "%s/tx", dev->name); in mctp_i2c_midev_init()
740 if (IS_ERR(midev->tx_thread)) in mctp_i2c_midev_init()
741 return ERR_CAST(midev->tx_thread); in mctp_i2c_midev_init()
743 midev->ndev = dev; in mctp_i2c_midev_init()
744 get_device(&adap->dev); in mctp_i2c_midev_init()
745 midev->adapter = adap; in mctp_i2c_midev_init()
746 get_device(&mcli->client->dev); in mctp_i2c_midev_init()
747 midev->client = mcli; in mctp_i2c_midev_init()
748 INIT_LIST_HEAD(&midev->list); in mctp_i2c_midev_init()
749 spin_lock_init(&midev->lock); in mctp_i2c_midev_init()
750 midev->i2c_lock_count = 0; in mctp_i2c_midev_init()
751 midev->release_count = 0; in mctp_i2c_midev_init()
752 init_completion(&midev->rx_done); in mctp_i2c_midev_init()
753 complete(&midev->rx_done); in mctp_i2c_midev_init()
754 init_waitqueue_head(&midev->tx_wq); in mctp_i2c_midev_init()
755 skb_queue_head_init(&midev->tx_queue); in mctp_i2c_midev_init()
758 spin_lock_irqsave(&mcli->sel_lock, flags); in mctp_i2c_midev_init()
759 list_add(&midev->list, &mcli->devs); in mctp_i2c_midev_init()
761 if (!mcli->sel) in mctp_i2c_midev_init()
763 spin_unlock_irqrestore(&mcli->sel_lock, flags); in mctp_i2c_midev_init()
766 wake_up_process(midev->tx_thread); in mctp_i2c_midev_init()
774 struct mctp_i2c_client *mcli = midev->client; in mctp_i2c_midev_free()
777 if (midev->tx_thread) { in mctp_i2c_midev_free()
778 kthread_stop(midev->tx_thread); in mctp_i2c_midev_free()
779 midev->tx_thread = NULL; in mctp_i2c_midev_free()
786 spin_lock_irqsave(&mcli->sel_lock, flags); in mctp_i2c_midev_free()
787 list_del(&midev->list); in mctp_i2c_midev_free()
788 if (mcli->sel == midev) { in mctp_i2c_midev_free()
791 first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list); in mctp_i2c_midev_free()
794 spin_unlock_irqrestore(&mcli->sel_lock, flags); in mctp_i2c_midev_free()
796 skb_queue_purge(&midev->tx_queue); in mctp_i2c_midev_free()
797 put_device(&midev->adapter->dev); in mctp_i2c_midev_free()
798 put_device(&mcli->client->dev); in mctp_i2c_midev_free()
807 kthread_stop(midev->tx_thread); in mctp_i2c_unregister()
808 midev->tx_thread = NULL; in mctp_i2c_unregister()
811 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_unregister()
812 midev->allow_rx = false; in mctp_i2c_unregister()
813 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_unregister()
814 wait_for_completion(&midev->rx_done); in mctp_i2c_unregister()
816 mctp_unregister_netdev(midev->ndev); in mctp_i2c_unregister()
819 free_netdev(midev->ndev); in mctp_i2c_unregister()
826 /* Perform cleanup here to ensure that mcli->sel isn't holding in mctp_i2c_ndo_uninit()
839 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_ndo_open()
840 midev->allow_rx = true; in mctp_i2c_ndo_open()
841 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_ndo_open()
857 if (root != mcli->client->adapter) { in mctp_i2c_add_netdev()
858 dev_err(&mcli->client->dev, in mctp_i2c_add_netdev()
860 mcli->client->adapter->name, root->name); in mctp_i2c_add_netdev()
861 return -EINVAL; in mctp_i2c_add_netdev()
865 snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr); in mctp_i2c_add_netdev()
868 dev_err(&mcli->client->dev, "alloc netdev failed\n"); in mctp_i2c_add_netdev()
869 rc = -ENOMEM; in mctp_i2c_add_netdev()
872 dev_net_set(ndev, current->nsproxy->net_ns); in mctp_i2c_add_netdev()
873 SET_NETDEV_DEV(ndev, &adap->dev); in mctp_i2c_add_netdev()
874 dev_addr_set(ndev, &mcli->lladdr); in mctp_i2c_add_netdev()
885 dev_err(&mcli->client->dev, in mctp_i2c_add_netdev()
887 ndev->name, rc); in mctp_i2c_add_netdev()
891 spin_lock_irqsave(&midev->lock, flags); in mctp_i2c_add_netdev()
892 midev->allow_rx = false; in mctp_i2c_add_netdev()
893 spin_unlock_irqrestore(&midev->lock, flags); in mctp_i2c_add_netdev()
912 spin_lock_irqsave(&mcli->sel_lock, flags); in mctp_i2c_remove_netdev()
913 /* List size is limited by number of MCTP netdevs on a single hardware bus */ in mctp_i2c_remove_netdev()
914 list_for_each_entry(m, &mcli->devs, list) in mctp_i2c_remove_netdev()
915 if (m->adapter == adap) { in mctp_i2c_remove_netdev()
919 spin_unlock_irqrestore(&mcli->sel_lock, flags); in mctp_i2c_remove_netdev()
933 if (dev->type != &i2c_adapter_type) in mctp_i2c_get_adapter()
937 WARN_ONCE(!root, "MCTP I2C failed to find root adapter for %s\n", in mctp_i2c_get_adapter()
946 /* Determines whether a device is an i2c adapter with the "mctp-controller"
951 if (!adap->dev.of_node) in mctp_i2c_adapter_match()
953 return of_property_read_bool(adap->dev.of_node, MCTP_I2C_OF_PROP); in mctp_i2c_adapter_match()
957 * new mctp-i2c client is probed.
967 if (mcli->client->adapter != root) in mctp_i2c_client_try_attach()
969 /* Must either have mctp-controller property on the adapter, or in mctp_i2c_client_try_attach()
970 * be a root adapter if it's non-devicetree in mctp_i2c_client_try_attach()
987 /* Check for mctp-controller property on the adapter */ in mctp_i2c_notify_add()
994 if (m->client->adapter == root) { in mctp_i2c_notify_add()
1003 dev_warn(dev, "Failed adding mctp-i2c net device\n"); in mctp_i2c_notify_add()
1019 if (mcli->client->adapter == root) { in mctp_i2c_notify_del()
1039 list_add(&mcli->list, &driver_clients); in mctp_i2c_probe()
1042 /* Add a netdev for adapters that have a 'mctp-controller' property */ in mctp_i2c_probe()
1056 list_del(&mcli->list); in mctp_i2c_remove()
1058 list_for_each_entry_safe(midev, tmp, &mcli->devs, list) in mctp_i2c_remove()
1065 /* We look for a 'mctp-controller' property on I2C busses as they are
1089 { "mctp-i2c-interface" },
1095 { .compatible = "mctp-i2c-controller" },
1102 .name = "mctp-i2c-interface",
1114 pr_info("MCTP I2C interface driver\n"); in mctp_i2c_mod_init()
1132 pr_warn("MCTP I2C could not unregister notifier, %d\n", rc); in mctp_i2c_mod_exit()
1139 MODULE_DESCRIPTION("MCTP I2C device");