device.c 28 KB

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