The cam_sim structure was being deallocated unconditionally by device
[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.4 2004/03/15 03:05:11 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 <bus/cam/cam.h>
40 #include <bus/cam/cam_ccb.h>
41 #include <bus/cam/cam_periph.h>
42 #include <bus/cam/cam_sim.h>
43 #include <bus/cam/cam_xpt_sim.h>
44 #include <bus/cam/scsi/scsi_all.h>
45 #include <bus/cam/scsi/scsi_message.h>
46
47 #include <machine/resource.h>
48 #include <machine/bus.h>
49
50 #include "mlyreg.h"
51 #include "mlyio.h"
52 #include "mlyvar.h"
53 #include "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             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             mly_printf(sc, "CAM SIM attach failed\n");
175             return(ENOMEM);
176         }
177     }
178     cam_simq_release(devq);
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;
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; 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", chn);
219             cam_sim_free(sc->mly_cam_sim[chn]);
220         }
221     }
222 }
223
224 /********************************************************************************
225  * Handle an action requested by CAM
226  */
227 static void
228 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
229 {
230     struct mly_softc    *sc = cam_sim_softc(sim);
231
232     debug_called(2);
233
234     switch (ccb->ccb_h.func_code) {
235
236         /* perform SCSI I/O */
237     case XPT_SCSI_IO:
238     {
239         struct ccb_scsiio       *csio = &ccb->csio;
240         int                     bus, target;
241
242         bus = cam_sim_bus(sim);
243         target = csio->ccb_h.target_id;
244
245         debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
246
247         /*  check for I/O attempt to a protected device */
248         if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
249             debug(2, "  device protected");
250             csio->ccb_h.status = CAM_REQ_CMP_ERR;
251         }
252
253         /* check for I/O attempt to nonexistent device */
254         if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
255             debug(2, "  device does not exist");
256             csio->ccb_h.status = CAM_REQ_CMP_ERR;
257         }
258
259         /* XXX increase if/when we support large SCSI commands */
260         if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
261             debug(2, "  command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
262             csio->ccb_h.status = CAM_REQ_CMP_ERR;
263         }
264
265         /* check that the CDB pointer is not to a physical address */
266         if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
267             debug(2, "  CDB pointer is to physical address");
268             csio->ccb_h.status = CAM_REQ_CMP_ERR;
269         }
270
271         /* if there is data transfer, it must be to/from a virtual address */
272         if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
273             if (csio->ccb_h.flags & CAM_DATA_PHYS) {            /* we can't map it */
274                 debug(2, "  data pointer is to physical address");
275                 csio->ccb_h.status = CAM_REQ_CMP_ERR;
276             }
277             if (csio->ccb_h.flags & CAM_SCATTER_VALID) {        /* we want to do the s/g setup */
278                 debug(2, "  data has premature s/g setup");
279                 csio->ccb_h.status = CAM_REQ_CMP_ERR;
280             }
281         }
282
283         /* abandon aborted ccbs or those that have failed validation */
284         if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
285             debug(2, "abandoning CCB due to abort/validation failure");
286             break;
287         }
288
289         /* save the channel number in the ccb */
290         csio->ccb_h.sim_priv.entries[0].field = bus;
291
292         /* enqueue the ccb and start I/O */
293         mly_enqueue_ccb(sc, ccb);
294         mly_startio(sc);
295         return;
296     }
297
298         /* perform geometry calculations */
299     case XPT_CALC_GEOMETRY:
300     {
301         struct ccb_calc_geometry        *ccg = &ccb->ccg;
302         u_int32_t                       secs_per_cylinder;
303
304         debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
305
306         if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
307             ccg->heads = 255;
308             ccg->secs_per_track = 63;
309         } else {                                /* MLY_BIOSGEOM_2G */
310             ccg->heads = 128;
311             ccg->secs_per_track = 32;
312         }
313         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
314         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
315         ccb->ccb_h.status = CAM_REQ_CMP;
316         break;
317     }
318
319         /* handle path attribute inquiry */
320     case XPT_PATH_INQ:
321     {
322         struct ccb_pathinq      *cpi = &ccb->cpi;
323
324         debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
325
326         cpi->version_num = 1;
327         cpi->hba_inquiry = PI_TAG_ABLE;         /* XXX extra flags for physical channels? */
328         cpi->target_sprt = 0;
329         cpi->hba_misc = 0;
330         cpi->max_target = MLY_MAX_TARGETS - 1;
331         cpi->max_lun = MLY_MAX_LUNS - 1;
332         cpi->initiator_id = sc->mly_controllerparam->initiator_id;
333         strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
334         strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
335         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
336         cpi->unit_number = cam_sim_unit(sim);
337         cpi->bus_id = cam_sim_bus(sim);
338         cpi->base_transfer_speed = 132 * 1024;  /* XXX what to set this to? */
339         ccb->ccb_h.status = CAM_REQ_CMP;
340         break;
341     }
342
343     case XPT_GET_TRAN_SETTINGS:
344     {
345         struct ccb_trans_settings       *cts = &ccb->cts;
346         int                             bus, target;
347
348         bus = cam_sim_bus(sim);
349         target = cts->ccb_h.target_id;
350
351         debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
352         cts->valid = 0;
353
354         /* logical device? */
355         if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
356             /* nothing special for these */
357
358         /* physical device? */
359         } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
360             /* allow CAM to try tagged transactions */
361             cts->flags |= CCB_TRANS_TAG_ENB;
362             cts->valid |= CCB_TRANS_TQ_VALID;
363
364             /* convert speed (MHz) to usec */
365             if (sc->mly_btl[bus][target].mb_speed == 0) {
366                 cts->sync_period = 1000000 / 5;
367             } else {
368                 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
369             }
370
371             /* convert bus width to CAM internal encoding */
372             switch (sc->mly_btl[bus][target].mb_width) {
373             case 32:
374                 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
375                 break;
376             case 16:
377                 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
378                 break;
379             case 8:
380             default:
381                 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
382                 break;
383             }
384             cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
385
386             /* not a device, bail out */
387         } else {
388             cts->ccb_h.status = CAM_REQ_CMP_ERR;
389             break;
390         }
391
392         /* disconnect always OK */
393         cts->flags |= CCB_TRANS_DISC_ENB;
394         cts->valid |= CCB_TRANS_DISC_VALID;
395
396         cts->ccb_h.status = CAM_REQ_CMP;
397         break;
398     }
399
400     default:            /* we can't do this */
401         debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
402         ccb->ccb_h.status = CAM_REQ_INVALID;
403         break;
404     }
405
406     xpt_done(ccb);
407 }
408
409 /********************************************************************************
410  * Check for possibly-completed commands.
411  */
412 static void
413 mly_cam_poll(struct cam_sim *sim)
414 {
415     struct mly_softc    *sc = cam_sim_softc(sim);
416
417     debug_called(2);
418
419     mly_done(sc);
420 }
421
422 /********************************************************************************
423  * Pull a CCB off the work queue and turn it into a command.
424  */
425 int
426 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
427 {
428     struct mly_command                  *mc;
429     struct mly_command_scsi_small       *ss;
430     struct ccb_scsiio                   *csio;
431     int                                 error;
432
433     debug_called(2);
434
435     error = 0;
436     mc = NULL;
437     csio = NULL;
438
439     /* check for a CCB */
440     if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
441         goto out;
442
443     /* get a command to back it */
444     if (mly_alloc_command(sc, &mc)) {
445         error = ENOMEM;
446         goto out;
447     }
448
449     /* build the command */
450     mc->mc_data = csio->data_ptr;
451     mc->mc_length = csio->dxfer_len;
452     mc->mc_complete = mly_cam_complete;
453     mc->mc_private = csio;
454
455     /* build the packet for the controller */
456     ss = &mc->mc_packet->scsi_small;
457     ss->opcode = MDACMD_SCSI;
458     if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
459         ss->command_control.disable_disconnect = 1;
460     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
461         ss->command_control.data_direction = MLY_CCB_WRITE;
462     ss->data_size = csio->dxfer_len;
463     ss->addr.phys.lun = csio->ccb_h.target_lun;
464     ss->addr.phys.target = csio->ccb_h.target_id;
465     ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
466     if (csio->ccb_h.timeout < (60 * 1000)) {
467         ss->timeout.value = csio->ccb_h.timeout / 1000;
468         ss->timeout.scale = MLY_TIMEOUT_SECONDS;
469     } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
470         ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
471         ss->timeout.scale = MLY_TIMEOUT_MINUTES;
472     } else {
473         ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000);     /* overflow? */
474         ss->timeout.scale = MLY_TIMEOUT_HOURS;
475     }
476     ss->maximum_sense_size = csio->sense_len;
477     ss->cdb_length = csio->cdb_len;
478     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
479         bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
480     } else {
481         bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
482     }
483
484 out:
485     if (error != 0) {
486         if (mc != NULL) {
487             mly_release_command(mc);
488             mc = NULL;
489         }
490         if (csio != NULL)
491             mly_requeue_ccb(sc, (union ccb *)csio);
492     }
493     *mcp = mc;
494     return(error);
495 }
496
497 /********************************************************************************
498  * Handle completion of a command - pass results back through the CCB
499  */
500 static void
501 mly_cam_complete(struct mly_command *mc)
502 {
503     struct mly_softc            *sc = mc->mc_sc;
504     struct ccb_scsiio           *csio = (struct ccb_scsiio *)mc->mc_private;
505     struct scsi_inquiry_data    *inq = (struct scsi_inquiry_data *)csio->data_ptr;
506     struct mly_btl              *btl;
507     u_int8_t                    cmd;
508     int                         bus, target;
509
510     debug_called(2);
511
512     csio->scsi_status = mc->mc_status;
513     switch(mc->mc_status) {
514     case SCSI_STATUS_OK:
515         /*
516          * In order to report logical device type and status, we overwrite
517          * the result of the INQUIRY command to logical devices.
518          */
519         bus = csio->ccb_h.sim_priv.entries[0].field;
520         if (bus >= sc->mly_controllerinfo->physical_channels_present) {
521             if (csio->ccb_h.flags & CAM_CDB_POINTER) {
522                 cmd = *csio->cdb_io.cdb_ptr;
523             } else {
524                 cmd = csio->cdb_io.cdb_bytes[0];
525             }
526             if (cmd == INQUIRY) {
527                 target = csio->ccb_h.target_id;
528                 btl = &sc->mly_btl[bus][target];
529                 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
530                 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
531                 padstr(inq->revision, "", 4);
532             }
533         }
534
535         debug(2, "SCSI_STATUS_OK");
536         csio->ccb_h.status = CAM_REQ_CMP;
537         break;
538
539     case SCSI_STATUS_CHECK_COND:
540         debug(2, "SCSI_STATUS_CHECK_COND  sense %d  resid %d", mc->mc_sense, mc->mc_resid);
541         csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
542         bzero(&csio->sense_data, SSD_FULL_SIZE);
543         bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
544         csio->sense_len = mc->mc_sense;
545         csio->ccb_h.status |= CAM_AUTOSNS_VALID;
546         csio->resid = mc->mc_resid;     /* XXX this is a signed value... */
547         break;
548
549     case SCSI_STATUS_BUSY:
550         debug(2, "SCSI_STATUS_BUSY");
551         csio->ccb_h.status = CAM_SCSI_BUSY;
552         break;
553
554     default:
555         debug(2, "unknown status 0x%x", csio->scsi_status);
556         csio->ccb_h.status = CAM_REQ_CMP_ERR;
557         break;
558     }
559     xpt_done((union ccb *)csio);
560     mly_release_command(mc);
561 }
562
563 /********************************************************************************
564  * Find a peripheral attahed at (bus),(target)
565  */
566 static struct cam_periph *
567 mly_find_periph(struct mly_softc *sc, int bus, int target)
568 {
569     struct cam_periph   *periph;
570     struct cam_path     *path;
571     int                 status;
572
573     status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
574     if (status == CAM_REQ_CMP) {
575         periph = cam_periph_find(path, NULL);
576         xpt_free_path(path);
577     } else {
578         periph = NULL;
579     }
580     return(periph);
581 }
582
583 /********************************************************************************
584  * Name the device at (bus)(target)
585  */
586 int
587 mly_name_device(struct mly_softc *sc, int bus, int target)
588 {
589     struct cam_periph   *periph;
590
591     if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
592         sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
593         return(0);
594     }
595     sc->mly_btl[bus][target].mb_name[0] = 0;
596     return(ENOENT);
597 }