Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Management Controller Transport Protocol (MCTP)
0004  * Implements DMTF specification
0005  * "DSP0237 Management Component Transport Protocol (MCTP) SMBus/I2C
0006  * Transport Binding"
0007  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0237_1.2.0.pdf
0008  *
0009  * A netdev is created for each I2C bus that handles MCTP. In the case of an I2C
0010  * mux topology a single I2C client is attached to the root of the mux topology,
0011  * shared between all mux I2C busses underneath. For non-mux cases an I2C client
0012  * is attached per netdev.
0013  *
0014  * mctp-i2c-controller.yml devicetree binding has further details.
0015  *
0016  * Copyright (c) 2022 Code Construct
0017  * Copyright (c) 2022 Google
0018  */
0019 
0020 #include <linux/module.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/i2c.h>
0023 #include <linux/i2c-mux.h>
0024 #include <linux/if_arp.h>
0025 #include <net/mctp.h>
0026 #include <net/mctpdevice.h>
0027 
0028 /* byte_count is limited to u8 */
0029 #define MCTP_I2C_MAXBLOCK 255
0030 /* One byte is taken by source_slave */
0031 #define MCTP_I2C_MAXMTU (MCTP_I2C_MAXBLOCK - 1)
0032 #define MCTP_I2C_MINMTU (64 + 4)
0033 /* Allow space for dest_address, command, byte_count, data, PEC */
0034 #define MCTP_I2C_BUFSZ (3 + MCTP_I2C_MAXBLOCK + 1)
0035 #define MCTP_I2C_MINLEN 8
0036 #define MCTP_I2C_COMMANDCODE 0x0f
0037 #define MCTP_I2C_TX_WORK_LEN 100
0038 /* Sufficient for 64kB at min mtu */
0039 #define MCTP_I2C_TX_QUEUE_LEN 1100
0040 
0041 #define MCTP_I2C_OF_PROP "mctp-controller"
0042 
0043 enum {
0044     MCTP_I2C_FLOW_STATE_NEW = 0,
0045     MCTP_I2C_FLOW_STATE_ACTIVE,
0046 };
0047 
0048 /* List of all struct mctp_i2c_client
0049  * Lock protects driver_clients and also prevents adding/removing adapters
0050  * during mctp_i2c_client probe/remove.
0051  */
0052 static DEFINE_MUTEX(driver_clients_lock);
0053 static LIST_HEAD(driver_clients);
0054 
0055 struct mctp_i2c_client;
0056 
0057 /* The netdev structure. One of these per I2C adapter. */
0058 struct mctp_i2c_dev {
0059     struct net_device *ndev;
0060     struct i2c_adapter *adapter;
0061     struct mctp_i2c_client *client;
0062     struct list_head list; /* For mctp_i2c_client.devs */
0063 
0064     size_t rx_pos;
0065     u8 rx_buffer[MCTP_I2C_BUFSZ];
0066     struct completion rx_done;
0067 
0068     struct task_struct *tx_thread;
0069     wait_queue_head_t tx_wq;
0070     struct sk_buff_head tx_queue;
0071     u8 tx_scratch[MCTP_I2C_BUFSZ];
0072 
0073     /* A fake entry in our tx queue to perform an unlock operation */
0074     struct sk_buff unlock_marker;
0075 
0076     /* Spinlock protects i2c_lock_count, release_count, allow_rx */
0077     spinlock_t lock;
0078     int i2c_lock_count;
0079     int release_count;
0080     /* Indicates that the netif is ready to receive incoming packets */
0081     bool allow_rx;
0082 
0083 };
0084 
0085 /* The i2c client structure. One per hardware i2c bus at the top of the
0086  * mux tree, shared by multiple netdevs
0087  */
0088 struct mctp_i2c_client {
0089     struct i2c_client *client;
0090     u8 lladdr;
0091 
0092     struct mctp_i2c_dev *sel;
0093     struct list_head devs;
0094     spinlock_t sel_lock; /* Protects sel and devs */
0095 
0096     struct list_head list; /* For driver_clients */
0097 };
0098 
0099 /* Header on the wire. */
0100 struct mctp_i2c_hdr {
0101     u8 dest_slave;
0102     u8 command;
0103     /* Count of bytes following byte_count, excluding PEC */
0104     u8 byte_count;
0105     u8 source_slave;
0106 };
0107 
0108 static int mctp_i2c_recv(struct mctp_i2c_dev *midev);
0109 static int mctp_i2c_slave_cb(struct i2c_client *client,
0110                  enum i2c_slave_event event, u8 *val);
0111 static void mctp_i2c_ndo_uninit(struct net_device *dev);
0112 static int mctp_i2c_ndo_open(struct net_device *dev);
0113 
0114 static struct i2c_adapter *mux_root_adapter(struct i2c_adapter *adap)
0115 {
0116 #if IS_ENABLED(CONFIG_I2C_MUX)
0117     return i2c_root_adapter(&adap->dev);
0118 #else
0119     /* In non-mux config all i2c adapters are root adapters */
0120     return adap;
0121 #endif
0122 }
0123 
0124 /* Creates a new i2c slave device attached to the root adapter.
0125  * Sets up the slave callback.
0126  * Must be called with a client on a root adapter.
0127  */
0128 static struct mctp_i2c_client *mctp_i2c_new_client(struct i2c_client *client)
0129 {
0130     struct mctp_i2c_client *mcli = NULL;
0131     struct i2c_adapter *root = NULL;
0132     int rc;
0133 
0134     if (client->flags & I2C_CLIENT_TEN) {
0135         dev_err(&client->dev, "failed, MCTP requires a 7-bit I2C address, addr=0x%x\n",
0136             client->addr);
0137         rc = -EINVAL;
0138         goto err;
0139     }
0140 
0141     root = mux_root_adapter(client->adapter);
0142     if (!root) {
0143         dev_err(&client->dev, "failed to find root adapter\n");
0144         rc = -ENOENT;
0145         goto err;
0146     }
0147     if (root != client->adapter) {
0148         dev_err(&client->dev,
0149             "A mctp-i2c-controller client cannot be placed on an I2C mux adapter.\n"
0150             " It should be placed on the mux tree root adapter\n"
0151             " then set mctp-controller property on adapters to attach\n");
0152         rc = -EINVAL;
0153         goto err;
0154     }
0155 
0156     mcli = kzalloc(sizeof(*mcli), GFP_KERNEL);
0157     if (!mcli) {
0158         rc = -ENOMEM;
0159         goto err;
0160     }
0161     spin_lock_init(&mcli->sel_lock);
0162     INIT_LIST_HEAD(&mcli->devs);
0163     INIT_LIST_HEAD(&mcli->list);
0164     mcli->lladdr = client->addr & 0xff;
0165     mcli->client = client;
0166     i2c_set_clientdata(client, mcli);
0167 
0168     rc = i2c_slave_register(mcli->client, mctp_i2c_slave_cb);
0169     if (rc < 0) {
0170         dev_err(&client->dev, "i2c register failed %d\n", rc);
0171         mcli->client = NULL;
0172         i2c_set_clientdata(client, NULL);
0173         goto err;
0174     }
0175 
0176     return mcli;
0177 err:
0178     if (mcli) {
0179         if (mcli->client)
0180             i2c_unregister_device(mcli->client);
0181         kfree(mcli);
0182     }
0183     return ERR_PTR(rc);
0184 }
0185 
0186 static void mctp_i2c_free_client(struct mctp_i2c_client *mcli)
0187 {
0188     int rc;
0189 
0190     WARN_ON(!mutex_is_locked(&driver_clients_lock));
0191     WARN_ON(!list_empty(&mcli->devs));
0192     WARN_ON(mcli->sel); /* sanity check, no locking */
0193 
0194     rc = i2c_slave_unregister(mcli->client);
0195     /* Leak if it fails, we can't propagate errors upwards */
0196     if (rc < 0)
0197         dev_err(&mcli->client->dev, "i2c unregister failed %d\n", rc);
0198     else
0199         kfree(mcli);
0200 }
0201 
0202 /* Switch the mctp i2c device to receive responses.
0203  * Call with sel_lock held
0204  */
0205 static void __mctp_i2c_device_select(struct mctp_i2c_client *mcli,
0206                      struct mctp_i2c_dev *midev)
0207 {
0208     assert_spin_locked(&mcli->sel_lock);
0209     if (midev)
0210         dev_hold(midev->ndev);
0211     if (mcli->sel)
0212         dev_put(mcli->sel->ndev);
0213     mcli->sel = midev;
0214 }
0215 
0216 /* Switch the mctp i2c device to receive responses */
0217 static void mctp_i2c_device_select(struct mctp_i2c_client *mcli,
0218                    struct mctp_i2c_dev *midev)
0219 {
0220     unsigned long flags;
0221 
0222     spin_lock_irqsave(&mcli->sel_lock, flags);
0223     __mctp_i2c_device_select(mcli, midev);
0224     spin_unlock_irqrestore(&mcli->sel_lock, flags);
0225 }
0226 
0227 static int mctp_i2c_slave_cb(struct i2c_client *client,
0228                  enum i2c_slave_event event, u8 *val)
0229 {
0230     struct mctp_i2c_client *mcli = i2c_get_clientdata(client);
0231     struct mctp_i2c_dev *midev = NULL;
0232     unsigned long flags;
0233     int rc = 0;
0234 
0235     spin_lock_irqsave(&mcli->sel_lock, flags);
0236     midev = mcli->sel;
0237     if (midev)
0238         dev_hold(midev->ndev);
0239     spin_unlock_irqrestore(&mcli->sel_lock, flags);
0240 
0241     if (!midev)
0242         return 0;
0243 
0244     switch (event) {
0245     case I2C_SLAVE_WRITE_RECEIVED:
0246         if (midev->rx_pos < MCTP_I2C_BUFSZ) {
0247             midev->rx_buffer[midev->rx_pos] = *val;
0248             midev->rx_pos++;
0249         } else {
0250             midev->ndev->stats.rx_over_errors++;
0251         }
0252 
0253         break;
0254     case I2C_SLAVE_WRITE_REQUESTED:
0255         /* dest_slave as first byte */
0256         midev->rx_buffer[0] = mcli->lladdr << 1;
0257         midev->rx_pos = 1;
0258         break;
0259     case I2C_SLAVE_STOP:
0260         rc = mctp_i2c_recv(midev);
0261         break;
0262     default:
0263         break;
0264     }
0265 
0266     dev_put(midev->ndev);
0267     return rc;
0268 }
0269 
0270 /* Processes incoming data that has been accumulated by the slave cb */
0271 static int mctp_i2c_recv(struct mctp_i2c_dev *midev)
0272 {
0273     struct net_device *ndev = midev->ndev;
0274     struct mctp_i2c_hdr *hdr;
0275     struct mctp_skb_cb *cb;
0276     struct sk_buff *skb;
0277     unsigned long flags;
0278     u8 pec, calc_pec;
0279     size_t recvlen;
0280     int status;
0281 
0282     /* + 1 for the PEC */
0283     if (midev->rx_pos < MCTP_I2C_MINLEN + 1) {
0284         ndev->stats.rx_length_errors++;
0285         return -EINVAL;
0286     }
0287     /* recvlen excludes PEC */
0288     recvlen = midev->rx_pos - 1;
0289 
0290     hdr = (void *)midev->rx_buffer;
0291     if (hdr->command != MCTP_I2C_COMMANDCODE) {
0292         ndev->stats.rx_dropped++;
0293         return -EINVAL;
0294     }
0295 
0296     if (hdr->byte_count + offsetof(struct mctp_i2c_hdr, source_slave) != recvlen) {
0297         ndev->stats.rx_length_errors++;
0298         return -EINVAL;
0299     }
0300 
0301     pec = midev->rx_buffer[midev->rx_pos - 1];
0302     calc_pec = i2c_smbus_pec(0, midev->rx_buffer, recvlen);
0303     if (pec != calc_pec) {
0304         ndev->stats.rx_crc_errors++;
0305         return -EINVAL;
0306     }
0307 
0308     skb = netdev_alloc_skb(ndev, recvlen);
0309     if (!skb) {
0310         ndev->stats.rx_dropped++;
0311         return -ENOMEM;
0312     }
0313 
0314     skb->protocol = htons(ETH_P_MCTP);
0315     skb_put_data(skb, midev->rx_buffer, recvlen);
0316     skb_reset_mac_header(skb);
0317     skb_pull(skb, sizeof(struct mctp_i2c_hdr));
0318     skb_reset_network_header(skb);
0319 
0320     cb = __mctp_cb(skb);
0321     cb->halen = 1;
0322     cb->haddr[0] = hdr->source_slave >> 1;
0323 
0324     /* We need to ensure that the netif is not used once netdev
0325      * unregister occurs
0326      */
0327     spin_lock_irqsave(&midev->lock, flags);
0328     if (midev->allow_rx) {
0329         reinit_completion(&midev->rx_done);
0330         spin_unlock_irqrestore(&midev->lock, flags);
0331 
0332         status = netif_rx(skb);
0333         complete(&midev->rx_done);
0334     } else {
0335         status = NET_RX_DROP;
0336         spin_unlock_irqrestore(&midev->lock, flags);
0337     }
0338 
0339     if (status == NET_RX_SUCCESS) {
0340         ndev->stats.rx_packets++;
0341         ndev->stats.rx_bytes += recvlen;
0342     } else {
0343         ndev->stats.rx_dropped++;
0344     }
0345     return 0;
0346 }
0347 
0348 enum mctp_i2c_flow_state {
0349     MCTP_I2C_TX_FLOW_INVALID,
0350     MCTP_I2C_TX_FLOW_NONE,
0351     MCTP_I2C_TX_FLOW_NEW,
0352     MCTP_I2C_TX_FLOW_EXISTING,
0353 };
0354 
0355 static enum mctp_i2c_flow_state
0356 mctp_i2c_get_tx_flow_state(struct mctp_i2c_dev *midev, struct sk_buff *skb)
0357 {
0358     enum mctp_i2c_flow_state state;
0359     struct mctp_sk_key *key;
0360     struct mctp_flow *flow;
0361     unsigned long flags;
0362 
0363     flow = skb_ext_find(skb, SKB_EXT_MCTP);
0364     if (!flow)
0365         return MCTP_I2C_TX_FLOW_NONE;
0366 
0367     key = flow->key;
0368     if (!key)
0369         return MCTP_I2C_TX_FLOW_NONE;
0370 
0371     spin_lock_irqsave(&key->lock, flags);
0372     /* If the key is present but invalid, we're unlikely to be able
0373      * to handle the flow at all; just drop now
0374      */
0375     if (!key->valid) {
0376         state = MCTP_I2C_TX_FLOW_INVALID;
0377 
0378     } else if (key->dev_flow_state == MCTP_I2C_FLOW_STATE_NEW) {
0379         key->dev_flow_state = MCTP_I2C_FLOW_STATE_ACTIVE;
0380         state = MCTP_I2C_TX_FLOW_NEW;
0381     } else {
0382         state = MCTP_I2C_TX_FLOW_EXISTING;
0383     }
0384 
0385     spin_unlock_irqrestore(&key->lock, flags);
0386 
0387     return state;
0388 }
0389 
0390 /* We're not contending with ourselves here; we only need to exclude other
0391  * i2c clients from using the bus. refcounts are simply to prevent
0392  * recursive locking.
0393  */
0394 static void mctp_i2c_lock_nest(struct mctp_i2c_dev *midev)
0395 {
0396     unsigned long flags;
0397     bool lock;
0398 
0399     spin_lock_irqsave(&midev->lock, flags);
0400     lock = midev->i2c_lock_count == 0;
0401     midev->i2c_lock_count++;
0402     spin_unlock_irqrestore(&midev->lock, flags);
0403 
0404     if (lock)
0405         i2c_lock_bus(midev->adapter, I2C_LOCK_SEGMENT);
0406 }
0407 
0408 static void mctp_i2c_unlock_nest(struct mctp_i2c_dev *midev)
0409 {
0410     unsigned long flags;
0411     bool unlock;
0412 
0413     spin_lock_irqsave(&midev->lock, flags);
0414     if (!WARN_ONCE(midev->i2c_lock_count == 0, "lock count underflow!"))
0415         midev->i2c_lock_count--;
0416     unlock = midev->i2c_lock_count == 0;
0417     spin_unlock_irqrestore(&midev->lock, flags);
0418 
0419     if (unlock)
0420         i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
0421 }
0422 
0423 /* Unlocks the bus if was previously locked, used for cleanup */
0424 static void mctp_i2c_unlock_reset(struct mctp_i2c_dev *midev)
0425 {
0426     unsigned long flags;
0427     bool unlock;
0428 
0429     spin_lock_irqsave(&midev->lock, flags);
0430     unlock = midev->i2c_lock_count > 0;
0431     midev->i2c_lock_count = 0;
0432     spin_unlock_irqrestore(&midev->lock, flags);
0433 
0434     if (unlock)
0435         i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
0436 }
0437 
0438 static void mctp_i2c_xmit(struct mctp_i2c_dev *midev, struct sk_buff *skb)
0439 {
0440     struct net_device_stats *stats = &midev->ndev->stats;
0441     enum mctp_i2c_flow_state fs;
0442     struct mctp_i2c_hdr *hdr;
0443     struct i2c_msg msg = {0};
0444     u8 *pecp;
0445     int rc;
0446 
0447     fs = mctp_i2c_get_tx_flow_state(midev, skb);
0448 
0449     hdr = (void *)skb_mac_header(skb);
0450     /* Sanity check that packet contents matches skb length,
0451      * and can't exceed MCTP_I2C_BUFSZ
0452      */
0453     if (skb->len != hdr->byte_count + 3) {
0454         dev_warn_ratelimited(&midev->adapter->dev,
0455                      "Bad tx length %d vs skb %u\n",
0456                      hdr->byte_count + 3, skb->len);
0457         return;
0458     }
0459 
0460     if (skb_tailroom(skb) >= 1) {
0461         /* Linear case with space, we can just append the PEC */
0462         skb_put(skb, 1);
0463     } else {
0464         /* Otherwise need to copy the buffer */
0465         skb_copy_bits(skb, 0, midev->tx_scratch, skb->len);
0466         hdr = (void *)midev->tx_scratch;
0467     }
0468 
0469     pecp = (void *)&hdr->source_slave + hdr->byte_count;
0470     *pecp = i2c_smbus_pec(0, (u8 *)hdr, hdr->byte_count + 3);
0471     msg.buf = (void *)&hdr->command;
0472     /* command, bytecount, data, pec */
0473     msg.len = 2 + hdr->byte_count + 1;
0474     msg.addr = hdr->dest_slave >> 1;
0475 
0476     switch (fs) {
0477     case MCTP_I2C_TX_FLOW_NONE:
0478         /* no flow: full lock & unlock */
0479         mctp_i2c_lock_nest(midev);
0480         mctp_i2c_device_select(midev->client, midev);
0481         rc = __i2c_transfer(midev->adapter, &msg, 1);
0482         mctp_i2c_unlock_nest(midev);
0483         break;
0484 
0485     case MCTP_I2C_TX_FLOW_NEW:
0486         /* new flow: lock, tx, but don't unlock; that will happen
0487          * on flow release
0488          */
0489         mctp_i2c_lock_nest(midev);
0490         mctp_i2c_device_select(midev->client, midev);
0491         fallthrough;
0492 
0493     case MCTP_I2C_TX_FLOW_EXISTING:
0494         /* existing flow: we already have the lock; just tx */
0495         rc = __i2c_transfer(midev->adapter, &msg, 1);
0496         break;
0497 
0498     case MCTP_I2C_TX_FLOW_INVALID:
0499         return;
0500     }
0501 
0502     if (rc < 0) {
0503         dev_warn_ratelimited(&midev->adapter->dev,
0504                      "__i2c_transfer failed %d\n", rc);
0505         stats->tx_errors++;
0506     } else {
0507         stats->tx_bytes += skb->len;
0508         stats->tx_packets++;
0509     }
0510 }
0511 
0512 static void mctp_i2c_flow_release(struct mctp_i2c_dev *midev)
0513 {
0514     unsigned long flags;
0515     bool unlock;
0516 
0517     spin_lock_irqsave(&midev->lock, flags);
0518     if (midev->release_count > midev->i2c_lock_count) {
0519         WARN_ONCE(1, "release count overflow");
0520         midev->release_count = midev->i2c_lock_count;
0521     }
0522 
0523     midev->i2c_lock_count -= midev->release_count;
0524     unlock = midev->i2c_lock_count == 0 && midev->release_count > 0;
0525     midev->release_count = 0;
0526     spin_unlock_irqrestore(&midev->lock, flags);
0527 
0528     if (unlock)
0529         i2c_unlock_bus(midev->adapter, I2C_LOCK_SEGMENT);
0530 }
0531 
0532 static int mctp_i2c_header_create(struct sk_buff *skb, struct net_device *dev,
0533                   unsigned short type, const void *daddr,
0534        const void *saddr, unsigned int len)
0535 {
0536     struct mctp_i2c_hdr *hdr;
0537     struct mctp_hdr *mhdr;
0538     u8 lldst, llsrc;
0539 
0540     if (len > MCTP_I2C_MAXMTU)
0541         return -EMSGSIZE;
0542 
0543     lldst = *((u8 *)daddr);
0544     llsrc = *((u8 *)saddr);
0545 
0546     skb_push(skb, sizeof(struct mctp_i2c_hdr));
0547     skb_reset_mac_header(skb);
0548     hdr = (void *)skb_mac_header(skb);
0549     mhdr = mctp_hdr(skb);
0550     hdr->dest_slave = (lldst << 1) & 0xff;
0551     hdr->command = MCTP_I2C_COMMANDCODE;
0552     hdr->byte_count = len + 1;
0553     hdr->source_slave = ((llsrc << 1) & 0xff) | 0x01;
0554     mhdr->ver = 0x01;
0555 
0556     return sizeof(struct mctp_i2c_hdr);
0557 }
0558 
0559 static int mctp_i2c_tx_thread(void *data)
0560 {
0561     struct mctp_i2c_dev *midev = data;
0562     struct sk_buff *skb;
0563     unsigned long flags;
0564 
0565     for (;;) {
0566         if (kthread_should_stop())
0567             break;
0568 
0569         spin_lock_irqsave(&midev->tx_queue.lock, flags);
0570         skb = __skb_dequeue(&midev->tx_queue);
0571         if (netif_queue_stopped(midev->ndev))
0572             netif_wake_queue(midev->ndev);
0573         spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
0574 
0575         if (skb == &midev->unlock_marker) {
0576             mctp_i2c_flow_release(midev);
0577 
0578         } else if (skb) {
0579             mctp_i2c_xmit(midev, skb);
0580             kfree_skb(skb);
0581 
0582         } else {
0583             wait_event_idle(midev->tx_wq,
0584                     !skb_queue_empty(&midev->tx_queue) ||
0585                    kthread_should_stop());
0586         }
0587     }
0588 
0589     return 0;
0590 }
0591 
0592 static netdev_tx_t mctp_i2c_start_xmit(struct sk_buff *skb,
0593                        struct net_device *dev)
0594 {
0595     struct mctp_i2c_dev *midev = netdev_priv(dev);
0596     unsigned long flags;
0597 
0598     spin_lock_irqsave(&midev->tx_queue.lock, flags);
0599     if (skb_queue_len(&midev->tx_queue) >= MCTP_I2C_TX_WORK_LEN) {
0600         netif_stop_queue(dev);
0601         spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
0602         netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
0603         return NETDEV_TX_BUSY;
0604     }
0605 
0606     __skb_queue_tail(&midev->tx_queue, skb);
0607     if (skb_queue_len(&midev->tx_queue) == MCTP_I2C_TX_WORK_LEN)
0608         netif_stop_queue(dev);
0609     spin_unlock_irqrestore(&midev->tx_queue.lock, flags);
0610 
0611     wake_up(&midev->tx_wq);
0612     return NETDEV_TX_OK;
0613 }
0614 
0615 static void mctp_i2c_release_flow(struct mctp_dev *mdev,
0616                   struct mctp_sk_key *key)
0617 
0618 {
0619     struct mctp_i2c_dev *midev = netdev_priv(mdev->dev);
0620     unsigned long flags;
0621 
0622     spin_lock_irqsave(&midev->lock, flags);
0623     midev->release_count++;
0624     spin_unlock_irqrestore(&midev->lock, flags);
0625 
0626     /* Ensure we have a release operation queued, through the fake
0627      * marker skb
0628      */
0629     spin_lock(&midev->tx_queue.lock);
0630     if (!midev->unlock_marker.next)
0631         __skb_queue_tail(&midev->tx_queue, &midev->unlock_marker);
0632     spin_unlock(&midev->tx_queue.lock);
0633 
0634     wake_up(&midev->tx_wq);
0635 }
0636 
0637 static const struct net_device_ops mctp_i2c_ops = {
0638     .ndo_start_xmit = mctp_i2c_start_xmit,
0639     .ndo_uninit = mctp_i2c_ndo_uninit,
0640     .ndo_open = mctp_i2c_ndo_open,
0641 };
0642 
0643 static const struct header_ops mctp_i2c_headops = {
0644     .create = mctp_i2c_header_create,
0645 };
0646 
0647 static const struct mctp_netdev_ops mctp_i2c_mctp_ops = {
0648     .release_flow = mctp_i2c_release_flow,
0649 };
0650 
0651 static void mctp_i2c_net_setup(struct net_device *dev)
0652 {
0653     dev->type = ARPHRD_MCTP;
0654 
0655     dev->mtu = MCTP_I2C_MAXMTU;
0656     dev->min_mtu = MCTP_I2C_MINMTU;
0657     dev->max_mtu = MCTP_I2C_MAXMTU;
0658     dev->tx_queue_len = MCTP_I2C_TX_QUEUE_LEN;
0659 
0660     dev->hard_header_len = sizeof(struct mctp_i2c_hdr);
0661     dev->addr_len = 1;
0662 
0663     dev->netdev_ops     = &mctp_i2c_ops;
0664     dev->header_ops     = &mctp_i2c_headops;
0665 }
0666 
0667 /* Populates the mctp_i2c_dev priv struct for a netdev.
0668  * Returns an error pointer on failure.
0669  */
0670 static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
0671                         struct mctp_i2c_client *mcli,
0672                         struct i2c_adapter *adap)
0673 {
0674     struct mctp_i2c_dev *midev = netdev_priv(dev);
0675     unsigned long flags;
0676 
0677     midev->tx_thread = kthread_create(mctp_i2c_tx_thread, midev,
0678                       "%s/tx", dev->name);
0679     if (IS_ERR(midev->tx_thread))
0680         return ERR_CAST(midev->tx_thread);
0681 
0682     midev->ndev = dev;
0683     get_device(&adap->dev);
0684     midev->adapter = adap;
0685     get_device(&mcli->client->dev);
0686     midev->client = mcli;
0687     INIT_LIST_HEAD(&midev->list);
0688     spin_lock_init(&midev->lock);
0689     midev->i2c_lock_count = 0;
0690     midev->release_count = 0;
0691     init_completion(&midev->rx_done);
0692     complete(&midev->rx_done);
0693     init_waitqueue_head(&midev->tx_wq);
0694     skb_queue_head_init(&midev->tx_queue);
0695 
0696     /* Add to the parent mcli */
0697     spin_lock_irqsave(&mcli->sel_lock, flags);
0698     list_add(&midev->list, &mcli->devs);
0699     /* Select a device by default */
0700     if (!mcli->sel)
0701         __mctp_i2c_device_select(mcli, midev);
0702     spin_unlock_irqrestore(&mcli->sel_lock, flags);
0703 
0704     /* Start the worker thread */
0705     wake_up_process(midev->tx_thread);
0706 
0707     return midev;
0708 }
0709 
0710 /* Counterpart of mctp_i2c_midev_init */
0711 static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)
0712 {
0713     struct mctp_i2c_client *mcli = midev->client;
0714     unsigned long flags;
0715 
0716     if (midev->tx_thread) {
0717         kthread_stop(midev->tx_thread);
0718         midev->tx_thread = NULL;
0719     }
0720 
0721     /* Unconditionally unlock on close */
0722     mctp_i2c_unlock_reset(midev);
0723 
0724     /* Remove the netdev from the parent i2c client. */
0725     spin_lock_irqsave(&mcli->sel_lock, flags);
0726     list_del(&midev->list);
0727     if (mcli->sel == midev) {
0728         struct mctp_i2c_dev *first;
0729 
0730         first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list);
0731         __mctp_i2c_device_select(mcli, first);
0732     }
0733     spin_unlock_irqrestore(&mcli->sel_lock, flags);
0734 
0735     skb_queue_purge(&midev->tx_queue);
0736     put_device(&midev->adapter->dev);
0737     put_device(&mcli->client->dev);
0738 }
0739 
0740 /* Stops, unregisters, and frees midev */
0741 static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)
0742 {
0743     unsigned long flags;
0744 
0745     /* Stop tx thread prior to unregister, it uses netif_() functions */
0746     kthread_stop(midev->tx_thread);
0747     midev->tx_thread = NULL;
0748 
0749     /* Prevent any new rx in mctp_i2c_recv(), let any pending work finish */
0750     spin_lock_irqsave(&midev->lock, flags);
0751     midev->allow_rx = false;
0752     spin_unlock_irqrestore(&midev->lock, flags);
0753     wait_for_completion(&midev->rx_done);
0754 
0755     mctp_unregister_netdev(midev->ndev);
0756     /* midev has been freed now by mctp_i2c_ndo_uninit callback */
0757 
0758     free_netdev(midev->ndev);
0759 }
0760 
0761 static void mctp_i2c_ndo_uninit(struct net_device *dev)
0762 {
0763     struct mctp_i2c_dev *midev = netdev_priv(dev);
0764 
0765     /* Perform cleanup here to ensure that mcli->sel isn't holding
0766      * a reference that would prevent unregister_netdevice()
0767      * from completing.
0768      */
0769     mctp_i2c_midev_free(midev);
0770 }
0771 
0772 static int mctp_i2c_ndo_open(struct net_device *dev)
0773 {
0774     struct mctp_i2c_dev *midev = netdev_priv(dev);
0775     unsigned long flags;
0776 
0777     /* i2c rx handler can only pass packets once the netdev is registered */
0778     spin_lock_irqsave(&midev->lock, flags);
0779     midev->allow_rx = true;
0780     spin_unlock_irqrestore(&midev->lock, flags);
0781 
0782     return 0;
0783 }
0784 
0785 static int mctp_i2c_add_netdev(struct mctp_i2c_client *mcli,
0786                    struct i2c_adapter *adap)
0787 {
0788     struct mctp_i2c_dev *midev = NULL;
0789     struct net_device *ndev = NULL;
0790     struct i2c_adapter *root;
0791     unsigned long flags;
0792     char namebuf[30];
0793     int rc;
0794 
0795     root = mux_root_adapter(adap);
0796     if (root != mcli->client->adapter) {
0797         dev_err(&mcli->client->dev,
0798             "I2C adapter %s is not a child bus of %s\n",
0799             mcli->client->adapter->name, root->name);
0800         return -EINVAL;
0801     }
0802 
0803     WARN_ON(!mutex_is_locked(&driver_clients_lock));
0804     snprintf(namebuf, sizeof(namebuf), "mctpi2c%d", adap->nr);
0805     ndev = alloc_netdev(sizeof(*midev), namebuf, NET_NAME_ENUM, mctp_i2c_net_setup);
0806     if (!ndev) {
0807         dev_err(&mcli->client->dev, "alloc netdev failed\n");
0808         rc = -ENOMEM;
0809         goto err;
0810     }
0811     dev_net_set(ndev, current->nsproxy->net_ns);
0812     SET_NETDEV_DEV(ndev, &adap->dev);
0813     dev_addr_set(ndev, &mcli->lladdr);
0814 
0815     midev = mctp_i2c_midev_init(ndev, mcli, adap);
0816     if (IS_ERR(midev)) {
0817         rc = PTR_ERR(midev);
0818         midev = NULL;
0819         goto err;
0820     }
0821 
0822     rc = mctp_register_netdev(ndev, &mctp_i2c_mctp_ops);
0823     if (rc < 0) {
0824         dev_err(&mcli->client->dev,
0825             "register netdev \"%s\" failed %d\n",
0826             ndev->name, rc);
0827         goto err;
0828     }
0829 
0830     spin_lock_irqsave(&midev->lock, flags);
0831     midev->allow_rx = false;
0832     spin_unlock_irqrestore(&midev->lock, flags);
0833 
0834     return 0;
0835 err:
0836     if (midev)
0837         mctp_i2c_midev_free(midev);
0838     if (ndev)
0839         free_netdev(ndev);
0840     return rc;
0841 }
0842 
0843 /* Removes any netdev for adap. mcli is the parent root i2c client */
0844 static void mctp_i2c_remove_netdev(struct mctp_i2c_client *mcli,
0845                    struct i2c_adapter *adap)
0846 {
0847     struct mctp_i2c_dev *midev = NULL, *m = NULL;
0848     unsigned long flags;
0849 
0850     WARN_ON(!mutex_is_locked(&driver_clients_lock));
0851     spin_lock_irqsave(&mcli->sel_lock, flags);
0852     /* List size is limited by number of MCTP netdevs on a single hardware bus */
0853     list_for_each_entry(m, &mcli->devs, list)
0854         if (m->adapter == adap) {
0855             midev = m;
0856             break;
0857         }
0858     spin_unlock_irqrestore(&mcli->sel_lock, flags);
0859 
0860     if (midev)
0861         mctp_i2c_unregister(midev);
0862 }
0863 
0864 /* Determines whether a device is an i2c adapter.
0865  * Optionally returns the root i2c_adapter
0866  */
0867 static struct i2c_adapter *mctp_i2c_get_adapter(struct device *dev,
0868                         struct i2c_adapter **ret_root)
0869 {
0870     struct i2c_adapter *root, *adap;
0871 
0872     if (dev->type != &i2c_adapter_type)
0873         return NULL;
0874     adap = to_i2c_adapter(dev);
0875     root = mux_root_adapter(adap);
0876     WARN_ONCE(!root, "MCTP I2C failed to find root adapter for %s\n",
0877           dev_name(dev));
0878     if (!root)
0879         return NULL;
0880     if (ret_root)
0881         *ret_root = root;
0882     return adap;
0883 }
0884 
0885 /* Determines whether a device is an i2c adapter with the "mctp-controller"
0886  * devicetree property set. If adap is not an OF node, returns match_no_of
0887  */
0888 static bool mctp_i2c_adapter_match(struct i2c_adapter *adap, bool match_no_of)
0889 {
0890     if (!adap->dev.of_node)
0891         return match_no_of;
0892     return of_property_read_bool(adap->dev.of_node, MCTP_I2C_OF_PROP);
0893 }
0894 
0895 /* Called for each existing i2c device (adapter or client) when a
0896  * new mctp-i2c client is probed.
0897  */
0898 static int mctp_i2c_client_try_attach(struct device *dev, void *data)
0899 {
0900     struct i2c_adapter *adap = NULL, *root = NULL;
0901     struct mctp_i2c_client *mcli = data;
0902 
0903     adap = mctp_i2c_get_adapter(dev, &root);
0904     if (!adap)
0905         return 0;
0906     if (mcli->client->adapter != root)
0907         return 0;
0908     /* Must either have mctp-controller property on the adapter, or
0909      * be a root adapter if it's non-devicetree
0910      */
0911     if (!mctp_i2c_adapter_match(adap, adap == root))
0912         return 0;
0913 
0914     return mctp_i2c_add_netdev(mcli, adap);
0915 }
0916 
0917 static void mctp_i2c_notify_add(struct device *dev)
0918 {
0919     struct mctp_i2c_client *mcli = NULL, *m = NULL;
0920     struct i2c_adapter *root = NULL, *adap = NULL;
0921     int rc;
0922 
0923     adap = mctp_i2c_get_adapter(dev, &root);
0924     if (!adap)
0925         return;
0926     /* Check for mctp-controller property on the adapter */
0927     if (!mctp_i2c_adapter_match(adap, false))
0928         return;
0929 
0930     /* Find an existing mcli for adap's root */
0931     mutex_lock(&driver_clients_lock);
0932     list_for_each_entry(m, &driver_clients, list) {
0933         if (m->client->adapter == root) {
0934             mcli = m;
0935             break;
0936         }
0937     }
0938 
0939     if (mcli) {
0940         rc = mctp_i2c_add_netdev(mcli, adap);
0941         if (rc < 0)
0942             dev_warn(dev, "Failed adding mctp-i2c net device\n");
0943     }
0944     mutex_unlock(&driver_clients_lock);
0945 }
0946 
0947 static void mctp_i2c_notify_del(struct device *dev)
0948 {
0949     struct i2c_adapter *root = NULL, *adap = NULL;
0950     struct mctp_i2c_client *mcli = NULL;
0951 
0952     adap = mctp_i2c_get_adapter(dev, &root);
0953     if (!adap)
0954         return;
0955 
0956     mutex_lock(&driver_clients_lock);
0957     list_for_each_entry(mcli, &driver_clients, list) {
0958         if (mcli->client->adapter == root) {
0959             mctp_i2c_remove_netdev(mcli, adap);
0960             break;
0961         }
0962     }
0963     mutex_unlock(&driver_clients_lock);
0964 }
0965 
0966 static int mctp_i2c_probe(struct i2c_client *client)
0967 {
0968     struct mctp_i2c_client *mcli = NULL;
0969     int rc;
0970 
0971     mutex_lock(&driver_clients_lock);
0972     mcli = mctp_i2c_new_client(client);
0973     if (IS_ERR(mcli)) {
0974         rc = PTR_ERR(mcli);
0975         mcli = NULL;
0976         goto out;
0977     } else {
0978         list_add(&mcli->list, &driver_clients);
0979     }
0980 
0981     /* Add a netdev for adapters that have a 'mctp-controller' property */
0982     i2c_for_each_dev(mcli, mctp_i2c_client_try_attach);
0983     rc = 0;
0984 out:
0985     mutex_unlock(&driver_clients_lock);
0986     return rc;
0987 }
0988 
0989 static int mctp_i2c_remove(struct i2c_client *client)
0990 {
0991     struct mctp_i2c_client *mcli = i2c_get_clientdata(client);
0992     struct mctp_i2c_dev *midev = NULL, *tmp = NULL;
0993 
0994     mutex_lock(&driver_clients_lock);
0995     list_del(&mcli->list);
0996     /* Remove all child adapter netdevs */
0997     list_for_each_entry_safe(midev, tmp, &mcli->devs, list)
0998         mctp_i2c_unregister(midev);
0999 
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>");