iucv.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /*
  2. * IUCV base infrastructure.
  3. *
  4. * Copyright 2001, 2006 IBM Deutschland Entwicklung GmbH, IBM Corporation
  5. * Author(s):
  6. * Original source:
  7. * Alan Altmark (Alan_Altmark@us.ibm.com) Sept. 2000
  8. * Xenia Tkatschow (xenia@us.ibm.com)
  9. * 2Gb awareness and general cleanup:
  10. * Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  11. * Rewritten for af_iucv:
  12. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  13. *
  14. * Documentation used:
  15. * The original source
  16. * CP Programming Service, IBM document # SC24-5760
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2, or (at your option)
  21. * any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/kernel.h>
  36. #include <linux/slab.h>
  37. #include <linux/init.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/list.h>
  40. #include <linux/errno.h>
  41. #include <linux/err.h>
  42. #include <linux/device.h>
  43. #include <linux/cpu.h>
  44. #include <net/iucv/iucv.h>
  45. #include <asm/atomic.h>
  46. #include <asm/ebcdic.h>
  47. #include <asm/io.h>
  48. #include <asm/s390_ext.h>
  49. #include <asm/s390_rdev.h>
  50. #include <asm/smp.h>
  51. /*
  52. * FLAGS:
  53. * All flags are defined in the field IPFLAGS1 of each function
  54. * and can be found in CP Programming Services.
  55. * IPSRCCLS - Indicates you have specified a source class.
  56. * IPTRGCLS - Indicates you have specified a target class.
  57. * IPFGPID - Indicates you have specified a pathid.
  58. * IPFGMID - Indicates you have specified a message ID.
  59. * IPNORPY - Indicates a one-way message. No reply expected.
  60. * IPALL - Indicates that all paths are affected.
  61. */
  62. #define IUCV_IPSRCCLS 0x01
  63. #define IUCV_IPTRGCLS 0x01
  64. #define IUCV_IPFGPID 0x02
  65. #define IUCV_IPFGMID 0x04
  66. #define IUCV_IPNORPY 0x10
  67. #define IUCV_IPALL 0x80
  68. static int iucv_bus_match (struct device *dev, struct device_driver *drv)
  69. {
  70. return 0;
  71. }
  72. struct bus_type iucv_bus = {
  73. .name = "iucv",
  74. .match = iucv_bus_match,
  75. };
  76. struct device *iucv_root;
  77. static int iucv_available;
  78. /* General IUCV interrupt structure */
  79. struct iucv_irq_data {
  80. u16 ippathid;
  81. u8 ipflags1;
  82. u8 iptype;
  83. u32 res2[8];
  84. };
  85. struct iucv_work {
  86. struct list_head list;
  87. struct iucv_irq_data data;
  88. };
  89. static LIST_HEAD(iucv_work_queue);
  90. static DEFINE_SPINLOCK(iucv_work_lock);
  91. static struct iucv_irq_data *iucv_irq_data;
  92. static cpumask_t iucv_buffer_cpumask = CPU_MASK_NONE;
  93. static cpumask_t iucv_irq_cpumask = CPU_MASK_NONE;
  94. static void iucv_tasklet_handler(unsigned long);
  95. static DECLARE_TASKLET(iucv_tasklet, iucv_tasklet_handler,0);
  96. enum iucv_command_codes {
  97. IUCV_QUERY = 0,
  98. IUCV_RETRIEVE_BUFFER = 2,
  99. IUCV_SEND = 4,
  100. IUCV_RECEIVE = 5,
  101. IUCV_REPLY = 6,
  102. IUCV_REJECT = 8,
  103. IUCV_PURGE = 9,
  104. IUCV_ACCEPT = 10,
  105. IUCV_CONNECT = 11,
  106. IUCV_DECLARE_BUFFER = 12,
  107. IUCV_QUIESCE = 13,
  108. IUCV_RESUME = 14,
  109. IUCV_SEVER = 15,
  110. IUCV_SETMASK = 16,
  111. };
  112. /*
  113. * Error messages that are used with the iucv_sever function. They get
  114. * converted to EBCDIC.
  115. */
  116. static char iucv_error_no_listener[16] = "NO LISTENER";
  117. static char iucv_error_no_memory[16] = "NO MEMORY";
  118. static char iucv_error_pathid[16] = "INVALID PATHID";
  119. /*
  120. * iucv_handler_list: List of registered handlers.
  121. */
  122. static LIST_HEAD(iucv_handler_list);
  123. /*
  124. * iucv_path_table: an array of iucv_path structures.
  125. */
  126. static struct iucv_path **iucv_path_table;
  127. static unsigned long iucv_max_pathid;
  128. /*
  129. * iucv_lock: spinlock protecting iucv_handler_list and iucv_pathid_table
  130. */
  131. static DEFINE_SPINLOCK(iucv_table_lock);
  132. /*
  133. * iucv_tasklet_cpu: contains the number of the cpu executing the tasklet.
  134. * Needed for iucv_path_sever called from tasklet.
  135. */
  136. static int iucv_tasklet_cpu = -1;
  137. /*
  138. * Mutex and wait queue for iucv_register/iucv_unregister.
  139. */
  140. static DEFINE_MUTEX(iucv_register_mutex);
  141. /*
  142. * Counter for number of non-smp capable handlers.
  143. */
  144. static int iucv_nonsmp_handler;
  145. /*
  146. * IUCV control data structure. Used by iucv_path_accept, iucv_path_connect,
  147. * iucv_path_quiesce and iucv_path_sever.
  148. */
  149. struct iucv_cmd_control {
  150. u16 ippathid;
  151. u8 ipflags1;
  152. u8 iprcode;
  153. u16 ipmsglim;
  154. u16 res1;
  155. u8 ipvmid[8];
  156. u8 ipuser[16];
  157. u8 iptarget[8];
  158. } __attribute__ ((packed,aligned(8)));
  159. /*
  160. * Data in parameter list iucv structure. Used by iucv_message_send,
  161. * iucv_message_send2way and iucv_message_reply.
  162. */
  163. struct iucv_cmd_dpl {
  164. u16 ippathid;
  165. u8 ipflags1;
  166. u8 iprcode;
  167. u32 ipmsgid;
  168. u32 iptrgcls;
  169. u8 iprmmsg[8];
  170. u32 ipsrccls;
  171. u32 ipmsgtag;
  172. u32 ipbfadr2;
  173. u32 ipbfln2f;
  174. u32 res;
  175. } __attribute__ ((packed,aligned(8)));
  176. /*
  177. * Data in buffer iucv structure. Used by iucv_message_receive,
  178. * iucv_message_reject, iucv_message_send, iucv_message_send2way
  179. * and iucv_declare_cpu.
  180. */
  181. struct iucv_cmd_db {
  182. u16 ippathid;
  183. u8 ipflags1;
  184. u8 iprcode;
  185. u32 ipmsgid;
  186. u32 iptrgcls;
  187. u32 ipbfadr1;
  188. u32 ipbfln1f;
  189. u32 ipsrccls;
  190. u32 ipmsgtag;
  191. u32 ipbfadr2;
  192. u32 ipbfln2f;
  193. u32 res;
  194. } __attribute__ ((packed,aligned(8)));
  195. /*
  196. * Purge message iucv structure. Used by iucv_message_purge.
  197. */
  198. struct iucv_cmd_purge {
  199. u16 ippathid;
  200. u8 ipflags1;
  201. u8 iprcode;
  202. u32 ipmsgid;
  203. u8 ipaudit[3];
  204. u8 res1[5];
  205. u32 res2;
  206. u32 ipsrccls;
  207. u32 ipmsgtag;
  208. u32 res3[3];
  209. } __attribute__ ((packed,aligned(8)));
  210. /*
  211. * Set mask iucv structure. Used by iucv_enable_cpu.
  212. */
  213. struct iucv_cmd_set_mask {
  214. u8 ipmask;
  215. u8 res1[2];
  216. u8 iprcode;
  217. u32 res2[9];
  218. } __attribute__ ((packed,aligned(8)));
  219. union iucv_param {
  220. struct iucv_cmd_control ctrl;
  221. struct iucv_cmd_dpl dpl;
  222. struct iucv_cmd_db db;
  223. struct iucv_cmd_purge purge;
  224. struct iucv_cmd_set_mask set_mask;
  225. };
  226. /*
  227. * Anchor for per-cpu IUCV command parameter block.
  228. */
  229. static union iucv_param *iucv_param;
  230. /**
  231. * iucv_call_b2f0
  232. * @code: identifier of IUCV call to CP.
  233. * @parm: pointer to a struct iucv_parm block
  234. *
  235. * Calls CP to execute IUCV commands.
  236. *
  237. * Returns the result of the CP IUCV call.
  238. */
  239. static inline int iucv_call_b2f0(int command, union iucv_param *parm)
  240. {
  241. register unsigned long reg0 asm ("0");
  242. register unsigned long reg1 asm ("1");
  243. int ccode;
  244. reg0 = command;
  245. reg1 = virt_to_phys(parm);
  246. asm volatile(
  247. " .long 0xb2f01000\n"
  248. " ipm %0\n"
  249. " srl %0,28\n"
  250. : "=d" (ccode), "=m" (*parm), "+d" (reg0), "+a" (reg1)
  251. : "m" (*parm) : "cc");
  252. return (ccode == 1) ? parm->ctrl.iprcode : ccode;
  253. }
  254. /**
  255. * iucv_query_maxconn
  256. *
  257. * Determines the maximum number of connections that may be established.
  258. *
  259. * Returns the maximum number of connections or -EPERM is IUCV is not
  260. * available.
  261. */
  262. static int iucv_query_maxconn(void)
  263. {
  264. register unsigned long reg0 asm ("0");
  265. register unsigned long reg1 asm ("1");
  266. void *param;
  267. int ccode;
  268. param = kzalloc(sizeof(union iucv_param), GFP_KERNEL|GFP_DMA);
  269. if (!param)
  270. return -ENOMEM;
  271. reg0 = IUCV_QUERY;
  272. reg1 = (unsigned long) param;
  273. asm volatile (
  274. " .long 0xb2f01000\n"
  275. " ipm %0\n"
  276. " srl %0,28\n"
  277. : "=d" (ccode), "+d" (reg0), "+d" (reg1) : : "cc");
  278. if (ccode == 0)
  279. iucv_max_pathid = reg0;
  280. kfree(param);
  281. return ccode ? -EPERM : 0;
  282. }
  283. /**
  284. * iucv_allow_cpu
  285. * @data: unused
  286. *
  287. * Allow iucv interrupts on this cpu.
  288. */
  289. static void iucv_allow_cpu(void *data)
  290. {
  291. int cpu = smp_processor_id();
  292. union iucv_param *parm;
  293. /*
  294. * Enable all iucv interrupts.
  295. * ipmask contains bits for the different interrupts
  296. * 0x80 - Flag to allow nonpriority message pending interrupts
  297. * 0x40 - Flag to allow priority message pending interrupts
  298. * 0x20 - Flag to allow nonpriority message completion interrupts
  299. * 0x10 - Flag to allow priority message completion interrupts
  300. * 0x08 - Flag to allow IUCV control interrupts
  301. */
  302. parm = percpu_ptr(iucv_param, smp_processor_id());
  303. memset(parm, 0, sizeof(union iucv_param));
  304. parm->set_mask.ipmask = 0xf8;
  305. iucv_call_b2f0(IUCV_SETMASK, parm);
  306. /* Set indication that iucv interrupts are allowed for this cpu. */
  307. cpu_set(cpu, iucv_irq_cpumask);
  308. }
  309. /**
  310. * iucv_block_cpu
  311. * @data: unused
  312. *
  313. * Block iucv interrupts on this cpu.
  314. */
  315. static void iucv_block_cpu(void *data)
  316. {
  317. int cpu = smp_processor_id();
  318. union iucv_param *parm;
  319. /* Disable all iucv interrupts. */
  320. parm = percpu_ptr(iucv_param, smp_processor_id());
  321. memset(parm, 0, sizeof(union iucv_param));
  322. iucv_call_b2f0(IUCV_SETMASK, parm);
  323. /* Clear indication that iucv interrupts are allowed for this cpu. */
  324. cpu_clear(cpu, iucv_irq_cpumask);
  325. }
  326. /**
  327. * iucv_declare_cpu
  328. * @data: unused
  329. *
  330. * Declare a interupt buffer on this cpu.
  331. */
  332. static void iucv_declare_cpu(void *data)
  333. {
  334. int cpu = smp_processor_id();
  335. union iucv_param *parm;
  336. int rc;
  337. if (cpu_isset(cpu, iucv_buffer_cpumask))
  338. return;
  339. /* Declare interrupt buffer. */
  340. parm = percpu_ptr(iucv_param, cpu);
  341. memset(parm, 0, sizeof(union iucv_param));
  342. parm->db.ipbfadr1 = virt_to_phys(percpu_ptr(iucv_irq_data, cpu));
  343. rc = iucv_call_b2f0(IUCV_DECLARE_BUFFER, parm);
  344. if (rc) {
  345. char *err = "Unknown";
  346. switch(rc) {
  347. case 0x03:
  348. err = "Directory error";
  349. break;
  350. case 0x0a:
  351. err = "Invalid length";
  352. break;
  353. case 0x13:
  354. err = "Buffer already exists";
  355. break;
  356. case 0x3e:
  357. err = "Buffer overlap";
  358. break;
  359. case 0x5c:
  360. err = "Paging or storage error";
  361. break;
  362. }
  363. printk(KERN_WARNING "iucv_register: iucv_declare_buffer "
  364. "on cpu %i returned error 0x%02x (%s)\n", cpu, rc, err);
  365. return;
  366. }
  367. /* Set indication that an iucv buffer exists for this cpu. */
  368. cpu_set(cpu, iucv_buffer_cpumask);
  369. if (iucv_nonsmp_handler == 0 || cpus_empty(iucv_irq_cpumask))
  370. /* Enable iucv interrupts on this cpu. */
  371. iucv_allow_cpu(NULL);
  372. else
  373. /* Disable iucv interrupts on this cpu. */
  374. iucv_block_cpu(NULL);
  375. }
  376. /**
  377. * iucv_retrieve_cpu
  378. * @data: unused
  379. *
  380. * Retrieve interrupt buffer on this cpu.
  381. */
  382. static void iucv_retrieve_cpu(void *data)
  383. {
  384. int cpu = smp_processor_id();
  385. union iucv_param *parm;
  386. if (!cpu_isset(cpu, iucv_buffer_cpumask))
  387. return;
  388. /* Block iucv interrupts. */
  389. iucv_block_cpu(NULL);
  390. /* Retrieve interrupt buffer. */
  391. parm = percpu_ptr(iucv_param, cpu);
  392. iucv_call_b2f0(IUCV_RETRIEVE_BUFFER, parm);
  393. /* Clear indication that an iucv buffer exists for this cpu. */
  394. cpu_clear(cpu, iucv_buffer_cpumask);
  395. }
  396. /**
  397. * iucv_setmask_smp
  398. *
  399. * Allow iucv interrupts on all cpus.
  400. */
  401. static void iucv_setmask_mp(void)
  402. {
  403. int cpu;
  404. for_each_online_cpu(cpu)
  405. /* Enable all cpus with a declared buffer. */
  406. if (cpu_isset(cpu, iucv_buffer_cpumask) &&
  407. !cpu_isset(cpu, iucv_irq_cpumask))
  408. smp_call_function_on(iucv_allow_cpu, NULL, 0, 1, cpu);
  409. }
  410. /**
  411. * iucv_setmask_up
  412. *
  413. * Allow iucv interrupts on a single cpus.
  414. */
  415. static void iucv_setmask_up(void)
  416. {
  417. cpumask_t cpumask;
  418. int cpu;
  419. /* Disable all cpu but the first in cpu_irq_cpumask. */
  420. cpumask = iucv_irq_cpumask;
  421. cpu_clear(first_cpu(iucv_irq_cpumask), cpumask);
  422. for_each_cpu_mask(cpu, cpumask)
  423. smp_call_function_on(iucv_block_cpu, NULL, 0, 1, cpu);
  424. }
  425. /**
  426. * iucv_enable
  427. *
  428. * This function makes iucv ready for use. It allocates the pathid
  429. * table, declares an iucv interrupt buffer and enables the iucv
  430. * interrupts. Called when the first user has registered an iucv
  431. * handler.
  432. */
  433. static int iucv_enable(void)
  434. {
  435. size_t alloc_size;
  436. int cpu, rc;
  437. rc = -ENOMEM;
  438. alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
  439. iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
  440. if (!iucv_path_table)
  441. goto out;
  442. /* Declare per cpu buffers. */
  443. rc = -EIO;
  444. for_each_online_cpu(cpu)
  445. smp_call_function_on(iucv_declare_cpu, NULL, 0, 1, cpu);
  446. if (cpus_empty(iucv_buffer_cpumask))
  447. /* No cpu could declare an iucv buffer. */
  448. goto out_path;
  449. return 0;
  450. out_path:
  451. kfree(iucv_path_table);
  452. out:
  453. return rc;
  454. }
  455. /**
  456. * iucv_disable
  457. *
  458. * This function shuts down iucv. It disables iucv interrupts, retrieves
  459. * the iucv interrupt buffer and frees the pathid table. Called after the
  460. * last user unregister its iucv handler.
  461. */
  462. static void iucv_disable(void)
  463. {
  464. on_each_cpu(iucv_retrieve_cpu, NULL, 0, 1);
  465. kfree(iucv_path_table);
  466. }
  467. static int __cpuinit iucv_cpu_notify(struct notifier_block *self,
  468. unsigned long action, void *hcpu)
  469. {
  470. cpumask_t cpumask;
  471. long cpu = (long) hcpu;
  472. switch (action) {
  473. case CPU_UP_PREPARE:
  474. if (!percpu_populate(iucv_irq_data,
  475. sizeof(struct iucv_irq_data),
  476. GFP_KERNEL|GFP_DMA, cpu))
  477. return NOTIFY_BAD;
  478. if (!percpu_populate(iucv_param, sizeof(union iucv_param),
  479. GFP_KERNEL|GFP_DMA, cpu)) {
  480. percpu_depopulate(iucv_irq_data, cpu);
  481. return NOTIFY_BAD;
  482. }
  483. break;
  484. case CPU_UP_CANCELED:
  485. case CPU_DEAD:
  486. percpu_depopulate(iucv_param, cpu);
  487. percpu_depopulate(iucv_irq_data, cpu);
  488. break;
  489. case CPU_ONLINE:
  490. case CPU_DOWN_FAILED:
  491. smp_call_function_on(iucv_declare_cpu, NULL, 0, 1, cpu);
  492. break;
  493. case CPU_DOWN_PREPARE:
  494. cpumask = iucv_buffer_cpumask;
  495. cpu_clear(cpu, cpumask);
  496. if (cpus_empty(cpumask))
  497. /* Can't offline last IUCV enabled cpu. */
  498. return NOTIFY_BAD;
  499. smp_call_function_on(iucv_retrieve_cpu, NULL, 0, 1, cpu);
  500. if (cpus_empty(iucv_irq_cpumask))
  501. smp_call_function_on(iucv_allow_cpu, NULL, 0, 1,
  502. first_cpu(iucv_buffer_cpumask));
  503. break;
  504. }
  505. return NOTIFY_OK;
  506. }
  507. static struct notifier_block iucv_cpu_notifier = {
  508. .notifier_call = iucv_cpu_notify,
  509. };
  510. /**
  511. * iucv_sever_pathid
  512. * @pathid: path identification number.
  513. * @userdata: 16-bytes of user data.
  514. *
  515. * Sever an iucv path to free up the pathid. Used internally.
  516. */
  517. static int iucv_sever_pathid(u16 pathid, u8 userdata[16])
  518. {
  519. union iucv_param *parm;
  520. parm = percpu_ptr(iucv_param, smp_processor_id());
  521. memset(parm, 0, sizeof(union iucv_param));
  522. if (userdata)
  523. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  524. parm->ctrl.ippathid = pathid;
  525. return iucv_call_b2f0(IUCV_SEVER, parm);
  526. }
  527. /**
  528. * __iucv_cleanup_pathid
  529. * @dummy: unused dummy argument
  530. *
  531. * Nop function called via smp_call_function to force work items from
  532. * pending external iucv interrupts to the work queue.
  533. */
  534. static void __iucv_cleanup_pathid(void *dummy)
  535. {
  536. }
  537. /**
  538. * iucv_cleanup_pathid
  539. * @pathid: 16 bit pathid
  540. *
  541. * Function called after a path has been severed to find all remaining
  542. * work items for the now stale pathid. The caller needs to hold the
  543. * iucv_table_lock.
  544. */
  545. static void iucv_cleanup_pathid(u16 pathid)
  546. {
  547. struct iucv_work *p, *n;
  548. /*
  549. * Path is severed, the pathid can be reused immediatly on
  550. * a iucv connect or a connection pending interrupt.
  551. * iucv_path_connect and connection pending interrupt will
  552. * wait until the iucv_table_lock is released before the
  553. * recycled pathid enters the system.
  554. * Force remaining interrupts to the work queue, then
  555. * scan the work queue for items of this path.
  556. */
  557. smp_call_function(__iucv_cleanup_pathid, NULL, 0, 1);
  558. spin_lock_irq(&iucv_work_lock);
  559. list_for_each_entry_safe(p, n, &iucv_work_queue, list) {
  560. /* Remove work items for pathid except connection pending */
  561. if (p->data.ippathid == pathid && p->data.iptype != 0x01) {
  562. list_del(&p->list);
  563. kfree(p);
  564. }
  565. }
  566. spin_unlock_irq(&iucv_work_lock);
  567. }
  568. /**
  569. * iucv_register:
  570. * @handler: address of iucv handler structure
  571. * @smp: != 0 indicates that the handler can deal with out of order messages
  572. *
  573. * Registers a driver with IUCV.
  574. *
  575. * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
  576. * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
  577. */
  578. int iucv_register(struct iucv_handler *handler, int smp)
  579. {
  580. int rc;
  581. if (!iucv_available)
  582. return -ENOSYS;
  583. mutex_lock(&iucv_register_mutex);
  584. if (!smp)
  585. iucv_nonsmp_handler++;
  586. if (list_empty(&iucv_handler_list)) {
  587. rc = iucv_enable();
  588. if (rc)
  589. goto out_mutex;
  590. } else if (!smp && iucv_nonsmp_handler == 1)
  591. iucv_setmask_up();
  592. INIT_LIST_HEAD(&handler->paths);
  593. spin_lock_irq(&iucv_table_lock);
  594. list_add_tail(&handler->list, &iucv_handler_list);
  595. spin_unlock_irq(&iucv_table_lock);
  596. rc = 0;
  597. out_mutex:
  598. mutex_unlock(&iucv_register_mutex);
  599. return rc;
  600. }
  601. /**
  602. * iucv_unregister
  603. * @handler: address of iucv handler structure
  604. * @smp: != 0 indicates that the handler can deal with out of order messages
  605. *
  606. * Unregister driver from IUCV.
  607. */
  608. void iucv_unregister(struct iucv_handler *handler, int smp)
  609. {
  610. struct iucv_path *p, *n;
  611. mutex_lock(&iucv_register_mutex);
  612. spin_lock_bh(&iucv_table_lock);
  613. /* Remove handler from the iucv_handler_list. */
  614. list_del_init(&handler->list);
  615. /* Sever all pathids still refering to the handler. */
  616. list_for_each_entry_safe(p, n, &handler->paths, list) {
  617. iucv_sever_pathid(p->pathid, NULL);
  618. iucv_path_table[p->pathid] = NULL;
  619. list_del(&p->list);
  620. iucv_cleanup_pathid(p->pathid);
  621. iucv_path_free(p);
  622. }
  623. spin_unlock_bh(&iucv_table_lock);
  624. if (!smp)
  625. iucv_nonsmp_handler--;
  626. if (list_empty(&iucv_handler_list))
  627. iucv_disable();
  628. else if (!smp && iucv_nonsmp_handler == 0)
  629. iucv_setmask_mp();
  630. mutex_unlock(&iucv_register_mutex);
  631. }
  632. /**
  633. * iucv_path_accept
  634. * @path: address of iucv path structure
  635. * @handler: address of iucv handler structure
  636. * @userdata: 16 bytes of data reflected to the communication partner
  637. * @private: private data passed to interrupt handlers for this path
  638. *
  639. * This function is issued after the user received a connection pending
  640. * external interrupt and now wishes to complete the IUCV communication path.
  641. *
  642. * Returns the result of the CP IUCV call.
  643. */
  644. int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
  645. u8 userdata[16], void *private)
  646. {
  647. union iucv_param *parm;
  648. int rc;
  649. local_bh_disable();
  650. /* Prepare parameter block. */
  651. parm = percpu_ptr(iucv_param, smp_processor_id());
  652. memset(parm, 0, sizeof(union iucv_param));
  653. parm->ctrl.ippathid = path->pathid;
  654. parm->ctrl.ipmsglim = path->msglim;
  655. if (userdata)
  656. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  657. parm->ctrl.ipflags1 = path->flags;
  658. rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
  659. if (!rc) {
  660. path->private = private;
  661. path->msglim = parm->ctrl.ipmsglim;
  662. path->flags = parm->ctrl.ipflags1;
  663. }
  664. local_bh_enable();
  665. return rc;
  666. }
  667. /**
  668. * iucv_path_connect
  669. * @path: address of iucv path structure
  670. * @handler: address of iucv handler structure
  671. * @userid: 8-byte user identification
  672. * @system: 8-byte target system identification
  673. * @userdata: 16 bytes of data reflected to the communication partner
  674. * @private: private data passed to interrupt handlers for this path
  675. *
  676. * This function establishes an IUCV path. Although the connect may complete
  677. * successfully, you are not able to use the path until you receive an IUCV
  678. * Connection Complete external interrupt.
  679. *
  680. * Returns the result of the CP IUCV call.
  681. */
  682. int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
  683. u8 userid[8], u8 system[8], u8 userdata[16],
  684. void *private)
  685. {
  686. union iucv_param *parm;
  687. int rc;
  688. preempt_disable();
  689. if (iucv_tasklet_cpu != smp_processor_id())
  690. spin_lock_bh(&iucv_table_lock);
  691. parm = percpu_ptr(iucv_param, smp_processor_id());
  692. memset(parm, 0, sizeof(union iucv_param));
  693. parm->ctrl.ipmsglim = path->msglim;
  694. parm->ctrl.ipflags1 = path->flags;
  695. if (userid) {
  696. memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
  697. ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
  698. EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
  699. }
  700. if (system) {
  701. memcpy(parm->ctrl.iptarget, system,
  702. sizeof(parm->ctrl.iptarget));
  703. ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
  704. EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
  705. }
  706. if (userdata)
  707. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  708. rc = iucv_call_b2f0(IUCV_CONNECT, parm);
  709. if (!rc) {
  710. if (parm->ctrl.ippathid < iucv_max_pathid) {
  711. path->pathid = parm->ctrl.ippathid;
  712. path->msglim = parm->ctrl.ipmsglim;
  713. path->flags = parm->ctrl.ipflags1;
  714. path->handler = handler;
  715. path->private = private;
  716. list_add_tail(&path->list, &handler->paths);
  717. iucv_path_table[path->pathid] = path;
  718. } else {
  719. iucv_sever_pathid(parm->ctrl.ippathid,
  720. iucv_error_pathid);
  721. rc = -EIO;
  722. }
  723. }
  724. if (iucv_tasklet_cpu != smp_processor_id())
  725. spin_unlock_bh(&iucv_table_lock);
  726. preempt_enable();
  727. return rc;
  728. }
  729. /**
  730. * iucv_path_quiesce:
  731. * @path: address of iucv path structure
  732. * @userdata: 16 bytes of data reflected to the communication partner
  733. *
  734. * This function temporarily suspends incoming messages on an IUCV path.
  735. * You can later reactivate the path by invoking the iucv_resume function.
  736. *
  737. * Returns the result from the CP IUCV call.
  738. */
  739. int iucv_path_quiesce(struct iucv_path *path, u8 userdata[16])
  740. {
  741. union iucv_param *parm;
  742. int rc;
  743. local_bh_disable();
  744. parm = percpu_ptr(iucv_param, smp_processor_id());
  745. memset(parm, 0, sizeof(union iucv_param));
  746. if (userdata)
  747. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  748. parm->ctrl.ippathid = path->pathid;
  749. rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
  750. local_bh_enable();
  751. return rc;
  752. }
  753. /**
  754. * iucv_path_resume:
  755. * @path: address of iucv path structure
  756. * @userdata: 16 bytes of data reflected to the communication partner
  757. *
  758. * This function resumes incoming messages on an IUCV path that has
  759. * been stopped with iucv_path_quiesce.
  760. *
  761. * Returns the result from the CP IUCV call.
  762. */
  763. int iucv_path_resume(struct iucv_path *path, u8 userdata[16])
  764. {
  765. union iucv_param *parm;
  766. int rc;
  767. local_bh_disable();
  768. parm = percpu_ptr(iucv_param, smp_processor_id());
  769. memset(parm, 0, sizeof(union iucv_param));
  770. if (userdata)
  771. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  772. parm->ctrl.ippathid = path->pathid;
  773. rc = iucv_call_b2f0(IUCV_RESUME, parm);
  774. local_bh_enable();
  775. return rc;
  776. }
  777. /**
  778. * iucv_path_sever
  779. * @path: address of iucv path structure
  780. * @userdata: 16 bytes of data reflected to the communication partner
  781. *
  782. * This function terminates an IUCV path.
  783. *
  784. * Returns the result from the CP IUCV call.
  785. */
  786. int iucv_path_sever(struct iucv_path *path, u8 userdata[16])
  787. {
  788. int rc;
  789. preempt_disable();
  790. if (iucv_tasklet_cpu != smp_processor_id())
  791. spin_lock_bh(&iucv_table_lock);
  792. rc = iucv_sever_pathid(path->pathid, userdata);
  793. if (!rc) {
  794. iucv_path_table[path->pathid] = NULL;
  795. list_del_init(&path->list);
  796. iucv_cleanup_pathid(path->pathid);
  797. }
  798. if (iucv_tasklet_cpu != smp_processor_id())
  799. spin_unlock_bh(&iucv_table_lock);
  800. preempt_enable();
  801. return rc;
  802. }
  803. /**
  804. * iucv_message_purge
  805. * @path: address of iucv path structure
  806. * @msg: address of iucv msg structure
  807. * @srccls: source class of message
  808. *
  809. * Cancels a message you have sent.
  810. *
  811. * Returns the result from the CP IUCV call.
  812. */
  813. int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
  814. u32 srccls)
  815. {
  816. union iucv_param *parm;
  817. int rc;
  818. local_bh_disable();
  819. parm = percpu_ptr(iucv_param, smp_processor_id());
  820. memset(parm, 0, sizeof(union iucv_param));
  821. parm->purge.ippathid = path->pathid;
  822. parm->purge.ipmsgid = msg->id;
  823. parm->purge.ipsrccls = srccls;
  824. parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
  825. rc = iucv_call_b2f0(IUCV_PURGE, parm);
  826. if (!rc) {
  827. msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
  828. msg->tag = parm->purge.ipmsgtag;
  829. }
  830. local_bh_enable();
  831. return rc;
  832. }
  833. /**
  834. * iucv_message_receive
  835. * @path: address of iucv path structure
  836. * @msg: address of iucv msg structure
  837. * @flags: how the message is received (IUCV_IPBUFLST)
  838. * @buffer: address of data buffer or address of struct iucv_array
  839. * @size: length of data buffer
  840. * @residual:
  841. *
  842. * This function receives messages that are being sent to you over
  843. * established paths. This function will deal with RMDATA messages
  844. * embedded in struct iucv_message as well.
  845. *
  846. * Returns the result from the CP IUCV call.
  847. */
  848. int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
  849. u8 flags, void *buffer, size_t size, size_t *residual)
  850. {
  851. union iucv_param *parm;
  852. struct iucv_array *array;
  853. u8 *rmmsg;
  854. size_t copy;
  855. int rc;
  856. if (msg->flags & IUCV_IPRMDATA) {
  857. /*
  858. * Message is 8 bytes long and has been stored to the
  859. * message descriptor itself.
  860. */
  861. rc = (size < 8) ? 5 : 0;
  862. if (residual)
  863. *residual = abs(size - 8);
  864. rmmsg = msg->rmmsg;
  865. if (flags & IUCV_IPBUFLST) {
  866. /* Copy to struct iucv_array. */
  867. size = (size < 8) ? size : 8;
  868. for (array = buffer; size > 0; array++) {
  869. copy = min_t(size_t, size, array->length);
  870. memcpy((u8 *)(addr_t) array->address,
  871. rmmsg, copy);
  872. rmmsg += copy;
  873. size -= copy;
  874. }
  875. } else {
  876. /* Copy to direct buffer. */
  877. memcpy(buffer, rmmsg, min_t(size_t, size, 8));
  878. }
  879. return 0;
  880. }
  881. local_bh_disable();
  882. parm = percpu_ptr(iucv_param, smp_processor_id());
  883. memset(parm, 0, sizeof(union iucv_param));
  884. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  885. parm->db.ipbfln1f = (u32) size;
  886. parm->db.ipmsgid = msg->id;
  887. parm->db.ippathid = path->pathid;
  888. parm->db.iptrgcls = msg->class;
  889. parm->db.ipflags1 = (flags | IUCV_IPFGPID |
  890. IUCV_IPFGMID | IUCV_IPTRGCLS);
  891. rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
  892. if (!rc || rc == 5) {
  893. msg->flags = parm->db.ipflags1;
  894. if (residual)
  895. *residual = parm->db.ipbfln1f;
  896. }
  897. local_bh_enable();
  898. return rc;
  899. }
  900. /**
  901. * iucv_message_reject
  902. * @path: address of iucv path structure
  903. * @msg: address of iucv msg structure
  904. *
  905. * The reject function refuses a specified message. Between the time you
  906. * are notified of a message and the time that you complete the message,
  907. * the message may be rejected.
  908. *
  909. * Returns the result from the CP IUCV call.
  910. */
  911. int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
  912. {
  913. union iucv_param *parm;
  914. int rc;
  915. local_bh_disable();
  916. parm = percpu_ptr(iucv_param, smp_processor_id());
  917. memset(parm, 0, sizeof(union iucv_param));
  918. parm->db.ippathid = path->pathid;
  919. parm->db.ipmsgid = msg->id;
  920. parm->db.iptrgcls = msg->class;
  921. parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
  922. rc = iucv_call_b2f0(IUCV_REJECT, parm);
  923. local_bh_enable();
  924. return rc;
  925. }
  926. /**
  927. * iucv_message_reply
  928. * @path: address of iucv path structure
  929. * @msg: address of iucv msg structure
  930. * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
  931. * @reply: address of reply data buffer or address of struct iucv_array
  932. * @size: length of reply data buffer
  933. *
  934. * This function responds to the two-way messages that you receive. You
  935. * must identify completely the message to which you wish to reply. ie,
  936. * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
  937. * the parameter list.
  938. *
  939. * Returns the result from the CP IUCV call.
  940. */
  941. int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
  942. u8 flags, void *reply, size_t size)
  943. {
  944. union iucv_param *parm;
  945. int rc;
  946. local_bh_disable();
  947. parm = percpu_ptr(iucv_param, smp_processor_id());
  948. memset(parm, 0, sizeof(union iucv_param));
  949. if (flags & IUCV_IPRMDATA) {
  950. parm->dpl.ippathid = path->pathid;
  951. parm->dpl.ipflags1 = flags;
  952. parm->dpl.ipmsgid = msg->id;
  953. parm->dpl.iptrgcls = msg->class;
  954. memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
  955. } else {
  956. parm->db.ipbfadr1 = (u32)(addr_t) reply;
  957. parm->db.ipbfln1f = (u32) size;
  958. parm->db.ippathid = path->pathid;
  959. parm->db.ipflags1 = flags;
  960. parm->db.ipmsgid = msg->id;
  961. parm->db.iptrgcls = msg->class;
  962. }
  963. rc = iucv_call_b2f0(IUCV_REPLY, parm);
  964. local_bh_enable();
  965. return rc;
  966. }
  967. /**
  968. * iucv_message_send
  969. * @path: address of iucv path structure
  970. * @msg: address of iucv msg structure
  971. * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
  972. * @srccls: source class of message
  973. * @buffer: address of send buffer or address of struct iucv_array
  974. * @size: length of send buffer
  975. *
  976. * This function transmits data to another application. Data to be
  977. * transmitted is in a buffer and this is a one-way message and the
  978. * receiver will not reply to the message.
  979. *
  980. * Returns the result from the CP IUCV call.
  981. */
  982. int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
  983. u8 flags, u32 srccls, void *buffer, size_t size)
  984. {
  985. union iucv_param *parm;
  986. int rc;
  987. local_bh_disable();
  988. parm = percpu_ptr(iucv_param, smp_processor_id());
  989. memset(parm, 0, sizeof(union iucv_param));
  990. if (flags & IUCV_IPRMDATA) {
  991. /* Message of 8 bytes can be placed into the parameter list. */
  992. parm->dpl.ippathid = path->pathid;
  993. parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
  994. parm->dpl.iptrgcls = msg->class;
  995. parm->dpl.ipsrccls = srccls;
  996. parm->dpl.ipmsgtag = msg->tag;
  997. memcpy(parm->dpl.iprmmsg, buffer, 8);
  998. } else {
  999. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  1000. parm->db.ipbfln1f = (u32) size;
  1001. parm->db.ippathid = path->pathid;
  1002. parm->db.ipflags1 = flags | IUCV_IPNORPY;
  1003. parm->db.iptrgcls = msg->class;
  1004. parm->db.ipsrccls = srccls;
  1005. parm->db.ipmsgtag = msg->tag;
  1006. }
  1007. rc = iucv_call_b2f0(IUCV_SEND, parm);
  1008. if (!rc)
  1009. msg->id = parm->db.ipmsgid;
  1010. local_bh_enable();
  1011. return rc;
  1012. }
  1013. /**
  1014. * iucv_message_send2way
  1015. * @path: address of iucv path structure
  1016. * @msg: address of iucv msg structure
  1017. * @flags: how the message is sent and the reply is received
  1018. * (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
  1019. * @srccls: source class of message
  1020. * @buffer: address of send buffer or address of struct iucv_array
  1021. * @size: length of send buffer
  1022. * @ansbuf: address of answer buffer or address of struct iucv_array
  1023. * @asize: size of reply buffer
  1024. *
  1025. * This function transmits data to another application. Data to be
  1026. * transmitted is in a buffer. The receiver of the send is expected to
  1027. * reply to the message and a buffer is provided into which IUCV moves
  1028. * the reply to this message.
  1029. *
  1030. * Returns the result from the CP IUCV call.
  1031. */
  1032. int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
  1033. u8 flags, u32 srccls, void *buffer, size_t size,
  1034. void *answer, size_t asize, size_t *residual)
  1035. {
  1036. union iucv_param *parm;
  1037. int rc;
  1038. local_bh_disable();
  1039. parm = percpu_ptr(iucv_param, smp_processor_id());
  1040. memset(parm, 0, sizeof(union iucv_param));
  1041. if (flags & IUCV_IPRMDATA) {
  1042. parm->dpl.ippathid = path->pathid;
  1043. parm->dpl.ipflags1 = path->flags; /* priority message */
  1044. parm->dpl.iptrgcls = msg->class;
  1045. parm->dpl.ipsrccls = srccls;
  1046. parm->dpl.ipmsgtag = msg->tag;
  1047. parm->dpl.ipbfadr2 = (u32)(addr_t) answer;
  1048. parm->dpl.ipbfln2f = (u32) asize;
  1049. memcpy(parm->dpl.iprmmsg, buffer, 8);
  1050. } else {
  1051. parm->db.ippathid = path->pathid;
  1052. parm->db.ipflags1 = path->flags; /* priority message */
  1053. parm->db.iptrgcls = msg->class;
  1054. parm->db.ipsrccls = srccls;
  1055. parm->db.ipmsgtag = msg->tag;
  1056. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  1057. parm->db.ipbfln1f = (u32) size;
  1058. parm->db.ipbfadr2 = (u32)(addr_t) answer;
  1059. parm->db.ipbfln2f = (u32) asize;
  1060. }
  1061. rc = iucv_call_b2f0(IUCV_SEND, parm);
  1062. if (!rc)
  1063. msg->id = parm->db.ipmsgid;
  1064. local_bh_enable();
  1065. return rc;
  1066. }
  1067. /**
  1068. * iucv_path_pending
  1069. * @data: Pointer to external interrupt buffer
  1070. *
  1071. * Process connection pending work item. Called from tasklet while holding
  1072. * iucv_table_lock.
  1073. */
  1074. struct iucv_path_pending {
  1075. u16 ippathid;
  1076. u8 ipflags1;
  1077. u8 iptype;
  1078. u16 ipmsglim;
  1079. u16 res1;
  1080. u8 ipvmid[8];
  1081. u8 ipuser[16];
  1082. u32 res3;
  1083. u8 ippollfg;
  1084. u8 res4[3];
  1085. } __attribute__ ((packed));
  1086. static void iucv_path_pending(struct iucv_irq_data *data)
  1087. {
  1088. struct iucv_path_pending *ipp = (void *) data;
  1089. struct iucv_handler *handler;
  1090. struct iucv_path *path;
  1091. char *error;
  1092. BUG_ON(iucv_path_table[ipp->ippathid]);
  1093. /* New pathid, handler found. Create a new path struct. */
  1094. error = iucv_error_no_memory;
  1095. path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
  1096. if (!path)
  1097. goto out_sever;
  1098. path->pathid = ipp->ippathid;
  1099. iucv_path_table[path->pathid] = path;
  1100. EBCASC(ipp->ipvmid, 8);
  1101. /* Call registered handler until one is found that wants the path. */
  1102. list_for_each_entry(handler, &iucv_handler_list, list) {
  1103. if (!handler->path_pending)
  1104. continue;
  1105. /*
  1106. * Add path to handler to allow a call to iucv_path_sever
  1107. * inside the path_pending function. If the handler returns
  1108. * an error remove the path from the handler again.
  1109. */
  1110. list_add(&path->list, &handler->paths);
  1111. path->handler = handler;
  1112. if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
  1113. return;
  1114. list_del(&path->list);
  1115. path->handler = NULL;
  1116. }
  1117. /* No handler wanted the path. */
  1118. iucv_path_table[path->pathid] = NULL;
  1119. iucv_path_free(path);
  1120. error = iucv_error_no_listener;
  1121. out_sever:
  1122. iucv_sever_pathid(ipp->ippathid, error);
  1123. }
  1124. /**
  1125. * iucv_path_complete
  1126. * @data: Pointer to external interrupt buffer
  1127. *
  1128. * Process connection complete work item. Called from tasklet while holding
  1129. * iucv_table_lock.
  1130. */
  1131. struct iucv_path_complete {
  1132. u16 ippathid;
  1133. u8 ipflags1;
  1134. u8 iptype;
  1135. u16 ipmsglim;
  1136. u16 res1;
  1137. u8 res2[8];
  1138. u8 ipuser[16];
  1139. u32 res3;
  1140. u8 ippollfg;
  1141. u8 res4[3];
  1142. } __attribute__ ((packed));
  1143. static void iucv_path_complete(struct iucv_irq_data *data)
  1144. {
  1145. struct iucv_path_complete *ipc = (void *) data;
  1146. struct iucv_path *path = iucv_path_table[ipc->ippathid];
  1147. BUG_ON(!path || !path->handler);
  1148. if (path->handler->path_complete)
  1149. path->handler->path_complete(path, ipc->ipuser);
  1150. }
  1151. /**
  1152. * iucv_path_severed
  1153. * @data: Pointer to external interrupt buffer
  1154. *
  1155. * Process connection severed work item. Called from tasklet while holding
  1156. * iucv_table_lock.
  1157. */
  1158. struct iucv_path_severed {
  1159. u16 ippathid;
  1160. u8 res1;
  1161. u8 iptype;
  1162. u32 res2;
  1163. u8 res3[8];
  1164. u8 ipuser[16];
  1165. u32 res4;
  1166. u8 ippollfg;
  1167. u8 res5[3];
  1168. } __attribute__ ((packed));
  1169. static void iucv_path_severed(struct iucv_irq_data *data)
  1170. {
  1171. struct iucv_path_severed *ips = (void *) data;
  1172. struct iucv_path *path = iucv_path_table[ips->ippathid];
  1173. BUG_ON(!path || !path->handler);
  1174. if (path->handler->path_severed)
  1175. path->handler->path_severed(path, ips->ipuser);
  1176. else {
  1177. iucv_sever_pathid(path->pathid, NULL);
  1178. iucv_path_table[path->pathid] = NULL;
  1179. list_del_init(&path->list);
  1180. iucv_cleanup_pathid(path->pathid);
  1181. iucv_path_free(path);
  1182. }
  1183. }
  1184. /**
  1185. * iucv_path_quiesced
  1186. * @data: Pointer to external interrupt buffer
  1187. *
  1188. * Process connection quiesced work item. Called from tasklet while holding
  1189. * iucv_table_lock.
  1190. */
  1191. struct iucv_path_quiesced {
  1192. u16 ippathid;
  1193. u8 res1;
  1194. u8 iptype;
  1195. u32 res2;
  1196. u8 res3[8];
  1197. u8 ipuser[16];
  1198. u32 res4;
  1199. u8 ippollfg;
  1200. u8 res5[3];
  1201. } __attribute__ ((packed));
  1202. static void iucv_path_quiesced(struct iucv_irq_data *data)
  1203. {
  1204. struct iucv_path_quiesced *ipq = (void *) data;
  1205. struct iucv_path *path = iucv_path_table[ipq->ippathid];
  1206. BUG_ON(!path || !path->handler);
  1207. if (path->handler->path_quiesced)
  1208. path->handler->path_quiesced(path, ipq->ipuser);
  1209. }
  1210. /**
  1211. * iucv_path_resumed
  1212. * @data: Pointer to external interrupt buffer
  1213. *
  1214. * Process connection resumed work item. Called from tasklet while holding
  1215. * iucv_table_lock.
  1216. */
  1217. struct iucv_path_resumed {
  1218. u16 ippathid;
  1219. u8 res1;
  1220. u8 iptype;
  1221. u32 res2;
  1222. u8 res3[8];
  1223. u8 ipuser[16];
  1224. u32 res4;
  1225. u8 ippollfg;
  1226. u8 res5[3];
  1227. } __attribute__ ((packed));
  1228. static void iucv_path_resumed(struct iucv_irq_data *data)
  1229. {
  1230. struct iucv_path_resumed *ipr = (void *) data;
  1231. struct iucv_path *path = iucv_path_table[ipr->ippathid];
  1232. BUG_ON(!path || !path->handler);
  1233. if (path->handler->path_resumed)
  1234. path->handler->path_resumed(path, ipr->ipuser);
  1235. }
  1236. /**
  1237. * iucv_message_complete
  1238. * @data: Pointer to external interrupt buffer
  1239. *
  1240. * Process message complete work item. Called from tasklet while holding
  1241. * iucv_table_lock.
  1242. */
  1243. struct iucv_message_complete {
  1244. u16 ippathid;
  1245. u8 ipflags1;
  1246. u8 iptype;
  1247. u32 ipmsgid;
  1248. u32 ipaudit;
  1249. u8 iprmmsg[8];
  1250. u32 ipsrccls;
  1251. u32 ipmsgtag;
  1252. u32 res;
  1253. u32 ipbfln2f;
  1254. u8 ippollfg;
  1255. u8 res2[3];
  1256. } __attribute__ ((packed));
  1257. static void iucv_message_complete(struct iucv_irq_data *data)
  1258. {
  1259. struct iucv_message_complete *imc = (void *) data;
  1260. struct iucv_path *path = iucv_path_table[imc->ippathid];
  1261. struct iucv_message msg;
  1262. BUG_ON(!path || !path->handler);
  1263. if (path->handler->message_complete) {
  1264. msg.flags = imc->ipflags1;
  1265. msg.id = imc->ipmsgid;
  1266. msg.audit = imc->ipaudit;
  1267. memcpy(msg.rmmsg, imc->iprmmsg, 8);
  1268. msg.class = imc->ipsrccls;
  1269. msg.tag = imc->ipmsgtag;
  1270. msg.length = imc->ipbfln2f;
  1271. path->handler->message_complete(path, &msg);
  1272. }
  1273. }
  1274. /**
  1275. * iucv_message_pending
  1276. * @data: Pointer to external interrupt buffer
  1277. *
  1278. * Process message pending work item. Called from tasklet while holding
  1279. * iucv_table_lock.
  1280. */
  1281. struct iucv_message_pending {
  1282. u16 ippathid;
  1283. u8 ipflags1;
  1284. u8 iptype;
  1285. u32 ipmsgid;
  1286. u32 iptrgcls;
  1287. union {
  1288. u32 iprmmsg1_u32;
  1289. u8 iprmmsg1[4];
  1290. } ln1msg1;
  1291. union {
  1292. u32 ipbfln1f;
  1293. u8 iprmmsg2[4];
  1294. } ln1msg2;
  1295. u32 res1[3];
  1296. u32 ipbfln2f;
  1297. u8 ippollfg;
  1298. u8 res2[3];
  1299. } __attribute__ ((packed));
  1300. static void iucv_message_pending(struct iucv_irq_data *data)
  1301. {
  1302. struct iucv_message_pending *imp = (void *) data;
  1303. struct iucv_path *path = iucv_path_table[imp->ippathid];
  1304. struct iucv_message msg;
  1305. BUG_ON(!path || !path->handler);
  1306. if (path->handler->message_pending) {
  1307. msg.flags = imp->ipflags1;
  1308. msg.id = imp->ipmsgid;
  1309. msg.class = imp->iptrgcls;
  1310. if (imp->ipflags1 & IUCV_IPRMDATA) {
  1311. memcpy(msg.rmmsg, imp->ln1msg1.iprmmsg1, 8);
  1312. msg.length = 8;
  1313. } else
  1314. msg.length = imp->ln1msg2.ipbfln1f;
  1315. msg.reply_size = imp->ipbfln2f;
  1316. path->handler->message_pending(path, &msg);
  1317. }
  1318. }
  1319. /**
  1320. * iucv_tasklet_handler:
  1321. *
  1322. * This tasklet loops over the queue of irq buffers created by
  1323. * iucv_external_interrupt, calls the appropriate action handler
  1324. * and then frees the buffer.
  1325. */
  1326. static void iucv_tasklet_handler(unsigned long ignored)
  1327. {
  1328. typedef void iucv_irq_fn(struct iucv_irq_data *);
  1329. static iucv_irq_fn *irq_fn[] = {
  1330. [0x01] = iucv_path_pending,
  1331. [0x02] = iucv_path_complete,
  1332. [0x03] = iucv_path_severed,
  1333. [0x04] = iucv_path_quiesced,
  1334. [0x05] = iucv_path_resumed,
  1335. [0x06] = iucv_message_complete,
  1336. [0x07] = iucv_message_complete,
  1337. [0x08] = iucv_message_pending,
  1338. [0x09] = iucv_message_pending,
  1339. };
  1340. struct iucv_work *p;
  1341. /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
  1342. spin_lock(&iucv_table_lock);
  1343. iucv_tasklet_cpu = smp_processor_id();
  1344. spin_lock_irq(&iucv_work_lock);
  1345. while (!list_empty(&iucv_work_queue)) {
  1346. p = list_entry(iucv_work_queue.next, struct iucv_work, list);
  1347. list_del_init(&p->list);
  1348. spin_unlock_irq(&iucv_work_lock);
  1349. irq_fn[p->data.iptype](&p->data);
  1350. kfree(p);
  1351. spin_lock_irq(&iucv_work_lock);
  1352. }
  1353. spin_unlock_irq(&iucv_work_lock);
  1354. iucv_tasklet_cpu = -1;
  1355. spin_unlock(&iucv_table_lock);
  1356. }
  1357. /**
  1358. * iucv_external_interrupt
  1359. * @code: irq code
  1360. *
  1361. * Handles external interrupts coming in from CP.
  1362. * Places the interrupt buffer on a queue and schedules iucv_tasklet_handler().
  1363. */
  1364. static void iucv_external_interrupt(u16 code)
  1365. {
  1366. struct iucv_irq_data *p;
  1367. struct iucv_work *work;
  1368. p = percpu_ptr(iucv_irq_data, smp_processor_id());
  1369. if (p->ippathid >= iucv_max_pathid) {
  1370. printk(KERN_WARNING "iucv_do_int: Got interrupt with "
  1371. "pathid %d > max_connections (%ld)\n",
  1372. p->ippathid, iucv_max_pathid - 1);
  1373. iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
  1374. return;
  1375. }
  1376. if (p->iptype < 0x01 || p->iptype > 0x09) {
  1377. printk(KERN_ERR "iucv_do_int: unknown iucv interrupt\n");
  1378. return;
  1379. }
  1380. work = kmalloc(sizeof(struct iucv_work), GFP_ATOMIC);
  1381. if (!work) {
  1382. printk(KERN_WARNING "iucv_external_interrupt: out of memory\n");
  1383. return;
  1384. }
  1385. memcpy(&work->data, p, sizeof(work->data));
  1386. spin_lock(&iucv_work_lock);
  1387. list_add_tail(&work->list, &iucv_work_queue);
  1388. spin_unlock(&iucv_work_lock);
  1389. tasklet_schedule(&iucv_tasklet);
  1390. }
  1391. /**
  1392. * iucv_init
  1393. *
  1394. * Allocates and initializes various data structures.
  1395. */
  1396. static int iucv_init(void)
  1397. {
  1398. int rc;
  1399. if (!MACHINE_IS_VM) {
  1400. rc = -EPROTONOSUPPORT;
  1401. goto out;
  1402. }
  1403. rc = iucv_query_maxconn();
  1404. if (rc)
  1405. goto out;
  1406. rc = register_external_interrupt (0x4000, iucv_external_interrupt);
  1407. if (rc)
  1408. goto out;
  1409. rc = bus_register(&iucv_bus);
  1410. if (rc)
  1411. goto out_int;
  1412. iucv_root = s390_root_dev_register("iucv");
  1413. if (IS_ERR(iucv_root)) {
  1414. rc = PTR_ERR(iucv_root);
  1415. goto out_bus;
  1416. }
  1417. /* Note: GFP_DMA used used to get memory below 2G */
  1418. iucv_irq_data = percpu_alloc(sizeof(struct iucv_irq_data),
  1419. GFP_KERNEL|GFP_DMA);
  1420. if (!iucv_irq_data) {
  1421. rc = -ENOMEM;
  1422. goto out_root;
  1423. }
  1424. /* Allocate parameter blocks. */
  1425. iucv_param = percpu_alloc(sizeof(union iucv_param),
  1426. GFP_KERNEL|GFP_DMA);
  1427. if (!iucv_param) {
  1428. rc = -ENOMEM;
  1429. goto out_extint;
  1430. }
  1431. register_hotcpu_notifier(&iucv_cpu_notifier);
  1432. ASCEBC(iucv_error_no_listener, 16);
  1433. ASCEBC(iucv_error_no_memory, 16);
  1434. ASCEBC(iucv_error_pathid, 16);
  1435. iucv_available = 1;
  1436. return 0;
  1437. out_extint:
  1438. percpu_free(iucv_irq_data);
  1439. out_root:
  1440. s390_root_dev_unregister(iucv_root);
  1441. out_bus:
  1442. bus_unregister(&iucv_bus);
  1443. out_int:
  1444. unregister_external_interrupt(0x4000, iucv_external_interrupt);
  1445. out:
  1446. return rc;
  1447. }
  1448. /**
  1449. * iucv_exit
  1450. *
  1451. * Frees everything allocated from iucv_init.
  1452. */
  1453. static void iucv_exit(void)
  1454. {
  1455. struct iucv_work *p, *n;
  1456. spin_lock_irq(&iucv_work_lock);
  1457. list_for_each_entry_safe(p, n, &iucv_work_queue, list)
  1458. kfree(p);
  1459. spin_unlock_irq(&iucv_work_lock);
  1460. unregister_hotcpu_notifier(&iucv_cpu_notifier);
  1461. percpu_free(iucv_param);
  1462. percpu_free(iucv_irq_data);
  1463. s390_root_dev_unregister(iucv_root);
  1464. bus_unregister(&iucv_bus);
  1465. unregister_external_interrupt(0x4000, iucv_external_interrupt);
  1466. }
  1467. subsys_initcall(iucv_init);
  1468. module_exit(iucv_exit);
  1469. /**
  1470. * Export all public stuff
  1471. */
  1472. EXPORT_SYMBOL (iucv_bus);
  1473. EXPORT_SYMBOL (iucv_root);
  1474. EXPORT_SYMBOL (iucv_register);
  1475. EXPORT_SYMBOL (iucv_unregister);
  1476. EXPORT_SYMBOL (iucv_path_accept);
  1477. EXPORT_SYMBOL (iucv_path_connect);
  1478. EXPORT_SYMBOL (iucv_path_quiesce);
  1479. EXPORT_SYMBOL (iucv_path_sever);
  1480. EXPORT_SYMBOL (iucv_message_purge);
  1481. EXPORT_SYMBOL (iucv_message_receive);
  1482. EXPORT_SYMBOL (iucv_message_reject);
  1483. EXPORT_SYMBOL (iucv_message_reply);
  1484. EXPORT_SYMBOL (iucv_message_send);
  1485. EXPORT_SYMBOL (iucv_message_send2way);
  1486. MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
  1487. MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
  1488. MODULE_LICENSE("GPL");