device.c 56 KB

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