mthca_eq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. * $Id: mthca_eq.c 1382 2004-12-24 02:21:02Z roland $
  34. */
  35. #include <linux/init.h>
  36. #include <linux/errno.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/pci.h>
  39. #include "mthca_dev.h"
  40. #include "mthca_cmd.h"
  41. #include "mthca_config_reg.h"
  42. enum {
  43. MTHCA_NUM_ASYNC_EQE = 0x80,
  44. MTHCA_NUM_CMD_EQE = 0x80,
  45. MTHCA_EQ_ENTRY_SIZE = 0x20
  46. };
  47. /*
  48. * Must be packed because start is 64 bits but only aligned to 32 bits.
  49. */
  50. struct mthca_eq_context {
  51. __be32 flags;
  52. __be64 start;
  53. __be32 logsize_usrpage;
  54. __be32 tavor_pd; /* reserved for Arbel */
  55. u8 reserved1[3];
  56. u8 intr;
  57. __be32 arbel_pd; /* lost_count for Tavor */
  58. __be32 lkey;
  59. u32 reserved2[2];
  60. __be32 consumer_index;
  61. __be32 producer_index;
  62. u32 reserved3[4];
  63. } __attribute__((packed));
  64. #define MTHCA_EQ_STATUS_OK ( 0 << 28)
  65. #define MTHCA_EQ_STATUS_OVERFLOW ( 9 << 28)
  66. #define MTHCA_EQ_STATUS_WRITE_FAIL (10 << 28)
  67. #define MTHCA_EQ_OWNER_SW ( 0 << 24)
  68. #define MTHCA_EQ_OWNER_HW ( 1 << 24)
  69. #define MTHCA_EQ_FLAG_TR ( 1 << 18)
  70. #define MTHCA_EQ_FLAG_OI ( 1 << 17)
  71. #define MTHCA_EQ_STATE_ARMED ( 1 << 8)
  72. #define MTHCA_EQ_STATE_FIRED ( 2 << 8)
  73. #define MTHCA_EQ_STATE_ALWAYS_ARMED ( 3 << 8)
  74. #define MTHCA_EQ_STATE_ARBEL ( 8 << 8)
  75. enum {
  76. MTHCA_EVENT_TYPE_COMP = 0x00,
  77. MTHCA_EVENT_TYPE_PATH_MIG = 0x01,
  78. MTHCA_EVENT_TYPE_COMM_EST = 0x02,
  79. MTHCA_EVENT_TYPE_SQ_DRAINED = 0x03,
  80. MTHCA_EVENT_TYPE_SRQ_LAST_WQE = 0x13,
  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_LAST_WQE)
  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 cqn;
  137. u32 reserved1;
  138. u8 reserved2[3];
  139. u8 syndrome;
  140. } __attribute__((packed)) cq_err;
  141. struct {
  142. u32 reserved1[2];
  143. __be32 port;
  144. } __attribute__((packed)) port_change;
  145. } event;
  146. u8 reserved3[3];
  147. u8 owner;
  148. } __attribute__((packed));
  149. #define MTHCA_EQ_ENTRY_OWNER_SW (0 << 7)
  150. #define MTHCA_EQ_ENTRY_OWNER_HW (1 << 7)
  151. static inline u64 async_mask(struct mthca_dev *dev)
  152. {
  153. return dev->mthca_flags & MTHCA_FLAG_SRQ ?
  154. MTHCA_ASYNC_EVENT_MASK | MTHCA_SRQ_EVENT_MASK :
  155. MTHCA_ASYNC_EVENT_MASK;
  156. }
  157. static inline void tavor_set_eq_ci(struct mthca_dev *dev, struct mthca_eq *eq, u32 ci)
  158. {
  159. __be32 doorbell[2];
  160. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_SET_CI | eq->eqn);
  161. doorbell[1] = cpu_to_be32(ci & (eq->nent - 1));
  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(doorbell,
  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. __be32 doorbell[2];
  194. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_REQ_NOT | eqn);
  195. doorbell[1] = 0;
  196. mthca_write64(doorbell,
  197. dev->kar + MTHCA_EQ_DOORBELL,
  198. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  199. }
  200. static inline void arbel_eq_req_not(struct mthca_dev *dev, u32 eqn_mask)
  201. {
  202. writel(eqn_mask, dev->eq_regs.arbel.eq_arm);
  203. }
  204. static inline void disarm_cq(struct mthca_dev *dev, int eqn, int cqn)
  205. {
  206. if (!mthca_is_memfree(dev)) {
  207. __be32 doorbell[2];
  208. doorbell[0] = cpu_to_be32(MTHCA_EQ_DB_DISARM_CQ | eqn);
  209. doorbell[1] = cpu_to_be32(cqn);
  210. mthca_write64(doorbell,
  211. dev->kar + MTHCA_EQ_DOORBELL,
  212. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  213. }
  214. }
  215. static inline struct mthca_eqe *get_eqe(struct mthca_eq *eq, u32 entry)
  216. {
  217. unsigned long off = (entry & (eq->nent - 1)) * MTHCA_EQ_ENTRY_SIZE;
  218. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  219. }
  220. static inline struct mthca_eqe* next_eqe_sw(struct mthca_eq *eq)
  221. {
  222. struct mthca_eqe* eqe;
  223. eqe = get_eqe(eq, eq->cons_index);
  224. return (MTHCA_EQ_ENTRY_OWNER_HW & eqe->owner) ? NULL : eqe;
  225. }
  226. static inline void set_eqe_hw(struct mthca_eqe *eqe)
  227. {
  228. eqe->owner = MTHCA_EQ_ENTRY_OWNER_HW;
  229. }
  230. static void port_change(struct mthca_dev *dev, int port, int active)
  231. {
  232. struct ib_event record;
  233. mthca_dbg(dev, "Port change to %s for port %d\n",
  234. active ? "active" : "down", port);
  235. record.device = &dev->ib_dev;
  236. record.event = active ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  237. record.element.port_num = port;
  238. ib_dispatch_event(&record);
  239. }
  240. static int mthca_eq_int(struct mthca_dev *dev, struct mthca_eq *eq)
  241. {
  242. struct mthca_eqe *eqe;
  243. int disarm_cqn;
  244. int eqes_found = 0;
  245. while ((eqe = next_eqe_sw(eq))) {
  246. int set_ci = 0;
  247. /*
  248. * Make sure we read EQ entry contents after we've
  249. * checked the ownership bit.
  250. */
  251. rmb();
  252. switch (eqe->type) {
  253. case MTHCA_EVENT_TYPE_COMP:
  254. disarm_cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  255. disarm_cq(dev, eq->eqn, disarm_cqn);
  256. mthca_cq_event(dev, disarm_cqn);
  257. break;
  258. case MTHCA_EVENT_TYPE_PATH_MIG:
  259. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  260. IB_EVENT_PATH_MIG);
  261. break;
  262. case MTHCA_EVENT_TYPE_COMM_EST:
  263. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  264. IB_EVENT_COMM_EST);
  265. break;
  266. case MTHCA_EVENT_TYPE_SQ_DRAINED:
  267. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  268. IB_EVENT_SQ_DRAINED);
  269. break;
  270. case MTHCA_EVENT_TYPE_WQ_CATAS_ERROR:
  271. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  272. IB_EVENT_QP_FATAL);
  273. break;
  274. case MTHCA_EVENT_TYPE_PATH_MIG_FAILED:
  275. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  276. IB_EVENT_PATH_MIG_ERR);
  277. break;
  278. case MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  279. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  280. IB_EVENT_QP_REQ_ERR);
  281. break;
  282. case MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR:
  283. mthca_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
  284. IB_EVENT_QP_ACCESS_ERR);
  285. break;
  286. case MTHCA_EVENT_TYPE_CMD:
  287. mthca_cmd_event(dev,
  288. be16_to_cpu(eqe->event.cmd.token),
  289. eqe->event.cmd.status,
  290. be64_to_cpu(eqe->event.cmd.out_param));
  291. /*
  292. * cmd_event() may add more commands.
  293. * The card will think the queue has overflowed if
  294. * we don't tell it we've been processing events.
  295. */
  296. set_ci = 1;
  297. break;
  298. case MTHCA_EVENT_TYPE_PORT_CHANGE:
  299. port_change(dev,
  300. (be32_to_cpu(eqe->event.port_change.port) >> 28) & 3,
  301. eqe->subtype == 0x4);
  302. break;
  303. case MTHCA_EVENT_TYPE_CQ_ERROR:
  304. mthca_warn(dev, "CQ %s on CQN %06x\n",
  305. eqe->event.cq_err.syndrome == 1 ?
  306. "overrun" : "access violation",
  307. be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
  308. break;
  309. case MTHCA_EVENT_TYPE_EQ_OVERFLOW:
  310. mthca_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
  311. break;
  312. case MTHCA_EVENT_TYPE_EEC_CATAS_ERROR:
  313. case MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR:
  314. case MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR:
  315. case MTHCA_EVENT_TYPE_ECC_DETECT:
  316. default:
  317. mthca_warn(dev, "Unhandled event %02x(%02x) on EQ %d\n",
  318. eqe->type, eqe->subtype, eq->eqn);
  319. break;
  320. };
  321. set_eqe_hw(eqe);
  322. ++eq->cons_index;
  323. eqes_found = 1;
  324. if (unlikely(set_ci)) {
  325. /*
  326. * Conditional on hca_type is OK here because
  327. * this is a rare case, not the fast path.
  328. */
  329. set_eq_ci(dev, eq, eq->cons_index);
  330. set_ci = 0;
  331. }
  332. }
  333. /*
  334. * Rely on caller to set consumer index so that we don't have
  335. * to test hca_type in our interrupt handling fast path.
  336. */
  337. return eqes_found;
  338. }
  339. static irqreturn_t mthca_tavor_interrupt(int irq, void *dev_ptr, struct pt_regs *regs)
  340. {
  341. struct mthca_dev *dev = dev_ptr;
  342. u32 ecr;
  343. int i;
  344. if (dev->eq_table.clr_mask)
  345. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  346. ecr = readl(dev->eq_regs.tavor.ecr_base + 4);
  347. if (ecr) {
  348. writel(ecr, dev->eq_regs.tavor.ecr_base +
  349. MTHCA_ECR_CLR_BASE - MTHCA_ECR_BASE + 4);
  350. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  351. if (ecr & dev->eq_table.eq[i].eqn_mask &&
  352. mthca_eq_int(dev, &dev->eq_table.eq[i])) {
  353. tavor_set_eq_ci(dev, &dev->eq_table.eq[i],
  354. dev->eq_table.eq[i].cons_index);
  355. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  356. }
  357. }
  358. return IRQ_RETVAL(ecr);
  359. }
  360. static irqreturn_t mthca_tavor_msi_x_interrupt(int irq, void *eq_ptr,
  361. struct pt_regs *regs)
  362. {
  363. struct mthca_eq *eq = eq_ptr;
  364. struct mthca_dev *dev = eq->dev;
  365. mthca_eq_int(dev, eq);
  366. tavor_set_eq_ci(dev, eq, eq->cons_index);
  367. tavor_eq_req_not(dev, eq->eqn);
  368. /* MSI-X vectors always belong to us */
  369. return IRQ_HANDLED;
  370. }
  371. static irqreturn_t mthca_arbel_interrupt(int irq, void *dev_ptr, struct pt_regs *regs)
  372. {
  373. struct mthca_dev *dev = dev_ptr;
  374. int work = 0;
  375. int i;
  376. if (dev->eq_table.clr_mask)
  377. writel(dev->eq_table.clr_mask, dev->eq_table.clr_int);
  378. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  379. if (mthca_eq_int(dev, &dev->eq_table.eq[i])) {
  380. work = 1;
  381. arbel_set_eq_ci(dev, &dev->eq_table.eq[i],
  382. dev->eq_table.eq[i].cons_index);
  383. }
  384. arbel_eq_req_not(dev, dev->eq_table.arm_mask);
  385. return IRQ_RETVAL(work);
  386. }
  387. static irqreturn_t mthca_arbel_msi_x_interrupt(int irq, void *eq_ptr,
  388. struct pt_regs *regs)
  389. {
  390. struct mthca_eq *eq = eq_ptr;
  391. struct mthca_dev *dev = eq->dev;
  392. mthca_eq_int(dev, eq);
  393. arbel_set_eq_ci(dev, eq, eq->cons_index);
  394. arbel_eq_req_not(dev, eq->eqn_mask);
  395. /* MSI-X vectors always belong to us */
  396. return IRQ_HANDLED;
  397. }
  398. static int __devinit mthca_create_eq(struct mthca_dev *dev,
  399. int nent,
  400. u8 intr,
  401. struct mthca_eq *eq)
  402. {
  403. int npages = (nent * MTHCA_EQ_ENTRY_SIZE + PAGE_SIZE - 1) /
  404. PAGE_SIZE;
  405. u64 *dma_list = NULL;
  406. dma_addr_t t;
  407. struct mthca_mailbox *mailbox;
  408. struct mthca_eq_context *eq_context;
  409. int err = -ENOMEM;
  410. int i;
  411. u8 status;
  412. /* Make sure EQ size is aligned to a power of 2 size. */
  413. for (i = 1; i < nent; i <<= 1)
  414. ; /* nothing */
  415. nent = i;
  416. eq->dev = dev;
  417. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  418. GFP_KERNEL);
  419. if (!eq->page_list)
  420. goto err_out;
  421. for (i = 0; i < npages; ++i)
  422. eq->page_list[i].buf = NULL;
  423. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  424. if (!dma_list)
  425. goto err_out_free;
  426. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  427. if (IS_ERR(mailbox))
  428. goto err_out_free;
  429. eq_context = mailbox->buf;
  430. for (i = 0; i < npages; ++i) {
  431. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  432. PAGE_SIZE, &t, GFP_KERNEL);
  433. if (!eq->page_list[i].buf)
  434. goto err_out_free_pages;
  435. dma_list[i] = t;
  436. pci_unmap_addr_set(&eq->page_list[i], mapping, t);
  437. memset(eq->page_list[i].buf, 0, PAGE_SIZE);
  438. }
  439. for (i = 0; i < nent; ++i)
  440. set_eqe_hw(get_eqe(eq, i));
  441. eq->eqn = mthca_alloc(&dev->eq_table.alloc);
  442. if (eq->eqn == -1)
  443. goto err_out_free_pages;
  444. err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
  445. dma_list, PAGE_SHIFT, npages,
  446. 0, npages * PAGE_SIZE,
  447. MTHCA_MPT_FLAG_LOCAL_WRITE |
  448. MTHCA_MPT_FLAG_LOCAL_READ,
  449. &eq->mr);
  450. if (err)
  451. goto err_out_free_eq;
  452. eq->nent = nent;
  453. memset(eq_context, 0, sizeof *eq_context);
  454. eq_context->flags = cpu_to_be32(MTHCA_EQ_STATUS_OK |
  455. MTHCA_EQ_OWNER_HW |
  456. MTHCA_EQ_STATE_ARMED |
  457. MTHCA_EQ_FLAG_TR);
  458. if (mthca_is_memfree(dev))
  459. eq_context->flags |= cpu_to_be32(MTHCA_EQ_STATE_ARBEL);
  460. eq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);
  461. if (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, 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 __devinit 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. if (!request_mem_region(base + offset, size, DRV_NAME))
  558. return -EBUSY;
  559. *map = ioremap(base + offset, size);
  560. if (!*map) {
  561. release_mem_region(base + offset, size);
  562. return -ENOMEM;
  563. }
  564. return 0;
  565. }
  566. static void mthca_unmap_reg(struct mthca_dev *dev, unsigned long offset,
  567. unsigned long size, void __iomem *map)
  568. {
  569. unsigned long base = pci_resource_start(dev->pdev, 0);
  570. release_mem_region(base + offset, size);
  571. iounmap(map);
  572. }
  573. static int __devinit mthca_map_eq_regs(struct mthca_dev *dev)
  574. {
  575. unsigned long mthca_base;
  576. mthca_base = pci_resource_start(dev->pdev, 0);
  577. if (mthca_is_memfree(dev)) {
  578. /*
  579. * We assume that the EQ arm and EQ set CI registers
  580. * fall within the first BAR. We can't trust the
  581. * values firmware gives us, since those addresses are
  582. * valid on the HCA's side of the PCI bus but not
  583. * necessarily the host side.
  584. */
  585. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  586. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  587. &dev->clr_base)) {
  588. mthca_err(dev, "Couldn't map interrupt clear register, "
  589. "aborting.\n");
  590. return -ENOMEM;
  591. }
  592. /*
  593. * Add 4 because we limit ourselves to EQs 0 ... 31,
  594. * so we only need the low word of the register.
  595. */
  596. if (mthca_map_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  597. dev->fw.arbel.eq_arm_base) + 4, 4,
  598. &dev->eq_regs.arbel.eq_arm)) {
  599. mthca_err(dev, "Couldn't map EQ arm register, aborting.\n");
  600. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  601. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  602. dev->clr_base);
  603. return -ENOMEM;
  604. }
  605. if (mthca_map_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  606. dev->fw.arbel.eq_set_ci_base,
  607. MTHCA_EQ_SET_CI_SIZE,
  608. &dev->eq_regs.arbel.eq_set_ci_base)) {
  609. mthca_err(dev, "Couldn't map EQ CI register, aborting.\n");
  610. mthca_unmap_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  611. dev->fw.arbel.eq_arm_base) + 4, 4,
  612. dev->eq_regs.arbel.eq_arm);
  613. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  614. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  615. dev->clr_base);
  616. return -ENOMEM;
  617. }
  618. } else {
  619. if (mthca_map_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  620. &dev->clr_base)) {
  621. mthca_err(dev, "Couldn't map interrupt clear register, "
  622. "aborting.\n");
  623. return -ENOMEM;
  624. }
  625. if (mthca_map_reg(dev, MTHCA_ECR_BASE,
  626. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  627. &dev->eq_regs.tavor.ecr_base)) {
  628. mthca_err(dev, "Couldn't map ecr register, "
  629. "aborting.\n");
  630. mthca_unmap_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  631. dev->clr_base);
  632. return -ENOMEM;
  633. }
  634. }
  635. return 0;
  636. }
  637. static void __devexit mthca_unmap_eq_regs(struct mthca_dev *dev)
  638. {
  639. if (mthca_is_memfree(dev)) {
  640. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  641. dev->fw.arbel.eq_set_ci_base,
  642. MTHCA_EQ_SET_CI_SIZE,
  643. dev->eq_regs.arbel.eq_set_ci_base);
  644. mthca_unmap_reg(dev, ((pci_resource_len(dev->pdev, 0) - 1) &
  645. dev->fw.arbel.eq_arm_base) + 4, 4,
  646. dev->eq_regs.arbel.eq_arm);
  647. mthca_unmap_reg(dev, (pci_resource_len(dev->pdev, 0) - 1) &
  648. dev->fw.arbel.clr_int_base, MTHCA_CLR_INT_SIZE,
  649. dev->clr_base);
  650. } else {
  651. mthca_unmap_reg(dev, MTHCA_ECR_BASE,
  652. MTHCA_ECR_SIZE + MTHCA_ECR_CLR_SIZE,
  653. dev->eq_regs.tavor.ecr_base);
  654. mthca_unmap_reg(dev, MTHCA_CLR_INT_BASE, MTHCA_CLR_INT_SIZE,
  655. dev->clr_base);
  656. }
  657. }
  658. int __devinit mthca_map_eq_icm(struct mthca_dev *dev, u64 icm_virt)
  659. {
  660. int ret;
  661. u8 status;
  662. /*
  663. * We assume that mapping one page is enough for the whole EQ
  664. * context table. This is fine with all current HCAs, because
  665. * we only use 32 EQs and each EQ uses 32 bytes of context
  666. * memory, or 1 KB total.
  667. */
  668. dev->eq_table.icm_virt = icm_virt;
  669. dev->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
  670. if (!dev->eq_table.icm_page)
  671. return -ENOMEM;
  672. dev->eq_table.icm_dma = pci_map_page(dev->pdev, dev->eq_table.icm_page, 0,
  673. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  674. if (pci_dma_mapping_error(dev->eq_table.icm_dma)) {
  675. __free_page(dev->eq_table.icm_page);
  676. return -ENOMEM;
  677. }
  678. ret = mthca_MAP_ICM_page(dev, dev->eq_table.icm_dma, icm_virt, &status);
  679. if (!ret && status)
  680. ret = -EINVAL;
  681. if (ret) {
  682. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  683. PCI_DMA_BIDIRECTIONAL);
  684. __free_page(dev->eq_table.icm_page);
  685. }
  686. return ret;
  687. }
  688. void __devexit mthca_unmap_eq_icm(struct mthca_dev *dev)
  689. {
  690. u8 status;
  691. mthca_UNMAP_ICM(dev, dev->eq_table.icm_virt, PAGE_SIZE / 4096, &status);
  692. pci_unmap_page(dev->pdev, dev->eq_table.icm_dma, PAGE_SIZE,
  693. PCI_DMA_BIDIRECTIONAL);
  694. __free_page(dev->eq_table.icm_page);
  695. }
  696. int __devinit mthca_init_eq_table(struct mthca_dev *dev)
  697. {
  698. int err;
  699. u8 status;
  700. u8 intr;
  701. int i;
  702. err = mthca_alloc_init(&dev->eq_table.alloc,
  703. dev->limits.num_eqs,
  704. dev->limits.num_eqs - 1,
  705. dev->limits.reserved_eqs);
  706. if (err)
  707. return err;
  708. err = mthca_map_eq_regs(dev);
  709. if (err)
  710. goto err_out_free;
  711. if (dev->mthca_flags & MTHCA_FLAG_MSI ||
  712. dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  713. dev->eq_table.clr_mask = 0;
  714. } else {
  715. dev->eq_table.clr_mask =
  716. swab32(1 << (dev->eq_table.inta_pin & 31));
  717. dev->eq_table.clr_int = dev->clr_base +
  718. (dev->eq_table.inta_pin < 31 ? 4 : 0);
  719. }
  720. dev->eq_table.arm_mask = 0;
  721. intr = (dev->mthca_flags & MTHCA_FLAG_MSI) ?
  722. 128 : dev->eq_table.inta_pin;
  723. err = mthca_create_eq(dev, dev->limits.num_cqs,
  724. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 128 : intr,
  725. &dev->eq_table.eq[MTHCA_EQ_COMP]);
  726. if (err)
  727. goto err_out_unmap;
  728. err = mthca_create_eq(dev, MTHCA_NUM_ASYNC_EQE,
  729. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 129 : intr,
  730. &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  731. if (err)
  732. goto err_out_comp;
  733. err = mthca_create_eq(dev, MTHCA_NUM_CMD_EQE,
  734. (dev->mthca_flags & MTHCA_FLAG_MSI_X) ? 130 : intr,
  735. &dev->eq_table.eq[MTHCA_EQ_CMD]);
  736. if (err)
  737. goto err_out_async;
  738. if (dev->mthca_flags & MTHCA_FLAG_MSI_X) {
  739. static const char *eq_name[] = {
  740. [MTHCA_EQ_COMP] = DRV_NAME " (comp)",
  741. [MTHCA_EQ_ASYNC] = DRV_NAME " (async)",
  742. [MTHCA_EQ_CMD] = DRV_NAME " (cmd)"
  743. };
  744. for (i = 0; i < MTHCA_NUM_EQ; ++i) {
  745. err = request_irq(dev->eq_table.eq[i].msi_x_vector,
  746. mthca_is_memfree(dev) ?
  747. mthca_arbel_msi_x_interrupt :
  748. mthca_tavor_msi_x_interrupt,
  749. 0, eq_name[i], dev->eq_table.eq + i);
  750. if (err)
  751. goto err_out_cmd;
  752. dev->eq_table.eq[i].have_irq = 1;
  753. }
  754. } else {
  755. err = request_irq(dev->pdev->irq,
  756. mthca_is_memfree(dev) ?
  757. mthca_arbel_interrupt :
  758. mthca_tavor_interrupt,
  759. SA_SHIRQ, DRV_NAME, dev);
  760. if (err)
  761. goto err_out_cmd;
  762. dev->eq_table.have_irq = 1;
  763. }
  764. err = mthca_MAP_EQ(dev, async_mask(dev),
  765. 0, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  766. if (err)
  767. mthca_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  768. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, err);
  769. if (status)
  770. mthca_warn(dev, "MAP_EQ for async EQ %d returned status 0x%02x\n",
  771. dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, status);
  772. err = mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  773. 0, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  774. if (err)
  775. mthca_warn(dev, "MAP_EQ for cmd EQ %d failed (%d)\n",
  776. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, err);
  777. if (status)
  778. mthca_warn(dev, "MAP_EQ for cmd EQ %d returned status 0x%02x\n",
  779. dev->eq_table.eq[MTHCA_EQ_CMD].eqn, status);
  780. for (i = 0; i < MTHCA_EQ_CMD; ++i)
  781. if (mthca_is_memfree(dev))
  782. arbel_eq_req_not(dev, dev->eq_table.eq[i].eqn_mask);
  783. else
  784. tavor_eq_req_not(dev, dev->eq_table.eq[i].eqn);
  785. return 0;
  786. err_out_cmd:
  787. mthca_free_irqs(dev);
  788. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_CMD]);
  789. err_out_async:
  790. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_ASYNC]);
  791. err_out_comp:
  792. mthca_free_eq(dev, &dev->eq_table.eq[MTHCA_EQ_COMP]);
  793. err_out_unmap:
  794. mthca_unmap_eq_regs(dev);
  795. err_out_free:
  796. mthca_alloc_cleanup(&dev->eq_table.alloc);
  797. return err;
  798. }
  799. void __devexit mthca_cleanup_eq_table(struct mthca_dev *dev)
  800. {
  801. u8 status;
  802. int i;
  803. mthca_free_irqs(dev);
  804. mthca_MAP_EQ(dev, async_mask(dev),
  805. 1, dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn, &status);
  806. mthca_MAP_EQ(dev, MTHCA_CMD_EVENT_MASK,
  807. 1, dev->eq_table.eq[MTHCA_EQ_CMD].eqn, &status);
  808. for (i = 0; i < MTHCA_NUM_EQ; ++i)
  809. mthca_free_eq(dev, &dev->eq_table.eq[i]);
  810. mthca_unmap_eq_regs(dev);
  811. mthca_alloc_cleanup(&dev->eq_table.alloc);
  812. }