qdio_main.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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. unsigned int *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. /* all done or next buffer state different */
  90. if (ccq == 0 || ccq == 32)
  91. return 0;
  92. /* not all buffers processed */
  93. if (ccq == 96 || ccq == 97)
  94. return 1;
  95. /* notify devices immediately */
  96. DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
  97. return -EIO;
  98. }
  99. /**
  100. * qdio_do_eqbs - extract buffer states for QEBSM
  101. * @q: queue to manipulate
  102. * @state: state of the extracted buffers
  103. * @start: buffer number to start at
  104. * @count: count of buffers to examine
  105. * @auto_ack: automatically acknowledge buffers
  106. *
  107. * Returns the number of successfully extracted equal buffer states.
  108. * Stops processing if a state is different from the last buffers state.
  109. */
  110. static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
  111. int start, int count, int auto_ack)
  112. {
  113. unsigned int ccq = 0;
  114. int tmp_count = count, tmp_start = start;
  115. int nr = q->nr;
  116. int rc;
  117. BUG_ON(!q->irq_ptr->sch_token);
  118. qdio_perf_stat_inc(&perf_stats.debug_eqbs_all);
  119. if (!q->is_input_q)
  120. nr += q->irq_ptr->nr_input_qs;
  121. again:
  122. ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
  123. auto_ack);
  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. qdio_perf_stat_inc(&perf_stats.debug_eqbs_incomplete);
  130. return (count - tmp_count);
  131. }
  132. if (rc == 1) {
  133. DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
  134. goto again;
  135. }
  136. if (rc < 0) {
  137. DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
  138. DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
  139. q->handler(q->irq_ptr->cdev,
  140. QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  141. 0, -1, -1, q->irq_ptr->int_parm);
  142. return 0;
  143. }
  144. return count - tmp_count;
  145. }
  146. /**
  147. * qdio_do_sqbs - set buffer states for QEBSM
  148. * @q: queue to manipulate
  149. * @state: new state of the buffers
  150. * @start: first buffer number to change
  151. * @count: how many buffers to change
  152. *
  153. * Returns the number of successfully changed buffers.
  154. * Does retrying until the specified count of buffer states is set or an
  155. * error occurs.
  156. */
  157. static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
  158. int count)
  159. {
  160. unsigned int ccq = 0;
  161. int tmp_count = count, tmp_start = start;
  162. int nr = q->nr;
  163. int rc;
  164. if (!count)
  165. return 0;
  166. BUG_ON(!q->irq_ptr->sch_token);
  167. qdio_perf_stat_inc(&perf_stats.debug_sqbs_all);
  168. if (!q->is_input_q)
  169. nr += q->irq_ptr->nr_input_qs;
  170. again:
  171. ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
  172. rc = qdio_check_ccq(q, ccq);
  173. if (rc == 1) {
  174. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
  175. qdio_perf_stat_inc(&perf_stats.debug_sqbs_incomplete);
  176. goto again;
  177. }
  178. if (rc < 0) {
  179. DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
  180. DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
  181. q->handler(q->irq_ptr->cdev,
  182. QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  183. 0, -1, -1, q->irq_ptr->int_parm);
  184. return 0;
  185. }
  186. WARN_ON(tmp_count);
  187. return count - tmp_count;
  188. }
  189. /* returns number of examined buffers and their common state in *state */
  190. static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
  191. unsigned char *state, unsigned int count,
  192. int auto_ack)
  193. {
  194. unsigned char __state = 0;
  195. int i;
  196. BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
  197. BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
  198. if (is_qebsm(q))
  199. return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
  200. for (i = 0; i < count; i++) {
  201. if (!__state)
  202. __state = q->slsb.val[bufnr];
  203. else if (q->slsb.val[bufnr] != __state)
  204. break;
  205. bufnr = next_buf(bufnr);
  206. }
  207. *state = __state;
  208. return i;
  209. }
  210. inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
  211. unsigned char *state, int auto_ack)
  212. {
  213. return get_buf_states(q, bufnr, state, 1, auto_ack);
  214. }
  215. /* wrap-around safe setting of slsb states, returns number of changed buffers */
  216. static inline int set_buf_states(struct qdio_q *q, int bufnr,
  217. unsigned char state, int count)
  218. {
  219. int i;
  220. BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
  221. BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
  222. if (is_qebsm(q))
  223. return qdio_do_sqbs(q, state, bufnr, count);
  224. for (i = 0; i < count; i++) {
  225. xchg(&q->slsb.val[bufnr], state);
  226. bufnr = next_buf(bufnr);
  227. }
  228. return count;
  229. }
  230. static inline int set_buf_state(struct qdio_q *q, int bufnr,
  231. unsigned char state)
  232. {
  233. return set_buf_states(q, bufnr, state, 1);
  234. }
  235. /* set slsb states to initial state */
  236. void qdio_init_buf_states(struct qdio_irq *irq_ptr)
  237. {
  238. struct qdio_q *q;
  239. int i;
  240. for_each_input_queue(irq_ptr, q, i)
  241. set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
  242. QDIO_MAX_BUFFERS_PER_Q);
  243. for_each_output_queue(irq_ptr, q, i)
  244. set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
  245. QDIO_MAX_BUFFERS_PER_Q);
  246. }
  247. static int qdio_siga_sync(struct qdio_q *q, unsigned int output,
  248. unsigned int input)
  249. {
  250. int cc;
  251. if (!need_siga_sync(q))
  252. return 0;
  253. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
  254. qdio_perf_stat_inc(&perf_stats.siga_sync);
  255. cc = do_siga_sync(q->irq_ptr->schid, output, input);
  256. if (cc)
  257. DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
  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 int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
  276. {
  277. unsigned long schid;
  278. unsigned int fc = 0;
  279. u64 start_time = 0;
  280. int cc;
  281. if (q->u.out.use_enh_siga)
  282. fc = 3;
  283. if (is_qebsm(q)) {
  284. schid = q->irq_ptr->sch_token;
  285. fc |= 0x80;
  286. }
  287. else
  288. schid = *((u32 *)&q->irq_ptr->schid);
  289. again:
  290. cc = do_siga_output(schid, q->mask, busy_bit, fc);
  291. /* hipersocket busy condition */
  292. if (*busy_bit) {
  293. WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
  294. if (!start_time) {
  295. start_time = get_usecs();
  296. goto again;
  297. }
  298. if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
  299. goto again;
  300. }
  301. return cc;
  302. }
  303. static inline int qdio_siga_input(struct qdio_q *q)
  304. {
  305. int cc;
  306. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
  307. qdio_perf_stat_inc(&perf_stats.siga_in);
  308. cc = do_siga_input(q->irq_ptr->schid, q->mask);
  309. if (cc)
  310. DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
  311. return cc;
  312. }
  313. /* called from thinint inbound handler */
  314. void qdio_sync_after_thinint(struct qdio_q *q)
  315. {
  316. if (pci_out_supported(q)) {
  317. if (need_siga_sync_thinint(q))
  318. qdio_siga_sync_all(q);
  319. else if (need_siga_sync_out_thinint(q))
  320. qdio_siga_sync_out(q);
  321. } else
  322. qdio_siga_sync_q(q);
  323. }
  324. inline void qdio_stop_polling(struct qdio_q *q)
  325. {
  326. if (!q->u.in.polling)
  327. return;
  328. q->u.in.polling = 0;
  329. qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
  330. /* show the card that we are not polling anymore */
  331. if (is_qebsm(q)) {
  332. set_buf_states(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT,
  333. q->u.in.ack_count);
  334. q->u.in.ack_count = 0;
  335. } else
  336. set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
  337. }
  338. static void announce_buffer_error(struct qdio_q *q, int count)
  339. {
  340. q->qdio_error |= QDIO_ERROR_SLSB_STATE;
  341. /* special handling for no target buffer empty */
  342. if ((!q->is_input_q &&
  343. (q->sbal[q->first_to_check]->element[15].flags & 0xff) == 0x10)) {
  344. qdio_perf_stat_inc(&perf_stats.outbound_target_full);
  345. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%3d",
  346. q->first_to_check);
  347. return;
  348. }
  349. DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
  350. DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
  351. DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
  352. DBF_ERROR("F14:%2x F15:%2x",
  353. q->sbal[q->first_to_check]->element[14].flags & 0xff,
  354. q->sbal[q->first_to_check]->element[15].flags & 0xff);
  355. }
  356. static inline void inbound_primed(struct qdio_q *q, int count)
  357. {
  358. int new;
  359. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %3d", count);
  360. /* for QEBSM the ACK was already set by EQBS */
  361. if (is_qebsm(q)) {
  362. if (!q->u.in.polling) {
  363. q->u.in.polling = 1;
  364. q->u.in.ack_count = count;
  365. q->last_move_ftc = q->first_to_check;
  366. return;
  367. }
  368. /* delete the previous ACK's */
  369. set_buf_states(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT,
  370. q->u.in.ack_count);
  371. q->u.in.ack_count = count;
  372. q->last_move_ftc = q->first_to_check;
  373. return;
  374. }
  375. /*
  376. * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
  377. * or by the next inbound run.
  378. */
  379. new = add_buf(q->first_to_check, count - 1);
  380. if (q->u.in.polling) {
  381. /* reset the previous ACK but first set the new one */
  382. set_buf_state(q, new, SLSB_P_INPUT_ACK);
  383. set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
  384. } else {
  385. q->u.in.polling = 1;
  386. set_buf_state(q, new, SLSB_P_INPUT_ACK);
  387. }
  388. /*
  389. * last_move_ftc points to the ACK'ed buffer and not to the last turns
  390. * first_to_check like for qebsm. Since it is only used to check if
  391. * the queue front moved in qdio_inbound_q_done this is not a problem.
  392. */
  393. q->last_move_ftc = new;
  394. count--;
  395. if (!count)
  396. return;
  397. /*
  398. * Need to change all PRIMED buffers to NOT_INIT, otherwise
  399. * we're loosing initiative in the thinint code.
  400. */
  401. set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT,
  402. count);
  403. }
  404. static int get_inbound_buffer_frontier(struct qdio_q *q)
  405. {
  406. int count, stop;
  407. unsigned char state;
  408. /*
  409. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  410. * would return 0.
  411. */
  412. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  413. stop = add_buf(q->first_to_check, count);
  414. /*
  415. * No siga sync here, as a PCI or we after a thin interrupt
  416. * will sync the queues.
  417. */
  418. /* need to set count to 1 for non-qebsm */
  419. if (!is_qebsm(q))
  420. count = 1;
  421. check_next:
  422. if (q->first_to_check == stop)
  423. goto out;
  424. count = get_buf_states(q, q->first_to_check, &state, count, 1);
  425. if (!count)
  426. goto out;
  427. switch (state) {
  428. case SLSB_P_INPUT_PRIMED:
  429. inbound_primed(q, count);
  430. /*
  431. * No siga-sync needed for non-qebsm here, as the inbound queue
  432. * will be synced on the next siga-r, resp.
  433. * tiqdio_is_inbound_q_done will do the siga-sync.
  434. */
  435. q->first_to_check = add_buf(q->first_to_check, count);
  436. atomic_sub(count, &q->nr_buf_used);
  437. goto check_next;
  438. case SLSB_P_INPUT_ERROR:
  439. announce_buffer_error(q, count);
  440. /* process the buffer, the upper layer will take care of it */
  441. q->first_to_check = add_buf(q->first_to_check, count);
  442. atomic_sub(count, &q->nr_buf_used);
  443. break;
  444. case SLSB_CU_INPUT_EMPTY:
  445. case SLSB_P_INPUT_NOT_INIT:
  446. case SLSB_P_INPUT_ACK:
  447. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
  448. break;
  449. default:
  450. BUG();
  451. }
  452. out:
  453. return q->first_to_check;
  454. }
  455. int qdio_inbound_q_moved(struct qdio_q *q)
  456. {
  457. int bufnr;
  458. bufnr = get_inbound_buffer_frontier(q);
  459. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  460. if (!need_siga_sync(q) && !pci_out_supported(q))
  461. q->u.in.timestamp = get_usecs();
  462. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in moved");
  463. return 1;
  464. } else
  465. return 0;
  466. }
  467. static int qdio_inbound_q_done(struct qdio_q *q)
  468. {
  469. unsigned char state = 0;
  470. if (!atomic_read(&q->nr_buf_used))
  471. return 1;
  472. /*
  473. * We need that one for synchronization with the adapter, as it
  474. * does a kind of PCI avoidance.
  475. */
  476. qdio_siga_sync_q(q);
  477. get_buf_state(q, q->first_to_check, &state, 0);
  478. if (state == SLSB_P_INPUT_PRIMED)
  479. /* we got something to do */
  480. return 0;
  481. /* on VM, we don't poll, so the q is always done here */
  482. if (need_siga_sync(q) || pci_out_supported(q))
  483. return 1;
  484. /*
  485. * At this point we know, that inbound first_to_check
  486. * has (probably) not moved (see qdio_inbound_processing).
  487. */
  488. if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
  489. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%3d",
  490. q->first_to_check);
  491. return 1;
  492. } else {
  493. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in notd:%3d",
  494. q->first_to_check);
  495. return 0;
  496. }
  497. }
  498. void qdio_kick_inbound_handler(struct qdio_q *q)
  499. {
  500. int count, start, end;
  501. qdio_perf_stat_inc(&perf_stats.inbound_handler);
  502. start = q->first_to_kick;
  503. end = q->first_to_check;
  504. if (end >= start)
  505. count = end - start;
  506. else
  507. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  508. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%3d c:%3d", start, count);
  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, 0);
  561. if (!count)
  562. return q->first_to_check;
  563. switch (state) {
  564. case SLSB_P_OUTPUT_EMPTY:
  565. /* the adapter got it */
  566. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %3d", q->nr, count);
  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, count);
  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. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
  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. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
  606. return 1;
  607. } else
  608. return 0;
  609. }
  610. static void qdio_kick_outbound_q(struct qdio_q *q)
  611. {
  612. unsigned int busy_bit;
  613. int cc;
  614. if (!need_siga_out(q))
  615. return;
  616. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
  617. qdio_perf_stat_inc(&perf_stats.siga_out);
  618. cc = qdio_siga_output(q, &busy_bit);
  619. switch (cc) {
  620. case 0:
  621. break;
  622. case 2:
  623. if (busy_bit) {
  624. DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
  625. q->qdio_error = cc | QDIO_ERROR_SIGA_BUSY;
  626. } else {
  627. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d",
  628. q->nr);
  629. q->qdio_error = cc;
  630. }
  631. break;
  632. case 1:
  633. case 3:
  634. DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
  635. q->qdio_error = cc;
  636. break;
  637. }
  638. }
  639. static void qdio_kick_outbound_handler(struct qdio_q *q)
  640. {
  641. int start, end, count;
  642. start = q->first_to_kick;
  643. end = q->last_move_ftc;
  644. if (end >= start)
  645. count = end - start;
  646. else
  647. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  648. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kickouth: %1d", q->nr);
  649. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "s:%3d c:%3d", start, count);
  650. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  651. return;
  652. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
  653. q->irq_ptr->int_parm);
  654. /* for the next time: */
  655. q->first_to_kick = q->last_move_ftc;
  656. q->qdio_error = 0;
  657. }
  658. static void __qdio_outbound_processing(struct qdio_q *q)
  659. {
  660. unsigned long flags;
  661. qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
  662. spin_lock_irqsave(&q->lock, flags);
  663. BUG_ON(atomic_read(&q->nr_buf_used) < 0);
  664. if (qdio_outbound_q_moved(q))
  665. qdio_kick_outbound_handler(q);
  666. spin_unlock_irqrestore(&q->lock, flags);
  667. if (queue_type(q) == QDIO_ZFCP_QFMT)
  668. if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
  669. goto sched;
  670. /* bail out for HiperSockets unicast queues */
  671. if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
  672. return;
  673. if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
  674. (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
  675. goto sched;
  676. if (q->u.out.pci_out_enabled)
  677. return;
  678. /*
  679. * Now we know that queue type is either qeth without pci enabled
  680. * or HiperSockets multicast. Make sure buffer switch from PRIMED to
  681. * EMPTY is noticed and outbound_handler is called after some time.
  682. */
  683. if (qdio_outbound_q_done(q))
  684. del_timer(&q->u.out.timer);
  685. else {
  686. if (!timer_pending(&q->u.out.timer)) {
  687. mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
  688. qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
  689. }
  690. }
  691. return;
  692. sched:
  693. if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  694. return;
  695. tasklet_schedule(&q->tasklet);
  696. }
  697. /* outbound tasklet */
  698. void qdio_outbound_processing(unsigned long data)
  699. {
  700. struct qdio_q *q = (struct qdio_q *)data;
  701. __qdio_outbound_processing(q);
  702. }
  703. void qdio_outbound_timer(unsigned long data)
  704. {
  705. struct qdio_q *q = (struct qdio_q *)data;
  706. if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  707. return;
  708. tasklet_schedule(&q->tasklet);
  709. }
  710. /* called from thinint inbound tasklet */
  711. void qdio_check_outbound_after_thinint(struct qdio_q *q)
  712. {
  713. struct qdio_q *out;
  714. int i;
  715. if (!pci_out_supported(q))
  716. return;
  717. for_each_output_queue(q->irq_ptr, out, i)
  718. if (!qdio_outbound_q_done(out))
  719. tasklet_schedule(&out->tasklet);
  720. }
  721. static inline void qdio_set_state(struct qdio_irq *irq_ptr,
  722. enum qdio_irq_states state)
  723. {
  724. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
  725. irq_ptr->state = state;
  726. mb();
  727. }
  728. static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
  729. {
  730. if (irb->esw.esw0.erw.cons) {
  731. DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
  732. DBF_ERROR_HEX(irb, 64);
  733. DBF_ERROR_HEX(irb->ecw, 64);
  734. }
  735. }
  736. /* PCI interrupt handler */
  737. static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
  738. {
  739. int i;
  740. struct qdio_q *q;
  741. if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  742. return;
  743. qdio_perf_stat_inc(&perf_stats.pci_int);
  744. for_each_input_queue(irq_ptr, q, i)
  745. tasklet_schedule(&q->tasklet);
  746. if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
  747. return;
  748. for_each_output_queue(irq_ptr, q, i) {
  749. if (qdio_outbound_q_done(q))
  750. continue;
  751. if (!siga_syncs_out_pci(q))
  752. qdio_siga_sync_q(q);
  753. tasklet_schedule(&q->tasklet);
  754. }
  755. }
  756. static void qdio_handle_activate_check(struct ccw_device *cdev,
  757. unsigned long intparm, int cstat, int dstat)
  758. {
  759. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  760. struct qdio_q *q;
  761. DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
  762. DBF_ERROR("intp :%lx", intparm);
  763. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  764. if (irq_ptr->nr_input_qs) {
  765. q = irq_ptr->input_qs[0];
  766. } else if (irq_ptr->nr_output_qs) {
  767. q = irq_ptr->output_qs[0];
  768. } else {
  769. dump_stack();
  770. goto no_handler;
  771. }
  772. q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  773. 0, -1, -1, irq_ptr->int_parm);
  774. no_handler:
  775. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  776. }
  777. static void qdio_call_shutdown(struct work_struct *work)
  778. {
  779. struct ccw_device_private *priv;
  780. struct ccw_device *cdev;
  781. priv = container_of(work, struct ccw_device_private, kick_work);
  782. cdev = priv->cdev;
  783. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  784. put_device(&cdev->dev);
  785. }
  786. static void qdio_int_error(struct ccw_device *cdev)
  787. {
  788. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  789. switch (irq_ptr->state) {
  790. case QDIO_IRQ_STATE_INACTIVE:
  791. case QDIO_IRQ_STATE_CLEANUP:
  792. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  793. break;
  794. case QDIO_IRQ_STATE_ESTABLISHED:
  795. case QDIO_IRQ_STATE_ACTIVE:
  796. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  797. if (get_device(&cdev->dev)) {
  798. /* Can't call shutdown from interrupt context. */
  799. PREPARE_WORK(&cdev->private->kick_work,
  800. qdio_call_shutdown);
  801. queue_work(ccw_device_work, &cdev->private->kick_work);
  802. }
  803. break;
  804. default:
  805. WARN_ON(1);
  806. }
  807. wake_up(&cdev->private->wait_q);
  808. }
  809. static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
  810. int dstat)
  811. {
  812. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  813. if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
  814. DBF_ERROR("EQ:ck con");
  815. goto error;
  816. }
  817. if (!(dstat & DEV_STAT_DEV_END)) {
  818. DBF_ERROR("EQ:no dev");
  819. goto error;
  820. }
  821. if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
  822. DBF_ERROR("EQ: bad io");
  823. goto error;
  824. }
  825. return 0;
  826. error:
  827. DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
  828. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  829. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  830. return 1;
  831. }
  832. static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
  833. int dstat)
  834. {
  835. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  836. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
  837. if (!qdio_establish_check_errors(cdev, cstat, dstat))
  838. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
  839. }
  840. /* qdio interrupt handler */
  841. void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
  842. struct irb *irb)
  843. {
  844. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  845. int cstat, dstat;
  846. qdio_perf_stat_inc(&perf_stats.qdio_int);
  847. if (!intparm || !irq_ptr) {
  848. DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
  849. return;
  850. }
  851. if (IS_ERR(irb)) {
  852. switch (PTR_ERR(irb)) {
  853. case -EIO:
  854. DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
  855. return;
  856. case -ETIMEDOUT:
  857. DBF_ERROR("%4x IO timeout", irq_ptr->schid.sch_no);
  858. qdio_int_error(cdev);
  859. return;
  860. default:
  861. WARN_ON(1);
  862. return;
  863. }
  864. }
  865. qdio_irq_check_sense(irq_ptr, irb);
  866. cstat = irb->scsw.cmd.cstat;
  867. dstat = irb->scsw.cmd.dstat;
  868. switch (irq_ptr->state) {
  869. case QDIO_IRQ_STATE_INACTIVE:
  870. qdio_establish_handle_irq(cdev, cstat, dstat);
  871. break;
  872. case QDIO_IRQ_STATE_CLEANUP:
  873. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  874. break;
  875. case QDIO_IRQ_STATE_ESTABLISHED:
  876. case QDIO_IRQ_STATE_ACTIVE:
  877. if (cstat & SCHN_STAT_PCI) {
  878. qdio_int_handler_pci(irq_ptr);
  879. /* no state change so no need to wake up wait_q */
  880. return;
  881. }
  882. if ((cstat & ~SCHN_STAT_PCI) || dstat) {
  883. qdio_handle_activate_check(cdev, intparm, cstat,
  884. dstat);
  885. break;
  886. }
  887. default:
  888. WARN_ON(1);
  889. }
  890. wake_up(&cdev->private->wait_q);
  891. }
  892. /**
  893. * qdio_get_ssqd_desc - get qdio subchannel description
  894. * @cdev: ccw device to get description for
  895. * @data: where to store the ssqd
  896. *
  897. * Returns 0 or an error code. The results of the chsc are stored in the
  898. * specified structure.
  899. */
  900. int qdio_get_ssqd_desc(struct ccw_device *cdev,
  901. struct qdio_ssqd_desc *data)
  902. {
  903. if (!cdev || !cdev->private)
  904. return -EINVAL;
  905. DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
  906. return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
  907. }
  908. EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
  909. /**
  910. * qdio_cleanup - shutdown queues and free data structures
  911. * @cdev: associated ccw device
  912. * @how: use halt or clear to shutdown
  913. *
  914. * This function calls qdio_shutdown() for @cdev with method @how.
  915. * and qdio_free(). The qdio_free() return value is ignored since
  916. * !irq_ptr is already checked.
  917. */
  918. int qdio_cleanup(struct ccw_device *cdev, int how)
  919. {
  920. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  921. int rc;
  922. if (!irq_ptr)
  923. return -ENODEV;
  924. rc = qdio_shutdown(cdev, how);
  925. qdio_free(cdev);
  926. return rc;
  927. }
  928. EXPORT_SYMBOL_GPL(qdio_cleanup);
  929. static void qdio_shutdown_queues(struct ccw_device *cdev)
  930. {
  931. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  932. struct qdio_q *q;
  933. int i;
  934. for_each_input_queue(irq_ptr, q, i)
  935. tasklet_kill(&q->tasklet);
  936. for_each_output_queue(irq_ptr, q, i) {
  937. del_timer(&q->u.out.timer);
  938. tasklet_kill(&q->tasklet);
  939. }
  940. }
  941. /**
  942. * qdio_shutdown - shut down a qdio subchannel
  943. * @cdev: associated ccw device
  944. * @how: use halt or clear to shutdown
  945. */
  946. int qdio_shutdown(struct ccw_device *cdev, int how)
  947. {
  948. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  949. int rc;
  950. unsigned long flags;
  951. if (!irq_ptr)
  952. return -ENODEV;
  953. BUG_ON(irqs_disabled());
  954. DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
  955. mutex_lock(&irq_ptr->setup_mutex);
  956. /*
  957. * Subchannel was already shot down. We cannot prevent being called
  958. * twice since cio may trigger a shutdown asynchronously.
  959. */
  960. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  961. mutex_unlock(&irq_ptr->setup_mutex);
  962. return 0;
  963. }
  964. /*
  965. * Indicate that the device is going down. Scheduling the queue
  966. * tasklets is forbidden from here on.
  967. */
  968. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  969. tiqdio_remove_input_queues(irq_ptr);
  970. qdio_shutdown_queues(cdev);
  971. qdio_shutdown_debug_entries(irq_ptr, cdev);
  972. /* cleanup subchannel */
  973. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  974. if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
  975. rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
  976. else
  977. /* default behaviour is halt */
  978. rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
  979. if (rc) {
  980. DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
  981. DBF_ERROR("rc:%4d", rc);
  982. goto no_cleanup;
  983. }
  984. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
  985. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  986. wait_event_interruptible_timeout(cdev->private->wait_q,
  987. irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
  988. irq_ptr->state == QDIO_IRQ_STATE_ERR,
  989. 10 * HZ);
  990. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  991. no_cleanup:
  992. qdio_shutdown_thinint(irq_ptr);
  993. /* restore interrupt handler */
  994. if ((void *)cdev->handler == (void *)qdio_int_handler)
  995. cdev->handler = irq_ptr->orig_handler;
  996. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  997. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  998. mutex_unlock(&irq_ptr->setup_mutex);
  999. if (rc)
  1000. return rc;
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL_GPL(qdio_shutdown);
  1004. /**
  1005. * qdio_free - free data structures for a qdio subchannel
  1006. * @cdev: associated ccw device
  1007. */
  1008. int qdio_free(struct ccw_device *cdev)
  1009. {
  1010. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  1011. if (!irq_ptr)
  1012. return -ENODEV;
  1013. DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
  1014. mutex_lock(&irq_ptr->setup_mutex);
  1015. if (irq_ptr->debug_area != NULL) {
  1016. debug_unregister(irq_ptr->debug_area);
  1017. irq_ptr->debug_area = NULL;
  1018. }
  1019. cdev->private->qdio_data = NULL;
  1020. mutex_unlock(&irq_ptr->setup_mutex);
  1021. qdio_release_memory(irq_ptr);
  1022. return 0;
  1023. }
  1024. EXPORT_SYMBOL_GPL(qdio_free);
  1025. /**
  1026. * qdio_initialize - allocate and establish queues for a qdio subchannel
  1027. * @init_data: initialization data
  1028. *
  1029. * This function first allocates queues via qdio_allocate() and on success
  1030. * establishes them via qdio_establish().
  1031. */
  1032. int qdio_initialize(struct qdio_initialize *init_data)
  1033. {
  1034. int rc;
  1035. rc = qdio_allocate(init_data);
  1036. if (rc)
  1037. return rc;
  1038. rc = qdio_establish(init_data);
  1039. if (rc)
  1040. qdio_free(init_data->cdev);
  1041. return rc;
  1042. }
  1043. EXPORT_SYMBOL_GPL(qdio_initialize);
  1044. /**
  1045. * qdio_allocate - allocate qdio queues and associated data
  1046. * @init_data: initialization data
  1047. */
  1048. int qdio_allocate(struct qdio_initialize *init_data)
  1049. {
  1050. struct qdio_irq *irq_ptr;
  1051. DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
  1052. if ((init_data->no_input_qs && !init_data->input_handler) ||
  1053. (init_data->no_output_qs && !init_data->output_handler))
  1054. return -EINVAL;
  1055. if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
  1056. (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
  1057. return -EINVAL;
  1058. if ((!init_data->input_sbal_addr_array) ||
  1059. (!init_data->output_sbal_addr_array))
  1060. return -EINVAL;
  1061. /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
  1062. irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1063. if (!irq_ptr)
  1064. goto out_err;
  1065. mutex_init(&irq_ptr->setup_mutex);
  1066. qdio_allocate_dbf(init_data, irq_ptr);
  1067. /*
  1068. * Allocate a page for the chsc calls in qdio_establish.
  1069. * Must be pre-allocated since a zfcp recovery will call
  1070. * qdio_establish. In case of low memory and swap on a zfcp disk
  1071. * we may not be able to allocate memory otherwise.
  1072. */
  1073. irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
  1074. if (!irq_ptr->chsc_page)
  1075. goto out_rel;
  1076. /* qdr is used in ccw1.cda which is u32 */
  1077. irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1078. if (!irq_ptr->qdr)
  1079. goto out_rel;
  1080. WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
  1081. if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
  1082. init_data->no_output_qs))
  1083. goto out_rel;
  1084. init_data->cdev->private->qdio_data = irq_ptr;
  1085. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1086. return 0;
  1087. out_rel:
  1088. qdio_release_memory(irq_ptr);
  1089. out_err:
  1090. return -ENOMEM;
  1091. }
  1092. EXPORT_SYMBOL_GPL(qdio_allocate);
  1093. /**
  1094. * qdio_establish - establish queues on a qdio subchannel
  1095. * @init_data: initialization data
  1096. */
  1097. int qdio_establish(struct qdio_initialize *init_data)
  1098. {
  1099. struct qdio_irq *irq_ptr;
  1100. struct ccw_device *cdev = init_data->cdev;
  1101. unsigned long saveflags;
  1102. int rc;
  1103. DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
  1104. irq_ptr = cdev->private->qdio_data;
  1105. if (!irq_ptr)
  1106. return -ENODEV;
  1107. if (cdev->private->state != DEV_STATE_ONLINE)
  1108. return -EINVAL;
  1109. mutex_lock(&irq_ptr->setup_mutex);
  1110. qdio_setup_irq(init_data);
  1111. rc = qdio_establish_thinint(irq_ptr);
  1112. if (rc) {
  1113. mutex_unlock(&irq_ptr->setup_mutex);
  1114. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1115. return rc;
  1116. }
  1117. /* establish q */
  1118. irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
  1119. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1120. irq_ptr->ccw.count = irq_ptr->equeue.count;
  1121. irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
  1122. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1123. ccw_device_set_options_mask(cdev, 0);
  1124. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
  1125. if (rc) {
  1126. DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
  1127. DBF_ERROR("rc:%4x", rc);
  1128. }
  1129. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1130. if (rc) {
  1131. mutex_unlock(&irq_ptr->setup_mutex);
  1132. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1133. return rc;
  1134. }
  1135. wait_event_interruptible_timeout(cdev->private->wait_q,
  1136. irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
  1137. irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
  1138. if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
  1139. mutex_unlock(&irq_ptr->setup_mutex);
  1140. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1141. return -EIO;
  1142. }
  1143. qdio_setup_ssqd_info(irq_ptr);
  1144. DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
  1145. DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
  1146. /* qebsm is now setup if available, initialize buffer states */
  1147. qdio_init_buf_states(irq_ptr);
  1148. mutex_unlock(&irq_ptr->setup_mutex);
  1149. qdio_print_subchannel_info(irq_ptr, cdev);
  1150. qdio_setup_debug_entries(irq_ptr, cdev);
  1151. return 0;
  1152. }
  1153. EXPORT_SYMBOL_GPL(qdio_establish);
  1154. /**
  1155. * qdio_activate - activate queues on a qdio subchannel
  1156. * @cdev: associated cdev
  1157. */
  1158. int qdio_activate(struct ccw_device *cdev)
  1159. {
  1160. struct qdio_irq *irq_ptr;
  1161. int rc;
  1162. unsigned long saveflags;
  1163. DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
  1164. irq_ptr = cdev->private->qdio_data;
  1165. if (!irq_ptr)
  1166. return -ENODEV;
  1167. if (cdev->private->state != DEV_STATE_ONLINE)
  1168. return -EINVAL;
  1169. mutex_lock(&irq_ptr->setup_mutex);
  1170. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1171. rc = -EBUSY;
  1172. goto out;
  1173. }
  1174. irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
  1175. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1176. irq_ptr->ccw.count = irq_ptr->aqueue.count;
  1177. irq_ptr->ccw.cda = 0;
  1178. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1179. ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
  1180. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
  1181. 0, DOIO_DENY_PREFETCH);
  1182. if (rc) {
  1183. DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
  1184. DBF_ERROR("rc:%4x", rc);
  1185. }
  1186. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1187. if (rc)
  1188. goto out;
  1189. if (is_thinint_irq(irq_ptr))
  1190. tiqdio_add_input_queues(irq_ptr);
  1191. /* wait for subchannel to become active */
  1192. msleep(5);
  1193. switch (irq_ptr->state) {
  1194. case QDIO_IRQ_STATE_STOPPED:
  1195. case QDIO_IRQ_STATE_ERR:
  1196. rc = -EIO;
  1197. break;
  1198. default:
  1199. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
  1200. rc = 0;
  1201. }
  1202. out:
  1203. mutex_unlock(&irq_ptr->setup_mutex);
  1204. return rc;
  1205. }
  1206. EXPORT_SYMBOL_GPL(qdio_activate);
  1207. static inline int buf_in_between(int bufnr, int start, int count)
  1208. {
  1209. int end = add_buf(start, count);
  1210. if (end > start) {
  1211. if (bufnr >= start && bufnr < end)
  1212. return 1;
  1213. else
  1214. return 0;
  1215. }
  1216. /* wrap-around case */
  1217. if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
  1218. (bufnr < end))
  1219. return 1;
  1220. else
  1221. return 0;
  1222. }
  1223. /**
  1224. * handle_inbound - reset processed input buffers
  1225. * @q: queue containing the buffers
  1226. * @callflags: flags
  1227. * @bufnr: first buffer to process
  1228. * @count: how many buffers are emptied
  1229. */
  1230. static void handle_inbound(struct qdio_q *q, unsigned int callflags,
  1231. int bufnr, int count)
  1232. {
  1233. int used, cc, diff;
  1234. if (!q->u.in.polling)
  1235. goto set;
  1236. /* protect against stop polling setting an ACK for an emptied slsb */
  1237. if (count == QDIO_MAX_BUFFERS_PER_Q) {
  1238. /* overwriting everything, just delete polling status */
  1239. q->u.in.polling = 0;
  1240. q->u.in.ack_count = 0;
  1241. goto set;
  1242. } else if (buf_in_between(q->last_move_ftc, bufnr, count)) {
  1243. if (is_qebsm(q)) {
  1244. /* partial overwrite, just update last_move_ftc */
  1245. diff = add_buf(bufnr, count);
  1246. diff = sub_buf(diff, q->last_move_ftc);
  1247. q->u.in.ack_count -= diff;
  1248. if (q->u.in.ack_count <= 0) {
  1249. q->u.in.polling = 0;
  1250. q->u.in.ack_count = 0;
  1251. goto set;
  1252. }
  1253. q->last_move_ftc = add_buf(q->last_move_ftc, diff);
  1254. }
  1255. else
  1256. /* the only ACK will be deleted, so stop polling */
  1257. q->u.in.polling = 0;
  1258. }
  1259. set:
  1260. count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
  1261. used = atomic_add_return(count, &q->nr_buf_used) - count;
  1262. BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
  1263. /* no need to signal as long as the adapter had free buffers */
  1264. if (used)
  1265. return;
  1266. if (need_siga_in(q)) {
  1267. cc = qdio_siga_input(q);
  1268. if (cc)
  1269. q->qdio_error = cc;
  1270. }
  1271. }
  1272. /**
  1273. * handle_outbound - process filled outbound buffers
  1274. * @q: queue containing the buffers
  1275. * @callflags: flags
  1276. * @bufnr: first buffer to process
  1277. * @count: how many buffers are filled
  1278. */
  1279. static void handle_outbound(struct qdio_q *q, unsigned int callflags,
  1280. int bufnr, int count)
  1281. {
  1282. unsigned char state;
  1283. int used;
  1284. qdio_perf_stat_inc(&perf_stats.outbound_handler);
  1285. count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
  1286. used = atomic_add_return(count, &q->nr_buf_used);
  1287. BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
  1288. if (callflags & QDIO_FLAG_PCI_OUT)
  1289. q->u.out.pci_out_enabled = 1;
  1290. else
  1291. q->u.out.pci_out_enabled = 0;
  1292. if (queue_type(q) == QDIO_IQDIO_QFMT) {
  1293. if (multicast_outbound(q))
  1294. qdio_kick_outbound_q(q);
  1295. else
  1296. if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
  1297. (count > 1) &&
  1298. (count <= q->irq_ptr->ssqd_desc.mmwc)) {
  1299. /* exploit enhanced SIGA */
  1300. q->u.out.use_enh_siga = 1;
  1301. qdio_kick_outbound_q(q);
  1302. } else {
  1303. /*
  1304. * One siga-w per buffer required for unicast
  1305. * HiperSockets.
  1306. */
  1307. q->u.out.use_enh_siga = 0;
  1308. while (count--)
  1309. qdio_kick_outbound_q(q);
  1310. }
  1311. /* report CC=2 conditions synchronously */
  1312. if (q->qdio_error)
  1313. __qdio_outbound_processing(q);
  1314. goto out;
  1315. }
  1316. if (need_siga_sync(q)) {
  1317. qdio_siga_sync_q(q);
  1318. goto out;
  1319. }
  1320. /* try to fast requeue buffers */
  1321. get_buf_state(q, prev_buf(bufnr), &state, 0);
  1322. if (state != SLSB_CU_OUTPUT_PRIMED)
  1323. qdio_kick_outbound_q(q);
  1324. else {
  1325. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "fast-req");
  1326. qdio_perf_stat_inc(&perf_stats.fast_requeue);
  1327. }
  1328. out:
  1329. tasklet_schedule(&q->tasklet);
  1330. }
  1331. /**
  1332. * do_QDIO - process input or output buffers
  1333. * @cdev: associated ccw_device for the qdio subchannel
  1334. * @callflags: input or output and special flags from the program
  1335. * @q_nr: queue number
  1336. * @bufnr: buffer number
  1337. * @count: how many buffers to process
  1338. */
  1339. int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
  1340. int q_nr, int bufnr, int count)
  1341. {
  1342. struct qdio_irq *irq_ptr;
  1343. if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
  1344. (count > QDIO_MAX_BUFFERS_PER_Q) ||
  1345. (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
  1346. return -EINVAL;
  1347. if (!count)
  1348. return 0;
  1349. irq_ptr = cdev->private->qdio_data;
  1350. if (!irq_ptr)
  1351. return -ENODEV;
  1352. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1353. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO input");
  1354. else
  1355. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO output");
  1356. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "q:%1d flag:%4x", q_nr, callflags);
  1357. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "buf:%2d cnt:%3d", bufnr, count);
  1358. if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
  1359. return -EBUSY;
  1360. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1361. handle_inbound(irq_ptr->input_qs[q_nr], callflags, bufnr,
  1362. count);
  1363. else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
  1364. handle_outbound(irq_ptr->output_qs[q_nr], callflags, bufnr,
  1365. count);
  1366. else
  1367. return -EINVAL;
  1368. return 0;
  1369. }
  1370. EXPORT_SYMBOL_GPL(do_QDIO);
  1371. static int __init init_QDIO(void)
  1372. {
  1373. int rc;
  1374. rc = qdio_setup_init();
  1375. if (rc)
  1376. return rc;
  1377. rc = tiqdio_allocate_memory();
  1378. if (rc)
  1379. goto out_cache;
  1380. rc = qdio_debug_init();
  1381. if (rc)
  1382. goto out_ti;
  1383. rc = qdio_setup_perf_stats();
  1384. if (rc)
  1385. goto out_debug;
  1386. rc = tiqdio_register_thinints();
  1387. if (rc)
  1388. goto out_perf;
  1389. return 0;
  1390. out_perf:
  1391. qdio_remove_perf_stats();
  1392. out_debug:
  1393. qdio_debug_exit();
  1394. out_ti:
  1395. tiqdio_free_memory();
  1396. out_cache:
  1397. qdio_setup_exit();
  1398. return rc;
  1399. }
  1400. static void __exit exit_QDIO(void)
  1401. {
  1402. tiqdio_unregister_thinints();
  1403. tiqdio_free_memory();
  1404. qdio_remove_perf_stats();
  1405. qdio_debug_exit();
  1406. qdio_setup_exit();
  1407. }
  1408. module_init(init_QDIO);
  1409. module_exit(exit_QDIO);