eq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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/slab.h>
  35. #include <linux/export.h>
  36. #include <linux/mm.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/mlx4/cmd.h>
  39. #include "mlx4.h"
  40. #include "fw.h"
  41. enum {
  42. MLX4_IRQNAME_SIZE = 32
  43. };
  44. enum {
  45. MLX4_NUM_ASYNC_EQE = 0x100,
  46. MLX4_NUM_SPARE_EQE = 0x80,
  47. MLX4_EQ_ENTRY_SIZE = 0x20
  48. };
  49. /*
  50. * Must be packed because start is 64 bits but only aligned to 32 bits.
  51. */
  52. struct mlx4_eq_context {
  53. __be32 flags;
  54. u16 reserved1[3];
  55. __be16 page_offset;
  56. u8 log_eq_size;
  57. u8 reserved2[4];
  58. u8 eq_period;
  59. u8 reserved3;
  60. u8 eq_max_count;
  61. u8 reserved4[3];
  62. u8 intr;
  63. u8 log_page_size;
  64. u8 reserved5[2];
  65. u8 mtt_base_addr_h;
  66. __be32 mtt_base_addr_l;
  67. u32 reserved6[2];
  68. __be32 consumer_index;
  69. __be32 producer_index;
  70. u32 reserved7[4];
  71. };
  72. #define MLX4_EQ_STATUS_OK ( 0 << 28)
  73. #define MLX4_EQ_STATUS_WRITE_FAIL (10 << 28)
  74. #define MLX4_EQ_OWNER_SW ( 0 << 24)
  75. #define MLX4_EQ_OWNER_HW ( 1 << 24)
  76. #define MLX4_EQ_FLAG_EC ( 1 << 18)
  77. #define MLX4_EQ_FLAG_OI ( 1 << 17)
  78. #define MLX4_EQ_STATE_ARMED ( 9 << 8)
  79. #define MLX4_EQ_STATE_FIRED (10 << 8)
  80. #define MLX4_EQ_STATE_ALWAYS_ARMED (11 << 8)
  81. #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG) | \
  82. (1ull << MLX4_EVENT_TYPE_COMM_EST) | \
  83. (1ull << MLX4_EVENT_TYPE_SQ_DRAINED) | \
  84. (1ull << MLX4_EVENT_TYPE_CQ_ERROR) | \
  85. (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR) | \
  86. (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR) | \
  87. (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED) | \
  88. (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
  89. (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR) | \
  90. (1ull << MLX4_EVENT_TYPE_PORT_CHANGE) | \
  91. (1ull << MLX4_EVENT_TYPE_ECC_DETECT) | \
  92. (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR) | \
  93. (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
  94. (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT) | \
  95. (1ull << MLX4_EVENT_TYPE_CMD))
  96. static void eq_set_ci(struct mlx4_eq *eq, int req_not)
  97. {
  98. __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
  99. req_not << 31),
  100. eq->doorbell);
  101. /* We still want ordering, just not swabbing, so add a barrier */
  102. mb();
  103. }
  104. static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
  105. {
  106. unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
  107. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  108. }
  109. static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
  110. {
  111. struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
  112. return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
  113. }
  114. static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
  115. {
  116. struct mlx4_eqe *eqe;
  117. int cqn;
  118. int eqes_found = 0;
  119. int set_ci = 0;
  120. int port;
  121. while ((eqe = next_eqe_sw(eq))) {
  122. /*
  123. * Make sure we read EQ entry contents after we've
  124. * checked the ownership bit.
  125. */
  126. rmb();
  127. switch (eqe->type) {
  128. case MLX4_EVENT_TYPE_COMP:
  129. cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  130. mlx4_cq_completion(dev, cqn);
  131. break;
  132. case MLX4_EVENT_TYPE_PATH_MIG:
  133. case MLX4_EVENT_TYPE_COMM_EST:
  134. case MLX4_EVENT_TYPE_SQ_DRAINED:
  135. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  136. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  137. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  138. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  139. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  140. mlx4_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  141. eqe->type);
  142. break;
  143. case MLX4_EVENT_TYPE_SRQ_LIMIT:
  144. case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
  145. mlx4_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) & 0xffffff,
  146. eqe->type);
  147. break;
  148. case MLX4_EVENT_TYPE_CMD:
  149. mlx4_cmd_event(dev,
  150. be16_to_cpu(eqe->event.cmd.token),
  151. eqe->event.cmd.status,
  152. be64_to_cpu(eqe->event.cmd.out_param));
  153. break;
  154. case MLX4_EVENT_TYPE_PORT_CHANGE:
  155. port = be32_to_cpu(eqe->event.port_change.port) >> 28;
  156. if (eqe->subtype == MLX4_PORT_CHANGE_SUBTYPE_DOWN) {
  157. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_DOWN,
  158. port);
  159. mlx4_priv(dev)->sense.do_sense_port[port] = 1;
  160. } else {
  161. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_PORT_UP,
  162. port);
  163. mlx4_priv(dev)->sense.do_sense_port[port] = 0;
  164. }
  165. break;
  166. case MLX4_EVENT_TYPE_CQ_ERROR:
  167. mlx4_warn(dev, "CQ %s on CQN %06x\n",
  168. eqe->event.cq_err.syndrome == 1 ?
  169. "overrun" : "access violation",
  170. be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
  171. mlx4_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
  172. eqe->type);
  173. break;
  174. case MLX4_EVENT_TYPE_EQ_OVERFLOW:
  175. mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
  176. break;
  177. case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
  178. case MLX4_EVENT_TYPE_ECC_DETECT:
  179. default:
  180. mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at index %u\n",
  181. eqe->type, eqe->subtype, eq->eqn, eq->cons_index);
  182. break;
  183. }
  184. ++eq->cons_index;
  185. eqes_found = 1;
  186. ++set_ci;
  187. /*
  188. * The HCA will think the queue has overflowed if we
  189. * don't tell it we've been processing events. We
  190. * create our EQs with MLX4_NUM_SPARE_EQE extra
  191. * entries, so we must update our consumer index at
  192. * least that often.
  193. */
  194. if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
  195. eq_set_ci(eq, 0);
  196. set_ci = 0;
  197. }
  198. }
  199. eq_set_ci(eq, 1);
  200. return eqes_found;
  201. }
  202. static irqreturn_t mlx4_interrupt(int irq, void *dev_ptr)
  203. {
  204. struct mlx4_dev *dev = dev_ptr;
  205. struct mlx4_priv *priv = mlx4_priv(dev);
  206. int work = 0;
  207. int i;
  208. writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
  209. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  210. work |= mlx4_eq_int(dev, &priv->eq_table.eq[i]);
  211. return IRQ_RETVAL(work);
  212. }
  213. static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
  214. {
  215. struct mlx4_eq *eq = eq_ptr;
  216. struct mlx4_dev *dev = eq->dev;
  217. mlx4_eq_int(dev, eq);
  218. /* MSI-X vectors always belong to us */
  219. return IRQ_HANDLED;
  220. }
  221. static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
  222. int eq_num)
  223. {
  224. return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
  225. 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B,
  226. MLX4_CMD_WRAPPED);
  227. }
  228. static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  229. int eq_num)
  230. {
  231. return mlx4_cmd(dev, mailbox->dma, eq_num, 0, MLX4_CMD_SW2HW_EQ,
  232. MLX4_CMD_TIME_CLASS_A,
  233. MLX4_CMD_WRAPPED);
  234. }
  235. static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  236. int eq_num)
  237. {
  238. return mlx4_cmd_box(dev, 0, mailbox->dma, eq_num, 0, MLX4_CMD_HW2SW_EQ,
  239. MLX4_CMD_TIME_CLASS_A,
  240. MLX4_CMD_WRAPPED);
  241. }
  242. static int mlx4_num_eq_uar(struct mlx4_dev *dev)
  243. {
  244. /*
  245. * Each UAR holds 4 EQ doorbells. To figure out how many UARs
  246. * we need to map, take the difference of highest index and
  247. * the lowest index we'll use and add 1.
  248. */
  249. return (dev->caps.num_comp_vectors + 1 + dev->caps.reserved_eqs +
  250. dev->caps.comp_pool)/4 - dev->caps.reserved_eqs/4 + 1;
  251. }
  252. static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
  253. {
  254. struct mlx4_priv *priv = mlx4_priv(dev);
  255. int index;
  256. index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
  257. if (!priv->eq_table.uar_map[index]) {
  258. priv->eq_table.uar_map[index] =
  259. ioremap(pci_resource_start(dev->pdev, 2) +
  260. ((eq->eqn / 4) << PAGE_SHIFT),
  261. PAGE_SIZE);
  262. if (!priv->eq_table.uar_map[index]) {
  263. mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
  264. eq->eqn);
  265. return NULL;
  266. }
  267. }
  268. return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
  269. }
  270. static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
  271. u8 intr, struct mlx4_eq *eq)
  272. {
  273. struct mlx4_priv *priv = mlx4_priv(dev);
  274. struct mlx4_cmd_mailbox *mailbox;
  275. struct mlx4_eq_context *eq_context;
  276. int npages;
  277. u64 *dma_list = NULL;
  278. dma_addr_t t;
  279. u64 mtt_addr;
  280. int err = -ENOMEM;
  281. int i;
  282. eq->dev = dev;
  283. eq->nent = roundup_pow_of_two(max(nent, 2));
  284. npages = PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE;
  285. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  286. GFP_KERNEL);
  287. if (!eq->page_list)
  288. goto err_out;
  289. for (i = 0; i < npages; ++i)
  290. eq->page_list[i].buf = NULL;
  291. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  292. if (!dma_list)
  293. goto err_out_free;
  294. mailbox = mlx4_alloc_cmd_mailbox(dev);
  295. if (IS_ERR(mailbox))
  296. goto err_out_free;
  297. eq_context = mailbox->buf;
  298. for (i = 0; i < npages; ++i) {
  299. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  300. PAGE_SIZE, &t, GFP_KERNEL);
  301. if (!eq->page_list[i].buf)
  302. goto err_out_free_pages;
  303. dma_list[i] = t;
  304. eq->page_list[i].map = t;
  305. memset(eq->page_list[i].buf, 0, PAGE_SIZE);
  306. }
  307. eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
  308. if (eq->eqn == -1)
  309. goto err_out_free_pages;
  310. eq->doorbell = mlx4_get_eq_uar(dev, eq);
  311. if (!eq->doorbell) {
  312. err = -ENOMEM;
  313. goto err_out_free_eq;
  314. }
  315. err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
  316. if (err)
  317. goto err_out_free_eq;
  318. err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
  319. if (err)
  320. goto err_out_free_mtt;
  321. memset(eq_context, 0, sizeof *eq_context);
  322. eq_context->flags = cpu_to_be32(MLX4_EQ_STATUS_OK |
  323. MLX4_EQ_STATE_ARMED);
  324. eq_context->log_eq_size = ilog2(eq->nent);
  325. eq_context->intr = intr;
  326. eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
  327. mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
  328. eq_context->mtt_base_addr_h = mtt_addr >> 32;
  329. eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  330. err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
  331. if (err) {
  332. mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
  333. goto err_out_free_mtt;
  334. }
  335. kfree(dma_list);
  336. mlx4_free_cmd_mailbox(dev, mailbox);
  337. eq->cons_index = 0;
  338. return err;
  339. err_out_free_mtt:
  340. mlx4_mtt_cleanup(dev, &eq->mtt);
  341. err_out_free_eq:
  342. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  343. err_out_free_pages:
  344. for (i = 0; i < npages; ++i)
  345. if (eq->page_list[i].buf)
  346. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  347. eq->page_list[i].buf,
  348. eq->page_list[i].map);
  349. mlx4_free_cmd_mailbox(dev, mailbox);
  350. err_out_free:
  351. kfree(eq->page_list);
  352. kfree(dma_list);
  353. err_out:
  354. return err;
  355. }
  356. static void mlx4_free_eq(struct mlx4_dev *dev,
  357. struct mlx4_eq *eq)
  358. {
  359. struct mlx4_priv *priv = mlx4_priv(dev);
  360. struct mlx4_cmd_mailbox *mailbox;
  361. int err;
  362. int npages = PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE;
  363. int i;
  364. mailbox = mlx4_alloc_cmd_mailbox(dev);
  365. if (IS_ERR(mailbox))
  366. return;
  367. err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
  368. if (err)
  369. mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
  370. if (0) {
  371. mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  372. for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
  373. if (i % 4 == 0)
  374. pr_cont("[%02x] ", i * 4);
  375. pr_cont(" %08x", be32_to_cpup(mailbox->buf + i * 4));
  376. if ((i + 1) % 4 == 0)
  377. pr_cont("\n");
  378. }
  379. }
  380. mlx4_mtt_cleanup(dev, &eq->mtt);
  381. for (i = 0; i < npages; ++i)
  382. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  383. eq->page_list[i].buf,
  384. eq->page_list[i].map);
  385. kfree(eq->page_list);
  386. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  387. mlx4_free_cmd_mailbox(dev, mailbox);
  388. }
  389. static void mlx4_free_irqs(struct mlx4_dev *dev)
  390. {
  391. struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
  392. struct mlx4_priv *priv = mlx4_priv(dev);
  393. int i, vec;
  394. if (eq_table->have_irq)
  395. free_irq(dev->pdev->irq, dev);
  396. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  397. if (eq_table->eq[i].have_irq) {
  398. free_irq(eq_table->eq[i].irq, eq_table->eq + i);
  399. eq_table->eq[i].have_irq = 0;
  400. }
  401. for (i = 0; i < dev->caps.comp_pool; i++) {
  402. /*
  403. * Freeing the assigned irq's
  404. * all bits should be 0, but we need to validate
  405. */
  406. if (priv->msix_ctl.pool_bm & 1ULL << i) {
  407. /* NO need protecting*/
  408. vec = dev->caps.num_comp_vectors + 1 + i;
  409. free_irq(priv->eq_table.eq[vec].irq,
  410. &priv->eq_table.eq[vec]);
  411. }
  412. }
  413. kfree(eq_table->irq_names);
  414. }
  415. static int mlx4_map_clr_int(struct mlx4_dev *dev)
  416. {
  417. struct mlx4_priv *priv = mlx4_priv(dev);
  418. priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
  419. priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
  420. if (!priv->clr_base) {
  421. mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
  422. return -ENOMEM;
  423. }
  424. return 0;
  425. }
  426. static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
  427. {
  428. struct mlx4_priv *priv = mlx4_priv(dev);
  429. iounmap(priv->clr_base);
  430. }
  431. int mlx4_alloc_eq_table(struct mlx4_dev *dev)
  432. {
  433. struct mlx4_priv *priv = mlx4_priv(dev);
  434. priv->eq_table.eq = kcalloc(dev->caps.num_eqs - dev->caps.reserved_eqs,
  435. sizeof *priv->eq_table.eq, GFP_KERNEL);
  436. if (!priv->eq_table.eq)
  437. return -ENOMEM;
  438. return 0;
  439. }
  440. void mlx4_free_eq_table(struct mlx4_dev *dev)
  441. {
  442. kfree(mlx4_priv(dev)->eq_table.eq);
  443. }
  444. int mlx4_init_eq_table(struct mlx4_dev *dev)
  445. {
  446. struct mlx4_priv *priv = mlx4_priv(dev);
  447. int err;
  448. int i;
  449. priv->eq_table.uar_map = kcalloc(sizeof *priv->eq_table.uar_map,
  450. mlx4_num_eq_uar(dev), GFP_KERNEL);
  451. if (!priv->eq_table.uar_map) {
  452. err = -ENOMEM;
  453. goto err_out_free;
  454. }
  455. err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
  456. dev->caps.num_eqs - 1, dev->caps.reserved_eqs, 0);
  457. if (err)
  458. goto err_out_free;
  459. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  460. priv->eq_table.uar_map[i] = NULL;
  461. err = mlx4_map_clr_int(dev);
  462. if (err)
  463. goto err_out_bitmap;
  464. priv->eq_table.clr_mask =
  465. swab32(1 << (priv->eq_table.inta_pin & 31));
  466. priv->eq_table.clr_int = priv->clr_base +
  467. (priv->eq_table.inta_pin < 32 ? 4 : 0);
  468. priv->eq_table.irq_names =
  469. kmalloc(MLX4_IRQNAME_SIZE * (dev->caps.num_comp_vectors + 1 +
  470. dev->caps.comp_pool),
  471. GFP_KERNEL);
  472. if (!priv->eq_table.irq_names) {
  473. err = -ENOMEM;
  474. goto err_out_bitmap;
  475. }
  476. for (i = 0; i < dev->caps.num_comp_vectors; ++i) {
  477. err = mlx4_create_eq(dev, dev->caps.num_cqs -
  478. dev->caps.reserved_cqs +
  479. MLX4_NUM_SPARE_EQE,
  480. (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
  481. &priv->eq_table.eq[i]);
  482. if (err) {
  483. --i;
  484. goto err_out_unmap;
  485. }
  486. }
  487. err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
  488. (dev->flags & MLX4_FLAG_MSI_X) ? dev->caps.num_comp_vectors : 0,
  489. &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  490. if (err)
  491. goto err_out_comp;
  492. /*if additional completion vectors poolsize is 0 this loop will not run*/
  493. for (i = dev->caps.num_comp_vectors + 1;
  494. i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i) {
  495. err = mlx4_create_eq(dev, dev->caps.num_cqs -
  496. dev->caps.reserved_cqs +
  497. MLX4_NUM_SPARE_EQE,
  498. (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
  499. &priv->eq_table.eq[i]);
  500. if (err) {
  501. --i;
  502. goto err_out_unmap;
  503. }
  504. }
  505. if (dev->flags & MLX4_FLAG_MSI_X) {
  506. const char *eq_name;
  507. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i) {
  508. if (i < dev->caps.num_comp_vectors) {
  509. snprintf(priv->eq_table.irq_names +
  510. i * MLX4_IRQNAME_SIZE,
  511. MLX4_IRQNAME_SIZE,
  512. "mlx4-comp-%d@pci:%s", i,
  513. pci_name(dev->pdev));
  514. } else {
  515. snprintf(priv->eq_table.irq_names +
  516. i * MLX4_IRQNAME_SIZE,
  517. MLX4_IRQNAME_SIZE,
  518. "mlx4-async@pci:%s",
  519. pci_name(dev->pdev));
  520. }
  521. eq_name = priv->eq_table.irq_names +
  522. i * MLX4_IRQNAME_SIZE;
  523. err = request_irq(priv->eq_table.eq[i].irq,
  524. mlx4_msi_x_interrupt, 0, eq_name,
  525. priv->eq_table.eq + i);
  526. if (err)
  527. goto err_out_async;
  528. priv->eq_table.eq[i].have_irq = 1;
  529. }
  530. } else {
  531. snprintf(priv->eq_table.irq_names,
  532. MLX4_IRQNAME_SIZE,
  533. DRV_NAME "@pci:%s",
  534. pci_name(dev->pdev));
  535. err = request_irq(dev->pdev->irq, mlx4_interrupt,
  536. IRQF_SHARED, priv->eq_table.irq_names, dev);
  537. if (err)
  538. goto err_out_async;
  539. priv->eq_table.have_irq = 1;
  540. }
  541. err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  542. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  543. if (err)
  544. mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  545. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn, err);
  546. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  547. eq_set_ci(&priv->eq_table.eq[i], 1);
  548. return 0;
  549. err_out_async:
  550. mlx4_free_eq(dev, &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  551. err_out_comp:
  552. i = dev->caps.num_comp_vectors - 1;
  553. err_out_unmap:
  554. while (i >= 0) {
  555. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  556. --i;
  557. }
  558. mlx4_unmap_clr_int(dev);
  559. mlx4_free_irqs(dev);
  560. err_out_bitmap:
  561. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  562. err_out_free:
  563. kfree(priv->eq_table.uar_map);
  564. return err;
  565. }
  566. void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
  567. {
  568. struct mlx4_priv *priv = mlx4_priv(dev);
  569. int i;
  570. mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
  571. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  572. mlx4_free_irqs(dev);
  573. for (i = 0; i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i)
  574. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  575. mlx4_unmap_clr_int(dev);
  576. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  577. if (priv->eq_table.uar_map[i])
  578. iounmap(priv->eq_table.uar_map[i]);
  579. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  580. kfree(priv->eq_table.uar_map);
  581. }
  582. /* A test that verifies that we can accept interrupts on all
  583. * the irq vectors of the device.
  584. * Interrupts are checked using the NOP command.
  585. */
  586. int mlx4_test_interrupts(struct mlx4_dev *dev)
  587. {
  588. struct mlx4_priv *priv = mlx4_priv(dev);
  589. int i;
  590. int err;
  591. err = mlx4_NOP(dev);
  592. /* When not in MSI_X, there is only one irq to check */
  593. if (!(dev->flags & MLX4_FLAG_MSI_X))
  594. return err;
  595. /* A loop over all completion vectors, for each vector we will check
  596. * whether it works by mapping command completions to that vector
  597. * and performing a NOP command
  598. */
  599. for(i = 0; !err && (i < dev->caps.num_comp_vectors); ++i) {
  600. /* Temporary use polling for command completions */
  601. mlx4_cmd_use_polling(dev);
  602. /* Map the new eq to handle all asyncronous events */
  603. err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  604. priv->eq_table.eq[i].eqn);
  605. if (err) {
  606. mlx4_warn(dev, "Failed mapping eq for interrupt test\n");
  607. mlx4_cmd_use_events(dev);
  608. break;
  609. }
  610. /* Go back to using events */
  611. mlx4_cmd_use_events(dev);
  612. err = mlx4_NOP(dev);
  613. }
  614. /* Return to default */
  615. mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  616. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  617. return err;
  618. }
  619. EXPORT_SYMBOL(mlx4_test_interrupts);
  620. int mlx4_assign_eq(struct mlx4_dev *dev, char* name, int * vector)
  621. {
  622. struct mlx4_priv *priv = mlx4_priv(dev);
  623. int vec = 0, err = 0, i;
  624. spin_lock(&priv->msix_ctl.pool_lock);
  625. for (i = 0; !vec && i < dev->caps.comp_pool; i++) {
  626. if (~priv->msix_ctl.pool_bm & 1ULL << i) {
  627. priv->msix_ctl.pool_bm |= 1ULL << i;
  628. vec = dev->caps.num_comp_vectors + 1 + i;
  629. snprintf(priv->eq_table.irq_names +
  630. vec * MLX4_IRQNAME_SIZE,
  631. MLX4_IRQNAME_SIZE, "%s", name);
  632. err = request_irq(priv->eq_table.eq[vec].irq,
  633. mlx4_msi_x_interrupt, 0,
  634. &priv->eq_table.irq_names[vec<<5],
  635. priv->eq_table.eq + vec);
  636. if (err) {
  637. /*zero out bit by fliping it*/
  638. priv->msix_ctl.pool_bm ^= 1 << i;
  639. vec = 0;
  640. continue;
  641. /*we dont want to break here*/
  642. }
  643. eq_set_ci(&priv->eq_table.eq[vec], 1);
  644. }
  645. }
  646. spin_unlock(&priv->msix_ctl.pool_lock);
  647. if (vec) {
  648. *vector = vec;
  649. } else {
  650. *vector = 0;
  651. err = (i == dev->caps.comp_pool) ? -ENOSPC : err;
  652. }
  653. return err;
  654. }
  655. EXPORT_SYMBOL(mlx4_assign_eq);
  656. void mlx4_release_eq(struct mlx4_dev *dev, int vec)
  657. {
  658. struct mlx4_priv *priv = mlx4_priv(dev);
  659. /*bm index*/
  660. int i = vec - dev->caps.num_comp_vectors - 1;
  661. if (likely(i >= 0)) {
  662. /*sanity check , making sure were not trying to free irq's
  663. Belonging to a legacy EQ*/
  664. spin_lock(&priv->msix_ctl.pool_lock);
  665. if (priv->msix_ctl.pool_bm & 1ULL << i) {
  666. free_irq(priv->eq_table.eq[vec].irq,
  667. &priv->eq_table.eq[vec]);
  668. priv->msix_ctl.pool_bm &= ~(1ULL << i);
  669. }
  670. spin_unlock(&priv->msix_ctl.pool_lock);
  671. }
  672. }
  673. EXPORT_SYMBOL(mlx4_release_eq);