viopath.c 19 KB

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