mthca_eq.c 26 KB

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