ionic_main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/printk.h>
  4. #include <linux/dynamic_debug.h>
  5. #include <linux/module.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/utsname.h>
  8. #include <generated/utsrelease.h>
  9. #include <linux/ctype.h>
  10. #include "ionic.h"
  11. #include "ionic_bus.h"
  12. #include "ionic_lif.h"
  13. #include "ionic_debugfs.h"
  14. MODULE_DESCRIPTION(IONIC_DRV_DESCRIPTION);
  15. MODULE_AUTHOR("Shannon Nelson <shannon.nelson@amd.com>");
  16. MODULE_LICENSE("GPL");
  17. static const char *ionic_error_to_str(enum ionic_status_code code)
  18. {
  19. switch (code) {
  20. case IONIC_RC_SUCCESS:
  21. return "IONIC_RC_SUCCESS";
  22. case IONIC_RC_EVERSION:
  23. return "IONIC_RC_EVERSION";
  24. case IONIC_RC_EOPCODE:
  25. return "IONIC_RC_EOPCODE";
  26. case IONIC_RC_EIO:
  27. return "IONIC_RC_EIO";
  28. case IONIC_RC_EPERM:
  29. return "IONIC_RC_EPERM";
  30. case IONIC_RC_EQID:
  31. return "IONIC_RC_EQID";
  32. case IONIC_RC_EQTYPE:
  33. return "IONIC_RC_EQTYPE";
  34. case IONIC_RC_ENOENT:
  35. return "IONIC_RC_ENOENT";
  36. case IONIC_RC_EINTR:
  37. return "IONIC_RC_EINTR";
  38. case IONIC_RC_EAGAIN:
  39. return "IONIC_RC_EAGAIN";
  40. case IONIC_RC_ENOMEM:
  41. return "IONIC_RC_ENOMEM";
  42. case IONIC_RC_EFAULT:
  43. return "IONIC_RC_EFAULT";
  44. case IONIC_RC_EBUSY:
  45. return "IONIC_RC_EBUSY";
  46. case IONIC_RC_EEXIST:
  47. return "IONIC_RC_EEXIST";
  48. case IONIC_RC_EINVAL:
  49. return "IONIC_RC_EINVAL";
  50. case IONIC_RC_ENOSPC:
  51. return "IONIC_RC_ENOSPC";
  52. case IONIC_RC_ERANGE:
  53. return "IONIC_RC_ERANGE";
  54. case IONIC_RC_BAD_ADDR:
  55. return "IONIC_RC_BAD_ADDR";
  56. case IONIC_RC_DEV_CMD:
  57. return "IONIC_RC_DEV_CMD";
  58. case IONIC_RC_ENOSUPP:
  59. return "IONIC_RC_ENOSUPP";
  60. case IONIC_RC_ERROR:
  61. return "IONIC_RC_ERROR";
  62. case IONIC_RC_ERDMA:
  63. return "IONIC_RC_ERDMA";
  64. case IONIC_RC_EBAD_FW:
  65. return "IONIC_RC_EBAD_FW";
  66. default:
  67. return "IONIC_RC_UNKNOWN";
  68. }
  69. }
  70. static int ionic_error_to_errno(enum ionic_status_code code)
  71. {
  72. switch (code) {
  73. case IONIC_RC_SUCCESS:
  74. return 0;
  75. case IONIC_RC_EVERSION:
  76. case IONIC_RC_EQTYPE:
  77. case IONIC_RC_EQID:
  78. case IONIC_RC_EINVAL:
  79. case IONIC_RC_ENOSUPP:
  80. return -EINVAL;
  81. case IONIC_RC_EPERM:
  82. return -EPERM;
  83. case IONIC_RC_ENOENT:
  84. return -ENOENT;
  85. case IONIC_RC_EAGAIN:
  86. return -EAGAIN;
  87. case IONIC_RC_ENOMEM:
  88. return -ENOMEM;
  89. case IONIC_RC_EFAULT:
  90. return -EFAULT;
  91. case IONIC_RC_EBUSY:
  92. return -EBUSY;
  93. case IONIC_RC_EEXIST:
  94. return -EEXIST;
  95. case IONIC_RC_ENOSPC:
  96. return -ENOSPC;
  97. case IONIC_RC_ERANGE:
  98. return -ERANGE;
  99. case IONIC_RC_BAD_ADDR:
  100. return -EFAULT;
  101. case IONIC_RC_EOPCODE:
  102. case IONIC_RC_EINTR:
  103. case IONIC_RC_DEV_CMD:
  104. case IONIC_RC_ERROR:
  105. case IONIC_RC_ERDMA:
  106. case IONIC_RC_EIO:
  107. default:
  108. return -EIO;
  109. }
  110. }
  111. static const char *ionic_opcode_to_str(enum ionic_cmd_opcode opcode)
  112. {
  113. switch (opcode) {
  114. case IONIC_CMD_NOP:
  115. return "IONIC_CMD_NOP";
  116. case IONIC_CMD_INIT:
  117. return "IONIC_CMD_INIT";
  118. case IONIC_CMD_RESET:
  119. return "IONIC_CMD_RESET";
  120. case IONIC_CMD_IDENTIFY:
  121. return "IONIC_CMD_IDENTIFY";
  122. case IONIC_CMD_GETATTR:
  123. return "IONIC_CMD_GETATTR";
  124. case IONIC_CMD_SETATTR:
  125. return "IONIC_CMD_SETATTR";
  126. case IONIC_CMD_PORT_IDENTIFY:
  127. return "IONIC_CMD_PORT_IDENTIFY";
  128. case IONIC_CMD_PORT_INIT:
  129. return "IONIC_CMD_PORT_INIT";
  130. case IONIC_CMD_PORT_RESET:
  131. return "IONIC_CMD_PORT_RESET";
  132. case IONIC_CMD_PORT_GETATTR:
  133. return "IONIC_CMD_PORT_GETATTR";
  134. case IONIC_CMD_PORT_SETATTR:
  135. return "IONIC_CMD_PORT_SETATTR";
  136. case IONIC_CMD_LIF_INIT:
  137. return "IONIC_CMD_LIF_INIT";
  138. case IONIC_CMD_LIF_RESET:
  139. return "IONIC_CMD_LIF_RESET";
  140. case IONIC_CMD_LIF_IDENTIFY:
  141. return "IONIC_CMD_LIF_IDENTIFY";
  142. case IONIC_CMD_LIF_SETATTR:
  143. return "IONIC_CMD_LIF_SETATTR";
  144. case IONIC_CMD_LIF_GETATTR:
  145. return "IONIC_CMD_LIF_GETATTR";
  146. case IONIC_CMD_LIF_SETPHC:
  147. return "IONIC_CMD_LIF_SETPHC";
  148. case IONIC_CMD_RX_MODE_SET:
  149. return "IONIC_CMD_RX_MODE_SET";
  150. case IONIC_CMD_RX_FILTER_ADD:
  151. return "IONIC_CMD_RX_FILTER_ADD";
  152. case IONIC_CMD_RX_FILTER_DEL:
  153. return "IONIC_CMD_RX_FILTER_DEL";
  154. case IONIC_CMD_Q_IDENTIFY:
  155. return "IONIC_CMD_Q_IDENTIFY";
  156. case IONIC_CMD_Q_INIT:
  157. return "IONIC_CMD_Q_INIT";
  158. case IONIC_CMD_Q_CONTROL:
  159. return "IONIC_CMD_Q_CONTROL";
  160. case IONIC_CMD_RDMA_RESET_LIF:
  161. return "IONIC_CMD_RDMA_RESET_LIF";
  162. case IONIC_CMD_RDMA_CREATE_EQ:
  163. return "IONIC_CMD_RDMA_CREATE_EQ";
  164. case IONIC_CMD_RDMA_CREATE_CQ:
  165. return "IONIC_CMD_RDMA_CREATE_CQ";
  166. case IONIC_CMD_RDMA_CREATE_ADMINQ:
  167. return "IONIC_CMD_RDMA_CREATE_ADMINQ";
  168. case IONIC_CMD_FW_DOWNLOAD:
  169. return "IONIC_CMD_FW_DOWNLOAD";
  170. case IONIC_CMD_FW_CONTROL:
  171. return "IONIC_CMD_FW_CONTROL";
  172. case IONIC_CMD_FW_DOWNLOAD_V1:
  173. return "IONIC_CMD_FW_DOWNLOAD_V1";
  174. case IONIC_CMD_FW_CONTROL_V1:
  175. return "IONIC_CMD_FW_CONTROL_V1";
  176. case IONIC_CMD_VF_GETATTR:
  177. return "IONIC_CMD_VF_GETATTR";
  178. case IONIC_CMD_VF_SETATTR:
  179. return "IONIC_CMD_VF_SETATTR";
  180. default:
  181. return "DEVCMD_UNKNOWN";
  182. }
  183. }
  184. static void ionic_adminq_flush(struct ionic_lif *lif)
  185. {
  186. struct ionic_admin_desc_info *desc_info;
  187. struct ionic_admin_cmd *desc;
  188. unsigned long irqflags;
  189. struct ionic_queue *q;
  190. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  191. if (!lif->adminqcq) {
  192. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  193. return;
  194. }
  195. q = &lif->adminqcq->q;
  196. while (q->tail_idx != q->head_idx) {
  197. desc = &q->adminq[q->tail_idx];
  198. desc_info = &q->admin_info[q->tail_idx];
  199. memset(desc, 0, sizeof(union ionic_adminq_cmd));
  200. desc_info->ctx = NULL;
  201. q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
  202. }
  203. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  204. }
  205. void ionic_adminq_netdev_err_print(struct ionic_lif *lif, u8 opcode,
  206. u8 status, int err)
  207. {
  208. const char *stat_str;
  209. stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
  210. ionic_error_to_str(status);
  211. netdev_err(lif->netdev, "%s (%d) failed: %s (%d)\n",
  212. ionic_opcode_to_str(opcode), opcode, stat_str, err);
  213. }
  214. static int ionic_adminq_check_err(struct ionic_lif *lif,
  215. struct ionic_admin_ctx *ctx,
  216. const bool timeout,
  217. const bool do_msg)
  218. {
  219. int err = 0;
  220. if (ctx->comp.comp.status || timeout) {
  221. err = timeout ? -ETIMEDOUT :
  222. ionic_error_to_errno(ctx->comp.comp.status);
  223. if (do_msg)
  224. ionic_adminq_netdev_err_print(lif, ctx->cmd.cmd.opcode,
  225. ctx->comp.comp.status, err);
  226. if (timeout)
  227. ionic_adminq_flush(lif);
  228. }
  229. return err;
  230. }
  231. bool ionic_notifyq_service(struct ionic_cq *cq)
  232. {
  233. struct ionic_deferred_work *work;
  234. union ionic_notifyq_comp *comp;
  235. struct net_device *netdev;
  236. struct ionic_queue *q;
  237. struct ionic_lif *lif;
  238. u64 eid;
  239. comp = &((union ionic_notifyq_comp *)cq->base)[cq->tail_idx];
  240. q = cq->bound_q;
  241. lif = q->admin_info[0].ctx;
  242. netdev = lif->netdev;
  243. eid = le64_to_cpu(comp->event.eid);
  244. /* Have we run out of new completions to process? */
  245. if ((s64)(eid - lif->last_eid) <= 0)
  246. return false;
  247. lif->last_eid = eid;
  248. dev_dbg(lif->ionic->dev, "notifyq event:\n");
  249. dynamic_hex_dump("event ", DUMP_PREFIX_OFFSET, 16, 1,
  250. comp, sizeof(*comp), true);
  251. switch (le16_to_cpu(comp->event.ecode)) {
  252. case IONIC_EVENT_LINK_CHANGE:
  253. ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
  254. break;
  255. case IONIC_EVENT_RESET:
  256. if (lif->ionic->idev.fw_status_ready &&
  257. !test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
  258. !test_and_set_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
  259. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  260. if (!work) {
  261. netdev_err(lif->netdev, "Reset event dropped\n");
  262. clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
  263. } else {
  264. work->type = IONIC_DW_TYPE_LIF_RESET;
  265. ionic_lif_deferred_enqueue(lif, work);
  266. }
  267. }
  268. break;
  269. default:
  270. netdev_warn(netdev, "Notifyq event ecode=%d eid=%lld\n",
  271. comp->event.ecode, eid);
  272. break;
  273. }
  274. return true;
  275. }
  276. bool ionic_adminq_service(struct ionic_cq *cq)
  277. {
  278. struct ionic_admin_desc_info *desc_info;
  279. struct ionic_queue *q = cq->bound_q;
  280. struct ionic_admin_comp *comp;
  281. u16 index;
  282. comp = &((struct ionic_admin_comp *)cq->base)[cq->tail_idx];
  283. if (!color_match(comp->color, cq->done_color))
  284. return false;
  285. /* check for empty queue */
  286. if (q->tail_idx == q->head_idx)
  287. return false;
  288. do {
  289. desc_info = &q->admin_info[q->tail_idx];
  290. index = q->tail_idx;
  291. q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
  292. if (likely(desc_info->ctx)) {
  293. struct ionic_admin_ctx *ctx = desc_info->ctx;
  294. memcpy(&ctx->comp, comp, sizeof(*comp));
  295. dev_dbg(q->dev, "comp admin queue command:\n");
  296. dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
  297. &ctx->comp, sizeof(ctx->comp), true);
  298. complete_all(&ctx->work);
  299. desc_info->ctx = NULL;
  300. }
  301. } while (index != le16_to_cpu(comp->comp_index));
  302. return true;
  303. }
  304. bool ionic_adminq_poke_doorbell(struct ionic_queue *q)
  305. {
  306. struct ionic_lif *lif = q->lif;
  307. unsigned long now, then, dif;
  308. unsigned long irqflags;
  309. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  310. if (q->tail_idx == q->head_idx) {
  311. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  312. return false;
  313. }
  314. now = READ_ONCE(jiffies);
  315. then = q->dbell_jiffies;
  316. dif = now - then;
  317. if (dif > q->dbell_deadline) {
  318. ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type,
  319. q->dbval | q->head_idx);
  320. q->dbell_jiffies = now;
  321. }
  322. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  323. return true;
  324. }
  325. int ionic_adminq_post(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
  326. {
  327. struct ionic_admin_desc_info *desc_info;
  328. struct ionic_admin_cmd *desc;
  329. unsigned long irqflags;
  330. struct ionic_queue *q;
  331. int err = 0;
  332. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  333. if (!lif->adminqcq) {
  334. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  335. return -EIO;
  336. }
  337. q = &lif->adminqcq->q;
  338. if (!ionic_q_has_space(q, 1)) {
  339. err = -ENOSPC;
  340. goto err_out;
  341. }
  342. err = ionic_heartbeat_check(lif->ionic);
  343. if (err)
  344. goto err_out;
  345. desc_info = &q->admin_info[q->head_idx];
  346. desc_info->ctx = ctx;
  347. desc = &q->adminq[q->head_idx];
  348. memcpy(desc, &ctx->cmd, sizeof(ctx->cmd));
  349. dev_dbg(&lif->netdev->dev, "post admin queue command:\n");
  350. dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
  351. &ctx->cmd, sizeof(ctx->cmd), true);
  352. ionic_q_post(q, true);
  353. err_out:
  354. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  355. return err;
  356. }
  357. int ionic_adminq_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx,
  358. const int err, const bool do_msg)
  359. {
  360. struct net_device *netdev = lif->netdev;
  361. unsigned long time_limit;
  362. unsigned long time_start;
  363. unsigned long time_done;
  364. unsigned long remaining;
  365. const char *name;
  366. name = ionic_opcode_to_str(ctx->cmd.cmd.opcode);
  367. if (err) {
  368. if (do_msg && !test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  369. netdev_err(netdev, "Posting of %s (%d) failed: %d\n",
  370. name, ctx->cmd.cmd.opcode, err);
  371. ctx->comp.comp.status = IONIC_RC_ERROR;
  372. return err;
  373. }
  374. time_start = jiffies;
  375. time_limit = time_start + HZ * (ulong)DEVCMD_TIMEOUT;
  376. do {
  377. remaining = wait_for_completion_timeout(&ctx->work,
  378. IONIC_ADMINQ_TIME_SLICE);
  379. /* check for done */
  380. if (remaining)
  381. break;
  382. /* force a check of FW status and break out if FW reset */
  383. ionic_heartbeat_check(lif->ionic);
  384. if ((test_bit(IONIC_LIF_F_FW_RESET, lif->state) &&
  385. !lif->ionic->idev.fw_status_ready) ||
  386. test_bit(IONIC_LIF_F_FW_STOPPING, lif->state)) {
  387. if (do_msg)
  388. netdev_warn(netdev, "%s (%d) interrupted, FW in reset\n",
  389. name, ctx->cmd.cmd.opcode);
  390. ctx->comp.comp.status = IONIC_RC_ERROR;
  391. return -ENXIO;
  392. }
  393. } while (time_before(jiffies, time_limit));
  394. time_done = jiffies;
  395. dev_dbg(lif->ionic->dev, "%s: elapsed %d msecs\n",
  396. __func__, jiffies_to_msecs(time_done - time_start));
  397. return ionic_adminq_check_err(lif, ctx,
  398. time_after_eq(time_done, time_limit),
  399. do_msg);
  400. }
  401. static int __ionic_adminq_post_wait(struct ionic_lif *lif,
  402. struct ionic_admin_ctx *ctx,
  403. const bool do_msg)
  404. {
  405. int err;
  406. if (!ionic_is_fw_running(&lif->ionic->idev))
  407. return 0;
  408. err = ionic_adminq_post(lif, ctx);
  409. return ionic_adminq_wait(lif, ctx, err, do_msg);
  410. }
  411. int ionic_adminq_post_wait(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
  412. {
  413. return __ionic_adminq_post_wait(lif, ctx, true);
  414. }
  415. int ionic_adminq_post_wait_nomsg(struct ionic_lif *lif, struct ionic_admin_ctx *ctx)
  416. {
  417. return __ionic_adminq_post_wait(lif, ctx, false);
  418. }
  419. static void ionic_dev_cmd_clean(struct ionic *ionic)
  420. {
  421. struct ionic_dev *idev = &ionic->idev;
  422. if (!idev->dev_cmd_regs)
  423. return;
  424. iowrite32(0, &idev->dev_cmd_regs->doorbell);
  425. memset_io(&idev->dev_cmd_regs->cmd, 0, sizeof(idev->dev_cmd_regs->cmd));
  426. }
  427. void ionic_dev_cmd_dev_err_print(struct ionic *ionic, u8 opcode, u8 status,
  428. int err)
  429. {
  430. const char *stat_str;
  431. stat_str = (err == -ETIMEDOUT) ? "TIMEOUT" :
  432. ionic_error_to_str(status);
  433. dev_err(ionic->dev, "DEV_CMD %s (%d) error, %s (%d) failed\n",
  434. ionic_opcode_to_str(opcode), opcode, stat_str, err);
  435. }
  436. static int __ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds,
  437. const bool do_msg)
  438. {
  439. struct ionic_dev *idev = &ionic->idev;
  440. unsigned long start_time;
  441. unsigned long max_wait;
  442. unsigned long duration;
  443. bool fw_up;
  444. int opcode;
  445. bool done;
  446. int err;
  447. /* Wait for dev cmd to complete, retrying if we get EAGAIN,
  448. * but don't wait any longer than max_seconds.
  449. */
  450. max_wait = jiffies + (max_seconds * HZ);
  451. try_again:
  452. done = false;
  453. opcode = idev->opcode;
  454. start_time = jiffies;
  455. for (fw_up = ionic_is_fw_running(idev);
  456. !done && fw_up && time_before(jiffies, max_wait);
  457. fw_up = ionic_is_fw_running(idev)) {
  458. done = ionic_dev_cmd_done(idev);
  459. if (done)
  460. break;
  461. usleep_range(100, 200);
  462. }
  463. duration = jiffies - start_time;
  464. dev_dbg(ionic->dev, "DEVCMD %s (%d) done=%d took %ld secs (%ld jiffies)\n",
  465. ionic_opcode_to_str(opcode), opcode,
  466. done, duration / HZ, duration);
  467. if (!done && !fw_up) {
  468. ionic_dev_cmd_clean(ionic);
  469. dev_warn(ionic->dev, "DEVCMD %s (%d) interrupted - FW is down\n",
  470. ionic_opcode_to_str(opcode), opcode);
  471. return -ENXIO;
  472. }
  473. if (!done && !time_before(jiffies, max_wait)) {
  474. ionic_dev_cmd_clean(ionic);
  475. dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
  476. ionic_opcode_to_str(opcode), opcode, max_seconds);
  477. return -ETIMEDOUT;
  478. }
  479. err = ionic_dev_cmd_status(&ionic->idev);
  480. if (err) {
  481. if (err == IONIC_RC_EAGAIN &&
  482. time_before(jiffies, (max_wait - HZ))) {
  483. dev_dbg(ionic->dev, "DEV_CMD %s (%d), %s (%d) retrying...\n",
  484. ionic_opcode_to_str(opcode), opcode,
  485. ionic_error_to_str(err), err);
  486. iowrite32(0, &idev->dev_cmd_regs->done);
  487. msleep(1000);
  488. iowrite32(1, &idev->dev_cmd_regs->doorbell);
  489. goto try_again;
  490. }
  491. if (!(opcode == IONIC_CMD_FW_CONTROL && err == IONIC_RC_EAGAIN))
  492. if (do_msg)
  493. ionic_dev_cmd_dev_err_print(ionic, opcode, err,
  494. ionic_error_to_errno(err));
  495. return ionic_error_to_errno(err);
  496. }
  497. ionic_dev_cmd_clean(ionic);
  498. return 0;
  499. }
  500. int ionic_dev_cmd_wait(struct ionic *ionic, unsigned long max_seconds)
  501. {
  502. return __ionic_dev_cmd_wait(ionic, max_seconds, true);
  503. }
  504. int ionic_dev_cmd_wait_nomsg(struct ionic *ionic, unsigned long max_seconds)
  505. {
  506. return __ionic_dev_cmd_wait(ionic, max_seconds, false);
  507. }
  508. int ionic_setup(struct ionic *ionic)
  509. {
  510. int err;
  511. err = ionic_dev_setup(ionic);
  512. if (err)
  513. return err;
  514. ionic_reset(ionic);
  515. return 0;
  516. }
  517. int ionic_identify(struct ionic *ionic)
  518. {
  519. struct ionic_identity *ident = &ionic->ident;
  520. struct ionic_dev *idev = &ionic->idev;
  521. size_t sz;
  522. int err;
  523. memset(ident, 0, sizeof(*ident));
  524. ident->drv.os_type = cpu_to_le32(IONIC_OS_TYPE_LINUX);
  525. strscpy(ident->drv.driver_ver_str, UTS_RELEASE,
  526. sizeof(ident->drv.driver_ver_str));
  527. mutex_lock(&ionic->dev_cmd_lock);
  528. sz = min(sizeof(ident->drv), sizeof(idev->dev_cmd_regs->data));
  529. memcpy_toio(&idev->dev_cmd_regs->data, &ident->drv, sz);
  530. ionic_dev_cmd_identify(idev, IONIC_DEV_IDENTITY_VERSION_2);
  531. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  532. if (!err) {
  533. sz = min(sizeof(ident->dev), sizeof(idev->dev_cmd_regs->data));
  534. memcpy_fromio(&ident->dev, &idev->dev_cmd_regs->data, sz);
  535. }
  536. mutex_unlock(&ionic->dev_cmd_lock);
  537. if (err) {
  538. dev_err(ionic->dev, "Cannot identify ionic: %d\n", err);
  539. goto err_out;
  540. }
  541. if (isprint(idev->dev_info.fw_version[0]) &&
  542. isascii(idev->dev_info.fw_version[0]))
  543. dev_info(ionic->dev, "FW: %.*s\n",
  544. (int)(sizeof(idev->dev_info.fw_version) - 1),
  545. idev->dev_info.fw_version);
  546. else
  547. dev_info(ionic->dev, "FW: (invalid string) 0x%02x 0x%02x 0x%02x 0x%02x ...\n",
  548. (u8)idev->dev_info.fw_version[0],
  549. (u8)idev->dev_info.fw_version[1],
  550. (u8)idev->dev_info.fw_version[2],
  551. (u8)idev->dev_info.fw_version[3]);
  552. err = ionic_lif_identify(ionic, IONIC_LIF_TYPE_CLASSIC,
  553. &ionic->ident.lif);
  554. if (err) {
  555. dev_err(ionic->dev, "Cannot identify LIFs: %d\n", err);
  556. goto err_out;
  557. }
  558. return 0;
  559. err_out:
  560. return err;
  561. }
  562. int ionic_init(struct ionic *ionic)
  563. {
  564. struct ionic_dev *idev = &ionic->idev;
  565. int err;
  566. mutex_lock(&ionic->dev_cmd_lock);
  567. ionic_dev_cmd_init(idev);
  568. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  569. mutex_unlock(&ionic->dev_cmd_lock);
  570. return err;
  571. }
  572. int ionic_reset(struct ionic *ionic)
  573. {
  574. struct ionic_dev *idev = &ionic->idev;
  575. int err;
  576. if (!ionic_is_fw_running(idev))
  577. return 0;
  578. mutex_lock(&ionic->dev_cmd_lock);
  579. ionic_dev_cmd_reset(idev);
  580. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  581. mutex_unlock(&ionic->dev_cmd_lock);
  582. return err;
  583. }
  584. int ionic_port_identify(struct ionic *ionic)
  585. {
  586. struct ionic_identity *ident = &ionic->ident;
  587. struct ionic_dev *idev = &ionic->idev;
  588. size_t sz;
  589. int err;
  590. mutex_lock(&ionic->dev_cmd_lock);
  591. ionic_dev_cmd_port_identify(idev);
  592. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  593. if (!err) {
  594. sz = min(sizeof(ident->port), sizeof(idev->dev_cmd_regs->data));
  595. memcpy_fromio(&ident->port, &idev->dev_cmd_regs->data, sz);
  596. }
  597. mutex_unlock(&ionic->dev_cmd_lock);
  598. return err;
  599. }
  600. int ionic_port_init(struct ionic *ionic)
  601. {
  602. struct ionic_identity *ident = &ionic->ident;
  603. struct ionic_dev *idev = &ionic->idev;
  604. size_t sz;
  605. int err;
  606. if (!idev->port_info) {
  607. idev->port_info_sz = ALIGN(sizeof(*idev->port_info), PAGE_SIZE);
  608. idev->port_info = dma_alloc_coherent(ionic->dev,
  609. idev->port_info_sz,
  610. &idev->port_info_pa,
  611. GFP_KERNEL);
  612. if (!idev->port_info)
  613. return -ENOMEM;
  614. }
  615. sz = min(sizeof(ident->port.config), sizeof(idev->dev_cmd_regs->data));
  616. mutex_lock(&ionic->dev_cmd_lock);
  617. memcpy_toio(&idev->dev_cmd_regs->data, &ident->port.config, sz);
  618. ionic_dev_cmd_port_init(idev);
  619. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  620. ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
  621. ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  622. mutex_unlock(&ionic->dev_cmd_lock);
  623. if (err) {
  624. dev_err(ionic->dev, "Failed to init port\n");
  625. dma_free_coherent(ionic->dev, idev->port_info_sz,
  626. idev->port_info, idev->port_info_pa);
  627. idev->port_info = NULL;
  628. idev->port_info_pa = 0;
  629. }
  630. return err;
  631. }
  632. int ionic_port_reset(struct ionic *ionic)
  633. {
  634. struct ionic_dev *idev = &ionic->idev;
  635. int err = 0;
  636. if (!idev->port_info)
  637. return 0;
  638. if (ionic_is_fw_running(idev)) {
  639. mutex_lock(&ionic->dev_cmd_lock);
  640. ionic_dev_cmd_port_reset(idev);
  641. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  642. mutex_unlock(&ionic->dev_cmd_lock);
  643. }
  644. dma_free_coherent(ionic->dev, idev->port_info_sz,
  645. idev->port_info, idev->port_info_pa);
  646. idev->port_info = NULL;
  647. idev->port_info_pa = 0;
  648. return err;
  649. }
  650. static int __init ionic_init_module(void)
  651. {
  652. int ret;
  653. ionic_debugfs_create();
  654. ret = ionic_bus_register_driver();
  655. if (ret)
  656. ionic_debugfs_destroy();
  657. return ret;
  658. }
  659. static void __exit ionic_cleanup_module(void)
  660. {
  661. ionic_bus_unregister_driver();
  662. ionic_debugfs_destroy();
  663. pr_info("%s removed\n", IONIC_DRV_NAME);
  664. }
  665. module_init(ionic_init_module);
  666. module_exit(ionic_cleanup_module);