fm10k_ptp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2014 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. __le16 dglort;
  51. /* create clone for us to return on the Tx path */
  52. clone = skb_clone_sk(skb);
  53. if (!clone)
  54. return;
  55. FM10K_CB(clone)->ts_tx_timeout = jiffies + FM10K_TS_TX_TIMEOUT;
  56. dglort = FM10K_CB(clone)->fi.w.dglort;
  57. spin_lock_irqsave(&list->lock, flags);
  58. /* attempt to locate any buffers with the same dglort,
  59. * if none are present then insert skb in tail of list
  60. */
  61. skb = fm10k_ts_tx_skb(interface, FM10K_CB(clone)->fi.w.dglort);
  62. if (!skb)
  63. __skb_queue_tail(list, clone);
  64. spin_unlock_irqrestore(&list->lock, flags);
  65. /* if list is already has one then we just free the clone */
  66. if (skb)
  67. kfree_skb(skb);
  68. else
  69. skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
  70. }
  71. void fm10k_ts_tx_hwtstamp(struct fm10k_intfc *interface, __le16 dglort,
  72. u64 systime)
  73. {
  74. struct skb_shared_hwtstamps shhwtstamps;
  75. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  76. struct sk_buff *skb;
  77. unsigned long flags;
  78. spin_lock_irqsave(&list->lock, flags);
  79. /* attempt to locate and pull the sk_buff out of the list */
  80. skb = fm10k_ts_tx_skb(interface, dglort);
  81. if (skb)
  82. __skb_unlink(skb, list);
  83. spin_unlock_irqrestore(&list->lock, flags);
  84. /* if not found do nothing */
  85. if (!skb)
  86. return;
  87. /* timestamp the sk_buff and return it to the socket */
  88. fm10k_systime_to_hwtstamp(interface, &shhwtstamps, systime);
  89. skb_complete_tx_timestamp(skb, &shhwtstamps);
  90. }
  91. void fm10k_ts_tx_subtask(struct fm10k_intfc *interface)
  92. {
  93. struct sk_buff_head *list = &interface->ts_tx_skb_queue;
  94. struct sk_buff *skb, *tmp;
  95. unsigned long flags;
  96. /* If we're down or resetting, just bail */
  97. if (test_bit(__FM10K_DOWN, &interface->state) ||
  98. test_bit(__FM10K_RESETTING, &interface->state))
  99. return;
  100. spin_lock_irqsave(&list->lock, flags);
  101. /* walk though the list and flush any expired timestamp packets */
  102. skb_queue_walk_safe(list, skb, tmp) {
  103. if (!time_is_after_jiffies(FM10K_CB(skb)->ts_tx_timeout))
  104. continue;
  105. __skb_unlink(skb, list);
  106. kfree_skb(skb);
  107. interface->tx_hwtstamp_timeouts++;
  108. }
  109. spin_unlock_irqrestore(&list->lock, flags);
  110. }
  111. static u64 fm10k_systime_read(struct fm10k_intfc *interface)
  112. {
  113. struct fm10k_hw *hw = &interface->hw;
  114. return hw->mac.ops.read_systime(hw);
  115. }
  116. void fm10k_ts_reset(struct fm10k_intfc *interface)
  117. {
  118. s64 ns = ktime_to_ns(ktime_get_real());
  119. unsigned long flags;
  120. /* reinitialize the clock */
  121. write_lock_irqsave(&interface->systime_lock, flags);
  122. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  123. write_unlock_irqrestore(&interface->systime_lock, flags);
  124. }
  125. void fm10k_ts_init(struct fm10k_intfc *interface)
  126. {
  127. /* Initialize lock protecting systime access */
  128. rwlock_init(&interface->systime_lock);
  129. /* Initialize skb queue for pending timestamp requests */
  130. skb_queue_head_init(&interface->ts_tx_skb_queue);
  131. /* reset the clock to current kernel time */
  132. fm10k_ts_reset(interface);
  133. }
  134. /**
  135. * fm10k_get_ts_config - get current hardware timestamping configuration
  136. * @netdev: network interface device structure
  137. * @ifreq: ioctl data
  138. *
  139. * This function returns the current timestamping settings. Rather than
  140. * attempt to deconstruct registers to fill in the values, simply keep a copy
  141. * of the old settings around, and return a copy when requested.
  142. */
  143. int fm10k_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  144. {
  145. struct fm10k_intfc *interface = netdev_priv(netdev);
  146. struct hwtstamp_config *config = &interface->ts_config;
  147. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  148. -EFAULT : 0;
  149. }
  150. /**
  151. * fm10k_set_ts_config - control hardware time stamping
  152. * @netdev: network interface device structure
  153. * @ifreq: ioctl data
  154. *
  155. * Outgoing time stamping can be enabled and disabled. Play nice and
  156. * disable it when requested, although it shouldn't cause any overhead
  157. * when no packet needs it. At most one packet in the queue may be
  158. * marked for time stamping, otherwise it would be impossible to tell
  159. * for sure to which packet the hardware time stamp belongs.
  160. *
  161. * Incoming time stamping has to be configured via the hardware
  162. * filters. Not all combinations are supported, in particular event
  163. * type has to be specified. Matching the kind of event packet is
  164. * not supported, with the exception of "all V2 events regardless of
  165. * level 2 or 4".
  166. *
  167. * Since hardware always timestamps Path delay packets when timestamping V2
  168. * packets, regardless of the type specified in the register, only use V2
  169. * Event mode. This more accurately tells the user what the hardware is going
  170. * to do anyways.
  171. */
  172. int fm10k_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  173. {
  174. struct fm10k_intfc *interface = netdev_priv(netdev);
  175. struct hwtstamp_config ts_config;
  176. if (copy_from_user(&ts_config, ifr->ifr_data, sizeof(ts_config)))
  177. return -EFAULT;
  178. /* reserved for future extensions */
  179. if (ts_config.flags)
  180. return -EINVAL;
  181. switch (ts_config.tx_type) {
  182. case HWTSTAMP_TX_OFF:
  183. break;
  184. case HWTSTAMP_TX_ON:
  185. /* we likely need some check here to see if this is supported */
  186. break;
  187. default:
  188. return -ERANGE;
  189. }
  190. switch (ts_config.rx_filter) {
  191. case HWTSTAMP_FILTER_NONE:
  192. interface->flags &= ~FM10K_FLAG_RX_TS_ENABLED;
  193. break;
  194. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  195. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  196. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  197. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  198. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  199. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  200. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  201. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  202. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  203. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  204. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  205. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  206. case HWTSTAMP_FILTER_ALL:
  207. interface->flags |= FM10K_FLAG_RX_TS_ENABLED;
  208. ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
  209. break;
  210. default:
  211. return -ERANGE;
  212. }
  213. /* save these settings for future reference */
  214. interface->ts_config = ts_config;
  215. return copy_to_user(ifr->ifr_data, &ts_config, sizeof(ts_config)) ?
  216. -EFAULT : 0;
  217. }
  218. static int fm10k_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  219. {
  220. struct fm10k_intfc *interface;
  221. struct fm10k_hw *hw;
  222. int err;
  223. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  224. hw = &interface->hw;
  225. err = hw->mac.ops.adjust_systime(hw, ppb);
  226. /* the only error we should see is if the value is out of range */
  227. return (err == FM10K_ERR_PARAM) ? -ERANGE : err;
  228. }
  229. static int fm10k_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  230. {
  231. struct fm10k_intfc *interface;
  232. unsigned long flags;
  233. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  234. write_lock_irqsave(&interface->systime_lock, flags);
  235. interface->ptp_adjust += delta;
  236. write_unlock_irqrestore(&interface->systime_lock, flags);
  237. return 0;
  238. }
  239. static int fm10k_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  240. {
  241. struct fm10k_intfc *interface;
  242. unsigned long flags;
  243. u64 now;
  244. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  245. read_lock_irqsave(&interface->systime_lock, flags);
  246. now = fm10k_systime_read(interface) + interface->ptp_adjust;
  247. read_unlock_irqrestore(&interface->systime_lock, flags);
  248. *ts = ns_to_timespec(now);
  249. return 0;
  250. }
  251. static int fm10k_ptp_settime(struct ptp_clock_info *ptp,
  252. const struct timespec *ts)
  253. {
  254. struct fm10k_intfc *interface;
  255. unsigned long flags;
  256. u64 ns = timespec_to_ns(ts);
  257. interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
  258. write_lock_irqsave(&interface->systime_lock, flags);
  259. interface->ptp_adjust = fm10k_systime_read(interface) - ns;
  260. write_unlock_irqrestore(&interface->systime_lock, flags);
  261. return 0;
  262. }
  263. static int fm10k_ptp_enable(struct ptp_clock_info *ptp,
  264. struct ptp_clock_request *rq, int 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->gettime = fm10k_ptp_gettime;
  347. ptp_caps->settime = 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. }