viopath.c 19 KB

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