xref: /linux/drivers/net/mctp/mctp-i2c.c (revision 21ab7031cbff8c6b6f608234e18ffe0473e98f9d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Management Controller Transport Protocol (MCTP)
4  * Implements DMTF specification
5  * "DSP0237 Management Component Transport Protocol (MCTP) SMBus/I2C
6  * Transport Binding"
7  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0237_1.2.0.pdf
8  *
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.
13  *
14  * mctp-i2c-controller.yml devicetree binding has further details.
15  *
16  * Copyright (c) 2022 Code Construct
17  * Copyright (c) 2022 Google
18  */
19 
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>
25 #include <net/mctp.h>
26 #include <net/mctpdevice.h>
27 
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
40 
41 #define MCTP_I2C_OF_PROP "mctp-controller"
42 
43 enum {
44 	MCTP_I2C_FLOW_STATE_NEW = 0,
45 	MCTP_I2C_FLOW_STATE_ACTIVE,
46 	MCTP_I2C_FLOW_STATE_INVALID,
47 };
48 
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.
52  */
53 static DEFINE_MUTEX(driver_clients_lock);
54 static LIST_HEAD(driver_clients);
55 
56 struct mctp_i2c_client;
57 
58 /* The netdev structure. One of these per I2C adapter. */
59 struct mctp_i2c_dev {
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 */
64 
65 	size_t rx_pos;
66 	u8 rx_buffer[MCTP_I2C_BUFSZ];
67 	struct completion rx_done;
68 
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];
73 
74 	/* A fake entry in our tx queue to perform an unlock operation */
75 	struct sk_buff unlock_marker;
76 
77 	/* Spinlock protects i2c_lock_count, release_count, allow_rx */
78 	spinlock_t lock;
79 	int i2c_lock_count;
80 	int release_count;
81 	/* Indicates that the netif is ready to receive incoming packets */
82 	bool allow_rx;
83 
84 };
85 
86 /* The i2c client structure. One per hardware i2c bus at the top of the
87  * mux tree, shared by multiple netdevs
88  */
89 struct mctp_i2c_client {
90 	struct i2c_client *client;
91 	u8 lladdr;
92 
93 	struct mctp_i2c_dev *sel;
94 	struct list_head devs;
95 	spinlock_t sel_lock; /* Protects sel and devs */
96 
97 	struct list_head list; /* For driver_clients */
98 };
99 
100 /* Header on the wire. */
101 struct mctp_i2c_hdr {
102 	u8 dest_slave;
103 	u8 command;
104 	/* Count of bytes following byte_count, excluding PEC */
105 	u8 byte_count;
106 	u8 source_slave;
107 };
108 
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);
114 
115 static struct i2c_adapter *mux_root_adapter(struct i2c_adapter *adap)
116 {
117 #if IS_ENABLED(CONFIG_I2C_MUX)
118 	return i2c_root_adapter(&adap->dev);
119 #else
120 	/* In non-mux config all i2c adapters are root adapters */
121 	return adap;
122 #endif
123 }
124 
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.
128  */
129 static struct mctp_i2c_client *mctp_i2c_new_client(struct i2c_client *client)
130 {
131 	struct mctp_i2c_client *mcli = NULL;
132 	struct i2c_adapter *root = NULL;
133 	int rc;
134 
135 	if (client->flags & I2C_CLIENT_TEN) {
136 		dev_err(&client->dev, "failed, MCTP requires a 7-bit I2C address, addr=0x%x\n",
137 			client->addr);
138 		rc = -EINVAL;
139 		goto err;
140 	}
141 
142 	root = mux_root_adapter(client->adapter);
143 	if (!root) {
144 		dev_err(&client->dev, "failed to find root adapter\n");
145 		rc = -ENOENT;
146 		goto err;
147 	}
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");
153 		rc = -EINVAL;
154 		goto err;
155 	}
156 
157 	mcli = kzalloc(sizeof(*mcli), GFP_KERNEL);
158 	if (!mcli) {
159 		rc = -ENOMEM;
160 		goto err;
161 	}
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);
168 
169 	rc = i2c_slave_register(mcli->client, mctp_i2c_slave_cb);
170 	if (rc < 0) {
171 		dev_err(&client->dev, "i2c register failed %d\n", rc);
172 		mcli->client = NULL;
173 		i2c_set_clientdata(client, NULL);
174 		goto err;
175 	}
176 
177 	return mcli;
178 err:
179 	if (mcli) {
180 		if (mcli->client)
181 			i2c_unregister_device(mcli->client);
182 		kfree(mcli);
183 	}
184 	return ERR_PTR(rc);
185 }
186 
187 static void mctp_i2c_free_client(struct mctp_i2c_client *mcli)
188 {
189 	int rc;
190 
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 */
194 
195 	rc = i2c_slave_unregister(mcli->client);
196 	/* Leak if it fails, we can't propagate errors upwards */
197 	if (rc < 0)
198 		dev_err(&mcli->client->dev, "i2c unregister failed %d\n", rc);
199 	else
200 		kfree(mcli);
201 }
202 
203 /* Switch the mctp i2c device to receive responses.
204  * Call with sel_lock held
205  */
206 static void __mctp_i2c_device_select(struct mctp_i2c_client *mcli,
207 				     struct mctp_i2c_dev *midev)
208 {
209 	assert_spin_locked(&mcli->sel_lock);
210 	if (midev)
211 		dev_hold(midev->ndev);
212 	if (mcli->sel)
213 		dev_put(mcli->sel->ndev);
214 	mcli->sel = midev;
215 }
216 
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)
220 {
221 	unsigned long flags;
222 
223 	spin_lock_irqsave(&mcli->sel_lock, flags);
224 	__mctp_i2c_device_select(mcli, midev);
225 	spin_unlock_irqrestore(&mcli->sel_lock, flags);
226 }
227 
228 static int mctp_i2c_slave_cb(struct i2c_client *client,
229 			     enum i2c_slave_event event, u8 *val)
230 {
231 	struct mctp_i2c_client *mcli = i2c_get_clientdata(client);
232 	struct mctp_i2c_dev *midev = NULL;
233 	unsigned long flags;
234 	int rc = 0;
235 
236 	spin_lock_irqsave(&mcli->sel_lock, flags);
237 	midev = mcli->sel;
238 	if (midev)
239 		dev_hold(midev->ndev);
240 	spin_unlock_irqrestore(&mcli->sel_lock, flags);
241 
242 	if (!midev)
243 		return 0;
244 
245 	switch (event) {
246 	case I2C_SLAVE_WRITE_RECEIVED:
247 		if (midev->rx_pos < MCTP_I2C_BUFSZ) {
248 			midev->rx_buffer[midev->rx_pos] = *val;
249 			midev->rx_pos++;
250 		} else {
251 			midev->ndev->stats.rx_over_errors++;
252 		}
253 
254 		break;
255 	case I2C_SLAVE_WRITE_REQUESTED:
256 		/* dest_slave as first byte */
257 		midev->rx_buffer[0] = mcli->lladdr << 1;
258 		midev->rx_pos = 1;
259 		break;
260 	case I2C_SLAVE_STOP:
261 		rc = mctp_i2c_recv(midev);
262 		break;
263 	default:
264 		break;
265 	}
266 
267 	dev_put(midev->ndev);
268 	return rc;
269 }
270 
271 /* Processes incoming data that has been accumulated by the slave cb */
272 static int mctp_i2c_recv(struct mctp_i2c_dev *midev)
273 {
274 	struct net_device *ndev = midev->ndev;
275 	struct mctp_i2c_hdr *hdr;
276 	struct mctp_skb_cb *cb;
277 	struct sk_buff *skb;
278 	unsigned long flags;
279 	u8 pec, calc_pec;
280 	size_t recvlen;
281 	int status;
282 
283 	/* + 1 for the PEC */
284 	if (midev->rx_pos < MCTP_I2C_MINLEN + 1) {
285 		ndev->stats.rx_length_errors++;
286 		return -EINVAL;
287 	}
288 	/* recvlen excludes PEC */
289 	recvlen = midev->rx_pos - 1;
290 
291 	hdr = (void *)midev->rx_buffer;
292 	if (hdr->command != MCTP_I2C_COMMANDCODE) {
293 		ndev->stats.rx_dropped++;
294 		return -EINVAL;
295 	}
296 
297 	if (hdr->byte_count + offsetof(struct mctp_i2c_hdr, source_slave) != recvlen) {
298 		ndev->stats.rx_length_errors++;
299 		return -EINVAL;
300 	}
301 
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++;
306 		return -EINVAL;
307 	}
308 
309 	skb = netdev_alloc_skb(ndev, recvlen);
310 	if (!skb) {
311 		ndev->stats.rx_dropped++;
312 		return -ENOMEM;
313 	}
314 
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);
320 
321 	cb = __mctp_cb(skb);
322 	cb->halen = 1;
323 	cb->haddr[0] = hdr->source_slave >> 1;
324 
325 	/* We need to ensure that the netif is not used once netdev
326 	 * unregister occurs
327 	 */
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);
332 
333 		status = netif_rx(skb);
334 		complete(&midev->rx_done);
335 	} else {
336 		status = NET_RX_DROP;
337 		spin_unlock_irqrestore(&midev->lock, flags);
338 	}
339 
340 	if (status == NET_RX_SUCCESS) {
341 		ndev->stats.rx_packets++;
342 		ndev->stats.rx_bytes += recvlen;
343 	} else {
344 		ndev->stats.rx_dropped++;
345 	}
346 	return 0;
347 }
348 
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,
354 };
355 
356 static enum mctp_i2c_flow_state
357 mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev *midev, struct sk_buff *skb)
358 {
359 	enum mctp_i2c_flow_state state;
360 	struct mctp_sk_key *key;
361 	struct mctp_flow *flow;
362 	unsigned long flags;
363 
364 	flow = skb_ext_find(skb, SKB_EXT_MCTP);
365 	if (!flow)
366 		return MCTP_I2C_TX_FLOW_NONE;
367 
368 	key = flow->key;
369 	if (!key)
370 		return MCTP_I2C_TX_FLOW_NONE;
371 
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
375 	 */
376 	if (!key->valid) {
377 		state = MCTP_I2C_TX_FLOW_INVALID;
378 	} else {
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;
383 			break;
384 		case MCTP_I2C_FLOW_STATE_ACTIVE:
385 			state = MCTP_I2C_TX_FLOW_EXISTING;
386 			break;
387 		default:
388 			state = MCTP_I2C_TX_FLOW_INVALID;
389 		}
390 	}
391 
392 	spin_unlock_irqrestore(&key->lock, flags);
393 
394 	return state;
395 }
396 
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
399  * recursive locking.
400  */
401 static void mctp_i2c_lock_nest(struct mctp_i2c_dev *midev)
402 {
403 	unsigned long flags;
404 	bool lock;
405 
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);
410 
411 	if (lock)
412 		i2c_lock_bus(midev->adapter, I2C_LOCK_SEGMENT);
413 }
414 
415 static void mctp_i2c_unlock_nest(struct mctp_i2c_dev *midev)
416 {
417 	unsigned long flags;
418 	bool unlock;
419 
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);
425 
426 	if (unlock)
427 		i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
428 }
429 
430 /* Unlocks the bus if was previously locked, used for cleanup */
431 static void mctp_i2c_unlock_reset(struct mctp_i2c_dev *midev)
432 {
433 	unsigned long flags;
434 	bool unlock;
435 
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);
440 
441 	if (unlock)
442 		i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
443 }
444 
445 static void mctp_i2c_xmit(struct mctp_i2c_dev *midev, struct sk_buff *skb)
446 {
447 	struct net_device_stats *stats = &midev->ndev->stats;
448 	enum mctp_i2c_flow_state fs;
449 	struct mctp_i2c_hdr *hdr;
450 	struct i2c_msg msg = {0};
451 	u8 *pecp;
452 	int rc;
453 
454 	fs = mctp_i2c_get_tx_flow_state(midev, skb);
455 
456 	hdr = (void *)skb_mac_header(skb);
457 	/* Sanity check that packet contents matches skb length,
458 	 * and can't exceed MCTP_I2C_BUFSZ
459 	 */
460 	if (skb->len != hdr->byte_count + 3) {
461 		dev_warn_ratelimited(&midev->adapter->dev,
462 				     "Bad tx length %d vs skb %u\n",
463 				     hdr->byte_count + 3, skb->len);
464 		return;
465 	}
466 
467 	if (skb_tailroom(skb) >= 1) {
468 		/* Linear case with space, we can just append the PEC */
469 		skb_put(skb, 1);
470 	} else {
471 		/* Otherwise need to copy the buffer */
472 		skb_copy_bits(skb, 0, midev->tx_scratch, skb->len);
473 		hdr = (void *)midev->tx_scratch;
474 	}
475 
476 	pecp = (void *)&hdr->source_slave + hdr->byte_count;
477 	*pecp = i2c_smbus_pec(0, (u8 *)hdr, hdr->byte_count + 3);
478 	msg.buf = (void *)&hdr->command;
479 	/* command, bytecount, data, pec */
480 	msg.len = 2 + hdr->byte_count + 1;
481 	msg.addr = hdr->dest_slave >> 1;
482 
483 	switch (fs) {
484 	case MCTP_I2C_TX_FLOW_NONE:
485 		/* no flow: full lock & unlock */
486 		mctp_i2c_lock_nest(midev);
487 		mctp_i2c_device_select(midev->client, midev);
488 		rc = __i2c_transfer(midev->adapter, &msg, 1);
489 		mctp_i2c_unlock_nest(midev);
490 		break;
491 
492 	case MCTP_I2C_TX_FLOW_NEW:
493 		/* new flow: lock, tx, but don't unlock; that will happen
494 		 * on flow release
495 		 */
496 		mctp_i2c_lock_nest(midev);
497 		mctp_i2c_device_select(midev->client, midev);
498 		fallthrough;
499 
500 	case MCTP_I2C_TX_FLOW_EXISTING:
501 		/* existing flow: we already have the lock; just tx */
502 		rc = __i2c_transfer(midev->adapter, &msg, 1);
503 		break;
504 
505 	case MCTP_I2C_TX_FLOW_INVALID:
506 		return;
507 	}
508 
509 	if (rc < 0) {
510 		dev_warn_ratelimited(&midev->adapter->dev,
511 				     "__i2c_transfer failed %d\n", rc);
512 		stats->tx_errors++;
513 	} else {
514 		stats->tx_bytes += skb->len;
515 		stats->tx_packets++;
516 	}
517 }
518 
519 static void mctp_i2c_flow_release(struct mctp_i2c_dev *midev)
520 {
521 	unsigned long flags;
522 	bool unlock;
523 
524 	spin_lock_irqsave(&midev->lock, flags);
525 	if (midev->release_count > midev->i2c_lock_count) {
526 		WARN_ONCE(1, "release count overflow");
527 		midev->release_count = midev->i2c_lock_count;
528 	}
529 
530 	midev->i2c_lock_count -= midev->release_count;
531 	unlock = midev->i2c_lock_count == 0 && midev->release_count > 0;
532 	midev->release_count = 0;
533 	spin_unlock_irqrestore(&midev->lock, flags);
534 
535 	if (unlock)
536 		i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
537 }
538 
539 static int mctp_i2c_header_create(struct sk_buff *skb, struct net_device *dev,
540 				  unsigned short type, const void *daddr,
541 	   const void *saddr, unsigned int len)
542 {
543 	struct mctp_i2c_hdr *hdr;
544 	struct mctp_hdr *mhdr;
545 	u8 lldst, llsrc;
546 
547 	if (len > MCTP_I2C_MAXMTU)
548 		return -EMSGSIZE;
549 
550 	lldst = *((u8 *)daddr);
551 	llsrc = *((u8 *)saddr);
552 
553 	skb_push(skb, sizeof(struct mctp_i2c_hdr));
554 	skb_reset_mac_header(skb);
555 	hdr = (void *)skb_mac_header(skb);
556 	mhdr = mctp_hdr(skb);
557 	hdr->dest_slave = (lldst << 1) & 0xff;
558 	hdr->command = MCTP_I2C_COMMANDCODE;
559 	hdr->byte_count = len + 1;
560 	hdr->source_slave = ((llsrc << 1) & 0xff) | 0x01;
561 	mhdr->ver = 0x01;
562 
563 	return sizeof(struct mctp_i2c_hdr);
564 }
565 
566 static int mctp_i2c_tx_thread(void *data)
567 {
568 	struct mctp_i2c_dev *midev = data;
569 	struct sk_buff *skb;
570 	unsigned long flags;
571 
572 	for (;;) {
573 		if (kthread_should_stop())
574 			break;
575 
576 		spin_lock_irqsave(&midev->tx_queue.lock, flags);
577 		skb = __skb_dequeue(&midev->tx_queue);
578 		if (netif_queue_stopped(midev->ndev))
579 			netif_wake_queue(midev->ndev);
580 		spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
581 
582 		if (skb == &midev->unlock_marker) {
583 			mctp_i2c_flow_release(midev);
584 
585 		} else if (skb) {
586 			mctp_i2c_xmit(midev, skb);
587 			kfree_skb(skb);
588 
589 		} else {
590 			wait_event_idle(midev->tx_wq,
591 					!skb_queue_empty(&midev->tx_queue) ||
592 				   kthread_should_stop());
593 		}
594 	}
595 
596 	return 0;
597 }
598 
599 static netdev_tx_t mctp_i2c_start_xmit(struct sk_buff *skb,
600 				       struct net_device *dev)
601 {
602 	struct mctp_i2c_dev *midev = netdev_priv(dev);
603 	unsigned long flags;
604 
605 	spin_lock_irqsave(&midev->tx_queue.lock, flags);
606 	if (skb_queue_len(&midev->tx_queue) >= MCTP_I2C_TX_WORK_LEN) {
607 		netif_stop_queue(dev);
608 		spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
609 		netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
610 		return NETDEV_TX_BUSY;
611 	}
612 
613 	__skb_queue_tail(&midev->tx_queue, skb);
614 	if (skb_queue_len(&midev->tx_queue) == MCTP_I2C_TX_WORK_LEN)
615 		netif_stop_queue(dev);
616 	spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
617 
618 	wake_up(&midev->tx_wq);
619 	return NETDEV_TX_OK;
620 }
621 
622 static void mctp_i2c_release_flow(struct mctp_dev *mdev,
623 				  struct mctp_sk_key *key)
624 
625 {
626 	struct mctp_i2c_dev *midev = netdev_priv(mdev->dev);
627 	bool queue_release = false;
628 	unsigned long flags;
629 
630 	spin_lock_irqsave(&midev->lock, flags);
631 	/* if we have seen the flow/key previously, we need to pair the
632 	 * original lock with a release
633 	 */
634 	if (key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE) {
635 		midev->release_count++;
636 		queue_release = true;
637 	}
638 	key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID;
639 	spin_unlock_irqrestore(&midev->lock, flags);
640 
641 	if (queue_release) {
642 		/* Ensure we have a release operation queued, through the fake
643 		 * marker skb
644 		 */
645 		spin_lock(&midev->tx_queue.lock);
646 		if (!midev->unlock_marker.next)
647 			__skb_queue_tail(&midev->tx_queue,
648 					 &midev->unlock_marker);
649 		spin_unlock(&midev->tx_queue.lock);
650 		wake_up(&midev->tx_wq);
651 	}
652 }
653 
654 static const struct net_device_ops mctp_i2c_ops = {
655 	.ndo_start_xmit = mctp_i2c_start_xmit,
656 	.ndo_uninit = mctp_i2c_ndo_uninit,
657 	.ndo_open = mctp_i2c_ndo_open,
658 };
659 
660 static const struct header_ops mctp_i2c_headops = {
661 	.create = mctp_i2c_header_create,
662 };
663 
664 static const struct mctp_netdev_ops mctp_i2c_mctp_ops = {
665 	.release_flow = mctp_i2c_release_flow,
666 };
667 
668 static void mctp_i2c_net_setup(struct net_device *dev)
669 {
670 	dev->type = ARPHRD_MCTP;
671 
672 	dev->mtu = MCTP_I2C_MAXMTU;
673 	dev->min_mtu = MCTP_I2C_MINMTU;
674 	dev->max_mtu = MCTP_I2C_MAXMTU;
675 	dev->tx_queue_len = MCTP_I2C_TX_QUEUE_LEN;
676 
677 	dev->hard_header_len = sizeof(struct mctp_i2c_hdr);
678 	dev->addr_len = 1;
679 
680 	dev->netdev_ops		= &mctp_i2c_ops;
681 	dev->header_ops		= &mctp_i2c_headops;
682 }
683 
684 /* Populates the mctp_i2c_dev priv struct for a netdev.
685  * Returns an error pointer on failure.
686  */
687 static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
688 						struct mctp_i2c_client *mcli,
689 						struct i2c_adapter *adap)
690 {
691 	struct mctp_i2c_dev *midev = netdev_priv(dev);
692 	unsigned long flags;
693 
694 	midev->tx_thread = kthread_create(mctp_i2c_tx_thread, midev,
695 					  "%s/tx", dev->name);
696 	if (IS_ERR(midev->tx_thread))
697 		return ERR_CAST(midev->tx_thread);
698 
699 	midev->ndev = dev;
700 	get_device(&adap->dev);
701 	midev->adapter = adap;
702 	get_device(&mcli->client->dev);
703 	midev->client = mcli;
704 	INIT_LIST_HEAD(&midev->list);
705 	spin_lock_init(&midev->lock);
706 	midev->i2c_lock_count = 0;
707 	midev->release_count = 0;
708 	init_completion(&midev->rx_done);
709 	complete(&midev->rx_done);
710 	init_waitqueue_head(&midev->tx_wq);
711 	skb_queue_head_init(&midev->tx_queue);
712 
713 	/* Add to the parent mcli */
714 	spin_lock_irqsave(&mcli->sel_lock, flags);
715 	list_add(&midev->list, &mcli->devs);
716 	/* Select a device by default */
717 	if (!mcli->sel)
718 		__mctp_i2c_device_select(mcli, midev);
719 	spin_unlock_irqrestore(&mcli->sel_lock, flags);
720 
721 	/* Start the worker thread */
722 	wake_up_process(midev->tx_thread);
723 
724 	return midev;
725 }
726 
727 /* Counterpart of mctp_i2c_midev_init */
728 static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)
729 {
730 	struct mctp_i2c_client *mcli = midev->client;
731 	unsigned long flags;
732 
733 	if (midev->tx_thread) {
734 		kthread_stop(midev->tx_thread);
735 		midev->tx_thread = NULL;
736 	}
737 
738 	/* Unconditionally unlock on close */
739 	mctp_i2c_unlock_reset(midev);
740 
741 	/* Remove the netdev from the parent i2c client. */
742 	spin_lock_irqsave(&mcli->sel_lock, flags);
743 	list_del(&midev->list);
744 	if (mcli->sel == midev) {
745 		struct mctp_i2c_dev *first;
746 
747 		first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list);
748 		__mctp_i2c_device_select(mcli, first);
749 	}
750 	spin_unlock_irqrestore(&mcli->sel_lock, flags);
751 
752 	skb_queue_purge(&midev->tx_queue);
753 	put_device(&midev->adapter->dev);
754 	put_device(&mcli->client->dev);
755 }
756 
757 /* Stops, unregisters, and frees midev */
758 static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)
759 {
760 	unsigned long flags;
761 
762 	/* Stop tx thread prior to unregister, it uses netif_() functions */
763 	kthread_stop(midev->tx_thread);
764 	midev->tx_thread = NULL;
765 
766 	/* Prevent any new rx in mctp_i2c_recv(), let any pending work finish */
767 	spin_lock_irqsave(&midev->lock, flags);
768 	midev->allow_rx = false;
769 	spin_unlock_irqrestore(&midev->lock, flags);
770 	wait_for_completion(&midev->rx_done);
771 
772 	mctp_unregister_netdev(midev->ndev);
773 	/* midev has been freed now by mctp_i2c_ndo_uninit callback */
774 
775 	free_netdev(midev->ndev);
776 }
777 
778 static void mctp_i2c_ndo_uninit(struct net_device *dev)
779 {
780 	struct mctp_i2c_dev *midev = netdev_priv(dev);
781 
782 	/* Perform cleanup here to ensure that mcli->sel isn't holding
783 	 * a reference that would prevent unregister_netdevice()
784 	 * from completing.
785 	 */
786 	mctp_i2c_midev_free(midev);
787 }
788 
789 static int mctp_i2c_ndo_open(struct net_device *dev)
790 {
791 	struct mctp_i2c_dev *midev = netdev_priv(dev);
792 	unsigned long flags;
793 
794 	/* i2c rx handler can only pass packets once the netdev is registered */
795 	spin_lock_irqsave(&midev->lock, flags);
796 	midev->allow_rx = true;
797 	spin_unlock_irqrestore(&midev->lock, flags);
798 
799 	return 0;
800 }
801 
802 static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
803 			       struct i2c_adapter *adap)
804 {
805 	struct mctp_i2c_dev *midev = NULL;
806 	struct net_device *ndev = NULL;
807 	struct i2c_adapter *root;
808 	unsigned long flags;
809 	char namebuf[30];
810 	int rc;
811 
812 	root = mux_root_adapter(adap);
813 	if (root != mcli->client->adapter) {
814 		dev_err(&mcli->client->dev,
815 			"I2C adapter %s is not a child bus of %s\n",
816 			mcli->client->adapter->name, root->name);
817 		return -EINVAL;
818 	}
819 
820 	WARN_ON(!mutex_is_locked(&driver_clients_lock));
821 	snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr);
822 	ndev = alloc_netdev(sizeof(*midev), namebuf, NET_NAME_ENUM, mctp_i2c_net_setup);
823 	if (!ndev) {
824 		dev_err(&mcli->client->dev, "alloc netdev failed\n");
825 		rc = -ENOMEM;
826 		goto err;
827 	}
828 	dev_net_set(ndev, current->nsproxy->net_ns);
829 	SET_NETDEV_DEV(ndev, &adap->dev);
830 	dev_addr_set(ndev, &mcli->lladdr);
831 
832 	midev = mctp_i2c_midev_init(ndev, mcli, adap);
833 	if (IS_ERR(midev)) {
834 		rc = PTR_ERR(midev);
835 		midev = NULL;
836 		goto err;
837 	}
838 
839 	rc = mctp_register_netdev(ndev, &mctp_i2c_mctp_ops);
840 	if (rc < 0) {
841 		dev_err(&mcli->client->dev,
842 			"register netdev \"%s\" failed %d\n",
843 			ndev->name, rc);
844 		goto err;
845 	}
846 
847 	spin_lock_irqsave(&midev->lock, flags);
848 	midev->allow_rx = false;
849 	spin_unlock_irqrestore(&midev->lock, flags);
850 
851 	return 0;
852 err:
853 	if (midev)
854 		mctp_i2c_midev_free(midev);
855 	if (ndev)
856 		free_netdev(ndev);
857 	return rc;
858 }
859 
860 /* Removes any netdev for adap. mcli is the parent root i2c client */
861 static void mctp_i2c_remove_netdev(struct mctp_i2c_client *mcli,
862 				   struct i2c_adapter *adap)
863 {
864 	struct mctp_i2c_dev *midev = NULL, *m = NULL;
865 	unsigned long flags;
866 
867 	WARN_ON(!mutex_is_locked(&driver_clients_lock));
868 	spin_lock_irqsave(&mcli->sel_lock, flags);
869 	/* List size is limited by number of MCTP netdevs on a single hardware bus */
870 	list_for_each_entry(m, &mcli->devs, list)
871 		if (m->adapter == adap) {
872 			midev = m;
873 			break;
874 		}
875 	spin_unlock_irqrestore(&mcli->sel_lock, flags);
876 
877 	if (midev)
878 		mctp_i2c_unregister(midev);
879 }
880 
881 /* Determines whether a device is an i2c adapter.
882  * Optionally returns the root i2c_adapter
883  */
884 static struct i2c_adapter *mctp_i2c_get_adapter(struct device *dev,
885 						struct i2c_adapter **ret_root)
886 {
887 	struct i2c_adapter *root, *adap;
888 
889 	if (dev->type != &i2c_adapter_type)
890 		return NULL;
891 	adap = to_i2c_adapter(dev);
892 	root = mux_root_adapter(adap);
893 	WARN_ONCE(!root, "MCTP I2C failed to find root adapter for %s\n",
894 		  dev_name(dev));
895 	if (!root)
896 		return NULL;
897 	if (ret_root)
898 		*ret_root = root;
899 	return adap;
900 }
901 
902 /* Determines whether a device is an i2c adapter with the "mctp-controller"
903  * devicetree property set. If adap is not an OF node, returns match_no_of
904  */
905 static bool mctp_i2c_adapter_match(struct i2c_adapter *adap, bool match_no_of)
906 {
907 	if (!adap->dev.of_node)
908 		return match_no_of;
909 	return of_property_read_bool(adap->dev.of_node, MCTP_I2C_OF_PROP);
910 }
911 
912 /* Called for each existing i2c device (adapter or client) when a
913  * new mctp-i2c client is probed.
914  */
915 static int mctp_i2c_client_try_attach(struct device *dev, void *data)
916 {
917 	struct i2c_adapter *adap = NULL, *root = NULL;
918 	struct mctp_i2c_client *mcli = data;
919 
920 	adap = mctp_i2c_get_adapter(dev, &root);
921 	if (!adap)
922 		return 0;
923 	if (mcli->client->adapter != root)
924 		return 0;
925 	/* Must either have mctp-controller property on the adapter, or
926 	 * be a root adapter if it's non-devicetree
927 	 */
928 	if (!mctp_i2c_adapter_match(adap, adap == root))
929 		return 0;
930 
931 	return mctp_i2c_add_netdev(mcli, adap);
932 }
933 
934 static void mctp_i2c_notify_add(struct device *dev)
935 {
936 	struct mctp_i2c_client *mcli = NULL, *m = NULL;
937 	struct i2c_adapter *root = NULL, *adap = NULL;
938 	int rc;
939 
940 	adap = mctp_i2c_get_adapter(dev, &root);
941 	if (!adap)
942 		return;
943 	/* Check for mctp-controller property on the adapter */
944 	if (!mctp_i2c_adapter_match(adap, false))
945 		return;
946 
947 	/* Find an existing mcli for adap's root */
948 	mutex_lock(&driver_clients_lock);
949 	list_for_each_entry(m, &driver_clients, list) {
950 		if (m->client->adapter == root) {
951 			mcli = m;
952 			break;
953 		}
954 	}
955 
956 	if (mcli) {
957 		rc = mctp_i2c_add_netdev(mcli, adap);
958 		if (rc < 0)
959 			dev_warn(dev, "Failed adding mctp-i2c net device\n");
960 	}
961 	mutex_unlock(&driver_clients_lock);
962 }
963 
964 static void mctp_i2c_notify_del(struct device *dev)
965 {
966 	struct i2c_adapter *root = NULL, *adap = NULL;
967 	struct mctp_i2c_client *mcli = NULL;
968 
969 	adap = mctp_i2c_get_adapter(dev, &root);
970 	if (!adap)
971 		return;
972 
973 	mutex_lock(&driver_clients_lock);
974 	list_for_each_entry(mcli, &driver_clients, list) {
975 		if (mcli->client->adapter == root) {
976 			mctp_i2c_remove_netdev(mcli, adap);
977 			break;
978 		}
979 	}
980 	mutex_unlock(&driver_clients_lock);
981 }
982 
983 static int mctp_i2c_probe(struct i2c_client *client)
984 {
985 	struct mctp_i2c_client *mcli = NULL;
986 	int rc;
987 
988 	mutex_lock(&driver_clients_lock);
989 	mcli = mctp_i2c_new_client(client);
990 	if (IS_ERR(mcli)) {
991 		rc = PTR_ERR(mcli);
992 		mcli = NULL;
993 		goto out;
994 	} else {
995 		list_add(&mcli->list, &driver_clients);
996 	}
997 
998 	/* Add a netdev for adapters that have a 'mctp-controller' property */
999 	i2c_for_each_dev(mcli, mctp_i2c_client_try_attach);
1000 	rc = 0;
1001 out:
1002 	mutex_unlock(&driver_clients_lock);
1003 	return rc;
1004 }
1005 
1006 static void mctp_i2c_remove(struct i2c_client *client)
1007 {
1008 	struct mctp_i2c_client *mcli = i2c_get_clientdata(client);
1009 	struct mctp_i2c_dev *midev = NULL, *tmp = NULL;
1010 
1011 	mutex_lock(&driver_clients_lock);
1012 	list_del(&mcli->list);
1013 	/* Remove all child adapter netdevs */
1014 	list_for_each_entry_safe(midev, tmp, &mcli->devs, list)
1015 		mctp_i2c_unregister(midev);
1016 
1017 	mctp_i2c_free_client(mcli);
1018 	mutex_unlock(&driver_clients_lock);
1019 }
1020 
1021 /* We look for a 'mctp-controller' property on I2C busses as they are
1022  * added/deleted, creating/removing netdevs as required.
1023  */
1024 static int mctp_i2c_notifier_call(struct notifier_block *nb,
1025 				  unsigned long action, void *data)
1026 {
1027 	struct device *dev = data;
1028 
1029 	switch (action) {
1030 	case BUS_NOTIFY_ADD_DEVICE:
1031 		mctp_i2c_notify_add(dev);
1032 		break;
1033 	case BUS_NOTIFY_DEL_DEVICE:
1034 		mctp_i2c_notify_del(dev);
1035 		break;
1036 	}
1037 	return NOTIFY_DONE;
1038 }
1039 
1040 static struct notifier_block mctp_i2c_notifier = {
1041 	.notifier_call = mctp_i2c_notifier_call,
1042 };
1043 
1044 static const struct i2c_device_id mctp_i2c_id[] = {
1045 	{ "mctp-i2c-interface", 0 },
1046 	{},
1047 };
1048 MODULE_DEVICE_TABLE(i2c, mctp_i2c_id);
1049 
1050 static const struct of_device_id mctp_i2c_of_match[] = {
1051 	{ .compatible = "mctp-i2c-controller" },
1052 	{},
1053 };
1054 MODULE_DEVICE_TABLE(of, mctp_i2c_of_match);
1055 
1056 static struct i2c_driver mctp_i2c_driver = {
1057 	.driver = {
1058 		.name = "mctp-i2c-interface",
1059 		.of_match_table = mctp_i2c_of_match,
1060 	},
1061 	.probe_new = mctp_i2c_probe,
1062 	.remove = mctp_i2c_remove,
1063 	.id_table = mctp_i2c_id,
1064 };
1065 
1066 static __init int mctp_i2c_mod_init(void)
1067 {
1068 	int rc;
1069 
1070 	pr_info("MCTP I2C interface driver\n");
1071 	rc = i2c_add_driver(&mctp_i2c_driver);
1072 	if (rc < 0)
1073 		return rc;
1074 	rc = bus_register_notifier(&i2c_bus_type, &mctp_i2c_notifier);
1075 	if (rc < 0) {
1076 		i2c_del_driver(&mctp_i2c_driver);
1077 		return rc;
1078 	}
1079 	return 0;
1080 }
1081 
1082 static __exit void mctp_i2c_mod_exit(void)
1083 {
1084 	int rc;
1085 
1086 	rc = bus_unregister_notifier(&i2c_bus_type, &mctp_i2c_notifier);
1087 	if (rc < 0)
1088 		pr_warn("MCTP I2C could not unregister notifier, %d\n", rc);
1089 	i2c_del_driver(&mctp_i2c_driver);
1090 }
1091 
1092 module_init(mctp_i2c_mod_init);
1093 module_exit(mctp_i2c_mod_exit);
1094 
1095 MODULE_DESCRIPTION("MCTP I2C device");
1096 MODULE_LICENSE("GPL v2");
1097 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");
1098