ionic_dev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/errno.h>
  6. #include <linux/io.h>
  7. #include <linux/slab.h>
  8. #include <linux/etherdevice.h>
  9. #include "ionic.h"
  10. #include "ionic_dev.h"
  11. #include "ionic_lif.h"
  12. static void ionic_watchdog_cb(struct timer_list *t)
  13. {
  14. struct ionic *ionic = from_timer(ionic, t, watchdog_timer);
  15. struct ionic_lif *lif = ionic->lif;
  16. struct ionic_deferred_work *work;
  17. int hb;
  18. mod_timer(&ionic->watchdog_timer,
  19. round_jiffies(jiffies + ionic->watchdog_period));
  20. if (!lif)
  21. return;
  22. hb = ionic_heartbeat_check(ionic);
  23. dev_dbg(ionic->dev, "%s: hb %d running %d UP %d\n",
  24. __func__, hb, netif_running(lif->netdev),
  25. test_bit(IONIC_LIF_F_UP, lif->state));
  26. if (hb >= 0 &&
  27. !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  28. ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
  29. if (test_bit(IONIC_LIF_F_FILTER_SYNC_NEEDED, lif->state) &&
  30. !test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  31. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  32. if (!work) {
  33. netdev_err(lif->netdev, "rxmode change dropped\n");
  34. return;
  35. }
  36. work->type = IONIC_DW_TYPE_RX_MODE;
  37. netdev_dbg(lif->netdev, "deferred: rx_mode\n");
  38. ionic_lif_deferred_enqueue(lif, work);
  39. }
  40. }
  41. static void ionic_napi_schedule_do_softirq(struct napi_struct *napi)
  42. {
  43. local_bh_disable();
  44. napi_schedule(napi);
  45. local_bh_enable();
  46. }
  47. void ionic_doorbell_napi_work(struct work_struct *work)
  48. {
  49. struct ionic_qcq *qcq = container_of(work, struct ionic_qcq,
  50. doorbell_napi_work);
  51. unsigned long now, then, dif;
  52. now = READ_ONCE(jiffies);
  53. then = qcq->q.dbell_jiffies;
  54. dif = now - then;
  55. if (dif > qcq->q.dbell_deadline)
  56. ionic_napi_schedule_do_softirq(&qcq->napi);
  57. }
  58. static int ionic_get_preferred_cpu(struct ionic *ionic,
  59. struct ionic_intr_info *intr)
  60. {
  61. int cpu;
  62. cpu = cpumask_first_and(*intr->affinity_mask, cpu_online_mask);
  63. if (cpu >= nr_cpu_ids)
  64. cpu = cpumask_local_spread(0, dev_to_node(ionic->dev));
  65. return cpu;
  66. }
  67. static void ionic_queue_dbell_napi_work(struct ionic *ionic,
  68. struct ionic_qcq *qcq)
  69. {
  70. int cpu;
  71. if (!(qcq->flags & IONIC_QCQ_F_INTR))
  72. return;
  73. cpu = ionic_get_preferred_cpu(ionic, &qcq->intr);
  74. queue_work_on(cpu, ionic->wq, &qcq->doorbell_napi_work);
  75. }
  76. static void ionic_doorbell_check_dwork(struct work_struct *work)
  77. {
  78. struct ionic *ionic = container_of(work, struct ionic,
  79. doorbell_check_dwork.work);
  80. struct ionic_lif *lif = ionic->lif;
  81. mutex_lock(&lif->queue_lock);
  82. if (test_bit(IONIC_LIF_F_FW_STOPPING, lif->state) ||
  83. test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  84. mutex_unlock(&lif->queue_lock);
  85. return;
  86. }
  87. ionic_napi_schedule_do_softirq(&lif->adminqcq->napi);
  88. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  89. int i;
  90. for (i = 0; i < lif->nxqs; i++) {
  91. ionic_queue_dbell_napi_work(ionic, lif->txqcqs[i]);
  92. ionic_queue_dbell_napi_work(ionic, lif->rxqcqs[i]);
  93. }
  94. if (lif->hwstamp_txq &&
  95. lif->hwstamp_txq->flags & IONIC_QCQ_F_INTR)
  96. ionic_napi_schedule_do_softirq(&lif->hwstamp_txq->napi);
  97. if (lif->hwstamp_rxq &&
  98. lif->hwstamp_rxq->flags & IONIC_QCQ_F_INTR)
  99. ionic_napi_schedule_do_softirq(&lif->hwstamp_rxq->napi);
  100. }
  101. mutex_unlock(&lif->queue_lock);
  102. ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
  103. }
  104. bool ionic_doorbell_wa(struct ionic *ionic)
  105. {
  106. u8 asic_type = ionic->idev.dev_info.asic_type;
  107. return !asic_type || asic_type == IONIC_ASIC_TYPE_ELBA;
  108. }
  109. static int ionic_watchdog_init(struct ionic *ionic)
  110. {
  111. struct ionic_dev *idev = &ionic->idev;
  112. timer_setup(&ionic->watchdog_timer, ionic_watchdog_cb, 0);
  113. ionic->watchdog_period = IONIC_WATCHDOG_SECS * HZ;
  114. /* set times to ensure the first check will proceed */
  115. atomic_long_set(&idev->last_check_time, jiffies - 2 * HZ);
  116. idev->last_hb_time = jiffies - 2 * ionic->watchdog_period;
  117. /* init as ready, so no transition if the first check succeeds */
  118. idev->last_fw_hb = 0;
  119. idev->fw_hb_ready = true;
  120. idev->fw_status_ready = true;
  121. idev->fw_generation = IONIC_FW_STS_F_GENERATION &
  122. ioread8(&idev->dev_info_regs->fw_status);
  123. ionic->wq = alloc_workqueue("%s-wq", WQ_UNBOUND, 0,
  124. dev_name(ionic->dev));
  125. if (!ionic->wq) {
  126. dev_err(ionic->dev, "alloc_workqueue failed");
  127. return -ENOMEM;
  128. }
  129. if (ionic_doorbell_wa(ionic))
  130. INIT_DELAYED_WORK(&ionic->doorbell_check_dwork,
  131. ionic_doorbell_check_dwork);
  132. return 0;
  133. }
  134. void ionic_queue_doorbell_check(struct ionic *ionic, int delay)
  135. {
  136. int cpu;
  137. if (!ionic->lif->doorbell_wa)
  138. return;
  139. cpu = ionic_get_preferred_cpu(ionic, &ionic->lif->adminqcq->intr);
  140. queue_delayed_work_on(cpu, ionic->wq, &ionic->doorbell_check_dwork,
  141. delay);
  142. }
  143. void ionic_init_devinfo(struct ionic *ionic)
  144. {
  145. struct ionic_dev *idev = &ionic->idev;
  146. idev->dev_info.asic_type = ioread8(&idev->dev_info_regs->asic_type);
  147. idev->dev_info.asic_rev = ioread8(&idev->dev_info_regs->asic_rev);
  148. memcpy_fromio(idev->dev_info.fw_version,
  149. idev->dev_info_regs->fw_version,
  150. IONIC_DEVINFO_FWVERS_BUFLEN);
  151. memcpy_fromio(idev->dev_info.serial_num,
  152. idev->dev_info_regs->serial_num,
  153. IONIC_DEVINFO_SERIAL_BUFLEN);
  154. idev->dev_info.fw_version[IONIC_DEVINFO_FWVERS_BUFLEN] = 0;
  155. idev->dev_info.serial_num[IONIC_DEVINFO_SERIAL_BUFLEN] = 0;
  156. dev_dbg(ionic->dev, "fw_version %s\n", idev->dev_info.fw_version);
  157. }
  158. int ionic_dev_setup(struct ionic *ionic)
  159. {
  160. struct ionic_dev_bar *bar = ionic->bars;
  161. unsigned int num_bars = ionic->num_bars;
  162. struct ionic_dev *idev = &ionic->idev;
  163. struct device *dev = ionic->dev;
  164. int size;
  165. u32 sig;
  166. int err;
  167. /* BAR0: dev_cmd and interrupts */
  168. if (num_bars < 1) {
  169. dev_err(dev, "No bars found, aborting\n");
  170. return -EFAULT;
  171. }
  172. if (bar->len < IONIC_BAR0_SIZE) {
  173. dev_err(dev, "Resource bar size %lu too small, aborting\n",
  174. bar->len);
  175. return -EFAULT;
  176. }
  177. idev->dev_info_regs = bar->vaddr + IONIC_BAR0_DEV_INFO_REGS_OFFSET;
  178. idev->dev_cmd_regs = bar->vaddr + IONIC_BAR0_DEV_CMD_REGS_OFFSET;
  179. idev->intr_status = bar->vaddr + IONIC_BAR0_INTR_STATUS_OFFSET;
  180. idev->intr_ctrl = bar->vaddr + IONIC_BAR0_INTR_CTRL_OFFSET;
  181. idev->hwstamp_regs = &idev->dev_info_regs->hwstamp;
  182. sig = ioread32(&idev->dev_info_regs->signature);
  183. if (sig != IONIC_DEV_INFO_SIGNATURE) {
  184. dev_err(dev, "Incompatible firmware signature %x", sig);
  185. return -EFAULT;
  186. }
  187. ionic_init_devinfo(ionic);
  188. /* BAR1: doorbells */
  189. bar++;
  190. if (num_bars < 2) {
  191. dev_err(dev, "Doorbell bar missing, aborting\n");
  192. return -EFAULT;
  193. }
  194. err = ionic_watchdog_init(ionic);
  195. if (err)
  196. return err;
  197. idev->db_pages = bar->vaddr;
  198. idev->phy_db_pages = bar->bus_addr;
  199. /* BAR2: optional controller memory mapping */
  200. bar++;
  201. mutex_init(&idev->cmb_inuse_lock);
  202. if (num_bars < 3 || !ionic->bars[IONIC_PCI_BAR_CMB].len) {
  203. idev->cmb_inuse = NULL;
  204. return 0;
  205. }
  206. idev->phy_cmb_pages = bar->bus_addr;
  207. idev->cmb_npages = bar->len / PAGE_SIZE;
  208. size = BITS_TO_LONGS(idev->cmb_npages) * sizeof(long);
  209. idev->cmb_inuse = kzalloc(size, GFP_KERNEL);
  210. if (!idev->cmb_inuse)
  211. dev_warn(dev, "No memory for CMB, disabling\n");
  212. return 0;
  213. }
  214. void ionic_dev_teardown(struct ionic *ionic)
  215. {
  216. struct ionic_dev *idev = &ionic->idev;
  217. kfree(idev->cmb_inuse);
  218. idev->cmb_inuse = NULL;
  219. idev->phy_cmb_pages = 0;
  220. idev->cmb_npages = 0;
  221. if (ionic->wq) {
  222. destroy_workqueue(ionic->wq);
  223. ionic->wq = NULL;
  224. }
  225. mutex_destroy(&idev->cmb_inuse_lock);
  226. }
  227. /* Devcmd Interface */
  228. static bool __ionic_is_fw_running(struct ionic_dev *idev, u8 *status_ptr)
  229. {
  230. u8 fw_status;
  231. if (!idev->dev_info_regs) {
  232. if (status_ptr)
  233. *status_ptr = 0xff;
  234. return false;
  235. }
  236. fw_status = ioread8(&idev->dev_info_regs->fw_status);
  237. if (status_ptr)
  238. *status_ptr = fw_status;
  239. /* firmware is useful only if the running bit is set and
  240. * fw_status != 0xff (bad PCI read)
  241. */
  242. return (fw_status != 0xff) && (fw_status & IONIC_FW_STS_F_RUNNING);
  243. }
  244. bool ionic_is_fw_running(struct ionic_dev *idev)
  245. {
  246. return __ionic_is_fw_running(idev, NULL);
  247. }
  248. int ionic_heartbeat_check(struct ionic *ionic)
  249. {
  250. unsigned long check_time, last_check_time;
  251. struct ionic_dev *idev = &ionic->idev;
  252. struct ionic_lif *lif = ionic->lif;
  253. bool fw_status_ready = true;
  254. bool fw_hb_ready;
  255. u8 fw_generation;
  256. u8 fw_status;
  257. u32 fw_hb;
  258. /* wait a least one second before testing again */
  259. check_time = jiffies;
  260. last_check_time = atomic_long_read(&idev->last_check_time);
  261. do_check_time:
  262. if (time_before(check_time, last_check_time + HZ))
  263. return 0;
  264. if (!atomic_long_try_cmpxchg_relaxed(&idev->last_check_time,
  265. &last_check_time, check_time)) {
  266. /* if called concurrently, only the first should proceed. */
  267. dev_dbg(ionic->dev, "%s: do_check_time again\n", __func__);
  268. goto do_check_time;
  269. }
  270. /* If fw_status is not ready don't bother with the generation */
  271. if (!__ionic_is_fw_running(idev, &fw_status)) {
  272. fw_status_ready = false;
  273. } else {
  274. fw_generation = fw_status & IONIC_FW_STS_F_GENERATION;
  275. if (idev->fw_generation != fw_generation) {
  276. dev_info(ionic->dev, "FW generation 0x%02x -> 0x%02x\n",
  277. idev->fw_generation, fw_generation);
  278. idev->fw_generation = fw_generation;
  279. /* If the generation changed, the fw status is not
  280. * ready so we need to trigger a fw-down cycle. After
  281. * the down, the next watchdog will see the fw is up
  282. * and the generation value stable, so will trigger
  283. * the fw-up activity.
  284. *
  285. * If we had already moved to FW_RESET from a RESET event,
  286. * it is possible that we never saw the fw_status go to 0,
  287. * so we fake the current idev->fw_status_ready here to
  288. * force the transition and get FW up again.
  289. */
  290. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  291. idev->fw_status_ready = false; /* go to running */
  292. else
  293. fw_status_ready = false; /* go to down */
  294. }
  295. }
  296. dev_dbg(ionic->dev, "fw_status 0x%02x ready %d idev->ready %d last_hb 0x%x state 0x%02lx\n",
  297. fw_status, fw_status_ready, idev->fw_status_ready,
  298. idev->last_fw_hb, lif->state[0]);
  299. /* is this a transition? */
  300. if (fw_status_ready != idev->fw_status_ready &&
  301. !test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
  302. bool trigger = false;
  303. idev->fw_status_ready = fw_status_ready;
  304. if (!fw_status_ready &&
  305. !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
  306. !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
  307. dev_info(ionic->dev, "FW stopped 0x%02x\n", fw_status);
  308. trigger = true;
  309. } else if (fw_status_ready &&
  310. test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  311. dev_info(ionic->dev, "FW running 0x%02x\n", fw_status);
  312. trigger = true;
  313. }
  314. if (trigger) {
  315. struct ionic_deferred_work *work;
  316. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  317. if (work) {
  318. work->type = IONIC_DW_TYPE_LIF_RESET;
  319. work->fw_status = fw_status_ready;
  320. ionic_lif_deferred_enqueue(lif, work);
  321. }
  322. }
  323. }
  324. if (!idev->fw_status_ready)
  325. return -ENXIO;
  326. /* Because of some variability in the actual FW heartbeat, we
  327. * wait longer than the DEVCMD_TIMEOUT before checking again.
  328. */
  329. last_check_time = idev->last_hb_time;
  330. if (time_before(check_time, last_check_time + DEVCMD_TIMEOUT * 2 * HZ))
  331. return 0;
  332. fw_hb = ioread32(&idev->dev_info_regs->fw_heartbeat);
  333. fw_hb_ready = fw_hb != idev->last_fw_hb;
  334. /* early FW version had no heartbeat, so fake it */
  335. if (!fw_hb_ready && !fw_hb)
  336. fw_hb_ready = true;
  337. dev_dbg(ionic->dev, "%s: fw_hb %u last_fw_hb %u ready %u\n",
  338. __func__, fw_hb, idev->last_fw_hb, fw_hb_ready);
  339. idev->last_fw_hb = fw_hb;
  340. /* log a transition */
  341. if (fw_hb_ready != idev->fw_hb_ready) {
  342. idev->fw_hb_ready = fw_hb_ready;
  343. if (!fw_hb_ready)
  344. dev_info(ionic->dev, "FW heartbeat stalled at %d\n", fw_hb);
  345. else
  346. dev_info(ionic->dev, "FW heartbeat restored at %d\n", fw_hb);
  347. }
  348. if (!fw_hb_ready)
  349. return -ENXIO;
  350. idev->last_hb_time = check_time;
  351. return 0;
  352. }
  353. u8 ionic_dev_cmd_status(struct ionic_dev *idev)
  354. {
  355. if (!idev->dev_cmd_regs)
  356. return (u8)PCI_ERROR_RESPONSE;
  357. return ioread8(&idev->dev_cmd_regs->comp.comp.status);
  358. }
  359. bool ionic_dev_cmd_done(struct ionic_dev *idev)
  360. {
  361. if (!idev->dev_cmd_regs)
  362. return false;
  363. return ioread32(&idev->dev_cmd_regs->done) & IONIC_DEV_CMD_DONE;
  364. }
  365. void ionic_dev_cmd_comp(struct ionic_dev *idev, union ionic_dev_cmd_comp *comp)
  366. {
  367. if (!idev->dev_cmd_regs)
  368. return;
  369. memcpy_fromio(comp, &idev->dev_cmd_regs->comp, sizeof(*comp));
  370. }
  371. void ionic_dev_cmd_go(struct ionic_dev *idev, union ionic_dev_cmd *cmd)
  372. {
  373. idev->opcode = cmd->cmd.opcode;
  374. if (!idev->dev_cmd_regs)
  375. return;
  376. memcpy_toio(&idev->dev_cmd_regs->cmd, cmd, sizeof(*cmd));
  377. iowrite32(0, &idev->dev_cmd_regs->done);
  378. iowrite32(1, &idev->dev_cmd_regs->doorbell);
  379. }
  380. /* Device commands */
  381. void ionic_dev_cmd_identify(struct ionic_dev *idev, u8 ver)
  382. {
  383. union ionic_dev_cmd cmd = {
  384. .identify.opcode = IONIC_CMD_IDENTIFY,
  385. .identify.ver = ver,
  386. };
  387. ionic_dev_cmd_go(idev, &cmd);
  388. }
  389. void ionic_dev_cmd_init(struct ionic_dev *idev)
  390. {
  391. union ionic_dev_cmd cmd = {
  392. .init.opcode = IONIC_CMD_INIT,
  393. .init.type = 0,
  394. };
  395. ionic_dev_cmd_go(idev, &cmd);
  396. }
  397. void ionic_dev_cmd_reset(struct ionic_dev *idev)
  398. {
  399. union ionic_dev_cmd cmd = {
  400. .reset.opcode = IONIC_CMD_RESET,
  401. };
  402. ionic_dev_cmd_go(idev, &cmd);
  403. }
  404. /* Port commands */
  405. void ionic_dev_cmd_port_identify(struct ionic_dev *idev)
  406. {
  407. union ionic_dev_cmd cmd = {
  408. .port_init.opcode = IONIC_CMD_PORT_IDENTIFY,
  409. .port_init.index = 0,
  410. };
  411. ionic_dev_cmd_go(idev, &cmd);
  412. }
  413. void ionic_dev_cmd_port_init(struct ionic_dev *idev)
  414. {
  415. union ionic_dev_cmd cmd = {
  416. .port_init.opcode = IONIC_CMD_PORT_INIT,
  417. .port_init.index = 0,
  418. .port_init.info_pa = cpu_to_le64(idev->port_info_pa),
  419. };
  420. ionic_dev_cmd_go(idev, &cmd);
  421. }
  422. void ionic_dev_cmd_port_reset(struct ionic_dev *idev)
  423. {
  424. union ionic_dev_cmd cmd = {
  425. .port_reset.opcode = IONIC_CMD_PORT_RESET,
  426. .port_reset.index = 0,
  427. };
  428. ionic_dev_cmd_go(idev, &cmd);
  429. }
  430. void ionic_dev_cmd_port_state(struct ionic_dev *idev, u8 state)
  431. {
  432. union ionic_dev_cmd cmd = {
  433. .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
  434. .port_setattr.index = 0,
  435. .port_setattr.attr = IONIC_PORT_ATTR_STATE,
  436. .port_setattr.state = state,
  437. };
  438. ionic_dev_cmd_go(idev, &cmd);
  439. }
  440. void ionic_dev_cmd_port_speed(struct ionic_dev *idev, u32 speed)
  441. {
  442. union ionic_dev_cmd cmd = {
  443. .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
  444. .port_setattr.index = 0,
  445. .port_setattr.attr = IONIC_PORT_ATTR_SPEED,
  446. .port_setattr.speed = cpu_to_le32(speed),
  447. };
  448. ionic_dev_cmd_go(idev, &cmd);
  449. }
  450. void ionic_dev_cmd_port_autoneg(struct ionic_dev *idev, u8 an_enable)
  451. {
  452. union ionic_dev_cmd cmd = {
  453. .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
  454. .port_setattr.index = 0,
  455. .port_setattr.attr = IONIC_PORT_ATTR_AUTONEG,
  456. .port_setattr.an_enable = an_enable,
  457. };
  458. ionic_dev_cmd_go(idev, &cmd);
  459. }
  460. void ionic_dev_cmd_port_fec(struct ionic_dev *idev, u8 fec_type)
  461. {
  462. union ionic_dev_cmd cmd = {
  463. .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
  464. .port_setattr.index = 0,
  465. .port_setattr.attr = IONIC_PORT_ATTR_FEC,
  466. .port_setattr.fec_type = fec_type,
  467. };
  468. ionic_dev_cmd_go(idev, &cmd);
  469. }
  470. void ionic_dev_cmd_port_pause(struct ionic_dev *idev, u8 pause_type)
  471. {
  472. union ionic_dev_cmd cmd = {
  473. .port_setattr.opcode = IONIC_CMD_PORT_SETATTR,
  474. .port_setattr.index = 0,
  475. .port_setattr.attr = IONIC_PORT_ATTR_PAUSE,
  476. .port_setattr.pause_type = pause_type,
  477. };
  478. ionic_dev_cmd_go(idev, &cmd);
  479. }
  480. /* VF commands */
  481. int ionic_set_vf_config(struct ionic *ionic, int vf,
  482. struct ionic_vf_setattr_cmd *vfc)
  483. {
  484. union ionic_dev_cmd cmd = {
  485. .vf_setattr.opcode = IONIC_CMD_VF_SETATTR,
  486. .vf_setattr.attr = vfc->attr,
  487. .vf_setattr.vf_index = cpu_to_le16(vf),
  488. };
  489. int err;
  490. memcpy(cmd.vf_setattr.pad, vfc->pad, sizeof(vfc->pad));
  491. mutex_lock(&ionic->dev_cmd_lock);
  492. ionic_dev_cmd_go(&ionic->idev, &cmd);
  493. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  494. mutex_unlock(&ionic->dev_cmd_lock);
  495. return err;
  496. }
  497. void ionic_vf_start(struct ionic *ionic)
  498. {
  499. union ionic_dev_cmd cmd = {
  500. .vf_ctrl.opcode = IONIC_CMD_VF_CTRL,
  501. .vf_ctrl.ctrl_opcode = IONIC_VF_CTRL_START_ALL,
  502. };
  503. if (!(ionic->ident.dev.capabilities & cpu_to_le64(IONIC_DEV_CAP_VF_CTRL)))
  504. return;
  505. ionic_dev_cmd_go(&ionic->idev, &cmd);
  506. ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  507. }
  508. /* LIF commands */
  509. void ionic_dev_cmd_queue_identify(struct ionic_dev *idev,
  510. u16 lif_type, u8 qtype, u8 qver)
  511. {
  512. union ionic_dev_cmd cmd = {
  513. .q_identify.opcode = IONIC_CMD_Q_IDENTIFY,
  514. .q_identify.lif_type = cpu_to_le16(lif_type),
  515. .q_identify.type = qtype,
  516. .q_identify.ver = qver,
  517. };
  518. ionic_dev_cmd_go(idev, &cmd);
  519. }
  520. void ionic_dev_cmd_lif_identify(struct ionic_dev *idev, u8 type, u8 ver)
  521. {
  522. union ionic_dev_cmd cmd = {
  523. .lif_identify.opcode = IONIC_CMD_LIF_IDENTIFY,
  524. .lif_identify.type = type,
  525. .lif_identify.ver = ver,
  526. };
  527. ionic_dev_cmd_go(idev, &cmd);
  528. }
  529. void ionic_dev_cmd_lif_init(struct ionic_dev *idev, u16 lif_index,
  530. dma_addr_t info_pa)
  531. {
  532. union ionic_dev_cmd cmd = {
  533. .lif_init.opcode = IONIC_CMD_LIF_INIT,
  534. .lif_init.index = cpu_to_le16(lif_index),
  535. .lif_init.info_pa = cpu_to_le64(info_pa),
  536. };
  537. ionic_dev_cmd_go(idev, &cmd);
  538. }
  539. void ionic_dev_cmd_lif_reset(struct ionic_dev *idev, u16 lif_index)
  540. {
  541. union ionic_dev_cmd cmd = {
  542. .lif_init.opcode = IONIC_CMD_LIF_RESET,
  543. .lif_init.index = cpu_to_le16(lif_index),
  544. };
  545. ionic_dev_cmd_go(idev, &cmd);
  546. }
  547. void ionic_dev_cmd_adminq_init(struct ionic_dev *idev, struct ionic_qcq *qcq,
  548. u16 lif_index, u16 intr_index)
  549. {
  550. struct ionic_queue *q = &qcq->q;
  551. struct ionic_cq *cq = &qcq->cq;
  552. union ionic_dev_cmd cmd = {
  553. .q_init.opcode = IONIC_CMD_Q_INIT,
  554. .q_init.lif_index = cpu_to_le16(lif_index),
  555. .q_init.type = q->type,
  556. .q_init.ver = qcq->q.lif->qtype_info[q->type].version,
  557. .q_init.index = cpu_to_le32(q->index),
  558. .q_init.flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
  559. IONIC_QINIT_F_ENA),
  560. .q_init.pid = cpu_to_le16(q->pid),
  561. .q_init.intr_index = cpu_to_le16(intr_index),
  562. .q_init.ring_size = ilog2(q->num_descs),
  563. .q_init.ring_base = cpu_to_le64(q->base_pa),
  564. .q_init.cq_ring_base = cpu_to_le64(cq->base_pa),
  565. };
  566. ionic_dev_cmd_go(idev, &cmd);
  567. }
  568. int ionic_db_page_num(struct ionic_lif *lif, int pid)
  569. {
  570. return (lif->hw_index * lif->dbid_count) + pid;
  571. }
  572. int ionic_get_cmb(struct ionic_lif *lif, u32 *pgid, phys_addr_t *pgaddr, int order)
  573. {
  574. struct ionic_dev *idev = &lif->ionic->idev;
  575. int ret;
  576. mutex_lock(&idev->cmb_inuse_lock);
  577. ret = bitmap_find_free_region(idev->cmb_inuse, idev->cmb_npages, order);
  578. mutex_unlock(&idev->cmb_inuse_lock);
  579. if (ret < 0)
  580. return ret;
  581. *pgid = ret;
  582. *pgaddr = idev->phy_cmb_pages + ret * PAGE_SIZE;
  583. return 0;
  584. }
  585. void ionic_put_cmb(struct ionic_lif *lif, u32 pgid, int order)
  586. {
  587. struct ionic_dev *idev = &lif->ionic->idev;
  588. mutex_lock(&idev->cmb_inuse_lock);
  589. bitmap_release_region(idev->cmb_inuse, pgid, order);
  590. mutex_unlock(&idev->cmb_inuse_lock);
  591. }
  592. int ionic_cq_init(struct ionic_lif *lif, struct ionic_cq *cq,
  593. struct ionic_intr_info *intr,
  594. unsigned int num_descs, size_t desc_size)
  595. {
  596. unsigned int ring_size;
  597. if (desc_size == 0 || !is_power_of_2(num_descs))
  598. return -EINVAL;
  599. ring_size = ilog2(num_descs);
  600. if (ring_size < 2 || ring_size > 16)
  601. return -EINVAL;
  602. cq->lif = lif;
  603. cq->bound_intr = intr;
  604. cq->num_descs = num_descs;
  605. cq->desc_size = desc_size;
  606. cq->tail_idx = 0;
  607. cq->done_color = 1;
  608. cq->idev = &lif->ionic->idev;
  609. return 0;
  610. }
  611. unsigned int ionic_cq_service(struct ionic_cq *cq, unsigned int work_to_do,
  612. ionic_cq_cb cb, ionic_cq_done_cb done_cb,
  613. void *done_arg)
  614. {
  615. unsigned int work_done = 0;
  616. if (work_to_do == 0)
  617. return 0;
  618. while (cb(cq)) {
  619. if (cq->tail_idx == cq->num_descs - 1)
  620. cq->done_color = !cq->done_color;
  621. cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
  622. if (++work_done >= work_to_do)
  623. break;
  624. }
  625. if (work_done && done_cb)
  626. done_cb(done_arg);
  627. return work_done;
  628. }
  629. int ionic_q_init(struct ionic_lif *lif, struct ionic_dev *idev,
  630. struct ionic_queue *q, unsigned int index, const char *name,
  631. unsigned int num_descs, size_t desc_size,
  632. size_t sg_desc_size, unsigned int pid)
  633. {
  634. unsigned int ring_size;
  635. if (desc_size == 0 || !is_power_of_2(num_descs))
  636. return -EINVAL;
  637. ring_size = ilog2(num_descs);
  638. if (ring_size < 2 || ring_size > 16)
  639. return -EINVAL;
  640. q->lif = lif;
  641. q->index = index;
  642. q->num_descs = num_descs;
  643. q->desc_size = desc_size;
  644. q->sg_desc_size = sg_desc_size;
  645. q->tail_idx = 0;
  646. q->head_idx = 0;
  647. q->pid = pid;
  648. snprintf(q->name, sizeof(q->name), "L%d-%s%u", lif->index, name, index);
  649. return 0;
  650. }
  651. void ionic_q_post(struct ionic_queue *q, bool ring_doorbell)
  652. {
  653. struct ionic_lif *lif = q->lif;
  654. struct device *dev = q->dev;
  655. q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
  656. dev_dbg(dev, "lif=%d qname=%s qid=%d qtype=%d p_index=%d ringdb=%d\n",
  657. q->lif->index, q->name, q->hw_type, q->hw_index,
  658. q->head_idx, ring_doorbell);
  659. if (ring_doorbell) {
  660. ionic_dbell_ring(lif->kern_dbpage, q->hw_type,
  661. q->dbval | q->head_idx);
  662. q->dbell_jiffies = jiffies;
  663. }
  664. }
  665. bool ionic_q_is_posted(struct ionic_queue *q, unsigned int pos)
  666. {
  667. unsigned int mask, tail, head;
  668. mask = q->num_descs - 1;
  669. tail = q->tail_idx;
  670. head = q->head_idx;
  671. return ((pos - tail) & mask) < ((head - tail) & mask);
  672. }