dasd_devmap.c 27 KB

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