hvcs.c 47 KB

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