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