virtio_ccw.c 38 KB

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