dev.c 29 KB

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