dasd_devmap.c 29 KB

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