fm10k_ptp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2015 Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * The full GNU General Public License is included in this distribution in
  14. * the file called "COPYING".
  15. *
  16. * Contact Information:
  17. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. */
  20. #include <linux/ptp_classify.h>
  21. #include <linux/ptp_clock_kernel.h>
  22. #include "fm10k.h"
  23. #define FM10K_TS_TX_TIMEOUT (HZ * 15)
  24. void fm10k_systime_to_hwtstamp(struct fm10k_intfc *interface,
  25. struct skb_shared_hwtstamps *hwtstamp,
  26. u64 systime)
  27. {
  28. unsigned long flags;
  29. read_lock_irqsave(&interface->systime_lock, flags);
  30. systime += interface->ptp_adjust;
  31. read_unlock_irqrestore(&interface->systime_lock, flags);
  32. hwtstamp->hwtstamp = ns_to_ktime(systime);
  33. }
  34. static struct sk_buff *fm10k_ts_tx_skb(struct fm10k_intfc *interface,
  35. __le16 dglort)
  36. {
  37. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  38. struct sk_buff *skb;
  39. skb_queue_walk(list, skb) {
  40. if (FM10K_CB(skb)->fi.w.dglort == dglort)
  41. return skb;
  42. }
  43. return NULL;
  44. }
  45. void fm10k_ts_tx_enqueue(struct fm10k_intfc *interface, struct sk_buff *skb)
  46. {
  47. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  48. struct sk_buff *clone;
  49. unsigned long flags;
  50. /* create clone for us to return on the Tx path */
  51. clone = skb_clone_sk(skb);
  52. if (!clone)
  53. return;
  54. FM10K_CB(clone)->ts_tx_timeout = jiffies + FM10K_TS_TX_TIMEOUT;
  55. spin_lock_irqsave(&list->lock, flags);
  56. /* attempt to locate any buffers with the same dglort,
  57. * if none are present then insert skb in tail of list
  58. */
  59. skb = fm10k_ts_tx_skb(interface, FM10K_CB(clone)->fi.w.dglort);
  60. if (!skb) {
  61. skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
  62. __skb_queue_tail(list, clone);
  63. }
  64. spin_unlock_irqrestore(&list->lock, flags);
  65. /* if list is already has one then we just free the clone */
  66. if (skb)
  67. dev_kfree_skb(clone);
  68. }
  69. void fm10k_ts_tx_hwtstamp(struct fm10k_intfc *interface, __le16 dglort,
  70. u64 systime)
  71. {
  72. struct skb_shared_hwtstamps shhwtstamps;
  73. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  74. struct sk_buff *skb;
  75. unsigned long flags;
  76. spin_lock_irqsave(&list->lock, flags);
  77. /* attempt to locate and pull the sk_buff out of the list */
  78. skb = fm10k_ts_tx_skb(interface, dglort);
  79. if (skb)
  80. __skb_unlink(skb, list);
  81. spin_unlock_irqrestore(&list->lock, flags);
  82. /* if not found do nothing */
  83. if (!skb)
  84. return;
  85. /* timestamp the sk_buff and free out copy */
  86. fm10k_systime_to_hwtstamp(interface, &shhwtstamps, systime);
  87. skb_tstamp_tx(skb, &shhwtstamps);
  88. dev_kfree_skb_any(skb);
  89. }
  90. void fm10k_ts_tx_subtask(struct fm10k_intfc *interface)
  91. {
  92. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  93. struct sk_buff *skb, *tmp;
  94. unsigned long flags;
  95. /* If we're down or resetting, just bail */
  96. if (test_bit(__FM10K_DOWN, &interface->state) ||
  97. test_bit(__FM10K_RESETTING, &interface->state))
  98. return;
  99. spin_lock_irqsave(&list->lock, flags);
  100. /* walk though the list and flush any expired timestamp packets */
  101. skb_queue_walk_safe(list, skb, tmp) {
  102. if (!time_is_after_jiffies(FM10K_CB(skb)->ts_tx_timeout))
  103. continue;
  104. __skb_unlink(skb, list);
  105. kfree_skb(skb);
  106. interface->tx_hwtstamp_timeouts++;
  107. }
  108. spin_unlock_irqrestore(&list->lock, flags);
  109. }
  110. static u64 fm10k_systime_read(struct fm10k_intfc *interface)
  111. {
  112. struct fm10k_hw *hw = &interface->hw;
  113. return hw->mac.ops.read_systime(hw);
  114. }
  115. void fm10k_ts_reset(struct fm10k_intfc *interface)
  116. {
  117. s64 ns = ktime_to_ns(ktime_get_real());
  118. unsigned long flags;
  119. /* reinitialize the clock */
  120. write_lock_irqsave(&interface->systime_lock, flags);
  121. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  122. write_unlock_irqrestore(&interface->systime_lock, flags);
  123. }
  124. void fm10k_ts_init(struct fm10k_intfc *interface)
  125. {
  126. /* Initialize lock protecting systime access */
  127. rwlock_init(&interface->systime_lock);
  128. /* Initialize skb queue for pending timestamp requests */
  129. skb_queue_head_init(&interface->ts_tx_skb_queue);
  130. /* reset the clock to current kernel time */
  131. fm10k_ts_reset(interface);
  132. }
  133. /**
  134. * fm10k_get_ts_config - get current hardware timestamping configuration
  135. * @netdev: network interface device structure
  136. * @ifreq: ioctl data
  137. *
  138. * This function returns the current timestamping settings. Rather than
  139. * attempt to deconstruct registers to fill in the values, simply keep a copy
  140. * of the old settings around, and return a copy when requested.
  141. */
  142. int fm10k_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  143. {
  144. struct fm10k_intfc *interface = netdev_priv(netdev);
  145. struct hwtstamp_config *config = &interface->ts_config;
  146. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  147. -EFAULT : 0;
  148. }
  149. /**
  150. * fm10k_set_ts_config - control hardware time stamping
  151. * @netdev: network interface device structure
  152. * @ifreq: ioctl data
  153. *
  154. * Outgoing time stamping can be enabled and disabled. Play nice and
  155. * disable it when requested, although it shouldn't cause any overhead
  156. * when no packet needs it. At most one packet in the queue may be
  157. * marked for time stamping, otherwise it would be impossible to tell
  158. * for sure to which packet the hardware time stamp belongs.
  159. *
  160. * Incoming time stamping has to be configured via the hardware
  161. * filters. Not all combinations are supported, in particular event
  162. * type has to be specified. Matching the kind of event packet is
  163. * not supported, with the exception of "all V2 events regardless of
  164. * level 2 or 4".
  165. *
  166. * Since hardware always timestamps Path delay packets when timestamping V2
  167. * packets, regardless of the type specified in the register, only use V2
  168. * Event mode. This more accurately tells the user what the hardware is going
  169. * to do anyways.
  170. */
  171. int fm10k_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  172. {
  173. struct fm10k_intfc *interface = netdev_priv(netdev);
  174. struct hwtstamp_config ts_config;
  175. if (copy_from_user(&ts_config, ifr->ifr_data, sizeof(ts_config)))
  176. return -EFAULT;
  177. /* reserved for future extensions */
  178. if (ts_config.flags)
  179. return -EINVAL;
  180. switch (ts_config.tx_type) {
  181. case HWTSTAMP_TX_OFF:
  182. break;
  183. case HWTSTAMP_TX_ON:
  184. /* we likely need some check here to see if this is supported */
  185. break;
  186. default:
  187. return -ERANGE;
  188. }
  189. switch (ts_config.rx_filter) {
  190. case HWTSTAMP_FILTER_NONE:
  191. interface->flags &= ~FM10K_FLAG_RX_TS_ENABLED;
  192. break;
  193. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  194. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  195. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  196. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  197. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  198. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  199. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  200. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  201. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  202. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  203. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  204. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  205. case HWTSTAMP_FILTER_ALL:
  206. interface->flags |= FM10K_FLAG_RX_TS_ENABLED;
  207. ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
  208. break;
  209. default:
  210. return -ERANGE;
  211. }
  212. /* save these settings for future reference */
  213. interface->ts_config = ts_config;
  214. return copy_to_user(ifr->ifr_data, &ts_config, sizeof(ts_config)) ?
  215. -EFAULT : 0;
  216. }
  217. static int fm10k_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  218. {
  219. struct fm10k_intfc *interface;
  220. struct fm10k_hw *hw;
  221. int err;
  222. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  223. hw = &interface->hw;
  224. err = hw->mac.ops.adjust_systime(hw, ppb);
  225. /* the only error we should see is if the value is out of range */
  226. return (err == FM10K_ERR_PARAM) ? -ERANGE : err;
  227. }
  228. static int fm10k_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  229. {
  230. struct fm10k_intfc *interface;
  231. unsigned long flags;
  232. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  233. write_lock_irqsave(&interface->systime_lock, flags);
  234. interface->ptp_adjust += delta;
  235. write_unlock_irqrestore(&interface->systime_lock, flags);
  236. return 0;
  237. }
  238. static int fm10k_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  239. {
  240. struct fm10k_intfc *interface;
  241. unsigned long flags;
  242. u64 now;
  243. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  244. read_lock_irqsave(&interface->systime_lock, flags);
  245. now = fm10k_systime_read(interface) + interface->ptp_adjust;
  246. read_unlock_irqrestore(&interface->systime_lock, flags);
  247. *ts = ns_to_timespec64(now);
  248. return 0;
  249. }
  250. static int fm10k_ptp_settime(struct ptp_clock_info *ptp,
  251. const struct timespec64 *ts)
  252. {
  253. struct fm10k_intfc *interface;
  254. unsigned long flags;
  255. u64 ns = timespec64_to_ns(ts);
  256. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  257. write_lock_irqsave(&interface->systime_lock, flags);
  258. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  259. write_unlock_irqrestore(&interface->systime_lock, flags);
  260. return 0;
  261. }
  262. static int fm10k_ptp_enable(struct ptp_clock_info *ptp,
  263. struct ptp_clock_request *rq,
  264. int __always_unused on)
  265. {
  266. struct ptp_clock_time *t = &rq->perout.period;
  267. struct fm10k_intfc *interface;
  268. struct fm10k_hw *hw;
  269. u64 period;
  270. u32 step;
  271. /* we can only support periodic output */
  272. if (rq->type != PTP_CLK_REQ_PEROUT)
  273. return -EINVAL;
  274. /* verify the requested channel is there */
  275. if (rq->perout.index >= ptp->n_per_out)
  276. return -EINVAL;
  277. /* we cannot enforce start time as there is no
  278. * mechanism for that in the hardware, we can only control
  279. * the period.
  280. */
  281. /* we cannot support periods greater than 4 seconds due to reg limit */
  282. if (t->sec > 4 || t->sec < 0)
  283. return -ERANGE;
  284. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  285. hw = &interface->hw;
  286. /* we simply cannot support the operation if we don't have BAR4 */
  287. if (!hw->sw_addr)
  288. return -ENOTSUPP;
  289. /* convert to unsigned 64b ns, verify we can put it in a 32b register */
  290. period = t->sec * 1000000000LL + t->nsec;
  291. /* determine the minimum size for period */
  292. step = 2 * (fm10k_read_reg(hw, FM10K_SYSTIME_CFG) &
  293. FM10K_SYSTIME_CFG_STEP_MASK);
  294. /* verify the value is in range supported by hardware */
  295. if ((period && (period < step)) || (period > U32_MAX))
  296. return -ERANGE;
  297. /* notify hardware of request to being sending pulses */
  298. fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_PULSE(rq->perout.index),
  299. (u32)period);
  300. return 0;
  301. }
  302. static struct ptp_pin_desc fm10k_ptp_pd[2] = {
  303. {
  304. .name = "IEEE1588_PULSE0",
  305. .index = 0,
  306. .func = PTP_PF_PEROUT,
  307. .chan = 0
  308. },
  309. {
  310. .name = "IEEE1588_PULSE1",
  311. .index = 1,
  312. .func = PTP_PF_PEROUT,
  313. .chan = 1
  314. }
  315. };
  316. static int fm10k_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
  317. enum ptp_pin_function func, unsigned int chan)
  318. {
  319. /* verify the requested pin is there */
  320. if (pin >= ptp->n_pins || !ptp->pin_config)
  321. return -EINVAL;
  322. /* enforce locked channels, no changing them */
  323. if (chan != ptp->pin_config[pin].chan)
  324. return -EINVAL;
  325. /* we want to keep the functions locked as well */
  326. if (func != ptp->pin_config[pin].func)
  327. return -EINVAL;
  328. return 0;
  329. }
  330. void fm10k_ptp_register(struct fm10k_intfc *interface)
  331. {
  332. struct ptp_clock_info *ptp_caps = &interface->ptp_caps;
  333. struct device *dev = &interface->pdev->dev;
  334. struct ptp_clock *ptp_clock;
  335. snprintf(ptp_caps->name, sizeof(ptp_caps->name),
  336. "%s", interface->netdev->name);
  337. ptp_caps->owner = THIS_MODULE;
  338. /* This math is simply the inverse of the math in
  339. * fm10k_adjust_systime_pf applied to an adjustment value
  340. * of 2^30 - 1 which is the maximum value of the register:
  341. * max_ppb == ((2^30 - 1) * 5^9) / 2^31
  342. */
  343. ptp_caps->max_adj = 976562;
  344. ptp_caps->adjfreq = fm10k_ptp_adjfreq;
  345. ptp_caps->adjtime = fm10k_ptp_adjtime;
  346. ptp_caps->gettime64 = fm10k_ptp_gettime;
  347. ptp_caps->settime64 = fm10k_ptp_settime;
  348. /* provide pins if BAR4 is accessible */
  349. if (interface->sw_addr) {
  350. /* enable periodic outputs */
  351. ptp_caps->n_per_out = 2;
  352. ptp_caps->enable = fm10k_ptp_enable;
  353. /* enable clock pins */
  354. ptp_caps->verify = fm10k_ptp_verify;
  355. ptp_caps->n_pins = 2;
  356. ptp_caps->pin_config = fm10k_ptp_pd;
  357. }
  358. ptp_clock = ptp_clock_register(ptp_caps, dev);
  359. if (IS_ERR(ptp_clock)) {
  360. ptp_clock = NULL;
  361. dev_err(dev, "ptp_clock_register failed\n");
  362. } else {
  363. dev_info(dev, "registered PHC device %s\n", ptp_caps->name);
  364. }
  365. interface->ptp_clock = ptp_clock;
  366. }
  367. void fm10k_ptp_unregister(struct fm10k_intfc *interface)
  368. {
  369. struct ptp_clock *ptp_clock = interface->ptp_clock;
  370. struct device *dev = &interface->pdev->dev;
  371. if (!ptp_clock)
  372. return;
  373. interface->ptp_clock = NULL;
  374. ptp_clock_unregister(ptp_clock);
  375. dev_info(dev, "removed PHC %s\n", interface->ptp_caps.name);
  376. }