blob: 29e48b2ab7121c089846b206de7cae3bfc2cd160 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * File...........: linux/drivers/s390/block/dasd_devmap.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Horst Hummel <Horst.Hummel@de.ibm.com>
5 * Carsten Otte <Cotte@de.ibm.com>
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Bugreports.to..: <Linux390@de.ibm.com>
8 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9 *
10 * Device mapping and dasd= parameter parsing functions. All devmap
11 * functions may not be called from interrupt context. In particular
12 * dasd_get_device is a no-no from interrupt context.
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ctype.h>
17#include <linux/init.h>
Rusty Russell8d3b33f2006-03-25 03:07:05 -080018#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/debug.h>
21#include <asm/uaccess.h>
22
23/* This is ugly... */
24#define PRINTK_HEADER "dasd_devmap:"
25
26#include "dasd_int.h"
27
28kmem_cache_t *dasd_page_cache;
Horst Hummel40545572006-06-29 15:08:18 +020029EXPORT_SYMBOL_GPL(dasd_page_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * dasd_devmap_t is used to store the features and the relation
33 * between device number and device index. To find a dasd_devmap_t
34 * that corresponds to a device number of a device index each
35 * dasd_devmap_t is added to two linked lists, one to search by
36 * the device number and one to search by the device index. As
37 * soon as big minor numbers are available the device index list
38 * can be removed since the device number will then be identical
39 * to the device index.
40 */
41struct dasd_devmap {
42 struct list_head list;
43 char bus_id[BUS_ID_SIZE];
44 unsigned int devindex;
45 unsigned short features;
46 struct dasd_device *device;
Horst Hummel3d052592006-04-27 18:40:28 -070047 struct dasd_uid uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50/*
Horst Hummel40545572006-06-29 15:08:18 +020051 * dasd_servermap is used to store the server_id of all storage servers
52 * accessed by DASD device driver.
53 */
54struct dasd_servermap {
55 struct list_head list;
56 struct server_id {
57 char vendor[4];
58 char serial[15];
59 } sid;
60};
61
62static struct list_head dasd_serverlist;
63
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * Parameter parsing functions for dasd= parameter. The syntax is:
66 * <devno> : (0x)?[0-9a-fA-F]+
67 * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
68 * <feature> : ro
69 * <feature_list> : \(<feature>(:<feature>)*\)
70 * <devno-range> : <devno>(-<devno>)?<feature_list>?
71 * <busid-range> : <busid>(-<busid>)?<feature_list>?
72 * <devices> : <devno-range>|<busid-range>
73 * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
74 *
75 * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
76 */
77
78int dasd_probeonly = 0; /* is true, when probeonly mode is active */
79int dasd_autodetect = 0; /* is true, when autodetection is active */
Horst Hummel40545572006-06-29 15:08:18 +020080int dasd_nopav = 0; /* is true, when PAV is disabled */
81EXPORT_SYMBOL_GPL(dasd_nopav);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
85 * it is named 'dasd' to directly be filled by insmod with the comma separated
86 * strings when running as a module.
87 */
88static char *dasd[256];
Rusty Russell8d3b33f2006-03-25 03:07:05 -080089module_param_array(dasd, charp, NULL, 0);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
Horst Hummeld0710c72006-08-10 15:45:16 +020092 * Single spinlock to protect devmap and servermap structures and lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94static DEFINE_SPINLOCK(dasd_devmap_lock);
95
96/*
97 * Hash lists for devmap structures.
98 */
99static struct list_head dasd_hashlists[256];
100int dasd_max_devindex;
101
102static struct dasd_devmap *dasd_add_busid(char *, int);
103
104static inline int
105dasd_hash_busid(char *bus_id)
106{
107 int hash, i;
108
109 hash = 0;
110 for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
111 hash += *bus_id;
112 return hash & 0xff;
113}
114
115#ifndef MODULE
116/*
117 * The parameter parsing functions for builtin-drivers are called
118 * before kmalloc works. Store the pointers to the parameters strings
119 * into dasd[] for later processing.
120 */
121static int __init
122dasd_call_setup(char *str)
123{
124 static int count = 0;
125
126 if (count < 256)
127 dasd[count++] = str;
128 return 1;
129}
130
131__setup ("dasd=", dasd_call_setup);
132#endif /* #ifndef MODULE */
133
134/*
135 * Read a device busid/devno from a string.
136 */
137static inline int
138dasd_busid(char **str, int *id0, int *id1, int *devno)
139{
140 int val, old_style;
Horst Hummel138c0142006-06-29 14:58:12 +0200141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* check for leading '0x' */
143 old_style = 0;
144 if ((*str)[0] == '0' && (*str)[1] == 'x') {
145 *str += 2;
146 old_style = 1;
147 }
148 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
149 return -EINVAL;
150 val = simple_strtoul(*str, str, 16);
151 if (old_style || (*str)[0] != '.') {
152 *id0 = *id1 = 0;
153 if (val < 0 || val > 0xffff)
154 return -EINVAL;
155 *devno = val;
156 return 0;
157 }
158 /* New style x.y.z busid */
159 if (val < 0 || val > 0xff)
160 return -EINVAL;
161 *id0 = val;
162 (*str)++;
163 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
164 return -EINVAL;
165 val = simple_strtoul(*str, str, 16);
166 if (val < 0 || val > 0xff || (*str)++[0] != '.')
167 return -EINVAL;
168 *id1 = val;
169 if (!isxdigit((*str)[0])) /* We require at least one hex digit */
170 return -EINVAL;
171 val = simple_strtoul(*str, str, 16);
172 if (val < 0 || val > 0xffff)
173 return -EINVAL;
174 *devno = val;
175 return 0;
176}
177
178/*
179 * Read colon separated list of dasd features. Currently there is
180 * only one: "ro" for read-only devices. The default feature set
181 * is empty (value 0).
182 */
183static inline int
184dasd_feature_list(char *str, char **endp)
185{
186 int features, len, rc;
187
188 rc = 0;
189 if (*str != '(') {
190 *endp = str;
191 return DASD_FEATURE_DEFAULT;
192 }
193 str++;
194 features = 0;
195
196 while (1) {
Horst Hummel138c0142006-06-29 14:58:12 +0200197 for (len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 str[len] && str[len] != ':' && str[len] != ')'; len++);
199 if (len == 2 && !strncmp(str, "ro", 2))
200 features |= DASD_FEATURE_READONLY;
201 else if (len == 4 && !strncmp(str, "diag", 4))
202 features |= DASD_FEATURE_USEDIAG;
203 else {
204 MESSAGE(KERN_WARNING,
205 "unsupported feature: %*s, "
206 "ignoring setting", len, str);
207 rc = -EINVAL;
208 }
209 str += len;
210 if (*str != ':')
211 break;
212 str++;
213 }
214 if (*str != ')') {
215 MESSAGE(KERN_WARNING, "%s",
216 "missing ')' in dasd parameter string\n");
217 rc = -EINVAL;
218 } else
219 str++;
220 *endp = str;
221 if (rc != 0)
222 return rc;
223 return features;
224}
225
226/*
227 * Try to match the first element on the comma separated parse string
228 * with one of the known keywords. If a keyword is found, take the approprate
229 * action and return a pointer to the residual string. If the first element
230 * could not be matched to any keyword then return an error code.
231 */
232static char *
233dasd_parse_keyword( char *parsestring ) {
234
235 char *nextcomma, *residual_str;
236 int length;
237
238 nextcomma = strchr(parsestring,',');
239 if (nextcomma) {
240 length = nextcomma - parsestring;
241 residual_str = nextcomma + 1;
242 } else {
243 length = strlen(parsestring);
244 residual_str = parsestring + length;
245 }
Horst Hummel40545572006-06-29 15:08:18 +0200246 if (strncmp("autodetect", parsestring, length) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dasd_autodetect = 1;
248 MESSAGE (KERN_INFO, "%s",
249 "turning to autodetection mode");
250 return residual_str;
251 }
Horst Hummel40545572006-06-29 15:08:18 +0200252 if (strncmp("probeonly", parsestring, length) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 dasd_probeonly = 1;
254 MESSAGE(KERN_INFO, "%s",
255 "turning to probeonly mode");
256 return residual_str;
257 }
Horst Hummel40545572006-06-29 15:08:18 +0200258 if (strncmp("nopav", parsestring, length) == 0) {
259 dasd_nopav = 1;
260 MESSAGE(KERN_INFO, "%s", "disable PAV mode");
261 return residual_str;
262 }
263 if (strncmp("fixedbuffers", parsestring, length) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (dasd_page_cache)
265 return residual_str;
266 dasd_page_cache =
Heiko Carstens2f6c55f2006-08-16 13:49:27 +0200267 kmem_cache_create("dasd_page_cache", PAGE_SIZE,
268 PAGE_SIZE, SLAB_CACHE_DMA,
269 NULL, NULL );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!dasd_page_cache)
271 MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
272 "fixed buffer mode disabled.");
273 else
274 MESSAGE (KERN_INFO, "%s",
275 "turning on fixed buffer mode");
276 return residual_str;
277 }
278 return ERR_PTR(-EINVAL);
279}
280
281/*
282 * Try to interprete the first element on the comma separated parse string
283 * as a device number or a range of devices. If the interpretation is
284 * successfull, create the matching dasd_devmap entries and return a pointer
285 * to the residual string.
286 * If interpretation fails or in case of an error, return an error code.
287 */
288static char *
289dasd_parse_range( char *parsestring ) {
290
291 struct dasd_devmap *devmap;
292 int from, from_id0, from_id1;
293 int to, to_id0, to_id1;
294 int features, rc;
295 char bus_id[BUS_ID_SIZE+1], *str;
296
297 str = parsestring;
298 rc = dasd_busid(&str, &from_id0, &from_id1, &from);
299 if (rc == 0) {
300 to = from;
301 to_id0 = from_id0;
302 to_id1 = from_id1;
303 if (*str == '-') {
304 str++;
305 rc = dasd_busid(&str, &to_id0, &to_id1, &to);
306 }
307 }
308 if (rc == 0 &&
309 (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
310 rc = -EINVAL;
311 if (rc) {
312 MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
313 return ERR_PTR(rc);
314 }
315 features = dasd_feature_list(str, &str);
316 if (features < 0)
317 return ERR_PTR(-EINVAL);
Horst Hummel40545572006-06-29 15:08:18 +0200318 /* each device in dasd= parameter should be set initially online */
319 features |= DASD_FEATURE_INITIAL_ONLINE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 while (from <= to) {
321 sprintf(bus_id, "%01x.%01x.%04x",
322 from_id0, from_id1, from++);
323 devmap = dasd_add_busid(bus_id, features);
324 if (IS_ERR(devmap))
325 return (char *)devmap;
326 }
327 if (*str == ',')
328 return str + 1;
329 if (*str == '\0')
330 return str;
331 MESSAGE(KERN_WARNING,
332 "junk at end of dasd parameter string: %s\n", str);
333 return ERR_PTR(-EINVAL);
334}
335
336static inline char *
337dasd_parse_next_element( char *parsestring ) {
338 char * residual_str;
339 residual_str = dasd_parse_keyword(parsestring);
340 if (!IS_ERR(residual_str))
341 return residual_str;
342 residual_str = dasd_parse_range(parsestring);
343 return residual_str;
344}
345
346/*
347 * Parse parameters stored in dasd[]
348 * The 'dasd=...' parameter allows to specify a comma separated list of
349 * keywords and device ranges. When the dasd driver is build into the kernel,
350 * the complete list will be stored as one element of the dasd[] array.
351 * When the dasd driver is build as a module, then the list is broken into
352 * it's elements and each dasd[] entry contains one element.
353 */
354int
355dasd_parse(void)
356{
357 int rc, i;
358 char *parsestring;
359
360 rc = 0;
361 for (i = 0; i < 256; i++) {
362 if (dasd[i] == NULL)
363 break;
364 parsestring = dasd[i];
365 /* loop over the comma separated list in the parsestring */
366 while (*parsestring) {
367 parsestring = dasd_parse_next_element(parsestring);
368 if(IS_ERR(parsestring)) {
369 rc = PTR_ERR(parsestring);
370 break;
371 }
372 }
373 if (rc) {
374 DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
375 break;
376 }
377 }
378 return rc;
379}
380
381/*
382 * Add a devmap for the device specified by busid. It is possible that
383 * the devmap already exists (dasd= parameter). The order of the devices
384 * added through this function will define the kdevs for the individual
Horst Hummel138c0142006-06-29 14:58:12 +0200385 * devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387static struct dasd_devmap *
388dasd_add_busid(char *bus_id, int features)
389{
390 struct dasd_devmap *devmap, *new, *tmp;
391 int hash;
392
393 new = (struct dasd_devmap *)
Horst Hummel138c0142006-06-29 14:58:12 +0200394 kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!new)
396 return ERR_PTR(-ENOMEM);
397 spin_lock(&dasd_devmap_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200398 devmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 hash = dasd_hash_busid(bus_id);
400 list_for_each_entry(tmp, &dasd_hashlists[hash], list)
401 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
402 devmap = tmp;
403 break;
404 }
405 if (!devmap) {
406 /* This bus_id is new. */
407 new->devindex = dasd_max_devindex++;
408 strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
409 new->features = features;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200410 new->device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 list_add(&new->list, &dasd_hashlists[hash]);
412 devmap = new;
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200413 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 spin_unlock(&dasd_devmap_lock);
Jesper Juhl17fd6822005-11-07 01:01:30 -0800416 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return devmap;
418}
419
420/*
421 * Find devmap for device with given bus_id.
422 */
423static struct dasd_devmap *
424dasd_find_busid(char *bus_id)
425{
426 struct dasd_devmap *devmap, *tmp;
427 int hash;
428
429 spin_lock(&dasd_devmap_lock);
430 devmap = ERR_PTR(-ENODEV);
431 hash = dasd_hash_busid(bus_id);
432 list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
433 if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
434 devmap = tmp;
435 break;
436 }
437 }
438 spin_unlock(&dasd_devmap_lock);
439 return devmap;
440}
441
442/*
443 * Check if busid has been added to the list of dasd ranges.
444 */
445int
446dasd_busid_known(char *bus_id)
447{
448 return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
449}
450
451/*
452 * Forget all about the device numbers added so far.
453 * This may only be called at module unload or system shutdown.
454 */
455static void
456dasd_forget_ranges(void)
457{
458 struct dasd_devmap *devmap, *n;
459 int i;
460
461 spin_lock(&dasd_devmap_lock);
462 for (i = 0; i < 256; i++) {
463 list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
Eric Sesterhenn606f4422006-03-26 18:33:07 +0200464 BUG_ON(devmap->device != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 list_del(&devmap->list);
466 kfree(devmap);
467 }
468 }
469 spin_unlock(&dasd_devmap_lock);
470}
471
472/*
473 * Find the device struct by its device index.
474 */
475struct dasd_device *
476dasd_device_from_devindex(int devindex)
477{
478 struct dasd_devmap *devmap, *tmp;
479 struct dasd_device *device;
480 int i;
481
482 spin_lock(&dasd_devmap_lock);
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200483 devmap = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 for (i = 0; (i < 256) && !devmap; i++)
485 list_for_each_entry(tmp, &dasd_hashlists[i], list)
486 if (tmp->devindex == devindex) {
487 /* Found the devmap for the device. */
488 devmap = tmp;
489 break;
490 }
491 if (devmap && devmap->device) {
492 device = devmap->device;
493 dasd_get_device(device);
494 } else
495 device = ERR_PTR(-ENODEV);
496 spin_unlock(&dasd_devmap_lock);
497 return device;
498}
499
500/*
501 * Return devmap for cdev. If no devmap exists yet, create one and
502 * connect it to the cdev.
503 */
504static struct dasd_devmap *
505dasd_devmap_from_cdev(struct ccw_device *cdev)
506{
507 struct dasd_devmap *devmap;
508
509 devmap = dasd_find_busid(cdev->dev.bus_id);
510 if (IS_ERR(devmap))
511 devmap = dasd_add_busid(cdev->dev.bus_id,
512 DASD_FEATURE_DEFAULT);
513 return devmap;
514}
515
516/*
517 * Create a dasd device structure for cdev.
518 */
519struct dasd_device *
520dasd_create_device(struct ccw_device *cdev)
521{
522 struct dasd_devmap *devmap;
523 struct dasd_device *device;
524 int rc;
525
526 devmap = dasd_devmap_from_cdev(cdev);
527 if (IS_ERR(devmap))
528 return (void *) devmap;
529 cdev->dev.driver_data = devmap;
530
531 device = dasd_alloc_device();
532 if (IS_ERR(device))
533 return device;
534 atomic_set(&device->ref_count, 2);
535
536 spin_lock(&dasd_devmap_lock);
537 if (!devmap->device) {
538 devmap->device = device;
539 device->devindex = devmap->devindex;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700540 device->features = devmap->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 get_device(&cdev->dev);
542 device->cdev = cdev;
543 rc = 0;
544 } else
545 /* Someone else was faster. */
546 rc = -EBUSY;
547 spin_unlock(&dasd_devmap_lock);
548
549 if (rc) {
550 dasd_free_device(device);
551 return ERR_PTR(rc);
552 }
553 return device;
554}
555
556/*
557 * Wait queue for dasd_delete_device waits.
558 */
559static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
560
561/*
562 * Remove a dasd device structure. The passed referenced
563 * is destroyed.
564 */
565void
566dasd_delete_device(struct dasd_device *device)
567{
568 struct ccw_device *cdev;
569 struct dasd_devmap *devmap;
570
571 /* First remove device pointer from devmap. */
572 devmap = dasd_find_busid(device->cdev->dev.bus_id);
Eric Sesterhenn606f4422006-03-26 18:33:07 +0200573 BUG_ON(IS_ERR(devmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_lock(&dasd_devmap_lock);
575 if (devmap->device != device) {
576 spin_unlock(&dasd_devmap_lock);
577 dasd_put_device(device);
578 return;
579 }
580 devmap->device = NULL;
581 spin_unlock(&dasd_devmap_lock);
582
583 /* Drop ref_count by 2, one for the devmap reference and
584 * one for the passed reference. */
585 atomic_sub(2, &device->ref_count);
586
587 /* Wait for reference counter to drop to zero. */
588 wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
589
590 /* Disconnect dasd_device structure from ccw_device structure. */
591 cdev = device->cdev;
592 device->cdev = NULL;
593
594 /* Disconnect dasd_devmap structure from ccw_device structure. */
595 cdev->dev.driver_data = NULL;
596
597 /* Put ccw_device structure. */
598 put_device(&cdev->dev);
599
600 /* Now the device structure can be freed. */
601 dasd_free_device(device);
602}
603
604/*
605 * Reference counter dropped to zero. Wake up waiter
606 * in dasd_delete_device.
607 */
608void
609dasd_put_device_wake(struct dasd_device *device)
610{
611 wake_up(&dasd_delete_wq);
612}
613
614/*
615 * Return dasd_device structure associated with cdev.
616 */
617struct dasd_device *
618dasd_device_from_cdev(struct ccw_device *cdev)
619{
620 struct dasd_devmap *devmap;
621 struct dasd_device *device;
622
623 device = ERR_PTR(-ENODEV);
624 spin_lock(&dasd_devmap_lock);
625 devmap = cdev->dev.driver_data;
626 if (devmap && devmap->device) {
627 device = devmap->device;
628 dasd_get_device(device);
629 }
630 spin_unlock(&dasd_devmap_lock);
631 return device;
632}
633
634/*
635 * SECTION: files in sysfs
636 */
637
638/*
639 * readonly controls the readonly status of a dasd
640 */
641static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400642dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct dasd_devmap *devmap;
645 int ro_flag;
646
647 devmap = dasd_find_busid(dev->bus_id);
648 if (!IS_ERR(devmap))
649 ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
650 else
651 ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
652 return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
653}
654
655static ssize_t
Horst Hummel138c0142006-06-29 14:58:12 +0200656dasd_ro_store(struct device *dev, struct device_attribute *attr,
657 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct dasd_devmap *devmap;
660 int ro_flag;
661
662 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
663 if (IS_ERR(devmap))
664 return PTR_ERR(devmap);
665 ro_flag = buf[0] == '1';
666 spin_lock(&dasd_devmap_lock);
667 if (ro_flag)
668 devmap->features |= DASD_FEATURE_READONLY;
669 else
670 devmap->features &= ~DASD_FEATURE_READONLY;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700671 if (devmap->device)
672 devmap->device->features = devmap->features;
Horst Hummelf24acd42005-05-01 08:58:59 -0700673 if (devmap->device && devmap->device->gdp)
674 set_disk_ro(devmap->device->gdp, ro_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 spin_unlock(&dasd_devmap_lock);
676 return count;
677}
678
679static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
680
681/*
682 * use_diag controls whether the driver should use diag rather than ssch
683 * to talk to the device
684 */
Horst Hummel138c0142006-06-29 14:58:12 +0200685static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400686dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct dasd_devmap *devmap;
689 int use_diag;
690
691 devmap = dasd_find_busid(dev->bus_id);
692 if (!IS_ERR(devmap))
693 use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
694 else
695 use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
696 return sprintf(buf, use_diag ? "1\n" : "0\n");
697}
698
699static ssize_t
Horst Hummel138c0142006-06-29 14:58:12 +0200700dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
701 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct dasd_devmap *devmap;
704 ssize_t rc;
705 int use_diag;
706
707 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
708 if (IS_ERR(devmap))
709 return PTR_ERR(devmap);
710 use_diag = buf[0] == '1';
711 spin_lock(&dasd_devmap_lock);
712 /* Changing diag discipline flag is only allowed in offline state. */
713 rc = count;
714 if (!devmap->device) {
715 if (use_diag)
716 devmap->features |= DASD_FEATURE_USEDIAG;
717 else
718 devmap->features &= ~DASD_FEATURE_USEDIAG;
719 } else
720 rc = -EPERM;
721 spin_unlock(&dasd_devmap_lock);
722 return rc;
723}
724
Horst Hummel138c0142006-06-29 14:58:12 +0200725static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727static ssize_t
Horst Hummel138c0142006-06-29 14:58:12 +0200728dasd_discipline_show(struct device *dev, struct device_attribute *attr,
729 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct dasd_devmap *devmap;
732 char *dname;
733
734 spin_lock(&dasd_devmap_lock);
735 dname = "none";
736 devmap = dev->driver_data;
737 if (devmap && devmap->device && devmap->device->discipline)
738 dname = devmap->device->discipline->name;
739 spin_unlock(&dasd_devmap_lock);
740 return snprintf(buf, PAGE_SIZE, "%s\n", dname);
741}
742
743static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
744
Horst Hummel3d052592006-04-27 18:40:28 -0700745static ssize_t
746dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
747{
748 struct dasd_devmap *devmap;
749 int alias;
750
751 devmap = dasd_find_busid(dev->bus_id);
752 spin_lock(&dasd_devmap_lock);
753 if (!IS_ERR(devmap))
754 alias = devmap->uid.alias;
755 else
756 alias = 0;
757 spin_unlock(&dasd_devmap_lock);
758
759 return sprintf(buf, alias ? "1\n" : "0\n");
760}
761
762static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
763
764static ssize_t
765dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
766{
767 struct dasd_devmap *devmap;
768 char *vendor;
769
770 devmap = dasd_find_busid(dev->bus_id);
771 spin_lock(&dasd_devmap_lock);
772 if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
773 vendor = devmap->uid.vendor;
774 else
775 vendor = "";
776 spin_unlock(&dasd_devmap_lock);
777
778 return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
779}
780
781static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
782
783#define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
784 /* SSID */ 4 + 1 + /* unit addr */ 2 + 1)
785
786static ssize_t
787dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
788{
789 struct dasd_devmap *devmap;
790 char uid[UID_STRLEN];
791
792 devmap = dasd_find_busid(dev->bus_id);
793 spin_lock(&dasd_devmap_lock);
794 if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
795 snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x",
796 devmap->uid.vendor, devmap->uid.serial,
797 devmap->uid.ssid, devmap->uid.unit_addr);
798 else
799 uid[0] = 0;
800 spin_unlock(&dasd_devmap_lock);
801
802 return snprintf(buf, PAGE_SIZE, "%s\n", uid);
803}
804
805static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
806
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800807/*
808 * extended error-reporting
809 */
810static ssize_t
811dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
812{
813 struct dasd_devmap *devmap;
814 int eer_flag;
815
816 devmap = dasd_find_busid(dev->bus_id);
817 if (!IS_ERR(devmap) && devmap->device)
818 eer_flag = dasd_eer_enabled(devmap->device);
819 else
820 eer_flag = 0;
821 return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
822}
823
824static ssize_t
825dasd_eer_store(struct device *dev, struct device_attribute *attr,
826 const char *buf, size_t count)
827{
828 struct dasd_devmap *devmap;
829 int rc;
830
831 devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
832 if (IS_ERR(devmap))
833 return PTR_ERR(devmap);
834 if (!devmap->device)
835 return count;
836 if (buf[0] == '1') {
837 rc = dasd_eer_enable(devmap->device);
838 if (rc)
839 return rc;
840 } else
841 dasd_eer_disable(devmap->device);
842 return count;
843}
844
845static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static struct attribute * dasd_attrs[] = {
848 &dev_attr_readonly.attr,
849 &dev_attr_discipline.attr,
Horst Hummel3d052592006-04-27 18:40:28 -0700850 &dev_attr_alias.attr,
851 &dev_attr_vendor.attr,
852 &dev_attr_uid.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 &dev_attr_use_diag.attr,
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800854 &dev_attr_eer_enabled.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 NULL,
856};
857
858static struct attribute_group dasd_attr_group = {
859 .attrs = dasd_attrs,
860};
861
Horst Hummel40545572006-06-29 15:08:18 +0200862/*
Horst Hummel3d052592006-04-27 18:40:28 -0700863 * Return copy of the device unique identifier.
864 */
865int
866dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
867{
868 struct dasd_devmap *devmap;
869
870 devmap = dasd_find_busid(cdev->dev.bus_id);
871 if (IS_ERR(devmap))
872 return PTR_ERR(devmap);
873 spin_lock(&dasd_devmap_lock);
874 *uid = devmap->uid;
875 spin_unlock(&dasd_devmap_lock);
876 return 0;
877}
878
879/*
880 * Register the given device unique identifier into devmap struct.
Horst Hummeld0710c72006-08-10 15:45:16 +0200881 * In addition check if the related storage server is already contained in the
882 * dasd_serverlist. If server is not contained, create new entry.
Horst Hummel40545572006-06-29 15:08:18 +0200883 * Return 0 if server was already in serverlist,
884 * 1 if the server was added successful
885 * <0 in case of error.
Horst Hummel3d052592006-04-27 18:40:28 -0700886 */
887int
888dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
889{
890 struct dasd_devmap *devmap;
Horst Hummeld0710c72006-08-10 15:45:16 +0200891 struct dasd_servermap *srv, *tmp;
Horst Hummel3d052592006-04-27 18:40:28 -0700892
893 devmap = dasd_find_busid(cdev->dev.bus_id);
894 if (IS_ERR(devmap))
895 return PTR_ERR(devmap);
Horst Hummeld0710c72006-08-10 15:45:16 +0200896
897 /* generate entry for servermap */
898 srv = (struct dasd_servermap *)
899 kzalloc(sizeof(struct dasd_servermap), GFP_KERNEL);
900 if (!srv)
901 return -ENOMEM;
902 strncpy(srv->sid.vendor, uid->vendor, sizeof(srv->sid.vendor) - 1);
903 strncpy(srv->sid.serial, uid->serial, sizeof(srv->sid.serial) - 1);
904
905 /* server is already contained ? */
Horst Hummel3d052592006-04-27 18:40:28 -0700906 spin_lock(&dasd_devmap_lock);
907 devmap->uid = *uid;
Horst Hummeld0710c72006-08-10 15:45:16 +0200908 list_for_each_entry(tmp, &dasd_serverlist, list) {
909 if (!memcmp(&srv->sid, &tmp->sid,
910 sizeof(struct dasd_servermap))) {
911 kfree(srv);
912 srv = NULL;
913 break;
914 }
915 }
916
917 /* add servermap to serverlist */
918 if (srv)
919 list_add(&srv->list, &dasd_serverlist);
Horst Hummel3d052592006-04-27 18:40:28 -0700920 spin_unlock(&dasd_devmap_lock);
Horst Hummeld0710c72006-08-10 15:45:16 +0200921
922 return (srv ? 1 : 0);
Horst Hummel3d052592006-04-27 18:40:28 -0700923}
Horst Hummel40545572006-06-29 15:08:18 +0200924EXPORT_SYMBOL_GPL(dasd_set_uid);
Horst Hummel3d052592006-04-27 18:40:28 -0700925
Horst Hummelf24acd42005-05-01 08:58:59 -0700926/*
927 * Return value of the specified feature.
928 */
929int
930dasd_get_feature(struct ccw_device *cdev, int feature)
931{
932 struct dasd_devmap *devmap;
933
934 devmap = dasd_find_busid(cdev->dev.bus_id);
935 if (IS_ERR(devmap))
Horst Hummel40545572006-06-29 15:08:18 +0200936 return PTR_ERR(devmap);
Horst Hummelf24acd42005-05-01 08:58:59 -0700937
938 return ((devmap->features & feature) != 0);
939}
940
941/*
942 * Set / reset given feature.
943 * Flag indicates wether to set (!=0) or the reset (=0) the feature.
944 */
945int
946dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
947{
948 struct dasd_devmap *devmap;
949
950 devmap = dasd_find_busid(cdev->dev.bus_id);
951 if (IS_ERR(devmap))
Horst Hummel40545572006-06-29 15:08:18 +0200952 return PTR_ERR(devmap);
Horst Hummelf24acd42005-05-01 08:58:59 -0700953
954 spin_lock(&dasd_devmap_lock);
955 if (flag)
956 devmap->features |= feature;
957 else
958 devmap->features &= ~feature;
Horst Hummelc6eb7b72005-09-03 15:57:58 -0700959 if (devmap->device)
960 devmap->device->features = devmap->features;
Horst Hummelf24acd42005-05-01 08:58:59 -0700961 spin_unlock(&dasd_devmap_lock);
962 return 0;
963}
964
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966int
967dasd_add_sysfs_files(struct ccw_device *cdev)
968{
969 return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
970}
971
972void
973dasd_remove_sysfs_files(struct ccw_device *cdev)
974{
975 sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
976}
977
978
979int
980dasd_devmap_init(void)
981{
982 int i;
983
984 /* Initialize devmap structures. */
985 dasd_max_devindex = 0;
986 for (i = 0; i < 256; i++)
987 INIT_LIST_HEAD(&dasd_hashlists[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Horst Hummel40545572006-06-29 15:08:18 +0200989 /* Initialize servermap structure. */
990 INIT_LIST_HEAD(&dasd_serverlist);
991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994void
995dasd_devmap_exit(void)
996{
997 dasd_forget_ranges();
998}