1 // SPDX-License-Identifier: GPL-2.0
3 * Management Controller Transport Protocol (MCTP)
4 * Implements DMTF specification
5 * "DSP0237 Management Component Transport Protocol (MCTP) SMBus/I2C
7 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0237_1.2.0.pdf
9 * A netdev is created for each I2C bus that handles MCTP. In the case of an I2C
10 * mux topology a single I2C client is attached to the root of the mux topology,
11 * shared between all mux I2C busses underneath. For non-mux cases an I2C client
12 * is attached per netdev.
14 * mctp-i2c-controller.yml devicetree binding has further details.
16 * Copyright (c) 2022 Code Construct
17 * Copyright (c) 2022 Google
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/i2c.h>
23 #include <linux/i2c-mux.h>
24 #include <linux/if_arp.h>
26 #include <net/mctpdevice.h>
28 /* byte_count is limited to u8 */
29 #define MCTP_I2C_MAXBLOCK 255
30 /* One byte is taken by source_slave */
31 #define MCTP_I2C_MAXMTU (MCTP_I2C_MAXBLOCK - 1)
32 #define MCTP_I2C_MINMTU (64 + 4)
33 /* Allow space for dest_address, command, byte_count, data, PEC */
34 #define MCTP_I2C_BUFSZ (3 + MCTP_I2C_MAXBLOCK + 1)
35 #define MCTP_I2C_MINLEN 8
36 #define MCTP_I2C_COMMANDCODE 0x0f
37 #define MCTP_I2C_TX_WORK_LEN 100
38 /* Sufficient for 64kB at min mtu */
39 #define MCTP_I2C_TX_QUEUE_LEN 1100
41 #define MCTP_I2C_OF_PROP "mctp-controller"
44 MCTP_I2C_FLOW_STATE_NEW
= 0,
45 MCTP_I2C_FLOW_STATE_ACTIVE
,
46 MCTP_I2C_FLOW_STATE_INVALID
,
49 /* List of all struct mctp_i2c_client
50 * Lock protects driver_clients and also prevents adding/removing adapters
51 * during mctp_i2c_client probe/remove.
53 static DEFINE_MUTEX(driver_clients_lock
);
54 static LIST_HEAD(driver_clients
);
56 struct mctp_i2c_client
;
58 /* The netdev structure. One of these per I2C adapter. */
60 struct net_device
*ndev
;
61 struct i2c_adapter
*adapter
;
62 struct mctp_i2c_client
*client
;
63 struct list_head list
; /* For mctp_i2c_client.devs */
66 u8 rx_buffer
[MCTP_I2C_BUFSZ
];
67 struct completion rx_done
;
69 struct task_struct
*tx_thread
;
70 wait_queue_head_t tx_wq
;
71 struct sk_buff_head tx_queue
;
72 u8 tx_scratch
[MCTP_I2C_BUFSZ
];
74 /* A fake entry in our tx queue to perform an unlock operation */
75 struct sk_buff unlock_marker
;
77 /* Spinlock protects i2c_lock_count, release_count, allow_rx */
81 /* Indicates that the netif is ready to receive incoming packets */
86 /* The i2c client structure. One per hardware i2c bus at the top of the
87 * mux tree, shared by multiple netdevs
89 struct mctp_i2c_client
{
90 struct i2c_client
*client
;
93 struct mctp_i2c_dev
*sel
;
94 struct list_head devs
;
95 spinlock_t sel_lock
; /* Protects sel and devs */
97 struct list_head list
; /* For driver_clients */
100 /* Header on the wire. */
101 struct mctp_i2c_hdr
{
104 /* Count of bytes following byte_count, excluding PEC */
109 static int mctp_i2c_recv(struct mctp_i2c_dev
*midev
);
110 static int mctp_i2c_slave_cb(struct i2c_client
*client
,
111 enum i2c_slave_event event
, u8
*val
);
112 static void mctp_i2c_ndo_uninit(struct net_device
*dev
);
113 static int mctp_i2c_ndo_open(struct net_device
*dev
);
115 static struct i2c_adapter
*mux_root_adapter(struct i2c_adapter
*adap
)
117 #if IS_ENABLED(CONFIG_I2C_MUX)
118 return i2c_root_adapter(&adap
->dev
);
120 /* In non-mux config all i2c adapters are root adapters */
125 /* Creates a new i2c slave device attached to the root adapter.
126 * Sets up the slave callback.
127 * Must be called with a client on a root adapter.
129 static struct mctp_i2c_client
*mctp_i2c_new_client(struct i2c_client
*client
)
131 struct mctp_i2c_client
*mcli
= NULL
;
132 struct i2c_adapter
*root
= NULL
;
135 if (client
->flags
& I2C_CLIENT_TEN
) {
136 dev_err(&client
->dev
, "failed, MCTP requires a 7-bit I2C address, addr=0x%x\n",
142 root
= mux_root_adapter(client
->adapter
);
144 dev_err(&client
->dev
, "failed to find root adapter\n");
148 if (root
!= client
->adapter
) {
149 dev_err(&client
->dev
,
150 "A mctp-i2c-controller client cannot be placed on an I2C mux adapter.\n"
151 " It should be placed on the mux tree root adapter\n"
152 " then set mctp-controller property on adapters to attach\n");
157 mcli
= kzalloc(sizeof(*mcli
), GFP_KERNEL
);
162 spin_lock_init(&mcli
->sel_lock
);
163 INIT_LIST_HEAD(&mcli
->devs
);
164 INIT_LIST_HEAD(&mcli
->list
);
165 mcli
->lladdr
= client
->addr
& 0xff;
166 mcli
->client
= client
;
167 i2c_set_clientdata(client
, mcli
);
169 rc
= i2c_slave_register(mcli
->client
, mctp_i2c_slave_cb
);
171 dev_err(&client
->dev
, "i2c register failed %d\n", rc
);
173 i2c_set_clientdata(client
, NULL
);
181 i2c_unregister_device(mcli
->client
);
187 static void mctp_i2c_free_client(struct mctp_i2c_client
*mcli
)
191 WARN_ON(!mutex_is_locked(&driver_clients_lock
));
192 WARN_ON(!list_empty(&mcli
->devs
));
193 WARN_ON(mcli
->sel
); /* sanity check, no locking */
195 rc
= i2c_slave_unregister(mcli
->client
);
196 /* Leak if it fails, we can't propagate errors upwards */
198 dev_err(&mcli
->client
->dev
, "i2c unregister failed %d\n", rc
);
203 /* Switch the mctp i2c device to receive responses.
204 * Call with sel_lock held
206 static void __mctp_i2c_device_select(struct mctp_i2c_client
*mcli
,
207 struct mctp_i2c_dev
*midev
)
209 assert_spin_locked(&mcli
->sel_lock
);
211 dev_hold(midev
->ndev
);
213 dev_put(mcli
->sel
->ndev
);
217 /* Switch the mctp i2c device to receive responses */
218 static void mctp_i2c_device_select(struct mctp_i2c_client
*mcli
,
219 struct mctp_i2c_dev
*midev
)
223 spin_lock_irqsave(&mcli
->sel_lock
, flags
);
224 __mctp_i2c_device_select(mcli
, midev
);
225 spin_unlock_irqrestore(&mcli
->sel_lock
, flags
);
228 static int mctp_i2c_slave_cb(struct i2c_client
*client
,
229 enum i2c_slave_event event
, u8
*val
)
231 struct mctp_i2c_client
*mcli
= i2c_get_clientdata(client
);
232 struct mctp_i2c_dev
*midev
= NULL
;
236 spin_lock_irqsave(&mcli
->sel_lock
, flags
);
239 dev_hold(midev
->ndev
);
240 spin_unlock_irqrestore(&mcli
->sel_lock
, flags
);
246 case I2C_SLAVE_WRITE_RECEIVED
:
247 if (midev
->rx_pos
< MCTP_I2C_BUFSZ
) {
248 midev
->rx_buffer
[midev
->rx_pos
] = *val
;
251 midev
->ndev
->stats
.rx_over_errors
++;
255 case I2C_SLAVE_WRITE_REQUESTED
:
256 /* dest_slave as first byte */
257 midev
->rx_buffer
[0] = mcli
->lladdr
<< 1;
261 rc
= mctp_i2c_recv(midev
);
267 dev_put(midev
->ndev
);
271 /* Processes incoming data that has been accumulated by the slave cb */
272 static int mctp_i2c_recv(struct mctp_i2c_dev
*midev
)
274 struct net_device
*ndev
= midev
->ndev
;
275 struct mctp_i2c_hdr
*hdr
;
276 struct mctp_skb_cb
*cb
;
283 /* + 1 for the PEC */
284 if (midev
->rx_pos
< MCTP_I2C_MINLEN
+ 1) {
285 ndev
->stats
.rx_length_errors
++;
288 /* recvlen excludes PEC */
289 recvlen
= midev
->rx_pos
- 1;
291 hdr
= (void *)midev
->rx_buffer
;
292 if (hdr
->command
!= MCTP_I2C_COMMANDCODE
) {
293 ndev
->stats
.rx_dropped
++;
297 if (hdr
->byte_count
+ offsetof(struct mctp_i2c_hdr
, source_slave
) != recvlen
) {
298 ndev
->stats
.rx_length_errors
++;
302 pec
= midev
->rx_buffer
[midev
->rx_pos
- 1];
303 calc_pec
= i2c_smbus_pec(0, midev
->rx_buffer
, recvlen
);
304 if (pec
!= calc_pec
) {
305 ndev
->stats
.rx_crc_errors
++;
309 skb
= netdev_alloc_skb(ndev
, recvlen
);
311 ndev
->stats
.rx_dropped
++;
315 skb
->protocol
= htons(ETH_P_MCTP
);
316 skb_put_data(skb
, midev
->rx_buffer
, recvlen
);
317 skb_reset_mac_header(skb
);
318 skb_pull(skb
, sizeof(struct mctp_i2c_hdr
));
319 skb_reset_network_header(skb
);
323 cb
->haddr
[0] = hdr
->source_slave
>> 1;
325 /* We need to ensure that the netif is not used once netdev
328 spin_lock_irqsave(&midev
->lock
, flags
);
329 if (midev
->allow_rx
) {
330 reinit_completion(&midev
->rx_done
);
331 spin_unlock_irqrestore(&midev
->lock
, flags
);
333 status
= netif_rx(skb
);
334 complete(&midev
->rx_done
);
336 status
= NET_RX_DROP
;
337 spin_unlock_irqrestore(&midev
->lock
, flags
);
340 if (status
== NET_RX_SUCCESS
) {
341 ndev
->stats
.rx_packets
++;
342 ndev
->stats
.rx_bytes
+= recvlen
;
344 ndev
->stats
.rx_dropped
++;
349 enum mctp_i2c_flow_state
{
350 MCTP_I2C_TX_FLOW_INVALID
,
351 MCTP_I2C_TX_FLOW_NONE
,
352 MCTP_I2C_TX_FLOW_NEW
,
353 MCTP_I2C_TX_FLOW_EXISTING
,
356 static enum mctp_i2c_flow_state
357 mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev
*midev
, struct sk_buff
*skb
)
359 enum mctp_i2c_flow_state state
;
360 struct mctp_sk_key
*key
;
361 struct mctp_flow
*flow
;
364 flow
= skb_ext_find(skb
, SKB_EXT_MCTP
);
366 return MCTP_I2C_TX_FLOW_NONE
;
370 return MCTP_I2C_TX_FLOW_NONE
;
372 spin_lock_irqsave(&key
->lock
, flags
);
373 /* If the key is present but invalid, we're unlikely to be able
374 * to handle the flow at all; just drop now
377 state
= MCTP_I2C_TX_FLOW_INVALID
;
379 switch (key
->dev_flow_state
) {
380 case MCTP_I2C_FLOW_STATE_NEW
:
381 key
->dev_flow_state
= MCTP_I2C_FLOW_STATE_ACTIVE
;
382 state
= MCTP_I2C_TX_FLOW_NEW
;
384 case MCTP_I2C_FLOW_STATE_ACTIVE
:
385 state
= MCTP_I2C_TX_FLOW_EXISTING
;
388 state
= MCTP_I2C_TX_FLOW_INVALID
;
392 spin_unlock_irqrestore(&key
->lock
, flags
);
397 /* We're not contending with ourselves here; we only need to exclude other
398 * i2c clients from using the bus. refcounts are simply to prevent
401 static void mctp_i2c_lock_nest(struct mctp_i2c_dev
*midev
)
406 spin_lock_irqsave(&midev
->lock
, flags
);
407 lock
= midev
->i2c_lock_count
== 0;
408 midev
->i2c_lock_count
++;
409 spin_unlock_irqrestore(&midev
->lock
, flags
);
412 i2c_lock_bus(midev
->adapter
, I2C_LOCK_SEGMENT
);
415 static void mctp_i2c_unlock_nest(struct mctp_i2c_dev
*midev
)
420 spin_lock_irqsave(&midev
->lock
, flags
);
421 if (!WARN_ONCE(midev
->i2c_lock_count
== 0, "lock count underflow!"))
422 midev
->i2c_lock_count
--;
423 unlock
= midev
->i2c_lock_count
== 0;
424 spin_unlock_irqrestore(&midev
->lock
, flags
);
427 i2c_unlock_bus(midev
->adapter
, I2C_LOCK_SEGMENT
);
430 /* Unlocks the bus if was previously locked, used for cleanup */
431 static void mctp_i2c_unlock_reset(struct mctp_i2c_dev
*midev
)
436 spin_lock_irqsave(&midev
->lock
, flags
);
437 unlock
= midev
->i2c_lock_count
> 0;
438 midev
->i2c_lock_count
= 0;
439 spin_unlock_irqrestore(&midev
->lock
, flags
);
442 i2c_unlock_bus(midev
->adapter
, I2C_LOCK_SEGMENT
);
445 static void mctp_i2c_invalidate_tx_flow(struct mctp_i2c_dev
*midev
,
448 struct mctp_sk_key
*key
;
449 struct mctp_flow
*flow
;
453 flow
= skb_ext_find(skb
, SKB_EXT_MCTP
);
461 spin_lock_irqsave(&key
->lock
, flags
);
462 if (key
->manual_alloc
) {
463 /* we don't have control over lifetimes for manually-allocated
464 * keys, so cannot assume we can invalidate all future flows
465 * that would use this key.
469 release
= key
->dev_flow_state
== MCTP_I2C_FLOW_STATE_ACTIVE
;
470 key
->dev_flow_state
= MCTP_I2C_FLOW_STATE_INVALID
;
472 spin_unlock_irqrestore(&key
->lock
, flags
);
474 /* if we have changed state from active, the flow held a reference on
475 * the lock; release that now.
478 mctp_i2c_unlock_nest(midev
);
481 static void mctp_i2c_xmit(struct mctp_i2c_dev
*midev
, struct sk_buff
*skb
)
483 struct net_device_stats
*stats
= &midev
->ndev
->stats
;
484 enum mctp_i2c_flow_state fs
;
485 struct mctp_i2c_hdr
*hdr
;
486 struct i2c_msg msg
= {0};
490 fs
= mctp_i2c_get_tx_flow_state(midev
, skb
);
492 hdr
= (void *)skb_mac_header(skb
);
493 /* Sanity check that packet contents matches skb length,
494 * and can't exceed MCTP_I2C_BUFSZ
496 if (skb
->len
!= hdr
->byte_count
+ 3) {
497 dev_warn_ratelimited(&midev
->adapter
->dev
,
498 "Bad tx length %d vs skb %u\n",
499 hdr
->byte_count
+ 3, skb
->len
);
503 if (skb_tailroom(skb
) >= 1) {
504 /* Linear case with space, we can just append the PEC */
507 /* Otherwise need to copy the buffer */
508 skb_copy_bits(skb
, 0, midev
->tx_scratch
, skb
->len
);
509 hdr
= (void *)midev
->tx_scratch
;
512 pecp
= (void *)&hdr
->source_slave
+ hdr
->byte_count
;
513 *pecp
= i2c_smbus_pec(0, (u8
*)hdr
, hdr
->byte_count
+ 3);
514 msg
.buf
= (void *)&hdr
->command
;
515 /* command, bytecount, data, pec */
516 msg
.len
= 2 + hdr
->byte_count
+ 1;
517 msg
.addr
= hdr
->dest_slave
>> 1;
520 case MCTP_I2C_TX_FLOW_NONE
:
521 /* no flow: full lock & unlock */
522 mctp_i2c_lock_nest(midev
);
523 mctp_i2c_device_select(midev
->client
, midev
);
524 rc
= __i2c_transfer(midev
->adapter
, &msg
, 1);
525 mctp_i2c_unlock_nest(midev
);
528 case MCTP_I2C_TX_FLOW_NEW
:
529 /* new flow: lock, tx, but don't unlock; that will happen
532 mctp_i2c_lock_nest(midev
);
533 mctp_i2c_device_select(midev
->client
, midev
);
536 case MCTP_I2C_TX_FLOW_EXISTING
:
537 /* existing flow: we already have the lock; just tx */
538 rc
= __i2c_transfer(midev
->adapter
, &msg
, 1);
540 /* on tx errors, the flow can no longer be considered valid */
542 mctp_i2c_invalidate_tx_flow(midev
, skb
);
546 case MCTP_I2C_TX_FLOW_INVALID
:
551 dev_warn_ratelimited(&midev
->adapter
->dev
,
552 "__i2c_transfer failed %d\n", rc
);
555 stats
->tx_bytes
+= skb
->len
;
560 static void mctp_i2c_flow_release(struct mctp_i2c_dev
*midev
)
565 spin_lock_irqsave(&midev
->lock
, flags
);
566 if (midev
->release_count
> midev
->i2c_lock_count
) {
567 WARN_ONCE(1, "release count overflow");
568 midev
->release_count
= midev
->i2c_lock_count
;
571 midev
->i2c_lock_count
-= midev
->release_count
;
572 unlock
= midev
->i2c_lock_count
== 0 && midev
->release_count
> 0;
573 midev
->release_count
= 0;
574 spin_unlock_irqrestore(&midev
->lock
, flags
);
577 i2c_unlock_bus(midev
->adapter
, I2C_LOCK_SEGMENT
);
580 static int mctp_i2c_header_create(struct sk_buff
*skb
, struct net_device
*dev
,
581 unsigned short type
, const void *daddr
,
582 const void *saddr
, unsigned int len
)
584 struct mctp_i2c_hdr
*hdr
;
585 struct mctp_hdr
*mhdr
;
588 if (len
> MCTP_I2C_MAXMTU
)
591 if (!daddr
|| !saddr
)
594 lldst
= *((u8
*)daddr
);
595 llsrc
= *((u8
*)saddr
);
597 skb_push(skb
, sizeof(struct mctp_i2c_hdr
));
598 skb_reset_mac_header(skb
);
599 hdr
= (void *)skb_mac_header(skb
);
600 mhdr
= mctp_hdr(skb
);
601 hdr
->dest_slave
= (lldst
<< 1) & 0xff;
602 hdr
->command
= MCTP_I2C_COMMANDCODE
;
603 hdr
->byte_count
= len
+ 1;
604 hdr
->source_slave
= ((llsrc
<< 1) & 0xff) | 0x01;
607 return sizeof(struct mctp_i2c_hdr
);
610 static int mctp_i2c_tx_thread(void *data
)
612 struct mctp_i2c_dev
*midev
= data
;
617 if (kthread_should_stop())
620 spin_lock_irqsave(&midev
->tx_queue
.lock
, flags
);
621 skb
= __skb_dequeue(&midev
->tx_queue
);
622 if (netif_queue_stopped(midev
->ndev
))
623 netif_wake_queue(midev
->ndev
);
624 spin_unlock_irqrestore(&midev
->tx_queue
.lock
, flags
);
626 if (skb
== &midev
->unlock_marker
) {
627 mctp_i2c_flow_release(midev
);
630 mctp_i2c_xmit(midev
, skb
);
634 wait_event_idle(midev
->tx_wq
,
635 !skb_queue_empty(&midev
->tx_queue
) ||
636 kthread_should_stop());
643 static netdev_tx_t
mctp_i2c_start_xmit(struct sk_buff
*skb
,
644 struct net_device
*dev
)
646 struct mctp_i2c_dev
*midev
= netdev_priv(dev
);
649 spin_lock_irqsave(&midev
->tx_queue
.lock
, flags
);
650 if (skb_queue_len(&midev
->tx_queue
) >= MCTP_I2C_TX_WORK_LEN
) {
651 netif_stop_queue(dev
);
652 spin_unlock_irqrestore(&midev
->tx_queue
.lock
, flags
);
653 netdev_err(dev
, "BUG! Tx Ring full when queue awake!\n");
654 return NETDEV_TX_BUSY
;
657 __skb_queue_tail(&midev
->tx_queue
, skb
);
658 if (skb_queue_len(&midev
->tx_queue
) == MCTP_I2C_TX_WORK_LEN
)
659 netif_stop_queue(dev
);
660 spin_unlock_irqrestore(&midev
->tx_queue
.lock
, flags
);
662 wake_up(&midev
->tx_wq
);
666 static void mctp_i2c_release_flow(struct mctp_dev
*mdev
,
667 struct mctp_sk_key
*key
)
670 struct mctp_i2c_dev
*midev
= netdev_priv(mdev
->dev
);
671 bool queue_release
= false;
674 spin_lock_irqsave(&midev
->lock
, flags
);
675 /* if we have seen the flow/key previously, we need to pair the
676 * original lock with a release
678 if (key
->dev_flow_state
== MCTP_I2C_FLOW_STATE_ACTIVE
) {
679 midev
->release_count
++;
680 queue_release
= true;
682 key
->dev_flow_state
= MCTP_I2C_FLOW_STATE_INVALID
;
683 spin_unlock_irqrestore(&midev
->lock
, flags
);
686 /* Ensure we have a release operation queued, through the fake
689 spin_lock(&midev
->tx_queue
.lock
);
690 if (!midev
->unlock_marker
.next
)
691 __skb_queue_tail(&midev
->tx_queue
,
692 &midev
->unlock_marker
);
693 spin_unlock(&midev
->tx_queue
.lock
);
694 wake_up(&midev
->tx_wq
);
698 static const struct net_device_ops mctp_i2c_ops
= {
699 .ndo_start_xmit
= mctp_i2c_start_xmit
,
700 .ndo_uninit
= mctp_i2c_ndo_uninit
,
701 .ndo_open
= mctp_i2c_ndo_open
,
704 static const struct header_ops mctp_i2c_headops
= {
705 .create
= mctp_i2c_header_create
,
708 static const struct mctp_netdev_ops mctp_i2c_mctp_ops
= {
709 .release_flow
= mctp_i2c_release_flow
,
712 static void mctp_i2c_net_setup(struct net_device
*dev
)
714 dev
->type
= ARPHRD_MCTP
;
716 dev
->mtu
= MCTP_I2C_MAXMTU
;
717 dev
->min_mtu
= MCTP_I2C_MINMTU
;
718 dev
->max_mtu
= MCTP_I2C_MAXMTU
;
719 dev
->tx_queue_len
= MCTP_I2C_TX_QUEUE_LEN
;
721 dev
->hard_header_len
= sizeof(struct mctp_i2c_hdr
);
724 dev
->netdev_ops
= &mctp_i2c_ops
;
725 dev
->header_ops
= &mctp_i2c_headops
;
728 /* Populates the mctp_i2c_dev priv struct for a netdev.
729 * Returns an error pointer on failure.
731 static struct mctp_i2c_dev
*mctp_i2c_midev_init(struct net_device
*dev
,
732 struct mctp_i2c_client
*mcli
,
733 struct i2c_adapter
*adap
)
735 struct mctp_i2c_dev
*midev
= netdev_priv(dev
);
738 midev
->tx_thread
= kthread_create(mctp_i2c_tx_thread
, midev
,
740 if (IS_ERR(midev
->tx_thread
))
741 return ERR_CAST(midev
->tx_thread
);
744 get_device(&adap
->dev
);
745 midev
->adapter
= adap
;
746 get_device(&mcli
->client
->dev
);
747 midev
->client
= mcli
;
748 INIT_LIST_HEAD(&midev
->list
);
749 spin_lock_init(&midev
->lock
);
750 midev
->i2c_lock_count
= 0;
751 midev
->release_count
= 0;
752 init_completion(&midev
->rx_done
);
753 complete(&midev
->rx_done
);
754 init_waitqueue_head(&midev
->tx_wq
);
755 skb_queue_head_init(&midev
->tx_queue
);
757 /* Add to the parent mcli */
758 spin_lock_irqsave(&mcli
->sel_lock
, flags
);
759 list_add(&midev
->list
, &mcli
->devs
);
760 /* Select a device by default */
762 __mctp_i2c_device_select(mcli
, midev
);
763 spin_unlock_irqrestore(&mcli
->sel_lock
, flags
);
765 /* Start the worker thread */
766 wake_up_process(midev
->tx_thread
);
771 /* Counterpart of mctp_i2c_midev_init */
772 static void mctp_i2c_midev_free(struct mctp_i2c_dev
*midev
)
774 struct mctp_i2c_client
*mcli
= midev
->client
;
777 if (midev
->tx_thread
) {
778 kthread_stop(midev
->tx_thread
);
779 midev
->tx_thread
= NULL
;
782 /* Unconditionally unlock on close */
783 mctp_i2c_unlock_reset(midev
);
785 /* Remove the netdev from the parent i2c client. */
786 spin_lock_irqsave(&mcli
->sel_lock
, flags
);
787 list_del(&midev
->list
);
788 if (mcli
->sel
== midev
) {
789 struct mctp_i2c_dev
*first
;
791 first
= list_first_entry_or_null(&mcli
->devs
, struct mctp_i2c_dev
, list
);
792 __mctp_i2c_device_select(mcli
, first
);
794 spin_unlock_irqrestore(&mcli
->sel_lock
, flags
);
796 skb_queue_purge(&midev
->tx_queue
);
797 put_device(&midev
->adapter
->dev
);
798 put_device(&mcli
->client
->dev
);
801 /* Stops, unregisters, and frees midev */
802 static void mctp_i2c_unregister(struct mctp_i2c_dev
*midev
)
806 /* Stop tx thread prior to unregister, it uses netif_() functions */
807 kthread_stop(midev
->tx_thread
);
808 midev
->tx_thread
= NULL
;
810 /* Prevent any new rx in mctp_i2c_recv(), let any pending work finish */
811 spin_lock_irqsave(&midev
->lock
, flags
);
812 midev
->allow_rx
= false;
813 spin_unlock_irqrestore(&midev
->lock
, flags
);
814 wait_for_completion(&midev
->rx_done
);
816 mctp_unregister_netdev(midev
->ndev
);
817 /* midev has been freed now by mctp_i2c_ndo_uninit callback */
819 free_netdev(midev
->ndev
);
822 static void mctp_i2c_ndo_uninit(struct net_device
*dev
)
824 struct mctp_i2c_dev
*midev
= netdev_priv(dev
);
826 /* Perform cleanup here to ensure that mcli->sel isn't holding
827 * a reference that would prevent unregister_netdevice()
830 mctp_i2c_midev_free(midev
);
833 static int mctp_i2c_ndo_open(struct net_device
*dev
)
835 struct mctp_i2c_dev
*midev
= netdev_priv(dev
);
838 /* i2c rx handler can only pass packets once the netdev is registered */
839 spin_lock_irqsave(&midev
->lock
, flags
);
840 midev
->allow_rx
= true;
841 spin_unlock_irqrestore(&midev
->lock
, flags
);
846 static int mctp_i2c_add_netdev(struct mctp_i2c_client
*mcli
,
847 struct i2c_adapter
*adap
)
849 struct mctp_i2c_dev
*midev
= NULL
;
850 struct net_device
*ndev
= NULL
;
851 struct i2c_adapter
*root
;
856 root
= mux_root_adapter(adap
);
857 if (root
!= mcli
->client
->adapter
) {
858 dev_err(&mcli
->client
->dev
,
859 "I2C adapter %s is not a child bus of %s\n",
860 mcli
->client
->adapter
->name
, root
->name
);
864 WARN_ON(!mutex_is_locked(&driver_clients_lock
));
865 snprintf(namebuf
, sizeof(namebuf
), "mctpi2c%d", adap
->nr
);
866 ndev
= alloc_netdev(sizeof(*midev
), namebuf
, NET_NAME_ENUM
, mctp_i2c_net_setup
);
868 dev_err(&mcli
->client
->dev
, "alloc netdev failed\n");
872 dev_net_set(ndev
, current
->nsproxy
->net_ns
);
873 SET_NETDEV_DEV(ndev
, &adap
->dev
);
874 dev_addr_set(ndev
, &mcli
->lladdr
);
876 midev
= mctp_i2c_midev_init(ndev
, mcli
, adap
);
883 rc
= mctp_register_netdev(ndev
, &mctp_i2c_mctp_ops
,
884 MCTP_PHYS_BINDING_SMBUS
);
886 dev_err(&mcli
->client
->dev
,
887 "register netdev \"%s\" failed %d\n",
892 spin_lock_irqsave(&midev
->lock
, flags
);
893 midev
->allow_rx
= false;
894 spin_unlock_irqrestore(&midev
->lock
, flags
);
899 mctp_i2c_midev_free(midev
);
905 /* Removes any netdev for adap. mcli is the parent root i2c client */
906 static void mctp_i2c_remove_netdev(struct mctp_i2c_client
*mcli
,
907 struct i2c_adapter
*adap
)
909 struct mctp_i2c_dev
*midev
= NULL
, *m
= NULL
;
912 WARN_ON(!mutex_is_locked(&driver_clients_lock
));
913 spin_lock_irqsave(&mcli
->sel_lock
, flags
);
914 /* List size is limited by number of MCTP netdevs on a single hardware bus */
915 list_for_each_entry(m
, &mcli
->devs
, list
)
916 if (m
->adapter
== adap
) {
920 spin_unlock_irqrestore(&mcli
->sel_lock
, flags
);
923 mctp_i2c_unregister(midev
);
926 /* Determines whether a device is an i2c adapter.
927 * Optionally returns the root i2c_adapter
929 static struct i2c_adapter
*mctp_i2c_get_adapter(struct device
*dev
,
930 struct i2c_adapter
**ret_root
)
932 struct i2c_adapter
*root
, *adap
;
934 if (dev
->type
!= &i2c_adapter_type
)
936 adap
= to_i2c_adapter(dev
);
937 root
= mux_root_adapter(adap
);
938 WARN_ONCE(!root
, "MCTP I2C failed to find root adapter for %s\n",
947 /* Determines whether a device is an i2c adapter with the "mctp-controller"
948 * devicetree property set. If adap is not an OF node, returns match_no_of
950 static bool mctp_i2c_adapter_match(struct i2c_adapter
*adap
, bool match_no_of
)
952 if (!adap
->dev
.of_node
)
954 return of_property_read_bool(adap
->dev
.of_node
, MCTP_I2C_OF_PROP
);
957 /* Called for each existing i2c device (adapter or client) when a
958 * new mctp-i2c client is probed.
960 static int mctp_i2c_client_try_attach(struct device
*dev
, void *data
)
962 struct i2c_adapter
*adap
= NULL
, *root
= NULL
;
963 struct mctp_i2c_client
*mcli
= data
;
965 adap
= mctp_i2c_get_adapter(dev
, &root
);
968 if (mcli
->client
->adapter
!= root
)
970 /* Must either have mctp-controller property on the adapter, or
971 * be a root adapter if it's non-devicetree
973 if (!mctp_i2c_adapter_match(adap
, adap
== root
))
976 return mctp_i2c_add_netdev(mcli
, adap
);
979 static void mctp_i2c_notify_add(struct device
*dev
)
981 struct mctp_i2c_client
*mcli
= NULL
, *m
= NULL
;
982 struct i2c_adapter
*root
= NULL
, *adap
= NULL
;
985 adap
= mctp_i2c_get_adapter(dev
, &root
);
988 /* Check for mctp-controller property on the adapter */
989 if (!mctp_i2c_adapter_match(adap
, false))
992 /* Find an existing mcli for adap's root */
993 mutex_lock(&driver_clients_lock
);
994 list_for_each_entry(m
, &driver_clients
, list
) {
995 if (m
->client
->adapter
== root
) {
1002 rc
= mctp_i2c_add_netdev(mcli
, adap
);
1004 dev_warn(dev
, "Failed adding mctp-i2c net device\n");
1006 mutex_unlock(&driver_clients_lock
);
1009 static void mctp_i2c_notify_del(struct device
*dev
)
1011 struct i2c_adapter
*root
= NULL
, *adap
= NULL
;
1012 struct mctp_i2c_client
*mcli
= NULL
;
1014 adap
= mctp_i2c_get_adapter(dev
, &root
);
1018 mutex_lock(&driver_clients_lock
);
1019 list_for_each_entry(mcli
, &driver_clients
, list
) {
1020 if (mcli
->client
->adapter
== root
) {
1021 mctp_i2c_remove_netdev(mcli
, adap
);
1025 mutex_unlock(&driver_clients_lock
);
1028 static int mctp_i2c_probe(struct i2c_client
*client
)
1030 struct mctp_i2c_client
*mcli
= NULL
;
1033 mutex_lock(&driver_clients_lock
);
1034 mcli
= mctp_i2c_new_client(client
);
1040 list_add(&mcli
->list
, &driver_clients
);
1043 /* Add a netdev for adapters that have a 'mctp-controller' property */
1044 i2c_for_each_dev(mcli
, mctp_i2c_client_try_attach
);
1047 mutex_unlock(&driver_clients_lock
);
1051 static void mctp_i2c_remove(struct i2c_client
*client
)
1053 struct mctp_i2c_client
*mcli
= i2c_get_clientdata(client
);
1054 struct mctp_i2c_dev
*midev
= NULL
, *tmp
= NULL
;
1056 mutex_lock(&driver_clients_lock
);
1057 list_del(&mcli
->list
);
1058 /* Remove all child adapter netdevs */
1059 list_for_each_entry_safe(midev
, tmp
, &mcli
->devs
, list
)
1060 mctp_i2c_unregister(midev
);
1062 mctp_i2c_free_client(mcli
);
1063 mutex_unlock(&driver_clients_lock
);
1066 /* We look for a 'mctp-controller' property on I2C busses as they are
1067 * added/deleted, creating/removing netdevs as required.
1069 static int mctp_i2c_notifier_call(struct notifier_block
*nb
,
1070 unsigned long action
, void *data
)
1072 struct device
*dev
= data
;
1075 case BUS_NOTIFY_ADD_DEVICE
:
1076 mctp_i2c_notify_add(dev
);
1078 case BUS_NOTIFY_DEL_DEVICE
:
1079 mctp_i2c_notify_del(dev
);
1085 static struct notifier_block mctp_i2c_notifier
= {
1086 .notifier_call
= mctp_i2c_notifier_call
,
1089 static const struct i2c_device_id mctp_i2c_id
[] = {
1090 { "mctp-i2c-interface" },
1093 MODULE_DEVICE_TABLE(i2c
, mctp_i2c_id
);
1095 static const struct of_device_id mctp_i2c_of_match
[] = {
1096 { .compatible
= "mctp-i2c-controller" },
1099 MODULE_DEVICE_TABLE(of
, mctp_i2c_of_match
);
1101 static struct i2c_driver mctp_i2c_driver
= {
1103 .name
= "mctp-i2c-interface",
1104 .of_match_table
= mctp_i2c_of_match
,
1106 .probe
= mctp_i2c_probe
,
1107 .remove
= mctp_i2c_remove
,
1108 .id_table
= mctp_i2c_id
,
1111 static __init
int mctp_i2c_mod_init(void)
1115 pr_info("MCTP I2C interface driver\n");
1116 rc
= i2c_add_driver(&mctp_i2c_driver
);
1119 rc
= bus_register_notifier(&i2c_bus_type
, &mctp_i2c_notifier
);
1121 i2c_del_driver(&mctp_i2c_driver
);
1127 static __exit
void mctp_i2c_mod_exit(void)
1131 rc
= bus_unregister_notifier(&i2c_bus_type
, &mctp_i2c_notifier
);
1133 pr_warn("MCTP I2C could not unregister notifier, %d\n", rc
);
1134 i2c_del_driver(&mctp_i2c_driver
);
1137 module_init(mctp_i2c_mod_init
);
1138 module_exit(mctp_i2c_mod_exit
);
1140 MODULE_DESCRIPTION("MCTP I2C device");
1141 MODULE_LICENSE("GPL v2");
1142 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");