ehca_irq.c 21 KB

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