device.c 28 KB

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