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#include <linux/blkdev.h>
26
27#include <asm/unaligned.h>
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_cmnd.h>
31
32#include "sd.h"
33
34
35
36
37
38
39
40
41
42static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
43 struct blk_zone *zone)
44{
45 struct scsi_device *sdp = sdkp->device;
46
47 memset(zone, 0, sizeof(struct blk_zone));
48
49 zone->type = buf[0] & 0x0f;
50 zone->cond = (buf[1] >> 4) & 0xf;
51 if (buf[1] & 0x01)
52 zone->reset = 1;
53 if (buf[1] & 0x02)
54 zone->non_seq = 1;
55
56 zone->len = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
57 zone->start = logical_to_sectors(sdp, get_unaligned_be64(&buf[16]));
58 zone->wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
59 if (zone->type != ZBC_ZONE_TYPE_CONV &&
60 zone->cond == ZBC_ZONE_COND_FULL)
61 zone->wp = zone->start + zone->len;
62}
63
64
65
66
67
68
69
70
71
72
73static int sd_zbc_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
74 unsigned int buflen, sector_t lba)
75{
76 struct scsi_device *sdp = sdkp->device;
77 const int timeout = sdp->request_queue->rq_timeout;
78 struct scsi_sense_hdr sshdr;
79 unsigned char cmd[16];
80 unsigned int rep_len;
81 int result;
82
83 memset(cmd, 0, 16);
84 cmd[0] = ZBC_IN;
85 cmd[1] = ZI_REPORT_ZONES;
86 put_unaligned_be64(lba, &cmd[2]);
87 put_unaligned_be32(buflen, &cmd[10]);
88 memset(buf, 0, buflen);
89
90 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
91 buf, buflen, &sshdr,
92 timeout, SD_MAX_RETRIES, NULL);
93 if (result) {
94 sd_printk(KERN_ERR, sdkp,
95 "REPORT ZONES lba %llu failed with %d/%d\n",
96 (unsigned long long)lba,
97 host_byte(result), driver_byte(result));
98 return -EIO;
99 }
100
101 rep_len = get_unaligned_be32(&buf[0]);
102 if (rep_len < 64) {
103 sd_printk(KERN_ERR, sdkp,
104 "REPORT ZONES report invalid length %u\n",
105 rep_len);
106 return -EIO;
107 }
108
109 return 0;
110}
111
112
113
114
115
116
117
118int sd_zbc_setup_report_cmnd(struct scsi_cmnd *cmd)
119{
120 struct request *rq = cmd->request;
121 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
122 sector_t lba, sector = blk_rq_pos(rq);
123 unsigned int nr_bytes = blk_rq_bytes(rq);
124 int ret;
125
126 WARN_ON(nr_bytes == 0);
127
128 if (!sd_is_zoned(sdkp))
129
130 return BLKPREP_KILL;
131
132 ret = scsi_init_io(cmd);
133 if (ret != BLKPREP_OK)
134 return ret;
135
136 cmd->cmd_len = 16;
137 memset(cmd->cmnd, 0, cmd->cmd_len);
138 cmd->cmnd[0] = ZBC_IN;
139 cmd->cmnd[1] = ZI_REPORT_ZONES;
140 lba = sectors_to_logical(sdkp->device, sector);
141 put_unaligned_be64(lba, &cmd->cmnd[2]);
142 put_unaligned_be32(nr_bytes, &cmd->cmnd[10]);
143
144 cmd->cmnd[14] = ZBC_REPORT_ZONE_PARTIAL;
145
146 cmd->sc_data_direction = DMA_FROM_DEVICE;
147 cmd->sdb.length = nr_bytes;
148 cmd->transfersize = sdkp->device->sector_size;
149 cmd->allowed = 0;
150
151
152
153
154
155 rq->__data_len = nr_bytes;
156
157 return BLKPREP_OK;
158}
159
160
161
162
163
164
165
166
167
168static void sd_zbc_report_zones_complete(struct scsi_cmnd *scmd,
169 unsigned int good_bytes)
170{
171 struct request *rq = scmd->request;
172 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
173 struct sg_mapping_iter miter;
174 struct blk_zone_report_hdr hdr;
175 struct blk_zone zone;
176 unsigned int offset, bytes = 0;
177 unsigned long flags;
178 u8 *buf;
179
180 if (good_bytes < 64)
181 return;
182
183 memset(&hdr, 0, sizeof(struct blk_zone_report_hdr));
184
185 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
186 SG_MITER_TO_SG | SG_MITER_ATOMIC);
187
188 local_irq_save(flags);
189 while (sg_miter_next(&miter) && bytes < good_bytes) {
190
191 buf = miter.addr;
192 offset = 0;
193
194 if (bytes == 0) {
195
196 hdr.nr_zones = min_t(unsigned int,
197 (good_bytes - 64) / 64,
198 get_unaligned_be32(&buf[0]) / 64);
199 memcpy(buf, &hdr, sizeof(struct blk_zone_report_hdr));
200 offset += 64;
201 bytes += 64;
202 }
203
204
205 while (offset < miter.length && hdr.nr_zones) {
206 WARN_ON(offset > miter.length);
207 buf = miter.addr + offset;
208 sd_zbc_parse_report(sdkp, buf, &zone);
209 memcpy(buf, &zone, sizeof(struct blk_zone));
210 offset += 64;
211 bytes += 64;
212 hdr.nr_zones--;
213 }
214
215 if (!hdr.nr_zones)
216 break;
217
218 }
219 sg_miter_stop(&miter);
220 local_irq_restore(flags);
221}
222
223
224
225
226
227static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
228{
229 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
230}
231
232
233
234
235
236
237
238int sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
239{
240 struct request *rq = cmd->request;
241 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
242 sector_t sector = blk_rq_pos(rq);
243 sector_t block = sectors_to_logical(sdkp->device, sector);
244
245 if (!sd_is_zoned(sdkp))
246
247 return BLKPREP_KILL;
248
249 if (sdkp->device->changed)
250 return BLKPREP_KILL;
251
252 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
253
254 return BLKPREP_KILL;
255
256 cmd->cmd_len = 16;
257 memset(cmd->cmnd, 0, cmd->cmd_len);
258 cmd->cmnd[0] = ZBC_OUT;
259 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
260 put_unaligned_be64(block, &cmd->cmnd[2]);
261
262 rq->timeout = SD_TIMEOUT;
263 cmd->sc_data_direction = DMA_NONE;
264 cmd->transfersize = 0;
265 cmd->allowed = 0;
266
267 return BLKPREP_OK;
268}
269
270
271
272
273
274
275
276
277
278
279void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
280 struct scsi_sense_hdr *sshdr)
281{
282 int result = cmd->result;
283 struct request *rq = cmd->request;
284
285 switch (req_op(rq)) {
286 case REQ_OP_ZONE_RESET:
287
288 if (result &&
289 sshdr->sense_key == ILLEGAL_REQUEST &&
290 sshdr->asc == 0x24)
291
292
293
294
295
296 rq->rq_flags |= RQF_QUIET;
297 break;
298
299 case REQ_OP_WRITE:
300 case REQ_OP_WRITE_ZEROES:
301 case REQ_OP_WRITE_SAME:
302 break;
303
304 case REQ_OP_ZONE_REPORT:
305
306 if (!result)
307 sd_zbc_report_zones_complete(cmd, good_bytes);
308 break;
309
310 }
311}
312
313
314
315
316
317
318
319
320static int sd_zbc_read_zoned_characteristics(struct scsi_disk *sdkp,
321 unsigned char *buf)
322{
323
324 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
325 sd_printk(KERN_NOTICE, sdkp,
326 "Unconstrained-read check failed\n");
327 return -ENODEV;
328 }
329
330 if (sdkp->device->type != TYPE_ZBC) {
331
332 sdkp->urswrz = 1;
333 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
334 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
335 sdkp->zones_max_open = 0;
336 } else {
337
338 sdkp->urswrz = buf[4] & 1;
339 sdkp->zones_optimal_open = 0;
340 sdkp->zones_optimal_nonseq = 0;
341 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
342 }
343
344 return 0;
345}
346
347
348
349
350
351
352
353
354
355
356
357static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf)
358{
359 sector_t lba;
360 int ret;
361
362 if (sdkp->rc_basis != 0)
363 return 0;
364
365
366 ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
367 if (ret)
368 return ret;
369
370
371 lba = get_unaligned_be64(&buf[8]);
372 if (lba + 1 == sdkp->capacity)
373 return 0;
374
375 if (sdkp->first_scan)
376 sd_printk(KERN_WARNING, sdkp,
377 "Changing capacity from %llu to max LBA+1 %llu\n",
378 (unsigned long long)sdkp->capacity,
379 (unsigned long long)lba + 1);
380 sdkp->capacity = lba + 1;
381
382 return 0;
383}
384
385#define SD_ZBC_BUF_SIZE 131072U
386
387
388
389
390
391
392
393
394
395
396
397static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp)
398{
399 u64 zone_blocks = 0;
400 sector_t block = 0;
401 unsigned char *buf;
402 unsigned char *rec;
403 unsigned int buf_len;
404 unsigned int list_length;
405 s64 ret;
406 u8 same;
407
408
409 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
410 if (!buf)
411 return -ENOMEM;
412
413
414 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
415 if (ret)
416 goto out_free;
417
418 same = buf[4] & 0x0f;
419 if (same > 0) {
420 rec = &buf[64];
421 zone_blocks = get_unaligned_be64(&rec[8]);
422 goto out;
423 }
424
425
426
427
428
429
430 do {
431
432
433 list_length = get_unaligned_be32(&buf[0]) + 64;
434 rec = buf + 64;
435 buf_len = min(list_length, SD_ZBC_BUF_SIZE);
436
437
438 while (rec < buf + buf_len) {
439 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
440
441 if (zone_blocks == 0) {
442 zone_blocks = this_zone_blocks;
443 } else if (this_zone_blocks != zone_blocks &&
444 (block + this_zone_blocks < sdkp->capacity
445 || this_zone_blocks > zone_blocks)) {
446 zone_blocks = 0;
447 goto out;
448 }
449 block += this_zone_blocks;
450 rec += 64;
451 }
452
453 if (block < sdkp->capacity) {
454 ret = sd_zbc_report_zones(sdkp, buf,
455 SD_ZBC_BUF_SIZE, block);
456 if (ret)
457 goto out_free;
458 }
459
460 } while (block < sdkp->capacity);
461
462out:
463 if (!zone_blocks) {
464 if (sdkp->first_scan)
465 sd_printk(KERN_NOTICE, sdkp,
466 "Devices with non constant zone "
467 "size are not supported\n");
468 ret = -ENODEV;
469 } else if (!is_power_of_2(zone_blocks)) {
470 if (sdkp->first_scan)
471 sd_printk(KERN_NOTICE, sdkp,
472 "Devices with non power of 2 zone "
473 "size are not supported\n");
474 ret = -ENODEV;
475 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
476 if (sdkp->first_scan)
477 sd_printk(KERN_NOTICE, sdkp,
478 "Zone size too large\n");
479 ret = -ENODEV;
480 } else {
481 ret = zone_blocks;
482 }
483
484out_free:
485 kfree(buf);
486
487 return ret;
488}
489
490
491
492
493
494
495static inline unsigned long *
496sd_zbc_alloc_zone_bitmap(u32 nr_zones, int numa_node)
497{
498 return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
499 GFP_KERNEL, numa_node);
500}
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf,
517 unsigned int buflen, u32 zone_shift,
518 unsigned long *seq_zones_bitmap)
519{
520 sector_t lba, next_lba = sdkp->capacity;
521 unsigned int buf_len, list_length;
522 unsigned char *rec;
523 u8 type, cond;
524
525 list_length = get_unaligned_be32(&buf[0]) + 64;
526 buf_len = min(list_length, buflen);
527 rec = buf + 64;
528
529 while (rec < buf + buf_len) {
530 type = rec[0] & 0x0f;
531 cond = (rec[1] >> 4) & 0xf;
532 lba = get_unaligned_be64(&rec[16]);
533 if (type != ZBC_ZONE_TYPE_CONV &&
534 cond != ZBC_ZONE_COND_READONLY &&
535 cond != ZBC_ZONE_COND_OFFLINE)
536 set_bit(lba >> zone_shift, seq_zones_bitmap);
537 next_lba = lba + get_unaligned_be64(&rec[8]);
538 rec += 64;
539 }
540
541 return next_lba;
542}
543
544
545
546
547
548
549
550
551
552static unsigned long *
553sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp, u32 zone_shift,
554 u32 nr_zones)
555{
556 struct request_queue *q = sdkp->disk->queue;
557 unsigned long *seq_zones_bitmap;
558 sector_t lba = 0;
559 unsigned char *buf;
560 int ret = -ENOMEM;
561
562 seq_zones_bitmap = sd_zbc_alloc_zone_bitmap(nr_zones, q->node);
563 if (!seq_zones_bitmap)
564 return ERR_PTR(-ENOMEM);
565
566 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
567 if (!buf)
568 goto out;
569
570 while (lba < sdkp->capacity) {
571 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, lba);
572 if (ret)
573 goto out;
574 lba = sd_zbc_get_seq_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
575 zone_shift, seq_zones_bitmap);
576 }
577
578 if (lba != sdkp->capacity) {
579
580 ret = -EIO;
581 }
582
583out:
584 kfree(buf);
585 if (ret) {
586 kfree(seq_zones_bitmap);
587 return ERR_PTR(ret);
588 }
589 return seq_zones_bitmap;
590}
591
592static void sd_zbc_cleanup(struct scsi_disk *sdkp)
593{
594 struct request_queue *q = sdkp->disk->queue;
595
596 kfree(q->seq_zones_bitmap);
597 q->seq_zones_bitmap = NULL;
598
599 kfree(q->seq_zones_wlock);
600 q->seq_zones_wlock = NULL;
601
602 q->nr_zones = 0;
603}
604
605static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks)
606{
607 struct request_queue *q = sdkp->disk->queue;
608 u32 zone_shift = ilog2(zone_blocks);
609 u32 nr_zones;
610 int ret;
611
612
613 blk_queue_chunk_sectors(q,
614 logical_to_sectors(sdkp->device, zone_blocks));
615 nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift;
616
617
618
619
620
621 if (nr_zones != sdkp->nr_zones || nr_zones != q->nr_zones) {
622 unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
623 size_t zone_bitmap_size;
624
625 if (nr_zones) {
626 seq_zones_wlock = sd_zbc_alloc_zone_bitmap(nr_zones,
627 q->node);
628 if (!seq_zones_wlock) {
629 ret = -ENOMEM;
630 goto err;
631 }
632
633 seq_zones_bitmap = sd_zbc_setup_seq_zones_bitmap(sdkp,
634 zone_shift, nr_zones);
635 if (IS_ERR(seq_zones_bitmap)) {
636 ret = PTR_ERR(seq_zones_bitmap);
637 kfree(seq_zones_wlock);
638 goto err;
639 }
640 }
641 zone_bitmap_size = BITS_TO_LONGS(nr_zones) *
642 sizeof(unsigned long);
643 blk_mq_freeze_queue(q);
644 if (q->nr_zones != nr_zones) {
645
646 sdkp->device->use_16_for_rw = 1;
647 sdkp->device->use_10_for_rw = 0;
648
649 sdkp->zone_blocks = zone_blocks;
650 sdkp->zone_shift = zone_shift;
651 sdkp->nr_zones = nr_zones;
652 q->nr_zones = nr_zones;
653 swap(q->seq_zones_wlock, seq_zones_wlock);
654 swap(q->seq_zones_bitmap, seq_zones_bitmap);
655 } else if (memcmp(q->seq_zones_bitmap, seq_zones_bitmap,
656 zone_bitmap_size) != 0) {
657 memcpy(q->seq_zones_bitmap, seq_zones_bitmap,
658 zone_bitmap_size);
659 }
660 blk_mq_unfreeze_queue(q);
661 kfree(seq_zones_wlock);
662 kfree(seq_zones_bitmap);
663 }
664
665 return 0;
666
667err:
668 sd_zbc_cleanup(sdkp);
669 return ret;
670}
671
672int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
673{
674 int64_t zone_blocks;
675 int ret;
676
677 if (!sd_is_zoned(sdkp))
678
679
680
681
682 return 0;
683
684
685 ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
686 if (ret)
687 goto err;
688
689
690
691
692
693
694 if (!sdkp->urswrz) {
695 if (sdkp->first_scan)
696 sd_printk(KERN_NOTICE, sdkp,
697 "constrained reads devices are not supported\n");
698 ret = -ENODEV;
699 goto err;
700 }
701
702
703 ret = sd_zbc_check_capacity(sdkp, buf);
704 if (ret)
705 goto err;
706
707
708
709
710
711 zone_blocks = sd_zbc_check_zone_size(sdkp);
712 ret = -EFBIG;
713 if (zone_blocks != (u32)zone_blocks)
714 goto err;
715 ret = zone_blocks;
716 if (ret < 0)
717 goto err;
718
719
720 ret = sd_zbc_setup(sdkp, zone_blocks);
721 if (ret)
722 goto err;
723
724 return 0;
725
726err:
727 sdkp->capacity = 0;
728 sd_zbc_cleanup(sdkp);
729
730 return ret;
731}
732
733void sd_zbc_remove(struct scsi_disk *sdkp)
734{
735 sd_zbc_cleanup(sdkp);
736}
737
738void sd_zbc_print_zones(struct scsi_disk *sdkp)
739{
740 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
741 return;
742
743 if (sdkp->capacity & (sdkp->zone_blocks - 1))
744 sd_printk(KERN_NOTICE, sdkp,
745 "%u zones of %u logical blocks + 1 runt zone\n",
746 sdkp->nr_zones - 1,
747 sdkp->zone_blocks);
748 else
749 sd_printk(KERN_NOTICE, sdkp,
750 "%u zones of %u logical blocks\n",
751 sdkp->nr_zones,
752 sdkp->zone_blocks);
753}
754