viopath.c 19 KB

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