dasd_devmap.c 28 KB

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