1 // SPDX-License-Identifier: GPL-2.0
3 * Implements DMTF specification
4 * "DSP0233 Management Component Transport Protocol (MCTP) I3C Transport
6 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0233_1.0.0.pdf
8 * Copyright (c) 2023 Code Construct
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/i3c/device.h>
14 #include <linux/i3c/master.h>
15 #include <linux/if_arp.h>
16 #include <asm/unaligned.h>
18 #include <net/mctpdevice.h>
20 #define MCTP_I3C_MAXBUF 65536
21 /* 48 bit Provisioned Id */
24 /* 64 byte payload, 4 byte MCTP header */
25 static const int MCTP_I3C_MINMTU = 64 + 4;
26 /* One byte less to allow for the PEC */
27 static const int MCTP_I3C_MAXMTU = MCTP_I3C_MAXBUF - 1;
28 /* 4 byte MCTP header, no data, 1 byte PEC */
29 static const int MCTP_I3C_MINLEN = 4 + 1;
31 /* Sufficient for 64kB at min mtu */
32 static const int MCTP_I3C_TX_QUEUE_LEN = 1100;
34 /* Somewhat arbitrary */
35 static const int MCTP_I3C_IBI_SLOTS = 8;
37 /* Mandatory Data Byte in an IBI, from DSP0233 */
38 #define I3C_MDB_MCTP 0xAE
39 /* From MIPI Device Characteristics Register (DCR) Assignments */
40 #define I3C_DCR_MCTP 0xCC
42 static const char *MCTP_I3C_OF_PROP = "mctp-controller";
44 /* List of mctp_i3c_busdev */
45 static LIST_HEAD(busdevs);
46 /* Protects busdevs, as well as mctp_i3c_bus.devs lists */
47 static DEFINE_MUTEX(busdevs_lock);
50 struct net_device *ndev;
52 struct task_struct *tx_thread;
53 wait_queue_head_t tx_wq;
54 /* tx_lock protects tx_skb and devs */
56 /* Next skb to transmit */
57 struct sk_buff *tx_skb;
58 /* Scratch buffer for xmit */
59 u8 tx_scratch[MCTP_I3C_MAXBUF];
61 /* Element of busdevs */
62 struct list_head list;
64 /* Provisioned ID of our controller */
68 /* Head of mctp_i3c_device.list. Protected by busdevs_lock */
69 struct list_head devs;
72 struct mctp_i3c_device {
73 struct i3c_device *i3c;
74 struct mctp_i3c_bus *mbus;
75 struct list_head list; /* Element of mctp_i3c_bus.devs */
77 /* Held while tx_thread is using this device */
80 /* Whether BCR indicates MDB is present in IBI */
82 /* I3C dynamic address */
84 /* Maximum read length */
86 /* Maximum write length */
92 /* We synthesise a mac header using the Provisioned ID.
93 * Used to pass dest to mctp_i3c_start_xmit.
95 struct mctp_i3c_internal_hdr {
100 static int mctp_i3c_read(struct mctp_i3c_device *mi)
102 struct i3c_priv_xfer xfer = { .rnw = 1, .len = mi->mrl };
103 struct net_device_stats *stats = &mi->mbus->ndev->stats;
104 struct mctp_i3c_internal_hdr *ihdr = NULL;
105 struct sk_buff *skb = NULL;
106 struct mctp_skb_cb *cb;
110 skb = netdev_alloc_skb(mi->mbus->ndev,
111 mi->mrl + sizeof(struct mctp_i3c_internal_hdr));
118 skb->protocol = htons(ETH_P_MCTP);
119 /* Create a header for internal use */
120 skb_reset_mac_header(skb);
121 ihdr = skb_put(skb, sizeof(struct mctp_i3c_internal_hdr));
122 put_unaligned_be48(mi->pid, ihdr->source);
123 put_unaligned_be48(mi->mbus->pid, ihdr->dest);
124 skb_pull(skb, sizeof(struct mctp_i3c_internal_hdr));
126 xfer.data.in = skb_put(skb, mi->mrl);
128 rc = i3c_device_do_priv_xfers(mi->i3c, &xfer, 1);
132 if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
133 /* Bad i3c bus driver */
137 if (xfer.len < MCTP_I3C_MINLEN) {
138 stats->rx_length_errors++;
143 /* check PEC, including address byte */
144 addr = mi->addr << 1 | 1;
145 pec = i2c_smbus_pec(0, &addr, 1);
146 pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
147 if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
148 stats->rx_crc_errors++;
154 skb_trim(skb, xfer.len - 1);
157 cb->halen = PID_SIZE;
158 put_unaligned_be48(mi->pid, cb->haddr);
160 net_status = netif_rx(skb);
162 if (net_status == NET_RX_SUCCESS) {
164 stats->rx_bytes += xfer.len - 1;
175 static void mctp_i3c_ibi_handler(struct i3c_device *i3c,
176 const struct i3c_ibi_payload *payload)
178 struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
180 if (WARN_ON_ONCE(!mi))
184 if (payload->len > 0) {
185 if (((u8 *)payload->data)[0] != I3C_MDB_MCTP) {
186 /* Not a mctp-i3c interrupt, ignore it */
190 /* The BCR advertised a Mandatory Data Byte but the
191 * device didn't send one.
193 dev_warn_once(i3cdev_to_dev(i3c), "IBI with missing MDB");
200 static int mctp_i3c_setup(struct mctp_i3c_device *mi)
202 const struct i3c_ibi_setup ibi = {
203 .max_payload_len = 1,
204 .num_slots = MCTP_I3C_IBI_SLOTS,
205 .handler = mctp_i3c_ibi_handler,
207 struct i3c_device_info info;
210 i3c_device_get_info(mi->i3c, &info);
211 mi->have_mdb = info.bcr & BIT(2);
212 mi->addr = info.dyn_addr;
213 mi->mwl = info.max_write_len;
214 mi->mrl = info.max_read_len;
217 rc = i3c_device_request_ibi(mi->i3c, &ibi);
218 if (rc == -ENOTSUPP) {
219 /* This driver only supports In-Band Interrupt mode.
220 * Support for Polling Mode could be added if required.
221 * (ENOTSUPP is from the i3c layer, not EOPNOTSUPP).
223 dev_warn(i3cdev_to_dev(mi->i3c),
224 "Failed, bus driver doesn't support In-Band Interrupts");
227 dev_err(i3cdev_to_dev(mi->i3c),
228 "Failed requesting IBI (%d)\n", rc);
232 rc = i3c_device_enable_ibi(mi->i3c);
234 /* Assume a driver supporting request_ibi also
235 * supports enable_ibi.
237 dev_err(i3cdev_to_dev(mi->i3c), "Failed enabling IBI (%d)\n", rc);
244 i3c_device_free_ibi(mi->i3c);
250 /* Adds a new MCTP i3c_device to a bus */
251 static int mctp_i3c_add_device(struct mctp_i3c_bus *mbus,
252 struct i3c_device *i3c)
253 __must_hold(&busdevs_lock)
255 struct mctp_i3c_device *mi = NULL;
258 mi = kzalloc(sizeof(*mi), GFP_KERNEL);
265 mutex_init(&mi->lock);
266 list_add(&mi->list, &mbus->devs);
268 i3cdev_set_drvdata(i3c, mi);
269 rc = mctp_i3c_setup(mi);
280 dev_warn(i3cdev_to_dev(i3c), "Error adding mctp-i3c device, %d\n", rc);
284 static int mctp_i3c_probe(struct i3c_device *i3c)
286 struct mctp_i3c_bus *b = NULL, *mbus = NULL;
288 /* Look for a known bus */
289 mutex_lock(&busdevs_lock);
290 list_for_each_entry(b, &busdevs, list)
291 if (b->bus == i3c->bus) {
295 mutex_unlock(&busdevs_lock);
298 /* probably no "mctp-controller" property on the i3c bus */
302 return mctp_i3c_add_device(mbus, i3c);
305 static void mctp_i3c_remove_device(struct mctp_i3c_device *mi)
306 __must_hold(&busdevs_lock)
308 /* Ensure the tx thread isn't using the device */
309 mutex_lock(&mi->lock);
311 /* Counterpart of mctp_i3c_setup */
312 i3c_device_disable_ibi(mi->i3c);
313 i3c_device_free_ibi(mi->i3c);
315 /* Counterpart of mctp_i3c_add_device */
316 i3cdev_set_drvdata(mi->i3c, NULL);
319 /* Safe to unlock after removing from the list */
320 mutex_unlock(&mi->lock);
324 static void mctp_i3c_remove(struct i3c_device *i3c)
326 struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
328 /* We my have received a Bus Remove notify prior to device remove,
329 * so mi will already be removed.
334 mutex_lock(&busdevs_lock);
335 mctp_i3c_remove_device(mi);
336 mutex_unlock(&busdevs_lock);
339 /* Returns the device for an address, with mi->lock held */
340 static struct mctp_i3c_device *
341 mctp_i3c_lookup(struct mctp_i3c_bus *mbus, u64 pid)
343 struct mctp_i3c_device *mi = NULL, *ret = NULL;
345 mutex_lock(&busdevs_lock);
346 list_for_each_entry(mi, &mbus->devs, list)
347 if (mi->pid == pid) {
349 mutex_lock(&mi->lock);
352 mutex_unlock(&busdevs_lock);
356 static void mctp_i3c_xmit(struct mctp_i3c_bus *mbus, struct sk_buff *skb)
358 struct net_device_stats *stats = &mbus->ndev->stats;
359 struct i3c_priv_xfer xfer = { .rnw = false };
360 struct mctp_i3c_internal_hdr *ihdr = NULL;
361 struct mctp_i3c_device *mi = NULL;
362 unsigned int data_len;
368 skb_pull(skb, sizeof(struct mctp_i3c_internal_hdr));
371 ihdr = (void *)skb_mac_header(skb);
373 pid = get_unaligned_be48(ihdr->dest);
374 mi = mctp_i3c_lookup(mbus, pid);
376 /* I3C endpoint went away after the packet was enqueued? */
381 if (WARN_ON_ONCE(data_len + 1 > MCTP_I3C_MAXBUF))
384 if (data_len + 1 > (unsigned int)mi->mwl) {
385 /* Route MTU was larger than supported by the endpoint */
390 /* Need a linear buffer with space for the PEC */
391 xfer.len = data_len + 1;
392 if (skb_tailroom(skb) >= 1) {
396 /* Otherwise need to copy the buffer */
397 skb_copy_bits(skb, 0, mbus->tx_scratch, skb->len);
398 data = mbus->tx_scratch;
401 /* PEC calculation */
402 addr = mi->addr << 1;
403 pec = i2c_smbus_pec(0, &addr, 1);
404 pec = i2c_smbus_pec(pec, data, data_len);
405 data[data_len] = pec;
407 xfer.data.out = data;
408 rc = i3c_device_do_priv_xfers(mi->i3c, &xfer, 1);
410 stats->tx_bytes += data_len;
418 mutex_unlock(&mi->lock);
421 static int mctp_i3c_tx_thread(void *data)
423 struct mctp_i3c_bus *mbus = data;
427 if (kthread_should_stop())
430 spin_lock_bh(&mbus->tx_lock);
433 spin_unlock_bh(&mbus->tx_lock);
435 if (netif_queue_stopped(mbus->ndev))
436 netif_wake_queue(mbus->ndev);
439 mctp_i3c_xmit(mbus, skb);
442 wait_event_idle(mbus->tx_wq,
443 mbus->tx_skb || kthread_should_stop());
450 static netdev_tx_t mctp_i3c_start_xmit(struct sk_buff *skb,
451 struct net_device *ndev)
453 struct mctp_i3c_bus *mbus = netdev_priv(ndev);
456 spin_lock(&mbus->tx_lock);
457 netif_stop_queue(ndev);
459 dev_warn_ratelimited(&ndev->dev, "TX with queue stopped");
460 ret = NETDEV_TX_BUSY;
465 spin_unlock(&mbus->tx_lock);
467 if (ret == NETDEV_TX_OK)
468 wake_up(&mbus->tx_wq);
473 static void mctp_i3c_bus_free(struct mctp_i3c_bus *mbus)
474 __must_hold(&busdevs_lock)
476 struct mctp_i3c_device *mi = NULL, *tmp = NULL;
478 if (mbus->tx_thread) {
479 kthread_stop(mbus->tx_thread);
480 mbus->tx_thread = NULL;
483 /* Remove any child devices */
484 list_for_each_entry_safe(mi, tmp, &mbus->devs, list) {
485 mctp_i3c_remove_device(mi);
488 kfree_skb(mbus->tx_skb);
489 list_del(&mbus->list);
492 static void mctp_i3c_ndo_uninit(struct net_device *ndev)
494 struct mctp_i3c_bus *mbus = netdev_priv(ndev);
496 /* Perform cleanup here to ensure there are no remaining references */
497 mctp_i3c_bus_free(mbus);
500 static int mctp_i3c_header_create(struct sk_buff *skb, struct net_device *dev,
501 unsigned short type, const void *daddr,
502 const void *saddr, unsigned int len)
504 struct mctp_i3c_internal_hdr *ihdr;
506 skb_push(skb, sizeof(struct mctp_i3c_internal_hdr));
507 skb_reset_mac_header(skb);
508 ihdr = (void *)skb_mac_header(skb);
509 memcpy(ihdr->dest, daddr, PID_SIZE);
510 memcpy(ihdr->source, saddr, PID_SIZE);
514 static const struct net_device_ops mctp_i3c_ops = {
515 .ndo_start_xmit = mctp_i3c_start_xmit,
516 .ndo_uninit = mctp_i3c_ndo_uninit,
519 static const struct header_ops mctp_i3c_headops = {
520 .create = mctp_i3c_header_create,
523 static void mctp_i3c_net_setup(struct net_device *dev)
525 dev->type = ARPHRD_MCTP;
527 dev->mtu = MCTP_I3C_MAXMTU;
528 dev->min_mtu = MCTP_I3C_MINMTU;
529 dev->max_mtu = MCTP_I3C_MAXMTU;
530 dev->tx_queue_len = MCTP_I3C_TX_QUEUE_LEN;
532 dev->hard_header_len = sizeof(struct mctp_i3c_internal_hdr);
533 dev->addr_len = PID_SIZE;
535 dev->netdev_ops = &mctp_i3c_ops;
536 dev->header_ops = &mctp_i3c_headops;
539 static bool mctp_i3c_is_mctp_controller(struct i3c_bus *bus)
541 struct i3c_dev_desc *master = bus->cur_master;
546 return of_property_read_bool(master->common.master->dev.of_node,
550 /* Returns the Provisioned Id of a local bus master */
551 static int mctp_i3c_bus_local_pid(struct i3c_bus *bus, u64 *ret_pid)
553 struct i3c_dev_desc *master;
555 master = bus->cur_master;
556 if (WARN_ON_ONCE(!master))
558 *ret_pid = master->info.pid;
563 /* Returns an ERR_PTR on failure */
564 static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
565 __must_hold(&busdevs_lock)
567 struct mctp_i3c_bus *mbus = NULL;
568 struct net_device *ndev = NULL;
569 char namebuf[IFNAMSIZ];
573 if (!mctp_i3c_is_mctp_controller(bus))
574 return ERR_PTR(-ENOENT);
576 snprintf(namebuf, sizeof(namebuf), "mctpi3c%d", bus->id);
577 ndev = alloc_netdev(sizeof(*mbus), namebuf, NET_NAME_ENUM,
584 mbus = netdev_priv(ndev);
587 INIT_LIST_HEAD(&mbus->devs);
588 list_add(&mbus->list, &busdevs);
590 rc = mctp_i3c_bus_local_pid(bus, &mbus->pid);
592 dev_err(&ndev->dev, "No I3C PID available\n");
593 goto err_free_uninit;
595 put_unaligned_be48(mbus->pid, addr);
596 dev_addr_set(ndev, addr);
598 init_waitqueue_head(&mbus->tx_wq);
599 spin_lock_init(&mbus->tx_lock);
600 mbus->tx_thread = kthread_run(mctp_i3c_tx_thread, mbus,
601 "%s/tx", ndev->name);
602 if (IS_ERR(mbus->tx_thread)) {
603 dev_warn(&ndev->dev, "Error creating thread: %pe\n",
605 rc = PTR_ERR(mbus->tx_thread);
606 mbus->tx_thread = NULL;
607 goto err_free_uninit;
610 rc = mctp_register_netdev(ndev, NULL);
612 dev_warn(&ndev->dev, "netdev register failed: %d\n", rc);
613 goto err_free_netdev;
618 /* uninit will not get called if a netdev has not been registered,
619 * so we perform the same mbus cleanup manually.
621 mctp_i3c_bus_free(mbus);
630 static void mctp_i3c_bus_remove(struct mctp_i3c_bus *mbus)
631 __must_hold(&busdevs_lock)
633 /* Unregister calls through to ndo_uninit -> mctp_i3c_bus_free() */
634 mctp_unregister_netdev(mbus->ndev);
636 free_netdev(mbus->ndev);
637 /* mbus is deallocated */
640 /* Removes all mctp-i3c busses */
641 static void mctp_i3c_bus_remove_all(void)
643 struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
645 mutex_lock(&busdevs_lock);
646 list_for_each_entry_safe(mbus, tmp, &busdevs, list) {
647 mctp_i3c_bus_remove(mbus);
649 mutex_unlock(&busdevs_lock);
652 /* Adds a i3c_bus if it isn't already in the busdevs list.
653 * Suitable as an i3c_for_each_bus_locked callback.
655 static int mctp_i3c_bus_add_new(struct i3c_bus *bus, void *data)
657 struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
660 mutex_lock(&busdevs_lock);
661 list_for_each_entry_safe(mbus, tmp, &busdevs, list)
662 if (mbus->bus == bus)
665 /* It is OK for a bus to already exist. That can occur due to
666 * the race in mod_init between notifier and for_each_bus
669 mctp_i3c_bus_add(bus);
670 mutex_unlock(&busdevs_lock);
674 static void mctp_i3c_notify_bus_remove(struct i3c_bus *bus)
676 struct mctp_i3c_bus *mbus = NULL, *tmp;
678 mutex_lock(&busdevs_lock);
679 list_for_each_entry_safe(mbus, tmp, &busdevs, list)
680 if (mbus->bus == bus)
681 mctp_i3c_bus_remove(mbus);
682 mutex_unlock(&busdevs_lock);
685 static int mctp_i3c_notifier_call(struct notifier_block *nb,
686 unsigned long action, void *data)
689 case I3C_NOTIFY_BUS_ADD:
690 mctp_i3c_bus_add_new((struct i3c_bus *)data, NULL);
692 case I3C_NOTIFY_BUS_REMOVE:
693 mctp_i3c_notify_bus_remove((struct i3c_bus *)data);
699 static struct notifier_block mctp_i3c_notifier = {
700 .notifier_call = mctp_i3c_notifier_call,
703 static const struct i3c_device_id mctp_i3c_ids[] = {
704 I3C_CLASS(I3C_DCR_MCTP, NULL),
708 static struct i3c_driver mctp_i3c_driver = {
712 .probe = mctp_i3c_probe,
713 .remove = mctp_i3c_remove,
714 .id_table = mctp_i3c_ids,
717 static __init int mctp_i3c_mod_init(void)
721 rc = i3c_register_notifier(&mctp_i3c_notifier);
723 i3c_driver_unregister(&mctp_i3c_driver);
727 i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL);
729 rc = i3c_driver_register(&mctp_i3c_driver);
736 static __exit void mctp_i3c_mod_exit(void)
740 i3c_driver_unregister(&mctp_i3c_driver);
742 rc = i3c_unregister_notifier(&mctp_i3c_notifier);
744 pr_warn("MCTP I3C could not unregister notifier, %d\n", rc);
746 mctp_i3c_bus_remove_all();
749 module_init(mctp_i3c_mod_init);
750 module_exit(mctp_i3c_mod_exit);
752 MODULE_DEVICE_TABLE(i3c, mctp_i3c_ids);
753 MODULE_DESCRIPTION("MCTP I3C device");
754 MODULE_LICENSE("GPL");
755 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");