eq.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. 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/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/slab.h>
  36. #include <linux/export.h>
  37. #include <linux/mm.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/mlx4/cmd.h>
  40. #include "mlx4.h"
  41. #include "fw.h"
  42. enum {
  43. MLX4_IRQNAME_SIZE = 32
  44. };
  45. enum {
  46. MLX4_NUM_ASYNC_EQE = 0x100,
  47. MLX4_NUM_SPARE_EQE = 0x80,
  48. MLX4_EQ_ENTRY_SIZE = 0x20
  49. };
  50. #define MLX4_EQ_STATUS_OK ( 0 << 28)
  51. #define MLX4_EQ_STATUS_WRITE_FAIL (10 << 28)
  52. #define MLX4_EQ_OWNER_SW ( 0 << 24)
  53. #define MLX4_EQ_OWNER_HW ( 1 << 24)
  54. #define MLX4_EQ_FLAG_EC ( 1 << 18)
  55. #define MLX4_EQ_FLAG_OI ( 1 << 17)
  56. #define MLX4_EQ_STATE_ARMED ( 9 << 8)
  57. #define MLX4_EQ_STATE_FIRED (10 << 8)
  58. #define MLX4_EQ_STATE_ALWAYS_ARMED (11 << 8)
  59. #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG) | \
  60. (1ull << MLX4_EVENT_TYPE_COMM_EST) | \
  61. (1ull << MLX4_EVENT_TYPE_SQ_DRAINED) | \
  62. (1ull << MLX4_EVENT_TYPE_CQ_ERROR) | \
  63. (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR) | \
  64. (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR) | \
  65. (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED) | \
  66. (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
  67. (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR) | \
  68. (1ull << MLX4_EVENT_TYPE_PORT_CHANGE) | \
  69. (1ull << MLX4_EVENT_TYPE_ECC_DETECT) | \
  70. (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR) | \
  71. (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
  72. (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT) | \
  73. (1ull << MLX4_EVENT_TYPE_CMD) | \
  74. (1ull << MLX4_EVENT_TYPE_COMM_CHANNEL) | \
  75. (1ull << MLX4_EVENT_TYPE_FLR_EVENT))
  76. static void eq_set_ci(struct mlx4_eq *eq, int req_not)
  77. {
  78. __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
  79. req_not << 31),
  80. eq->doorbell);
  81. /* We still want ordering, just not swabbing, so add a barrier */
  82. mb();
  83. }
  84. static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
  85. {
  86. unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
  87. return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
  88. }
  89. static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
  90. {
  91. struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
  92. return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
  93. }
  94. static struct mlx4_eqe *next_slave_event_eqe(struct mlx4_slave_event_eq *slave_eq)
  95. {
  96. struct mlx4_eqe *eqe =
  97. &slave_eq->event_eqe[slave_eq->cons & (SLAVE_EVENT_EQ_SIZE - 1)];
  98. return (!!(eqe->owner & 0x80) ^
  99. !!(slave_eq->cons & SLAVE_EVENT_EQ_SIZE)) ?
  100. eqe : NULL;
  101. }
  102. void mlx4_gen_slave_eqe(struct work_struct *work)
  103. {
  104. struct mlx4_mfunc_master_ctx *master =
  105. container_of(work, struct mlx4_mfunc_master_ctx,
  106. slave_event_work);
  107. struct mlx4_mfunc *mfunc =
  108. container_of(master, struct mlx4_mfunc, master);
  109. struct mlx4_priv *priv = container_of(mfunc, struct mlx4_priv, mfunc);
  110. struct mlx4_dev *dev = &priv->dev;
  111. struct mlx4_slave_event_eq *slave_eq = &mfunc->master.slave_eq;
  112. struct mlx4_eqe *eqe;
  113. u8 slave;
  114. int i;
  115. for (eqe = next_slave_event_eqe(slave_eq); eqe;
  116. eqe = next_slave_event_eqe(slave_eq)) {
  117. slave = eqe->slave_id;
  118. /* All active slaves need to receive the event */
  119. if (slave == ALL_SLAVES) {
  120. for (i = 0; i < dev->num_slaves; i++) {
  121. if (i != dev->caps.function &&
  122. master->slave_state[i].active)
  123. if (mlx4_GEN_EQE(dev, i, eqe))
  124. mlx4_warn(dev, "Failed to "
  125. " generate event "
  126. "for slave %d\n", i);
  127. }
  128. } else {
  129. if (mlx4_GEN_EQE(dev, slave, eqe))
  130. mlx4_warn(dev, "Failed to generate event "
  131. "for slave %d\n", slave);
  132. }
  133. ++slave_eq->cons;
  134. }
  135. }
  136. static void slave_event(struct mlx4_dev *dev, u8 slave, struct mlx4_eqe *eqe)
  137. {
  138. struct mlx4_priv *priv = mlx4_priv(dev);
  139. struct mlx4_slave_event_eq *slave_eq = &priv->mfunc.master.slave_eq;
  140. struct mlx4_eqe *s_eqe =
  141. &slave_eq->event_eqe[slave_eq->prod & (SLAVE_EVENT_EQ_SIZE - 1)];
  142. if ((!!(s_eqe->owner & 0x80)) ^
  143. (!!(slave_eq->prod & SLAVE_EVENT_EQ_SIZE))) {
  144. mlx4_warn(dev, "Master failed to generate an EQE for slave: %d. "
  145. "No free EQE on slave events queue\n", slave);
  146. return;
  147. }
  148. memcpy(s_eqe, eqe, sizeof(struct mlx4_eqe) - 1);
  149. s_eqe->slave_id = slave;
  150. /* ensure all information is written before setting the ownersip bit */
  151. wmb();
  152. s_eqe->owner = !!(slave_eq->prod & SLAVE_EVENT_EQ_SIZE) ? 0x0 : 0x80;
  153. ++slave_eq->prod;
  154. queue_work(priv->mfunc.master.comm_wq,
  155. &priv->mfunc.master.slave_event_work);
  156. }
  157. static void mlx4_slave_event(struct mlx4_dev *dev, int slave,
  158. struct mlx4_eqe *eqe)
  159. {
  160. struct mlx4_priv *priv = mlx4_priv(dev);
  161. struct mlx4_slave_state *s_slave =
  162. &priv->mfunc.master.slave_state[slave];
  163. if (!s_slave->active) {
  164. /*mlx4_warn(dev, "Trying to pass event to inactive slave\n");*/
  165. return;
  166. }
  167. slave_event(dev, slave, eqe);
  168. }
  169. void mlx4_master_handle_slave_flr(struct work_struct *work)
  170. {
  171. struct mlx4_mfunc_master_ctx *master =
  172. container_of(work, struct mlx4_mfunc_master_ctx,
  173. slave_flr_event_work);
  174. struct mlx4_mfunc *mfunc =
  175. container_of(master, struct mlx4_mfunc, master);
  176. struct mlx4_priv *priv =
  177. container_of(mfunc, struct mlx4_priv, mfunc);
  178. struct mlx4_dev *dev = &priv->dev;
  179. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  180. int i;
  181. int err;
  182. mlx4_dbg(dev, "mlx4_handle_slave_flr\n");
  183. for (i = 0 ; i < dev->num_slaves; i++) {
  184. if (MLX4_COMM_CMD_FLR == slave_state[i].last_cmd) {
  185. mlx4_dbg(dev, "mlx4_handle_slave_flr: "
  186. "clean slave: %d\n", i);
  187. mlx4_delete_all_resources_for_slave(dev, i);
  188. /*return the slave to running mode*/
  189. spin_lock(&priv->mfunc.master.slave_state_lock);
  190. slave_state[i].last_cmd = MLX4_COMM_CMD_RESET;
  191. slave_state[i].is_slave_going_down = 0;
  192. spin_unlock(&priv->mfunc.master.slave_state_lock);
  193. /*notify the FW:*/
  194. err = mlx4_cmd(dev, 0, i, 0, MLX4_CMD_INFORM_FLR_DONE,
  195. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  196. if (err)
  197. mlx4_warn(dev, "Failed to notify FW on "
  198. "FLR done (slave:%d)\n", i);
  199. }
  200. }
  201. }
  202. static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
  203. {
  204. struct mlx4_priv *priv = mlx4_priv(dev);
  205. struct mlx4_eqe *eqe;
  206. int cqn;
  207. int eqes_found = 0;
  208. int set_ci = 0;
  209. int port;
  210. int slave = 0;
  211. int ret;
  212. u32 flr_slave;
  213. u8 update_slave_state;
  214. int i;
  215. while ((eqe = next_eqe_sw(eq))) {
  216. /*
  217. * Make sure we read EQ entry contents after we've
  218. * checked the ownership bit.
  219. */
  220. rmb();
  221. switch (eqe->type) {
  222. case MLX4_EVENT_TYPE_COMP:
  223. cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
  224. mlx4_cq_completion(dev, cqn);
  225. break;
  226. case MLX4_EVENT_TYPE_PATH_MIG:
  227. case MLX4_EVENT_TYPE_COMM_EST:
  228. case MLX4_EVENT_TYPE_SQ_DRAINED:
  229. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  230. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  231. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  232. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  233. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  234. mlx4_dbg(dev, "event %d arrived\n", eqe->type);
  235. if (mlx4_is_master(dev)) {
  236. /* forward only to slave owning the QP */
  237. ret = mlx4_get_slave_from_resource_id(dev,
  238. RES_QP,
  239. be32_to_cpu(eqe->event.qp.qpn)
  240. & 0xffffff, &slave);
  241. if (ret && ret != -ENOENT) {
  242. mlx4_dbg(dev, "QP event %02x(%02x) on "
  243. "EQ %d at index %u: could "
  244. "not get slave id (%d)\n",
  245. eqe->type, eqe->subtype,
  246. eq->eqn, eq->cons_index, ret);
  247. break;
  248. }
  249. if (!ret && slave != dev->caps.function) {
  250. mlx4_slave_event(dev, slave, eqe);
  251. break;
  252. }
  253. }
  254. mlx4_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) &
  255. 0xffffff, eqe->type);
  256. break;
  257. case MLX4_EVENT_TYPE_SRQ_LIMIT:
  258. mlx4_warn(dev, "%s: MLX4_EVENT_TYPE_SRQ_LIMIT\n",
  259. __func__);
  260. case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
  261. if (mlx4_is_master(dev)) {
  262. /* forward only to slave owning the SRQ */
  263. ret = mlx4_get_slave_from_resource_id(dev,
  264. RES_SRQ,
  265. be32_to_cpu(eqe->event.srq.srqn)
  266. & 0xffffff,
  267. &slave);
  268. if (ret && ret != -ENOENT) {
  269. mlx4_warn(dev, "SRQ event %02x(%02x) "
  270. "on EQ %d at index %u: could"
  271. " not get slave id (%d)\n",
  272. eqe->type, eqe->subtype,
  273. eq->eqn, eq->cons_index, ret);
  274. break;
  275. }
  276. mlx4_warn(dev, "%s: slave:%d, srq_no:0x%x,"
  277. " event: %02x(%02x)\n", __func__,
  278. slave,
  279. be32_to_cpu(eqe->event.srq.srqn),
  280. eqe->type, eqe->subtype);
  281. if (!ret && slave != dev->caps.function) {
  282. mlx4_warn(dev, "%s: sending event "
  283. "%02x(%02x) to slave:%d\n",
  284. __func__, eqe->type,
  285. eqe->subtype, slave);
  286. mlx4_slave_event(dev, slave, eqe);
  287. break;
  288. }
  289. }
  290. mlx4_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) &
  291. 0xffffff, eqe->type);
  292. break;
  293. case MLX4_EVENT_TYPE_CMD:
  294. mlx4_cmd_event(dev,
  295. be16_to_cpu(eqe->event.cmd.token),
  296. eqe->event.cmd.status,
  297. be64_to_cpu(eqe->event.cmd.out_param));
  298. break;
  299. case MLX4_EVENT_TYPE_PORT_CHANGE:
  300. port = be32_to_cpu(eqe->event.port_change.port) >> 28;
  301. if (eqe->subtype == MLX4_PORT_CHANGE_SUBTYPE_DOWN) {
  302. mlx4_dispatch_event(dev,
  303. MLX4_DEV_EVENT_PORT_DOWN,
  304. port);
  305. mlx4_priv(dev)->sense.do_sense_port[port] = 1;
  306. if (mlx4_is_master(dev))
  307. /*change the state of all slave's port
  308. * to down:*/
  309. for (i = 0; i < dev->num_slaves; i++) {
  310. mlx4_dbg(dev, "%s: Sending "
  311. "MLX4_PORT_CHANGE_SUBTYPE_DOWN"
  312. " to slave: %d, port:%d\n",
  313. __func__, i, port);
  314. if (i == dev->caps.function)
  315. continue;
  316. mlx4_slave_event(dev, i, eqe);
  317. }
  318. } else {
  319. mlx4_dispatch_event(dev,
  320. MLX4_DEV_EVENT_PORT_UP,
  321. port);
  322. mlx4_priv(dev)->sense.do_sense_port[port] = 0;
  323. if (mlx4_is_master(dev)) {
  324. for (i = 0; i < dev->num_slaves; i++) {
  325. if (i == dev->caps.function)
  326. continue;
  327. mlx4_slave_event(dev, i, eqe);
  328. }
  329. }
  330. }
  331. break;
  332. case MLX4_EVENT_TYPE_CQ_ERROR:
  333. mlx4_warn(dev, "CQ %s on CQN %06x\n",
  334. eqe->event.cq_err.syndrome == 1 ?
  335. "overrun" : "access violation",
  336. be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
  337. if (mlx4_is_master(dev)) {
  338. ret = mlx4_get_slave_from_resource_id(dev,
  339. RES_CQ,
  340. be32_to_cpu(eqe->event.cq_err.cqn)
  341. & 0xffffff, &slave);
  342. if (ret && ret != -ENOENT) {
  343. mlx4_dbg(dev, "CQ event %02x(%02x) on "
  344. "EQ %d at index %u: could "
  345. "not get slave id (%d)\n",
  346. eqe->type, eqe->subtype,
  347. eq->eqn, eq->cons_index, ret);
  348. break;
  349. }
  350. if (!ret && slave != dev->caps.function) {
  351. mlx4_slave_event(dev, slave, eqe);
  352. break;
  353. }
  354. }
  355. mlx4_cq_event(dev,
  356. be32_to_cpu(eqe->event.cq_err.cqn)
  357. & 0xffffff,
  358. eqe->type);
  359. break;
  360. case MLX4_EVENT_TYPE_EQ_OVERFLOW:
  361. mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
  362. break;
  363. case MLX4_EVENT_TYPE_COMM_CHANNEL:
  364. if (!mlx4_is_master(dev)) {
  365. mlx4_warn(dev, "Received comm channel event "
  366. "for non master device\n");
  367. break;
  368. }
  369. memcpy(&priv->mfunc.master.comm_arm_bit_vector,
  370. eqe->event.comm_channel_arm.bit_vec,
  371. sizeof eqe->event.comm_channel_arm.bit_vec);
  372. queue_work(priv->mfunc.master.comm_wq,
  373. &priv->mfunc.master.comm_work);
  374. break;
  375. case MLX4_EVENT_TYPE_FLR_EVENT:
  376. flr_slave = be32_to_cpu(eqe->event.flr_event.slave_id);
  377. if (!mlx4_is_master(dev)) {
  378. mlx4_warn(dev, "Non-master function received"
  379. "FLR event\n");
  380. break;
  381. }
  382. mlx4_dbg(dev, "FLR event for slave: %d\n", flr_slave);
  383. if (flr_slave > dev->num_slaves) {
  384. mlx4_warn(dev,
  385. "Got FLR for unknown function: %d\n",
  386. flr_slave);
  387. update_slave_state = 0;
  388. } else
  389. update_slave_state = 1;
  390. spin_lock(&priv->mfunc.master.slave_state_lock);
  391. if (update_slave_state) {
  392. priv->mfunc.master.slave_state[flr_slave].active = false;
  393. priv->mfunc.master.slave_state[flr_slave].last_cmd = MLX4_COMM_CMD_FLR;
  394. priv->mfunc.master.slave_state[flr_slave].is_slave_going_down = 1;
  395. }
  396. spin_unlock(&priv->mfunc.master.slave_state_lock);
  397. queue_work(priv->mfunc.master.comm_wq,
  398. &priv->mfunc.master.slave_flr_event_work);
  399. break;
  400. case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
  401. case MLX4_EVENT_TYPE_ECC_DETECT:
  402. default:
  403. mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at "
  404. "index %u. owner=%x, nent=0x%x, slave=%x, "
  405. "ownership=%s\n",
  406. eqe->type, eqe->subtype, eq->eqn,
  407. eq->cons_index, eqe->owner, eq->nent,
  408. eqe->slave_id,
  409. !!(eqe->owner & 0x80) ^
  410. !!(eq->cons_index & eq->nent) ? "HW" : "SW");
  411. break;
  412. };
  413. ++eq->cons_index;
  414. eqes_found = 1;
  415. ++set_ci;
  416. /*
  417. * The HCA will think the queue has overflowed if we
  418. * don't tell it we've been processing events. We
  419. * create our EQs with MLX4_NUM_SPARE_EQE extra
  420. * entries, so we must update our consumer index at
  421. * least that often.
  422. */
  423. if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
  424. eq_set_ci(eq, 0);
  425. set_ci = 0;
  426. }
  427. }
  428. eq_set_ci(eq, 1);
  429. return eqes_found;
  430. }
  431. static irqreturn_t mlx4_interrupt(int irq, void *dev_ptr)
  432. {
  433. struct mlx4_dev *dev = dev_ptr;
  434. struct mlx4_priv *priv = mlx4_priv(dev);
  435. int work = 0;
  436. int i;
  437. writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
  438. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  439. work |= mlx4_eq_int(dev, &priv->eq_table.eq[i]);
  440. return IRQ_RETVAL(work);
  441. }
  442. static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
  443. {
  444. struct mlx4_eq *eq = eq_ptr;
  445. struct mlx4_dev *dev = eq->dev;
  446. mlx4_eq_int(dev, eq);
  447. /* MSI-X vectors always belong to us */
  448. return IRQ_HANDLED;
  449. }
  450. int mlx4_MAP_EQ_wrapper(struct mlx4_dev *dev, int slave,
  451. struct mlx4_vhcr *vhcr,
  452. struct mlx4_cmd_mailbox *inbox,
  453. struct mlx4_cmd_mailbox *outbox,
  454. struct mlx4_cmd_info *cmd)
  455. {
  456. struct mlx4_priv *priv = mlx4_priv(dev);
  457. struct mlx4_slave_event_eq_info *event_eq =
  458. priv->mfunc.master.slave_state[slave].event_eq;
  459. u32 in_modifier = vhcr->in_modifier;
  460. u32 eqn = in_modifier & 0x1FF;
  461. u64 in_param = vhcr->in_param;
  462. int err = 0;
  463. int i;
  464. if (slave == dev->caps.function)
  465. err = mlx4_cmd(dev, in_param, (in_modifier & 0x80000000) | eqn,
  466. 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B,
  467. MLX4_CMD_NATIVE);
  468. if (!err)
  469. for (i = 0; i < MLX4_EVENT_TYPES_NUM; ++i)
  470. if (in_param & (1LL << i))
  471. event_eq[i].eqn = in_modifier >> 31 ? -1 : eqn;
  472. return err;
  473. }
  474. static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
  475. int eq_num)
  476. {
  477. return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
  478. 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B,
  479. MLX4_CMD_WRAPPED);
  480. }
  481. static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  482. int eq_num)
  483. {
  484. return mlx4_cmd(dev, mailbox->dma, eq_num, 0,
  485. MLX4_CMD_SW2HW_EQ, MLX4_CMD_TIME_CLASS_A,
  486. MLX4_CMD_WRAPPED);
  487. }
  488. static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  489. int eq_num)
  490. {
  491. return mlx4_cmd_box(dev, 0, mailbox->dma, eq_num,
  492. 0, MLX4_CMD_HW2SW_EQ, MLX4_CMD_TIME_CLASS_A,
  493. MLX4_CMD_WRAPPED);
  494. }
  495. static int mlx4_num_eq_uar(struct mlx4_dev *dev)
  496. {
  497. /*
  498. * Each UAR holds 4 EQ doorbells. To figure out how many UARs
  499. * we need to map, take the difference of highest index and
  500. * the lowest index we'll use and add 1.
  501. */
  502. return (dev->caps.num_comp_vectors + 1 + dev->caps.reserved_eqs +
  503. dev->caps.comp_pool)/4 - dev->caps.reserved_eqs/4 + 1;
  504. }
  505. static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
  506. {
  507. struct mlx4_priv *priv = mlx4_priv(dev);
  508. int index;
  509. index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
  510. if (!priv->eq_table.uar_map[index]) {
  511. priv->eq_table.uar_map[index] =
  512. ioremap(pci_resource_start(dev->pdev, 2) +
  513. ((eq->eqn / 4) << PAGE_SHIFT),
  514. PAGE_SIZE);
  515. if (!priv->eq_table.uar_map[index]) {
  516. mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
  517. eq->eqn);
  518. return NULL;
  519. }
  520. }
  521. return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
  522. }
  523. static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
  524. u8 intr, struct mlx4_eq *eq)
  525. {
  526. struct mlx4_priv *priv = mlx4_priv(dev);
  527. struct mlx4_cmd_mailbox *mailbox;
  528. struct mlx4_eq_context *eq_context;
  529. int npages;
  530. u64 *dma_list = NULL;
  531. dma_addr_t t;
  532. u64 mtt_addr;
  533. int err = -ENOMEM;
  534. int i;
  535. eq->dev = dev;
  536. eq->nent = roundup_pow_of_two(max(nent, 2));
  537. npages = PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE;
  538. eq->page_list = kmalloc(npages * sizeof *eq->page_list,
  539. GFP_KERNEL);
  540. if (!eq->page_list)
  541. goto err_out;
  542. for (i = 0; i < npages; ++i)
  543. eq->page_list[i].buf = NULL;
  544. dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
  545. if (!dma_list)
  546. goto err_out_free;
  547. mailbox = mlx4_alloc_cmd_mailbox(dev);
  548. if (IS_ERR(mailbox))
  549. goto err_out_free;
  550. eq_context = mailbox->buf;
  551. for (i = 0; i < npages; ++i) {
  552. eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
  553. PAGE_SIZE, &t, GFP_KERNEL);
  554. if (!eq->page_list[i].buf)
  555. goto err_out_free_pages;
  556. dma_list[i] = t;
  557. eq->page_list[i].map = t;
  558. memset(eq->page_list[i].buf, 0, PAGE_SIZE);
  559. }
  560. eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
  561. if (eq->eqn == -1)
  562. goto err_out_free_pages;
  563. eq->doorbell = mlx4_get_eq_uar(dev, eq);
  564. if (!eq->doorbell) {
  565. err = -ENOMEM;
  566. goto err_out_free_eq;
  567. }
  568. err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
  569. if (err)
  570. goto err_out_free_eq;
  571. err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
  572. if (err)
  573. goto err_out_free_mtt;
  574. memset(eq_context, 0, sizeof *eq_context);
  575. eq_context->flags = cpu_to_be32(MLX4_EQ_STATUS_OK |
  576. MLX4_EQ_STATE_ARMED);
  577. eq_context->log_eq_size = ilog2(eq->nent);
  578. eq_context->intr = intr;
  579. eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
  580. mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
  581. eq_context->mtt_base_addr_h = mtt_addr >> 32;
  582. eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  583. err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
  584. if (err) {
  585. mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
  586. goto err_out_free_mtt;
  587. }
  588. kfree(dma_list);
  589. mlx4_free_cmd_mailbox(dev, mailbox);
  590. eq->cons_index = 0;
  591. return err;
  592. err_out_free_mtt:
  593. mlx4_mtt_cleanup(dev, &eq->mtt);
  594. err_out_free_eq:
  595. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  596. err_out_free_pages:
  597. for (i = 0; i < npages; ++i)
  598. if (eq->page_list[i].buf)
  599. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  600. eq->page_list[i].buf,
  601. eq->page_list[i].map);
  602. mlx4_free_cmd_mailbox(dev, mailbox);
  603. err_out_free:
  604. kfree(eq->page_list);
  605. kfree(dma_list);
  606. err_out:
  607. return err;
  608. }
  609. static void mlx4_free_eq(struct mlx4_dev *dev,
  610. struct mlx4_eq *eq)
  611. {
  612. struct mlx4_priv *priv = mlx4_priv(dev);
  613. struct mlx4_cmd_mailbox *mailbox;
  614. int err;
  615. int npages = PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE;
  616. int i;
  617. mailbox = mlx4_alloc_cmd_mailbox(dev);
  618. if (IS_ERR(mailbox))
  619. return;
  620. err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
  621. if (err)
  622. mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
  623. if (0) {
  624. mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
  625. for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
  626. if (i % 4 == 0)
  627. pr_cont("[%02x] ", i * 4);
  628. pr_cont(" %08x", be32_to_cpup(mailbox->buf + i * 4));
  629. if ((i + 1) % 4 == 0)
  630. pr_cont("\n");
  631. }
  632. }
  633. mlx4_mtt_cleanup(dev, &eq->mtt);
  634. for (i = 0; i < npages; ++i)
  635. dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
  636. eq->page_list[i].buf,
  637. eq->page_list[i].map);
  638. kfree(eq->page_list);
  639. mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
  640. mlx4_free_cmd_mailbox(dev, mailbox);
  641. }
  642. static void mlx4_free_irqs(struct mlx4_dev *dev)
  643. {
  644. struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
  645. struct mlx4_priv *priv = mlx4_priv(dev);
  646. int i, vec;
  647. if (eq_table->have_irq)
  648. free_irq(dev->pdev->irq, dev);
  649. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  650. if (eq_table->eq[i].have_irq) {
  651. free_irq(eq_table->eq[i].irq, eq_table->eq + i);
  652. eq_table->eq[i].have_irq = 0;
  653. }
  654. for (i = 0; i < dev->caps.comp_pool; i++) {
  655. /*
  656. * Freeing the assigned irq's
  657. * all bits should be 0, but we need to validate
  658. */
  659. if (priv->msix_ctl.pool_bm & 1ULL << i) {
  660. /* NO need protecting*/
  661. vec = dev->caps.num_comp_vectors + 1 + i;
  662. free_irq(priv->eq_table.eq[vec].irq,
  663. &priv->eq_table.eq[vec]);
  664. }
  665. }
  666. kfree(eq_table->irq_names);
  667. }
  668. static int mlx4_map_clr_int(struct mlx4_dev *dev)
  669. {
  670. struct mlx4_priv *priv = mlx4_priv(dev);
  671. priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
  672. priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
  673. if (!priv->clr_base) {
  674. mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
  675. return -ENOMEM;
  676. }
  677. return 0;
  678. }
  679. static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
  680. {
  681. struct mlx4_priv *priv = mlx4_priv(dev);
  682. iounmap(priv->clr_base);
  683. }
  684. int mlx4_alloc_eq_table(struct mlx4_dev *dev)
  685. {
  686. struct mlx4_priv *priv = mlx4_priv(dev);
  687. priv->eq_table.eq = kcalloc(dev->caps.num_eqs - dev->caps.reserved_eqs,
  688. sizeof *priv->eq_table.eq, GFP_KERNEL);
  689. if (!priv->eq_table.eq)
  690. return -ENOMEM;
  691. return 0;
  692. }
  693. void mlx4_free_eq_table(struct mlx4_dev *dev)
  694. {
  695. kfree(mlx4_priv(dev)->eq_table.eq);
  696. }
  697. int mlx4_init_eq_table(struct mlx4_dev *dev)
  698. {
  699. struct mlx4_priv *priv = mlx4_priv(dev);
  700. int err;
  701. int i;
  702. priv->eq_table.uar_map = kcalloc(mlx4_num_eq_uar(dev),
  703. sizeof *priv->eq_table.uar_map,
  704. GFP_KERNEL);
  705. if (!priv->eq_table.uar_map) {
  706. err = -ENOMEM;
  707. goto err_out_free;
  708. }
  709. err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
  710. dev->caps.num_eqs - 1, dev->caps.reserved_eqs, 0);
  711. if (err)
  712. goto err_out_free;
  713. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  714. priv->eq_table.uar_map[i] = NULL;
  715. if (!mlx4_is_slave(dev)) {
  716. err = mlx4_map_clr_int(dev);
  717. if (err)
  718. goto err_out_bitmap;
  719. priv->eq_table.clr_mask =
  720. swab32(1 << (priv->eq_table.inta_pin & 31));
  721. priv->eq_table.clr_int = priv->clr_base +
  722. (priv->eq_table.inta_pin < 32 ? 4 : 0);
  723. }
  724. priv->eq_table.irq_names =
  725. kmalloc(MLX4_IRQNAME_SIZE * (dev->caps.num_comp_vectors + 1 +
  726. dev->caps.comp_pool),
  727. GFP_KERNEL);
  728. if (!priv->eq_table.irq_names) {
  729. err = -ENOMEM;
  730. goto err_out_bitmap;
  731. }
  732. for (i = 0; i < dev->caps.num_comp_vectors; ++i) {
  733. err = mlx4_create_eq(dev, dev->caps.num_cqs -
  734. dev->caps.reserved_cqs +
  735. MLX4_NUM_SPARE_EQE,
  736. (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
  737. &priv->eq_table.eq[i]);
  738. if (err) {
  739. --i;
  740. goto err_out_unmap;
  741. }
  742. }
  743. err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
  744. (dev->flags & MLX4_FLAG_MSI_X) ? dev->caps.num_comp_vectors : 0,
  745. &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  746. if (err)
  747. goto err_out_comp;
  748. /*if additional completion vectors poolsize is 0 this loop will not run*/
  749. for (i = dev->caps.num_comp_vectors + 1;
  750. i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i) {
  751. err = mlx4_create_eq(dev, dev->caps.num_cqs -
  752. dev->caps.reserved_cqs +
  753. MLX4_NUM_SPARE_EQE,
  754. (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
  755. &priv->eq_table.eq[i]);
  756. if (err) {
  757. --i;
  758. goto err_out_unmap;
  759. }
  760. }
  761. if (dev->flags & MLX4_FLAG_MSI_X) {
  762. const char *eq_name;
  763. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i) {
  764. if (i < dev->caps.num_comp_vectors) {
  765. snprintf(priv->eq_table.irq_names +
  766. i * MLX4_IRQNAME_SIZE,
  767. MLX4_IRQNAME_SIZE,
  768. "mlx4-comp-%d@pci:%s", i,
  769. pci_name(dev->pdev));
  770. } else {
  771. snprintf(priv->eq_table.irq_names +
  772. i * MLX4_IRQNAME_SIZE,
  773. MLX4_IRQNAME_SIZE,
  774. "mlx4-async@pci:%s",
  775. pci_name(dev->pdev));
  776. }
  777. eq_name = priv->eq_table.irq_names +
  778. i * MLX4_IRQNAME_SIZE;
  779. err = request_irq(priv->eq_table.eq[i].irq,
  780. mlx4_msi_x_interrupt, 0, eq_name,
  781. priv->eq_table.eq + i);
  782. if (err)
  783. goto err_out_async;
  784. priv->eq_table.eq[i].have_irq = 1;
  785. }
  786. } else {
  787. snprintf(priv->eq_table.irq_names,
  788. MLX4_IRQNAME_SIZE,
  789. DRV_NAME "@pci:%s",
  790. pci_name(dev->pdev));
  791. err = request_irq(dev->pdev->irq, mlx4_interrupt,
  792. IRQF_SHARED, priv->eq_table.irq_names, dev);
  793. if (err)
  794. goto err_out_async;
  795. priv->eq_table.have_irq = 1;
  796. }
  797. err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  798. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  799. if (err)
  800. mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
  801. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn, err);
  802. for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
  803. eq_set_ci(&priv->eq_table.eq[i], 1);
  804. return 0;
  805. err_out_async:
  806. mlx4_free_eq(dev, &priv->eq_table.eq[dev->caps.num_comp_vectors]);
  807. err_out_comp:
  808. i = dev->caps.num_comp_vectors - 1;
  809. err_out_unmap:
  810. while (i >= 0) {
  811. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  812. --i;
  813. }
  814. if (!mlx4_is_slave(dev))
  815. mlx4_unmap_clr_int(dev);
  816. mlx4_free_irqs(dev);
  817. err_out_bitmap:
  818. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  819. err_out_free:
  820. kfree(priv->eq_table.uar_map);
  821. return err;
  822. }
  823. void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
  824. {
  825. struct mlx4_priv *priv = mlx4_priv(dev);
  826. int i;
  827. mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
  828. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  829. mlx4_free_irqs(dev);
  830. for (i = 0; i < dev->caps.num_comp_vectors + dev->caps.comp_pool + 1; ++i)
  831. mlx4_free_eq(dev, &priv->eq_table.eq[i]);
  832. if (!mlx4_is_slave(dev))
  833. mlx4_unmap_clr_int(dev);
  834. for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
  835. if (priv->eq_table.uar_map[i])
  836. iounmap(priv->eq_table.uar_map[i]);
  837. mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
  838. kfree(priv->eq_table.uar_map);
  839. }
  840. /* A test that verifies that we can accept interrupts on all
  841. * the irq vectors of the device.
  842. * Interrupts are checked using the NOP command.
  843. */
  844. int mlx4_test_interrupts(struct mlx4_dev *dev)
  845. {
  846. struct mlx4_priv *priv = mlx4_priv(dev);
  847. int i;
  848. int err;
  849. err = mlx4_NOP(dev);
  850. /* When not in MSI_X, there is only one irq to check */
  851. if (!(dev->flags & MLX4_FLAG_MSI_X) || mlx4_is_slave(dev))
  852. return err;
  853. /* A loop over all completion vectors, for each vector we will check
  854. * whether it works by mapping command completions to that vector
  855. * and performing a NOP command
  856. */
  857. for(i = 0; !err && (i < dev->caps.num_comp_vectors); ++i) {
  858. /* Temporary use polling for command completions */
  859. mlx4_cmd_use_polling(dev);
  860. /* Map the new eq to handle all asyncronous events */
  861. err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  862. priv->eq_table.eq[i].eqn);
  863. if (err) {
  864. mlx4_warn(dev, "Failed mapping eq for interrupt test\n");
  865. mlx4_cmd_use_events(dev);
  866. break;
  867. }
  868. /* Go back to using events */
  869. mlx4_cmd_use_events(dev);
  870. err = mlx4_NOP(dev);
  871. }
  872. /* Return to default */
  873. mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
  874. priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
  875. return err;
  876. }
  877. EXPORT_SYMBOL(mlx4_test_interrupts);
  878. int mlx4_assign_eq(struct mlx4_dev *dev, char* name, int * vector)
  879. {
  880. struct mlx4_priv *priv = mlx4_priv(dev);
  881. int vec = 0, err = 0, i;
  882. mutex_lock(&priv->msix_ctl.pool_lock);
  883. for (i = 0; !vec && i < dev->caps.comp_pool; i++) {
  884. if (~priv->msix_ctl.pool_bm & 1ULL << i) {
  885. priv->msix_ctl.pool_bm |= 1ULL << i;
  886. vec = dev->caps.num_comp_vectors + 1 + i;
  887. snprintf(priv->eq_table.irq_names +
  888. vec * MLX4_IRQNAME_SIZE,
  889. MLX4_IRQNAME_SIZE, "%s", name);
  890. err = request_irq(priv->eq_table.eq[vec].irq,
  891. mlx4_msi_x_interrupt, 0,
  892. &priv->eq_table.irq_names[vec<<5],
  893. priv->eq_table.eq + vec);
  894. if (err) {
  895. /*zero out bit by fliping it*/
  896. priv->msix_ctl.pool_bm ^= 1 << i;
  897. vec = 0;
  898. continue;
  899. /*we dont want to break here*/
  900. }
  901. eq_set_ci(&priv->eq_table.eq[vec], 1);
  902. }
  903. }
  904. mutex_unlock(&priv->msix_ctl.pool_lock);
  905. if (vec) {
  906. *vector = vec;
  907. } else {
  908. *vector = 0;
  909. err = (i == dev->caps.comp_pool) ? -ENOSPC : err;
  910. }
  911. return err;
  912. }
  913. EXPORT_SYMBOL(mlx4_assign_eq);
  914. void mlx4_release_eq(struct mlx4_dev *dev, int vec)
  915. {
  916. struct mlx4_priv *priv = mlx4_priv(dev);
  917. /*bm index*/
  918. int i = vec - dev->caps.num_comp_vectors - 1;
  919. if (likely(i >= 0)) {
  920. /*sanity check , making sure were not trying to free irq's
  921. Belonging to a legacy EQ*/
  922. mutex_lock(&priv->msix_ctl.pool_lock);
  923. if (priv->msix_ctl.pool_bm & 1ULL << i) {
  924. free_irq(priv->eq_table.eq[vec].irq,
  925. &priv->eq_table.eq[vec]);
  926. priv->msix_ctl.pool_bm &= ~(1ULL << i);
  927. }
  928. mutex_unlock(&priv->msix_ctl.pool_lock);
  929. }
  930. }
  931. EXPORT_SYMBOL(mlx4_release_eq);