FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-all.c
1 /*-
2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
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 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/8.4/sys/dev/ata/ata-all.c 247125 2013-02-21 20:29:43Z mav $");
29
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ata.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/endian.h>
37 #include <sys/ctype.h>
38 #include <sys/conf.h>
39 #include <sys/bus.h>
40 #include <sys/bio.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
43 #include <sys/sema.h>
44 #include <sys/taskqueue.h>
45 #include <vm/uma.h>
46 #include <machine/stdarg.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
49 #include <sys/rman.h>
50 #include <dev/ata/ata-all.h>
51 #include <ata_if.h>
52
53 #ifdef ATA_CAM
54 #include <cam/cam.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_debug.h>
59 #endif
60
61 #ifndef ATA_CAM
62 /* device structure */
63 static d_ioctl_t ata_ioctl;
64 static struct cdevsw ata_cdevsw = {
65 .d_version = D_VERSION,
66 .d_flags = D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
67 .d_ioctl = ata_ioctl,
68 .d_name = "ata",
69 };
70 #endif
71
72 /* prototypes */
73 #ifndef ATA_CAM
74 static void ata_boot_attach(void);
75 static device_t ata_add_child(device_t, struct ata_device *, int);
76 #else
77 static void ataaction(struct cam_sim *sim, union ccb *ccb);
78 static void atapoll(struct cam_sim *sim);
79 #endif
80 static void ata_conn_event(void *, int);
81 #ifndef ATA_CAM
82 static void bswap(int8_t *, int);
83 static void btrim(int8_t *, int);
84 static void bpack(int8_t *, int8_t *, int);
85 #endif
86 static void ata_interrupt_locked(void *data);
87 #ifdef ATA_CAM
88 static void ata_periodic_poll(void *data);
89 #endif
90
91 /* global vars */
92 MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
93 int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
94 #ifndef ATA_CAM
95 struct intr_config_hook *ata_delayed_attach = NULL;
96 #endif
97 devclass_t ata_devclass;
98 uma_zone_t ata_request_zone;
99 uma_zone_t ata_composite_zone;
100 #ifndef ATA_CAM
101 int ata_wc = 1;
102 int ata_setmax = 0;
103 #endif
104 int ata_dma_check_80pin = 1;
105
106 /* local vars */
107 #ifndef ATA_CAM
108 static int ata_dma = 1;
109 static int atapi_dma = 1;
110 #endif
111
112 /* sysctl vars */
113 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
114 #ifndef ATA_CAM
115 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
116 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
117 "ATA disk DMA mode control");
118 #endif
119 TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
120 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
121 CTLFLAG_RW, &ata_dma_check_80pin, 1,
122 "Check for 80pin cable before setting ATA DMA mode");
123 #ifndef ATA_CAM
124 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
125 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
126 "ATAPI device DMA mode control");
127 TUNABLE_INT("hw.ata.wc", &ata_wc);
128 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
129 "ATA disk write caching");
130 TUNABLE_INT("hw.ata.setmax", &ata_setmax);
131 SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
132 "ATA disk set max native address");
133 #endif
134 #ifdef ATA_CAM
135 FEATURE(ata_cam, "ATA devices are accessed through the cam(4) driver");
136 #endif
137
138 /*
139 * newbus device interface related functions
140 */
141 int
142 ata_probe(device_t dev)
143 {
144 return 0;
145 }
146
147 int
148 ata_attach(device_t dev)
149 {
150 struct ata_channel *ch = device_get_softc(dev);
151 int error, rid;
152 #ifdef ATA_CAM
153 struct cam_devq *devq;
154 const char *res;
155 char buf[64];
156 int i, mode;
157 #endif
158
159 /* check that we have a virgin channel to attach */
160 if (ch->r_irq)
161 return EEXIST;
162
163 /* initialize the softc basics */
164 ch->dev = dev;
165 ch->state = ATA_IDLE;
166 bzero(&ch->state_mtx, sizeof(struct mtx));
167 mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
168 bzero(&ch->queue_mtx, sizeof(struct mtx));
169 mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
170 TAILQ_INIT(&ch->ata_queue);
171 TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
172 #ifdef ATA_CAM
173 for (i = 0; i < 16; i++) {
174 ch->user[i].revision = 0;
175 snprintf(buf, sizeof(buf), "dev%d.sata_rev", i);
176 if (resource_int_value(device_get_name(dev),
177 device_get_unit(dev), buf, &mode) != 0 &&
178 resource_int_value(device_get_name(dev),
179 device_get_unit(dev), "sata_rev", &mode) != 0)
180 mode = -1;
181 if (mode >= 0)
182 ch->user[i].revision = mode;
183 ch->user[i].mode = 0;
184 snprintf(buf, sizeof(buf), "dev%d.mode", i);
185 if (resource_string_value(device_get_name(dev),
186 device_get_unit(dev), buf, &res) == 0)
187 mode = ata_str2mode(res);
188 else if (resource_string_value(device_get_name(dev),
189 device_get_unit(dev), "mode", &res) == 0)
190 mode = ata_str2mode(res);
191 else
192 mode = -1;
193 if (mode >= 0)
194 ch->user[i].mode = mode;
195 if (ch->flags & ATA_SATA)
196 ch->user[i].bytecount = 8192;
197 else
198 ch->user[i].bytecount = MAXPHYS;
199 ch->user[i].caps = 0;
200 ch->curr[i] = ch->user[i];
201 if (ch->pm_level > 0)
202 ch->user[i].caps |= CTS_SATA_CAPS_H_PMREQ;
203 if (ch->pm_level > 1)
204 ch->user[i].caps |= CTS_SATA_CAPS_D_PMREQ;
205 }
206 callout_init(&ch->poll_callout, 1);
207 #endif
208
209 #ifndef ATA_CAM
210 /* reset the controller HW, the channel and device(s) */
211 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
212 pause("ataatch", 1);
213 ATA_RESET(dev);
214 ATA_LOCKING(dev, ATA_LF_UNLOCK);
215 #endif
216
217 /* allocate DMA resources if DMA HW present*/
218 if (ch->dma.alloc)
219 ch->dma.alloc(dev);
220
221 /* setup interrupt delivery */
222 rid = ATA_IRQ_RID;
223 ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
224 RF_SHAREABLE | RF_ACTIVE);
225 if (!ch->r_irq) {
226 device_printf(dev, "unable to allocate interrupt\n");
227 return ENXIO;
228 }
229 if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
230 ata_interrupt, ch, &ch->ih))) {
231 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
232 device_printf(dev, "unable to setup interrupt\n");
233 return error;
234 }
235
236 #ifndef ATA_CAM
237 /* probe and attach devices on this channel unless we are in early boot */
238 if (!ata_delayed_attach)
239 ata_identify(dev);
240 return (0);
241 #else
242 if (ch->flags & ATA_PERIODIC_POLL)
243 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
244 mtx_lock(&ch->state_mtx);
245 /* Create the device queue for our SIM. */
246 devq = cam_simq_alloc(1);
247 if (devq == NULL) {
248 device_printf(dev, "Unable to allocate simq\n");
249 error = ENOMEM;
250 goto err1;
251 }
252 /* Construct SIM entry */
253 ch->sim = cam_sim_alloc(ataaction, atapoll, "ata", ch,
254 device_get_unit(dev), &ch->state_mtx, 1, 0, devq);
255 if (ch->sim == NULL) {
256 device_printf(dev, "unable to allocate sim\n");
257 cam_simq_free(devq);
258 error = ENOMEM;
259 goto err1;
260 }
261 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
262 device_printf(dev, "unable to register xpt bus\n");
263 error = ENXIO;
264 goto err2;
265 }
266 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
267 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
268 device_printf(dev, "unable to create path\n");
269 error = ENXIO;
270 goto err3;
271 }
272 mtx_unlock(&ch->state_mtx);
273 return (0);
274
275 err3:
276 xpt_bus_deregister(cam_sim_path(ch->sim));
277 err2:
278 cam_sim_free(ch->sim, /*free_devq*/TRUE);
279 ch->sim = NULL;
280 err1:
281 bus_release_resource(dev, SYS_RES_IRQ, rid, ch->r_irq);
282 mtx_unlock(&ch->state_mtx);
283 if (ch->flags & ATA_PERIODIC_POLL)
284 callout_drain(&ch->poll_callout);
285 return (error);
286 #endif
287 }
288
289 int
290 ata_detach(device_t dev)
291 {
292 struct ata_channel *ch = device_get_softc(dev);
293 #ifndef ATA_CAM
294 device_t *children;
295 int nchildren, i;
296 #endif
297
298 /* check that we have a valid channel to detach */
299 if (!ch->r_irq)
300 return ENXIO;
301
302 /* grap the channel lock so no new requests gets launched */
303 mtx_lock(&ch->state_mtx);
304 ch->state |= ATA_STALL_QUEUE;
305 mtx_unlock(&ch->state_mtx);
306 #ifdef ATA_CAM
307 if (ch->flags & ATA_PERIODIC_POLL)
308 callout_drain(&ch->poll_callout);
309 #endif
310
311 #ifndef ATA_CAM
312 /* detach & delete all children */
313 if (!device_get_children(dev, &children, &nchildren)) {
314 for (i = 0; i < nchildren; i++)
315 if (children[i])
316 device_delete_child(dev, children[i]);
317 free(children, M_TEMP);
318 }
319 #endif
320 taskqueue_drain(taskqueue_thread, &ch->conntask);
321
322 #ifdef ATA_CAM
323 mtx_lock(&ch->state_mtx);
324 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
325 xpt_free_path(ch->path);
326 xpt_bus_deregister(cam_sim_path(ch->sim));
327 cam_sim_free(ch->sim, /*free_devq*/TRUE);
328 ch->sim = NULL;
329 mtx_unlock(&ch->state_mtx);
330 #endif
331
332 /* release resources */
333 bus_teardown_intr(dev, ch->r_irq, ch->ih);
334 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
335 ch->r_irq = NULL;
336
337 /* free DMA resources if DMA HW present*/
338 if (ch->dma.free)
339 ch->dma.free(dev);
340
341 mtx_destroy(&ch->state_mtx);
342 mtx_destroy(&ch->queue_mtx);
343 return 0;
344 }
345
346 static void
347 ata_conn_event(void *context, int dummy)
348 {
349 device_t dev = (device_t)context;
350 #ifdef ATA_CAM
351 struct ata_channel *ch = device_get_softc(dev);
352 union ccb *ccb;
353
354 mtx_lock(&ch->state_mtx);
355 if (ch->sim == NULL) {
356 mtx_unlock(&ch->state_mtx);
357 return;
358 }
359 ata_reinit(dev);
360 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
361 return;
362 if (xpt_create_path(&ccb->ccb_h.path, NULL,
363 cam_sim_path(ch->sim),
364 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
365 xpt_free_ccb(ccb);
366 return;
367 }
368 xpt_rescan(ccb);
369 mtx_unlock(&ch->state_mtx);
370 #else
371 ata_reinit(dev);
372 #endif
373 }
374
375 int
376 ata_reinit(device_t dev)
377 {
378 struct ata_channel *ch = device_get_softc(dev);
379 struct ata_request *request;
380 #ifndef ATA_CAM
381 device_t *children;
382 int nchildren, i;
383
384 /* check that we have a valid channel to reinit */
385 if (!ch || !ch->r_irq)
386 return ENXIO;
387
388 if (bootverbose)
389 device_printf(dev, "reiniting channel ..\n");
390
391 /* poll for locking the channel */
392 while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
393 pause("atarini", 1);
394
395 /* catch eventual request in ch->running */
396 mtx_lock(&ch->state_mtx);
397 if (ch->state & ATA_STALL_QUEUE) {
398 /* Recursive reinits and reinits during detach prohobited. */
399 mtx_unlock(&ch->state_mtx);
400 return (ENXIO);
401 }
402 if ((request = ch->running))
403 callout_stop(&request->callout);
404 ch->running = NULL;
405
406 /* unconditionally grap the channel lock */
407 ch->state |= ATA_STALL_QUEUE;
408 mtx_unlock(&ch->state_mtx);
409
410 /* reset the controller HW, the channel and device(s) */
411 ATA_RESET(dev);
412
413 /* reinit the children and delete any that fails */
414 if (!device_get_children(dev, &children, &nchildren)) {
415 mtx_lock(&Giant); /* newbus suckage it needs Giant */
416 for (i = 0; i < nchildren; i++) {
417 /* did any children go missing ? */
418 if (children[i] && device_is_attached(children[i]) &&
419 ATA_REINIT(children[i])) {
420 /*
421 * if we had a running request and its device matches
422 * this child we need to inform the request that the
423 * device is gone.
424 */
425 if (request && request->dev == children[i]) {
426 request->result = ENXIO;
427 device_printf(request->dev, "FAILURE - device detached\n");
428
429 /* if not timeout finish request here */
430 if (!(request->flags & ATA_R_TIMEOUT))
431 ata_finish(request);
432 request = NULL;
433 }
434 device_delete_child(dev, children[i]);
435 }
436 }
437 free(children, M_TEMP);
438 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
439 }
440
441 /* if we still have a good request put it on the queue again */
442 if (request && !(request->flags & ATA_R_TIMEOUT)) {
443 device_printf(request->dev,
444 "WARNING - %s requeued due to channel reset",
445 ata_cmd2str(request));
446 if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
447 printf(" LBA=%ju", request->u.ata.lba);
448 printf("\n");
449 request->flags |= ATA_R_REQUEUE;
450 ata_queue_request(request);
451 }
452
453 /* we're done release the channel for new work */
454 mtx_lock(&ch->state_mtx);
455 ch->state = ATA_IDLE;
456 mtx_unlock(&ch->state_mtx);
457 ATA_LOCKING(dev, ATA_LF_UNLOCK);
458
459 /* Add new children. */
460 /* ata_identify(dev); */
461
462 if (bootverbose)
463 device_printf(dev, "reinit done ..\n");
464
465 /* kick off requests on the queue */
466 ata_start(dev);
467 #else
468 xpt_freeze_simq(ch->sim, 1);
469 if ((request = ch->running)) {
470 ch->running = NULL;
471 if (ch->state == ATA_ACTIVE)
472 ch->state = ATA_IDLE;
473 callout_stop(&request->callout);
474 if (ch->dma.unload)
475 ch->dma.unload(request);
476 request->result = ERESTART;
477 ata_cam_end_transaction(dev, request);
478 }
479 /* reset the controller HW, the channel and device(s) */
480 ATA_RESET(dev);
481 /* Tell the XPT about the event */
482 xpt_async(AC_BUS_RESET, ch->path, NULL);
483 xpt_release_simq(ch->sim, TRUE);
484 #endif
485 return(0);
486 }
487
488 int
489 ata_suspend(device_t dev)
490 {
491 struct ata_channel *ch;
492
493 /* check for valid device */
494 if (!dev || !(ch = device_get_softc(dev)))
495 return ENXIO;
496
497 #ifdef ATA_CAM
498 if (ch->flags & ATA_PERIODIC_POLL)
499 callout_drain(&ch->poll_callout);
500 mtx_lock(&ch->state_mtx);
501 xpt_freeze_simq(ch->sim, 1);
502 while (ch->state != ATA_IDLE)
503 msleep(ch, &ch->state_mtx, PRIBIO, "atasusp", hz/100);
504 mtx_unlock(&ch->state_mtx);
505 #else
506 /* wait for the channel to be IDLE or detached before suspending */
507 while (ch->r_irq) {
508 mtx_lock(&ch->state_mtx);
509 if (ch->state == ATA_IDLE) {
510 ch->state = ATA_ACTIVE;
511 mtx_unlock(&ch->state_mtx);
512 break;
513 }
514 mtx_unlock(&ch->state_mtx);
515 tsleep(ch, PRIBIO, "atasusp", hz/10);
516 }
517 ATA_LOCKING(dev, ATA_LF_UNLOCK);
518 #endif
519 return(0);
520 }
521
522 int
523 ata_resume(device_t dev)
524 {
525 struct ata_channel *ch;
526 int error;
527
528 /* check for valid device */
529 if (!dev || !(ch = device_get_softc(dev)))
530 return ENXIO;
531
532 #ifdef ATA_CAM
533 mtx_lock(&ch->state_mtx);
534 error = ata_reinit(dev);
535 xpt_release_simq(ch->sim, TRUE);
536 mtx_unlock(&ch->state_mtx);
537 if (ch->flags & ATA_PERIODIC_POLL)
538 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
539 #else
540 /* reinit the devices, we dont know what mode/state they are in */
541 error = ata_reinit(dev);
542 /* kick off requests on the queue */
543 ata_start(dev);
544 #endif
545 return error;
546 }
547
548 void
549 ata_interrupt(void *data)
550 {
551 #ifdef ATA_CAM
552 struct ata_channel *ch = (struct ata_channel *)data;
553
554 mtx_lock(&ch->state_mtx);
555 xpt_batch_start(ch->sim);
556 #endif
557 ata_interrupt_locked(data);
558 #ifdef ATA_CAM
559 xpt_batch_done(ch->sim);
560 mtx_unlock(&ch->state_mtx);
561 #endif
562 }
563
564 static void
565 ata_interrupt_locked(void *data)
566 {
567 struct ata_channel *ch = (struct ata_channel *)data;
568 struct ata_request *request;
569
570 #ifndef ATA_CAM
571 mtx_lock(&ch->state_mtx);
572 #endif
573 do {
574 /* ignore interrupt if its not for us */
575 if (ch->hw.status && !ch->hw.status(ch->dev))
576 break;
577
578 /* do we have a running request */
579 if (!(request = ch->running))
580 break;
581
582 ATA_DEBUG_RQ(request, "interrupt");
583
584 /* safetycheck for the right state */
585 if (ch->state == ATA_IDLE) {
586 device_printf(request->dev, "interrupt on idle channel ignored\n");
587 break;
588 }
589
590 /*
591 * we have the HW locks, so end the transaction for this request
592 * if it finishes immediately otherwise wait for next interrupt
593 */
594 if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
595 ch->running = NULL;
596 if (ch->state == ATA_ACTIVE)
597 ch->state = ATA_IDLE;
598 #ifdef ATA_CAM
599 ata_cam_end_transaction(ch->dev, request);
600 #else
601 mtx_unlock(&ch->state_mtx);
602 ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
603 ata_finish(request);
604 #endif
605 return;
606 }
607 } while (0);
608 #ifndef ATA_CAM
609 mtx_unlock(&ch->state_mtx);
610 #endif
611 }
612
613 #ifdef ATA_CAM
614 static void
615 ata_periodic_poll(void *data)
616 {
617 struct ata_channel *ch = (struct ata_channel *)data;
618
619 callout_reset(&ch->poll_callout, hz, ata_periodic_poll, ch);
620 ata_interrupt(ch);
621 }
622 #endif
623
624 void
625 ata_print_cable(device_t dev, u_int8_t *who)
626 {
627 device_printf(dev,
628 "DMA limited to UDMA33, %s found non-ATA66 cable\n", who);
629 }
630
631 #ifndef ATA_CAM
632 int
633 ata_check_80pin(device_t dev, int mode)
634 {
635 struct ata_device *atadev = device_get_softc(dev);
636
637 if (!ata_dma_check_80pin) {
638 if (bootverbose)
639 device_printf(dev, "Skipping 80pin cable check\n");
640 return mode;
641 }
642
643 if (mode > ATA_UDMA2 && !(atadev->param.hwres & ATA_CABLE_ID)) {
644 ata_print_cable(dev, "device");
645 mode = ATA_UDMA2;
646 }
647 return mode;
648 }
649 #endif
650
651 #ifndef ATA_CAM
652 void
653 ata_setmode(device_t dev)
654 {
655 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
656 struct ata_device *atadev = device_get_softc(dev);
657 int error, mode, pmode;
658
659 mode = atadev->mode;
660 do {
661 pmode = mode = ata_limit_mode(dev, mode, ATA_DMA_MAX);
662 mode = ATA_SETMODE(device_get_parent(dev), atadev->unit, mode);
663 if ((ch->flags & (ATA_CHECKS_CABLE | ATA_SATA)) == 0)
664 mode = ata_check_80pin(dev, mode);
665 } while (pmode != mode); /* Interate till successfull negotiation. */
666 error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
667 if (bootverbose)
668 device_printf(dev, "%ssetting %s\n",
669 (error) ? "FAILURE " : "", ata_mode2str(mode));
670 atadev->mode = mode;
671 }
672 #endif
673
674 /*
675 * device related interfaces
676 */
677 #ifndef ATA_CAM
678 static int
679 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
680 int32_t flag, struct thread *td)
681 {
682 device_t device, *children;
683 struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
684 int *value = (int *)data;
685 int i, nchildren, error = ENOTTY;
686
687 switch (cmd) {
688 case IOCATAGMAXCHANNEL:
689 /* In case we have channel 0..n this will return n+1. */
690 *value = devclass_get_maxunit(ata_devclass);
691 error = 0;
692 break;
693
694 case IOCATAREINIT:
695 if (*value >= devclass_get_maxunit(ata_devclass) ||
696 !(device = devclass_get_device(ata_devclass, *value)) ||
697 !device_is_attached(device))
698 return ENXIO;
699 error = ata_reinit(device);
700 break;
701
702 case IOCATAATTACH:
703 if (*value >= devclass_get_maxunit(ata_devclass) ||
704 !(device = devclass_get_device(ata_devclass, *value)) ||
705 !device_is_attached(device))
706 return ENXIO;
707 error = DEVICE_ATTACH(device);
708 break;
709
710 case IOCATADETACH:
711 if (*value >= devclass_get_maxunit(ata_devclass) ||
712 !(device = devclass_get_device(ata_devclass, *value)) ||
713 !device_is_attached(device))
714 return ENXIO;
715 error = DEVICE_DETACH(device);
716 break;
717
718 case IOCATADEVICES:
719 if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
720 !(device = devclass_get_device(ata_devclass, devices->channel)) ||
721 !device_is_attached(device))
722 return ENXIO;
723 bzero(devices->name[0], 32);
724 bzero(&devices->params[0], sizeof(struct ata_params));
725 bzero(devices->name[1], 32);
726 bzero(&devices->params[1], sizeof(struct ata_params));
727 if (!device_get_children(device, &children, &nchildren)) {
728 for (i = 0; i < nchildren; i++) {
729 if (children[i] && device_is_attached(children[i])) {
730 struct ata_device *atadev = device_get_softc(children[i]);
731
732 if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
733 strncpy(devices->name[0],
734 device_get_nameunit(children[i]), 32);
735 bcopy(&atadev->param, &devices->params[0],
736 sizeof(struct ata_params));
737 }
738 if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
739 strncpy(devices->name[1],
740 device_get_nameunit(children[i]), 32);
741 bcopy(&atadev->param, &devices->params[1],
742 sizeof(struct ata_params));
743 }
744 }
745 }
746 free(children, M_TEMP);
747 error = 0;
748 }
749 else
750 error = ENODEV;
751 break;
752
753 default:
754 if (ata_raid_ioctl_func)
755 error = ata_raid_ioctl_func(cmd, data);
756 }
757 return error;
758 }
759 #endif
760
761 #ifndef ATA_CAM
762 int
763 ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
764 {
765 struct ata_device *atadev = device_get_softc(dev);
766 struct ata_channel *ch = device_get_softc(device_get_parent(dev));
767 struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
768 struct ata_params *params = (struct ata_params *)data;
769 int *mode = (int *)data;
770 struct ata_request *request;
771 caddr_t buf;
772 int error;
773
774 switch (cmd) {
775 case IOCATAREQUEST:
776 if (ioc_request->count >
777 (ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS)) {
778 return (EFBIG);
779 }
780 if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
781 return ENOMEM;
782 }
783 if (!(request = ata_alloc_request())) {
784 free(buf, M_ATA);
785 return ENOMEM;
786 }
787 request->dev = atadev->dev;
788 if (ioc_request->flags & ATA_CMD_WRITE) {
789 error = copyin(ioc_request->data, buf, ioc_request->count);
790 if (error) {
791 free(buf, M_ATA);
792 ata_free_request(request);
793 return error;
794 }
795 }
796 if (ioc_request->flags & ATA_CMD_ATAPI) {
797 request->flags = ATA_R_ATAPI;
798 bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
799 }
800 else {
801 request->u.ata.command = ioc_request->u.ata.command;
802 request->u.ata.feature = ioc_request->u.ata.feature;
803 request->u.ata.lba = ioc_request->u.ata.lba;
804 request->u.ata.count = ioc_request->u.ata.count;
805 }
806 request->timeout = ioc_request->timeout;
807 request->data = buf;
808 request->bytecount = ioc_request->count;
809 request->transfersize = request->bytecount;
810 if (ioc_request->flags & ATA_CMD_CONTROL)
811 request->flags |= ATA_R_CONTROL;
812 if (ioc_request->flags & ATA_CMD_READ)
813 request->flags |= ATA_R_READ;
814 if (ioc_request->flags & ATA_CMD_WRITE)
815 request->flags |= ATA_R_WRITE;
816 ata_queue_request(request);
817 if (request->flags & ATA_R_ATAPI) {
818 bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
819 sizeof(struct atapi_sense));
820 }
821 else {
822 ioc_request->u.ata.command = request->u.ata.command;
823 ioc_request->u.ata.feature = request->u.ata.feature;
824 ioc_request->u.ata.lba = request->u.ata.lba;
825 ioc_request->u.ata.count = request->u.ata.count;
826 }
827 ioc_request->error = request->result;
828 if (ioc_request->flags & ATA_CMD_READ)
829 error = copyout(buf, ioc_request->data, ioc_request->count);
830 else
831 error = 0;
832 free(buf, M_ATA);
833 ata_free_request(request);
834 return error;
835
836 case IOCATAGPARM:
837 ata_getparam(atadev, 0);
838 bcopy(&atadev->param, params, sizeof(struct ata_params));
839 return 0;
840
841 case IOCATASMODE:
842 atadev->mode = *mode;
843 ata_setmode(dev);
844 return 0;
845
846 case IOCATAGMODE:
847 *mode = atadev->mode |
848 (ATA_GETREV(device_get_parent(dev), atadev->unit) << 8);
849 return 0;
850 case IOCATASSPINDOWN:
851 atadev->spindown = *mode;
852 return 0;
853 case IOCATAGSPINDOWN:
854 *mode = atadev->spindown;
855 return 0;
856 default:
857 return ENOTTY;
858 }
859 }
860 #endif
861
862 #ifndef ATA_CAM
863 static void
864 ata_boot_attach(void)
865 {
866 struct ata_channel *ch;
867 int ctlr;
868
869 mtx_lock(&Giant); /* newbus suckage it needs Giant */
870
871 /* kick off probe and attach on all channels */
872 for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
873 if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
874 ata_identify(ch->dev);
875 }
876 }
877
878 /* release the hook that got us here, we are only needed once during boot */
879 if (ata_delayed_attach) {
880 config_intrhook_disestablish(ata_delayed_attach);
881 free(ata_delayed_attach, M_TEMP);
882 ata_delayed_attach = NULL;
883 }
884
885 mtx_unlock(&Giant); /* newbus suckage dealt with, release Giant */
886 }
887 #endif
888
889 /*
890 * misc support functions
891 */
892 #ifndef ATA_CAM
893 static device_t
894 ata_add_child(device_t parent, struct ata_device *atadev, int unit)
895 {
896 device_t child;
897
898 if ((child = device_add_child(parent, (unit < 0) ? NULL : "ad", unit))) {
899 device_set_softc(child, atadev);
900 device_quiet(child);
901 atadev->dev = child;
902 atadev->max_iosize = DEV_BSIZE;
903 atadev->mode = ATA_PIO_MAX;
904 }
905 return child;
906 }
907 #endif
908
909 #ifndef ATA_CAM
910 int
911 ata_getparam(struct ata_device *atadev, int init)
912 {
913 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
914 struct ata_request *request;
915 const char *res;
916 char buf[64];
917 u_int8_t command = 0;
918 int error = ENOMEM, retries = 2, mode = -1;
919
920 if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
921 command = ATA_ATA_IDENTIFY;
922 if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
923 command = ATA_ATAPI_IDENTIFY;
924 if (!command)
925 return ENXIO;
926
927 while (retries-- > 0 && error) {
928 if (!(request = ata_alloc_request()))
929 break;
930 request->dev = atadev->dev;
931 request->timeout = 1;
932 request->retries = 0;
933 request->u.ata.command = command;
934 request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_DIRECT);
935 if (!bootverbose)
936 request->flags |= ATA_R_QUIET;
937 request->data = (void *)&atadev->param;
938 request->bytecount = sizeof(struct ata_params);
939 request->donecount = 0;
940 request->transfersize = DEV_BSIZE;
941 ata_queue_request(request);
942 error = request->result;
943 ata_free_request(request);
944 }
945
946 if (!error && (isprint(atadev->param.model[0]) ||
947 isprint(atadev->param.model[1]))) {
948 struct ata_params *atacap = &atadev->param;
949 int16_t *ptr;
950
951 for (ptr = (int16_t *)atacap;
952 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
953 *ptr = le16toh(*ptr);
954 }
955 if (!(!strncmp(atacap->model, "FX", 2) ||
956 !strncmp(atacap->model, "NEC", 3) ||
957 !strncmp(atacap->model, "Pioneer", 7) ||
958 !strncmp(atacap->model, "SHARP", 5))) {
959 bswap(atacap->model, sizeof(atacap->model));
960 bswap(atacap->revision, sizeof(atacap->revision));
961 bswap(atacap->serial, sizeof(atacap->serial));
962 }
963 btrim(atacap->model, sizeof(atacap->model));
964 bpack(atacap->model, atacap->model, sizeof(atacap->model));
965 btrim(atacap->revision, sizeof(atacap->revision));
966 bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
967 btrim(atacap->serial, sizeof(atacap->serial));
968 bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
969
970 if (bootverbose)
971 printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
972 device_get_unit(ch->dev),
973 ata_unit2str(atadev),
974 ata_mode2str(ata_pmode(atacap)),
975 ata_mode2str(ata_wmode(atacap)),
976 ata_mode2str(ata_umode(atacap)),
977 (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
978
979 if (init) {
980 char buffer[64];
981
982 sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
983 device_set_desc_copy(atadev->dev, buffer);
984 if ((atadev->param.config & ATA_PROTO_ATAPI) &&
985 (atadev->param.config != ATA_CFA_MAGIC1) &&
986 (atadev->param.config != ATA_CFA_MAGIC2)) {
987 if (atapi_dma &&
988 (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
989 ata_umode(&atadev->param) >= ATA_UDMA2)
990 atadev->mode = ATA_DMA_MAX;
991 }
992 else {
993 if (ata_dma &&
994 (ata_umode(&atadev->param) > 0 ||
995 ata_wmode(&atadev->param) > 0))
996 atadev->mode = ATA_DMA_MAX;
997 }
998 snprintf(buf, sizeof(buf), "dev%d.mode", atadev->unit);
999 if (resource_string_value(device_get_name(ch->dev),
1000 device_get_unit(ch->dev), buf, &res) == 0)
1001 mode = ata_str2mode(res);
1002 else if (resource_string_value(device_get_name(ch->dev),
1003 device_get_unit(ch->dev), "mode", &res) == 0)
1004 mode = ata_str2mode(res);
1005 if (mode >= 0)
1006 atadev->mode = mode;
1007 }
1008 }
1009 else {
1010 if (!error)
1011 error = ENXIO;
1012 }
1013 return error;
1014 }
1015 #endif
1016
1017 #ifndef ATA_CAM
1018 int
1019 ata_identify(device_t dev)
1020 {
1021 struct ata_channel *ch = device_get_softc(dev);
1022 struct ata_device *atadev;
1023 device_t *children;
1024 device_t child, master = NULL;
1025 int nchildren, i, n = ch->devices;
1026
1027 if (bootverbose)
1028 device_printf(dev, "Identifying devices: %08x\n", ch->devices);
1029
1030 mtx_lock(&Giant);
1031 /* Skip existing devices. */
1032 if (!device_get_children(dev, &children, &nchildren)) {
1033 for (i = 0; i < nchildren; i++) {
1034 if (children[i] && (atadev = device_get_softc(children[i])))
1035 n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
1036 }
1037 free(children, M_TEMP);
1038 }
1039 /* Create new devices. */
1040 if (bootverbose)
1041 device_printf(dev, "New devices: %08x\n", n);
1042 if (n == 0) {
1043 mtx_unlock(&Giant);
1044 return (0);
1045 }
1046 for (i = 0; i < ATA_PM; ++i) {
1047 if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
1048 int unit = -1;
1049
1050 if (!(atadev = malloc(sizeof(struct ata_device),
1051 M_ATA, M_NOWAIT | M_ZERO))) {
1052 device_printf(dev, "out of memory\n");
1053 return ENOMEM;
1054 }
1055 atadev->unit = i;
1056 #ifdef ATA_STATIC_ID
1057 if (n & (ATA_ATA_MASTER << i))
1058 unit = (device_get_unit(dev) << 1) + i;
1059 #endif
1060 if ((child = ata_add_child(dev, atadev, unit))) {
1061 /*
1062 * PATA slave should be identified first, to allow
1063 * device cable detection on master to work properly.
1064 */
1065 if (i == 0 && (n & ATA_PORTMULTIPLIER) == 0 &&
1066 (n & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << 1)) != 0) {
1067 master = child;
1068 continue;
1069 }
1070 if (ata_getparam(atadev, 1)) {
1071 device_delete_child(dev, child);
1072 free(atadev, M_ATA);
1073 }
1074 }
1075 else
1076 free(atadev, M_ATA);
1077 }
1078 }
1079 if (master) {
1080 atadev = device_get_softc(master);
1081 if (ata_getparam(atadev, 1)) {
1082 device_delete_child(dev, master);
1083 free(atadev, M_ATA);
1084 }
1085 }
1086 bus_generic_probe(dev);
1087 bus_generic_attach(dev);
1088 mtx_unlock(&Giant);
1089 return 0;
1090 }
1091 #endif
1092
1093 void
1094 ata_default_registers(device_t dev)
1095 {
1096 struct ata_channel *ch = device_get_softc(dev);
1097
1098 /* fill in the defaults from whats setup already */
1099 ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
1100 ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
1101 ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
1102 ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
1103 ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
1104 ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
1105 ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
1106 ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
1107 }
1108
1109 void
1110 ata_modify_if_48bit(struct ata_request *request)
1111 {
1112 struct ata_channel *ch = device_get_softc(request->parent);
1113 struct ata_device *atadev = device_get_softc(request->dev);
1114
1115 request->flags &= ~ATA_R_48BIT;
1116
1117 if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
1118 request->u.ata.count > 256) &&
1119 atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1120
1121 /* translate command into 48bit version */
1122 switch (request->u.ata.command) {
1123 case ATA_READ:
1124 request->u.ata.command = ATA_READ48;
1125 break;
1126 case ATA_READ_MUL:
1127 request->u.ata.command = ATA_READ_MUL48;
1128 break;
1129 case ATA_READ_DMA:
1130 if (ch->flags & ATA_NO_48BIT_DMA) {
1131 if (request->transfersize > DEV_BSIZE)
1132 request->u.ata.command = ATA_READ_MUL48;
1133 else
1134 request->u.ata.command = ATA_READ48;
1135 request->flags &= ~ATA_R_DMA;
1136 }
1137 else
1138 request->u.ata.command = ATA_READ_DMA48;
1139 break;
1140 case ATA_READ_DMA_QUEUED:
1141 if (ch->flags & ATA_NO_48BIT_DMA) {
1142 if (request->transfersize > DEV_BSIZE)
1143 request->u.ata.command = ATA_READ_MUL48;
1144 else
1145 request->u.ata.command = ATA_READ48;
1146 request->flags &= ~ATA_R_DMA;
1147 }
1148 else
1149 request->u.ata.command = ATA_READ_DMA_QUEUED48;
1150 break;
1151 case ATA_WRITE:
1152 request->u.ata.command = ATA_WRITE48;
1153 break;
1154 case ATA_WRITE_MUL:
1155 request->u.ata.command = ATA_WRITE_MUL48;
1156 break;
1157 case ATA_WRITE_DMA:
1158 if (ch->flags & ATA_NO_48BIT_DMA) {
1159 if (request->transfersize > DEV_BSIZE)
1160 request->u.ata.command = ATA_WRITE_MUL48;
1161 else
1162 request->u.ata.command = ATA_WRITE48;
1163 request->flags &= ~ATA_R_DMA;
1164 }
1165 else
1166 request->u.ata.command = ATA_WRITE_DMA48;
1167 break;
1168 case ATA_WRITE_DMA_QUEUED:
1169 if (ch->flags & ATA_NO_48BIT_DMA) {
1170 if (request->transfersize > DEV_BSIZE)
1171 request->u.ata.command = ATA_WRITE_MUL48;
1172 else
1173 request->u.ata.command = ATA_WRITE48;
1174 request->u.ata.command = ATA_WRITE48;
1175 request->flags &= ~ATA_R_DMA;
1176 }
1177 else
1178 request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
1179 break;
1180 case ATA_FLUSHCACHE:
1181 request->u.ata.command = ATA_FLUSHCACHE48;
1182 break;
1183 case ATA_SET_MAX_ADDRESS:
1184 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1185 break;
1186 default:
1187 return;
1188 }
1189 request->flags |= ATA_R_48BIT;
1190 }
1191 else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
1192
1193 /* translate command into 48bit version */
1194 switch (request->u.ata.command) {
1195 case ATA_FLUSHCACHE:
1196 request->u.ata.command = ATA_FLUSHCACHE48;
1197 break;
1198 case ATA_READ_NATIVE_MAX_ADDRESS:
1199 request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
1200 break;
1201 case ATA_SET_MAX_ADDRESS:
1202 request->u.ata.command = ATA_SET_MAX_ADDRESS48;
1203 break;
1204 default:
1205 return;
1206 }
1207 request->flags |= ATA_R_48BIT;
1208 }
1209 }
1210
1211 void
1212 ata_udelay(int interval)
1213 {
1214 /* for now just use DELAY, the timer/sleep subsytems are not there yet */
1215 if (1 || interval < (1000000/hz) || ata_delayed_attach)
1216 DELAY(interval);
1217 else
1218 pause("ataslp", interval/(1000000/hz));
1219 }
1220
1221 #ifndef ATA_CAM
1222 const char *
1223 ata_unit2str(struct ata_device *atadev)
1224 {
1225 struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
1226 static char str[8];
1227
1228 if (ch->devices & ATA_PORTMULTIPLIER)
1229 sprintf(str, "port%d", atadev->unit);
1230 else
1231 sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
1232 return str;
1233 }
1234 #endif
1235
1236 const char *
1237 ata_mode2str(int mode)
1238 {
1239 switch (mode) {
1240 case -1: return "UNSUPPORTED";
1241 case ATA_PIO0: return "PIO0";
1242 case ATA_PIO1: return "PIO1";
1243 case ATA_PIO2: return "PIO2";
1244 case ATA_PIO3: return "PIO3";
1245 case ATA_PIO4: return "PIO4";
1246 case ATA_WDMA0: return "WDMA0";
1247 case ATA_WDMA1: return "WDMA1";
1248 case ATA_WDMA2: return "WDMA2";
1249 case ATA_UDMA0: return "UDMA16";
1250 case ATA_UDMA1: return "UDMA25";
1251 case ATA_UDMA2: return "UDMA33";
1252 case ATA_UDMA3: return "UDMA40";
1253 case ATA_UDMA4: return "UDMA66";
1254 case ATA_UDMA5: return "UDMA100";
1255 case ATA_UDMA6: return "UDMA133";
1256 case ATA_SA150: return "SATA150";
1257 case ATA_SA300: return "SATA300";
1258 default:
1259 if (mode & ATA_DMA_MASK)
1260 return "BIOSDMA";
1261 else
1262 return "BIOSPIO";
1263 }
1264 }
1265
1266 int
1267 ata_str2mode(const char *str)
1268 {
1269
1270 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
1271 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
1272 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
1273 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
1274 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
1275 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
1276 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
1277 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
1278 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
1279 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
1280 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
1281 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
1282 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
1283 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
1284 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
1285 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
1286 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
1287 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
1288 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
1289 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
1290 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
1291 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
1292 return (-1);
1293 }
1294
1295 #ifndef ATA_CAM
1296 const char *
1297 ata_satarev2str(int rev)
1298 {
1299 switch (rev) {
1300 case 0: return "";
1301 case 1: return "SATA 1.5Gb/s";
1302 case 2: return "SATA 3Gb/s";
1303 case 3: return "SATA 6Gb/s";
1304 case 0xff: return "SATA";
1305 default: return "???";
1306 }
1307 }
1308 #endif
1309
1310 int
1311 ata_atapi(device_t dev, int target)
1312 {
1313 struct ata_channel *ch = device_get_softc(dev);
1314
1315 return (ch->devices & (ATA_ATAPI_MASTER << target));
1316 }
1317
1318 #ifndef ATA_CAM
1319 int
1320 ata_pmode(struct ata_params *ap)
1321 {
1322 if (ap->atavalid & ATA_FLAG_64_70) {
1323 if (ap->apiomodes & 0x02)
1324 return ATA_PIO4;
1325 if (ap->apiomodes & 0x01)
1326 return ATA_PIO3;
1327 }
1328 if (ap->mwdmamodes & 0x04)
1329 return ATA_PIO4;
1330 if (ap->mwdmamodes & 0x02)
1331 return ATA_PIO3;
1332 if (ap->mwdmamodes & 0x01)
1333 return ATA_PIO2;
1334 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1335 return ATA_PIO2;
1336 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1337 return ATA_PIO1;
1338 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1339 return ATA_PIO0;
1340 return ATA_PIO0;
1341 }
1342 #endif
1343
1344 #ifndef ATA_CAM
1345 int
1346 ata_wmode(struct ata_params *ap)
1347 {
1348 if (ap->mwdmamodes & 0x04)
1349 return ATA_WDMA2;
1350 if (ap->mwdmamodes & 0x02)
1351 return ATA_WDMA1;
1352 if (ap->mwdmamodes & 0x01)
1353 return ATA_WDMA0;
1354 return -1;
1355 }
1356 #endif
1357
1358 #ifndef ATA_CAM
1359 int
1360 ata_umode(struct ata_params *ap)
1361 {
1362 if (ap->atavalid & ATA_FLAG_88) {
1363 if (ap->udmamodes & 0x40)
1364 return ATA_UDMA6;
1365 if (ap->udmamodes & 0x20)
1366 return ATA_UDMA5;
1367 if (ap->udmamodes & 0x10)
1368 return ATA_UDMA4;
1369 if (ap->udmamodes & 0x08)
1370 return ATA_UDMA3;
1371 if (ap->udmamodes & 0x04)
1372 return ATA_UDMA2;
1373 if (ap->udmamodes & 0x02)
1374 return ATA_UDMA1;
1375 if (ap->udmamodes & 0x01)
1376 return ATA_UDMA0;
1377 }
1378 return -1;
1379 }
1380 #endif
1381
1382 #ifndef ATA_CAM
1383 int
1384 ata_limit_mode(device_t dev, int mode, int maxmode)
1385 {
1386 struct ata_device *atadev = device_get_softc(dev);
1387
1388 if (maxmode && mode > maxmode)
1389 mode = maxmode;
1390
1391 if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1392 return min(mode, ata_umode(&atadev->param));
1393
1394 if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1395 return min(mode, ata_wmode(&atadev->param));
1396
1397 if (mode > ata_pmode(&atadev->param))
1398 return min(mode, ata_pmode(&atadev->param));
1399
1400 return mode;
1401 }
1402 #endif
1403
1404 #ifndef ATA_CAM
1405 static void
1406 bswap(int8_t *buf, int len)
1407 {
1408 u_int16_t *ptr = (u_int16_t*)(buf + len);
1409
1410 while (--ptr >= (u_int16_t*)buf)
1411 *ptr = ntohs(*ptr);
1412 }
1413 #endif
1414
1415 #ifndef ATA_CAM
1416 static void
1417 btrim(int8_t *buf, int len)
1418 {
1419 int8_t *ptr;
1420
1421 for (ptr = buf; ptr < buf+len; ++ptr)
1422 if (!*ptr || *ptr == '_')
1423 *ptr = ' ';
1424 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1425 *ptr = 0;
1426 }
1427 #endif
1428
1429 #ifndef ATA_CAM
1430 static void
1431 bpack(int8_t *src, int8_t *dst, int len)
1432 {
1433 int i, j, blank;
1434
1435 for (i = j = blank = 0 ; i < len; i++) {
1436 if (blank && src[i] == ' ') continue;
1437 if (blank && src[i] != ' ') {
1438 dst[j++] = src[i];
1439 blank = 0;
1440 continue;
1441 }
1442 if (src[i] == ' ') {
1443 blank = 1;
1444 if (i == 0)
1445 continue;
1446 }
1447 dst[j++] = src[i];
1448 }
1449 if (j < len)
1450 dst[j] = 0x00;
1451 }
1452 #endif
1453
1454 #ifdef ATA_CAM
1455 void
1456 ata_cam_begin_transaction(device_t dev, union ccb *ccb)
1457 {
1458 struct ata_channel *ch = device_get_softc(dev);
1459 struct ata_request *request;
1460
1461 if (!(request = ata_alloc_request())) {
1462 device_printf(dev, "FAILURE - out of memory in start\n");
1463 ccb->ccb_h.status = CAM_REQ_INVALID;
1464 xpt_done(ccb);
1465 return;
1466 }
1467 bzero(request, sizeof(*request));
1468
1469 /* setup request */
1470 request->dev = NULL;
1471 request->parent = dev;
1472 request->unit = ccb->ccb_h.target_id;
1473 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1474 request->data = ccb->ataio.data_ptr;
1475 request->bytecount = ccb->ataio.dxfer_len;
1476 request->u.ata.command = ccb->ataio.cmd.command;
1477 request->u.ata.feature = ((uint16_t)ccb->ataio.cmd.features_exp << 8) |
1478 (uint16_t)ccb->ataio.cmd.features;
1479 request->u.ata.count = ((uint16_t)ccb->ataio.cmd.sector_count_exp << 8) |
1480 (uint16_t)ccb->ataio.cmd.sector_count;
1481 if (ccb->ataio.cmd.flags & CAM_ATAIO_48BIT) {
1482 request->flags |= ATA_R_48BIT;
1483 request->u.ata.lba =
1484 ((uint64_t)ccb->ataio.cmd.lba_high_exp << 40) |
1485 ((uint64_t)ccb->ataio.cmd.lba_mid_exp << 32) |
1486 ((uint64_t)ccb->ataio.cmd.lba_low_exp << 24);
1487 } else {
1488 request->u.ata.lba =
1489 ((uint64_t)(ccb->ataio.cmd.device & 0x0f) << 24);
1490 }
1491 request->u.ata.lba |= ((uint64_t)ccb->ataio.cmd.lba_high << 16) |
1492 ((uint64_t)ccb->ataio.cmd.lba_mid << 8) |
1493 (uint64_t)ccb->ataio.cmd.lba_low;
1494 if (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)
1495 request->flags |= ATA_R_NEEDRESULT;
1496 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1497 ccb->ataio.cmd.flags & CAM_ATAIO_DMA)
1498 request->flags |= ATA_R_DMA;
1499 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1500 request->flags |= ATA_R_READ;
1501 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1502 request->flags |= ATA_R_WRITE;
1503 if (ccb->ataio.cmd.command == ATA_READ_MUL ||
1504 ccb->ataio.cmd.command == ATA_READ_MUL48 ||
1505 ccb->ataio.cmd.command == ATA_WRITE_MUL ||
1506 ccb->ataio.cmd.command == ATA_WRITE_MUL48) {
1507 request->transfersize = min(request->bytecount,
1508 ch->curr[ccb->ccb_h.target_id].bytecount);
1509 } else
1510 request->transfersize = min(request->bytecount, 512);
1511 } else {
1512 request->data = ccb->csio.data_ptr;
1513 request->bytecount = ccb->csio.dxfer_len;
1514 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1515 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1516 request->u.atapi.ccb, ccb->csio.cdb_len);
1517 request->flags |= ATA_R_ATAPI;
1518 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1519 request->flags |= ATA_R_ATAPI16;
1520 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1521 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1522 request->flags |= ATA_R_DMA;
1523 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1524 request->flags |= ATA_R_READ;
1525 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
1526 request->flags |= ATA_R_WRITE;
1527 request->transfersize = min(request->bytecount,
1528 ch->curr[ccb->ccb_h.target_id].bytecount);
1529 }
1530 request->retries = 0;
1531 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1532 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1533 request->ccb = ccb;
1534
1535 ch->running = request;
1536 ch->state = ATA_ACTIVE;
1537 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1538 ch->running = NULL;
1539 ch->state = ATA_IDLE;
1540 ata_cam_end_transaction(dev, request);
1541 return;
1542 }
1543 }
1544
1545 static void
1546 ata_cam_request_sense(device_t dev, struct ata_request *request)
1547 {
1548 struct ata_channel *ch = device_get_softc(dev);
1549 union ccb *ccb = request->ccb;
1550
1551 ch->requestsense = 1;
1552
1553 bzero(request, sizeof(*request));
1554 request->dev = NULL;
1555 request->parent = dev;
1556 request->unit = ccb->ccb_h.target_id;
1557 request->data = (void *)&ccb->csio.sense_data;
1558 request->bytecount = ccb->csio.sense_len;
1559 request->u.atapi.ccb[0] = ATAPI_REQUEST_SENSE;
1560 request->u.atapi.ccb[4] = ccb->csio.sense_len;
1561 request->flags |= ATA_R_ATAPI;
1562 if (ch->curr[ccb->ccb_h.target_id].atapi == 16)
1563 request->flags |= ATA_R_ATAPI16;
1564 if (ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1565 request->flags |= ATA_R_DMA;
1566 request->flags |= ATA_R_READ;
1567 request->transfersize = min(request->bytecount,
1568 ch->curr[ccb->ccb_h.target_id].bytecount);
1569 request->retries = 0;
1570 request->timeout = (ccb->ccb_h.timeout + 999) / 1000;
1571 callout_init_mtx(&request->callout, &ch->state_mtx, CALLOUT_RETURNUNLOCKED);
1572 request->ccb = ccb;
1573
1574 ch->running = request;
1575 ch->state = ATA_ACTIVE;
1576 if (ch->hw.begin_transaction(request) == ATA_OP_FINISHED) {
1577 ch->running = NULL;
1578 ch->state = ATA_IDLE;
1579 ata_cam_end_transaction(dev, request);
1580 return;
1581 }
1582 }
1583
1584 static void
1585 ata_cam_process_sense(device_t dev, struct ata_request *request)
1586 {
1587 struct ata_channel *ch = device_get_softc(dev);
1588 union ccb *ccb = request->ccb;
1589 int fatalerr = 0;
1590
1591 ch->requestsense = 0;
1592
1593 if (request->flags & ATA_R_TIMEOUT)
1594 fatalerr = 1;
1595 if ((request->flags & ATA_R_TIMEOUT) == 0 &&
1596 (request->status & ATA_S_ERROR) == 0 &&
1597 request->result == 0) {
1598 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1599 } else {
1600 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1601 ccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1602 }
1603
1604 ata_free_request(request);
1605 xpt_done(ccb);
1606 /* Do error recovery if needed. */
1607 if (fatalerr)
1608 ata_reinit(dev);
1609 }
1610
1611 void
1612 ata_cam_end_transaction(device_t dev, struct ata_request *request)
1613 {
1614 struct ata_channel *ch = device_get_softc(dev);
1615 union ccb *ccb = request->ccb;
1616 int fatalerr = 0;
1617
1618 if (ch->requestsense) {
1619 ata_cam_process_sense(dev, request);
1620 return;
1621 }
1622
1623 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1624 if (request->flags & ATA_R_TIMEOUT) {
1625 xpt_freeze_simq(ch->sim, 1);
1626 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1627 ccb->ccb_h.status |= CAM_CMD_TIMEOUT | CAM_RELEASE_SIMQ;
1628 fatalerr = 1;
1629 } else if (request->status & ATA_S_ERROR) {
1630 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1631 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1632 } else {
1633 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1634 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1635 }
1636 } else if (request->result == ERESTART)
1637 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1638 else if (request->result != 0)
1639 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1640 else
1641 ccb->ccb_h.status |= CAM_REQ_CMP;
1642 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
1643 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1644 xpt_freeze_devq(ccb->ccb_h.path, 1);
1645 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1646 }
1647 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1648 ((request->status & ATA_S_ERROR) ||
1649 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT))) {
1650 struct ata_res *res = &ccb->ataio.res;
1651 res->status = request->status;
1652 res->error = request->error;
1653 res->lba_low = request->u.ata.lba;
1654 res->lba_mid = request->u.ata.lba >> 8;
1655 res->lba_high = request->u.ata.lba >> 16;
1656 res->device = request->u.ata.lba >> 24;
1657 res->lba_low_exp = request->u.ata.lba >> 24;
1658 res->lba_mid_exp = request->u.ata.lba >> 32;
1659 res->lba_high_exp = request->u.ata.lba >> 40;
1660 res->sector_count = request->u.ata.count;
1661 res->sector_count_exp = request->u.ata.count >> 8;
1662 }
1663 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1664 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1665 ccb->ataio.resid =
1666 ccb->ataio.dxfer_len - request->donecount;
1667 } else {
1668 ccb->csio.resid =
1669 ccb->csio.dxfer_len - request->donecount;
1670 }
1671 }
1672 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1673 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)
1674 ata_cam_request_sense(dev, request);
1675 else {
1676 ata_free_request(request);
1677 xpt_done(ccb);
1678 }
1679 /* Do error recovery if needed. */
1680 if (fatalerr)
1681 ata_reinit(dev);
1682 }
1683
1684 static int
1685 ata_check_ids(device_t dev, union ccb *ccb)
1686 {
1687 struct ata_channel *ch = device_get_softc(dev);
1688
1689 if (ccb->ccb_h.target_id > ((ch->flags & ATA_NO_SLAVE) ? 0 : 1)) {
1690 ccb->ccb_h.status = CAM_TID_INVALID;
1691 xpt_done(ccb);
1692 return (-1);
1693 }
1694 if (ccb->ccb_h.target_lun != 0) {
1695 ccb->ccb_h.status = CAM_LUN_INVALID;
1696 xpt_done(ccb);
1697 return (-1);
1698 }
1699 return (0);
1700 }
1701
1702 static void
1703 ataaction(struct cam_sim *sim, union ccb *ccb)
1704 {
1705 device_t dev;
1706 struct ata_channel *ch;
1707
1708 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ataaction func_code=%x\n",
1709 ccb->ccb_h.func_code));
1710
1711 ch = (struct ata_channel *)cam_sim_softc(sim);
1712 dev = ch->dev;
1713 switch (ccb->ccb_h.func_code) {
1714 /* Common cases first */
1715 case XPT_ATA_IO: /* Execute the requested I/O operation */
1716 case XPT_SCSI_IO:
1717 if (ata_check_ids(dev, ccb))
1718 return;
1719 if ((ch->devices & ((ATA_ATA_MASTER | ATA_ATAPI_MASTER)
1720 << ccb->ccb_h.target_id)) == 0) {
1721 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1722 break;
1723 }
1724 if (ch->running)
1725 device_printf(dev, "already running!\n");
1726 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1727 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1728 (ccb->ataio.cmd.control & ATA_A_RESET)) {
1729 struct ata_res *res = &ccb->ataio.res;
1730
1731 bzero(res, sizeof(*res));
1732 if (ch->devices & (ATA_ATA_MASTER << ccb->ccb_h.target_id)) {
1733 res->lba_high = 0;
1734 res->lba_mid = 0;
1735 } else {
1736 res->lba_high = 0xeb;
1737 res->lba_mid = 0x14;
1738 }
1739 ccb->ccb_h.status = CAM_REQ_CMP;
1740 break;
1741 }
1742 ata_cam_begin_transaction(dev, ccb);
1743 return;
1744 case XPT_EN_LUN: /* Enable LUN as a target */
1745 case XPT_TARGET_IO: /* Execute target I/O request */
1746 case XPT_ACCEPT_TARGET_IO: /* Accept Host Target Mode CDB */
1747 case XPT_CONT_TARGET_IO: /* Continue Host Target I/O Connection*/
1748 case XPT_ABORT: /* Abort the specified CCB */
1749 /* XXX Implement */
1750 ccb->ccb_h.status = CAM_REQ_INVALID;
1751 break;
1752 case XPT_SET_TRAN_SETTINGS:
1753 {
1754 struct ccb_trans_settings *cts = &ccb->cts;
1755 struct ata_cam_device *d;
1756
1757 if (ata_check_ids(dev, ccb))
1758 return;
1759 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1760 d = &ch->curr[ccb->ccb_h.target_id];
1761 else
1762 d = &ch->user[ccb->ccb_h.target_id];
1763 if (ch->flags & ATA_SATA) {
1764 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1765 d->revision = cts->xport_specific.sata.revision;
1766 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE) {
1767 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1768 d->mode = ATA_SETMODE(ch->dev,
1769 ccb->ccb_h.target_id,
1770 cts->xport_specific.sata.mode);
1771 } else
1772 d->mode = cts->xport_specific.sata.mode;
1773 }
1774 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1775 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1776 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1777 d->atapi = cts->xport_specific.sata.atapi;
1778 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_CAPS)
1779 d->caps = cts->xport_specific.sata.caps;
1780 } else {
1781 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_MODE) {
1782 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1783 d->mode = ATA_SETMODE(ch->dev,
1784 ccb->ccb_h.target_id,
1785 cts->xport_specific.ata.mode);
1786 } else
1787 d->mode = cts->xport_specific.ata.mode;
1788 }
1789 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_BYTECOUNT)
1790 d->bytecount = cts->xport_specific.ata.bytecount;
1791 if (cts->xport_specific.ata.valid & CTS_ATA_VALID_ATAPI)
1792 d->atapi = cts->xport_specific.ata.atapi;
1793 }
1794 ccb->ccb_h.status = CAM_REQ_CMP;
1795 break;
1796 }
1797 case XPT_GET_TRAN_SETTINGS:
1798 {
1799 struct ccb_trans_settings *cts = &ccb->cts;
1800 struct ata_cam_device *d;
1801
1802 if (ata_check_ids(dev, ccb))
1803 return;
1804 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1805 d = &ch->curr[ccb->ccb_h.target_id];
1806 else
1807 d = &ch->user[ccb->ccb_h.target_id];
1808 cts->protocol = PROTO_UNSPECIFIED;
1809 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1810 if (ch->flags & ATA_SATA) {
1811 cts->transport = XPORT_SATA;
1812 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1813 cts->xport_specific.sata.valid = 0;
1814 cts->xport_specific.sata.mode = d->mode;
1815 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1816 cts->xport_specific.sata.bytecount = d->bytecount;
1817 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1818 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1819 cts->xport_specific.sata.revision =
1820 ATA_GETREV(dev, ccb->ccb_h.target_id);
1821 if (cts->xport_specific.sata.revision != 0xff) {
1822 cts->xport_specific.sata.valid |=
1823 CTS_SATA_VALID_REVISION;
1824 }
1825 cts->xport_specific.sata.caps =
1826 d->caps & CTS_SATA_CAPS_D;
1827 if (ch->pm_level) {
1828 cts->xport_specific.sata.caps |=
1829 CTS_SATA_CAPS_H_PMREQ;
1830 }
1831 cts->xport_specific.sata.caps &=
1832 ch->user[ccb->ccb_h.target_id].caps;
1833 cts->xport_specific.sata.valid |=
1834 CTS_SATA_VALID_CAPS;
1835 } else {
1836 cts->xport_specific.sata.revision = d->revision;
1837 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1838 cts->xport_specific.sata.caps = d->caps;
1839 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1840 }
1841 cts->xport_specific.sata.atapi = d->atapi;
1842 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1843 } else {
1844 cts->transport = XPORT_ATA;
1845 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1846 cts->xport_specific.ata.valid = 0;
1847 cts->xport_specific.ata.mode = d->mode;
1848 cts->xport_specific.ata.valid |= CTS_ATA_VALID_MODE;
1849 cts->xport_specific.ata.bytecount = d->bytecount;
1850 cts->xport_specific.ata.valid |= CTS_ATA_VALID_BYTECOUNT;
1851 cts->xport_specific.ata.atapi = d->atapi;
1852 cts->xport_specific.ata.valid |= CTS_ATA_VALID_ATAPI;
1853 }
1854 ccb->ccb_h.status = CAM_REQ_CMP;
1855 break;
1856 }
1857 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1858 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1859 ata_reinit(dev);
1860 ccb->ccb_h.status = CAM_REQ_CMP;
1861 break;
1862 case XPT_TERM_IO: /* Terminate the I/O process */
1863 /* XXX Implement */
1864 ccb->ccb_h.status = CAM_REQ_INVALID;
1865 break;
1866 case XPT_PATH_INQ: /* Path routing inquiry */
1867 {
1868 struct ccb_pathinq *cpi = &ccb->cpi;
1869
1870 cpi->version_num = 1; /* XXX??? */
1871 cpi->hba_inquiry = PI_SDTR_ABLE;
1872 cpi->target_sprt = 0;
1873 cpi->hba_misc = PIM_SEQSCAN;
1874 cpi->hba_eng_cnt = 0;
1875 if (ch->flags & ATA_NO_SLAVE)
1876 cpi->max_target = 0;
1877 else
1878 cpi->max_target = 1;
1879 cpi->max_lun = 0;
1880 cpi->initiator_id = 0;
1881 cpi->bus_id = cam_sim_bus(sim);
1882 if (ch->flags & ATA_SATA)
1883 cpi->base_transfer_speed = 150000;
1884 else
1885 cpi->base_transfer_speed = 3300;
1886 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1887 strncpy(cpi->hba_vid, "ATA", HBA_IDLEN);
1888 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1889 cpi->unit_number = cam_sim_unit(sim);
1890 if (ch->flags & ATA_SATA)
1891 cpi->transport = XPORT_SATA;
1892 else
1893 cpi->transport = XPORT_ATA;
1894 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1895 cpi->protocol = PROTO_ATA;
1896 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1897 cpi->maxio = ch->dma.max_iosize ? ch->dma.max_iosize : DFLTPHYS;
1898 cpi->ccb_h.status = CAM_REQ_CMP;
1899 break;
1900 }
1901 default:
1902 ccb->ccb_h.status = CAM_REQ_INVALID;
1903 break;
1904 }
1905 xpt_done(ccb);
1906 }
1907
1908 static void
1909 atapoll(struct cam_sim *sim)
1910 {
1911 struct ata_channel *ch = (struct ata_channel *)cam_sim_softc(sim);
1912
1913 ata_interrupt_locked(ch);
1914 }
1915 #endif
1916
1917 /*
1918 * module handeling
1919 */
1920 static int
1921 ata_module_event_handler(module_t mod, int what, void *arg)
1922 {
1923 #ifndef ATA_CAM
1924 static struct cdev *atacdev;
1925 #endif
1926
1927 switch (what) {
1928 case MOD_LOAD:
1929 #ifndef ATA_CAM
1930 /* register controlling device */
1931 atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1932
1933 if (cold) {
1934 /* register boot attach to be run when interrupts are enabled */
1935 if (!(ata_delayed_attach = (struct intr_config_hook *)
1936 malloc(sizeof(struct intr_config_hook),
1937 M_TEMP, M_NOWAIT | M_ZERO))) {
1938 printf("ata: malloc of delayed attach hook failed\n");
1939 return EIO;
1940 }
1941 ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1942 if (config_intrhook_establish(ata_delayed_attach) != 0) {
1943 printf("ata: config_intrhook_establish failed\n");
1944 free(ata_delayed_attach, M_TEMP);
1945 }
1946 }
1947 #endif
1948 return 0;
1949
1950 case MOD_UNLOAD:
1951 #ifndef ATA_CAM
1952 /* deregister controlling device */
1953 destroy_dev(atacdev);
1954 #endif
1955 return 0;
1956
1957 default:
1958 return EOPNOTSUPP;
1959 }
1960 }
1961
1962 static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1963 DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1964 MODULE_VERSION(ata, 1);
1965 #ifdef ATA_CAM
1966 MODULE_DEPEND(ata, cam, 1, 1, 1);
1967 #endif
1968
1969 static void
1970 ata_init(void)
1971 {
1972 ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1973 NULL, NULL, NULL, NULL, 0, 0);
1974 ata_composite_zone = uma_zcreate("ata_composite",
1975 sizeof(struct ata_composite),
1976 NULL, NULL, NULL, NULL, 0, 0);
1977 }
1978 SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1979
1980 static void
1981 ata_uninit(void)
1982 {
1983 uma_zdestroy(ata_composite_zone);
1984 uma_zdestroy(ata_request_zone);
1985 }
1986 SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
Cache object: 5a456a2773286a34dbf7db6f5cc07a0e
|