virtio_blk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. //#define DEBUG
  2. #include <linux/spinlock.h>
  3. #include <linux/slab.h>
  4. #include <linux/blkdev.h>
  5. #include <linux/hdreg.h>
  6. #include <linux/module.h>
  7. #include <linux/mutex.h>
  8. #include <linux/virtio.h>
  9. #include <linux/virtio_blk.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/string_helpers.h>
  12. #include <scsi/scsi_cmnd.h>
  13. #include <linux/idr.h>
  14. #include <linux/blk-mq.h>
  15. #include <linux/numa.h>
  16. #define PART_BITS 4
  17. #define VQ_NAME_LEN 16
  18. static int major;
  19. static DEFINE_IDA(vd_index_ida);
  20. static struct workqueue_struct *virtblk_wq;
  21. struct virtio_blk_vq {
  22. struct virtqueue *vq;
  23. spinlock_t lock;
  24. char name[VQ_NAME_LEN];
  25. } ____cacheline_aligned_in_smp;
  26. struct virtio_blk {
  27. struct virtio_device *vdev;
  28. /* The disk structure for the kernel. */
  29. struct gendisk *disk;
  30. /* Block layer tags. */
  31. struct blk_mq_tag_set tag_set;
  32. /* Process context for config space updates */
  33. struct work_struct config_work;
  34. /* What host tells us, plus 2 for header & tailer. */
  35. unsigned int sg_elems;
  36. /* Ida index - used to track minor number allocations. */
  37. int index;
  38. /* num of vqs */
  39. int num_vqs;
  40. struct virtio_blk_vq *vqs;
  41. };
  42. struct virtblk_req {
  43. struct request *req;
  44. struct virtio_blk_outhdr out_hdr;
  45. struct virtio_scsi_inhdr in_hdr;
  46. u8 status;
  47. struct scatterlist sg[];
  48. };
  49. static inline int virtblk_result(struct virtblk_req *vbr)
  50. {
  51. switch (vbr->status) {
  52. case VIRTIO_BLK_S_OK:
  53. return 0;
  54. case VIRTIO_BLK_S_UNSUPP:
  55. return -ENOTTY;
  56. default:
  57. return -EIO;
  58. }
  59. }
  60. static int __virtblk_add_req(struct virtqueue *vq,
  61. struct virtblk_req *vbr,
  62. struct scatterlist *data_sg,
  63. bool have_data)
  64. {
  65. struct scatterlist hdr, status, cmd, sense, inhdr, *sgs[6];
  66. unsigned int num_out = 0, num_in = 0;
  67. __virtio32 type = vbr->out_hdr.type & ~cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT);
  68. sg_init_one(&hdr, &vbr->out_hdr, sizeof(vbr->out_hdr));
  69. sgs[num_out++] = &hdr;
  70. /*
  71. * If this is a packet command we need a couple of additional headers.
  72. * Behind the normal outhdr we put a segment with the scsi command
  73. * block, and before the normal inhdr we put the sense data and the
  74. * inhdr with additional status information.
  75. */
  76. if (type == cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_SCSI_CMD)) {
  77. sg_init_one(&cmd, vbr->req->cmd, vbr->req->cmd_len);
  78. sgs[num_out++] = &cmd;
  79. }
  80. if (have_data) {
  81. if (vbr->out_hdr.type & cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_OUT))
  82. sgs[num_out++] = data_sg;
  83. else
  84. sgs[num_out + num_in++] = data_sg;
  85. }
  86. if (type == cpu_to_virtio32(vq->vdev, VIRTIO_BLK_T_SCSI_CMD)) {
  87. sg_init_one(&sense, vbr->req->sense, SCSI_SENSE_BUFFERSIZE);
  88. sgs[num_out + num_in++] = &sense;
  89. sg_init_one(&inhdr, &vbr->in_hdr, sizeof(vbr->in_hdr));
  90. sgs[num_out + num_in++] = &inhdr;
  91. }
  92. sg_init_one(&status, &vbr->status, sizeof(vbr->status));
  93. sgs[num_out + num_in++] = &status;
  94. return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
  95. }
  96. static inline void virtblk_request_done(struct request *req)
  97. {
  98. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  99. struct virtio_blk *vblk = req->q->queuedata;
  100. int error = virtblk_result(vbr);
  101. if (req->cmd_type == REQ_TYPE_BLOCK_PC) {
  102. req->resid_len = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.residual);
  103. req->sense_len = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.sense_len);
  104. req->errors = virtio32_to_cpu(vblk->vdev, vbr->in_hdr.errors);
  105. } else if (req->cmd_type == REQ_TYPE_DRV_PRIV) {
  106. req->errors = (error != 0);
  107. }
  108. blk_mq_end_request(req, error);
  109. }
  110. static void virtblk_done(struct virtqueue *vq)
  111. {
  112. struct virtio_blk *vblk = vq->vdev->priv;
  113. bool req_done = false;
  114. int qid = vq->index;
  115. struct virtblk_req *vbr;
  116. unsigned long flags;
  117. unsigned int len;
  118. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  119. do {
  120. virtqueue_disable_cb(vq);
  121. while ((vbr = virtqueue_get_buf(vblk->vqs[qid].vq, &len)) != NULL) {
  122. blk_mq_complete_request(vbr->req, vbr->req->errors);
  123. req_done = true;
  124. }
  125. if (unlikely(virtqueue_is_broken(vq)))
  126. break;
  127. } while (!virtqueue_enable_cb(vq));
  128. /* In case queue is stopped waiting for more buffers. */
  129. if (req_done)
  130. blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
  131. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  132. }
  133. static int virtio_queue_rq(struct blk_mq_hw_ctx *hctx,
  134. const struct blk_mq_queue_data *bd)
  135. {
  136. struct virtio_blk *vblk = hctx->queue->queuedata;
  137. struct request *req = bd->rq;
  138. struct virtblk_req *vbr = blk_mq_rq_to_pdu(req);
  139. unsigned long flags;
  140. unsigned int num;
  141. int qid = hctx->queue_num;
  142. int err;
  143. bool notify = false;
  144. BUG_ON(req->nr_phys_segments + 2 > vblk->sg_elems);
  145. vbr->req = req;
  146. if (req_op(req) == REQ_OP_FLUSH) {
  147. vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_FLUSH);
  148. vbr->out_hdr.sector = 0;
  149. vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(vbr->req));
  150. } else {
  151. switch (req->cmd_type) {
  152. case REQ_TYPE_FS:
  153. vbr->out_hdr.type = 0;
  154. vbr->out_hdr.sector = cpu_to_virtio64(vblk->vdev, blk_rq_pos(vbr->req));
  155. vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(vbr->req));
  156. break;
  157. case REQ_TYPE_BLOCK_PC:
  158. vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_SCSI_CMD);
  159. vbr->out_hdr.sector = 0;
  160. vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(vbr->req));
  161. break;
  162. case REQ_TYPE_DRV_PRIV:
  163. vbr->out_hdr.type = cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_GET_ID);
  164. vbr->out_hdr.sector = 0;
  165. vbr->out_hdr.ioprio = cpu_to_virtio32(vblk->vdev, req_get_ioprio(vbr->req));
  166. break;
  167. default:
  168. /* We don't put anything else in the queue. */
  169. BUG();
  170. }
  171. }
  172. blk_mq_start_request(req);
  173. num = blk_rq_map_sg(hctx->queue, vbr->req, vbr->sg);
  174. if (num) {
  175. if (rq_data_dir(vbr->req) == WRITE)
  176. vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_OUT);
  177. else
  178. vbr->out_hdr.type |= cpu_to_virtio32(vblk->vdev, VIRTIO_BLK_T_IN);
  179. }
  180. spin_lock_irqsave(&vblk->vqs[qid].lock, flags);
  181. err = __virtblk_add_req(vblk->vqs[qid].vq, vbr, vbr->sg, num);
  182. if (err) {
  183. virtqueue_kick(vblk->vqs[qid].vq);
  184. blk_mq_stop_hw_queue(hctx);
  185. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  186. /* Out of mem doesn't actually happen, since we fall back
  187. * to direct descriptors */
  188. if (err == -ENOMEM || err == -ENOSPC)
  189. return BLK_MQ_RQ_QUEUE_BUSY;
  190. return BLK_MQ_RQ_QUEUE_ERROR;
  191. }
  192. if (bd->last && virtqueue_kick_prepare(vblk->vqs[qid].vq))
  193. notify = true;
  194. spin_unlock_irqrestore(&vblk->vqs[qid].lock, flags);
  195. if (notify)
  196. virtqueue_notify(vblk->vqs[qid].vq);
  197. return BLK_MQ_RQ_QUEUE_OK;
  198. }
  199. /* return id (s/n) string for *disk to *id_str
  200. */
  201. static int virtblk_get_id(struct gendisk *disk, char *id_str)
  202. {
  203. struct virtio_blk *vblk = disk->private_data;
  204. struct request_queue *q = vblk->disk->queue;
  205. struct request *req;
  206. int err;
  207. req = blk_get_request(q, READ, GFP_KERNEL);
  208. if (IS_ERR(req))
  209. return PTR_ERR(req);
  210. req->cmd_type = REQ_TYPE_DRV_PRIV;
  211. err = blk_rq_map_kern(q, req, id_str, VIRTIO_BLK_ID_BYTES, GFP_KERNEL);
  212. if (err)
  213. goto out;
  214. err = blk_execute_rq(vblk->disk->queue, vblk->disk, req, false);
  215. out:
  216. blk_put_request(req);
  217. return err;
  218. }
  219. static int virtblk_ioctl(struct block_device *bdev, fmode_t mode,
  220. unsigned int cmd, unsigned long data)
  221. {
  222. struct gendisk *disk = bdev->bd_disk;
  223. struct virtio_blk *vblk = disk->private_data;
  224. /*
  225. * Only allow the generic SCSI ioctls if the host can support it.
  226. */
  227. if (!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_SCSI))
  228. return -ENOTTY;
  229. return scsi_cmd_blk_ioctl(bdev, mode, cmd,
  230. (void __user *)data);
  231. }
  232. /* We provide getgeo only to please some old bootloader/partitioning tools */
  233. static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
  234. {
  235. struct virtio_blk *vblk = bd->bd_disk->private_data;
  236. /* see if the host passed in geometry config */
  237. if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
  238. virtio_cread(vblk->vdev, struct virtio_blk_config,
  239. geometry.cylinders, &geo->cylinders);
  240. virtio_cread(vblk->vdev, struct virtio_blk_config,
  241. geometry.heads, &geo->heads);
  242. virtio_cread(vblk->vdev, struct virtio_blk_config,
  243. geometry.sectors, &geo->sectors);
  244. } else {
  245. /* some standard values, similar to sd */
  246. geo->heads = 1 << 6;
  247. geo->sectors = 1 << 5;
  248. geo->cylinders = get_capacity(bd->bd_disk) >> 11;
  249. }
  250. return 0;
  251. }
  252. static const struct block_device_operations virtblk_fops = {
  253. .ioctl = virtblk_ioctl,
  254. .owner = THIS_MODULE,
  255. .getgeo = virtblk_getgeo,
  256. };
  257. static int index_to_minor(int index)
  258. {
  259. return index << PART_BITS;
  260. }
  261. static int minor_to_index(int minor)
  262. {
  263. return minor >> PART_BITS;
  264. }
  265. static ssize_t virtblk_serial_show(struct device *dev,
  266. struct device_attribute *attr, char *buf)
  267. {
  268. struct gendisk *disk = dev_to_disk(dev);
  269. int err;
  270. /* sysfs gives us a PAGE_SIZE buffer */
  271. BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
  272. buf[VIRTIO_BLK_ID_BYTES] = '\0';
  273. err = virtblk_get_id(disk, buf);
  274. if (!err)
  275. return strlen(buf);
  276. if (err == -EIO) /* Unsupported? Make it empty. */
  277. return 0;
  278. return err;
  279. }
  280. static DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
  281. static void virtblk_config_changed_work(struct work_struct *work)
  282. {
  283. struct virtio_blk *vblk =
  284. container_of(work, struct virtio_blk, config_work);
  285. struct virtio_device *vdev = vblk->vdev;
  286. struct request_queue *q = vblk->disk->queue;
  287. char cap_str_2[10], cap_str_10[10];
  288. char *envp[] = { "RESIZE=1", NULL };
  289. u64 capacity;
  290. /* Host must always specify the capacity. */
  291. virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);
  292. /* If capacity is too big, truncate with warning. */
  293. if ((sector_t)capacity != capacity) {
  294. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  295. (unsigned long long)capacity);
  296. capacity = (sector_t)-1;
  297. }
  298. string_get_size(capacity, queue_logical_block_size(q),
  299. STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  300. string_get_size(capacity, queue_logical_block_size(q),
  301. STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
  302. dev_notice(&vdev->dev,
  303. "new size: %llu %d-byte logical blocks (%s/%s)\n",
  304. (unsigned long long)capacity,
  305. queue_logical_block_size(q),
  306. cap_str_10, cap_str_2);
  307. set_capacity(vblk->disk, capacity);
  308. revalidate_disk(vblk->disk);
  309. kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
  310. }
  311. static void virtblk_config_changed(struct virtio_device *vdev)
  312. {
  313. struct virtio_blk *vblk = vdev->priv;
  314. queue_work(virtblk_wq, &vblk->config_work);
  315. }
  316. static int init_vq(struct virtio_blk *vblk)
  317. {
  318. int err;
  319. int i;
  320. vq_callback_t **callbacks;
  321. const char **names;
  322. struct virtqueue **vqs;
  323. unsigned short num_vqs;
  324. struct virtio_device *vdev = vblk->vdev;
  325. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
  326. struct virtio_blk_config, num_queues,
  327. &num_vqs);
  328. if (err)
  329. num_vqs = 1;
  330. vblk->vqs = kmalloc_array(num_vqs, sizeof(*vblk->vqs), GFP_KERNEL);
  331. if (!vblk->vqs)
  332. return -ENOMEM;
  333. names = kmalloc_array(num_vqs, sizeof(*names), GFP_KERNEL);
  334. callbacks = kmalloc_array(num_vqs, sizeof(*callbacks), GFP_KERNEL);
  335. vqs = kmalloc_array(num_vqs, sizeof(*vqs), GFP_KERNEL);
  336. if (!names || !callbacks || !vqs) {
  337. err = -ENOMEM;
  338. goto out;
  339. }
  340. for (i = 0; i < num_vqs; i++) {
  341. callbacks[i] = virtblk_done;
  342. snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
  343. names[i] = vblk->vqs[i].name;
  344. }
  345. /* Discover virtqueues and write information to configuration. */
  346. err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
  347. if (err)
  348. goto out;
  349. for (i = 0; i < num_vqs; i++) {
  350. spin_lock_init(&vblk->vqs[i].lock);
  351. vblk->vqs[i].vq = vqs[i];
  352. }
  353. vblk->num_vqs = num_vqs;
  354. out:
  355. kfree(vqs);
  356. kfree(callbacks);
  357. kfree(names);
  358. if (err)
  359. kfree(vblk->vqs);
  360. return err;
  361. }
  362. /*
  363. * Legacy naming scheme used for virtio devices. We are stuck with it for
  364. * virtio blk but don't ever use it for any new driver.
  365. */
  366. static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
  367. {
  368. const int base = 'z' - 'a' + 1;
  369. char *begin = buf + strlen(prefix);
  370. char *end = buf + buflen;
  371. char *p;
  372. int unit;
  373. p = end - 1;
  374. *p = '\0';
  375. unit = base;
  376. do {
  377. if (p == begin)
  378. return -EINVAL;
  379. *--p = 'a' + (index % unit);
  380. index = (index / unit) - 1;
  381. } while (index >= 0);
  382. memmove(begin, p, end - p);
  383. memcpy(buf, prefix, strlen(prefix));
  384. return 0;
  385. }
  386. static int virtblk_get_cache_mode(struct virtio_device *vdev)
  387. {
  388. u8 writeback;
  389. int err;
  390. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE,
  391. struct virtio_blk_config, wce,
  392. &writeback);
  393. /*
  394. * If WCE is not configurable and flush is not available,
  395. * assume no writeback cache is in use.
  396. */
  397. if (err)
  398. writeback = virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH);
  399. return writeback;
  400. }
  401. static void virtblk_update_cache_mode(struct virtio_device *vdev)
  402. {
  403. u8 writeback = virtblk_get_cache_mode(vdev);
  404. struct virtio_blk *vblk = vdev->priv;
  405. blk_queue_write_cache(vblk->disk->queue, writeback, false);
  406. revalidate_disk(vblk->disk);
  407. }
  408. static const char *const virtblk_cache_types[] = {
  409. "write through", "write back"
  410. };
  411. static ssize_t
  412. virtblk_cache_type_store(struct device *dev, struct device_attribute *attr,
  413. const char *buf, size_t count)
  414. {
  415. struct gendisk *disk = dev_to_disk(dev);
  416. struct virtio_blk *vblk = disk->private_data;
  417. struct virtio_device *vdev = vblk->vdev;
  418. int i;
  419. BUG_ON(!virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_CONFIG_WCE));
  420. for (i = ARRAY_SIZE(virtblk_cache_types); --i >= 0; )
  421. if (sysfs_streq(buf, virtblk_cache_types[i]))
  422. break;
  423. if (i < 0)
  424. return -EINVAL;
  425. virtio_cwrite8(vdev, offsetof(struct virtio_blk_config, wce), i);
  426. virtblk_update_cache_mode(vdev);
  427. return count;
  428. }
  429. static ssize_t
  430. virtblk_cache_type_show(struct device *dev, struct device_attribute *attr,
  431. char *buf)
  432. {
  433. struct gendisk *disk = dev_to_disk(dev);
  434. struct virtio_blk *vblk = disk->private_data;
  435. u8 writeback = virtblk_get_cache_mode(vblk->vdev);
  436. BUG_ON(writeback >= ARRAY_SIZE(virtblk_cache_types));
  437. return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]);
  438. }
  439. static const struct device_attribute dev_attr_cache_type_ro =
  440. __ATTR(cache_type, S_IRUGO,
  441. virtblk_cache_type_show, NULL);
  442. static const struct device_attribute dev_attr_cache_type_rw =
  443. __ATTR(cache_type, S_IRUGO|S_IWUSR,
  444. virtblk_cache_type_show, virtblk_cache_type_store);
  445. static int virtblk_init_request(void *data, struct request *rq,
  446. unsigned int hctx_idx, unsigned int request_idx,
  447. unsigned int numa_node)
  448. {
  449. struct virtio_blk *vblk = data;
  450. struct virtblk_req *vbr = blk_mq_rq_to_pdu(rq);
  451. sg_init_table(vbr->sg, vblk->sg_elems);
  452. return 0;
  453. }
  454. static struct blk_mq_ops virtio_mq_ops = {
  455. .queue_rq = virtio_queue_rq,
  456. .complete = virtblk_request_done,
  457. .init_request = virtblk_init_request,
  458. };
  459. static unsigned int virtblk_queue_depth;
  460. module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
  461. static int virtblk_probe(struct virtio_device *vdev)
  462. {
  463. struct virtio_blk *vblk;
  464. struct request_queue *q;
  465. int err, index;
  466. u64 cap;
  467. u32 v, blk_size, sg_elems, opt_io_size;
  468. u16 min_io_size;
  469. u8 physical_block_exp, alignment_offset;
  470. if (!vdev->config->get) {
  471. dev_err(&vdev->dev, "%s failure: config access disabled\n",
  472. __func__);
  473. return -EINVAL;
  474. }
  475. err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
  476. GFP_KERNEL);
  477. if (err < 0)
  478. goto out;
  479. index = err;
  480. /* We need to know how many segments before we allocate. */
  481. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SEG_MAX,
  482. struct virtio_blk_config, seg_max,
  483. &sg_elems);
  484. /* We need at least one SG element, whatever they say. */
  485. if (err || !sg_elems)
  486. sg_elems = 1;
  487. /* We need an extra sg elements at head and tail. */
  488. sg_elems += 2;
  489. vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
  490. if (!vblk) {
  491. err = -ENOMEM;
  492. goto out_free_index;
  493. }
  494. vblk->vdev = vdev;
  495. vblk->sg_elems = sg_elems;
  496. INIT_WORK(&vblk->config_work, virtblk_config_changed_work);
  497. err = init_vq(vblk);
  498. if (err)
  499. goto out_free_vblk;
  500. /* FIXME: How many partitions? How long is a piece of string? */
  501. vblk->disk = alloc_disk(1 << PART_BITS);
  502. if (!vblk->disk) {
  503. err = -ENOMEM;
  504. goto out_free_vq;
  505. }
  506. /* Default queue sizing is to fill the ring. */
  507. if (!virtblk_queue_depth) {
  508. virtblk_queue_depth = vblk->vqs[0].vq->num_free;
  509. /* ... but without indirect descs, we use 2 descs per req */
  510. if (!virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC))
  511. virtblk_queue_depth /= 2;
  512. }
  513. memset(&vblk->tag_set, 0, sizeof(vblk->tag_set));
  514. vblk->tag_set.ops = &virtio_mq_ops;
  515. vblk->tag_set.queue_depth = virtblk_queue_depth;
  516. vblk->tag_set.numa_node = NUMA_NO_NODE;
  517. vblk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  518. vblk->tag_set.cmd_size =
  519. sizeof(struct virtblk_req) +
  520. sizeof(struct scatterlist) * sg_elems;
  521. vblk->tag_set.driver_data = vblk;
  522. vblk->tag_set.nr_hw_queues = vblk->num_vqs;
  523. err = blk_mq_alloc_tag_set(&vblk->tag_set);
  524. if (err)
  525. goto out_put_disk;
  526. q = vblk->disk->queue = blk_mq_init_queue(&vblk->tag_set);
  527. if (IS_ERR(q)) {
  528. err = -ENOMEM;
  529. goto out_free_tags;
  530. }
  531. q->queuedata = vblk;
  532. virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
  533. vblk->disk->major = major;
  534. vblk->disk->first_minor = index_to_minor(index);
  535. vblk->disk->private_data = vblk;
  536. vblk->disk->fops = &virtblk_fops;
  537. vblk->disk->flags |= GENHD_FL_EXT_DEVT;
  538. vblk->index = index;
  539. /* configure queue flush support */
  540. virtblk_update_cache_mode(vdev);
  541. /* If disk is read-only in the host, the guest should obey */
  542. if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
  543. set_disk_ro(vblk->disk, 1);
  544. /* Host must always specify the capacity. */
  545. virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
  546. /* If capacity is too big, truncate with warning. */
  547. if ((sector_t)cap != cap) {
  548. dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
  549. (unsigned long long)cap);
  550. cap = (sector_t)-1;
  551. }
  552. set_capacity(vblk->disk, cap);
  553. /* We can handle whatever the host told us to handle. */
  554. blk_queue_max_segments(q, vblk->sg_elems-2);
  555. /* No need to bounce any requests */
  556. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  557. /* No real sector limit. */
  558. blk_queue_max_hw_sectors(q, -1U);
  559. /* Host can optionally specify maximum segment size and number of
  560. * segments. */
  561. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_SIZE_MAX,
  562. struct virtio_blk_config, size_max, &v);
  563. if (!err)
  564. blk_queue_max_segment_size(q, v);
  565. else
  566. blk_queue_max_segment_size(q, -1U);
  567. /* Host can optionally specify the block size of the device */
  568. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
  569. struct virtio_blk_config, blk_size,
  570. &blk_size);
  571. if (!err)
  572. blk_queue_logical_block_size(q, blk_size);
  573. else
  574. blk_size = queue_logical_block_size(q);
  575. /* Use topology information if available */
  576. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  577. struct virtio_blk_config, physical_block_exp,
  578. &physical_block_exp);
  579. if (!err && physical_block_exp)
  580. blk_queue_physical_block_size(q,
  581. blk_size * (1 << physical_block_exp));
  582. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  583. struct virtio_blk_config, alignment_offset,
  584. &alignment_offset);
  585. if (!err && alignment_offset)
  586. blk_queue_alignment_offset(q, blk_size * alignment_offset);
  587. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  588. struct virtio_blk_config, min_io_size,
  589. &min_io_size);
  590. if (!err && min_io_size)
  591. blk_queue_io_min(q, blk_size * min_io_size);
  592. err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
  593. struct virtio_blk_config, opt_io_size,
  594. &opt_io_size);
  595. if (!err && opt_io_size)
  596. blk_queue_io_opt(q, blk_size * opt_io_size);
  597. virtio_device_ready(vdev);
  598. device_add_disk(&vdev->dev, vblk->disk);
  599. err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
  600. if (err)
  601. goto out_del_disk;
  602. if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE))
  603. err = device_create_file(disk_to_dev(vblk->disk),
  604. &dev_attr_cache_type_rw);
  605. else
  606. err = device_create_file(disk_to_dev(vblk->disk),
  607. &dev_attr_cache_type_ro);
  608. if (err)
  609. goto out_del_disk;
  610. return 0;
  611. out_del_disk:
  612. del_gendisk(vblk->disk);
  613. blk_cleanup_queue(vblk->disk->queue);
  614. out_free_tags:
  615. blk_mq_free_tag_set(&vblk->tag_set);
  616. out_put_disk:
  617. put_disk(vblk->disk);
  618. out_free_vq:
  619. vdev->config->del_vqs(vdev);
  620. out_free_vblk:
  621. kfree(vblk);
  622. out_free_index:
  623. ida_simple_remove(&vd_index_ida, index);
  624. out:
  625. return err;
  626. }
  627. static void virtblk_remove(struct virtio_device *vdev)
  628. {
  629. struct virtio_blk *vblk = vdev->priv;
  630. int index = vblk->index;
  631. int refc;
  632. /* Make sure no work handler is accessing the device. */
  633. flush_work(&vblk->config_work);
  634. del_gendisk(vblk->disk);
  635. blk_cleanup_queue(vblk->disk->queue);
  636. blk_mq_free_tag_set(&vblk->tag_set);
  637. /* Stop all the virtqueues. */
  638. vdev->config->reset(vdev);
  639. refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
  640. put_disk(vblk->disk);
  641. vdev->config->del_vqs(vdev);
  642. kfree(vblk->vqs);
  643. kfree(vblk);
  644. /* Only free device id if we don't have any users */
  645. if (refc == 1)
  646. ida_simple_remove(&vd_index_ida, index);
  647. }
  648. #ifdef CONFIG_PM_SLEEP
  649. static int virtblk_freeze(struct virtio_device *vdev)
  650. {
  651. struct virtio_blk *vblk = vdev->priv;
  652. /* Ensure we don't receive any more interrupts */
  653. vdev->config->reset(vdev);
  654. /* Make sure no work handler is accessing the device. */
  655. flush_work(&vblk->config_work);
  656. blk_mq_stop_hw_queues(vblk->disk->queue);
  657. vdev->config->del_vqs(vdev);
  658. return 0;
  659. }
  660. static int virtblk_restore(struct virtio_device *vdev)
  661. {
  662. struct virtio_blk *vblk = vdev->priv;
  663. int ret;
  664. ret = init_vq(vdev->priv);
  665. if (ret)
  666. return ret;
  667. virtio_device_ready(vdev);
  668. blk_mq_start_stopped_hw_queues(vblk->disk->queue, true);
  669. return 0;
  670. }
  671. #endif
  672. static const struct virtio_device_id id_table[] = {
  673. { VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID },
  674. { 0 },
  675. };
  676. static unsigned int features_legacy[] = {
  677. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  678. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE, VIRTIO_BLK_F_SCSI,
  679. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  680. VIRTIO_BLK_F_MQ,
  681. }
  682. ;
  683. static unsigned int features[] = {
  684. VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX, VIRTIO_BLK_F_GEOMETRY,
  685. VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
  686. VIRTIO_BLK_F_FLUSH, VIRTIO_BLK_F_TOPOLOGY, VIRTIO_BLK_F_CONFIG_WCE,
  687. VIRTIO_BLK_F_MQ,
  688. };
  689. static struct virtio_driver virtio_blk = {
  690. .feature_table = features,
  691. .feature_table_size = ARRAY_SIZE(features),
  692. .feature_table_legacy = features_legacy,
  693. .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
  694. .driver.name = KBUILD_MODNAME,
  695. .driver.owner = THIS_MODULE,
  696. .id_table = id_table,
  697. .probe = virtblk_probe,
  698. .remove = virtblk_remove,
  699. .config_changed = virtblk_config_changed,
  700. #ifdef CONFIG_PM_SLEEP
  701. .freeze = virtblk_freeze,
  702. .restore = virtblk_restore,
  703. #endif
  704. };
  705. static int __init init(void)
  706. {
  707. int error;
  708. virtblk_wq = alloc_workqueue("virtio-blk", 0, 0);
  709. if (!virtblk_wq)
  710. return -ENOMEM;
  711. major = register_blkdev(0, "virtblk");
  712. if (major < 0) {
  713. error = major;
  714. goto out_destroy_workqueue;
  715. }
  716. error = register_virtio_driver(&virtio_blk);
  717. if (error)
  718. goto out_unregister_blkdev;
  719. return 0;
  720. out_unregister_blkdev:
  721. unregister_blkdev(major, "virtblk");
  722. out_destroy_workqueue:
  723. destroy_workqueue(virtblk_wq);
  724. return error;
  725. }
  726. static void __exit fini(void)
  727. {
  728. unregister_virtio_driver(&virtio_blk);
  729. unregister_blkdev(major, "virtblk");
  730. destroy_workqueue(virtblk_wq);
  731. }
  732. module_init(init);
  733. module_exit(fini);
  734. MODULE_DEVICE_TABLE(virtio, id_table);
  735. MODULE_DESCRIPTION("Virtio block driver");
  736. MODULE_LICENSE("GPL");