dasd_devmap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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 <asm/debug.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/ipl.h>
  22. /* This is ugly... */
  23. #define PRINTK_HEADER "dasd_devmap:"
  24. #define DASD_BUS_ID_SIZE 20
  25. #include "dasd_int.h"
  26. struct kmem_cache *dasd_page_cache;
  27. EXPORT_SYMBOL_GPL(dasd_page_cache);
  28. /*
  29. * dasd_devmap_t is used to store the features and the relation
  30. * between device number and device index. To find a dasd_devmap_t
  31. * that corresponds to a device number of a device index each
  32. * dasd_devmap_t is added to two linked lists, one to search by
  33. * the device number and one to search by the device index. As
  34. * soon as big minor numbers are available the device index list
  35. * can be removed since the device number will then be identical
  36. * to the device index.
  37. */
  38. struct dasd_devmap {
  39. struct list_head list;
  40. char bus_id[DASD_BUS_ID_SIZE];
  41. unsigned int devindex;
  42. unsigned short features;
  43. struct dasd_device *device;
  44. struct dasd_uid uid;
  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. int val;
  668. char *endp;
  669. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  670. if (IS_ERR(devmap))
  671. return PTR_ERR(devmap);
  672. val = simple_strtoul(buf, &endp, 0);
  673. if (((endp + 1) < (buf + count)) || (val > 1))
  674. return -EINVAL;
  675. spin_lock(&dasd_devmap_lock);
  676. if (val)
  677. devmap->features |= DASD_FEATURE_READONLY;
  678. else
  679. devmap->features &= ~DASD_FEATURE_READONLY;
  680. if (devmap->device)
  681. devmap->device->features = devmap->features;
  682. if (devmap->device && devmap->device->block
  683. && devmap->device->block->gdp)
  684. set_disk_ro(devmap->device->block->gdp, val);
  685. spin_unlock(&dasd_devmap_lock);
  686. return count;
  687. }
  688. static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
  689. /*
  690. * erplog controls the logging of ERP related data
  691. * (e.g. failing channel programs).
  692. */
  693. static ssize_t
  694. dasd_erplog_show(struct device *dev, struct device_attribute *attr, char *buf)
  695. {
  696. struct dasd_devmap *devmap;
  697. int erplog;
  698. devmap = dasd_find_busid(dev_name(dev));
  699. if (!IS_ERR(devmap))
  700. erplog = (devmap->features & DASD_FEATURE_ERPLOG) != 0;
  701. else
  702. erplog = (DASD_FEATURE_DEFAULT & DASD_FEATURE_ERPLOG) != 0;
  703. return snprintf(buf, PAGE_SIZE, erplog ? "1\n" : "0\n");
  704. }
  705. static ssize_t
  706. dasd_erplog_store(struct device *dev, struct device_attribute *attr,
  707. const char *buf, size_t count)
  708. {
  709. struct dasd_devmap *devmap;
  710. int val;
  711. char *endp;
  712. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  713. if (IS_ERR(devmap))
  714. return PTR_ERR(devmap);
  715. val = simple_strtoul(buf, &endp, 0);
  716. if (((endp + 1) < (buf + count)) || (val > 1))
  717. return -EINVAL;
  718. spin_lock(&dasd_devmap_lock);
  719. if (val)
  720. devmap->features |= DASD_FEATURE_ERPLOG;
  721. else
  722. devmap->features &= ~DASD_FEATURE_ERPLOG;
  723. if (devmap->device)
  724. devmap->device->features = devmap->features;
  725. spin_unlock(&dasd_devmap_lock);
  726. return count;
  727. }
  728. static DEVICE_ATTR(erplog, 0644, dasd_erplog_show, dasd_erplog_store);
  729. /*
  730. * use_diag controls whether the driver should use diag rather than ssch
  731. * to talk to the device
  732. */
  733. static ssize_t
  734. dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
  735. {
  736. struct dasd_devmap *devmap;
  737. int use_diag;
  738. devmap = dasd_find_busid(dev_name(dev));
  739. if (!IS_ERR(devmap))
  740. use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
  741. else
  742. use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
  743. return sprintf(buf, use_diag ? "1\n" : "0\n");
  744. }
  745. static ssize_t
  746. dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
  747. const char *buf, size_t count)
  748. {
  749. struct dasd_devmap *devmap;
  750. ssize_t rc;
  751. int val;
  752. char *endp;
  753. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  754. if (IS_ERR(devmap))
  755. return PTR_ERR(devmap);
  756. val = simple_strtoul(buf, &endp, 0);
  757. if (((endp + 1) < (buf + count)) || (val > 1))
  758. return -EINVAL;
  759. spin_lock(&dasd_devmap_lock);
  760. /* Changing diag discipline flag is only allowed in offline state. */
  761. rc = count;
  762. if (!devmap->device) {
  763. if (val)
  764. devmap->features |= DASD_FEATURE_USEDIAG;
  765. else
  766. devmap->features &= ~DASD_FEATURE_USEDIAG;
  767. } else
  768. rc = -EPERM;
  769. spin_unlock(&dasd_devmap_lock);
  770. return rc;
  771. }
  772. static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
  773. static ssize_t
  774. dasd_discipline_show(struct device *dev, struct device_attribute *attr,
  775. char *buf)
  776. {
  777. struct dasd_device *device;
  778. ssize_t len;
  779. device = dasd_device_from_cdev(to_ccwdev(dev));
  780. if (!IS_ERR(device) && device->discipline) {
  781. len = snprintf(buf, PAGE_SIZE, "%s\n",
  782. device->discipline->name);
  783. dasd_put_device(device);
  784. } else
  785. len = snprintf(buf, PAGE_SIZE, "none\n");
  786. return len;
  787. }
  788. static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
  789. static ssize_t
  790. dasd_device_status_show(struct device *dev, struct device_attribute *attr,
  791. char *buf)
  792. {
  793. struct dasd_device *device;
  794. ssize_t len;
  795. device = dasd_device_from_cdev(to_ccwdev(dev));
  796. if (!IS_ERR(device)) {
  797. switch (device->state) {
  798. case DASD_STATE_NEW:
  799. len = snprintf(buf, PAGE_SIZE, "new\n");
  800. break;
  801. case DASD_STATE_KNOWN:
  802. len = snprintf(buf, PAGE_SIZE, "detected\n");
  803. break;
  804. case DASD_STATE_BASIC:
  805. len = snprintf(buf, PAGE_SIZE, "basic\n");
  806. break;
  807. case DASD_STATE_UNFMT:
  808. len = snprintf(buf, PAGE_SIZE, "unformatted\n");
  809. break;
  810. case DASD_STATE_READY:
  811. len = snprintf(buf, PAGE_SIZE, "ready\n");
  812. break;
  813. case DASD_STATE_ONLINE:
  814. len = snprintf(buf, PAGE_SIZE, "online\n");
  815. break;
  816. default:
  817. len = snprintf(buf, PAGE_SIZE, "no stat\n");
  818. break;
  819. }
  820. dasd_put_device(device);
  821. } else
  822. len = snprintf(buf, PAGE_SIZE, "unknown\n");
  823. return len;
  824. }
  825. static DEVICE_ATTR(status, 0444, dasd_device_status_show, NULL);
  826. static ssize_t
  827. dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
  828. {
  829. struct dasd_devmap *devmap;
  830. int alias;
  831. devmap = dasd_find_busid(dev_name(dev));
  832. spin_lock(&dasd_devmap_lock);
  833. if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
  834. spin_unlock(&dasd_devmap_lock);
  835. return sprintf(buf, "0\n");
  836. }
  837. if (devmap->uid.type == UA_BASE_PAV_ALIAS ||
  838. devmap->uid.type == UA_HYPER_PAV_ALIAS)
  839. alias = 1;
  840. else
  841. alias = 0;
  842. spin_unlock(&dasd_devmap_lock);
  843. return sprintf(buf, alias ? "1\n" : "0\n");
  844. }
  845. static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
  846. static ssize_t
  847. dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
  848. {
  849. struct dasd_devmap *devmap;
  850. char *vendor;
  851. devmap = dasd_find_busid(dev_name(dev));
  852. spin_lock(&dasd_devmap_lock);
  853. if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
  854. vendor = devmap->uid.vendor;
  855. else
  856. vendor = "";
  857. spin_unlock(&dasd_devmap_lock);
  858. return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
  859. }
  860. static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
  861. #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
  862. /* SSID */ 4 + 1 + /* unit addr */ 2 + 1 +\
  863. /* vduit */ 32 + 1)
  864. static ssize_t
  865. dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
  866. {
  867. struct dasd_devmap *devmap;
  868. char uid_string[UID_STRLEN];
  869. char ua_string[3];
  870. struct dasd_uid *uid;
  871. devmap = dasd_find_busid(dev_name(dev));
  872. spin_lock(&dasd_devmap_lock);
  873. if (IS_ERR(devmap) || strlen(devmap->uid.vendor) == 0) {
  874. spin_unlock(&dasd_devmap_lock);
  875. return sprintf(buf, "\n");
  876. }
  877. uid = &devmap->uid;
  878. switch (uid->type) {
  879. case UA_BASE_DEVICE:
  880. sprintf(ua_string, "%02x", uid->real_unit_addr);
  881. break;
  882. case UA_BASE_PAV_ALIAS:
  883. sprintf(ua_string, "%02x", uid->base_unit_addr);
  884. break;
  885. case UA_HYPER_PAV_ALIAS:
  886. sprintf(ua_string, "xx");
  887. break;
  888. default:
  889. /* should not happen, treat like base device */
  890. sprintf(ua_string, "%02x", uid->real_unit_addr);
  891. break;
  892. }
  893. if (strlen(uid->vduit) > 0)
  894. snprintf(uid_string, sizeof(uid_string),
  895. "%s.%s.%04x.%s.%s",
  896. uid->vendor, uid->serial,
  897. uid->ssid, ua_string,
  898. uid->vduit);
  899. else
  900. snprintf(uid_string, sizeof(uid_string),
  901. "%s.%s.%04x.%s",
  902. uid->vendor, uid->serial,
  903. uid->ssid, ua_string);
  904. spin_unlock(&dasd_devmap_lock);
  905. return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
  906. }
  907. static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
  908. /*
  909. * extended error-reporting
  910. */
  911. static ssize_t
  912. dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
  913. {
  914. struct dasd_devmap *devmap;
  915. int eer_flag;
  916. devmap = dasd_find_busid(dev_name(dev));
  917. if (!IS_ERR(devmap) && devmap->device)
  918. eer_flag = dasd_eer_enabled(devmap->device);
  919. else
  920. eer_flag = 0;
  921. return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
  922. }
  923. static ssize_t
  924. dasd_eer_store(struct device *dev, struct device_attribute *attr,
  925. const char *buf, size_t count)
  926. {
  927. struct dasd_devmap *devmap;
  928. int val, rc;
  929. char *endp;
  930. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  931. if (IS_ERR(devmap))
  932. return PTR_ERR(devmap);
  933. if (!devmap->device)
  934. return -ENODEV;
  935. val = simple_strtoul(buf, &endp, 0);
  936. if (((endp + 1) < (buf + count)) || (val > 1))
  937. return -EINVAL;
  938. if (val) {
  939. rc = dasd_eer_enable(devmap->device);
  940. if (rc)
  941. return rc;
  942. } else
  943. dasd_eer_disable(devmap->device);
  944. return count;
  945. }
  946. static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
  947. static struct attribute * dasd_attrs[] = {
  948. &dev_attr_readonly.attr,
  949. &dev_attr_discipline.attr,
  950. &dev_attr_status.attr,
  951. &dev_attr_alias.attr,
  952. &dev_attr_vendor.attr,
  953. &dev_attr_uid.attr,
  954. &dev_attr_use_diag.attr,
  955. &dev_attr_eer_enabled.attr,
  956. &dev_attr_erplog.attr,
  957. &dev_attr_failfast.attr,
  958. NULL,
  959. };
  960. static struct attribute_group dasd_attr_group = {
  961. .attrs = dasd_attrs,
  962. };
  963. /*
  964. * Return copy of the device unique identifier.
  965. */
  966. int
  967. dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  968. {
  969. struct dasd_devmap *devmap;
  970. devmap = dasd_find_busid(dev_name(&cdev->dev));
  971. if (IS_ERR(devmap))
  972. return PTR_ERR(devmap);
  973. spin_lock(&dasd_devmap_lock);
  974. *uid = devmap->uid;
  975. spin_unlock(&dasd_devmap_lock);
  976. return 0;
  977. }
  978. EXPORT_SYMBOL_GPL(dasd_get_uid);
  979. /*
  980. * Register the given device unique identifier into devmap struct.
  981. * In addition check if the related storage server subsystem ID is already
  982. * contained in the dasd_server_ssid_list. If subsystem ID is not contained,
  983. * create new entry.
  984. * Return 0 if server was already in serverlist,
  985. * 1 if the server was added successful
  986. * <0 in case of error.
  987. */
  988. int
  989. dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  990. {
  991. struct dasd_devmap *devmap;
  992. devmap = dasd_find_busid(dev_name(&cdev->dev));
  993. if (IS_ERR(devmap))
  994. return PTR_ERR(devmap);
  995. spin_lock(&dasd_devmap_lock);
  996. devmap->uid = *uid;
  997. spin_unlock(&dasd_devmap_lock);
  998. return 0;
  999. }
  1000. EXPORT_SYMBOL_GPL(dasd_set_uid);
  1001. /*
  1002. * Return value of the specified feature.
  1003. */
  1004. int
  1005. dasd_get_feature(struct ccw_device *cdev, int feature)
  1006. {
  1007. struct dasd_devmap *devmap;
  1008. devmap = dasd_find_busid(dev_name(&cdev->dev));
  1009. if (IS_ERR(devmap))
  1010. return PTR_ERR(devmap);
  1011. return ((devmap->features & feature) != 0);
  1012. }
  1013. /*
  1014. * Set / reset given feature.
  1015. * Flag indicates wether to set (!=0) or the reset (=0) the feature.
  1016. */
  1017. int
  1018. dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
  1019. {
  1020. struct dasd_devmap *devmap;
  1021. devmap = dasd_find_busid(dev_name(&cdev->dev));
  1022. if (IS_ERR(devmap))
  1023. return PTR_ERR(devmap);
  1024. spin_lock(&dasd_devmap_lock);
  1025. if (flag)
  1026. devmap->features |= feature;
  1027. else
  1028. devmap->features &= ~feature;
  1029. if (devmap->device)
  1030. devmap->device->features = devmap->features;
  1031. spin_unlock(&dasd_devmap_lock);
  1032. return 0;
  1033. }
  1034. int
  1035. dasd_add_sysfs_files(struct ccw_device *cdev)
  1036. {
  1037. return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
  1038. }
  1039. void
  1040. dasd_remove_sysfs_files(struct ccw_device *cdev)
  1041. {
  1042. sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
  1043. }
  1044. int
  1045. dasd_devmap_init(void)
  1046. {
  1047. int i;
  1048. /* Initialize devmap structures. */
  1049. dasd_max_devindex = 0;
  1050. for (i = 0; i < 256; i++)
  1051. INIT_LIST_HEAD(&dasd_hashlists[i]);
  1052. return 0;
  1053. }
  1054. void
  1055. dasd_devmap_exit(void)
  1056. {
  1057. dasd_forget_ranges();
  1058. }