ionic_phc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2021 Pensando Systems, Inc */
  3. #include <linux/netdevice.h>
  4. #include <linux/etherdevice.h>
  5. #include "ionic.h"
  6. #include "ionic_bus.h"
  7. #include "ionic_lif.h"
  8. #include "ionic_ethtool.h"
  9. static int ionic_hwstamp_tx_mode(int config_tx_type)
  10. {
  11. switch (config_tx_type) {
  12. case HWTSTAMP_TX_OFF:
  13. return IONIC_TXSTAMP_OFF;
  14. case HWTSTAMP_TX_ON:
  15. return IONIC_TXSTAMP_ON;
  16. case HWTSTAMP_TX_ONESTEP_SYNC:
  17. return IONIC_TXSTAMP_ONESTEP_SYNC;
  18. case HWTSTAMP_TX_ONESTEP_P2P:
  19. return IONIC_TXSTAMP_ONESTEP_P2P;
  20. default:
  21. return -ERANGE;
  22. }
  23. }
  24. static u64 ionic_hwstamp_rx_filt(int config_rx_filter)
  25. {
  26. switch (config_rx_filter) {
  27. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  28. return IONIC_PKT_CLS_PTP1_ALL;
  29. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  30. return IONIC_PKT_CLS_PTP1_SYNC;
  31. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  32. return IONIC_PKT_CLS_PTP1_SYNC | IONIC_PKT_CLS_PTP1_DREQ;
  33. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  34. return IONIC_PKT_CLS_PTP2_L4_ALL;
  35. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  36. return IONIC_PKT_CLS_PTP2_L4_SYNC;
  37. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  38. return IONIC_PKT_CLS_PTP2_L4_SYNC | IONIC_PKT_CLS_PTP2_L4_DREQ;
  39. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  40. return IONIC_PKT_CLS_PTP2_L2_ALL;
  41. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  42. return IONIC_PKT_CLS_PTP2_L2_SYNC;
  43. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  44. return IONIC_PKT_CLS_PTP2_L2_SYNC | IONIC_PKT_CLS_PTP2_L2_DREQ;
  45. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  46. return IONIC_PKT_CLS_PTP2_ALL;
  47. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  48. return IONIC_PKT_CLS_PTP2_SYNC;
  49. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  50. return IONIC_PKT_CLS_PTP2_SYNC | IONIC_PKT_CLS_PTP2_DREQ;
  51. case HWTSTAMP_FILTER_NTP_ALL:
  52. return IONIC_PKT_CLS_NTP_ALL;
  53. default:
  54. return 0;
  55. }
  56. }
  57. static int ionic_lif_hwstamp_set_ts_config(struct ionic_lif *lif,
  58. struct hwtstamp_config *new_ts)
  59. {
  60. struct ionic *ionic = lif->ionic;
  61. struct hwtstamp_config *config;
  62. struct hwtstamp_config ts;
  63. int tx_mode = 0;
  64. u64 rx_filt = 0;
  65. int err, err2;
  66. bool rx_all;
  67. __le64 mask;
  68. if (!lif->phc || !lif->phc->ptp)
  69. return -EOPNOTSUPP;
  70. mutex_lock(&lif->phc->config_lock);
  71. if (new_ts) {
  72. config = new_ts;
  73. } else {
  74. /* If called with new_ts == NULL, replay the previous request
  75. * primarily for recovery after a FW_RESET.
  76. * We saved the previous configuration request info, so copy
  77. * the previous request for reference, clear the current state
  78. * to match the device's reset state, and run with it.
  79. */
  80. config = &ts;
  81. memcpy(config, &lif->phc->ts_config, sizeof(*config));
  82. memset(&lif->phc->ts_config, 0, sizeof(lif->phc->ts_config));
  83. lif->phc->ts_config_tx_mode = 0;
  84. lif->phc->ts_config_rx_filt = 0;
  85. }
  86. tx_mode = ionic_hwstamp_tx_mode(config->tx_type);
  87. if (tx_mode < 0) {
  88. err = tx_mode;
  89. goto err_queues;
  90. }
  91. mask = cpu_to_le64(BIT_ULL(tx_mode));
  92. if ((ionic->ident.lif.eth.hwstamp_tx_modes & mask) != mask) {
  93. err = -ERANGE;
  94. goto err_queues;
  95. }
  96. rx_filt = ionic_hwstamp_rx_filt(config->rx_filter);
  97. rx_all = config->rx_filter != HWTSTAMP_FILTER_NONE && !rx_filt;
  98. mask = cpu_to_le64(rx_filt);
  99. if ((ionic->ident.lif.eth.hwstamp_rx_filters & mask) != mask) {
  100. rx_filt = 0;
  101. rx_all = true;
  102. config->rx_filter = HWTSTAMP_FILTER_ALL;
  103. }
  104. dev_dbg(ionic->dev, "%s: config_rx_filter %d rx_filt %#llx rx_all %d\n",
  105. __func__, config->rx_filter, rx_filt, rx_all);
  106. if (tx_mode) {
  107. err = ionic_lif_create_hwstamp_txq(lif);
  108. if (err)
  109. goto err_queues;
  110. }
  111. if (rx_filt) {
  112. err = ionic_lif_create_hwstamp_rxq(lif);
  113. if (err)
  114. goto err_queues;
  115. }
  116. if (tx_mode != lif->phc->ts_config_tx_mode) {
  117. err = ionic_lif_set_hwstamp_txmode(lif, tx_mode);
  118. if (err)
  119. goto err_txmode;
  120. }
  121. if (rx_filt != lif->phc->ts_config_rx_filt) {
  122. err = ionic_lif_set_hwstamp_rxfilt(lif, rx_filt);
  123. if (err)
  124. goto err_rxfilt;
  125. }
  126. if (rx_all != (lif->phc->ts_config.rx_filter == HWTSTAMP_FILTER_ALL)) {
  127. err = ionic_lif_config_hwstamp_rxq_all(lif, rx_all);
  128. if (err)
  129. goto err_rxall;
  130. }
  131. memcpy(&lif->phc->ts_config, config, sizeof(*config));
  132. lif->phc->ts_config_rx_filt = rx_filt;
  133. lif->phc->ts_config_tx_mode = tx_mode;
  134. mutex_unlock(&lif->phc->config_lock);
  135. return 0;
  136. err_rxall:
  137. if (rx_filt != lif->phc->ts_config_rx_filt) {
  138. rx_filt = lif->phc->ts_config_rx_filt;
  139. err2 = ionic_lif_set_hwstamp_rxfilt(lif, rx_filt);
  140. if (err2)
  141. dev_err(ionic->dev,
  142. "Failed to revert rx timestamp filter: %d\n", err2);
  143. }
  144. err_rxfilt:
  145. if (tx_mode != lif->phc->ts_config_tx_mode) {
  146. tx_mode = lif->phc->ts_config_tx_mode;
  147. err2 = ionic_lif_set_hwstamp_txmode(lif, tx_mode);
  148. if (err2)
  149. dev_err(ionic->dev,
  150. "Failed to revert tx timestamp mode: %d\n", err2);
  151. }
  152. err_txmode:
  153. /* special queues remain allocated, just unused */
  154. err_queues:
  155. mutex_unlock(&lif->phc->config_lock);
  156. return err;
  157. }
  158. int ionic_lif_hwstamp_set(struct ionic_lif *lif, struct ifreq *ifr)
  159. {
  160. struct hwtstamp_config config;
  161. int err;
  162. if (!lif->phc || !lif->phc->ptp)
  163. return -EOPNOTSUPP;
  164. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  165. return -EFAULT;
  166. mutex_lock(&lif->queue_lock);
  167. err = ionic_lif_hwstamp_set_ts_config(lif, &config);
  168. mutex_unlock(&lif->queue_lock);
  169. if (err) {
  170. netdev_info(lif->netdev, "hwstamp set failed: %d\n", err);
  171. return err;
  172. }
  173. if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
  174. return -EFAULT;
  175. return 0;
  176. }
  177. void ionic_lif_hwstamp_replay(struct ionic_lif *lif)
  178. {
  179. int err;
  180. if (!lif->phc || !lif->phc->ptp)
  181. return;
  182. mutex_lock(&lif->queue_lock);
  183. err = ionic_lif_hwstamp_set_ts_config(lif, NULL);
  184. mutex_unlock(&lif->queue_lock);
  185. if (err)
  186. netdev_info(lif->netdev, "hwstamp replay failed: %d\n", err);
  187. }
  188. void ionic_lif_hwstamp_recreate_queues(struct ionic_lif *lif)
  189. {
  190. int err;
  191. if (!lif->phc || !lif->phc->ptp)
  192. return;
  193. mutex_lock(&lif->phc->config_lock);
  194. if (lif->phc->ts_config_tx_mode) {
  195. err = ionic_lif_create_hwstamp_txq(lif);
  196. if (err)
  197. netdev_info(lif->netdev, "hwstamp recreate txq failed: %d\n", err);
  198. }
  199. if (lif->phc->ts_config_rx_filt) {
  200. err = ionic_lif_create_hwstamp_rxq(lif);
  201. if (err)
  202. netdev_info(lif->netdev, "hwstamp recreate rxq failed: %d\n", err);
  203. }
  204. mutex_unlock(&lif->phc->config_lock);
  205. }
  206. int ionic_lif_hwstamp_get(struct ionic_lif *lif, struct ifreq *ifr)
  207. {
  208. struct hwtstamp_config config;
  209. if (!lif->phc || !lif->phc->ptp)
  210. return -EOPNOTSUPP;
  211. mutex_lock(&lif->phc->config_lock);
  212. memcpy(&config, &lif->phc->ts_config, sizeof(config));
  213. mutex_unlock(&lif->phc->config_lock);
  214. if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
  215. return -EFAULT;
  216. return 0;
  217. }
  218. static u64 ionic_hwstamp_read(struct ionic *ionic,
  219. struct ptp_system_timestamp *sts)
  220. {
  221. u32 tick_high_before, tick_high, tick_low;
  222. /* read and discard low part to defeat hw staging of high part */
  223. ioread32(&ionic->idev.hwstamp_regs->tick_low);
  224. tick_high_before = ioread32(&ionic->idev.hwstamp_regs->tick_high);
  225. ptp_read_system_prets(sts);
  226. tick_low = ioread32(&ionic->idev.hwstamp_regs->tick_low);
  227. ptp_read_system_postts(sts);
  228. tick_high = ioread32(&ionic->idev.hwstamp_regs->tick_high);
  229. /* If tick_high changed, re-read tick_low once more. Assume tick_high
  230. * cannot change again so soon as in the span of re-reading tick_low.
  231. */
  232. if (tick_high != tick_high_before) {
  233. ptp_read_system_prets(sts);
  234. tick_low = ioread32(&ionic->idev.hwstamp_regs->tick_low);
  235. ptp_read_system_postts(sts);
  236. }
  237. return (u64)tick_low | ((u64)tick_high << 32);
  238. }
  239. static u64 ionic_cc_read(const struct cyclecounter *cc)
  240. {
  241. struct ionic_phc *phc = container_of(cc, struct ionic_phc, cc);
  242. struct ionic *ionic = phc->lif->ionic;
  243. return ionic_hwstamp_read(ionic, NULL);
  244. }
  245. static int ionic_setphc_cmd(struct ionic_phc *phc, struct ionic_admin_ctx *ctx)
  246. {
  247. ctx->work = COMPLETION_INITIALIZER_ONSTACK(ctx->work);
  248. ctx->cmd.lif_setphc.opcode = IONIC_CMD_LIF_SETPHC;
  249. ctx->cmd.lif_setphc.lif_index = cpu_to_le16(phc->lif->index);
  250. ctx->cmd.lif_setphc.tick = cpu_to_le64(phc->tc.cycle_last);
  251. ctx->cmd.lif_setphc.nsec = cpu_to_le64(phc->tc.nsec);
  252. ctx->cmd.lif_setphc.frac = cpu_to_le64(phc->tc.frac);
  253. ctx->cmd.lif_setphc.mult = cpu_to_le32(phc->cc.mult);
  254. ctx->cmd.lif_setphc.shift = cpu_to_le32(phc->cc.shift);
  255. return ionic_adminq_post(phc->lif, ctx);
  256. }
  257. static int ionic_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
  258. {
  259. struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info);
  260. struct ionic_admin_ctx ctx = {};
  261. unsigned long irqflags;
  262. s64 adj;
  263. int err;
  264. /* Reject phc adjustments during device upgrade */
  265. if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state))
  266. return -EBUSY;
  267. /* Adjustment value scaled by 2^16 million */
  268. adj = (s64)scaled_ppm * phc->init_cc_mult;
  269. /* Adjustment value to scale */
  270. adj /= (s64)SCALED_PPM;
  271. /* Final adjusted multiplier */
  272. adj += phc->init_cc_mult;
  273. spin_lock_irqsave(&phc->lock, irqflags);
  274. /* update the point-in-time basis to now, before adjusting the rate */
  275. timecounter_read(&phc->tc);
  276. phc->cc.mult = adj;
  277. /* Setphc commands are posted in-order, sequenced by phc->lock. We
  278. * need to drop the lock before waiting for the command to complete.
  279. */
  280. err = ionic_setphc_cmd(phc, &ctx);
  281. spin_unlock_irqrestore(&phc->lock, irqflags);
  282. return ionic_adminq_wait(phc->lif, &ctx, err, true);
  283. }
  284. static int ionic_phc_adjtime(struct ptp_clock_info *info, s64 delta)
  285. {
  286. struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info);
  287. struct ionic_admin_ctx ctx = {};
  288. unsigned long irqflags;
  289. int err;
  290. /* Reject phc adjustments during device upgrade */
  291. if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state))
  292. return -EBUSY;
  293. spin_lock_irqsave(&phc->lock, irqflags);
  294. timecounter_adjtime(&phc->tc, delta);
  295. /* Setphc commands are posted in-order, sequenced by phc->lock. We
  296. * need to drop the lock before waiting for the command to complete.
  297. */
  298. err = ionic_setphc_cmd(phc, &ctx);
  299. spin_unlock_irqrestore(&phc->lock, irqflags);
  300. return ionic_adminq_wait(phc->lif, &ctx, err, true);
  301. }
  302. static int ionic_phc_settime64(struct ptp_clock_info *info,
  303. const struct timespec64 *ts)
  304. {
  305. struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info);
  306. struct ionic_admin_ctx ctx = {};
  307. unsigned long irqflags;
  308. int err;
  309. u64 ns;
  310. /* Reject phc adjustments during device upgrade */
  311. if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state))
  312. return -EBUSY;
  313. ns = timespec64_to_ns(ts);
  314. spin_lock_irqsave(&phc->lock, irqflags);
  315. timecounter_init(&phc->tc, &phc->cc, ns);
  316. /* Setphc commands are posted in-order, sequenced by phc->lock. We
  317. * need to drop the lock before waiting for the command to complete.
  318. */
  319. err = ionic_setphc_cmd(phc, &ctx);
  320. spin_unlock_irqrestore(&phc->lock, irqflags);
  321. return ionic_adminq_wait(phc->lif, &ctx, err, true);
  322. }
  323. static int ionic_phc_gettimex64(struct ptp_clock_info *info,
  324. struct timespec64 *ts,
  325. struct ptp_system_timestamp *sts)
  326. {
  327. struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info);
  328. struct ionic *ionic = phc->lif->ionic;
  329. unsigned long irqflags;
  330. u64 tick, ns;
  331. /* Do not attempt to read device time during upgrade */
  332. if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state))
  333. return -EBUSY;
  334. spin_lock_irqsave(&phc->lock, irqflags);
  335. tick = ionic_hwstamp_read(ionic, sts);
  336. ns = timecounter_cyc2time(&phc->tc, tick);
  337. spin_unlock_irqrestore(&phc->lock, irqflags);
  338. *ts = ns_to_timespec64(ns);
  339. return 0;
  340. }
  341. static long ionic_phc_aux_work(struct ptp_clock_info *info)
  342. {
  343. struct ionic_phc *phc = container_of(info, struct ionic_phc, ptp_info);
  344. struct ionic_admin_ctx ctx = {};
  345. unsigned long irqflags;
  346. int err;
  347. /* Do not update phc during device upgrade, but keep polling to resume
  348. * after upgrade. Since we don't update the point in time basis, there
  349. * is no expectation that we are maintaining the phc time during the
  350. * upgrade. After upgrade, it will need to be readjusted back to the
  351. * correct time by the ptp daemon.
  352. */
  353. if (test_bit(IONIC_LIF_F_FW_RESET, phc->lif->state))
  354. return phc->aux_work_delay;
  355. spin_lock_irqsave(&phc->lock, irqflags);
  356. /* update point-in-time basis to now */
  357. timecounter_read(&phc->tc);
  358. /* Setphc commands are posted in-order, sequenced by phc->lock. We
  359. * need to drop the lock before waiting for the command to complete.
  360. */
  361. err = ionic_setphc_cmd(phc, &ctx);
  362. spin_unlock_irqrestore(&phc->lock, irqflags);
  363. ionic_adminq_wait(phc->lif, &ctx, err, true);
  364. return phc->aux_work_delay;
  365. }
  366. ktime_t ionic_lif_phc_ktime(struct ionic_lif *lif, u64 tick)
  367. {
  368. unsigned long irqflags;
  369. u64 ns;
  370. if (!lif->phc)
  371. return 0;
  372. spin_lock_irqsave(&lif->phc->lock, irqflags);
  373. ns = timecounter_cyc2time(&lif->phc->tc, tick);
  374. spin_unlock_irqrestore(&lif->phc->lock, irqflags);
  375. return ns_to_ktime(ns);
  376. }
  377. static const struct ptp_clock_info ionic_ptp_info = {
  378. .owner = THIS_MODULE,
  379. .name = "ionic_ptp",
  380. .adjfine = ionic_phc_adjfine,
  381. .adjtime = ionic_phc_adjtime,
  382. .gettimex64 = ionic_phc_gettimex64,
  383. .settime64 = ionic_phc_settime64,
  384. .do_aux_work = ionic_phc_aux_work,
  385. };
  386. void ionic_lif_register_phc(struct ionic_lif *lif)
  387. {
  388. if (!lif->phc || !(lif->hw_features & IONIC_ETH_HW_TIMESTAMP))
  389. return;
  390. lif->phc->ptp = ptp_clock_register(&lif->phc->ptp_info, lif->ionic->dev);
  391. if (IS_ERR(lif->phc->ptp)) {
  392. dev_warn(lif->ionic->dev, "Cannot register phc device: %ld\n",
  393. PTR_ERR(lif->phc->ptp));
  394. lif->phc->ptp = NULL;
  395. }
  396. if (lif->phc->ptp)
  397. ptp_schedule_worker(lif->phc->ptp, lif->phc->aux_work_delay);
  398. }
  399. void ionic_lif_unregister_phc(struct ionic_lif *lif)
  400. {
  401. if (!lif->phc || !lif->phc->ptp)
  402. return;
  403. ptp_clock_unregister(lif->phc->ptp);
  404. lif->phc->ptp = NULL;
  405. }
  406. void ionic_lif_alloc_phc(struct ionic_lif *lif)
  407. {
  408. struct ionic *ionic = lif->ionic;
  409. struct ionic_phc *phc;
  410. u64 delay, diff, mult;
  411. u64 frac = 0;
  412. u64 features;
  413. u32 shift;
  414. if (!ionic->idev.hwstamp_regs)
  415. return;
  416. features = le64_to_cpu(ionic->ident.lif.eth.config.features);
  417. if (!(features & IONIC_ETH_HW_TIMESTAMP))
  418. return;
  419. phc = devm_kzalloc(ionic->dev, sizeof(*phc), GFP_KERNEL);
  420. if (!phc)
  421. return;
  422. phc->lif = lif;
  423. phc->cc.read = ionic_cc_read;
  424. phc->cc.mask = le64_to_cpu(ionic->ident.dev.hwstamp_mask);
  425. phc->cc.mult = le32_to_cpu(ionic->ident.dev.hwstamp_mult);
  426. phc->cc.shift = le32_to_cpu(ionic->ident.dev.hwstamp_shift);
  427. if (!phc->cc.mult) {
  428. dev_err(lif->ionic->dev,
  429. "Invalid device PHC mask multiplier %u, disabling HW timestamp support\n",
  430. phc->cc.mult);
  431. devm_kfree(lif->ionic->dev, phc);
  432. lif->phc = NULL;
  433. return;
  434. }
  435. dev_dbg(lif->ionic->dev, "Device PHC mask %#llx mult %u shift %u\n",
  436. phc->cc.mask, phc->cc.mult, phc->cc.shift);
  437. spin_lock_init(&phc->lock);
  438. mutex_init(&phc->config_lock);
  439. /* max ticks is limited by the multiplier, or by the update period. */
  440. if (phc->cc.shift + 2 + ilog2(IONIC_PHC_UPDATE_NS) >= 64) {
  441. /* max ticks that do not overflow when multiplied by max
  442. * adjusted multiplier (twice the initial multiplier)
  443. */
  444. diff = U64_MAX / phc->cc.mult / 2;
  445. } else {
  446. /* approx ticks at four times the update period */
  447. diff = (u64)IONIC_PHC_UPDATE_NS << (phc->cc.shift + 2);
  448. diff = DIV_ROUND_UP(diff, phc->cc.mult);
  449. }
  450. /* transform to bitmask */
  451. diff |= diff >> 1;
  452. diff |= diff >> 2;
  453. diff |= diff >> 4;
  454. diff |= diff >> 8;
  455. diff |= diff >> 16;
  456. diff |= diff >> 32;
  457. /* constrain to the hardware bitmask */
  458. diff &= phc->cc.mask;
  459. /* the wrap period is now defined by diff
  460. *
  461. * we will update the time basis at about 1/4 the wrap period, so
  462. * should not see a difference of more than +/- diff/4.
  463. *
  464. * this is sufficient not see a difference of more than +/- diff/2, as
  465. * required by timecounter_cyc2time, to detect an old time stamp.
  466. *
  467. * adjust the initial multiplier, being careful to avoid overflow:
  468. * - do not overflow 63 bits: init_cc_mult * SCALED_PPM
  469. * - do not overflow 64 bits: max_mult * (diff / 2)
  470. *
  471. * we want to increase the initial multiplier as much as possible, to
  472. * allow for more precise adjustment in ionic_phc_adjfine.
  473. *
  474. * only adjust the multiplier if we can double it or more.
  475. */
  476. mult = U64_MAX / 2 / max(diff / 2, SCALED_PPM);
  477. shift = mult / phc->cc.mult;
  478. if (shift >= 2) {
  479. /* initial multiplier will be 2^n of hardware cc.mult */
  480. shift = fls(shift);
  481. /* increase cc.mult and cc.shift by the same 2^n and n. */
  482. phc->cc.mult <<= shift;
  483. phc->cc.shift += shift;
  484. }
  485. dev_dbg(lif->ionic->dev, "Initial PHC mask %#llx mult %u shift %u\n",
  486. phc->cc.mask, phc->cc.mult, phc->cc.shift);
  487. /* frequency adjustments are relative to the initial multiplier */
  488. phc->init_cc_mult = phc->cc.mult;
  489. timecounter_init(&phc->tc, &phc->cc, ktime_get_real_ns());
  490. /* Update cycle_last at 1/4 the wrap period, or IONIC_PHC_UPDATE_NS */
  491. delay = min_t(u64, IONIC_PHC_UPDATE_NS,
  492. cyclecounter_cyc2ns(&phc->cc, diff / 4, 0, &frac));
  493. dev_dbg(lif->ionic->dev, "Work delay %llu ms\n", delay / NSEC_PER_MSEC);
  494. phc->aux_work_delay = nsecs_to_jiffies(delay);
  495. phc->ptp_info = ionic_ptp_info;
  496. /* We have allowed to adjust the multiplier up to +/- 1 part per 1.
  497. * Here expressed as NORMAL_PPB (1 billion parts per billion).
  498. */
  499. phc->ptp_info.max_adj = NORMAL_PPB;
  500. lif->phc = phc;
  501. }
  502. void ionic_lif_free_phc(struct ionic_lif *lif)
  503. {
  504. if (!lif->phc)
  505. return;
  506. mutex_destroy(&lif->phc->config_lock);
  507. devm_kfree(lif->ionic->dev, lif->phc);
  508. lif->phc = NULL;
  509. }