qdio_main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. QDIO_DBF_TEXT5(0, trace, "sigaout");
  293. QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
  294. qdio_perf_stat_inc(&perf_stats.siga_out);
  295. again:
  296. cc = qdio_do_siga_output(q, &busy_bit);
  297. if (queue_type(q) == QDIO_IQDIO_QFMT && cc == 2 && busy_bit) {
  298. if (!start_time)
  299. start_time = get_usecs();
  300. else if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
  301. goto again;
  302. }
  303. if (cc == 2 && busy_bit)
  304. cc |= QDIO_ERROR_SIGA_BUSY;
  305. if (cc)
  306. QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
  307. return cc;
  308. }
  309. static inline int qdio_siga_input(struct qdio_q *q)
  310. {
  311. int cc;
  312. QDIO_DBF_TEXT4(0, trace, "sigain");
  313. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  314. qdio_perf_stat_inc(&perf_stats.siga_in);
  315. cc = do_siga_input(q->irq_ptr->schid, q->mask);
  316. if (cc)
  317. QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
  318. return cc;
  319. }
  320. /* called from thinint inbound handler */
  321. void qdio_sync_after_thinint(struct qdio_q *q)
  322. {
  323. if (pci_out_supported(q)) {
  324. if (need_siga_sync_thinint(q))
  325. qdio_siga_sync_all(q);
  326. else if (need_siga_sync_out_thinint(q))
  327. qdio_siga_sync_out(q);
  328. } else
  329. qdio_siga_sync_q(q);
  330. }
  331. inline void qdio_stop_polling(struct qdio_q *q)
  332. {
  333. spin_lock_bh(&q->u.in.lock);
  334. if (!q->u.in.polling) {
  335. spin_unlock_bh(&q->u.in.lock);
  336. return;
  337. }
  338. q->u.in.polling = 0;
  339. qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
  340. /* show the card that we are not polling anymore */
  341. set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
  342. spin_unlock_bh(&q->u.in.lock);
  343. }
  344. static void announce_buffer_error(struct qdio_q *q)
  345. {
  346. char dbf_text[15];
  347. if (q->is_input_q)
  348. QDIO_DBF_TEXT3(1, trace, "inperr");
  349. else
  350. QDIO_DBF_TEXT3(0, trace, "outperr");
  351. sprintf(dbf_text, "%x-%x-%x", q->first_to_check,
  352. q->sbal[q->first_to_check]->element[14].flags,
  353. q->sbal[q->first_to_check]->element[15].flags);
  354. QDIO_DBF_TEXT3(1, trace, dbf_text);
  355. QDIO_DBF_HEX2(1, trace, q->sbal[q->first_to_check], 256);
  356. q->qdio_error = QDIO_ERROR_SLSB_STATE;
  357. }
  358. static int get_inbound_buffer_frontier(struct qdio_q *q)
  359. {
  360. int count, stop;
  361. unsigned char state;
  362. /*
  363. * If we still poll don't update last_move_ftc, keep the
  364. * previously ACK buffer there.
  365. */
  366. if (!q->u.in.polling)
  367. q->last_move_ftc = q->first_to_check;
  368. /*
  369. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  370. * would return 0.
  371. */
  372. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  373. stop = add_buf(q->first_to_check, count);
  374. /*
  375. * No siga sync here, as a PCI or we after a thin interrupt
  376. * will sync the queues.
  377. */
  378. /* need to set count to 1 for non-qebsm */
  379. if (!is_qebsm(q))
  380. count = 1;
  381. check_next:
  382. if (q->first_to_check == stop)
  383. goto out;
  384. count = get_buf_states(q, q->first_to_check, &state, count);
  385. if (!count)
  386. goto out;
  387. switch (state) {
  388. case SLSB_P_INPUT_PRIMED:
  389. QDIO_DBF_TEXT5(0, trace, "inptprim");
  390. /*
  391. * Only ACK the first buffer. The ACK will be removed in
  392. * qdio_stop_polling.
  393. */
  394. if (q->u.in.polling)
  395. state = SLSB_P_INPUT_NOT_INIT;
  396. else {
  397. q->u.in.polling = 1;
  398. state = SLSB_P_INPUT_ACK;
  399. }
  400. set_buf_state(q, q->first_to_check, state);
  401. /*
  402. * Need to change all PRIMED buffers to NOT_INIT, otherwise
  403. * we're loosing initiative in the thinint code.
  404. */
  405. if (count > 1)
  406. set_buf_states(q, next_buf(q->first_to_check),
  407. SLSB_P_INPUT_NOT_INIT, count - 1);
  408. /*
  409. * No siga-sync needed for non-qebsm here, as the inbound queue
  410. * will be synced on the next siga-r, resp.
  411. * tiqdio_is_inbound_q_done will do the siga-sync.
  412. */
  413. q->first_to_check = add_buf(q->first_to_check, count);
  414. atomic_sub(count, &q->nr_buf_used);
  415. goto check_next;
  416. case SLSB_P_INPUT_ERROR:
  417. announce_buffer_error(q);
  418. /* process the buffer, the upper layer will take care of it */
  419. q->first_to_check = add_buf(q->first_to_check, count);
  420. atomic_sub(count, &q->nr_buf_used);
  421. break;
  422. case SLSB_CU_INPUT_EMPTY:
  423. case SLSB_P_INPUT_NOT_INIT:
  424. case SLSB_P_INPUT_ACK:
  425. QDIO_DBF_TEXT5(0, trace, "inpnipro");
  426. break;
  427. default:
  428. BUG();
  429. }
  430. out:
  431. QDIO_DBF_HEX4(0, trace, &q->first_to_check, sizeof(int));
  432. return q->first_to_check;
  433. }
  434. int qdio_inbound_q_moved(struct qdio_q *q)
  435. {
  436. int bufnr;
  437. bufnr = get_inbound_buffer_frontier(q);
  438. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  439. if (!need_siga_sync(q) && !pci_out_supported(q))
  440. q->u.in.timestamp = get_usecs();
  441. QDIO_DBF_TEXT4(0, trace, "inhasmvd");
  442. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  443. return 1;
  444. } else
  445. return 0;
  446. }
  447. static int qdio_inbound_q_done(struct qdio_q *q)
  448. {
  449. unsigned char state;
  450. #ifdef CONFIG_QDIO_DEBUG
  451. char dbf_text[15];
  452. #endif
  453. if (!atomic_read(&q->nr_buf_used))
  454. return 1;
  455. /*
  456. * We need that one for synchronization with the adapter, as it
  457. * does a kind of PCI avoidance.
  458. */
  459. qdio_siga_sync_q(q);
  460. get_buf_state(q, q->first_to_check, &state);
  461. if (state == SLSB_P_INPUT_PRIMED)
  462. /* we got something to do */
  463. return 0;
  464. /* on VM, we don't poll, so the q is always done here */
  465. if (need_siga_sync(q) || pci_out_supported(q))
  466. return 1;
  467. /*
  468. * At this point we know, that inbound first_to_check
  469. * has (probably) not moved (see qdio_inbound_processing).
  470. */
  471. if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
  472. #ifdef CONFIG_QDIO_DEBUG
  473. QDIO_DBF_TEXT4(0, trace, "inqisdon");
  474. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  475. sprintf(dbf_text, "pf%02x", q->first_to_check);
  476. QDIO_DBF_TEXT4(0, trace, dbf_text);
  477. #endif /* CONFIG_QDIO_DEBUG */
  478. return 1;
  479. } else {
  480. #ifdef CONFIG_QDIO_DEBUG
  481. QDIO_DBF_TEXT4(0, trace, "inqisntd");
  482. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  483. sprintf(dbf_text, "pf%02x", q->first_to_check);
  484. QDIO_DBF_TEXT4(0, trace, dbf_text);
  485. #endif /* CONFIG_QDIO_DEBUG */
  486. return 0;
  487. }
  488. }
  489. void qdio_kick_inbound_handler(struct qdio_q *q)
  490. {
  491. int count, start, end;
  492. #ifdef CONFIG_QDIO_DEBUG
  493. char dbf_text[15];
  494. #endif
  495. qdio_perf_stat_inc(&perf_stats.inbound_handler);
  496. start = q->first_to_kick;
  497. end = q->first_to_check;
  498. if (end >= start)
  499. count = end - start;
  500. else
  501. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  502. #ifdef CONFIG_QDIO_DEBUG
  503. sprintf(dbf_text, "s=%2xc=%2x", start, count);
  504. QDIO_DBF_TEXT4(0, trace, dbf_text);
  505. #endif /* CONFIG_QDIO_DEBUG */
  506. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  507. return;
  508. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
  509. start, count, q->irq_ptr->int_parm);
  510. /* for the next time */
  511. q->first_to_kick = q->first_to_check;
  512. q->qdio_error = 0;
  513. }
  514. static void __qdio_inbound_processing(struct qdio_q *q)
  515. {
  516. qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
  517. again:
  518. if (!qdio_inbound_q_moved(q))
  519. return;
  520. qdio_kick_inbound_handler(q);
  521. if (!qdio_inbound_q_done(q))
  522. /* means poll time is not yet over */
  523. goto again;
  524. qdio_stop_polling(q);
  525. /*
  526. * We need to check again to not lose initiative after
  527. * resetting the ACK state.
  528. */
  529. if (!qdio_inbound_q_done(q))
  530. goto again;
  531. }
  532. /* inbound tasklet */
  533. void qdio_inbound_processing(unsigned long data)
  534. {
  535. struct qdio_q *q = (struct qdio_q *)data;
  536. __qdio_inbound_processing(q);
  537. }
  538. static int get_outbound_buffer_frontier(struct qdio_q *q)
  539. {
  540. int count, stop;
  541. unsigned char state;
  542. if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
  543. (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
  544. qdio_siga_sync_q(q);
  545. /*
  546. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  547. * would return 0.
  548. */
  549. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  550. stop = add_buf(q->first_to_check, count);
  551. /* need to set count to 1 for non-qebsm */
  552. if (!is_qebsm(q))
  553. count = 1;
  554. check_next:
  555. if (q->first_to_check == stop)
  556. return q->first_to_check;
  557. count = get_buf_states(q, q->first_to_check, &state, count);
  558. if (!count)
  559. return q->first_to_check;
  560. switch (state) {
  561. case SLSB_P_OUTPUT_EMPTY:
  562. /* the adapter got it */
  563. QDIO_DBF_TEXT5(0, trace, "outpempt");
  564. atomic_sub(count, &q->nr_buf_used);
  565. q->first_to_check = add_buf(q->first_to_check, count);
  566. /*
  567. * We fetch all buffer states at once. get_buf_states may
  568. * return count < stop. For QEBSM we do not loop.
  569. */
  570. if (is_qebsm(q))
  571. break;
  572. goto check_next;
  573. case SLSB_P_OUTPUT_ERROR:
  574. announce_buffer_error(q);
  575. /* process the buffer, the upper layer will take care of it */
  576. q->first_to_check = add_buf(q->first_to_check, count);
  577. atomic_sub(count, &q->nr_buf_used);
  578. break;
  579. case SLSB_CU_OUTPUT_PRIMED:
  580. /* the adapter has not fetched the output yet */
  581. QDIO_DBF_TEXT5(0, trace, "outpprim");
  582. break;
  583. case SLSB_P_OUTPUT_NOT_INIT:
  584. case SLSB_P_OUTPUT_HALTED:
  585. break;
  586. default:
  587. BUG();
  588. }
  589. return q->first_to_check;
  590. }
  591. /* all buffers processed? */
  592. static inline int qdio_outbound_q_done(struct qdio_q *q)
  593. {
  594. return atomic_read(&q->nr_buf_used) == 0;
  595. }
  596. static inline int qdio_outbound_q_moved(struct qdio_q *q)
  597. {
  598. int bufnr;
  599. bufnr = get_outbound_buffer_frontier(q);
  600. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  601. q->last_move_ftc = bufnr;
  602. QDIO_DBF_TEXT4(0, trace, "oqhasmvd");
  603. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  604. return 1;
  605. } else
  606. return 0;
  607. }
  608. /*
  609. * VM could present us cc=2 and busy bit set on SIGA-write
  610. * during reconfiguration of their Guest LAN (only in iqdio mode,
  611. * otherwise qdio is asynchronous and cc=2 and busy bit there will take
  612. * the queues down immediately).
  613. *
  614. * Therefore qdio_siga_output will try for a short time constantly,
  615. * if such a condition occurs. If it doesn't change, it will
  616. * increase the busy_siga_counter and save the timestamp, and
  617. * schedule the queue for later processing. qdio_outbound_processing
  618. * will check out the counter. If non-zero, it will call qdio_kick_outbound_q
  619. * as often as the value of the counter. This will attempt further SIGA
  620. * instructions. For each successful SIGA, the counter is
  621. * decreased, for failing SIGAs the counter remains the same, after
  622. * all. After some time of no movement, qdio_kick_outbound_q will
  623. * finally fail and reflect corresponding error codes to call
  624. * the upper layer module and have it take the queues down.
  625. *
  626. * Note that this is a change from the original HiperSockets design
  627. * (saying cc=2 and busy bit means take the queues down), but in
  628. * these days Guest LAN didn't exist... excessive cc=2 with busy bit
  629. * conditions will still take the queues down, but the threshold is
  630. * higher due to the Guest LAN environment.
  631. *
  632. * Called from outbound tasklet and do_QDIO handler.
  633. */
  634. static void qdio_kick_outbound_q(struct qdio_q *q)
  635. {
  636. int rc;
  637. #ifdef CONFIG_QDIO_DEBUG
  638. char dbf_text[15];
  639. QDIO_DBF_TEXT5(0, trace, "kickoutq");
  640. QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
  641. #endif /* CONFIG_QDIO_DEBUG */
  642. if (!need_siga_out(q))
  643. return;
  644. rc = qdio_siga_output(q);
  645. switch (rc) {
  646. case 0:
  647. /* went smooth this time, reset timestamp */
  648. q->u.out.timestamp = 0;
  649. /* TODO: improve error handling for CC=0 case */
  650. #ifdef CONFIG_QDIO_DEBUG
  651. QDIO_DBF_TEXT3(0, trace, "cc2reslv");
  652. sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
  653. atomic_read(&q->u.out.busy_siga_counter));
  654. QDIO_DBF_TEXT3(0, trace, dbf_text);
  655. #endif /* CONFIG_QDIO_DEBUG */
  656. break;
  657. /* cc=2 and busy bit */
  658. case (2 | QDIO_ERROR_SIGA_BUSY):
  659. atomic_inc(&q->u.out.busy_siga_counter);
  660. /* if the last siga was successful, save timestamp here */
  661. if (!q->u.out.timestamp)
  662. q->u.out.timestamp = get_usecs();
  663. /* if we're in time, don't touch qdio_error */
  664. if (get_usecs() - q->u.out.timestamp < QDIO_BUSY_BIT_GIVE_UP) {
  665. tasklet_schedule(&q->tasklet);
  666. break;
  667. }
  668. QDIO_DBF_TEXT2(0, trace, "cc2REPRT");
  669. #ifdef CONFIG_QDIO_DEBUG
  670. sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
  671. atomic_read(&q->u.out.busy_siga_counter));
  672. QDIO_DBF_TEXT3(0, trace, dbf_text);
  673. #endif /* CONFIG_QDIO_DEBUG */
  674. default:
  675. /* for plain cc=1, 2 or 3 */
  676. q->qdio_error = rc;
  677. }
  678. }
  679. static void qdio_kick_outbound_handler(struct qdio_q *q)
  680. {
  681. int start, end, count;
  682. #ifdef CONFIG_QDIO_DEBUG
  683. char dbf_text[15];
  684. #endif
  685. start = q->first_to_kick;
  686. end = q->last_move_ftc;
  687. if (end >= start)
  688. count = end - start;
  689. else
  690. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  691. #ifdef CONFIG_QDIO_DEBUG
  692. QDIO_DBF_TEXT4(0, trace, "kickouth");
  693. QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
  694. sprintf(dbf_text, "s=%2xc=%2x", start, count);
  695. QDIO_DBF_TEXT4(0, trace, dbf_text);
  696. #endif /* CONFIG_QDIO_DEBUG */
  697. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  698. return;
  699. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
  700. q->irq_ptr->int_parm);
  701. /* for the next time: */
  702. q->first_to_kick = q->last_move_ftc;
  703. q->qdio_error = 0;
  704. }
  705. static void __qdio_outbound_processing(struct qdio_q *q)
  706. {
  707. int siga_attempts;
  708. qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
  709. /* see comment in qdio_kick_outbound_q */
  710. siga_attempts = atomic_read(&q->u.out.busy_siga_counter);
  711. while (siga_attempts--) {
  712. atomic_dec(&q->u.out.busy_siga_counter);
  713. qdio_kick_outbound_q(q);
  714. }
  715. BUG_ON(atomic_read(&q->nr_buf_used) < 0);
  716. if (qdio_outbound_q_moved(q))
  717. qdio_kick_outbound_handler(q);
  718. if (queue_type(q) == QDIO_ZFCP_QFMT) {
  719. if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
  720. tasklet_schedule(&q->tasklet);
  721. return;
  722. }
  723. /* bail out for HiperSockets unicast queues */
  724. if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
  725. return;
  726. if (q->u.out.pci_out_enabled)
  727. return;
  728. /*
  729. * Now we know that queue type is either qeth without pci enabled
  730. * or HiperSockets multicast. Make sure buffer switch from PRIMED to
  731. * EMPTY is noticed and outbound_handler is called after some time.
  732. */
  733. if (qdio_outbound_q_done(q))
  734. del_timer(&q->u.out.timer);
  735. else {
  736. if (!timer_pending(&q->u.out.timer)) {
  737. mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
  738. qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
  739. }
  740. }
  741. }
  742. /* outbound tasklet */
  743. void qdio_outbound_processing(unsigned long data)
  744. {
  745. struct qdio_q *q = (struct qdio_q *)data;
  746. __qdio_outbound_processing(q);
  747. }
  748. void qdio_outbound_timer(unsigned long data)
  749. {
  750. struct qdio_q *q = (struct qdio_q *)data;
  751. tasklet_schedule(&q->tasklet);
  752. }
  753. /* called from thinint inbound tasklet */
  754. void qdio_check_outbound_after_thinint(struct qdio_q *q)
  755. {
  756. struct qdio_q *out;
  757. int i;
  758. if (!pci_out_supported(q))
  759. return;
  760. for_each_output_queue(q->irq_ptr, out, i)
  761. if (!qdio_outbound_q_done(out))
  762. tasklet_schedule(&out->tasklet);
  763. }
  764. static inline void qdio_set_state(struct qdio_irq *irq_ptr,
  765. enum qdio_irq_states state)
  766. {
  767. #ifdef CONFIG_QDIO_DEBUG
  768. char dbf_text[15];
  769. QDIO_DBF_TEXT5(0, trace, "newstate");
  770. sprintf(dbf_text, "%4x%4x", irq_ptr->schid.sch_no, state);
  771. QDIO_DBF_TEXT5(0, trace, dbf_text);
  772. #endif /* CONFIG_QDIO_DEBUG */
  773. irq_ptr->state = state;
  774. mb();
  775. }
  776. static void qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
  777. {
  778. char dbf_text[15];
  779. if (irb->esw.esw0.erw.cons) {
  780. sprintf(dbf_text, "sens%4x", schid.sch_no);
  781. QDIO_DBF_TEXT2(1, trace, dbf_text);
  782. QDIO_DBF_HEX0(0, trace, irb, 64);
  783. QDIO_DBF_HEX0(0, trace, irb->ecw, 64);
  784. }
  785. }
  786. /* PCI interrupt handler */
  787. static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
  788. {
  789. int i;
  790. struct qdio_q *q;
  791. qdio_perf_stat_inc(&perf_stats.pci_int);
  792. for_each_input_queue(irq_ptr, q, i)
  793. tasklet_schedule(&q->tasklet);
  794. if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
  795. return;
  796. for_each_output_queue(irq_ptr, q, i) {
  797. if (qdio_outbound_q_done(q))
  798. continue;
  799. if (!siga_syncs_out_pci(q))
  800. qdio_siga_sync_q(q);
  801. tasklet_schedule(&q->tasklet);
  802. }
  803. }
  804. static void qdio_handle_activate_check(struct ccw_device *cdev,
  805. unsigned long intparm, int cstat, int dstat)
  806. {
  807. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  808. struct qdio_q *q;
  809. char dbf_text[15];
  810. QDIO_DBF_TEXT2(1, trace, "ick2");
  811. sprintf(dbf_text, "%s", cdev->dev.bus_id);
  812. QDIO_DBF_TEXT2(1, trace, dbf_text);
  813. QDIO_DBF_HEX2(0, trace, &intparm, sizeof(int));
  814. QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
  815. QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
  816. if (irq_ptr->nr_input_qs) {
  817. q = irq_ptr->input_qs[0];
  818. } else if (irq_ptr->nr_output_qs) {
  819. q = irq_ptr->output_qs[0];
  820. } else {
  821. dump_stack();
  822. goto no_handler;
  823. }
  824. q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  825. 0, -1, -1, irq_ptr->int_parm);
  826. no_handler:
  827. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  828. }
  829. static void qdio_call_shutdown(struct work_struct *work)
  830. {
  831. struct ccw_device_private *priv;
  832. struct ccw_device *cdev;
  833. priv = container_of(work, struct ccw_device_private, kick_work);
  834. cdev = priv->cdev;
  835. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  836. put_device(&cdev->dev);
  837. }
  838. static void qdio_int_error(struct ccw_device *cdev)
  839. {
  840. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  841. switch (irq_ptr->state) {
  842. case QDIO_IRQ_STATE_INACTIVE:
  843. case QDIO_IRQ_STATE_CLEANUP:
  844. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  845. break;
  846. case QDIO_IRQ_STATE_ESTABLISHED:
  847. case QDIO_IRQ_STATE_ACTIVE:
  848. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  849. if (get_device(&cdev->dev)) {
  850. /* Can't call shutdown from interrupt context. */
  851. PREPARE_WORK(&cdev->private->kick_work,
  852. qdio_call_shutdown);
  853. queue_work(ccw_device_work, &cdev->private->kick_work);
  854. }
  855. break;
  856. default:
  857. WARN_ON(1);
  858. }
  859. wake_up(&cdev->private->wait_q);
  860. }
  861. static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
  862. int dstat)
  863. {
  864. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  865. if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
  866. QDIO_DBF_TEXT2(1, setup, "eq:ckcon");
  867. goto error;
  868. }
  869. if (!(dstat & DEV_STAT_DEV_END)) {
  870. QDIO_DBF_TEXT2(1, setup, "eq:no de");
  871. goto error;
  872. }
  873. if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
  874. QDIO_DBF_TEXT2(1, setup, "eq:badio");
  875. goto error;
  876. }
  877. return 0;
  878. error:
  879. QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
  880. QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
  881. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  882. return 1;
  883. }
  884. static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
  885. int dstat)
  886. {
  887. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  888. char dbf_text[15];
  889. sprintf(dbf_text, "qehi%4x", cdev->private->schid.sch_no);
  890. QDIO_DBF_TEXT0(0, setup, dbf_text);
  891. QDIO_DBF_TEXT0(0, trace, dbf_text);
  892. if (!qdio_establish_check_errors(cdev, cstat, dstat))
  893. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
  894. }
  895. /* qdio interrupt handler */
  896. void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
  897. struct irb *irb)
  898. {
  899. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  900. int cstat, dstat;
  901. char dbf_text[15];
  902. qdio_perf_stat_inc(&perf_stats.qdio_int);
  903. if (!intparm || !irq_ptr) {
  904. sprintf(dbf_text, "qihd%4x", cdev->private->schid.sch_no);
  905. QDIO_DBF_TEXT2(1, setup, dbf_text);
  906. return;
  907. }
  908. if (IS_ERR(irb)) {
  909. switch (PTR_ERR(irb)) {
  910. case -EIO:
  911. sprintf(dbf_text, "ierr%4x",
  912. cdev->private->schid.sch_no);
  913. QDIO_DBF_TEXT2(1, setup, dbf_text);
  914. qdio_int_error(cdev);
  915. return;
  916. case -ETIMEDOUT:
  917. sprintf(dbf_text, "qtoh%4x",
  918. cdev->private->schid.sch_no);
  919. QDIO_DBF_TEXT2(1, setup, dbf_text);
  920. qdio_int_error(cdev);
  921. return;
  922. default:
  923. WARN_ON(1);
  924. return;
  925. }
  926. }
  927. qdio_irq_check_sense(irq_ptr->schid, irb);
  928. cstat = irb->scsw.cmd.cstat;
  929. dstat = irb->scsw.cmd.dstat;
  930. switch (irq_ptr->state) {
  931. case QDIO_IRQ_STATE_INACTIVE:
  932. qdio_establish_handle_irq(cdev, cstat, dstat);
  933. break;
  934. case QDIO_IRQ_STATE_CLEANUP:
  935. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  936. break;
  937. case QDIO_IRQ_STATE_ESTABLISHED:
  938. case QDIO_IRQ_STATE_ACTIVE:
  939. if (cstat & SCHN_STAT_PCI) {
  940. qdio_int_handler_pci(irq_ptr);
  941. /* no state change so no need to wake up wait_q */
  942. return;
  943. }
  944. if ((cstat & ~SCHN_STAT_PCI) || dstat) {
  945. qdio_handle_activate_check(cdev, intparm, cstat,
  946. dstat);
  947. break;
  948. }
  949. default:
  950. WARN_ON(1);
  951. }
  952. wake_up(&cdev->private->wait_q);
  953. }
  954. /**
  955. * qdio_get_ssqd_desc - get qdio subchannel description
  956. * @cdev: ccw device to get description for
  957. *
  958. * Returns a pointer to the saved qdio subchannel description,
  959. * or NULL for not setup qdio devices.
  960. */
  961. struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev)
  962. {
  963. struct qdio_irq *irq_ptr;
  964. QDIO_DBF_TEXT0(0, setup, "getssqd");
  965. irq_ptr = cdev->private->qdio_data;
  966. if (!irq_ptr)
  967. return NULL;
  968. return &irq_ptr->ssqd_desc;
  969. }
  970. EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
  971. /**
  972. * qdio_cleanup - shutdown queues and free data structures
  973. * @cdev: associated ccw device
  974. * @how: use halt or clear to shutdown
  975. *
  976. * This function calls qdio_shutdown() for @cdev with method @how
  977. * and on success qdio_free() for @cdev.
  978. */
  979. int qdio_cleanup(struct ccw_device *cdev, int how)
  980. {
  981. struct qdio_irq *irq_ptr;
  982. char dbf_text[15];
  983. int rc;
  984. irq_ptr = cdev->private->qdio_data;
  985. if (!irq_ptr)
  986. return -ENODEV;
  987. sprintf(dbf_text, "qcln%4x", irq_ptr->schid.sch_no);
  988. QDIO_DBF_TEXT1(0, trace, dbf_text);
  989. QDIO_DBF_TEXT0(0, setup, dbf_text);
  990. rc = qdio_shutdown(cdev, how);
  991. if (rc == 0)
  992. rc = qdio_free(cdev);
  993. return rc;
  994. }
  995. EXPORT_SYMBOL_GPL(qdio_cleanup);
  996. static void qdio_shutdown_queues(struct ccw_device *cdev)
  997. {
  998. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  999. struct qdio_q *q;
  1000. int i;
  1001. for_each_input_queue(irq_ptr, q, i)
  1002. tasklet_disable(&q->tasklet);
  1003. for_each_output_queue(irq_ptr, q, i) {
  1004. tasklet_disable(&q->tasklet);
  1005. del_timer(&q->u.out.timer);
  1006. }
  1007. }
  1008. /**
  1009. * qdio_shutdown - shut down a qdio subchannel
  1010. * @cdev: associated ccw device
  1011. * @how: use halt or clear to shutdown
  1012. */
  1013. int qdio_shutdown(struct ccw_device *cdev, int how)
  1014. {
  1015. struct qdio_irq *irq_ptr;
  1016. int rc;
  1017. unsigned long flags;
  1018. char dbf_text[15];
  1019. irq_ptr = cdev->private->qdio_data;
  1020. if (!irq_ptr)
  1021. return -ENODEV;
  1022. mutex_lock(&irq_ptr->setup_mutex);
  1023. /*
  1024. * Subchannel was already shot down. We cannot prevent being called
  1025. * twice since cio may trigger a shutdown asynchronously.
  1026. */
  1027. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1028. mutex_unlock(&irq_ptr->setup_mutex);
  1029. return 0;
  1030. }
  1031. sprintf(dbf_text, "qsqs%4x", irq_ptr->schid.sch_no);
  1032. QDIO_DBF_TEXT1(0, trace, dbf_text);
  1033. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1034. tiqdio_remove_input_queues(irq_ptr);
  1035. qdio_shutdown_queues(cdev);
  1036. qdio_shutdown_debug_entries(irq_ptr, cdev);
  1037. /* cleanup subchannel */
  1038. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1039. if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
  1040. rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
  1041. else
  1042. /* default behaviour is halt */
  1043. rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
  1044. if (rc) {
  1045. sprintf(dbf_text, "sher%4x", irq_ptr->schid.sch_no);
  1046. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1047. sprintf(dbf_text, "rc=%d", rc);
  1048. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1049. goto no_cleanup;
  1050. }
  1051. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
  1052. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1053. wait_event_interruptible_timeout(cdev->private->wait_q,
  1054. irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
  1055. irq_ptr->state == QDIO_IRQ_STATE_ERR,
  1056. 10 * HZ);
  1057. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1058. no_cleanup:
  1059. qdio_shutdown_thinint(irq_ptr);
  1060. /* restore interrupt handler */
  1061. if ((void *)cdev->handler == (void *)qdio_int_handler)
  1062. cdev->handler = irq_ptr->orig_handler;
  1063. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1064. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1065. mutex_unlock(&irq_ptr->setup_mutex);
  1066. module_put(THIS_MODULE);
  1067. if (rc)
  1068. return rc;
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL_GPL(qdio_shutdown);
  1072. /**
  1073. * qdio_free - free data structures for a qdio subchannel
  1074. * @cdev: associated ccw device
  1075. */
  1076. int qdio_free(struct ccw_device *cdev)
  1077. {
  1078. struct qdio_irq *irq_ptr;
  1079. char dbf_text[15];
  1080. irq_ptr = cdev->private->qdio_data;
  1081. if (!irq_ptr)
  1082. return -ENODEV;
  1083. mutex_lock(&irq_ptr->setup_mutex);
  1084. sprintf(dbf_text, "qfqs%4x", irq_ptr->schid.sch_no);
  1085. QDIO_DBF_TEXT1(0, trace, dbf_text);
  1086. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1087. cdev->private->qdio_data = NULL;
  1088. mutex_unlock(&irq_ptr->setup_mutex);
  1089. qdio_release_memory(irq_ptr);
  1090. return 0;
  1091. }
  1092. EXPORT_SYMBOL_GPL(qdio_free);
  1093. /**
  1094. * qdio_initialize - allocate and establish queues for a qdio subchannel
  1095. * @init_data: initialization data
  1096. *
  1097. * This function first allocates queues via qdio_allocate() and on success
  1098. * establishes them via qdio_establish().
  1099. */
  1100. int qdio_initialize(struct qdio_initialize *init_data)
  1101. {
  1102. int rc;
  1103. char dbf_text[15];
  1104. sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
  1105. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1106. QDIO_DBF_TEXT0(0, trace, dbf_text);
  1107. rc = qdio_allocate(init_data);
  1108. if (rc)
  1109. return rc;
  1110. rc = qdio_establish(init_data);
  1111. if (rc)
  1112. qdio_free(init_data->cdev);
  1113. return rc;
  1114. }
  1115. EXPORT_SYMBOL_GPL(qdio_initialize);
  1116. /**
  1117. * qdio_allocate - allocate qdio queues and associated data
  1118. * @init_data: initialization data
  1119. */
  1120. int qdio_allocate(struct qdio_initialize *init_data)
  1121. {
  1122. struct qdio_irq *irq_ptr;
  1123. char dbf_text[15];
  1124. sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
  1125. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1126. QDIO_DBF_TEXT0(0, trace, 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 = kzalloc(sizeof(struct qdr), 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. irq_ptr = cdev->private->qdio_data;
  1184. if (!irq_ptr)
  1185. return -ENODEV;
  1186. if (cdev->private->state != DEV_STATE_ONLINE)
  1187. return -EINVAL;
  1188. if (!try_module_get(THIS_MODULE))
  1189. return -EINVAL;
  1190. sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
  1191. QDIO_DBF_TEXT0(0, setup, dbf_text);
  1192. QDIO_DBF_TEXT0(0, trace, dbf_text);
  1193. mutex_lock(&irq_ptr->setup_mutex);
  1194. qdio_setup_irq(init_data);
  1195. rc = qdio_establish_thinint(irq_ptr);
  1196. if (rc) {
  1197. mutex_unlock(&irq_ptr->setup_mutex);
  1198. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1199. return rc;
  1200. }
  1201. /* establish q */
  1202. irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
  1203. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1204. irq_ptr->ccw.count = irq_ptr->equeue.count;
  1205. irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
  1206. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1207. ccw_device_set_options_mask(cdev, 0);
  1208. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
  1209. if (rc) {
  1210. sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
  1211. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1212. sprintf(dbf_text, "eq:rc%4x", rc);
  1213. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1214. }
  1215. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1216. if (rc) {
  1217. mutex_unlock(&irq_ptr->setup_mutex);
  1218. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1219. return rc;
  1220. }
  1221. wait_event_interruptible_timeout(cdev->private->wait_q,
  1222. irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
  1223. irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
  1224. if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
  1225. mutex_unlock(&irq_ptr->setup_mutex);
  1226. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1227. return -EIO;
  1228. }
  1229. qdio_setup_ssqd_info(irq_ptr);
  1230. sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
  1231. QDIO_DBF_TEXT2(0, setup, dbf_text);
  1232. /* qebsm is now setup if available, initialize buffer states */
  1233. qdio_init_buf_states(irq_ptr);
  1234. mutex_unlock(&irq_ptr->setup_mutex);
  1235. qdio_print_subchannel_info(irq_ptr, cdev);
  1236. qdio_setup_debug_entries(irq_ptr, cdev);
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL_GPL(qdio_establish);
  1240. /**
  1241. * qdio_activate - activate queues on a qdio subchannel
  1242. * @cdev: associated cdev
  1243. */
  1244. int qdio_activate(struct ccw_device *cdev)
  1245. {
  1246. struct qdio_irq *irq_ptr;
  1247. int rc;
  1248. unsigned long saveflags;
  1249. char dbf_text[20];
  1250. irq_ptr = cdev->private->qdio_data;
  1251. if (!irq_ptr)
  1252. return -ENODEV;
  1253. if (cdev->private->state != DEV_STATE_ONLINE)
  1254. return -EINVAL;
  1255. mutex_lock(&irq_ptr->setup_mutex);
  1256. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1257. rc = -EBUSY;
  1258. goto out;
  1259. }
  1260. sprintf(dbf_text, "qact%4x", irq_ptr->schid.sch_no);
  1261. QDIO_DBF_TEXT2(0, setup, dbf_text);
  1262. QDIO_DBF_TEXT2(0, trace, dbf_text);
  1263. irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
  1264. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1265. irq_ptr->ccw.count = irq_ptr->aqueue.count;
  1266. irq_ptr->ccw.cda = 0;
  1267. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1268. ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
  1269. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
  1270. 0, DOIO_DENY_PREFETCH);
  1271. if (rc) {
  1272. sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
  1273. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1274. sprintf(dbf_text, "aq:rc%4x", rc);
  1275. QDIO_DBF_TEXT2(1, setup, dbf_text);
  1276. }
  1277. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1278. if (rc)
  1279. goto out;
  1280. if (is_thinint_irq(irq_ptr))
  1281. tiqdio_add_input_queues(irq_ptr);
  1282. /* wait for subchannel to become active */
  1283. msleep(5);
  1284. switch (irq_ptr->state) {
  1285. case QDIO_IRQ_STATE_STOPPED:
  1286. case QDIO_IRQ_STATE_ERR:
  1287. mutex_unlock(&irq_ptr->setup_mutex);
  1288. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1289. return -EIO;
  1290. default:
  1291. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
  1292. rc = 0;
  1293. }
  1294. out:
  1295. mutex_unlock(&irq_ptr->setup_mutex);
  1296. return rc;
  1297. }
  1298. EXPORT_SYMBOL_GPL(qdio_activate);
  1299. static inline int buf_in_between(int bufnr, int start, int count)
  1300. {
  1301. int end = add_buf(start, count);
  1302. if (end > start) {
  1303. if (bufnr >= start && bufnr < end)
  1304. return 1;
  1305. else
  1306. return 0;
  1307. }
  1308. /* wrap-around case */
  1309. if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
  1310. (bufnr < end))
  1311. return 1;
  1312. else
  1313. return 0;
  1314. }
  1315. /**
  1316. * handle_inbound - reset processed input buffers
  1317. * @q: queue containing the buffers
  1318. * @callflags: flags
  1319. * @bufnr: first buffer to process
  1320. * @count: how many buffers are emptied
  1321. */
  1322. static void handle_inbound(struct qdio_q *q, unsigned int callflags,
  1323. int bufnr, int count)
  1324. {
  1325. unsigned long flags;
  1326. int used, rc;
  1327. /*
  1328. * do_QDIO could run in parallel with the queue tasklet so the
  1329. * upper-layer programm could empty the ACK'ed buffer here.
  1330. * If that happens we must clear the polling flag, otherwise
  1331. * qdio_stop_polling() could set the buffer to NOT_INIT after
  1332. * it was set to EMPTY which would kill us.
  1333. */
  1334. spin_lock_irqsave(&q->u.in.lock, flags);
  1335. if (q->u.in.polling)
  1336. if (buf_in_between(q->last_move_ftc, bufnr, count))
  1337. q->u.in.polling = 0;
  1338. count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
  1339. spin_unlock_irqrestore(&q->u.in.lock, flags);
  1340. used = atomic_add_return(count, &q->nr_buf_used) - count;
  1341. BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
  1342. /* no need to signal as long as the adapter had free buffers */
  1343. if (used)
  1344. return;
  1345. if (need_siga_in(q)) {
  1346. rc = qdio_siga_input(q);
  1347. if (rc)
  1348. q->qdio_error = rc;
  1349. }
  1350. }
  1351. /**
  1352. * handle_outbound - process filled outbound buffers
  1353. * @q: queue containing the buffers
  1354. * @callflags: flags
  1355. * @bufnr: first buffer to process
  1356. * @count: how many buffers are filled
  1357. */
  1358. static void handle_outbound(struct qdio_q *q, unsigned int callflags,
  1359. int bufnr, int count)
  1360. {
  1361. unsigned char state;
  1362. int used;
  1363. qdio_perf_stat_inc(&perf_stats.outbound_handler);
  1364. count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
  1365. used = atomic_add_return(count, &q->nr_buf_used);
  1366. BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
  1367. if (callflags & QDIO_FLAG_PCI_OUT)
  1368. q->u.out.pci_out_enabled = 1;
  1369. else
  1370. q->u.out.pci_out_enabled = 0;
  1371. if (queue_type(q) == QDIO_IQDIO_QFMT) {
  1372. if (multicast_outbound(q))
  1373. qdio_kick_outbound_q(q);
  1374. else
  1375. /*
  1376. * One siga-w per buffer required for unicast
  1377. * HiperSockets.
  1378. */
  1379. while (count--)
  1380. qdio_kick_outbound_q(q);
  1381. goto out;
  1382. }
  1383. if (need_siga_sync(q)) {
  1384. qdio_siga_sync_q(q);
  1385. goto out;
  1386. }
  1387. /* try to fast requeue buffers */
  1388. get_buf_state(q, prev_buf(bufnr), &state);
  1389. if (state != SLSB_CU_OUTPUT_PRIMED)
  1390. qdio_kick_outbound_q(q);
  1391. else {
  1392. QDIO_DBF_TEXT5(0, trace, "fast-req");
  1393. qdio_perf_stat_inc(&perf_stats.fast_requeue);
  1394. }
  1395. out:
  1396. /* Fixme: could wait forever if called from process context */
  1397. tasklet_schedule(&q->tasklet);
  1398. }
  1399. /**
  1400. * do_QDIO - process input or output buffers
  1401. * @cdev: associated ccw_device for the qdio subchannel
  1402. * @callflags: input or output and special flags from the program
  1403. * @q_nr: queue number
  1404. * @bufnr: buffer number
  1405. * @count: how many buffers to process
  1406. */
  1407. int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
  1408. int q_nr, int bufnr, int count)
  1409. {
  1410. struct qdio_irq *irq_ptr;
  1411. #ifdef CONFIG_QDIO_DEBUG
  1412. char dbf_text[20];
  1413. sprintf(dbf_text, "doQD%04x", cdev->private->schid.sch_no);
  1414. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1415. #endif /* CONFIG_QDIO_DEBUG */
  1416. if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
  1417. (count > QDIO_MAX_BUFFERS_PER_Q) ||
  1418. (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
  1419. return -EINVAL;
  1420. if (!count)
  1421. return 0;
  1422. irq_ptr = cdev->private->qdio_data;
  1423. if (!irq_ptr)
  1424. return -ENODEV;
  1425. #ifdef CONFIG_QDIO_DEBUG
  1426. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1427. QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
  1428. sizeof(void *));
  1429. else
  1430. QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
  1431. sizeof(void *));
  1432. sprintf(dbf_text, "flag%04x", callflags);
  1433. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1434. sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
  1435. QDIO_DBF_TEXT3(0, trace, dbf_text);
  1436. #endif /* CONFIG_QDIO_DEBUG */
  1437. if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
  1438. return -EBUSY;
  1439. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1440. handle_inbound(irq_ptr->input_qs[q_nr],
  1441. callflags, bufnr, count);
  1442. else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
  1443. handle_outbound(irq_ptr->output_qs[q_nr],
  1444. callflags, bufnr, count);
  1445. else {
  1446. QDIO_DBF_TEXT3(1, trace, "doQD:inv");
  1447. return -EINVAL;
  1448. }
  1449. return 0;
  1450. }
  1451. EXPORT_SYMBOL_GPL(do_QDIO);
  1452. static int __init init_QDIO(void)
  1453. {
  1454. int rc;
  1455. rc = qdio_setup_init();
  1456. if (rc)
  1457. return rc;
  1458. rc = tiqdio_allocate_memory();
  1459. if (rc)
  1460. goto out_cache;
  1461. rc = qdio_debug_init();
  1462. if (rc)
  1463. goto out_ti;
  1464. rc = qdio_setup_perf_stats();
  1465. if (rc)
  1466. goto out_debug;
  1467. rc = tiqdio_register_thinints();
  1468. if (rc)
  1469. goto out_perf;
  1470. return 0;
  1471. out_perf:
  1472. qdio_remove_perf_stats();
  1473. out_debug:
  1474. qdio_debug_exit();
  1475. out_ti:
  1476. tiqdio_free_memory();
  1477. out_cache:
  1478. qdio_setup_exit();
  1479. return rc;
  1480. }
  1481. static void __exit exit_QDIO(void)
  1482. {
  1483. tiqdio_unregister_thinints();
  1484. tiqdio_free_memory();
  1485. qdio_remove_perf_stats();
  1486. qdio_debug_exit();
  1487. qdio_setup_exit();
  1488. }
  1489. module_init(init_QDIO);
  1490. module_exit(exit_QDIO);