virtio_ccw.c 35 KB

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