This source file includes following definitions.
- sd_zbc_parse_report
- sd_zbc_do_report_zones
- sd_zbc_alloc_report_buffer
- sd_zbc_report_zones
- sd_zbc_zone_sectors
- sd_zbc_setup_reset_cmnd
- sd_zbc_complete
- sd_zbc_check_zoned_characteristics
- sd_zbc_check_zones
- sd_zbc_read_zones
- sd_zbc_print_zones
1
2
3
4
5
6
7
8
9
10
11 #include <linux/blkdev.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
14
15 #include <asm/unaligned.h>
16
17 #include <scsi/scsi.h>
18 #include <scsi/scsi_cmnd.h>
19
20 #include "sd.h"
21
22
23
24
25
26
27
28
29
30 static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
31 struct blk_zone *zone)
32 {
33 struct scsi_device *sdp = sdkp->device;
34
35 memset(zone, 0, sizeof(struct blk_zone));
36
37 zone->type = buf[0] & 0x0f;
38 zone->cond = (buf[1] >> 4) & 0xf;
39 if (buf[1] & 0x01)
40 zone->reset = 1;
41 if (buf[1] & 0x02)
42 zone->non_seq = 1;
43
44 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
45 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
46 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
47 if (zone->type != ZBC_ZONE_TYPE_CONV &&
48 zone->cond == ZBC_ZONE_COND_FULL)
49 zone->wp = zone->start + zone->len;
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
67 unsigned int buflen, sector_t lba,
68 bool partial)
69 {
70 struct scsi_device *sdp = sdkp->device;
71 const int timeout = sdp->request_queue->rq_timeout;
72 struct scsi_sense_hdr sshdr;
73 unsigned char cmd[16];
74 unsigned int rep_len;
75 int result;
76
77 memset(cmd, 0, 16);
78 cmd[0] = ZBC_IN;
79 cmd[1] = ZI_REPORT_ZONES;
80 put_unaligned_be64(lba, &cmd[2]);
81 put_unaligned_be32(buflen, &cmd[10]);
82 if (partial)
83 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
84
85 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
86 buf, buflen, &sshdr,
87 timeout, SD_MAX_RETRIES, NULL);
88 if (result) {
89 sd_printk(KERN_ERR, sdkp,
90 "REPORT ZONES lba %llu failed with %d/%d\n",
91 (unsigned long long)lba,
92 host_byte(result), driver_byte(result));
93 return -EIO;
94 }
95
96 rep_len = get_unaligned_be32(&buf[0]);
97 if (rep_len < 64) {
98 sd_printk(KERN_ERR, sdkp,
99 "REPORT ZONES report invalid length %u\n",
100 rep_len);
101 return -EIO;
102 }
103
104 return 0;
105 }
106
107
108
109
110 #define SD_ZBC_REPORT_MAX_ZONES 8192U
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
126 unsigned int nr_zones, size_t *buflen)
127 {
128 struct request_queue *q = sdkp->disk->queue;
129 size_t bufsize;
130 void *buf;
131
132
133
134
135
136
137
138
139
140
141 nr_zones = min(nr_zones, SD_ZBC_REPORT_MAX_ZONES);
142 bufsize = roundup((nr_zones + 1) * 64, 512);
143 bufsize = min_t(size_t, bufsize,
144 queue_max_hw_sectors(q) << SECTOR_SHIFT);
145 bufsize = min_t(size_t, bufsize, queue_max_segments(q) << PAGE_SHIFT);
146
147 buf = vzalloc(bufsize);
148 if (buf)
149 *buflen = bufsize;
150
151 return buf;
152 }
153
154
155
156
157
158
159
160
161
162
163 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
164 struct blk_zone *zones, unsigned int *nr_zones)
165 {
166 struct scsi_disk *sdkp = scsi_disk(disk);
167 unsigned int i, nrz = *nr_zones;
168 unsigned char *buf;
169 size_t buflen = 0, offset = 0;
170 int ret = 0;
171
172 if (!sd_is_zoned(sdkp))
173
174 return -EOPNOTSUPP;
175
176 buf = sd_zbc_alloc_report_buffer(sdkp, nrz, &buflen);
177 if (!buf)
178 return -ENOMEM;
179
180 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
181 sectors_to_logical(sdkp->device, sector), true);
182 if (ret)
183 goto out;
184
185 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
186 for (i = 0; i < nrz; i++) {
187 offset += 64;
188 sd_zbc_parse_report(sdkp, buf + offset, zones);
189 zones++;
190 }
191
192 *nr_zones = nrz;
193
194 out:
195 kvfree(buf);
196
197 return ret;
198 }
199
200
201
202
203
204 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
205 {
206 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
207 }
208
209
210
211
212
213
214
215
216 blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd, bool all)
217 {
218 struct request *rq = cmd->request;
219 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
220 sector_t sector = blk_rq_pos(rq);
221 sector_t block = sectors_to_logical(sdkp->device, sector);
222
223 if (!sd_is_zoned(sdkp))
224
225 return BLK_STS_IOERR;
226
227 if (sdkp->device->changed)
228 return BLK_STS_IOERR;
229
230 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
231
232 return BLK_STS_IOERR;
233
234 cmd->cmd_len = 16;
235 memset(cmd->cmnd, 0, cmd->cmd_len);
236 cmd->cmnd[0] = ZBC_OUT;
237 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
238 if (all)
239 cmd->cmnd[14] = 0x1;
240 else
241 put_unaligned_be64(block, &cmd->cmnd[2]);
242
243 rq->timeout = SD_TIMEOUT;
244 cmd->sc_data_direction = DMA_NONE;
245 cmd->transfersize = 0;
246 cmd->allowed = 0;
247
248 return BLK_STS_OK;
249 }
250
251
252
253
254
255
256
257
258
259
260 void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
261 struct scsi_sense_hdr *sshdr)
262 {
263 int result = cmd->result;
264 struct request *rq = cmd->request;
265
266 if (req_op(rq) == REQ_OP_ZONE_RESET &&
267 result &&
268 sshdr->sense_key == ILLEGAL_REQUEST &&
269 sshdr->asc == 0x24) {
270
271
272
273
274
275 rq->rq_flags |= RQF_QUIET;
276 }
277 }
278
279
280
281
282
283
284
285
286 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
287 unsigned char *buf)
288 {
289
290 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
291 sd_printk(KERN_NOTICE, sdkp,
292 "Read zoned characteristics VPD page failed\n");
293 return -ENODEV;
294 }
295
296 if (sdkp->device->type != TYPE_ZBC) {
297
298 sdkp->urswrz = 1;
299 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
300 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
301 sdkp->zones_max_open = 0;
302 } else {
303
304 sdkp->urswrz = buf[4] & 1;
305 sdkp->zones_optimal_open = 0;
306 sdkp->zones_optimal_nonseq = 0;
307 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
308 }
309
310
311
312
313
314
315 if (!sdkp->urswrz) {
316 if (sdkp->first_scan)
317 sd_printk(KERN_NOTICE, sdkp,
318 "constrained reads devices are not supported\n");
319 return -ENODEV;
320 }
321
322 return 0;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks)
339 {
340 size_t bufsize, buflen;
341 unsigned int noio_flag;
342 u64 zone_blocks = 0;
343 sector_t max_lba, block = 0;
344 unsigned char *buf;
345 unsigned char *rec;
346 int ret;
347 u8 same;
348
349
350 noio_flag = memalloc_noio_save();
351
352
353 buf = sd_zbc_alloc_report_buffer(sdkp, SD_ZBC_REPORT_MAX_ZONES,
354 &bufsize);
355 if (!buf) {
356 ret = -ENOMEM;
357 goto out;
358 }
359
360
361 ret = sd_zbc_do_report_zones(sdkp, buf, bufsize, 0, false);
362 if (ret)
363 goto out_free;
364
365 if (sdkp->rc_basis == 0) {
366
367 max_lba = get_unaligned_be64(&buf[8]);
368 if (sdkp->capacity != max_lba + 1) {
369 if (sdkp->first_scan)
370 sd_printk(KERN_WARNING, sdkp,
371 "Changing capacity from %llu to max LBA+1 %llu\n",
372 (unsigned long long)sdkp->capacity,
373 (unsigned long long)max_lba + 1);
374 sdkp->capacity = max_lba + 1;
375 }
376 }
377
378
379
380
381
382 same = buf[4] & 0x0f;
383 if (same > 0) {
384 rec = &buf[64];
385 zone_blocks = get_unaligned_be64(&rec[8]);
386 goto out;
387 }
388
389
390
391
392
393
394 do {
395
396
397 buflen = min_t(size_t, get_unaligned_be32(&buf[0]) + 64,
398 bufsize);
399 rec = buf + 64;
400
401
402 while (rec < buf + buflen) {
403 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
404
405 if (zone_blocks == 0) {
406 zone_blocks = this_zone_blocks;
407 } else if (this_zone_blocks != zone_blocks &&
408 (block + this_zone_blocks < sdkp->capacity
409 || this_zone_blocks > zone_blocks)) {
410 zone_blocks = 0;
411 goto out;
412 }
413 block += this_zone_blocks;
414 rec += 64;
415 }
416
417 if (block < sdkp->capacity) {
418 ret = sd_zbc_do_report_zones(sdkp, buf, bufsize, block,
419 true);
420 if (ret)
421 goto out_free;
422 }
423
424 } while (block < sdkp->capacity);
425
426 out:
427 if (!zone_blocks) {
428 if (sdkp->first_scan)
429 sd_printk(KERN_NOTICE, sdkp,
430 "Devices with non constant zone "
431 "size are not supported\n");
432 ret = -ENODEV;
433 } else if (!is_power_of_2(zone_blocks)) {
434 if (sdkp->first_scan)
435 sd_printk(KERN_NOTICE, sdkp,
436 "Devices with non power of 2 zone "
437 "size are not supported\n");
438 ret = -ENODEV;
439 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
440 if (sdkp->first_scan)
441 sd_printk(KERN_NOTICE, sdkp,
442 "Zone size too large\n");
443 ret = -EFBIG;
444 } else {
445 *zblocks = zone_blocks;
446 ret = 0;
447 }
448
449 out_free:
450 memalloc_noio_restore(noio_flag);
451 kvfree(buf);
452
453 return ret;
454 }
455
456 int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
457 {
458 struct gendisk *disk = sdkp->disk;
459 unsigned int nr_zones;
460 u32 zone_blocks = 0;
461 int ret;
462
463 if (!sd_is_zoned(sdkp))
464
465
466
467
468 return 0;
469
470
471 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
472 if (ret)
473 goto err;
474
475
476
477
478
479 ret = sd_zbc_check_zones(sdkp, &zone_blocks);
480 if (ret != 0)
481 goto err;
482
483
484 blk_queue_chunk_sectors(sdkp->disk->queue,
485 logical_to_sectors(sdkp->device, zone_blocks));
486 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, sdkp->disk->queue);
487 blk_queue_required_elevator_features(sdkp->disk->queue,
488 ELEVATOR_F_ZBD_SEQ_WRITE);
489 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
490
491
492 sdkp->device->use_16_for_rw = 1;
493 sdkp->device->use_10_for_rw = 0;
494
495
496
497
498
499
500 if (sdkp->first_scan) {
501 sdkp->zone_blocks = zone_blocks;
502 sdkp->nr_zones = nr_zones;
503 return 0;
504 }
505
506 if (sdkp->zone_blocks != zone_blocks ||
507 sdkp->nr_zones != nr_zones ||
508 disk->queue->nr_zones != nr_zones) {
509 ret = blk_revalidate_disk_zones(disk);
510 if (ret != 0)
511 goto err;
512 sdkp->zone_blocks = zone_blocks;
513 sdkp->nr_zones = nr_zones;
514 }
515
516 return 0;
517
518 err:
519 sdkp->capacity = 0;
520
521 return ret;
522 }
523
524 void sd_zbc_print_zones(struct scsi_disk *sdkp)
525 {
526 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
527 return;
528
529 if (sdkp->capacity & (sdkp->zone_blocks - 1))
530 sd_printk(KERN_NOTICE, sdkp,
531 "%u zones of %u logical blocks + 1 runt zone\n",
532 sdkp->nr_zones - 1,
533 sdkp->zone_blocks);
534 else
535 sd_printk(KERN_NOTICE, sdkp,
536 "%u zones of %u logical blocks\n",
537 sdkp->nr_zones,
538 sdkp->zone_blocks);
539 }