virtio_ccw.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * ccw based virtio transport
  3. *
  4. * Copyright IBM Corp. 2012, 2014
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  11. */
  12. #include <linux/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/err.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/pfn.h>
  22. #include <linux/async.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/io.h>
  28. #include <linux/kvm_para.h>
  29. #include <linux/notifier.h>
  30. #include <asm/diag.h>
  31. #include <asm/setup.h>
  32. #include <asm/irq.h>
  33. #include <asm/cio.h>
  34. #include <asm/ccwdev.h>
  35. #include <asm/virtio-ccw.h>
  36. #include <asm/isc.h>
  37. #include <asm/airq.h>
  38. /*
  39. * virtio related functions
  40. */
  41. struct vq_config_block {
  42. __u16 index;
  43. __u16 num;
  44. } __packed;
  45. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  46. /* same as PCI config space size, should be enough for all drivers */
  47. struct virtio_ccw_device {
  48. struct virtio_device vdev;
  49. __u8 *status;
  50. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  51. struct ccw_device *cdev;
  52. __u32 curr_io;
  53. int err;
  54. unsigned int revision; /* Transport revision */
  55. wait_queue_head_t wait_q;
  56. spinlock_t lock;
  57. struct list_head virtqueues;
  58. unsigned long indicators;
  59. unsigned long indicators2;
  60. struct vq_config_block *config_block;
  61. bool is_thinint;
  62. bool going_away;
  63. bool device_lost;
  64. unsigned int config_ready;
  65. void *airq_info;
  66. };
  67. struct vq_info_block_legacy {
  68. __u64 queue;
  69. __u32 align;
  70. __u16 index;
  71. __u16 num;
  72. } __packed;
  73. struct vq_info_block {
  74. __u64 desc;
  75. __u32 res0;
  76. __u16 index;
  77. __u16 num;
  78. __u64 avail;
  79. __u64 used;
  80. } __packed;
  81. struct virtio_feature_desc {
  82. __u32 features;
  83. __u8 index;
  84. } __packed;
  85. struct virtio_thinint_area {
  86. unsigned long summary_indicator;
  87. unsigned long indicator;
  88. u64 bit_nr;
  89. u8 isc;
  90. } __packed;
  91. struct virtio_rev_info {
  92. __u16 revision;
  93. __u16 length;
  94. __u8 data[];
  95. };
  96. /* the highest virtio-ccw revision we support */
  97. #define VIRTIO_CCW_REV_MAX 1
  98. struct virtio_ccw_vq_info {
  99. struct virtqueue *vq;
  100. int num;
  101. void *queue;
  102. union {
  103. struct vq_info_block s;
  104. struct vq_info_block_legacy l;
  105. } *info_block;
  106. int bit_nr;
  107. struct list_head node;
  108. long cookie;
  109. };
  110. #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
  111. #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
  112. #define MAX_AIRQ_AREAS 20
  113. static int virtio_ccw_use_airq = 1;
  114. struct airq_info {
  115. rwlock_t lock;
  116. u8 summary_indicator;
  117. struct airq_struct airq;
  118. struct airq_iv *aiv;
  119. };
  120. static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
  121. #define CCW_CMD_SET_VQ 0x13
  122. #define CCW_CMD_VDEV_RESET 0x33
  123. #define CCW_CMD_SET_IND 0x43
  124. #define CCW_CMD_SET_CONF_IND 0x53
  125. #define CCW_CMD_READ_FEAT 0x12
  126. #define CCW_CMD_WRITE_FEAT 0x11
  127. #define CCW_CMD_READ_CONF 0x22
  128. #define CCW_CMD_WRITE_CONF 0x21
  129. #define CCW_CMD_WRITE_STATUS 0x31
  130. #define CCW_CMD_READ_VQ_CONF 0x32
  131. #define CCW_CMD_READ_STATUS 0x72
  132. #define CCW_CMD_SET_IND_ADAPTER 0x73
  133. #define CCW_CMD_SET_VIRTIO_REV 0x83
  134. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  135. #define VIRTIO_CCW_DOING_RESET 0x00040000
  136. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  137. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  138. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  139. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  140. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  141. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  142. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  143. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  144. #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
  145. #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
  146. #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
  147. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  148. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  149. {
  150. return container_of(vdev, struct virtio_ccw_device, vdev);
  151. }
  152. static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
  153. {
  154. unsigned long i, flags;
  155. write_lock_irqsave(&info->lock, flags);
  156. for (i = 0; i < airq_iv_end(info->aiv); i++) {
  157. if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
  158. airq_iv_free_bit(info->aiv, i);
  159. airq_iv_set_ptr(info->aiv, i, 0);
  160. break;
  161. }
  162. }
  163. write_unlock_irqrestore(&info->lock, flags);
  164. }
  165. static void virtio_airq_handler(struct airq_struct *airq)
  166. {
  167. struct airq_info *info = container_of(airq, struct airq_info, airq);
  168. unsigned long ai;
  169. inc_irq_stat(IRQIO_VAI);
  170. read_lock(&info->lock);
  171. /* Walk through indicators field, summary indicator active. */
  172. for (ai = 0;;) {
  173. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  174. if (ai == -1UL)
  175. break;
  176. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  177. }
  178. info->summary_indicator = 0;
  179. smp_wmb();
  180. /* Walk through indicators field, summary indicator not active. */
  181. for (ai = 0;;) {
  182. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  183. if (ai == -1UL)
  184. break;
  185. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  186. }
  187. read_unlock(&info->lock);
  188. }
  189. static struct airq_info *new_airq_info(void)
  190. {
  191. struct airq_info *info;
  192. int rc;
  193. info = kzalloc(sizeof(*info), GFP_KERNEL);
  194. if (!info)
  195. return NULL;
  196. rwlock_init(&info->lock);
  197. info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
  198. if (!info->aiv) {
  199. kfree(info);
  200. return NULL;
  201. }
  202. info->airq.handler = virtio_airq_handler;
  203. info->airq.lsi_ptr = &info->summary_indicator;
  204. info->airq.lsi_mask = 0xff;
  205. info->airq.isc = VIRTIO_AIRQ_ISC;
  206. rc = register_adapter_interrupt(&info->airq);
  207. if (rc) {
  208. airq_iv_release(info->aiv);
  209. kfree(info);
  210. return NULL;
  211. }
  212. return info;
  213. }
  214. static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
  215. u64 *first, void **airq_info)
  216. {
  217. int i, j;
  218. struct airq_info *info;
  219. unsigned long indicator_addr = 0;
  220. unsigned long bit, flags;
  221. for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
  222. if (!airq_areas[i])
  223. airq_areas[i] = new_airq_info();
  224. info = airq_areas[i];
  225. if (!info)
  226. return 0;
  227. write_lock_irqsave(&info->lock, flags);
  228. bit = airq_iv_alloc(info->aiv, nvqs);
  229. if (bit == -1UL) {
  230. /* Not enough vacancies. */
  231. write_unlock_irqrestore(&info->lock, flags);
  232. continue;
  233. }
  234. *first = bit;
  235. *airq_info = info;
  236. indicator_addr = (unsigned long)info->aiv->vector;
  237. for (j = 0; j < nvqs; j++) {
  238. airq_iv_set_ptr(info->aiv, bit + j,
  239. (unsigned long)vqs[j]);
  240. }
  241. write_unlock_irqrestore(&info->lock, flags);
  242. }
  243. return indicator_addr;
  244. }
  245. static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
  246. {
  247. struct virtio_ccw_vq_info *info;
  248. list_for_each_entry(info, &vcdev->virtqueues, node)
  249. drop_airq_indicator(info->vq, vcdev->airq_info);
  250. }
  251. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  252. {
  253. unsigned long flags;
  254. __u32 ret;
  255. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  256. if (vcdev->err)
  257. ret = 0;
  258. else
  259. ret = vcdev->curr_io & flag;
  260. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  261. return ret;
  262. }
  263. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  264. struct ccw1 *ccw, __u32 intparm)
  265. {
  266. int ret;
  267. unsigned long flags;
  268. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  269. do {
  270. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  271. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  272. if (!ret) {
  273. if (!vcdev->curr_io)
  274. vcdev->err = 0;
  275. vcdev->curr_io |= flag;
  276. }
  277. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  278. cpu_relax();
  279. } while (ret == -EBUSY);
  280. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  281. return ret ? ret : vcdev->err;
  282. }
  283. static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
  284. struct ccw1 *ccw)
  285. {
  286. int ret;
  287. unsigned long *indicatorp = NULL;
  288. struct virtio_thinint_area *thinint_area = NULL;
  289. struct airq_info *airq_info = vcdev->airq_info;
  290. if (vcdev->is_thinint) {
  291. thinint_area = kzalloc(sizeof(*thinint_area),
  292. GFP_DMA | GFP_KERNEL);
  293. if (!thinint_area)
  294. return;
  295. thinint_area->summary_indicator =
  296. (unsigned long) &airq_info->summary_indicator;
  297. thinint_area->isc = VIRTIO_AIRQ_ISC;
  298. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  299. ccw->count = sizeof(*thinint_area);
  300. ccw->cda = (__u32)(unsigned long) thinint_area;
  301. } else {
  302. /* payload is the address of the indicators */
  303. indicatorp = kmalloc(sizeof(&vcdev->indicators),
  304. GFP_DMA | GFP_KERNEL);
  305. if (!indicatorp)
  306. return;
  307. *indicatorp = 0;
  308. ccw->cmd_code = CCW_CMD_SET_IND;
  309. ccw->count = sizeof(&vcdev->indicators);
  310. ccw->cda = (__u32)(unsigned long) indicatorp;
  311. }
  312. /* Deregister indicators from host. */
  313. vcdev->indicators = 0;
  314. ccw->flags = 0;
  315. ret = ccw_io_helper(vcdev, ccw,
  316. vcdev->is_thinint ?
  317. VIRTIO_CCW_DOING_SET_IND_ADAPTER :
  318. VIRTIO_CCW_DOING_SET_IND);
  319. if (ret && (ret != -ENODEV))
  320. dev_info(&vcdev->cdev->dev,
  321. "Failed to deregister indicators (%d)\n", ret);
  322. else if (vcdev->is_thinint)
  323. virtio_ccw_drop_indicators(vcdev);
  324. kfree(indicatorp);
  325. kfree(thinint_area);
  326. }
  327. static inline long __do_kvm_notify(struct subchannel_id schid,
  328. unsigned long queue_index,
  329. long cookie)
  330. {
  331. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  332. register struct subchannel_id __schid asm("2") = schid;
  333. register unsigned long __index asm("3") = queue_index;
  334. register long __rc asm("2");
  335. register long __cookie asm("4") = cookie;
  336. asm volatile ("diag 2,4,0x500\n"
  337. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
  338. "d"(__cookie)
  339. : "memory", "cc");
  340. return __rc;
  341. }
  342. static inline long do_kvm_notify(struct subchannel_id schid,
  343. unsigned long queue_index,
  344. long cookie)
  345. {
  346. diag_stat_inc(DIAG_STAT_X500);
  347. return __do_kvm_notify(schid, queue_index, cookie);
  348. }
  349. static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
  350. {
  351. struct virtio_ccw_vq_info *info = vq->priv;
  352. struct virtio_ccw_device *vcdev;
  353. struct subchannel_id schid;
  354. vcdev = to_vc_device(info->vq->vdev);
  355. ccw_device_get_schid(vcdev->cdev, &schid);
  356. info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
  357. if (info->cookie < 0)
  358. return false;
  359. return true;
  360. }
  361. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  362. struct ccw1 *ccw, int index)
  363. {
  364. int ret;
  365. vcdev->config_block->index = index;
  366. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  367. ccw->flags = 0;
  368. ccw->count = sizeof(struct vq_config_block);
  369. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  370. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  371. if (ret)
  372. return ret;
  373. return vcdev->config_block->num;
  374. }
  375. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  376. {
  377. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  378. struct virtio_ccw_vq_info *info = vq->priv;
  379. unsigned long flags;
  380. unsigned long size;
  381. int ret;
  382. unsigned int index = vq->index;
  383. /* Remove from our list. */
  384. spin_lock_irqsave(&vcdev->lock, flags);
  385. list_del(&info->node);
  386. spin_unlock_irqrestore(&vcdev->lock, flags);
  387. /* Release from host. */
  388. if (vcdev->revision == 0) {
  389. info->info_block->l.queue = 0;
  390. info->info_block->l.align = 0;
  391. info->info_block->l.index = index;
  392. info->info_block->l.num = 0;
  393. ccw->count = sizeof(info->info_block->l);
  394. } else {
  395. info->info_block->s.desc = 0;
  396. info->info_block->s.index = index;
  397. info->info_block->s.num = 0;
  398. info->info_block->s.avail = 0;
  399. info->info_block->s.used = 0;
  400. ccw->count = sizeof(info->info_block->s);
  401. }
  402. ccw->cmd_code = CCW_CMD_SET_VQ;
  403. ccw->flags = 0;
  404. ccw->cda = (__u32)(unsigned long)(info->info_block);
  405. ret = ccw_io_helper(vcdev, ccw,
  406. VIRTIO_CCW_DOING_SET_VQ | index);
  407. /*
  408. * -ENODEV isn't considered an error: The device is gone anyway.
  409. * This may happen on device detach.
  410. */
  411. if (ret && (ret != -ENODEV))
  412. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
  413. ret, index);
  414. vring_del_virtqueue(vq);
  415. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  416. free_pages_exact(info->queue, size);
  417. kfree(info->info_block);
  418. kfree(info);
  419. }
  420. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  421. {
  422. struct virtqueue *vq, *n;
  423. struct ccw1 *ccw;
  424. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  425. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  426. if (!ccw)
  427. return;
  428. virtio_ccw_drop_indicator(vcdev, ccw);
  429. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  430. virtio_ccw_del_vq(vq, ccw);
  431. kfree(ccw);
  432. }
  433. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  434. int i, vq_callback_t *callback,
  435. const char *name,
  436. struct ccw1 *ccw)
  437. {
  438. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  439. int err;
  440. struct virtqueue *vq = NULL;
  441. struct virtio_ccw_vq_info *info;
  442. unsigned long size = 0; /* silence the compiler */
  443. unsigned long flags;
  444. /* Allocate queue. */
  445. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  446. if (!info) {
  447. dev_warn(&vcdev->cdev->dev, "no info\n");
  448. err = -ENOMEM;
  449. goto out_err;
  450. }
  451. info->info_block = kzalloc(sizeof(*info->info_block),
  452. GFP_DMA | GFP_KERNEL);
  453. if (!info->info_block) {
  454. dev_warn(&vcdev->cdev->dev, "no info block\n");
  455. err = -ENOMEM;
  456. goto out_err;
  457. }
  458. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  459. if (info->num < 0) {
  460. err = info->num;
  461. goto out_err;
  462. }
  463. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  464. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  465. if (info->queue == NULL) {
  466. dev_warn(&vcdev->cdev->dev, "no queue\n");
  467. err = -ENOMEM;
  468. goto out_err;
  469. }
  470. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  471. true, info->queue, virtio_ccw_kvm_notify,
  472. callback, name);
  473. if (!vq) {
  474. /* For now, we fail if we can't get the requested size. */
  475. dev_warn(&vcdev->cdev->dev, "no vq\n");
  476. err = -ENOMEM;
  477. goto out_err;
  478. }
  479. /* Register it with the host. */
  480. if (vcdev->revision == 0) {
  481. info->info_block->l.queue = (__u64)info->queue;
  482. info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
  483. info->info_block->l.index = i;
  484. info->info_block->l.num = info->num;
  485. ccw->count = sizeof(info->info_block->l);
  486. } else {
  487. info->info_block->s.desc = (__u64)info->queue;
  488. info->info_block->s.index = i;
  489. info->info_block->s.num = info->num;
  490. info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
  491. info->info_block->s.used = (__u64)virtqueue_get_used(vq);
  492. ccw->count = sizeof(info->info_block->s);
  493. }
  494. ccw->cmd_code = CCW_CMD_SET_VQ;
  495. ccw->flags = 0;
  496. ccw->cda = (__u32)(unsigned long)(info->info_block);
  497. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  498. if (err) {
  499. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  500. goto out_err;
  501. }
  502. info->vq = vq;
  503. vq->priv = info;
  504. /* Save it to our list. */
  505. spin_lock_irqsave(&vcdev->lock, flags);
  506. list_add(&info->node, &vcdev->virtqueues);
  507. spin_unlock_irqrestore(&vcdev->lock, flags);
  508. return vq;
  509. out_err:
  510. if (vq)
  511. vring_del_virtqueue(vq);
  512. if (info) {
  513. if (info->queue)
  514. free_pages_exact(info->queue, size);
  515. kfree(info->info_block);
  516. }
  517. kfree(info);
  518. return ERR_PTR(err);
  519. }
  520. static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
  521. struct virtqueue *vqs[], int nvqs,
  522. struct ccw1 *ccw)
  523. {
  524. int ret;
  525. struct virtio_thinint_area *thinint_area = NULL;
  526. struct airq_info *info;
  527. thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
  528. if (!thinint_area) {
  529. ret = -ENOMEM;
  530. goto out;
  531. }
  532. /* Try to get an indicator. */
  533. thinint_area->indicator = get_airq_indicator(vqs, nvqs,
  534. &thinint_area->bit_nr,
  535. &vcdev->airq_info);
  536. if (!thinint_area->indicator) {
  537. ret = -ENOSPC;
  538. goto out;
  539. }
  540. info = vcdev->airq_info;
  541. thinint_area->summary_indicator =
  542. (unsigned long) &info->summary_indicator;
  543. thinint_area->isc = VIRTIO_AIRQ_ISC;
  544. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  545. ccw->flags = CCW_FLAG_SLI;
  546. ccw->count = sizeof(*thinint_area);
  547. ccw->cda = (__u32)(unsigned long)thinint_area;
  548. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
  549. if (ret) {
  550. if (ret == -EOPNOTSUPP) {
  551. /*
  552. * The host does not support adapter interrupts
  553. * for virtio-ccw, stop trying.
  554. */
  555. virtio_ccw_use_airq = 0;
  556. pr_info("Adapter interrupts unsupported on host\n");
  557. } else
  558. dev_warn(&vcdev->cdev->dev,
  559. "enabling adapter interrupts = %d\n", ret);
  560. virtio_ccw_drop_indicators(vcdev);
  561. }
  562. out:
  563. kfree(thinint_area);
  564. return ret;
  565. }
  566. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  567. struct virtqueue *vqs[],
  568. vq_callback_t *callbacks[],
  569. const char * const names[])
  570. {
  571. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  572. unsigned long *indicatorp = NULL;
  573. int ret, i;
  574. struct ccw1 *ccw;
  575. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  576. if (!ccw)
  577. return -ENOMEM;
  578. for (i = 0; i < nvqs; ++i) {
  579. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  580. ccw);
  581. if (IS_ERR(vqs[i])) {
  582. ret = PTR_ERR(vqs[i]);
  583. vqs[i] = NULL;
  584. goto out;
  585. }
  586. }
  587. ret = -ENOMEM;
  588. /*
  589. * We need a data area under 2G to communicate. Our payload is
  590. * the address of the indicators.
  591. */
  592. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  593. if (!indicatorp)
  594. goto out;
  595. *indicatorp = (unsigned long) &vcdev->indicators;
  596. if (vcdev->is_thinint) {
  597. ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
  598. if (ret)
  599. /* no error, just fall back to legacy interrupts */
  600. vcdev->is_thinint = 0;
  601. }
  602. if (!vcdev->is_thinint) {
  603. /* Register queue indicators with host. */
  604. vcdev->indicators = 0;
  605. ccw->cmd_code = CCW_CMD_SET_IND;
  606. ccw->flags = 0;
  607. ccw->count = sizeof(&vcdev->indicators);
  608. ccw->cda = (__u32)(unsigned long) indicatorp;
  609. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  610. if (ret)
  611. goto out;
  612. }
  613. /* Register indicators2 with host for config changes */
  614. *indicatorp = (unsigned long) &vcdev->indicators2;
  615. vcdev->indicators2 = 0;
  616. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  617. ccw->flags = 0;
  618. ccw->count = sizeof(&vcdev->indicators2);
  619. ccw->cda = (__u32)(unsigned long) indicatorp;
  620. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  621. if (ret)
  622. goto out;
  623. kfree(indicatorp);
  624. kfree(ccw);
  625. return 0;
  626. out:
  627. kfree(indicatorp);
  628. kfree(ccw);
  629. virtio_ccw_del_vqs(vdev);
  630. return ret;
  631. }
  632. static void virtio_ccw_reset(struct virtio_device *vdev)
  633. {
  634. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  635. struct ccw1 *ccw;
  636. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  637. if (!ccw)
  638. return;
  639. /* Zero status bits. */
  640. *vcdev->status = 0;
  641. /* Send a reset ccw on device. */
  642. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  643. ccw->flags = 0;
  644. ccw->count = 0;
  645. ccw->cda = 0;
  646. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  647. kfree(ccw);
  648. }
  649. static u64 virtio_ccw_get_features(struct virtio_device *vdev)
  650. {
  651. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  652. struct virtio_feature_desc *features;
  653. int ret;
  654. u64 rc;
  655. struct ccw1 *ccw;
  656. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  657. if (!ccw)
  658. return 0;
  659. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  660. if (!features) {
  661. rc = 0;
  662. goto out_free;
  663. }
  664. /* Read the feature bits from the host. */
  665. features->index = 0;
  666. ccw->cmd_code = CCW_CMD_READ_FEAT;
  667. ccw->flags = 0;
  668. ccw->count = sizeof(*features);
  669. ccw->cda = (__u32)(unsigned long)features;
  670. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  671. if (ret) {
  672. rc = 0;
  673. goto out_free;
  674. }
  675. rc = le32_to_cpu(features->features);
  676. if (vcdev->revision == 0)
  677. goto out_free;
  678. /* Read second half of the feature bits from the host. */
  679. features->index = 1;
  680. ccw->cmd_code = CCW_CMD_READ_FEAT;
  681. ccw->flags = 0;
  682. ccw->count = sizeof(*features);
  683. ccw->cda = (__u32)(unsigned long)features;
  684. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  685. if (ret == 0)
  686. rc |= (u64)le32_to_cpu(features->features) << 32;
  687. out_free:
  688. kfree(features);
  689. kfree(ccw);
  690. return rc;
  691. }
  692. static int virtio_ccw_finalize_features(struct virtio_device *vdev)
  693. {
  694. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  695. struct virtio_feature_desc *features;
  696. struct ccw1 *ccw;
  697. int ret;
  698. if (vcdev->revision >= 1 &&
  699. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  700. dev_err(&vdev->dev, "virtio: device uses revision 1 "
  701. "but does not have VIRTIO_F_VERSION_1\n");
  702. return -EINVAL;
  703. }
  704. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  705. if (!ccw)
  706. return -ENOMEM;
  707. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  708. if (!features) {
  709. ret = -ENOMEM;
  710. goto out_free;
  711. }
  712. /* Give virtio_ring a chance to accept features. */
  713. vring_transport_features(vdev);
  714. features->index = 0;
  715. features->features = cpu_to_le32((u32)vdev->features);
  716. /* Write the first half of the feature bits to the host. */
  717. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  718. ccw->flags = 0;
  719. ccw->count = sizeof(*features);
  720. ccw->cda = (__u32)(unsigned long)features;
  721. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  722. if (ret)
  723. goto out_free;
  724. if (vcdev->revision == 0)
  725. goto out_free;
  726. features->index = 1;
  727. features->features = cpu_to_le32(vdev->features >> 32);
  728. /* Write the second half of the feature bits to the host. */
  729. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  730. ccw->flags = 0;
  731. ccw->count = sizeof(*features);
  732. ccw->cda = (__u32)(unsigned long)features;
  733. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  734. out_free:
  735. kfree(features);
  736. kfree(ccw);
  737. return ret;
  738. }
  739. static void virtio_ccw_get_config(struct virtio_device *vdev,
  740. unsigned int offset, void *buf, unsigned len)
  741. {
  742. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  743. int ret;
  744. struct ccw1 *ccw;
  745. void *config_area;
  746. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  747. if (!ccw)
  748. return;
  749. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  750. if (!config_area)
  751. goto out_free;
  752. /* Read the config area from the host. */
  753. ccw->cmd_code = CCW_CMD_READ_CONF;
  754. ccw->flags = 0;
  755. ccw->count = offset + len;
  756. ccw->cda = (__u32)(unsigned long)config_area;
  757. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  758. if (ret)
  759. goto out_free;
  760. memcpy(vcdev->config, config_area, offset + len);
  761. if (buf)
  762. memcpy(buf, &vcdev->config[offset], len);
  763. if (vcdev->config_ready < offset + len)
  764. vcdev->config_ready = offset + len;
  765. out_free:
  766. kfree(config_area);
  767. kfree(ccw);
  768. }
  769. static void virtio_ccw_set_config(struct virtio_device *vdev,
  770. unsigned int offset, const void *buf,
  771. unsigned len)
  772. {
  773. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  774. struct ccw1 *ccw;
  775. void *config_area;
  776. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  777. if (!ccw)
  778. return;
  779. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  780. if (!config_area)
  781. goto out_free;
  782. /* Make sure we don't overwrite fields. */
  783. if (vcdev->config_ready < offset)
  784. virtio_ccw_get_config(vdev, 0, NULL, offset);
  785. memcpy(&vcdev->config[offset], buf, len);
  786. /* Write the config area to the host. */
  787. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  788. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  789. ccw->flags = 0;
  790. ccw->count = offset + len;
  791. ccw->cda = (__u32)(unsigned long)config_area;
  792. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  793. out_free:
  794. kfree(config_area);
  795. kfree(ccw);
  796. }
  797. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  798. {
  799. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  800. u8 old_status = *vcdev->status;
  801. struct ccw1 *ccw;
  802. if (vcdev->revision < 1)
  803. return *vcdev->status;
  804. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  805. if (!ccw)
  806. return old_status;
  807. ccw->cmd_code = CCW_CMD_READ_STATUS;
  808. ccw->flags = 0;
  809. ccw->count = sizeof(*vcdev->status);
  810. ccw->cda = (__u32)(unsigned long)vcdev->status;
  811. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
  812. /*
  813. * If the channel program failed (should only happen if the device
  814. * was hotunplugged, and then we clean up via the machine check
  815. * handler anyway), vcdev->status was not overwritten and we just
  816. * return the old status, which is fine.
  817. */
  818. kfree(ccw);
  819. return *vcdev->status;
  820. }
  821. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  822. {
  823. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  824. u8 old_status = *vcdev->status;
  825. struct ccw1 *ccw;
  826. int ret;
  827. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  828. if (!ccw)
  829. return;
  830. /* Write the status to the host. */
  831. *vcdev->status = status;
  832. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  833. ccw->flags = 0;
  834. ccw->count = sizeof(status);
  835. ccw->cda = (__u32)(unsigned long)vcdev->status;
  836. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  837. /* Write failed? We assume status is unchanged. */
  838. if (ret)
  839. *vcdev->status = old_status;
  840. kfree(ccw);
  841. }
  842. static const struct virtio_config_ops virtio_ccw_config_ops = {
  843. .get_features = virtio_ccw_get_features,
  844. .finalize_features = virtio_ccw_finalize_features,
  845. .get = virtio_ccw_get_config,
  846. .set = virtio_ccw_set_config,
  847. .get_status = virtio_ccw_get_status,
  848. .set_status = virtio_ccw_set_status,
  849. .reset = virtio_ccw_reset,
  850. .find_vqs = virtio_ccw_find_vqs,
  851. .del_vqs = virtio_ccw_del_vqs,
  852. };
  853. /*
  854. * ccw bus driver related functions
  855. */
  856. static void virtio_ccw_release_dev(struct device *_d)
  857. {
  858. struct virtio_device *dev = dev_to_virtio(_d);
  859. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  860. kfree(vcdev->status);
  861. kfree(vcdev->config_block);
  862. kfree(vcdev);
  863. }
  864. static int irb_is_error(struct irb *irb)
  865. {
  866. if (scsw_cstat(&irb->scsw) != 0)
  867. return 1;
  868. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  869. return 1;
  870. if (scsw_cc(&irb->scsw) != 0)
  871. return 1;
  872. return 0;
  873. }
  874. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  875. int index)
  876. {
  877. struct virtio_ccw_vq_info *info;
  878. unsigned long flags;
  879. struct virtqueue *vq;
  880. vq = NULL;
  881. spin_lock_irqsave(&vcdev->lock, flags);
  882. list_for_each_entry(info, &vcdev->virtqueues, node) {
  883. if (info->vq->index == index) {
  884. vq = info->vq;
  885. break;
  886. }
  887. }
  888. spin_unlock_irqrestore(&vcdev->lock, flags);
  889. return vq;
  890. }
  891. static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
  892. __u32 activity)
  893. {
  894. if (vcdev->curr_io & activity) {
  895. switch (activity) {
  896. case VIRTIO_CCW_DOING_READ_FEAT:
  897. case VIRTIO_CCW_DOING_WRITE_FEAT:
  898. case VIRTIO_CCW_DOING_READ_CONFIG:
  899. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  900. case VIRTIO_CCW_DOING_WRITE_STATUS:
  901. case VIRTIO_CCW_DOING_READ_STATUS:
  902. case VIRTIO_CCW_DOING_SET_VQ:
  903. case VIRTIO_CCW_DOING_SET_IND:
  904. case VIRTIO_CCW_DOING_SET_CONF_IND:
  905. case VIRTIO_CCW_DOING_RESET:
  906. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  907. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  908. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  909. vcdev->curr_io &= ~activity;
  910. wake_up(&vcdev->wait_q);
  911. break;
  912. default:
  913. /* don't know what to do... */
  914. dev_warn(&vcdev->cdev->dev,
  915. "Suspicious activity '%08x'\n", activity);
  916. WARN_ON(1);
  917. break;
  918. }
  919. }
  920. }
  921. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  922. unsigned long intparm,
  923. struct irb *irb)
  924. {
  925. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  926. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  927. int i;
  928. struct virtqueue *vq;
  929. if (!vcdev)
  930. return;
  931. if (IS_ERR(irb)) {
  932. vcdev->err = PTR_ERR(irb);
  933. virtio_ccw_check_activity(vcdev, activity);
  934. /* Don't poke around indicators, something's wrong. */
  935. return;
  936. }
  937. /* Check if it's a notification from the host. */
  938. if ((intparm == 0) &&
  939. (scsw_stctl(&irb->scsw) ==
  940. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  941. /* OK */
  942. }
  943. if (irb_is_error(irb)) {
  944. /* Command reject? */
  945. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  946. (irb->ecw[0] & SNS0_CMD_REJECT))
  947. vcdev->err = -EOPNOTSUPP;
  948. else
  949. /* Map everything else to -EIO. */
  950. vcdev->err = -EIO;
  951. }
  952. virtio_ccw_check_activity(vcdev, activity);
  953. for_each_set_bit(i, &vcdev->indicators,
  954. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  955. /* The bit clear must happen before the vring kick. */
  956. clear_bit(i, &vcdev->indicators);
  957. barrier();
  958. vq = virtio_ccw_vq_by_ind(vcdev, i);
  959. vring_interrupt(0, vq);
  960. }
  961. if (test_bit(0, &vcdev->indicators2)) {
  962. virtio_config_changed(&vcdev->vdev);
  963. clear_bit(0, &vcdev->indicators2);
  964. }
  965. }
  966. /*
  967. * We usually want to autoonline all devices, but give the admin
  968. * a way to exempt devices from this.
  969. */
  970. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  971. (8*sizeof(long)))
  972. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  973. static char *no_auto = "";
  974. module_param(no_auto, charp, 0444);
  975. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  976. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  977. {
  978. struct ccw_dev_id id;
  979. ccw_device_get_id(cdev, &id);
  980. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  981. return 0;
  982. return 1;
  983. }
  984. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  985. {
  986. struct ccw_device *cdev = data;
  987. int ret;
  988. ret = ccw_device_set_online(cdev);
  989. if (ret)
  990. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  991. }
  992. static int virtio_ccw_probe(struct ccw_device *cdev)
  993. {
  994. cdev->handler = virtio_ccw_int_handler;
  995. if (virtio_ccw_check_autoonline(cdev))
  996. async_schedule(virtio_ccw_auto_online, cdev);
  997. return 0;
  998. }
  999. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  1000. {
  1001. unsigned long flags;
  1002. struct virtio_ccw_device *vcdev;
  1003. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1004. vcdev = dev_get_drvdata(&cdev->dev);
  1005. if (!vcdev || vcdev->going_away) {
  1006. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1007. return NULL;
  1008. }
  1009. vcdev->going_away = true;
  1010. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1011. return vcdev;
  1012. }
  1013. static void virtio_ccw_remove(struct ccw_device *cdev)
  1014. {
  1015. unsigned long flags;
  1016. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1017. if (vcdev && cdev->online) {
  1018. if (vcdev->device_lost)
  1019. virtio_break_device(&vcdev->vdev);
  1020. unregister_virtio_device(&vcdev->vdev);
  1021. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1022. dev_set_drvdata(&cdev->dev, NULL);
  1023. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1024. }
  1025. cdev->handler = NULL;
  1026. }
  1027. static int virtio_ccw_offline(struct ccw_device *cdev)
  1028. {
  1029. unsigned long flags;
  1030. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1031. if (!vcdev)
  1032. return 0;
  1033. if (vcdev->device_lost)
  1034. virtio_break_device(&vcdev->vdev);
  1035. unregister_virtio_device(&vcdev->vdev);
  1036. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1037. dev_set_drvdata(&cdev->dev, NULL);
  1038. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1039. return 0;
  1040. }
  1041. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  1042. {
  1043. struct virtio_rev_info *rev;
  1044. struct ccw1 *ccw;
  1045. int ret;
  1046. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  1047. if (!ccw)
  1048. return -ENOMEM;
  1049. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  1050. if (!rev) {
  1051. kfree(ccw);
  1052. return -ENOMEM;
  1053. }
  1054. /* Set transport revision */
  1055. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  1056. ccw->flags = 0;
  1057. ccw->count = sizeof(*rev);
  1058. ccw->cda = (__u32)(unsigned long)rev;
  1059. vcdev->revision = VIRTIO_CCW_REV_MAX;
  1060. do {
  1061. rev->revision = vcdev->revision;
  1062. /* none of our supported revisions carry payload */
  1063. rev->length = 0;
  1064. ret = ccw_io_helper(vcdev, ccw,
  1065. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1066. if (ret == -EOPNOTSUPP) {
  1067. if (vcdev->revision == 0)
  1068. /*
  1069. * The host device does not support setting
  1070. * the revision: let's operate it in legacy
  1071. * mode.
  1072. */
  1073. ret = 0;
  1074. else
  1075. vcdev->revision--;
  1076. }
  1077. } while (ret == -EOPNOTSUPP);
  1078. kfree(ccw);
  1079. kfree(rev);
  1080. return ret;
  1081. }
  1082. static int virtio_ccw_online(struct ccw_device *cdev)
  1083. {
  1084. int ret;
  1085. struct virtio_ccw_device *vcdev;
  1086. unsigned long flags;
  1087. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1088. if (!vcdev) {
  1089. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1090. ret = -ENOMEM;
  1091. goto out_free;
  1092. }
  1093. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1094. GFP_DMA | GFP_KERNEL);
  1095. if (!vcdev->config_block) {
  1096. ret = -ENOMEM;
  1097. goto out_free;
  1098. }
  1099. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1100. if (!vcdev->status) {
  1101. ret = -ENOMEM;
  1102. goto out_free;
  1103. }
  1104. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1105. vcdev->vdev.dev.parent = &cdev->dev;
  1106. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1107. vcdev->vdev.config = &virtio_ccw_config_ops;
  1108. vcdev->cdev = cdev;
  1109. init_waitqueue_head(&vcdev->wait_q);
  1110. INIT_LIST_HEAD(&vcdev->virtqueues);
  1111. spin_lock_init(&vcdev->lock);
  1112. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1113. dev_set_drvdata(&cdev->dev, vcdev);
  1114. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1115. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1116. vcdev->vdev.id.device = cdev->id.cu_model;
  1117. ret = virtio_ccw_set_transport_rev(vcdev);
  1118. if (ret)
  1119. goto out_free;
  1120. ret = register_virtio_device(&vcdev->vdev);
  1121. if (ret) {
  1122. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1123. ret);
  1124. goto out_put;
  1125. }
  1126. return 0;
  1127. out_put:
  1128. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1129. dev_set_drvdata(&cdev->dev, NULL);
  1130. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1131. put_device(&vcdev->vdev.dev);
  1132. return ret;
  1133. out_free:
  1134. if (vcdev) {
  1135. kfree(vcdev->status);
  1136. kfree(vcdev->config_block);
  1137. }
  1138. kfree(vcdev);
  1139. return ret;
  1140. }
  1141. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1142. {
  1143. int rc;
  1144. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1145. /*
  1146. * Make sure vcdev is set
  1147. * i.e. set_offline/remove callback not already running
  1148. */
  1149. if (!vcdev)
  1150. return NOTIFY_DONE;
  1151. switch (event) {
  1152. case CIO_GONE:
  1153. vcdev->device_lost = true;
  1154. rc = NOTIFY_DONE;
  1155. break;
  1156. default:
  1157. rc = NOTIFY_DONE;
  1158. break;
  1159. }
  1160. return rc;
  1161. }
  1162. static struct ccw_device_id virtio_ids[] = {
  1163. { CCW_DEVICE(0x3832, 0) },
  1164. {},
  1165. };
  1166. static struct ccw_driver virtio_ccw_driver = {
  1167. .driver = {
  1168. .owner = THIS_MODULE,
  1169. .name = "virtio_ccw",
  1170. },
  1171. .ids = virtio_ids,
  1172. .probe = virtio_ccw_probe,
  1173. .remove = virtio_ccw_remove,
  1174. .set_offline = virtio_ccw_offline,
  1175. .set_online = virtio_ccw_online,
  1176. .notify = virtio_ccw_cio_notify,
  1177. .int_class = IRQIO_VIR,
  1178. };
  1179. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1180. int max_digit, int max_val)
  1181. {
  1182. int diff;
  1183. diff = 0;
  1184. *val = 0;
  1185. while (diff <= max_digit) {
  1186. int value = hex_to_bin(**cp);
  1187. if (value < 0)
  1188. break;
  1189. *val = *val * 16 + value;
  1190. (*cp)++;
  1191. diff++;
  1192. }
  1193. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1194. return 1;
  1195. return 0;
  1196. }
  1197. static int __init parse_busid(char *str, unsigned int *cssid,
  1198. unsigned int *ssid, unsigned int *devno)
  1199. {
  1200. char *str_work;
  1201. int rc, ret;
  1202. rc = 1;
  1203. if (*str == '\0')
  1204. goto out;
  1205. str_work = str;
  1206. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1207. if (ret || (str_work[0] != '.'))
  1208. goto out;
  1209. str_work++;
  1210. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1211. if (ret || (str_work[0] != '.'))
  1212. goto out;
  1213. str_work++;
  1214. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1215. if (ret || (str_work[0] != '\0'))
  1216. goto out;
  1217. rc = 0;
  1218. out:
  1219. return rc;
  1220. }
  1221. static void __init no_auto_parse(void)
  1222. {
  1223. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1224. char *parm, *str;
  1225. int rc;
  1226. str = no_auto;
  1227. while ((parm = strsep(&str, ","))) {
  1228. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1229. &from_ssid, &from);
  1230. if (rc)
  1231. continue;
  1232. if (parm != NULL) {
  1233. rc = parse_busid(parm, &to_cssid,
  1234. &to_ssid, &to);
  1235. if ((from_ssid > to_ssid) ||
  1236. ((from_ssid == to_ssid) && (from > to)))
  1237. rc = -EINVAL;
  1238. } else {
  1239. to_cssid = from_cssid;
  1240. to_ssid = from_ssid;
  1241. to = from;
  1242. }
  1243. if (rc)
  1244. continue;
  1245. while ((from_ssid < to_ssid) ||
  1246. ((from_ssid == to_ssid) && (from <= to))) {
  1247. set_bit(from, devs_no_auto[from_ssid]);
  1248. from++;
  1249. if (from > __MAX_SUBCHANNEL) {
  1250. from_ssid++;
  1251. from = 0;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. static int __init virtio_ccw_init(void)
  1257. {
  1258. /* parse no_auto string before we do anything further */
  1259. no_auto_parse();
  1260. return ccw_driver_register(&virtio_ccw_driver);
  1261. }
  1262. device_initcall(virtio_ccw_init);