qdio_main.c 41 KB

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