mthca_eq.c 25 KB

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