Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / raid / mly / mly_cam.c
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *      $FreeBSD: src/sys/dev/mly/mly_cam.c,v 1.1.2.3 2001/04/21 04:09:06 msmith Exp $
28  *      $DragonFly: src/sys/dev/raid/mly/Attic/mly_cam.c,v 1.2 2003/06/17 04:28:28 dillon Exp $
29  */
30 /*
31  * CAM interface for FreeBSD
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/devicestat.h>
38
39 #include <cam/cam.h>
40 #include <cam/cam_ccb.h>
41 #include <cam/cam_periph.h>
42 #include <cam/cam_sim.h>
43 #include <cam/cam_xpt_sim.h>
44 #include <cam/scsi/scsi_all.h>
45 #include <cam/scsi/scsi_message.h>
46
47 #include <machine/resource.h>
48 #include <machine/bus.h>
49
50 #include <dev/mly/mlyreg.h>
51 #include <dev/mly/mlyio.h>
52 #include <dev/mly/mlyvar.h>
53 #include <dev/mly/mly_tables.h>
54
55 static void                     mly_cam_poll(struct cam_sim *sim);
56 static void                     mly_cam_action(struct cam_sim *sim, union ccb *ccb);
57 static void                     mly_cam_complete(struct mly_command *mc);
58 static struct cam_periph        *mly_find_periph(struct mly_softc *sc, int bus, int target);
59
60 /********************************************************************************
61  * CAM-specific queue primitives
62  */
63 static __inline void
64 mly_initq_ccb(struct mly_softc *sc)
65 {
66     TAILQ_INIT(&sc->mly_cam_ccbq);
67     MLYQ_INIT(sc, MLYQ_CCB);
68 }
69
70 static __inline void
71 mly_enqueue_ccb(struct mly_softc *sc, union ccb *ccb)
72 {
73     int         s;
74
75     s = splcam();
76     TAILQ_INSERT_TAIL(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
77     MLYQ_ADD(sc, MLYQ_CCB);
78     splx(s);
79 }
80
81 static __inline void
82 mly_requeue_ccb(struct mly_softc *sc, union ccb *ccb)
83 {
84     int         s;
85
86     s = splcam();
87     TAILQ_INSERT_HEAD(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
88     MLYQ_ADD(sc, MLYQ_CCB);
89     splx(s);
90 }
91
92 static __inline union ccb *
93 mly_dequeue_ccb(struct mly_softc *sc)
94 {
95     union ccb   *ccb;
96     int         s;
97
98     s = splcam();
99     if ((ccb = (union ccb *)TAILQ_FIRST(&sc->mly_cam_ccbq)) != NULL) {
100         TAILQ_REMOVE(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
101         MLYQ_REMOVE(sc, MLYQ_CCB);
102     }
103     splx(s);
104     return(ccb);
105 }
106
107 /********************************************************************************
108  * space-fill a character string
109  */
110 static __inline void
111 padstr(char *targ, char *src, int len)
112 {
113     while (len-- > 0) {
114         if (*src != 0) {
115             *targ++ = *src++;
116         } else {
117             *targ++ = ' ';
118         }
119     }
120 }
121
122 /********************************************************************************
123  * Attach the real and virtual SCSI busses to CAM
124  */
125 int
126 mly_cam_attach(struct mly_softc *sc)
127 {
128     struct cam_devq     *devq;
129     int                 chn, i;
130
131     debug_called(1);
132
133     /* initialise the CCB queue */
134     mly_initq_ccb(sc);
135
136     /*
137      * Allocate a devq for all our channels combined.
138      */
139     if ((devq = cam_simq_alloc(sc->mly_controllerinfo->maximum_parallel_commands)) == NULL) {
140         mly_printf(sc, "can't allocate CAM SIM\n");
141         return(ENOMEM);
142     }
143
144     /*
145      * Iterate over channels, registering them with CAM.
146      *
147      * Physical channels are set up to support tagged commands and only a single
148      * untagged command.  Virtual channels do not support tags, and don't need them.
149      */
150     for (i = 0, chn = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {
151         /* allocate a sim */
152         if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action, 
153                                                   mly_cam_poll, 
154                                                   "mly", 
155                                                   sc,
156                                                   device_get_unit(sc->mly_dev), 
157                                                   1,
158                                                   sc->mly_controllerinfo->maximum_parallel_commands,
159                                                   devq)) ==  NULL) {
160             cam_simq_free(devq);
161             mly_printf(sc, "CAM SIM attach failed\n");
162             return(ENOMEM);
163         }
164     }
165     for (i = 0; i < sc->mly_controllerinfo->virtual_channels_present; i++, chn++) {
166         /* allocate a sim */
167         if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action, 
168                                                   mly_cam_poll, 
169                                                   "mly", 
170                                                   sc,
171                                                   device_get_unit(sc->mly_dev), 
172                                                   sc->mly_controllerinfo->maximum_parallel_commands,
173                                                   0,
174                                                   devq)) ==  NULL) {
175             cam_simq_free(devq);
176             mly_printf(sc, "CAM SIM attach failed\n");
177             return(ENOMEM);
178         }
179     }
180
181     for (i = 0; i < chn; i++) {
182         /* register the bus IDs so we can get them later */
183         if (xpt_bus_register(sc->mly_cam_sim[i], i)) {
184             mly_printf(sc, "CAM XPT bus registration failed\n");
185             return(ENXIO);
186         }
187         debug(1, "registered sim %p bus %d", sc->mly_cam_sim[i], i);
188     }
189
190     return(0);
191 }
192
193 /********************************************************************************
194  * Detach from CAM
195  */
196 void
197 mly_cam_detach(struct mly_softc *sc)
198 {
199     int         chn, nchn, first;
200
201     debug_called(1);
202
203     nchn = sc->mly_controllerinfo->physical_channels_present +
204         sc->mly_controllerinfo->virtual_channels_present;
205
206     /*
207      * Iterate over channels, deregistering as we go.
208      */
209     nchn = sc->mly_controllerinfo->physical_channels_present +
210         sc->mly_controllerinfo->virtual_channels_present;
211     for (chn = 0, first = 1; chn < nchn; chn++) {
212
213         /*
214          * If a sim was registered for this channel, free it.
215          */
216         if (sc->mly_cam_sim[chn] != NULL) {
217             debug(1, "deregister bus %d", chn);
218             xpt_bus_deregister(cam_sim_path(sc->mly_cam_sim[chn]));
219             debug(1, "free sim for channel %d (%sfree queue)", chn, first ? "" : "don't ");
220             cam_sim_free(sc->mly_cam_sim[chn], first ? TRUE : FALSE);
221             first = 0;
222         }
223     }
224 }
225
226 /********************************************************************************
227  * Handle an action requested by CAM
228  */
229 static void
230 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
231 {
232     struct mly_softc    *sc = cam_sim_softc(sim);
233
234     debug_called(2);
235
236     switch (ccb->ccb_h.func_code) {
237
238         /* perform SCSI I/O */
239     case XPT_SCSI_IO:
240     {
241         struct ccb_scsiio       *csio = &ccb->csio;
242         int                     bus, target;
243
244         bus = cam_sim_bus(sim);
245         target = csio->ccb_h.target_id;
246
247         debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
248
249         /*  check for I/O attempt to a protected device */
250         if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
251             debug(2, "  device protected");
252             csio->ccb_h.status = CAM_REQ_CMP_ERR;
253         }
254
255         /* check for I/O attempt to nonexistent device */
256         if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
257             debug(2, "  device does not exist");
258             csio->ccb_h.status = CAM_REQ_CMP_ERR;
259         }
260
261         /* XXX increase if/when we support large SCSI commands */
262         if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
263             debug(2, "  command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
264             csio->ccb_h.status = CAM_REQ_CMP_ERR;
265         }
266
267         /* check that the CDB pointer is not to a physical address */
268         if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
269             debug(2, "  CDB pointer is to physical address");
270             csio->ccb_h.status = CAM_REQ_CMP_ERR;
271         }
272
273         /* if there is data transfer, it must be to/from a virtual address */
274         if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
275             if (csio->ccb_h.flags & CAM_DATA_PHYS) {            /* we can't map it */
276                 debug(2, "  data pointer is to physical address");
277                 csio->ccb_h.status = CAM_REQ_CMP_ERR;
278             }
279             if (csio->ccb_h.flags & CAM_SCATTER_VALID) {        /* we want to do the s/g setup */
280                 debug(2, "  data has premature s/g setup");
281                 csio->ccb_h.status = CAM_REQ_CMP_ERR;
282             }
283         }
284
285         /* abandon aborted ccbs or those that have failed validation */
286         if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
287             debug(2, "abandoning CCB due to abort/validation failure");
288             break;
289         }
290
291         /* save the channel number in the ccb */
292         csio->ccb_h.sim_priv.entries[0].field = bus;
293
294         /* enqueue the ccb and start I/O */
295         mly_enqueue_ccb(sc, ccb);
296         mly_startio(sc);
297         return;
298     }
299
300         /* perform geometry calculations */
301     case XPT_CALC_GEOMETRY:
302     {
303         struct ccb_calc_geometry        *ccg = &ccb->ccg;
304         u_int32_t                       secs_per_cylinder;
305
306         debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
307
308         if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
309             ccg->heads = 255;
310             ccg->secs_per_track = 63;
311         } else {                                /* MLY_BIOSGEOM_2G */
312             ccg->heads = 128;
313             ccg->secs_per_track = 32;
314         }
315         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
316         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
317         ccb->ccb_h.status = CAM_REQ_CMP;
318         break;
319     }
320
321         /* handle path attribute inquiry */
322     case XPT_PATH_INQ:
323     {
324         struct ccb_pathinq      *cpi = &ccb->cpi;
325
326         debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
327
328         cpi->version_num = 1;
329         cpi->hba_inquiry = PI_TAG_ABLE;         /* XXX extra flags for physical channels? */
330         cpi->target_sprt = 0;
331         cpi->hba_misc = 0;
332         cpi->max_target = MLY_MAX_TARGETS - 1;
333         cpi->max_lun = MLY_MAX_LUNS - 1;
334         cpi->initiator_id = sc->mly_controllerparam->initiator_id;
335         strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
336         strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
337         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
338         cpi->unit_number = cam_sim_unit(sim);
339         cpi->bus_id = cam_sim_bus(sim);
340         cpi->base_transfer_speed = 132 * 1024;  /* XXX what to set this to? */
341         ccb->ccb_h.status = CAM_REQ_CMP;
342         break;
343     }
344
345     case XPT_GET_TRAN_SETTINGS:
346     {
347         struct ccb_trans_settings       *cts = &ccb->cts;
348         int                             bus, target;
349
350         bus = cam_sim_bus(sim);
351         target = cts->ccb_h.target_id;
352
353         debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
354         cts->valid = 0;
355
356         /* logical device? */
357         if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
358             /* nothing special for these */
359
360         /* physical device? */
361         } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
362             /* allow CAM to try tagged transactions */
363             cts->flags |= CCB_TRANS_TAG_ENB;
364             cts->valid |= CCB_TRANS_TQ_VALID;
365
366             /* convert speed (MHz) to usec */
367             if (sc->mly_btl[bus][target].mb_speed == 0) {
368                 cts->sync_period = 1000000 / 5;
369             } else {
370                 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
371             }
372
373             /* convert bus width to CAM internal encoding */
374             switch (sc->mly_btl[bus][target].mb_width) {
375             case 32:
376                 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
377                 break;
378             case 16:
379                 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
380                 break;
381             case 8:
382             default:
383                 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
384                 break;
385             }
386             cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
387
388             /* not a device, bail out */
389         } else {
390             cts->ccb_h.status = CAM_REQ_CMP_ERR;
391             break;
392         }
393
394         /* disconnect always OK */
395         cts->flags |= CCB_TRANS_DISC_ENB;
396         cts->valid |= CCB_TRANS_DISC_VALID;
397
398         cts->ccb_h.status = CAM_REQ_CMP;
399         break;
400     }
401
402     default:            /* we can't do this */
403         debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
404         ccb->ccb_h.status = CAM_REQ_INVALID;
405         break;
406     }
407
408     xpt_done(ccb);
409 }
410
411 /********************************************************************************
412  * Check for possibly-completed commands.
413  */
414 static void
415 mly_cam_poll(struct cam_sim *sim)
416 {
417     struct mly_softc    *sc = cam_sim_softc(sim);
418
419     debug_called(2);
420
421     mly_done(sc);
422 }
423
424 /********************************************************************************
425  * Pull a CCB off the work queue and turn it into a command.
426  */
427 int
428 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
429 {
430     struct mly_command                  *mc;
431     struct mly_command_scsi_small       *ss;
432     struct ccb_scsiio                   *csio;
433     int                                 error;
434
435     debug_called(2);
436
437     error = 0;
438     mc = NULL;
439     csio = NULL;
440
441     /* check for a CCB */
442     if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
443         goto out;
444
445     /* get a command to back it */
446     if (mly_alloc_command(sc, &mc)) {
447         error = ENOMEM;
448         goto out;
449     }
450
451     /* build the command */
452     mc->mc_data = csio->data_ptr;
453     mc->mc_length = csio->dxfer_len;
454     mc->mc_complete = mly_cam_complete;
455     mc->mc_private = csio;
456
457     /* build the packet for the controller */
458     ss = &mc->mc_packet->scsi_small;
459     ss->opcode = MDACMD_SCSI;
460     if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
461         ss->command_control.disable_disconnect = 1;
462     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
463         ss->command_control.data_direction = MLY_CCB_WRITE;
464     ss->data_size = csio->dxfer_len;
465     ss->addr.phys.lun = csio->ccb_h.target_lun;
466     ss->addr.phys.target = csio->ccb_h.target_id;
467     ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
468     if (csio->ccb_h.timeout < (60 * 1000)) {
469         ss->timeout.value = csio->ccb_h.timeout / 1000;
470         ss->timeout.scale = MLY_TIMEOUT_SECONDS;
471     } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
472         ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
473         ss->timeout.scale = MLY_TIMEOUT_MINUTES;
474     } else {
475         ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000);     /* overflow? */
476         ss->timeout.scale = MLY_TIMEOUT_HOURS;
477     }
478     ss->maximum_sense_size = csio->sense_len;
479     ss->cdb_length = csio->cdb_len;
480     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
481         bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
482     } else {
483         bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
484     }
485
486 out:
487     if (error != 0) {
488         if (mc != NULL) {
489             mly_release_command(mc);
490             mc = NULL;
491         }
492         if (csio != NULL)
493             mly_requeue_ccb(sc, (union ccb *)csio);
494     }
495     *mcp = mc;
496     return(error);
497 }
498
499 /********************************************************************************
500  * Handle completion of a command - pass results back through the CCB
501  */
502 static void
503 mly_cam_complete(struct mly_command *mc)
504 {
505     struct mly_softc            *sc = mc->mc_sc;
506     struct ccb_scsiio           *csio = (struct ccb_scsiio *)mc->mc_private;
507     struct scsi_inquiry_data    *inq = (struct scsi_inquiry_data *)csio->data_ptr;
508     struct mly_btl              *btl;
509     u_int8_t                    cmd;
510     int                         bus, target;
511
512     debug_called(2);
513
514     csio->scsi_status = mc->mc_status;
515     switch(mc->mc_status) {
516     case SCSI_STATUS_OK:
517         /*
518          * In order to report logical device type and status, we overwrite
519          * the result of the INQUIRY command to logical devices.
520          */
521         bus = csio->ccb_h.sim_priv.entries[0].field;
522         if (bus >= sc->mly_controllerinfo->physical_channels_present) {
523             if (csio->ccb_h.flags & CAM_CDB_POINTER) {
524                 cmd = *csio->cdb_io.cdb_ptr;
525             } else {
526                 cmd = csio->cdb_io.cdb_bytes[0];
527             }
528             if (cmd == INQUIRY) {
529                 target = csio->ccb_h.target_id;
530                 btl = &sc->mly_btl[bus][target];
531                 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
532                 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
533                 padstr(inq->revision, "", 4);
534             }
535         }
536
537         debug(2, "SCSI_STATUS_OK");
538         csio->ccb_h.status = CAM_REQ_CMP;
539         break;
540
541     case SCSI_STATUS_CHECK_COND:
542         debug(2, "SCSI_STATUS_CHECK_COND  sense %d  resid %d", mc->mc_sense, mc->mc_resid);
543         csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
544         bzero(&csio->sense_data, SSD_FULL_SIZE);
545         bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
546         csio->sense_len = mc->mc_sense;
547         csio->ccb_h.status |= CAM_AUTOSNS_VALID;
548         csio->resid = mc->mc_resid;     /* XXX this is a signed value... */
549         break;
550
551     case SCSI_STATUS_BUSY:
552         debug(2, "SCSI_STATUS_BUSY");
553         csio->ccb_h.status = CAM_SCSI_BUSY;
554         break;
555
556     default:
557         debug(2, "unknown status 0x%x", csio->scsi_status);
558         csio->ccb_h.status = CAM_REQ_CMP_ERR;
559         break;
560     }
561     xpt_done((union ccb *)csio);
562     mly_release_command(mc);
563 }
564
565 /********************************************************************************
566  * Find a peripheral attahed at (bus),(target)
567  */
568 static struct cam_periph *
569 mly_find_periph(struct mly_softc *sc, int bus, int target)
570 {
571     struct cam_periph   *periph;
572     struct cam_path     *path;
573     int                 status;
574
575     status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
576     if (status == CAM_REQ_CMP) {
577         periph = cam_periph_find(path, NULL);
578         xpt_free_path(path);
579     } else {
580         periph = NULL;
581     }
582     return(periph);
583 }
584
585 /********************************************************************************
586  * Name the device at (bus)(target)
587  */
588 int
589 mly_name_device(struct mly_softc *sc, int bus, int target)
590 {
591     struct cam_periph   *periph;
592
593     if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
594         sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
595         return(0);
596     }
597     sc->mly_btl[bus][target].mb_name[0] = 0;
598     return(ENOENT);
599 }