vmlogrdr.c 22 KB

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