device.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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 void 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->drv && cdev->drv->set_offline)
  405. ccw_device_set_offline(cdev);
  406. }
  407. static int online_store_recog_and_online(struct ccw_device *cdev)
  408. {
  409. int ret;
  410. /* Do device recognition, if needed. */
  411. if (cdev->id.cu_type == 0) {
  412. ret = ccw_device_recognition(cdev);
  413. if (ret) {
  414. CIO_MSG_EVENT(0, "Couldn't start recognition "
  415. "for device 0.%x.%04x (ret=%d)\n",
  416. cdev->private->dev_id.ssid,
  417. cdev->private->dev_id.devno, ret);
  418. return ret;
  419. }
  420. wait_event(cdev->private->wait_q,
  421. cdev->private->flags.recog_done);
  422. }
  423. if (cdev->drv && cdev->drv->set_online)
  424. ccw_device_set_online(cdev);
  425. return 0;
  426. }
  427. static int online_store_handle_online(struct ccw_device *cdev, int force)
  428. {
  429. int ret;
  430. ret = online_store_recog_and_online(cdev);
  431. if (ret)
  432. return ret;
  433. if (force && cdev->private->state == DEV_STATE_BOXED) {
  434. ret = ccw_device_stlck(cdev);
  435. if (ret)
  436. return ret;
  437. if (cdev->id.cu_type == 0)
  438. cdev->private->state = DEV_STATE_NOT_OPER;
  439. online_store_recog_and_online(cdev);
  440. }
  441. return 0;
  442. }
  443. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. struct ccw_device *cdev = to_ccwdev(dev);
  447. int force, ret;
  448. unsigned long i;
  449. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  450. return -EAGAIN;
  451. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  452. atomic_set(&cdev->private->onoff, 0);
  453. return -EINVAL;
  454. }
  455. if (!strncmp(buf, "force\n", count)) {
  456. force = 1;
  457. i = 1;
  458. ret = 0;
  459. } else {
  460. force = 0;
  461. ret = strict_strtoul(buf, 16, &i);
  462. }
  463. if (ret)
  464. goto out;
  465. switch (i) {
  466. case 0:
  467. online_store_handle_offline(cdev);
  468. ret = count;
  469. break;
  470. case 1:
  471. ret = online_store_handle_online(cdev, force);
  472. if (!ret)
  473. ret = count;
  474. break;
  475. default:
  476. ret = -EINVAL;
  477. }
  478. out:
  479. if (cdev->drv)
  480. module_put(cdev->drv->owner);
  481. atomic_set(&cdev->private->onoff, 0);
  482. return ret;
  483. }
  484. static ssize_t
  485. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  486. {
  487. struct ccw_device *cdev = to_ccwdev(dev);
  488. struct subchannel *sch;
  489. if (ccw_device_is_orphan(cdev))
  490. return sprintf(buf, "no device\n");
  491. switch (cdev->private->state) {
  492. case DEV_STATE_BOXED:
  493. return sprintf(buf, "boxed\n");
  494. case DEV_STATE_DISCONNECTED:
  495. case DEV_STATE_DISCONNECTED_SENSE_ID:
  496. case DEV_STATE_NOT_OPER:
  497. sch = to_subchannel(dev->parent);
  498. if (!sch->lpm)
  499. return sprintf(buf, "no path\n");
  500. else
  501. return sprintf(buf, "no device\n");
  502. default:
  503. /* All other states considered fine. */
  504. return sprintf(buf, "good\n");
  505. }
  506. }
  507. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  508. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  509. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  510. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  511. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  512. static DEVICE_ATTR(online, 0644, online_show, online_store);
  513. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  514. static struct attribute *io_subchannel_attrs[] = {
  515. &dev_attr_chpids.attr,
  516. &dev_attr_pimpampom.attr,
  517. NULL,
  518. };
  519. static struct attribute_group io_subchannel_attr_group = {
  520. .attrs = io_subchannel_attrs,
  521. };
  522. static struct attribute * ccwdev_attrs[] = {
  523. &dev_attr_devtype.attr,
  524. &dev_attr_cutype.attr,
  525. &dev_attr_modalias.attr,
  526. &dev_attr_online.attr,
  527. &dev_attr_cmb_enable.attr,
  528. &dev_attr_availability.attr,
  529. NULL,
  530. };
  531. static struct attribute_group ccwdev_attr_group = {
  532. .attrs = ccwdev_attrs,
  533. };
  534. static struct attribute_group *ccwdev_attr_groups[] = {
  535. &ccwdev_attr_group,
  536. NULL,
  537. };
  538. /* this is a simple abstraction for device_register that sets the
  539. * correct bus type and adds the bus specific files */
  540. static int ccw_device_register(struct ccw_device *cdev)
  541. {
  542. struct device *dev = &cdev->dev;
  543. int ret;
  544. dev->bus = &ccw_bus_type;
  545. if ((ret = device_add(dev)))
  546. return ret;
  547. set_bit(1, &cdev->private->registered);
  548. return ret;
  549. }
  550. struct match_data {
  551. struct ccw_dev_id dev_id;
  552. struct ccw_device * sibling;
  553. };
  554. static int
  555. match_devno(struct device * dev, void * data)
  556. {
  557. struct match_data * d = data;
  558. struct ccw_device * cdev;
  559. cdev = to_ccwdev(dev);
  560. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  561. !ccw_device_is_orphan(cdev) &&
  562. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  563. (cdev != d->sibling))
  564. return 1;
  565. return 0;
  566. }
  567. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  568. struct ccw_device *sibling)
  569. {
  570. struct device *dev;
  571. struct match_data data;
  572. data.dev_id = *dev_id;
  573. data.sibling = sibling;
  574. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  575. return dev ? to_ccwdev(dev) : NULL;
  576. }
  577. static int match_orphan(struct device *dev, void *data)
  578. {
  579. struct ccw_dev_id *dev_id;
  580. struct ccw_device *cdev;
  581. dev_id = data;
  582. cdev = to_ccwdev(dev);
  583. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  584. }
  585. static struct ccw_device *
  586. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  587. struct ccw_dev_id *dev_id)
  588. {
  589. struct device *dev;
  590. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  591. match_orphan);
  592. return dev ? to_ccwdev(dev) : NULL;
  593. }
  594. static void
  595. ccw_device_add_changed(struct work_struct *work)
  596. {
  597. struct ccw_device_private *priv;
  598. struct ccw_device *cdev;
  599. priv = container_of(work, struct ccw_device_private, kick_work);
  600. cdev = priv->cdev;
  601. if (device_add(&cdev->dev)) {
  602. put_device(&cdev->dev);
  603. return;
  604. }
  605. set_bit(1, &cdev->private->registered);
  606. }
  607. void ccw_device_do_unreg_rereg(struct work_struct *work)
  608. {
  609. struct ccw_device_private *priv;
  610. struct ccw_device *cdev;
  611. struct subchannel *sch;
  612. priv = container_of(work, struct ccw_device_private, kick_work);
  613. cdev = priv->cdev;
  614. sch = to_subchannel(cdev->dev.parent);
  615. ccw_device_unregister(cdev);
  616. PREPARE_WORK(&cdev->private->kick_work,
  617. ccw_device_add_changed);
  618. queue_work(ccw_device_work, &cdev->private->kick_work);
  619. }
  620. static void
  621. ccw_device_release(struct device *dev)
  622. {
  623. struct ccw_device *cdev;
  624. cdev = to_ccwdev(dev);
  625. /* Release reference of parent subchannel. */
  626. put_device(cdev->dev.parent);
  627. kfree(cdev->private);
  628. kfree(cdev);
  629. }
  630. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  631. {
  632. struct ccw_device *cdev;
  633. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  634. if (cdev) {
  635. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  636. GFP_KERNEL | GFP_DMA);
  637. if (cdev->private)
  638. return cdev;
  639. }
  640. kfree(cdev);
  641. return ERR_PTR(-ENOMEM);
  642. }
  643. static int io_subchannel_initialize_dev(struct subchannel *sch,
  644. struct ccw_device *cdev)
  645. {
  646. cdev->private->cdev = cdev;
  647. atomic_set(&cdev->private->onoff, 0);
  648. cdev->dev.parent = &sch->dev;
  649. cdev->dev.release = ccw_device_release;
  650. INIT_WORK(&cdev->private->kick_work, NULL);
  651. cdev->dev.groups = ccwdev_attr_groups;
  652. /* Do first half of device_register. */
  653. device_initialize(&cdev->dev);
  654. if (!get_device(&sch->dev)) {
  655. /* Release reference from device_initialize(). */
  656. put_device(&cdev->dev);
  657. return -ENODEV;
  658. }
  659. return 0;
  660. }
  661. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  662. {
  663. struct ccw_device *cdev;
  664. int ret;
  665. cdev = io_subchannel_allocate_dev(sch);
  666. if (!IS_ERR(cdev)) {
  667. ret = io_subchannel_initialize_dev(sch, cdev);
  668. if (ret) {
  669. kfree(cdev);
  670. cdev = ERR_PTR(ret);
  671. }
  672. }
  673. return cdev;
  674. }
  675. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  676. static void sch_attach_device(struct subchannel *sch,
  677. struct ccw_device *cdev)
  678. {
  679. css_update_ssd_info(sch);
  680. spin_lock_irq(sch->lock);
  681. sch_set_cdev(sch, cdev);
  682. cdev->private->schid = sch->schid;
  683. cdev->ccwlock = sch->lock;
  684. ccw_device_trigger_reprobe(cdev);
  685. spin_unlock_irq(sch->lock);
  686. }
  687. static void sch_attach_disconnected_device(struct subchannel *sch,
  688. struct ccw_device *cdev)
  689. {
  690. struct subchannel *other_sch;
  691. int ret;
  692. /* Get reference for new parent. */
  693. if (!get_device(&sch->dev))
  694. return;
  695. other_sch = to_subchannel(cdev->dev.parent);
  696. /* Note: device_move() changes cdev->dev.parent */
  697. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  698. if (ret) {
  699. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  700. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  701. cdev->private->dev_id.devno, ret);
  702. /* Put reference for new parent. */
  703. put_device(&sch->dev);
  704. return;
  705. }
  706. sch_set_cdev(other_sch, NULL);
  707. /* No need to keep a subchannel without ccw device around. */
  708. css_sch_device_unregister(other_sch);
  709. sch_attach_device(sch, cdev);
  710. /* Put reference for old parent. */
  711. put_device(&other_sch->dev);
  712. }
  713. static void sch_attach_orphaned_device(struct subchannel *sch,
  714. struct ccw_device *cdev)
  715. {
  716. int ret;
  717. struct subchannel *pseudo_sch;
  718. /* Get reference for new parent. */
  719. if (!get_device(&sch->dev))
  720. return;
  721. pseudo_sch = to_subchannel(cdev->dev.parent);
  722. /*
  723. * Try to move the ccw device to its new subchannel.
  724. * Note: device_move() changes cdev->dev.parent
  725. */
  726. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  727. if (ret) {
  728. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  729. "failed (ret=%d)!\n",
  730. cdev->private->dev_id.ssid,
  731. cdev->private->dev_id.devno, ret);
  732. /* Put reference for new parent. */
  733. put_device(&sch->dev);
  734. return;
  735. }
  736. sch_attach_device(sch, cdev);
  737. /* Put reference on pseudo subchannel. */
  738. put_device(&pseudo_sch->dev);
  739. }
  740. static void sch_create_and_recog_new_device(struct subchannel *sch)
  741. {
  742. struct ccw_device *cdev;
  743. /* Need to allocate a new ccw device. */
  744. cdev = io_subchannel_create_ccwdev(sch);
  745. if (IS_ERR(cdev)) {
  746. /* OK, we did everything we could... */
  747. css_sch_device_unregister(sch);
  748. return;
  749. }
  750. spin_lock_irq(sch->lock);
  751. sch_set_cdev(sch, cdev);
  752. spin_unlock_irq(sch->lock);
  753. /* Start recognition for the new ccw device. */
  754. if (io_subchannel_recog(cdev, sch)) {
  755. spin_lock_irq(sch->lock);
  756. sch_set_cdev(sch, NULL);
  757. spin_unlock_irq(sch->lock);
  758. css_sch_device_unregister(sch);
  759. /* Put reference from io_subchannel_create_ccwdev(). */
  760. put_device(&sch->dev);
  761. /* Give up initial reference. */
  762. put_device(&cdev->dev);
  763. }
  764. }
  765. void ccw_device_move_to_orphanage(struct work_struct *work)
  766. {
  767. struct ccw_device_private *priv;
  768. struct ccw_device *cdev;
  769. struct ccw_device *replacing_cdev;
  770. struct subchannel *sch;
  771. int ret;
  772. struct channel_subsystem *css;
  773. struct ccw_dev_id dev_id;
  774. priv = container_of(work, struct ccw_device_private, kick_work);
  775. cdev = priv->cdev;
  776. sch = to_subchannel(cdev->dev.parent);
  777. css = to_css(sch->dev.parent);
  778. dev_id.devno = sch->schib.pmcw.dev;
  779. dev_id.ssid = sch->schid.ssid;
  780. /* Increase refcount for pseudo subchannel. */
  781. get_device(&css->pseudo_subchannel->dev);
  782. /*
  783. * Move the orphaned ccw device to the orphanage so the replacing
  784. * ccw device can take its place on the subchannel.
  785. * Note: device_move() changes cdev->dev.parent
  786. */
  787. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
  788. DPM_ORDER_NONE);
  789. if (ret) {
  790. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  791. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  792. cdev->private->dev_id.devno, ret);
  793. /* Decrease refcount for pseudo subchannel again. */
  794. put_device(&css->pseudo_subchannel->dev);
  795. return;
  796. }
  797. cdev->ccwlock = css->pseudo_subchannel->lock;
  798. /*
  799. * Search for the replacing ccw device
  800. * - among the disconnected devices
  801. * - in the orphanage
  802. */
  803. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  804. if (replacing_cdev) {
  805. sch_attach_disconnected_device(sch, replacing_cdev);
  806. /* Release reference from get_disc_ccwdev_by_dev_id() */
  807. put_device(&replacing_cdev->dev);
  808. /* Release reference of subchannel from old cdev. */
  809. put_device(&sch->dev);
  810. return;
  811. }
  812. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  813. if (replacing_cdev) {
  814. sch_attach_orphaned_device(sch, replacing_cdev);
  815. /* Release reference from get_orphaned_ccwdev_by_dev_id() */
  816. put_device(&replacing_cdev->dev);
  817. /* Release reference of subchannel from old cdev. */
  818. put_device(&sch->dev);
  819. return;
  820. }
  821. sch_create_and_recog_new_device(sch);
  822. /* Release reference of subchannel from old cdev. */
  823. put_device(&sch->dev);
  824. }
  825. /*
  826. * Register recognized device.
  827. */
  828. static void
  829. io_subchannel_register(struct work_struct *work)
  830. {
  831. struct ccw_device_private *priv;
  832. struct ccw_device *cdev;
  833. struct subchannel *sch;
  834. int ret;
  835. unsigned long flags;
  836. priv = container_of(work, struct ccw_device_private, kick_work);
  837. cdev = priv->cdev;
  838. sch = to_subchannel(cdev->dev.parent);
  839. /*
  840. * Check if subchannel is still registered. It may have become
  841. * unregistered if a machine check hit us after finishing
  842. * device recognition but before the register work could be
  843. * queued.
  844. */
  845. if (!device_is_registered(&sch->dev))
  846. goto out_err;
  847. css_update_ssd_info(sch);
  848. /*
  849. * io_subchannel_register() will also be called after device
  850. * recognition has been done for a boxed device (which will already
  851. * be registered). We need to reprobe since we may now have sense id
  852. * information.
  853. */
  854. if (device_is_registered(&cdev->dev)) {
  855. if (!cdev->drv) {
  856. ret = device_reprobe(&cdev->dev);
  857. if (ret)
  858. /* We can't do much here. */
  859. CIO_MSG_EVENT(0, "device_reprobe() returned"
  860. " %d for 0.%x.%04x\n", ret,
  861. cdev->private->dev_id.ssid,
  862. cdev->private->dev_id.devno);
  863. }
  864. goto out;
  865. }
  866. /*
  867. * Now we know this subchannel will stay, we can throw
  868. * our delayed uevent.
  869. */
  870. dev_set_uevent_suppress(&sch->dev, 0);
  871. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  872. /* make it known to the system */
  873. ret = ccw_device_register(cdev);
  874. if (ret) {
  875. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  876. cdev->private->dev_id.ssid,
  877. cdev->private->dev_id.devno, ret);
  878. spin_lock_irqsave(sch->lock, flags);
  879. sch_set_cdev(sch, NULL);
  880. spin_unlock_irqrestore(sch->lock, flags);
  881. /* Release initial device reference. */
  882. put_device(&cdev->dev);
  883. goto out_err;
  884. }
  885. out:
  886. cdev->private->flags.recog_done = 1;
  887. wake_up(&cdev->private->wait_q);
  888. out_err:
  889. /* Release reference for workqueue processing. */
  890. put_device(&cdev->dev);
  891. if (atomic_dec_and_test(&ccw_device_init_count))
  892. wake_up(&ccw_device_init_wq);
  893. }
  894. static void ccw_device_call_sch_unregister(struct work_struct *work)
  895. {
  896. struct ccw_device_private *priv;
  897. struct ccw_device *cdev;
  898. struct subchannel *sch;
  899. priv = container_of(work, struct ccw_device_private, kick_work);
  900. cdev = priv->cdev;
  901. /* Get subchannel reference for local processing. */
  902. if (!get_device(cdev->dev.parent))
  903. return;
  904. sch = to_subchannel(cdev->dev.parent);
  905. css_sch_device_unregister(sch);
  906. /* Reset intparm to zeroes. */
  907. sch->config.intparm = 0;
  908. cio_commit_config(sch);
  909. /* Release cdev reference for workqueue processing.*/
  910. put_device(&cdev->dev);
  911. /* Release subchannel reference for local processing. */
  912. put_device(&sch->dev);
  913. }
  914. /*
  915. * subchannel recognition done. Called from the state machine.
  916. */
  917. void
  918. io_subchannel_recog_done(struct ccw_device *cdev)
  919. {
  920. struct subchannel *sch;
  921. if (css_init_done == 0) {
  922. cdev->private->flags.recog_done = 1;
  923. return;
  924. }
  925. switch (cdev->private->state) {
  926. case DEV_STATE_NOT_OPER:
  927. cdev->private->flags.recog_done = 1;
  928. /* Remove device found not operational. */
  929. if (!get_device(&cdev->dev))
  930. break;
  931. sch = to_subchannel(cdev->dev.parent);
  932. PREPARE_WORK(&cdev->private->kick_work,
  933. ccw_device_call_sch_unregister);
  934. queue_work(slow_path_wq, &cdev->private->kick_work);
  935. if (atomic_dec_and_test(&ccw_device_init_count))
  936. wake_up(&ccw_device_init_wq);
  937. break;
  938. case DEV_STATE_BOXED:
  939. /* Device did not respond in time. */
  940. case DEV_STATE_OFFLINE:
  941. /*
  942. * We can't register the device in interrupt context so
  943. * we schedule a work item.
  944. */
  945. if (!get_device(&cdev->dev))
  946. break;
  947. PREPARE_WORK(&cdev->private->kick_work,
  948. io_subchannel_register);
  949. queue_work(slow_path_wq, &cdev->private->kick_work);
  950. break;
  951. }
  952. }
  953. static int
  954. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  955. {
  956. int rc;
  957. struct ccw_device_private *priv;
  958. sch_set_cdev(sch, cdev);
  959. cdev->ccwlock = sch->lock;
  960. /* Init private data. */
  961. priv = cdev->private;
  962. priv->dev_id.devno = sch->schib.pmcw.dev;
  963. priv->dev_id.ssid = sch->schid.ssid;
  964. priv->schid = sch->schid;
  965. priv->state = DEV_STATE_NOT_OPER;
  966. INIT_LIST_HEAD(&priv->cmb_list);
  967. init_waitqueue_head(&priv->wait_q);
  968. init_timer(&priv->timer);
  969. /* Set an initial name for the device. */
  970. if (cio_is_console(sch->schid))
  971. cdev->dev.init_name = cio_get_console_cdev_name(sch);
  972. else
  973. dev_set_name(&cdev->dev, "0.%x.%04x",
  974. sch->schid.ssid, sch->schib.pmcw.dev);
  975. /* Increase counter of devices currently in recognition. */
  976. atomic_inc(&ccw_device_init_count);
  977. /* Start async. device sensing. */
  978. spin_lock_irq(sch->lock);
  979. rc = ccw_device_recognition(cdev);
  980. spin_unlock_irq(sch->lock);
  981. if (rc) {
  982. if (atomic_dec_and_test(&ccw_device_init_count))
  983. wake_up(&ccw_device_init_wq);
  984. }
  985. return rc;
  986. }
  987. static void ccw_device_move_to_sch(struct work_struct *work)
  988. {
  989. struct ccw_device_private *priv;
  990. int rc;
  991. struct subchannel *sch;
  992. struct ccw_device *cdev;
  993. struct subchannel *former_parent;
  994. priv = container_of(work, struct ccw_device_private, kick_work);
  995. sch = priv->sch;
  996. cdev = priv->cdev;
  997. former_parent = to_subchannel(cdev->dev.parent);
  998. /* Get reference for new parent. */
  999. if (!get_device(&sch->dev))
  1000. return;
  1001. mutex_lock(&sch->reg_mutex);
  1002. /*
  1003. * Try to move the ccw device to its new subchannel.
  1004. * Note: device_move() changes cdev->dev.parent
  1005. */
  1006. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  1007. mutex_unlock(&sch->reg_mutex);
  1008. if (rc) {
  1009. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  1010. "0.%x.%04x failed (ret=%d)!\n",
  1011. cdev->private->dev_id.ssid,
  1012. cdev->private->dev_id.devno, sch->schid.ssid,
  1013. sch->schid.sch_no, rc);
  1014. css_sch_device_unregister(sch);
  1015. /* Put reference for new parent again. */
  1016. put_device(&sch->dev);
  1017. goto out;
  1018. }
  1019. if (!sch_is_pseudo_sch(former_parent)) {
  1020. spin_lock_irq(former_parent->lock);
  1021. sch_set_cdev(former_parent, NULL);
  1022. spin_unlock_irq(former_parent->lock);
  1023. css_sch_device_unregister(former_parent);
  1024. /* Reset intparm to zeroes. */
  1025. former_parent->config.intparm = 0;
  1026. cio_commit_config(former_parent);
  1027. }
  1028. sch_attach_device(sch, cdev);
  1029. out:
  1030. /* Put reference for old parent. */
  1031. put_device(&former_parent->dev);
  1032. put_device(&cdev->dev);
  1033. }
  1034. static void io_subchannel_irq(struct subchannel *sch)
  1035. {
  1036. struct ccw_device *cdev;
  1037. cdev = sch_get_cdev(sch);
  1038. CIO_TRACE_EVENT(3, "IRQ");
  1039. CIO_TRACE_EVENT(3, dev_name(&sch->dev));
  1040. if (cdev)
  1041. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  1042. }
  1043. void io_subchannel_init_config(struct subchannel *sch)
  1044. {
  1045. memset(&sch->config, 0, sizeof(sch->config));
  1046. sch->config.csense = 1;
  1047. /* Use subchannel mp mode when there is more than 1 installed CHPID. */
  1048. if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
  1049. sch->config.mp = 1;
  1050. }
  1051. static void io_subchannel_init_fields(struct subchannel *sch)
  1052. {
  1053. if (cio_is_console(sch->schid))
  1054. sch->opm = 0xff;
  1055. else
  1056. sch->opm = chp_get_sch_opm(sch);
  1057. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  1058. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1059. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1060. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1061. sch->schib.pmcw.dev, sch->schid.ssid,
  1062. sch->schid.sch_no, sch->schib.pmcw.pim,
  1063. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1064. io_subchannel_init_config(sch);
  1065. }
  1066. static void io_subchannel_do_unreg(struct work_struct *work)
  1067. {
  1068. struct subchannel *sch;
  1069. sch = container_of(work, struct subchannel, work);
  1070. css_sch_device_unregister(sch);
  1071. /* Reset intparm to zeroes. */
  1072. sch->config.intparm = 0;
  1073. cio_commit_config(sch);
  1074. put_device(&sch->dev);
  1075. }
  1076. /* Schedule unregister if we have no cdev. */
  1077. static void io_subchannel_schedule_removal(struct subchannel *sch)
  1078. {
  1079. get_device(&sch->dev);
  1080. INIT_WORK(&sch->work, io_subchannel_do_unreg);
  1081. queue_work(slow_path_wq, &sch->work);
  1082. }
  1083. /*
  1084. * Note: We always return 0 so that we bind to the device even on error.
  1085. * This is needed so that our remove function is called on unregister.
  1086. */
  1087. static int io_subchannel_probe(struct subchannel *sch)
  1088. {
  1089. struct ccw_device *cdev;
  1090. int rc;
  1091. unsigned long flags;
  1092. struct ccw_dev_id dev_id;
  1093. cdev = sch_get_cdev(sch);
  1094. if (cdev) {
  1095. rc = sysfs_create_group(&sch->dev.kobj,
  1096. &io_subchannel_attr_group);
  1097. if (rc)
  1098. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1099. "attributes for subchannel "
  1100. "0.%x.%04x (rc=%d)\n",
  1101. sch->schid.ssid, sch->schid.sch_no, rc);
  1102. /*
  1103. * This subchannel already has an associated ccw_device.
  1104. * Throw the delayed uevent for the subchannel, register
  1105. * the ccw_device and exit. This happens for all early
  1106. * devices, e.g. the console.
  1107. */
  1108. dev_set_uevent_suppress(&sch->dev, 0);
  1109. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1110. cdev->dev.groups = ccwdev_attr_groups;
  1111. device_initialize(&cdev->dev);
  1112. ccw_device_register(cdev);
  1113. /*
  1114. * Check if the device is already online. If it is
  1115. * the reference count needs to be corrected since we
  1116. * didn't obtain a reference in ccw_device_set_online.
  1117. */
  1118. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1119. cdev->private->state != DEV_STATE_OFFLINE &&
  1120. cdev->private->state != DEV_STATE_BOXED)
  1121. get_device(&cdev->dev);
  1122. return 0;
  1123. }
  1124. io_subchannel_init_fields(sch);
  1125. rc = cio_commit_config(sch);
  1126. if (rc)
  1127. goto out_schedule;
  1128. rc = sysfs_create_group(&sch->dev.kobj,
  1129. &io_subchannel_attr_group);
  1130. if (rc)
  1131. goto out_schedule;
  1132. /* Allocate I/O subchannel private data. */
  1133. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1134. GFP_KERNEL | GFP_DMA);
  1135. if (!sch->private)
  1136. goto out_err;
  1137. /*
  1138. * First check if a fitting device may be found amongst the
  1139. * disconnected devices or in the orphanage.
  1140. */
  1141. dev_id.devno = sch->schib.pmcw.dev;
  1142. dev_id.ssid = sch->schid.ssid;
  1143. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1144. if (!cdev)
  1145. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1146. &dev_id);
  1147. if (cdev) {
  1148. /*
  1149. * Schedule moving the device until when we have a registered
  1150. * subchannel to move to and succeed the probe. We can
  1151. * unregister later again, when the probe is through.
  1152. */
  1153. cdev->private->sch = sch;
  1154. PREPARE_WORK(&cdev->private->kick_work,
  1155. ccw_device_move_to_sch);
  1156. queue_work(slow_path_wq, &cdev->private->kick_work);
  1157. return 0;
  1158. }
  1159. cdev = io_subchannel_create_ccwdev(sch);
  1160. if (IS_ERR(cdev))
  1161. goto out_err;
  1162. rc = io_subchannel_recog(cdev, sch);
  1163. if (rc) {
  1164. spin_lock_irqsave(sch->lock, flags);
  1165. io_subchannel_recog_done(cdev);
  1166. spin_unlock_irqrestore(sch->lock, flags);
  1167. }
  1168. return 0;
  1169. out_err:
  1170. kfree(sch->private);
  1171. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1172. out_schedule:
  1173. io_subchannel_schedule_removal(sch);
  1174. return 0;
  1175. }
  1176. static int
  1177. io_subchannel_remove (struct subchannel *sch)
  1178. {
  1179. struct ccw_device *cdev;
  1180. unsigned long flags;
  1181. cdev = sch_get_cdev(sch);
  1182. if (!cdev)
  1183. return 0;
  1184. /* Set ccw device to not operational and drop reference. */
  1185. spin_lock_irqsave(cdev->ccwlock, flags);
  1186. sch_set_cdev(sch, NULL);
  1187. cdev->private->state = DEV_STATE_NOT_OPER;
  1188. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1189. ccw_device_unregister(cdev);
  1190. put_device(&cdev->dev);
  1191. kfree(sch->private);
  1192. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1193. return 0;
  1194. }
  1195. static int io_subchannel_notify(struct subchannel *sch, int event)
  1196. {
  1197. struct ccw_device *cdev;
  1198. cdev = sch_get_cdev(sch);
  1199. if (!cdev)
  1200. return 0;
  1201. return ccw_device_notify(cdev, event);
  1202. }
  1203. static void io_subchannel_verify(struct subchannel *sch)
  1204. {
  1205. struct ccw_device *cdev;
  1206. cdev = sch_get_cdev(sch);
  1207. if (cdev)
  1208. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1209. }
  1210. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1211. {
  1212. if (cio_update_schib(sch))
  1213. return 0;
  1214. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1215. return 1;
  1216. return 0;
  1217. }
  1218. static void terminate_internal_io(struct subchannel *sch,
  1219. struct ccw_device *cdev)
  1220. {
  1221. if (cio_clear(sch)) {
  1222. /* Recheck device in case clear failed. */
  1223. sch->lpm = 0;
  1224. if (cdev->online)
  1225. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1226. else
  1227. css_schedule_eval(sch->schid);
  1228. return;
  1229. }
  1230. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1231. /* Request retry of internal operation. */
  1232. cdev->private->flags.intretry = 1;
  1233. /* Call handler. */
  1234. if (cdev->handler)
  1235. cdev->handler(cdev, cdev->private->intparm,
  1236. ERR_PTR(-EIO));
  1237. }
  1238. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1239. {
  1240. struct ccw_device *cdev;
  1241. cdev = sch_get_cdev(sch);
  1242. if (!cdev)
  1243. return;
  1244. if (check_for_io_on_path(sch, mask)) {
  1245. if (cdev->private->state == DEV_STATE_ONLINE)
  1246. ccw_device_kill_io(cdev);
  1247. else {
  1248. terminate_internal_io(sch, cdev);
  1249. /* Re-start path verification. */
  1250. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1251. }
  1252. } else
  1253. /* trigger path verification. */
  1254. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1255. }
  1256. static int io_subchannel_chp_event(struct subchannel *sch,
  1257. struct chp_link *link, int event)
  1258. {
  1259. int mask;
  1260. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1261. if (!mask)
  1262. return 0;
  1263. switch (event) {
  1264. case CHP_VARY_OFF:
  1265. sch->opm &= ~mask;
  1266. sch->lpm &= ~mask;
  1267. io_subchannel_terminate_path(sch, mask);
  1268. break;
  1269. case CHP_VARY_ON:
  1270. sch->opm |= mask;
  1271. sch->lpm |= mask;
  1272. io_subchannel_verify(sch);
  1273. break;
  1274. case CHP_OFFLINE:
  1275. if (cio_update_schib(sch))
  1276. return -ENODEV;
  1277. io_subchannel_terminate_path(sch, mask);
  1278. break;
  1279. case CHP_ONLINE:
  1280. if (cio_update_schib(sch))
  1281. return -ENODEV;
  1282. sch->lpm |= mask & sch->opm;
  1283. io_subchannel_verify(sch);
  1284. break;
  1285. }
  1286. return 0;
  1287. }
  1288. static void
  1289. io_subchannel_shutdown(struct subchannel *sch)
  1290. {
  1291. struct ccw_device *cdev;
  1292. int ret;
  1293. cdev = sch_get_cdev(sch);
  1294. if (cio_is_console(sch->schid))
  1295. return;
  1296. if (!sch->schib.pmcw.ena)
  1297. /* Nothing to do. */
  1298. return;
  1299. ret = cio_disable_subchannel(sch);
  1300. if (ret != -EBUSY)
  1301. /* Subchannel is disabled, we're done. */
  1302. return;
  1303. cdev->private->state = DEV_STATE_QUIESCE;
  1304. if (cdev->handler)
  1305. cdev->handler(cdev, cdev->private->intparm,
  1306. ERR_PTR(-EIO));
  1307. ret = ccw_device_cancel_halt_clear(cdev);
  1308. if (ret == -EBUSY) {
  1309. ccw_device_set_timeout(cdev, HZ/10);
  1310. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1311. }
  1312. cio_disable_subchannel(sch);
  1313. }
  1314. static int io_subchannel_get_status(struct subchannel *sch)
  1315. {
  1316. struct schib schib;
  1317. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1318. return CIO_GONE;
  1319. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1320. return CIO_REVALIDATE;
  1321. if (!sch->lpm)
  1322. return CIO_NO_PATH;
  1323. return CIO_OPER;
  1324. }
  1325. static int device_is_disconnected(struct ccw_device *cdev)
  1326. {
  1327. if (!cdev)
  1328. return 0;
  1329. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1330. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1331. }
  1332. static int recovery_check(struct device *dev, void *data)
  1333. {
  1334. struct ccw_device *cdev = to_ccwdev(dev);
  1335. int *redo = data;
  1336. spin_lock_irq(cdev->ccwlock);
  1337. switch (cdev->private->state) {
  1338. case DEV_STATE_DISCONNECTED:
  1339. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1340. cdev->private->dev_id.ssid,
  1341. cdev->private->dev_id.devno);
  1342. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1343. *redo = 1;
  1344. break;
  1345. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1346. *redo = 1;
  1347. break;
  1348. }
  1349. spin_unlock_irq(cdev->ccwlock);
  1350. return 0;
  1351. }
  1352. static void recovery_work_func(struct work_struct *unused)
  1353. {
  1354. int redo = 0;
  1355. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1356. if (redo) {
  1357. spin_lock_irq(&recovery_lock);
  1358. if (!timer_pending(&recovery_timer)) {
  1359. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1360. recovery_phase++;
  1361. mod_timer(&recovery_timer, jiffies +
  1362. recovery_delay[recovery_phase] * HZ);
  1363. }
  1364. spin_unlock_irq(&recovery_lock);
  1365. } else
  1366. CIO_MSG_EVENT(4, "recovery: end\n");
  1367. }
  1368. static DECLARE_WORK(recovery_work, recovery_work_func);
  1369. static void recovery_func(unsigned long data)
  1370. {
  1371. /*
  1372. * We can't do our recovery in softirq context and it's not
  1373. * performance critical, so we schedule it.
  1374. */
  1375. schedule_work(&recovery_work);
  1376. }
  1377. static void ccw_device_schedule_recovery(void)
  1378. {
  1379. unsigned long flags;
  1380. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1381. spin_lock_irqsave(&recovery_lock, flags);
  1382. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1383. recovery_phase = 0;
  1384. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1385. }
  1386. spin_unlock_irqrestore(&recovery_lock, flags);
  1387. }
  1388. static int purge_fn(struct device *dev, void *data)
  1389. {
  1390. struct ccw_device *cdev = to_ccwdev(dev);
  1391. struct ccw_device_private *priv = cdev->private;
  1392. int unreg;
  1393. spin_lock_irq(cdev->ccwlock);
  1394. unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
  1395. (priv->state == DEV_STATE_OFFLINE);
  1396. spin_unlock_irq(cdev->ccwlock);
  1397. if (!unreg)
  1398. goto out;
  1399. if (!get_device(&cdev->dev))
  1400. goto out;
  1401. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
  1402. priv->dev_id.devno);
  1403. PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister);
  1404. queue_work(slow_path_wq, &cdev->private->kick_work);
  1405. out:
  1406. /* Abort loop in case of pending signal. */
  1407. if (signal_pending(current))
  1408. return -EINTR;
  1409. return 0;
  1410. }
  1411. /**
  1412. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1413. *
  1414. * Unregister all ccw devices that are offline and on the blacklist.
  1415. */
  1416. int ccw_purge_blacklisted(void)
  1417. {
  1418. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1419. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1420. return 0;
  1421. }
  1422. static void device_set_disconnected(struct ccw_device *cdev)
  1423. {
  1424. if (!cdev)
  1425. return;
  1426. ccw_device_set_timeout(cdev, 0);
  1427. cdev->private->flags.fake_irb = 0;
  1428. cdev->private->state = DEV_STATE_DISCONNECTED;
  1429. if (cdev->online)
  1430. ccw_device_schedule_recovery();
  1431. }
  1432. void ccw_device_set_notoper(struct ccw_device *cdev)
  1433. {
  1434. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1435. CIO_TRACE_EVENT(2, "notoper");
  1436. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1437. ccw_device_set_timeout(cdev, 0);
  1438. cio_disable_subchannel(sch);
  1439. cdev->private->state = DEV_STATE_NOT_OPER;
  1440. }
  1441. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1442. {
  1443. int event, ret, disc;
  1444. unsigned long flags;
  1445. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
  1446. struct ccw_device *cdev;
  1447. spin_lock_irqsave(sch->lock, flags);
  1448. cdev = sch_get_cdev(sch);
  1449. disc = device_is_disconnected(cdev);
  1450. if (disc && slow) {
  1451. /* Disconnected devices are evaluated directly only.*/
  1452. spin_unlock_irqrestore(sch->lock, flags);
  1453. return 0;
  1454. }
  1455. /* No interrupt after machine check - kill pending timers. */
  1456. if (cdev)
  1457. ccw_device_set_timeout(cdev, 0);
  1458. if (!disc && !slow) {
  1459. /* Non-disconnected devices are evaluated on the slow path. */
  1460. spin_unlock_irqrestore(sch->lock, flags);
  1461. return -EAGAIN;
  1462. }
  1463. event = io_subchannel_get_status(sch);
  1464. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1465. sch->schid.ssid, sch->schid.sch_no, event,
  1466. disc ? "disconnected" : "normal",
  1467. slow ? "slow" : "fast");
  1468. /* Analyze subchannel status. */
  1469. action = NONE;
  1470. switch (event) {
  1471. case CIO_NO_PATH:
  1472. if (disc) {
  1473. /* Check if paths have become available. */
  1474. action = REPROBE;
  1475. break;
  1476. }
  1477. /* fall through */
  1478. case CIO_GONE:
  1479. /* Ask driver what to do with device. */
  1480. if (io_subchannel_notify(sch, event))
  1481. action = DISC;
  1482. else
  1483. action = UNREGISTER;
  1484. break;
  1485. case CIO_REVALIDATE:
  1486. /* Device will be removed, so no notify necessary. */
  1487. if (disc)
  1488. /* Reprobe because immediate unregister might block. */
  1489. action = REPROBE;
  1490. else
  1491. action = UNREGISTER_PROBE;
  1492. break;
  1493. case CIO_OPER:
  1494. if (disc)
  1495. /* Get device operational again. */
  1496. action = REPROBE;
  1497. break;
  1498. }
  1499. /* Perform action. */
  1500. ret = 0;
  1501. switch (action) {
  1502. case UNREGISTER:
  1503. case UNREGISTER_PROBE:
  1504. ccw_device_set_notoper(cdev);
  1505. /* Unregister device (will use subchannel lock). */
  1506. spin_unlock_irqrestore(sch->lock, flags);
  1507. css_sch_device_unregister(sch);
  1508. spin_lock_irqsave(sch->lock, flags);
  1509. /* Reset intparm to zeroes. */
  1510. sch->config.intparm = 0;
  1511. cio_commit_config(sch);
  1512. break;
  1513. case REPROBE:
  1514. ccw_device_trigger_reprobe(cdev);
  1515. break;
  1516. case DISC:
  1517. device_set_disconnected(cdev);
  1518. break;
  1519. default:
  1520. break;
  1521. }
  1522. spin_unlock_irqrestore(sch->lock, flags);
  1523. /* Probe if necessary. */
  1524. if (action == UNREGISTER_PROBE)
  1525. ret = css_probe_device(sch->schid);
  1526. return ret;
  1527. }
  1528. #ifdef CONFIG_CCW_CONSOLE
  1529. static struct ccw_device console_cdev;
  1530. static char console_cdev_name[10] = "0.x.xxxx";
  1531. static struct ccw_device_private console_private;
  1532. static int console_cdev_in_use;
  1533. static DEFINE_SPINLOCK(ccw_console_lock);
  1534. spinlock_t * cio_get_console_lock(void)
  1535. {
  1536. return &ccw_console_lock;
  1537. }
  1538. static int ccw_device_console_enable(struct ccw_device *cdev,
  1539. struct subchannel *sch)
  1540. {
  1541. int rc;
  1542. /* Attach subchannel private data. */
  1543. sch->private = cio_get_console_priv();
  1544. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1545. io_subchannel_init_fields(sch);
  1546. rc = cio_commit_config(sch);
  1547. if (rc)
  1548. return rc;
  1549. sch->driver = &io_subchannel_driver;
  1550. /* Initialize the ccw_device structure. */
  1551. cdev->dev.parent= &sch->dev;
  1552. rc = io_subchannel_recog(cdev, sch);
  1553. if (rc)
  1554. return rc;
  1555. /* Now wait for the async. recognition to come to an end. */
  1556. spin_lock_irq(cdev->ccwlock);
  1557. while (!dev_fsm_final_state(cdev))
  1558. wait_cons_dev();
  1559. rc = -EIO;
  1560. if (cdev->private->state != DEV_STATE_OFFLINE)
  1561. goto out_unlock;
  1562. ccw_device_online(cdev);
  1563. while (!dev_fsm_final_state(cdev))
  1564. wait_cons_dev();
  1565. if (cdev->private->state != DEV_STATE_ONLINE)
  1566. goto out_unlock;
  1567. rc = 0;
  1568. out_unlock:
  1569. spin_unlock_irq(cdev->ccwlock);
  1570. return 0;
  1571. }
  1572. struct ccw_device *
  1573. ccw_device_probe_console(void)
  1574. {
  1575. struct subchannel *sch;
  1576. int ret;
  1577. if (xchg(&console_cdev_in_use, 1) != 0)
  1578. return ERR_PTR(-EBUSY);
  1579. sch = cio_probe_console();
  1580. if (IS_ERR(sch)) {
  1581. console_cdev_in_use = 0;
  1582. return (void *) sch;
  1583. }
  1584. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1585. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1586. console_cdev.private = &console_private;
  1587. console_private.cdev = &console_cdev;
  1588. ret = ccw_device_console_enable(&console_cdev, sch);
  1589. if (ret) {
  1590. cio_release_console();
  1591. console_cdev_in_use = 0;
  1592. return ERR_PTR(ret);
  1593. }
  1594. console_cdev.online = 1;
  1595. return &console_cdev;
  1596. }
  1597. const char *cio_get_console_cdev_name(struct subchannel *sch)
  1598. {
  1599. snprintf(console_cdev_name, 10, "0.%x.%04x",
  1600. sch->schid.ssid, sch->schib.pmcw.dev);
  1601. return (const char *)console_cdev_name;
  1602. }
  1603. #endif
  1604. /*
  1605. * get ccw_device matching the busid, but only if owned by cdrv
  1606. */
  1607. static int
  1608. __ccwdev_check_busid(struct device *dev, void *id)
  1609. {
  1610. char *bus_id;
  1611. bus_id = id;
  1612. return (strcmp(bus_id, dev_name(dev)) == 0);
  1613. }
  1614. /**
  1615. * get_ccwdev_by_busid() - obtain device from a bus id
  1616. * @cdrv: driver the device is owned by
  1617. * @bus_id: bus id of the device to be searched
  1618. *
  1619. * This function searches all devices owned by @cdrv for a device with a bus
  1620. * id matching @bus_id.
  1621. * Returns:
  1622. * If a match is found, its reference count of the found device is increased
  1623. * and it is returned; else %NULL is returned.
  1624. */
  1625. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1626. const char *bus_id)
  1627. {
  1628. struct device *dev;
  1629. struct device_driver *drv;
  1630. drv = get_driver(&cdrv->driver);
  1631. if (!drv)
  1632. return NULL;
  1633. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1634. __ccwdev_check_busid);
  1635. put_driver(drv);
  1636. return dev ? to_ccwdev(dev) : NULL;
  1637. }
  1638. /************************** device driver handling ************************/
  1639. /* This is the implementation of the ccw_driver class. The probe, remove
  1640. * and release methods are initially very similar to the device_driver
  1641. * implementations, with the difference that they have ccw_device
  1642. * arguments.
  1643. *
  1644. * A ccw driver also contains the information that is needed for
  1645. * device matching.
  1646. */
  1647. static int
  1648. ccw_device_probe (struct device *dev)
  1649. {
  1650. struct ccw_device *cdev = to_ccwdev(dev);
  1651. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1652. int ret;
  1653. cdev->drv = cdrv; /* to let the driver call _set_online */
  1654. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1655. if (ret) {
  1656. cdev->drv = NULL;
  1657. return ret;
  1658. }
  1659. return 0;
  1660. }
  1661. static int
  1662. ccw_device_remove (struct device *dev)
  1663. {
  1664. struct ccw_device *cdev = to_ccwdev(dev);
  1665. struct ccw_driver *cdrv = cdev->drv;
  1666. int ret;
  1667. if (cdrv->remove)
  1668. cdrv->remove(cdev);
  1669. if (cdev->online) {
  1670. cdev->online = 0;
  1671. spin_lock_irq(cdev->ccwlock);
  1672. ret = ccw_device_offline(cdev);
  1673. spin_unlock_irq(cdev->ccwlock);
  1674. if (ret == 0)
  1675. wait_event(cdev->private->wait_q,
  1676. dev_fsm_final_state(cdev));
  1677. else
  1678. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1679. "device 0.%x.%04x\n",
  1680. ret, cdev->private->dev_id.ssid,
  1681. cdev->private->dev_id.devno);
  1682. /* Give up reference obtained in ccw_device_set_online(). */
  1683. put_device(&cdev->dev);
  1684. }
  1685. ccw_device_set_timeout(cdev, 0);
  1686. cdev->drv = NULL;
  1687. return 0;
  1688. }
  1689. static void ccw_device_shutdown(struct device *dev)
  1690. {
  1691. struct ccw_device *cdev;
  1692. cdev = to_ccwdev(dev);
  1693. if (cdev->drv && cdev->drv->shutdown)
  1694. cdev->drv->shutdown(cdev);
  1695. disable_cmf(cdev);
  1696. }
  1697. struct bus_type ccw_bus_type = {
  1698. .name = "ccw",
  1699. .match = ccw_bus_match,
  1700. .uevent = ccw_uevent,
  1701. .probe = ccw_device_probe,
  1702. .remove = ccw_device_remove,
  1703. .shutdown = ccw_device_shutdown,
  1704. };
  1705. /**
  1706. * ccw_driver_register() - register a ccw driver
  1707. * @cdriver: driver to be registered
  1708. *
  1709. * This function is mainly a wrapper around driver_register().
  1710. * Returns:
  1711. * %0 on success and a negative error value on failure.
  1712. */
  1713. int ccw_driver_register(struct ccw_driver *cdriver)
  1714. {
  1715. struct device_driver *drv = &cdriver->driver;
  1716. drv->bus = &ccw_bus_type;
  1717. drv->name = cdriver->name;
  1718. drv->owner = cdriver->owner;
  1719. return driver_register(drv);
  1720. }
  1721. /**
  1722. * ccw_driver_unregister() - deregister a ccw driver
  1723. * @cdriver: driver to be deregistered
  1724. *
  1725. * This function is mainly a wrapper around driver_unregister().
  1726. */
  1727. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1728. {
  1729. driver_unregister(&cdriver->driver);
  1730. }
  1731. /* Helper func for qdio. */
  1732. struct subchannel_id
  1733. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1734. {
  1735. struct subchannel *sch;
  1736. sch = to_subchannel(cdev->dev.parent);
  1737. return sch->schid;
  1738. }
  1739. MODULE_LICENSE("GPL");
  1740. EXPORT_SYMBOL(ccw_device_set_online);
  1741. EXPORT_SYMBOL(ccw_device_set_offline);
  1742. EXPORT_SYMBOL(ccw_driver_register);
  1743. EXPORT_SYMBOL(ccw_driver_unregister);
  1744. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1745. EXPORT_SYMBOL(ccw_bus_type);
  1746. EXPORT_SYMBOL(ccw_device_work);
  1747. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);