mpt.c revision 164837
1169691Skan/*- 2169691Skan * Generic routines for LSI Fusion adapters. 3169691Skan * FreeBSD Version. 4169691Skan * 5169691Skan * Copyright (c) 2000, 2001 by Greg Ansley 6169691Skan * 7169691Skan * Redistribution and use in source and binary forms, with or without 8169691Skan * modification, are permitted provided that the following conditions 9169691Skan * are met: 10169691Skan * 1. Redistributions of source code must retain the above copyright 11169691Skan * notice immediately at the beginning of the file, without modification, 12169691Skan * this list of conditions, and the following disclaimer. 13169691Skan * 2. The name of the author may not be used to endorse or promote products 14169691Skan * derived from this software without specific prior written permission. 15169691Skan * 16169691Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17169691Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19169691Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20169691Skan * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21169691Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22169691Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23169691Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24169691Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25169691Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26169691Skan * SUCH DAMAGE. 27169691Skan */ 28169691Skan/*- 29169691Skan * Copyright (c) 2002, 2006 by Matthew Jacob 30169691Skan * All rights reserved. 31169691Skan * 32169691Skan * Redistribution and use in source and binary forms, with or without 33169691Skan * modification, are permitted provided that the following conditions are 34169691Skan * met: 35169691Skan * 1. Redistributions of source code must retain the above copyright 36169691Skan * notice, this list of conditions and the following disclaimer. 37169691Skan * 2. Redistributions in binary form must reproduce at minimum a disclaimer 38169691Skan * substantially similar to the "NO WARRANTY" disclaimer below 39169691Skan * ("Disclaimer") and any redistribution must be conditioned upon including 40169691Skan * a substantially similar Disclaimer requirement for further binary 41169691Skan * redistribution. 42169691Skan * 3. Neither the names of the above listed copyright holders nor the names 43169691Skan * of any contributors may be used to endorse or promote products derived 44169691Skan * from this software without specific prior written permission. 45169691Skan * 46169691Skan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 47169691Skan * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48169691Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49169691Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 50169691Skan * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51169691Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52169691Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53169691Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54169691Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55169691Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT 56169691Skan * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57169691Skan * 58169691Skan * Support from Chris Ellsworth in order to make SAS adapters work 59169691Skan * is gratefully acknowledged. 60169691Skan * 61169691Skan * 62 * Support from LSI-Logic has also gone a great deal toward making this a 63 * workable subsystem and is gratefully acknowledged. 64 */ 65/*- 66 * Copyright (c) 2004, Avid Technology, Inc. and its contributors. 67 * Copyright (c) 2005, WHEEL Sp. z o.o. 68 * Copyright (c) 2004, 2005 Justin T. Gibbs 69 * All rights reserved. 70 * 71 * Redistribution and use in source and binary forms, with or without 72 * modification, are permitted provided that the following conditions are 73 * met: 74 * 1. Redistributions of source code must retain the above copyright 75 * notice, this list of conditions and the following disclaimer. 76 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 77 * substantially similar to the "NO WARRANTY" disclaimer below 78 * ("Disclaimer") and any redistribution must be conditioned upon including 79 * a substantially similar Disclaimer requirement for further binary 80 * redistribution. 81 * 3. Neither the names of the above listed copyright holders nor the names 82 * of any contributors may be used to endorse or promote products derived 83 * from this software without specific prior written permission. 84 * 85 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 86 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 89 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 90 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 91 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 92 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 93 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 94 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT 95 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 96 */ 97 98#include <sys/cdefs.h> 99__FBSDID("$FreeBSD: head/sys/dev/mpt/mpt.c 164837 2006-12-03 00:28:11Z mjacob $"); 100 101#include <dev/mpt/mpt.h> 102#include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */ 103#include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */ 104 105#include <dev/mpt/mpilib/mpi.h> 106#include <dev/mpt/mpilib/mpi_ioc.h> 107#include <dev/mpt/mpilib/mpi_fc.h> 108#include <dev/mpt/mpilib/mpi_targ.h> 109 110#include <sys/sysctl.h> 111 112#define MPT_MAX_TRYS 3 113#define MPT_MAX_WAIT 300000 114 115static int maxwait_ack = 0; 116static int maxwait_int = 0; 117static int maxwait_state = 0; 118 119static TAILQ_HEAD(, mpt_softc) mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq); 120mpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS]; 121 122static mpt_reply_handler_t mpt_default_reply_handler; 123static mpt_reply_handler_t mpt_config_reply_handler; 124static mpt_reply_handler_t mpt_handshake_reply_handler; 125static mpt_reply_handler_t mpt_event_reply_handler; 126static void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 127 MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context); 128static int mpt_send_event_request(struct mpt_softc *mpt, int onoff); 129static int mpt_soft_reset(struct mpt_softc *mpt); 130static void mpt_hard_reset(struct mpt_softc *mpt); 131static int mpt_configure_ioc(struct mpt_softc *mpt); 132static int mpt_enable_ioc(struct mpt_softc *mpt, int); 133 134/************************* Personality Module Support *************************/ 135/* 136 * We include one extra entry that is guaranteed to be NULL 137 * to simplify our itterator. 138 */ 139static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1]; 140static __inline struct mpt_personality* 141 mpt_pers_find(struct mpt_softc *, u_int); 142static __inline struct mpt_personality* 143 mpt_pers_find_reverse(struct mpt_softc *, u_int); 144 145static __inline struct mpt_personality * 146mpt_pers_find(struct mpt_softc *mpt, u_int start_at) 147{ 148 KASSERT(start_at <= MPT_MAX_PERSONALITIES, 149 ("mpt_pers_find: starting position out of range\n")); 150 151 while (start_at < MPT_MAX_PERSONALITIES 152 && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 153 start_at++; 154 } 155 return (mpt_personalities[start_at]); 156} 157 158/* 159 * Used infrequently, so no need to optimize like a forward 160 * traversal where we use the MAX+1 is guaranteed to be NULL 161 * trick. 162 */ 163static __inline struct mpt_personality * 164mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at) 165{ 166 while (start_at < MPT_MAX_PERSONALITIES 167 && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 168 start_at--; 169 } 170 if (start_at < MPT_MAX_PERSONALITIES) 171 return (mpt_personalities[start_at]); 172 return (NULL); 173} 174 175#define MPT_PERS_FOREACH(mpt, pers) \ 176 for (pers = mpt_pers_find(mpt, /*start_at*/0); \ 177 pers != NULL; \ 178 pers = mpt_pers_find(mpt, /*start_at*/pers->id+1)) 179 180#define MPT_PERS_FOREACH_REVERSE(mpt, pers) \ 181 for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\ 182 pers != NULL; \ 183 pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1)) 184 185static mpt_load_handler_t mpt_stdload; 186static mpt_probe_handler_t mpt_stdprobe; 187static mpt_attach_handler_t mpt_stdattach; 188static mpt_enable_handler_t mpt_stdenable; 189static mpt_ready_handler_t mpt_stdready; 190static mpt_event_handler_t mpt_stdevent; 191static mpt_reset_handler_t mpt_stdreset; 192static mpt_shutdown_handler_t mpt_stdshutdown; 193static mpt_detach_handler_t mpt_stddetach; 194static mpt_unload_handler_t mpt_stdunload; 195static struct mpt_personality mpt_default_personality = 196{ 197 .load = mpt_stdload, 198 .probe = mpt_stdprobe, 199 .attach = mpt_stdattach, 200 .enable = mpt_stdenable, 201 .ready = mpt_stdready, 202 .event = mpt_stdevent, 203 .reset = mpt_stdreset, 204 .shutdown = mpt_stdshutdown, 205 .detach = mpt_stddetach, 206 .unload = mpt_stdunload 207}; 208 209static mpt_load_handler_t mpt_core_load; 210static mpt_attach_handler_t mpt_core_attach; 211static mpt_enable_handler_t mpt_core_enable; 212static mpt_reset_handler_t mpt_core_ioc_reset; 213static mpt_event_handler_t mpt_core_event; 214static mpt_shutdown_handler_t mpt_core_shutdown; 215static mpt_shutdown_handler_t mpt_core_detach; 216static mpt_unload_handler_t mpt_core_unload; 217static struct mpt_personality mpt_core_personality = 218{ 219 .name = "mpt_core", 220 .load = mpt_core_load, 221 .attach = mpt_core_attach, 222 .enable = mpt_core_enable, 223 .event = mpt_core_event, 224 .reset = mpt_core_ioc_reset, 225 .shutdown = mpt_core_shutdown, 226 .detach = mpt_core_detach, 227 .unload = mpt_core_unload, 228}; 229 230/* 231 * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need 232 * ordering information. We want the core to always register FIRST. 233 * other modules are set to SI_ORDER_SECOND. 234 */ 235static moduledata_t mpt_core_mod = { 236 "mpt_core", mpt_modevent, &mpt_core_personality 237}; 238DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 239MODULE_VERSION(mpt_core, 1); 240 241#define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id)) 242 243int 244mpt_modevent(module_t mod, int type, void *data) 245{ 246 struct mpt_personality *pers; 247 int error; 248 249 pers = (struct mpt_personality *)data; 250 251 error = 0; 252 switch (type) { 253 case MOD_LOAD: 254 { 255 mpt_load_handler_t **def_handler; 256 mpt_load_handler_t **pers_handler; 257 int i; 258 259 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 260 if (mpt_personalities[i] == NULL) 261 break; 262 } 263 if (i >= MPT_MAX_PERSONALITIES) { 264 error = ENOMEM; 265 break; 266 } 267 pers->id = i; 268 mpt_personalities[i] = pers; 269 270 /* Install standard/noop handlers for any NULL entries. */ 271 def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality); 272 pers_handler = MPT_PERS_FIRST_HANDLER(pers); 273 while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) { 274 if (*pers_handler == NULL) 275 *pers_handler = *def_handler; 276 pers_handler++; 277 def_handler++; 278 } 279 280 error = (pers->load(pers)); 281 if (error != 0) 282 mpt_personalities[i] = NULL; 283 break; 284 } 285 case MOD_SHUTDOWN: 286 break; 287#if __FreeBSD_version >= 500000 288 case MOD_QUIESCE: 289 break; 290#endif 291 case MOD_UNLOAD: 292 error = pers->unload(pers); 293 mpt_personalities[pers->id] = NULL; 294 break; 295 default: 296 error = EINVAL; 297 break; 298 } 299 return (error); 300} 301 302int 303mpt_stdload(struct mpt_personality *pers) 304{ 305 /* Load is always successfull. */ 306 return (0); 307} 308 309int 310mpt_stdprobe(struct mpt_softc *mpt) 311{ 312 /* Probe is always successfull. */ 313 return (0); 314} 315 316int 317mpt_stdattach(struct mpt_softc *mpt) 318{ 319 /* Attach is always successfull. */ 320 return (0); 321} 322 323int 324mpt_stdenable(struct mpt_softc *mpt) 325{ 326 /* Enable is always successfull. */ 327 return (0); 328} 329 330void 331mpt_stdready(struct mpt_softc *mpt) 332{ 333} 334 335 336int 337mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg) 338{ 339 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF); 340 /* Event was not for us. */ 341 return (0); 342} 343 344void 345mpt_stdreset(struct mpt_softc *mpt, int type) 346{ 347} 348 349void 350mpt_stdshutdown(struct mpt_softc *mpt) 351{ 352} 353 354void 355mpt_stddetach(struct mpt_softc *mpt) 356{ 357} 358 359int 360mpt_stdunload(struct mpt_personality *pers) 361{ 362 /* Unload is always successfull. */ 363 return (0); 364} 365 366/* 367 * Post driver attachment, we may want to perform some global actions. 368 * Here is the hook to do so. 369 */ 370 371static void 372mpt_postattach(void *unused) 373{ 374 struct mpt_softc *mpt; 375 struct mpt_personality *pers; 376 377 TAILQ_FOREACH(mpt, &mpt_tailq, links) { 378 MPT_PERS_FOREACH(mpt, pers) 379 pers->ready(mpt); 380 } 381} 382SYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL); 383 384 385/******************************* Bus DMA Support ******************************/ 386void 387mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 388{ 389 struct mpt_map_info *map_info; 390 391 map_info = (struct mpt_map_info *)arg; 392 map_info->error = error; 393 map_info->phys = segs->ds_addr; 394} 395 396/**************************** Reply/Event Handling ****************************/ 397int 398mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type, 399 mpt_handler_t handler, uint32_t *phandler_id) 400{ 401 402 switch (type) { 403 case MPT_HANDLER_REPLY: 404 { 405 u_int cbi; 406 u_int free_cbi; 407 408 if (phandler_id == NULL) 409 return (EINVAL); 410 411 free_cbi = MPT_HANDLER_ID_NONE; 412 for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) { 413 /* 414 * If the same handler is registered multiple 415 * times, don't error out. Just return the 416 * index of the original registration. 417 */ 418 if (mpt_reply_handlers[cbi] == handler.reply_handler) { 419 *phandler_id = MPT_CBI_TO_HID(cbi); 420 return (0); 421 } 422 423 /* 424 * Fill from the front in the hope that 425 * all registered handlers consume only a 426 * single cache line. 427 * 428 * We don't break on the first empty slot so 429 * that the full table is checked to see if 430 * this handler was previously registered. 431 */ 432 if (free_cbi == MPT_HANDLER_ID_NONE && 433 (mpt_reply_handlers[cbi] 434 == mpt_default_reply_handler)) 435 free_cbi = cbi; 436 } 437 if (free_cbi == MPT_HANDLER_ID_NONE) { 438 return (ENOMEM); 439 } 440 mpt_reply_handlers[free_cbi] = handler.reply_handler; 441 *phandler_id = MPT_CBI_TO_HID(free_cbi); 442 break; 443 } 444 default: 445 mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type); 446 return (EINVAL); 447 } 448 return (0); 449} 450 451int 452mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type, 453 mpt_handler_t handler, uint32_t handler_id) 454{ 455 456 switch (type) { 457 case MPT_HANDLER_REPLY: 458 { 459 u_int cbi; 460 461 cbi = MPT_CBI(handler_id); 462 if (cbi >= MPT_NUM_REPLY_HANDLERS 463 || mpt_reply_handlers[cbi] != handler.reply_handler) 464 return (ENOENT); 465 mpt_reply_handlers[cbi] = mpt_default_reply_handler; 466 break; 467 } 468 default: 469 mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type); 470 return (EINVAL); 471 } 472 return (0); 473} 474 475static int 476mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req, 477 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 478{ 479 mpt_prt(mpt, 480 "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n", 481 req, req->serno, reply_desc, reply_frame); 482 483 if (reply_frame != NULL) 484 mpt_dump_reply_frame(mpt, reply_frame); 485 486 mpt_prt(mpt, "Reply Frame Ignored\n"); 487 488 return (/*free_reply*/TRUE); 489} 490 491static int 492mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req, 493 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 494{ 495 if (req != NULL) { 496 497 if (reply_frame != NULL) { 498 MSG_CONFIG *cfgp; 499 MSG_CONFIG_REPLY *reply; 500 501 cfgp = (MSG_CONFIG *)req->req_vbuf; 502 reply = (MSG_CONFIG_REPLY *)reply_frame; 503 req->IOCStatus = le16toh(reply_frame->IOCStatus); 504 bcopy(&reply->Header, &cfgp->Header, 505 sizeof(cfgp->Header)); 506 } 507 req->state &= ~REQ_STATE_QUEUED; 508 req->state |= REQ_STATE_DONE; 509 TAILQ_REMOVE(&mpt->request_pending_list, req, links); 510 if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) { 511 wakeup(req); 512 } else if ((req->state & REQ_STATE_TIMEDOUT) != 0) { 513 /* 514 * Whew- we can free this request (late completion) 515 */ 516 mpt_free_request(mpt, req); 517 } 518 } 519 520 return (TRUE); 521} 522 523static int 524mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req, 525 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 526{ 527 /* Nothing to be done. */ 528 return (TRUE); 529} 530 531static int 532mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req, 533 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 534{ 535 int free_reply; 536 537 KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler")); 538 KASSERT(req != NULL, ("null request in mpt_event_reply_handler")); 539 540 free_reply = TRUE; 541 switch (reply_frame->Function) { 542 case MPI_FUNCTION_EVENT_NOTIFICATION: 543 { 544 MSG_EVENT_NOTIFY_REPLY *msg; 545 struct mpt_personality *pers; 546 u_int handled; 547 548 handled = 0; 549 msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 550 msg->EventDataLength = le16toh(msg->EventDataLength); 551 msg->IOCStatus = le16toh(msg->IOCStatus); 552 msg->IOCLogInfo = le32toh(msg->IOCLogInfo); 553 msg->Event = le32toh(msg->Event); 554 MPT_PERS_FOREACH(mpt, pers) 555 handled += pers->event(mpt, req, msg); 556 557 if (handled == 0 && mpt->mpt_pers_mask == 0) { 558 mpt_lprt(mpt, MPT_PRT_INFO, 559 "No Handlers For Any Event Notify Frames. " 560 "Event %#x (ACK %sequired).\n", 561 msg->Event, msg->AckRequired? "r" : "not r"); 562 } else if (handled == 0) { 563 mpt_lprt(mpt, MPT_PRT_WARN, 564 "Unhandled Event Notify Frame. Event %#x " 565 "(ACK %sequired).\n", 566 msg->Event, msg->AckRequired? "r" : "not r"); 567 } 568 569 if (msg->AckRequired) { 570 request_t *ack_req; 571 uint32_t context; 572 573 context = req->index | MPT_REPLY_HANDLER_EVENTS; 574 ack_req = mpt_get_request(mpt, FALSE); 575 if (ack_req == NULL) { 576 struct mpt_evtf_record *evtf; 577 578 evtf = (struct mpt_evtf_record *)reply_frame; 579 evtf->context = context; 580 LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links); 581 free_reply = FALSE; 582 break; 583 } 584 mpt_send_event_ack(mpt, ack_req, msg, context); 585 /* 586 * Don't check for CONTINUATION_REPLY here 587 */ 588 return (free_reply); 589 } 590 break; 591 } 592 case MPI_FUNCTION_PORT_ENABLE: 593 mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n"); 594 break; 595 case MPI_FUNCTION_EVENT_ACK: 596 break; 597 default: 598 mpt_prt(mpt, "unknown event function: %x\n", 599 reply_frame->Function); 600 break; 601 } 602 603 /* 604 * I'm not sure that this continuation stuff works as it should. 605 * 606 * I've had FC async events occur that free the frame up because 607 * the continuation bit isn't set, and then additional async events 608 * then occur using the same context. As you might imagine, this 609 * leads to Very Bad Thing. 610 * 611 * Let's just be safe for now and not free them up until we figure 612 * out what's actually happening here. 613 */ 614#if 0 615 if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) { 616 TAILQ_REMOVE(&mpt->request_pending_list, req, links); 617 mpt_free_request(mpt, req); 618 mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation", 619 reply_frame->Function, req, req->serno); 620 if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 621 MSG_EVENT_NOTIFY_REPLY *msg = 622 (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 623 mpt_prtc(mpt, " Event=0x%x AckReq=%d", 624 msg->Event, msg->AckRequired); 625 } 626 } else { 627 mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation", 628 reply_frame->Function, req, req->serno); 629 if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 630 MSG_EVENT_NOTIFY_REPLY *msg = 631 (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 632 mpt_prtc(mpt, " Event=0x%x AckReq=%d", 633 msg->Event, msg->AckRequired); 634 } 635 mpt_prtc(mpt, "\n"); 636 } 637#endif 638 return (free_reply); 639} 640 641/* 642 * Process an asynchronous event from the IOC. 643 */ 644static int 645mpt_core_event(struct mpt_softc *mpt, request_t *req, 646 MSG_EVENT_NOTIFY_REPLY *msg) 647{ 648 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n", 649 msg->Event & 0xFF); 650 switch(msg->Event & 0xFF) { 651 case MPI_EVENT_NONE: 652 break; 653 case MPI_EVENT_LOG_DATA: 654 { 655 int i; 656 657 /* Some error occured that LSI wants logged */ 658 mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n", 659 msg->IOCLogInfo); 660 mpt_prt(mpt, "\tEvtLogData: Event Data:"); 661 for (i = 0; i < msg->EventDataLength; i++) 662 mpt_prtc(mpt, " %08x", msg->Data[i]); 663 mpt_prtc(mpt, "\n"); 664 break; 665 } 666 case MPI_EVENT_EVENT_CHANGE: 667 /* 668 * This is just an acknowledgement 669 * of our mpt_send_event_request. 670 */ 671 break; 672 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE: 673 break; 674 default: 675 return (0); 676 break; 677 } 678 return (1); 679} 680 681static void 682mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 683 MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context) 684{ 685 MSG_EVENT_ACK *ackp; 686 687 ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf; 688 memset(ackp, 0, sizeof (*ackp)); 689 ackp->Function = MPI_FUNCTION_EVENT_ACK; 690 ackp->Event = htole32(msg->Event); 691 ackp->EventContext = htole32(msg->EventContext); 692 ackp->MsgContext = htole32(context); 693 mpt_check_doorbell(mpt); 694 mpt_send_cmd(mpt, ack_req); 695} 696 697/***************************** Interrupt Handling *****************************/ 698void 699mpt_intr(void *arg) 700{ 701 struct mpt_softc *mpt; 702 uint32_t reply_desc; 703 int ntrips = 0; 704 705 mpt = (struct mpt_softc *)arg; 706 mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n"); 707 while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) { 708 request_t *req; 709 MSG_DEFAULT_REPLY *reply_frame; 710 uint32_t reply_baddr; 711 uint32_t ctxt_idx; 712 u_int cb_index; 713 u_int req_index; 714 int free_rf; 715 716 req = NULL; 717 reply_frame = NULL; 718 reply_baddr = 0; 719 if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) { 720 u_int offset; 721 /* 722 * Insure that the reply frame is coherent. 723 */ 724 reply_baddr = MPT_REPLY_BADDR(reply_desc); 725 offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF); 726 bus_dmamap_sync_range(mpt->reply_dmat, 727 mpt->reply_dmap, offset, MPT_REPLY_SIZE, 728 BUS_DMASYNC_POSTREAD); 729 reply_frame = MPT_REPLY_OTOV(mpt, offset); 730 ctxt_idx = le32toh(reply_frame->MsgContext); 731 } else { 732 uint32_t type; 733 734 type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc); 735 ctxt_idx = reply_desc; 736 mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n", 737 reply_desc); 738 739 switch (type) { 740 case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT: 741 ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK; 742 break; 743 case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET: 744 ctxt_idx = GET_IO_INDEX(reply_desc); 745 if (mpt->tgt_cmd_ptrs == NULL) { 746 mpt_prt(mpt, 747 "mpt_intr: no target cmd ptrs\n"); 748 reply_desc = MPT_REPLY_EMPTY; 749 break; 750 } 751 if (ctxt_idx >= mpt->tgt_cmds_allocated) { 752 mpt_prt(mpt, 753 "mpt_intr: bad tgt cmd ctxt %u\n", 754 ctxt_idx); 755 reply_desc = MPT_REPLY_EMPTY; 756 ntrips = 1000; 757 break; 758 } 759 req = mpt->tgt_cmd_ptrs[ctxt_idx]; 760 if (req == NULL) { 761 mpt_prt(mpt, "no request backpointer " 762 "at index %u", ctxt_idx); 763 reply_desc = MPT_REPLY_EMPTY; 764 ntrips = 1000; 765 break; 766 } 767 /* 768 * Reformulate ctxt_idx to be just as if 769 * it were another type of context reply 770 * so the code below will find the request 771 * via indexing into the pool. 772 */ 773 ctxt_idx = 774 req->index | mpt->scsi_tgt_handler_id; 775 req = NULL; 776 break; 777 case MPI_CONTEXT_REPLY_TYPE_LAN: 778 mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n", 779 reply_desc); 780 reply_desc = MPT_REPLY_EMPTY; 781 break; 782 default: 783 mpt_prt(mpt, "Context Reply 0x%08x?\n", type); 784 reply_desc = MPT_REPLY_EMPTY; 785 break; 786 } 787 if (reply_desc == MPT_REPLY_EMPTY) { 788 if (ntrips++ > 1000) { 789 break; 790 } 791 continue; 792 } 793 } 794 795 cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx); 796 req_index = MPT_CONTEXT_TO_REQI(ctxt_idx); 797 if (req_index < MPT_MAX_REQUESTS(mpt)) { 798 req = &mpt->request_pool[req_index]; 799 } else { 800 mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc ==" 801 " 0x%x)\n", req_index, reply_desc); 802 } 803 804 free_rf = mpt_reply_handlers[cb_index](mpt, req, 805 reply_desc, reply_frame); 806 807 if (reply_frame != NULL && free_rf) { 808 mpt_free_reply(mpt, reply_baddr); 809 } 810 811 /* 812 * If we got ourselves disabled, don't get stuck in a loop 813 */ 814 if (mpt->disabled) { 815 mpt_disable_ints(mpt); 816 break; 817 } 818 if (ntrips++ > 1000) { 819 break; 820 } 821 } 822 mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n"); 823} 824 825/******************************* Error Recovery *******************************/ 826void 827mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain, 828 u_int iocstatus) 829{ 830 MSG_DEFAULT_REPLY ioc_status_frame; 831 request_t *req; 832 833 memset(&ioc_status_frame, 0, sizeof(ioc_status_frame)); 834 ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4); 835 ioc_status_frame.IOCStatus = iocstatus; 836 while((req = TAILQ_FIRST(chain)) != NULL) { 837 MSG_REQUEST_HEADER *msg_hdr; 838 u_int cb_index; 839 840 TAILQ_REMOVE(chain, req, links); 841 msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf; 842 ioc_status_frame.Function = msg_hdr->Function; 843 ioc_status_frame.MsgContext = msg_hdr->MsgContext; 844 cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext)); 845 mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext, 846 &ioc_status_frame); 847 } 848} 849 850/********************************* Diagnostics ********************************/ 851/* 852 * Perform a diagnostic dump of a reply frame. 853 */ 854void 855mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame) 856{ 857 mpt_prt(mpt, "Address Reply:\n"); 858 mpt_print_reply(reply_frame); 859} 860 861/******************************* Doorbell Access ******************************/ 862static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt); 863static __inline uint32_t mpt_rd_intr(struct mpt_softc *mpt); 864 865static __inline uint32_t 866mpt_rd_db(struct mpt_softc *mpt) 867{ 868 return mpt_read(mpt, MPT_OFFSET_DOORBELL); 869} 870 871static __inline uint32_t 872mpt_rd_intr(struct mpt_softc *mpt) 873{ 874 return mpt_read(mpt, MPT_OFFSET_INTR_STATUS); 875} 876 877/* Busy wait for a door bell to be read by IOC */ 878static int 879mpt_wait_db_ack(struct mpt_softc *mpt) 880{ 881 int i; 882 for (i=0; i < MPT_MAX_WAIT; i++) { 883 if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) { 884 maxwait_ack = i > maxwait_ack ? i : maxwait_ack; 885 return (MPT_OK); 886 } 887 DELAY(200); 888 } 889 return (MPT_FAIL); 890} 891 892/* Busy wait for a door bell interrupt */ 893static int 894mpt_wait_db_int(struct mpt_softc *mpt) 895{ 896 int i; 897 for (i = 0; i < MPT_MAX_WAIT; i++) { 898 if (MPT_DB_INTR(mpt_rd_intr(mpt))) { 899 maxwait_int = i > maxwait_int ? i : maxwait_int; 900 return MPT_OK; 901 } 902 DELAY(100); 903 } 904 return (MPT_FAIL); 905} 906 907/* Wait for IOC to transition to a give state */ 908void 909mpt_check_doorbell(struct mpt_softc *mpt) 910{ 911 uint32_t db = mpt_rd_db(mpt); 912 if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) { 913 mpt_prt(mpt, "Device not running\n"); 914 mpt_print_db(db); 915 } 916} 917 918/* Wait for IOC to transition to a give state */ 919static int 920mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state) 921{ 922 int i; 923 924 for (i = 0; i < MPT_MAX_WAIT; i++) { 925 uint32_t db = mpt_rd_db(mpt); 926 if (MPT_STATE(db) == state) { 927 maxwait_state = i > maxwait_state ? i : maxwait_state; 928 return (MPT_OK); 929 } 930 DELAY(100); 931 } 932 return (MPT_FAIL); 933} 934 935 936/************************* Intialization/Configuration ************************/ 937static int mpt_download_fw(struct mpt_softc *mpt); 938 939/* Issue the reset COMMAND to the IOC */ 940static int 941mpt_soft_reset(struct mpt_softc *mpt) 942{ 943 mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n"); 944 945 /* Have to use hard reset if we are not in Running state */ 946 if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) { 947 mpt_prt(mpt, "soft reset failed: device not running\n"); 948 return (MPT_FAIL); 949 } 950 951 /* If door bell is in use we don't have a chance of getting 952 * a word in since the IOC probably crashed in message 953 * processing. So don't waste our time. 954 */ 955 if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) { 956 mpt_prt(mpt, "soft reset failed: doorbell wedged\n"); 957 return (MPT_FAIL); 958 } 959 960 /* Send the reset request to the IOC */ 961 mpt_write(mpt, MPT_OFFSET_DOORBELL, 962 MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT); 963 if (mpt_wait_db_ack(mpt) != MPT_OK) { 964 mpt_prt(mpt, "soft reset failed: ack timeout\n"); 965 return (MPT_FAIL); 966 } 967 968 /* Wait for the IOC to reload and come out of reset state */ 969 if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) { 970 mpt_prt(mpt, "soft reset failed: device did not restart\n"); 971 return (MPT_FAIL); 972 } 973 974 return MPT_OK; 975} 976 977static int 978mpt_enable_diag_mode(struct mpt_softc *mpt) 979{ 980 int try; 981 982 try = 20; 983 while (--try) { 984 985 if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0) 986 break; 987 988 /* Enable diagnostic registers */ 989 mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF); 990 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE); 991 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE); 992 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE); 993 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE); 994 mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE); 995 996 DELAY(100000); 997 } 998 if (try == 0) 999 return (EIO); 1000 return (0); 1001} 1002 1003static void 1004mpt_disable_diag_mode(struct mpt_softc *mpt) 1005{ 1006 mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF); 1007} 1008 1009/* This is a magic diagnostic reset that resets all the ARM 1010 * processors in the chip. 1011 */ 1012static void 1013mpt_hard_reset(struct mpt_softc *mpt) 1014{ 1015 int error; 1016 int wait; 1017 uint32_t diagreg; 1018 1019 mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n"); 1020 1021 error = mpt_enable_diag_mode(mpt); 1022 if (error) { 1023 mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n"); 1024 mpt_prt(mpt, "Trying to reset anyway.\n"); 1025 } 1026 1027 diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 1028 1029 /* 1030 * This appears to be a workaround required for some 1031 * firmware or hardware revs. 1032 */ 1033 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM); 1034 DELAY(1000); 1035 1036 /* Diag. port is now active so we can now hit the reset bit */ 1037 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER); 1038 1039 /* 1040 * Ensure that the reset has finished. We delay 1ms 1041 * prior to reading the register to make sure the chip 1042 * has sufficiently completed its reset to handle register 1043 * accesses. 1044 */ 1045 wait = 5000; 1046 do { 1047 DELAY(1000); 1048 diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 1049 } while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0); 1050 1051 if (wait == 0) { 1052 mpt_prt(mpt, "WARNING - Failed hard reset! " 1053 "Trying to initialize anyway.\n"); 1054 } 1055 1056 /* 1057 * If we have firmware to download, it must be loaded before 1058 * the controller will become operational. Do so now. 1059 */ 1060 if (mpt->fw_image != NULL) { 1061 1062 error = mpt_download_fw(mpt); 1063 1064 if (error) { 1065 mpt_prt(mpt, "WARNING - Firmware Download Failed!\n"); 1066 mpt_prt(mpt, "Trying to initialize anyway.\n"); 1067 } 1068 } 1069 1070 /* 1071 * Reseting the controller should have disabled write 1072 * access to the diagnostic registers, but disable 1073 * manually to be sure. 1074 */ 1075 mpt_disable_diag_mode(mpt); 1076} 1077 1078static void 1079mpt_core_ioc_reset(struct mpt_softc *mpt, int type) 1080{ 1081 /* 1082 * Complete all pending requests with a status 1083 * appropriate for an IOC reset. 1084 */ 1085 mpt_complete_request_chain(mpt, &mpt->request_pending_list, 1086 MPI_IOCSTATUS_INVALID_STATE); 1087} 1088 1089 1090/* 1091 * Reset the IOC when needed. Try software command first then if needed 1092 * poke at the magic diagnostic reset. Note that a hard reset resets 1093 * *both* IOCs on dual function chips (FC929 && LSI1030) as well as 1094 * fouls up the PCI configuration registers. 1095 */ 1096int 1097mpt_reset(struct mpt_softc *mpt, int reinit) 1098{ 1099 struct mpt_personality *pers; 1100 int ret; 1101 int retry_cnt = 0; 1102 1103 /* 1104 * Try a soft reset. If that fails, get out the big hammer. 1105 */ 1106 again: 1107 if ((ret = mpt_soft_reset(mpt)) != MPT_OK) { 1108 int cnt; 1109 for (cnt = 0; cnt < 5; cnt++) { 1110 /* Failed; do a hard reset */ 1111 mpt_hard_reset(mpt); 1112 1113 /* 1114 * Wait for the IOC to reload 1115 * and come out of reset state 1116 */ 1117 ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1118 if (ret == MPT_OK) { 1119 break; 1120 } 1121 /* 1122 * Okay- try to check again... 1123 */ 1124 ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1125 if (ret == MPT_OK) { 1126 break; 1127 } 1128 mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n", 1129 retry_cnt, cnt); 1130 } 1131 } 1132 1133 if (retry_cnt == 0) { 1134 /* 1135 * Invoke reset handlers. We bump the reset count so 1136 * that mpt_wait_req() understands that regardless of 1137 * the specified wait condition, it should stop its wait. 1138 */ 1139 mpt->reset_cnt++; 1140 MPT_PERS_FOREACH(mpt, pers) 1141 pers->reset(mpt, ret); 1142 } 1143 1144 if (reinit) { 1145 ret = mpt_enable_ioc(mpt, 1); 1146 if (ret == MPT_OK) { 1147 mpt_enable_ints(mpt); 1148 } 1149 } 1150 if (ret != MPT_OK && retry_cnt++ < 2) { 1151 goto again; 1152 } 1153 return ret; 1154} 1155 1156/* Return a command buffer to the free queue */ 1157void 1158mpt_free_request(struct mpt_softc *mpt, request_t *req) 1159{ 1160 request_t *nxt; 1161 struct mpt_evtf_record *record; 1162 uint32_t reply_baddr; 1163 1164 if (req == NULL || req != &mpt->request_pool[req->index]) { 1165 panic("mpt_free_request bad req ptr\n"); 1166 return; 1167 } 1168 if ((nxt = req->chain) != NULL) { 1169 req->chain = NULL; 1170 mpt_free_request(mpt, nxt); /* NB: recursion */ 1171 } 1172 KASSERT(req->state != REQ_STATE_FREE, ("freeing free request")); 1173 KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request")); 1174 KASSERT(MPT_OWNED(mpt), ("mpt_free_request: mpt not locked\n")); 1175 KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1176 ("mpt_free_request: req %p:%u func %x already on freelist", 1177 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1178 KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1179 ("mpt_free_request: req %p:%u func %x on pending list", 1180 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1181#ifdef INVARIANTS 1182 mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__); 1183#endif 1184 1185 req->ccb = NULL; 1186 if (LIST_EMPTY(&mpt->ack_frames)) { 1187 /* 1188 * Insert free ones at the tail 1189 */ 1190 req->serno = 0; 1191 req->state = REQ_STATE_FREE; 1192#ifdef INVARIANTS 1193 memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER)); 1194#endif 1195 TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links); 1196 if (mpt->getreqwaiter != 0) { 1197 mpt->getreqwaiter = 0; 1198 wakeup(&mpt->request_free_list); 1199 } 1200 return; 1201 } 1202 1203 /* 1204 * Process an ack frame deferred due to resource shortage. 1205 */ 1206 record = LIST_FIRST(&mpt->ack_frames); 1207 LIST_REMOVE(record, links); 1208 req->state = REQ_STATE_ALLOCATED; 1209 mpt_assign_serno(mpt, req); 1210 mpt_send_event_ack(mpt, req, &record->reply, record->context); 1211 reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply) 1212 + (mpt->reply_phys & 0xFFFFFFFF); 1213 mpt_free_reply(mpt, reply_baddr); 1214} 1215 1216/* Get a command buffer from the free queue */ 1217request_t * 1218mpt_get_request(struct mpt_softc *mpt, int sleep_ok) 1219{ 1220 request_t *req; 1221 1222retry: 1223 KASSERT(MPT_OWNED(mpt), ("mpt_get_request: mpt not locked\n")); 1224 req = TAILQ_FIRST(&mpt->request_free_list); 1225 if (req != NULL) { 1226 KASSERT(req == &mpt->request_pool[req->index], 1227 ("mpt_get_request: corrupted request free list\n")); 1228 KASSERT(req->state == REQ_STATE_FREE, 1229 ("req %p:%u not free on free list %x index %d function %x", 1230 req, req->serno, req->state, req->index, 1231 ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1232 TAILQ_REMOVE(&mpt->request_free_list, req, links); 1233 req->state = REQ_STATE_ALLOCATED; 1234 req->chain = NULL; 1235 mpt_assign_serno(mpt, req); 1236 } else if (sleep_ok != 0) { 1237 mpt->getreqwaiter = 1; 1238 mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0); 1239 goto retry; 1240 } 1241 return (req); 1242} 1243 1244/* Pass the command to the IOC */ 1245void 1246mpt_send_cmd(struct mpt_softc *mpt, request_t *req) 1247{ 1248 if (mpt->verbose > MPT_PRT_DEBUG2) { 1249 mpt_dump_request(mpt, req); 1250 } 1251 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1252 BUS_DMASYNC_PREWRITE); 1253 req->state |= REQ_STATE_QUEUED; 1254 KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1255 ("req %p:%u func %x on freelist list in mpt_send_cmd", 1256 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1257 KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1258 ("req %p:%u func %x already on pending list in mpt_send_cmd", 1259 req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1260 TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links); 1261 mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf); 1262} 1263 1264/* 1265 * Wait for a request to complete. 1266 * 1267 * Inputs: 1268 * mpt softc of controller executing request 1269 * req request to wait for 1270 * sleep_ok nonzero implies may sleep in this context 1271 * time_ms timeout in ms. 0 implies no timeout. 1272 * 1273 * Return Values: 1274 * 0 Request completed 1275 * non-0 Timeout fired before request completion. 1276 */ 1277int 1278mpt_wait_req(struct mpt_softc *mpt, request_t *req, 1279 mpt_req_state_t state, mpt_req_state_t mask, 1280 int sleep_ok, int time_ms) 1281{ 1282 int error; 1283 int timeout; 1284 u_int saved_cnt; 1285 1286 /* 1287 * timeout is in ms. 0 indicates infinite wait. 1288 * Convert to ticks or 500us units depending on 1289 * our sleep mode. 1290 */ 1291 if (sleep_ok != 0) { 1292 timeout = (time_ms * hz) / 1000; 1293 } else { 1294 timeout = time_ms * 2; 1295 } 1296 req->state |= REQ_STATE_NEED_WAKEUP; 1297 mask &= ~REQ_STATE_NEED_WAKEUP; 1298 saved_cnt = mpt->reset_cnt; 1299 while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) { 1300 if (sleep_ok != 0) { 1301 error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout); 1302 if (error == EWOULDBLOCK) { 1303 timeout = 0; 1304 break; 1305 } 1306 } else { 1307 if (time_ms != 0 && --timeout == 0) { 1308 break; 1309 } 1310 DELAY(500); 1311 mpt_intr(mpt); 1312 } 1313 } 1314 req->state &= ~REQ_STATE_NEED_WAKEUP; 1315 if (mpt->reset_cnt != saved_cnt) { 1316 return (EIO); 1317 } 1318 if (time_ms && timeout <= 0) { 1319 MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf; 1320 req->state |= REQ_STATE_TIMEDOUT; 1321 mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function); 1322 return (ETIMEDOUT); 1323 } 1324 return (0); 1325} 1326 1327/* 1328 * Send a command to the IOC via the handshake register. 1329 * 1330 * Only done at initialization time and for certain unusual 1331 * commands such as device/bus reset as specified by LSI. 1332 */ 1333int 1334mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd) 1335{ 1336 int i; 1337 uint32_t data, *data32; 1338 1339 /* Check condition of the IOC */ 1340 data = mpt_rd_db(mpt); 1341 if ((MPT_STATE(data) != MPT_DB_STATE_READY 1342 && MPT_STATE(data) != MPT_DB_STATE_RUNNING 1343 && MPT_STATE(data) != MPT_DB_STATE_FAULT) 1344 || MPT_DB_IS_IN_USE(data)) { 1345 mpt_prt(mpt, "handshake aborted - invalid doorbell state\n"); 1346 mpt_print_db(data); 1347 return (EBUSY); 1348 } 1349 1350 /* We move things in 32 bit chunks */ 1351 len = (len + 3) >> 2; 1352 data32 = cmd; 1353 1354 /* Clear any left over pending doorbell interupts */ 1355 if (MPT_DB_INTR(mpt_rd_intr(mpt))) 1356 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1357 1358 /* 1359 * Tell the handshake reg. we are going to send a command 1360 * and how long it is going to be. 1361 */ 1362 data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) | 1363 (len << MPI_DOORBELL_ADD_DWORDS_SHIFT); 1364 mpt_write(mpt, MPT_OFFSET_DOORBELL, data); 1365 1366 /* Wait for the chip to notice */ 1367 if (mpt_wait_db_int(mpt) != MPT_OK) { 1368 mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n"); 1369 return (ETIMEDOUT); 1370 } 1371 1372 /* Clear the interrupt */ 1373 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1374 1375 if (mpt_wait_db_ack(mpt) != MPT_OK) { 1376 mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n"); 1377 return (ETIMEDOUT); 1378 } 1379 1380 /* Send the command */ 1381 for (i = 0; i < len; i++) { 1382 mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++)); 1383 if (mpt_wait_db_ack(mpt) != MPT_OK) { 1384 mpt_prt(mpt, 1385 "mpt_send_handshake_cmd: timeout @ index %d\n", i); 1386 return (ETIMEDOUT); 1387 } 1388 } 1389 return MPT_OK; 1390} 1391 1392/* Get the response from the handshake register */ 1393int 1394mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply) 1395{ 1396 int left, reply_left; 1397 u_int16_t *data16; 1398 uint32_t data; 1399 MSG_DEFAULT_REPLY *hdr; 1400 1401 /* We move things out in 16 bit chunks */ 1402 reply_len >>= 1; 1403 data16 = (u_int16_t *)reply; 1404 1405 hdr = (MSG_DEFAULT_REPLY *)reply; 1406 1407 /* Get first word */ 1408 if (mpt_wait_db_int(mpt) != MPT_OK) { 1409 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n"); 1410 return ETIMEDOUT; 1411 } 1412 data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1413 *data16++ = le16toh(data & MPT_DB_DATA_MASK); 1414 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1415 1416 /* Get Second Word */ 1417 if (mpt_wait_db_int(mpt) != MPT_OK) { 1418 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n"); 1419 return ETIMEDOUT; 1420 } 1421 data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1422 *data16++ = le16toh(data & MPT_DB_DATA_MASK); 1423 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1424 1425 /* 1426 * With the second word, we can now look at the length. 1427 * Warn about a reply that's too short (except for IOC FACTS REPLY) 1428 */ 1429 if ((reply_len >> 1) != hdr->MsgLength && 1430 (hdr->Function != MPI_FUNCTION_IOC_FACTS)){ 1431#if __FreeBSD_version >= 500000 1432 mpt_prt(mpt, "reply length does not match message length: " 1433 "got %x; expected %zx for function %x\n", 1434 hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1435#else 1436 mpt_prt(mpt, "reply length does not match message length: " 1437 "got %x; expected %x for function %x\n", 1438 hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1439#endif 1440 } 1441 1442 /* Get rest of the reply; but don't overflow the provided buffer */ 1443 left = (hdr->MsgLength << 1) - 2; 1444 reply_left = reply_len - 2; 1445 while (left--) { 1446 u_int16_t datum; 1447 1448 if (mpt_wait_db_int(mpt) != MPT_OK) { 1449 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n"); 1450 return ETIMEDOUT; 1451 } 1452 data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1453 datum = le16toh(data & MPT_DB_DATA_MASK); 1454 1455 if (reply_left-- > 0) 1456 *data16++ = datum; 1457 1458 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1459 } 1460 1461 /* One more wait & clear at the end */ 1462 if (mpt_wait_db_int(mpt) != MPT_OK) { 1463 mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n"); 1464 return ETIMEDOUT; 1465 } 1466 mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1467 1468 if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1469 if (mpt->verbose >= MPT_PRT_TRACE) 1470 mpt_print_reply(hdr); 1471 return (MPT_FAIL | hdr->IOCStatus); 1472 } 1473 1474 return (0); 1475} 1476 1477static int 1478mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp) 1479{ 1480 MSG_IOC_FACTS f_req; 1481 int error; 1482 1483 memset(&f_req, 0, sizeof f_req); 1484 f_req.Function = MPI_FUNCTION_IOC_FACTS; 1485 f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1486 error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1487 if (error) 1488 return(error); 1489 error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1490 return (error); 1491} 1492 1493static int 1494mpt_get_portfacts(struct mpt_softc *mpt, MSG_PORT_FACTS_REPLY *freplp) 1495{ 1496 MSG_PORT_FACTS f_req; 1497 int error; 1498 1499 /* XXX: Only getting PORT FACTS for Port 0 */ 1500 memset(&f_req, 0, sizeof f_req); 1501 f_req.Function = MPI_FUNCTION_PORT_FACTS; 1502 f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1503 error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1504 if (error) 1505 return(error); 1506 error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1507 return (error); 1508} 1509 1510/* 1511 * Send the initialization request. This is where we specify how many 1512 * SCSI busses and how many devices per bus we wish to emulate. 1513 * This is also the command that specifies the max size of the reply 1514 * frames from the IOC that we will be allocating. 1515 */ 1516static int 1517mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who) 1518{ 1519 int error = 0; 1520 MSG_IOC_INIT init; 1521 MSG_IOC_INIT_REPLY reply; 1522 1523 memset(&init, 0, sizeof init); 1524 init.WhoInit = who; 1525 init.Function = MPI_FUNCTION_IOC_INIT; 1526 init.MaxDevices = mpt->mpt_max_devices; 1527 init.MaxBuses = 1; 1528 1529 init.MsgVersion = htole16(MPI_VERSION); 1530 init.HeaderVersion = htole16(MPI_HEADER_VERSION); 1531 init.ReplyFrameSize = htole16(MPT_REPLY_SIZE); 1532 init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1533 1534 if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) { 1535 return(error); 1536 } 1537 1538 error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply); 1539 return (error); 1540} 1541 1542 1543/* 1544 * Utiltity routine to read configuration headers and pages 1545 */ 1546int 1547mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, u_int Action, 1548 u_int PageVersion, u_int PageLength, u_int PageNumber, 1549 u_int PageType, uint32_t PageAddress, bus_addr_t addr, 1550 bus_size_t len, int sleep_ok, int timeout_ms) 1551{ 1552 MSG_CONFIG *cfgp; 1553 SGE_SIMPLE32 *se; 1554 1555 cfgp = req->req_vbuf; 1556 memset(cfgp, 0, sizeof *cfgp); 1557 cfgp->Action = Action; 1558 cfgp->Function = MPI_FUNCTION_CONFIG; 1559 cfgp->Header.PageVersion = PageVersion; 1560 cfgp->Header.PageLength = PageLength; 1561 cfgp->Header.PageNumber = PageNumber; 1562 cfgp->Header.PageType = PageType; 1563 cfgp->PageAddress = htole32(PageAddress); 1564 se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE; 1565 se->Address = htole32(addr); 1566 MPI_pSGE_SET_LENGTH(se, len); 1567 MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT | 1568 MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER | 1569 MPI_SGE_FLAGS_END_OF_LIST | 1570 ((Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT 1571 || Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM) 1572 ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST))); 1573 se->FlagsLength = htole32(se->FlagsLength); 1574 cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 1575 1576 mpt_check_doorbell(mpt); 1577 mpt_send_cmd(mpt, req); 1578 return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 1579 sleep_ok, timeout_ms)); 1580} 1581 1582 1583int 1584mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber, 1585 uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt, 1586 int sleep_ok, int timeout_ms) 1587{ 1588 request_t *req; 1589 MSG_CONFIG *cfgp; 1590 int error; 1591 1592 req = mpt_get_request(mpt, sleep_ok); 1593 if (req == NULL) { 1594 mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n"); 1595 return (ENOMEM); 1596 } 1597 1598 error = mpt_issue_cfg_req(mpt, req, MPI_CONFIG_ACTION_PAGE_HEADER, 1599 /*PageVersion*/0, /*PageLength*/0, PageNumber, 1600 PageType, PageAddress, /*addr*/0, /*len*/0, 1601 sleep_ok, timeout_ms); 1602 if (error != 0) { 1603 /* 1604 * Leave the request. Without resetting the chip, it's 1605 * still owned by it and we'll just get into trouble 1606 * freeing it now. Mark it as abandoned so that if it 1607 * shows up later it can be freed. 1608 */ 1609 mpt_prt(mpt, "read_cfg_header timed out\n"); 1610 return (ETIMEDOUT); 1611 } 1612 1613 switch (req->IOCStatus & MPI_IOCSTATUS_MASK) { 1614 case MPI_IOCSTATUS_SUCCESS: 1615 cfgp = req->req_vbuf; 1616 bcopy(&cfgp->Header, rslt, sizeof(*rslt)); 1617 error = 0; 1618 break; 1619 case MPI_IOCSTATUS_CONFIG_INVALID_PAGE: 1620 mpt_lprt(mpt, MPT_PRT_DEBUG, 1621 "Invalid Page Type %d Number %d Addr 0x%0x\n", 1622 PageType, PageNumber, PageAddress); 1623 error = EINVAL; 1624 break; 1625 default: 1626 mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n", 1627 req->IOCStatus); 1628 error = EIO; 1629 break; 1630 } 1631 mpt_free_request(mpt, req); 1632 return (error); 1633} 1634 1635int 1636mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1637 CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1638 int timeout_ms) 1639{ 1640 request_t *req; 1641 int error; 1642 1643 req = mpt_get_request(mpt, sleep_ok); 1644 if (req == NULL) { 1645 mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n"); 1646 return (-1); 1647 } 1648 1649 error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion, 1650 hdr->PageLength, hdr->PageNumber, 1651 hdr->PageType & MPI_CONFIG_PAGETYPE_MASK, 1652 PageAddress, req->req_pbuf + MPT_RQSL(mpt), 1653 len, sleep_ok, timeout_ms); 1654 if (error != 0) { 1655 mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action); 1656 return (-1); 1657 } 1658 1659 if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1660 mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n", 1661 req->IOCStatus); 1662 mpt_free_request(mpt, req); 1663 return (-1); 1664 } 1665 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1666 BUS_DMASYNC_POSTREAD); 1667 memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len); 1668 mpt_free_request(mpt, req); 1669 return (0); 1670} 1671 1672int 1673mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1674 CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1675 int timeout_ms) 1676{ 1677 request_t *req; 1678 u_int hdr_attr; 1679 int error; 1680 1681 hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK; 1682 if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE && 1683 hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) { 1684 mpt_prt(mpt, "page type 0x%x not changeable\n", 1685 hdr->PageType & MPI_CONFIG_PAGETYPE_MASK); 1686 return (-1); 1687 } 1688 1689#if 0 1690 /* 1691 * We shouldn't mask off other bits here. 1692 */ 1693 hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK; 1694#endif 1695 1696 req = mpt_get_request(mpt, sleep_ok); 1697 if (req == NULL) 1698 return (-1); 1699 1700 memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len); 1701 1702 /* 1703 * There isn't any point in restoring stripped out attributes 1704 * if you then mask them going down to issue the request. 1705 */ 1706 1707#if 0 1708 /* Restore stripped out attributes */ 1709 hdr->PageType |= hdr_attr; 1710 1711 error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion, 1712 hdr->PageLength, hdr->PageNumber, 1713 hdr->PageType & MPI_CONFIG_PAGETYPE_MASK, 1714 PageAddress, req->req_pbuf + MPT_RQSL(mpt), 1715 len, sleep_ok, timeout_ms); 1716#else 1717 error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion, 1718 hdr->PageLength, hdr->PageNumber, 1719 hdr->PageType, PageAddress, 1720 req->req_pbuf + MPT_RQSL(mpt), 1721 len, sleep_ok, timeout_ms); 1722#endif 1723 if (error != 0) { 1724 mpt_prt(mpt, "mpt_write_cfg_page timed out\n"); 1725 return (-1); 1726 } 1727 1728 if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1729 mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n", 1730 req->IOCStatus); 1731 mpt_free_request(mpt, req); 1732 return (-1); 1733 } 1734 mpt_free_request(mpt, req); 1735 return (0); 1736} 1737 1738/* 1739 * Read IOC configuration information 1740 */ 1741static int 1742mpt_read_config_info_ioc(struct mpt_softc *mpt) 1743{ 1744 CONFIG_PAGE_HEADER hdr; 1745 struct mpt_raid_volume *mpt_raid; 1746 int rv; 1747 int i; 1748 size_t len; 1749 1750 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 1751 2, 0, &hdr, FALSE, 5000); 1752 /* 1753 * If it's an invalid page, so what? Not a supported function.... 1754 */ 1755 if (rv == EINVAL) { 1756 return (0); 1757 } 1758 if (rv) { 1759 return (rv); 1760 } 1761 1762#if __FreeBSD_version >= 500000 1763 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 2 Header: ver %x, len %zx, " 1764 "num %x, type %x\n", hdr.PageVersion, 1765 hdr.PageLength * sizeof(uint32_t), 1766 hdr.PageNumber, hdr.PageType); 1767#else 1768 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 2 Header: ver %x, len %z, " 1769 "num %x, type %x\n", hdr.PageVersion, 1770 hdr.PageLength * sizeof(uint32_t), 1771 hdr.PageNumber, hdr.PageType); 1772#endif 1773 1774 len = hdr.PageLength * sizeof(uint32_t); 1775 mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1776 if (mpt->ioc_page2 == NULL) { 1777 mpt_prt(mpt, "unable to allocate memory for IOC page 2\n"); 1778 mpt_raid_free_mem(mpt); 1779 return (ENOMEM); 1780 } 1781 memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr)); 1782 rv = mpt_read_cur_cfg_page(mpt, 0, 1783 &mpt->ioc_page2->Header, len, FALSE, 5000); 1784 if (rv) { 1785 mpt_prt(mpt, "failed to read IOC Page 2\n"); 1786 mpt_raid_free_mem(mpt); 1787 return (EIO); 1788 } 1789 1790 if (mpt->ioc_page2->CapabilitiesFlags != 0) { 1791 uint32_t mask; 1792 1793 mpt_prt(mpt, "Capabilities: ("); 1794 for (mask = 1; mask != 0; mask <<= 1) { 1795 if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) { 1796 continue; 1797 } 1798 switch (mask) { 1799 case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT: 1800 mpt_prtc(mpt, " RAID-0"); 1801 break; 1802 case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT: 1803 mpt_prtc(mpt, " RAID-1E"); 1804 break; 1805 case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT: 1806 mpt_prtc(mpt, " RAID-1"); 1807 break; 1808 case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT: 1809 mpt_prtc(mpt, " SES"); 1810 break; 1811 case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT: 1812 mpt_prtc(mpt, " SAFTE"); 1813 break; 1814 case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT: 1815 mpt_prtc(mpt, " Multi-Channel-Arrays"); 1816 default: 1817 break; 1818 } 1819 } 1820 mpt_prtc(mpt, " )\n"); 1821 if ((mpt->ioc_page2->CapabilitiesFlags 1822 & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT 1823 | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT 1824 | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) { 1825 mpt_prt(mpt, "%d Active Volume%s(%d Max)\n", 1826 mpt->ioc_page2->NumActiveVolumes, 1827 mpt->ioc_page2->NumActiveVolumes != 1 1828 ? "s " : " ", 1829 mpt->ioc_page2->MaxVolumes); 1830 mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n", 1831 mpt->ioc_page2->NumActivePhysDisks, 1832 mpt->ioc_page2->NumActivePhysDisks != 1 1833 ? "s " : " ", 1834 mpt->ioc_page2->MaxPhysDisks); 1835 } 1836 } 1837 1838 len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume); 1839 mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1840 if (mpt->raid_volumes == NULL) { 1841 mpt_prt(mpt, "Could not allocate RAID volume data\n"); 1842 mpt_raid_free_mem(mpt); 1843 return (ENOMEM); 1844 } 1845 1846 /* 1847 * Copy critical data out of ioc_page2 so that we can 1848 * safely refresh the page without windows of unreliable 1849 * data. 1850 */ 1851 mpt->raid_max_volumes = mpt->ioc_page2->MaxVolumes; 1852 1853 len = sizeof(*mpt->raid_volumes->config_page) + 1854 (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1)); 1855 for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) { 1856 mpt_raid = &mpt->raid_volumes[i]; 1857 mpt_raid->config_page = 1858 malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1859 if (mpt_raid->config_page == NULL) { 1860 mpt_prt(mpt, "Could not allocate RAID page data\n"); 1861 mpt_raid_free_mem(mpt); 1862 return (ENOMEM); 1863 } 1864 } 1865 mpt->raid_page0_len = len; 1866 1867 len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk); 1868 mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1869 if (mpt->raid_disks == NULL) { 1870 mpt_prt(mpt, "Could not allocate RAID disk data\n"); 1871 mpt_raid_free_mem(mpt); 1872 return (ENOMEM); 1873 } 1874 mpt->raid_max_disks = mpt->ioc_page2->MaxPhysDisks; 1875 1876 /* 1877 * Load page 3. 1878 */ 1879 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 1880 3, 0, &hdr, FALSE, 5000); 1881 if (rv) { 1882 mpt_raid_free_mem(mpt); 1883 return (EIO); 1884 } 1885 1886 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n", 1887 hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType); 1888 1889 len = hdr.PageLength * sizeof(uint32_t); 1890 mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1891 if (mpt->ioc_page3 == NULL) { 1892 mpt_prt(mpt, "unable to allocate memory for IOC page 3\n"); 1893 mpt_raid_free_mem(mpt); 1894 return (ENOMEM); 1895 } 1896 memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr)); 1897 rv = mpt_read_cur_cfg_page(mpt, 0, 1898 &mpt->ioc_page3->Header, len, FALSE, 5000); 1899 if (rv) { 1900 mpt_raid_free_mem(mpt); 1901 return (EIO); 1902 } 1903 mpt_raid_wakeup(mpt); 1904 return (0); 1905} 1906 1907/* 1908 * Enable IOC port 1909 */ 1910static int 1911mpt_send_port_enable(struct mpt_softc *mpt, int port) 1912{ 1913 request_t *req; 1914 MSG_PORT_ENABLE *enable_req; 1915 int error; 1916 1917 req = mpt_get_request(mpt, /*sleep_ok*/FALSE); 1918 if (req == NULL) 1919 return (-1); 1920 1921 enable_req = req->req_vbuf; 1922 memset(enable_req, 0, MPT_RQSL(mpt)); 1923 1924 enable_req->Function = MPI_FUNCTION_PORT_ENABLE; 1925 enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 1926 enable_req->PortNumber = port; 1927 1928 mpt_check_doorbell(mpt); 1929 mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port); 1930 1931 mpt_send_cmd(mpt, req); 1932 error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 1933 FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000); 1934 if (error != 0) { 1935 mpt_prt(mpt, "port %d enable timed out\n", port); 1936 return (-1); 1937 } 1938 mpt_free_request(mpt, req); 1939 mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port); 1940 return (0); 1941} 1942 1943/* 1944 * Enable/Disable asynchronous event reporting. 1945 */ 1946static int 1947mpt_send_event_request(struct mpt_softc *mpt, int onoff) 1948{ 1949 request_t *req; 1950 MSG_EVENT_NOTIFY *enable_req; 1951 1952 req = mpt_get_request(mpt, FALSE); 1953 if (req == NULL) { 1954 return (ENOMEM); 1955 } 1956 enable_req = req->req_vbuf; 1957 memset(enable_req, 0, sizeof *enable_req); 1958 1959 enable_req->Function = MPI_FUNCTION_EVENT_NOTIFICATION; 1960 enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS); 1961 enable_req->Switch = onoff; 1962 1963 mpt_check_doorbell(mpt); 1964 mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n", 1965 onoff ? "en" : "dis"); 1966 /* 1967 * Send the command off, but don't wait for it. 1968 */ 1969 mpt_send_cmd(mpt, req); 1970 return (0); 1971} 1972 1973/* 1974 * Un-mask the interupts on the chip. 1975 */ 1976void 1977mpt_enable_ints(struct mpt_softc *mpt) 1978{ 1979 /* Unmask every thing except door bell int */ 1980 mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK); 1981} 1982 1983/* 1984 * Mask the interupts on the chip. 1985 */ 1986void 1987mpt_disable_ints(struct mpt_softc *mpt) 1988{ 1989 /* Mask all interrupts */ 1990 mpt_write(mpt, MPT_OFFSET_INTR_MASK, 1991 MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK); 1992} 1993 1994static void 1995mpt_sysctl_attach(struct mpt_softc *mpt) 1996{ 1997#if __FreeBSD_version >= 500000 1998 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev); 1999 struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev); 2000 2001 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 2002 "debug", CTLFLAG_RW, &mpt->verbose, 0, 2003 "Debugging/Verbose level"); 2004 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 2005 "role", CTLFLAG_RD, &mpt->role, 0, 2006 "HBA role"); 2007#endif 2008} 2009 2010int 2011mpt_attach(struct mpt_softc *mpt) 2012{ 2013 struct mpt_personality *pers; 2014 int i; 2015 int error; 2016 2017 TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links); 2018 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 2019 pers = mpt_personalities[i]; 2020 if (pers == NULL) { 2021 continue; 2022 } 2023 if (pers->probe(mpt) == 0) { 2024 error = pers->attach(mpt); 2025 if (error != 0) { 2026 mpt_detach(mpt); 2027 return (error); 2028 } 2029 mpt->mpt_pers_mask |= (0x1 << pers->id); 2030 pers->use_count++; 2031 } 2032 } 2033 2034 /* 2035 * Now that we've attached everything, do the enable function 2036 * for all of the personalities. This allows the personalities 2037 * to do setups that are appropriate for them prior to enabling 2038 * any ports. 2039 */ 2040 for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 2041 pers = mpt_personalities[i]; 2042 if (pers != NULL && MPT_PERS_ATTACHED(pers, mpt) != 0) { 2043 error = pers->enable(mpt); 2044 if (error != 0) { 2045 mpt_prt(mpt, "personality %s attached but would" 2046 " not enable (%d)\n", pers->name, error); 2047 mpt_detach(mpt); 2048 return (error); 2049 } 2050 } 2051 } 2052 return (0); 2053} 2054 2055int 2056mpt_shutdown(struct mpt_softc *mpt) 2057{ 2058 struct mpt_personality *pers; 2059 2060 MPT_PERS_FOREACH_REVERSE(mpt, pers) { 2061 pers->shutdown(mpt); 2062 } 2063 return (0); 2064} 2065 2066int 2067mpt_detach(struct mpt_softc *mpt) 2068{ 2069 struct mpt_personality *pers; 2070 2071 MPT_PERS_FOREACH_REVERSE(mpt, pers) { 2072 pers->detach(mpt); 2073 mpt->mpt_pers_mask &= ~(0x1 << pers->id); 2074 pers->use_count--; 2075 } 2076 TAILQ_REMOVE(&mpt_tailq, mpt, links); 2077 return (0); 2078} 2079 2080int 2081mpt_core_load(struct mpt_personality *pers) 2082{ 2083 int i; 2084 2085 /* 2086 * Setup core handlers and insert the default handler 2087 * into all "empty slots". 2088 */ 2089 for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) { 2090 mpt_reply_handlers[i] = mpt_default_reply_handler; 2091 } 2092 2093 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] = 2094 mpt_event_reply_handler; 2095 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] = 2096 mpt_config_reply_handler; 2097 mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] = 2098 mpt_handshake_reply_handler; 2099 return (0); 2100} 2101 2102/* 2103 * Initialize per-instance driver data and perform 2104 * initial controller configuration. 2105 */ 2106int 2107mpt_core_attach(struct mpt_softc *mpt) 2108{ 2109 int val; 2110 int error; 2111 2112 2113 LIST_INIT(&mpt->ack_frames); 2114 2115 /* Put all request buffers on the free list */ 2116 TAILQ_INIT(&mpt->request_pending_list); 2117 TAILQ_INIT(&mpt->request_free_list); 2118 TAILQ_INIT(&mpt->request_timeout_list); 2119 for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) { 2120 request_t *req = &mpt->request_pool[val]; 2121 req->state = REQ_STATE_ALLOCATED; 2122 mpt_free_request(mpt, req); 2123 } 2124 2125 for (val = 0; val < MPT_MAX_LUNS; val++) { 2126 STAILQ_INIT(&mpt->trt[val].atios); 2127 STAILQ_INIT(&mpt->trt[val].inots); 2128 } 2129 STAILQ_INIT(&mpt->trt_wildcard.atios); 2130 STAILQ_INIT(&mpt->trt_wildcard.inots); 2131 2132 mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE; 2133 2134 mpt_sysctl_attach(mpt); 2135 2136 mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n", 2137 mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL))); 2138 2139 error = mpt_configure_ioc(mpt); 2140 2141 return (error); 2142} 2143 2144int 2145mpt_core_enable(struct mpt_softc *mpt) 2146{ 2147 /* 2148 * We enter with the IOC enabled, but async events 2149 * not enabled, ports not enabled and interrupts 2150 * not enabled. 2151 */ 2152 2153 /* 2154 * Enable asynchronous event reporting- all personalities 2155 * have attached so that they should be able to now field 2156 * async events. 2157 */ 2158 mpt_send_event_request(mpt, 1); 2159 2160 /* 2161 * Catch any pending interrupts 2162 * 2163 * This seems to be crucial- otherwise 2164 * the portenable below times out. 2165 */ 2166 mpt_intr(mpt); 2167 2168 /* 2169 * Enable Interrupts 2170 */ 2171 mpt_enable_ints(mpt); 2172 2173 /* 2174 * Catch any pending interrupts 2175 * 2176 * This seems to be crucial- otherwise 2177 * the portenable below times out. 2178 */ 2179 mpt_intr(mpt); 2180 2181 /* 2182 * Enable the port. 2183 */ 2184 if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2185 mpt_prt(mpt, "failed to enable port 0\n"); 2186 return (ENXIO); 2187 } 2188 return (0); 2189} 2190 2191void 2192mpt_core_shutdown(struct mpt_softc *mpt) 2193{ 2194 mpt_disable_ints(mpt); 2195} 2196 2197void 2198mpt_core_detach(struct mpt_softc *mpt) 2199{ 2200 mpt_disable_ints(mpt); 2201} 2202 2203int 2204mpt_core_unload(struct mpt_personality *pers) 2205{ 2206 /* Unload is always successfull. */ 2207 return (0); 2208} 2209 2210#define FW_UPLOAD_REQ_SIZE \ 2211 (sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION) \ 2212 + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32)) 2213 2214static int 2215mpt_upload_fw(struct mpt_softc *mpt) 2216{ 2217 uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE]; 2218 MSG_FW_UPLOAD_REPLY fw_reply; 2219 MSG_FW_UPLOAD *fw_req; 2220 FW_UPLOAD_TCSGE *tsge; 2221 SGE_SIMPLE32 *sge; 2222 uint32_t flags; 2223 int error; 2224 2225 memset(&fw_req_buf, 0, sizeof(fw_req_buf)); 2226 fw_req = (MSG_FW_UPLOAD *)fw_req_buf; 2227 fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM; 2228 fw_req->Function = MPI_FUNCTION_FW_UPLOAD; 2229 fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 2230 tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL; 2231 tsge->DetailsLength = 12; 2232 tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT; 2233 tsge->ImageSize = htole32(mpt->fw_image_size); 2234 sge = (SGE_SIMPLE32 *)(tsge + 1); 2235 flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER 2236 | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT 2237 | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST); 2238 flags <<= MPI_SGE_FLAGS_SHIFT; 2239 sge->FlagsLength = htole32(flags | mpt->fw_image_size); 2240 sge->Address = htole32(mpt->fw_phys); 2241 error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf); 2242 if (error) 2243 return(error); 2244 error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply); 2245 return (error); 2246} 2247 2248static void 2249mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr, 2250 uint32_t *data, bus_size_t len) 2251{ 2252 uint32_t *data_end; 2253 2254 data_end = data + (roundup2(len, sizeof(uint32_t)) / 4); 2255 if (mpt->is_sas) { 2256 pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2257 } 2258 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr); 2259 while (data != data_end) { 2260 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data); 2261 data++; 2262 } 2263 if (mpt->is_sas) { 2264 pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2265 } 2266} 2267 2268static int 2269mpt_download_fw(struct mpt_softc *mpt) 2270{ 2271 MpiFwHeader_t *fw_hdr; 2272 int error; 2273 uint32_t ext_offset; 2274 uint32_t data; 2275 2276 mpt_prt(mpt, "Downloading Firmware - Image Size %d\n", 2277 mpt->fw_image_size); 2278 2279 error = mpt_enable_diag_mode(mpt); 2280 if (error != 0) { 2281 mpt_prt(mpt, "Could not enter diagnostic mode!\n"); 2282 return (EIO); 2283 } 2284 2285 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, 2286 MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM); 2287 2288 fw_hdr = (MpiFwHeader_t *)mpt->fw_image; 2289 mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr, 2290 fw_hdr->ImageSize); 2291 2292 ext_offset = fw_hdr->NextImageHeaderOffset; 2293 while (ext_offset != 0) { 2294 MpiExtImageHeader_t *ext; 2295 2296 ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset); 2297 ext_offset = ext->NextImageHeaderOffset; 2298 2299 mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext, 2300 ext->ImageSize); 2301 } 2302 2303 if (mpt->is_sas) { 2304 pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2305 } 2306 /* Setup the address to jump to on reset. */ 2307 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr); 2308 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue); 2309 2310 /* 2311 * The controller sets the "flash bad" status after attempting 2312 * to auto-boot from flash. Clear the status so that the controller 2313 * will continue the boot process with our newly installed firmware. 2314 */ 2315 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2316 data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL; 2317 mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2318 mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data); 2319 2320 if (mpt->is_sas) { 2321 pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2322 } 2323 2324 /* 2325 * Re-enable the processor and clear the boot halt flag. 2326 */ 2327 data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 2328 data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM); 2329 mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data); 2330 2331 mpt_disable_diag_mode(mpt); 2332 return (0); 2333} 2334 2335/* 2336 * Allocate/Initialize data structures for the controller. Called 2337 * once at instance startup. 2338 */ 2339static int 2340mpt_configure_ioc(struct mpt_softc *mpt) 2341{ 2342 MSG_PORT_FACTS_REPLY pfp; 2343 MSG_IOC_FACTS_REPLY facts; 2344 int try; 2345 int needreset; 2346 uint32_t max_chain_depth; 2347 2348 needreset = 0; 2349 for (try = 0; try < MPT_MAX_TRYS; try++) { 2350 2351 /* 2352 * No need to reset if the IOC is already in the READY state. 2353 * 2354 * Force reset if initialization failed previously. 2355 * Note that a hard_reset of the second channel of a '929 2356 * will stop operation of the first channel. Hopefully, if the 2357 * first channel is ok, the second will not require a hard 2358 * reset. 2359 */ 2360 if (needreset || MPT_STATE(mpt_rd_db(mpt)) != 2361 MPT_DB_STATE_READY) { 2362 if (mpt_reset(mpt, FALSE) != MPT_OK) { 2363 continue; 2364 } 2365 } 2366 needreset = 0; 2367 2368 if (mpt_get_iocfacts(mpt, &facts) != MPT_OK) { 2369 mpt_prt(mpt, "mpt_get_iocfacts failed\n"); 2370 needreset = 1; 2371 continue; 2372 } 2373 2374 mpt->mpt_global_credits = le16toh(facts.GlobalCredits); 2375 mpt->request_frame_size = le16toh(facts.RequestFrameSize); 2376 mpt->ioc_facts_flags = facts.Flags; 2377 mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n", 2378 le16toh(facts.MsgVersion) >> 8, 2379 le16toh(facts.MsgVersion) & 0xFF, 2380 le16toh(facts.HeaderVersion) >> 8, 2381 le16toh(facts.HeaderVersion) & 0xFF); 2382 2383 /* 2384 * Now that we know request frame size, we can calculate 2385 * the actual (reasonable) segment limit for read/write I/O. 2386 * 2387 * This limit is constrained by: 2388 * 2389 * + The size of each area we allocate per command (and how 2390 * many chain segments we can fit into it). 2391 * + The total number of areas we've set up. 2392 * + The actual chain depth the card will allow. 2393 * 2394 * The first area's segment count is limited by the I/O request 2395 * at the head of it. We cannot allocate realistically more 2396 * than MPT_MAX_REQUESTS areas. Therefore, to account for both 2397 * conditions, we'll just start out with MPT_MAX_REQUESTS-2. 2398 * 2399 */ 2400 max_chain_depth = facts.MaxChainDepth; 2401 2402 /* total number of request areas we (can) allocate */ 2403 mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2; 2404 2405 /* converted to the number of chain areas possible */ 2406 mpt->max_seg_cnt *= MPT_NRFM(mpt); 2407 2408 /* limited by the number of chain areas the card will support */ 2409 if (mpt->max_seg_cnt > max_chain_depth) { 2410 mpt_lprt(mpt, MPT_PRT_DEBUG, 2411 "chain depth limited to %u (from %u)\n", 2412 max_chain_depth, mpt->max_seg_cnt); 2413 mpt->max_seg_cnt = max_chain_depth; 2414 } 2415 2416 /* converted to the number of simple sges in chain segments. */ 2417 mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1); 2418 2419 mpt_lprt(mpt, MPT_PRT_DEBUG, 2420 "Maximum Segment Count: %u\n", mpt->max_seg_cnt); 2421 mpt_lprt(mpt, MPT_PRT_DEBUG, 2422 "MsgLength=%u IOCNumber = %d\n", 2423 facts.MsgLength, facts.IOCNumber); 2424 mpt_lprt(mpt, MPT_PRT_DEBUG, 2425 "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes " 2426 "Request Frame Size %u bytes Max Chain Depth %u\n", 2427 mpt->mpt_global_credits, facts.BlockSize, 2428 mpt->request_frame_size << 2, max_chain_depth); 2429 mpt_lprt(mpt, MPT_PRT_DEBUG, 2430 "IOCFACTS: Num Ports %d, FWImageSize %d, " 2431 "Flags=%#x\n", facts.NumberOfPorts, 2432 le32toh(facts.FWImageSize), facts.Flags); 2433 2434 2435 if ((facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) != 0) { 2436 struct mpt_map_info mi; 2437 int error; 2438 2439 /* 2440 * In some configurations, the IOC's firmware is 2441 * stored in a shared piece of system NVRAM that 2442 * is only accessable via the BIOS. In this 2443 * case, the firmware keeps a copy of firmware in 2444 * RAM until the OS driver retrieves it. Once 2445 * retrieved, we are responsible for re-downloading 2446 * the firmware after any hard-reset. 2447 */ 2448 mpt->fw_image_size = le32toh(facts.FWImageSize); 2449 error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 2450 /*alignment*/1, /*boundary*/0, 2451 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 2452 /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, 2453 /*filterarg*/NULL, mpt->fw_image_size, 2454 /*nsegments*/1, /*maxsegsz*/mpt->fw_image_size, 2455 /*flags*/0, &mpt->fw_dmat); 2456 if (error != 0) { 2457 mpt_prt(mpt, "cannot create fw dma tag\n"); 2458 return (ENOMEM); 2459 } 2460 error = bus_dmamem_alloc(mpt->fw_dmat, 2461 (void **)&mpt->fw_image, BUS_DMA_NOWAIT, 2462 &mpt->fw_dmap); 2463 if (error != 0) { 2464 mpt_prt(mpt, "cannot allocate fw mem.\n"); 2465 bus_dma_tag_destroy(mpt->fw_dmat); 2466 return (ENOMEM); 2467 } 2468 mi.mpt = mpt; 2469 mi.error = 0; 2470 bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap, 2471 mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, 2472 &mi, 0); 2473 mpt->fw_phys = mi.phys; 2474 2475 error = mpt_upload_fw(mpt); 2476 if (error != 0) { 2477 mpt_prt(mpt, "fw upload failed.\n"); 2478 bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap); 2479 bus_dmamem_free(mpt->fw_dmat, mpt->fw_image, 2480 mpt->fw_dmap); 2481 bus_dma_tag_destroy(mpt->fw_dmat); 2482 mpt->fw_image = NULL; 2483 return (EIO); 2484 } 2485 } 2486 2487 if (mpt_get_portfacts(mpt, &pfp) != MPT_OK) { 2488 mpt_prt(mpt, "mpt_get_portfacts failed\n"); 2489 needreset = 1; 2490 continue; 2491 } 2492 2493 mpt_lprt(mpt, MPT_PRT_DEBUG, 2494 "PORTFACTS: Type %x PFlags %x IID %d MaxDev %d\n", 2495 pfp.PortType, pfp.ProtocolFlags, pfp.PortSCSIID, 2496 pfp.MaxDevices); 2497 2498 mpt->mpt_port_type = pfp.PortType; 2499 mpt->mpt_proto_flags = le16toh(pfp.ProtocolFlags); 2500 if (pfp.PortType != MPI_PORTFACTS_PORTTYPE_SCSI && 2501 pfp.PortType != MPI_PORTFACTS_PORTTYPE_SAS && 2502 pfp.PortType != MPI_PORTFACTS_PORTTYPE_FC) { 2503 mpt_prt(mpt, "Unsupported Port Type (%x)\n", 2504 pfp.PortType); 2505 return (ENXIO); 2506 } 2507 mpt->mpt_max_tgtcmds = le16toh(pfp.MaxPostedCmdBuffers); 2508 2509 if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_FC) { 2510 mpt->is_fc = 1; 2511 mpt->is_sas = 0; 2512 mpt->is_spi = 0; 2513 } else if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_SAS) { 2514 mpt->is_fc = 0; 2515 mpt->is_sas = 1; 2516 mpt->is_spi = 0; 2517 } else { 2518 mpt->is_fc = 0; 2519 mpt->is_sas = 0; 2520 mpt->is_spi = 1; 2521 } 2522 mpt->mpt_ini_id = pfp.PortSCSIID; 2523 mpt->mpt_max_devices = pfp.MaxDevices; 2524 2525 /* 2526 * Set our role with what this port supports. 2527 * 2528 * Note this might be changed later in different modules 2529 * if this is different from what is wanted. 2530 */ 2531 mpt->role = MPT_ROLE_NONE; 2532 if (mpt->mpt_proto_flags & MPI_PORTFACTS_PROTOCOL_INITIATOR) { 2533 mpt->role |= MPT_ROLE_INITIATOR; 2534 } 2535 if (mpt->mpt_proto_flags & MPI_PORTFACTS_PROTOCOL_TARGET) { 2536 mpt->role |= MPT_ROLE_TARGET; 2537 } 2538 if (mpt_enable_ioc(mpt, 0) != MPT_OK) { 2539 mpt_prt(mpt, "unable to initialize IOC\n"); 2540 return (ENXIO); 2541 } 2542 2543 /* 2544 * Read IOC configuration information. 2545 * 2546 * We need this to determine whether or not we have certain 2547 * settings for Integrated Mirroring (e.g.). 2548 */ 2549 mpt_read_config_info_ioc(mpt); 2550 2551 /* Everything worked */ 2552 break; 2553 } 2554 2555 if (try >= MPT_MAX_TRYS) { 2556 mpt_prt(mpt, "failed to initialize IOC"); 2557 return (EIO); 2558 } 2559 2560 return (0); 2561} 2562 2563static int 2564mpt_enable_ioc(struct mpt_softc *mpt, int portenable) 2565{ 2566 uint32_t pptr; 2567 int val; 2568 2569 if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) { 2570 mpt_prt(mpt, "mpt_send_ioc_init failed\n"); 2571 return (EIO); 2572 } 2573 2574 mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n"); 2575 2576 if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) { 2577 mpt_prt(mpt, "IOC failed to go to run state\n"); 2578 return (ENXIO); 2579 } 2580 mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n"); 2581 2582 /* 2583 * Give it reply buffers 2584 * 2585 * Do *not* exceed global credits. 2586 */ 2587 for (val = 0, pptr = mpt->reply_phys; 2588 (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE); 2589 pptr += MPT_REPLY_SIZE) { 2590 mpt_free_reply(mpt, pptr); 2591 if (++val == mpt->mpt_global_credits - 1) 2592 break; 2593 } 2594 2595 2596 /* 2597 * Enable the port if asked. This is only done if we're resetting 2598 * the IOC after initial startup. 2599 */ 2600 if (portenable) { 2601 /* 2602 * Enable asynchronous event reporting 2603 */ 2604 mpt_send_event_request(mpt, 1); 2605 2606 if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2607 mpt_prt(mpt, "failed to enable port 0\n"); 2608 return (ENXIO); 2609 } 2610 } 2611 return (MPT_OK); 2612} 2613