dev.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*
  2. * Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  3. * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/can.h>
  25. #include <linux/can/dev.h>
  26. #include <linux/can/skb.h>
  27. #include <linux/can/netlink.h>
  28. #include <linux/can/led.h>
  29. #include <linux/of.h>
  30. #include <net/rtnetlink.h>
  31. #define MOD_DESC "CAN device driver interface"
  32. MODULE_DESCRIPTION(MOD_DESC);
  33. MODULE_LICENSE("GPL v2");
  34. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  35. /* CAN DLC to real data length conversion helpers */
  36. static const u8 dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
  37. 8, 12, 16, 20, 24, 32, 48, 64};
  38. /* get data length from can_dlc with sanitized can_dlc */
  39. u8 can_dlc2len(u8 can_dlc)
  40. {
  41. return dlc2len[can_dlc & 0x0F];
  42. }
  43. EXPORT_SYMBOL_GPL(can_dlc2len);
  44. static const u8 len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
  45. 9, 9, 9, 9, /* 9 - 12 */
  46. 10, 10, 10, 10, /* 13 - 16 */
  47. 11, 11, 11, 11, /* 17 - 20 */
  48. 12, 12, 12, 12, /* 21 - 24 */
  49. 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
  50. 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
  51. 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
  52. 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
  53. 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
  54. /* map the sanitized data length to an appropriate data length code */
  55. u8 can_len2dlc(u8 len)
  56. {
  57. if (unlikely(len > 64))
  58. return 0xF;
  59. return len2dlc[len];
  60. }
  61. EXPORT_SYMBOL_GPL(can_len2dlc);
  62. #ifdef CONFIG_CAN_CALC_BITTIMING
  63. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  64. #define CAN_CALC_SYNC_SEG 1
  65. /*
  66. * Bit-timing calculation derived from:
  67. *
  68. * Code based on LinCAN sources and H8S2638 project
  69. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  70. * Copyright 2005 Stanislav Marek
  71. * email: pisa@cmp.felk.cvut.cz
  72. *
  73. * Calculates proper bit-timing parameters for a specified bit-rate
  74. * and sample-point, which can then be used to set the bit-timing
  75. * registers of the CAN controller. You can find more information
  76. * in the header file linux/can/netlink.h.
  77. */
  78. static int can_update_sample_point(const struct can_bittiming_const *btc,
  79. unsigned int sample_point_nominal, unsigned int tseg,
  80. unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
  81. unsigned int *sample_point_error_ptr)
  82. {
  83. unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
  84. unsigned int sample_point, best_sample_point = 0;
  85. unsigned int tseg1, tseg2;
  86. int i;
  87. for (i = 0; i <= 1; i++) {
  88. tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
  89. tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
  90. tseg1 = tseg - tseg2;
  91. if (tseg1 > btc->tseg1_max) {
  92. tseg1 = btc->tseg1_max;
  93. tseg2 = tseg - tseg1;
  94. }
  95. sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
  96. sample_point_error = abs(sample_point_nominal - sample_point);
  97. if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) {
  98. best_sample_point = sample_point;
  99. best_sample_point_error = sample_point_error;
  100. *tseg1_ptr = tseg1;
  101. *tseg2_ptr = tseg2;
  102. }
  103. }
  104. if (sample_point_error_ptr)
  105. *sample_point_error_ptr = best_sample_point_error;
  106. return best_sample_point;
  107. }
  108. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  109. const struct can_bittiming_const *btc)
  110. {
  111. struct can_priv *priv = netdev_priv(dev);
  112. unsigned int bitrate; /* current bitrate */
  113. unsigned int bitrate_error; /* difference between current and nominal value */
  114. unsigned int best_bitrate_error = UINT_MAX;
  115. unsigned int sample_point_error; /* difference between current and nominal value */
  116. unsigned int best_sample_point_error = UINT_MAX;
  117. unsigned int sample_point_nominal; /* nominal sample point */
  118. unsigned int best_tseg = 0; /* current best value for tseg */
  119. unsigned int best_brp = 0; /* current best value for brp */
  120. unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
  121. u64 v64;
  122. /* Use CiA recommended sample points */
  123. if (bt->sample_point) {
  124. sample_point_nominal = bt->sample_point;
  125. } else {
  126. if (bt->bitrate > 800000)
  127. sample_point_nominal = 750;
  128. else if (bt->bitrate > 500000)
  129. sample_point_nominal = 800;
  130. else
  131. sample_point_nominal = 875;
  132. }
  133. /* tseg even = round down, odd = round up */
  134. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  135. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  136. tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
  137. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  138. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  139. /* choose brp step which is possible in system */
  140. brp = (brp / btc->brp_inc) * btc->brp_inc;
  141. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  142. continue;
  143. bitrate = priv->clock.freq / (brp * tsegall);
  144. bitrate_error = abs(bt->bitrate - bitrate);
  145. /* tseg brp biterror */
  146. if (bitrate_error > best_bitrate_error)
  147. continue;
  148. /* reset sample point error if we have a better bitrate */
  149. if (bitrate_error < best_bitrate_error)
  150. best_sample_point_error = UINT_MAX;
  151. can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error);
  152. if (sample_point_error > best_sample_point_error)
  153. continue;
  154. best_sample_point_error = sample_point_error;
  155. best_bitrate_error = bitrate_error;
  156. best_tseg = tseg / 2;
  157. best_brp = brp;
  158. if (bitrate_error == 0 && sample_point_error == 0)
  159. break;
  160. }
  161. if (best_bitrate_error) {
  162. /* Error in one-tenth of a percent */
  163. v64 = (u64)best_bitrate_error * 1000;
  164. do_div(v64, bt->bitrate);
  165. bitrate_error = (u32)v64;
  166. if (bitrate_error > CAN_CALC_MAX_ERROR) {
  167. netdev_err(dev,
  168. "bitrate error %d.%d%% too high\n",
  169. bitrate_error / 10, bitrate_error % 10);
  170. return -EDOM;
  171. }
  172. netdev_warn(dev, "bitrate error %d.%d%%\n",
  173. bitrate_error / 10, bitrate_error % 10);
  174. }
  175. /* real sample point */
  176. bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg,
  177. &tseg1, &tseg2, NULL);
  178. v64 = (u64)best_brp * 1000 * 1000 * 1000;
  179. do_div(v64, priv->clock.freq);
  180. bt->tq = (u32)v64;
  181. bt->prop_seg = tseg1 / 2;
  182. bt->phase_seg1 = tseg1 - bt->prop_seg;
  183. bt->phase_seg2 = tseg2;
  184. /* check for sjw user settings */
  185. if (!bt->sjw || !btc->sjw_max) {
  186. bt->sjw = 1;
  187. } else {
  188. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  189. if (bt->sjw > btc->sjw_max)
  190. bt->sjw = btc->sjw_max;
  191. /* bt->sjw must not be higher than tseg2 */
  192. if (tseg2 < bt->sjw)
  193. bt->sjw = tseg2;
  194. }
  195. bt->brp = best_brp;
  196. /* real bitrate */
  197. bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
  198. return 0;
  199. }
  200. #else /* !CONFIG_CAN_CALC_BITTIMING */
  201. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  202. const struct can_bittiming_const *btc)
  203. {
  204. netdev_err(dev, "bit-timing calculation not available\n");
  205. return -EINVAL;
  206. }
  207. #endif /* CONFIG_CAN_CALC_BITTIMING */
  208. /*
  209. * Checks the validity of the specified bit-timing parameters prop_seg,
  210. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  211. * prescaler value brp. You can find more information in the header
  212. * file linux/can/netlink.h.
  213. */
  214. static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
  215. const struct can_bittiming_const *btc)
  216. {
  217. struct can_priv *priv = netdev_priv(dev);
  218. int tseg1, alltseg;
  219. u64 brp64;
  220. tseg1 = bt->prop_seg + bt->phase_seg1;
  221. if (!bt->sjw)
  222. bt->sjw = 1;
  223. if (bt->sjw > btc->sjw_max ||
  224. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  225. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  226. return -ERANGE;
  227. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  228. if (btc->brp_inc > 1)
  229. do_div(brp64, btc->brp_inc);
  230. brp64 += 500000000UL - 1;
  231. do_div(brp64, 1000000000UL); /* the practicable BRP */
  232. if (btc->brp_inc > 1)
  233. brp64 *= btc->brp_inc;
  234. bt->brp = (u32)brp64;
  235. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  236. return -EINVAL;
  237. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  238. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  239. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  240. return 0;
  241. }
  242. /* Checks the validity of predefined bitrate settings */
  243. static int can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
  244. const u32 *bitrate_const,
  245. const unsigned int bitrate_const_cnt)
  246. {
  247. struct can_priv *priv = netdev_priv(dev);
  248. unsigned int i;
  249. for (i = 0; i < bitrate_const_cnt; i++) {
  250. if (bt->bitrate == bitrate_const[i])
  251. break;
  252. }
  253. if (i >= priv->bitrate_const_cnt)
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  258. const struct can_bittiming_const *btc,
  259. const u32 *bitrate_const,
  260. const unsigned int bitrate_const_cnt)
  261. {
  262. int err;
  263. /*
  264. * Depending on the given can_bittiming parameter structure the CAN
  265. * timing parameters are calculated based on the provided bitrate OR
  266. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  267. * provided directly which are then checked and fixed up.
  268. */
  269. if (!bt->tq && bt->bitrate && btc)
  270. err = can_calc_bittiming(dev, bt, btc);
  271. else if (bt->tq && !bt->bitrate && btc)
  272. err = can_fixup_bittiming(dev, bt, btc);
  273. else if (!bt->tq && bt->bitrate && bitrate_const)
  274. err = can_validate_bitrate(dev, bt, bitrate_const,
  275. bitrate_const_cnt);
  276. else
  277. err = -EINVAL;
  278. return err;
  279. }
  280. static void can_update_state_error_stats(struct net_device *dev,
  281. enum can_state new_state)
  282. {
  283. struct can_priv *priv = netdev_priv(dev);
  284. if (new_state <= priv->state)
  285. return;
  286. switch (new_state) {
  287. case CAN_STATE_ERROR_WARNING:
  288. priv->can_stats.error_warning++;
  289. break;
  290. case CAN_STATE_ERROR_PASSIVE:
  291. priv->can_stats.error_passive++;
  292. break;
  293. case CAN_STATE_BUS_OFF:
  294. priv->can_stats.bus_off++;
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  301. {
  302. switch (state) {
  303. case CAN_STATE_ERROR_ACTIVE:
  304. return CAN_ERR_CRTL_ACTIVE;
  305. case CAN_STATE_ERROR_WARNING:
  306. return CAN_ERR_CRTL_TX_WARNING;
  307. case CAN_STATE_ERROR_PASSIVE:
  308. return CAN_ERR_CRTL_TX_PASSIVE;
  309. default:
  310. return 0;
  311. }
  312. }
  313. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  314. {
  315. switch (state) {
  316. case CAN_STATE_ERROR_ACTIVE:
  317. return CAN_ERR_CRTL_ACTIVE;
  318. case CAN_STATE_ERROR_WARNING:
  319. return CAN_ERR_CRTL_RX_WARNING;
  320. case CAN_STATE_ERROR_PASSIVE:
  321. return CAN_ERR_CRTL_RX_PASSIVE;
  322. default:
  323. return 0;
  324. }
  325. }
  326. void can_change_state(struct net_device *dev, struct can_frame *cf,
  327. enum can_state tx_state, enum can_state rx_state)
  328. {
  329. struct can_priv *priv = netdev_priv(dev);
  330. enum can_state new_state = max(tx_state, rx_state);
  331. if (unlikely(new_state == priv->state)) {
  332. netdev_warn(dev, "%s: oops, state did not change", __func__);
  333. return;
  334. }
  335. netdev_dbg(dev, "New error state: %d\n", new_state);
  336. can_update_state_error_stats(dev, new_state);
  337. priv->state = new_state;
  338. if (!cf)
  339. return;
  340. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  341. cf->can_id |= CAN_ERR_BUSOFF;
  342. return;
  343. }
  344. cf->can_id |= CAN_ERR_CRTL;
  345. cf->data[1] |= tx_state >= rx_state ?
  346. can_tx_state_to_frame(dev, tx_state) : 0;
  347. cf->data[1] |= tx_state <= rx_state ?
  348. can_rx_state_to_frame(dev, rx_state) : 0;
  349. }
  350. EXPORT_SYMBOL_GPL(can_change_state);
  351. /*
  352. * Local echo of CAN messages
  353. *
  354. * CAN network devices *should* support a local echo functionality
  355. * (see Documentation/networking/can.rst). To test the handling of CAN
  356. * interfaces that do not support the local echo both driver types are
  357. * implemented. In the case that the driver does not support the echo
  358. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  359. * to perform the echo as a fallback solution.
  360. */
  361. static void can_flush_echo_skb(struct net_device *dev)
  362. {
  363. struct can_priv *priv = netdev_priv(dev);
  364. struct net_device_stats *stats = &dev->stats;
  365. int i;
  366. for (i = 0; i < priv->echo_skb_max; i++) {
  367. if (priv->echo_skb[i]) {
  368. kfree_skb(priv->echo_skb[i]);
  369. priv->echo_skb[i] = NULL;
  370. stats->tx_dropped++;
  371. stats->tx_aborted_errors++;
  372. }
  373. }
  374. }
  375. /*
  376. * Put the skb on the stack to be looped backed locally lateron
  377. *
  378. * The function is typically called in the start_xmit function
  379. * of the device driver. The driver must protect access to
  380. * priv->echo_skb, if necessary.
  381. */
  382. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  383. unsigned int idx)
  384. {
  385. struct can_priv *priv = netdev_priv(dev);
  386. BUG_ON(idx >= priv->echo_skb_max);
  387. /* check flag whether this packet has to be looped back */
  388. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  389. (skb->protocol != htons(ETH_P_CAN) &&
  390. skb->protocol != htons(ETH_P_CANFD))) {
  391. kfree_skb(skb);
  392. return;
  393. }
  394. if (!priv->echo_skb[idx]) {
  395. skb = can_create_echo_skb(skb);
  396. if (!skb)
  397. return;
  398. /* make settings for echo to reduce code in irq context */
  399. skb->pkt_type = PACKET_BROADCAST;
  400. skb->ip_summed = CHECKSUM_UNNECESSARY;
  401. skb->dev = dev;
  402. /* save this skb for tx interrupt echo handling */
  403. priv->echo_skb[idx] = skb;
  404. } else {
  405. /* locking problem with netif_stop_queue() ?? */
  406. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  407. kfree_skb(skb);
  408. }
  409. }
  410. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  411. struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
  412. {
  413. struct can_priv *priv = netdev_priv(dev);
  414. if (idx >= priv->echo_skb_max) {
  415. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  416. __func__, idx, priv->echo_skb_max);
  417. return NULL;
  418. }
  419. if (priv->echo_skb[idx]) {
  420. /* Using "struct canfd_frame::len" for the frame
  421. * length is supported on both CAN and CANFD frames.
  422. */
  423. struct sk_buff *skb = priv->echo_skb[idx];
  424. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  425. /* get the real payload length for netdev statistics */
  426. if (cf->can_id & CAN_RTR_FLAG)
  427. *len_ptr = 0;
  428. else
  429. *len_ptr = cf->len;
  430. priv->echo_skb[idx] = NULL;
  431. return skb;
  432. }
  433. return NULL;
  434. }
  435. /*
  436. * Get the skb from the stack and loop it back locally
  437. *
  438. * The function is typically called when the TX done interrupt
  439. * is handled in the device driver. The driver must protect
  440. * access to priv->echo_skb, if necessary.
  441. */
  442. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  443. {
  444. struct sk_buff *skb;
  445. u8 len;
  446. skb = __can_get_echo_skb(dev, idx, &len);
  447. if (!skb)
  448. return 0;
  449. skb_get(skb);
  450. if (netif_rx(skb) == NET_RX_SUCCESS)
  451. dev_consume_skb_any(skb);
  452. else
  453. dev_kfree_skb_any(skb);
  454. return len;
  455. }
  456. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  457. /*
  458. * Remove the skb from the stack and free it.
  459. *
  460. * The function is typically called when TX failed.
  461. */
  462. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  463. {
  464. struct can_priv *priv = netdev_priv(dev);
  465. BUG_ON(idx >= priv->echo_skb_max);
  466. if (priv->echo_skb[idx]) {
  467. dev_kfree_skb_any(priv->echo_skb[idx]);
  468. priv->echo_skb[idx] = NULL;
  469. }
  470. }
  471. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  472. /*
  473. * CAN device restart for bus-off recovery
  474. */
  475. static void can_restart(struct net_device *dev)
  476. {
  477. struct can_priv *priv = netdev_priv(dev);
  478. struct net_device_stats *stats = &dev->stats;
  479. struct sk_buff *skb;
  480. struct can_frame *cf;
  481. int err;
  482. BUG_ON(netif_carrier_ok(dev));
  483. /*
  484. * No synchronization needed because the device is bus-off and
  485. * no messages can come in or go out.
  486. */
  487. can_flush_echo_skb(dev);
  488. /* send restart message upstream */
  489. skb = alloc_can_err_skb(dev, &cf);
  490. if (skb == NULL) {
  491. err = -ENOMEM;
  492. goto restart;
  493. }
  494. cf->can_id |= CAN_ERR_RESTARTED;
  495. stats->rx_packets++;
  496. stats->rx_bytes += cf->can_dlc;
  497. netif_rx_ni(skb);
  498. restart:
  499. netdev_dbg(dev, "restarted\n");
  500. priv->can_stats.restarts++;
  501. /* Now restart the device */
  502. err = priv->do_set_mode(dev, CAN_MODE_START);
  503. netif_carrier_on(dev);
  504. if (err)
  505. netdev_err(dev, "Error %d during restart", err);
  506. }
  507. static void can_restart_work(struct work_struct *work)
  508. {
  509. struct delayed_work *dwork = to_delayed_work(work);
  510. struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
  511. can_restart(priv->dev);
  512. }
  513. int can_restart_now(struct net_device *dev)
  514. {
  515. struct can_priv *priv = netdev_priv(dev);
  516. /*
  517. * A manual restart is only permitted if automatic restart is
  518. * disabled and the device is in the bus-off state
  519. */
  520. if (priv->restart_ms)
  521. return -EINVAL;
  522. if (priv->state != CAN_STATE_BUS_OFF)
  523. return -EBUSY;
  524. cancel_delayed_work_sync(&priv->restart_work);
  525. can_restart(dev);
  526. return 0;
  527. }
  528. /*
  529. * CAN bus-off
  530. *
  531. * This functions should be called when the device goes bus-off to
  532. * tell the netif layer that no more packets can be sent or received.
  533. * If enabled, a timer is started to trigger bus-off recovery.
  534. */
  535. void can_bus_off(struct net_device *dev)
  536. {
  537. struct can_priv *priv = netdev_priv(dev);
  538. netdev_info(dev, "bus-off\n");
  539. netif_carrier_off(dev);
  540. if (priv->restart_ms)
  541. schedule_delayed_work(&priv->restart_work,
  542. msecs_to_jiffies(priv->restart_ms));
  543. }
  544. EXPORT_SYMBOL_GPL(can_bus_off);
  545. static void can_setup(struct net_device *dev)
  546. {
  547. dev->type = ARPHRD_CAN;
  548. dev->mtu = CAN_MTU;
  549. dev->hard_header_len = 0;
  550. dev->addr_len = 0;
  551. dev->tx_queue_len = 10;
  552. /* New-style flags. */
  553. dev->flags = IFF_NOARP;
  554. dev->features = NETIF_F_HW_CSUM;
  555. }
  556. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  557. {
  558. struct sk_buff *skb;
  559. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  560. sizeof(struct can_frame));
  561. if (unlikely(!skb))
  562. return NULL;
  563. skb->protocol = htons(ETH_P_CAN);
  564. skb->pkt_type = PACKET_BROADCAST;
  565. skb->ip_summed = CHECKSUM_UNNECESSARY;
  566. skb_reset_mac_header(skb);
  567. skb_reset_network_header(skb);
  568. skb_reset_transport_header(skb);
  569. can_skb_reserve(skb);
  570. can_skb_prv(skb)->ifindex = dev->ifindex;
  571. can_skb_prv(skb)->skbcnt = 0;
  572. *cf = skb_put_zero(skb, sizeof(struct can_frame));
  573. return skb;
  574. }
  575. EXPORT_SYMBOL_GPL(alloc_can_skb);
  576. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  577. struct canfd_frame **cfd)
  578. {
  579. struct sk_buff *skb;
  580. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  581. sizeof(struct canfd_frame));
  582. if (unlikely(!skb))
  583. return NULL;
  584. skb->protocol = htons(ETH_P_CANFD);
  585. skb->pkt_type = PACKET_BROADCAST;
  586. skb->ip_summed = CHECKSUM_UNNECESSARY;
  587. skb_reset_mac_header(skb);
  588. skb_reset_network_header(skb);
  589. skb_reset_transport_header(skb);
  590. can_skb_reserve(skb);
  591. can_skb_prv(skb)->ifindex = dev->ifindex;
  592. can_skb_prv(skb)->skbcnt = 0;
  593. *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
  594. return skb;
  595. }
  596. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  597. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  598. {
  599. struct sk_buff *skb;
  600. skb = alloc_can_skb(dev, cf);
  601. if (unlikely(!skb))
  602. return NULL;
  603. (*cf)->can_id = CAN_ERR_FLAG;
  604. (*cf)->can_dlc = CAN_ERR_DLC;
  605. return skb;
  606. }
  607. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  608. /*
  609. * Allocate and setup space for the CAN network device
  610. */
  611. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  612. unsigned int txqs, unsigned int rxqs)
  613. {
  614. struct net_device *dev;
  615. struct can_priv *priv;
  616. int size;
  617. if (echo_skb_max)
  618. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  619. echo_skb_max * sizeof(struct sk_buff *);
  620. else
  621. size = sizeof_priv;
  622. dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
  623. txqs, rxqs);
  624. if (!dev)
  625. return NULL;
  626. priv = netdev_priv(dev);
  627. priv->dev = dev;
  628. if (echo_skb_max) {
  629. priv->echo_skb_max = echo_skb_max;
  630. priv->echo_skb = (void *)priv +
  631. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  632. }
  633. priv->state = CAN_STATE_STOPPED;
  634. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  635. return dev;
  636. }
  637. EXPORT_SYMBOL_GPL(alloc_candev_mqs);
  638. /*
  639. * Free space of the CAN network device
  640. */
  641. void free_candev(struct net_device *dev)
  642. {
  643. free_netdev(dev);
  644. }
  645. EXPORT_SYMBOL_GPL(free_candev);
  646. /*
  647. * changing MTU and control mode for CAN/CANFD devices
  648. */
  649. int can_change_mtu(struct net_device *dev, int new_mtu)
  650. {
  651. struct can_priv *priv = netdev_priv(dev);
  652. /* Do not allow changing the MTU while running */
  653. if (dev->flags & IFF_UP)
  654. return -EBUSY;
  655. /* allow change of MTU according to the CANFD ability of the device */
  656. switch (new_mtu) {
  657. case CAN_MTU:
  658. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  659. if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
  660. return -EINVAL;
  661. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  662. break;
  663. case CANFD_MTU:
  664. /* check for potential CANFD ability */
  665. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  666. !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
  667. return -EINVAL;
  668. priv->ctrlmode |= CAN_CTRLMODE_FD;
  669. break;
  670. default:
  671. return -EINVAL;
  672. }
  673. dev->mtu = new_mtu;
  674. return 0;
  675. }
  676. EXPORT_SYMBOL_GPL(can_change_mtu);
  677. /*
  678. * Common open function when the device gets opened.
  679. *
  680. * This function should be called in the open function of the device
  681. * driver.
  682. */
  683. int open_candev(struct net_device *dev)
  684. {
  685. struct can_priv *priv = netdev_priv(dev);
  686. if (!priv->bittiming.bitrate) {
  687. netdev_err(dev, "bit-timing not yet defined\n");
  688. return -EINVAL;
  689. }
  690. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  691. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  692. (!priv->data_bittiming.bitrate ||
  693. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  694. netdev_err(dev, "incorrect/missing data bit-timing\n");
  695. return -EINVAL;
  696. }
  697. /* Switch carrier on if device was stopped while in bus-off state */
  698. if (!netif_carrier_ok(dev))
  699. netif_carrier_on(dev);
  700. return 0;
  701. }
  702. EXPORT_SYMBOL_GPL(open_candev);
  703. #ifdef CONFIG_OF
  704. /* Common function that can be used to understand the limitation of
  705. * a transceiver when it provides no means to determine these limitations
  706. * at runtime.
  707. */
  708. void of_can_transceiver(struct net_device *dev)
  709. {
  710. struct device_node *dn;
  711. struct can_priv *priv = netdev_priv(dev);
  712. struct device_node *np = dev->dev.parent->of_node;
  713. int ret;
  714. dn = of_get_child_by_name(np, "can-transceiver");
  715. if (!dn)
  716. return;
  717. ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
  718. of_node_put(dn);
  719. if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
  720. netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
  721. }
  722. EXPORT_SYMBOL_GPL(of_can_transceiver);
  723. #endif
  724. /*
  725. * Common close function for cleanup before the device gets closed.
  726. *
  727. * This function should be called in the close function of the device
  728. * driver.
  729. */
  730. void close_candev(struct net_device *dev)
  731. {
  732. struct can_priv *priv = netdev_priv(dev);
  733. cancel_delayed_work_sync(&priv->restart_work);
  734. can_flush_echo_skb(dev);
  735. }
  736. EXPORT_SYMBOL_GPL(close_candev);
  737. /*
  738. * CAN netlink interface
  739. */
  740. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  741. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  742. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  743. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  744. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  745. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  746. [IFLA_CAN_BITTIMING_CONST]
  747. = { .len = sizeof(struct can_bittiming_const) },
  748. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  749. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  750. [IFLA_CAN_DATA_BITTIMING]
  751. = { .len = sizeof(struct can_bittiming) },
  752. [IFLA_CAN_DATA_BITTIMING_CONST]
  753. = { .len = sizeof(struct can_bittiming_const) },
  754. [IFLA_CAN_TERMINATION] = { .type = NLA_U16 },
  755. };
  756. static int can_validate(struct nlattr *tb[], struct nlattr *data[],
  757. struct netlink_ext_ack *extack)
  758. {
  759. bool is_can_fd = false;
  760. /* Make sure that valid CAN FD configurations always consist of
  761. * - nominal/arbitration bittiming
  762. * - data bittiming
  763. * - control mode with CAN_CTRLMODE_FD set
  764. */
  765. if (!data)
  766. return 0;
  767. if (data[IFLA_CAN_CTRLMODE]) {
  768. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  769. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  770. }
  771. if (is_can_fd) {
  772. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  773. return -EOPNOTSUPP;
  774. }
  775. if (data[IFLA_CAN_DATA_BITTIMING]) {
  776. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  777. return -EOPNOTSUPP;
  778. }
  779. return 0;
  780. }
  781. static int can_changelink(struct net_device *dev, struct nlattr *tb[],
  782. struct nlattr *data[],
  783. struct netlink_ext_ack *extack)
  784. {
  785. struct can_priv *priv = netdev_priv(dev);
  786. int err;
  787. /* We need synchronization with dev->stop() */
  788. ASSERT_RTNL();
  789. if (data[IFLA_CAN_BITTIMING]) {
  790. struct can_bittiming bt;
  791. /* Do not allow changing bittiming while running */
  792. if (dev->flags & IFF_UP)
  793. return -EBUSY;
  794. /* Calculate bittiming parameters based on
  795. * bittiming_const if set, otherwise pass bitrate
  796. * directly via do_set_bitrate(). Bail out if neither
  797. * is given.
  798. */
  799. if (!priv->bittiming_const && !priv->do_set_bittiming)
  800. return -EOPNOTSUPP;
  801. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  802. err = can_get_bittiming(dev, &bt,
  803. priv->bittiming_const,
  804. priv->bitrate_const,
  805. priv->bitrate_const_cnt);
  806. if (err)
  807. return err;
  808. if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
  809. netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
  810. priv->bitrate_max);
  811. return -EINVAL;
  812. }
  813. memcpy(&priv->bittiming, &bt, sizeof(bt));
  814. if (priv->do_set_bittiming) {
  815. /* Finally, set the bit-timing registers */
  816. err = priv->do_set_bittiming(dev);
  817. if (err)
  818. return err;
  819. }
  820. }
  821. if (data[IFLA_CAN_CTRLMODE]) {
  822. struct can_ctrlmode *cm;
  823. u32 ctrlstatic;
  824. u32 maskedflags;
  825. /* Do not allow changing controller mode while running */
  826. if (dev->flags & IFF_UP)
  827. return -EBUSY;
  828. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  829. ctrlstatic = priv->ctrlmode_static;
  830. maskedflags = cm->flags & cm->mask;
  831. /* check whether provided bits are allowed to be passed */
  832. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  833. return -EOPNOTSUPP;
  834. /* do not check for static fd-non-iso if 'fd' is disabled */
  835. if (!(maskedflags & CAN_CTRLMODE_FD))
  836. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  837. /* make sure static options are provided by configuration */
  838. if ((maskedflags & ctrlstatic) != ctrlstatic)
  839. return -EOPNOTSUPP;
  840. /* clear bits to be modified and copy the flag values */
  841. priv->ctrlmode &= ~cm->mask;
  842. priv->ctrlmode |= maskedflags;
  843. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  844. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  845. dev->mtu = CANFD_MTU;
  846. else
  847. dev->mtu = CAN_MTU;
  848. }
  849. if (data[IFLA_CAN_RESTART_MS]) {
  850. /* Do not allow changing restart delay while running */
  851. if (dev->flags & IFF_UP)
  852. return -EBUSY;
  853. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  854. }
  855. if (data[IFLA_CAN_RESTART]) {
  856. /* Do not allow a restart while not running */
  857. if (!(dev->flags & IFF_UP))
  858. return -EINVAL;
  859. err = can_restart_now(dev);
  860. if (err)
  861. return err;
  862. }
  863. if (data[IFLA_CAN_DATA_BITTIMING]) {
  864. struct can_bittiming dbt;
  865. /* Do not allow changing bittiming while running */
  866. if (dev->flags & IFF_UP)
  867. return -EBUSY;
  868. /* Calculate bittiming parameters based on
  869. * data_bittiming_const if set, otherwise pass bitrate
  870. * directly via do_set_bitrate(). Bail out if neither
  871. * is given.
  872. */
  873. if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
  874. return -EOPNOTSUPP;
  875. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  876. sizeof(dbt));
  877. err = can_get_bittiming(dev, &dbt,
  878. priv->data_bittiming_const,
  879. priv->data_bitrate_const,
  880. priv->data_bitrate_const_cnt);
  881. if (err)
  882. return err;
  883. if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
  884. netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
  885. priv->bitrate_max);
  886. return -EINVAL;
  887. }
  888. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  889. if (priv->do_set_data_bittiming) {
  890. /* Finally, set the bit-timing registers */
  891. err = priv->do_set_data_bittiming(dev);
  892. if (err)
  893. return err;
  894. }
  895. }
  896. if (data[IFLA_CAN_TERMINATION]) {
  897. const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
  898. const unsigned int num_term = priv->termination_const_cnt;
  899. unsigned int i;
  900. if (!priv->do_set_termination)
  901. return -EOPNOTSUPP;
  902. /* check whether given value is supported by the interface */
  903. for (i = 0; i < num_term; i++) {
  904. if (termval == priv->termination_const[i])
  905. break;
  906. }
  907. if (i >= num_term)
  908. return -EINVAL;
  909. /* Finally, set the termination value */
  910. err = priv->do_set_termination(dev, termval);
  911. if (err)
  912. return err;
  913. priv->termination = termval;
  914. }
  915. return 0;
  916. }
  917. static size_t can_get_size(const struct net_device *dev)
  918. {
  919. struct can_priv *priv = netdev_priv(dev);
  920. size_t size = 0;
  921. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  922. size += nla_total_size(sizeof(struct can_bittiming));
  923. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  924. size += nla_total_size(sizeof(struct can_bittiming_const));
  925. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  926. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  927. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  928. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  929. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  930. size += nla_total_size(sizeof(struct can_berr_counter));
  931. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  932. size += nla_total_size(sizeof(struct can_bittiming));
  933. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  934. size += nla_total_size(sizeof(struct can_bittiming_const));
  935. if (priv->termination_const) {
  936. size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
  937. size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
  938. priv->termination_const_cnt);
  939. }
  940. if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
  941. size += nla_total_size(sizeof(*priv->bitrate_const) *
  942. priv->bitrate_const_cnt);
  943. if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
  944. size += nla_total_size(sizeof(*priv->data_bitrate_const) *
  945. priv->data_bitrate_const_cnt);
  946. size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
  947. return size;
  948. }
  949. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  950. {
  951. struct can_priv *priv = netdev_priv(dev);
  952. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  953. struct can_berr_counter bec = { };
  954. enum can_state state = priv->state;
  955. if (priv->do_get_state)
  956. priv->do_get_state(dev, &state);
  957. if ((priv->bittiming.bitrate &&
  958. nla_put(skb, IFLA_CAN_BITTIMING,
  959. sizeof(priv->bittiming), &priv->bittiming)) ||
  960. (priv->bittiming_const &&
  961. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  962. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  963. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  964. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  965. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  966. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  967. (priv->do_get_berr_counter &&
  968. !priv->do_get_berr_counter(dev, &bec) &&
  969. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  970. (priv->data_bittiming.bitrate &&
  971. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  972. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  973. (priv->data_bittiming_const &&
  974. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  975. sizeof(*priv->data_bittiming_const),
  976. priv->data_bittiming_const)) ||
  977. (priv->termination_const &&
  978. (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
  979. nla_put(skb, IFLA_CAN_TERMINATION_CONST,
  980. sizeof(*priv->termination_const) *
  981. priv->termination_const_cnt,
  982. priv->termination_const))) ||
  983. (priv->bitrate_const &&
  984. nla_put(skb, IFLA_CAN_BITRATE_CONST,
  985. sizeof(*priv->bitrate_const) *
  986. priv->bitrate_const_cnt,
  987. priv->bitrate_const)) ||
  988. (priv->data_bitrate_const &&
  989. nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
  990. sizeof(*priv->data_bitrate_const) *
  991. priv->data_bitrate_const_cnt,
  992. priv->data_bitrate_const)) ||
  993. (nla_put(skb, IFLA_CAN_BITRATE_MAX,
  994. sizeof(priv->bitrate_max),
  995. &priv->bitrate_max))
  996. )
  997. return -EMSGSIZE;
  998. return 0;
  999. }
  1000. static size_t can_get_xstats_size(const struct net_device *dev)
  1001. {
  1002. return sizeof(struct can_device_stats);
  1003. }
  1004. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  1005. {
  1006. struct can_priv *priv = netdev_priv(dev);
  1007. if (nla_put(skb, IFLA_INFO_XSTATS,
  1008. sizeof(priv->can_stats), &priv->can_stats))
  1009. goto nla_put_failure;
  1010. return 0;
  1011. nla_put_failure:
  1012. return -EMSGSIZE;
  1013. }
  1014. static int can_newlink(struct net *src_net, struct net_device *dev,
  1015. struct nlattr *tb[], struct nlattr *data[],
  1016. struct netlink_ext_ack *extack)
  1017. {
  1018. return -EOPNOTSUPP;
  1019. }
  1020. static void can_dellink(struct net_device *dev, struct list_head *head)
  1021. {
  1022. return;
  1023. }
  1024. static struct rtnl_link_ops can_link_ops __read_mostly = {
  1025. .kind = "can",
  1026. .netns_refund = true,
  1027. .maxtype = IFLA_CAN_MAX,
  1028. .policy = can_policy,
  1029. .setup = can_setup,
  1030. .validate = can_validate,
  1031. .newlink = can_newlink,
  1032. .changelink = can_changelink,
  1033. .dellink = can_dellink,
  1034. .get_size = can_get_size,
  1035. .fill_info = can_fill_info,
  1036. .get_xstats_size = can_get_xstats_size,
  1037. .fill_xstats = can_fill_xstats,
  1038. };
  1039. /*
  1040. * Register the CAN network device
  1041. */
  1042. int register_candev(struct net_device *dev)
  1043. {
  1044. struct can_priv *priv = netdev_priv(dev);
  1045. /* Ensure termination_const, termination_const_cnt and
  1046. * do_set_termination consistency. All must be either set or
  1047. * unset.
  1048. */
  1049. if ((!priv->termination_const != !priv->termination_const_cnt) ||
  1050. (!priv->termination_const != !priv->do_set_termination))
  1051. return -EINVAL;
  1052. if (!priv->bitrate_const != !priv->bitrate_const_cnt)
  1053. return -EINVAL;
  1054. if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
  1055. return -EINVAL;
  1056. dev->rtnl_link_ops = &can_link_ops;
  1057. netif_carrier_off(dev);
  1058. return register_netdev(dev);
  1059. }
  1060. EXPORT_SYMBOL_GPL(register_candev);
  1061. /*
  1062. * Unregister the CAN network device
  1063. */
  1064. void unregister_candev(struct net_device *dev)
  1065. {
  1066. unregister_netdev(dev);
  1067. }
  1068. EXPORT_SYMBOL_GPL(unregister_candev);
  1069. /*
  1070. * Test if a network device is a candev based device
  1071. * and return the can_priv* if so.
  1072. */
  1073. struct can_priv *safe_candev_priv(struct net_device *dev)
  1074. {
  1075. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  1076. return NULL;
  1077. return netdev_priv(dev);
  1078. }
  1079. EXPORT_SYMBOL_GPL(safe_candev_priv);
  1080. static __init int can_dev_init(void)
  1081. {
  1082. int err;
  1083. can_led_notifier_init();
  1084. err = rtnl_link_register(&can_link_ops);
  1085. if (!err)
  1086. printk(KERN_INFO MOD_DESC "\n");
  1087. return err;
  1088. }
  1089. module_init(can_dev_init);
  1090. static __exit void can_dev_exit(void)
  1091. {
  1092. rtnl_link_unregister(&can_link_ops);
  1093. can_led_notifier_exit();
  1094. }
  1095. module_exit(can_dev_exit);
  1096. MODULE_ALIAS_RTNL_LINK("can");