dev.c 34 KB

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