ehca_irq.c 20 KB

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