dev.c 26 KB

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