device.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/list.h>
  17. #include <linux/device.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/timer.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include <asm/cmb.h>
  24. #include <asm/isc.h>
  25. #include "chp.h"
  26. #include "cio.h"
  27. #include "cio_debug.h"
  28. #include "css.h"
  29. #include "device.h"
  30. #include "ioasm.h"
  31. #include "io_sch.h"
  32. #include "blacklist.h"
  33. static struct timer_list recovery_timer;
  34. static DEFINE_SPINLOCK(recovery_lock);
  35. static int recovery_phase;
  36. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  37. /******************* bus type handling ***********************/
  38. /* The Linux driver model distinguishes between a bus type and
  39. * the bus itself. Of course we only have one channel
  40. * subsystem driver and one channel system per machine, but
  41. * we still use the abstraction. T.R. says it's a good idea. */
  42. static int
  43. ccw_bus_match (struct device * dev, struct device_driver * drv)
  44. {
  45. struct ccw_device *cdev = to_ccwdev(dev);
  46. struct ccw_driver *cdrv = to_ccwdrv(drv);
  47. const struct ccw_device_id *ids = cdrv->ids, *found;
  48. if (!ids)
  49. return 0;
  50. found = ccw_device_id_match(ids, &cdev->id);
  51. if (!found)
  52. return 0;
  53. cdev->id.driver_info = found->driver_info;
  54. return 1;
  55. }
  56. /* Store modalias string delimited by prefix/suffix string into buffer with
  57. * specified size. Return length of resulting string (excluding trailing '\0')
  58. * even if string doesn't fit buffer (snprintf semantics). */
  59. static int snprint_alias(char *buf, size_t size,
  60. struct ccw_device_id *id, const char *suffix)
  61. {
  62. int len;
  63. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  64. if (len > size)
  65. return len;
  66. buf += len;
  67. size -= len;
  68. if (id->dev_type != 0)
  69. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  70. id->dev_model, suffix);
  71. else
  72. len += snprintf(buf, size, "dtdm%s", suffix);
  73. return len;
  74. }
  75. /* Set up environment variables for ccw device uevent. Return 0 on success,
  76. * non-zero otherwise. */
  77. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  78. {
  79. struct ccw_device *cdev = to_ccwdev(dev);
  80. struct ccw_device_id *id = &(cdev->id);
  81. int ret;
  82. char modalias_buf[30];
  83. /* CU_TYPE= */
  84. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  85. if (ret)
  86. return ret;
  87. /* CU_MODEL= */
  88. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  89. if (ret)
  90. return ret;
  91. /* The next two can be zero, that's ok for us */
  92. /* DEV_TYPE= */
  93. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  94. if (ret)
  95. return ret;
  96. /* DEV_MODEL= */
  97. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  98. if (ret)
  99. return ret;
  100. /* MODALIAS= */
  101. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  102. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  103. return ret;
  104. }
  105. struct bus_type ccw_bus_type;
  106. static void io_subchannel_irq(struct subchannel *);
  107. static int io_subchannel_probe(struct subchannel *);
  108. static int io_subchannel_remove(struct subchannel *);
  109. static void io_subchannel_shutdown(struct subchannel *);
  110. static int io_subchannel_sch_event(struct subchannel *, int);
  111. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  112. int);
  113. static struct css_device_id io_subchannel_ids[] = {
  114. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  115. { /* end of list */ },
  116. };
  117. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  118. static struct css_driver io_subchannel_driver = {
  119. .owner = THIS_MODULE,
  120. .subchannel_type = io_subchannel_ids,
  121. .name = "io_subchannel",
  122. .irq = io_subchannel_irq,
  123. .sch_event = io_subchannel_sch_event,
  124. .chp_event = io_subchannel_chp_event,
  125. .probe = io_subchannel_probe,
  126. .remove = io_subchannel_remove,
  127. .shutdown = io_subchannel_shutdown,
  128. };
  129. struct workqueue_struct *ccw_device_work;
  130. wait_queue_head_t ccw_device_init_wq;
  131. atomic_t ccw_device_init_count;
  132. static void recovery_func(unsigned long data);
  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. setup_timer(&recovery_timer, recovery_func, 0);
  140. ccw_device_work = create_singlethread_workqueue("cio");
  141. if (!ccw_device_work)
  142. return -ENOMEM; /* FIXME: better errno ? */
  143. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  144. if (!slow_path_wq) {
  145. ret = -ENOMEM; /* FIXME: better errno ? */
  146. goto out_err;
  147. }
  148. if ((ret = bus_register (&ccw_bus_type)))
  149. goto out_err;
  150. ret = css_driver_register(&io_subchannel_driver);
  151. if (ret)
  152. goto out_err;
  153. wait_event(ccw_device_init_wq,
  154. atomic_read(&ccw_device_init_count) == 0);
  155. flush_workqueue(ccw_device_work);
  156. return 0;
  157. out_err:
  158. if (ccw_device_work)
  159. destroy_workqueue(ccw_device_work);
  160. if (slow_path_wq)
  161. destroy_workqueue(slow_path_wq);
  162. return ret;
  163. }
  164. static void __exit
  165. cleanup_ccw_bus_type (void)
  166. {
  167. css_driver_unregister(&io_subchannel_driver);
  168. bus_unregister(&ccw_bus_type);
  169. destroy_workqueue(ccw_device_work);
  170. }
  171. subsys_initcall(init_ccw_bus_type);
  172. module_exit(cleanup_ccw_bus_type);
  173. /************************ device handling **************************/
  174. /*
  175. * A ccw_device has some interfaces in sysfs in addition to the
  176. * standard ones.
  177. * The following entries are designed to export the information which
  178. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  179. * are obvious, so they don't have an entry :)
  180. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  181. */
  182. static ssize_t
  183. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  184. {
  185. struct subchannel *sch = to_subchannel(dev);
  186. struct chsc_ssd_info *ssd = &sch->ssd_info;
  187. ssize_t ret = 0;
  188. int chp;
  189. int mask;
  190. for (chp = 0; chp < 8; chp++) {
  191. mask = 0x80 >> chp;
  192. if (ssd->path_mask & mask)
  193. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  194. else
  195. ret += sprintf(buf + ret, "00 ");
  196. }
  197. ret += sprintf (buf+ret, "\n");
  198. return min((ssize_t)PAGE_SIZE, ret);
  199. }
  200. static ssize_t
  201. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  202. {
  203. struct subchannel *sch = to_subchannel(dev);
  204. struct pmcw *pmcw = &sch->schib.pmcw;
  205. return sprintf (buf, "%02x %02x %02x\n",
  206. pmcw->pim, pmcw->pam, pmcw->pom);
  207. }
  208. static ssize_t
  209. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  210. {
  211. struct ccw_device *cdev = to_ccwdev(dev);
  212. struct ccw_device_id *id = &(cdev->id);
  213. if (id->dev_type != 0)
  214. return sprintf(buf, "%04x/%02x\n",
  215. id->dev_type, id->dev_model);
  216. else
  217. return sprintf(buf, "n/a\n");
  218. }
  219. static ssize_t
  220. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  221. {
  222. struct ccw_device *cdev = to_ccwdev(dev);
  223. struct ccw_device_id *id = &(cdev->id);
  224. return sprintf(buf, "%04x/%02x\n",
  225. id->cu_type, id->cu_model);
  226. }
  227. static ssize_t
  228. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  229. {
  230. struct ccw_device *cdev = to_ccwdev(dev);
  231. struct ccw_device_id *id = &(cdev->id);
  232. int len;
  233. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  234. return len > PAGE_SIZE ? PAGE_SIZE : len;
  235. }
  236. static ssize_t
  237. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  238. {
  239. struct ccw_device *cdev = to_ccwdev(dev);
  240. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  241. }
  242. int ccw_device_is_orphan(struct ccw_device *cdev)
  243. {
  244. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  245. }
  246. static void ccw_device_unregister(struct ccw_device *cdev)
  247. {
  248. if (test_and_clear_bit(1, &cdev->private->registered))
  249. device_del(&cdev->dev);
  250. }
  251. static void ccw_device_remove_orphan_cb(struct work_struct *work)
  252. {
  253. struct ccw_device_private *priv;
  254. struct ccw_device *cdev;
  255. priv = container_of(work, struct ccw_device_private, kick_work);
  256. cdev = priv->cdev;
  257. ccw_device_unregister(cdev);
  258. put_device(&cdev->dev);
  259. /* Release cdev reference for workqueue processing. */
  260. put_device(&cdev->dev);
  261. }
  262. static void ccw_device_call_sch_unregister(struct work_struct *work);
  263. static void
  264. ccw_device_remove_disconnected(struct ccw_device *cdev)
  265. {
  266. unsigned long flags;
  267. /*
  268. * Forced offline in disconnected state means
  269. * 'throw away device'.
  270. */
  271. /* Get cdev reference for workqueue processing. */
  272. if (!get_device(&cdev->dev))
  273. return;
  274. if (ccw_device_is_orphan(cdev)) {
  275. /*
  276. * Deregister ccw device.
  277. * Unfortunately, we cannot do this directly from the
  278. * attribute method.
  279. */
  280. spin_lock_irqsave(cdev->ccwlock, flags);
  281. cdev->private->state = DEV_STATE_NOT_OPER;
  282. spin_unlock_irqrestore(cdev->ccwlock, flags);
  283. PREPARE_WORK(&cdev->private->kick_work,
  284. ccw_device_remove_orphan_cb);
  285. } else
  286. /* Deregister subchannel, which will kill the ccw device. */
  287. PREPARE_WORK(&cdev->private->kick_work,
  288. ccw_device_call_sch_unregister);
  289. queue_work(slow_path_wq, &cdev->private->kick_work);
  290. }
  291. /**
  292. * ccw_device_set_offline() - disable a ccw device for I/O
  293. * @cdev: target ccw device
  294. *
  295. * This function calls the driver's set_offline() function for @cdev, if
  296. * given, and then disables @cdev.
  297. * Returns:
  298. * %0 on success and a negative error value on failure.
  299. * Context:
  300. * enabled, ccw device lock not held
  301. */
  302. int ccw_device_set_offline(struct ccw_device *cdev)
  303. {
  304. int ret;
  305. if (!cdev)
  306. return -ENODEV;
  307. if (!cdev->online || !cdev->drv)
  308. return -EINVAL;
  309. if (cdev->drv->set_offline) {
  310. ret = cdev->drv->set_offline(cdev);
  311. if (ret != 0)
  312. return ret;
  313. }
  314. cdev->online = 0;
  315. spin_lock_irq(cdev->ccwlock);
  316. ret = ccw_device_offline(cdev);
  317. if (ret == -ENODEV) {
  318. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  319. cdev->private->state = DEV_STATE_OFFLINE;
  320. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  321. }
  322. spin_unlock_irq(cdev->ccwlock);
  323. /* Give up reference from ccw_device_set_online(). */
  324. put_device(&cdev->dev);
  325. return ret;
  326. }
  327. spin_unlock_irq(cdev->ccwlock);
  328. if (ret == 0) {
  329. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  330. /* Give up reference from ccw_device_set_online(). */
  331. put_device(&cdev->dev);
  332. } else {
  333. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  334. "device 0.%x.%04x\n",
  335. ret, cdev->private->dev_id.ssid,
  336. cdev->private->dev_id.devno);
  337. cdev->online = 1;
  338. }
  339. return ret;
  340. }
  341. /**
  342. * ccw_device_set_online() - enable a ccw device for I/O
  343. * @cdev: target ccw device
  344. *
  345. * This function first enables @cdev and then calls the driver's set_online()
  346. * function for @cdev, if given. If set_online() returns an error, @cdev is
  347. * disabled again.
  348. * Returns:
  349. * %0 on success and a negative error value on failure.
  350. * Context:
  351. * enabled, ccw device lock not held
  352. */
  353. int ccw_device_set_online(struct ccw_device *cdev)
  354. {
  355. int ret;
  356. if (!cdev)
  357. return -ENODEV;
  358. if (cdev->online || !cdev->drv)
  359. return -EINVAL;
  360. /* Hold on to an extra reference while device is online. */
  361. if (!get_device(&cdev->dev))
  362. return -ENODEV;
  363. spin_lock_irq(cdev->ccwlock);
  364. ret = ccw_device_online(cdev);
  365. spin_unlock_irq(cdev->ccwlock);
  366. if (ret == 0)
  367. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  368. else {
  369. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  370. "device 0.%x.%04x\n",
  371. ret, cdev->private->dev_id.ssid,
  372. cdev->private->dev_id.devno);
  373. /* Give up online reference since onlining failed. */
  374. put_device(&cdev->dev);
  375. return ret;
  376. }
  377. if (cdev->private->state != DEV_STATE_ONLINE) {
  378. /* Give up online reference since onlining failed. */
  379. put_device(&cdev->dev);
  380. return -ENODEV;
  381. }
  382. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  383. cdev->online = 1;
  384. return 0;
  385. }
  386. spin_lock_irq(cdev->ccwlock);
  387. ret = ccw_device_offline(cdev);
  388. spin_unlock_irq(cdev->ccwlock);
  389. if (ret == 0)
  390. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  391. else
  392. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  393. "device 0.%x.%04x\n",
  394. ret, cdev->private->dev_id.ssid,
  395. cdev->private->dev_id.devno);
  396. /* Give up online reference since onlining failed. */
  397. put_device(&cdev->dev);
  398. return (ret == 0) ? -ENODEV : ret;
  399. }
  400. static int online_store_handle_offline(struct ccw_device *cdev)
  401. {
  402. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  403. ccw_device_remove_disconnected(cdev);
  404. else if (cdev->online && cdev->drv && cdev->drv->set_offline)
  405. return ccw_device_set_offline(cdev);
  406. return 0;
  407. }
  408. static int online_store_recog_and_online(struct ccw_device *cdev)
  409. {
  410. int ret;
  411. /* Do device recognition, if needed. */
  412. if (cdev->id.cu_type == 0) {
  413. ret = ccw_device_recognition(cdev);
  414. if (ret) {
  415. CIO_MSG_EVENT(0, "Couldn't start recognition "
  416. "for device 0.%x.%04x (ret=%d)\n",
  417. cdev->private->dev_id.ssid,
  418. cdev->private->dev_id.devno, ret);
  419. return ret;
  420. }
  421. wait_event(cdev->private->wait_q,
  422. cdev->private->flags.recog_done);
  423. }
  424. if (cdev->drv && cdev->drv->set_online)
  425. ccw_device_set_online(cdev);
  426. return 0;
  427. }
  428. static int online_store_handle_online(struct ccw_device *cdev, int force)
  429. {
  430. int ret;
  431. ret = online_store_recog_and_online(cdev);
  432. if (ret)
  433. return ret;
  434. if (force && cdev->private->state == DEV_STATE_BOXED) {
  435. ret = ccw_device_stlck(cdev);
  436. if (ret)
  437. return ret;
  438. if (cdev->id.cu_type == 0)
  439. cdev->private->state = DEV_STATE_NOT_OPER;
  440. online_store_recog_and_online(cdev);
  441. }
  442. return 0;
  443. }
  444. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  445. const char *buf, size_t count)
  446. {
  447. struct ccw_device *cdev = to_ccwdev(dev);
  448. int force, ret;
  449. unsigned long i;
  450. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  451. return -EAGAIN;
  452. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  453. atomic_set(&cdev->private->onoff, 0);
  454. return -EINVAL;
  455. }
  456. if (!strncmp(buf, "force\n", count)) {
  457. force = 1;
  458. i = 1;
  459. ret = 0;
  460. } else {
  461. force = 0;
  462. ret = strict_strtoul(buf, 16, &i);
  463. }
  464. if (ret)
  465. goto out;
  466. switch (i) {
  467. case 0:
  468. ret = online_store_handle_offline(cdev);
  469. break;
  470. case 1:
  471. ret = online_store_handle_online(cdev, force);
  472. break;
  473. default:
  474. ret = -EINVAL;
  475. }
  476. out:
  477. if (cdev->drv)
  478. module_put(cdev->drv->owner);
  479. atomic_set(&cdev->private->onoff, 0);
  480. return (ret < 0) ? ret : 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. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  512. static struct attribute *io_subchannel_attrs[] = {
  513. &dev_attr_chpids.attr,
  514. &dev_attr_pimpampom.attr,
  515. NULL,
  516. };
  517. static struct attribute_group io_subchannel_attr_group = {
  518. .attrs = io_subchannel_attrs,
  519. };
  520. static struct attribute * ccwdev_attrs[] = {
  521. &dev_attr_devtype.attr,
  522. &dev_attr_cutype.attr,
  523. &dev_attr_modalias.attr,
  524. &dev_attr_online.attr,
  525. &dev_attr_cmb_enable.attr,
  526. &dev_attr_availability.attr,
  527. NULL,
  528. };
  529. static struct attribute_group ccwdev_attr_group = {
  530. .attrs = ccwdev_attrs,
  531. };
  532. static struct attribute_group *ccwdev_attr_groups[] = {
  533. &ccwdev_attr_group,
  534. NULL,
  535. };
  536. /* this is a simple abstraction for device_register that sets the
  537. * correct bus type and adds the bus specific files */
  538. static int ccw_device_register(struct ccw_device *cdev)
  539. {
  540. struct device *dev = &cdev->dev;
  541. int ret;
  542. dev->bus = &ccw_bus_type;
  543. if ((ret = device_add(dev)))
  544. return ret;
  545. set_bit(1, &cdev->private->registered);
  546. return ret;
  547. }
  548. struct match_data {
  549. struct ccw_dev_id dev_id;
  550. struct ccw_device * sibling;
  551. };
  552. static int
  553. match_devno(struct device * dev, void * data)
  554. {
  555. struct match_data * d = data;
  556. struct ccw_device * cdev;
  557. cdev = to_ccwdev(dev);
  558. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  559. !ccw_device_is_orphan(cdev) &&
  560. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  561. (cdev != d->sibling))
  562. return 1;
  563. return 0;
  564. }
  565. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  566. struct ccw_device *sibling)
  567. {
  568. struct device *dev;
  569. struct match_data data;
  570. data.dev_id = *dev_id;
  571. data.sibling = sibling;
  572. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  573. return dev ? to_ccwdev(dev) : NULL;
  574. }
  575. static int match_orphan(struct device *dev, void *data)
  576. {
  577. struct ccw_dev_id *dev_id;
  578. struct ccw_device *cdev;
  579. dev_id = data;
  580. cdev = to_ccwdev(dev);
  581. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  582. }
  583. static struct ccw_device *
  584. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  585. struct ccw_dev_id *dev_id)
  586. {
  587. struct device *dev;
  588. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  589. match_orphan);
  590. return dev ? to_ccwdev(dev) : NULL;
  591. }
  592. void ccw_device_do_unbind_bind(struct work_struct *work)
  593. {
  594. struct ccw_device_private *priv;
  595. struct ccw_device *cdev;
  596. struct subchannel *sch;
  597. int ret;
  598. priv = container_of(work, struct ccw_device_private, kick_work);
  599. cdev = priv->cdev;
  600. sch = to_subchannel(cdev->dev.parent);
  601. if (test_bit(1, &cdev->private->registered)) {
  602. device_release_driver(&cdev->dev);
  603. ret = device_attach(&cdev->dev);
  604. WARN_ON(ret == -ENODEV);
  605. }
  606. }
  607. static void
  608. ccw_device_release(struct device *dev)
  609. {
  610. struct ccw_device *cdev;
  611. cdev = to_ccwdev(dev);
  612. /* Release reference of parent subchannel. */
  613. put_device(cdev->dev.parent);
  614. kfree(cdev->private);
  615. kfree(cdev);
  616. }
  617. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  618. {
  619. struct ccw_device *cdev;
  620. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  621. if (cdev) {
  622. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  623. GFP_KERNEL | GFP_DMA);
  624. if (cdev->private)
  625. return cdev;
  626. }
  627. kfree(cdev);
  628. return ERR_PTR(-ENOMEM);
  629. }
  630. static int io_subchannel_initialize_dev(struct subchannel *sch,
  631. struct ccw_device *cdev)
  632. {
  633. cdev->private->cdev = cdev;
  634. atomic_set(&cdev->private->onoff, 0);
  635. cdev->dev.parent = &sch->dev;
  636. cdev->dev.release = ccw_device_release;
  637. INIT_WORK(&cdev->private->kick_work, NULL);
  638. cdev->dev.groups = ccwdev_attr_groups;
  639. /* Do first half of device_register. */
  640. device_initialize(&cdev->dev);
  641. if (!get_device(&sch->dev)) {
  642. /* Release reference from device_initialize(). */
  643. put_device(&cdev->dev);
  644. return -ENODEV;
  645. }
  646. return 0;
  647. }
  648. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  649. {
  650. struct ccw_device *cdev;
  651. int ret;
  652. cdev = io_subchannel_allocate_dev(sch);
  653. if (!IS_ERR(cdev)) {
  654. ret = io_subchannel_initialize_dev(sch, cdev);
  655. if (ret) {
  656. kfree(cdev);
  657. cdev = ERR_PTR(ret);
  658. }
  659. }
  660. return cdev;
  661. }
  662. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  663. static void sch_attach_device(struct subchannel *sch,
  664. struct ccw_device *cdev)
  665. {
  666. css_update_ssd_info(sch);
  667. spin_lock_irq(sch->lock);
  668. sch_set_cdev(sch, cdev);
  669. cdev->private->schid = sch->schid;
  670. cdev->ccwlock = sch->lock;
  671. ccw_device_trigger_reprobe(cdev);
  672. spin_unlock_irq(sch->lock);
  673. }
  674. static void sch_attach_disconnected_device(struct subchannel *sch,
  675. struct ccw_device *cdev)
  676. {
  677. struct subchannel *other_sch;
  678. int ret;
  679. /* Get reference for new parent. */
  680. if (!get_device(&sch->dev))
  681. return;
  682. other_sch = to_subchannel(cdev->dev.parent);
  683. /* Note: device_move() changes cdev->dev.parent */
  684. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  685. if (ret) {
  686. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  687. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  688. cdev->private->dev_id.devno, ret);
  689. /* Put reference for new parent. */
  690. put_device(&sch->dev);
  691. return;
  692. }
  693. sch_set_cdev(other_sch, NULL);
  694. /* No need to keep a subchannel without ccw device around. */
  695. css_sch_device_unregister(other_sch);
  696. sch_attach_device(sch, cdev);
  697. /* Put reference for old parent. */
  698. put_device(&other_sch->dev);
  699. }
  700. static void sch_attach_orphaned_device(struct subchannel *sch,
  701. struct ccw_device *cdev)
  702. {
  703. int ret;
  704. struct subchannel *pseudo_sch;
  705. /* Get reference for new parent. */
  706. if (!get_device(&sch->dev))
  707. return;
  708. pseudo_sch = to_subchannel(cdev->dev.parent);
  709. /*
  710. * Try to move the ccw device to its new subchannel.
  711. * Note: device_move() changes cdev->dev.parent
  712. */
  713. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_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. /* Put reference for new parent. */
  720. put_device(&sch->dev);
  721. return;
  722. }
  723. sch_attach_device(sch, cdev);
  724. /* Put reference on pseudo subchannel. */
  725. put_device(&pseudo_sch->dev);
  726. }
  727. static void sch_create_and_recog_new_device(struct subchannel *sch)
  728. {
  729. struct ccw_device *cdev;
  730. /* Need to allocate a new ccw device. */
  731. cdev = io_subchannel_create_ccwdev(sch);
  732. if (IS_ERR(cdev)) {
  733. /* OK, we did everything we could... */
  734. css_sch_device_unregister(sch);
  735. return;
  736. }
  737. spin_lock_irq(sch->lock);
  738. sch_set_cdev(sch, cdev);
  739. spin_unlock_irq(sch->lock);
  740. /* Start recognition for the new ccw device. */
  741. if (io_subchannel_recog(cdev, sch)) {
  742. spin_lock_irq(sch->lock);
  743. sch_set_cdev(sch, NULL);
  744. spin_unlock_irq(sch->lock);
  745. css_sch_device_unregister(sch);
  746. /* Put reference from io_subchannel_create_ccwdev(). */
  747. put_device(&sch->dev);
  748. /* Give up initial reference. */
  749. put_device(&cdev->dev);
  750. }
  751. }
  752. void ccw_device_move_to_orphanage(struct work_struct *work)
  753. {
  754. struct ccw_device_private *priv;
  755. struct ccw_device *cdev;
  756. struct ccw_device *replacing_cdev;
  757. struct subchannel *sch;
  758. int ret;
  759. struct channel_subsystem *css;
  760. struct ccw_dev_id dev_id;
  761. priv = container_of(work, struct ccw_device_private, kick_work);
  762. cdev = priv->cdev;
  763. sch = to_subchannel(cdev->dev.parent);
  764. css = to_css(sch->dev.parent);
  765. dev_id.devno = sch->schib.pmcw.dev;
  766. dev_id.ssid = sch->schid.ssid;
  767. /* Increase refcount for pseudo subchannel. */
  768. get_device(&css->pseudo_subchannel->dev);
  769. /*
  770. * Move the orphaned ccw device to the orphanage so the replacing
  771. * ccw device can take its place on the subchannel.
  772. * Note: device_move() changes cdev->dev.parent
  773. */
  774. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
  775. DPM_ORDER_NONE);
  776. if (ret) {
  777. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  778. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  779. cdev->private->dev_id.devno, ret);
  780. /* Decrease refcount for pseudo subchannel again. */
  781. put_device(&css->pseudo_subchannel->dev);
  782. return;
  783. }
  784. cdev->ccwlock = css->pseudo_subchannel->lock;
  785. /*
  786. * Search for the replacing ccw device
  787. * - among the disconnected devices
  788. * - in the orphanage
  789. */
  790. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  791. if (replacing_cdev) {
  792. sch_attach_disconnected_device(sch, replacing_cdev);
  793. /* Release reference from get_disc_ccwdev_by_dev_id() */
  794. put_device(&replacing_cdev->dev);
  795. /* Release reference of subchannel from old cdev. */
  796. put_device(&sch->dev);
  797. return;
  798. }
  799. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  800. if (replacing_cdev) {
  801. sch_attach_orphaned_device(sch, replacing_cdev);
  802. /* Release reference from get_orphaned_ccwdev_by_dev_id() */
  803. put_device(&replacing_cdev->dev);
  804. /* Release reference of subchannel from old cdev. */
  805. put_device(&sch->dev);
  806. return;
  807. }
  808. sch_create_and_recog_new_device(sch);
  809. /* Release reference of subchannel from old cdev. */
  810. put_device(&sch->dev);
  811. }
  812. /*
  813. * Register recognized device.
  814. */
  815. static void
  816. io_subchannel_register(struct work_struct *work)
  817. {
  818. struct ccw_device_private *priv;
  819. struct ccw_device *cdev;
  820. struct subchannel *sch;
  821. int ret;
  822. unsigned long flags;
  823. priv = container_of(work, struct ccw_device_private, kick_work);
  824. cdev = priv->cdev;
  825. sch = to_subchannel(cdev->dev.parent);
  826. /*
  827. * Check if subchannel is still registered. It may have become
  828. * unregistered if a machine check hit us after finishing
  829. * device recognition but before the register work could be
  830. * queued.
  831. */
  832. if (!device_is_registered(&sch->dev))
  833. goto out_err;
  834. css_update_ssd_info(sch);
  835. /*
  836. * io_subchannel_register() will also be called after device
  837. * recognition has been done for a boxed device (which will already
  838. * be registered). We need to reprobe since we may now have sense id
  839. * information.
  840. */
  841. if (device_is_registered(&cdev->dev)) {
  842. if (!cdev->drv) {
  843. ret = device_reprobe(&cdev->dev);
  844. if (ret)
  845. /* We can't do much here. */
  846. CIO_MSG_EVENT(0, "device_reprobe() returned"
  847. " %d for 0.%x.%04x\n", ret,
  848. cdev->private->dev_id.ssid,
  849. cdev->private->dev_id.devno);
  850. }
  851. goto out;
  852. }
  853. /*
  854. * Now we know this subchannel will stay, we can throw
  855. * our delayed uevent.
  856. */
  857. dev_set_uevent_suppress(&sch->dev, 0);
  858. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  859. /* make it known to the system */
  860. ret = ccw_device_register(cdev);
  861. if (ret) {
  862. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  863. cdev->private->dev_id.ssid,
  864. cdev->private->dev_id.devno, ret);
  865. spin_lock_irqsave(sch->lock, flags);
  866. sch_set_cdev(sch, NULL);
  867. spin_unlock_irqrestore(sch->lock, flags);
  868. /* Release initial device reference. */
  869. put_device(&cdev->dev);
  870. goto out_err;
  871. }
  872. out:
  873. cdev->private->flags.recog_done = 1;
  874. wake_up(&cdev->private->wait_q);
  875. out_err:
  876. /* Release reference for workqueue processing. */
  877. put_device(&cdev->dev);
  878. if (atomic_dec_and_test(&ccw_device_init_count))
  879. wake_up(&ccw_device_init_wq);
  880. }
  881. static void ccw_device_call_sch_unregister(struct work_struct *work)
  882. {
  883. struct ccw_device_private *priv;
  884. struct ccw_device *cdev;
  885. struct subchannel *sch;
  886. priv = container_of(work, struct ccw_device_private, kick_work);
  887. cdev = priv->cdev;
  888. /* Get subchannel reference for local processing. */
  889. if (!get_device(cdev->dev.parent))
  890. return;
  891. sch = to_subchannel(cdev->dev.parent);
  892. css_sch_device_unregister(sch);
  893. /* Reset intparm to zeroes. */
  894. sch->config.intparm = 0;
  895. cio_commit_config(sch);
  896. /* Release cdev reference for workqueue processing.*/
  897. put_device(&cdev->dev);
  898. /* Release subchannel reference for local processing. */
  899. put_device(&sch->dev);
  900. }
  901. /*
  902. * subchannel recognition done. Called from the state machine.
  903. */
  904. void
  905. io_subchannel_recog_done(struct ccw_device *cdev)
  906. {
  907. if (css_init_done == 0) {
  908. cdev->private->flags.recog_done = 1;
  909. return;
  910. }
  911. switch (cdev->private->state) {
  912. case DEV_STATE_NOT_OPER:
  913. cdev->private->flags.recog_done = 1;
  914. /* Remove device found not operational. */
  915. if (!get_device(&cdev->dev))
  916. break;
  917. PREPARE_WORK(&cdev->private->kick_work,
  918. ccw_device_call_sch_unregister);
  919. queue_work(slow_path_wq, &cdev->private->kick_work);
  920. if (atomic_dec_and_test(&ccw_device_init_count))
  921. wake_up(&ccw_device_init_wq);
  922. break;
  923. case DEV_STATE_BOXED:
  924. /* Device did not respond in time. */
  925. case DEV_STATE_OFFLINE:
  926. /*
  927. * We can't register the device in interrupt context so
  928. * we schedule a work item.
  929. */
  930. if (!get_device(&cdev->dev))
  931. break;
  932. PREPARE_WORK(&cdev->private->kick_work,
  933. io_subchannel_register);
  934. queue_work(slow_path_wq, &cdev->private->kick_work);
  935. break;
  936. }
  937. }
  938. static int
  939. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  940. {
  941. int rc;
  942. struct ccw_device_private *priv;
  943. sch_set_cdev(sch, cdev);
  944. cdev->ccwlock = sch->lock;
  945. /* Init private data. */
  946. priv = cdev->private;
  947. priv->dev_id.devno = sch->schib.pmcw.dev;
  948. priv->dev_id.ssid = sch->schid.ssid;
  949. priv->schid = sch->schid;
  950. priv->state = DEV_STATE_NOT_OPER;
  951. INIT_LIST_HEAD(&priv->cmb_list);
  952. init_waitqueue_head(&priv->wait_q);
  953. init_timer(&priv->timer);
  954. /* Set an initial name for the device. */
  955. if (cio_is_console(sch->schid))
  956. cdev->dev.init_name = cio_get_console_cdev_name(sch);
  957. else
  958. dev_set_name(&cdev->dev, "0.%x.%04x",
  959. sch->schid.ssid, sch->schib.pmcw.dev);
  960. /* Increase counter of devices currently in recognition. */
  961. atomic_inc(&ccw_device_init_count);
  962. /* Start async. device sensing. */
  963. spin_lock_irq(sch->lock);
  964. rc = ccw_device_recognition(cdev);
  965. spin_unlock_irq(sch->lock);
  966. if (rc) {
  967. if (atomic_dec_and_test(&ccw_device_init_count))
  968. wake_up(&ccw_device_init_wq);
  969. }
  970. return rc;
  971. }
  972. static void ccw_device_move_to_sch(struct work_struct *work)
  973. {
  974. struct ccw_device_private *priv;
  975. int rc;
  976. struct subchannel *sch;
  977. struct ccw_device *cdev;
  978. struct subchannel *former_parent;
  979. priv = container_of(work, struct ccw_device_private, kick_work);
  980. sch = priv->sch;
  981. cdev = priv->cdev;
  982. former_parent = to_subchannel(cdev->dev.parent);
  983. /* Get reference for new parent. */
  984. if (!get_device(&sch->dev))
  985. return;
  986. mutex_lock(&sch->reg_mutex);
  987. /*
  988. * Try to move the ccw device to its new subchannel.
  989. * Note: device_move() changes cdev->dev.parent
  990. */
  991. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  992. mutex_unlock(&sch->reg_mutex);
  993. if (rc) {
  994. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  995. "0.%x.%04x failed (ret=%d)!\n",
  996. cdev->private->dev_id.ssid,
  997. cdev->private->dev_id.devno, sch->schid.ssid,
  998. sch->schid.sch_no, rc);
  999. css_sch_device_unregister(sch);
  1000. /* Put reference for new parent again. */
  1001. put_device(&sch->dev);
  1002. goto out;
  1003. }
  1004. if (!sch_is_pseudo_sch(former_parent)) {
  1005. spin_lock_irq(former_parent->lock);
  1006. sch_set_cdev(former_parent, NULL);
  1007. spin_unlock_irq(former_parent->lock);
  1008. css_sch_device_unregister(former_parent);
  1009. /* Reset intparm to zeroes. */
  1010. former_parent->config.intparm = 0;
  1011. cio_commit_config(former_parent);
  1012. }
  1013. sch_attach_device(sch, cdev);
  1014. out:
  1015. /* Put reference for old parent. */
  1016. put_device(&former_parent->dev);
  1017. put_device(&cdev->dev);
  1018. }
  1019. static void io_subchannel_irq(struct subchannel *sch)
  1020. {
  1021. struct ccw_device *cdev;
  1022. cdev = sch_get_cdev(sch);
  1023. CIO_TRACE_EVENT(3, "IRQ");
  1024. CIO_TRACE_EVENT(3, dev_name(&sch->dev));
  1025. if (cdev)
  1026. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  1027. }
  1028. void io_subchannel_init_config(struct subchannel *sch)
  1029. {
  1030. memset(&sch->config, 0, sizeof(sch->config));
  1031. sch->config.csense = 1;
  1032. /* Use subchannel mp mode when there is more than 1 installed CHPID. */
  1033. if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
  1034. sch->config.mp = 1;
  1035. }
  1036. static void io_subchannel_init_fields(struct subchannel *sch)
  1037. {
  1038. if (cio_is_console(sch->schid))
  1039. sch->opm = 0xff;
  1040. else
  1041. sch->opm = chp_get_sch_opm(sch);
  1042. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  1043. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1044. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1045. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1046. sch->schib.pmcw.dev, sch->schid.ssid,
  1047. sch->schid.sch_no, sch->schib.pmcw.pim,
  1048. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1049. io_subchannel_init_config(sch);
  1050. }
  1051. static void io_subchannel_do_unreg(struct work_struct *work)
  1052. {
  1053. struct subchannel *sch;
  1054. sch = container_of(work, struct subchannel, work);
  1055. css_sch_device_unregister(sch);
  1056. /* Reset intparm to zeroes. */
  1057. sch->config.intparm = 0;
  1058. cio_commit_config(sch);
  1059. put_device(&sch->dev);
  1060. }
  1061. /* Schedule unregister if we have no cdev. */
  1062. static void io_subchannel_schedule_removal(struct subchannel *sch)
  1063. {
  1064. get_device(&sch->dev);
  1065. INIT_WORK(&sch->work, io_subchannel_do_unreg);
  1066. queue_work(slow_path_wq, &sch->work);
  1067. }
  1068. /*
  1069. * Note: We always return 0 so that we bind to the device even on error.
  1070. * This is needed so that our remove function is called on unregister.
  1071. */
  1072. static int io_subchannel_probe(struct subchannel *sch)
  1073. {
  1074. struct ccw_device *cdev;
  1075. int rc;
  1076. unsigned long flags;
  1077. struct ccw_dev_id dev_id;
  1078. cdev = sch_get_cdev(sch);
  1079. if (cdev) {
  1080. rc = sysfs_create_group(&sch->dev.kobj,
  1081. &io_subchannel_attr_group);
  1082. if (rc)
  1083. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1084. "attributes for subchannel "
  1085. "0.%x.%04x (rc=%d)\n",
  1086. sch->schid.ssid, sch->schid.sch_no, rc);
  1087. /*
  1088. * This subchannel already has an associated ccw_device.
  1089. * Throw the delayed uevent for the subchannel, register
  1090. * the ccw_device and exit. This happens for all early
  1091. * devices, e.g. the console.
  1092. */
  1093. dev_set_uevent_suppress(&sch->dev, 0);
  1094. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1095. cdev->dev.groups = ccwdev_attr_groups;
  1096. device_initialize(&cdev->dev);
  1097. ccw_device_register(cdev);
  1098. /*
  1099. * Check if the device is already online. If it is
  1100. * the reference count needs to be corrected since we
  1101. * didn't obtain a reference in ccw_device_set_online.
  1102. */
  1103. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1104. cdev->private->state != DEV_STATE_OFFLINE &&
  1105. cdev->private->state != DEV_STATE_BOXED)
  1106. get_device(&cdev->dev);
  1107. return 0;
  1108. }
  1109. io_subchannel_init_fields(sch);
  1110. rc = cio_commit_config(sch);
  1111. if (rc)
  1112. goto out_schedule;
  1113. rc = sysfs_create_group(&sch->dev.kobj,
  1114. &io_subchannel_attr_group);
  1115. if (rc)
  1116. goto out_schedule;
  1117. /* Allocate I/O subchannel private data. */
  1118. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1119. GFP_KERNEL | GFP_DMA);
  1120. if (!sch->private)
  1121. goto out_err;
  1122. /*
  1123. * First check if a fitting device may be found amongst the
  1124. * disconnected devices or in the orphanage.
  1125. */
  1126. dev_id.devno = sch->schib.pmcw.dev;
  1127. dev_id.ssid = sch->schid.ssid;
  1128. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1129. if (!cdev)
  1130. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1131. &dev_id);
  1132. if (cdev) {
  1133. /*
  1134. * Schedule moving the device until when we have a registered
  1135. * subchannel to move to and succeed the probe. We can
  1136. * unregister later again, when the probe is through.
  1137. */
  1138. cdev->private->sch = sch;
  1139. PREPARE_WORK(&cdev->private->kick_work,
  1140. ccw_device_move_to_sch);
  1141. queue_work(slow_path_wq, &cdev->private->kick_work);
  1142. return 0;
  1143. }
  1144. cdev = io_subchannel_create_ccwdev(sch);
  1145. if (IS_ERR(cdev))
  1146. goto out_err;
  1147. rc = io_subchannel_recog(cdev, sch);
  1148. if (rc) {
  1149. spin_lock_irqsave(sch->lock, flags);
  1150. io_subchannel_recog_done(cdev);
  1151. spin_unlock_irqrestore(sch->lock, flags);
  1152. }
  1153. return 0;
  1154. out_err:
  1155. kfree(sch->private);
  1156. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1157. out_schedule:
  1158. io_subchannel_schedule_removal(sch);
  1159. return 0;
  1160. }
  1161. static int
  1162. io_subchannel_remove (struct subchannel *sch)
  1163. {
  1164. struct ccw_device *cdev;
  1165. unsigned long flags;
  1166. cdev = sch_get_cdev(sch);
  1167. if (!cdev)
  1168. return 0;
  1169. /* Set ccw device to not operational and drop reference. */
  1170. spin_lock_irqsave(cdev->ccwlock, flags);
  1171. sch_set_cdev(sch, NULL);
  1172. cdev->private->state = DEV_STATE_NOT_OPER;
  1173. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1174. ccw_device_unregister(cdev);
  1175. put_device(&cdev->dev);
  1176. kfree(sch->private);
  1177. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1178. return 0;
  1179. }
  1180. static int io_subchannel_notify(struct subchannel *sch, int event)
  1181. {
  1182. struct ccw_device *cdev;
  1183. cdev = sch_get_cdev(sch);
  1184. if (!cdev)
  1185. return 0;
  1186. return ccw_device_notify(cdev, event);
  1187. }
  1188. static void io_subchannel_verify(struct subchannel *sch)
  1189. {
  1190. struct ccw_device *cdev;
  1191. cdev = sch_get_cdev(sch);
  1192. if (cdev)
  1193. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1194. }
  1195. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1196. {
  1197. if (cio_update_schib(sch))
  1198. return 0;
  1199. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1200. return 1;
  1201. return 0;
  1202. }
  1203. static void terminate_internal_io(struct subchannel *sch,
  1204. struct ccw_device *cdev)
  1205. {
  1206. if (cio_clear(sch)) {
  1207. /* Recheck device in case clear failed. */
  1208. sch->lpm = 0;
  1209. if (cdev->online)
  1210. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1211. else
  1212. css_schedule_eval(sch->schid);
  1213. return;
  1214. }
  1215. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1216. /* Request retry of internal operation. */
  1217. cdev->private->flags.intretry = 1;
  1218. /* Call handler. */
  1219. if (cdev->handler)
  1220. cdev->handler(cdev, cdev->private->intparm,
  1221. ERR_PTR(-EIO));
  1222. }
  1223. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1224. {
  1225. struct ccw_device *cdev;
  1226. cdev = sch_get_cdev(sch);
  1227. if (!cdev)
  1228. return;
  1229. if (check_for_io_on_path(sch, mask)) {
  1230. if (cdev->private->state == DEV_STATE_ONLINE)
  1231. ccw_device_kill_io(cdev);
  1232. else {
  1233. terminate_internal_io(sch, cdev);
  1234. /* Re-start path verification. */
  1235. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1236. }
  1237. } else
  1238. /* trigger path verification. */
  1239. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1240. }
  1241. static int io_subchannel_chp_event(struct subchannel *sch,
  1242. struct chp_link *link, int event)
  1243. {
  1244. int mask;
  1245. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1246. if (!mask)
  1247. return 0;
  1248. switch (event) {
  1249. case CHP_VARY_OFF:
  1250. sch->opm &= ~mask;
  1251. sch->lpm &= ~mask;
  1252. io_subchannel_terminate_path(sch, mask);
  1253. break;
  1254. case CHP_VARY_ON:
  1255. sch->opm |= mask;
  1256. sch->lpm |= mask;
  1257. io_subchannel_verify(sch);
  1258. break;
  1259. case CHP_OFFLINE:
  1260. if (cio_update_schib(sch))
  1261. return -ENODEV;
  1262. io_subchannel_terminate_path(sch, mask);
  1263. break;
  1264. case CHP_ONLINE:
  1265. if (cio_update_schib(sch))
  1266. return -ENODEV;
  1267. sch->lpm |= mask & sch->opm;
  1268. io_subchannel_verify(sch);
  1269. break;
  1270. }
  1271. return 0;
  1272. }
  1273. static void
  1274. io_subchannel_shutdown(struct subchannel *sch)
  1275. {
  1276. struct ccw_device *cdev;
  1277. int ret;
  1278. cdev = sch_get_cdev(sch);
  1279. if (cio_is_console(sch->schid))
  1280. return;
  1281. if (!sch->schib.pmcw.ena)
  1282. /* Nothing to do. */
  1283. return;
  1284. ret = cio_disable_subchannel(sch);
  1285. if (ret != -EBUSY)
  1286. /* Subchannel is disabled, we're done. */
  1287. return;
  1288. cdev->private->state = DEV_STATE_QUIESCE;
  1289. if (cdev->handler)
  1290. cdev->handler(cdev, cdev->private->intparm,
  1291. ERR_PTR(-EIO));
  1292. ret = ccw_device_cancel_halt_clear(cdev);
  1293. if (ret == -EBUSY) {
  1294. ccw_device_set_timeout(cdev, HZ/10);
  1295. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1296. }
  1297. cio_disable_subchannel(sch);
  1298. }
  1299. static int io_subchannel_get_status(struct subchannel *sch)
  1300. {
  1301. struct schib schib;
  1302. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1303. return CIO_GONE;
  1304. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1305. return CIO_REVALIDATE;
  1306. if (!sch->lpm)
  1307. return CIO_NO_PATH;
  1308. return CIO_OPER;
  1309. }
  1310. static int device_is_disconnected(struct ccw_device *cdev)
  1311. {
  1312. if (!cdev)
  1313. return 0;
  1314. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1315. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1316. }
  1317. static int recovery_check(struct device *dev, void *data)
  1318. {
  1319. struct ccw_device *cdev = to_ccwdev(dev);
  1320. int *redo = data;
  1321. spin_lock_irq(cdev->ccwlock);
  1322. switch (cdev->private->state) {
  1323. case DEV_STATE_DISCONNECTED:
  1324. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1325. cdev->private->dev_id.ssid,
  1326. cdev->private->dev_id.devno);
  1327. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1328. *redo = 1;
  1329. break;
  1330. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1331. *redo = 1;
  1332. break;
  1333. }
  1334. spin_unlock_irq(cdev->ccwlock);
  1335. return 0;
  1336. }
  1337. static void recovery_work_func(struct work_struct *unused)
  1338. {
  1339. int redo = 0;
  1340. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1341. if (redo) {
  1342. spin_lock_irq(&recovery_lock);
  1343. if (!timer_pending(&recovery_timer)) {
  1344. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1345. recovery_phase++;
  1346. mod_timer(&recovery_timer, jiffies +
  1347. recovery_delay[recovery_phase] * HZ);
  1348. }
  1349. spin_unlock_irq(&recovery_lock);
  1350. } else
  1351. CIO_MSG_EVENT(4, "recovery: end\n");
  1352. }
  1353. static DECLARE_WORK(recovery_work, recovery_work_func);
  1354. static void recovery_func(unsigned long data)
  1355. {
  1356. /*
  1357. * We can't do our recovery in softirq context and it's not
  1358. * performance critical, so we schedule it.
  1359. */
  1360. schedule_work(&recovery_work);
  1361. }
  1362. static void ccw_device_schedule_recovery(void)
  1363. {
  1364. unsigned long flags;
  1365. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1366. spin_lock_irqsave(&recovery_lock, flags);
  1367. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1368. recovery_phase = 0;
  1369. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1370. }
  1371. spin_unlock_irqrestore(&recovery_lock, flags);
  1372. }
  1373. static int purge_fn(struct device *dev, void *data)
  1374. {
  1375. struct ccw_device *cdev = to_ccwdev(dev);
  1376. struct ccw_device_private *priv = cdev->private;
  1377. int unreg;
  1378. spin_lock_irq(cdev->ccwlock);
  1379. unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
  1380. (priv->state == DEV_STATE_OFFLINE);
  1381. spin_unlock_irq(cdev->ccwlock);
  1382. if (!unreg)
  1383. goto out;
  1384. if (!get_device(&cdev->dev))
  1385. goto out;
  1386. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
  1387. priv->dev_id.devno);
  1388. PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
  1389. queue_work(slow_path_wq, &cdev->private->kick_work);
  1390. out:
  1391. /* Abort loop in case of pending signal. */
  1392. if (signal_pending(current))
  1393. return -EINTR;
  1394. return 0;
  1395. }
  1396. /**
  1397. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1398. *
  1399. * Unregister all ccw devices that are offline and on the blacklist.
  1400. */
  1401. int ccw_purge_blacklisted(void)
  1402. {
  1403. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1404. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1405. return 0;
  1406. }
  1407. static void device_set_disconnected(struct ccw_device *cdev)
  1408. {
  1409. if (!cdev)
  1410. return;
  1411. ccw_device_set_timeout(cdev, 0);
  1412. cdev->private->flags.fake_irb = 0;
  1413. cdev->private->state = DEV_STATE_DISCONNECTED;
  1414. if (cdev->online)
  1415. ccw_device_schedule_recovery();
  1416. }
  1417. void ccw_device_set_notoper(struct ccw_device *cdev)
  1418. {
  1419. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1420. CIO_TRACE_EVENT(2, "notoper");
  1421. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1422. ccw_device_set_timeout(cdev, 0);
  1423. cio_disable_subchannel(sch);
  1424. cdev->private->state = DEV_STATE_NOT_OPER;
  1425. }
  1426. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1427. {
  1428. int event, ret, disc;
  1429. unsigned long flags;
  1430. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
  1431. struct ccw_device *cdev;
  1432. spin_lock_irqsave(sch->lock, flags);
  1433. cdev = sch_get_cdev(sch);
  1434. disc = device_is_disconnected(cdev);
  1435. if (disc && slow) {
  1436. /* Disconnected devices are evaluated directly only.*/
  1437. spin_unlock_irqrestore(sch->lock, flags);
  1438. return 0;
  1439. }
  1440. /* No interrupt after machine check - kill pending timers. */
  1441. if (cdev)
  1442. ccw_device_set_timeout(cdev, 0);
  1443. if (!disc && !slow) {
  1444. /* Non-disconnected devices are evaluated on the slow path. */
  1445. spin_unlock_irqrestore(sch->lock, flags);
  1446. return -EAGAIN;
  1447. }
  1448. event = io_subchannel_get_status(sch);
  1449. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1450. sch->schid.ssid, sch->schid.sch_no, event,
  1451. disc ? "disconnected" : "normal",
  1452. slow ? "slow" : "fast");
  1453. /* Analyze subchannel status. */
  1454. action = NONE;
  1455. switch (event) {
  1456. case CIO_NO_PATH:
  1457. if (disc) {
  1458. /* Check if paths have become available. */
  1459. action = REPROBE;
  1460. break;
  1461. }
  1462. /* fall through */
  1463. case CIO_GONE:
  1464. /* Ask driver what to do with device. */
  1465. if (io_subchannel_notify(sch, event))
  1466. action = DISC;
  1467. else
  1468. action = UNREGISTER;
  1469. break;
  1470. case CIO_REVALIDATE:
  1471. /* Device will be removed, so no notify necessary. */
  1472. if (disc)
  1473. /* Reprobe because immediate unregister might block. */
  1474. action = REPROBE;
  1475. else
  1476. action = UNREGISTER_PROBE;
  1477. break;
  1478. case CIO_OPER:
  1479. if (disc)
  1480. /* Get device operational again. */
  1481. action = REPROBE;
  1482. break;
  1483. }
  1484. /* Perform action. */
  1485. ret = 0;
  1486. switch (action) {
  1487. case UNREGISTER:
  1488. case UNREGISTER_PROBE:
  1489. ccw_device_set_notoper(cdev);
  1490. /* Unregister device (will use subchannel lock). */
  1491. spin_unlock_irqrestore(sch->lock, flags);
  1492. css_sch_device_unregister(sch);
  1493. spin_lock_irqsave(sch->lock, flags);
  1494. /* Reset intparm to zeroes. */
  1495. sch->config.intparm = 0;
  1496. cio_commit_config(sch);
  1497. break;
  1498. case REPROBE:
  1499. ccw_device_trigger_reprobe(cdev);
  1500. break;
  1501. case DISC:
  1502. device_set_disconnected(cdev);
  1503. break;
  1504. default:
  1505. break;
  1506. }
  1507. spin_unlock_irqrestore(sch->lock, flags);
  1508. /* Probe if necessary. */
  1509. if (action == UNREGISTER_PROBE)
  1510. ret = css_probe_device(sch->schid);
  1511. return ret;
  1512. }
  1513. #ifdef CONFIG_CCW_CONSOLE
  1514. static struct ccw_device console_cdev;
  1515. static char console_cdev_name[10] = "0.x.xxxx";
  1516. static struct ccw_device_private console_private;
  1517. static int console_cdev_in_use;
  1518. static DEFINE_SPINLOCK(ccw_console_lock);
  1519. spinlock_t * cio_get_console_lock(void)
  1520. {
  1521. return &ccw_console_lock;
  1522. }
  1523. static int ccw_device_console_enable(struct ccw_device *cdev,
  1524. struct subchannel *sch)
  1525. {
  1526. int rc;
  1527. /* Attach subchannel private data. */
  1528. sch->private = cio_get_console_priv();
  1529. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1530. io_subchannel_init_fields(sch);
  1531. rc = cio_commit_config(sch);
  1532. if (rc)
  1533. return rc;
  1534. sch->driver = &io_subchannel_driver;
  1535. /* Initialize the ccw_device structure. */
  1536. cdev->dev.parent= &sch->dev;
  1537. rc = io_subchannel_recog(cdev, sch);
  1538. if (rc)
  1539. return rc;
  1540. /* Now wait for the async. recognition to come to an end. */
  1541. spin_lock_irq(cdev->ccwlock);
  1542. while (!dev_fsm_final_state(cdev))
  1543. wait_cons_dev();
  1544. rc = -EIO;
  1545. if (cdev->private->state != DEV_STATE_OFFLINE)
  1546. goto out_unlock;
  1547. ccw_device_online(cdev);
  1548. while (!dev_fsm_final_state(cdev))
  1549. wait_cons_dev();
  1550. if (cdev->private->state != DEV_STATE_ONLINE)
  1551. goto out_unlock;
  1552. rc = 0;
  1553. out_unlock:
  1554. spin_unlock_irq(cdev->ccwlock);
  1555. return 0;
  1556. }
  1557. struct ccw_device *
  1558. ccw_device_probe_console(void)
  1559. {
  1560. struct subchannel *sch;
  1561. int ret;
  1562. if (xchg(&console_cdev_in_use, 1) != 0)
  1563. return ERR_PTR(-EBUSY);
  1564. sch = cio_probe_console();
  1565. if (IS_ERR(sch)) {
  1566. console_cdev_in_use = 0;
  1567. return (void *) sch;
  1568. }
  1569. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1570. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1571. console_cdev.private = &console_private;
  1572. console_private.cdev = &console_cdev;
  1573. ret = ccw_device_console_enable(&console_cdev, sch);
  1574. if (ret) {
  1575. cio_release_console();
  1576. console_cdev_in_use = 0;
  1577. return ERR_PTR(ret);
  1578. }
  1579. console_cdev.online = 1;
  1580. return &console_cdev;
  1581. }
  1582. const char *cio_get_console_cdev_name(struct subchannel *sch)
  1583. {
  1584. snprintf(console_cdev_name, 10, "0.%x.%04x",
  1585. sch->schid.ssid, sch->schib.pmcw.dev);
  1586. return (const char *)console_cdev_name;
  1587. }
  1588. #endif
  1589. /*
  1590. * get ccw_device matching the busid, but only if owned by cdrv
  1591. */
  1592. static int
  1593. __ccwdev_check_busid(struct device *dev, void *id)
  1594. {
  1595. char *bus_id;
  1596. bus_id = id;
  1597. return (strcmp(bus_id, dev_name(dev)) == 0);
  1598. }
  1599. /**
  1600. * get_ccwdev_by_busid() - obtain device from a bus id
  1601. * @cdrv: driver the device is owned by
  1602. * @bus_id: bus id of the device to be searched
  1603. *
  1604. * This function searches all devices owned by @cdrv for a device with a bus
  1605. * id matching @bus_id.
  1606. * Returns:
  1607. * If a match is found, its reference count of the found device is increased
  1608. * and it is returned; else %NULL is returned.
  1609. */
  1610. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1611. const char *bus_id)
  1612. {
  1613. struct device *dev;
  1614. struct device_driver *drv;
  1615. drv = get_driver(&cdrv->driver);
  1616. if (!drv)
  1617. return NULL;
  1618. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1619. __ccwdev_check_busid);
  1620. put_driver(drv);
  1621. return dev ? to_ccwdev(dev) : NULL;
  1622. }
  1623. /************************** device driver handling ************************/
  1624. /* This is the implementation of the ccw_driver class. The probe, remove
  1625. * and release methods are initially very similar to the device_driver
  1626. * implementations, with the difference that they have ccw_device
  1627. * arguments.
  1628. *
  1629. * A ccw driver also contains the information that is needed for
  1630. * device matching.
  1631. */
  1632. static int
  1633. ccw_device_probe (struct device *dev)
  1634. {
  1635. struct ccw_device *cdev = to_ccwdev(dev);
  1636. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1637. int ret;
  1638. cdev->drv = cdrv; /* to let the driver call _set_online */
  1639. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1640. if (ret) {
  1641. cdev->drv = NULL;
  1642. return ret;
  1643. }
  1644. return 0;
  1645. }
  1646. static int
  1647. ccw_device_remove (struct device *dev)
  1648. {
  1649. struct ccw_device *cdev = to_ccwdev(dev);
  1650. struct ccw_driver *cdrv = cdev->drv;
  1651. int ret;
  1652. if (cdrv->remove)
  1653. cdrv->remove(cdev);
  1654. if (cdev->online) {
  1655. cdev->online = 0;
  1656. spin_lock_irq(cdev->ccwlock);
  1657. ret = ccw_device_offline(cdev);
  1658. spin_unlock_irq(cdev->ccwlock);
  1659. if (ret == 0)
  1660. wait_event(cdev->private->wait_q,
  1661. dev_fsm_final_state(cdev));
  1662. else
  1663. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1664. "device 0.%x.%04x\n",
  1665. ret, cdev->private->dev_id.ssid,
  1666. cdev->private->dev_id.devno);
  1667. /* Give up reference obtained in ccw_device_set_online(). */
  1668. put_device(&cdev->dev);
  1669. }
  1670. ccw_device_set_timeout(cdev, 0);
  1671. cdev->drv = NULL;
  1672. return 0;
  1673. }
  1674. static void ccw_device_shutdown(struct device *dev)
  1675. {
  1676. struct ccw_device *cdev;
  1677. cdev = to_ccwdev(dev);
  1678. if (cdev->drv && cdev->drv->shutdown)
  1679. cdev->drv->shutdown(cdev);
  1680. disable_cmf(cdev);
  1681. }
  1682. struct bus_type ccw_bus_type = {
  1683. .name = "ccw",
  1684. .match = ccw_bus_match,
  1685. .uevent = ccw_uevent,
  1686. .probe = ccw_device_probe,
  1687. .remove = ccw_device_remove,
  1688. .shutdown = ccw_device_shutdown,
  1689. };
  1690. /**
  1691. * ccw_driver_register() - register a ccw driver
  1692. * @cdriver: driver to be registered
  1693. *
  1694. * This function is mainly a wrapper around driver_register().
  1695. * Returns:
  1696. * %0 on success and a negative error value on failure.
  1697. */
  1698. int ccw_driver_register(struct ccw_driver *cdriver)
  1699. {
  1700. struct device_driver *drv = &cdriver->driver;
  1701. drv->bus = &ccw_bus_type;
  1702. drv->name = cdriver->name;
  1703. drv->owner = cdriver->owner;
  1704. return driver_register(drv);
  1705. }
  1706. /**
  1707. * ccw_driver_unregister() - deregister a ccw driver
  1708. * @cdriver: driver to be deregistered
  1709. *
  1710. * This function is mainly a wrapper around driver_unregister().
  1711. */
  1712. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1713. {
  1714. driver_unregister(&cdriver->driver);
  1715. }
  1716. /* Helper func for qdio. */
  1717. struct subchannel_id
  1718. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1719. {
  1720. struct subchannel *sch;
  1721. sch = to_subchannel(cdev->dev.parent);
  1722. return sch->schid;
  1723. }
  1724. MODULE_LICENSE("GPL");
  1725. EXPORT_SYMBOL(ccw_device_set_online);
  1726. EXPORT_SYMBOL(ccw_device_set_offline);
  1727. EXPORT_SYMBOL(ccw_driver_register);
  1728. EXPORT_SYMBOL(ccw_driver_unregister);
  1729. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1730. EXPORT_SYMBOL(ccw_bus_type);
  1731. EXPORT_SYMBOL(ccw_device_work);
  1732. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);