vmlogrdr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * drivers/s390/char/vmlogrdr.c
  3. * character device driver for reading z/VM system service records
  4. *
  5. *
  6. * Copyright 2004 IBM Corporation
  7. * character device driver for reading z/VM system service records,
  8. * Version 1.0
  9. * Author(s): Xenia Tkatschow <xenia@us.ibm.com>
  10. * Stefan Weinhuber <wein@de.ibm.com>
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/types.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <asm/atomic.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/cpcmd.h>
  22. #include <asm/debug.h>
  23. #include <asm/ebcdic.h>
  24. #include <net/iucv/iucv.h>
  25. #include <linux/kmod.h>
  26. #include <linux/cdev.h>
  27. #include <linux/device.h>
  28. #include <linux/string.h>
  29. MODULE_AUTHOR
  30. ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
  31. " Stefan Weinhuber (wein@de.ibm.com)");
  32. MODULE_DESCRIPTION ("Character device driver for reading z/VM "
  33. "system service records.");
  34. MODULE_LICENSE("GPL");
  35. /*
  36. * The size of the buffer for iucv data transfer is one page,
  37. * but in addition to the data we read from iucv we also
  38. * place an integer and some characters into that buffer,
  39. * so the maximum size for record data is a little less then
  40. * one page.
  41. */
  42. #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
  43. /*
  44. * The elements that are concurrently accessed by bottom halves are
  45. * connection_established, iucv_path_severed, local_interrupt_buffer
  46. * and receive_ready. The first three can be protected by
  47. * priv_lock. receive_ready is atomic, so it can be incremented and
  48. * decremented without holding a lock.
  49. * The variable dev_in_use needs to be protected by the lock, since
  50. * it's a flag used by open to make sure that the device is opened only
  51. * by one user at the same time.
  52. */
  53. struct vmlogrdr_priv_t {
  54. char system_service[8];
  55. char internal_name[8];
  56. char recording_name[8];
  57. struct iucv_path *path;
  58. int connection_established;
  59. int iucv_path_severed;
  60. struct iucv_message local_interrupt_buffer;
  61. atomic_t receive_ready;
  62. int minor_num;
  63. char * buffer;
  64. char * current_position;
  65. int remaining;
  66. ulong residual_length;
  67. int buffer_free;
  68. int dev_in_use; /* 1: already opened, 0: not opened*/
  69. spinlock_t priv_lock;
  70. struct device *device;
  71. struct class_device *class_device;
  72. int autorecording;
  73. int autopurge;
  74. };
  75. /*
  76. * File operation structure for vmlogrdr devices
  77. */
  78. static int vmlogrdr_open(struct inode *, struct file *);
  79. static int vmlogrdr_release(struct inode *, struct file *);
  80. static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
  81. size_t count, loff_t * ppos);
  82. static struct file_operations vmlogrdr_fops = {
  83. .owner = THIS_MODULE,
  84. .open = vmlogrdr_open,
  85. .release = vmlogrdr_release,
  86. .read = vmlogrdr_read,
  87. };
  88. static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
  89. static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
  90. static void vmlogrdr_iucv_message_pending(struct iucv_path *,
  91. struct iucv_message *);
  92. static struct iucv_handler vmlogrdr_iucv_handler = {
  93. .path_complete = vmlogrdr_iucv_path_complete,
  94. .path_severed = vmlogrdr_iucv_path_severed,
  95. .message_pending = vmlogrdr_iucv_message_pending,
  96. };
  97. static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
  98. static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
  99. /*
  100. * pointer to system service private structure
  101. * minor number 0 --> logrec
  102. * minor number 1 --> account
  103. * minor number 2 --> symptom
  104. */
  105. static struct vmlogrdr_priv_t sys_ser[] = {
  106. { .system_service = "*LOGREC ",
  107. .internal_name = "logrec",
  108. .recording_name = "EREP",
  109. .minor_num = 0,
  110. .buffer_free = 1,
  111. .priv_lock = SPIN_LOCK_UNLOCKED,
  112. .autorecording = 1,
  113. .autopurge = 1,
  114. },
  115. { .system_service = "*ACCOUNT",
  116. .internal_name = "account",
  117. .recording_name = "ACCOUNT",
  118. .minor_num = 1,
  119. .buffer_free = 1,
  120. .priv_lock = SPIN_LOCK_UNLOCKED,
  121. .autorecording = 1,
  122. .autopurge = 1,
  123. },
  124. { .system_service = "*SYMPTOM",
  125. .internal_name = "symptom",
  126. .recording_name = "SYMPTOM",
  127. .minor_num = 2,
  128. .buffer_free = 1,
  129. .priv_lock = SPIN_LOCK_UNLOCKED,
  130. .autorecording = 1,
  131. .autopurge = 1,
  132. }
  133. };
  134. #define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
  135. static char FENCE[] = {"EOR"};
  136. static int vmlogrdr_major = 0;
  137. static struct cdev *vmlogrdr_cdev = NULL;
  138. static int recording_class_AB;
  139. static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
  140. {
  141. struct vmlogrdr_priv_t * logptr = path->private;
  142. spin_lock(&logptr->priv_lock);
  143. logptr->connection_established = 1;
  144. spin_unlock(&logptr->priv_lock);
  145. wake_up(&conn_wait_queue);
  146. }
  147. static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
  148. {
  149. struct vmlogrdr_priv_t * logptr = path->private;
  150. u8 reason = (u8) ipuser[8];
  151. printk (KERN_ERR "vmlogrdr: connection severed with"
  152. " reason %i\n", reason);
  153. iucv_path_sever(path, NULL);
  154. kfree(path);
  155. logptr->path = NULL;
  156. spin_lock(&logptr->priv_lock);
  157. logptr->connection_established = 0;
  158. logptr->iucv_path_severed = 1;
  159. spin_unlock(&logptr->priv_lock);
  160. wake_up(&conn_wait_queue);
  161. /* just in case we're sleeping waiting for a record */
  162. wake_up_interruptible(&read_wait_queue);
  163. }
  164. static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
  165. struct iucv_message *msg)
  166. {
  167. struct vmlogrdr_priv_t * logptr = path->private;
  168. /*
  169. * This function is the bottom half so it should be quick.
  170. * Copy the external interrupt data into our local eib and increment
  171. * the usage count
  172. */
  173. spin_lock(&logptr->priv_lock);
  174. memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
  175. atomic_inc(&logptr->receive_ready);
  176. spin_unlock(&logptr->priv_lock);
  177. wake_up_interruptible(&read_wait_queue);
  178. }
  179. static int vmlogrdr_get_recording_class_AB(void)
  180. {
  181. char cp_command[]="QUERY COMMAND RECORDING ";
  182. char cp_response[80];
  183. char *tail;
  184. int len,i;
  185. printk (KERN_DEBUG "vmlogrdr: query command: %s\n", cp_command);
  186. cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
  187. printk (KERN_DEBUG "vmlogrdr: response: %s", cp_response);
  188. len = strnlen(cp_response,sizeof(cp_response));
  189. // now the parsing
  190. tail=strnchr(cp_response,len,'=');
  191. if (!tail)
  192. return 0;
  193. tail++;
  194. if (!strncmp("ANY",tail,3))
  195. return 1;
  196. if (!strncmp("NONE",tail,4))
  197. return 0;
  198. /*
  199. * expect comma separated list of classes here, if one of them
  200. * is A or B return 1 otherwise 0
  201. */
  202. for (i=tail-cp_response; i<len; i++)
  203. if ( cp_response[i]=='A' || cp_response[i]=='B' )
  204. return 1;
  205. return 0;
  206. }
  207. static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
  208. int action, int purge)
  209. {
  210. char cp_command[80];
  211. char cp_response[160];
  212. char *onoff, *qid_string;
  213. memset(cp_command, 0x00, sizeof(cp_command));
  214. memset(cp_response, 0x00, sizeof(cp_response));
  215. onoff = ((action == 1) ? "ON" : "OFF");
  216. qid_string = ((recording_class_AB == 1) ? " QID * " : "");
  217. /*
  218. * The recording commands needs to be called with option QID
  219. * for guests that have previlege classes A or B.
  220. * Purging has to be done as separate step, because recording
  221. * can't be switched on as long as records are on the queue.
  222. * Doing both at the same time doesn't work.
  223. */
  224. if (purge) {
  225. snprintf(cp_command, sizeof(cp_command),
  226. "RECORDING %s PURGE %s",
  227. logptr->recording_name,
  228. qid_string);
  229. printk (KERN_DEBUG "vmlogrdr: recording command: %s\n",
  230. cp_command);
  231. cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
  232. printk (KERN_DEBUG "vmlogrdr: recording response: %s",
  233. cp_response);
  234. }
  235. memset(cp_command, 0x00, sizeof(cp_command));
  236. memset(cp_response, 0x00, sizeof(cp_response));
  237. snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
  238. logptr->recording_name,
  239. onoff,
  240. qid_string);
  241. printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
  242. cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
  243. printk (KERN_DEBUG "vmlogrdr: recording response: %s",
  244. cp_response);
  245. /* The recording command will usually answer with 'Command complete'
  246. * on success, but when the specific service was never connected
  247. * before then there might be an additional informational message
  248. * 'HCPCRC8072I Recording entry not found' before the
  249. * 'Command complete'. So I use strstr rather then the strncmp.
  250. */
  251. if (strstr(cp_response,"Command complete"))
  252. return 0;
  253. else
  254. return -EIO;
  255. }
  256. static int vmlogrdr_open (struct inode *inode, struct file *filp)
  257. {
  258. int dev_num = 0;
  259. struct vmlogrdr_priv_t * logptr = NULL;
  260. int connect_rc = 0;
  261. int ret;
  262. dev_num = iminor(inode);
  263. if (dev_num > MAXMINOR)
  264. return -ENODEV;
  265. logptr = &sys_ser[dev_num];
  266. /*
  267. * only allow for blocking reads to be open
  268. */
  269. if (filp->f_flags & O_NONBLOCK)
  270. return -ENOSYS;
  271. /* Besure this device hasn't already been opened */
  272. spin_lock_bh(&logptr->priv_lock);
  273. if (logptr->dev_in_use) {
  274. spin_unlock_bh(&logptr->priv_lock);
  275. return -EBUSY;
  276. }
  277. logptr->dev_in_use = 1;
  278. logptr->connection_established = 0;
  279. logptr->iucv_path_severed = 0;
  280. atomic_set(&logptr->receive_ready, 0);
  281. logptr->buffer_free = 1;
  282. spin_unlock_bh(&logptr->priv_lock);
  283. /* set the file options */
  284. filp->private_data = logptr;
  285. filp->f_op = &vmlogrdr_fops;
  286. /* start recording for this service*/
  287. if (logptr->autorecording) {
  288. ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
  289. if (ret)
  290. printk (KERN_WARNING "vmlogrdr: failed to start "
  291. "recording automatically\n");
  292. }
  293. /* create connection to the system service */
  294. logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
  295. if (!logptr->path)
  296. goto out_dev;
  297. connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
  298. logptr->system_service, NULL, NULL,
  299. logptr);
  300. if (connect_rc) {
  301. printk (KERN_ERR "vmlogrdr: iucv connection to %s "
  302. "failed with rc %i \n", logptr->system_service,
  303. connect_rc);
  304. goto out_path;
  305. }
  306. /* We've issued the connect and now we must wait for a
  307. * ConnectionComplete or ConnectinSevered Interrupt
  308. * before we can continue to process.
  309. */
  310. wait_event(conn_wait_queue, (logptr->connection_established)
  311. || (logptr->iucv_path_severed));
  312. if (logptr->iucv_path_severed)
  313. goto out_record;
  314. return nonseekable_open(inode, filp);
  315. out_record:
  316. if (logptr->autorecording)
  317. vmlogrdr_recording(logptr,0,logptr->autopurge);
  318. out_path:
  319. kfree(logptr->path); /* kfree(NULL) is ok. */
  320. logptr->path = NULL;
  321. out_dev:
  322. logptr->dev_in_use = 0;
  323. return -EIO;
  324. }
  325. static int vmlogrdr_release (struct inode *inode, struct file *filp)
  326. {
  327. int ret;
  328. struct vmlogrdr_priv_t * logptr = filp->private_data;
  329. if (logptr->autorecording) {
  330. ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
  331. if (ret)
  332. printk (KERN_WARNING "vmlogrdr: failed to stop "
  333. "recording automatically\n");
  334. }
  335. logptr->dev_in_use = 0;
  336. return 0;
  337. }
  338. static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
  339. {
  340. int rc, *temp;
  341. /* we need to keep track of two data sizes here:
  342. * The number of bytes we need to receive from iucv and
  343. * the total number of bytes we actually write into the buffer.
  344. */
  345. int user_data_count, iucv_data_count;
  346. char * buffer;
  347. if (atomic_read(&priv->receive_ready)) {
  348. spin_lock_bh(&priv->priv_lock);
  349. if (priv->residual_length){
  350. /* receive second half of a record */
  351. iucv_data_count = priv->residual_length;
  352. user_data_count = 0;
  353. buffer = priv->buffer;
  354. } else {
  355. /* receive a new record:
  356. * We need to return the total length of the record
  357. * + size of FENCE in the first 4 bytes of the buffer.
  358. */
  359. iucv_data_count = priv->local_interrupt_buffer.length;
  360. user_data_count = sizeof(int);
  361. temp = (int*)priv->buffer;
  362. *temp= iucv_data_count + sizeof(FENCE);
  363. buffer = priv->buffer + sizeof(int);
  364. }
  365. /*
  366. * If the record is bigger then our buffer, we receive only
  367. * a part of it. We can get the rest later.
  368. */
  369. if (iucv_data_count > NET_BUFFER_SIZE)
  370. iucv_data_count = NET_BUFFER_SIZE;
  371. rc = iucv_message_receive(priv->path,
  372. &priv->local_interrupt_buffer,
  373. 0, buffer, iucv_data_count,
  374. &priv->residual_length);
  375. spin_unlock_bh(&priv->priv_lock);
  376. /* An rc of 5 indicates that the record was bigger then
  377. * the buffer, which is OK for us. A 9 indicates that the
  378. * record was purged befor we could receive it.
  379. */
  380. if (rc == 5)
  381. rc = 0;
  382. if (rc == 9)
  383. atomic_set(&priv->receive_ready, 0);
  384. } else {
  385. rc = 1;
  386. }
  387. if (!rc) {
  388. priv->buffer_free = 0;
  389. user_data_count += iucv_data_count;
  390. priv->current_position = priv->buffer;
  391. if (priv->residual_length == 0){
  392. /* the whole record has been captured,
  393. * now add the fence */
  394. atomic_dec(&priv->receive_ready);
  395. buffer = priv->buffer + user_data_count;
  396. memcpy(buffer, FENCE, sizeof(FENCE));
  397. user_data_count += sizeof(FENCE);
  398. }
  399. priv->remaining = user_data_count;
  400. }
  401. return rc;
  402. }
  403. static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
  404. size_t count, loff_t * ppos)
  405. {
  406. int rc;
  407. struct vmlogrdr_priv_t * priv = filp->private_data;
  408. while (priv->buffer_free) {
  409. rc = vmlogrdr_receive_data(priv);
  410. if (rc) {
  411. rc = wait_event_interruptible(read_wait_queue,
  412. atomic_read(&priv->receive_ready));
  413. if (rc)
  414. return rc;
  415. }
  416. }
  417. /* copy only up to end of record */
  418. if (count > priv->remaining)
  419. count = priv->remaining;
  420. if (copy_to_user(data, priv->current_position, count))
  421. return -EFAULT;
  422. *ppos += count;
  423. priv->current_position += count;
  424. priv->remaining -= count;
  425. /* if all data has been transferred, set buffer free */
  426. if (priv->remaining == 0)
  427. priv->buffer_free = 1;
  428. return count;
  429. }
  430. static ssize_t vmlogrdr_autopurge_store(struct device * dev,
  431. struct device_attribute *attr,
  432. const char * buf, size_t count)
  433. {
  434. struct vmlogrdr_priv_t *priv = dev->driver_data;
  435. ssize_t ret = count;
  436. switch (buf[0]) {
  437. case '0':
  438. priv->autopurge=0;
  439. break;
  440. case '1':
  441. priv->autopurge=1;
  442. break;
  443. default:
  444. ret = -EINVAL;
  445. }
  446. return ret;
  447. }
  448. static ssize_t vmlogrdr_autopurge_show(struct device *dev,
  449. struct device_attribute *attr,
  450. char *buf)
  451. {
  452. struct vmlogrdr_priv_t *priv = dev->driver_data;
  453. return sprintf(buf, "%u\n", priv->autopurge);
  454. }
  455. static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
  456. vmlogrdr_autopurge_store);
  457. static ssize_t vmlogrdr_purge_store(struct device * dev,
  458. struct device_attribute *attr,
  459. const char * buf, size_t count)
  460. {
  461. char cp_command[80];
  462. char cp_response[80];
  463. struct vmlogrdr_priv_t *priv = dev->driver_data;
  464. if (buf[0] != '1')
  465. return -EINVAL;
  466. memset(cp_command, 0x00, sizeof(cp_command));
  467. memset(cp_response, 0x00, sizeof(cp_response));
  468. /*
  469. * The recording command needs to be called with option QID
  470. * for guests that have previlege classes A or B.
  471. * Other guests will not recognize the command and we have to
  472. * issue the same command without the QID parameter.
  473. */
  474. if (recording_class_AB)
  475. snprintf(cp_command, sizeof(cp_command),
  476. "RECORDING %s PURGE QID * ",
  477. priv->recording_name);
  478. else
  479. snprintf(cp_command, sizeof(cp_command),
  480. "RECORDING %s PURGE ",
  481. priv->recording_name);
  482. printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
  483. cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
  484. printk (KERN_DEBUG "vmlogrdr: recording response: %s",
  485. cp_response);
  486. return count;
  487. }
  488. static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
  489. static ssize_t vmlogrdr_autorecording_store(struct device *dev,
  490. struct device_attribute *attr,
  491. const char *buf, size_t count)
  492. {
  493. struct vmlogrdr_priv_t *priv = dev->driver_data;
  494. ssize_t ret = count;
  495. switch (buf[0]) {
  496. case '0':
  497. priv->autorecording=0;
  498. break;
  499. case '1':
  500. priv->autorecording=1;
  501. break;
  502. default:
  503. ret = -EINVAL;
  504. }
  505. return ret;
  506. }
  507. static ssize_t vmlogrdr_autorecording_show(struct device *dev,
  508. struct device_attribute *attr,
  509. char *buf)
  510. {
  511. struct vmlogrdr_priv_t *priv = dev->driver_data;
  512. return sprintf(buf, "%u\n", priv->autorecording);
  513. }
  514. static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
  515. vmlogrdr_autorecording_store);
  516. static ssize_t vmlogrdr_recording_store(struct device * dev,
  517. struct device_attribute *attr,
  518. const char * buf, size_t count)
  519. {
  520. struct vmlogrdr_priv_t *priv = dev->driver_data;
  521. ssize_t ret;
  522. switch (buf[0]) {
  523. case '0':
  524. ret = vmlogrdr_recording(priv,0,0);
  525. break;
  526. case '1':
  527. ret = vmlogrdr_recording(priv,1,0);
  528. break;
  529. default:
  530. ret = -EINVAL;
  531. }
  532. if (ret)
  533. return ret;
  534. else
  535. return count;
  536. }
  537. static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
  538. static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
  539. char *buf)
  540. {
  541. char cp_command[] = "QUERY RECORDING ";
  542. int len;
  543. cpcmd(cp_command, buf, 4096, NULL);
  544. len = strlen(buf);
  545. return len;
  546. }
  547. static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
  548. NULL);
  549. static struct attribute *vmlogrdr_attrs[] = {
  550. &dev_attr_autopurge.attr,
  551. &dev_attr_purge.attr,
  552. &dev_attr_autorecording.attr,
  553. &dev_attr_recording.attr,
  554. NULL,
  555. };
  556. static struct attribute_group vmlogrdr_attr_group = {
  557. .attrs = vmlogrdr_attrs,
  558. };
  559. static struct class *vmlogrdr_class;
  560. static struct device_driver vmlogrdr_driver = {
  561. .name = "vmlogrdr",
  562. .bus = &iucv_bus,
  563. };
  564. static int vmlogrdr_register_driver(void)
  565. {
  566. int ret;
  567. /* Register with iucv driver */
  568. ret = iucv_register(&vmlogrdr_iucv_handler, 1);
  569. if (ret) {
  570. printk (KERN_ERR "vmlogrdr: failed to register with"
  571. "iucv driver\n");
  572. goto out;
  573. }
  574. ret = driver_register(&vmlogrdr_driver);
  575. if (ret) {
  576. printk(KERN_ERR "vmlogrdr: failed to register driver.\n");
  577. goto out_iucv;
  578. }
  579. ret = driver_create_file(&vmlogrdr_driver,
  580. &driver_attr_recording_status);
  581. if (ret) {
  582. printk(KERN_ERR "vmlogrdr: failed to add driver attribute.\n");
  583. goto out_driver;
  584. }
  585. vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
  586. if (IS_ERR(vmlogrdr_class)) {
  587. printk(KERN_ERR "vmlogrdr: failed to create class.\n");
  588. ret = PTR_ERR(vmlogrdr_class);
  589. vmlogrdr_class = NULL;
  590. goto out_attr;
  591. }
  592. return 0;
  593. out_attr:
  594. driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
  595. out_driver:
  596. driver_unregister(&vmlogrdr_driver);
  597. out_iucv:
  598. iucv_unregister(&vmlogrdr_iucv_handler, 1);
  599. out:
  600. return ret;
  601. }
  602. static void vmlogrdr_unregister_driver(void)
  603. {
  604. class_destroy(vmlogrdr_class);
  605. vmlogrdr_class = NULL;
  606. driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
  607. driver_unregister(&vmlogrdr_driver);
  608. iucv_unregister(&vmlogrdr_iucv_handler, 1);
  609. }
  610. static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
  611. {
  612. struct device *dev;
  613. int ret;
  614. dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  615. if (dev) {
  616. snprintf(dev->bus_id, BUS_ID_SIZE, "%s",
  617. priv->internal_name);
  618. dev->bus = &iucv_bus;
  619. dev->parent = iucv_root;
  620. dev->driver = &vmlogrdr_driver;
  621. /*
  622. * The release function could be called after the
  623. * module has been unloaded. It's _only_ task is to
  624. * free the struct. Therefore, we specify kfree()
  625. * directly here. (Probably a little bit obfuscating
  626. * but legitime ...).
  627. */
  628. dev->release = (void (*)(struct device *))kfree;
  629. } else
  630. return -ENOMEM;
  631. ret = device_register(dev);
  632. if (ret)
  633. return ret;
  634. ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
  635. if (ret) {
  636. device_unregister(dev);
  637. return ret;
  638. }
  639. priv->class_device = class_device_create(
  640. vmlogrdr_class,
  641. NULL,
  642. MKDEV(vmlogrdr_major, priv->minor_num),
  643. dev,
  644. "%s", dev->bus_id );
  645. if (IS_ERR(priv->class_device)) {
  646. ret = PTR_ERR(priv->class_device);
  647. priv->class_device=NULL;
  648. sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
  649. device_unregister(dev);
  650. return ret;
  651. }
  652. dev->driver_data = priv;
  653. priv->device = dev;
  654. return 0;
  655. }
  656. static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
  657. {
  658. class_device_destroy(vmlogrdr_class,
  659. MKDEV(vmlogrdr_major, priv->minor_num));
  660. if (priv->device != NULL) {
  661. sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
  662. device_unregister(priv->device);
  663. priv->device=NULL;
  664. }
  665. return 0;
  666. }
  667. static int vmlogrdr_register_cdev(dev_t dev)
  668. {
  669. int rc = 0;
  670. vmlogrdr_cdev = cdev_alloc();
  671. if (!vmlogrdr_cdev) {
  672. return -ENOMEM;
  673. }
  674. vmlogrdr_cdev->owner = THIS_MODULE;
  675. vmlogrdr_cdev->ops = &vmlogrdr_fops;
  676. vmlogrdr_cdev->dev = dev;
  677. rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
  678. if (!rc)
  679. return 0;
  680. // cleanup: cdev is not fully registered, no cdev_del here!
  681. kobject_put(&vmlogrdr_cdev->kobj);
  682. vmlogrdr_cdev=NULL;
  683. return rc;
  684. }
  685. static void vmlogrdr_cleanup(void)
  686. {
  687. int i;
  688. if (vmlogrdr_cdev) {
  689. cdev_del(vmlogrdr_cdev);
  690. vmlogrdr_cdev=NULL;
  691. }
  692. for (i=0; i < MAXMINOR; ++i ) {
  693. vmlogrdr_unregister_device(&sys_ser[i]);
  694. free_page((unsigned long)sys_ser[i].buffer);
  695. }
  696. vmlogrdr_unregister_driver();
  697. if (vmlogrdr_major) {
  698. unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
  699. vmlogrdr_major=0;
  700. }
  701. }
  702. static int vmlogrdr_init(void)
  703. {
  704. int rc;
  705. int i;
  706. dev_t dev;
  707. if (! MACHINE_IS_VM) {
  708. printk (KERN_ERR "vmlogrdr: not running under VM, "
  709. "driver not loaded.\n");
  710. return -ENODEV;
  711. }
  712. recording_class_AB = vmlogrdr_get_recording_class_AB();
  713. rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
  714. if (rc)
  715. return rc;
  716. vmlogrdr_major = MAJOR(dev);
  717. rc=vmlogrdr_register_driver();
  718. if (rc)
  719. goto cleanup;
  720. for (i=0; i < MAXMINOR; ++i ) {
  721. sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
  722. if (!sys_ser[i].buffer) {
  723. rc = ENOMEM;
  724. break;
  725. }
  726. sys_ser[i].current_position = sys_ser[i].buffer;
  727. rc=vmlogrdr_register_device(&sys_ser[i]);
  728. if (rc)
  729. break;
  730. }
  731. if (rc)
  732. goto cleanup;
  733. rc = vmlogrdr_register_cdev(dev);
  734. if (rc)
  735. goto cleanup;
  736. printk (KERN_INFO "vmlogrdr: driver loaded\n");
  737. return 0;
  738. cleanup:
  739. vmlogrdr_cleanup();
  740. printk (KERN_ERR "vmlogrdr: driver not loaded.\n");
  741. return rc;
  742. }
  743. static void vmlogrdr_exit(void)
  744. {
  745. vmlogrdr_cleanup();
  746. printk (KERN_INFO "vmlogrdr: driver unloaded\n");
  747. return;
  748. }
  749. module_init(vmlogrdr_init);
  750. module_exit(vmlogrdr_exit);