1*b346a95bSkrw /* $OpenBSD: softraid_concat.c,v 1.27 2020/04/25 14:37:43 krw Exp $ */
22b5fc845Sjsing /*
32b5fc845Sjsing * Copyright (c) 2008 Marco Peereboom <marco@peereboom.us>
42b5fc845Sjsing * Copyright (c) 2011 Joel Sing <jsing@openbsd.org>
52b5fc845Sjsing *
62b5fc845Sjsing * Permission to use, copy, modify, and distribute this software for any
72b5fc845Sjsing * purpose with or without fee is hereby granted, provided that the above
82b5fc845Sjsing * copyright notice and this permission notice appear in all copies.
92b5fc845Sjsing *
102b5fc845Sjsing * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
112b5fc845Sjsing * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
122b5fc845Sjsing * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
132b5fc845Sjsing * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
142b5fc845Sjsing * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
152b5fc845Sjsing * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
162b5fc845Sjsing * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
172b5fc845Sjsing */
182b5fc845Sjsing
192b5fc845Sjsing #include "bio.h"
202b5fc845Sjsing
212b5fc845Sjsing #include <sys/param.h>
222b5fc845Sjsing #include <sys/systm.h>
232b5fc845Sjsing #include <sys/device.h>
242b5fc845Sjsing #include <sys/buf.h>
252b5fc845Sjsing #include <sys/queue.h>
262b5fc845Sjsing #include <sys/sensors.h>
272b5fc845Sjsing
282b5fc845Sjsing #include <scsi/scsi_all.h>
292b5fc845Sjsing #include <scsi/scsiconf.h>
302b5fc845Sjsing #include <scsi/scsi_disk.h>
312b5fc845Sjsing
322b5fc845Sjsing #include <dev/softraidvar.h>
332b5fc845Sjsing
342b5fc845Sjsing /* CONCAT functions. */
352b5fc845Sjsing int sr_concat_create(struct sr_discipline *, struct bioc_createraid *,
362b5fc845Sjsing int, int64_t);
372b5fc845Sjsing int sr_concat_assemble(struct sr_discipline *, struct bioc_createraid *,
387c003ea3Sjsing int, void *);
39ceed1c06Sjsing int sr_concat_init(struct sr_discipline *);
402b5fc845Sjsing int sr_concat_rw(struct sr_workunit *);
412b5fc845Sjsing
422b5fc845Sjsing /* Discipline initialisation. */
432b5fc845Sjsing void
sr_concat_discipline_init(struct sr_discipline * sd)442b5fc845Sjsing sr_concat_discipline_init(struct sr_discipline *sd)
452b5fc845Sjsing {
462b5fc845Sjsing /* Fill out discipline members. */
472b5fc845Sjsing sd->sd_type = SR_MD_CONCAT;
486d81b338Sjsing strlcpy(sd->sd_name, "CONCAT", sizeof(sd->sd_name));
492b5fc845Sjsing sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE |
502b5fc845Sjsing SR_CAP_NON_COERCED;
512b5fc845Sjsing sd->sd_max_wu = SR_CONCAT_NOWU;
522b5fc845Sjsing
532b5fc845Sjsing /* Setup discipline specific function pointers. */
542b5fc845Sjsing sd->sd_assemble = sr_concat_assemble;
552b5fc845Sjsing sd->sd_create = sr_concat_create;
562b5fc845Sjsing sd->sd_scsi_rw = sr_concat_rw;
572b5fc845Sjsing }
582b5fc845Sjsing
592b5fc845Sjsing int
sr_concat_create(struct sr_discipline * sd,struct bioc_createraid * bc,int no_chunk,int64_t coerced_size)602b5fc845Sjsing sr_concat_create(struct sr_discipline *sd, struct bioc_createraid *bc,
612b5fc845Sjsing int no_chunk, int64_t coerced_size)
622b5fc845Sjsing {
632e5e6558Skrw int i;
642e5e6558Skrw
65*b346a95bSkrw if (no_chunk < 1) {
66*b346a95bSkrw sr_error(sd->sd_sc, "%s requires one or more chunks",
67984c6b2dSjsing sd->sd_name);
682b5fc845Sjsing return EINVAL;
695f082130Sjsing }
702b5fc845Sjsing
712e5e6558Skrw sd->sd_meta->ssdi.ssd_size = 0;
722e5e6558Skrw for (i = 0; i < no_chunk; i++) {
732e5e6558Skrw sd->sd_meta->ssdi.ssd_size +=
742e5e6558Skrw sd->sd_vol.sv_chunks[i]->src_size;
752e5e6558Skrw }
762e5e6558Skrw
77ceed1c06Sjsing return sr_concat_init(sd);
782b5fc845Sjsing }
792b5fc845Sjsing
802b5fc845Sjsing int
sr_concat_assemble(struct sr_discipline * sd,struct bioc_createraid * bc,int no_chunk,void * data)812b5fc845Sjsing sr_concat_assemble(struct sr_discipline *sd, struct bioc_createraid *bc,
827c003ea3Sjsing int no_chunk, void *data)
832b5fc845Sjsing {
84ceed1c06Sjsing return sr_concat_init(sd);
85ceed1c06Sjsing }
86ceed1c06Sjsing
87ceed1c06Sjsing int
sr_concat_init(struct sr_discipline * sd)88ceed1c06Sjsing sr_concat_init(struct sr_discipline *sd)
89ceed1c06Sjsing {
90ceed1c06Sjsing sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * sd->sd_meta->ssdi.ssd_chunk_no;
912b5fc845Sjsing
922b5fc845Sjsing return 0;
932b5fc845Sjsing }
942b5fc845Sjsing
952b5fc845Sjsing int
sr_concat_rw(struct sr_workunit * wu)962b5fc845Sjsing sr_concat_rw(struct sr_workunit *wu)
972b5fc845Sjsing {
982b5fc845Sjsing struct sr_discipline *sd = wu->swu_dis;
992b5fc845Sjsing struct scsi_xfer *xs = wu->swu_xs;
1002b5fc845Sjsing struct sr_ccb *ccb;
1012b5fc845Sjsing struct sr_chunk *scp;
102d9ec6765Skrw daddr_t blkno;
103d9ec6765Skrw int64_t lbaoffs, offset;
1043fb9c90bSkrw int64_t no_chunk, chunkend, chunk, chunksize;
1053fb9c90bSkrw int64_t length, leftover;
1062b5fc845Sjsing u_int8_t *data;
1072b5fc845Sjsing
108d9ec6765Skrw /* blkno and scsi error will be handled by sr_validate_io */
109d9ec6765Skrw if (sr_validate_io(wu, &blkno, "sr_concat_rw"))
1102b5fc845Sjsing goto bad;
1112b5fc845Sjsing
1122b5fc845Sjsing no_chunk = sd->sd_meta->ssdi.ssd_chunk_no;
1132b5fc845Sjsing
114d9ec6765Skrw DNPRINTF(SR_D_DIS, "%s: %s: front end io: blkno %lld size %d\n",
1152b5fc845Sjsing DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname,
116d9ec6765Skrw (long long)blkno, xs->datalen);
1172b5fc845Sjsing
1182b5fc845Sjsing /* All offsets are in bytes. */
119d9ec6765Skrw lbaoffs = blkno << DEV_BSHIFT;
1202b5fc845Sjsing leftover = xs->datalen;
1212b5fc845Sjsing data = xs->data;
12261ce5139Sjsing for (;;) {
1232b5fc845Sjsing
1242b5fc845Sjsing chunkend = 0;
125d9ec6765Skrw offset = lbaoffs;
1262b5fc845Sjsing for (chunk = 0; chunk < no_chunk; chunk++) {
1272b5fc845Sjsing chunksize = sd->sd_vol.sv_chunks[chunk]->src_size <<
1282b5fc845Sjsing DEV_BSHIFT;
1292b5fc845Sjsing chunkend += chunksize;
1302b5fc845Sjsing if (lbaoffs < chunkend)
1312b5fc845Sjsing break;
132d9ec6765Skrw offset -= chunksize;
1332b5fc845Sjsing }
1342b5fc845Sjsing if (lbaoffs > chunkend)
1352b5fc845Sjsing goto bad;
1362b5fc845Sjsing
1372b5fc845Sjsing length = MIN(MIN(leftover, chunkend - lbaoffs), MAXPHYS);
1382b5fc845Sjsing
1392b5fc845Sjsing /* make sure chunk is online */
1402b5fc845Sjsing scp = sd->sd_vol.sv_chunks[chunk];
14161ce5139Sjsing if (scp->src_meta.scm_status != BIOC_SDONLINE)
1422b5fc845Sjsing goto bad;
1432b5fc845Sjsing
1441d84833fSjsing DNPRINTF(SR_D_DIS, "%s: %s %s io lbaoffs %lld "
145d9ec6765Skrw "chunk %lld chunkend %lld offset %lld length %lld "
1461d84833fSjsing "leftover %lld data %p\n",
1471d84833fSjsing DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name,
148d9ec6765Skrw lbaoffs, chunk, chunkend, offset, length, leftover, data);
1491d84833fSjsing
150d9ec6765Skrw blkno = offset >> DEV_BSHIFT;
151d9ec6765Skrw ccb = sr_ccb_rw(sd, chunk, blkno, length, data, xs->flags, 0);
1522b5fc845Sjsing if (!ccb) {
1532b5fc845Sjsing /* should never happen but handle more gracefully */
1542b5fc845Sjsing printf("%s: %s: too many ccbs queued\n",
15561ce5139Sjsing DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname);
1562b5fc845Sjsing goto bad;
1572b5fc845Sjsing }
15861ce5139Sjsing sr_wu_enqueue_ccb(wu, ccb);
1592b5fc845Sjsing
1602b5fc845Sjsing leftover -= length;
1612b5fc845Sjsing if (leftover == 0)
1622b5fc845Sjsing break;
1632b5fc845Sjsing data += length;
1642b5fc845Sjsing lbaoffs += length;
1652b5fc845Sjsing }
1662b5fc845Sjsing
16760781f8cSjsing sr_schedule_wu(wu);
1682b5fc845Sjsing
1692b5fc845Sjsing return (0);
17060781f8cSjsing
1712b5fc845Sjsing bad:
1722b5fc845Sjsing /* wu is unwound by sr_wu_put */
1732b5fc845Sjsing return (1);
1742b5fc845Sjsing }
175