dasd_devmap.c 29 KB

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