mthca_eq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. u8 status;
  416. eq->dev = dev;
  417. eq->nent = roundup_pow_of_two(max(nent, 2));
  418. npages = ALIGN(eq->nent * MTHCA_EQ_ENTRY_SIZE, PAGE_SIZE) / PAGE_SIZE;
  419. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  420. GFP_KERNEL);
  421. if (!eq->page_list)
  422. goto err_out;
  423. for (i = 0; i < npages; ++i)
  424. eq->page_list[i].buf = NULL;
  425. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  426. if (!dma_list)
  427. goto err_out_free;
  428. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  429. if (IS_ERR(mailbox))
  430. goto err_out_free;
  431. eq_context = mailbox->buf;
  432. for (i = 0; i < npages; ++i) {
  433. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  434. PAGE_SIZE, &t, GFP_KERNEL);
  435. if (!eq->page_list[i].buf)
  436. goto err_out_free_pages;
  437. dma_list[i] = t;
  438. dma_unmap_addr_set(&eq->page_list[i], mapping, t);
  439. clear_page(eq->page_list[i].buf);
  440. }
  441. for (i = 0; i < eq->nent; ++i)
  442. set_eqe_hw(get_eqe(eq, i));
  443. eq->eqn = mthca_alloc(&dev->eq_table.alloc);
  444. if (eq->eqn == -1)
  445. goto err_out_free_pages;
  446. err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
  447. dma_list, PAGE_SHIFT, npages,
  448. 0, npages * PAGE_SIZE,
  449. MTHCA_MPT_FLAG_LOCAL_WRITE |
  450. MTHCA_MPT_FLAG_LOCAL_READ,
  451. &eq->mr);
  452. if (err)
  453. goto err_out_free_eq;
  454. memset(eq_context, 0, sizeof *eq_context);
  455. eq_context->flags = cpu_to_be32(MTHCA_EQ_STATUS_OK |
  456. MTHCA_EQ_OWNER_HW |
  457. MTHCA_EQ_STATE_ARMED |
  458. MTHCA_EQ_FLAG_TR);
  459. if (mthca_is_memfree(dev))
  460. eq_context->flags |= cpu_to_be32(MTHCA_EQ_STATE_ARBEL);
  461. eq_context->logsize_usrpage = cpu_to_be32((ffs(eq->nent) - 1) << 24);
  462. if (mthca_is_memfree(dev)) {
  463. eq_context->arbel_pd = cpu_to_be32(dev->driver_pd.pd_num);
  464. } else {
  465. eq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);
  466. eq_context->tavor_pd = cpu_to_be32(dev->driver_pd.pd_num);
  467. }
  468. eq_context->intr = intr;
  469. eq_context->lkey = cpu_to_be32(eq->mr.ibmr.lkey);
  470. err = mthca_SW2HW_EQ(dev, mailbox, eq->eqn, &status);
  471. if (err) {
  472. mthca_warn(dev, "SW2HW_EQ failed (%d)\n", err);
  473. goto err_out_free_mr;
  474. }
  475. if (status) {
  476. mthca_warn(dev, "SW2HW_EQ returned status 0x%02x\n",
  477. status);
  478. err = -EINVAL;
  479. goto err_out_free_mr;
  480. }
  481. kfree(dma_list);
  482. mthca_free_mailbox(dev, mailbox);
  483. eq->eqn_mask = swab32(1 << eq->eqn);
  484. eq->cons_index = 0;
  485. dev->eq_table.arm_mask |= eq->eqn_mask;
  486. mthca_dbg(dev, "Allocated EQ %d with %d entries\n",
  487. eq->eqn, eq->nent);
  488. return err;
  489. err_out_free_mr:
  490. mthca_free_mr(dev, &eq->mr);
  491. err_out_free_eq:
  492. mthca_free(&dev->eq_table.alloc, eq->eqn);
  493. err_out_free_pages:
  494. for (i = 0; i < npages; ++i)
  495. if (eq->page_list[i].buf)
  496. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  497. eq->page_list[i].buf,
  498. dma_unmap_addr(&eq->page_list[i],
  499. mapping));
  500. mthca_free_mailbox(dev, mailbox);
  501. err_out_free:
  502. kfree(eq->page_list);
  503. kfree(dma_list);
  504. err_out:
  505. return err;
  506. }
  507. static void mthca_free_eq(struct mthca_dev *dev,
  508. struct mthca_eq *eq)
  509. {
  510. struct mthca_mailbox *mailbox;
  511. int err;
  512. u8 status;
  513. int npages = (eq->nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
  514. PAGE_SIZE;
  515. int i;
  516. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  517. if (IS_ERR(mailbox))
  518. return;
  519. err = mthca_HW2SW_EQ(dev, mailbox, 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", status);
  524. dev->eq_table.arm_mask &= ~eq->eqn_mask;
  525. if (0) {
  526. mthca_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  527. for (i = 0; i < sizeof (struct mthca_eq_context) / 4; ++i) {
  528. if (i % 4 == 0)
  529. printk("[%02x] ", i * 4);
  530. printk(" %08x", be32_to_cpup(mailbox->buf + i * 4));
  531. if ((i + 1) % 4 == 0)
  532. printk("\n");
  533. }
  534. }
  535. mthca_free_mr(dev, &eq->mr);
  536. for (i = 0; i < npages; ++i)
  537. pci_free_consistent(dev->pdev, PAGE_SIZE,
  538. eq->page_list[i].buf,
  539. dma_unmap_addr(&eq->page_list[i], mapping));
  540. kfree(eq->page_list);
  541. mthca_free_mailbox(dev, mailbox);
  542. }
  543. static void mthca_free_irqs(struct mthca_dev *dev)
  544. {
  545. int i;
  546. if (dev->eq_table.have_irq)
  547. free_irq(dev->pdev->irq, dev);
  548. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  549. if (dev->eq_table.eq[i].have_irq) {
  550. free_irq(dev->eq_table.eq[i].msi_x_vector,
  551. dev->eq_table.eq + i);
  552. dev->eq_table.eq[i].have_irq = 0;
  553. }
  554. }
  555. static int mthca_map_reg(struct mthca_dev *dev,
  556. unsigned long offset, unsigned long size,
  557. void __iomem **map)
  558. {
  559. unsigned long base = pci_resource_start(dev->pdev, 0);
  560. *map = ioremap(base + offset, size);
  561. if (!*map)
  562. return -ENOMEM;
  563. return 0;
  564. }
  565. static int mthca_map_eq_regs(struct mthca_dev *dev)
  566. {
  567. if (mthca_is_memfree(dev)) {
  568. /*
  569. * We assume that the EQ arm and EQ set CI registers
  570. * fall within the first BAR. We can't trust the
  571. * values firmware gives us, since those addresses are
  572. * valid on the HCA's side of the PCI bus but not
  573. * necessarily the host side.
  574. */
  575. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  576. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  577. &dev->clr_base)) {
  578. mthca_err(dev, "Couldn't map interrupt clear register, "
  579. "aborting.\n");
  580. return -ENOMEM;
  581. }
  582. /*
  583. * Add 4 because we limit ourselves to EQs 0 ... 31,
  584. * so we only need the low word of the register.
  585. */
  586. if (mthca_map_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  587. dev->fw.arbel.eq_arm_base) + 4, 4,
  588. &dev->eq_regs.arbel.eq_arm)) {
  589. mthca_err(dev, "Couldn't map EQ arm register, aborting.\n");
  590. iounmap(dev->clr_base);
  591. return -ENOMEM;
  592. }
  593. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  594. dev->fw.arbel.eq_set_ci_base,
  595. MTHCA_EQ_SET_CI_SIZE,
  596. &dev->eq_regs.arbel.eq_set_ci_base)) {
  597. mthca_err(dev, "Couldn't map EQ CI register, aborting.\n");
  598. iounmap(dev->eq_regs.arbel.eq_arm);
  599. iounmap(dev->clr_base);
  600. return -ENOMEM;
  601. }
  602. } else {
  603. if (mthca_map_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  604. &dev->clr_base)) {
  605. mthca_err(dev, "Couldn't map interrupt clear register, "
  606. "aborting.\n");
  607. return -ENOMEM;
  608. }
  609. if (mthca_map_reg(dev, MTHCA_ECR_BASE,
  610. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  611. &dev->eq_regs.tavor.ecr_base)) {
  612. mthca_err(dev, "Couldn't map ecr register, "
  613. "aborting.\n");
  614. iounmap(dev->clr_base);
  615. return -ENOMEM;
  616. }
  617. }
  618. return 0;
  619. }
  620. static void mthca_unmap_eq_regs(struct mthca_dev *dev)
  621. {
  622. if (mthca_is_memfree(dev)) {
  623. iounmap(dev->eq_regs.arbel.eq_set_ci_base);
  624. iounmap(dev->eq_regs.arbel.eq_arm);
  625. iounmap(dev->clr_base);
  626. } else {
  627. iounmap(dev->eq_regs.tavor.ecr_base);
  628. iounmap(dev->clr_base);
  629. }
  630. }
  631. int mthca_map_eq_icm(struct mthca_dev *dev, u64 icm_virt)
  632. {
  633. int ret;
  634. u8 status;
  635. /*
  636. * We assume that mapping one page is enough for the whole EQ
  637. * context table. This is fine with all current HCAs, because
  638. * we only use 32 EQs and each EQ uses 32 bytes of context
  639. * memory, or 1 KB total.
  640. */
  641. dev->eq_table.icm_virt = icm_virt;
  642. dev->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
  643. if (!dev->eq_table.icm_page)
  644. return -ENOMEM;
  645. dev->eq_table.icm_dma = pci_map_page(dev->pdev, dev->eq_table.icm_page, 0,
  646. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  647. if (pci_dma_mapping_error(dev->pdev, dev->eq_table.icm_dma)) {
  648. __free_page(dev->eq_table.icm_page);
  649. return -ENOMEM;
  650. }
  651. ret = mthca_MAP_ICM_page(dev, dev->eq_table.icm_dma, icm_virt, &status);
  652. if (!ret && status)
  653. ret = -EINVAL;
  654. if (ret) {
  655. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  656. PCI_DMA_BIDIRECTIONAL);
  657. __free_page(dev->eq_table.icm_page);
  658. }
  659. return ret;
  660. }
  661. void mthca_unmap_eq_icm(struct mthca_dev *dev)
  662. {
  663. u8 status;
  664. mthca_UNMAP_ICM(dev, dev->eq_table.icm_virt, 1, &status);
  665. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  666. PCI_DMA_BIDIRECTIONAL);
  667. __free_page(dev->eq_table.icm_page);
  668. }
  669. int mthca_init_eq_table(struct mthca_dev *dev)
  670. {
  671. int err;
  672. u8 status;
  673. u8 intr;
  674. int i;
  675. err = mthca_alloc_init(&dev->eq_table.alloc,
  676. dev->limits.num_eqs,
  677. dev->limits.num_eqs - 1,
  678. dev->limits.reserved_eqs);
  679. if (err)
  680. return err;
  681. err = mthca_map_eq_regs(dev);
  682. if (err)
  683. goto err_out_free;
  684. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  685. dev->eq_table.clr_mask = 0;
  686. } else {
  687. dev->eq_table.clr_mask =
  688. swab32(1 << (dev->eq_table.inta_pin & 31));
  689. dev->eq_table.clr_int = dev->clr_base +
  690. (dev->eq_table.inta_pin < 32 ? 4 : 0);
  691. }
  692. dev->eq_table.arm_mask = 0;
  693. intr = dev->eq_table.inta_pin;
  694. err = mthca_create_eq(dev, dev->limits.num_cqs + MTHCA_NUM_SPARE_EQE,
  695. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
  696. &dev->eq_table.eq[MTHCA_EQ_COMP]);
  697. if (err)
  698. goto err_out_unmap;
  699. err = mthca_create_eq(dev, MTHCA_NUM_ASYNC_EQE + MTHCA_NUM_SPARE_EQE,
  700. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 129 : intr,
  701. &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  702. if (err)
  703. goto err_out_comp;
  704. err = mthca_create_eq(dev, MTHCA_NUM_CMD_EQE + MTHCA_NUM_SPARE_EQE,
  705. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 130 : intr,
  706. &dev->eq_table.eq[MTHCA_EQ_CMD]);
  707. if (err)
  708. goto err_out_async;
  709. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  710. static const char *eq_name[] = {
  711. [MTHCA_EQ_COMP] = DRV_NAME "-comp",
  712. [MTHCA_EQ_ASYNC] = DRV_NAME "-async",
  713. [MTHCA_EQ_CMD] = DRV_NAME "-cmd"
  714. };
  715. for (i = 0; i < MTHCA_NUM_EQ; ++i) {
  716. snprintf(dev->eq_table.eq[i].irq_name,
  717. IB_DEVICE_NAME_MAX,
  718. "%s@pci:%s", eq_name[i],
  719. pci_name(dev->pdev));
  720. err = request_irq(dev->eq_table.eq[i].msi_x_vector,
  721. mthca_is_memfree(dev) ?
  722. mthca_arbel_msi_x_interrupt :
  723. mthca_tavor_msi_x_interrupt,
  724. 0, dev->eq_table.eq[i].irq_name,
  725. dev->eq_table.eq + i);
  726. if (err)
  727. goto err_out_cmd;
  728. dev->eq_table.eq[i].have_irq = 1;
  729. }
  730. } else {
  731. snprintf(dev->eq_table.eq[0].irq_name, IB_DEVICE_NAME_MAX,
  732. DRV_NAME "@pci:%s", pci_name(dev->pdev));
  733. err = request_irq(dev->pdev->irq,
  734. mthca_is_memfree(dev) ?
  735. mthca_arbel_interrupt :
  736. mthca_tavor_interrupt,
  737. IRQF_SHARED, dev->eq_table.eq[0].irq_name, dev);
  738. if (err)
  739. goto err_out_cmd;
  740. dev->eq_table.have_irq = 1;
  741. }
  742. err = mthca_MAP_EQ(dev, async_mask(dev),
  743. 0, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  744. if (err)
  745. mthca_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  746. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, err);
  747. if (status)
  748. mthca_warn(dev, "MAP_EQ for async EQ %d returned status 0x%02x\n",
  749. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, status);
  750. err = mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  751. 0, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  752. if (err)
  753. mthca_warn(dev, "MAP_EQ for cmd EQ %d failed (%d)\n",
  754. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, err);
  755. if (status)
  756. mthca_warn(dev, "MAP_EQ for cmd EQ %d returned status 0x%02x\n",
  757. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, status);
  758. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  759. if (mthca_is_memfree(dev))
  760. arbel_eq_req_not(dev, dev->eq_table.eq[i].eqn_mask);
  761. else
  762. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  763. return 0;
  764. err_out_cmd:
  765. mthca_free_irqs(dev);
  766. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_CMD]);
  767. err_out_async:
  768. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  769. err_out_comp:
  770. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_COMP]);
  771. err_out_unmap:
  772. mthca_unmap_eq_regs(dev);
  773. err_out_free:
  774. mthca_alloc_cleanup(&dev->eq_table.alloc);
  775. return err;
  776. }
  777. void mthca_cleanup_eq_table(struct mthca_dev *dev)
  778. {
  779. u8 status;
  780. int i;
  781. mthca_free_irqs(dev);
  782. mthca_MAP_EQ(dev, async_mask(dev),
  783. 1, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  784. mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  785. 1, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  786. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  787. mthca_free_eq(dev, &dev->eq_table.eq[i]);
  788. mthca_unmap_eq_regs(dev);
  789. mthca_alloc_cleanup(&dev->eq_table.alloc);
  790. }