Merge branch 'vendor/FILE'
[dragonfly.git] / sys / dev / raid / 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  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  *
31  *            Copyright 1994-2009 The FreeBSD Project.
32  *            All rights reserved.
33  *
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  *
40  *    THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
42  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FREEBSD PROJECT OR
44  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY,OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47  * PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY
48  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  *
52  * The views and conclusions contained in the software and documentation
53  * are those of the authors and should not be interpreted as representing
54  * official policies,either expressed or implied, of the FreeBSD Project.
55  *
56  * $FreeBSD: src/sys/dev/mfi/mfi_cam.c,v 1.6 2011/06/28 08:36:48 kevlo Exp $
57  */
58
59 #include "opt_mfi.h"
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/module.h>
66 #include <sys/bus.h>
67 #include <sys/conf.h>
68 #include <sys/eventhandler.h>
69 #include <sys/rman.h>
70 #include <sys/bus_dma.h>
71 #include <sys/buf2.h>
72 #include <sys/ioccom.h>
73 #include <sys/uio.h>
74 #include <sys/proc.h>
75 #include <sys/signalvar.h>
76
77 #include <bus/cam/cam.h>
78 #include <bus/cam/cam_ccb.h>
79 #include <bus/cam/cam_debug.h>
80 #include <bus/cam/cam_sim.h>
81 #include <bus/cam/cam_xpt_sim.h>
82 #include <bus/cam/scsi/scsi_all.h>
83 #include <bus/cam/scsi/scsi_message.h>
84
85 #include <machine/md_var.h>
86
87 #include <dev/raid/mfi/mfireg.h>
88 #include <dev/raid/mfi/mfi_ioctl.h>
89 #include <dev/raid/mfi/mfivar.h>
90
91 struct mfip_softc {
92         device_t        dev;
93         struct mfi_softc *mfi_sc;
94         struct cam_devq *devq;
95         struct cam_sim  *sim;
96         struct cam_path *path;
97 };
98
99 static int      mfip_probe(device_t);
100 static int      mfip_attach(device_t);
101 static int      mfip_detach(device_t);
102 static void     mfip_cam_action(struct cam_sim *, union ccb *);
103 static void     mfip_cam_poll(struct cam_sim *);
104 static struct mfi_command * mfip_start(void *);
105 static void     mfip_done(struct mfi_command *cm);
106
107 static devclass_t       mfip_devclass;
108 static device_method_t  mfip_methods[] = {
109         DEVMETHOD(device_probe,         mfip_probe),
110         DEVMETHOD(device_attach,        mfip_attach),
111         DEVMETHOD(device_detach,        mfip_detach),
112         {0, 0}
113 };
114 static driver_t mfip_driver = {
115         "mfip",
116         mfip_methods,
117         sizeof(struct mfip_softc)
118 };
119 DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, NULL, NULL);
120 MODULE_DEPEND(mfip, cam, 1, 1, 1);
121 MODULE_DEPEND(mfip, mfi, 1, 1, 1);
122
123 #define ccb_mfip_ptr sim_priv.entries[0].ptr
124
125 static int
126 mfip_probe(device_t dev)
127 {
128
129         device_set_desc(dev, "SCSI Passthrough Bus");
130         return (0);
131 }
132
133 static int
134 mfip_attach(device_t dev)
135 {
136         struct mfip_softc *sc;
137         struct mfi_softc *mfisc;
138
139         sc = device_get_softc(dev);
140         if (sc == NULL)
141                 return (EINVAL);
142
143         mfisc = device_get_softc(device_get_parent(dev));
144         sc->dev = dev;
145         sc->mfi_sc = mfisc;
146         mfisc->mfi_cam_start = mfip_start;
147
148         if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
149                 return (ENOMEM);
150
151         sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
152                                 device_get_unit(dev), &mfisc->mfi_io_lock, 1,
153                                 MFI_SCSI_MAX_CMDS, sc->devq);
154         if (sc->sim == NULL) {
155                 device_printf(dev, "CAM SIM attach failed\n");
156                 return (EINVAL);
157         }
158
159         lockmgr(&mfisc->mfi_io_lock, LK_EXCLUSIVE);
160         if (xpt_bus_register(sc->sim, 0) != 0) {
161                 device_printf(dev, "XPT bus registration failed\n");
162                 cam_sim_free(sc->sim);
163                 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
164                 return (EINVAL);
165         }
166         lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
167
168         return (0);
169 }
170
171 static int
172 mfip_detach(device_t dev)
173 {
174         struct mfip_softc *sc;
175
176         sc = device_get_softc(dev);
177         if (sc == NULL)
178                 return (EINVAL);
179
180         if (sc->sim != NULL) {
181                 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_EXCLUSIVE);
182                 xpt_bus_deregister(cam_sim_path(sc->sim));
183                 cam_sim_free(sc->sim);
184                 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_RELEASE);
185         }
186
187         return (0);
188 }
189
190 static void
191 mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
192 {
193         struct mfip_softc *sc = cam_sim_softc(sim);
194         struct mfi_softc *mfisc = sc->mfi_sc;
195
196         KKASSERT(lockstatus(&mfisc->mfi_io_lock, curthread) != 0);
197
198         switch (ccb->ccb_h.func_code) {
199         case XPT_PATH_INQ:
200         {
201                 struct ccb_pathinq *cpi = &ccb->cpi;
202
203                 cpi->version_num = 1;
204                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
205                 cpi->target_sprt = 0;
206                 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
207                 cpi->hba_eng_cnt = 0;
208                 cpi->max_target = MFI_SCSI_MAX_TARGETS;
209                 cpi->max_lun = MFI_SCSI_MAX_LUNS;
210                 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
211                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
212                 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
213                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
214                 cpi->unit_number = cam_sim_unit(sim);
215                 cpi->bus_id = cam_sim_bus(sim);
216                 cpi->base_transfer_speed = 150000;
217                 cpi->transport = XPORT_SAS;
218                 cpi->transport_version = 0;
219                 cpi->protocol = PROTO_SCSI;
220                 cpi->protocol_version = SCSI_REV_2;
221                 cpi->ccb_h.status = CAM_REQ_CMP;
222                 break;
223         }
224         case XPT_RESET_BUS:
225                 ccb->ccb_h.status = CAM_REQ_CMP;
226                 break;
227         case XPT_RESET_DEV:
228                 ccb->ccb_h.status = CAM_REQ_CMP;
229                 break;
230         case XPT_GET_TRAN_SETTINGS:
231         {
232                 struct ccb_trans_settings_sas *sas =
233                     &ccb->cts.xport_specific.sas;
234
235                 ccb->cts.protocol = PROTO_SCSI;
236                 ccb->cts.protocol_version = SCSI_REV_2;
237                 ccb->cts.transport = XPORT_SAS;
238                 ccb->cts.transport_version = 0;
239
240                 sas->valid &= ~CTS_SAS_VALID_SPEED;
241                 sas->bitrate = 150000;
242
243                 ccb->ccb_h.status = CAM_REQ_CMP;
244                 break;
245         }
246         case XPT_SET_TRAN_SETTINGS:
247                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
248                 break;
249         case XPT_SCSI_IO:
250         {
251                 struct ccb_hdr          *ccbh = &ccb->ccb_h;
252                 struct ccb_scsiio       *csio = &ccb->csio;
253
254                 ccbh->status = CAM_REQ_INPROG;
255                 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
256                         ccbh->status = CAM_REQ_INVALID;
257                         break;
258                 }
259                 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
260                         if (ccbh->flags & CAM_DATA_PHYS) {
261                                 ccbh->status = CAM_REQ_INVALID;
262                                 break;
263                         }
264                         if (ccbh->flags & CAM_SCATTER_VALID) {
265                                 ccbh->status = CAM_REQ_INVALID;
266                                 break;
267                         }
268                 }
269
270                 ccbh->ccb_mfip_ptr = sc;
271                 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
272                 mfi_startio(mfisc);
273                 return;
274         }
275         default:
276                 ccb->ccb_h.status = CAM_REQ_INVALID;
277                 break;
278         }
279
280         xpt_done(ccb);
281         return;
282 }
283
284 static struct mfi_command *
285 mfip_start(void *data)
286 {
287         union ccb *ccb = data;
288         struct ccb_hdr *ccbh = &ccb->ccb_h;
289         struct ccb_scsiio *csio = &ccb->csio;
290         struct mfip_softc *sc;
291         struct mfi_pass_frame *pt;
292         struct mfi_command *cm;
293         uint32_t context = 0;
294
295         sc = ccbh->ccb_mfip_ptr;
296
297         if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
298                 return (NULL);
299
300         /* Zero out the MFI frame */
301         context = cm->cm_frame->header.context;
302         bzero(cm->cm_frame, sizeof(union mfi_frame));
303         cm->cm_frame->header.context = context;
304
305         pt = &cm->cm_frame->pass;
306         pt->header.cmd = MFI_CMD_PD_SCSI_IO;
307         pt->header.cmd_status = 0;
308         pt->header.scsi_status = 0;
309         pt->header.target_id = ccbh->target_id;
310         pt->header.lun_id = ccbh->target_lun;
311         pt->header.flags = 0;
312         pt->header.timeout = 0;
313         pt->header.data_len = csio->dxfer_len;
314         pt->header.sense_len = MFI_SENSE_LEN;
315         pt->header.cdb_len = csio->cdb_len;
316         pt->sense_addr_lo = cm->cm_sense_busaddr;
317         pt->sense_addr_hi = 0;
318         if (ccbh->flags & CAM_CDB_POINTER)
319                 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
320         else
321                 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
322         cm->cm_complete = mfip_done;
323         cm->cm_private = ccb;
324         cm->cm_sg = &pt->sgl;
325         cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
326         cm->cm_data = csio->data_ptr;
327         cm->cm_len = csio->dxfer_len;
328         switch (ccbh->flags & CAM_DIR_MASK) {
329         case CAM_DIR_IN:
330                 cm->cm_flags = MFI_CMD_DATAIN;
331                 break;
332         case CAM_DIR_OUT:
333                 cm->cm_flags = MFI_CMD_DATAOUT;
334                 break;
335         case CAM_DIR_NONE:
336         default:
337                 cm->cm_data = NULL;
338                 cm->cm_len = 0;
339                 cm->cm_flags = 0;
340                 break;
341         }
342
343         TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
344         return (cm);
345 }
346
347 static void
348 mfip_done(struct mfi_command *cm)
349 {
350         union ccb *ccb = cm->cm_private;
351         struct ccb_hdr *ccbh = &ccb->ccb_h;
352         struct ccb_scsiio *csio = &ccb->csio;
353         struct mfi_pass_frame *pt;
354
355         pt = &cm->cm_frame->pass;
356
357         switch (pt->header.cmd_status) {
358         case MFI_STAT_OK:
359         {
360                 uint8_t command, device;
361
362                 ccbh->status = CAM_REQ_CMP;
363                 csio->scsi_status = pt->header.scsi_status;
364                 if (ccbh->flags & CAM_CDB_POINTER)
365                         command = csio->cdb_io.cdb_ptr[0];
366                 else
367                         command = csio->cdb_io.cdb_bytes[0];
368                 if (command == INQUIRY) {
369                         device = csio->data_ptr[0] & 0x1f;
370                         if ((device == T_DIRECT) || (device == T_PROCESSOR))
371                                 csio->data_ptr[0] =
372                                      (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
373                 }
374                 break;
375         }
376         case MFI_STAT_SCSI_DONE_WITH_ERROR:
377         {
378                 int sense_len;
379
380                 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
381                 csio->scsi_status = pt->header.scsi_status;
382                 sense_len = min(pt->header.sense_len, sizeof(struct scsi_sense_data));
383                 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
384                 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
385                 break;
386         }
387         case MFI_STAT_DEVICE_NOT_FOUND:
388                 ccbh->status = CAM_SEL_TIMEOUT;
389                 break;
390         case MFI_STAT_SCSI_IO_FAILED:
391                 ccbh->status = CAM_REQ_CMP_ERR;
392                 csio->scsi_status = pt->header.scsi_status;
393                 break;
394         default:
395                 ccbh->status = CAM_REQ_CMP_ERR;
396                 csio->scsi_status = pt->header.scsi_status;
397                 break;
398         }
399
400         mfi_release_command(cm);
401         xpt_done(ccb);
402 }
403
404 static void
405 mfip_cam_poll(struct cam_sim *sim)
406 {
407         return;
408 }