mfi(4): Update to LSI's version 4.23 of the driver.
[dragonfly.git] / sys / dev / raid / mfi / mfi_cam.c
CommitLineData
249d29c8
SW
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 *
f0d8b1f2 26 * $FreeBSD: src/sys/dev/mfi/mfi_cam.c,v 1.7 2011/10/13 20:06:19 marius Exp $
590ba11d 27 * FreeBSD projects/head_mfi/ r232949
249d29c8
SW
28 */
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/bus.h>
38#include <sys/conf.h>
39#include <sys/eventhandler.h>
40#include <sys/rman.h>
41#include <sys/bus_dma.h>
42#include <sys/buf2.h>
249d29c8
SW
43#include <sys/uio.h>
44#include <sys/proc.h>
45#include <sys/signalvar.h>
46
47#include <bus/cam/cam.h>
48#include <bus/cam/cam_ccb.h>
49#include <bus/cam/cam_debug.h>
50#include <bus/cam/cam_sim.h>
51#include <bus/cam/cam_xpt_sim.h>
52#include <bus/cam/scsi/scsi_all.h>
53#include <bus/cam/scsi/scsi_message.h>
54
249d29c8 55#include <machine/md_var.h>
249d29c8
SW
56
57#include <dev/raid/mfi/mfireg.h>
58#include <dev/raid/mfi/mfi_ioctl.h>
59#include <dev/raid/mfi/mfivar.h>
60
61struct mfip_softc {
62 device_t dev;
63 struct mfi_softc *mfi_sc;
64 struct cam_devq *devq;
65 struct cam_sim *sim;
66 struct cam_path *path;
67};
68
69static int mfip_probe(device_t);
70static int mfip_attach(device_t);
71static int mfip_detach(device_t);
72static void mfip_cam_action(struct cam_sim *, union ccb *);
73static void mfip_cam_poll(struct cam_sim *);
74static struct mfi_command * mfip_start(void *);
75static void mfip_done(struct mfi_command *cm);
76
77static devclass_t mfip_devclass;
78static device_method_t mfip_methods[] = {
79 DEVMETHOD(device_probe, mfip_probe),
80 DEVMETHOD(device_attach, mfip_attach),
81 DEVMETHOD(device_detach, mfip_detach),
82 {0, 0}
83};
84static driver_t mfip_driver = {
85 "mfip",
86 mfip_methods,
87 sizeof(struct mfip_softc)
88};
aa2b9d05 89DRIVER_MODULE(mfip, mfi, mfip_driver, mfip_devclass, NULL, NULL);
249d29c8
SW
90MODULE_DEPEND(mfip, cam, 1, 1, 1);
91MODULE_DEPEND(mfip, mfi, 1, 1, 1);
92
93#define ccb_mfip_ptr sim_priv.entries[0].ptr
94
95static int
96mfip_probe(device_t dev)
97{
98
99 device_set_desc(dev, "SCSI Passthrough Bus");
100 return (0);
101}
102
103static int
104mfip_attach(device_t dev)
105{
106 struct mfip_softc *sc;
107 struct mfi_softc *mfisc;
108
109 sc = device_get_softc(dev);
110 if (sc == NULL)
111 return (EINVAL);
112
113 mfisc = device_get_softc(device_get_parent(dev));
114 sc->dev = dev;
115 sc->mfi_sc = mfisc;
116 mfisc->mfi_cam_start = mfip_start;
117
118 if ((sc->devq = cam_simq_alloc(MFI_SCSI_MAX_CMDS)) == NULL)
119 return (ENOMEM);
120
121 sc->sim = cam_sim_alloc(mfip_cam_action, mfip_cam_poll, "mfi", sc,
122 device_get_unit(dev), &mfisc->mfi_io_lock, 1,
123 MFI_SCSI_MAX_CMDS, sc->devq);
124 if (sc->sim == NULL) {
590ba11d 125 cam_simq_release(sc->devq);
249d29c8
SW
126 device_printf(dev, "CAM SIM attach failed\n");
127 return (EINVAL);
128 }
129
130 lockmgr(&mfisc->mfi_io_lock, LK_EXCLUSIVE);
131 if (xpt_bus_register(sc->sim, 0) != 0) {
132 device_printf(dev, "XPT bus registration failed\n");
133 cam_sim_free(sc->sim);
590ba11d 134 cam_simq_release(sc->devq);
249d29c8
SW
135 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
136 return (EINVAL);
137 }
138 lockmgr(&mfisc->mfi_io_lock, LK_RELEASE);
139
140 return (0);
141}
142
143static int
144mfip_detach(device_t dev)
145{
146 struct mfip_softc *sc;
147
148 sc = device_get_softc(dev);
149 if (sc == NULL)
150 return (EINVAL);
151
152 if (sc->sim != NULL) {
153 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_EXCLUSIVE);
154 xpt_bus_deregister(cam_sim_path(sc->sim));
155 cam_sim_free(sc->sim);
156 lockmgr(&sc->mfi_sc->mfi_io_lock, LK_RELEASE);
157 }
158
590ba11d
SW
159 if (sc->devq != NULL)
160 cam_simq_release(sc->devq);
161
249d29c8
SW
162 return (0);
163}
164
165static void
166mfip_cam_action(struct cam_sim *sim, union ccb *ccb)
167{
168 struct mfip_softc *sc = cam_sim_softc(sim);
169 struct mfi_softc *mfisc = sc->mfi_sc;
170
171 KKASSERT(lockstatus(&mfisc->mfi_io_lock, curthread) != 0);
172
173 switch (ccb->ccb_h.func_code) {
174 case XPT_PATH_INQ:
175 {
176 struct ccb_pathinq *cpi = &ccb->cpi;
177
178 cpi->version_num = 1;
179 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
180 cpi->target_sprt = 0;
181 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
182 cpi->hba_eng_cnt = 0;
183 cpi->max_target = MFI_SCSI_MAX_TARGETS;
184 cpi->max_lun = MFI_SCSI_MAX_LUNS;
185 cpi->initiator_id = MFI_SCSI_INITIATOR_ID;
186 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
187 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
188 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
189 cpi->unit_number = cam_sim_unit(sim);
190 cpi->bus_id = cam_sim_bus(sim);
191 cpi->base_transfer_speed = 150000;
192 cpi->transport = XPORT_SAS;
193 cpi->transport_version = 0;
194 cpi->protocol = PROTO_SCSI;
195 cpi->protocol_version = SCSI_REV_2;
196 cpi->ccb_h.status = CAM_REQ_CMP;
197 break;
198 }
199 case XPT_RESET_BUS:
200 ccb->ccb_h.status = CAM_REQ_CMP;
201 break;
202 case XPT_RESET_DEV:
203 ccb->ccb_h.status = CAM_REQ_CMP;
204 break;
205 case XPT_GET_TRAN_SETTINGS:
206 {
207 struct ccb_trans_settings_sas *sas =
208 &ccb->cts.xport_specific.sas;
209
210 ccb->cts.protocol = PROTO_SCSI;
211 ccb->cts.protocol_version = SCSI_REV_2;
212 ccb->cts.transport = XPORT_SAS;
213 ccb->cts.transport_version = 0;
214
215 sas->valid &= ~CTS_SAS_VALID_SPEED;
216 sas->bitrate = 150000;
217
218 ccb->ccb_h.status = CAM_REQ_CMP;
219 break;
220 }
221 case XPT_SET_TRAN_SETTINGS:
222 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
223 break;
224 case XPT_SCSI_IO:
225 {
226 struct ccb_hdr *ccbh = &ccb->ccb_h;
227 struct ccb_scsiio *csio = &ccb->csio;
228
229 ccbh->status = CAM_REQ_INPROG;
230 if (csio->cdb_len > MFI_SCSI_MAX_CDB_LEN) {
231 ccbh->status = CAM_REQ_INVALID;
232 break;
233 }
234 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
235 if (ccbh->flags & CAM_DATA_PHYS) {
236 ccbh->status = CAM_REQ_INVALID;
237 break;
238 }
239 if (ccbh->flags & CAM_SCATTER_VALID) {
240 ccbh->status = CAM_REQ_INVALID;
241 break;
242 }
243 }
244
245 ccbh->ccb_mfip_ptr = sc;
246 TAILQ_INSERT_TAIL(&mfisc->mfi_cam_ccbq, ccbh, sim_links.tqe);
247 mfi_startio(mfisc);
248 return;
249 }
250 default:
251 ccb->ccb_h.status = CAM_REQ_INVALID;
252 break;
253 }
254
255 xpt_done(ccb);
256 return;
257}
258
259static struct mfi_command *
260mfip_start(void *data)
261{
262 union ccb *ccb = data;
263 struct ccb_hdr *ccbh = &ccb->ccb_h;
264 struct ccb_scsiio *csio = &ccb->csio;
265 struct mfip_softc *sc;
266 struct mfi_pass_frame *pt;
267 struct mfi_command *cm;
17566092 268 uint32_t context = 0;
249d29c8
SW
269
270 sc = ccbh->ccb_mfip_ptr;
271
272 if ((cm = mfi_dequeue_free(sc->mfi_sc)) == NULL)
273 return (NULL);
274
17566092
SW
275 /* Zero out the MFI frame */
276 context = cm->cm_frame->header.context;
277 bzero(cm->cm_frame, sizeof(union mfi_frame));
278 cm->cm_frame->header.context = context;
279
249d29c8
SW
280 pt = &cm->cm_frame->pass;
281 pt->header.cmd = MFI_CMD_PD_SCSI_IO;
282 pt->header.cmd_status = 0;
283 pt->header.scsi_status = 0;
284 pt->header.target_id = ccbh->target_id;
285 pt->header.lun_id = ccbh->target_lun;
286 pt->header.flags = 0;
287 pt->header.timeout = 0;
288 pt->header.data_len = csio->dxfer_len;
289 pt->header.sense_len = MFI_SENSE_LEN;
290 pt->header.cdb_len = csio->cdb_len;
291 pt->sense_addr_lo = cm->cm_sense_busaddr;
292 pt->sense_addr_hi = 0;
293 if (ccbh->flags & CAM_CDB_POINTER)
294 bcopy(csio->cdb_io.cdb_ptr, &pt->cdb[0], csio->cdb_len);
295 else
296 bcopy(csio->cdb_io.cdb_bytes, &pt->cdb[0], csio->cdb_len);
297 cm->cm_complete = mfip_done;
298 cm->cm_private = ccb;
299 cm->cm_sg = &pt->sgl;
300 cm->cm_total_frame_size = MFI_PASS_FRAME_SIZE;
301 cm->cm_data = csio->data_ptr;
302 cm->cm_len = csio->dxfer_len;
303 switch (ccbh->flags & CAM_DIR_MASK) {
304 case CAM_DIR_IN:
305 cm->cm_flags = MFI_CMD_DATAIN;
306 break;
307 case CAM_DIR_OUT:
308 cm->cm_flags = MFI_CMD_DATAOUT;
309 break;
310 case CAM_DIR_NONE:
311 default:
312 cm->cm_data = NULL;
313 cm->cm_len = 0;
314 cm->cm_flags = 0;
315 break;
316 }
317
318 TAILQ_REMOVE(&sc->mfi_sc->mfi_cam_ccbq, ccbh, sim_links.tqe);
319 return (cm);
320}
321
322static void
323mfip_done(struct mfi_command *cm)
324{
325 union ccb *ccb = cm->cm_private;
326 struct ccb_hdr *ccbh = &ccb->ccb_h;
327 struct ccb_scsiio *csio = &ccb->csio;
249d29c8
SW
328 struct mfi_pass_frame *pt;
329
249d29c8
SW
330 pt = &cm->cm_frame->pass;
331
332 switch (pt->header.cmd_status) {
333 case MFI_STAT_OK:
334 {
335 uint8_t command, device;
336
337 ccbh->status = CAM_REQ_CMP;
338 csio->scsi_status = pt->header.scsi_status;
339 if (ccbh->flags & CAM_CDB_POINTER)
769308d7 340 command = csio->cdb_io.cdb_ptr[0];
249d29c8 341 else
769308d7 342 command = csio->cdb_io.cdb_bytes[0];
249d29c8 343 if (command == INQUIRY) {
769308d7 344 device = csio->data_ptr[0] & 0x1f;
249d29c8
SW
345 if ((device == T_DIRECT) || (device == T_PROCESSOR))
346 csio->data_ptr[0] =
ede4fee5 347 (csio->data_ptr[0] & 0xe0) | T_NODEVICE;
249d29c8
SW
348 }
349 break;
350 }
351 case MFI_STAT_SCSI_DONE_WITH_ERROR:
352 {
353 int sense_len;
354
355 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
356 csio->scsi_status = pt->header.scsi_status;
f0d8b1f2
SW
357 if (pt->header.sense_len < csio->sense_len)
358 csio->sense_resid = csio->sense_len -
359 pt->header.sense_len;
360 else
361 csio->sense_resid = 0;
362 sense_len = min(pt->header.sense_len,
363 sizeof(struct scsi_sense_data));
249d29c8
SW
364 bzero(&csio->sense_data, sizeof(struct scsi_sense_data));
365 bcopy(&cm->cm_sense->data[0], &csio->sense_data, sense_len);
366 break;
367 }
368 case MFI_STAT_DEVICE_NOT_FOUND:
369 ccbh->status = CAM_SEL_TIMEOUT;
370 break;
371 case MFI_STAT_SCSI_IO_FAILED:
372 ccbh->status = CAM_REQ_CMP_ERR;
373 csio->scsi_status = pt->header.scsi_status;
374 break;
375 default:
376 ccbh->status = CAM_REQ_CMP_ERR;
377 csio->scsi_status = pt->header.scsi_status;
378 break;
379 }
380
381 mfi_release_command(cm);
382 xpt_done(ccb);
383}
384
385static void
386mfip_cam_poll(struct cam_sim *sim)
387{
388 return;
389}