ehca_irq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * Functions for EQs, NEQs and interrupts
  5. *
  6. * Authors: Heiko J Schick <schickhj@de.ibm.com>
  7. * Khadija Souissi <souissi@de.ibm.com>
  8. * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  9. * Joachim Fenkes <fenkes@de.ibm.com>
  10. *
  11. * Copyright (c) 2005 IBM Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  16. * BSD.
  17. *
  18. * OpenIB BSD License
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials
  29. * provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  39. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #include <linux/slab.h>
  44. #include <linux/smpboot.h>
  45. #include "ehca_classes.h"
  46. #include "ehca_irq.h"
  47. #include "ehca_iverbs.h"
  48. #include "ehca_tools.h"
  49. #include "hcp_if.h"
  50. #include "hipz_fns.h"
  51. #include "ipz_pt_fn.h"
  52. #define EQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
  53. #define EQE_CQ_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
  54. #define EQE_EE_IDENTIFIER EHCA_BMASK_IBM( 2, 7)
  55. #define EQE_CQ_NUMBER EHCA_BMASK_IBM( 8, 31)
  56. #define EQE_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
  57. #define EQE_QP_TOKEN EHCA_BMASK_IBM(32, 63)
  58. #define EQE_CQ_TOKEN EHCA_BMASK_IBM(32, 63)
  59. #define NEQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
  60. #define NEQE_EVENT_CODE EHCA_BMASK_IBM( 2, 7)
  61. #define NEQE_PORT_NUMBER EHCA_BMASK_IBM( 8, 15)
  62. #define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16, 16)
  63. #define NEQE_DISRUPTIVE EHCA_BMASK_IBM(16, 16)
  64. #define NEQE_SPECIFIC_EVENT EHCA_BMASK_IBM(16, 23)
  65. #define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52, 63)
  66. #define ERROR_DATA_TYPE EHCA_BMASK_IBM( 0, 7)
  67. static void queue_comp_task(struct ehca_cq *__cq);
  68. static struct ehca_comp_pool *pool;
  69. static inline void comp_event_callback(struct ehca_cq *cq)
  70. {
  71. if (!cq->ib_cq.comp_handler)
  72. return;
  73. spin_lock(&cq->cb_lock);
  74. cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context);
  75. spin_unlock(&cq->cb_lock);
  76. return;
  77. }
  78. static void print_error_data(struct ehca_shca *shca, void *data,
  79. u64 *rblock, int length)
  80. {
  81. u64 type = EHCA_BMASK_GET(ERROR_DATA_TYPE, rblock[2]);
  82. u64 resource = rblock[1];
  83. switch (type) {
  84. case 0x1: /* Queue Pair */
  85. {
  86. struct ehca_qp *qp = (struct ehca_qp *)data;
  87. /* only print error data if AER is set */
  88. if (rblock[6] == 0)
  89. return;
  90. ehca_err(&shca->ib_device,
  91. "QP 0x%x (resource=%llx) has errors.",
  92. qp->ib_qp.qp_num, resource);
  93. break;
  94. }
  95. case 0x4: /* Completion Queue */
  96. {
  97. struct ehca_cq *cq = (struct ehca_cq *)data;
  98. ehca_err(&shca->ib_device,
  99. "CQ 0x%x (resource=%llx) has errors.",
  100. cq->cq_number, resource);
  101. break;
  102. }
  103. default:
  104. ehca_err(&shca->ib_device,
  105. "Unknown error type: %llx on %s.",
  106. type, shca->ib_device.name);
  107. break;
  108. }
  109. ehca_err(&shca->ib_device, "Error data is available: %llx.", resource);
  110. ehca_err(&shca->ib_device, "EHCA ----- error data begin "
  111. "---------------------------------------------------");
  112. ehca_dmp(rblock, length, "resource=%llx", resource);
  113. ehca_err(&shca->ib_device, "EHCA ----- error data end "
  114. "----------------------------------------------------");
  115. return;
  116. }
  117. int ehca_error_data(struct ehca_shca *shca, void *data,
  118. u64 resource)
  119. {
  120. unsigned long ret;
  121. u64 *rblock;
  122. unsigned long block_count;
  123. rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
  124. if (!rblock) {
  125. ehca_err(&shca->ib_device, "Cannot allocate rblock memory.");
  126. ret = -ENOMEM;
  127. goto error_data1;
  128. }
  129. /* rblock must be 4K aligned and should be 4K large */
  130. ret = hipz_h_error_data(shca->ipz_hca_handle,
  131. resource,
  132. rblock,
  133. &block_count);
  134. if (ret == H_R_STATE)
  135. ehca_err(&shca->ib_device,
  136. "No error data is available: %llx.", resource);
  137. else if (ret == H_SUCCESS) {
  138. int length;
  139. length = EHCA_BMASK_GET(ERROR_DATA_LENGTH, rblock[0]);
  140. if (length > EHCA_PAGESIZE)
  141. length = EHCA_PAGESIZE;
  142. print_error_data(shca, data, rblock, length);
  143. } else
  144. ehca_err(&shca->ib_device,
  145. "Error data could not be fetched: %llx", resource);
  146. ehca_free_fw_ctrlblock(rblock);
  147. error_data1:
  148. return ret;
  149. }
  150. static void dispatch_qp_event(struct ehca_shca *shca, struct ehca_qp *qp,
  151. enum ib_event_type event_type)
  152. {
  153. struct ib_event event;
  154. /* PATH_MIG without the QP ever having been armed is false alarm */
  155. if (event_type == IB_EVENT_PATH_MIG && !qp->mig_armed)
  156. return;
  157. event.device = &shca->ib_device;
  158. event.event = event_type;
  159. if (qp->ext_type == EQPT_SRQ) {
  160. if (!qp->ib_srq.event_handler)
  161. return;
  162. event.element.srq = &qp->ib_srq;
  163. qp->ib_srq.event_handler(&event, qp->ib_srq.srq_context);
  164. } else {
  165. if (!qp->ib_qp.event_handler)
  166. return;
  167. event.element.qp = &qp->ib_qp;
  168. qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
  169. }
  170. }
  171. static void qp_event_callback(struct ehca_shca *shca, u64 eqe,
  172. enum ib_event_type event_type, int fatal)
  173. {
  174. struct ehca_qp *qp;
  175. u32 token = EHCA_BMASK_GET(EQE_QP_TOKEN, eqe);
  176. read_lock(&ehca_qp_idr_lock);
  177. qp = idr_find(&ehca_qp_idr, token);
  178. if (qp)
  179. atomic_inc(&qp->nr_events);
  180. read_unlock(&ehca_qp_idr_lock);
  181. if (!qp)
  182. return;
  183. if (fatal)
  184. ehca_error_data(shca, qp, qp->ipz_qp_handle.handle);
  185. dispatch_qp_event(shca, qp, fatal && qp->ext_type == EQPT_SRQ ?
  186. IB_EVENT_SRQ_ERR : event_type);
  187. /*
  188. * eHCA only processes one WQE at a time for SRQ base QPs,
  189. * so the last WQE has been processed as soon as the QP enters
  190. * error state.
  191. */
  192. if (fatal && qp->ext_type == EQPT_SRQBASE)
  193. dispatch_qp_event(shca, qp, IB_EVENT_QP_LAST_WQE_REACHED);
  194. if (atomic_dec_and_test(&qp->nr_events))
  195. wake_up(&qp->wait_completion);
  196. return;
  197. }
  198. static void cq_event_callback(struct ehca_shca *shca,
  199. u64 eqe)
  200. {
  201. struct ehca_cq *cq;
  202. u32 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe);
  203. read_lock(&ehca_cq_idr_lock);
  204. cq = idr_find(&ehca_cq_idr, token);
  205. if (cq)
  206. atomic_inc(&cq->nr_events);
  207. read_unlock(&ehca_cq_idr_lock);
  208. if (!cq)
  209. return;
  210. ehca_error_data(shca, cq, cq->ipz_cq_handle.handle);
  211. if (atomic_dec_and_test(&cq->nr_events))
  212. wake_up(&cq->wait_completion);
  213. return;
  214. }
  215. static void parse_identifier(struct ehca_shca *shca, u64 eqe)
  216. {
  217. u8 identifier = EHCA_BMASK_GET(EQE_EE_IDENTIFIER, eqe);
  218. switch (identifier) {
  219. case 0x02: /* path migrated */
  220. qp_event_callback(shca, eqe, IB_EVENT_PATH_MIG, 0);
  221. break;
  222. case 0x03: /* communication established */
  223. qp_event_callback(shca, eqe, IB_EVENT_COMM_EST, 0);
  224. break;
  225. case 0x04: /* send queue drained */
  226. qp_event_callback(shca, eqe, IB_EVENT_SQ_DRAINED, 0);
  227. break;
  228. case 0x05: /* QP error */
  229. case 0x06: /* QP error */
  230. qp_event_callback(shca, eqe, IB_EVENT_QP_FATAL, 1);
  231. break;
  232. case 0x07: /* CQ error */
  233. case 0x08: /* CQ error */
  234. cq_event_callback(shca, eqe);
  235. break;
  236. case 0x09: /* MRMWPTE error */
  237. ehca_err(&shca->ib_device, "MRMWPTE error.");
  238. break;
  239. case 0x0A: /* port event */
  240. ehca_err(&shca->ib_device, "Port event.");
  241. break;
  242. case 0x0B: /* MR access error */
  243. ehca_err(&shca->ib_device, "MR access error.");
  244. break;
  245. case 0x0C: /* EQ error */
  246. ehca_err(&shca->ib_device, "EQ error.");
  247. break;
  248. case 0x0D: /* P/Q_Key mismatch */
  249. ehca_err(&shca->ib_device, "P/Q_Key mismatch.");
  250. break;
  251. case 0x10: /* sampling complete */
  252. ehca_err(&shca->ib_device, "Sampling complete.");
  253. break;
  254. case 0x11: /* unaffiliated access error */
  255. ehca_err(&shca->ib_device, "Unaffiliated access error.");
  256. break;
  257. case 0x12: /* path migrating */
  258. ehca_err(&shca->ib_device, "Path migrating.");
  259. break;
  260. case 0x13: /* interface trace stopped */
  261. ehca_err(&shca->ib_device, "Interface trace stopped.");
  262. break;
  263. case 0x14: /* first error capture info available */
  264. ehca_info(&shca->ib_device, "First error capture available");
  265. break;
  266. case 0x15: /* SRQ limit reached */
  267. qp_event_callback(shca, eqe, IB_EVENT_SRQ_LIMIT_REACHED, 0);
  268. break;
  269. default:
  270. ehca_err(&shca->ib_device, "Unknown identifier: %x on %s.",
  271. identifier, shca->ib_device.name);
  272. break;
  273. }
  274. return;
  275. }
  276. static void dispatch_port_event(struct ehca_shca *shca, int port_num,
  277. enum ib_event_type type, const char *msg)
  278. {
  279. struct ib_event event;
  280. ehca_info(&shca->ib_device, "port %d %s.", port_num, msg);
  281. event.device = &shca->ib_device;
  282. event.event = type;
  283. event.element.port_num = port_num;
  284. ib_dispatch_event(&event);
  285. }
  286. static void notify_port_conf_change(struct ehca_shca *shca, int port_num)
  287. {
  288. struct ehca_sma_attr new_attr;
  289. struct ehca_sma_attr *old_attr = &shca->sport[port_num - 1].saved_attr;
  290. ehca_query_sma_attr(shca, port_num, &new_attr);
  291. if (new_attr.sm_sl != old_attr->sm_sl ||
  292. new_attr.sm_lid != old_attr->sm_lid)
  293. dispatch_port_event(shca, port_num, IB_EVENT_SM_CHANGE,
  294. "SM changed");
  295. if (new_attr.lid != old_attr->lid ||
  296. new_attr.lmc != old_attr->lmc)
  297. dispatch_port_event(shca, port_num, IB_EVENT_LID_CHANGE,
  298. "LID changed");
  299. if (new_attr.pkey_tbl_len != old_attr->pkey_tbl_len ||
  300. memcmp(new_attr.pkeys, old_attr->pkeys,
  301. sizeof(u16) * new_attr.pkey_tbl_len))
  302. dispatch_port_event(shca, port_num, IB_EVENT_PKEY_CHANGE,
  303. "P_Key changed");
  304. *old_attr = new_attr;
  305. }
  306. /* replay modify_qp for sqps -- return 0 if all is well, 1 if AQP1 destroyed */
  307. static int replay_modify_qp(struct ehca_sport *sport)
  308. {
  309. int aqp1_destroyed;
  310. unsigned long flags;
  311. spin_lock_irqsave(&sport->mod_sqp_lock, flags);
  312. aqp1_destroyed = !sport->ibqp_sqp[IB_QPT_GSI];
  313. if (sport->ibqp_sqp[IB_QPT_SMI])
  314. ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_SMI]);
  315. if (!aqp1_destroyed)
  316. ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_GSI]);
  317. spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
  318. return aqp1_destroyed;
  319. }
  320. static void parse_ec(struct ehca_shca *shca, u64 eqe)
  321. {
  322. u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
  323. u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
  324. u8 spec_event;
  325. struct ehca_sport *sport = &shca->sport[port - 1];
  326. switch (ec) {
  327. case 0x30: /* port availability change */
  328. if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
  329. /* only replay modify_qp calls in autodetect mode;
  330. * if AQP1 was destroyed, the port is already down
  331. * again and we can drop the event.
  332. */
  333. if (ehca_nr_ports < 0)
  334. if (replay_modify_qp(sport))
  335. break;
  336. sport->port_state = IB_PORT_ACTIVE;
  337. dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
  338. "is active");
  339. ehca_query_sma_attr(shca, port, &sport->saved_attr);
  340. } else {
  341. sport->port_state = IB_PORT_DOWN;
  342. dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
  343. "is inactive");
  344. }
  345. break;
  346. case 0x31:
  347. /* port configuration change
  348. * disruptive change is caused by
  349. * LID, PKEY or SM change
  350. */
  351. if (EHCA_BMASK_GET(NEQE_DISRUPTIVE, eqe)) {
  352. ehca_warn(&shca->ib_device, "disruptive port "
  353. "%d configuration change", port);
  354. sport->port_state = IB_PORT_DOWN;
  355. dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
  356. "is inactive");
  357. sport->port_state = IB_PORT_ACTIVE;
  358. dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
  359. "is active");
  360. ehca_query_sma_attr(shca, port,
  361. &sport->saved_attr);
  362. } else
  363. notify_port_conf_change(shca, port);
  364. break;
  365. case 0x32: /* adapter malfunction */
  366. ehca_err(&shca->ib_device, "Adapter malfunction.");
  367. break;
  368. case 0x33: /* trace stopped */
  369. ehca_err(&shca->ib_device, "Traced stopped.");
  370. break;
  371. case 0x34: /* util async event */
  372. spec_event = EHCA_BMASK_GET(NEQE_SPECIFIC_EVENT, eqe);
  373. if (spec_event == 0x80) /* client reregister required */
  374. dispatch_port_event(shca, port,
  375. IB_EVENT_CLIENT_REREGISTER,
  376. "client reregister req.");
  377. else
  378. ehca_warn(&shca->ib_device, "Unknown util async "
  379. "event %x on port %x", spec_event, port);
  380. break;
  381. default:
  382. ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
  383. ec, shca->ib_device.name);
  384. break;
  385. }
  386. return;
  387. }
  388. static inline void reset_eq_pending(struct ehca_cq *cq)
  389. {
  390. u64 CQx_EP;
  391. struct h_galpa gal = cq->galpas.kernel;
  392. hipz_galpa_store_cq(gal, cqx_ep, 0x0);
  393. CQx_EP = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_ep));
  394. return;
  395. }
  396. irqreturn_t ehca_interrupt_neq(int irq, void *dev_id)
  397. {
  398. struct ehca_shca *shca = (struct ehca_shca*)dev_id;
  399. tasklet_hi_schedule(&shca->neq.interrupt_task);
  400. return IRQ_HANDLED;
  401. }
  402. void ehca_tasklet_neq(unsigned long data)
  403. {
  404. struct ehca_shca *shca = (struct ehca_shca*)data;
  405. struct ehca_eqe *eqe;
  406. u64 ret;
  407. eqe = ehca_poll_eq(shca, &shca->neq);
  408. while (eqe) {
  409. if (!EHCA_BMASK_GET(NEQE_COMPLETION_EVENT, eqe->entry))
  410. parse_ec(shca, eqe->entry);
  411. eqe = ehca_poll_eq(shca, &shca->neq);
  412. }
  413. ret = hipz_h_reset_event(shca->ipz_hca_handle,
  414. shca->neq.ipz_eq_handle, 0xFFFFFFFFFFFFFFFFL);
  415. if (ret != H_SUCCESS)
  416. ehca_err(&shca->ib_device, "Can't clear notification events.");
  417. return;
  418. }
  419. irqreturn_t ehca_interrupt_eq(int irq, void *dev_id)
  420. {
  421. struct ehca_shca *shca = (struct ehca_shca*)dev_id;
  422. tasklet_hi_schedule(&shca->eq.interrupt_task);
  423. return IRQ_HANDLED;
  424. }
  425. static inline void process_eqe(struct ehca_shca *shca, struct ehca_eqe *eqe)
  426. {
  427. u64 eqe_value;
  428. u32 token;
  429. struct ehca_cq *cq;
  430. eqe_value = eqe->entry;
  431. ehca_dbg(&shca->ib_device, "eqe_value=%llx", eqe_value);
  432. if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
  433. ehca_dbg(&shca->ib_device, "Got completion event");
  434. token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
  435. read_lock(&ehca_cq_idr_lock);
  436. cq = idr_find(&ehca_cq_idr, token);
  437. if (cq)
  438. atomic_inc(&cq->nr_events);
  439. read_unlock(&ehca_cq_idr_lock);
  440. if (cq == NULL) {
  441. ehca_err(&shca->ib_device,
  442. "Invalid eqe for non-existing cq token=%x",
  443. token);
  444. return;
  445. }
  446. reset_eq_pending(cq);
  447. if (ehca_scaling_code)
  448. queue_comp_task(cq);
  449. else {
  450. comp_event_callback(cq);
  451. if (atomic_dec_and_test(&cq->nr_events))
  452. wake_up(&cq->wait_completion);
  453. }
  454. } else {
  455. ehca_dbg(&shca->ib_device, "Got non completion event");
  456. parse_identifier(shca, eqe_value);
  457. }
  458. }
  459. void ehca_process_eq(struct ehca_shca *shca, int is_irq)
  460. {
  461. struct ehca_eq *eq = &shca->eq;
  462. struct ehca_eqe_cache_entry *eqe_cache = eq->eqe_cache;
  463. u64 eqe_value, ret;
  464. int eqe_cnt, i;
  465. int eq_empty = 0;
  466. spin_lock(&eq->irq_spinlock);
  467. if (is_irq) {
  468. const int max_query_cnt = 100;
  469. int query_cnt = 0;
  470. int int_state = 1;
  471. do {
  472. int_state = hipz_h_query_int_state(
  473. shca->ipz_hca_handle, eq->ist);
  474. query_cnt++;
  475. iosync();
  476. } while (int_state && query_cnt < max_query_cnt);
  477. if (unlikely((query_cnt == max_query_cnt)))
  478. ehca_dbg(&shca->ib_device, "int_state=%x query_cnt=%x",
  479. int_state, query_cnt);
  480. }
  481. /* read out all eqes */
  482. eqe_cnt = 0;
  483. do {
  484. u32 token;
  485. eqe_cache[eqe_cnt].eqe = ehca_poll_eq(shca, eq);
  486. if (!eqe_cache[eqe_cnt].eqe)
  487. break;
  488. eqe_value = eqe_cache[eqe_cnt].eqe->entry;
  489. if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
  490. token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
  491. read_lock(&ehca_cq_idr_lock);
  492. eqe_cache[eqe_cnt].cq = idr_find(&ehca_cq_idr, token);
  493. if (eqe_cache[eqe_cnt].cq)
  494. atomic_inc(&eqe_cache[eqe_cnt].cq->nr_events);
  495. read_unlock(&ehca_cq_idr_lock);
  496. if (!eqe_cache[eqe_cnt].cq) {
  497. ehca_err(&shca->ib_device,
  498. "Invalid eqe for non-existing cq "
  499. "token=%x", token);
  500. continue;
  501. }
  502. } else
  503. eqe_cache[eqe_cnt].cq = NULL;
  504. eqe_cnt++;
  505. } while (eqe_cnt < EHCA_EQE_CACHE_SIZE);
  506. if (!eqe_cnt) {
  507. if (is_irq)
  508. ehca_dbg(&shca->ib_device,
  509. "No eqe found for irq event");
  510. goto unlock_irq_spinlock;
  511. } else if (!is_irq) {
  512. ret = hipz_h_eoi(eq->ist);
  513. if (ret != H_SUCCESS)
  514. ehca_err(&shca->ib_device,
  515. "bad return code EOI -rc = %lld\n", ret);
  516. ehca_dbg(&shca->ib_device, "deadman found %x eqe", eqe_cnt);
  517. }
  518. if (unlikely(eqe_cnt == EHCA_EQE_CACHE_SIZE))
  519. ehca_dbg(&shca->ib_device, "too many eqes for one irq event");
  520. /* enable irq for new packets */
  521. for (i = 0; i < eqe_cnt; i++) {
  522. if (eq->eqe_cache[i].cq)
  523. reset_eq_pending(eq->eqe_cache[i].cq);
  524. }
  525. /* check eq */
  526. spin_lock(&eq->spinlock);
  527. eq_empty = (!ipz_eqit_eq_peek_valid(&shca->eq.ipz_queue));
  528. spin_unlock(&eq->spinlock);
  529. /* call completion handler for cached eqes */
  530. for (i = 0; i < eqe_cnt; i++)
  531. if (eq->eqe_cache[i].cq) {
  532. if (ehca_scaling_code)
  533. queue_comp_task(eq->eqe_cache[i].cq);
  534. else {
  535. struct ehca_cq *cq = eq->eqe_cache[i].cq;
  536. comp_event_callback(cq);
  537. if (atomic_dec_and_test(&cq->nr_events))
  538. wake_up(&cq->wait_completion);
  539. }
  540. } else {
  541. ehca_dbg(&shca->ib_device, "Got non completion event");
  542. parse_identifier(shca, eq->eqe_cache[i].eqe->entry);
  543. }
  544. /* poll eq if not empty */
  545. if (eq_empty)
  546. goto unlock_irq_spinlock;
  547. do {
  548. struct ehca_eqe *eqe;
  549. eqe = ehca_poll_eq(shca, &shca->eq);
  550. if (!eqe)
  551. break;
  552. process_eqe(shca, eqe);
  553. } while (1);
  554. unlock_irq_spinlock:
  555. spin_unlock(&eq->irq_spinlock);
  556. }
  557. void ehca_tasklet_eq(unsigned long data)
  558. {
  559. ehca_process_eq((struct ehca_shca*)data, 1);
  560. }
  561. static int find_next_online_cpu(struct ehca_comp_pool *pool)
  562. {
  563. int cpu;
  564. unsigned long flags;
  565. WARN_ON_ONCE(!in_interrupt());
  566. if (ehca_debug_level >= 3)
  567. ehca_dmp(cpu_online_mask, cpumask_size(), "");
  568. spin_lock_irqsave(&pool->last_cpu_lock, flags);
  569. do {
  570. cpu = cpumask_next(pool->last_cpu, cpu_online_mask);
  571. if (cpu >= nr_cpu_ids)
  572. cpu = cpumask_first(cpu_online_mask);
  573. pool->last_cpu = cpu;
  574. } while (!per_cpu_ptr(pool->cpu_comp_tasks, cpu)->active);
  575. spin_unlock_irqrestore(&pool->last_cpu_lock, flags);
  576. return cpu;
  577. }
  578. static void __queue_comp_task(struct ehca_cq *__cq,
  579. struct ehca_cpu_comp_task *cct,
  580. struct task_struct *thread)
  581. {
  582. unsigned long flags;
  583. spin_lock_irqsave(&cct->task_lock, flags);
  584. spin_lock(&__cq->task_lock);
  585. if (__cq->nr_callbacks == 0) {
  586. __cq->nr_callbacks++;
  587. list_add_tail(&__cq->entry, &cct->cq_list);
  588. cct->cq_jobs++;
  589. wake_up_process(thread);
  590. } else
  591. __cq->nr_callbacks++;
  592. spin_unlock(&__cq->task_lock);
  593. spin_unlock_irqrestore(&cct->task_lock, flags);
  594. }
  595. static void queue_comp_task(struct ehca_cq *__cq)
  596. {
  597. int cpu_id;
  598. struct ehca_cpu_comp_task *cct;
  599. struct task_struct *thread;
  600. int cq_jobs;
  601. unsigned long flags;
  602. cpu_id = find_next_online_cpu(pool);
  603. BUG_ON(!cpu_online(cpu_id));
  604. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
  605. thread = *per_cpu_ptr(pool->cpu_comp_threads, cpu_id);
  606. BUG_ON(!cct || !thread);
  607. spin_lock_irqsave(&cct->task_lock, flags);
  608. cq_jobs = cct->cq_jobs;
  609. spin_unlock_irqrestore(&cct->task_lock, flags);
  610. if (cq_jobs > 0) {
  611. cpu_id = find_next_online_cpu(pool);
  612. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
  613. thread = *per_cpu_ptr(pool->cpu_comp_threads, cpu_id);
  614. BUG_ON(!cct || !thread);
  615. }
  616. __queue_comp_task(__cq, cct, thread);
  617. }
  618. static void run_comp_task(struct ehca_cpu_comp_task *cct)
  619. {
  620. struct ehca_cq *cq;
  621. while (!list_empty(&cct->cq_list)) {
  622. cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
  623. spin_unlock_irq(&cct->task_lock);
  624. comp_event_callback(cq);
  625. if (atomic_dec_and_test(&cq->nr_events))
  626. wake_up(&cq->wait_completion);
  627. spin_lock_irq(&cct->task_lock);
  628. spin_lock(&cq->task_lock);
  629. cq->nr_callbacks--;
  630. if (!cq->nr_callbacks) {
  631. list_del_init(cct->cq_list.next);
  632. cct->cq_jobs--;
  633. }
  634. spin_unlock(&cq->task_lock);
  635. }
  636. }
  637. static void comp_task_park(unsigned int cpu)
  638. {
  639. struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  640. struct ehca_cpu_comp_task *target;
  641. struct task_struct *thread;
  642. struct ehca_cq *cq, *tmp;
  643. LIST_HEAD(list);
  644. spin_lock_irq(&cct->task_lock);
  645. cct->cq_jobs = 0;
  646. cct->active = 0;
  647. list_splice_init(&cct->cq_list, &list);
  648. spin_unlock_irq(&cct->task_lock);
  649. cpu = find_next_online_cpu(pool);
  650. target = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  651. thread = *per_cpu_ptr(pool->cpu_comp_threads, cpu);
  652. spin_lock_irq(&target->task_lock);
  653. list_for_each_entry_safe(cq, tmp, &list, entry) {
  654. list_del(&cq->entry);
  655. __queue_comp_task(cq, target, thread);
  656. }
  657. spin_unlock_irq(&target->task_lock);
  658. }
  659. static void comp_task_stop(unsigned int cpu, bool online)
  660. {
  661. struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  662. spin_lock_irq(&cct->task_lock);
  663. cct->cq_jobs = 0;
  664. cct->active = 0;
  665. WARN_ON(!list_empty(&cct->cq_list));
  666. spin_unlock_irq(&cct->task_lock);
  667. }
  668. static int comp_task_should_run(unsigned int cpu)
  669. {
  670. struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  671. return cct->cq_jobs;
  672. }
  673. static void comp_task(unsigned int cpu)
  674. {
  675. struct ehca_cpu_comp_task *cct = this_cpu_ptr(pool->cpu_comp_tasks);
  676. int cql_empty;
  677. spin_lock_irq(&cct->task_lock);
  678. cql_empty = list_empty(&cct->cq_list);
  679. if (!cql_empty) {
  680. __set_current_state(TASK_RUNNING);
  681. run_comp_task(cct);
  682. }
  683. spin_unlock_irq(&cct->task_lock);
  684. }
  685. static struct smp_hotplug_thread comp_pool_threads = {
  686. .thread_should_run = comp_task_should_run,
  687. .thread_fn = comp_task,
  688. .thread_comm = "ehca_comp/%u",
  689. .cleanup = comp_task_stop,
  690. .park = comp_task_park,
  691. };
  692. int ehca_create_comp_pool(void)
  693. {
  694. int cpu, ret = -ENOMEM;
  695. if (!ehca_scaling_code)
  696. return 0;
  697. pool = kzalloc(sizeof(struct ehca_comp_pool), GFP_KERNEL);
  698. if (pool == NULL)
  699. return -ENOMEM;
  700. spin_lock_init(&pool->last_cpu_lock);
  701. pool->last_cpu = cpumask_any(cpu_online_mask);
  702. pool->cpu_comp_tasks = alloc_percpu(struct ehca_cpu_comp_task);
  703. if (!pool->cpu_comp_tasks)
  704. goto out_pool;
  705. pool->cpu_comp_threads = alloc_percpu(struct task_struct *);
  706. if (!pool->cpu_comp_threads)
  707. goto out_tasks;
  708. for_each_present_cpu(cpu) {
  709. struct ehca_cpu_comp_task *cct;
  710. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  711. spin_lock_init(&cct->task_lock);
  712. INIT_LIST_HEAD(&cct->cq_list);
  713. }
  714. comp_pool_threads.store = pool->cpu_comp_threads;
  715. ret = smpboot_register_percpu_thread(&comp_pool_threads);
  716. if (ret)
  717. goto out_threads;
  718. pr_info("eHCA scaling code enabled\n");
  719. return ret;
  720. out_threads:
  721. free_percpu(pool->cpu_comp_threads);
  722. out_tasks:
  723. free_percpu(pool->cpu_comp_tasks);
  724. out_pool:
  725. kfree(pool);
  726. return ret;
  727. }
  728. void ehca_destroy_comp_pool(void)
  729. {
  730. if (!ehca_scaling_code)
  731. return;
  732. smpboot_unregister_percpu_thread(&comp_pool_threads);
  733. free_percpu(pool->cpu_comp_threads);
  734. free_percpu(pool->cpu_comp_tasks);
  735. kfree(pool);
  736. }