iucv.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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. #ifdef CONFIG_HOTPLUG_CPU
  468. static int __cpuinit iucv_cpu_notify(struct notifier_block *self,
  469. unsigned long action, void *hcpu)
  470. {
  471. cpumask_t cpumask;
  472. long cpu = (long) hcpu;
  473. switch (action) {
  474. case CPU_UP_PREPARE:
  475. if (!percpu_populate(iucv_irq_data,
  476. sizeof(struct iucv_irq_data),
  477. GFP_KERNEL|GFP_DMA, cpu))
  478. return NOTIFY_BAD;
  479. if (!percpu_populate(iucv_param, sizeof(union iucv_param),
  480. GFP_KERNEL|GFP_DMA, cpu)) {
  481. percpu_depopulate(iucv_irq_data, cpu);
  482. return NOTIFY_BAD;
  483. }
  484. break;
  485. case CPU_UP_CANCELED:
  486. case CPU_DEAD:
  487. percpu_depopulate(iucv_param, cpu);
  488. percpu_depopulate(iucv_irq_data, cpu);
  489. break;
  490. case CPU_ONLINE:
  491. case CPU_DOWN_FAILED:
  492. smp_call_function_on(iucv_declare_cpu, NULL, 0, 1, cpu);
  493. break;
  494. case CPU_DOWN_PREPARE:
  495. cpumask = iucv_buffer_cpumask;
  496. cpu_clear(cpu, cpumask);
  497. if (cpus_empty(cpumask))
  498. /* Can't offline last IUCV enabled cpu. */
  499. return NOTIFY_BAD;
  500. smp_call_function_on(iucv_retrieve_cpu, NULL, 0, 1, cpu);
  501. if (cpus_empty(iucv_irq_cpumask))
  502. smp_call_function_on(iucv_allow_cpu, NULL, 0, 1,
  503. first_cpu(iucv_buffer_cpumask));
  504. break;
  505. }
  506. return NOTIFY_OK;
  507. }
  508. static struct notifier_block iucv_cpu_notifier = {
  509. .notifier_call = iucv_cpu_notify,
  510. };
  511. #endif
  512. /**
  513. * iucv_sever_pathid
  514. * @pathid: path identification number.
  515. * @userdata: 16-bytes of user data.
  516. *
  517. * Sever an iucv path to free up the pathid. Used internally.
  518. */
  519. static int iucv_sever_pathid(u16 pathid, u8 userdata[16])
  520. {
  521. union iucv_param *parm;
  522. parm = percpu_ptr(iucv_param, smp_processor_id());
  523. memset(parm, 0, sizeof(union iucv_param));
  524. if (userdata)
  525. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  526. parm->ctrl.ippathid = pathid;
  527. return iucv_call_b2f0(IUCV_SEVER, parm);
  528. }
  529. /**
  530. * __iucv_cleanup_pathid
  531. * @dummy: unused dummy argument
  532. *
  533. * Nop function called via smp_call_function to force work items from
  534. * pending external iucv interrupts to the work queue.
  535. */
  536. static void __iucv_cleanup_pathid(void *dummy)
  537. {
  538. }
  539. /**
  540. * iucv_cleanup_pathid
  541. * @pathid: 16 bit pathid
  542. *
  543. * Function called after a path has been severed to find all remaining
  544. * work items for the now stale pathid. The caller needs to hold the
  545. * iucv_table_lock.
  546. */
  547. static void iucv_cleanup_pathid(u16 pathid)
  548. {
  549. struct iucv_work *p, *n;
  550. /*
  551. * Path is severed, the pathid can be reused immediatly on
  552. * a iucv connect or a connection pending interrupt.
  553. * iucv_path_connect and connection pending interrupt will
  554. * wait until the iucv_table_lock is released before the
  555. * recycled pathid enters the system.
  556. * Force remaining interrupts to the work queue, then
  557. * scan the work queue for items of this path.
  558. */
  559. smp_call_function(__iucv_cleanup_pathid, NULL, 0, 1);
  560. spin_lock_irq(&iucv_work_lock);
  561. list_for_each_entry_safe(p, n, &iucv_work_queue, list) {
  562. /* Remove work items for pathid except connection pending */
  563. if (p->data.ippathid == pathid && p->data.iptype != 0x01) {
  564. list_del(&p->list);
  565. kfree(p);
  566. }
  567. }
  568. spin_unlock_irq(&iucv_work_lock);
  569. }
  570. /**
  571. * iucv_register:
  572. * @handler: address of iucv handler structure
  573. * @smp: != 0 indicates that the handler can deal with out of order messages
  574. *
  575. * Registers a driver with IUCV.
  576. *
  577. * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
  578. * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
  579. */
  580. int iucv_register(struct iucv_handler *handler, int smp)
  581. {
  582. int rc;
  583. if (!iucv_available)
  584. return -ENOSYS;
  585. mutex_lock(&iucv_register_mutex);
  586. if (!smp)
  587. iucv_nonsmp_handler++;
  588. if (list_empty(&iucv_handler_list)) {
  589. rc = iucv_enable();
  590. if (rc)
  591. goto out_mutex;
  592. } else if (!smp && iucv_nonsmp_handler == 1)
  593. iucv_setmask_up();
  594. INIT_LIST_HEAD(&handler->paths);
  595. spin_lock_irq(&iucv_table_lock);
  596. list_add_tail(&handler->list, &iucv_handler_list);
  597. spin_unlock_irq(&iucv_table_lock);
  598. rc = 0;
  599. out_mutex:
  600. mutex_unlock(&iucv_register_mutex);
  601. return rc;
  602. }
  603. /**
  604. * iucv_unregister
  605. * @handler: address of iucv handler structure
  606. * @smp: != 0 indicates that the handler can deal with out of order messages
  607. *
  608. * Unregister driver from IUCV.
  609. */
  610. void iucv_unregister(struct iucv_handler *handler, int smp)
  611. {
  612. struct iucv_path *p, *n;
  613. mutex_lock(&iucv_register_mutex);
  614. spin_lock_bh(&iucv_table_lock);
  615. /* Remove handler from the iucv_handler_list. */
  616. list_del_init(&handler->list);
  617. /* Sever all pathids still refering to the handler. */
  618. list_for_each_entry_safe(p, n, &handler->paths, list) {
  619. iucv_sever_pathid(p->pathid, NULL);
  620. iucv_path_table[p->pathid] = NULL;
  621. list_del(&p->list);
  622. iucv_cleanup_pathid(p->pathid);
  623. iucv_path_free(p);
  624. }
  625. spin_unlock_bh(&iucv_table_lock);
  626. if (!smp)
  627. iucv_nonsmp_handler--;
  628. if (list_empty(&iucv_handler_list))
  629. iucv_disable();
  630. else if (!smp && iucv_nonsmp_handler == 0)
  631. iucv_setmask_mp();
  632. mutex_unlock(&iucv_register_mutex);
  633. }
  634. /**
  635. * iucv_path_accept
  636. * @path: address of iucv path structure
  637. * @handler: address of iucv handler structure
  638. * @userdata: 16 bytes of data reflected to the communication partner
  639. * @private: private data passed to interrupt handlers for this path
  640. *
  641. * This function is issued after the user received a connection pending
  642. * external interrupt and now wishes to complete the IUCV communication path.
  643. *
  644. * Returns the result of the CP IUCV call.
  645. */
  646. int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
  647. u8 userdata[16], void *private)
  648. {
  649. union iucv_param *parm;
  650. int rc;
  651. local_bh_disable();
  652. /* Prepare parameter block. */
  653. parm = percpu_ptr(iucv_param, smp_processor_id());
  654. memset(parm, 0, sizeof(union iucv_param));
  655. parm->ctrl.ippathid = path->pathid;
  656. parm->ctrl.ipmsglim = path->msglim;
  657. if (userdata)
  658. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  659. parm->ctrl.ipflags1 = path->flags;
  660. rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
  661. if (!rc) {
  662. path->private = private;
  663. path->msglim = parm->ctrl.ipmsglim;
  664. path->flags = parm->ctrl.ipflags1;
  665. }
  666. local_bh_enable();
  667. return rc;
  668. }
  669. /**
  670. * iucv_path_connect
  671. * @path: address of iucv path structure
  672. * @handler: address of iucv handler structure
  673. * @userid: 8-byte user identification
  674. * @system: 8-byte target system identification
  675. * @userdata: 16 bytes of data reflected to the communication partner
  676. * @private: private data passed to interrupt handlers for this path
  677. *
  678. * This function establishes an IUCV path. Although the connect may complete
  679. * successfully, you are not able to use the path until you receive an IUCV
  680. * Connection Complete external interrupt.
  681. *
  682. * Returns the result of the CP IUCV call.
  683. */
  684. int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
  685. u8 userid[8], u8 system[8], u8 userdata[16],
  686. void *private)
  687. {
  688. union iucv_param *parm;
  689. int rc;
  690. preempt_disable();
  691. if (iucv_tasklet_cpu != smp_processor_id())
  692. spin_lock_bh(&iucv_table_lock);
  693. parm = percpu_ptr(iucv_param, smp_processor_id());
  694. memset(parm, 0, sizeof(union iucv_param));
  695. parm->ctrl.ipmsglim = path->msglim;
  696. parm->ctrl.ipflags1 = path->flags;
  697. if (userid) {
  698. memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
  699. ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
  700. EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
  701. }
  702. if (system) {
  703. memcpy(parm->ctrl.iptarget, system,
  704. sizeof(parm->ctrl.iptarget));
  705. ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
  706. EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
  707. }
  708. if (userdata)
  709. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  710. rc = iucv_call_b2f0(IUCV_CONNECT, parm);
  711. if (!rc) {
  712. if (parm->ctrl.ippathid < iucv_max_pathid) {
  713. path->pathid = parm->ctrl.ippathid;
  714. path->msglim = parm->ctrl.ipmsglim;
  715. path->flags = parm->ctrl.ipflags1;
  716. path->handler = handler;
  717. path->private = private;
  718. list_add_tail(&path->list, &handler->paths);
  719. iucv_path_table[path->pathid] = path;
  720. } else {
  721. iucv_sever_pathid(parm->ctrl.ippathid,
  722. iucv_error_pathid);
  723. rc = -EIO;
  724. }
  725. }
  726. if (iucv_tasklet_cpu != smp_processor_id())
  727. spin_unlock_bh(&iucv_table_lock);
  728. preempt_enable();
  729. return rc;
  730. }
  731. /**
  732. * iucv_path_quiesce:
  733. * @path: address of iucv path structure
  734. * @userdata: 16 bytes of data reflected to the communication partner
  735. *
  736. * This function temporarily suspends incoming messages on an IUCV path.
  737. * You can later reactivate the path by invoking the iucv_resume function.
  738. *
  739. * Returns the result from the CP IUCV call.
  740. */
  741. int iucv_path_quiesce(struct iucv_path *path, u8 userdata[16])
  742. {
  743. union iucv_param *parm;
  744. int rc;
  745. local_bh_disable();
  746. parm = percpu_ptr(iucv_param, smp_processor_id());
  747. memset(parm, 0, sizeof(union iucv_param));
  748. if (userdata)
  749. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  750. parm->ctrl.ippathid = path->pathid;
  751. rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
  752. local_bh_enable();
  753. return rc;
  754. }
  755. /**
  756. * iucv_path_resume:
  757. * @path: address of iucv path structure
  758. * @userdata: 16 bytes of data reflected to the communication partner
  759. *
  760. * This function resumes incoming messages on an IUCV path that has
  761. * been stopped with iucv_path_quiesce.
  762. *
  763. * Returns the result from the CP IUCV call.
  764. */
  765. int iucv_path_resume(struct iucv_path *path, u8 userdata[16])
  766. {
  767. union iucv_param *parm;
  768. int rc;
  769. local_bh_disable();
  770. parm = percpu_ptr(iucv_param, smp_processor_id());
  771. memset(parm, 0, sizeof(union iucv_param));
  772. if (userdata)
  773. memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
  774. parm->ctrl.ippathid = path->pathid;
  775. rc = iucv_call_b2f0(IUCV_RESUME, parm);
  776. local_bh_enable();
  777. return rc;
  778. }
  779. /**
  780. * iucv_path_sever
  781. * @path: address of iucv path structure
  782. * @userdata: 16 bytes of data reflected to the communication partner
  783. *
  784. * This function terminates an IUCV path.
  785. *
  786. * Returns the result from the CP IUCV call.
  787. */
  788. int iucv_path_sever(struct iucv_path *path, u8 userdata[16])
  789. {
  790. int rc;
  791. preempt_disable();
  792. if (iucv_tasklet_cpu != smp_processor_id())
  793. spin_lock_bh(&iucv_table_lock);
  794. rc = iucv_sever_pathid(path->pathid, userdata);
  795. if (!rc) {
  796. iucv_path_table[path->pathid] = NULL;
  797. list_del_init(&path->list);
  798. iucv_cleanup_pathid(path->pathid);
  799. }
  800. if (iucv_tasklet_cpu != smp_processor_id())
  801. spin_unlock_bh(&iucv_table_lock);
  802. preempt_enable();
  803. return rc;
  804. }
  805. /**
  806. * iucv_message_purge
  807. * @path: address of iucv path structure
  808. * @msg: address of iucv msg structure
  809. * @srccls: source class of message
  810. *
  811. * Cancels a message you have sent.
  812. *
  813. * Returns the result from the CP IUCV call.
  814. */
  815. int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
  816. u32 srccls)
  817. {
  818. union iucv_param *parm;
  819. int rc;
  820. local_bh_disable();
  821. parm = percpu_ptr(iucv_param, smp_processor_id());
  822. memset(parm, 0, sizeof(union iucv_param));
  823. parm->purge.ippathid = path->pathid;
  824. parm->purge.ipmsgid = msg->id;
  825. parm->purge.ipsrccls = srccls;
  826. parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
  827. rc = iucv_call_b2f0(IUCV_PURGE, parm);
  828. if (!rc) {
  829. msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
  830. msg->tag = parm->purge.ipmsgtag;
  831. }
  832. local_bh_enable();
  833. return rc;
  834. }
  835. /**
  836. * iucv_message_receive
  837. * @path: address of iucv path structure
  838. * @msg: address of iucv msg structure
  839. * @flags: how the message is received (IUCV_IPBUFLST)
  840. * @buffer: address of data buffer or address of struct iucv_array
  841. * @size: length of data buffer
  842. * @residual:
  843. *
  844. * This function receives messages that are being sent to you over
  845. * established paths. This function will deal with RMDATA messages
  846. * embedded in struct iucv_message as well.
  847. *
  848. * Returns the result from the CP IUCV call.
  849. */
  850. int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
  851. u8 flags, void *buffer, size_t size, size_t *residual)
  852. {
  853. union iucv_param *parm;
  854. struct iucv_array *array;
  855. u8 *rmmsg;
  856. size_t copy;
  857. int rc;
  858. if (msg->flags & IUCV_IPRMDATA) {
  859. /*
  860. * Message is 8 bytes long and has been stored to the
  861. * message descriptor itself.
  862. */
  863. rc = (size < 8) ? 5 : 0;
  864. if (residual)
  865. *residual = abs(size - 8);
  866. rmmsg = msg->rmmsg;
  867. if (flags & IUCV_IPBUFLST) {
  868. /* Copy to struct iucv_array. */
  869. size = (size < 8) ? size : 8;
  870. for (array = buffer; size > 0; array++) {
  871. copy = min_t(size_t, size, array->length);
  872. memcpy((u8 *)(addr_t) array->address,
  873. rmmsg, copy);
  874. rmmsg += copy;
  875. size -= copy;
  876. }
  877. } else {
  878. /* Copy to direct buffer. */
  879. memcpy(buffer, rmmsg, min_t(size_t, size, 8));
  880. }
  881. return 0;
  882. }
  883. local_bh_disable();
  884. parm = percpu_ptr(iucv_param, smp_processor_id());
  885. memset(parm, 0, sizeof(union iucv_param));
  886. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  887. parm->db.ipbfln1f = (u32) size;
  888. parm->db.ipmsgid = msg->id;
  889. parm->db.ippathid = path->pathid;
  890. parm->db.iptrgcls = msg->class;
  891. parm->db.ipflags1 = (flags | IUCV_IPFGPID |
  892. IUCV_IPFGMID | IUCV_IPTRGCLS);
  893. rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
  894. if (!rc || rc == 5) {
  895. msg->flags = parm->db.ipflags1;
  896. if (residual)
  897. *residual = parm->db.ipbfln1f;
  898. }
  899. local_bh_enable();
  900. return rc;
  901. }
  902. /**
  903. * iucv_message_reject
  904. * @path: address of iucv path structure
  905. * @msg: address of iucv msg structure
  906. *
  907. * The reject function refuses a specified message. Between the time you
  908. * are notified of a message and the time that you complete the message,
  909. * the message may be rejected.
  910. *
  911. * Returns the result from the CP IUCV call.
  912. */
  913. int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
  914. {
  915. union iucv_param *parm;
  916. int rc;
  917. local_bh_disable();
  918. parm = percpu_ptr(iucv_param, smp_processor_id());
  919. memset(parm, 0, sizeof(union iucv_param));
  920. parm->db.ippathid = path->pathid;
  921. parm->db.ipmsgid = msg->id;
  922. parm->db.iptrgcls = msg->class;
  923. parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
  924. rc = iucv_call_b2f0(IUCV_REJECT, parm);
  925. local_bh_enable();
  926. return rc;
  927. }
  928. /**
  929. * iucv_message_reply
  930. * @path: address of iucv path structure
  931. * @msg: address of iucv msg structure
  932. * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
  933. * @reply: address of reply data buffer or address of struct iucv_array
  934. * @size: length of reply data buffer
  935. *
  936. * This function responds to the two-way messages that you receive. You
  937. * must identify completely the message to which you wish to reply. ie,
  938. * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
  939. * the parameter list.
  940. *
  941. * Returns the result from the CP IUCV call.
  942. */
  943. int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
  944. u8 flags, void *reply, size_t size)
  945. {
  946. union iucv_param *parm;
  947. int rc;
  948. local_bh_disable();
  949. parm = percpu_ptr(iucv_param, smp_processor_id());
  950. memset(parm, 0, sizeof(union iucv_param));
  951. if (flags & IUCV_IPRMDATA) {
  952. parm->dpl.ippathid = path->pathid;
  953. parm->dpl.ipflags1 = flags;
  954. parm->dpl.ipmsgid = msg->id;
  955. parm->dpl.iptrgcls = msg->class;
  956. memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
  957. } else {
  958. parm->db.ipbfadr1 = (u32)(addr_t) reply;
  959. parm->db.ipbfln1f = (u32) size;
  960. parm->db.ippathid = path->pathid;
  961. parm->db.ipflags1 = flags;
  962. parm->db.ipmsgid = msg->id;
  963. parm->db.iptrgcls = msg->class;
  964. }
  965. rc = iucv_call_b2f0(IUCV_REPLY, parm);
  966. local_bh_enable();
  967. return rc;
  968. }
  969. /**
  970. * iucv_message_send
  971. * @path: address of iucv path structure
  972. * @msg: address of iucv msg structure
  973. * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
  974. * @srccls: source class of message
  975. * @buffer: address of send buffer or address of struct iucv_array
  976. * @size: length of send buffer
  977. *
  978. * This function transmits data to another application. Data to be
  979. * transmitted is in a buffer and this is a one-way message and the
  980. * receiver will not reply to the message.
  981. *
  982. * Returns the result from the CP IUCV call.
  983. */
  984. int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
  985. u8 flags, u32 srccls, void *buffer, size_t size)
  986. {
  987. union iucv_param *parm;
  988. int rc;
  989. local_bh_disable();
  990. parm = percpu_ptr(iucv_param, smp_processor_id());
  991. memset(parm, 0, sizeof(union iucv_param));
  992. if (flags & IUCV_IPRMDATA) {
  993. /* Message of 8 bytes can be placed into the parameter list. */
  994. parm->dpl.ippathid = path->pathid;
  995. parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
  996. parm->dpl.iptrgcls = msg->class;
  997. parm->dpl.ipsrccls = srccls;
  998. parm->dpl.ipmsgtag = msg->tag;
  999. memcpy(parm->dpl.iprmmsg, buffer, 8);
  1000. } else {
  1001. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  1002. parm->db.ipbfln1f = (u32) size;
  1003. parm->db.ippathid = path->pathid;
  1004. parm->db.ipflags1 = flags | IUCV_IPNORPY;
  1005. parm->db.iptrgcls = msg->class;
  1006. parm->db.ipsrccls = srccls;
  1007. parm->db.ipmsgtag = msg->tag;
  1008. }
  1009. rc = iucv_call_b2f0(IUCV_SEND, parm);
  1010. if (!rc)
  1011. msg->id = parm->db.ipmsgid;
  1012. local_bh_enable();
  1013. return rc;
  1014. }
  1015. /**
  1016. * iucv_message_send2way
  1017. * @path: address of iucv path structure
  1018. * @msg: address of iucv msg structure
  1019. * @flags: how the message is sent and the reply is received
  1020. * (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
  1021. * @srccls: source class of message
  1022. * @buffer: address of send buffer or address of struct iucv_array
  1023. * @size: length of send buffer
  1024. * @ansbuf: address of answer buffer or address of struct iucv_array
  1025. * @asize: size of reply buffer
  1026. *
  1027. * This function transmits data to another application. Data to be
  1028. * transmitted is in a buffer. The receiver of the send is expected to
  1029. * reply to the message and a buffer is provided into which IUCV moves
  1030. * the reply to this message.
  1031. *
  1032. * Returns the result from the CP IUCV call.
  1033. */
  1034. int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
  1035. u8 flags, u32 srccls, void *buffer, size_t size,
  1036. void *answer, size_t asize, size_t *residual)
  1037. {
  1038. union iucv_param *parm;
  1039. int rc;
  1040. local_bh_disable();
  1041. parm = percpu_ptr(iucv_param, smp_processor_id());
  1042. memset(parm, 0, sizeof(union iucv_param));
  1043. if (flags & IUCV_IPRMDATA) {
  1044. parm->dpl.ippathid = path->pathid;
  1045. parm->dpl.ipflags1 = path->flags; /* priority message */
  1046. parm->dpl.iptrgcls = msg->class;
  1047. parm->dpl.ipsrccls = srccls;
  1048. parm->dpl.ipmsgtag = msg->tag;
  1049. parm->dpl.ipbfadr2 = (u32)(addr_t) answer;
  1050. parm->dpl.ipbfln2f = (u32) asize;
  1051. memcpy(parm->dpl.iprmmsg, buffer, 8);
  1052. } else {
  1053. parm->db.ippathid = path->pathid;
  1054. parm->db.ipflags1 = path->flags; /* priority message */
  1055. parm->db.iptrgcls = msg->class;
  1056. parm->db.ipsrccls = srccls;
  1057. parm->db.ipmsgtag = msg->tag;
  1058. parm->db.ipbfadr1 = (u32)(addr_t) buffer;
  1059. parm->db.ipbfln1f = (u32) size;
  1060. parm->db.ipbfadr2 = (u32)(addr_t) answer;
  1061. parm->db.ipbfln2f = (u32) asize;
  1062. }
  1063. rc = iucv_call_b2f0(IUCV_SEND, parm);
  1064. if (!rc)
  1065. msg->id = parm->db.ipmsgid;
  1066. local_bh_enable();
  1067. return rc;
  1068. }
  1069. /**
  1070. * iucv_path_pending
  1071. * @data: Pointer to external interrupt buffer
  1072. *
  1073. * Process connection pending work item. Called from tasklet while holding
  1074. * iucv_table_lock.
  1075. */
  1076. struct iucv_path_pending {
  1077. u16 ippathid;
  1078. u8 ipflags1;
  1079. u8 iptype;
  1080. u16 ipmsglim;
  1081. u16 res1;
  1082. u8 ipvmid[8];
  1083. u8 ipuser[16];
  1084. u32 res3;
  1085. u8 ippollfg;
  1086. u8 res4[3];
  1087. } __attribute__ ((packed));
  1088. static void iucv_path_pending(struct iucv_irq_data *data)
  1089. {
  1090. struct iucv_path_pending *ipp = (void *) data;
  1091. struct iucv_handler *handler;
  1092. struct iucv_path *path;
  1093. char *error;
  1094. BUG_ON(iucv_path_table[ipp->ippathid]);
  1095. /* New pathid, handler found. Create a new path struct. */
  1096. error = iucv_error_no_memory;
  1097. path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
  1098. if (!path)
  1099. goto out_sever;
  1100. path->pathid = ipp->ippathid;
  1101. iucv_path_table[path->pathid] = path;
  1102. EBCASC(ipp->ipvmid, 8);
  1103. /* Call registered handler until one is found that wants the path. */
  1104. list_for_each_entry(handler, &iucv_handler_list, list) {
  1105. if (!handler->path_pending)
  1106. continue;
  1107. /*
  1108. * Add path to handler to allow a call to iucv_path_sever
  1109. * inside the path_pending function. If the handler returns
  1110. * an error remove the path from the handler again.
  1111. */
  1112. list_add(&path->list, &handler->paths);
  1113. path->handler = handler;
  1114. if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
  1115. return;
  1116. list_del(&path->list);
  1117. path->handler = NULL;
  1118. }
  1119. /* No handler wanted the path. */
  1120. iucv_path_table[path->pathid] = NULL;
  1121. iucv_path_free(path);
  1122. error = iucv_error_no_listener;
  1123. out_sever:
  1124. iucv_sever_pathid(ipp->ippathid, error);
  1125. }
  1126. /**
  1127. * iucv_path_complete
  1128. * @data: Pointer to external interrupt buffer
  1129. *
  1130. * Process connection complete work item. Called from tasklet while holding
  1131. * iucv_table_lock.
  1132. */
  1133. struct iucv_path_complete {
  1134. u16 ippathid;
  1135. u8 ipflags1;
  1136. u8 iptype;
  1137. u16 ipmsglim;
  1138. u16 res1;
  1139. u8 res2[8];
  1140. u8 ipuser[16];
  1141. u32 res3;
  1142. u8 ippollfg;
  1143. u8 res4[3];
  1144. } __attribute__ ((packed));
  1145. static void iucv_path_complete(struct iucv_irq_data *data)
  1146. {
  1147. struct iucv_path_complete *ipc = (void *) data;
  1148. struct iucv_path *path = iucv_path_table[ipc->ippathid];
  1149. BUG_ON(!path || !path->handler);
  1150. if (path->handler->path_complete)
  1151. path->handler->path_complete(path, ipc->ipuser);
  1152. }
  1153. /**
  1154. * iucv_path_severed
  1155. * @data: Pointer to external interrupt buffer
  1156. *
  1157. * Process connection severed work item. Called from tasklet while holding
  1158. * iucv_table_lock.
  1159. */
  1160. struct iucv_path_severed {
  1161. u16 ippathid;
  1162. u8 res1;
  1163. u8 iptype;
  1164. u32 res2;
  1165. u8 res3[8];
  1166. u8 ipuser[16];
  1167. u32 res4;
  1168. u8 ippollfg;
  1169. u8 res5[3];
  1170. } __attribute__ ((packed));
  1171. static void iucv_path_severed(struct iucv_irq_data *data)
  1172. {
  1173. struct iucv_path_severed *ips = (void *) data;
  1174. struct iucv_path *path = iucv_path_table[ips->ippathid];
  1175. BUG_ON(!path || !path->handler);
  1176. if (path->handler->path_severed)
  1177. path->handler->path_severed(path, ips->ipuser);
  1178. else {
  1179. iucv_sever_pathid(path->pathid, NULL);
  1180. iucv_path_table[path->pathid] = NULL;
  1181. list_del_init(&path->list);
  1182. iucv_cleanup_pathid(path->pathid);
  1183. iucv_path_free(path);
  1184. }
  1185. }
  1186. /**
  1187. * iucv_path_quiesced
  1188. * @data: Pointer to external interrupt buffer
  1189. *
  1190. * Process connection quiesced work item. Called from tasklet while holding
  1191. * iucv_table_lock.
  1192. */
  1193. struct iucv_path_quiesced {
  1194. u16 ippathid;
  1195. u8 res1;
  1196. u8 iptype;
  1197. u32 res2;
  1198. u8 res3[8];
  1199. u8 ipuser[16];
  1200. u32 res4;
  1201. u8 ippollfg;
  1202. u8 res5[3];
  1203. } __attribute__ ((packed));
  1204. static void iucv_path_quiesced(struct iucv_irq_data *data)
  1205. {
  1206. struct iucv_path_quiesced *ipq = (void *) data;
  1207. struct iucv_path *path = iucv_path_table[ipq->ippathid];
  1208. BUG_ON(!path || !path->handler);
  1209. if (path->handler->path_quiesced)
  1210. path->handler->path_quiesced(path, ipq->ipuser);
  1211. }
  1212. /**
  1213. * iucv_path_resumed
  1214. * @data: Pointer to external interrupt buffer
  1215. *
  1216. * Process connection resumed work item. Called from tasklet while holding
  1217. * iucv_table_lock.
  1218. */
  1219. struct iucv_path_resumed {
  1220. u16 ippathid;
  1221. u8 res1;
  1222. u8 iptype;
  1223. u32 res2;
  1224. u8 res3[8];
  1225. u8 ipuser[16];
  1226. u32 res4;
  1227. u8 ippollfg;
  1228. u8 res5[3];
  1229. } __attribute__ ((packed));
  1230. static void iucv_path_resumed(struct iucv_irq_data *data)
  1231. {
  1232. struct iucv_path_resumed *ipr = (void *) data;
  1233. struct iucv_path *path = iucv_path_table[ipr->ippathid];
  1234. BUG_ON(!path || !path->handler);
  1235. if (path->handler->path_resumed)
  1236. path->handler->path_resumed(path, ipr->ipuser);
  1237. }
  1238. /**
  1239. * iucv_message_complete
  1240. * @data: Pointer to external interrupt buffer
  1241. *
  1242. * Process message complete work item. Called from tasklet while holding
  1243. * iucv_table_lock.
  1244. */
  1245. struct iucv_message_complete {
  1246. u16 ippathid;
  1247. u8 ipflags1;
  1248. u8 iptype;
  1249. u32 ipmsgid;
  1250. u32 ipaudit;
  1251. u8 iprmmsg[8];
  1252. u32 ipsrccls;
  1253. u32 ipmsgtag;
  1254. u32 res;
  1255. u32 ipbfln2f;
  1256. u8 ippollfg;
  1257. u8 res2[3];
  1258. } __attribute__ ((packed));
  1259. static void iucv_message_complete(struct iucv_irq_data *data)
  1260. {
  1261. struct iucv_message_complete *imc = (void *) data;
  1262. struct iucv_path *path = iucv_path_table[imc->ippathid];
  1263. struct iucv_message msg;
  1264. BUG_ON(!path || !path->handler);
  1265. if (path->handler->message_complete) {
  1266. msg.flags = imc->ipflags1;
  1267. msg.id = imc->ipmsgid;
  1268. msg.audit = imc->ipaudit;
  1269. memcpy(msg.rmmsg, imc->iprmmsg, 8);
  1270. msg.class = imc->ipsrccls;
  1271. msg.tag = imc->ipmsgtag;
  1272. msg.length = imc->ipbfln2f;
  1273. path->handler->message_complete(path, &msg);
  1274. }
  1275. }
  1276. /**
  1277. * iucv_message_pending
  1278. * @data: Pointer to external interrupt buffer
  1279. *
  1280. * Process message pending work item. Called from tasklet while holding
  1281. * iucv_table_lock.
  1282. */
  1283. struct iucv_message_pending {
  1284. u16 ippathid;
  1285. u8 ipflags1;
  1286. u8 iptype;
  1287. u32 ipmsgid;
  1288. u32 iptrgcls;
  1289. union {
  1290. u32 iprmmsg1_u32;
  1291. u8 iprmmsg1[4];
  1292. } ln1msg1;
  1293. union {
  1294. u32 ipbfln1f;
  1295. u8 iprmmsg2[4];
  1296. } ln1msg2;
  1297. u32 res1[3];
  1298. u32 ipbfln2f;
  1299. u8 ippollfg;
  1300. u8 res2[3];
  1301. } __attribute__ ((packed));
  1302. static void iucv_message_pending(struct iucv_irq_data *data)
  1303. {
  1304. struct iucv_message_pending *imp = (void *) data;
  1305. struct iucv_path *path = iucv_path_table[imp->ippathid];
  1306. struct iucv_message msg;
  1307. BUG_ON(!path || !path->handler);
  1308. if (path->handler->message_pending) {
  1309. msg.flags = imp->ipflags1;
  1310. msg.id = imp->ipmsgid;
  1311. msg.class = imp->iptrgcls;
  1312. if (imp->ipflags1 & IUCV_IPRMDATA) {
  1313. memcpy(msg.rmmsg, imp->ln1msg1.iprmmsg1, 8);
  1314. msg.length = 8;
  1315. } else
  1316. msg.length = imp->ln1msg2.ipbfln1f;
  1317. msg.reply_size = imp->ipbfln2f;
  1318. path->handler->message_pending(path, &msg);
  1319. }
  1320. }
  1321. /**
  1322. * iucv_tasklet_handler:
  1323. *
  1324. * This tasklet loops over the queue of irq buffers created by
  1325. * iucv_external_interrupt, calls the appropriate action handler
  1326. * and then frees the buffer.
  1327. */
  1328. static void iucv_tasklet_handler(unsigned long ignored)
  1329. {
  1330. typedef void iucv_irq_fn(struct iucv_irq_data *);
  1331. static iucv_irq_fn *irq_fn[] = {
  1332. [0x01] = iucv_path_pending,
  1333. [0x02] = iucv_path_complete,
  1334. [0x03] = iucv_path_severed,
  1335. [0x04] = iucv_path_quiesced,
  1336. [0x05] = iucv_path_resumed,
  1337. [0x06] = iucv_message_complete,
  1338. [0x07] = iucv_message_complete,
  1339. [0x08] = iucv_message_pending,
  1340. [0x09] = iucv_message_pending,
  1341. };
  1342. struct iucv_work *p;
  1343. /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
  1344. spin_lock(&iucv_table_lock);
  1345. iucv_tasklet_cpu = smp_processor_id();
  1346. spin_lock_irq(&iucv_work_lock);
  1347. while (!list_empty(&iucv_work_queue)) {
  1348. p = list_entry(iucv_work_queue.next, struct iucv_work, list);
  1349. list_del_init(&p->list);
  1350. spin_unlock_irq(&iucv_work_lock);
  1351. irq_fn[p->data.iptype](&p->data);
  1352. kfree(p);
  1353. spin_lock_irq(&iucv_work_lock);
  1354. }
  1355. spin_unlock_irq(&iucv_work_lock);
  1356. iucv_tasklet_cpu = -1;
  1357. spin_unlock(&iucv_table_lock);
  1358. }
  1359. /**
  1360. * iucv_external_interrupt
  1361. * @code: irq code
  1362. *
  1363. * Handles external interrupts coming in from CP.
  1364. * Places the interrupt buffer on a queue and schedules iucv_tasklet_handler().
  1365. */
  1366. static void iucv_external_interrupt(u16 code)
  1367. {
  1368. struct iucv_irq_data *p;
  1369. struct iucv_work *work;
  1370. p = percpu_ptr(iucv_irq_data, smp_processor_id());
  1371. if (p->ippathid >= iucv_max_pathid) {
  1372. printk(KERN_WARNING "iucv_do_int: Got interrupt with "
  1373. "pathid %d > max_connections (%ld)\n",
  1374. p->ippathid, iucv_max_pathid - 1);
  1375. iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
  1376. return;
  1377. }
  1378. if (p->iptype < 0x01 || p->iptype > 0x09) {
  1379. printk(KERN_ERR "iucv_do_int: unknown iucv interrupt\n");
  1380. return;
  1381. }
  1382. work = kmalloc(sizeof(struct iucv_work), GFP_ATOMIC);
  1383. if (!work) {
  1384. printk(KERN_WARNING "iucv_external_interrupt: out of memory\n");
  1385. return;
  1386. }
  1387. memcpy(&work->data, p, sizeof(work->data));
  1388. spin_lock(&iucv_work_lock);
  1389. list_add_tail(&work->list, &iucv_work_queue);
  1390. spin_unlock(&iucv_work_lock);
  1391. tasklet_schedule(&iucv_tasklet);
  1392. }
  1393. /**
  1394. * iucv_init
  1395. *
  1396. * Allocates and initializes various data structures.
  1397. */
  1398. static int iucv_init(void)
  1399. {
  1400. int rc;
  1401. if (!MACHINE_IS_VM) {
  1402. rc = -EPROTONOSUPPORT;
  1403. goto out;
  1404. }
  1405. rc = iucv_query_maxconn();
  1406. if (rc)
  1407. goto out;
  1408. rc = register_external_interrupt (0x4000, iucv_external_interrupt);
  1409. if (rc)
  1410. goto out;
  1411. rc = bus_register(&iucv_bus);
  1412. if (rc)
  1413. goto out_int;
  1414. iucv_root = s390_root_dev_register("iucv");
  1415. if (IS_ERR(iucv_root)) {
  1416. rc = PTR_ERR(iucv_root);
  1417. goto out_bus;
  1418. }
  1419. /* Note: GFP_DMA used used to get memory below 2G */
  1420. iucv_irq_data = percpu_alloc(sizeof(struct iucv_irq_data),
  1421. GFP_KERNEL|GFP_DMA);
  1422. if (!iucv_irq_data) {
  1423. rc = -ENOMEM;
  1424. goto out_root;
  1425. }
  1426. /* Allocate parameter blocks. */
  1427. iucv_param = percpu_alloc(sizeof(union iucv_param),
  1428. GFP_KERNEL|GFP_DMA);
  1429. if (!iucv_param) {
  1430. rc = -ENOMEM;
  1431. goto out_extint;
  1432. }
  1433. register_hotcpu_notifier(&iucv_cpu_notifier);
  1434. ASCEBC(iucv_error_no_listener, 16);
  1435. ASCEBC(iucv_error_no_memory, 16);
  1436. ASCEBC(iucv_error_pathid, 16);
  1437. iucv_available = 1;
  1438. return 0;
  1439. out_extint:
  1440. percpu_free(iucv_irq_data);
  1441. out_root:
  1442. s390_root_dev_unregister(iucv_root);
  1443. out_bus:
  1444. bus_unregister(&iucv_bus);
  1445. out_int:
  1446. unregister_external_interrupt(0x4000, iucv_external_interrupt);
  1447. out:
  1448. return rc;
  1449. }
  1450. /**
  1451. * iucv_exit
  1452. *
  1453. * Frees everything allocated from iucv_init.
  1454. */
  1455. static void iucv_exit(void)
  1456. {
  1457. struct iucv_work *p, *n;
  1458. spin_lock_irq(&iucv_work_lock);
  1459. list_for_each_entry_safe(p, n, &iucv_work_queue, list)
  1460. kfree(p);
  1461. spin_unlock_irq(&iucv_work_lock);
  1462. unregister_hotcpu_notifier(&iucv_cpu_notifier);
  1463. percpu_free(iucv_param);
  1464. percpu_free(iucv_irq_data);
  1465. s390_root_dev_unregister(iucv_root);
  1466. bus_unregister(&iucv_bus);
  1467. unregister_external_interrupt(0x4000, iucv_external_interrupt);
  1468. }
  1469. subsys_initcall(iucv_init);
  1470. module_exit(iucv_exit);
  1471. /**
  1472. * Export all public stuff
  1473. */
  1474. EXPORT_SYMBOL (iucv_bus);
  1475. EXPORT_SYMBOL (iucv_root);
  1476. EXPORT_SYMBOL (iucv_register);
  1477. EXPORT_SYMBOL (iucv_unregister);
  1478. EXPORT_SYMBOL (iucv_path_accept);
  1479. EXPORT_SYMBOL (iucv_path_connect);
  1480. EXPORT_SYMBOL (iucv_path_quiesce);
  1481. EXPORT_SYMBOL (iucv_path_sever);
  1482. EXPORT_SYMBOL (iucv_message_purge);
  1483. EXPORT_SYMBOL (iucv_message_receive);
  1484. EXPORT_SYMBOL (iucv_message_reject);
  1485. EXPORT_SYMBOL (iucv_message_reply);
  1486. EXPORT_SYMBOL (iucv_message_send);
  1487. EXPORT_SYMBOL (iucv_message_send2way);
  1488. MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
  1489. MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
  1490. MODULE_LICENSE("GPL");