device.c 51 KB

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