xref: /dragonfly/sys/dev/raid/aac/aac_cam.c (revision 10cbe914)
1 /*-
2  * Copyright (c) 2002 Adaptec, Inc.
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  *	$FreeBSD: src/sys/dev/aac/aac_cam.c,v 1.2.2.4 2003/04/08 13:22:08 scottl Exp $
27  *	$DragonFly: src/sys/dev/raid/aac/aac_cam.c,v 1.10 2008/05/18 20:30:23 pavalos Exp $
28  */
29 
30 /*
31  * CAM front-end for communicating with non-DASD devices
32  */
33 
34 #include "opt_aac.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 
43 #include <bus/cam/cam.h>
44 #include <bus/cam/cam_ccb.h>
45 #include <bus/cam/cam_debug.h>
46 #include <bus/cam/cam_sim.h>
47 #include <bus/cam/cam_xpt_sim.h>
48 #include <bus/cam/scsi/scsi_all.h>
49 #include <bus/cam/scsi/scsi_message.h>
50 
51 #include <sys/bus.h>
52 #include <sys/conf.h>
53 #include <sys/devicestat.h>
54 #include <sys/disk.h>
55 #include <sys/rman.h>
56 
57 #include <machine/md_var.h>
58 
59 #include <sys/mplock2.h>
60 
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 
64 #include "aacreg.h"
65 #include "aac_ioctl.h"
66 #include "aacvar.h"
67 
68 struct aac_cam {
69 	device_t		dev;
70 	struct aac_sim		*inf;
71 	struct cam_sim		*sim;
72 	struct cam_path		*path;
73 };
74 
75 static int aac_cam_probe(device_t dev);
76 static int aac_cam_attach(device_t dev);
77 static int aac_cam_detach(device_t dev);
78 static void aac_cam_action(struct cam_sim *, union ccb *);
79 static void aac_cam_poll(struct cam_sim *);
80 static void aac_cam_complete(struct aac_command *);
81 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
82 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
83 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
84 
85 static devclass_t	aac_pass_devclass;
86 
87 static device_method_t	aac_pass_methods[] = {
88 	DEVMETHOD(device_probe,		aac_cam_probe),
89 	DEVMETHOD(device_attach,	aac_cam_attach),
90 	DEVMETHOD(device_detach,	aac_cam_detach),
91 	{ 0, 0 }
92 };
93 
94 static driver_t	aac_pass_driver = {
95 	"aacp",
96 	aac_pass_methods,
97 	sizeof(struct aac_cam)
98 };
99 
100 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0);
101 MODULE_DEPEND(aacp, cam, 1, 1, 1);
102 
103 MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
104 
105 static void
106 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
107 {
108 	struct aac_cam *camsc;
109 
110 	switch (event->ev_type) {
111 	case AAC_EVENT_CMFREE:
112 		camsc = arg;
113 		kfree(event, M_AACCAM);
114 		xpt_release_simq(camsc->sim, 1);
115 		break;
116 	default:
117 		device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
118 		    event->ev_type);
119 		break;
120 	}
121 
122 	return;
123 }
124 
125 static int
126 aac_cam_probe(device_t dev)
127 {
128 	debug_called(2);
129 
130 	return (0);
131 }
132 
133 static int
134 aac_cam_detach(device_t dev)
135 {
136 	struct aac_cam *camsc;
137 	debug_called(2);
138 
139 	camsc = (struct aac_cam *)device_get_softc(dev);
140 
141 	get_mplock();
142 
143 	xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
144 	xpt_free_path(camsc->path);
145 	xpt_bus_deregister(cam_sim_path(camsc->sim));
146 	cam_sim_free(camsc->sim);
147 
148 	rel_mplock();
149 
150 	return (0);
151 }
152 
153 /*
154  * Register the driver as a CAM SIM
155  */
156 static int
157 aac_cam_attach(device_t dev)
158 {
159 	struct cam_devq *devq;
160 	struct cam_sim *sim;
161 	struct cam_path *path;
162 	struct aac_cam *camsc;
163 	struct aac_sim *inf;
164 
165 	debug_called(1);
166 
167 	camsc = (struct aac_cam *)device_get_softc(dev);
168 	inf = (struct aac_sim *)device_get_ivars(dev);
169 	camsc->inf = inf;
170 
171 	devq = cam_simq_alloc(inf->TargetsPerBus);
172 	if (devq == NULL)
173 		return (EIO);
174 
175 	sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
176 	    device_get_unit(dev), &sim_mplock, 1, 1, devq);
177 	cam_simq_release(devq);
178 	if (sim == NULL) {
179 		return (EIO);
180 	}
181 
182 	/* Since every bus has it's own sim, every bus 'appears' as bus 0 */
183 	if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
184 		cam_sim_free(sim);
185 		return (EIO);
186 	}
187 
188 	if (xpt_create_path(&path, NULL, cam_sim_path(sim),
189 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
190 		xpt_bus_deregister(cam_sim_path(sim));
191 		cam_sim_free(sim);
192 		return (EIO);
193 	}
194 
195 	camsc->sim = sim;
196 	camsc->path = path;
197 
198 	return (0);
199 }
200 
201 static void
202 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
203 {
204 	struct	aac_cam *camsc;
205 	struct	aac_softc *sc;
206 	struct	aac_srb32 *srb;
207 	struct	aac_fib *fib;
208 	struct	aac_command *cm;
209 
210 	debug_called(2);
211 
212 	camsc = (struct aac_cam *)cam_sim_softc(sim);
213 	sc = camsc->inf->aac_sc;
214 
215 	/* Synchronous ops, and ops that don't require communication with the
216 	 * controller */
217 	switch(ccb->ccb_h.func_code) {
218 	case XPT_SCSI_IO:
219 	case XPT_RESET_DEV:
220 		/* These are handled down below */
221 		break;
222 	case XPT_CALC_GEOMETRY:
223 	{
224 		struct ccb_calc_geometry *ccg;
225 		u_int32_t size_mb;
226 		u_int32_t secs_per_cylinder;
227 
228 		ccg = &ccb->ccg;
229 		size_mb = ccg->volume_size /
230 		    ((1024L * 1024L) / ccg->block_size);
231 		if (size_mb >= (2 * 1024)) {		/* 2GB */
232 			ccg->heads = 255;
233 			ccg->secs_per_track = 63;
234 		} else if (size_mb >= (1 * 1024)) {	/* 1GB */
235 			ccg->heads = 128;
236 			ccg->secs_per_track = 32;
237 		} else {
238 			ccg->heads = 64;
239 			ccg->secs_per_track = 32;
240 		}
241 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
242 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
243 
244 		ccb->ccb_h.status = CAM_REQ_CMP;
245 		xpt_done(ccb);
246 		return;
247 	}
248 	case XPT_PATH_INQ:
249 	{
250 		struct ccb_pathinq *cpi = &ccb->cpi;
251 
252 		cpi->version_num = 1;
253 		cpi->hba_inquiry = PI_WIDE_16;
254 		cpi->target_sprt = 0;
255 
256 		/* Resetting via the passthrough causes problems. */
257 		cpi->hba_misc = PIM_NOBUSRESET;
258 		cpi->hba_eng_cnt = 0;
259 		cpi->max_target = camsc->inf->TargetsPerBus;
260 		cpi->max_lun = 8;	/* Per the controller spec */
261 		cpi->initiator_id = camsc->inf->InitiatorBusId;
262 		cpi->bus_id = camsc->inf->BusNumber;
263 		cpi->base_transfer_speed = 3300;
264 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
265 		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
266 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
267 		cpi->unit_number = cam_sim_unit(sim);
268                 cpi->transport = XPORT_SPI;
269                 cpi->transport_version = 2;
270                 cpi->protocol = PROTO_SCSI;
271                 cpi->protocol_version = SCSI_REV_2;
272 		ccb->ccb_h.status = CAM_REQ_CMP;
273 		xpt_done(ccb);
274 		return;
275 	}
276 	case XPT_GET_TRAN_SETTINGS:
277 	{
278 		struct ccb_trans_settings_scsi *scsi =
279 			&ccb->cts.proto_specific.scsi;
280 		struct ccb_trans_settings_spi *spi =
281 			&ccb->cts.xport_specific.spi;
282 		ccb->cts.protocol = PROTO_SCSI;
283 		ccb->cts.protocol_version = SCSI_REV_2;
284 		ccb->cts.transport = XPORT_SPI;
285 		ccb->cts.transport_version = 2;
286 		if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
287 			scsi->valid = CTS_SCSI_VALID_TQ;
288 			spi->valid |= CTS_SPI_VALID_DISC;
289 		} else {
290 			scsi->valid = 0;
291 		}
292 		ccb->ccb_h.status = CAM_REQ_CMP;
293 		xpt_done(ccb);
294 		return;
295 	}
296 	case XPT_SET_TRAN_SETTINGS:
297 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
298 		xpt_done(ccb);
299 		return;
300 	case XPT_RESET_BUS:
301 		if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
302 			ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
303 		} else {
304 			ccb->ccb_h.status = CAM_REQ_CMP;
305 		}
306 		xpt_done(ccb);
307 		return;
308 	case XPT_ABORT:
309 		ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
310 		xpt_done(ccb);
311 		return;
312 	case XPT_TERM_IO:
313 		ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
314 		xpt_done(ccb);
315 		return;
316 	default:
317 		device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
318 		    ccb->ccb_h.func_code);
319 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
320 		xpt_done(ccb);
321 		return;
322 	}
323 
324 	/* Async ops that require communcation with the controller */
325 
326 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
327 	if (aac_alloc_command(sc, &cm)) {
328 		struct aac_event *event;
329 
330 		xpt_freeze_simq(sim, 1);
331 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
332 		xpt_done(ccb);
333 		event = kmalloc(sizeof(struct aac_event), M_AACCAM,
334 				M_INTWAIT | M_ZERO);
335 		event->ev_callback = aac_cam_event;
336 		event->ev_arg = camsc;
337 		event->ev_type = AAC_EVENT_CMFREE;
338 		aac_add_event(sc, event);
339 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
340 		return;
341 	}
342 
343 	fib = cm->cm_fib;
344 	srb = (struct aac_srb32 *)&fib->data[0];
345 	cm->cm_datalen = 0;
346 
347 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
348 	case CAM_DIR_IN:
349 		srb->flags = AAC_SRB_FLAGS_DATA_IN;
350 		cm->cm_flags |= AAC_CMD_DATAIN;
351 		break;
352 	case CAM_DIR_OUT:
353 		srb->flags = AAC_SRB_FLAGS_DATA_OUT;
354 		cm->cm_flags |= AAC_CMD_DATAOUT;
355 		break;
356 	case CAM_DIR_NONE:
357 		srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
358 		break;
359 	default:
360 		srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
361 		cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
362 		break;
363 	}
364 
365 	switch(ccb->ccb_h.func_code) {
366 	case XPT_SCSI_IO:
367 	{
368 		struct ccb_scsiio *csio = &ccb->csio;
369 
370 		srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
371 
372 		/*
373 		 * Copy the CDB into the SRB.  It's only 6-16 bytes,
374 		 * so a copy is not too expensive.
375 		 */
376 		srb->cdb_len = csio->cdb_len;
377 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
378 			bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
379 			    srb->cdb_len);
380 		else
381 			bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
382 			    srb->cdb_len);
383 
384 		/* Map the s/g list. XXX 32bit addresses only! */
385 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
386 			if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
387 				srb->data_len = csio->dxfer_len;
388 				if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
389 					/*
390 					 * XXX This isn't 64-bit clean.
391 					 * However, this condition is not
392 					 * normally used in CAM.
393 					 */
394 					srb->sg_map32.SgCount = 1;
395 					srb->sg_map32.SgEntry[0].SgAddress =
396 					    (uint32_t)(uintptr_t)csio->data_ptr;
397 					srb->sg_map32.SgEntry[0].SgByteCount =
398 					    csio->dxfer_len;
399 				} else {
400 					/*
401 					 * Arrange things so that the S/G
402 					 * map will get set up automagically
403 					 */
404 					cm->cm_data = (void *)csio->data_ptr;
405 					cm->cm_datalen = csio->dxfer_len;
406 					cm->cm_sgtable = &srb->sg_map32;
407 				}
408 			} else {
409 				/* XXX Need to handle multiple s/g elements */
410 				panic("aac_cam: multiple s/g elements");
411 			}
412 		} else {
413 			srb->sg_map32.SgCount = 0;
414 			srb->sg_map32.SgEntry[0].SgByteCount = 0;
415 			srb->data_len = 0;
416 		}
417 
418 		break;
419 	}
420 	case XPT_RESET_DEV:
421 		if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
422 			srb->function = AAC_SRB_FUNC_RESET_DEVICE;
423 			break;
424 		} else {
425 			ccb->ccb_h.status = CAM_REQ_CMP;
426 			xpt_done(ccb);
427 			AAC_LOCK_RELEASE(&sc->aac_io_lock);
428 			return;
429 		}
430 	default:
431 		break;
432 	}
433 
434 	srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
435 	srb->target = ccb->ccb_h.target_id;
436 	srb->lun = ccb->ccb_h.target_lun;
437 	srb->timeout = ccb->ccb_h.timeout;	/* XXX */
438 	srb->retry_limit = 0;
439 
440 	cm->cm_complete = aac_cam_complete;
441 	cm->cm_private = ccb;
442 	cm->cm_timestamp = time_second;
443 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
444 
445 	fib->Header.XferState =
446 	    AAC_FIBSTATE_HOSTOWNED	|
447 	    AAC_FIBSTATE_INITIALISED	|
448 	    AAC_FIBSTATE_FROMHOST	|
449 	    AAC_FIBSTATE_REXPECTED	|
450 	    AAC_FIBSTATE_NORM;
451 	fib->Header.Command = ScsiPortCommand;
452 	fib->Header.Size = sizeof(struct aac_fib_header) +
453 	    sizeof(struct aac_srb32);
454 
455 	aac_enqueue_ready(cm);
456 	aac_startio(cm->cm_sc);
457 
458 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
459 
460 	return;
461 }
462 
463 static void
464 aac_cam_poll(struct cam_sim *sim)
465 {
466 	/*
467 	 * Pinging the interrupt routine isn't very safe, nor is it
468 	 * really necessary.  Do nothing.
469 	 */
470 }
471 
472 static void
473 aac_cam_complete(struct aac_command *cm)
474 {
475 	union	ccb *ccb;
476 	struct 	aac_srb_response *srbr;
477 	struct	aac_softc *sc;
478 
479 	debug_called(2);
480 
481 	sc = cm->cm_sc;
482 	ccb = cm->cm_private;
483 	srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
484 
485 	if (srbr->fib_status != 0) {
486 		device_printf(sc->aac_dev, "Passthru FIB failed!\n");
487 		ccb->ccb_h.status = CAM_REQ_ABORTED;
488 	} else {
489 		/*
490 		 * The SRB error codes just happen to match the CAM error
491 		 * codes.  How convienient!
492 		 */
493 		ccb->ccb_h.status = srbr->srb_status;
494 
495 		/* Take care of SCSI_IO ops. */
496 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
497 			u_int8_t command, device;
498 
499 			ccb->csio.scsi_status = srbr->scsi_status;
500 
501 			/* Take care of autosense */
502 			if (srbr->sense_len) {
503 				int sense_len, scsi_sense_len;
504 
505 				scsi_sense_len = sizeof(struct scsi_sense_data);
506 				bzero(&ccb->csio.sense_data, scsi_sense_len);
507 				sense_len = (srbr->sense_len >
508 				    scsi_sense_len) ? scsi_sense_len :
509 				    srbr->sense_len;
510 				bcopy(&srbr->sense[0], &ccb->csio.sense_data,
511 				    srbr->sense_len);
512 				ccb->csio.sense_len = sense_len;
513 				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
514 				/* scsi_sense_print(&ccb->csio); */
515 			}
516 
517 			/* If this is an inquiry command, fake things out */
518 			if (ccb->ccb_h.flags & CAM_CDB_POINTER)
519 				command = ccb->csio.cdb_io.cdb_ptr[0];
520 			else
521 				command = ccb->csio.cdb_io.cdb_bytes[0];
522 
523 			if ((command == INQUIRY) &&
524 			    (ccb->ccb_h.status == CAM_REQ_CMP)) {
525 				device = ccb->csio.data_ptr[0] & 0x1f;
526 				/*
527 				 * We want DASD and PROC devices to only be
528 				 * visible through the pass device.
529 				 */
530 				if ((device == T_DIRECT) ||
531 				    (device == T_PROCESSOR) ||
532 				    (sc->flags & AAC_FLAGS_CAM_PASSONLY))
533 					ccb->csio.data_ptr[0] =
534 					    ((ccb->csio.data_ptr[0] & 0xe0) |
535 					    T_NODEVICE);
536 			}
537 		}
538 	}
539 
540 	aac_release_command(cm);
541 	xpt_done(ccb);
542 
543 	return;
544 }
545 
546 static u_int32_t
547 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
548 {
549 	struct aac_fib *fib;
550 	struct aac_softc *sc;
551 	struct aac_cam *camsc;
552 	struct aac_vmioctl *vmi;
553 	struct aac_resetbus *rbc;
554 	int e;
555 
556 	camsc = (struct aac_cam *)cam_sim_softc(sim);
557 	sc = camsc->inf->aac_sc;
558 
559 	if (sc == NULL) {
560 		kprintf("Null sc?\n");
561 		return (CAM_REQ_ABORTED);
562 	}
563 
564 	AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
565 	aac_alloc_sync_fib(sc, &fib);
566 
567 	vmi = (struct aac_vmioctl *)&fib->data[0];
568 	bzero(vmi, sizeof(struct aac_vmioctl));
569 
570 	vmi->Command = VM_Ioctl;
571 	vmi->ObjType = FT_DRIVE;
572 	vmi->MethId = sc->scsi_method_id;
573 	vmi->ObjId = 0;
574 	vmi->IoctlCmd = ResetBus;
575 
576 	rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
577 	rbc->BusNumber = camsc->inf->BusNumber;
578 
579 	e = aac_sync_fib(sc, ContainerCommand, 0, fib,
580 	    sizeof(struct aac_vmioctl));
581 	if (e) {
582 		device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
583 		    e);
584 		aac_release_sync_fib(sc);
585 		AAC_LOCK_RELEASE(&sc->aac_io_lock);
586 		return (CAM_REQ_ABORTED);
587 	}
588 
589 	aac_release_sync_fib(sc);
590 	AAC_LOCK_RELEASE(&sc->aac_io_lock);
591 	return (CAM_REQ_CMP);
592 }
593 
594 static u_int32_t
595 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
596 {
597 	return (CAM_UA_ABORT);
598 }
599 
600 static u_int32_t
601 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
602 {
603 	return (CAM_UA_TERMIO);
604 }
605