device.c 37 KB

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