dev.c 29 KB

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