blob: a14fef11776ec846c482178ee555c8122a598d22 [file] [log] [blame]
Hannes Reinecke89d94752016-10-18 15:40:34 +09001/*
2 * SCSI Zoned Block commands
3 *
4 * Copyright (C) 2014-2015 SUSE Linux GmbH
5 * Written by: Hannes Reinecke <hare@suse.de>
6 * Modified by: Damien Le Moal <damien.lemoal@hgst.com>
7 * Modified by: Shaun Tancheff <shaun.tancheff@seagate.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
21 * USA.
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>
Hannes Reinecke89d94752016-10-18 15:40:34 +090031
32#include "sd.h"
Hannes Reinecke89d94752016-10-18 15:40:34 +090033
34/**
Damien Le Moale98f42b2017-10-11 05:54:22 +090035 * sd_zbc_parse_report - Convert a zone descriptor to a struct blk_zone,
36 * @sdkp: The disk the report originated from
37 * @buf: Address of the report zone descriptor
38 * @zone: the destination zone structure
39 *
40 * All LBA sized values are converted to 512B sectors unit.
Hannes Reinecke89d94752016-10-18 15:40:34 +090041 */
Damien Le Moale98f42b2017-10-11 05:54:22 +090042static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
Hannes Reinecke89d94752016-10-18 15:40:34 +090043 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/**
Damien Le Moale98f42b2017-10-11 05:54:22 +090065 * sd_zbc_report_zones - Issue a REPORT ZONES scsi command.
66 * @sdkp: The target disk
67 * @buf: Buffer to use for the reply
68 * @buflen: the buffer size
69 * @lba: Start LBA of the report
70 *
71 * For internal use during device validation.
Hannes Reinecke89d94752016-10-18 15:40:34 +090072 */
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
Damien Le Moale98f42b2017-10-11 05:54:22 +0900112/**
113 * sd_zbc_setup_report_cmnd - Prepare a REPORT ZONES scsi command
114 * @cmd: The command to setup
115 *
116 * Call in sd_init_command() for a REQ_OP_ZONE_REPORT request.
117 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900118int 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 /* Not a zoned device */
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 /* Do partial report for speeding things up */
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 * Report may return less bytes than requested. Make sure
153 * to report completion on the entire initial request.
154 */
155 rq->__data_len = nr_bytes;
156
157 return BLKPREP_OK;
158}
159
Damien Le Moale98f42b2017-10-11 05:54:22 +0900160/**
161 * sd_zbc_report_zones_complete - Process a REPORT ZONES scsi command reply.
162 * @scmd: The completed report zones command
163 * @good_bytes: reply size in bytes
164 *
165 * Convert all reported zone descriptors to struct blk_zone. The conversion
166 * is done in-place, directly in the request specified sg buffer.
167 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900168static 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 /* Set the report header */
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 /* Parse zone descriptors */
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
Damien Le Moale98f42b2017-10-11 05:54:22 +0900223/**
224 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
225 * @sdkp: The target disk
226 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900227static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
228{
229 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
230}
231
Damien Le Moale98f42b2017-10-11 05:54:22 +0900232/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900233 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
234 * @cmd: the command to setup
235 *
236 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
237 */
Hannes Reinecke89d94752016-10-18 15:40:34 +0900238int 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);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900244
245 if (!sd_is_zoned(sdkp))
246 /* Not a zoned device */
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 /* Unaligned request */
254 return BLKPREP_KILL;
255
Hannes Reinecke89d94752016-10-18 15:40:34 +0900256 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
Damien Le Moale98f42b2017-10-11 05:54:22 +0900270/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900271 * sd_zbc_complete - ZBC command post processing.
272 * @cmd: Completed command
273 * @good_bytes: Command reply bytes
274 * @sshdr: command sense header
275 *
276 * Called from sd_done(). Process report zones reply and handle reset zone
277 * and write commands errors.
278 */
279void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
Hannes Reinecke89d94752016-10-18 15:40:34 +0900280 struct scsi_sense_hdr *sshdr)
281{
282 int result = cmd->result;
283 struct request *rq = cmd->request;
284
285 switch (req_op(rq)) {
Damien Le Moal868ed5a2017-04-24 16:51:15 +0900286 case REQ_OP_ZONE_RESET:
287
288 if (result &&
289 sshdr->sense_key == ILLEGAL_REQUEST &&
290 sshdr->asc == 0x24)
291 /*
292 * INVALID FIELD IN CDB error: reset of a conventional
293 * zone was attempted. Nothing to worry about, so be
294 * quiet about the error.
295 */
296 rq->rq_flags |= RQF_QUIET;
297 break;
298
Hannes Reinecke89d94752016-10-18 15:40:34 +0900299 case REQ_OP_WRITE:
Christoph Hellwig02d26102017-04-05 19:21:02 +0200300 case REQ_OP_WRITE_ZEROES:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900301 case REQ_OP_WRITE_SAME:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900302 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/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900314 * sd_zbc_read_zoned_characteristics - Read zoned block device characteristics
315 * @sdkp: Target disk
316 * @buf: Buffer where to store the VPD page data
317 *
318 * Read VPD page B6.
Hannes Reinecke89d94752016-10-18 15:40:34 +0900319 */
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 /* Host-aware */
332 sdkp->urswrz = 1;
Damien Le Moal4a109032017-10-11 05:54:25 +0900333 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
334 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900335 sdkp->zones_max_open = 0;
336 } else {
337 /* Host-managed */
338 sdkp->urswrz = buf[4] & 1;
339 sdkp->zones_optimal_open = 0;
340 sdkp->zones_optimal_nonseq = 0;
Damien Le Moal4a109032017-10-11 05:54:25 +0900341 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900342 }
343
344 return 0;
345}
346
347/**
Damien Le Moale98f42b2017-10-11 05:54:22 +0900348 * sd_zbc_check_capacity - Check reported capacity.
349 * @sdkp: Target disk
350 * @buf: Buffer to use for commands
351 *
352 * ZBC drive may report only the capacity of the first conventional zones at
353 * LBA 0. This is indicated by the RC_BASIS field of the read capacity reply.
354 * Check this here. If the disk reported only its conventional zones capacity,
355 * get the total capacity by doing a report zones.
Hannes Reinecke89d94752016-10-18 15:40:34 +0900356 */
Damien Le Moale98f42b2017-10-11 05:54:22 +0900357static int sd_zbc_check_capacity(struct scsi_disk *sdkp, unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900358{
359 sector_t lba;
360 int ret;
361
362 if (sdkp->rc_basis != 0)
363 return 0;
364
365 /* Do a report zone to get the maximum LBA to check capacity */
366 ret = sd_zbc_report_zones(sdkp, buf, SD_BUF_SIZE, 0);
367 if (ret)
368 return ret;
369
370 /* The max_lba field is the capacity of this device */
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
Damien Le Moale8c77ec2017-10-11 05:54:24 +0900385#define SD_ZBC_BUF_SIZE 131072U
Hannes Reinecke89d94752016-10-18 15:40:34 +0900386
Damien Le Moale98f42b2017-10-11 05:54:22 +0900387/**
388 * sd_zbc_check_zone_size - Check the device zone sizes
389 * @sdkp: Target disk
390 *
391 * Check that all zones of the device are equal. The last zone can however
392 * be smaller. The zone size must also be a power of two number of LBAs.
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700393 *
394 * Returns the zone size in bytes upon success or an error code upon failure.
Damien Le Moale98f42b2017-10-11 05:54:22 +0900395 */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700396static s64 sd_zbc_check_zone_size(struct scsi_disk *sdkp)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900397{
Damien Le Moal4b433922018-03-02 07:19:28 +0900398 u64 zone_blocks = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900399 sector_t block = 0;
400 unsigned char *buf;
401 unsigned char *rec;
402 unsigned int buf_len;
403 unsigned int list_length;
404 int ret;
405 u8 same;
406
Hannes Reinecke89d94752016-10-18 15:40:34 +0900407 /* Get a buffer */
408 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
409 if (!buf)
410 return -ENOMEM;
411
412 /* Do a report zone to get the same field */
413 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0);
Damien Le Moal4b433922018-03-02 07:19:28 +0900414 if (ret)
415 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900416
417 same = buf[4] & 0x0f;
418 if (same > 0) {
419 rec = &buf[64];
420 zone_blocks = get_unaligned_be64(&rec[8]);
421 goto out;
422 }
423
424 /*
425 * Check the size of all zones: all zones must be of
426 * equal size, except the last zone which can be smaller
427 * than other zones.
428 */
429 do {
430
431 /* Parse REPORT ZONES header */
432 list_length = get_unaligned_be32(&buf[0]) + 64;
433 rec = buf + 64;
Damien Le Moale8c77ec2017-10-11 05:54:24 +0900434 buf_len = min(list_length, SD_ZBC_BUF_SIZE);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900435
436 /* Parse zone descriptors */
437 while (rec < buf + buf_len) {
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700438 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
439
440 if (zone_blocks == 0) {
441 zone_blocks = this_zone_blocks;
442 } else if (this_zone_blocks != zone_blocks &&
443 (block + this_zone_blocks < sdkp->capacity
444 || this_zone_blocks > zone_blocks)) {
Damien Le Moal3aadbe22018-05-31 17:42:40 +0900445 zone_blocks = 0;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900446 goto out;
447 }
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700448 block += this_zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900449 rec += 64;
450 }
451
452 if (block < sdkp->capacity) {
453 ret = sd_zbc_report_zones(sdkp, buf,
454 SD_ZBC_BUF_SIZE, block);
455 if (ret)
Damien Le Moal4b433922018-03-02 07:19:28 +0900456 goto out_free;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900457 }
458
459 } while (block < sdkp->capacity);
460
Hannes Reinecke89d94752016-10-18 15:40:34 +0900461out:
Hannes Reinecke89d94752016-10-18 15:40:34 +0900462 if (!zone_blocks) {
463 if (sdkp->first_scan)
464 sd_printk(KERN_NOTICE, sdkp,
465 "Devices with non constant zone "
466 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900467 ret = -ENODEV;
468 } else if (!is_power_of_2(zone_blocks)) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900469 if (sdkp->first_scan)
470 sd_printk(KERN_NOTICE, sdkp,
471 "Devices with non power of 2 zone "
472 "size are not supported\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900473 ret = -ENODEV;
474 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
Hannes Reinecke89d94752016-10-18 15:40:34 +0900475 if (sdkp->first_scan)
476 sd_printk(KERN_NOTICE, sdkp,
477 "Zone size too large\n");
Damien Le Moal4b433922018-03-02 07:19:28 +0900478 ret = -ENODEV;
479 } else {
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700480 ret = zone_blocks;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900481 }
482
Damien Le Moal4b433922018-03-02 07:19:28 +0900483out_free:
484 kfree(buf);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900485
Damien Le Moal4b433922018-03-02 07:19:28 +0900486 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900487}
488
Damien Le Moal23349ca2017-12-21 15:43:43 +0900489/**
490 * sd_zbc_alloc_zone_bitmap - Allocate a zone bitmap (one bit per zone).
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700491 * @nr_zones: Number of zones to allocate space for.
492 * @numa_node: NUMA node to allocate the memory from.
Damien Le Moal23349ca2017-12-21 15:43:43 +0900493 */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700494static inline unsigned long *
495sd_zbc_alloc_zone_bitmap(u32 nr_zones, int numa_node)
Damien Le Moal23349ca2017-12-21 15:43:43 +0900496{
Kees Cook590b5b72018-06-12 14:04:20 -0700497 return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700498 GFP_KERNEL, numa_node);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900499}
500
501/**
502 * sd_zbc_get_seq_zones - Parse report zones reply to identify sequential zones
503 * @sdkp: disk used
504 * @buf: report reply buffer
Bart Van Asschee0b14a42018-03-01 15:07:19 -0800505 * @buflen: length of @buf
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700506 * @zone_shift: logarithm base 2 of the number of blocks in a zone
Bart Van Asschee0b14a42018-03-01 15:07:19 -0800507 * @seq_zones_bitmap: bitmap of sequential zones to set
Damien Le Moal23349ca2017-12-21 15:43:43 +0900508 *
509 * Parse reported zone descriptors in @buf to identify sequential zones and
510 * set the reported zone bit in @seq_zones_bitmap accordingly.
511 * Since read-only and offline zones cannot be written, do not
512 * mark them as sequential in the bitmap.
513 * Return the LBA after the last zone reported.
514 */
515static sector_t sd_zbc_get_seq_zones(struct scsi_disk *sdkp, unsigned char *buf,
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700516 unsigned int buflen, u32 zone_shift,
Damien Le Moal23349ca2017-12-21 15:43:43 +0900517 unsigned long *seq_zones_bitmap)
518{
519 sector_t lba, next_lba = sdkp->capacity;
520 unsigned int buf_len, list_length;
521 unsigned char *rec;
522 u8 type, cond;
523
524 list_length = get_unaligned_be32(&buf[0]) + 64;
525 buf_len = min(list_length, buflen);
526 rec = buf + 64;
527
528 while (rec < buf + buf_len) {
529 type = rec[0] & 0x0f;
530 cond = (rec[1] >> 4) & 0xf;
531 lba = get_unaligned_be64(&rec[16]);
532 if (type != ZBC_ZONE_TYPE_CONV &&
533 cond != ZBC_ZONE_COND_READONLY &&
534 cond != ZBC_ZONE_COND_OFFLINE)
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700535 set_bit(lba >> zone_shift, seq_zones_bitmap);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900536 next_lba = lba + get_unaligned_be64(&rec[8]);
537 rec += 64;
538 }
539
540 return next_lba;
541}
542
543/**
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700544 * sd_zbc_setup_seq_zones_bitmap - Initialize a seq zone bitmap.
Damien Le Moal23349ca2017-12-21 15:43:43 +0900545 * @sdkp: target disk
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700546 * @zone_shift: logarithm base 2 of the number of blocks in a zone
547 * @nr_zones: number of zones to set up a seq zone bitmap for
Damien Le Moal23349ca2017-12-21 15:43:43 +0900548 *
549 * Allocate a zone bitmap and initialize it by identifying sequential zones.
550 */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700551static unsigned long *
552sd_zbc_setup_seq_zones_bitmap(struct scsi_disk *sdkp, u32 zone_shift,
553 u32 nr_zones)
Damien Le Moal23349ca2017-12-21 15:43:43 +0900554{
555 struct request_queue *q = sdkp->disk->queue;
556 unsigned long *seq_zones_bitmap;
557 sector_t lba = 0;
558 unsigned char *buf;
559 int ret = -ENOMEM;
560
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700561 seq_zones_bitmap = sd_zbc_alloc_zone_bitmap(nr_zones, q->node);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900562 if (!seq_zones_bitmap)
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700563 return ERR_PTR(-ENOMEM);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900564
565 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
566 if (!buf)
567 goto out;
568
569 while (lba < sdkp->capacity) {
570 ret = sd_zbc_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, lba);
571 if (ret)
572 goto out;
573 lba = sd_zbc_get_seq_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700574 zone_shift, seq_zones_bitmap);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900575 }
576
577 if (lba != sdkp->capacity) {
578 /* Something went wrong */
579 ret = -EIO;
580 }
581
582out:
583 kfree(buf);
584 if (ret) {
585 kfree(seq_zones_bitmap);
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700586 return ERR_PTR(ret);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900587 }
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700588 return seq_zones_bitmap;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900589}
590
591static void sd_zbc_cleanup(struct scsi_disk *sdkp)
592{
593 struct request_queue *q = sdkp->disk->queue;
594
595 kfree(q->seq_zones_bitmap);
596 q->seq_zones_bitmap = NULL;
597
598 kfree(q->seq_zones_wlock);
599 q->seq_zones_wlock = NULL;
600
601 q->nr_zones = 0;
602}
603
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700604static int sd_zbc_setup(struct scsi_disk *sdkp, u32 zone_blocks)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900605{
Damien Le Moal23349ca2017-12-21 15:43:43 +0900606 struct request_queue *q = sdkp->disk->queue;
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700607 u32 zone_shift = ilog2(zone_blocks);
608 u32 nr_zones;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900609 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900610
611 /* chunk_sectors indicates the zone size */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700612 blk_queue_chunk_sectors(q,
613 logical_to_sectors(sdkp->device, zone_blocks));
614 nr_zones = round_up(sdkp->capacity, zone_blocks) >> zone_shift;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900615
Damien Le Moal23349ca2017-12-21 15:43:43 +0900616 /*
617 * Initialize the device request queue information if the number
618 * of zones changed.
619 */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700620 if (nr_zones != sdkp->nr_zones || nr_zones != q->nr_zones) {
621 unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
622 size_t zone_bitmap_size;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900623
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700624 if (nr_zones) {
625 seq_zones_wlock = sd_zbc_alloc_zone_bitmap(nr_zones,
626 q->node);
627 if (!seq_zones_wlock) {
Damien Le Moal23349ca2017-12-21 15:43:43 +0900628 ret = -ENOMEM;
629 goto err;
630 }
631
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700632 seq_zones_bitmap = sd_zbc_setup_seq_zones_bitmap(sdkp,
633 zone_shift, nr_zones);
634 if (IS_ERR(seq_zones_bitmap)) {
635 ret = PTR_ERR(seq_zones_bitmap);
636 kfree(seq_zones_wlock);
Damien Le Moal23349ca2017-12-21 15:43:43 +0900637 goto err;
638 }
639 }
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700640 zone_bitmap_size = BITS_TO_LONGS(nr_zones) *
641 sizeof(unsigned long);
642 blk_mq_freeze_queue(q);
643 if (q->nr_zones != nr_zones) {
644 /* READ16/WRITE16 is mandatory for ZBC disks */
645 sdkp->device->use_16_for_rw = 1;
646 sdkp->device->use_10_for_rw = 0;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900647
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700648 sdkp->zone_blocks = zone_blocks;
649 sdkp->zone_shift = zone_shift;
650 sdkp->nr_zones = nr_zones;
651 q->nr_zones = nr_zones;
652 swap(q->seq_zones_wlock, seq_zones_wlock);
653 swap(q->seq_zones_bitmap, seq_zones_bitmap);
654 } else if (memcmp(q->seq_zones_bitmap, seq_zones_bitmap,
655 zone_bitmap_size) != 0) {
656 memcpy(q->seq_zones_bitmap, seq_zones_bitmap,
657 zone_bitmap_size);
658 }
659 blk_mq_unfreeze_queue(q);
660 kfree(seq_zones_wlock);
661 kfree(seq_zones_bitmap);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900662 }
663
664 return 0;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900665
666err:
667 sd_zbc_cleanup(sdkp);
668 return ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900669}
670
Damien Le Moale98f42b2017-10-11 05:54:22 +0900671int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900672{
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700673 int64_t zone_blocks;
Bart Van Asschef7053242017-04-24 16:51:14 +0900674 int ret;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900675
676 if (!sd_is_zoned(sdkp))
677 /*
678 * Device managed or normal SCSI disk,
679 * no special handling required
680 */
681 return 0;
682
Hannes Reinecke89d94752016-10-18 15:40:34 +0900683 /* Get zoned block device characteristics */
684 ret = sd_zbc_read_zoned_characteristics(sdkp, buf);
685 if (ret)
686 goto err;
687
688 /*
689 * Check for unconstrained reads: host-managed devices with
690 * constrained reads (drives failing read after write pointer)
691 * are not supported.
692 */
693 if (!sdkp->urswrz) {
694 if (sdkp->first_scan)
695 sd_printk(KERN_NOTICE, sdkp,
696 "constrained reads devices are not supported\n");
697 ret = -ENODEV;
698 goto err;
699 }
700
701 /* Check capacity */
702 ret = sd_zbc_check_capacity(sdkp, buf);
703 if (ret)
704 goto err;
Hannes Reinecke89d94752016-10-18 15:40:34 +0900705
706 /*
707 * Check zone size: only devices with a constant zone size (except
708 * an eventual last runt zone) that is a power of 2 are supported.
709 */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700710 zone_blocks = sd_zbc_check_zone_size(sdkp);
711 ret = -EFBIG;
712 if (zone_blocks != (u32)zone_blocks)
713 goto err;
714 ret = zone_blocks;
715 if (ret < 0)
Hannes Reinecke89d94752016-10-18 15:40:34 +0900716 goto err;
717
718 /* The drive satisfies the kernel restrictions: set it up */
Bart Van Asscheccce20f2018-04-16 18:04:41 -0700719 ret = sd_zbc_setup(sdkp, zone_blocks);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900720 if (ret)
721 goto err;
722
Hannes Reinecke89d94752016-10-18 15:40:34 +0900723 return 0;
724
725err:
726 sdkp->capacity = 0;
Damien Le Moal23349ca2017-12-21 15:43:43 +0900727 sd_zbc_cleanup(sdkp);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900728
729 return ret;
730}
731
732void sd_zbc_remove(struct scsi_disk *sdkp)
733{
Damien Le Moal23349ca2017-12-21 15:43:43 +0900734 sd_zbc_cleanup(sdkp);
Hannes Reinecke89d94752016-10-18 15:40:34 +0900735}
736
737void sd_zbc_print_zones(struct scsi_disk *sdkp)
738{
739 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
740 return;
741
742 if (sdkp->capacity & (sdkp->zone_blocks - 1))
743 sd_printk(KERN_NOTICE, sdkp,
744 "%u zones of %u logical blocks + 1 runt zone\n",
745 sdkp->nr_zones - 1,
746 sdkp->zone_blocks);
747 else
748 sd_printk(KERN_NOTICE, sdkp,
749 "%u zones of %u logical blocks\n",
750 sdkp->nr_zones,
751 sdkp->zone_blocks);
752}