mctp-i3c.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #include <linux/module.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/i3c/device.h>
  13. #include <linux/i3c/master.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/unaligned.h>
  16. #include <net/mctp.h>
  17. #include <net/mctpdevice.h>
  18. #define MCTP_I3C_MAXBUF 65536
  19. /* 48 bit Provisioned Id */
  20. #define PID_SIZE 6
  21. /* 64 byte payload, 4 byte MCTP header */
  22. static const int MCTP_I3C_MINMTU = 64 + 4;
  23. /* One byte less to allow for the PEC */
  24. static const int MCTP_I3C_MAXMTU = MCTP_I3C_MAXBUF - 1;
  25. /* 4 byte MCTP header, no data, 1 byte PEC */
  26. static const int MCTP_I3C_MINLEN = 4 + 1;
  27. /* Sufficient for 64kB at min mtu */
  28. static const int MCTP_I3C_TX_QUEUE_LEN = 1100;
  29. /* Somewhat arbitrary */
  30. static const int MCTP_I3C_IBI_SLOTS = 8;
  31. /* Mandatory Data Byte in an IBI, from DSP0233 */
  32. #define I3C_MDB_MCTP 0xAE
  33. /* From MIPI Device Characteristics Register (DCR) Assignments */
  34. #define I3C_DCR_MCTP 0xCC
  35. static const char *MCTP_I3C_OF_PROP = "mctp-controller";
  36. /* List of mctp_i3c_busdev */
  37. static LIST_HEAD(busdevs);
  38. /* Protects busdevs, as well as mctp_i3c_bus.devs lists */
  39. static DEFINE_MUTEX(busdevs_lock);
  40. struct mctp_i3c_bus {
  41. struct net_device *ndev;
  42. struct task_struct *tx_thread;
  43. wait_queue_head_t tx_wq;
  44. /* tx_lock protects tx_skb and devs */
  45. spinlock_t tx_lock;
  46. /* Next skb to transmit */
  47. struct sk_buff *tx_skb;
  48. /* Scratch buffer for xmit */
  49. u8 tx_scratch[MCTP_I3C_MAXBUF];
  50. /* Element of busdevs */
  51. struct list_head list;
  52. /* Provisioned ID of our controller */
  53. u64 pid;
  54. struct i3c_bus *bus;
  55. /* Head of mctp_i3c_device.list. Protected by busdevs_lock */
  56. struct list_head devs;
  57. };
  58. struct mctp_i3c_device {
  59. struct i3c_device *i3c;
  60. struct mctp_i3c_bus *mbus;
  61. struct list_head list; /* Element of mctp_i3c_bus.devs */
  62. /* Held while tx_thread is using this device */
  63. struct mutex lock;
  64. /* Whether BCR indicates MDB is present in IBI */
  65. bool have_mdb;
  66. /* I3C dynamic address */
  67. u8 addr;
  68. /* Maximum read length */
  69. u16 mrl;
  70. /* Maximum write length */
  71. u16 mwl;
  72. /* Provisioned ID */
  73. u64 pid;
  74. };
  75. /* We synthesise a mac header using the Provisioned ID.
  76. * Used to pass dest to mctp_i3c_start_xmit.
  77. */
  78. struct mctp_i3c_internal_hdr {
  79. u8 dest[PID_SIZE];
  80. u8 source[PID_SIZE];
  81. } __packed;
  82. static int mctp_i3c_read(struct mctp_i3c_device *mi)
  83. {
  84. struct i3c_priv_xfer xfer = { .rnw = 1, .len = mi->mrl };
  85. struct net_device_stats *stats = &mi->mbus->ndev->stats;
  86. struct mctp_i3c_internal_hdr *ihdr = NULL;
  87. struct sk_buff *skb = NULL;
  88. struct mctp_skb_cb *cb;
  89. int net_status, rc;
  90. u8 pec, addr;
  91. skb = netdev_alloc_skb(mi->mbus->ndev,
  92. mi->mrl + sizeof(struct mctp_i3c_internal_hdr));
  93. if (!skb) {
  94. stats->rx_dropped++;
  95. rc = -ENOMEM;
  96. goto err;
  97. }
  98. skb->protocol = htons(ETH_P_MCTP);
  99. /* Create a header for internal use */
  100. skb_reset_mac_header(skb);
  101. ihdr = skb_put(skb, sizeof(struct mctp_i3c_internal_hdr));
  102. put_unaligned_be48(mi->pid, ihdr->source);
  103. put_unaligned_be48(mi->mbus->pid, ihdr->dest);
  104. skb_pull(skb, sizeof(struct mctp_i3c_internal_hdr));
  105. xfer.data.in = skb_put(skb, mi->mrl);
  106. /* Make sure netif_rx() is read in the same order as i3c. */
  107. mutex_lock(&mi->lock);
  108. rc = i3c_device_do_priv_xfers(mi->i3c, &xfer, 1);
  109. if (rc < 0)
  110. goto err;
  111. if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
  112. /* Bad i3c bus driver */
  113. rc = -EIO;
  114. goto err;
  115. }
  116. if (xfer.len < MCTP_I3C_MINLEN) {
  117. stats->rx_length_errors++;
  118. rc = -EIO;
  119. goto err;
  120. }
  121. /* check PEC, including address byte */
  122. addr = mi->addr << 1 | 1;
  123. pec = i2c_smbus_pec(0, &addr, 1);
  124. pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
  125. if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
  126. stats->rx_crc_errors++;
  127. rc = -EINVAL;
  128. goto err;
  129. }
  130. /* Remove PEC */
  131. skb_trim(skb, xfer.len - 1);
  132. cb = __mctp_cb(skb);
  133. cb->halen = PID_SIZE;
  134. put_unaligned_be48(mi->pid, cb->haddr);
  135. net_status = netif_rx(skb);
  136. if (net_status == NET_RX_SUCCESS) {
  137. stats->rx_packets++;
  138. stats->rx_bytes += xfer.len - 1;
  139. } else {
  140. stats->rx_dropped++;
  141. }
  142. mutex_unlock(&mi->lock);
  143. return 0;
  144. err:
  145. mutex_unlock(&mi->lock);
  146. kfree_skb(skb);
  147. return rc;
  148. }
  149. static void mctp_i3c_ibi_handler(struct i3c_device *i3c,
  150. const struct i3c_ibi_payload *payload)
  151. {
  152. struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
  153. if (WARN_ON_ONCE(!mi))
  154. return;
  155. if (mi->have_mdb) {
  156. if (payload->len > 0) {
  157. if (((u8 *)payload->data)[0] != I3C_MDB_MCTP) {
  158. /* Not a mctp-i3c interrupt, ignore it */
  159. return;
  160. }
  161. } else {
  162. /* The BCR advertised a Mandatory Data Byte but the
  163. * device didn't send one.
  164. */
  165. dev_warn_once(i3cdev_to_dev(i3c), "IBI with missing MDB");
  166. }
  167. }
  168. mctp_i3c_read(mi);
  169. }
  170. static int mctp_i3c_setup(struct mctp_i3c_device *mi)
  171. {
  172. const struct i3c_ibi_setup ibi = {
  173. .max_payload_len = 1,
  174. .num_slots = MCTP_I3C_IBI_SLOTS,
  175. .handler = mctp_i3c_ibi_handler,
  176. };
  177. struct i3c_device_info info;
  178. int rc;
  179. i3c_device_get_info(mi->i3c, &info);
  180. mi->have_mdb = info.bcr & BIT(2);
  181. mi->addr = info.dyn_addr;
  182. mi->mwl = info.max_write_len;
  183. mi->mrl = info.max_read_len;
  184. mi->pid = info.pid;
  185. rc = i3c_device_request_ibi(mi->i3c, &ibi);
  186. if (rc == -ENOTSUPP) {
  187. /* This driver only supports In-Band Interrupt mode.
  188. * Support for Polling Mode could be added if required.
  189. * (ENOTSUPP is from the i3c layer, not EOPNOTSUPP).
  190. */
  191. dev_warn(i3cdev_to_dev(mi->i3c),
  192. "Failed, bus driver doesn't support In-Band Interrupts");
  193. goto err;
  194. } else if (rc < 0) {
  195. dev_err(i3cdev_to_dev(mi->i3c),
  196. "Failed requesting IBI (%d)\n", rc);
  197. goto err;
  198. }
  199. rc = i3c_device_enable_ibi(mi->i3c);
  200. if (rc < 0) {
  201. /* Assume a driver supporting request_ibi also
  202. * supports enable_ibi.
  203. */
  204. dev_err(i3cdev_to_dev(mi->i3c), "Failed enabling IBI (%d)\n", rc);
  205. goto err_free_ibi;
  206. }
  207. return 0;
  208. err_free_ibi:
  209. i3c_device_free_ibi(mi->i3c);
  210. err:
  211. return rc;
  212. }
  213. /* Adds a new MCTP i3c_device to a bus */
  214. static int mctp_i3c_add_device(struct mctp_i3c_bus *mbus,
  215. struct i3c_device *i3c)
  216. __must_hold(&busdevs_lock)
  217. {
  218. struct mctp_i3c_device *mi = NULL;
  219. int rc;
  220. mi = kzalloc(sizeof(*mi), GFP_KERNEL);
  221. if (!mi) {
  222. rc = -ENOMEM;
  223. goto err;
  224. }
  225. mi->mbus = mbus;
  226. mi->i3c = i3c;
  227. mutex_init(&mi->lock);
  228. list_add(&mi->list, &mbus->devs);
  229. i3cdev_set_drvdata(i3c, mi);
  230. rc = mctp_i3c_setup(mi);
  231. if (rc < 0)
  232. goto err_free;
  233. return 0;
  234. err_free:
  235. list_del(&mi->list);
  236. kfree(mi);
  237. err:
  238. dev_warn(i3cdev_to_dev(i3c), "Error adding mctp-i3c device, %d\n", rc);
  239. return rc;
  240. }
  241. static int mctp_i3c_probe(struct i3c_device *i3c)
  242. {
  243. struct mctp_i3c_bus *b = NULL, *mbus = NULL;
  244. /* Look for a known bus */
  245. mutex_lock(&busdevs_lock);
  246. list_for_each_entry(b, &busdevs, list)
  247. if (b->bus == i3c->bus) {
  248. mbus = b;
  249. break;
  250. }
  251. mutex_unlock(&busdevs_lock);
  252. if (!mbus) {
  253. /* probably no "mctp-controller" property on the i3c bus */
  254. return -ENODEV;
  255. }
  256. return mctp_i3c_add_device(mbus, i3c);
  257. }
  258. static void mctp_i3c_remove_device(struct mctp_i3c_device *mi)
  259. __must_hold(&busdevs_lock)
  260. {
  261. /* Ensure the tx thread isn't using the device */
  262. mutex_lock(&mi->lock);
  263. /* Counterpart of mctp_i3c_setup */
  264. i3c_device_disable_ibi(mi->i3c);
  265. i3c_device_free_ibi(mi->i3c);
  266. /* Counterpart of mctp_i3c_add_device */
  267. i3cdev_set_drvdata(mi->i3c, NULL);
  268. list_del(&mi->list);
  269. /* Safe to unlock after removing from the list */
  270. mutex_unlock(&mi->lock);
  271. kfree(mi);
  272. }
  273. static void mctp_i3c_remove(struct i3c_device *i3c)
  274. {
  275. struct mctp_i3c_device *mi = i3cdev_get_drvdata(i3c);
  276. /* We my have received a Bus Remove notify prior to device remove,
  277. * so mi will already be removed.
  278. */
  279. if (!mi)
  280. return;
  281. mutex_lock(&busdevs_lock);
  282. mctp_i3c_remove_device(mi);
  283. mutex_unlock(&busdevs_lock);
  284. }
  285. /* Returns the device for an address, with mi->lock held */
  286. static struct mctp_i3c_device *
  287. mctp_i3c_lookup(struct mctp_i3c_bus *mbus, u64 pid)
  288. {
  289. struct mctp_i3c_device *mi = NULL, *ret = NULL;
  290. mutex_lock(&busdevs_lock);
  291. list_for_each_entry(mi, &mbus->devs, list)
  292. if (mi->pid == pid) {
  293. ret = mi;
  294. mutex_lock(&mi->lock);
  295. break;
  296. }
  297. mutex_unlock(&busdevs_lock);
  298. return ret;
  299. }
  300. static void mctp_i3c_xmit(struct mctp_i3c_bus *mbus, struct sk_buff *skb)
  301. {
  302. struct net_device_stats *stats = &mbus->ndev->stats;
  303. struct i3c_priv_xfer xfer = { .rnw = false };
  304. struct mctp_i3c_internal_hdr *ihdr = NULL;
  305. struct mctp_i3c_device *mi = NULL;
  306. unsigned int data_len;
  307. u8 *data = NULL;
  308. u8 addr, pec;
  309. int rc = 0;
  310. u64 pid;
  311. skb_pull(skb, sizeof(struct mctp_i3c_internal_hdr));
  312. data_len = skb->len;
  313. ihdr = (void *)skb_mac_header(skb);
  314. pid = get_unaligned_be48(ihdr->dest);
  315. mi = mctp_i3c_lookup(mbus, pid);
  316. if (!mi) {
  317. /* I3C endpoint went away after the packet was enqueued? */
  318. stats->tx_dropped++;
  319. goto out;
  320. }
  321. if (WARN_ON_ONCE(data_len + 1 > MCTP_I3C_MAXBUF))
  322. goto out;
  323. if (data_len + 1 > (unsigned int)mi->mwl) {
  324. /* Route MTU was larger than supported by the endpoint */
  325. stats->tx_dropped++;
  326. goto out;
  327. }
  328. /* Need a linear buffer with space for the PEC */
  329. xfer.len = data_len + 1;
  330. if (skb_tailroom(skb) >= 1) {
  331. skb_put(skb, 1);
  332. data = skb->data;
  333. } else {
  334. /* Otherwise need to copy the buffer */
  335. skb_copy_bits(skb, 0, mbus->tx_scratch, skb->len);
  336. data = mbus->tx_scratch;
  337. }
  338. /* PEC calculation */
  339. addr = mi->addr << 1;
  340. pec = i2c_smbus_pec(0, &addr, 1);
  341. pec = i2c_smbus_pec(pec, data, data_len);
  342. data[data_len] = pec;
  343. xfer.data.out = data;
  344. rc = i3c_device_do_priv_xfers(mi->i3c, &xfer, 1);
  345. if (rc == 0) {
  346. stats->tx_bytes += data_len;
  347. stats->tx_packets++;
  348. } else {
  349. stats->tx_errors++;
  350. }
  351. out:
  352. if (mi)
  353. mutex_unlock(&mi->lock);
  354. }
  355. static int mctp_i3c_tx_thread(void *data)
  356. {
  357. struct mctp_i3c_bus *mbus = data;
  358. struct sk_buff *skb;
  359. for (;;) {
  360. if (kthread_should_stop())
  361. break;
  362. spin_lock_bh(&mbus->tx_lock);
  363. skb = mbus->tx_skb;
  364. mbus->tx_skb = NULL;
  365. spin_unlock_bh(&mbus->tx_lock);
  366. if (netif_queue_stopped(mbus->ndev))
  367. netif_wake_queue(mbus->ndev);
  368. if (skb) {
  369. mctp_i3c_xmit(mbus, skb);
  370. kfree_skb(skb);
  371. } else {
  372. wait_event_idle(mbus->tx_wq,
  373. mbus->tx_skb || kthread_should_stop());
  374. }
  375. }
  376. return 0;
  377. }
  378. static netdev_tx_t mctp_i3c_start_xmit(struct sk_buff *skb,
  379. struct net_device *ndev)
  380. {
  381. struct mctp_i3c_bus *mbus = netdev_priv(ndev);
  382. netdev_tx_t ret;
  383. spin_lock(&mbus->tx_lock);
  384. netif_stop_queue(ndev);
  385. if (mbus->tx_skb) {
  386. dev_warn_ratelimited(&ndev->dev, "TX with queue stopped");
  387. ret = NETDEV_TX_BUSY;
  388. } else {
  389. mbus->tx_skb = skb;
  390. ret = NETDEV_TX_OK;
  391. }
  392. spin_unlock(&mbus->tx_lock);
  393. if (ret == NETDEV_TX_OK)
  394. wake_up(&mbus->tx_wq);
  395. return ret;
  396. }
  397. static void mctp_i3c_bus_free(struct mctp_i3c_bus *mbus)
  398. __must_hold(&busdevs_lock)
  399. {
  400. struct mctp_i3c_device *mi = NULL, *tmp = NULL;
  401. if (mbus->tx_thread) {
  402. kthread_stop(mbus->tx_thread);
  403. mbus->tx_thread = NULL;
  404. }
  405. /* Remove any child devices */
  406. list_for_each_entry_safe(mi, tmp, &mbus->devs, list) {
  407. mctp_i3c_remove_device(mi);
  408. }
  409. kfree_skb(mbus->tx_skb);
  410. list_del(&mbus->list);
  411. }
  412. static void mctp_i3c_ndo_uninit(struct net_device *ndev)
  413. {
  414. struct mctp_i3c_bus *mbus = netdev_priv(ndev);
  415. /* Perform cleanup here to ensure there are no remaining references */
  416. mctp_i3c_bus_free(mbus);
  417. }
  418. static int mctp_i3c_header_create(struct sk_buff *skb, struct net_device *dev,
  419. unsigned short type, const void *daddr,
  420. const void *saddr, unsigned int len)
  421. {
  422. struct mctp_i3c_internal_hdr *ihdr;
  423. int rc;
  424. if (!daddr || !saddr)
  425. return -EINVAL;
  426. rc = skb_cow_head(skb, sizeof(struct mctp_i3c_internal_hdr));
  427. if (rc)
  428. return rc;
  429. skb_push(skb, sizeof(struct mctp_i3c_internal_hdr));
  430. skb_reset_mac_header(skb);
  431. ihdr = (void *)skb_mac_header(skb);
  432. memcpy(ihdr->dest, daddr, PID_SIZE);
  433. memcpy(ihdr->source, saddr, PID_SIZE);
  434. return 0;
  435. }
  436. static const struct net_device_ops mctp_i3c_ops = {
  437. .ndo_start_xmit = mctp_i3c_start_xmit,
  438. .ndo_uninit = mctp_i3c_ndo_uninit,
  439. };
  440. static const struct header_ops mctp_i3c_headops = {
  441. .create = mctp_i3c_header_create,
  442. };
  443. static void mctp_i3c_net_setup(struct net_device *dev)
  444. {
  445. dev->type = ARPHRD_MCTP;
  446. dev->mtu = MCTP_I3C_MAXMTU;
  447. dev->min_mtu = MCTP_I3C_MINMTU;
  448. dev->max_mtu = MCTP_I3C_MAXMTU;
  449. dev->tx_queue_len = MCTP_I3C_TX_QUEUE_LEN;
  450. dev->hard_header_len = sizeof(struct mctp_i3c_internal_hdr);
  451. dev->addr_len = PID_SIZE;
  452. dev->netdev_ops = &mctp_i3c_ops;
  453. dev->header_ops = &mctp_i3c_headops;
  454. }
  455. static bool mctp_i3c_is_mctp_controller(struct i3c_bus *bus)
  456. {
  457. struct i3c_dev_desc *master = bus->cur_master;
  458. if (!master)
  459. return false;
  460. return of_property_read_bool(master->common.master->dev.of_node,
  461. MCTP_I3C_OF_PROP);
  462. }
  463. /* Returns the Provisioned Id of a local bus master */
  464. static int mctp_i3c_bus_local_pid(struct i3c_bus *bus, u64 *ret_pid)
  465. {
  466. struct i3c_dev_desc *master;
  467. master = bus->cur_master;
  468. if (WARN_ON_ONCE(!master))
  469. return -ENOENT;
  470. *ret_pid = master->info.pid;
  471. return 0;
  472. }
  473. /* Returns an ERR_PTR on failure */
  474. static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
  475. __must_hold(&busdevs_lock)
  476. {
  477. struct mctp_i3c_bus *mbus = NULL;
  478. struct net_device *ndev = NULL;
  479. char namebuf[IFNAMSIZ];
  480. u8 addr[PID_SIZE];
  481. int rc;
  482. if (!mctp_i3c_is_mctp_controller(bus))
  483. return ERR_PTR(-ENOENT);
  484. snprintf(namebuf, sizeof(namebuf), "mctpi3c%d", bus->id);
  485. ndev = alloc_netdev(sizeof(*mbus), namebuf, NET_NAME_ENUM,
  486. mctp_i3c_net_setup);
  487. if (!ndev) {
  488. rc = -ENOMEM;
  489. goto err;
  490. }
  491. mbus = netdev_priv(ndev);
  492. mbus->ndev = ndev;
  493. mbus->bus = bus;
  494. INIT_LIST_HEAD(&mbus->devs);
  495. list_add(&mbus->list, &busdevs);
  496. rc = mctp_i3c_bus_local_pid(bus, &mbus->pid);
  497. if (rc < 0) {
  498. dev_err(&ndev->dev, "No I3C PID available\n");
  499. goto err_free_uninit;
  500. }
  501. put_unaligned_be48(mbus->pid, addr);
  502. dev_addr_set(ndev, addr);
  503. init_waitqueue_head(&mbus->tx_wq);
  504. spin_lock_init(&mbus->tx_lock);
  505. mbus->tx_thread = kthread_run(mctp_i3c_tx_thread, mbus,
  506. "%s/tx", ndev->name);
  507. if (IS_ERR(mbus->tx_thread)) {
  508. dev_warn(&ndev->dev, "Error creating thread: %pe\n",
  509. mbus->tx_thread);
  510. rc = PTR_ERR(mbus->tx_thread);
  511. mbus->tx_thread = NULL;
  512. goto err_free_uninit;
  513. }
  514. rc = mctp_register_netdev(ndev, NULL);
  515. if (rc < 0) {
  516. dev_warn(&ndev->dev, "netdev register failed: %d\n", rc);
  517. goto err_free_netdev;
  518. }
  519. return mbus;
  520. err_free_uninit:
  521. /* uninit will not get called if a netdev has not been registered,
  522. * so we perform the same mbus cleanup manually.
  523. */
  524. mctp_i3c_bus_free(mbus);
  525. err_free_netdev:
  526. free_netdev(ndev);
  527. err:
  528. return ERR_PTR(rc);
  529. }
  530. static void mctp_i3c_bus_remove(struct mctp_i3c_bus *mbus)
  531. __must_hold(&busdevs_lock)
  532. {
  533. /* Unregister calls through to ndo_uninit -> mctp_i3c_bus_free() */
  534. mctp_unregister_netdev(mbus->ndev);
  535. free_netdev(mbus->ndev);
  536. /* mbus is deallocated */
  537. }
  538. /* Removes all mctp-i3c busses */
  539. static void mctp_i3c_bus_remove_all(void)
  540. {
  541. struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
  542. mutex_lock(&busdevs_lock);
  543. list_for_each_entry_safe(mbus, tmp, &busdevs, list) {
  544. mctp_i3c_bus_remove(mbus);
  545. }
  546. mutex_unlock(&busdevs_lock);
  547. }
  548. /* Adds a i3c_bus if it isn't already in the busdevs list.
  549. * Suitable as an i3c_for_each_bus_locked callback.
  550. */
  551. static int mctp_i3c_bus_add_new(struct i3c_bus *bus, void *data)
  552. {
  553. struct mctp_i3c_bus *mbus = NULL, *tmp = NULL;
  554. bool exists = false;
  555. mutex_lock(&busdevs_lock);
  556. list_for_each_entry_safe(mbus, tmp, &busdevs, list)
  557. if (mbus->bus == bus)
  558. exists = true;
  559. /* It is OK for a bus to already exist. That can occur due to
  560. * the race in mod_init between notifier and for_each_bus
  561. */
  562. if (!exists)
  563. mctp_i3c_bus_add(bus);
  564. mutex_unlock(&busdevs_lock);
  565. return 0;
  566. }
  567. static void mctp_i3c_notify_bus_remove(struct i3c_bus *bus)
  568. {
  569. struct mctp_i3c_bus *mbus = NULL, *tmp;
  570. mutex_lock(&busdevs_lock);
  571. list_for_each_entry_safe(mbus, tmp, &busdevs, list)
  572. if (mbus->bus == bus)
  573. mctp_i3c_bus_remove(mbus);
  574. mutex_unlock(&busdevs_lock);
  575. }
  576. static int mctp_i3c_notifier_call(struct notifier_block *nb,
  577. unsigned long action, void *data)
  578. {
  579. switch (action) {
  580. case I3C_NOTIFY_BUS_ADD:
  581. mctp_i3c_bus_add_new((struct i3c_bus *)data, NULL);
  582. break;
  583. case I3C_NOTIFY_BUS_REMOVE:
  584. mctp_i3c_notify_bus_remove((struct i3c_bus *)data);
  585. break;
  586. }
  587. return NOTIFY_DONE;
  588. }
  589. static struct notifier_block mctp_i3c_notifier = {
  590. .notifier_call = mctp_i3c_notifier_call,
  591. };
  592. static const struct i3c_device_id mctp_i3c_ids[] = {
  593. I3C_CLASS(I3C_DCR_MCTP, NULL),
  594. { 0 },
  595. };
  596. static struct i3c_driver mctp_i3c_driver = {
  597. .driver = {
  598. .name = "mctp-i3c",
  599. },
  600. .probe = mctp_i3c_probe,
  601. .remove = mctp_i3c_remove,
  602. .id_table = mctp_i3c_ids,
  603. };
  604. static __init int mctp_i3c_mod_init(void)
  605. {
  606. int rc;
  607. rc = i3c_register_notifier(&mctp_i3c_notifier);
  608. if (rc < 0) {
  609. i3c_driver_unregister(&mctp_i3c_driver);
  610. return rc;
  611. }
  612. i3c_for_each_bus_locked(mctp_i3c_bus_add_new, NULL);
  613. rc = i3c_driver_register(&mctp_i3c_driver);
  614. if (rc < 0)
  615. return rc;
  616. return 0;
  617. }
  618. static __exit void mctp_i3c_mod_exit(void)
  619. {
  620. int rc;
  621. i3c_driver_unregister(&mctp_i3c_driver);
  622. rc = i3c_unregister_notifier(&mctp_i3c_notifier);
  623. if (rc < 0)
  624. pr_warn("MCTP I3C could not unregister notifier, %d\n", rc);
  625. mctp_i3c_bus_remove_all();
  626. }
  627. module_init(mctp_i3c_mod_init);
  628. module_exit(mctp_i3c_mod_exit);
  629. MODULE_DEVICE_TABLE(i3c, mctp_i3c_ids);
  630. MODULE_DESCRIPTION("MCTP I3C device");
  631. MODULE_LICENSE("GPL");
  632. MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");