dasd_devmap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. dasd_put_device(device);
  849. return sprintf(buf, "1\n");
  850. }
  851. }
  852. dasd_put_device(device);
  853. return sprintf(buf, "0\n");
  854. }
  855. static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
  856. static ssize_t dasd_vendor_show(struct device *dev,
  857. struct device_attribute *attr, char *buf)
  858. {
  859. struct dasd_device *device;
  860. struct dasd_uid uid;
  861. char *vendor;
  862. device = dasd_device_from_cdev(to_ccwdev(dev));
  863. vendor = "";
  864. if (IS_ERR(device))
  865. return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
  866. if (device->discipline && device->discipline->get_uid &&
  867. !device->discipline->get_uid(device, &uid))
  868. vendor = uid.vendor;
  869. dasd_put_device(device);
  870. return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
  871. }
  872. static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
  873. #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
  874. /* SSID */ 4 + 1 + /* unit addr */ 2 + 1 +\
  875. /* vduit */ 32 + 1)
  876. static ssize_t
  877. dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
  878. {
  879. struct dasd_device *device;
  880. struct dasd_uid uid;
  881. char uid_string[UID_STRLEN];
  882. char ua_string[3];
  883. device = dasd_device_from_cdev(to_ccwdev(dev));
  884. uid_string[0] = 0;
  885. if (IS_ERR(device))
  886. return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
  887. if (device->discipline && device->discipline->get_uid &&
  888. !device->discipline->get_uid(device, &uid)) {
  889. switch (uid.type) {
  890. case UA_BASE_DEVICE:
  891. snprintf(ua_string, sizeof(ua_string), "%02x",
  892. uid.real_unit_addr);
  893. break;
  894. case UA_BASE_PAV_ALIAS:
  895. snprintf(ua_string, sizeof(ua_string), "%02x",
  896. uid.base_unit_addr);
  897. break;
  898. case UA_HYPER_PAV_ALIAS:
  899. snprintf(ua_string, sizeof(ua_string), "xx");
  900. break;
  901. default:
  902. /* should not happen, treat like base device */
  903. snprintf(ua_string, sizeof(ua_string), "%02x",
  904. uid.real_unit_addr);
  905. break;
  906. }
  907. if (strlen(uid.vduit) > 0)
  908. snprintf(uid_string, sizeof(uid_string),
  909. "%s.%s.%04x.%s.%s",
  910. uid.vendor, uid.serial, uid.ssid, ua_string,
  911. uid.vduit);
  912. else
  913. snprintf(uid_string, sizeof(uid_string),
  914. "%s.%s.%04x.%s",
  915. uid.vendor, uid.serial, uid.ssid, ua_string);
  916. }
  917. dasd_put_device(device);
  918. return snprintf(buf, PAGE_SIZE, "%s\n", uid_string);
  919. }
  920. static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
  921. /*
  922. * extended error-reporting
  923. */
  924. static ssize_t
  925. dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
  926. {
  927. struct dasd_devmap *devmap;
  928. int eer_flag;
  929. devmap = dasd_find_busid(dev_name(dev));
  930. if (!IS_ERR(devmap) && devmap->device)
  931. eer_flag = dasd_eer_enabled(devmap->device);
  932. else
  933. eer_flag = 0;
  934. return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
  935. }
  936. static ssize_t
  937. dasd_eer_store(struct device *dev, struct device_attribute *attr,
  938. const char *buf, size_t count)
  939. {
  940. struct dasd_devmap *devmap;
  941. int val, rc;
  942. char *endp;
  943. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  944. if (IS_ERR(devmap))
  945. return PTR_ERR(devmap);
  946. if (!devmap->device)
  947. return -ENODEV;
  948. val = simple_strtoul(buf, &endp, 0);
  949. if (((endp + 1) < (buf + count)) || (val > 1))
  950. return -EINVAL;
  951. if (val) {
  952. rc = dasd_eer_enable(devmap->device);
  953. if (rc)
  954. return rc;
  955. } else
  956. dasd_eer_disable(devmap->device);
  957. return count;
  958. }
  959. static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
  960. static struct attribute * dasd_attrs[] = {
  961. &dev_attr_readonly.attr,
  962. &dev_attr_discipline.attr,
  963. &dev_attr_status.attr,
  964. &dev_attr_alias.attr,
  965. &dev_attr_vendor.attr,
  966. &dev_attr_uid.attr,
  967. &dev_attr_use_diag.attr,
  968. &dev_attr_eer_enabled.attr,
  969. &dev_attr_erplog.attr,
  970. &dev_attr_failfast.attr,
  971. NULL,
  972. };
  973. static struct attribute_group dasd_attr_group = {
  974. .attrs = dasd_attrs,
  975. };
  976. /*
  977. * Return value of the specified feature.
  978. */
  979. int
  980. dasd_get_feature(struct ccw_device *cdev, int feature)
  981. {
  982. struct dasd_devmap *devmap;
  983. devmap = dasd_find_busid(dev_name(&cdev->dev));
  984. if (IS_ERR(devmap))
  985. return PTR_ERR(devmap);
  986. return ((devmap->features & feature) != 0);
  987. }
  988. /*
  989. * Set / reset given feature.
  990. * Flag indicates wether to set (!=0) or the reset (=0) the feature.
  991. */
  992. int
  993. dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
  994. {
  995. struct dasd_devmap *devmap;
  996. devmap = dasd_find_busid(dev_name(&cdev->dev));
  997. if (IS_ERR(devmap))
  998. return PTR_ERR(devmap);
  999. spin_lock(&dasd_devmap_lock);
  1000. if (flag)
  1001. devmap->features |= feature;
  1002. else
  1003. devmap->features &= ~feature;
  1004. if (devmap->device)
  1005. devmap->device->features = devmap->features;
  1006. spin_unlock(&dasd_devmap_lock);
  1007. return 0;
  1008. }
  1009. int
  1010. dasd_add_sysfs_files(struct ccw_device *cdev)
  1011. {
  1012. return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
  1013. }
  1014. void
  1015. dasd_remove_sysfs_files(struct ccw_device *cdev)
  1016. {
  1017. sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
  1018. }
  1019. int
  1020. dasd_devmap_init(void)
  1021. {
  1022. int i;
  1023. /* Initialize devmap structures. */
  1024. dasd_max_devindex = 0;
  1025. for (i = 0; i < 256; i++)
  1026. INIT_LIST_HEAD(&dasd_hashlists[i]);
  1027. return 0;
  1028. }
  1029. void
  1030. dasd_devmap_exit(void)
  1031. {
  1032. dasd_forget_ranges();
  1033. }