ehca_irq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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 "ehca_classes.h"
  44. #include "ehca_irq.h"
  45. #include "ehca_iverbs.h"
  46. #include "ehca_tools.h"
  47. #include "hcp_if.h"
  48. #include "hipz_fns.h"
  49. #include "ipz_pt_fn.h"
  50. #define EQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
  51. #define EQE_CQ_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
  52. #define EQE_EE_IDENTIFIER EHCA_BMASK_IBM( 2, 7)
  53. #define EQE_CQ_NUMBER EHCA_BMASK_IBM( 8, 31)
  54. #define EQE_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
  55. #define EQE_QP_TOKEN EHCA_BMASK_IBM(32, 63)
  56. #define EQE_CQ_TOKEN EHCA_BMASK_IBM(32, 63)
  57. #define NEQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
  58. #define NEQE_EVENT_CODE EHCA_BMASK_IBM( 2, 7)
  59. #define NEQE_PORT_NUMBER EHCA_BMASK_IBM( 8, 15)
  60. #define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16, 16)
  61. #define NEQE_DISRUPTIVE EHCA_BMASK_IBM(16, 16)
  62. #define NEQE_SPECIFIC_EVENT EHCA_BMASK_IBM(16, 23)
  63. #define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52, 63)
  64. #define ERROR_DATA_TYPE EHCA_BMASK_IBM( 0, 7)
  65. static void queue_comp_task(struct ehca_cq *__cq);
  66. static struct ehca_comp_pool *pool;
  67. static inline void comp_event_callback(struct ehca_cq *cq)
  68. {
  69. if (!cq->ib_cq.comp_handler)
  70. return;
  71. spin_lock(&cq->cb_lock);
  72. cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context);
  73. spin_unlock(&cq->cb_lock);
  74. return;
  75. }
  76. static void print_error_data(struct ehca_shca *shca, void *data,
  77. u64 *rblock, int length)
  78. {
  79. u64 type = EHCA_BMASK_GET(ERROR_DATA_TYPE, rblock[2]);
  80. u64 resource = rblock[1];
  81. switch (type) {
  82. case 0x1: /* Queue Pair */
  83. {
  84. struct ehca_qp *qp = (struct ehca_qp *)data;
  85. /* only print error data if AER is set */
  86. if (rblock[6] == 0)
  87. return;
  88. ehca_err(&shca->ib_device,
  89. "QP 0x%x (resource=%lx) has errors.",
  90. qp->ib_qp.qp_num, resource);
  91. break;
  92. }
  93. case 0x4: /* Completion Queue */
  94. {
  95. struct ehca_cq *cq = (struct ehca_cq *)data;
  96. ehca_err(&shca->ib_device,
  97. "CQ 0x%x (resource=%lx) has errors.",
  98. cq->cq_number, resource);
  99. break;
  100. }
  101. default:
  102. ehca_err(&shca->ib_device,
  103. "Unknown error type: %lx on %s.",
  104. type, shca->ib_device.name);
  105. break;
  106. }
  107. ehca_err(&shca->ib_device, "Error data is available: %lx.", resource);
  108. ehca_err(&shca->ib_device, "EHCA ----- error data begin "
  109. "---------------------------------------------------");
  110. ehca_dmp(rblock, length, "resource=%lx", resource);
  111. ehca_err(&shca->ib_device, "EHCA ----- error data end "
  112. "----------------------------------------------------");
  113. return;
  114. }
  115. int ehca_error_data(struct ehca_shca *shca, void *data,
  116. u64 resource)
  117. {
  118. unsigned long ret;
  119. u64 *rblock;
  120. unsigned long block_count;
  121. rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
  122. if (!rblock) {
  123. ehca_err(&shca->ib_device, "Cannot allocate rblock memory.");
  124. ret = -ENOMEM;
  125. goto error_data1;
  126. }
  127. /* rblock must be 4K aligned and should be 4K large */
  128. ret = hipz_h_error_data(shca->ipz_hca_handle,
  129. resource,
  130. rblock,
  131. &block_count);
  132. if (ret == H_R_STATE)
  133. ehca_err(&shca->ib_device,
  134. "No error data is available: %lx.", resource);
  135. else if (ret == H_SUCCESS) {
  136. int length;
  137. length = EHCA_BMASK_GET(ERROR_DATA_LENGTH, rblock[0]);
  138. if (length > EHCA_PAGESIZE)
  139. length = EHCA_PAGESIZE;
  140. print_error_data(shca, data, rblock, length);
  141. } else
  142. ehca_err(&shca->ib_device,
  143. "Error data could not be fetched: %lx", resource);
  144. ehca_free_fw_ctrlblock(rblock);
  145. error_data1:
  146. return ret;
  147. }
  148. static void dispatch_qp_event(struct ehca_shca *shca, struct ehca_qp *qp,
  149. enum ib_event_type event_type)
  150. {
  151. struct ib_event event;
  152. event.device = &shca->ib_device;
  153. event.event = event_type;
  154. if (qp->ext_type == EQPT_SRQ) {
  155. if (!qp->ib_srq.event_handler)
  156. return;
  157. event.element.srq = &qp->ib_srq;
  158. qp->ib_srq.event_handler(&event, qp->ib_srq.srq_context);
  159. } else {
  160. if (!qp->ib_qp.event_handler)
  161. return;
  162. event.element.qp = &qp->ib_qp;
  163. qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
  164. }
  165. }
  166. static void qp_event_callback(struct ehca_shca *shca, u64 eqe,
  167. enum ib_event_type event_type, int fatal)
  168. {
  169. struct ehca_qp *qp;
  170. u32 token = EHCA_BMASK_GET(EQE_QP_TOKEN, eqe);
  171. read_lock(&ehca_qp_idr_lock);
  172. qp = idr_find(&ehca_qp_idr, token);
  173. if (qp)
  174. atomic_inc(&qp->nr_events);
  175. read_unlock(&ehca_qp_idr_lock);
  176. if (!qp)
  177. return;
  178. if (fatal)
  179. ehca_error_data(shca, qp, qp->ipz_qp_handle.handle);
  180. dispatch_qp_event(shca, qp, fatal && qp->ext_type == EQPT_SRQ ?
  181. IB_EVENT_SRQ_ERR : event_type);
  182. /*
  183. * eHCA only processes one WQE at a time for SRQ base QPs,
  184. * so the last WQE has been processed as soon as the QP enters
  185. * error state.
  186. */
  187. if (fatal && qp->ext_type == EQPT_SRQBASE)
  188. dispatch_qp_event(shca, qp, IB_EVENT_QP_LAST_WQE_REACHED);
  189. if (atomic_dec_and_test(&qp->nr_events))
  190. wake_up(&qp->wait_completion);
  191. return;
  192. }
  193. static void cq_event_callback(struct ehca_shca *shca,
  194. u64 eqe)
  195. {
  196. struct ehca_cq *cq;
  197. u32 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe);
  198. read_lock(&ehca_cq_idr_lock);
  199. cq = idr_find(&ehca_cq_idr, token);
  200. if (cq)
  201. atomic_inc(&cq->nr_events);
  202. read_unlock(&ehca_cq_idr_lock);
  203. if (!cq)
  204. return;
  205. ehca_error_data(shca, cq, cq->ipz_cq_handle.handle);
  206. if (atomic_dec_and_test(&cq->nr_events))
  207. wake_up(&cq->wait_completion);
  208. return;
  209. }
  210. static void parse_identifier(struct ehca_shca *shca, u64 eqe)
  211. {
  212. u8 identifier = EHCA_BMASK_GET(EQE_EE_IDENTIFIER, eqe);
  213. switch (identifier) {
  214. case 0x02: /* path migrated */
  215. qp_event_callback(shca, eqe, IB_EVENT_PATH_MIG, 0);
  216. break;
  217. case 0x03: /* communication established */
  218. qp_event_callback(shca, eqe, IB_EVENT_COMM_EST, 0);
  219. break;
  220. case 0x04: /* send queue drained */
  221. qp_event_callback(shca, eqe, IB_EVENT_SQ_DRAINED, 0);
  222. break;
  223. case 0x05: /* QP error */
  224. case 0x06: /* QP error */
  225. qp_event_callback(shca, eqe, IB_EVENT_QP_FATAL, 1);
  226. break;
  227. case 0x07: /* CQ error */
  228. case 0x08: /* CQ error */
  229. cq_event_callback(shca, eqe);
  230. break;
  231. case 0x09: /* MRMWPTE error */
  232. ehca_err(&shca->ib_device, "MRMWPTE error.");
  233. break;
  234. case 0x0A: /* port event */
  235. ehca_err(&shca->ib_device, "Port event.");
  236. break;
  237. case 0x0B: /* MR access error */
  238. ehca_err(&shca->ib_device, "MR access error.");
  239. break;
  240. case 0x0C: /* EQ error */
  241. ehca_err(&shca->ib_device, "EQ error.");
  242. break;
  243. case 0x0D: /* P/Q_Key mismatch */
  244. ehca_err(&shca->ib_device, "P/Q_Key mismatch.");
  245. break;
  246. case 0x10: /* sampling complete */
  247. ehca_err(&shca->ib_device, "Sampling complete.");
  248. break;
  249. case 0x11: /* unaffiliated access error */
  250. ehca_err(&shca->ib_device, "Unaffiliated access error.");
  251. break;
  252. case 0x12: /* path migrating */
  253. ehca_err(&shca->ib_device, "Path migrating.");
  254. break;
  255. case 0x13: /* interface trace stopped */
  256. ehca_err(&shca->ib_device, "Interface trace stopped.");
  257. break;
  258. case 0x14: /* first error capture info available */
  259. ehca_info(&shca->ib_device, "First error capture available");
  260. break;
  261. case 0x15: /* SRQ limit reached */
  262. qp_event_callback(shca, eqe, IB_EVENT_SRQ_LIMIT_REACHED, 0);
  263. break;
  264. default:
  265. ehca_err(&shca->ib_device, "Unknown identifier: %x on %s.",
  266. identifier, shca->ib_device.name);
  267. break;
  268. }
  269. return;
  270. }
  271. static void dispatch_port_event(struct ehca_shca *shca, int port_num,
  272. enum ib_event_type type, const char *msg)
  273. {
  274. struct ib_event event;
  275. ehca_info(&shca->ib_device, "port %d %s.", port_num, msg);
  276. event.device = &shca->ib_device;
  277. event.event = type;
  278. event.element.port_num = port_num;
  279. ib_dispatch_event(&event);
  280. }
  281. static void notify_port_conf_change(struct ehca_shca *shca, int port_num)
  282. {
  283. struct ehca_sma_attr new_attr;
  284. struct ehca_sma_attr *old_attr = &shca->sport[port_num - 1].saved_attr;
  285. ehca_query_sma_attr(shca, port_num, &new_attr);
  286. if (new_attr.sm_sl != old_attr->sm_sl ||
  287. new_attr.sm_lid != old_attr->sm_lid)
  288. dispatch_port_event(shca, port_num, IB_EVENT_SM_CHANGE,
  289. "SM changed");
  290. if (new_attr.lid != old_attr->lid ||
  291. new_attr.lmc != old_attr->lmc)
  292. dispatch_port_event(shca, port_num, IB_EVENT_LID_CHANGE,
  293. "LID changed");
  294. if (new_attr.pkey_tbl_len != old_attr->pkey_tbl_len ||
  295. memcmp(new_attr.pkeys, old_attr->pkeys,
  296. sizeof(u16) * new_attr.pkey_tbl_len))
  297. dispatch_port_event(shca, port_num, IB_EVENT_PKEY_CHANGE,
  298. "P_Key changed");
  299. *old_attr = new_attr;
  300. }
  301. static void parse_ec(struct ehca_shca *shca, u64 eqe)
  302. {
  303. u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
  304. u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
  305. u8 spec_event;
  306. struct ehca_sport *sport = &shca->sport[port - 1];
  307. unsigned long flags;
  308. switch (ec) {
  309. case 0x30: /* port availability change */
  310. if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
  311. int suppress_event;
  312. /* replay modify_qp for sqps */
  313. spin_lock_irqsave(&sport->mod_sqp_lock, flags);
  314. suppress_event = !sport->ibqp_sqp[IB_QPT_GSI];
  315. if (sport->ibqp_sqp[IB_QPT_SMI])
  316. ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_SMI]);
  317. if (!suppress_event)
  318. ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_GSI]);
  319. spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
  320. /* AQP1 was destroyed, ignore this event */
  321. if (suppress_event)
  322. break;
  323. sport->port_state = IB_PORT_ACTIVE;
  324. dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
  325. "is active");
  326. ehca_query_sma_attr(shca, port,
  327. &sport->saved_attr);
  328. } else {
  329. sport->port_state = IB_PORT_DOWN;
  330. dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
  331. "is inactive");
  332. }
  333. break;
  334. case 0x31:
  335. /* port configuration change
  336. * disruptive change is caused by
  337. * LID, PKEY or SM change
  338. */
  339. if (EHCA_BMASK_GET(NEQE_DISRUPTIVE, eqe)) {
  340. ehca_warn(&shca->ib_device, "disruptive port "
  341. "%d configuration change", port);
  342. sport->port_state = IB_PORT_DOWN;
  343. dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
  344. "is inactive");
  345. sport->port_state = IB_PORT_ACTIVE;
  346. dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
  347. "is active");
  348. ehca_query_sma_attr(shca, port,
  349. &sport->saved_attr);
  350. } else
  351. notify_port_conf_change(shca, port);
  352. break;
  353. case 0x32: /* adapter malfunction */
  354. ehca_err(&shca->ib_device, "Adapter malfunction.");
  355. break;
  356. case 0x33: /* trace stopped */
  357. ehca_err(&shca->ib_device, "Traced stopped.");
  358. break;
  359. case 0x34: /* util async event */
  360. spec_event = EHCA_BMASK_GET(NEQE_SPECIFIC_EVENT, eqe);
  361. if (spec_event == 0x80) /* client reregister required */
  362. dispatch_port_event(shca, port,
  363. IB_EVENT_CLIENT_REREGISTER,
  364. "client reregister req.");
  365. else
  366. ehca_warn(&shca->ib_device, "Unknown util async "
  367. "event %x on port %x", spec_event, port);
  368. break;
  369. default:
  370. ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
  371. ec, shca->ib_device.name);
  372. break;
  373. }
  374. return;
  375. }
  376. static inline void reset_eq_pending(struct ehca_cq *cq)
  377. {
  378. u64 CQx_EP;
  379. struct h_galpa gal = cq->galpas.kernel;
  380. hipz_galpa_store_cq(gal, cqx_ep, 0x0);
  381. CQx_EP = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_ep));
  382. return;
  383. }
  384. irqreturn_t ehca_interrupt_neq(int irq, void *dev_id)
  385. {
  386. struct ehca_shca *shca = (struct ehca_shca*)dev_id;
  387. tasklet_hi_schedule(&shca->neq.interrupt_task);
  388. return IRQ_HANDLED;
  389. }
  390. void ehca_tasklet_neq(unsigned long data)
  391. {
  392. struct ehca_shca *shca = (struct ehca_shca*)data;
  393. struct ehca_eqe *eqe;
  394. u64 ret;
  395. eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
  396. while (eqe) {
  397. if (!EHCA_BMASK_GET(NEQE_COMPLETION_EVENT, eqe->entry))
  398. parse_ec(shca, eqe->entry);
  399. eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
  400. }
  401. ret = hipz_h_reset_event(shca->ipz_hca_handle,
  402. shca->neq.ipz_eq_handle, 0xFFFFFFFFFFFFFFFFL);
  403. if (ret != H_SUCCESS)
  404. ehca_err(&shca->ib_device, "Can't clear notification events.");
  405. return;
  406. }
  407. irqreturn_t ehca_interrupt_eq(int irq, void *dev_id)
  408. {
  409. struct ehca_shca *shca = (struct ehca_shca*)dev_id;
  410. tasklet_hi_schedule(&shca->eq.interrupt_task);
  411. return IRQ_HANDLED;
  412. }
  413. static inline void process_eqe(struct ehca_shca *shca, struct ehca_eqe *eqe)
  414. {
  415. u64 eqe_value;
  416. u32 token;
  417. struct ehca_cq *cq;
  418. eqe_value = eqe->entry;
  419. ehca_dbg(&shca->ib_device, "eqe_value=%lx", eqe_value);
  420. if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
  421. ehca_dbg(&shca->ib_device, "Got completion event");
  422. token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
  423. read_lock(&ehca_cq_idr_lock);
  424. cq = idr_find(&ehca_cq_idr, token);
  425. if (cq)
  426. atomic_inc(&cq->nr_events);
  427. read_unlock(&ehca_cq_idr_lock);
  428. if (cq == NULL) {
  429. ehca_err(&shca->ib_device,
  430. "Invalid eqe for non-existing cq token=%x",
  431. token);
  432. return;
  433. }
  434. reset_eq_pending(cq);
  435. if (ehca_scaling_code)
  436. queue_comp_task(cq);
  437. else {
  438. comp_event_callback(cq);
  439. if (atomic_dec_and_test(&cq->nr_events))
  440. wake_up(&cq->wait_completion);
  441. }
  442. } else {
  443. ehca_dbg(&shca->ib_device, "Got non completion event");
  444. parse_identifier(shca, eqe_value);
  445. }
  446. }
  447. void ehca_process_eq(struct ehca_shca *shca, int is_irq)
  448. {
  449. struct ehca_eq *eq = &shca->eq;
  450. struct ehca_eqe_cache_entry *eqe_cache = eq->eqe_cache;
  451. u64 eqe_value;
  452. unsigned long flags;
  453. int eqe_cnt, i;
  454. int eq_empty = 0;
  455. spin_lock_irqsave(&eq->irq_spinlock, flags);
  456. if (is_irq) {
  457. const int max_query_cnt = 100;
  458. int query_cnt = 0;
  459. int int_state = 1;
  460. do {
  461. int_state = hipz_h_query_int_state(
  462. shca->ipz_hca_handle, eq->ist);
  463. query_cnt++;
  464. iosync();
  465. } while (int_state && query_cnt < max_query_cnt);
  466. if (unlikely((query_cnt == max_query_cnt)))
  467. ehca_dbg(&shca->ib_device, "int_state=%x query_cnt=%x",
  468. int_state, query_cnt);
  469. }
  470. /* read out all eqes */
  471. eqe_cnt = 0;
  472. do {
  473. u32 token;
  474. eqe_cache[eqe_cnt].eqe =
  475. (struct ehca_eqe *)ehca_poll_eq(shca, eq);
  476. if (!eqe_cache[eqe_cnt].eqe)
  477. break;
  478. eqe_value = eqe_cache[eqe_cnt].eqe->entry;
  479. if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
  480. token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
  481. read_lock(&ehca_cq_idr_lock);
  482. eqe_cache[eqe_cnt].cq = idr_find(&ehca_cq_idr, token);
  483. if (eqe_cache[eqe_cnt].cq)
  484. atomic_inc(&eqe_cache[eqe_cnt].cq->nr_events);
  485. read_unlock(&ehca_cq_idr_lock);
  486. if (!eqe_cache[eqe_cnt].cq) {
  487. ehca_err(&shca->ib_device,
  488. "Invalid eqe for non-existing cq "
  489. "token=%x", token);
  490. continue;
  491. }
  492. } else
  493. eqe_cache[eqe_cnt].cq = NULL;
  494. eqe_cnt++;
  495. } while (eqe_cnt < EHCA_EQE_CACHE_SIZE);
  496. if (!eqe_cnt) {
  497. if (is_irq)
  498. ehca_dbg(&shca->ib_device,
  499. "No eqe found for irq event");
  500. goto unlock_irq_spinlock;
  501. } else if (!is_irq)
  502. ehca_dbg(&shca->ib_device, "deadman found %x eqe", eqe_cnt);
  503. if (unlikely(eqe_cnt == EHCA_EQE_CACHE_SIZE))
  504. ehca_dbg(&shca->ib_device, "too many eqes for one irq event");
  505. /* enable irq for new packets */
  506. for (i = 0; i < eqe_cnt; i++) {
  507. if (eq->eqe_cache[i].cq)
  508. reset_eq_pending(eq->eqe_cache[i].cq);
  509. }
  510. /* check eq */
  511. spin_lock(&eq->spinlock);
  512. eq_empty = (!ipz_eqit_eq_peek_valid(&shca->eq.ipz_queue));
  513. spin_unlock(&eq->spinlock);
  514. /* call completion handler for cached eqes */
  515. for (i = 0; i < eqe_cnt; i++)
  516. if (eq->eqe_cache[i].cq) {
  517. if (ehca_scaling_code)
  518. queue_comp_task(eq->eqe_cache[i].cq);
  519. else {
  520. struct ehca_cq *cq = eq->eqe_cache[i].cq;
  521. comp_event_callback(cq);
  522. if (atomic_dec_and_test(&cq->nr_events))
  523. wake_up(&cq->wait_completion);
  524. }
  525. } else {
  526. ehca_dbg(&shca->ib_device, "Got non completion event");
  527. parse_identifier(shca, eq->eqe_cache[i].eqe->entry);
  528. }
  529. /* poll eq if not empty */
  530. if (eq_empty)
  531. goto unlock_irq_spinlock;
  532. do {
  533. struct ehca_eqe *eqe;
  534. eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->eq);
  535. if (!eqe)
  536. break;
  537. process_eqe(shca, eqe);
  538. } while (1);
  539. unlock_irq_spinlock:
  540. spin_unlock_irqrestore(&eq->irq_spinlock, flags);
  541. }
  542. void ehca_tasklet_eq(unsigned long data)
  543. {
  544. ehca_process_eq((struct ehca_shca*)data, 1);
  545. }
  546. static inline int find_next_online_cpu(struct ehca_comp_pool *pool)
  547. {
  548. int cpu;
  549. unsigned long flags;
  550. WARN_ON_ONCE(!in_interrupt());
  551. if (ehca_debug_level >= 3)
  552. ehca_dmp(&cpu_online_map, sizeof(cpumask_t), "");
  553. spin_lock_irqsave(&pool->last_cpu_lock, flags);
  554. cpu = next_cpu(pool->last_cpu, cpu_online_map);
  555. if (cpu == NR_CPUS)
  556. cpu = first_cpu(cpu_online_map);
  557. pool->last_cpu = cpu;
  558. spin_unlock_irqrestore(&pool->last_cpu_lock, flags);
  559. return cpu;
  560. }
  561. static void __queue_comp_task(struct ehca_cq *__cq,
  562. struct ehca_cpu_comp_task *cct)
  563. {
  564. unsigned long flags;
  565. spin_lock_irqsave(&cct->task_lock, flags);
  566. spin_lock(&__cq->task_lock);
  567. if (__cq->nr_callbacks == 0) {
  568. __cq->nr_callbacks++;
  569. list_add_tail(&__cq->entry, &cct->cq_list);
  570. cct->cq_jobs++;
  571. wake_up(&cct->wait_queue);
  572. } else
  573. __cq->nr_callbacks++;
  574. spin_unlock(&__cq->task_lock);
  575. spin_unlock_irqrestore(&cct->task_lock, flags);
  576. }
  577. static void queue_comp_task(struct ehca_cq *__cq)
  578. {
  579. int cpu_id;
  580. struct ehca_cpu_comp_task *cct;
  581. int cq_jobs;
  582. unsigned long flags;
  583. cpu_id = find_next_online_cpu(pool);
  584. BUG_ON(!cpu_online(cpu_id));
  585. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
  586. BUG_ON(!cct);
  587. spin_lock_irqsave(&cct->task_lock, flags);
  588. cq_jobs = cct->cq_jobs;
  589. spin_unlock_irqrestore(&cct->task_lock, flags);
  590. if (cq_jobs > 0) {
  591. cpu_id = find_next_online_cpu(pool);
  592. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
  593. BUG_ON(!cct);
  594. }
  595. __queue_comp_task(__cq, cct);
  596. }
  597. static void run_comp_task(struct ehca_cpu_comp_task *cct)
  598. {
  599. struct ehca_cq *cq;
  600. unsigned long flags;
  601. spin_lock_irqsave(&cct->task_lock, flags);
  602. while (!list_empty(&cct->cq_list)) {
  603. cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
  604. spin_unlock_irqrestore(&cct->task_lock, flags);
  605. comp_event_callback(cq);
  606. if (atomic_dec_and_test(&cq->nr_events))
  607. wake_up(&cq->wait_completion);
  608. spin_lock_irqsave(&cct->task_lock, flags);
  609. spin_lock(&cq->task_lock);
  610. cq->nr_callbacks--;
  611. if (!cq->nr_callbacks) {
  612. list_del_init(cct->cq_list.next);
  613. cct->cq_jobs--;
  614. }
  615. spin_unlock(&cq->task_lock);
  616. }
  617. spin_unlock_irqrestore(&cct->task_lock, flags);
  618. }
  619. static int comp_task(void *__cct)
  620. {
  621. struct ehca_cpu_comp_task *cct = __cct;
  622. int cql_empty;
  623. DECLARE_WAITQUEUE(wait, current);
  624. set_current_state(TASK_INTERRUPTIBLE);
  625. while (!kthread_should_stop()) {
  626. add_wait_queue(&cct->wait_queue, &wait);
  627. spin_lock_irq(&cct->task_lock);
  628. cql_empty = list_empty(&cct->cq_list);
  629. spin_unlock_irq(&cct->task_lock);
  630. if (cql_empty)
  631. schedule();
  632. else
  633. __set_current_state(TASK_RUNNING);
  634. remove_wait_queue(&cct->wait_queue, &wait);
  635. spin_lock_irq(&cct->task_lock);
  636. cql_empty = list_empty(&cct->cq_list);
  637. spin_unlock_irq(&cct->task_lock);
  638. if (!cql_empty)
  639. run_comp_task(__cct);
  640. set_current_state(TASK_INTERRUPTIBLE);
  641. }
  642. __set_current_state(TASK_RUNNING);
  643. return 0;
  644. }
  645. static struct task_struct *create_comp_task(struct ehca_comp_pool *pool,
  646. int cpu)
  647. {
  648. struct ehca_cpu_comp_task *cct;
  649. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  650. spin_lock_init(&cct->task_lock);
  651. INIT_LIST_HEAD(&cct->cq_list);
  652. init_waitqueue_head(&cct->wait_queue);
  653. cct->task = kthread_create(comp_task, cct, "ehca_comp/%d", cpu);
  654. return cct->task;
  655. }
  656. static void destroy_comp_task(struct ehca_comp_pool *pool,
  657. int cpu)
  658. {
  659. struct ehca_cpu_comp_task *cct;
  660. struct task_struct *task;
  661. unsigned long flags_cct;
  662. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  663. spin_lock_irqsave(&cct->task_lock, flags_cct);
  664. task = cct->task;
  665. cct->task = NULL;
  666. cct->cq_jobs = 0;
  667. spin_unlock_irqrestore(&cct->task_lock, flags_cct);
  668. if (task)
  669. kthread_stop(task);
  670. }
  671. static void __cpuinit take_over_work(struct ehca_comp_pool *pool, int cpu)
  672. {
  673. struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  674. LIST_HEAD(list);
  675. struct ehca_cq *cq;
  676. unsigned long flags_cct;
  677. spin_lock_irqsave(&cct->task_lock, flags_cct);
  678. list_splice_init(&cct->cq_list, &list);
  679. while (!list_empty(&list)) {
  680. cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
  681. list_del(&cq->entry);
  682. __queue_comp_task(cq, per_cpu_ptr(pool->cpu_comp_tasks,
  683. smp_processor_id()));
  684. }
  685. spin_unlock_irqrestore(&cct->task_lock, flags_cct);
  686. }
  687. static int __cpuinit comp_pool_callback(struct notifier_block *nfb,
  688. unsigned long action,
  689. void *hcpu)
  690. {
  691. unsigned int cpu = (unsigned long)hcpu;
  692. struct ehca_cpu_comp_task *cct;
  693. switch (action) {
  694. case CPU_UP_PREPARE:
  695. case CPU_UP_PREPARE_FROZEN:
  696. ehca_gen_dbg("CPU: %x (CPU_PREPARE)", cpu);
  697. if (!create_comp_task(pool, cpu)) {
  698. ehca_gen_err("Can't create comp_task for cpu: %x", cpu);
  699. return NOTIFY_BAD;
  700. }
  701. break;
  702. case CPU_UP_CANCELED:
  703. case CPU_UP_CANCELED_FROZEN:
  704. ehca_gen_dbg("CPU: %x (CPU_CANCELED)", cpu);
  705. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  706. kthread_bind(cct->task, any_online_cpu(cpu_online_map));
  707. destroy_comp_task(pool, cpu);
  708. break;
  709. case CPU_ONLINE:
  710. case CPU_ONLINE_FROZEN:
  711. ehca_gen_dbg("CPU: %x (CPU_ONLINE)", cpu);
  712. cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
  713. kthread_bind(cct->task, cpu);
  714. wake_up_process(cct->task);
  715. break;
  716. case CPU_DOWN_PREPARE:
  717. case CPU_DOWN_PREPARE_FROZEN:
  718. ehca_gen_dbg("CPU: %x (CPU_DOWN_PREPARE)", cpu);
  719. break;
  720. case CPU_DOWN_FAILED:
  721. case CPU_DOWN_FAILED_FROZEN:
  722. ehca_gen_dbg("CPU: %x (CPU_DOWN_FAILED)", cpu);
  723. break;
  724. case CPU_DEAD:
  725. case CPU_DEAD_FROZEN:
  726. ehca_gen_dbg("CPU: %x (CPU_DEAD)", cpu);
  727. destroy_comp_task(pool, cpu);
  728. take_over_work(pool, cpu);
  729. break;
  730. }
  731. return NOTIFY_OK;
  732. }
  733. static struct notifier_block comp_pool_callback_nb __cpuinitdata = {
  734. .notifier_call = comp_pool_callback,
  735. .priority = 0,
  736. };
  737. int ehca_create_comp_pool(void)
  738. {
  739. int cpu;
  740. struct task_struct *task;
  741. if (!ehca_scaling_code)
  742. return 0;
  743. pool = kzalloc(sizeof(struct ehca_comp_pool), GFP_KERNEL);
  744. if (pool == NULL)
  745. return -ENOMEM;
  746. spin_lock_init(&pool->last_cpu_lock);
  747. pool->last_cpu = any_online_cpu(cpu_online_map);
  748. pool->cpu_comp_tasks = alloc_percpu(struct ehca_cpu_comp_task);
  749. if (pool->cpu_comp_tasks == NULL) {
  750. kfree(pool);
  751. return -EINVAL;
  752. }
  753. for_each_online_cpu(cpu) {
  754. task = create_comp_task(pool, cpu);
  755. if (task) {
  756. kthread_bind(task, cpu);
  757. wake_up_process(task);
  758. }
  759. }
  760. register_hotcpu_notifier(&comp_pool_callback_nb);
  761. printk(KERN_INFO "eHCA scaling code enabled\n");
  762. return 0;
  763. }
  764. void ehca_destroy_comp_pool(void)
  765. {
  766. int i;
  767. if (!ehca_scaling_code)
  768. return;
  769. unregister_hotcpu_notifier(&comp_pool_callback_nb);
  770. for (i = 0; i < NR_CPUS; i++) {
  771. if (cpu_online(i))
  772. destroy_comp_task(pool, i);
  773. }
  774. free_percpu(pool->cpu_comp_tasks);
  775. kfree(pool);
  776. }