device.c 54 KB

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