viopath.c 19 KB

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