device.c 50 KB

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