]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/twa/tw_osl_cam.c
Add KTR support and move some performance debugging variables in the EC
[FreeBSD/FreeBSD.git] / sys / dev / twa / tw_osl_cam.c
1 /*
2  * Copyright (c) 2004-05 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$
28  */
29
30 /*
31  * AMCC'S 3ware driver for 9000 series storage controllers.
32  *
33  * Author: Vinod Kashyap
34  */
35
36
37 /*
38  * FreeBSD CAM related functions.
39  */
40
41
42 #include <dev/twa/tw_osl_includes.h>
43
44 #include <cam/cam.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/cam_sim.h>
47 #include <cam/cam_xpt_sim.h>
48 #include <cam/cam_xpt_periph.h>
49 #include <cam/cam_debug.h>
50 #include <cam/cam_periph.h>
51
52 #include <cam/scsi/scsi_all.h>
53 #include <cam/scsi/scsi_message.h>
54
55 static TW_VOID  twa_action(struct cam_sim *sim, union ccb *ccb);
56 static TW_VOID  twa_poll(struct cam_sim *sim);
57 static TW_VOID  twa_async(TW_VOID *callback_arg, TW_UINT32 code,
58         struct cam_path *path, TW_VOID *arg);
59 static TW_VOID  twa_timeout(TW_VOID *arg);
60 static TW_VOID  twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
61
62 static TW_INT32 tw_osli_execute_scsi(struct tw_osli_req_context *req,
63         union ccb *ccb);
64
65
66
67 /*
68  * Function name:       tw_osli_cam_attach
69  * Description:         Attaches the driver to CAM.
70  *
71  * Input:               sc      -- ptr to OSL internal ctlr context
72  * Output:              None
73  * Return value:        0       -- success
74  *                      non-zero-- failure
75  */
76 TW_INT32
77 tw_osli_cam_attach(struct twa_softc *sc)
78 {
79         struct cam_devq         *devq;
80         struct ccb_setasync     csa;
81         TW_INT32                error;
82
83         tw_osli_dbg_dprintf(3, sc, "entered");
84
85         /*
86          * Create the device queue for our SIM.
87          */
88         if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
89                 tw_osli_printf(sc, "error = %d",
90                         TW_CL_SEVERITY_ERROR_STRING,
91                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
92                         0x2100,
93                         "Failed to create SIM device queue",
94                         ENOMEM);
95                 return(ENOMEM);
96         }
97
98         /*
99          * Create a SIM entry.  Though we can support TW_OSLI_MAX_NUM_IOS
100          * simultaneous requests, we claim to be able to handle only
101          * (TW_OSLI_MAX_NUM_IOS - 1), so that we always have a request
102          * packet available to service ioctls.
103          */
104         tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
105         sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
106                         device_get_unit(sc->bus_dev),
107                         TW_OSLI_MAX_NUM_IOS - 1, 1, devq);
108         if (sc->sim == NULL) {
109                 cam_simq_free(devq);
110                 tw_osli_printf(sc, "error = %d",
111                         TW_CL_SEVERITY_ERROR_STRING,
112                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
113                         0x2101,
114                         "Failed to create a SIM entry",
115                         ENOMEM);
116                 return(ENOMEM);
117         }
118
119         /*
120          * Register the bus.
121          */
122         tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
123         mtx_lock(&Giant);
124         if (xpt_bus_register(sc->sim, 0) != CAM_SUCCESS) {
125                 cam_sim_free(sc->sim, TRUE);
126                 sc->sim = NULL; /* so cam_detach will not try to free it */
127                 tw_osli_printf(sc, "error = %d",
128                         TW_CL_SEVERITY_ERROR_STRING,
129                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
130                         0x2102,
131                         "Failed to register the bus",
132                         ENXIO);
133                 return(ENXIO);
134         }
135
136         tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
137         if (xpt_create_path(&sc->path, NULL,
138                                 cam_sim_path(sc->sim),
139                                 CAM_TARGET_WILDCARD,
140                                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
141                 xpt_bus_deregister(cam_sim_path (sc->sim));
142                 /* Passing TRUE to cam_sim_free will free the devq as well. */
143                 cam_sim_free(sc->sim, TRUE);
144                 tw_osli_printf(sc, "error = %d",
145                         TW_CL_SEVERITY_ERROR_STRING,
146                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
147                         0x2103,
148                         "Failed to create path",
149                         ENXIO);
150                 return(ENXIO);
151         }
152
153         tw_osli_dbg_dprintf(3, sc, "Calling xpt_setup_ccb");
154         xpt_setup_ccb(&csa.ccb_h, sc->path, 5);
155         csa.ccb_h.func_code = XPT_SASYNC_CB;
156         csa.event_enable = AC_FOUND_DEVICE | AC_LOST_DEVICE;
157         csa.callback = twa_async;
158         csa.callback_arg = sc;
159         xpt_action((union ccb *)&csa);
160         mtx_unlock(&Giant);
161
162         tw_osli_dbg_dprintf(3, sc, "Calling tw_osli_request_bus_scan");
163         /*
164          * Request a bus scan, so that CAM gets to know of
165          * the logical units that we control.
166          */
167         if ((error = tw_osli_request_bus_scan(sc)))
168                 tw_osli_printf(sc, "error = %d",
169                         TW_CL_SEVERITY_ERROR_STRING,
170                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
171                         0x2104,
172                         "Bus scan request to CAM failed",
173                         error);
174         
175         tw_osli_dbg_dprintf(3, sc, "exiting");
176         return(0);
177 }
178
179
180
181 /*
182  * Function name:       tw_osli_cam_detach
183  * Description:         Detaches the driver from CAM.
184  *
185  * Input:               sc      -- ptr to OSL internal ctlr context
186  * Output:              None
187  * Return value:        None
188  */
189 TW_VOID
190 tw_osli_cam_detach(struct twa_softc *sc)
191 {
192         tw_osli_dbg_dprintf(3, sc, "entered");
193
194         mtx_lock(&Giant);
195         if (sc->path)
196                 xpt_free_path(sc->path);
197         if (sc->sim) {
198                 xpt_bus_deregister(cam_sim_path(sc->sim));
199                 /* Passing TRUE to cam_sim_free will free the devq as well. */
200                 cam_sim_free(sc->sim, TRUE);
201         }
202         mtx_unlock(&Giant);
203 }
204
205
206
207 /*
208  * Function name:       tw_osli_execute_scsi
209  * Description:         Build a fw cmd, based on a CAM style ccb, and
210  *                      send it down.
211  *
212  * Input:               req     -- ptr to OSL internal request context
213  *                      ccb     -- ptr to CAM style ccb
214  * Output:              None
215  * Return value:        0       -- success
216  *                      non-zero-- failure
217  */
218 TW_INT32
219 tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
220 {
221         struct twa_softc                *sc = req->ctlr;
222         struct tw_cl_req_packet         *req_pkt;
223         struct tw_cl_scsi_req_packet    *scsi_req;
224         struct ccb_hdr                  *ccb_h = &(ccb->ccb_h);
225         struct ccb_scsiio               *csio = &(ccb->csio);
226         TW_INT32                        error;
227
228         tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
229                 csio->cdb_io.cdb_bytes[0]);
230
231         if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
232                 tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %x",
233                         ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
234                 ccb_h->status |= CAM_TID_INVALID;
235                 xpt_done(ccb);
236                 return(1);
237         }
238         if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
239                 tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
240                         ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
241                 ccb_h->status |= CAM_LUN_INVALID;
242                 xpt_done(ccb);
243                 return(1);
244         }
245
246         if(ccb_h->flags & CAM_CDB_PHYS) {
247                 tw_osli_printf(sc, "",
248                         TW_CL_SEVERITY_ERROR_STRING,
249                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
250                         0x2105,
251                         "Physical CDB address!");
252                 ccb_h->status = CAM_REQ_CMP_ERR;
253                 xpt_done(ccb);
254                 return(1);
255         }
256
257         /*
258          * We are going to work on this request.  Mark it as enqueued (though
259          * we don't actually queue it...)
260          */
261         ccb_h->status |= CAM_SIM_QUEUED;
262
263         if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
264                 if(ccb_h->flags & CAM_DIR_IN)
265                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
266                 else
267                         req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
268         }
269
270         /* Build the CL understood request packet for SCSI cmds. */
271         req_pkt = &req->req_pkt;
272         req_pkt->status = 0;
273         req_pkt->tw_osl_callback = tw_osl_complete_io;
274         scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
275         scsi_req->unit = ccb_h->target_id;
276         scsi_req->lun = ccb_h->target_lun;
277         scsi_req->sense_len = 0;
278         scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
279         scsi_req->scsi_status = 0;
280         if(ccb_h->flags & CAM_CDB_POINTER)
281                 scsi_req->cdb = csio->cdb_io.cdb_ptr;
282         else
283                 scsi_req->cdb = csio->cdb_io.cdb_bytes;
284         scsi_req->cdb_len = csio->cdb_len;
285
286         if (!(ccb_h->flags & CAM_DATA_PHYS)) {
287                 /* Virtual data addresses.  Need to convert them... */
288                 tw_osli_dbg_dprintf(3, sc,
289                         "XPT_SCSI_IO: Single virtual address!");
290                 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
291                         if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
292                                 tw_osli_printf(sc, "size = %d",
293                                         TW_CL_SEVERITY_ERROR_STRING,
294                                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
295                                         0x2106,
296                                         "I/O size too big",
297                                         csio->dxfer_len);
298                                 ccb_h->status = CAM_REQ_TOO_BIG;
299                                 xpt_done(ccb);
300                                 return(1);
301                         }
302
303                         if ((req->length = csio->dxfer_len)) {
304                                 req->data = csio->data_ptr;
305                                 scsi_req->sgl_entries = 1;
306                         }
307                 } else {
308                         tw_osli_printf(sc, "",
309                                 TW_CL_SEVERITY_ERROR_STRING,
310                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
311                                 0x2107,
312                                 "XPT_SCSI_IO: Got SGList");
313                         ccb_h->status = CAM_REQ_CMP_ERR;
314                         xpt_done(ccb);
315                         return(1);
316                 }
317         } else {
318                 /* Data addresses are physical. */
319                 tw_osli_printf(sc, "",
320                         TW_CL_SEVERITY_ERROR_STRING,
321                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
322                         0x2108,
323                         "XPT_SCSI_IO: Physical data addresses");
324                 ccb_h->status = CAM_REQ_CMP_ERR;
325                 ccb_h->status |= CAM_RELEASE_SIMQ;
326                 ccb_h->status &= ~CAM_SIM_QUEUED;
327                 xpt_done(ccb);
328                 return(1);
329         }
330
331         ccb_h->timeout_ch = timeout(twa_timeout, req,
332                 (ccb_h->timeout * hz) / 1000);
333         /*
334          * twa_map_load_data_callback will fill in the SGL,
335          * and submit the I/O.
336          */
337         error = tw_osli_map_request(req);
338         return(error);
339 }
340
341
342
343 /*
344  * Function name:       twa_action
345  * Description:         Driver entry point for CAM's use.
346  *
347  * Input:               sim     -- sim corresponding to the ctlr
348  *                      ccb     -- ptr to CAM request
349  * Output:              None
350  * Return value:        None
351  */
352 TW_VOID
353 twa_action(struct cam_sim *sim, union ccb *ccb)
354 {
355         struct twa_softc        *sc = (struct twa_softc *)cam_sim_softc(sim);
356         struct ccb_hdr          *ccb_h = &(ccb->ccb_h);
357
358         switch (ccb_h->func_code) {
359         case XPT_SCSI_IO:       /* SCSI I/O */
360         {
361                 struct tw_osli_req_context      *req;
362
363                 if ((sc->state & TW_OSLI_CTLR_STATE_SIMQ_FROZEN) ||
364                                 ((req = tw_osli_get_request(sc)) == NULL)) {
365                         tw_osli_dbg_dprintf(2, sc,
366                                 "simq frozen/Cannot get request pkt.");
367                         /*
368                          * Freeze the simq to maintain ccb ordering.  The next
369                          * ccb that gets completed will unfreeze the simq.
370                          */
371                         tw_osli_disallow_new_requests(sc);
372                         ccb_h->status |= CAM_REQUEUE_REQ;
373                         xpt_done(ccb);
374                         break;
375                 }
376                 req->req_handle.osl_req_ctxt = req;
377                 req->orig_req = ccb;
378                 if (tw_osli_execute_scsi(req, ccb))
379                         tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
380                 break;
381         }
382
383         case XPT_ABORT:
384                 tw_osli_dbg_dprintf(2, sc, "Abort request.");
385                 ccb_h->status = CAM_UA_ABORT;
386                 xpt_done(ccb);
387                 break;
388
389         case XPT_RESET_BUS:
390                 tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
391                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
392                         0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
393                         "Received Reset Bus request from CAM",
394                         " ");
395
396                 if (tw_cl_reset_ctlr(&sc->ctlr_handle)) {
397                         tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
398                                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
399                                 0x2109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
400                                 "Failed to reset bus",
401                                 " ");
402                         ccb_h->status = CAM_REQ_CMP_ERR;
403                 }
404                 else
405                         ccb_h->status = CAM_REQ_CMP;
406
407                 xpt_done(ccb);
408                 break;
409
410         case XPT_SET_TRAN_SETTINGS:
411                 tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
412
413                 /*
414                  * This command is not supported, since it's very specific
415                  * to SCSI, and we are doing ATA.
416                  */
417                 ccb_h->status = CAM_FUNC_NOTAVAIL;
418                 xpt_done(ccb);
419                 break;
420
421         case XPT_GET_TRAN_SETTINGS: 
422         {
423                 struct ccb_trans_settings       *cts = &ccb->cts;
424
425                 tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
426                 cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
427                 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
428                 ccb_h->status = CAM_REQ_CMP;
429                 xpt_done(ccb);
430                 break;
431         }
432
433         case XPT_CALC_GEOMETRY:
434                 tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
435                 cam_calc_geometry(&ccb->ccg, 1/* extended */);
436                 xpt_done(ccb);
437                 break;
438
439         case XPT_PATH_INQ:    /* Path inquiry -- get twa properties */
440         {
441                 struct ccb_pathinq      *path_inq = &ccb->cpi;
442
443                 tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
444
445                 path_inq->version_num = 1;
446                 path_inq->hba_inquiry = 0;
447                 path_inq->target_sprt = 0;
448                 path_inq->hba_misc = 0;
449                 path_inq->hba_eng_cnt = 0;
450                 path_inq->max_target = TW_CL_MAX_NUM_UNITS;
451                 path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
452                 path_inq->unit_number = cam_sim_unit(sim);
453                 path_inq->bus_id = cam_sim_bus(sim);
454                 path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
455                 path_inq->base_transfer_speed = 100000;
456                 strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
457                 strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
458                 strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
459                 ccb_h->status = CAM_REQ_CMP;
460                 xpt_done(ccb);
461                 break;
462         }
463
464         default:
465                 tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
466                 ccb_h->status = CAM_REQ_INVALID;
467                 xpt_done(ccb);
468                 break;
469         }
470 }
471
472
473
474 /*
475  * Function name:       twa_poll
476  * Description:         Driver entry point called when interrupts are not
477  *                      available.
478  *
479  * Input:               sim     -- sim corresponding to the controller
480  * Output:              None
481  * Return value:        None
482  */
483 TW_VOID
484 twa_poll(struct cam_sim *sim)
485 {
486         struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
487
488         tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
489         /*
490          * It's been observed that twa_poll can get called (from
491          * dashutdown --> xpt_polled_action) even when interrupts are
492          * active, in which case, the ISR might clear the interrupt,
493          * leaving the call to tw_cl_interrupt below, no way of determining
494          * that the response from firmware is ready, resulting in
495          * tw_cl_deferred_interrupt never getting called.  To cover this case,
496          * we will make the call to tw_cl_deferred_interrupt not dependent
497          * on the return value from tw_cl_interrupt.
498          */
499         tw_cl_interrupt(&(sc->ctlr_handle));
500         tw_cl_deferred_interrupt(&(sc->ctlr_handle));
501         tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
502 }
503
504
505
506 /*
507  * Function name:       twa_async
508  * Description:         Driver entry point for CAM to notify driver of special
509  *                      events.  We don't use this for now.
510  *
511  * Input:               callback_arg    -- ptr to per ctlr structure
512  *                      code            -- code associated with the event
513  *                      path            -- cam path
514  *                      arg             -- 
515  * Output:              None
516  * Return value:        0       -- success
517  *                      non-zero-- failure
518  */
519 TW_VOID
520 twa_async(TW_VOID *callback_arg, TW_UINT32 code, 
521         struct cam_path *path, TW_VOID *arg)
522 {
523 #ifdef TW_OSL_DEBUG
524         struct twa_softc *sc = (struct twa_softc *)callback_arg;
525 #endif /* TW_OSL_DEBUG */
526
527         tw_osli_dbg_dprintf(3, sc, "sc = %p, code = %x, path = %p, arg = %p",
528                 sc, code, path, arg);
529 }
530
531
532
533 /*
534  * Function name:       twa_timeout
535  * Description:         Driver entry point for being alerted on a request
536  *                      timing out.
537  *
538  * Input:               arg     -- ptr to timed out request
539  * Output:              None
540  * Return value:        None
541  */
542 static TW_VOID
543 twa_timeout(TW_VOID *arg)
544 {
545         struct tw_osli_req_context      *req =
546                 (struct tw_osli_req_context *)arg;
547
548         tw_cl_create_event(&(req->ctlr->ctlr_handle), TW_CL_TRUE,
549                 TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
550                 0x210B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
551                 "Request timed out!",
552                 "request = %p", req);
553         tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
554 }
555
556
557
558 /*
559  * Function name:       tw_osli_request_bus_scan
560  * Description:         Requests CAM for a scan of the bus.
561  *
562  * Input:               sc      -- ptr to per ctlr structure
563  * Output:              None
564  * Return value:        0       -- success
565  *                      non-zero-- failure
566  */
567 TW_INT32
568 tw_osli_request_bus_scan(struct twa_softc *sc)
569 {
570         struct cam_path *path;
571         union ccb       *ccb;
572
573         tw_osli_dbg_dprintf(3, sc, "entering");
574
575         /* If we get here before sc->sim is initialized, return an error. */
576         if (!(sc->sim))
577                 return(ENXIO);
578         if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK)) == NULL)
579                 return(ENOMEM);
580         bzero(ccb, sizeof(union ccb));
581         mtx_lock(&Giant);
582         if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->sim),
583                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
584                 return(EIO);
585
586         xpt_setup_ccb(&ccb->ccb_h, path, 5);
587         ccb->ccb_h.func_code = XPT_SCAN_BUS;
588         ccb->ccb_h.cbfcnp = twa_bus_scan_cb;
589         ccb->crcn.flags = CAM_FLAG_NONE;
590         xpt_action(ccb);
591         mtx_unlock(&Giant);
592         return(0);
593 }
594
595
596
597 /*
598  * Function name:       twa_bus_scan_cb
599  * Description:         Callback from CAM on a bus scan request.
600  *
601  * Input:               periph  -- we don't use this
602  *                      ccb     -- bus scan request ccb that we sent to CAM
603  * Output:              None
604  * Return value:        None
605  */
606 static TW_VOID
607 twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
608 {
609         tw_osli_dbg_printf(3, "entering");
610
611         if (ccb->ccb_h.status != CAM_REQ_CMP)
612                 printf("cam_scan_callback: failure status = %x\n",
613                         ccb->ccb_h.status);
614         else
615                 tw_osli_dbg_printf(3, "success");
616
617         xpt_free_path(ccb->ccb_h.path);
618         free(ccb, M_TEMP);
619 }
620
621
622
623 /*
624  * Function name:       tw_osli_allow_new_requests
625  * Description:         Sets the appropriate status bits in a ccb such that,
626  *                      when the ccb is completed by a call to xpt_done,
627  *                      CAM knows that it's ok to unfreeze the flow of new
628  *                      requests to this controller, if the flow is frozen.
629  *
630  * Input:               sc      -- ptr to OSL internal ctlr context
631  *                      ccb     -- ptr to CAM request
632  * Output:              None
633  * Return value:        None
634  */
635 TW_VOID
636 tw_osli_allow_new_requests(struct twa_softc *sc, TW_VOID *ccb)
637 {
638         ((union ccb *)(ccb))->ccb_h.status |= CAM_RELEASE_SIMQ;
639         sc->state &= ~TW_OSLI_CTLR_STATE_SIMQ_FROZEN;
640 }
641
642
643
644 /*
645  * Function name:       tw_osli_disallow_new_requests
646  * Description:         Calls the appropriate CAM function, so as to freeze
647  *                      the flow of new requests from CAM to this controller.
648  *
649  * Input:               sc      -- ptr to OSL internal ctlr context
650  * Output:              None
651  * Return value:        None
652  */
653 TW_VOID
654 tw_osli_disallow_new_requests(struct twa_softc *sc)
655 {
656         mtx_lock(&Giant);
657         xpt_freeze_simq(sc->sim, 1);
658         mtx_unlock(&Giant);
659         sc->state |= TW_OSLI_CTLR_STATE_SIMQ_FROZEN;
660 }
661
662
663
664 /*
665  * Function name:       tw_osl_ctlr_busy
666  * Description:         CL calls this function on cmd queue full or otherwise,
667  *                      when it is too busy to accept new requests.
668  *
669  * Input:               ctlr_handle     -- ptr to controller handle
670  *                      req_handle      -- ptr to request handle sent by OSL.
671  * Output:              None
672  * Return value:        None
673  */
674 TW_VOID
675 tw_osl_ctlr_busy(struct tw_cl_ctlr_handle *ctlr_handle,
676         struct tw_cl_req_handle *req_handle)
677 {
678         tw_osli_disallow_new_requests(ctlr_handle->osl_ctlr_ctxt);
679 }
680
681
682
683 /*
684  * Function name:       tw_osl_scan_bus
685  * Description:         CL calls this function to request for a bus scan.
686  *
687  * Input:               ctlr_handle     -- ptr to controller handle
688  * Output:              None
689  * Return value:        None
690  */
691 TW_VOID
692 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
693 {
694         struct twa_softc        *sc = ctlr_handle->osl_ctlr_ctxt;
695         TW_INT32                error;
696
697         if ((error = tw_osli_request_bus_scan(sc)))
698                 tw_osli_printf(sc, "error = %d",
699                         TW_CL_SEVERITY_ERROR_STRING,
700                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
701                         0x2109,
702                         "Bus scan request to CAM failed",
703                         error);
704 }
705
706
707
708 /*
709  * Function name:       tw_osl_complete_io
710  * Description:         Called to complete CAM scsi requests.
711  *
712  * Input:               req_handle      -- ptr to request handle
713  * Output:              None
714  * Return value:        None
715  */
716 TW_VOID
717 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
718 {
719         struct tw_osli_req_context      *req = req_handle->osl_req_ctxt;
720         struct tw_cl_req_packet         *req_pkt =
721                 (struct tw_cl_req_packet *)(&req->req_pkt);
722         struct tw_cl_scsi_req_packet    *scsi_req;
723         struct twa_softc                *sc = req->ctlr;
724         union ccb                       *ccb = (union ccb *)(req->orig_req);
725
726         tw_osli_dbg_dprintf(10, sc, "entering");
727
728         if (req->state != TW_OSLI_REQ_STATE_BUSY)
729                 tw_osli_printf(sc, "request = %p, status = %d",
730                         TW_CL_SEVERITY_ERROR_STRING,
731                         TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
732                         0x210A,
733                         "Unposted command completed!!",
734                         req, req->state);
735
736         /*
737          * Remove request from the busy queue.  Just mark it complete.
738          * There's no need to move it into the complete queue as we are
739          * going to be done with it right now.
740          */
741         req->state = TW_OSLI_REQ_STATE_COMPLETE;
742         tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
743
744         tw_osli_unmap_request(req);
745
746         untimeout(twa_timeout, req, ccb->ccb_h.timeout_ch);
747         if (req->error_code) {
748                 /* This request never got submitted to the firmware. */
749                 if (req->error_code == EBUSY) {
750                         /*
751                          * Cmd queue is full, or the Common Layer is out of
752                          * resources.  The simq will already have been frozen
753                          * by CL's call to tw_osl_ctlr_busy, and this will
754                          * maintain ccb ordering.  The next ccb that gets
755                          * completed will unfreeze the simq.
756                          */
757                         ccb->ccb_h.status |= CAM_REQUEUE_REQ;
758                 }
759                 else if (req->error_code == EFBIG)
760                         ccb->ccb_h.status = CAM_REQ_TOO_BIG;
761                 else
762                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
763         } else {
764                 scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
765                 if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
766                         ccb->ccb_h.status = CAM_REQ_CMP;
767                 else {
768                         if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
769                                 ccb->ccb_h.status |= CAM_TID_INVALID;
770                         else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
771                                 ccb->ccb_h.status |= CAM_LUN_INVALID;
772                         else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
773                                 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
774                         else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
775                                 ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
776                         /*
777                          * If none of the above errors occurred, simply
778                          * mark completion error.
779                          */
780                         if (ccb->ccb_h.status == 0)
781                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
782
783                         if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
784                                 ccb->csio.sense_len = scsi_req->sense_len;
785                                 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
786                         }
787                 }
788
789                 ccb->csio.scsi_status = scsi_req->scsi_status;
790                 /* If simq is frozen, unfreeze it. */
791                 if (sc->state & TW_OSLI_CTLR_STATE_SIMQ_FROZEN)
792                         tw_osli_allow_new_requests(sc, (TW_VOID *)ccb);
793         }
794
795         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
796         xpt_done(ccb);
797         if (! req->error_code)
798                  /* twa_action will free the request otherwise */
799                 tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
800 }
801