dev.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. /*
  64. * Bit-timing calculation derived from:
  65. *
  66. * Code based on LinCAN sources and H8S2638 project
  67. * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
  68. * Copyright 2005 Stanislav Marek
  69. * email: pisa@cmp.felk.cvut.cz
  70. *
  71. * Calculates proper bit-timing parameters for a specified bit-rate
  72. * and sample-point, which can then be used to set the bit-timing
  73. * registers of the CAN controller. You can find more information
  74. * in the header file linux/can/netlink.h.
  75. */
  76. static int can_update_spt(const struct can_bittiming_const *btc,
  77. int sampl_pt, int tseg, int *tseg1, int *tseg2)
  78. {
  79. *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
  80. if (*tseg2 < btc->tseg2_min)
  81. *tseg2 = btc->tseg2_min;
  82. if (*tseg2 > btc->tseg2_max)
  83. *tseg2 = btc->tseg2_max;
  84. *tseg1 = tseg - *tseg2;
  85. if (*tseg1 > btc->tseg1_max) {
  86. *tseg1 = btc->tseg1_max;
  87. *tseg2 = tseg - *tseg1;
  88. }
  89. return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
  90. }
  91. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  92. const struct can_bittiming_const *btc)
  93. {
  94. struct can_priv *priv = netdev_priv(dev);
  95. long best_error = 1000000000, error = 0;
  96. int best_tseg = 0, best_brp = 0, brp = 0;
  97. int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
  98. int spt_error = 1000, spt = 0, sampl_pt;
  99. long rate;
  100. u64 v64;
  101. /* Use CiA recommended sample points */
  102. if (bt->sample_point) {
  103. sampl_pt = bt->sample_point;
  104. } else {
  105. if (bt->bitrate > 800000)
  106. sampl_pt = 750;
  107. else if (bt->bitrate > 500000)
  108. sampl_pt = 800;
  109. else
  110. sampl_pt = 875;
  111. }
  112. /* tseg even = round down, odd = round up */
  113. for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
  114. tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
  115. tsegall = 1 + tseg / 2;
  116. /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
  117. brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
  118. /* chose brp step which is possible in system */
  119. brp = (brp / btc->brp_inc) * btc->brp_inc;
  120. if ((brp < btc->brp_min) || (brp > btc->brp_max))
  121. continue;
  122. rate = priv->clock.freq / (brp * tsegall);
  123. error = bt->bitrate - rate;
  124. /* tseg brp biterror */
  125. if (error < 0)
  126. error = -error;
  127. if (error > best_error)
  128. continue;
  129. best_error = error;
  130. if (error == 0) {
  131. spt = can_update_spt(btc, sampl_pt, tseg / 2,
  132. &tseg1, &tseg2);
  133. error = sampl_pt - spt;
  134. if (error < 0)
  135. error = -error;
  136. if (error > spt_error)
  137. continue;
  138. spt_error = error;
  139. }
  140. best_tseg = tseg / 2;
  141. best_brp = brp;
  142. if (error == 0)
  143. break;
  144. }
  145. if (best_error) {
  146. /* Error in one-tenth of a percent */
  147. error = (best_error * 1000) / bt->bitrate;
  148. if (error > CAN_CALC_MAX_ERROR) {
  149. netdev_err(dev,
  150. "bitrate error %ld.%ld%% too high\n",
  151. error / 10, error % 10);
  152. return -EDOM;
  153. } else {
  154. netdev_warn(dev, "bitrate error %ld.%ld%%\n",
  155. error / 10, error % 10);
  156. }
  157. }
  158. /* real sample point */
  159. bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
  160. &tseg1, &tseg2);
  161. v64 = (u64)best_brp * 1000000000UL;
  162. do_div(v64, priv->clock.freq);
  163. bt->tq = (u32)v64;
  164. bt->prop_seg = tseg1 / 2;
  165. bt->phase_seg1 = tseg1 - bt->prop_seg;
  166. bt->phase_seg2 = tseg2;
  167. /* check for sjw user settings */
  168. if (!bt->sjw || !btc->sjw_max)
  169. bt->sjw = 1;
  170. else {
  171. /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
  172. if (bt->sjw > btc->sjw_max)
  173. bt->sjw = btc->sjw_max;
  174. /* bt->sjw must not be higher than tseg2 */
  175. if (tseg2 < bt->sjw)
  176. bt->sjw = tseg2;
  177. }
  178. bt->brp = best_brp;
  179. /* real bit-rate */
  180. bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
  181. return 0;
  182. }
  183. #else /* !CONFIG_CAN_CALC_BITTIMING */
  184. static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  185. const struct can_bittiming_const *btc)
  186. {
  187. netdev_err(dev, "bit-timing calculation not available\n");
  188. return -EINVAL;
  189. }
  190. #endif /* CONFIG_CAN_CALC_BITTIMING */
  191. /*
  192. * Checks the validity of the specified bit-timing parameters prop_seg,
  193. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  194. * prescaler value brp. You can find more information in the header
  195. * file linux/can/netlink.h.
  196. */
  197. static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
  198. const struct can_bittiming_const *btc)
  199. {
  200. struct can_priv *priv = netdev_priv(dev);
  201. int tseg1, alltseg;
  202. u64 brp64;
  203. tseg1 = bt->prop_seg + bt->phase_seg1;
  204. if (!bt->sjw)
  205. bt->sjw = 1;
  206. if (bt->sjw > btc->sjw_max ||
  207. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  208. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  209. return -ERANGE;
  210. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  211. if (btc->brp_inc > 1)
  212. do_div(brp64, btc->brp_inc);
  213. brp64 += 500000000UL - 1;
  214. do_div(brp64, 1000000000UL); /* the practicable BRP */
  215. if (btc->brp_inc > 1)
  216. brp64 *= btc->brp_inc;
  217. bt->brp = (u32)brp64;
  218. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  219. return -EINVAL;
  220. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  221. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  222. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  223. return 0;
  224. }
  225. static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
  226. const struct can_bittiming_const *btc)
  227. {
  228. int err;
  229. /* Check if the CAN device has bit-timing parameters */
  230. if (!btc)
  231. return -EOPNOTSUPP;
  232. /*
  233. * Depending on the given can_bittiming parameter structure the CAN
  234. * timing parameters are calculated based on the provided bitrate OR
  235. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  236. * provided directly which are then checked and fixed up.
  237. */
  238. if (!bt->tq && bt->bitrate)
  239. err = can_calc_bittiming(dev, bt, btc);
  240. else if (bt->tq && !bt->bitrate)
  241. err = can_fixup_bittiming(dev, bt, btc);
  242. else
  243. err = -EINVAL;
  244. return err;
  245. }
  246. static void can_update_state_error_stats(struct net_device *dev,
  247. enum can_state new_state)
  248. {
  249. struct can_priv *priv = netdev_priv(dev);
  250. if (new_state <= priv->state)
  251. return;
  252. switch (new_state) {
  253. case CAN_STATE_ERROR_WARNING:
  254. priv->can_stats.error_warning++;
  255. break;
  256. case CAN_STATE_ERROR_PASSIVE:
  257. priv->can_stats.error_passive++;
  258. break;
  259. case CAN_STATE_BUS_OFF:
  260. priv->can_stats.bus_off++;
  261. break;
  262. default:
  263. break;
  264. }
  265. }
  266. static int can_tx_state_to_frame(struct net_device *dev, enum can_state state)
  267. {
  268. switch (state) {
  269. case CAN_STATE_ERROR_ACTIVE:
  270. return CAN_ERR_CRTL_ACTIVE;
  271. case CAN_STATE_ERROR_WARNING:
  272. return CAN_ERR_CRTL_TX_WARNING;
  273. case CAN_STATE_ERROR_PASSIVE:
  274. return CAN_ERR_CRTL_TX_PASSIVE;
  275. default:
  276. return 0;
  277. }
  278. }
  279. static int can_rx_state_to_frame(struct net_device *dev, enum can_state state)
  280. {
  281. switch (state) {
  282. case CAN_STATE_ERROR_ACTIVE:
  283. return CAN_ERR_CRTL_ACTIVE;
  284. case CAN_STATE_ERROR_WARNING:
  285. return CAN_ERR_CRTL_RX_WARNING;
  286. case CAN_STATE_ERROR_PASSIVE:
  287. return CAN_ERR_CRTL_RX_PASSIVE;
  288. default:
  289. return 0;
  290. }
  291. }
  292. void can_change_state(struct net_device *dev, struct can_frame *cf,
  293. enum can_state tx_state, enum can_state rx_state)
  294. {
  295. struct can_priv *priv = netdev_priv(dev);
  296. enum can_state new_state = max(tx_state, rx_state);
  297. if (unlikely(new_state == priv->state)) {
  298. netdev_warn(dev, "%s: oops, state did not change", __func__);
  299. return;
  300. }
  301. netdev_dbg(dev, "New error state: %d\n", new_state);
  302. can_update_state_error_stats(dev, new_state);
  303. priv->state = new_state;
  304. if (unlikely(new_state == CAN_STATE_BUS_OFF)) {
  305. cf->can_id |= CAN_ERR_BUSOFF;
  306. return;
  307. }
  308. cf->can_id |= CAN_ERR_CRTL;
  309. cf->data[1] |= tx_state >= rx_state ?
  310. can_tx_state_to_frame(dev, tx_state) : 0;
  311. cf->data[1] |= tx_state <= rx_state ?
  312. can_rx_state_to_frame(dev, rx_state) : 0;
  313. }
  314. EXPORT_SYMBOL_GPL(can_change_state);
  315. /*
  316. * Local echo of CAN messages
  317. *
  318. * CAN network devices *should* support a local echo functionality
  319. * (see Documentation/networking/can.txt). To test the handling of CAN
  320. * interfaces that do not support the local echo both driver types are
  321. * implemented. In the case that the driver does not support the echo
  322. * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core
  323. * to perform the echo as a fallback solution.
  324. */
  325. static void can_flush_echo_skb(struct net_device *dev)
  326. {
  327. struct can_priv *priv = netdev_priv(dev);
  328. struct net_device_stats *stats = &dev->stats;
  329. int i;
  330. for (i = 0; i < priv->echo_skb_max; i++) {
  331. if (priv->echo_skb[i]) {
  332. kfree_skb(priv->echo_skb[i]);
  333. priv->echo_skb[i] = NULL;
  334. stats->tx_dropped++;
  335. stats->tx_aborted_errors++;
  336. }
  337. }
  338. }
  339. /*
  340. * Put the skb on the stack to be looped backed locally lateron
  341. *
  342. * The function is typically called in the start_xmit function
  343. * of the device driver. The driver must protect access to
  344. * priv->echo_skb, if necessary.
  345. */
  346. void can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
  347. unsigned int idx)
  348. {
  349. struct can_priv *priv = netdev_priv(dev);
  350. BUG_ON(idx >= priv->echo_skb_max);
  351. /* check flag whether this packet has to be looped back */
  352. if (!(dev->flags & IFF_ECHO) || skb->pkt_type != PACKET_LOOPBACK ||
  353. (skb->protocol != htons(ETH_P_CAN) &&
  354. skb->protocol != htons(ETH_P_CANFD))) {
  355. kfree_skb(skb);
  356. return;
  357. }
  358. if (!priv->echo_skb[idx]) {
  359. skb = can_create_echo_skb(skb);
  360. if (!skb)
  361. return;
  362. /* make settings for echo to reduce code in irq context */
  363. skb->pkt_type = PACKET_BROADCAST;
  364. skb->ip_summed = CHECKSUM_UNNECESSARY;
  365. skb->dev = dev;
  366. /* save this skb for tx interrupt echo handling */
  367. priv->echo_skb[idx] = skb;
  368. } else {
  369. /* locking problem with netif_stop_queue() ?? */
  370. netdev_err(dev, "%s: BUG! echo_skb is occupied!\n", __func__);
  371. kfree_skb(skb);
  372. }
  373. }
  374. EXPORT_SYMBOL_GPL(can_put_echo_skb);
  375. struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr)
  376. {
  377. struct can_priv *priv = netdev_priv(dev);
  378. if (idx >= priv->echo_skb_max) {
  379. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  380. __func__, idx, priv->echo_skb_max);
  381. return NULL;
  382. }
  383. if (priv->echo_skb[idx]) {
  384. /* Using "struct canfd_frame::len" for the frame
  385. * length is supported on both CAN and CANFD frames.
  386. */
  387. struct sk_buff *skb = priv->echo_skb[idx];
  388. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  389. u8 len = cf->len;
  390. *len_ptr = len;
  391. priv->echo_skb[idx] = NULL;
  392. return skb;
  393. }
  394. return NULL;
  395. }
  396. /*
  397. * Get the skb from the stack and loop it back locally
  398. *
  399. * The function is typically called when the TX done interrupt
  400. * is handled in the device driver. The driver must protect
  401. * access to priv->echo_skb, if necessary.
  402. */
  403. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  404. {
  405. struct sk_buff *skb;
  406. u8 len;
  407. skb = __can_get_echo_skb(dev, idx, &len);
  408. if (!skb)
  409. return 0;
  410. netif_rx(skb);
  411. return len;
  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. /*
  661. * Common close function for cleanup before the device gets closed.
  662. *
  663. * This function should be called in the close function of the device
  664. * driver.
  665. */
  666. void close_candev(struct net_device *dev)
  667. {
  668. struct can_priv *priv = netdev_priv(dev);
  669. cancel_delayed_work_sync(&priv->restart_work);
  670. can_flush_echo_skb(dev);
  671. }
  672. EXPORT_SYMBOL_GPL(close_candev);
  673. /*
  674. * CAN netlink interface
  675. */
  676. static const struct nla_policy can_policy[IFLA_CAN_MAX + 1] = {
  677. [IFLA_CAN_STATE] = { .type = NLA_U32 },
  678. [IFLA_CAN_CTRLMODE] = { .len = sizeof(struct can_ctrlmode) },
  679. [IFLA_CAN_RESTART_MS] = { .type = NLA_U32 },
  680. [IFLA_CAN_RESTART] = { .type = NLA_U32 },
  681. [IFLA_CAN_BITTIMING] = { .len = sizeof(struct can_bittiming) },
  682. [IFLA_CAN_BITTIMING_CONST]
  683. = { .len = sizeof(struct can_bittiming_const) },
  684. [IFLA_CAN_CLOCK] = { .len = sizeof(struct can_clock) },
  685. [IFLA_CAN_BERR_COUNTER] = { .len = sizeof(struct can_berr_counter) },
  686. [IFLA_CAN_DATA_BITTIMING]
  687. = { .len = sizeof(struct can_bittiming) },
  688. [IFLA_CAN_DATA_BITTIMING_CONST]
  689. = { .len = sizeof(struct can_bittiming_const) },
  690. };
  691. static int can_validate(struct nlattr *tb[], struct nlattr *data[])
  692. {
  693. bool is_can_fd = false;
  694. /* Make sure that valid CAN FD configurations always consist of
  695. * - nominal/arbitration bittiming
  696. * - data bittiming
  697. * - control mode with CAN_CTRLMODE_FD set
  698. */
  699. if (!data)
  700. return 0;
  701. if (data[IFLA_CAN_CTRLMODE]) {
  702. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  703. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  704. }
  705. if (is_can_fd) {
  706. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  707. return -EOPNOTSUPP;
  708. }
  709. if (data[IFLA_CAN_DATA_BITTIMING]) {
  710. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  711. return -EOPNOTSUPP;
  712. }
  713. return 0;
  714. }
  715. static int can_changelink(struct net_device *dev,
  716. struct nlattr *tb[], struct nlattr *data[])
  717. {
  718. struct can_priv *priv = netdev_priv(dev);
  719. int err;
  720. /* We need synchronization with dev->stop() */
  721. ASSERT_RTNL();
  722. if (data[IFLA_CAN_BITTIMING]) {
  723. struct can_bittiming bt;
  724. /* Do not allow changing bittiming while running */
  725. if (dev->flags & IFF_UP)
  726. return -EBUSY;
  727. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  728. err = can_get_bittiming(dev, &bt, priv->bittiming_const);
  729. if (err)
  730. return err;
  731. memcpy(&priv->bittiming, &bt, sizeof(bt));
  732. if (priv->do_set_bittiming) {
  733. /* Finally, set the bit-timing registers */
  734. err = priv->do_set_bittiming(dev);
  735. if (err)
  736. return err;
  737. }
  738. }
  739. if (data[IFLA_CAN_CTRLMODE]) {
  740. struct can_ctrlmode *cm;
  741. u32 ctrlstatic;
  742. u32 maskedflags;
  743. /* Do not allow changing controller mode while running */
  744. if (dev->flags & IFF_UP)
  745. return -EBUSY;
  746. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  747. ctrlstatic = priv->ctrlmode_static;
  748. maskedflags = cm->flags & cm->mask;
  749. /* check whether provided bits are allowed to be passed */
  750. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  751. return -EOPNOTSUPP;
  752. /* do not check for static fd-non-iso if 'fd' is disabled */
  753. if (!(maskedflags & CAN_CTRLMODE_FD))
  754. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  755. /* make sure static options are provided by configuration */
  756. if ((maskedflags & ctrlstatic) != ctrlstatic)
  757. return -EOPNOTSUPP;
  758. /* clear bits to be modified and copy the flag values */
  759. priv->ctrlmode &= ~cm->mask;
  760. priv->ctrlmode |= maskedflags;
  761. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  762. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  763. dev->mtu = CANFD_MTU;
  764. else
  765. dev->mtu = CAN_MTU;
  766. }
  767. if (data[IFLA_CAN_RESTART_MS]) {
  768. /* Do not allow changing restart delay while running */
  769. if (dev->flags & IFF_UP)
  770. return -EBUSY;
  771. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  772. }
  773. if (data[IFLA_CAN_RESTART]) {
  774. /* Do not allow a restart while not running */
  775. if (!(dev->flags & IFF_UP))
  776. return -EINVAL;
  777. err = can_restart_now(dev);
  778. if (err)
  779. return err;
  780. }
  781. if (data[IFLA_CAN_DATA_BITTIMING]) {
  782. struct can_bittiming dbt;
  783. /* Do not allow changing bittiming while running */
  784. if (dev->flags & IFF_UP)
  785. return -EBUSY;
  786. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  787. sizeof(dbt));
  788. err = can_get_bittiming(dev, &dbt, priv->data_bittiming_const);
  789. if (err)
  790. return err;
  791. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  792. if (priv->do_set_data_bittiming) {
  793. /* Finally, set the bit-timing registers */
  794. err = priv->do_set_data_bittiming(dev);
  795. if (err)
  796. return err;
  797. }
  798. }
  799. return 0;
  800. }
  801. static size_t can_get_size(const struct net_device *dev)
  802. {
  803. struct can_priv *priv = netdev_priv(dev);
  804. size_t size = 0;
  805. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  806. size += nla_total_size(sizeof(struct can_bittiming));
  807. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  808. size += nla_total_size(sizeof(struct can_bittiming_const));
  809. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  810. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  811. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  812. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  813. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  814. size += nla_total_size(sizeof(struct can_berr_counter));
  815. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  816. size += nla_total_size(sizeof(struct can_bittiming));
  817. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  818. size += nla_total_size(sizeof(struct can_bittiming_const));
  819. return size;
  820. }
  821. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  822. {
  823. struct can_priv *priv = netdev_priv(dev);
  824. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  825. struct can_berr_counter bec;
  826. enum can_state state = priv->state;
  827. if (priv->do_get_state)
  828. priv->do_get_state(dev, &state);
  829. if ((priv->bittiming.bitrate &&
  830. nla_put(skb, IFLA_CAN_BITTIMING,
  831. sizeof(priv->bittiming), &priv->bittiming)) ||
  832. (priv->bittiming_const &&
  833. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  834. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  835. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  836. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  837. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  838. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  839. (priv->do_get_berr_counter &&
  840. !priv->do_get_berr_counter(dev, &bec) &&
  841. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  842. (priv->data_bittiming.bitrate &&
  843. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  844. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  845. (priv->data_bittiming_const &&
  846. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  847. sizeof(*priv->data_bittiming_const),
  848. priv->data_bittiming_const)))
  849. return -EMSGSIZE;
  850. return 0;
  851. }
  852. static size_t can_get_xstats_size(const struct net_device *dev)
  853. {
  854. return sizeof(struct can_device_stats);
  855. }
  856. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  857. {
  858. struct can_priv *priv = netdev_priv(dev);
  859. if (nla_put(skb, IFLA_INFO_XSTATS,
  860. sizeof(priv->can_stats), &priv->can_stats))
  861. goto nla_put_failure;
  862. return 0;
  863. nla_put_failure:
  864. return -EMSGSIZE;
  865. }
  866. static int can_newlink(struct net *src_net, struct net_device *dev,
  867. struct nlattr *tb[], struct nlattr *data[])
  868. {
  869. return -EOPNOTSUPP;
  870. }
  871. static void can_dellink(struct net_device *dev, struct list_head *head)
  872. {
  873. return;
  874. }
  875. static struct rtnl_link_ops can_link_ops __read_mostly = {
  876. .kind = "can",
  877. .maxtype = IFLA_CAN_MAX,
  878. .policy = can_policy,
  879. .setup = can_setup,
  880. .validate = can_validate,
  881. .newlink = can_newlink,
  882. .changelink = can_changelink,
  883. .dellink = can_dellink,
  884. .get_size = can_get_size,
  885. .fill_info = can_fill_info,
  886. .get_xstats_size = can_get_xstats_size,
  887. .fill_xstats = can_fill_xstats,
  888. };
  889. /*
  890. * Register the CAN network device
  891. */
  892. int register_candev(struct net_device *dev)
  893. {
  894. dev->rtnl_link_ops = &can_link_ops;
  895. return register_netdev(dev);
  896. }
  897. EXPORT_SYMBOL_GPL(register_candev);
  898. /*
  899. * Unregister the CAN network device
  900. */
  901. void unregister_candev(struct net_device *dev)
  902. {
  903. unregister_netdev(dev);
  904. }
  905. EXPORT_SYMBOL_GPL(unregister_candev);
  906. /*
  907. * Test if a network device is a candev based device
  908. * and return the can_priv* if so.
  909. */
  910. struct can_priv *safe_candev_priv(struct net_device *dev)
  911. {
  912. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  913. return NULL;
  914. return netdev_priv(dev);
  915. }
  916. EXPORT_SYMBOL_GPL(safe_candev_priv);
  917. static __init int can_dev_init(void)
  918. {
  919. int err;
  920. can_led_notifier_init();
  921. err = rtnl_link_register(&can_link_ops);
  922. if (!err)
  923. printk(KERN_INFO MOD_DESC "\n");
  924. return err;
  925. }
  926. module_init(can_dev_init);
  927. static __exit void can_dev_exit(void)
  928. {
  929. rtnl_link_unregister(&can_link_ops);
  930. can_led_notifier_exit();
  931. }
  932. module_exit(can_dev_exit);
  933. MODULE_ALIAS_RTNL_LINK("can");