device.c 37 KB

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