dasd_devmap.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  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. *
  14. */
  15. #include <linux/ctype.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <asm/debug.h>
  19. #include <asm/uaccess.h>
  20. /* This is ugly... */
  21. #define PRINTK_HEADER "dasd_devmap:"
  22. #include "dasd_int.h"
  23. struct kmem_cache *dasd_page_cache;
  24. EXPORT_SYMBOL_GPL(dasd_page_cache);
  25. /*
  26. * dasd_devmap_t is used to store the features and the relation
  27. * between device number and device index. To find a dasd_devmap_t
  28. * that corresponds to a device number of a device index each
  29. * dasd_devmap_t is added to two linked lists, one to search by
  30. * the device number and one to search by the device index. As
  31. * soon as big minor numbers are available the device index list
  32. * can be removed since the device number will then be identical
  33. * to the device index.
  34. */
  35. struct dasd_devmap {
  36. struct list_head list;
  37. char bus_id[BUS_ID_SIZE];
  38. unsigned int devindex;
  39. unsigned short features;
  40. struct dasd_device *device;
  41. struct dasd_uid uid;
  42. };
  43. /*
  44. * dasd_server_ssid_map contains a globally unique storage server subsystem ID.
  45. * dasd_server_ssid_list contains the list of all subsystem IDs accessed by
  46. * the DASD device driver.
  47. */
  48. struct dasd_server_ssid_map {
  49. struct list_head list;
  50. struct system_id {
  51. char vendor[4];
  52. char serial[15];
  53. __u16 ssid;
  54. } sid;
  55. };
  56. static struct list_head dasd_server_ssid_list;
  57. /*
  58. * Parameter parsing functions for dasd= parameter. The syntax is:
  59. * <devno> : (0x)?[0-9a-fA-F]+
  60. * <busid> : [0-0a-f]\.[0-9a-f]\.(0x)?[0-9a-fA-F]+
  61. * <feature> : ro
  62. * <feature_list> : \(<feature>(:<feature>)*\)
  63. * <devno-range> : <devno>(-<devno>)?<feature_list>?
  64. * <busid-range> : <busid>(-<busid>)?<feature_list>?
  65. * <devices> : <devno-range>|<busid-range>
  66. * <dasd_module> : dasd_diag_mod|dasd_eckd_mod|dasd_fba_mod
  67. *
  68. * <dasd> : autodetect|probeonly|<devices>(,<devices>)*
  69. */
  70. int dasd_probeonly = 0; /* is true, when probeonly mode is active */
  71. int dasd_autodetect = 0; /* is true, when autodetection is active */
  72. int dasd_nopav = 0; /* is true, when PAV is disabled */
  73. EXPORT_SYMBOL_GPL(dasd_nopav);
  74. /*
  75. * char *dasd[] is intended to hold the ranges supplied by the dasd= statement
  76. * it is named 'dasd' to directly be filled by insmod with the comma separated
  77. * strings when running as a module.
  78. */
  79. static char *dasd[256];
  80. module_param_array(dasd, charp, NULL, 0);
  81. /*
  82. * Single spinlock to protect devmap and servermap structures and lists.
  83. */
  84. static DEFINE_SPINLOCK(dasd_devmap_lock);
  85. /*
  86. * Hash lists for devmap structures.
  87. */
  88. static struct list_head dasd_hashlists[256];
  89. int dasd_max_devindex;
  90. static struct dasd_devmap *dasd_add_busid(char *, int);
  91. static inline int
  92. dasd_hash_busid(char *bus_id)
  93. {
  94. int hash, i;
  95. hash = 0;
  96. for (i = 0; (i < BUS_ID_SIZE) && *bus_id; i++, bus_id++)
  97. hash += *bus_id;
  98. return hash & 0xff;
  99. }
  100. #ifndef MODULE
  101. /*
  102. * The parameter parsing functions for builtin-drivers are called
  103. * before kmalloc works. Store the pointers to the parameters strings
  104. * into dasd[] for later processing.
  105. */
  106. static int __init
  107. dasd_call_setup(char *str)
  108. {
  109. static int count = 0;
  110. if (count < 256)
  111. dasd[count++] = str;
  112. return 1;
  113. }
  114. __setup ("dasd=", dasd_call_setup);
  115. #endif /* #ifndef MODULE */
  116. /*
  117. * Read a device busid/devno from a string.
  118. */
  119. static int
  120. dasd_busid(char **str, int *id0, int *id1, int *devno)
  121. {
  122. int val, old_style;
  123. /* check for leading '0x' */
  124. old_style = 0;
  125. if ((*str)[0] == '0' && (*str)[1] == 'x') {
  126. *str += 2;
  127. old_style = 1;
  128. }
  129. if (!isxdigit((*str)[0])) /* We require at least one hex digit */
  130. return -EINVAL;
  131. val = simple_strtoul(*str, str, 16);
  132. if (old_style || (*str)[0] != '.') {
  133. *id0 = *id1 = 0;
  134. if (val < 0 || val > 0xffff)
  135. return -EINVAL;
  136. *devno = val;
  137. return 0;
  138. }
  139. /* New style x.y.z busid */
  140. if (val < 0 || val > 0xff)
  141. return -EINVAL;
  142. *id0 = val;
  143. (*str)++;
  144. if (!isxdigit((*str)[0])) /* We require at least one hex digit */
  145. return -EINVAL;
  146. val = simple_strtoul(*str, str, 16);
  147. if (val < 0 || val > 0xff || (*str)++[0] != '.')
  148. return -EINVAL;
  149. *id1 = val;
  150. if (!isxdigit((*str)[0])) /* We require at least one hex digit */
  151. return -EINVAL;
  152. val = simple_strtoul(*str, str, 16);
  153. if (val < 0 || val > 0xffff)
  154. return -EINVAL;
  155. *devno = val;
  156. return 0;
  157. }
  158. /*
  159. * Read colon separated list of dasd features. Currently there is
  160. * only one: "ro" for read-only devices. The default feature set
  161. * is empty (value 0).
  162. */
  163. static int
  164. dasd_feature_list(char *str, char **endp)
  165. {
  166. int features, len, rc;
  167. rc = 0;
  168. if (*str != '(') {
  169. *endp = str;
  170. return DASD_FEATURE_DEFAULT;
  171. }
  172. str++;
  173. features = 0;
  174. while (1) {
  175. for (len = 0;
  176. str[len] && str[len] != ':' && str[len] != ')'; len++);
  177. if (len == 2 && !strncmp(str, "ro", 2))
  178. features |= DASD_FEATURE_READONLY;
  179. else if (len == 4 && !strncmp(str, "diag", 4))
  180. features |= DASD_FEATURE_USEDIAG;
  181. else if (len == 6 && !strncmp(str, "erplog", 6))
  182. features |= DASD_FEATURE_ERPLOG;
  183. else {
  184. MESSAGE(KERN_WARNING,
  185. "unsupported feature: %*s, "
  186. "ignoring setting", len, str);
  187. rc = -EINVAL;
  188. }
  189. str += len;
  190. if (*str != ':')
  191. break;
  192. str++;
  193. }
  194. if (*str != ')') {
  195. MESSAGE(KERN_WARNING, "%s",
  196. "missing ')' in dasd parameter string\n");
  197. rc = -EINVAL;
  198. } else
  199. str++;
  200. *endp = str;
  201. if (rc != 0)
  202. return rc;
  203. return features;
  204. }
  205. /*
  206. * Try to match the first element on the comma separated parse string
  207. * with one of the known keywords. If a keyword is found, take the approprate
  208. * action and return a pointer to the residual string. If the first element
  209. * could not be matched to any keyword then return an error code.
  210. */
  211. static char *
  212. dasd_parse_keyword( char *parsestring ) {
  213. char *nextcomma, *residual_str;
  214. int length;
  215. nextcomma = strchr(parsestring,',');
  216. if (nextcomma) {
  217. length = nextcomma - parsestring;
  218. residual_str = nextcomma + 1;
  219. } else {
  220. length = strlen(parsestring);
  221. residual_str = parsestring + length;
  222. }
  223. if (strncmp("autodetect", parsestring, length) == 0) {
  224. dasd_autodetect = 1;
  225. MESSAGE (KERN_INFO, "%s",
  226. "turning to autodetection mode");
  227. return residual_str;
  228. }
  229. if (strncmp("probeonly", parsestring, length) == 0) {
  230. dasd_probeonly = 1;
  231. MESSAGE(KERN_INFO, "%s",
  232. "turning to probeonly mode");
  233. return residual_str;
  234. }
  235. if (strncmp("nopav", parsestring, length) == 0) {
  236. if (MACHINE_IS_VM)
  237. MESSAGE(KERN_INFO, "%s", "'nopav' not supported on VM");
  238. else {
  239. dasd_nopav = 1;
  240. MESSAGE(KERN_INFO, "%s", "disable PAV mode");
  241. }
  242. return residual_str;
  243. }
  244. if (strncmp("fixedbuffers", parsestring, length) == 0) {
  245. if (dasd_page_cache)
  246. return residual_str;
  247. dasd_page_cache =
  248. kmem_cache_create("dasd_page_cache", PAGE_SIZE,
  249. PAGE_SIZE, SLAB_CACHE_DMA,
  250. NULL, NULL );
  251. if (!dasd_page_cache)
  252. MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
  253. "fixed buffer mode disabled.");
  254. else
  255. MESSAGE (KERN_INFO, "%s",
  256. "turning on fixed buffer mode");
  257. return residual_str;
  258. }
  259. return ERR_PTR(-EINVAL);
  260. }
  261. /*
  262. * Try to interprete the first element on the comma separated parse string
  263. * as a device number or a range of devices. If the interpretation is
  264. * successfull, create the matching dasd_devmap entries and return a pointer
  265. * to the residual string.
  266. * If interpretation fails or in case of an error, return an error code.
  267. */
  268. static char *
  269. dasd_parse_range( char *parsestring ) {
  270. struct dasd_devmap *devmap;
  271. int from, from_id0, from_id1;
  272. int to, to_id0, to_id1;
  273. int features, rc;
  274. char bus_id[BUS_ID_SIZE+1], *str;
  275. str = parsestring;
  276. rc = dasd_busid(&str, &from_id0, &from_id1, &from);
  277. if (rc == 0) {
  278. to = from;
  279. to_id0 = from_id0;
  280. to_id1 = from_id1;
  281. if (*str == '-') {
  282. str++;
  283. rc = dasd_busid(&str, &to_id0, &to_id1, &to);
  284. }
  285. }
  286. if (rc == 0 &&
  287. (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
  288. rc = -EINVAL;
  289. if (rc) {
  290. MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
  291. return ERR_PTR(rc);
  292. }
  293. features = dasd_feature_list(str, &str);
  294. if (features < 0)
  295. return ERR_PTR(-EINVAL);
  296. /* each device in dasd= parameter should be set initially online */
  297. features |= DASD_FEATURE_INITIAL_ONLINE;
  298. while (from <= to) {
  299. sprintf(bus_id, "%01x.%01x.%04x",
  300. from_id0, from_id1, from++);
  301. devmap = dasd_add_busid(bus_id, features);
  302. if (IS_ERR(devmap))
  303. return (char *)devmap;
  304. }
  305. if (*str == ',')
  306. return str + 1;
  307. if (*str == '\0')
  308. return str;
  309. MESSAGE(KERN_WARNING,
  310. "junk at end of dasd parameter string: %s\n", str);
  311. return ERR_PTR(-EINVAL);
  312. }
  313. static char *
  314. dasd_parse_next_element( char *parsestring ) {
  315. char * residual_str;
  316. residual_str = dasd_parse_keyword(parsestring);
  317. if (!IS_ERR(residual_str))
  318. return residual_str;
  319. residual_str = dasd_parse_range(parsestring);
  320. return residual_str;
  321. }
  322. /*
  323. * Parse parameters stored in dasd[]
  324. * The 'dasd=...' parameter allows to specify a comma separated list of
  325. * keywords and device ranges. When the dasd driver is build into the kernel,
  326. * the complete list will be stored as one element of the dasd[] array.
  327. * When the dasd driver is build as a module, then the list is broken into
  328. * it's elements and each dasd[] entry contains one element.
  329. */
  330. int
  331. dasd_parse(void)
  332. {
  333. int rc, i;
  334. char *parsestring;
  335. rc = 0;
  336. for (i = 0; i < 256; i++) {
  337. if (dasd[i] == NULL)
  338. break;
  339. parsestring = dasd[i];
  340. /* loop over the comma separated list in the parsestring */
  341. while (*parsestring) {
  342. parsestring = dasd_parse_next_element(parsestring);
  343. if(IS_ERR(parsestring)) {
  344. rc = PTR_ERR(parsestring);
  345. break;
  346. }
  347. }
  348. if (rc) {
  349. DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
  350. break;
  351. }
  352. }
  353. return rc;
  354. }
  355. /*
  356. * Add a devmap for the device specified by busid. It is possible that
  357. * the devmap already exists (dasd= parameter). The order of the devices
  358. * added through this function will define the kdevs for the individual
  359. * devices.
  360. */
  361. static struct dasd_devmap *
  362. dasd_add_busid(char *bus_id, int features)
  363. {
  364. struct dasd_devmap *devmap, *new, *tmp;
  365. int hash;
  366. new = (struct dasd_devmap *)
  367. kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
  368. if (!new)
  369. return ERR_PTR(-ENOMEM);
  370. spin_lock(&dasd_devmap_lock);
  371. devmap = NULL;
  372. hash = dasd_hash_busid(bus_id);
  373. list_for_each_entry(tmp, &dasd_hashlists[hash], list)
  374. if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
  375. devmap = tmp;
  376. break;
  377. }
  378. if (!devmap) {
  379. /* This bus_id is new. */
  380. new->devindex = dasd_max_devindex++;
  381. strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
  382. new->features = features;
  383. new->device = NULL;
  384. list_add(&new->list, &dasd_hashlists[hash]);
  385. devmap = new;
  386. new = NULL;
  387. }
  388. spin_unlock(&dasd_devmap_lock);
  389. kfree(new);
  390. return devmap;
  391. }
  392. /*
  393. * Find devmap for device with given bus_id.
  394. */
  395. static struct dasd_devmap *
  396. dasd_find_busid(char *bus_id)
  397. {
  398. struct dasd_devmap *devmap, *tmp;
  399. int hash;
  400. spin_lock(&dasd_devmap_lock);
  401. devmap = ERR_PTR(-ENODEV);
  402. hash = dasd_hash_busid(bus_id);
  403. list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
  404. if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
  405. devmap = tmp;
  406. break;
  407. }
  408. }
  409. spin_unlock(&dasd_devmap_lock);
  410. return devmap;
  411. }
  412. /*
  413. * Check if busid has been added to the list of dasd ranges.
  414. */
  415. int
  416. dasd_busid_known(char *bus_id)
  417. {
  418. return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
  419. }
  420. /*
  421. * Forget all about the device numbers added so far.
  422. * This may only be called at module unload or system shutdown.
  423. */
  424. static void
  425. dasd_forget_ranges(void)
  426. {
  427. struct dasd_devmap *devmap, *n;
  428. int i;
  429. spin_lock(&dasd_devmap_lock);
  430. for (i = 0; i < 256; i++) {
  431. list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
  432. BUG_ON(devmap->device != NULL);
  433. list_del(&devmap->list);
  434. kfree(devmap);
  435. }
  436. }
  437. spin_unlock(&dasd_devmap_lock);
  438. }
  439. /*
  440. * Find the device struct by its device index.
  441. */
  442. struct dasd_device *
  443. dasd_device_from_devindex(int devindex)
  444. {
  445. struct dasd_devmap *devmap, *tmp;
  446. struct dasd_device *device;
  447. int i;
  448. spin_lock(&dasd_devmap_lock);
  449. devmap = NULL;
  450. for (i = 0; (i < 256) && !devmap; i++)
  451. list_for_each_entry(tmp, &dasd_hashlists[i], list)
  452. if (tmp->devindex == devindex) {
  453. /* Found the devmap for the device. */
  454. devmap = tmp;
  455. break;
  456. }
  457. if (devmap && devmap->device) {
  458. device = devmap->device;
  459. dasd_get_device(device);
  460. } else
  461. device = ERR_PTR(-ENODEV);
  462. spin_unlock(&dasd_devmap_lock);
  463. return device;
  464. }
  465. /*
  466. * Return devmap for cdev. If no devmap exists yet, create one and
  467. * connect it to the cdev.
  468. */
  469. static struct dasd_devmap *
  470. dasd_devmap_from_cdev(struct ccw_device *cdev)
  471. {
  472. struct dasd_devmap *devmap;
  473. devmap = dasd_find_busid(cdev->dev.bus_id);
  474. if (IS_ERR(devmap))
  475. devmap = dasd_add_busid(cdev->dev.bus_id,
  476. DASD_FEATURE_DEFAULT);
  477. return devmap;
  478. }
  479. /*
  480. * Create a dasd device structure for cdev.
  481. */
  482. struct dasd_device *
  483. dasd_create_device(struct ccw_device *cdev)
  484. {
  485. struct dasd_devmap *devmap;
  486. struct dasd_device *device;
  487. unsigned long flags;
  488. int rc;
  489. devmap = dasd_devmap_from_cdev(cdev);
  490. if (IS_ERR(devmap))
  491. return (void *) devmap;
  492. device = dasd_alloc_device();
  493. if (IS_ERR(device))
  494. return device;
  495. atomic_set(&device->ref_count, 3);
  496. spin_lock(&dasd_devmap_lock);
  497. if (!devmap->device) {
  498. devmap->device = device;
  499. device->devindex = devmap->devindex;
  500. device->features = devmap->features;
  501. get_device(&cdev->dev);
  502. device->cdev = cdev;
  503. rc = 0;
  504. } else
  505. /* Someone else was faster. */
  506. rc = -EBUSY;
  507. spin_unlock(&dasd_devmap_lock);
  508. if (rc) {
  509. dasd_free_device(device);
  510. return ERR_PTR(rc);
  511. }
  512. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  513. cdev->dev.driver_data = device;
  514. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  515. return device;
  516. }
  517. /*
  518. * Wait queue for dasd_delete_device waits.
  519. */
  520. static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
  521. /*
  522. * Remove a dasd device structure. The passed referenced
  523. * is destroyed.
  524. */
  525. void
  526. dasd_delete_device(struct dasd_device *device)
  527. {
  528. struct ccw_device *cdev;
  529. struct dasd_devmap *devmap;
  530. unsigned long flags;
  531. /* First remove device pointer from devmap. */
  532. devmap = dasd_find_busid(device->cdev->dev.bus_id);
  533. BUG_ON(IS_ERR(devmap));
  534. spin_lock(&dasd_devmap_lock);
  535. if (devmap->device != device) {
  536. spin_unlock(&dasd_devmap_lock);
  537. dasd_put_device(device);
  538. return;
  539. }
  540. devmap->device = NULL;
  541. spin_unlock(&dasd_devmap_lock);
  542. /* Disconnect dasd_device structure from ccw_device structure. */
  543. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  544. device->cdev->dev.driver_data = NULL;
  545. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  546. /*
  547. * Drop ref_count by 3, one for the devmap reference, one for
  548. * the cdev reference and one for the passed reference.
  549. */
  550. atomic_sub(3, &device->ref_count);
  551. /* Wait for reference counter to drop to zero. */
  552. wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
  553. /* Disconnect dasd_device structure from ccw_device structure. */
  554. cdev = device->cdev;
  555. device->cdev = NULL;
  556. /* Put ccw_device structure. */
  557. put_device(&cdev->dev);
  558. /* Now the device structure can be freed. */
  559. dasd_free_device(device);
  560. }
  561. /*
  562. * Reference counter dropped to zero. Wake up waiter
  563. * in dasd_delete_device.
  564. */
  565. void
  566. dasd_put_device_wake(struct dasd_device *device)
  567. {
  568. wake_up(&dasd_delete_wq);
  569. }
  570. /*
  571. * Return dasd_device structure associated with cdev.
  572. * This function needs to be called with the ccw device
  573. * lock held. It can be used from interrupt context.
  574. */
  575. struct dasd_device *
  576. dasd_device_from_cdev_locked(struct ccw_device *cdev)
  577. {
  578. struct dasd_device *device = cdev->dev.driver_data;
  579. if (!device)
  580. return ERR_PTR(-ENODEV);
  581. dasd_get_device(device);
  582. return device;
  583. }
  584. /*
  585. * Return dasd_device structure associated with cdev.
  586. */
  587. struct dasd_device *
  588. dasd_device_from_cdev(struct ccw_device *cdev)
  589. {
  590. struct dasd_device *device;
  591. unsigned long flags;
  592. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  593. device = dasd_device_from_cdev_locked(cdev);
  594. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  595. return device;
  596. }
  597. /*
  598. * SECTION: files in sysfs
  599. */
  600. /*
  601. * readonly controls the readonly status of a dasd
  602. */
  603. static ssize_t
  604. dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
  605. {
  606. struct dasd_devmap *devmap;
  607. int ro_flag;
  608. devmap = dasd_find_busid(dev->bus_id);
  609. if (!IS_ERR(devmap))
  610. ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
  611. else
  612. ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
  613. return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
  614. }
  615. static ssize_t
  616. dasd_ro_store(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct dasd_devmap *devmap;
  620. int val;
  621. char *endp;
  622. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  623. if (IS_ERR(devmap))
  624. return PTR_ERR(devmap);
  625. val = simple_strtoul(buf, &endp, 0);
  626. if (((endp + 1) < (buf + count)) || (val > 1))
  627. return -EINVAL;
  628. spin_lock(&dasd_devmap_lock);
  629. if (val)
  630. devmap->features |= DASD_FEATURE_READONLY;
  631. else
  632. devmap->features &= ~DASD_FEATURE_READONLY;
  633. if (devmap->device)
  634. devmap->device->features = devmap->features;
  635. if (devmap->device && devmap->device->gdp)
  636. set_disk_ro(devmap->device->gdp, val);
  637. spin_unlock(&dasd_devmap_lock);
  638. return count;
  639. }
  640. static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
  641. /*
  642. * erplog controls the logging of ERP related data
  643. * (e.g. failing channel programs).
  644. */
  645. static ssize_t
  646. dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
  647. {
  648. struct dasd_devmap *devmap;
  649. int erplog;
  650. devmap = dasd_find_busid(dev->bus_id);
  651. if (!IS_ERR(devmap))
  652. erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
  653. else
  654. erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
  655. return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");
  656. }
  657. static ssize_t
  658. dasd_erplog_store(struct device *dev, struct device_attribute *attr,
  659. const char *buf, size_t count)
  660. {
  661. struct dasd_devmap *devmap;
  662. int val;
  663. char *endp;
  664. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  665. if (IS_ERR(devmap))
  666. return PTR_ERR(devmap);
  667. val = simple_strtoul(buf, &endp, 0);
  668. if (((endp + 1) < (buf + count)) || (val > 1))
  669. return -EINVAL;
  670. spin_lock(&dasd_devmap_lock);
  671. if (val)
  672. devmap->features |= DASD_FEATURE_ERPLOG;
  673. else
  674. devmap->features &= ~DASD_FEATURE_ERPLOG;
  675. if (devmap->device)
  676. devmap->device->features = devmap->features;
  677. spin_unlock(&dasd_devmap_lock);
  678. return count;
  679. }
  680. static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
  681. /*
  682. * use_diag controls whether the driver should use diag rather than ssch
  683. * to talk to the device
  684. */
  685. static ssize_t
  686. dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
  687. {
  688. struct dasd_devmap *devmap;
  689. int use_diag;
  690. devmap = dasd_find_busid(dev->bus_id);
  691. if (!IS_ERR(devmap))
  692. use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
  693. else
  694. use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
  695. return sprintf(buf, use_diag ? "1\n" : "0\n");
  696. }
  697. static ssize_t
  698. dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
  699. const char *buf, size_t count)
  700. {
  701. struct dasd_devmap *devmap;
  702. ssize_t rc;
  703. int val;
  704. char *endp;
  705. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  706. if (IS_ERR(devmap))
  707. return PTR_ERR(devmap);
  708. val = simple_strtoul(buf, &endp, 0);
  709. if (((endp + 1) < (buf + count)) || (val > 1))
  710. return -EINVAL;
  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 (val)
  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. static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
  725. static ssize_t
  726. dasd_discipline_show(struct device *dev, struct device_attribute *attr,
  727. char *buf)
  728. {
  729. struct dasd_device *device;
  730. ssize_t len;
  731. device = dasd_device_from_cdev(to_ccwdev(dev));
  732. if (!IS_ERR(device) && device->discipline) {
  733. len = snprintf(buf, PAGE_SIZE, "%s\n",
  734. device->discipline->name);
  735. dasd_put_device(device);
  736. } else
  737. len = snprintf(buf, PAGE_SIZE, "none\n");
  738. return len;
  739. }
  740. static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
  741. static ssize_t
  742. dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
  743. {
  744. struct dasd_devmap *devmap;
  745. int alias;
  746. devmap = dasd_find_busid(dev->bus_id);
  747. spin_lock(&dasd_devmap_lock);
  748. if (!IS_ERR(devmap))
  749. alias = devmap->uid.alias;
  750. else
  751. alias = 0;
  752. spin_unlock(&dasd_devmap_lock);
  753. return sprintf(buf, alias ? "1\n" : "0\n");
  754. }
  755. static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
  756. static ssize_t
  757. dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
  758. {
  759. struct dasd_devmap *devmap;
  760. char *vendor;
  761. devmap = dasd_find_busid(dev->bus_id);
  762. spin_lock(&dasd_devmap_lock);
  763. if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
  764. vendor = devmap->uid.vendor;
  765. else
  766. vendor = "";
  767. spin_unlock(&dasd_devmap_lock);
  768. return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
  769. }
  770. static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
  771. #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
  772. /* SSID */ 4 + 1 + /* unit addr */ 2 + 1)
  773. static ssize_t
  774. dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
  775. {
  776. struct dasd_devmap *devmap;
  777. char uid[UID_STRLEN];
  778. devmap = dasd_find_busid(dev->bus_id);
  779. spin_lock(&dasd_devmap_lock);
  780. if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
  781. snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x",
  782. devmap->uid.vendor, devmap->uid.serial,
  783. devmap->uid.ssid, devmap->uid.unit_addr);
  784. else
  785. uid[0] = 0;
  786. spin_unlock(&dasd_devmap_lock);
  787. return snprintf(buf, PAGE_SIZE, "%s\n", uid);
  788. }
  789. static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
  790. /*
  791. * extended error-reporting
  792. */
  793. static ssize_t
  794. dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
  795. {
  796. struct dasd_devmap *devmap;
  797. int eer_flag;
  798. devmap = dasd_find_busid(dev->bus_id);
  799. if (!IS_ERR(devmap) && devmap->device)
  800. eer_flag = dasd_eer_enabled(devmap->device);
  801. else
  802. eer_flag = 0;
  803. return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
  804. }
  805. static ssize_t
  806. dasd_eer_store(struct device *dev, struct device_attribute *attr,
  807. const char *buf, size_t count)
  808. {
  809. struct dasd_devmap *devmap;
  810. int val, rc;
  811. char *endp;
  812. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  813. if (IS_ERR(devmap))
  814. return PTR_ERR(devmap);
  815. if (!devmap->device)
  816. return -ENODEV;
  817. val = simple_strtoul(buf, &endp, 0);
  818. if (((endp + 1) < (buf + count)) || (val > 1))
  819. return -EINVAL;
  820. if (val) {
  821. rc = dasd_eer_enable(devmap->device);
  822. if (rc)
  823. return rc;
  824. } else
  825. dasd_eer_disable(devmap->device);
  826. return count;
  827. }
  828. static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
  829. static struct attribute * dasd_attrs[] = {
  830. &dev_attr_readonly.attr,
  831. &dev_attr_discipline.attr,
  832. &dev_attr_alias.attr,
  833. &dev_attr_vendor.attr,
  834. &dev_attr_uid.attr,
  835. &dev_attr_use_diag.attr,
  836. &dev_attr_eer_enabled.attr,
  837. &dev_attr_erplog.attr,
  838. NULL,
  839. };
  840. static struct attribute_group dasd_attr_group = {
  841. .attrs = dasd_attrs,
  842. };
  843. /*
  844. * Return copy of the device unique identifier.
  845. */
  846. int
  847. dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  848. {
  849. struct dasd_devmap *devmap;
  850. devmap = dasd_find_busid(cdev->dev.bus_id);
  851. if (IS_ERR(devmap))
  852. return PTR_ERR(devmap);
  853. spin_lock(&dasd_devmap_lock);
  854. *uid = devmap->uid;
  855. spin_unlock(&dasd_devmap_lock);
  856. return 0;
  857. }
  858. /*
  859. * Register the given device unique identifier into devmap struct.
  860. * In addition check if the related storage server subsystem ID is already
  861. * contained in the dasd_server_ssid_list. If subsystem ID is not contained,
  862. * create new entry.
  863. * Return 0 if server was already in serverlist,
  864. * 1 if the server was added successful
  865. * <0 in case of error.
  866. */
  867. int
  868. dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  869. {
  870. struct dasd_devmap *devmap;
  871. struct dasd_server_ssid_map *srv, *tmp;
  872. devmap = dasd_find_busid(cdev->dev.bus_id);
  873. if (IS_ERR(devmap))
  874. return PTR_ERR(devmap);
  875. /* generate entry for server_ssid_map */
  876. srv = (struct dasd_server_ssid_map *)
  877. kzalloc(sizeof(struct dasd_server_ssid_map), GFP_KERNEL);
  878. if (!srv)
  879. return -ENOMEM;
  880. strncpy(srv->sid.vendor, uid->vendor, sizeof(srv->sid.vendor) - 1);
  881. strncpy(srv->sid.serial, uid->serial, sizeof(srv->sid.serial) - 1);
  882. srv->sid.ssid = uid->ssid;
  883. /* server is already contained ? */
  884. spin_lock(&dasd_devmap_lock);
  885. devmap->uid = *uid;
  886. list_for_each_entry(tmp, &dasd_server_ssid_list, list) {
  887. if (!memcmp(&srv->sid, &tmp->sid,
  888. sizeof(struct system_id))) {
  889. kfree(srv);
  890. srv = NULL;
  891. break;
  892. }
  893. }
  894. /* add servermap to serverlist */
  895. if (srv)
  896. list_add(&srv->list, &dasd_server_ssid_list);
  897. spin_unlock(&dasd_devmap_lock);
  898. return (srv ? 1 : 0);
  899. }
  900. EXPORT_SYMBOL_GPL(dasd_set_uid);
  901. /*
  902. * Return value of the specified feature.
  903. */
  904. int
  905. dasd_get_feature(struct ccw_device *cdev, int feature)
  906. {
  907. struct dasd_devmap *devmap;
  908. devmap = dasd_find_busid(cdev->dev.bus_id);
  909. if (IS_ERR(devmap))
  910. return PTR_ERR(devmap);
  911. return ((devmap->features & feature) != 0);
  912. }
  913. /*
  914. * Set / reset given feature.
  915. * Flag indicates wether to set (!=0) or the reset (=0) the feature.
  916. */
  917. int
  918. dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
  919. {
  920. struct dasd_devmap *devmap;
  921. devmap = dasd_find_busid(cdev->dev.bus_id);
  922. if (IS_ERR(devmap))
  923. return PTR_ERR(devmap);
  924. spin_lock(&dasd_devmap_lock);
  925. if (flag)
  926. devmap->features |= feature;
  927. else
  928. devmap->features &= ~feature;
  929. if (devmap->device)
  930. devmap->device->features = devmap->features;
  931. spin_unlock(&dasd_devmap_lock);
  932. return 0;
  933. }
  934. int
  935. dasd_add_sysfs_files(struct ccw_device *cdev)
  936. {
  937. return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
  938. }
  939. void
  940. dasd_remove_sysfs_files(struct ccw_device *cdev)
  941. {
  942. sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
  943. }
  944. int
  945. dasd_devmap_init(void)
  946. {
  947. int i;
  948. /* Initialize devmap structures. */
  949. dasd_max_devindex = 0;
  950. for (i = 0; i < 256; i++)
  951. INIT_LIST_HEAD(&dasd_hashlists[i]);
  952. /* Initialize servermap structure. */
  953. INIT_LIST_HEAD(&dasd_server_ssid_list);
  954. return 0;
  955. }
  956. void
  957. dasd_devmap_exit(void)
  958. {
  959. dasd_forget_ranges();
  960. }