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