device.c 37 KB

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