device.c 35 KB

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