1665484d8SDoug Ambrisko /*
2ecea5be4SKashyap D Desai * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Marian Choy
38e727371SKashyap D Desai * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
4ecea5be4SKashyap D Desai * Support: freebsdraid@avagotech.com
5665484d8SDoug Ambrisko *
6665484d8SDoug Ambrisko * Redistribution and use in source and binary forms, with or without
78e727371SKashyap D Desai * modification, are permitted provided that the following conditions are
88e727371SKashyap D Desai * met:
9665484d8SDoug Ambrisko *
108e727371SKashyap D Desai * 1. Redistributions of source code must retain the above copyright notice,
118e727371SKashyap D Desai * this list of conditions and the following disclaimer. 2. Redistributions
128e727371SKashyap D Desai * in binary form must reproduce the above copyright notice, this list of
138e727371SKashyap D Desai * conditions and the following disclaimer in the documentation and/or other
148e727371SKashyap D Desai * materials provided with the distribution. 3. Neither the name of the
158e727371SKashyap D Desai * <ORGANIZATION> nor the names of its contributors may be used to endorse or
168e727371SKashyap D Desai * promote products derived from this software without specific prior written
178e727371SKashyap D Desai * permission.
18665484d8SDoug Ambrisko *
198e727371SKashyap D Desai * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
208e727371SKashyap D Desai * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218e727371SKashyap D Desai * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228e727371SKashyap D Desai * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
238e727371SKashyap D Desai * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
248e727371SKashyap D Desai * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
258e727371SKashyap D Desai * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
268e727371SKashyap D Desai * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
278e727371SKashyap D Desai * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288e727371SKashyap D Desai * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29665484d8SDoug Ambrisko * POSSIBILITY OF SUCH DAMAGE.
30665484d8SDoug Ambrisko *
318e727371SKashyap D Desai * The views and conclusions contained in the software and documentation are
328e727371SKashyap D Desai * those of the authors and should not be interpreted as representing
33665484d8SDoug Ambrisko * official policies,either expressed or implied, of the FreeBSD Project.
34665484d8SDoug Ambrisko *
35ecea5be4SKashyap D Desai * Send feedback to: <megaraidfbsd@avagotech.com> Mail to: AVAGO TECHNOLOGIES 1621
368e727371SKashyap D Desai * Barber Lane, Milpitas, CA 95035 ATTN: MegaRaid FreeBSD
37665484d8SDoug Ambrisko *
38665484d8SDoug Ambrisko */
39665484d8SDoug Ambrisko
40665484d8SDoug Ambrisko #include <sys/cdefs.h>
41665484d8SDoug Ambrisko #include <dev/mrsas/mrsas.h>
42665484d8SDoug Ambrisko #include <dev/mrsas/mrsas_ioctl.h>
43665484d8SDoug Ambrisko
44665484d8SDoug Ambrisko #include <cam/cam.h>
45665484d8SDoug Ambrisko #include <cam/cam_ccb.h>
46665484d8SDoug Ambrisko
47665484d8SDoug Ambrisko #include <sys/sysctl.h>
48665484d8SDoug Ambrisko #include <sys/types.h>
498071588dSKashyap D Desai #include <sys/sysent.h>
50665484d8SDoug Ambrisko #include <sys/kthread.h>
51665484d8SDoug Ambrisko #include <sys/taskqueue.h>
52d18d1b47SKashyap D Desai #include <sys/smp.h>
53e34a057cSAlfredo Dal'Ava Junior #include <sys/endian.h>
54665484d8SDoug Ambrisko
55665484d8SDoug Ambrisko /*
56665484d8SDoug Ambrisko * Function prototypes
57665484d8SDoug Ambrisko */
58665484d8SDoug Ambrisko static d_open_t mrsas_open;
59665484d8SDoug Ambrisko static d_close_t mrsas_close;
60665484d8SDoug Ambrisko static d_ioctl_t mrsas_ioctl;
61da011113SKashyap D Desai static d_poll_t mrsas_poll;
62665484d8SDoug Ambrisko
638071588dSKashyap D Desai static void mrsas_ich_startup(void *arg);
64536094dcSKashyap D Desai static struct mrsas_mgmt_info mrsas_mgmt_info;
65665484d8SDoug Ambrisko static struct mrsas_ident *mrsas_find_ident(device_t);
66d18d1b47SKashyap D Desai static int mrsas_setup_msix(struct mrsas_softc *sc);
67d18d1b47SKashyap D Desai static int mrsas_allocate_msix(struct mrsas_softc *sc);
68665484d8SDoug Ambrisko static void mrsas_shutdown_ctlr(struct mrsas_softc *sc, u_int32_t opcode);
69665484d8SDoug Ambrisko static void mrsas_flush_cache(struct mrsas_softc *sc);
70665484d8SDoug Ambrisko static void mrsas_reset_reply_desc(struct mrsas_softc *sc);
71665484d8SDoug Ambrisko static void mrsas_ocr_thread(void *arg);
72665484d8SDoug Ambrisko static int mrsas_get_map_info(struct mrsas_softc *sc);
73665484d8SDoug Ambrisko static int mrsas_get_ld_map_info(struct mrsas_softc *sc);
74665484d8SDoug Ambrisko static int mrsas_sync_map_info(struct mrsas_softc *sc);
75665484d8SDoug Ambrisko static int mrsas_get_pd_list(struct mrsas_softc *sc);
76665484d8SDoug Ambrisko static int mrsas_get_ld_list(struct mrsas_softc *sc);
77665484d8SDoug Ambrisko static int mrsas_setup_irq(struct mrsas_softc *sc);
78665484d8SDoug Ambrisko static int mrsas_alloc_mem(struct mrsas_softc *sc);
79665484d8SDoug Ambrisko static int mrsas_init_fw(struct mrsas_softc *sc);
80665484d8SDoug Ambrisko static int mrsas_setup_raidmap(struct mrsas_softc *sc);
81a688fcd0SKashyap D Desai static void megasas_setup_jbod_map(struct mrsas_softc *sc);
82a688fcd0SKashyap D Desai static int megasas_sync_pd_seq_num(struct mrsas_softc *sc, boolean_t pend);
83665484d8SDoug Ambrisko static int mrsas_clear_intr(struct mrsas_softc *sc);
84af51c29fSKashyap D Desai static int mrsas_get_ctrl_info(struct mrsas_softc *sc);
85af51c29fSKashyap D Desai static void mrsas_update_ext_vd_details(struct mrsas_softc *sc);
868e727371SKashyap D Desai static int
878e727371SKashyap D Desai mrsas_issue_blocked_abort_cmd(struct mrsas_softc *sc,
88665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd_to_abort);
8979b4460bSKashyap D Desai static void
9079b4460bSKashyap D Desai mrsas_get_pd_info(struct mrsas_softc *sc, u_int16_t device_id);
91dbcc81dfSKashyap D Desai static struct mrsas_softc *
92dbcc81dfSKashyap D Desai mrsas_get_softc_instance(struct cdev *dev,
935844115eSKashyap D Desai u_long cmd, caddr_t arg);
94e315cf4dSKashyap D Desai u_int32_t
95e315cf4dSKashyap D Desai mrsas_read_reg_with_retries(struct mrsas_softc *sc, int offset);
96665484d8SDoug Ambrisko u_int32_t mrsas_read_reg(struct mrsas_softc *sc, int offset);
978e727371SKashyap D Desai u_int8_t
988e727371SKashyap D Desai mrsas_build_mptmfi_passthru(struct mrsas_softc *sc,
99665484d8SDoug Ambrisko struct mrsas_mfi_cmd *mfi_cmd);
100daeed973SKashyap D Desai void mrsas_complete_outstanding_ioctls(struct mrsas_softc *sc);
101665484d8SDoug Ambrisko int mrsas_transition_to_ready(struct mrsas_softc *sc, int ocr);
102665484d8SDoug Ambrisko int mrsas_init_adapter(struct mrsas_softc *sc);
103665484d8SDoug Ambrisko int mrsas_alloc_mpt_cmds(struct mrsas_softc *sc);
104665484d8SDoug Ambrisko int mrsas_alloc_ioc_cmd(struct mrsas_softc *sc);
105665484d8SDoug Ambrisko int mrsas_alloc_ctlr_info_cmd(struct mrsas_softc *sc);
106665484d8SDoug Ambrisko int mrsas_ioc_init(struct mrsas_softc *sc);
107665484d8SDoug Ambrisko int mrsas_bus_scan(struct mrsas_softc *sc);
108665484d8SDoug Ambrisko int mrsas_issue_dcmd(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
109665484d8SDoug Ambrisko int mrsas_issue_polled(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
110f0c7594bSKashyap D Desai int mrsas_reset_ctrl(struct mrsas_softc *sc, u_int8_t reset_reason);
111f0c7594bSKashyap D Desai int mrsas_wait_for_outstanding(struct mrsas_softc *sc, u_int8_t check_reason);
1124bb0a4f0SKashyap D Desai int mrsas_complete_cmd(struct mrsas_softc *sc, u_int32_t MSIxIndex);
1138bb601acSKashyap D Desai int mrsas_reset_targets(struct mrsas_softc *sc);
1148e727371SKashyap D Desai int
1158e727371SKashyap D Desai mrsas_issue_blocked_cmd(struct mrsas_softc *sc,
116665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd);
1178e727371SKashyap D Desai int
1188e727371SKashyap D Desai mrsas_alloc_tmp_dcmd(struct mrsas_softc *sc, struct mrsas_tmp_dcmd *tcmd,
119665484d8SDoug Ambrisko int size);
120665484d8SDoug Ambrisko void mrsas_release_mfi_cmd(struct mrsas_mfi_cmd *cmd);
121665484d8SDoug Ambrisko void mrsas_wakeup(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
122665484d8SDoug Ambrisko void mrsas_complete_aen(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
123665484d8SDoug Ambrisko void mrsas_complete_abort(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
124665484d8SDoug Ambrisko void mrsas_disable_intr(struct mrsas_softc *sc);
125665484d8SDoug Ambrisko void mrsas_enable_intr(struct mrsas_softc *sc);
126665484d8SDoug Ambrisko void mrsas_free_ioc_cmd(struct mrsas_softc *sc);
127665484d8SDoug Ambrisko void mrsas_free_mem(struct mrsas_softc *sc);
128665484d8SDoug Ambrisko void mrsas_free_tmp_dcmd(struct mrsas_tmp_dcmd *tmp);
129665484d8SDoug Ambrisko void mrsas_isr(void *arg);
130665484d8SDoug Ambrisko void mrsas_teardown_intr(struct mrsas_softc *sc);
131665484d8SDoug Ambrisko void mrsas_addr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
132665484d8SDoug Ambrisko void mrsas_kill_hba(struct mrsas_softc *sc);
133665484d8SDoug Ambrisko void mrsas_aen_handler(struct mrsas_softc *sc);
1348e727371SKashyap D Desai void
1358e727371SKashyap D Desai mrsas_write_reg(struct mrsas_softc *sc, int offset,
136665484d8SDoug Ambrisko u_int32_t value);
1378e727371SKashyap D Desai void
1388e727371SKashyap D Desai mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
139665484d8SDoug Ambrisko u_int32_t req_desc_hi);
140665484d8SDoug Ambrisko void mrsas_free_ctlr_info_cmd(struct mrsas_softc *sc);
1418e727371SKashyap D Desai void
1428e727371SKashyap D Desai mrsas_complete_mptmfi_passthru(struct mrsas_softc *sc,
143665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd, u_int8_t status);
144665484d8SDoug Ambrisko struct mrsas_mfi_cmd *mrsas_get_mfi_cmd(struct mrsas_softc *sc);
1458e727371SKashyap D Desai
1468e727371SKashyap D Desai MRSAS_REQUEST_DESCRIPTOR_UNION *mrsas_build_mpt_cmd
1478e727371SKashyap D Desai (struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
148665484d8SDoug Ambrisko
149665484d8SDoug Ambrisko extern int mrsas_cam_attach(struct mrsas_softc *sc);
150665484d8SDoug Ambrisko extern void mrsas_cam_detach(struct mrsas_softc *sc);
151665484d8SDoug Ambrisko extern void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
152665484d8SDoug Ambrisko extern void mrsas_free_frame(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd);
153665484d8SDoug Ambrisko extern int mrsas_alloc_mfi_cmds(struct mrsas_softc *sc);
154665484d8SDoug Ambrisko extern struct mrsas_mpt_cmd *mrsas_get_mpt_cmd(struct mrsas_softc *sc);
155536094dcSKashyap D Desai extern int mrsas_passthru(struct mrsas_softc *sc, void *arg, u_long ioctlCmd);
156665484d8SDoug Ambrisko extern uint8_t MR_ValidateMapInfo(struct mrsas_softc *sc);
1574799d485SKashyap D Desai extern u_int16_t MR_GetLDTgtId(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
1584799d485SKashyap D Desai extern MR_LD_RAID *MR_LdRaidGet(u_int32_t ld, MR_DRV_RAID_MAP_ALL * map);
159665484d8SDoug Ambrisko extern void mrsas_xpt_freeze(struct mrsas_softc *sc);
160665484d8SDoug Ambrisko extern void mrsas_xpt_release(struct mrsas_softc *sc);
1618e727371SKashyap D Desai extern MRSAS_REQUEST_DESCRIPTOR_UNION *
1628e727371SKashyap D Desai mrsas_get_request_desc(struct mrsas_softc *sc,
163665484d8SDoug Ambrisko u_int16_t index);
164665484d8SDoug Ambrisko extern int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
165665484d8SDoug Ambrisko static int mrsas_alloc_evt_log_info_cmd(struct mrsas_softc *sc);
166665484d8SDoug Ambrisko static void mrsas_free_evt_log_info_cmd(struct mrsas_softc *sc);
1672a1d3bcdSKashyap D Desai void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
1682a1d3bcdSKashyap D Desai
1692a1d3bcdSKashyap D Desai void mrsas_map_mpt_cmd_status(struct mrsas_mpt_cmd *cmd,
1702a1d3bcdSKashyap D Desai union ccb *ccb_ptr, u_int8_t status, u_int8_t extStatus,
1712a1d3bcdSKashyap D Desai u_int32_t data_length, u_int8_t *sense);
172b518670cSKashyap D Desai void
173b518670cSKashyap D Desai mrsas_write_64bit_req_desc(struct mrsas_softc *sc, u_int32_t req_desc_lo,
174b518670cSKashyap D Desai u_int32_t req_desc_hi);
1752a1d3bcdSKashyap D Desai
1767029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, mrsas, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1777029da5cSPawel Biernacki "MRSAS Driver Parameters");
178665484d8SDoug Ambrisko
1798e727371SKashyap D Desai /*
180665484d8SDoug Ambrisko * PCI device struct and table
181665484d8SDoug Ambrisko *
182665484d8SDoug Ambrisko */
183665484d8SDoug Ambrisko typedef struct mrsas_ident {
184665484d8SDoug Ambrisko uint16_t vendor;
185665484d8SDoug Ambrisko uint16_t device;
186665484d8SDoug Ambrisko uint16_t subvendor;
187665484d8SDoug Ambrisko uint16_t subdevice;
188665484d8SDoug Ambrisko const char *desc;
189665484d8SDoug Ambrisko } MRSAS_CTLR_ID;
190665484d8SDoug Ambrisko
191665484d8SDoug Ambrisko MRSAS_CTLR_ID device_table[] = {
192ecea5be4SKashyap D Desai {0x1000, MRSAS_TBOLT, 0xffff, 0xffff, "AVAGO Thunderbolt SAS Controller"},
193ecea5be4SKashyap D Desai {0x1000, MRSAS_INVADER, 0xffff, 0xffff, "AVAGO Invader SAS Controller"},
194ecea5be4SKashyap D Desai {0x1000, MRSAS_FURY, 0xffff, 0xffff, "AVAGO Fury SAS Controller"},
195c620f351SKashyap D Desai {0x1000, MRSAS_INTRUDER, 0xffff, 0xffff, "AVAGO Intruder SAS Controller"},
196c620f351SKashyap D Desai {0x1000, MRSAS_INTRUDER_24, 0xffff, 0xffff, "AVAGO Intruder_24 SAS Controller"},
1978cd174a4SKashyap D Desai {0x1000, MRSAS_CUTLASS_52, 0xffff, 0xffff, "AVAGO Cutlass_52 SAS Controller"},
1988cd174a4SKashyap D Desai {0x1000, MRSAS_CUTLASS_53, 0xffff, 0xffff, "AVAGO Cutlass_53 SAS Controller"},
1997aade8bfSKashyap D Desai {0x1000, MRSAS_VENTURA, 0xffff, 0xffff, "AVAGO Ventura SAS Controller"},
2007aade8bfSKashyap D Desai {0x1000, MRSAS_CRUSADER, 0xffff, 0xffff, "AVAGO Crusader SAS Controller"},
2017aade8bfSKashyap D Desai {0x1000, MRSAS_HARPOON, 0xffff, 0xffff, "AVAGO Harpoon SAS Controller"},
2027aade8bfSKashyap D Desai {0x1000, MRSAS_TOMCAT, 0xffff, 0xffff, "AVAGO Tomcat SAS Controller"},
2037aade8bfSKashyap D Desai {0x1000, MRSAS_VENTURA_4PORT, 0xffff, 0xffff, "AVAGO Ventura_4Port SAS Controller"},
2047aade8bfSKashyap D Desai {0x1000, MRSAS_CRUSADER_4PORT, 0xffff, 0xffff, "AVAGO Crusader_4Port SAS Controller"},
2052909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E0, 0xffff, 0xffff, "BROADCOM AERO-10E0 SAS Controller"},
2062909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E1, 0xffff, 0xffff, "BROADCOM AERO-10E1 SAS Controller"},
2072909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E2, 0xffff, 0xffff, "BROADCOM AERO-10E2 SAS Controller"},
2082909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E3, 0xffff, 0xffff, "BROADCOM AERO-10E3 SAS Controller"},
2092909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E4, 0xffff, 0xffff, "BROADCOM AERO-10E4 SAS Controller"},
2102909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E5, 0xffff, 0xffff, "BROADCOM AERO-10E5 SAS Controller"},
2112909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E6, 0xffff, 0xffff, "BROADCOM AERO-10E6 SAS Controller"},
2122909aab4SKashyap D Desai {0x1000, MRSAS_AERO_10E7, 0xffff, 0xffff, "BROADCOM AERO-10E7 SAS Controller"},
213665484d8SDoug Ambrisko {0, 0, 0, 0, NULL}
214665484d8SDoug Ambrisko };
215665484d8SDoug Ambrisko
2168e727371SKashyap D Desai /*
217665484d8SDoug Ambrisko * Character device entry points
218665484d8SDoug Ambrisko *
219665484d8SDoug Ambrisko */
220665484d8SDoug Ambrisko static struct cdevsw mrsas_cdevsw = {
221665484d8SDoug Ambrisko .d_version = D_VERSION,
222665484d8SDoug Ambrisko .d_open = mrsas_open,
223665484d8SDoug Ambrisko .d_close = mrsas_close,
224665484d8SDoug Ambrisko .d_ioctl = mrsas_ioctl,
225da011113SKashyap D Desai .d_poll = mrsas_poll,
226665484d8SDoug Ambrisko .d_name = "mrsas",
227665484d8SDoug Ambrisko };
228665484d8SDoug Ambrisko
229665484d8SDoug Ambrisko MALLOC_DEFINE(M_MRSAS, "mrsasbuf", "Buffers for the MRSAS driver");
230665484d8SDoug Ambrisko
231665484d8SDoug Ambrisko int
mrsas_open(struct cdev * dev,int oflags,int devtype,struct thread * td)2327fc5f329SJohn Baldwin mrsas_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
233665484d8SDoug Ambrisko {
234665484d8SDoug Ambrisko
235665484d8SDoug Ambrisko return (0);
236665484d8SDoug Ambrisko }
237665484d8SDoug Ambrisko
238665484d8SDoug Ambrisko int
mrsas_close(struct cdev * dev,int fflag,int devtype,struct thread * td)2397fc5f329SJohn Baldwin mrsas_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
240665484d8SDoug Ambrisko {
241665484d8SDoug Ambrisko
242665484d8SDoug Ambrisko return (0);
243665484d8SDoug Ambrisko }
244665484d8SDoug Ambrisko
245e315cf4dSKashyap D Desai u_int32_t
mrsas_read_reg_with_retries(struct mrsas_softc * sc,int offset)246e315cf4dSKashyap D Desai mrsas_read_reg_with_retries(struct mrsas_softc *sc, int offset)
247e315cf4dSKashyap D Desai {
248e315cf4dSKashyap D Desai u_int32_t i = 0, ret_val;
249e315cf4dSKashyap D Desai
250e315cf4dSKashyap D Desai if (sc->is_aero) {
251e315cf4dSKashyap D Desai do {
252e315cf4dSKashyap D Desai ret_val = mrsas_read_reg(sc, offset);
253e315cf4dSKashyap D Desai i++;
254e315cf4dSKashyap D Desai } while(ret_val == 0 && i < 3);
255e315cf4dSKashyap D Desai } else
256e315cf4dSKashyap D Desai ret_val = mrsas_read_reg(sc, offset);
257e315cf4dSKashyap D Desai
258e315cf4dSKashyap D Desai return ret_val;
259e315cf4dSKashyap D Desai }
260e315cf4dSKashyap D Desai
2618e727371SKashyap D Desai /*
262665484d8SDoug Ambrisko * Register Read/Write Functions
263665484d8SDoug Ambrisko *
264665484d8SDoug Ambrisko */
265665484d8SDoug Ambrisko void
mrsas_write_reg(struct mrsas_softc * sc,int offset,u_int32_t value)266665484d8SDoug Ambrisko mrsas_write_reg(struct mrsas_softc *sc, int offset,
267665484d8SDoug Ambrisko u_int32_t value)
268665484d8SDoug Ambrisko {
269665484d8SDoug Ambrisko bus_space_tag_t bus_tag = sc->bus_tag;
270665484d8SDoug Ambrisko bus_space_handle_t bus_handle = sc->bus_handle;
271665484d8SDoug Ambrisko
272665484d8SDoug Ambrisko bus_space_write_4(bus_tag, bus_handle, offset, value);
273665484d8SDoug Ambrisko }
274665484d8SDoug Ambrisko
275665484d8SDoug Ambrisko u_int32_t
mrsas_read_reg(struct mrsas_softc * sc,int offset)276665484d8SDoug Ambrisko mrsas_read_reg(struct mrsas_softc *sc, int offset)
277665484d8SDoug Ambrisko {
278665484d8SDoug Ambrisko bus_space_tag_t bus_tag = sc->bus_tag;
279665484d8SDoug Ambrisko bus_space_handle_t bus_handle = sc->bus_handle;
280665484d8SDoug Ambrisko
281665484d8SDoug Ambrisko return ((u_int32_t)bus_space_read_4(bus_tag, bus_handle, offset));
282665484d8SDoug Ambrisko }
283665484d8SDoug Ambrisko
2848e727371SKashyap D Desai /*
285665484d8SDoug Ambrisko * Interrupt Disable/Enable/Clear Functions
286665484d8SDoug Ambrisko *
287665484d8SDoug Ambrisko */
2888e727371SKashyap D Desai void
mrsas_disable_intr(struct mrsas_softc * sc)2898e727371SKashyap D Desai mrsas_disable_intr(struct mrsas_softc *sc)
290665484d8SDoug Ambrisko {
291665484d8SDoug Ambrisko u_int32_t mask = 0xFFFFFFFF;
292665484d8SDoug Ambrisko
2932f863eb8SKashyap D Desai sc->mask_interrupts = 1;
294665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, outbound_intr_mask), mask);
295665484d8SDoug Ambrisko /* Dummy read to force pci flush */
29698470f0eSScott Long (void)mrsas_read_reg(sc, offsetof(mrsas_reg_set, outbound_intr_mask));
297665484d8SDoug Ambrisko }
298665484d8SDoug Ambrisko
2998e727371SKashyap D Desai void
mrsas_enable_intr(struct mrsas_softc * sc)3008e727371SKashyap D Desai mrsas_enable_intr(struct mrsas_softc *sc)
301665484d8SDoug Ambrisko {
302665484d8SDoug Ambrisko u_int32_t mask = MFI_FUSION_ENABLE_INTERRUPT_MASK;
303665484d8SDoug Ambrisko
3042f863eb8SKashyap D Desai sc->mask_interrupts = 0;
305665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, outbound_intr_status), ~0);
30698470f0eSScott Long (void)mrsas_read_reg(sc, offsetof(mrsas_reg_set, outbound_intr_status));
307665484d8SDoug Ambrisko
308665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, outbound_intr_mask), ~mask);
30998470f0eSScott Long (void)mrsas_read_reg(sc, offsetof(mrsas_reg_set, outbound_intr_mask));
310665484d8SDoug Ambrisko }
311665484d8SDoug Ambrisko
3128e727371SKashyap D Desai static int
mrsas_clear_intr(struct mrsas_softc * sc)3138e727371SKashyap D Desai mrsas_clear_intr(struct mrsas_softc *sc)
314665484d8SDoug Ambrisko {
3158bb601acSKashyap D Desai u_int32_t status;
316665484d8SDoug Ambrisko
317665484d8SDoug Ambrisko /* Read received interrupt */
318e315cf4dSKashyap D Desai status = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_intr_status));
319665484d8SDoug Ambrisko
320665484d8SDoug Ambrisko /* Not our interrupt, so just return */
321665484d8SDoug Ambrisko if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
322665484d8SDoug Ambrisko return (0);
323665484d8SDoug Ambrisko
324665484d8SDoug Ambrisko /* We got a reply interrupt */
325665484d8SDoug Ambrisko return (1);
326665484d8SDoug Ambrisko }
327665484d8SDoug Ambrisko
3288e727371SKashyap D Desai /*
329665484d8SDoug Ambrisko * PCI Support Functions
330665484d8SDoug Ambrisko *
331665484d8SDoug Ambrisko */
3328e727371SKashyap D Desai static struct mrsas_ident *
mrsas_find_ident(device_t dev)3338e727371SKashyap D Desai mrsas_find_ident(device_t dev)
334665484d8SDoug Ambrisko {
335665484d8SDoug Ambrisko struct mrsas_ident *pci_device;
336665484d8SDoug Ambrisko
3378e727371SKashyap D Desai for (pci_device = device_table; pci_device->vendor != 0; pci_device++) {
338665484d8SDoug Ambrisko if ((pci_device->vendor == pci_get_vendor(dev)) &&
339665484d8SDoug Ambrisko (pci_device->device == pci_get_device(dev)) &&
340665484d8SDoug Ambrisko ((pci_device->subvendor == pci_get_subvendor(dev)) ||
341665484d8SDoug Ambrisko (pci_device->subvendor == 0xffff)) &&
342665484d8SDoug Ambrisko ((pci_device->subdevice == pci_get_subdevice(dev)) ||
343665484d8SDoug Ambrisko (pci_device->subdevice == 0xffff)))
344665484d8SDoug Ambrisko return (pci_device);
345665484d8SDoug Ambrisko }
346665484d8SDoug Ambrisko return (NULL);
347665484d8SDoug Ambrisko }
348665484d8SDoug Ambrisko
3498e727371SKashyap D Desai static int
mrsas_probe(device_t dev)3508e727371SKashyap D Desai mrsas_probe(device_t dev)
351665484d8SDoug Ambrisko {
352665484d8SDoug Ambrisko static u_int8_t first_ctrl = 1;
353665484d8SDoug Ambrisko struct mrsas_ident *id;
354665484d8SDoug Ambrisko
355665484d8SDoug Ambrisko if ((id = mrsas_find_ident(dev)) != NULL) {
356665484d8SDoug Ambrisko if (first_ctrl) {
357ecea5be4SKashyap D Desai printf("AVAGO MegaRAID SAS FreeBSD mrsas driver version: %s\n",
3588e727371SKashyap D Desai MRSAS_VERSION);
359665484d8SDoug Ambrisko first_ctrl = 0;
360665484d8SDoug Ambrisko }
361665484d8SDoug Ambrisko device_set_desc(dev, id->desc);
362665484d8SDoug Ambrisko /* between BUS_PROBE_DEFAULT and BUS_PROBE_LOW_PRIORITY */
363665484d8SDoug Ambrisko return (-30);
364665484d8SDoug Ambrisko }
365665484d8SDoug Ambrisko return (ENXIO);
366665484d8SDoug Ambrisko }
367665484d8SDoug Ambrisko
3688e727371SKashyap D Desai /*
369665484d8SDoug Ambrisko * mrsas_setup_sysctl: setup sysctl values for mrsas
370665484d8SDoug Ambrisko * input: Adapter instance soft state
371665484d8SDoug Ambrisko *
372665484d8SDoug Ambrisko * Setup sysctl entries for mrsas driver.
373665484d8SDoug Ambrisko */
374665484d8SDoug Ambrisko static void
mrsas_setup_sysctl(struct mrsas_softc * sc)375665484d8SDoug Ambrisko mrsas_setup_sysctl(struct mrsas_softc *sc)
376665484d8SDoug Ambrisko {
377665484d8SDoug Ambrisko struct sysctl_ctx_list *sysctl_ctx = NULL;
378665484d8SDoug Ambrisko struct sysctl_oid *sysctl_tree = NULL;
379665484d8SDoug Ambrisko char tmpstr[80], tmpstr2[80];
380665484d8SDoug Ambrisko
381665484d8SDoug Ambrisko /*
382665484d8SDoug Ambrisko * Setup the sysctl variable so the user can change the debug level
383665484d8SDoug Ambrisko * on the fly.
384665484d8SDoug Ambrisko */
385665484d8SDoug Ambrisko snprintf(tmpstr, sizeof(tmpstr), "MRSAS controller %d",
386665484d8SDoug Ambrisko device_get_unit(sc->mrsas_dev));
387665484d8SDoug Ambrisko snprintf(tmpstr2, sizeof(tmpstr2), "%d", device_get_unit(sc->mrsas_dev));
388665484d8SDoug Ambrisko
389665484d8SDoug Ambrisko sysctl_ctx = device_get_sysctl_ctx(sc->mrsas_dev);
390665484d8SDoug Ambrisko if (sysctl_ctx != NULL)
391665484d8SDoug Ambrisko sysctl_tree = device_get_sysctl_tree(sc->mrsas_dev);
392665484d8SDoug Ambrisko
393665484d8SDoug Ambrisko if (sysctl_tree == NULL) {
394665484d8SDoug Ambrisko sysctl_ctx_init(&sc->sysctl_ctx);
395665484d8SDoug Ambrisko sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
396665484d8SDoug Ambrisko SYSCTL_STATIC_CHILDREN(_hw_mrsas), OID_AUTO, tmpstr2,
3977029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr);
398665484d8SDoug Ambrisko if (sc->sysctl_tree == NULL)
399665484d8SDoug Ambrisko return;
400665484d8SDoug Ambrisko sysctl_ctx = &sc->sysctl_ctx;
401665484d8SDoug Ambrisko sysctl_tree = sc->sysctl_tree;
402665484d8SDoug Ambrisko }
403665484d8SDoug Ambrisko SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
404665484d8SDoug Ambrisko OID_AUTO, "disable_ocr", CTLFLAG_RW, &sc->disableOnlineCtrlReset, 0,
405665484d8SDoug Ambrisko "Disable the use of OCR");
406665484d8SDoug Ambrisko
407665484d8SDoug Ambrisko SYSCTL_ADD_STRING(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
408665484d8SDoug Ambrisko OID_AUTO, "driver_version", CTLFLAG_RD, MRSAS_VERSION,
409665484d8SDoug Ambrisko strlen(MRSAS_VERSION), "driver version");
410665484d8SDoug Ambrisko
411665484d8SDoug Ambrisko SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
412665484d8SDoug Ambrisko OID_AUTO, "reset_count", CTLFLAG_RD,
413665484d8SDoug Ambrisko &sc->reset_count, 0, "number of ocr from start of the day");
414665484d8SDoug Ambrisko
415665484d8SDoug Ambrisko SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
416665484d8SDoug Ambrisko OID_AUTO, "fw_outstanding", CTLFLAG_RD,
417f0188618SHans Petter Selasky &sc->fw_outstanding.val_rdonly, 0, "FW outstanding commands");
418665484d8SDoug Ambrisko
419665484d8SDoug Ambrisko SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
420665484d8SDoug Ambrisko OID_AUTO, "io_cmds_highwater", CTLFLAG_RD,
421665484d8SDoug Ambrisko &sc->io_cmds_highwater, 0, "Max FW outstanding commands");
422665484d8SDoug Ambrisko
423665484d8SDoug Ambrisko SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
424665484d8SDoug Ambrisko OID_AUTO, "mrsas_debug", CTLFLAG_RW, &sc->mrsas_debug, 0,
425665484d8SDoug Ambrisko "Driver debug level");
426665484d8SDoug Ambrisko
427665484d8SDoug Ambrisko SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
428665484d8SDoug Ambrisko OID_AUTO, "mrsas_io_timeout", CTLFLAG_RW, &sc->mrsas_io_timeout,
429665484d8SDoug Ambrisko 0, "Driver IO timeout value in mili-second.");
430665484d8SDoug Ambrisko
431665484d8SDoug Ambrisko SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
432665484d8SDoug Ambrisko OID_AUTO, "mrsas_fw_fault_check_delay", CTLFLAG_RW,
433665484d8SDoug Ambrisko &sc->mrsas_fw_fault_check_delay,
434665484d8SDoug Ambrisko 0, "FW fault check thread delay in seconds. <default is 1 sec>");
435665484d8SDoug Ambrisko
436665484d8SDoug Ambrisko SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
437665484d8SDoug Ambrisko OID_AUTO, "reset_in_progress", CTLFLAG_RD,
438665484d8SDoug Ambrisko &sc->reset_in_progress, 0, "ocr in progress status");
439665484d8SDoug Ambrisko
440d993dd83SKashyap D Desai SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
441d993dd83SKashyap D Desai OID_AUTO, "block_sync_cache", CTLFLAG_RW,
442d993dd83SKashyap D Desai &sc->block_sync_cache, 0,
443d993dd83SKashyap D Desai "Block SYNC CACHE at driver. <default: 0, send it to FW>");
444821df4b9SKashyap D Desai SYSCTL_ADD_UINT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
445821df4b9SKashyap D Desai OID_AUTO, "stream detection", CTLFLAG_RW,
446821df4b9SKashyap D Desai &sc->drv_stream_detection, 0,
447821df4b9SKashyap D Desai "Disable/Enable Stream detection. <default: 1, Enable Stream Detection>");
4483d273176SKashyap D Desai SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
4493d273176SKashyap D Desai OID_AUTO, "prp_count", CTLFLAG_RD,
4503d273176SKashyap D Desai &sc->prp_count.val_rdonly, 0, "Number of IOs for which PRPs are built");
4513d273176SKashyap D Desai SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
4523d273176SKashyap D Desai OID_AUTO, "SGE holes", CTLFLAG_RD,
4533d273176SKashyap D Desai &sc->sge_holes.val_rdonly, 0, "Number of IOs with holes in SGEs");
454665484d8SDoug Ambrisko }
455665484d8SDoug Ambrisko
4568e727371SKashyap D Desai /*
457665484d8SDoug Ambrisko * mrsas_get_tunables: get tunable parameters.
458665484d8SDoug Ambrisko * input: Adapter instance soft state
459665484d8SDoug Ambrisko *
460665484d8SDoug Ambrisko * Get tunable parameters. This will help to debug driver at boot time.
461665484d8SDoug Ambrisko */
462665484d8SDoug Ambrisko static void
mrsas_get_tunables(struct mrsas_softc * sc)463665484d8SDoug Ambrisko mrsas_get_tunables(struct mrsas_softc *sc)
464665484d8SDoug Ambrisko {
465665484d8SDoug Ambrisko char tmpstr[80];
466665484d8SDoug Ambrisko
467665484d8SDoug Ambrisko /* XXX default to some debugging for now */
46856d91e49SKashyap D Desai sc->mrsas_debug =
46956d91e49SKashyap D Desai (MRSAS_FAULT | MRSAS_OCR | MRSAS_INFO | MRSAS_TRACE | MRSAS_AEN);
470665484d8SDoug Ambrisko sc->mrsas_io_timeout = MRSAS_IO_TIMEOUT;
471665484d8SDoug Ambrisko sc->mrsas_fw_fault_check_delay = 1;
472665484d8SDoug Ambrisko sc->reset_count = 0;
473665484d8SDoug Ambrisko sc->reset_in_progress = 0;
474d993dd83SKashyap D Desai sc->block_sync_cache = 0;
475821df4b9SKashyap D Desai sc->drv_stream_detection = 1;
476665484d8SDoug Ambrisko
477665484d8SDoug Ambrisko /*
478665484d8SDoug Ambrisko * Grab the global variables.
479665484d8SDoug Ambrisko */
480665484d8SDoug Ambrisko TUNABLE_INT_FETCH("hw.mrsas.debug_level", &sc->mrsas_debug);
481665484d8SDoug Ambrisko
48216dc2814SKashyap D Desai /*
48316dc2814SKashyap D Desai * Grab the global variables.
48416dc2814SKashyap D Desai */
48516dc2814SKashyap D Desai TUNABLE_INT_FETCH("hw.mrsas.lb_pending_cmds", &sc->lb_pending_cmds);
48616dc2814SKashyap D Desai
487665484d8SDoug Ambrisko /* Grab the unit-instance variables */
488665484d8SDoug Ambrisko snprintf(tmpstr, sizeof(tmpstr), "dev.mrsas.%d.debug_level",
489665484d8SDoug Ambrisko device_get_unit(sc->mrsas_dev));
490665484d8SDoug Ambrisko TUNABLE_INT_FETCH(tmpstr, &sc->mrsas_debug);
491665484d8SDoug Ambrisko }
492665484d8SDoug Ambrisko
4938e727371SKashyap D Desai /*
494665484d8SDoug Ambrisko * mrsas_alloc_evt_log_info cmd: Allocates memory to get event log information.
495665484d8SDoug Ambrisko * Used to get sequence number at driver load time.
496665484d8SDoug Ambrisko * input: Adapter soft state
497665484d8SDoug Ambrisko *
498665484d8SDoug Ambrisko * Allocates DMAable memory for the event log info internal command.
499665484d8SDoug Ambrisko */
5008e727371SKashyap D Desai int
mrsas_alloc_evt_log_info_cmd(struct mrsas_softc * sc)5018e727371SKashyap D Desai mrsas_alloc_evt_log_info_cmd(struct mrsas_softc *sc)
502665484d8SDoug Ambrisko {
503665484d8SDoug Ambrisko int el_info_size;
504665484d8SDoug Ambrisko
505665484d8SDoug Ambrisko /* Allocate get event log info command */
506665484d8SDoug Ambrisko el_info_size = sizeof(struct mrsas_evt_log_info);
5078e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
5088e727371SKashyap D Desai 1, 0,
5098e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
5108e727371SKashyap D Desai BUS_SPACE_MAXADDR,
5118e727371SKashyap D Desai NULL, NULL,
5128e727371SKashyap D Desai el_info_size,
5138e727371SKashyap D Desai 1,
5148e727371SKashyap D Desai el_info_size,
5158e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
5168e727371SKashyap D Desai NULL, NULL,
517665484d8SDoug Ambrisko &sc->el_info_tag)) {
518665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate event log info tag\n");
519665484d8SDoug Ambrisko return (ENOMEM);
520665484d8SDoug Ambrisko }
521665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->el_info_tag, (void **)&sc->el_info_mem,
522665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->el_info_dmamap)) {
523665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate event log info cmd mem\n");
524665484d8SDoug Ambrisko return (ENOMEM);
525665484d8SDoug Ambrisko }
526665484d8SDoug Ambrisko if (bus_dmamap_load(sc->el_info_tag, sc->el_info_dmamap,
527665484d8SDoug Ambrisko sc->el_info_mem, el_info_size, mrsas_addr_cb,
528665484d8SDoug Ambrisko &sc->el_info_phys_addr, BUS_DMA_NOWAIT)) {
529665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load event log info cmd mem\n");
530665484d8SDoug Ambrisko return (ENOMEM);
531665484d8SDoug Ambrisko }
532665484d8SDoug Ambrisko memset(sc->el_info_mem, 0, el_info_size);
533665484d8SDoug Ambrisko return (0);
534665484d8SDoug Ambrisko }
535665484d8SDoug Ambrisko
5368e727371SKashyap D Desai /*
537665484d8SDoug Ambrisko * mrsas_free_evt_info_cmd: Free memory for Event log info command
538665484d8SDoug Ambrisko * input: Adapter soft state
539665484d8SDoug Ambrisko *
540665484d8SDoug Ambrisko * Deallocates memory for the event log info internal command.
541665484d8SDoug Ambrisko */
5428e727371SKashyap D Desai void
mrsas_free_evt_log_info_cmd(struct mrsas_softc * sc)5438e727371SKashyap D Desai mrsas_free_evt_log_info_cmd(struct mrsas_softc *sc)
544665484d8SDoug Ambrisko {
545665484d8SDoug Ambrisko if (sc->el_info_phys_addr)
546665484d8SDoug Ambrisko bus_dmamap_unload(sc->el_info_tag, sc->el_info_dmamap);
547665484d8SDoug Ambrisko if (sc->el_info_mem != NULL)
548665484d8SDoug Ambrisko bus_dmamem_free(sc->el_info_tag, sc->el_info_mem, sc->el_info_dmamap);
549665484d8SDoug Ambrisko if (sc->el_info_tag != NULL)
550665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->el_info_tag);
551665484d8SDoug Ambrisko }
552665484d8SDoug Ambrisko
5538e727371SKashyap D Desai /*
554665484d8SDoug Ambrisko * mrsas_get_seq_num: Get latest event sequence number
555665484d8SDoug Ambrisko * @sc: Adapter soft state
556665484d8SDoug Ambrisko * @eli: Firmware event log sequence number information.
5578e727371SKashyap D Desai *
558665484d8SDoug Ambrisko * Firmware maintains a log of all events in a non-volatile area.
559665484d8SDoug Ambrisko * Driver get the sequence number using DCMD
560665484d8SDoug Ambrisko * "MR_DCMD_CTRL_EVENT_GET_INFO" at driver load time.
561665484d8SDoug Ambrisko */
562665484d8SDoug Ambrisko
563665484d8SDoug Ambrisko static int
mrsas_get_seq_num(struct mrsas_softc * sc,struct mrsas_evt_log_info * eli)564665484d8SDoug Ambrisko mrsas_get_seq_num(struct mrsas_softc *sc,
565665484d8SDoug Ambrisko struct mrsas_evt_log_info *eli)
566665484d8SDoug Ambrisko {
567665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
568665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
569f0c7594bSKashyap D Desai u_int8_t do_ocr = 1, retcode = 0;
570665484d8SDoug Ambrisko
571665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
572665484d8SDoug Ambrisko
573665484d8SDoug Ambrisko if (!cmd) {
574665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Failed to get a free cmd\n");
575665484d8SDoug Ambrisko return -ENOMEM;
576665484d8SDoug Ambrisko }
577665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
578665484d8SDoug Ambrisko
579665484d8SDoug Ambrisko if (mrsas_alloc_evt_log_info_cmd(sc) != SUCCESS) {
580665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate evt log info cmd\n");
581665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
582665484d8SDoug Ambrisko return -ENOMEM;
583665484d8SDoug Ambrisko }
584665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
585665484d8SDoug Ambrisko
586665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
587665484d8SDoug Ambrisko dcmd->cmd_status = 0x0;
588665484d8SDoug Ambrisko dcmd->sge_count = 1;
589e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_READ);
590665484d8SDoug Ambrisko dcmd->timeout = 0;
591665484d8SDoug Ambrisko dcmd->pad_0 = 0;
592e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sizeof(struct mrsas_evt_log_info));
593e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_CTRL_EVENT_GET_INFO);
594e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(sc->el_info_phys_addr & 0xFFFFFFFF);
595e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sizeof(struct mrsas_evt_log_info));
596665484d8SDoug Ambrisko
597f0c7594bSKashyap D Desai retcode = mrsas_issue_blocked_cmd(sc, cmd);
598f0c7594bSKashyap D Desai if (retcode == ETIMEDOUT)
599f0c7594bSKashyap D Desai goto dcmd_timeout;
600665484d8SDoug Ambrisko
601f0c7594bSKashyap D Desai do_ocr = 0;
602665484d8SDoug Ambrisko /*
603665484d8SDoug Ambrisko * Copy the data back into callers buffer
604665484d8SDoug Ambrisko */
605665484d8SDoug Ambrisko memcpy(eli, sc->el_info_mem, sizeof(struct mrsas_evt_log_info));
606665484d8SDoug Ambrisko mrsas_free_evt_log_info_cmd(sc);
607f0c7594bSKashyap D Desai
608f0c7594bSKashyap D Desai dcmd_timeout:
609f0c7594bSKashyap D Desai if (do_ocr)
610f0c7594bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
611f0c7594bSKashyap D Desai else
612665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
613665484d8SDoug Ambrisko
614f0c7594bSKashyap D Desai return retcode;
615665484d8SDoug Ambrisko }
616665484d8SDoug Ambrisko
6178e727371SKashyap D Desai /*
618665484d8SDoug Ambrisko * mrsas_register_aen: Register for asynchronous event notification
619665484d8SDoug Ambrisko * @sc: Adapter soft state
620665484d8SDoug Ambrisko * @seq_num: Starting sequence number
621665484d8SDoug Ambrisko * @class_locale: Class of the event
6228e727371SKashyap D Desai *
623665484d8SDoug Ambrisko * This function subscribes for events beyond the @seq_num
624665484d8SDoug Ambrisko * and type @class_locale.
625665484d8SDoug Ambrisko *
6268e727371SKashyap D Desai */
627665484d8SDoug Ambrisko static int
mrsas_register_aen(struct mrsas_softc * sc,u_int32_t seq_num,u_int32_t class_locale_word)628665484d8SDoug Ambrisko mrsas_register_aen(struct mrsas_softc *sc, u_int32_t seq_num,
629665484d8SDoug Ambrisko u_int32_t class_locale_word)
630665484d8SDoug Ambrisko {
631665484d8SDoug Ambrisko int ret_val;
632665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
633665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
634665484d8SDoug Ambrisko union mrsas_evt_class_locale curr_aen;
635665484d8SDoug Ambrisko union mrsas_evt_class_locale prev_aen;
636665484d8SDoug Ambrisko
637665484d8SDoug Ambrisko /*
638665484d8SDoug Ambrisko * If there an AEN pending already (aen_cmd), check if the
6398e727371SKashyap D Desai * class_locale of that pending AEN is inclusive of the new AEN
6408e727371SKashyap D Desai * request we currently have. If it is, then we don't have to do
6418e727371SKashyap D Desai * anything. In other words, whichever events the current AEN request
6428e727371SKashyap D Desai * is subscribing to, have already been subscribed to. If the old_cmd
6438e727371SKashyap D Desai * is _not_ inclusive, then we have to abort that command, form a
6448e727371SKashyap D Desai * class_locale that is superset of both old and current and re-issue
6458e727371SKashyap D Desai * to the FW
6468e727371SKashyap D Desai */
647665484d8SDoug Ambrisko
648665484d8SDoug Ambrisko curr_aen.word = class_locale_word;
649665484d8SDoug Ambrisko
650665484d8SDoug Ambrisko if (sc->aen_cmd) {
651e34a057cSAlfredo Dal'Ava Junior prev_aen.word = le32toh(sc->aen_cmd->frame->dcmd.mbox.w[1]);
652665484d8SDoug Ambrisko
653665484d8SDoug Ambrisko /*
654665484d8SDoug Ambrisko * A class whose enum value is smaller is inclusive of all
655665484d8SDoug Ambrisko * higher values. If a PROGRESS (= -1) was previously
656665484d8SDoug Ambrisko * registered, then a new registration requests for higher
657665484d8SDoug Ambrisko * classes need not be sent to FW. They are automatically
6588e727371SKashyap D Desai * included. Locale numbers don't have such hierarchy. They
6598e727371SKashyap D Desai * are bitmap values
660665484d8SDoug Ambrisko */
661665484d8SDoug Ambrisko if ((prev_aen.members.class <= curr_aen.members.class) &&
662665484d8SDoug Ambrisko !((prev_aen.members.locale & curr_aen.members.locale) ^
663665484d8SDoug Ambrisko curr_aen.members.locale)) {
664665484d8SDoug Ambrisko /*
665665484d8SDoug Ambrisko * Previously issued event registration includes
666665484d8SDoug Ambrisko * current request. Nothing to do.
667665484d8SDoug Ambrisko */
668665484d8SDoug Ambrisko return 0;
669665484d8SDoug Ambrisko } else {
670665484d8SDoug Ambrisko curr_aen.members.locale |= prev_aen.members.locale;
671665484d8SDoug Ambrisko
672665484d8SDoug Ambrisko if (prev_aen.members.class < curr_aen.members.class)
673665484d8SDoug Ambrisko curr_aen.members.class = prev_aen.members.class;
674665484d8SDoug Ambrisko
675665484d8SDoug Ambrisko sc->aen_cmd->abort_aen = 1;
676665484d8SDoug Ambrisko ret_val = mrsas_issue_blocked_abort_cmd(sc,
677665484d8SDoug Ambrisko sc->aen_cmd);
678665484d8SDoug Ambrisko
679665484d8SDoug Ambrisko if (ret_val) {
680731b7561SKashyap D Desai printf("mrsas: Failed to abort previous AEN command\n");
681665484d8SDoug Ambrisko return ret_val;
682c2a20ff9SKashyap D Desai } else
683c2a20ff9SKashyap D Desai sc->aen_cmd = NULL;
684665484d8SDoug Ambrisko }
685665484d8SDoug Ambrisko }
686665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
687665484d8SDoug Ambrisko if (!cmd)
688731b7561SKashyap D Desai return ENOMEM;
689665484d8SDoug Ambrisko
690665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
691665484d8SDoug Ambrisko
692665484d8SDoug Ambrisko memset(sc->evt_detail_mem, 0, sizeof(struct mrsas_evt_detail));
693665484d8SDoug Ambrisko
694665484d8SDoug Ambrisko /*
695665484d8SDoug Ambrisko * Prepare DCMD for aen registration
696665484d8SDoug Ambrisko */
697665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
698665484d8SDoug Ambrisko
699665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
700665484d8SDoug Ambrisko dcmd->cmd_status = 0x0;
701665484d8SDoug Ambrisko dcmd->sge_count = 1;
702e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_READ);
703665484d8SDoug Ambrisko dcmd->timeout = 0;
704665484d8SDoug Ambrisko dcmd->pad_0 = 0;
705e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sizeof(struct mrsas_evt_detail));
706e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_CTRL_EVENT_WAIT);
707e34a057cSAlfredo Dal'Ava Junior dcmd->mbox.w[0] = htole32(seq_num);
708665484d8SDoug Ambrisko sc->last_seq_num = seq_num;
709e34a057cSAlfredo Dal'Ava Junior dcmd->mbox.w[1] = htole32(curr_aen.word);
710e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32((u_int32_t)sc->evt_detail_phys_addr & 0xFFFFFFFF);
711e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sizeof(struct mrsas_evt_detail));
712665484d8SDoug Ambrisko
713665484d8SDoug Ambrisko if (sc->aen_cmd != NULL) {
714665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
715665484d8SDoug Ambrisko return 0;
716665484d8SDoug Ambrisko }
717665484d8SDoug Ambrisko /*
718665484d8SDoug Ambrisko * Store reference to the cmd used to register for AEN. When an
719665484d8SDoug Ambrisko * application wants us to register for AEN, we have to abort this
720665484d8SDoug Ambrisko * cmd and re-register with a new EVENT LOCALE supplied by that app
721665484d8SDoug Ambrisko */
722665484d8SDoug Ambrisko sc->aen_cmd = cmd;
723665484d8SDoug Ambrisko
724665484d8SDoug Ambrisko /*
7258e727371SKashyap D Desai * Issue the aen registration frame
726665484d8SDoug Ambrisko */
727665484d8SDoug Ambrisko if (mrsas_issue_dcmd(sc, cmd)) {
728665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot issue AEN DCMD command.\n");
729665484d8SDoug Ambrisko return (1);
730665484d8SDoug Ambrisko }
731665484d8SDoug Ambrisko return 0;
732665484d8SDoug Ambrisko }
7338e727371SKashyap D Desai
7348e727371SKashyap D Desai /*
7358e727371SKashyap D Desai * mrsas_start_aen: Subscribes to AEN during driver load time
736665484d8SDoug Ambrisko * @instance: Adapter soft state
737665484d8SDoug Ambrisko */
7388e727371SKashyap D Desai static int
mrsas_start_aen(struct mrsas_softc * sc)7398e727371SKashyap D Desai mrsas_start_aen(struct mrsas_softc *sc)
740665484d8SDoug Ambrisko {
741665484d8SDoug Ambrisko struct mrsas_evt_log_info eli;
742665484d8SDoug Ambrisko union mrsas_evt_class_locale class_locale;
743665484d8SDoug Ambrisko
744665484d8SDoug Ambrisko /* Get the latest sequence number from FW */
745665484d8SDoug Ambrisko
746665484d8SDoug Ambrisko memset(&eli, 0, sizeof(eli));
747665484d8SDoug Ambrisko
748665484d8SDoug Ambrisko if (mrsas_get_seq_num(sc, &eli))
749665484d8SDoug Ambrisko return -1;
750665484d8SDoug Ambrisko
751665484d8SDoug Ambrisko /* Register AEN with FW for latest sequence number plus 1 */
752665484d8SDoug Ambrisko class_locale.members.reserved = 0;
753665484d8SDoug Ambrisko class_locale.members.locale = MR_EVT_LOCALE_ALL;
754665484d8SDoug Ambrisko class_locale.members.class = MR_EVT_CLASS_DEBUG;
755665484d8SDoug Ambrisko
756665484d8SDoug Ambrisko return mrsas_register_aen(sc, eli.newest_seq_num + 1,
757665484d8SDoug Ambrisko class_locale.word);
758d18d1b47SKashyap D Desai
759665484d8SDoug Ambrisko }
760665484d8SDoug Ambrisko
7618e727371SKashyap D Desai /*
762d18d1b47SKashyap D Desai * mrsas_setup_msix: Allocate MSI-x vectors
7638e727371SKashyap D Desai * @sc: adapter soft state
764d18d1b47SKashyap D Desai */
7658e727371SKashyap D Desai static int
mrsas_setup_msix(struct mrsas_softc * sc)7668e727371SKashyap D Desai mrsas_setup_msix(struct mrsas_softc *sc)
767d18d1b47SKashyap D Desai {
768d18d1b47SKashyap D Desai int i;
7698e727371SKashyap D Desai
770d18d1b47SKashyap D Desai for (i = 0; i < sc->msix_vectors; i++) {
771d18d1b47SKashyap D Desai sc->irq_context[i].sc = sc;
772d18d1b47SKashyap D Desai sc->irq_context[i].MSIxIndex = i;
773d18d1b47SKashyap D Desai sc->irq_id[i] = i + 1;
774d18d1b47SKashyap D Desai sc->mrsas_irq[i] = bus_alloc_resource_any
775d18d1b47SKashyap D Desai (sc->mrsas_dev, SYS_RES_IRQ, &sc->irq_id[i]
776d18d1b47SKashyap D Desai ,RF_ACTIVE);
777d18d1b47SKashyap D Desai if (sc->mrsas_irq[i] == NULL) {
778d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "Can't allocate MSI-x\n");
779d18d1b47SKashyap D Desai goto irq_alloc_failed;
780d18d1b47SKashyap D Desai }
781d18d1b47SKashyap D Desai if (bus_setup_intr(sc->mrsas_dev,
782d18d1b47SKashyap D Desai sc->mrsas_irq[i],
783d18d1b47SKashyap D Desai INTR_MPSAFE | INTR_TYPE_CAM,
784d18d1b47SKashyap D Desai NULL, mrsas_isr, &sc->irq_context[i],
785d18d1b47SKashyap D Desai &sc->intr_handle[i])) {
786d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev,
787d18d1b47SKashyap D Desai "Cannot set up MSI-x interrupt handler\n");
788d18d1b47SKashyap D Desai goto irq_alloc_failed;
789d18d1b47SKashyap D Desai }
790d18d1b47SKashyap D Desai }
791d18d1b47SKashyap D Desai return SUCCESS;
792d18d1b47SKashyap D Desai
793d18d1b47SKashyap D Desai irq_alloc_failed:
794d18d1b47SKashyap D Desai mrsas_teardown_intr(sc);
795d18d1b47SKashyap D Desai return (FAIL);
796d18d1b47SKashyap D Desai }
797d18d1b47SKashyap D Desai
7988e727371SKashyap D Desai /*
799d18d1b47SKashyap D Desai * mrsas_allocate_msix: Setup MSI-x vectors
8008e727371SKashyap D Desai * @sc: adapter soft state
801d18d1b47SKashyap D Desai */
8028e727371SKashyap D Desai static int
mrsas_allocate_msix(struct mrsas_softc * sc)8038e727371SKashyap D Desai mrsas_allocate_msix(struct mrsas_softc *sc)
804d18d1b47SKashyap D Desai {
805d18d1b47SKashyap D Desai if (pci_alloc_msix(sc->mrsas_dev, &sc->msix_vectors) == 0) {
806d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "Using MSI-X with %d number"
807d18d1b47SKashyap D Desai " of vectors\n", sc->msix_vectors);
808d18d1b47SKashyap D Desai } else {
809d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "MSI-x setup failed\n");
810d18d1b47SKashyap D Desai goto irq_alloc_failed;
811d18d1b47SKashyap D Desai }
812d18d1b47SKashyap D Desai return SUCCESS;
813d18d1b47SKashyap D Desai
814d18d1b47SKashyap D Desai irq_alloc_failed:
815d18d1b47SKashyap D Desai mrsas_teardown_intr(sc);
816d18d1b47SKashyap D Desai return (FAIL);
817d18d1b47SKashyap D Desai }
8188e727371SKashyap D Desai
8198e727371SKashyap D Desai /*
820665484d8SDoug Ambrisko * mrsas_attach: PCI entry point
8218e727371SKashyap D Desai * input: pointer to device struct
822665484d8SDoug Ambrisko *
8238e727371SKashyap D Desai * Performs setup of PCI and registers, initializes mutexes and linked lists,
8248e727371SKashyap D Desai * registers interrupts and CAM, and initializes the adapter/controller to
8258e727371SKashyap D Desai * its proper state.
826665484d8SDoug Ambrisko */
8278e727371SKashyap D Desai static int
mrsas_attach(device_t dev)8288e727371SKashyap D Desai mrsas_attach(device_t dev)
829665484d8SDoug Ambrisko {
830665484d8SDoug Ambrisko struct mrsas_softc *sc = device_get_softc(dev);
8317aade8bfSKashyap D Desai uint32_t cmd, error;
832665484d8SDoug Ambrisko
8334bb0a4f0SKashyap D Desai memset(sc, 0, sizeof(struct mrsas_softc));
8344bb0a4f0SKashyap D Desai
835665484d8SDoug Ambrisko /* Look up our softc and initialize its fields. */
836665484d8SDoug Ambrisko sc->mrsas_dev = dev;
837665484d8SDoug Ambrisko sc->device_id = pci_get_device(dev);
838665484d8SDoug Ambrisko
8392909aab4SKashyap D Desai switch (sc->device_id) {
8402909aab4SKashyap D Desai case MRSAS_INVADER:
8412909aab4SKashyap D Desai case MRSAS_FURY:
8422909aab4SKashyap D Desai case MRSAS_INTRUDER:
8432909aab4SKashyap D Desai case MRSAS_INTRUDER_24:
8442909aab4SKashyap D Desai case MRSAS_CUTLASS_52:
8452909aab4SKashyap D Desai case MRSAS_CUTLASS_53:
846f9c63081SKashyap D Desai sc->mrsas_gen3_ctrl = 1;
8472909aab4SKashyap D Desai break;
8482909aab4SKashyap D Desai case MRSAS_VENTURA:
8492909aab4SKashyap D Desai case MRSAS_CRUSADER:
8502909aab4SKashyap D Desai case MRSAS_HARPOON:
8512909aab4SKashyap D Desai case MRSAS_TOMCAT:
8522909aab4SKashyap D Desai case MRSAS_VENTURA_4PORT:
8532909aab4SKashyap D Desai case MRSAS_CRUSADER_4PORT:
8547aade8bfSKashyap D Desai sc->is_ventura = true;
8552909aab4SKashyap D Desai break;
8562909aab4SKashyap D Desai case MRSAS_AERO_10E1:
8572909aab4SKashyap D Desai case MRSAS_AERO_10E5:
8582909aab4SKashyap D Desai device_printf(dev, "Adapter is in configurable secure mode\n");
8592909aab4SKashyap D Desai case MRSAS_AERO_10E2:
8602909aab4SKashyap D Desai case MRSAS_AERO_10E6:
8612909aab4SKashyap D Desai sc->is_aero = true;
8622909aab4SKashyap D Desai break;
8632909aab4SKashyap D Desai case MRSAS_AERO_10E0:
8642909aab4SKashyap D Desai case MRSAS_AERO_10E3:
8652909aab4SKashyap D Desai case MRSAS_AERO_10E4:
8662909aab4SKashyap D Desai case MRSAS_AERO_10E7:
8672909aab4SKashyap D Desai device_printf(dev, "Adapter is in non-secure mode\n");
8682909aab4SKashyap D Desai return SUCCESS;
869f9c63081SKashyap D Desai }
870f9c63081SKashyap D Desai
871665484d8SDoug Ambrisko mrsas_get_tunables(sc);
872665484d8SDoug Ambrisko
873665484d8SDoug Ambrisko /*
874665484d8SDoug Ambrisko * Set up PCI and registers
875665484d8SDoug Ambrisko */
876665484d8SDoug Ambrisko cmd = pci_read_config(dev, PCIR_COMMAND, 2);
877665484d8SDoug Ambrisko /* Force the busmaster enable bit on. */
878665484d8SDoug Ambrisko cmd |= PCIM_CMD_BUSMASTEREN;
879665484d8SDoug Ambrisko pci_write_config(dev, PCIR_COMMAND, cmd, 2);
880665484d8SDoug Ambrisko
8812909aab4SKashyap D Desai /* For Ventura/Aero system registers are mapped to BAR0 */
8822909aab4SKashyap D Desai if (sc->is_ventura || sc->is_aero)
8837aade8bfSKashyap D Desai sc->reg_res_id = PCIR_BAR(0); /* BAR0 offset */
8847aade8bfSKashyap D Desai else
8857aade8bfSKashyap D Desai sc->reg_res_id = PCIR_BAR(1); /* BAR1 offset */
886665484d8SDoug Ambrisko
88743cd6160SJustin Hibbits if ((sc->reg_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
88843cd6160SJustin Hibbits &(sc->reg_res_id), RF_ACTIVE))
889665484d8SDoug Ambrisko == NULL) {
890665484d8SDoug Ambrisko device_printf(dev, "Cannot allocate PCI registers\n");
891665484d8SDoug Ambrisko goto attach_fail;
892665484d8SDoug Ambrisko }
893665484d8SDoug Ambrisko sc->bus_tag = rman_get_bustag(sc->reg_res);
894665484d8SDoug Ambrisko sc->bus_handle = rman_get_bushandle(sc->reg_res);
895665484d8SDoug Ambrisko
896665484d8SDoug Ambrisko /* Intialize mutexes */
897665484d8SDoug Ambrisko mtx_init(&sc->sim_lock, "mrsas_sim_lock", NULL, MTX_DEF);
898665484d8SDoug Ambrisko mtx_init(&sc->pci_lock, "mrsas_pci_lock", NULL, MTX_DEF);
899665484d8SDoug Ambrisko mtx_init(&sc->io_lock, "mrsas_io_lock", NULL, MTX_DEF);
900665484d8SDoug Ambrisko mtx_init(&sc->aen_lock, "mrsas_aen_lock", NULL, MTX_DEF);
901665484d8SDoug Ambrisko mtx_init(&sc->ioctl_lock, "mrsas_ioctl_lock", NULL, MTX_SPIN);
902665484d8SDoug Ambrisko mtx_init(&sc->mpt_cmd_pool_lock, "mrsas_mpt_cmd_pool_lock", NULL, MTX_DEF);
903665484d8SDoug Ambrisko mtx_init(&sc->mfi_cmd_pool_lock, "mrsas_mfi_cmd_pool_lock", NULL, MTX_DEF);
904665484d8SDoug Ambrisko mtx_init(&sc->raidmap_lock, "mrsas_raidmap_lock", NULL, MTX_DEF);
905821df4b9SKashyap D Desai mtx_init(&sc->stream_lock, "mrsas_stream_lock", NULL, MTX_DEF);
906665484d8SDoug Ambrisko
907665484d8SDoug Ambrisko /* Intialize linked list */
908665484d8SDoug Ambrisko TAILQ_INIT(&sc->mrsas_mpt_cmd_list_head);
909665484d8SDoug Ambrisko TAILQ_INIT(&sc->mrsas_mfi_cmd_list_head);
910665484d8SDoug Ambrisko
911f5fb2237SKashyap D Desai mrsas_atomic_set(&sc->fw_outstanding, 0);
9128bb601acSKashyap D Desai mrsas_atomic_set(&sc->target_reset_outstanding, 0);
9133d273176SKashyap D Desai mrsas_atomic_set(&sc->prp_count, 0);
9143d273176SKashyap D Desai mrsas_atomic_set(&sc->sge_holes, 0);
915665484d8SDoug Ambrisko
916665484d8SDoug Ambrisko sc->io_cmds_highwater = 0;
917665484d8SDoug Ambrisko
918665484d8SDoug Ambrisko sc->adprecovery = MRSAS_HBA_OPERATIONAL;
919665484d8SDoug Ambrisko sc->UnevenSpanSupport = 0;
920665484d8SDoug Ambrisko
921d18d1b47SKashyap D Desai sc->msix_enable = 0;
922d18d1b47SKashyap D Desai
923665484d8SDoug Ambrisko /* Initialize Firmware */
924665484d8SDoug Ambrisko if (mrsas_init_fw(sc) != SUCCESS) {
925665484d8SDoug Ambrisko goto attach_fail_fw;
926665484d8SDoug Ambrisko }
9278071588dSKashyap D Desai /* Register mrsas to CAM layer */
928665484d8SDoug Ambrisko if ((mrsas_cam_attach(sc) != SUCCESS)) {
929665484d8SDoug Ambrisko goto attach_fail_cam;
930665484d8SDoug Ambrisko }
931665484d8SDoug Ambrisko /* Register IRQs */
932665484d8SDoug Ambrisko if (mrsas_setup_irq(sc) != SUCCESS) {
933665484d8SDoug Ambrisko goto attach_fail_irq;
934665484d8SDoug Ambrisko }
935665484d8SDoug Ambrisko error = mrsas_kproc_create(mrsas_ocr_thread, sc,
936665484d8SDoug Ambrisko &sc->ocr_thread, 0, 0, "mrsas_ocr%d",
937665484d8SDoug Ambrisko device_get_unit(sc->mrsas_dev));
938665484d8SDoug Ambrisko if (error) {
9398071588dSKashyap D Desai device_printf(sc->mrsas_dev, "Error %d starting OCR thread\n", error);
9408071588dSKashyap D Desai goto attach_fail_ocr_thread;
941665484d8SDoug Ambrisko }
942536094dcSKashyap D Desai /*
9438071588dSKashyap D Desai * After FW initialization and OCR thread creation
9448071588dSKashyap D Desai * we will defer the cdev creation, AEN setup on ICH callback
945536094dcSKashyap D Desai */
9468071588dSKashyap D Desai sc->mrsas_ich.ich_func = mrsas_ich_startup;
9478071588dSKashyap D Desai sc->mrsas_ich.ich_arg = sc;
9488071588dSKashyap D Desai if (config_intrhook_establish(&sc->mrsas_ich) != 0) {
9498071588dSKashyap D Desai device_printf(sc->mrsas_dev, "Config hook is already established\n");
9508071588dSKashyap D Desai }
9518071588dSKashyap D Desai mrsas_setup_sysctl(sc);
9528071588dSKashyap D Desai return SUCCESS;
953536094dcSKashyap D Desai
9548071588dSKashyap D Desai attach_fail_ocr_thread:
9558071588dSKashyap D Desai if (sc->ocr_thread_active)
9568071588dSKashyap D Desai wakeup(&sc->ocr_chan);
957665484d8SDoug Ambrisko attach_fail_irq:
958665484d8SDoug Ambrisko mrsas_teardown_intr(sc);
959665484d8SDoug Ambrisko attach_fail_cam:
960665484d8SDoug Ambrisko mrsas_cam_detach(sc);
961665484d8SDoug Ambrisko attach_fail_fw:
962d18d1b47SKashyap D Desai /* if MSIX vector is allocated and FW Init FAILED then release MSIX */
963d18d1b47SKashyap D Desai if (sc->msix_enable == 1)
964d18d1b47SKashyap D Desai pci_release_msi(sc->mrsas_dev);
965665484d8SDoug Ambrisko mrsas_free_mem(sc);
966665484d8SDoug Ambrisko mtx_destroy(&sc->sim_lock);
967665484d8SDoug Ambrisko mtx_destroy(&sc->aen_lock);
968665484d8SDoug Ambrisko mtx_destroy(&sc->pci_lock);
969665484d8SDoug Ambrisko mtx_destroy(&sc->io_lock);
970665484d8SDoug Ambrisko mtx_destroy(&sc->ioctl_lock);
971665484d8SDoug Ambrisko mtx_destroy(&sc->mpt_cmd_pool_lock);
972665484d8SDoug Ambrisko mtx_destroy(&sc->mfi_cmd_pool_lock);
973665484d8SDoug Ambrisko mtx_destroy(&sc->raidmap_lock);
974821df4b9SKashyap D Desai mtx_destroy(&sc->stream_lock);
975665484d8SDoug Ambrisko attach_fail:
976665484d8SDoug Ambrisko if (sc->reg_res) {
977665484d8SDoug Ambrisko bus_release_resource(sc->mrsas_dev, SYS_RES_MEMORY,
978665484d8SDoug Ambrisko sc->reg_res_id, sc->reg_res);
979665484d8SDoug Ambrisko }
980665484d8SDoug Ambrisko return (ENXIO);
981665484d8SDoug Ambrisko }
982665484d8SDoug Ambrisko
9838e727371SKashyap D Desai /*
9848071588dSKashyap D Desai * Interrupt config hook
9858071588dSKashyap D Desai */
9868071588dSKashyap D Desai static void
mrsas_ich_startup(void * arg)9878071588dSKashyap D Desai mrsas_ich_startup(void *arg)
9888071588dSKashyap D Desai {
98979b4460bSKashyap D Desai int i = 0;
9908071588dSKashyap D Desai struct mrsas_softc *sc = (struct mrsas_softc *)arg;
9918071588dSKashyap D Desai
9928071588dSKashyap D Desai /*
9938071588dSKashyap D Desai * Intialize a counting Semaphore to take care no. of concurrent IOCTLs
9948071588dSKashyap D Desai */
995731b7561SKashyap D Desai sema_init(&sc->ioctl_count_sema, MRSAS_MAX_IOCTL_CMDS,
9968071588dSKashyap D Desai IOCTL_SEMA_DESCRIPTION);
9978071588dSKashyap D Desai
9988071588dSKashyap D Desai /* Create a /dev entry for mrsas controller. */
9998071588dSKashyap D Desai sc->mrsas_cdev = make_dev(&mrsas_cdevsw, device_get_unit(sc->mrsas_dev), UID_ROOT,
10008071588dSKashyap D Desai GID_OPERATOR, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), "mrsas%u",
10018071588dSKashyap D Desai device_get_unit(sc->mrsas_dev));
10028071588dSKashyap D Desai
10038071588dSKashyap D Desai if (device_get_unit(sc->mrsas_dev) == 0) {
10048071588dSKashyap D Desai make_dev_alias_p(MAKEDEV_CHECKNAME,
10058071588dSKashyap D Desai &sc->mrsas_linux_emulator_cdev, sc->mrsas_cdev,
10068071588dSKashyap D Desai "megaraid_sas_ioctl_node");
10078071588dSKashyap D Desai }
10088071588dSKashyap D Desai if (sc->mrsas_cdev)
10098071588dSKashyap D Desai sc->mrsas_cdev->si_drv1 = sc;
10108071588dSKashyap D Desai
10118071588dSKashyap D Desai /*
10128071588dSKashyap D Desai * Add this controller to mrsas_mgmt_info structure so that it can be
10138071588dSKashyap D Desai * exported to management applications
10148071588dSKashyap D Desai */
10158071588dSKashyap D Desai if (device_get_unit(sc->mrsas_dev) == 0)
10168071588dSKashyap D Desai memset(&mrsas_mgmt_info, 0, sizeof(mrsas_mgmt_info));
10178071588dSKashyap D Desai
10188071588dSKashyap D Desai mrsas_mgmt_info.count++;
10198071588dSKashyap D Desai mrsas_mgmt_info.sc_ptr[mrsas_mgmt_info.max_index] = sc;
10208071588dSKashyap D Desai mrsas_mgmt_info.max_index++;
10218071588dSKashyap D Desai
10228071588dSKashyap D Desai /* Enable Interrupts */
10238071588dSKashyap D Desai mrsas_enable_intr(sc);
10248071588dSKashyap D Desai
102579b4460bSKashyap D Desai /* Call DCMD get_pd_info for all system PDs */
102679b4460bSKashyap D Desai for (i = 0; i < MRSAS_MAX_PD; i++) {
102779b4460bSKashyap D Desai if ((sc->target_list[i].target_id != 0xffff) &&
102879b4460bSKashyap D Desai sc->pd_info_mem)
102979b4460bSKashyap D Desai mrsas_get_pd_info(sc, sc->target_list[i].target_id);
103079b4460bSKashyap D Desai }
103179b4460bSKashyap D Desai
10328071588dSKashyap D Desai /* Initiate AEN (Asynchronous Event Notification) */
10338071588dSKashyap D Desai if (mrsas_start_aen(sc)) {
10348071588dSKashyap D Desai device_printf(sc->mrsas_dev, "Error: AEN registration FAILED !!! "
10358071588dSKashyap D Desai "Further events from the controller will not be communicated.\n"
10368071588dSKashyap D Desai "Either there is some problem in the controller"
10378071588dSKashyap D Desai "or the controller does not support AEN.\n"
10388071588dSKashyap D Desai "Please contact to the SUPPORT TEAM if the problem persists\n");
10398071588dSKashyap D Desai }
10408071588dSKashyap D Desai if (sc->mrsas_ich.ich_arg != NULL) {
10418071588dSKashyap D Desai device_printf(sc->mrsas_dev, "Disestablish mrsas intr hook\n");
10428071588dSKashyap D Desai config_intrhook_disestablish(&sc->mrsas_ich);
10438071588dSKashyap D Desai sc->mrsas_ich.ich_arg = NULL;
10448071588dSKashyap D Desai }
10458071588dSKashyap D Desai }
10468071588dSKashyap D Desai
10478071588dSKashyap D Desai /*
1048665484d8SDoug Ambrisko * mrsas_detach: De-allocates and teardown resources
10498e727371SKashyap D Desai * input: pointer to device struct
1050665484d8SDoug Ambrisko *
10518e727371SKashyap D Desai * This function is the entry point for device disconnect and detach.
10528e727371SKashyap D Desai * It performs memory de-allocations, shutdown of the controller and various
1053665484d8SDoug Ambrisko * teardown and destroy resource functions.
1054665484d8SDoug Ambrisko */
10558e727371SKashyap D Desai static int
mrsas_detach(device_t dev)10568e727371SKashyap D Desai mrsas_detach(device_t dev)
1057665484d8SDoug Ambrisko {
1058665484d8SDoug Ambrisko struct mrsas_softc *sc;
1059665484d8SDoug Ambrisko int i = 0;
1060665484d8SDoug Ambrisko
1061665484d8SDoug Ambrisko sc = device_get_softc(dev);
1062665484d8SDoug Ambrisko sc->remove_in_progress = 1;
1063536094dcSKashyap D Desai
1064839ee025SKashyap D Desai /* Destroy the character device so no other IOCTL will be handled */
10658071588dSKashyap D Desai if ((device_get_unit(dev) == 0) && sc->mrsas_linux_emulator_cdev)
10668071588dSKashyap D Desai destroy_dev(sc->mrsas_linux_emulator_cdev);
1067839ee025SKashyap D Desai destroy_dev(sc->mrsas_cdev);
1068839ee025SKashyap D Desai
1069536094dcSKashyap D Desai /*
1070536094dcSKashyap D Desai * Take the instance off the instance array. Note that we will not
1071536094dcSKashyap D Desai * decrement the max_index. We let this array be sparse array
1072536094dcSKashyap D Desai */
1073536094dcSKashyap D Desai for (i = 0; i < mrsas_mgmt_info.max_index; i++) {
1074536094dcSKashyap D Desai if (mrsas_mgmt_info.sc_ptr[i] == sc) {
1075536094dcSKashyap D Desai mrsas_mgmt_info.count--;
1076536094dcSKashyap D Desai mrsas_mgmt_info.sc_ptr[i] = NULL;
1077536094dcSKashyap D Desai break;
1078536094dcSKashyap D Desai }
1079536094dcSKashyap D Desai }
1080536094dcSKashyap D Desai
1081665484d8SDoug Ambrisko if (sc->ocr_thread_active)
1082665484d8SDoug Ambrisko wakeup(&sc->ocr_chan);
1083665484d8SDoug Ambrisko while (sc->reset_in_progress) {
1084665484d8SDoug Ambrisko i++;
1085665484d8SDoug Ambrisko if (!(i % MRSAS_RESET_NOTICE_INTERVAL)) {
1086665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_INFO,
1087f0c7594bSKashyap D Desai "[%2d]waiting for OCR to be finished from %s\n", i, __func__);
1088665484d8SDoug Ambrisko }
1089665484d8SDoug Ambrisko pause("mr_shutdown", hz);
1090665484d8SDoug Ambrisko }
1091665484d8SDoug Ambrisko i = 0;
1092665484d8SDoug Ambrisko while (sc->ocr_thread_active) {
1093665484d8SDoug Ambrisko i++;
1094665484d8SDoug Ambrisko if (!(i % MRSAS_RESET_NOTICE_INTERVAL)) {
1095665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_INFO,
1096665484d8SDoug Ambrisko "[%2d]waiting for "
1097665484d8SDoug Ambrisko "mrsas_ocr thread to quit ocr %d\n", i,
1098665484d8SDoug Ambrisko sc->ocr_thread_active);
1099665484d8SDoug Ambrisko }
1100665484d8SDoug Ambrisko pause("mr_shutdown", hz);
1101665484d8SDoug Ambrisko }
1102665484d8SDoug Ambrisko mrsas_flush_cache(sc);
1103665484d8SDoug Ambrisko mrsas_shutdown_ctlr(sc, MR_DCMD_CTRL_SHUTDOWN);
1104665484d8SDoug Ambrisko mrsas_disable_intr(sc);
1105821df4b9SKashyap D Desai
11062909aab4SKashyap D Desai if ((sc->is_ventura || sc->is_aero) && sc->streamDetectByLD) {
1107821df4b9SKashyap D Desai for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
1108821df4b9SKashyap D Desai free(sc->streamDetectByLD[i], M_MRSAS);
1109821df4b9SKashyap D Desai free(sc->streamDetectByLD, M_MRSAS);
1110821df4b9SKashyap D Desai sc->streamDetectByLD = NULL;
1111821df4b9SKashyap D Desai }
1112821df4b9SKashyap D Desai
1113665484d8SDoug Ambrisko mrsas_cam_detach(sc);
1114665484d8SDoug Ambrisko mrsas_teardown_intr(sc);
1115665484d8SDoug Ambrisko mrsas_free_mem(sc);
1116665484d8SDoug Ambrisko mtx_destroy(&sc->sim_lock);
1117665484d8SDoug Ambrisko mtx_destroy(&sc->aen_lock);
1118665484d8SDoug Ambrisko mtx_destroy(&sc->pci_lock);
1119665484d8SDoug Ambrisko mtx_destroy(&sc->io_lock);
1120665484d8SDoug Ambrisko mtx_destroy(&sc->ioctl_lock);
1121665484d8SDoug Ambrisko mtx_destroy(&sc->mpt_cmd_pool_lock);
1122665484d8SDoug Ambrisko mtx_destroy(&sc->mfi_cmd_pool_lock);
1123665484d8SDoug Ambrisko mtx_destroy(&sc->raidmap_lock);
1124821df4b9SKashyap D Desai mtx_destroy(&sc->stream_lock);
1125839ee025SKashyap D Desai
1126839ee025SKashyap D Desai /* Wait for all the semaphores to be released */
1127731b7561SKashyap D Desai while (sema_value(&sc->ioctl_count_sema) != MRSAS_MAX_IOCTL_CMDS)
1128839ee025SKashyap D Desai pause("mr_shutdown", hz);
1129839ee025SKashyap D Desai
1130839ee025SKashyap D Desai /* Destroy the counting semaphore created for Ioctl */
1131839ee025SKashyap D Desai sema_destroy(&sc->ioctl_count_sema);
1132839ee025SKashyap D Desai
1133665484d8SDoug Ambrisko if (sc->reg_res) {
1134665484d8SDoug Ambrisko bus_release_resource(sc->mrsas_dev,
1135665484d8SDoug Ambrisko SYS_RES_MEMORY, sc->reg_res_id, sc->reg_res);
1136665484d8SDoug Ambrisko }
1137665484d8SDoug Ambrisko if (sc->sysctl_tree != NULL)
1138665484d8SDoug Ambrisko sysctl_ctx_free(&sc->sysctl_ctx);
1139839ee025SKashyap D Desai
1140665484d8SDoug Ambrisko return (0);
1141665484d8SDoug Ambrisko }
1142665484d8SDoug Ambrisko
1143f28ecf2bSAndriy Gapon static int
mrsas_shutdown(device_t dev)1144f28ecf2bSAndriy Gapon mrsas_shutdown(device_t dev)
1145f28ecf2bSAndriy Gapon {
1146f28ecf2bSAndriy Gapon struct mrsas_softc *sc;
1147f28ecf2bSAndriy Gapon int i;
1148f28ecf2bSAndriy Gapon
1149f28ecf2bSAndriy Gapon sc = device_get_softc(dev);
1150f28ecf2bSAndriy Gapon sc->remove_in_progress = 1;
1151879e0604SMateusz Guzik if (!KERNEL_PANICKED()) {
1152f28ecf2bSAndriy Gapon if (sc->ocr_thread_active)
1153f28ecf2bSAndriy Gapon wakeup(&sc->ocr_chan);
1154f28ecf2bSAndriy Gapon i = 0;
1155f28ecf2bSAndriy Gapon while (sc->reset_in_progress && i < 15) {
1156f28ecf2bSAndriy Gapon i++;
1157f28ecf2bSAndriy Gapon if ((i % MRSAS_RESET_NOTICE_INTERVAL) == 0) {
1158f28ecf2bSAndriy Gapon mrsas_dprint(sc, MRSAS_INFO,
1159f28ecf2bSAndriy Gapon "[%2d]waiting for OCR to be finished "
1160f28ecf2bSAndriy Gapon "from %s\n", i, __func__);
1161f28ecf2bSAndriy Gapon }
1162f28ecf2bSAndriy Gapon pause("mr_shutdown", hz);
1163f28ecf2bSAndriy Gapon }
1164f28ecf2bSAndriy Gapon if (sc->reset_in_progress) {
1165f28ecf2bSAndriy Gapon mrsas_dprint(sc, MRSAS_INFO,
1166f28ecf2bSAndriy Gapon "gave up waiting for OCR to be finished\n");
116779c4c4beSChandrakanth Patil return (0);
1168f28ecf2bSAndriy Gapon }
1169f28ecf2bSAndriy Gapon }
1170f28ecf2bSAndriy Gapon
1171f28ecf2bSAndriy Gapon mrsas_flush_cache(sc);
1172f28ecf2bSAndriy Gapon mrsas_shutdown_ctlr(sc, MR_DCMD_CTRL_SHUTDOWN);
1173f28ecf2bSAndriy Gapon mrsas_disable_intr(sc);
1174f28ecf2bSAndriy Gapon return (0);
1175f28ecf2bSAndriy Gapon }
1176f28ecf2bSAndriy Gapon
11778e727371SKashyap D Desai /*
1178665484d8SDoug Ambrisko * mrsas_free_mem: Frees allocated memory
1179665484d8SDoug Ambrisko * input: Adapter instance soft state
1180665484d8SDoug Ambrisko *
1181665484d8SDoug Ambrisko * This function is called from mrsas_detach() to free previously allocated
1182665484d8SDoug Ambrisko * memory.
1183665484d8SDoug Ambrisko */
11848e727371SKashyap D Desai void
mrsas_free_mem(struct mrsas_softc * sc)11858e727371SKashyap D Desai mrsas_free_mem(struct mrsas_softc *sc)
1186665484d8SDoug Ambrisko {
1187665484d8SDoug Ambrisko int i;
11882a1d3bcdSKashyap D Desai u_int32_t max_fw_cmds;
1189665484d8SDoug Ambrisko struct mrsas_mfi_cmd *mfi_cmd;
1190665484d8SDoug Ambrisko struct mrsas_mpt_cmd *mpt_cmd;
1191665484d8SDoug Ambrisko
1192665484d8SDoug Ambrisko /*
1193665484d8SDoug Ambrisko * Free RAID map memory
1194665484d8SDoug Ambrisko */
11958e727371SKashyap D Desai for (i = 0; i < 2; i++) {
1196665484d8SDoug Ambrisko if (sc->raidmap_phys_addr[i])
1197665484d8SDoug Ambrisko bus_dmamap_unload(sc->raidmap_tag[i], sc->raidmap_dmamap[i]);
1198665484d8SDoug Ambrisko if (sc->raidmap_mem[i] != NULL)
1199665484d8SDoug Ambrisko bus_dmamem_free(sc->raidmap_tag[i], sc->raidmap_mem[i], sc->raidmap_dmamap[i]);
1200665484d8SDoug Ambrisko if (sc->raidmap_tag[i] != NULL)
1201665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->raidmap_tag[i]);
12024799d485SKashyap D Desai
12034799d485SKashyap D Desai if (sc->ld_drv_map[i] != NULL)
12044799d485SKashyap D Desai free(sc->ld_drv_map[i], M_MRSAS);
1205665484d8SDoug Ambrisko }
1206a688fcd0SKashyap D Desai for (i = 0; i < 2; i++) {
1207a688fcd0SKashyap D Desai if (sc->jbodmap_phys_addr[i])
1208a688fcd0SKashyap D Desai bus_dmamap_unload(sc->jbodmap_tag[i], sc->jbodmap_dmamap[i]);
1209a688fcd0SKashyap D Desai if (sc->jbodmap_mem[i] != NULL)
1210a688fcd0SKashyap D Desai bus_dmamem_free(sc->jbodmap_tag[i], sc->jbodmap_mem[i], sc->jbodmap_dmamap[i]);
1211a688fcd0SKashyap D Desai if (sc->jbodmap_tag[i] != NULL)
1212a688fcd0SKashyap D Desai bus_dma_tag_destroy(sc->jbodmap_tag[i]);
1213a688fcd0SKashyap D Desai }
1214665484d8SDoug Ambrisko /*
1215453130d9SPedro F. Giffuni * Free version buffer memory
1216665484d8SDoug Ambrisko */
1217665484d8SDoug Ambrisko if (sc->verbuf_phys_addr)
1218665484d8SDoug Ambrisko bus_dmamap_unload(sc->verbuf_tag, sc->verbuf_dmamap);
1219665484d8SDoug Ambrisko if (sc->verbuf_mem != NULL)
1220665484d8SDoug Ambrisko bus_dmamem_free(sc->verbuf_tag, sc->verbuf_mem, sc->verbuf_dmamap);
1221665484d8SDoug Ambrisko if (sc->verbuf_tag != NULL)
1222665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->verbuf_tag);
1223665484d8SDoug Ambrisko
1224665484d8SDoug Ambrisko /*
1225665484d8SDoug Ambrisko * Free sense buffer memory
1226665484d8SDoug Ambrisko */
1227665484d8SDoug Ambrisko if (sc->sense_phys_addr)
1228665484d8SDoug Ambrisko bus_dmamap_unload(sc->sense_tag, sc->sense_dmamap);
1229665484d8SDoug Ambrisko if (sc->sense_mem != NULL)
1230665484d8SDoug Ambrisko bus_dmamem_free(sc->sense_tag, sc->sense_mem, sc->sense_dmamap);
1231665484d8SDoug Ambrisko if (sc->sense_tag != NULL)
1232665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->sense_tag);
1233665484d8SDoug Ambrisko
1234665484d8SDoug Ambrisko /*
1235665484d8SDoug Ambrisko * Free chain frame memory
1236665484d8SDoug Ambrisko */
1237665484d8SDoug Ambrisko if (sc->chain_frame_phys_addr)
1238665484d8SDoug Ambrisko bus_dmamap_unload(sc->chain_frame_tag, sc->chain_frame_dmamap);
1239665484d8SDoug Ambrisko if (sc->chain_frame_mem != NULL)
1240665484d8SDoug Ambrisko bus_dmamem_free(sc->chain_frame_tag, sc->chain_frame_mem, sc->chain_frame_dmamap);
1241665484d8SDoug Ambrisko if (sc->chain_frame_tag != NULL)
1242665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->chain_frame_tag);
1243665484d8SDoug Ambrisko
1244665484d8SDoug Ambrisko /*
1245665484d8SDoug Ambrisko * Free IO Request memory
1246665484d8SDoug Ambrisko */
1247665484d8SDoug Ambrisko if (sc->io_request_phys_addr)
1248665484d8SDoug Ambrisko bus_dmamap_unload(sc->io_request_tag, sc->io_request_dmamap);
1249665484d8SDoug Ambrisko if (sc->io_request_mem != NULL)
1250665484d8SDoug Ambrisko bus_dmamem_free(sc->io_request_tag, sc->io_request_mem, sc->io_request_dmamap);
1251665484d8SDoug Ambrisko if (sc->io_request_tag != NULL)
1252665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->io_request_tag);
1253665484d8SDoug Ambrisko
1254665484d8SDoug Ambrisko /*
1255665484d8SDoug Ambrisko * Free Reply Descriptor memory
1256665484d8SDoug Ambrisko */
1257665484d8SDoug Ambrisko if (sc->reply_desc_phys_addr)
1258665484d8SDoug Ambrisko bus_dmamap_unload(sc->reply_desc_tag, sc->reply_desc_dmamap);
1259665484d8SDoug Ambrisko if (sc->reply_desc_mem != NULL)
1260665484d8SDoug Ambrisko bus_dmamem_free(sc->reply_desc_tag, sc->reply_desc_mem, sc->reply_desc_dmamap);
1261665484d8SDoug Ambrisko if (sc->reply_desc_tag != NULL)
1262665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->reply_desc_tag);
1263665484d8SDoug Ambrisko
1264665484d8SDoug Ambrisko /*
1265665484d8SDoug Ambrisko * Free event detail memory
1266665484d8SDoug Ambrisko */
1267665484d8SDoug Ambrisko if (sc->evt_detail_phys_addr)
1268665484d8SDoug Ambrisko bus_dmamap_unload(sc->evt_detail_tag, sc->evt_detail_dmamap);
1269665484d8SDoug Ambrisko if (sc->evt_detail_mem != NULL)
1270665484d8SDoug Ambrisko bus_dmamem_free(sc->evt_detail_tag, sc->evt_detail_mem, sc->evt_detail_dmamap);
1271665484d8SDoug Ambrisko if (sc->evt_detail_tag != NULL)
1272665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->evt_detail_tag);
1273665484d8SDoug Ambrisko
1274665484d8SDoug Ambrisko /*
127579b4460bSKashyap D Desai * Free PD info memory
127679b4460bSKashyap D Desai */
127779b4460bSKashyap D Desai if (sc->pd_info_phys_addr)
127879b4460bSKashyap D Desai bus_dmamap_unload(sc->pd_info_tag, sc->pd_info_dmamap);
127979b4460bSKashyap D Desai if (sc->pd_info_mem != NULL)
128079b4460bSKashyap D Desai bus_dmamem_free(sc->pd_info_tag, sc->pd_info_mem, sc->pd_info_dmamap);
128179b4460bSKashyap D Desai if (sc->pd_info_tag != NULL)
128279b4460bSKashyap D Desai bus_dma_tag_destroy(sc->pd_info_tag);
128379b4460bSKashyap D Desai
128479b4460bSKashyap D Desai /*
1285665484d8SDoug Ambrisko * Free MFI frames
1286665484d8SDoug Ambrisko */
1287665484d8SDoug Ambrisko if (sc->mfi_cmd_list) {
1288665484d8SDoug Ambrisko for (i = 0; i < MRSAS_MAX_MFI_CMDS; i++) {
1289665484d8SDoug Ambrisko mfi_cmd = sc->mfi_cmd_list[i];
1290665484d8SDoug Ambrisko mrsas_free_frame(sc, mfi_cmd);
1291665484d8SDoug Ambrisko }
1292665484d8SDoug Ambrisko }
1293665484d8SDoug Ambrisko if (sc->mficmd_frame_tag != NULL)
1294665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->mficmd_frame_tag);
1295665484d8SDoug Ambrisko
1296665484d8SDoug Ambrisko /*
1297665484d8SDoug Ambrisko * Free MPT internal command list
1298665484d8SDoug Ambrisko */
12992a1d3bcdSKashyap D Desai max_fw_cmds = sc->max_fw_cmds;
1300665484d8SDoug Ambrisko if (sc->mpt_cmd_list) {
13012a1d3bcdSKashyap D Desai for (i = 0; i < max_fw_cmds; i++) {
1302665484d8SDoug Ambrisko mpt_cmd = sc->mpt_cmd_list[i];
1303665484d8SDoug Ambrisko bus_dmamap_destroy(sc->data_tag, mpt_cmd->data_dmamap);
1304665484d8SDoug Ambrisko free(sc->mpt_cmd_list[i], M_MRSAS);
1305665484d8SDoug Ambrisko }
1306665484d8SDoug Ambrisko free(sc->mpt_cmd_list, M_MRSAS);
1307665484d8SDoug Ambrisko sc->mpt_cmd_list = NULL;
1308665484d8SDoug Ambrisko }
1309665484d8SDoug Ambrisko /*
1310665484d8SDoug Ambrisko * Free MFI internal command list
1311665484d8SDoug Ambrisko */
1312665484d8SDoug Ambrisko
1313665484d8SDoug Ambrisko if (sc->mfi_cmd_list) {
1314665484d8SDoug Ambrisko for (i = 0; i < MRSAS_MAX_MFI_CMDS; i++) {
1315665484d8SDoug Ambrisko free(sc->mfi_cmd_list[i], M_MRSAS);
1316665484d8SDoug Ambrisko }
1317665484d8SDoug Ambrisko free(sc->mfi_cmd_list, M_MRSAS);
1318665484d8SDoug Ambrisko sc->mfi_cmd_list = NULL;
1319665484d8SDoug Ambrisko }
1320665484d8SDoug Ambrisko /*
1321665484d8SDoug Ambrisko * Free request descriptor memory
1322665484d8SDoug Ambrisko */
1323665484d8SDoug Ambrisko free(sc->req_desc, M_MRSAS);
1324665484d8SDoug Ambrisko sc->req_desc = NULL;
1325665484d8SDoug Ambrisko
1326665484d8SDoug Ambrisko /*
1327665484d8SDoug Ambrisko * Destroy parent tag
1328665484d8SDoug Ambrisko */
1329665484d8SDoug Ambrisko if (sc->mrsas_parent_tag != NULL)
1330665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->mrsas_parent_tag);
1331af51c29fSKashyap D Desai
1332af51c29fSKashyap D Desai /*
1333af51c29fSKashyap D Desai * Free ctrl_info memory
1334af51c29fSKashyap D Desai */
1335af51c29fSKashyap D Desai if (sc->ctrl_info != NULL)
1336af51c29fSKashyap D Desai free(sc->ctrl_info, M_MRSAS);
1337665484d8SDoug Ambrisko }
1338665484d8SDoug Ambrisko
13398e727371SKashyap D Desai /*
1340665484d8SDoug Ambrisko * mrsas_teardown_intr: Teardown interrupt
1341665484d8SDoug Ambrisko * input: Adapter instance soft state
1342665484d8SDoug Ambrisko *
13438e727371SKashyap D Desai * This function is called from mrsas_detach() to teardown and release bus
13448e727371SKashyap D Desai * interrupt resourse.
1345665484d8SDoug Ambrisko */
13468e727371SKashyap D Desai void
mrsas_teardown_intr(struct mrsas_softc * sc)13478e727371SKashyap D Desai mrsas_teardown_intr(struct mrsas_softc *sc)
1348665484d8SDoug Ambrisko {
1349d18d1b47SKashyap D Desai int i;
13508e727371SKashyap D Desai
1351d18d1b47SKashyap D Desai if (!sc->msix_enable) {
1352d18d1b47SKashyap D Desai if (sc->intr_handle[0])
1353d18d1b47SKashyap D Desai bus_teardown_intr(sc->mrsas_dev, sc->mrsas_irq[0], sc->intr_handle[0]);
1354d18d1b47SKashyap D Desai if (sc->mrsas_irq[0] != NULL)
13558e727371SKashyap D Desai bus_release_resource(sc->mrsas_dev, SYS_RES_IRQ,
13568e727371SKashyap D Desai sc->irq_id[0], sc->mrsas_irq[0]);
1357d18d1b47SKashyap D Desai sc->intr_handle[0] = NULL;
1358d18d1b47SKashyap D Desai } else {
1359d18d1b47SKashyap D Desai for (i = 0; i < sc->msix_vectors; i++) {
1360d18d1b47SKashyap D Desai if (sc->intr_handle[i])
1361d18d1b47SKashyap D Desai bus_teardown_intr(sc->mrsas_dev, sc->mrsas_irq[i],
1362d18d1b47SKashyap D Desai sc->intr_handle[i]);
1363d18d1b47SKashyap D Desai
1364d18d1b47SKashyap D Desai if (sc->mrsas_irq[i] != NULL)
1365d18d1b47SKashyap D Desai bus_release_resource(sc->mrsas_dev, SYS_RES_IRQ,
1366d18d1b47SKashyap D Desai sc->irq_id[i], sc->mrsas_irq[i]);
1367d18d1b47SKashyap D Desai
1368d18d1b47SKashyap D Desai sc->intr_handle[i] = NULL;
1369d18d1b47SKashyap D Desai }
1370d18d1b47SKashyap D Desai pci_release_msi(sc->mrsas_dev);
1371d18d1b47SKashyap D Desai }
1372d18d1b47SKashyap D Desai
1373665484d8SDoug Ambrisko }
1374665484d8SDoug Ambrisko
13758e727371SKashyap D Desai /*
1376665484d8SDoug Ambrisko * mrsas_suspend: Suspend entry point
1377665484d8SDoug Ambrisko * input: Device struct pointer
1378665484d8SDoug Ambrisko *
1379665484d8SDoug Ambrisko * This function is the entry point for system suspend from the OS.
1380665484d8SDoug Ambrisko */
13818e727371SKashyap D Desai static int
mrsas_suspend(device_t dev)13828e727371SKashyap D Desai mrsas_suspend(device_t dev)
1383665484d8SDoug Ambrisko {
13844bb0a4f0SKashyap D Desai /* This will be filled when the driver will have hibernation support */
1385665484d8SDoug Ambrisko return (0);
1386665484d8SDoug Ambrisko }
1387665484d8SDoug Ambrisko
13888e727371SKashyap D Desai /*
1389665484d8SDoug Ambrisko * mrsas_resume: Resume entry point
1390665484d8SDoug Ambrisko * input: Device struct pointer
1391665484d8SDoug Ambrisko *
1392665484d8SDoug Ambrisko * This function is the entry point for system resume from the OS.
1393665484d8SDoug Ambrisko */
13948e727371SKashyap D Desai static int
mrsas_resume(device_t dev)13958e727371SKashyap D Desai mrsas_resume(device_t dev)
1396665484d8SDoug Ambrisko {
13974bb0a4f0SKashyap D Desai /* This will be filled when the driver will have hibernation support */
1398665484d8SDoug Ambrisko return (0);
1399665484d8SDoug Ambrisko }
1400665484d8SDoug Ambrisko
14015844115eSKashyap D Desai /**
14025844115eSKashyap D Desai * mrsas_get_softc_instance: Find softc instance based on cmd type
14035844115eSKashyap D Desai *
14045844115eSKashyap D Desai * This function will return softc instance based on cmd type.
14055844115eSKashyap D Desai * In some case, application fire ioctl on required management instance and
14065844115eSKashyap D Desai * do not provide host_no. Use cdev->si_drv1 to get softc instance for those
14075844115eSKashyap D Desai * case, else get the softc instance from host_no provided by application in
14085844115eSKashyap D Desai * user data.
14095844115eSKashyap D Desai */
14105844115eSKashyap D Desai
14115844115eSKashyap D Desai static struct mrsas_softc *
mrsas_get_softc_instance(struct cdev * dev,u_long cmd,caddr_t arg)14125844115eSKashyap D Desai mrsas_get_softc_instance(struct cdev *dev, u_long cmd, caddr_t arg)
14135844115eSKashyap D Desai {
14145844115eSKashyap D Desai struct mrsas_softc *sc = NULL;
14155844115eSKashyap D Desai struct mrsas_iocpacket *user_ioc = (struct mrsas_iocpacket *)arg;
1416dbcc81dfSKashyap D Desai
14175844115eSKashyap D Desai if (cmd == MRSAS_IOC_GET_PCI_INFO) {
14185844115eSKashyap D Desai sc = dev->si_drv1;
14195844115eSKashyap D Desai } else {
1420dbcc81dfSKashyap D Desai /*
1421dbcc81dfSKashyap D Desai * get the Host number & the softc from data sent by the
1422dbcc81dfSKashyap D Desai * Application
1423dbcc81dfSKashyap D Desai */
14245844115eSKashyap D Desai sc = mrsas_mgmt_info.sc_ptr[user_ioc->host_no];
14255844115eSKashyap D Desai if (sc == NULL)
14265bae00d6SSteven Hartland printf("There is no Controller number %d\n",
14275bae00d6SSteven Hartland user_ioc->host_no);
14285bae00d6SSteven Hartland else if (user_ioc->host_no >= mrsas_mgmt_info.max_index)
14295844115eSKashyap D Desai mrsas_dprint(sc, MRSAS_FAULT,
14305bae00d6SSteven Hartland "Invalid Controller number %d\n", user_ioc->host_no);
14315844115eSKashyap D Desai }
14325844115eSKashyap D Desai
14335844115eSKashyap D Desai return sc;
14345844115eSKashyap D Desai }
14355844115eSKashyap D Desai
14368e727371SKashyap D Desai /*
1437665484d8SDoug Ambrisko * mrsas_ioctl: IOCtl commands entry point.
1438665484d8SDoug Ambrisko *
1439665484d8SDoug Ambrisko * This function is the entry point for IOCtls from the OS. It calls the
1440665484d8SDoug Ambrisko * appropriate function for processing depending on the command received.
1441665484d8SDoug Ambrisko */
1442665484d8SDoug Ambrisko static int
mrsas_ioctl(struct cdev * dev,u_long cmd,caddr_t arg,int flag,struct thread * td)14437fc5f329SJohn Baldwin mrsas_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag,
14447fc5f329SJohn Baldwin struct thread *td)
1445665484d8SDoug Ambrisko {
1446665484d8SDoug Ambrisko struct mrsas_softc *sc;
1447665484d8SDoug Ambrisko int ret = 0, i = 0;
14485844115eSKashyap D Desai MRSAS_DRV_PCI_INFORMATION *pciDrvInfo;
1449665484d8SDoug Ambrisko
1450e315351fSDoug Ambrisko switch (cmd) {
1451e315351fSDoug Ambrisko case MFIIO_PASSTHRU:
1452e315351fSDoug Ambrisko sc = (struct mrsas_softc *)(dev->si_drv1);
1453e315351fSDoug Ambrisko break;
1454e315351fSDoug Ambrisko default:
14555844115eSKashyap D Desai sc = mrsas_get_softc_instance(dev, cmd, arg);
1456e315351fSDoug Ambrisko break;
1457e315351fSDoug Ambrisko }
14585844115eSKashyap D Desai if (!sc)
1459536094dcSKashyap D Desai return ENOENT;
14605844115eSKashyap D Desai
1461808517a4SKashyap D Desai if (sc->remove_in_progress ||
1462808517a4SKashyap D Desai (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR)) {
1463665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_INFO,
1464808517a4SKashyap D Desai "Either driver remove or shutdown called or "
1465808517a4SKashyap D Desai "HW is in unrecoverable critical error state.\n");
1466665484d8SDoug Ambrisko return ENOENT;
1467665484d8SDoug Ambrisko }
1468665484d8SDoug Ambrisko mtx_lock_spin(&sc->ioctl_lock);
1469665484d8SDoug Ambrisko if (!sc->reset_in_progress) {
1470665484d8SDoug Ambrisko mtx_unlock_spin(&sc->ioctl_lock);
1471665484d8SDoug Ambrisko goto do_ioctl;
1472665484d8SDoug Ambrisko }
1473665484d8SDoug Ambrisko mtx_unlock_spin(&sc->ioctl_lock);
1474665484d8SDoug Ambrisko while (sc->reset_in_progress) {
1475665484d8SDoug Ambrisko i++;
1476665484d8SDoug Ambrisko if (!(i % MRSAS_RESET_NOTICE_INTERVAL)) {
1477665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_INFO,
1478f0c7594bSKashyap D Desai "[%2d]waiting for OCR to be finished from %s\n", i, __func__);
1479665484d8SDoug Ambrisko }
1480665484d8SDoug Ambrisko pause("mr_ioctl", hz);
1481665484d8SDoug Ambrisko }
1482665484d8SDoug Ambrisko
1483665484d8SDoug Ambrisko do_ioctl:
1484665484d8SDoug Ambrisko switch (cmd) {
1485536094dcSKashyap D Desai case MRSAS_IOC_FIRMWARE_PASS_THROUGH64:
1486536094dcSKashyap D Desai #ifdef COMPAT_FREEBSD32
1487536094dcSKashyap D Desai case MRSAS_IOC_FIRMWARE_PASS_THROUGH32:
1488536094dcSKashyap D Desai #endif
14898e727371SKashyap D Desai /*
14908e727371SKashyap D Desai * Decrement the Ioctl counting Semaphore before getting an
14918e727371SKashyap D Desai * mfi command
14928e727371SKashyap D Desai */
1493839ee025SKashyap D Desai sema_wait(&sc->ioctl_count_sema);
1494839ee025SKashyap D Desai
1495536094dcSKashyap D Desai ret = mrsas_passthru(sc, (void *)arg, cmd);
1496839ee025SKashyap D Desai
1497839ee025SKashyap D Desai /* Increment the Ioctl counting semaphore value */
1498839ee025SKashyap D Desai sema_post(&sc->ioctl_count_sema);
1499839ee025SKashyap D Desai
1500665484d8SDoug Ambrisko break;
1501665484d8SDoug Ambrisko case MRSAS_IOC_SCAN_BUS:
1502665484d8SDoug Ambrisko ret = mrsas_bus_scan(sc);
1503665484d8SDoug Ambrisko break;
15045844115eSKashyap D Desai
15055844115eSKashyap D Desai case MRSAS_IOC_GET_PCI_INFO:
15065844115eSKashyap D Desai pciDrvInfo = (MRSAS_DRV_PCI_INFORMATION *) arg;
15075844115eSKashyap D Desai memset(pciDrvInfo, 0, sizeof(MRSAS_DRV_PCI_INFORMATION));
15085844115eSKashyap D Desai pciDrvInfo->busNumber = pci_get_bus(sc->mrsas_dev);
15095844115eSKashyap D Desai pciDrvInfo->deviceNumber = pci_get_slot(sc->mrsas_dev);
15105844115eSKashyap D Desai pciDrvInfo->functionNumber = pci_get_function(sc->mrsas_dev);
15115844115eSKashyap D Desai pciDrvInfo->domainID = pci_get_domain(sc->mrsas_dev);
15125844115eSKashyap D Desai mrsas_dprint(sc, MRSAS_INFO, "pci bus no: %d,"
15135844115eSKashyap D Desai "pci device no: %d, pci function no: %d,"
15145844115eSKashyap D Desai "pci domain ID: %d\n",
15155844115eSKashyap D Desai pciDrvInfo->busNumber, pciDrvInfo->deviceNumber,
15165844115eSKashyap D Desai pciDrvInfo->functionNumber, pciDrvInfo->domainID);
15175844115eSKashyap D Desai ret = 0;
15185844115eSKashyap D Desai break;
15195844115eSKashyap D Desai
1520e315351fSDoug Ambrisko case MFIIO_PASSTHRU:
1521e315351fSDoug Ambrisko ret = mrsas_user_command(sc, (struct mfi_ioc_passthru *)arg);
1522e315351fSDoug Ambrisko break;
1523e315351fSDoug Ambrisko
1524536094dcSKashyap D Desai default:
1525536094dcSKashyap D Desai mrsas_dprint(sc, MRSAS_TRACE, "IOCTL command 0x%lx is not handled\n", cmd);
1526839ee025SKashyap D Desai ret = ENOENT;
1527665484d8SDoug Ambrisko }
1528665484d8SDoug Ambrisko
1529665484d8SDoug Ambrisko return (ret);
1530665484d8SDoug Ambrisko }
1531665484d8SDoug Ambrisko
15328e727371SKashyap D Desai /*
1533da011113SKashyap D Desai * mrsas_poll: poll entry point for mrsas driver fd
1534da011113SKashyap D Desai *
15358e727371SKashyap D Desai * This function is the entry point for poll from the OS. It waits for some AEN
15368e727371SKashyap D Desai * events to be triggered from the controller and notifies back.
1537da011113SKashyap D Desai */
1538da011113SKashyap D Desai static int
mrsas_poll(struct cdev * dev,int poll_events,struct thread * td)1539da011113SKashyap D Desai mrsas_poll(struct cdev *dev, int poll_events, struct thread *td)
1540da011113SKashyap D Desai {
1541da011113SKashyap D Desai struct mrsas_softc *sc;
1542da011113SKashyap D Desai int revents = 0;
1543da011113SKashyap D Desai
1544da011113SKashyap D Desai sc = dev->si_drv1;
1545da011113SKashyap D Desai
1546da011113SKashyap D Desai if (poll_events & (POLLIN | POLLRDNORM)) {
1547da011113SKashyap D Desai if (sc->mrsas_aen_triggered) {
1548da011113SKashyap D Desai revents |= poll_events & (POLLIN | POLLRDNORM);
1549da011113SKashyap D Desai }
1550da011113SKashyap D Desai }
1551da011113SKashyap D Desai if (revents == 0) {
1552da011113SKashyap D Desai if (poll_events & (POLLIN | POLLRDNORM)) {
1553ecea5be4SKashyap D Desai mtx_lock(&sc->aen_lock);
1554da011113SKashyap D Desai sc->mrsas_poll_waiting = 1;
1555da011113SKashyap D Desai selrecord(td, &sc->mrsas_select);
1556ecea5be4SKashyap D Desai mtx_unlock(&sc->aen_lock);
1557da011113SKashyap D Desai }
1558da011113SKashyap D Desai }
1559da011113SKashyap D Desai return revents;
1560da011113SKashyap D Desai }
1561da011113SKashyap D Desai
15628e727371SKashyap D Desai /*
15638e727371SKashyap D Desai * mrsas_setup_irq: Set up interrupt
1564665484d8SDoug Ambrisko * input: Adapter instance soft state
1565665484d8SDoug Ambrisko *
1566665484d8SDoug Ambrisko * This function sets up interrupts as a bus resource, with flags indicating
1567665484d8SDoug Ambrisko * resource permitting contemporaneous sharing and for resource to activate
1568665484d8SDoug Ambrisko * atomically.
1569665484d8SDoug Ambrisko */
15708e727371SKashyap D Desai static int
mrsas_setup_irq(struct mrsas_softc * sc)15718e727371SKashyap D Desai mrsas_setup_irq(struct mrsas_softc *sc)
1572665484d8SDoug Ambrisko {
1573d18d1b47SKashyap D Desai if (sc->msix_enable && (mrsas_setup_msix(sc) == SUCCESS))
1574d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "MSI-x interrupts setup success\n");
1575665484d8SDoug Ambrisko
1576d18d1b47SKashyap D Desai else {
1577d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "Fall back to legacy interrupt\n");
1578d18d1b47SKashyap D Desai sc->irq_context[0].sc = sc;
1579d18d1b47SKashyap D Desai sc->irq_context[0].MSIxIndex = 0;
1580d18d1b47SKashyap D Desai sc->irq_id[0] = 0;
1581d18d1b47SKashyap D Desai sc->mrsas_irq[0] = bus_alloc_resource_any(sc->mrsas_dev,
1582d18d1b47SKashyap D Desai SYS_RES_IRQ, &sc->irq_id[0], RF_SHAREABLE | RF_ACTIVE);
1583d18d1b47SKashyap D Desai if (sc->mrsas_irq[0] == NULL) {
1584d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "Cannot allocate legcay"
1585d18d1b47SKashyap D Desai "interrupt\n");
1586d18d1b47SKashyap D Desai return (FAIL);
1587d18d1b47SKashyap D Desai }
1588d18d1b47SKashyap D Desai if (bus_setup_intr(sc->mrsas_dev, sc->mrsas_irq[0],
1589d18d1b47SKashyap D Desai INTR_MPSAFE | INTR_TYPE_CAM, NULL, mrsas_isr,
1590d18d1b47SKashyap D Desai &sc->irq_context[0], &sc->intr_handle[0])) {
1591d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "Cannot set up legacy"
1592d18d1b47SKashyap D Desai "interrupt\n");
1593d18d1b47SKashyap D Desai return (FAIL);
1594d18d1b47SKashyap D Desai }
1595d18d1b47SKashyap D Desai }
1596665484d8SDoug Ambrisko return (0);
1597665484d8SDoug Ambrisko }
1598665484d8SDoug Ambrisko
1599665484d8SDoug Ambrisko /*
1600665484d8SDoug Ambrisko * mrsas_isr: ISR entry point
1601665484d8SDoug Ambrisko * input: argument pointer
1602665484d8SDoug Ambrisko *
16038e727371SKashyap D Desai * This function is the interrupt service routine entry point. There are two
16048e727371SKashyap D Desai * types of interrupts, state change interrupt and response interrupt. If an
16058e727371SKashyap D Desai * interrupt is not ours, we just return.
1606665484d8SDoug Ambrisko */
16078e727371SKashyap D Desai void
mrsas_isr(void * arg)16088e727371SKashyap D Desai mrsas_isr(void *arg)
1609665484d8SDoug Ambrisko {
1610d18d1b47SKashyap D Desai struct mrsas_irq_context *irq_context = (struct mrsas_irq_context *)arg;
1611d18d1b47SKashyap D Desai struct mrsas_softc *sc = irq_context->sc;
1612d18d1b47SKashyap D Desai int status = 0;
1613665484d8SDoug Ambrisko
16142f863eb8SKashyap D Desai if (sc->mask_interrupts)
16152f863eb8SKashyap D Desai return;
16162f863eb8SKashyap D Desai
1617d18d1b47SKashyap D Desai if (!sc->msix_vectors) {
1618665484d8SDoug Ambrisko status = mrsas_clear_intr(sc);
1619665484d8SDoug Ambrisko if (!status)
1620665484d8SDoug Ambrisko return;
1621d18d1b47SKashyap D Desai }
1622665484d8SDoug Ambrisko /* If we are resetting, bail */
1623f5fb2237SKashyap D Desai if (mrsas_test_bit(MRSAS_FUSION_IN_RESET, &sc->reset_flags)) {
1624665484d8SDoug Ambrisko printf(" Entered into ISR when OCR is going active. \n");
1625665484d8SDoug Ambrisko mrsas_clear_intr(sc);
1626665484d8SDoug Ambrisko return;
1627665484d8SDoug Ambrisko }
1628665484d8SDoug Ambrisko /* Process for reply request and clear response interrupt */
1629d18d1b47SKashyap D Desai if (mrsas_complete_cmd(sc, irq_context->MSIxIndex) != SUCCESS)
1630665484d8SDoug Ambrisko mrsas_clear_intr(sc);
1631665484d8SDoug Ambrisko
1632665484d8SDoug Ambrisko return;
1633665484d8SDoug Ambrisko }
1634665484d8SDoug Ambrisko
1635665484d8SDoug Ambrisko /*
1636665484d8SDoug Ambrisko * mrsas_complete_cmd: Process reply request
1637665484d8SDoug Ambrisko * input: Adapter instance soft state
1638665484d8SDoug Ambrisko *
16398e727371SKashyap D Desai * This function is called from mrsas_isr() to process reply request and clear
16408e727371SKashyap D Desai * response interrupt. Processing of the reply request entails walking
16418e727371SKashyap D Desai * through the reply descriptor array for the command request pended from
16428e727371SKashyap D Desai * Firmware. We look at the Function field to determine the command type and
16438e727371SKashyap D Desai * perform the appropriate action. Before we return, we clear the response
16448e727371SKashyap D Desai * interrupt.
1645665484d8SDoug Ambrisko */
16464bb0a4f0SKashyap D Desai int
mrsas_complete_cmd(struct mrsas_softc * sc,u_int32_t MSIxIndex)16478e727371SKashyap D Desai mrsas_complete_cmd(struct mrsas_softc *sc, u_int32_t MSIxIndex)
1648665484d8SDoug Ambrisko {
1649665484d8SDoug Ambrisko Mpi2ReplyDescriptorsUnion_t *desc;
1650665484d8SDoug Ambrisko MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1651665484d8SDoug Ambrisko MRSAS_RAID_SCSI_IO_REQUEST *scsi_io_req;
16522a1d3bcdSKashyap D Desai struct mrsas_mpt_cmd *cmd_mpt, *r1_cmd = NULL;
1653665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd_mfi;
16542a1d3bcdSKashyap D Desai u_int8_t reply_descript_type, *sense;
1655665484d8SDoug Ambrisko u_int16_t smid, num_completed;
1656665484d8SDoug Ambrisko u_int8_t status, extStatus;
1657665484d8SDoug Ambrisko union desc_value desc_val;
1658665484d8SDoug Ambrisko PLD_LOAD_BALANCE_INFO lbinfo;
16592a1d3bcdSKashyap D Desai u_int32_t device_id, data_length;
1660665484d8SDoug Ambrisko int threshold_reply_count = 0;
16618bb601acSKashyap D Desai #if TM_DEBUG
16628bb601acSKashyap D Desai MR_TASK_MANAGE_REQUEST *mr_tm_req;
16638bb601acSKashyap D Desai MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
16648bb601acSKashyap D Desai #endif
1665665484d8SDoug Ambrisko
1666665484d8SDoug Ambrisko /* If we have a hardware error, not need to continue */
1667665484d8SDoug Ambrisko if (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR)
1668665484d8SDoug Ambrisko return (DONE);
1669665484d8SDoug Ambrisko
1670665484d8SDoug Ambrisko desc = sc->reply_desc_mem;
1671d18d1b47SKashyap D Desai desc += ((MSIxIndex * sc->reply_alloc_sz) / sizeof(MPI2_REPLY_DESCRIPTORS_UNION))
1672d18d1b47SKashyap D Desai + sc->last_reply_idx[MSIxIndex];
1673665484d8SDoug Ambrisko
1674665484d8SDoug Ambrisko reply_desc = (MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *) desc;
1675665484d8SDoug Ambrisko
1676665484d8SDoug Ambrisko desc_val.word = desc->Words;
1677665484d8SDoug Ambrisko num_completed = 0;
1678665484d8SDoug Ambrisko
1679665484d8SDoug Ambrisko reply_descript_type = reply_desc->ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1680665484d8SDoug Ambrisko
1681665484d8SDoug Ambrisko /* Find our reply descriptor for the command and process */
16828e727371SKashyap D Desai while ((desc_val.u.low != 0xFFFFFFFF) && (desc_val.u.high != 0xFFFFFFFF)) {
1683e34a057cSAlfredo Dal'Ava Junior smid = le16toh(reply_desc->SMID);
1684665484d8SDoug Ambrisko cmd_mpt = sc->mpt_cmd_list[smid - 1];
1685665484d8SDoug Ambrisko scsi_io_req = (MRSAS_RAID_SCSI_IO_REQUEST *) cmd_mpt->io_request;
1686665484d8SDoug Ambrisko
1687503c4f8dSKashyap D Desai status = scsi_io_req->RaidContext.raid_context.status;
1688503c4f8dSKashyap D Desai extStatus = scsi_io_req->RaidContext.raid_context.exStatus;
16892a1d3bcdSKashyap D Desai sense = cmd_mpt->sense;
16902a1d3bcdSKashyap D Desai data_length = scsi_io_req->DataLength;
1691665484d8SDoug Ambrisko
16928e727371SKashyap D Desai switch (scsi_io_req->Function) {
16938bb601acSKashyap D Desai case MPI2_FUNCTION_SCSI_TASK_MGMT:
16948bb601acSKashyap D Desai #if TM_DEBUG
16958bb601acSKashyap D Desai mr_tm_req = (MR_TASK_MANAGE_REQUEST *) cmd_mpt->io_request;
16968bb601acSKashyap D Desai mpi_tm_req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)
16978bb601acSKashyap D Desai &mr_tm_req->TmRequest;
16988bb601acSKashyap D Desai device_printf(sc->mrsas_dev, "TM completion type 0x%X, "
16998bb601acSKashyap D Desai "TaskMID: 0x%X", mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
17008bb601acSKashyap D Desai #endif
17018bb601acSKashyap D Desai wakeup_one((void *)&sc->ocr_chan);
17028bb601acSKashyap D Desai break;
1703665484d8SDoug Ambrisko case MPI2_FUNCTION_SCSI_IO_REQUEST: /* Fast Path IO. */
1704665484d8SDoug Ambrisko device_id = cmd_mpt->ccb_ptr->ccb_h.target_id;
1705665484d8SDoug Ambrisko lbinfo = &sc->load_balance_info[device_id];
17062a1d3bcdSKashyap D Desai /* R1 load balancing for READ */
1707665484d8SDoug Ambrisko if (cmd_mpt->load_balance == MRSAS_LOAD_BALANCE_FLAG) {
170816dc2814SKashyap D Desai mrsas_atomic_dec(&lbinfo->scsi_pending_cmds[cmd_mpt->pd_r1_lb]);
1709665484d8SDoug Ambrisko cmd_mpt->load_balance &= ~MRSAS_LOAD_BALANCE_FLAG;
1710665484d8SDoug Ambrisko }
17118e727371SKashyap D Desai /* Fall thru and complete IO */
1712665484d8SDoug Ambrisko case MRSAS_MPI2_FUNCTION_LD_IO_REQUEST:
17132a1d3bcdSKashyap D Desai if (cmd_mpt->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
17142a1d3bcdSKashyap D Desai mrsas_map_mpt_cmd_status(cmd_mpt, cmd_mpt->ccb_ptr, status,
1715e34a057cSAlfredo Dal'Ava Junior extStatus, le32toh(data_length), sense);
1716665484d8SDoug Ambrisko mrsas_cmd_done(sc, cmd_mpt);
17175437c8b8SKashyap D Desai mrsas_atomic_dec(&sc->fw_outstanding);
17182a1d3bcdSKashyap D Desai } else {
17192a1d3bcdSKashyap D Desai /*
17202a1d3bcdSKashyap D Desai * If the peer Raid 1/10 fast path failed,
17212a1d3bcdSKashyap D Desai * mark IO as failed to the scsi layer.
17222a1d3bcdSKashyap D Desai * Overwrite the current status by the failed status
17232a1d3bcdSKashyap D Desai * and make sure that if any command fails,
17242a1d3bcdSKashyap D Desai * driver returns fail status to CAM.
17252a1d3bcdSKashyap D Desai */
17262a1d3bcdSKashyap D Desai cmd_mpt->cmd_completed = 1;
17272a1d3bcdSKashyap D Desai r1_cmd = cmd_mpt->peer_cmd;
17282a1d3bcdSKashyap D Desai if (r1_cmd->cmd_completed) {
17292a1d3bcdSKashyap D Desai if (r1_cmd->io_request->RaidContext.raid_context.status != MFI_STAT_OK) {
17302a1d3bcdSKashyap D Desai status = r1_cmd->io_request->RaidContext.raid_context.status;
17312a1d3bcdSKashyap D Desai extStatus = r1_cmd->io_request->RaidContext.raid_context.exStatus;
17322a1d3bcdSKashyap D Desai data_length = r1_cmd->io_request->DataLength;
17332a1d3bcdSKashyap D Desai sense = r1_cmd->sense;
17342a1d3bcdSKashyap D Desai }
1735*4640df1bSMark Johnston mtx_lock(&sc->sim_lock);
17362a1d3bcdSKashyap D Desai r1_cmd->ccb_ptr = NULL;
17372a1d3bcdSKashyap D Desai if (r1_cmd->callout_owner) {
17382a1d3bcdSKashyap D Desai callout_stop(&r1_cmd->cm_callout);
17392a1d3bcdSKashyap D Desai r1_cmd->callout_owner = false;
17402a1d3bcdSKashyap D Desai }
1741*4640df1bSMark Johnston mtx_unlock(&sc->sim_lock);
17422a1d3bcdSKashyap D Desai mrsas_release_mpt_cmd(r1_cmd);
17435437c8b8SKashyap D Desai mrsas_atomic_dec(&sc->fw_outstanding);
17442a1d3bcdSKashyap D Desai mrsas_map_mpt_cmd_status(cmd_mpt, cmd_mpt->ccb_ptr, status,
1745e34a057cSAlfredo Dal'Ava Junior extStatus, le32toh(data_length), sense);
17462a1d3bcdSKashyap D Desai mrsas_cmd_done(sc, cmd_mpt);
1747f5fb2237SKashyap D Desai mrsas_atomic_dec(&sc->fw_outstanding);
17485437c8b8SKashyap D Desai }
17495437c8b8SKashyap D Desai }
1750665484d8SDoug Ambrisko break;
1751665484d8SDoug Ambrisko case MRSAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /* MFI command */
1752665484d8SDoug Ambrisko cmd_mfi = sc->mfi_cmd_list[cmd_mpt->sync_cmd_idx];
1753731b7561SKashyap D Desai /*
1754731b7561SKashyap D Desai * Make sure NOT TO release the mfi command from the called
1755731b7561SKashyap D Desai * function's context if it is fired with issue_polled call.
1756731b7561SKashyap D Desai * And also make sure that the issue_polled call should only be
1757731b7561SKashyap D Desai * used if INTERRUPT IS DISABLED.
1758731b7561SKashyap D Desai */
1759e34a057cSAlfredo Dal'Ava Junior if (cmd_mfi->frame->hdr.flags & htole16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
1760731b7561SKashyap D Desai mrsas_release_mfi_cmd(cmd_mfi);
1761731b7561SKashyap D Desai else
1762665484d8SDoug Ambrisko mrsas_complete_mptmfi_passthru(sc, cmd_mfi, status);
1763665484d8SDoug Ambrisko break;
1764665484d8SDoug Ambrisko }
1765665484d8SDoug Ambrisko
1766d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex]++;
1767d18d1b47SKashyap D Desai if (sc->last_reply_idx[MSIxIndex] >= sc->reply_q_depth)
1768d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex] = 0;
1769665484d8SDoug Ambrisko
17708e727371SKashyap D Desai desc->Words = ~((uint64_t)0x00); /* set it back to all
17718e727371SKashyap D Desai * 0xFFFFFFFFs */
1772665484d8SDoug Ambrisko num_completed++;
1773665484d8SDoug Ambrisko threshold_reply_count++;
1774665484d8SDoug Ambrisko
1775665484d8SDoug Ambrisko /* Get the next reply descriptor */
1776d18d1b47SKashyap D Desai if (!sc->last_reply_idx[MSIxIndex]) {
1777665484d8SDoug Ambrisko desc = sc->reply_desc_mem;
1778d18d1b47SKashyap D Desai desc += ((MSIxIndex * sc->reply_alloc_sz) / sizeof(MPI2_REPLY_DESCRIPTORS_UNION));
1779d18d1b47SKashyap D Desai } else
1780665484d8SDoug Ambrisko desc++;
1781665484d8SDoug Ambrisko
1782665484d8SDoug Ambrisko reply_desc = (MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *) desc;
1783665484d8SDoug Ambrisko desc_val.word = desc->Words;
1784665484d8SDoug Ambrisko
1785665484d8SDoug Ambrisko reply_descript_type = reply_desc->ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1786665484d8SDoug Ambrisko
1787665484d8SDoug Ambrisko if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1788665484d8SDoug Ambrisko break;
1789665484d8SDoug Ambrisko
1790665484d8SDoug Ambrisko /*
17918e727371SKashyap D Desai * Write to reply post index after completing threshold reply
17928e727371SKashyap D Desai * count and still there are more replies in reply queue
17938e727371SKashyap D Desai * pending to be completed.
1794665484d8SDoug Ambrisko */
1795665484d8SDoug Ambrisko if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
1796d18d1b47SKashyap D Desai if (sc->msix_enable) {
17977aade8bfSKashyap D Desai if (sc->msix_combined)
1798d18d1b47SKashyap D Desai mrsas_write_reg(sc, sc->msix_reg_offset[MSIxIndex / 8],
1799d18d1b47SKashyap D Desai ((MSIxIndex & 0x7) << 24) |
1800d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex]);
1801d18d1b47SKashyap D Desai else
1802d18d1b47SKashyap D Desai mrsas_write_reg(sc, sc->msix_reg_offset[0], (MSIxIndex << 24) |
1803d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex]);
1804d18d1b47SKashyap D Desai } else
1805d18d1b47SKashyap D Desai mrsas_write_reg(sc, offsetof(mrsas_reg_set,
1806d18d1b47SKashyap D Desai reply_post_host_index), sc->last_reply_idx[0]);
1807d18d1b47SKashyap D Desai
1808665484d8SDoug Ambrisko threshold_reply_count = 0;
1809665484d8SDoug Ambrisko }
1810665484d8SDoug Ambrisko }
1811665484d8SDoug Ambrisko
1812665484d8SDoug Ambrisko /* No match, just return */
1813665484d8SDoug Ambrisko if (num_completed == 0)
1814665484d8SDoug Ambrisko return (DONE);
1815665484d8SDoug Ambrisko
1816665484d8SDoug Ambrisko /* Clear response interrupt */
1817d18d1b47SKashyap D Desai if (sc->msix_enable) {
18187aade8bfSKashyap D Desai if (sc->msix_combined) {
1819d18d1b47SKashyap D Desai mrsas_write_reg(sc, sc->msix_reg_offset[MSIxIndex / 8],
1820d18d1b47SKashyap D Desai ((MSIxIndex & 0x7) << 24) |
1821d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex]);
1822d18d1b47SKashyap D Desai } else
1823d18d1b47SKashyap D Desai mrsas_write_reg(sc, sc->msix_reg_offset[0], (MSIxIndex << 24) |
1824d18d1b47SKashyap D Desai sc->last_reply_idx[MSIxIndex]);
1825d18d1b47SKashyap D Desai } else
1826d18d1b47SKashyap D Desai mrsas_write_reg(sc, offsetof(mrsas_reg_set,
1827d18d1b47SKashyap D Desai reply_post_host_index), sc->last_reply_idx[0]);
1828665484d8SDoug Ambrisko
1829665484d8SDoug Ambrisko return (0);
1830665484d8SDoug Ambrisko }
1831665484d8SDoug Ambrisko
1832665484d8SDoug Ambrisko /*
1833665484d8SDoug Ambrisko * mrsas_map_mpt_cmd_status: Allocate DMAable memory.
1834665484d8SDoug Ambrisko * input: Adapter instance soft state
1835665484d8SDoug Ambrisko *
1836665484d8SDoug Ambrisko * This function is called from mrsas_complete_cmd(), for LD IO and FastPath IO.
18378e727371SKashyap D Desai * It checks the command status and maps the appropriate CAM status for the
18388e727371SKashyap D Desai * CCB.
1839665484d8SDoug Ambrisko */
18408e727371SKashyap D Desai void
mrsas_map_mpt_cmd_status(struct mrsas_mpt_cmd * cmd,union ccb * ccb_ptr,u_int8_t status,u_int8_t extStatus,u_int32_t data_length,u_int8_t * sense)18412a1d3bcdSKashyap D Desai mrsas_map_mpt_cmd_status(struct mrsas_mpt_cmd *cmd, union ccb *ccb_ptr, u_int8_t status,
18422a1d3bcdSKashyap D Desai u_int8_t extStatus, u_int32_t data_length, u_int8_t *sense)
1843665484d8SDoug Ambrisko {
1844665484d8SDoug Ambrisko struct mrsas_softc *sc = cmd->sc;
1845665484d8SDoug Ambrisko u_int8_t *sense_data;
1846665484d8SDoug Ambrisko
1847665484d8SDoug Ambrisko switch (status) {
1848665484d8SDoug Ambrisko case MFI_STAT_OK:
18492a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status = CAM_REQ_CMP;
1850665484d8SDoug Ambrisko break;
1851665484d8SDoug Ambrisko case MFI_STAT_SCSI_IO_FAILED:
1852665484d8SDoug Ambrisko case MFI_STAT_SCSI_DONE_WITH_ERROR:
18532a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status = CAM_SCSI_STATUS_ERROR;
18542a1d3bcdSKashyap D Desai sense_data = (u_int8_t *)&ccb_ptr->csio.sense_data;
1855665484d8SDoug Ambrisko if (sense_data) {
1856665484d8SDoug Ambrisko /* For now just copy 18 bytes back */
18572a1d3bcdSKashyap D Desai memcpy(sense_data, sense, 18);
18582a1d3bcdSKashyap D Desai ccb_ptr->csio.sense_len = 18;
18592a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status |= CAM_AUTOSNS_VALID;
1860665484d8SDoug Ambrisko }
1861665484d8SDoug Ambrisko break;
1862665484d8SDoug Ambrisko case MFI_STAT_LD_OFFLINE:
1863665484d8SDoug Ambrisko case MFI_STAT_DEVICE_NOT_FOUND:
18642a1d3bcdSKashyap D Desai if (ccb_ptr->ccb_h.target_lun)
18652a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status |= CAM_LUN_INVALID;
1866665484d8SDoug Ambrisko else
18672a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status |= CAM_DEV_NOT_THERE;
1868665484d8SDoug Ambrisko break;
1869665484d8SDoug Ambrisko case MFI_STAT_CONFIG_SEQ_MISMATCH:
18702a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status |= CAM_REQUEUE_REQ;
1871665484d8SDoug Ambrisko break;
1872665484d8SDoug Ambrisko default:
1873665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "FW cmd complete status %x\n", status);
18742a1d3bcdSKashyap D Desai ccb_ptr->ccb_h.status = CAM_REQ_CMP_ERR;
18752a1d3bcdSKashyap D Desai ccb_ptr->csio.scsi_status = status;
1876665484d8SDoug Ambrisko }
1877665484d8SDoug Ambrisko return;
1878665484d8SDoug Ambrisko }
1879665484d8SDoug Ambrisko
1880665484d8SDoug Ambrisko /*
18818e727371SKashyap D Desai * mrsas_alloc_mem: Allocate DMAable memory
1882665484d8SDoug Ambrisko * input: Adapter instance soft state
1883665484d8SDoug Ambrisko *
18848e727371SKashyap D Desai * This function creates the parent DMA tag and allocates DMAable memory. DMA
18858e727371SKashyap D Desai * tag describes constraints of DMA mapping. Memory allocated is mapped into
18868e727371SKashyap D Desai * Kernel virtual address. Callback argument is physical memory address.
1887665484d8SDoug Ambrisko */
18888e727371SKashyap D Desai static int
mrsas_alloc_mem(struct mrsas_softc * sc)18898e727371SKashyap D Desai mrsas_alloc_mem(struct mrsas_softc *sc)
1890665484d8SDoug Ambrisko {
18914ad83576SKashyap D Desai u_int32_t verbuf_size, io_req_size, reply_desc_size, sense_size, chain_frame_size,
189279b4460bSKashyap D Desai evt_detail_size, count, pd_info_size;
1893665484d8SDoug Ambrisko
1894665484d8SDoug Ambrisko /*
1895665484d8SDoug Ambrisko * Allocate parent DMA tag
1896665484d8SDoug Ambrisko */
1897fa3d57c2SAlexander Motin if (bus_dma_tag_create(
1898fa3d57c2SAlexander Motin bus_get_dma_tag(sc->mrsas_dev), /* parent */
1899665484d8SDoug Ambrisko 1, /* alignment */
1900665484d8SDoug Ambrisko 0, /* boundary */
1901665484d8SDoug Ambrisko BUS_SPACE_MAXADDR, /* lowaddr */
1902665484d8SDoug Ambrisko BUS_SPACE_MAXADDR, /* highaddr */
1903665484d8SDoug Ambrisko NULL, NULL, /* filter, filterarg */
1904fa3d57c2SAlexander Motin BUS_SPACE_MAXSIZE, /* maxsize */
1905fa3d57c2SAlexander Motin BUS_SPACE_UNRESTRICTED, /* nsegments */
1906fa3d57c2SAlexander Motin BUS_SPACE_MAXSIZE, /* maxsegsize */
1907665484d8SDoug Ambrisko 0, /* flags */
1908665484d8SDoug Ambrisko NULL, NULL, /* lockfunc, lockarg */
1909665484d8SDoug Ambrisko &sc->mrsas_parent_tag /* tag */
1910665484d8SDoug Ambrisko )) {
1911665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate parent DMA tag\n");
1912665484d8SDoug Ambrisko return (ENOMEM);
1913665484d8SDoug Ambrisko }
1914665484d8SDoug Ambrisko /*
1915665484d8SDoug Ambrisko * Allocate for version buffer
1916665484d8SDoug Ambrisko */
1917665484d8SDoug Ambrisko verbuf_size = MRSAS_MAX_NAME_LENGTH * (sizeof(bus_addr_t));
19188e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
19198e727371SKashyap D Desai 1, 0,
19208e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
19218e727371SKashyap D Desai BUS_SPACE_MAXADDR,
19228e727371SKashyap D Desai NULL, NULL,
19238e727371SKashyap D Desai verbuf_size,
19248e727371SKashyap D Desai 1,
19258e727371SKashyap D Desai verbuf_size,
19268e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
19278e727371SKashyap D Desai NULL, NULL,
1928665484d8SDoug Ambrisko &sc->verbuf_tag)) {
1929665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate verbuf DMA tag\n");
1930665484d8SDoug Ambrisko return (ENOMEM);
1931665484d8SDoug Ambrisko }
1932665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->verbuf_tag, (void **)&sc->verbuf_mem,
1933665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->verbuf_dmamap)) {
1934665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate verbuf memory\n");
1935665484d8SDoug Ambrisko return (ENOMEM);
1936665484d8SDoug Ambrisko }
1937665484d8SDoug Ambrisko bzero(sc->verbuf_mem, verbuf_size);
1938665484d8SDoug Ambrisko if (bus_dmamap_load(sc->verbuf_tag, sc->verbuf_dmamap, sc->verbuf_mem,
19398e727371SKashyap D Desai verbuf_size, mrsas_addr_cb, &sc->verbuf_phys_addr,
19408e727371SKashyap D Desai BUS_DMA_NOWAIT)) {
1941665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load verbuf DMA map\n");
1942665484d8SDoug Ambrisko return (ENOMEM);
1943665484d8SDoug Ambrisko }
1944665484d8SDoug Ambrisko /*
1945665484d8SDoug Ambrisko * Allocate IO Request Frames
1946665484d8SDoug Ambrisko */
1947665484d8SDoug Ambrisko io_req_size = sc->io_frames_alloc_sz;
19488e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
19498e727371SKashyap D Desai 16, 0,
19508e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
19518e727371SKashyap D Desai BUS_SPACE_MAXADDR,
19528e727371SKashyap D Desai NULL, NULL,
19538e727371SKashyap D Desai io_req_size,
19548e727371SKashyap D Desai 1,
19558e727371SKashyap D Desai io_req_size,
19568e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
19578e727371SKashyap D Desai NULL, NULL,
1958665484d8SDoug Ambrisko &sc->io_request_tag)) {
1959665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot create IO request tag\n");
1960665484d8SDoug Ambrisko return (ENOMEM);
1961665484d8SDoug Ambrisko }
1962665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->io_request_tag, (void **)&sc->io_request_mem,
1963665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->io_request_dmamap)) {
1964665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc IO request memory\n");
1965665484d8SDoug Ambrisko return (ENOMEM);
1966665484d8SDoug Ambrisko }
1967665484d8SDoug Ambrisko bzero(sc->io_request_mem, io_req_size);
1968665484d8SDoug Ambrisko if (bus_dmamap_load(sc->io_request_tag, sc->io_request_dmamap,
1969665484d8SDoug Ambrisko sc->io_request_mem, io_req_size, mrsas_addr_cb,
1970665484d8SDoug Ambrisko &sc->io_request_phys_addr, BUS_DMA_NOWAIT)) {
1971665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load IO request memory\n");
1972665484d8SDoug Ambrisko return (ENOMEM);
1973665484d8SDoug Ambrisko }
1974665484d8SDoug Ambrisko /*
1975665484d8SDoug Ambrisko * Allocate Chain Frames
1976665484d8SDoug Ambrisko */
1977665484d8SDoug Ambrisko chain_frame_size = sc->chain_frames_alloc_sz;
19788e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
19798e727371SKashyap D Desai 4, 0,
19808e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
19818e727371SKashyap D Desai BUS_SPACE_MAXADDR,
19828e727371SKashyap D Desai NULL, NULL,
19838e727371SKashyap D Desai chain_frame_size,
19848e727371SKashyap D Desai 1,
19858e727371SKashyap D Desai chain_frame_size,
19868e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
19878e727371SKashyap D Desai NULL, NULL,
1988665484d8SDoug Ambrisko &sc->chain_frame_tag)) {
1989665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot create chain frame tag\n");
1990665484d8SDoug Ambrisko return (ENOMEM);
1991665484d8SDoug Ambrisko }
1992665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->chain_frame_tag, (void **)&sc->chain_frame_mem,
1993665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->chain_frame_dmamap)) {
1994665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc chain frame memory\n");
1995665484d8SDoug Ambrisko return (ENOMEM);
1996665484d8SDoug Ambrisko }
1997665484d8SDoug Ambrisko bzero(sc->chain_frame_mem, chain_frame_size);
1998665484d8SDoug Ambrisko if (bus_dmamap_load(sc->chain_frame_tag, sc->chain_frame_dmamap,
1999665484d8SDoug Ambrisko sc->chain_frame_mem, chain_frame_size, mrsas_addr_cb,
2000665484d8SDoug Ambrisko &sc->chain_frame_phys_addr, BUS_DMA_NOWAIT)) {
2001665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load chain frame memory\n");
2002665484d8SDoug Ambrisko return (ENOMEM);
2003665484d8SDoug Ambrisko }
2004d18d1b47SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
2005665484d8SDoug Ambrisko /*
2006665484d8SDoug Ambrisko * Allocate Reply Descriptor Array
2007665484d8SDoug Ambrisko */
2008d18d1b47SKashyap D Desai reply_desc_size = sc->reply_alloc_sz * count;
20098e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
20108e727371SKashyap D Desai 16, 0,
20118e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
20128e727371SKashyap D Desai BUS_SPACE_MAXADDR,
20138e727371SKashyap D Desai NULL, NULL,
20148e727371SKashyap D Desai reply_desc_size,
20158e727371SKashyap D Desai 1,
20168e727371SKashyap D Desai reply_desc_size,
20178e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
20188e727371SKashyap D Desai NULL, NULL,
2019665484d8SDoug Ambrisko &sc->reply_desc_tag)) {
2020665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot create reply descriptor tag\n");
2021665484d8SDoug Ambrisko return (ENOMEM);
2022665484d8SDoug Ambrisko }
2023665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->reply_desc_tag, (void **)&sc->reply_desc_mem,
2024665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->reply_desc_dmamap)) {
2025665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc reply descriptor memory\n");
2026665484d8SDoug Ambrisko return (ENOMEM);
2027665484d8SDoug Ambrisko }
2028665484d8SDoug Ambrisko if (bus_dmamap_load(sc->reply_desc_tag, sc->reply_desc_dmamap,
2029665484d8SDoug Ambrisko sc->reply_desc_mem, reply_desc_size, mrsas_addr_cb,
2030665484d8SDoug Ambrisko &sc->reply_desc_phys_addr, BUS_DMA_NOWAIT)) {
2031665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load reply descriptor memory\n");
2032665484d8SDoug Ambrisko return (ENOMEM);
2033665484d8SDoug Ambrisko }
2034665484d8SDoug Ambrisko /*
2035665484d8SDoug Ambrisko * Allocate Sense Buffer Array. Keep in lower 4GB
2036665484d8SDoug Ambrisko */
2037665484d8SDoug Ambrisko sense_size = sc->max_fw_cmds * MRSAS_SENSE_LEN;
20388e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
20398e727371SKashyap D Desai 64, 0,
20408e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
20418e727371SKashyap D Desai BUS_SPACE_MAXADDR,
20428e727371SKashyap D Desai NULL, NULL,
20438e727371SKashyap D Desai sense_size,
20448e727371SKashyap D Desai 1,
20458e727371SKashyap D Desai sense_size,
20468e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
20478e727371SKashyap D Desai NULL, NULL,
2048665484d8SDoug Ambrisko &sc->sense_tag)) {
2049665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate sense buf tag\n");
2050665484d8SDoug Ambrisko return (ENOMEM);
2051665484d8SDoug Ambrisko }
2052665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->sense_tag, (void **)&sc->sense_mem,
2053665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->sense_dmamap)) {
2054665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate sense buf memory\n");
2055665484d8SDoug Ambrisko return (ENOMEM);
2056665484d8SDoug Ambrisko }
2057665484d8SDoug Ambrisko if (bus_dmamap_load(sc->sense_tag, sc->sense_dmamap,
2058665484d8SDoug Ambrisko sc->sense_mem, sense_size, mrsas_addr_cb, &sc->sense_phys_addr,
2059665484d8SDoug Ambrisko BUS_DMA_NOWAIT)) {
2060665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load sense buf memory\n");
2061665484d8SDoug Ambrisko return (ENOMEM);
2062665484d8SDoug Ambrisko }
20632a1d3bcdSKashyap D Desai
2064665484d8SDoug Ambrisko /*
2065665484d8SDoug Ambrisko * Allocate for Event detail structure
2066665484d8SDoug Ambrisko */
2067665484d8SDoug Ambrisko evt_detail_size = sizeof(struct mrsas_evt_detail);
20688e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
20698e727371SKashyap D Desai 1, 0,
20708e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
20718e727371SKashyap D Desai BUS_SPACE_MAXADDR,
20728e727371SKashyap D Desai NULL, NULL,
20738e727371SKashyap D Desai evt_detail_size,
20748e727371SKashyap D Desai 1,
20758e727371SKashyap D Desai evt_detail_size,
20768e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
20778e727371SKashyap D Desai NULL, NULL,
2078665484d8SDoug Ambrisko &sc->evt_detail_tag)) {
2079665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot create Event detail tag\n");
2080665484d8SDoug Ambrisko return (ENOMEM);
2081665484d8SDoug Ambrisko }
2082665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->evt_detail_tag, (void **)&sc->evt_detail_mem,
2083665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->evt_detail_dmamap)) {
2084665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc Event detail buffer memory\n");
2085665484d8SDoug Ambrisko return (ENOMEM);
2086665484d8SDoug Ambrisko }
2087665484d8SDoug Ambrisko bzero(sc->evt_detail_mem, evt_detail_size);
2088665484d8SDoug Ambrisko if (bus_dmamap_load(sc->evt_detail_tag, sc->evt_detail_dmamap,
2089665484d8SDoug Ambrisko sc->evt_detail_mem, evt_detail_size, mrsas_addr_cb,
2090665484d8SDoug Ambrisko &sc->evt_detail_phys_addr, BUS_DMA_NOWAIT)) {
2091665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load Event detail buffer memory\n");
2092665484d8SDoug Ambrisko return (ENOMEM);
2093665484d8SDoug Ambrisko }
209479b4460bSKashyap D Desai
209579b4460bSKashyap D Desai /*
209679b4460bSKashyap D Desai * Allocate for PD INFO structure
209779b4460bSKashyap D Desai */
209879b4460bSKashyap D Desai pd_info_size = sizeof(struct mrsas_pd_info);
209979b4460bSKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
210079b4460bSKashyap D Desai 1, 0,
210179b4460bSKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
210279b4460bSKashyap D Desai BUS_SPACE_MAXADDR,
210379b4460bSKashyap D Desai NULL, NULL,
210479b4460bSKashyap D Desai pd_info_size,
210579b4460bSKashyap D Desai 1,
210679b4460bSKashyap D Desai pd_info_size,
210779b4460bSKashyap D Desai BUS_DMA_ALLOCNOW,
210879b4460bSKashyap D Desai NULL, NULL,
210979b4460bSKashyap D Desai &sc->pd_info_tag)) {
211079b4460bSKashyap D Desai device_printf(sc->mrsas_dev, "Cannot create PD INFO tag\n");
211179b4460bSKashyap D Desai return (ENOMEM);
211279b4460bSKashyap D Desai }
211379b4460bSKashyap D Desai if (bus_dmamem_alloc(sc->pd_info_tag, (void **)&sc->pd_info_mem,
211479b4460bSKashyap D Desai BUS_DMA_NOWAIT, &sc->pd_info_dmamap)) {
211579b4460bSKashyap D Desai device_printf(sc->mrsas_dev, "Cannot alloc PD INFO buffer memory\n");
211679b4460bSKashyap D Desai return (ENOMEM);
211779b4460bSKashyap D Desai }
211879b4460bSKashyap D Desai bzero(sc->pd_info_mem, pd_info_size);
211979b4460bSKashyap D Desai if (bus_dmamap_load(sc->pd_info_tag, sc->pd_info_dmamap,
212079b4460bSKashyap D Desai sc->pd_info_mem, pd_info_size, mrsas_addr_cb,
212179b4460bSKashyap D Desai &sc->pd_info_phys_addr, BUS_DMA_NOWAIT)) {
212279b4460bSKashyap D Desai device_printf(sc->mrsas_dev, "Cannot load PD INFO buffer memory\n");
212379b4460bSKashyap D Desai return (ENOMEM);
212479b4460bSKashyap D Desai }
212579b4460bSKashyap D Desai
2126665484d8SDoug Ambrisko /*
2127665484d8SDoug Ambrisko * Create a dma tag for data buffers; size will be the maximum
2128665484d8SDoug Ambrisko * possible I/O size (280kB).
2129665484d8SDoug Ambrisko */
21308e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
21318e727371SKashyap D Desai 1,
21328e727371SKashyap D Desai 0,
21338e727371SKashyap D Desai BUS_SPACE_MAXADDR,
21348e727371SKashyap D Desai BUS_SPACE_MAXADDR,
21358e727371SKashyap D Desai NULL, NULL,
2136cd853791SKonstantin Belousov maxphys,
21373a3fc6cbSKashyap D Desai sc->max_num_sge, /* nsegments */
2138cd853791SKonstantin Belousov maxphys,
21398e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
21408e727371SKashyap D Desai busdma_lock_mutex,
21418e727371SKashyap D Desai &sc->io_lock,
2142665484d8SDoug Ambrisko &sc->data_tag)) {
2143665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot create data dma tag\n");
2144665484d8SDoug Ambrisko return (ENOMEM);
2145665484d8SDoug Ambrisko }
2146665484d8SDoug Ambrisko return (0);
2147665484d8SDoug Ambrisko }
2148665484d8SDoug Ambrisko
2149665484d8SDoug Ambrisko /*
2150665484d8SDoug Ambrisko * mrsas_addr_cb: Callback function of bus_dmamap_load()
21518e727371SKashyap D Desai * input: callback argument, machine dependent type
21528e727371SKashyap D Desai * that describes DMA segments, number of segments, error code
2153665484d8SDoug Ambrisko *
21548e727371SKashyap D Desai * This function is for the driver to receive mapping information resultant of
21558e727371SKashyap D Desai * the bus_dmamap_load(). The information is actually not being used, but the
21568e727371SKashyap D Desai * address is saved anyway.
2157665484d8SDoug Ambrisko */
2158665484d8SDoug Ambrisko void
mrsas_addr_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)2159665484d8SDoug Ambrisko mrsas_addr_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
2160665484d8SDoug Ambrisko {
2161665484d8SDoug Ambrisko bus_addr_t *addr;
2162665484d8SDoug Ambrisko
2163665484d8SDoug Ambrisko addr = arg;
2164665484d8SDoug Ambrisko *addr = segs[0].ds_addr;
2165665484d8SDoug Ambrisko }
2166665484d8SDoug Ambrisko
2167665484d8SDoug Ambrisko /*
2168665484d8SDoug Ambrisko * mrsas_setup_raidmap: Set up RAID map.
2169665484d8SDoug Ambrisko * input: Adapter instance soft state
2170665484d8SDoug Ambrisko *
2171665484d8SDoug Ambrisko * Allocate DMA memory for the RAID maps and perform setup.
2172665484d8SDoug Ambrisko */
21738e727371SKashyap D Desai static int
mrsas_setup_raidmap(struct mrsas_softc * sc)21748e727371SKashyap D Desai mrsas_setup_raidmap(struct mrsas_softc *sc)
2175665484d8SDoug Ambrisko {
21764799d485SKashyap D Desai int i;
21774799d485SKashyap D Desai
21784799d485SKashyap D Desai for (i = 0; i < 2; i++) {
21794799d485SKashyap D Desai sc->ld_drv_map[i] =
21804799d485SKashyap D Desai (void *)malloc(sc->drv_map_sz, M_MRSAS, M_NOWAIT);
21814799d485SKashyap D Desai /* Do Error handling */
21824799d485SKashyap D Desai if (!sc->ld_drv_map[i]) {
21834799d485SKashyap D Desai device_printf(sc->mrsas_dev, "Could not allocate memory for local map");
21844799d485SKashyap D Desai
21854799d485SKashyap D Desai if (i == 1)
21864799d485SKashyap D Desai free(sc->ld_drv_map[0], M_MRSAS);
21878e727371SKashyap D Desai /* ABORT driver initialization */
21884799d485SKashyap D Desai goto ABORT;
21894799d485SKashyap D Desai }
21904799d485SKashyap D Desai }
21914799d485SKashyap D Desai
21928e727371SKashyap D Desai for (int i = 0; i < 2; i++) {
21938e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
21948e727371SKashyap D Desai 4, 0,
21958e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
21968e727371SKashyap D Desai BUS_SPACE_MAXADDR,
21978e727371SKashyap D Desai NULL, NULL,
21988e727371SKashyap D Desai sc->max_map_sz,
21998e727371SKashyap D Desai 1,
22008e727371SKashyap D Desai sc->max_map_sz,
22018e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
22028e727371SKashyap D Desai NULL, NULL,
2203665484d8SDoug Ambrisko &sc->raidmap_tag[i])) {
22044799d485SKashyap D Desai device_printf(sc->mrsas_dev,
22054799d485SKashyap D Desai "Cannot allocate raid map tag.\n");
2206665484d8SDoug Ambrisko return (ENOMEM);
2207665484d8SDoug Ambrisko }
22084799d485SKashyap D Desai if (bus_dmamem_alloc(sc->raidmap_tag[i],
22094799d485SKashyap D Desai (void **)&sc->raidmap_mem[i],
2210665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->raidmap_dmamap[i])) {
22114799d485SKashyap D Desai device_printf(sc->mrsas_dev,
22124799d485SKashyap D Desai "Cannot allocate raidmap memory.\n");
2213665484d8SDoug Ambrisko return (ENOMEM);
2214665484d8SDoug Ambrisko }
22154799d485SKashyap D Desai bzero(sc->raidmap_mem[i], sc->max_map_sz);
22164799d485SKashyap D Desai
2217665484d8SDoug Ambrisko if (bus_dmamap_load(sc->raidmap_tag[i], sc->raidmap_dmamap[i],
22184799d485SKashyap D Desai sc->raidmap_mem[i], sc->max_map_sz,
22194799d485SKashyap D Desai mrsas_addr_cb, &sc->raidmap_phys_addr[i],
2220665484d8SDoug Ambrisko BUS_DMA_NOWAIT)) {
2221665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load raidmap memory.\n");
2222665484d8SDoug Ambrisko return (ENOMEM);
2223665484d8SDoug Ambrisko }
2224665484d8SDoug Ambrisko if (!sc->raidmap_mem[i]) {
22254799d485SKashyap D Desai device_printf(sc->mrsas_dev,
22264799d485SKashyap D Desai "Cannot allocate memory for raid map.\n");
2227665484d8SDoug Ambrisko return (ENOMEM);
2228665484d8SDoug Ambrisko }
2229665484d8SDoug Ambrisko }
2230665484d8SDoug Ambrisko
2231665484d8SDoug Ambrisko if (!mrsas_get_map_info(sc))
2232665484d8SDoug Ambrisko mrsas_sync_map_info(sc);
2233665484d8SDoug Ambrisko
2234665484d8SDoug Ambrisko return (0);
22354799d485SKashyap D Desai
22364799d485SKashyap D Desai ABORT:
22374799d485SKashyap D Desai return (1);
2238665484d8SDoug Ambrisko }
2239665484d8SDoug Ambrisko
2240a688fcd0SKashyap D Desai /**
2241a688fcd0SKashyap D Desai * megasas_setup_jbod_map - setup jbod map for FP seq_number.
2242a688fcd0SKashyap D Desai * @sc: Adapter soft state
2243a688fcd0SKashyap D Desai *
2244a688fcd0SKashyap D Desai * Return 0 on success.
2245a688fcd0SKashyap D Desai */
2246a688fcd0SKashyap D Desai void
megasas_setup_jbod_map(struct mrsas_softc * sc)2247a688fcd0SKashyap D Desai megasas_setup_jbod_map(struct mrsas_softc *sc)
2248a688fcd0SKashyap D Desai {
2249a688fcd0SKashyap D Desai int i;
2250a688fcd0SKashyap D Desai uint32_t pd_seq_map_sz;
2251a688fcd0SKashyap D Desai
2252a688fcd0SKashyap D Desai pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
2253a688fcd0SKashyap D Desai (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
2254a688fcd0SKashyap D Desai
2255a688fcd0SKashyap D Desai if (!sc->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
2256a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 0;
2257a688fcd0SKashyap D Desai return;
2258a688fcd0SKashyap D Desai }
2259a688fcd0SKashyap D Desai if (sc->jbodmap_mem[0])
2260a688fcd0SKashyap D Desai goto skip_alloc;
2261a688fcd0SKashyap D Desai
2262a688fcd0SKashyap D Desai for (i = 0; i < 2; i++) {
2263a688fcd0SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
2264a688fcd0SKashyap D Desai 4, 0,
2265a688fcd0SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
2266a688fcd0SKashyap D Desai BUS_SPACE_MAXADDR,
2267a688fcd0SKashyap D Desai NULL, NULL,
2268a688fcd0SKashyap D Desai pd_seq_map_sz,
2269a688fcd0SKashyap D Desai 1,
2270a688fcd0SKashyap D Desai pd_seq_map_sz,
2271a688fcd0SKashyap D Desai BUS_DMA_ALLOCNOW,
2272a688fcd0SKashyap D Desai NULL, NULL,
2273a688fcd0SKashyap D Desai &sc->jbodmap_tag[i])) {
2274a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
2275a688fcd0SKashyap D Desai "Cannot allocate jbod map tag.\n");
2276a688fcd0SKashyap D Desai return;
2277a688fcd0SKashyap D Desai }
2278a688fcd0SKashyap D Desai if (bus_dmamem_alloc(sc->jbodmap_tag[i],
2279a688fcd0SKashyap D Desai (void **)&sc->jbodmap_mem[i],
2280a688fcd0SKashyap D Desai BUS_DMA_NOWAIT, &sc->jbodmap_dmamap[i])) {
2281a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
2282a688fcd0SKashyap D Desai "Cannot allocate jbod map memory.\n");
2283a688fcd0SKashyap D Desai return;
2284a688fcd0SKashyap D Desai }
2285a688fcd0SKashyap D Desai bzero(sc->jbodmap_mem[i], pd_seq_map_sz);
2286a688fcd0SKashyap D Desai
2287a688fcd0SKashyap D Desai if (bus_dmamap_load(sc->jbodmap_tag[i], sc->jbodmap_dmamap[i],
2288a688fcd0SKashyap D Desai sc->jbodmap_mem[i], pd_seq_map_sz,
2289a688fcd0SKashyap D Desai mrsas_addr_cb, &sc->jbodmap_phys_addr[i],
2290a688fcd0SKashyap D Desai BUS_DMA_NOWAIT)) {
2291a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "Cannot load jbod map memory.\n");
2292a688fcd0SKashyap D Desai return;
2293a688fcd0SKashyap D Desai }
2294a688fcd0SKashyap D Desai if (!sc->jbodmap_mem[i]) {
2295a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
2296a688fcd0SKashyap D Desai "Cannot allocate memory for jbod map.\n");
2297a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 0;
2298a688fcd0SKashyap D Desai return;
2299a688fcd0SKashyap D Desai }
2300a688fcd0SKashyap D Desai }
2301a688fcd0SKashyap D Desai
2302a688fcd0SKashyap D Desai skip_alloc:
2303a688fcd0SKashyap D Desai if (!megasas_sync_pd_seq_num(sc, false) &&
2304a688fcd0SKashyap D Desai !megasas_sync_pd_seq_num(sc, true))
2305a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 1;
2306a688fcd0SKashyap D Desai else
2307a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 0;
2308a688fcd0SKashyap D Desai
2309a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "Jbod map is supported\n");
2310a688fcd0SKashyap D Desai }
2311a688fcd0SKashyap D Desai
23128e727371SKashyap D Desai /*
2313665484d8SDoug Ambrisko * mrsas_init_fw: Initialize Firmware
2314665484d8SDoug Ambrisko * input: Adapter soft state
2315665484d8SDoug Ambrisko *
23168e727371SKashyap D Desai * Calls transition_to_ready() to make sure Firmware is in operational state and
23178e727371SKashyap D Desai * calls mrsas_init_adapter() to send IOC_INIT command to Firmware. It
23188e727371SKashyap D Desai * issues internal commands to get the controller info after the IOC_INIT
23198e727371SKashyap D Desai * command response is received by Firmware. Note: code relating to
23208e727371SKashyap D Desai * get_pdlist, get_ld_list and max_sectors are currently not being used, it
23218e727371SKashyap D Desai * is left here as placeholder.
2322665484d8SDoug Ambrisko */
23238e727371SKashyap D Desai static int
mrsas_init_fw(struct mrsas_softc * sc)23248e727371SKashyap D Desai mrsas_init_fw(struct mrsas_softc *sc)
2325665484d8SDoug Ambrisko {
2326d18d1b47SKashyap D Desai
2327d18d1b47SKashyap D Desai int ret, loop, ocr = 0;
2328665484d8SDoug Ambrisko u_int32_t max_sectors_1;
2329665484d8SDoug Ambrisko u_int32_t max_sectors_2;
2330665484d8SDoug Ambrisko u_int32_t tmp_sectors;
23313d273176SKashyap D Desai u_int32_t scratch_pad_2, scratch_pad_3, scratch_pad_4;
2332d18d1b47SKashyap D Desai int msix_enable = 0;
2333d18d1b47SKashyap D Desai int fw_msix_count = 0;
2334821df4b9SKashyap D Desai int i, j;
2335665484d8SDoug Ambrisko
2336665484d8SDoug Ambrisko /* Make sure Firmware is ready */
2337665484d8SDoug Ambrisko ret = mrsas_transition_to_ready(sc, ocr);
2338665484d8SDoug Ambrisko if (ret != SUCCESS) {
2339665484d8SDoug Ambrisko return (ret);
2340665484d8SDoug Ambrisko }
23412909aab4SKashyap D Desai if (sc->is_ventura || sc->is_aero) {
2342e315cf4dSKashyap D Desai scratch_pad_3 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_scratch_pad_3));
23434ad83576SKashyap D Desai #if VD_EXT_DEBUG
23444ad83576SKashyap D Desai device_printf(sc->mrsas_dev, "scratch_pad_3 0x%x\n", scratch_pad_3);
23454ad83576SKashyap D Desai #endif
23464ad83576SKashyap D Desai sc->maxRaidMapSize = ((scratch_pad_3 >>
23474ad83576SKashyap D Desai MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
23484ad83576SKashyap D Desai MR_MAX_RAID_MAP_SIZE_MASK);
23494ad83576SKashyap D Desai }
2350d18d1b47SKashyap D Desai /* MSI-x index 0- reply post host index register */
2351d18d1b47SKashyap D Desai sc->msix_reg_offset[0] = MPI2_REPLY_POST_HOST_INDEX_OFFSET;
2352d18d1b47SKashyap D Desai /* Check if MSI-X is supported while in ready state */
2353e315cf4dSKashyap D Desai msix_enable = (mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_scratch_pad)) & 0x4000000) >> 0x1a;
2354d18d1b47SKashyap D Desai
2355d18d1b47SKashyap D Desai if (msix_enable) {
2356e315cf4dSKashyap D Desai scratch_pad_2 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
2357d18d1b47SKashyap D Desai outbound_scratch_pad_2));
2358d18d1b47SKashyap D Desai
2359d18d1b47SKashyap D Desai /* Check max MSI-X vectors */
2360d18d1b47SKashyap D Desai if (sc->device_id == MRSAS_TBOLT) {
2361d18d1b47SKashyap D Desai sc->msix_vectors = (scratch_pad_2
2362d18d1b47SKashyap D Desai & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
2363d18d1b47SKashyap D Desai fw_msix_count = sc->msix_vectors;
2364d18d1b47SKashyap D Desai } else {
2365d18d1b47SKashyap D Desai /* Invader/Fury supports 96 MSI-X vectors */
2366d18d1b47SKashyap D Desai sc->msix_vectors = ((scratch_pad_2
2367d18d1b47SKashyap D Desai & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
2368d18d1b47SKashyap D Desai >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
2369d18d1b47SKashyap D Desai fw_msix_count = sc->msix_vectors;
2370d18d1b47SKashyap D Desai
23717aade8bfSKashyap D Desai if ((sc->mrsas_gen3_ctrl && (sc->msix_vectors > 8)) ||
23722909aab4SKashyap D Desai ((sc->is_ventura || sc->is_aero) && (sc->msix_vectors > 16)))
23737aade8bfSKashyap D Desai sc->msix_combined = true;
23747aade8bfSKashyap D Desai /*
23757aade8bfSKashyap D Desai * Save 1-15 reply post index
23767aade8bfSKashyap D Desai * address to local memory Index 0
23777aade8bfSKashyap D Desai * is already saved from reg offset
23787aade8bfSKashyap D Desai * MPI2_REPLY_POST_HOST_INDEX_OFFSET
23797aade8bfSKashyap D Desai */
2380d18d1b47SKashyap D Desai for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY;
2381d18d1b47SKashyap D Desai loop++) {
2382d18d1b47SKashyap D Desai sc->msix_reg_offset[loop] =
2383d18d1b47SKashyap D Desai MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2384d18d1b47SKashyap D Desai (loop * 0x10);
2385d18d1b47SKashyap D Desai }
2386d18d1b47SKashyap D Desai }
2387d18d1b47SKashyap D Desai
2388d18d1b47SKashyap D Desai /* Don't bother allocating more MSI-X vectors than cpus */
2389d18d1b47SKashyap D Desai sc->msix_vectors = min(sc->msix_vectors,
2390d18d1b47SKashyap D Desai mp_ncpus);
2391d18d1b47SKashyap D Desai
2392d18d1b47SKashyap D Desai /* Allocate MSI-x vectors */
2393d18d1b47SKashyap D Desai if (mrsas_allocate_msix(sc) == SUCCESS)
2394d18d1b47SKashyap D Desai sc->msix_enable = 1;
2395d18d1b47SKashyap D Desai else
2396d18d1b47SKashyap D Desai sc->msix_enable = 0;
2397d18d1b47SKashyap D Desai
2398d18d1b47SKashyap D Desai device_printf(sc->mrsas_dev, "FW supports <%d> MSIX vector,"
2399d18d1b47SKashyap D Desai "Online CPU %d Current MSIX <%d>\n",
2400d18d1b47SKashyap D Desai fw_msix_count, mp_ncpus, sc->msix_vectors);
2401d18d1b47SKashyap D Desai }
24027aade8bfSKashyap D Desai /*
24037aade8bfSKashyap D Desai * MSI-X host index 0 is common for all adapter.
24047aade8bfSKashyap D Desai * It is used for all MPT based Adapters.
24057aade8bfSKashyap D Desai */
24067aade8bfSKashyap D Desai if (sc->msix_combined) {
24077aade8bfSKashyap D Desai sc->msix_reg_offset[0] =
24087aade8bfSKashyap D Desai MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET;
24097aade8bfSKashyap D Desai }
2410665484d8SDoug Ambrisko if (mrsas_init_adapter(sc) != SUCCESS) {
2411665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Adapter initialize Fail.\n");
2412665484d8SDoug Ambrisko return (1);
2413665484d8SDoug Ambrisko }
241479b4460bSKashyap D Desai
24152909aab4SKashyap D Desai if (sc->is_ventura || sc->is_aero) {
2416e315cf4dSKashyap D Desai scratch_pad_4 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
24173d273176SKashyap D Desai outbound_scratch_pad_4));
24183d273176SKashyap D Desai if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >= MR_DEFAULT_NVME_PAGE_SHIFT)
24193d273176SKashyap D Desai sc->nvme_page_size = 1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK);
24203d273176SKashyap D Desai
24213d273176SKashyap D Desai device_printf(sc->mrsas_dev, "NVME page size\t: (%d)\n", sc->nvme_page_size);
24223d273176SKashyap D Desai }
24233d273176SKashyap D Desai
2424665484d8SDoug Ambrisko /* Allocate internal commands for pass-thru */
2425665484d8SDoug Ambrisko if (mrsas_alloc_mfi_cmds(sc) != SUCCESS) {
2426665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Allocate MFI cmd failed.\n");
2427665484d8SDoug Ambrisko return (1);
2428665484d8SDoug Ambrisko }
2429af51c29fSKashyap D Desai sc->ctrl_info = malloc(sizeof(struct mrsas_ctrl_info), M_MRSAS, M_NOWAIT);
2430af51c29fSKashyap D Desai if (!sc->ctrl_info) {
2431af51c29fSKashyap D Desai device_printf(sc->mrsas_dev, "Malloc for ctrl_info failed.\n");
2432af51c29fSKashyap D Desai return (1);
2433af51c29fSKashyap D Desai }
24344799d485SKashyap D Desai /*
24358e727371SKashyap D Desai * Get the controller info from FW, so that the MAX VD support
24368e727371SKashyap D Desai * availability can be decided.
24374799d485SKashyap D Desai */
2438af51c29fSKashyap D Desai if (mrsas_get_ctrl_info(sc)) {
24394799d485SKashyap D Desai device_printf(sc->mrsas_dev, "Unable to get FW ctrl_info.\n");
2440af51c29fSKashyap D Desai return (1);
24414799d485SKashyap D Desai }
244277cf7df8SKashyap D Desai sc->secure_jbod_support =
2443af51c29fSKashyap D Desai (u_int8_t)sc->ctrl_info->adapterOperations3.supportSecurityonJBOD;
244477cf7df8SKashyap D Desai
244577cf7df8SKashyap D Desai if (sc->secure_jbod_support)
244677cf7df8SKashyap D Desai device_printf(sc->mrsas_dev, "FW supports SED \n");
244777cf7df8SKashyap D Desai
2448a688fcd0SKashyap D Desai if (sc->use_seqnum_jbod_fp)
2449a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "FW supports JBOD Map \n");
2450a688fcd0SKashyap D Desai
2451c376f864SKashyap D Desai if (sc->support_morethan256jbod)
2452c376f864SKashyap D Desai device_printf(sc->mrsas_dev, "FW supports JBOD Map Ext \n");
2453c376f864SKashyap D Desai
2454665484d8SDoug Ambrisko if (mrsas_setup_raidmap(sc) != SUCCESS) {
2455a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "Error: RAID map setup FAILED !!! "
2456a688fcd0SKashyap D Desai "There seems to be some problem in the controller\n"
2457a688fcd0SKashyap D Desai "Please contact to the SUPPORT TEAM if the problem persists\n");
2458665484d8SDoug Ambrisko }
2459a688fcd0SKashyap D Desai megasas_setup_jbod_map(sc);
2460a688fcd0SKashyap D Desai
246179b4460bSKashyap D Desai memset(sc->target_list, 0,
246279b4460bSKashyap D Desai MRSAS_MAX_TM_TARGETS * sizeof(struct mrsas_target));
246379b4460bSKashyap D Desai for (i = 0; i < MRSAS_MAX_TM_TARGETS; i++)
246479b4460bSKashyap D Desai sc->target_list[i].target_id = 0xffff;
246579b4460bSKashyap D Desai
2466665484d8SDoug Ambrisko /* For pass-thru, get PD/LD list and controller info */
24674799d485SKashyap D Desai memset(sc->pd_list, 0,
24684799d485SKashyap D Desai MRSAS_MAX_PD * sizeof(struct mrsas_pd_list));
2469a688fcd0SKashyap D Desai if (mrsas_get_pd_list(sc) != SUCCESS) {
2470a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "Get PD list failed.\n");
2471a688fcd0SKashyap D Desai return (1);
2472a688fcd0SKashyap D Desai }
24734799d485SKashyap D Desai memset(sc->ld_ids, 0xff, MRSAS_MAX_LD_IDS);
2474a688fcd0SKashyap D Desai if (mrsas_get_ld_list(sc) != SUCCESS) {
2475a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev, "Get LD lsit failed.\n");
2476a688fcd0SKashyap D Desai return (1);
2477a688fcd0SKashyap D Desai }
2478821df4b9SKashyap D Desai
24792909aab4SKashyap D Desai if ((sc->is_ventura || sc->is_aero) && sc->drv_stream_detection) {
2480821df4b9SKashyap D Desai sc->streamDetectByLD = malloc(sizeof(PTR_LD_STREAM_DETECT) *
2481821df4b9SKashyap D Desai MAX_LOGICAL_DRIVES_EXT, M_MRSAS, M_NOWAIT);
2482821df4b9SKashyap D Desai if (!sc->streamDetectByLD) {
2483821df4b9SKashyap D Desai device_printf(sc->mrsas_dev,
2484821df4b9SKashyap D Desai "unable to allocate stream detection for pool of LDs\n");
2485821df4b9SKashyap D Desai return (1);
2486821df4b9SKashyap D Desai }
2487821df4b9SKashyap D Desai for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
2488821df4b9SKashyap D Desai sc->streamDetectByLD[i] = malloc(sizeof(LD_STREAM_DETECT), M_MRSAS, M_NOWAIT);
2489821df4b9SKashyap D Desai if (!sc->streamDetectByLD[i]) {
2490821df4b9SKashyap D Desai device_printf(sc->mrsas_dev, "unable to allocate stream detect by LD\n");
2491821df4b9SKashyap D Desai for (j = 0; j < i; ++j)
2492821df4b9SKashyap D Desai free(sc->streamDetectByLD[j], M_MRSAS);
2493821df4b9SKashyap D Desai free(sc->streamDetectByLD, M_MRSAS);
2494821df4b9SKashyap D Desai sc->streamDetectByLD = NULL;
2495821df4b9SKashyap D Desai return (1);
2496821df4b9SKashyap D Desai }
2497821df4b9SKashyap D Desai memset(sc->streamDetectByLD[i], 0, sizeof(LD_STREAM_DETECT));
2498821df4b9SKashyap D Desai sc->streamDetectByLD[i]->mruBitMap = MR_STREAM_BITMAP;
2499821df4b9SKashyap D Desai }
2500821df4b9SKashyap D Desai }
2501821df4b9SKashyap D Desai
2502665484d8SDoug Ambrisko /*
25038e727371SKashyap D Desai * Compute the max allowed sectors per IO: The controller info has
25048e727371SKashyap D Desai * two limits on max sectors. Driver should use the minimum of these
25058e727371SKashyap D Desai * two.
2506665484d8SDoug Ambrisko *
2507665484d8SDoug Ambrisko * 1 << stripe_sz_ops.min = max sectors per strip
2508665484d8SDoug Ambrisko *
25098e727371SKashyap D Desai * Note that older firmwares ( < FW ver 30) didn't report information to
25108e727371SKashyap D Desai * calculate max_sectors_1. So the number ended up as zero always.
2511665484d8SDoug Ambrisko */
2512665484d8SDoug Ambrisko tmp_sectors = 0;
2513af51c29fSKashyap D Desai max_sectors_1 = (1 << sc->ctrl_info->stripe_sz_ops.min) *
2514af51c29fSKashyap D Desai sc->ctrl_info->max_strips_per_io;
2515af51c29fSKashyap D Desai max_sectors_2 = sc->ctrl_info->max_request_size;
2516665484d8SDoug Ambrisko tmp_sectors = min(max_sectors_1, max_sectors_2);
2517fa3d57c2SAlexander Motin sc->max_sectors_per_req = (sc->max_num_sge - 1) * MRSAS_PAGE_SIZE / 512;
25184799d485SKashyap D Desai
25194799d485SKashyap D Desai if (tmp_sectors && (sc->max_sectors_per_req > tmp_sectors))
25204799d485SKashyap D Desai sc->max_sectors_per_req = tmp_sectors;
25214799d485SKashyap D Desai
2522665484d8SDoug Ambrisko sc->disableOnlineCtrlReset =
2523af51c29fSKashyap D Desai sc->ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
2524665484d8SDoug Ambrisko sc->UnevenSpanSupport =
2525af51c29fSKashyap D Desai sc->ctrl_info->adapterOperations2.supportUnevenSpans;
2526665484d8SDoug Ambrisko if (sc->UnevenSpanSupport) {
25278e727371SKashyap D Desai device_printf(sc->mrsas_dev, "FW supports: UnevenSpanSupport=%x\n\n",
2528665484d8SDoug Ambrisko sc->UnevenSpanSupport);
25294799d485SKashyap D Desai
2530665484d8SDoug Ambrisko if (MR_ValidateMapInfo(sc))
2531665484d8SDoug Ambrisko sc->fast_path_io = 1;
2532665484d8SDoug Ambrisko else
2533665484d8SDoug Ambrisko sc->fast_path_io = 0;
2534665484d8SDoug Ambrisko }
25355437c8b8SKashyap D Desai
25365437c8b8SKashyap D Desai device_printf(sc->mrsas_dev, "max_fw_cmds: %u max_scsi_cmds: %u\n",
25375437c8b8SKashyap D Desai sc->max_fw_cmds, sc->max_scsi_cmds);
2538665484d8SDoug Ambrisko return (0);
2539665484d8SDoug Ambrisko }
2540665484d8SDoug Ambrisko
25418e727371SKashyap D Desai /*
2542665484d8SDoug Ambrisko * mrsas_init_adapter: Initializes the adapter/controller
2543665484d8SDoug Ambrisko * input: Adapter soft state
2544665484d8SDoug Ambrisko *
2545665484d8SDoug Ambrisko * Prepares for the issuing of the IOC Init cmd to FW for initializing the
2546665484d8SDoug Ambrisko * ROC/controller. The FW register is read to determined the number of
2547665484d8SDoug Ambrisko * commands that is supported. All memory allocations for IO is based on
2548665484d8SDoug Ambrisko * max_cmd. Appropriate calculations are performed in this function.
2549665484d8SDoug Ambrisko */
25508e727371SKashyap D Desai int
mrsas_init_adapter(struct mrsas_softc * sc)25518e727371SKashyap D Desai mrsas_init_adapter(struct mrsas_softc *sc)
2552665484d8SDoug Ambrisko {
2553665484d8SDoug Ambrisko uint32_t status;
25542a1d3bcdSKashyap D Desai u_int32_t scratch_pad_2;
2555665484d8SDoug Ambrisko int ret;
2556d18d1b47SKashyap D Desai int i = 0;
2557665484d8SDoug Ambrisko
2558665484d8SDoug Ambrisko /* Read FW status register */
2559e315cf4dSKashyap D Desai status = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_scratch_pad));
2560665484d8SDoug Ambrisko
2561665484d8SDoug Ambrisko sc->max_fw_cmds = status & MRSAS_FWSTATE_MAXCMD_MASK;
2562665484d8SDoug Ambrisko
2563665484d8SDoug Ambrisko /* Decrement the max supported by 1, to correlate with FW */
2564665484d8SDoug Ambrisko sc->max_fw_cmds = sc->max_fw_cmds - 1;
256554f784f5SKashyap D Desai sc->max_scsi_cmds = sc->max_fw_cmds - MRSAS_MAX_MFI_CMDS;
2566665484d8SDoug Ambrisko
2567665484d8SDoug Ambrisko /* Determine allocation size of command frames */
25682a1d3bcdSKashyap D Desai sc->reply_q_depth = ((sc->max_fw_cmds + 1 + 15) / 16 * 16) * 2;
25692a1d3bcdSKashyap D Desai sc->request_alloc_sz = sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * sc->max_fw_cmds;
2570665484d8SDoug Ambrisko sc->reply_alloc_sz = sizeof(MPI2_REPLY_DESCRIPTORS_UNION) * (sc->reply_q_depth);
25712a1d3bcdSKashyap D Desai sc->io_frames_alloc_sz = MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
25722a1d3bcdSKashyap D Desai (MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * (sc->max_fw_cmds + 1));
2573e315cf4dSKashyap D Desai scratch_pad_2 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
25743a3fc6cbSKashyap D Desai outbound_scratch_pad_2));
2575e34a057cSAlfredo Dal'Ava Junior
2576e34a057cSAlfredo Dal'Ava Junior mrsas_dprint(sc, MRSAS_TRACE, "%s: sc->reply_q_depth 0x%x,"
2577e34a057cSAlfredo Dal'Ava Junior "sc->request_alloc_sz 0x%x, sc->reply_alloc_sz 0x%x,"
2578e34a057cSAlfredo Dal'Ava Junior "sc->io_frames_alloc_sz 0x%x\n", __func__,
2579e34a057cSAlfredo Dal'Ava Junior sc->reply_q_depth, sc->request_alloc_sz,
2580e34a057cSAlfredo Dal'Ava Junior sc->reply_alloc_sz, sc->io_frames_alloc_sz);
2581e34a057cSAlfredo Dal'Ava Junior
25823a3fc6cbSKashyap D Desai /*
25833a3fc6cbSKashyap D Desai * If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
25843a3fc6cbSKashyap D Desai * Firmware support extended IO chain frame which is 4 time more
25853a3fc6cbSKashyap D Desai * than legacy Firmware. Legacy Firmware - Frame size is (8 * 128) =
25863a3fc6cbSKashyap D Desai * 1K 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
25873a3fc6cbSKashyap D Desai */
25883a3fc6cbSKashyap D Desai if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
25893a3fc6cbSKashyap D Desai sc->max_chain_frame_sz =
25903a3fc6cbSKashyap D Desai ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >> 5)
25913a3fc6cbSKashyap D Desai * MEGASAS_1MB_IO;
25923a3fc6cbSKashyap D Desai else
25933a3fc6cbSKashyap D Desai sc->max_chain_frame_sz =
25943a3fc6cbSKashyap D Desai ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >> 5)
25953a3fc6cbSKashyap D Desai * MEGASAS_256K_IO;
25963a3fc6cbSKashyap D Desai
25972a1d3bcdSKashyap D Desai sc->chain_frames_alloc_sz = sc->max_chain_frame_sz * sc->max_fw_cmds;
2598665484d8SDoug Ambrisko sc->max_sge_in_main_msg = (MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
2599665484d8SDoug Ambrisko offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL)) / 16;
2600665484d8SDoug Ambrisko
26013a3fc6cbSKashyap D Desai sc->max_sge_in_chain = sc->max_chain_frame_sz / sizeof(MPI2_SGE_IO_UNION);
2602665484d8SDoug Ambrisko sc->max_num_sge = sc->max_sge_in_main_msg + sc->max_sge_in_chain - 2;
2603665484d8SDoug Ambrisko
26042a1d3bcdSKashyap D Desai mrsas_dprint(sc, MRSAS_INFO,
26052a1d3bcdSKashyap D Desai "max sge: 0x%x, max chain frame size: 0x%x, "
2606e34a057cSAlfredo Dal'Ava Junior "max fw cmd: 0x%x sc->chain_frames_alloc_sz: 0x%x\n",
2607e34a057cSAlfredo Dal'Ava Junior sc->max_num_sge,
2608e34a057cSAlfredo Dal'Ava Junior sc->max_chain_frame_sz, sc->max_fw_cmds,
2609e34a057cSAlfredo Dal'Ava Junior sc->chain_frames_alloc_sz);
26103a3fc6cbSKashyap D Desai
2611665484d8SDoug Ambrisko /* Used for pass thru MFI frame (DCMD) */
2612665484d8SDoug Ambrisko sc->chain_offset_mfi_pthru = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 16;
2613665484d8SDoug Ambrisko
2614665484d8SDoug Ambrisko sc->chain_offset_io_request = (MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
2615665484d8SDoug Ambrisko sizeof(MPI2_SGE_IO_UNION)) / 16;
2616665484d8SDoug Ambrisko
2617d18d1b47SKashyap D Desai int count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
26188e727371SKashyap D Desai
2619d18d1b47SKashyap D Desai for (i = 0; i < count; i++)
2620d18d1b47SKashyap D Desai sc->last_reply_idx[i] = 0;
2621665484d8SDoug Ambrisko
2622665484d8SDoug Ambrisko ret = mrsas_alloc_mem(sc);
2623665484d8SDoug Ambrisko if (ret != SUCCESS)
2624665484d8SDoug Ambrisko return (ret);
2625665484d8SDoug Ambrisko
2626665484d8SDoug Ambrisko ret = mrsas_alloc_mpt_cmds(sc);
2627665484d8SDoug Ambrisko if (ret != SUCCESS)
2628665484d8SDoug Ambrisko return (ret);
2629665484d8SDoug Ambrisko
2630665484d8SDoug Ambrisko ret = mrsas_ioc_init(sc);
2631665484d8SDoug Ambrisko if (ret != SUCCESS)
2632665484d8SDoug Ambrisko return (ret);
2633665484d8SDoug Ambrisko
2634665484d8SDoug Ambrisko return (0);
2635665484d8SDoug Ambrisko }
2636665484d8SDoug Ambrisko
26378e727371SKashyap D Desai /*
2638665484d8SDoug Ambrisko * mrsas_alloc_ioc_cmd: Allocates memory for IOC Init command
2639665484d8SDoug Ambrisko * input: Adapter soft state
2640665484d8SDoug Ambrisko *
2641665484d8SDoug Ambrisko * Allocates for the IOC Init cmd to FW to initialize the ROC/controller.
2642665484d8SDoug Ambrisko */
26438e727371SKashyap D Desai int
mrsas_alloc_ioc_cmd(struct mrsas_softc * sc)26448e727371SKashyap D Desai mrsas_alloc_ioc_cmd(struct mrsas_softc *sc)
2645665484d8SDoug Ambrisko {
2646665484d8SDoug Ambrisko int ioc_init_size;
2647665484d8SDoug Ambrisko
2648665484d8SDoug Ambrisko /* Allocate IOC INIT command */
2649665484d8SDoug Ambrisko ioc_init_size = 1024 + sizeof(MPI2_IOC_INIT_REQUEST);
26508e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
26518e727371SKashyap D Desai 1, 0,
26528e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
26538e727371SKashyap D Desai BUS_SPACE_MAXADDR,
26548e727371SKashyap D Desai NULL, NULL,
26558e727371SKashyap D Desai ioc_init_size,
26568e727371SKashyap D Desai 1,
26578e727371SKashyap D Desai ioc_init_size,
26588e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
26598e727371SKashyap D Desai NULL, NULL,
2660665484d8SDoug Ambrisko &sc->ioc_init_tag)) {
2661665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate ioc init tag\n");
2662665484d8SDoug Ambrisko return (ENOMEM);
2663665484d8SDoug Ambrisko }
2664665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->ioc_init_tag, (void **)&sc->ioc_init_mem,
2665665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->ioc_init_dmamap)) {
2666665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate ioc init cmd mem\n");
2667665484d8SDoug Ambrisko return (ENOMEM);
2668665484d8SDoug Ambrisko }
2669665484d8SDoug Ambrisko bzero(sc->ioc_init_mem, ioc_init_size);
2670665484d8SDoug Ambrisko if (bus_dmamap_load(sc->ioc_init_tag, sc->ioc_init_dmamap,
2671665484d8SDoug Ambrisko sc->ioc_init_mem, ioc_init_size, mrsas_addr_cb,
2672665484d8SDoug Ambrisko &sc->ioc_init_phys_mem, BUS_DMA_NOWAIT)) {
2673665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load ioc init cmd mem\n");
2674665484d8SDoug Ambrisko return (ENOMEM);
2675665484d8SDoug Ambrisko }
2676665484d8SDoug Ambrisko return (0);
2677665484d8SDoug Ambrisko }
2678665484d8SDoug Ambrisko
26798e727371SKashyap D Desai /*
2680665484d8SDoug Ambrisko * mrsas_free_ioc_cmd: Allocates memory for IOC Init command
2681665484d8SDoug Ambrisko * input: Adapter soft state
2682665484d8SDoug Ambrisko *
2683665484d8SDoug Ambrisko * Deallocates memory of the IOC Init cmd.
2684665484d8SDoug Ambrisko */
26858e727371SKashyap D Desai void
mrsas_free_ioc_cmd(struct mrsas_softc * sc)26868e727371SKashyap D Desai mrsas_free_ioc_cmd(struct mrsas_softc *sc)
2687665484d8SDoug Ambrisko {
2688665484d8SDoug Ambrisko if (sc->ioc_init_phys_mem)
2689665484d8SDoug Ambrisko bus_dmamap_unload(sc->ioc_init_tag, sc->ioc_init_dmamap);
2690665484d8SDoug Ambrisko if (sc->ioc_init_mem != NULL)
2691665484d8SDoug Ambrisko bus_dmamem_free(sc->ioc_init_tag, sc->ioc_init_mem, sc->ioc_init_dmamap);
2692665484d8SDoug Ambrisko if (sc->ioc_init_tag != NULL)
2693665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->ioc_init_tag);
2694665484d8SDoug Ambrisko }
2695665484d8SDoug Ambrisko
26968e727371SKashyap D Desai /*
2697665484d8SDoug Ambrisko * mrsas_ioc_init: Sends IOC Init command to FW
2698665484d8SDoug Ambrisko * input: Adapter soft state
2699665484d8SDoug Ambrisko *
2700665484d8SDoug Ambrisko * Issues the IOC Init cmd to FW to initialize the ROC/controller.
2701665484d8SDoug Ambrisko */
27028e727371SKashyap D Desai int
mrsas_ioc_init(struct mrsas_softc * sc)27038e727371SKashyap D Desai mrsas_ioc_init(struct mrsas_softc *sc)
2704665484d8SDoug Ambrisko {
2705665484d8SDoug Ambrisko struct mrsas_init_frame *init_frame;
2706665484d8SDoug Ambrisko pMpi2IOCInitRequest_t IOCInitMsg;
2707665484d8SDoug Ambrisko MRSAS_REQUEST_DESCRIPTOR_UNION req_desc;
2708e80341d5SKashyap D Desai u_int8_t max_wait = MRSAS_INTERNAL_CMD_WAIT_TIME;
2709665484d8SDoug Ambrisko bus_addr_t phys_addr;
2710665484d8SDoug Ambrisko int i, retcode = 0;
2711d993dd83SKashyap D Desai u_int32_t scratch_pad_2;
2712665484d8SDoug Ambrisko
2713665484d8SDoug Ambrisko /* Allocate memory for the IOC INIT command */
2714665484d8SDoug Ambrisko if (mrsas_alloc_ioc_cmd(sc)) {
2715665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate IOC command.\n");
2716665484d8SDoug Ambrisko return (1);
2717665484d8SDoug Ambrisko }
2718d993dd83SKashyap D Desai
2719d993dd83SKashyap D Desai if (!sc->block_sync_cache) {
2720e315cf4dSKashyap D Desai scratch_pad_2 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
2721d993dd83SKashyap D Desai outbound_scratch_pad_2));
2722d993dd83SKashyap D Desai sc->fw_sync_cache_support = (scratch_pad_2 &
2723d993dd83SKashyap D Desai MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
2724d993dd83SKashyap D Desai }
2725d993dd83SKashyap D Desai
2726665484d8SDoug Ambrisko IOCInitMsg = (pMpi2IOCInitRequest_t)(((char *)sc->ioc_init_mem) + 1024);
2727665484d8SDoug Ambrisko IOCInitMsg->Function = MPI2_FUNCTION_IOC_INIT;
2728665484d8SDoug Ambrisko IOCInitMsg->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
2729e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->MsgVersion = htole16(MPI2_VERSION);
2730e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->HeaderVersion = htole16(MPI2_HEADER_VERSION);
2731e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->SystemRequestFrameSize = htole16(MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
2732e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->ReplyDescriptorPostQueueDepth = htole16(sc->reply_q_depth);
2733e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->ReplyDescriptorPostQueueAddress = htole64(sc->reply_desc_phys_addr);
2734e34a057cSAlfredo Dal'Ava Junior IOCInitMsg->SystemRequestFrameBaseAddress = htole64(sc->io_request_phys_addr);
2735d18d1b47SKashyap D Desai IOCInitMsg->HostMSIxVectors = (sc->msix_vectors > 0 ? sc->msix_vectors : 0);
27363d273176SKashyap D Desai IOCInitMsg->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
2737665484d8SDoug Ambrisko
2738665484d8SDoug Ambrisko init_frame = (struct mrsas_init_frame *)sc->ioc_init_mem;
2739665484d8SDoug Ambrisko init_frame->cmd = MFI_CMD_INIT;
2740665484d8SDoug Ambrisko init_frame->cmd_status = 0xFF;
2741e34a057cSAlfredo Dal'Ava Junior init_frame->flags |= htole16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
2742665484d8SDoug Ambrisko
2743d18d1b47SKashyap D Desai /* driver support Extended MSIX */
27442909aab4SKashyap D Desai if (sc->mrsas_gen3_ctrl || sc->is_ventura || sc->is_aero) {
2745d18d1b47SKashyap D Desai init_frame->driver_operations.
2746d18d1b47SKashyap D Desai mfi_capabilities.support_additional_msix = 1;
2747d18d1b47SKashyap D Desai }
2748665484d8SDoug Ambrisko if (sc->verbuf_mem) {
2749665484d8SDoug Ambrisko snprintf((char *)sc->verbuf_mem, strlen(MRSAS_VERSION) + 2, "%s\n",
2750665484d8SDoug Ambrisko MRSAS_VERSION);
2751665484d8SDoug Ambrisko init_frame->driver_ver_lo = (bus_addr_t)sc->verbuf_phys_addr;
2752665484d8SDoug Ambrisko init_frame->driver_ver_hi = 0;
2753665484d8SDoug Ambrisko }
275416dc2814SKashyap D Desai init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb = 1;
27554799d485SKashyap D Desai init_frame->driver_operations.mfi_capabilities.support_max_255lds = 1;
275677cf7df8SKashyap D Desai init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw = 1;
27573a3fc6cbSKashyap D Desai if (sc->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
27583a3fc6cbSKashyap D Desai init_frame->driver_operations.mfi_capabilities.support_ext_io_size = 1;
2759665484d8SDoug Ambrisko
2760e34a057cSAlfredo Dal'Ava Junior init_frame->driver_operations.reg = htole32(init_frame->driver_operations.reg);
2761e34a057cSAlfredo Dal'Ava Junior
2762e34a057cSAlfredo Dal'Ava Junior phys_addr = (bus_addr_t)sc->ioc_init_phys_mem + 1024;
2763e34a057cSAlfredo Dal'Ava Junior init_frame->queue_info_new_phys_addr_lo = htole32(phys_addr);
2764e34a057cSAlfredo Dal'Ava Junior init_frame->data_xfer_len = htole32(sizeof(Mpi2IOCInitRequest_t));
2765e34a057cSAlfredo Dal'Ava Junior
276659fffbcfSAlfredo Dal'Ava Junior req_desc.addr.Words = htole64((bus_addr_t)sc->ioc_init_phys_mem);
2767665484d8SDoug Ambrisko req_desc.MFAIo.RequestFlags =
2768665484d8SDoug Ambrisko (MRSAS_REQ_DESCRIPT_FLAGS_MFA << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2769665484d8SDoug Ambrisko
2770665484d8SDoug Ambrisko mrsas_disable_intr(sc);
2771665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "Issuing IOC INIT command to FW.\n");
2772b518670cSKashyap D Desai mrsas_write_64bit_req_desc(sc, req_desc.addr.u.low, req_desc.addr.u.high);
2773665484d8SDoug Ambrisko
2774665484d8SDoug Ambrisko /*
2775665484d8SDoug Ambrisko * Poll response timer to wait for Firmware response. While this
2776665484d8SDoug Ambrisko * timer with the DELAY call could block CPU, the time interval for
2777665484d8SDoug Ambrisko * this is only 1 millisecond.
2778665484d8SDoug Ambrisko */
2779665484d8SDoug Ambrisko if (init_frame->cmd_status == 0xFF) {
2780665484d8SDoug Ambrisko for (i = 0; i < (max_wait * 1000); i++) {
2781665484d8SDoug Ambrisko if (init_frame->cmd_status == 0xFF)
2782665484d8SDoug Ambrisko DELAY(1000);
2783665484d8SDoug Ambrisko else
2784665484d8SDoug Ambrisko break;
2785665484d8SDoug Ambrisko }
2786665484d8SDoug Ambrisko }
2787665484d8SDoug Ambrisko if (init_frame->cmd_status == 0)
2788665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
2789665484d8SDoug Ambrisko "IOC INIT response received from FW.\n");
27908e727371SKashyap D Desai else {
2791665484d8SDoug Ambrisko if (init_frame->cmd_status == 0xFF)
2792665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "IOC Init timed out after %d seconds.\n", max_wait);
2793665484d8SDoug Ambrisko else
2794665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "IOC Init failed, status = 0x%x\n", init_frame->cmd_status);
2795665484d8SDoug Ambrisko retcode = 1;
2796665484d8SDoug Ambrisko }
2797665484d8SDoug Ambrisko
2798b518670cSKashyap D Desai if (sc->is_aero) {
2799e315cf4dSKashyap D Desai scratch_pad_2 = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
2800b518670cSKashyap D Desai outbound_scratch_pad_2));
2801b518670cSKashyap D Desai sc->atomic_desc_support = (scratch_pad_2 &
2802b518670cSKashyap D Desai MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
2803b518670cSKashyap D Desai device_printf(sc->mrsas_dev, "FW supports atomic descriptor: %s\n",
2804b518670cSKashyap D Desai sc->atomic_desc_support ? "Yes" : "No");
2805b518670cSKashyap D Desai }
2806b518670cSKashyap D Desai
2807665484d8SDoug Ambrisko mrsas_free_ioc_cmd(sc);
2808665484d8SDoug Ambrisko return (retcode);
2809665484d8SDoug Ambrisko }
2810665484d8SDoug Ambrisko
28118e727371SKashyap D Desai /*
2812665484d8SDoug Ambrisko * mrsas_alloc_mpt_cmds: Allocates the command packets
2813665484d8SDoug Ambrisko * input: Adapter instance soft state
2814665484d8SDoug Ambrisko *
2815665484d8SDoug Ambrisko * This function allocates the internal commands for IOs. Each command that is
28168e727371SKashyap D Desai * issued to FW is wrapped in a local data structure called mrsas_mpt_cmd. An
28178e727371SKashyap D Desai * array is allocated with mrsas_mpt_cmd context. The free commands are
2818665484d8SDoug Ambrisko * maintained in a linked list (cmd pool). SMID value range is from 1 to
2819665484d8SDoug Ambrisko * max_fw_cmds.
2820665484d8SDoug Ambrisko */
28218e727371SKashyap D Desai int
mrsas_alloc_mpt_cmds(struct mrsas_softc * sc)28228e727371SKashyap D Desai mrsas_alloc_mpt_cmds(struct mrsas_softc *sc)
2823665484d8SDoug Ambrisko {
2824665484d8SDoug Ambrisko int i, j;
28252a1d3bcdSKashyap D Desai u_int32_t max_fw_cmds, count;
2826665484d8SDoug Ambrisko struct mrsas_mpt_cmd *cmd;
2827665484d8SDoug Ambrisko pMpi2ReplyDescriptorsUnion_t reply_desc;
2828665484d8SDoug Ambrisko u_int32_t offset, chain_offset, sense_offset;
2829665484d8SDoug Ambrisko bus_addr_t io_req_base_phys, chain_frame_base_phys, sense_base_phys;
2830665484d8SDoug Ambrisko u_int8_t *io_req_base, *chain_frame_base, *sense_base;
2831665484d8SDoug Ambrisko
28322a1d3bcdSKashyap D Desai max_fw_cmds = sc->max_fw_cmds;
2833665484d8SDoug Ambrisko
2834665484d8SDoug Ambrisko sc->req_desc = malloc(sc->request_alloc_sz, M_MRSAS, M_NOWAIT);
2835665484d8SDoug Ambrisko if (!sc->req_desc) {
2836665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Out of memory, cannot alloc req desc\n");
2837665484d8SDoug Ambrisko return (ENOMEM);
2838665484d8SDoug Ambrisko }
2839665484d8SDoug Ambrisko memset(sc->req_desc, 0, sc->request_alloc_sz);
2840665484d8SDoug Ambrisko
2841665484d8SDoug Ambrisko /*
28428e727371SKashyap D Desai * sc->mpt_cmd_list is an array of struct mrsas_mpt_cmd pointers.
28438e727371SKashyap D Desai * Allocate the dynamic array first and then allocate individual
28448e727371SKashyap D Desai * commands.
2845665484d8SDoug Ambrisko */
28462a1d3bcdSKashyap D Desai sc->mpt_cmd_list = malloc(sizeof(struct mrsas_mpt_cmd *) * max_fw_cmds,
28472a1d3bcdSKashyap D Desai M_MRSAS, M_NOWAIT);
2848665484d8SDoug Ambrisko if (!sc->mpt_cmd_list) {
2849665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc memory for mpt_cmd_list.\n");
2850665484d8SDoug Ambrisko return (ENOMEM);
2851665484d8SDoug Ambrisko }
28522a1d3bcdSKashyap D Desai memset(sc->mpt_cmd_list, 0, sizeof(struct mrsas_mpt_cmd *) * max_fw_cmds);
28532a1d3bcdSKashyap D Desai for (i = 0; i < max_fw_cmds; i++) {
2854665484d8SDoug Ambrisko sc->mpt_cmd_list[i] = malloc(sizeof(struct mrsas_mpt_cmd),
2855665484d8SDoug Ambrisko M_MRSAS, M_NOWAIT);
2856665484d8SDoug Ambrisko if (!sc->mpt_cmd_list[i]) {
2857665484d8SDoug Ambrisko for (j = 0; j < i; j++)
2858665484d8SDoug Ambrisko free(sc->mpt_cmd_list[j], M_MRSAS);
2859665484d8SDoug Ambrisko free(sc->mpt_cmd_list, M_MRSAS);
2860665484d8SDoug Ambrisko sc->mpt_cmd_list = NULL;
2861665484d8SDoug Ambrisko return (ENOMEM);
2862665484d8SDoug Ambrisko }
2863665484d8SDoug Ambrisko }
2864665484d8SDoug Ambrisko
2865665484d8SDoug Ambrisko io_req_base = (u_int8_t *)sc->io_request_mem + MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
2866665484d8SDoug Ambrisko io_req_base_phys = (bus_addr_t)sc->io_request_phys_addr + MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
2867665484d8SDoug Ambrisko chain_frame_base = (u_int8_t *)sc->chain_frame_mem;
2868665484d8SDoug Ambrisko chain_frame_base_phys = (bus_addr_t)sc->chain_frame_phys_addr;
2869665484d8SDoug Ambrisko sense_base = (u_int8_t *)sc->sense_mem;
2870665484d8SDoug Ambrisko sense_base_phys = (bus_addr_t)sc->sense_phys_addr;
28712a1d3bcdSKashyap D Desai for (i = 0; i < max_fw_cmds; i++) {
2872665484d8SDoug Ambrisko cmd = sc->mpt_cmd_list[i];
2873665484d8SDoug Ambrisko offset = MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
28743a3fc6cbSKashyap D Desai chain_offset = sc->max_chain_frame_sz * i;
2875665484d8SDoug Ambrisko sense_offset = MRSAS_SENSE_LEN * i;
2876665484d8SDoug Ambrisko memset(cmd, 0, sizeof(struct mrsas_mpt_cmd));
2877665484d8SDoug Ambrisko cmd->index = i + 1;
2878665484d8SDoug Ambrisko cmd->ccb_ptr = NULL;
28792a1d3bcdSKashyap D Desai cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
28808bb601acSKashyap D Desai callout_init_mtx(&cmd->cm_callout, &sc->sim_lock, 0);
2881665484d8SDoug Ambrisko cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
2882665484d8SDoug Ambrisko cmd->sc = sc;
2883665484d8SDoug Ambrisko cmd->io_request = (MRSAS_RAID_SCSI_IO_REQUEST *) (io_req_base + offset);
2884665484d8SDoug Ambrisko memset(cmd->io_request, 0, sizeof(MRSAS_RAID_SCSI_IO_REQUEST));
2885665484d8SDoug Ambrisko cmd->io_request_phys_addr = io_req_base_phys + offset;
2886665484d8SDoug Ambrisko cmd->chain_frame = (MPI2_SGE_IO_UNION *) (chain_frame_base + chain_offset);
2887665484d8SDoug Ambrisko cmd->chain_frame_phys_addr = chain_frame_base_phys + chain_offset;
2888665484d8SDoug Ambrisko cmd->sense = sense_base + sense_offset;
2889665484d8SDoug Ambrisko cmd->sense_phys_addr = sense_base_phys + sense_offset;
2890665484d8SDoug Ambrisko if (bus_dmamap_create(sc->data_tag, 0, &cmd->data_dmamap)) {
2891665484d8SDoug Ambrisko return (FAIL);
2892665484d8SDoug Ambrisko }
2893665484d8SDoug Ambrisko TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
2894665484d8SDoug Ambrisko }
2895665484d8SDoug Ambrisko
2896665484d8SDoug Ambrisko /* Initialize reply descriptor array to 0xFFFFFFFF */
2897665484d8SDoug Ambrisko reply_desc = sc->reply_desc_mem;
2898d18d1b47SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
2899d18d1b47SKashyap D Desai for (i = 0; i < sc->reply_q_depth * count; i++, reply_desc++) {
2900665484d8SDoug Ambrisko reply_desc->Words = MRSAS_ULONG_MAX;
2901665484d8SDoug Ambrisko }
2902665484d8SDoug Ambrisko return (0);
2903665484d8SDoug Ambrisko }
2904665484d8SDoug Ambrisko
29058e727371SKashyap D Desai /*
2906b518670cSKashyap D Desai * mrsas_write_64bit_req_dsc: Writes 64 bit request descriptor to FW
2907b518670cSKashyap D Desai * input: Adapter softstate
2908b518670cSKashyap D Desai * request descriptor address low
2909b518670cSKashyap D Desai * request descriptor address high
2910b518670cSKashyap D Desai */
2911b518670cSKashyap D Desai void
mrsas_write_64bit_req_desc(struct mrsas_softc * sc,u_int32_t req_desc_lo,u_int32_t req_desc_hi)2912b518670cSKashyap D Desai mrsas_write_64bit_req_desc(struct mrsas_softc *sc, u_int32_t req_desc_lo,
2913b518670cSKashyap D Desai u_int32_t req_desc_hi)
2914b518670cSKashyap D Desai {
2915b518670cSKashyap D Desai mtx_lock(&sc->pci_lock);
2916b518670cSKashyap D Desai mrsas_write_reg(sc, offsetof(mrsas_reg_set, inbound_low_queue_port),
2917e34a057cSAlfredo Dal'Ava Junior le32toh(req_desc_lo));
2918b518670cSKashyap D Desai mrsas_write_reg(sc, offsetof(mrsas_reg_set, inbound_high_queue_port),
2919e34a057cSAlfredo Dal'Ava Junior le32toh(req_desc_hi));
2920b518670cSKashyap D Desai mtx_unlock(&sc->pci_lock);
2921b518670cSKashyap D Desai }
2922b518670cSKashyap D Desai
2923b518670cSKashyap D Desai /*
2924665484d8SDoug Ambrisko * mrsas_fire_cmd: Sends command to FW
2925665484d8SDoug Ambrisko * input: Adapter softstate
2926665484d8SDoug Ambrisko * request descriptor address low
2927665484d8SDoug Ambrisko * request descriptor address high
2928665484d8SDoug Ambrisko *
2929665484d8SDoug Ambrisko * This functions fires the command to Firmware by writing to the
2930665484d8SDoug Ambrisko * inbound_low_queue_port and inbound_high_queue_port.
2931665484d8SDoug Ambrisko */
29328e727371SKashyap D Desai void
mrsas_fire_cmd(struct mrsas_softc * sc,u_int32_t req_desc_lo,u_int32_t req_desc_hi)29338e727371SKashyap D Desai mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
2934665484d8SDoug Ambrisko u_int32_t req_desc_hi)
2935665484d8SDoug Ambrisko {
2936b518670cSKashyap D Desai if (sc->atomic_desc_support)
2937b518670cSKashyap D Desai mrsas_write_reg(sc, offsetof(mrsas_reg_set, inbound_single_queue_port),
2938e34a057cSAlfredo Dal'Ava Junior le32toh(req_desc_lo));
2939b518670cSKashyap D Desai else
2940b518670cSKashyap D Desai mrsas_write_64bit_req_desc(sc, req_desc_lo, req_desc_hi);
2941665484d8SDoug Ambrisko }
2942665484d8SDoug Ambrisko
29438e727371SKashyap D Desai /*
29448e727371SKashyap D Desai * mrsas_transition_to_ready: Move FW to Ready state input:
29458e727371SKashyap D Desai * Adapter instance soft state
2946665484d8SDoug Ambrisko *
29478e727371SKashyap D Desai * During the initialization, FW passes can potentially be in any one of several
29488e727371SKashyap D Desai * possible states. If the FW in operational, waiting-for-handshake states,
29498e727371SKashyap D Desai * driver must take steps to bring it to ready state. Otherwise, it has to
29508e727371SKashyap D Desai * wait for the ready state.
2951665484d8SDoug Ambrisko */
29528e727371SKashyap D Desai int
mrsas_transition_to_ready(struct mrsas_softc * sc,int ocr)29538e727371SKashyap D Desai mrsas_transition_to_ready(struct mrsas_softc *sc, int ocr)
2954665484d8SDoug Ambrisko {
2955665484d8SDoug Ambrisko int i;
2956665484d8SDoug Ambrisko u_int8_t max_wait;
2957665484d8SDoug Ambrisko u_int32_t val, fw_state;
295898470f0eSScott Long u_int32_t cur_state __unused;
2959665484d8SDoug Ambrisko u_int32_t abs_state, curr_abs_state;
2960665484d8SDoug Ambrisko
2961e315cf4dSKashyap D Desai val = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_scratch_pad));
2962665484d8SDoug Ambrisko fw_state = val & MFI_STATE_MASK;
2963665484d8SDoug Ambrisko max_wait = MRSAS_RESET_WAIT_TIME;
2964665484d8SDoug Ambrisko
2965665484d8SDoug Ambrisko if (fw_state != MFI_STATE_READY)
2966665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Waiting for FW to come to ready state\n");
2967665484d8SDoug Ambrisko
2968665484d8SDoug Ambrisko while (fw_state != MFI_STATE_READY) {
2969e315cf4dSKashyap D Desai abs_state = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, outbound_scratch_pad));
2970665484d8SDoug Ambrisko switch (fw_state) {
2971665484d8SDoug Ambrisko case MFI_STATE_FAULT:
2972665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "FW is in FAULT state!!\n");
2973665484d8SDoug Ambrisko if (ocr) {
2974665484d8SDoug Ambrisko cur_state = MFI_STATE_FAULT;
2975665484d8SDoug Ambrisko break;
29768e727371SKashyap D Desai } else
2977665484d8SDoug Ambrisko return -ENODEV;
2978665484d8SDoug Ambrisko case MFI_STATE_WAIT_HANDSHAKE:
2979665484d8SDoug Ambrisko /* Set the CLR bit in inbound doorbell */
2980665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, doorbell),
2981665484d8SDoug Ambrisko MFI_INIT_CLEAR_HANDSHAKE | MFI_INIT_HOTPLUG);
2982665484d8SDoug Ambrisko cur_state = MFI_STATE_WAIT_HANDSHAKE;
2983665484d8SDoug Ambrisko break;
2984665484d8SDoug Ambrisko case MFI_STATE_BOOT_MESSAGE_PENDING:
2985665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, doorbell),
2986665484d8SDoug Ambrisko MFI_INIT_HOTPLUG);
2987665484d8SDoug Ambrisko cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2988665484d8SDoug Ambrisko break;
2989665484d8SDoug Ambrisko case MFI_STATE_OPERATIONAL:
29908e727371SKashyap D Desai /*
29918e727371SKashyap D Desai * Bring it to READY state; assuming max wait 10
29928e727371SKashyap D Desai * secs
29938e727371SKashyap D Desai */
2994665484d8SDoug Ambrisko mrsas_disable_intr(sc);
2995665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, doorbell), MFI_RESET_FLAGS);
2996665484d8SDoug Ambrisko for (i = 0; i < max_wait * 1000; i++) {
2997e315cf4dSKashyap D Desai if (mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set, doorbell)) & 1)
2998665484d8SDoug Ambrisko DELAY(1000);
2999665484d8SDoug Ambrisko else
3000665484d8SDoug Ambrisko break;
3001665484d8SDoug Ambrisko }
3002665484d8SDoug Ambrisko cur_state = MFI_STATE_OPERATIONAL;
3003665484d8SDoug Ambrisko break;
3004665484d8SDoug Ambrisko case MFI_STATE_UNDEFINED:
30058e727371SKashyap D Desai /*
30068e727371SKashyap D Desai * This state should not last for more than 2
30078e727371SKashyap D Desai * seconds
30088e727371SKashyap D Desai */
3009665484d8SDoug Ambrisko cur_state = MFI_STATE_UNDEFINED;
3010665484d8SDoug Ambrisko break;
3011665484d8SDoug Ambrisko case MFI_STATE_BB_INIT:
3012665484d8SDoug Ambrisko cur_state = MFI_STATE_BB_INIT;
3013665484d8SDoug Ambrisko break;
3014665484d8SDoug Ambrisko case MFI_STATE_FW_INIT:
3015665484d8SDoug Ambrisko cur_state = MFI_STATE_FW_INIT;
3016665484d8SDoug Ambrisko break;
3017665484d8SDoug Ambrisko case MFI_STATE_FW_INIT_2:
3018665484d8SDoug Ambrisko cur_state = MFI_STATE_FW_INIT_2;
3019665484d8SDoug Ambrisko break;
3020665484d8SDoug Ambrisko case MFI_STATE_DEVICE_SCAN:
3021665484d8SDoug Ambrisko cur_state = MFI_STATE_DEVICE_SCAN;
3022665484d8SDoug Ambrisko break;
3023665484d8SDoug Ambrisko case MFI_STATE_FLUSH_CACHE:
3024665484d8SDoug Ambrisko cur_state = MFI_STATE_FLUSH_CACHE;
3025665484d8SDoug Ambrisko break;
3026665484d8SDoug Ambrisko default:
3027665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Unknown state 0x%x\n", fw_state);
3028665484d8SDoug Ambrisko return -ENODEV;
3029665484d8SDoug Ambrisko }
3030665484d8SDoug Ambrisko
3031665484d8SDoug Ambrisko /*
3032665484d8SDoug Ambrisko * The cur_state should not last for more than max_wait secs
3033665484d8SDoug Ambrisko */
3034665484d8SDoug Ambrisko for (i = 0; i < (max_wait * 1000); i++) {
3035e315cf4dSKashyap D Desai fw_state = (mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3036665484d8SDoug Ambrisko outbound_scratch_pad)) & MFI_STATE_MASK);
3037e315cf4dSKashyap D Desai curr_abs_state = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3038665484d8SDoug Ambrisko outbound_scratch_pad));
3039665484d8SDoug Ambrisko if (abs_state == curr_abs_state)
3040665484d8SDoug Ambrisko DELAY(1000);
3041665484d8SDoug Ambrisko else
3042665484d8SDoug Ambrisko break;
3043665484d8SDoug Ambrisko }
3044665484d8SDoug Ambrisko
3045665484d8SDoug Ambrisko /*
3046665484d8SDoug Ambrisko * Return error if fw_state hasn't changed after max_wait
3047665484d8SDoug Ambrisko */
3048665484d8SDoug Ambrisko if (curr_abs_state == abs_state) {
3049665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "FW state [%d] hasn't changed "
3050665484d8SDoug Ambrisko "in %d secs\n", fw_state, max_wait);
3051665484d8SDoug Ambrisko return -ENODEV;
3052665484d8SDoug Ambrisko }
3053665484d8SDoug Ambrisko }
3054665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "FW now in Ready state\n");
3055665484d8SDoug Ambrisko return 0;
3056665484d8SDoug Ambrisko }
3057665484d8SDoug Ambrisko
30588e727371SKashyap D Desai /*
3059665484d8SDoug Ambrisko * mrsas_get_mfi_cmd: Get a cmd from free command pool
3060665484d8SDoug Ambrisko * input: Adapter soft state
3061665484d8SDoug Ambrisko *
3062665484d8SDoug Ambrisko * This function removes an MFI command from the command list.
3063665484d8SDoug Ambrisko */
30648e727371SKashyap D Desai struct mrsas_mfi_cmd *
mrsas_get_mfi_cmd(struct mrsas_softc * sc)30658e727371SKashyap D Desai mrsas_get_mfi_cmd(struct mrsas_softc *sc)
3066665484d8SDoug Ambrisko {
3067665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd = NULL;
3068665484d8SDoug Ambrisko
3069665484d8SDoug Ambrisko mtx_lock(&sc->mfi_cmd_pool_lock);
3070665484d8SDoug Ambrisko if (!TAILQ_EMPTY(&sc->mrsas_mfi_cmd_list_head)) {
3071665484d8SDoug Ambrisko cmd = TAILQ_FIRST(&sc->mrsas_mfi_cmd_list_head);
3072665484d8SDoug Ambrisko TAILQ_REMOVE(&sc->mrsas_mfi_cmd_list_head, cmd, next);
3073665484d8SDoug Ambrisko }
3074665484d8SDoug Ambrisko mtx_unlock(&sc->mfi_cmd_pool_lock);
3075665484d8SDoug Ambrisko
3076665484d8SDoug Ambrisko return cmd;
3077665484d8SDoug Ambrisko }
3078665484d8SDoug Ambrisko
30798e727371SKashyap D Desai /*
30808e727371SKashyap D Desai * mrsas_ocr_thread: Thread to handle OCR/Kill Adapter.
3081665484d8SDoug Ambrisko * input: Adapter Context.
3082665484d8SDoug Ambrisko *
30838e727371SKashyap D Desai * This function will check FW status register and flag do_timeout_reset flag.
30848e727371SKashyap D Desai * It will do OCR/Kill adapter if FW is in fault state or IO timed out has
30858e727371SKashyap D Desai * trigger reset.
3086665484d8SDoug Ambrisko */
3087665484d8SDoug Ambrisko static void
mrsas_ocr_thread(void * arg)3088665484d8SDoug Ambrisko mrsas_ocr_thread(void *arg)
3089665484d8SDoug Ambrisko {
3090665484d8SDoug Ambrisko struct mrsas_softc *sc;
3091665484d8SDoug Ambrisko u_int32_t fw_status, fw_state;
30928bb601acSKashyap D Desai u_int8_t tm_target_reset_failed = 0;
3093665484d8SDoug Ambrisko
3094665484d8SDoug Ambrisko sc = (struct mrsas_softc *)arg;
3095665484d8SDoug Ambrisko
3096665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_TRACE, "%s\n", __func__);
3097665484d8SDoug Ambrisko sc->ocr_thread_active = 1;
3098665484d8SDoug Ambrisko mtx_lock(&sc->sim_lock);
3099665484d8SDoug Ambrisko for (;;) {
3100665484d8SDoug Ambrisko /* Sleep for 1 second and check the queue status */
3101665484d8SDoug Ambrisko msleep(&sc->ocr_chan, &sc->sim_lock, PRIBIO,
3102665484d8SDoug Ambrisko "mrsas_ocr", sc->mrsas_fw_fault_check_delay * hz);
3103f0c7594bSKashyap D Desai if (sc->remove_in_progress ||
3104f0c7594bSKashyap D Desai sc->adprecovery == MRSAS_HW_CRITICAL_ERROR) {
3105665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3106f0c7594bSKashyap D Desai "Exit due to %s from %s\n",
3107f0c7594bSKashyap D Desai sc->remove_in_progress ? "Shutdown" :
3108f0c7594bSKashyap D Desai "Hardware critical error", __func__);
3109665484d8SDoug Ambrisko break;
3110665484d8SDoug Ambrisko }
3111e315cf4dSKashyap D Desai fw_status = mrsas_read_reg_with_retries(sc,
3112665484d8SDoug Ambrisko offsetof(mrsas_reg_set, outbound_scratch_pad));
3113665484d8SDoug Ambrisko fw_state = fw_status & MFI_STATE_MASK;
31148bb601acSKashyap D Desai if (fw_state == MFI_STATE_FAULT || sc->do_timedout_reset ||
31158bb601acSKashyap D Desai mrsas_atomic_read(&sc->target_reset_outstanding)) {
31168bb601acSKashyap D Desai /* First, freeze further IOs to come to the SIM */
31178bb601acSKashyap D Desai mrsas_xpt_freeze(sc);
31188bb601acSKashyap D Desai
31198bb601acSKashyap D Desai /* If this is an IO timeout then go for target reset */
31208bb601acSKashyap D Desai if (mrsas_atomic_read(&sc->target_reset_outstanding)) {
31218bb601acSKashyap D Desai device_printf(sc->mrsas_dev, "Initiating Target RESET "
31228bb601acSKashyap D Desai "because of SCSI IO timeout!\n");
31238bb601acSKashyap D Desai
31248bb601acSKashyap D Desai /* Let the remaining IOs to complete */
31258bb601acSKashyap D Desai msleep(&sc->ocr_chan, &sc->sim_lock, PRIBIO,
31268bb601acSKashyap D Desai "mrsas_reset_targets", 5 * hz);
31278bb601acSKashyap D Desai
31288bb601acSKashyap D Desai /* Try to reset the target device */
31298bb601acSKashyap D Desai if (mrsas_reset_targets(sc) == FAIL)
31308bb601acSKashyap D Desai tm_target_reset_failed = 1;
31318bb601acSKashyap D Desai }
31328bb601acSKashyap D Desai
31338bb601acSKashyap D Desai /* If this is a DCMD timeout or FW fault,
31348bb601acSKashyap D Desai * then go for controller reset
31358bb601acSKashyap D Desai */
31368bb601acSKashyap D Desai if (fw_state == MFI_STATE_FAULT || tm_target_reset_failed ||
31378bb601acSKashyap D Desai (sc->do_timedout_reset == MFI_DCMD_TIMEOUT_OCR)) {
31388bb601acSKashyap D Desai if (tm_target_reset_failed)
31398bb601acSKashyap D Desai device_printf(sc->mrsas_dev, "Initiaiting OCR because of "
31408bb601acSKashyap D Desai "TM FAILURE!\n");
31418bb601acSKashyap D Desai else
31428bb601acSKashyap D Desai device_printf(sc->mrsas_dev, "Initiaiting OCR "
31438bb601acSKashyap D Desai "because of %s!\n", sc->do_timedout_reset ?
31448bb601acSKashyap D Desai "DCMD IO Timeout" : "FW fault");
31458bb601acSKashyap D Desai
3146665484d8SDoug Ambrisko mtx_lock_spin(&sc->ioctl_lock);
3147665484d8SDoug Ambrisko sc->reset_in_progress = 1;
3148665484d8SDoug Ambrisko mtx_unlock_spin(&sc->ioctl_lock);
31498bb601acSKashyap D Desai sc->reset_count++;
31508bb601acSKashyap D Desai
315185c0a961SKashyap D Desai /*
315285c0a961SKashyap D Desai * Wait for the AEN task to be completed if it is running.
315385c0a961SKashyap D Desai */
315485c0a961SKashyap D Desai mtx_unlock(&sc->sim_lock);
315585c0a961SKashyap D Desai taskqueue_drain(sc->ev_tq, &sc->ev_task);
315685c0a961SKashyap D Desai mtx_lock(&sc->sim_lock);
315785c0a961SKashyap D Desai
315885c0a961SKashyap D Desai taskqueue_block(sc->ev_tq);
31598bb601acSKashyap D Desai /* Try to reset the controller */
3160f0c7594bSKashyap D Desai mrsas_reset_ctrl(sc, sc->do_timedout_reset);
31618bb601acSKashyap D Desai
3162665484d8SDoug Ambrisko sc->do_timedout_reset = 0;
31638bb601acSKashyap D Desai sc->reset_in_progress = 0;
31648bb601acSKashyap D Desai tm_target_reset_failed = 0;
31658bb601acSKashyap D Desai mrsas_atomic_set(&sc->target_reset_outstanding, 0);
31668bb601acSKashyap D Desai memset(sc->target_reset_pool, 0,
31678bb601acSKashyap D Desai sizeof(sc->target_reset_pool));
316885c0a961SKashyap D Desai taskqueue_unblock(sc->ev_tq);
31698bb601acSKashyap D Desai }
31708bb601acSKashyap D Desai
31718bb601acSKashyap D Desai /* Now allow IOs to come to the SIM */
31728bb601acSKashyap D Desai mrsas_xpt_release(sc);
3173665484d8SDoug Ambrisko }
3174665484d8SDoug Ambrisko }
3175665484d8SDoug Ambrisko mtx_unlock(&sc->sim_lock);
3176665484d8SDoug Ambrisko sc->ocr_thread_active = 0;
3177665484d8SDoug Ambrisko mrsas_kproc_exit(0);
3178665484d8SDoug Ambrisko }
3179665484d8SDoug Ambrisko
31808e727371SKashyap D Desai /*
31818e727371SKashyap D Desai * mrsas_reset_reply_desc: Reset Reply descriptor as part of OCR.
3182665484d8SDoug Ambrisko * input: Adapter Context.
3183665484d8SDoug Ambrisko *
31848e727371SKashyap D Desai * This function will clear reply descriptor so that post OCR driver and FW will
31858e727371SKashyap D Desai * lost old history.
3186665484d8SDoug Ambrisko */
31878e727371SKashyap D Desai void
mrsas_reset_reply_desc(struct mrsas_softc * sc)31888e727371SKashyap D Desai mrsas_reset_reply_desc(struct mrsas_softc *sc)
3189665484d8SDoug Ambrisko {
3190d18d1b47SKashyap D Desai int i, count;
3191665484d8SDoug Ambrisko pMpi2ReplyDescriptorsUnion_t reply_desc;
3192665484d8SDoug Ambrisko
3193d18d1b47SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
3194d18d1b47SKashyap D Desai for (i = 0; i < count; i++)
3195d18d1b47SKashyap D Desai sc->last_reply_idx[i] = 0;
3196d18d1b47SKashyap D Desai
3197665484d8SDoug Ambrisko reply_desc = sc->reply_desc_mem;
3198665484d8SDoug Ambrisko for (i = 0; i < sc->reply_q_depth; i++, reply_desc++) {
3199665484d8SDoug Ambrisko reply_desc->Words = MRSAS_ULONG_MAX;
3200665484d8SDoug Ambrisko }
3201665484d8SDoug Ambrisko }
3202665484d8SDoug Ambrisko
32038e727371SKashyap D Desai /*
32048e727371SKashyap D Desai * mrsas_reset_ctrl: Core function to OCR/Kill adapter.
3205665484d8SDoug Ambrisko * input: Adapter Context.
3206665484d8SDoug Ambrisko *
32078e727371SKashyap D Desai * This function will run from thread context so that it can sleep. 1. Do not
32088e727371SKashyap D Desai * handle OCR if FW is in HW critical error. 2. Wait for outstanding command
32098e727371SKashyap D Desai * to complete for 180 seconds. 3. If #2 does not find any outstanding
32108e727371SKashyap D Desai * command Controller is in working state, so skip OCR. Otherwise, do
32118e727371SKashyap D Desai * OCR/kill Adapter based on flag disableOnlineCtrlReset. 4. Start of the
32128e727371SKashyap D Desai * OCR, return all SCSI command back to CAM layer which has ccb_ptr. 5. Post
3213453130d9SPedro F. Giffuni * OCR, Re-fire Management command and move Controller to Operation state.
3214665484d8SDoug Ambrisko */
32158e727371SKashyap D Desai int
mrsas_reset_ctrl(struct mrsas_softc * sc,u_int8_t reset_reason)3216f0c7594bSKashyap D Desai mrsas_reset_ctrl(struct mrsas_softc *sc, u_int8_t reset_reason)
3217665484d8SDoug Ambrisko {
3218665484d8SDoug Ambrisko int retval = SUCCESS, i, j, retry = 0;
3219665484d8SDoug Ambrisko u_int32_t host_diag, abs_state, status_reg, reset_adapter;
3220665484d8SDoug Ambrisko union ccb *ccb;
3221665484d8SDoug Ambrisko struct mrsas_mfi_cmd *mfi_cmd;
3222665484d8SDoug Ambrisko struct mrsas_mpt_cmd *mpt_cmd;
3223f0c7594bSKashyap D Desai union mrsas_evt_class_locale class_locale;
32242d53b485SKashyap D Desai MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3225665484d8SDoug Ambrisko
3226665484d8SDoug Ambrisko if (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR) {
3227665484d8SDoug Ambrisko device_printf(sc->mrsas_dev,
3228665484d8SDoug Ambrisko "mrsas: Hardware critical error, returning FAIL.\n");
3229665484d8SDoug Ambrisko return FAIL;
3230665484d8SDoug Ambrisko }
3231f5fb2237SKashyap D Desai mrsas_set_bit(MRSAS_FUSION_IN_RESET, &sc->reset_flags);
3232665484d8SDoug Ambrisko sc->adprecovery = MRSAS_ADPRESET_SM_INFAULT;
3233665484d8SDoug Ambrisko mrsas_disable_intr(sc);
3234f0c7594bSKashyap D Desai msleep(&sc->ocr_chan, &sc->sim_lock, PRIBIO, "mrsas_ocr",
3235f0c7594bSKashyap D Desai sc->mrsas_fw_fault_check_delay * hz);
3236665484d8SDoug Ambrisko
3237665484d8SDoug Ambrisko /* First try waiting for commands to complete */
3238f0c7594bSKashyap D Desai if (mrsas_wait_for_outstanding(sc, reset_reason)) {
3239665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3240665484d8SDoug Ambrisko "resetting adapter from %s.\n",
3241665484d8SDoug Ambrisko __func__);
3242665484d8SDoug Ambrisko /* Now return commands back to the CAM layer */
32435b2490f8SKashyap D Desai mtx_unlock(&sc->sim_lock);
3244665484d8SDoug Ambrisko for (i = 0; i < sc->max_fw_cmds; i++) {
3245665484d8SDoug Ambrisko mpt_cmd = sc->mpt_cmd_list[i];
32462a1d3bcdSKashyap D Desai
32472a1d3bcdSKashyap D Desai if (mpt_cmd->peer_cmd) {
32482a1d3bcdSKashyap D Desai mrsas_dprint(sc, MRSAS_OCR,
32492a1d3bcdSKashyap D Desai "R1 FP command [%d] - (mpt_cmd) %p, (peer_cmd) %p\n",
32502a1d3bcdSKashyap D Desai i, mpt_cmd, mpt_cmd->peer_cmd);
32512a1d3bcdSKashyap D Desai }
32522a1d3bcdSKashyap D Desai
3253665484d8SDoug Ambrisko if (mpt_cmd->ccb_ptr) {
32542a1d3bcdSKashyap D Desai if (mpt_cmd->callout_owner) {
3255665484d8SDoug Ambrisko ccb = (union ccb *)(mpt_cmd->ccb_ptr);
3256665484d8SDoug Ambrisko ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
3257665484d8SDoug Ambrisko mrsas_cmd_done(sc, mpt_cmd);
32582a1d3bcdSKashyap D Desai } else {
32592a1d3bcdSKashyap D Desai mpt_cmd->ccb_ptr = NULL;
32602a1d3bcdSKashyap D Desai mrsas_release_mpt_cmd(mpt_cmd);
3261665484d8SDoug Ambrisko }
3262665484d8SDoug Ambrisko }
32632a1d3bcdSKashyap D Desai }
32642a1d3bcdSKashyap D Desai
32652a1d3bcdSKashyap D Desai mrsas_atomic_set(&sc->fw_outstanding, 0);
32662a1d3bcdSKashyap D Desai
32675b2490f8SKashyap D Desai mtx_lock(&sc->sim_lock);
3268665484d8SDoug Ambrisko
3269e315cf4dSKashyap D Desai status_reg = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3270665484d8SDoug Ambrisko outbound_scratch_pad));
3271665484d8SDoug Ambrisko abs_state = status_reg & MFI_STATE_MASK;
3272665484d8SDoug Ambrisko reset_adapter = status_reg & MFI_RESET_ADAPTER;
3273665484d8SDoug Ambrisko if (sc->disableOnlineCtrlReset ||
3274665484d8SDoug Ambrisko (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
3275665484d8SDoug Ambrisko /* Reset not supported, kill adapter */
3276665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "Reset not supported, killing adapter.\n");
3277665484d8SDoug Ambrisko mrsas_kill_hba(sc);
3278665484d8SDoug Ambrisko retval = FAIL;
3279665484d8SDoug Ambrisko goto out;
3280665484d8SDoug Ambrisko }
3281665484d8SDoug Ambrisko /* Now try to reset the chip */
3282665484d8SDoug Ambrisko for (i = 0; i < MRSAS_FUSION_MAX_RESET_TRIES; i++) {
3283665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3284665484d8SDoug Ambrisko MPI2_WRSEQ_FLUSH_KEY_VALUE);
3285665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3286665484d8SDoug Ambrisko MPI2_WRSEQ_1ST_KEY_VALUE);
3287665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3288665484d8SDoug Ambrisko MPI2_WRSEQ_2ND_KEY_VALUE);
3289665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3290665484d8SDoug Ambrisko MPI2_WRSEQ_3RD_KEY_VALUE);
3291665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3292665484d8SDoug Ambrisko MPI2_WRSEQ_4TH_KEY_VALUE);
3293665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3294665484d8SDoug Ambrisko MPI2_WRSEQ_5TH_KEY_VALUE);
3295665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_seq_offset),
3296665484d8SDoug Ambrisko MPI2_WRSEQ_6TH_KEY_VALUE);
3297665484d8SDoug Ambrisko
3298665484d8SDoug Ambrisko /* Check that the diag write enable (DRWE) bit is on */
3299e315cf4dSKashyap D Desai host_diag = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3300665484d8SDoug Ambrisko fusion_host_diag));
3301665484d8SDoug Ambrisko retry = 0;
3302665484d8SDoug Ambrisko while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3303665484d8SDoug Ambrisko DELAY(100 * 1000);
3304e315cf4dSKashyap D Desai host_diag = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3305665484d8SDoug Ambrisko fusion_host_diag));
3306665484d8SDoug Ambrisko if (retry++ == 100) {
3307665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3308665484d8SDoug Ambrisko "Host diag unlock failed!\n");
3309665484d8SDoug Ambrisko break;
3310665484d8SDoug Ambrisko }
3311665484d8SDoug Ambrisko }
3312665484d8SDoug Ambrisko if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3313665484d8SDoug Ambrisko continue;
3314665484d8SDoug Ambrisko
3315665484d8SDoug Ambrisko /* Send chip reset command */
3316665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, fusion_host_diag),
3317665484d8SDoug Ambrisko host_diag | HOST_DIAG_RESET_ADAPTER);
3318665484d8SDoug Ambrisko DELAY(3000 * 1000);
3319665484d8SDoug Ambrisko
3320665484d8SDoug Ambrisko /* Make sure reset adapter bit is cleared */
3321e315cf4dSKashyap D Desai host_diag = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3322665484d8SDoug Ambrisko fusion_host_diag));
3323665484d8SDoug Ambrisko retry = 0;
3324665484d8SDoug Ambrisko while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3325665484d8SDoug Ambrisko DELAY(100 * 1000);
3326e315cf4dSKashyap D Desai host_diag = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3327665484d8SDoug Ambrisko fusion_host_diag));
3328665484d8SDoug Ambrisko if (retry++ == 1000) {
3329665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3330665484d8SDoug Ambrisko "Diag reset adapter never cleared!\n");
3331665484d8SDoug Ambrisko break;
3332665484d8SDoug Ambrisko }
3333665484d8SDoug Ambrisko }
3334665484d8SDoug Ambrisko if (host_diag & HOST_DIAG_RESET_ADAPTER)
3335665484d8SDoug Ambrisko continue;
3336665484d8SDoug Ambrisko
3337e315cf4dSKashyap D Desai abs_state = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3338665484d8SDoug Ambrisko outbound_scratch_pad)) & MFI_STATE_MASK;
3339665484d8SDoug Ambrisko retry = 0;
3340665484d8SDoug Ambrisko
3341665484d8SDoug Ambrisko while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3342665484d8SDoug Ambrisko DELAY(100 * 1000);
3343e315cf4dSKashyap D Desai abs_state = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3344665484d8SDoug Ambrisko outbound_scratch_pad)) & MFI_STATE_MASK;
3345665484d8SDoug Ambrisko }
3346665484d8SDoug Ambrisko if (abs_state <= MFI_STATE_FW_INIT) {
3347665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "firmware state < MFI_STATE_FW_INIT,"
3348665484d8SDoug Ambrisko " state = 0x%x\n", abs_state);
3349665484d8SDoug Ambrisko continue;
3350665484d8SDoug Ambrisko }
3351665484d8SDoug Ambrisko /* Wait for FW to become ready */
3352665484d8SDoug Ambrisko if (mrsas_transition_to_ready(sc, 1)) {
3353665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3354665484d8SDoug Ambrisko "mrsas: Failed to transition controller to ready.\n");
3355665484d8SDoug Ambrisko continue;
3356665484d8SDoug Ambrisko }
3357665484d8SDoug Ambrisko mrsas_reset_reply_desc(sc);
3358665484d8SDoug Ambrisko if (mrsas_ioc_init(sc)) {
3359665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "mrsas_ioc_init() failed!\n");
3360665484d8SDoug Ambrisko continue;
3361665484d8SDoug Ambrisko }
3362665484d8SDoug Ambrisko for (j = 0; j < sc->max_fw_cmds; j++) {
3363665484d8SDoug Ambrisko mpt_cmd = sc->mpt_cmd_list[j];
3364665484d8SDoug Ambrisko if (mpt_cmd->sync_cmd_idx != (u_int32_t)MRSAS_ULONG_MAX) {
3365665484d8SDoug Ambrisko mfi_cmd = sc->mfi_cmd_list[mpt_cmd->sync_cmd_idx];
33662d53b485SKashyap D Desai /* If not an IOCTL then release the command else re-fire */
33672d53b485SKashyap D Desai if (!mfi_cmd->sync_cmd) {
3368665484d8SDoug Ambrisko mrsas_release_mfi_cmd(mfi_cmd);
33692d53b485SKashyap D Desai } else {
33702d53b485SKashyap D Desai req_desc = mrsas_get_request_desc(sc,
33712d53b485SKashyap D Desai mfi_cmd->cmd_id.context.smid - 1);
33722d53b485SKashyap D Desai mrsas_dprint(sc, MRSAS_OCR,
33732d53b485SKashyap D Desai "Re-fire command DCMD opcode 0x%x index %d\n ",
33742d53b485SKashyap D Desai mfi_cmd->frame->dcmd.opcode, j);
33752d53b485SKashyap D Desai if (!req_desc)
33762d53b485SKashyap D Desai device_printf(sc->mrsas_dev,
33772d53b485SKashyap D Desai "Cannot build MPT cmd.\n");
33782d53b485SKashyap D Desai else
33792d53b485SKashyap D Desai mrsas_fire_cmd(sc, req_desc->addr.u.low,
33802d53b485SKashyap D Desai req_desc->addr.u.high);
33812d53b485SKashyap D Desai }
3382665484d8SDoug Ambrisko }
3383665484d8SDoug Ambrisko }
3384f0c7594bSKashyap D Desai
3385665484d8SDoug Ambrisko /* Reset load balance info */
3386665484d8SDoug Ambrisko memset(sc->load_balance_info, 0,
33874799d485SKashyap D Desai sizeof(LD_LOAD_BALANCE_INFO) * MAX_LOGICAL_DRIVES_EXT);
3388665484d8SDoug Ambrisko
3389af51c29fSKashyap D Desai if (mrsas_get_ctrl_info(sc)) {
3390af51c29fSKashyap D Desai mrsas_kill_hba(sc);
33912f863eb8SKashyap D Desai retval = FAIL;
33922f863eb8SKashyap D Desai goto out;
3393af51c29fSKashyap D Desai }
3394665484d8SDoug Ambrisko if (!mrsas_get_map_info(sc))
3395665484d8SDoug Ambrisko mrsas_sync_map_info(sc);
3396665484d8SDoug Ambrisko
3397a688fcd0SKashyap D Desai megasas_setup_jbod_map(sc);
3398a688fcd0SKashyap D Desai
33992909aab4SKashyap D Desai if ((sc->is_ventura || sc->is_aero) && sc->streamDetectByLD) {
3400821df4b9SKashyap D Desai for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
3401821df4b9SKashyap D Desai memset(sc->streamDetectByLD[i], 0, sizeof(LD_STREAM_DETECT));
3402821df4b9SKashyap D Desai sc->streamDetectByLD[i]->mruBitMap = MR_STREAM_BITMAP;
3403821df4b9SKashyap D Desai }
3404821df4b9SKashyap D Desai }
3405821df4b9SKashyap D Desai
34062f863eb8SKashyap D Desai mrsas_clear_bit(MRSAS_FUSION_IN_RESET, &sc->reset_flags);
34072f863eb8SKashyap D Desai mrsas_enable_intr(sc);
34082f863eb8SKashyap D Desai sc->adprecovery = MRSAS_HBA_OPERATIONAL;
34092f863eb8SKashyap D Desai
3410f0c7594bSKashyap D Desai /* Register AEN with FW for last sequence number */
3411f0c7594bSKashyap D Desai class_locale.members.reserved = 0;
3412f0c7594bSKashyap D Desai class_locale.members.locale = MR_EVT_LOCALE_ALL;
3413f0c7594bSKashyap D Desai class_locale.members.class = MR_EVT_CLASS_DEBUG;
3414f0c7594bSKashyap D Desai
34152d53b485SKashyap D Desai mtx_unlock(&sc->sim_lock);
3416f0c7594bSKashyap D Desai if (mrsas_register_aen(sc, sc->last_seq_num,
3417f0c7594bSKashyap D Desai class_locale.word)) {
3418f0c7594bSKashyap D Desai device_printf(sc->mrsas_dev,
3419f0c7594bSKashyap D Desai "ERROR: AEN registration FAILED from OCR !!! "
3420f0c7594bSKashyap D Desai "Further events from the controller cannot be notified."
3421f0c7594bSKashyap D Desai "Either there is some problem in the controller"
3422f0c7594bSKashyap D Desai "or the controller does not support AEN.\n"
3423f0c7594bSKashyap D Desai "Please contact to the SUPPORT TEAM if the problem persists\n");
3424f0c7594bSKashyap D Desai }
34252d53b485SKashyap D Desai mtx_lock(&sc->sim_lock);
34262d53b485SKashyap D Desai
3427665484d8SDoug Ambrisko /* Adapter reset completed successfully */
3428665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Reset successful\n");
3429665484d8SDoug Ambrisko retval = SUCCESS;
3430665484d8SDoug Ambrisko goto out;
3431665484d8SDoug Ambrisko }
3432665484d8SDoug Ambrisko /* Reset failed, kill the adapter */
3433665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Reset failed, killing adapter.\n");
3434665484d8SDoug Ambrisko mrsas_kill_hba(sc);
3435665484d8SDoug Ambrisko retval = FAIL;
3436665484d8SDoug Ambrisko } else {
3437f5fb2237SKashyap D Desai mrsas_clear_bit(MRSAS_FUSION_IN_RESET, &sc->reset_flags);
3438665484d8SDoug Ambrisko mrsas_enable_intr(sc);
3439665484d8SDoug Ambrisko sc->adprecovery = MRSAS_HBA_OPERATIONAL;
3440665484d8SDoug Ambrisko }
3441665484d8SDoug Ambrisko out:
3442f5fb2237SKashyap D Desai mrsas_clear_bit(MRSAS_FUSION_IN_RESET, &sc->reset_flags);
3443665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3444665484d8SDoug Ambrisko "Reset Exit with %d.\n", retval);
3445665484d8SDoug Ambrisko return retval;
3446665484d8SDoug Ambrisko }
3447665484d8SDoug Ambrisko
34488e727371SKashyap D Desai /*
34498e727371SKashyap D Desai * mrsas_kill_hba: Kill HBA when OCR is not supported
3450665484d8SDoug Ambrisko * input: Adapter Context.
3451665484d8SDoug Ambrisko *
3452665484d8SDoug Ambrisko * This function will kill HBA when OCR is not supported.
3453665484d8SDoug Ambrisko */
34548e727371SKashyap D Desai void
mrsas_kill_hba(struct mrsas_softc * sc)34558e727371SKashyap D Desai mrsas_kill_hba(struct mrsas_softc *sc)
3456665484d8SDoug Ambrisko {
3457daeed973SKashyap D Desai sc->adprecovery = MRSAS_HW_CRITICAL_ERROR;
3458f0c7594bSKashyap D Desai DELAY(1000 * 1000);
3459665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "%s\n", __func__);
3460665484d8SDoug Ambrisko mrsas_write_reg(sc, offsetof(mrsas_reg_set, doorbell),
3461665484d8SDoug Ambrisko MFI_STOP_ADP);
3462665484d8SDoug Ambrisko /* Flush */
3463665484d8SDoug Ambrisko mrsas_read_reg(sc, offsetof(mrsas_reg_set, doorbell));
3464daeed973SKashyap D Desai mrsas_complete_outstanding_ioctls(sc);
3465daeed973SKashyap D Desai }
3466daeed973SKashyap D Desai
3467daeed973SKashyap D Desai /**
3468daeed973SKashyap D Desai * mrsas_complete_outstanding_ioctls Complete pending IOCTLS after kill_hba
3469daeed973SKashyap D Desai * input: Controller softc
3470daeed973SKashyap D Desai *
3471daeed973SKashyap D Desai * Returns void
3472daeed973SKashyap D Desai */
3473dbcc81dfSKashyap D Desai void
mrsas_complete_outstanding_ioctls(struct mrsas_softc * sc)3474dbcc81dfSKashyap D Desai mrsas_complete_outstanding_ioctls(struct mrsas_softc *sc)
3475dbcc81dfSKashyap D Desai {
3476daeed973SKashyap D Desai int i;
3477daeed973SKashyap D Desai struct mrsas_mpt_cmd *cmd_mpt;
3478daeed973SKashyap D Desai struct mrsas_mfi_cmd *cmd_mfi;
3479daeed973SKashyap D Desai u_int32_t count, MSIxIndex;
3480daeed973SKashyap D Desai
3481daeed973SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
3482daeed973SKashyap D Desai for (i = 0; i < sc->max_fw_cmds; i++) {
3483daeed973SKashyap D Desai cmd_mpt = sc->mpt_cmd_list[i];
3484daeed973SKashyap D Desai
3485daeed973SKashyap D Desai if (cmd_mpt->sync_cmd_idx != (u_int32_t)MRSAS_ULONG_MAX) {
3486daeed973SKashyap D Desai cmd_mfi = sc->mfi_cmd_list[cmd_mpt->sync_cmd_idx];
3487daeed973SKashyap D Desai if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT) {
3488daeed973SKashyap D Desai for (MSIxIndex = 0; MSIxIndex < count; MSIxIndex++)
3489daeed973SKashyap D Desai mrsas_complete_mptmfi_passthru(sc, cmd_mfi,
3490503c4f8dSKashyap D Desai cmd_mpt->io_request->RaidContext.raid_context.status);
3491daeed973SKashyap D Desai }
3492daeed973SKashyap D Desai }
3493daeed973SKashyap D Desai }
3494665484d8SDoug Ambrisko }
3495665484d8SDoug Ambrisko
34968e727371SKashyap D Desai /*
34978e727371SKashyap D Desai * mrsas_wait_for_outstanding: Wait for outstanding commands
3498665484d8SDoug Ambrisko * input: Adapter Context.
3499665484d8SDoug Ambrisko *
35008e727371SKashyap D Desai * This function will wait for 180 seconds for outstanding commands to be
35018e727371SKashyap D Desai * completed.
3502665484d8SDoug Ambrisko */
35038e727371SKashyap D Desai int
mrsas_wait_for_outstanding(struct mrsas_softc * sc,u_int8_t check_reason)3504f0c7594bSKashyap D Desai mrsas_wait_for_outstanding(struct mrsas_softc *sc, u_int8_t check_reason)
3505665484d8SDoug Ambrisko {
3506665484d8SDoug Ambrisko int i, outstanding, retval = 0;
3507d18d1b47SKashyap D Desai u_int32_t fw_state, count, MSIxIndex;
3508d18d1b47SKashyap D Desai
3509665484d8SDoug Ambrisko for (i = 0; i < MRSAS_RESET_WAIT_TIME; i++) {
3510665484d8SDoug Ambrisko if (sc->remove_in_progress) {
3511665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3512665484d8SDoug Ambrisko "Driver remove or shutdown called.\n");
3513665484d8SDoug Ambrisko retval = 1;
3514665484d8SDoug Ambrisko goto out;
3515665484d8SDoug Ambrisko }
3516665484d8SDoug Ambrisko /* Check if firmware is in fault state */
3517e315cf4dSKashyap D Desai fw_state = mrsas_read_reg_with_retries(sc, offsetof(mrsas_reg_set,
3518665484d8SDoug Ambrisko outbound_scratch_pad)) & MFI_STATE_MASK;
3519665484d8SDoug Ambrisko if (fw_state == MFI_STATE_FAULT) {
3520665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3521665484d8SDoug Ambrisko "Found FW in FAULT state, will reset adapter.\n");
3522e2e8afb1SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
3523e2e8afb1SKashyap D Desai mtx_unlock(&sc->sim_lock);
3524e2e8afb1SKashyap D Desai for (MSIxIndex = 0; MSIxIndex < count; MSIxIndex++)
3525e2e8afb1SKashyap D Desai mrsas_complete_cmd(sc, MSIxIndex);
3526e2e8afb1SKashyap D Desai mtx_lock(&sc->sim_lock);
3527665484d8SDoug Ambrisko retval = 1;
3528665484d8SDoug Ambrisko goto out;
3529665484d8SDoug Ambrisko }
3530f0c7594bSKashyap D Desai if (check_reason == MFI_DCMD_TIMEOUT_OCR) {
3531f0c7594bSKashyap D Desai mrsas_dprint(sc, MRSAS_OCR,
3532f0c7594bSKashyap D Desai "DCMD IO TIMEOUT detected, will reset adapter.\n");
3533f0c7594bSKashyap D Desai retval = 1;
3534f0c7594bSKashyap D Desai goto out;
3535f0c7594bSKashyap D Desai }
3536f5fb2237SKashyap D Desai outstanding = mrsas_atomic_read(&sc->fw_outstanding);
3537665484d8SDoug Ambrisko if (!outstanding)
3538665484d8SDoug Ambrisko goto out;
3539665484d8SDoug Ambrisko
3540665484d8SDoug Ambrisko if (!(i % MRSAS_RESET_NOTICE_INTERVAL)) {
3541665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR, "[%2d]waiting for %d "
3542665484d8SDoug Ambrisko "commands to complete\n", i, outstanding);
3543d18d1b47SKashyap D Desai count = sc->msix_vectors > 0 ? sc->msix_vectors : 1;
35442d53b485SKashyap D Desai mtx_unlock(&sc->sim_lock);
3545d18d1b47SKashyap D Desai for (MSIxIndex = 0; MSIxIndex < count; MSIxIndex++)
3546d18d1b47SKashyap D Desai mrsas_complete_cmd(sc, MSIxIndex);
35472d53b485SKashyap D Desai mtx_lock(&sc->sim_lock);
3548665484d8SDoug Ambrisko }
3549665484d8SDoug Ambrisko DELAY(1000 * 1000);
3550665484d8SDoug Ambrisko }
3551665484d8SDoug Ambrisko
3552f5fb2237SKashyap D Desai if (mrsas_atomic_read(&sc->fw_outstanding)) {
3553665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_OCR,
3554665484d8SDoug Ambrisko " pending commands remain after waiting,"
3555665484d8SDoug Ambrisko " will reset adapter.\n");
3556665484d8SDoug Ambrisko retval = 1;
3557665484d8SDoug Ambrisko }
3558665484d8SDoug Ambrisko out:
3559665484d8SDoug Ambrisko return retval;
3560665484d8SDoug Ambrisko }
3561665484d8SDoug Ambrisko
35628e727371SKashyap D Desai /*
3563665484d8SDoug Ambrisko * mrsas_release_mfi_cmd: Return a cmd to free command pool
3564665484d8SDoug Ambrisko * input: Command packet for return to free cmd pool
3565665484d8SDoug Ambrisko *
3566731b7561SKashyap D Desai * This function returns the MFI & MPT command to the command list.
3567665484d8SDoug Ambrisko */
35688e727371SKashyap D Desai void
mrsas_release_mfi_cmd(struct mrsas_mfi_cmd * cmd_mfi)3569731b7561SKashyap D Desai mrsas_release_mfi_cmd(struct mrsas_mfi_cmd *cmd_mfi)
3570665484d8SDoug Ambrisko {
3571731b7561SKashyap D Desai struct mrsas_softc *sc = cmd_mfi->sc;
3572731b7561SKashyap D Desai struct mrsas_mpt_cmd *cmd_mpt;
3573731b7561SKashyap D Desai
3574665484d8SDoug Ambrisko mtx_lock(&sc->mfi_cmd_pool_lock);
3575731b7561SKashyap D Desai /*
3576731b7561SKashyap D Desai * Release the mpt command (if at all it is allocated
3577731b7561SKashyap D Desai * associated with the mfi command
3578731b7561SKashyap D Desai */
3579731b7561SKashyap D Desai if (cmd_mfi->cmd_id.context.smid) {
3580731b7561SKashyap D Desai mtx_lock(&sc->mpt_cmd_pool_lock);
3581731b7561SKashyap D Desai /* Get the mpt cmd from mfi cmd frame's smid value */
3582731b7561SKashyap D Desai cmd_mpt = sc->mpt_cmd_list[cmd_mfi->cmd_id.context.smid-1];
3583731b7561SKashyap D Desai cmd_mpt->flags = 0;
3584731b7561SKashyap D Desai cmd_mpt->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
3585731b7561SKashyap D Desai TAILQ_INSERT_HEAD(&(sc->mrsas_mpt_cmd_list_head), cmd_mpt, next);
3586731b7561SKashyap D Desai mtx_unlock(&sc->mpt_cmd_pool_lock);
3587731b7561SKashyap D Desai }
3588731b7561SKashyap D Desai /* Release the mfi command */
3589731b7561SKashyap D Desai cmd_mfi->ccb_ptr = NULL;
3590731b7561SKashyap D Desai cmd_mfi->cmd_id.frame_count = 0;
3591731b7561SKashyap D Desai TAILQ_INSERT_HEAD(&(sc->mrsas_mfi_cmd_list_head), cmd_mfi, next);
3592665484d8SDoug Ambrisko mtx_unlock(&sc->mfi_cmd_pool_lock);
3593665484d8SDoug Ambrisko
3594665484d8SDoug Ambrisko return;
3595665484d8SDoug Ambrisko }
3596665484d8SDoug Ambrisko
35978e727371SKashyap D Desai /*
35988e727371SKashyap D Desai * mrsas_get_controller_info: Returns FW's controller structure
3599665484d8SDoug Ambrisko * input: Adapter soft state
3600665484d8SDoug Ambrisko * Controller information structure
3601665484d8SDoug Ambrisko *
36028e727371SKashyap D Desai * Issues an internal command (DCMD) to get the FW's controller structure. This
36038e727371SKashyap D Desai * information is mainly used to find out the maximum IO transfer per command
36048e727371SKashyap D Desai * supported by the FW.
3605665484d8SDoug Ambrisko */
36068e727371SKashyap D Desai static int
mrsas_get_ctrl_info(struct mrsas_softc * sc)3607af51c29fSKashyap D Desai mrsas_get_ctrl_info(struct mrsas_softc *sc)
3608665484d8SDoug Ambrisko {
3609665484d8SDoug Ambrisko int retcode = 0;
3610f0c7594bSKashyap D Desai u_int8_t do_ocr = 1;
3611665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
3612665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
3613665484d8SDoug Ambrisko
3614665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
3615665484d8SDoug Ambrisko
3616665484d8SDoug Ambrisko if (!cmd) {
3617665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Failed to get a free cmd\n");
3618665484d8SDoug Ambrisko return -ENOMEM;
3619665484d8SDoug Ambrisko }
3620665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
3621665484d8SDoug Ambrisko
3622665484d8SDoug Ambrisko if (mrsas_alloc_ctlr_info_cmd(sc) != SUCCESS) {
3623665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate get ctlr info cmd\n");
3624665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
3625665484d8SDoug Ambrisko return -ENOMEM;
3626665484d8SDoug Ambrisko }
3627665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3628665484d8SDoug Ambrisko
3629665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
3630665484d8SDoug Ambrisko dcmd->cmd_status = 0xFF;
3631665484d8SDoug Ambrisko dcmd->sge_count = 1;
3632665484d8SDoug Ambrisko dcmd->flags = MFI_FRAME_DIR_READ;
3633665484d8SDoug Ambrisko dcmd->timeout = 0;
3634665484d8SDoug Ambrisko dcmd->pad_0 = 0;
3635e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sizeof(struct mrsas_ctrl_info));
3636e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_CTRL_GET_INFO);
3637e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(sc->ctlr_info_phys_addr & 0xFFFFFFFF);
3638e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sizeof(struct mrsas_ctrl_info));
3639665484d8SDoug Ambrisko
36408bc320adSKashyap D Desai if (!sc->mask_interrupts)
36418bc320adSKashyap D Desai retcode = mrsas_issue_blocked_cmd(sc, cmd);
36428bc320adSKashyap D Desai else
3643f0c7594bSKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
36448bc320adSKashyap D Desai
3645f0c7594bSKashyap D Desai if (retcode == ETIMEDOUT)
3646f0c7594bSKashyap D Desai goto dcmd_timeout;
3647e34a057cSAlfredo Dal'Ava Junior else {
3648f0c7594bSKashyap D Desai memcpy(sc->ctrl_info, sc->ctlr_info_mem, sizeof(struct mrsas_ctrl_info));
3649e34a057cSAlfredo Dal'Ava Junior le32_to_cpus(&sc->ctrl_info->properties.OnOffProperties);
3650e34a057cSAlfredo Dal'Ava Junior le32_to_cpus(&sc->ctrl_info->adapterOperations2);
3651e34a057cSAlfredo Dal'Ava Junior le32_to_cpus(&sc->ctrl_info->adapterOperations3);
3652e34a057cSAlfredo Dal'Ava Junior le16_to_cpus(&sc->ctrl_info->adapterOperations4);
3653e34a057cSAlfredo Dal'Ava Junior }
3654665484d8SDoug Ambrisko
3655f0c7594bSKashyap D Desai do_ocr = 0;
3656af51c29fSKashyap D Desai mrsas_update_ext_vd_details(sc);
3657af51c29fSKashyap D Desai
3658a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp =
3659a688fcd0SKashyap D Desai sc->ctrl_info->adapterOperations3.useSeqNumJbodFP;
3660c376f864SKashyap D Desai sc->support_morethan256jbod =
3661c376f864SKashyap D Desai sc->ctrl_info->adapterOperations4.supportPdMapTargetId;
3662c376f864SKashyap D Desai
36638bc320adSKashyap D Desai sc->disableOnlineCtrlReset =
36648bc320adSKashyap D Desai sc->ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3665a688fcd0SKashyap D Desai
3666f0c7594bSKashyap D Desai dcmd_timeout:
3667665484d8SDoug Ambrisko mrsas_free_ctlr_info_cmd(sc);
3668f0c7594bSKashyap D Desai
3669f0c7594bSKashyap D Desai if (do_ocr)
3670f0c7594bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
3671f0c7594bSKashyap D Desai
36728bc320adSKashyap D Desai if (!sc->mask_interrupts)
36738bc320adSKashyap D Desai mrsas_release_mfi_cmd(cmd);
36748bc320adSKashyap D Desai
3675665484d8SDoug Ambrisko return (retcode);
3676665484d8SDoug Ambrisko }
3677665484d8SDoug Ambrisko
36788e727371SKashyap D Desai /*
3679af51c29fSKashyap D Desai * mrsas_update_ext_vd_details : Update details w.r.t Extended VD
3680af51c29fSKashyap D Desai * input:
3681af51c29fSKashyap D Desai * sc - Controller's softc
3682af51c29fSKashyap D Desai */
3683dbcc81dfSKashyap D Desai static void
mrsas_update_ext_vd_details(struct mrsas_softc * sc)3684dbcc81dfSKashyap D Desai mrsas_update_ext_vd_details(struct mrsas_softc *sc)
3685af51c29fSKashyap D Desai {
36864ad83576SKashyap D Desai u_int32_t ventura_map_sz = 0;
3687af51c29fSKashyap D Desai sc->max256vdSupport =
3688af51c29fSKashyap D Desai sc->ctrl_info->adapterOperations3.supportMaxExtLDs;
36894ad83576SKashyap D Desai
3690af51c29fSKashyap D Desai /* Below is additional check to address future FW enhancement */
3691af51c29fSKashyap D Desai if (sc->ctrl_info->max_lds > 64)
3692af51c29fSKashyap D Desai sc->max256vdSupport = 1;
3693af51c29fSKashyap D Desai
3694af51c29fSKashyap D Desai sc->drv_supported_vd_count = MRSAS_MAX_LD_CHANNELS
3695af51c29fSKashyap D Desai * MRSAS_MAX_DEV_PER_CHANNEL;
3696af51c29fSKashyap D Desai sc->drv_supported_pd_count = MRSAS_MAX_PD_CHANNELS
3697af51c29fSKashyap D Desai * MRSAS_MAX_DEV_PER_CHANNEL;
3698af51c29fSKashyap D Desai if (sc->max256vdSupport) {
3699af51c29fSKashyap D Desai sc->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
3700af51c29fSKashyap D Desai sc->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
3701af51c29fSKashyap D Desai } else {
3702af51c29fSKashyap D Desai sc->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
3703af51c29fSKashyap D Desai sc->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
3704af51c29fSKashyap D Desai }
3705af51c29fSKashyap D Desai
37064ad83576SKashyap D Desai if (sc->maxRaidMapSize) {
37074ad83576SKashyap D Desai ventura_map_sz = sc->maxRaidMapSize *
37084ad83576SKashyap D Desai MR_MIN_MAP_SIZE;
37094ad83576SKashyap D Desai sc->current_map_sz = ventura_map_sz;
37104ad83576SKashyap D Desai sc->max_map_sz = ventura_map_sz;
37114ad83576SKashyap D Desai } else {
3712af51c29fSKashyap D Desai sc->old_map_sz = sizeof(MR_FW_RAID_MAP) +
37134ad83576SKashyap D Desai (sizeof(MR_LD_SPAN_MAP) * (sc->fw_supported_vd_count - 1));
3714af51c29fSKashyap D Desai sc->new_map_sz = sizeof(MR_FW_RAID_MAP_EXT);
3715af51c29fSKashyap D Desai sc->max_map_sz = max(sc->old_map_sz, sc->new_map_sz);
3716af51c29fSKashyap D Desai if (sc->max256vdSupport)
3717af51c29fSKashyap D Desai sc->current_map_sz = sc->new_map_sz;
3718af51c29fSKashyap D Desai else
3719af51c29fSKashyap D Desai sc->current_map_sz = sc->old_map_sz;
3720af51c29fSKashyap D Desai }
3721af51c29fSKashyap D Desai
37224ad83576SKashyap D Desai sc->drv_map_sz = sizeof(MR_DRV_RAID_MAP_ALL);
37234ad83576SKashyap D Desai #if VD_EXT_DEBUG
37244ad83576SKashyap D Desai device_printf(sc->mrsas_dev, "sc->maxRaidMapSize 0x%x \n",
37254ad83576SKashyap D Desai sc->maxRaidMapSize);
37264ad83576SKashyap D Desai device_printf(sc->mrsas_dev,
37274ad83576SKashyap D Desai "new_map_sz = 0x%x, old_map_sz = 0x%x, "
37284ad83576SKashyap D Desai "ventura_map_sz = 0x%x, current_map_sz = 0x%x "
37294ad83576SKashyap D Desai "fusion->drv_map_sz =0x%x, size of driver raid map 0x%lx \n",
37304ad83576SKashyap D Desai sc->new_map_sz, sc->old_map_sz, ventura_map_sz,
37314ad83576SKashyap D Desai sc->current_map_sz, sc->drv_map_sz, sizeof(MR_DRV_RAID_MAP_ALL));
37324ad83576SKashyap D Desai #endif
37334ad83576SKashyap D Desai }
37344ad83576SKashyap D Desai
3735af51c29fSKashyap D Desai /*
3736665484d8SDoug Ambrisko * mrsas_alloc_ctlr_info_cmd: Allocates memory for controller info command
3737665484d8SDoug Ambrisko * input: Adapter soft state
3738665484d8SDoug Ambrisko *
3739665484d8SDoug Ambrisko * Allocates DMAable memory for the controller info internal command.
3740665484d8SDoug Ambrisko */
37418e727371SKashyap D Desai int
mrsas_alloc_ctlr_info_cmd(struct mrsas_softc * sc)37428e727371SKashyap D Desai mrsas_alloc_ctlr_info_cmd(struct mrsas_softc *sc)
3743665484d8SDoug Ambrisko {
3744665484d8SDoug Ambrisko int ctlr_info_size;
3745665484d8SDoug Ambrisko
3746665484d8SDoug Ambrisko /* Allocate get controller info command */
3747665484d8SDoug Ambrisko ctlr_info_size = sizeof(struct mrsas_ctrl_info);
37488e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
37498e727371SKashyap D Desai 1, 0,
37508e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
37518e727371SKashyap D Desai BUS_SPACE_MAXADDR,
37528e727371SKashyap D Desai NULL, NULL,
37538e727371SKashyap D Desai ctlr_info_size,
37548e727371SKashyap D Desai 1,
37558e727371SKashyap D Desai ctlr_info_size,
37568e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
37578e727371SKashyap D Desai NULL, NULL,
3758665484d8SDoug Ambrisko &sc->ctlr_info_tag)) {
3759665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate ctlr info tag\n");
3760665484d8SDoug Ambrisko return (ENOMEM);
3761665484d8SDoug Ambrisko }
3762665484d8SDoug Ambrisko if (bus_dmamem_alloc(sc->ctlr_info_tag, (void **)&sc->ctlr_info_mem,
3763665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &sc->ctlr_info_dmamap)) {
3764665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate ctlr info cmd mem\n");
3765665484d8SDoug Ambrisko return (ENOMEM);
3766665484d8SDoug Ambrisko }
3767665484d8SDoug Ambrisko if (bus_dmamap_load(sc->ctlr_info_tag, sc->ctlr_info_dmamap,
3768665484d8SDoug Ambrisko sc->ctlr_info_mem, ctlr_info_size, mrsas_addr_cb,
3769665484d8SDoug Ambrisko &sc->ctlr_info_phys_addr, BUS_DMA_NOWAIT)) {
3770665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load ctlr info cmd mem\n");
3771665484d8SDoug Ambrisko return (ENOMEM);
3772665484d8SDoug Ambrisko }
3773665484d8SDoug Ambrisko memset(sc->ctlr_info_mem, 0, ctlr_info_size);
3774665484d8SDoug Ambrisko return (0);
3775665484d8SDoug Ambrisko }
3776665484d8SDoug Ambrisko
37778e727371SKashyap D Desai /*
3778665484d8SDoug Ambrisko * mrsas_free_ctlr_info_cmd: Free memory for controller info command
3779665484d8SDoug Ambrisko * input: Adapter soft state
3780665484d8SDoug Ambrisko *
3781665484d8SDoug Ambrisko * Deallocates memory of the get controller info cmd.
3782665484d8SDoug Ambrisko */
37838e727371SKashyap D Desai void
mrsas_free_ctlr_info_cmd(struct mrsas_softc * sc)37848e727371SKashyap D Desai mrsas_free_ctlr_info_cmd(struct mrsas_softc *sc)
3785665484d8SDoug Ambrisko {
3786665484d8SDoug Ambrisko if (sc->ctlr_info_phys_addr)
3787665484d8SDoug Ambrisko bus_dmamap_unload(sc->ctlr_info_tag, sc->ctlr_info_dmamap);
3788665484d8SDoug Ambrisko if (sc->ctlr_info_mem != NULL)
3789665484d8SDoug Ambrisko bus_dmamem_free(sc->ctlr_info_tag, sc->ctlr_info_mem, sc->ctlr_info_dmamap);
3790665484d8SDoug Ambrisko if (sc->ctlr_info_tag != NULL)
3791665484d8SDoug Ambrisko bus_dma_tag_destroy(sc->ctlr_info_tag);
3792665484d8SDoug Ambrisko }
3793665484d8SDoug Ambrisko
37948e727371SKashyap D Desai /*
3795665484d8SDoug Ambrisko * mrsas_issue_polled: Issues a polling command
3796665484d8SDoug Ambrisko * inputs: Adapter soft state
3797665484d8SDoug Ambrisko * Command packet to be issued
3798665484d8SDoug Ambrisko *
37998e727371SKashyap D Desai * This function is for posting of internal commands to Firmware. MFI requires
38008e727371SKashyap D Desai * the cmd_status to be set to 0xFF before posting. The maximun wait time of
38018e727371SKashyap D Desai * the poll response timer is 180 seconds.
3802665484d8SDoug Ambrisko */
38038e727371SKashyap D Desai int
mrsas_issue_polled(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)38048e727371SKashyap D Desai mrsas_issue_polled(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
3805665484d8SDoug Ambrisko {
3806665484d8SDoug Ambrisko struct mrsas_header *frame_hdr = &cmd->frame->hdr;
3807665484d8SDoug Ambrisko u_int8_t max_wait = MRSAS_INTERNAL_CMD_WAIT_TIME;
3808f0c7594bSKashyap D Desai int i, retcode = SUCCESS;
3809665484d8SDoug Ambrisko
3810665484d8SDoug Ambrisko frame_hdr->cmd_status = 0xFF;
3811e34a057cSAlfredo Dal'Ava Junior frame_hdr->flags |= htole16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
3812665484d8SDoug Ambrisko
3813665484d8SDoug Ambrisko /* Issue the frame using inbound queue port */
3814665484d8SDoug Ambrisko if (mrsas_issue_dcmd(sc, cmd)) {
3815665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot issue DCMD internal command.\n");
3816665484d8SDoug Ambrisko return (1);
3817665484d8SDoug Ambrisko }
3818665484d8SDoug Ambrisko /*
3819665484d8SDoug Ambrisko * Poll response timer to wait for Firmware response. While this
3820665484d8SDoug Ambrisko * timer with the DELAY call could block CPU, the time interval for
3821665484d8SDoug Ambrisko * this is only 1 millisecond.
3822665484d8SDoug Ambrisko */
3823665484d8SDoug Ambrisko if (frame_hdr->cmd_status == 0xFF) {
3824665484d8SDoug Ambrisko for (i = 0; i < (max_wait * 1000); i++) {
3825665484d8SDoug Ambrisko if (frame_hdr->cmd_status == 0xFF)
3826665484d8SDoug Ambrisko DELAY(1000);
3827665484d8SDoug Ambrisko else
3828665484d8SDoug Ambrisko break;
3829665484d8SDoug Ambrisko }
3830665484d8SDoug Ambrisko }
3831f0c7594bSKashyap D Desai if (frame_hdr->cmd_status == 0xFF) {
3832f0c7594bSKashyap D Desai device_printf(sc->mrsas_dev, "DCMD timed out after %d "
3833f0c7594bSKashyap D Desai "seconds from %s\n", max_wait, __func__);
3834f0c7594bSKashyap D Desai device_printf(sc->mrsas_dev, "DCMD opcode 0x%X\n",
3835f0c7594bSKashyap D Desai cmd->frame->dcmd.opcode);
3836f0c7594bSKashyap D Desai retcode = ETIMEDOUT;
3837665484d8SDoug Ambrisko }
3838665484d8SDoug Ambrisko return (retcode);
3839665484d8SDoug Ambrisko }
3840665484d8SDoug Ambrisko
38418e727371SKashyap D Desai /*
38428e727371SKashyap D Desai * mrsas_issue_dcmd: Issues a MFI Pass thru cmd
38438e727371SKashyap D Desai * input: Adapter soft state mfi cmd pointer
3844665484d8SDoug Ambrisko *
3845665484d8SDoug Ambrisko * This function is called by mrsas_issued_blocked_cmd() and
38468e727371SKashyap D Desai * mrsas_issued_polled(), to build the MPT command and then fire the command
38478e727371SKashyap D Desai * to Firmware.
3848665484d8SDoug Ambrisko */
3849665484d8SDoug Ambrisko int
mrsas_issue_dcmd(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)3850665484d8SDoug Ambrisko mrsas_issue_dcmd(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
3851665484d8SDoug Ambrisko {
3852665484d8SDoug Ambrisko MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3853665484d8SDoug Ambrisko
3854665484d8SDoug Ambrisko req_desc = mrsas_build_mpt_cmd(sc, cmd);
3855665484d8SDoug Ambrisko if (!req_desc) {
3856665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot build MPT cmd.\n");
3857665484d8SDoug Ambrisko return (1);
3858665484d8SDoug Ambrisko }
3859665484d8SDoug Ambrisko mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
3860665484d8SDoug Ambrisko
3861665484d8SDoug Ambrisko return (0);
3862665484d8SDoug Ambrisko }
3863665484d8SDoug Ambrisko
38648e727371SKashyap D Desai /*
38658e727371SKashyap D Desai * mrsas_build_mpt_cmd: Calls helper function to build Passthru cmd
38668e727371SKashyap D Desai * input: Adapter soft state mfi cmd to build
3867665484d8SDoug Ambrisko *
38688e727371SKashyap D Desai * This function is called by mrsas_issue_cmd() to build the MPT-MFI passthru
38698e727371SKashyap D Desai * command and prepares the MPT command to send to Firmware.
3870665484d8SDoug Ambrisko */
3871665484d8SDoug Ambrisko MRSAS_REQUEST_DESCRIPTOR_UNION *
mrsas_build_mpt_cmd(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)3872665484d8SDoug Ambrisko mrsas_build_mpt_cmd(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
3873665484d8SDoug Ambrisko {
3874665484d8SDoug Ambrisko MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3875665484d8SDoug Ambrisko u_int16_t index;
3876665484d8SDoug Ambrisko
3877665484d8SDoug Ambrisko if (mrsas_build_mptmfi_passthru(sc, cmd)) {
3878665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot build MPT-MFI passthru cmd.\n");
3879665484d8SDoug Ambrisko return NULL;
3880665484d8SDoug Ambrisko }
3881665484d8SDoug Ambrisko index = cmd->cmd_id.context.smid;
3882665484d8SDoug Ambrisko
3883665484d8SDoug Ambrisko req_desc = mrsas_get_request_desc(sc, index - 1);
3884665484d8SDoug Ambrisko if (!req_desc)
3885665484d8SDoug Ambrisko return NULL;
3886665484d8SDoug Ambrisko
3887665484d8SDoug Ambrisko req_desc->addr.Words = 0;
3888665484d8SDoug Ambrisko req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3889665484d8SDoug Ambrisko
3890e34a057cSAlfredo Dal'Ava Junior req_desc->SCSIIO.SMID = htole16(index);
3891665484d8SDoug Ambrisko
3892665484d8SDoug Ambrisko return (req_desc);
3893665484d8SDoug Ambrisko }
3894665484d8SDoug Ambrisko
38958e727371SKashyap D Desai /*
38968e727371SKashyap D Desai * mrsas_build_mptmfi_passthru: Builds a MPT MFI Passthru command
38978e727371SKashyap D Desai * input: Adapter soft state mfi cmd pointer
3898665484d8SDoug Ambrisko *
38998e727371SKashyap D Desai * The MPT command and the io_request are setup as a passthru command. The SGE
39008e727371SKashyap D Desai * chain address is set to frame_phys_addr of the MFI command.
3901665484d8SDoug Ambrisko */
3902665484d8SDoug Ambrisko u_int8_t
mrsas_build_mptmfi_passthru(struct mrsas_softc * sc,struct mrsas_mfi_cmd * mfi_cmd)3903665484d8SDoug Ambrisko mrsas_build_mptmfi_passthru(struct mrsas_softc *sc, struct mrsas_mfi_cmd *mfi_cmd)
3904665484d8SDoug Ambrisko {
3905665484d8SDoug Ambrisko MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3906665484d8SDoug Ambrisko PTR_MRSAS_RAID_SCSI_IO_REQUEST io_req;
3907665484d8SDoug Ambrisko struct mrsas_mpt_cmd *mpt_cmd;
3908665484d8SDoug Ambrisko struct mrsas_header *frame_hdr = &mfi_cmd->frame->hdr;
3909665484d8SDoug Ambrisko
3910665484d8SDoug Ambrisko mpt_cmd = mrsas_get_mpt_cmd(sc);
3911665484d8SDoug Ambrisko if (!mpt_cmd)
3912665484d8SDoug Ambrisko return (1);
3913665484d8SDoug Ambrisko
3914665484d8SDoug Ambrisko /* Save the smid. To be used for returning the cmd */
3915665484d8SDoug Ambrisko mfi_cmd->cmd_id.context.smid = mpt_cmd->index;
3916665484d8SDoug Ambrisko
3917665484d8SDoug Ambrisko mpt_cmd->sync_cmd_idx = mfi_cmd->index;
3918665484d8SDoug Ambrisko
3919665484d8SDoug Ambrisko /*
39208e727371SKashyap D Desai * For cmds where the flag is set, store the flag and check on
39218e727371SKashyap D Desai * completion. For cmds with this flag, don't call
3922665484d8SDoug Ambrisko * mrsas_complete_cmd.
3923665484d8SDoug Ambrisko */
3924665484d8SDoug Ambrisko
3925e34a057cSAlfredo Dal'Ava Junior if (frame_hdr->flags & htole16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3926665484d8SDoug Ambrisko mpt_cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
3927665484d8SDoug Ambrisko
3928665484d8SDoug Ambrisko io_req = mpt_cmd->io_request;
3929665484d8SDoug Ambrisko
39302909aab4SKashyap D Desai if (sc->mrsas_gen3_ctrl || sc->is_ventura || sc->is_aero) {
3931665484d8SDoug Ambrisko pMpi25IeeeSgeChain64_t sgl_ptr_end = (pMpi25IeeeSgeChain64_t)&io_req->SGL;
39328e727371SKashyap D Desai
3933665484d8SDoug Ambrisko sgl_ptr_end += sc->max_sge_in_main_msg - 1;
3934665484d8SDoug Ambrisko sgl_ptr_end->Flags = 0;
3935665484d8SDoug Ambrisko }
3936665484d8SDoug Ambrisko mpi25_ieee_chain = (MPI25_IEEE_SGE_CHAIN64 *) & io_req->SGL.IeeeChain;
3937665484d8SDoug Ambrisko
3938665484d8SDoug Ambrisko io_req->Function = MRSAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3939665484d8SDoug Ambrisko io_req->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 4;
3940665484d8SDoug Ambrisko io_req->ChainOffset = sc->chain_offset_mfi_pthru;
3941665484d8SDoug Ambrisko
3942e34a057cSAlfredo Dal'Ava Junior mpi25_ieee_chain->Address = htole64(mfi_cmd->frame_phys_addr);
3943665484d8SDoug Ambrisko
3944665484d8SDoug Ambrisko mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3945665484d8SDoug Ambrisko MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3946665484d8SDoug Ambrisko
3947e34a057cSAlfredo Dal'Ava Junior mpi25_ieee_chain->Length = htole32(sc->max_chain_frame_sz);
3948665484d8SDoug Ambrisko
3949665484d8SDoug Ambrisko return (0);
3950665484d8SDoug Ambrisko }
3951665484d8SDoug Ambrisko
39528e727371SKashyap D Desai /*
39538e727371SKashyap D Desai * mrsas_issue_blocked_cmd: Synchronous wrapper around regular FW cmds
39548e727371SKashyap D Desai * input: Adapter soft state Command to be issued
3955665484d8SDoug Ambrisko *
39568e727371SKashyap D Desai * This function waits on an event for the command to be returned from the ISR.
39578e727371SKashyap D Desai * Max wait time is MRSAS_INTERNAL_CMD_WAIT_TIME secs. Used for issuing
39588e727371SKashyap D Desai * internal and ioctl commands.
3959665484d8SDoug Ambrisko */
39608e727371SKashyap D Desai int
mrsas_issue_blocked_cmd(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)39618e727371SKashyap D Desai mrsas_issue_blocked_cmd(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
3962665484d8SDoug Ambrisko {
3963665484d8SDoug Ambrisko u_int8_t max_wait = MRSAS_INTERNAL_CMD_WAIT_TIME;
3964665484d8SDoug Ambrisko unsigned long total_time = 0;
3965f0c7594bSKashyap D Desai int retcode = SUCCESS;
3966665484d8SDoug Ambrisko
3967665484d8SDoug Ambrisko /* Initialize cmd_status */
3968f0c7594bSKashyap D Desai cmd->cmd_status = 0xFF;
3969665484d8SDoug Ambrisko
3970665484d8SDoug Ambrisko /* Build MPT-MFI command for issue to FW */
3971665484d8SDoug Ambrisko if (mrsas_issue_dcmd(sc, cmd)) {
3972665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot issue DCMD internal command.\n");
3973665484d8SDoug Ambrisko return (1);
3974665484d8SDoug Ambrisko }
3975665484d8SDoug Ambrisko sc->chan = (void *)&cmd;
3976665484d8SDoug Ambrisko
3977665484d8SDoug Ambrisko while (1) {
3978f0c7594bSKashyap D Desai if (cmd->cmd_status == 0xFF) {
3979665484d8SDoug Ambrisko tsleep((void *)&sc->chan, 0, "mrsas_sleep", hz);
39808e727371SKashyap D Desai } else
3981665484d8SDoug Ambrisko break;
3982f0c7594bSKashyap D Desai
3983f0c7594bSKashyap D Desai if (!cmd->sync_cmd) { /* cmd->sync will be set for an IOCTL
3984f0c7594bSKashyap D Desai * command */
3985665484d8SDoug Ambrisko total_time++;
3986665484d8SDoug Ambrisko if (total_time >= max_wait) {
39878e727371SKashyap D Desai device_printf(sc->mrsas_dev,
39888e727371SKashyap D Desai "Internal command timed out after %d seconds.\n", max_wait);
3989665484d8SDoug Ambrisko retcode = 1;
3990665484d8SDoug Ambrisko break;
3991665484d8SDoug Ambrisko }
3992665484d8SDoug Ambrisko }
3993f0c7594bSKashyap D Desai }
39949f0c0e6eSJohn Baldwin sc->chan = NULL;
3995f0c7594bSKashyap D Desai
3996f0c7594bSKashyap D Desai if (cmd->cmd_status == 0xFF) {
3997f0c7594bSKashyap D Desai device_printf(sc->mrsas_dev, "DCMD timed out after %d "
3998f0c7594bSKashyap D Desai "seconds from %s\n", max_wait, __func__);
3999f0c7594bSKashyap D Desai device_printf(sc->mrsas_dev, "DCMD opcode 0x%X\n",
4000f0c7594bSKashyap D Desai cmd->frame->dcmd.opcode);
4001f0c7594bSKashyap D Desai retcode = ETIMEDOUT;
4002f0c7594bSKashyap D Desai }
4003665484d8SDoug Ambrisko return (retcode);
4004665484d8SDoug Ambrisko }
4005665484d8SDoug Ambrisko
40068e727371SKashyap D Desai /*
40078e727371SKashyap D Desai * mrsas_complete_mptmfi_passthru: Completes a command
40088e727371SKashyap D Desai * input: @sc: Adapter soft state
40098e727371SKashyap D Desai * @cmd: Command to be completed
40108e727371SKashyap D Desai * @status: cmd completion status
4011665484d8SDoug Ambrisko *
40128e727371SKashyap D Desai * This function is called from mrsas_complete_cmd() after an interrupt is
40138e727371SKashyap D Desai * received from Firmware, and io_request->Function is
4014665484d8SDoug Ambrisko * MRSAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST.
4015665484d8SDoug Ambrisko */
4016665484d8SDoug Ambrisko void
mrsas_complete_mptmfi_passthru(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd,u_int8_t status)4017665484d8SDoug Ambrisko mrsas_complete_mptmfi_passthru(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd,
4018665484d8SDoug Ambrisko u_int8_t status)
4019665484d8SDoug Ambrisko {
4020665484d8SDoug Ambrisko struct mrsas_header *hdr = &cmd->frame->hdr;
4021665484d8SDoug Ambrisko u_int8_t cmd_status = cmd->frame->hdr.cmd_status;
4022665484d8SDoug Ambrisko
4023665484d8SDoug Ambrisko /* Reset the retry counter for future re-tries */
4024665484d8SDoug Ambrisko cmd->retry_for_fw_reset = 0;
4025665484d8SDoug Ambrisko
4026665484d8SDoug Ambrisko if (cmd->ccb_ptr)
4027665484d8SDoug Ambrisko cmd->ccb_ptr = NULL;
4028665484d8SDoug Ambrisko
4029665484d8SDoug Ambrisko switch (hdr->cmd) {
4030665484d8SDoug Ambrisko case MFI_CMD_INVALID:
4031665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "MFI_CMD_INVALID command.\n");
4032665484d8SDoug Ambrisko break;
4033665484d8SDoug Ambrisko case MFI_CMD_PD_SCSI_IO:
4034665484d8SDoug Ambrisko case MFI_CMD_LD_SCSI_IO:
4035665484d8SDoug Ambrisko /*
4036665484d8SDoug Ambrisko * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
4037665484d8SDoug Ambrisko * issued either through an IO path or an IOCTL path. If it
4038665484d8SDoug Ambrisko * was via IOCTL, we will send it to internal completion.
4039665484d8SDoug Ambrisko */
4040665484d8SDoug Ambrisko if (cmd->sync_cmd) {
4041665484d8SDoug Ambrisko cmd->sync_cmd = 0;
4042665484d8SDoug Ambrisko mrsas_wakeup(sc, cmd);
4043665484d8SDoug Ambrisko break;
4044665484d8SDoug Ambrisko }
4045665484d8SDoug Ambrisko case MFI_CMD_SMP:
4046665484d8SDoug Ambrisko case MFI_CMD_STP:
4047665484d8SDoug Ambrisko case MFI_CMD_DCMD:
4048665484d8SDoug Ambrisko /* Check for LD map update */
4049665484d8SDoug Ambrisko if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
4050665484d8SDoug Ambrisko (cmd->frame->dcmd.mbox.b[1] == 1)) {
4051665484d8SDoug Ambrisko sc->fast_path_io = 0;
4052665484d8SDoug Ambrisko mtx_lock(&sc->raidmap_lock);
4053f0c7594bSKashyap D Desai sc->map_update_cmd = NULL;
4054665484d8SDoug Ambrisko if (cmd_status != 0) {
4055665484d8SDoug Ambrisko if (cmd_status != MFI_STAT_NOT_FOUND)
4056665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "map sync failed, status=%x\n", cmd_status);
4057665484d8SDoug Ambrisko else {
4058665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4059665484d8SDoug Ambrisko mtx_unlock(&sc->raidmap_lock);
4060665484d8SDoug Ambrisko break;
4061665484d8SDoug Ambrisko }
40628e727371SKashyap D Desai } else
4063665484d8SDoug Ambrisko sc->map_id++;
4064665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4065665484d8SDoug Ambrisko if (MR_ValidateMapInfo(sc))
4066665484d8SDoug Ambrisko sc->fast_path_io = 0;
4067665484d8SDoug Ambrisko else
4068665484d8SDoug Ambrisko sc->fast_path_io = 1;
4069665484d8SDoug Ambrisko mrsas_sync_map_info(sc);
4070665484d8SDoug Ambrisko mtx_unlock(&sc->raidmap_lock);
4071665484d8SDoug Ambrisko break;
4072665484d8SDoug Ambrisko }
4073665484d8SDoug Ambrisko if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
4074665484d8SDoug Ambrisko cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
4075da011113SKashyap D Desai sc->mrsas_aen_triggered = 0;
4076665484d8SDoug Ambrisko }
4077a688fcd0SKashyap D Desai /* FW has an updated PD sequence */
4078a688fcd0SKashyap D Desai if ((cmd->frame->dcmd.opcode ==
4079a688fcd0SKashyap D Desai MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4080a688fcd0SKashyap D Desai (cmd->frame->dcmd.mbox.b[0] == 1)) {
4081a688fcd0SKashyap D Desai mtx_lock(&sc->raidmap_lock);
4082a688fcd0SKashyap D Desai sc->jbod_seq_cmd = NULL;
4083a688fcd0SKashyap D Desai mrsas_release_mfi_cmd(cmd);
4084a688fcd0SKashyap D Desai
4085a688fcd0SKashyap D Desai if (cmd_status == MFI_STAT_OK) {
4086a688fcd0SKashyap D Desai sc->pd_seq_map_id++;
4087a688fcd0SKashyap D Desai /* Re-register a pd sync seq num cmd */
4088a688fcd0SKashyap D Desai if (megasas_sync_pd_seq_num(sc, true))
4089a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 0;
4090a688fcd0SKashyap D Desai } else {
4091a688fcd0SKashyap D Desai sc->use_seqnum_jbod_fp = 0;
4092a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
4093a688fcd0SKashyap D Desai "Jbod map sync failed, status=%x\n", cmd_status);
4094a688fcd0SKashyap D Desai }
4095a688fcd0SKashyap D Desai mtx_unlock(&sc->raidmap_lock);
4096a688fcd0SKashyap D Desai break;
4097a688fcd0SKashyap D Desai }
4098665484d8SDoug Ambrisko /* See if got an event notification */
4099e34a057cSAlfredo Dal'Ava Junior if (le32toh(cmd->frame->dcmd.opcode) == MR_DCMD_CTRL_EVENT_WAIT)
4100665484d8SDoug Ambrisko mrsas_complete_aen(sc, cmd);
4101665484d8SDoug Ambrisko else
4102665484d8SDoug Ambrisko mrsas_wakeup(sc, cmd);
4103665484d8SDoug Ambrisko break;
4104665484d8SDoug Ambrisko case MFI_CMD_ABORT:
4105665484d8SDoug Ambrisko /* Command issued to abort another cmd return */
4106665484d8SDoug Ambrisko mrsas_complete_abort(sc, cmd);
4107665484d8SDoug Ambrisko break;
4108665484d8SDoug Ambrisko default:
4109665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Unknown command completed! [0x%X]\n", hdr->cmd);
4110665484d8SDoug Ambrisko break;
4111665484d8SDoug Ambrisko }
4112665484d8SDoug Ambrisko }
4113665484d8SDoug Ambrisko
41148e727371SKashyap D Desai /*
41158e727371SKashyap D Desai * mrsas_wakeup: Completes an internal command
4116665484d8SDoug Ambrisko * input: Adapter soft state
4117665484d8SDoug Ambrisko * Command to be completed
4118665484d8SDoug Ambrisko *
41198e727371SKashyap D Desai * In mrsas_issue_blocked_cmd(), after a command is issued to Firmware, a wait
41208e727371SKashyap D Desai * timer is started. This function is called from
41218e727371SKashyap D Desai * mrsas_complete_mptmfi_passthru() as it completes the command, to wake up
41228e727371SKashyap D Desai * from the command wait.
4123665484d8SDoug Ambrisko */
41248e727371SKashyap D Desai void
mrsas_wakeup(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)41258e727371SKashyap D Desai mrsas_wakeup(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
4126665484d8SDoug Ambrisko {
4127665484d8SDoug Ambrisko cmd->cmd_status = cmd->frame->io.cmd_status;
4128665484d8SDoug Ambrisko
4129f0c7594bSKashyap D Desai if (cmd->cmd_status == 0xFF)
4130665484d8SDoug Ambrisko cmd->cmd_status = 0;
4131665484d8SDoug Ambrisko
4132665484d8SDoug Ambrisko sc->chan = (void *)&cmd;
4133665484d8SDoug Ambrisko wakeup_one((void *)&sc->chan);
4134665484d8SDoug Ambrisko return;
4135665484d8SDoug Ambrisko }
4136665484d8SDoug Ambrisko
41378e727371SKashyap D Desai /*
41388e727371SKashyap D Desai * mrsas_shutdown_ctlr: Instructs FW to shutdown the controller input:
41398e727371SKashyap D Desai * Adapter soft state Shutdown/Hibernate
4140665484d8SDoug Ambrisko *
41418e727371SKashyap D Desai * This function issues a DCMD internal command to Firmware to initiate shutdown
41428e727371SKashyap D Desai * of the controller.
4143665484d8SDoug Ambrisko */
41448e727371SKashyap D Desai static void
mrsas_shutdown_ctlr(struct mrsas_softc * sc,u_int32_t opcode)41458e727371SKashyap D Desai mrsas_shutdown_ctlr(struct mrsas_softc *sc, u_int32_t opcode)
4146665484d8SDoug Ambrisko {
4147665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4148665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
4149665484d8SDoug Ambrisko
4150665484d8SDoug Ambrisko if (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR)
4151665484d8SDoug Ambrisko return;
4152665484d8SDoug Ambrisko
4153665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4154665484d8SDoug Ambrisko if (!cmd) {
4155665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate for shutdown cmd.\n");
4156665484d8SDoug Ambrisko return;
4157665484d8SDoug Ambrisko }
4158665484d8SDoug Ambrisko if (sc->aen_cmd)
4159665484d8SDoug Ambrisko mrsas_issue_blocked_abort_cmd(sc, sc->aen_cmd);
4160665484d8SDoug Ambrisko if (sc->map_update_cmd)
4161665484d8SDoug Ambrisko mrsas_issue_blocked_abort_cmd(sc, sc->map_update_cmd);
4162a688fcd0SKashyap D Desai if (sc->jbod_seq_cmd)
4163a688fcd0SKashyap D Desai mrsas_issue_blocked_abort_cmd(sc, sc->jbod_seq_cmd);
4164665484d8SDoug Ambrisko
4165665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4166665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4167665484d8SDoug Ambrisko
4168665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4169665484d8SDoug Ambrisko dcmd->cmd_status = 0x0;
4170665484d8SDoug Ambrisko dcmd->sge_count = 0;
4171665484d8SDoug Ambrisko dcmd->flags = MFI_FRAME_DIR_NONE;
4172665484d8SDoug Ambrisko dcmd->timeout = 0;
4173665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4174665484d8SDoug Ambrisko dcmd->data_xfer_len = 0;
4175665484d8SDoug Ambrisko dcmd->opcode = opcode;
4176665484d8SDoug Ambrisko
4177665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Preparing to shut down controller.\n");
4178665484d8SDoug Ambrisko
4179665484d8SDoug Ambrisko mrsas_issue_blocked_cmd(sc, cmd);
4180665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4181665484d8SDoug Ambrisko
4182665484d8SDoug Ambrisko return;
4183665484d8SDoug Ambrisko }
4184665484d8SDoug Ambrisko
41858e727371SKashyap D Desai /*
41868e727371SKashyap D Desai * mrsas_flush_cache: Requests FW to flush all its caches input:
41878e727371SKashyap D Desai * Adapter soft state
4188665484d8SDoug Ambrisko *
4189665484d8SDoug Ambrisko * This function is issues a DCMD internal command to Firmware to initiate
4190665484d8SDoug Ambrisko * flushing of all caches.
4191665484d8SDoug Ambrisko */
41928e727371SKashyap D Desai static void
mrsas_flush_cache(struct mrsas_softc * sc)41938e727371SKashyap D Desai mrsas_flush_cache(struct mrsas_softc *sc)
4194665484d8SDoug Ambrisko {
4195665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4196665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
4197665484d8SDoug Ambrisko
4198665484d8SDoug Ambrisko if (sc->adprecovery == MRSAS_HW_CRITICAL_ERROR)
4199665484d8SDoug Ambrisko return;
4200665484d8SDoug Ambrisko
4201665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4202665484d8SDoug Ambrisko if (!cmd) {
4203665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate for flush cache cmd.\n");
4204665484d8SDoug Ambrisko return;
4205665484d8SDoug Ambrisko }
4206665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4207665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4208665484d8SDoug Ambrisko
4209665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4210665484d8SDoug Ambrisko dcmd->cmd_status = 0x0;
4211665484d8SDoug Ambrisko dcmd->sge_count = 0;
4212665484d8SDoug Ambrisko dcmd->flags = MFI_FRAME_DIR_NONE;
4213665484d8SDoug Ambrisko dcmd->timeout = 0;
4214665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4215665484d8SDoug Ambrisko dcmd->data_xfer_len = 0;
4216665484d8SDoug Ambrisko dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4217665484d8SDoug Ambrisko dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4218665484d8SDoug Ambrisko
4219665484d8SDoug Ambrisko mrsas_issue_blocked_cmd(sc, cmd);
4220665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4221665484d8SDoug Ambrisko
4222665484d8SDoug Ambrisko return;
4223665484d8SDoug Ambrisko }
4224665484d8SDoug Ambrisko
4225a688fcd0SKashyap D Desai int
megasas_sync_pd_seq_num(struct mrsas_softc * sc,boolean_t pend)4226a688fcd0SKashyap D Desai megasas_sync_pd_seq_num(struct mrsas_softc *sc, boolean_t pend)
4227a688fcd0SKashyap D Desai {
4228a688fcd0SKashyap D Desai int retcode = 0;
4229a688fcd0SKashyap D Desai u_int8_t do_ocr = 1;
4230a688fcd0SKashyap D Desai struct mrsas_mfi_cmd *cmd;
4231a688fcd0SKashyap D Desai struct mrsas_dcmd_frame *dcmd;
4232a688fcd0SKashyap D Desai uint32_t pd_seq_map_sz;
4233a688fcd0SKashyap D Desai struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4234a688fcd0SKashyap D Desai bus_addr_t pd_seq_h;
4235a688fcd0SKashyap D Desai
4236a688fcd0SKashyap D Desai pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4237a688fcd0SKashyap D Desai (sizeof(struct MR_PD_CFG_SEQ) *
4238a688fcd0SKashyap D Desai (MAX_PHYSICAL_DEVICES - 1));
4239a688fcd0SKashyap D Desai
4240a688fcd0SKashyap D Desai cmd = mrsas_get_mfi_cmd(sc);
4241a688fcd0SKashyap D Desai if (!cmd) {
4242a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
4243a688fcd0SKashyap D Desai "Cannot alloc for ld map info cmd.\n");
4244a688fcd0SKashyap D Desai return 1;
4245a688fcd0SKashyap D Desai }
4246a688fcd0SKashyap D Desai dcmd = &cmd->frame->dcmd;
4247a688fcd0SKashyap D Desai
4248a688fcd0SKashyap D Desai pd_sync = (void *)sc->jbodmap_mem[(sc->pd_seq_map_id & 1)];
4249a688fcd0SKashyap D Desai pd_seq_h = sc->jbodmap_phys_addr[(sc->pd_seq_map_id & 1)];
4250a688fcd0SKashyap D Desai if (!pd_sync) {
4251a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
4252a688fcd0SKashyap D Desai "Failed to alloc mem for jbod map info.\n");
4253a688fcd0SKashyap D Desai mrsas_release_mfi_cmd(cmd);
4254a688fcd0SKashyap D Desai return (ENOMEM);
4255a688fcd0SKashyap D Desai }
4256a688fcd0SKashyap D Desai memset(pd_sync, 0, pd_seq_map_sz);
4257a688fcd0SKashyap D Desai memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4258a688fcd0SKashyap D Desai dcmd->cmd = MFI_CMD_DCMD;
4259a688fcd0SKashyap D Desai dcmd->cmd_status = 0xFF;
4260a688fcd0SKashyap D Desai dcmd->sge_count = 1;
4261a688fcd0SKashyap D Desai dcmd->timeout = 0;
4262a688fcd0SKashyap D Desai dcmd->pad_0 = 0;
4263e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(pd_seq_map_sz);
4264e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
4265e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(pd_seq_h & 0xFFFFFFFF);
4266e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(pd_seq_map_sz);
4267a688fcd0SKashyap D Desai
4268a688fcd0SKashyap D Desai if (pend) {
4269a688fcd0SKashyap D Desai dcmd->mbox.b[0] = MRSAS_DCMD_MBOX_PEND_FLAG;
4270e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_WRITE);
4271a688fcd0SKashyap D Desai sc->jbod_seq_cmd = cmd;
4272a688fcd0SKashyap D Desai if (mrsas_issue_dcmd(sc, cmd)) {
4273a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
4274a688fcd0SKashyap D Desai "Fail to send sync map info command.\n");
4275a688fcd0SKashyap D Desai return 1;
4276a688fcd0SKashyap D Desai } else
4277a688fcd0SKashyap D Desai return 0;
4278a688fcd0SKashyap D Desai } else
4279e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_READ);
4280a688fcd0SKashyap D Desai
4281a688fcd0SKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
4282a688fcd0SKashyap D Desai if (retcode == ETIMEDOUT)
4283a688fcd0SKashyap D Desai goto dcmd_timeout;
4284a688fcd0SKashyap D Desai
4285e34a057cSAlfredo Dal'Ava Junior if (le32toh(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
4286a688fcd0SKashyap D Desai device_printf(sc->mrsas_dev,
4287a688fcd0SKashyap D Desai "driver supports max %d JBOD, but FW reports %d\n",
4288a688fcd0SKashyap D Desai MAX_PHYSICAL_DEVICES, pd_sync->count);
4289a688fcd0SKashyap D Desai retcode = -EINVAL;
4290a688fcd0SKashyap D Desai }
4291a688fcd0SKashyap D Desai if (!retcode)
4292a688fcd0SKashyap D Desai sc->pd_seq_map_id++;
4293a688fcd0SKashyap D Desai do_ocr = 0;
4294a688fcd0SKashyap D Desai
4295a688fcd0SKashyap D Desai dcmd_timeout:
4296a688fcd0SKashyap D Desai if (do_ocr)
4297a688fcd0SKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
4298a688fcd0SKashyap D Desai
4299a688fcd0SKashyap D Desai return (retcode);
4300a688fcd0SKashyap D Desai }
4301a688fcd0SKashyap D Desai
43028e727371SKashyap D Desai /*
43038e727371SKashyap D Desai * mrsas_get_map_info: Load and validate RAID map input:
43048e727371SKashyap D Desai * Adapter instance soft state
4305665484d8SDoug Ambrisko *
43068e727371SKashyap D Desai * This function calls mrsas_get_ld_map_info() and MR_ValidateMapInfo() to load
43078e727371SKashyap D Desai * and validate RAID map. It returns 0 if successful, 1 other- wise.
4308665484d8SDoug Ambrisko */
43098e727371SKashyap D Desai static int
mrsas_get_map_info(struct mrsas_softc * sc)43108e727371SKashyap D Desai mrsas_get_map_info(struct mrsas_softc *sc)
4311665484d8SDoug Ambrisko {
4312665484d8SDoug Ambrisko uint8_t retcode = 0;
4313665484d8SDoug Ambrisko
4314665484d8SDoug Ambrisko sc->fast_path_io = 0;
4315665484d8SDoug Ambrisko if (!mrsas_get_ld_map_info(sc)) {
4316665484d8SDoug Ambrisko retcode = MR_ValidateMapInfo(sc);
4317665484d8SDoug Ambrisko if (retcode == 0) {
4318665484d8SDoug Ambrisko sc->fast_path_io = 1;
4319665484d8SDoug Ambrisko return 0;
4320665484d8SDoug Ambrisko }
4321665484d8SDoug Ambrisko }
4322665484d8SDoug Ambrisko return 1;
4323665484d8SDoug Ambrisko }
4324665484d8SDoug Ambrisko
43258e727371SKashyap D Desai /*
43268e727371SKashyap D Desai * mrsas_get_ld_map_info: Get FW's ld_map structure input:
43278e727371SKashyap D Desai * Adapter instance soft state
4328665484d8SDoug Ambrisko *
43298e727371SKashyap D Desai * Issues an internal command (DCMD) to get the FW's controller PD list
43308e727371SKashyap D Desai * structure.
4331665484d8SDoug Ambrisko */
43328e727371SKashyap D Desai static int
mrsas_get_ld_map_info(struct mrsas_softc * sc)43338e727371SKashyap D Desai mrsas_get_ld_map_info(struct mrsas_softc *sc)
4334665484d8SDoug Ambrisko {
4335665484d8SDoug Ambrisko int retcode = 0;
4336665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4337665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
43384799d485SKashyap D Desai void *map;
4339665484d8SDoug Ambrisko bus_addr_t map_phys_addr = 0;
4340665484d8SDoug Ambrisko
4341665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4342665484d8SDoug Ambrisko if (!cmd) {
43434799d485SKashyap D Desai device_printf(sc->mrsas_dev,
43444799d485SKashyap D Desai "Cannot alloc for ld map info cmd.\n");
4345665484d8SDoug Ambrisko return 1;
4346665484d8SDoug Ambrisko }
4347665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4348665484d8SDoug Ambrisko
43494799d485SKashyap D Desai map = (void *)sc->raidmap_mem[(sc->map_id & 1)];
4350665484d8SDoug Ambrisko map_phys_addr = sc->raidmap_phys_addr[(sc->map_id & 1)];
4351665484d8SDoug Ambrisko if (!map) {
43524799d485SKashyap D Desai device_printf(sc->mrsas_dev,
43534799d485SKashyap D Desai "Failed to alloc mem for ld map info.\n");
4354665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4355665484d8SDoug Ambrisko return (ENOMEM);
4356665484d8SDoug Ambrisko }
43574799d485SKashyap D Desai memset(map, 0, sizeof(sc->max_map_sz));
4358665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4359665484d8SDoug Ambrisko
4360665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4361665484d8SDoug Ambrisko dcmd->cmd_status = 0xFF;
4362665484d8SDoug Ambrisko dcmd->sge_count = 1;
4363e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_READ);
4364665484d8SDoug Ambrisko dcmd->timeout = 0;
4365665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4366e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sc->current_map_sz);
4367e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_LD_MAP_GET_INFO);
4368e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(map_phys_addr & 0xFFFFFFFF);
4369e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sc->current_map_sz);
43704799d485SKashyap D Desai
4371f0c7594bSKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
4372f0c7594bSKashyap D Desai if (retcode == ETIMEDOUT)
4373f0c7594bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
43744799d485SKashyap D Desai
4375665484d8SDoug Ambrisko return (retcode);
4376665484d8SDoug Ambrisko }
4377665484d8SDoug Ambrisko
43788e727371SKashyap D Desai /*
43798e727371SKashyap D Desai * mrsas_sync_map_info: Get FW's ld_map structure input:
43808e727371SKashyap D Desai * Adapter instance soft state
4381665484d8SDoug Ambrisko *
43828e727371SKashyap D Desai * Issues an internal command (DCMD) to get the FW's controller PD list
43838e727371SKashyap D Desai * structure.
4384665484d8SDoug Ambrisko */
43858e727371SKashyap D Desai static int
mrsas_sync_map_info(struct mrsas_softc * sc)43868e727371SKashyap D Desai mrsas_sync_map_info(struct mrsas_softc *sc)
4387665484d8SDoug Ambrisko {
4388665484d8SDoug Ambrisko int retcode = 0, i;
4389665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4390665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
439198470f0eSScott Long uint32_t num_lds;
4392665484d8SDoug Ambrisko MR_LD_TARGET_SYNC *target_map = NULL;
43934799d485SKashyap D Desai MR_DRV_RAID_MAP_ALL *map;
4394665484d8SDoug Ambrisko MR_LD_RAID *raid;
4395665484d8SDoug Ambrisko MR_LD_TARGET_SYNC *ld_sync;
4396665484d8SDoug Ambrisko bus_addr_t map_phys_addr = 0;
4397665484d8SDoug Ambrisko
4398665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4399665484d8SDoug Ambrisko if (!cmd) {
4400731b7561SKashyap D Desai device_printf(sc->mrsas_dev, "Cannot alloc for sync map info cmd\n");
4401731b7561SKashyap D Desai return ENOMEM;
4402665484d8SDoug Ambrisko }
44034799d485SKashyap D Desai map = sc->ld_drv_map[sc->map_id & 1];
4404665484d8SDoug Ambrisko num_lds = map->raidMap.ldCount;
4405665484d8SDoug Ambrisko
4406665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4407665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4408665484d8SDoug Ambrisko
44098e727371SKashyap D Desai target_map = (MR_LD_TARGET_SYNC *) sc->raidmap_mem[(sc->map_id - 1) & 1];
44104799d485SKashyap D Desai memset(target_map, 0, sc->max_map_sz);
4411665484d8SDoug Ambrisko
4412665484d8SDoug Ambrisko map_phys_addr = sc->raidmap_phys_addr[(sc->map_id - 1) & 1];
4413665484d8SDoug Ambrisko
4414665484d8SDoug Ambrisko ld_sync = (MR_LD_TARGET_SYNC *) target_map;
4415665484d8SDoug Ambrisko
4416665484d8SDoug Ambrisko for (i = 0; i < num_lds; i++, ld_sync++) {
4417665484d8SDoug Ambrisko raid = MR_LdRaidGet(i, map);
4418665484d8SDoug Ambrisko ld_sync->targetId = MR_GetLDTgtId(i, map);
4419665484d8SDoug Ambrisko ld_sync->seqNum = raid->seqNum;
4420665484d8SDoug Ambrisko }
4421665484d8SDoug Ambrisko
4422665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4423665484d8SDoug Ambrisko dcmd->cmd_status = 0xFF;
4424665484d8SDoug Ambrisko dcmd->sge_count = 1;
4425e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_WRITE);
4426665484d8SDoug Ambrisko dcmd->timeout = 0;
4427665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4428e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sc->current_map_sz);
4429665484d8SDoug Ambrisko dcmd->mbox.b[0] = num_lds;
4430665484d8SDoug Ambrisko dcmd->mbox.b[1] = MRSAS_DCMD_MBOX_PEND_FLAG;
4431e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_LD_MAP_GET_INFO);
4432e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(map_phys_addr & 0xFFFFFFFF);
4433e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sc->current_map_sz);
4434665484d8SDoug Ambrisko
4435665484d8SDoug Ambrisko sc->map_update_cmd = cmd;
4436665484d8SDoug Ambrisko if (mrsas_issue_dcmd(sc, cmd)) {
44374799d485SKashyap D Desai device_printf(sc->mrsas_dev,
44384799d485SKashyap D Desai "Fail to send sync map info command.\n");
4439665484d8SDoug Ambrisko return (1);
4440665484d8SDoug Ambrisko }
4441665484d8SDoug Ambrisko return (retcode);
4442665484d8SDoug Ambrisko }
4443665484d8SDoug Ambrisko
444479b4460bSKashyap D Desai /* Input: dcmd.opcode - MR_DCMD_PD_GET_INFO
444579b4460bSKashyap D Desai * dcmd.mbox.s[0] - deviceId for this physical drive
444679b4460bSKashyap D Desai * dcmd.sge IN - ptr to returned MR_PD_INFO structure
444779b4460bSKashyap D Desai * Desc: Firmware return the physical drive info structure
444879b4460bSKashyap D Desai *
444979b4460bSKashyap D Desai */
445079b4460bSKashyap D Desai static void
mrsas_get_pd_info(struct mrsas_softc * sc,u_int16_t device_id)445179b4460bSKashyap D Desai mrsas_get_pd_info(struct mrsas_softc *sc, u_int16_t device_id)
445279b4460bSKashyap D Desai {
445379b4460bSKashyap D Desai int retcode;
445479b4460bSKashyap D Desai u_int8_t do_ocr = 1;
445579b4460bSKashyap D Desai struct mrsas_mfi_cmd *cmd;
445679b4460bSKashyap D Desai struct mrsas_dcmd_frame *dcmd;
445779b4460bSKashyap D Desai
445879b4460bSKashyap D Desai cmd = mrsas_get_mfi_cmd(sc);
445979b4460bSKashyap D Desai
446079b4460bSKashyap D Desai if (!cmd) {
446179b4460bSKashyap D Desai device_printf(sc->mrsas_dev,
446279b4460bSKashyap D Desai "Cannot alloc for get PD info cmd\n");
446379b4460bSKashyap D Desai return;
446479b4460bSKashyap D Desai }
446579b4460bSKashyap D Desai dcmd = &cmd->frame->dcmd;
446679b4460bSKashyap D Desai
446779b4460bSKashyap D Desai memset(sc->pd_info_mem, 0, sizeof(struct mrsas_pd_info));
446879b4460bSKashyap D Desai memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
446979b4460bSKashyap D Desai
4470e34a057cSAlfredo Dal'Ava Junior dcmd->mbox.s[0] = htole16(device_id);
447179b4460bSKashyap D Desai dcmd->cmd = MFI_CMD_DCMD;
447279b4460bSKashyap D Desai dcmd->cmd_status = 0xFF;
447379b4460bSKashyap D Desai dcmd->sge_count = 1;
447479b4460bSKashyap D Desai dcmd->flags = MFI_FRAME_DIR_READ;
447579b4460bSKashyap D Desai dcmd->timeout = 0;
447679b4460bSKashyap D Desai dcmd->pad_0 = 0;
4477e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sizeof(struct mrsas_pd_info));
4478e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_PD_GET_INFO);
4479e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32((u_int32_t)sc->pd_info_phys_addr & 0xFFFFFFFF);
4480e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sizeof(struct mrsas_pd_info));
448179b4460bSKashyap D Desai
448279b4460bSKashyap D Desai if (!sc->mask_interrupts)
448379b4460bSKashyap D Desai retcode = mrsas_issue_blocked_cmd(sc, cmd);
448479b4460bSKashyap D Desai else
448579b4460bSKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
448679b4460bSKashyap D Desai
448779b4460bSKashyap D Desai if (retcode == ETIMEDOUT)
448879b4460bSKashyap D Desai goto dcmd_timeout;
448979b4460bSKashyap D Desai
449079b4460bSKashyap D Desai sc->target_list[device_id].interface_type =
4491e34a057cSAlfredo Dal'Ava Junior le16toh(sc->pd_info_mem->state.ddf.pdType.intf);
449279b4460bSKashyap D Desai
449379b4460bSKashyap D Desai do_ocr = 0;
449479b4460bSKashyap D Desai
449579b4460bSKashyap D Desai dcmd_timeout:
449679b4460bSKashyap D Desai
449779b4460bSKashyap D Desai if (do_ocr)
449879b4460bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
449979b4460bSKashyap D Desai
450079b4460bSKashyap D Desai if (!sc->mask_interrupts)
450179b4460bSKashyap D Desai mrsas_release_mfi_cmd(cmd);
450279b4460bSKashyap D Desai }
450379b4460bSKashyap D Desai
450479b4460bSKashyap D Desai /*
450579b4460bSKashyap D Desai * mrsas_add_target: Add target ID of system PD/VD to driver's data structure.
450679b4460bSKashyap D Desai * sc: Adapter's soft state
450779b4460bSKashyap D Desai * target_id: Unique target id per controller(managed by driver)
450879b4460bSKashyap D Desai * for system PDs- target ID ranges from 0 to (MRSAS_MAX_PD - 1)
450979b4460bSKashyap D Desai * for VDs- target ID ranges from MRSAS_MAX_PD to MRSAS_MAX_TM_TARGETS
451079b4460bSKashyap D Desai * return: void
451179b4460bSKashyap D Desai * Descripton: This function will be called whenever system PD or VD is created.
451279b4460bSKashyap D Desai */
mrsas_add_target(struct mrsas_softc * sc,u_int16_t target_id)451379b4460bSKashyap D Desai static void mrsas_add_target(struct mrsas_softc *sc,
451479b4460bSKashyap D Desai u_int16_t target_id)
451579b4460bSKashyap D Desai {
451679b4460bSKashyap D Desai sc->target_list[target_id].target_id = target_id;
451779b4460bSKashyap D Desai
451879b4460bSKashyap D Desai device_printf(sc->mrsas_dev,
451979b4460bSKashyap D Desai "%s created target ID: 0x%x\n",
452079b4460bSKashyap D Desai (target_id < MRSAS_MAX_PD ? "System PD" : "VD"),
452179b4460bSKashyap D Desai (target_id < MRSAS_MAX_PD ? target_id : (target_id - MRSAS_MAX_PD)));
452279b4460bSKashyap D Desai /*
452379b4460bSKashyap D Desai * If interrupts are enabled, then only fire DCMD to get pd_info
452479b4460bSKashyap D Desai * for system PDs
452579b4460bSKashyap D Desai */
452679b4460bSKashyap D Desai if (!sc->mask_interrupts && sc->pd_info_mem &&
452779b4460bSKashyap D Desai (target_id < MRSAS_MAX_PD))
452879b4460bSKashyap D Desai mrsas_get_pd_info(sc, target_id);
452979b4460bSKashyap D Desai
453079b4460bSKashyap D Desai }
453179b4460bSKashyap D Desai
453279b4460bSKashyap D Desai /*
453379b4460bSKashyap D Desai * mrsas_remove_target: Remove target ID of system PD/VD from driver's data structure.
453479b4460bSKashyap D Desai * sc: Adapter's soft state
453579b4460bSKashyap D Desai * target_id: Unique target id per controller(managed by driver)
453679b4460bSKashyap D Desai * for system PDs- target ID ranges from 0 to (MRSAS_MAX_PD - 1)
453779b4460bSKashyap D Desai * for VDs- target ID ranges from MRSAS_MAX_PD to MRSAS_MAX_TM_TARGETS
453879b4460bSKashyap D Desai * return: void
453979b4460bSKashyap D Desai * Descripton: This function will be called whenever system PD or VD is deleted
454079b4460bSKashyap D Desai */
mrsas_remove_target(struct mrsas_softc * sc,u_int16_t target_id)454179b4460bSKashyap D Desai static void mrsas_remove_target(struct mrsas_softc *sc,
454279b4460bSKashyap D Desai u_int16_t target_id)
454379b4460bSKashyap D Desai {
454479b4460bSKashyap D Desai sc->target_list[target_id].target_id = 0xffff;
454579b4460bSKashyap D Desai device_printf(sc->mrsas_dev,
454679b4460bSKashyap D Desai "%s deleted target ID: 0x%x\n",
454779b4460bSKashyap D Desai (target_id < MRSAS_MAX_PD ? "System PD" : "VD"),
454879b4460bSKashyap D Desai (target_id < MRSAS_MAX_PD ? target_id : (target_id - MRSAS_MAX_PD)));
454979b4460bSKashyap D Desai }
455079b4460bSKashyap D Desai
45518e727371SKashyap D Desai /*
45528e727371SKashyap D Desai * mrsas_get_pd_list: Returns FW's PD list structure input:
45538e727371SKashyap D Desai * Adapter soft state
4554665484d8SDoug Ambrisko *
45558e727371SKashyap D Desai * Issues an internal command (DCMD) to get the FW's controller PD list
45568e727371SKashyap D Desai * structure. This information is mainly used to find out about system
45578e727371SKashyap D Desai * supported by Firmware.
4558665484d8SDoug Ambrisko */
45598e727371SKashyap D Desai static int
mrsas_get_pd_list(struct mrsas_softc * sc)45608e727371SKashyap D Desai mrsas_get_pd_list(struct mrsas_softc *sc)
4561665484d8SDoug Ambrisko {
4562665484d8SDoug Ambrisko int retcode = 0, pd_index = 0, pd_count = 0, pd_list_size;
4563f0c7594bSKashyap D Desai u_int8_t do_ocr = 1;
4564665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4565665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
4566665484d8SDoug Ambrisko struct MR_PD_LIST *pd_list_mem;
4567665484d8SDoug Ambrisko struct MR_PD_ADDRESS *pd_addr;
4568665484d8SDoug Ambrisko bus_addr_t pd_list_phys_addr = 0;
4569665484d8SDoug Ambrisko struct mrsas_tmp_dcmd *tcmd;
4570e34a057cSAlfredo Dal'Ava Junior u_int16_t dev_id;
4571665484d8SDoug Ambrisko
4572665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4573665484d8SDoug Ambrisko if (!cmd) {
45744799d485SKashyap D Desai device_printf(sc->mrsas_dev,
45754799d485SKashyap D Desai "Cannot alloc for get PD list cmd\n");
4576665484d8SDoug Ambrisko return 1;
4577665484d8SDoug Ambrisko }
4578665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4579665484d8SDoug Ambrisko
4580665484d8SDoug Ambrisko tcmd = malloc(sizeof(struct mrsas_tmp_dcmd), M_MRSAS, M_NOWAIT);
4581665484d8SDoug Ambrisko pd_list_size = MRSAS_MAX_PD * sizeof(struct MR_PD_LIST);
4582665484d8SDoug Ambrisko if (mrsas_alloc_tmp_dcmd(sc, tcmd, pd_list_size) != SUCCESS) {
45834799d485SKashyap D Desai device_printf(sc->mrsas_dev,
45844799d485SKashyap D Desai "Cannot alloc dmamap for get PD list cmd\n");
4585665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4586f0c7594bSKashyap D Desai mrsas_free_tmp_dcmd(tcmd);
4587f0c7594bSKashyap D Desai free(tcmd, M_MRSAS);
4588665484d8SDoug Ambrisko return (ENOMEM);
45898e727371SKashyap D Desai } else {
4590665484d8SDoug Ambrisko pd_list_mem = tcmd->tmp_dcmd_mem;
4591665484d8SDoug Ambrisko pd_list_phys_addr = tcmd->tmp_dcmd_phys_addr;
4592665484d8SDoug Ambrisko }
4593665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4594665484d8SDoug Ambrisko
4595665484d8SDoug Ambrisko dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4596665484d8SDoug Ambrisko dcmd->mbox.b[1] = 0;
4597665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4598665484d8SDoug Ambrisko dcmd->cmd_status = 0xFF;
4599665484d8SDoug Ambrisko dcmd->sge_count = 1;
4600e34a057cSAlfredo Dal'Ava Junior dcmd->flags = htole16(MFI_FRAME_DIR_READ);
4601665484d8SDoug Ambrisko dcmd->timeout = 0;
4602665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4603e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(MRSAS_MAX_PD * sizeof(struct MR_PD_LIST));
4604e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_PD_LIST_QUERY);
4605e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(pd_list_phys_addr & 0xFFFFFFFF);
4606e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(MRSAS_MAX_PD * sizeof(struct MR_PD_LIST));
4607665484d8SDoug Ambrisko
4608731b7561SKashyap D Desai if (!sc->mask_interrupts)
4609731b7561SKashyap D Desai retcode = mrsas_issue_blocked_cmd(sc, cmd);
4610731b7561SKashyap D Desai else
4611f0c7594bSKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
4612731b7561SKashyap D Desai
4613f0c7594bSKashyap D Desai if (retcode == ETIMEDOUT)
4614f0c7594bSKashyap D Desai goto dcmd_timeout;
4615665484d8SDoug Ambrisko
4616665484d8SDoug Ambrisko /* Get the instance PD list */
4617665484d8SDoug Ambrisko pd_count = MRSAS_MAX_PD;
4618665484d8SDoug Ambrisko pd_addr = pd_list_mem->addr;
4619e34a057cSAlfredo Dal'Ava Junior if (le32toh(pd_list_mem->count) < pd_count) {
46204799d485SKashyap D Desai memset(sc->local_pd_list, 0,
46214799d485SKashyap D Desai MRSAS_MAX_PD * sizeof(struct mrsas_pd_list));
4622e34a057cSAlfredo Dal'Ava Junior for (pd_index = 0; pd_index < le32toh(pd_list_mem->count); pd_index++) {
4623e34a057cSAlfredo Dal'Ava Junior dev_id = le16toh(pd_addr->deviceId);
4624e34a057cSAlfredo Dal'Ava Junior sc->local_pd_list[dev_id].tid = dev_id;
4625e34a057cSAlfredo Dal'Ava Junior sc->local_pd_list[dev_id].driveType =
4626e34a057cSAlfredo Dal'Ava Junior le16toh(pd_addr->scsiDevType);
4627e34a057cSAlfredo Dal'Ava Junior sc->local_pd_list[dev_id].driveState =
46284799d485SKashyap D Desai MR_PD_STATE_SYSTEM;
4629e34a057cSAlfredo Dal'Ava Junior if (sc->target_list[dev_id].target_id == 0xffff)
4630e34a057cSAlfredo Dal'Ava Junior mrsas_add_target(sc, dev_id);
4631665484d8SDoug Ambrisko pd_addr++;
4632665484d8SDoug Ambrisko }
463379b4460bSKashyap D Desai for (pd_index = 0; pd_index < MRSAS_MAX_PD; pd_index++) {
463479b4460bSKashyap D Desai if ((sc->local_pd_list[pd_index].driveState !=
463579b4460bSKashyap D Desai MR_PD_STATE_SYSTEM) &&
463679b4460bSKashyap D Desai (sc->target_list[pd_index].target_id !=
463779b4460bSKashyap D Desai 0xffff)) {
463879b4460bSKashyap D Desai mrsas_remove_target(sc, pd_index);
463979b4460bSKashyap D Desai }
464079b4460bSKashyap D Desai }
46418e727371SKashyap D Desai /*
46428e727371SKashyap D Desai * Use mutext/spinlock if pd_list component size increase more than
46438e727371SKashyap D Desai * 32 bit.
46448e727371SKashyap D Desai */
4645665484d8SDoug Ambrisko memcpy(sc->pd_list, sc->local_pd_list, sizeof(sc->local_pd_list));
4646f0c7594bSKashyap D Desai do_ocr = 0;
4647f0c7594bSKashyap D Desai }
4648f0c7594bSKashyap D Desai dcmd_timeout:
4649665484d8SDoug Ambrisko mrsas_free_tmp_dcmd(tcmd);
4650665484d8SDoug Ambrisko free(tcmd, M_MRSAS);
4651f0c7594bSKashyap D Desai
4652f0c7594bSKashyap D Desai if (do_ocr)
4653f0c7594bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
4654731b7561SKashyap D Desai
4655731b7561SKashyap D Desai if (!sc->mask_interrupts)
4656f0c7594bSKashyap D Desai mrsas_release_mfi_cmd(cmd);
4657f0c7594bSKashyap D Desai
4658665484d8SDoug Ambrisko return (retcode);
4659665484d8SDoug Ambrisko }
4660665484d8SDoug Ambrisko
46618e727371SKashyap D Desai /*
46628e727371SKashyap D Desai * mrsas_get_ld_list: Returns FW's LD list structure input:
46638e727371SKashyap D Desai * Adapter soft state
4664665484d8SDoug Ambrisko *
46658e727371SKashyap D Desai * Issues an internal command (DCMD) to get the FW's controller PD list
46668e727371SKashyap D Desai * structure. This information is mainly used to find out about supported by
46678e727371SKashyap D Desai * the FW.
4668665484d8SDoug Ambrisko */
46698e727371SKashyap D Desai static int
mrsas_get_ld_list(struct mrsas_softc * sc)46708e727371SKashyap D Desai mrsas_get_ld_list(struct mrsas_softc *sc)
4671665484d8SDoug Ambrisko {
467279b4460bSKashyap D Desai int ld_list_size, retcode = 0, ld_index = 0, ids = 0, drv_tgt_id;
4673f0c7594bSKashyap D Desai u_int8_t do_ocr = 1;
4674665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4675665484d8SDoug Ambrisko struct mrsas_dcmd_frame *dcmd;
4676665484d8SDoug Ambrisko struct MR_LD_LIST *ld_list_mem;
4677665484d8SDoug Ambrisko bus_addr_t ld_list_phys_addr = 0;
4678665484d8SDoug Ambrisko struct mrsas_tmp_dcmd *tcmd;
4679665484d8SDoug Ambrisko
4680665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4681665484d8SDoug Ambrisko if (!cmd) {
46824799d485SKashyap D Desai device_printf(sc->mrsas_dev,
46834799d485SKashyap D Desai "Cannot alloc for get LD list cmd\n");
4684665484d8SDoug Ambrisko return 1;
4685665484d8SDoug Ambrisko }
4686665484d8SDoug Ambrisko dcmd = &cmd->frame->dcmd;
4687665484d8SDoug Ambrisko
4688665484d8SDoug Ambrisko tcmd = malloc(sizeof(struct mrsas_tmp_dcmd), M_MRSAS, M_NOWAIT);
4689665484d8SDoug Ambrisko ld_list_size = sizeof(struct MR_LD_LIST);
4690665484d8SDoug Ambrisko if (mrsas_alloc_tmp_dcmd(sc, tcmd, ld_list_size) != SUCCESS) {
46914799d485SKashyap D Desai device_printf(sc->mrsas_dev,
46924799d485SKashyap D Desai "Cannot alloc dmamap for get LD list cmd\n");
4693665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4694f0c7594bSKashyap D Desai mrsas_free_tmp_dcmd(tcmd);
4695f0c7594bSKashyap D Desai free(tcmd, M_MRSAS);
4696665484d8SDoug Ambrisko return (ENOMEM);
46978e727371SKashyap D Desai } else {
4698665484d8SDoug Ambrisko ld_list_mem = tcmd->tmp_dcmd_mem;
4699665484d8SDoug Ambrisko ld_list_phys_addr = tcmd->tmp_dcmd_phys_addr;
4700665484d8SDoug Ambrisko }
4701665484d8SDoug Ambrisko memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4702665484d8SDoug Ambrisko
47034799d485SKashyap D Desai if (sc->max256vdSupport)
47044799d485SKashyap D Desai dcmd->mbox.b[0] = 1;
47054799d485SKashyap D Desai
4706665484d8SDoug Ambrisko dcmd->cmd = MFI_CMD_DCMD;
4707665484d8SDoug Ambrisko dcmd->cmd_status = 0xFF;
4708665484d8SDoug Ambrisko dcmd->sge_count = 1;
4709665484d8SDoug Ambrisko dcmd->flags = MFI_FRAME_DIR_READ;
4710665484d8SDoug Ambrisko dcmd->timeout = 0;
4711e34a057cSAlfredo Dal'Ava Junior dcmd->data_xfer_len = htole32(sizeof(struct MR_LD_LIST));
4712e34a057cSAlfredo Dal'Ava Junior dcmd->opcode = htole32(MR_DCMD_LD_GET_LIST);
4713e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].phys_addr = htole32(ld_list_phys_addr);
4714e34a057cSAlfredo Dal'Ava Junior dcmd->sgl.sge32[0].length = htole32(sizeof(struct MR_LD_LIST));
4715665484d8SDoug Ambrisko dcmd->pad_0 = 0;
4716665484d8SDoug Ambrisko
4717731b7561SKashyap D Desai if (!sc->mask_interrupts)
4718731b7561SKashyap D Desai retcode = mrsas_issue_blocked_cmd(sc, cmd);
4719731b7561SKashyap D Desai else
4720f0c7594bSKashyap D Desai retcode = mrsas_issue_polled(sc, cmd);
4721731b7561SKashyap D Desai
4722f0c7594bSKashyap D Desai if (retcode == ETIMEDOUT)
4723f0c7594bSKashyap D Desai goto dcmd_timeout;
4724665484d8SDoug Ambrisko
47254799d485SKashyap D Desai #if VD_EXT_DEBUG
47264799d485SKashyap D Desai printf("Number of LDs %d\n", ld_list_mem->ldCount);
47274799d485SKashyap D Desai #endif
47284799d485SKashyap D Desai
4729665484d8SDoug Ambrisko /* Get the instance LD list */
4730e34a057cSAlfredo Dal'Ava Junior if (le32toh(ld_list_mem->ldCount) <= sc->fw_supported_vd_count) {
4731e34a057cSAlfredo Dal'Ava Junior sc->CurLdCount = le32toh(ld_list_mem->ldCount);
47324799d485SKashyap D Desai memset(sc->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4733e34a057cSAlfredo Dal'Ava Junior for (ld_index = 0; ld_index < le32toh(ld_list_mem->ldCount); ld_index++) {
4734665484d8SDoug Ambrisko ids = ld_list_mem->ldList[ld_index].ref.ld_context.targetId;
473579b4460bSKashyap D Desai drv_tgt_id = ids + MRSAS_MAX_PD;
473679b4460bSKashyap D Desai if (ld_list_mem->ldList[ld_index].state != 0) {
4737665484d8SDoug Ambrisko sc->ld_ids[ids] = ld_list_mem->ldList[ld_index].ref.ld_context.targetId;
473879b4460bSKashyap D Desai if (sc->target_list[drv_tgt_id].target_id ==
473979b4460bSKashyap D Desai 0xffff)
474079b4460bSKashyap D Desai mrsas_add_target(sc, drv_tgt_id);
474179b4460bSKashyap D Desai } else {
474279b4460bSKashyap D Desai if (sc->target_list[drv_tgt_id].target_id !=
474379b4460bSKashyap D Desai 0xffff)
474479b4460bSKashyap D Desai mrsas_remove_target(sc,
474579b4460bSKashyap D Desai drv_tgt_id);
4746665484d8SDoug Ambrisko }
4747665484d8SDoug Ambrisko }
474879b4460bSKashyap D Desai
4749f0c7594bSKashyap D Desai do_ocr = 0;
4750665484d8SDoug Ambrisko }
4751f0c7594bSKashyap D Desai dcmd_timeout:
4752665484d8SDoug Ambrisko mrsas_free_tmp_dcmd(tcmd);
4753665484d8SDoug Ambrisko free(tcmd, M_MRSAS);
4754f0c7594bSKashyap D Desai
4755f0c7594bSKashyap D Desai if (do_ocr)
4756f0c7594bSKashyap D Desai sc->do_timedout_reset = MFI_DCMD_TIMEOUT_OCR;
4757731b7561SKashyap D Desai if (!sc->mask_interrupts)
4758f0c7594bSKashyap D Desai mrsas_release_mfi_cmd(cmd);
4759f0c7594bSKashyap D Desai
4760665484d8SDoug Ambrisko return (retcode);
4761665484d8SDoug Ambrisko }
4762665484d8SDoug Ambrisko
47638e727371SKashyap D Desai /*
47648e727371SKashyap D Desai * mrsas_alloc_tmp_dcmd: Allocates memory for temporary command input:
476532c601b6SGordon Bergling * Adapter soft state Temp command Size of allocation
4766665484d8SDoug Ambrisko *
4767665484d8SDoug Ambrisko * Allocates DMAable memory for a temporary internal command. The allocated
4768665484d8SDoug Ambrisko * memory is initialized to all zeros upon successful loading of the dma
4769665484d8SDoug Ambrisko * mapped memory.
4770665484d8SDoug Ambrisko */
47718e727371SKashyap D Desai int
mrsas_alloc_tmp_dcmd(struct mrsas_softc * sc,struct mrsas_tmp_dcmd * tcmd,int size)47728e727371SKashyap D Desai mrsas_alloc_tmp_dcmd(struct mrsas_softc *sc,
47738e727371SKashyap D Desai struct mrsas_tmp_dcmd *tcmd, int size)
4774665484d8SDoug Ambrisko {
47758e727371SKashyap D Desai if (bus_dma_tag_create(sc->mrsas_parent_tag,
47768e727371SKashyap D Desai 1, 0,
47778e727371SKashyap D Desai BUS_SPACE_MAXADDR_32BIT,
47788e727371SKashyap D Desai BUS_SPACE_MAXADDR,
47798e727371SKashyap D Desai NULL, NULL,
47808e727371SKashyap D Desai size,
47818e727371SKashyap D Desai 1,
47828e727371SKashyap D Desai size,
47838e727371SKashyap D Desai BUS_DMA_ALLOCNOW,
47848e727371SKashyap D Desai NULL, NULL,
4785665484d8SDoug Ambrisko &tcmd->tmp_dcmd_tag)) {
4786665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate tmp dcmd tag\n");
4787665484d8SDoug Ambrisko return (ENOMEM);
4788665484d8SDoug Ambrisko }
4789665484d8SDoug Ambrisko if (bus_dmamem_alloc(tcmd->tmp_dcmd_tag, (void **)&tcmd->tmp_dcmd_mem,
4790665484d8SDoug Ambrisko BUS_DMA_NOWAIT, &tcmd->tmp_dcmd_dmamap)) {
4791665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot allocate tmp dcmd mem\n");
4792665484d8SDoug Ambrisko return (ENOMEM);
4793665484d8SDoug Ambrisko }
4794665484d8SDoug Ambrisko if (bus_dmamap_load(tcmd->tmp_dcmd_tag, tcmd->tmp_dcmd_dmamap,
4795665484d8SDoug Ambrisko tcmd->tmp_dcmd_mem, size, mrsas_addr_cb,
4796665484d8SDoug Ambrisko &tcmd->tmp_dcmd_phys_addr, BUS_DMA_NOWAIT)) {
4797665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot load tmp dcmd mem\n");
4798665484d8SDoug Ambrisko return (ENOMEM);
4799665484d8SDoug Ambrisko }
4800665484d8SDoug Ambrisko memset(tcmd->tmp_dcmd_mem, 0, size);
4801665484d8SDoug Ambrisko return (0);
4802665484d8SDoug Ambrisko }
4803665484d8SDoug Ambrisko
48048e727371SKashyap D Desai /*
48058e727371SKashyap D Desai * mrsas_free_tmp_dcmd: Free memory for temporary command input:
48068e727371SKashyap D Desai * temporary dcmd pointer
4807665484d8SDoug Ambrisko *
48088e727371SKashyap D Desai * Deallocates memory of the temporary command for use in the construction of
48098e727371SKashyap D Desai * the internal DCMD.
4810665484d8SDoug Ambrisko */
48118e727371SKashyap D Desai void
mrsas_free_tmp_dcmd(struct mrsas_tmp_dcmd * tmp)48128e727371SKashyap D Desai mrsas_free_tmp_dcmd(struct mrsas_tmp_dcmd *tmp)
4813665484d8SDoug Ambrisko {
4814665484d8SDoug Ambrisko if (tmp->tmp_dcmd_phys_addr)
4815665484d8SDoug Ambrisko bus_dmamap_unload(tmp->tmp_dcmd_tag, tmp->tmp_dcmd_dmamap);
4816665484d8SDoug Ambrisko if (tmp->tmp_dcmd_mem != NULL)
4817665484d8SDoug Ambrisko bus_dmamem_free(tmp->tmp_dcmd_tag, tmp->tmp_dcmd_mem, tmp->tmp_dcmd_dmamap);
4818665484d8SDoug Ambrisko if (tmp->tmp_dcmd_tag != NULL)
4819665484d8SDoug Ambrisko bus_dma_tag_destroy(tmp->tmp_dcmd_tag);
4820665484d8SDoug Ambrisko }
4821665484d8SDoug Ambrisko
48228e727371SKashyap D Desai /*
48238e727371SKashyap D Desai * mrsas_issue_blocked_abort_cmd: Aborts previously issued cmd input:
48248e727371SKashyap D Desai * Adapter soft state Previously issued cmd to be aborted
4825665484d8SDoug Ambrisko *
4826665484d8SDoug Ambrisko * This function is used to abort previously issued commands, such as AEN and
4827665484d8SDoug Ambrisko * RAID map sync map commands. The abort command is sent as a DCMD internal
4828665484d8SDoug Ambrisko * command and subsequently the driver will wait for a return status. The
4829665484d8SDoug Ambrisko * max wait time is MRSAS_INTERNAL_CMD_WAIT_TIME seconds.
4830665484d8SDoug Ambrisko */
48318e727371SKashyap D Desai static int
mrsas_issue_blocked_abort_cmd(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd_to_abort)48328e727371SKashyap D Desai mrsas_issue_blocked_abort_cmd(struct mrsas_softc *sc,
4833665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd_to_abort)
4834665484d8SDoug Ambrisko {
4835665484d8SDoug Ambrisko struct mrsas_mfi_cmd *cmd;
4836665484d8SDoug Ambrisko struct mrsas_abort_frame *abort_fr;
4837665484d8SDoug Ambrisko u_int8_t retcode = 0;
4838665484d8SDoug Ambrisko unsigned long total_time = 0;
4839665484d8SDoug Ambrisko u_int8_t max_wait = MRSAS_INTERNAL_CMD_WAIT_TIME;
4840665484d8SDoug Ambrisko
4841665484d8SDoug Ambrisko cmd = mrsas_get_mfi_cmd(sc);
4842665484d8SDoug Ambrisko if (!cmd) {
4843665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Cannot alloc for abort cmd\n");
4844665484d8SDoug Ambrisko return (1);
4845665484d8SDoug Ambrisko }
4846665484d8SDoug Ambrisko abort_fr = &cmd->frame->abort;
4847665484d8SDoug Ambrisko
4848665484d8SDoug Ambrisko /* Prepare and issue the abort frame */
4849665484d8SDoug Ambrisko abort_fr->cmd = MFI_CMD_ABORT;
4850665484d8SDoug Ambrisko abort_fr->cmd_status = 0xFF;
4851665484d8SDoug Ambrisko abort_fr->flags = 0;
4852665484d8SDoug Ambrisko abort_fr->abort_context = cmd_to_abort->index;
4853665484d8SDoug Ambrisko abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
4854665484d8SDoug Ambrisko abort_fr->abort_mfi_phys_addr_hi = 0;
4855665484d8SDoug Ambrisko
4856665484d8SDoug Ambrisko cmd->sync_cmd = 1;
4857665484d8SDoug Ambrisko cmd->cmd_status = 0xFF;
4858665484d8SDoug Ambrisko
4859665484d8SDoug Ambrisko if (mrsas_issue_dcmd(sc, cmd)) {
4860665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Fail to send abort command.\n");
4861665484d8SDoug Ambrisko return (1);
4862665484d8SDoug Ambrisko }
4863665484d8SDoug Ambrisko /* Wait for this cmd to complete */
4864665484d8SDoug Ambrisko sc->chan = (void *)&cmd;
4865665484d8SDoug Ambrisko while (1) {
4866665484d8SDoug Ambrisko if (cmd->cmd_status == 0xFF) {
4867665484d8SDoug Ambrisko tsleep((void *)&sc->chan, 0, "mrsas_sleep", hz);
48688e727371SKashyap D Desai } else
4869665484d8SDoug Ambrisko break;
4870665484d8SDoug Ambrisko total_time++;
4871665484d8SDoug Ambrisko if (total_time >= max_wait) {
4872665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "Abort cmd timed out after %d sec.\n", max_wait);
4873665484d8SDoug Ambrisko retcode = 1;
4874665484d8SDoug Ambrisko break;
4875665484d8SDoug Ambrisko }
4876665484d8SDoug Ambrisko }
4877665484d8SDoug Ambrisko
4878665484d8SDoug Ambrisko cmd->sync_cmd = 0;
4879665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
4880665484d8SDoug Ambrisko return (retcode);
4881665484d8SDoug Ambrisko }
4882665484d8SDoug Ambrisko
48838e727371SKashyap D Desai /*
48848e727371SKashyap D Desai * mrsas_complete_abort: Completes aborting a command input:
48858e727371SKashyap D Desai * Adapter soft state Cmd that was issued to abort another cmd
4886665484d8SDoug Ambrisko *
48878e727371SKashyap D Desai * The mrsas_issue_blocked_abort_cmd() function waits for the command status to
48888e727371SKashyap D Desai * change after sending the command. This function is called from
4889665484d8SDoug Ambrisko * mrsas_complete_mptmfi_passthru() to wake up the sleep thread associated.
4890665484d8SDoug Ambrisko */
48918e727371SKashyap D Desai void
mrsas_complete_abort(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)48928e727371SKashyap D Desai mrsas_complete_abort(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
4893665484d8SDoug Ambrisko {
4894665484d8SDoug Ambrisko if (cmd->sync_cmd) {
4895665484d8SDoug Ambrisko cmd->sync_cmd = 0;
4896665484d8SDoug Ambrisko cmd->cmd_status = 0;
4897665484d8SDoug Ambrisko sc->chan = (void *)&cmd;
4898665484d8SDoug Ambrisko wakeup_one((void *)&sc->chan);
4899665484d8SDoug Ambrisko }
4900665484d8SDoug Ambrisko return;
4901665484d8SDoug Ambrisko }
4902665484d8SDoug Ambrisko
49038e727371SKashyap D Desai /*
49048e727371SKashyap D Desai * mrsas_aen_handler: AEN processing callback function from thread context
4905665484d8SDoug Ambrisko * input: Adapter soft state
4906665484d8SDoug Ambrisko *
49078e727371SKashyap D Desai * Asynchronous event handler
4908665484d8SDoug Ambrisko */
49098e727371SKashyap D Desai void
mrsas_aen_handler(struct mrsas_softc * sc)49108e727371SKashyap D Desai mrsas_aen_handler(struct mrsas_softc *sc)
4911665484d8SDoug Ambrisko {
4912665484d8SDoug Ambrisko union mrsas_evt_class_locale class_locale;
4913665484d8SDoug Ambrisko int doscan = 0;
4914665484d8SDoug Ambrisko u_int32_t seq_num;
4915f0c7594bSKashyap D Desai int error, fail_aen = 0;
4916665484d8SDoug Ambrisko
49175bae00d6SSteven Hartland if (sc == NULL) {
49185bae00d6SSteven Hartland printf("invalid instance!\n");
4919665484d8SDoug Ambrisko return;
4920665484d8SDoug Ambrisko }
492185c0a961SKashyap D Desai if (sc->remove_in_progress || sc->reset_in_progress) {
492285c0a961SKashyap D Desai device_printf(sc->mrsas_dev, "Returning from %s, line no %d\n",
492385c0a961SKashyap D Desai __func__, __LINE__);
492485c0a961SKashyap D Desai return;
492585c0a961SKashyap D Desai }
4926665484d8SDoug Ambrisko if (sc->evt_detail_mem) {
4927665484d8SDoug Ambrisko switch (sc->evt_detail_mem->code) {
4928665484d8SDoug Ambrisko case MR_EVT_PD_INSERTED:
4929f0c7594bSKashyap D Desai fail_aen = mrsas_get_pd_list(sc);
4930f0c7594bSKashyap D Desai if (!fail_aen)
4931665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_1);
4932f0c7594bSKashyap D Desai else
4933f0c7594bSKashyap D Desai goto skip_register_aen;
4934665484d8SDoug Ambrisko break;
4935665484d8SDoug Ambrisko case MR_EVT_PD_REMOVED:
4936f0c7594bSKashyap D Desai fail_aen = mrsas_get_pd_list(sc);
4937f0c7594bSKashyap D Desai if (!fail_aen)
4938665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_1);
4939f0c7594bSKashyap D Desai else
4940f0c7594bSKashyap D Desai goto skip_register_aen;
4941665484d8SDoug Ambrisko break;
4942665484d8SDoug Ambrisko case MR_EVT_LD_OFFLINE:
4943665484d8SDoug Ambrisko case MR_EVT_CFG_CLEARED:
4944665484d8SDoug Ambrisko case MR_EVT_LD_DELETED:
4945665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_0);
4946665484d8SDoug Ambrisko break;
4947665484d8SDoug Ambrisko case MR_EVT_LD_CREATED:
4948f0c7594bSKashyap D Desai fail_aen = mrsas_get_ld_list(sc);
4949f0c7594bSKashyap D Desai if (!fail_aen)
4950665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_0);
4951f0c7594bSKashyap D Desai else
4952f0c7594bSKashyap D Desai goto skip_register_aen;
4953665484d8SDoug Ambrisko break;
4954665484d8SDoug Ambrisko case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4955665484d8SDoug Ambrisko case MR_EVT_FOREIGN_CFG_IMPORTED:
4956665484d8SDoug Ambrisko case MR_EVT_LD_STATE_CHANGE:
4957665484d8SDoug Ambrisko doscan = 1;
4958665484d8SDoug Ambrisko break;
49598bc320adSKashyap D Desai case MR_EVT_CTRL_PROP_CHANGED:
49608bc320adSKashyap D Desai fail_aen = mrsas_get_ctrl_info(sc);
49618bc320adSKashyap D Desai if (fail_aen)
49628bc320adSKashyap D Desai goto skip_register_aen;
49638bc320adSKashyap D Desai break;
4964665484d8SDoug Ambrisko default:
4965665484d8SDoug Ambrisko break;
4966665484d8SDoug Ambrisko }
4967665484d8SDoug Ambrisko } else {
4968665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "invalid evt_detail\n");
4969665484d8SDoug Ambrisko return;
4970665484d8SDoug Ambrisko }
4971665484d8SDoug Ambrisko if (doscan) {
4972f0c7594bSKashyap D Desai fail_aen = mrsas_get_pd_list(sc);
4973f0c7594bSKashyap D Desai if (!fail_aen) {
4974665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_AEN, "scanning ...sim 1\n");
4975665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_1);
4976f0c7594bSKashyap D Desai } else
4977f0c7594bSKashyap D Desai goto skip_register_aen;
4978f0c7594bSKashyap D Desai
4979f0c7594bSKashyap D Desai fail_aen = mrsas_get_ld_list(sc);
4980f0c7594bSKashyap D Desai if (!fail_aen) {
4981665484d8SDoug Ambrisko mrsas_dprint(sc, MRSAS_AEN, "scanning ...sim 0\n");
4982665484d8SDoug Ambrisko mrsas_bus_scan_sim(sc, sc->sim_0);
4983f0c7594bSKashyap D Desai } else
4984f0c7594bSKashyap D Desai goto skip_register_aen;
4985665484d8SDoug Ambrisko }
4986665484d8SDoug Ambrisko seq_num = sc->evt_detail_mem->seq_num + 1;
4987665484d8SDoug Ambrisko
49888e727371SKashyap D Desai /* Register AEN with FW for latest sequence number plus 1 */
4989665484d8SDoug Ambrisko class_locale.members.reserved = 0;
4990665484d8SDoug Ambrisko class_locale.members.locale = MR_EVT_LOCALE_ALL;
4991665484d8SDoug Ambrisko class_locale.members.class = MR_EVT_CLASS_DEBUG;
4992665484d8SDoug Ambrisko
4993665484d8SDoug Ambrisko if (sc->aen_cmd != NULL)
4994665484d8SDoug Ambrisko return;
4995665484d8SDoug Ambrisko
4996665484d8SDoug Ambrisko mtx_lock(&sc->aen_lock);
4997665484d8SDoug Ambrisko error = mrsas_register_aen(sc, seq_num,
4998665484d8SDoug Ambrisko class_locale.word);
4999665484d8SDoug Ambrisko mtx_unlock(&sc->aen_lock);
5000665484d8SDoug Ambrisko
5001665484d8SDoug Ambrisko if (error)
5002665484d8SDoug Ambrisko device_printf(sc->mrsas_dev, "register aen failed error %x\n", error);
5003665484d8SDoug Ambrisko
5004f0c7594bSKashyap D Desai skip_register_aen:
5005f0c7594bSKashyap D Desai return;
5006f0c7594bSKashyap D Desai
5007665484d8SDoug Ambrisko }
5008665484d8SDoug Ambrisko
50098e727371SKashyap D Desai /*
5010665484d8SDoug Ambrisko * mrsas_complete_aen: Completes AEN command
5011665484d8SDoug Ambrisko * input: Adapter soft state
5012665484d8SDoug Ambrisko * Cmd that was issued to abort another cmd
5013665484d8SDoug Ambrisko *
50148e727371SKashyap D Desai * This function will be called from ISR and will continue event processing from
50158e727371SKashyap D Desai * thread context by enqueuing task in ev_tq (callback function
50168e727371SKashyap D Desai * "mrsas_aen_handler").
5017665484d8SDoug Ambrisko */
50188e727371SKashyap D Desai void
mrsas_complete_aen(struct mrsas_softc * sc,struct mrsas_mfi_cmd * cmd)50198e727371SKashyap D Desai mrsas_complete_aen(struct mrsas_softc *sc, struct mrsas_mfi_cmd *cmd)
5020665484d8SDoug Ambrisko {
5021665484d8SDoug Ambrisko /*
50228e727371SKashyap D Desai * Don't signal app if it is just an aborted previously registered
50238e727371SKashyap D Desai * aen
5024665484d8SDoug Ambrisko */
5025665484d8SDoug Ambrisko if ((!cmd->abort_aen) && (sc->remove_in_progress == 0)) {
5026da011113SKashyap D Desai sc->mrsas_aen_triggered = 1;
5027ecea5be4SKashyap D Desai mtx_lock(&sc->aen_lock);
5028da011113SKashyap D Desai if (sc->mrsas_poll_waiting) {
5029da011113SKashyap D Desai sc->mrsas_poll_waiting = 0;
5030da011113SKashyap D Desai selwakeup(&sc->mrsas_select);
5031da011113SKashyap D Desai }
5032ecea5be4SKashyap D Desai mtx_unlock(&sc->aen_lock);
50338e727371SKashyap D Desai } else
5034665484d8SDoug Ambrisko cmd->abort_aen = 0;
5035665484d8SDoug Ambrisko
5036665484d8SDoug Ambrisko sc->aen_cmd = NULL;
5037665484d8SDoug Ambrisko mrsas_release_mfi_cmd(cmd);
5038665484d8SDoug Ambrisko
5039665484d8SDoug Ambrisko taskqueue_enqueue(sc->ev_tq, &sc->ev_task);
5040665484d8SDoug Ambrisko
5041665484d8SDoug Ambrisko return;
5042665484d8SDoug Ambrisko }
5043665484d8SDoug Ambrisko
5044665484d8SDoug Ambrisko static device_method_t mrsas_methods[] = {
5045665484d8SDoug Ambrisko DEVMETHOD(device_probe, mrsas_probe),
5046665484d8SDoug Ambrisko DEVMETHOD(device_attach, mrsas_attach),
5047665484d8SDoug Ambrisko DEVMETHOD(device_detach, mrsas_detach),
5048f28ecf2bSAndriy Gapon DEVMETHOD(device_shutdown, mrsas_shutdown),
5049665484d8SDoug Ambrisko DEVMETHOD(device_suspend, mrsas_suspend),
5050665484d8SDoug Ambrisko DEVMETHOD(device_resume, mrsas_resume),
5051665484d8SDoug Ambrisko DEVMETHOD(bus_print_child, bus_generic_print_child),
5052665484d8SDoug Ambrisko DEVMETHOD(bus_driver_added, bus_generic_driver_added),
5053665484d8SDoug Ambrisko {0, 0}
5054665484d8SDoug Ambrisko };
5055665484d8SDoug Ambrisko
5056665484d8SDoug Ambrisko static driver_t mrsas_driver = {
5057665484d8SDoug Ambrisko "mrsas",
5058665484d8SDoug Ambrisko mrsas_methods,
5059665484d8SDoug Ambrisko sizeof(struct mrsas_softc)
5060665484d8SDoug Ambrisko };
5061665484d8SDoug Ambrisko
50628b2be9cbSJohn Baldwin DRIVER_MODULE(mrsas, pci, mrsas_driver, 0, 0);
5063665484d8SDoug Ambrisko MODULE_DEPEND(mrsas, cam, 1, 1, 1);
5064