mthca_eq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Mellanox Technologies. 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/errno.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/pci.h>
  36. #include <linux/slab.h>
  37. #include "mthca_dev.h"
  38. #include "mthca_cmd.h"
  39. #include "mthca_config_reg.h"
  40. enum {
  41. MTHCA_NUM_ASYNC_EQE = 0x80,
  42. MTHCA_NUM_CMD_EQE = 0x80,
  43. MTHCA_NUM_SPARE_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. __be32 flags;
  51. __be64 start;
  52. __be32 logsize_usrpage;
  53. __be32 tavor_pd; /* reserved for Arbel */
  54. u8 reserved1[3];
  55. u8 intr;
  56. __be32 arbel_pd; /* lost_count for Tavor */
  57. __be32 lkey;
  58. u32 reserved2[2];
  59. __be32 consumer_index;
  60. __be32 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_QP_LAST_WQE = 0x13,
  80. MTHCA_EVENT_TYPE_SRQ_LIMIT = 0x14,
  81. MTHCA_EVENT_TYPE_CQ_ERROR = 0x04,
  82. MTHCA_EVENT_TYPE_WQ_CATAS_ERROR = 0x05,
  83. MTHCA_EVENT_TYPE_EEC_CATAS_ERROR = 0x06,
  84. MTHCA_EVENT_TYPE_PATH_MIG_FAILED = 0x07,
  85. MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR = 0x10,
  86. MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR = 0x11,
  87. MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR = 0x12,
  88. MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR = 0x08,
  89. MTHCA_EVENT_TYPE_PORT_CHANGE = 0x09,
  90. MTHCA_EVENT_TYPE_EQ_OVERFLOW = 0x0f,
  91. MTHCA_EVENT_TYPE_ECC_DETECT = 0x0e,
  92. MTHCA_EVENT_TYPE_CMD = 0x0a
  93. };
  94. #define MTHCA_ASYNC_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_PATH_MIG) | \
  95. (1ULL << MTHCA_EVENT_TYPE_COMM_EST) | \
  96. (1ULL << MTHCA_EVENT_TYPE_SQ_DRAINED) | \
  97. (1ULL << MTHCA_EVENT_TYPE_CQ_ERROR) | \
  98. (1ULL << MTHCA_EVENT_TYPE_WQ_CATAS_ERROR) | \
  99. (1ULL << MTHCA_EVENT_TYPE_EEC_CATAS_ERROR) | \
  100. (1ULL << MTHCA_EVENT_TYPE_PATH_MIG_FAILED) | \
  101. (1ULL << MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
  102. (1ULL << MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR) | \
  103. (1ULL << MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR) | \
  104. (1ULL << MTHCA_EVENT_TYPE_PORT_CHANGE) | \
  105. (1ULL << MTHCA_EVENT_TYPE_ECC_DETECT))
  106. #define MTHCA_SRQ_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR) | \
  107. (1ULL << MTHCA_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
  108. (1ULL << MTHCA_EVENT_TYPE_SRQ_LIMIT))
  109. #define MTHCA_CMD_EVENT_MASK (1ULL << MTHCA_EVENT_TYPE_CMD)
  110. #define MTHCA_EQ_DB_INC_CI (1 << 24)
  111. #define MTHCA_EQ_DB_REQ_NOT (2 << 24)
  112. #define MTHCA_EQ_DB_DISARM_CQ (3 << 24)
  113. #define MTHCA_EQ_DB_SET_CI (4 << 24)
  114. #define MTHCA_EQ_DB_ALWAYS_ARM (5 << 24)
  115. struct mthca_eqe {
  116. u8 reserved1;
  117. u8 type;
  118. u8 reserved2;
  119. u8 subtype;
  120. union {
  121. u32 raw[6];
  122. struct {
  123. __be32 cqn;
  124. } __attribute__((packed)) comp;
  125. struct {
  126. u16 reserved1;
  127. __be16 token;
  128. u32 reserved2;
  129. u8 reserved3[3];
  130. u8 status;
  131. __be64 out_param;
  132. } __attribute__((packed)) cmd;
  133. struct {
  134. __be32 qpn;
  135. } __attribute__((packed)) qp;
  136. struct {
  137. __be32 srqn;
  138. } __attribute__((packed)) srq;
  139. struct {
  140. __be32 cqn;
  141. u32 reserved1;
  142. u8 reserved2[3];
  143. u8 syndrome;
  144. } __attribute__((packed)) cq_err;
  145. struct {
  146. u32 reserved1[2];
  147. __be32 port;
  148. } __attribute__((packed)) port_change;
  149. } event;
  150. u8 reserved3[3];
  151. u8 owner;
  152. } __attribute__((packed));
  153. #define MTHCA_EQ_ENTRY_OWNER_SW (0 << 7)
  154. #define MTHCA_EQ_ENTRY_OWNER_HW (1 << 7)
  155. static inline u64 async_mask(struct mthca_dev *dev)
  156. {
  157. return dev->mthca_flags & MTHCA_FLAG_SRQ ?
  158. MTHCA_ASYNC_EVENT_MASK | MTHCA_SRQ_EVENT_MASK :
  159. MTHCA_ASYNC_EVENT_MASK;
  160. }
  161. static inline void tavor_set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  162. {
  163. /*
  164. * This barrier makes sure that all updates to ownership bits
  165. * done by set_eqe_hw() hit memory before the consumer index
  166. * is updated. set_eq_ci() allows the HCA to possibly write
  167. * more EQ entries, and we want to avoid the exceedingly
  168. * unlikely possibility of the HCA writing an entry and then
  169. * having set_eqe_hw() overwrite the owner field.
  170. */
  171. wmb();
  172. mthca_write64(MTHCA_EQ_DB_SET_CI | eq->eqn, ci & (eq->nent - 1),
  173. dev->kar + MTHCA_EQ_DOORBELL,
  174. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  175. }
  176. static inline void arbel_set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  177. {
  178. /* See comment in tavor_set_eq_ci() above. */
  179. wmb();
  180. __raw_writel((__force u32) cpu_to_be32(ci),
  181. dev->eq_regs.arbel.eq_set_ci_base + eq->eqn * 8);
  182. /* We still want ordering, just not swabbing, so add a barrier */
  183. mb();
  184. }
  185. static inline void set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  186. {
  187. if (mthca_is_memfree(dev))
  188. arbel_set_eq_ci(dev, eq, ci);
  189. else
  190. tavor_set_eq_ci(dev, eq, ci);
  191. }
  192. static inline void tavor_eq_req_not(struct mthca_dev *dev, int eqn)
  193. {
  194. mthca_write64(MTHCA_EQ_DB_REQ_NOT | eqn, 0,
  195. dev->kar + MTHCA_EQ_DOORBELL,
  196. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  197. }
  198. static inline void arbel_eq_req_not(struct mthca_dev *dev, u32 eqn_mask)
  199. {
  200. writel(eqn_mask, dev->eq_regs.arbel.eq_arm);
  201. }
  202. static inline void disarm_cq(struct mthca_dev *dev, int eqn, int cqn)
  203. {
  204. if (!mthca_is_memfree(dev)) {
  205. mthca_write64(MTHCA_EQ_DB_DISARM_CQ | eqn, cqn,
  206. dev->kar + MTHCA_EQ_DOORBELL,
  207. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  208. }
  209. }
  210. static inline struct mthca_eqe *get_eqe(struct mthca_eq *eq, u32 entry)
  211. {
  212. unsigned long off = (entry & (eq->nent - 1)) * MTHCA_EQ_ENTRY_SIZE;
  213. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  214. }
  215. static inline struct mthca_eqe *next_eqe_sw(struct mthca_eq *eq)
  216. {
  217. struct mthca_eqe *eqe;
  218. eqe = get_eqe(eq, eq->cons_index);
  219. return (MTHCA_EQ_ENTRY_OWNER_HW & eqe->owner) ? NULL : eqe;
  220. }
  221. static inline void set_eqe_hw(struct mthca_eqe *eqe)
  222. {
  223. eqe->owner = MTHCA_EQ_ENTRY_OWNER_HW;
  224. }
  225. static void port_change(struct mthca_dev *dev, int port, int active)
  226. {
  227. struct ib_event record;
  228. mthca_dbg(dev, "Port change to %s for port %d\n",
  229. active ? "active" : "down", port);
  230. record.device = &dev->ib_dev;
  231. record.event = active ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  232. record.element.port_num = port;
  233. ib_dispatch_event(&record);
  234. }
  235. static int mthca_eq_int(struct mthca_dev *dev, struct mthca_eq *eq)
  236. {
  237. struct mthca_eqe *eqe;
  238. int disarm_cqn;
  239. int eqes_found = 0;
  240. int set_ci = 0;
  241. while ((eqe = next_eqe_sw(eq))) {
  242. /*
  243. * Make sure we read EQ entry contents after we've
  244. * checked the ownership bit.
  245. */
  246. rmb();
  247. switch (eqe->type) {
  248. case MTHCA_EVENT_TYPE_COMP:
  249. disarm_cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  250. disarm_cq(dev, eq->eqn, disarm_cqn);
  251. mthca_cq_completion(dev, disarm_cqn);
  252. break;
  253. case MTHCA_EVENT_TYPE_PATH_MIG:
  254. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  255. IB_EVENT_PATH_MIG);
  256. break;
  257. case MTHCA_EVENT_TYPE_COMM_EST:
  258. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  259. IB_EVENT_COMM_EST);
  260. break;
  261. case MTHCA_EVENT_TYPE_SQ_DRAINED:
  262. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  263. IB_EVENT_SQ_DRAINED);
  264. break;
  265. case MTHCA_EVENT_TYPE_SRQ_QP_LAST_WQE:
  266. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  267. IB_EVENT_QP_LAST_WQE_REACHED);
  268. break;
  269. case MTHCA_EVENT_TYPE_SRQ_LIMIT:
  270. mthca_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) & 0xffffff,
  271. IB_EVENT_SRQ_LIMIT_REACHED);
  272. break;
  273. case MTHCA_EVENT_TYPE_WQ_CATAS_ERROR:
  274. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  275. IB_EVENT_QP_FATAL);
  276. break;
  277. case MTHCA_EVENT_TYPE_PATH_MIG_FAILED:
  278. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  279. IB_EVENT_PATH_MIG_ERR);
  280. break;
  281. case MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  282. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  283. IB_EVENT_QP_REQ_ERR);
  284. break;
  285. case MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR:
  286. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  287. IB_EVENT_QP_ACCESS_ERR);
  288. break;
  289. case MTHCA_EVENT_TYPE_CMD:
  290. mthca_cmd_event(dev,
  291. be16_to_cpu(eqe->event.cmd.token),
  292. eqe->event.cmd.status,
  293. be64_to_cpu(eqe->event.cmd.out_param));
  294. break;
  295. case MTHCA_EVENT_TYPE_PORT_CHANGE:
  296. port_change(dev,
  297. (be32_to_cpu(eqe->event.port_change.port) >> 28) & 3,
  298. eqe->subtype == 0x4);
  299. break;
  300. case MTHCA_EVENT_TYPE_CQ_ERROR:
  301. mthca_warn(dev, "CQ %s on CQN %06x\n",
  302. eqe->event.cq_err.syndrome == 1 ?
  303. "overrun" : "access violation",
  304. be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
  305. mthca_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
  306. IB_EVENT_CQ_ERR);
  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. ++set_ci;
  324. /*
  325. * The HCA will think the queue has overflowed if we
  326. * don't tell it we've been processing events. We
  327. * create our EQs with MTHCA_NUM_SPARE_EQE extra
  328. * entries, so we must update our consumer index at
  329. * least that often.
  330. */
  331. if (unlikely(set_ci >= MTHCA_NUM_SPARE_EQE)) {
  332. /*
  333. * Conditional on hca_type is OK here because
  334. * this is a rare case, not the fast path.
  335. */
  336. set_eq_ci(dev, eq, eq->cons_index);
  337. set_ci = 0;
  338. }
  339. }
  340. /*
  341. * Rely on caller to set consumer index so that we don't have
  342. * to test hca_type in our interrupt handling fast path.
  343. */
  344. return eqes_found;
  345. }
  346. static irqreturn_t mthca_tavor_interrupt(int irq, void *dev_ptr)
  347. {
  348. struct mthca_dev *dev = dev_ptr;
  349. u32 ecr;
  350. int i;
  351. if (dev->eq_table.clr_mask)
  352. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  353. ecr = readl(dev->eq_regs.tavor.ecr_base + 4);
  354. if (!ecr)
  355. return IRQ_NONE;
  356. writel(ecr, dev->eq_regs.tavor.ecr_base +
  357. MTHCA_ECR_CLR_BASE - MTHCA_ECR_BASE + 4);
  358. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  359. if (ecr & dev->eq_table.eq[i].eqn_mask) {
  360. if (mthca_eq_int(dev, &dev->eq_table.eq[i]))
  361. tavor_set_eq_ci(dev, &dev->eq_table.eq[i],
  362. dev->eq_table.eq[i].cons_index);
  363. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  364. }
  365. return IRQ_HANDLED;
  366. }
  367. static irqreturn_t mthca_tavor_msi_x_interrupt(int irq, void *eq_ptr)
  368. {
  369. struct mthca_eq *eq = eq_ptr;
  370. struct mthca_dev *dev = eq->dev;
  371. mthca_eq_int(dev, eq);
  372. tavor_set_eq_ci(dev, eq, eq->cons_index);
  373. tavor_eq_req_not(dev, eq->eqn);
  374. /* MSI-X vectors always belong to us */
  375. return IRQ_HANDLED;
  376. }
  377. static irqreturn_t mthca_arbel_interrupt(int irq, void *dev_ptr)
  378. {
  379. struct mthca_dev *dev = dev_ptr;
  380. int work = 0;
  381. int i;
  382. if (dev->eq_table.clr_mask)
  383. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  384. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  385. if (mthca_eq_int(dev, &dev->eq_table.eq[i])) {
  386. work = 1;
  387. arbel_set_eq_ci(dev, &dev->eq_table.eq[i],
  388. dev->eq_table.eq[i].cons_index);
  389. }
  390. arbel_eq_req_not(dev, dev->eq_table.arm_mask);
  391. return IRQ_RETVAL(work);
  392. }
  393. static irqreturn_t mthca_arbel_msi_x_interrupt(int irq, void *eq_ptr)
  394. {
  395. struct mthca_eq *eq = eq_ptr;
  396. struct mthca_dev *dev = eq->dev;
  397. mthca_eq_int(dev, eq);
  398. arbel_set_eq_ci(dev, eq, eq->cons_index);
  399. arbel_eq_req_not(dev, eq->eqn_mask);
  400. /* MSI-X vectors always belong to us */
  401. return IRQ_HANDLED;
  402. }
  403. static int mthca_create_eq(struct mthca_dev *dev,
  404. int nent,
  405. u8 intr,
  406. struct mthca_eq *eq)
  407. {
  408. int npages;
  409. u64 *dma_list = NULL;
  410. dma_addr_t t;
  411. struct mthca_mailbox *mailbox;
  412. struct mthca_eq_context *eq_context;
  413. int err = -ENOMEM;
  414. int i;
  415. eq->dev = dev;
  416. eq->nent = roundup_pow_of_two(max(nent, 2));
  417. npages = ALIGN(eq->nent * MTHCA_EQ_ENTRY_SIZE, PAGE_SIZE) / PAGE_SIZE;
  418. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  419. GFP_KERNEL);
  420. if (!eq->page_list)
  421. goto err_out;
  422. for (i = 0; i < npages; ++i)
  423. eq->page_list[i].buf = NULL;
  424. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  425. if (!dma_list)
  426. goto err_out_free;
  427. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  428. if (IS_ERR(mailbox))
  429. goto err_out_free;
  430. eq_context = mailbox->buf;
  431. for (i = 0; i < npages; ++i) {
  432. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  433. PAGE_SIZE, &t, GFP_KERNEL);
  434. if (!eq->page_list[i].buf)
  435. goto err_out_free_pages;
  436. dma_list[i] = t;
  437. dma_unmap_addr_set(&eq->page_list[i], mapping, t);
  438. clear_page(eq->page_list[i].buf);
  439. }
  440. for (i = 0; i < eq->nent; ++i)
  441. set_eqe_hw(get_eqe(eq, i));
  442. eq->eqn = mthca_alloc(&dev->eq_table.alloc);
  443. if (eq->eqn == -1)
  444. goto err_out_free_pages;
  445. err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
  446. dma_list, PAGE_SHIFT, npages,
  447. 0, npages * PAGE_SIZE,
  448. MTHCA_MPT_FLAG_LOCAL_WRITE |
  449. MTHCA_MPT_FLAG_LOCAL_READ,
  450. &eq->mr);
  451. if (err)
  452. goto err_out_free_eq;
  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 (mthca_is_memfree(dev))
  459. eq_context->flags |= cpu_to_be32(MTHCA_EQ_STATE_ARBEL);
  460. eq_context->logsize_usrpage = cpu_to_be32((ffs(eq->nent) - 1) << 24);
  461. if (mthca_is_memfree(dev)) {
  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, mailbox, eq->eqn);
  470. if (err) {
  471. mthca_warn(dev, "SW2HW_EQ returned %d\n", err);
  472. goto err_out_free_mr;
  473. }
  474. kfree(dma_list);
  475. mthca_free_mailbox(dev, mailbox);
  476. eq->eqn_mask = swab32(1 << eq->eqn);
  477. eq->cons_index = 0;
  478. dev->eq_table.arm_mask |= eq->eqn_mask;
  479. mthca_dbg(dev, "Allocated EQ %d with %d entries\n",
  480. eq->eqn, eq->nent);
  481. return err;
  482. err_out_free_mr:
  483. mthca_free_mr(dev, &eq->mr);
  484. err_out_free_eq:
  485. mthca_free(&dev->eq_table.alloc, eq->eqn);
  486. err_out_free_pages:
  487. for (i = 0; i < npages; ++i)
  488. if (eq->page_list[i].buf)
  489. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  490. eq->page_list[i].buf,
  491. dma_unmap_addr(&eq->page_list[i],
  492. mapping));
  493. mthca_free_mailbox(dev, mailbox);
  494. err_out_free:
  495. kfree(eq->page_list);
  496. kfree(dma_list);
  497. err_out:
  498. return err;
  499. }
  500. static void mthca_free_eq(struct mthca_dev *dev,
  501. struct mthca_eq *eq)
  502. {
  503. struct mthca_mailbox *mailbox;
  504. int err;
  505. int npages = (eq->nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
  506. PAGE_SIZE;
  507. int i;
  508. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  509. if (IS_ERR(mailbox))
  510. return;
  511. err = mthca_HW2SW_EQ(dev, mailbox, eq->eqn);
  512. if (err)
  513. mthca_warn(dev, "HW2SW_EQ returned %d\n", err);
  514. dev->eq_table.arm_mask &= ~eq->eqn_mask;
  515. if (0) {
  516. mthca_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  517. for (i = 0; i < sizeof (struct mthca_eq_context) / 4; ++i) {
  518. if (i % 4 == 0)
  519. printk("[%02x] ", i * 4);
  520. printk(" %08x", be32_to_cpup(mailbox->buf + i * 4));
  521. if ((i + 1) % 4 == 0)
  522. printk("\n");
  523. }
  524. }
  525. mthca_free_mr(dev, &eq->mr);
  526. for (i = 0; i < npages; ++i)
  527. pci_free_consistent(dev->pdev, PAGE_SIZE,
  528. eq->page_list[i].buf,
  529. dma_unmap_addr(&eq->page_list[i], mapping));
  530. kfree(eq->page_list);
  531. mthca_free_mailbox(dev, mailbox);
  532. }
  533. static void mthca_free_irqs(struct mthca_dev *dev)
  534. {
  535. int i;
  536. if (dev->eq_table.have_irq)
  537. free_irq(dev->pdev->irq, dev);
  538. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  539. if (dev->eq_table.eq[i].have_irq) {
  540. free_irq(dev->eq_table.eq[i].msi_x_vector,
  541. dev->eq_table.eq + i);
  542. dev->eq_table.eq[i].have_irq = 0;
  543. }
  544. }
  545. static int mthca_map_reg(struct mthca_dev *dev,
  546. unsigned long offset, unsigned long size,
  547. void __iomem **map)
  548. {
  549. phys_addr_t base = pci_resource_start(dev->pdev, 0);
  550. *map = ioremap(base + offset, size);
  551. if (!*map)
  552. return -ENOMEM;
  553. return 0;
  554. }
  555. static int mthca_map_eq_regs(struct mthca_dev *dev)
  556. {
  557. if (mthca_is_memfree(dev)) {
  558. /*
  559. * We assume that the EQ arm and EQ set CI registers
  560. * fall within the first BAR. We can't trust the
  561. * values firmware gives us, since those addresses are
  562. * valid on the HCA's side of the PCI bus but not
  563. * necessarily the host side.
  564. */
  565. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  566. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  567. &dev->clr_base)) {
  568. mthca_err(dev, "Couldn't map interrupt clear register, "
  569. "aborting.\n");
  570. return -ENOMEM;
  571. }
  572. /*
  573. * Add 4 because we limit ourselves to EQs 0 ... 31,
  574. * so we only need the low word of the register.
  575. */
  576. if (mthca_map_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  577. dev->fw.arbel.eq_arm_base) + 4, 4,
  578. &dev->eq_regs.arbel.eq_arm)) {
  579. mthca_err(dev, "Couldn't map EQ arm register, aborting.\n");
  580. iounmap(dev->clr_base);
  581. return -ENOMEM;
  582. }
  583. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  584. dev->fw.arbel.eq_set_ci_base,
  585. MTHCA_EQ_SET_CI_SIZE,
  586. &dev->eq_regs.arbel.eq_set_ci_base)) {
  587. mthca_err(dev, "Couldn't map EQ CI register, aborting.\n");
  588. iounmap(dev->eq_regs.arbel.eq_arm);
  589. iounmap(dev->clr_base);
  590. return -ENOMEM;
  591. }
  592. } else {
  593. if (mthca_map_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  594. &dev->clr_base)) {
  595. mthca_err(dev, "Couldn't map interrupt clear register, "
  596. "aborting.\n");
  597. return -ENOMEM;
  598. }
  599. if (mthca_map_reg(dev, MTHCA_ECR_BASE,
  600. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  601. &dev->eq_regs.tavor.ecr_base)) {
  602. mthca_err(dev, "Couldn't map ecr register, "
  603. "aborting.\n");
  604. iounmap(dev->clr_base);
  605. return -ENOMEM;
  606. }
  607. }
  608. return 0;
  609. }
  610. static void mthca_unmap_eq_regs(struct mthca_dev *dev)
  611. {
  612. if (mthca_is_memfree(dev)) {
  613. iounmap(dev->eq_regs.arbel.eq_set_ci_base);
  614. iounmap(dev->eq_regs.arbel.eq_arm);
  615. iounmap(dev->clr_base);
  616. } else {
  617. iounmap(dev->eq_regs.tavor.ecr_base);
  618. iounmap(dev->clr_base);
  619. }
  620. }
  621. int mthca_map_eq_icm(struct mthca_dev *dev, u64 icm_virt)
  622. {
  623. int ret;
  624. /*
  625. * We assume that mapping one page is enough for the whole EQ
  626. * context table. This is fine with all current HCAs, because
  627. * we only use 32 EQs and each EQ uses 32 bytes of context
  628. * memory, or 1 KB total.
  629. */
  630. dev->eq_table.icm_virt = icm_virt;
  631. dev->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
  632. if (!dev->eq_table.icm_page)
  633. return -ENOMEM;
  634. dev->eq_table.icm_dma = pci_map_page(dev->pdev, dev->eq_table.icm_page, 0,
  635. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  636. if (pci_dma_mapping_error(dev->pdev, dev->eq_table.icm_dma)) {
  637. __free_page(dev->eq_table.icm_page);
  638. return -ENOMEM;
  639. }
  640. ret = mthca_MAP_ICM_page(dev, dev->eq_table.icm_dma, icm_virt);
  641. if (ret) {
  642. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  643. PCI_DMA_BIDIRECTIONAL);
  644. __free_page(dev->eq_table.icm_page);
  645. }
  646. return ret;
  647. }
  648. void mthca_unmap_eq_icm(struct mthca_dev *dev)
  649. {
  650. mthca_UNMAP_ICM(dev, dev->eq_table.icm_virt, 1);
  651. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  652. PCI_DMA_BIDIRECTIONAL);
  653. __free_page(dev->eq_table.icm_page);
  654. }
  655. int mthca_init_eq_table(struct mthca_dev *dev)
  656. {
  657. int err;
  658. u8 intr;
  659. int i;
  660. err = mthca_alloc_init(&dev->eq_table.alloc,
  661. dev->limits.num_eqs,
  662. dev->limits.num_eqs - 1,
  663. dev->limits.reserved_eqs);
  664. if (err)
  665. return err;
  666. err = mthca_map_eq_regs(dev);
  667. if (err)
  668. goto err_out_free;
  669. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  670. dev->eq_table.clr_mask = 0;
  671. } else {
  672. dev->eq_table.clr_mask =
  673. swab32(1 << (dev->eq_table.inta_pin & 31));
  674. dev->eq_table.clr_int = dev->clr_base +
  675. (dev->eq_table.inta_pin < 32 ? 4 : 0);
  676. }
  677. dev->eq_table.arm_mask = 0;
  678. intr = dev->eq_table.inta_pin;
  679. err = mthca_create_eq(dev, dev->limits.num_cqs + MTHCA_NUM_SPARE_EQE,
  680. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
  681. &dev->eq_table.eq[MTHCA_EQ_COMP]);
  682. if (err)
  683. goto err_out_unmap;
  684. err = mthca_create_eq(dev, MTHCA_NUM_ASYNC_EQE + MTHCA_NUM_SPARE_EQE,
  685. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 129 : intr,
  686. &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  687. if (err)
  688. goto err_out_comp;
  689. err = mthca_create_eq(dev, MTHCA_NUM_CMD_EQE + MTHCA_NUM_SPARE_EQE,
  690. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 130 : intr,
  691. &dev->eq_table.eq[MTHCA_EQ_CMD]);
  692. if (err)
  693. goto err_out_async;
  694. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  695. static const char *eq_name[] = {
  696. [MTHCA_EQ_COMP] = DRV_NAME "-comp",
  697. [MTHCA_EQ_ASYNC] = DRV_NAME "-async",
  698. [MTHCA_EQ_CMD] = DRV_NAME "-cmd"
  699. };
  700. for (i = 0; i < MTHCA_NUM_EQ; ++i) {
  701. snprintf(dev->eq_table.eq[i].irq_name,
  702. IB_DEVICE_NAME_MAX,
  703. "%s@pci:%s", eq_name[i],
  704. pci_name(dev->pdev));
  705. err = request_irq(dev->eq_table.eq[i].msi_x_vector,
  706. mthca_is_memfree(dev) ?
  707. mthca_arbel_msi_x_interrupt :
  708. mthca_tavor_msi_x_interrupt,
  709. 0, dev->eq_table.eq[i].irq_name,
  710. dev->eq_table.eq + i);
  711. if (err)
  712. goto err_out_cmd;
  713. dev->eq_table.eq[i].have_irq = 1;
  714. }
  715. } else {
  716. snprintf(dev->eq_table.eq[0].irq_name, IB_DEVICE_NAME_MAX,
  717. DRV_NAME "@pci:%s", pci_name(dev->pdev));
  718. err = request_irq(dev->pdev->irq,
  719. mthca_is_memfree(dev) ?
  720. mthca_arbel_interrupt :
  721. mthca_tavor_interrupt,
  722. IRQF_SHARED, dev->eq_table.eq[0].irq_name, dev);
  723. if (err)
  724. goto err_out_cmd;
  725. dev->eq_table.have_irq = 1;
  726. }
  727. err = mthca_MAP_EQ(dev, async_mask(dev),
  728. 0, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
  729. if (err)
  730. mthca_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  731. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, err);
  732. err = mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  733. 0, dev->eq_table.eq[MTHCA_EQ_CMD].eqn);
  734. if (err)
  735. mthca_warn(dev, "MAP_EQ for cmd EQ %d failed (%d)\n",
  736. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, err);
  737. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  738. if (mthca_is_memfree(dev))
  739. arbel_eq_req_not(dev, dev->eq_table.eq[i].eqn_mask);
  740. else
  741. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  742. return 0;
  743. err_out_cmd:
  744. mthca_free_irqs(dev);
  745. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_CMD]);
  746. err_out_async:
  747. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  748. err_out_comp:
  749. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_COMP]);
  750. err_out_unmap:
  751. mthca_unmap_eq_regs(dev);
  752. err_out_free:
  753. mthca_alloc_cleanup(&dev->eq_table.alloc);
  754. return err;
  755. }
  756. void mthca_cleanup_eq_table(struct mthca_dev *dev)
  757. {
  758. int i;
  759. mthca_free_irqs(dev);
  760. mthca_MAP_EQ(dev, async_mask(dev),
  761. 1, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
  762. mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  763. 1, dev->eq_table.eq[MTHCA_EQ_CMD].eqn);
  764. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  765. mthca_free_eq(dev, &dev->eq_table.eq[i]);
  766. mthca_unmap_eq_regs(dev);
  767. mthca_alloc_cleanup(&dev->eq_table.alloc);
  768. }