dasd_devmap.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. kmem_cache_t *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 inline 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 inline 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 {
  182. MESSAGE(KERN_WARNING,
  183. "unsupported feature: %*s, "
  184. "ignoring setting", len, str);
  185. rc = -EINVAL;
  186. }
  187. str += len;
  188. if (*str != ':')
  189. break;
  190. str++;
  191. }
  192. if (*str != ')') {
  193. MESSAGE(KERN_WARNING, "%s",
  194. "missing ')' in dasd parameter string\n");
  195. rc = -EINVAL;
  196. } else
  197. str++;
  198. *endp = str;
  199. if (rc != 0)
  200. return rc;
  201. return features;
  202. }
  203. /*
  204. * Try to match the first element on the comma separated parse string
  205. * with one of the known keywords. If a keyword is found, take the approprate
  206. * action and return a pointer to the residual string. If the first element
  207. * could not be matched to any keyword then return an error code.
  208. */
  209. static char *
  210. dasd_parse_keyword( char *parsestring ) {
  211. char *nextcomma, *residual_str;
  212. int length;
  213. nextcomma = strchr(parsestring,',');
  214. if (nextcomma) {
  215. length = nextcomma - parsestring;
  216. residual_str = nextcomma + 1;
  217. } else {
  218. length = strlen(parsestring);
  219. residual_str = parsestring + length;
  220. }
  221. if (strncmp("autodetect", parsestring, length) == 0) {
  222. dasd_autodetect = 1;
  223. MESSAGE (KERN_INFO, "%s",
  224. "turning to autodetection mode");
  225. return residual_str;
  226. }
  227. if (strncmp("probeonly", parsestring, length) == 0) {
  228. dasd_probeonly = 1;
  229. MESSAGE(KERN_INFO, "%s",
  230. "turning to probeonly mode");
  231. return residual_str;
  232. }
  233. if (strncmp("nopav", parsestring, length) == 0) {
  234. if (MACHINE_IS_VM)
  235. MESSAGE(KERN_INFO, "%s", "'nopav' not supported on VM");
  236. else {
  237. dasd_nopav = 1;
  238. MESSAGE(KERN_INFO, "%s", "disable PAV mode");
  239. }
  240. return residual_str;
  241. }
  242. if (strncmp("fixedbuffers", parsestring, length) == 0) {
  243. if (dasd_page_cache)
  244. return residual_str;
  245. dasd_page_cache =
  246. kmem_cache_create("dasd_page_cache", PAGE_SIZE,
  247. PAGE_SIZE, SLAB_CACHE_DMA,
  248. NULL, NULL );
  249. if (!dasd_page_cache)
  250. MESSAGE(KERN_WARNING, "%s", "Failed to create slab, "
  251. "fixed buffer mode disabled.");
  252. else
  253. MESSAGE (KERN_INFO, "%s",
  254. "turning on fixed buffer mode");
  255. return residual_str;
  256. }
  257. return ERR_PTR(-EINVAL);
  258. }
  259. /*
  260. * Try to interprete the first element on the comma separated parse string
  261. * as a device number or a range of devices. If the interpretation is
  262. * successfull, create the matching dasd_devmap entries and return a pointer
  263. * to the residual string.
  264. * If interpretation fails or in case of an error, return an error code.
  265. */
  266. static char *
  267. dasd_parse_range( char *parsestring ) {
  268. struct dasd_devmap *devmap;
  269. int from, from_id0, from_id1;
  270. int to, to_id0, to_id1;
  271. int features, rc;
  272. char bus_id[BUS_ID_SIZE+1], *str;
  273. str = parsestring;
  274. rc = dasd_busid(&str, &from_id0, &from_id1, &from);
  275. if (rc == 0) {
  276. to = from;
  277. to_id0 = from_id0;
  278. to_id1 = from_id1;
  279. if (*str == '-') {
  280. str++;
  281. rc = dasd_busid(&str, &to_id0, &to_id1, &to);
  282. }
  283. }
  284. if (rc == 0 &&
  285. (from_id0 != to_id0 || from_id1 != to_id1 || from > to))
  286. rc = -EINVAL;
  287. if (rc) {
  288. MESSAGE(KERN_ERR, "Invalid device range %s", parsestring);
  289. return ERR_PTR(rc);
  290. }
  291. features = dasd_feature_list(str, &str);
  292. if (features < 0)
  293. return ERR_PTR(-EINVAL);
  294. /* each device in dasd= parameter should be set initially online */
  295. features |= DASD_FEATURE_INITIAL_ONLINE;
  296. while (from <= to) {
  297. sprintf(bus_id, "%01x.%01x.%04x",
  298. from_id0, from_id1, from++);
  299. devmap = dasd_add_busid(bus_id, features);
  300. if (IS_ERR(devmap))
  301. return (char *)devmap;
  302. }
  303. if (*str == ',')
  304. return str + 1;
  305. if (*str == '\0')
  306. return str;
  307. MESSAGE(KERN_WARNING,
  308. "junk at end of dasd parameter string: %s\n", str);
  309. return ERR_PTR(-EINVAL);
  310. }
  311. static inline char *
  312. dasd_parse_next_element( char *parsestring ) {
  313. char * residual_str;
  314. residual_str = dasd_parse_keyword(parsestring);
  315. if (!IS_ERR(residual_str))
  316. return residual_str;
  317. residual_str = dasd_parse_range(parsestring);
  318. return residual_str;
  319. }
  320. /*
  321. * Parse parameters stored in dasd[]
  322. * The 'dasd=...' parameter allows to specify a comma separated list of
  323. * keywords and device ranges. When the dasd driver is build into the kernel,
  324. * the complete list will be stored as one element of the dasd[] array.
  325. * When the dasd driver is build as a module, then the list is broken into
  326. * it's elements and each dasd[] entry contains one element.
  327. */
  328. int
  329. dasd_parse(void)
  330. {
  331. int rc, i;
  332. char *parsestring;
  333. rc = 0;
  334. for (i = 0; i < 256; i++) {
  335. if (dasd[i] == NULL)
  336. break;
  337. parsestring = dasd[i];
  338. /* loop over the comma separated list in the parsestring */
  339. while (*parsestring) {
  340. parsestring = dasd_parse_next_element(parsestring);
  341. if(IS_ERR(parsestring)) {
  342. rc = PTR_ERR(parsestring);
  343. break;
  344. }
  345. }
  346. if (rc) {
  347. DBF_EVENT(DBF_ALERT, "%s", "invalid range found");
  348. break;
  349. }
  350. }
  351. return rc;
  352. }
  353. /*
  354. * Add a devmap for the device specified by busid. It is possible that
  355. * the devmap already exists (dasd= parameter). The order of the devices
  356. * added through this function will define the kdevs for the individual
  357. * devices.
  358. */
  359. static struct dasd_devmap *
  360. dasd_add_busid(char *bus_id, int features)
  361. {
  362. struct dasd_devmap *devmap, *new, *tmp;
  363. int hash;
  364. new = (struct dasd_devmap *)
  365. kzalloc(sizeof(struct dasd_devmap), GFP_KERNEL);
  366. if (!new)
  367. return ERR_PTR(-ENOMEM);
  368. spin_lock(&dasd_devmap_lock);
  369. devmap = NULL;
  370. hash = dasd_hash_busid(bus_id);
  371. list_for_each_entry(tmp, &dasd_hashlists[hash], list)
  372. if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
  373. devmap = tmp;
  374. break;
  375. }
  376. if (!devmap) {
  377. /* This bus_id is new. */
  378. new->devindex = dasd_max_devindex++;
  379. strncpy(new->bus_id, bus_id, BUS_ID_SIZE);
  380. new->features = features;
  381. new->device = NULL;
  382. list_add(&new->list, &dasd_hashlists[hash]);
  383. devmap = new;
  384. new = NULL;
  385. }
  386. spin_unlock(&dasd_devmap_lock);
  387. kfree(new);
  388. return devmap;
  389. }
  390. /*
  391. * Find devmap for device with given bus_id.
  392. */
  393. static struct dasd_devmap *
  394. dasd_find_busid(char *bus_id)
  395. {
  396. struct dasd_devmap *devmap, *tmp;
  397. int hash;
  398. spin_lock(&dasd_devmap_lock);
  399. devmap = ERR_PTR(-ENODEV);
  400. hash = dasd_hash_busid(bus_id);
  401. list_for_each_entry(tmp, &dasd_hashlists[hash], list) {
  402. if (strncmp(tmp->bus_id, bus_id, BUS_ID_SIZE) == 0) {
  403. devmap = tmp;
  404. break;
  405. }
  406. }
  407. spin_unlock(&dasd_devmap_lock);
  408. return devmap;
  409. }
  410. /*
  411. * Check if busid has been added to the list of dasd ranges.
  412. */
  413. int
  414. dasd_busid_known(char *bus_id)
  415. {
  416. return IS_ERR(dasd_find_busid(bus_id)) ? -ENOENT : 0;
  417. }
  418. /*
  419. * Forget all about the device numbers added so far.
  420. * This may only be called at module unload or system shutdown.
  421. */
  422. static void
  423. dasd_forget_ranges(void)
  424. {
  425. struct dasd_devmap *devmap, *n;
  426. int i;
  427. spin_lock(&dasd_devmap_lock);
  428. for (i = 0; i < 256; i++) {
  429. list_for_each_entry_safe(devmap, n, &dasd_hashlists[i], list) {
  430. BUG_ON(devmap->device != NULL);
  431. list_del(&devmap->list);
  432. kfree(devmap);
  433. }
  434. }
  435. spin_unlock(&dasd_devmap_lock);
  436. }
  437. /*
  438. * Find the device struct by its device index.
  439. */
  440. struct dasd_device *
  441. dasd_device_from_devindex(int devindex)
  442. {
  443. struct dasd_devmap *devmap, *tmp;
  444. struct dasd_device *device;
  445. int i;
  446. spin_lock(&dasd_devmap_lock);
  447. devmap = NULL;
  448. for (i = 0; (i < 256) && !devmap; i++)
  449. list_for_each_entry(tmp, &dasd_hashlists[i], list)
  450. if (tmp->devindex == devindex) {
  451. /* Found the devmap for the device. */
  452. devmap = tmp;
  453. break;
  454. }
  455. if (devmap && devmap->device) {
  456. device = devmap->device;
  457. dasd_get_device(device);
  458. } else
  459. device = ERR_PTR(-ENODEV);
  460. spin_unlock(&dasd_devmap_lock);
  461. return device;
  462. }
  463. /*
  464. * Return devmap for cdev. If no devmap exists yet, create one and
  465. * connect it to the cdev.
  466. */
  467. static struct dasd_devmap *
  468. dasd_devmap_from_cdev(struct ccw_device *cdev)
  469. {
  470. struct dasd_devmap *devmap;
  471. devmap = dasd_find_busid(cdev->dev.bus_id);
  472. if (IS_ERR(devmap))
  473. devmap = dasd_add_busid(cdev->dev.bus_id,
  474. DASD_FEATURE_DEFAULT);
  475. return devmap;
  476. }
  477. /*
  478. * Create a dasd device structure for cdev.
  479. */
  480. struct dasd_device *
  481. dasd_create_device(struct ccw_device *cdev)
  482. {
  483. struct dasd_devmap *devmap;
  484. struct dasd_device *device;
  485. unsigned long flags;
  486. int rc;
  487. devmap = dasd_devmap_from_cdev(cdev);
  488. if (IS_ERR(devmap))
  489. return (void *) devmap;
  490. device = dasd_alloc_device();
  491. if (IS_ERR(device))
  492. return device;
  493. atomic_set(&device->ref_count, 3);
  494. spin_lock(&dasd_devmap_lock);
  495. if (!devmap->device) {
  496. devmap->device = device;
  497. device->devindex = devmap->devindex;
  498. device->features = devmap->features;
  499. get_device(&cdev->dev);
  500. device->cdev = cdev;
  501. rc = 0;
  502. } else
  503. /* Someone else was faster. */
  504. rc = -EBUSY;
  505. spin_unlock(&dasd_devmap_lock);
  506. if (rc) {
  507. dasd_free_device(device);
  508. return ERR_PTR(rc);
  509. }
  510. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  511. cdev->dev.driver_data = device;
  512. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  513. return device;
  514. }
  515. /*
  516. * Wait queue for dasd_delete_device waits.
  517. */
  518. static DECLARE_WAIT_QUEUE_HEAD(dasd_delete_wq);
  519. /*
  520. * Remove a dasd device structure. The passed referenced
  521. * is destroyed.
  522. */
  523. void
  524. dasd_delete_device(struct dasd_device *device)
  525. {
  526. struct ccw_device *cdev;
  527. struct dasd_devmap *devmap;
  528. unsigned long flags;
  529. /* First remove device pointer from devmap. */
  530. devmap = dasd_find_busid(device->cdev->dev.bus_id);
  531. BUG_ON(IS_ERR(devmap));
  532. spin_lock(&dasd_devmap_lock);
  533. if (devmap->device != device) {
  534. spin_unlock(&dasd_devmap_lock);
  535. dasd_put_device(device);
  536. return;
  537. }
  538. devmap->device = NULL;
  539. spin_unlock(&dasd_devmap_lock);
  540. /* Disconnect dasd_device structure from ccw_device structure. */
  541. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  542. device->cdev->dev.driver_data = NULL;
  543. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  544. /*
  545. * Drop ref_count by 3, one for the devmap reference, one for
  546. * the cdev reference and one for the passed reference.
  547. */
  548. atomic_sub(3, &device->ref_count);
  549. /* Wait for reference counter to drop to zero. */
  550. wait_event(dasd_delete_wq, atomic_read(&device->ref_count) == 0);
  551. /* Disconnect dasd_device structure from ccw_device structure. */
  552. cdev = device->cdev;
  553. device->cdev = NULL;
  554. /* Put ccw_device structure. */
  555. put_device(&cdev->dev);
  556. /* Now the device structure can be freed. */
  557. dasd_free_device(device);
  558. }
  559. /*
  560. * Reference counter dropped to zero. Wake up waiter
  561. * in dasd_delete_device.
  562. */
  563. void
  564. dasd_put_device_wake(struct dasd_device *device)
  565. {
  566. wake_up(&dasd_delete_wq);
  567. }
  568. /*
  569. * Return dasd_device structure associated with cdev.
  570. * This function needs to be called with the ccw device
  571. * lock held. It can be used from interrupt context.
  572. */
  573. struct dasd_device *
  574. dasd_device_from_cdev_locked(struct ccw_device *cdev)
  575. {
  576. struct dasd_device *device = cdev->dev.driver_data;
  577. if (!device)
  578. return ERR_PTR(-ENODEV);
  579. dasd_get_device(device);
  580. return device;
  581. }
  582. /*
  583. * Return dasd_device structure associated with cdev.
  584. */
  585. struct dasd_device *
  586. dasd_device_from_cdev(struct ccw_device *cdev)
  587. {
  588. struct dasd_device *device;
  589. unsigned long flags;
  590. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  591. device = dasd_device_from_cdev_locked(cdev);
  592. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  593. return device;
  594. }
  595. /*
  596. * SECTION: files in sysfs
  597. */
  598. /*
  599. * readonly controls the readonly status of a dasd
  600. */
  601. static ssize_t
  602. dasd_ro_show(struct device *dev, struct device_attribute *attr, char *buf)
  603. {
  604. struct dasd_devmap *devmap;
  605. int ro_flag;
  606. devmap = dasd_find_busid(dev->bus_id);
  607. if (!IS_ERR(devmap))
  608. ro_flag = (devmap->features & DASD_FEATURE_READONLY) != 0;
  609. else
  610. ro_flag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_READONLY) != 0;
  611. return snprintf(buf, PAGE_SIZE, ro_flag ? "1\n" : "0\n");
  612. }
  613. static ssize_t
  614. dasd_ro_store(struct device *dev, struct device_attribute *attr,
  615. const char *buf, size_t count)
  616. {
  617. struct dasd_devmap *devmap;
  618. int ro_flag;
  619. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  620. if (IS_ERR(devmap))
  621. return PTR_ERR(devmap);
  622. ro_flag = buf[0] == '1';
  623. spin_lock(&dasd_devmap_lock);
  624. if (ro_flag)
  625. devmap->features |= DASD_FEATURE_READONLY;
  626. else
  627. devmap->features &= ~DASD_FEATURE_READONLY;
  628. if (devmap->device)
  629. devmap->device->features = devmap->features;
  630. if (devmap->device && devmap->device->gdp)
  631. set_disk_ro(devmap->device->gdp, ro_flag);
  632. spin_unlock(&dasd_devmap_lock);
  633. return count;
  634. }
  635. static DEVICE_ATTR(readonly, 0644, dasd_ro_show, dasd_ro_store);
  636. /*
  637. * use_diag controls whether the driver should use diag rather than ssch
  638. * to talk to the device
  639. */
  640. static ssize_t
  641. dasd_use_diag_show(struct device *dev, struct device_attribute *attr, char *buf)
  642. {
  643. struct dasd_devmap *devmap;
  644. int use_diag;
  645. devmap = dasd_find_busid(dev->bus_id);
  646. if (!IS_ERR(devmap))
  647. use_diag = (devmap->features & DASD_FEATURE_USEDIAG) != 0;
  648. else
  649. use_diag = (DASD_FEATURE_DEFAULT & DASD_FEATURE_USEDIAG) != 0;
  650. return sprintf(buf, use_diag ? "1\n" : "0\n");
  651. }
  652. static ssize_t
  653. dasd_use_diag_store(struct device *dev, struct device_attribute *attr,
  654. const char *buf, size_t count)
  655. {
  656. struct dasd_devmap *devmap;
  657. ssize_t rc;
  658. int use_diag;
  659. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  660. if (IS_ERR(devmap))
  661. return PTR_ERR(devmap);
  662. use_diag = buf[0] == '1';
  663. spin_lock(&dasd_devmap_lock);
  664. /* Changing diag discipline flag is only allowed in offline state. */
  665. rc = count;
  666. if (!devmap->device) {
  667. if (use_diag)
  668. devmap->features |= DASD_FEATURE_USEDIAG;
  669. else
  670. devmap->features &= ~DASD_FEATURE_USEDIAG;
  671. } else
  672. rc = -EPERM;
  673. spin_unlock(&dasd_devmap_lock);
  674. return rc;
  675. }
  676. static DEVICE_ATTR(use_diag, 0644, dasd_use_diag_show, dasd_use_diag_store);
  677. static ssize_t
  678. dasd_discipline_show(struct device *dev, struct device_attribute *attr,
  679. char *buf)
  680. {
  681. struct dasd_device *device;
  682. ssize_t len;
  683. device = dasd_device_from_cdev(to_ccwdev(dev));
  684. if (!IS_ERR(device) && device->discipline) {
  685. len = snprintf(buf, PAGE_SIZE, "%s\n",
  686. device->discipline->name);
  687. dasd_put_device(device);
  688. } else
  689. len = snprintf(buf, PAGE_SIZE, "none\n");
  690. return len;
  691. }
  692. static DEVICE_ATTR(discipline, 0444, dasd_discipline_show, NULL);
  693. static ssize_t
  694. dasd_alias_show(struct device *dev, struct device_attribute *attr, char *buf)
  695. {
  696. struct dasd_devmap *devmap;
  697. int alias;
  698. devmap = dasd_find_busid(dev->bus_id);
  699. spin_lock(&dasd_devmap_lock);
  700. if (!IS_ERR(devmap))
  701. alias = devmap->uid.alias;
  702. else
  703. alias = 0;
  704. spin_unlock(&dasd_devmap_lock);
  705. return sprintf(buf, alias ? "1\n" : "0\n");
  706. }
  707. static DEVICE_ATTR(alias, 0444, dasd_alias_show, NULL);
  708. static ssize_t
  709. dasd_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
  710. {
  711. struct dasd_devmap *devmap;
  712. char *vendor;
  713. devmap = dasd_find_busid(dev->bus_id);
  714. spin_lock(&dasd_devmap_lock);
  715. if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
  716. vendor = devmap->uid.vendor;
  717. else
  718. vendor = "";
  719. spin_unlock(&dasd_devmap_lock);
  720. return snprintf(buf, PAGE_SIZE, "%s\n", vendor);
  721. }
  722. static DEVICE_ATTR(vendor, 0444, dasd_vendor_show, NULL);
  723. #define UID_STRLEN ( /* vendor */ 3 + 1 + /* serial */ 14 + 1 +\
  724. /* SSID */ 4 + 1 + /* unit addr */ 2 + 1)
  725. static ssize_t
  726. dasd_uid_show(struct device *dev, struct device_attribute *attr, char *buf)
  727. {
  728. struct dasd_devmap *devmap;
  729. char uid[UID_STRLEN];
  730. devmap = dasd_find_busid(dev->bus_id);
  731. spin_lock(&dasd_devmap_lock);
  732. if (!IS_ERR(devmap) && strlen(devmap->uid.vendor) > 0)
  733. snprintf(uid, sizeof(uid), "%s.%s.%04x.%02x",
  734. devmap->uid.vendor, devmap->uid.serial,
  735. devmap->uid.ssid, devmap->uid.unit_addr);
  736. else
  737. uid[0] = 0;
  738. spin_unlock(&dasd_devmap_lock);
  739. return snprintf(buf, PAGE_SIZE, "%s\n", uid);
  740. }
  741. static DEVICE_ATTR(uid, 0444, dasd_uid_show, NULL);
  742. /*
  743. * extended error-reporting
  744. */
  745. static ssize_t
  746. dasd_eer_show(struct device *dev, struct device_attribute *attr, char *buf)
  747. {
  748. struct dasd_devmap *devmap;
  749. int eer_flag;
  750. devmap = dasd_find_busid(dev->bus_id);
  751. if (!IS_ERR(devmap) && devmap->device)
  752. eer_flag = dasd_eer_enabled(devmap->device);
  753. else
  754. eer_flag = 0;
  755. return snprintf(buf, PAGE_SIZE, eer_flag ? "1\n" : "0\n");
  756. }
  757. static ssize_t
  758. dasd_eer_store(struct device *dev, struct device_attribute *attr,
  759. const char *buf, size_t count)
  760. {
  761. struct dasd_devmap *devmap;
  762. int rc;
  763. devmap = dasd_devmap_from_cdev(to_ccwdev(dev));
  764. if (IS_ERR(devmap))
  765. return PTR_ERR(devmap);
  766. if (!devmap->device)
  767. return count;
  768. if (buf[0] == '1') {
  769. rc = dasd_eer_enable(devmap->device);
  770. if (rc)
  771. return rc;
  772. } else
  773. dasd_eer_disable(devmap->device);
  774. return count;
  775. }
  776. static DEVICE_ATTR(eer_enabled, 0644, dasd_eer_show, dasd_eer_store);
  777. static struct attribute * dasd_attrs[] = {
  778. &dev_attr_readonly.attr,
  779. &dev_attr_discipline.attr,
  780. &dev_attr_alias.attr,
  781. &dev_attr_vendor.attr,
  782. &dev_attr_uid.attr,
  783. &dev_attr_use_diag.attr,
  784. &dev_attr_eer_enabled.attr,
  785. NULL,
  786. };
  787. static struct attribute_group dasd_attr_group = {
  788. .attrs = dasd_attrs,
  789. };
  790. /*
  791. * Return copy of the device unique identifier.
  792. */
  793. int
  794. dasd_get_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  795. {
  796. struct dasd_devmap *devmap;
  797. devmap = dasd_find_busid(cdev->dev.bus_id);
  798. if (IS_ERR(devmap))
  799. return PTR_ERR(devmap);
  800. spin_lock(&dasd_devmap_lock);
  801. *uid = devmap->uid;
  802. spin_unlock(&dasd_devmap_lock);
  803. return 0;
  804. }
  805. /*
  806. * Register the given device unique identifier into devmap struct.
  807. * In addition check if the related storage server subsystem ID is already
  808. * contained in the dasd_server_ssid_list. If subsystem ID is not contained,
  809. * create new entry.
  810. * Return 0 if server was already in serverlist,
  811. * 1 if the server was added successful
  812. * <0 in case of error.
  813. */
  814. int
  815. dasd_set_uid(struct ccw_device *cdev, struct dasd_uid *uid)
  816. {
  817. struct dasd_devmap *devmap;
  818. struct dasd_server_ssid_map *srv, *tmp;
  819. devmap = dasd_find_busid(cdev->dev.bus_id);
  820. if (IS_ERR(devmap))
  821. return PTR_ERR(devmap);
  822. /* generate entry for server_ssid_map */
  823. srv = (struct dasd_server_ssid_map *)
  824. kzalloc(sizeof(struct dasd_server_ssid_map), GFP_KERNEL);
  825. if (!srv)
  826. return -ENOMEM;
  827. strncpy(srv->sid.vendor, uid->vendor, sizeof(srv->sid.vendor) - 1);
  828. strncpy(srv->sid.serial, uid->serial, sizeof(srv->sid.serial) - 1);
  829. srv->sid.ssid = uid->ssid;
  830. /* server is already contained ? */
  831. spin_lock(&dasd_devmap_lock);
  832. devmap->uid = *uid;
  833. list_for_each_entry(tmp, &dasd_server_ssid_list, list) {
  834. if (!memcmp(&srv->sid, &tmp->sid,
  835. sizeof(struct system_id))) {
  836. kfree(srv);
  837. srv = NULL;
  838. break;
  839. }
  840. }
  841. /* add servermap to serverlist */
  842. if (srv)
  843. list_add(&srv->list, &dasd_server_ssid_list);
  844. spin_unlock(&dasd_devmap_lock);
  845. return (srv ? 1 : 0);
  846. }
  847. EXPORT_SYMBOL_GPL(dasd_set_uid);
  848. /*
  849. * Return value of the specified feature.
  850. */
  851. int
  852. dasd_get_feature(struct ccw_device *cdev, int feature)
  853. {
  854. struct dasd_devmap *devmap;
  855. devmap = dasd_find_busid(cdev->dev.bus_id);
  856. if (IS_ERR(devmap))
  857. return PTR_ERR(devmap);
  858. return ((devmap->features & feature) != 0);
  859. }
  860. /*
  861. * Set / reset given feature.
  862. * Flag indicates wether to set (!=0) or the reset (=0) the feature.
  863. */
  864. int
  865. dasd_set_feature(struct ccw_device *cdev, int feature, int flag)
  866. {
  867. struct dasd_devmap *devmap;
  868. devmap = dasd_find_busid(cdev->dev.bus_id);
  869. if (IS_ERR(devmap))
  870. return PTR_ERR(devmap);
  871. spin_lock(&dasd_devmap_lock);
  872. if (flag)
  873. devmap->features |= feature;
  874. else
  875. devmap->features &= ~feature;
  876. if (devmap->device)
  877. devmap->device->features = devmap->features;
  878. spin_unlock(&dasd_devmap_lock);
  879. return 0;
  880. }
  881. int
  882. dasd_add_sysfs_files(struct ccw_device *cdev)
  883. {
  884. return sysfs_create_group(&cdev->dev.kobj, &dasd_attr_group);
  885. }
  886. void
  887. dasd_remove_sysfs_files(struct ccw_device *cdev)
  888. {
  889. sysfs_remove_group(&cdev->dev.kobj, &dasd_attr_group);
  890. }
  891. int
  892. dasd_devmap_init(void)
  893. {
  894. int i;
  895. /* Initialize devmap structures. */
  896. dasd_max_devindex = 0;
  897. for (i = 0; i < 256; i++)
  898. INIT_LIST_HEAD(&dasd_hashlists[i]);
  899. /* Initialize servermap structure. */
  900. INIT_LIST_HEAD(&dasd_server_ssid_list);
  901. return 0;
  902. }
  903. void
  904. dasd_devmap_exit(void)
  905. {
  906. dasd_forget_ranges();
  907. }