hvcs.c 47 KB

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