device.c 35 KB

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