dev.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. if (idx >= priv->echo_skb_max) {
  415. netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
  416. __func__, idx, priv->echo_skb_max);
  417. return NULL;
  418. }
  419. if (priv->echo_skb[idx]) {
  420. /* Using "struct canfd_frame::len" for the frame
  421. * length is supported on both CAN and CANFD frames.
  422. */
  423. struct sk_buff *skb = priv->echo_skb[idx];
  424. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  425. u8 len = cf->len;
  426. *len_ptr = len;
  427. priv->echo_skb[idx] = NULL;
  428. return skb;
  429. }
  430. return NULL;
  431. }
  432. /*
  433. * Get the skb from the stack and loop it back locally
  434. *
  435. * The function is typically called when the TX done interrupt
  436. * is handled in the device driver. The driver must protect
  437. * access to priv->echo_skb, if necessary.
  438. */
  439. unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
  440. {
  441. struct sk_buff *skb;
  442. u8 len;
  443. skb = __can_get_echo_skb(dev, idx, &len);
  444. if (!skb)
  445. return 0;
  446. netif_rx(skb);
  447. return len;
  448. }
  449. EXPORT_SYMBOL_GPL(can_get_echo_skb);
  450. /*
  451. * Remove the skb from the stack and free it.
  452. *
  453. * The function is typically called when TX failed.
  454. */
  455. void can_free_echo_skb(struct net_device *dev, unsigned int idx)
  456. {
  457. struct can_priv *priv = netdev_priv(dev);
  458. BUG_ON(idx >= priv->echo_skb_max);
  459. if (priv->echo_skb[idx]) {
  460. dev_kfree_skb_any(priv->echo_skb[idx]);
  461. priv->echo_skb[idx] = NULL;
  462. }
  463. }
  464. EXPORT_SYMBOL_GPL(can_free_echo_skb);
  465. /*
  466. * CAN device restart for bus-off recovery
  467. */
  468. static void can_restart(struct net_device *dev)
  469. {
  470. struct can_priv *priv = netdev_priv(dev);
  471. struct net_device_stats *stats = &dev->stats;
  472. struct sk_buff *skb;
  473. struct can_frame *cf;
  474. int err;
  475. BUG_ON(netif_carrier_ok(dev));
  476. /*
  477. * No synchronization needed because the device is bus-off and
  478. * no messages can come in or go out.
  479. */
  480. can_flush_echo_skb(dev);
  481. /* send restart message upstream */
  482. skb = alloc_can_err_skb(dev, &cf);
  483. if (skb == NULL) {
  484. err = -ENOMEM;
  485. goto restart;
  486. }
  487. cf->can_id |= CAN_ERR_RESTARTED;
  488. netif_rx(skb);
  489. stats->rx_packets++;
  490. stats->rx_bytes += cf->can_dlc;
  491. restart:
  492. netdev_dbg(dev, "restarted\n");
  493. priv->can_stats.restarts++;
  494. /* Now restart the device */
  495. err = priv->do_set_mode(dev, CAN_MODE_START);
  496. netif_carrier_on(dev);
  497. if (err)
  498. netdev_err(dev, "Error %d during restart", err);
  499. }
  500. static void can_restart_work(struct work_struct *work)
  501. {
  502. struct delayed_work *dwork = to_delayed_work(work);
  503. struct can_priv *priv = container_of(dwork, struct can_priv, restart_work);
  504. can_restart(priv->dev);
  505. }
  506. int can_restart_now(struct net_device *dev)
  507. {
  508. struct can_priv *priv = netdev_priv(dev);
  509. /*
  510. * A manual restart is only permitted if automatic restart is
  511. * disabled and the device is in the bus-off state
  512. */
  513. if (priv->restart_ms)
  514. return -EINVAL;
  515. if (priv->state != CAN_STATE_BUS_OFF)
  516. return -EBUSY;
  517. cancel_delayed_work_sync(&priv->restart_work);
  518. can_restart(dev);
  519. return 0;
  520. }
  521. /*
  522. * CAN bus-off
  523. *
  524. * This functions should be called when the device goes bus-off to
  525. * tell the netif layer that no more packets can be sent or received.
  526. * If enabled, a timer is started to trigger bus-off recovery.
  527. */
  528. void can_bus_off(struct net_device *dev)
  529. {
  530. struct can_priv *priv = netdev_priv(dev);
  531. netdev_info(dev, "bus-off\n");
  532. netif_carrier_off(dev);
  533. if (priv->restart_ms)
  534. schedule_delayed_work(&priv->restart_work,
  535. msecs_to_jiffies(priv->restart_ms));
  536. }
  537. EXPORT_SYMBOL_GPL(can_bus_off);
  538. static void can_setup(struct net_device *dev)
  539. {
  540. dev->type = ARPHRD_CAN;
  541. dev->mtu = CAN_MTU;
  542. dev->hard_header_len = 0;
  543. dev->addr_len = 0;
  544. dev->tx_queue_len = 10;
  545. /* New-style flags. */
  546. dev->flags = IFF_NOARP;
  547. dev->features = NETIF_F_HW_CSUM;
  548. }
  549. struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf)
  550. {
  551. struct sk_buff *skb;
  552. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  553. sizeof(struct can_frame));
  554. if (unlikely(!skb))
  555. return NULL;
  556. skb->protocol = htons(ETH_P_CAN);
  557. skb->pkt_type = PACKET_BROADCAST;
  558. skb->ip_summed = CHECKSUM_UNNECESSARY;
  559. skb_reset_mac_header(skb);
  560. skb_reset_network_header(skb);
  561. skb_reset_transport_header(skb);
  562. can_skb_reserve(skb);
  563. can_skb_prv(skb)->ifindex = dev->ifindex;
  564. can_skb_prv(skb)->skbcnt = 0;
  565. *cf = skb_put_zero(skb, sizeof(struct can_frame));
  566. return skb;
  567. }
  568. EXPORT_SYMBOL_GPL(alloc_can_skb);
  569. struct sk_buff *alloc_canfd_skb(struct net_device *dev,
  570. struct canfd_frame **cfd)
  571. {
  572. struct sk_buff *skb;
  573. skb = netdev_alloc_skb(dev, sizeof(struct can_skb_priv) +
  574. sizeof(struct canfd_frame));
  575. if (unlikely(!skb))
  576. return NULL;
  577. skb->protocol = htons(ETH_P_CANFD);
  578. skb->pkt_type = PACKET_BROADCAST;
  579. skb->ip_summed = CHECKSUM_UNNECESSARY;
  580. skb_reset_mac_header(skb);
  581. skb_reset_network_header(skb);
  582. skb_reset_transport_header(skb);
  583. can_skb_reserve(skb);
  584. can_skb_prv(skb)->ifindex = dev->ifindex;
  585. can_skb_prv(skb)->skbcnt = 0;
  586. *cfd = skb_put_zero(skb, sizeof(struct canfd_frame));
  587. return skb;
  588. }
  589. EXPORT_SYMBOL_GPL(alloc_canfd_skb);
  590. struct sk_buff *alloc_can_err_skb(struct net_device *dev, struct can_frame **cf)
  591. {
  592. struct sk_buff *skb;
  593. skb = alloc_can_skb(dev, cf);
  594. if (unlikely(!skb))
  595. return NULL;
  596. (*cf)->can_id = CAN_ERR_FLAG;
  597. (*cf)->can_dlc = CAN_ERR_DLC;
  598. return skb;
  599. }
  600. EXPORT_SYMBOL_GPL(alloc_can_err_skb);
  601. /*
  602. * Allocate and setup space for the CAN network device
  603. */
  604. struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
  605. unsigned int txqs, unsigned int rxqs)
  606. {
  607. struct net_device *dev;
  608. struct can_priv *priv;
  609. int size;
  610. if (echo_skb_max)
  611. size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
  612. echo_skb_max * sizeof(struct sk_buff *);
  613. else
  614. size = sizeof_priv;
  615. dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
  616. txqs, rxqs);
  617. if (!dev)
  618. return NULL;
  619. priv = netdev_priv(dev);
  620. priv->dev = dev;
  621. if (echo_skb_max) {
  622. priv->echo_skb_max = echo_skb_max;
  623. priv->echo_skb = (void *)priv +
  624. ALIGN(sizeof_priv, sizeof(struct sk_buff *));
  625. }
  626. priv->state = CAN_STATE_STOPPED;
  627. INIT_DELAYED_WORK(&priv->restart_work, can_restart_work);
  628. return dev;
  629. }
  630. EXPORT_SYMBOL_GPL(alloc_candev_mqs);
  631. /*
  632. * Free space of the CAN network device
  633. */
  634. void free_candev(struct net_device *dev)
  635. {
  636. free_netdev(dev);
  637. }
  638. EXPORT_SYMBOL_GPL(free_candev);
  639. /*
  640. * changing MTU and control mode for CAN/CANFD devices
  641. */
  642. int can_change_mtu(struct net_device *dev, int new_mtu)
  643. {
  644. struct can_priv *priv = netdev_priv(dev);
  645. /* Do not allow changing the MTU while running */
  646. if (dev->flags & IFF_UP)
  647. return -EBUSY;
  648. /* allow change of MTU according to the CANFD ability of the device */
  649. switch (new_mtu) {
  650. case CAN_MTU:
  651. /* 'CANFD-only' controllers can not switch to CAN_MTU */
  652. if (priv->ctrlmode_static & CAN_CTRLMODE_FD)
  653. return -EINVAL;
  654. priv->ctrlmode &= ~CAN_CTRLMODE_FD;
  655. break;
  656. case CANFD_MTU:
  657. /* check for potential CANFD ability */
  658. if (!(priv->ctrlmode_supported & CAN_CTRLMODE_FD) &&
  659. !(priv->ctrlmode_static & CAN_CTRLMODE_FD))
  660. return -EINVAL;
  661. priv->ctrlmode |= CAN_CTRLMODE_FD;
  662. break;
  663. default:
  664. return -EINVAL;
  665. }
  666. dev->mtu = new_mtu;
  667. return 0;
  668. }
  669. EXPORT_SYMBOL_GPL(can_change_mtu);
  670. /*
  671. * Common open function when the device gets opened.
  672. *
  673. * This function should be called in the open function of the device
  674. * driver.
  675. */
  676. int open_candev(struct net_device *dev)
  677. {
  678. struct can_priv *priv = netdev_priv(dev);
  679. if (!priv->bittiming.bitrate) {
  680. netdev_err(dev, "bit-timing not yet defined\n");
  681. return -EINVAL;
  682. }
  683. /* For CAN FD the data bitrate has to be >= the arbitration bitrate */
  684. if ((priv->ctrlmode & CAN_CTRLMODE_FD) &&
  685. (!priv->data_bittiming.bitrate ||
  686. (priv->data_bittiming.bitrate < priv->bittiming.bitrate))) {
  687. netdev_err(dev, "incorrect/missing data bit-timing\n");
  688. return -EINVAL;
  689. }
  690. /* Switch carrier on if device was stopped while in bus-off state */
  691. if (!netif_carrier_ok(dev))
  692. netif_carrier_on(dev);
  693. return 0;
  694. }
  695. EXPORT_SYMBOL_GPL(open_candev);
  696. #ifdef CONFIG_OF
  697. /* Common function that can be used to understand the limitation of
  698. * a transceiver when it provides no means to determine these limitations
  699. * at runtime.
  700. */
  701. void of_can_transceiver(struct net_device *dev)
  702. {
  703. struct device_node *dn;
  704. struct can_priv *priv = netdev_priv(dev);
  705. struct device_node *np = dev->dev.parent->of_node;
  706. int ret;
  707. dn = of_get_child_by_name(np, "can-transceiver");
  708. if (!dn)
  709. return;
  710. ret = of_property_read_u32(dn, "max-bitrate", &priv->bitrate_max);
  711. if ((ret && ret != -EINVAL) || (!ret && !priv->bitrate_max))
  712. netdev_warn(dev, "Invalid value for transceiver max bitrate. Ignoring bitrate limit.\n");
  713. }
  714. EXPORT_SYMBOL_GPL(of_can_transceiver);
  715. #endif
  716. /*
  717. * Common close function for cleanup before the device gets closed.
  718. *
  719. * This function should be called in the close function of the device
  720. * driver.
  721. */
  722. void close_candev(struct net_device *dev)
  723. {
  724. struct can_priv *priv = netdev_priv(dev);
  725. cancel_delayed_work_sync(&priv->restart_work);
  726. can_flush_echo_skb(dev);
  727. }
  728. EXPORT_SYMBOL_GPL(close_candev);
  729. /*
  730. * CAN netlink interface
  731. */
  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. };
  747. static int can_validate(struct nlattr *tb[], struct nlattr *data[],
  748. struct netlink_ext_ack *extack)
  749. {
  750. bool is_can_fd = false;
  751. /* Make sure that valid CAN FD configurations always consist of
  752. * - nominal/arbitration bittiming
  753. * - data bittiming
  754. * - control mode with CAN_CTRLMODE_FD set
  755. */
  756. if (!data)
  757. return 0;
  758. if (data[IFLA_CAN_CTRLMODE]) {
  759. struct can_ctrlmode *cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  760. is_can_fd = cm->flags & cm->mask & CAN_CTRLMODE_FD;
  761. }
  762. if (is_can_fd) {
  763. if (!data[IFLA_CAN_BITTIMING] || !data[IFLA_CAN_DATA_BITTIMING])
  764. return -EOPNOTSUPP;
  765. }
  766. if (data[IFLA_CAN_DATA_BITTIMING]) {
  767. if (!is_can_fd || !data[IFLA_CAN_BITTIMING])
  768. return -EOPNOTSUPP;
  769. }
  770. return 0;
  771. }
  772. static int can_changelink(struct net_device *dev, struct nlattr *tb[],
  773. struct nlattr *data[],
  774. struct netlink_ext_ack *extack)
  775. {
  776. struct can_priv *priv = netdev_priv(dev);
  777. int err;
  778. /* We need synchronization with dev->stop() */
  779. ASSERT_RTNL();
  780. if (data[IFLA_CAN_BITTIMING]) {
  781. struct can_bittiming bt;
  782. /* Do not allow changing bittiming while running */
  783. if (dev->flags & IFF_UP)
  784. return -EBUSY;
  785. /* Calculate bittiming parameters based on
  786. * bittiming_const if set, otherwise pass bitrate
  787. * directly via do_set_bitrate(). Bail out if neither
  788. * is given.
  789. */
  790. if (!priv->bittiming_const && !priv->do_set_bittiming)
  791. return -EOPNOTSUPP;
  792. memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
  793. err = can_get_bittiming(dev, &bt,
  794. priv->bittiming_const,
  795. priv->bitrate_const,
  796. priv->bitrate_const_cnt);
  797. if (err)
  798. return err;
  799. if (priv->bitrate_max && bt.bitrate > priv->bitrate_max) {
  800. netdev_err(dev, "arbitration bitrate surpasses transceiver capabilities of %d bps\n",
  801. priv->bitrate_max);
  802. return -EINVAL;
  803. }
  804. memcpy(&priv->bittiming, &bt, sizeof(bt));
  805. if (priv->do_set_bittiming) {
  806. /* Finally, set the bit-timing registers */
  807. err = priv->do_set_bittiming(dev);
  808. if (err)
  809. return err;
  810. }
  811. }
  812. if (data[IFLA_CAN_CTRLMODE]) {
  813. struct can_ctrlmode *cm;
  814. u32 ctrlstatic;
  815. u32 maskedflags;
  816. /* Do not allow changing controller mode while running */
  817. if (dev->flags & IFF_UP)
  818. return -EBUSY;
  819. cm = nla_data(data[IFLA_CAN_CTRLMODE]);
  820. ctrlstatic = priv->ctrlmode_static;
  821. maskedflags = cm->flags & cm->mask;
  822. /* check whether provided bits are allowed to be passed */
  823. if (cm->mask & ~(priv->ctrlmode_supported | ctrlstatic))
  824. return -EOPNOTSUPP;
  825. /* do not check for static fd-non-iso if 'fd' is disabled */
  826. if (!(maskedflags & CAN_CTRLMODE_FD))
  827. ctrlstatic &= ~CAN_CTRLMODE_FD_NON_ISO;
  828. /* make sure static options are provided by configuration */
  829. if ((maskedflags & ctrlstatic) != ctrlstatic)
  830. return -EOPNOTSUPP;
  831. /* clear bits to be modified and copy the flag values */
  832. priv->ctrlmode &= ~cm->mask;
  833. priv->ctrlmode |= maskedflags;
  834. /* CAN_CTRLMODE_FD can only be set when driver supports FD */
  835. if (priv->ctrlmode & CAN_CTRLMODE_FD)
  836. dev->mtu = CANFD_MTU;
  837. else
  838. dev->mtu = CAN_MTU;
  839. }
  840. if (data[IFLA_CAN_RESTART_MS]) {
  841. /* Do not allow changing restart delay while running */
  842. if (dev->flags & IFF_UP)
  843. return -EBUSY;
  844. priv->restart_ms = nla_get_u32(data[IFLA_CAN_RESTART_MS]);
  845. }
  846. if (data[IFLA_CAN_RESTART]) {
  847. /* Do not allow a restart while not running */
  848. if (!(dev->flags & IFF_UP))
  849. return -EINVAL;
  850. err = can_restart_now(dev);
  851. if (err)
  852. return err;
  853. }
  854. if (data[IFLA_CAN_DATA_BITTIMING]) {
  855. struct can_bittiming dbt;
  856. /* Do not allow changing bittiming while running */
  857. if (dev->flags & IFF_UP)
  858. return -EBUSY;
  859. /* Calculate bittiming parameters based on
  860. * data_bittiming_const if set, otherwise pass bitrate
  861. * directly via do_set_bitrate(). Bail out if neither
  862. * is given.
  863. */
  864. if (!priv->data_bittiming_const && !priv->do_set_data_bittiming)
  865. return -EOPNOTSUPP;
  866. memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
  867. sizeof(dbt));
  868. err = can_get_bittiming(dev, &dbt,
  869. priv->data_bittiming_const,
  870. priv->data_bitrate_const,
  871. priv->data_bitrate_const_cnt);
  872. if (err)
  873. return err;
  874. if (priv->bitrate_max && dbt.bitrate > priv->bitrate_max) {
  875. netdev_err(dev, "canfd data bitrate surpasses transceiver capabilities of %d bps\n",
  876. priv->bitrate_max);
  877. return -EINVAL;
  878. }
  879. memcpy(&priv->data_bittiming, &dbt, sizeof(dbt));
  880. if (priv->do_set_data_bittiming) {
  881. /* Finally, set the bit-timing registers */
  882. err = priv->do_set_data_bittiming(dev);
  883. if (err)
  884. return err;
  885. }
  886. }
  887. if (data[IFLA_CAN_TERMINATION]) {
  888. const u16 termval = nla_get_u16(data[IFLA_CAN_TERMINATION]);
  889. const unsigned int num_term = priv->termination_const_cnt;
  890. unsigned int i;
  891. if (!priv->do_set_termination)
  892. return -EOPNOTSUPP;
  893. /* check whether given value is supported by the interface */
  894. for (i = 0; i < num_term; i++) {
  895. if (termval == priv->termination_const[i])
  896. break;
  897. }
  898. if (i >= num_term)
  899. return -EINVAL;
  900. /* Finally, set the termination value */
  901. err = priv->do_set_termination(dev, termval);
  902. if (err)
  903. return err;
  904. priv->termination = termval;
  905. }
  906. return 0;
  907. }
  908. static size_t can_get_size(const struct net_device *dev)
  909. {
  910. struct can_priv *priv = netdev_priv(dev);
  911. size_t size = 0;
  912. if (priv->bittiming.bitrate) /* IFLA_CAN_BITTIMING */
  913. size += nla_total_size(sizeof(struct can_bittiming));
  914. if (priv->bittiming_const) /* IFLA_CAN_BITTIMING_CONST */
  915. size += nla_total_size(sizeof(struct can_bittiming_const));
  916. size += nla_total_size(sizeof(struct can_clock)); /* IFLA_CAN_CLOCK */
  917. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_STATE */
  918. size += nla_total_size(sizeof(struct can_ctrlmode)); /* IFLA_CAN_CTRLMODE */
  919. size += nla_total_size(sizeof(u32)); /* IFLA_CAN_RESTART_MS */
  920. if (priv->do_get_berr_counter) /* IFLA_CAN_BERR_COUNTER */
  921. size += nla_total_size(sizeof(struct can_berr_counter));
  922. if (priv->data_bittiming.bitrate) /* IFLA_CAN_DATA_BITTIMING */
  923. size += nla_total_size(sizeof(struct can_bittiming));
  924. if (priv->data_bittiming_const) /* IFLA_CAN_DATA_BITTIMING_CONST */
  925. size += nla_total_size(sizeof(struct can_bittiming_const));
  926. if (priv->termination_const) {
  927. size += nla_total_size(sizeof(priv->termination)); /* IFLA_CAN_TERMINATION */
  928. size += nla_total_size(sizeof(*priv->termination_const) * /* IFLA_CAN_TERMINATION_CONST */
  929. priv->termination_const_cnt);
  930. }
  931. if (priv->bitrate_const) /* IFLA_CAN_BITRATE_CONST */
  932. size += nla_total_size(sizeof(*priv->bitrate_const) *
  933. priv->bitrate_const_cnt);
  934. if (priv->data_bitrate_const) /* IFLA_CAN_DATA_BITRATE_CONST */
  935. size += nla_total_size(sizeof(*priv->data_bitrate_const) *
  936. priv->data_bitrate_const_cnt);
  937. size += sizeof(priv->bitrate_max); /* IFLA_CAN_BITRATE_MAX */
  938. return size;
  939. }
  940. static int can_fill_info(struct sk_buff *skb, const struct net_device *dev)
  941. {
  942. struct can_priv *priv = netdev_priv(dev);
  943. struct can_ctrlmode cm = {.flags = priv->ctrlmode};
  944. struct can_berr_counter bec;
  945. enum can_state state = priv->state;
  946. if (priv->do_get_state)
  947. priv->do_get_state(dev, &state);
  948. if ((priv->bittiming.bitrate &&
  949. nla_put(skb, IFLA_CAN_BITTIMING,
  950. sizeof(priv->bittiming), &priv->bittiming)) ||
  951. (priv->bittiming_const &&
  952. nla_put(skb, IFLA_CAN_BITTIMING_CONST,
  953. sizeof(*priv->bittiming_const), priv->bittiming_const)) ||
  954. nla_put(skb, IFLA_CAN_CLOCK, sizeof(priv->clock), &priv->clock) ||
  955. nla_put_u32(skb, IFLA_CAN_STATE, state) ||
  956. nla_put(skb, IFLA_CAN_CTRLMODE, sizeof(cm), &cm) ||
  957. nla_put_u32(skb, IFLA_CAN_RESTART_MS, priv->restart_ms) ||
  958. (priv->do_get_berr_counter &&
  959. !priv->do_get_berr_counter(dev, &bec) &&
  960. nla_put(skb, IFLA_CAN_BERR_COUNTER, sizeof(bec), &bec)) ||
  961. (priv->data_bittiming.bitrate &&
  962. nla_put(skb, IFLA_CAN_DATA_BITTIMING,
  963. sizeof(priv->data_bittiming), &priv->data_bittiming)) ||
  964. (priv->data_bittiming_const &&
  965. nla_put(skb, IFLA_CAN_DATA_BITTIMING_CONST,
  966. sizeof(*priv->data_bittiming_const),
  967. priv->data_bittiming_const)) ||
  968. (priv->termination_const &&
  969. (nla_put_u16(skb, IFLA_CAN_TERMINATION, priv->termination) ||
  970. nla_put(skb, IFLA_CAN_TERMINATION_CONST,
  971. sizeof(*priv->termination_const) *
  972. priv->termination_const_cnt,
  973. priv->termination_const))) ||
  974. (priv->bitrate_const &&
  975. nla_put(skb, IFLA_CAN_BITRATE_CONST,
  976. sizeof(*priv->bitrate_const) *
  977. priv->bitrate_const_cnt,
  978. priv->bitrate_const)) ||
  979. (priv->data_bitrate_const &&
  980. nla_put(skb, IFLA_CAN_DATA_BITRATE_CONST,
  981. sizeof(*priv->data_bitrate_const) *
  982. priv->data_bitrate_const_cnt,
  983. priv->data_bitrate_const)) ||
  984. (nla_put(skb, IFLA_CAN_BITRATE_MAX,
  985. sizeof(priv->bitrate_max),
  986. &priv->bitrate_max))
  987. )
  988. return -EMSGSIZE;
  989. return 0;
  990. }
  991. static size_t can_get_xstats_size(const struct net_device *dev)
  992. {
  993. return sizeof(struct can_device_stats);
  994. }
  995. static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
  996. {
  997. struct can_priv *priv = netdev_priv(dev);
  998. if (nla_put(skb, IFLA_INFO_XSTATS,
  999. sizeof(priv->can_stats), &priv->can_stats))
  1000. goto nla_put_failure;
  1001. return 0;
  1002. nla_put_failure:
  1003. return -EMSGSIZE;
  1004. }
  1005. static int can_newlink(struct net *src_net, struct net_device *dev,
  1006. struct nlattr *tb[], struct nlattr *data[],
  1007. struct netlink_ext_ack *extack)
  1008. {
  1009. return -EOPNOTSUPP;
  1010. }
  1011. static void can_dellink(struct net_device *dev, struct list_head *head)
  1012. {
  1013. return;
  1014. }
  1015. static struct rtnl_link_ops can_link_ops __read_mostly = {
  1016. .kind = "can",
  1017. .maxtype = IFLA_CAN_MAX,
  1018. .policy = can_policy,
  1019. .setup = can_setup,
  1020. .validate = can_validate,
  1021. .newlink = can_newlink,
  1022. .changelink = can_changelink,
  1023. .dellink = can_dellink,
  1024. .get_size = can_get_size,
  1025. .fill_info = can_fill_info,
  1026. .get_xstats_size = can_get_xstats_size,
  1027. .fill_xstats = can_fill_xstats,
  1028. };
  1029. /*
  1030. * Register the CAN network device
  1031. */
  1032. int register_candev(struct net_device *dev)
  1033. {
  1034. struct can_priv *priv = netdev_priv(dev);
  1035. /* Ensure termination_const, termination_const_cnt and
  1036. * do_set_termination consistency. All must be either set or
  1037. * unset.
  1038. */
  1039. if ((!priv->termination_const != !priv->termination_const_cnt) ||
  1040. (!priv->termination_const != !priv->do_set_termination))
  1041. return -EINVAL;
  1042. if (!priv->bitrate_const != !priv->bitrate_const_cnt)
  1043. return -EINVAL;
  1044. if (!priv->data_bitrate_const != !priv->data_bitrate_const_cnt)
  1045. return -EINVAL;
  1046. dev->rtnl_link_ops = &can_link_ops;
  1047. netif_carrier_off(dev);
  1048. return register_netdev(dev);
  1049. }
  1050. EXPORT_SYMBOL_GPL(register_candev);
  1051. /*
  1052. * Unregister the CAN network device
  1053. */
  1054. void unregister_candev(struct net_device *dev)
  1055. {
  1056. unregister_netdev(dev);
  1057. }
  1058. EXPORT_SYMBOL_GPL(unregister_candev);
  1059. /*
  1060. * Test if a network device is a candev based device
  1061. * and return the can_priv* if so.
  1062. */
  1063. struct can_priv *safe_candev_priv(struct net_device *dev)
  1064. {
  1065. if ((dev->type != ARPHRD_CAN) || (dev->rtnl_link_ops != &can_link_ops))
  1066. return NULL;
  1067. return netdev_priv(dev);
  1068. }
  1069. EXPORT_SYMBOL_GPL(safe_candev_priv);
  1070. static __init int can_dev_init(void)
  1071. {
  1072. int err;
  1073. can_led_notifier_init();
  1074. err = rtnl_link_register(&can_link_ops);
  1075. if (!err)
  1076. printk(KERN_INFO MOD_DESC "\n");
  1077. return err;
  1078. }
  1079. module_init(can_dev_init);
  1080. static __exit void can_dev_exit(void)
  1081. {
  1082. rtnl_link_unregister(&can_link_ops);
  1083. can_led_notifier_exit();
  1084. }
  1085. module_exit(can_dev_exit);
  1086. MODULE_ALIAS_RTNL_LINK("can");