hvcs.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /*
  2. * IBM eServer Hypervisor Virtual Console Server Device Driver
  3. * Copyright (C) 2003, 2004 IBM Corp.
  4. * Ryan S. Arnold (rsa@us.ibm.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
  21. *
  22. * This is the device driver for the IBM Hypervisor Virtual Console Server,
  23. * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
  24. * user space applications access to the system consoles of logically
  25. * partitioned operating systems, e.g. Linux, running on the same partitioned
  26. * Power5 ppc64 system. Physical hardware consoles per partition are not
  27. * practical on this hardware so system consoles are accessed by this driver
  28. * using inter-partition firmware interfaces to virtual terminal devices.
  29. *
  30. * A vty is known to the HMC as a "virtual serial server adapter". It is a
  31. * virtual terminal device that is created by firmware upon partition creation
  32. * to act as a partitioned OS's console device.
  33. *
  34. * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
  35. * Linux system upon their creation by the HMC or their exposure during boot.
  36. * The non-user interactive backend of this driver is implemented as a vio
  37. * device driver so that it can receive notification of vty-server lifetimes
  38. * after it registers with the vio bus to handle vty-server probe and remove
  39. * callbacks.
  40. *
  41. * Many vty-servers can be configured to connect to one vty, but a vty can
  42. * only be actively connected to by a single vty-server, in any manner, at one
  43. * time. If the HMC is currently hosting the console for a target Linux
  44. * partition; attempts to open the tty device to the partition's console using
  45. * the hvcs on any partition will return -EBUSY with every open attempt until
  46. * the HMC frees the connection between its vty-server and the desired
  47. * partition's vty device. Conversely, a vty-server may only be connected to
  48. * a single vty at one time even though it may have several configured vty
  49. * partner possibilities.
  50. *
  51. * Firmware does not provide notification of vty partner changes to this
  52. * driver. This means that an HMC Super Admin may add or remove partner vtys
  53. * from a vty-server's partner list but the changes will not be signaled to
  54. * the vty-server. Firmware only notifies the driver when a vty-server is
  55. * added or removed from the system. To compensate for this deficiency, this
  56. * driver implements a sysfs update attribute which provides a method for
  57. * rescanning partner information upon a user's request.
  58. *
  59. * Each vty-server, prior to being exposed to this driver is reference counted
  60. * using the 2.6 Linux kernel kref construct.
  61. *
  62. * For direction on installation and usage of this driver please reference
  63. * Documentation/powerpc/hvcs.txt.
  64. */
  65. #include <linux/device.h>
  66. #include <linux/init.h>
  67. #include <linux/interrupt.h>
  68. #include <linux/kernel.h>
  69. #include <linux/kref.h>
  70. #include <linux/kthread.h>
  71. #include <linux/list.h>
  72. #include <linux/major.h>
  73. #include <linux/module.h>
  74. #include <linux/moduleparam.h>
  75. #include <linux/sched.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/stat.h>
  78. #include <linux/tty.h>
  79. #include <linux/tty_flip.h>
  80. #include <asm/hvconsole.h>
  81. #include <asm/hvcserver.h>
  82. #include <asm/uaccess.h>
  83. #include <asm/vio.h>
  84. /*
  85. * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
  86. * Removed braces around single statements following conditionals. Removed '=
  87. * 0' after static int declarations since these default to zero. Removed
  88. * list_for_each_safe() and replaced with list_for_each_entry() in
  89. * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
  90. * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
  91. * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
  92. * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
  93. * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
  94. * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
  95. * list traversals from a deletion. Removed '= NULL' from pointer declaration
  96. * statements since they are initialized NULL by default. Removed wmb()
  97. * instances from hvcs_try_write(). They probably aren't needed with locking in
  98. * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
  99. * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
  100. * the coupling between /dev/hvcs* and a vty-server can be automatically
  101. * determined. Moved kobject_put() in hvcs_open outside of the
  102. * spin_unlock_irqrestore().
  103. *
  104. * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
  105. * align with how the tty layer always assigns the lowest index available. This
  106. * change resulted in a list of ints that denotes which indexes are available.
  107. * Device additions and removals use the new hvcs_get_index() and
  108. * hvcs_return_index() helper functions. The list is created with
  109. * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
  110. * Without these fixes hotplug vty-server adapter support goes crazy with this
  111. * driver if the user removes a vty-server adapter. Moved free_irq() outside of
  112. * the hvcs_final_close() function in order to get it out of the spinlock.
  113. * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
  114. * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
  115. * arch/powerepc/include/asm/hvcserver.h
  116. *
  117. * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
  118. * prevent possible lockup with realtime scheduling as similarily pointed out by
  119. * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
  120. * to reorder cleanup operations and prevent discarding of pending data during
  121. * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
  122. * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
  123. */
  124. #define HVCS_DRIVER_VERSION "1.3.3"
  125. MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
  126. MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
  127. MODULE_LICENSE("GPL");
  128. MODULE_VERSION(HVCS_DRIVER_VERSION);
  129. /*
  130. * Wait this long per iteration while trying to push buffered data to the
  131. * hypervisor before allowing the tty to complete a close operation.
  132. */
  133. #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  134. /*
  135. * Since the Linux TTY code does not currently (2-04-2004) support dynamic
  136. * addition of tty derived devices and we shouldn't allocate thousands of
  137. * tty_device pointers when the number of vty-server & vty partner connections
  138. * will most often be much lower than this, we'll arbitrarily allocate
  139. * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
  140. * register the tty_driver. This can be overridden using an insmod parameter.
  141. */
  142. #define HVCS_DEFAULT_SERVER_ADAPTERS 64
  143. /*
  144. * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
  145. * nodes as a sanity check. Theoretically there can be over 1 Billion
  146. * vty-server & vty partner connections.
  147. */
  148. #define HVCS_MAX_SERVER_ADAPTERS 1024
  149. /*
  150. * We let Linux assign us a major number and we start the minors at zero. There
  151. * is no intuitive mapping between minor number and the target vty-server
  152. * adapter except that each new vty-server adapter is always assigned to the
  153. * smallest minor number available.
  154. */
  155. #define HVCS_MINOR_START 0
  156. /*
  157. * The hcall interface involves putting 8 chars into each of two registers.
  158. * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
  159. * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
  160. * little (tiny) bit slower because an unaligned load is slower than aligned
  161. * load.
  162. */
  163. #define __ALIGNED__ __attribute__((__aligned__(8)))
  164. /*
  165. * How much data can firmware send with each hvc_put_chars()? Maybe this
  166. * should be moved into an architecture specific area.
  167. */
  168. #define HVCS_BUFF_LEN 16
  169. /*
  170. * This is the maximum amount of data we'll let the user send us (hvcs_write) at
  171. * once in a chunk as a sanity check.
  172. */
  173. #define HVCS_MAX_FROM_USER 4096
  174. /*
  175. * Be careful when adding flags to this line discipline. Don't add anything
  176. * that will cause echoing or we'll go into recursive loop echoing chars back
  177. * and forth with the console drivers.
  178. */
  179. static struct ktermios hvcs_tty_termios = {
  180. .c_iflag = IGNBRK | IGNPAR,
  181. .c_oflag = OPOST,
  182. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  183. .c_cc = INIT_C_CC,
  184. .c_ispeed = 38400,
  185. .c_ospeed = 38400
  186. };
  187. /*
  188. * This value is used to take the place of a command line parameter when the
  189. * module is inserted. It starts as -1 and stays as such if the user doesn't
  190. * specify a module insmod parameter. If they DO specify one then it is set to
  191. * the value of the integer passed in.
  192. */
  193. static int hvcs_parm_num_devs = -1;
  194. module_param(hvcs_parm_num_devs, int, 0);
  195. static const char hvcs_driver_name[] = "hvcs";
  196. static const char hvcs_device_node[] = "hvcs";
  197. static const char hvcs_driver_string[]
  198. = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
  199. /* Status of partner info rescan triggered via sysfs. */
  200. static int hvcs_rescan_status;
  201. static struct tty_driver *hvcs_tty_driver;
  202. /*
  203. * In order to be somewhat sane this driver always associates the hvcs_struct
  204. * index element with the numerically equal tty->index. This means that a
  205. * hotplugged vty-server adapter will always map to the lowest index valued
  206. * device node. If vty-servers were hotplug removed from the system and then
  207. * new ones added the new vty-server may have the largest slot number of all
  208. * the vty-server adapters in the partition but it may have the lowest dev node
  209. * index of all the adapters due to the hole left by the hotplug removed
  210. * adapter. There are a set of functions provided to get the lowest index for
  211. * a new device as well as return the index to the list. This list is allocated
  212. * with a number of elements equal to the number of device nodes requested when
  213. * the module was inserted.
  214. */
  215. static int *hvcs_index_list;
  216. /*
  217. * How large is the list? This is kept for traversal since the list is
  218. * dynamically created.
  219. */
  220. static int hvcs_index_count;
  221. /*
  222. * Used by the khvcsd to pick up I/O operations when the kernel_thread is
  223. * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
  224. */
  225. static int hvcs_kicked;
  226. /*
  227. * Use by the kthread construct for task operations like waking the sleeping
  228. * thread and stopping the kthread.
  229. */
  230. static struct task_struct *hvcs_task;
  231. /*
  232. * We allocate this for the use of all of the hvcs_structs when they fetch
  233. * partner info.
  234. */
  235. static unsigned long *hvcs_pi_buff;
  236. /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
  237. static DEFINE_SPINLOCK(hvcs_pi_lock);
  238. /* One vty-server per hvcs_struct */
  239. struct hvcs_struct {
  240. spinlock_t lock;
  241. /*
  242. * This index identifies this hvcs device as the complement to a
  243. * specific tty index.
  244. */
  245. unsigned int index;
  246. struct tty_struct *tty;
  247. int open_count;
  248. /*
  249. * Used to tell the driver kernel_thread what operations need to take
  250. * place upon this hvcs_struct instance.
  251. */
  252. int todo_mask;
  253. /*
  254. * This buffer is required so that when hvcs_write_room() reports that
  255. * it can send HVCS_BUFF_LEN characters that it will buffer the full
  256. * HVCS_BUFF_LEN characters if need be. This is essential for opost
  257. * writes since they do not do high level buffering and expect to be
  258. * able to send what the driver commits to sending buffering
  259. * [e.g. tab to space conversions in n_tty.c opost()].
  260. */
  261. char buffer[HVCS_BUFF_LEN];
  262. int chars_in_buffer;
  263. /*
  264. * Any variable below the kref is valid before a tty is connected and
  265. * stays valid after the tty is disconnected. These shouldn't be
  266. * whacked until the koject refcount reaches zero though some entries
  267. * may be changed via sysfs initiatives.
  268. */
  269. struct kref kref; /* ref count & hvcs_struct lifetime */
  270. int connected; /* is the vty-server currently connected to a vty? */
  271. uint32_t p_unit_address; /* partner unit address */
  272. uint32_t p_partition_ID; /* partner partition ID */
  273. char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
  274. struct list_head next; /* list management */
  275. struct vio_dev *vdev;
  276. };
  277. /* Required to back map a kref to its containing object */
  278. #define from_kref(k) container_of(k, struct hvcs_struct, kref)
  279. static LIST_HEAD(hvcs_structs);
  280. static DEFINE_SPINLOCK(hvcs_structs_lock);
  281. static void hvcs_unthrottle(struct tty_struct *tty);
  282. static void hvcs_throttle(struct tty_struct *tty);
  283. static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
  284. static int hvcs_write(struct tty_struct *tty,
  285. const unsigned char *buf, int count);
  286. static int hvcs_write_room(struct tty_struct *tty);
  287. static int hvcs_chars_in_buffer(struct tty_struct *tty);
  288. static int hvcs_has_pi(struct hvcs_struct *hvcsd);
  289. static void hvcs_set_pi(struct hvcs_partner_info *pi,
  290. struct hvcs_struct *hvcsd);
  291. static int hvcs_get_pi(struct hvcs_struct *hvcsd);
  292. static int hvcs_rescan_devices_list(void);
  293. static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
  294. static void hvcs_partner_free(struct hvcs_struct *hvcsd);
  295. static int hvcs_enable_device(struct hvcs_struct *hvcsd,
  296. uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
  297. static int hvcs_open(struct tty_struct *tty, struct file *filp);
  298. static void hvcs_close(struct tty_struct *tty, struct file *filp);
  299. static void hvcs_hangup(struct tty_struct * tty);
  300. static int __devinit hvcs_probe(struct vio_dev *dev,
  301. const struct vio_device_id *id);
  302. static int __devexit hvcs_remove(struct vio_dev *dev);
  303. static int __init hvcs_module_init(void);
  304. static void __exit hvcs_module_exit(void);
  305. #define HVCS_SCHED_READ 0x00000001
  306. #define HVCS_QUICK_READ 0x00000002
  307. #define HVCS_TRY_WRITE 0x00000004
  308. #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
  309. static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
  310. {
  311. return dev_get_drvdata(&viod->dev);
  312. }
  313. /* The sysfs interface for the driver and devices */
  314. static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
  315. {
  316. struct vio_dev *viod = to_vio_dev(dev);
  317. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  318. unsigned long flags;
  319. int retval;
  320. spin_lock_irqsave(&hvcsd->lock, flags);
  321. retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
  322. spin_unlock_irqrestore(&hvcsd->lock, flags);
  323. return retval;
  324. }
  325. static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
  326. static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
  327. {
  328. struct vio_dev *viod = to_vio_dev(dev);
  329. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  330. unsigned long flags;
  331. int retval;
  332. spin_lock_irqsave(&hvcsd->lock, flags);
  333. retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
  334. spin_unlock_irqrestore(&hvcsd->lock, flags);
  335. return retval;
  336. }
  337. static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
  338. static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
  339. size_t count)
  340. {
  341. /*
  342. * Don't need this feature at the present time because firmware doesn't
  343. * yet support multiple partners.
  344. */
  345. printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
  346. return -EPERM;
  347. }
  348. static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
  349. {
  350. struct vio_dev *viod = to_vio_dev(dev);
  351. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  352. unsigned long flags;
  353. int retval;
  354. spin_lock_irqsave(&hvcsd->lock, flags);
  355. retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
  356. spin_unlock_irqrestore(&hvcsd->lock, flags);
  357. return retval;
  358. }
  359. static DEVICE_ATTR(current_vty,
  360. S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
  361. static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
  362. size_t count)
  363. {
  364. struct vio_dev *viod = to_vio_dev(dev);
  365. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  366. unsigned long flags;
  367. /* writing a '0' to this sysfs entry will result in the disconnect. */
  368. if (simple_strtol(buf, NULL, 0) != 0)
  369. return -EINVAL;
  370. spin_lock_irqsave(&hvcsd->lock, flags);
  371. if (hvcsd->open_count > 0) {
  372. spin_unlock_irqrestore(&hvcsd->lock, flags);
  373. printk(KERN_INFO "HVCS: vterm state unchanged. "
  374. "The hvcs device node is still in use.\n");
  375. return -EPERM;
  376. }
  377. if (hvcsd->connected == 0) {
  378. spin_unlock_irqrestore(&hvcsd->lock, flags);
  379. printk(KERN_INFO "HVCS: vterm state unchanged. The"
  380. " vty-server is not connected to a vty.\n");
  381. return -EPERM;
  382. }
  383. hvcs_partner_free(hvcsd);
  384. printk(KERN_INFO "HVCS: Closed vty-server@%X and"
  385. " partner vty@%X:%d connection.\n",
  386. hvcsd->vdev->unit_address,
  387. hvcsd->p_unit_address,
  388. (uint32_t)hvcsd->p_partition_ID);
  389. spin_unlock_irqrestore(&hvcsd->lock, flags);
  390. return count;
  391. }
  392. static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  393. {
  394. struct vio_dev *viod = to_vio_dev(dev);
  395. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  396. unsigned long flags;
  397. int retval;
  398. spin_lock_irqsave(&hvcsd->lock, flags);
  399. retval = sprintf(buf, "%d\n", hvcsd->connected);
  400. spin_unlock_irqrestore(&hvcsd->lock, flags);
  401. return retval;
  402. }
  403. static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
  404. hvcs_vterm_state_show, hvcs_vterm_state_store);
  405. static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
  406. {
  407. struct vio_dev *viod = to_vio_dev(dev);
  408. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  409. unsigned long flags;
  410. int retval;
  411. spin_lock_irqsave(&hvcsd->lock, flags);
  412. retval = sprintf(buf, "%d\n", hvcsd->index);
  413. spin_unlock_irqrestore(&hvcsd->lock, flags);
  414. return retval;
  415. }
  416. static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
  417. static struct attribute *hvcs_attrs[] = {
  418. &dev_attr_partner_vtys.attr,
  419. &dev_attr_partner_clcs.attr,
  420. &dev_attr_current_vty.attr,
  421. &dev_attr_vterm_state.attr,
  422. &dev_attr_index.attr,
  423. NULL,
  424. };
  425. static struct attribute_group hvcs_attr_group = {
  426. .attrs = hvcs_attrs,
  427. };
  428. static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
  429. {
  430. /* A 1 means it is updating, a 0 means it is done updating */
  431. return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
  432. }
  433. static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
  434. size_t count)
  435. {
  436. if ((simple_strtol(buf, NULL, 0) != 1)
  437. && (hvcs_rescan_status != 0))
  438. return -EINVAL;
  439. hvcs_rescan_status = 1;
  440. printk(KERN_INFO "HVCS: rescanning partner info for all"
  441. " vty-servers.\n");
  442. hvcs_rescan_devices_list();
  443. hvcs_rescan_status = 0;
  444. return count;
  445. }
  446. static DRIVER_ATTR(rescan,
  447. S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
  448. static void hvcs_kick(void)
  449. {
  450. hvcs_kicked = 1;
  451. wmb();
  452. wake_up_process(hvcs_task);
  453. }
  454. static void hvcs_unthrottle(struct tty_struct *tty)
  455. {
  456. struct hvcs_struct *hvcsd = tty->driver_data;
  457. unsigned long flags;
  458. spin_lock_irqsave(&hvcsd->lock, flags);
  459. hvcsd->todo_mask |= HVCS_SCHED_READ;
  460. spin_unlock_irqrestore(&hvcsd->lock, flags);
  461. hvcs_kick();
  462. }
  463. static void hvcs_throttle(struct tty_struct *tty)
  464. {
  465. struct hvcs_struct *hvcsd = tty->driver_data;
  466. unsigned long flags;
  467. spin_lock_irqsave(&hvcsd->lock, flags);
  468. vio_disable_interrupts(hvcsd->vdev);
  469. spin_unlock_irqrestore(&hvcsd->lock, flags);
  470. }
  471. /*
  472. * If the device is being removed we don't have to worry about this interrupt
  473. * handler taking any further interrupts because they are disabled which means
  474. * the hvcs_struct will always be valid in this handler.
  475. */
  476. static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
  477. {
  478. struct hvcs_struct *hvcsd = dev_instance;
  479. spin_lock(&hvcsd->lock);
  480. vio_disable_interrupts(hvcsd->vdev);
  481. hvcsd->todo_mask |= HVCS_SCHED_READ;
  482. spin_unlock(&hvcsd->lock);
  483. hvcs_kick();
  484. return IRQ_HANDLED;
  485. }
  486. /* This function must be called with the hvcsd->lock held */
  487. static void hvcs_try_write(struct hvcs_struct *hvcsd)
  488. {
  489. uint32_t unit_address = hvcsd->vdev->unit_address;
  490. struct tty_struct *tty = hvcsd->tty;
  491. int sent;
  492. if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
  493. /* won't send partial writes */
  494. sent = hvc_put_chars(unit_address,
  495. &hvcsd->buffer[0],
  496. hvcsd->chars_in_buffer );
  497. if (sent > 0) {
  498. hvcsd->chars_in_buffer = 0;
  499. /* wmb(); */
  500. hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
  501. /* wmb(); */
  502. /*
  503. * We are still obligated to deliver the data to the
  504. * hypervisor even if the tty has been closed because
  505. * we commited to delivering it. But don't try to wake
  506. * a non-existent tty.
  507. */
  508. if (tty) {
  509. tty_wakeup(tty);
  510. }
  511. }
  512. }
  513. }
  514. static int hvcs_io(struct hvcs_struct *hvcsd)
  515. {
  516. uint32_t unit_address;
  517. struct tty_struct *tty;
  518. char buf[HVCS_BUFF_LEN] __ALIGNED__;
  519. unsigned long flags;
  520. int got = 0;
  521. spin_lock_irqsave(&hvcsd->lock, flags);
  522. unit_address = hvcsd->vdev->unit_address;
  523. tty = hvcsd->tty;
  524. hvcs_try_write(hvcsd);
  525. if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
  526. hvcsd->todo_mask &= ~(HVCS_READ_MASK);
  527. goto bail;
  528. } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
  529. goto bail;
  530. /* remove the read masks */
  531. hvcsd->todo_mask &= ~(HVCS_READ_MASK);
  532. if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
  533. got = hvc_get_chars(unit_address,
  534. &buf[0],
  535. HVCS_BUFF_LEN);
  536. tty_insert_flip_string(tty, buf, got);
  537. }
  538. /* Give the TTY time to process the data we just sent. */
  539. if (got)
  540. hvcsd->todo_mask |= HVCS_QUICK_READ;
  541. spin_unlock_irqrestore(&hvcsd->lock, flags);
  542. /* This is synch because tty->low_latency == 1 */
  543. if(got)
  544. tty_flip_buffer_push(tty);
  545. if (!got) {
  546. /* Do this _after_ the flip_buffer_push */
  547. spin_lock_irqsave(&hvcsd->lock, flags);
  548. vio_enable_interrupts(hvcsd->vdev);
  549. spin_unlock_irqrestore(&hvcsd->lock, flags);
  550. }
  551. return hvcsd->todo_mask;
  552. bail:
  553. spin_unlock_irqrestore(&hvcsd->lock, flags);
  554. return hvcsd->todo_mask;
  555. }
  556. static int khvcsd(void *unused)
  557. {
  558. struct hvcs_struct *hvcsd;
  559. int hvcs_todo_mask;
  560. __set_current_state(TASK_RUNNING);
  561. do {
  562. hvcs_todo_mask = 0;
  563. hvcs_kicked = 0;
  564. wmb();
  565. spin_lock(&hvcs_structs_lock);
  566. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  567. hvcs_todo_mask |= hvcs_io(hvcsd);
  568. }
  569. spin_unlock(&hvcs_structs_lock);
  570. /*
  571. * If any of the hvcs adapters want to try a write or quick read
  572. * don't schedule(), yield a smidgen then execute the hvcs_io
  573. * thread again for those that want the write.
  574. */
  575. if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
  576. yield();
  577. continue;
  578. }
  579. set_current_state(TASK_INTERRUPTIBLE);
  580. if (!hvcs_kicked)
  581. schedule();
  582. __set_current_state(TASK_RUNNING);
  583. } while (!kthread_should_stop());
  584. return 0;
  585. }
  586. static struct vio_device_id hvcs_driver_table[] __devinitdata= {
  587. {"serial-server", "hvterm2"},
  588. { "", "" }
  589. };
  590. MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
  591. static void hvcs_return_index(int index)
  592. {
  593. /* Paranoia check */
  594. if (!hvcs_index_list)
  595. return;
  596. if (index < 0 || index >= hvcs_index_count)
  597. return;
  598. if (hvcs_index_list[index] == -1)
  599. return;
  600. else
  601. hvcs_index_list[index] = -1;
  602. }
  603. /* callback when the kref ref count reaches zero */
  604. static void destroy_hvcs_struct(struct kref *kref)
  605. {
  606. struct hvcs_struct *hvcsd = from_kref(kref);
  607. struct vio_dev *vdev;
  608. unsigned long flags;
  609. spin_lock(&hvcs_structs_lock);
  610. spin_lock_irqsave(&hvcsd->lock, flags);
  611. /* the list_del poisons the pointers */
  612. list_del(&(hvcsd->next));
  613. if (hvcsd->connected == 1) {
  614. hvcs_partner_free(hvcsd);
  615. printk(KERN_INFO "HVCS: Closed vty-server@%X and"
  616. " partner vty@%X:%d connection.\n",
  617. hvcsd->vdev->unit_address,
  618. hvcsd->p_unit_address,
  619. (uint32_t)hvcsd->p_partition_ID);
  620. }
  621. printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
  622. hvcsd->vdev->unit_address);
  623. vdev = hvcsd->vdev;
  624. hvcsd->vdev = NULL;
  625. hvcsd->p_unit_address = 0;
  626. hvcsd->p_partition_ID = 0;
  627. hvcs_return_index(hvcsd->index);
  628. memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
  629. spin_unlock_irqrestore(&hvcsd->lock, flags);
  630. spin_unlock(&hvcs_structs_lock);
  631. sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
  632. kfree(hvcsd);
  633. }
  634. static int hvcs_get_index(void)
  635. {
  636. int i;
  637. /* Paranoia check */
  638. if (!hvcs_index_list) {
  639. printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
  640. return -EFAULT;
  641. }
  642. /* Find the numerically lowest first free index. */
  643. for(i = 0; i < hvcs_index_count; i++) {
  644. if (hvcs_index_list[i] == -1) {
  645. hvcs_index_list[i] = 0;
  646. return i;
  647. }
  648. }
  649. return -1;
  650. }
  651. static int __devinit hvcs_probe(
  652. struct vio_dev *dev,
  653. const struct vio_device_id *id)
  654. {
  655. struct hvcs_struct *hvcsd;
  656. int index;
  657. int retval;
  658. if (!dev || !id) {
  659. printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
  660. return -EPERM;
  661. }
  662. /* early to avoid cleanup on failure */
  663. index = hvcs_get_index();
  664. if (index < 0) {
  665. return -EFAULT;
  666. }
  667. hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
  668. if (!hvcsd)
  669. return -ENODEV;
  670. spin_lock_init(&hvcsd->lock);
  671. /* Automatically incs the refcount the first time */
  672. kref_init(&hvcsd->kref);
  673. hvcsd->vdev = dev;
  674. dev_set_drvdata(&dev->dev, hvcsd);
  675. hvcsd->index = index;
  676. /* hvcsd->index = ++hvcs_struct_count; */
  677. hvcsd->chars_in_buffer = 0;
  678. hvcsd->todo_mask = 0;
  679. hvcsd->connected = 0;
  680. /*
  681. * This will populate the hvcs_struct's partner info fields for the
  682. * first time.
  683. */
  684. if (hvcs_get_pi(hvcsd)) {
  685. printk(KERN_ERR "HVCS: Failed to fetch partner"
  686. " info for vty-server@%X on device probe.\n",
  687. hvcsd->vdev->unit_address);
  688. }
  689. /*
  690. * If a user app opens a tty that corresponds to this vty-server before
  691. * the hvcs_struct has been added to the devices list then the user app
  692. * will get -ENODEV.
  693. */
  694. spin_lock(&hvcs_structs_lock);
  695. list_add_tail(&(hvcsd->next), &hvcs_structs);
  696. spin_unlock(&hvcs_structs_lock);
  697. retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
  698. if (retval) {
  699. printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
  700. hvcsd->vdev->unit_address);
  701. return retval;
  702. }
  703. printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
  704. /*
  705. * DON'T enable interrupts here because there is no user to receive the
  706. * data.
  707. */
  708. return 0;
  709. }
  710. static int __devexit hvcs_remove(struct vio_dev *dev)
  711. {
  712. struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
  713. unsigned long flags;
  714. struct tty_struct *tty;
  715. if (!hvcsd)
  716. return -ENODEV;
  717. /* By this time the vty-server won't be getting any more interrupts */
  718. spin_lock_irqsave(&hvcsd->lock, flags);
  719. tty = hvcsd->tty;
  720. spin_unlock_irqrestore(&hvcsd->lock, flags);
  721. /*
  722. * Let the last holder of this object cause it to be removed, which
  723. * would probably be tty_hangup below.
  724. */
  725. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  726. /*
  727. * The hangup is a scheduled function which will auto chain call
  728. * hvcs_hangup. The tty should always be valid at this time unless a
  729. * simultaneous tty close already cleaned up the hvcs_struct.
  730. */
  731. if (tty)
  732. tty_hangup(tty);
  733. printk(KERN_INFO "HVCS: vty-server@%X removed from the"
  734. " vio bus.\n", dev->unit_address);
  735. return 0;
  736. };
  737. static struct vio_driver hvcs_vio_driver = {
  738. .id_table = hvcs_driver_table,
  739. .probe = hvcs_probe,
  740. .remove = __devexit_p(hvcs_remove),
  741. .driver = {
  742. .name = hvcs_driver_name,
  743. .owner = THIS_MODULE,
  744. }
  745. };
  746. /* Only called from hvcs_get_pi please */
  747. static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
  748. {
  749. int clclength;
  750. hvcsd->p_unit_address = pi->unit_address;
  751. hvcsd->p_partition_ID = pi->partition_ID;
  752. clclength = strlen(&pi->location_code[0]);
  753. if (clclength > HVCS_CLC_LENGTH)
  754. clclength = HVCS_CLC_LENGTH;
  755. /* copy the null-term char too */
  756. strncpy(&hvcsd->p_location_code[0],
  757. &pi->location_code[0], clclength + 1);
  758. }
  759. /*
  760. * Traverse the list and add the partner info that is found to the hvcs_struct
  761. * struct entry. NOTE: At this time I know that partner info will return a
  762. * single entry but in the future there may be multiple partner info entries per
  763. * vty-server and you'll want to zero out that list and reset it. If for some
  764. * reason you have an old version of this driver but there IS more than one
  765. * partner info then hvcsd->p_* will hold the last partner info data from the
  766. * firmware query. A good way to update this code would be to replace the three
  767. * partner info fields in hvcs_struct with a list of hvcs_partner_info
  768. * instances.
  769. *
  770. * This function must be called with the hvcsd->lock held.
  771. */
  772. static int hvcs_get_pi(struct hvcs_struct *hvcsd)
  773. {
  774. struct hvcs_partner_info *pi;
  775. uint32_t unit_address = hvcsd->vdev->unit_address;
  776. struct list_head head;
  777. int retval;
  778. spin_lock(&hvcs_pi_lock);
  779. if (!hvcs_pi_buff) {
  780. spin_unlock(&hvcs_pi_lock);
  781. return -EFAULT;
  782. }
  783. retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
  784. spin_unlock(&hvcs_pi_lock);
  785. if (retval) {
  786. printk(KERN_ERR "HVCS: Failed to fetch partner"
  787. " info for vty-server@%x.\n", unit_address);
  788. return retval;
  789. }
  790. /* nixes the values if the partner vty went away */
  791. hvcsd->p_unit_address = 0;
  792. hvcsd->p_partition_ID = 0;
  793. list_for_each_entry(pi, &head, node)
  794. hvcs_set_pi(pi, hvcsd);
  795. hvcs_free_partner_info(&head);
  796. return 0;
  797. }
  798. /*
  799. * This function is executed by the driver "rescan" sysfs entry. It shouldn't
  800. * be executed elsewhere, in order to prevent deadlock issues.
  801. */
  802. static int hvcs_rescan_devices_list(void)
  803. {
  804. struct hvcs_struct *hvcsd;
  805. unsigned long flags;
  806. spin_lock(&hvcs_structs_lock);
  807. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  808. spin_lock_irqsave(&hvcsd->lock, flags);
  809. hvcs_get_pi(hvcsd);
  810. spin_unlock_irqrestore(&hvcsd->lock, flags);
  811. }
  812. spin_unlock(&hvcs_structs_lock);
  813. return 0;
  814. }
  815. /*
  816. * Farm this off into its own function because it could be more complex once
  817. * multiple partners support is added. This function should be called with
  818. * the hvcsd->lock held.
  819. */
  820. static int hvcs_has_pi(struct hvcs_struct *hvcsd)
  821. {
  822. if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
  823. return 0;
  824. return 1;
  825. }
  826. /*
  827. * NOTE: It is possible that the super admin removed a partner vty and then
  828. * added a different vty as the new partner.
  829. *
  830. * This function must be called with the hvcsd->lock held.
  831. */
  832. static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
  833. {
  834. int retval;
  835. unsigned int unit_address = hvcsd->vdev->unit_address;
  836. /*
  837. * If there wasn't any pi when the device was added it doesn't meant
  838. * there isn't any now. This driver isn't notified when a new partner
  839. * vty is added to a vty-server so we discover changes on our own.
  840. * Please see comments in hvcs_register_connection() for justification
  841. * of this bizarre code.
  842. */
  843. retval = hvcs_register_connection(unit_address,
  844. hvcsd->p_partition_ID,
  845. hvcsd->p_unit_address);
  846. if (!retval) {
  847. hvcsd->connected = 1;
  848. return 0;
  849. } else if (retval != -EINVAL)
  850. return retval;
  851. /*
  852. * As per the spec re-get the pi and try again if -EINVAL after the
  853. * first connection attempt.
  854. */
  855. if (hvcs_get_pi(hvcsd))
  856. return -ENOMEM;
  857. if (!hvcs_has_pi(hvcsd))
  858. return -ENODEV;
  859. retval = hvcs_register_connection(unit_address,
  860. hvcsd->p_partition_ID,
  861. hvcsd->p_unit_address);
  862. if (retval != -EINVAL) {
  863. hvcsd->connected = 1;
  864. return retval;
  865. }
  866. /*
  867. * EBUSY is the most likely scenario though the vty could have been
  868. * removed or there really could be an hcall error due to the parameter
  869. * data but thanks to ambiguous firmware return codes we can't really
  870. * tell.
  871. */
  872. printk(KERN_INFO "HVCS: vty-server or partner"
  873. " vty is busy. Try again later.\n");
  874. return -EBUSY;
  875. }
  876. /* This function must be called with the hvcsd->lock held */
  877. static void hvcs_partner_free(struct hvcs_struct *hvcsd)
  878. {
  879. int retval;
  880. do {
  881. retval = hvcs_free_connection(hvcsd->vdev->unit_address);
  882. } while (retval == -EBUSY);
  883. hvcsd->connected = 0;
  884. }
  885. /* This helper function must be called WITHOUT the hvcsd->lock held */
  886. static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
  887. unsigned int irq, struct vio_dev *vdev)
  888. {
  889. unsigned long flags;
  890. int rc;
  891. /*
  892. * It is possible that the vty-server was removed between the time that
  893. * the conn was registered and now.
  894. */
  895. if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
  896. IRQF_DISABLED, "ibmhvcs", hvcsd))) {
  897. /*
  898. * It is possible the vty-server was removed after the irq was
  899. * requested but before we have time to enable interrupts.
  900. */
  901. if (vio_enable_interrupts(vdev) == H_SUCCESS)
  902. return 0;
  903. else {
  904. printk(KERN_ERR "HVCS: int enable failed for"
  905. " vty-server@%X.\n", unit_address);
  906. free_irq(irq, hvcsd);
  907. }
  908. } else
  909. printk(KERN_ERR "HVCS: irq req failed for"
  910. " vty-server@%X.\n", unit_address);
  911. spin_lock_irqsave(&hvcsd->lock, flags);
  912. hvcs_partner_free(hvcsd);
  913. spin_unlock_irqrestore(&hvcsd->lock, flags);
  914. return rc;
  915. }
  916. /*
  917. * This always increments the kref ref count if the call is successful.
  918. * Please remember to dec when you are done with the instance.
  919. *
  920. * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
  921. * calling this function or you will get deadlock.
  922. */
  923. static struct hvcs_struct *hvcs_get_by_index(int index)
  924. {
  925. struct hvcs_struct *hvcsd = NULL;
  926. unsigned long flags;
  927. spin_lock(&hvcs_structs_lock);
  928. /* We can immediately discard OOB requests */
  929. if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
  930. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  931. spin_lock_irqsave(&hvcsd->lock, flags);
  932. if (hvcsd->index == index) {
  933. kref_get(&hvcsd->kref);
  934. spin_unlock_irqrestore(&hvcsd->lock, flags);
  935. spin_unlock(&hvcs_structs_lock);
  936. return hvcsd;
  937. }
  938. spin_unlock_irqrestore(&hvcsd->lock, flags);
  939. }
  940. hvcsd = NULL;
  941. }
  942. spin_unlock(&hvcs_structs_lock);
  943. return hvcsd;
  944. }
  945. /*
  946. * This is invoked via the tty_open interface when a user app connects to the
  947. * /dev node.
  948. */
  949. static int hvcs_open(struct tty_struct *tty, struct file *filp)
  950. {
  951. struct hvcs_struct *hvcsd;
  952. int rc, retval = 0;
  953. unsigned long flags;
  954. unsigned int irq;
  955. struct vio_dev *vdev;
  956. unsigned long unit_address;
  957. if (tty->driver_data)
  958. goto fast_open;
  959. /*
  960. * Is there a vty-server that shares the same index?
  961. * This function increments the kref index.
  962. */
  963. if (!(hvcsd = hvcs_get_by_index(tty->index))) {
  964. printk(KERN_WARNING "HVCS: open failed, no device associated"
  965. " with tty->index %d.\n", tty->index);
  966. return -ENODEV;
  967. }
  968. spin_lock_irqsave(&hvcsd->lock, flags);
  969. if (hvcsd->connected == 0)
  970. if ((retval = hvcs_partner_connect(hvcsd)))
  971. goto error_release;
  972. hvcsd->open_count = 1;
  973. hvcsd->tty = tty;
  974. tty->driver_data = hvcsd;
  975. memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
  976. /*
  977. * Save these in the spinlock for the enable operations that need them
  978. * outside of the spinlock.
  979. */
  980. irq = hvcsd->vdev->irq;
  981. vdev = hvcsd->vdev;
  982. unit_address = hvcsd->vdev->unit_address;
  983. hvcsd->todo_mask |= HVCS_SCHED_READ;
  984. spin_unlock_irqrestore(&hvcsd->lock, flags);
  985. /*
  986. * This must be done outside of the spinlock because it requests irqs
  987. * and will grab the spinlock and free the connection if it fails.
  988. */
  989. if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
  990. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  991. printk(KERN_WARNING "HVCS: enable device failed.\n");
  992. return rc;
  993. }
  994. goto open_success;
  995. fast_open:
  996. hvcsd = tty->driver_data;
  997. spin_lock_irqsave(&hvcsd->lock, flags);
  998. kref_get(&hvcsd->kref);
  999. hvcsd->open_count++;
  1000. hvcsd->todo_mask |= HVCS_SCHED_READ;
  1001. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1002. open_success:
  1003. hvcs_kick();
  1004. printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
  1005. hvcsd->vdev->unit_address );
  1006. return 0;
  1007. error_release:
  1008. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1009. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  1010. printk(KERN_WARNING "HVCS: partner connect failed.\n");
  1011. return retval;
  1012. }
  1013. static void hvcs_close(struct tty_struct *tty, struct file *filp)
  1014. {
  1015. struct hvcs_struct *hvcsd;
  1016. unsigned long flags;
  1017. int irq = NO_IRQ;
  1018. /*
  1019. * Is someone trying to close the file associated with this device after
  1020. * we have hung up? If so tty->driver_data wouldn't be valid.
  1021. */
  1022. if (tty_hung_up_p(filp))
  1023. return;
  1024. /*
  1025. * No driver_data means that this close was probably issued after a
  1026. * failed hvcs_open by the tty layer's release_dev() api and we can just
  1027. * exit cleanly.
  1028. */
  1029. if (!tty->driver_data)
  1030. return;
  1031. hvcsd = tty->driver_data;
  1032. spin_lock_irqsave(&hvcsd->lock, flags);
  1033. if (--hvcsd->open_count == 0) {
  1034. vio_disable_interrupts(hvcsd->vdev);
  1035. /*
  1036. * NULL this early so that the kernel_thread doesn't try to
  1037. * execute any operations on the TTY even though it is obligated
  1038. * to deliver any pending I/O to the hypervisor.
  1039. */
  1040. hvcsd->tty = NULL;
  1041. irq = hvcsd->vdev->irq;
  1042. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1043. tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
  1044. /*
  1045. * This line is important because it tells hvcs_open that this
  1046. * device needs to be re-configured the next time hvcs_open is
  1047. * called.
  1048. */
  1049. tty->driver_data = NULL;
  1050. free_irq(irq, hvcsd);
  1051. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  1052. return;
  1053. } else if (hvcsd->open_count < 0) {
  1054. printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
  1055. " is missmanaged.\n",
  1056. hvcsd->vdev->unit_address, hvcsd->open_count);
  1057. }
  1058. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1059. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  1060. }
  1061. static void hvcs_hangup(struct tty_struct * tty)
  1062. {
  1063. struct hvcs_struct *hvcsd = tty->driver_data;
  1064. unsigned long flags;
  1065. int temp_open_count;
  1066. int irq = NO_IRQ;
  1067. spin_lock_irqsave(&hvcsd->lock, flags);
  1068. /* Preserve this so that we know how many kref refs to put */
  1069. temp_open_count = hvcsd->open_count;
  1070. /*
  1071. * Don't kref put inside the spinlock because the destruction
  1072. * callback may use the spinlock and it may get called before the
  1073. * spinlock has been released.
  1074. */
  1075. vio_disable_interrupts(hvcsd->vdev);
  1076. hvcsd->todo_mask = 0;
  1077. /* I don't think the tty needs the hvcs_struct pointer after a hangup */
  1078. hvcsd->tty->driver_data = NULL;
  1079. hvcsd->tty = NULL;
  1080. hvcsd->open_count = 0;
  1081. /* This will drop any buffered data on the floor which is OK in a hangup
  1082. * scenario. */
  1083. memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
  1084. hvcsd->chars_in_buffer = 0;
  1085. irq = hvcsd->vdev->irq;
  1086. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1087. free_irq(irq, hvcsd);
  1088. /*
  1089. * We need to kref_put() for every open_count we have since the
  1090. * tty_hangup() function doesn't invoke a close per open connection on a
  1091. * non-console device.
  1092. */
  1093. while(temp_open_count) {
  1094. --temp_open_count;
  1095. /*
  1096. * The final put will trigger destruction of the hvcs_struct.
  1097. * NOTE: If this hangup was signaled from user space then the
  1098. * final put will never happen.
  1099. */
  1100. kref_put(&hvcsd->kref, destroy_hvcs_struct);
  1101. }
  1102. }
  1103. /*
  1104. * NOTE: This is almost always from_user since user level apps interact with the
  1105. * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
  1106. * hvcs_remove (which removes the target device and executes tty_hangup()) that
  1107. * tty_hangup will allow hvcs_write time to complete execution before it
  1108. * terminates our device.
  1109. */
  1110. static int hvcs_write(struct tty_struct *tty,
  1111. const unsigned char *buf, int count)
  1112. {
  1113. struct hvcs_struct *hvcsd = tty->driver_data;
  1114. unsigned int unit_address;
  1115. const unsigned char *charbuf;
  1116. unsigned long flags;
  1117. int total_sent = 0;
  1118. int tosend = 0;
  1119. int result = 0;
  1120. /*
  1121. * If they don't check the return code off of their open they may
  1122. * attempt this even if there is no connected device.
  1123. */
  1124. if (!hvcsd)
  1125. return -ENODEV;
  1126. /* Reasonable size to prevent user level flooding */
  1127. if (count > HVCS_MAX_FROM_USER) {
  1128. printk(KERN_WARNING "HVCS write: count being truncated to"
  1129. " HVCS_MAX_FROM_USER.\n");
  1130. count = HVCS_MAX_FROM_USER;
  1131. }
  1132. charbuf = buf;
  1133. spin_lock_irqsave(&hvcsd->lock, flags);
  1134. /*
  1135. * Somehow an open succedded but the device was removed or the
  1136. * connection terminated between the vty-server and partner vty during
  1137. * the middle of a write operation? This is a crummy place to do this
  1138. * but we want to keep it all in the spinlock.
  1139. */
  1140. if (hvcsd->open_count <= 0) {
  1141. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1142. return -ENODEV;
  1143. }
  1144. unit_address = hvcsd->vdev->unit_address;
  1145. while (count > 0) {
  1146. tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
  1147. /*
  1148. * No more space, this probably means that the last call to
  1149. * hvcs_write() didn't succeed and the buffer was filled up.
  1150. */
  1151. if (!tosend)
  1152. break;
  1153. memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
  1154. &charbuf[total_sent],
  1155. tosend);
  1156. hvcsd->chars_in_buffer += tosend;
  1157. result = 0;
  1158. /*
  1159. * If this is true then we don't want to try writing to the
  1160. * hypervisor because that is the kernel_threads job now. We'll
  1161. * just add to the buffer.
  1162. */
  1163. if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
  1164. /* won't send partial writes */
  1165. result = hvc_put_chars(unit_address,
  1166. &hvcsd->buffer[0],
  1167. hvcsd->chars_in_buffer);
  1168. /*
  1169. * Since we know we have enough room in hvcsd->buffer for
  1170. * tosend we record that it was sent regardless of whether the
  1171. * hypervisor actually took it because we have it buffered.
  1172. */
  1173. total_sent+=tosend;
  1174. count-=tosend;
  1175. if (result == 0) {
  1176. hvcsd->todo_mask |= HVCS_TRY_WRITE;
  1177. hvcs_kick();
  1178. break;
  1179. }
  1180. hvcsd->chars_in_buffer = 0;
  1181. /*
  1182. * Test after the chars_in_buffer reset otherwise this could
  1183. * deadlock our writes if hvc_put_chars fails.
  1184. */
  1185. if (result < 0)
  1186. break;
  1187. }
  1188. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1189. if (result == -1)
  1190. return -EIO;
  1191. else
  1192. return total_sent;
  1193. }
  1194. /*
  1195. * This is really asking how much can we guarentee that we can send or that we
  1196. * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
  1197. * return value, hence the reason for hvcs_struct buffering.
  1198. */
  1199. static int hvcs_write_room(struct tty_struct *tty)
  1200. {
  1201. struct hvcs_struct *hvcsd = tty->driver_data;
  1202. if (!hvcsd || hvcsd->open_count <= 0)
  1203. return 0;
  1204. return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
  1205. }
  1206. static int hvcs_chars_in_buffer(struct tty_struct *tty)
  1207. {
  1208. struct hvcs_struct *hvcsd = tty->driver_data;
  1209. return hvcsd->chars_in_buffer;
  1210. }
  1211. static const struct tty_operations hvcs_ops = {
  1212. .open = hvcs_open,
  1213. .close = hvcs_close,
  1214. .hangup = hvcs_hangup,
  1215. .write = hvcs_write,
  1216. .write_room = hvcs_write_room,
  1217. .chars_in_buffer = hvcs_chars_in_buffer,
  1218. .unthrottle = hvcs_unthrottle,
  1219. .throttle = hvcs_throttle,
  1220. };
  1221. static int hvcs_alloc_index_list(int n)
  1222. {
  1223. int i;
  1224. hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
  1225. if (!hvcs_index_list)
  1226. return -ENOMEM;
  1227. hvcs_index_count = n;
  1228. for (i = 0; i < hvcs_index_count; i++)
  1229. hvcs_index_list[i] = -1;
  1230. return 0;
  1231. }
  1232. static void hvcs_free_index_list(void)
  1233. {
  1234. /* Paranoia check to be thorough. */
  1235. kfree(hvcs_index_list);
  1236. hvcs_index_list = NULL;
  1237. hvcs_index_count = 0;
  1238. }
  1239. static int __init hvcs_module_init(void)
  1240. {
  1241. int rc;
  1242. int num_ttys_to_alloc;
  1243. printk(KERN_INFO "Initializing %s\n", hvcs_driver_string);
  1244. /* Has the user specified an overload with an insmod param? */
  1245. if (hvcs_parm_num_devs <= 0 ||
  1246. (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
  1247. num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
  1248. } else
  1249. num_ttys_to_alloc = hvcs_parm_num_devs;
  1250. hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
  1251. if (!hvcs_tty_driver)
  1252. return -ENOMEM;
  1253. if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
  1254. rc = -ENOMEM;
  1255. goto index_fail;
  1256. }
  1257. hvcs_tty_driver->owner = THIS_MODULE;
  1258. hvcs_tty_driver->driver_name = hvcs_driver_name;
  1259. hvcs_tty_driver->name = hvcs_device_node;
  1260. /*
  1261. * We'll let the system assign us a major number, indicated by leaving
  1262. * it blank.
  1263. */
  1264. hvcs_tty_driver->minor_start = HVCS_MINOR_START;
  1265. hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1266. /*
  1267. * We role our own so that we DONT ECHO. We can't echo because the
  1268. * device we are connecting to already echoes by default and this would
  1269. * throw us into a horrible recursive echo-echo-echo loop.
  1270. */
  1271. hvcs_tty_driver->init_termios = hvcs_tty_termios;
  1272. hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
  1273. tty_set_operations(hvcs_tty_driver, &hvcs_ops);
  1274. /*
  1275. * The following call will result in sysfs entries that denote the
  1276. * dynamically assigned major and minor numbers for our devices.
  1277. */
  1278. if (tty_register_driver(hvcs_tty_driver)) {
  1279. printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
  1280. rc = -EIO;
  1281. goto register_fail;
  1282. }
  1283. hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1284. if (!hvcs_pi_buff) {
  1285. rc = -ENOMEM;
  1286. goto buff_alloc_fail;
  1287. }
  1288. hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
  1289. if (IS_ERR(hvcs_task)) {
  1290. printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n");
  1291. rc = -EIO;
  1292. goto kthread_fail;
  1293. }
  1294. rc = vio_register_driver(&hvcs_vio_driver);
  1295. if (rc) {
  1296. printk(KERN_ERR "HVCS: can't register vio driver\n");
  1297. goto vio_fail;
  1298. }
  1299. /*
  1300. * This needs to be done AFTER the vio_register_driver() call or else
  1301. * the kobjects won't be initialized properly.
  1302. */
  1303. rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
  1304. if (rc) {
  1305. printk(KERN_ERR "HVCS: sysfs attr create failed\n");
  1306. goto attr_fail;
  1307. }
  1308. printk(KERN_INFO "HVCS: driver module inserted.\n");
  1309. return 0;
  1310. attr_fail:
  1311. vio_unregister_driver(&hvcs_vio_driver);
  1312. vio_fail:
  1313. kthread_stop(hvcs_task);
  1314. kthread_fail:
  1315. kfree(hvcs_pi_buff);
  1316. buff_alloc_fail:
  1317. tty_unregister_driver(hvcs_tty_driver);
  1318. register_fail:
  1319. hvcs_free_index_list();
  1320. index_fail:
  1321. put_tty_driver(hvcs_tty_driver);
  1322. hvcs_tty_driver = NULL;
  1323. return rc;
  1324. }
  1325. static void __exit hvcs_module_exit(void)
  1326. {
  1327. /*
  1328. * This driver receives hvcs_remove callbacks for each device upon
  1329. * module removal.
  1330. */
  1331. /*
  1332. * This synchronous operation will wake the khvcsd kthread if it is
  1333. * asleep and will return when khvcsd has terminated.
  1334. */
  1335. kthread_stop(hvcs_task);
  1336. spin_lock(&hvcs_pi_lock);
  1337. kfree(hvcs_pi_buff);
  1338. hvcs_pi_buff = NULL;
  1339. spin_unlock(&hvcs_pi_lock);
  1340. driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
  1341. vio_unregister_driver(&hvcs_vio_driver);
  1342. tty_unregister_driver(hvcs_tty_driver);
  1343. hvcs_free_index_list();
  1344. put_tty_driver(hvcs_tty_driver);
  1345. printk(KERN_INFO "HVCS: driver module removed.\n");
  1346. }
  1347. module_init(hvcs_module_init);
  1348. module_exit(hvcs_module_exit);