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 <linux/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 /* Make sure netif_rx() is read in the same order as i3c. */
129 mutex_lock(&mi
->lock
);
130 rc
= i3c_device_do_priv_xfers(mi
->i3c
, &xfer
, 1);
134 if (WARN_ON_ONCE(xfer
.len
> mi
->mrl
)) {
135 /* Bad i3c bus driver */
139 if (xfer
.len
< MCTP_I3C_MINLEN
) {
140 stats
->rx_length_errors
++;
145 /* check PEC, including address byte */
146 addr
= mi
->addr
<< 1 | 1;
147 pec
= i2c_smbus_pec(0, &addr
, 1);
148 pec
= i2c_smbus_pec(pec
, xfer
.data
.in
, xfer
.len
- 1);
149 if (pec
!= ((u8
*)xfer
.data
.in
)[xfer
.len
- 1]) {
150 stats
->rx_crc_errors
++;
156 skb_trim(skb
, xfer
.len
- 1);
159 cb
->halen
= PID_SIZE
;
160 put_unaligned_be48(mi
->pid
, cb
->haddr
);
162 net_status
= netif_rx(skb
);
164 if (net_status
== NET_RX_SUCCESS
) {
166 stats
->rx_bytes
+= xfer
.len
- 1;
171 mutex_unlock(&mi
->lock
);
174 mutex_unlock(&mi
->lock
);
179 static void mctp_i3c_ibi_handler(struct i3c_device
*i3c
,
180 const struct i3c_ibi_payload
*payload
)
182 struct mctp_i3c_device
*mi
= i3cdev_get_drvdata(i3c
);
184 if (WARN_ON_ONCE(!mi
))
188 if (payload
->len
> 0) {
189 if (((u8
*)payload
->data
)[0] != I3C_MDB_MCTP
) {
190 /* Not a mctp-i3c interrupt, ignore it */
194 /* The BCR advertised a Mandatory Data Byte but the
195 * device didn't send one.
197 dev_warn_once(i3cdev_to_dev(i3c
), "IBI with missing MDB");
204 static int mctp_i3c_setup(struct mctp_i3c_device
*mi
)
206 const struct i3c_ibi_setup ibi
= {
207 .max_payload_len
= 1,
208 .num_slots
= MCTP_I3C_IBI_SLOTS
,
209 .handler
= mctp_i3c_ibi_handler
,
211 struct i3c_device_info info
;
214 i3c_device_get_info(mi
->i3c
, &info
);
215 mi
->have_mdb
= info
.bcr
& BIT(2);
216 mi
->addr
= info
.dyn_addr
;
217 mi
->mwl
= info
.max_write_len
;
218 mi
->mrl
= info
.max_read_len
;
221 rc
= i3c_device_request_ibi(mi
->i3c
, &ibi
);
222 if (rc
== -ENOTSUPP
) {
223 /* This driver only supports In-Band Interrupt mode.
224 * Support for Polling Mode could be added if required.
225 * (ENOTSUPP is from the i3c layer, not EOPNOTSUPP).
227 dev_warn(i3cdev_to_dev(mi
->i3c
),
228 "Failed, bus driver doesn't support In-Band Interrupts");
231 dev_err(i3cdev_to_dev(mi
->i3c
),
232 "Failed requesting IBI (%d)\n", rc
);
236 rc
= i3c_device_enable_ibi(mi
->i3c
);
238 /* Assume a driver supporting request_ibi also
239 * supports enable_ibi.
241 dev_err(i3cdev_to_dev(mi
->i3c
), "Failed enabling IBI (%d)\n", rc
);
248 i3c_device_free_ibi(mi
->i3c
);
254 /* Adds a new MCTP i3c_device to a bus */
255 static int mctp_i3c_add_device(struct mctp_i3c_bus
*mbus
,
256 struct i3c_device
*i3c
)
257 __must_hold(&busdevs_lock
)
259 struct mctp_i3c_device
*mi
= NULL
;
262 mi
= kzalloc(sizeof(*mi
), GFP_KERNEL
);
269 mutex_init(&mi
->lock
);
270 list_add(&mi
->list
, &mbus
->devs
);
272 i3cdev_set_drvdata(i3c
, mi
);
273 rc
= mctp_i3c_setup(mi
);
284 dev_warn(i3cdev_to_dev(i3c
), "Error adding mctp-i3c device, %d\n", rc
);
288 static int mctp_i3c_probe(struct i3c_device
*i3c
)
290 struct mctp_i3c_bus
*b
= NULL
, *mbus
= NULL
;
292 /* Look for a known bus */
293 mutex_lock(&busdevs_lock
);
294 list_for_each_entry(b
, &busdevs
, list
)
295 if (b
->bus
== i3c
->bus
) {
299 mutex_unlock(&busdevs_lock
);
302 /* probably no "mctp-controller" property on the i3c bus */
306 return mctp_i3c_add_device(mbus
, i3c
);
309 static void mctp_i3c_remove_device(struct mctp_i3c_device
*mi
)
310 __must_hold(&busdevs_lock
)
312 /* Ensure the tx thread isn't using the device */
313 mutex_lock(&mi
->lock
);
315 /* Counterpart of mctp_i3c_setup */
316 i3c_device_disable_ibi(mi
->i3c
);
317 i3c_device_free_ibi(mi
->i3c
);
319 /* Counterpart of mctp_i3c_add_device */
320 i3cdev_set_drvdata(mi
->i3c
, NULL
);
323 /* Safe to unlock after removing from the list */
324 mutex_unlock(&mi
->lock
);
328 static void mctp_i3c_remove(struct i3c_device
*i3c
)
330 struct mctp_i3c_device
*mi
= i3cdev_get_drvdata(i3c
);
332 /* We my have received a Bus Remove notify prior to device remove,
333 * so mi will already be removed.
338 mutex_lock(&busdevs_lock
);
339 mctp_i3c_remove_device(mi
);
340 mutex_unlock(&busdevs_lock
);
343 /* Returns the device for an address, with mi->lock held */
344 static struct mctp_i3c_device
*
345 mctp_i3c_lookup(struct mctp_i3c_bus
*mbus
, u64 pid
)
347 struct mctp_i3c_device
*mi
= NULL
, *ret
= NULL
;
349 mutex_lock(&busdevs_lock
);
350 list_for_each_entry(mi
, &mbus
->devs
, list
)
351 if (mi
->pid
== pid
) {
353 mutex_lock(&mi
->lock
);
356 mutex_unlock(&busdevs_lock
);
360 static void mctp_i3c_xmit(struct mctp_i3c_bus
*mbus
, struct sk_buff
*skb
)
362 struct net_device_stats
*stats
= &mbus
->ndev
->stats
;
363 struct i3c_priv_xfer xfer
= { .rnw
= false };
364 struct mctp_i3c_internal_hdr
*ihdr
= NULL
;
365 struct mctp_i3c_device
*mi
= NULL
;
366 unsigned int data_len
;
372 skb_pull(skb
, sizeof(struct mctp_i3c_internal_hdr
));
375 ihdr
= (void *)skb_mac_header(skb
);
377 pid
= get_unaligned_be48(ihdr
->dest
);
378 mi
= mctp_i3c_lookup(mbus
, pid
);
380 /* I3C endpoint went away after the packet was enqueued? */
385 if (WARN_ON_ONCE(data_len
+ 1 > MCTP_I3C_MAXBUF
))
388 if (data_len
+ 1 > (unsigned int)mi
->mwl
) {
389 /* Route MTU was larger than supported by the endpoint */
394 /* Need a linear buffer with space for the PEC */
395 xfer
.len
= data_len
+ 1;
396 if (skb_tailroom(skb
) >= 1) {
400 /* Otherwise need to copy the buffer */
401 skb_copy_bits(skb
, 0, mbus
->tx_scratch
, skb
->len
);
402 data
= mbus
->tx_scratch
;
405 /* PEC calculation */
406 addr
= mi
->addr
<< 1;
407 pec
= i2c_smbus_pec(0, &addr
, 1);
408 pec
= i2c_smbus_pec(pec
, data
, data_len
);
409 data
[data_len
] = pec
;
411 xfer
.data
.out
= data
;
412 rc
= i3c_device_do_priv_xfers(mi
->i3c
, &xfer
, 1);
414 stats
->tx_bytes
+= data_len
;
422 mutex_unlock(&mi
->lock
);
425 static int mctp_i3c_tx_thread(void *data
)
427 struct mctp_i3c_bus
*mbus
= data
;
431 if (kthread_should_stop())
434 spin_lock_bh(&mbus
->tx_lock
);
437 spin_unlock_bh(&mbus
->tx_lock
);
439 if (netif_queue_stopped(mbus
->ndev
))
440 netif_wake_queue(mbus
->ndev
);
443 mctp_i3c_xmit(mbus
, skb
);
446 wait_event_idle(mbus
->tx_wq
,
447 mbus
->tx_skb
|| kthread_should_stop());
454 static netdev_tx_t
mctp_i3c_start_xmit(struct sk_buff
*skb
,
455 struct net_device
*ndev
)
457 struct mctp_i3c_bus
*mbus
= netdev_priv(ndev
);
460 spin_lock(&mbus
->tx_lock
);
461 netif_stop_queue(ndev
);
463 dev_warn_ratelimited(&ndev
->dev
, "TX with queue stopped");
464 ret
= NETDEV_TX_BUSY
;
469 spin_unlock(&mbus
->tx_lock
);
471 if (ret
== NETDEV_TX_OK
)
472 wake_up(&mbus
->tx_wq
);
477 static void mctp_i3c_bus_free(struct mctp_i3c_bus
*mbus
)
478 __must_hold(&busdevs_lock
)
480 struct mctp_i3c_device
*mi
= NULL
, *tmp
= NULL
;
482 if (mbus
->tx_thread
) {
483 kthread_stop(mbus
->tx_thread
);
484 mbus
->tx_thread
= NULL
;
487 /* Remove any child devices */
488 list_for_each_entry_safe(mi
, tmp
, &mbus
->devs
, list
) {
489 mctp_i3c_remove_device(mi
);
492 kfree_skb(mbus
->tx_skb
);
493 list_del(&mbus
->list
);
496 static void mctp_i3c_ndo_uninit(struct net_device
*ndev
)
498 struct mctp_i3c_bus
*mbus
= netdev_priv(ndev
);
500 /* Perform cleanup here to ensure there are no remaining references */
501 mctp_i3c_bus_free(mbus
);
504 static int mctp_i3c_header_create(struct sk_buff
*skb
, struct net_device
*dev
,
505 unsigned short type
, const void *daddr
,
506 const void *saddr
, unsigned int len
)
508 struct mctp_i3c_internal_hdr
*ihdr
;
510 skb_push(skb
, sizeof(struct mctp_i3c_internal_hdr
));
511 skb_reset_mac_header(skb
);
512 ihdr
= (void *)skb_mac_header(skb
);
513 memcpy(ihdr
->dest
, daddr
, PID_SIZE
);
514 memcpy(ihdr
->source
, saddr
, PID_SIZE
);
518 static const struct net_device_ops mctp_i3c_ops
= {
519 .ndo_start_xmit
= mctp_i3c_start_xmit
,
520 .ndo_uninit
= mctp_i3c_ndo_uninit
,
523 static const struct header_ops mctp_i3c_headops
= {
524 .create
= mctp_i3c_header_create
,
527 static void mctp_i3c_net_setup(struct net_device
*dev
)
529 dev
->type
= ARPHRD_MCTP
;
531 dev
->mtu
= MCTP_I3C_MAXMTU
;
532 dev
->min_mtu
= MCTP_I3C_MINMTU
;
533 dev
->max_mtu
= MCTP_I3C_MAXMTU
;
534 dev
->tx_queue_len
= MCTP_I3C_TX_QUEUE_LEN
;
536 dev
->hard_header_len
= sizeof(struct mctp_i3c_internal_hdr
);
537 dev
->addr_len
= PID_SIZE
;
539 dev
->netdev_ops
= &mctp_i3c_ops
;
540 dev
->header_ops
= &mctp_i3c_headops
;
543 static bool mctp_i3c_is_mctp_controller(struct i3c_bus
*bus
)
545 struct i3c_dev_desc
*master
= bus
->cur_master
;
550 return of_property_read_bool(master
->common
.master
->dev
.of_node
,
554 /* Returns the Provisioned Id of a local bus master */
555 static int mctp_i3c_bus_local_pid(struct i3c_bus
*bus
, u64
*ret_pid
)
557 struct i3c_dev_desc
*master
;
559 master
= bus
->cur_master
;
560 if (WARN_ON_ONCE(!master
))
562 *ret_pid
= master
->info
.pid
;
567 /* Returns an ERR_PTR on failure */
568 static struct mctp_i3c_bus
*mctp_i3c_bus_add(struct i3c_bus
*bus
)
569 __must_hold(&busdevs_lock
)
571 struct mctp_i3c_bus
*mbus
= NULL
;
572 struct net_device
*ndev
= NULL
;
573 char namebuf
[IFNAMSIZ
];
577 if (!mctp_i3c_is_mctp_controller(bus
))
578 return ERR_PTR(-ENOENT
);
580 snprintf(namebuf
, sizeof(namebuf
), "mctpi3c%d", bus
->id
);
581 ndev
= alloc_netdev(sizeof(*mbus
), namebuf
, NET_NAME_ENUM
,
588 mbus
= netdev_priv(ndev
);
591 INIT_LIST_HEAD(&mbus
->devs
);
592 list_add(&mbus
->list
, &busdevs
);
594 rc
= mctp_i3c_bus_local_pid(bus
, &mbus
->pid
);
596 dev_err(&ndev
->dev
, "No I3C PID available\n");
597 goto err_free_uninit
;
599 put_unaligned_be48(mbus
->pid
, addr
);
600 dev_addr_set(ndev
, addr
);
602 init_waitqueue_head(&mbus
->tx_wq
);
603 spin_lock_init(&mbus
->tx_lock
);
604 mbus
->tx_thread
= kthread_run(mctp_i3c_tx_thread
, mbus
,
605 "%s/tx", ndev
->name
);
606 if (IS_ERR(mbus
->tx_thread
)) {
607 dev_warn(&ndev
->dev
, "Error creating thread: %pe\n",
609 rc
= PTR_ERR(mbus
->tx_thread
);
610 mbus
->tx_thread
= NULL
;
611 goto err_free_uninit
;
614 rc
= mctp_register_netdev(ndev
, NULL
, MCTP_PHYS_BINDING_I3C
);
616 dev_warn(&ndev
->dev
, "netdev register failed: %d\n", rc
);
617 goto err_free_netdev
;
622 /* uninit will not get called if a netdev has not been registered,
623 * so we perform the same mbus cleanup manually.
625 mctp_i3c_bus_free(mbus
);
634 static void mctp_i3c_bus_remove(struct mctp_i3c_bus
*mbus
)
635 __must_hold(&busdevs_lock
)
637 /* Unregister calls through to ndo_uninit -> mctp_i3c_bus_free() */
638 mctp_unregister_netdev(mbus
->ndev
);
640 free_netdev(mbus
->ndev
);
641 /* mbus is deallocated */
644 /* Removes all mctp-i3c busses */
645 static void mctp_i3c_bus_remove_all(void)
647 struct mctp_i3c_bus
*mbus
= NULL
, *tmp
= NULL
;
649 mutex_lock(&busdevs_lock
);
650 list_for_each_entry_safe(mbus
, tmp
, &busdevs
, list
) {
651 mctp_i3c_bus_remove(mbus
);
653 mutex_unlock(&busdevs_lock
);
656 /* Adds a i3c_bus if it isn't already in the busdevs list.
657 * Suitable as an i3c_for_each_bus_locked callback.
659 static int mctp_i3c_bus_add_new(struct i3c_bus
*bus
, void *data
)
661 struct mctp_i3c_bus
*mbus
= NULL
, *tmp
= NULL
;
664 mutex_lock(&busdevs_lock
);
665 list_for_each_entry_safe(mbus
, tmp
, &busdevs
, list
)
666 if (mbus
->bus
== bus
)
669 /* It is OK for a bus to already exist. That can occur due to
670 * the race in mod_init between notifier and for_each_bus
673 mctp_i3c_bus_add(bus
);
674 mutex_unlock(&busdevs_lock
);
678 static void mctp_i3c_notify_bus_remove(struct i3c_bus
*bus
)
680 struct mctp_i3c_bus
*mbus
= NULL
, *tmp
;
682 mutex_lock(&busdevs_lock
);
683 list_for_each_entry_safe(mbus
, tmp
, &busdevs
, list
)
684 if (mbus
->bus
== bus
)
685 mctp_i3c_bus_remove(mbus
);
686 mutex_unlock(&busdevs_lock
);
689 static int mctp_i3c_notifier_call(struct notifier_block
*nb
,
690 unsigned long action
, void *data
)
693 case I3C_NOTIFY_BUS_ADD
:
694 mctp_i3c_bus_add_new((struct i3c_bus
*)data
, NULL
);
696 case I3C_NOTIFY_BUS_REMOVE
:
697 mctp_i3c_notify_bus_remove((struct i3c_bus
*)data
);
703 static struct notifier_block mctp_i3c_notifier
= {
704 .notifier_call
= mctp_i3c_notifier_call
,
707 static const struct i3c_device_id mctp_i3c_ids
[] = {
708 I3C_CLASS(I3C_DCR_MCTP
, NULL
),
712 static struct i3c_driver mctp_i3c_driver
= {
716 .probe
= mctp_i3c_probe
,
717 .remove
= mctp_i3c_remove
,
718 .id_table
= mctp_i3c_ids
,
721 static __init
int mctp_i3c_mod_init(void)
725 rc
= i3c_register_notifier(&mctp_i3c_notifier
);
727 i3c_driver_unregister(&mctp_i3c_driver
);
731 i3c_for_each_bus_locked(mctp_i3c_bus_add_new
, NULL
);
733 rc
= i3c_driver_register(&mctp_i3c_driver
);
740 static __exit
void mctp_i3c_mod_exit(void)
744 i3c_driver_unregister(&mctp_i3c_driver
);
746 rc
= i3c_unregister_notifier(&mctp_i3c_notifier
);
748 pr_warn("MCTP I3C could not unregister notifier, %d\n", rc
);
750 mctp_i3c_bus_remove_all();
753 module_init(mctp_i3c_mod_init
);
754 module_exit(mctp_i3c_mod_exit
);
756 MODULE_DEVICE_TABLE(i3c
, mctp_i3c_ids
);
757 MODULE_DESCRIPTION("MCTP I3C device");
758 MODULE_LICENSE("GPL");
759 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");