hvcs.c 46 KB

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