qdio_main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*
  2. * linux/drivers/s390/cio/qdio_main.c
  3. *
  4. * Linux for s390 qdio support, buffer handling, qdio API and module support.
  5. *
  6. * Copyright 2000,2008 IBM Corp.
  7. * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
  8. * Jan Glauber <jang@linux.vnet.ibm.com>
  9. * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/timer.h>
  15. #include <linux/delay.h>
  16. #include <asm/atomic.h>
  17. #include <asm/debug.h>
  18. #include <asm/qdio.h>
  19. #include "cio.h"
  20. #include "css.h"
  21. #include "device.h"
  22. #include "qdio.h"
  23. #include "qdio_debug.h"
  24. #include "qdio_perf.h"
  25. MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
  26. "Jan Glauber <jang@linux.vnet.ibm.com>");
  27. MODULE_DESCRIPTION("QDIO base support");
  28. MODULE_LICENSE("GPL");
  29. static inline int do_siga_sync(struct subchannel_id schid,
  30. unsigned int out_mask, unsigned int in_mask)
  31. {
  32. register unsigned long __fc asm ("0") = 2;
  33. register struct subchannel_id __schid asm ("1") = schid;
  34. register unsigned long out asm ("2") = out_mask;
  35. register unsigned long in asm ("3") = in_mask;
  36. int cc;
  37. asm volatile(
  38. " siga 0\n"
  39. " ipm %0\n"
  40. " srl %0,28\n"
  41. : "=d" (cc)
  42. : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
  43. return cc;
  44. }
  45. static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
  46. {
  47. register unsigned long __fc asm ("0") = 1;
  48. register struct subchannel_id __schid asm ("1") = schid;
  49. register unsigned long __mask asm ("2") = mask;
  50. int cc;
  51. asm volatile(
  52. " siga 0\n"
  53. " ipm %0\n"
  54. " srl %0,28\n"
  55. : "=d" (cc)
  56. : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
  57. return cc;
  58. }
  59. /**
  60. * do_siga_output - perform SIGA-w/wt function
  61. * @schid: subchannel id or in case of QEBSM the subchannel token
  62. * @mask: which output queues to process
  63. * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
  64. * @fc: function code to perform
  65. *
  66. * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
  67. * Note: For IQDC unicast queues only the highest priority queue is processed.
  68. */
  69. static inline int do_siga_output(unsigned long schid, unsigned long mask,
  70. u32 *bb, unsigned int fc)
  71. {
  72. register unsigned long __fc asm("0") = fc;
  73. register unsigned long __schid asm("1") = schid;
  74. register unsigned long __mask asm("2") = mask;
  75. int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
  76. asm volatile(
  77. " siga 0\n"
  78. "0: ipm %0\n"
  79. " srl %0,28\n"
  80. "1:\n"
  81. EX_TABLE(0b, 1b)
  82. : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
  83. : : "cc", "memory");
  84. *bb = ((unsigned int) __fc) >> 31;
  85. return cc;
  86. }
  87. static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
  88. {
  89. char dbf_text[15];
  90. /* all done or next buffer state different */
  91. if (ccq == 0 || ccq == 32)
  92. return 0;
  93. /* not all buffers processed */
  94. if (ccq == 96 || ccq == 97)
  95. return 1;
  96. /* notify devices immediately */
  97. sprintf(dbf_text, "%d", ccq);
  98. QDIO_DBF_TEXT2(1, trace, dbf_text);
  99. return -EIO;
  100. }
  101. /**
  102. * qdio_do_eqbs - extract buffer states for QEBSM
  103. * @q: queue to manipulate
  104. * @state: state of the extracted buffers
  105. * @start: buffer number to start at
  106. * @count: count of buffers to examine
  107. *
  108. * Returns the number of successfull extracted equal buffer states.
  109. * Stops processing if a state is different from the last buffers state.
  110. */
  111. static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
  112. int start, int count)
  113. {
  114. unsigned int ccq = 0;
  115. int tmp_count = count, tmp_start = start;
  116. int nr = q->nr;
  117. int rc;
  118. char dbf_text[15];
  119. BUG_ON(!q->irq_ptr->sch_token);
  120. if (!q->is_input_q)
  121. nr += q->irq_ptr->nr_input_qs;
  122. again:
  123. ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
  124. rc = qdio_check_ccq(q, ccq);
  125. /* At least one buffer was processed, return and extract the remaining
  126. * buffers later.
  127. */
  128. if ((ccq == 96) && (count != tmp_count))
  129. return (count - tmp_count);
  130. if (rc == 1) {
  131. QDIO_DBF_TEXT5(1, trace, "eqAGAIN");
  132. goto again;
  133. }
  134. if (rc < 0) {
  135. QDIO_DBF_TEXT2(1, trace, "eqberr");
  136. sprintf(dbf_text, "%2x,%2x,%d,%d", count, tmp_count, ccq, nr);
  137. QDIO_DBF_TEXT2(1, trace, dbf_text);
  138. q->handler(q->irq_ptr->cdev,
  139. QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  140. 0, -1, -1, q->irq_ptr->int_parm);
  141. return 0;
  142. }
  143. return count - tmp_count;
  144. }
  145. /**
  146. * qdio_do_sqbs - set buffer states for QEBSM
  147. * @q: queue to manipulate
  148. * @state: new state of the buffers
  149. * @start: first buffer number to change
  150. * @count: how many buffers to change
  151. *
  152. * Returns the number of successfully changed buffers.
  153. * Does retrying until the specified count of buffer states is set or an
  154. * error occurs.
  155. */
  156. static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
  157. int count)
  158. {
  159. unsigned int ccq = 0;
  160. int tmp_count = count, tmp_start = start;
  161. int nr = q->nr;
  162. int rc;
  163. char dbf_text[15];
  164. BUG_ON(!q->irq_ptr->sch_token);
  165. if (!q->is_input_q)
  166. nr += q->irq_ptr->nr_input_qs;
  167. again:
  168. ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
  169. rc = qdio_check_ccq(q, ccq);
  170. if (rc == 1) {
  171. QDIO_DBF_TEXT5(1, trace, "sqAGAIN");
  172. goto again;
  173. }
  174. if (rc < 0) {
  175. QDIO_DBF_TEXT3(1, trace, "sqberr");
  176. sprintf(dbf_text, "%2x,%2x", count, tmp_count);
  177. QDIO_DBF_TEXT3(1, trace, dbf_text);
  178. sprintf(dbf_text, "%d,%d", ccq, nr);
  179. QDIO_DBF_TEXT3(1, trace, dbf_text);
  180. q->handler(q->irq_ptr->cdev,
  181. QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  182. 0, -1, -1, q->irq_ptr->int_parm);
  183. return 0;
  184. }
  185. WARN_ON(tmp_count);
  186. return count - tmp_count;
  187. }
  188. /* returns number of examined buffers and their common state in *state */
  189. static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
  190. unsigned char *state, unsigned int count)
  191. {
  192. unsigned char __state = 0;
  193. int i;
  194. BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
  195. BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
  196. if (is_qebsm(q))
  197. return qdio_do_eqbs(q, state, bufnr, count);
  198. for (i = 0; i < count; i++) {
  199. if (!__state)
  200. __state = q->slsb.val[bufnr];
  201. else if (q->slsb.val[bufnr] != __state)
  202. break;
  203. bufnr = next_buf(bufnr);
  204. }
  205. *state = __state;
  206. return i;
  207. }
  208. inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
  209. unsigned char *state)
  210. {
  211. return get_buf_states(q, bufnr, state, 1);
  212. }
  213. /* wrap-around safe setting of slsb states, returns number of changed buffers */
  214. static inline int set_buf_states(struct qdio_q *q, int bufnr,
  215. unsigned char state, int count)
  216. {
  217. int i;
  218. BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
  219. BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
  220. if (is_qebsm(q))
  221. return qdio_do_sqbs(q, state, bufnr, count);
  222. for (i = 0; i < count; i++) {
  223. xchg(&q->slsb.val[bufnr], state);
  224. bufnr = next_buf(bufnr);
  225. }
  226. return count;
  227. }
  228. static inline int set_buf_state(struct qdio_q *q, int bufnr,
  229. unsigned char state)
  230. {
  231. return set_buf_states(q, bufnr, state, 1);
  232. }
  233. /* set slsb states to initial state */
  234. void qdio_init_buf_states(struct qdio_irq *irq_ptr)
  235. {
  236. struct qdio_q *q;
  237. int i;
  238. for_each_input_queue(irq_ptr, q, i)
  239. set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
  240. QDIO_MAX_BUFFERS_PER_Q);
  241. for_each_output_queue(irq_ptr, q, i)
  242. set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
  243. QDIO_MAX_BUFFERS_PER_Q);
  244. }
  245. static int qdio_siga_sync(struct qdio_q *q, unsigned int output,
  246. unsigned int input)
  247. {
  248. int cc;
  249. if (!need_siga_sync(q))
  250. return 0;
  251. qdio_perf_stat_inc(&perf_stats.siga_sync);
  252. cc = do_siga_sync(q->irq_ptr->schid, output, input);
  253. if (cc) {
  254. QDIO_DBF_TEXT4(0, trace, "sigasync");
  255. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  256. QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
  257. }
  258. return cc;
  259. }
  260. inline int qdio_siga_sync_q(struct qdio_q *q)
  261. {
  262. if (q->is_input_q)
  263. return qdio_siga_sync(q, 0, q->mask);
  264. else
  265. return qdio_siga_sync(q, q->mask, 0);
  266. }
  267. static inline int qdio_siga_sync_out(struct qdio_q *q)
  268. {
  269. return qdio_siga_sync(q, ~0U, 0);
  270. }
  271. static inline int qdio_siga_sync_all(struct qdio_q *q)
  272. {
  273. return qdio_siga_sync(q, ~0U, ~0U);
  274. }
  275. static inline int qdio_do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
  276. {
  277. unsigned int fc = 0;
  278. unsigned long schid;
  279. if (!is_qebsm(q))
  280. schid = *((u32 *)&q->irq_ptr->schid);
  281. else {
  282. schid = q->irq_ptr->sch_token;
  283. fc |= 0x80;
  284. }
  285. return do_siga_output(schid, q->mask, busy_bit, fc);
  286. }
  287. static int qdio_siga_output(struct qdio_q *q)
  288. {
  289. int cc;
  290. u32 busy_bit;
  291. u64 start_time = 0;
  292. char dbf_text[15];
  293. QDIO_DBF_TEXT5(0, trace, "sigaout");
  294. QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
  295. qdio_perf_stat_inc(&perf_stats.siga_out);
  296. again:
  297. cc = qdio_do_siga_output(q, &busy_bit);
  298. if (queue_type(q) == QDIO_IQDIO_QFMT && cc == 2 && busy_bit) {
  299. sprintf(dbf_text, "bb%4x%2x", q->irq_ptr->schid.sch_no, q->nr);
  300. QDIO_DBF_TEXT3(0, trace, dbf_text);
  301. if (!start_time)
  302. start_time = get_usecs();
  303. else if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
  304. goto again;
  305. }
  306. if (cc == 2 && busy_bit)
  307. cc |= QDIO_ERROR_SIGA_BUSY;
  308. if (cc)
  309. QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
  310. return cc;
  311. }
  312. static inline int qdio_siga_input(struct qdio_q *q)
  313. {
  314. int cc;
  315. QDIO_DBF_TEXT4(0, trace, "sigain");
  316. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  317. qdio_perf_stat_inc(&perf_stats.siga_in);
  318. cc = do_siga_input(q->irq_ptr->schid, q->mask);
  319. if (cc)
  320. QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
  321. return cc;
  322. }
  323. /* called from thinint inbound handler */
  324. void qdio_sync_after_thinint(struct qdio_q *q)
  325. {
  326. if (pci_out_supported(q)) {
  327. if (need_siga_sync_thinint(q))
  328. qdio_siga_sync_all(q);
  329. else if (need_siga_sync_out_thinint(q))
  330. qdio_siga_sync_out(q);
  331. } else
  332. qdio_siga_sync_q(q);
  333. }
  334. inline void qdio_stop_polling(struct qdio_q *q)
  335. {
  336. spin_lock_bh(&q->u.in.lock);
  337. if (!q->u.in.polling) {
  338. spin_unlock_bh(&q->u.in.lock);
  339. return;
  340. }
  341. q->u.in.polling = 0;
  342. qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
  343. /* show the card that we are not polling anymore */
  344. set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
  345. spin_unlock_bh(&q->u.in.lock);
  346. }
  347. static void announce_buffer_error(struct qdio_q *q)
  348. {
  349. char dbf_text[15];
  350. if (q->is_input_q)
  351. QDIO_DBF_TEXT3(1, trace, "inperr");
  352. else
  353. QDIO_DBF_TEXT3(0, trace, "outperr");
  354. sprintf(dbf_text, "%x-%x-%x", q->first_to_check,
  355. q->sbal[q->first_to_check]->element[14].flags,
  356. q->sbal[q->first_to_check]->element[15].flags);
  357. QDIO_DBF_TEXT3(1, trace, dbf_text);
  358. QDIO_DBF_HEX2(1, trace, q->sbal[q->first_to_check], 256);
  359. q->qdio_error = QDIO_ERROR_SLSB_STATE;
  360. }
  361. static int get_inbound_buffer_frontier(struct qdio_q *q)
  362. {
  363. int count, stop;
  364. unsigned char state;
  365. /*
  366. * If we still poll don't update last_move_ftc, keep the
  367. * previously ACK buffer there.
  368. */
  369. if (!q->u.in.polling)
  370. q->last_move_ftc = q->first_to_check;
  371. /*
  372. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  373. * would return 0.
  374. */
  375. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  376. stop = add_buf(q->first_to_check, count);
  377. /*
  378. * No siga sync here, as a PCI or we after a thin interrupt
  379. * will sync the queues.
  380. */
  381. /* need to set count to 1 for non-qebsm */
  382. if (!is_qebsm(q))
  383. count = 1;
  384. check_next:
  385. if (q->first_to_check == stop)
  386. goto out;
  387. count = get_buf_states(q, q->first_to_check, &state, count);
  388. if (!count)
  389. goto out;
  390. switch (state) {
  391. case SLSB_P_INPUT_PRIMED:
  392. QDIO_DBF_TEXT5(0, trace, "inptprim");
  393. /*
  394. * Only ACK the first buffer. The ACK will be removed in
  395. * qdio_stop_polling.
  396. */
  397. if (q->u.in.polling)
  398. state = SLSB_P_INPUT_NOT_INIT;
  399. else {
  400. q->u.in.polling = 1;
  401. state = SLSB_P_INPUT_ACK;
  402. }
  403. set_buf_state(q, q->first_to_check, state);
  404. /*
  405. * Need to change all PRIMED buffers to NOT_INIT, otherwise
  406. * we're loosing initiative in the thinint code.
  407. */
  408. if (count > 1)
  409. set_buf_states(q, next_buf(q->first_to_check),
  410. SLSB_P_INPUT_NOT_INIT, count - 1);
  411. /*
  412. * No siga-sync needed for non-qebsm here, as the inbound queue
  413. * will be synced on the next siga-r, resp.
  414. * tiqdio_is_inbound_q_done will do the siga-sync.
  415. */
  416. q->first_to_check = add_buf(q->first_to_check, count);
  417. atomic_sub(count, &q->nr_buf_used);
  418. goto check_next;
  419. case SLSB_P_INPUT_ERROR:
  420. announce_buffer_error(q);
  421. /* process the buffer, the upper layer will take care of it */
  422. q->first_to_check = add_buf(q->first_to_check, count);
  423. atomic_sub(count, &q->nr_buf_used);
  424. break;
  425. case SLSB_CU_INPUT_EMPTY:
  426. case SLSB_P_INPUT_NOT_INIT:
  427. case SLSB_P_INPUT_ACK:
  428. QDIO_DBF_TEXT5(0, trace, "inpnipro");
  429. break;
  430. default:
  431. BUG();
  432. }
  433. out:
  434. QDIO_DBF_HEX4(0, trace, &q->first_to_check, sizeof(int));
  435. return q->first_to_check;
  436. }
  437. int qdio_inbound_q_moved(struct qdio_q *q)
  438. {
  439. int bufnr;
  440. bufnr = get_inbound_buffer_frontier(q);
  441. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  442. if (!need_siga_sync(q) && !pci_out_supported(q))
  443. q->u.in.timestamp = get_usecs();
  444. QDIO_DBF_TEXT4(0, trace, "inhasmvd");
  445. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  446. return 1;
  447. } else
  448. return 0;
  449. }
  450. static int qdio_inbound_q_done(struct qdio_q *q)
  451. {
  452. unsigned char state;
  453. #ifdef CONFIG_QDIO_DEBUG
  454. char dbf_text[15];
  455. #endif
  456. if (!atomic_read(&q->nr_buf_used))
  457. return 1;
  458. /*
  459. * We need that one for synchronization with the adapter, as it
  460. * does a kind of PCI avoidance.
  461. */
  462. qdio_siga_sync_q(q);
  463. get_buf_state(q, q->first_to_check, &state);
  464. if (state == SLSB_P_INPUT_PRIMED)
  465. /* we got something to do */
  466. return 0;
  467. /* on VM, we don't poll, so the q is always done here */
  468. if (need_siga_sync(q) || pci_out_supported(q))
  469. return 1;
  470. /*
  471. * At this point we know, that inbound first_to_check
  472. * has (probably) not moved (see qdio_inbound_processing).
  473. */
  474. if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
  475. #ifdef CONFIG_QDIO_DEBUG
  476. QDIO_DBF_TEXT4(0, trace, "inqisdon");
  477. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  478. sprintf(dbf_text, "pf%02x", q->first_to_check);
  479. QDIO_DBF_TEXT4(0, trace, dbf_text);
  480. #endif /* CONFIG_QDIO_DEBUG */
  481. return 1;
  482. } else {
  483. #ifdef CONFIG_QDIO_DEBUG
  484. QDIO_DBF_TEXT4(0, trace, "inqisntd");
  485. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  486. sprintf(dbf_text, "pf%02x", q->first_to_check);
  487. QDIO_DBF_TEXT4(0, trace, dbf_text);
  488. #endif /* CONFIG_QDIO_DEBUG */
  489. return 0;
  490. }
  491. }
  492. void qdio_kick_inbound_handler(struct qdio_q *q)
  493. {
  494. int count, start, end;
  495. #ifdef CONFIG_QDIO_DEBUG
  496. char dbf_text[15];
  497. #endif
  498. qdio_perf_stat_inc(&perf_stats.inbound_handler);
  499. start = q->first_to_kick;
  500. end = q->first_to_check;
  501. if (end >= start)
  502. count = end - start;
  503. else
  504. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  505. #ifdef CONFIG_QDIO_DEBUG
  506. sprintf(dbf_text, "s=%2xc=%2x", start, count);
  507. QDIO_DBF_TEXT4(0, trace, dbf_text);
  508. #endif /* CONFIG_QDIO_DEBUG */
  509. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  510. return;
  511. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
  512. start, count, q->irq_ptr->int_parm);
  513. /* for the next time */
  514. q->first_to_kick = q->first_to_check;
  515. q->qdio_error = 0;
  516. }
  517. static void __qdio_inbound_processing(struct qdio_q *q)
  518. {
  519. qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
  520. again:
  521. if (!qdio_inbound_q_moved(q))
  522. return;
  523. qdio_kick_inbound_handler(q);
  524. if (!qdio_inbound_q_done(q))
  525. /* means poll time is not yet over */
  526. goto again;
  527. qdio_stop_polling(q);
  528. /*
  529. * We need to check again to not lose initiative after
  530. * resetting the ACK state.
  531. */
  532. if (!qdio_inbound_q_done(q))
  533. goto again;
  534. }
  535. /* inbound tasklet */
  536. void qdio_inbound_processing(unsigned long data)
  537. {
  538. struct qdio_q *q = (struct qdio_q *)data;
  539. __qdio_inbound_processing(q);
  540. }
  541. static int get_outbound_buffer_frontier(struct qdio_q *q)
  542. {
  543. int count, stop;
  544. unsigned char state;
  545. if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
  546. (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
  547. qdio_siga_sync_q(q);
  548. /*
  549. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  550. * would return 0.
  551. */
  552. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  553. stop = add_buf(q->first_to_check, count);
  554. /* need to set count to 1 for non-qebsm */
  555. if (!is_qebsm(q))
  556. count = 1;
  557. check_next:
  558. if (q->first_to_check == stop)
  559. return q->first_to_check;
  560. count = get_buf_states(q, q->first_to_check, &state, count);
  561. if (!count)
  562. return q->first_to_check;
  563. switch (state) {
  564. case SLSB_P_OUTPUT_EMPTY:
  565. /* the adapter got it */
  566. QDIO_DBF_TEXT5(0, trace, "outpempt");
  567. atomic_sub(count, &q->nr_buf_used);
  568. q->first_to_check = add_buf(q->first_to_check, count);
  569. /*
  570. * We fetch all buffer states at once. get_buf_states may
  571. * return count < stop. For QEBSM we do not loop.
  572. */
  573. if (is_qebsm(q))
  574. break;
  575. goto check_next;
  576. case SLSB_P_OUTPUT_ERROR:
  577. announce_buffer_error(q);
  578. /* process the buffer, the upper layer will take care of it */
  579. q->first_to_check = add_buf(q->first_to_check, count);
  580. atomic_sub(count, &q->nr_buf_used);
  581. break;
  582. case SLSB_CU_OUTPUT_PRIMED:
  583. /* the adapter has not fetched the output yet */
  584. QDIO_DBF_TEXT5(0, trace, "outpprim");
  585. break;
  586. case SLSB_P_OUTPUT_NOT_INIT:
  587. case SLSB_P_OUTPUT_HALTED:
  588. break;
  589. default:
  590. BUG();
  591. }
  592. return q->first_to_check;
  593. }
  594. /* all buffers processed? */
  595. static inline int qdio_outbound_q_done(struct qdio_q *q)
  596. {
  597. return atomic_read(&q->nr_buf_used) == 0;
  598. }
  599. static inline int qdio_outbound_q_moved(struct qdio_q *q)
  600. {
  601. int bufnr;
  602. bufnr = get_outbound_buffer_frontier(q);
  603. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  604. q->last_move_ftc = bufnr;
  605. QDIO_DBF_TEXT4(0, trace, "oqhasmvd");
  606. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  607. return 1;
  608. } else
  609. return 0;
  610. }
  611. /*
  612. * VM could present us cc=2 and busy bit set on SIGA-write
  613. * during reconfiguration of their Guest LAN (only in iqdio mode,
  614. * otherwise qdio is asynchronous and cc=2 and busy bit there will take
  615. * the queues down immediately).
  616. *
  617. * Therefore qdio_siga_output will try for a short time constantly,
  618. * if such a condition occurs. If it doesn't change, it will
  619. * increase the busy_siga_counter and save the timestamp, and
  620. * schedule the queue for later processing. qdio_outbound_processing
  621. * will check out the counter. If non-zero, it will call qdio_kick_outbound_q
  622. * as often as the value of the counter. This will attempt further SIGA
  623. * instructions. For each successful SIGA, the counter is
  624. * decreased, for failing SIGAs the counter remains the same, after
  625. * all. After some time of no movement, qdio_kick_outbound_q will
  626. * finally fail and reflect corresponding error codes to call
  627. * the upper layer module and have it take the queues down.
  628. *
  629. * Note that this is a change from the original HiperSockets design
  630. * (saying cc=2 and busy bit means take the queues down), but in
  631. * these days Guest LAN didn't exist... excessive cc=2 with busy bit
  632. * conditions will still take the queues down, but the threshold is
  633. * higher due to the Guest LAN environment.
  634. *
  635. * Called from outbound tasklet and do_QDIO handler.
  636. */
  637. static void qdio_kick_outbound_q(struct qdio_q *q)
  638. {
  639. int rc;
  640. #ifdef CONFIG_QDIO_DEBUG
  641. char dbf_text[15];
  642. QDIO_DBF_TEXT5(0, trace, "kickoutq");
  643. QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
  644. #endif /* CONFIG_QDIO_DEBUG */
  645. if (!need_siga_out(q))
  646. return;
  647. rc = qdio_siga_output(q);
  648. switch (rc) {
  649. case 0:
  650. /* TODO: improve error handling for CC=0 case */
  651. #ifdef CONFIG_QDIO_DEBUG
  652. if (q->u.out.timestamp) {
  653. QDIO_DBF_TEXT3(0, trace, "cc2reslv");
  654. sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no,
  655. q->nr,
  656. atomic_read(&q->u.out.busy_siga_counter));
  657. QDIO_DBF_TEXT3(0, trace, dbf_text);
  658. }
  659. #endif /* CONFIG_QDIO_DEBUG */
  660. /* went smooth this time, reset timestamp */
  661. q->u.out.timestamp = 0;
  662. break;
  663. /* cc=2 and busy bit */
  664. case (2 | QDIO_ERROR_SIGA_BUSY):
  665. atomic_inc(&q->u.out.busy_siga_counter);
  666. /* if the last siga was successful, save timestamp here */
  667. if (!q->u.out.timestamp)
  668. q->u.out.timestamp = get_usecs();
  669. /* if we're in time, don't touch qdio_error */
  670. if (get_usecs() - q->u.out.timestamp < QDIO_BUSY_BIT_GIVE_UP) {
  671. tasklet_schedule(&q->tasklet);
  672. break;
  673. }
  674. QDIO_DBF_TEXT2(0, trace, "cc2REPRT");
  675. #ifdef CONFIG_QDIO_DEBUG
  676. sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
  677. atomic_read(&q->u.out.busy_siga_counter));
  678. QDIO_DBF_TEXT3(0, trace, dbf_text);
  679. #endif /* CONFIG_QDIO_DEBUG */
  680. default:
  681. /* for plain cc=1, 2 or 3 */
  682. q->qdio_error = rc;
  683. }
  684. }
  685. static void qdio_kick_outbound_handler(struct qdio_q *q)
  686. {
  687. int start, end, count;
  688. #ifdef CONFIG_QDIO_DEBUG
  689. char dbf_text[15];
  690. #endif
  691. start = q->first_to_kick;
  692. end = q->last_move_ftc;
  693. if (end >= start)
  694. count = end - start;
  695. else
  696. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  697. #ifdef CONFIG_QDIO_DEBUG
  698. QDIO_DBF_TEXT4(0, trace, "kickouth");
  699. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  700. sprintf(dbf_text, "s=%2xc=%2x", start, count);
  701. QDIO_DBF_TEXT4(0, trace, dbf_text);
  702. #endif /* CONFIG_QDIO_DEBUG */
  703. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  704. return;
  705. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
  706. q->irq_ptr->int_parm);
  707. /* for the next time: */
  708. q->first_to_kick = q->last_move_ftc;
  709. q->qdio_error = 0;
  710. }
  711. static void __qdio_outbound_processing(struct qdio_q *q)
  712. {
  713. int siga_attempts;
  714. qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
  715. /* see comment in qdio_kick_outbound_q */
  716. siga_attempts = atomic_read(&q->u.out.busy_siga_counter);
  717. while (siga_attempts--) {
  718. atomic_dec(&q->u.out.busy_siga_counter);
  719. qdio_kick_outbound_q(q);
  720. }
  721. BUG_ON(atomic_read(&q->nr_buf_used) < 0);
  722. if (qdio_outbound_q_moved(q))
  723. qdio_kick_outbound_handler(q);
  724. if (queue_type(q) == QDIO_ZFCP_QFMT) {
  725. if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
  726. tasklet_schedule(&q->tasklet);
  727. return;
  728. }
  729. /* bail out for HiperSockets unicast queues */
  730. if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
  731. return;
  732. if (q->u.out.pci_out_enabled)
  733. return;
  734. /*
  735. * Now we know that queue type is either qeth without pci enabled
  736. * or HiperSockets multicast. Make sure buffer switch from PRIMED to
  737. * EMPTY is noticed and outbound_handler is called after some time.
  738. */
  739. if (qdio_outbound_q_done(q))
  740. del_timer(&q->u.out.timer);
  741. else {
  742. if (!timer_pending(&q->u.out.timer)) {
  743. mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
  744. qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
  745. }
  746. }
  747. }
  748. /* outbound tasklet */
  749. void qdio_outbound_processing(unsigned long data)
  750. {
  751. struct qdio_q *q = (struct qdio_q *)data;
  752. __qdio_outbound_processing(q);
  753. }
  754. void qdio_outbound_timer(unsigned long data)
  755. {
  756. struct qdio_q *q = (struct qdio_q *)data;
  757. tasklet_schedule(&q->tasklet);
  758. }
  759. /* called from thinint inbound tasklet */
  760. void qdio_check_outbound_after_thinint(struct qdio_q *q)
  761. {
  762. struct qdio_q *out;
  763. int i;
  764. if (!pci_out_supported(q))
  765. return;
  766. for_each_output_queue(q->irq_ptr, out, i)
  767. if (!qdio_outbound_q_done(out))
  768. tasklet_schedule(&out->tasklet);
  769. }
  770. static inline void qdio_set_state(struct qdio_irq *irq_ptr,
  771. enum qdio_irq_states state)
  772. {
  773. #ifdef CONFIG_QDIO_DEBUG
  774. char dbf_text[15];
  775. QDIO_DBF_TEXT5(0, trace, "newstate");
  776. sprintf(dbf_text, "%4x%4x", irq_ptr->schid.sch_no, state);
  777. QDIO_DBF_TEXT5(0, trace, dbf_text);
  778. #endif /* CONFIG_QDIO_DEBUG */
  779. irq_ptr->state = state;
  780. mb();
  781. }
  782. static void qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
  783. {
  784. char dbf_text[15];
  785. if (irb->esw.esw0.erw.cons) {
  786. sprintf(dbf_text, "sens%4x", schid.sch_no);
  787. QDIO_DBF_TEXT2(1, trace, dbf_text);
  788. QDIO_DBF_HEX0(0, trace, irb, 64);
  789. QDIO_DBF_HEX0(0, trace, irb->ecw, 64);
  790. }
  791. }
  792. /* PCI interrupt handler */
  793. static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
  794. {
  795. int i;
  796. struct qdio_q *q;
  797. qdio_perf_stat_inc(&perf_stats.pci_int);
  798. for_each_input_queue(irq_ptr, q, i)
  799. tasklet_schedule(&q->tasklet);
  800. if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
  801. return;
  802. for_each_output_queue(irq_ptr, q, i) {
  803. if (qdio_outbound_q_done(q))
  804. continue;
  805. if (!siga_syncs_out_pci(q))
  806. qdio_siga_sync_q(q);
  807. tasklet_schedule(&q->tasklet);
  808. }
  809. }
  810. static void qdio_handle_activate_check(struct ccw_device *cdev,
  811. unsigned long intparm, int cstat, int dstat)
  812. {
  813. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  814. struct qdio_q *q;
  815. char dbf_text[15];
  816. QDIO_DBF_TEXT2(1, trace, "ick2");
  817. sprintf(dbf_text, "%s", cdev->dev.bus_id);
  818. QDIO_DBF_TEXT2(1, trace, dbf_text);
  819. QDIO_DBF_HEX2(0, trace, &intparm, sizeof(int));
  820. QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
  821. QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
  822. if (irq_ptr->nr_input_qs) {
  823. q = irq_ptr->input_qs[0];
  824. } else if (irq_ptr->nr_output_qs) {
  825. q = irq_ptr->output_qs[0];
  826. } else {
  827. dump_stack();
  828. goto no_handler;
  829. }
  830. q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  831. 0, -1, -1, irq_ptr->int_parm);
  832. no_handler:
  833. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  834. }
  835. static void qdio_call_shutdown(struct work_struct *work)
  836. {
  837. struct ccw_device_private *priv;
  838. struct ccw_device *cdev;
  839. priv = container_of(work, struct ccw_device_private, kick_work);
  840. cdev = priv->cdev;
  841. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  842. put_device(&cdev->dev);
  843. }
  844. static void qdio_int_error(struct ccw_device *cdev)
  845. {
  846. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  847. switch (irq_ptr->state) {
  848. case QDIO_IRQ_STATE_INACTIVE:
  849. case QDIO_IRQ_STATE_CLEANUP:
  850. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  851. break;
  852. case QDIO_IRQ_STATE_ESTABLISHED:
  853. case QDIO_IRQ_STATE_ACTIVE:
  854. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  855. if (get_device(&cdev->dev)) {
  856. /* Can't call shutdown from interrupt context. */
  857. PREPARE_WORK(&cdev->private->kick_work,
  858. qdio_call_shutdown);
  859. queue_work(ccw_device_work, &cdev->private->kick_work);
  860. }
  861. break;
  862. default:
  863. WARN_ON(1);
  864. }
  865. wake_up(&cdev->private->wait_q);
  866. }
  867. static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
  868. int dstat)
  869. {
  870. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  871. if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
  872. QDIO_DBF_TEXT2(1, setup, "eq:ckcon");
  873. goto error;
  874. }
  875. if (!(dstat & DEV_STAT_DEV_END)) {
  876. QDIO_DBF_TEXT2(1, setup, "eq:no de");
  877. goto error;
  878. }
  879. if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
  880. QDIO_DBF_TEXT2(1, setup, "eq:badio");
  881. goto error;
  882. }
  883. return 0;
  884. error:
  885. QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
  886. QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
  887. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  888. return 1;
  889. }
  890. static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
  891. int dstat)
  892. {
  893. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  894. char dbf_text[15];
  895. sprintf(dbf_text, "qehi%4x", cdev->private->schid.sch_no);
  896. QDIO_DBF_TEXT0(0, setup, dbf_text);
  897. QDIO_DBF_TEXT0(0, trace, dbf_text);
  898. if (!qdio_establish_check_errors(cdev, cstat, dstat))
  899. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
  900. }
  901. /* qdio interrupt handler */
  902. void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
  903. struct irb *irb)
  904. {
  905. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  906. int cstat, dstat;
  907. char dbf_text[15];
  908. qdio_perf_stat_inc(&perf_stats.qdio_int);
  909. if (!intparm || !irq_ptr) {
  910. sprintf(dbf_text, "qihd%4x", cdev->private->schid.sch_no);
  911. QDIO_DBF_TEXT2(1, setup, dbf_text);
  912. return;
  913. }
  914. if (IS_ERR(irb)) {
  915. switch (PTR_ERR(irb)) {
  916. case -EIO:
  917. sprintf(dbf_text, "ierr%4x", irq_ptr->schid.sch_no);
  918. QDIO_DBF_TEXT2(1, setup, dbf_text);
  919. qdio_int_error(cdev);
  920. return;
  921. case -ETIMEDOUT:
  922. sprintf(dbf_text, "qtoh%4x", irq_ptr->schid.sch_no);
  923. QDIO_DBF_TEXT2(1, setup, dbf_text);
  924. qdio_int_error(cdev);
  925. return;
  926. default:
  927. WARN_ON(1);
  928. return;
  929. }
  930. }
  931. qdio_irq_check_sense(irq_ptr->schid, irb);
  932. cstat = irb->scsw.cmd.cstat;
  933. dstat = irb->scsw.cmd.dstat;
  934. switch (irq_ptr->state) {
  935. case QDIO_IRQ_STATE_INACTIVE:
  936. qdio_establish_handle_irq(cdev, cstat, dstat);
  937. break;
  938. case QDIO_IRQ_STATE_CLEANUP:
  939. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  940. break;
  941. case QDIO_IRQ_STATE_ESTABLISHED:
  942. case QDIO_IRQ_STATE_ACTIVE:
  943. if (cstat & SCHN_STAT_PCI) {
  944. qdio_int_handler_pci(irq_ptr);
  945. /* no state change so no need to wake up wait_q */
  946. return;
  947. }
  948. if ((cstat & ~SCHN_STAT_PCI) || dstat) {
  949. qdio_handle_activate_check(cdev, intparm, cstat,
  950. dstat);
  951. break;
  952. }
  953. default:
  954. WARN_ON(1);
  955. }
  956. wake_up(&cdev->private->wait_q);
  957. }
  958. /**
  959. * qdio_get_ssqd_desc - get qdio subchannel description
  960. * @cdev: ccw device to get description for
  961. *
  962. * Returns a pointer to the saved qdio subchannel description,
  963. * or NULL for not setup qdio devices.
  964. */
  965. struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev)
  966. {
  967. struct qdio_irq *irq_ptr;
  968. char dbf_text[15];
  969. sprintf(dbf_text, "qssq%4x", cdev->private->schid.sch_no);
  970. QDIO_DBF_TEXT0(0, setup, dbf_text);
  971. irq_ptr = cdev->private->qdio_data;
  972. if (!irq_ptr)
  973. return NULL;
  974. return &irq_ptr->ssqd_desc;
  975. }
  976. EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
  977. /**
  978. * qdio_cleanup - shutdown queues and free data structures
  979. * @cdev: associated ccw device
  980. * @how: use halt or clear to shutdown
  981. *
  982. * This function calls qdio_shutdown() for @cdev with method @how
  983. * and on success qdio_free() for @cdev.
  984. */
  985. int qdio_cleanup(struct ccw_device *cdev, int how)
  986. {
  987. struct qdio_irq *irq_ptr;
  988. char dbf_text[15];
  989. int rc;
  990. sprintf(dbf_text, "qcln%4x", cdev->private->schid.sch_no);
  991. QDIO_DBF_TEXT0(0, setup, dbf_text);
  992. irq_ptr = cdev->private->qdio_data;
  993. if (!irq_ptr)
  994. return -ENODEV;
  995. rc = qdio_shutdown(cdev, how);
  996. if (rc == 0)
  997. rc = qdio_free(cdev);
  998. return rc;
  999. }
  1000. EXPORT_SYMBOL_GPL(qdio_cleanup);
  1001. static void qdio_shutdown_queues(struct ccw_device *cdev)
  1002. {
  1003. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  1004. struct qdio_q *q;
  1005. int i;
  1006. for_each_input_queue(irq_ptr, q, i)
  1007. tasklet_disable(&q->tasklet);
  1008. for_each_output_queue(irq_ptr, q, i) {
  1009. tasklet_disable(&q->tasklet);
  1010. del_timer(&q->u.out.timer);
  1011. }
  1012. }
  1013. /**
  1014. * qdio_shutdown - shut down a qdio subchannel
  1015. * @cdev: associated ccw device
  1016. * @how: use halt or clear to shutdown
  1017. */
  1018. int qdio_shutdown(struct ccw_device *cdev, int how)
  1019. {
  1020. struct qdio_irq *irq_ptr;
  1021. int rc;
  1022. unsigned long flags;
  1023. char dbf_text[15];
  1024. sprintf(dbf_text, "qshu%4x", cdev->private->schid.sch_no);
  1025. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1026. irq_ptr = cdev->private->qdio_data;
  1027. if (!irq_ptr)
  1028. return -ENODEV;
  1029. mutex_lock(&irq_ptr->setup_mutex);
  1030. /*
  1031. * Subchannel was already shot down. We cannot prevent being called
  1032. * twice since cio may trigger a shutdown asynchronously.
  1033. */
  1034. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1035. mutex_unlock(&irq_ptr->setup_mutex);
  1036. return 0;
  1037. }
  1038. tiqdio_remove_input_queues(irq_ptr);
  1039. qdio_shutdown_queues(cdev);
  1040. qdio_shutdown_debug_entries(irq_ptr, cdev);
  1041. /* cleanup subchannel */
  1042. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1043. if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
  1044. rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
  1045. else
  1046. /* default behaviour is halt */
  1047. rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
  1048. if (rc) {
  1049. sprintf(dbf_text, "sher%4x", irq_ptr->schid.sch_no);
  1050. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1051. sprintf(dbf_text, "rc=%d", rc);
  1052. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1053. goto no_cleanup;
  1054. }
  1055. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
  1056. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1057. wait_event_interruptible_timeout(cdev->private->wait_q,
  1058. irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
  1059. irq_ptr->state == QDIO_IRQ_STATE_ERR,
  1060. 10 * HZ);
  1061. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1062. no_cleanup:
  1063. qdio_shutdown_thinint(irq_ptr);
  1064. /* restore interrupt handler */
  1065. if ((void *)cdev->handler == (void *)qdio_int_handler)
  1066. cdev->handler = irq_ptr->orig_handler;
  1067. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1068. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1069. mutex_unlock(&irq_ptr->setup_mutex);
  1070. if (rc)
  1071. return rc;
  1072. return 0;
  1073. }
  1074. EXPORT_SYMBOL_GPL(qdio_shutdown);
  1075. /**
  1076. * qdio_free - free data structures for a qdio subchannel
  1077. * @cdev: associated ccw device
  1078. */
  1079. int qdio_free(struct ccw_device *cdev)
  1080. {
  1081. struct qdio_irq *irq_ptr;
  1082. char dbf_text[15];
  1083. sprintf(dbf_text, "qfre%4x", cdev->private->schid.sch_no);
  1084. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1085. irq_ptr = cdev->private->qdio_data;
  1086. if (!irq_ptr)
  1087. return -ENODEV;
  1088. mutex_lock(&irq_ptr->setup_mutex);
  1089. cdev->private->qdio_data = NULL;
  1090. mutex_unlock(&irq_ptr->setup_mutex);
  1091. qdio_release_memory(irq_ptr);
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL_GPL(qdio_free);
  1095. /**
  1096. * qdio_initialize - allocate and establish queues for a qdio subchannel
  1097. * @init_data: initialization data
  1098. *
  1099. * This function first allocates queues via qdio_allocate() and on success
  1100. * establishes them via qdio_establish().
  1101. */
  1102. int qdio_initialize(struct qdio_initialize *init_data)
  1103. {
  1104. int rc;
  1105. char dbf_text[15];
  1106. sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
  1107. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1108. rc = qdio_allocate(init_data);
  1109. if (rc)
  1110. return rc;
  1111. rc = qdio_establish(init_data);
  1112. if (rc)
  1113. qdio_free(init_data->cdev);
  1114. return rc;
  1115. }
  1116. EXPORT_SYMBOL_GPL(qdio_initialize);
  1117. /**
  1118. * qdio_allocate - allocate qdio queues and associated data
  1119. * @init_data: initialization data
  1120. */
  1121. int qdio_allocate(struct qdio_initialize *init_data)
  1122. {
  1123. struct qdio_irq *irq_ptr;
  1124. char dbf_text[15];
  1125. sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
  1126. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1127. if ((init_data->no_input_qs && !init_data->input_handler) ||
  1128. (init_data->no_output_qs && !init_data->output_handler))
  1129. return -EINVAL;
  1130. if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
  1131. (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
  1132. return -EINVAL;
  1133. if ((!init_data->input_sbal_addr_array) ||
  1134. (!init_data->output_sbal_addr_array))
  1135. return -EINVAL;
  1136. qdio_allocate_do_dbf(init_data);
  1137. /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
  1138. irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1139. if (!irq_ptr)
  1140. goto out_err;
  1141. QDIO_DBF_TEXT0(0, setup, "irq_ptr:");
  1142. QDIO_DBF_HEX0(0, setup, &irq_ptr, sizeof(void *));
  1143. mutex_init(&irq_ptr->setup_mutex);
  1144. /*
  1145. * Allocate a page for the chsc calls in qdio_establish.
  1146. * Must be pre-allocated since a zfcp recovery will call
  1147. * qdio_establish. In case of low memory and swap on a zfcp disk
  1148. * we may not be able to allocate memory otherwise.
  1149. */
  1150. irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
  1151. if (!irq_ptr->chsc_page)
  1152. goto out_rel;
  1153. /* qdr is used in ccw1.cda which is u32 */
  1154. irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1155. if (!irq_ptr->qdr)
  1156. goto out_rel;
  1157. WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
  1158. QDIO_DBF_TEXT0(0, setup, "qdr:");
  1159. QDIO_DBF_HEX0(0, setup, &irq_ptr->qdr, sizeof(void *));
  1160. if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
  1161. init_data->no_output_qs))
  1162. goto out_rel;
  1163. init_data->cdev->private->qdio_data = irq_ptr;
  1164. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1165. return 0;
  1166. out_rel:
  1167. qdio_release_memory(irq_ptr);
  1168. out_err:
  1169. return -ENOMEM;
  1170. }
  1171. EXPORT_SYMBOL_GPL(qdio_allocate);
  1172. /**
  1173. * qdio_establish - establish queues on a qdio subchannel
  1174. * @init_data: initialization data
  1175. */
  1176. int qdio_establish(struct qdio_initialize *init_data)
  1177. {
  1178. char dbf_text[20];
  1179. struct qdio_irq *irq_ptr;
  1180. struct ccw_device *cdev = init_data->cdev;
  1181. unsigned long saveflags;
  1182. int rc;
  1183. sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
  1184. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1185. irq_ptr = cdev->private->qdio_data;
  1186. if (!irq_ptr)
  1187. return -ENODEV;
  1188. if (cdev->private->state != DEV_STATE_ONLINE)
  1189. return -EINVAL;
  1190. mutex_lock(&irq_ptr->setup_mutex);
  1191. qdio_setup_irq(init_data);
  1192. rc = qdio_establish_thinint(irq_ptr);
  1193. if (rc) {
  1194. mutex_unlock(&irq_ptr->setup_mutex);
  1195. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1196. return rc;
  1197. }
  1198. /* establish q */
  1199. irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
  1200. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1201. irq_ptr->ccw.count = irq_ptr->equeue.count;
  1202. irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
  1203. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1204. ccw_device_set_options_mask(cdev, 0);
  1205. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
  1206. if (rc) {
  1207. sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
  1208. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1209. sprintf(dbf_text, "eq:rc%4x", rc);
  1210. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1211. }
  1212. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1213. if (rc) {
  1214. mutex_unlock(&irq_ptr->setup_mutex);
  1215. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1216. return rc;
  1217. }
  1218. wait_event_interruptible_timeout(cdev->private->wait_q,
  1219. irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
  1220. irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
  1221. if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
  1222. mutex_unlock(&irq_ptr->setup_mutex);
  1223. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1224. return -EIO;
  1225. }
  1226. qdio_setup_ssqd_info(irq_ptr);
  1227. sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
  1228. QDIO_DBF_TEXT2(0, setup, dbf_text);
  1229. /* qebsm is now setup if available, initialize buffer states */
  1230. qdio_init_buf_states(irq_ptr);
  1231. mutex_unlock(&irq_ptr->setup_mutex);
  1232. qdio_print_subchannel_info(irq_ptr, cdev);
  1233. qdio_setup_debug_entries(irq_ptr, cdev);
  1234. return 0;
  1235. }
  1236. EXPORT_SYMBOL_GPL(qdio_establish);
  1237. /**
  1238. * qdio_activate - activate queues on a qdio subchannel
  1239. * @cdev: associated cdev
  1240. */
  1241. int qdio_activate(struct ccw_device *cdev)
  1242. {
  1243. struct qdio_irq *irq_ptr;
  1244. int rc;
  1245. unsigned long saveflags;
  1246. char dbf_text[20];
  1247. sprintf(dbf_text, "qact%4x", cdev->private->schid.sch_no);
  1248. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1249. irq_ptr = cdev->private->qdio_data;
  1250. if (!irq_ptr)
  1251. return -ENODEV;
  1252. if (cdev->private->state != DEV_STATE_ONLINE)
  1253. return -EINVAL;
  1254. mutex_lock(&irq_ptr->setup_mutex);
  1255. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1256. rc = -EBUSY;
  1257. goto out;
  1258. }
  1259. irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
  1260. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1261. irq_ptr->ccw.count = irq_ptr->aqueue.count;
  1262. irq_ptr->ccw.cda = 0;
  1263. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1264. ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
  1265. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
  1266. 0, DOIO_DENY_PREFETCH);
  1267. if (rc) {
  1268. sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
  1269. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1270. sprintf(dbf_text, "aq:rc%4x", rc);
  1271. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1272. }
  1273. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1274. if (rc)
  1275. goto out;
  1276. if (is_thinint_irq(irq_ptr))
  1277. tiqdio_add_input_queues(irq_ptr);
  1278. /* wait for subchannel to become active */
  1279. msleep(5);
  1280. switch (irq_ptr->state) {
  1281. case QDIO_IRQ_STATE_STOPPED:
  1282. case QDIO_IRQ_STATE_ERR:
  1283. mutex_unlock(&irq_ptr->setup_mutex);
  1284. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1285. return -EIO;
  1286. default:
  1287. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
  1288. rc = 0;
  1289. }
  1290. out:
  1291. mutex_unlock(&irq_ptr->setup_mutex);
  1292. return rc;
  1293. }
  1294. EXPORT_SYMBOL_GPL(qdio_activate);
  1295. static inline int buf_in_between(int bufnr, int start, int count)
  1296. {
  1297. int end = add_buf(start, count);
  1298. if (end > start) {
  1299. if (bufnr >= start && bufnr < end)
  1300. return 1;
  1301. else
  1302. return 0;
  1303. }
  1304. /* wrap-around case */
  1305. if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
  1306. (bufnr < end))
  1307. return 1;
  1308. else
  1309. return 0;
  1310. }
  1311. /**
  1312. * handle_inbound - reset processed input buffers
  1313. * @q: queue containing the buffers
  1314. * @callflags: flags
  1315. * @bufnr: first buffer to process
  1316. * @count: how many buffers are emptied
  1317. */
  1318. static void handle_inbound(struct qdio_q *q, unsigned int callflags,
  1319. int bufnr, int count)
  1320. {
  1321. unsigned long flags;
  1322. int used, rc;
  1323. /*
  1324. * do_QDIO could run in parallel with the queue tasklet so the
  1325. * upper-layer programm could empty the ACK'ed buffer here.
  1326. * If that happens we must clear the polling flag, otherwise
  1327. * qdio_stop_polling() could set the buffer to NOT_INIT after
  1328. * it was set to EMPTY which would kill us.
  1329. */
  1330. spin_lock_irqsave(&q->u.in.lock, flags);
  1331. if (q->u.in.polling)
  1332. if (buf_in_between(q->last_move_ftc, bufnr, count))
  1333. q->u.in.polling = 0;
  1334. count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
  1335. spin_unlock_irqrestore(&q->u.in.lock, flags);
  1336. used = atomic_add_return(count, &q->nr_buf_used) - count;
  1337. BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
  1338. /* no need to signal as long as the adapter had free buffers */
  1339. if (used)
  1340. return;
  1341. if (need_siga_in(q)) {
  1342. rc = qdio_siga_input(q);
  1343. if (rc)
  1344. q->qdio_error = rc;
  1345. }
  1346. }
  1347. /**
  1348. * handle_outbound - process filled outbound buffers
  1349. * @q: queue containing the buffers
  1350. * @callflags: flags
  1351. * @bufnr: first buffer to process
  1352. * @count: how many buffers are filled
  1353. */
  1354. static void handle_outbound(struct qdio_q *q, unsigned int callflags,
  1355. int bufnr, int count)
  1356. {
  1357. unsigned char state;
  1358. int used;
  1359. qdio_perf_stat_inc(&perf_stats.outbound_handler);
  1360. count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
  1361. used = atomic_add_return(count, &q->nr_buf_used);
  1362. BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
  1363. if (callflags & QDIO_FLAG_PCI_OUT)
  1364. q->u.out.pci_out_enabled = 1;
  1365. else
  1366. q->u.out.pci_out_enabled = 0;
  1367. if (queue_type(q) == QDIO_IQDIO_QFMT) {
  1368. if (multicast_outbound(q))
  1369. qdio_kick_outbound_q(q);
  1370. else
  1371. /*
  1372. * One siga-w per buffer required for unicast
  1373. * HiperSockets.
  1374. */
  1375. while (count--)
  1376. qdio_kick_outbound_q(q);
  1377. goto out;
  1378. }
  1379. if (need_siga_sync(q)) {
  1380. qdio_siga_sync_q(q);
  1381. goto out;
  1382. }
  1383. /* try to fast requeue buffers */
  1384. get_buf_state(q, prev_buf(bufnr), &state);
  1385. if (state != SLSB_CU_OUTPUT_PRIMED)
  1386. qdio_kick_outbound_q(q);
  1387. else {
  1388. QDIO_DBF_TEXT5(0, trace, "fast-req");
  1389. qdio_perf_stat_inc(&perf_stats.fast_requeue);
  1390. }
  1391. out:
  1392. /* Fixme: could wait forever if called from process context */
  1393. tasklet_schedule(&q->tasklet);
  1394. }
  1395. /**
  1396. * do_QDIO - process input or output buffers
  1397. * @cdev: associated ccw_device for the qdio subchannel
  1398. * @callflags: input or output and special flags from the program
  1399. * @q_nr: queue number
  1400. * @bufnr: buffer number
  1401. * @count: how many buffers to process
  1402. */
  1403. int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
  1404. int q_nr, int bufnr, int count)
  1405. {
  1406. struct qdio_irq *irq_ptr;
  1407. #ifdef CONFIG_QDIO_DEBUG
  1408. char dbf_text[20];
  1409. sprintf(dbf_text, "doQD%4x", cdev->private->schid.sch_no);
  1410. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1411. #endif /* CONFIG_QDIO_DEBUG */
  1412. if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
  1413. (count > QDIO_MAX_BUFFERS_PER_Q) ||
  1414. (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
  1415. return -EINVAL;
  1416. if (!count)
  1417. return 0;
  1418. irq_ptr = cdev->private->qdio_data;
  1419. if (!irq_ptr)
  1420. return -ENODEV;
  1421. #ifdef CONFIG_QDIO_DEBUG
  1422. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1423. QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
  1424. sizeof(void *));
  1425. else
  1426. QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
  1427. sizeof(void *));
  1428. sprintf(dbf_text, "flag%04x", callflags);
  1429. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1430. sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
  1431. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1432. #endif /* CONFIG_QDIO_DEBUG */
  1433. if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
  1434. return -EBUSY;
  1435. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1436. handle_inbound(irq_ptr->input_qs[q_nr],
  1437. callflags, bufnr, count);
  1438. else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
  1439. handle_outbound(irq_ptr->output_qs[q_nr],
  1440. callflags, bufnr, count);
  1441. else {
  1442. QDIO_DBF_TEXT3(1, trace, "doQD:inv");
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. EXPORT_SYMBOL_GPL(do_QDIO);
  1448. static int __init init_QDIO(void)
  1449. {
  1450. int rc;
  1451. rc = qdio_setup_init();
  1452. if (rc)
  1453. return rc;
  1454. rc = tiqdio_allocate_memory();
  1455. if (rc)
  1456. goto out_cache;
  1457. rc = qdio_debug_init();
  1458. if (rc)
  1459. goto out_ti;
  1460. rc = qdio_setup_perf_stats();
  1461. if (rc)
  1462. goto out_debug;
  1463. rc = tiqdio_register_thinints();
  1464. if (rc)
  1465. goto out_perf;
  1466. return 0;
  1467. out_perf:
  1468. qdio_remove_perf_stats();
  1469. out_debug:
  1470. qdio_debug_exit();
  1471. out_ti:
  1472. tiqdio_free_memory();
  1473. out_cache:
  1474. qdio_setup_exit();
  1475. return rc;
  1476. }
  1477. static void __exit exit_QDIO(void)
  1478. {
  1479. tiqdio_unregister_thinints();
  1480. tiqdio_free_memory();
  1481. qdio_remove_perf_stats();
  1482. qdio_debug_exit();
  1483. qdio_setup_exit();
  1484. }
  1485. module_init(init_QDIO);
  1486. module_exit(exit_QDIO);