virtio_ccw.c 35 KB

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