mpt.c revision 170252
1139749Simp/*- 2156000Smjacob * Generic routines for LSI Fusion adapters. 3101704Smjacob * FreeBSD Version. 4101704Smjacob * 5101704Smjacob * Copyright (c) 2000, 2001 by Greg Ansley 6101704Smjacob * 7101704Smjacob * Redistribution and use in source and binary forms, with or without 8101704Smjacob * modification, are permitted provided that the following conditions 9101704Smjacob * are met: 10101704Smjacob * 1. Redistributions of source code must retain the above copyright 11101704Smjacob * notice immediately at the beginning of the file, without modification, 12101704Smjacob * this list of conditions, and the following disclaimer. 13101704Smjacob * 2. The name of the author may not be used to endorse or promote products 14101704Smjacob * derived from this software without specific prior written permission. 15101704Smjacob * 16101704Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17101704Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18101704Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19101704Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20101704Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21101704Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22101704Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23101704Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24101704Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25101704Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26101704Smjacob * SUCH DAMAGE. 27156000Smjacob */ 28156000Smjacob/*- 29156000Smjacob * Copyright (c) 2002, 2006 by Matthew Jacob 30156000Smjacob * All rights reserved. 31156104Smjacob * 32156000Smjacob * Redistribution and use in source and binary forms, with or without 33156000Smjacob * modification, are permitted provided that the following conditions are 34156000Smjacob * met: 35156000Smjacob * 1. Redistributions of source code must retain the above copyright 36156000Smjacob * notice, this list of conditions and the following disclaimer. 37156000Smjacob * 2. Redistributions in binary form must reproduce at minimum a disclaimer 38156000Smjacob * substantially similar to the "NO WARRANTY" disclaimer below 39156000Smjacob * ("Disclaimer") and any redistribution must be conditioned upon including 40156000Smjacob * a substantially similar Disclaimer requirement for further binary 41156000Smjacob * redistribution. 42156000Smjacob * 3. Neither the names of the above listed copyright holders nor the names 43156000Smjacob * of any contributors may be used to endorse or promote products derived 44156000Smjacob * from this software without specific prior written permission. 45156104Smjacob * 46156000Smjacob * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 47156000Smjacob * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48156000Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49156000Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 50156000Smjacob * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51156000Smjacob * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52156000Smjacob * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53156000Smjacob * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54156000Smjacob * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55156000Smjacob * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT 56156000Smjacob * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57147883Sscottl * 58156000Smjacob * Support from Chris Ellsworth in order to make SAS adapters work 59156000Smjacob * is gratefully acknowledged. 60159052Smjacob * 61159052Smjacob * 62159052Smjacob * Support from LSI-Logic has also gone a great deal toward making this a 63159052Smjacob * workable subsystem and is gratefully acknowledged. 64101704Smjacob */ 65156000Smjacob/*- 66147883Sscottl * Copyright (c) 2004, Avid Technology, Inc. and its contributors. 67147883Sscottl * Copyright (c) 2005, WHEEL Sp. z o.o. 68147883Sscottl * Copyright (c) 2004, 2005 Justin T. Gibbs 69147883Sscottl * All rights reserved. 70156104Smjacob * 71147883Sscottl * Redistribution and use in source and binary forms, with or without 72147883Sscottl * modification, are permitted provided that the following conditions are 73147883Sscottl * met: 74147883Sscottl * 1. Redistributions of source code must retain the above copyright 75147883Sscottl * notice, this list of conditions and the following disclaimer. 76147883Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer 77147883Sscottl * substantially similar to the "NO WARRANTY" disclaimer below 78147883Sscottl * ("Disclaimer") and any redistribution must be conditioned upon including 79147883Sscottl * a substantially similar Disclaimer requirement for further binary 80147883Sscottl * redistribution. 81148679Sgibbs * 3. Neither the names of the above listed copyright holders nor the names 82148679Sgibbs * of any contributors may be used to endorse or promote products derived 83148679Sgibbs * from this software without specific prior written permission. 84156104Smjacob * 85147883Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 86147883Sscottl * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87147883Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88147883Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 89147883Sscottl * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 90147883Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 91147883Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 92147883Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 93147883Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 94147883Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT 95147883Sscottl * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 96101704Smjacob */ 97101704Smjacob 98134123Sobrien#include <sys/cdefs.h> 99134123Sobrien__FBSDID("$FreeBSD: head/sys/dev/mpt/mpt.c 170252 2007-06-03 23:13:05Z scottl $"); 100134123Sobrien 101147883Sscottl#include <dev/mpt/mpt.h> 102147883Sscottl#include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */ 103147883Sscottl#include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */ 104102199Smjacob 105147883Sscottl#include <dev/mpt/mpilib/mpi.h> 106147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h> 107157117Smjacob#include <dev/mpt/mpilib/mpi_fc.h> 108157117Smjacob#include <dev/mpt/mpilib/mpi_targ.h> 109147883Sscottl 110147883Sscottl#include <sys/sysctl.h> 111147883Sscottl 112101704Smjacob#define MPT_MAX_TRYS 3 113101704Smjacob#define MPT_MAX_WAIT 300000 114101704Smjacob 115101704Smjacobstatic int maxwait_ack = 0; 116101704Smjacobstatic int maxwait_int = 0; 117101704Smjacobstatic int maxwait_state = 0; 118101704Smjacob 119162133Smjacobstatic TAILQ_HEAD(, mpt_softc) mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq); 120147883Sscottlmpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS]; 121101704Smjacob 122147883Sscottlstatic mpt_reply_handler_t mpt_default_reply_handler; 123147883Sscottlstatic mpt_reply_handler_t mpt_config_reply_handler; 124147883Sscottlstatic mpt_reply_handler_t mpt_handshake_reply_handler; 125147883Sscottlstatic mpt_reply_handler_t mpt_event_reply_handler; 126147883Sscottlstatic void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 127147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context); 128155521Smjacobstatic int mpt_send_event_request(struct mpt_softc *mpt, int onoff); 129147883Sscottlstatic int mpt_soft_reset(struct mpt_softc *mpt); 130147883Sscottlstatic void mpt_hard_reset(struct mpt_softc *mpt); 131164990Smjacobstatic int mpt_configure_ioc(struct mpt_softc *mpt, int, int); 132157117Smjacobstatic int mpt_enable_ioc(struct mpt_softc *mpt, int); 133147883Sscottl 134147883Sscottl/************************* Personality Module Support *************************/ 135147883Sscottl/* 136147883Sscottl * We include one extra entry that is guaranteed to be NULL 137147883Sscottl * to simplify our itterator. 138147883Sscottl */ 139147883Sscottlstatic struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1]; 140147883Sscottlstatic __inline struct mpt_personality* 141147883Sscottl mpt_pers_find(struct mpt_softc *, u_int); 142147883Sscottlstatic __inline struct mpt_personality* 143147883Sscottl mpt_pers_find_reverse(struct mpt_softc *, u_int); 144147883Sscottl 145147883Sscottlstatic __inline struct mpt_personality * 146147883Sscottlmpt_pers_find(struct mpt_softc *mpt, u_int start_at) 147101704Smjacob{ 148147883Sscottl KASSERT(start_at <= MPT_MAX_PERSONALITIES, 149147883Sscottl ("mpt_pers_find: starting position out of range\n")); 150147883Sscottl 151147883Sscottl while (start_at < MPT_MAX_PERSONALITIES 152147883Sscottl && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 153147883Sscottl start_at++; 154147883Sscottl } 155147883Sscottl return (mpt_personalities[start_at]); 156147883Sscottl} 157147883Sscottl 158147883Sscottl/* 159157117Smjacob * Used infrequently, so no need to optimize like a forward 160147883Sscottl * traversal where we use the MAX+1 is guaranteed to be NULL 161147883Sscottl * trick. 162147883Sscottl */ 163147883Sscottlstatic __inline struct mpt_personality * 164147883Sscottlmpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at) 165147883Sscottl{ 166147883Sscottl while (start_at < MPT_MAX_PERSONALITIES 167147883Sscottl && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 168147883Sscottl start_at--; 169147883Sscottl } 170147883Sscottl if (start_at < MPT_MAX_PERSONALITIES) 171147883Sscottl return (mpt_personalities[start_at]); 172147883Sscottl return (NULL); 173147883Sscottl} 174147883Sscottl 175147883Sscottl#define MPT_PERS_FOREACH(mpt, pers) \ 176147883Sscottl for (pers = mpt_pers_find(mpt, /*start_at*/0); \ 177147883Sscottl pers != NULL; \ 178147883Sscottl pers = mpt_pers_find(mpt, /*start_at*/pers->id+1)) 179147883Sscottl 180147883Sscottl#define MPT_PERS_FOREACH_REVERSE(mpt, pers) \ 181147883Sscottl for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\ 182147883Sscottl pers != NULL; \ 183147883Sscottl pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1)) 184147883Sscottl 185147883Sscottlstatic mpt_load_handler_t mpt_stdload; 186147883Sscottlstatic mpt_probe_handler_t mpt_stdprobe; 187147883Sscottlstatic mpt_attach_handler_t mpt_stdattach; 188157117Smjacobstatic mpt_enable_handler_t mpt_stdenable; 189162133Smjacobstatic mpt_ready_handler_t mpt_stdready; 190147883Sscottlstatic mpt_event_handler_t mpt_stdevent; 191147883Sscottlstatic mpt_reset_handler_t mpt_stdreset; 192147883Sscottlstatic mpt_shutdown_handler_t mpt_stdshutdown; 193147883Sscottlstatic mpt_detach_handler_t mpt_stddetach; 194147883Sscottlstatic mpt_unload_handler_t mpt_stdunload; 195147883Sscottlstatic struct mpt_personality mpt_default_personality = 196147883Sscottl{ 197147883Sscottl .load = mpt_stdload, 198147883Sscottl .probe = mpt_stdprobe, 199147883Sscottl .attach = mpt_stdattach, 200157117Smjacob .enable = mpt_stdenable, 201162133Smjacob .ready = mpt_stdready, 202147883Sscottl .event = mpt_stdevent, 203147883Sscottl .reset = mpt_stdreset, 204147883Sscottl .shutdown = mpt_stdshutdown, 205147883Sscottl .detach = mpt_stddetach, 206147883Sscottl .unload = mpt_stdunload 207147883Sscottl}; 208147883Sscottl 209147883Sscottlstatic mpt_load_handler_t mpt_core_load; 210147883Sscottlstatic mpt_attach_handler_t mpt_core_attach; 211157117Smjacobstatic mpt_enable_handler_t mpt_core_enable; 212147883Sscottlstatic mpt_reset_handler_t mpt_core_ioc_reset; 213147883Sscottlstatic mpt_event_handler_t mpt_core_event; 214147883Sscottlstatic mpt_shutdown_handler_t mpt_core_shutdown; 215147883Sscottlstatic mpt_shutdown_handler_t mpt_core_detach; 216147883Sscottlstatic mpt_unload_handler_t mpt_core_unload; 217147883Sscottlstatic struct mpt_personality mpt_core_personality = 218147883Sscottl{ 219147883Sscottl .name = "mpt_core", 220147883Sscottl .load = mpt_core_load, 221147883Sscottl .attach = mpt_core_attach, 222157117Smjacob .enable = mpt_core_enable, 223147883Sscottl .event = mpt_core_event, 224147883Sscottl .reset = mpt_core_ioc_reset, 225147883Sscottl .shutdown = mpt_core_shutdown, 226147883Sscottl .detach = mpt_core_detach, 227147883Sscottl .unload = mpt_core_unload, 228147883Sscottl}; 229147883Sscottl 230147883Sscottl/* 231147883Sscottl * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need 232147883Sscottl * ordering information. We want the core to always register FIRST. 233147883Sscottl * other modules are set to SI_ORDER_SECOND. 234147883Sscottl */ 235147883Sscottlstatic moduledata_t mpt_core_mod = { 236147883Sscottl "mpt_core", mpt_modevent, &mpt_core_personality 237147883Sscottl}; 238147883SscottlDECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 239147883SscottlMODULE_VERSION(mpt_core, 1); 240147883Sscottl 241157117Smjacob#define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id)) 242147883Sscottl 243147883Sscottlint 244147883Sscottlmpt_modevent(module_t mod, int type, void *data) 245147883Sscottl{ 246147883Sscottl struct mpt_personality *pers; 247147883Sscottl int error; 248147883Sscottl 249147883Sscottl pers = (struct mpt_personality *)data; 250147883Sscottl 251147883Sscottl error = 0; 252147883Sscottl switch (type) { 253147883Sscottl case MOD_LOAD: 254147883Sscottl { 255147883Sscottl mpt_load_handler_t **def_handler; 256147883Sscottl mpt_load_handler_t **pers_handler; 257147883Sscottl int i; 258147883Sscottl 259147883Sscottl for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 260147883Sscottl if (mpt_personalities[i] == NULL) 261147883Sscottl break; 262147883Sscottl } 263147883Sscottl if (i >= MPT_MAX_PERSONALITIES) { 264147883Sscottl error = ENOMEM; 265147883Sscottl break; 266147883Sscottl } 267147883Sscottl pers->id = i; 268147883Sscottl mpt_personalities[i] = pers; 269147883Sscottl 270147883Sscottl /* Install standard/noop handlers for any NULL entries. */ 271147883Sscottl def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality); 272147883Sscottl pers_handler = MPT_PERS_FIRST_HANDLER(pers); 273147883Sscottl while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) { 274147883Sscottl if (*pers_handler == NULL) 275147883Sscottl *pers_handler = *def_handler; 276147883Sscottl pers_handler++; 277147883Sscottl def_handler++; 278147883Sscottl } 279147883Sscottl 280147883Sscottl error = (pers->load(pers)); 281147883Sscottl if (error != 0) 282147883Sscottl mpt_personalities[i] = NULL; 283147883Sscottl break; 284147883Sscottl } 285147883Sscottl case MOD_SHUTDOWN: 286147883Sscottl break; 287157117Smjacob#if __FreeBSD_version >= 500000 288147883Sscottl case MOD_QUIESCE: 289147883Sscottl break; 290157117Smjacob#endif 291147883Sscottl case MOD_UNLOAD: 292147883Sscottl error = pers->unload(pers); 293147883Sscottl mpt_personalities[pers->id] = NULL; 294147883Sscottl break; 295147883Sscottl default: 296147883Sscottl error = EINVAL; 297147883Sscottl break; 298147883Sscottl } 299147883Sscottl return (error); 300147883Sscottl} 301147883Sscottl 302147883Sscottlint 303147883Sscottlmpt_stdload(struct mpt_personality *pers) 304147883Sscottl{ 305147883Sscottl /* Load is always successfull. */ 306147883Sscottl return (0); 307147883Sscottl} 308147883Sscottl 309147883Sscottlint 310147883Sscottlmpt_stdprobe(struct mpt_softc *mpt) 311147883Sscottl{ 312147883Sscottl /* Probe is always successfull. */ 313147883Sscottl return (0); 314147883Sscottl} 315147883Sscottl 316147883Sscottlint 317147883Sscottlmpt_stdattach(struct mpt_softc *mpt) 318147883Sscottl{ 319147883Sscottl /* Attach is always successfull. */ 320147883Sscottl return (0); 321147883Sscottl} 322147883Sscottl 323147883Sscottlint 324157117Smjacobmpt_stdenable(struct mpt_softc *mpt) 325157117Smjacob{ 326157117Smjacob /* Enable is always successfull. */ 327157117Smjacob return (0); 328157117Smjacob} 329157117Smjacob 330162133Smjacobvoid 331162133Smjacobmpt_stdready(struct mpt_softc *mpt) 332162133Smjacob{ 333162133Smjacob} 334162133Smjacob 335162133Smjacob 336157117Smjacobint 337155521Smjacobmpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg) 338147883Sscottl{ 339155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF); 340147883Sscottl /* Event was not for us. */ 341147883Sscottl return (0); 342147883Sscottl} 343147883Sscottl 344147883Sscottlvoid 345147883Sscottlmpt_stdreset(struct mpt_softc *mpt, int type) 346147883Sscottl{ 347147883Sscottl} 348147883Sscottl 349147883Sscottlvoid 350147883Sscottlmpt_stdshutdown(struct mpt_softc *mpt) 351147883Sscottl{ 352147883Sscottl} 353147883Sscottl 354147883Sscottlvoid 355147883Sscottlmpt_stddetach(struct mpt_softc *mpt) 356147883Sscottl{ 357147883Sscottl} 358147883Sscottl 359147883Sscottlint 360147883Sscottlmpt_stdunload(struct mpt_personality *pers) 361147883Sscottl{ 362147883Sscottl /* Unload is always successfull. */ 363147883Sscottl return (0); 364147883Sscottl} 365147883Sscottl 366162133Smjacob/* 367162133Smjacob * Post driver attachment, we may want to perform some global actions. 368162133Smjacob * Here is the hook to do so. 369162133Smjacob */ 370162133Smjacob 371162133Smjacobstatic void 372162133Smjacobmpt_postattach(void *unused) 373162133Smjacob{ 374162133Smjacob struct mpt_softc *mpt; 375162133Smjacob struct mpt_personality *pers; 376162133Smjacob 377162133Smjacob TAILQ_FOREACH(mpt, &mpt_tailq, links) { 378162133Smjacob MPT_PERS_FOREACH(mpt, pers) 379162133Smjacob pers->ready(mpt); 380162133Smjacob } 381162133Smjacob} 382162133SmjacobSYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL); 383162133Smjacob 384162133Smjacob 385147883Sscottl/******************************* Bus DMA Support ******************************/ 386147883Sscottlvoid 387147883Sscottlmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 388147883Sscottl{ 389147883Sscottl struct mpt_map_info *map_info; 390147883Sscottl 391147883Sscottl map_info = (struct mpt_map_info *)arg; 392147883Sscottl map_info->error = error; 393147883Sscottl map_info->phys = segs->ds_addr; 394147883Sscottl} 395147883Sscottl 396147883Sscottl/**************************** Reply/Event Handling ****************************/ 397147883Sscottlint 398147883Sscottlmpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type, 399147883Sscottl mpt_handler_t handler, uint32_t *phandler_id) 400147883Sscottl{ 401147883Sscottl 402147883Sscottl switch (type) { 403147883Sscottl case MPT_HANDLER_REPLY: 404147883Sscottl { 405147883Sscottl u_int cbi; 406147883Sscottl u_int free_cbi; 407147883Sscottl 408147883Sscottl if (phandler_id == NULL) 409147883Sscottl return (EINVAL); 410147883Sscottl 411147883Sscottl free_cbi = MPT_HANDLER_ID_NONE; 412147883Sscottl for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) { 413147883Sscottl /* 414147883Sscottl * If the same handler is registered multiple 415147883Sscottl * times, don't error out. Just return the 416147883Sscottl * index of the original registration. 417147883Sscottl */ 418147883Sscottl if (mpt_reply_handlers[cbi] == handler.reply_handler) { 419147883Sscottl *phandler_id = MPT_CBI_TO_HID(cbi); 420147883Sscottl return (0); 421147883Sscottl } 422147883Sscottl 423147883Sscottl /* 424147883Sscottl * Fill from the front in the hope that 425147883Sscottl * all registered handlers consume only a 426147883Sscottl * single cache line. 427147883Sscottl * 428147883Sscottl * We don't break on the first empty slot so 429147883Sscottl * that the full table is checked to see if 430147883Sscottl * this handler was previously registered. 431147883Sscottl */ 432157117Smjacob if (free_cbi == MPT_HANDLER_ID_NONE && 433157117Smjacob (mpt_reply_handlers[cbi] 434147883Sscottl == mpt_default_reply_handler)) 435147883Sscottl free_cbi = cbi; 436147883Sscottl } 437157117Smjacob if (free_cbi == MPT_HANDLER_ID_NONE) { 438147883Sscottl return (ENOMEM); 439157117Smjacob } 440147883Sscottl mpt_reply_handlers[free_cbi] = handler.reply_handler; 441147883Sscottl *phandler_id = MPT_CBI_TO_HID(free_cbi); 442147883Sscottl break; 443147883Sscottl } 444147883Sscottl default: 445147883Sscottl mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type); 446147883Sscottl return (EINVAL); 447147883Sscottl } 448147883Sscottl return (0); 449147883Sscottl} 450147883Sscottl 451147883Sscottlint 452147883Sscottlmpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type, 453147883Sscottl mpt_handler_t handler, uint32_t handler_id) 454147883Sscottl{ 455147883Sscottl 456147883Sscottl switch (type) { 457147883Sscottl case MPT_HANDLER_REPLY: 458147883Sscottl { 459147883Sscottl u_int cbi; 460147883Sscottl 461147883Sscottl cbi = MPT_CBI(handler_id); 462147883Sscottl if (cbi >= MPT_NUM_REPLY_HANDLERS 463147883Sscottl || mpt_reply_handlers[cbi] != handler.reply_handler) 464147883Sscottl return (ENOENT); 465147883Sscottl mpt_reply_handlers[cbi] = mpt_default_reply_handler; 466147883Sscottl break; 467147883Sscottl } 468147883Sscottl default: 469147883Sscottl mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type); 470147883Sscottl return (EINVAL); 471147883Sscottl } 472147883Sscottl return (0); 473147883Sscottl} 474147883Sscottl 475147883Sscottlstatic int 476147883Sscottlmpt_default_reply_handler(struct mpt_softc *mpt, request_t *req, 477157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 478147883Sscottl{ 479157117Smjacob mpt_prt(mpt, 480157117Smjacob "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n", 481157117Smjacob req, req->serno, reply_desc, reply_frame); 482147883Sscottl 483147883Sscottl if (reply_frame != NULL) 484147883Sscottl mpt_dump_reply_frame(mpt, reply_frame); 485147883Sscottl 486157117Smjacob mpt_prt(mpt, "Reply Frame Ignored\n"); 487147883Sscottl 488147883Sscottl return (/*free_reply*/TRUE); 489147883Sscottl} 490147883Sscottl 491147883Sscottlstatic int 492147883Sscottlmpt_config_reply_handler(struct mpt_softc *mpt, request_t *req, 493157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 494147883Sscottl{ 495147883Sscottl if (req != NULL) { 496147883Sscottl 497147883Sscottl if (reply_frame != NULL) { 498147883Sscottl MSG_CONFIG *cfgp; 499147883Sscottl MSG_CONFIG_REPLY *reply; 500147883Sscottl 501147883Sscottl cfgp = (MSG_CONFIG *)req->req_vbuf; 502147883Sscottl reply = (MSG_CONFIG_REPLY *)reply_frame; 503147883Sscottl req->IOCStatus = le16toh(reply_frame->IOCStatus); 504147883Sscottl bcopy(&reply->Header, &cfgp->Header, 505147883Sscottl sizeof(cfgp->Header)); 506170252Sscottl cfgp->ExtPageLength = reply->ExtPageLength; 507170252Sscottl cfgp->ExtPageType = reply->ExtPageType; 508147883Sscottl } 509147883Sscottl req->state &= ~REQ_STATE_QUEUED; 510147883Sscottl req->state |= REQ_STATE_DONE; 511147883Sscottl TAILQ_REMOVE(&mpt->request_pending_list, req, links); 512157354Smjacob if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) { 513147883Sscottl wakeup(req); 514160396Smjacob } else if ((req->state & REQ_STATE_TIMEDOUT) != 0) { 515160396Smjacob /* 516160396Smjacob * Whew- we can free this request (late completion) 517160396Smjacob */ 518160396Smjacob mpt_free_request(mpt, req); 519157354Smjacob } 520147883Sscottl } 521147883Sscottl 522157354Smjacob return (TRUE); 523147883Sscottl} 524147883Sscottl 525147883Sscottlstatic int 526147883Sscottlmpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req, 527157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 528147883Sscottl{ 529147883Sscottl /* Nothing to be done. */ 530157354Smjacob return (TRUE); 531147883Sscottl} 532147883Sscottl 533147883Sscottlstatic int 534147883Sscottlmpt_event_reply_handler(struct mpt_softc *mpt, request_t *req, 535157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 536147883Sscottl{ 537147883Sscottl int free_reply; 538147883Sscottl 539157354Smjacob KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler")); 540157354Smjacob KASSERT(req != NULL, ("null request in mpt_event_reply_handler")); 541147883Sscottl 542147883Sscottl free_reply = TRUE; 543147883Sscottl switch (reply_frame->Function) { 544147883Sscottl case MPI_FUNCTION_EVENT_NOTIFICATION: 545147883Sscottl { 546147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg; 547147883Sscottl struct mpt_personality *pers; 548147883Sscottl u_int handled; 549147883Sscottl 550147883Sscottl handled = 0; 551147883Sscottl msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 552164315Sjb msg->EventDataLength = le16toh(msg->EventDataLength); 553164315Sjb msg->IOCStatus = le16toh(msg->IOCStatus); 554164315Sjb msg->IOCLogInfo = le32toh(msg->IOCLogInfo); 555164315Sjb msg->Event = le32toh(msg->Event); 556147883Sscottl MPT_PERS_FOREACH(mpt, pers) 557147883Sscottl handled += pers->event(mpt, req, msg); 558147883Sscottl 559155521Smjacob if (handled == 0 && mpt->mpt_pers_mask == 0) { 560156301Smjacob mpt_lprt(mpt, MPT_PRT_INFO, 561155521Smjacob "No Handlers For Any Event Notify Frames. " 562155521Smjacob "Event %#x (ACK %sequired).\n", 563155521Smjacob msg->Event, msg->AckRequired? "r" : "not r"); 564155521Smjacob } else if (handled == 0) { 565156301Smjacob mpt_lprt(mpt, MPT_PRT_WARN, 566155521Smjacob "Unhandled Event Notify Frame. Event %#x " 567155521Smjacob "(ACK %sequired).\n", 568155521Smjacob msg->Event, msg->AckRequired? "r" : "not r"); 569155521Smjacob } 570147883Sscottl 571147883Sscottl if (msg->AckRequired) { 572147883Sscottl request_t *ack_req; 573147883Sscottl uint32_t context; 574147883Sscottl 575164315Sjb context = req->index | MPT_REPLY_HANDLER_EVENTS; 576157354Smjacob ack_req = mpt_get_request(mpt, FALSE); 577147883Sscottl if (ack_req == NULL) { 578147883Sscottl struct mpt_evtf_record *evtf; 579147883Sscottl 580147883Sscottl evtf = (struct mpt_evtf_record *)reply_frame; 581147883Sscottl evtf->context = context; 582147883Sscottl LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links); 583147883Sscottl free_reply = FALSE; 584147883Sscottl break; 585147883Sscottl } 586147883Sscottl mpt_send_event_ack(mpt, ack_req, msg, context); 587157354Smjacob /* 588157354Smjacob * Don't check for CONTINUATION_REPLY here 589157354Smjacob */ 590157354Smjacob return (free_reply); 591147883Sscottl } 592147883Sscottl break; 593147883Sscottl } 594147883Sscottl case MPI_FUNCTION_PORT_ENABLE: 595157354Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n"); 596147883Sscottl break; 597147883Sscottl case MPI_FUNCTION_EVENT_ACK: 598147883Sscottl break; 599147883Sscottl default: 600157354Smjacob mpt_prt(mpt, "unknown event function: %x\n", 601147883Sscottl reply_frame->Function); 602147883Sscottl break; 603147883Sscottl } 604147883Sscottl 605157354Smjacob /* 606157354Smjacob * I'm not sure that this continuation stuff works as it should. 607157354Smjacob * 608157354Smjacob * I've had FC async events occur that free the frame up because 609157354Smjacob * the continuation bit isn't set, and then additional async events 610157354Smjacob * then occur using the same context. As you might imagine, this 611157354Smjacob * leads to Very Bad Thing. 612157354Smjacob * 613157354Smjacob * Let's just be safe for now and not free them up until we figure 614157354Smjacob * out what's actually happening here. 615157354Smjacob */ 616157354Smjacob#if 0 617157354Smjacob if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) { 618147883Sscottl TAILQ_REMOVE(&mpt->request_pending_list, req, links); 619157354Smjacob mpt_free_request(mpt, req); 620157354Smjacob mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation", 621157354Smjacob reply_frame->Function, req, req->serno); 622157354Smjacob if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 623157354Smjacob MSG_EVENT_NOTIFY_REPLY *msg = 624157354Smjacob (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 625157354Smjacob mpt_prtc(mpt, " Event=0x%x AckReq=%d", 626157354Smjacob msg->Event, msg->AckRequired); 627157354Smjacob } 628157354Smjacob } else { 629157354Smjacob mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation", 630157354Smjacob reply_frame->Function, req, req->serno); 631157354Smjacob if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 632157354Smjacob MSG_EVENT_NOTIFY_REPLY *msg = 633157354Smjacob (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 634157354Smjacob mpt_prtc(mpt, " Event=0x%x AckReq=%d", 635157354Smjacob msg->Event, msg->AckRequired); 636157354Smjacob } 637157354Smjacob mpt_prtc(mpt, "\n"); 638147883Sscottl } 639157354Smjacob#endif 640147883Sscottl return (free_reply); 641147883Sscottl} 642147883Sscottl 643147883Sscottl/* 644147883Sscottl * Process an asynchronous event from the IOC. 645147883Sscottl */ 646147883Sscottlstatic int 647147883Sscottlmpt_core_event(struct mpt_softc *mpt, request_t *req, 648147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg) 649147883Sscottl{ 650155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n", 651155521Smjacob msg->Event & 0xFF); 652147883Sscottl switch(msg->Event & 0xFF) { 653147883Sscottl case MPI_EVENT_NONE: 654147883Sscottl break; 655147883Sscottl case MPI_EVENT_LOG_DATA: 656147883Sscottl { 657147883Sscottl int i; 658147883Sscottl 659147883Sscottl /* Some error occured that LSI wants logged */ 660147883Sscottl mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n", 661147883Sscottl msg->IOCLogInfo); 662147883Sscottl mpt_prt(mpt, "\tEvtLogData: Event Data:"); 663147883Sscottl for (i = 0; i < msg->EventDataLength; i++) 664147883Sscottl mpt_prtc(mpt, " %08x", msg->Data[i]); 665147883Sscottl mpt_prtc(mpt, "\n"); 666147883Sscottl break; 667147883Sscottl } 668147883Sscottl case MPI_EVENT_EVENT_CHANGE: 669147883Sscottl /* 670147883Sscottl * This is just an acknowledgement 671147883Sscottl * of our mpt_send_event_request. 672147883Sscottl */ 673147883Sscottl break; 674155521Smjacob case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE: 675155521Smjacob break; 676147883Sscottl default: 677157354Smjacob return (0); 678147883Sscottl break; 679147883Sscottl } 680157354Smjacob return (1); 681147883Sscottl} 682147883Sscottl 683147883Sscottlstatic void 684147883Sscottlmpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 685147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context) 686147883Sscottl{ 687147883Sscottl MSG_EVENT_ACK *ackp; 688147883Sscottl 689147883Sscottl ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf; 690157354Smjacob memset(ackp, 0, sizeof (*ackp)); 691147883Sscottl ackp->Function = MPI_FUNCTION_EVENT_ACK; 692164315Sjb ackp->Event = htole32(msg->Event); 693164315Sjb ackp->EventContext = htole32(msg->EventContext); 694164315Sjb ackp->MsgContext = htole32(context); 695147883Sscottl mpt_check_doorbell(mpt); 696147883Sscottl mpt_send_cmd(mpt, ack_req); 697147883Sscottl} 698147883Sscottl 699147883Sscottl/***************************** Interrupt Handling *****************************/ 700147883Sscottlvoid 701147883Sscottlmpt_intr(void *arg) 702147883Sscottl{ 703147883Sscottl struct mpt_softc *mpt; 704157117Smjacob uint32_t reply_desc; 705157117Smjacob int ntrips = 0; 706147883Sscottl 707147883Sscottl mpt = (struct mpt_softc *)arg; 708157662Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n"); 709169293Smjacob MPT_LOCK_ASSERT(mpt); 710169293Smjacob 711147883Sscottl while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) { 712147883Sscottl request_t *req; 713147883Sscottl MSG_DEFAULT_REPLY *reply_frame; 714147883Sscottl uint32_t reply_baddr; 715157117Smjacob uint32_t ctxt_idx; 716147883Sscottl u_int cb_index; 717147883Sscottl u_int req_index; 718147883Sscottl int free_rf; 719147883Sscottl 720147883Sscottl req = NULL; 721147883Sscottl reply_frame = NULL; 722147883Sscottl reply_baddr = 0; 723147883Sscottl if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) { 724147883Sscottl u_int offset; 725147883Sscottl /* 726147883Sscottl * Insure that the reply frame is coherent. 727147883Sscottl */ 728157354Smjacob reply_baddr = MPT_REPLY_BADDR(reply_desc); 729147883Sscottl offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF); 730157117Smjacob bus_dmamap_sync_range(mpt->reply_dmat, 731157117Smjacob mpt->reply_dmap, offset, MPT_REPLY_SIZE, 732157117Smjacob BUS_DMASYNC_POSTREAD); 733147883Sscottl reply_frame = MPT_REPLY_OTOV(mpt, offset); 734157117Smjacob ctxt_idx = le32toh(reply_frame->MsgContext); 735157117Smjacob } else { 736157117Smjacob uint32_t type; 737157117Smjacob 738157117Smjacob type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc); 739157117Smjacob ctxt_idx = reply_desc; 740157117Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n", 741157117Smjacob reply_desc); 742157117Smjacob 743157117Smjacob switch (type) { 744157117Smjacob case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT: 745157117Smjacob ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK; 746157117Smjacob break; 747157117Smjacob case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET: 748157117Smjacob ctxt_idx = GET_IO_INDEX(reply_desc); 749157117Smjacob if (mpt->tgt_cmd_ptrs == NULL) { 750157117Smjacob mpt_prt(mpt, 751157117Smjacob "mpt_intr: no target cmd ptrs\n"); 752157117Smjacob reply_desc = MPT_REPLY_EMPTY; 753157117Smjacob break; 754157117Smjacob } 755157117Smjacob if (ctxt_idx >= mpt->tgt_cmds_allocated) { 756157117Smjacob mpt_prt(mpt, 757157117Smjacob "mpt_intr: bad tgt cmd ctxt %u\n", 758157117Smjacob ctxt_idx); 759157117Smjacob reply_desc = MPT_REPLY_EMPTY; 760157117Smjacob ntrips = 1000; 761157117Smjacob break; 762157117Smjacob } 763157117Smjacob req = mpt->tgt_cmd_ptrs[ctxt_idx]; 764157117Smjacob if (req == NULL) { 765157117Smjacob mpt_prt(mpt, "no request backpointer " 766157117Smjacob "at index %u", ctxt_idx); 767157117Smjacob reply_desc = MPT_REPLY_EMPTY; 768157117Smjacob ntrips = 1000; 769157117Smjacob break; 770157117Smjacob } 771157117Smjacob /* 772157117Smjacob * Reformulate ctxt_idx to be just as if 773157117Smjacob * it were another type of context reply 774157117Smjacob * so the code below will find the request 775157117Smjacob * via indexing into the pool. 776157117Smjacob */ 777157117Smjacob ctxt_idx = 778157117Smjacob req->index | mpt->scsi_tgt_handler_id; 779157117Smjacob req = NULL; 780157117Smjacob break; 781157117Smjacob case MPI_CONTEXT_REPLY_TYPE_LAN: 782157117Smjacob mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n", 783157117Smjacob reply_desc); 784157117Smjacob reply_desc = MPT_REPLY_EMPTY; 785157117Smjacob break; 786157117Smjacob default: 787157117Smjacob mpt_prt(mpt, "Context Reply 0x%08x?\n", type); 788157117Smjacob reply_desc = MPT_REPLY_EMPTY; 789157117Smjacob break; 790157117Smjacob } 791157117Smjacob if (reply_desc == MPT_REPLY_EMPTY) { 792157117Smjacob if (ntrips++ > 1000) { 793157117Smjacob break; 794157117Smjacob } 795157117Smjacob continue; 796157117Smjacob } 797147883Sscottl } 798157117Smjacob 799157117Smjacob cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx); 800157117Smjacob req_index = MPT_CONTEXT_TO_REQI(ctxt_idx); 801157117Smjacob if (req_index < MPT_MAX_REQUESTS(mpt)) { 802147883Sscottl req = &mpt->request_pool[req_index]; 803157354Smjacob } else { 804157354Smjacob mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc ==" 805157354Smjacob " 0x%x)\n", req_index, reply_desc); 806157117Smjacob } 807147883Sscottl 808157117Smjacob free_rf = mpt_reply_handlers[cb_index](mpt, req, 809157117Smjacob reply_desc, reply_frame); 810147883Sscottl 811157354Smjacob if (reply_frame != NULL && free_rf) { 812147883Sscottl mpt_free_reply(mpt, reply_baddr); 813157354Smjacob } 814157117Smjacob 815157117Smjacob /* 816157117Smjacob * If we got ourselves disabled, don't get stuck in a loop 817157117Smjacob */ 818157117Smjacob if (mpt->disabled) { 819157117Smjacob mpt_disable_ints(mpt); 820157117Smjacob break; 821157117Smjacob } 822157117Smjacob if (ntrips++ > 1000) { 823157117Smjacob break; 824157117Smjacob } 825147883Sscottl } 826157662Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n"); 827147883Sscottl} 828147883Sscottl 829147883Sscottl/******************************* Error Recovery *******************************/ 830147883Sscottlvoid 831147883Sscottlmpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain, 832147883Sscottl u_int iocstatus) 833147883Sscottl{ 834147883Sscottl MSG_DEFAULT_REPLY ioc_status_frame; 835147883Sscottl request_t *req; 836147883Sscottl 837157354Smjacob memset(&ioc_status_frame, 0, sizeof(ioc_status_frame)); 838147883Sscottl ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4); 839156104Smjacob ioc_status_frame.IOCStatus = iocstatus; 840147883Sscottl while((req = TAILQ_FIRST(chain)) != NULL) { 841147883Sscottl MSG_REQUEST_HEADER *msg_hdr; 842147883Sscottl u_int cb_index; 843157354Smjacob 844156796Smjacob TAILQ_REMOVE(chain, req, links); 845147883Sscottl msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf; 846156104Smjacob ioc_status_frame.Function = msg_hdr->Function; 847156104Smjacob ioc_status_frame.MsgContext = msg_hdr->MsgContext; 848147883Sscottl cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext)); 849157117Smjacob mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext, 850157117Smjacob &ioc_status_frame); 851147883Sscottl } 852147883Sscottl} 853147883Sscottl 854147883Sscottl/********************************* Diagnostics ********************************/ 855147883Sscottl/* 856147883Sscottl * Perform a diagnostic dump of a reply frame. 857147883Sscottl */ 858147883Sscottlvoid 859147883Sscottlmpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame) 860147883Sscottl{ 861147883Sscottl mpt_prt(mpt, "Address Reply:\n"); 862147883Sscottl mpt_print_reply(reply_frame); 863147883Sscottl} 864147883Sscottl 865147883Sscottl/******************************* Doorbell Access ******************************/ 866147883Sscottlstatic __inline uint32_t mpt_rd_db(struct mpt_softc *mpt); 867147883Sscottlstatic __inline uint32_t mpt_rd_intr(struct mpt_softc *mpt); 868147883Sscottl 869147883Sscottlstatic __inline uint32_t 870147883Sscottlmpt_rd_db(struct mpt_softc *mpt) 871147883Sscottl{ 872101704Smjacob return mpt_read(mpt, MPT_OFFSET_DOORBELL); 873101704Smjacob} 874101704Smjacob 875147883Sscottlstatic __inline uint32_t 876147883Sscottlmpt_rd_intr(struct mpt_softc *mpt) 877101704Smjacob{ 878101704Smjacob return mpt_read(mpt, MPT_OFFSET_INTR_STATUS); 879101704Smjacob} 880101704Smjacob 881101704Smjacob/* Busy wait for a door bell to be read by IOC */ 882101704Smjacobstatic int 883147883Sscottlmpt_wait_db_ack(struct mpt_softc *mpt) 884101704Smjacob{ 885101704Smjacob int i; 886101704Smjacob for (i=0; i < MPT_MAX_WAIT; i++) { 887101704Smjacob if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) { 888101704Smjacob maxwait_ack = i > maxwait_ack ? i : maxwait_ack; 889157117Smjacob return (MPT_OK); 890101704Smjacob } 891157117Smjacob DELAY(200); 892101704Smjacob } 893157117Smjacob return (MPT_FAIL); 894101704Smjacob} 895101704Smjacob 896101704Smjacob/* Busy wait for a door bell interrupt */ 897101704Smjacobstatic int 898147883Sscottlmpt_wait_db_int(struct mpt_softc *mpt) 899101704Smjacob{ 900101704Smjacob int i; 901164837Smjacob for (i = 0; i < MPT_MAX_WAIT; i++) { 902101704Smjacob if (MPT_DB_INTR(mpt_rd_intr(mpt))) { 903101704Smjacob maxwait_int = i > maxwait_int ? i : maxwait_int; 904101704Smjacob return MPT_OK; 905101704Smjacob } 906101704Smjacob DELAY(100); 907101704Smjacob } 908157117Smjacob return (MPT_FAIL); 909101704Smjacob} 910101704Smjacob 911101704Smjacob/* Wait for IOC to transition to a give state */ 912101704Smjacobvoid 913147883Sscottlmpt_check_doorbell(struct mpt_softc *mpt) 914101704Smjacob{ 915147883Sscottl uint32_t db = mpt_rd_db(mpt); 916101704Smjacob if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) { 917147883Sscottl mpt_prt(mpt, "Device not running\n"); 918101704Smjacob mpt_print_db(db); 919101704Smjacob } 920101704Smjacob} 921101704Smjacob 922101704Smjacob/* Wait for IOC to transition to a give state */ 923101704Smjacobstatic int 924147883Sscottlmpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state) 925101704Smjacob{ 926101704Smjacob int i; 927101704Smjacob 928101704Smjacob for (i = 0; i < MPT_MAX_WAIT; i++) { 929147883Sscottl uint32_t db = mpt_rd_db(mpt); 930101704Smjacob if (MPT_STATE(db) == state) { 931101704Smjacob maxwait_state = i > maxwait_state ? i : maxwait_state; 932101704Smjacob return (MPT_OK); 933101704Smjacob } 934101704Smjacob DELAY(100); 935101704Smjacob } 936101704Smjacob return (MPT_FAIL); 937101704Smjacob} 938101704Smjacob 939101704Smjacob 940147883Sscottl/************************* Intialization/Configuration ************************/ 941147883Sscottlstatic int mpt_download_fw(struct mpt_softc *mpt); 942147883Sscottl 943101704Smjacob/* Issue the reset COMMAND to the IOC */ 944147883Sscottlstatic int 945147883Sscottlmpt_soft_reset(struct mpt_softc *mpt) 946101704Smjacob{ 947147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n"); 948101704Smjacob 949101704Smjacob /* Have to use hard reset if we are not in Running state */ 950101704Smjacob if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) { 951147883Sscottl mpt_prt(mpt, "soft reset failed: device not running\n"); 952157117Smjacob return (MPT_FAIL); 953101704Smjacob } 954101704Smjacob 955101704Smjacob /* If door bell is in use we don't have a chance of getting 956101704Smjacob * a word in since the IOC probably crashed in message 957101704Smjacob * processing. So don't waste our time. 958101704Smjacob */ 959101704Smjacob if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) { 960147883Sscottl mpt_prt(mpt, "soft reset failed: doorbell wedged\n"); 961157117Smjacob return (MPT_FAIL); 962101704Smjacob } 963101704Smjacob 964101704Smjacob /* Send the reset request to the IOC */ 965101704Smjacob mpt_write(mpt, MPT_OFFSET_DOORBELL, 966101704Smjacob MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT); 967101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 968147883Sscottl mpt_prt(mpt, "soft reset failed: ack timeout\n"); 969157117Smjacob return (MPT_FAIL); 970101704Smjacob } 971101704Smjacob 972101704Smjacob /* Wait for the IOC to reload and come out of reset state */ 973101704Smjacob if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) { 974147883Sscottl mpt_prt(mpt, "soft reset failed: device did not restart\n"); 975157117Smjacob return (MPT_FAIL); 976101704Smjacob } 977101704Smjacob 978101704Smjacob return MPT_OK; 979101704Smjacob} 980101704Smjacob 981147883Sscottlstatic int 982147883Sscottlmpt_enable_diag_mode(struct mpt_softc *mpt) 983147883Sscottl{ 984147883Sscottl int try; 985147883Sscottl 986147883Sscottl try = 20; 987147883Sscottl while (--try) { 988147883Sscottl 989147883Sscottl if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0) 990147883Sscottl break; 991147883Sscottl 992147883Sscottl /* Enable diagnostic registers */ 993147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF); 994147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE); 995147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE); 996147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE); 997147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE); 998147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE); 999147883Sscottl 1000147883Sscottl DELAY(100000); 1001147883Sscottl } 1002147883Sscottl if (try == 0) 1003147883Sscottl return (EIO); 1004147883Sscottl return (0); 1005147883Sscottl} 1006147883Sscottl 1007147883Sscottlstatic void 1008147883Sscottlmpt_disable_diag_mode(struct mpt_softc *mpt) 1009147883Sscottl{ 1010147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF); 1011147883Sscottl} 1012147883Sscottl 1013101704Smjacob/* This is a magic diagnostic reset that resets all the ARM 1014156104Smjacob * processors in the chip. 1015101704Smjacob */ 1016147883Sscottlstatic void 1017147883Sscottlmpt_hard_reset(struct mpt_softc *mpt) 1018101704Smjacob{ 1019147883Sscottl int error; 1020147883Sscottl int wait; 1021147883Sscottl uint32_t diagreg; 1022147883Sscottl 1023147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n"); 1024147883Sscottl 1025147883Sscottl error = mpt_enable_diag_mode(mpt); 1026147883Sscottl if (error) { 1027147883Sscottl mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n"); 1028147883Sscottl mpt_prt(mpt, "Trying to reset anyway.\n"); 1029101704Smjacob } 1030101704Smjacob 1031147883Sscottl diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 1032101704Smjacob 1033147883Sscottl /* 1034147883Sscottl * This appears to be a workaround required for some 1035147883Sscottl * firmware or hardware revs. 1036147883Sscottl */ 1037147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM); 1038147883Sscottl DELAY(1000); 1039147883Sscottl 1040101704Smjacob /* Diag. port is now active so we can now hit the reset bit */ 1041147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER); 1042101704Smjacob 1043147883Sscottl /* 1044147883Sscottl * Ensure that the reset has finished. We delay 1ms 1045147883Sscottl * prior to reading the register to make sure the chip 1046147883Sscottl * has sufficiently completed its reset to handle register 1047147883Sscottl * accesses. 1048147883Sscottl */ 1049147883Sscottl wait = 5000; 1050147883Sscottl do { 1051147883Sscottl DELAY(1000); 1052147883Sscottl diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 1053147883Sscottl } while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0); 1054101704Smjacob 1055147883Sscottl if (wait == 0) { 1056147883Sscottl mpt_prt(mpt, "WARNING - Failed hard reset! " 1057147883Sscottl "Trying to initialize anyway.\n"); 1058147883Sscottl } 1059101704Smjacob 1060147883Sscottl /* 1061147883Sscottl * If we have firmware to download, it must be loaded before 1062147883Sscottl * the controller will become operational. Do so now. 1063147883Sscottl */ 1064147883Sscottl if (mpt->fw_image != NULL) { 1065147883Sscottl 1066147883Sscottl error = mpt_download_fw(mpt); 1067147883Sscottl 1068147883Sscottl if (error) { 1069147883Sscottl mpt_prt(mpt, "WARNING - Firmware Download Failed!\n"); 1070147883Sscottl mpt_prt(mpt, "Trying to initialize anyway.\n"); 1071147883Sscottl } 1072101704Smjacob } 1073101704Smjacob 1074147883Sscottl /* 1075147883Sscottl * Reseting the controller should have disabled write 1076147883Sscottl * access to the diagnostic registers, but disable 1077147883Sscottl * manually to be sure. 1078147883Sscottl */ 1079147883Sscottl mpt_disable_diag_mode(mpt); 1080101704Smjacob} 1081101704Smjacob 1082147883Sscottlstatic void 1083147883Sscottlmpt_core_ioc_reset(struct mpt_softc *mpt, int type) 1084147883Sscottl{ 1085147883Sscottl /* 1086147883Sscottl * Complete all pending requests with a status 1087147883Sscottl * appropriate for an IOC reset. 1088147883Sscottl */ 1089147883Sscottl mpt_complete_request_chain(mpt, &mpt->request_pending_list, 1090147883Sscottl MPI_IOCSTATUS_INVALID_STATE); 1091147883Sscottl} 1092147883Sscottl 1093147883Sscottl 1094101704Smjacob/* 1095101704Smjacob * Reset the IOC when needed. Try software command first then if needed 1096101704Smjacob * poke at the magic diagnostic reset. Note that a hard reset resets 1097101704Smjacob * *both* IOCs on dual function chips (FC929 && LSI1030) as well as 1098101704Smjacob * fouls up the PCI configuration registers. 1099101704Smjacob */ 1100101704Smjacobint 1101147883Sscottlmpt_reset(struct mpt_softc *mpt, int reinit) 1102101704Smjacob{ 1103147883Sscottl struct mpt_personality *pers; 1104147883Sscottl int ret; 1105156104Smjacob int retry_cnt = 0; 1106101704Smjacob 1107156104Smjacob /* 1108156104Smjacob * Try a soft reset. If that fails, get out the big hammer. 1109156104Smjacob */ 1110156104Smjacob again: 1111101704Smjacob if ((ret = mpt_soft_reset(mpt)) != MPT_OK) { 1112156104Smjacob int cnt; 1113156104Smjacob for (cnt = 0; cnt < 5; cnt++) { 1114156104Smjacob /* Failed; do a hard reset */ 1115156104Smjacob mpt_hard_reset(mpt); 1116101704Smjacob 1117156104Smjacob /* 1118156104Smjacob * Wait for the IOC to reload 1119156104Smjacob * and come out of reset state 1120156104Smjacob */ 1121156104Smjacob ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1122156104Smjacob if (ret == MPT_OK) { 1123156104Smjacob break; 1124156104Smjacob } 1125156104Smjacob /* 1126156104Smjacob * Okay- try to check again... 1127156104Smjacob */ 1128156104Smjacob ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1129156104Smjacob if (ret == MPT_OK) { 1130156104Smjacob break; 1131156104Smjacob } 1132156104Smjacob mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n", 1133156104Smjacob retry_cnt, cnt); 1134156104Smjacob } 1135101704Smjacob } 1136101704Smjacob 1137156104Smjacob if (retry_cnt == 0) { 1138156104Smjacob /* 1139156104Smjacob * Invoke reset handlers. We bump the reset count so 1140156104Smjacob * that mpt_wait_req() understands that regardless of 1141156104Smjacob * the specified wait condition, it should stop its wait. 1142156104Smjacob */ 1143156104Smjacob mpt->reset_cnt++; 1144156104Smjacob MPT_PERS_FOREACH(mpt, pers) 1145156104Smjacob pers->reset(mpt, ret); 1146156104Smjacob } 1147147883Sscottl 1148157354Smjacob if (reinit) { 1149157117Smjacob ret = mpt_enable_ioc(mpt, 1); 1150156104Smjacob if (ret == MPT_OK) { 1151156104Smjacob mpt_enable_ints(mpt); 1152156104Smjacob } 1153156104Smjacob } 1154156104Smjacob if (ret != MPT_OK && retry_cnt++ < 2) { 1155156104Smjacob goto again; 1156156104Smjacob } 1157101704Smjacob return ret; 1158101704Smjacob} 1159101704Smjacob 1160101704Smjacob/* Return a command buffer to the free queue */ 1161101704Smjacobvoid 1162147883Sscottlmpt_free_request(struct mpt_softc *mpt, request_t *req) 1163101704Smjacob{ 1164155521Smjacob request_t *nxt; 1165147883Sscottl struct mpt_evtf_record *record; 1166147883Sscottl uint32_t reply_baddr; 1167147883Sscottl 1168103871Smjacob if (req == NULL || req != &mpt->request_pool[req->index]) { 1169101704Smjacob panic("mpt_free_request bad req ptr\n"); 1170101704Smjacob return; 1171101704Smjacob } 1172155521Smjacob if ((nxt = req->chain) != NULL) { 1173155521Smjacob req->chain = NULL; 1174155521Smjacob mpt_free_request(mpt, nxt); /* NB: recursion */ 1175155521Smjacob } 1176157354Smjacob KASSERT(req->state != REQ_STATE_FREE, ("freeing free request")); 1177157354Smjacob KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request")); 1178169293Smjacob MPT_LOCK_ASSERT(mpt); 1179157662Smjacob KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1180157662Smjacob ("mpt_free_request: req %p:%u func %x already on freelist", 1181157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1182157662Smjacob KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1183157662Smjacob ("mpt_free_request: req %p:%u func %x on pending list", 1184157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1185157662Smjacob#ifdef INVARIANTS 1186157662Smjacob mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__); 1187157662Smjacob#endif 1188157354Smjacob 1189101704Smjacob req->ccb = NULL; 1190147883Sscottl if (LIST_EMPTY(&mpt->ack_frames)) { 1191157117Smjacob /* 1192157117Smjacob * Insert free ones at the tail 1193157117Smjacob */ 1194157354Smjacob req->serno = 0; 1195157354Smjacob req->state = REQ_STATE_FREE; 1196157662Smjacob#ifdef INVARIANTS 1197157662Smjacob memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER)); 1198157662Smjacob#endif 1199157117Smjacob TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links); 1200147883Sscottl if (mpt->getreqwaiter != 0) { 1201147883Sscottl mpt->getreqwaiter = 0; 1202147883Sscottl wakeup(&mpt->request_free_list); 1203147883Sscottl } 1204147883Sscottl return; 1205147883Sscottl } 1206147883Sscottl 1207147883Sscottl /* 1208147883Sscottl * Process an ack frame deferred due to resource shortage. 1209147883Sscottl */ 1210147883Sscottl record = LIST_FIRST(&mpt->ack_frames); 1211147883Sscottl LIST_REMOVE(record, links); 1212157354Smjacob req->state = REQ_STATE_ALLOCATED; 1213157662Smjacob mpt_assign_serno(mpt, req); 1214147883Sscottl mpt_send_event_ack(mpt, req, &record->reply, record->context); 1215147883Sscottl reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply) 1216147883Sscottl + (mpt->reply_phys & 0xFFFFFFFF); 1217147883Sscottl mpt_free_reply(mpt, reply_baddr); 1218101704Smjacob} 1219101704Smjacob 1220101704Smjacob/* Get a command buffer from the free queue */ 1221101704Smjacobrequest_t * 1222147883Sscottlmpt_get_request(struct mpt_softc *mpt, int sleep_ok) 1223101704Smjacob{ 1224101704Smjacob request_t *req; 1225147883Sscottl 1226147883Sscottlretry: 1227169293Smjacob MPT_LOCK_ASSERT(mpt); 1228147883Sscottl req = TAILQ_FIRST(&mpt->request_free_list); 1229101704Smjacob if (req != NULL) { 1230147883Sscottl KASSERT(req == &mpt->request_pool[req->index], 1231147883Sscottl ("mpt_get_request: corrupted request free list\n")); 1232157354Smjacob KASSERT(req->state == REQ_STATE_FREE, 1233157662Smjacob ("req %p:%u not free on free list %x index %d function %x", 1234157662Smjacob req, req->serno, req->state, req->index, 1235157662Smjacob ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1236147883Sscottl TAILQ_REMOVE(&mpt->request_free_list, req, links); 1237147883Sscottl req->state = REQ_STATE_ALLOCATED; 1238155521Smjacob req->chain = NULL; 1239157662Smjacob mpt_assign_serno(mpt, req); 1240147883Sscottl } else if (sleep_ok != 0) { 1241147883Sscottl mpt->getreqwaiter = 1; 1242147883Sscottl mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0); 1243147883Sscottl goto retry; 1244101704Smjacob } 1245157354Smjacob return (req); 1246101704Smjacob} 1247101704Smjacob 1248101704Smjacob/* Pass the command to the IOC */ 1249101704Smjacobvoid 1250147883Sscottlmpt_send_cmd(struct mpt_softc *mpt, request_t *req) 1251101704Smjacob{ 1252158982Smjacob if (mpt->verbose > MPT_PRT_DEBUG2) { 1253158932Smjacob mpt_dump_request(mpt, req); 1254155521Smjacob } 1255101704Smjacob bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1256103914Smjacob BUS_DMASYNC_PREWRITE); 1257147883Sscottl req->state |= REQ_STATE_QUEUED; 1258157662Smjacob KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1259157662Smjacob ("req %p:%u func %x on freelist list in mpt_send_cmd", 1260157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1261157662Smjacob KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1262157662Smjacob ("req %p:%u func %x already on pending list in mpt_send_cmd", 1263157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1264147883Sscottl TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links); 1265147883Sscottl mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf); 1266101704Smjacob} 1267101704Smjacob 1268101704Smjacob/* 1269147883Sscottl * Wait for a request to complete. 1270147883Sscottl * 1271147883Sscottl * Inputs: 1272147883Sscottl * mpt softc of controller executing request 1273147883Sscottl * req request to wait for 1274147883Sscottl * sleep_ok nonzero implies may sleep in this context 1275147883Sscottl * time_ms timeout in ms. 0 implies no timeout. 1276147883Sscottl * 1277147883Sscottl * Return Values: 1278147883Sscottl * 0 Request completed 1279147883Sscottl * non-0 Timeout fired before request completion. 1280101704Smjacob */ 1281147883Sscottlint 1282147883Sscottlmpt_wait_req(struct mpt_softc *mpt, request_t *req, 1283147883Sscottl mpt_req_state_t state, mpt_req_state_t mask, 1284147883Sscottl int sleep_ok, int time_ms) 1285101704Smjacob{ 1286147883Sscottl int error; 1287147883Sscottl int timeout; 1288147883Sscottl u_int saved_cnt; 1289101704Smjacob 1290147883Sscottl /* 1291147883Sscottl * timeout is in ms. 0 indicates infinite wait. 1292147883Sscottl * Convert to ticks or 500us units depending on 1293147883Sscottl * our sleep mode. 1294147883Sscottl */ 1295157117Smjacob if (sleep_ok != 0) { 1296147883Sscottl timeout = (time_ms * hz) / 1000; 1297157117Smjacob } else { 1298147883Sscottl timeout = time_ms * 2; 1299157117Smjacob } 1300147883Sscottl req->state |= REQ_STATE_NEED_WAKEUP; 1301147883Sscottl mask &= ~REQ_STATE_NEED_WAKEUP; 1302155521Smjacob saved_cnt = mpt->reset_cnt; 1303157117Smjacob while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) { 1304147883Sscottl if (sleep_ok != 0) { 1305147883Sscottl error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout); 1306147883Sscottl if (error == EWOULDBLOCK) { 1307147883Sscottl timeout = 0; 1308147883Sscottl break; 1309147883Sscottl } 1310147883Sscottl } else { 1311147883Sscottl if (time_ms != 0 && --timeout == 0) { 1312147883Sscottl break; 1313147883Sscottl } 1314147883Sscottl DELAY(500); 1315147883Sscottl mpt_intr(mpt); 1316147883Sscottl } 1317147883Sscottl } 1318147883Sscottl req->state &= ~REQ_STATE_NEED_WAKEUP; 1319157117Smjacob if (mpt->reset_cnt != saved_cnt) { 1320147883Sscottl return (EIO); 1321157117Smjacob } 1322157117Smjacob if (time_ms && timeout <= 0) { 1323157117Smjacob MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf; 1324160396Smjacob req->state |= REQ_STATE_TIMEDOUT; 1325157117Smjacob mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function); 1326147883Sscottl return (ETIMEDOUT); 1327157117Smjacob } 1328147883Sscottl return (0); 1329101704Smjacob} 1330101704Smjacob 1331101704Smjacob/* 1332101704Smjacob * Send a command to the IOC via the handshake register. 1333101704Smjacob * 1334101704Smjacob * Only done at initialization time and for certain unusual 1335101704Smjacob * commands such as device/bus reset as specified by LSI. 1336101704Smjacob */ 1337101704Smjacobint 1338147883Sscottlmpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd) 1339101704Smjacob{ 1340101704Smjacob int i; 1341147883Sscottl uint32_t data, *data32; 1342101704Smjacob 1343101704Smjacob /* Check condition of the IOC */ 1344101704Smjacob data = mpt_rd_db(mpt); 1345147883Sscottl if ((MPT_STATE(data) != MPT_DB_STATE_READY 1346147883Sscottl && MPT_STATE(data) != MPT_DB_STATE_RUNNING 1347147883Sscottl && MPT_STATE(data) != MPT_DB_STATE_FAULT) 1348147883Sscottl || MPT_DB_IS_IN_USE(data)) { 1349147883Sscottl mpt_prt(mpt, "handshake aborted - invalid doorbell state\n"); 1350101704Smjacob mpt_print_db(data); 1351147883Sscottl return (EBUSY); 1352101704Smjacob } 1353101704Smjacob 1354101704Smjacob /* We move things in 32 bit chunks */ 1355101704Smjacob len = (len + 3) >> 2; 1356101704Smjacob data32 = cmd; 1357101704Smjacob 1358101704Smjacob /* Clear any left over pending doorbell interupts */ 1359101704Smjacob if (MPT_DB_INTR(mpt_rd_intr(mpt))) 1360101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1361101704Smjacob 1362101704Smjacob /* 1363101704Smjacob * Tell the handshake reg. we are going to send a command 1364101704Smjacob * and how long it is going to be. 1365101704Smjacob */ 1366101704Smjacob data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) | 1367101704Smjacob (len << MPI_DOORBELL_ADD_DWORDS_SHIFT); 1368101704Smjacob mpt_write(mpt, MPT_OFFSET_DOORBELL, data); 1369101704Smjacob 1370101704Smjacob /* Wait for the chip to notice */ 1371101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1372164837Smjacob mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n"); 1373147883Sscottl return (ETIMEDOUT); 1374101704Smjacob } 1375101704Smjacob 1376101704Smjacob /* Clear the interrupt */ 1377101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1378101704Smjacob 1379101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 1380164837Smjacob mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n"); 1381147883Sscottl return (ETIMEDOUT); 1382101704Smjacob } 1383101704Smjacob 1384101704Smjacob /* Send the command */ 1385101704Smjacob for (i = 0; i < len; i++) { 1386164315Sjb mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++)); 1387101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 1388156104Smjacob mpt_prt(mpt, 1389164837Smjacob "mpt_send_handshake_cmd: timeout @ index %d\n", i); 1390147883Sscottl return (ETIMEDOUT); 1391101704Smjacob } 1392101704Smjacob } 1393101704Smjacob return MPT_OK; 1394101704Smjacob} 1395101704Smjacob 1396101704Smjacob/* Get the response from the handshake register */ 1397101704Smjacobint 1398147883Sscottlmpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply) 1399101704Smjacob{ 1400101704Smjacob int left, reply_left; 1401101704Smjacob u_int16_t *data16; 1402164315Sjb uint32_t data; 1403101704Smjacob MSG_DEFAULT_REPLY *hdr; 1404101704Smjacob 1405101704Smjacob /* We move things out in 16 bit chunks */ 1406101704Smjacob reply_len >>= 1; 1407101704Smjacob data16 = (u_int16_t *)reply; 1408101704Smjacob 1409101704Smjacob hdr = (MSG_DEFAULT_REPLY *)reply; 1410101704Smjacob 1411101704Smjacob /* Get first word */ 1412101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1413147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n"); 1414101704Smjacob return ETIMEDOUT; 1415101704Smjacob } 1416164315Sjb data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1417164315Sjb *data16++ = le16toh(data & MPT_DB_DATA_MASK); 1418101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1419101704Smjacob 1420101704Smjacob /* Get Second Word */ 1421101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1422147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n"); 1423101704Smjacob return ETIMEDOUT; 1424101704Smjacob } 1425164315Sjb data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1426164315Sjb *data16++ = le16toh(data & MPT_DB_DATA_MASK); 1427101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1428101704Smjacob 1429157354Smjacob /* 1430157354Smjacob * With the second word, we can now look at the length. 1431157354Smjacob * Warn about a reply that's too short (except for IOC FACTS REPLY) 1432157354Smjacob */ 1433157354Smjacob if ((reply_len >> 1) != hdr->MsgLength && 1434157354Smjacob (hdr->Function != MPI_FUNCTION_IOC_FACTS)){ 1435157354Smjacob#if __FreeBSD_version >= 500000 1436103914Smjacob mpt_prt(mpt, "reply length does not match message length: " 1437157382Smjacob "got %x; expected %zx for function %x\n", 1438157354Smjacob hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1439157354Smjacob#else 1440157354Smjacob mpt_prt(mpt, "reply length does not match message length: " 1441157382Smjacob "got %x; expected %x for function %x\n", 1442157354Smjacob hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1443157354Smjacob#endif 1444101704Smjacob } 1445101704Smjacob 1446101704Smjacob /* Get rest of the reply; but don't overflow the provided buffer */ 1447101704Smjacob left = (hdr->MsgLength << 1) - 2; 1448101704Smjacob reply_left = reply_len - 2; 1449101704Smjacob while (left--) { 1450101704Smjacob u_int16_t datum; 1451101704Smjacob 1452101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1453147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n"); 1454101704Smjacob return ETIMEDOUT; 1455101704Smjacob } 1456164315Sjb data = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1457164315Sjb datum = le16toh(data & MPT_DB_DATA_MASK); 1458101704Smjacob 1459101704Smjacob if (reply_left-- > 0) 1460164315Sjb *data16++ = datum; 1461101704Smjacob 1462101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1463101704Smjacob } 1464101704Smjacob 1465101704Smjacob /* One more wait & clear at the end */ 1466101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1467147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n"); 1468101704Smjacob return ETIMEDOUT; 1469101704Smjacob } 1470101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1471101704Smjacob 1472101704Smjacob if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1473147883Sscottl if (mpt->verbose >= MPT_PRT_TRACE) 1474101704Smjacob mpt_print_reply(hdr); 1475101704Smjacob return (MPT_FAIL | hdr->IOCStatus); 1476101704Smjacob } 1477101704Smjacob 1478101704Smjacob return (0); 1479101704Smjacob} 1480101704Smjacob 1481101704Smjacobstatic int 1482147883Sscottlmpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp) 1483101704Smjacob{ 1484101704Smjacob MSG_IOC_FACTS f_req; 1485101704Smjacob int error; 1486101704Smjacob 1487157354Smjacob memset(&f_req, 0, sizeof f_req); 1488101704Smjacob f_req.Function = MPI_FUNCTION_IOC_FACTS; 1489147883Sscottl f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1490101704Smjacob error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1491164990Smjacob if (error) { 1492101704Smjacob return(error); 1493164990Smjacob } 1494101704Smjacob error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1495101704Smjacob return (error); 1496101704Smjacob} 1497101704Smjacob 1498102199Smjacobstatic int 1499164990Smjacobmpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp) 1500102199Smjacob{ 1501102199Smjacob MSG_PORT_FACTS f_req; 1502102199Smjacob int error; 1503102199Smjacob 1504147883Sscottl memset(&f_req, 0, sizeof f_req); 1505102199Smjacob f_req.Function = MPI_FUNCTION_PORT_FACTS; 1506164990Smjacob f_req.PortNumber = port; 1507147883Sscottl f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1508102199Smjacob error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1509164990Smjacob if (error) { 1510102199Smjacob return(error); 1511164990Smjacob } 1512102199Smjacob error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1513102199Smjacob return (error); 1514102199Smjacob} 1515102199Smjacob 1516101704Smjacob/* 1517101704Smjacob * Send the initialization request. This is where we specify how many 1518101704Smjacob * SCSI busses and how many devices per bus we wish to emulate. 1519101704Smjacob * This is also the command that specifies the max size of the reply 1520101704Smjacob * frames from the IOC that we will be allocating. 1521101704Smjacob */ 1522101704Smjacobstatic int 1523147883Sscottlmpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who) 1524101704Smjacob{ 1525101704Smjacob int error = 0; 1526101704Smjacob MSG_IOC_INIT init; 1527101704Smjacob MSG_IOC_INIT_REPLY reply; 1528101704Smjacob 1529157354Smjacob memset(&init, 0, sizeof init); 1530101704Smjacob init.WhoInit = who; 1531101704Smjacob init.Function = MPI_FUNCTION_IOC_INIT; 1532164990Smjacob init.MaxDevices = 0; /* at least 256 devices per bus */ 1533164990Smjacob init.MaxBuses = 16; /* at least 16 busses */ 1534155521Smjacob 1535155521Smjacob init.MsgVersion = htole16(MPI_VERSION); 1536155521Smjacob init.HeaderVersion = htole16(MPI_HEADER_VERSION); 1537155521Smjacob init.ReplyFrameSize = htole16(MPT_REPLY_SIZE); 1538147883Sscottl init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1539101704Smjacob 1540101704Smjacob if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) { 1541101704Smjacob return(error); 1542101704Smjacob } 1543101704Smjacob 1544101704Smjacob error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply); 1545101704Smjacob return (error); 1546101704Smjacob} 1547101704Smjacob 1548102199Smjacob 1549102199Smjacob/* 1550102199Smjacob * Utiltity routine to read configuration headers and pages 1551102199Smjacob */ 1552147883Sscottlint 1553170252Sscottlmpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params, 1554170252Sscottl bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms) 1555101704Smjacob{ 1556102199Smjacob MSG_CONFIG *cfgp; 1557147883Sscottl SGE_SIMPLE32 *se; 1558102199Smjacob 1559102199Smjacob cfgp = req->req_vbuf; 1560147883Sscottl memset(cfgp, 0, sizeof *cfgp); 1561170252Sscottl cfgp->Action = params->Action; 1562102199Smjacob cfgp->Function = MPI_FUNCTION_CONFIG; 1563170252Sscottl cfgp->Header.PageVersion = params->PageVersion; 1564170252Sscottl cfgp->Header.PageNumber = params->PageNumber; 1565170252Sscottl cfgp->PageAddress = htole32(params->PageAddress); 1566170252Sscottl if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) == 1567170252Sscottl MPI_CONFIG_PAGETYPE_EXTENDED) { 1568170252Sscottl cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED; 1569170252Sscottl cfgp->Header.PageLength = 0; 1570170252Sscottl cfgp->ExtPageLength = htole16(params->ExtPageLength); 1571170252Sscottl cfgp->ExtPageType = params->ExtPageType; 1572170252Sscottl } else { 1573170252Sscottl cfgp->Header.PageType = params->PageType; 1574170252Sscottl cfgp->Header.PageLength = params->PageLength; 1575170252Sscottl } 1576147883Sscottl se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE; 1577164315Sjb se->Address = htole32(addr); 1578147883Sscottl MPI_pSGE_SET_LENGTH(se, len); 1579147883Sscottl MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT | 1580147883Sscottl MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER | 1581147883Sscottl MPI_SGE_FLAGS_END_OF_LIST | 1582170252Sscottl ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT 1583170252Sscottl || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM) 1584147883Sscottl ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST))); 1585164315Sjb se->FlagsLength = htole32(se->FlagsLength); 1586147883Sscottl cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 1587102199Smjacob 1588102199Smjacob mpt_check_doorbell(mpt); 1589102199Smjacob mpt_send_cmd(mpt, req); 1590147883Sscottl return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 1591147883Sscottl sleep_ok, timeout_ms)); 1592147883Sscottl} 1593102199Smjacob 1594170252Sscottlint 1595170252Sscottlmpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber, 1596170252Sscottl uint32_t PageAddress, int ExtPageType, 1597170252Sscottl CONFIG_EXTENDED_PAGE_HEADER *rslt, 1598170252Sscottl int sleep_ok, int timeout_ms) 1599170252Sscottl{ 1600170252Sscottl request_t *req; 1601170252Sscottl cfgparms_t params; 1602170252Sscottl MSG_CONFIG_REPLY *cfgp; 1603170252Sscottl int error; 1604147883Sscottl 1605170252Sscottl req = mpt_get_request(mpt, sleep_ok); 1606170252Sscottl if (req == NULL) { 1607170252Sscottl mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n"); 1608170252Sscottl return (ENOMEM); 1609170252Sscottl } 1610170252Sscottl 1611170252Sscottl params.Action = MPI_CONFIG_ACTION_PAGE_HEADER; 1612170252Sscottl params.PageVersion = PageVersion; 1613170252Sscottl params.PageLength = 0; 1614170252Sscottl params.PageNumber = PageNumber; 1615170252Sscottl params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED; 1616170252Sscottl params.PageAddress = PageAddress; 1617170252Sscottl params.ExtPageType = ExtPageType; 1618170252Sscottl params.ExtPageLength = 0; 1619170252Sscottl error = mpt_issue_cfg_req(mpt, req, ¶ms, /*addr*/0, /*len*/0, 1620170252Sscottl sleep_ok, timeout_ms); 1621170252Sscottl if (error != 0) { 1622170252Sscottl /* 1623170252Sscottl * Leave the request. Without resetting the chip, it's 1624170252Sscottl * still owned by it and we'll just get into trouble 1625170252Sscottl * freeing it now. Mark it as abandoned so that if it 1626170252Sscottl * shows up later it can be freed. 1627170252Sscottl */ 1628170252Sscottl mpt_prt(mpt, "read_extcfg_header timed out\n"); 1629170252Sscottl return (ETIMEDOUT); 1630170252Sscottl } 1631170252Sscottl 1632170252Sscottl switch (req->IOCStatus & MPI_IOCSTATUS_MASK) { 1633170252Sscottl case MPI_IOCSTATUS_SUCCESS: 1634170252Sscottl cfgp = req->req_vbuf; 1635170252Sscottl rslt->PageVersion = cfgp->Header.PageVersion; 1636170252Sscottl rslt->PageNumber = cfgp->Header.PageNumber; 1637170252Sscottl rslt->PageType = cfgp->Header.PageType; 1638170252Sscottl rslt->ExtPageLength = cfgp->ExtPageLength; 1639170252Sscottl rslt->ExtPageType = cfgp->ExtPageType; 1640170252Sscottl error = 0; 1641170252Sscottl break; 1642170252Sscottl case MPI_IOCSTATUS_CONFIG_INVALID_PAGE: 1643170252Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, 1644170252Sscottl "Invalid Page Type %d Number %d Addr 0x%0x\n", 1645170252Sscottl MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress); 1646170252Sscottl error = EINVAL; 1647170252Sscottl break; 1648170252Sscottl default: 1649170252Sscottl mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n", 1650170252Sscottl req->IOCStatus); 1651170252Sscottl error = EIO; 1652170252Sscottl break; 1653170252Sscottl } 1654170252Sscottl mpt_free_request(mpt, req); 1655170252Sscottl return (error); 1656170252Sscottl} 1657170252Sscottl 1658147883Sscottlint 1659170252Sscottlmpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1660170252Sscottl CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len, 1661170252Sscottl int sleep_ok, int timeout_ms) 1662170252Sscottl{ 1663170252Sscottl request_t *req; 1664170252Sscottl cfgparms_t params; 1665170252Sscottl int error; 1666170252Sscottl 1667170252Sscottl req = mpt_get_request(mpt, sleep_ok); 1668170252Sscottl if (req == NULL) { 1669170252Sscottl mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n"); 1670170252Sscottl return (-1); 1671170252Sscottl } 1672170252Sscottl 1673170252Sscottl params.Action = Action; 1674170252Sscottl params.PageVersion = hdr->PageVersion; 1675170252Sscottl params.PageLength = 0; 1676170252Sscottl params.PageNumber = hdr->PageNumber; 1677170252Sscottl params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED; 1678170252Sscottl params.PageAddress = PageAddress; 1679170252Sscottl params.ExtPageType = hdr->ExtPageType; 1680170252Sscottl params.ExtPageLength = hdr->ExtPageLength; 1681170252Sscottl error = mpt_issue_cfg_req(mpt, req, ¶ms, 1682170252Sscottl req->req_pbuf + MPT_RQSL(mpt), 1683170252Sscottl len, sleep_ok, timeout_ms); 1684170252Sscottl if (error != 0) { 1685170252Sscottl mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action); 1686170252Sscottl return (-1); 1687170252Sscottl } 1688170252Sscottl 1689170252Sscottl if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1690170252Sscottl mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n", 1691170252Sscottl req->IOCStatus); 1692170252Sscottl mpt_free_request(mpt, req); 1693170252Sscottl return (-1); 1694170252Sscottl } 1695170252Sscottl bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1696170252Sscottl BUS_DMASYNC_POSTREAD); 1697170252Sscottl memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len); 1698170252Sscottl mpt_free_request(mpt, req); 1699170252Sscottl return (0); 1700170252Sscottl} 1701170252Sscottl 1702170252Sscottlint 1703147883Sscottlmpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber, 1704147883Sscottl uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt, 1705147883Sscottl int sleep_ok, int timeout_ms) 1706147883Sscottl{ 1707147883Sscottl request_t *req; 1708170252Sscottl cfgparms_t params; 1709156104Smjacob MSG_CONFIG *cfgp; 1710147883Sscottl int error; 1711147883Sscottl 1712147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1713147883Sscottl if (req == NULL) { 1714147883Sscottl mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n"); 1715156104Smjacob return (ENOMEM); 1716102199Smjacob } 1717147883Sscottl 1718170252Sscottl params.Action = MPI_CONFIG_ACTION_PAGE_HEADER; 1719170252Sscottl params.PageVersion = 0; 1720170252Sscottl params.PageLength = 0; 1721170252Sscottl params.PageNumber = PageNumber; 1722170252Sscottl params.PageType = PageType; 1723170252Sscottl params.PageAddress = PageAddress; 1724170252Sscottl error = mpt_issue_cfg_req(mpt, req, ¶ms, /*addr*/0, /*len*/0, 1725147883Sscottl sleep_ok, timeout_ms); 1726147883Sscottl if (error != 0) { 1727160396Smjacob /* 1728160396Smjacob * Leave the request. Without resetting the chip, it's 1729160396Smjacob * still owned by it and we'll just get into trouble 1730160396Smjacob * freeing it now. Mark it as abandoned so that if it 1731160396Smjacob * shows up later it can be freed. 1732160396Smjacob */ 1733147883Sscottl mpt_prt(mpt, "read_cfg_header timed out\n"); 1734156104Smjacob return (ETIMEDOUT); 1735147883Sscottl } 1736147883Sscottl 1737156104Smjacob switch (req->IOCStatus & MPI_IOCSTATUS_MASK) { 1738156104Smjacob case MPI_IOCSTATUS_SUCCESS: 1739147883Sscottl cfgp = req->req_vbuf; 1740147883Sscottl bcopy(&cfgp->Header, rslt, sizeof(*rslt)); 1741147883Sscottl error = 0; 1742156104Smjacob break; 1743156104Smjacob case MPI_IOCSTATUS_CONFIG_INVALID_PAGE: 1744156104Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 1745156104Smjacob "Invalid Page Type %d Number %d Addr 0x%0x\n", 1746156104Smjacob PageType, PageNumber, PageAddress); 1747156104Smjacob error = EINVAL; 1748156104Smjacob break; 1749156104Smjacob default: 1750156104Smjacob mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n", 1751156104Smjacob req->IOCStatus); 1752156104Smjacob error = EIO; 1753156104Smjacob break; 1754147883Sscottl } 1755102199Smjacob mpt_free_request(mpt, req); 1756147883Sscottl return (error); 1757102199Smjacob} 1758102199Smjacob 1759102822Smjacobint 1760147883Sscottlmpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1761147883Sscottl CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1762147883Sscottl int timeout_ms) 1763102199Smjacob{ 1764147883Sscottl request_t *req; 1765170252Sscottl cfgparms_t params; 1766147883Sscottl int error; 1767102199Smjacob 1768147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1769147883Sscottl if (req == NULL) { 1770147883Sscottl mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n"); 1771147883Sscottl return (-1); 1772147883Sscottl } 1773102199Smjacob 1774170252Sscottl params.Action = Action; 1775170252Sscottl params.PageVersion = hdr->PageVersion; 1776170252Sscottl params.PageLength = hdr->PageLength; 1777170252Sscottl params.PageNumber = hdr->PageNumber; 1778170252Sscottl params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK; 1779170252Sscottl params.PageAddress = PageAddress; 1780170252Sscottl error = mpt_issue_cfg_req(mpt, req, ¶ms, 1781170252Sscottl req->req_pbuf + MPT_RQSL(mpt), 1782147883Sscottl len, sleep_ok, timeout_ms); 1783147883Sscottl if (error != 0) { 1784147883Sscottl mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action); 1785147883Sscottl return (-1); 1786147883Sscottl } 1787102199Smjacob 1788147883Sscottl if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1789147883Sscottl mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n", 1790147883Sscottl req->IOCStatus); 1791147883Sscottl mpt_free_request(mpt, req); 1792102199Smjacob return (-1); 1793102199Smjacob } 1794102199Smjacob bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1795102199Smjacob BUS_DMASYNC_POSTREAD); 1796157117Smjacob memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len); 1797102199Smjacob mpt_free_request(mpt, req); 1798102199Smjacob return (0); 1799102199Smjacob} 1800102199Smjacob 1801102822Smjacobint 1802147883Sscottlmpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1803147883Sscottl CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1804147883Sscottl int timeout_ms) 1805102199Smjacob{ 1806147883Sscottl request_t *req; 1807170252Sscottl cfgparms_t params; 1808147883Sscottl u_int hdr_attr; 1809147883Sscottl int error; 1810102199Smjacob 1811102199Smjacob hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK; 1812102199Smjacob if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE && 1813102199Smjacob hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) { 1814147883Sscottl mpt_prt(mpt, "page type 0x%x not changeable\n", 1815147883Sscottl hdr->PageType & MPI_CONFIG_PAGETYPE_MASK); 1816102199Smjacob return (-1); 1817102199Smjacob } 1818102199Smjacob 1819160290Smjacob#if 0 1820160290Smjacob /* 1821160290Smjacob * We shouldn't mask off other bits here. 1822160290Smjacob */ 1823160290Smjacob hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK; 1824160290Smjacob#endif 1825160290Smjacob 1826147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1827147883Sscottl if (req == NULL) 1828147883Sscottl return (-1); 1829102199Smjacob 1830160290Smjacob memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len); 1831160290Smjacob 1832160290Smjacob /* 1833160290Smjacob * There isn't any point in restoring stripped out attributes 1834160290Smjacob * if you then mask them going down to issue the request. 1835160290Smjacob */ 1836160290Smjacob 1837170252Sscottl params.Action = Action; 1838170252Sscottl params.PageVersion = hdr->PageVersion; 1839170252Sscottl params.PageLength = hdr->PageLength; 1840170252Sscottl params.PageNumber = hdr->PageNumber; 1841170252Sscottl params.PageAddress = PageAddress; 1842160290Smjacob#if 0 1843147883Sscottl /* Restore stripped out attributes */ 1844147883Sscottl hdr->PageType |= hdr_attr; 1845170252Sscottl params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK; 1846160290Smjacob#else 1847170252Sscottl params.PageType = hdr->PageType; 1848170252Sscottl#endif 1849170252Sscottl error = mpt_issue_cfg_req(mpt, req, ¶ms, 1850160290Smjacob req->req_pbuf + MPT_RQSL(mpt), 1851160290Smjacob len, sleep_ok, timeout_ms); 1852147883Sscottl if (error != 0) { 1853147883Sscottl mpt_prt(mpt, "mpt_write_cfg_page timed out\n"); 1854147883Sscottl return (-1); 1855147883Sscottl } 1856102199Smjacob 1857147883Sscottl if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1858147883Sscottl mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n", 1859147883Sscottl req->IOCStatus); 1860147883Sscottl mpt_free_request(mpt, req); 1861147883Sscottl return (-1); 1862102199Smjacob } 1863147883Sscottl mpt_free_request(mpt, req); 1864147883Sscottl return (0); 1865147883Sscottl} 1866102199Smjacob 1867147883Sscottl/* 1868147883Sscottl * Read IOC configuration information 1869147883Sscottl */ 1870147883Sscottlstatic int 1871147883Sscottlmpt_read_config_info_ioc(struct mpt_softc *mpt) 1872147883Sscottl{ 1873147883Sscottl CONFIG_PAGE_HEADER hdr; 1874147883Sscottl struct mpt_raid_volume *mpt_raid; 1875147883Sscottl int rv; 1876147883Sscottl int i; 1877147883Sscottl size_t len; 1878147883Sscottl 1879147883Sscottl rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 1880158982Smjacob 2, 0, &hdr, FALSE, 5000); 1881156104Smjacob /* 1882156104Smjacob * If it's an invalid page, so what? Not a supported function.... 1883156104Smjacob */ 1884158982Smjacob if (rv == EINVAL) { 1885156104Smjacob return (0); 1886158982Smjacob } 1887158982Smjacob if (rv) { 1888156104Smjacob return (rv); 1889158982Smjacob } 1890147883Sscottl 1891164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 1892164990Smjacob "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n", 1893164990Smjacob hdr.PageVersion, hdr.PageLength << 2, 1894164990Smjacob hdr.PageNumber, hdr.PageType); 1895147883Sscottl 1896147883Sscottl len = hdr.PageLength * sizeof(uint32_t); 1897151075Sscottl mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1898158982Smjacob if (mpt->ioc_page2 == NULL) { 1899158982Smjacob mpt_prt(mpt, "unable to allocate memory for IOC page 2\n"); 1900158982Smjacob mpt_raid_free_mem(mpt); 1901147883Sscottl return (ENOMEM); 1902158982Smjacob } 1903147883Sscottl memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr)); 1904158982Smjacob rv = mpt_read_cur_cfg_page(mpt, 0, 1905158982Smjacob &mpt->ioc_page2->Header, len, FALSE, 5000); 1906147883Sscottl if (rv) { 1907147883Sscottl mpt_prt(mpt, "failed to read IOC Page 2\n"); 1908158982Smjacob mpt_raid_free_mem(mpt); 1909158982Smjacob return (EIO); 1910158982Smjacob } 1911164990Smjacob mpt2host_config_page_ioc2(mpt->ioc_page2); 1912158982Smjacob 1913158982Smjacob if (mpt->ioc_page2->CapabilitiesFlags != 0) { 1914147883Sscottl uint32_t mask; 1915147883Sscottl 1916147883Sscottl mpt_prt(mpt, "Capabilities: ("); 1917147883Sscottl for (mask = 1; mask != 0; mask <<= 1) { 1918158982Smjacob if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) { 1919147883Sscottl continue; 1920158982Smjacob } 1921147883Sscottl switch (mask) { 1922147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT: 1923147883Sscottl mpt_prtc(mpt, " RAID-0"); 1924147883Sscottl break; 1925147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT: 1926147883Sscottl mpt_prtc(mpt, " RAID-1E"); 1927147883Sscottl break; 1928147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT: 1929147883Sscottl mpt_prtc(mpt, " RAID-1"); 1930147883Sscottl break; 1931147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT: 1932147883Sscottl mpt_prtc(mpt, " SES"); 1933147883Sscottl break; 1934147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT: 1935147883Sscottl mpt_prtc(mpt, " SAFTE"); 1936147883Sscottl break; 1937147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT: 1938147883Sscottl mpt_prtc(mpt, " Multi-Channel-Arrays"); 1939147883Sscottl default: 1940147883Sscottl break; 1941147883Sscottl } 1942102199Smjacob } 1943147883Sscottl mpt_prtc(mpt, " )\n"); 1944147883Sscottl if ((mpt->ioc_page2->CapabilitiesFlags 1945147883Sscottl & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT 1946147883Sscottl | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT 1947147883Sscottl | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) { 1948147883Sscottl mpt_prt(mpt, "%d Active Volume%s(%d Max)\n", 1949147883Sscottl mpt->ioc_page2->NumActiveVolumes, 1950147883Sscottl mpt->ioc_page2->NumActiveVolumes != 1 1951147883Sscottl ? "s " : " ", 1952147883Sscottl mpt->ioc_page2->MaxVolumes); 1953147883Sscottl mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n", 1954147883Sscottl mpt->ioc_page2->NumActivePhysDisks, 1955147883Sscottl mpt->ioc_page2->NumActivePhysDisks != 1 1956147883Sscottl ? "s " : " ", 1957147883Sscottl mpt->ioc_page2->MaxPhysDisks); 1958147883Sscottl } 1959147883Sscottl } 1960102199Smjacob 1961147883Sscottl len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume); 1962158982Smjacob mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1963147883Sscottl if (mpt->raid_volumes == NULL) { 1964147883Sscottl mpt_prt(mpt, "Could not allocate RAID volume data\n"); 1965158982Smjacob mpt_raid_free_mem(mpt); 1966158982Smjacob return (ENOMEM); 1967147883Sscottl } 1968147883Sscottl 1969147883Sscottl /* 1970147883Sscottl * Copy critical data out of ioc_page2 so that we can 1971147883Sscottl * safely refresh the page without windows of unreliable 1972147883Sscottl * data. 1973147883Sscottl */ 1974147883Sscottl mpt->raid_max_volumes = mpt->ioc_page2->MaxVolumes; 1975147883Sscottl 1976158982Smjacob len = sizeof(*mpt->raid_volumes->config_page) + 1977158982Smjacob (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1)); 1978147883Sscottl for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) { 1979147883Sscottl mpt_raid = &mpt->raid_volumes[i]; 1980158982Smjacob mpt_raid->config_page = 1981158982Smjacob malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1982147883Sscottl if (mpt_raid->config_page == NULL) { 1983147883Sscottl mpt_prt(mpt, "Could not allocate RAID page data\n"); 1984158982Smjacob mpt_raid_free_mem(mpt); 1985158982Smjacob return (ENOMEM); 1986147883Sscottl } 1987147883Sscottl } 1988147883Sscottl mpt->raid_page0_len = len; 1989147883Sscottl 1990147883Sscottl len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk); 1991158982Smjacob mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1992147883Sscottl if (mpt->raid_disks == NULL) { 1993147883Sscottl mpt_prt(mpt, "Could not allocate RAID disk data\n"); 1994158982Smjacob mpt_raid_free_mem(mpt); 1995158982Smjacob return (ENOMEM); 1996147883Sscottl } 1997147883Sscottl mpt->raid_max_disks = mpt->ioc_page2->MaxPhysDisks; 1998147883Sscottl 1999158982Smjacob /* 2000158982Smjacob * Load page 3. 2001158982Smjacob */ 2002147883Sscottl rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 2003158982Smjacob 3, 0, &hdr, FALSE, 5000); 2004158982Smjacob if (rv) { 2005158982Smjacob mpt_raid_free_mem(mpt); 2006147883Sscottl return (EIO); 2007158982Smjacob } 2008147883Sscottl 2009147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n", 2010158982Smjacob hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType); 2011147883Sscottl 2012147883Sscottl len = hdr.PageLength * sizeof(uint32_t); 2013151075Sscottl mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 2014158982Smjacob if (mpt->ioc_page3 == NULL) { 2015158982Smjacob mpt_prt(mpt, "unable to allocate memory for IOC page 3\n"); 2016158982Smjacob mpt_raid_free_mem(mpt); 2017158982Smjacob return (ENOMEM); 2018158982Smjacob } 2019147883Sscottl memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr)); 2020158982Smjacob rv = mpt_read_cur_cfg_page(mpt, 0, 2021158982Smjacob &mpt->ioc_page3->Header, len, FALSE, 5000); 2022147883Sscottl if (rv) { 2023158982Smjacob mpt_raid_free_mem(mpt); 2024158982Smjacob return (EIO); 2025102199Smjacob } 2026147883Sscottl mpt_raid_wakeup(mpt); 2027102199Smjacob return (0); 2028102199Smjacob} 2029102199Smjacob 2030102199Smjacob/* 2031102199Smjacob * Enable IOC port 2032102199Smjacob */ 2033102199Smjacobstatic int 2034147883Sscottlmpt_send_port_enable(struct mpt_softc *mpt, int port) 2035102199Smjacob{ 2036147883Sscottl request_t *req; 2037101704Smjacob MSG_PORT_ENABLE *enable_req; 2038147883Sscottl int error; 2039101704Smjacob 2040147883Sscottl req = mpt_get_request(mpt, /*sleep_ok*/FALSE); 2041147883Sscottl if (req == NULL) 2042147883Sscottl return (-1); 2043101704Smjacob 2044101704Smjacob enable_req = req->req_vbuf; 2045157354Smjacob memset(enable_req, 0, MPT_RQSL(mpt)); 2046101704Smjacob 2047101704Smjacob enable_req->Function = MPI_FUNCTION_PORT_ENABLE; 2048147883Sscottl enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 2049101704Smjacob enable_req->PortNumber = port; 2050101704Smjacob 2051101704Smjacob mpt_check_doorbell(mpt); 2052147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port); 2053147883Sscottl 2054147883Sscottl mpt_send_cmd(mpt, req); 2055147883Sscottl error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 2056157662Smjacob FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000); 2057147883Sscottl if (error != 0) { 2058157117Smjacob mpt_prt(mpt, "port %d enable timed out\n", port); 2059147883Sscottl return (-1); 2060101704Smjacob } 2061101704Smjacob mpt_free_request(mpt, req); 2062157117Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port); 2063101704Smjacob return (0); 2064101704Smjacob} 2065101704Smjacob 2066101704Smjacob/* 2067101704Smjacob * Enable/Disable asynchronous event reporting. 2068101704Smjacob */ 2069101704Smjacobstatic int 2070147883Sscottlmpt_send_event_request(struct mpt_softc *mpt, int onoff) 2071101704Smjacob{ 2072101704Smjacob request_t *req; 2073101704Smjacob MSG_EVENT_NOTIFY *enable_req; 2074101704Smjacob 2075157354Smjacob req = mpt_get_request(mpt, FALSE); 2076157354Smjacob if (req == NULL) { 2077157354Smjacob return (ENOMEM); 2078157354Smjacob } 2079101704Smjacob enable_req = req->req_vbuf; 2080157354Smjacob memset(enable_req, 0, sizeof *enable_req); 2081101704Smjacob 2082101704Smjacob enable_req->Function = MPI_FUNCTION_EVENT_NOTIFICATION; 2083147883Sscottl enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS); 2084101704Smjacob enable_req->Switch = onoff; 2085101704Smjacob 2086101704Smjacob mpt_check_doorbell(mpt); 2087157354Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n", 2088157354Smjacob onoff ? "en" : "dis"); 2089157354Smjacob /* 2090157354Smjacob * Send the command off, but don't wait for it. 2091157354Smjacob */ 2092101704Smjacob mpt_send_cmd(mpt, req); 2093101704Smjacob return (0); 2094101704Smjacob} 2095101704Smjacob 2096101704Smjacob/* 2097101704Smjacob * Un-mask the interupts on the chip. 2098101704Smjacob */ 2099101704Smjacobvoid 2100147883Sscottlmpt_enable_ints(struct mpt_softc *mpt) 2101101704Smjacob{ 2102101704Smjacob /* Unmask every thing except door bell int */ 2103101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK); 2104101704Smjacob} 2105101704Smjacob 2106101704Smjacob/* 2107101704Smjacob * Mask the interupts on the chip. 2108101704Smjacob */ 2109101704Smjacobvoid 2110147883Sscottlmpt_disable_ints(struct mpt_softc *mpt) 2111101704Smjacob{ 2112101704Smjacob /* Mask all interrupts */ 2113156104Smjacob mpt_write(mpt, MPT_OFFSET_INTR_MASK, 2114101704Smjacob MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK); 2115101704Smjacob} 2116101704Smjacob 2117147883Sscottlstatic void 2118147883Sscottlmpt_sysctl_attach(struct mpt_softc *mpt) 2119147883Sscottl{ 2120157117Smjacob#if __FreeBSD_version >= 500000 2121147883Sscottl struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev); 2122147883Sscottl struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev); 2123147883Sscottl 2124156104Smjacob SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 2125147883Sscottl "debug", CTLFLAG_RW, &mpt->verbose, 0, 2126147883Sscottl "Debugging/Verbose level"); 2127160290Smjacob SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 2128160290Smjacob "role", CTLFLAG_RD, &mpt->role, 0, 2129160290Smjacob "HBA role"); 2130165814Smjacob#ifdef MPT_TEST_MULTIPATH 2131165814Smjacob SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 2132165814Smjacob "failure_id", CTLFLAG_RW, &mpt->failure_id, -1, 2133165814Smjacob "Next Target to Fail"); 2134157117Smjacob#endif 2135165814Smjacob#endif 2136147883Sscottl} 2137147883Sscottl 2138101704Smjacobint 2139147883Sscottlmpt_attach(struct mpt_softc *mpt) 2140101704Smjacob{ 2141157117Smjacob struct mpt_personality *pers; 2142147883Sscottl int i; 2143157117Smjacob int error; 2144147883Sscottl 2145162133Smjacob TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links); 2146147883Sscottl for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 2147147883Sscottl pers = mpt_personalities[i]; 2148157117Smjacob if (pers == NULL) { 2149147883Sscottl continue; 2150157117Smjacob } 2151147883Sscottl if (pers->probe(mpt) == 0) { 2152147883Sscottl error = pers->attach(mpt); 2153147883Sscottl if (error != 0) { 2154147883Sscottl mpt_detach(mpt); 2155147883Sscottl return (error); 2156147883Sscottl } 2157147883Sscottl mpt->mpt_pers_mask |= (0x1 << pers->id); 2158147883Sscottl pers->use_count++; 2159147883Sscottl } 2160147883Sscottl } 2161155521Smjacob 2162157117Smjacob /* 2163157117Smjacob * Now that we've attached everything, do the enable function 2164157117Smjacob * for all of the personalities. This allows the personalities 2165157117Smjacob * to do setups that are appropriate for them prior to enabling 2166157117Smjacob * any ports. 2167157117Smjacob */ 2168157117Smjacob for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 2169157117Smjacob pers = mpt_personalities[i]; 2170157117Smjacob if (pers != NULL && MPT_PERS_ATTACHED(pers, mpt) != 0) { 2171157117Smjacob error = pers->enable(mpt); 2172157117Smjacob if (error != 0) { 2173157117Smjacob mpt_prt(mpt, "personality %s attached but would" 2174157117Smjacob " not enable (%d)\n", pers->name, error); 2175157117Smjacob mpt_detach(mpt); 2176157117Smjacob return (error); 2177157117Smjacob } 2178157117Smjacob } 2179157117Smjacob } 2180147883Sscottl return (0); 2181147883Sscottl} 2182147883Sscottl 2183147883Sscottlint 2184147883Sscottlmpt_shutdown(struct mpt_softc *mpt) 2185147883Sscottl{ 2186147883Sscottl struct mpt_personality *pers; 2187147883Sscottl 2188157117Smjacob MPT_PERS_FOREACH_REVERSE(mpt, pers) { 2189147883Sscottl pers->shutdown(mpt); 2190157117Smjacob } 2191147883Sscottl return (0); 2192147883Sscottl} 2193147883Sscottl 2194147883Sscottlint 2195147883Sscottlmpt_detach(struct mpt_softc *mpt) 2196147883Sscottl{ 2197147883Sscottl struct mpt_personality *pers; 2198147883Sscottl 2199147883Sscottl MPT_PERS_FOREACH_REVERSE(mpt, pers) { 2200147883Sscottl pers->detach(mpt); 2201147883Sscottl mpt->mpt_pers_mask &= ~(0x1 << pers->id); 2202147883Sscottl pers->use_count--; 2203147883Sscottl } 2204162133Smjacob TAILQ_REMOVE(&mpt_tailq, mpt, links); 2205147883Sscottl return (0); 2206147883Sscottl} 2207147883Sscottl 2208147883Sscottlint 2209147883Sscottlmpt_core_load(struct mpt_personality *pers) 2210147883Sscottl{ 2211147883Sscottl int i; 2212147883Sscottl 2213147883Sscottl /* 2214147883Sscottl * Setup core handlers and insert the default handler 2215147883Sscottl * into all "empty slots". 2216147883Sscottl */ 2217157117Smjacob for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) { 2218147883Sscottl mpt_reply_handlers[i] = mpt_default_reply_handler; 2219157117Smjacob } 2220147883Sscottl 2221147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] = 2222147883Sscottl mpt_event_reply_handler; 2223147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] = 2224147883Sscottl mpt_config_reply_handler; 2225147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] = 2226147883Sscottl mpt_handshake_reply_handler; 2227147883Sscottl return (0); 2228147883Sscottl} 2229147883Sscottl 2230147883Sscottl/* 2231147883Sscottl * Initialize per-instance driver data and perform 2232147883Sscottl * initial controller configuration. 2233147883Sscottl */ 2234147883Sscottlint 2235147883Sscottlmpt_core_attach(struct mpt_softc *mpt) 2236147883Sscottl{ 2237169293Smjacob int val, error; 2238101704Smjacob 2239147883Sscottl LIST_INIT(&mpt->ack_frames); 2240147883Sscottl /* Put all request buffers on the free list */ 2241147883Sscottl TAILQ_INIT(&mpt->request_pending_list); 2242147883Sscottl TAILQ_INIT(&mpt->request_free_list); 2243157354Smjacob TAILQ_INIT(&mpt->request_timeout_list); 2244169293Smjacob MPT_LOCK(mpt); 2245157117Smjacob for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) { 2246157354Smjacob request_t *req = &mpt->request_pool[val]; 2247157354Smjacob req->state = REQ_STATE_ALLOCATED; 2248157354Smjacob mpt_free_request(mpt, req); 2249157117Smjacob } 2250169293Smjacob MPT_UNLOCK(mpt); 2251157117Smjacob for (val = 0; val < MPT_MAX_LUNS; val++) { 2252157117Smjacob STAILQ_INIT(&mpt->trt[val].atios); 2253157117Smjacob STAILQ_INIT(&mpt->trt[val].inots); 2254157117Smjacob } 2255157117Smjacob STAILQ_INIT(&mpt->trt_wildcard.atios); 2256157117Smjacob STAILQ_INIT(&mpt->trt_wildcard.inots); 2257165814Smjacob#ifdef MPT_TEST_MULTIPATH 2258165814Smjacob mpt->failure_id = -1; 2259165814Smjacob#endif 2260157117Smjacob mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE; 2261147883Sscottl mpt_sysctl_attach(mpt); 2262147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n", 2263157117Smjacob mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL))); 2264169293Smjacob 2265169293Smjacob MPT_LOCK(mpt); 2266169293Smjacob error = mpt_configure_ioc(mpt, 0, 0); 2267169293Smjacob MPT_UNLOCK(mpt); 2268169293Smjacob 2269169293Smjacob return (error); 2270147883Sscottl} 2271147883Sscottl 2272157117Smjacobint 2273157117Smjacobmpt_core_enable(struct mpt_softc *mpt) 2274157117Smjacob{ 2275157117Smjacob /* 2276157117Smjacob * We enter with the IOC enabled, but async events 2277157117Smjacob * not enabled, ports not enabled and interrupts 2278157117Smjacob * not enabled. 2279157117Smjacob */ 2280169293Smjacob MPT_LOCK(mpt); 2281157117Smjacob 2282157117Smjacob /* 2283157117Smjacob * Enable asynchronous event reporting- all personalities 2284157117Smjacob * have attached so that they should be able to now field 2285157117Smjacob * async events. 2286157117Smjacob */ 2287157117Smjacob mpt_send_event_request(mpt, 1); 2288157117Smjacob 2289157117Smjacob /* 2290157117Smjacob * Catch any pending interrupts 2291157117Smjacob * 2292157117Smjacob * This seems to be crucial- otherwise 2293157117Smjacob * the portenable below times out. 2294157117Smjacob */ 2295157117Smjacob mpt_intr(mpt); 2296157117Smjacob 2297157117Smjacob /* 2298157117Smjacob * Enable Interrupts 2299157117Smjacob */ 2300157117Smjacob mpt_enable_ints(mpt); 2301157117Smjacob 2302157117Smjacob /* 2303157117Smjacob * Catch any pending interrupts 2304157117Smjacob * 2305157117Smjacob * This seems to be crucial- otherwise 2306157117Smjacob * the portenable below times out. 2307157117Smjacob */ 2308157117Smjacob mpt_intr(mpt); 2309157117Smjacob 2310157117Smjacob /* 2311157662Smjacob * Enable the port. 2312157117Smjacob */ 2313157117Smjacob if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2314157117Smjacob mpt_prt(mpt, "failed to enable port 0\n"); 2315169293Smjacob MPT_UNLOCK(mpt); 2316157117Smjacob return (ENXIO); 2317157117Smjacob } 2318169293Smjacob MPT_UNLOCK(mpt); 2319157117Smjacob return (0); 2320157117Smjacob} 2321157117Smjacob 2322147883Sscottlvoid 2323147883Sscottlmpt_core_shutdown(struct mpt_softc *mpt) 2324147883Sscottl{ 2325157117Smjacob mpt_disable_ints(mpt); 2326147883Sscottl} 2327147883Sscottl 2328147883Sscottlvoid 2329147883Sscottlmpt_core_detach(struct mpt_softc *mpt) 2330147883Sscottl{ 2331164990Smjacob /* 2332164990Smjacob * XXX: FREE MEMORY 2333164990Smjacob */ 2334157117Smjacob mpt_disable_ints(mpt); 2335147883Sscottl} 2336147883Sscottl 2337147883Sscottlint 2338147883Sscottlmpt_core_unload(struct mpt_personality *pers) 2339147883Sscottl{ 2340147883Sscottl /* Unload is always successfull. */ 2341147883Sscottl return (0); 2342147883Sscottl} 2343147883Sscottl 2344147883Sscottl#define FW_UPLOAD_REQ_SIZE \ 2345147883Sscottl (sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION) \ 2346147883Sscottl + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32)) 2347147883Sscottl 2348147883Sscottlstatic int 2349147883Sscottlmpt_upload_fw(struct mpt_softc *mpt) 2350147883Sscottl{ 2351147883Sscottl uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE]; 2352147883Sscottl MSG_FW_UPLOAD_REPLY fw_reply; 2353147883Sscottl MSG_FW_UPLOAD *fw_req; 2354147883Sscottl FW_UPLOAD_TCSGE *tsge; 2355147883Sscottl SGE_SIMPLE32 *sge; 2356147883Sscottl uint32_t flags; 2357147883Sscottl int error; 2358147883Sscottl 2359147883Sscottl memset(&fw_req_buf, 0, sizeof(fw_req_buf)); 2360147883Sscottl fw_req = (MSG_FW_UPLOAD *)fw_req_buf; 2361147883Sscottl fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM; 2362147883Sscottl fw_req->Function = MPI_FUNCTION_FW_UPLOAD; 2363147883Sscottl fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 2364147883Sscottl tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL; 2365147883Sscottl tsge->DetailsLength = 12; 2366147883Sscottl tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT; 2367147883Sscottl tsge->ImageSize = htole32(mpt->fw_image_size); 2368147883Sscottl sge = (SGE_SIMPLE32 *)(tsge + 1); 2369147883Sscottl flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER 2370147883Sscottl | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT 2371147883Sscottl | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST); 2372147883Sscottl flags <<= MPI_SGE_FLAGS_SHIFT; 2373147883Sscottl sge->FlagsLength = htole32(flags | mpt->fw_image_size); 2374147883Sscottl sge->Address = htole32(mpt->fw_phys); 2375147883Sscottl error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf); 2376147883Sscottl if (error) 2377147883Sscottl return(error); 2378147883Sscottl error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply); 2379147883Sscottl return (error); 2380147883Sscottl} 2381147883Sscottl 2382147883Sscottlstatic void 2383147883Sscottlmpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr, 2384147883Sscottl uint32_t *data, bus_size_t len) 2385147883Sscottl{ 2386147883Sscottl uint32_t *data_end; 2387147883Sscottl 2388147883Sscottl data_end = data + (roundup2(len, sizeof(uint32_t)) / 4); 2389159919Smjacob if (mpt->is_sas) { 2390159919Smjacob pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2391159919Smjacob } 2392147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr); 2393147883Sscottl while (data != data_end) { 2394147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data); 2395147883Sscottl data++; 2396101704Smjacob } 2397159919Smjacob if (mpt->is_sas) { 2398159919Smjacob pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2399159919Smjacob } 2400147883Sscottl} 2401101704Smjacob 2402147883Sscottlstatic int 2403147883Sscottlmpt_download_fw(struct mpt_softc *mpt) 2404147883Sscottl{ 2405147883Sscottl MpiFwHeader_t *fw_hdr; 2406147883Sscottl int error; 2407147883Sscottl uint32_t ext_offset; 2408147883Sscottl uint32_t data; 2409147883Sscottl 2410147883Sscottl mpt_prt(mpt, "Downloading Firmware - Image Size %d\n", 2411147883Sscottl mpt->fw_image_size); 2412147883Sscottl 2413147883Sscottl error = mpt_enable_diag_mode(mpt); 2414147883Sscottl if (error != 0) { 2415147883Sscottl mpt_prt(mpt, "Could not enter diagnostic mode!\n"); 2416147883Sscottl return (EIO); 2417101704Smjacob } 2418101704Smjacob 2419147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, 2420147883Sscottl MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM); 2421147883Sscottl 2422147883Sscottl fw_hdr = (MpiFwHeader_t *)mpt->fw_image; 2423147883Sscottl mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr, 2424147883Sscottl fw_hdr->ImageSize); 2425147883Sscottl 2426147883Sscottl ext_offset = fw_hdr->NextImageHeaderOffset; 2427147883Sscottl while (ext_offset != 0) { 2428147883Sscottl MpiExtImageHeader_t *ext; 2429147883Sscottl 2430147883Sscottl ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset); 2431147883Sscottl ext_offset = ext->NextImageHeaderOffset; 2432147883Sscottl 2433147883Sscottl mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext, 2434147883Sscottl ext->ImageSize); 2435147883Sscottl } 2436147883Sscottl 2437159919Smjacob if (mpt->is_sas) { 2438159919Smjacob pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2439159919Smjacob } 2440147883Sscottl /* Setup the address to jump to on reset. */ 2441147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr); 2442147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue); 2443147883Sscottl 2444101704Smjacob /* 2445147883Sscottl * The controller sets the "flash bad" status after attempting 2446147883Sscottl * to auto-boot from flash. Clear the status so that the controller 2447147883Sscottl * will continue the boot process with our newly installed firmware. 2448101704Smjacob */ 2449147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2450147883Sscottl data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL; 2451147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2452147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data); 2453147883Sscottl 2454159919Smjacob if (mpt->is_sas) { 2455159919Smjacob pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2456159919Smjacob } 2457155521Smjacob 2458147883Sscottl /* 2459147883Sscottl * Re-enable the processor and clear the boot halt flag. 2460147883Sscottl */ 2461147883Sscottl data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 2462147883Sscottl data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM); 2463147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data); 2464147883Sscottl 2465147883Sscottl mpt_disable_diag_mode(mpt); 2466147883Sscottl return (0); 2467147883Sscottl} 2468147883Sscottl 2469147883Sscottl/* 2470147883Sscottl * Allocate/Initialize data structures for the controller. Called 2471147883Sscottl * once at instance startup. 2472147883Sscottl */ 2473147883Sscottlstatic int 2474164990Smjacobmpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset) 2475147883Sscottl{ 2476164990Smjacob PTR_MSG_PORT_FACTS_REPLY pfp; 2477164990Smjacob int error, port; 2478164990Smjacob size_t len; 2479147883Sscottl 2480164990Smjacob if (tn == MPT_MAX_TRYS) { 2481164990Smjacob return (-1); 2482164990Smjacob } 2483147883Sscottl 2484164990Smjacob /* 2485164990Smjacob * No need to reset if the IOC is already in the READY state. 2486164990Smjacob * 2487164990Smjacob * Force reset if initialization failed previously. 2488164990Smjacob * Note that a hard_reset of the second channel of a '929 2489164990Smjacob * will stop operation of the first channel. Hopefully, if the 2490164990Smjacob * first channel is ok, the second will not require a hard 2491164990Smjacob * reset. 2492164990Smjacob */ 2493164990Smjacob if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) { 2494164990Smjacob if (mpt_reset(mpt, FALSE) != MPT_OK) { 2495164990Smjacob return (mpt_configure_ioc(mpt, tn++, 1)); 2496101704Smjacob } 2497147883Sscottl needreset = 0; 2498164990Smjacob } 2499101704Smjacob 2500164990Smjacob if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) { 2501164990Smjacob mpt_prt(mpt, "mpt_get_iocfacts failed\n"); 2502164990Smjacob return (mpt_configure_ioc(mpt, tn++, 1)); 2503164990Smjacob } 2504164990Smjacob mpt2host_iocfacts_reply(&mpt->ioc_facts); 2505102199Smjacob 2506164990Smjacob mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n", 2507164990Smjacob mpt->ioc_facts.MsgVersion >> 8, 2508164990Smjacob mpt->ioc_facts.MsgVersion & 0xFF, 2509164990Smjacob mpt->ioc_facts.HeaderVersion >> 8, 2510164990Smjacob mpt->ioc_facts.HeaderVersion & 0xFF); 2511155521Smjacob 2512164990Smjacob /* 2513164990Smjacob * Now that we know request frame size, we can calculate 2514164990Smjacob * the actual (reasonable) segment limit for read/write I/O. 2515164990Smjacob * 2516164990Smjacob * This limit is constrained by: 2517164990Smjacob * 2518164990Smjacob * + The size of each area we allocate per command (and how 2519164990Smjacob * many chain segments we can fit into it). 2520164990Smjacob * + The total number of areas we've set up. 2521164990Smjacob * + The actual chain depth the card will allow. 2522164990Smjacob * 2523164990Smjacob * The first area's segment count is limited by the I/O request 2524164990Smjacob * at the head of it. We cannot allocate realistically more 2525164990Smjacob * than MPT_MAX_REQUESTS areas. Therefore, to account for both 2526164990Smjacob * conditions, we'll just start out with MPT_MAX_REQUESTS-2. 2527164990Smjacob * 2528164990Smjacob */ 2529164990Smjacob /* total number of request areas we (can) allocate */ 2530164990Smjacob mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2; 2531155521Smjacob 2532164990Smjacob /* converted to the number of chain areas possible */ 2533164990Smjacob mpt->max_seg_cnt *= MPT_NRFM(mpt); 2534155521Smjacob 2535164990Smjacob /* limited by the number of chain areas the card will support */ 2536164990Smjacob if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) { 2537164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 2538164990Smjacob "chain depth limited to %u (from %u)\n", 2539164990Smjacob mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt); 2540164990Smjacob mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth; 2541164990Smjacob } 2542155521Smjacob 2543164990Smjacob /* converted to the number of simple sges in chain segments. */ 2544164990Smjacob mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1); 2545155521Smjacob 2546164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "Maximum Segment Count: %u\n", 2547164990Smjacob mpt->max_seg_cnt); 2548164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "MsgLength=%u IOCNumber = %d\n", 2549164990Smjacob mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber); 2550164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 2551164990Smjacob "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes " 2552164990Smjacob "Request Frame Size %u bytes Max Chain Depth %u\n", 2553164990Smjacob mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize, 2554164990Smjacob mpt->ioc_facts.RequestFrameSize << 2, 2555164990Smjacob mpt->ioc_facts.MaxChainDepth); 2556164990Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "IOCFACTS: Num Ports %d, FWImageSize %d, " 2557164990Smjacob "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts, 2558164990Smjacob mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags); 2559155521Smjacob 2560164990Smjacob len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY); 2561164990Smjacob mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 2562164990Smjacob if (mpt->port_facts == NULL) { 2563164990Smjacob mpt_prt(mpt, "unable to allocate memory for port facts\n"); 2564164990Smjacob return (ENOMEM); 2565164990Smjacob } 2566147883Sscottl 2567155521Smjacob 2568164990Smjacob if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) && 2569164990Smjacob (mpt->fw_uploaded == 0)) { 2570164990Smjacob struct mpt_map_info mi; 2571147883Sscottl 2572164990Smjacob /* 2573164990Smjacob * In some configurations, the IOC's firmware is 2574164990Smjacob * stored in a shared piece of system NVRAM that 2575164990Smjacob * is only accessable via the BIOS. In this 2576164990Smjacob * case, the firmware keeps a copy of firmware in 2577164990Smjacob * RAM until the OS driver retrieves it. Once 2578164990Smjacob * retrieved, we are responsible for re-downloading 2579164990Smjacob * the firmware after any hard-reset. 2580164990Smjacob */ 2581164990Smjacob mpt->fw_image_size = mpt->ioc_facts.FWImageSize; 2582164990Smjacob error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0, 2583164990Smjacob BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 2584164990Smjacob mpt->fw_image_size, 1, mpt->fw_image_size, 0, 2585164990Smjacob &mpt->fw_dmat); 2586164990Smjacob if (error != 0) { 2587164990Smjacob mpt_prt(mpt, "cannot create firmwarew dma tag\n"); 2588164990Smjacob return (ENOMEM); 2589101704Smjacob } 2590164990Smjacob error = bus_dmamem_alloc(mpt->fw_dmat, 2591164990Smjacob (void **)&mpt->fw_image, BUS_DMA_NOWAIT, &mpt->fw_dmap); 2592164990Smjacob if (error != 0) { 2593164990Smjacob mpt_prt(mpt, "cannot allocate firmware memory\n"); 2594164990Smjacob bus_dma_tag_destroy(mpt->fw_dmat); 2595164990Smjacob return (ENOMEM); 2596164990Smjacob } 2597164990Smjacob mi.mpt = mpt; 2598164990Smjacob mi.error = 0; 2599164990Smjacob bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap, 2600164990Smjacob mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0); 2601164990Smjacob mpt->fw_phys = mi.phys; 2602101704Smjacob 2603164990Smjacob error = mpt_upload_fw(mpt); 2604164990Smjacob if (error != 0) { 2605164990Smjacob mpt_prt(mpt, "firmware upload failed.\n"); 2606164990Smjacob bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap); 2607164990Smjacob bus_dmamem_free(mpt->fw_dmat, mpt->fw_image, 2608164990Smjacob mpt->fw_dmap); 2609164990Smjacob bus_dma_tag_destroy(mpt->fw_dmat); 2610164990Smjacob mpt->fw_image = NULL; 2611164990Smjacob return (EIO); 2612102199Smjacob } 2613164990Smjacob mpt->fw_uploaded = 1; 2614164990Smjacob } 2615102199Smjacob 2616164990Smjacob for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) { 2617164990Smjacob pfp = &mpt->port_facts[port]; 2618164990Smjacob error = mpt_get_portfacts(mpt, 0, pfp); 2619164990Smjacob if (error != MPT_OK) { 2620164990Smjacob mpt_prt(mpt, 2621164990Smjacob "mpt_get_portfacts on port %d failed\n", port); 2622164990Smjacob free(mpt->port_facts, M_DEVBUF); 2623164990Smjacob mpt->port_facts = NULL; 2624164990Smjacob return (mpt_configure_ioc(mpt, tn++, 1)); 2625102199Smjacob } 2626164990Smjacob mpt2host_portfacts_reply(pfp); 2627157117Smjacob 2628164990Smjacob if (port > 0) { 2629164990Smjacob error = MPT_PRT_INFO; 2630102199Smjacob } else { 2631164990Smjacob error = MPT_PRT_DEBUG; 2632102199Smjacob } 2633164990Smjacob mpt_lprt(mpt, error, 2634164990Smjacob "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n", 2635164990Smjacob port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID, 2636164990Smjacob pfp->MaxDevices); 2637102199Smjacob 2638164990Smjacob } 2639157117Smjacob 2640164990Smjacob /* 2641164990Smjacob * XXX: Not yet supporting more than port 0 2642164990Smjacob */ 2643164990Smjacob pfp = &mpt->port_facts[0]; 2644164990Smjacob if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) { 2645164990Smjacob mpt->is_fc = 1; 2646164990Smjacob mpt->is_sas = 0; 2647164990Smjacob mpt->is_spi = 0; 2648164990Smjacob } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) { 2649164990Smjacob mpt->is_fc = 0; 2650164990Smjacob mpt->is_sas = 1; 2651164990Smjacob mpt->is_spi = 0; 2652164990Smjacob } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) { 2653164990Smjacob mpt->is_fc = 0; 2654164990Smjacob mpt->is_sas = 0; 2655164990Smjacob mpt->is_spi = 1; 2656164990Smjacob } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) { 2657164990Smjacob mpt_prt(mpt, "iSCSI not supported yet\n"); 2658164990Smjacob return (ENXIO); 2659164990Smjacob } else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) { 2660164990Smjacob mpt_prt(mpt, "Inactive Port\n"); 2661164990Smjacob return (ENXIO); 2662164990Smjacob } else { 2663164990Smjacob mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType); 2664164990Smjacob return (ENXIO); 2665164990Smjacob } 2666101704Smjacob 2667164990Smjacob /* 2668164990Smjacob * Set our role with what this port supports. 2669164990Smjacob * 2670164990Smjacob * Note this might be changed later in different modules 2671164990Smjacob * if this is different from what is wanted. 2672164990Smjacob */ 2673164990Smjacob mpt->role = MPT_ROLE_NONE; 2674164990Smjacob if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) { 2675164990Smjacob mpt->role |= MPT_ROLE_INITIATOR; 2676101704Smjacob } 2677164990Smjacob if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) { 2678164990Smjacob mpt->role |= MPT_ROLE_TARGET; 2679164990Smjacob } 2680101704Smjacob 2681164990Smjacob /* 2682164990Smjacob * Enable the IOC 2683164990Smjacob */ 2684164990Smjacob if (mpt_enable_ioc(mpt, 0) != MPT_OK) { 2685164990Smjacob mpt_prt(mpt, "unable to initialize IOC\n"); 2686164990Smjacob return (ENXIO); 2687101704Smjacob } 2688101704Smjacob 2689164990Smjacob /* 2690164990Smjacob * Read IOC configuration information. 2691164990Smjacob * 2692164990Smjacob * We need this to determine whether or not we have certain 2693164990Smjacob * settings for Integrated Mirroring (e.g.). 2694164990Smjacob */ 2695164990Smjacob mpt_read_config_info_ioc(mpt); 2696164990Smjacob 2697101704Smjacob return (0); 2698101704Smjacob} 2699147883Sscottl 2700147883Sscottlstatic int 2701157117Smjacobmpt_enable_ioc(struct mpt_softc *mpt, int portenable) 2702147883Sscottl{ 2703147883Sscottl uint32_t pptr; 2704147883Sscottl int val; 2705147883Sscottl 2706155521Smjacob if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) { 2707147883Sscottl mpt_prt(mpt, "mpt_send_ioc_init failed\n"); 2708147883Sscottl return (EIO); 2709147883Sscottl } 2710147883Sscottl 2711147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n"); 2712147883Sscottl 2713147883Sscottl if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) { 2714147883Sscottl mpt_prt(mpt, "IOC failed to go to run state\n"); 2715147883Sscottl return (ENXIO); 2716147883Sscottl } 2717155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n"); 2718147883Sscottl 2719147883Sscottl /* 2720147883Sscottl * Give it reply buffers 2721147883Sscottl * 2722147883Sscottl * Do *not* exceed global credits. 2723147883Sscottl */ 2724156104Smjacob for (val = 0, pptr = mpt->reply_phys; 2725156104Smjacob (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE); 2726147883Sscottl pptr += MPT_REPLY_SIZE) { 2727147883Sscottl mpt_free_reply(mpt, pptr); 2728164990Smjacob if (++val == mpt->ioc_facts.GlobalCredits - 1) 2729147883Sscottl break; 2730147883Sscottl } 2731147883Sscottl 2732157117Smjacob 2733147883Sscottl /* 2734157354Smjacob * Enable the port if asked. This is only done if we're resetting 2735157354Smjacob * the IOC after initial startup. 2736147883Sscottl */ 2737157117Smjacob if (portenable) { 2738157117Smjacob /* 2739157117Smjacob * Enable asynchronous event reporting 2740157117Smjacob */ 2741157117Smjacob mpt_send_event_request(mpt, 1); 2742147883Sscottl 2743157117Smjacob if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2744157117Smjacob mpt_prt(mpt, "failed to enable port 0\n"); 2745157117Smjacob return (ENXIO); 2746157117Smjacob } 2747147883Sscottl } 2748156104Smjacob return (MPT_OK); 2749147883Sscottl} 2750164990Smjacob 2751164990Smjacob/* 2752164990Smjacob * Endian Conversion Functions- only used on Big Endian machines 2753164990Smjacob */ 2754164990Smjacob#if _BYTE_ORDER == _BIG_ENDIAN 2755164990Smjacobvoid 2756164990Smjacobmpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge) 2757164990Smjacob{ 2758164990Smjacob MPT_2_HOST32(sge, FlagsLength); 2759164998Smjacob MPT_2_HOST32(sge, u.Address64.Low); 2760164998Smjacob MPT_2_HOST32(sge, u.Address64.High); 2761164998Smjacob} 2762164990Smjacob 2763164990Smjacobvoid 2764164990Smjacobmpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp) 2765164990Smjacob{ 2766164990Smjacob MPT_2_HOST16(rp, MsgVersion); 2767164990Smjacob MPT_2_HOST16(rp, HeaderVersion); 2768164990Smjacob MPT_2_HOST32(rp, MsgContext); 2769164990Smjacob MPT_2_HOST16(rp, IOCExceptions); 2770164990Smjacob MPT_2_HOST16(rp, IOCStatus); 2771164990Smjacob MPT_2_HOST32(rp, IOCLogInfo); 2772164990Smjacob MPT_2_HOST16(rp, ReplyQueueDepth); 2773164990Smjacob MPT_2_HOST16(rp, RequestFrameSize); 2774164990Smjacob MPT_2_HOST16(rp, Reserved_0101_FWVersion); 2775164990Smjacob MPT_2_HOST16(rp, ProductID); 2776164990Smjacob MPT_2_HOST32(rp, CurrentHostMfaHighAddr); 2777164990Smjacob MPT_2_HOST16(rp, GlobalCredits); 2778164990Smjacob MPT_2_HOST32(rp, CurrentSenseBufferHighAddr); 2779164990Smjacob MPT_2_HOST16(rp, CurReplyFrameSize); 2780164990Smjacob MPT_2_HOST32(rp, FWImageSize); 2781164990Smjacob MPT_2_HOST32(rp, IOCCapabilities); 2782164990Smjacob MPT_2_HOST32(rp, FWVersion.Word); 2783164990Smjacob MPT_2_HOST16(rp, HighPriorityQueueDepth); 2784164990Smjacob MPT_2_HOST16(rp, Reserved2); 2785164990Smjacob mpt2host_sge_simple_union(&rp->HostPageBufferSGE); 2786164990Smjacob MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr); 2787164990Smjacob} 2788164990Smjacob 2789164990Smjacobvoid 2790164990Smjacobmpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp) 2791164990Smjacob{ 2792164990Smjacob MPT_2_HOST16(pfp, Reserved); 2793164990Smjacob MPT_2_HOST16(pfp, Reserved1); 2794164990Smjacob MPT_2_HOST32(pfp, MsgContext); 2795164990Smjacob MPT_2_HOST16(pfp, Reserved2); 2796164990Smjacob MPT_2_HOST16(pfp, IOCStatus); 2797164990Smjacob MPT_2_HOST32(pfp, IOCLogInfo); 2798164990Smjacob MPT_2_HOST16(pfp, MaxDevices); 2799164990Smjacob MPT_2_HOST16(pfp, PortSCSIID); 2800164990Smjacob MPT_2_HOST16(pfp, ProtocolFlags); 2801164990Smjacob MPT_2_HOST16(pfp, MaxPostedCmdBuffers); 2802164990Smjacob MPT_2_HOST16(pfp, MaxPersistentIDs); 2803164990Smjacob MPT_2_HOST16(pfp, MaxLanBuckets); 2804164990Smjacob MPT_2_HOST16(pfp, Reserved4); 2805164990Smjacob MPT_2_HOST32(pfp, Reserved5); 2806164990Smjacob} 2807164990Smjacobvoid 2808164990Smjacobmpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2) 2809164990Smjacob{ 2810164990Smjacob int i; 2811164990Smjacob ioc2->CapabilitiesFlags = htole32(ioc2->CapabilitiesFlags); 2812164990Smjacob for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) { 2813164998Smjacob MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3); 2814164990Smjacob } 2815164990Smjacob} 2816164990Smjacob 2817164990Smjacobvoid 2818164990Smjacobmpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp) 2819164990Smjacob{ 2820164990Smjacob int i; 2821164990Smjacob MPT_2_HOST16(volp, VolumeStatus.Reserved); 2822164990Smjacob MPT_2_HOST16(volp, VolumeSettings.Settings); 2823164990Smjacob MPT_2_HOST32(volp, MaxLBA); 2824164990Smjacob MPT_2_HOST32(volp, Reserved1); 2825164990Smjacob MPT_2_HOST32(volp, StripeSize); 2826164990Smjacob MPT_2_HOST32(volp, Reserved2); 2827164990Smjacob MPT_2_HOST32(volp, Reserved3); 2828164990Smjacob for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) { 2829164998Smjacob MPT_2_HOST16(volp, PhysDisk[i].Reserved); 2830164990Smjacob } 2831164990Smjacob} 2832164990Smjacob 2833164990Smjacobvoid 2834164990Smjacobmpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi) 2835164990Smjacob{ 2836164998Smjacob MPT_2_HOST16(vi, TotalBlocks.High); 2837164998Smjacob MPT_2_HOST16(vi, TotalBlocks.Low); 2838164998Smjacob MPT_2_HOST16(vi, BlocksRemaining.High); 2839164998Smjacob MPT_2_HOST16(vi, BlocksRemaining.Low); 2840164990Smjacob} 2841164990Smjacob#endif 2842