eq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/interrupt.h>
  34. #include <linux/mm.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/mlx4/cmd.h>
  37. #include "mlx4.h"
  38. #include "fw.h"
  39. enum {
  40. MLX4_NUM_ASYNC_EQE = 0x100,
  41. MLX4_NUM_SPARE_EQE = 0x80,
  42. MLX4_EQ_ENTRY_SIZE = 0x20
  43. };
  44. /*
  45. * Must be packed because start is 64 bits but only aligned to 32 bits.
  46. */
  47. struct mlx4_eq_context {
  48. __be32 flags;
  49. u16 reserved1[3];
  50. __be16 page_offset;
  51. u8 log_eq_size;
  52. u8 reserved2[4];
  53. u8 eq_period;
  54. u8 reserved3;
  55. u8 eq_max_count;
  56. u8 reserved4[3];
  57. u8 intr;
  58. u8 log_page_size;
  59. u8 reserved5[2];
  60. u8 mtt_base_addr_h;
  61. __be32 mtt_base_addr_l;
  62. u32 reserved6[2];
  63. __be32 consumer_index;
  64. __be32 producer_index;
  65. u32 reserved7[4];
  66. };
  67. #define MLX4_EQ_STATUS_OK ( 0 << 28)
  68. #define MLX4_EQ_STATUS_WRITE_FAIL (10 << 28)
  69. #define MLX4_EQ_OWNER_SW ( 0 << 24)
  70. #define MLX4_EQ_OWNER_HW ( 1 << 24)
  71. #define MLX4_EQ_FLAG_EC ( 1 << 18)
  72. #define MLX4_EQ_FLAG_OI ( 1 << 17)
  73. #define MLX4_EQ_STATE_ARMED ( 9 << 8)
  74. #define MLX4_EQ_STATE_FIRED (10 << 8)
  75. #define MLX4_EQ_STATE_ALWAYS_ARMED (11 << 8)
  76. #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG) | \
  77. (1ull << MLX4_EVENT_TYPE_COMM_EST) | \
  78. (1ull << MLX4_EVENT_TYPE_SQ_DRAINED) | \
  79. (1ull << MLX4_EVENT_TYPE_CQ_ERROR) | \
  80. (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR) | \
  81. (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR) | \
  82. (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED) | \
  83. (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
  84. (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR) | \
  85. (1ull << MLX4_EVENT_TYPE_PORT_CHANGE) | \
  86. (1ull << MLX4_EVENT_TYPE_ECC_DETECT) | \
  87. (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR) | \
  88. (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
  89. (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT) | \
  90. (1ull << MLX4_EVENT_TYPE_CMD))
  91. struct mlx4_eqe {
  92. u8 reserved1;
  93. u8 type;
  94. u8 reserved2;
  95. u8 subtype;
  96. union {
  97. u32 raw[6];
  98. struct {
  99. __be32 cqn;
  100. } __attribute__((packed)) comp;
  101. struct {
  102. u16 reserved1;
  103. __be16 token;
  104. u32 reserved2;
  105. u8 reserved3[3];
  106. u8 status;
  107. __be64 out_param;
  108. } __attribute__((packed)) cmd;
  109. struct {
  110. __be32 qpn;
  111. } __attribute__((packed)) qp;
  112. struct {
  113. __be32 srqn;
  114. } __attribute__((packed)) srq;
  115. struct {
  116. __be32 cqn;
  117. u32 reserved1;
  118. u8 reserved2[3];
  119. u8 syndrome;
  120. } __attribute__((packed)) cq_err;
  121. struct {
  122. u32 reserved1[2];
  123. __be32 port;
  124. } __attribute__((packed)) port_change;
  125. } event;
  126. u8 reserved3[3];
  127. u8 owner;
  128. } __attribute__((packed));
  129. static void eq_set_ci(struct mlx4_eq *eq, int req_not)
  130. {
  131. __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
  132. req_not << 31),
  133. eq->doorbell);
  134. /* We still want ordering, just not swabbing, so add a barrier */
  135. mb();
  136. }
  137. static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
  138. {
  139. unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
  140. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  141. }
  142. static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
  143. {
  144. struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
  145. return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
  146. }
  147. static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
  148. {
  149. struct mlx4_eqe *eqe;
  150. int cqn;
  151. int eqes_found = 0;
  152. int set_ci = 0;
  153. int port;
  154. while ((eqe = next_eqe_sw(eq))) {
  155. /*
  156. * Make sure we read EQ entry contents after we've
  157. * checked the ownership bit.
  158. */
  159. rmb();
  160. switch (eqe->type) {
  161. case MLX4_EVENT_TYPE_COMP:
  162. cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  163. mlx4_cq_completion(dev, cqn);
  164. break;
  165. case MLX4_EVENT_TYPE_PATH_MIG:
  166. case MLX4_EVENT_TYPE_COMM_EST:
  167. case MLX4_EVENT_TYPE_SQ_DRAINED:
  168. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  169. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  170. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  171. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  172. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  173. mlx4_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  174. eqe->type);
  175. break;
  176. case MLX4_EVENT_TYPE_SRQ_LIMIT:
  177. case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
  178. mlx4_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) & 0xffffff,
  179. eqe->type);
  180. break;
  181. case MLX4_EVENT_TYPE_CMD:
  182. mlx4_cmd_event(dev,
  183. be16_to_cpu(eqe->event.cmd.token),
  184. eqe->event.cmd.status,
  185. be64_to_cpu(eqe->event.cmd.out_param));
  186. break;
  187. case MLX4_EVENT_TYPE_PORT_CHANGE:
  188. port = be32_to_cpu(eqe->event.port_change.port) >> 28;
  189. if (eqe->subtype == MLX4_PORT_CHANGE_SUBTYPE_DOWN) {
  190. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_DOWN,
  191. port);
  192. mlx4_priv(dev)->sense.do_sense_port[port] = 1;
  193. } else {
  194. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_UP,
  195. port);
  196. mlx4_priv(dev)->sense.do_sense_port[port] = 0;
  197. }
  198. break;
  199. case MLX4_EVENT_TYPE_CQ_ERROR:
  200. mlx4_warn(dev, "CQ %s on CQN %06x\n",
  201. eqe->event.cq_err.syndrome == 1 ?
  202. "overrun" : "access violation",
  203. be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
  204. mlx4_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
  205. eqe->type);
  206. break;
  207. case MLX4_EVENT_TYPE_EQ_OVERFLOW:
  208. mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
  209. break;
  210. case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
  211. case MLX4_EVENT_TYPE_ECC_DETECT:
  212. default:
  213. mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at index %u\n",
  214. eqe->type, eqe->subtype, eq->eqn, eq->cons_index);
  215. break;
  216. };
  217. ++eq->cons_index;
  218. eqes_found = 1;
  219. ++set_ci;
  220. /*
  221. * The HCA will think the queue has overflowed if we
  222. * don't tell it we've been processing events. We
  223. * create our EQs with MLX4_NUM_SPARE_EQE extra
  224. * entries, so we must update our consumer index at
  225. * least that often.
  226. */
  227. if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
  228. eq_set_ci(eq, 0);
  229. set_ci = 0;
  230. }
  231. }
  232. eq_set_ci(eq, 1);
  233. return eqes_found;
  234. }
  235. static irqreturn_t mlx4_interrupt(int irq, void *dev_ptr)
  236. {
  237. struct mlx4_dev *dev = dev_ptr;
  238. struct mlx4_priv *priv = mlx4_priv(dev);
  239. int work = 0;
  240. int i;
  241. writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
  242. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  243. work |= mlx4_eq_int(dev, &priv->eq_table.eq[i]);
  244. return IRQ_RETVAL(work);
  245. }
  246. static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
  247. {
  248. struct mlx4_eq *eq = eq_ptr;
  249. struct mlx4_dev *dev = eq->dev;
  250. mlx4_eq_int(dev, eq);
  251. /* MSI-X vectors always belong to us */
  252. return IRQ_HANDLED;
  253. }
  254. static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
  255. int eq_num)
  256. {
  257. return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
  258. 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B);
  259. }
  260. static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  261. int eq_num)
  262. {
  263. return mlx4_cmd(dev, mailbox->dma, eq_num, 0, MLX4_CMD_SW2HW_EQ,
  264. MLX4_CMD_TIME_CLASS_A);
  265. }
  266. static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  267. int eq_num)
  268. {
  269. return mlx4_cmd_box(dev, 0, mailbox->dma, eq_num, 0, MLX4_CMD_HW2SW_EQ,
  270. MLX4_CMD_TIME_CLASS_A);
  271. }
  272. static int mlx4_num_eq_uar(struct mlx4_dev *dev)
  273. {
  274. /*
  275. * Each UAR holds 4 EQ doorbells. To figure out how many UARs
  276. * we need to map, take the difference of highest index and
  277. * the lowest index we'll use and add 1.
  278. */
  279. return (dev->caps.num_comp_vectors + 1 + dev->caps.reserved_eqs) / 4 -
  280. dev->caps.reserved_eqs / 4 + 1;
  281. }
  282. static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
  283. {
  284. struct mlx4_priv *priv = mlx4_priv(dev);
  285. int index;
  286. index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
  287. if (!priv->eq_table.uar_map[index]) {
  288. priv->eq_table.uar_map[index] =
  289. ioremap(pci_resource_start(dev->pdev, 2) +
  290. ((eq->eqn / 4) << PAGE_SHIFT),
  291. PAGE_SIZE);
  292. if (!priv->eq_table.uar_map[index]) {
  293. mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
  294. eq->eqn);
  295. return NULL;
  296. }
  297. }
  298. return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
  299. }
  300. static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
  301. u8 intr, struct mlx4_eq *eq)
  302. {
  303. struct mlx4_priv *priv = mlx4_priv(dev);
  304. struct mlx4_cmd_mailbox *mailbox;
  305. struct mlx4_eq_context *eq_context;
  306. int npages;
  307. u64 *dma_list = NULL;
  308. dma_addr_t t;
  309. u64 mtt_addr;
  310. int err = -ENOMEM;
  311. int i;
  312. eq->dev = dev;
  313. eq->nent = roundup_pow_of_two(max(nent, 2));
  314. npages = PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE;
  315. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  316. GFP_KERNEL);
  317. if (!eq->page_list)
  318. goto err_out;
  319. for (i = 0; i < npages; ++i)
  320. eq->page_list[i].buf = NULL;
  321. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  322. if (!dma_list)
  323. goto err_out_free;
  324. mailbox = mlx4_alloc_cmd_mailbox(dev);
  325. if (IS_ERR(mailbox))
  326. goto err_out_free;
  327. eq_context = mailbox->buf;
  328. for (i = 0; i < npages; ++i) {
  329. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  330. PAGE_SIZE, &t, GFP_KERNEL);
  331. if (!eq->page_list[i].buf)
  332. goto err_out_free_pages;
  333. dma_list[i] = t;
  334. eq->page_list[i].map = t;
  335. memset(eq->page_list[i].buf, 0, PAGE_SIZE);
  336. }
  337. eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
  338. if (eq->eqn == -1)
  339. goto err_out_free_pages;
  340. eq->doorbell = mlx4_get_eq_uar(dev, eq);
  341. if (!eq->doorbell) {
  342. err = -ENOMEM;
  343. goto err_out_free_eq;
  344. }
  345. err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
  346. if (err)
  347. goto err_out_free_eq;
  348. err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
  349. if (err)
  350. goto err_out_free_mtt;
  351. memset(eq_context, 0, sizeof *eq_context);
  352. eq_context->flags = cpu_to_be32(MLX4_EQ_STATUS_OK |
  353. MLX4_EQ_STATE_ARMED);
  354. eq_context->log_eq_size = ilog2(eq->nent);
  355. eq_context->intr = intr;
  356. eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
  357. mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
  358. eq_context->mtt_base_addr_h = mtt_addr >> 32;
  359. eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  360. err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
  361. if (err) {
  362. mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
  363. goto err_out_free_mtt;
  364. }
  365. kfree(dma_list);
  366. mlx4_free_cmd_mailbox(dev, mailbox);
  367. eq->cons_index = 0;
  368. return err;
  369. err_out_free_mtt:
  370. mlx4_mtt_cleanup(dev, &eq->mtt);
  371. err_out_free_eq:
  372. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  373. err_out_free_pages:
  374. for (i = 0; i < npages; ++i)
  375. if (eq->page_list[i].buf)
  376. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  377. eq->page_list[i].buf,
  378. eq->page_list[i].map);
  379. mlx4_free_cmd_mailbox(dev, mailbox);
  380. err_out_free:
  381. kfree(eq->page_list);
  382. kfree(dma_list);
  383. err_out:
  384. return err;
  385. }
  386. static void mlx4_free_eq(struct mlx4_dev *dev,
  387. struct mlx4_eq *eq)
  388. {
  389. struct mlx4_priv *priv = mlx4_priv(dev);
  390. struct mlx4_cmd_mailbox *mailbox;
  391. int err;
  392. int npages = PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE;
  393. int i;
  394. mailbox = mlx4_alloc_cmd_mailbox(dev);
  395. if (IS_ERR(mailbox))
  396. return;
  397. err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
  398. if (err)
  399. mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
  400. if (0) {
  401. mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  402. for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
  403. if (i % 4 == 0)
  404. printk("[%02x] ", i * 4);
  405. printk(" %08x", be32_to_cpup(mailbox->buf + i * 4));
  406. if ((i + 1) % 4 == 0)
  407. printk("\n");
  408. }
  409. }
  410. mlx4_mtt_cleanup(dev, &eq->mtt);
  411. for (i = 0; i < npages; ++i)
  412. pci_free_consistent(dev->pdev, PAGE_SIZE,
  413. eq->page_list[i].buf,
  414. eq->page_list[i].map);
  415. kfree(eq->page_list);
  416. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  417. mlx4_free_cmd_mailbox(dev, mailbox);
  418. }
  419. static void mlx4_free_irqs(struct mlx4_dev *dev)
  420. {
  421. struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
  422. int i;
  423. if (eq_table->have_irq)
  424. free_irq(dev->pdev->irq, dev);
  425. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  426. if (eq_table->eq[i].have_irq) {
  427. free_irq(eq_table->eq[i].irq, eq_table->eq + i);
  428. eq_table->eq[i].have_irq = 0;
  429. }
  430. kfree(eq_table->irq_names);
  431. }
  432. static int mlx4_map_clr_int(struct mlx4_dev *dev)
  433. {
  434. struct mlx4_priv *priv = mlx4_priv(dev);
  435. priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
  436. priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
  437. if (!priv->clr_base) {
  438. mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
  439. return -ENOMEM;
  440. }
  441. return 0;
  442. }
  443. static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
  444. {
  445. struct mlx4_priv *priv = mlx4_priv(dev);
  446. iounmap(priv->clr_base);
  447. }
  448. int mlx4_map_eq_icm(struct mlx4_dev *dev, u64 icm_virt)
  449. {
  450. struct mlx4_priv *priv = mlx4_priv(dev);
  451. int ret;
  452. /*
  453. * We assume that mapping one page is enough for the whole EQ
  454. * context table. This is fine with all current HCAs, because
  455. * we only use 32 EQs and each EQ uses 64 bytes of context
  456. * memory, or 1 KB total.
  457. */
  458. priv->eq_table.icm_virt = icm_virt;
  459. priv->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
  460. if (!priv->eq_table.icm_page)
  461. return -ENOMEM;
  462. priv->eq_table.icm_dma = pci_map_page(dev->pdev, priv->eq_table.icm_page, 0,
  463. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  464. if (pci_dma_mapping_error(dev->pdev, priv->eq_table.icm_dma)) {
  465. __free_page(priv->eq_table.icm_page);
  466. return -ENOMEM;
  467. }
  468. ret = mlx4_MAP_ICM_page(dev, priv->eq_table.icm_dma, icm_virt);
  469. if (ret) {
  470. pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
  471. PCI_DMA_BIDIRECTIONAL);
  472. __free_page(priv->eq_table.icm_page);
  473. }
  474. return ret;
  475. }
  476. void mlx4_unmap_eq_icm(struct mlx4_dev *dev)
  477. {
  478. struct mlx4_priv *priv = mlx4_priv(dev);
  479. mlx4_UNMAP_ICM(dev, priv->eq_table.icm_virt, 1);
  480. pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
  481. PCI_DMA_BIDIRECTIONAL);
  482. __free_page(priv->eq_table.icm_page);
  483. }
  484. int mlx4_alloc_eq_table(struct mlx4_dev *dev)
  485. {
  486. struct mlx4_priv *priv = mlx4_priv(dev);
  487. priv->eq_table.eq = kcalloc(dev->caps.num_eqs - dev->caps.reserved_eqs,
  488. sizeof *priv->eq_table.eq, GFP_KERNEL);
  489. if (!priv->eq_table.eq)
  490. return -ENOMEM;
  491. return 0;
  492. }
  493. void mlx4_free_eq_table(struct mlx4_dev *dev)
  494. {
  495. kfree(mlx4_priv(dev)->eq_table.eq);
  496. }
  497. int mlx4_init_eq_table(struct mlx4_dev *dev)
  498. {
  499. struct mlx4_priv *priv = mlx4_priv(dev);
  500. int err;
  501. int i;
  502. priv->eq_table.uar_map = kcalloc(sizeof *priv->eq_table.uar_map,
  503. mlx4_num_eq_uar(dev), GFP_KERNEL);
  504. if (!priv->eq_table.uar_map) {
  505. err = -ENOMEM;
  506. goto err_out_free;
  507. }
  508. err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
  509. dev->caps.num_eqs - 1, dev->caps.reserved_eqs, 0);
  510. if (err)
  511. goto err_out_free;
  512. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  513. priv->eq_table.uar_map[i] = NULL;
  514. err = mlx4_map_clr_int(dev);
  515. if (err)
  516. goto err_out_bitmap;
  517. priv->eq_table.clr_mask =
  518. swab32(1 << (priv->eq_table.inta_pin & 31));
  519. priv->eq_table.clr_int = priv->clr_base +
  520. (priv->eq_table.inta_pin < 32 ? 4 : 0);
  521. priv->eq_table.irq_names = kmalloc(16 * dev->caps.num_comp_vectors, GFP_KERNEL);
  522. if (!priv->eq_table.irq_names) {
  523. err = -ENOMEM;
  524. goto err_out_bitmap;
  525. }
  526. for (i = 0; i < dev->caps.num_comp_vectors; ++i) {
  527. err = mlx4_create_eq(dev, dev->caps.num_cqs + MLX4_NUM_SPARE_EQE,
  528. (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
  529. &priv->eq_table.eq[i]);
  530. if (err) {
  531. --i;
  532. goto err_out_unmap;
  533. }
  534. }
  535. err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
  536. (dev->flags & MLX4_FLAG_MSI_X) ? dev->caps.num_comp_vectors : 0,
  537. &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  538. if (err)
  539. goto err_out_comp;
  540. if (dev->flags & MLX4_FLAG_MSI_X) {
  541. static const char async_eq_name[] = "mlx4-async";
  542. const char *eq_name;
  543. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i) {
  544. if (i < dev->caps.num_comp_vectors) {
  545. snprintf(priv->eq_table.irq_names + i * 16, 16,
  546. "mlx4-comp-%d", i);
  547. eq_name = priv->eq_table.irq_names + i * 16;
  548. } else
  549. eq_name = async_eq_name;
  550. err = request_irq(priv->eq_table.eq[i].irq,
  551. mlx4_msi_x_interrupt, 0, eq_name,
  552. priv->eq_table.eq + i);
  553. if (err)
  554. goto err_out_async;
  555. priv->eq_table.eq[i].have_irq = 1;
  556. }
  557. } else {
  558. err = request_irq(dev->pdev->irq, mlx4_interrupt,
  559. IRQF_SHARED, DRV_NAME, dev);
  560. if (err)
  561. goto err_out_async;
  562. priv->eq_table.have_irq = 1;
  563. }
  564. err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  565. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  566. if (err)
  567. mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  568. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn, err);
  569. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  570. eq_set_ci(&priv->eq_table.eq[i], 1);
  571. return 0;
  572. err_out_async:
  573. mlx4_free_eq(dev, &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  574. err_out_comp:
  575. i = dev->caps.num_comp_vectors - 1;
  576. err_out_unmap:
  577. while (i >= 0) {
  578. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  579. --i;
  580. }
  581. mlx4_unmap_clr_int(dev);
  582. mlx4_free_irqs(dev);
  583. err_out_bitmap:
  584. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  585. err_out_free:
  586. kfree(priv->eq_table.uar_map);
  587. return err;
  588. }
  589. void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
  590. {
  591. struct mlx4_priv *priv = mlx4_priv(dev);
  592. int i;
  593. mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
  594. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  595. mlx4_free_irqs(dev);
  596. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  597. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  598. mlx4_unmap_clr_int(dev);
  599. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  600. if (priv->eq_table.uar_map[i])
  601. iounmap(priv->eq_table.uar_map[i]);
  602. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  603. kfree(priv->eq_table.uar_map);
  604. }