dasd_devmap.c 29 KB

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