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
mux_root_adapter(struct i2c_adapter * adap)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 */
mctp_i2c_new_client(struct i2c_client * client)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_obj(*mcli);
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 i2c_unregister_device(mcli->client);
181 kfree(mcli);
182 }
183 return ERR_PTR(rc);
184 }
185
mctp_i2c_free_client(struct mctp_i2c_client * mcli)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 */
__mctp_i2c_device_select(struct mctp_i2c_client * mcli,struct mctp_i2c_dev * midev)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 */
mctp_i2c_device_select(struct mctp_i2c_client * mcli,struct mctp_i2c_dev * midev)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
mctp_i2c_slave_cb(struct i2c_client * client,enum i2c_slave_event event,u8 * val)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_READ_REQUESTED:
246 case I2C_SLAVE_READ_PROCESSED:
247 /* MCTP I2C transport only uses writes */
248 midev->rx_pos = 0;
249 *val = 0xff;
250 break;
251 case I2C_SLAVE_WRITE_RECEIVED:
252 if (midev->rx_pos < MCTP_I2C_BUFSZ) {
253 midev->rx_buffer[midev->rx_pos] = *val;
254 midev->rx_pos++;
255 } else {
256 midev->ndev->stats.rx_over_errors++;
257 }
258
259 break;
260 case I2C_SLAVE_WRITE_REQUESTED:
261 /* dest_slave as first byte */
262 midev->rx_buffer[0] = mcli->lladdr << 1;
263 midev->rx_pos = 1;
264 break;
265 case I2C_SLAVE_STOP:
266 rc = mctp_i2c_recv(midev);
267 break;
268 default:
269 break;
270 }
271
272 dev_put(midev->ndev);
273 return rc;
274 }
275
276 /* Processes incoming data that has been accumulated by the slave cb */
mctp_i2c_recv(struct mctp_i2c_dev * midev)277 static int mctp_i2c_recv(struct mctp_i2c_dev *midev)
278 {
279 struct net_device *ndev = midev->ndev;
280 struct mctp_i2c_hdr *hdr;
281 struct mctp_skb_cb *cb;
282 struct sk_buff *skb;
283 unsigned long flags;
284 u8 pec, calc_pec;
285 size_t recvlen;
286 int status;
287
288 if (midev->rx_pos == 0)
289 return 0;
290
291 /* + 1 for the PEC */
292 if (midev->rx_pos < MCTP_I2C_MINLEN + 1) {
293 ndev->stats.rx_length_errors++;
294 return -EINVAL;
295 }
296 /* recvlen excludes PEC */
297 recvlen = midev->rx_pos - 1;
298
299 hdr = (void *)midev->rx_buffer;
300 if (hdr->command != MCTP_I2C_COMMANDCODE) {
301 ndev->stats.rx_dropped++;
302 return -EINVAL;
303 }
304
305 if (hdr->byte_count + offsetof(struct mctp_i2c_hdr, source_slave) != recvlen) {
306 ndev->stats.rx_length_errors++;
307 return -EINVAL;
308 }
309
310 pec = midev->rx_buffer[midev->rx_pos - 1];
311 calc_pec = i2c_smbus_pec(0, midev->rx_buffer, recvlen);
312 if (pec != calc_pec) {
313 ndev->stats.rx_crc_errors++;
314 return -EINVAL;
315 }
316
317 skb = netdev_alloc_skb(ndev, recvlen);
318 if (!skb) {
319 ndev->stats.rx_dropped++;
320 return -ENOMEM;
321 }
322
323 skb->protocol = htons(ETH_P_MCTP);
324 skb_put_data(skb, midev->rx_buffer, recvlen);
325 skb_reset_mac_header(skb);
326 skb_pull(skb, sizeof(struct mctp_i2c_hdr));
327 skb_reset_network_header(skb);
328
329 cb = __mctp_cb(skb);
330 cb->halen = 1;
331 cb->haddr[0] = hdr->source_slave >> 1;
332
333 /* We need to ensure that the netif is not used once netdev
334 * unregister occurs
335 */
336 spin_lock_irqsave(&midev->lock, flags);
337 if (midev->allow_rx) {
338 reinit_completion(&midev->rx_done);
339 spin_unlock_irqrestore(&midev->lock, flags);
340
341 status = netif_rx(skb);
342 complete(&midev->rx_done);
343 } else {
344 status = NET_RX_DROP;
345 spin_unlock_irqrestore(&midev->lock, flags);
346 kfree_skb(skb);
347 }
348
349 if (status == NET_RX_SUCCESS) {
350 ndev->stats.rx_packets++;
351 ndev->stats.rx_bytes += recvlen;
352 } else {
353 ndev->stats.rx_dropped++;
354 }
355 return 0;
356 }
357
358 enum mctp_i2c_flow_state {
359 MCTP_I2C_TX_FLOW_INVALID,
360 MCTP_I2C_TX_FLOW_NONE,
361 MCTP_I2C_TX_FLOW_NEW,
362 MCTP_I2C_TX_FLOW_EXISTING,
363 };
364
365 static enum mctp_i2c_flow_state
mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev * midev,struct sk_buff * skb)366 mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev *midev, struct sk_buff *skb)
367 {
368 enum mctp_i2c_flow_state state;
369 struct mctp_sk_key *key;
370 struct mctp_flow *flow;
371 unsigned long flags;
372
373 flow = skb_ext_find(skb, SKB_EXT_MCTP);
374 if (!flow)
375 return MCTP_I2C_TX_FLOW_NONE;
376
377 key = flow->key;
378 if (!key)
379 return MCTP_I2C_TX_FLOW_NONE;
380
381 spin_lock_irqsave(&key->lock, flags);
382 /* If the key is present but invalid, we're unlikely to be able
383 * to handle the flow at all; just drop now
384 */
385 if (!key->valid) {
386 state = MCTP_I2C_TX_FLOW_INVALID;
387 } else {
388 switch (key->dev_flow_state) {
389 case MCTP_I2C_FLOW_STATE_NEW:
390 key->dev_flow_state = MCTP_I2C_FLOW_STATE_ACTIVE;
391 state = MCTP_I2C_TX_FLOW_NEW;
392 break;
393 case MCTP_I2C_FLOW_STATE_ACTIVE:
394 state = MCTP_I2C_TX_FLOW_EXISTING;
395 break;
396 default:
397 state = MCTP_I2C_TX_FLOW_INVALID;
398 }
399 }
400
401 spin_unlock_irqrestore(&key->lock, flags);
402
403 return state;
404 }
405
406 /* We're not contending with ourselves here; we only need to exclude other
407 * i2c clients from using the bus. refcounts are simply to prevent
408 * recursive locking.
409 */
mctp_i2c_lock_nest(struct mctp_i2c_dev * midev)410 static void mctp_i2c_lock_nest(struct mctp_i2c_dev *midev)
411 {
412 unsigned long flags;
413 bool lock;
414
415 spin_lock_irqsave(&midev->lock, flags);
416 lock = midev->i2c_lock_count == 0;
417 midev->i2c_lock_count++;
418 spin_unlock_irqrestore(&midev->lock, flags);
419
420 if (lock)
421 i2c_lock_bus(midev->adapter, I2C_LOCK_SEGMENT);
422 }
423
mctp_i2c_unlock_nest(struct mctp_i2c_dev * midev)424 static void mctp_i2c_unlock_nest(struct mctp_i2c_dev *midev)
425 {
426 unsigned long flags;
427 bool unlock;
428
429 spin_lock_irqsave(&midev->lock, flags);
430 if (!WARN_ONCE(midev->i2c_lock_count == 0, "lock count underflow!"))
431 midev->i2c_lock_count--;
432 unlock = midev->i2c_lock_count == 0;
433 spin_unlock_irqrestore(&midev->lock, flags);
434
435 if (unlock)
436 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
437 }
438
439 /* Unlocks the bus if was previously locked, used for cleanup */
mctp_i2c_unlock_reset(struct mctp_i2c_dev * midev)440 static void mctp_i2c_unlock_reset(struct mctp_i2c_dev *midev)
441 {
442 unsigned long flags;
443 bool unlock;
444
445 spin_lock_irqsave(&midev->lock, flags);
446 unlock = midev->i2c_lock_count > 0;
447 midev->i2c_lock_count = 0;
448 spin_unlock_irqrestore(&midev->lock, flags);
449
450 if (unlock)
451 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
452 }
453
mctp_i2c_invalidate_tx_flow(struct mctp_i2c_dev * midev,struct sk_buff * skb)454 static void mctp_i2c_invalidate_tx_flow(struct mctp_i2c_dev *midev,
455 struct sk_buff *skb)
456 {
457 struct mctp_sk_key *key;
458 struct mctp_flow *flow;
459 unsigned long flags;
460 bool release;
461
462 flow = skb_ext_find(skb, SKB_EXT_MCTP);
463 if (!flow)
464 return;
465
466 key = flow->key;
467 if (!key)
468 return;
469
470 spin_lock_irqsave(&key->lock, flags);
471 if (key->manual_alloc) {
472 /* we don't have control over lifetimes for manually-allocated
473 * keys, so cannot assume we can invalidate all future flows
474 * that would use this key.
475 */
476 release = false;
477 } else {
478 release = key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE;
479 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID;
480 }
481 spin_unlock_irqrestore(&key->lock, flags);
482
483 /* if we have changed state from active, the flow held a reference on
484 * the lock; release that now.
485 */
486 if (release)
487 mctp_i2c_unlock_nest(midev);
488 }
489
mctp_i2c_xmit(struct mctp_i2c_dev * midev,struct sk_buff * skb)490 static void mctp_i2c_xmit(struct mctp_i2c_dev *midev, struct sk_buff *skb)
491 {
492 struct net_device_stats *stats = &midev->ndev->stats;
493 enum mctp_i2c_flow_state fs;
494 struct mctp_i2c_hdr *hdr;
495 struct i2c_msg msg = {0};
496 u8 *pecp;
497 int rc;
498
499 fs = mctp_i2c_get_tx_flow_state(midev, skb);
500
501 hdr = (void *)skb_mac_header(skb);
502 /* Sanity check that packet contents matches skb length,
503 * and can't exceed MCTP_I2C_BUFSZ
504 */
505 if (skb->len != hdr->byte_count + 3) {
506 dev_warn_ratelimited(&midev->adapter->dev,
507 "Bad tx length %d vs skb %u\n",
508 hdr->byte_count + 3, skb->len);
509 return;
510 }
511
512 if (skb_tailroom(skb) >= 1) {
513 /* Linear case with space, we can just append the PEC */
514 skb_put(skb, 1);
515 } else {
516 /* Otherwise need to copy the buffer */
517 skb_copy_bits(skb, 0, midev->tx_scratch, skb->len);
518 hdr = (void *)midev->tx_scratch;
519 }
520
521 pecp = (void *)&hdr->source_slave + hdr->byte_count;
522 *pecp = i2c_smbus_pec(0, (u8 *)hdr, hdr->byte_count + 3);
523 msg.buf = (void *)&hdr->command;
524 /* command, bytecount, data, pec */
525 msg.len = 2 + hdr->byte_count + 1;
526 msg.addr = hdr->dest_slave >> 1;
527
528 switch (fs) {
529 case MCTP_I2C_TX_FLOW_NONE:
530 /* no flow: full lock & unlock */
531 mctp_i2c_lock_nest(midev);
532 mctp_i2c_device_select(midev->client, midev);
533 rc = __i2c_transfer(midev->adapter, &msg, 1);
534 mctp_i2c_unlock_nest(midev);
535 break;
536
537 case MCTP_I2C_TX_FLOW_NEW:
538 /* new flow: lock, tx, but don't unlock; that will happen
539 * on flow release
540 */
541 mctp_i2c_lock_nest(midev);
542 mctp_i2c_device_select(midev->client, midev);
543 fallthrough;
544
545 case MCTP_I2C_TX_FLOW_EXISTING:
546 /* existing flow: we already have the lock; just tx */
547 rc = __i2c_transfer(midev->adapter, &msg, 1);
548
549 /* on tx errors, the flow can no longer be considered valid */
550 if (rc < 0)
551 mctp_i2c_invalidate_tx_flow(midev, skb);
552
553 break;
554
555 case MCTP_I2C_TX_FLOW_INVALID:
556 return;
557 }
558
559 if (rc < 0) {
560 dev_warn_ratelimited(&midev->adapter->dev,
561 "__i2c_transfer failed %d\n", rc);
562 stats->tx_errors++;
563 } else {
564 stats->tx_bytes += skb->len;
565 stats->tx_packets++;
566 }
567 }
568
mctp_i2c_flow_release(struct mctp_i2c_dev * midev)569 static void mctp_i2c_flow_release(struct mctp_i2c_dev *midev)
570 {
571 unsigned long flags;
572 bool unlock;
573
574 spin_lock_irqsave(&midev->lock, flags);
575 if (midev->release_count > midev->i2c_lock_count) {
576 WARN_ONCE(1, "release count overflow");
577 midev->release_count = midev->i2c_lock_count;
578 }
579
580 midev->i2c_lock_count -= midev->release_count;
581 unlock = midev->i2c_lock_count == 0 && midev->release_count > 0;
582 midev->release_count = 0;
583 spin_unlock_irqrestore(&midev->lock, flags);
584
585 if (unlock)
586 i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
587 }
588
mctp_i2c_header_create(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)589 static int mctp_i2c_header_create(struct sk_buff *skb, struct net_device *dev,
590 unsigned short type, const void *daddr,
591 const void *saddr, unsigned int len)
592 {
593 struct mctp_i2c_hdr *hdr;
594 struct mctp_hdr *mhdr;
595 u8 lldst, llsrc;
596 int rc;
597
598 if (len > MCTP_I2C_MAXMTU)
599 return -EMSGSIZE;
600
601 if (!daddr || !saddr)
602 return -EINVAL;
603
604 lldst = *((u8 *)daddr);
605 llsrc = *((u8 *)saddr);
606
607 rc = skb_cow_head(skb, sizeof(struct mctp_i2c_hdr));
608 if (rc)
609 return rc;
610
611 skb_push(skb, sizeof(struct mctp_i2c_hdr));
612 skb_reset_mac_header(skb);
613 hdr = (void *)skb_mac_header(skb);
614 mhdr = mctp_hdr(skb);
615 hdr->dest_slave = (lldst << 1) & 0xff;
616 hdr->command = MCTP_I2C_COMMANDCODE;
617 hdr->byte_count = len + 1;
618 hdr->source_slave = ((llsrc << 1) & 0xff) | 0x01;
619 mhdr->ver = 0x01;
620
621 return sizeof(struct mctp_i2c_hdr);
622 }
623
mctp_i2c_tx_thread(void * data)624 static int mctp_i2c_tx_thread(void *data)
625 {
626 struct mctp_i2c_dev *midev = data;
627 struct sk_buff *skb;
628 unsigned long flags;
629
630 for (;;) {
631 if (kthread_should_stop())
632 break;
633
634 spin_lock_irqsave(&midev->tx_queue.lock, flags);
635 skb = __skb_dequeue(&midev->tx_queue);
636 if (netif_queue_stopped(midev->ndev))
637 netif_wake_queue(midev->ndev);
638 spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
639
640 if (skb == &midev->unlock_marker) {
641 mctp_i2c_flow_release(midev);
642
643 } else if (skb) {
644 mctp_i2c_xmit(midev, skb);
645 kfree_skb(skb);
646
647 } else {
648 wait_event_idle(midev->tx_wq,
649 !skb_queue_empty(&midev->tx_queue) ||
650 kthread_should_stop());
651 }
652 }
653
654 return 0;
655 }
656
mctp_i2c_start_xmit(struct sk_buff * skb,struct net_device * dev)657 static netdev_tx_t mctp_i2c_start_xmit(struct sk_buff *skb,
658 struct net_device *dev)
659 {
660 struct mctp_i2c_dev *midev = netdev_priv(dev);
661 unsigned long flags;
662
663 spin_lock_irqsave(&midev->tx_queue.lock, flags);
664 if (skb_queue_len(&midev->tx_queue) >= MCTP_I2C_TX_WORK_LEN) {
665 netif_stop_queue(dev);
666 spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
667 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
668 return NETDEV_TX_BUSY;
669 }
670
671 __skb_queue_tail(&midev->tx_queue, skb);
672 if (skb_queue_len(&midev->tx_queue) == MCTP_I2C_TX_WORK_LEN)
673 netif_stop_queue(dev);
674 spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
675
676 wake_up(&midev->tx_wq);
677 return NETDEV_TX_OK;
678 }
679
mctp_i2c_release_flow(struct mctp_dev * mdev,struct mctp_sk_key * key)680 static void mctp_i2c_release_flow(struct mctp_dev *mdev,
681 struct mctp_sk_key *key)
682
683 {
684 struct mctp_i2c_dev *midev = netdev_priv(mdev->dev);
685 bool queue_release = false;
686 unsigned long flags;
687
688 spin_lock_irqsave(&midev->lock, flags);
689 /* if we have seen the flow/key previously, we need to pair the
690 * original lock with a release
691 */
692 if (key->dev_flow_state == MCTP_I2C_FLOW_STATE_ACTIVE) {
693 midev->release_count++;
694 queue_release = true;
695 }
696 key->dev_flow_state = MCTP_I2C_FLOW_STATE_INVALID;
697 spin_unlock_irqrestore(&midev->lock, flags);
698
699 if (queue_release) {
700 /* Ensure we have a release operation queued, through the fake
701 * marker skb
702 */
703 spin_lock(&midev->tx_queue.lock);
704 if (!midev->unlock_marker.next)
705 __skb_queue_tail(&midev->tx_queue,
706 &midev->unlock_marker);
707 spin_unlock(&midev->tx_queue.lock);
708 wake_up(&midev->tx_wq);
709 }
710 }
711
712 static const struct net_device_ops mctp_i2c_ops = {
713 .ndo_start_xmit = mctp_i2c_start_xmit,
714 .ndo_uninit = mctp_i2c_ndo_uninit,
715 .ndo_open = mctp_i2c_ndo_open,
716 };
717
718 static const struct header_ops mctp_i2c_headops = {
719 .create = mctp_i2c_header_create,
720 };
721
722 static const struct mctp_netdev_ops mctp_i2c_mctp_ops = {
723 .release_flow = mctp_i2c_release_flow,
724 };
725
mctp_i2c_net_setup(struct net_device * dev)726 static void mctp_i2c_net_setup(struct net_device *dev)
727 {
728 dev->type = ARPHRD_MCTP;
729
730 dev->mtu = MCTP_I2C_MAXMTU;
731 dev->min_mtu = MCTP_I2C_MINMTU;
732 dev->max_mtu = MCTP_I2C_MAXMTU;
733 dev->tx_queue_len = MCTP_I2C_TX_QUEUE_LEN;
734
735 dev->hard_header_len = sizeof(struct mctp_i2c_hdr);
736 dev->addr_len = 1;
737
738 dev->netdev_ops = &mctp_i2c_ops;
739 dev->header_ops = &mctp_i2c_headops;
740 }
741
742 /* Populates the mctp_i2c_dev priv struct for a netdev.
743 * Returns an error pointer on failure.
744 */
mctp_i2c_midev_init(struct net_device * dev,struct mctp_i2c_client * mcli,struct i2c_adapter * adap)745 static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
746 struct mctp_i2c_client *mcli,
747 struct i2c_adapter *adap)
748 {
749 struct mctp_i2c_dev *midev = netdev_priv(dev);
750 unsigned long flags;
751
752 midev->tx_thread = kthread_create(mctp_i2c_tx_thread, midev,
753 "%s/tx", dev->name);
754 if (IS_ERR(midev->tx_thread))
755 return ERR_CAST(midev->tx_thread);
756
757 midev->ndev = dev;
758 get_device(&adap->dev);
759 midev->adapter = adap;
760 get_device(&mcli->client->dev);
761 midev->client = mcli;
762 INIT_LIST_HEAD(&midev->list);
763 spin_lock_init(&midev->lock);
764 midev->i2c_lock_count = 0;
765 midev->release_count = 0;
766 init_completion(&midev->rx_done);
767 complete(&midev->rx_done);
768 init_waitqueue_head(&midev->tx_wq);
769 skb_queue_head_init(&midev->tx_queue);
770
771 /* Add to the parent mcli */
772 spin_lock_irqsave(&mcli->sel_lock, flags);
773 list_add(&midev->list, &mcli->devs);
774 /* Select a device by default */
775 if (!mcli->sel)
776 __mctp_i2c_device_select(mcli, midev);
777 spin_unlock_irqrestore(&mcli->sel_lock, flags);
778
779 /* Start the worker thread */
780 wake_up_process(midev->tx_thread);
781
782 return midev;
783 }
784
785 /* Counterpart of mctp_i2c_midev_init */
mctp_i2c_midev_free(struct mctp_i2c_dev * midev)786 static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)
787 {
788 struct mctp_i2c_client *mcli = midev->client;
789 unsigned long flags;
790
791 if (midev->tx_thread) {
792 kthread_stop(midev->tx_thread);
793 midev->tx_thread = NULL;
794 }
795
796 /* Unconditionally unlock on close */
797 mctp_i2c_unlock_reset(midev);
798
799 /* Remove the netdev from the parent i2c client. */
800 spin_lock_irqsave(&mcli->sel_lock, flags);
801 list_del(&midev->list);
802 if (mcli->sel == midev) {
803 struct mctp_i2c_dev *first;
804
805 first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list);
806 __mctp_i2c_device_select(mcli, first);
807 }
808 spin_unlock_irqrestore(&mcli->sel_lock, flags);
809
810 skb_queue_purge(&midev->tx_queue);
811 put_device(&midev->adapter->dev);
812 put_device(&mcli->client->dev);
813 }
814
815 /* Stops, unregisters, and frees midev */
mctp_i2c_unregister(struct mctp_i2c_dev * midev)816 static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)
817 {
818 unsigned long flags;
819
820 /* Stop tx thread prior to unregister, it uses netif_() functions */
821 kthread_stop(midev->tx_thread);
822 midev->tx_thread = NULL;
823
824 /* Prevent any new rx in mctp_i2c_recv(), let any pending work finish */
825 spin_lock_irqsave(&midev->lock, flags);
826 midev->allow_rx = false;
827 spin_unlock_irqrestore(&midev->lock, flags);
828 wait_for_completion(&midev->rx_done);
829
830 mctp_unregister_netdev(midev->ndev);
831 /* midev has been freed now by mctp_i2c_ndo_uninit callback */
832
833 free_netdev(midev->ndev);
834 }
835
mctp_i2c_ndo_uninit(struct net_device * dev)836 static void mctp_i2c_ndo_uninit(struct net_device *dev)
837 {
838 struct mctp_i2c_dev *midev = netdev_priv(dev);
839
840 /* Perform cleanup here to ensure that mcli->sel isn't holding
841 * a reference that would prevent unregister_netdevice()
842 * from completing.
843 */
844 mctp_i2c_midev_free(midev);
845 }
846
mctp_i2c_ndo_open(struct net_device * dev)847 static int mctp_i2c_ndo_open(struct net_device *dev)
848 {
849 struct mctp_i2c_dev *midev = netdev_priv(dev);
850 unsigned long flags;
851
852 /* i2c rx handler can only pass packets once the netdev is registered */
853 spin_lock_irqsave(&midev->lock, flags);
854 midev->allow_rx = true;
855 spin_unlock_irqrestore(&midev->lock, flags);
856
857 return 0;
858 }
859
mctp_i2c_add_netdev(struct mctp_i2c_client * mcli,struct i2c_adapter * adap)860 static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
861 struct i2c_adapter *adap)
862 {
863 struct mctp_i2c_dev *midev = NULL;
864 struct net_device *ndev = NULL;
865 struct i2c_adapter *root;
866 unsigned long flags;
867 char namebuf[30];
868 int rc;
869
870 root = mux_root_adapter(adap);
871 if (root != mcli->client->adapter) {
872 dev_err(&mcli->client->dev,
873 "I2C adapter %s is not a child bus of %s\n",
874 mcli->client->adapter->name, root->name);
875 return -EINVAL;
876 }
877
878 WARN_ON(!mutex_is_locked(&driver_clients_lock));
879 snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr);
880 ndev = alloc_netdev(sizeof(*midev), namebuf, NET_NAME_ENUM, mctp_i2c_net_setup);
881 if (!ndev) {
882 dev_err(&mcli->client->dev, "alloc netdev failed\n");
883 rc = -ENOMEM;
884 goto err;
885 }
886 dev_net_set(ndev, current->nsproxy->net_ns);
887 SET_NETDEV_DEV(ndev, &adap->dev);
888 dev_addr_set(ndev, &mcli->lladdr);
889
890 midev = mctp_i2c_midev_init(ndev, mcli, adap);
891 if (IS_ERR(midev)) {
892 rc = PTR_ERR(midev);
893 midev = NULL;
894 goto err;
895 }
896
897 rc = mctp_register_netdev(ndev, &mctp_i2c_mctp_ops,
898 MCTP_PHYS_BINDING_SMBUS);
899 if (rc < 0) {
900 dev_err(&mcli->client->dev,
901 "register netdev \"%s\" failed %d\n",
902 ndev->name, rc);
903 goto err;
904 }
905
906 spin_lock_irqsave(&midev->lock, flags);
907 midev->allow_rx = false;
908 spin_unlock_irqrestore(&midev->lock, flags);
909
910 return 0;
911 err:
912 if (midev)
913 mctp_i2c_midev_free(midev);
914 if (ndev)
915 free_netdev(ndev);
916 return rc;
917 }
918
919 /* Removes any netdev for adap. mcli is the parent root i2c client */
mctp_i2c_remove_netdev(struct mctp_i2c_client * mcli,struct i2c_adapter * adap)920 static void mctp_i2c_remove_netdev(struct mctp_i2c_client *mcli,
921 struct i2c_adapter *adap)
922 {
923 struct mctp_i2c_dev *midev = NULL, *m = NULL;
924 unsigned long flags;
925
926 WARN_ON(!mutex_is_locked(&driver_clients_lock));
927 spin_lock_irqsave(&mcli->sel_lock, flags);
928 /* List size is limited by number of MCTP netdevs on a single hardware bus */
929 list_for_each_entry(m, &mcli->devs, list)
930 if (m->adapter == adap) {
931 midev = m;
932 break;
933 }
934 spin_unlock_irqrestore(&mcli->sel_lock, flags);
935
936 if (midev)
937 mctp_i2c_unregister(midev);
938 }
939
940 /* Determines whether a device is an i2c adapter.
941 * Optionally returns the root i2c_adapter
942 */
mctp_i2c_get_adapter(struct device * dev,struct i2c_adapter ** ret_root)943 static struct i2c_adapter *mctp_i2c_get_adapter(struct device *dev,
944 struct i2c_adapter **ret_root)
945 {
946 struct i2c_adapter *root, *adap;
947
948 if (dev->type != &i2c_adapter_type)
949 return NULL;
950 adap = to_i2c_adapter(dev);
951 root = mux_root_adapter(adap);
952 WARN_ONCE(!root, "MCTP I2C failed to find root adapter for %s\n",
953 dev_name(dev));
954 if (!root)
955 return NULL;
956 if (ret_root)
957 *ret_root = root;
958 return adap;
959 }
960
961 /* Determines whether a device is an i2c adapter with the "mctp-controller"
962 * devicetree property set. If adap is not an OF node, returns match_no_of
963 */
mctp_i2c_adapter_match(struct i2c_adapter * adap,bool match_no_of)964 static bool mctp_i2c_adapter_match(struct i2c_adapter *adap, bool match_no_of)
965 {
966 if (!adap->dev.of_node)
967 return match_no_of;
968 return of_property_read_bool(adap->dev.of_node, MCTP_I2C_OF_PROP);
969 }
970
971 /* Called for each existing i2c device (adapter or client) when a
972 * new mctp-i2c client is probed.
973 */
mctp_i2c_client_try_attach(struct device * dev,void * data)974 static int mctp_i2c_client_try_attach(struct device *dev, void *data)
975 {
976 struct i2c_adapter *adap = NULL, *root = NULL;
977 struct mctp_i2c_client *mcli = data;
978
979 adap = mctp_i2c_get_adapter(dev, &root);
980 if (!adap)
981 return 0;
982 if (mcli->client->adapter != root)
983 return 0;
984 /* Must either have mctp-controller property on the adapter, or
985 * be a root adapter if it's non-devicetree
986 */
987 if (!mctp_i2c_adapter_match(adap, adap == root))
988 return 0;
989
990 return mctp_i2c_add_netdev(mcli, adap);
991 }
992
mctp_i2c_notify_add(struct device * dev)993 static void mctp_i2c_notify_add(struct device *dev)
994 {
995 struct mctp_i2c_client *mcli = NULL, *m = NULL;
996 struct i2c_adapter *root = NULL, *adap = NULL;
997 int rc;
998
999 adap = mctp_i2c_get_adapter(dev, &root);
1000 if (!adap)
1001 return;
1002 /* Check for mctp-controller property on the adapter */
1003 if (!mctp_i2c_adapter_match(adap, false))
1004 return;
1005
1006 /* Find an existing mcli for adap's root */
1007 mutex_lock(&driver_clients_lock);
1008 list_for_each_entry(m, &driver_clients, list) {
1009 if (m->client->adapter == root) {
1010 mcli = m;
1011 break;
1012 }
1013 }
1014
1015 if (mcli) {
1016 rc = mctp_i2c_add_netdev(mcli, adap);
1017 if (rc < 0)
1018 dev_warn(dev, "Failed adding mctp-i2c net device\n");
1019 }
1020 mutex_unlock(&driver_clients_lock);
1021 }
1022
mctp_i2c_notify_del(struct device * dev)1023 static void mctp_i2c_notify_del(struct device *dev)
1024 {
1025 struct i2c_adapter *root = NULL, *adap = NULL;
1026 struct mctp_i2c_client *mcli = NULL;
1027
1028 adap = mctp_i2c_get_adapter(dev, &root);
1029 if (!adap)
1030 return;
1031
1032 mutex_lock(&driver_clients_lock);
1033 list_for_each_entry(mcli, &driver_clients, list) {
1034 if (mcli->client->adapter == root) {
1035 mctp_i2c_remove_netdev(mcli, adap);
1036 break;
1037 }
1038 }
1039 mutex_unlock(&driver_clients_lock);
1040 }
1041
mctp_i2c_probe(struct i2c_client * client)1042 static int mctp_i2c_probe(struct i2c_client *client)
1043 {
1044 struct mctp_i2c_client *mcli = NULL;
1045 int rc;
1046
1047 mutex_lock(&driver_clients_lock);
1048 mcli = mctp_i2c_new_client(client);
1049 if (IS_ERR(mcli)) {
1050 rc = PTR_ERR(mcli);
1051 mcli = NULL;
1052 goto out;
1053 } else {
1054 list_add(&mcli->list, &driver_clients);
1055 }
1056
1057 /* Add a netdev for adapters that have a 'mctp-controller' property */
1058 i2c_for_each_dev(mcli, mctp_i2c_client_try_attach);
1059 rc = 0;
1060 out:
1061 mutex_unlock(&driver_clients_lock);
1062 return rc;
1063 }
1064
mctp_i2c_remove(struct i2c_client * client)1065 static void mctp_i2c_remove(struct i2c_client *client)
1066 {
1067 struct mctp_i2c_client *mcli = i2c_get_clientdata(client);
1068 struct mctp_i2c_dev *midev = NULL, *tmp = NULL;
1069
1070 mutex_lock(&driver_clients_lock);
1071 list_del(&mcli->list);
1072 /* Remove all child adapter netdevs */
1073 list_for_each_entry_safe(midev, tmp, &mcli->devs, list)
1074 mctp_i2c_unregister(midev);
1075
1076 mctp_i2c_free_client(mcli);
1077 mutex_unlock(&driver_clients_lock);
1078 }
1079
1080 /* We look for a 'mctp-controller' property on I2C busses as they are
1081 * added/deleted, creating/removing netdevs as required.
1082 */
mctp_i2c_notifier_call(struct notifier_block * nb,unsigned long action,void * data)1083 static int mctp_i2c_notifier_call(struct notifier_block *nb,
1084 unsigned long action, void *data)
1085 {
1086 struct device *dev = data;
1087
1088 switch (action) {
1089 case BUS_NOTIFY_ADD_DEVICE:
1090 mctp_i2c_notify_add(dev);
1091 break;
1092 case BUS_NOTIFY_DEL_DEVICE:
1093 mctp_i2c_notify_del(dev);
1094 break;
1095 }
1096 return NOTIFY_DONE;
1097 }
1098
1099 static struct notifier_block mctp_i2c_notifier = {
1100 .notifier_call = mctp_i2c_notifier_call,
1101 };
1102
1103 static const struct i2c_device_id mctp_i2c_id[] = {
1104 { "mctp-i2c-interface" },
1105 {}
1106 };
1107 MODULE_DEVICE_TABLE(i2c, mctp_i2c_id);
1108
1109 static const struct of_device_id mctp_i2c_of_match[] = {
1110 { .compatible = "mctp-i2c-controller" },
1111 {},
1112 };
1113 MODULE_DEVICE_TABLE(of, mctp_i2c_of_match);
1114
1115 static struct i2c_driver mctp_i2c_driver = {
1116 .driver = {
1117 .name = "mctp-i2c-interface",
1118 .of_match_table = mctp_i2c_of_match,
1119 },
1120 .probe = mctp_i2c_probe,
1121 .remove = mctp_i2c_remove,
1122 .id_table = mctp_i2c_id,
1123 };
1124
mctp_i2c_mod_init(void)1125 static __init int mctp_i2c_mod_init(void)
1126 {
1127 int rc;
1128
1129 pr_info("MCTP I2C interface driver\n");
1130 rc = i2c_add_driver(&mctp_i2c_driver);
1131 if (rc < 0)
1132 return rc;
1133 rc = bus_register_notifier(&i2c_bus_type, &mctp_i2c_notifier);
1134 if (rc < 0) {
1135 i2c_del_driver(&mctp_i2c_driver);
1136 return rc;
1137 }
1138 return 0;
1139 }
1140
mctp_i2c_mod_exit(void)1141 static __exit void mctp_i2c_mod_exit(void)
1142 {
1143 int rc;
1144
1145 rc = bus_unregister_notifier(&i2c_bus_type, &mctp_i2c_notifier);
1146 if (rc < 0)
1147 pr_warn("MCTP I2C could not unregister notifier, %d\n", rc);
1148 i2c_del_driver(&mctp_i2c_driver);
1149 }
1150
1151 module_init(mctp_i2c_mod_init);
1152 module_exit(mctp_i2c_mod_exit);
1153
1154 MODULE_DESCRIPTION("MCTP I2C device");
1155 MODULE_LICENSE("GPL v2");
1156 MODULE_AUTHOR("Matt Johnston <matt@codeconstruct.com.au>");
1157