1/*
2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/twa/tw_osl_cam.c 208969 2010-06-09 21:40:38Z delphij $
28 */
29
30/*
31 * AMCC'S 3ware driver for 9000 series storage controllers.
32 *
33 * Author: Vinod Kashyap
34 * Modifications by: Adam Radford
35 * Modifications by: Manjunath Ranganathaiah
36 */
37
38
39/*
40 * FreeBSD CAM related functions.
41 */
42
43
44#include <dev/twa/tw_osl_includes.h>
45
46#include <cam/cam.h>
47#include <cam/cam_ccb.h>
48#include <cam/cam_sim.h>
49#include <cam/cam_xpt_sim.h>
50#include <cam/cam_debug.h>
51#include <cam/cam_periph.h>
52
53#include <cam/scsi/scsi_all.h>
54#include <cam/scsi/scsi_message.h>
55
56static TW_VOID twa_action(struct cam_sim *sim, union ccb *ccb);
57static TW_VOID twa_poll(struct cam_sim *sim);
58static TW_VOID twa_timeout(TW_VOID *arg);
59
60static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
61 union ccb *ccb);
62
63
64
65/*
66 * Function name: tw_osli_cam_attach
67 * Description: Attaches the driver to CAM.
68 *
69 * Input: sc -- ptr to OSL internal ctlr context
70 * Output: None
71 * Return value: 0 -- success
72 * non-zero-- failure
73 */
74TW_INT32
75tw_osli_cam_attach(struct twa_softc *sc)
76{
77 struct cam_devq *devq;
78
79 tw_osli_dbg_dprintf(3, sc, "entered");
80
81 /*
82 * Create the device queue for our SIM.
83 */
84 if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_REQUESTS)) == NULL) {
85 tw_osli_printf(sc, "error = %d",
86 TW_CL_SEVERITY_ERROR_STRING,
87 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
88 0x2100,
89 "Failed to create SIM device queue",
90 ENOMEM);
91 return(ENOMEM);
92 }
93
94 /*
95 * Create a SIM entry. Though we can support TW_OSLI_MAX_NUM_REQUESTS
96 * simultaneous requests, we claim to be able to handle only
97 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
98 * packet available to service ioctls and AENs.
99 */
100 tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
101 sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
102 device_get_unit(sc->bus_dev), sc->sim_lock,
103 TW_OSLI_MAX_NUM_IOS, 1, devq);
104 if (sc->sim == NULL) {
105 cam_simq_free(devq);
106 tw_osli_printf(sc, "error = %d",
107 TW_CL_SEVERITY_ERROR_STRING,
108 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
109 0x2101,
110 "Failed to create a SIM entry",
111 ENOMEM);
112 return(ENOMEM);
113 }
114
115 /*
116 * Register the bus.
117 */
118 tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
119 mtx_lock(sc->sim_lock);
120 if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
121 cam_sim_free(sc->sim, TRUE);
122 sc->sim = NULL; /* so cam_detach will not try to free it */
123 tw_osli_printf(sc, "error = %d",
124 TW_CL_SEVERITY_ERROR_STRING,
125 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
126 0x2102,
127 "Failed to register the bus",
128 ENXIO);
129 mtx_unlock(sc->sim_lock);
130 return(ENXIO);
131 }
132
133 tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
134 if (xpt_create_path(&sc->path, NULL,
135 cam_sim_path(sc->sim),
136 CAM_TARGET_WILDCARD,
137 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
138 xpt_bus_deregister(cam_sim_path (sc->sim));
139 /* Passing TRUE to cam_sim_free will free the devq as well. */
140 cam_sim_free(sc->sim, TRUE);
141 tw_osli_printf(sc, "error = %d",
142 TW_CL_SEVERITY_ERROR_STRING,
143 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
144 0x2103,
145 "Failed to create path",
146 ENXIO);
147 mtx_unlock(sc->sim_lock);
148 return(ENXIO);
149 }
150 mtx_unlock(sc->sim_lock);
151
152 tw_osli_dbg_dprintf(3, sc, "exiting");
153 return(0);
154}
155
156
157
158/*
159 * Function name: tw_osli_cam_detach
160 * Description: Detaches the driver from CAM.
161 *
162 * Input: sc -- ptr to OSL internal ctlr context
163 * Output: None
164 * Return value: None
165 */
166TW_VOID
167tw_osli_cam_detach(struct twa_softc *sc)
168{
169 tw_osli_dbg_dprintf(3, sc, "entered");
170
171 mtx_lock(sc->sim_lock);
172
173 if (sc->path)
174 xpt_free_path(sc->path);
175 if (sc->sim) {
176 xpt_bus_deregister(cam_sim_path(sc->sim));
177 /* Passing TRUE to cam_sim_free will free the devq as well. */
178 cam_sim_free(sc->sim, TRUE);
179 }
180 /* It's ok have 1 hold count while destroying the mutex */
181 mtx_destroy(sc->sim_lock);
182}
183
184
185
186/*
187 * Function name: tw_osli_execute_scsi
188 * Description: Build a fw cmd, based on a CAM style ccb, and
189 * send it down.
190 *
191 * Input: req -- ptr to OSL internal request context
192 * ccb -- ptr to CAM style ccb
193 * Output: None
194 * Return value: 0 -- success
195 * non-zero-- failure
196 */
197TW_INT32
198tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
199{
200 struct twa_softc *sc = req->ctlr;
201 struct tw_cl_req_packet *req_pkt;
202 struct tw_cl_scsi_req_packet *scsi_req;
203 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
204 struct ccb_scsiio *csio = &(ccb->csio);
205 TW_INT32 error;
206
207 tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
208 csio->cdb_io.cdb_bytes[0]);
209
210 if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
211 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %x",
212 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
213 ccb_h->status |= CAM_TID_INVALID;
214 xpt_done(ccb);
215 return(1);
216 }
217 if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
218 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
219 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
220 ccb_h->status |= CAM_LUN_INVALID;
221 xpt_done(ccb);
222 return(1);
223 }
224
225 if(ccb_h->flags & CAM_CDB_PHYS) {
226 tw_osli_printf(sc, "",
227 TW_CL_SEVERITY_ERROR_STRING,
228 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
229 0x2105,
230 "Physical CDB address!");
231 ccb_h->status = CAM_REQ_INVALID;
232 xpt_done(ccb);
233 return(1);
234 }
235
236 /*
237 * We are going to work on this request. Mark it as enqueued (though
238 * we don't actually queue it...)
239 */
240 ccb_h->status |= CAM_SIM_QUEUED;
241
242 if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
243 if(ccb_h->flags & CAM_DIR_IN)
244 req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
245 else
246 req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
247 }
248
249 /* Build the CL understood request packet for SCSI cmds. */
250 req_pkt = &req->req_pkt;
251 req_pkt->status = 0;
252 req_pkt->tw_osl_callback = tw_osl_complete_io;
253 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
254 scsi_req->unit = ccb_h->target_id;
255 scsi_req->lun = ccb_h->target_lun;
256 scsi_req->sense_len = 0;
257 scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
258 scsi_req->scsi_status = 0;
259 if(ccb_h->flags & CAM_CDB_POINTER)
260 scsi_req->cdb = csio->cdb_io.cdb_ptr;
261 else
262 scsi_req->cdb = csio->cdb_io.cdb_bytes;
263 scsi_req->cdb_len = csio->cdb_len;
264
265 if (!(ccb_h->flags & CAM_DATA_PHYS)) {
266 /* Virtual data addresses. Need to convert them... */
267 tw_osli_dbg_dprintf(3, sc,
268 "XPT_SCSI_IO: Single virtual address!");
269 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
270 if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
271 tw_osli_printf(sc, "size = %d",
272 TW_CL_SEVERITY_ERROR_STRING,
273 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
274 0x2106,
275 "I/O size too big",
276 csio->dxfer_len);
277 ccb_h->status = CAM_REQ_TOO_BIG;
278 xpt_done(ccb);
279 return(1);
280 }
281
282 if ((req->length = csio->dxfer_len)) {
283 req->data = csio->data_ptr;
284 scsi_req->sgl_entries = 1;
285 }
286 } else {
287 tw_osli_printf(sc, "",
288 TW_CL_SEVERITY_ERROR_STRING,
289 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
290 0x2107,
291 "XPT_SCSI_IO: Got SGList");
292 ccb_h->status = CAM_REQ_INVALID;
293 xpt_done(ccb);
294 return(1);
295 }
296 } else {
297 /* Data addresses are physical. */
298 tw_osli_printf(sc, "",
299 TW_CL_SEVERITY_ERROR_STRING,
300 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
301 0x2108,
302 "XPT_SCSI_IO: Physical data addresses");
303 ccb_h->status = CAM_REQ_INVALID;
304 ccb_h->status &= ~CAM_SIM_QUEUED;
305 xpt_done(ccb);
306 return(1);
307 }
308
309 ccb_h->timeout_ch = timeout(twa_timeout, req,
310 (ccb_h->timeout * hz) / 1000);
311 /*
312 * twa_map_load_data_callback will fill in the SGL,
313 * and submit the I/O.
314 */
315 error = tw_osli_map_request(req);
316 return(error);
317}
318
319
320
321/*
322 * Function name: twa_action
323 * Description: Driver entry point for CAM's use.
324 *
325 * Input: sim -- sim corresponding to the ctlr
326 * ccb -- ptr to CAM request
327 * Output: None
328 * Return value: None
329 */
330TW_VOID
331twa_action(struct cam_sim *sim, union ccb *ccb)
332{
333 struct twa_softc *sc = (struct twa_softc *)cam_sim_softc(sim);
334 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
335
336 switch (ccb_h->func_code) {
337 case XPT_SCSI_IO: /* SCSI I/O */
338 {
339 struct tw_osli_req_context *req;
340
341 req = tw_osli_get_request(sc);
342 if (req == NULL) {
343 tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
344 /*
345 * Freeze the simq to maintain ccb ordering. The next
346 * ccb that gets completed will unfreeze the simq.
347 */
348 ccb_h->status |= CAM_REQUEUE_REQ;
349 xpt_done(ccb);
350 break;
351 }
352 req->req_handle.osl_req_ctxt = req;
353 req->req_handle.is_io = TW_CL_TRUE;
354 req->orig_req = ccb;
355 if (tw_osli_execute_scsi(req, ccb))
356 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
357 break;
358 }
359
360 case XPT_ABORT:
361 tw_osli_dbg_dprintf(2, sc, "Abort request.");
362 ccb_h->status = CAM_UA_ABORT;
363 xpt_done(ccb);
364 break;
365
366 case XPT_RESET_BUS:
367 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
368 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
369 0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
370 "Received Reset Bus request from CAM",
371 " ");
372
373 mtx_unlock(sc->sim_lock);
374 if (tw_cl_reset_ctlr(&sc->ctlr_handle)) {
375 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
376 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
377 0x2109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
378 "Failed to reset bus",
379 " ");
380 ccb_h->status = CAM_REQ_CMP_ERR;
381 }
382 else
383 ccb_h->status = CAM_REQ_CMP;
384
385 mtx_lock(sc->sim_lock);
386 xpt_done(ccb);
387 break;
388
389 case XPT_SET_TRAN_SETTINGS:
390 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
391
392 /*
393 * This command is not supported, since it's very specific
394 * to SCSI, and we are doing ATA.
395 */
396 ccb_h->status = CAM_FUNC_NOTAVAIL;
397 xpt_done(ccb);
398 break;
399
400 case XPT_GET_TRAN_SETTINGS:
401 {
402 struct ccb_trans_settings *cts = &ccb->cts;
403 struct ccb_trans_settings_scsi *scsi =
404 &cts->proto_specific.scsi;
405 struct ccb_trans_settings_spi *spi =
406 &cts->xport_specific.spi;
407
408 cts->protocol = PROTO_SCSI;
409 cts->protocol_version = SCSI_REV_2;
410 cts->transport = XPORT_SPI;
411 cts->transport_version = 2;
412
413 spi->valid = CTS_SPI_VALID_DISC;
414 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
415 scsi->valid = CTS_SCSI_VALID_TQ;
416 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
417 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
418 ccb_h->status = CAM_REQ_CMP;
419 xpt_done(ccb);
420 break;
421 }
422
423 case XPT_CALC_GEOMETRY:
424 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
425 cam_calc_geometry(&ccb->ccg, 1/* extended */);
426 xpt_done(ccb);
427 break;
428
429 case XPT_PATH_INQ: /* Path inquiry -- get twa properties */
430 {
431 struct ccb_pathinq *path_inq = &ccb->cpi;
432
433 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
434
435 path_inq->version_num = 1;
436 path_inq->hba_inquiry = 0;
437 path_inq->target_sprt = 0;
438 path_inq->hba_misc = 0;
439 path_inq->hba_eng_cnt = 0;
440 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
441 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
442 path_inq->unit_number = cam_sim_unit(sim);
443 path_inq->bus_id = cam_sim_bus(sim);
444 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
445 path_inq->base_transfer_speed = 100000;
446 strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
447 strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
448 strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
449 path_inq->transport = XPORT_SPI;
450 path_inq->transport_version = 2;
451 path_inq->protocol = PROTO_SCSI;
452 path_inq->protocol_version = SCSI_REV_2;
453 ccb_h->status = CAM_REQ_CMP;
454 xpt_done(ccb);
455 break;
456 }
457
458 default:
459 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
460 ccb_h->status = CAM_REQ_INVALID;
461 xpt_done(ccb);
462 break;
463 }
464}
465
466
467
468/*
469 * Function name: twa_poll
470 * Description: Driver entry point called when interrupts are not
471 * available.
472 *
473 * Input: sim -- sim corresponding to the controller
474 * Output: None
475 * Return value: None
476 */
477TW_VOID
478twa_poll(struct cam_sim *sim)
479{
480 struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
481
482 tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
483 tw_cl_interrupt(&(sc->ctlr_handle));
484 tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
485}
486
487
488
489/*
490 * Function name: twa_timeout
491 * Description: Driver entry point for being alerted on a request
492 * timing out.
493 *
494 * Input: arg -- ptr to timed out request
495 * Output: None
496 * Return value: None
497 */
498static TW_VOID
499twa_timeout(TW_VOID *arg)
500{
501 struct tw_osli_req_context *req =
502 (struct tw_osli_req_context *)arg;
503
504 tw_cl_create_event(&(req->ctlr->ctlr_handle), TW_CL_TRUE,
505 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
506 0x210B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
507 "Request timed out!",
508 "request = %p", req);
509 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
510}
511
512
513
514/*
515 * Function name: tw_osli_request_bus_scan
516 * Description: Requests CAM for a scan of the bus.
517 *
518 * Input: sc -- ptr to per ctlr structure
519 * Output: None
520 * Return value: 0 -- success
521 * non-zero-- failure
522 */
523TW_INT32
524tw_osli_request_bus_scan(struct twa_softc *sc)
525{
526 union ccb *ccb;
527
528 tw_osli_dbg_dprintf(3, sc, "entering");
529
530 /* If we get here before sc->sim is initialized, return an error. */
531 if (!(sc->sim))
532 return(ENXIO);
533 if ((ccb = xpt_alloc_ccb()) == NULL)
534 return(ENOMEM);
535 mtx_lock(sc->sim_lock);
536 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sc->sim),
537 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
538 xpt_free_ccb(ccb);
539 mtx_unlock(sc->sim_lock);
540 return(EIO);
541 }
542
543 xpt_rescan(ccb);
544 mtx_unlock(sc->sim_lock);
545 return(0);
546}
547
548
549
550/*
551 * Function name: tw_osli_disallow_new_requests
552 * Description: Calls the appropriate CAM function, so as to freeze
553 * the flow of new requests from CAM to this controller.
554 *
555 * Input: sc -- ptr to OSL internal ctlr context
556 * req_handle -- ptr to request handle sent by OSL.
557 * Output: None
558 * Return value: None
559 */
560TW_VOID
561tw_osli_disallow_new_requests(struct twa_softc *sc,
562 struct tw_cl_req_handle *req_handle)
563{
564 /* Only freeze/release the simq for IOs */
565 if (req_handle->is_io) {
566 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
567 union ccb *ccb = (union ccb *)(req->orig_req);
568
569 xpt_freeze_simq(sc->sim, 1);
570 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
571 }
572}
573
574
575
576/*
577 * Function name: tw_osl_ctlr_busy
578 * Description: CL calls this function on cmd queue full or otherwise,
579 * when it is too busy to accept new requests.
580 *
581 * Input: ctlr_handle -- ptr to controller handle
582 * req_handle -- ptr to request handle sent by OSL.
583 * Output: None
584 * Return value: None
585 */
586TW_VOID
587tw_osl_ctlr_busy(struct tw_cl_ctlr_handle *ctlr_handle,
588 struct tw_cl_req_handle *req_handle)
589{
590 tw_osli_disallow_new_requests(ctlr_handle->osl_ctlr_ctxt, req_handle);
591}
592
593
594
595/*
596 * Function name: tw_osl_scan_bus
597 * Description: CL calls this function to request for a bus scan.
598 *
599 * Input: ctlr_handle -- ptr to controller handle
600 * Output: None
601 * Return value: None
602 */
603TW_VOID
604tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
605{
606 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
607 TW_INT32 error;
608
609 if ((error = tw_osli_request_bus_scan(sc)))
610 tw_osli_printf(sc, "error = %d",
611 TW_CL_SEVERITY_ERROR_STRING,
612 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
613 0x2109,
614 "Bus scan request to CAM failed",
615 error);
616}
617
618
619
620/*
621 * Function name: tw_osl_complete_io
622 * Description: Called to complete CAM scsi requests.
623 *
624 * Input: req_handle -- ptr to request handle
625 * Output: None
626 * Return value: None
627 */
628TW_VOID
629tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
630{
631 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
632 struct tw_cl_req_packet *req_pkt =
633 (struct tw_cl_req_packet *)(&req->req_pkt);
634 struct tw_cl_scsi_req_packet *scsi_req;
635 struct twa_softc *sc = req->ctlr;
636 union ccb *ccb = (union ccb *)(req->orig_req);
637
638 tw_osli_dbg_dprintf(10, sc, "entering");
639
640 if (req->state != TW_OSLI_REQ_STATE_BUSY)
641 tw_osli_printf(sc, "request = %p, status = %d",
642 TW_CL_SEVERITY_ERROR_STRING,
643 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
644 0x210A,
645 "Unposted command completed!!",
646 req, req->state);
647
648 /*
649 * Remove request from the busy queue. Just mark it complete.
650 * There's no need to move it into the complete queue as we are
651 * going to be done with it right now.
652 */
653 req->state = TW_OSLI_REQ_STATE_COMPLETE;
654 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
655
656 tw_osli_unmap_request(req);
657
658 untimeout(twa_timeout, req, ccb->ccb_h.timeout_ch);
659 if (req->error_code) {
660 /* This request never got submitted to the firmware. */
661 if (req->error_code == EBUSY) {
662 /*
663 * Cmd queue is full, or the Common Layer is out of
664 * resources. The simq will already have been frozen.
665 * When this ccb gets completed will unfreeze the simq.
666 */
667 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
668 }
669 else if (req->error_code == EFBIG)
670 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
671 else
672 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
673 } else {
674 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
675 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
676 ccb->ccb_h.status = CAM_REQ_CMP;
677 else {
678 if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
679 ccb->ccb_h.status |= CAM_TID_INVALID;
680 else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
681 ccb->ccb_h.status |= CAM_LUN_INVALID;
682 else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
683 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
684 else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
685 ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
686 /*
687 * If none of the above errors occurred, simply
688 * mark completion error.
689 */
690 if (ccb->ccb_h.status == 0)
691 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
692
693 if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
694 ccb->csio.sense_len = scsi_req->sense_len;
695 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
696 }
697 }
698
699 ccb->csio.scsi_status = scsi_req->scsi_status;
700 }
701
702 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
703 mtx_lock(sc->sim_lock);
704 xpt_done(ccb);
705 mtx_unlock(sc->sim_lock);
706 if (! req->error_code)
707 /* twa_action will free the request otherwise */
708 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
709}
710
2 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
3 * Copyright (c) 2004-05 Vinod Kashyap.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/twa/tw_osl_cam.c 208969 2010-06-09 21:40:38Z delphij $
28 */
29
30/*
31 * AMCC'S 3ware driver for 9000 series storage controllers.
32 *
33 * Author: Vinod Kashyap
34 * Modifications by: Adam Radford
35 * Modifications by: Manjunath Ranganathaiah
36 */
37
38
39/*
40 * FreeBSD CAM related functions.
41 */
42
43
44#include <dev/twa/tw_osl_includes.h>
45
46#include <cam/cam.h>
47#include <cam/cam_ccb.h>
48#include <cam/cam_sim.h>
49#include <cam/cam_xpt_sim.h>
50#include <cam/cam_debug.h>
51#include <cam/cam_periph.h>
52
53#include <cam/scsi/scsi_all.h>
54#include <cam/scsi/scsi_message.h>
55
56static TW_VOID twa_action(struct cam_sim *sim, union ccb *ccb);
57static TW_VOID twa_poll(struct cam_sim *sim);
58static TW_VOID twa_timeout(TW_VOID *arg);
59
60static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
61 union ccb *ccb);
62
63
64
65/*
66 * Function name: tw_osli_cam_attach
67 * Description: Attaches the driver to CAM.
68 *
69 * Input: sc -- ptr to OSL internal ctlr context
70 * Output: None
71 * Return value: 0 -- success
72 * non-zero-- failure
73 */
74TW_INT32
75tw_osli_cam_attach(struct twa_softc *sc)
76{
77 struct cam_devq *devq;
78
79 tw_osli_dbg_dprintf(3, sc, "entered");
80
81 /*
82 * Create the device queue for our SIM.
83 */
84 if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_REQUESTS)) == NULL) {
85 tw_osli_printf(sc, "error = %d",
86 TW_CL_SEVERITY_ERROR_STRING,
87 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
88 0x2100,
89 "Failed to create SIM device queue",
90 ENOMEM);
91 return(ENOMEM);
92 }
93
94 /*
95 * Create a SIM entry. Though we can support TW_OSLI_MAX_NUM_REQUESTS
96 * simultaneous requests, we claim to be able to handle only
97 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
98 * packet available to service ioctls and AENs.
99 */
100 tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
101 sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
102 device_get_unit(sc->bus_dev), sc->sim_lock,
103 TW_OSLI_MAX_NUM_IOS, 1, devq);
104 if (sc->sim == NULL) {
105 cam_simq_free(devq);
106 tw_osli_printf(sc, "error = %d",
107 TW_CL_SEVERITY_ERROR_STRING,
108 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
109 0x2101,
110 "Failed to create a SIM entry",
111 ENOMEM);
112 return(ENOMEM);
113 }
114
115 /*
116 * Register the bus.
117 */
118 tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
119 mtx_lock(sc->sim_lock);
120 if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
121 cam_sim_free(sc->sim, TRUE);
122 sc->sim = NULL; /* so cam_detach will not try to free it */
123 tw_osli_printf(sc, "error = %d",
124 TW_CL_SEVERITY_ERROR_STRING,
125 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
126 0x2102,
127 "Failed to register the bus",
128 ENXIO);
129 mtx_unlock(sc->sim_lock);
130 return(ENXIO);
131 }
132
133 tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
134 if (xpt_create_path(&sc->path, NULL,
135 cam_sim_path(sc->sim),
136 CAM_TARGET_WILDCARD,
137 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
138 xpt_bus_deregister(cam_sim_path (sc->sim));
139 /* Passing TRUE to cam_sim_free will free the devq as well. */
140 cam_sim_free(sc->sim, TRUE);
141 tw_osli_printf(sc, "error = %d",
142 TW_CL_SEVERITY_ERROR_STRING,
143 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
144 0x2103,
145 "Failed to create path",
146 ENXIO);
147 mtx_unlock(sc->sim_lock);
148 return(ENXIO);
149 }
150 mtx_unlock(sc->sim_lock);
151
152 tw_osli_dbg_dprintf(3, sc, "exiting");
153 return(0);
154}
155
156
157
158/*
159 * Function name: tw_osli_cam_detach
160 * Description: Detaches the driver from CAM.
161 *
162 * Input: sc -- ptr to OSL internal ctlr context
163 * Output: None
164 * Return value: None
165 */
166TW_VOID
167tw_osli_cam_detach(struct twa_softc *sc)
168{
169 tw_osli_dbg_dprintf(3, sc, "entered");
170
171 mtx_lock(sc->sim_lock);
172
173 if (sc->path)
174 xpt_free_path(sc->path);
175 if (sc->sim) {
176 xpt_bus_deregister(cam_sim_path(sc->sim));
177 /* Passing TRUE to cam_sim_free will free the devq as well. */
178 cam_sim_free(sc->sim, TRUE);
179 }
180 /* It's ok have 1 hold count while destroying the mutex */
181 mtx_destroy(sc->sim_lock);
182}
183
184
185
186/*
187 * Function name: tw_osli_execute_scsi
188 * Description: Build a fw cmd, based on a CAM style ccb, and
189 * send it down.
190 *
191 * Input: req -- ptr to OSL internal request context
192 * ccb -- ptr to CAM style ccb
193 * Output: None
194 * Return value: 0 -- success
195 * non-zero-- failure
196 */
197TW_INT32
198tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
199{
200 struct twa_softc *sc = req->ctlr;
201 struct tw_cl_req_packet *req_pkt;
202 struct tw_cl_scsi_req_packet *scsi_req;
203 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
204 struct ccb_scsiio *csio = &(ccb->csio);
205 TW_INT32 error;
206
207 tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
208 csio->cdb_io.cdb_bytes[0]);
209
210 if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
211 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %x",
212 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
213 ccb_h->status |= CAM_TID_INVALID;
214 xpt_done(ccb);
215 return(1);
216 }
217 if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
218 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
219 ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
220 ccb_h->status |= CAM_LUN_INVALID;
221 xpt_done(ccb);
222 return(1);
223 }
224
225 if(ccb_h->flags & CAM_CDB_PHYS) {
226 tw_osli_printf(sc, "",
227 TW_CL_SEVERITY_ERROR_STRING,
228 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
229 0x2105,
230 "Physical CDB address!");
231 ccb_h->status = CAM_REQ_INVALID;
232 xpt_done(ccb);
233 return(1);
234 }
235
236 /*
237 * We are going to work on this request. Mark it as enqueued (though
238 * we don't actually queue it...)
239 */
240 ccb_h->status |= CAM_SIM_QUEUED;
241
242 if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
243 if(ccb_h->flags & CAM_DIR_IN)
244 req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
245 else
246 req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
247 }
248
249 /* Build the CL understood request packet for SCSI cmds. */
250 req_pkt = &req->req_pkt;
251 req_pkt->status = 0;
252 req_pkt->tw_osl_callback = tw_osl_complete_io;
253 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
254 scsi_req->unit = ccb_h->target_id;
255 scsi_req->lun = ccb_h->target_lun;
256 scsi_req->sense_len = 0;
257 scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
258 scsi_req->scsi_status = 0;
259 if(ccb_h->flags & CAM_CDB_POINTER)
260 scsi_req->cdb = csio->cdb_io.cdb_ptr;
261 else
262 scsi_req->cdb = csio->cdb_io.cdb_bytes;
263 scsi_req->cdb_len = csio->cdb_len;
264
265 if (!(ccb_h->flags & CAM_DATA_PHYS)) {
266 /* Virtual data addresses. Need to convert them... */
267 tw_osli_dbg_dprintf(3, sc,
268 "XPT_SCSI_IO: Single virtual address!");
269 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
270 if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
271 tw_osli_printf(sc, "size = %d",
272 TW_CL_SEVERITY_ERROR_STRING,
273 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
274 0x2106,
275 "I/O size too big",
276 csio->dxfer_len);
277 ccb_h->status = CAM_REQ_TOO_BIG;
278 xpt_done(ccb);
279 return(1);
280 }
281
282 if ((req->length = csio->dxfer_len)) {
283 req->data = csio->data_ptr;
284 scsi_req->sgl_entries = 1;
285 }
286 } else {
287 tw_osli_printf(sc, "",
288 TW_CL_SEVERITY_ERROR_STRING,
289 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
290 0x2107,
291 "XPT_SCSI_IO: Got SGList");
292 ccb_h->status = CAM_REQ_INVALID;
293 xpt_done(ccb);
294 return(1);
295 }
296 } else {
297 /* Data addresses are physical. */
298 tw_osli_printf(sc, "",
299 TW_CL_SEVERITY_ERROR_STRING,
300 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
301 0x2108,
302 "XPT_SCSI_IO: Physical data addresses");
303 ccb_h->status = CAM_REQ_INVALID;
304 ccb_h->status &= ~CAM_SIM_QUEUED;
305 xpt_done(ccb);
306 return(1);
307 }
308
309 ccb_h->timeout_ch = timeout(twa_timeout, req,
310 (ccb_h->timeout * hz) / 1000);
311 /*
312 * twa_map_load_data_callback will fill in the SGL,
313 * and submit the I/O.
314 */
315 error = tw_osli_map_request(req);
316 return(error);
317}
318
319
320
321/*
322 * Function name: twa_action
323 * Description: Driver entry point for CAM's use.
324 *
325 * Input: sim -- sim corresponding to the ctlr
326 * ccb -- ptr to CAM request
327 * Output: None
328 * Return value: None
329 */
330TW_VOID
331twa_action(struct cam_sim *sim, union ccb *ccb)
332{
333 struct twa_softc *sc = (struct twa_softc *)cam_sim_softc(sim);
334 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
335
336 switch (ccb_h->func_code) {
337 case XPT_SCSI_IO: /* SCSI I/O */
338 {
339 struct tw_osli_req_context *req;
340
341 req = tw_osli_get_request(sc);
342 if (req == NULL) {
343 tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
344 /*
345 * Freeze the simq to maintain ccb ordering. The next
346 * ccb that gets completed will unfreeze the simq.
347 */
348 ccb_h->status |= CAM_REQUEUE_REQ;
349 xpt_done(ccb);
350 break;
351 }
352 req->req_handle.osl_req_ctxt = req;
353 req->req_handle.is_io = TW_CL_TRUE;
354 req->orig_req = ccb;
355 if (tw_osli_execute_scsi(req, ccb))
356 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
357 break;
358 }
359
360 case XPT_ABORT:
361 tw_osli_dbg_dprintf(2, sc, "Abort request.");
362 ccb_h->status = CAM_UA_ABORT;
363 xpt_done(ccb);
364 break;
365
366 case XPT_RESET_BUS:
367 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
368 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
369 0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
370 "Received Reset Bus request from CAM",
371 " ");
372
373 mtx_unlock(sc->sim_lock);
374 if (tw_cl_reset_ctlr(&sc->ctlr_handle)) {
375 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
376 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
377 0x2109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
378 "Failed to reset bus",
379 " ");
380 ccb_h->status = CAM_REQ_CMP_ERR;
381 }
382 else
383 ccb_h->status = CAM_REQ_CMP;
384
385 mtx_lock(sc->sim_lock);
386 xpt_done(ccb);
387 break;
388
389 case XPT_SET_TRAN_SETTINGS:
390 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
391
392 /*
393 * This command is not supported, since it's very specific
394 * to SCSI, and we are doing ATA.
395 */
396 ccb_h->status = CAM_FUNC_NOTAVAIL;
397 xpt_done(ccb);
398 break;
399
400 case XPT_GET_TRAN_SETTINGS:
401 {
402 struct ccb_trans_settings *cts = &ccb->cts;
403 struct ccb_trans_settings_scsi *scsi =
404 &cts->proto_specific.scsi;
405 struct ccb_trans_settings_spi *spi =
406 &cts->xport_specific.spi;
407
408 cts->protocol = PROTO_SCSI;
409 cts->protocol_version = SCSI_REV_2;
410 cts->transport = XPORT_SPI;
411 cts->transport_version = 2;
412
413 spi->valid = CTS_SPI_VALID_DISC;
414 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
415 scsi->valid = CTS_SCSI_VALID_TQ;
416 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
417 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
418 ccb_h->status = CAM_REQ_CMP;
419 xpt_done(ccb);
420 break;
421 }
422
423 case XPT_CALC_GEOMETRY:
424 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
425 cam_calc_geometry(&ccb->ccg, 1/* extended */);
426 xpt_done(ccb);
427 break;
428
429 case XPT_PATH_INQ: /* Path inquiry -- get twa properties */
430 {
431 struct ccb_pathinq *path_inq = &ccb->cpi;
432
433 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
434
435 path_inq->version_num = 1;
436 path_inq->hba_inquiry = 0;
437 path_inq->target_sprt = 0;
438 path_inq->hba_misc = 0;
439 path_inq->hba_eng_cnt = 0;
440 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
441 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
442 path_inq->unit_number = cam_sim_unit(sim);
443 path_inq->bus_id = cam_sim_bus(sim);
444 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
445 path_inq->base_transfer_speed = 100000;
446 strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
447 strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
448 strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
449 path_inq->transport = XPORT_SPI;
450 path_inq->transport_version = 2;
451 path_inq->protocol = PROTO_SCSI;
452 path_inq->protocol_version = SCSI_REV_2;
453 ccb_h->status = CAM_REQ_CMP;
454 xpt_done(ccb);
455 break;
456 }
457
458 default:
459 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
460 ccb_h->status = CAM_REQ_INVALID;
461 xpt_done(ccb);
462 break;
463 }
464}
465
466
467
468/*
469 * Function name: twa_poll
470 * Description: Driver entry point called when interrupts are not
471 * available.
472 *
473 * Input: sim -- sim corresponding to the controller
474 * Output: None
475 * Return value: None
476 */
477TW_VOID
478twa_poll(struct cam_sim *sim)
479{
480 struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
481
482 tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
483 tw_cl_interrupt(&(sc->ctlr_handle));
484 tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
485}
486
487
488
489/*
490 * Function name: twa_timeout
491 * Description: Driver entry point for being alerted on a request
492 * timing out.
493 *
494 * Input: arg -- ptr to timed out request
495 * Output: None
496 * Return value: None
497 */
498static TW_VOID
499twa_timeout(TW_VOID *arg)
500{
501 struct tw_osli_req_context *req =
502 (struct tw_osli_req_context *)arg;
503
504 tw_cl_create_event(&(req->ctlr->ctlr_handle), TW_CL_TRUE,
505 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
506 0x210B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
507 "Request timed out!",
508 "request = %p", req);
509 tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
510}
511
512
513
514/*
515 * Function name: tw_osli_request_bus_scan
516 * Description: Requests CAM for a scan of the bus.
517 *
518 * Input: sc -- ptr to per ctlr structure
519 * Output: None
520 * Return value: 0 -- success
521 * non-zero-- failure
522 */
523TW_INT32
524tw_osli_request_bus_scan(struct twa_softc *sc)
525{
526 union ccb *ccb;
527
528 tw_osli_dbg_dprintf(3, sc, "entering");
529
530 /* If we get here before sc->sim is initialized, return an error. */
531 if (!(sc->sim))
532 return(ENXIO);
533 if ((ccb = xpt_alloc_ccb()) == NULL)
534 return(ENOMEM);
535 mtx_lock(sc->sim_lock);
536 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sc->sim),
537 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
538 xpt_free_ccb(ccb);
539 mtx_unlock(sc->sim_lock);
540 return(EIO);
541 }
542
543 xpt_rescan(ccb);
544 mtx_unlock(sc->sim_lock);
545 return(0);
546}
547
548
549
550/*
551 * Function name: tw_osli_disallow_new_requests
552 * Description: Calls the appropriate CAM function, so as to freeze
553 * the flow of new requests from CAM to this controller.
554 *
555 * Input: sc -- ptr to OSL internal ctlr context
556 * req_handle -- ptr to request handle sent by OSL.
557 * Output: None
558 * Return value: None
559 */
560TW_VOID
561tw_osli_disallow_new_requests(struct twa_softc *sc,
562 struct tw_cl_req_handle *req_handle)
563{
564 /* Only freeze/release the simq for IOs */
565 if (req_handle->is_io) {
566 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
567 union ccb *ccb = (union ccb *)(req->orig_req);
568
569 xpt_freeze_simq(sc->sim, 1);
570 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
571 }
572}
573
574
575
576/*
577 * Function name: tw_osl_ctlr_busy
578 * Description: CL calls this function on cmd queue full or otherwise,
579 * when it is too busy to accept new requests.
580 *
581 * Input: ctlr_handle -- ptr to controller handle
582 * req_handle -- ptr to request handle sent by OSL.
583 * Output: None
584 * Return value: None
585 */
586TW_VOID
587tw_osl_ctlr_busy(struct tw_cl_ctlr_handle *ctlr_handle,
588 struct tw_cl_req_handle *req_handle)
589{
590 tw_osli_disallow_new_requests(ctlr_handle->osl_ctlr_ctxt, req_handle);
591}
592
593
594
595/*
596 * Function name: tw_osl_scan_bus
597 * Description: CL calls this function to request for a bus scan.
598 *
599 * Input: ctlr_handle -- ptr to controller handle
600 * Output: None
601 * Return value: None
602 */
603TW_VOID
604tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
605{
606 struct twa_softc *sc = ctlr_handle->osl_ctlr_ctxt;
607 TW_INT32 error;
608
609 if ((error = tw_osli_request_bus_scan(sc)))
610 tw_osli_printf(sc, "error = %d",
611 TW_CL_SEVERITY_ERROR_STRING,
612 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
613 0x2109,
614 "Bus scan request to CAM failed",
615 error);
616}
617
618
619
620/*
621 * Function name: tw_osl_complete_io
622 * Description: Called to complete CAM scsi requests.
623 *
624 * Input: req_handle -- ptr to request handle
625 * Output: None
626 * Return value: None
627 */
628TW_VOID
629tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
630{
631 struct tw_osli_req_context *req = req_handle->osl_req_ctxt;
632 struct tw_cl_req_packet *req_pkt =
633 (struct tw_cl_req_packet *)(&req->req_pkt);
634 struct tw_cl_scsi_req_packet *scsi_req;
635 struct twa_softc *sc = req->ctlr;
636 union ccb *ccb = (union ccb *)(req->orig_req);
637
638 tw_osli_dbg_dprintf(10, sc, "entering");
639
640 if (req->state != TW_OSLI_REQ_STATE_BUSY)
641 tw_osli_printf(sc, "request = %p, status = %d",
642 TW_CL_SEVERITY_ERROR_STRING,
643 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
644 0x210A,
645 "Unposted command completed!!",
646 req, req->state);
647
648 /*
649 * Remove request from the busy queue. Just mark it complete.
650 * There's no need to move it into the complete queue as we are
651 * going to be done with it right now.
652 */
653 req->state = TW_OSLI_REQ_STATE_COMPLETE;
654 tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
655
656 tw_osli_unmap_request(req);
657
658 untimeout(twa_timeout, req, ccb->ccb_h.timeout_ch);
659 if (req->error_code) {
660 /* This request never got submitted to the firmware. */
661 if (req->error_code == EBUSY) {
662 /*
663 * Cmd queue is full, or the Common Layer is out of
664 * resources. The simq will already have been frozen.
665 * When this ccb gets completed will unfreeze the simq.
666 */
667 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
668 }
669 else if (req->error_code == EFBIG)
670 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
671 else
672 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
673 } else {
674 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
675 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
676 ccb->ccb_h.status = CAM_REQ_CMP;
677 else {
678 if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
679 ccb->ccb_h.status |= CAM_TID_INVALID;
680 else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
681 ccb->ccb_h.status |= CAM_LUN_INVALID;
682 else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
683 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
684 else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
685 ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
686 /*
687 * If none of the above errors occurred, simply
688 * mark completion error.
689 */
690 if (ccb->ccb_h.status == 0)
691 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
692
693 if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
694 ccb->csio.sense_len = scsi_req->sense_len;
695 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
696 }
697 }
698
699 ccb->csio.scsi_status = scsi_req->scsi_status;
700 }
701
702 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
703 mtx_lock(sc->sim_lock);
704 xpt_done(ccb);
705 mtx_unlock(sc->sim_lock);
706 if (! req->error_code)
707 /* twa_action will free the request otherwise */
708 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
709}
710