ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / char / sclp_cpi.c
1 /*
2  * Author: Martin Peschke <mpeschke@de.ibm.com>
3  * Copyright (C) 2001 IBM Entwicklung GmbH, IBM Corporation
4  *
5  * SCLP Control-Program Identification.
6  */
7
8 #include <linux/config.h>
9 #include <linux/version.h>
10 #include <linux/kmod.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/string.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <asm/ebcdic.h>
19 #include <asm/semaphore.h>
20
21 #include "sclp.h"
22 #include "sclp_rw.h"
23
24 #define CPI_LENGTH_SYSTEM_TYPE  8
25 #define CPI_LENGTH_SYSTEM_NAME  8
26 #define CPI_LENGTH_SYSPLEX_NAME 8
27
28 struct cpi_evbuf {
29         struct evbuf_header header;
30         u8      id_format;
31         u8      reserved0;
32         u8      system_type[CPI_LENGTH_SYSTEM_TYPE];
33         u64     reserved1;
34         u8      system_name[CPI_LENGTH_SYSTEM_NAME];
35         u64     reserved2;
36         u64     system_level;
37         u64     reserved3;
38         u8      sysplex_name[CPI_LENGTH_SYSPLEX_NAME];
39         u8      reserved4[16];
40 } __attribute__((packed));
41
42 struct cpi_sccb {
43         struct sccb_header header;
44         struct cpi_evbuf cpi_evbuf;
45 } __attribute__((packed));
46
47 /* Event type structure for write message and write priority message */
48 static struct sclp_register sclp_cpi_event =
49 {
50         .send_mask = EvTyp_CtlProgIdent_Mask
51 };
52
53 MODULE_AUTHOR(
54         "Martin Peschke, IBM Deutschland Entwicklung GmbH "
55         "<mpeschke@de.ibm.com>");
56
57 MODULE_DESCRIPTION(
58         "identify this operating system instance to the S/390 "
59         "or zSeries hardware");
60
61 static char *system_name = NULL;
62 module_param(system_name, charp, 0);
63 MODULE_PARM_DESC(system_name, "e.g. hostname - max. 8 characters");
64
65 static char *sysplex_name = NULL;
66 #ifdef ALLOW_SYSPLEX_NAME
67 module_param(sysplex_name, charp, 0);
68 MODULE_PARM_DESC(sysplex_name, "if applicable - max. 8 characters");
69 #endif
70
71 /* use default value for this field (as well as for system level) */
72 static char *system_type = "LINUX";
73
74 static int
75 cpi_check_parms(void)
76 {
77         /* reject if no system type specified */
78         if (!system_type) {
79                 printk("cpi: bug: no system type specified\n");
80                 return -EINVAL;
81         }
82
83         /* reject if system type larger than 8 characters */
84         if (strlen(system_type) > CPI_LENGTH_SYSTEM_NAME) {
85                 printk("cpi: bug: system type has length of %li characters - "
86                        "only %i characters supported\n",
87                        strlen(system_type), CPI_LENGTH_SYSTEM_TYPE);
88                 return -EINVAL;
89         }
90
91         /* reject if no system name specified */
92         if (!system_name) {
93                 printk("cpi: no system name specified\n");
94                 return -EINVAL;
95         }
96
97         /* reject if system name larger than 8 characters */
98         if (strlen(system_name) > CPI_LENGTH_SYSTEM_NAME) {
99                 printk("cpi: system name has length of %li characters - "
100                        "only %i characters supported\n",
101                        strlen(system_name), CPI_LENGTH_SYSTEM_NAME);
102                 return -EINVAL;
103         }
104
105         /* reject if specified sysplex name larger than 8 characters */
106         if (sysplex_name && strlen(sysplex_name) > CPI_LENGTH_SYSPLEX_NAME) {
107                 printk("cpi: sysplex name has length of %li characters"
108                        " - only %i characters supported\n",
109                        strlen(sysplex_name), CPI_LENGTH_SYSPLEX_NAME);
110                 return -EINVAL;
111         }
112         return 0;
113 }
114
115 static void
116 cpi_callback(struct sclp_req *req, void *data)
117 {
118         struct semaphore *sem;
119
120         sem = (struct semaphore *) data;
121         up(sem);
122 }
123
124 static struct sclp_req *
125 cpi_prepare_req(void)
126 {
127         struct sclp_req *req;
128         struct cpi_sccb *sccb;
129         struct cpi_evbuf *evb;
130
131         req = (struct sclp_req *) kmalloc(sizeof(struct sclp_req), GFP_KERNEL);
132         if (req == NULL)
133                 return ERR_PTR(-ENOMEM);
134         sccb = (struct cpi_sccb *) __get_free_page(GFP_KERNEL | GFP_DMA);
135         if (sccb == NULL) {
136                 kfree(req);
137                 return ERR_PTR(-ENOMEM);
138         }
139         memset(sccb, 0, sizeof(struct cpi_sccb));
140
141         /* setup SCCB for Control-Program Identification */
142         sccb->header.length = sizeof(struct cpi_sccb);
143         sccb->cpi_evbuf.header.length = sizeof(struct cpi_evbuf);
144         sccb->cpi_evbuf.header.type = 0x0B;
145         evb = &sccb->cpi_evbuf;
146
147         /* set system type */
148         memset(evb->system_type, ' ', CPI_LENGTH_SYSTEM_TYPE);
149         memcpy(evb->system_type, system_type, strlen(system_type));
150         sclp_ascebc_str(evb->system_type, CPI_LENGTH_SYSTEM_TYPE);
151         EBC_TOUPPER(evb->system_type, CPI_LENGTH_SYSTEM_TYPE);
152
153         /* set system name */
154         memset(evb->system_name, ' ', CPI_LENGTH_SYSTEM_NAME);
155         memcpy(evb->system_name, system_name, strlen(system_name));
156         sclp_ascebc_str(evb->system_name, CPI_LENGTH_SYSTEM_NAME);
157         EBC_TOUPPER(evb->system_name, CPI_LENGTH_SYSTEM_NAME);
158
159         /* set sytem level */
160         evb->system_level = LINUX_VERSION_CODE;
161
162         /* set sysplex name */
163         if (sysplex_name) {
164                 memset(evb->sysplex_name, ' ', CPI_LENGTH_SYSPLEX_NAME);
165                 memcpy(evb->sysplex_name, sysplex_name, strlen(sysplex_name));
166                 sclp_ascebc_str(evb->sysplex_name, CPI_LENGTH_SYSPLEX_NAME);
167                 EBC_TOUPPER(evb->sysplex_name, CPI_LENGTH_SYSPLEX_NAME);
168         }
169
170         /* prepare request data structure presented to SCLP driver */
171         req->command = SCLP_CMDW_WRITEDATA;
172         req->sccb = sccb;
173         req->status = SCLP_REQ_FILLED;
174         req->callback = cpi_callback;
175         return req;
176 }
177
178 static void
179 cpi_free_req(struct sclp_req *req)
180 {
181         free_page((unsigned long) req->sccb);
182         kfree(req);
183 }
184
185 static int __init
186 cpi_module_init(void)
187 {
188         struct semaphore sem;
189         struct sclp_req *req;
190         int rc;
191
192         rc = cpi_check_parms();
193         if (rc)
194                 return rc;
195
196         rc = sclp_register(&sclp_cpi_event);
197         if (rc) {
198                 /* could not register sclp event. Die. */
199                 printk("cpi: could not register to hardware console.\n");
200                 return -EINVAL;
201         }
202         if (!(sclp_cpi_event.sclp_send_mask & EvTyp_CtlProgIdent_Mask)) {
203                 printk("cpi: no control program identification support\n");
204                 sclp_unregister(&sclp_cpi_event);
205                 return -ENOTSUPP;
206         }
207
208         req = cpi_prepare_req();
209         if (IS_ERR(req)) {
210                 printk("cpi: couldn't allocate request\n");
211                 sclp_unregister(&sclp_cpi_event);
212                 return PTR_ERR(req);
213         }
214
215         /* Prepare semaphore */
216         sema_init(&sem, 0);
217         req->callback_data = &sem;
218         /* Add request to sclp queue */
219         sclp_add_request(req);
220         /* make "insmod" sleep until callback arrives */
221         down(&sem);
222
223         rc = ((struct cpi_sccb *) req->sccb)->header.response_code;
224         if (rc != 0x0020) {
225                 printk("cpi: failed with response code 0x%x\n", rc);
226                 rc = -ECOMM;
227         } else
228                 rc = 0;
229
230         cpi_free_req(req);
231         sclp_unregister(&sclp_cpi_event);
232
233         return rc;
234 }
235
236
237 static void __exit cpi_module_exit(void)
238 {
239 }
240
241
242 /* declare driver module init/cleanup functions */
243 module_init(cpi_module_init);
244 module_exit(cpi_module_exit);
245