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