dev.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  243. const struct can_bittiming_const *btc)
  244. {
  245. int err;
  246. /* Check if the CAN device has bit-timing parameters */
  247. if (!btc)
  248. return -EOPNOTSUPP;
  249. /*
  250. * Depending on the given can_bittiming parameter structure the CAN
  251. * timing parameters are calculated based on the provided bitrate OR
  252. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  253. * provided directly which are then checked and fixed up.
  254. */
  255. if (!bt->tq && bt->bitrate)
  256. err = can_calc_bittiming(dev, bt, btc);
  257. else if (bt->tq && !bt->bitrate)
  258. err = can_fixup_bittiming(dev, bt, btc);
  259. else
  260. err = -EINVAL;
  261. return err;
  262. }
  263. static void can_update_state_error_stats(struct net_device *dev,
  264. enum can_state new_state)
  265. {
  266. struct can_priv *priv = netdev_priv(dev);
  267. if (new_state <= priv->state)
  268. return;
  269. switch (new_state) {
  270. case CAN_STATE_ERROR_WARNING:
  271. priv->can_stats.error_warning++;
  272. break;
  273. case CAN_STATE_ERROR_PASSIVE:
  274. priv->can_stats.error_passive++;
  275. break;
  276. case CAN_STATE_BUS_OFF:
  277. priv->can_stats.bus_off++;
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  284. {
  285. switch (state) {
  286. case CAN_STATE_ERROR_ACTIVE:
  287. return CAN_ERR_CRTL_ACTIVE;
  288. case CAN_STATE_ERROR_WARNING:
  289. return CAN_ERR_CRTL_TX_WARNING;
  290. case CAN_STATE_ERROR_PASSIVE:
  291. return CAN_ERR_CRTL_TX_PASSIVE;
  292. default:
  293. return 0;
  294. }
  295. }
  296. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  297. {
  298. switch (state) {
  299. case CAN_STATE_ERROR_ACTIVE:
  300. return CAN_ERR_CRTL_ACTIVE;
  301. case CAN_STATE_ERROR_WARNING:
  302. return CAN_ERR_CRTL_RX_WARNING;
  303. case CAN_STATE_ERROR_PASSIVE:
  304. return CAN_ERR_CRTL_RX_PASSIVE;
  305. default:
  306. return 0;
  307. }
  308. }
  309. void can_change_state(struct net_device *dev, struct can_frame *cf,
  310. enum can_state tx_state, enum can_state rx_state)
  311. {
  312. struct can_priv *priv = netdev_priv(dev);
  313. enum can_state new_state = max(tx_state, rx_state);
  314. if (unlikely(new_state == priv->state)) {
  315. netdev_warn(dev, "%s: oops, state did not change", __func__);
  316. return;
  317. }
  318. netdev_dbg(dev, "New error state: %d\n", new_state);
  319. can_update_state_error_stats(dev, new_state);
  320. priv->state = new_state;
  321. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  322. cf->can_id |= CAN_ERR_BUSOFF;
  323. return;
  324. }
  325. cf->can_id |= CAN_ERR_CRTL;
  326. cf->data[1] |= tx_state >= rx_state ?
  327. can_tx_state_to_frame(dev, tx_state) : 0;
  328. cf->data[1] |= tx_state <= rx_state ?
  329. can_rx_state_to_frame(dev, rx_state) : 0;
  330. }
  331. EXPORT_SYMBOL_GPL(can_change_state);
  332. /*
  333. * Local echo of CAN messages
  334. *
  335. * CAN network devices *should* support a local echo functionality
  336. * (see Documentation/networking/can.txt). To test the handling of CAN
  337. * interfaces that do not support the local echo both driver types are
  338. * implemented. In the case that the driver does not support the echo
  339. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  340. * to perform the echo as a fallback solution.
  341. */
  342. static void can_flush_echo_skb(struct net_device *dev)
  343. {
  344. struct can_priv *priv = netdev_priv(dev);
  345. struct net_device_stats *stats = &dev->stats;
  346. int i;
  347. for (i = 0; i < priv->echo_skb_max; i++) {
  348. if (priv->echo_skb[i]) {
  349. kfree_skb(priv->echo_skb[i]);
  350. priv->echo_skb[i] = NULL;
  351. stats->tx_dropped++;
  352. stats->tx_aborted_errors++;
  353. }
  354. }
  355. }
  356. /*
  357. * Put the skb on the stack to be looped backed locally lateron
  358. *
  359. * The function is typically called in the start_xmit function
  360. * of the device driver. The driver must protect access to
  361. * priv->echo_skb, if necessary.
  362. */
  363. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  364. unsigned int idx)
  365. {
  366. struct can_priv *priv = netdev_priv(dev);
  367. BUG_ON(idx >= priv->echo_skb_max);
  368. /* check flag whether this packet has to be looped back */
  369. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  370. (skb->protocol != htons(ETH_P_CAN) &&
  371. skb->protocol != htons(ETH_P_CANFD))) {
  372. kfree_skb(skb);
  373. return;
  374. }
  375. if (!priv->echo_skb[idx]) {
  376. skb = can_create_echo_skb(skb);
  377. if (!skb)
  378. return;
  379. /* make settings for echo to reduce code in irq context */
  380. skb->pkt_type = PACKET_BROADCAST;
  381. skb->ip_summed = CHECKSUM_UNNECESSARY;
  382. skb->dev = dev;
  383. /* save this skb for tx interrupt echo handling */
  384. priv->echo_skb[idx] = skb;
  385. } else {
  386. /* locking problem with netif_stop_queue() ?? */
  387. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  388. kfree_skb(skb);
  389. }
  390. }
  391. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  392. /*
  393. * Get the skb from the stack and loop it back locally
  394. *
  395. * The function is typically called when the TX done interrupt
  396. * is handled in the device driver. The driver must protect
  397. * access to priv->echo_skb, if necessary.
  398. */
  399. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  400. {
  401. struct can_priv *priv = netdev_priv(dev);
  402. BUG_ON(idx >= priv->echo_skb_max);
  403. if (priv->echo_skb[idx]) {
  404. struct sk_buff *skb = priv->echo_skb[idx];
  405. struct can_frame *cf = (struct can_frame *)skb->data;
  406. u8 dlc = cf->can_dlc;
  407. netif_rx(priv->echo_skb[idx]);
  408. priv->echo_skb[idx] = NULL;
  409. return dlc;
  410. }
  411. return 0;
  412. }
  413. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  414. /*
  415. * Remove the skb from the stack and free it.
  416. *
  417. * The function is typically called when TX failed.
  418. */
  419. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  420. {
  421. struct can_priv *priv = netdev_priv(dev);
  422. BUG_ON(idx >= priv->echo_skb_max);
  423. if (priv->echo_skb[idx]) {
  424. dev_kfree_skb_any(priv->echo_skb[idx]);
  425. priv->echo_skb[idx] = NULL;
  426. }
  427. }
  428. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  429. /*
  430. * CAN device restart for bus-off recovery
  431. */
  432. static void can_restart(struct net_device *dev)
  433. {
  434. struct can_priv *priv = netdev_priv(dev);
  435. struct net_device_stats *stats = &dev->stats;
  436. struct sk_buff *skb;
  437. struct can_frame *cf;
  438. int err;
  439. BUG_ON(netif_carrier_ok(dev));
  440. /*
  441. * No synchronization needed because the device is bus-off and
  442. * no messages can come in or go out.
  443. */
  444. can_flush_echo_skb(dev);
  445. /* send restart message upstream */
  446. skb = alloc_can_err_skb(dev, &cf);
  447. if (skb == NULL) {
  448. err = -ENOMEM;
  449. goto restart;
  450. }
  451. cf->can_id |= CAN_ERR_RESTARTED;
  452. netif_rx(skb);
  453. stats->rx_packets++;
  454. stats->rx_bytes += cf->can_dlc;
  455. restart:
  456. netdev_dbg(dev, "restarted\n");
  457. priv->can_stats.restarts++;
  458. /* Now restart the device */
  459. err = priv->do_set_mode(dev, CAN_MODE_START);
  460. netif_carrier_on(dev);
  461. if (err)
  462. netdev_err(dev, "Error %d during restart", err);
  463. }
  464. static void can_restart_work(struct work_struct *work)
  465. {
  466. struct delayed_work *dwork = to_delayed_work(work);
  467. struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
  468. can_restart(priv->dev);
  469. }
  470. int can_restart_now(struct net_device *dev)
  471. {
  472. struct can_priv *priv = netdev_priv(dev);
  473. /*
  474. * A manual restart is only permitted if automatic restart is
  475. * disabled and the device is in the bus-off state
  476. */
  477. if (priv->restart_ms)
  478. return -EINVAL;
  479. if (priv->state != CAN_STATE_BUS_OFF)
  480. return -EBUSY;
  481. cancel_delayed_work_sync(&priv->restart_work);
  482. can_restart(dev);
  483. return 0;
  484. }
  485. /*
  486. * CAN bus-off
  487. *
  488. * This functions should be called when the device goes bus-off to
  489. * tell the netif layer that no more packets can be sent or received.
  490. * If enabled, a timer is started to trigger bus-off recovery.
  491. */
  492. void can_bus_off(struct net_device *dev)
  493. {
  494. struct can_priv *priv = netdev_priv(dev);
  495. netdev_dbg(dev, "bus-off\n");
  496. netif_carrier_off(dev);
  497. if (priv->restart_ms)
  498. schedule_delayed_work(&priv->restart_work,
  499. msecs_to_jiffies(priv->restart_ms));
  500. }
  501. EXPORT_SYMBOL_GPL(can_bus_off);
  502. static void can_setup(struct net_device *dev)
  503. {
  504. dev->type = ARPHRD_CAN;
  505. dev->mtu = CAN_MTU;
  506. dev->hard_header_len = 0;
  507. dev->addr_len = 0;
  508. dev->tx_queue_len = 10;
  509. /* New-style flags. */
  510. dev->flags = IFF_NOARP;
  511. dev->features = NETIF_F_HW_CSUM;
  512. }
  513. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  514. {
  515. struct sk_buff *skb;
  516. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  517. sizeof(struct can_frame));
  518. if (unlikely(!skb))
  519. return NULL;
  520. skb->protocol = htons(ETH_P_CAN);
  521. skb->pkt_type = PACKET_BROADCAST;
  522. skb->ip_summed = CHECKSUM_UNNECESSARY;
  523. skb_reset_mac_header(skb);
  524. skb_reset_network_header(skb);
  525. skb_reset_transport_header(skb);
  526. can_skb_reserve(skb);
  527. can_skb_prv(skb)->ifindex = dev->ifindex;
  528. can_skb_prv(skb)->skbcnt = 0;
  529. *cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
  530. memset(*cf, 0, sizeof(struct can_frame));
  531. return skb;
  532. }
  533. EXPORT_SYMBOL_GPL(alloc_can_skb);
  534. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  535. struct canfd_frame **cfd)
  536. {
  537. struct sk_buff *skb;
  538. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  539. sizeof(struct canfd_frame));
  540. if (unlikely(!skb))
  541. return NULL;
  542. skb->protocol = htons(ETH_P_CANFD);
  543. skb->pkt_type = PACKET_BROADCAST;
  544. skb->ip_summed = CHECKSUM_UNNECESSARY;
  545. skb_reset_mac_header(skb);
  546. skb_reset_network_header(skb);
  547. skb_reset_transport_header(skb);
  548. can_skb_reserve(skb);
  549. can_skb_prv(skb)->ifindex = dev->ifindex;
  550. can_skb_prv(skb)->skbcnt = 0;
  551. *cfd = (struct canfd_frame *)skb_put(skb, sizeof(struct canfd_frame));
  552. memset(*cfd, 0, sizeof(struct canfd_frame));
  553. return skb;
  554. }
  555. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  556. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  557. {
  558. struct sk_buff *skb;
  559. skb = alloc_can_skb(dev, cf);
  560. if (unlikely(!skb))
  561. return NULL;
  562. (*cf)->can_id = CAN_ERR_FLAG;
  563. (*cf)->can_dlc = CAN_ERR_DLC;
  564. return skb;
  565. }
  566. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  567. /*
  568. * Allocate and setup space for the CAN network device
  569. */
  570. struct net_device *alloc_candev(int sizeof_priv, unsigned int echo_skb_max)
  571. {
  572. struct net_device *dev;
  573. struct can_priv *priv;
  574. int size;
  575. if (echo_skb_max)
  576. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  577. echo_skb_max * sizeof(struct sk_buff *);
  578. else
  579. size = sizeof_priv;
  580. dev = alloc_netdev(size, "can%d", NET_NAME_UNKNOWN, can_setup);
  581. if (!dev)
  582. return NULL;
  583. priv = netdev_priv(dev);
  584. priv->dev = dev;
  585. if (echo_skb_max) {
  586. priv->echo_skb_max = echo_skb_max;
  587. priv->echo_skb = (void *)priv +
  588. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  589. }
  590. priv->state = CAN_STATE_STOPPED;
  591. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  592. return dev;
  593. }
  594. EXPORT_SYMBOL_GPL(alloc_candev);
  595. /*
  596. * Free space of the CAN network device
  597. */
  598. void free_candev(struct net_device *dev)
  599. {
  600. free_netdev(dev);
  601. }
  602. EXPORT_SYMBOL_GPL(free_candev);
  603. /*
  604. * changing MTU and control mode for CAN/CANFD devices
  605. */
  606. int can_change_mtu(struct net_device *dev, int new_mtu)
  607. {
  608. struct can_priv *priv = netdev_priv(dev);
  609. /* Do not allow changing the MTU while running */
  610. if (dev->flags & IFF_UP)
  611. return -EBUSY;
  612. /* allow change of MTU according to the CANFD ability of the device */
  613. switch (new_mtu) {
  614. case CAN_MTU:
  615. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  616. if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
  617. return -EINVAL;
  618. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  619. break;
  620. case CANFD_MTU:
  621. /* check for potential CANFD ability */
  622. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  623. !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
  624. return -EINVAL;
  625. priv->ctrlmode |= CAN_CTRLMODE_FD;
  626. break;
  627. default:
  628. return -EINVAL;
  629. }
  630. dev->mtu = new_mtu;
  631. return 0;
  632. }
  633. EXPORT_SYMBOL_GPL(can_change_mtu);
  634. /*
  635. * Common open function when the device gets opened.
  636. *
  637. * This function should be called in the open function of the device
  638. * driver.
  639. */
  640. int open_candev(struct net_device *dev)
  641. {
  642. struct can_priv *priv = netdev_priv(dev);
  643. if (!priv->bittiming.bitrate) {
  644. netdev_err(dev, "bit-timing not yet defined\n");
  645. return -EINVAL;
  646. }
  647. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  648. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  649. (!priv->data_bittiming.bitrate ||
  650. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  651. netdev_err(dev, "incorrect/missing data bit-timing\n");
  652. return -EINVAL;
  653. }
  654. /* Switch carrier on if device was stopped while in bus-off state */
  655. if (!netif_carrier_ok(dev))
  656. netif_carrier_on(dev);
  657. return 0;
  658. }
  659. EXPORT_SYMBOL_GPL(open_candev);
  660. #ifdef CONFIG_OF
  661. /*
  662. * Common function that can be used to understand the limitation of
  663. * a transceiver when it provides no means to determine these limitations
  664. * at runtime.
  665. */
  666. void of_can_transceiver(struct net_device *dev)
  667. {
  668. struct device_node *dn;
  669. struct can_priv *priv = netdev_priv(dev);
  670. struct device_node *np = dev->dev.parent->of_node;
  671. int ret;
  672. dn = of_get_child_by_name(np, "can-transceiver");
  673. if (!dn)
  674. return;
  675. ret = of_property_read_u32(dn, "max-bitrate", &priv->max_bitrate);
  676. if ((ret && ret != -EINVAL) || (!ret && !priv->max_bitrate))
  677. netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
  678. }
  679. EXPORT_SYMBOL_GPL(of_can_transceiver);
  680. #endif
  681. /*
  682. * Common close function for cleanup before the device gets closed.
  683. *
  684. * This function should be called in the close function of the device
  685. * driver.
  686. */
  687. void close_candev(struct net_device *dev)
  688. {
  689. struct can_priv *priv = netdev_priv(dev);
  690. cancel_delayed_work_sync(&priv->restart_work);
  691. can_flush_echo_skb(dev);
  692. }
  693. EXPORT_SYMBOL_GPL(close_candev);
  694. /*
  695. * CAN netlink interface
  696. */
  697. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  698. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  699. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  700. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  701. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  702. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  703. [IFLA_CAN_BITTIMING_CONST]
  704. = { .len = sizeof(struct can_bittiming_const) },
  705. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  706. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  707. [IFLA_CAN_DATA_BITTIMING]
  708. = { .len = sizeof(struct can_bittiming) },
  709. [IFLA_CAN_DATA_BITTIMING_CONST]
  710. = { .len = sizeof(struct can_bittiming_const) },
  711. };
  712. static int can_validate(struct nlattr *tb[], struct nlattr *data[])
  713. {
  714. bool is_can_fd = false;
  715. /* Make sure that valid CAN FD configurations always consist of
  716. * - nominal/arbitration bittiming
  717. * - data bittiming
  718. * - control mode with CAN_CTRLMODE_FD set
  719. */
  720. if (!data)
  721. return 0;
  722. if (data[IFLA_CAN_CTRLMODE]) {
  723. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  724. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  725. }
  726. if (is_can_fd) {
  727. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  728. return -EOPNOTSUPP;
  729. }
  730. if (data[IFLA_CAN_DATA_BITTIMING]) {
  731. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  732. return -EOPNOTSUPP;
  733. }
  734. return 0;
  735. }
  736. static int can_changelink(struct net_device *dev,
  737. struct nlattr *tb[], struct nlattr *data[])
  738. {
  739. struct can_priv *priv = netdev_priv(dev);
  740. int err;
  741. /* We need synchronization with dev->stop() */
  742. ASSERT_RTNL();
  743. if (data[IFLA_CAN_BITTIMING]) {
  744. struct can_bittiming bt;
  745. /* Do not allow changing bittiming while running */
  746. if (dev->flags & IFF_UP)
  747. return -EBUSY;
  748. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  749. err = can_get_bittiming(dev, &bt, priv->bittiming_const);
  750. if (err)
  751. return err;
  752. if (priv->max_bitrate && bt.bitrate > priv->max_bitrate) {
  753. netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
  754. priv->max_bitrate);
  755. return -EINVAL;
  756. }
  757. memcpy(&priv->bittiming, &bt, sizeof(bt));
  758. if (priv->do_set_bittiming) {
  759. /* Finally, set the bit-timing registers */
  760. err = priv->do_set_bittiming(dev);
  761. if (err)
  762. return err;
  763. }
  764. }
  765. if (data[IFLA_CAN_CTRLMODE]) {
  766. struct can_ctrlmode *cm;
  767. u32 ctrlstatic;
  768. u32 maskedflags;
  769. /* Do not allow changing controller mode while running */
  770. if (dev->flags & IFF_UP)
  771. return -EBUSY;
  772. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  773. ctrlstatic = priv->ctrlmode_static;
  774. maskedflags = cm->flags & cm->mask;
  775. /* check whether provided bits are allowed to be passed */
  776. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  777. return -EOPNOTSUPP;
  778. /* do not check for static fd-non-iso if 'fd' is disabled */
  779. if (!(maskedflags & CAN_CTRLMODE_FD))
  780. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  781. /* make sure static options are provided by configuration */
  782. if ((maskedflags & ctrlstatic) != ctrlstatic)
  783. return -EOPNOTSUPP;
  784. /* clear bits to be modified and copy the flag values */
  785. priv->ctrlmode &= ~cm->mask;
  786. priv->ctrlmode |= maskedflags;
  787. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  788. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  789. dev->mtu = CANFD_MTU;
  790. else
  791. dev->mtu = CAN_MTU;
  792. }
  793. if (data[IFLA_CAN_RESTART_MS]) {
  794. /* Do not allow changing restart delay while running */
  795. if (dev->flags & IFF_UP)
  796. return -EBUSY;
  797. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  798. }
  799. if (data[IFLA_CAN_RESTART]) {
  800. /* Do not allow a restart while not running */
  801. if (!(dev->flags & IFF_UP))
  802. return -EINVAL;
  803. err = can_restart_now(dev);
  804. if (err)
  805. return err;
  806. }
  807. if (data[IFLA_CAN_DATA_BITTIMING]) {
  808. struct can_bittiming dbt;
  809. /* Do not allow changing bittiming while running */
  810. if (dev->flags & IFF_UP)
  811. return -EBUSY;
  812. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  813. sizeof(dbt));
  814. err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const);
  815. if (err)
  816. return err;
  817. if (priv->max_bitrate && dbt.bitrate > priv->max_bitrate) {
  818. netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
  819. priv->max_bitrate);
  820. return -EINVAL;
  821. }
  822. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  823. if (priv->do_set_data_bittiming) {
  824. /* Finally, set the bit-timing registers */
  825. err = priv->do_set_data_bittiming(dev);
  826. if (err)
  827. return err;
  828. }
  829. }
  830. return 0;
  831. }
  832. static size_t can_get_size(const struct net_device *dev)
  833. {
  834. struct can_priv *priv = netdev_priv(dev);
  835. size_t size = 0;
  836. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  837. size += nla_total_size(sizeof(struct can_bittiming));
  838. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  839. size += nla_total_size(sizeof(struct can_bittiming_const));
  840. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  841. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  842. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  843. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  844. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  845. size += nla_total_size(sizeof(struct can_berr_counter));
  846. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  847. size += nla_total_size(sizeof(struct can_bittiming));
  848. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  849. size += nla_total_size(sizeof(struct can_bittiming_const));
  850. return size;
  851. }
  852. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  853. {
  854. struct can_priv *priv = netdev_priv(dev);
  855. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  856. struct can_berr_counter bec;
  857. enum can_state state = priv->state;
  858. if (priv->do_get_state)
  859. priv->do_get_state(dev, &state);
  860. if ((priv->bittiming.bitrate &&
  861. nla_put(skb, IFLA_CAN_BITTIMING,
  862. sizeof(priv->bittiming), &priv->bittiming)) ||
  863. (priv->bittiming_const &&
  864. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  865. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  866. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  867. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  868. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  869. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  870. (priv->do_get_berr_counter &&
  871. !priv->do_get_berr_counter(dev, &bec) &&
  872. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  873. (priv->data_bittiming.bitrate &&
  874. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  875. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  876. (priv->data_bittiming_const &&
  877. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  878. sizeof(*priv->data_bittiming_const),
  879. priv->data_bittiming_const)))
  880. return -EMSGSIZE;
  881. return 0;
  882. }
  883. static size_t can_get_xstats_size(const struct net_device *dev)
  884. {
  885. return sizeof(struct can_device_stats);
  886. }
  887. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  888. {
  889. struct can_priv *priv = netdev_priv(dev);
  890. if (nla_put(skb, IFLA_INFO_XSTATS,
  891. sizeof(priv->can_stats), &priv->can_stats))
  892. goto nla_put_failure;
  893. return 0;
  894. nla_put_failure:
  895. return -EMSGSIZE;
  896. }
  897. static int can_newlink(struct net *src_net, struct net_device *dev,
  898. struct nlattr *tb[], struct nlattr *data[])
  899. {
  900. return -EOPNOTSUPP;
  901. }
  902. static void can_dellink(struct net_device *dev, struct list_head *head)
  903. {
  904. return;
  905. }
  906. static struct rtnl_link_ops can_link_ops __read_mostly = {
  907. .kind = "can",
  908. .maxtype = IFLA_CAN_MAX,
  909. .policy = can_policy,
  910. .setup = can_setup,
  911. .validate = can_validate,
  912. .newlink = can_newlink,
  913. .changelink = can_changelink,
  914. .dellink = can_dellink,
  915. .get_size = can_get_size,
  916. .fill_info = can_fill_info,
  917. .get_xstats_size = can_get_xstats_size,
  918. .fill_xstats = can_fill_xstats,
  919. };
  920. /*
  921. * Register the CAN network device
  922. */
  923. int register_candev(struct net_device *dev)
  924. {
  925. dev->rtnl_link_ops = &can_link_ops;
  926. return register_netdev(dev);
  927. }
  928. EXPORT_SYMBOL_GPL(register_candev);
  929. /*
  930. * Unregister the CAN network device
  931. */
  932. void unregister_candev(struct net_device *dev)
  933. {
  934. unregister_netdev(dev);
  935. }
  936. EXPORT_SYMBOL_GPL(unregister_candev);
  937. /*
  938. * Test if a network device is a candev based device
  939. * and return the can_priv* if so.
  940. */
  941. struct can_priv *safe_candev_priv(struct net_device *dev)
  942. {
  943. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  944. return NULL;
  945. return netdev_priv(dev);
  946. }
  947. EXPORT_SYMBOL_GPL(safe_candev_priv);
  948. static __init int can_dev_init(void)
  949. {
  950. int err;
  951. can_led_notifier_init();
  952. err = rtnl_link_register(&can_link_ops);
  953. if (!err)
  954. printk(KERN_INFO MOD_DESC "\n");
  955. return err;
  956. }
  957. module_init(can_dev_init);
  958. static __exit void can_dev_exit(void)
  959. {
  960. rtnl_link_unregister(&can_link_ops);
  961. can_led_notifier_exit();
  962. }
  963. module_exit(can_dev_exit);
  964. MODULE_ALIAS_RTNL_LINK("can");