mpt.c revision 158982
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. 60101704Smjacob */ 61156000Smjacob/*- 62147883Sscottl * Copyright (c) 2004, Avid Technology, Inc. and its contributors. 63147883Sscottl * Copyright (c) 2005, WHEEL Sp. z o.o. 64147883Sscottl * Copyright (c) 2004, 2005 Justin T. Gibbs 65147883Sscottl * All rights reserved. 66156104Smjacob * 67147883Sscottl * Redistribution and use in source and binary forms, with or without 68147883Sscottl * modification, are permitted provided that the following conditions are 69147883Sscottl * met: 70147883Sscottl * 1. Redistributions of source code must retain the above copyright 71147883Sscottl * notice, this list of conditions and the following disclaimer. 72147883Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer 73147883Sscottl * substantially similar to the "NO WARRANTY" disclaimer below 74147883Sscottl * ("Disclaimer") and any redistribution must be conditioned upon including 75147883Sscottl * a substantially similar Disclaimer requirement for further binary 76147883Sscottl * redistribution. 77148679Sgibbs * 3. Neither the names of the above listed copyright holders nor the names 78148679Sgibbs * of any contributors may be used to endorse or promote products derived 79148679Sgibbs * from this software without specific prior written permission. 80156104Smjacob * 81147883Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 82147883Sscottl * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 83147883Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 84147883Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 85147883Sscottl * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 86147883Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 87147883Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 88147883Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 89147883Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 90147883Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT 91147883Sscottl * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 92101704Smjacob */ 93101704Smjacob 94134123Sobrien#include <sys/cdefs.h> 95134123Sobrien__FBSDID("$FreeBSD: head/sys/dev/mpt/mpt.c 158982 2006-05-27 17:26:57Z mjacob $"); 96134123Sobrien 97147883Sscottl#include <dev/mpt/mpt.h> 98147883Sscottl#include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */ 99147883Sscottl#include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */ 100102199Smjacob 101147883Sscottl#include <dev/mpt/mpilib/mpi.h> 102147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h> 103157117Smjacob#include <dev/mpt/mpilib/mpi_fc.h> 104157117Smjacob#include <dev/mpt/mpilib/mpi_targ.h> 105147883Sscottl 106147883Sscottl#include <sys/sysctl.h> 107147883Sscottl 108101704Smjacob#define MPT_MAX_TRYS 3 109101704Smjacob#define MPT_MAX_WAIT 300000 110101704Smjacob 111101704Smjacobstatic int maxwait_ack = 0; 112101704Smjacobstatic int maxwait_int = 0; 113101704Smjacobstatic int maxwait_state = 0; 114101704Smjacob 115147883SscottlTAILQ_HEAD(, mpt_softc) mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq); 116147883Sscottlmpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS]; 117101704Smjacob 118147883Sscottlstatic mpt_reply_handler_t mpt_default_reply_handler; 119147883Sscottlstatic mpt_reply_handler_t mpt_config_reply_handler; 120147883Sscottlstatic mpt_reply_handler_t mpt_handshake_reply_handler; 121147883Sscottlstatic mpt_reply_handler_t mpt_event_reply_handler; 122147883Sscottlstatic void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 123147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context); 124155521Smjacobstatic int mpt_send_event_request(struct mpt_softc *mpt, int onoff); 125147883Sscottlstatic int mpt_soft_reset(struct mpt_softc *mpt); 126147883Sscottlstatic void mpt_hard_reset(struct mpt_softc *mpt); 127147883Sscottlstatic int mpt_configure_ioc(struct mpt_softc *mpt); 128157117Smjacobstatic int mpt_enable_ioc(struct mpt_softc *mpt, int); 129147883Sscottl 130147883Sscottl/************************* Personality Module Support *************************/ 131147883Sscottl/* 132147883Sscottl * We include one extra entry that is guaranteed to be NULL 133147883Sscottl * to simplify our itterator. 134147883Sscottl */ 135147883Sscottlstatic struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1]; 136147883Sscottlstatic __inline struct mpt_personality* 137147883Sscottl mpt_pers_find(struct mpt_softc *, u_int); 138147883Sscottlstatic __inline struct mpt_personality* 139147883Sscottl mpt_pers_find_reverse(struct mpt_softc *, u_int); 140147883Sscottl 141147883Sscottlstatic __inline struct mpt_personality * 142147883Sscottlmpt_pers_find(struct mpt_softc *mpt, u_int start_at) 143101704Smjacob{ 144147883Sscottl KASSERT(start_at <= MPT_MAX_PERSONALITIES, 145147883Sscottl ("mpt_pers_find: starting position out of range\n")); 146147883Sscottl 147147883Sscottl while (start_at < MPT_MAX_PERSONALITIES 148147883Sscottl && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 149147883Sscottl start_at++; 150147883Sscottl } 151147883Sscottl return (mpt_personalities[start_at]); 152147883Sscottl} 153147883Sscottl 154147883Sscottl/* 155157117Smjacob * Used infrequently, so no need to optimize like a forward 156147883Sscottl * traversal where we use the MAX+1 is guaranteed to be NULL 157147883Sscottl * trick. 158147883Sscottl */ 159147883Sscottlstatic __inline struct mpt_personality * 160147883Sscottlmpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at) 161147883Sscottl{ 162147883Sscottl while (start_at < MPT_MAX_PERSONALITIES 163147883Sscottl && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) { 164147883Sscottl start_at--; 165147883Sscottl } 166147883Sscottl if (start_at < MPT_MAX_PERSONALITIES) 167147883Sscottl return (mpt_personalities[start_at]); 168147883Sscottl return (NULL); 169147883Sscottl} 170147883Sscottl 171147883Sscottl#define MPT_PERS_FOREACH(mpt, pers) \ 172147883Sscottl for (pers = mpt_pers_find(mpt, /*start_at*/0); \ 173147883Sscottl pers != NULL; \ 174147883Sscottl pers = mpt_pers_find(mpt, /*start_at*/pers->id+1)) 175147883Sscottl 176147883Sscottl#define MPT_PERS_FOREACH_REVERSE(mpt, pers) \ 177147883Sscottl for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\ 178147883Sscottl pers != NULL; \ 179147883Sscottl pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1)) 180147883Sscottl 181147883Sscottlstatic mpt_load_handler_t mpt_stdload; 182147883Sscottlstatic mpt_probe_handler_t mpt_stdprobe; 183147883Sscottlstatic mpt_attach_handler_t mpt_stdattach; 184157117Smjacobstatic mpt_enable_handler_t mpt_stdenable; 185147883Sscottlstatic mpt_event_handler_t mpt_stdevent; 186147883Sscottlstatic mpt_reset_handler_t mpt_stdreset; 187147883Sscottlstatic mpt_shutdown_handler_t mpt_stdshutdown; 188147883Sscottlstatic mpt_detach_handler_t mpt_stddetach; 189147883Sscottlstatic mpt_unload_handler_t mpt_stdunload; 190147883Sscottlstatic struct mpt_personality mpt_default_personality = 191147883Sscottl{ 192147883Sscottl .load = mpt_stdload, 193147883Sscottl .probe = mpt_stdprobe, 194147883Sscottl .attach = mpt_stdattach, 195157117Smjacob .enable = mpt_stdenable, 196147883Sscottl .event = mpt_stdevent, 197147883Sscottl .reset = mpt_stdreset, 198147883Sscottl .shutdown = mpt_stdshutdown, 199147883Sscottl .detach = mpt_stddetach, 200147883Sscottl .unload = mpt_stdunload 201147883Sscottl}; 202147883Sscottl 203147883Sscottlstatic mpt_load_handler_t mpt_core_load; 204147883Sscottlstatic mpt_attach_handler_t mpt_core_attach; 205157117Smjacobstatic mpt_enable_handler_t mpt_core_enable; 206147883Sscottlstatic mpt_reset_handler_t mpt_core_ioc_reset; 207147883Sscottlstatic mpt_event_handler_t mpt_core_event; 208147883Sscottlstatic mpt_shutdown_handler_t mpt_core_shutdown; 209147883Sscottlstatic mpt_shutdown_handler_t mpt_core_detach; 210147883Sscottlstatic mpt_unload_handler_t mpt_core_unload; 211147883Sscottlstatic struct mpt_personality mpt_core_personality = 212147883Sscottl{ 213147883Sscottl .name = "mpt_core", 214147883Sscottl .load = mpt_core_load, 215147883Sscottl .attach = mpt_core_attach, 216157117Smjacob .enable = mpt_core_enable, 217147883Sscottl .event = mpt_core_event, 218147883Sscottl .reset = mpt_core_ioc_reset, 219147883Sscottl .shutdown = mpt_core_shutdown, 220147883Sscottl .detach = mpt_core_detach, 221147883Sscottl .unload = mpt_core_unload, 222147883Sscottl}; 223147883Sscottl 224147883Sscottl/* 225147883Sscottl * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need 226147883Sscottl * ordering information. We want the core to always register FIRST. 227147883Sscottl * other modules are set to SI_ORDER_SECOND. 228147883Sscottl */ 229147883Sscottlstatic moduledata_t mpt_core_mod = { 230147883Sscottl "mpt_core", mpt_modevent, &mpt_core_personality 231147883Sscottl}; 232147883SscottlDECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 233147883SscottlMODULE_VERSION(mpt_core, 1); 234147883Sscottl 235157117Smjacob#define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id)) 236147883Sscottl 237147883Sscottl 238147883Sscottlint 239147883Sscottlmpt_modevent(module_t mod, int type, void *data) 240147883Sscottl{ 241147883Sscottl struct mpt_personality *pers; 242147883Sscottl int error; 243147883Sscottl 244147883Sscottl pers = (struct mpt_personality *)data; 245147883Sscottl 246147883Sscottl error = 0; 247147883Sscottl switch (type) { 248147883Sscottl case MOD_LOAD: 249147883Sscottl { 250147883Sscottl mpt_load_handler_t **def_handler; 251147883Sscottl mpt_load_handler_t **pers_handler; 252147883Sscottl int i; 253147883Sscottl 254147883Sscottl for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 255147883Sscottl if (mpt_personalities[i] == NULL) 256147883Sscottl break; 257147883Sscottl } 258147883Sscottl if (i >= MPT_MAX_PERSONALITIES) { 259147883Sscottl error = ENOMEM; 260147883Sscottl break; 261147883Sscottl } 262147883Sscottl pers->id = i; 263147883Sscottl mpt_personalities[i] = pers; 264147883Sscottl 265147883Sscottl /* Install standard/noop handlers for any NULL entries. */ 266147883Sscottl def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality); 267147883Sscottl pers_handler = MPT_PERS_FIRST_HANDLER(pers); 268147883Sscottl while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) { 269147883Sscottl if (*pers_handler == NULL) 270147883Sscottl *pers_handler = *def_handler; 271147883Sscottl pers_handler++; 272147883Sscottl def_handler++; 273147883Sscottl } 274147883Sscottl 275147883Sscottl error = (pers->load(pers)); 276147883Sscottl if (error != 0) 277147883Sscottl mpt_personalities[i] = NULL; 278147883Sscottl break; 279147883Sscottl } 280147883Sscottl case MOD_SHUTDOWN: 281147883Sscottl break; 282157117Smjacob#if __FreeBSD_version >= 500000 283147883Sscottl case MOD_QUIESCE: 284147883Sscottl break; 285157117Smjacob#endif 286147883Sscottl case MOD_UNLOAD: 287147883Sscottl error = pers->unload(pers); 288147883Sscottl mpt_personalities[pers->id] = NULL; 289147883Sscottl break; 290147883Sscottl default: 291147883Sscottl error = EINVAL; 292147883Sscottl break; 293147883Sscottl } 294147883Sscottl return (error); 295147883Sscottl} 296147883Sscottl 297147883Sscottlint 298147883Sscottlmpt_stdload(struct mpt_personality *pers) 299147883Sscottl{ 300147883Sscottl /* Load is always successfull. */ 301147883Sscottl return (0); 302147883Sscottl} 303147883Sscottl 304147883Sscottlint 305147883Sscottlmpt_stdprobe(struct mpt_softc *mpt) 306147883Sscottl{ 307147883Sscottl /* Probe is always successfull. */ 308147883Sscottl return (0); 309147883Sscottl} 310147883Sscottl 311147883Sscottlint 312147883Sscottlmpt_stdattach(struct mpt_softc *mpt) 313147883Sscottl{ 314147883Sscottl /* Attach is always successfull. */ 315147883Sscottl return (0); 316147883Sscottl} 317147883Sscottl 318147883Sscottlint 319157117Smjacobmpt_stdenable(struct mpt_softc *mpt) 320157117Smjacob{ 321157117Smjacob /* Enable is always successfull. */ 322157117Smjacob return (0); 323157117Smjacob} 324157117Smjacob 325157117Smjacobint 326155521Smjacobmpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg) 327147883Sscottl{ 328155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF); 329147883Sscottl /* Event was not for us. */ 330147883Sscottl return (0); 331147883Sscottl} 332147883Sscottl 333147883Sscottlvoid 334147883Sscottlmpt_stdreset(struct mpt_softc *mpt, int type) 335147883Sscottl{ 336147883Sscottl} 337147883Sscottl 338147883Sscottlvoid 339147883Sscottlmpt_stdshutdown(struct mpt_softc *mpt) 340147883Sscottl{ 341147883Sscottl} 342147883Sscottl 343147883Sscottlvoid 344147883Sscottlmpt_stddetach(struct mpt_softc *mpt) 345147883Sscottl{ 346147883Sscottl} 347147883Sscottl 348147883Sscottlint 349147883Sscottlmpt_stdunload(struct mpt_personality *pers) 350147883Sscottl{ 351147883Sscottl /* Unload is always successfull. */ 352147883Sscottl return (0); 353147883Sscottl} 354147883Sscottl 355147883Sscottl/******************************* Bus DMA Support ******************************/ 356147883Sscottlvoid 357147883Sscottlmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 358147883Sscottl{ 359147883Sscottl struct mpt_map_info *map_info; 360147883Sscottl 361147883Sscottl map_info = (struct mpt_map_info *)arg; 362147883Sscottl map_info->error = error; 363147883Sscottl map_info->phys = segs->ds_addr; 364147883Sscottl} 365147883Sscottl 366147883Sscottl/**************************** Reply/Event Handling ****************************/ 367147883Sscottlint 368147883Sscottlmpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type, 369147883Sscottl mpt_handler_t handler, uint32_t *phandler_id) 370147883Sscottl{ 371147883Sscottl 372147883Sscottl switch (type) { 373147883Sscottl case MPT_HANDLER_REPLY: 374147883Sscottl { 375147883Sscottl u_int cbi; 376147883Sscottl u_int free_cbi; 377147883Sscottl 378147883Sscottl if (phandler_id == NULL) 379147883Sscottl return (EINVAL); 380147883Sscottl 381147883Sscottl free_cbi = MPT_HANDLER_ID_NONE; 382147883Sscottl for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) { 383147883Sscottl /* 384147883Sscottl * If the same handler is registered multiple 385147883Sscottl * times, don't error out. Just return the 386147883Sscottl * index of the original registration. 387147883Sscottl */ 388147883Sscottl if (mpt_reply_handlers[cbi] == handler.reply_handler) { 389147883Sscottl *phandler_id = MPT_CBI_TO_HID(cbi); 390147883Sscottl return (0); 391147883Sscottl } 392147883Sscottl 393147883Sscottl /* 394147883Sscottl * Fill from the front in the hope that 395147883Sscottl * all registered handlers consume only a 396147883Sscottl * single cache line. 397147883Sscottl * 398147883Sscottl * We don't break on the first empty slot so 399147883Sscottl * that the full table is checked to see if 400147883Sscottl * this handler was previously registered. 401147883Sscottl */ 402157117Smjacob if (free_cbi == MPT_HANDLER_ID_NONE && 403157117Smjacob (mpt_reply_handlers[cbi] 404147883Sscottl == mpt_default_reply_handler)) 405147883Sscottl free_cbi = cbi; 406147883Sscottl } 407157117Smjacob if (free_cbi == MPT_HANDLER_ID_NONE) { 408147883Sscottl return (ENOMEM); 409157117Smjacob } 410147883Sscottl mpt_reply_handlers[free_cbi] = handler.reply_handler; 411147883Sscottl *phandler_id = MPT_CBI_TO_HID(free_cbi); 412147883Sscottl break; 413147883Sscottl } 414147883Sscottl default: 415147883Sscottl mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type); 416147883Sscottl return (EINVAL); 417147883Sscottl } 418147883Sscottl return (0); 419147883Sscottl} 420147883Sscottl 421147883Sscottlint 422147883Sscottlmpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type, 423147883Sscottl mpt_handler_t handler, uint32_t handler_id) 424147883Sscottl{ 425147883Sscottl 426147883Sscottl switch (type) { 427147883Sscottl case MPT_HANDLER_REPLY: 428147883Sscottl { 429147883Sscottl u_int cbi; 430147883Sscottl 431147883Sscottl cbi = MPT_CBI(handler_id); 432147883Sscottl if (cbi >= MPT_NUM_REPLY_HANDLERS 433147883Sscottl || mpt_reply_handlers[cbi] != handler.reply_handler) 434147883Sscottl return (ENOENT); 435147883Sscottl mpt_reply_handlers[cbi] = mpt_default_reply_handler; 436147883Sscottl break; 437147883Sscottl } 438147883Sscottl default: 439147883Sscottl mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type); 440147883Sscottl return (EINVAL); 441147883Sscottl } 442147883Sscottl return (0); 443147883Sscottl} 444147883Sscottl 445147883Sscottlstatic int 446147883Sscottlmpt_default_reply_handler(struct mpt_softc *mpt, request_t *req, 447157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 448147883Sscottl{ 449157117Smjacob mpt_prt(mpt, 450157117Smjacob "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n", 451157117Smjacob req, req->serno, reply_desc, reply_frame); 452147883Sscottl 453147883Sscottl if (reply_frame != NULL) 454147883Sscottl mpt_dump_reply_frame(mpt, reply_frame); 455147883Sscottl 456157117Smjacob mpt_prt(mpt, "Reply Frame Ignored\n"); 457147883Sscottl 458147883Sscottl return (/*free_reply*/TRUE); 459147883Sscottl} 460147883Sscottl 461147883Sscottlstatic int 462147883Sscottlmpt_config_reply_handler(struct mpt_softc *mpt, request_t *req, 463157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 464147883Sscottl{ 465147883Sscottl if (req != NULL) { 466147883Sscottl 467147883Sscottl if (reply_frame != NULL) { 468147883Sscottl MSG_CONFIG *cfgp; 469147883Sscottl MSG_CONFIG_REPLY *reply; 470147883Sscottl 471147883Sscottl cfgp = (MSG_CONFIG *)req->req_vbuf; 472147883Sscottl reply = (MSG_CONFIG_REPLY *)reply_frame; 473147883Sscottl req->IOCStatus = le16toh(reply_frame->IOCStatus); 474147883Sscottl bcopy(&reply->Header, &cfgp->Header, 475147883Sscottl sizeof(cfgp->Header)); 476147883Sscottl } 477147883Sscottl req->state &= ~REQ_STATE_QUEUED; 478147883Sscottl req->state |= REQ_STATE_DONE; 479147883Sscottl TAILQ_REMOVE(&mpt->request_pending_list, req, links); 480157354Smjacob if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) { 481147883Sscottl wakeup(req); 482157354Smjacob } 483147883Sscottl } 484147883Sscottl 485157354Smjacob return (TRUE); 486147883Sscottl} 487147883Sscottl 488147883Sscottlstatic int 489147883Sscottlmpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req, 490157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 491147883Sscottl{ 492147883Sscottl /* Nothing to be done. */ 493157354Smjacob return (TRUE); 494147883Sscottl} 495147883Sscottl 496147883Sscottlstatic int 497147883Sscottlmpt_event_reply_handler(struct mpt_softc *mpt, request_t *req, 498157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame) 499147883Sscottl{ 500147883Sscottl int free_reply; 501147883Sscottl 502157354Smjacob KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler")); 503157354Smjacob KASSERT(req != NULL, ("null request in mpt_event_reply_handler")); 504147883Sscottl 505147883Sscottl free_reply = TRUE; 506147883Sscottl switch (reply_frame->Function) { 507147883Sscottl case MPI_FUNCTION_EVENT_NOTIFICATION: 508147883Sscottl { 509147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg; 510147883Sscottl struct mpt_personality *pers; 511147883Sscottl u_int handled; 512147883Sscottl 513147883Sscottl handled = 0; 514147883Sscottl msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 515147883Sscottl MPT_PERS_FOREACH(mpt, pers) 516147883Sscottl handled += pers->event(mpt, req, msg); 517147883Sscottl 518155521Smjacob if (handled == 0 && mpt->mpt_pers_mask == 0) { 519156301Smjacob mpt_lprt(mpt, MPT_PRT_INFO, 520155521Smjacob "No Handlers For Any Event Notify Frames. " 521155521Smjacob "Event %#x (ACK %sequired).\n", 522155521Smjacob msg->Event, msg->AckRequired? "r" : "not r"); 523155521Smjacob } else if (handled == 0) { 524156301Smjacob mpt_lprt(mpt, MPT_PRT_WARN, 525155521Smjacob "Unhandled Event Notify Frame. Event %#x " 526155521Smjacob "(ACK %sequired).\n", 527155521Smjacob msg->Event, msg->AckRequired? "r" : "not r"); 528155521Smjacob } 529147883Sscottl 530147883Sscottl if (msg->AckRequired) { 531147883Sscottl request_t *ack_req; 532147883Sscottl uint32_t context; 533147883Sscottl 534147883Sscottl context = htole32(req->index|MPT_REPLY_HANDLER_EVENTS); 535157354Smjacob ack_req = mpt_get_request(mpt, FALSE); 536147883Sscottl if (ack_req == NULL) { 537147883Sscottl struct mpt_evtf_record *evtf; 538147883Sscottl 539147883Sscottl evtf = (struct mpt_evtf_record *)reply_frame; 540147883Sscottl evtf->context = context; 541147883Sscottl LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links); 542147883Sscottl free_reply = FALSE; 543147883Sscottl break; 544147883Sscottl } 545147883Sscottl mpt_send_event_ack(mpt, ack_req, msg, context); 546157354Smjacob /* 547157354Smjacob * Don't check for CONTINUATION_REPLY here 548157354Smjacob */ 549157354Smjacob return (free_reply); 550147883Sscottl } 551147883Sscottl break; 552147883Sscottl } 553147883Sscottl case MPI_FUNCTION_PORT_ENABLE: 554157354Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n"); 555147883Sscottl break; 556147883Sscottl case MPI_FUNCTION_EVENT_ACK: 557147883Sscottl break; 558147883Sscottl default: 559157354Smjacob mpt_prt(mpt, "unknown event function: %x\n", 560147883Sscottl reply_frame->Function); 561147883Sscottl break; 562147883Sscottl } 563147883Sscottl 564157354Smjacob /* 565157354Smjacob * I'm not sure that this continuation stuff works as it should. 566157354Smjacob * 567157354Smjacob * I've had FC async events occur that free the frame up because 568157354Smjacob * the continuation bit isn't set, and then additional async events 569157354Smjacob * then occur using the same context. As you might imagine, this 570157354Smjacob * leads to Very Bad Thing. 571157354Smjacob * 572157354Smjacob * Let's just be safe for now and not free them up until we figure 573157354Smjacob * out what's actually happening here. 574157354Smjacob */ 575157354Smjacob#if 0 576157354Smjacob if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) { 577147883Sscottl TAILQ_REMOVE(&mpt->request_pending_list, req, links); 578157354Smjacob mpt_free_request(mpt, req); 579157354Smjacob mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation", 580157354Smjacob reply_frame->Function, req, req->serno); 581157354Smjacob if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 582157354Smjacob MSG_EVENT_NOTIFY_REPLY *msg = 583157354Smjacob (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 584157354Smjacob mpt_prtc(mpt, " Event=0x%x AckReq=%d", 585157354Smjacob msg->Event, msg->AckRequired); 586157354Smjacob } 587157354Smjacob } else { 588157354Smjacob mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation", 589157354Smjacob reply_frame->Function, req, req->serno); 590157354Smjacob if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) { 591157354Smjacob MSG_EVENT_NOTIFY_REPLY *msg = 592157354Smjacob (MSG_EVENT_NOTIFY_REPLY *)reply_frame; 593157354Smjacob mpt_prtc(mpt, " Event=0x%x AckReq=%d", 594157354Smjacob msg->Event, msg->AckRequired); 595157354Smjacob } 596157354Smjacob mpt_prtc(mpt, "\n"); 597147883Sscottl } 598157354Smjacob#endif 599147883Sscottl return (free_reply); 600147883Sscottl} 601147883Sscottl 602147883Sscottl/* 603147883Sscottl * Process an asynchronous event from the IOC. 604147883Sscottl */ 605147883Sscottlstatic int 606147883Sscottlmpt_core_event(struct mpt_softc *mpt, request_t *req, 607147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg) 608147883Sscottl{ 609155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n", 610155521Smjacob msg->Event & 0xFF); 611147883Sscottl switch(msg->Event & 0xFF) { 612147883Sscottl case MPI_EVENT_NONE: 613147883Sscottl break; 614147883Sscottl case MPI_EVENT_LOG_DATA: 615147883Sscottl { 616147883Sscottl int i; 617147883Sscottl 618147883Sscottl /* Some error occured that LSI wants logged */ 619147883Sscottl mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n", 620147883Sscottl msg->IOCLogInfo); 621147883Sscottl mpt_prt(mpt, "\tEvtLogData: Event Data:"); 622147883Sscottl for (i = 0; i < msg->EventDataLength; i++) 623147883Sscottl mpt_prtc(mpt, " %08x", msg->Data[i]); 624147883Sscottl mpt_prtc(mpt, "\n"); 625147883Sscottl break; 626147883Sscottl } 627147883Sscottl case MPI_EVENT_EVENT_CHANGE: 628147883Sscottl /* 629147883Sscottl * This is just an acknowledgement 630147883Sscottl * of our mpt_send_event_request. 631147883Sscottl */ 632147883Sscottl break; 633155521Smjacob case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE: 634155521Smjacob break; 635147883Sscottl default: 636157354Smjacob return (0); 637147883Sscottl break; 638147883Sscottl } 639157354Smjacob return (1); 640147883Sscottl} 641147883Sscottl 642147883Sscottlstatic void 643147883Sscottlmpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req, 644147883Sscottl MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context) 645147883Sscottl{ 646147883Sscottl MSG_EVENT_ACK *ackp; 647147883Sscottl 648147883Sscottl ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf; 649157354Smjacob memset(ackp, 0, sizeof (*ackp)); 650147883Sscottl ackp->Function = MPI_FUNCTION_EVENT_ACK; 651147883Sscottl ackp->Event = msg->Event; 652147883Sscottl ackp->EventContext = msg->EventContext; 653147883Sscottl ackp->MsgContext = context; 654147883Sscottl mpt_check_doorbell(mpt); 655147883Sscottl mpt_send_cmd(mpt, ack_req); 656147883Sscottl} 657147883Sscottl 658147883Sscottl/***************************** Interrupt Handling *****************************/ 659147883Sscottlvoid 660147883Sscottlmpt_intr(void *arg) 661147883Sscottl{ 662147883Sscottl struct mpt_softc *mpt; 663157117Smjacob uint32_t reply_desc; 664157117Smjacob int ntrips = 0; 665147883Sscottl 666147883Sscottl mpt = (struct mpt_softc *)arg; 667157662Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n"); 668147883Sscottl while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) { 669147883Sscottl request_t *req; 670147883Sscottl MSG_DEFAULT_REPLY *reply_frame; 671147883Sscottl uint32_t reply_baddr; 672157117Smjacob uint32_t ctxt_idx; 673147883Sscottl u_int cb_index; 674147883Sscottl u_int req_index; 675147883Sscottl int free_rf; 676147883Sscottl 677147883Sscottl req = NULL; 678147883Sscottl reply_frame = NULL; 679147883Sscottl reply_baddr = 0; 680147883Sscottl if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) { 681147883Sscottl u_int offset; 682147883Sscottl /* 683147883Sscottl * Insure that the reply frame is coherent. 684147883Sscottl */ 685157354Smjacob reply_baddr = MPT_REPLY_BADDR(reply_desc); 686147883Sscottl offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF); 687157117Smjacob bus_dmamap_sync_range(mpt->reply_dmat, 688157117Smjacob mpt->reply_dmap, offset, MPT_REPLY_SIZE, 689157117Smjacob BUS_DMASYNC_POSTREAD); 690147883Sscottl reply_frame = MPT_REPLY_OTOV(mpt, offset); 691157117Smjacob ctxt_idx = le32toh(reply_frame->MsgContext); 692157117Smjacob } else { 693157117Smjacob uint32_t type; 694157117Smjacob 695157117Smjacob type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc); 696157117Smjacob ctxt_idx = reply_desc; 697157117Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n", 698157117Smjacob reply_desc); 699157117Smjacob 700157117Smjacob switch (type) { 701157117Smjacob case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT: 702157117Smjacob ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK; 703157117Smjacob break; 704157117Smjacob case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET: 705157117Smjacob ctxt_idx = GET_IO_INDEX(reply_desc); 706157117Smjacob if (mpt->tgt_cmd_ptrs == NULL) { 707157117Smjacob mpt_prt(mpt, 708157117Smjacob "mpt_intr: no target cmd ptrs\n"); 709157117Smjacob reply_desc = MPT_REPLY_EMPTY; 710157117Smjacob break; 711157117Smjacob } 712157117Smjacob if (ctxt_idx >= mpt->tgt_cmds_allocated) { 713157117Smjacob mpt_prt(mpt, 714157117Smjacob "mpt_intr: bad tgt cmd ctxt %u\n", 715157117Smjacob ctxt_idx); 716157117Smjacob reply_desc = MPT_REPLY_EMPTY; 717157117Smjacob ntrips = 1000; 718157117Smjacob break; 719157117Smjacob } 720157117Smjacob req = mpt->tgt_cmd_ptrs[ctxt_idx]; 721157117Smjacob if (req == NULL) { 722157117Smjacob mpt_prt(mpt, "no request backpointer " 723157117Smjacob "at index %u", ctxt_idx); 724157117Smjacob reply_desc = MPT_REPLY_EMPTY; 725157117Smjacob ntrips = 1000; 726157117Smjacob break; 727157117Smjacob } 728157117Smjacob /* 729157117Smjacob * Reformulate ctxt_idx to be just as if 730157117Smjacob * it were another type of context reply 731157117Smjacob * so the code below will find the request 732157117Smjacob * via indexing into the pool. 733157117Smjacob */ 734157117Smjacob ctxt_idx = 735157117Smjacob req->index | mpt->scsi_tgt_handler_id; 736157117Smjacob req = NULL; 737157117Smjacob break; 738157117Smjacob case MPI_CONTEXT_REPLY_TYPE_LAN: 739157117Smjacob mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n", 740157117Smjacob reply_desc); 741157117Smjacob reply_desc = MPT_REPLY_EMPTY; 742157117Smjacob break; 743157117Smjacob default: 744157117Smjacob mpt_prt(mpt, "Context Reply 0x%08x?\n", type); 745157117Smjacob reply_desc = MPT_REPLY_EMPTY; 746157117Smjacob break; 747157117Smjacob } 748157117Smjacob if (reply_desc == MPT_REPLY_EMPTY) { 749157117Smjacob if (ntrips++ > 1000) { 750157117Smjacob break; 751157117Smjacob } 752157117Smjacob continue; 753157117Smjacob } 754147883Sscottl } 755157117Smjacob 756157117Smjacob cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx); 757157117Smjacob req_index = MPT_CONTEXT_TO_REQI(ctxt_idx); 758157117Smjacob if (req_index < MPT_MAX_REQUESTS(mpt)) { 759147883Sscottl req = &mpt->request_pool[req_index]; 760157354Smjacob } else { 761157354Smjacob mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc ==" 762157354Smjacob " 0x%x)\n", req_index, reply_desc); 763157117Smjacob } 764147883Sscottl 765157117Smjacob free_rf = mpt_reply_handlers[cb_index](mpt, req, 766157117Smjacob reply_desc, reply_frame); 767147883Sscottl 768157354Smjacob if (reply_frame != NULL && free_rf) { 769147883Sscottl mpt_free_reply(mpt, reply_baddr); 770157354Smjacob } 771157117Smjacob 772157117Smjacob /* 773157117Smjacob * If we got ourselves disabled, don't get stuck in a loop 774157117Smjacob */ 775157117Smjacob if (mpt->disabled) { 776157117Smjacob mpt_disable_ints(mpt); 777157117Smjacob break; 778157117Smjacob } 779157117Smjacob if (ntrips++ > 1000) { 780157117Smjacob break; 781157117Smjacob } 782147883Sscottl } 783157662Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n"); 784147883Sscottl} 785147883Sscottl 786147883Sscottl/******************************* Error Recovery *******************************/ 787147883Sscottlvoid 788147883Sscottlmpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain, 789147883Sscottl u_int iocstatus) 790147883Sscottl{ 791147883Sscottl MSG_DEFAULT_REPLY ioc_status_frame; 792147883Sscottl request_t *req; 793147883Sscottl 794157354Smjacob memset(&ioc_status_frame, 0, sizeof(ioc_status_frame)); 795147883Sscottl ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4); 796156104Smjacob ioc_status_frame.IOCStatus = iocstatus; 797147883Sscottl while((req = TAILQ_FIRST(chain)) != NULL) { 798147883Sscottl MSG_REQUEST_HEADER *msg_hdr; 799147883Sscottl u_int cb_index; 800157354Smjacob 801156796Smjacob TAILQ_REMOVE(chain, req, links); 802147883Sscottl msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf; 803156104Smjacob ioc_status_frame.Function = msg_hdr->Function; 804156104Smjacob ioc_status_frame.MsgContext = msg_hdr->MsgContext; 805147883Sscottl cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext)); 806157117Smjacob mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext, 807157117Smjacob &ioc_status_frame); 808147883Sscottl } 809147883Sscottl} 810147883Sscottl 811147883Sscottl/********************************* Diagnostics ********************************/ 812147883Sscottl/* 813147883Sscottl * Perform a diagnostic dump of a reply frame. 814147883Sscottl */ 815147883Sscottlvoid 816147883Sscottlmpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame) 817147883Sscottl{ 818147883Sscottl mpt_prt(mpt, "Address Reply:\n"); 819147883Sscottl mpt_print_reply(reply_frame); 820147883Sscottl} 821147883Sscottl 822147883Sscottl/******************************* Doorbell Access ******************************/ 823147883Sscottlstatic __inline uint32_t mpt_rd_db(struct mpt_softc *mpt); 824147883Sscottlstatic __inline uint32_t mpt_rd_intr(struct mpt_softc *mpt); 825147883Sscottl 826147883Sscottlstatic __inline uint32_t 827147883Sscottlmpt_rd_db(struct mpt_softc *mpt) 828147883Sscottl{ 829101704Smjacob return mpt_read(mpt, MPT_OFFSET_DOORBELL); 830101704Smjacob} 831101704Smjacob 832147883Sscottlstatic __inline uint32_t 833147883Sscottlmpt_rd_intr(struct mpt_softc *mpt) 834101704Smjacob{ 835101704Smjacob return mpt_read(mpt, MPT_OFFSET_INTR_STATUS); 836101704Smjacob} 837101704Smjacob 838101704Smjacob/* Busy wait for a door bell to be read by IOC */ 839101704Smjacobstatic int 840147883Sscottlmpt_wait_db_ack(struct mpt_softc *mpt) 841101704Smjacob{ 842101704Smjacob int i; 843101704Smjacob for (i=0; i < MPT_MAX_WAIT; i++) { 844101704Smjacob if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) { 845101704Smjacob maxwait_ack = i > maxwait_ack ? i : maxwait_ack; 846157117Smjacob return (MPT_OK); 847101704Smjacob } 848157117Smjacob DELAY(200); 849101704Smjacob } 850157117Smjacob return (MPT_FAIL); 851101704Smjacob} 852101704Smjacob 853101704Smjacob/* Busy wait for a door bell interrupt */ 854101704Smjacobstatic int 855147883Sscottlmpt_wait_db_int(struct mpt_softc *mpt) 856101704Smjacob{ 857101704Smjacob int i; 858101704Smjacob for (i=0; i < MPT_MAX_WAIT; i++) { 859101704Smjacob if (MPT_DB_INTR(mpt_rd_intr(mpt))) { 860101704Smjacob maxwait_int = i > maxwait_int ? i : maxwait_int; 861101704Smjacob return MPT_OK; 862101704Smjacob } 863101704Smjacob DELAY(100); 864101704Smjacob } 865157117Smjacob return (MPT_FAIL); 866101704Smjacob} 867101704Smjacob 868101704Smjacob/* Wait for IOC to transition to a give state */ 869101704Smjacobvoid 870147883Sscottlmpt_check_doorbell(struct mpt_softc *mpt) 871101704Smjacob{ 872147883Sscottl uint32_t db = mpt_rd_db(mpt); 873101704Smjacob if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) { 874147883Sscottl mpt_prt(mpt, "Device not running\n"); 875101704Smjacob mpt_print_db(db); 876101704Smjacob } 877101704Smjacob} 878101704Smjacob 879101704Smjacob/* Wait for IOC to transition to a give state */ 880101704Smjacobstatic int 881147883Sscottlmpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state) 882101704Smjacob{ 883101704Smjacob int i; 884101704Smjacob 885101704Smjacob for (i = 0; i < MPT_MAX_WAIT; i++) { 886147883Sscottl uint32_t db = mpt_rd_db(mpt); 887101704Smjacob if (MPT_STATE(db) == state) { 888101704Smjacob maxwait_state = i > maxwait_state ? i : maxwait_state; 889101704Smjacob return (MPT_OK); 890101704Smjacob } 891101704Smjacob DELAY(100); 892101704Smjacob } 893101704Smjacob return (MPT_FAIL); 894101704Smjacob} 895101704Smjacob 896101704Smjacob 897147883Sscottl/************************* Intialization/Configuration ************************/ 898147883Sscottlstatic int mpt_download_fw(struct mpt_softc *mpt); 899147883Sscottl 900101704Smjacob/* Issue the reset COMMAND to the IOC */ 901147883Sscottlstatic int 902147883Sscottlmpt_soft_reset(struct mpt_softc *mpt) 903101704Smjacob{ 904147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n"); 905101704Smjacob 906101704Smjacob /* Have to use hard reset if we are not in Running state */ 907101704Smjacob if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) { 908147883Sscottl mpt_prt(mpt, "soft reset failed: device not running\n"); 909157117Smjacob return (MPT_FAIL); 910101704Smjacob } 911101704Smjacob 912101704Smjacob /* If door bell is in use we don't have a chance of getting 913101704Smjacob * a word in since the IOC probably crashed in message 914101704Smjacob * processing. So don't waste our time. 915101704Smjacob */ 916101704Smjacob if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) { 917147883Sscottl mpt_prt(mpt, "soft reset failed: doorbell wedged\n"); 918157117Smjacob return (MPT_FAIL); 919101704Smjacob } 920101704Smjacob 921101704Smjacob /* Send the reset request to the IOC */ 922101704Smjacob mpt_write(mpt, MPT_OFFSET_DOORBELL, 923101704Smjacob MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT); 924101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 925147883Sscottl mpt_prt(mpt, "soft reset failed: ack timeout\n"); 926157117Smjacob return (MPT_FAIL); 927101704Smjacob } 928101704Smjacob 929101704Smjacob /* Wait for the IOC to reload and come out of reset state */ 930101704Smjacob if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) { 931147883Sscottl mpt_prt(mpt, "soft reset failed: device did not restart\n"); 932157117Smjacob return (MPT_FAIL); 933101704Smjacob } 934101704Smjacob 935101704Smjacob return MPT_OK; 936101704Smjacob} 937101704Smjacob 938147883Sscottlstatic int 939147883Sscottlmpt_enable_diag_mode(struct mpt_softc *mpt) 940147883Sscottl{ 941147883Sscottl int try; 942147883Sscottl 943147883Sscottl try = 20; 944147883Sscottl while (--try) { 945147883Sscottl 946147883Sscottl if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0) 947147883Sscottl break; 948147883Sscottl 949147883Sscottl /* Enable diagnostic registers */ 950147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF); 951147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE); 952147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE); 953147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE); 954147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE); 955147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE); 956147883Sscottl 957147883Sscottl DELAY(100000); 958147883Sscottl } 959147883Sscottl if (try == 0) 960147883Sscottl return (EIO); 961147883Sscottl return (0); 962147883Sscottl} 963147883Sscottl 964147883Sscottlstatic void 965147883Sscottlmpt_disable_diag_mode(struct mpt_softc *mpt) 966147883Sscottl{ 967147883Sscottl mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF); 968147883Sscottl} 969147883Sscottl 970101704Smjacob/* This is a magic diagnostic reset that resets all the ARM 971156104Smjacob * processors in the chip. 972101704Smjacob */ 973147883Sscottlstatic void 974147883Sscottlmpt_hard_reset(struct mpt_softc *mpt) 975101704Smjacob{ 976147883Sscottl int error; 977147883Sscottl int wait; 978147883Sscottl uint32_t diagreg; 979147883Sscottl 980147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n"); 981147883Sscottl 982147883Sscottl error = mpt_enable_diag_mode(mpt); 983147883Sscottl if (error) { 984147883Sscottl mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n"); 985147883Sscottl mpt_prt(mpt, "Trying to reset anyway.\n"); 986101704Smjacob } 987101704Smjacob 988147883Sscottl diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 989101704Smjacob 990147883Sscottl /* 991147883Sscottl * This appears to be a workaround required for some 992147883Sscottl * firmware or hardware revs. 993147883Sscottl */ 994147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM); 995147883Sscottl DELAY(1000); 996147883Sscottl 997101704Smjacob /* Diag. port is now active so we can now hit the reset bit */ 998147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER); 999101704Smjacob 1000147883Sscottl /* 1001147883Sscottl * Ensure that the reset has finished. We delay 1ms 1002147883Sscottl * prior to reading the register to make sure the chip 1003147883Sscottl * has sufficiently completed its reset to handle register 1004147883Sscottl * accesses. 1005147883Sscottl */ 1006147883Sscottl wait = 5000; 1007147883Sscottl do { 1008147883Sscottl DELAY(1000); 1009147883Sscottl diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 1010147883Sscottl } while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0); 1011101704Smjacob 1012147883Sscottl if (wait == 0) { 1013147883Sscottl mpt_prt(mpt, "WARNING - Failed hard reset! " 1014147883Sscottl "Trying to initialize anyway.\n"); 1015147883Sscottl } 1016101704Smjacob 1017147883Sscottl /* 1018147883Sscottl * If we have firmware to download, it must be loaded before 1019147883Sscottl * the controller will become operational. Do so now. 1020147883Sscottl */ 1021147883Sscottl if (mpt->fw_image != NULL) { 1022147883Sscottl 1023147883Sscottl error = mpt_download_fw(mpt); 1024147883Sscottl 1025147883Sscottl if (error) { 1026147883Sscottl mpt_prt(mpt, "WARNING - Firmware Download Failed!\n"); 1027147883Sscottl mpt_prt(mpt, "Trying to initialize anyway.\n"); 1028147883Sscottl } 1029101704Smjacob } 1030101704Smjacob 1031147883Sscottl /* 1032147883Sscottl * Reseting the controller should have disabled write 1033147883Sscottl * access to the diagnostic registers, but disable 1034147883Sscottl * manually to be sure. 1035147883Sscottl */ 1036147883Sscottl mpt_disable_diag_mode(mpt); 1037101704Smjacob} 1038101704Smjacob 1039147883Sscottlstatic void 1040147883Sscottlmpt_core_ioc_reset(struct mpt_softc *mpt, int type) 1041147883Sscottl{ 1042147883Sscottl /* 1043147883Sscottl * Complete all pending requests with a status 1044147883Sscottl * appropriate for an IOC reset. 1045147883Sscottl */ 1046147883Sscottl mpt_complete_request_chain(mpt, &mpt->request_pending_list, 1047147883Sscottl MPI_IOCSTATUS_INVALID_STATE); 1048147883Sscottl} 1049147883Sscottl 1050147883Sscottl 1051101704Smjacob/* 1052101704Smjacob * Reset the IOC when needed. Try software command first then if needed 1053101704Smjacob * poke at the magic diagnostic reset. Note that a hard reset resets 1054101704Smjacob * *both* IOCs on dual function chips (FC929 && LSI1030) as well as 1055101704Smjacob * fouls up the PCI configuration registers. 1056101704Smjacob */ 1057101704Smjacobint 1058147883Sscottlmpt_reset(struct mpt_softc *mpt, int reinit) 1059101704Smjacob{ 1060147883Sscottl struct mpt_personality *pers; 1061147883Sscottl int ret; 1062156104Smjacob int retry_cnt = 0; 1063101704Smjacob 1064156104Smjacob /* 1065156104Smjacob * Try a soft reset. If that fails, get out the big hammer. 1066156104Smjacob */ 1067156104Smjacob again: 1068101704Smjacob if ((ret = mpt_soft_reset(mpt)) != MPT_OK) { 1069156104Smjacob int cnt; 1070156104Smjacob for (cnt = 0; cnt < 5; cnt++) { 1071156104Smjacob /* Failed; do a hard reset */ 1072156104Smjacob mpt_hard_reset(mpt); 1073101704Smjacob 1074156104Smjacob /* 1075156104Smjacob * Wait for the IOC to reload 1076156104Smjacob * and come out of reset state 1077156104Smjacob */ 1078156104Smjacob ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1079156104Smjacob if (ret == MPT_OK) { 1080156104Smjacob break; 1081156104Smjacob } 1082156104Smjacob /* 1083156104Smjacob * Okay- try to check again... 1084156104Smjacob */ 1085156104Smjacob ret = mpt_wait_state(mpt, MPT_DB_STATE_READY); 1086156104Smjacob if (ret == MPT_OK) { 1087156104Smjacob break; 1088156104Smjacob } 1089156104Smjacob mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n", 1090156104Smjacob retry_cnt, cnt); 1091156104Smjacob } 1092101704Smjacob } 1093101704Smjacob 1094156104Smjacob if (retry_cnt == 0) { 1095156104Smjacob /* 1096156104Smjacob * Invoke reset handlers. We bump the reset count so 1097156104Smjacob * that mpt_wait_req() understands that regardless of 1098156104Smjacob * the specified wait condition, it should stop its wait. 1099156104Smjacob */ 1100156104Smjacob mpt->reset_cnt++; 1101156104Smjacob MPT_PERS_FOREACH(mpt, pers) 1102156104Smjacob pers->reset(mpt, ret); 1103156104Smjacob } 1104147883Sscottl 1105157354Smjacob if (reinit) { 1106157117Smjacob ret = mpt_enable_ioc(mpt, 1); 1107156104Smjacob if (ret == MPT_OK) { 1108156104Smjacob mpt_enable_ints(mpt); 1109156104Smjacob } 1110156104Smjacob } 1111156104Smjacob if (ret != MPT_OK && retry_cnt++ < 2) { 1112156104Smjacob goto again; 1113156104Smjacob } 1114101704Smjacob return ret; 1115101704Smjacob} 1116101704Smjacob 1117101704Smjacob/* Return a command buffer to the free queue */ 1118101704Smjacobvoid 1119147883Sscottlmpt_free_request(struct mpt_softc *mpt, request_t *req) 1120101704Smjacob{ 1121155521Smjacob request_t *nxt; 1122147883Sscottl struct mpt_evtf_record *record; 1123147883Sscottl uint32_t reply_baddr; 1124147883Sscottl 1125103871Smjacob if (req == NULL || req != &mpt->request_pool[req->index]) { 1126101704Smjacob panic("mpt_free_request bad req ptr\n"); 1127101704Smjacob return; 1128101704Smjacob } 1129155521Smjacob if ((nxt = req->chain) != NULL) { 1130155521Smjacob req->chain = NULL; 1131155521Smjacob mpt_free_request(mpt, nxt); /* NB: recursion */ 1132155521Smjacob } 1133157354Smjacob KASSERT(req->state != REQ_STATE_FREE, ("freeing free request")); 1134157354Smjacob KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request")); 1135157662Smjacob KASSERT(MPT_OWNED(mpt), ("mpt_free_request: mpt not locked\n")); 1136157662Smjacob KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1137157662Smjacob ("mpt_free_request: req %p:%u func %x already on freelist", 1138157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1139157662Smjacob KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1140157662Smjacob ("mpt_free_request: req %p:%u func %x on pending list", 1141157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1142157662Smjacob#ifdef INVARIANTS 1143157662Smjacob mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__); 1144157662Smjacob#endif 1145157354Smjacob 1146101704Smjacob req->ccb = NULL; 1147147883Sscottl if (LIST_EMPTY(&mpt->ack_frames)) { 1148157117Smjacob /* 1149157117Smjacob * Insert free ones at the tail 1150157117Smjacob */ 1151157354Smjacob req->serno = 0; 1152157354Smjacob req->state = REQ_STATE_FREE; 1153157662Smjacob#ifdef INVARIANTS 1154157662Smjacob memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER)); 1155157662Smjacob#endif 1156157117Smjacob TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links); 1157147883Sscottl if (mpt->getreqwaiter != 0) { 1158147883Sscottl mpt->getreqwaiter = 0; 1159147883Sscottl wakeup(&mpt->request_free_list); 1160147883Sscottl } 1161147883Sscottl return; 1162147883Sscottl } 1163147883Sscottl 1164147883Sscottl /* 1165147883Sscottl * Process an ack frame deferred due to resource shortage. 1166147883Sscottl */ 1167147883Sscottl record = LIST_FIRST(&mpt->ack_frames); 1168147883Sscottl LIST_REMOVE(record, links); 1169157354Smjacob req->state = REQ_STATE_ALLOCATED; 1170157662Smjacob mpt_assign_serno(mpt, req); 1171147883Sscottl mpt_send_event_ack(mpt, req, &record->reply, record->context); 1172147883Sscottl reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply) 1173147883Sscottl + (mpt->reply_phys & 0xFFFFFFFF); 1174147883Sscottl mpt_free_reply(mpt, reply_baddr); 1175101704Smjacob} 1176101704Smjacob 1177101704Smjacob/* Get a command buffer from the free queue */ 1178101704Smjacobrequest_t * 1179147883Sscottlmpt_get_request(struct mpt_softc *mpt, int sleep_ok) 1180101704Smjacob{ 1181101704Smjacob request_t *req; 1182147883Sscottl 1183147883Sscottlretry: 1184157662Smjacob KASSERT(MPT_OWNED(mpt), ("mpt_get_request: mpt not locked\n")); 1185147883Sscottl req = TAILQ_FIRST(&mpt->request_free_list); 1186101704Smjacob if (req != NULL) { 1187147883Sscottl KASSERT(req == &mpt->request_pool[req->index], 1188147883Sscottl ("mpt_get_request: corrupted request free list\n")); 1189157354Smjacob KASSERT(req->state == REQ_STATE_FREE, 1190157662Smjacob ("req %p:%u not free on free list %x index %d function %x", 1191157662Smjacob req, req->serno, req->state, req->index, 1192157662Smjacob ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1193147883Sscottl TAILQ_REMOVE(&mpt->request_free_list, req, links); 1194147883Sscottl req->state = REQ_STATE_ALLOCATED; 1195155521Smjacob req->chain = NULL; 1196157662Smjacob mpt_assign_serno(mpt, req); 1197147883Sscottl } else if (sleep_ok != 0) { 1198147883Sscottl mpt->getreqwaiter = 1; 1199147883Sscottl mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0); 1200147883Sscottl goto retry; 1201101704Smjacob } 1202157354Smjacob return (req); 1203101704Smjacob} 1204101704Smjacob 1205101704Smjacob/* Pass the command to the IOC */ 1206101704Smjacobvoid 1207147883Sscottlmpt_send_cmd(struct mpt_softc *mpt, request_t *req) 1208101704Smjacob{ 1209158982Smjacob if (mpt->verbose > MPT_PRT_DEBUG2) { 1210158932Smjacob mpt_dump_request(mpt, req); 1211155521Smjacob } 1212101704Smjacob bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1213103914Smjacob BUS_DMASYNC_PREWRITE); 1214147883Sscottl req->state |= REQ_STATE_QUEUED; 1215157662Smjacob KASSERT(mpt_req_on_free_list(mpt, req) == 0, 1216157662Smjacob ("req %p:%u func %x on freelist list in mpt_send_cmd", 1217157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1218157662Smjacob KASSERT(mpt_req_on_pending_list(mpt, req) == 0, 1219157662Smjacob ("req %p:%u func %x already on pending list in mpt_send_cmd", 1220157662Smjacob req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function)); 1221147883Sscottl TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links); 1222147883Sscottl mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf); 1223101704Smjacob} 1224101704Smjacob 1225101704Smjacob/* 1226147883Sscottl * Wait for a request to complete. 1227147883Sscottl * 1228147883Sscottl * Inputs: 1229147883Sscottl * mpt softc of controller executing request 1230147883Sscottl * req request to wait for 1231147883Sscottl * sleep_ok nonzero implies may sleep in this context 1232147883Sscottl * time_ms timeout in ms. 0 implies no timeout. 1233147883Sscottl * 1234147883Sscottl * Return Values: 1235147883Sscottl * 0 Request completed 1236147883Sscottl * non-0 Timeout fired before request completion. 1237101704Smjacob */ 1238147883Sscottlint 1239147883Sscottlmpt_wait_req(struct mpt_softc *mpt, request_t *req, 1240147883Sscottl mpt_req_state_t state, mpt_req_state_t mask, 1241147883Sscottl int sleep_ok, int time_ms) 1242101704Smjacob{ 1243147883Sscottl int error; 1244147883Sscottl int timeout; 1245147883Sscottl u_int saved_cnt; 1246101704Smjacob 1247147883Sscottl /* 1248147883Sscottl * timeout is in ms. 0 indicates infinite wait. 1249147883Sscottl * Convert to ticks or 500us units depending on 1250147883Sscottl * our sleep mode. 1251147883Sscottl */ 1252157117Smjacob if (sleep_ok != 0) { 1253147883Sscottl timeout = (time_ms * hz) / 1000; 1254157117Smjacob } else { 1255147883Sscottl timeout = time_ms * 2; 1256157117Smjacob } 1257147883Sscottl req->state |= REQ_STATE_NEED_WAKEUP; 1258147883Sscottl mask &= ~REQ_STATE_NEED_WAKEUP; 1259155521Smjacob saved_cnt = mpt->reset_cnt; 1260157117Smjacob while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) { 1261147883Sscottl if (sleep_ok != 0) { 1262147883Sscottl error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout); 1263147883Sscottl if (error == EWOULDBLOCK) { 1264147883Sscottl timeout = 0; 1265147883Sscottl break; 1266147883Sscottl } 1267147883Sscottl } else { 1268147883Sscottl if (time_ms != 0 && --timeout == 0) { 1269147883Sscottl break; 1270147883Sscottl } 1271147883Sscottl DELAY(500); 1272147883Sscottl mpt_intr(mpt); 1273147883Sscottl } 1274147883Sscottl } 1275147883Sscottl req->state &= ~REQ_STATE_NEED_WAKEUP; 1276157117Smjacob if (mpt->reset_cnt != saved_cnt) { 1277147883Sscottl return (EIO); 1278157117Smjacob } 1279157117Smjacob if (time_ms && timeout <= 0) { 1280157117Smjacob MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf; 1281157117Smjacob mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function); 1282147883Sscottl return (ETIMEDOUT); 1283157117Smjacob } 1284147883Sscottl return (0); 1285101704Smjacob} 1286101704Smjacob 1287101704Smjacob/* 1288101704Smjacob * Send a command to the IOC via the handshake register. 1289101704Smjacob * 1290101704Smjacob * Only done at initialization time and for certain unusual 1291101704Smjacob * commands such as device/bus reset as specified by LSI. 1292101704Smjacob */ 1293101704Smjacobint 1294147883Sscottlmpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd) 1295101704Smjacob{ 1296101704Smjacob int i; 1297147883Sscottl uint32_t data, *data32; 1298101704Smjacob 1299101704Smjacob /* Check condition of the IOC */ 1300101704Smjacob data = mpt_rd_db(mpt); 1301147883Sscottl if ((MPT_STATE(data) != MPT_DB_STATE_READY 1302147883Sscottl && MPT_STATE(data) != MPT_DB_STATE_RUNNING 1303147883Sscottl && MPT_STATE(data) != MPT_DB_STATE_FAULT) 1304147883Sscottl || MPT_DB_IS_IN_USE(data)) { 1305147883Sscottl mpt_prt(mpt, "handshake aborted - invalid doorbell state\n"); 1306101704Smjacob mpt_print_db(data); 1307147883Sscottl return (EBUSY); 1308101704Smjacob } 1309101704Smjacob 1310101704Smjacob /* We move things in 32 bit chunks */ 1311101704Smjacob len = (len + 3) >> 2; 1312101704Smjacob data32 = cmd; 1313101704Smjacob 1314101704Smjacob /* Clear any left over pending doorbell interupts */ 1315101704Smjacob if (MPT_DB_INTR(mpt_rd_intr(mpt))) 1316101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1317101704Smjacob 1318101704Smjacob /* 1319101704Smjacob * Tell the handshake reg. we are going to send a command 1320101704Smjacob * and how long it is going to be. 1321101704Smjacob */ 1322101704Smjacob data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) | 1323101704Smjacob (len << MPI_DOORBELL_ADD_DWORDS_SHIFT); 1324101704Smjacob mpt_write(mpt, MPT_OFFSET_DOORBELL, data); 1325101704Smjacob 1326101704Smjacob /* Wait for the chip to notice */ 1327101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1328147883Sscottl mpt_prt(mpt, "mpt_send_handshake_cmd timeout1\n"); 1329147883Sscottl return (ETIMEDOUT); 1330101704Smjacob } 1331101704Smjacob 1332101704Smjacob /* Clear the interrupt */ 1333101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1334101704Smjacob 1335101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 1336147883Sscottl mpt_prt(mpt, "mpt_send_handshake_cmd timeout2\n"); 1337147883Sscottl return (ETIMEDOUT); 1338101704Smjacob } 1339101704Smjacob 1340101704Smjacob /* Send the command */ 1341101704Smjacob for (i = 0; i < len; i++) { 1342101704Smjacob mpt_write(mpt, MPT_OFFSET_DOORBELL, *data32++); 1343101704Smjacob if (mpt_wait_db_ack(mpt) != MPT_OK) { 1344156104Smjacob mpt_prt(mpt, 1345147883Sscottl "mpt_send_handshake_cmd timeout! index = %d\n", 1346147883Sscottl i); 1347147883Sscottl return (ETIMEDOUT); 1348101704Smjacob } 1349101704Smjacob } 1350101704Smjacob return MPT_OK; 1351101704Smjacob} 1352101704Smjacob 1353101704Smjacob/* Get the response from the handshake register */ 1354101704Smjacobint 1355147883Sscottlmpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply) 1356101704Smjacob{ 1357101704Smjacob int left, reply_left; 1358101704Smjacob u_int16_t *data16; 1359101704Smjacob MSG_DEFAULT_REPLY *hdr; 1360101704Smjacob 1361101704Smjacob /* We move things out in 16 bit chunks */ 1362101704Smjacob reply_len >>= 1; 1363101704Smjacob data16 = (u_int16_t *)reply; 1364101704Smjacob 1365101704Smjacob hdr = (MSG_DEFAULT_REPLY *)reply; 1366101704Smjacob 1367101704Smjacob /* Get first word */ 1368101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1369147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n"); 1370101704Smjacob return ETIMEDOUT; 1371101704Smjacob } 1372101704Smjacob *data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK; 1373101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1374101704Smjacob 1375101704Smjacob /* Get Second Word */ 1376101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1377147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n"); 1378101704Smjacob return ETIMEDOUT; 1379101704Smjacob } 1380101704Smjacob *data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK; 1381101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1382101704Smjacob 1383157354Smjacob /* 1384157354Smjacob * With the second word, we can now look at the length. 1385157354Smjacob * Warn about a reply that's too short (except for IOC FACTS REPLY) 1386157354Smjacob */ 1387157354Smjacob if ((reply_len >> 1) != hdr->MsgLength && 1388157354Smjacob (hdr->Function != MPI_FUNCTION_IOC_FACTS)){ 1389157354Smjacob#if __FreeBSD_version >= 500000 1390103914Smjacob mpt_prt(mpt, "reply length does not match message length: " 1391157382Smjacob "got %x; expected %zx for function %x\n", 1392157354Smjacob hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1393157354Smjacob#else 1394157354Smjacob mpt_prt(mpt, "reply length does not match message length: " 1395157382Smjacob "got %x; expected %x for function %x\n", 1396157354Smjacob hdr->MsgLength << 2, reply_len << 1, hdr->Function); 1397157354Smjacob#endif 1398101704Smjacob } 1399101704Smjacob 1400101704Smjacob /* Get rest of the reply; but don't overflow the provided buffer */ 1401101704Smjacob left = (hdr->MsgLength << 1) - 2; 1402101704Smjacob reply_left = reply_len - 2; 1403101704Smjacob while (left--) { 1404101704Smjacob u_int16_t datum; 1405101704Smjacob 1406101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1407147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n"); 1408101704Smjacob return ETIMEDOUT; 1409101704Smjacob } 1410101704Smjacob datum = mpt_read(mpt, MPT_OFFSET_DOORBELL); 1411101704Smjacob 1412101704Smjacob if (reply_left-- > 0) 1413101704Smjacob *data16++ = datum & MPT_DB_DATA_MASK; 1414101704Smjacob 1415101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1416101704Smjacob } 1417101704Smjacob 1418101704Smjacob /* One more wait & clear at the end */ 1419101704Smjacob if (mpt_wait_db_int(mpt) != MPT_OK) { 1420147883Sscottl mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n"); 1421101704Smjacob return ETIMEDOUT; 1422101704Smjacob } 1423101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0); 1424101704Smjacob 1425101704Smjacob if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1426147883Sscottl if (mpt->verbose >= MPT_PRT_TRACE) 1427101704Smjacob mpt_print_reply(hdr); 1428101704Smjacob return (MPT_FAIL | hdr->IOCStatus); 1429101704Smjacob } 1430101704Smjacob 1431101704Smjacob return (0); 1432101704Smjacob} 1433101704Smjacob 1434101704Smjacobstatic int 1435147883Sscottlmpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp) 1436101704Smjacob{ 1437101704Smjacob MSG_IOC_FACTS f_req; 1438101704Smjacob int error; 1439101704Smjacob 1440157354Smjacob memset(&f_req, 0, sizeof f_req); 1441101704Smjacob f_req.Function = MPI_FUNCTION_IOC_FACTS; 1442147883Sscottl f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1443101704Smjacob error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1444101704Smjacob if (error) 1445101704Smjacob return(error); 1446101704Smjacob error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1447101704Smjacob return (error); 1448101704Smjacob} 1449101704Smjacob 1450102199Smjacobstatic int 1451147883Sscottlmpt_get_portfacts(struct mpt_softc *mpt, MSG_PORT_FACTS_REPLY *freplp) 1452102199Smjacob{ 1453102199Smjacob MSG_PORT_FACTS f_req; 1454102199Smjacob int error; 1455102199Smjacob 1456102199Smjacob /* XXX: Only getting PORT FACTS for Port 0 */ 1457147883Sscottl memset(&f_req, 0, sizeof f_req); 1458102199Smjacob f_req.Function = MPI_FUNCTION_PORT_FACTS; 1459147883Sscottl f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1460102199Smjacob error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req); 1461102199Smjacob if (error) 1462102199Smjacob return(error); 1463102199Smjacob error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp); 1464102199Smjacob return (error); 1465102199Smjacob} 1466102199Smjacob 1467101704Smjacob/* 1468101704Smjacob * Send the initialization request. This is where we specify how many 1469101704Smjacob * SCSI busses and how many devices per bus we wish to emulate. 1470101704Smjacob * This is also the command that specifies the max size of the reply 1471101704Smjacob * frames from the IOC that we will be allocating. 1472101704Smjacob */ 1473101704Smjacobstatic int 1474147883Sscottlmpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who) 1475101704Smjacob{ 1476101704Smjacob int error = 0; 1477101704Smjacob MSG_IOC_INIT init; 1478101704Smjacob MSG_IOC_INIT_REPLY reply; 1479101704Smjacob 1480157354Smjacob memset(&init, 0, sizeof init); 1481101704Smjacob init.WhoInit = who; 1482101704Smjacob init.Function = MPI_FUNCTION_IOC_INIT; 1483101704Smjacob if (mpt->is_fc) { 1484101704Smjacob init.MaxDevices = 255; 1485155521Smjacob } else if (mpt->is_sas) { 1486155521Smjacob init.MaxDevices = mpt->mpt_max_devices; 1487101704Smjacob } else { 1488101704Smjacob init.MaxDevices = 16; 1489101704Smjacob } 1490101704Smjacob init.MaxBuses = 1; 1491155521Smjacob 1492155521Smjacob init.MsgVersion = htole16(MPI_VERSION); 1493155521Smjacob init.HeaderVersion = htole16(MPI_HEADER_VERSION); 1494155521Smjacob init.ReplyFrameSize = htole16(MPT_REPLY_SIZE); 1495147883Sscottl init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 1496101704Smjacob 1497101704Smjacob if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) { 1498101704Smjacob return(error); 1499101704Smjacob } 1500101704Smjacob 1501101704Smjacob error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply); 1502101704Smjacob return (error); 1503101704Smjacob} 1504101704Smjacob 1505102199Smjacob 1506102199Smjacob/* 1507102199Smjacob * Utiltity routine to read configuration headers and pages 1508102199Smjacob */ 1509147883Sscottlint 1510147883Sscottlmpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, u_int Action, 1511147883Sscottl u_int PageVersion, u_int PageLength, u_int PageNumber, 1512147883Sscottl u_int PageType, uint32_t PageAddress, bus_addr_t addr, 1513147883Sscottl bus_size_t len, int sleep_ok, int timeout_ms) 1514101704Smjacob{ 1515102199Smjacob MSG_CONFIG *cfgp; 1516147883Sscottl SGE_SIMPLE32 *se; 1517102199Smjacob 1518102199Smjacob cfgp = req->req_vbuf; 1519147883Sscottl memset(cfgp, 0, sizeof *cfgp); 1520147883Sscottl cfgp->Action = Action; 1521102199Smjacob cfgp->Function = MPI_FUNCTION_CONFIG; 1522147883Sscottl cfgp->Header.PageVersion = PageVersion; 1523147883Sscottl cfgp->Header.PageLength = PageLength; 1524147883Sscottl cfgp->Header.PageNumber = PageNumber; 1525147883Sscottl cfgp->Header.PageType = PageType; 1526102199Smjacob cfgp->PageAddress = PageAddress; 1527147883Sscottl se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE; 1528147883Sscottl se->Address = addr; 1529147883Sscottl MPI_pSGE_SET_LENGTH(se, len); 1530147883Sscottl MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT | 1531147883Sscottl MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER | 1532147883Sscottl MPI_SGE_FLAGS_END_OF_LIST | 1533147883Sscottl ((Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT 1534147883Sscottl || Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM) 1535147883Sscottl ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST))); 1536147883Sscottl cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 1537102199Smjacob 1538102199Smjacob mpt_check_doorbell(mpt); 1539102199Smjacob mpt_send_cmd(mpt, req); 1540147883Sscottl return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 1541147883Sscottl sleep_ok, timeout_ms)); 1542147883Sscottl} 1543102199Smjacob 1544147883Sscottl 1545147883Sscottlint 1546147883Sscottlmpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber, 1547147883Sscottl uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt, 1548147883Sscottl int sleep_ok, int timeout_ms) 1549147883Sscottl{ 1550147883Sscottl request_t *req; 1551156104Smjacob MSG_CONFIG *cfgp; 1552147883Sscottl int error; 1553147883Sscottl 1554147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1555147883Sscottl if (req == NULL) { 1556147883Sscottl mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n"); 1557156104Smjacob return (ENOMEM); 1558102199Smjacob } 1559147883Sscottl 1560147883Sscottl error = mpt_issue_cfg_req(mpt, req, MPI_CONFIG_ACTION_PAGE_HEADER, 1561147883Sscottl /*PageVersion*/0, /*PageLength*/0, PageNumber, 1562147883Sscottl PageType, PageAddress, /*addr*/0, /*len*/0, 1563147883Sscottl sleep_ok, timeout_ms); 1564147883Sscottl if (error != 0) { 1565156104Smjacob mpt_free_request(mpt, req); 1566147883Sscottl mpt_prt(mpt, "read_cfg_header timed out\n"); 1567156104Smjacob return (ETIMEDOUT); 1568147883Sscottl } 1569147883Sscottl 1570156104Smjacob switch (req->IOCStatus & MPI_IOCSTATUS_MASK) { 1571156104Smjacob case MPI_IOCSTATUS_SUCCESS: 1572147883Sscottl cfgp = req->req_vbuf; 1573147883Sscottl bcopy(&cfgp->Header, rslt, sizeof(*rslt)); 1574147883Sscottl error = 0; 1575156104Smjacob break; 1576156104Smjacob case MPI_IOCSTATUS_CONFIG_INVALID_PAGE: 1577156104Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 1578156104Smjacob "Invalid Page Type %d Number %d Addr 0x%0x\n", 1579156104Smjacob PageType, PageNumber, PageAddress); 1580156104Smjacob error = EINVAL; 1581156104Smjacob break; 1582156104Smjacob default: 1583156104Smjacob mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n", 1584156104Smjacob req->IOCStatus); 1585156104Smjacob error = EIO; 1586156104Smjacob break; 1587147883Sscottl } 1588102199Smjacob mpt_free_request(mpt, req); 1589147883Sscottl return (error); 1590102199Smjacob} 1591102199Smjacob 1592102822Smjacobint 1593147883Sscottlmpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1594147883Sscottl CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1595147883Sscottl int timeout_ms) 1596102199Smjacob{ 1597147883Sscottl request_t *req; 1598147883Sscottl int error; 1599102199Smjacob 1600147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1601147883Sscottl if (req == NULL) { 1602147883Sscottl mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n"); 1603147883Sscottl return (-1); 1604147883Sscottl } 1605102199Smjacob 1606147883Sscottl error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion, 1607147883Sscottl hdr->PageLength, hdr->PageNumber, 1608147883Sscottl hdr->PageType & MPI_CONFIG_PAGETYPE_MASK, 1609157117Smjacob PageAddress, req->req_pbuf + MPT_RQSL(mpt), 1610147883Sscottl len, sleep_ok, timeout_ms); 1611147883Sscottl if (error != 0) { 1612147883Sscottl mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action); 1613147883Sscottl return (-1); 1614147883Sscottl } 1615102199Smjacob 1616147883Sscottl if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1617147883Sscottl mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n", 1618147883Sscottl req->IOCStatus); 1619147883Sscottl mpt_free_request(mpt, req); 1620102199Smjacob return (-1); 1621102199Smjacob } 1622102199Smjacob bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap, 1623102199Smjacob BUS_DMASYNC_POSTREAD); 1624157117Smjacob memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len); 1625102199Smjacob mpt_free_request(mpt, req); 1626102199Smjacob return (0); 1627102199Smjacob} 1628102199Smjacob 1629102822Smjacobint 1630147883Sscottlmpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress, 1631147883Sscottl CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok, 1632147883Sscottl int timeout_ms) 1633102199Smjacob{ 1634147883Sscottl request_t *req; 1635147883Sscottl u_int hdr_attr; 1636147883Sscottl int error; 1637102199Smjacob 1638102199Smjacob hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK; 1639102199Smjacob if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE && 1640102199Smjacob hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) { 1641147883Sscottl mpt_prt(mpt, "page type 0x%x not changeable\n", 1642147883Sscottl hdr->PageType & MPI_CONFIG_PAGETYPE_MASK); 1643102199Smjacob return (-1); 1644102199Smjacob } 1645147883Sscottl hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK, 1646102199Smjacob 1647147883Sscottl req = mpt_get_request(mpt, sleep_ok); 1648147883Sscottl if (req == NULL) 1649147883Sscottl return (-1); 1650102199Smjacob 1651157117Smjacob memcpy(((caddr_t)req->req_vbuf)+MPT_RQSL(mpt), hdr, len); 1652147883Sscottl /* Restore stripped out attributes */ 1653147883Sscottl hdr->PageType |= hdr_attr; 1654102199Smjacob 1655147883Sscottl error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion, 1656147883Sscottl hdr->PageLength, hdr->PageNumber, 1657147883Sscottl hdr->PageType & MPI_CONFIG_PAGETYPE_MASK, 1658157117Smjacob PageAddress, req->req_pbuf + MPT_RQSL(mpt), 1659147883Sscottl len, sleep_ok, timeout_ms); 1660147883Sscottl if (error != 0) { 1661147883Sscottl mpt_prt(mpt, "mpt_write_cfg_page timed out\n"); 1662147883Sscottl return (-1); 1663147883Sscottl } 1664102199Smjacob 1665147883Sscottl if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) { 1666147883Sscottl mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n", 1667147883Sscottl req->IOCStatus); 1668147883Sscottl mpt_free_request(mpt, req); 1669147883Sscottl return (-1); 1670102199Smjacob } 1671147883Sscottl mpt_free_request(mpt, req); 1672147883Sscottl return (0); 1673147883Sscottl} 1674102199Smjacob 1675147883Sscottl/* 1676147883Sscottl * Read IOC configuration information 1677147883Sscottl */ 1678147883Sscottlstatic int 1679147883Sscottlmpt_read_config_info_ioc(struct mpt_softc *mpt) 1680147883Sscottl{ 1681147883Sscottl CONFIG_PAGE_HEADER hdr; 1682147883Sscottl struct mpt_raid_volume *mpt_raid; 1683147883Sscottl int rv; 1684147883Sscottl int i; 1685147883Sscottl size_t len; 1686147883Sscottl 1687147883Sscottl rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 1688158982Smjacob 2, 0, &hdr, FALSE, 5000); 1689156104Smjacob /* 1690156104Smjacob * If it's an invalid page, so what? Not a supported function.... 1691156104Smjacob */ 1692158982Smjacob if (rv == EINVAL) { 1693156104Smjacob return (0); 1694158982Smjacob } 1695158982Smjacob if (rv) { 1696156104Smjacob return (rv); 1697158982Smjacob } 1698147883Sscottl 1699157354Smjacob#if __FreeBSD_version >= 500000 1700157141Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 2 Header: ver %x, len %zx, " 1701147883Sscottl "num %x, type %x\n", hdr.PageVersion, 1702147883Sscottl hdr.PageLength * sizeof(uint32_t), 1703147883Sscottl hdr.PageNumber, hdr.PageType); 1704157354Smjacob#else 1705157354Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 2 Header: ver %x, len %z, " 1706157354Smjacob "num %x, type %x\n", hdr.PageVersion, 1707157354Smjacob hdr.PageLength * sizeof(uint32_t), 1708157354Smjacob hdr.PageNumber, hdr.PageType); 1709157354Smjacob#endif 1710147883Sscottl 1711147883Sscottl len = hdr.PageLength * sizeof(uint32_t); 1712151075Sscottl mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1713158982Smjacob if (mpt->ioc_page2 == NULL) { 1714158982Smjacob mpt_prt(mpt, "unable to allocate memory for IOC page 2\n"); 1715158982Smjacob mpt_raid_free_mem(mpt); 1716147883Sscottl return (ENOMEM); 1717158982Smjacob } 1718147883Sscottl memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr)); 1719158982Smjacob rv = mpt_read_cur_cfg_page(mpt, 0, 1720158982Smjacob &mpt->ioc_page2->Header, len, FALSE, 5000); 1721147883Sscottl if (rv) { 1722147883Sscottl mpt_prt(mpt, "failed to read IOC Page 2\n"); 1723158982Smjacob mpt_raid_free_mem(mpt); 1724158982Smjacob return (EIO); 1725158982Smjacob } 1726158982Smjacob 1727158982Smjacob if (mpt->ioc_page2->CapabilitiesFlags != 0) { 1728147883Sscottl uint32_t mask; 1729147883Sscottl 1730147883Sscottl mpt_prt(mpt, "Capabilities: ("); 1731147883Sscottl for (mask = 1; mask != 0; mask <<= 1) { 1732158982Smjacob if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) { 1733147883Sscottl continue; 1734158982Smjacob } 1735147883Sscottl switch (mask) { 1736147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT: 1737147883Sscottl mpt_prtc(mpt, " RAID-0"); 1738147883Sscottl break; 1739147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT: 1740147883Sscottl mpt_prtc(mpt, " RAID-1E"); 1741147883Sscottl break; 1742147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT: 1743147883Sscottl mpt_prtc(mpt, " RAID-1"); 1744147883Sscottl break; 1745147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT: 1746147883Sscottl mpt_prtc(mpt, " SES"); 1747147883Sscottl break; 1748147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT: 1749147883Sscottl mpt_prtc(mpt, " SAFTE"); 1750147883Sscottl break; 1751147883Sscottl case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT: 1752147883Sscottl mpt_prtc(mpt, " Multi-Channel-Arrays"); 1753147883Sscottl default: 1754147883Sscottl break; 1755147883Sscottl } 1756102199Smjacob } 1757147883Sscottl mpt_prtc(mpt, " )\n"); 1758147883Sscottl if ((mpt->ioc_page2->CapabilitiesFlags 1759147883Sscottl & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT 1760147883Sscottl | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT 1761147883Sscottl | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) { 1762147883Sscottl mpt_prt(mpt, "%d Active Volume%s(%d Max)\n", 1763147883Sscottl mpt->ioc_page2->NumActiveVolumes, 1764147883Sscottl mpt->ioc_page2->NumActiveVolumes != 1 1765147883Sscottl ? "s " : " ", 1766147883Sscottl mpt->ioc_page2->MaxVolumes); 1767147883Sscottl mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n", 1768147883Sscottl mpt->ioc_page2->NumActivePhysDisks, 1769147883Sscottl mpt->ioc_page2->NumActivePhysDisks != 1 1770147883Sscottl ? "s " : " ", 1771147883Sscottl mpt->ioc_page2->MaxPhysDisks); 1772147883Sscottl } 1773147883Sscottl } 1774102199Smjacob 1775147883Sscottl len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume); 1776158982Smjacob mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1777147883Sscottl if (mpt->raid_volumes == NULL) { 1778147883Sscottl mpt_prt(mpt, "Could not allocate RAID volume data\n"); 1779158982Smjacob mpt_raid_free_mem(mpt); 1780158982Smjacob return (ENOMEM); 1781147883Sscottl } 1782147883Sscottl 1783147883Sscottl /* 1784147883Sscottl * Copy critical data out of ioc_page2 so that we can 1785147883Sscottl * safely refresh the page without windows of unreliable 1786147883Sscottl * data. 1787147883Sscottl */ 1788147883Sscottl mpt->raid_max_volumes = mpt->ioc_page2->MaxVolumes; 1789147883Sscottl 1790158982Smjacob len = sizeof(*mpt->raid_volumes->config_page) + 1791158982Smjacob (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1)); 1792147883Sscottl for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) { 1793147883Sscottl mpt_raid = &mpt->raid_volumes[i]; 1794158982Smjacob mpt_raid->config_page = 1795158982Smjacob malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1796147883Sscottl if (mpt_raid->config_page == NULL) { 1797147883Sscottl mpt_prt(mpt, "Could not allocate RAID page data\n"); 1798158982Smjacob mpt_raid_free_mem(mpt); 1799158982Smjacob return (ENOMEM); 1800147883Sscottl } 1801147883Sscottl } 1802147883Sscottl mpt->raid_page0_len = len; 1803147883Sscottl 1804147883Sscottl len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk); 1805158982Smjacob mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1806147883Sscottl if (mpt->raid_disks == NULL) { 1807147883Sscottl mpt_prt(mpt, "Could not allocate RAID disk data\n"); 1808158982Smjacob mpt_raid_free_mem(mpt); 1809158982Smjacob return (ENOMEM); 1810147883Sscottl } 1811147883Sscottl mpt->raid_max_disks = mpt->ioc_page2->MaxPhysDisks; 1812147883Sscottl 1813158982Smjacob /* 1814158982Smjacob * Load page 3. 1815158982Smjacob */ 1816147883Sscottl rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC, 1817158982Smjacob 3, 0, &hdr, FALSE, 5000); 1818158982Smjacob if (rv) { 1819158982Smjacob mpt_raid_free_mem(mpt); 1820147883Sscottl return (EIO); 1821158982Smjacob } 1822147883Sscottl 1823147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n", 1824158982Smjacob hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType); 1825147883Sscottl 1826147883Sscottl len = hdr.PageLength * sizeof(uint32_t); 1827151075Sscottl mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); 1828158982Smjacob if (mpt->ioc_page3 == NULL) { 1829158982Smjacob mpt_prt(mpt, "unable to allocate memory for IOC page 3\n"); 1830158982Smjacob mpt_raid_free_mem(mpt); 1831158982Smjacob return (ENOMEM); 1832158982Smjacob } 1833147883Sscottl memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr)); 1834158982Smjacob rv = mpt_read_cur_cfg_page(mpt, 0, 1835158982Smjacob &mpt->ioc_page3->Header, len, FALSE, 5000); 1836147883Sscottl if (rv) { 1837158982Smjacob mpt_raid_free_mem(mpt); 1838158982Smjacob return (EIO); 1839102199Smjacob } 1840147883Sscottl mpt_raid_wakeup(mpt); 1841102199Smjacob return (0); 1842102199Smjacob} 1843102199Smjacob 1844102199Smjacob/* 1845102199Smjacob * Enable IOC port 1846102199Smjacob */ 1847102199Smjacobstatic int 1848147883Sscottlmpt_send_port_enable(struct mpt_softc *mpt, int port) 1849102199Smjacob{ 1850147883Sscottl request_t *req; 1851101704Smjacob MSG_PORT_ENABLE *enable_req; 1852147883Sscottl int error; 1853101704Smjacob 1854147883Sscottl req = mpt_get_request(mpt, /*sleep_ok*/FALSE); 1855147883Sscottl if (req == NULL) 1856147883Sscottl return (-1); 1857101704Smjacob 1858101704Smjacob enable_req = req->req_vbuf; 1859157354Smjacob memset(enable_req, 0, MPT_RQSL(mpt)); 1860101704Smjacob 1861101704Smjacob enable_req->Function = MPI_FUNCTION_PORT_ENABLE; 1862147883Sscottl enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG); 1863101704Smjacob enable_req->PortNumber = port; 1864101704Smjacob 1865101704Smjacob mpt_check_doorbell(mpt); 1866147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port); 1867147883Sscottl 1868147883Sscottl mpt_send_cmd(mpt, req); 1869147883Sscottl error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 1870157662Smjacob FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000); 1871147883Sscottl if (error != 0) { 1872157117Smjacob mpt_prt(mpt, "port %d enable timed out\n", port); 1873147883Sscottl return (-1); 1874101704Smjacob } 1875101704Smjacob mpt_free_request(mpt, req); 1876157117Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port); 1877101704Smjacob return (0); 1878101704Smjacob} 1879101704Smjacob 1880101704Smjacob/* 1881101704Smjacob * Enable/Disable asynchronous event reporting. 1882101704Smjacob */ 1883101704Smjacobstatic int 1884147883Sscottlmpt_send_event_request(struct mpt_softc *mpt, int onoff) 1885101704Smjacob{ 1886101704Smjacob request_t *req; 1887101704Smjacob MSG_EVENT_NOTIFY *enable_req; 1888101704Smjacob 1889157354Smjacob req = mpt_get_request(mpt, FALSE); 1890157354Smjacob if (req == NULL) { 1891157354Smjacob return (ENOMEM); 1892157354Smjacob } 1893101704Smjacob enable_req = req->req_vbuf; 1894157354Smjacob memset(enable_req, 0, sizeof *enable_req); 1895101704Smjacob 1896101704Smjacob enable_req->Function = MPI_FUNCTION_EVENT_NOTIFICATION; 1897147883Sscottl enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS); 1898101704Smjacob enable_req->Switch = onoff; 1899101704Smjacob 1900101704Smjacob mpt_check_doorbell(mpt); 1901157354Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n", 1902157354Smjacob onoff ? "en" : "dis"); 1903157354Smjacob /* 1904157354Smjacob * Send the command off, but don't wait for it. 1905157354Smjacob */ 1906101704Smjacob mpt_send_cmd(mpt, req); 1907101704Smjacob return (0); 1908101704Smjacob} 1909101704Smjacob 1910101704Smjacob/* 1911101704Smjacob * Un-mask the interupts on the chip. 1912101704Smjacob */ 1913101704Smjacobvoid 1914147883Sscottlmpt_enable_ints(struct mpt_softc *mpt) 1915101704Smjacob{ 1916101704Smjacob /* Unmask every thing except door bell int */ 1917101704Smjacob mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK); 1918101704Smjacob} 1919101704Smjacob 1920101704Smjacob/* 1921101704Smjacob * Mask the interupts on the chip. 1922101704Smjacob */ 1923101704Smjacobvoid 1924147883Sscottlmpt_disable_ints(struct mpt_softc *mpt) 1925101704Smjacob{ 1926101704Smjacob /* Mask all interrupts */ 1927156104Smjacob mpt_write(mpt, MPT_OFFSET_INTR_MASK, 1928101704Smjacob MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK); 1929101704Smjacob} 1930101704Smjacob 1931147883Sscottlstatic void 1932147883Sscottlmpt_sysctl_attach(struct mpt_softc *mpt) 1933147883Sscottl{ 1934157117Smjacob#if __FreeBSD_version >= 500000 1935147883Sscottl struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev); 1936147883Sscottl struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev); 1937147883Sscottl 1938156104Smjacob SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 1939147883Sscottl "debug", CTLFLAG_RW, &mpt->verbose, 0, 1940147883Sscottl "Debugging/Verbose level"); 1941157117Smjacob#endif 1942147883Sscottl} 1943147883Sscottl 1944101704Smjacobint 1945147883Sscottlmpt_attach(struct mpt_softc *mpt) 1946101704Smjacob{ 1947157117Smjacob struct mpt_personality *pers; 1948147883Sscottl int i; 1949157117Smjacob int error; 1950147883Sscottl 1951147883Sscottl for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 1952147883Sscottl pers = mpt_personalities[i]; 1953157117Smjacob if (pers == NULL) { 1954147883Sscottl continue; 1955157117Smjacob } 1956147883Sscottl if (pers->probe(mpt) == 0) { 1957147883Sscottl error = pers->attach(mpt); 1958147883Sscottl if (error != 0) { 1959147883Sscottl mpt_detach(mpt); 1960147883Sscottl return (error); 1961147883Sscottl } 1962147883Sscottl mpt->mpt_pers_mask |= (0x1 << pers->id); 1963147883Sscottl pers->use_count++; 1964147883Sscottl } 1965147883Sscottl } 1966155521Smjacob 1967157117Smjacob /* 1968157117Smjacob * Now that we've attached everything, do the enable function 1969157117Smjacob * for all of the personalities. This allows the personalities 1970157117Smjacob * to do setups that are appropriate for them prior to enabling 1971157117Smjacob * any ports. 1972157117Smjacob */ 1973157117Smjacob for (i = 0; i < MPT_MAX_PERSONALITIES; i++) { 1974157117Smjacob pers = mpt_personalities[i]; 1975157117Smjacob if (pers != NULL && MPT_PERS_ATTACHED(pers, mpt) != 0) { 1976157117Smjacob error = pers->enable(mpt); 1977157117Smjacob if (error != 0) { 1978157117Smjacob mpt_prt(mpt, "personality %s attached but would" 1979157117Smjacob " not enable (%d)\n", pers->name, error); 1980157117Smjacob mpt_detach(mpt); 1981157117Smjacob return (error); 1982157117Smjacob } 1983157117Smjacob } 1984157117Smjacob } 1985147883Sscottl return (0); 1986147883Sscottl} 1987147883Sscottl 1988147883Sscottlint 1989147883Sscottlmpt_shutdown(struct mpt_softc *mpt) 1990147883Sscottl{ 1991147883Sscottl struct mpt_personality *pers; 1992147883Sscottl 1993157117Smjacob MPT_PERS_FOREACH_REVERSE(mpt, pers) { 1994147883Sscottl pers->shutdown(mpt); 1995157117Smjacob } 1996147883Sscottl return (0); 1997147883Sscottl} 1998147883Sscottl 1999147883Sscottlint 2000147883Sscottlmpt_detach(struct mpt_softc *mpt) 2001147883Sscottl{ 2002147883Sscottl struct mpt_personality *pers; 2003147883Sscottl 2004147883Sscottl MPT_PERS_FOREACH_REVERSE(mpt, pers) { 2005147883Sscottl pers->detach(mpt); 2006147883Sscottl mpt->mpt_pers_mask &= ~(0x1 << pers->id); 2007147883Sscottl pers->use_count--; 2008147883Sscottl } 2009147883Sscottl 2010147883Sscottl return (0); 2011147883Sscottl} 2012147883Sscottl 2013147883Sscottlint 2014147883Sscottlmpt_core_load(struct mpt_personality *pers) 2015147883Sscottl{ 2016147883Sscottl int i; 2017147883Sscottl 2018147883Sscottl /* 2019147883Sscottl * Setup core handlers and insert the default handler 2020147883Sscottl * into all "empty slots". 2021147883Sscottl */ 2022157117Smjacob for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) { 2023147883Sscottl mpt_reply_handlers[i] = mpt_default_reply_handler; 2024157117Smjacob } 2025147883Sscottl 2026147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] = 2027147883Sscottl mpt_event_reply_handler; 2028147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] = 2029147883Sscottl mpt_config_reply_handler; 2030147883Sscottl mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] = 2031147883Sscottl mpt_handshake_reply_handler; 2032147883Sscottl return (0); 2033147883Sscottl} 2034147883Sscottl 2035147883Sscottl/* 2036147883Sscottl * Initialize per-instance driver data and perform 2037147883Sscottl * initial controller configuration. 2038147883Sscottl */ 2039147883Sscottlint 2040147883Sscottlmpt_core_attach(struct mpt_softc *mpt) 2041147883Sscottl{ 2042101704Smjacob int val; 2043147883Sscottl int error; 2044101704Smjacob 2045157117Smjacob 2046147883Sscottl LIST_INIT(&mpt->ack_frames); 2047147883Sscottl 2048147883Sscottl /* Put all request buffers on the free list */ 2049147883Sscottl TAILQ_INIT(&mpt->request_pending_list); 2050147883Sscottl TAILQ_INIT(&mpt->request_free_list); 2051157354Smjacob TAILQ_INIT(&mpt->request_timeout_list); 2052157117Smjacob for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) { 2053157354Smjacob request_t *req = &mpt->request_pool[val]; 2054157354Smjacob req->state = REQ_STATE_ALLOCATED; 2055157354Smjacob mpt_free_request(mpt, req); 2056157117Smjacob } 2057147883Sscottl 2058157117Smjacob for (val = 0; val < MPT_MAX_LUNS; val++) { 2059157117Smjacob STAILQ_INIT(&mpt->trt[val].atios); 2060157117Smjacob STAILQ_INIT(&mpt->trt[val].inots); 2061157117Smjacob } 2062157117Smjacob STAILQ_INIT(&mpt->trt_wildcard.atios); 2063157117Smjacob STAILQ_INIT(&mpt->trt_wildcard.inots); 2064157117Smjacob 2065157117Smjacob mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE; 2066157117Smjacob 2067147883Sscottl mpt_sysctl_attach(mpt); 2068147883Sscottl 2069147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n", 2070157117Smjacob mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL))); 2071147883Sscottl 2072147883Sscottl error = mpt_configure_ioc(mpt); 2073147883Sscottl 2074147883Sscottl return (error); 2075147883Sscottl} 2076147883Sscottl 2077157117Smjacobint 2078157117Smjacobmpt_core_enable(struct mpt_softc *mpt) 2079157117Smjacob{ 2080157117Smjacob /* 2081157117Smjacob * We enter with the IOC enabled, but async events 2082157117Smjacob * not enabled, ports not enabled and interrupts 2083157117Smjacob * not enabled. 2084157117Smjacob */ 2085157117Smjacob 2086157117Smjacob /* 2087157117Smjacob * Enable asynchronous event reporting- all personalities 2088157117Smjacob * have attached so that they should be able to now field 2089157117Smjacob * async events. 2090157117Smjacob */ 2091157117Smjacob mpt_send_event_request(mpt, 1); 2092157117Smjacob 2093157117Smjacob /* 2094157117Smjacob * Catch any pending interrupts 2095157117Smjacob * 2096157117Smjacob * This seems to be crucial- otherwise 2097157117Smjacob * the portenable below times out. 2098157117Smjacob */ 2099157117Smjacob mpt_intr(mpt); 2100157117Smjacob 2101157117Smjacob /* 2102157117Smjacob * Enable Interrupts 2103157117Smjacob */ 2104157117Smjacob mpt_enable_ints(mpt); 2105157117Smjacob 2106157117Smjacob /* 2107157117Smjacob * Catch any pending interrupts 2108157117Smjacob * 2109157117Smjacob * This seems to be crucial- otherwise 2110157117Smjacob * the portenable below times out. 2111157117Smjacob */ 2112157117Smjacob mpt_intr(mpt); 2113157117Smjacob 2114157117Smjacob /* 2115157662Smjacob * Enable the port. 2116157117Smjacob */ 2117157117Smjacob if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2118157117Smjacob mpt_prt(mpt, "failed to enable port 0\n"); 2119157117Smjacob return (ENXIO); 2120157117Smjacob } 2121157117Smjacob return (0); 2122157117Smjacob} 2123157117Smjacob 2124147883Sscottlvoid 2125147883Sscottlmpt_core_shutdown(struct mpt_softc *mpt) 2126147883Sscottl{ 2127157117Smjacob mpt_disable_ints(mpt); 2128147883Sscottl} 2129147883Sscottl 2130147883Sscottlvoid 2131147883Sscottlmpt_core_detach(struct mpt_softc *mpt) 2132147883Sscottl{ 2133157117Smjacob mpt_disable_ints(mpt); 2134147883Sscottl} 2135147883Sscottl 2136147883Sscottlint 2137147883Sscottlmpt_core_unload(struct mpt_personality *pers) 2138147883Sscottl{ 2139147883Sscottl /* Unload is always successfull. */ 2140147883Sscottl return (0); 2141147883Sscottl} 2142147883Sscottl 2143147883Sscottl#define FW_UPLOAD_REQ_SIZE \ 2144147883Sscottl (sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION) \ 2145147883Sscottl + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32)) 2146147883Sscottl 2147147883Sscottlstatic int 2148147883Sscottlmpt_upload_fw(struct mpt_softc *mpt) 2149147883Sscottl{ 2150147883Sscottl uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE]; 2151147883Sscottl MSG_FW_UPLOAD_REPLY fw_reply; 2152147883Sscottl MSG_FW_UPLOAD *fw_req; 2153147883Sscottl FW_UPLOAD_TCSGE *tsge; 2154147883Sscottl SGE_SIMPLE32 *sge; 2155147883Sscottl uint32_t flags; 2156147883Sscottl int error; 2157147883Sscottl 2158147883Sscottl memset(&fw_req_buf, 0, sizeof(fw_req_buf)); 2159147883Sscottl fw_req = (MSG_FW_UPLOAD *)fw_req_buf; 2160147883Sscottl fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM; 2161147883Sscottl fw_req->Function = MPI_FUNCTION_FW_UPLOAD; 2162147883Sscottl fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE); 2163147883Sscottl tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL; 2164147883Sscottl tsge->DetailsLength = 12; 2165147883Sscottl tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT; 2166147883Sscottl tsge->ImageSize = htole32(mpt->fw_image_size); 2167147883Sscottl sge = (SGE_SIMPLE32 *)(tsge + 1); 2168147883Sscottl flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER 2169147883Sscottl | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT 2170147883Sscottl | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST); 2171147883Sscottl flags <<= MPI_SGE_FLAGS_SHIFT; 2172147883Sscottl sge->FlagsLength = htole32(flags | mpt->fw_image_size); 2173147883Sscottl sge->Address = htole32(mpt->fw_phys); 2174147883Sscottl error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf); 2175147883Sscottl if (error) 2176147883Sscottl return(error); 2177147883Sscottl error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply); 2178147883Sscottl return (error); 2179147883Sscottl} 2180147883Sscottl 2181147883Sscottlstatic void 2182147883Sscottlmpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr, 2183147883Sscottl uint32_t *data, bus_size_t len) 2184147883Sscottl{ 2185147883Sscottl uint32_t *data_end; 2186147883Sscottl 2187147883Sscottl data_end = data + (roundup2(len, sizeof(uint32_t)) / 4); 2188155521Smjacob pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2189147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr); 2190147883Sscottl while (data != data_end) { 2191147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data); 2192147883Sscottl data++; 2193101704Smjacob } 2194155521Smjacob pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2195147883Sscottl} 2196101704Smjacob 2197147883Sscottlstatic int 2198147883Sscottlmpt_download_fw(struct mpt_softc *mpt) 2199147883Sscottl{ 2200147883Sscottl MpiFwHeader_t *fw_hdr; 2201147883Sscottl int error; 2202147883Sscottl uint32_t ext_offset; 2203147883Sscottl uint32_t data; 2204147883Sscottl 2205147883Sscottl mpt_prt(mpt, "Downloading Firmware - Image Size %d\n", 2206147883Sscottl mpt->fw_image_size); 2207147883Sscottl 2208147883Sscottl error = mpt_enable_diag_mode(mpt); 2209147883Sscottl if (error != 0) { 2210147883Sscottl mpt_prt(mpt, "Could not enter diagnostic mode!\n"); 2211147883Sscottl return (EIO); 2212101704Smjacob } 2213101704Smjacob 2214147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, 2215147883Sscottl MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM); 2216147883Sscottl 2217147883Sscottl fw_hdr = (MpiFwHeader_t *)mpt->fw_image; 2218147883Sscottl mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr, 2219147883Sscottl fw_hdr->ImageSize); 2220147883Sscottl 2221147883Sscottl ext_offset = fw_hdr->NextImageHeaderOffset; 2222147883Sscottl while (ext_offset != 0) { 2223147883Sscottl MpiExtImageHeader_t *ext; 2224147883Sscottl 2225147883Sscottl ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset); 2226147883Sscottl ext_offset = ext->NextImageHeaderOffset; 2227147883Sscottl 2228147883Sscottl mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext, 2229147883Sscottl ext->ImageSize); 2230147883Sscottl } 2231147883Sscottl 2232155521Smjacob pci_enable_io(mpt->dev, SYS_RES_IOPORT); 2233147883Sscottl /* Setup the address to jump to on reset. */ 2234147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr); 2235147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue); 2236147883Sscottl 2237101704Smjacob /* 2238147883Sscottl * The controller sets the "flash bad" status after attempting 2239147883Sscottl * to auto-boot from flash. Clear the status so that the controller 2240147883Sscottl * will continue the boot process with our newly installed firmware. 2241101704Smjacob */ 2242147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2243147883Sscottl data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL; 2244147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE); 2245147883Sscottl mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data); 2246147883Sscottl 2247155521Smjacob pci_disable_io(mpt->dev, SYS_RES_IOPORT); 2248155521Smjacob 2249147883Sscottl /* 2250147883Sscottl * Re-enable the processor and clear the boot halt flag. 2251147883Sscottl */ 2252147883Sscottl data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC); 2253147883Sscottl data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM); 2254147883Sscottl mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data); 2255147883Sscottl 2256147883Sscottl mpt_disable_diag_mode(mpt); 2257147883Sscottl return (0); 2258147883Sscottl} 2259147883Sscottl 2260147883Sscottl/* 2261147883Sscottl * Allocate/Initialize data structures for the controller. Called 2262147883Sscottl * once at instance startup. 2263147883Sscottl */ 2264147883Sscottlstatic int 2265147883Sscottlmpt_configure_ioc(struct mpt_softc *mpt) 2266147883Sscottl{ 2267147883Sscottl MSG_PORT_FACTS_REPLY pfp; 2268147883Sscottl MSG_IOC_FACTS_REPLY facts; 2269147883Sscottl int try; 2270147883Sscottl int needreset; 2271155521Smjacob uint32_t max_chain_depth; 2272147883Sscottl 2273147883Sscottl needreset = 0; 2274101704Smjacob for (try = 0; try < MPT_MAX_TRYS; try++) { 2275147883Sscottl 2276101704Smjacob /* 2277101704Smjacob * No need to reset if the IOC is already in the READY state. 2278101704Smjacob * 2279101704Smjacob * Force reset if initialization failed previously. 2280101704Smjacob * Note that a hard_reset of the second channel of a '929 2281101704Smjacob * will stop operation of the first channel. Hopefully, if the 2282156104Smjacob * first channel is ok, the second will not require a hard 2283101704Smjacob * reset. 2284101704Smjacob */ 2285157117Smjacob if (needreset || MPT_STATE(mpt_rd_db(mpt)) != 2286101704Smjacob MPT_DB_STATE_READY) { 2287157117Smjacob if (mpt_reset(mpt, FALSE) != MPT_OK) { 2288101704Smjacob continue; 2289157117Smjacob } 2290101704Smjacob } 2291147883Sscottl needreset = 0; 2292101704Smjacob 2293101704Smjacob if (mpt_get_iocfacts(mpt, &facts) != MPT_OK) { 2294147883Sscottl mpt_prt(mpt, "mpt_get_iocfacts failed\n"); 2295147883Sscottl needreset = 1; 2296101704Smjacob continue; 2297102199Smjacob } 2298102199Smjacob 2299147883Sscottl mpt->mpt_global_credits = le16toh(facts.GlobalCredits); 2300147883Sscottl mpt->request_frame_size = le16toh(facts.RequestFrameSize); 2301155521Smjacob mpt->ioc_facts_flags = facts.Flags; 2302147883Sscottl mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n", 2303147883Sscottl le16toh(facts.MsgVersion) >> 8, 2304147883Sscottl le16toh(facts.MsgVersion) & 0xFF, 2305147883Sscottl le16toh(facts.HeaderVersion) >> 8, 2306147883Sscottl le16toh(facts.HeaderVersion) & 0xFF); 2307155521Smjacob 2308155521Smjacob /* 2309155521Smjacob * Now that we know request frame size, we can calculate 2310155521Smjacob * the actual (reasonable) segment limit for read/write I/O. 2311155521Smjacob * 2312155521Smjacob * This limit is constrained by: 2313155521Smjacob * 2314155521Smjacob * + The size of each area we allocate per command (and how 2315155521Smjacob * many chain segments we can fit into it). 2316155521Smjacob * + The total number of areas we've set up. 2317155521Smjacob * + The actual chain depth the card will allow. 2318155521Smjacob * 2319155521Smjacob * The first area's segment count is limited by the I/O request 2320155521Smjacob * at the head of it. We cannot allocate realistically more 2321155521Smjacob * than MPT_MAX_REQUESTS areas. Therefore, to account for both 2322155521Smjacob * conditions, we'll just start out with MPT_MAX_REQUESTS-2. 2323155521Smjacob * 2324155521Smjacob */ 2325155521Smjacob max_chain_depth = facts.MaxChainDepth; 2326155521Smjacob 2327155521Smjacob /* total number of request areas we (can) allocate */ 2328155521Smjacob mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2; 2329155521Smjacob 2330155521Smjacob /* converted to the number of chain areas possible */ 2331155521Smjacob mpt->max_seg_cnt *= MPT_NRFM(mpt); 2332155521Smjacob 2333155521Smjacob /* limited by the number of chain areas the card will support */ 2334155521Smjacob if (mpt->max_seg_cnt > max_chain_depth) { 2335155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 2336155521Smjacob "chain depth limited to %u (from %u)\n", 2337155521Smjacob max_chain_depth, mpt->max_seg_cnt); 2338155521Smjacob mpt->max_seg_cnt = max_chain_depth; 2339155521Smjacob } 2340155521Smjacob 2341155521Smjacob /* converted to the number of simple sges in chain segments. */ 2342155521Smjacob mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1); 2343155521Smjacob 2344147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, 2345155521Smjacob "Maximum Segment Count: %u\n", mpt->max_seg_cnt); 2346155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, 2347147883Sscottl "MsgLength=%u IOCNumber = %d\n", 2348147883Sscottl facts.MsgLength, facts.IOCNumber); 2349147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, 2350155521Smjacob "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes " 2351155521Smjacob "Request Frame Size %u bytes Max Chain Depth %u\n", 2352155521Smjacob mpt->mpt_global_credits, facts.BlockSize, 2353155521Smjacob mpt->request_frame_size << 2, max_chain_depth); 2354147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, 2355147883Sscottl "IOCFACTS: Num Ports %d, FWImageSize %d, " 2356147883Sscottl "Flags=%#x\n", facts.NumberOfPorts, 2357147883Sscottl le32toh(facts.FWImageSize), facts.Flags); 2358147883Sscottl 2359155521Smjacob 2360147883Sscottl if ((facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) != 0) { 2361147883Sscottl struct mpt_map_info mi; 2362147883Sscottl int error; 2363147883Sscottl 2364147883Sscottl /* 2365147883Sscottl * In some configurations, the IOC's firmware is 2366147883Sscottl * stored in a shared piece of system NVRAM that 2367147883Sscottl * is only accessable via the BIOS. In this 2368147883Sscottl * case, the firmware keeps a copy of firmware in 2369147883Sscottl * RAM until the OS driver retrieves it. Once 2370147883Sscottl * retrieved, we are responsible for re-downloading 2371147883Sscottl * the firmware after any hard-reset. 2372147883Sscottl */ 2373147883Sscottl mpt->fw_image_size = le32toh(facts.FWImageSize); 2374147883Sscottl error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 2375147883Sscottl /*alignment*/1, /*boundary*/0, 2376147883Sscottl /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 2377147883Sscottl /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, 2378147883Sscottl /*filterarg*/NULL, mpt->fw_image_size, 2379147883Sscottl /*nsegments*/1, /*maxsegsz*/mpt->fw_image_size, 2380147883Sscottl /*flags*/0, &mpt->fw_dmat); 2381147883Sscottl if (error != 0) { 2382147883Sscottl mpt_prt(mpt, "cannot create fw dma tag\n"); 2383147883Sscottl return (ENOMEM); 2384147883Sscottl } 2385147883Sscottl error = bus_dmamem_alloc(mpt->fw_dmat, 2386147883Sscottl (void **)&mpt->fw_image, BUS_DMA_NOWAIT, 2387147883Sscottl &mpt->fw_dmap); 2388147883Sscottl if (error != 0) { 2389147883Sscottl mpt_prt(mpt, "cannot allocate fw mem.\n"); 2390147883Sscottl bus_dma_tag_destroy(mpt->fw_dmat); 2391147883Sscottl return (ENOMEM); 2392147883Sscottl } 2393147883Sscottl mi.mpt = mpt; 2394147883Sscottl mi.error = 0; 2395147883Sscottl bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap, 2396147883Sscottl mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, 2397147883Sscottl &mi, 0); 2398147883Sscottl mpt->fw_phys = mi.phys; 2399147883Sscottl 2400147883Sscottl error = mpt_upload_fw(mpt); 2401147883Sscottl if (error != 0) { 2402147883Sscottl mpt_prt(mpt, "fw upload failed.\n"); 2403147883Sscottl bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap); 2404147883Sscottl bus_dmamem_free(mpt->fw_dmat, mpt->fw_image, 2405147883Sscottl mpt->fw_dmap); 2406147883Sscottl bus_dma_tag_destroy(mpt->fw_dmat); 2407147883Sscottl mpt->fw_image = NULL; 2408147883Sscottl return (EIO); 2409147883Sscottl } 2410101704Smjacob } 2411101704Smjacob 2412102199Smjacob if (mpt_get_portfacts(mpt, &pfp) != MPT_OK) { 2413147883Sscottl mpt_prt(mpt, "mpt_get_portfacts failed\n"); 2414147883Sscottl needreset = 1; 2415102199Smjacob continue; 2416102199Smjacob } 2417102199Smjacob 2418147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, 2419147883Sscottl "PORTFACTS: Type %x PFlags %x IID %d MaxDev %d\n", 2420147883Sscottl pfp.PortType, pfp.ProtocolFlags, pfp.PortSCSIID, 2421147883Sscottl pfp.MaxDevices); 2422102199Smjacob 2423147883Sscottl mpt->mpt_port_type = pfp.PortType; 2424147883Sscottl mpt->mpt_proto_flags = pfp.ProtocolFlags; 2425102199Smjacob if (pfp.PortType != MPI_PORTFACTS_PORTTYPE_SCSI && 2426155521Smjacob pfp.PortType != MPI_PORTFACTS_PORTTYPE_SAS && 2427102199Smjacob pfp.PortType != MPI_PORTFACTS_PORTTYPE_FC) { 2428147883Sscottl mpt_prt(mpt, "Unsupported Port Type (%x)\n", 2429102199Smjacob pfp.PortType); 2430102199Smjacob return (ENXIO); 2431102199Smjacob } 2432157117Smjacob mpt->mpt_max_tgtcmds = le16toh(pfp.MaxPostedCmdBuffers); 2433157117Smjacob 2434102199Smjacob if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_FC) { 2435102199Smjacob mpt->is_fc = 1; 2436155521Smjacob mpt->is_sas = 0; 2437155521Smjacob } else if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_SAS) { 2438155521Smjacob mpt->is_fc = 0; 2439155521Smjacob mpt->is_sas = 1; 2440102199Smjacob } else { 2441102199Smjacob mpt->is_fc = 0; 2442155521Smjacob mpt->is_sas = 0; 2443102199Smjacob } 2444102199Smjacob mpt->mpt_ini_id = pfp.PortSCSIID; 2445155521Smjacob mpt->mpt_max_devices = pfp.MaxDevices; 2446102199Smjacob 2447157117Smjacob /* 2448157662Smjacob * Set our expected role with what this port supports. 2449157117Smjacob */ 2450157117Smjacob 2451157662Smjacob mpt->role = MPT_ROLE_NONE; 2452157117Smjacob if (pfp.ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) { 2453157662Smjacob mpt->role |= MPT_ROLE_INITIATOR; 2454157117Smjacob } 2455157117Smjacob if (pfp.ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) { 2456157662Smjacob mpt->role |= MPT_ROLE_TARGET; 2457157117Smjacob } 2458157662Smjacob if (mpt->role == MPT_ROLE_NONE) { 2459157117Smjacob mpt_prt(mpt, "port does not support either target or " 2460157117Smjacob "initiator role\n"); 2461147883Sscottl return (ENXIO); 2462102199Smjacob } 2463102199Smjacob 2464157117Smjacob if (mpt_enable_ioc(mpt, 0) != MPT_OK) { 2465157117Smjacob mpt_prt(mpt, "unable to initialize IOC\n"); 2466157117Smjacob return (ENXIO); 2467157117Smjacob } 2468157117Smjacob 2469101704Smjacob /* 2470157117Smjacob * Read IOC configuration information. 2471158982Smjacob * 2472158982Smjacob * We need this to determine whether or not we have certain 2473158982Smjacob * settings for Integrated Mirroring (e.g.). 2474101704Smjacob */ 2475147883Sscottl mpt_read_config_info_ioc(mpt); 2476101704Smjacob 2477101704Smjacob /* Everything worked */ 2478101704Smjacob break; 2479101704Smjacob } 2480101704Smjacob 2481101704Smjacob if (try >= MPT_MAX_TRYS) { 2482103914Smjacob mpt_prt(mpt, "failed to initialize IOC"); 2483101704Smjacob return (EIO); 2484101704Smjacob } 2485101704Smjacob 2486101704Smjacob return (0); 2487101704Smjacob} 2488147883Sscottl 2489147883Sscottlstatic int 2490157117Smjacobmpt_enable_ioc(struct mpt_softc *mpt, int portenable) 2491147883Sscottl{ 2492147883Sscottl uint32_t pptr; 2493147883Sscottl int val; 2494147883Sscottl 2495155521Smjacob if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) { 2496147883Sscottl mpt_prt(mpt, "mpt_send_ioc_init failed\n"); 2497147883Sscottl return (EIO); 2498147883Sscottl } 2499147883Sscottl 2500147883Sscottl mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n"); 2501147883Sscottl 2502147883Sscottl if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) { 2503147883Sscottl mpt_prt(mpt, "IOC failed to go to run state\n"); 2504147883Sscottl return (ENXIO); 2505147883Sscottl } 2506155521Smjacob mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n"); 2507147883Sscottl 2508147883Sscottl /* 2509147883Sscottl * Give it reply buffers 2510147883Sscottl * 2511147883Sscottl * Do *not* exceed global credits. 2512147883Sscottl */ 2513156104Smjacob for (val = 0, pptr = mpt->reply_phys; 2514156104Smjacob (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE); 2515147883Sscottl pptr += MPT_REPLY_SIZE) { 2516147883Sscottl mpt_free_reply(mpt, pptr); 2517147883Sscottl if (++val == mpt->mpt_global_credits - 1) 2518147883Sscottl break; 2519147883Sscottl } 2520147883Sscottl 2521157117Smjacob 2522147883Sscottl /* 2523157354Smjacob * Enable the port if asked. This is only done if we're resetting 2524157354Smjacob * the IOC after initial startup. 2525147883Sscottl */ 2526157117Smjacob if (portenable) { 2527157117Smjacob /* 2528157117Smjacob * Enable asynchronous event reporting 2529157117Smjacob */ 2530157117Smjacob mpt_send_event_request(mpt, 1); 2531147883Sscottl 2532157117Smjacob if (mpt_send_port_enable(mpt, 0) != MPT_OK) { 2533157117Smjacob mpt_prt(mpt, "failed to enable port 0\n"); 2534157117Smjacob return (ENXIO); 2535157117Smjacob } 2536147883Sscottl } 2537156104Smjacob return (MPT_OK); 2538147883Sscottl} 2539