device.c 54 KB

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