viopath.c 18 KB

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