device.c 30 KB

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