device.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/workqueue.h>
  21. #include <asm/ccwdev.h>
  22. #include <asm/cio.h>
  23. #include <asm/param.h> /* HZ */
  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_uevent (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. static int io_subchannel_probe (struct subchannel *);
  93. static int io_subchannel_remove (struct subchannel *);
  94. void io_subchannel_irq (struct device *);
  95. static int io_subchannel_notify(struct device *, int);
  96. static void io_subchannel_verify(struct device *);
  97. static void io_subchannel_ioterm(struct device *);
  98. static void io_subchannel_shutdown(struct subchannel *);
  99. struct css_driver io_subchannel_driver = {
  100. .subchannel_type = SUBCHANNEL_TYPE_IO,
  101. .drv = {
  102. .name = "io_subchannel",
  103. .bus = &css_bus_type,
  104. },
  105. .irq = io_subchannel_irq,
  106. .notify = io_subchannel_notify,
  107. .verify = io_subchannel_verify,
  108. .termination = io_subchannel_ioterm,
  109. .probe = io_subchannel_probe,
  110. .remove = io_subchannel_remove,
  111. .shutdown = io_subchannel_shutdown,
  112. };
  113. struct workqueue_struct *ccw_device_work;
  114. struct workqueue_struct *ccw_device_notify_work;
  115. static wait_queue_head_t ccw_device_init_wq;
  116. static atomic_t ccw_device_init_count;
  117. static int __init
  118. init_ccw_bus_type (void)
  119. {
  120. int ret;
  121. init_waitqueue_head(&ccw_device_init_wq);
  122. atomic_set(&ccw_device_init_count, 0);
  123. ccw_device_work = create_singlethread_workqueue("cio");
  124. if (!ccw_device_work)
  125. return -ENOMEM; /* FIXME: better errno ? */
  126. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  127. if (!ccw_device_notify_work) {
  128. ret = -ENOMEM; /* FIXME: better errno ? */
  129. goto out_err;
  130. }
  131. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  132. if (!slow_path_wq) {
  133. ret = -ENOMEM; /* FIXME: better errno ? */
  134. goto out_err;
  135. }
  136. if ((ret = bus_register (&ccw_bus_type)))
  137. goto out_err;
  138. if ((ret = driver_register(&io_subchannel_driver.drv)))
  139. goto out_err;
  140. wait_event(ccw_device_init_wq,
  141. atomic_read(&ccw_device_init_count) == 0);
  142. flush_workqueue(ccw_device_work);
  143. return 0;
  144. out_err:
  145. if (ccw_device_work)
  146. destroy_workqueue(ccw_device_work);
  147. if (ccw_device_notify_work)
  148. destroy_workqueue(ccw_device_notify_work);
  149. if (slow_path_wq)
  150. destroy_workqueue(slow_path_wq);
  151. return ret;
  152. }
  153. static void __exit
  154. cleanup_ccw_bus_type (void)
  155. {
  156. driver_unregister(&io_subchannel_driver.drv);
  157. bus_unregister(&ccw_bus_type);
  158. destroy_workqueue(ccw_device_notify_work);
  159. destroy_workqueue(ccw_device_work);
  160. }
  161. subsys_initcall(init_ccw_bus_type);
  162. module_exit(cleanup_ccw_bus_type);
  163. /************************ device handling **************************/
  164. /*
  165. * A ccw_device has some interfaces in sysfs in addition to the
  166. * standard ones.
  167. * The following entries are designed to export the information which
  168. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  169. * are obvious, so they don't have an entry :)
  170. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  171. */
  172. static ssize_t
  173. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  174. {
  175. struct subchannel *sch = to_subchannel(dev);
  176. struct ssd_info *ssd = &sch->ssd_info;
  177. ssize_t ret = 0;
  178. int chp;
  179. for (chp = 0; chp < 8; chp++)
  180. ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
  181. ret += sprintf (buf+ret, "\n");
  182. return min((ssize_t)PAGE_SIZE, ret);
  183. }
  184. static ssize_t
  185. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  186. {
  187. struct subchannel *sch = to_subchannel(dev);
  188. struct pmcw *pmcw = &sch->schib.pmcw;
  189. return sprintf (buf, "%02x %02x %02x\n",
  190. pmcw->pim, pmcw->pam, pmcw->pom);
  191. }
  192. static ssize_t
  193. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. struct ccw_device *cdev = to_ccwdev(dev);
  196. struct ccw_device_id *id = &(cdev->id);
  197. if (id->dev_type != 0)
  198. return sprintf(buf, "%04x/%02x\n",
  199. id->dev_type, id->dev_model);
  200. else
  201. return sprintf(buf, "n/a\n");
  202. }
  203. static ssize_t
  204. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  205. {
  206. struct ccw_device *cdev = to_ccwdev(dev);
  207. struct ccw_device_id *id = &(cdev->id);
  208. return sprintf(buf, "%04x/%02x\n",
  209. id->cu_type, id->cu_model);
  210. }
  211. static ssize_t
  212. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  213. {
  214. struct ccw_device *cdev = to_ccwdev(dev);
  215. struct ccw_device_id *id = &(cdev->id);
  216. int ret;
  217. ret = sprintf(buf, "ccw:t%04Xm%02X",
  218. id->cu_type, id->cu_model);
  219. if (id->dev_type != 0)
  220. ret += sprintf(buf + ret, "dt%04Xdm%02X\n",
  221. id->dev_type, id->dev_model);
  222. else
  223. ret += sprintf(buf + ret, "dtdm\n");
  224. return ret;
  225. }
  226. static ssize_t
  227. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  228. {
  229. struct ccw_device *cdev = to_ccwdev(dev);
  230. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  231. }
  232. static void
  233. ccw_device_remove_disconnected(struct ccw_device *cdev)
  234. {
  235. struct subchannel *sch;
  236. /*
  237. * Forced offline in disconnected state means
  238. * 'throw away device'.
  239. */
  240. sch = to_subchannel(cdev->dev.parent);
  241. device_unregister(&sch->dev);
  242. /* Reset intparm to zeroes. */
  243. sch->schib.pmcw.intparm = 0;
  244. cio_modify(sch);
  245. put_device(&sch->dev);
  246. }
  247. int
  248. ccw_device_set_offline(struct ccw_device *cdev)
  249. {
  250. int ret;
  251. if (!cdev)
  252. return -ENODEV;
  253. if (!cdev->online || !cdev->drv)
  254. return -EINVAL;
  255. if (cdev->drv->set_offline) {
  256. ret = cdev->drv->set_offline(cdev);
  257. if (ret != 0)
  258. return ret;
  259. }
  260. cdev->online = 0;
  261. spin_lock_irq(cdev->ccwlock);
  262. ret = ccw_device_offline(cdev);
  263. if (ret == -ENODEV) {
  264. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  265. cdev->private->state = DEV_STATE_OFFLINE;
  266. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  267. }
  268. spin_unlock_irq(cdev->ccwlock);
  269. return ret;
  270. }
  271. spin_unlock_irq(cdev->ccwlock);
  272. if (ret == 0)
  273. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  274. else {
  275. pr_debug("ccw_device_offline returned %d, device %s\n",
  276. ret, cdev->dev.bus_id);
  277. cdev->online = 1;
  278. }
  279. return ret;
  280. }
  281. int
  282. ccw_device_set_online(struct ccw_device *cdev)
  283. {
  284. int ret;
  285. if (!cdev)
  286. return -ENODEV;
  287. if (cdev->online || !cdev->drv)
  288. return -EINVAL;
  289. spin_lock_irq(cdev->ccwlock);
  290. ret = ccw_device_online(cdev);
  291. spin_unlock_irq(cdev->ccwlock);
  292. if (ret == 0)
  293. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  294. else {
  295. pr_debug("ccw_device_online returned %d, device %s\n",
  296. ret, cdev->dev.bus_id);
  297. return ret;
  298. }
  299. if (cdev->private->state != DEV_STATE_ONLINE)
  300. return -ENODEV;
  301. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  302. cdev->online = 1;
  303. return 0;
  304. }
  305. spin_lock_irq(cdev->ccwlock);
  306. ret = ccw_device_offline(cdev);
  307. spin_unlock_irq(cdev->ccwlock);
  308. if (ret == 0)
  309. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  310. else
  311. pr_debug("ccw_device_offline returned %d, device %s\n",
  312. ret, cdev->dev.bus_id);
  313. return (ret == 0) ? -ENODEV : ret;
  314. }
  315. static ssize_t
  316. online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  317. {
  318. struct ccw_device *cdev = to_ccwdev(dev);
  319. int i, force, ret;
  320. char *tmp;
  321. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  322. return -EAGAIN;
  323. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  324. atomic_set(&cdev->private->onoff, 0);
  325. return -EINVAL;
  326. }
  327. if (!strncmp(buf, "force\n", count)) {
  328. force = 1;
  329. i = 1;
  330. } else {
  331. force = 0;
  332. i = simple_strtoul(buf, &tmp, 16);
  333. }
  334. if (i == 1) {
  335. /* Do device recognition, if needed. */
  336. if (cdev->id.cu_type == 0) {
  337. ret = ccw_device_recognition(cdev);
  338. if (ret) {
  339. printk(KERN_WARNING"Couldn't start recognition "
  340. "for device %s (ret=%d)\n",
  341. cdev->dev.bus_id, ret);
  342. goto out;
  343. }
  344. wait_event(cdev->private->wait_q,
  345. cdev->private->flags.recog_done);
  346. }
  347. if (cdev->drv && cdev->drv->set_online)
  348. ccw_device_set_online(cdev);
  349. } else if (i == 0) {
  350. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  351. ccw_device_remove_disconnected(cdev);
  352. else if (cdev->drv && cdev->drv->set_offline)
  353. ccw_device_set_offline(cdev);
  354. }
  355. if (force && cdev->private->state == DEV_STATE_BOXED) {
  356. ret = ccw_device_stlck(cdev);
  357. if (ret) {
  358. printk(KERN_WARNING"ccw_device_stlck for device %s "
  359. "returned %d!\n", cdev->dev.bus_id, ret);
  360. goto out;
  361. }
  362. /* Do device recognition, if needed. */
  363. if (cdev->id.cu_type == 0) {
  364. cdev->private->state = DEV_STATE_NOT_OPER;
  365. ret = ccw_device_recognition(cdev);
  366. if (ret) {
  367. printk(KERN_WARNING"Couldn't start recognition "
  368. "for device %s (ret=%d)\n",
  369. cdev->dev.bus_id, ret);
  370. goto out;
  371. }
  372. wait_event(cdev->private->wait_q,
  373. cdev->private->flags.recog_done);
  374. }
  375. if (cdev->drv && cdev->drv->set_online)
  376. ccw_device_set_online(cdev);
  377. }
  378. out:
  379. if (cdev->drv)
  380. module_put(cdev->drv->owner);
  381. atomic_set(&cdev->private->onoff, 0);
  382. return count;
  383. }
  384. static ssize_t
  385. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  386. {
  387. struct ccw_device *cdev = to_ccwdev(dev);
  388. struct subchannel *sch;
  389. switch (cdev->private->state) {
  390. case DEV_STATE_BOXED:
  391. return sprintf(buf, "boxed\n");
  392. case DEV_STATE_DISCONNECTED:
  393. case DEV_STATE_DISCONNECTED_SENSE_ID:
  394. case DEV_STATE_NOT_OPER:
  395. sch = to_subchannel(dev->parent);
  396. if (!sch->lpm)
  397. return sprintf(buf, "no path\n");
  398. else
  399. return sprintf(buf, "no device\n");
  400. default:
  401. /* All other states considered fine. */
  402. return sprintf(buf, "good\n");
  403. }
  404. }
  405. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  406. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  407. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  408. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  409. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  410. static DEVICE_ATTR(online, 0644, online_show, online_store);
  411. extern struct device_attribute dev_attr_cmb_enable;
  412. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  413. static struct attribute * subch_attrs[] = {
  414. &dev_attr_chpids.attr,
  415. &dev_attr_pimpampom.attr,
  416. NULL,
  417. };
  418. static struct attribute_group subch_attr_group = {
  419. .attrs = subch_attrs,
  420. };
  421. static inline int
  422. subchannel_add_files (struct device *dev)
  423. {
  424. return sysfs_create_group(&dev->kobj, &subch_attr_group);
  425. }
  426. static struct attribute * ccwdev_attrs[] = {
  427. &dev_attr_devtype.attr,
  428. &dev_attr_cutype.attr,
  429. &dev_attr_modalias.attr,
  430. &dev_attr_online.attr,
  431. &dev_attr_cmb_enable.attr,
  432. &dev_attr_availability.attr,
  433. NULL,
  434. };
  435. static struct attribute_group ccwdev_attr_group = {
  436. .attrs = ccwdev_attrs,
  437. };
  438. static inline int
  439. device_add_files (struct device *dev)
  440. {
  441. return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
  442. }
  443. static inline void
  444. device_remove_files(struct device *dev)
  445. {
  446. sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
  447. }
  448. /* this is a simple abstraction for device_register that sets the
  449. * correct bus type and adds the bus specific files */
  450. int
  451. ccw_device_register(struct ccw_device *cdev)
  452. {
  453. struct device *dev = &cdev->dev;
  454. int ret;
  455. dev->bus = &ccw_bus_type;
  456. if ((ret = device_add(dev)))
  457. return ret;
  458. set_bit(1, &cdev->private->registered);
  459. if ((ret = device_add_files(dev))) {
  460. if (test_and_clear_bit(1, &cdev->private->registered))
  461. device_del(dev);
  462. }
  463. return ret;
  464. }
  465. struct match_data {
  466. unsigned int devno;
  467. unsigned int ssid;
  468. struct ccw_device * sibling;
  469. };
  470. static int
  471. match_devno(struct device * dev, void * data)
  472. {
  473. struct match_data * d = (struct match_data *)data;
  474. struct ccw_device * cdev;
  475. cdev = to_ccwdev(dev);
  476. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  477. (cdev->private->devno == d->devno) &&
  478. (cdev->private->ssid == d->ssid) &&
  479. (cdev != d->sibling)) {
  480. cdev->private->state = DEV_STATE_NOT_OPER;
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. static struct ccw_device *
  486. get_disc_ccwdev_by_devno(unsigned int devno, unsigned int ssid,
  487. struct ccw_device *sibling)
  488. {
  489. struct device *dev;
  490. struct match_data data = {
  491. .devno = devno,
  492. .ssid = ssid,
  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. sch->schid.ssid, 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->schid, &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.%x.%04x",
  562. sch->schid.ssid, 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->ssid = sch->schid.ssid;
  683. priv->sch_no = sch->schid.sch_no;
  684. priv->state = DEV_STATE_NOT_OPER;
  685. INIT_LIST_HEAD(&priv->cmb_list);
  686. init_waitqueue_head(&priv->wait_q);
  687. init_timer(&priv->timer);
  688. /* Set an initial name for the device. */
  689. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  690. sch->schid.ssid, sch->schib.pmcw.dev);
  691. /* Increase counter of devices currently in recognition. */
  692. atomic_inc(&ccw_device_init_count);
  693. /* Start async. device sensing. */
  694. spin_lock_irq(&sch->lock);
  695. rc = ccw_device_recognition(cdev);
  696. spin_unlock_irq(&sch->lock);
  697. if (rc) {
  698. if (atomic_dec_and_test(&ccw_device_init_count))
  699. wake_up(&ccw_device_init_wq);
  700. }
  701. return rc;
  702. }
  703. static int
  704. io_subchannel_probe (struct subchannel *sch)
  705. {
  706. struct ccw_device *cdev;
  707. int rc;
  708. unsigned long flags;
  709. if (sch->dev.driver_data) {
  710. /*
  711. * This subchannel already has an associated ccw_device.
  712. * Register it and exit. This happens for all early
  713. * device, e.g. the console.
  714. */
  715. cdev = sch->dev.driver_data;
  716. device_initialize(&cdev->dev);
  717. ccw_device_register(cdev);
  718. subchannel_add_files(&sch->dev);
  719. /*
  720. * Check if the device is already online. If it is
  721. * the reference count needs to be corrected
  722. * (see ccw_device_online and css_init_done for the
  723. * ugly details).
  724. */
  725. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  726. cdev->private->state != DEV_STATE_OFFLINE &&
  727. cdev->private->state != DEV_STATE_BOXED)
  728. get_device(&cdev->dev);
  729. return 0;
  730. }
  731. cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
  732. if (!cdev)
  733. return -ENOMEM;
  734. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  735. GFP_KERNEL | GFP_DMA);
  736. if (!cdev->private) {
  737. kfree(cdev);
  738. return -ENOMEM;
  739. }
  740. atomic_set(&cdev->private->onoff, 0);
  741. cdev->dev = (struct device) {
  742. .parent = &sch->dev,
  743. .release = ccw_device_release,
  744. };
  745. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  746. /* Do first half of device_register. */
  747. device_initialize(&cdev->dev);
  748. if (!get_device(&sch->dev)) {
  749. if (cdev->dev.release)
  750. cdev->dev.release(&cdev->dev);
  751. return -ENODEV;
  752. }
  753. rc = io_subchannel_recog(cdev, sch);
  754. if (rc) {
  755. spin_lock_irqsave(&sch->lock, flags);
  756. sch->dev.driver_data = NULL;
  757. spin_unlock_irqrestore(&sch->lock, flags);
  758. if (cdev->dev.release)
  759. cdev->dev.release(&cdev->dev);
  760. }
  761. return rc;
  762. }
  763. static void
  764. ccw_device_unregister(void *data)
  765. {
  766. struct ccw_device *cdev;
  767. cdev = (struct ccw_device *)data;
  768. if (test_and_clear_bit(1, &cdev->private->registered))
  769. device_unregister(&cdev->dev);
  770. put_device(&cdev->dev);
  771. }
  772. static int
  773. io_subchannel_remove (struct subchannel *sch)
  774. {
  775. struct ccw_device *cdev;
  776. unsigned long flags;
  777. if (!sch->dev.driver_data)
  778. return 0;
  779. cdev = sch->dev.driver_data;
  780. /* Set ccw device to not operational and drop reference. */
  781. spin_lock_irqsave(cdev->ccwlock, flags);
  782. sch->dev.driver_data = NULL;
  783. cdev->private->state = DEV_STATE_NOT_OPER;
  784. spin_unlock_irqrestore(cdev->ccwlock, flags);
  785. /*
  786. * Put unregistration on workqueue to avoid livelocks on the css bus
  787. * semaphore.
  788. */
  789. if (get_device(&cdev->dev)) {
  790. PREPARE_WORK(&cdev->private->kick_work,
  791. ccw_device_unregister, (void *) cdev);
  792. queue_work(ccw_device_work, &cdev->private->kick_work);
  793. }
  794. return 0;
  795. }
  796. static int
  797. io_subchannel_notify(struct device *dev, int event)
  798. {
  799. struct ccw_device *cdev;
  800. cdev = dev->driver_data;
  801. if (!cdev)
  802. return 0;
  803. if (!cdev->drv)
  804. return 0;
  805. if (!cdev->online)
  806. return 0;
  807. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  808. }
  809. static void
  810. io_subchannel_verify(struct device *dev)
  811. {
  812. struct ccw_device *cdev;
  813. cdev = dev->driver_data;
  814. if (cdev)
  815. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  816. }
  817. static void
  818. io_subchannel_ioterm(struct device *dev)
  819. {
  820. struct ccw_device *cdev;
  821. cdev = dev->driver_data;
  822. if (!cdev)
  823. return;
  824. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  825. if (cdev->handler)
  826. cdev->handler(cdev, cdev->private->intparm,
  827. ERR_PTR(-EIO));
  828. }
  829. static void
  830. io_subchannel_shutdown(struct subchannel *sch)
  831. {
  832. struct ccw_device *cdev;
  833. int ret;
  834. cdev = sch->dev.driver_data;
  835. if (cio_is_console(sch->schid))
  836. return;
  837. if (!sch->schib.pmcw.ena)
  838. /* Nothing to do. */
  839. return;
  840. ret = cio_disable_subchannel(sch);
  841. if (ret != -EBUSY)
  842. /* Subchannel is disabled, we're done. */
  843. return;
  844. cdev->private->state = DEV_STATE_QUIESCE;
  845. if (cdev->handler)
  846. cdev->handler(cdev, cdev->private->intparm,
  847. ERR_PTR(-EIO));
  848. ret = ccw_device_cancel_halt_clear(cdev);
  849. if (ret == -EBUSY) {
  850. ccw_device_set_timeout(cdev, HZ/10);
  851. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  852. }
  853. cio_disable_subchannel(sch);
  854. }
  855. #ifdef CONFIG_CCW_CONSOLE
  856. static struct ccw_device console_cdev;
  857. static struct ccw_device_private console_private;
  858. static int console_cdev_in_use;
  859. static int
  860. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  861. {
  862. int rc;
  863. /* Initialize the ccw_device structure. */
  864. cdev->dev = (struct device) {
  865. .parent = &sch->dev,
  866. };
  867. rc = io_subchannel_recog(cdev, sch);
  868. if (rc)
  869. return rc;
  870. /* Now wait for the async. recognition to come to an end. */
  871. spin_lock_irq(cdev->ccwlock);
  872. while (!dev_fsm_final_state(cdev))
  873. wait_cons_dev();
  874. rc = -EIO;
  875. if (cdev->private->state != DEV_STATE_OFFLINE)
  876. goto out_unlock;
  877. ccw_device_online(cdev);
  878. while (!dev_fsm_final_state(cdev))
  879. wait_cons_dev();
  880. if (cdev->private->state != DEV_STATE_ONLINE)
  881. goto out_unlock;
  882. rc = 0;
  883. out_unlock:
  884. spin_unlock_irq(cdev->ccwlock);
  885. return 0;
  886. }
  887. struct ccw_device *
  888. ccw_device_probe_console(void)
  889. {
  890. struct subchannel *sch;
  891. int ret;
  892. if (xchg(&console_cdev_in_use, 1) != 0)
  893. return ERR_PTR(-EBUSY);
  894. sch = cio_probe_console();
  895. if (IS_ERR(sch)) {
  896. console_cdev_in_use = 0;
  897. return (void *) sch;
  898. }
  899. memset(&console_cdev, 0, sizeof(struct ccw_device));
  900. memset(&console_private, 0, sizeof(struct ccw_device_private));
  901. console_cdev.private = &console_private;
  902. ret = ccw_device_console_enable(&console_cdev, sch);
  903. if (ret) {
  904. cio_release_console();
  905. console_cdev_in_use = 0;
  906. return ERR_PTR(ret);
  907. }
  908. console_cdev.online = 1;
  909. return &console_cdev;
  910. }
  911. #endif
  912. /*
  913. * get ccw_device matching the busid, but only if owned by cdrv
  914. */
  915. static int
  916. __ccwdev_check_busid(struct device *dev, void *id)
  917. {
  918. char *bus_id;
  919. bus_id = (char *)id;
  920. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  921. }
  922. struct ccw_device *
  923. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  924. {
  925. struct device *dev;
  926. struct device_driver *drv;
  927. drv = get_driver(&cdrv->driver);
  928. if (!drv)
  929. return NULL;
  930. dev = driver_find_device(drv, NULL, (void *)bus_id,
  931. __ccwdev_check_busid);
  932. put_driver(drv);
  933. return dev ? to_ccwdev(dev) : 0;
  934. }
  935. /************************** device driver handling ************************/
  936. /* This is the implementation of the ccw_driver class. The probe, remove
  937. * and release methods are initially very similar to the device_driver
  938. * implementations, with the difference that they have ccw_device
  939. * arguments.
  940. *
  941. * A ccw driver also contains the information that is needed for
  942. * device matching.
  943. */
  944. static int
  945. ccw_device_probe (struct device *dev)
  946. {
  947. struct ccw_device *cdev = to_ccwdev(dev);
  948. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  949. int ret;
  950. cdev->drv = cdrv; /* to let the driver call _set_online */
  951. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  952. if (ret) {
  953. cdev->drv = 0;
  954. return ret;
  955. }
  956. return 0;
  957. }
  958. static int
  959. ccw_device_remove (struct device *dev)
  960. {
  961. struct ccw_device *cdev = to_ccwdev(dev);
  962. struct ccw_driver *cdrv = cdev->drv;
  963. int ret;
  964. pr_debug("removing device %s\n", cdev->dev.bus_id);
  965. if (cdrv->remove)
  966. cdrv->remove(cdev);
  967. if (cdev->online) {
  968. cdev->online = 0;
  969. spin_lock_irq(cdev->ccwlock);
  970. ret = ccw_device_offline(cdev);
  971. spin_unlock_irq(cdev->ccwlock);
  972. if (ret == 0)
  973. wait_event(cdev->private->wait_q,
  974. dev_fsm_final_state(cdev));
  975. else
  976. //FIXME: we can't fail!
  977. pr_debug("ccw_device_offline returned %d, device %s\n",
  978. ret, cdev->dev.bus_id);
  979. }
  980. ccw_device_set_timeout(cdev, 0);
  981. cdev->drv = 0;
  982. return 0;
  983. }
  984. struct bus_type ccw_bus_type = {
  985. .name = "ccw",
  986. .match = ccw_bus_match,
  987. .uevent = ccw_uevent,
  988. .probe = ccw_device_probe,
  989. .remove = ccw_device_remove,
  990. };
  991. int
  992. ccw_driver_register (struct ccw_driver *cdriver)
  993. {
  994. struct device_driver *drv = &cdriver->driver;
  995. drv->bus = &ccw_bus_type;
  996. drv->name = cdriver->name;
  997. return driver_register(drv);
  998. }
  999. void
  1000. ccw_driver_unregister (struct ccw_driver *cdriver)
  1001. {
  1002. driver_unregister(&cdriver->driver);
  1003. }
  1004. /* Helper func for qdio. */
  1005. struct subchannel_id
  1006. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1007. {
  1008. struct subchannel *sch;
  1009. sch = to_subchannel(cdev->dev.parent);
  1010. return sch->schid;
  1011. }
  1012. MODULE_LICENSE("GPL");
  1013. EXPORT_SYMBOL(ccw_device_set_online);
  1014. EXPORT_SYMBOL(ccw_device_set_offline);
  1015. EXPORT_SYMBOL(ccw_driver_register);
  1016. EXPORT_SYMBOL(ccw_driver_unregister);
  1017. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1018. EXPORT_SYMBOL(ccw_bus_type);
  1019. EXPORT_SYMBOL(ccw_device_work);
  1020. EXPORT_SYMBOL(ccw_device_notify_work);
  1021. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);