dasd_devmap.c 28 KB

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