xref: /freebsd/sys/dev/aac/aac_cam.c (revision 2ffaffaa6e51a7e7f61076e57539b4e3e76b425f)
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 
224 		/* Resetting via the passthrough causes problems. */
225 		cpi->hba_misc = PIM_NOBUSRESET;
226 		cpi->hba_eng_cnt = 0;
227 		cpi->max_target = camsc->inf->TargetsPerBus;
228 		cpi->max_lun = 8;	/* Per the controller spec */
229 		cpi->initiator_id = camsc->inf->InitiatorBusId;
230 		cpi->bus_id = camsc->inf->BusNumber;
231 		cpi->base_transfer_speed = 3300;
232 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
233 		strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
234 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
235 		cpi->unit_number = cam_sim_unit(sim);
236 
237 		ccb->ccb_h.status = CAM_REQ_CMP;
238 		xpt_done(ccb);
239 		return;
240 	}
241 	case XPT_GET_TRAN_SETTINGS:
242 	{
243 		u_int32_t handle;
244 
245 		handle = AAC_BTL_TO_HANDLE(camsc->inf->BusNumber,
246 		    ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
247 		ccb->ccb_h.status = aac_cam_get_tran_settings(sc, &ccb->cts,
248 		    handle);
249 		xpt_done(ccb);
250 		return;
251 	}
252 	case XPT_SET_TRAN_SETTINGS:
253 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
254 		xpt_done(ccb);
255 		return;
256 	case XPT_RESET_BUS:
257 		if (!(sc->quirks & AAC_QUIRK_CAM_NORESET)) {
258 			ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
259 		} else {
260 			ccb->ccb_h.status = CAM_REQ_CMP;
261 		}
262 		xpt_done(ccb);
263 		return;
264 	case XPT_ABORT:
265 		ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
266 		xpt_done(ccb);
267 		return;
268 	case XPT_TERM_IO:
269 		ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
270 		xpt_done(ccb);
271 		return;
272 	default:
273 		device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
274 		    ccb->ccb_h.func_code);
275 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
276 		xpt_done(ccb);
277 		return;
278 	}
279 
280 	/* Async ops that require communcation with the controller */
281 
282 	if (aac_alloc_command(sc, &cm)) {
283 		xpt_freeze_simq(sim, 1);
284 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
285 		xpt_done(ccb);
286 		return;
287 	}
288 
289 	fib = cm->cm_fib;
290 	srb = (struct aac_srb32 *)&fib->data[0];
291 	cm->cm_datalen = 0;
292 
293 	switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
294 	case CAM_DIR_IN:
295 		srb->flags = AAC_SRB_FLAGS_DATA_IN;
296 		cm->cm_flags |= AAC_CMD_DATAIN;
297 		break;
298 	case CAM_DIR_OUT:
299 		srb->flags = AAC_SRB_FLAGS_DATA_OUT;
300 		cm->cm_flags |= AAC_CMD_DATAOUT;
301 		break;
302 	case CAM_DIR_NONE:
303 		srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
304 		break;
305 	default:
306 		srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
307 		cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
308 		break;
309 	}
310 
311 	switch(ccb->ccb_h.func_code) {
312 	case XPT_SCSI_IO:
313 	{
314 		struct ccb_scsiio *csio = &ccb->csio;
315 
316 		srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
317 
318 		/*
319 		 * Copy the CDB into the SRB.  It's only 6-16 bytes,
320 		 * so a copy is not too expensive.
321 		 */
322 		srb->cdb_len = csio->cdb_len;
323 		if (ccb->ccb_h.flags & CAM_CDB_POINTER)
324 			bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
325 			    srb->cdb_len);
326 		else
327 			bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
328 			    srb->cdb_len);
329 
330 		/* Map the s/g list. XXX 32bit addresses only! */
331 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
332 			if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
333 				srb->data_len = csio->dxfer_len;
334 				if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
335 					srb->sg_map32.SgCount = 1;
336 					srb->sg_map32.SgEntry[0].SgAddress =
337 					    (u_int32_t)csio->data_ptr;
338 					srb->sg_map32.SgEntry[0].SgByteCount =
339 					    csio->dxfer_len;
340 				} else {
341 					/*
342 					 * Arrange things so that the S/G
343 					 * map will get set up automagically
344 					 */
345 					cm->cm_data = (void *)csio->data_ptr;
346 					cm->cm_datalen = csio->dxfer_len;
347 					cm->cm_sgtable = &srb->sg_map32;
348 				}
349 			} else {
350 				/* XXX Need to handle multiple s/g elements */
351 				panic("aac_cam: multiple s/g elements");
352 			}
353 		} else {
354 			srb->sg_map32.SgCount = 0;
355 			srb->sg_map32.SgEntry[0].SgByteCount = 0;
356 			srb->data_len = 0;
357 		}
358 
359 		break;
360 	}
361 	case XPT_RESET_DEV:
362 		if (!(sc->quirks & AAC_QUIRK_CAM_NORESET)) {
363 			srb->function = AAC_SRB_FUNC_RESET_DEVICE;
364 			break;
365 		} else {
366 			ccb->ccb_h.status = CAM_REQ_CMP;
367 			xpt_done(ccb);
368 			return;
369 		}
370 	default:
371 		break;
372 	}
373 
374 	srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
375 	srb->target = ccb->ccb_h.target_id;
376 	srb->lun = ccb->ccb_h.target_lun;
377 	srb->timeout = ccb->ccb_h.timeout;	/* XXX */
378 	srb->retry_limit = 0;
379 
380 	cm->cm_complete = aac_cam_complete;
381 	cm->cm_private = ccb;
382 	cm->cm_timestamp = time_second;
383 	cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
384 
385 	fib->Header.XferState =
386 	    AAC_FIBSTATE_HOSTOWNED	|
387 	    AAC_FIBSTATE_INITIALISED	|
388 	    AAC_FIBSTATE_FROMHOST	|
389 	    AAC_FIBSTATE_REXPECTED	|
390 	    AAC_FIBSTATE_NORM;
391 	fib->Header.Command = ScsiPortCommand;
392 	fib->Header.Size = sizeof(struct aac_fib_header) +
393 	    sizeof(struct aac_srb32);
394 
395 	aac_enqueue_ready(cm);
396 	aac_startio(cm->cm_sc);
397 
398 	return;
399 }
400 
401 static void
402 aac_cam_poll(struct cam_sim *sim)
403 {
404 	/*
405 	 * Pinging the interrupt routine isn't very safe, nor is it
406 	 * really necessary.  Do nothing.
407 	 */
408 }
409 
410 static void
411 aac_cam_complete(struct aac_command *cm)
412 {
413 	union	ccb *ccb;
414 	struct 	aac_srb_response *srbr;
415 	struct	aac_softc *sc;
416 
417 	debug_called(2);
418 
419 	sc = cm->cm_sc;
420 	ccb = cm->cm_private;
421 	srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
422 
423 	if (srbr->fib_status != 0) {
424 		device_printf(sc->aac_dev, "Passthru FIB failed!\n");
425 		ccb->ccb_h.status = CAM_REQ_ABORTED;
426 	} else {
427 		/*
428 		 * The SRB error codes just happen to match the CAM error
429 		 * codes.  How convienient!
430 		 */
431 		ccb->ccb_h.status = srbr->srb_status;
432 
433 		/* Take care of SCSI_IO ops. */
434 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
435 			u_int8_t command, device;
436 
437 			ccb->csio.scsi_status = srbr->scsi_status;
438 
439 			/* Take care of autosense */
440 			if (srbr->sense_len) {
441 				int sense_len, scsi_sense_len;
442 
443 				scsi_sense_len = sizeof(struct scsi_sense_data);
444 				bzero(&ccb->csio.sense_data, scsi_sense_len);
445 				sense_len = (srbr->sense_len >
446 				    scsi_sense_len) ? scsi_sense_len :
447 				    srbr->sense_len;
448 				bcopy(&srbr->sense[0], &ccb->csio.sense_data,
449 				    srbr->sense_len);
450 				ccb->csio.sense_len = sense_len;
451 				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
452 				scsi_sense_print(&ccb->csio);
453 			}
454 
455 			/* If this is an inquiry command, fake things out */
456 			if (ccb->ccb_h.flags & CAM_CDB_POINTER)
457 				command = ccb->csio.cdb_io.cdb_ptr[0];
458 			else
459 				command = ccb->csio.cdb_io.cdb_bytes[0];
460 
461 			if ((command == INQUIRY) &&
462 			    (ccb->ccb_h.status == CAM_REQ_CMP)) {
463 				device = ccb->csio.data_ptr[0] & 0x1f;
464 				/*
465 				 * We want DASD and PROC devices to only be
466 				 * visible through the pass device.
467 				 */
468 				if ((device == T_DIRECT) ||
469 				    (device == T_PROCESSOR) ||
470 				    (sc->quirks & AAC_QUIRK_CAM_PASSONLY))
471 					ccb->csio.data_ptr[0] =
472 					    ((device & 0xe0) | T_NODEVICE);
473 			}
474 		}
475 	}
476 
477 	aac_release_command(cm);
478 
479 	xpt_done(ccb);
480 
481 	return;
482 }
483 
484 static u_int32_t
485 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
486 {
487 	struct aac_fib *fib;
488 	struct aac_softc *sc;
489 	struct aac_cam *camsc;
490 	struct aac_vmioctl *vmi;
491 	struct aac_resetbus *rbc;
492 	int e;
493 
494 	camsc = (struct aac_cam *)cam_sim_softc(sim);
495 	sc = camsc->inf->aac_sc;
496 
497 	if (sc == NULL) {
498 		printf("Null sc?\n");
499 		return (CAM_REQ_ABORTED);
500 	}
501 
502 	aac_alloc_sync_fib(sc, &fib, 0);
503 
504 	vmi = (struct aac_vmioctl *)&fib->data[0];
505 	bzero(vmi, sizeof(struct aac_vmioctl));
506 
507 	vmi->Command = VM_Ioctl;
508 	vmi->ObjType = FT_DRIVE;
509 	vmi->MethId = sc->scsi_method_id;
510 	vmi->ObjId = 0;
511 	vmi->IoctlCmd = ResetBus;
512 
513 	rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
514 	rbc->BusNumber = camsc->inf->BusNumber;
515 
516 	e = aac_sync_fib(sc, ContainerCommand, 0, fib,
517 	    sizeof(struct aac_vmioctl));
518 	if (e) {
519 		device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
520 		    e);
521 		aac_release_sync_fib(sc);
522 		return (CAM_REQ_ABORTED);
523 	}
524 
525 	aac_release_sync_fib(sc);
526 	return (CAM_REQ_CMP);
527 }
528 
529 static u_int32_t
530 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
531 {
532 	return (CAM_UA_ABORT);
533 }
534 
535 static u_int32_t
536 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
537 {
538 	return (CAM_UA_TERMIO);
539 }
540 
541 static int
542 aac_cam_get_tran_settings(struct aac_softc *sc, struct ccb_trans_settings *cts, u_int32_t handle)
543 {
544 	struct aac_fib *fib;
545 	struct aac_vmioctl *vmi;
546 	struct aac_vmi_devinfo_resp *vmi_resp;
547 	int error;
548 
549 	aac_alloc_sync_fib(sc, &fib, 0);
550 	vmi = (struct aac_vmioctl *)&fib->data[0];
551 	bzero(vmi, sizeof(struct aac_vmioctl));
552 
553 	vmi->Command = VM_Ioctl;
554 	vmi->ObjType = FT_DRIVE;
555 	vmi->MethId = sc->scsi_method_id;
556 	vmi->ObjId = handle;
557 	vmi->IoctlCmd = GetDeviceProbeInfo;
558 
559 	error = aac_sync_fib(sc, ContainerCommand, 0, fib,
560 	    sizeof(struct aac_vmioctl));
561 	if (error) {
562 		device_printf(sc->aac_dev, "Error %d sending GetDeviceProbeInfo"
563 		              " command\n", error);
564 		aac_release_sync_fib(sc);
565 		return (CAM_REQ_INVALID);
566 	}
567 
568 	vmi_resp = (struct aac_vmi_devinfo_resp *)&fib->data[0];
569 	if (vmi_resp->Status != ST_OK) {
570 		/*
571 		 * The only reason why this command will return an error is
572 		 * if the requested device doesn't exist.
573 		 */
574 		debug(1, "GetDeviceProbeInfo returned %d\n", vmi_resp->Status);
575 		aac_release_sync_fib(sc);
576 		return (CAM_DEV_NOT_THERE);
577 	}
578 
579 	cts->bus_width = ((vmi_resp->Inquiry7 & 0x60) >> 5);
580 	cts->valid = CCB_TRANS_BUS_WIDTH_VALID;
581 
582 	if (vmi_resp->ScsiRate) {
583 		cts->sync_period =
584 		    scsi_calc_syncparam((10000 / vmi_resp->ScsiRate));
585 		cts->sync_offset = vmi_resp->ScsiOffset;
586 		cts->valid |= CCB_TRANS_SYNC_RATE_VALID		|
587 			      CCB_TRANS_SYNC_OFFSET_VALID;
588 	}
589 
590 	cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
591 	cts->valid |= CCB_TRANS_DISC_VALID		|
592 		      CCB_TRANS_TQ_VALID;
593 
594 	aac_release_sync_fib(sc);
595 	return (CAM_REQ_CMP);
596 }
597