bnxt_ptp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2021 Broadcom Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/pci.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/timekeeping.h>
  16. #include <linux/ptp_classify.h>
  17. #include <linux/clocksource.h>
  18. #include "bnxt_hsi.h"
  19. #include "bnxt.h"
  20. #include "bnxt_hwrm.h"
  21. #include "bnxt_ptp.h"
  22. static int bnxt_ptp_cfg_settime(struct bnxt *bp, u64 time)
  23. {
  24. struct hwrm_func_ptp_cfg_input *req;
  25. int rc;
  26. rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
  27. if (rc)
  28. return rc;
  29. req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_SET_TIME);
  30. req->ptp_set_time = cpu_to_le64(time);
  31. return hwrm_req_send(bp, req);
  32. }
  33. int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id, u16 *hdr_off)
  34. {
  35. unsigned int ptp_class;
  36. struct ptp_header *hdr;
  37. ptp_class = ptp_classify_raw(skb);
  38. switch (ptp_class & PTP_CLASS_VMASK) {
  39. case PTP_CLASS_V1:
  40. case PTP_CLASS_V2:
  41. hdr = ptp_parse_header(skb, ptp_class);
  42. if (!hdr)
  43. return -EINVAL;
  44. *hdr_off = (u8 *)hdr - skb->data;
  45. *seq_id = ntohs(hdr->sequence_id);
  46. return 0;
  47. default:
  48. return -ERANGE;
  49. }
  50. }
  51. static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info,
  52. const struct timespec64 *ts)
  53. {
  54. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  55. ptp_info);
  56. u64 ns = timespec64_to_ns(ts);
  57. unsigned long flags;
  58. if (BNXT_PTP_USE_RTC(ptp->bp))
  59. return bnxt_ptp_cfg_settime(ptp->bp, ns);
  60. spin_lock_irqsave(&ptp->ptp_lock, flags);
  61. timecounter_init(&ptp->tc, &ptp->cc, ns);
  62. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  63. return 0;
  64. }
  65. /* Caller holds ptp_lock */
  66. static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
  67. u64 *ns)
  68. {
  69. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  70. u32 high_before, high_now, low;
  71. if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
  72. return -EIO;
  73. high_before = readl(bp->bar0 + ptp->refclk_mapped_regs[1]);
  74. ptp_read_system_prets(sts);
  75. low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
  76. ptp_read_system_postts(sts);
  77. high_now = readl(bp->bar0 + ptp->refclk_mapped_regs[1]);
  78. if (high_now != high_before) {
  79. ptp_read_system_prets(sts);
  80. low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
  81. ptp_read_system_postts(sts);
  82. }
  83. *ns = ((u64)high_now << 32) | low;
  84. return 0;
  85. }
  86. static void bnxt_ptp_get_current_time(struct bnxt *bp)
  87. {
  88. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  89. unsigned long flags;
  90. if (!ptp)
  91. return;
  92. spin_lock_irqsave(&ptp->ptp_lock, flags);
  93. WRITE_ONCE(ptp->old_time, ptp->current_time);
  94. bnxt_refclk_read(bp, NULL, &ptp->current_time);
  95. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  96. }
  97. static int bnxt_hwrm_port_ts_query(struct bnxt *bp, u32 flags, u64 *ts,
  98. u32 txts_tmo, int slot)
  99. {
  100. struct hwrm_port_ts_query_output *resp;
  101. struct hwrm_port_ts_query_input *req;
  102. int rc;
  103. rc = hwrm_req_init(bp, req, HWRM_PORT_TS_QUERY);
  104. if (rc)
  105. return rc;
  106. req->flags = cpu_to_le32(flags);
  107. if ((flags & PORT_TS_QUERY_REQ_FLAGS_PATH) ==
  108. PORT_TS_QUERY_REQ_FLAGS_PATH_TX) {
  109. struct bnxt_ptp_tx_req *txts_req = &bp->ptp_cfg->txts_req[slot];
  110. u32 tmo_us = txts_tmo * 1000;
  111. req->enables = cpu_to_le16(BNXT_PTP_QTS_TX_ENABLES);
  112. req->ptp_seq_id = cpu_to_le32(txts_req->tx_seqid);
  113. req->ptp_hdr_offset = cpu_to_le16(txts_req->tx_hdr_off);
  114. if (!tmo_us)
  115. tmo_us = BNXT_PTP_QTS_TIMEOUT;
  116. tmo_us = min(tmo_us, BNXT_PTP_QTS_MAX_TMO_US);
  117. req->ts_req_timeout = cpu_to_le16(tmo_us);
  118. }
  119. resp = hwrm_req_hold(bp, req);
  120. rc = hwrm_req_send_silent(bp, req);
  121. if (!rc)
  122. *ts = le64_to_cpu(resp->ptp_msg_ts);
  123. hwrm_req_drop(bp, req);
  124. return rc;
  125. }
  126. static int bnxt_ptp_gettimex(struct ptp_clock_info *ptp_info,
  127. struct timespec64 *ts,
  128. struct ptp_system_timestamp *sts)
  129. {
  130. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  131. ptp_info);
  132. unsigned long flags;
  133. u64 ns, cycles;
  134. int rc;
  135. spin_lock_irqsave(&ptp->ptp_lock, flags);
  136. rc = bnxt_refclk_read(ptp->bp, sts, &cycles);
  137. if (rc) {
  138. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  139. return rc;
  140. }
  141. ns = timecounter_cyc2time(&ptp->tc, cycles);
  142. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  143. *ts = ns_to_timespec64(ns);
  144. return 0;
  145. }
  146. /* Caller holds ptp_lock */
  147. void bnxt_ptp_update_current_time(struct bnxt *bp)
  148. {
  149. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  150. bnxt_refclk_read(ptp->bp, NULL, &ptp->current_time);
  151. WRITE_ONCE(ptp->old_time, ptp->current_time);
  152. }
  153. static int bnxt_ptp_adjphc(struct bnxt_ptp_cfg *ptp, s64 delta)
  154. {
  155. struct hwrm_port_mac_cfg_input *req;
  156. unsigned long flags;
  157. int rc;
  158. rc = hwrm_req_init(ptp->bp, req, HWRM_PORT_MAC_CFG);
  159. if (rc)
  160. return rc;
  161. req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_ADJ_PHASE);
  162. req->ptp_adj_phase = cpu_to_le64(delta);
  163. rc = hwrm_req_send(ptp->bp, req);
  164. if (rc) {
  165. netdev_err(ptp->bp->dev, "ptp adjphc failed. rc = %x\n", rc);
  166. } else {
  167. spin_lock_irqsave(&ptp->ptp_lock, flags);
  168. bnxt_ptp_update_current_time(ptp->bp);
  169. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  170. }
  171. return rc;
  172. }
  173. static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
  174. {
  175. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  176. ptp_info);
  177. unsigned long flags;
  178. if (BNXT_PTP_USE_RTC(ptp->bp))
  179. return bnxt_ptp_adjphc(ptp, delta);
  180. spin_lock_irqsave(&ptp->ptp_lock, flags);
  181. timecounter_adjtime(&ptp->tc, delta);
  182. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  183. return 0;
  184. }
  185. static int bnxt_ptp_adjfine_rtc(struct bnxt *bp, long scaled_ppm)
  186. {
  187. s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
  188. struct hwrm_port_mac_cfg_input *req;
  189. int rc;
  190. rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG);
  191. if (rc)
  192. return rc;
  193. req->ptp_freq_adj_ppb = cpu_to_le32(ppb);
  194. req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_FREQ_ADJ_PPB);
  195. rc = hwrm_req_send(bp, req);
  196. if (rc)
  197. netdev_err(bp->dev,
  198. "ptp adjfine failed. rc = %d\n", rc);
  199. return rc;
  200. }
  201. static int bnxt_ptp_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
  202. {
  203. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  204. ptp_info);
  205. struct bnxt *bp = ptp->bp;
  206. unsigned long flags;
  207. if (!BNXT_MH(bp))
  208. return bnxt_ptp_adjfine_rtc(bp, scaled_ppm);
  209. spin_lock_irqsave(&ptp->ptp_lock, flags);
  210. timecounter_read(&ptp->tc);
  211. ptp->cc.mult = adjust_by_scaled_ppm(ptp->cmult, scaled_ppm);
  212. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  213. return 0;
  214. }
  215. void bnxt_ptp_pps_event(struct bnxt *bp, u32 data1, u32 data2)
  216. {
  217. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  218. struct ptp_clock_event event;
  219. unsigned long flags;
  220. u64 ns, pps_ts;
  221. pps_ts = EVENT_PPS_TS(data2, data1);
  222. spin_lock_irqsave(&ptp->ptp_lock, flags);
  223. ns = timecounter_cyc2time(&ptp->tc, pps_ts);
  224. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  225. switch (EVENT_DATA2_PPS_EVENT_TYPE(data2)) {
  226. case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_INTERNAL:
  227. event.pps_times.ts_real = ns_to_timespec64(ns);
  228. event.type = PTP_CLOCK_PPSUSR;
  229. event.index = EVENT_DATA2_PPS_PIN_NUM(data2);
  230. break;
  231. case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_EXTERNAL:
  232. event.timestamp = ns;
  233. event.type = PTP_CLOCK_EXTTS;
  234. event.index = EVENT_DATA2_PPS_PIN_NUM(data2);
  235. break;
  236. }
  237. ptp_clock_event(bp->ptp_cfg->ptp_clock, &event);
  238. }
  239. static int bnxt_ptp_cfg_pin(struct bnxt *bp, u8 pin, u8 usage)
  240. {
  241. struct hwrm_func_ptp_pin_cfg_input *req;
  242. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  243. u8 state = usage != BNXT_PPS_PIN_NONE;
  244. u8 *pin_state, *pin_usg;
  245. u32 enables;
  246. int rc;
  247. if (!TSIO_PIN_VALID(pin)) {
  248. netdev_err(ptp->bp->dev, "1PPS: Invalid pin. Check pin-function configuration\n");
  249. return -EOPNOTSUPP;
  250. }
  251. rc = hwrm_req_init(ptp->bp, req, HWRM_FUNC_PTP_PIN_CFG);
  252. if (rc)
  253. return rc;
  254. enables = (FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_STATE |
  255. FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_USAGE) << (pin * 2);
  256. req->enables = cpu_to_le32(enables);
  257. pin_state = &req->pin0_state;
  258. pin_usg = &req->pin0_usage;
  259. *(pin_state + (pin * 2)) = state;
  260. *(pin_usg + (pin * 2)) = usage;
  261. rc = hwrm_req_send(ptp->bp, req);
  262. if (rc)
  263. return rc;
  264. ptp->pps_info.pins[pin].usage = usage;
  265. ptp->pps_info.pins[pin].state = state;
  266. return 0;
  267. }
  268. static int bnxt_ptp_cfg_event(struct bnxt *bp, u8 event)
  269. {
  270. struct hwrm_func_ptp_cfg_input *req;
  271. int rc;
  272. rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
  273. if (rc)
  274. return rc;
  275. req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_PPS_EVENT);
  276. req->ptp_pps_event = event;
  277. return hwrm_req_send(bp, req);
  278. }
  279. int bnxt_ptp_cfg_tstamp_filters(struct bnxt *bp)
  280. {
  281. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  282. struct hwrm_port_mac_cfg_input *req;
  283. int rc;
  284. if (!ptp || !ptp->tstamp_filters)
  285. return -EIO;
  286. rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG);
  287. if (rc)
  288. goto out;
  289. if (!(bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS) && (ptp->tstamp_filters &
  290. (PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE |
  291. PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE))) {
  292. ptp->tstamp_filters &= ~(PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE |
  293. PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE);
  294. netdev_warn(bp->dev, "Unsupported FW for all RX pkts timestamp filter\n");
  295. }
  296. req->flags = cpu_to_le32(ptp->tstamp_filters);
  297. req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
  298. req->rx_ts_capture_ptp_msg_type = cpu_to_le16(ptp->rxctl);
  299. rc = hwrm_req_send(bp, req);
  300. if (!rc) {
  301. bp->ptp_all_rx_tstamp = !!(ptp->tstamp_filters &
  302. PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE);
  303. return 0;
  304. }
  305. ptp->tstamp_filters = 0;
  306. out:
  307. bp->ptp_all_rx_tstamp = 0;
  308. netdev_warn(bp->dev, "Failed to configure HW packet timestamp filters\n");
  309. return rc;
  310. }
  311. void bnxt_ptp_reapply_pps(struct bnxt *bp)
  312. {
  313. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  314. struct bnxt_pps *pps;
  315. u32 pin = 0;
  316. int rc;
  317. if (!ptp || !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) ||
  318. !(ptp->ptp_info.pin_config))
  319. return;
  320. pps = &ptp->pps_info;
  321. for (pin = 0; pin < BNXT_MAX_TSIO_PINS; pin++) {
  322. if (pps->pins[pin].state) {
  323. rc = bnxt_ptp_cfg_pin(bp, pin, pps->pins[pin].usage);
  324. if (!rc && pps->pins[pin].event)
  325. rc = bnxt_ptp_cfg_event(bp,
  326. pps->pins[pin].event);
  327. if (rc)
  328. netdev_err(bp->dev, "1PPS: Failed to configure pin%d\n",
  329. pin);
  330. }
  331. }
  332. }
  333. static int bnxt_get_target_cycles(struct bnxt_ptp_cfg *ptp, u64 target_ns,
  334. u64 *cycles_delta)
  335. {
  336. u64 cycles_now;
  337. u64 nsec_now, nsec_delta;
  338. unsigned long flags;
  339. int rc;
  340. spin_lock_irqsave(&ptp->ptp_lock, flags);
  341. rc = bnxt_refclk_read(ptp->bp, NULL, &cycles_now);
  342. if (rc) {
  343. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  344. return rc;
  345. }
  346. nsec_now = timecounter_cyc2time(&ptp->tc, cycles_now);
  347. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  348. nsec_delta = target_ns - nsec_now;
  349. *cycles_delta = div64_u64(nsec_delta << ptp->cc.shift, ptp->cc.mult);
  350. return 0;
  351. }
  352. static int bnxt_ptp_perout_cfg(struct bnxt_ptp_cfg *ptp,
  353. struct ptp_clock_request *rq)
  354. {
  355. struct hwrm_func_ptp_cfg_input *req;
  356. struct bnxt *bp = ptp->bp;
  357. struct timespec64 ts;
  358. u64 target_ns, delta;
  359. u16 enables;
  360. int rc;
  361. ts.tv_sec = rq->perout.start.sec;
  362. ts.tv_nsec = rq->perout.start.nsec;
  363. target_ns = timespec64_to_ns(&ts);
  364. rc = bnxt_get_target_cycles(ptp, target_ns, &delta);
  365. if (rc)
  366. return rc;
  367. rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
  368. if (rc)
  369. return rc;
  370. enables = FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PERIOD |
  371. FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_UP |
  372. FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PHASE;
  373. req->enables = cpu_to_le16(enables);
  374. req->ptp_pps_event = 0;
  375. req->ptp_freq_adj_dll_source = 0;
  376. req->ptp_freq_adj_dll_phase = 0;
  377. req->ptp_freq_adj_ext_period = cpu_to_le32(NSEC_PER_SEC);
  378. req->ptp_freq_adj_ext_up = 0;
  379. req->ptp_freq_adj_ext_phase_lower = cpu_to_le32(delta);
  380. return hwrm_req_send(bp, req);
  381. }
  382. static int bnxt_ptp_enable(struct ptp_clock_info *ptp_info,
  383. struct ptp_clock_request *rq, int on)
  384. {
  385. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  386. ptp_info);
  387. struct bnxt *bp = ptp->bp;
  388. int pin_id;
  389. int rc;
  390. switch (rq->type) {
  391. case PTP_CLK_REQ_EXTTS:
  392. /* Configure an External PPS IN */
  393. pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS,
  394. rq->extts.index);
  395. if (!TSIO_PIN_VALID(pin_id))
  396. return -EOPNOTSUPP;
  397. if (!on)
  398. break;
  399. rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_IN);
  400. if (rc)
  401. return rc;
  402. rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_EXTERNAL);
  403. if (!rc)
  404. ptp->pps_info.pins[pin_id].event = BNXT_PPS_EVENT_EXTERNAL;
  405. return rc;
  406. case PTP_CLK_REQ_PEROUT:
  407. /* Configure a Periodic PPS OUT */
  408. pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
  409. rq->perout.index);
  410. if (!TSIO_PIN_VALID(pin_id))
  411. return -EOPNOTSUPP;
  412. if (!on)
  413. break;
  414. rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_OUT);
  415. if (!rc)
  416. rc = bnxt_ptp_perout_cfg(ptp, rq);
  417. return rc;
  418. case PTP_CLK_REQ_PPS:
  419. /* Configure PHC PPS IN */
  420. rc = bnxt_ptp_cfg_pin(bp, 0, BNXT_PPS_PIN_PPS_IN);
  421. if (rc)
  422. return rc;
  423. rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_INTERNAL);
  424. if (!rc)
  425. ptp->pps_info.pins[0].event = BNXT_PPS_EVENT_INTERNAL;
  426. return rc;
  427. default:
  428. netdev_err(ptp->bp->dev, "Unrecognized PIN function\n");
  429. return -EOPNOTSUPP;
  430. }
  431. return bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_NONE);
  432. }
  433. static int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
  434. {
  435. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  436. u32 flags = 0;
  437. switch (ptp->rx_filter) {
  438. case HWTSTAMP_FILTER_ALL:
  439. flags = PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE;
  440. break;
  441. case HWTSTAMP_FILTER_NONE:
  442. flags = PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
  443. if (bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS)
  444. flags |= PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE;
  445. break;
  446. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  447. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  448. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  449. flags = PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
  450. break;
  451. }
  452. if (ptp->tx_tstamp_en)
  453. flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
  454. else
  455. flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
  456. ptp->tstamp_filters = flags;
  457. return bnxt_ptp_cfg_tstamp_filters(bp);
  458. }
  459. int bnxt_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  460. {
  461. struct bnxt *bp = netdev_priv(dev);
  462. struct hwtstamp_config stmpconf;
  463. struct bnxt_ptp_cfg *ptp;
  464. u16 old_rxctl;
  465. int old_rx_filter, rc;
  466. u8 old_tx_tstamp_en;
  467. ptp = bp->ptp_cfg;
  468. if (!ptp)
  469. return -EOPNOTSUPP;
  470. if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
  471. return -EFAULT;
  472. if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
  473. stmpconf.tx_type != HWTSTAMP_TX_OFF)
  474. return -ERANGE;
  475. old_rx_filter = ptp->rx_filter;
  476. old_rxctl = ptp->rxctl;
  477. old_tx_tstamp_en = ptp->tx_tstamp_en;
  478. switch (stmpconf.rx_filter) {
  479. case HWTSTAMP_FILTER_NONE:
  480. ptp->rxctl = 0;
  481. ptp->rx_filter = HWTSTAMP_FILTER_NONE;
  482. break;
  483. case HWTSTAMP_FILTER_ALL:
  484. if (bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS) {
  485. ptp->rx_filter = HWTSTAMP_FILTER_ALL;
  486. break;
  487. }
  488. return -EOPNOTSUPP;
  489. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  490. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  491. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  492. ptp->rxctl = BNXT_PTP_MSG_EVENTS;
  493. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  494. break;
  495. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  496. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  497. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  498. ptp->rxctl = BNXT_PTP_MSG_SYNC;
  499. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  500. break;
  501. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  502. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  503. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  504. ptp->rxctl = BNXT_PTP_MSG_DELAY_REQ;
  505. ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  506. break;
  507. default:
  508. return -ERANGE;
  509. }
  510. if (stmpconf.tx_type == HWTSTAMP_TX_ON)
  511. ptp->tx_tstamp_en = 1;
  512. else
  513. ptp->tx_tstamp_en = 0;
  514. rc = bnxt_hwrm_ptp_cfg(bp);
  515. if (rc)
  516. goto ts_set_err;
  517. stmpconf.rx_filter = ptp->rx_filter;
  518. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  519. -EFAULT : 0;
  520. ts_set_err:
  521. ptp->rx_filter = old_rx_filter;
  522. ptp->rxctl = old_rxctl;
  523. ptp->tx_tstamp_en = old_tx_tstamp_en;
  524. return rc;
  525. }
  526. int bnxt_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  527. {
  528. struct bnxt *bp = netdev_priv(dev);
  529. struct hwtstamp_config stmpconf;
  530. struct bnxt_ptp_cfg *ptp;
  531. ptp = bp->ptp_cfg;
  532. if (!ptp)
  533. return -EOPNOTSUPP;
  534. stmpconf.flags = 0;
  535. stmpconf.tx_type = ptp->tx_tstamp_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  536. stmpconf.rx_filter = ptp->rx_filter;
  537. return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
  538. -EFAULT : 0;
  539. }
  540. static int bnxt_map_regs(struct bnxt *bp, u32 *reg_arr, int count, int reg_win)
  541. {
  542. u32 reg_base = *reg_arr & BNXT_GRC_BASE_MASK;
  543. u32 win_off;
  544. int i;
  545. for (i = 0; i < count; i++) {
  546. if ((reg_arr[i] & BNXT_GRC_BASE_MASK) != reg_base)
  547. return -ERANGE;
  548. }
  549. win_off = BNXT_GRCPF_REG_WINDOW_BASE_OUT + (reg_win - 1) * 4;
  550. writel(reg_base, bp->bar0 + win_off);
  551. return 0;
  552. }
  553. static int bnxt_map_ptp_regs(struct bnxt *bp)
  554. {
  555. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  556. u32 *reg_arr;
  557. int rc, i;
  558. reg_arr = ptp->refclk_regs;
  559. if (BNXT_CHIP_P5(bp)) {
  560. rc = bnxt_map_regs(bp, reg_arr, 2, BNXT_PTP_GRC_WIN);
  561. if (rc)
  562. return rc;
  563. for (i = 0; i < 2; i++)
  564. ptp->refclk_mapped_regs[i] = BNXT_PTP_GRC_WIN_BASE +
  565. (ptp->refclk_regs[i] & BNXT_GRC_OFFSET_MASK);
  566. return 0;
  567. }
  568. if (bp->flags & BNXT_FLAG_CHIP_P7) {
  569. for (i = 0; i < 2; i++) {
  570. if (reg_arr[i] & BNXT_GRC_BASE_MASK)
  571. return -EINVAL;
  572. ptp->refclk_mapped_regs[i] = reg_arr[i];
  573. }
  574. return 0;
  575. }
  576. return -ENODEV;
  577. }
  578. static void bnxt_unmap_ptp_regs(struct bnxt *bp)
  579. {
  580. writel(0, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT +
  581. (BNXT_PTP_GRC_WIN - 1) * 4);
  582. }
  583. static u64 bnxt_cc_read(const struct cyclecounter *cc)
  584. {
  585. struct bnxt_ptp_cfg *ptp = container_of(cc, struct bnxt_ptp_cfg, cc);
  586. u64 ns = 0;
  587. bnxt_refclk_read(ptp->bp, NULL, &ns);
  588. return ns;
  589. }
  590. static int bnxt_stamp_tx_skb(struct bnxt *bp, int slot)
  591. {
  592. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  593. struct skb_shared_hwtstamps timestamp;
  594. struct bnxt_ptp_tx_req *txts_req;
  595. unsigned long now = jiffies;
  596. unsigned long flags;
  597. u64 ts = 0, ns = 0;
  598. u32 tmo = 0;
  599. int rc;
  600. txts_req = &ptp->txts_req[slot];
  601. /* make sure bnxt_get_tx_ts_p5() has updated abs_txts_tmo */
  602. smp_rmb();
  603. if (!time_after_eq(now, txts_req->abs_txts_tmo))
  604. tmo = jiffies_to_msecs(txts_req->abs_txts_tmo - now);
  605. rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_PATH_TX, &ts,
  606. tmo, slot);
  607. if (!rc) {
  608. memset(&timestamp, 0, sizeof(timestamp));
  609. spin_lock_irqsave(&ptp->ptp_lock, flags);
  610. ns = timecounter_cyc2time(&ptp->tc, ts);
  611. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  612. timestamp.hwtstamp = ns_to_ktime(ns);
  613. skb_tstamp_tx(txts_req->tx_skb, &timestamp);
  614. ptp->stats.ts_pkts++;
  615. } else {
  616. if (!time_after_eq(jiffies, txts_req->abs_txts_tmo))
  617. return -EAGAIN;
  618. ptp->stats.ts_lost++;
  619. netdev_warn_once(bp->dev,
  620. "TS query for TX timer failed rc = %x\n", rc);
  621. }
  622. dev_kfree_skb_any(txts_req->tx_skb);
  623. txts_req->tx_skb = NULL;
  624. return 0;
  625. }
  626. static long bnxt_ptp_ts_aux_work(struct ptp_clock_info *ptp_info)
  627. {
  628. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  629. ptp_info);
  630. unsigned long now = jiffies;
  631. struct bnxt *bp = ptp->bp;
  632. u16 cons = ptp->txts_cons;
  633. unsigned long flags;
  634. u32 num_requests;
  635. int rc = 0;
  636. num_requests = BNXT_MAX_TX_TS - READ_ONCE(ptp->tx_avail);
  637. while (num_requests--) {
  638. if (IS_ERR(ptp->txts_req[cons].tx_skb))
  639. goto next_slot;
  640. if (!ptp->txts_req[cons].tx_skb)
  641. break;
  642. rc = bnxt_stamp_tx_skb(bp, cons);
  643. if (rc == -EAGAIN)
  644. break;
  645. next_slot:
  646. BNXT_PTP_INC_TX_AVAIL(ptp);
  647. cons = NEXT_TXTS(cons);
  648. }
  649. ptp->txts_cons = cons;
  650. if (!time_after_eq(now, ptp->next_period)) {
  651. if (rc == -EAGAIN)
  652. return 0;
  653. return ptp->next_period - now;
  654. }
  655. bnxt_ptp_get_current_time(bp);
  656. ptp->next_period = now + HZ;
  657. if (time_after_eq(now, ptp->next_overflow_check)) {
  658. spin_lock_irqsave(&ptp->ptp_lock, flags);
  659. timecounter_read(&ptp->tc);
  660. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  661. ptp->next_overflow_check = now + BNXT_PHC_OVERFLOW_PERIOD;
  662. }
  663. if (rc == -EAGAIN)
  664. return 0;
  665. return HZ;
  666. }
  667. void bnxt_ptp_free_txts_skbs(struct bnxt_ptp_cfg *ptp)
  668. {
  669. struct bnxt_ptp_tx_req *txts_req;
  670. u16 cons = ptp->txts_cons;
  671. /* make sure ptp aux worker finished with
  672. * possible BNXT_STATE_OPEN set
  673. */
  674. ptp_cancel_worker_sync(ptp->ptp_clock);
  675. ptp->tx_avail = BNXT_MAX_TX_TS;
  676. while (cons != ptp->txts_prod) {
  677. txts_req = &ptp->txts_req[cons];
  678. if (!IS_ERR_OR_NULL(txts_req->tx_skb))
  679. dev_kfree_skb_any(txts_req->tx_skb);
  680. cons = NEXT_TXTS(cons);
  681. }
  682. ptp->txts_cons = cons;
  683. ptp_schedule_worker(ptp->ptp_clock, 0);
  684. }
  685. int bnxt_ptp_get_txts_prod(struct bnxt_ptp_cfg *ptp, u16 *prod)
  686. {
  687. spin_lock_bh(&ptp->ptp_tx_lock);
  688. if (ptp->tx_avail) {
  689. *prod = ptp->txts_prod;
  690. ptp->txts_prod = NEXT_TXTS(*prod);
  691. ptp->tx_avail--;
  692. spin_unlock_bh(&ptp->ptp_tx_lock);
  693. return 0;
  694. }
  695. spin_unlock_bh(&ptp->ptp_tx_lock);
  696. atomic64_inc(&ptp->stats.ts_err);
  697. return -ENOSPC;
  698. }
  699. void bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb, u16 prod)
  700. {
  701. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  702. struct bnxt_ptp_tx_req *txts_req;
  703. txts_req = &ptp->txts_req[prod];
  704. txts_req->abs_txts_tmo = jiffies + msecs_to_jiffies(ptp->txts_tmo);
  705. /* make sure abs_txts_tmo is written first */
  706. smp_wmb();
  707. txts_req->tx_skb = skb;
  708. ptp_schedule_worker(ptp->ptp_clock, 0);
  709. }
  710. int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts)
  711. {
  712. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  713. u64 time;
  714. if (!ptp)
  715. return -ENODEV;
  716. BNXT_READ_TIME64(ptp, time, ptp->old_time);
  717. *ts = (time & BNXT_HI_TIMER_MASK) | pkt_ts;
  718. if (pkt_ts < (time & BNXT_LO_TIMER_MASK))
  719. *ts += BNXT_LO_TIMER_MASK + 1;
  720. return 0;
  721. }
  722. void bnxt_tx_ts_cmp(struct bnxt *bp, struct bnxt_napi *bnapi,
  723. struct tx_ts_cmp *tscmp)
  724. {
  725. struct skb_shared_hwtstamps timestamp = {};
  726. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  727. u32 opaque = tscmp->tx_ts_cmp_opaque;
  728. struct bnxt_tx_ring_info *txr;
  729. struct bnxt_sw_tx_bd *tx_buf;
  730. unsigned long flags;
  731. u64 ts, ns;
  732. u16 cons;
  733. txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
  734. ts = BNXT_GET_TX_TS_48B_NS(tscmp);
  735. cons = TX_OPAQUE_IDX(opaque);
  736. tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
  737. if (tx_buf->is_ts_pkt) {
  738. if (BNXT_TX_TS_ERR(tscmp)) {
  739. netdev_err(bp->dev,
  740. "timestamp completion error 0x%x 0x%x\n",
  741. le32_to_cpu(tscmp->tx_ts_cmp_flags_type),
  742. le32_to_cpu(tscmp->tx_ts_cmp_errors_v));
  743. } else {
  744. spin_lock_irqsave(&ptp->ptp_lock, flags);
  745. ns = timecounter_cyc2time(&ptp->tc, ts);
  746. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  747. timestamp.hwtstamp = ns_to_ktime(ns);
  748. skb_tstamp_tx(tx_buf->skb, &timestamp);
  749. }
  750. tx_buf->is_ts_pkt = 0;
  751. }
  752. }
  753. static const struct ptp_clock_info bnxt_ptp_caps = {
  754. .owner = THIS_MODULE,
  755. .name = "bnxt clock",
  756. .max_adj = BNXT_MAX_PHC_DRIFT,
  757. .n_alarm = 0,
  758. .n_ext_ts = 0,
  759. .n_per_out = 0,
  760. .n_pins = 0,
  761. .pps = 0,
  762. .adjfine = bnxt_ptp_adjfine,
  763. .adjtime = bnxt_ptp_adjtime,
  764. .do_aux_work = bnxt_ptp_ts_aux_work,
  765. .gettimex64 = bnxt_ptp_gettimex,
  766. .settime64 = bnxt_ptp_settime,
  767. .enable = bnxt_ptp_enable,
  768. };
  769. static int bnxt_ptp_verify(struct ptp_clock_info *ptp_info, unsigned int pin,
  770. enum ptp_pin_function func, unsigned int chan)
  771. {
  772. struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
  773. ptp_info);
  774. /* Allow only PPS pin function configuration */
  775. if (ptp->pps_info.pins[pin].usage <= BNXT_PPS_PIN_PPS_OUT &&
  776. func != PTP_PF_PHYSYNC)
  777. return 0;
  778. else
  779. return -EOPNOTSUPP;
  780. }
  781. static int bnxt_ptp_pps_init(struct bnxt *bp)
  782. {
  783. struct hwrm_func_ptp_pin_qcfg_output *resp;
  784. struct hwrm_func_ptp_pin_qcfg_input *req;
  785. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  786. struct ptp_clock_info *ptp_info;
  787. struct bnxt_pps *pps_info;
  788. u8 *pin_usg;
  789. u32 i, rc;
  790. /* Query current/default PIN CFG */
  791. rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_PIN_QCFG);
  792. if (rc)
  793. return rc;
  794. resp = hwrm_req_hold(bp, req);
  795. rc = hwrm_req_send(bp, req);
  796. if (rc || !resp->num_pins) {
  797. hwrm_req_drop(bp, req);
  798. return -EOPNOTSUPP;
  799. }
  800. ptp_info = &ptp->ptp_info;
  801. pps_info = &ptp->pps_info;
  802. pps_info->num_pins = resp->num_pins;
  803. ptp_info->n_pins = pps_info->num_pins;
  804. ptp_info->pin_config = kcalloc(ptp_info->n_pins,
  805. sizeof(*ptp_info->pin_config),
  806. GFP_KERNEL);
  807. if (!ptp_info->pin_config) {
  808. hwrm_req_drop(bp, req);
  809. return -ENOMEM;
  810. }
  811. /* Report the TSIO capability to kernel */
  812. pin_usg = &resp->pin0_usage;
  813. for (i = 0; i < pps_info->num_pins; i++, pin_usg++) {
  814. snprintf(ptp_info->pin_config[i].name,
  815. sizeof(ptp_info->pin_config[i].name), "bnxt_pps%d", i);
  816. ptp_info->pin_config[i].index = i;
  817. ptp_info->pin_config[i].chan = i;
  818. if (*pin_usg == BNXT_PPS_PIN_PPS_IN)
  819. ptp_info->pin_config[i].func = PTP_PF_EXTTS;
  820. else if (*pin_usg == BNXT_PPS_PIN_PPS_OUT)
  821. ptp_info->pin_config[i].func = PTP_PF_PEROUT;
  822. else
  823. ptp_info->pin_config[i].func = PTP_PF_NONE;
  824. pps_info->pins[i].usage = *pin_usg;
  825. }
  826. hwrm_req_drop(bp, req);
  827. /* Only 1 each of ext_ts and per_out pins is available in HW */
  828. ptp_info->n_ext_ts = 1;
  829. ptp_info->n_per_out = 1;
  830. ptp_info->pps = 1;
  831. ptp_info->verify = bnxt_ptp_verify;
  832. return 0;
  833. }
  834. static bool bnxt_pps_config_ok(struct bnxt *bp)
  835. {
  836. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  837. return !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) == !ptp->ptp_info.pin_config;
  838. }
  839. static void bnxt_ptp_timecounter_init(struct bnxt *bp, bool init_tc)
  840. {
  841. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  842. if (!ptp->ptp_clock) {
  843. memset(&ptp->cc, 0, sizeof(ptp->cc));
  844. ptp->cc.read = bnxt_cc_read;
  845. ptp->cc.mask = CYCLECOUNTER_MASK(48);
  846. if (BNXT_MH(bp)) {
  847. /* Use timecounter based non-real time mode */
  848. ptp->cc.shift = BNXT_CYCLES_SHIFT;
  849. ptp->cc.mult = clocksource_khz2mult(BNXT_DEVCLK_FREQ, ptp->cc.shift);
  850. ptp->cmult = ptp->cc.mult;
  851. } else {
  852. ptp->cc.shift = 0;
  853. ptp->cc.mult = 1;
  854. }
  855. ptp->next_overflow_check = jiffies + BNXT_PHC_OVERFLOW_PERIOD;
  856. }
  857. if (init_tc)
  858. timecounter_init(&ptp->tc, &ptp->cc, ktime_to_ns(ktime_get_real()));
  859. }
  860. /* Caller holds ptp_lock */
  861. void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns)
  862. {
  863. timecounter_init(&ptp->tc, &ptp->cc, ns);
  864. /* For RTC, cycle_last must be in sync with the timecounter value. */
  865. ptp->tc.cycle_last = ns & ptp->cc.mask;
  866. }
  867. int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg)
  868. {
  869. struct timespec64 tsp;
  870. unsigned long flags;
  871. u64 ns;
  872. int rc;
  873. if (!bp->ptp_cfg || !BNXT_PTP_USE_RTC(bp))
  874. return -ENODEV;
  875. if (!phc_cfg) {
  876. ktime_get_real_ts64(&tsp);
  877. ns = timespec64_to_ns(&tsp);
  878. rc = bnxt_ptp_cfg_settime(bp, ns);
  879. if (rc)
  880. return rc;
  881. } else {
  882. rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_CURRENT_TIME,
  883. &ns, 0, 0);
  884. if (rc)
  885. return rc;
  886. }
  887. spin_lock_irqsave(&bp->ptp_cfg->ptp_lock, flags);
  888. bnxt_ptp_rtc_timecounter_init(bp->ptp_cfg, ns);
  889. spin_unlock_irqrestore(&bp->ptp_cfg->ptp_lock, flags);
  890. return 0;
  891. }
  892. static void bnxt_ptp_free(struct bnxt *bp)
  893. {
  894. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  895. if (ptp->ptp_clock) {
  896. ptp_clock_unregister(ptp->ptp_clock);
  897. ptp->ptp_clock = NULL;
  898. kfree(ptp->ptp_info.pin_config);
  899. ptp->ptp_info.pin_config = NULL;
  900. }
  901. }
  902. int bnxt_ptp_init(struct bnxt *bp)
  903. {
  904. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  905. int rc;
  906. if (!ptp)
  907. return 0;
  908. rc = bnxt_map_ptp_regs(bp);
  909. if (rc)
  910. return rc;
  911. if (ptp->ptp_clock && bnxt_pps_config_ok(bp))
  912. return 0;
  913. bnxt_ptp_free(bp);
  914. WRITE_ONCE(ptp->tx_avail, BNXT_MAX_TX_TS);
  915. spin_lock_init(&ptp->ptp_lock);
  916. spin_lock_init(&ptp->ptp_tx_lock);
  917. if (BNXT_PTP_USE_RTC(bp)) {
  918. bnxt_ptp_timecounter_init(bp, false);
  919. rc = bnxt_ptp_init_rtc(bp, ptp->rtc_configured);
  920. if (rc)
  921. goto out;
  922. } else {
  923. bnxt_ptp_timecounter_init(bp, true);
  924. bnxt_ptp_adjfine_rtc(bp, 0);
  925. }
  926. bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true);
  927. ptp->ptp_info = bnxt_ptp_caps;
  928. if ((bp->fw_cap & BNXT_FW_CAP_PTP_PPS)) {
  929. if (bnxt_ptp_pps_init(bp))
  930. netdev_err(bp->dev, "1pps not initialized, continuing without 1pps support\n");
  931. }
  932. ptp->ptp_clock = ptp_clock_register(&ptp->ptp_info, &bp->pdev->dev);
  933. if (IS_ERR(ptp->ptp_clock)) {
  934. int err = PTR_ERR(ptp->ptp_clock);
  935. ptp->ptp_clock = NULL;
  936. rc = err;
  937. goto out;
  938. }
  939. ptp->stats.ts_pkts = 0;
  940. ptp->stats.ts_lost = 0;
  941. atomic64_set(&ptp->stats.ts_err, 0);
  942. if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
  943. unsigned long flags;
  944. spin_lock_irqsave(&ptp->ptp_lock, flags);
  945. bnxt_refclk_read(bp, NULL, &ptp->current_time);
  946. WRITE_ONCE(ptp->old_time, ptp->current_time);
  947. spin_unlock_irqrestore(&ptp->ptp_lock, flags);
  948. ptp_schedule_worker(ptp->ptp_clock, 0);
  949. }
  950. ptp->txts_tmo = BNXT_PTP_DFLT_TX_TMO;
  951. return 0;
  952. out:
  953. bnxt_ptp_free(bp);
  954. bnxt_unmap_ptp_regs(bp);
  955. return rc;
  956. }
  957. void bnxt_ptp_clear(struct bnxt *bp)
  958. {
  959. struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
  960. if (!ptp)
  961. return;
  962. if (ptp->ptp_clock)
  963. ptp_clock_unregister(ptp->ptp_clock);
  964. ptp->ptp_clock = NULL;
  965. kfree(ptp->ptp_info.pin_config);
  966. ptp->ptp_info.pin_config = NULL;
  967. bnxt_unmap_ptp_regs(bp);
  968. }