dev.c 35 KB

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