device.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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/delay.h>
  21. #include <linux/timer.h>
  22. #include <linux/kernel_stat.h>
  23. #include <asm/ccwdev.h>
  24. #include <asm/cio.h>
  25. #include <asm/param.h> /* HZ */
  26. #include <asm/cmb.h>
  27. #include <asm/isc.h>
  28. #include "chp.h"
  29. #include "cio.h"
  30. #include "cio_debug.h"
  31. #include "css.h"
  32. #include "device.h"
  33. #include "ioasm.h"
  34. #include "io_sch.h"
  35. #include "blacklist.h"
  36. #include "chsc.h"
  37. static struct timer_list recovery_timer;
  38. static DEFINE_SPINLOCK(recovery_lock);
  39. static int recovery_phase;
  40. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  41. static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
  42. static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
  43. static struct bus_type ccw_bus_type;
  44. /******************* bus type handling ***********************/
  45. /* The Linux driver model distinguishes between a bus type and
  46. * the bus itself. Of course we only have one channel
  47. * subsystem driver and one channel system per machine, but
  48. * we still use the abstraction. T.R. says it's a good idea. */
  49. static int
  50. ccw_bus_match (struct device * dev, struct device_driver * drv)
  51. {
  52. struct ccw_device *cdev = to_ccwdev(dev);
  53. struct ccw_driver *cdrv = to_ccwdrv(drv);
  54. const struct ccw_device_id *ids = cdrv->ids, *found;
  55. if (!ids)
  56. return 0;
  57. found = ccw_device_id_match(ids, &cdev->id);
  58. if (!found)
  59. return 0;
  60. cdev->id.driver_info = found->driver_info;
  61. return 1;
  62. }
  63. /* Store modalias string delimited by prefix/suffix string into buffer with
  64. * specified size. Return length of resulting string (excluding trailing '\0')
  65. * even if string doesn't fit buffer (snprintf semantics). */
  66. static int snprint_alias(char *buf, size_t size,
  67. struct ccw_device_id *id, const char *suffix)
  68. {
  69. int len;
  70. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  71. if (len > size)
  72. return len;
  73. buf += len;
  74. size -= len;
  75. if (id->dev_type != 0)
  76. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  77. id->dev_model, suffix);
  78. else
  79. len += snprintf(buf, size, "dtdm%s", suffix);
  80. return len;
  81. }
  82. /* Set up environment variables for ccw device uevent. Return 0 on success,
  83. * non-zero otherwise. */
  84. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  85. {
  86. struct ccw_device *cdev = to_ccwdev(dev);
  87. struct ccw_device_id *id = &(cdev->id);
  88. int ret;
  89. char modalias_buf[30];
  90. /* CU_TYPE= */
  91. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  92. if (ret)
  93. return ret;
  94. /* CU_MODEL= */
  95. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  96. if (ret)
  97. return ret;
  98. /* The next two can be zero, that's ok for us */
  99. /* DEV_TYPE= */
  100. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  101. if (ret)
  102. return ret;
  103. /* DEV_MODEL= */
  104. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  105. if (ret)
  106. return ret;
  107. /* MODALIAS= */
  108. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  109. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  110. return ret;
  111. }
  112. static void io_subchannel_irq(struct subchannel *);
  113. static int io_subchannel_probe(struct subchannel *);
  114. static int io_subchannel_remove(struct subchannel *);
  115. static void io_subchannel_shutdown(struct subchannel *);
  116. static int io_subchannel_sch_event(struct subchannel *, int);
  117. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  118. int);
  119. static void recovery_func(unsigned long data);
  120. static struct css_device_id io_subchannel_ids[] = {
  121. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  122. { /* end of list */ },
  123. };
  124. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  125. static int io_subchannel_prepare(struct subchannel *sch)
  126. {
  127. struct ccw_device *cdev;
  128. /*
  129. * Don't allow suspend while a ccw device registration
  130. * is still outstanding.
  131. */
  132. cdev = sch_get_cdev(sch);
  133. if (cdev && !device_is_registered(&cdev->dev))
  134. return -EAGAIN;
  135. return 0;
  136. }
  137. static int io_subchannel_settle(void)
  138. {
  139. int ret;
  140. ret = wait_event_interruptible(ccw_device_init_wq,
  141. atomic_read(&ccw_device_init_count) == 0);
  142. if (ret)
  143. return -EINTR;
  144. flush_workqueue(cio_work_q);
  145. return 0;
  146. }
  147. static struct css_driver io_subchannel_driver = {
  148. .drv = {
  149. .owner = THIS_MODULE,
  150. .name = "io_subchannel",
  151. },
  152. .subchannel_type = io_subchannel_ids,
  153. .irq = io_subchannel_irq,
  154. .sch_event = io_subchannel_sch_event,
  155. .chp_event = io_subchannel_chp_event,
  156. .probe = io_subchannel_probe,
  157. .remove = io_subchannel_remove,
  158. .shutdown = io_subchannel_shutdown,
  159. .prepare = io_subchannel_prepare,
  160. .settle = io_subchannel_settle,
  161. };
  162. int __init io_subchannel_init(void)
  163. {
  164. int ret;
  165. setup_timer(&recovery_timer, recovery_func, 0);
  166. ret = bus_register(&ccw_bus_type);
  167. if (ret)
  168. return ret;
  169. ret = css_driver_register(&io_subchannel_driver);
  170. if (ret)
  171. bus_unregister(&ccw_bus_type);
  172. return ret;
  173. }
  174. /************************ device handling **************************/
  175. /*
  176. * A ccw_device has some interfaces in sysfs in addition to the
  177. * standard ones.
  178. * The following entries are designed to export the information which
  179. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  180. * are obvious, so they don't have an entry :)
  181. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  182. */
  183. static ssize_t
  184. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  185. {
  186. struct subchannel *sch = to_subchannel(dev);
  187. struct chsc_ssd_info *ssd = &sch->ssd_info;
  188. ssize_t ret = 0;
  189. int chp;
  190. int mask;
  191. for (chp = 0; chp < 8; chp++) {
  192. mask = 0x80 >> chp;
  193. if (ssd->path_mask & mask)
  194. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  195. else
  196. ret += sprintf(buf + ret, "00 ");
  197. }
  198. ret += sprintf (buf+ret, "\n");
  199. return min((ssize_t)PAGE_SIZE, ret);
  200. }
  201. static ssize_t
  202. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  203. {
  204. struct subchannel *sch = to_subchannel(dev);
  205. struct pmcw *pmcw = &sch->schib.pmcw;
  206. return sprintf (buf, "%02x %02x %02x\n",
  207. pmcw->pim, pmcw->pam, pmcw->pom);
  208. }
  209. static ssize_t
  210. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  211. {
  212. struct ccw_device *cdev = to_ccwdev(dev);
  213. struct ccw_device_id *id = &(cdev->id);
  214. if (id->dev_type != 0)
  215. return sprintf(buf, "%04x/%02x\n",
  216. id->dev_type, id->dev_model);
  217. else
  218. return sprintf(buf, "n/a\n");
  219. }
  220. static ssize_t
  221. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  222. {
  223. struct ccw_device *cdev = to_ccwdev(dev);
  224. struct ccw_device_id *id = &(cdev->id);
  225. return sprintf(buf, "%04x/%02x\n",
  226. id->cu_type, id->cu_model);
  227. }
  228. static ssize_t
  229. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  230. {
  231. struct ccw_device *cdev = to_ccwdev(dev);
  232. struct ccw_device_id *id = &(cdev->id);
  233. int len;
  234. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  235. return len > PAGE_SIZE ? PAGE_SIZE : len;
  236. }
  237. static ssize_t
  238. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  239. {
  240. struct ccw_device *cdev = to_ccwdev(dev);
  241. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  242. }
  243. int ccw_device_is_orphan(struct ccw_device *cdev)
  244. {
  245. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  246. }
  247. static void ccw_device_unregister(struct ccw_device *cdev)
  248. {
  249. if (device_is_registered(&cdev->dev)) {
  250. /* Undo device_add(). */
  251. device_del(&cdev->dev);
  252. }
  253. if (cdev->private->flags.initialized) {
  254. cdev->private->flags.initialized = 0;
  255. /* Release reference from device_initialize(). */
  256. put_device(&cdev->dev);
  257. }
  258. }
  259. static void io_subchannel_quiesce(struct subchannel *);
  260. /**
  261. * ccw_device_set_offline() - disable a ccw device for I/O
  262. * @cdev: target ccw device
  263. *
  264. * This function calls the driver's set_offline() function for @cdev, if
  265. * given, and then disables @cdev.
  266. * Returns:
  267. * %0 on success and a negative error value on failure.
  268. * Context:
  269. * enabled, ccw device lock not held
  270. */
  271. int ccw_device_set_offline(struct ccw_device *cdev)
  272. {
  273. struct subchannel *sch;
  274. int ret, state;
  275. if (!cdev)
  276. return -ENODEV;
  277. if (!cdev->online || !cdev->drv)
  278. return -EINVAL;
  279. if (cdev->drv->set_offline) {
  280. ret = cdev->drv->set_offline(cdev);
  281. if (ret != 0)
  282. return ret;
  283. }
  284. spin_lock_irq(cdev->ccwlock);
  285. sch = to_subchannel(cdev->dev.parent);
  286. cdev->online = 0;
  287. /* Wait until a final state or DISCONNECTED is reached */
  288. while (!dev_fsm_final_state(cdev) &&
  289. cdev->private->state != DEV_STATE_DISCONNECTED) {
  290. spin_unlock_irq(cdev->ccwlock);
  291. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  292. cdev->private->state == DEV_STATE_DISCONNECTED));
  293. spin_lock_irq(cdev->ccwlock);
  294. }
  295. do {
  296. ret = ccw_device_offline(cdev);
  297. if (!ret)
  298. break;
  299. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
  300. "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
  301. cdev->private->dev_id.devno);
  302. if (ret != -EBUSY)
  303. goto error;
  304. state = cdev->private->state;
  305. spin_unlock_irq(cdev->ccwlock);
  306. io_subchannel_quiesce(sch);
  307. spin_lock_irq(cdev->ccwlock);
  308. cdev->private->state = state;
  309. } while (ret == -EBUSY);
  310. spin_unlock_irq(cdev->ccwlock);
  311. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  312. cdev->private->state == DEV_STATE_DISCONNECTED));
  313. /* Inform the user if set offline failed. */
  314. if (cdev->private->state == DEV_STATE_BOXED) {
  315. pr_warning("%s: The device entered boxed state while "
  316. "being set offline\n", dev_name(&cdev->dev));
  317. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  318. pr_warning("%s: The device stopped operating while "
  319. "being set offline\n", dev_name(&cdev->dev));
  320. }
  321. /* Give up reference from ccw_device_set_online(). */
  322. put_device(&cdev->dev);
  323. return 0;
  324. error:
  325. cdev->private->state = DEV_STATE_OFFLINE;
  326. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  327. spin_unlock_irq(cdev->ccwlock);
  328. /* Give up reference from ccw_device_set_online(). */
  329. put_device(&cdev->dev);
  330. return -ENODEV;
  331. }
  332. /**
  333. * ccw_device_set_online() - enable a ccw device for I/O
  334. * @cdev: target ccw device
  335. *
  336. * This function first enables @cdev and then calls the driver's set_online()
  337. * function for @cdev, if given. If set_online() returns an error, @cdev is
  338. * disabled again.
  339. * Returns:
  340. * %0 on success and a negative error value on failure.
  341. * Context:
  342. * enabled, ccw device lock not held
  343. */
  344. int ccw_device_set_online(struct ccw_device *cdev)
  345. {
  346. int ret;
  347. int ret2;
  348. if (!cdev)
  349. return -ENODEV;
  350. if (cdev->online || !cdev->drv)
  351. return -EINVAL;
  352. /* Hold on to an extra reference while device is online. */
  353. if (!get_device(&cdev->dev))
  354. return -ENODEV;
  355. spin_lock_irq(cdev->ccwlock);
  356. ret = ccw_device_online(cdev);
  357. spin_unlock_irq(cdev->ccwlock);
  358. if (ret == 0)
  359. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  360. else {
  361. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  362. "device 0.%x.%04x\n",
  363. ret, cdev->private->dev_id.ssid,
  364. cdev->private->dev_id.devno);
  365. /* Give up online reference since onlining failed. */
  366. put_device(&cdev->dev);
  367. return ret;
  368. }
  369. spin_lock_irq(cdev->ccwlock);
  370. /* Check if online processing was successful */
  371. if ((cdev->private->state != DEV_STATE_ONLINE) &&
  372. (cdev->private->state != DEV_STATE_W4SENSE)) {
  373. spin_unlock_irq(cdev->ccwlock);
  374. /* Inform the user that set online failed. */
  375. if (cdev->private->state == DEV_STATE_BOXED) {
  376. pr_warning("%s: Setting the device online failed "
  377. "because it is boxed\n",
  378. dev_name(&cdev->dev));
  379. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  380. pr_warning("%s: Setting the device online failed "
  381. "because it is not operational\n",
  382. dev_name(&cdev->dev));
  383. }
  384. /* Give up online reference since onlining failed. */
  385. put_device(&cdev->dev);
  386. return -ENODEV;
  387. }
  388. spin_unlock_irq(cdev->ccwlock);
  389. if (cdev->drv->set_online)
  390. ret = cdev->drv->set_online(cdev);
  391. if (ret)
  392. goto rollback;
  393. spin_lock_irq(cdev->ccwlock);
  394. cdev->online = 1;
  395. spin_unlock_irq(cdev->ccwlock);
  396. return 0;
  397. rollback:
  398. spin_lock_irq(cdev->ccwlock);
  399. /* Wait until a final state or DISCONNECTED is reached */
  400. while (!dev_fsm_final_state(cdev) &&
  401. cdev->private->state != DEV_STATE_DISCONNECTED) {
  402. spin_unlock_irq(cdev->ccwlock);
  403. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  404. cdev->private->state == DEV_STATE_DISCONNECTED));
  405. spin_lock_irq(cdev->ccwlock);
  406. }
  407. ret2 = ccw_device_offline(cdev);
  408. if (ret2)
  409. goto error;
  410. spin_unlock_irq(cdev->ccwlock);
  411. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  412. cdev->private->state == DEV_STATE_DISCONNECTED));
  413. /* Give up online reference since onlining failed. */
  414. put_device(&cdev->dev);
  415. return ret;
  416. error:
  417. CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
  418. "device 0.%x.%04x\n",
  419. ret2, cdev->private->dev_id.ssid,
  420. cdev->private->dev_id.devno);
  421. cdev->private->state = DEV_STATE_OFFLINE;
  422. spin_unlock_irq(cdev->ccwlock);
  423. /* Give up online reference since onlining failed. */
  424. put_device(&cdev->dev);
  425. return ret;
  426. }
  427. static int online_store_handle_offline(struct ccw_device *cdev)
  428. {
  429. if (cdev->private->state == DEV_STATE_DISCONNECTED) {
  430. spin_lock_irq(cdev->ccwlock);
  431. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
  432. spin_unlock_irq(cdev->ccwlock);
  433. return 0;
  434. }
  435. if (cdev->drv && cdev->drv->set_offline)
  436. return ccw_device_set_offline(cdev);
  437. return -EINVAL;
  438. }
  439. static int online_store_recog_and_online(struct ccw_device *cdev)
  440. {
  441. /* Do device recognition, if needed. */
  442. if (cdev->private->state == DEV_STATE_BOXED) {
  443. spin_lock_irq(cdev->ccwlock);
  444. ccw_device_recognition(cdev);
  445. spin_unlock_irq(cdev->ccwlock);
  446. wait_event(cdev->private->wait_q,
  447. cdev->private->flags.recog_done);
  448. if (cdev->private->state != DEV_STATE_OFFLINE)
  449. /* recognition failed */
  450. return -EAGAIN;
  451. }
  452. if (cdev->drv && cdev->drv->set_online)
  453. return ccw_device_set_online(cdev);
  454. return -EINVAL;
  455. }
  456. static int online_store_handle_online(struct ccw_device *cdev, int force)
  457. {
  458. int ret;
  459. ret = online_store_recog_and_online(cdev);
  460. if (ret && !force)
  461. return ret;
  462. if (force && cdev->private->state == DEV_STATE_BOXED) {
  463. ret = ccw_device_stlck(cdev);
  464. if (ret)
  465. return ret;
  466. if (cdev->id.cu_type == 0)
  467. cdev->private->state = DEV_STATE_NOT_OPER;
  468. ret = online_store_recog_and_online(cdev);
  469. if (ret)
  470. return ret;
  471. }
  472. return 0;
  473. }
  474. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. struct ccw_device *cdev = to_ccwdev(dev);
  478. int force, ret;
  479. unsigned long i;
  480. /* Prevent conflict between multiple on-/offline processing requests. */
  481. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  482. return -EAGAIN;
  483. /* Prevent conflict between internal I/Os and on-/offline processing. */
  484. if (!dev_fsm_final_state(cdev) &&
  485. cdev->private->state != DEV_STATE_DISCONNECTED) {
  486. ret = -EAGAIN;
  487. goto out;
  488. }
  489. /* Prevent conflict between pending work and on-/offline processing.*/
  490. if (work_pending(&cdev->private->todo_work)) {
  491. ret = -EAGAIN;
  492. goto out;
  493. }
  494. if (!strncmp(buf, "force\n", count)) {
  495. force = 1;
  496. i = 1;
  497. ret = 0;
  498. } else {
  499. force = 0;
  500. ret = kstrtoul(buf, 16, &i);
  501. }
  502. if (ret)
  503. goto out;
  504. device_lock(dev);
  505. switch (i) {
  506. case 0:
  507. ret = online_store_handle_offline(cdev);
  508. break;
  509. case 1:
  510. ret = online_store_handle_online(cdev, force);
  511. break;
  512. default:
  513. ret = -EINVAL;
  514. }
  515. device_unlock(dev);
  516. out:
  517. atomic_set(&cdev->private->onoff, 0);
  518. return (ret < 0) ? ret : count;
  519. }
  520. static ssize_t
  521. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  522. {
  523. struct ccw_device *cdev = to_ccwdev(dev);
  524. struct subchannel *sch;
  525. if (ccw_device_is_orphan(cdev))
  526. return sprintf(buf, "no device\n");
  527. switch (cdev->private->state) {
  528. case DEV_STATE_BOXED:
  529. return sprintf(buf, "boxed\n");
  530. case DEV_STATE_DISCONNECTED:
  531. case DEV_STATE_DISCONNECTED_SENSE_ID:
  532. case DEV_STATE_NOT_OPER:
  533. sch = to_subchannel(dev->parent);
  534. if (!sch->lpm)
  535. return sprintf(buf, "no path\n");
  536. else
  537. return sprintf(buf, "no device\n");
  538. default:
  539. /* All other states considered fine. */
  540. return sprintf(buf, "good\n");
  541. }
  542. }
  543. static ssize_t
  544. initiate_logging(struct device *dev, struct device_attribute *attr,
  545. const char *buf, size_t count)
  546. {
  547. struct subchannel *sch = to_subchannel(dev);
  548. int rc;
  549. rc = chsc_siosl(sch->schid);
  550. if (rc < 0) {
  551. pr_warning("Logging for subchannel 0.%x.%04x failed with "
  552. "errno=%d\n",
  553. sch->schid.ssid, sch->schid.sch_no, rc);
  554. return rc;
  555. }
  556. pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
  557. sch->schid.ssid, sch->schid.sch_no);
  558. return count;
  559. }
  560. static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
  561. char *buf)
  562. {
  563. struct subchannel *sch = to_subchannel(dev);
  564. return sprintf(buf, "%02x\n", sch->vpm);
  565. }
  566. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  567. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  568. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  569. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  570. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  571. static DEVICE_ATTR(online, 0644, online_show, online_store);
  572. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  573. static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
  574. static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
  575. static struct attribute *io_subchannel_attrs[] = {
  576. &dev_attr_chpids.attr,
  577. &dev_attr_pimpampom.attr,
  578. &dev_attr_logging.attr,
  579. &dev_attr_vpm.attr,
  580. NULL,
  581. };
  582. static struct attribute_group io_subchannel_attr_group = {
  583. .attrs = io_subchannel_attrs,
  584. };
  585. static struct attribute * ccwdev_attrs[] = {
  586. &dev_attr_devtype.attr,
  587. &dev_attr_cutype.attr,
  588. &dev_attr_modalias.attr,
  589. &dev_attr_online.attr,
  590. &dev_attr_cmb_enable.attr,
  591. &dev_attr_availability.attr,
  592. NULL,
  593. };
  594. static struct attribute_group ccwdev_attr_group = {
  595. .attrs = ccwdev_attrs,
  596. };
  597. static const struct attribute_group *ccwdev_attr_groups[] = {
  598. &ccwdev_attr_group,
  599. NULL,
  600. };
  601. static int ccw_device_add(struct ccw_device *cdev)
  602. {
  603. struct device *dev = &cdev->dev;
  604. dev->bus = &ccw_bus_type;
  605. return device_add(dev);
  606. }
  607. static int match_dev_id(struct device *dev, void *data)
  608. {
  609. struct ccw_device *cdev = to_ccwdev(dev);
  610. struct ccw_dev_id *dev_id = data;
  611. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  612. }
  613. /**
  614. * get_ccwdev_by_dev_id() - obtain device from a ccw device id
  615. * @dev_id: id of the device to be searched
  616. *
  617. * This function searches all devices attached to the ccw bus for a device
  618. * matching @dev_id.
  619. * Returns:
  620. * If a device is found its reference count is increased and returned;
  621. * else %NULL is returned.
  622. */
  623. struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
  624. {
  625. struct device *dev;
  626. dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
  627. return dev ? to_ccwdev(dev) : NULL;
  628. }
  629. EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
  630. static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
  631. {
  632. int ret;
  633. if (device_is_registered(&cdev->dev)) {
  634. device_release_driver(&cdev->dev);
  635. ret = device_attach(&cdev->dev);
  636. WARN_ON(ret == -ENODEV);
  637. }
  638. }
  639. static void
  640. ccw_device_release(struct device *dev)
  641. {
  642. struct ccw_device *cdev;
  643. cdev = to_ccwdev(dev);
  644. /* Release reference of parent subchannel. */
  645. put_device(cdev->dev.parent);
  646. kfree(cdev->private);
  647. kfree(cdev);
  648. }
  649. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  650. {
  651. struct ccw_device *cdev;
  652. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  653. if (cdev) {
  654. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  655. GFP_KERNEL | GFP_DMA);
  656. if (cdev->private)
  657. return cdev;
  658. }
  659. kfree(cdev);
  660. return ERR_PTR(-ENOMEM);
  661. }
  662. static void ccw_device_todo(struct work_struct *work);
  663. static int io_subchannel_initialize_dev(struct subchannel *sch,
  664. struct ccw_device *cdev)
  665. {
  666. struct ccw_device_private *priv = cdev->private;
  667. int ret;
  668. priv->cdev = cdev;
  669. priv->int_class = IRQIO_CIO;
  670. priv->state = DEV_STATE_NOT_OPER;
  671. priv->dev_id.devno = sch->schib.pmcw.dev;
  672. priv->dev_id.ssid = sch->schid.ssid;
  673. priv->schid = sch->schid;
  674. INIT_WORK(&priv->todo_work, ccw_device_todo);
  675. INIT_LIST_HEAD(&priv->cmb_list);
  676. init_waitqueue_head(&priv->wait_q);
  677. init_timer(&priv->timer);
  678. atomic_set(&priv->onoff, 0);
  679. cdev->ccwlock = sch->lock;
  680. cdev->dev.parent = &sch->dev;
  681. cdev->dev.release = ccw_device_release;
  682. cdev->dev.groups = ccwdev_attr_groups;
  683. /* Do first half of device_register. */
  684. device_initialize(&cdev->dev);
  685. ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
  686. cdev->private->dev_id.devno);
  687. if (ret)
  688. goto out_put;
  689. if (!get_device(&sch->dev)) {
  690. ret = -ENODEV;
  691. goto out_put;
  692. }
  693. priv->flags.initialized = 1;
  694. spin_lock_irq(sch->lock);
  695. sch_set_cdev(sch, cdev);
  696. spin_unlock_irq(sch->lock);
  697. return 0;
  698. out_put:
  699. /* Release reference from device_initialize(). */
  700. put_device(&cdev->dev);
  701. return ret;
  702. }
  703. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  704. {
  705. struct ccw_device *cdev;
  706. int ret;
  707. cdev = io_subchannel_allocate_dev(sch);
  708. if (!IS_ERR(cdev)) {
  709. ret = io_subchannel_initialize_dev(sch, cdev);
  710. if (ret)
  711. cdev = ERR_PTR(ret);
  712. }
  713. return cdev;
  714. }
  715. static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
  716. static void sch_create_and_recog_new_device(struct subchannel *sch)
  717. {
  718. struct ccw_device *cdev;
  719. /* Need to allocate a new ccw device. */
  720. cdev = io_subchannel_create_ccwdev(sch);
  721. if (IS_ERR(cdev)) {
  722. /* OK, we did everything we could... */
  723. css_sch_device_unregister(sch);
  724. return;
  725. }
  726. /* Start recognition for the new ccw device. */
  727. io_subchannel_recog(cdev, sch);
  728. }
  729. /*
  730. * Register recognized device.
  731. */
  732. static void io_subchannel_register(struct ccw_device *cdev)
  733. {
  734. struct subchannel *sch;
  735. int ret, adjust_init_count = 1;
  736. unsigned long flags;
  737. sch = to_subchannel(cdev->dev.parent);
  738. /*
  739. * Check if subchannel is still registered. It may have become
  740. * unregistered if a machine check hit us after finishing
  741. * device recognition but before the register work could be
  742. * queued.
  743. */
  744. if (!device_is_registered(&sch->dev))
  745. goto out_err;
  746. css_update_ssd_info(sch);
  747. /*
  748. * io_subchannel_register() will also be called after device
  749. * recognition has been done for a boxed device (which will already
  750. * be registered). We need to reprobe since we may now have sense id
  751. * information.
  752. */
  753. if (device_is_registered(&cdev->dev)) {
  754. if (!cdev->drv) {
  755. ret = device_reprobe(&cdev->dev);
  756. if (ret)
  757. /* We can't do much here. */
  758. CIO_MSG_EVENT(0, "device_reprobe() returned"
  759. " %d for 0.%x.%04x\n", ret,
  760. cdev->private->dev_id.ssid,
  761. cdev->private->dev_id.devno);
  762. }
  763. adjust_init_count = 0;
  764. goto out;
  765. }
  766. /*
  767. * Now we know this subchannel will stay, we can throw
  768. * our delayed uevent.
  769. */
  770. dev_set_uevent_suppress(&sch->dev, 0);
  771. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  772. /* make it known to the system */
  773. ret = ccw_device_add(cdev);
  774. if (ret) {
  775. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  776. cdev->private->dev_id.ssid,
  777. cdev->private->dev_id.devno, ret);
  778. spin_lock_irqsave(sch->lock, flags);
  779. sch_set_cdev(sch, NULL);
  780. spin_unlock_irqrestore(sch->lock, flags);
  781. /* Release initial device reference. */
  782. put_device(&cdev->dev);
  783. goto out_err;
  784. }
  785. out:
  786. cdev->private->flags.recog_done = 1;
  787. wake_up(&cdev->private->wait_q);
  788. out_err:
  789. if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
  790. wake_up(&ccw_device_init_wq);
  791. }
  792. static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
  793. {
  794. struct subchannel *sch;
  795. /* Get subchannel reference for local processing. */
  796. if (!get_device(cdev->dev.parent))
  797. return;
  798. sch = to_subchannel(cdev->dev.parent);
  799. css_sch_device_unregister(sch);
  800. /* Release subchannel reference for local processing. */
  801. put_device(&sch->dev);
  802. }
  803. /*
  804. * subchannel recognition done. Called from the state machine.
  805. */
  806. void
  807. io_subchannel_recog_done(struct ccw_device *cdev)
  808. {
  809. if (css_init_done == 0) {
  810. cdev->private->flags.recog_done = 1;
  811. return;
  812. }
  813. switch (cdev->private->state) {
  814. case DEV_STATE_BOXED:
  815. /* Device did not respond in time. */
  816. case DEV_STATE_NOT_OPER:
  817. cdev->private->flags.recog_done = 1;
  818. /* Remove device found not operational. */
  819. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  820. if (atomic_dec_and_test(&ccw_device_init_count))
  821. wake_up(&ccw_device_init_wq);
  822. break;
  823. case DEV_STATE_OFFLINE:
  824. /*
  825. * We can't register the device in interrupt context so
  826. * we schedule a work item.
  827. */
  828. ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
  829. break;
  830. }
  831. }
  832. static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  833. {
  834. /* Increase counter of devices currently in recognition. */
  835. atomic_inc(&ccw_device_init_count);
  836. /* Start async. device sensing. */
  837. spin_lock_irq(sch->lock);
  838. ccw_device_recognition(cdev);
  839. spin_unlock_irq(sch->lock);
  840. }
  841. static int ccw_device_move_to_sch(struct ccw_device *cdev,
  842. struct subchannel *sch)
  843. {
  844. struct subchannel *old_sch;
  845. int rc, old_enabled = 0;
  846. old_sch = to_subchannel(cdev->dev.parent);
  847. /* Obtain child reference for new parent. */
  848. if (!get_device(&sch->dev))
  849. return -ENODEV;
  850. if (!sch_is_pseudo_sch(old_sch)) {
  851. spin_lock_irq(old_sch->lock);
  852. old_enabled = old_sch->schib.pmcw.ena;
  853. rc = 0;
  854. if (old_enabled)
  855. rc = cio_disable_subchannel(old_sch);
  856. spin_unlock_irq(old_sch->lock);
  857. if (rc == -EBUSY) {
  858. /* Release child reference for new parent. */
  859. put_device(&sch->dev);
  860. return rc;
  861. }
  862. }
  863. mutex_lock(&sch->reg_mutex);
  864. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  865. mutex_unlock(&sch->reg_mutex);
  866. if (rc) {
  867. CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
  868. cdev->private->dev_id.ssid,
  869. cdev->private->dev_id.devno, sch->schid.ssid,
  870. sch->schib.pmcw.dev, rc);
  871. if (old_enabled) {
  872. /* Try to reenable the old subchannel. */
  873. spin_lock_irq(old_sch->lock);
  874. cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
  875. spin_unlock_irq(old_sch->lock);
  876. }
  877. /* Release child reference for new parent. */
  878. put_device(&sch->dev);
  879. return rc;
  880. }
  881. /* Clean up old subchannel. */
  882. if (!sch_is_pseudo_sch(old_sch)) {
  883. spin_lock_irq(old_sch->lock);
  884. sch_set_cdev(old_sch, NULL);
  885. spin_unlock_irq(old_sch->lock);
  886. css_schedule_eval(old_sch->schid);
  887. }
  888. /* Release child reference for old parent. */
  889. put_device(&old_sch->dev);
  890. /* Initialize new subchannel. */
  891. spin_lock_irq(sch->lock);
  892. cdev->private->schid = sch->schid;
  893. cdev->ccwlock = sch->lock;
  894. if (!sch_is_pseudo_sch(sch))
  895. sch_set_cdev(sch, cdev);
  896. spin_unlock_irq(sch->lock);
  897. if (!sch_is_pseudo_sch(sch))
  898. css_update_ssd_info(sch);
  899. return 0;
  900. }
  901. static int ccw_device_move_to_orph(struct ccw_device *cdev)
  902. {
  903. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  904. struct channel_subsystem *css = to_css(sch->dev.parent);
  905. return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
  906. }
  907. static void io_subchannel_irq(struct subchannel *sch)
  908. {
  909. struct ccw_device *cdev;
  910. cdev = sch_get_cdev(sch);
  911. CIO_TRACE_EVENT(6, "IRQ");
  912. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  913. if (cdev)
  914. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  915. else
  916. inc_irq_stat(IRQIO_CIO);
  917. }
  918. void io_subchannel_init_config(struct subchannel *sch)
  919. {
  920. memset(&sch->config, 0, sizeof(sch->config));
  921. sch->config.csense = 1;
  922. }
  923. static void io_subchannel_init_fields(struct subchannel *sch)
  924. {
  925. if (cio_is_console(sch->schid))
  926. sch->opm = 0xff;
  927. else
  928. sch->opm = chp_get_sch_opm(sch);
  929. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  930. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  931. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  932. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  933. sch->schib.pmcw.dev, sch->schid.ssid,
  934. sch->schid.sch_no, sch->schib.pmcw.pim,
  935. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  936. io_subchannel_init_config(sch);
  937. }
  938. /*
  939. * Note: We always return 0 so that we bind to the device even on error.
  940. * This is needed so that our remove function is called on unregister.
  941. */
  942. static int io_subchannel_probe(struct subchannel *sch)
  943. {
  944. struct io_subchannel_private *io_priv;
  945. struct ccw_device *cdev;
  946. int rc;
  947. if (cio_is_console(sch->schid)) {
  948. rc = sysfs_create_group(&sch->dev.kobj,
  949. &io_subchannel_attr_group);
  950. if (rc)
  951. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  952. "attributes for subchannel "
  953. "0.%x.%04x (rc=%d)\n",
  954. sch->schid.ssid, sch->schid.sch_no, rc);
  955. /*
  956. * The console subchannel already has an associated ccw_device.
  957. * Throw the delayed uevent for the subchannel, register
  958. * the ccw_device and exit.
  959. */
  960. dev_set_uevent_suppress(&sch->dev, 0);
  961. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  962. cdev = sch_get_cdev(sch);
  963. rc = ccw_device_add(cdev);
  964. if (rc) {
  965. /* Release online reference. */
  966. put_device(&cdev->dev);
  967. goto out_schedule;
  968. }
  969. if (atomic_dec_and_test(&ccw_device_init_count))
  970. wake_up(&ccw_device_init_wq);
  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. static void ccw_device_set_int_class(struct ccw_device *cdev)
  1414. {
  1415. struct ccw_driver *cdrv = cdev->drv;
  1416. /* Note: we interpret class 0 in this context as an uninitialized
  1417. * field since it translates to a non-I/O interrupt class. */
  1418. if (cdrv->int_class != 0)
  1419. cdev->private->int_class = cdrv->int_class;
  1420. else
  1421. cdev->private->int_class = IRQIO_CIO;
  1422. }
  1423. #ifdef CONFIG_CCW_CONSOLE
  1424. int __init ccw_device_enable_console(struct ccw_device *cdev)
  1425. {
  1426. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1427. int rc;
  1428. if (!cdev->drv || !cdev->handler)
  1429. return -EINVAL;
  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. io_subchannel_recog(cdev, sch);
  1436. /* Now wait for the async. recognition to come to an end. */
  1437. spin_lock_irq(cdev->ccwlock);
  1438. while (!dev_fsm_final_state(cdev))
  1439. ccw_device_wait_idle(cdev);
  1440. /* Hold on to an extra reference while device is online. */
  1441. get_device(&cdev->dev);
  1442. rc = ccw_device_online(cdev);
  1443. if (rc)
  1444. goto out_unlock;
  1445. while (!dev_fsm_final_state(cdev))
  1446. ccw_device_wait_idle(cdev);
  1447. if (cdev->private->state == DEV_STATE_ONLINE)
  1448. cdev->online = 1;
  1449. else
  1450. rc = -EIO;
  1451. out_unlock:
  1452. spin_unlock_irq(cdev->ccwlock);
  1453. if (rc) /* Give up online reference since onlining failed. */
  1454. put_device(&cdev->dev);
  1455. return rc;
  1456. }
  1457. struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
  1458. {
  1459. struct io_subchannel_private *io_priv;
  1460. struct ccw_device *cdev;
  1461. struct subchannel *sch;
  1462. sch = cio_probe_console();
  1463. if (IS_ERR(sch))
  1464. return ERR_CAST(sch);
  1465. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  1466. if (!io_priv) {
  1467. put_device(&sch->dev);
  1468. return ERR_PTR(-ENOMEM);
  1469. }
  1470. set_io_private(sch, io_priv);
  1471. cdev = io_subchannel_create_ccwdev(sch);
  1472. if (IS_ERR(cdev)) {
  1473. put_device(&sch->dev);
  1474. kfree(io_priv);
  1475. return cdev;
  1476. }
  1477. cdev->drv = drv;
  1478. ccw_device_set_int_class(cdev);
  1479. return cdev;
  1480. }
  1481. void __init ccw_device_destroy_console(struct ccw_device *cdev)
  1482. {
  1483. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1484. struct io_subchannel_private *io_priv = to_io_private(sch);
  1485. set_io_private(sch, NULL);
  1486. put_device(&sch->dev);
  1487. put_device(&cdev->dev);
  1488. kfree(io_priv);
  1489. }
  1490. /**
  1491. * ccw_device_wait_idle() - busy wait for device to become idle
  1492. * @cdev: ccw device
  1493. *
  1494. * Poll until activity control is zero, that is, no function or data
  1495. * transfer is pending/active.
  1496. * Called with device lock being held.
  1497. */
  1498. void ccw_device_wait_idle(struct ccw_device *cdev)
  1499. {
  1500. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1501. while (1) {
  1502. cio_tsch(sch);
  1503. if (sch->schib.scsw.cmd.actl == 0)
  1504. break;
  1505. udelay_simple(100);
  1506. }
  1507. }
  1508. static int ccw_device_pm_restore(struct device *dev);
  1509. int ccw_device_force_console(struct ccw_device *cdev)
  1510. {
  1511. return ccw_device_pm_restore(&cdev->dev);
  1512. }
  1513. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1514. #endif
  1515. /*
  1516. * get ccw_device matching the busid, but only if owned by cdrv
  1517. */
  1518. static int
  1519. __ccwdev_check_busid(struct device *dev, void *id)
  1520. {
  1521. char *bus_id;
  1522. bus_id = id;
  1523. return (strcmp(bus_id, dev_name(dev)) == 0);
  1524. }
  1525. /**
  1526. * get_ccwdev_by_busid() - obtain device from a bus id
  1527. * @cdrv: driver the device is owned by
  1528. * @bus_id: bus id of the device to be searched
  1529. *
  1530. * This function searches all devices owned by @cdrv for a device with a bus
  1531. * id matching @bus_id.
  1532. * Returns:
  1533. * If a match is found, its reference count of the found device is increased
  1534. * and it is returned; else %NULL is returned.
  1535. */
  1536. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1537. const char *bus_id)
  1538. {
  1539. struct device *dev;
  1540. dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
  1541. __ccwdev_check_busid);
  1542. return dev ? to_ccwdev(dev) : NULL;
  1543. }
  1544. /************************** device driver handling ************************/
  1545. /* This is the implementation of the ccw_driver class. The probe, remove
  1546. * and release methods are initially very similar to the device_driver
  1547. * implementations, with the difference that they have ccw_device
  1548. * arguments.
  1549. *
  1550. * A ccw driver also contains the information that is needed for
  1551. * device matching.
  1552. */
  1553. static int
  1554. ccw_device_probe (struct device *dev)
  1555. {
  1556. struct ccw_device *cdev = to_ccwdev(dev);
  1557. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1558. int ret;
  1559. cdev->drv = cdrv; /* to let the driver call _set_online */
  1560. ccw_device_set_int_class(cdev);
  1561. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1562. if (ret) {
  1563. cdev->drv = NULL;
  1564. cdev->private->int_class = IRQIO_CIO;
  1565. return ret;
  1566. }
  1567. return 0;
  1568. }
  1569. static int ccw_device_remove(struct device *dev)
  1570. {
  1571. struct ccw_device *cdev = to_ccwdev(dev);
  1572. struct ccw_driver *cdrv = cdev->drv;
  1573. int ret;
  1574. if (cdrv->remove)
  1575. cdrv->remove(cdev);
  1576. spin_lock_irq(cdev->ccwlock);
  1577. if (cdev->online) {
  1578. cdev->online = 0;
  1579. ret = ccw_device_offline(cdev);
  1580. spin_unlock_irq(cdev->ccwlock);
  1581. if (ret == 0)
  1582. wait_event(cdev->private->wait_q,
  1583. dev_fsm_final_state(cdev));
  1584. else
  1585. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1586. "device 0.%x.%04x\n",
  1587. ret, cdev->private->dev_id.ssid,
  1588. cdev->private->dev_id.devno);
  1589. /* Give up reference obtained in ccw_device_set_online(). */
  1590. put_device(&cdev->dev);
  1591. spin_lock_irq(cdev->ccwlock);
  1592. }
  1593. ccw_device_set_timeout(cdev, 0);
  1594. cdev->drv = NULL;
  1595. cdev->private->int_class = IRQIO_CIO;
  1596. spin_unlock_irq(cdev->ccwlock);
  1597. __disable_cmf(cdev);
  1598. return 0;
  1599. }
  1600. static void ccw_device_shutdown(struct device *dev)
  1601. {
  1602. struct ccw_device *cdev;
  1603. cdev = to_ccwdev(dev);
  1604. if (cdev->drv && cdev->drv->shutdown)
  1605. cdev->drv->shutdown(cdev);
  1606. __disable_cmf(cdev);
  1607. }
  1608. static int ccw_device_pm_prepare(struct device *dev)
  1609. {
  1610. struct ccw_device *cdev = to_ccwdev(dev);
  1611. if (work_pending(&cdev->private->todo_work))
  1612. return -EAGAIN;
  1613. /* Fail while device is being set online/offline. */
  1614. if (atomic_read(&cdev->private->onoff))
  1615. return -EAGAIN;
  1616. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1617. return cdev->drv->prepare(cdev);
  1618. return 0;
  1619. }
  1620. static void ccw_device_pm_complete(struct device *dev)
  1621. {
  1622. struct ccw_device *cdev = to_ccwdev(dev);
  1623. if (cdev->online && cdev->drv && cdev->drv->complete)
  1624. cdev->drv->complete(cdev);
  1625. }
  1626. static int ccw_device_pm_freeze(struct device *dev)
  1627. {
  1628. struct ccw_device *cdev = to_ccwdev(dev);
  1629. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1630. int ret, cm_enabled;
  1631. /* Fail suspend while device is in transistional state. */
  1632. if (!dev_fsm_final_state(cdev))
  1633. return -EAGAIN;
  1634. if (!cdev->online)
  1635. return 0;
  1636. if (cdev->drv && cdev->drv->freeze) {
  1637. ret = cdev->drv->freeze(cdev);
  1638. if (ret)
  1639. return ret;
  1640. }
  1641. spin_lock_irq(sch->lock);
  1642. cm_enabled = cdev->private->cmb != NULL;
  1643. spin_unlock_irq(sch->lock);
  1644. if (cm_enabled) {
  1645. /* Don't have the css write on memory. */
  1646. ret = ccw_set_cmf(cdev, 0);
  1647. if (ret)
  1648. return ret;
  1649. }
  1650. /* From here on, disallow device driver I/O. */
  1651. spin_lock_irq(sch->lock);
  1652. ret = cio_disable_subchannel(sch);
  1653. spin_unlock_irq(sch->lock);
  1654. return ret;
  1655. }
  1656. static int ccw_device_pm_thaw(struct device *dev)
  1657. {
  1658. struct ccw_device *cdev = to_ccwdev(dev);
  1659. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1660. int ret, cm_enabled;
  1661. if (!cdev->online)
  1662. return 0;
  1663. spin_lock_irq(sch->lock);
  1664. /* Allow device driver I/O again. */
  1665. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1666. cm_enabled = cdev->private->cmb != NULL;
  1667. spin_unlock_irq(sch->lock);
  1668. if (ret)
  1669. return ret;
  1670. if (cm_enabled) {
  1671. ret = ccw_set_cmf(cdev, 1);
  1672. if (ret)
  1673. return ret;
  1674. }
  1675. if (cdev->drv && cdev->drv->thaw)
  1676. ret = cdev->drv->thaw(cdev);
  1677. return ret;
  1678. }
  1679. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1680. {
  1681. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1682. spin_lock_irq(sch->lock);
  1683. if (cio_is_console(sch->schid)) {
  1684. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1685. goto out_unlock;
  1686. }
  1687. /*
  1688. * While we were sleeping, devices may have gone or become
  1689. * available again. Kick re-detection.
  1690. */
  1691. cdev->private->flags.resuming = 1;
  1692. cdev->private->path_new_mask = LPM_ANYPATH;
  1693. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  1694. spin_unlock_irq(sch->lock);
  1695. css_wait_for_slow_path();
  1696. /* cdev may have been moved to a different subchannel. */
  1697. sch = to_subchannel(cdev->dev.parent);
  1698. spin_lock_irq(sch->lock);
  1699. if (cdev->private->state != DEV_STATE_ONLINE &&
  1700. cdev->private->state != DEV_STATE_OFFLINE)
  1701. goto out_unlock;
  1702. ccw_device_recognition(cdev);
  1703. spin_unlock_irq(sch->lock);
  1704. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1705. cdev->private->state == DEV_STATE_DISCONNECTED);
  1706. spin_lock_irq(sch->lock);
  1707. out_unlock:
  1708. cdev->private->flags.resuming = 0;
  1709. spin_unlock_irq(sch->lock);
  1710. }
  1711. static int resume_handle_boxed(struct ccw_device *cdev)
  1712. {
  1713. cdev->private->state = DEV_STATE_BOXED;
  1714. if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
  1715. return 0;
  1716. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1717. return -ENODEV;
  1718. }
  1719. static int resume_handle_disc(struct ccw_device *cdev)
  1720. {
  1721. cdev->private->state = DEV_STATE_DISCONNECTED;
  1722. if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
  1723. return 0;
  1724. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1725. return -ENODEV;
  1726. }
  1727. static int ccw_device_pm_restore(struct device *dev)
  1728. {
  1729. struct ccw_device *cdev = to_ccwdev(dev);
  1730. struct subchannel *sch;
  1731. int ret = 0;
  1732. __ccw_device_pm_restore(cdev);
  1733. sch = to_subchannel(cdev->dev.parent);
  1734. spin_lock_irq(sch->lock);
  1735. if (cio_is_console(sch->schid))
  1736. goto out_restore;
  1737. /* check recognition results */
  1738. switch (cdev->private->state) {
  1739. case DEV_STATE_OFFLINE:
  1740. case DEV_STATE_ONLINE:
  1741. cdev->private->flags.donotify = 0;
  1742. break;
  1743. case DEV_STATE_BOXED:
  1744. ret = resume_handle_boxed(cdev);
  1745. if (ret)
  1746. goto out_unlock;
  1747. goto out_restore;
  1748. default:
  1749. ret = resume_handle_disc(cdev);
  1750. if (ret)
  1751. goto out_unlock;
  1752. goto out_restore;
  1753. }
  1754. /* check if the device type has changed */
  1755. if (!ccw_device_test_sense_data(cdev)) {
  1756. ccw_device_update_sense_data(cdev);
  1757. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  1758. ret = -ENODEV;
  1759. goto out_unlock;
  1760. }
  1761. if (!cdev->online)
  1762. goto out_unlock;
  1763. if (ccw_device_online(cdev)) {
  1764. ret = resume_handle_disc(cdev);
  1765. if (ret)
  1766. goto out_unlock;
  1767. goto out_restore;
  1768. }
  1769. spin_unlock_irq(sch->lock);
  1770. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1771. spin_lock_irq(sch->lock);
  1772. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
  1773. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1774. ret = -ENODEV;
  1775. goto out_unlock;
  1776. }
  1777. /* reenable cmf, if needed */
  1778. if (cdev->private->cmb) {
  1779. spin_unlock_irq(sch->lock);
  1780. ret = ccw_set_cmf(cdev, 1);
  1781. spin_lock_irq(sch->lock);
  1782. if (ret) {
  1783. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1784. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1785. cdev->private->dev_id.devno, ret);
  1786. ret = 0;
  1787. }
  1788. }
  1789. out_restore:
  1790. spin_unlock_irq(sch->lock);
  1791. if (cdev->online && cdev->drv && cdev->drv->restore)
  1792. ret = cdev->drv->restore(cdev);
  1793. return ret;
  1794. out_unlock:
  1795. spin_unlock_irq(sch->lock);
  1796. return ret;
  1797. }
  1798. static const struct dev_pm_ops ccw_pm_ops = {
  1799. .prepare = ccw_device_pm_prepare,
  1800. .complete = ccw_device_pm_complete,
  1801. .freeze = ccw_device_pm_freeze,
  1802. .thaw = ccw_device_pm_thaw,
  1803. .restore = ccw_device_pm_restore,
  1804. };
  1805. static struct bus_type ccw_bus_type = {
  1806. .name = "ccw",
  1807. .match = ccw_bus_match,
  1808. .uevent = ccw_uevent,
  1809. .probe = ccw_device_probe,
  1810. .remove = ccw_device_remove,
  1811. .shutdown = ccw_device_shutdown,
  1812. .pm = &ccw_pm_ops,
  1813. };
  1814. /**
  1815. * ccw_driver_register() - register a ccw driver
  1816. * @cdriver: driver to be registered
  1817. *
  1818. * This function is mainly a wrapper around driver_register().
  1819. * Returns:
  1820. * %0 on success and a negative error value on failure.
  1821. */
  1822. int ccw_driver_register(struct ccw_driver *cdriver)
  1823. {
  1824. struct device_driver *drv = &cdriver->driver;
  1825. drv->bus = &ccw_bus_type;
  1826. return driver_register(drv);
  1827. }
  1828. /**
  1829. * ccw_driver_unregister() - deregister a ccw driver
  1830. * @cdriver: driver to be deregistered
  1831. *
  1832. * This function is mainly a wrapper around driver_unregister().
  1833. */
  1834. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1835. {
  1836. driver_unregister(&cdriver->driver);
  1837. }
  1838. static void ccw_device_todo(struct work_struct *work)
  1839. {
  1840. struct ccw_device_private *priv;
  1841. struct ccw_device *cdev;
  1842. struct subchannel *sch;
  1843. enum cdev_todo todo;
  1844. priv = container_of(work, struct ccw_device_private, todo_work);
  1845. cdev = priv->cdev;
  1846. sch = to_subchannel(cdev->dev.parent);
  1847. /* Find out todo. */
  1848. spin_lock_irq(cdev->ccwlock);
  1849. todo = priv->todo;
  1850. priv->todo = CDEV_TODO_NOTHING;
  1851. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1852. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1853. spin_unlock_irq(cdev->ccwlock);
  1854. /* Perform todo. */
  1855. switch (todo) {
  1856. case CDEV_TODO_ENABLE_CMF:
  1857. cmf_reenable(cdev);
  1858. break;
  1859. case CDEV_TODO_REBIND:
  1860. ccw_device_do_unbind_bind(cdev);
  1861. break;
  1862. case CDEV_TODO_REGISTER:
  1863. io_subchannel_register(cdev);
  1864. break;
  1865. case CDEV_TODO_UNREG_EVAL:
  1866. if (!sch_is_pseudo_sch(sch))
  1867. css_schedule_eval(sch->schid);
  1868. /* fall-through */
  1869. case CDEV_TODO_UNREG:
  1870. if (sch_is_pseudo_sch(sch))
  1871. ccw_device_unregister(cdev);
  1872. else
  1873. ccw_device_call_sch_unregister(cdev);
  1874. break;
  1875. default:
  1876. break;
  1877. }
  1878. /* Release workqueue ref. */
  1879. put_device(&cdev->dev);
  1880. }
  1881. /**
  1882. * ccw_device_sched_todo - schedule ccw device operation
  1883. * @cdev: ccw device
  1884. * @todo: todo
  1885. *
  1886. * Schedule the operation identified by @todo to be performed on the slow path
  1887. * workqueue. Do nothing if another operation with higher priority is already
  1888. * scheduled. Needs to be called with ccwdev lock held.
  1889. */
  1890. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1891. {
  1892. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1893. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1894. todo);
  1895. if (cdev->private->todo >= todo)
  1896. return;
  1897. cdev->private->todo = todo;
  1898. /* Get workqueue ref. */
  1899. if (!get_device(&cdev->dev))
  1900. return;
  1901. if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
  1902. /* Already queued, release workqueue ref. */
  1903. put_device(&cdev->dev);
  1904. }
  1905. }
  1906. /**
  1907. * ccw_device_siosl() - initiate logging
  1908. * @cdev: ccw device
  1909. *
  1910. * This function is used to invoke model-dependent logging within the channel
  1911. * subsystem.
  1912. */
  1913. int ccw_device_siosl(struct ccw_device *cdev)
  1914. {
  1915. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1916. return chsc_siosl(sch->schid);
  1917. }
  1918. EXPORT_SYMBOL_GPL(ccw_device_siosl);
  1919. MODULE_LICENSE("GPL");
  1920. EXPORT_SYMBOL(ccw_device_set_online);
  1921. EXPORT_SYMBOL(ccw_device_set_offline);
  1922. EXPORT_SYMBOL(ccw_driver_register);
  1923. EXPORT_SYMBOL(ccw_driver_unregister);
  1924. EXPORT_SYMBOL(get_ccwdev_by_busid);