mctp-i2c.c 27 KB

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