qdio_main.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. }
  385. else {
  386. q->u.in.polling = 1;
  387. set_buf_state(q, q->first_to_check, SLSB_P_INPUT_ACK);
  388. }
  389. q->last_move_ftc = new;
  390. count--;
  391. if (!count)
  392. return;
  393. /*
  394. * Need to change all PRIMED buffers to NOT_INIT, otherwise
  395. * we're loosing initiative in the thinint code.
  396. */
  397. set_buf_states(q, next_buf(q->first_to_check), SLSB_P_INPUT_NOT_INIT,
  398. count);
  399. }
  400. static int get_inbound_buffer_frontier(struct qdio_q *q)
  401. {
  402. int count, stop;
  403. unsigned char state;
  404. /*
  405. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  406. * would return 0.
  407. */
  408. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  409. stop = add_buf(q->first_to_check, count);
  410. /*
  411. * No siga sync here, as a PCI or we after a thin interrupt
  412. * will sync the queues.
  413. */
  414. /* need to set count to 1 for non-qebsm */
  415. if (!is_qebsm(q))
  416. count = 1;
  417. check_next:
  418. if (q->first_to_check == stop)
  419. goto out;
  420. count = get_buf_states(q, q->first_to_check, &state, count, 1);
  421. if (!count)
  422. goto out;
  423. switch (state) {
  424. case SLSB_P_INPUT_PRIMED:
  425. inbound_primed(q, count);
  426. /*
  427. * No siga-sync needed for non-qebsm here, as the inbound queue
  428. * will be synced on the next siga-r, resp.
  429. * tiqdio_is_inbound_q_done will do the siga-sync.
  430. */
  431. q->first_to_check = add_buf(q->first_to_check, count);
  432. atomic_sub(count, &q->nr_buf_used);
  433. goto check_next;
  434. case SLSB_P_INPUT_ERROR:
  435. announce_buffer_error(q, count);
  436. /* process the buffer, the upper layer will take care of it */
  437. q->first_to_check = add_buf(q->first_to_check, count);
  438. atomic_sub(count, &q->nr_buf_used);
  439. break;
  440. case SLSB_CU_INPUT_EMPTY:
  441. case SLSB_P_INPUT_NOT_INIT:
  442. case SLSB_P_INPUT_ACK:
  443. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
  444. break;
  445. default:
  446. BUG();
  447. }
  448. out:
  449. return q->first_to_check;
  450. }
  451. int qdio_inbound_q_moved(struct qdio_q *q)
  452. {
  453. int bufnr;
  454. bufnr = get_inbound_buffer_frontier(q);
  455. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  456. if (!need_siga_sync(q) && !pci_out_supported(q))
  457. q->u.in.timestamp = get_usecs();
  458. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in moved");
  459. return 1;
  460. } else
  461. return 0;
  462. }
  463. static int qdio_inbound_q_done(struct qdio_q *q)
  464. {
  465. unsigned char state = 0;
  466. if (!atomic_read(&q->nr_buf_used))
  467. return 1;
  468. /*
  469. * We need that one for synchronization with the adapter, as it
  470. * does a kind of PCI avoidance.
  471. */
  472. qdio_siga_sync_q(q);
  473. get_buf_state(q, q->first_to_check, &state, 0);
  474. if (state == SLSB_P_INPUT_PRIMED)
  475. /* we got something to do */
  476. return 0;
  477. /* on VM, we don't poll, so the q is always done here */
  478. if (need_siga_sync(q) || pci_out_supported(q))
  479. return 1;
  480. /*
  481. * At this point we know, that inbound first_to_check
  482. * has (probably) not moved (see qdio_inbound_processing).
  483. */
  484. if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
  485. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%3d",
  486. q->first_to_check);
  487. return 1;
  488. } else {
  489. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in notd:%3d",
  490. q->first_to_check);
  491. return 0;
  492. }
  493. }
  494. void qdio_kick_inbound_handler(struct qdio_q *q)
  495. {
  496. int count, start, end;
  497. qdio_perf_stat_inc(&perf_stats.inbound_handler);
  498. start = q->first_to_kick;
  499. end = q->first_to_check;
  500. if (end >= start)
  501. count = end - start;
  502. else
  503. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  504. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%3d c:%3d", start, count);
  505. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  506. return;
  507. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
  508. start, count, q->irq_ptr->int_parm);
  509. /* for the next time */
  510. q->first_to_kick = q->first_to_check;
  511. q->qdio_error = 0;
  512. }
  513. static void __qdio_inbound_processing(struct qdio_q *q)
  514. {
  515. qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
  516. again:
  517. if (!qdio_inbound_q_moved(q))
  518. return;
  519. qdio_kick_inbound_handler(q);
  520. if (!qdio_inbound_q_done(q))
  521. /* means poll time is not yet over */
  522. goto again;
  523. qdio_stop_polling(q);
  524. /*
  525. * We need to check again to not lose initiative after
  526. * resetting the ACK state.
  527. */
  528. if (!qdio_inbound_q_done(q))
  529. goto again;
  530. }
  531. /* inbound tasklet */
  532. void qdio_inbound_processing(unsigned long data)
  533. {
  534. struct qdio_q *q = (struct qdio_q *)data;
  535. __qdio_inbound_processing(q);
  536. }
  537. static int get_outbound_buffer_frontier(struct qdio_q *q)
  538. {
  539. int count, stop;
  540. unsigned char state;
  541. if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
  542. (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
  543. qdio_siga_sync_q(q);
  544. /*
  545. * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
  546. * would return 0.
  547. */
  548. count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
  549. stop = add_buf(q->first_to_check, count);
  550. /* need to set count to 1 for non-qebsm */
  551. if (!is_qebsm(q))
  552. count = 1;
  553. check_next:
  554. if (q->first_to_check == stop)
  555. return q->first_to_check;
  556. count = get_buf_states(q, q->first_to_check, &state, count, 0);
  557. if (!count)
  558. return q->first_to_check;
  559. switch (state) {
  560. case SLSB_P_OUTPUT_EMPTY:
  561. /* the adapter got it */
  562. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %3d", q->nr, count);
  563. atomic_sub(count, &q->nr_buf_used);
  564. q->first_to_check = add_buf(q->first_to_check, count);
  565. /*
  566. * We fetch all buffer states at once. get_buf_states may
  567. * return count < stop. For QEBSM we do not loop.
  568. */
  569. if (is_qebsm(q))
  570. break;
  571. goto check_next;
  572. case SLSB_P_OUTPUT_ERROR:
  573. announce_buffer_error(q, count);
  574. /* process the buffer, the upper layer will take care of it */
  575. q->first_to_check = add_buf(q->first_to_check, count);
  576. atomic_sub(count, &q->nr_buf_used);
  577. break;
  578. case SLSB_CU_OUTPUT_PRIMED:
  579. /* the adapter has not fetched the output yet */
  580. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
  581. break;
  582. case SLSB_P_OUTPUT_NOT_INIT:
  583. case SLSB_P_OUTPUT_HALTED:
  584. break;
  585. default:
  586. BUG();
  587. }
  588. return q->first_to_check;
  589. }
  590. /* all buffers processed? */
  591. static inline int qdio_outbound_q_done(struct qdio_q *q)
  592. {
  593. return atomic_read(&q->nr_buf_used) == 0;
  594. }
  595. static inline int qdio_outbound_q_moved(struct qdio_q *q)
  596. {
  597. int bufnr;
  598. bufnr = get_outbound_buffer_frontier(q);
  599. if ((bufnr != q->last_move_ftc) || q->qdio_error) {
  600. q->last_move_ftc = bufnr;
  601. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
  602. return 1;
  603. } else
  604. return 0;
  605. }
  606. static void qdio_kick_outbound_q(struct qdio_q *q)
  607. {
  608. unsigned int busy_bit;
  609. int cc;
  610. if (!need_siga_out(q))
  611. return;
  612. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
  613. qdio_perf_stat_inc(&perf_stats.siga_out);
  614. cc = qdio_siga_output(q, &busy_bit);
  615. switch (cc) {
  616. case 0:
  617. break;
  618. case 2:
  619. if (busy_bit) {
  620. DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
  621. q->qdio_error = cc | QDIO_ERROR_SIGA_BUSY;
  622. } else {
  623. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d",
  624. q->nr);
  625. q->qdio_error = cc;
  626. }
  627. break;
  628. case 1:
  629. case 3:
  630. DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
  631. q->qdio_error = cc;
  632. break;
  633. }
  634. }
  635. static void qdio_kick_outbound_handler(struct qdio_q *q)
  636. {
  637. int start, end, count;
  638. start = q->first_to_kick;
  639. end = q->last_move_ftc;
  640. if (end >= start)
  641. count = end - start;
  642. else
  643. count = end + QDIO_MAX_BUFFERS_PER_Q - start;
  644. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kickouth: %1d", q->nr);
  645. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "s:%3d c:%3d", start, count);
  646. if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
  647. return;
  648. q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
  649. q->irq_ptr->int_parm);
  650. /* for the next time: */
  651. q->first_to_kick = q->last_move_ftc;
  652. q->qdio_error = 0;
  653. }
  654. static void __qdio_outbound_processing(struct qdio_q *q)
  655. {
  656. unsigned long flags;
  657. qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
  658. spin_lock_irqsave(&q->lock, flags);
  659. BUG_ON(atomic_read(&q->nr_buf_used) < 0);
  660. if (qdio_outbound_q_moved(q))
  661. qdio_kick_outbound_handler(q);
  662. spin_unlock_irqrestore(&q->lock, flags);
  663. if (queue_type(q) == QDIO_ZFCP_QFMT)
  664. if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
  665. goto sched;
  666. /* bail out for HiperSockets unicast queues */
  667. if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
  668. return;
  669. if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
  670. (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
  671. goto sched;
  672. if (q->u.out.pci_out_enabled)
  673. return;
  674. /*
  675. * Now we know that queue type is either qeth without pci enabled
  676. * or HiperSockets multicast. Make sure buffer switch from PRIMED to
  677. * EMPTY is noticed and outbound_handler is called after some time.
  678. */
  679. if (qdio_outbound_q_done(q))
  680. del_timer(&q->u.out.timer);
  681. else {
  682. if (!timer_pending(&q->u.out.timer)) {
  683. mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
  684. qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
  685. }
  686. }
  687. return;
  688. sched:
  689. if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  690. return;
  691. tasklet_schedule(&q->tasklet);
  692. }
  693. /* outbound tasklet */
  694. void qdio_outbound_processing(unsigned long data)
  695. {
  696. struct qdio_q *q = (struct qdio_q *)data;
  697. __qdio_outbound_processing(q);
  698. }
  699. void qdio_outbound_timer(unsigned long data)
  700. {
  701. struct qdio_q *q = (struct qdio_q *)data;
  702. if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  703. return;
  704. tasklet_schedule(&q->tasklet);
  705. }
  706. /* called from thinint inbound tasklet */
  707. void qdio_check_outbound_after_thinint(struct qdio_q *q)
  708. {
  709. struct qdio_q *out;
  710. int i;
  711. if (!pci_out_supported(q))
  712. return;
  713. for_each_output_queue(q->irq_ptr, out, i)
  714. if (!qdio_outbound_q_done(out))
  715. tasklet_schedule(&out->tasklet);
  716. }
  717. static inline void qdio_set_state(struct qdio_irq *irq_ptr,
  718. enum qdio_irq_states state)
  719. {
  720. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
  721. irq_ptr->state = state;
  722. mb();
  723. }
  724. static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
  725. {
  726. if (irb->esw.esw0.erw.cons) {
  727. DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
  728. DBF_ERROR_HEX(irb, 64);
  729. DBF_ERROR_HEX(irb->ecw, 64);
  730. }
  731. }
  732. /* PCI interrupt handler */
  733. static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
  734. {
  735. int i;
  736. struct qdio_q *q;
  737. if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
  738. return;
  739. qdio_perf_stat_inc(&perf_stats.pci_int);
  740. for_each_input_queue(irq_ptr, q, i)
  741. tasklet_schedule(&q->tasklet);
  742. if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
  743. return;
  744. for_each_output_queue(irq_ptr, q, i) {
  745. if (qdio_outbound_q_done(q))
  746. continue;
  747. if (!siga_syncs_out_pci(q))
  748. qdio_siga_sync_q(q);
  749. tasklet_schedule(&q->tasklet);
  750. }
  751. }
  752. static void qdio_handle_activate_check(struct ccw_device *cdev,
  753. unsigned long intparm, int cstat, int dstat)
  754. {
  755. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  756. struct qdio_q *q;
  757. DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
  758. DBF_ERROR("intp :%lx", intparm);
  759. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  760. if (irq_ptr->nr_input_qs) {
  761. q = irq_ptr->input_qs[0];
  762. } else if (irq_ptr->nr_output_qs) {
  763. q = irq_ptr->output_qs[0];
  764. } else {
  765. dump_stack();
  766. goto no_handler;
  767. }
  768. q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  769. 0, -1, -1, irq_ptr->int_parm);
  770. no_handler:
  771. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  772. }
  773. static void qdio_call_shutdown(struct work_struct *work)
  774. {
  775. struct ccw_device_private *priv;
  776. struct ccw_device *cdev;
  777. priv = container_of(work, struct ccw_device_private, kick_work);
  778. cdev = priv->cdev;
  779. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  780. put_device(&cdev->dev);
  781. }
  782. static void qdio_int_error(struct ccw_device *cdev)
  783. {
  784. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  785. switch (irq_ptr->state) {
  786. case QDIO_IRQ_STATE_INACTIVE:
  787. case QDIO_IRQ_STATE_CLEANUP:
  788. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  789. break;
  790. case QDIO_IRQ_STATE_ESTABLISHED:
  791. case QDIO_IRQ_STATE_ACTIVE:
  792. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  793. if (get_device(&cdev->dev)) {
  794. /* Can't call shutdown from interrupt context. */
  795. PREPARE_WORK(&cdev->private->kick_work,
  796. qdio_call_shutdown);
  797. queue_work(ccw_device_work, &cdev->private->kick_work);
  798. }
  799. break;
  800. default:
  801. WARN_ON(1);
  802. }
  803. wake_up(&cdev->private->wait_q);
  804. }
  805. static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
  806. int dstat)
  807. {
  808. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  809. if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
  810. DBF_ERROR("EQ:ck con");
  811. goto error;
  812. }
  813. if (!(dstat & DEV_STAT_DEV_END)) {
  814. DBF_ERROR("EQ:no dev");
  815. goto error;
  816. }
  817. if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
  818. DBF_ERROR("EQ: bad io");
  819. goto error;
  820. }
  821. return 0;
  822. error:
  823. DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
  824. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  825. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  826. return 1;
  827. }
  828. static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
  829. int dstat)
  830. {
  831. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  832. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
  833. if (!qdio_establish_check_errors(cdev, cstat, dstat))
  834. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
  835. }
  836. /* qdio interrupt handler */
  837. void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
  838. struct irb *irb)
  839. {
  840. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  841. int cstat, dstat;
  842. qdio_perf_stat_inc(&perf_stats.qdio_int);
  843. if (!intparm || !irq_ptr) {
  844. DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
  845. return;
  846. }
  847. if (IS_ERR(irb)) {
  848. switch (PTR_ERR(irb)) {
  849. case -EIO:
  850. DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
  851. return;
  852. case -ETIMEDOUT:
  853. DBF_ERROR("%4x IO timeout", irq_ptr->schid.sch_no);
  854. qdio_int_error(cdev);
  855. return;
  856. default:
  857. WARN_ON(1);
  858. return;
  859. }
  860. }
  861. qdio_irq_check_sense(irq_ptr, irb);
  862. cstat = irb->scsw.cmd.cstat;
  863. dstat = irb->scsw.cmd.dstat;
  864. switch (irq_ptr->state) {
  865. case QDIO_IRQ_STATE_INACTIVE:
  866. qdio_establish_handle_irq(cdev, cstat, dstat);
  867. break;
  868. case QDIO_IRQ_STATE_CLEANUP:
  869. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  870. break;
  871. case QDIO_IRQ_STATE_ESTABLISHED:
  872. case QDIO_IRQ_STATE_ACTIVE:
  873. if (cstat & SCHN_STAT_PCI) {
  874. qdio_int_handler_pci(irq_ptr);
  875. /* no state change so no need to wake up wait_q */
  876. return;
  877. }
  878. if ((cstat & ~SCHN_STAT_PCI) || dstat) {
  879. qdio_handle_activate_check(cdev, intparm, cstat,
  880. dstat);
  881. break;
  882. }
  883. default:
  884. WARN_ON(1);
  885. }
  886. wake_up(&cdev->private->wait_q);
  887. }
  888. /**
  889. * qdio_get_ssqd_desc - get qdio subchannel description
  890. * @cdev: ccw device to get description for
  891. * @data: where to store the ssqd
  892. *
  893. * Returns 0 or an error code. The results of the chsc are stored in the
  894. * specified structure.
  895. */
  896. int qdio_get_ssqd_desc(struct ccw_device *cdev,
  897. struct qdio_ssqd_desc *data)
  898. {
  899. if (!cdev || !cdev->private)
  900. return -EINVAL;
  901. DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
  902. return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
  903. }
  904. EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
  905. /**
  906. * qdio_cleanup - shutdown queues and free data structures
  907. * @cdev: associated ccw device
  908. * @how: use halt or clear to shutdown
  909. *
  910. * This function calls qdio_shutdown() for @cdev with method @how.
  911. * and qdio_free(). The qdio_free() return value is ignored since
  912. * !irq_ptr is already checked.
  913. */
  914. int qdio_cleanup(struct ccw_device *cdev, int how)
  915. {
  916. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  917. int rc;
  918. if (!irq_ptr)
  919. return -ENODEV;
  920. rc = qdio_shutdown(cdev, how);
  921. qdio_free(cdev);
  922. return rc;
  923. }
  924. EXPORT_SYMBOL_GPL(qdio_cleanup);
  925. static void qdio_shutdown_queues(struct ccw_device *cdev)
  926. {
  927. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  928. struct qdio_q *q;
  929. int i;
  930. for_each_input_queue(irq_ptr, q, i)
  931. tasklet_kill(&q->tasklet);
  932. for_each_output_queue(irq_ptr, q, i) {
  933. del_timer(&q->u.out.timer);
  934. tasklet_kill(&q->tasklet);
  935. }
  936. }
  937. /**
  938. * qdio_shutdown - shut down a qdio subchannel
  939. * @cdev: associated ccw device
  940. * @how: use halt or clear to shutdown
  941. */
  942. int qdio_shutdown(struct ccw_device *cdev, int how)
  943. {
  944. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  945. int rc;
  946. unsigned long flags;
  947. if (!irq_ptr)
  948. return -ENODEV;
  949. BUG_ON(irqs_disabled());
  950. DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
  951. mutex_lock(&irq_ptr->setup_mutex);
  952. /*
  953. * Subchannel was already shot down. We cannot prevent being called
  954. * twice since cio may trigger a shutdown asynchronously.
  955. */
  956. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  957. mutex_unlock(&irq_ptr->setup_mutex);
  958. return 0;
  959. }
  960. /*
  961. * Indicate that the device is going down. Scheduling the queue
  962. * tasklets is forbidden from here on.
  963. */
  964. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  965. tiqdio_remove_input_queues(irq_ptr);
  966. qdio_shutdown_queues(cdev);
  967. qdio_shutdown_debug_entries(irq_ptr, cdev);
  968. /* cleanup subchannel */
  969. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  970. if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
  971. rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
  972. else
  973. /* default behaviour is halt */
  974. rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
  975. if (rc) {
  976. DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
  977. DBF_ERROR("rc:%4d", rc);
  978. goto no_cleanup;
  979. }
  980. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
  981. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  982. wait_event_interruptible_timeout(cdev->private->wait_q,
  983. irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
  984. irq_ptr->state == QDIO_IRQ_STATE_ERR,
  985. 10 * HZ);
  986. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  987. no_cleanup:
  988. qdio_shutdown_thinint(irq_ptr);
  989. /* restore interrupt handler */
  990. if ((void *)cdev->handler == (void *)qdio_int_handler)
  991. cdev->handler = irq_ptr->orig_handler;
  992. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  993. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  994. mutex_unlock(&irq_ptr->setup_mutex);
  995. if (rc)
  996. return rc;
  997. return 0;
  998. }
  999. EXPORT_SYMBOL_GPL(qdio_shutdown);
  1000. /**
  1001. * qdio_free - free data structures for a qdio subchannel
  1002. * @cdev: associated ccw device
  1003. */
  1004. int qdio_free(struct ccw_device *cdev)
  1005. {
  1006. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  1007. if (!irq_ptr)
  1008. return -ENODEV;
  1009. DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
  1010. mutex_lock(&irq_ptr->setup_mutex);
  1011. if (irq_ptr->debug_area != NULL) {
  1012. debug_unregister(irq_ptr->debug_area);
  1013. irq_ptr->debug_area = NULL;
  1014. }
  1015. cdev->private->qdio_data = NULL;
  1016. mutex_unlock(&irq_ptr->setup_mutex);
  1017. qdio_release_memory(irq_ptr);
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL_GPL(qdio_free);
  1021. /**
  1022. * qdio_initialize - allocate and establish queues for a qdio subchannel
  1023. * @init_data: initialization data
  1024. *
  1025. * This function first allocates queues via qdio_allocate() and on success
  1026. * establishes them via qdio_establish().
  1027. */
  1028. int qdio_initialize(struct qdio_initialize *init_data)
  1029. {
  1030. int rc;
  1031. rc = qdio_allocate(init_data);
  1032. if (rc)
  1033. return rc;
  1034. rc = qdio_establish(init_data);
  1035. if (rc)
  1036. qdio_free(init_data->cdev);
  1037. return rc;
  1038. }
  1039. EXPORT_SYMBOL_GPL(qdio_initialize);
  1040. /**
  1041. * qdio_allocate - allocate qdio queues and associated data
  1042. * @init_data: initialization data
  1043. */
  1044. int qdio_allocate(struct qdio_initialize *init_data)
  1045. {
  1046. struct qdio_irq *irq_ptr;
  1047. DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
  1048. if ((init_data->no_input_qs && !init_data->input_handler) ||
  1049. (init_data->no_output_qs && !init_data->output_handler))
  1050. return -EINVAL;
  1051. if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
  1052. (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
  1053. return -EINVAL;
  1054. if ((!init_data->input_sbal_addr_array) ||
  1055. (!init_data->output_sbal_addr_array))
  1056. return -EINVAL;
  1057. /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
  1058. irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1059. if (!irq_ptr)
  1060. goto out_err;
  1061. mutex_init(&irq_ptr->setup_mutex);
  1062. qdio_allocate_dbf(init_data, irq_ptr);
  1063. /*
  1064. * Allocate a page for the chsc calls in qdio_establish.
  1065. * Must be pre-allocated since a zfcp recovery will call
  1066. * qdio_establish. In case of low memory and swap on a zfcp disk
  1067. * we may not be able to allocate memory otherwise.
  1068. */
  1069. irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
  1070. if (!irq_ptr->chsc_page)
  1071. goto out_rel;
  1072. /* qdr is used in ccw1.cda which is u32 */
  1073. irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1074. if (!irq_ptr->qdr)
  1075. goto out_rel;
  1076. WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
  1077. if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
  1078. init_data->no_output_qs))
  1079. goto out_rel;
  1080. init_data->cdev->private->qdio_data = irq_ptr;
  1081. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1082. return 0;
  1083. out_rel:
  1084. qdio_release_memory(irq_ptr);
  1085. out_err:
  1086. return -ENOMEM;
  1087. }
  1088. EXPORT_SYMBOL_GPL(qdio_allocate);
  1089. /**
  1090. * qdio_establish - establish queues on a qdio subchannel
  1091. * @init_data: initialization data
  1092. */
  1093. int qdio_establish(struct qdio_initialize *init_data)
  1094. {
  1095. struct qdio_irq *irq_ptr;
  1096. struct ccw_device *cdev = init_data->cdev;
  1097. unsigned long saveflags;
  1098. int rc;
  1099. DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
  1100. irq_ptr = cdev->private->qdio_data;
  1101. if (!irq_ptr)
  1102. return -ENODEV;
  1103. if (cdev->private->state != DEV_STATE_ONLINE)
  1104. return -EINVAL;
  1105. mutex_lock(&irq_ptr->setup_mutex);
  1106. qdio_setup_irq(init_data);
  1107. rc = qdio_establish_thinint(irq_ptr);
  1108. if (rc) {
  1109. mutex_unlock(&irq_ptr->setup_mutex);
  1110. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1111. return rc;
  1112. }
  1113. /* establish q */
  1114. irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
  1115. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1116. irq_ptr->ccw.count = irq_ptr->equeue.count;
  1117. irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
  1118. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1119. ccw_device_set_options_mask(cdev, 0);
  1120. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
  1121. if (rc) {
  1122. DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
  1123. DBF_ERROR("rc:%4x", rc);
  1124. }
  1125. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1126. if (rc) {
  1127. mutex_unlock(&irq_ptr->setup_mutex);
  1128. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1129. return rc;
  1130. }
  1131. wait_event_interruptible_timeout(cdev->private->wait_q,
  1132. irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
  1133. irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
  1134. if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
  1135. mutex_unlock(&irq_ptr->setup_mutex);
  1136. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1137. return -EIO;
  1138. }
  1139. qdio_setup_ssqd_info(irq_ptr);
  1140. DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
  1141. DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
  1142. /* qebsm is now setup if available, initialize buffer states */
  1143. qdio_init_buf_states(irq_ptr);
  1144. mutex_unlock(&irq_ptr->setup_mutex);
  1145. qdio_print_subchannel_info(irq_ptr, cdev);
  1146. qdio_setup_debug_entries(irq_ptr, cdev);
  1147. return 0;
  1148. }
  1149. EXPORT_SYMBOL_GPL(qdio_establish);
  1150. /**
  1151. * qdio_activate - activate queues on a qdio subchannel
  1152. * @cdev: associated cdev
  1153. */
  1154. int qdio_activate(struct ccw_device *cdev)
  1155. {
  1156. struct qdio_irq *irq_ptr;
  1157. int rc;
  1158. unsigned long saveflags;
  1159. DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
  1160. irq_ptr = cdev->private->qdio_data;
  1161. if (!irq_ptr)
  1162. return -ENODEV;
  1163. if (cdev->private->state != DEV_STATE_ONLINE)
  1164. return -EINVAL;
  1165. mutex_lock(&irq_ptr->setup_mutex);
  1166. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1167. rc = -EBUSY;
  1168. goto out;
  1169. }
  1170. irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
  1171. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1172. irq_ptr->ccw.count = irq_ptr->aqueue.count;
  1173. irq_ptr->ccw.cda = 0;
  1174. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1175. ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
  1176. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
  1177. 0, DOIO_DENY_PREFETCH);
  1178. if (rc) {
  1179. DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
  1180. DBF_ERROR("rc:%4x", rc);
  1181. }
  1182. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1183. if (rc)
  1184. goto out;
  1185. if (is_thinint_irq(irq_ptr))
  1186. tiqdio_add_input_queues(irq_ptr);
  1187. /* wait for subchannel to become active */
  1188. msleep(5);
  1189. switch (irq_ptr->state) {
  1190. case QDIO_IRQ_STATE_STOPPED:
  1191. case QDIO_IRQ_STATE_ERR:
  1192. rc = -EIO;
  1193. break;
  1194. default:
  1195. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
  1196. rc = 0;
  1197. }
  1198. out:
  1199. mutex_unlock(&irq_ptr->setup_mutex);
  1200. return rc;
  1201. }
  1202. EXPORT_SYMBOL_GPL(qdio_activate);
  1203. static inline int buf_in_between(int bufnr, int start, int count)
  1204. {
  1205. int end = add_buf(start, count);
  1206. if (end > start) {
  1207. if (bufnr >= start && bufnr < end)
  1208. return 1;
  1209. else
  1210. return 0;
  1211. }
  1212. /* wrap-around case */
  1213. if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
  1214. (bufnr < end))
  1215. return 1;
  1216. else
  1217. return 0;
  1218. }
  1219. /**
  1220. * handle_inbound - reset processed input buffers
  1221. * @q: queue containing the buffers
  1222. * @callflags: flags
  1223. * @bufnr: first buffer to process
  1224. * @count: how many buffers are emptied
  1225. */
  1226. static void handle_inbound(struct qdio_q *q, unsigned int callflags,
  1227. int bufnr, int count)
  1228. {
  1229. int used, cc, diff;
  1230. if (!q->u.in.polling)
  1231. goto set;
  1232. /* protect against stop polling setting an ACK for an emptied slsb */
  1233. if (count == QDIO_MAX_BUFFERS_PER_Q) {
  1234. /* overwriting everything, just delete polling status */
  1235. q->u.in.polling = 0;
  1236. q->u.in.ack_count = 0;
  1237. goto set;
  1238. } else if (buf_in_between(q->last_move_ftc, bufnr, count)) {
  1239. if (is_qebsm(q)) {
  1240. /* partial overwrite, just update last_move_ftc */
  1241. diff = add_buf(bufnr, count);
  1242. diff = sub_buf(diff, q->last_move_ftc);
  1243. q->u.in.ack_count -= diff;
  1244. if (q->u.in.ack_count <= 0) {
  1245. q->u.in.polling = 0;
  1246. q->u.in.ack_count = 0;
  1247. /* TODO: must we set last_move_ftc to something meaningful? */
  1248. goto set;
  1249. }
  1250. q->last_move_ftc = add_buf(q->last_move_ftc, diff);
  1251. }
  1252. else
  1253. /* the only ACK will be deleted, so stop polling */
  1254. q->u.in.polling = 0;
  1255. }
  1256. set:
  1257. count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
  1258. used = atomic_add_return(count, &q->nr_buf_used) - count;
  1259. BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
  1260. /* no need to signal as long as the adapter had free buffers */
  1261. if (used)
  1262. return;
  1263. if (need_siga_in(q)) {
  1264. cc = qdio_siga_input(q);
  1265. if (cc)
  1266. q->qdio_error = cc;
  1267. }
  1268. }
  1269. /**
  1270. * handle_outbound - process filled outbound buffers
  1271. * @q: queue containing the buffers
  1272. * @callflags: flags
  1273. * @bufnr: first buffer to process
  1274. * @count: how many buffers are filled
  1275. */
  1276. static void handle_outbound(struct qdio_q *q, unsigned int callflags,
  1277. int bufnr, int count)
  1278. {
  1279. unsigned char state;
  1280. int used;
  1281. qdio_perf_stat_inc(&perf_stats.outbound_handler);
  1282. count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
  1283. used = atomic_add_return(count, &q->nr_buf_used);
  1284. BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
  1285. if (callflags & QDIO_FLAG_PCI_OUT)
  1286. q->u.out.pci_out_enabled = 1;
  1287. else
  1288. q->u.out.pci_out_enabled = 0;
  1289. if (queue_type(q) == QDIO_IQDIO_QFMT) {
  1290. if (multicast_outbound(q))
  1291. qdio_kick_outbound_q(q);
  1292. else
  1293. if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
  1294. (count > 1) &&
  1295. (count <= q->irq_ptr->ssqd_desc.mmwc)) {
  1296. /* exploit enhanced SIGA */
  1297. q->u.out.use_enh_siga = 1;
  1298. qdio_kick_outbound_q(q);
  1299. } else {
  1300. /*
  1301. * One siga-w per buffer required for unicast
  1302. * HiperSockets.
  1303. */
  1304. q->u.out.use_enh_siga = 0;
  1305. while (count--)
  1306. qdio_kick_outbound_q(q);
  1307. }
  1308. /* report CC=2 conditions synchronously */
  1309. if (q->qdio_error)
  1310. __qdio_outbound_processing(q);
  1311. goto out;
  1312. }
  1313. if (need_siga_sync(q)) {
  1314. qdio_siga_sync_q(q);
  1315. goto out;
  1316. }
  1317. /* try to fast requeue buffers */
  1318. get_buf_state(q, prev_buf(bufnr), &state, 0);
  1319. if (state != SLSB_CU_OUTPUT_PRIMED)
  1320. qdio_kick_outbound_q(q);
  1321. else {
  1322. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "fast-req");
  1323. qdio_perf_stat_inc(&perf_stats.fast_requeue);
  1324. }
  1325. out:
  1326. tasklet_schedule(&q->tasklet);
  1327. }
  1328. /**
  1329. * do_QDIO - process input or output buffers
  1330. * @cdev: associated ccw_device for the qdio subchannel
  1331. * @callflags: input or output and special flags from the program
  1332. * @q_nr: queue number
  1333. * @bufnr: buffer number
  1334. * @count: how many buffers to process
  1335. */
  1336. int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
  1337. int q_nr, int bufnr, int count)
  1338. {
  1339. struct qdio_irq *irq_ptr;
  1340. if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
  1341. (count > QDIO_MAX_BUFFERS_PER_Q) ||
  1342. (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
  1343. return -EINVAL;
  1344. if (!count)
  1345. return 0;
  1346. irq_ptr = cdev->private->qdio_data;
  1347. if (!irq_ptr)
  1348. return -ENODEV;
  1349. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1350. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO input");
  1351. else
  1352. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO output");
  1353. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "q:%1d flag:%4x", q_nr, callflags);
  1354. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "buf:%2d cnt:%3d", bufnr, count);
  1355. if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
  1356. return -EBUSY;
  1357. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1358. handle_inbound(irq_ptr->input_qs[q_nr], callflags, bufnr,
  1359. count);
  1360. else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
  1361. handle_outbound(irq_ptr->output_qs[q_nr], callflags, bufnr,
  1362. count);
  1363. else
  1364. return -EINVAL;
  1365. return 0;
  1366. }
  1367. EXPORT_SYMBOL_GPL(do_QDIO);
  1368. static int __init init_QDIO(void)
  1369. {
  1370. int rc;
  1371. rc = qdio_setup_init();
  1372. if (rc)
  1373. return rc;
  1374. rc = tiqdio_allocate_memory();
  1375. if (rc)
  1376. goto out_cache;
  1377. rc = qdio_debug_init();
  1378. if (rc)
  1379. goto out_ti;
  1380. rc = qdio_setup_perf_stats();
  1381. if (rc)
  1382. goto out_debug;
  1383. rc = tiqdio_register_thinints();
  1384. if (rc)
  1385. goto out_perf;
  1386. return 0;
  1387. out_perf:
  1388. qdio_remove_perf_stats();
  1389. out_debug:
  1390. qdio_debug_exit();
  1391. out_ti:
  1392. tiqdio_free_memory();
  1393. out_cache:
  1394. qdio_setup_exit();
  1395. return rc;
  1396. }
  1397. static void __exit exit_QDIO(void)
  1398. {
  1399. tiqdio_unregister_thinints();
  1400. tiqdio_free_memory();
  1401. qdio_remove_perf_stats();
  1402. qdio_debug_exit();
  1403. qdio_setup_exit();
  1404. }
  1405. module_init(init_QDIO);
  1406. module_exit(exit_QDIO);