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