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