viopath.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* -*- linux-c -*-
  2. *
  3. * iSeries Virtual I/O Message Path code
  4. *
  5. * Authors: Dave Boutcher <boutcher@us.ibm.com>
  6. * Ryan Arnold <ryanarn@us.ibm.com>
  7. * Colin Devilbiss <devilbis@us.ibm.com>
  8. *
  9. * (C) Copyright 2000-2005 IBM Corporation
  10. *
  11. * This code is used by the iSeries virtual disk, cd,
  12. * tape, and console to communicate with OS/400 in another
  13. * partition.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of the
  18. * License, or (at your option) anyu later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software Foundation,
  27. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/errno.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/string.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/wait.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/interrupt.h>
  40. #include <asm/system.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/prom.h>
  43. #include <asm/firmware.h>
  44. #include <asm/iseries/hv_types.h>
  45. #include <asm/iseries/hv_lp_event.h>
  46. #include <asm/iseries/hv_lp_config.h>
  47. #include <asm/iseries/mf.h>
  48. #include <asm/iseries/vio.h>
  49. /* Status of the path to each other partition in the system.
  50. * This is overkill, since we will only ever establish connections
  51. * to our hosting partition and the primary partition on the system.
  52. * But this allows for other support in the future.
  53. */
  54. static struct viopathStatus {
  55. int isOpen; /* Did we open the path? */
  56. int isActive; /* Do we have a mon msg outstanding */
  57. int users[VIO_MAX_SUBTYPES];
  58. HvLpInstanceId mSourceInst;
  59. HvLpInstanceId mTargetInst;
  60. int numberAllocated;
  61. } viopathStatus[HVMAXARCHITECTEDLPS];
  62. static DEFINE_SPINLOCK(statuslock);
  63. /*
  64. * For each kind of event we allocate a buffer that is
  65. * guaranteed not to cross a page boundary
  66. */
  67. static unsigned char event_buffer[VIO_MAX_SUBTYPES * 256]
  68. __attribute__((__aligned__(4096)));
  69. static atomic_t event_buffer_available[VIO_MAX_SUBTYPES];
  70. static int event_buffer_initialised;
  71. static void handleMonitorEvent(struct HvLpEvent *event);
  72. /*
  73. * We use this structure to handle asynchronous responses. The caller
  74. * blocks on the semaphore and the handler posts the semaphore. However,
  75. * if system_state is not SYSTEM_RUNNING, then wait_atomic is used ...
  76. */
  77. struct alloc_parms {
  78. struct semaphore sem;
  79. int number;
  80. atomic_t wait_atomic;
  81. int used_wait_atomic;
  82. };
  83. /* Put a sequence number in each mon msg. The value is not
  84. * important. Start at something other than 0 just for
  85. * readability. wrapping this is ok.
  86. */
  87. static u8 viomonseq = 22;
  88. /* Our hosting logical partition. We get this at startup
  89. * time, and different modules access this variable directly.
  90. */
  91. HvLpIndex viopath_hostLp = HvLpIndexInvalid;
  92. EXPORT_SYMBOL(viopath_hostLp);
  93. HvLpIndex viopath_ourLp = HvLpIndexInvalid;
  94. EXPORT_SYMBOL(viopath_ourLp);
  95. /* For each kind of incoming event we set a pointer to a
  96. * routine to call.
  97. */
  98. static vio_event_handler_t *vio_handler[VIO_MAX_SUBTYPES];
  99. #define VIOPATH_KERN_WARN KERN_WARNING "viopath: "
  100. #define VIOPATH_KERN_INFO KERN_INFO "viopath: "
  101. static int proc_viopath_show(struct seq_file *m, void *v)
  102. {
  103. char *buf;
  104. u16 vlanMap;
  105. dma_addr_t handle;
  106. HvLpEvent_Rc hvrc;
  107. DECLARE_MUTEX_LOCKED(Semaphore);
  108. struct device_node *node;
  109. const char *sysid;
  110. buf = kzalloc(HW_PAGE_SIZE, GFP_KERNEL);
  111. if (!buf)
  112. return 0;
  113. handle = dma_map_single(iSeries_vio_dev, buf, HW_PAGE_SIZE,
  114. DMA_FROM_DEVICE);
  115. hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  116. HvLpEvent_Type_VirtualIo,
  117. viomajorsubtype_config | vioconfigget,
  118. HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  119. viopath_sourceinst(viopath_hostLp),
  120. viopath_targetinst(viopath_hostLp),
  121. (u64)(unsigned long)&Semaphore, VIOVERSION << 16,
  122. ((u64)handle) << 32, HW_PAGE_SIZE, 0, 0);
  123. if (hvrc != HvLpEvent_Rc_Good)
  124. printk(VIOPATH_KERN_WARN "hv error on op %d\n", (int)hvrc);
  125. down(&Semaphore);
  126. vlanMap = HvLpConfig_getVirtualLanIndexMap();
  127. buf[HW_PAGE_SIZE-1] = '\0';
  128. seq_printf(m, "%s", buf);
  129. dma_unmap_single(iSeries_vio_dev, handle, HW_PAGE_SIZE,
  130. DMA_FROM_DEVICE);
  131. kfree(buf);
  132. seq_printf(m, "AVAILABLE_VETH=%x\n", vlanMap);
  133. node = of_find_node_by_path("/");
  134. sysid = NULL;
  135. if (node != NULL)
  136. sysid = of_get_property(node, "system-id", NULL);
  137. if (sysid == NULL)
  138. seq_printf(m, "SRLNBR=<UNKNOWN>\n");
  139. else
  140. /* Skip "IBM," on front of serial number, see dt.c */
  141. seq_printf(m, "SRLNBR=%s\n", sysid + 4);
  142. of_node_put(node);
  143. return 0;
  144. }
  145. static int proc_viopath_open(struct inode *inode, struct file *file)
  146. {
  147. return single_open(file, proc_viopath_show, NULL);
  148. }
  149. static const struct file_operations proc_viopath_operations = {
  150. .open = proc_viopath_open,
  151. .read = seq_read,
  152. .llseek = seq_lseek,
  153. .release = single_release,
  154. };
  155. static int __init vio_proc_init(void)
  156. {
  157. struct proc_dir_entry *e;
  158. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  159. return 0;
  160. e = create_proc_entry("iSeries/config", 0, NULL);
  161. if (e)
  162. e->proc_fops = &proc_viopath_operations;
  163. return 0;
  164. }
  165. __initcall(vio_proc_init);
  166. /* See if a given LP is active. Allow for invalid lps to be passed in
  167. * and just return invalid
  168. */
  169. int viopath_isactive(HvLpIndex lp)
  170. {
  171. if (lp == HvLpIndexInvalid)
  172. return 0;
  173. if (lp < HVMAXARCHITECTEDLPS)
  174. return viopathStatus[lp].isActive;
  175. else
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(viopath_isactive);
  179. /*
  180. * We cache the source and target instance ids for each
  181. * partition.
  182. */
  183. HvLpInstanceId viopath_sourceinst(HvLpIndex lp)
  184. {
  185. return viopathStatus[lp].mSourceInst;
  186. }
  187. EXPORT_SYMBOL(viopath_sourceinst);
  188. HvLpInstanceId viopath_targetinst(HvLpIndex lp)
  189. {
  190. return viopathStatus[lp].mTargetInst;
  191. }
  192. EXPORT_SYMBOL(viopath_targetinst);
  193. /*
  194. * Send a monitor message. This is a message with the acknowledge
  195. * bit on that the other side will NOT explicitly acknowledge. When
  196. * the other side goes down, the hypervisor will acknowledge any
  197. * outstanding messages....so we will know when the other side dies.
  198. */
  199. static void sendMonMsg(HvLpIndex remoteLp)
  200. {
  201. HvLpEvent_Rc hvrc;
  202. viopathStatus[remoteLp].mSourceInst =
  203. HvCallEvent_getSourceLpInstanceId(remoteLp,
  204. HvLpEvent_Type_VirtualIo);
  205. viopathStatus[remoteLp].mTargetInst =
  206. HvCallEvent_getTargetLpInstanceId(remoteLp,
  207. HvLpEvent_Type_VirtualIo);
  208. /*
  209. * Deliberately ignore the return code here. if we call this
  210. * more than once, we don't care.
  211. */
  212. vio_setHandler(viomajorsubtype_monitor, handleMonitorEvent);
  213. hvrc = HvCallEvent_signalLpEventFast(remoteLp, HvLpEvent_Type_VirtualIo,
  214. viomajorsubtype_monitor, HvLpEvent_AckInd_DoAck,
  215. HvLpEvent_AckType_DeferredAck,
  216. viopathStatus[remoteLp].mSourceInst,
  217. viopathStatus[remoteLp].mTargetInst,
  218. viomonseq++, 0, 0, 0, 0, 0);
  219. if (hvrc == HvLpEvent_Rc_Good)
  220. viopathStatus[remoteLp].isActive = 1;
  221. else {
  222. printk(VIOPATH_KERN_WARN "could not connect to partition %d\n",
  223. remoteLp);
  224. viopathStatus[remoteLp].isActive = 0;
  225. }
  226. }
  227. static void handleMonitorEvent(struct HvLpEvent *event)
  228. {
  229. HvLpIndex remoteLp;
  230. int i;
  231. /*
  232. * This handler is _also_ called as part of the loop
  233. * at the end of this routine, so it must be able to
  234. * ignore NULL events...
  235. */
  236. if (!event)
  237. return;
  238. /*
  239. * First see if this is just a normal monitor message from the
  240. * other partition
  241. */
  242. if (hvlpevent_is_int(event)) {
  243. remoteLp = event->xSourceLp;
  244. if (!viopathStatus[remoteLp].isActive)
  245. sendMonMsg(remoteLp);
  246. return;
  247. }
  248. /*
  249. * This path is for an acknowledgement; the other partition
  250. * died
  251. */
  252. remoteLp = event->xTargetLp;
  253. if ((event->xSourceInstanceId != viopathStatus[remoteLp].mSourceInst) ||
  254. (event->xTargetInstanceId != viopathStatus[remoteLp].mTargetInst)) {
  255. printk(VIOPATH_KERN_WARN "ignoring ack....mismatched instances\n");
  256. return;
  257. }
  258. printk(VIOPATH_KERN_WARN "partition %d ended\n", remoteLp);
  259. viopathStatus[remoteLp].isActive = 0;
  260. /*
  261. * For each active handler, pass them a NULL
  262. * message to indicate that the other partition
  263. * died
  264. */
  265. for (i = 0; i < VIO_MAX_SUBTYPES; i++) {
  266. if (vio_handler[i] != NULL)
  267. (*vio_handler[i])(NULL);
  268. }
  269. }
  270. int vio_setHandler(int subtype, vio_event_handler_t *beh)
  271. {
  272. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  273. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
  274. return -EINVAL;
  275. if (vio_handler[subtype] != NULL)
  276. return -EBUSY;
  277. vio_handler[subtype] = beh;
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(vio_setHandler);
  281. int vio_clearHandler(int subtype)
  282. {
  283. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  284. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
  285. return -EINVAL;
  286. if (vio_handler[subtype] == NULL)
  287. return -EAGAIN;
  288. vio_handler[subtype] = NULL;
  289. return 0;
  290. }
  291. EXPORT_SYMBOL(vio_clearHandler);
  292. static void handleConfig(struct HvLpEvent *event)
  293. {
  294. if (!event)
  295. return;
  296. if (hvlpevent_is_int(event)) {
  297. printk(VIOPATH_KERN_WARN
  298. "unexpected config request from partition %d",
  299. event->xSourceLp);
  300. if (hvlpevent_need_ack(event)) {
  301. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  302. HvCallEvent_ackLpEvent(event);
  303. }
  304. return;
  305. }
  306. up((struct semaphore *)event->xCorrelationToken);
  307. }
  308. /*
  309. * Initialization of the hosting partition
  310. */
  311. void vio_set_hostlp(void)
  312. {
  313. /*
  314. * If this has already been set then we DON'T want to either change
  315. * it or re-register the proc file system
  316. */
  317. if (viopath_hostLp != HvLpIndexInvalid)
  318. return;
  319. /*
  320. * Figure out our hosting partition. This isn't allowed to change
  321. * while we're active
  322. */
  323. viopath_ourLp = HvLpConfig_getLpIndex();
  324. viopath_hostLp = HvLpConfig_getHostingLpIndex(viopath_ourLp);
  325. if (viopath_hostLp != HvLpIndexInvalid)
  326. vio_setHandler(viomajorsubtype_config, handleConfig);
  327. }
  328. EXPORT_SYMBOL(vio_set_hostlp);
  329. static void vio_handleEvent(struct HvLpEvent *event)
  330. {
  331. HvLpIndex remoteLp;
  332. int subtype = (event->xSubtype & VIOMAJOR_SUBTYPE_MASK)
  333. >> VIOMAJOR_SUBTYPE_SHIFT;
  334. if (hvlpevent_is_int(event)) {
  335. remoteLp = event->xSourceLp;
  336. /*
  337. * The isActive is checked because if the hosting partition
  338. * went down and came back up it would not be active but it
  339. * would have different source and target instances, in which
  340. * case we'd want to reset them. This case really protects
  341. * against an unauthorized active partition sending interrupts
  342. * or acks to this linux partition.
  343. */
  344. if (viopathStatus[remoteLp].isActive
  345. && (event->xSourceInstanceId !=
  346. viopathStatus[remoteLp].mTargetInst)) {
  347. printk(VIOPATH_KERN_WARN
  348. "message from invalid partition. "
  349. "int msg rcvd, source inst (%d) doesnt match (%d)\n",
  350. viopathStatus[remoteLp].mTargetInst,
  351. event->xSourceInstanceId);
  352. return;
  353. }
  354. if (viopathStatus[remoteLp].isActive
  355. && (event->xTargetInstanceId !=
  356. viopathStatus[remoteLp].mSourceInst)) {
  357. printk(VIOPATH_KERN_WARN
  358. "message from invalid partition. "
  359. "int msg rcvd, target inst (%d) doesnt match (%d)\n",
  360. viopathStatus[remoteLp].mSourceInst,
  361. event->xTargetInstanceId);
  362. return;
  363. }
  364. } else {
  365. remoteLp = event->xTargetLp;
  366. if (event->xSourceInstanceId !=
  367. viopathStatus[remoteLp].mSourceInst) {
  368. printk(VIOPATH_KERN_WARN
  369. "message from invalid partition. "
  370. "ack msg rcvd, source inst (%d) doesnt match (%d)\n",
  371. viopathStatus[remoteLp].mSourceInst,
  372. event->xSourceInstanceId);
  373. return;
  374. }
  375. if (event->xTargetInstanceId !=
  376. viopathStatus[remoteLp].mTargetInst) {
  377. printk(VIOPATH_KERN_WARN
  378. "message from invalid partition. "
  379. "viopath: ack msg rcvd, target inst (%d) doesnt match (%d)\n",
  380. viopathStatus[remoteLp].mTargetInst,
  381. event->xTargetInstanceId);
  382. return;
  383. }
  384. }
  385. if (vio_handler[subtype] == NULL) {
  386. printk(VIOPATH_KERN_WARN
  387. "unexpected virtual io event subtype %d from partition %d\n",
  388. event->xSubtype, remoteLp);
  389. /* No handler. Ack if necessary */
  390. if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
  391. event->xRc = HvLpEvent_Rc_InvalidSubtype;
  392. HvCallEvent_ackLpEvent(event);
  393. }
  394. return;
  395. }
  396. /* This innocuous little line is where all the real work happens */
  397. (*vio_handler[subtype])(event);
  398. }
  399. static void viopath_donealloc(void *parm, int number)
  400. {
  401. struct alloc_parms *parmsp = parm;
  402. parmsp->number = number;
  403. if (parmsp->used_wait_atomic)
  404. atomic_set(&parmsp->wait_atomic, 0);
  405. else
  406. up(&parmsp->sem);
  407. }
  408. static int allocateEvents(HvLpIndex remoteLp, int numEvents)
  409. {
  410. struct alloc_parms parms;
  411. if (system_state != SYSTEM_RUNNING) {
  412. parms.used_wait_atomic = 1;
  413. atomic_set(&parms.wait_atomic, 1);
  414. } else {
  415. parms.used_wait_atomic = 0;
  416. init_MUTEX_LOCKED(&parms.sem);
  417. }
  418. mf_allocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo, 250, /* It would be nice to put a real number here! */
  419. numEvents, &viopath_donealloc, &parms);
  420. if (system_state != SYSTEM_RUNNING) {
  421. while (atomic_read(&parms.wait_atomic))
  422. mb();
  423. } else
  424. down(&parms.sem);
  425. return parms.number;
  426. }
  427. int viopath_open(HvLpIndex remoteLp, int subtype, int numReq)
  428. {
  429. int i;
  430. unsigned long flags;
  431. int tempNumAllocated;
  432. if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
  433. return -EINVAL;
  434. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  435. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
  436. return -EINVAL;
  437. spin_lock_irqsave(&statuslock, flags);
  438. if (!event_buffer_initialised) {
  439. for (i = 0; i < VIO_MAX_SUBTYPES; i++)
  440. atomic_set(&event_buffer_available[i], 1);
  441. event_buffer_initialised = 1;
  442. }
  443. viopathStatus[remoteLp].users[subtype]++;
  444. if (!viopathStatus[remoteLp].isOpen) {
  445. viopathStatus[remoteLp].isOpen = 1;
  446. HvCallEvent_openLpEventPath(remoteLp, HvLpEvent_Type_VirtualIo);
  447. /*
  448. * Don't hold the spinlock during an operation that
  449. * can sleep.
  450. */
  451. spin_unlock_irqrestore(&statuslock, flags);
  452. tempNumAllocated = allocateEvents(remoteLp, 1);
  453. spin_lock_irqsave(&statuslock, flags);
  454. viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
  455. if (viopathStatus[remoteLp].numberAllocated == 0) {
  456. HvCallEvent_closeLpEventPath(remoteLp,
  457. HvLpEvent_Type_VirtualIo);
  458. spin_unlock_irqrestore(&statuslock, flags);
  459. return -ENOMEM;
  460. }
  461. viopathStatus[remoteLp].mSourceInst =
  462. HvCallEvent_getSourceLpInstanceId(remoteLp,
  463. HvLpEvent_Type_VirtualIo);
  464. viopathStatus[remoteLp].mTargetInst =
  465. HvCallEvent_getTargetLpInstanceId(remoteLp,
  466. HvLpEvent_Type_VirtualIo);
  467. HvLpEvent_registerHandler(HvLpEvent_Type_VirtualIo,
  468. &vio_handleEvent);
  469. sendMonMsg(remoteLp);
  470. printk(VIOPATH_KERN_INFO "opening connection to partition %d, "
  471. "setting sinst %d, tinst %d\n",
  472. remoteLp, viopathStatus[remoteLp].mSourceInst,
  473. viopathStatus[remoteLp].mTargetInst);
  474. }
  475. spin_unlock_irqrestore(&statuslock, flags);
  476. tempNumAllocated = allocateEvents(remoteLp, numReq);
  477. spin_lock_irqsave(&statuslock, flags);
  478. viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
  479. spin_unlock_irqrestore(&statuslock, flags);
  480. return 0;
  481. }
  482. EXPORT_SYMBOL(viopath_open);
  483. int viopath_close(HvLpIndex remoteLp, int subtype, int numReq)
  484. {
  485. unsigned long flags;
  486. int i;
  487. int numOpen;
  488. struct alloc_parms parms;
  489. if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
  490. return -EINVAL;
  491. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  492. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
  493. return -EINVAL;
  494. spin_lock_irqsave(&statuslock, flags);
  495. /*
  496. * If the viopath_close somehow gets called before a
  497. * viopath_open it could decrement to -1 which is a non
  498. * recoverable state so we'll prevent this from
  499. * happening.
  500. */
  501. if (viopathStatus[remoteLp].users[subtype] > 0)
  502. viopathStatus[remoteLp].users[subtype]--;
  503. spin_unlock_irqrestore(&statuslock, flags);
  504. parms.used_wait_atomic = 0;
  505. init_MUTEX_LOCKED(&parms.sem);
  506. mf_deallocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo,
  507. numReq, &viopath_donealloc, &parms);
  508. down(&parms.sem);
  509. spin_lock_irqsave(&statuslock, flags);
  510. for (i = 0, numOpen = 0; i < VIO_MAX_SUBTYPES; i++)
  511. numOpen += viopathStatus[remoteLp].users[i];
  512. if ((viopathStatus[remoteLp].isOpen) && (numOpen == 0)) {
  513. printk(VIOPATH_KERN_INFO "closing connection to partition %d",
  514. remoteLp);
  515. HvCallEvent_closeLpEventPath(remoteLp,
  516. HvLpEvent_Type_VirtualIo);
  517. viopathStatus[remoteLp].isOpen = 0;
  518. viopathStatus[remoteLp].isActive = 0;
  519. for (i = 0; i < VIO_MAX_SUBTYPES; i++)
  520. atomic_set(&event_buffer_available[i], 0);
  521. event_buffer_initialised = 0;
  522. }
  523. spin_unlock_irqrestore(&statuslock, flags);
  524. return 0;
  525. }
  526. EXPORT_SYMBOL(viopath_close);
  527. void *vio_get_event_buffer(int subtype)
  528. {
  529. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  530. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
  531. return NULL;
  532. if (atomic_dec_if_positive(&event_buffer_available[subtype]) == 0)
  533. return &event_buffer[subtype * 256];
  534. else
  535. return NULL;
  536. }
  537. EXPORT_SYMBOL(vio_get_event_buffer);
  538. void vio_free_event_buffer(int subtype, void *buffer)
  539. {
  540. subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
  541. if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) {
  542. printk(VIOPATH_KERN_WARN
  543. "unexpected subtype %d freeing event buffer\n", subtype);
  544. return;
  545. }
  546. if (atomic_read(&event_buffer_available[subtype]) != 0) {
  547. printk(VIOPATH_KERN_WARN
  548. "freeing unallocated event buffer, subtype %d\n",
  549. subtype);
  550. return;
  551. }
  552. if (buffer != &event_buffer[subtype * 256]) {
  553. printk(VIOPATH_KERN_WARN
  554. "freeing invalid event buffer, subtype %d\n", subtype);
  555. }
  556. atomic_set(&event_buffer_available[subtype], 1);
  557. }
  558. EXPORT_SYMBOL(vio_free_event_buffer);
  559. static const struct vio_error_entry vio_no_error =
  560. { 0, 0, "Non-VIO Error" };
  561. static const struct vio_error_entry vio_unknown_error =
  562. { 0, EIO, "Unknown Error" };
  563. static const struct vio_error_entry vio_default_errors[] = {
  564. {0x0001, EIO, "No Connection"},
  565. {0x0002, EIO, "No Receiver"},
  566. {0x0003, EIO, "No Buffer Available"},
  567. {0x0004, EBADRQC, "Invalid Message Type"},
  568. {0x0000, 0, NULL},
  569. };
  570. const struct vio_error_entry *vio_lookup_rc(
  571. const struct vio_error_entry *local_table, u16 rc)
  572. {
  573. const struct vio_error_entry *cur;
  574. if (!rc)
  575. return &vio_no_error;
  576. if (local_table)
  577. for (cur = local_table; cur->rc; ++cur)
  578. if (cur->rc == rc)
  579. return cur;
  580. for (cur = vio_default_errors; cur->rc; ++cur)
  581. if (cur->rc == rc)
  582. return cur;
  583. return &vio_unknown_error;
  584. }
  585. EXPORT_SYMBOL(vio_lookup_rc);