2 * Copyright (c) 2010 by Panasas, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * "Simple" VHBA device
37 #define DISK_NBLKS ((DISK_SIZE << 20) >> DISK_SHIFT)
40 #define PSEUDO_SPC (PSEUDO_SPT * PSEUDO_HDS)
49 static void vhba_task(void *, int);
50 static void vhbasimple_act(vhbasimple_t *, struct ccb_scsiio *);
53 vhba_init(vhba_softc_t *vhba)
55 static vhbasimple_t vhbas;
57 vhbas.disk_size = DISK_SIZE << 20;
58 vhbas.disk = malloc(vhbas.disk_size, M_DEVBUF, M_WAITOK|M_ZERO);
59 vhba->private = &vhbas;
60 TASK_INIT(&vhbas.qt, 0, vhba_task, &vhbas);
65 vhba_fini(vhba_softc_t *vhba)
67 vhbasimple_t *vhbas = vhba->private;
69 free(vhbas->disk, M_DEVBUF);
73 vhba_kick(vhba_softc_t *vhba)
75 vhbasimple_t *vhbas = vhba->private;
76 taskqueue_enqueue(taskqueue_swi, &vhbas->qt);
80 vhba_task(void *arg, int pending)
82 vhbasimple_t *vhbas = arg;
85 mtx_lock(&vhbas->vhba->lock);
86 while ((ccbh = TAILQ_FIRST(&vhbas->vhba->actv)) != NULL) {
87 TAILQ_REMOVE(&vhbas->vhba->actv, ccbh, sim_links.tqe);
88 vhbasimple_act(vhbas, (struct ccb_scsiio *)ccbh);
90 while ((ccbh = TAILQ_FIRST(&vhbas->vhba->done)) != NULL) {
91 TAILQ_REMOVE(&vhbas->vhba->done, ccbh, sim_links.tqe);
92 xpt_done((union ccb *)ccbh);
94 mtx_unlock(&vhbas->vhba->lock);
98 vhbasimple_act(vhbasimple_t *vhbas, struct ccb_scsiio *csio)
101 uint8_t *cdb, *ptr, status;
106 status = SCSI_STATUS_OK;
108 memset(&csio->sense_data, 0, sizeof (csio->sense_data));
109 cdb = csio->cdb_io.cdb_bytes;
111 if (csio->ccb_h.target_id >= MAX_TGT) {
112 csio->ccb_h.status = CAM_SEL_TIMEOUT;
113 TAILQ_INSERT_TAIL(&vhbas->vhba->done, &csio->ccb_h, sim_links.tqe);
116 if (csio->ccb_h.target_lun >= MAX_LUN && cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
117 vhba_fill_sense(csio, SSD_KEY_ILLEGAL_REQUEST, 0x25, 0x0);
118 TAILQ_INSERT_TAIL(&vhbas->vhba->done, &csio->ccb_h, sim_links.tqe);
127 uint8_t page = cdb[2] & SMS_PAGE_CODE;
128 uint8_t pgctl = cdb[2] & SMS_PAGE_CTRL_MASK;
131 case SMS_FORMAT_DEVICE_PAGE:
132 case SMS_GEOMETRY_PAGE:
134 case SMS_CONTROL_MODE_PAGE:
135 case SMS_ALL_PAGES_PAGE:
138 vhba_fill_sense(csio, SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x0);
139 TAILQ_INSERT_TAIL(&vhbas->vhba->done, &csio->ccb_h, sim_links.tqe);
142 memset(junk, 0, sizeof (junk));
143 if (cdb[1] & SMS_DBD) {
148 ptr[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
149 ptr[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
150 ptr[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
151 ptr[7] = ((1 << DISK_SHIFT)) & 0xff;
153 ptr[8] = (DISK_NBLKS >> 24) & 0xff;
154 ptr[9] = (DISK_NBLKS >> 16) & 0xff;
155 ptr[10] = (DISK_NBLKS >> 8) & 0xff;
156 ptr[11] = DISK_NBLKS & 0xff;
160 if (page == SMS_ALL_PAGES_PAGE || page == SMS_FORMAT_DEVICE_PAGE) {
161 ptr[0] = SMS_FORMAT_DEVICE_PAGE;
163 if (pgctl != SMS_PAGE_CTRL_CHANGEABLE) {
164 /* tracks per zone */
167 /* alternate sectors per zone */
170 /* alternate tracks per zone */
173 /* alternate tracks per logical unit */
176 /* sectors per track */
177 ptr[10] = (PSEUDO_SPT >> 8) & 0xff;
178 ptr[11] = PSEUDO_SPT & 0xff;
179 /* data bytes per physical sector */
180 ptr[12] = ((1 << DISK_SHIFT) >> 8) & 0xff;
181 ptr[13] = (1 << DISK_SHIFT) & 0xff;
185 /* track skew factor */
188 /* cylinder skew factor */
191 /* SSRC, HSEC, RMB, SURF */
196 if (page == SMS_ALL_PAGES_PAGE || page == SMS_GEOMETRY_PAGE) {
197 ptr[0] = SMS_GEOMETRY_PAGE;
199 if (pgctl != SMS_PAGE_CTRL_CHANGEABLE) {
200 uint32_t cyl = (DISK_NBLKS + ((PSEUDO_SPC - 1))) / PSEUDO_SPC;
201 /* number of cylinders */
202 ptr[2] = (cyl >> 24) & 0xff;
203 ptr[3] = (cyl >> 16) & 0xff;
205 /* number of heads */
207 /* starting cylinder- write precompensation */
211 /* starting cylinder- reduced write current */
215 /* drive step rate */
218 /* landing zone cylinder */
224 /* rotational offset */
226 /* medium rotation rate - 7200 RPM */
233 if (page == SMS_ALL_PAGES_PAGE || page == SMS_CACHE_PAGE) {
234 ptr[0] = SMS_CACHE_PAGE;
240 if (page == SMS_ALL_PAGES_PAGE || page == SMS_CONTROL_MODE_PAGE) {
241 ptr[0] = SMS_CONTROL_MODE_PAGE;
243 if (pgctl != SMS_PAGE_CTRL_CHANGEABLE) {
244 ptr[3] = 1 << 4; /* unrestricted reordering allowed */
245 ptr[8] = 0x75; /* 30000 ms */
250 nbyte = (char *)ptr - &junk[0];
253 if (cdb[0] == MODE_SENSE) {
254 data_len = min(cdb[4], csio->dxfer_len);
256 uint16_t tw = (cdb[7] << 8) | cdb[8];
257 data_len = min(tw, csio->dxfer_len);
259 data_len = min(data_len, nbyte);
261 memcpy(csio->data_ptr, junk, data_len);
263 csio->resid = csio->dxfer_len - data_len;
274 if (vhba_rwparm(cdb, &off, &data_len, DISK_NBLKS, DISK_SHIFT)) {
275 vhba_fill_sense(csio, SSD_KEY_ILLEGAL_REQUEST, 0x24, 0x0);
279 if ((cdb[0] & 0xf) == 8) {
280 memcpy(csio->data_ptr, &vhbas->disk[off], data_len);
282 memcpy(&vhbas->disk[off], csio->data_ptr, data_len);
284 csio->resid = csio->dxfer_len - data_len;
286 csio->resid = csio->dxfer_len;
292 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
293 vhba_fill_sense(csio, SSD_KEY_UNIT_ATTENTION, 0x24, 0x0);
296 if (cdb[8] & 0x1) { /* PMI */
297 csio->data_ptr[0] = 0xff;
298 csio->data_ptr[1] = 0xff;
299 csio->data_ptr[2] = 0xff;
300 csio->data_ptr[3] = 0xff;
302 uint64_t last_blk = DISK_NBLKS - 1;
303 if (last_blk < 0xffffffffULL) {
304 csio->data_ptr[0] = (last_blk >> 24) & 0xff;
305 csio->data_ptr[1] = (last_blk >> 16) & 0xff;
306 csio->data_ptr[2] = (last_blk >> 8) & 0xff;
307 csio->data_ptr[3] = (last_blk) & 0xff;
309 csio->data_ptr[0] = 0xff;
310 csio->data_ptr[1] = 0xff;
311 csio->data_ptr[2] = 0xff;
312 csio->data_ptr[3] = 0xff;
315 csio->data_ptr[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
316 csio->data_ptr[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
317 csio->data_ptr[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
318 csio->data_ptr[7] = ((1 << DISK_SHIFT)) & 0xff;
322 vhba_default_cmd(csio, MAX_LUN, NULL);
325 csio->ccb_h.status &= ~CAM_STATUS_MASK;
326 if (csio->scsi_status != SCSI_STATUS_OK) {
327 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
328 if (csio->scsi_status == SCSI_STATUS_CHECK_COND) {
329 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
332 csio->scsi_status = SCSI_STATUS_OK;
333 csio->ccb_h.status |= CAM_REQ_CMP;
335 TAILQ_INSERT_TAIL(&vhbas->vhba->done, &csio->ccb_h, sim_links.tqe);
337 DEV_MODULE(vhba_simple, vhba_modprobe, NULL);