-
Notifications
You must be signed in to change notification settings - Fork 4
/
linux-3.10.0-1160.99.1.patch
519 lines (484 loc) · 21.9 KB
/
linux-3.10.0-1160.99.1.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
From 800f857aee56df428e344c212488286a9c70ec5d Mon Sep 17 00:00:00 2001
From: Dongli Zhang <[email protected]>
Date: Sun, 29 Oct 2023 22:12:28 -0700
Subject: [PATCH 1/1] linux-3.10.0-1160.99.1
---
drivers/scsi/virtio_scsi.c | 56 ++++++++++
drivers/virtio/virtio_ring.c | 202 +++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 11 ++
include/scsi/scsi_cmnd.h | 8 ++
4 files changed, 277 insertions(+)
diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index 4a410a6b..b9e799a8 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -453,6 +453,10 @@ static void virtscsi_event_done(struct virtqueue *vq)
* @req_size : size of the request buffer
* @resp_size : size of the response buffer
*/
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|510| <<virtscsi_kick_cmd>> err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size);
+ */
static int virtscsi_add_cmd(struct virtqueue *vq,
struct virtio_scsi_cmd *cmd,
size_t req_size, size_t resp_size)
@@ -471,6 +475,21 @@ static int virtscsi_add_cmd(struct virtqueue *vq,
in = &scsi_in(sc)->table;
}
+ /*
+ * struct sg_table {
+ * struct scatterlist *sgl; // the list
+ * unsigned int nents; // number of mapped entries
+ * unsigned int orig_nents; // original size of list
+ * };
+ *
+ * struct sg_table *in;
+ * struct sg_table *out;
+ *
+ * struct scatterlist *sgs[4];
+ * struct scatterlist req;
+ * struct scatterlist resp;
+ */
+
/* Request header. */
sg_init_one(&req, &cmd->req, req_size);
sgs[out_num++] = &req;
@@ -487,9 +506,21 @@ static int virtscsi_add_cmd(struct virtqueue *vq,
if (in)
sgs[out_num + in_num++] = in->sgl;
+ /*
+ * data是cmd!!!
+ * struct virtio_scsi_cmd *cmd
+ */
return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
}
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|550| <<virtscsi_queuecommand>> if (virtscsi_kick_cmd(req_vq, cmd, sizeof cmd->req.cmd, sizeof cmd->resp.cmd) == 0)
+ * - drivers/scsi/virtio_scsi.c|629| <<virtscsi_tmf>> if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd, sizeof cmd->req.tmf, sizeof cmd->resp.tmf) < 0)
+ *
+ * sizeof cmd->req.cmd : sizeof(struct virtio_scsi_cmd_req)
+ * sizeof cmd->resp.cmd : sizeof(struct virtio_scsi_cmd_resp)
+ */
static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
struct virtio_scsi_cmd *cmd,
size_t req_size, size_t resp_size)
@@ -510,6 +541,11 @@ static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
return err;
}
+/*
+ * called by:
+ * - drivers/scsi/virtio_scsi.c|568| <<virtscsi_queuecommand_single>> return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc);
+ * - drivers/scsi/virtio_scsi.c|620| <<virtscsi_queuecommand_multi>> return virtscsi_queuecommand(vscsi, req_vq, sc);
+ */
static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
struct virtio_scsi_vq *req_vq,
struct scsi_cmnd *sc)
@@ -517,7 +553,20 @@ static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
struct virtio_scsi_cmd *cmd;
int ret;
+ /*
+ * struct virtio_scsi *vscsi
+ * -> struct virtio_device *vdev
+ * -> void *priv;
+ */
struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
+ /*
+ * struct scsi_cmnd *cmd:
+ * -> struct scsi_data_buffer sdb;
+ * -> struct sg_table table;
+ * -> struct scatterlist *sgl; // the list
+ * -> unsigned int nents; // number of mapped entries
+ * -> unsigned int orig_nents; // original size of list
+ */
BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
/* TODO: check feature bit and fail if unsupported? */
@@ -527,6 +576,9 @@ static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
"cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
ret = SCSI_MLQUEUE_HOST_BUSY;
+ /*
+ * struct virtio_scsi_cmd *cmd;
+ */
cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
if (!cmd)
goto out;
@@ -547,6 +599,10 @@ static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
+ /*
+ * sizeof cmd->req.cmd : sizeof(struct virtio_scsi_cmd_req)
+ * sizeof cmd->resp.cmd : sizeof(struct virtio_scsi_cmd_resp)
+ */
if (virtscsi_kick_cmd(req_vq, cmd,
sizeof cmd->req.cmd, sizeof cmd->resp.cmd) == 0)
ret = 0;
diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 4074cd63..083d6b38 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -69,6 +69,19 @@ struct vring_virtqueue {
/* Can we use weak barriers? */
bool weak_barriers;
+ /*
+ * 在以下设置vring_virtqueue->broken:
+ * - drivers/virtio/virtio_ring.c|52| <<BAD_RING>> (_vq)->broken = true; \
+ * - drivers/virtio/virtio_ring.c|565| <<virtqueue_notify>> vq->broken = true;
+ * - drivers/virtio/virtio_ring.c|916| <<__vring_new_virtqueue>> vq->broken = false;
+ * - drivers/virtio/virtio_ring.c|1135| <<virtio_break_device>> vq->broken = true;
+ * 在以下使用vring_virtqueue->broken:
+ * - drivers/virtio/virtio_ring.c|271| <<virtqueue_add>> if (unlikely(vq->broken)) {
+ * - drivers/virtio/virtio_ring.c|560| <<virtqueue_notify>> if (unlikely(vq->broken))
+ * - drivers/virtio/virtio_ring.c|663| <<virtqueue_get_buf>> if (unlikely(vq->broken)) {
+ * - drivers/virtio/virtio_ring.c|878| <<vring_interrupt>> if (unlikely(vq->broken))
+ * - drivers/virtio/virtio_ring.c|1121| <<virtqueue_is_broken>> return vq->broken;
+ */
/* Other side has made a mess, don't try any more. */
bool broken;
@@ -78,8 +91,25 @@ struct vring_virtqueue {
/* Host publishes avail event idx */
bool event;
+ /*
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|316| <<virtqueue_add>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|400| <<virtqueue_add>> vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
+ * - drivers/virtio/virtio_ring.c|402| <<virtqueue_add>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|652| <<detach_buf>> vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
+ * - drivers/virtio/virtio_ring.c|653| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|975| <<__vring_new_virtqueue>> vq->free_head = 0;
+ */
/* Head of free buffer list. */
unsigned int free_head;
+ /*
+ * 在以下使用vring_virtqueue->num_added:
+ * - drivers/virtio/virtio_ring.c|418| <<virtqueue_add>> vq->num_added++;
+ * - drivers/virtio/virtio_ring.c|425| <<virtqueue_add>> if (unlikely(vq->num_added == (1 << 16) - 1))
+ * - drivers/virtio/virtio_ring.c|567| <<virtqueue_kick_prepare>> old = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->num_added;
+ * - drivers/virtio/virtio_ring.c|569| <<virtqueue_kick_prepare>> vq->num_added = 0;
+ * - drivers/virtio/virtio_ring.c|960| <<__vring_new_virtqueue>> vq->num_added = 0;
+ */
/* Number we've added since last sync. */
unsigned int num_added;
@@ -103,6 +133,21 @@ struct vring_virtqueue {
ktime_t last_add_time;
#endif
+ /*
+ * 在以下使用vring_virtqueue->desc_state[]:
+ * - drivers/virtio/virtio_ring.c|405| <<virtqueue_add>> vq->desc_state[head].data = data;
+ * - drivers/virtio/virtio_ring.c|407| <<virtqueue_add>> vq->desc_state[head].indir_desc = desc;
+ * - drivers/virtio/virtio_ring.c|640| <<detach_buf>> vq->desc_state[head].data = NULL;
+ * - drivers/virtio/virtio_ring.c|659| <<detach_buf>> if (vq->desc_state[head].indir_desc) {
+ * - drivers/virtio/virtio_ring.c|660| <<detach_buf>> struct vring_desc *indir_desc = vq->desc_state[head].indir_desc;
+ * - drivers/virtio/virtio_ring.c|670| <<detach_buf>> kfree(vq->desc_state[head].indir_desc);
+ * - drivers/virtio/virtio_ring.c|671| <<detach_buf>> vq->desc_state[head].indir_desc = NULL;
+ * - drivers/virtio/virtio_ring.c|727| <<virtqueue_get_buf>> if (unlikely(!vq->desc_state[i].data)) {
+ * - drivers/virtio/virtio_ring.c|733| <<virtqueue_get_buf>> ret = vq->desc_state[i].data;
+ * - drivers/virtio/virtio_ring.c|894| <<virtqueue_detach_unused_buf>> if (!vq->desc_state[i].data)
+ 12 drivers/virtio/virtio_ring.c|897| <<virtqueue_detach_unused_buf>> buf = vq->desc_state[i].data;
+ 13 drivers/virtio/virtio_ring.c|978| <<bool>> memset(vq->desc_state, 0, vring.num * sizeof(struct vring_desc_state));
+ */
/* Per-descriptor state. */
struct vring_desc_state desc_state[];
};
@@ -164,6 +209,11 @@ struct device *vring_dma_dev(const struct vring_virtqueue *vq)
return vq->vq.vdev->dev.parent;
}
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|354| <<virtqueue_add>> dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
+ * - drivers/virtio/virtio_ring.c|367| <<virtqueue_add>> dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
+ */
/* Map one sg entry. */
static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
struct scatterlist *sg,
@@ -182,6 +232,10 @@ static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
direction);
}
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|383| <<virtqueue_add>> dma_addr_t addr = vring_map_single(vq, desc, total_sg * sizeof(struct vring_desc), DMA_TO_DEVICE);
+ */
static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
void *cpu_addr, size_t size,
enum dma_data_direction direction)
@@ -218,6 +272,12 @@ static void vring_unmap_one(const struct vring_virtqueue *vq,
}
}
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|355| <<virtqueue_add>> if (vring_mapping_error(vq, addr))
+ * - drivers/virtio/virtio_ring.c|368| <<virtqueue_add>> if (vring_mapping_error(vq, addr))
+ * - drivers/virtio/virtio_ring.c|386| <<virtqueue_add>> if (vring_mapping_error(vq, addr))
+ */
static int vring_mapping_error(const struct vring_virtqueue *vq,
dma_addr_t addr)
{
@@ -227,6 +287,22 @@ static int vring_mapping_error(const struct vring_virtqueue *vq,
return dma_mapping_error(vring_dma_dev(vq), addr);
}
+/*
+ * crash> vring_desc -x -o
+ * struct vring_desc {
+ * [0x0] __virtio64 addr;
+ * [0x8] __virtio32 len;
+ * [0xc] __virtio16 flags;
+ * [0xe] __virtio16 next;
+ * }
+ * SIZE: 0x10
+ *
+ * 在以下使用alloc_indirect():
+ * - drivers/virtio/virtio_ring.c|321| <<virtqueue_add>> desc = alloc_indirect(_vq, total_sg, gfp);
+ *
+ * 用kmalloc()分配total_sg个vring_desc
+ * 用数组形式的指针串接好
+ */
static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
unsigned int total_sg, gfp_t gfp)
{
@@ -240,6 +316,18 @@ static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
*/
gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
+ /*
+ * struct vring_desc {
+ * // Address (guest-physical).
+ * __virtio64 addr;
+ * // Length.
+ * __virtio32 len;
+ * // The flags as indicated above.
+ * __virtio16 flags;
+ * // We chain unused descriptors via this, too
+ * __virtio16 next;
+ * };
+ */
desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
if (!desc)
return NULL;
@@ -249,6 +337,15 @@ static struct vring_desc *alloc_indirect(struct virtqueue *_vq,
return desc;
}
+/*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|455| <<virtqueue_add_sgs>> return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
+ * - drivers/virtio/virtio_ring.c|477| <<virtqueue_add_outbuf>> return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
+ * - drivers/virtio/virtio_ring.c|499| <<virtqueue_add_inbuf>> return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
+ *
+ * 对于virtio-scsi data是cmd!!!
+ * struct virtio_scsi_cmd *cmd
+ */
static inline int virtqueue_add(struct virtqueue *_vq,
struct scatterlist *sgs[],
unsigned int total_sg,
@@ -257,6 +354,9 @@ static inline int virtqueue_add(struct virtqueue *_vq,
void *data,
gfp_t gfp)
{
+ /*
+ * 注意sgs和sg的区别
+ */
struct vring_virtqueue *vq = to_vvq(_vq);
struct scatterlist *sg;
struct vring_desc *desc;
@@ -268,6 +368,19 @@ static inline int virtqueue_add(struct virtqueue *_vq,
BUG_ON(data == NULL);
+ /*
+ * 在以下设置vring_virtqueue->broken:
+ * - drivers/virtio/virtio_ring.c|52| <<BAD_RING>> (_vq)->broken = true; \
+ * - drivers/virtio/virtio_ring.c|565| <<virtqueue_notify>> vq->broken = true;
+ * - drivers/virtio/virtio_ring.c|916| <<__vring_new_virtqueue>> vq->broken = false;
+ * - drivers/virtio/virtio_ring.c|1135| <<virtio_break_device>> vq->broken = true;
+ * 在以下使用vring_virtqueue->broken:
+ * - drivers/virtio/virtio_ring.c|271| <<virtqueue_add>> if (unlikely(vq->broken)) {
+ * - drivers/virtio/virtio_ring.c|560| <<virtqueue_notify>> if (unlikely(vq->broken))
+ * - drivers/virtio/virtio_ring.c|663| <<virtqueue_get_buf>> if (unlikely(vq->broken)) {
+ * - drivers/virtio/virtio_ring.c|878| <<vring_interrupt>> if (unlikely(vq->broken))
+ * - drivers/virtio/virtio_ring.c|1121| <<virtqueue_is_broken>> return vq->broken;
+ */
if (unlikely(vq->broken)) {
END_USE(vq);
return -EIO;
@@ -288,10 +401,34 @@ static inline int virtqueue_add(struct virtqueue *_vq,
BUG_ON(total_sg == 0);
+ /*
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|316| <<virtqueue_add>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|400| <<virtqueue_add>> vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
+ * - drivers/virtio/virtio_ring.c|402| <<virtqueue_add>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|652| <<detach_buf>> vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
+ * - drivers/virtio/virtio_ring.c|653| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|975| <<__vring_new_virtqueue>> vq->free_head = 0;
+ */
head = vq->free_head;
/* If the host supports indirect descriptor tables, and we have multiple
* buffers, then go indirect. FIXME: tune this threshold */
+ /*
+ * virtio_ring.c文件在以下使用virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|320| <<virtqueue_add>> if (vq->indirect && total_sg > 1 && vq->vq.num_free)
+ * - drivers/virtio/virtio_ring.c|340| <<virtqueue_add>> if (vq->vq.num_free < descs_used) {
+ * - drivers/virtio/virtio_ring.c|342| <<virtqueue_add>> descs_used, vq->vq.num_free);
+ * - drivers/virtio/virtio_ring.c|396| <<virtqueue_add>> vq->vq.num_free -= descs_used;
+ * - drivers/virtio/virtio_ring.c|648| <<detach_buf>> vq->vq.num_free++;
+ * - drivers/virtio/virtio_ring.c|656| <<detach_buf>> vq->vq.num_free++;
+ * - drivers/virtio/virtio_ring.c|904| <<virtqueue_detach_unused_buf>> BUG_ON(vq->vq.num_free != vq->vring.num);
+ * - drivers/virtio/virtio_ring.c|951| <<__vring_new_virtqueue>> vq->vq.num_free = vring.num;
+ *
+ * alloc_indirect()的注释:
+ * 用kmalloc()分配total_sg个vring_desc
+ * 用数组形式的指针串接好
+ */
if (vq->indirect && total_sg > 1 && vq->vq.num_free)
desc = alloc_indirect(_vq, total_sg, gfp);
else {
@@ -308,6 +445,9 @@ static inline int virtqueue_add(struct virtqueue *_vq,
} else {
indirect = false;
desc = vq->vring.desc;
+ /*
+ * head是上面的vq->free_head
+ */
i = head;
descs_used = total_sg;
}
@@ -324,6 +464,20 @@ static inline int virtqueue_add(struct virtqueue *_vq,
return -ENOSPC;
}
+ /*
+ * struct scatterlist {
+ * unsigned long page_link;
+ * unsigned int offset;
+ * unsigned int length;
+ * dma_addr_t dma_address;
+ * #ifdef CONFIG_NEED_SG_DMA_LENGTH
+ * unsigned int dma_length;
+ * #endif
+ * };
+ *
+ * struct scatterlist *sg;
+ */
+
for (n = 0; n < out_sgs; n++) {
for (sg = sgs[n]; sg; sg = sg_next(sg)) {
dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
@@ -364,18 +518,36 @@ static inline int virtqueue_add(struct virtqueue *_vq,
vq->vring.desc[head].flags = cpu_to_virtio16(_vq->vdev, VRING_DESC_F_INDIRECT);
vq->vring.desc[head].addr = cpu_to_virtio64(_vq->vdev, addr);
+ /*
+ * !!! len是total_sg * sizeof(struct vring_desc)
+ */
vq->vring.desc[head].len = cpu_to_virtio32(_vq->vdev, total_sg * sizeof(struct vring_desc));
}
/* We're using some buffers from the free list. */
vq->vq.num_free -= descs_used;
+ /*
+ * 在以下使用vring_virtqueue->free_head:
+ * - drivers/virtio/virtio_ring.c|316| <<virtqueue_add>> head = vq->free_head;
+ * - drivers/virtio/virtio_ring.c|400| <<virtqueue_add>> vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
+ * - drivers/virtio/virtio_ring.c|402| <<virtqueue_add>> vq->free_head = i;
+ * - drivers/virtio/virtio_ring.c|652| <<detach_buf>> vq->vring.desc[i].next = cpu_to_virtio16(vq->vq.vdev, vq->free_head);
+ * - drivers/virtio/virtio_ring.c|653| <<detach_buf>> vq->free_head = head;
+ * - drivers/virtio/virtio_ring.c|975| <<__vring_new_virtqueue>> vq->free_head = 0;
+ */
/* Update free pointer */
if (indirect)
vq->free_head = virtio16_to_cpu(_vq->vdev, vq->vring.desc[head].next);
else
vq->free_head = i;
+ /*
+ * 对于virtio-scsi data是cmd!!!
+ * struct virtio_scsi_cmd *cmd
+ *
+ * head是一开始的vq->free_head
+ */
/* Store token and indirect buffer state. */
vq->desc_state[head].data = data;
if (indirect)
@@ -390,6 +562,14 @@ static inline int virtqueue_add(struct virtqueue *_vq,
* new available array entries. */
virtio_wmb(vq->weak_barriers);
vq->vring.avail->idx = cpu_to_virtio16(_vq->vdev, virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) + 1);
+ /*
+ * 在以下使用vring_virtqueue->num_added:
+ * - drivers/virtio/virtio_ring.c|418| <<virtqueue_add>> vq->num_added++;
+ * - drivers/virtio/virtio_ring.c|425| <<virtqueue_add>> if (unlikely(vq->num_added == (1 << 16) - 1))
+ * - drivers/virtio/virtio_ring.c|567| <<virtqueue_kick_prepare>> old = virtio16_to_cpu(_vq->vdev, vq->vring.avail->idx) - vq->num_added;
+ * - drivers/virtio/virtio_ring.c|569| <<virtqueue_kick_prepare>> vq->num_added = 0;
+ * - drivers/virtio/virtio_ring.c|960| <<__vring_new_virtqueue>> vq->num_added = 0;
+ */
vq->num_added++;
pr_debug("Added buffer head %i to %p\n", head, vq);
@@ -437,6 +617,19 @@ unmap_release:
*
* Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
*/
+/*
+ * called by:
+ * - drivers/block/virtio_blk.c|131| <<__virtblk_add_req>> return virtqueue_add_sgs(vq, sgs, num_out, num_in, vbr, GFP_ATOMIC);
+ * - drivers/gpu/drm/virtio/virtgpu_vq.c|272| <<virtio_gpu_queue_ctrl_buffer_locked>> ret = virtqueue_add_sgs(vq, sgs, outcnt, incnt, vbuf, GFP_ATOMIC);
+ * - drivers/gpu/drm/virtio/virtgpu_vq.c|347| <<virtio_gpu_queue_cursor>> ret = virtqueue_add_sgs(vq, sgs, outcnt, 0, vbuf, GFP_ATOMIC);
+ * - drivers/net/virtio_net.c|864| <<virtnet_send_command>> virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
+ * - drivers/scsi/virtio_scsi.c|490| <<virtscsi_add_cmd>> return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, GFP_ATOMIC);
+ * - net/9p/trans_virtio.c|290| <<p9_virtio_request>> err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
+ * - net/9p/trans_virtio.c|449| <<p9_virtio_zc_request>> err = virtqueue_add_sgs(chan->vq, sgs, out_sgs, in_sgs, req->tc,
+ * - net/vmw_vsock/virtio_transport.c|161| <<virtio_transport_send_pkt_work>> ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
+ * - net/vmw_vsock/virtio_transport.c|249| <<virtio_vsock_rx_fill>> ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
+ * - tools/virtio/vringh_test.c|505| <<main>> err = virtqueue_add_sgs(vq, sgs, 1, 1, &err, GFP_KERNEL);
+ */
int virtqueue_add_sgs(struct virtqueue *_vq,
struct scatterlist *sgs[],
unsigned int out_sgs,
@@ -452,6 +645,15 @@ int virtqueue_add_sgs(struct virtqueue *_vq,
for (sg = sgs[i]; sg; sg = sg_next(sg))
total_sg++;
}
+ /*
+ * called by:
+ * - drivers/virtio/virtio_ring.c|455| <<virtqueue_add_sgs>> return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
+ * - drivers/virtio/virtio_ring.c|477| <<virtqueue_add_outbuf>> return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
+ * - drivers/virtio/virtio_ring.c|499| <<virtqueue_add_inbuf>> return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
+ *
+ * 对于virtio-scsi data是cmd!!!
+ * struct virtio_scsi_cmd *cmd
+ */
return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
}
EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 8e2c546d..a03873b4 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -30,6 +30,17 @@ struct virtqueue {
const char *name;
struct virtio_device *vdev;
unsigned int index;
+ /*
+ * virtio_ring.c文件在以下使用virtqueue->num_free:
+ * - drivers/virtio/virtio_ring.c|320| <<virtqueue_add>> if (vq->indirect && total_sg > 1 && vq->vq.num_free)
+ * - drivers/virtio/virtio_ring.c|340| <<virtqueue_add>> if (vq->vq.num_free < descs_used) {
+ * - drivers/virtio/virtio_ring.c|342| <<virtqueue_add>> descs_used, vq->vq.num_free);
+ * - drivers/virtio/virtio_ring.c|396| <<virtqueue_add>> vq->vq.num_free -= descs_used;
+ * - drivers/virtio/virtio_ring.c|648| <<detach_buf>> vq->vq.num_free++;
+ * - drivers/virtio/virtio_ring.c|656| <<detach_buf>> vq->vq.num_free++;
+ * - drivers/virtio/virtio_ring.c|904| <<virtqueue_detach_unused_buf>> BUG_ON(vq->vq.num_free != vq->vring.num);
+ * - drivers/virtio/virtio_ring.c|951| <<__vring_new_virtqueue>> vq->vq.num_free = vring.num;
+ */
unsigned int num_free;
void *priv;
};
diff --git a/include/scsi/scsi_cmnd.h b/include/scsi/scsi_cmnd.h
index 6517e173..b4ec92af 100644
--- a/include/scsi/scsi_cmnd.h
+++ b/include/scsi/scsi_cmnd.h
@@ -183,6 +183,14 @@ extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
{
+ /*
+ * struct scsi_cmnd *cmd:
+ * -> struct scsi_data_buffer sdb;
+ * -> struct sg_table table;
+ * -> struct scatterlist *sgl; // the list
+ * -> unsigned int nents; // number of mapped entries
+ * -> unsigned int orig_nents; // original size of list
+ */
return cmd->sdb.table.nents;
}
--
2.34.1