/drivers/infiniband/hw/mthca/mthca_eq.c

https://bitbucket.org/evzijst/gittest · C · 964 lines · 733 code · 148 blank · 83 comment · 89 complexity · ee47e43e4205283a41f9410db202c017 MD5 · raw file

  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: mthca_eq.c 1382 2004-12-24 02:21:02Z roland $
  33. */
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/pci.h>
  38. #include "mthca_dev.h"
  39. #include "mthca_cmd.h"
  40. #include "mthca_config_reg.h"
  41. enum {
  42. MTHCA_NUM_ASYNC_EQE = 0x80,
  43. MTHCA_NUM_CMD_EQE = 0x80,
  44. MTHCA_EQ_ENTRY_SIZE = 0x20
  45. };
  46. /*
  47. * Must be packed because start is 64 bits but only aligned to 32 bits.
  48. */
  49. struct mthca_eq_context {
  50. u32 flags;
  51. u64 start;
  52. u32 logsize_usrpage;
  53. u32 tavor_pd; /* reserved for Arbel */
  54. u8 reserved1[3];
  55. u8 intr;
  56. u32 arbel_pd; /* lost_count for Tavor */
  57. u32 lkey;
  58. u32 reserved2[2];
  59. u32 consumer_index;
  60. u32 producer_index;
  61. u32 reserved3[4];
  62. } __attribute__((packed));
  63. #define MTHCA_EQ_STATUS_OK ( 0 << 28)
  64. #define MTHCA_EQ_STATUS_OVERFLOW ( 9 << 28)
  65. #define MTHCA_EQ_STATUS_WRITE_FAIL (10 << 28)
  66. #define MTHCA_EQ_OWNER_SW ( 0 << 24)
  67. #define MTHCA_EQ_OWNER_HW ( 1 << 24)
  68. #define MTHCA_EQ_FLAG_TR ( 1 << 18)
  69. #define MTHCA_EQ_FLAG_OI ( 1 << 17)
  70. #define MTHCA_EQ_STATE_ARMED ( 1 << 8)
  71. #define MTHCA_EQ_STATE_FIRED ( 2 << 8)
  72. #define MTHCA_EQ_STATE_ALWAYS_ARMED ( 3 << 8)
  73. #define MTHCA_EQ_STATE_ARBEL ( 8 << 8)
  74. enum {
  75. MTHCA_EVENT_TYPE_COMP = 0x00,
  76. MTHCA_EVENT_TYPE_PATH_MIG = 0x01,
  77. MTHCA_EVENT_TYPE_COMM_EST = 0x02,
  78. MTHCA_EVENT_TYPE_SQ_DRAINED = 0x03,
  79. MTHCA_EVENT_TYPE_SRQ_LAST_WQE = 0x13,
  80. MTHCA_EVENT_TYPE_CQ_ERROR = 0x04,
  81. MTHCA_EVENT_TYPE_WQ_CATAS_ERROR = 0x05,
  82. MTHCA_EVENT_TYPE_EEC_CATAS_ERROR = 0x06,
  83. MTHCA_EVENT_TYPE_PATH_MIG_FAILED = 0x07,
  84. MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR = 0x10,
  85. MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR = 0x11,
  86. MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR = 0x12,
  87. MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR = 0x08,
  88. MTHCA_EVENT_TYPE_PORT_CHANGE = 0x09,
  89. MTHCA_EVENT_TYPE_EQ_OVERFLOW = 0x0f,
  90. MTHCA_EVENT_TYPE_ECC_DETECT = 0x0e,
  91. MTHCA_EVENT_TYPE_CMD = 0x0a
  92. };
  93. #define MTHCA_ASYNC_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_PATH_MIG) | \
  94. (1ULL << MTHCA_EVENT_TYPE_COMM_EST) | \
  95. (1ULL << MTHCA_EVENT_TYPE_SQ_DRAINED) | \
  96. (1ULL << MTHCA_EVENT_TYPE_CQ_ERROR) | \
  97. (1ULL << MTHCA_EVENT_TYPE_WQ_CATAS_ERROR) | \
  98. (1ULL << MTHCA_EVENT_TYPE_EEC_CATAS_ERROR) | \
  99. (1ULL << MTHCA_EVENT_TYPE_PATH_MIG_FAILED) | \
  100. (1ULL << MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
  101. (1ULL << MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR) | \
  102. (1ULL << MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR) | \
  103. (1ULL << MTHCA_EVENT_TYPE_PORT_CHANGE) | \
  104. (1ULL << MTHCA_EVENT_TYPE_ECC_DETECT))
  105. #define MTHCA_SRQ_EVENT_MASK (1ULL << MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR) | \
  106. (1ULL << MTHCA_EVENT_TYPE_SRQ_LAST_WQE)
  107. #define MTHCA_CMD_EVENT_MASK (1ULL << MTHCA_EVENT_TYPE_CMD)
  108. #define MTHCA_EQ_DB_INC_CI (1 << 24)
  109. #define MTHCA_EQ_DB_REQ_NOT (2 << 24)
  110. #define MTHCA_EQ_DB_DISARM_CQ (3 << 24)
  111. #define MTHCA_EQ_DB_SET_CI (4 << 24)
  112. #define MTHCA_EQ_DB_ALWAYS_ARM (5 << 24)
  113. struct mthca_eqe {
  114. u8 reserved1;
  115. u8 type;
  116. u8 reserved2;
  117. u8 subtype;
  118. union {
  119. u32 raw[6];
  120. struct {
  121. u32 cqn;
  122. } __attribute__((packed)) comp;
  123. struct {
  124. u16 reserved1;
  125. u16 token;
  126. u32 reserved2;
  127. u8 reserved3[3];
  128. u8 status;
  129. u64 out_param;
  130. } __attribute__((packed)) cmd;
  131. struct {
  132. u32 qpn;
  133. } __attribute__((packed)) qp;
  134. struct {
  135. u32 cqn;
  136. u32 reserved1;
  137. u8 reserved2[3];
  138. u8 syndrome;
  139. } __attribute__((packed)) cq_err;
  140. struct {
  141. u32 reserved1[2];
  142. u32 port;
  143. } __attribute__((packed)) port_change;
  144. } event;
  145. u8 reserved3[3];
  146. u8 owner;
  147. } __attribute__((packed));
  148. #define MTHCA_EQ_ENTRY_OWNER_SW (0 << 7)
  149. #define MTHCA_EQ_ENTRY_OWNER_HW (1 << 7)
  150. static inline u64 async_mask(struct mthca_dev *dev)
  151. {
  152. return dev->mthca_flags & MTHCA_FLAG_SRQ ?
  153. MTHCA_ASYNC_EVENT_MASK | MTHCA_SRQ_EVENT_MASK :
  154. MTHCA_ASYNC_EVENT_MASK;
  155. }
  156. static inline void tavor_set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  157. {
  158. u32 doorbell[2];
  159. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_SET_CI | eq->eqn);
  160. doorbell[1] = cpu_to_be32(ci & (eq->nent - 1));
  161. /*
  162. * This barrier makes sure that all updates to ownership bits
  163. * done by set_eqe_hw() hit memory before the consumer index
  164. * is updated. set_eq_ci() allows the HCA to possibly write
  165. * more EQ entries, and we want to avoid the exceedingly
  166. * unlikely possibility of the HCA writing an entry and then
  167. * having set_eqe_hw() overwrite the owner field.
  168. */
  169. wmb();
  170. mthca_write64(doorbell,
  171. dev->kar + MTHCA_EQ_DOORBELL,
  172. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  173. }
  174. static inline void arbel_set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  175. {
  176. /* See comment in tavor_set_eq_ci() above. */
  177. wmb();
  178. __raw_writel(cpu_to_be32(ci), dev->eq_regs.arbel.eq_set_ci_base +
  179. eq->eqn * 8);
  180. /* We still want ordering, just not swabbing, so add a barrier */
  181. mb();
  182. }
  183. static inline void set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  184. {
  185. if (dev->hca_type == ARBEL_NATIVE)
  186. arbel_set_eq_ci(dev, eq, ci);
  187. else
  188. tavor_set_eq_ci(dev, eq, ci);
  189. }
  190. static inline void tavor_eq_req_not(struct mthca_dev *dev, int eqn)
  191. {
  192. u32 doorbell[2];
  193. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_REQ_NOT | eqn);
  194. doorbell[1] = 0;
  195. mthca_write64(doorbell,
  196. dev->kar + MTHCA_EQ_DOORBELL,
  197. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  198. }
  199. static inline void arbel_eq_req_not(struct mthca_dev *dev, u32 eqn_mask)
  200. {
  201. writel(eqn_mask, dev->eq_regs.arbel.eq_arm);
  202. }
  203. static inline void disarm_cq(struct mthca_dev *dev, int eqn, int cqn)
  204. {
  205. if (dev->hca_type != ARBEL_NATIVE) {
  206. u32 doorbell[2];
  207. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_DISARM_CQ | eqn);
  208. doorbell[1] = cpu_to_be32(cqn);
  209. mthca_write64(doorbell,
  210. dev->kar + MTHCA_EQ_DOORBELL,
  211. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  212. }
  213. }
  214. static inline struct mthca_eqe *get_eqe(struct mthca_eq *eq, u32 entry)
  215. {
  216. unsigned long off = (entry & (eq->nent - 1)) * MTHCA_EQ_ENTRY_SIZE;
  217. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  218. }
  219. static inline struct mthca_eqe* next_eqe_sw(struct mthca_eq *eq)
  220. {
  221. struct mthca_eqe* eqe;
  222. eqe = get_eqe(eq, eq->cons_index);
  223. return (MTHCA_EQ_ENTRY_OWNER_HW & eqe->owner) ? NULL : eqe;
  224. }
  225. static inline void set_eqe_hw(struct mthca_eqe *eqe)
  226. {
  227. eqe->owner = MTHCA_EQ_ENTRY_OWNER_HW;
  228. }
  229. static void port_change(struct mthca_dev *dev, int port, int active)
  230. {
  231. struct ib_event record;
  232. mthca_dbg(dev, "Port change to %s for port %d\n",
  233. active ? "active" : "down", port);
  234. record.device = &dev->ib_dev;
  235. record.event = active ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  236. record.element.port_num = port;
  237. ib_dispatch_event(&record);
  238. }
  239. static int mthca_eq_int(struct mthca_dev *dev, struct mthca_eq *eq)
  240. {
  241. struct mthca_eqe *eqe;
  242. int disarm_cqn;
  243. int eqes_found = 0;
  244. while ((eqe = next_eqe_sw(eq))) {
  245. int set_ci = 0;
  246. /*
  247. * Make sure we read EQ entry contents after we've
  248. * checked the ownership bit.
  249. */
  250. rmb();
  251. switch (eqe->type) {
  252. case MTHCA_EVENT_TYPE_COMP:
  253. disarm_cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  254. disarm_cq(dev, eq->eqn, disarm_cqn);
  255. mthca_cq_event(dev, disarm_cqn);
  256. break;
  257. case MTHCA_EVENT_TYPE_PATH_MIG:
  258. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  259. IB_EVENT_PATH_MIG);
  260. break;
  261. case MTHCA_EVENT_TYPE_COMM_EST:
  262. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  263. IB_EVENT_COMM_EST);
  264. break;
  265. case MTHCA_EVENT_TYPE_SQ_DRAINED:
  266. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  267. IB_EVENT_SQ_DRAINED);
  268. break;
  269. case MTHCA_EVENT_TYPE_WQ_CATAS_ERROR:
  270. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  271. IB_EVENT_QP_FATAL);
  272. break;
  273. case MTHCA_EVENT_TYPE_PATH_MIG_FAILED:
  274. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  275. IB_EVENT_PATH_MIG_ERR);
  276. break;
  277. case MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  278. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  279. IB_EVENT_QP_REQ_ERR);
  280. break;
  281. case MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR:
  282. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  283. IB_EVENT_QP_ACCESS_ERR);
  284. break;
  285. case MTHCA_EVENT_TYPE_CMD:
  286. mthca_cmd_event(dev,
  287. be16_to_cpu(eqe->event.cmd.token),
  288. eqe->event.cmd.status,
  289. be64_to_cpu(eqe->event.cmd.out_param));
  290. /*
  291. * cmd_event() may add more commands.
  292. * The card will think the queue has overflowed if
  293. * we don't tell it we've been processing events.
  294. */
  295. set_ci = 1;
  296. break;
  297. case MTHCA_EVENT_TYPE_PORT_CHANGE:
  298. port_change(dev,
  299. (be32_to_cpu(eqe->event.port_change.port) >> 28) & 3,
  300. eqe->subtype == 0x4);
  301. break;
  302. case MTHCA_EVENT_TYPE_CQ_ERROR:
  303. mthca_warn(dev, "CQ %s on CQN %08x\n",
  304. eqe->event.cq_err.syndrome == 1 ?
  305. "overrun" : "access violation",
  306. be32_to_cpu(eqe->event.cq_err.cqn));
  307. break;
  308. case MTHCA_EVENT_TYPE_EQ_OVERFLOW:
  309. mthca_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
  310. break;
  311. case MTHCA_EVENT_TYPE_EEC_CATAS_ERROR:
  312. case MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR:
  313. case MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR:
  314. case MTHCA_EVENT_TYPE_ECC_DETECT:
  315. default:
  316. mthca_warn(dev, "Unhandled event %02x(%02x) on EQ %d\n",
  317. eqe->type, eqe->subtype, eq->eqn);
  318. break;
  319. };
  320. set_eqe_hw(eqe);
  321. ++eq->cons_index;
  322. eqes_found = 1;
  323. if (unlikely(set_ci)) {
  324. /*
  325. * Conditional on hca_type is OK here because
  326. * this is a rare case, not the fast path.
  327. */
  328. set_eq_ci(dev, eq, eq->cons_index);
  329. set_ci = 0;
  330. }
  331. }
  332. /*
  333. * Rely on caller to set consumer index so that we don't have
  334. * to test hca_type in our interrupt handling fast path.
  335. */
  336. return eqes_found;
  337. }
  338. static irqreturn_t mthca_tavor_interrupt(int irq, void *dev_ptr, struct pt_regs *regs)
  339. {
  340. struct mthca_dev *dev = dev_ptr;
  341. u32 ecr;
  342. int i;
  343. if (dev->eq_table.clr_mask)
  344. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  345. ecr = readl(dev->eq_regs.tavor.ecr_base + 4);
  346. if (ecr) {
  347. writel(ecr, dev->eq_regs.tavor.ecr_base +
  348. MTHCA_ECR_CLR_BASE - MTHCA_ECR_BASE + 4);
  349. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  350. if (ecr & dev->eq_table.eq[i].eqn_mask &&
  351. mthca_eq_int(dev, &dev->eq_table.eq[i])) {
  352. tavor_set_eq_ci(dev, &dev->eq_table.eq[i],
  353. dev->eq_table.eq[i].cons_index);
  354. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  355. }
  356. }
  357. return IRQ_RETVAL(ecr);
  358. }
  359. static irqreturn_t mthca_tavor_msi_x_interrupt(int irq, void *eq_ptr,
  360. struct pt_regs *regs)
  361. {
  362. struct mthca_eq *eq = eq_ptr;
  363. struct mthca_dev *dev = eq->dev;
  364. mthca_eq_int(dev, eq);
  365. tavor_set_eq_ci(dev, eq, eq->cons_index);
  366. tavor_eq_req_not(dev, eq->eqn);
  367. /* MSI-X vectors always belong to us */
  368. return IRQ_HANDLED;
  369. }
  370. static irqreturn_t mthca_arbel_interrupt(int irq, void *dev_ptr, struct pt_regs *regs)
  371. {
  372. struct mthca_dev *dev = dev_ptr;
  373. int work = 0;
  374. int i;
  375. if (dev->eq_table.clr_mask)
  376. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  377. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  378. if (mthca_eq_int(dev, &dev->eq_table.eq[i])) {
  379. work = 1;
  380. arbel_set_eq_ci(dev, &dev->eq_table.eq[i],
  381. dev->eq_table.eq[i].cons_index);
  382. }
  383. arbel_eq_req_not(dev, dev->eq_table.arm_mask);
  384. return IRQ_RETVAL(work);
  385. }
  386. static irqreturn_t mthca_arbel_msi_x_interrupt(int irq, void *eq_ptr,
  387. struct pt_regs *regs)
  388. {
  389. struct mthca_eq *eq = eq_ptr;
  390. struct mthca_dev *dev = eq->dev;
  391. mthca_eq_int(dev, eq);
  392. arbel_set_eq_ci(dev, eq, eq->cons_index);
  393. arbel_eq_req_not(dev, eq->eqn_mask);
  394. /* MSI-X vectors always belong to us */
  395. return IRQ_HANDLED;
  396. }
  397. static int __devinit mthca_create_eq(struct mthca_dev *dev,
  398. int nent,
  399. u8 intr,
  400. struct mthca_eq *eq)
  401. {
  402. int npages = (nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
  403. PAGE_SIZE;
  404. u64 *dma_list = NULL;
  405. dma_addr_t t;
  406. void *mailbox = NULL;
  407. struct mthca_eq_context *eq_context;
  408. int err = -ENOMEM;
  409. int i;
  410. u8 status;
  411. /* Make sure EQ size is aligned to a power of 2 size. */
  412. for (i = 1; i < nent; i <<= 1)
  413. ; /* nothing */
  414. nent = i;
  415. eq->dev = dev;
  416. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  417. GFP_KERNEL);
  418. if (!eq->page_list)
  419. goto err_out;
  420. for (i = 0; i < npages; ++i)
  421. eq->page_list[i].buf = NULL;
  422. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  423. if (!dma_list)
  424. goto err_out_free;
  425. mailbox = kmalloc(sizeof *eq_context + MTHCA_CMD_MAILBOX_EXTRA,
  426. GFP_KERNEL);
  427. if (!mailbox)
  428. goto err_out_free;
  429. eq_context = MAILBOX_ALIGN(mailbox);
  430. for (i = 0; i < npages; ++i) {
  431. eq->page_list[i].buf = pci_alloc_consistent(dev->pdev,
  432. PAGE_SIZE, &t);
  433. if (!eq->page_list[i].buf)
  434. goto err_out_free;
  435. dma_list[i] = t;
  436. pci_unmap_addr_set(&eq->page_list[i], mapping, t);
  437. memset(eq->page_list[i].buf, 0, PAGE_SIZE);
  438. }
  439. for (i = 0; i < nent; ++i)
  440. set_eqe_hw(get_eqe(eq, i));
  441. eq->eqn = mthca_alloc(&dev->eq_table.alloc);
  442. if (eq->eqn == -1)
  443. goto err_out_free;
  444. err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
  445. dma_list, PAGE_SHIFT, npages,
  446. 0, npages * PAGE_SIZE,
  447. MTHCA_MPT_FLAG_LOCAL_WRITE |
  448. MTHCA_MPT_FLAG_LOCAL_READ,
  449. &eq->mr);
  450. if (err)
  451. goto err_out_free_eq;
  452. eq->nent = nent;
  453. memset(eq_context, 0, sizeof *eq_context);
  454. eq_context->flags = cpu_to_be32(MTHCA_EQ_STATUS_OK |
  455. MTHCA_EQ_OWNER_HW |
  456. MTHCA_EQ_STATE_ARMED |
  457. MTHCA_EQ_FLAG_TR);
  458. if (dev->hca_type == ARBEL_NATIVE)
  459. eq_context->flags |= cpu_to_be32(MTHCA_EQ_STATE_ARBEL);
  460. eq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);
  461. if (dev->hca_type == ARBEL_NATIVE) {
  462. eq_context->arbel_pd = cpu_to_be32(dev->driver_pd.pd_num);
  463. } else {
  464. eq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);
  465. eq_context->tavor_pd = cpu_to_be32(dev->driver_pd.pd_num);
  466. }
  467. eq_context->intr = intr;
  468. eq_context->lkey = cpu_to_be32(eq->mr.ibmr.lkey);
  469. err = mthca_SW2HW_EQ(dev, eq_context, eq->eqn, &status);
  470. if (err) {
  471. mthca_warn(dev, "SW2HW_EQ failed (%d)\n", err);
  472. goto err_out_free_mr;
  473. }
  474. if (status) {
  475. mthca_warn(dev, "SW2HW_EQ returned status 0x%02x\n",
  476. status);
  477. err = -EINVAL;
  478. goto err_out_free_mr;
  479. }
  480. kfree(dma_list);
  481. kfree(mailbox);
  482. eq->eqn_mask = swab32(1 << eq->eqn);
  483. eq->cons_index = 0;
  484. dev->eq_table.arm_mask |= eq->eqn_mask;
  485. mthca_dbg(dev, "Allocated EQ %d with %d entries\n",
  486. eq->eqn, nent);
  487. return err;
  488. err_out_free_mr:
  489. mthca_free_mr(dev, &eq->mr);
  490. err_out_free_eq:
  491. mthca_free(&dev->eq_table.alloc, eq->eqn);
  492. err_out_free:
  493. for (i = 0; i < npages; ++i)
  494. if (eq->page_list[i].buf)
  495. pci_free_consistent(dev->pdev, PAGE_SIZE,
  496. eq->page_list[i].buf,
  497. pci_unmap_addr(&eq->page_list[i],
  498. mapping));
  499. kfree(eq->page_list);
  500. kfree(dma_list);
  501. kfree(mailbox);
  502. err_out:
  503. return err;
  504. }
  505. static void mthca_free_eq(struct mthca_dev *dev,
  506. struct mthca_eq *eq)
  507. {
  508. void *mailbox = NULL;
  509. int err;
  510. u8 status;
  511. int npages = (eq->nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
  512. PAGE_SIZE;
  513. int i;
  514. mailbox = kmalloc(sizeof (struct mthca_eq_context) + MTHCA_CMD_MAILBOX_EXTRA,
  515. GFP_KERNEL);
  516. if (!mailbox)
  517. return;
  518. err = mthca_HW2SW_EQ(dev, MAILBOX_ALIGN(mailbox),
  519. eq->eqn, &status);
  520. if (err)
  521. mthca_warn(dev, "HW2SW_EQ failed (%d)\n", err);
  522. if (status)
  523. mthca_warn(dev, "HW2SW_EQ returned status 0x%02x\n",
  524. status);
  525. dev->eq_table.arm_mask &= ~eq->eqn_mask;
  526. if (0) {
  527. mthca_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  528. for (i = 0; i < sizeof (struct mthca_eq_context) / 4; ++i) {
  529. if (i % 4 == 0)
  530. printk("[%02x] ", i * 4);
  531. printk(" %08x", be32_to_cpup(MAILBOX_ALIGN(mailbox) + i * 4));
  532. if ((i + 1) % 4 == 0)
  533. printk("\n");
  534. }
  535. }
  536. mthca_free_mr(dev, &eq->mr);
  537. for (i = 0; i < npages; ++i)
  538. pci_free_consistent(dev->pdev, PAGE_SIZE,
  539. eq->page_list[i].buf,
  540. pci_unmap_addr(&eq->page_list[i], mapping));
  541. kfree(eq->page_list);
  542. kfree(mailbox);
  543. }
  544. static void mthca_free_irqs(struct mthca_dev *dev)
  545. {
  546. int i;
  547. if (dev->eq_table.have_irq)
  548. free_irq(dev->pdev->irq, dev);
  549. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  550. if (dev->eq_table.eq[i].have_irq)
  551. free_irq(dev->eq_table.eq[i].msi_x_vector,
  552. dev->eq_table.eq + i);
  553. }
  554. static int __devinit mthca_map_reg(struct mthca_dev *dev,
  555. unsigned long offset, unsigned long size,
  556. void __iomem **map)
  557. {
  558. unsigned long base = pci_resource_start(dev->pdev, 0);
  559. if (!request_mem_region(base + offset, size, DRV_NAME))
  560. return -EBUSY;
  561. *map = ioremap(base + offset, size);
  562. if (!*map) {
  563. release_mem_region(base + offset, size);
  564. return -ENOMEM;
  565. }
  566. return 0;
  567. }
  568. static void mthca_unmap_reg(struct mthca_dev *dev, unsigned long offset,
  569. unsigned long size, void __iomem *map)
  570. {
  571. unsigned long base = pci_resource_start(dev->pdev, 0);
  572. release_mem_region(base + offset, size);
  573. iounmap(map);
  574. }
  575. static int __devinit mthca_map_eq_regs(struct mthca_dev *dev)
  576. {
  577. unsigned long mthca_base;
  578. mthca_base = pci_resource_start(dev->pdev, 0);
  579. if (dev->hca_type == ARBEL_NATIVE) {
  580. /*
  581. * We assume that the EQ arm and EQ set CI registers
  582. * fall within the first BAR. We can't trust the
  583. * values firmware gives us, since those addresses are
  584. * valid on the HCA's side of the PCI bus but not
  585. * necessarily the host side.
  586. */
  587. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  588. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  589. &dev->clr_base)) {
  590. mthca_err(dev, "Couldn't map interrupt clear register, "
  591. "aborting.\n");
  592. return -ENOMEM;
  593. }
  594. /*
  595. * Add 4 because we limit ourselves to EQs 0 ... 31,
  596. * so we only need the low word of the register.
  597. */
  598. if (mthca_map_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  599. dev->fw.arbel.eq_arm_base) + 4, 4,
  600. &dev->eq_regs.arbel.eq_arm)) {
  601. mthca_err(dev, "Couldn't map interrupt clear register, "
  602. "aborting.\n");
  603. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  604. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  605. dev->clr_base);
  606. return -ENOMEM;
  607. }
  608. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  609. dev->fw.arbel.eq_set_ci_base,
  610. MTHCA_EQ_SET_CI_SIZE,
  611. &dev->eq_regs.arbel.eq_set_ci_base)) {
  612. mthca_err(dev, "Couldn't map interrupt clear register, "
  613. "aborting.\n");
  614. mthca_unmap_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  615. dev->fw.arbel.eq_arm_base) + 4, 4,
  616. dev->eq_regs.arbel.eq_arm);
  617. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  618. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  619. dev->clr_base);
  620. return -ENOMEM;
  621. }
  622. } else {
  623. if (mthca_map_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  624. &dev->clr_base)) {
  625. mthca_err(dev, "Couldn't map interrupt clear register, "
  626. "aborting.\n");
  627. return -ENOMEM;
  628. }
  629. if (mthca_map_reg(dev, MTHCA_ECR_BASE,
  630. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  631. &dev->eq_regs.tavor.ecr_base)) {
  632. mthca_err(dev, "Couldn't map ecr register, "
  633. "aborting.\n");
  634. mthca_unmap_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  635. dev->clr_base);
  636. return -ENOMEM;
  637. }
  638. }
  639. return 0;
  640. }
  641. static void __devexit mthca_unmap_eq_regs(struct mthca_dev *dev)
  642. {
  643. if (dev->hca_type == ARBEL_NATIVE) {
  644. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  645. dev->fw.arbel.eq_set_ci_base,
  646. MTHCA_EQ_SET_CI_SIZE,
  647. dev->eq_regs.arbel.eq_set_ci_base);
  648. mthca_unmap_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  649. dev->fw.arbel.eq_arm_base) + 4, 4,
  650. dev->eq_regs.arbel.eq_arm);
  651. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  652. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  653. dev->clr_base);
  654. } else {
  655. mthca_unmap_reg(dev, MTHCA_ECR_BASE,
  656. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  657. dev->eq_regs.tavor.ecr_base);
  658. mthca_unmap_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  659. dev->clr_base);
  660. }
  661. }
  662. int __devinit mthca_map_eq_icm(struct mthca_dev *dev, u64 icm_virt)
  663. {
  664. int ret;
  665. u8 status;
  666. /*
  667. * We assume that mapping one page is enough for the whole EQ
  668. * context table. This is fine with all current HCAs, because
  669. * we only use 32 EQs and each EQ uses 32 bytes of context
  670. * memory, or 1 KB total.
  671. */
  672. dev->eq_table.icm_virt = icm_virt;
  673. dev->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
  674. if (!dev->eq_table.icm_page)
  675. return -ENOMEM;
  676. dev->eq_table.icm_dma = pci_map_page(dev->pdev, dev->eq_table.icm_page, 0,
  677. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  678. if (pci_dma_mapping_error(dev->eq_table.icm_dma)) {
  679. __free_page(dev->eq_table.icm_page);
  680. return -ENOMEM;
  681. }
  682. ret = mthca_MAP_ICM_page(dev, dev->eq_table.icm_dma, icm_virt, &status);
  683. if (!ret && status)
  684. ret = -EINVAL;
  685. if (ret) {
  686. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  687. PCI_DMA_BIDIRECTIONAL);
  688. __free_page(dev->eq_table.icm_page);
  689. }
  690. return ret;
  691. }
  692. void __devexit mthca_unmap_eq_icm(struct mthca_dev *dev)
  693. {
  694. u8 status;
  695. mthca_UNMAP_ICM(dev, dev->eq_table.icm_virt, PAGE_SIZE / 4096, &status);
  696. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  697. PCI_DMA_BIDIRECTIONAL);
  698. __free_page(dev->eq_table.icm_page);
  699. }
  700. int __devinit mthca_init_eq_table(struct mthca_dev *dev)
  701. {
  702. int err;
  703. u8 status;
  704. u8 intr;
  705. int i;
  706. err = mthca_alloc_init(&dev->eq_table.alloc,
  707. dev->limits.num_eqs,
  708. dev->limits.num_eqs - 1,
  709. dev->limits.reserved_eqs);
  710. if (err)
  711. return err;
  712. err = mthca_map_eq_regs(dev);
  713. if (err)
  714. goto err_out_free;
  715. if (dev->mthca_flags & MTHCA_FLAG_MSI ||
  716. dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  717. dev->eq_table.clr_mask = 0;
  718. } else {
  719. dev->eq_table.clr_mask =
  720. swab32(1 << (dev->eq_table.inta_pin & 31));
  721. dev->eq_table.clr_int = dev->clr_base +
  722. (dev->eq_table.inta_pin < 31 ? 4 : 0);
  723. }
  724. dev->eq_table.arm_mask = 0;
  725. intr = (dev->mthca_flags & MTHCA_FLAG_MSI) ?
  726. 128 : dev->eq_table.inta_pin;
  727. err = mthca_create_eq(dev, dev->limits.num_cqs,
  728. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
  729. &dev->eq_table.eq[MTHCA_EQ_COMP]);
  730. if (err)
  731. goto err_out_unmap;
  732. err = mthca_create_eq(dev, MTHCA_NUM_ASYNC_EQE,
  733. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 129 : intr,
  734. &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  735. if (err)
  736. goto err_out_comp;
  737. err = mthca_create_eq(dev, MTHCA_NUM_CMD_EQE,
  738. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 130 : intr,
  739. &dev->eq_table.eq[MTHCA_EQ_CMD]);
  740. if (err)
  741. goto err_out_async;
  742. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  743. static const char *eq_name[] = {
  744. [MTHCA_EQ_COMP] = DRV_NAME " (comp)",
  745. [MTHCA_EQ_ASYNC] = DRV_NAME " (async)",
  746. [MTHCA_EQ_CMD] = DRV_NAME " (cmd)"
  747. };
  748. for (i = 0; i < MTHCA_NUM_EQ; ++i) {
  749. err = request_irq(dev->eq_table.eq[i].msi_x_vector,
  750. dev->hca_type == ARBEL_NATIVE ?
  751. mthca_arbel_msi_x_interrupt :
  752. mthca_tavor_msi_x_interrupt,
  753. 0, eq_name[i], dev->eq_table.eq + i);
  754. if (err)
  755. goto err_out_cmd;
  756. dev->eq_table.eq[i].have_irq = 1;
  757. }
  758. } else {
  759. err = request_irq(dev->pdev->irq,
  760. dev->hca_type == ARBEL_NATIVE ?
  761. mthca_arbel_interrupt :
  762. mthca_tavor_interrupt,
  763. SA_SHIRQ, DRV_NAME, dev);
  764. if (err)
  765. goto err_out_cmd;
  766. dev->eq_table.have_irq = 1;
  767. }
  768. err = mthca_MAP_EQ(dev, async_mask(dev),
  769. 0, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  770. if (err)
  771. mthca_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  772. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, err);
  773. if (status)
  774. mthca_warn(dev, "MAP_EQ for async EQ %d returned status 0x%02x\n",
  775. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, status);
  776. err = mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  777. 0, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  778. if (err)
  779. mthca_warn(dev, "MAP_EQ for cmd EQ %d failed (%d)\n",
  780. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, err);
  781. if (status)
  782. mthca_warn(dev, "MAP_EQ for cmd EQ %d returned status 0x%02x\n",
  783. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, status);
  784. for (i = 0; i < MTHCA_EQ_CMD; ++i)
  785. if (dev->hca_type == ARBEL_NATIVE)
  786. arbel_eq_req_not(dev, dev->eq_table.eq[i].eqn_mask);
  787. else
  788. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  789. return 0;
  790. err_out_cmd:
  791. mthca_free_irqs(dev);
  792. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_CMD]);
  793. err_out_async:
  794. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  795. err_out_comp:
  796. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_COMP]);
  797. err_out_unmap:
  798. mthca_unmap_eq_regs(dev);
  799. err_out_free:
  800. mthca_alloc_cleanup(&dev->eq_table.alloc);
  801. return err;
  802. }
  803. void __devexit mthca_cleanup_eq_table(struct mthca_dev *dev)
  804. {
  805. u8 status;
  806. int i;
  807. mthca_free_irqs(dev);
  808. mthca_MAP_EQ(dev, async_mask(dev),
  809. 1, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  810. mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  811. 1, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  812. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  813. mthca_free_eq(dev, &dev->eq_table.eq[i]);
  814. mthca_unmap_eq_regs(dev);
  815. mthca_alloc_cleanup(&dev->eq_table.alloc);
  816. }