1 /* $OpenBSD: softraid_concat.c,v 1.11 2013/03/02 12:50:01 jsing Exp $ */ 2 /* 3 * Copyright (c) 2008 Marco Peereboom <marco@peereboom.us> 4 * Copyright (c) 2011 Joel Sing <jsing@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include "bio.h" 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/device.h> 24 #include <sys/buf.h> 25 #include <sys/queue.h> 26 #include <sys/sensors.h> 27 28 #include <scsi/scsi_all.h> 29 #include <scsi/scsiconf.h> 30 #include <scsi/scsi_disk.h> 31 32 #include <dev/softraidvar.h> 33 34 /* CONCAT functions. */ 35 int sr_concat_create(struct sr_discipline *, struct bioc_createraid *, 36 int, int64_t); 37 int sr_concat_assemble(struct sr_discipline *, struct bioc_createraid *, 38 int, void *); 39 int sr_concat_alloc_resources(struct sr_discipline *); 40 int sr_concat_free_resources(struct sr_discipline *); 41 int sr_concat_rw(struct sr_workunit *); 42 void sr_concat_intr(struct buf *); 43 44 /* Discipline initialisation. */ 45 void 46 sr_concat_discipline_init(struct sr_discipline *sd) 47 { 48 /* Fill out discipline members. */ 49 sd->sd_type = SR_MD_CONCAT; 50 strlcpy(sd->sd_name, "CONCAT", sizeof(sd->sd_name)); 51 sd->sd_capabilities = SR_CAP_SYSTEM_DISK | SR_CAP_AUTO_ASSEMBLE | 52 SR_CAP_NON_COERCED; 53 sd->sd_max_wu = SR_CONCAT_NOWU; 54 55 /* Setup discipline specific function pointers. */ 56 sd->sd_alloc_resources = sr_concat_alloc_resources; 57 sd->sd_assemble = sr_concat_assemble; 58 sd->sd_create = sr_concat_create; 59 sd->sd_free_resources = sr_concat_free_resources; 60 sd->sd_scsi_rw = sr_concat_rw; 61 sd->sd_scsi_intr = sr_concat_intr; 62 } 63 64 int 65 sr_concat_create(struct sr_discipline *sd, struct bioc_createraid *bc, 66 int no_chunk, int64_t coerced_size) 67 { 68 int i; 69 70 if (no_chunk < 2) { 71 sr_error(sd->sd_sc, "CONCAT requires two or more chunks"); 72 return EINVAL; 73 } 74 75 sd->sd_meta->ssdi.ssd_size = 0; 76 for (i = 0; i < no_chunk; i++) 77 sd->sd_meta->ssdi.ssd_size += 78 sd->sd_vol.sv_chunks[i]->src_size; 79 sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * no_chunk; 80 81 return 0; 82 } 83 84 int 85 sr_concat_assemble(struct sr_discipline *sd, struct bioc_createraid *bc, 86 int no_chunk, void *data) 87 { 88 sd->sd_max_ccb_per_wu = SR_CONCAT_NOWU * no_chunk; 89 90 return 0; 91 } 92 93 int 94 sr_concat_alloc_resources(struct sr_discipline *sd) 95 { 96 int rv = EINVAL; 97 98 DNPRINTF(SR_D_DIS, "%s: sr_concat_alloc_resources\n", 99 DEVNAME(sd->sd_sc)); 100 101 if (sr_wu_alloc(sd)) 102 goto bad; 103 if (sr_ccb_alloc(sd)) 104 goto bad; 105 106 rv = 0; 107 bad: 108 return (rv); 109 } 110 111 int 112 sr_concat_free_resources(struct sr_discipline *sd) 113 { 114 int rv = EINVAL; 115 116 DNPRINTF(SR_D_DIS, "%s: sr_concat_free_resources\n", 117 DEVNAME(sd->sd_sc)); 118 119 sr_wu_free(sd); 120 sr_ccb_free(sd); 121 122 rv = 0; 123 return (rv); 124 } 125 126 int 127 sr_concat_rw(struct sr_workunit *wu) 128 { 129 struct sr_discipline *sd = wu->swu_dis; 130 struct scsi_xfer *xs = wu->swu_xs; 131 struct sr_ccb *ccb; 132 struct sr_chunk *scp; 133 int s; 134 daddr64_t blk, lbaoffs, chunk, chunksize; 135 daddr64_t no_chunk, chunkend, physoffs; 136 daddr64_t length, leftover; 137 u_int8_t *data; 138 139 /* blk and scsi error will be handled by sr_validate_io */ 140 if (sr_validate_io(wu, &blk, "sr_concat_rw")) 141 goto bad; 142 143 no_chunk = sd->sd_meta->ssdi.ssd_chunk_no; 144 145 DNPRINTF(SR_D_DIS, "%s: %s: front end io: lba %lld size %d\n", 146 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, 147 blk, xs->datalen); 148 149 /* All offsets are in bytes. */ 150 lbaoffs = blk << DEV_BSHIFT; 151 leftover = xs->datalen; 152 data = xs->data; 153 for (;;) { 154 155 chunkend = 0; 156 physoffs = lbaoffs; 157 for (chunk = 0; chunk < no_chunk; chunk++) { 158 chunksize = sd->sd_vol.sv_chunks[chunk]->src_size << 159 DEV_BSHIFT; 160 chunkend += chunksize; 161 if (lbaoffs < chunkend) 162 break; 163 physoffs -= chunksize; 164 } 165 if (lbaoffs > chunkend) 166 goto bad; 167 168 length = MIN(MIN(leftover, chunkend - lbaoffs), MAXPHYS); 169 physoffs += sd->sd_meta->ssd_data_offset << DEV_BSHIFT; 170 171 /* make sure chunk is online */ 172 scp = sd->sd_vol.sv_chunks[chunk]; 173 if (scp->src_meta.scm_status != BIOC_SDONLINE) 174 goto bad; 175 176 DNPRINTF(SR_D_DIS, "%s: %s %s io lbaoffs %lld " 177 "chunk %lld chunkend %lld physoffs %lld length %lld " 178 "leftover %lld data %p\n", 179 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name, 180 lbaoffs, chunk, chunkend, physoffs, length, leftover, data); 181 182 blk = physoffs >> DEV_BSHIFT; 183 ccb = sr_ccb_rw(sd, chunk, blk, length, data, xs->flags, 0); 184 if (!ccb) { 185 /* should never happen but handle more gracefully */ 186 printf("%s: %s: too many ccbs queued\n", 187 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname); 188 goto bad; 189 } 190 sr_wu_enqueue_ccb(wu, ccb); 191 192 leftover -= length; 193 if (leftover == 0) 194 break; 195 data += length; 196 lbaoffs += length; 197 } 198 199 s = splbio(); 200 201 if (!sr_check_io_collision(wu)) 202 sr_raid_startwu(wu); 203 204 splx(s); 205 return (0); 206 bad: 207 /* wu is unwound by sr_wu_put */ 208 return (1); 209 } 210 211 void 212 sr_concat_intr(struct buf *bp) 213 { 214 struct sr_ccb *ccb = (struct sr_ccb *)bp; 215 struct sr_workunit *wu = ccb->ccb_wu; 216 #ifdef SR_DEBUG 217 struct sr_discipline *sd = wu->swu_dis; 218 struct scsi_xfer *xs = wu->swu_xs; 219 #endif 220 int s; 221 222 DNPRINTF(SR_D_INTR, "%s: %s %s intr bp %x xs %x\n", 223 DEVNAME(sd->sd_sc), sd->sd_meta->ssd_devname, sd->sd_name, bp, xs); 224 225 s = splbio(); 226 sr_ccb_done(ccb); 227 sr_wu_done(wu); 228 splx(s); 229 } 230