1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SCSI Zoned Block commands
4 *
5 * Copyright (C) 2014-2015 SUSE Linux GmbH
6 * Written by: Hannes Reinecke <hare@suse.de>
7 * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
8 * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
9 */
10
11 #include <linux/blkdev.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sched/mm.h>
14 #include <linux/mutex.h>
15
16 #include <asm/unaligned.h>
17
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
20
21 #include "sd.h"
22
23 #define CREATE_TRACE_POINTS
24 #include "sd_trace.h"
25
26 /**
27 * sd_zbc_get_zone_wp_offset - Get zone write pointer offset.
28 * @zone: Zone for which to return the write pointer offset.
29 *
30 * Return: offset of the write pointer from the start of the zone.
31 */
sd_zbc_get_zone_wp_offset(struct blk_zone * zone)32 static unsigned int sd_zbc_get_zone_wp_offset(struct blk_zone *zone)
33 {
34 if (zone->type == ZBC_ZONE_TYPE_CONV)
35 return 0;
36
37 switch (zone->cond) {
38 case BLK_ZONE_COND_IMP_OPEN:
39 case BLK_ZONE_COND_EXP_OPEN:
40 case BLK_ZONE_COND_CLOSED:
41 return zone->wp - zone->start;
42 case BLK_ZONE_COND_FULL:
43 return zone->len;
44 case BLK_ZONE_COND_EMPTY:
45 case BLK_ZONE_COND_OFFLINE:
46 case BLK_ZONE_COND_READONLY:
47 default:
48 /*
49 * Offline and read-only zones do not have a valid
50 * write pointer. Use 0 as for an empty zone.
51 */
52 return 0;
53 }
54 }
55
56 /* Whether or not a SCSI zone descriptor describes a gap zone. */
sd_zbc_is_gap_zone(const u8 buf[64])57 static bool sd_zbc_is_gap_zone(const u8 buf[64])
58 {
59 return (buf[0] & 0xf) == ZBC_ZONE_TYPE_GAP;
60 }
61
62 /**
63 * sd_zbc_parse_report - Parse a SCSI zone descriptor
64 * @sdkp: SCSI disk pointer.
65 * @buf: SCSI zone descriptor.
66 * @idx: Index of the zone relative to the first zone reported by the current
67 * sd_zbc_report_zones() call.
68 * @cb: Callback function pointer.
69 * @data: Second argument passed to @cb.
70 *
71 * Return: Value returned by @cb.
72 *
73 * Convert a SCSI zone descriptor into struct blk_zone format. Additionally,
74 * call @cb(blk_zone, @data).
75 */
sd_zbc_parse_report(struct scsi_disk * sdkp,const u8 buf[64],unsigned int idx,report_zones_cb cb,void * data)76 static int sd_zbc_parse_report(struct scsi_disk *sdkp, const u8 buf[64],
77 unsigned int idx, report_zones_cb cb, void *data)
78 {
79 struct scsi_device *sdp = sdkp->device;
80 struct blk_zone zone = { 0 };
81 sector_t start_lba, gran;
82 int ret;
83
84 if (WARN_ON_ONCE(sd_zbc_is_gap_zone(buf)))
85 return -EINVAL;
86
87 zone.type = buf[0] & 0x0f;
88 zone.cond = (buf[1] >> 4) & 0xf;
89 if (buf[1] & 0x01)
90 zone.reset = 1;
91 if (buf[1] & 0x02)
92 zone.non_seq = 1;
93
94 start_lba = get_unaligned_be64(&buf[16]);
95 zone.start = logical_to_sectors(sdp, start_lba);
96 zone.capacity = logical_to_sectors(sdp, get_unaligned_be64(&buf[8]));
97 zone.len = zone.capacity;
98 if (sdkp->zone_starting_lba_gran) {
99 gran = logical_to_sectors(sdp, sdkp->zone_starting_lba_gran);
100 if (zone.len > gran) {
101 sd_printk(KERN_ERR, sdkp,
102 "Invalid zone at LBA %llu with capacity %llu and length %llu; granularity = %llu\n",
103 start_lba,
104 sectors_to_logical(sdp, zone.capacity),
105 sectors_to_logical(sdp, zone.len),
106 sectors_to_logical(sdp, gran));
107 return -EINVAL;
108 }
109 /*
110 * Use the starting LBA granularity instead of the zone length
111 * obtained from the REPORT ZONES command.
112 */
113 zone.len = gran;
114 }
115 if (zone.cond == ZBC_ZONE_COND_FULL)
116 zone.wp = zone.start + zone.len;
117 else
118 zone.wp = logical_to_sectors(sdp, get_unaligned_be64(&buf[24]));
119
120 ret = cb(&zone, idx, data);
121 if (ret)
122 return ret;
123
124 if (sdkp->rev_wp_offset)
125 sdkp->rev_wp_offset[idx] = sd_zbc_get_zone_wp_offset(&zone);
126
127 return 0;
128 }
129
130 /**
131 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
132 * @sdkp: The target disk
133 * @buf: vmalloc-ed buffer to use for the reply
134 * @buflen: the buffer size
135 * @lba: Start LBA of the report
136 * @partial: Do partial report
137 *
138 * For internal use during device validation.
139 * Using partial=true can significantly speed up execution of a report zones
140 * command because the disk does not have to count all possible report matching
141 * zones and will only report the count of zones fitting in the command reply
142 * buffer.
143 */
sd_zbc_do_report_zones(struct scsi_disk * sdkp,unsigned char * buf,unsigned int buflen,sector_t lba,bool partial)144 static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
145 unsigned int buflen, sector_t lba,
146 bool partial)
147 {
148 struct scsi_device *sdp = sdkp->device;
149 const int timeout = sdp->request_queue->rq_timeout;
150 struct scsi_sense_hdr sshdr;
151 const struct scsi_exec_args exec_args = {
152 .sshdr = &sshdr,
153 };
154 unsigned char cmd[16];
155 unsigned int rep_len;
156 int result;
157
158 memset(cmd, 0, 16);
159 cmd[0] = ZBC_IN;
160 cmd[1] = ZI_REPORT_ZONES;
161 put_unaligned_be64(lba, &cmd[2]);
162 put_unaligned_be32(buflen, &cmd[10]);
163 if (partial)
164 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
165
166 result = scsi_execute_cmd(sdp, cmd, REQ_OP_DRV_IN, buf, buflen,
167 timeout, SD_MAX_RETRIES, &exec_args);
168 if (result) {
169 sd_printk(KERN_ERR, sdkp,
170 "REPORT ZONES start lba %llu failed\n", lba);
171 sd_print_result(sdkp, "REPORT ZONES", result);
172 if (result > 0 && scsi_sense_valid(&sshdr))
173 sd_print_sense_hdr(sdkp, &sshdr);
174 return -EIO;
175 }
176
177 rep_len = get_unaligned_be32(&buf[0]);
178 if (rep_len < 64) {
179 sd_printk(KERN_ERR, sdkp,
180 "REPORT ZONES report invalid length %u\n",
181 rep_len);
182 return -EIO;
183 }
184
185 return 0;
186 }
187
188 /**
189 * sd_zbc_alloc_report_buffer() - Allocate a buffer for report zones reply.
190 * @sdkp: The target disk
191 * @nr_zones: Maximum number of zones to report
192 * @buflen: Size of the buffer allocated
193 *
194 * Try to allocate a reply buffer for the number of requested zones.
195 * The size of the buffer allocated may be smaller than requested to
196 * satify the device constraint (max_hw_sectors, max_segments, etc).
197 *
198 * Return the address of the allocated buffer and update @buflen with
199 * the size of the allocated buffer.
200 */
sd_zbc_alloc_report_buffer(struct scsi_disk * sdkp,unsigned int nr_zones,size_t * buflen)201 static void *sd_zbc_alloc_report_buffer(struct scsi_disk *sdkp,
202 unsigned int nr_zones, size_t *buflen)
203 {
204 struct request_queue *q = sdkp->disk->queue;
205 unsigned int max_segments;
206 size_t bufsize;
207 void *buf;
208
209 /*
210 * Report zone buffer size should be at most 64B times the number of
211 * zones requested plus the 64B reply header, but should be aligned
212 * to SECTOR_SIZE for ATA devices.
213 * Make sure that this size does not exceed the hardware capabilities.
214 * Furthermore, since the report zone command cannot be split, make
215 * sure that the allocated buffer can always be mapped by limiting the
216 * number of pages allocated to the HBA max segments limit.
217 * Since max segments can be larger than the max inline bio vectors,
218 * further limit the allocated buffer to BIO_MAX_INLINE_VECS.
219 */
220 nr_zones = min(nr_zones, sdkp->zone_info.nr_zones);
221 bufsize = roundup((nr_zones + 1) * 64, SECTOR_SIZE);
222 bufsize = min_t(size_t, bufsize,
223 queue_max_hw_sectors(q) << SECTOR_SHIFT);
224 max_segments = min(BIO_MAX_INLINE_VECS, queue_max_segments(q));
225 bufsize = min_t(size_t, bufsize, max_segments << PAGE_SHIFT);
226
227 while (bufsize >= SECTOR_SIZE) {
228 buf = kvzalloc(bufsize, GFP_KERNEL | __GFP_NORETRY);
229 if (buf) {
230 *buflen = bufsize;
231 return buf;
232 }
233 bufsize = rounddown(bufsize >> 1, SECTOR_SIZE);
234 }
235
236 return NULL;
237 }
238
239 /**
240 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
241 * @sdkp: The target disk
242 */
sd_zbc_zone_sectors(struct scsi_disk * sdkp)243 static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
244 {
245 return logical_to_sectors(sdkp->device, sdkp->zone_info.zone_blocks);
246 }
247
248 /**
249 * sd_zbc_report_zones - SCSI .report_zones() callback.
250 * @disk: Disk to report zones for.
251 * @sector: Start sector.
252 * @nr_zones: Maximum number of zones to report.
253 * @cb: Callback function called to report zone information.
254 * @data: Second argument passed to @cb.
255 *
256 * Called by the block layer to iterate over zone information. See also the
257 * disk->fops->report_zones() calls in block/blk-zoned.c.
258 */
sd_zbc_report_zones(struct gendisk * disk,sector_t sector,unsigned int nr_zones,report_zones_cb cb,void * data)259 int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
260 unsigned int nr_zones, report_zones_cb cb, void *data)
261 {
262 struct scsi_disk *sdkp = scsi_disk(disk);
263 sector_t lba = sectors_to_logical(sdkp->device, sector);
264 unsigned int nr, i;
265 unsigned char *buf;
266 u64 zone_length, start_lba;
267 size_t offset, buflen = 0;
268 int zone_idx = 0;
269 int ret;
270
271 if (!sd_is_zoned(sdkp))
272 /* Not a zoned device */
273 return -EOPNOTSUPP;
274
275 if (!sdkp->capacity)
276 /* Device gone or invalid */
277 return -ENODEV;
278
279 buf = sd_zbc_alloc_report_buffer(sdkp, nr_zones, &buflen);
280 if (!buf)
281 return -ENOMEM;
282
283 while (zone_idx < nr_zones && lba < sdkp->capacity) {
284 ret = sd_zbc_do_report_zones(sdkp, buf, buflen, lba, true);
285 if (ret)
286 goto out;
287
288 offset = 0;
289 nr = min(nr_zones, get_unaligned_be32(&buf[0]) / 64);
290 if (!nr)
291 break;
292
293 for (i = 0; i < nr && zone_idx < nr_zones; i++) {
294 offset += 64;
295 start_lba = get_unaligned_be64(&buf[offset + 16]);
296 zone_length = get_unaligned_be64(&buf[offset + 8]);
297 if ((zone_idx == 0 &&
298 (lba < start_lba ||
299 lba >= start_lba + zone_length)) ||
300 (zone_idx > 0 && start_lba != lba) ||
301 start_lba + zone_length < start_lba) {
302 sd_printk(KERN_ERR, sdkp,
303 "Zone %d at LBA %llu is invalid: %llu + %llu\n",
304 zone_idx, lba, start_lba, zone_length);
305 ret = -EINVAL;
306 goto out;
307 }
308 lba = start_lba + zone_length;
309 if (sd_zbc_is_gap_zone(&buf[offset])) {
310 if (sdkp->zone_starting_lba_gran)
311 continue;
312 sd_printk(KERN_ERR, sdkp,
313 "Gap zone without constant LBA offsets\n");
314 ret = -EINVAL;
315 goto out;
316 }
317
318 ret = sd_zbc_parse_report(sdkp, buf + offset, zone_idx,
319 cb, data);
320 if (ret)
321 goto out;
322
323 zone_idx++;
324 }
325 }
326
327 ret = zone_idx;
328 out:
329 kvfree(buf);
330 return ret;
331 }
332
sd_zbc_cmnd_checks(struct scsi_cmnd * cmd)333 static blk_status_t sd_zbc_cmnd_checks(struct scsi_cmnd *cmd)
334 {
335 struct request *rq = scsi_cmd_to_rq(cmd);
336 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
337 sector_t sector = blk_rq_pos(rq);
338
339 if (!sd_is_zoned(sdkp))
340 /* Not a zoned device */
341 return BLK_STS_IOERR;
342
343 if (sdkp->device->changed)
344 return BLK_STS_IOERR;
345
346 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
347 /* Unaligned request */
348 return BLK_STS_IOERR;
349
350 return BLK_STS_OK;
351 }
352
353 #define SD_ZBC_INVALID_WP_OFST (~0u)
354 #define SD_ZBC_UPDATING_WP_OFST (SD_ZBC_INVALID_WP_OFST - 1)
355
sd_zbc_update_wp_offset_cb(struct blk_zone * zone,unsigned int idx,void * data)356 static int sd_zbc_update_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
357 void *data)
358 {
359 struct scsi_disk *sdkp = data;
360
361 lockdep_assert_held(&sdkp->zones_wp_offset_lock);
362
363 sdkp->zones_wp_offset[idx] = sd_zbc_get_zone_wp_offset(zone);
364
365 return 0;
366 }
367
368 /*
369 * An attempt to append a zone triggered an invalid write pointer error.
370 * Reread the write pointer of the zone(s) in which the append failed.
371 */
sd_zbc_update_wp_offset_workfn(struct work_struct * work)372 static void sd_zbc_update_wp_offset_workfn(struct work_struct *work)
373 {
374 struct scsi_disk *sdkp;
375 unsigned long flags;
376 sector_t zno;
377 int ret;
378
379 sdkp = container_of(work, struct scsi_disk, zone_wp_offset_work);
380
381 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
382 for (zno = 0; zno < sdkp->zone_info.nr_zones; zno++) {
383 if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
384 continue;
385
386 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
387 ret = sd_zbc_do_report_zones(sdkp, sdkp->zone_wp_update_buf,
388 SD_BUF_SIZE,
389 zno * sdkp->zone_info.zone_blocks, true);
390 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
391 if (!ret)
392 sd_zbc_parse_report(sdkp, sdkp->zone_wp_update_buf + 64,
393 zno, sd_zbc_update_wp_offset_cb,
394 sdkp);
395 }
396 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
397
398 scsi_device_put(sdkp->device);
399 }
400
401 /**
402 * sd_zbc_prepare_zone_append() - Prepare an emulated ZONE_APPEND command.
403 * @cmd: the command to setup
404 * @lba: the LBA to patch
405 * @nr_blocks: the number of LBAs to be written
406 *
407 * Called from sd_setup_read_write_cmnd() for REQ_OP_ZONE_APPEND.
408 * @sd_zbc_prepare_zone_append() handles the necessary zone wrote locking and
409 * patching of the lba for an emulated ZONE_APPEND command.
410 *
411 * In case the cached write pointer offset is %SD_ZBC_INVALID_WP_OFST it will
412 * schedule a REPORT ZONES command and return BLK_STS_IOERR.
413 */
sd_zbc_prepare_zone_append(struct scsi_cmnd * cmd,sector_t * lba,unsigned int nr_blocks)414 blk_status_t sd_zbc_prepare_zone_append(struct scsi_cmnd *cmd, sector_t *lba,
415 unsigned int nr_blocks)
416 {
417 struct request *rq = scsi_cmd_to_rq(cmd);
418 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
419 unsigned int wp_offset, zno = blk_rq_zone_no(rq);
420 unsigned long flags;
421 blk_status_t ret;
422
423 ret = sd_zbc_cmnd_checks(cmd);
424 if (ret != BLK_STS_OK)
425 return ret;
426
427 if (!blk_rq_zone_is_seq(rq))
428 return BLK_STS_IOERR;
429
430 /* Unlock of the write lock will happen in sd_zbc_complete() */
431 if (!blk_req_zone_write_trylock(rq))
432 return BLK_STS_ZONE_RESOURCE;
433
434 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
435 wp_offset = sdkp->zones_wp_offset[zno];
436 switch (wp_offset) {
437 case SD_ZBC_INVALID_WP_OFST:
438 /*
439 * We are about to schedule work to update a zone write pointer
440 * offset, which will cause the zone append command to be
441 * requeued. So make sure that the scsi device does not go away
442 * while the work is being processed.
443 */
444 if (scsi_device_get(sdkp->device)) {
445 ret = BLK_STS_IOERR;
446 break;
447 }
448 sdkp->zones_wp_offset[zno] = SD_ZBC_UPDATING_WP_OFST;
449 schedule_work(&sdkp->zone_wp_offset_work);
450 fallthrough;
451 case SD_ZBC_UPDATING_WP_OFST:
452 ret = BLK_STS_DEV_RESOURCE;
453 break;
454 default:
455 wp_offset = sectors_to_logical(sdkp->device, wp_offset);
456 if (wp_offset + nr_blocks > sdkp->zone_info.zone_blocks) {
457 ret = BLK_STS_IOERR;
458 break;
459 }
460
461 trace_scsi_prepare_zone_append(cmd, *lba, wp_offset);
462 *lba += wp_offset;
463 }
464 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
465 if (ret)
466 blk_req_zone_write_unlock(rq);
467 return ret;
468 }
469
470 /**
471 * sd_zbc_setup_zone_mgmt_cmnd - Prepare a zone ZBC_OUT command. The operations
472 * can be RESET WRITE POINTER, OPEN, CLOSE or FINISH.
473 * @cmd: the command to setup
474 * @op: Operation to be performed
475 * @all: All zones control
476 *
477 * Called from sd_init_command() for REQ_OP_ZONE_RESET, REQ_OP_ZONE_RESET_ALL,
478 * REQ_OP_ZONE_OPEN, REQ_OP_ZONE_CLOSE or REQ_OP_ZONE_FINISH requests.
479 */
sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd * cmd,unsigned char op,bool all)480 blk_status_t sd_zbc_setup_zone_mgmt_cmnd(struct scsi_cmnd *cmd,
481 unsigned char op, bool all)
482 {
483 struct request *rq = scsi_cmd_to_rq(cmd);
484 sector_t sector = blk_rq_pos(rq);
485 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
486 sector_t block = sectors_to_logical(sdkp->device, sector);
487 blk_status_t ret;
488
489 ret = sd_zbc_cmnd_checks(cmd);
490 if (ret != BLK_STS_OK)
491 return ret;
492
493 cmd->cmd_len = 16;
494 memset(cmd->cmnd, 0, cmd->cmd_len);
495 cmd->cmnd[0] = ZBC_OUT;
496 cmd->cmnd[1] = op;
497 if (all)
498 cmd->cmnd[14] = 0x1;
499 else
500 put_unaligned_be64(block, &cmd->cmnd[2]);
501
502 rq->timeout = SD_TIMEOUT;
503 cmd->sc_data_direction = DMA_NONE;
504 cmd->transfersize = 0;
505 cmd->allowed = 0;
506
507 return BLK_STS_OK;
508 }
509
sd_zbc_need_zone_wp_update(struct request * rq)510 static bool sd_zbc_need_zone_wp_update(struct request *rq)
511 {
512 switch (req_op(rq)) {
513 case REQ_OP_ZONE_APPEND:
514 case REQ_OP_ZONE_FINISH:
515 case REQ_OP_ZONE_RESET:
516 case REQ_OP_ZONE_RESET_ALL:
517 return true;
518 case REQ_OP_WRITE:
519 case REQ_OP_WRITE_ZEROES:
520 return blk_rq_zone_is_seq(rq);
521 default:
522 return false;
523 }
524 }
525
526 /**
527 * sd_zbc_zone_wp_update - Update cached zone write pointer upon cmd completion
528 * @cmd: Completed command
529 * @good_bytes: Command reply bytes
530 *
531 * Called from sd_zbc_complete() to handle the update of the cached zone write
532 * pointer value in case an update is needed.
533 */
sd_zbc_zone_wp_update(struct scsi_cmnd * cmd,unsigned int good_bytes)534 static unsigned int sd_zbc_zone_wp_update(struct scsi_cmnd *cmd,
535 unsigned int good_bytes)
536 {
537 int result = cmd->result;
538 struct request *rq = scsi_cmd_to_rq(cmd);
539 struct scsi_disk *sdkp = scsi_disk(rq->q->disk);
540 unsigned int zno = blk_rq_zone_no(rq);
541 enum req_op op = req_op(rq);
542 unsigned long flags;
543
544 /*
545 * If we got an error for a command that needs updating the write
546 * pointer offset cache, we must mark the zone wp offset entry as
547 * invalid to force an update from disk the next time a zone append
548 * command is issued.
549 */
550 spin_lock_irqsave(&sdkp->zones_wp_offset_lock, flags);
551
552 if (result && op != REQ_OP_ZONE_RESET_ALL) {
553 if (op == REQ_OP_ZONE_APPEND) {
554 /* Force complete completion (no retry) */
555 good_bytes = 0;
556 scsi_set_resid(cmd, blk_rq_bytes(rq));
557 }
558
559 /*
560 * Force an update of the zone write pointer offset on
561 * the next zone append access.
562 */
563 if (sdkp->zones_wp_offset[zno] != SD_ZBC_UPDATING_WP_OFST)
564 sdkp->zones_wp_offset[zno] = SD_ZBC_INVALID_WP_OFST;
565 goto unlock_wp_offset;
566 }
567
568 switch (op) {
569 case REQ_OP_ZONE_APPEND:
570 trace_scsi_zone_wp_update(cmd, rq->__sector,
571 sdkp->zones_wp_offset[zno], good_bytes);
572 rq->__sector += sdkp->zones_wp_offset[zno];
573 fallthrough;
574 case REQ_OP_WRITE_ZEROES:
575 case REQ_OP_WRITE:
576 if (sdkp->zones_wp_offset[zno] < sd_zbc_zone_sectors(sdkp))
577 sdkp->zones_wp_offset[zno] +=
578 good_bytes >> SECTOR_SHIFT;
579 break;
580 case REQ_OP_ZONE_RESET:
581 sdkp->zones_wp_offset[zno] = 0;
582 break;
583 case REQ_OP_ZONE_FINISH:
584 sdkp->zones_wp_offset[zno] = sd_zbc_zone_sectors(sdkp);
585 break;
586 case REQ_OP_ZONE_RESET_ALL:
587 memset(sdkp->zones_wp_offset, 0,
588 sdkp->zone_info.nr_zones * sizeof(unsigned int));
589 break;
590 default:
591 break;
592 }
593
594 unlock_wp_offset:
595 spin_unlock_irqrestore(&sdkp->zones_wp_offset_lock, flags);
596
597 return good_bytes;
598 }
599
600 /**
601 * sd_zbc_complete - ZBC command post processing.
602 * @cmd: Completed command
603 * @good_bytes: Command reply bytes
604 * @sshdr: command sense header
605 *
606 * Called from sd_done() to handle zone commands errors and updates to the
607 * device queue zone write pointer offset cahce.
608 */
sd_zbc_complete(struct scsi_cmnd * cmd,unsigned int good_bytes,struct scsi_sense_hdr * sshdr)609 unsigned int sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
610 struct scsi_sense_hdr *sshdr)
611 {
612 int result = cmd->result;
613 struct request *rq = scsi_cmd_to_rq(cmd);
614
615 if (op_is_zone_mgmt(req_op(rq)) &&
616 result &&
617 sshdr->sense_key == ILLEGAL_REQUEST &&
618 sshdr->asc == 0x24) {
619 /*
620 * INVALID FIELD IN CDB error: a zone management command was
621 * attempted on a conventional zone. Nothing to worry about,
622 * so be quiet about the error.
623 */
624 rq->rq_flags |= RQF_QUIET;
625 } else if (sd_zbc_need_zone_wp_update(rq))
626 good_bytes = sd_zbc_zone_wp_update(cmd, good_bytes);
627
628 if (req_op(rq) == REQ_OP_ZONE_APPEND)
629 blk_req_zone_write_unlock(rq);
630
631 return good_bytes;
632 }
633
634 /**
635 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
636 * @sdkp: Target disk
637 * @buf: Buffer where to store the VPD page data
638 *
639 * Read VPD page B6, get information and check that reads are unconstrained.
640 */
sd_zbc_check_zoned_characteristics(struct scsi_disk * sdkp,unsigned char * buf)641 static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
642 unsigned char *buf)
643 {
644 u64 zone_starting_lba_gran;
645
646 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
647 sd_printk(KERN_NOTICE, sdkp,
648 "Read zoned characteristics VPD page failed\n");
649 return -ENODEV;
650 }
651
652 if (sdkp->device->type != TYPE_ZBC) {
653 /* Host-aware */
654 sdkp->urswrz = 1;
655 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
656 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
657 sdkp->zones_max_open = 0;
658 return 0;
659 }
660
661 /* Host-managed */
662 sdkp->urswrz = buf[4] & 1;
663 sdkp->zones_optimal_open = 0;
664 sdkp->zones_optimal_nonseq = 0;
665 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
666 /* Check zone alignment method */
667 switch (buf[23] & 0xf) {
668 case 0:
669 case ZBC_CONSTANT_ZONE_LENGTH:
670 /* Use zone length */
671 break;
672 case ZBC_CONSTANT_ZONE_START_OFFSET:
673 zone_starting_lba_gran = get_unaligned_be64(&buf[24]);
674 if (zone_starting_lba_gran == 0 ||
675 !is_power_of_2(zone_starting_lba_gran) ||
676 logical_to_sectors(sdkp->device, zone_starting_lba_gran) >
677 UINT_MAX) {
678 sd_printk(KERN_ERR, sdkp,
679 "Invalid zone starting LBA granularity %llu\n",
680 zone_starting_lba_gran);
681 return -ENODEV;
682 }
683 sdkp->zone_starting_lba_gran = zone_starting_lba_gran;
684 break;
685 default:
686 sd_printk(KERN_ERR, sdkp, "Invalid zone alignment method\n");
687 return -ENODEV;
688 }
689
690 /*
691 * Check for unconstrained reads: host-managed devices with
692 * constrained reads (drives failing read after write pointer)
693 * are not supported.
694 */
695 if (!sdkp->urswrz) {
696 if (sdkp->first_scan)
697 sd_printk(KERN_NOTICE, sdkp,
698 "constrained reads devices are not supported\n");
699 return -ENODEV;
700 }
701
702 return 0;
703 }
704
705 /**
706 * sd_zbc_check_capacity - Check the device capacity
707 * @sdkp: Target disk
708 * @buf: command buffer
709 * @zblocks: zone size in logical blocks
710 *
711 * Get the device zone size and check that the device capacity as reported
712 * by READ CAPACITY matches the max_lba value (plus one) of the report zones
713 * command reply for devices with RC_BASIS == 0.
714 *
715 * Returns 0 upon success or an error code upon failure.
716 */
sd_zbc_check_capacity(struct scsi_disk * sdkp,unsigned char * buf,u32 * zblocks)717 static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf,
718 u32 *zblocks)
719 {
720 u64 zone_blocks;
721 sector_t max_lba;
722 unsigned char *rec;
723 int ret;
724
725 /* Do a report zone to get max_lba and the size of the first zone */
726 ret = sd_zbc_do_report_zones(sdkp, buf, SD_BUF_SIZE, 0, false);
727 if (ret)
728 return ret;
729
730 if (sdkp->rc_basis == 0) {
731 /* The max_lba field is the capacity of this device */
732 max_lba = get_unaligned_be64(&buf[8]);
733 if (sdkp->capacity != max_lba + 1) {
734 if (sdkp->first_scan)
735 sd_printk(KERN_WARNING, sdkp,
736 "Changing capacity from %llu to max LBA+1 %llu\n",
737 (unsigned long long)sdkp->capacity,
738 (unsigned long long)max_lba + 1);
739 sdkp->capacity = max_lba + 1;
740 }
741 }
742
743 if (sdkp->zone_starting_lba_gran == 0) {
744 /* Get the size of the first reported zone */
745 rec = buf + 64;
746 zone_blocks = get_unaligned_be64(&rec[8]);
747 if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
748 if (sdkp->first_scan)
749 sd_printk(KERN_NOTICE, sdkp,
750 "Zone size too large\n");
751 return -EFBIG;
752 }
753 } else {
754 zone_blocks = sdkp->zone_starting_lba_gran;
755 }
756
757 if (!is_power_of_2(zone_blocks)) {
758 sd_printk(KERN_ERR, sdkp,
759 "Zone size %llu is not a power of two.\n",
760 zone_blocks);
761 return -EINVAL;
762 }
763
764 *zblocks = zone_blocks;
765
766 return 0;
767 }
768
sd_zbc_print_zones(struct scsi_disk * sdkp)769 static void sd_zbc_print_zones(struct scsi_disk *sdkp)
770 {
771 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
772 return;
773
774 if (sdkp->capacity & (sdkp->zone_info.zone_blocks - 1))
775 sd_printk(KERN_NOTICE, sdkp,
776 "%u zones of %u logical blocks + 1 runt zone\n",
777 sdkp->zone_info.nr_zones - 1,
778 sdkp->zone_info.zone_blocks);
779 else
780 sd_printk(KERN_NOTICE, sdkp,
781 "%u zones of %u logical blocks\n",
782 sdkp->zone_info.nr_zones,
783 sdkp->zone_info.zone_blocks);
784 }
785
sd_zbc_init_disk(struct scsi_disk * sdkp)786 static int sd_zbc_init_disk(struct scsi_disk *sdkp)
787 {
788 sdkp->zones_wp_offset = NULL;
789 spin_lock_init(&sdkp->zones_wp_offset_lock);
790 sdkp->rev_wp_offset = NULL;
791 mutex_init(&sdkp->rev_mutex);
792 INIT_WORK(&sdkp->zone_wp_offset_work, sd_zbc_update_wp_offset_workfn);
793 sdkp->zone_wp_update_buf = kzalloc(SD_BUF_SIZE, GFP_KERNEL);
794 if (!sdkp->zone_wp_update_buf)
795 return -ENOMEM;
796
797 return 0;
798 }
799
sd_zbc_free_zone_info(struct scsi_disk * sdkp)800 void sd_zbc_free_zone_info(struct scsi_disk *sdkp)
801 {
802 if (!sdkp->zone_wp_update_buf)
803 return;
804
805 /* Serialize against revalidate zones */
806 mutex_lock(&sdkp->rev_mutex);
807
808 kvfree(sdkp->zones_wp_offset);
809 sdkp->zones_wp_offset = NULL;
810 kfree(sdkp->zone_wp_update_buf);
811 sdkp->zone_wp_update_buf = NULL;
812
813 sdkp->early_zone_info = (struct zoned_disk_info){ };
814 sdkp->zone_info = (struct zoned_disk_info){ };
815
816 mutex_unlock(&sdkp->rev_mutex);
817 }
818
sd_zbc_revalidate_zones_cb(struct gendisk * disk)819 static void sd_zbc_revalidate_zones_cb(struct gendisk *disk)
820 {
821 struct scsi_disk *sdkp = scsi_disk(disk);
822
823 swap(sdkp->zones_wp_offset, sdkp->rev_wp_offset);
824 }
825
826 /*
827 * Call blk_revalidate_disk_zones() if any of the zoned disk properties have
828 * changed that make it necessary to call that function. Called by
829 * sd_revalidate_disk() after the gendisk capacity has been set.
830 */
sd_zbc_revalidate_zones(struct scsi_disk * sdkp)831 int sd_zbc_revalidate_zones(struct scsi_disk *sdkp)
832 {
833 struct gendisk *disk = sdkp->disk;
834 struct request_queue *q = disk->queue;
835 u32 zone_blocks = sdkp->early_zone_info.zone_blocks;
836 unsigned int nr_zones = sdkp->early_zone_info.nr_zones;
837 int ret = 0;
838 unsigned int flags;
839
840 /*
841 * For all zoned disks, initialize zone append emulation data if not
842 * already done. This is necessary also for host-aware disks used as
843 * regular disks due to the presence of partitions as these partitions
844 * may be deleted and the disk zoned model changed back from
845 * BLK_ZONED_NONE to BLK_ZONED_HA.
846 */
847 if (sd_is_zoned(sdkp) && !sdkp->zone_wp_update_buf) {
848 ret = sd_zbc_init_disk(sdkp);
849 if (ret)
850 return ret;
851 }
852
853 /*
854 * There is nothing to do for regular disks, including host-aware disks
855 * that have partitions.
856 */
857 if (!blk_queue_is_zoned(q))
858 return 0;
859
860 /*
861 * Make sure revalidate zones are serialized to ensure exclusive
862 * updates of the scsi disk data.
863 */
864 mutex_lock(&sdkp->rev_mutex);
865
866 if (sdkp->zone_info.zone_blocks == zone_blocks &&
867 sdkp->zone_info.nr_zones == nr_zones &&
868 disk->nr_zones == nr_zones)
869 goto unlock;
870
871 flags = memalloc_noio_save();
872 sdkp->zone_info.zone_blocks = zone_blocks;
873 sdkp->zone_info.nr_zones = nr_zones;
874 sdkp->rev_wp_offset = kvcalloc(nr_zones, sizeof(u32), GFP_KERNEL);
875 if (!sdkp->rev_wp_offset) {
876 ret = -ENOMEM;
877 memalloc_noio_restore(flags);
878 goto unlock;
879 }
880
881 blk_queue_chunk_sectors(q,
882 logical_to_sectors(sdkp->device, zone_blocks));
883 blk_queue_max_zone_append_sectors(q,
884 q->limits.max_segments << PAGE_SECTORS_SHIFT);
885
886 ret = blk_revalidate_disk_zones(disk, sd_zbc_revalidate_zones_cb);
887
888 memalloc_noio_restore(flags);
889 kvfree(sdkp->rev_wp_offset);
890 sdkp->rev_wp_offset = NULL;
891
892 if (ret) {
893 sdkp->zone_info = (struct zoned_disk_info){ };
894 sdkp->capacity = 0;
895 goto unlock;
896 }
897
898 sd_zbc_print_zones(sdkp);
899
900 unlock:
901 mutex_unlock(&sdkp->rev_mutex);
902
903 return ret;
904 }
905
906 /**
907 * sd_zbc_read_zones - Read zone information and update the request queue
908 * @sdkp: SCSI disk pointer.
909 * @buf: 512 byte buffer used for storing SCSI command output.
910 *
911 * Read zone information and update the request queue zone characteristics and
912 * also the zoned device information in *sdkp. Called by sd_revalidate_disk()
913 * before the gendisk capacity has been set.
914 */
sd_zbc_read_zones(struct scsi_disk * sdkp,u8 buf[SD_BUF_SIZE])915 int sd_zbc_read_zones(struct scsi_disk *sdkp, u8 buf[SD_BUF_SIZE])
916 {
917 struct gendisk *disk = sdkp->disk;
918 struct request_queue *q = disk->queue;
919 unsigned int nr_zones;
920 u32 zone_blocks = 0;
921 int ret;
922
923 if (!sd_is_zoned(sdkp)) {
924 /*
925 * Device managed or normal SCSI disk, no special handling
926 * required. Nevertheless, free the disk zone information in
927 * case the device type changed.
928 */
929 sd_zbc_free_zone_info(sdkp);
930 return 0;
931 }
932
933 /* READ16/WRITE16/SYNC16 is mandatory for ZBC devices */
934 sdkp->device->use_16_for_rw = 1;
935 sdkp->device->use_10_for_rw = 0;
936 sdkp->device->use_16_for_sync = 1;
937
938 if (!blk_queue_is_zoned(q)) {
939 /*
940 * This can happen for a host aware disk with partitions.
941 * The block device zone model was already cleared by
942 * disk_set_zoned(). Only free the scsi disk zone
943 * information and exit early.
944 */
945 sd_zbc_free_zone_info(sdkp);
946 return 0;
947 }
948
949 /* Check zoned block device characteristics (unconstrained reads) */
950 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
951 if (ret)
952 goto err;
953
954 /* Check the device capacity reported by report zones */
955 ret = sd_zbc_check_capacity(sdkp, buf, &zone_blocks);
956 if (ret != 0)
957 goto err;
958
959 /* The drive satisfies the kernel restrictions: set it up */
960 blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, q);
961 blk_queue_required_elevator_features(q, ELEVATOR_F_ZBD_SEQ_WRITE);
962 if (sdkp->zones_max_open == U32_MAX)
963 disk_set_max_open_zones(disk, 0);
964 else
965 disk_set_max_open_zones(disk, sdkp->zones_max_open);
966 disk_set_max_active_zones(disk, 0);
967 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
968
969 sdkp->early_zone_info.nr_zones = nr_zones;
970 sdkp->early_zone_info.zone_blocks = zone_blocks;
971
972 return 0;
973
974 err:
975 sdkp->capacity = 0;
976
977 return ret;
978 }
979