]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mfi/mfi_cam.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / sys / dev / mfi / mfi_cam.c
1 /*-
2  * Copyright 2007 Scott Long
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
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
18  * FOR 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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_mfi.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/selinfo.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/eventhandler.h>
41 #include <sys/rman.h>
42 #include <sys/bus_dma.h>
43 #include <sys/bio.h>
44 #include <sys/ioccom.h>
45 #include <sys/uio.h>
46 #include <sys/proc.h>
47 #include <sys/signalvar.h>
48
49 #include <cam/cam.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_debug.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt_sim.h>
54 #include <cam/scsi/scsi_all.h>
55 #include <cam/scsi/scsi_message.h>
56
57 #include <sys/bus.h>
58 #include <sys/conf.h>
59 #include <machine/md_var.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
62 #include <sys/rman.h>
63
64 #include <dev/mfi/mfireg.h>
65 #include <dev/mfi/mfi_ioctl.h>
66 #include <dev/mfi/mfivar.h>
67
68 struct mfip_softc {
69         device_t        dev;
70         struct mfi_softc *mfi_sc;
71         struct cam_devq *devq;
72         struct cam_sim  *sim;
73         struct cam_path *path;
74 };
75
76 static int      mfip_probe(device_t);
77 static int      mfip_attach(device_t);
78 static int      mfip_detach(device_t);
79 static void     mfip_cam_action(struct cam_sim *, union ccb *);
80 static void     mfip_cam_poll(struct cam_sim *);
81 static struct mfi_command * mfip_start(void *);
82 static void     mfip_done(struct mfi_command *cm);
83
84 static devclass_t       mfip_devclass;
85 static device_method_t  mfip_methods[] = {
86         DEVMETHOD(device_probe,         mfip_probe),
87         DEVMETHOD(device_attach,        mfip_attach),
88         DEVMETHOD(device_detach,        mfip_detach),
89         {0, 0}
90 };
91 static driver_t mfip_driver = {
92         "mfip",
93         mfip_methods,
94         sizeof(struct mfip_softc)
95 };
96 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, 0, 0);
97 MODULE_DEPEND(mfip, cam, 1, 1, 1);
98
99 #define ccb_mfip_ptr sim_priv.entries[0].ptr
100
101 static int
102 mfip_probe(device_t dev)
103 {
104
105         device_set_desc(dev, "SCSI Passthrough Bus");
106         return (0);
107 }
108
109 static int
110 mfip_attach(device_t dev)
111 {
112         struct mfip_softc *sc;
113         struct mfi_softc *mfisc;
114
115         sc = device_get_softc(dev);
116         if (sc == NULL)
117                 return (EINVAL);
118
119         mfisc = device_get_softc(device_get_parent(dev));
120         sc->dev = dev;
121         sc->mfi_sc = mfisc;
122         mfisc->mfi_cam_start = mfip_start;
123
124         if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
125                 return (ENOMEM);
126
127         sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
128                                 device_get_unit(dev), 1,
129                                 MFI_SCSI_MAX_CMDS, sc->devq);
130         if (sc->sim == NULL) {
131                 cam_simq_free(sc->devq);
132                 device_printf(dev, "CAM SIM attach failed\n");
133                 return (EINVAL);
134         }
135
136         if (xpt_bus_register(sc->sim, 0) != 0) {
137                 device_printf(dev, "XPT bus registration failed\n");
138                 cam_sim_free(sc->sim, FALSE);
139                 cam_simq_free(sc->devq);
140                 return (EINVAL);
141         }
142
143         return (0);
144 }
145
146 static int
147 mfip_detach(device_t dev)
148 {
149         struct mfip_softc *sc;
150
151         sc = device_get_softc(dev);
152         if (sc == NULL)
153                 return (EINVAL);
154
155         if (sc->sim != NULL) {
156                 xpt_bus_deregister(cam_sim_path(sc->sim));
157                 cam_sim_free(sc->sim, FALSE);
158         }
159
160         if (sc->devq != NULL)
161                 cam_simq_free(sc->devq);
162
163         return (0);
164 }
165
166 static void
167 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
168 {
169         struct mfip_softc *sc = cam_sim_softc(sim);
170         struct mfi_softc *mfisc = sc->mfi_sc;
171
172         switch (ccb->ccb_h.func_code) {
173         case XPT_PATH_INQ:
174         {
175                 struct ccb_pathinq *cpi = &ccb->cpi;
176
177                 cpi->version_num = 1;
178                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
179                 cpi->target_sprt = 0;
180                 cpi->hba_misc = PIM_NOBUSRESET;
181                 cpi->hba_eng_cnt = 0;
182                 cpi->max_target = MFI_SCSI_MAX_TARGETS;
183                 cpi->max_lun = MFI_SCSI_MAX_LUNS;
184                 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
185                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
186                 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
187                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
188                 cpi->unit_number = cam_sim_unit(sim);
189                 cpi->bus_id = cam_sim_bus(sim);
190                 cpi->base_transfer_speed = 150000;
191                 cpi->ccb_h.status = CAM_REQ_CMP;
192                 break;
193         }
194         case XPT_RESET_BUS:
195                 ccb->ccb_h.status = CAM_REQ_CMP;
196                 break;
197         case XPT_RESET_DEV:
198                 ccb->ccb_h.status = CAM_REQ_CMP;
199                 break;
200         case XPT_GET_TRAN_SETTINGS:
201         {
202                 ccb->cts.flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
203                 ccb->cts.valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
204                 ccb->ccb_h.status = CAM_REQ_CMP;
205                 break;
206         }
207         case XPT_SET_TRAN_SETTINGS:
208                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
209                 break;
210         case XPT_SCSI_IO:
211         {
212                 struct ccb_hdr          *ccbh = &ccb->ccb_h;
213                 struct ccb_scsiio       *csio = &ccb->csio;
214
215                 ccbh->status = CAM_REQ_INPROG;
216                 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
217                         ccbh->status = CAM_REQ_INVALID;
218                         break;
219                 }
220                 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
221                         if (ccbh->flags & CAM_DATA_PHYS) {
222                                 ccbh->status = CAM_REQ_INVALID;
223                                 break;
224                         }
225                         if (ccbh->flags & CAM_SCATTER_VALID) {
226                                 ccbh->status = CAM_REQ_INVALID;
227                                 break;
228                         }
229                 }
230
231                 ccbh->ccb_mfip_ptr = sc;
232                 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
233                 mfi_startio(mfisc);
234                 return;
235         }
236         default:
237                 ccb->ccb_h.status = CAM_REQ_INVALID;
238                 break;
239         }
240
241         xpt_done(ccb);
242         return;
243 }
244
245 static struct mfi_command *
246 mfip_start(void *data)
247 {
248         union ccb *ccb = data;
249         struct ccb_hdr *ccbh = &ccb->ccb_h;
250         struct ccb_scsiio *csio = &ccb->csio;
251         struct mfip_softc *sc;
252         struct mfi_pass_frame *pt;
253         struct mfi_command *cm;
254
255         sc = ccbh->ccb_mfip_ptr;
256
257         if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
258                 return (NULL);
259
260         pt = &cm->cm_frame->pass;
261         pt->header.cmd = MFI_CMD_PD_SCSI_IO;
262         pt->header.cmd_status = 0;
263         pt->header.scsi_status = 0;
264         pt->header.target_id = ccbh->target_id;
265         pt->header.lun_id = ccbh->target_lun;
266         pt->header.flags = 0;
267         pt->header.timeout = 0;
268         pt->header.data_len = csio->dxfer_len;
269         pt->header.sense_len = MFI_SENSE_LEN;
270         pt->header.cdb_len = csio->cdb_len;
271         pt->sense_addr_lo = cm->cm_sense_busaddr;
272         pt->sense_addr_hi = 0;
273         if (ccbh->flags & CAM_CDB_POINTER)
274                 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
275         else
276                 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
277         cm->cm_complete = mfip_done;
278         cm->cm_private = ccb;
279         cm->cm_sg = &pt->sgl;
280         cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
281         cm->cm_data = csio->data_ptr;
282         cm->cm_len = csio->dxfer_len;
283         switch (ccbh->flags & CAM_DIR_MASK) {
284         case CAM_DIR_IN:
285                 cm->cm_flags = MFI_CMD_DATAIN;
286                 break;
287         case CAM_DIR_OUT:
288                 cm->cm_flags = MFI_CMD_DATAOUT;
289                 break;
290         case CAM_DIR_NONE:
291         default:
292                 cm->cm_data = NULL;
293                 cm->cm_len = 0;
294                 cm->cm_flags = 0;
295                 break;
296         }
297
298         TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
299         return (cm);
300 }
301
302 static void
303 mfip_done(struct mfi_command *cm)
304 {
305         union ccb *ccb = cm->cm_private;
306         struct ccb_hdr *ccbh = &ccb->ccb_h;
307         struct ccb_scsiio *csio = &ccb->csio;
308         struct mfip_softc *sc;
309         struct mfi_pass_frame *pt;
310
311         sc = ccbh->ccb_mfip_ptr;
312         pt = &cm->cm_frame->pass;
313
314         switch (pt->header.cmd_status) {
315         case MFI_STAT_OK:
316         {
317                 uint8_t command, device;
318
319                 ccbh->status = CAM_REQ_CMP;
320                 csio->scsi_status = pt->header.scsi_status;
321                 if (ccbh->flags & CAM_CDB_POINTER)
322                         command = ccb->csio.cdb_io.cdb_ptr[0];
323                 else
324                         command = ccb->csio.cdb_io.cdb_bytes[0];
325                 if (command == INQUIRY) {
326                         device = ccb->csio.data_ptr[0] & 0x1f;
327                         if ((device == T_DIRECT) || (device == T_PROCESSOR))
328                                 csio->data_ptr[0] =
329                                      (device & 0xe0) | T_NODEVICE;
330                 }
331                 break;
332         }
333         case MFI_STAT_SCSI_DONE_WITH_ERROR:
334         {
335                 int sense_len;
336
337                 ccbh->status = CAM_REQ_CMP_ERR | CAM_AUTOSNS_VALID;
338                 csio->scsi_status = pt->header.scsi_status;
339                 sense_len = min(pt->header.sense_len, sizeof(struct scsi_sense_data));
340                 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
341                 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
342                 break;
343         }
344         case MFI_STAT_DEVICE_NOT_FOUND:
345                 ccbh->status = CAM_DEV_NOT_THERE;
346                 break;
347         case MFI_STAT_SCSI_IO_FAILED:
348                 ccbh->status = CAM_REQ_CMP_ERR;
349                 csio->scsi_status = pt->header.scsi_status;
350                 break;
351         default:
352                 ccbh->status = CAM_REQ_CMP_ERR;
353                 csio->scsi_status = pt->header.scsi_status;
354                 break;
355         }
356
357         mfi_release_command(cm);
358         xpt_done(ccb);
359 }
360
361 static void
362 mfip_cam_poll(struct cam_sim *sim)
363 {
364         return;
365 }
366