scsi: return blk_status_t from scsi_init_io and ->init_command
[linux-block.git] / drivers / scsi / sd_zbc.c
CommitLineData
89d94756
HR
1/*
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>
89d94756
HR
31
32#include "sd.h"
89d94756
HR
33
34/**
e98f42bc
DLM
35 * 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.
89d94756 41 */
e98f42bc 42static void sd_zbc_parse_report(struct scsi_disk *sdkp, u8 *buf,
89d94756
HR
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/**
e76239a3 65 * sd_zbc_do_report_zones - Issue a REPORT ZONES scsi command.
e98f42bc
DLM
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
d2e428e4 70 * @partial: Do partial report
e98f42bc
DLM
71 *
72 * For internal use during device validation.
d2e428e4
DLM
73 * Using partial=true can significantly speed up execution of a report zones
74 * command because the disk does not have to count all possible report matching
75 * zones and will only report the count of zones fitting in the command reply
76 * buffer.
89d94756 77 */
e76239a3
CH
78static int sd_zbc_do_report_zones(struct scsi_disk *sdkp, unsigned char *buf,
79 unsigned int buflen, sector_t lba,
80 bool partial)
89d94756
HR
81{
82 struct scsi_device *sdp = sdkp->device;
83 const int timeout = sdp->request_queue->rq_timeout;
84 struct scsi_sense_hdr sshdr;
85 unsigned char cmd[16];
86 unsigned int rep_len;
87 int result;
88
89 memset(cmd, 0, 16);
90 cmd[0] = ZBC_IN;
91 cmd[1] = ZI_REPORT_ZONES;
92 put_unaligned_be64(lba, &cmd[2]);
93 put_unaligned_be32(buflen, &cmd[10]);
d2e428e4
DLM
94 if (partial)
95 cmd[14] = ZBC_REPORT_ZONE_PARTIAL;
89d94756
HR
96 memset(buf, 0, buflen);
97
98 result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
99 buf, buflen, &sshdr,
100 timeout, SD_MAX_RETRIES, NULL);
101 if (result) {
102 sd_printk(KERN_ERR, sdkp,
103 "REPORT ZONES lba %llu failed with %d/%d\n",
104 (unsigned long long)lba,
105 host_byte(result), driver_byte(result));
106 return -EIO;
107 }
108
109 rep_len = get_unaligned_be32(&buf[0]);
110 if (rep_len < 64) {
111 sd_printk(KERN_ERR, sdkp,
112 "REPORT ZONES report invalid length %u\n",
113 rep_len);
114 return -EIO;
115 }
116
117 return 0;
118}
119
e98f42bc 120/**
e76239a3
CH
121 * sd_zbc_report_zones - Disk report zones operation.
122 * @disk: The target disk
123 * @sector: Start 512B sector of the report
124 * @zones: Array of zone descriptors
125 * @nr_zones: Number of descriptors in the array
126 * @gfp_mask: Memory allocation mask
e98f42bc 127 *
e76239a3 128 * Execute a report zones command on the target disk.
e98f42bc 129 */
e76239a3
CH
130int sd_zbc_report_zones(struct gendisk *disk, sector_t sector,
131 struct blk_zone *zones, unsigned int *nr_zones,
132 gfp_t gfp_mask)
89d94756 133{
e76239a3
CH
134 struct scsi_disk *sdkp = scsi_disk(disk);
135 unsigned int i, buflen, nrz = *nr_zones;
136 unsigned char *buf;
137 size_t offset = 0;
138 int ret = 0;
89d94756
HR
139
140 if (!sd_is_zoned(sdkp))
141 /* Not a zoned device */
e76239a3 142 return -EOPNOTSUPP;
89d94756 143
e76239a3
CH
144 /*
145 * Get a reply buffer for the number of requested zones plus a header.
146 * For ATA, buffers must be aligned to 512B.
147 */
148 buflen = roundup((nrz + 1) * 64, 512);
149 buf = kmalloc(buflen, gfp_mask);
150 if (!buf)
151 return -ENOMEM;
89d94756 152
e76239a3
CH
153 ret = sd_zbc_do_report_zones(sdkp, buf, buflen,
154 sectors_to_logical(sdkp->device, sector), true);
155 if (ret)
156 goto out_free_buf;
89d94756 157
e76239a3
CH
158 nrz = min(nrz, get_unaligned_be32(&buf[0]) / 64);
159 for (i = 0; i < nrz; i++) {
160 offset += 64;
161 sd_zbc_parse_report(sdkp, buf + offset, zones);
162 zones++;
163 }
89d94756 164
e76239a3 165 *nr_zones = nrz;
89d94756 166
e76239a3
CH
167out_free_buf:
168 kfree(buf);
89d94756 169
e76239a3 170 return ret;
89d94756
HR
171}
172
e98f42bc
DLM
173/**
174 * sd_zbc_zone_sectors - Get the device zone size in number of 512B sectors.
175 * @sdkp: The target disk
176 */
89d94756
HR
177static inline sector_t sd_zbc_zone_sectors(struct scsi_disk *sdkp)
178{
179 return logical_to_sectors(sdkp->device, sdkp->zone_blocks);
180}
181
e98f42bc
DLM
182/**
183 * sd_zbc_setup_reset_cmnd - Prepare a RESET WRITE POINTER scsi command.
184 * @cmd: the command to setup
185 *
186 * Called from sd_init_command() for a REQ_OP_ZONE_RESET request.
187 */
159b2cbf 188blk_status_t sd_zbc_setup_reset_cmnd(struct scsi_cmnd *cmd)
89d94756
HR
189{
190 struct request *rq = cmd->request;
191 struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
192 sector_t sector = blk_rq_pos(rq);
193 sector_t block = sectors_to_logical(sdkp->device, sector);
89d94756
HR
194
195 if (!sd_is_zoned(sdkp))
196 /* Not a zoned device */
159b2cbf 197 return BLK_STS_IOERR;
89d94756
HR
198
199 if (sdkp->device->changed)
159b2cbf 200 return BLK_STS_IOERR;
89d94756
HR
201
202 if (sector & (sd_zbc_zone_sectors(sdkp) - 1))
203 /* Unaligned request */
159b2cbf 204 return BLK_STS_IOERR;
89d94756 205
89d94756
HR
206 cmd->cmd_len = 16;
207 memset(cmd->cmnd, 0, cmd->cmd_len);
208 cmd->cmnd[0] = ZBC_OUT;
209 cmd->cmnd[1] = ZO_RESET_WRITE_POINTER;
210 put_unaligned_be64(block, &cmd->cmnd[2]);
211
212 rq->timeout = SD_TIMEOUT;
213 cmd->sc_data_direction = DMA_NONE;
214 cmd->transfersize = 0;
215 cmd->allowed = 0;
216
159b2cbf 217 return BLK_STS_OK;
89d94756
HR
218}
219
e98f42bc
DLM
220/**
221 * sd_zbc_complete - ZBC command post processing.
222 * @cmd: Completed command
223 * @good_bytes: Command reply bytes
224 * @sshdr: command sense header
225 *
226 * Called from sd_done(). Process report zones reply and handle reset zone
227 * and write commands errors.
228 */
229void sd_zbc_complete(struct scsi_cmnd *cmd, unsigned int good_bytes,
89d94756
HR
230 struct scsi_sense_hdr *sshdr)
231{
232 int result = cmd->result;
233 struct request *rq = cmd->request;
234
235 switch (req_op(rq)) {
868ed5a5
DLM
236 case REQ_OP_ZONE_RESET:
237
238 if (result &&
239 sshdr->sense_key == ILLEGAL_REQUEST &&
240 sshdr->asc == 0x24)
241 /*
242 * INVALID FIELD IN CDB error: reset of a conventional
243 * zone was attempted. Nothing to worry about, so be
244 * quiet about the error.
245 */
246 rq->rq_flags |= RQF_QUIET;
247 break;
248
89d94756 249 case REQ_OP_WRITE:
02d26103 250 case REQ_OP_WRITE_ZEROES:
89d94756 251 case REQ_OP_WRITE_SAME:
89d94756 252 break;
89d94756
HR
253 }
254}
255
256/**
7f9d35d2 257 * sd_zbc_check_zoned_characteristics - Check zoned block device characteristics
e98f42bc
DLM
258 * @sdkp: Target disk
259 * @buf: Buffer where to store the VPD page data
260 *
7f9d35d2 261 * Read VPD page B6, get information and check that reads are unconstrained.
89d94756 262 */
7f9d35d2
DLM
263static int sd_zbc_check_zoned_characteristics(struct scsi_disk *sdkp,
264 unsigned char *buf)
89d94756
HR
265{
266
267 if (scsi_get_vpd_page(sdkp->device, 0xb6, buf, 64)) {
268 sd_printk(KERN_NOTICE, sdkp,
7f9d35d2 269 "Read zoned characteristics VPD page failed\n");
89d94756
HR
270 return -ENODEV;
271 }
272
273 if (sdkp->device->type != TYPE_ZBC) {
274 /* Host-aware */
275 sdkp->urswrz = 1;
4a109032
DLM
276 sdkp->zones_optimal_open = get_unaligned_be32(&buf[8]);
277 sdkp->zones_optimal_nonseq = get_unaligned_be32(&buf[12]);
89d94756
HR
278 sdkp->zones_max_open = 0;
279 } else {
280 /* Host-managed */
281 sdkp->urswrz = buf[4] & 1;
282 sdkp->zones_optimal_open = 0;
283 sdkp->zones_optimal_nonseq = 0;
4a109032 284 sdkp->zones_max_open = get_unaligned_be32(&buf[16]);
89d94756
HR
285 }
286
7f9d35d2
DLM
287 /*
288 * Check for unconstrained reads: host-managed devices with
289 * constrained reads (drives failing read after write pointer)
290 * are not supported.
291 */
292 if (!sdkp->urswrz) {
293 if (sdkp->first_scan)
294 sd_printk(KERN_NOTICE, sdkp,
295 "constrained reads devices are not supported\n");
296 return -ENODEV;
297 }
298
89d94756
HR
299 return 0;
300}
301
e8c77ec4 302#define SD_ZBC_BUF_SIZE 131072U
89d94756 303
e98f42bc 304/**
d2e428e4 305 * sd_zbc_check_zones - Check the device capacity and zone sizes
e98f42bc
DLM
306 * @sdkp: Target disk
307 *
d2e428e4
DLM
308 * Check that the device capacity as reported by READ CAPACITY matches the
309 * max_lba value (plus one)of the report zones command reply. Also check that
310 * all zones of the device have an equal size, only allowing the last zone of
311 * the disk to have a smaller size (runt zone). The zone size must also be a
312 * power of two.
ccce20fc 313 *
f13cff6c
DLM
314 * Returns the zone size in number of blocks upon success or an error code
315 * upon failure.
e98f42bc 316 */
5f832a39 317static int sd_zbc_check_zones(struct scsi_disk *sdkp, u32 *zblocks)
89d94756 318{
4b433924 319 u64 zone_blocks = 0;
d2e428e4 320 sector_t max_lba, block = 0;
89d94756
HR
321 unsigned char *buf;
322 unsigned char *rec;
323 unsigned int buf_len;
324 unsigned int list_length;
5f832a39 325 int ret;
89d94756
HR
326 u8 same;
327
89d94756
HR
328 /* Get a buffer */
329 buf = kmalloc(SD_ZBC_BUF_SIZE, GFP_KERNEL);
330 if (!buf)
331 return -ENOMEM;
332
d2e428e4 333 /* Do a report zone to get max_lba and the same field */
e76239a3 334 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE, 0, false);
4b433924
DLM
335 if (ret)
336 goto out_free;
89d94756 337
d2e428e4
DLM
338 if (sdkp->rc_basis == 0) {
339 /* The max_lba field is the capacity of this device */
340 max_lba = get_unaligned_be64(&buf[8]);
341 if (sdkp->capacity != max_lba + 1) {
342 if (sdkp->first_scan)
343 sd_printk(KERN_WARNING, sdkp,
344 "Changing capacity from %llu to max LBA+1 %llu\n",
345 (unsigned long long)sdkp->capacity,
346 (unsigned long long)max_lba + 1);
347 sdkp->capacity = max_lba + 1;
348 }
349 }
350
351 /*
352 * Check same field: for any value other than 0, we know that all zones
353 * have the same size.
354 */
89d94756
HR
355 same = buf[4] & 0x0f;
356 if (same > 0) {
357 rec = &buf[64];
358 zone_blocks = get_unaligned_be64(&rec[8]);
359 goto out;
360 }
361
362 /*
363 * Check the size of all zones: all zones must be of
364 * equal size, except the last zone which can be smaller
365 * than other zones.
366 */
367 do {
368
369 /* Parse REPORT ZONES header */
370 list_length = get_unaligned_be32(&buf[0]) + 64;
371 rec = buf + 64;
e8c77ec4 372 buf_len = min(list_length, SD_ZBC_BUF_SIZE);
89d94756
HR
373
374 /* Parse zone descriptors */
375 while (rec < buf + buf_len) {
ccce20fc
BVA
376 u64 this_zone_blocks = get_unaligned_be64(&rec[8]);
377
378 if (zone_blocks == 0) {
379 zone_blocks = this_zone_blocks;
380 } else if (this_zone_blocks != zone_blocks &&
381 (block + this_zone_blocks < sdkp->capacity
382 || this_zone_blocks > zone_blocks)) {
3aadbe25 383 zone_blocks = 0;
89d94756
HR
384 goto out;
385 }
ccce20fc 386 block += this_zone_blocks;
89d94756
HR
387 rec += 64;
388 }
389
390 if (block < sdkp->capacity) {
e76239a3
CH
391 ret = sd_zbc_do_report_zones(sdkp, buf, SD_ZBC_BUF_SIZE,
392 block, true);
89d94756 393 if (ret)
4b433924 394 goto out_free;
89d94756
HR
395 }
396
397 } while (block < sdkp->capacity);
398
89d94756 399out:
89d94756
HR
400 if (!zone_blocks) {
401 if (sdkp->first_scan)
402 sd_printk(KERN_NOTICE, sdkp,
403 "Devices with non constant zone "
404 "size are not supported\n");
4b433924
DLM
405 ret = -ENODEV;
406 } else if (!is_power_of_2(zone_blocks)) {
89d94756
HR
407 if (sdkp->first_scan)
408 sd_printk(KERN_NOTICE, sdkp,
409 "Devices with non power of 2 zone "
410 "size are not supported\n");
4b433924
DLM
411 ret = -ENODEV;
412 } else if (logical_to_sectors(sdkp->device, zone_blocks) > UINT_MAX) {
89d94756
HR
413 if (sdkp->first_scan)
414 sd_printk(KERN_NOTICE, sdkp,
415 "Zone size too large\n");
5f832a39 416 ret = -EFBIG;
4b433924 417 } else {
5f832a39
DLM
418 *zblocks = zone_blocks;
419 ret = 0;
89d94756
HR
420 }
421
4b433924
DLM
422out_free:
423 kfree(buf);
89d94756 424
4b433924 425 return ret;
89d94756
HR
426}
427
e98f42bc 428int sd_zbc_read_zones(struct scsi_disk *sdkp, unsigned char *buf)
89d94756 429{
bf505456
DLM
430 struct gendisk *disk = sdkp->disk;
431 unsigned int nr_zones;
5f832a39 432 u32 zone_blocks;
f7053240 433 int ret;
89d94756
HR
434
435 if (!sd_is_zoned(sdkp))
436 /*
437 * Device managed or normal SCSI disk,
438 * no special handling required
439 */
440 return 0;
441
7f9d35d2
DLM
442 /* Check zoned block device characteristics (unconstrained reads) */
443 ret = sd_zbc_check_zoned_characteristics(sdkp, buf);
89d94756
HR
444 if (ret)
445 goto err;
446
89d94756
HR
447 /*
448 * Check zone size: only devices with a constant zone size (except
449 * an eventual last runt zone) that is a power of 2 are supported.
450 */
5f832a39
DLM
451 ret = sd_zbc_check_zones(sdkp, &zone_blocks);
452 if (ret != 0)
89d94756
HR
453 goto err;
454
455 /* The drive satisfies the kernel restrictions: set it up */
bf505456
DLM
456 blk_queue_chunk_sectors(sdkp->disk->queue,
457 logical_to_sectors(sdkp->device, zone_blocks));
458 nr_zones = round_up(sdkp->capacity, zone_blocks) >> ilog2(zone_blocks);
459
460 /* READ16/WRITE16 is mandatory for ZBC disks */
461 sdkp->device->use_16_for_rw = 1;
462 sdkp->device->use_10_for_rw = 0;
463
464 /*
465 * If something changed, revalidate the disk zone bitmaps once we have
466 * the capacity, that is on the second revalidate execution during disk
467 * scan and always during normal revalidate.
468 */
469 if (sdkp->first_scan)
470 return 0;
471 if (sdkp->zone_blocks != zone_blocks ||
472 sdkp->nr_zones != nr_zones ||
473 disk->queue->nr_zones != nr_zones) {
474 ret = blk_revalidate_disk_zones(disk);
475 if (ret != 0)
476 goto err;
477 sdkp->zone_blocks = zone_blocks;
478 sdkp->nr_zones = nr_zones;
479 }
89d94756
HR
480
481 return 0;
482
483err:
484 sdkp->capacity = 0;
485
486 return ret;
487}
488
89d94756
HR
489void sd_zbc_print_zones(struct scsi_disk *sdkp)
490{
491 if (!sd_is_zoned(sdkp) || !sdkp->capacity)
492 return;
493
494 if (sdkp->capacity & (sdkp->zone_blocks - 1))
495 sd_printk(KERN_NOTICE, sdkp,
496 "%u zones of %u logical blocks + 1 runt zone\n",
497 sdkp->nr_zones - 1,
498 sdkp->zone_blocks);
499 else
500 sd_printk(KERN_NOTICE, sdkp,
501 "%u zones of %u logical blocks\n",
502 sdkp->nr_zones,
503 sdkp->zone_blocks);
504}