device.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/device.h>
  19. #include <linux/workqueue.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include "cio.h"
  24. #include "css.h"
  25. #include "device.h"
  26. #include "ioasm.h"
  27. /******************* bus type handling ***********************/
  28. /* The Linux driver model distinguishes between a bus type and
  29. * the bus itself. Of course we only have one channel
  30. * subsystem driver and one channel system per machine, but
  31. * we still use the abstraction. T.R. says it's a good idea. */
  32. static int
  33. ccw_bus_match (struct device * dev, struct device_driver * drv)
  34. {
  35. struct ccw_device *cdev = to_ccwdev(dev);
  36. struct ccw_driver *cdrv = to_ccwdrv(drv);
  37. const struct ccw_device_id *ids = cdrv->ids, *found;
  38. if (!ids)
  39. return 0;
  40. found = ccw_device_id_match(ids, &cdev->id);
  41. if (!found)
  42. return 0;
  43. cdev->id.driver_info = found->driver_info;
  44. return 1;
  45. }
  46. /* Store modalias string delimited by prefix/suffix string into buffer with
  47. * specified size. Return length of resulting string (excluding trailing '\0')
  48. * even if string doesn't fit buffer (snprintf semantics). */
  49. static int snprint_alias(char *buf, size_t size, const char *prefix,
  50. struct ccw_device_id *id, const char *suffix)
  51. {
  52. int len;
  53. len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
  54. id->cu_model);
  55. if (len > size)
  56. return len;
  57. buf += len;
  58. size -= len;
  59. if (id->dev_type != 0)
  60. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  61. id->dev_model, suffix);
  62. else
  63. len += snprintf(buf, size, "dtdm%s", suffix);
  64. return len;
  65. }
  66. /* Set up environment variables for ccw device uevent. Return 0 on success,
  67. * non-zero otherwise. */
  68. static int ccw_uevent(struct device *dev, char **envp, int num_envp,
  69. char *buffer, int buffer_size)
  70. {
  71. struct ccw_device *cdev = to_ccwdev(dev);
  72. struct ccw_device_id *id = &(cdev->id);
  73. int i = 0;
  74. int len;
  75. /* CU_TYPE= */
  76. len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
  77. if (len > buffer_size || i >= num_envp)
  78. return -ENOMEM;
  79. envp[i++] = buffer;
  80. buffer += len;
  81. buffer_size -= len;
  82. /* CU_MODEL= */
  83. len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
  84. if (len > buffer_size || i >= num_envp)
  85. return -ENOMEM;
  86. envp[i++] = buffer;
  87. buffer += len;
  88. buffer_size -= len;
  89. /* The next two can be zero, that's ok for us */
  90. /* DEV_TYPE= */
  91. len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
  92. if (len > buffer_size || i >= num_envp)
  93. return -ENOMEM;
  94. envp[i++] = buffer;
  95. buffer += len;
  96. buffer_size -= len;
  97. /* DEV_MODEL= */
  98. len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
  99. (unsigned char) id->dev_model) + 1;
  100. if (len > buffer_size || i >= num_envp)
  101. return -ENOMEM;
  102. envp[i++] = buffer;
  103. buffer += len;
  104. buffer_size -= len;
  105. /* MODALIAS= */
  106. len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
  107. if (len > buffer_size || i >= num_envp)
  108. return -ENOMEM;
  109. envp[i++] = buffer;
  110. buffer += len;
  111. buffer_size -= len;
  112. envp[i] = NULL;
  113. return 0;
  114. }
  115. struct bus_type ccw_bus_type;
  116. static int io_subchannel_probe (struct subchannel *);
  117. static int io_subchannel_remove (struct subchannel *);
  118. void io_subchannel_irq (struct device *);
  119. static int io_subchannel_notify(struct device *, int);
  120. static void io_subchannel_verify(struct device *);
  121. static void io_subchannel_ioterm(struct device *);
  122. static void io_subchannel_shutdown(struct subchannel *);
  123. struct css_driver io_subchannel_driver = {
  124. .subchannel_type = SUBCHANNEL_TYPE_IO,
  125. .drv = {
  126. .name = "io_subchannel",
  127. .bus = &css_bus_type,
  128. },
  129. .irq = io_subchannel_irq,
  130. .notify = io_subchannel_notify,
  131. .verify = io_subchannel_verify,
  132. .termination = io_subchannel_ioterm,
  133. .probe = io_subchannel_probe,
  134. .remove = io_subchannel_remove,
  135. .shutdown = io_subchannel_shutdown,
  136. };
  137. struct workqueue_struct *ccw_device_work;
  138. struct workqueue_struct *ccw_device_notify_work;
  139. wait_queue_head_t ccw_device_init_wq;
  140. atomic_t ccw_device_init_count;
  141. static int __init
  142. init_ccw_bus_type (void)
  143. {
  144. int ret;
  145. init_waitqueue_head(&ccw_device_init_wq);
  146. atomic_set(&ccw_device_init_count, 0);
  147. ccw_device_work = create_singlethread_workqueue("cio");
  148. if (!ccw_device_work)
  149. return -ENOMEM; /* FIXME: better errno ? */
  150. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  151. if (!ccw_device_notify_work) {
  152. ret = -ENOMEM; /* FIXME: better errno ? */
  153. goto out_err;
  154. }
  155. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  156. if (!slow_path_wq) {
  157. ret = -ENOMEM; /* FIXME: better errno ? */
  158. goto out_err;
  159. }
  160. if ((ret = bus_register (&ccw_bus_type)))
  161. goto out_err;
  162. if ((ret = driver_register(&io_subchannel_driver.drv)))
  163. goto out_err;
  164. wait_event(ccw_device_init_wq,
  165. atomic_read(&ccw_device_init_count) == 0);
  166. flush_workqueue(ccw_device_work);
  167. return 0;
  168. out_err:
  169. if (ccw_device_work)
  170. destroy_workqueue(ccw_device_work);
  171. if (ccw_device_notify_work)
  172. destroy_workqueue(ccw_device_notify_work);
  173. if (slow_path_wq)
  174. destroy_workqueue(slow_path_wq);
  175. return ret;
  176. }
  177. static void __exit
  178. cleanup_ccw_bus_type (void)
  179. {
  180. driver_unregister(&io_subchannel_driver.drv);
  181. bus_unregister(&ccw_bus_type);
  182. destroy_workqueue(ccw_device_notify_work);
  183. destroy_workqueue(ccw_device_work);
  184. }
  185. subsys_initcall(init_ccw_bus_type);
  186. module_exit(cleanup_ccw_bus_type);
  187. /************************ device handling **************************/
  188. /*
  189. * A ccw_device has some interfaces in sysfs in addition to the
  190. * standard ones.
  191. * The following entries are designed to export the information which
  192. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  193. * are obvious, so they don't have an entry :)
  194. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  195. */
  196. static ssize_t
  197. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  198. {
  199. struct subchannel *sch = to_subchannel(dev);
  200. struct ssd_info *ssd = &sch->ssd_info;
  201. ssize_t ret = 0;
  202. int chp;
  203. for (chp = 0; chp < 8; chp++)
  204. ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
  205. ret += sprintf (buf+ret, "\n");
  206. return min((ssize_t)PAGE_SIZE, ret);
  207. }
  208. static ssize_t
  209. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  210. {
  211. struct subchannel *sch = to_subchannel(dev);
  212. struct pmcw *pmcw = &sch->schib.pmcw;
  213. return sprintf (buf, "%02x %02x %02x\n",
  214. pmcw->pim, pmcw->pam, pmcw->pom);
  215. }
  216. static ssize_t
  217. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  218. {
  219. struct ccw_device *cdev = to_ccwdev(dev);
  220. struct ccw_device_id *id = &(cdev->id);
  221. if (id->dev_type != 0)
  222. return sprintf(buf, "%04x/%02x\n",
  223. id->dev_type, id->dev_model);
  224. else
  225. return sprintf(buf, "n/a\n");
  226. }
  227. static ssize_t
  228. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  229. {
  230. struct ccw_device *cdev = to_ccwdev(dev);
  231. struct ccw_device_id *id = &(cdev->id);
  232. return sprintf(buf, "%04x/%02x\n",
  233. id->cu_type, id->cu_model);
  234. }
  235. static ssize_t
  236. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct ccw_device *cdev = to_ccwdev(dev);
  239. struct ccw_device_id *id = &(cdev->id);
  240. int len;
  241. len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
  242. return len > PAGE_SIZE ? PAGE_SIZE : len;
  243. }
  244. static ssize_t
  245. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct ccw_device *cdev = to_ccwdev(dev);
  248. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  249. }
  250. static void
  251. ccw_device_remove_disconnected(struct ccw_device *cdev)
  252. {
  253. struct subchannel *sch;
  254. /*
  255. * Forced offline in disconnected state means
  256. * 'throw away device'.
  257. */
  258. sch = to_subchannel(cdev->dev.parent);
  259. css_sch_device_unregister(sch);
  260. /* Reset intparm to zeroes. */
  261. sch->schib.pmcw.intparm = 0;
  262. cio_modify(sch);
  263. put_device(&sch->dev);
  264. }
  265. int
  266. ccw_device_set_offline(struct ccw_device *cdev)
  267. {
  268. int ret;
  269. if (!cdev)
  270. return -ENODEV;
  271. if (!cdev->online || !cdev->drv)
  272. return -EINVAL;
  273. if (cdev->drv->set_offline) {
  274. ret = cdev->drv->set_offline(cdev);
  275. if (ret != 0)
  276. return ret;
  277. }
  278. cdev->online = 0;
  279. spin_lock_irq(cdev->ccwlock);
  280. ret = ccw_device_offline(cdev);
  281. if (ret == -ENODEV) {
  282. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  283. cdev->private->state = DEV_STATE_OFFLINE;
  284. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  285. }
  286. spin_unlock_irq(cdev->ccwlock);
  287. return ret;
  288. }
  289. spin_unlock_irq(cdev->ccwlock);
  290. if (ret == 0)
  291. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  292. else {
  293. pr_debug("ccw_device_offline returned %d, device %s\n",
  294. ret, cdev->dev.bus_id);
  295. cdev->online = 1;
  296. }
  297. return ret;
  298. }
  299. int
  300. ccw_device_set_online(struct ccw_device *cdev)
  301. {
  302. int ret;
  303. if (!cdev)
  304. return -ENODEV;
  305. if (cdev->online || !cdev->drv)
  306. return -EINVAL;
  307. spin_lock_irq(cdev->ccwlock);
  308. ret = ccw_device_online(cdev);
  309. spin_unlock_irq(cdev->ccwlock);
  310. if (ret == 0)
  311. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  312. else {
  313. pr_debug("ccw_device_online returned %d, device %s\n",
  314. ret, cdev->dev.bus_id);
  315. return ret;
  316. }
  317. if (cdev->private->state != DEV_STATE_ONLINE)
  318. return -ENODEV;
  319. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  320. cdev->online = 1;
  321. return 0;
  322. }
  323. spin_lock_irq(cdev->ccwlock);
  324. ret = ccw_device_offline(cdev);
  325. spin_unlock_irq(cdev->ccwlock);
  326. if (ret == 0)
  327. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  328. else
  329. pr_debug("ccw_device_offline returned %d, device %s\n",
  330. ret, cdev->dev.bus_id);
  331. return (ret == 0) ? -ENODEV : ret;
  332. }
  333. static ssize_t
  334. online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  335. {
  336. struct ccw_device *cdev = to_ccwdev(dev);
  337. int i, force, ret;
  338. char *tmp;
  339. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  340. return -EAGAIN;
  341. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  342. atomic_set(&cdev->private->onoff, 0);
  343. return -EINVAL;
  344. }
  345. if (!strncmp(buf, "force\n", count)) {
  346. force = 1;
  347. i = 1;
  348. } else {
  349. force = 0;
  350. i = simple_strtoul(buf, &tmp, 16);
  351. }
  352. if (i == 1) {
  353. /* Do device recognition, if needed. */
  354. if (cdev->id.cu_type == 0) {
  355. ret = ccw_device_recognition(cdev);
  356. if (ret) {
  357. printk(KERN_WARNING"Couldn't start recognition "
  358. "for device %s (ret=%d)\n",
  359. cdev->dev.bus_id, ret);
  360. goto out;
  361. }
  362. wait_event(cdev->private->wait_q,
  363. cdev->private->flags.recog_done);
  364. }
  365. if (cdev->drv && cdev->drv->set_online)
  366. ccw_device_set_online(cdev);
  367. } else if (i == 0) {
  368. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  369. ccw_device_remove_disconnected(cdev);
  370. else if (cdev->drv && cdev->drv->set_offline)
  371. ccw_device_set_offline(cdev);
  372. }
  373. if (force && cdev->private->state == DEV_STATE_BOXED) {
  374. ret = ccw_device_stlck(cdev);
  375. if (ret) {
  376. printk(KERN_WARNING"ccw_device_stlck for device %s "
  377. "returned %d!\n", cdev->dev.bus_id, ret);
  378. goto out;
  379. }
  380. /* Do device recognition, if needed. */
  381. if (cdev->id.cu_type == 0) {
  382. cdev->private->state = DEV_STATE_NOT_OPER;
  383. ret = ccw_device_recognition(cdev);
  384. if (ret) {
  385. printk(KERN_WARNING"Couldn't start recognition "
  386. "for device %s (ret=%d)\n",
  387. cdev->dev.bus_id, ret);
  388. goto out;
  389. }
  390. wait_event(cdev->private->wait_q,
  391. cdev->private->flags.recog_done);
  392. }
  393. if (cdev->drv && cdev->drv->set_online)
  394. ccw_device_set_online(cdev);
  395. }
  396. out:
  397. if (cdev->drv)
  398. module_put(cdev->drv->owner);
  399. atomic_set(&cdev->private->onoff, 0);
  400. return count;
  401. }
  402. static ssize_t
  403. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  404. {
  405. struct ccw_device *cdev = to_ccwdev(dev);
  406. struct subchannel *sch;
  407. switch (cdev->private->state) {
  408. case DEV_STATE_BOXED:
  409. return sprintf(buf, "boxed\n");
  410. case DEV_STATE_DISCONNECTED:
  411. case DEV_STATE_DISCONNECTED_SENSE_ID:
  412. case DEV_STATE_NOT_OPER:
  413. sch = to_subchannel(dev->parent);
  414. if (!sch->lpm)
  415. return sprintf(buf, "no path\n");
  416. else
  417. return sprintf(buf, "no device\n");
  418. default:
  419. /* All other states considered fine. */
  420. return sprintf(buf, "good\n");
  421. }
  422. }
  423. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  424. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  425. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  426. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  427. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  428. static DEVICE_ATTR(online, 0644, online_show, online_store);
  429. extern struct device_attribute dev_attr_cmb_enable;
  430. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  431. static struct attribute * subch_attrs[] = {
  432. &dev_attr_chpids.attr,
  433. &dev_attr_pimpampom.attr,
  434. NULL,
  435. };
  436. static struct attribute_group subch_attr_group = {
  437. .attrs = subch_attrs,
  438. };
  439. static inline int
  440. subchannel_add_files (struct device *dev)
  441. {
  442. return sysfs_create_group(&dev->kobj, &subch_attr_group);
  443. }
  444. static struct attribute * ccwdev_attrs[] = {
  445. &dev_attr_devtype.attr,
  446. &dev_attr_cutype.attr,
  447. &dev_attr_modalias.attr,
  448. &dev_attr_online.attr,
  449. &dev_attr_cmb_enable.attr,
  450. &dev_attr_availability.attr,
  451. NULL,
  452. };
  453. static struct attribute_group ccwdev_attr_group = {
  454. .attrs = ccwdev_attrs,
  455. };
  456. static inline int
  457. device_add_files (struct device *dev)
  458. {
  459. return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
  460. }
  461. static inline void
  462. device_remove_files(struct device *dev)
  463. {
  464. sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
  465. }
  466. /* this is a simple abstraction for device_register that sets the
  467. * correct bus type and adds the bus specific files */
  468. int
  469. ccw_device_register(struct ccw_device *cdev)
  470. {
  471. struct device *dev = &cdev->dev;
  472. int ret;
  473. dev->bus = &ccw_bus_type;
  474. if ((ret = device_add(dev)))
  475. return ret;
  476. set_bit(1, &cdev->private->registered);
  477. if ((ret = device_add_files(dev))) {
  478. if (test_and_clear_bit(1, &cdev->private->registered))
  479. device_del(dev);
  480. }
  481. return ret;
  482. }
  483. struct match_data {
  484. struct ccw_dev_id dev_id;
  485. struct ccw_device * sibling;
  486. };
  487. static int
  488. match_devno(struct device * dev, void * data)
  489. {
  490. struct match_data * d = data;
  491. struct ccw_device * cdev;
  492. cdev = to_ccwdev(dev);
  493. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  494. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  495. (cdev != d->sibling)) {
  496. cdev->private->state = DEV_STATE_NOT_OPER;
  497. return 1;
  498. }
  499. return 0;
  500. }
  501. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  502. struct ccw_device *sibling)
  503. {
  504. struct device *dev;
  505. struct match_data data;
  506. data.dev_id = *dev_id;
  507. data.sibling = sibling;
  508. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  509. return dev ? to_ccwdev(dev) : NULL;
  510. }
  511. static void
  512. ccw_device_add_changed(void *data)
  513. {
  514. struct ccw_device *cdev;
  515. cdev = data;
  516. if (device_add(&cdev->dev)) {
  517. put_device(&cdev->dev);
  518. return;
  519. }
  520. set_bit(1, &cdev->private->registered);
  521. if (device_add_files(&cdev->dev)) {
  522. if (test_and_clear_bit(1, &cdev->private->registered))
  523. device_unregister(&cdev->dev);
  524. }
  525. }
  526. extern int css_get_ssd_info(struct subchannel *sch);
  527. void
  528. ccw_device_do_unreg_rereg(void *data)
  529. {
  530. struct ccw_device *cdev;
  531. struct subchannel *sch;
  532. int need_rename;
  533. cdev = data;
  534. sch = to_subchannel(cdev->dev.parent);
  535. if (cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
  536. /*
  537. * The device number has changed. This is usually only when
  538. * a device has been detached under VM and then re-appeared
  539. * on another subchannel because of a different attachment
  540. * order than before. Ideally, we should should just switch
  541. * subchannels, but unfortunately, this is not possible with
  542. * the current implementation.
  543. * Instead, we search for the old subchannel for this device
  544. * number and deregister so there are no collisions with the
  545. * newly registered ccw_device.
  546. * FIXME: Find another solution so the block layer doesn't
  547. * get possibly sick...
  548. */
  549. struct ccw_device *other_cdev;
  550. struct ccw_dev_id dev_id;
  551. need_rename = 1;
  552. dev_id.devno = sch->schib.pmcw.dev;
  553. dev_id.ssid = sch->schid.ssid;
  554. other_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  555. if (other_cdev) {
  556. struct subchannel *other_sch;
  557. other_sch = to_subchannel(other_cdev->dev.parent);
  558. if (get_device(&other_sch->dev)) {
  559. stsch(other_sch->schid, &other_sch->schib);
  560. if (other_sch->schib.pmcw.dnv) {
  561. other_sch->schib.pmcw.intparm = 0;
  562. cio_modify(other_sch);
  563. }
  564. css_sch_device_unregister(other_sch);
  565. }
  566. }
  567. /* Update ssd info here. */
  568. css_get_ssd_info(sch);
  569. cdev->private->dev_id.devno = sch->schib.pmcw.dev;
  570. } else
  571. need_rename = 0;
  572. device_remove_files(&cdev->dev);
  573. if (test_and_clear_bit(1, &cdev->private->registered))
  574. device_del(&cdev->dev);
  575. if (need_rename)
  576. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  577. sch->schid.ssid, sch->schib.pmcw.dev);
  578. PREPARE_WORK(&cdev->private->kick_work,
  579. ccw_device_add_changed, cdev);
  580. queue_work(ccw_device_work, &cdev->private->kick_work);
  581. }
  582. static void
  583. ccw_device_release(struct device *dev)
  584. {
  585. struct ccw_device *cdev;
  586. cdev = to_ccwdev(dev);
  587. kfree(cdev->private);
  588. kfree(cdev);
  589. }
  590. /*
  591. * Register recognized device.
  592. */
  593. static void
  594. io_subchannel_register(void *data)
  595. {
  596. struct ccw_device *cdev;
  597. struct subchannel *sch;
  598. int ret;
  599. unsigned long flags;
  600. cdev = data;
  601. sch = to_subchannel(cdev->dev.parent);
  602. if (klist_node_attached(&cdev->dev.knode_parent)) {
  603. bus_rescan_devices(&ccw_bus_type);
  604. goto out;
  605. }
  606. /* make it known to the system */
  607. ret = ccw_device_register(cdev);
  608. if (ret) {
  609. printk (KERN_WARNING "%s: could not register %s\n",
  610. __func__, cdev->dev.bus_id);
  611. put_device(&cdev->dev);
  612. spin_lock_irqsave(&sch->lock, flags);
  613. sch->dev.driver_data = NULL;
  614. spin_unlock_irqrestore(&sch->lock, flags);
  615. kfree (cdev->private);
  616. kfree (cdev);
  617. put_device(&sch->dev);
  618. if (atomic_dec_and_test(&ccw_device_init_count))
  619. wake_up(&ccw_device_init_wq);
  620. return;
  621. }
  622. ret = subchannel_add_files(cdev->dev.parent);
  623. if (ret)
  624. printk(KERN_WARNING "%s: could not add attributes to %s\n",
  625. __func__, sch->dev.bus_id);
  626. put_device(&cdev->dev);
  627. out:
  628. cdev->private->flags.recog_done = 1;
  629. put_device(&sch->dev);
  630. wake_up(&cdev->private->wait_q);
  631. if (atomic_dec_and_test(&ccw_device_init_count))
  632. wake_up(&ccw_device_init_wq);
  633. }
  634. void
  635. ccw_device_call_sch_unregister(void *data)
  636. {
  637. struct ccw_device *cdev = data;
  638. struct subchannel *sch;
  639. sch = to_subchannel(cdev->dev.parent);
  640. css_sch_device_unregister(sch);
  641. /* Reset intparm to zeroes. */
  642. sch->schib.pmcw.intparm = 0;
  643. cio_modify(sch);
  644. put_device(&cdev->dev);
  645. put_device(&sch->dev);
  646. }
  647. /*
  648. * subchannel recognition done. Called from the state machine.
  649. */
  650. void
  651. io_subchannel_recog_done(struct ccw_device *cdev)
  652. {
  653. struct subchannel *sch;
  654. if (css_init_done == 0) {
  655. cdev->private->flags.recog_done = 1;
  656. return;
  657. }
  658. switch (cdev->private->state) {
  659. case DEV_STATE_NOT_OPER:
  660. cdev->private->flags.recog_done = 1;
  661. /* Remove device found not operational. */
  662. if (!get_device(&cdev->dev))
  663. break;
  664. sch = to_subchannel(cdev->dev.parent);
  665. PREPARE_WORK(&cdev->private->kick_work,
  666. ccw_device_call_sch_unregister, cdev);
  667. queue_work(slow_path_wq, &cdev->private->kick_work);
  668. if (atomic_dec_and_test(&ccw_device_init_count))
  669. wake_up(&ccw_device_init_wq);
  670. break;
  671. case DEV_STATE_BOXED:
  672. /* Device did not respond in time. */
  673. case DEV_STATE_OFFLINE:
  674. /*
  675. * We can't register the device in interrupt context so
  676. * we schedule a work item.
  677. */
  678. if (!get_device(&cdev->dev))
  679. break;
  680. PREPARE_WORK(&cdev->private->kick_work,
  681. io_subchannel_register, cdev);
  682. queue_work(slow_path_wq, &cdev->private->kick_work);
  683. break;
  684. }
  685. }
  686. static int
  687. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  688. {
  689. int rc;
  690. struct ccw_device_private *priv;
  691. sch->dev.driver_data = cdev;
  692. sch->driver = &io_subchannel_driver;
  693. cdev->ccwlock = &sch->lock;
  694. /* Init private data. */
  695. priv = cdev->private;
  696. priv->dev_id.devno = sch->schib.pmcw.dev;
  697. priv->dev_id.ssid = sch->schid.ssid;
  698. priv->schid = sch->schid;
  699. priv->state = DEV_STATE_NOT_OPER;
  700. INIT_LIST_HEAD(&priv->cmb_list);
  701. init_waitqueue_head(&priv->wait_q);
  702. init_timer(&priv->timer);
  703. /* Set an initial name for the device. */
  704. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  705. sch->schid.ssid, sch->schib.pmcw.dev);
  706. /* Increase counter of devices currently in recognition. */
  707. atomic_inc(&ccw_device_init_count);
  708. /* Start async. device sensing. */
  709. spin_lock_irq(&sch->lock);
  710. rc = ccw_device_recognition(cdev);
  711. spin_unlock_irq(&sch->lock);
  712. if (rc) {
  713. if (atomic_dec_and_test(&ccw_device_init_count))
  714. wake_up(&ccw_device_init_wq);
  715. }
  716. return rc;
  717. }
  718. static int
  719. io_subchannel_probe (struct subchannel *sch)
  720. {
  721. struct ccw_device *cdev;
  722. int rc;
  723. unsigned long flags;
  724. if (sch->dev.driver_data) {
  725. /*
  726. * This subchannel already has an associated ccw_device.
  727. * Register it and exit. This happens for all early
  728. * device, e.g. the console.
  729. */
  730. cdev = sch->dev.driver_data;
  731. device_initialize(&cdev->dev);
  732. ccw_device_register(cdev);
  733. subchannel_add_files(&sch->dev);
  734. /*
  735. * Check if the device is already online. If it is
  736. * the reference count needs to be corrected
  737. * (see ccw_device_online and css_init_done for the
  738. * ugly details).
  739. */
  740. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  741. cdev->private->state != DEV_STATE_OFFLINE &&
  742. cdev->private->state != DEV_STATE_BOXED)
  743. get_device(&cdev->dev);
  744. return 0;
  745. }
  746. cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
  747. if (!cdev)
  748. return -ENOMEM;
  749. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  750. GFP_KERNEL | GFP_DMA);
  751. if (!cdev->private) {
  752. kfree(cdev);
  753. return -ENOMEM;
  754. }
  755. atomic_set(&cdev->private->onoff, 0);
  756. cdev->dev.parent = &sch->dev;
  757. cdev->dev.release = ccw_device_release;
  758. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  759. /* Do first half of device_register. */
  760. device_initialize(&cdev->dev);
  761. if (!get_device(&sch->dev)) {
  762. if (cdev->dev.release)
  763. cdev->dev.release(&cdev->dev);
  764. return -ENODEV;
  765. }
  766. rc = io_subchannel_recog(cdev, sch);
  767. if (rc) {
  768. spin_lock_irqsave(&sch->lock, flags);
  769. sch->dev.driver_data = NULL;
  770. spin_unlock_irqrestore(&sch->lock, flags);
  771. if (cdev->dev.release)
  772. cdev->dev.release(&cdev->dev);
  773. }
  774. return rc;
  775. }
  776. static void
  777. ccw_device_unregister(void *data)
  778. {
  779. struct ccw_device *cdev;
  780. cdev = (struct ccw_device *)data;
  781. if (test_and_clear_bit(1, &cdev->private->registered))
  782. device_unregister(&cdev->dev);
  783. put_device(&cdev->dev);
  784. }
  785. static int
  786. io_subchannel_remove (struct subchannel *sch)
  787. {
  788. struct ccw_device *cdev;
  789. unsigned long flags;
  790. if (!sch->dev.driver_data)
  791. return 0;
  792. cdev = sch->dev.driver_data;
  793. /* Set ccw device to not operational and drop reference. */
  794. spin_lock_irqsave(cdev->ccwlock, flags);
  795. sch->dev.driver_data = NULL;
  796. cdev->private->state = DEV_STATE_NOT_OPER;
  797. spin_unlock_irqrestore(cdev->ccwlock, flags);
  798. /*
  799. * Put unregistration on workqueue to avoid livelocks on the css bus
  800. * semaphore.
  801. */
  802. if (get_device(&cdev->dev)) {
  803. PREPARE_WORK(&cdev->private->kick_work,
  804. ccw_device_unregister, cdev);
  805. queue_work(ccw_device_work, &cdev->private->kick_work);
  806. }
  807. return 0;
  808. }
  809. static int
  810. io_subchannel_notify(struct device *dev, int event)
  811. {
  812. struct ccw_device *cdev;
  813. cdev = dev->driver_data;
  814. if (!cdev)
  815. return 0;
  816. if (!cdev->drv)
  817. return 0;
  818. if (!cdev->online)
  819. return 0;
  820. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  821. }
  822. static void
  823. io_subchannel_verify(struct device *dev)
  824. {
  825. struct ccw_device *cdev;
  826. cdev = dev->driver_data;
  827. if (cdev)
  828. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  829. }
  830. static void
  831. io_subchannel_ioterm(struct device *dev)
  832. {
  833. struct ccw_device *cdev;
  834. cdev = dev->driver_data;
  835. if (!cdev)
  836. return;
  837. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  838. if (cdev->handler)
  839. cdev->handler(cdev, cdev->private->intparm,
  840. ERR_PTR(-EIO));
  841. }
  842. static void
  843. io_subchannel_shutdown(struct subchannel *sch)
  844. {
  845. struct ccw_device *cdev;
  846. int ret;
  847. cdev = sch->dev.driver_data;
  848. if (cio_is_console(sch->schid))
  849. return;
  850. if (!sch->schib.pmcw.ena)
  851. /* Nothing to do. */
  852. return;
  853. ret = cio_disable_subchannel(sch);
  854. if (ret != -EBUSY)
  855. /* Subchannel is disabled, we're done. */
  856. return;
  857. cdev->private->state = DEV_STATE_QUIESCE;
  858. if (cdev->handler)
  859. cdev->handler(cdev, cdev->private->intparm,
  860. ERR_PTR(-EIO));
  861. ret = ccw_device_cancel_halt_clear(cdev);
  862. if (ret == -EBUSY) {
  863. ccw_device_set_timeout(cdev, HZ/10);
  864. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  865. }
  866. cio_disable_subchannel(sch);
  867. }
  868. #ifdef CONFIG_CCW_CONSOLE
  869. static struct ccw_device console_cdev;
  870. static struct ccw_device_private console_private;
  871. static int console_cdev_in_use;
  872. static int
  873. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  874. {
  875. int rc;
  876. /* Initialize the ccw_device structure. */
  877. cdev->dev.parent= &sch->dev;
  878. rc = io_subchannel_recog(cdev, sch);
  879. if (rc)
  880. return rc;
  881. /* Now wait for the async. recognition to come to an end. */
  882. spin_lock_irq(cdev->ccwlock);
  883. while (!dev_fsm_final_state(cdev))
  884. wait_cons_dev();
  885. rc = -EIO;
  886. if (cdev->private->state != DEV_STATE_OFFLINE)
  887. goto out_unlock;
  888. ccw_device_online(cdev);
  889. while (!dev_fsm_final_state(cdev))
  890. wait_cons_dev();
  891. if (cdev->private->state != DEV_STATE_ONLINE)
  892. goto out_unlock;
  893. rc = 0;
  894. out_unlock:
  895. spin_unlock_irq(cdev->ccwlock);
  896. return 0;
  897. }
  898. struct ccw_device *
  899. ccw_device_probe_console(void)
  900. {
  901. struct subchannel *sch;
  902. int ret;
  903. if (xchg(&console_cdev_in_use, 1) != 0)
  904. return ERR_PTR(-EBUSY);
  905. sch = cio_probe_console();
  906. if (IS_ERR(sch)) {
  907. console_cdev_in_use = 0;
  908. return (void *) sch;
  909. }
  910. memset(&console_cdev, 0, sizeof(struct ccw_device));
  911. memset(&console_private, 0, sizeof(struct ccw_device_private));
  912. console_cdev.private = &console_private;
  913. ret = ccw_device_console_enable(&console_cdev, sch);
  914. if (ret) {
  915. cio_release_console();
  916. console_cdev_in_use = 0;
  917. return ERR_PTR(ret);
  918. }
  919. console_cdev.online = 1;
  920. return &console_cdev;
  921. }
  922. #endif
  923. /*
  924. * get ccw_device matching the busid, but only if owned by cdrv
  925. */
  926. static int
  927. __ccwdev_check_busid(struct device *dev, void *id)
  928. {
  929. char *bus_id;
  930. bus_id = id;
  931. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  932. }
  933. struct ccw_device *
  934. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  935. {
  936. struct device *dev;
  937. struct device_driver *drv;
  938. drv = get_driver(&cdrv->driver);
  939. if (!drv)
  940. return NULL;
  941. dev = driver_find_device(drv, NULL, (void *)bus_id,
  942. __ccwdev_check_busid);
  943. put_driver(drv);
  944. return dev ? to_ccwdev(dev) : NULL;
  945. }
  946. /************************** device driver handling ************************/
  947. /* This is the implementation of the ccw_driver class. The probe, remove
  948. * and release methods are initially very similar to the device_driver
  949. * implementations, with the difference that they have ccw_device
  950. * arguments.
  951. *
  952. * A ccw driver also contains the information that is needed for
  953. * device matching.
  954. */
  955. static int
  956. ccw_device_probe (struct device *dev)
  957. {
  958. struct ccw_device *cdev = to_ccwdev(dev);
  959. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  960. int ret;
  961. cdev->drv = cdrv; /* to let the driver call _set_online */
  962. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  963. if (ret) {
  964. cdev->drv = NULL;
  965. return ret;
  966. }
  967. return 0;
  968. }
  969. static int
  970. ccw_device_remove (struct device *dev)
  971. {
  972. struct ccw_device *cdev = to_ccwdev(dev);
  973. struct ccw_driver *cdrv = cdev->drv;
  974. int ret;
  975. pr_debug("removing device %s\n", cdev->dev.bus_id);
  976. if (cdrv->remove)
  977. cdrv->remove(cdev);
  978. if (cdev->online) {
  979. cdev->online = 0;
  980. spin_lock_irq(cdev->ccwlock);
  981. ret = ccw_device_offline(cdev);
  982. spin_unlock_irq(cdev->ccwlock);
  983. if (ret == 0)
  984. wait_event(cdev->private->wait_q,
  985. dev_fsm_final_state(cdev));
  986. else
  987. //FIXME: we can't fail!
  988. pr_debug("ccw_device_offline returned %d, device %s\n",
  989. ret, cdev->dev.bus_id);
  990. }
  991. ccw_device_set_timeout(cdev, 0);
  992. cdev->drv = NULL;
  993. return 0;
  994. }
  995. struct bus_type ccw_bus_type = {
  996. .name = "ccw",
  997. .match = ccw_bus_match,
  998. .uevent = ccw_uevent,
  999. .probe = ccw_device_probe,
  1000. .remove = ccw_device_remove,
  1001. };
  1002. int
  1003. ccw_driver_register (struct ccw_driver *cdriver)
  1004. {
  1005. struct device_driver *drv = &cdriver->driver;
  1006. drv->bus = &ccw_bus_type;
  1007. drv->name = cdriver->name;
  1008. return driver_register(drv);
  1009. }
  1010. void
  1011. ccw_driver_unregister (struct ccw_driver *cdriver)
  1012. {
  1013. driver_unregister(&cdriver->driver);
  1014. }
  1015. /* Helper func for qdio. */
  1016. struct subchannel_id
  1017. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1018. {
  1019. struct subchannel *sch;
  1020. sch = to_subchannel(cdev->dev.parent);
  1021. return sch->schid;
  1022. }
  1023. MODULE_LICENSE("GPL");
  1024. EXPORT_SYMBOL(ccw_device_set_online);
  1025. EXPORT_SYMBOL(ccw_device_set_offline);
  1026. EXPORT_SYMBOL(ccw_driver_register);
  1027. EXPORT_SYMBOL(ccw_driver_unregister);
  1028. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1029. EXPORT_SYMBOL(ccw_bus_type);
  1030. EXPORT_SYMBOL(ccw_device_work);
  1031. EXPORT_SYMBOL(ccw_device_notify_work);
  1032. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);