device.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 "cio_debug.h"
  25. #include "css.h"
  26. #include "device.h"
  27. #include "ioasm.h"
  28. /******************* bus type handling ***********************/
  29. /* The Linux driver model distinguishes between a bus type and
  30. * the bus itself. Of course we only have one channel
  31. * subsystem driver and one channel system per machine, but
  32. * we still use the abstraction. T.R. says it's a good idea. */
  33. static int
  34. ccw_bus_match (struct device * dev, struct device_driver * drv)
  35. {
  36. struct ccw_device *cdev = to_ccwdev(dev);
  37. struct ccw_driver *cdrv = to_ccwdrv(drv);
  38. const struct ccw_device_id *ids = cdrv->ids, *found;
  39. if (!ids)
  40. return 0;
  41. found = ccw_device_id_match(ids, &cdev->id);
  42. if (!found)
  43. return 0;
  44. cdev->id.driver_info = found->driver_info;
  45. return 1;
  46. }
  47. /* Store modalias string delimited by prefix/suffix string into buffer with
  48. * specified size. Return length of resulting string (excluding trailing '\0')
  49. * even if string doesn't fit buffer (snprintf semantics). */
  50. static int snprint_alias(char *buf, size_t size, const char *prefix,
  51. struct ccw_device_id *id, const char *suffix)
  52. {
  53. int len;
  54. len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
  55. id->cu_model);
  56. if (len > size)
  57. return len;
  58. buf += len;
  59. size -= len;
  60. if (id->dev_type != 0)
  61. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  62. id->dev_model, suffix);
  63. else
  64. len += snprintf(buf, size, "dtdm%s", suffix);
  65. return len;
  66. }
  67. /* Set up environment variables for ccw device uevent. Return 0 on success,
  68. * non-zero otherwise. */
  69. static int ccw_uevent(struct device *dev, char **envp, int num_envp,
  70. char *buffer, int buffer_size)
  71. {
  72. struct ccw_device *cdev = to_ccwdev(dev);
  73. struct ccw_device_id *id = &(cdev->id);
  74. int i = 0;
  75. int len;
  76. /* CU_TYPE= */
  77. len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
  78. if (len > buffer_size || i >= num_envp)
  79. return -ENOMEM;
  80. envp[i++] = buffer;
  81. buffer += len;
  82. buffer_size -= len;
  83. /* CU_MODEL= */
  84. len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
  85. if (len > buffer_size || i >= num_envp)
  86. return -ENOMEM;
  87. envp[i++] = buffer;
  88. buffer += len;
  89. buffer_size -= len;
  90. /* The next two can be zero, that's ok for us */
  91. /* DEV_TYPE= */
  92. len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
  93. if (len > buffer_size || i >= num_envp)
  94. return -ENOMEM;
  95. envp[i++] = buffer;
  96. buffer += len;
  97. buffer_size -= len;
  98. /* DEV_MODEL= */
  99. len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
  100. (unsigned char) id->dev_model) + 1;
  101. if (len > buffer_size || i >= num_envp)
  102. return -ENOMEM;
  103. envp[i++] = buffer;
  104. buffer += len;
  105. buffer_size -= len;
  106. /* MODALIAS= */
  107. len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
  108. if (len > buffer_size || i >= num_envp)
  109. return -ENOMEM;
  110. envp[i++] = buffer;
  111. buffer += len;
  112. buffer_size -= len;
  113. envp[i] = NULL;
  114. return 0;
  115. }
  116. struct bus_type ccw_bus_type;
  117. static int io_subchannel_probe (struct subchannel *);
  118. static int io_subchannel_remove (struct subchannel *);
  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. int ccw_device_is_orphan(struct ccw_device *cdev)
  251. {
  252. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  253. }
  254. static void ccw_device_unregister(struct work_struct *work)
  255. {
  256. struct ccw_device_private *priv;
  257. struct ccw_device *cdev;
  258. priv = container_of(work, struct ccw_device_private, kick_work);
  259. cdev = priv->cdev;
  260. if (test_and_clear_bit(1, &cdev->private->registered))
  261. device_unregister(&cdev->dev);
  262. put_device(&cdev->dev);
  263. }
  264. static void
  265. ccw_device_remove_disconnected(struct ccw_device *cdev)
  266. {
  267. struct subchannel *sch;
  268. unsigned long flags;
  269. /*
  270. * Forced offline in disconnected state means
  271. * 'throw away device'.
  272. */
  273. if (ccw_device_is_orphan(cdev)) {
  274. /* Deregister ccw device. */
  275. spin_lock_irqsave(cdev->ccwlock, flags);
  276. cdev->private->state = DEV_STATE_NOT_OPER;
  277. spin_unlock_irqrestore(cdev->ccwlock, flags);
  278. if (get_device(&cdev->dev)) {
  279. PREPARE_WORK(&cdev->private->kick_work,
  280. ccw_device_unregister);
  281. queue_work(ccw_device_work, &cdev->private->kick_work);
  282. }
  283. return ;
  284. }
  285. sch = to_subchannel(cdev->dev.parent);
  286. css_sch_device_unregister(sch);
  287. /* Reset intparm to zeroes. */
  288. sch->schib.pmcw.intparm = 0;
  289. cio_modify(sch);
  290. put_device(&sch->dev);
  291. }
  292. int
  293. ccw_device_set_offline(struct ccw_device *cdev)
  294. {
  295. int ret;
  296. if (!cdev)
  297. return -ENODEV;
  298. if (!cdev->online || !cdev->drv)
  299. return -EINVAL;
  300. if (cdev->drv->set_offline) {
  301. ret = cdev->drv->set_offline(cdev);
  302. if (ret != 0)
  303. return ret;
  304. }
  305. cdev->online = 0;
  306. spin_lock_irq(cdev->ccwlock);
  307. ret = ccw_device_offline(cdev);
  308. if (ret == -ENODEV) {
  309. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  310. cdev->private->state = DEV_STATE_OFFLINE;
  311. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  312. }
  313. spin_unlock_irq(cdev->ccwlock);
  314. return ret;
  315. }
  316. spin_unlock_irq(cdev->ccwlock);
  317. if (ret == 0)
  318. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  319. else {
  320. pr_debug("ccw_device_offline returned %d, device %s\n",
  321. ret, cdev->dev.bus_id);
  322. cdev->online = 1;
  323. }
  324. return ret;
  325. }
  326. int
  327. ccw_device_set_online(struct ccw_device *cdev)
  328. {
  329. int ret;
  330. if (!cdev)
  331. return -ENODEV;
  332. if (cdev->online || !cdev->drv)
  333. return -EINVAL;
  334. spin_lock_irq(cdev->ccwlock);
  335. ret = ccw_device_online(cdev);
  336. spin_unlock_irq(cdev->ccwlock);
  337. if (ret == 0)
  338. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  339. else {
  340. pr_debug("ccw_device_online returned %d, device %s\n",
  341. ret, cdev->dev.bus_id);
  342. return ret;
  343. }
  344. if (cdev->private->state != DEV_STATE_ONLINE)
  345. return -ENODEV;
  346. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  347. cdev->online = 1;
  348. return 0;
  349. }
  350. spin_lock_irq(cdev->ccwlock);
  351. ret = ccw_device_offline(cdev);
  352. spin_unlock_irq(cdev->ccwlock);
  353. if (ret == 0)
  354. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  355. else
  356. pr_debug("ccw_device_offline returned %d, device %s\n",
  357. ret, cdev->dev.bus_id);
  358. return (ret == 0) ? -ENODEV : ret;
  359. }
  360. static void online_store_handle_offline(struct ccw_device *cdev)
  361. {
  362. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  363. ccw_device_remove_disconnected(cdev);
  364. else if (cdev->drv && cdev->drv->set_offline)
  365. ccw_device_set_offline(cdev);
  366. }
  367. static int online_store_recog_and_online(struct ccw_device *cdev)
  368. {
  369. int ret;
  370. /* Do device recognition, if needed. */
  371. if (cdev->id.cu_type == 0) {
  372. ret = ccw_device_recognition(cdev);
  373. if (ret) {
  374. printk(KERN_WARNING"Couldn't start recognition "
  375. "for device %s (ret=%d)\n",
  376. cdev->dev.bus_id, ret);
  377. return ret;
  378. }
  379. wait_event(cdev->private->wait_q,
  380. cdev->private->flags.recog_done);
  381. }
  382. if (cdev->drv && cdev->drv->set_online)
  383. ccw_device_set_online(cdev);
  384. return 0;
  385. }
  386. static void online_store_handle_online(struct ccw_device *cdev, int force)
  387. {
  388. int ret;
  389. ret = online_store_recog_and_online(cdev);
  390. if (ret)
  391. return;
  392. if (force && cdev->private->state == DEV_STATE_BOXED) {
  393. ret = ccw_device_stlck(cdev);
  394. if (ret) {
  395. printk(KERN_WARNING"ccw_device_stlck for device %s "
  396. "returned %d!\n", cdev->dev.bus_id, ret);
  397. return;
  398. }
  399. if (cdev->id.cu_type == 0)
  400. cdev->private->state = DEV_STATE_NOT_OPER;
  401. online_store_recog_and_online(cdev);
  402. }
  403. }
  404. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  405. const char *buf, size_t count)
  406. {
  407. struct ccw_device *cdev = to_ccwdev(dev);
  408. int i, force;
  409. char *tmp;
  410. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  411. return -EAGAIN;
  412. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  413. atomic_set(&cdev->private->onoff, 0);
  414. return -EINVAL;
  415. }
  416. if (!strncmp(buf, "force\n", count)) {
  417. force = 1;
  418. i = 1;
  419. } else {
  420. force = 0;
  421. i = simple_strtoul(buf, &tmp, 16);
  422. }
  423. switch (i) {
  424. case 0:
  425. online_store_handle_offline(cdev);
  426. break;
  427. case 1:
  428. online_store_handle_online(cdev, force);
  429. break;
  430. default:
  431. count = -EINVAL;
  432. }
  433. if (cdev->drv)
  434. module_put(cdev->drv->owner);
  435. atomic_set(&cdev->private->onoff, 0);
  436. return count;
  437. }
  438. static ssize_t
  439. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  440. {
  441. struct ccw_device *cdev = to_ccwdev(dev);
  442. struct subchannel *sch;
  443. if (ccw_device_is_orphan(cdev))
  444. return sprintf(buf, "no device\n");
  445. switch (cdev->private->state) {
  446. case DEV_STATE_BOXED:
  447. return sprintf(buf, "boxed\n");
  448. case DEV_STATE_DISCONNECTED:
  449. case DEV_STATE_DISCONNECTED_SENSE_ID:
  450. case DEV_STATE_NOT_OPER:
  451. sch = to_subchannel(dev->parent);
  452. if (!sch->lpm)
  453. return sprintf(buf, "no path\n");
  454. else
  455. return sprintf(buf, "no device\n");
  456. default:
  457. /* All other states considered fine. */
  458. return sprintf(buf, "good\n");
  459. }
  460. }
  461. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  462. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  463. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  464. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  465. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  466. static DEVICE_ATTR(online, 0644, online_show, online_store);
  467. extern struct device_attribute dev_attr_cmb_enable;
  468. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  469. static struct attribute * subch_attrs[] = {
  470. &dev_attr_chpids.attr,
  471. &dev_attr_pimpampom.attr,
  472. NULL,
  473. };
  474. static struct attribute_group subch_attr_group = {
  475. .attrs = subch_attrs,
  476. };
  477. struct attribute_group *subch_attr_groups[] = {
  478. &subch_attr_group,
  479. NULL,
  480. };
  481. static struct attribute * ccwdev_attrs[] = {
  482. &dev_attr_devtype.attr,
  483. &dev_attr_cutype.attr,
  484. &dev_attr_modalias.attr,
  485. &dev_attr_online.attr,
  486. &dev_attr_cmb_enable.attr,
  487. &dev_attr_availability.attr,
  488. NULL,
  489. };
  490. static struct attribute_group ccwdev_attr_group = {
  491. .attrs = ccwdev_attrs,
  492. };
  493. static int
  494. device_add_files (struct device *dev)
  495. {
  496. return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
  497. }
  498. static void
  499. device_remove_files(struct device *dev)
  500. {
  501. sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
  502. }
  503. /* this is a simple abstraction for device_register that sets the
  504. * correct bus type and adds the bus specific files */
  505. static int ccw_device_register(struct ccw_device *cdev)
  506. {
  507. struct device *dev = &cdev->dev;
  508. int ret;
  509. dev->bus = &ccw_bus_type;
  510. if ((ret = device_add(dev)))
  511. return ret;
  512. set_bit(1, &cdev->private->registered);
  513. if ((ret = device_add_files(dev))) {
  514. if (test_and_clear_bit(1, &cdev->private->registered))
  515. device_del(dev);
  516. }
  517. return ret;
  518. }
  519. struct match_data {
  520. struct ccw_dev_id dev_id;
  521. struct ccw_device * sibling;
  522. };
  523. static int
  524. match_devno(struct device * dev, void * data)
  525. {
  526. struct match_data * d = data;
  527. struct ccw_device * cdev;
  528. cdev = to_ccwdev(dev);
  529. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  530. !ccw_device_is_orphan(cdev) &&
  531. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  532. (cdev != d->sibling))
  533. return 1;
  534. return 0;
  535. }
  536. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  537. struct ccw_device *sibling)
  538. {
  539. struct device *dev;
  540. struct match_data data;
  541. data.dev_id = *dev_id;
  542. data.sibling = sibling;
  543. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  544. return dev ? to_ccwdev(dev) : NULL;
  545. }
  546. static int match_orphan(struct device *dev, void *data)
  547. {
  548. struct ccw_dev_id *dev_id;
  549. struct ccw_device *cdev;
  550. dev_id = data;
  551. cdev = to_ccwdev(dev);
  552. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  553. }
  554. static struct ccw_device *
  555. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  556. struct ccw_dev_id *dev_id)
  557. {
  558. struct device *dev;
  559. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  560. match_orphan);
  561. return dev ? to_ccwdev(dev) : NULL;
  562. }
  563. static void
  564. ccw_device_add_changed(struct work_struct *work)
  565. {
  566. struct ccw_device_private *priv;
  567. struct ccw_device *cdev;
  568. priv = container_of(work, struct ccw_device_private, kick_work);
  569. cdev = priv->cdev;
  570. if (device_add(&cdev->dev)) {
  571. put_device(&cdev->dev);
  572. return;
  573. }
  574. set_bit(1, &cdev->private->registered);
  575. if (device_add_files(&cdev->dev)) {
  576. if (test_and_clear_bit(1, &cdev->private->registered))
  577. device_unregister(&cdev->dev);
  578. }
  579. }
  580. void ccw_device_do_unreg_rereg(struct work_struct *work)
  581. {
  582. struct ccw_device_private *priv;
  583. struct ccw_device *cdev;
  584. struct subchannel *sch;
  585. priv = container_of(work, struct ccw_device_private, kick_work);
  586. cdev = priv->cdev;
  587. sch = to_subchannel(cdev->dev.parent);
  588. device_remove_files(&cdev->dev);
  589. if (test_and_clear_bit(1, &cdev->private->registered))
  590. device_del(&cdev->dev);
  591. PREPARE_WORK(&cdev->private->kick_work,
  592. ccw_device_add_changed);
  593. queue_work(ccw_device_work, &cdev->private->kick_work);
  594. }
  595. static void
  596. ccw_device_release(struct device *dev)
  597. {
  598. struct ccw_device *cdev;
  599. cdev = to_ccwdev(dev);
  600. kfree(cdev->private);
  601. kfree(cdev);
  602. }
  603. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  604. {
  605. struct ccw_device *cdev;
  606. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  607. if (cdev) {
  608. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  609. GFP_KERNEL | GFP_DMA);
  610. if (cdev->private)
  611. return cdev;
  612. }
  613. kfree(cdev);
  614. return ERR_PTR(-ENOMEM);
  615. }
  616. static int io_subchannel_initialize_dev(struct subchannel *sch,
  617. struct ccw_device *cdev)
  618. {
  619. cdev->private->cdev = cdev;
  620. atomic_set(&cdev->private->onoff, 0);
  621. cdev->dev.parent = &sch->dev;
  622. cdev->dev.release = ccw_device_release;
  623. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  624. /* Do first half of device_register. */
  625. device_initialize(&cdev->dev);
  626. if (!get_device(&sch->dev)) {
  627. if (cdev->dev.release)
  628. cdev->dev.release(&cdev->dev);
  629. return -ENODEV;
  630. }
  631. return 0;
  632. }
  633. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  634. {
  635. struct ccw_device *cdev;
  636. int ret;
  637. cdev = io_subchannel_allocate_dev(sch);
  638. if (!IS_ERR(cdev)) {
  639. ret = io_subchannel_initialize_dev(sch, cdev);
  640. if (ret) {
  641. kfree(cdev);
  642. cdev = ERR_PTR(ret);
  643. }
  644. }
  645. return cdev;
  646. }
  647. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  648. static void sch_attach_device(struct subchannel *sch,
  649. struct ccw_device *cdev)
  650. {
  651. spin_lock_irq(sch->lock);
  652. sch->dev.driver_data = cdev;
  653. cdev->private->schid = sch->schid;
  654. cdev->ccwlock = sch->lock;
  655. device_trigger_reprobe(sch);
  656. spin_unlock_irq(sch->lock);
  657. }
  658. static void sch_attach_disconnected_device(struct subchannel *sch,
  659. struct ccw_device *cdev)
  660. {
  661. struct subchannel *other_sch;
  662. int ret;
  663. other_sch = to_subchannel(get_device(cdev->dev.parent));
  664. ret = device_move(&cdev->dev, &sch->dev);
  665. if (ret) {
  666. CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
  667. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  668. cdev->private->dev_id.devno, ret);
  669. put_device(&other_sch->dev);
  670. return;
  671. }
  672. other_sch->dev.driver_data = NULL;
  673. /* No need to keep a subchannel without ccw device around. */
  674. css_sch_device_unregister(other_sch);
  675. put_device(&other_sch->dev);
  676. sch_attach_device(sch, cdev);
  677. }
  678. static void sch_attach_orphaned_device(struct subchannel *sch,
  679. struct ccw_device *cdev)
  680. {
  681. int ret;
  682. /* Try to move the ccw device to its new subchannel. */
  683. ret = device_move(&cdev->dev, &sch->dev);
  684. if (ret) {
  685. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  686. "failed (ret=%d)!\n",
  687. cdev->private->dev_id.ssid,
  688. cdev->private->dev_id.devno, ret);
  689. return;
  690. }
  691. sch_attach_device(sch, cdev);
  692. }
  693. static void sch_create_and_recog_new_device(struct subchannel *sch)
  694. {
  695. struct ccw_device *cdev;
  696. /* Need to allocate a new ccw device. */
  697. cdev = io_subchannel_create_ccwdev(sch);
  698. if (IS_ERR(cdev)) {
  699. /* OK, we did everything we could... */
  700. css_sch_device_unregister(sch);
  701. return;
  702. }
  703. spin_lock_irq(sch->lock);
  704. sch->dev.driver_data = cdev;
  705. spin_unlock_irq(sch->lock);
  706. /* Start recognition for the new ccw device. */
  707. if (io_subchannel_recog(cdev, sch)) {
  708. spin_lock_irq(sch->lock);
  709. sch->dev.driver_data = NULL;
  710. spin_unlock_irq(sch->lock);
  711. if (cdev->dev.release)
  712. cdev->dev.release(&cdev->dev);
  713. css_sch_device_unregister(sch);
  714. }
  715. }
  716. void ccw_device_move_to_orphanage(struct work_struct *work)
  717. {
  718. struct ccw_device_private *priv;
  719. struct ccw_device *cdev;
  720. struct ccw_device *replacing_cdev;
  721. struct subchannel *sch;
  722. int ret;
  723. struct channel_subsystem *css;
  724. struct ccw_dev_id dev_id;
  725. priv = container_of(work, struct ccw_device_private, kick_work);
  726. cdev = priv->cdev;
  727. sch = to_subchannel(cdev->dev.parent);
  728. css = to_css(sch->dev.parent);
  729. dev_id.devno = sch->schib.pmcw.dev;
  730. dev_id.ssid = sch->schid.ssid;
  731. /*
  732. * Move the orphaned ccw device to the orphanage so the replacing
  733. * ccw device can take its place on the subchannel.
  734. */
  735. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  736. if (ret) {
  737. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  738. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  739. cdev->private->dev_id.devno, ret);
  740. return;
  741. }
  742. cdev->ccwlock = css->pseudo_subchannel->lock;
  743. /*
  744. * Search for the replacing ccw device
  745. * - among the disconnected devices
  746. * - in the orphanage
  747. */
  748. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  749. if (replacing_cdev) {
  750. sch_attach_disconnected_device(sch, replacing_cdev);
  751. return;
  752. }
  753. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  754. if (replacing_cdev) {
  755. sch_attach_orphaned_device(sch, replacing_cdev);
  756. return;
  757. }
  758. sch_create_and_recog_new_device(sch);
  759. }
  760. /*
  761. * Register recognized device.
  762. */
  763. static void
  764. io_subchannel_register(struct work_struct *work)
  765. {
  766. struct ccw_device_private *priv;
  767. struct ccw_device *cdev;
  768. struct subchannel *sch;
  769. int ret;
  770. unsigned long flags;
  771. priv = container_of(work, struct ccw_device_private, kick_work);
  772. cdev = priv->cdev;
  773. sch = to_subchannel(cdev->dev.parent);
  774. /*
  775. * io_subchannel_register() will also be called after device
  776. * recognition has been done for a boxed device (which will already
  777. * be registered). We need to reprobe since we may now have sense id
  778. * information.
  779. */
  780. if (klist_node_attached(&cdev->dev.knode_parent)) {
  781. if (!cdev->drv) {
  782. ret = device_reprobe(&cdev->dev);
  783. if (ret)
  784. /* We can't do much here. */
  785. dev_info(&cdev->dev, "device_reprobe() returned"
  786. " %d\n", ret);
  787. }
  788. goto out;
  789. }
  790. /* make it known to the system */
  791. ret = ccw_device_register(cdev);
  792. if (ret) {
  793. printk (KERN_WARNING "%s: could not register %s\n",
  794. __func__, cdev->dev.bus_id);
  795. put_device(&cdev->dev);
  796. spin_lock_irqsave(sch->lock, flags);
  797. sch->dev.driver_data = NULL;
  798. spin_unlock_irqrestore(sch->lock, flags);
  799. kfree (cdev->private);
  800. kfree (cdev);
  801. put_device(&sch->dev);
  802. if (atomic_dec_and_test(&ccw_device_init_count))
  803. wake_up(&ccw_device_init_wq);
  804. return;
  805. }
  806. put_device(&cdev->dev);
  807. out:
  808. cdev->private->flags.recog_done = 1;
  809. put_device(&sch->dev);
  810. wake_up(&cdev->private->wait_q);
  811. if (atomic_dec_and_test(&ccw_device_init_count))
  812. wake_up(&ccw_device_init_wq);
  813. }
  814. void
  815. ccw_device_call_sch_unregister(struct work_struct *work)
  816. {
  817. struct ccw_device_private *priv;
  818. struct ccw_device *cdev;
  819. struct subchannel *sch;
  820. priv = container_of(work, struct ccw_device_private, kick_work);
  821. cdev = priv->cdev;
  822. sch = to_subchannel(cdev->dev.parent);
  823. css_sch_device_unregister(sch);
  824. /* Reset intparm to zeroes. */
  825. sch->schib.pmcw.intparm = 0;
  826. cio_modify(sch);
  827. put_device(&cdev->dev);
  828. put_device(&sch->dev);
  829. }
  830. /*
  831. * subchannel recognition done. Called from the state machine.
  832. */
  833. void
  834. io_subchannel_recog_done(struct ccw_device *cdev)
  835. {
  836. struct subchannel *sch;
  837. if (css_init_done == 0) {
  838. cdev->private->flags.recog_done = 1;
  839. return;
  840. }
  841. switch (cdev->private->state) {
  842. case DEV_STATE_NOT_OPER:
  843. cdev->private->flags.recog_done = 1;
  844. /* Remove device found not operational. */
  845. if (!get_device(&cdev->dev))
  846. break;
  847. sch = to_subchannel(cdev->dev.parent);
  848. PREPARE_WORK(&cdev->private->kick_work,
  849. ccw_device_call_sch_unregister);
  850. queue_work(slow_path_wq, &cdev->private->kick_work);
  851. if (atomic_dec_and_test(&ccw_device_init_count))
  852. wake_up(&ccw_device_init_wq);
  853. break;
  854. case DEV_STATE_BOXED:
  855. /* Device did not respond in time. */
  856. case DEV_STATE_OFFLINE:
  857. /*
  858. * We can't register the device in interrupt context so
  859. * we schedule a work item.
  860. */
  861. if (!get_device(&cdev->dev))
  862. break;
  863. PREPARE_WORK(&cdev->private->kick_work,
  864. io_subchannel_register);
  865. queue_work(slow_path_wq, &cdev->private->kick_work);
  866. break;
  867. }
  868. }
  869. static int
  870. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  871. {
  872. int rc;
  873. struct ccw_device_private *priv;
  874. sch->dev.driver_data = cdev;
  875. sch->driver = &io_subchannel_driver;
  876. cdev->ccwlock = sch->lock;
  877. /* Init private data. */
  878. priv = cdev->private;
  879. priv->dev_id.devno = sch->schib.pmcw.dev;
  880. priv->dev_id.ssid = sch->schid.ssid;
  881. priv->schid = sch->schid;
  882. priv->state = DEV_STATE_NOT_OPER;
  883. INIT_LIST_HEAD(&priv->cmb_list);
  884. init_waitqueue_head(&priv->wait_q);
  885. init_timer(&priv->timer);
  886. /* Set an initial name for the device. */
  887. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  888. sch->schid.ssid, sch->schib.pmcw.dev);
  889. /* Increase counter of devices currently in recognition. */
  890. atomic_inc(&ccw_device_init_count);
  891. /* Start async. device sensing. */
  892. spin_lock_irq(sch->lock);
  893. rc = ccw_device_recognition(cdev);
  894. spin_unlock_irq(sch->lock);
  895. if (rc) {
  896. if (atomic_dec_and_test(&ccw_device_init_count))
  897. wake_up(&ccw_device_init_wq);
  898. }
  899. return rc;
  900. }
  901. static void ccw_device_move_to_sch(struct work_struct *work)
  902. {
  903. struct ccw_device_private *priv;
  904. int rc;
  905. struct subchannel *sch;
  906. struct ccw_device *cdev;
  907. struct subchannel *former_parent;
  908. priv = container_of(work, struct ccw_device_private, kick_work);
  909. sch = priv->sch;
  910. cdev = priv->cdev;
  911. former_parent = ccw_device_is_orphan(cdev) ?
  912. NULL : to_subchannel(get_device(cdev->dev.parent));
  913. mutex_lock(&sch->reg_mutex);
  914. /* Try to move the ccw device to its new subchannel. */
  915. rc = device_move(&cdev->dev, &sch->dev);
  916. mutex_unlock(&sch->reg_mutex);
  917. if (rc) {
  918. CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
  919. "0.%x.%04x failed (ret=%d)!\n",
  920. cdev->private->dev_id.ssid,
  921. cdev->private->dev_id.devno, sch->schid.ssid,
  922. sch->schid.sch_no, rc);
  923. css_sch_device_unregister(sch);
  924. goto out;
  925. }
  926. if (former_parent) {
  927. spin_lock_irq(former_parent->lock);
  928. former_parent->dev.driver_data = NULL;
  929. spin_unlock_irq(former_parent->lock);
  930. css_sch_device_unregister(former_parent);
  931. /* Reset intparm to zeroes. */
  932. former_parent->schib.pmcw.intparm = 0;
  933. cio_modify(former_parent);
  934. }
  935. sch_attach_device(sch, cdev);
  936. out:
  937. if (former_parent)
  938. put_device(&former_parent->dev);
  939. put_device(&cdev->dev);
  940. }
  941. static int
  942. io_subchannel_probe (struct subchannel *sch)
  943. {
  944. struct ccw_device *cdev;
  945. int rc;
  946. unsigned long flags;
  947. struct ccw_dev_id dev_id;
  948. if (sch->dev.driver_data) {
  949. /*
  950. * This subchannel already has an associated ccw_device.
  951. * Register it and exit. This happens for all early
  952. * device, e.g. the console.
  953. */
  954. cdev = sch->dev.driver_data;
  955. device_initialize(&cdev->dev);
  956. ccw_device_register(cdev);
  957. /*
  958. * Check if the device is already online. If it is
  959. * the reference count needs to be corrected
  960. * (see ccw_device_online and css_init_done for the
  961. * ugly details).
  962. */
  963. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  964. cdev->private->state != DEV_STATE_OFFLINE &&
  965. cdev->private->state != DEV_STATE_BOXED)
  966. get_device(&cdev->dev);
  967. return 0;
  968. }
  969. /*
  970. * First check if a fitting device may be found amongst the
  971. * disconnected devices or in the orphanage.
  972. */
  973. dev_id.devno = sch->schib.pmcw.dev;
  974. dev_id.ssid = sch->schid.ssid;
  975. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  976. if (!cdev)
  977. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  978. &dev_id);
  979. if (cdev) {
  980. /*
  981. * Schedule moving the device until when we have a registered
  982. * subchannel to move to and succeed the probe. We can
  983. * unregister later again, when the probe is through.
  984. */
  985. cdev->private->sch = sch;
  986. PREPARE_WORK(&cdev->private->kick_work,
  987. ccw_device_move_to_sch);
  988. queue_work(slow_path_wq, &cdev->private->kick_work);
  989. return 0;
  990. }
  991. cdev = io_subchannel_create_ccwdev(sch);
  992. if (IS_ERR(cdev))
  993. return PTR_ERR(cdev);
  994. rc = io_subchannel_recog(cdev, sch);
  995. if (rc) {
  996. spin_lock_irqsave(sch->lock, flags);
  997. sch->dev.driver_data = NULL;
  998. spin_unlock_irqrestore(sch->lock, flags);
  999. if (cdev->dev.release)
  1000. cdev->dev.release(&cdev->dev);
  1001. }
  1002. return rc;
  1003. }
  1004. static int
  1005. io_subchannel_remove (struct subchannel *sch)
  1006. {
  1007. struct ccw_device *cdev;
  1008. unsigned long flags;
  1009. if (!sch->dev.driver_data)
  1010. return 0;
  1011. cdev = sch->dev.driver_data;
  1012. /* Set ccw device to not operational and drop reference. */
  1013. spin_lock_irqsave(cdev->ccwlock, flags);
  1014. sch->dev.driver_data = NULL;
  1015. cdev->private->state = DEV_STATE_NOT_OPER;
  1016. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1017. /*
  1018. * Put unregistration on workqueue to avoid livelocks on the css bus
  1019. * semaphore.
  1020. */
  1021. if (get_device(&cdev->dev)) {
  1022. PREPARE_WORK(&cdev->private->kick_work,
  1023. ccw_device_unregister);
  1024. queue_work(ccw_device_work, &cdev->private->kick_work);
  1025. }
  1026. return 0;
  1027. }
  1028. static int
  1029. io_subchannel_notify(struct device *dev, int event)
  1030. {
  1031. struct ccw_device *cdev;
  1032. cdev = dev->driver_data;
  1033. if (!cdev)
  1034. return 0;
  1035. if (!cdev->drv)
  1036. return 0;
  1037. if (!cdev->online)
  1038. return 0;
  1039. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1040. }
  1041. static void
  1042. io_subchannel_verify(struct device *dev)
  1043. {
  1044. struct ccw_device *cdev;
  1045. cdev = dev->driver_data;
  1046. if (cdev)
  1047. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1048. }
  1049. static void
  1050. io_subchannel_ioterm(struct device *dev)
  1051. {
  1052. struct ccw_device *cdev;
  1053. cdev = dev->driver_data;
  1054. if (!cdev)
  1055. return;
  1056. /* Internal I/O will be retried by the interrupt handler. */
  1057. if (cdev->private->flags.intretry)
  1058. return;
  1059. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1060. if (cdev->handler)
  1061. cdev->handler(cdev, cdev->private->intparm,
  1062. ERR_PTR(-EIO));
  1063. }
  1064. static void
  1065. io_subchannel_shutdown(struct subchannel *sch)
  1066. {
  1067. struct ccw_device *cdev;
  1068. int ret;
  1069. cdev = sch->dev.driver_data;
  1070. if (cio_is_console(sch->schid))
  1071. return;
  1072. if (!sch->schib.pmcw.ena)
  1073. /* Nothing to do. */
  1074. return;
  1075. ret = cio_disable_subchannel(sch);
  1076. if (ret != -EBUSY)
  1077. /* Subchannel is disabled, we're done. */
  1078. return;
  1079. cdev->private->state = DEV_STATE_QUIESCE;
  1080. if (cdev->handler)
  1081. cdev->handler(cdev, cdev->private->intparm,
  1082. ERR_PTR(-EIO));
  1083. ret = ccw_device_cancel_halt_clear(cdev);
  1084. if (ret == -EBUSY) {
  1085. ccw_device_set_timeout(cdev, HZ/10);
  1086. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1087. }
  1088. cio_disable_subchannel(sch);
  1089. }
  1090. #ifdef CONFIG_CCW_CONSOLE
  1091. static struct ccw_device console_cdev;
  1092. static struct ccw_device_private console_private;
  1093. static int console_cdev_in_use;
  1094. static DEFINE_SPINLOCK(ccw_console_lock);
  1095. spinlock_t * cio_get_console_lock(void)
  1096. {
  1097. return &ccw_console_lock;
  1098. }
  1099. static int
  1100. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  1101. {
  1102. int rc;
  1103. /* Initialize the ccw_device structure. */
  1104. cdev->dev.parent= &sch->dev;
  1105. rc = io_subchannel_recog(cdev, sch);
  1106. if (rc)
  1107. return rc;
  1108. /* Now wait for the async. recognition to come to an end. */
  1109. spin_lock_irq(cdev->ccwlock);
  1110. while (!dev_fsm_final_state(cdev))
  1111. wait_cons_dev();
  1112. rc = -EIO;
  1113. if (cdev->private->state != DEV_STATE_OFFLINE)
  1114. goto out_unlock;
  1115. ccw_device_online(cdev);
  1116. while (!dev_fsm_final_state(cdev))
  1117. wait_cons_dev();
  1118. if (cdev->private->state != DEV_STATE_ONLINE)
  1119. goto out_unlock;
  1120. rc = 0;
  1121. out_unlock:
  1122. spin_unlock_irq(cdev->ccwlock);
  1123. return 0;
  1124. }
  1125. struct ccw_device *
  1126. ccw_device_probe_console(void)
  1127. {
  1128. struct subchannel *sch;
  1129. int ret;
  1130. if (xchg(&console_cdev_in_use, 1) != 0)
  1131. return ERR_PTR(-EBUSY);
  1132. sch = cio_probe_console();
  1133. if (IS_ERR(sch)) {
  1134. console_cdev_in_use = 0;
  1135. return (void *) sch;
  1136. }
  1137. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1138. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1139. console_cdev.private = &console_private;
  1140. console_private.cdev = &console_cdev;
  1141. ret = ccw_device_console_enable(&console_cdev, sch);
  1142. if (ret) {
  1143. cio_release_console();
  1144. console_cdev_in_use = 0;
  1145. return ERR_PTR(ret);
  1146. }
  1147. console_cdev.online = 1;
  1148. return &console_cdev;
  1149. }
  1150. #endif
  1151. /*
  1152. * get ccw_device matching the busid, but only if owned by cdrv
  1153. */
  1154. static int
  1155. __ccwdev_check_busid(struct device *dev, void *id)
  1156. {
  1157. char *bus_id;
  1158. bus_id = id;
  1159. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1160. }
  1161. struct ccw_device *
  1162. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  1163. {
  1164. struct device *dev;
  1165. struct device_driver *drv;
  1166. drv = get_driver(&cdrv->driver);
  1167. if (!drv)
  1168. return NULL;
  1169. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1170. __ccwdev_check_busid);
  1171. put_driver(drv);
  1172. return dev ? to_ccwdev(dev) : NULL;
  1173. }
  1174. /************************** device driver handling ************************/
  1175. /* This is the implementation of the ccw_driver class. The probe, remove
  1176. * and release methods are initially very similar to the device_driver
  1177. * implementations, with the difference that they have ccw_device
  1178. * arguments.
  1179. *
  1180. * A ccw driver also contains the information that is needed for
  1181. * device matching.
  1182. */
  1183. static int
  1184. ccw_device_probe (struct device *dev)
  1185. {
  1186. struct ccw_device *cdev = to_ccwdev(dev);
  1187. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1188. int ret;
  1189. cdev->drv = cdrv; /* to let the driver call _set_online */
  1190. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1191. if (ret) {
  1192. cdev->drv = NULL;
  1193. return ret;
  1194. }
  1195. return 0;
  1196. }
  1197. static int
  1198. ccw_device_remove (struct device *dev)
  1199. {
  1200. struct ccw_device *cdev = to_ccwdev(dev);
  1201. struct ccw_driver *cdrv = cdev->drv;
  1202. int ret;
  1203. pr_debug("removing device %s\n", cdev->dev.bus_id);
  1204. if (cdrv->remove)
  1205. cdrv->remove(cdev);
  1206. if (cdev->online) {
  1207. cdev->online = 0;
  1208. spin_lock_irq(cdev->ccwlock);
  1209. ret = ccw_device_offline(cdev);
  1210. spin_unlock_irq(cdev->ccwlock);
  1211. if (ret == 0)
  1212. wait_event(cdev->private->wait_q,
  1213. dev_fsm_final_state(cdev));
  1214. else
  1215. //FIXME: we can't fail!
  1216. pr_debug("ccw_device_offline returned %d, device %s\n",
  1217. ret, cdev->dev.bus_id);
  1218. }
  1219. ccw_device_set_timeout(cdev, 0);
  1220. cdev->drv = NULL;
  1221. return 0;
  1222. }
  1223. struct bus_type ccw_bus_type = {
  1224. .name = "ccw",
  1225. .match = ccw_bus_match,
  1226. .uevent = ccw_uevent,
  1227. .probe = ccw_device_probe,
  1228. .remove = ccw_device_remove,
  1229. };
  1230. int
  1231. ccw_driver_register (struct ccw_driver *cdriver)
  1232. {
  1233. struct device_driver *drv = &cdriver->driver;
  1234. drv->bus = &ccw_bus_type;
  1235. drv->name = cdriver->name;
  1236. return driver_register(drv);
  1237. }
  1238. void
  1239. ccw_driver_unregister (struct ccw_driver *cdriver)
  1240. {
  1241. driver_unregister(&cdriver->driver);
  1242. }
  1243. /* Helper func for qdio. */
  1244. struct subchannel_id
  1245. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1246. {
  1247. struct subchannel *sch;
  1248. sch = to_subchannel(cdev->dev.parent);
  1249. return sch->schid;
  1250. }
  1251. MODULE_LICENSE("GPL");
  1252. EXPORT_SYMBOL(ccw_device_set_online);
  1253. EXPORT_SYMBOL(ccw_device_set_offline);
  1254. EXPORT_SYMBOL(ccw_driver_register);
  1255. EXPORT_SYMBOL(ccw_driver_unregister);
  1256. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1257. EXPORT_SYMBOL(ccw_bus_type);
  1258. EXPORT_SYMBOL(ccw_device_work);
  1259. EXPORT_SYMBOL(ccw_device_notify_work);
  1260. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);