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