vmlogrdr.c 22 KB

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