qede_ptp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* QLogic qede NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "qede_ptp.h"
  33. struct qede_ptp {
  34. const struct qed_eth_ptp_ops *ops;
  35. struct ptp_clock_info clock_info;
  36. struct cyclecounter cc;
  37. struct timecounter tc;
  38. struct ptp_clock *clock;
  39. struct work_struct work;
  40. struct qede_dev *edev;
  41. struct sk_buff *tx_skb;
  42. /* ptp spinlock is used for protecting the cycle/time counter fields
  43. * and, also for serializing the qed PTP API invocations.
  44. */
  45. spinlock_t lock;
  46. bool hw_ts_ioctl_called;
  47. u16 tx_type;
  48. u16 rx_filter;
  49. };
  50. /**
  51. * qede_ptp_adjfreq
  52. * @ptp: the ptp clock structure
  53. * @ppb: parts per billion adjustment from base
  54. *
  55. * Adjust the frequency of the ptp cycle counter by the
  56. * indicated ppb from the base frequency.
  57. */
  58. static int qede_ptp_adjfreq(struct ptp_clock_info *info, s32 ppb)
  59. {
  60. struct qede_ptp *ptp = container_of(info, struct qede_ptp, clock_info);
  61. struct qede_dev *edev = ptp->edev;
  62. int rc;
  63. __qede_lock(edev);
  64. if (edev->state == QEDE_STATE_OPEN) {
  65. spin_lock_bh(&ptp->lock);
  66. rc = ptp->ops->adjfreq(edev->cdev, ppb);
  67. spin_unlock_bh(&ptp->lock);
  68. } else {
  69. DP_ERR(edev, "PTP adjfreq called while interface is down\n");
  70. rc = -EFAULT;
  71. }
  72. __qede_unlock(edev);
  73. return rc;
  74. }
  75. static int qede_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
  76. {
  77. struct qede_dev *edev;
  78. struct qede_ptp *ptp;
  79. ptp = container_of(info, struct qede_ptp, clock_info);
  80. edev = ptp->edev;
  81. DP_VERBOSE(edev, QED_MSG_DEBUG, "PTP adjtime called, delta = %llx\n",
  82. delta);
  83. spin_lock_bh(&ptp->lock);
  84. timecounter_adjtime(&ptp->tc, delta);
  85. spin_unlock_bh(&ptp->lock);
  86. return 0;
  87. }
  88. static int qede_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
  89. {
  90. struct qede_dev *edev;
  91. struct qede_ptp *ptp;
  92. u64 ns;
  93. ptp = container_of(info, struct qede_ptp, clock_info);
  94. edev = ptp->edev;
  95. spin_lock_bh(&ptp->lock);
  96. ns = timecounter_read(&ptp->tc);
  97. spin_unlock_bh(&ptp->lock);
  98. DP_VERBOSE(edev, QED_MSG_DEBUG, "PTP gettime called, ns = %llu\n", ns);
  99. *ts = ns_to_timespec64(ns);
  100. return 0;
  101. }
  102. static int qede_ptp_settime(struct ptp_clock_info *info,
  103. const struct timespec64 *ts)
  104. {
  105. struct qede_dev *edev;
  106. struct qede_ptp *ptp;
  107. u64 ns;
  108. ptp = container_of(info, struct qede_ptp, clock_info);
  109. edev = ptp->edev;
  110. ns = timespec64_to_ns(ts);
  111. DP_VERBOSE(edev, QED_MSG_DEBUG, "PTP settime called, ns = %llu\n", ns);
  112. /* Re-init the timecounter */
  113. spin_lock_bh(&ptp->lock);
  114. timecounter_init(&ptp->tc, &ptp->cc, ns);
  115. spin_unlock_bh(&ptp->lock);
  116. return 0;
  117. }
  118. /* Enable (or disable) ancillary features of the phc subsystem */
  119. static int qede_ptp_ancillary_feature_enable(struct ptp_clock_info *info,
  120. struct ptp_clock_request *rq,
  121. int on)
  122. {
  123. struct qede_dev *edev;
  124. struct qede_ptp *ptp;
  125. ptp = container_of(info, struct qede_ptp, clock_info);
  126. edev = ptp->edev;
  127. DP_ERR(edev, "PHC ancillary features are not supported\n");
  128. return -ENOTSUPP;
  129. }
  130. static void qede_ptp_task(struct work_struct *work)
  131. {
  132. struct skb_shared_hwtstamps shhwtstamps;
  133. struct qede_dev *edev;
  134. struct qede_ptp *ptp;
  135. u64 timestamp, ns;
  136. int rc;
  137. ptp = container_of(work, struct qede_ptp, work);
  138. edev = ptp->edev;
  139. /* Read Tx timestamp registers */
  140. spin_lock_bh(&ptp->lock);
  141. rc = ptp->ops->read_tx_ts(edev->cdev, &timestamp);
  142. spin_unlock_bh(&ptp->lock);
  143. if (rc) {
  144. /* Reschedule to keep checking for a valid timestamp value */
  145. schedule_work(&ptp->work);
  146. return;
  147. }
  148. ns = timecounter_cyc2time(&ptp->tc, timestamp);
  149. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  150. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  151. skb_tstamp_tx(ptp->tx_skb, &shhwtstamps);
  152. dev_kfree_skb_any(ptp->tx_skb);
  153. ptp->tx_skb = NULL;
  154. DP_VERBOSE(edev, QED_MSG_DEBUG,
  155. "Tx timestamp, timestamp cycles = %llu, ns = %llu\n",
  156. timestamp, ns);
  157. }
  158. /* Read the PHC. This API is invoked with ptp_lock held. */
  159. static u64 qede_ptp_read_cc(const struct cyclecounter *cc)
  160. {
  161. struct qede_dev *edev;
  162. struct qede_ptp *ptp;
  163. u64 phc_cycles;
  164. int rc;
  165. ptp = container_of(cc, struct qede_ptp, cc);
  166. edev = ptp->edev;
  167. rc = ptp->ops->read_cc(edev->cdev, &phc_cycles);
  168. if (rc)
  169. WARN_ONCE(1, "PHC read err %d\n", rc);
  170. DP_VERBOSE(edev, QED_MSG_DEBUG, "PHC read cycles = %llu\n", phc_cycles);
  171. return phc_cycles;
  172. }
  173. static void qede_ptp_init_cc(struct qede_dev *edev)
  174. {
  175. struct qede_ptp *ptp;
  176. ptp = edev->ptp;
  177. if (!ptp)
  178. return;
  179. memset(&ptp->cc, 0, sizeof(ptp->cc));
  180. ptp->cc.read = qede_ptp_read_cc;
  181. ptp->cc.mask = CYCLECOUNTER_MASK(64);
  182. ptp->cc.shift = 0;
  183. ptp->cc.mult = 1;
  184. }
  185. static int qede_ptp_cfg_filters(struct qede_dev *edev)
  186. {
  187. struct qede_ptp *ptp = edev->ptp;
  188. if (!ptp)
  189. return -EIO;
  190. if (!ptp->hw_ts_ioctl_called) {
  191. DP_INFO(edev, "TS IOCTL not called\n");
  192. return 0;
  193. }
  194. switch (ptp->tx_type) {
  195. case HWTSTAMP_TX_ON:
  196. edev->flags |= QEDE_TX_TIMESTAMPING_EN;
  197. ptp->ops->hwtstamp_tx_on(edev->cdev);
  198. break;
  199. case HWTSTAMP_TX_ONESTEP_SYNC:
  200. DP_ERR(edev, "One-step timestamping is not supported\n");
  201. return -ERANGE;
  202. }
  203. spin_lock_bh(&ptp->lock);
  204. switch (ptp->rx_filter) {
  205. case HWTSTAMP_FILTER_NONE:
  206. break;
  207. case HWTSTAMP_FILTER_ALL:
  208. case HWTSTAMP_FILTER_SOME:
  209. ptp->rx_filter = HWTSTAMP_FILTER_NONE;
  210. break;
  211. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  212. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  213. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  214. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  215. /* Initialize PTP detection for UDP/IPv4 events */
  216. ptp->ops->cfg_rx_filters(edev->cdev, QED_PTP_FILTER_IPV4);
  217. break;
  218. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  219. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  220. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  221. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  222. /* Initialize PTP detection for UDP/IPv4 or UDP/IPv6 events */
  223. ptp->ops->cfg_rx_filters(edev->cdev, QED_PTP_FILTER_IPV4_IPV6);
  224. break;
  225. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  226. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  227. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  228. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  229. /* Initialize PTP detection L2 events */
  230. ptp->ops->cfg_rx_filters(edev->cdev, QED_PTP_FILTER_L2);
  231. break;
  232. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  233. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  234. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  235. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  236. /* Initialize PTP detection L2, UDP/IPv4 or UDP/IPv6 events */
  237. ptp->ops->cfg_rx_filters(edev->cdev,
  238. QED_PTP_FILTER_L2_IPV4_IPV6);
  239. break;
  240. }
  241. spin_unlock_bh(&ptp->lock);
  242. return 0;
  243. }
  244. int qede_ptp_hw_ts(struct qede_dev *edev, struct ifreq *ifr)
  245. {
  246. struct hwtstamp_config config;
  247. struct qede_ptp *ptp;
  248. int rc;
  249. ptp = edev->ptp;
  250. if (!ptp)
  251. return -EIO;
  252. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  253. return -EFAULT;
  254. DP_VERBOSE(edev, QED_MSG_DEBUG,
  255. "HWTSTAMP IOCTL: Requested tx_type = %d, requested rx_filters = %d\n",
  256. config.tx_type, config.rx_filter);
  257. if (config.flags) {
  258. DP_ERR(edev, "config.flags is reserved for future use\n");
  259. return -EINVAL;
  260. }
  261. ptp->hw_ts_ioctl_called = 1;
  262. ptp->tx_type = config.tx_type;
  263. ptp->rx_filter = config.rx_filter;
  264. rc = qede_ptp_cfg_filters(edev);
  265. if (rc)
  266. return rc;
  267. config.rx_filter = ptp->rx_filter;
  268. return copy_to_user(ifr->ifr_data, &config,
  269. sizeof(config)) ? -EFAULT : 0;
  270. }
  271. /* Called during load, to initialize PTP-related stuff */
  272. static void qede_ptp_init(struct qede_dev *edev, bool init_tc)
  273. {
  274. struct qede_ptp *ptp;
  275. int rc;
  276. ptp = edev->ptp;
  277. if (!ptp)
  278. return;
  279. spin_lock_init(&ptp->lock);
  280. /* Configure PTP in HW */
  281. rc = ptp->ops->enable(edev->cdev);
  282. if (rc) {
  283. DP_ERR(edev, "Stopping PTP initialization\n");
  284. return;
  285. }
  286. /* Init work queue for Tx timestamping */
  287. INIT_WORK(&ptp->work, qede_ptp_task);
  288. /* Init cyclecounter and timecounter. This is done only in the first
  289. * load. If done in every load, PTP application will fail when doing
  290. * unload / load (e.g. MTU change) while it is running.
  291. */
  292. if (init_tc) {
  293. qede_ptp_init_cc(edev);
  294. timecounter_init(&ptp->tc, &ptp->cc,
  295. ktime_to_ns(ktime_get_real()));
  296. }
  297. DP_VERBOSE(edev, QED_MSG_DEBUG, "PTP initialization is successful\n");
  298. }
  299. void qede_ptp_start(struct qede_dev *edev, bool init_tc)
  300. {
  301. qede_ptp_init(edev, init_tc);
  302. qede_ptp_cfg_filters(edev);
  303. }
  304. void qede_ptp_remove(struct qede_dev *edev)
  305. {
  306. struct qede_ptp *ptp;
  307. ptp = edev->ptp;
  308. if (ptp && ptp->clock) {
  309. ptp_clock_unregister(ptp->clock);
  310. ptp->clock = NULL;
  311. }
  312. kfree(ptp);
  313. edev->ptp = NULL;
  314. }
  315. int qede_ptp_get_ts_info(struct qede_dev *edev, struct ethtool_ts_info *info)
  316. {
  317. struct qede_ptp *ptp = edev->ptp;
  318. if (!ptp)
  319. return -EIO;
  320. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  321. SOF_TIMESTAMPING_RX_SOFTWARE |
  322. SOF_TIMESTAMPING_SOFTWARE |
  323. SOF_TIMESTAMPING_TX_HARDWARE |
  324. SOF_TIMESTAMPING_RX_HARDWARE |
  325. SOF_TIMESTAMPING_RAW_HARDWARE;
  326. if (ptp->clock)
  327. info->phc_index = ptp_clock_index(ptp->clock);
  328. else
  329. info->phc_index = -1;
  330. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
  331. BIT(HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  332. BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
  333. BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
  334. BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
  335. BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
  336. BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
  337. BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  338. BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
  339. BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
  340. BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
  341. BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
  342. BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
  343. info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
  344. return 0;
  345. }
  346. /* Called during unload, to stop PTP-related stuff */
  347. void qede_ptp_stop(struct qede_dev *edev)
  348. {
  349. struct qede_ptp *ptp;
  350. ptp = edev->ptp;
  351. if (!ptp)
  352. return;
  353. /* Cancel PTP work queue. Should be done after the Tx queues are
  354. * drained to prevent additional scheduling.
  355. */
  356. cancel_work_sync(&ptp->work);
  357. if (ptp->tx_skb) {
  358. dev_kfree_skb_any(ptp->tx_skb);
  359. ptp->tx_skb = NULL;
  360. }
  361. /* Disable PTP in HW */
  362. spin_lock_bh(&ptp->lock);
  363. ptp->ops->disable(edev->cdev);
  364. spin_unlock_bh(&ptp->lock);
  365. }
  366. int qede_ptp_register_phc(struct qede_dev *edev)
  367. {
  368. struct qede_ptp *ptp;
  369. ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
  370. if (!ptp) {
  371. DP_INFO(edev, "Failed to allocate struct for PTP\n");
  372. return -ENOMEM;
  373. }
  374. ptp->edev = edev;
  375. ptp->ops = edev->ops->ptp;
  376. if (!ptp->ops) {
  377. kfree(ptp);
  378. edev->ptp = NULL;
  379. DP_ERR(edev, "PTP clock registeration failed\n");
  380. return -EIO;
  381. }
  382. edev->ptp = ptp;
  383. /* Fill the ptp_clock_info struct and register PTP clock */
  384. ptp->clock_info.owner = THIS_MODULE;
  385. snprintf(ptp->clock_info.name, 16, "%s", edev->ndev->name);
  386. ptp->clock_info.max_adj = QED_MAX_PHC_DRIFT_PPB;
  387. ptp->clock_info.n_alarm = 0;
  388. ptp->clock_info.n_ext_ts = 0;
  389. ptp->clock_info.n_per_out = 0;
  390. ptp->clock_info.pps = 0;
  391. ptp->clock_info.adjfreq = qede_ptp_adjfreq;
  392. ptp->clock_info.adjtime = qede_ptp_adjtime;
  393. ptp->clock_info.gettime64 = qede_ptp_gettime;
  394. ptp->clock_info.settime64 = qede_ptp_settime;
  395. ptp->clock_info.enable = qede_ptp_ancillary_feature_enable;
  396. ptp->clock = ptp_clock_register(&ptp->clock_info, &edev->pdev->dev);
  397. if (IS_ERR(ptp->clock)) {
  398. ptp->clock = NULL;
  399. kfree(ptp);
  400. edev->ptp = NULL;
  401. DP_ERR(edev, "PTP clock registeration failed\n");
  402. }
  403. return 0;
  404. }
  405. void qede_ptp_tx_ts(struct qede_dev *edev, struct sk_buff *skb)
  406. {
  407. struct qede_ptp *ptp;
  408. ptp = edev->ptp;
  409. if (!ptp)
  410. return;
  411. if (unlikely(!(edev->flags & QEDE_TX_TIMESTAMPING_EN))) {
  412. DP_NOTICE(edev,
  413. "Tx timestamping was not enabled, this packet will not be timestamped\n");
  414. } else if (unlikely(ptp->tx_skb)) {
  415. DP_NOTICE(edev,
  416. "The device supports only a single outstanding packet to timestamp, this packet will not be timestamped\n");
  417. } else {
  418. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  419. /* schedule check for Tx timestamp */
  420. ptp->tx_skb = skb_get(skb);
  421. schedule_work(&ptp->work);
  422. }
  423. }
  424. void qede_ptp_rx_ts(struct qede_dev *edev, struct sk_buff *skb)
  425. {
  426. struct qede_ptp *ptp;
  427. u64 timestamp, ns;
  428. int rc;
  429. ptp = edev->ptp;
  430. if (!ptp)
  431. return;
  432. spin_lock_bh(&ptp->lock);
  433. rc = ptp->ops->read_rx_ts(edev->cdev, &timestamp);
  434. if (rc) {
  435. spin_unlock_bh(&ptp->lock);
  436. DP_INFO(edev, "Invalid Rx timestamp\n");
  437. return;
  438. }
  439. ns = timecounter_cyc2time(&ptp->tc, timestamp);
  440. spin_unlock_bh(&ptp->lock);
  441. skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
  442. DP_VERBOSE(edev, QED_MSG_DEBUG,
  443. "Rx timestamp, timestamp cycles = %llu, ns = %llu\n",
  444. timestamp, ns);
  445. }