xref: /linux/drivers/net/mctp/mctp-i3c.c (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implements DMTF specification
4  * "DSP0233 Management Component Transport Protocol (MCTP) I3C Transport
5  * Binding"
6  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0233_1.0.0.pdf
7  *
8  * Copyright (c) 2023 Code Construct
9  */
10 
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>
17 #include <net/mctp.h>
18 #include <net/mctpdevice.h>
19 
20 #define MCTP_I3C_MAXBUF 65536
21 /* 48 bit Provisioned Id */
22 #define PID_SIZE 6
23 
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;
30 
31 /* Sufficient for 64kB at min mtu */
32 static const int MCTP_I3C_TX_QUEUE_LEN = 1100;
33 
34 /* Somewhat arbitrary */
35 static const int MCTP_I3C_IBI_SLOTS = 8;
36 
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
41 
42 static const char *MCTP_I3C_OF_PROP = "mctp-controller";
43 
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);
48 
49 struct mctp_i3c_bus {
50 	struct net_device *ndev;
51 
52 	struct task_struct *tx_thread;
53 	wait_queue_head_t tx_wq;
54 	/* tx_lock protects tx_skb and devs */
55 	spinlock_t tx_lock;
56 	/* Next skb to transmit */
57 	struct sk_buff *tx_skb;
58 	/* Scratch buffer for xmit */
59 	u8 tx_scratch[MCTP_I3C_MAXBUF];
60 
61 	/* Element of busdevs */
62 	struct list_head list;
63 
64 	/* Provisioned ID of our controller */
65 	u64 pid;
66 
67 	struct i3c_bus *bus;
68 	/* Head of mctp_i3c_device.list. Protected by busdevs_lock */
69 	struct list_head devs;
70 };
71 
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 */
76 
77 	/* Held while tx_thread is using this device */
78 	struct mutex lock;
79 
80 	/* Whether BCR indicates MDB is present in IBI */
81 	bool have_mdb;
82 	/* I3C dynamic address */
83 	u8 addr;
84 	/* Maximum read length */
85 	u16 mrl;
86 	/* Maximum write length */
87 	u16 mwl;
88 	/* Provisioned ID */
89 	u64 pid;
90 };
91 
92 /* We synthesise a mac header using the Provisioned ID.
93  * Used to pass dest to mctp_i3c_start_xmit.
94  */
95 struct mctp_i3c_internal_hdr {
96 	u8 dest[PID_SIZE];
97 	u8 source[PID_SIZE];
98 } __packed;
99 
mctp_i3c_read(struct mctp_i3c_device * mi)100 static int mctp_i3c_read(struct mctp_i3c_device *mi)
101 {
102 	struct i3c_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;
107 	int net_status, rc;
108 	u8 pec, addr;
109 
110 	skb = netdev_alloc_skb(mi->mbus->ndev,
111 			       mi->mrl + sizeof(struct mctp_i3c_internal_hdr));
112 	if (!skb) {
113 		stats->rx_dropped++;
114 		rc = -ENOMEM;
115 		goto err;
116 	}
117 
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));
125 
126 	xfer.data.in = skb_put(skb, mi->mrl);
127 
128 	/* Make sure netif_rx() is read in the same order as i3c. */
129 	mutex_lock(&mi->lock);
130 	rc = i3c_device_do_xfers(mi->i3c, &xfer, 1, I3C_SDR);
131 	if (rc < 0)
132 		goto err;
133 
134 	if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
135 		/* Bad i3c bus driver */
136 		rc = -EIO;
137 		goto err;
138 	}
139 	if (xfer.len < MCTP_I3C_MINLEN) {
140 		stats->rx_length_errors++;
141 		rc = -EIO;
142 		goto err;
143 	}
144 
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++;
151 		rc = -EINVAL;
152 		goto err;
153 	}
154 
155 	/* Remove PEC */
156 	skb_trim(skb, xfer.len - 1);
157 
158 	cb = __mctp_cb(skb);
159 	cb->halen = PID_SIZE;
160 	put_unaligned_be48(mi->pid, cb->haddr);
161 
162 	net_status = netif_rx(skb);
163 
164 	if (net_status == NET_RX_SUCCESS) {
165 		stats->rx_packets++;
166 		stats->rx_bytes += xfer.len - 1;
167 	} else {
168 		stats->rx_dropped++;
169 	}
170 
171 	mutex_unlock(&mi->lock);
172 	return 0;
173 err:
174 	mutex_unlock(&mi->lock);
175 	kfree_skb(skb);
176 	return rc;
177 }
178 
mctp_i3c_ibi_handler(struct i3c_device * i3c,const struct i3c_ibi_payload * payload)179 static void mctp_i3c_ibi_handler(struct i3c_device *i3c,
180 				 const struct i3c_ibi_payload *payload)
181 {
182 	struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
183 
184 	if (WARN_ON_ONCE(!mi))
185 		return;
186 
187 	if (mi->have_mdb) {
188 		if (payload->len > 0) {
189 			if (((u8 *)payload->data)[0] != I3C_MDB_MCTP) {
190 				/* Not a mctp-i3c interrupt, ignore it */
191 				return;
192 			}
193 		} else {
194 			/* The BCR advertised a Mandatory Data Byte but the
195 			 * device didn't send one.
196 			 */
197 			dev_warn_once(i3cdev_to_dev(i3c), "IBI with missing MDB");
198 		}
199 	}
200 
201 	mctp_i3c_read(mi);
202 }
203 
mctp_i3c_setup(struct mctp_i3c_device * mi)204 static int mctp_i3c_setup(struct mctp_i3c_device *mi)
205 {
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,
210 	};
211 	struct i3c_device_info info;
212 	int rc;
213 
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;
219 	mi->pid = info.pid;
220 
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).
226 		 */
227 		dev_warn(i3cdev_to_dev(mi->i3c),
228 			 "Failed, bus driver doesn't support In-Band Interrupts");
229 		goto err;
230 	} else if (rc < 0) {
231 		dev_err(i3cdev_to_dev(mi->i3c),
232 			"Failed requesting IBI (%d)\n", rc);
233 		goto err;
234 	}
235 
236 	rc = i3c_device_enable_ibi(mi->i3c);
237 	if (rc < 0) {
238 		/* Assume a driver supporting request_ibi also
239 		 * supports enable_ibi.
240 		 */
241 		dev_err(i3cdev_to_dev(mi->i3c), "Failed enabling IBI (%d)\n", rc);
242 		goto err_free_ibi;
243 	}
244 
245 	return 0;
246 
247 err_free_ibi:
248 	i3c_device_free_ibi(mi->i3c);
249 
250 err:
251 	return rc;
252 }
253 
254 /* Adds a new MCTP i3c_device to a bus */
mctp_i3c_add_device(struct mctp_i3c_bus * mbus,struct i3c_device * i3c)255 static int mctp_i3c_add_device(struct mctp_i3c_bus *mbus,
256 			       struct i3c_device *i3c)
257 __must_hold(&busdevs_lock)
258 {
259 	struct mctp_i3c_device *mi = NULL;
260 	int rc;
261 
262 	mi = kzalloc_obj(*mi);
263 	if (!mi) {
264 		rc = -ENOMEM;
265 		goto err;
266 	}
267 	mi->mbus = mbus;
268 	mi->i3c = i3c;
269 	mutex_init(&mi->lock);
270 	list_add(&mi->list, &mbus->devs);
271 
272 	i3cdev_set_drvdata(i3c, mi);
273 	rc = mctp_i3c_setup(mi);
274 	if (rc < 0)
275 		goto err_free;
276 
277 	return 0;
278 
279 err_free:
280 	list_del(&mi->list);
281 	kfree(mi);
282 
283 err:
284 	dev_warn(i3cdev_to_dev(i3c), "Error adding mctp-i3c device, %d\n", rc);
285 	return rc;
286 }
287 
mctp_i3c_probe(struct i3c_device * i3c)288 static int mctp_i3c_probe(struct i3c_device *i3c)
289 {
290 	struct mctp_i3c_bus *b = NULL, *mbus = NULL;
291 	int rc;
292 
293 	/* Look for a known bus */
294 	mutex_lock(&busdevs_lock);
295 	list_for_each_entry(b, &busdevs, list)
296 		if (b->bus == i3c->bus) {
297 			mbus = b;
298 			break;
299 		}
300 
301 	if (!mbus) {
302 		/* probably no "mctp-controller" property on the i3c bus */
303 		rc = -ENODEV;
304 	} else {
305 		rc = mctp_i3c_add_device(mbus, i3c);
306 	}
307 	mutex_unlock(&busdevs_lock);
308 
309 	return rc;
310 }
311 
mctp_i3c_remove_device(struct mctp_i3c_device * mi)312 static void mctp_i3c_remove_device(struct mctp_i3c_device *mi)
313 __must_hold(&busdevs_lock)
314 {
315 	/* Ensure the tx thread isn't using the device */
316 	mutex_lock(&mi->lock);
317 
318 	/* Counterpart of mctp_i3c_setup */
319 	i3c_device_disable_ibi(mi->i3c);
320 	i3c_device_free_ibi(mi->i3c);
321 
322 	/* Counterpart of mctp_i3c_add_device */
323 	i3cdev_set_drvdata(mi->i3c, NULL);
324 	list_del(&mi->list);
325 
326 	/* Safe to unlock after removing from the list */
327 	mutex_unlock(&mi->lock);
328 	kfree(mi);
329 }
330 
mctp_i3c_remove(struct i3c_device * i3c)331 static void mctp_i3c_remove(struct i3c_device *i3c)
332 {
333 	struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
334 
335 	/* We my have received a Bus Remove notify prior to device remove,
336 	 * so mi will already be removed.
337 	 */
338 	if (!mi)
339 		return;
340 
341 	mutex_lock(&busdevs_lock);
342 	mctp_i3c_remove_device(mi);
343 	mutex_unlock(&busdevs_lock);
344 }
345 
346 /* Returns the device for an address, with mi->lock held */
347 static struct mctp_i3c_device *
mctp_i3c_lookup(struct mctp_i3c_bus * mbus,u64 pid)348 mctp_i3c_lookup(struct mctp_i3c_bus *mbus, u64 pid)
349 {
350 	struct mctp_i3c_device *mi = NULL, *ret = NULL;
351 
352 	mutex_lock(&busdevs_lock);
353 	list_for_each_entry(mi, &mbus->devs, list)
354 		if (mi->pid == pid) {
355 			ret = mi;
356 			mutex_lock(&mi->lock);
357 			break;
358 		}
359 	mutex_unlock(&busdevs_lock);
360 	return ret;
361 }
362 
mctp_i3c_xmit(struct mctp_i3c_bus * mbus,struct sk_buff * skb)363 static void mctp_i3c_xmit(struct mctp_i3c_bus *mbus, struct sk_buff *skb)
364 {
365 	struct net_device_stats *stats = &mbus->ndev->stats;
366 	struct i3c_xfer xfer = { .rnw = false };
367 	struct mctp_i3c_internal_hdr *ihdr = NULL;
368 	struct mctp_i3c_device *mi = NULL;
369 	unsigned int data_len;
370 	u8 *data = NULL;
371 	u8 addr, pec;
372 	int rc = 0;
373 	u64 pid;
374 
375 	skb_pull(skb, sizeof(struct mctp_i3c_internal_hdr));
376 	data_len = skb->len;
377 
378 	ihdr = (void *)skb_mac_header(skb);
379 
380 	pid = get_unaligned_be48(ihdr->dest);
381 	mi = mctp_i3c_lookup(mbus, pid);
382 	if (!mi) {
383 		/* I3C endpoint went away after the packet was enqueued? */
384 		stats->tx_dropped++;
385 		goto out;
386 	}
387 
388 	if (WARN_ON_ONCE(data_len + 1 > MCTP_I3C_MAXBUF))
389 		goto out;
390 
391 	if (data_len + 1 > (unsigned int)mi->mwl) {
392 		/* Route MTU was larger than supported by the endpoint */
393 		stats->tx_dropped++;
394 		goto out;
395 	}
396 
397 	/* Need a linear buffer with space for the PEC */
398 	xfer.len = data_len + 1;
399 	if (skb_tailroom(skb) >= 1) {
400 		skb_put(skb, 1);
401 		data = skb->data;
402 	} else {
403 		/* Otherwise need to copy the buffer */
404 		skb_copy_bits(skb, 0, mbus->tx_scratch, skb->len);
405 		data = mbus->tx_scratch;
406 	}
407 
408 	/* PEC calculation */
409 	addr = mi->addr << 1;
410 	pec = i2c_smbus_pec(0, &addr, 1);
411 	pec = i2c_smbus_pec(pec, data, data_len);
412 	data[data_len] = pec;
413 
414 	xfer.data.out = data;
415 	rc = i3c_device_do_xfers(mi->i3c, &xfer, 1, I3C_SDR);
416 	if (rc == 0) {
417 		stats->tx_bytes += data_len;
418 		stats->tx_packets++;
419 	} else {
420 		stats->tx_errors++;
421 	}
422 
423 out:
424 	if (mi)
425 		mutex_unlock(&mi->lock);
426 }
427 
mctp_i3c_tx_thread(void * data)428 static int mctp_i3c_tx_thread(void *data)
429 {
430 	struct mctp_i3c_bus *mbus = data;
431 	struct sk_buff *skb;
432 
433 	for (;;) {
434 		if (kthread_should_stop())
435 			break;
436 
437 		spin_lock_bh(&mbus->tx_lock);
438 		skb = mbus->tx_skb;
439 		mbus->tx_skb = NULL;
440 		spin_unlock_bh(&mbus->tx_lock);
441 
442 		if (netif_queue_stopped(mbus->ndev))
443 			netif_wake_queue(mbus->ndev);
444 
445 		if (skb) {
446 			mctp_i3c_xmit(mbus, skb);
447 			kfree_skb(skb);
448 		} else {
449 			wait_event_idle(mbus->tx_wq,
450 					mbus->tx_skb || kthread_should_stop());
451 		}
452 	}
453 
454 	return 0;
455 }
456 
mctp_i3c_start_xmit(struct sk_buff * skb,struct net_device * ndev)457 static netdev_tx_t mctp_i3c_start_xmit(struct sk_buff *skb,
458 				       struct net_device *ndev)
459 {
460 	struct mctp_i3c_bus *mbus = netdev_priv(ndev);
461 	netdev_tx_t ret;
462 
463 	spin_lock(&mbus->tx_lock);
464 	netif_stop_queue(ndev);
465 	if (mbus->tx_skb) {
466 		dev_warn_ratelimited(&ndev->dev, "TX with queue stopped");
467 		ret = NETDEV_TX_BUSY;
468 	} else {
469 		mbus->tx_skb = skb;
470 		ret = NETDEV_TX_OK;
471 	}
472 	spin_unlock(&mbus->tx_lock);
473 
474 	if (ret == NETDEV_TX_OK)
475 		wake_up(&mbus->tx_wq);
476 
477 	return ret;
478 }
479 
mctp_i3c_bus_free(struct mctp_i3c_bus * mbus)480 static void mctp_i3c_bus_free(struct mctp_i3c_bus *mbus)
481 __must_hold(&busdevs_lock)
482 {
483 	struct mctp_i3c_device *mi = NULL, *tmp = NULL;
484 
485 	if (mbus->tx_thread) {
486 		kthread_stop(mbus->tx_thread);
487 		mbus->tx_thread = NULL;
488 	}
489 
490 	/* Remove any child devices */
491 	list_for_each_entry_safe(mi, tmp, &mbus->devs, list) {
492 		mctp_i3c_remove_device(mi);
493 	}
494 
495 	kfree_skb(mbus->tx_skb);
496 	list_del(&mbus->list);
497 }
498 
mctp_i3c_ndo_uninit(struct net_device * ndev)499 static void mctp_i3c_ndo_uninit(struct net_device *ndev)
500 {
501 	struct mctp_i3c_bus *mbus = netdev_priv(ndev);
502 
503 	/* Perform cleanup here to ensure there are no remaining references */
504 	mctp_i3c_bus_free(mbus);
505 }
506 
mctp_i3c_header_create(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)507 static int mctp_i3c_header_create(struct sk_buff *skb, struct net_device *dev,
508 				  unsigned short type, const void *daddr,
509 	   const void *saddr, unsigned int len)
510 {
511 	struct mctp_i3c_internal_hdr *ihdr;
512 	int rc;
513 
514 	if (!daddr || !saddr)
515 		return -EINVAL;
516 
517 	rc = skb_cow_head(skb, sizeof(struct mctp_i3c_internal_hdr));
518 	if (rc)
519 		return rc;
520 
521 	skb_push(skb, sizeof(struct mctp_i3c_internal_hdr));
522 	skb_reset_mac_header(skb);
523 	ihdr = (void *)skb_mac_header(skb);
524 	memcpy(ihdr->dest, daddr, PID_SIZE);
525 	memcpy(ihdr->source, saddr, PID_SIZE);
526 	return 0;
527 }
528 
529 static const struct net_device_ops mctp_i3c_ops = {
530 	.ndo_start_xmit = mctp_i3c_start_xmit,
531 	.ndo_uninit = mctp_i3c_ndo_uninit,
532 };
533 
534 static const struct header_ops mctp_i3c_headops = {
535 	.create = mctp_i3c_header_create,
536 };
537 
mctp_i3c_net_setup(struct net_device * dev)538 static void mctp_i3c_net_setup(struct net_device *dev)
539 {
540 	dev->type = ARPHRD_MCTP;
541 
542 	dev->mtu = MCTP_I3C_MAXMTU;
543 	dev->min_mtu = MCTP_I3C_MINMTU;
544 	dev->max_mtu = MCTP_I3C_MAXMTU;
545 	dev->tx_queue_len = MCTP_I3C_TX_QUEUE_LEN;
546 
547 	dev->hard_header_len = sizeof(struct mctp_i3c_internal_hdr);
548 	dev->addr_len = PID_SIZE;
549 
550 	dev->netdev_ops	= &mctp_i3c_ops;
551 	dev->header_ops	= &mctp_i3c_headops;
552 }
553 
mctp_i3c_is_mctp_controller(struct i3c_bus * bus)554 static bool mctp_i3c_is_mctp_controller(struct i3c_bus *bus)
555 {
556 	struct i3c_dev_desc *master = bus->cur_master;
557 
558 	if (!master)
559 		return false;
560 
561 	return of_property_read_bool(master->common.master->dev.of_node,
562 				     MCTP_I3C_OF_PROP);
563 }
564 
565 /* Returns the Provisioned Id of a local bus master */
mctp_i3c_bus_local_pid(struct i3c_bus * bus,u64 * ret_pid)566 static int mctp_i3c_bus_local_pid(struct i3c_bus *bus, u64 *ret_pid)
567 {
568 	struct i3c_dev_desc *master;
569 
570 	master = bus->cur_master;
571 	if (WARN_ON_ONCE(!master))
572 		return -ENOENT;
573 	*ret_pid = master->info.pid;
574 
575 	return 0;
576 }
577 
578 /* Returns an ERR_PTR on failure */
mctp_i3c_bus_add(struct i3c_bus * bus)579 static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
580 __must_hold(&busdevs_lock)
581 {
582 	struct mctp_i3c_bus *mbus = NULL;
583 	struct net_device *ndev = NULL;
584 	char namebuf[IFNAMSIZ];
585 	u8 addr[PID_SIZE];
586 	int rc;
587 
588 	if (!mctp_i3c_is_mctp_controller(bus))
589 		return ERR_PTR(-ENOENT);
590 
591 	snprintf(namebuf, sizeof(namebuf), "mctpi3c%d", bus->id);
592 	ndev = alloc_netdev(sizeof(*mbus), namebuf, NET_NAME_ENUM,
593 			    mctp_i3c_net_setup);
594 	if (!ndev) {
595 		rc = -ENOMEM;
596 		goto err;
597 	}
598 
599 	mbus = netdev_priv(ndev);
600 	mbus->ndev = ndev;
601 	mbus->bus = bus;
602 	INIT_LIST_HEAD(&mbus->devs);
603 	list_add(&mbus->list, &busdevs);
604 
605 	rc = mctp_i3c_bus_local_pid(bus, &mbus->pid);
606 	if (rc < 0) {
607 		dev_err(&ndev->dev, "No I3C PID available\n");
608 		goto err_free_uninit;
609 	}
610 	put_unaligned_be48(mbus->pid, addr);
611 	dev_addr_set(ndev, addr);
612 
613 	init_waitqueue_head(&mbus->tx_wq);
614 	spin_lock_init(&mbus->tx_lock);
615 	mbus->tx_thread = kthread_run(mctp_i3c_tx_thread, mbus,
616 				      "%s/tx", ndev->name);
617 	if (IS_ERR(mbus->tx_thread)) {
618 		dev_warn(&ndev->dev, "Error creating thread: %pe\n",
619 			 mbus->tx_thread);
620 		rc = PTR_ERR(mbus->tx_thread);
621 		mbus->tx_thread = NULL;
622 		goto err_free_uninit;
623 	}
624 
625 	rc = mctp_register_netdev(ndev, NULL, MCTP_PHYS_BINDING_I3C);
626 	if (rc < 0) {
627 		dev_warn(&ndev->dev, "netdev register failed: %d\n", rc);
628 		goto err_free_netdev;
629 	}
630 	return mbus;
631 
632 err_free_uninit:
633 	/* uninit will not get called if a netdev has not been registered,
634 	 * so we perform the same mbus cleanup manually.
635 	 */
636 	mctp_i3c_bus_free(mbus);
637 
638 err_free_netdev:
639 	free_netdev(ndev);
640 
641 err:
642 	return ERR_PTR(rc);
643 }
644 
mctp_i3c_bus_remove(struct mctp_i3c_bus * mbus)645 static void mctp_i3c_bus_remove(struct mctp_i3c_bus *mbus)
646 __must_hold(&busdevs_lock)
647 {
648 	/* Unregister calls through to ndo_uninit -> mctp_i3c_bus_free() */
649 	mctp_unregister_netdev(mbus->ndev);
650 
651 	free_netdev(mbus->ndev);
652 	/* mbus is deallocated */
653 }
654 
655 /* Removes all mctp-i3c busses */
mctp_i3c_bus_remove_all(void)656 static void mctp_i3c_bus_remove_all(void)
657 {
658 	struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
659 
660 	mutex_lock(&busdevs_lock);
661 	list_for_each_entry_safe(mbus, tmp, &busdevs, list) {
662 		mctp_i3c_bus_remove(mbus);
663 	}
664 	mutex_unlock(&busdevs_lock);
665 }
666 
667 /* Adds a i3c_bus if it isn't already in the busdevs list.
668  * Suitable as an i3c_for_each_bus_locked callback.
669  */
mctp_i3c_bus_add_new(struct i3c_bus * bus,void * data)670 static int mctp_i3c_bus_add_new(struct i3c_bus *bus, void *data)
671 {
672 	struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
673 	bool exists = false;
674 
675 	mutex_lock(&busdevs_lock);
676 	list_for_each_entry_safe(mbus, tmp, &busdevs, list)
677 		if (mbus->bus == bus)
678 			exists = true;
679 
680 	/* It is OK for a bus to already exist. That can occur due to
681 	 * the race in mod_init between notifier and for_each_bus
682 	 */
683 	if (!exists)
684 		mctp_i3c_bus_add(bus);
685 	mutex_unlock(&busdevs_lock);
686 	return 0;
687 }
688 
mctp_i3c_notify_bus_remove(struct i3c_bus * bus)689 static void mctp_i3c_notify_bus_remove(struct i3c_bus *bus)
690 {
691 	struct mctp_i3c_bus *mbus = NULL, *tmp;
692 
693 	mutex_lock(&busdevs_lock);
694 	list_for_each_entry_safe(mbus, tmp, &busdevs, list)
695 		if (mbus->bus == bus)
696 			mctp_i3c_bus_remove(mbus);
697 	mutex_unlock(&busdevs_lock);
698 }
699 
mctp_i3c_notifier_call(struct notifier_block * nb,unsigned long action,void * data)700 static int mctp_i3c_notifier_call(struct notifier_block *nb,
701 				  unsigned long action, void *data)
702 {
703 	switch (action) {
704 	case I3C_NOTIFY_BUS_ADD:
705 		mctp_i3c_bus_add_new((struct i3c_bus *)data, NULL);
706 		break;
707 	case I3C_NOTIFY_BUS_REMOVE:
708 		mctp_i3c_notify_bus_remove((struct i3c_bus *)data);
709 		break;
710 	}
711 	return NOTIFY_DONE;
712 }
713 
714 static struct notifier_block mctp_i3c_notifier = {
715 	.notifier_call = mctp_i3c_notifier_call,
716 };
717 
718 static const struct i3c_device_id mctp_i3c_ids[] = {
719 	I3C_CLASS(I3C_DCR_MCTP, NULL),
720 	{ 0 },
721 };
722 
723 static struct i3c_driver mctp_i3c_driver = {
724 	.driver = {
725 		.name = "mctp-i3c",
726 	},
727 	.probe = mctp_i3c_probe,
728 	.remove = mctp_i3c_remove,
729 	.id_table = mctp_i3c_ids,
730 };
731 
mctp_i3c_mod_init(void)732 static __init int mctp_i3c_mod_init(void)
733 {
734 	int rc;
735 
736 	rc = i3c_register_notifier(&mctp_i3c_notifier);
737 	if (rc < 0)
738 		return rc;
739 
740 	i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL);
741 
742 	rc = i3c_driver_register(&mctp_i3c_driver);
743 	if (rc < 0)
744 		goto err_unregister_notifier;
745 
746 	return 0;
747 
748 err_unregister_notifier:
749 	i3c_unregister_notifier(&mctp_i3c_notifier);
750 	mctp_i3c_bus_remove_all();
751 	return rc;
752 }
753 
mctp_i3c_mod_exit(void)754 static __exit void mctp_i3c_mod_exit(void)
755 {
756 	int rc;
757 
758 	i3c_driver_unregister(&mctp_i3c_driver);
759 
760 	rc = i3c_unregister_notifier(&mctp_i3c_notifier);
761 	if (rc < 0)
762 		pr_warn("MCTP I3C could not unregister notifier, %d\n", rc);
763 
764 	mctp_i3c_bus_remove_all();
765 }
766 
767 module_init(mctp_i3c_mod_init);
768 module_exit(mctp_i3c_mod_exit);
769 
770 MODULE_DEVICE_TABLE(i3c, mctp_i3c_ids);
771 MODULE_DESCRIPTION("MCTP I3C device");
772 MODULE_LICENSE("GPL");
773 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");
774