ehca_irq.c 24 KB

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