device.c 28 KB

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