qdio_main.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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 successfull 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. tasklet_schedule(&q->tasklet);
  666. return;
  667. }
  668. /* bail out for HiperSockets unicast queues */
  669. if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
  670. return;
  671. if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
  672. (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL) {
  673. tasklet_schedule(&q->tasklet);
  674. return;
  675. }
  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. }
  692. /* outbound tasklet */
  693. void qdio_outbound_processing(unsigned long data)
  694. {
  695. struct qdio_q *q = (struct qdio_q *)data;
  696. __qdio_outbound_processing(q);
  697. }
  698. void qdio_outbound_timer(unsigned long data)
  699. {
  700. struct qdio_q *q = (struct qdio_q *)data;
  701. tasklet_schedule(&q->tasklet);
  702. }
  703. /* called from thinint inbound tasklet */
  704. void qdio_check_outbound_after_thinint(struct qdio_q *q)
  705. {
  706. struct qdio_q *out;
  707. int i;
  708. if (!pci_out_supported(q))
  709. return;
  710. for_each_output_queue(q->irq_ptr, out, i)
  711. if (!qdio_outbound_q_done(out))
  712. tasklet_schedule(&out->tasklet);
  713. }
  714. static inline void qdio_set_state(struct qdio_irq *irq_ptr,
  715. enum qdio_irq_states state)
  716. {
  717. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
  718. irq_ptr->state = state;
  719. mb();
  720. }
  721. static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
  722. {
  723. if (irb->esw.esw0.erw.cons) {
  724. DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
  725. DBF_ERROR_HEX(irb, 64);
  726. DBF_ERROR_HEX(irb->ecw, 64);
  727. }
  728. }
  729. /* PCI interrupt handler */
  730. static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
  731. {
  732. int i;
  733. struct qdio_q *q;
  734. qdio_perf_stat_inc(&perf_stats.pci_int);
  735. for_each_input_queue(irq_ptr, q, i)
  736. tasklet_schedule(&q->tasklet);
  737. if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
  738. return;
  739. for_each_output_queue(irq_ptr, q, i) {
  740. if (qdio_outbound_q_done(q))
  741. continue;
  742. if (!siga_syncs_out_pci(q))
  743. qdio_siga_sync_q(q);
  744. tasklet_schedule(&q->tasklet);
  745. }
  746. }
  747. static void qdio_handle_activate_check(struct ccw_device *cdev,
  748. unsigned long intparm, int cstat, int dstat)
  749. {
  750. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  751. struct qdio_q *q;
  752. DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
  753. DBF_ERROR("intp :%lx", intparm);
  754. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  755. if (irq_ptr->nr_input_qs) {
  756. q = irq_ptr->input_qs[0];
  757. } else if (irq_ptr->nr_output_qs) {
  758. q = irq_ptr->output_qs[0];
  759. } else {
  760. dump_stack();
  761. goto no_handler;
  762. }
  763. q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
  764. 0, -1, -1, irq_ptr->int_parm);
  765. no_handler:
  766. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  767. }
  768. static void qdio_call_shutdown(struct work_struct *work)
  769. {
  770. struct ccw_device_private *priv;
  771. struct ccw_device *cdev;
  772. priv = container_of(work, struct ccw_device_private, kick_work);
  773. cdev = priv->cdev;
  774. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  775. put_device(&cdev->dev);
  776. }
  777. static void qdio_int_error(struct ccw_device *cdev)
  778. {
  779. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  780. switch (irq_ptr->state) {
  781. case QDIO_IRQ_STATE_INACTIVE:
  782. case QDIO_IRQ_STATE_CLEANUP:
  783. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  784. break;
  785. case QDIO_IRQ_STATE_ESTABLISHED:
  786. case QDIO_IRQ_STATE_ACTIVE:
  787. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
  788. if (get_device(&cdev->dev)) {
  789. /* Can't call shutdown from interrupt context. */
  790. PREPARE_WORK(&cdev->private->kick_work,
  791. qdio_call_shutdown);
  792. queue_work(ccw_device_work, &cdev->private->kick_work);
  793. }
  794. break;
  795. default:
  796. WARN_ON(1);
  797. }
  798. wake_up(&cdev->private->wait_q);
  799. }
  800. static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
  801. int dstat)
  802. {
  803. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  804. if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
  805. DBF_ERROR("EQ:ck con");
  806. goto error;
  807. }
  808. if (!(dstat & DEV_STAT_DEV_END)) {
  809. DBF_ERROR("EQ:no dev");
  810. goto error;
  811. }
  812. if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
  813. DBF_ERROR("EQ: bad io");
  814. goto error;
  815. }
  816. return 0;
  817. error:
  818. DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
  819. DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
  820. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
  821. return 1;
  822. }
  823. static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
  824. int dstat)
  825. {
  826. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  827. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
  828. if (!qdio_establish_check_errors(cdev, cstat, dstat))
  829. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
  830. }
  831. /* qdio interrupt handler */
  832. void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
  833. struct irb *irb)
  834. {
  835. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  836. int cstat, dstat;
  837. qdio_perf_stat_inc(&perf_stats.qdio_int);
  838. if (!intparm || !irq_ptr) {
  839. DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
  840. return;
  841. }
  842. if (IS_ERR(irb)) {
  843. switch (PTR_ERR(irb)) {
  844. case -EIO:
  845. DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
  846. return;
  847. case -ETIMEDOUT:
  848. DBF_ERROR("%4x IO timeout", irq_ptr->schid.sch_no);
  849. qdio_int_error(cdev);
  850. return;
  851. default:
  852. WARN_ON(1);
  853. return;
  854. }
  855. }
  856. qdio_irq_check_sense(irq_ptr, irb);
  857. cstat = irb->scsw.cmd.cstat;
  858. dstat = irb->scsw.cmd.dstat;
  859. switch (irq_ptr->state) {
  860. case QDIO_IRQ_STATE_INACTIVE:
  861. qdio_establish_handle_irq(cdev, cstat, dstat);
  862. break;
  863. case QDIO_IRQ_STATE_CLEANUP:
  864. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  865. break;
  866. case QDIO_IRQ_STATE_ESTABLISHED:
  867. case QDIO_IRQ_STATE_ACTIVE:
  868. if (cstat & SCHN_STAT_PCI) {
  869. qdio_int_handler_pci(irq_ptr);
  870. /* no state change so no need to wake up wait_q */
  871. return;
  872. }
  873. if ((cstat & ~SCHN_STAT_PCI) || dstat) {
  874. qdio_handle_activate_check(cdev, intparm, cstat,
  875. dstat);
  876. break;
  877. }
  878. default:
  879. WARN_ON(1);
  880. }
  881. wake_up(&cdev->private->wait_q);
  882. }
  883. /**
  884. * qdio_get_ssqd_desc - get qdio subchannel description
  885. * @cdev: ccw device to get description for
  886. * @data: where to store the ssqd
  887. *
  888. * Returns 0 or an error code. The results of the chsc are stored in the
  889. * specified structure.
  890. */
  891. int qdio_get_ssqd_desc(struct ccw_device *cdev,
  892. struct qdio_ssqd_desc *data)
  893. {
  894. if (!cdev || !cdev->private)
  895. return -EINVAL;
  896. DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
  897. return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
  898. }
  899. EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
  900. /**
  901. * qdio_cleanup - shutdown queues and free data structures
  902. * @cdev: associated ccw device
  903. * @how: use halt or clear to shutdown
  904. *
  905. * This function calls qdio_shutdown() for @cdev with method @how
  906. * and on success qdio_free() for @cdev.
  907. */
  908. int qdio_cleanup(struct ccw_device *cdev, int how)
  909. {
  910. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  911. int rc;
  912. if (!irq_ptr)
  913. return -ENODEV;
  914. rc = qdio_shutdown(cdev, how);
  915. if (rc == 0)
  916. rc = qdio_free(cdev);
  917. return rc;
  918. }
  919. EXPORT_SYMBOL_GPL(qdio_cleanup);
  920. static void qdio_shutdown_queues(struct ccw_device *cdev)
  921. {
  922. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  923. struct qdio_q *q;
  924. int i;
  925. for_each_input_queue(irq_ptr, q, i)
  926. tasklet_disable(&q->tasklet);
  927. for_each_output_queue(irq_ptr, q, i) {
  928. tasklet_disable(&q->tasklet);
  929. del_timer(&q->u.out.timer);
  930. }
  931. }
  932. /**
  933. * qdio_shutdown - shut down a qdio subchannel
  934. * @cdev: associated ccw device
  935. * @how: use halt or clear to shutdown
  936. */
  937. int qdio_shutdown(struct ccw_device *cdev, int how)
  938. {
  939. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  940. int rc;
  941. unsigned long flags;
  942. if (!irq_ptr)
  943. return -ENODEV;
  944. DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
  945. mutex_lock(&irq_ptr->setup_mutex);
  946. /*
  947. * Subchannel was already shot down. We cannot prevent being called
  948. * twice since cio may trigger a shutdown asynchronously.
  949. */
  950. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  951. mutex_unlock(&irq_ptr->setup_mutex);
  952. return 0;
  953. }
  954. tiqdio_remove_input_queues(irq_ptr);
  955. qdio_shutdown_queues(cdev);
  956. qdio_shutdown_debug_entries(irq_ptr, cdev);
  957. /* cleanup subchannel */
  958. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  959. if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
  960. rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
  961. else
  962. /* default behaviour is halt */
  963. rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
  964. if (rc) {
  965. DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
  966. DBF_ERROR("rc:%4d", rc);
  967. goto no_cleanup;
  968. }
  969. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
  970. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  971. wait_event_interruptible_timeout(cdev->private->wait_q,
  972. irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
  973. irq_ptr->state == QDIO_IRQ_STATE_ERR,
  974. 10 * HZ);
  975. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  976. no_cleanup:
  977. qdio_shutdown_thinint(irq_ptr);
  978. /* restore interrupt handler */
  979. if ((void *)cdev->handler == (void *)qdio_int_handler)
  980. cdev->handler = irq_ptr->orig_handler;
  981. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  982. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  983. mutex_unlock(&irq_ptr->setup_mutex);
  984. if (rc)
  985. return rc;
  986. return 0;
  987. }
  988. EXPORT_SYMBOL_GPL(qdio_shutdown);
  989. /**
  990. * qdio_free - free data structures for a qdio subchannel
  991. * @cdev: associated ccw device
  992. */
  993. int qdio_free(struct ccw_device *cdev)
  994. {
  995. struct qdio_irq *irq_ptr = cdev->private->qdio_data;
  996. if (!irq_ptr)
  997. return -ENODEV;
  998. DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
  999. mutex_lock(&irq_ptr->setup_mutex);
  1000. if (irq_ptr->debug_area != NULL) {
  1001. debug_unregister(irq_ptr->debug_area);
  1002. irq_ptr->debug_area = NULL;
  1003. }
  1004. cdev->private->qdio_data = NULL;
  1005. mutex_unlock(&irq_ptr->setup_mutex);
  1006. qdio_release_memory(irq_ptr);
  1007. return 0;
  1008. }
  1009. EXPORT_SYMBOL_GPL(qdio_free);
  1010. /**
  1011. * qdio_initialize - allocate and establish queues for a qdio subchannel
  1012. * @init_data: initialization data
  1013. *
  1014. * This function first allocates queues via qdio_allocate() and on success
  1015. * establishes them via qdio_establish().
  1016. */
  1017. int qdio_initialize(struct qdio_initialize *init_data)
  1018. {
  1019. int rc;
  1020. rc = qdio_allocate(init_data);
  1021. if (rc)
  1022. return rc;
  1023. rc = qdio_establish(init_data);
  1024. if (rc)
  1025. qdio_free(init_data->cdev);
  1026. return rc;
  1027. }
  1028. EXPORT_SYMBOL_GPL(qdio_initialize);
  1029. /**
  1030. * qdio_allocate - allocate qdio queues and associated data
  1031. * @init_data: initialization data
  1032. */
  1033. int qdio_allocate(struct qdio_initialize *init_data)
  1034. {
  1035. struct qdio_irq *irq_ptr;
  1036. DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
  1037. if ((init_data->no_input_qs && !init_data->input_handler) ||
  1038. (init_data->no_output_qs && !init_data->output_handler))
  1039. return -EINVAL;
  1040. if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
  1041. (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
  1042. return -EINVAL;
  1043. if ((!init_data->input_sbal_addr_array) ||
  1044. (!init_data->output_sbal_addr_array))
  1045. return -EINVAL;
  1046. /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
  1047. irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1048. if (!irq_ptr)
  1049. goto out_err;
  1050. mutex_init(&irq_ptr->setup_mutex);
  1051. qdio_allocate_dbf(init_data, irq_ptr);
  1052. /*
  1053. * Allocate a page for the chsc calls in qdio_establish.
  1054. * Must be pre-allocated since a zfcp recovery will call
  1055. * qdio_establish. In case of low memory and swap on a zfcp disk
  1056. * we may not be able to allocate memory otherwise.
  1057. */
  1058. irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
  1059. if (!irq_ptr->chsc_page)
  1060. goto out_rel;
  1061. /* qdr is used in ccw1.cda which is u32 */
  1062. irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  1063. if (!irq_ptr->qdr)
  1064. goto out_rel;
  1065. WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
  1066. if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
  1067. init_data->no_output_qs))
  1068. goto out_rel;
  1069. init_data->cdev->private->qdio_data = irq_ptr;
  1070. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
  1071. return 0;
  1072. out_rel:
  1073. qdio_release_memory(irq_ptr);
  1074. out_err:
  1075. return -ENOMEM;
  1076. }
  1077. EXPORT_SYMBOL_GPL(qdio_allocate);
  1078. /**
  1079. * qdio_establish - establish queues on a qdio subchannel
  1080. * @init_data: initialization data
  1081. */
  1082. int qdio_establish(struct qdio_initialize *init_data)
  1083. {
  1084. struct qdio_irq *irq_ptr;
  1085. struct ccw_device *cdev = init_data->cdev;
  1086. unsigned long saveflags;
  1087. int rc;
  1088. DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
  1089. irq_ptr = cdev->private->qdio_data;
  1090. if (!irq_ptr)
  1091. return -ENODEV;
  1092. if (cdev->private->state != DEV_STATE_ONLINE)
  1093. return -EINVAL;
  1094. mutex_lock(&irq_ptr->setup_mutex);
  1095. qdio_setup_irq(init_data);
  1096. rc = qdio_establish_thinint(irq_ptr);
  1097. if (rc) {
  1098. mutex_unlock(&irq_ptr->setup_mutex);
  1099. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1100. return rc;
  1101. }
  1102. /* establish q */
  1103. irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
  1104. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1105. irq_ptr->ccw.count = irq_ptr->equeue.count;
  1106. irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
  1107. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1108. ccw_device_set_options_mask(cdev, 0);
  1109. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
  1110. if (rc) {
  1111. DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
  1112. DBF_ERROR("rc:%4x", rc);
  1113. }
  1114. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1115. if (rc) {
  1116. mutex_unlock(&irq_ptr->setup_mutex);
  1117. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1118. return rc;
  1119. }
  1120. wait_event_interruptible_timeout(cdev->private->wait_q,
  1121. irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
  1122. irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
  1123. if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
  1124. mutex_unlock(&irq_ptr->setup_mutex);
  1125. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1126. return -EIO;
  1127. }
  1128. qdio_setup_ssqd_info(irq_ptr);
  1129. DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
  1130. DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
  1131. /* qebsm is now setup if available, initialize buffer states */
  1132. qdio_init_buf_states(irq_ptr);
  1133. mutex_unlock(&irq_ptr->setup_mutex);
  1134. qdio_print_subchannel_info(irq_ptr, cdev);
  1135. qdio_setup_debug_entries(irq_ptr, cdev);
  1136. return 0;
  1137. }
  1138. EXPORT_SYMBOL_GPL(qdio_establish);
  1139. /**
  1140. * qdio_activate - activate queues on a qdio subchannel
  1141. * @cdev: associated cdev
  1142. */
  1143. int qdio_activate(struct ccw_device *cdev)
  1144. {
  1145. struct qdio_irq *irq_ptr;
  1146. int rc;
  1147. unsigned long saveflags;
  1148. DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
  1149. irq_ptr = cdev->private->qdio_data;
  1150. if (!irq_ptr)
  1151. return -ENODEV;
  1152. if (cdev->private->state != DEV_STATE_ONLINE)
  1153. return -EINVAL;
  1154. mutex_lock(&irq_ptr->setup_mutex);
  1155. if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
  1156. rc = -EBUSY;
  1157. goto out;
  1158. }
  1159. irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
  1160. irq_ptr->ccw.flags = CCW_FLAG_SLI;
  1161. irq_ptr->ccw.count = irq_ptr->aqueue.count;
  1162. irq_ptr->ccw.cda = 0;
  1163. spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
  1164. ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
  1165. rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
  1166. 0, DOIO_DENY_PREFETCH);
  1167. if (rc) {
  1168. DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
  1169. DBF_ERROR("rc:%4x", rc);
  1170. }
  1171. spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
  1172. if (rc)
  1173. goto out;
  1174. if (is_thinint_irq(irq_ptr))
  1175. tiqdio_add_input_queues(irq_ptr);
  1176. /* wait for subchannel to become active */
  1177. msleep(5);
  1178. switch (irq_ptr->state) {
  1179. case QDIO_IRQ_STATE_STOPPED:
  1180. case QDIO_IRQ_STATE_ERR:
  1181. mutex_unlock(&irq_ptr->setup_mutex);
  1182. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  1183. return -EIO;
  1184. default:
  1185. qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
  1186. rc = 0;
  1187. }
  1188. out:
  1189. mutex_unlock(&irq_ptr->setup_mutex);
  1190. return rc;
  1191. }
  1192. EXPORT_SYMBOL_GPL(qdio_activate);
  1193. static inline int buf_in_between(int bufnr, int start, int count)
  1194. {
  1195. int end = add_buf(start, count);
  1196. if (end > start) {
  1197. if (bufnr >= start && bufnr < end)
  1198. return 1;
  1199. else
  1200. return 0;
  1201. }
  1202. /* wrap-around case */
  1203. if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
  1204. (bufnr < end))
  1205. return 1;
  1206. else
  1207. return 0;
  1208. }
  1209. /**
  1210. * handle_inbound - reset processed input buffers
  1211. * @q: queue containing the buffers
  1212. * @callflags: flags
  1213. * @bufnr: first buffer to process
  1214. * @count: how many buffers are emptied
  1215. */
  1216. static void handle_inbound(struct qdio_q *q, unsigned int callflags,
  1217. int bufnr, int count)
  1218. {
  1219. int used, cc, diff;
  1220. if (!q->u.in.polling)
  1221. goto set;
  1222. /* protect against stop polling setting an ACK for an emptied slsb */
  1223. if (count == QDIO_MAX_BUFFERS_PER_Q) {
  1224. /* overwriting everything, just delete polling status */
  1225. q->u.in.polling = 0;
  1226. q->u.in.ack_count = 0;
  1227. goto set;
  1228. } else if (buf_in_between(q->last_move_ftc, bufnr, count)) {
  1229. if (is_qebsm(q)) {
  1230. /* partial overwrite, just update last_move_ftc */
  1231. diff = add_buf(bufnr, count);
  1232. diff = sub_buf(diff, q->last_move_ftc);
  1233. q->u.in.ack_count -= diff;
  1234. if (q->u.in.ack_count <= 0) {
  1235. q->u.in.polling = 0;
  1236. q->u.in.ack_count = 0;
  1237. /* TODO: must we set last_move_ftc to something meaningful? */
  1238. goto set;
  1239. }
  1240. q->last_move_ftc = add_buf(q->last_move_ftc, diff);
  1241. }
  1242. else
  1243. /* the only ACK will be deleted, so stop polling */
  1244. q->u.in.polling = 0;
  1245. }
  1246. set:
  1247. count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
  1248. used = atomic_add_return(count, &q->nr_buf_used) - count;
  1249. BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
  1250. /* no need to signal as long as the adapter had free buffers */
  1251. if (used)
  1252. return;
  1253. if (need_siga_in(q)) {
  1254. cc = qdio_siga_input(q);
  1255. if (cc)
  1256. q->qdio_error = cc;
  1257. }
  1258. }
  1259. /**
  1260. * handle_outbound - process filled outbound buffers
  1261. * @q: queue containing the buffers
  1262. * @callflags: flags
  1263. * @bufnr: first buffer to process
  1264. * @count: how many buffers are filled
  1265. */
  1266. static void handle_outbound(struct qdio_q *q, unsigned int callflags,
  1267. int bufnr, int count)
  1268. {
  1269. unsigned char state;
  1270. int used;
  1271. qdio_perf_stat_inc(&perf_stats.outbound_handler);
  1272. count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
  1273. used = atomic_add_return(count, &q->nr_buf_used);
  1274. BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
  1275. if (callflags & QDIO_FLAG_PCI_OUT)
  1276. q->u.out.pci_out_enabled = 1;
  1277. else
  1278. q->u.out.pci_out_enabled = 0;
  1279. if (queue_type(q) == QDIO_IQDIO_QFMT) {
  1280. if (multicast_outbound(q))
  1281. qdio_kick_outbound_q(q);
  1282. else
  1283. if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
  1284. (count > 1) &&
  1285. (count <= q->irq_ptr->ssqd_desc.mmwc)) {
  1286. /* exploit enhanced SIGA */
  1287. q->u.out.use_enh_siga = 1;
  1288. qdio_kick_outbound_q(q);
  1289. } else {
  1290. /*
  1291. * One siga-w per buffer required for unicast
  1292. * HiperSockets.
  1293. */
  1294. q->u.out.use_enh_siga = 0;
  1295. while (count--)
  1296. qdio_kick_outbound_q(q);
  1297. }
  1298. /* report CC=2 conditions synchronously */
  1299. if (q->qdio_error)
  1300. __qdio_outbound_processing(q);
  1301. goto out;
  1302. }
  1303. if (need_siga_sync(q)) {
  1304. qdio_siga_sync_q(q);
  1305. goto out;
  1306. }
  1307. /* try to fast requeue buffers */
  1308. get_buf_state(q, prev_buf(bufnr), &state, 0);
  1309. if (state != SLSB_CU_OUTPUT_PRIMED)
  1310. qdio_kick_outbound_q(q);
  1311. else {
  1312. DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "fast-req");
  1313. qdio_perf_stat_inc(&perf_stats.fast_requeue);
  1314. }
  1315. out:
  1316. /* Fixme: could wait forever if called from process context */
  1317. tasklet_schedule(&q->tasklet);
  1318. }
  1319. /**
  1320. * do_QDIO - process input or output buffers
  1321. * @cdev: associated ccw_device for the qdio subchannel
  1322. * @callflags: input or output and special flags from the program
  1323. * @q_nr: queue number
  1324. * @bufnr: buffer number
  1325. * @count: how many buffers to process
  1326. */
  1327. int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
  1328. int q_nr, int bufnr, int count)
  1329. {
  1330. struct qdio_irq *irq_ptr;
  1331. if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
  1332. (count > QDIO_MAX_BUFFERS_PER_Q) ||
  1333. (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
  1334. return -EINVAL;
  1335. if (!count)
  1336. return 0;
  1337. irq_ptr = cdev->private->qdio_data;
  1338. if (!irq_ptr)
  1339. return -ENODEV;
  1340. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1341. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO input");
  1342. else
  1343. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO output");
  1344. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "q:%1d flag:%4x", q_nr, callflags);
  1345. DBF_DEV_EVENT(DBF_INFO, irq_ptr, "buf:%2d cnt:%3d", bufnr, count);
  1346. if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
  1347. return -EBUSY;
  1348. if (callflags & QDIO_FLAG_SYNC_INPUT)
  1349. handle_inbound(irq_ptr->input_qs[q_nr], callflags, bufnr,
  1350. count);
  1351. else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
  1352. handle_outbound(irq_ptr->output_qs[q_nr], callflags, bufnr,
  1353. count);
  1354. else
  1355. return -EINVAL;
  1356. return 0;
  1357. }
  1358. EXPORT_SYMBOL_GPL(do_QDIO);
  1359. static int __init init_QDIO(void)
  1360. {
  1361. int rc;
  1362. rc = qdio_setup_init();
  1363. if (rc)
  1364. return rc;
  1365. rc = tiqdio_allocate_memory();
  1366. if (rc)
  1367. goto out_cache;
  1368. rc = qdio_debug_init();
  1369. if (rc)
  1370. goto out_ti;
  1371. rc = qdio_setup_perf_stats();
  1372. if (rc)
  1373. goto out_debug;
  1374. rc = tiqdio_register_thinints();
  1375. if (rc)
  1376. goto out_perf;
  1377. return 0;
  1378. out_perf:
  1379. qdio_remove_perf_stats();
  1380. out_debug:
  1381. qdio_debug_exit();
  1382. out_ti:
  1383. tiqdio_free_memory();
  1384. out_cache:
  1385. qdio_setup_exit();
  1386. return rc;
  1387. }
  1388. static void __exit exit_QDIO(void)
  1389. {
  1390. tiqdio_unregister_thinints();
  1391. tiqdio_free_memory();
  1392. qdio_remove_perf_stats();
  1393. qdio_debug_exit();
  1394. qdio_setup_exit();
  1395. }
  1396. module_init(init_QDIO);
  1397. module_exit(exit_QDIO);