virtio_ccw.c 36 KB

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