fm10k_ptp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_queue_tail(list, clone);
  62. spin_unlock_irqrestore(&list->lock, flags);
  63. /* if list is already has one then we just free the clone */
  64. if (skb)
  65. kfree_skb(skb);
  66. else
  67. skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
  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 return it to the socket */
  86. fm10k_systime_to_hwtstamp(interface, &shhwtstamps, systime);
  87. skb_complete_tx_timestamp(skb, &shhwtstamps);
  88. }
  89. void fm10k_ts_tx_subtask(struct fm10k_intfc *interface)
  90. {
  91. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  92. struct sk_buff *skb, *tmp;
  93. unsigned long flags;
  94. /* If we're down or resetting, just bail */
  95. if (test_bit(__FM10K_DOWN, &interface->state) ||
  96. test_bit(__FM10K_RESETTING, &interface->state))
  97. return;
  98. spin_lock_irqsave(&list->lock, flags);
  99. /* walk though the list and flush any expired timestamp packets */
  100. skb_queue_walk_safe(list, skb, tmp) {
  101. if (!time_is_after_jiffies(FM10K_CB(skb)->ts_tx_timeout))
  102. continue;
  103. __skb_unlink(skb, list);
  104. kfree_skb(skb);
  105. interface->tx_hwtstamp_timeouts++;
  106. }
  107. spin_unlock_irqrestore(&list->lock, flags);
  108. }
  109. static u64 fm10k_systime_read(struct fm10k_intfc *interface)
  110. {
  111. struct fm10k_hw *hw = &interface->hw;
  112. return hw->mac.ops.read_systime(hw);
  113. }
  114. void fm10k_ts_reset(struct fm10k_intfc *interface)
  115. {
  116. s64 ns = ktime_to_ns(ktime_get_real());
  117. unsigned long flags;
  118. /* reinitialize the clock */
  119. write_lock_irqsave(&interface->systime_lock, flags);
  120. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  121. write_unlock_irqrestore(&interface->systime_lock, flags);
  122. }
  123. void fm10k_ts_init(struct fm10k_intfc *interface)
  124. {
  125. /* Initialize lock protecting systime access */
  126. rwlock_init(&interface->systime_lock);
  127. /* Initialize skb queue for pending timestamp requests */
  128. skb_queue_head_init(&interface->ts_tx_skb_queue);
  129. /* reset the clock to current kernel time */
  130. fm10k_ts_reset(interface);
  131. }
  132. /**
  133. * fm10k_get_ts_config - get current hardware timestamping configuration
  134. * @netdev: network interface device structure
  135. * @ifreq: ioctl data
  136. *
  137. * This function returns the current timestamping settings. Rather than
  138. * attempt to deconstruct registers to fill in the values, simply keep a copy
  139. * of the old settings around, and return a copy when requested.
  140. */
  141. int fm10k_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  142. {
  143. struct fm10k_intfc *interface = netdev_priv(netdev);
  144. struct hwtstamp_config *config = &interface->ts_config;
  145. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  146. -EFAULT : 0;
  147. }
  148. /**
  149. * fm10k_set_ts_config - control hardware time stamping
  150. * @netdev: network interface device structure
  151. * @ifreq: ioctl data
  152. *
  153. * Outgoing time stamping can be enabled and disabled. Play nice and
  154. * disable it when requested, although it shouldn't cause any overhead
  155. * when no packet needs it. At most one packet in the queue may be
  156. * marked for time stamping, otherwise it would be impossible to tell
  157. * for sure to which packet the hardware time stamp belongs.
  158. *
  159. * Incoming time stamping has to be configured via the hardware
  160. * filters. Not all combinations are supported, in particular event
  161. * type has to be specified. Matching the kind of event packet is
  162. * not supported, with the exception of "all V2 events regardless of
  163. * level 2 or 4".
  164. *
  165. * Since hardware always timestamps Path delay packets when timestamping V2
  166. * packets, regardless of the type specified in the register, only use V2
  167. * Event mode. This more accurately tells the user what the hardware is going
  168. * to do anyways.
  169. */
  170. int fm10k_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  171. {
  172. struct fm10k_intfc *interface = netdev_priv(netdev);
  173. struct hwtstamp_config ts_config;
  174. if (copy_from_user(&ts_config, ifr->ifr_data, sizeof(ts_config)))
  175. return -EFAULT;
  176. /* reserved for future extensions */
  177. if (ts_config.flags)
  178. return -EINVAL;
  179. switch (ts_config.tx_type) {
  180. case HWTSTAMP_TX_OFF:
  181. break;
  182. case HWTSTAMP_TX_ON:
  183. /* we likely need some check here to see if this is supported */
  184. break;
  185. default:
  186. return -ERANGE;
  187. }
  188. switch (ts_config.rx_filter) {
  189. case HWTSTAMP_FILTER_NONE:
  190. interface->flags &= ~FM10K_FLAG_RX_TS_ENABLED;
  191. break;
  192. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  193. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  194. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  195. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  196. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  197. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  198. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  199. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  200. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  201. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  202. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  203. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  204. case HWTSTAMP_FILTER_ALL:
  205. interface->flags |= FM10K_FLAG_RX_TS_ENABLED;
  206. ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
  207. break;
  208. default:
  209. return -ERANGE;
  210. }
  211. /* save these settings for future reference */
  212. interface->ts_config = ts_config;
  213. return copy_to_user(ifr->ifr_data, &ts_config, sizeof(ts_config)) ?
  214. -EFAULT : 0;
  215. }
  216. static int fm10k_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  217. {
  218. struct fm10k_intfc *interface;
  219. struct fm10k_hw *hw;
  220. int err;
  221. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  222. hw = &interface->hw;
  223. err = hw->mac.ops.adjust_systime(hw, ppb);
  224. /* the only error we should see is if the value is out of range */
  225. return (err == FM10K_ERR_PARAM) ? -ERANGE : err;
  226. }
  227. static int fm10k_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  228. {
  229. struct fm10k_intfc *interface;
  230. unsigned long flags;
  231. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  232. write_lock_irqsave(&interface->systime_lock, flags);
  233. interface->ptp_adjust += delta;
  234. write_unlock_irqrestore(&interface->systime_lock, flags);
  235. return 0;
  236. }
  237. static int fm10k_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  238. {
  239. struct fm10k_intfc *interface;
  240. unsigned long flags;
  241. u64 now;
  242. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  243. read_lock_irqsave(&interface->systime_lock, flags);
  244. now = fm10k_systime_read(interface) + interface->ptp_adjust;
  245. read_unlock_irqrestore(&interface->systime_lock, flags);
  246. *ts = ns_to_timespec64(now);
  247. return 0;
  248. }
  249. static int fm10k_ptp_settime(struct ptp_clock_info *ptp,
  250. const struct timespec64 *ts)
  251. {
  252. struct fm10k_intfc *interface;
  253. unsigned long flags;
  254. u64 ns = timespec64_to_ns(ts);
  255. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  256. write_lock_irqsave(&interface->systime_lock, flags);
  257. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  258. write_unlock_irqrestore(&interface->systime_lock, flags);
  259. return 0;
  260. }
  261. static int fm10k_ptp_enable(struct ptp_clock_info *ptp,
  262. struct ptp_clock_request *rq,
  263. int __always_unused on)
  264. {
  265. struct ptp_clock_time *t = &rq->perout.period;
  266. struct fm10k_intfc *interface;
  267. struct fm10k_hw *hw;
  268. u64 period;
  269. u32 step;
  270. /* we can only support periodic output */
  271. if (rq->type != PTP_CLK_REQ_PEROUT)
  272. return -EINVAL;
  273. /* verify the requested channel is there */
  274. if (rq->perout.index >= ptp->n_per_out)
  275. return -EINVAL;
  276. /* we cannot enforce start time as there is no
  277. * mechanism for that in the hardware, we can only control
  278. * the period.
  279. */
  280. /* we cannot support periods greater than 4 seconds due to reg limit */
  281. if (t->sec > 4 || t->sec < 0)
  282. return -ERANGE;
  283. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  284. hw = &interface->hw;
  285. /* we simply cannot support the operation if we don't have BAR4 */
  286. if (!hw->sw_addr)
  287. return -ENOTSUPP;
  288. /* convert to unsigned 64b ns, verify we can put it in a 32b register */
  289. period = t->sec * 1000000000LL + t->nsec;
  290. /* determine the minimum size for period */
  291. step = 2 * (fm10k_read_reg(hw, FM10K_SYSTIME_CFG) &
  292. FM10K_SYSTIME_CFG_STEP_MASK);
  293. /* verify the value is in range supported by hardware */
  294. if ((period && (period < step)) || (period > U32_MAX))
  295. return -ERANGE;
  296. /* notify hardware of request to being sending pulses */
  297. fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_PULSE(rq->perout.index),
  298. (u32)period);
  299. return 0;
  300. }
  301. static struct ptp_pin_desc fm10k_ptp_pd[2] = {
  302. {
  303. .name = "IEEE1588_PULSE0",
  304. .index = 0,
  305. .func = PTP_PF_PEROUT,
  306. .chan = 0
  307. },
  308. {
  309. .name = "IEEE1588_PULSE1",
  310. .index = 1,
  311. .func = PTP_PF_PEROUT,
  312. .chan = 1
  313. }
  314. };
  315. static int fm10k_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
  316. enum ptp_pin_function func, unsigned int chan)
  317. {
  318. /* verify the requested pin is there */
  319. if (pin >= ptp->n_pins || !ptp->pin_config)
  320. return -EINVAL;
  321. /* enforce locked channels, no changing them */
  322. if (chan != ptp->pin_config[pin].chan)
  323. return -EINVAL;
  324. /* we want to keep the functions locked as well */
  325. if (func != ptp->pin_config[pin].func)
  326. return -EINVAL;
  327. return 0;
  328. }
  329. void fm10k_ptp_register(struct fm10k_intfc *interface)
  330. {
  331. struct ptp_clock_info *ptp_caps = &interface->ptp_caps;
  332. struct device *dev = &interface->pdev->dev;
  333. struct ptp_clock *ptp_clock;
  334. snprintf(ptp_caps->name, sizeof(ptp_caps->name),
  335. "%s", interface->netdev->name);
  336. ptp_caps->owner = THIS_MODULE;
  337. /* This math is simply the inverse of the math in
  338. * fm10k_adjust_systime_pf applied to an adjustment value
  339. * of 2^30 - 1 which is the maximum value of the register:
  340. * max_ppb == ((2^30 - 1) * 5^9) / 2^31
  341. */
  342. ptp_caps->max_adj = 976562;
  343. ptp_caps->adjfreq = fm10k_ptp_adjfreq;
  344. ptp_caps->adjtime = fm10k_ptp_adjtime;
  345. ptp_caps->gettime64 = fm10k_ptp_gettime;
  346. ptp_caps->settime64 = fm10k_ptp_settime;
  347. /* provide pins if BAR4 is accessible */
  348. if (interface->sw_addr) {
  349. /* enable periodic outputs */
  350. ptp_caps->n_per_out = 2;
  351. ptp_caps->enable = fm10k_ptp_enable;
  352. /* enable clock pins */
  353. ptp_caps->verify = fm10k_ptp_verify;
  354. ptp_caps->n_pins = 2;
  355. ptp_caps->pin_config = fm10k_ptp_pd;
  356. }
  357. ptp_clock = ptp_clock_register(ptp_caps, dev);
  358. if (IS_ERR(ptp_clock)) {
  359. ptp_clock = NULL;
  360. dev_err(dev, "ptp_clock_register failed\n");
  361. } else {
  362. dev_info(dev, "registered PHC device %s\n", ptp_caps->name);
  363. }
  364. interface->ptp_clock = ptp_clock;
  365. }
  366. void fm10k_ptp_unregister(struct fm10k_intfc *interface)
  367. {
  368. struct ptp_clock *ptp_clock = interface->ptp_clock;
  369. struct device *dev = &interface->pdev->dev;
  370. if (!ptp_clock)
  371. return;
  372. interface->ptp_clock = NULL;
  373. ptp_clock_unregister(ptp_clock);
  374. dev_info(dev, "removed PHC %s\n", interface->ptp_caps.name);
  375. }