viopath.c 18 KB

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