device.c 35 KB

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