5017 use 64bit DMA in mpt_sas
[illumos-gate.git] / usr / src / uts / common / io / scsi / adapters / mpt_sas / mptsas.c
blob11232881e13c3fbdf8a48400c6ff862b1ef29336
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 * Copyright 2014 OmniTI Computer Consulting, Inc. All rights reserved.
27 * Copyright (c) 2014, Tegile Systems Inc. All rights reserved.
31 * Copyright (c) 2000 to 2010, LSI Corporation.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms of all code within
35 * this file that is exclusively owned by LSI, with or without
36 * modification, is permitted provided that, in addition to the CDDL 1.0
37 * License requirements, the following conditions are met:
39 * Neither the name of the author nor the names of its contributors may be
40 * used to endorse or promote products derived from this software without
41 * specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
47 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
50 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
51 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
54 * DAMAGE.
58 * mptsas - This is a driver based on LSI Logic's MPT2.0/2.5 interface.
62 #if defined(lint) || defined(DEBUG)
63 #define MPTSAS_DEBUG
64 #endif
67 * standard header files.
69 #include <sys/note.h>
70 #include <sys/scsi/scsi.h>
71 #include <sys/pci.h>
72 #include <sys/file.h>
73 #include <sys/policy.h>
74 #include <sys/model.h>
75 #include <sys/sysevent.h>
76 #include <sys/sysevent/eventdefs.h>
77 #include <sys/sysevent/dr.h>
78 #include <sys/sata/sata_defs.h>
79 #include <sys/scsi/generic/sas.h>
80 #include <sys/scsi/impl/scsi_sas.h>
82 #pragma pack(1)
83 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_type.h>
84 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2.h>
85 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_cnfg.h>
86 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_init.h>
87 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_ioc.h>
88 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_sas.h>
89 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_tool.h>
90 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_raid.h>
91 #pragma pack()
94 * private header files.
97 #include <sys/scsi/impl/scsi_reset_notify.h>
98 #include <sys/scsi/adapters/mpt_sas/mptsas_var.h>
99 #include <sys/scsi/adapters/mpt_sas/mptsas_ioctl.h>
100 #include <sys/scsi/adapters/mpt_sas/mptsas_smhba.h>
101 #include <sys/scsi/adapters/mpt_sas/mptsas_hash.h>
102 #include <sys/raidioctl.h>
104 #include <sys/fs/dv_node.h> /* devfs_clean */
107 * FMA header files
109 #include <sys/ddifm.h>
110 #include <sys/fm/protocol.h>
111 #include <sys/fm/util.h>
112 #include <sys/fm/io/ddi.h>
115 * autoconfiguration data and routines.
117 static int mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
118 static int mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
119 static int mptsas_power(dev_info_t *dip, int component, int level);
122 * cb_ops function
124 static int mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
125 cred_t *credp, int *rval);
126 #ifdef __sparc
127 static int mptsas_reset(dev_info_t *devi, ddi_reset_cmd_t cmd);
128 #else /* __sparc */
129 static int mptsas_quiesce(dev_info_t *devi);
130 #endif /* __sparc */
133 * Resource initilaization for hardware
135 static void mptsas_setup_cmd_reg(mptsas_t *mpt);
136 static void mptsas_disable_bus_master(mptsas_t *mpt);
137 static void mptsas_hba_fini(mptsas_t *mpt);
138 static void mptsas_cfg_fini(mptsas_t *mptsas_blkp);
139 static int mptsas_hba_setup(mptsas_t *mpt);
140 static void mptsas_hba_teardown(mptsas_t *mpt);
141 static int mptsas_config_space_init(mptsas_t *mpt);
142 static void mptsas_config_space_fini(mptsas_t *mpt);
143 static void mptsas_iport_register(mptsas_t *mpt);
144 static int mptsas_smp_setup(mptsas_t *mpt);
145 static void mptsas_smp_teardown(mptsas_t *mpt);
146 static int mptsas_cache_create(mptsas_t *mpt);
147 static void mptsas_cache_destroy(mptsas_t *mpt);
148 static int mptsas_alloc_request_frames(mptsas_t *mpt);
149 static int mptsas_alloc_sense_bufs(mptsas_t *mpt);
150 static int mptsas_alloc_reply_frames(mptsas_t *mpt);
151 static int mptsas_alloc_free_queue(mptsas_t *mpt);
152 static int mptsas_alloc_post_queue(mptsas_t *mpt);
153 static void mptsas_alloc_reply_args(mptsas_t *mpt);
154 static int mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
155 static void mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
156 static int mptsas_init_chip(mptsas_t *mpt, int first_time);
159 * SCSA function prototypes
161 static int mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
162 static int mptsas_scsi_reset(struct scsi_address *ap, int level);
163 static int mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
164 static int mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly);
165 static int mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value,
166 int tgtonly);
167 static void mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt);
168 static struct scsi_pkt *mptsas_scsi_init_pkt(struct scsi_address *ap,
169 struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
170 int tgtlen, int flags, int (*callback)(), caddr_t arg);
171 static void mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
172 static void mptsas_scsi_destroy_pkt(struct scsi_address *ap,
173 struct scsi_pkt *pkt);
174 static int mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
175 scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
176 static void mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
177 scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
178 static int mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
179 void (*callback)(caddr_t), caddr_t arg);
180 static int mptsas_get_name(struct scsi_device *sd, char *name, int len);
181 static int mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len);
182 static int mptsas_scsi_quiesce(dev_info_t *dip);
183 static int mptsas_scsi_unquiesce(dev_info_t *dip);
184 static int mptsas_bus_config(dev_info_t *pdip, uint_t flags,
185 ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
188 * SMP functions
190 static int mptsas_smp_start(struct smp_pkt *smp_pkt);
193 * internal function prototypes.
195 static void mptsas_list_add(mptsas_t *mpt);
196 static void mptsas_list_del(mptsas_t *mpt);
198 static int mptsas_quiesce_bus(mptsas_t *mpt);
199 static int mptsas_unquiesce_bus(mptsas_t *mpt);
201 static int mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size);
202 static void mptsas_free_handshake_msg(mptsas_t *mpt);
204 static void mptsas_ncmds_checkdrain(void *arg);
206 static int mptsas_prepare_pkt(mptsas_cmd_t *cmd);
207 static int mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *sp);
208 static int mptsas_accept_txwq_and_pkt(mptsas_t *mpt, mptsas_cmd_t *sp);
209 static void mptsas_accept_tx_waitq(mptsas_t *mpt);
211 static int mptsas_do_detach(dev_info_t *dev);
212 static int mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl);
213 static int mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun,
214 struct scsi_pkt *pkt);
215 static int mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp);
217 static void mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd);
218 static void mptsas_handle_event(void *args);
219 static int mptsas_handle_event_sync(void *args);
220 static void mptsas_handle_dr(void *args);
221 static void mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
222 dev_info_t *pdip);
224 static void mptsas_restart_cmd(void *);
226 static void mptsas_flush_hba(mptsas_t *mpt);
227 static void mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun,
228 uint8_t tasktype);
229 static void mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd,
230 uchar_t reason, uint_t stat);
232 static uint_t mptsas_intr(caddr_t arg1, caddr_t arg2);
233 static void mptsas_process_intr(mptsas_t *mpt,
234 pMpi2ReplyDescriptorsUnion_t reply_desc_union);
235 static void mptsas_handle_scsi_io_success(mptsas_t *mpt,
236 pMpi2ReplyDescriptorsUnion_t reply_desc);
237 static void mptsas_handle_address_reply(mptsas_t *mpt,
238 pMpi2ReplyDescriptorsUnion_t reply_desc);
239 static int mptsas_wait_intr(mptsas_t *mpt, int polltime);
240 static void mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd,
241 uint32_t *control, pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl);
243 static void mptsas_watch(void *arg);
244 static void mptsas_watchsubr(mptsas_t *mpt);
245 static void mptsas_cmd_timeout(mptsas_t *mpt, mptsas_target_t *ptgt);
247 static void mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd);
248 static int mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
249 uint8_t *data, uint32_t request_size, uint32_t reply_size,
250 uint32_t data_size, uint32_t direction, uint8_t *dataout,
251 uint32_t dataout_size, short timeout, int mode);
252 static int mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl);
254 static uint8_t mptsas_get_fw_diag_buffer_number(mptsas_t *mpt,
255 uint32_t unique_id);
256 static void mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd);
257 static int mptsas_post_fw_diag_buffer(mptsas_t *mpt,
258 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
259 static int mptsas_release_fw_diag_buffer(mptsas_t *mpt,
260 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
261 uint32_t diag_type);
262 static int mptsas_diag_register(mptsas_t *mpt,
263 mptsas_fw_diag_register_t *diag_register, uint32_t *return_code);
264 static int mptsas_diag_unregister(mptsas_t *mpt,
265 mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
266 static int mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
267 uint32_t *return_code);
268 static int mptsas_diag_read_buffer(mptsas_t *mpt,
269 mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
270 uint32_t *return_code, int ioctl_mode);
271 static int mptsas_diag_release(mptsas_t *mpt,
272 mptsas_fw_diag_release_t *diag_release, uint32_t *return_code);
273 static int mptsas_do_diag_action(mptsas_t *mpt, uint32_t action,
274 uint8_t *diag_action, uint32_t length, uint32_t *return_code,
275 int ioctl_mode);
276 static int mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *data,
277 int mode);
279 static int mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
280 int cmdlen, int tgtlen, int statuslen, int kf);
281 static void mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd);
283 static int mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags);
284 static void mptsas_kmem_cache_destructor(void *buf, void *cdrarg);
286 static int mptsas_cache_frames_constructor(void *buf, void *cdrarg,
287 int kmflags);
288 static void mptsas_cache_frames_destructor(void *buf, void *cdrarg);
290 static void mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
291 mptsas_cmd_t *cmd);
292 static void mptsas_check_task_mgt(mptsas_t *mpt,
293 pMpi2SCSIManagementReply_t reply, mptsas_cmd_t *cmd);
294 static int mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
295 mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
296 int *resid);
298 static int mptsas_alloc_active_slots(mptsas_t *mpt, int flag);
299 static void mptsas_free_active_slots(mptsas_t *mpt);
300 static int mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
302 static void mptsas_restart_hba(mptsas_t *mpt);
303 static void mptsas_restart_waitq(mptsas_t *mpt);
305 static void mptsas_deliver_doneq_thread(mptsas_t *mpt);
306 static void mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd);
307 static void mptsas_doneq_mv(mptsas_t *mpt, uint64_t t);
309 static mptsas_cmd_t *mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t);
310 static void mptsas_doneq_empty(mptsas_t *mpt);
311 static void mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg);
313 static mptsas_cmd_t *mptsas_waitq_rm(mptsas_t *mpt);
314 static void mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd);
315 static mptsas_cmd_t *mptsas_tx_waitq_rm(mptsas_t *mpt);
316 static void mptsas_tx_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd);
319 static void mptsas_start_watch_reset_delay();
320 static void mptsas_setup_bus_reset_delay(mptsas_t *mpt);
321 static void mptsas_watch_reset_delay(void *arg);
322 static int mptsas_watch_reset_delay_subr(mptsas_t *mpt);
325 * helper functions
327 static void mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
329 static dev_info_t *mptsas_find_child(dev_info_t *pdip, char *name);
330 static dev_info_t *mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy);
331 static dev_info_t *mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr,
332 int lun);
333 static mdi_pathinfo_t *mptsas_find_path_addr(dev_info_t *pdip, uint64_t sasaddr,
334 int lun);
335 static mdi_pathinfo_t *mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy);
336 static dev_info_t *mptsas_find_smp_child(dev_info_t *pdip, char *str_wwn);
338 static int mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy,
339 int *lun);
340 static int mptsas_parse_smp_name(char *name, uint64_t *wwn);
342 static mptsas_target_t *mptsas_phy_to_tgt(mptsas_t *mpt,
343 mptsas_phymask_t phymask, uint8_t phy);
344 static mptsas_target_t *mptsas_wwid_to_ptgt(mptsas_t *mpt,
345 mptsas_phymask_t phymask, uint64_t wwid);
346 static mptsas_smp_t *mptsas_wwid_to_psmp(mptsas_t *mpt,
347 mptsas_phymask_t phymask, uint64_t wwid);
349 static int mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun,
350 uchar_t page, unsigned char *buf, int len, int *rlen, uchar_t evpd);
352 static int mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
353 uint16_t *handle, mptsas_target_t **pptgt);
354 static void mptsas_update_phymask(mptsas_t *mpt);
356 static int mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
357 uint32_t *status, uint8_t cmd);
358 static dev_info_t *mptsas_get_dip_from_dev(dev_t dev,
359 mptsas_phymask_t *phymask);
360 static mptsas_target_t *mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr,
361 mptsas_phymask_t phymask);
362 static int mptsas_flush_led_status(mptsas_t *mpt, mptsas_target_t *ptgt);
366 * Enumeration / DR functions
368 static void mptsas_config_all(dev_info_t *pdip);
369 static int mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
370 dev_info_t **lundip);
371 static int mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
372 dev_info_t **lundip);
374 static int mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt);
375 static int mptsas_offline_target(dev_info_t *pdip, char *name);
377 static int mptsas_config_raid(dev_info_t *pdip, uint16_t target,
378 dev_info_t **dip);
380 static int mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt);
381 static int mptsas_probe_lun(dev_info_t *pdip, int lun,
382 dev_info_t **dip, mptsas_target_t *ptgt);
384 static int mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
385 dev_info_t **dip, mptsas_target_t *ptgt, int lun);
387 static int mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
388 char *guid, dev_info_t **dip, mptsas_target_t *ptgt, int lun);
389 static int mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
390 char *guid, dev_info_t **dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt,
391 int lun);
393 static void mptsas_offline_missed_luns(dev_info_t *pdip,
394 uint16_t *repluns, int lun_cnt, mptsas_target_t *ptgt);
395 static int mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
396 mdi_pathinfo_t *rpip, uint_t flags);
398 static int mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn,
399 dev_info_t **smp_dip);
400 static int mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
401 uint_t flags);
403 static int mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data,
404 int mode, int *rval);
405 static int mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data,
406 int mode, int *rval);
407 static int mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data,
408 int mode, int *rval);
409 static void mptsas_record_event(void *args);
410 static int mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data,
411 int mode);
413 mptsas_target_t *mptsas_tgt_alloc(mptsas_t *, uint16_t, uint64_t,
414 uint32_t, mptsas_phymask_t, uint8_t);
415 static mptsas_smp_t *mptsas_smp_alloc(mptsas_t *, mptsas_smp_t *);
416 static int mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
417 dev_info_t **smp_dip);
420 * Power management functions
422 static int mptsas_get_pci_cap(mptsas_t *mpt);
423 static int mptsas_init_pm(mptsas_t *mpt);
426 * MPT MSI tunable:
428 * By default MSI is enabled on all supported platforms.
430 boolean_t mptsas_enable_msi = B_TRUE;
431 boolean_t mptsas_physical_bind_failed_page_83 = B_FALSE;
434 * Global switch for use of MPI2.5 FAST PATH.
435 * We don't really know what FAST PATH actually does, so if it is suspected
436 * to cause problems it can be turned off by setting this variable to B_FALSE.
438 boolean_t mptsas_use_fastpath = B_TRUE;
440 static int mptsas_register_intrs(mptsas_t *);
441 static void mptsas_unregister_intrs(mptsas_t *);
442 static int mptsas_add_intrs(mptsas_t *, int);
443 static void mptsas_rem_intrs(mptsas_t *);
446 * FMA Prototypes
448 static void mptsas_fm_init(mptsas_t *mpt);
449 static void mptsas_fm_fini(mptsas_t *mpt);
450 static int mptsas_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void *);
452 extern pri_t minclsyspri, maxclsyspri;
455 * This device is created by the SCSI pseudo nexus driver (SCSI vHCI). It is
456 * under this device that the paths to a physical device are created when
457 * MPxIO is used.
459 extern dev_info_t *scsi_vhci_dip;
462 * Tunable timeout value for Inquiry VPD page 0x83
463 * By default the value is 30 seconds.
465 int mptsas_inq83_retry_timeout = 30;
468 * This is used to allocate memory for message frame storage, not for
469 * data I/O DMA. All message frames must be stored in the first 4G of
470 * physical memory.
472 ddi_dma_attr_t mptsas_dma_attrs = {
473 DMA_ATTR_V0, /* attribute layout version */
474 0x0ull, /* address low - should be 0 (longlong) */
475 0xffffffffull, /* address high - 32-bit max range */
476 0x00ffffffull, /* count max - max DMA object size */
477 4, /* allocation alignment requirements */
478 0x78, /* burstsizes - binary encoded values */
479 1, /* minxfer - gran. of DMA engine */
480 0x00ffffffull, /* maxxfer - gran. of DMA engine */
481 0xffffffffull, /* max segment size (DMA boundary) */
482 MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length */
483 512, /* granularity - device transfer size */
484 0 /* flags, set to 0 */
488 * This is used for data I/O DMA memory allocation. (full 64-bit DMA
489 * physical addresses are supported.)
491 ddi_dma_attr_t mptsas_dma_attrs64 = {
492 DMA_ATTR_V0, /* attribute layout version */
493 0x0ull, /* address low - should be 0 (longlong) */
494 0xffffffffffffffffull, /* address high - 64-bit max */
495 0x00ffffffull, /* count max - max DMA object size */
496 4, /* allocation alignment requirements */
497 0x78, /* burstsizes - binary encoded values */
498 1, /* minxfer - gran. of DMA engine */
499 0x00ffffffull, /* maxxfer - gran. of DMA engine */
500 0xffffffffull, /* max segment size (DMA boundary) */
501 MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length */
502 512, /* granularity - device transfer size */
503 0 /* flags, set to 0 */
506 ddi_device_acc_attr_t mptsas_dev_attr = {
507 DDI_DEVICE_ATTR_V1,
508 DDI_STRUCTURE_LE_ACC,
509 DDI_STRICTORDER_ACC,
510 DDI_DEFAULT_ACC
513 static struct cb_ops mptsas_cb_ops = {
514 scsi_hba_open, /* open */
515 scsi_hba_close, /* close */
516 nodev, /* strategy */
517 nodev, /* print */
518 nodev, /* dump */
519 nodev, /* read */
520 nodev, /* write */
521 mptsas_ioctl, /* ioctl */
522 nodev, /* devmap */
523 nodev, /* mmap */
524 nodev, /* segmap */
525 nochpoll, /* chpoll */
526 ddi_prop_op, /* cb_prop_op */
527 NULL, /* streamtab */
528 D_MP, /* cb_flag */
529 CB_REV, /* rev */
530 nodev, /* aread */
531 nodev /* awrite */
534 static struct dev_ops mptsas_ops = {
535 DEVO_REV, /* devo_rev, */
536 0, /* refcnt */
537 ddi_no_info, /* info */
538 nulldev, /* identify */
539 nulldev, /* probe */
540 mptsas_attach, /* attach */
541 mptsas_detach, /* detach */
542 #ifdef __sparc
543 mptsas_reset,
544 #else
545 nodev, /* reset */
546 #endif /* __sparc */
547 &mptsas_cb_ops, /* driver operations */
548 NULL, /* bus operations */
549 mptsas_power, /* power management */
550 #ifdef __sparc
551 ddi_quiesce_not_needed
552 #else
553 mptsas_quiesce /* quiesce */
554 #endif /* __sparc */
558 #define MPTSAS_MOD_STRING "MPTSAS HBA Driver 00.00.00.24"
560 static struct modldrv modldrv = {
561 &mod_driverops, /* Type of module. This one is a driver */
562 MPTSAS_MOD_STRING, /* Name of the module. */
563 &mptsas_ops, /* driver ops */
566 static struct modlinkage modlinkage = {
567 MODREV_1, &modldrv, NULL
569 #define TARGET_PROP "target"
570 #define LUN_PROP "lun"
571 #define LUN64_PROP "lun64"
572 #define SAS_PROP "sas-mpt"
573 #define MDI_GUID "wwn"
574 #define NDI_GUID "guid"
575 #define MPTSAS_DEV_GONE "mptsas_dev_gone"
578 * Local static data
580 #if defined(MPTSAS_DEBUG)
582 * Flags to indicate which debug messages are to be printed and which go to the
583 * debug log ring buffer. Default is to not print anything, and to log
584 * everything except the watchsubr() output which normally happens every second.
586 uint32_t mptsas_debugprt_flags = 0x0;
587 uint32_t mptsas_debuglog_flags = ~(1U << 30);
588 #endif /* defined(MPTSAS_DEBUG) */
589 uint32_t mptsas_debug_resets = 0;
591 static kmutex_t mptsas_global_mutex;
592 static void *mptsas_state; /* soft state ptr */
593 static krwlock_t mptsas_global_rwlock;
595 static kmutex_t mptsas_log_mutex;
596 static char mptsas_log_buf[256];
597 _NOTE(MUTEX_PROTECTS_DATA(mptsas_log_mutex, mptsas_log_buf))
599 static mptsas_t *mptsas_head, *mptsas_tail;
600 static clock_t mptsas_scsi_watchdog_tick;
601 static clock_t mptsas_tick;
602 static timeout_id_t mptsas_reset_watch;
603 static timeout_id_t mptsas_timeout_id;
604 static int mptsas_timeouts_enabled = 0;
607 * Default length for extended auto request sense buffers.
608 * All sense buffers need to be under the same alloc because there
609 * is only one common top 32bits (of 64bits) address register.
610 * Most requests only require 32 bytes, but some request >256.
611 * We use rmalloc()/rmfree() on this additional memory to manage the
612 * "extended" requests.
614 int mptsas_extreq_sense_bufsize = 256*64;
617 * We believe that all software resrictions of having to run with DMA
618 * attributes to limit allocation to the first 4G are removed.
619 * However, this flag remains to enable quick switchback should suspicious
620 * problems emerge.
621 * Note that scsi_alloc_consistent_buf() does still adhere to allocating
622 * 32 bit addressable memory, but we can cope if that is changed now.
624 int mptsas_use_64bit_msgaddr = 1;
627 * warlock directives
629 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_pkt \
630 mptsas_cmd NcrTableIndirect buf scsi_cdb scsi_status))
631 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", smp_pkt))
632 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device scsi_address))
633 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", mptsas_tgt_private))
634 _NOTE(SCHEME_PROTECTS_DATA("No Mutex Needed", scsi_hba_tran::tran_tgt_private))
637 * SM - HBA statics
639 char *mptsas_driver_rev = MPTSAS_MOD_STRING;
641 #ifdef MPTSAS_DEBUG
642 void debug_enter(char *);
643 #endif
646 * Notes:
647 * - scsi_hba_init(9F) initializes SCSI HBA modules
648 * - must call scsi_hba_fini(9F) if modload() fails
651 _init(void)
653 int status;
654 /* CONSTCOND */
655 ASSERT(NO_COMPETING_THREADS);
657 NDBG0(("_init"));
659 status = ddi_soft_state_init(&mptsas_state, MPTSAS_SIZE,
660 MPTSAS_INITIAL_SOFT_SPACE);
661 if (status != 0) {
662 return (status);
665 if ((status = scsi_hba_init(&modlinkage)) != 0) {
666 ddi_soft_state_fini(&mptsas_state);
667 return (status);
670 mutex_init(&mptsas_global_mutex, NULL, MUTEX_DRIVER, NULL);
671 rw_init(&mptsas_global_rwlock, NULL, RW_DRIVER, NULL);
672 mutex_init(&mptsas_log_mutex, NULL, MUTEX_DRIVER, NULL);
674 if ((status = mod_install(&modlinkage)) != 0) {
675 mutex_destroy(&mptsas_log_mutex);
676 rw_destroy(&mptsas_global_rwlock);
677 mutex_destroy(&mptsas_global_mutex);
678 ddi_soft_state_fini(&mptsas_state);
679 scsi_hba_fini(&modlinkage);
682 return (status);
686 * Notes:
687 * - scsi_hba_fini(9F) uninitializes SCSI HBA modules
690 _fini(void)
692 int status;
693 /* CONSTCOND */
694 ASSERT(NO_COMPETING_THREADS);
696 NDBG0(("_fini"));
698 if ((status = mod_remove(&modlinkage)) == 0) {
699 ddi_soft_state_fini(&mptsas_state);
700 scsi_hba_fini(&modlinkage);
701 mutex_destroy(&mptsas_global_mutex);
702 rw_destroy(&mptsas_global_rwlock);
703 mutex_destroy(&mptsas_log_mutex);
705 return (status);
709 * The loadable-module _info(9E) entry point
712 _info(struct modinfo *modinfop)
714 /* CONSTCOND */
715 ASSERT(NO_COMPETING_THREADS);
716 NDBG0(("mptsas _info"));
718 return (mod_info(&modlinkage, modinfop));
721 static int
722 mptsas_target_eval_devhdl(const void *op, void *arg)
724 uint16_t dh = *(uint16_t *)arg;
725 const mptsas_target_t *tp = op;
727 return ((int)tp->m_devhdl - (int)dh);
730 static int
731 mptsas_target_eval_slot(const void *op, void *arg)
733 mptsas_led_control_t *lcp = arg;
734 const mptsas_target_t *tp = op;
736 if (tp->m_enclosure != lcp->Enclosure)
737 return ((int)tp->m_enclosure - (int)lcp->Enclosure);
739 return ((int)tp->m_slot_num - (int)lcp->Slot);
742 static int
743 mptsas_target_eval_nowwn(const void *op, void *arg)
745 uint8_t phy = *(uint8_t *)arg;
746 const mptsas_target_t *tp = op;
748 if (tp->m_addr.mta_wwn != 0)
749 return (-1);
751 return ((int)tp->m_phynum - (int)phy);
754 static int
755 mptsas_smp_eval_devhdl(const void *op, void *arg)
757 uint16_t dh = *(uint16_t *)arg;
758 const mptsas_smp_t *sp = op;
760 return ((int)sp->m_devhdl - (int)dh);
763 static uint64_t
764 mptsas_target_addr_hash(const void *tp)
766 const mptsas_target_addr_t *tap = tp;
768 return ((tap->mta_wwn & 0xffffffffffffULL) |
769 ((uint64_t)tap->mta_phymask << 48));
772 static int
773 mptsas_target_addr_cmp(const void *a, const void *b)
775 const mptsas_target_addr_t *aap = a;
776 const mptsas_target_addr_t *bap = b;
778 if (aap->mta_wwn < bap->mta_wwn)
779 return (-1);
780 if (aap->mta_wwn > bap->mta_wwn)
781 return (1);
782 return ((int)bap->mta_phymask - (int)aap->mta_phymask);
785 static void
786 mptsas_target_free(void *op)
788 kmem_free(op, sizeof (mptsas_target_t));
791 static void
792 mptsas_smp_free(void *op)
794 kmem_free(op, sizeof (mptsas_smp_t));
797 static void
798 mptsas_destroy_hashes(mptsas_t *mpt)
800 mptsas_target_t *tp;
801 mptsas_smp_t *sp;
803 for (tp = refhash_first(mpt->m_targets); tp != NULL;
804 tp = refhash_next(mpt->m_targets, tp)) {
805 refhash_remove(mpt->m_targets, tp);
807 for (sp = refhash_first(mpt->m_smp_targets); sp != NULL;
808 sp = refhash_next(mpt->m_smp_targets, sp)) {
809 refhash_remove(mpt->m_smp_targets, sp);
811 refhash_destroy(mpt->m_targets);
812 refhash_destroy(mpt->m_smp_targets);
813 mpt->m_targets = NULL;
814 mpt->m_smp_targets = NULL;
817 static int
818 mptsas_iport_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
820 dev_info_t *pdip;
821 mptsas_t *mpt;
822 scsi_hba_tran_t *hba_tran;
823 char *iport = NULL;
824 char phymask[MPTSAS_MAX_PHYS];
825 mptsas_phymask_t phy_mask = 0;
826 int dynamic_port = 0;
827 uint32_t page_address;
828 char initiator_wwnstr[MPTSAS_WWN_STRLEN];
829 int rval = DDI_FAILURE;
830 int i = 0;
831 uint8_t numphys = 0;
832 uint8_t phy_id;
833 uint8_t phy_port = 0;
834 uint16_t attached_devhdl = 0;
835 uint32_t dev_info;
836 uint64_t attached_sas_wwn;
837 uint16_t dev_hdl;
838 uint16_t pdev_hdl;
839 uint16_t bay_num, enclosure, io_flags;
840 char attached_wwnstr[MPTSAS_WWN_STRLEN];
842 /* CONSTCOND */
843 ASSERT(NO_COMPETING_THREADS);
845 switch (cmd) {
846 case DDI_ATTACH:
847 break;
849 case DDI_RESUME:
851 * If this a scsi-iport node, nothing to do here.
853 return (DDI_SUCCESS);
855 default:
856 return (DDI_FAILURE);
859 pdip = ddi_get_parent(dip);
861 if ((hba_tran = ndi_flavorv_get(pdip, SCSA_FLAVOR_SCSI_DEVICE)) ==
862 NULL) {
863 cmn_err(CE_WARN, "Failed attach iport because fail to "
864 "get tran vector for the HBA node");
865 return (DDI_FAILURE);
868 mpt = TRAN2MPT(hba_tran);
869 ASSERT(mpt != NULL);
870 if (mpt == NULL)
871 return (DDI_FAILURE);
873 if ((hba_tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) ==
874 NULL) {
875 mptsas_log(mpt, CE_WARN, "Failed attach iport because fail to "
876 "get tran vector for the iport node");
877 return (DDI_FAILURE);
881 * Overwrite parent's tran_hba_private to iport's tran vector
883 hba_tran->tran_hba_private = mpt;
885 ddi_report_dev(dip);
888 * Get SAS address for initiator port according dev_handle
890 iport = ddi_get_name_addr(dip);
891 if (iport && strncmp(iport, "v0", 2) == 0) {
892 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
893 MPTSAS_VIRTUAL_PORT, 1) !=
894 DDI_PROP_SUCCESS) {
895 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
896 MPTSAS_VIRTUAL_PORT);
897 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
898 "prop update failed");
899 return (DDI_FAILURE);
901 return (DDI_SUCCESS);
904 mutex_enter(&mpt->m_mutex);
905 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
906 bzero(phymask, sizeof (phymask));
907 (void) sprintf(phymask,
908 "%x", mpt->m_phy_info[i].phy_mask);
909 if (strcmp(phymask, iport) == 0) {
910 break;
914 if (i == MPTSAS_MAX_PHYS) {
915 mptsas_log(mpt, CE_WARN, "Failed attach port %s because port"
916 "seems not exist", iport);
917 mutex_exit(&mpt->m_mutex);
918 return (DDI_FAILURE);
921 phy_mask = mpt->m_phy_info[i].phy_mask;
923 if (mpt->m_phy_info[i].port_flags & AUTO_PORT_CONFIGURATION)
924 dynamic_port = 1;
925 else
926 dynamic_port = 0;
929 * Update PHY info for smhba
931 if (mptsas_smhba_phy_init(mpt)) {
932 mutex_exit(&mpt->m_mutex);
933 mptsas_log(mpt, CE_WARN, "mptsas phy update "
934 "failed");
935 return (DDI_FAILURE);
938 mutex_exit(&mpt->m_mutex);
940 numphys = 0;
941 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
942 if ((phy_mask >> i) & 0x01) {
943 numphys++;
947 bzero(initiator_wwnstr, sizeof (initiator_wwnstr));
948 (void) sprintf(initiator_wwnstr, "w%016"PRIx64,
949 mpt->un.m_base_wwid);
951 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
952 SCSI_ADDR_PROP_INITIATOR_PORT, initiator_wwnstr) !=
953 DDI_PROP_SUCCESS) {
954 (void) ddi_prop_remove(DDI_DEV_T_NONE,
955 dip, SCSI_ADDR_PROP_INITIATOR_PORT);
956 mptsas_log(mpt, CE_WARN, "mptsas Initiator port "
957 "prop update failed");
958 return (DDI_FAILURE);
960 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
961 MPTSAS_NUM_PHYS, numphys) !=
962 DDI_PROP_SUCCESS) {
963 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, MPTSAS_NUM_PHYS);
964 return (DDI_FAILURE);
967 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
968 "phymask", phy_mask) !=
969 DDI_PROP_SUCCESS) {
970 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "phymask");
971 mptsas_log(mpt, CE_WARN, "mptsas phy mask "
972 "prop update failed");
973 return (DDI_FAILURE);
976 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
977 "dynamic-port", dynamic_port) !=
978 DDI_PROP_SUCCESS) {
979 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "dynamic-port");
980 mptsas_log(mpt, CE_WARN, "mptsas dynamic port "
981 "prop update failed");
982 return (DDI_FAILURE);
984 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
985 MPTSAS_VIRTUAL_PORT, 0) !=
986 DDI_PROP_SUCCESS) {
987 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
988 MPTSAS_VIRTUAL_PORT);
989 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
990 "prop update failed");
991 return (DDI_FAILURE);
993 mptsas_smhba_set_all_phy_props(mpt, dip, numphys, phy_mask,
994 &attached_devhdl);
996 mutex_enter(&mpt->m_mutex);
997 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
998 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)attached_devhdl;
999 rval = mptsas_get_sas_device_page0(mpt, page_address, &dev_hdl,
1000 &attached_sas_wwn, &dev_info, &phy_port, &phy_id,
1001 &pdev_hdl, &bay_num, &enclosure, &io_flags);
1002 if (rval != DDI_SUCCESS) {
1003 mptsas_log(mpt, CE_WARN,
1004 "Failed to get device page0 for handle:%d",
1005 attached_devhdl);
1006 mutex_exit(&mpt->m_mutex);
1007 return (DDI_FAILURE);
1010 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1011 bzero(phymask, sizeof (phymask));
1012 (void) sprintf(phymask, "%x", mpt->m_phy_info[i].phy_mask);
1013 if (strcmp(phymask, iport) == 0) {
1014 (void) sprintf(&mpt->m_phy_info[i].smhba_info.path[0],
1015 "%x",
1016 mpt->m_phy_info[i].phy_mask);
1019 mutex_exit(&mpt->m_mutex);
1021 bzero(attached_wwnstr, sizeof (attached_wwnstr));
1022 (void) sprintf(attached_wwnstr, "w%016"PRIx64,
1023 attached_sas_wwn);
1024 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
1025 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
1026 DDI_PROP_SUCCESS) {
1027 (void) ddi_prop_remove(DDI_DEV_T_NONE,
1028 dip, SCSI_ADDR_PROP_ATTACHED_PORT);
1029 return (DDI_FAILURE);
1032 /* Create kstats for each phy on this iport */
1034 mptsas_create_phy_stats(mpt, iport, dip);
1037 * register sas hba iport with mdi (MPxIO/vhci)
1039 if (mdi_phci_register(MDI_HCI_CLASS_SCSI,
1040 dip, 0) == MDI_SUCCESS) {
1041 mpt->m_mpxio_enable = TRUE;
1043 return (DDI_SUCCESS);
1047 * Notes:
1048 * Set up all device state and allocate data structures,
1049 * mutexes, condition variables, etc. for device operation.
1050 * Add interrupts needed.
1051 * Return DDI_SUCCESS if device is ready, else return DDI_FAILURE.
1053 static int
1054 mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1056 mptsas_t *mpt = NULL;
1057 int instance, i, j;
1058 int doneq_thread_num;
1059 char intr_added = 0;
1060 char map_setup = 0;
1061 char config_setup = 0;
1062 char hba_attach_setup = 0;
1063 char smp_attach_setup = 0;
1064 char mutex_init_done = 0;
1065 char event_taskq_create = 0;
1066 char dr_taskq_create = 0;
1067 char doneq_thread_create = 0;
1068 char added_watchdog = 0;
1069 scsi_hba_tran_t *hba_tran;
1070 uint_t mem_bar = MEM_SPACE;
1071 int rval = DDI_FAILURE;
1073 /* CONSTCOND */
1074 ASSERT(NO_COMPETING_THREADS);
1076 if (scsi_hba_iport_unit_address(dip)) {
1077 return (mptsas_iport_attach(dip, cmd));
1080 switch (cmd) {
1081 case DDI_ATTACH:
1082 break;
1084 case DDI_RESUME:
1085 if ((hba_tran = ddi_get_driver_private(dip)) == NULL)
1086 return (DDI_FAILURE);
1088 mpt = TRAN2MPT(hba_tran);
1090 if (!mpt) {
1091 return (DDI_FAILURE);
1095 * Reset hardware and softc to "no outstanding commands"
1096 * Note that a check condition can result on first command
1097 * to a target.
1099 mutex_enter(&mpt->m_mutex);
1102 * raise power.
1104 if (mpt->m_options & MPTSAS_OPT_PM) {
1105 mutex_exit(&mpt->m_mutex);
1106 (void) pm_busy_component(dip, 0);
1107 rval = pm_power_has_changed(dip, 0, PM_LEVEL_D0);
1108 if (rval == DDI_SUCCESS) {
1109 mutex_enter(&mpt->m_mutex);
1110 } else {
1112 * The pm_raise_power() call above failed,
1113 * and that can only occur if we were unable
1114 * to reset the hardware. This is probably
1115 * due to unhealty hardware, and because
1116 * important filesystems(such as the root
1117 * filesystem) could be on the attached disks,
1118 * it would not be a good idea to continue,
1119 * as we won't be entirely certain we are
1120 * writing correct data. So we panic() here
1121 * to not only prevent possible data corruption,
1122 * but to give developers or end users a hope
1123 * of identifying and correcting any problems.
1125 fm_panic("mptsas could not reset hardware "
1126 "during resume");
1130 mpt->m_suspended = 0;
1133 * Reinitialize ioc
1135 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1136 if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
1137 mutex_exit(&mpt->m_mutex);
1138 if (mpt->m_options & MPTSAS_OPT_PM) {
1139 (void) pm_idle_component(dip, 0);
1141 fm_panic("mptsas init chip fail during resume");
1144 * mptsas_update_driver_data needs interrupts so enable them
1145 * first.
1147 MPTSAS_ENABLE_INTR(mpt);
1148 mptsas_update_driver_data(mpt);
1150 /* start requests, if possible */
1151 mptsas_restart_hba(mpt);
1153 mutex_exit(&mpt->m_mutex);
1156 * Restart watch thread
1158 mutex_enter(&mptsas_global_mutex);
1159 if (mptsas_timeout_id == 0) {
1160 mptsas_timeout_id = timeout(mptsas_watch, NULL,
1161 mptsas_tick);
1162 mptsas_timeouts_enabled = 1;
1164 mutex_exit(&mptsas_global_mutex);
1166 /* report idle status to pm framework */
1167 if (mpt->m_options & MPTSAS_OPT_PM) {
1168 (void) pm_idle_component(dip, 0);
1171 return (DDI_SUCCESS);
1173 default:
1174 return (DDI_FAILURE);
1178 instance = ddi_get_instance(dip);
1181 * Allocate softc information.
1183 if (ddi_soft_state_zalloc(mptsas_state, instance) != DDI_SUCCESS) {
1184 mptsas_log(NULL, CE_WARN,
1185 "mptsas%d: cannot allocate soft state", instance);
1186 goto fail;
1189 mpt = ddi_get_soft_state(mptsas_state, instance);
1191 if (mpt == NULL) {
1192 mptsas_log(NULL, CE_WARN,
1193 "mptsas%d: cannot get soft state", instance);
1194 goto fail;
1197 /* Indicate that we are 'sizeof (scsi_*(9S))' clean. */
1198 scsi_size_clean(dip);
1200 mpt->m_dip = dip;
1201 mpt->m_instance = instance;
1203 /* Make a per-instance copy of the structures */
1204 mpt->m_io_dma_attr = mptsas_dma_attrs64;
1205 if (mptsas_use_64bit_msgaddr) {
1206 mpt->m_msg_dma_attr = mptsas_dma_attrs64;
1207 } else {
1208 mpt->m_msg_dma_attr = mptsas_dma_attrs;
1210 mpt->m_reg_acc_attr = mptsas_dev_attr;
1211 mpt->m_dev_acc_attr = mptsas_dev_attr;
1214 * Size of individual request sense buffer
1216 mpt->m_req_sense_size = EXTCMDS_STATUS_SIZE;
1219 * Initialize FMA
1221 mpt->m_fm_capabilities = ddi_getprop(DDI_DEV_T_ANY, mpt->m_dip,
1222 DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
1223 DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
1224 DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
1226 mptsas_fm_init(mpt);
1228 if (mptsas_alloc_handshake_msg(mpt,
1229 sizeof (Mpi2SCSITaskManagementRequest_t)) == DDI_FAILURE) {
1230 mptsas_log(mpt, CE_WARN, "cannot initialize handshake msg.");
1231 goto fail;
1235 * Setup configuration space
1237 if (mptsas_config_space_init(mpt) == FALSE) {
1238 mptsas_log(mpt, CE_WARN, "mptsas_config_space_init failed");
1239 goto fail;
1241 config_setup++;
1243 if (ddi_regs_map_setup(dip, mem_bar, (caddr_t *)&mpt->m_reg,
1244 0, 0, &mpt->m_reg_acc_attr, &mpt->m_datap) != DDI_SUCCESS) {
1245 mptsas_log(mpt, CE_WARN, "map setup failed");
1246 goto fail;
1248 map_setup++;
1251 * A taskq is created for dealing with the event handler
1253 if ((mpt->m_event_taskq = ddi_taskq_create(dip, "mptsas_event_taskq",
1254 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1255 mptsas_log(mpt, CE_NOTE, "ddi_taskq_create failed");
1256 goto fail;
1258 event_taskq_create++;
1261 * A taskq is created for dealing with dr events
1263 if ((mpt->m_dr_taskq = ddi_taskq_create(dip,
1264 "mptsas_dr_taskq",
1265 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1266 mptsas_log(mpt, CE_NOTE, "ddi_taskq_create for discovery "
1267 "failed");
1268 goto fail;
1270 dr_taskq_create++;
1272 mpt->m_doneq_thread_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1273 0, "mptsas_doneq_thread_threshold_prop", 10);
1274 mpt->m_doneq_length_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1275 0, "mptsas_doneq_length_threshold_prop", 8);
1276 mpt->m_doneq_thread_n = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1277 0, "mptsas_doneq_thread_n_prop", 8);
1279 if (mpt->m_doneq_thread_n) {
1280 cv_init(&mpt->m_doneq_thread_cv, NULL, CV_DRIVER, NULL);
1281 mutex_init(&mpt->m_doneq_mutex, NULL, MUTEX_DRIVER, NULL);
1283 mutex_enter(&mpt->m_doneq_mutex);
1284 mpt->m_doneq_thread_id =
1285 kmem_zalloc(sizeof (mptsas_doneq_thread_list_t)
1286 * mpt->m_doneq_thread_n, KM_SLEEP);
1288 for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1289 cv_init(&mpt->m_doneq_thread_id[j].cv, NULL,
1290 CV_DRIVER, NULL);
1291 mutex_init(&mpt->m_doneq_thread_id[j].mutex, NULL,
1292 MUTEX_DRIVER, NULL);
1293 mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1294 mpt->m_doneq_thread_id[j].flag |=
1295 MPTSAS_DONEQ_THREAD_ACTIVE;
1296 mpt->m_doneq_thread_id[j].arg.mpt = mpt;
1297 mpt->m_doneq_thread_id[j].arg.t = j;
1298 mpt->m_doneq_thread_id[j].threadp =
1299 thread_create(NULL, 0, mptsas_doneq_thread,
1300 &mpt->m_doneq_thread_id[j].arg,
1301 0, &p0, TS_RUN, minclsyspri);
1302 mpt->m_doneq_thread_id[j].donetail =
1303 &mpt->m_doneq_thread_id[j].doneq;
1304 mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1306 mutex_exit(&mpt->m_doneq_mutex);
1307 doneq_thread_create++;
1311 * Disable hardware interrupt since we're not ready to
1312 * handle it yet.
1314 MPTSAS_DISABLE_INTR(mpt);
1315 if (mptsas_register_intrs(mpt) == FALSE)
1316 goto fail;
1317 intr_added++;
1319 /* Initialize mutex used in interrupt handler */
1320 mutex_init(&mpt->m_mutex, NULL, MUTEX_DRIVER,
1321 DDI_INTR_PRI(mpt->m_intr_pri));
1322 mutex_init(&mpt->m_passthru_mutex, NULL, MUTEX_DRIVER, NULL);
1323 mutex_init(&mpt->m_tx_waitq_mutex, NULL, MUTEX_DRIVER,
1324 DDI_INTR_PRI(mpt->m_intr_pri));
1325 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1326 mutex_init(&mpt->m_phy_info[i].smhba_info.phy_mutex,
1327 NULL, MUTEX_DRIVER,
1328 DDI_INTR_PRI(mpt->m_intr_pri));
1331 cv_init(&mpt->m_cv, NULL, CV_DRIVER, NULL);
1332 cv_init(&mpt->m_passthru_cv, NULL, CV_DRIVER, NULL);
1333 cv_init(&mpt->m_fw_cv, NULL, CV_DRIVER, NULL);
1334 cv_init(&mpt->m_config_cv, NULL, CV_DRIVER, NULL);
1335 cv_init(&mpt->m_fw_diag_cv, NULL, CV_DRIVER, NULL);
1336 mutex_init_done++;
1338 mutex_enter(&mpt->m_mutex);
1340 * Initialize power management component
1342 if (mpt->m_options & MPTSAS_OPT_PM) {
1343 if (mptsas_init_pm(mpt)) {
1344 mutex_exit(&mpt->m_mutex);
1345 mptsas_log(mpt, CE_WARN, "mptsas pm initialization "
1346 "failed");
1347 goto fail;
1352 * Initialize chip using Message Unit Reset, if allowed
1354 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1355 if (mptsas_init_chip(mpt, TRUE) == DDI_FAILURE) {
1356 mutex_exit(&mpt->m_mutex);
1357 mptsas_log(mpt, CE_WARN, "mptsas chip initialization failed");
1358 goto fail;
1362 * Fill in the phy_info structure and get the base WWID
1364 if (mptsas_get_manufacture_page5(mpt) == DDI_FAILURE) {
1365 mptsas_log(mpt, CE_WARN,
1366 "mptsas_get_manufacture_page5 failed!");
1367 goto fail;
1370 if (mptsas_get_sas_io_unit_page_hndshk(mpt)) {
1371 mptsas_log(mpt, CE_WARN,
1372 "mptsas_get_sas_io_unit_page_hndshk failed!");
1373 goto fail;
1376 if (mptsas_get_manufacture_page0(mpt) == DDI_FAILURE) {
1377 mptsas_log(mpt, CE_WARN,
1378 "mptsas_get_manufacture_page0 failed!");
1379 goto fail;
1382 mutex_exit(&mpt->m_mutex);
1385 * Register the iport for multiple port HBA
1387 mptsas_iport_register(mpt);
1390 * initialize SCSI HBA transport structure
1392 if (mptsas_hba_setup(mpt) == FALSE)
1393 goto fail;
1394 hba_attach_setup++;
1396 if (mptsas_smp_setup(mpt) == FALSE)
1397 goto fail;
1398 smp_attach_setup++;
1400 if (mptsas_cache_create(mpt) == FALSE)
1401 goto fail;
1403 mpt->m_scsi_reset_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
1404 dip, 0, "scsi-reset-delay", SCSI_DEFAULT_RESET_DELAY);
1405 if (mpt->m_scsi_reset_delay == 0) {
1406 mptsas_log(mpt, CE_NOTE,
1407 "scsi_reset_delay of 0 is not recommended,"
1408 " resetting to SCSI_DEFAULT_RESET_DELAY\n");
1409 mpt->m_scsi_reset_delay = SCSI_DEFAULT_RESET_DELAY;
1413 * Initialize the wait and done FIFO queue
1415 mpt->m_donetail = &mpt->m_doneq;
1416 mpt->m_waitqtail = &mpt->m_waitq;
1417 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
1418 mpt->m_tx_draining = 0;
1421 * ioc cmd queue initialize
1423 mpt->m_ioc_event_cmdtail = &mpt->m_ioc_event_cmdq;
1424 mpt->m_dev_handle = 0xFFFF;
1426 MPTSAS_ENABLE_INTR(mpt);
1429 * enable event notification
1431 mutex_enter(&mpt->m_mutex);
1432 if (mptsas_ioc_enable_event_notification(mpt)) {
1433 mutex_exit(&mpt->m_mutex);
1434 goto fail;
1436 mutex_exit(&mpt->m_mutex);
1439 * used for mptsas_watch
1441 mptsas_list_add(mpt);
1443 mutex_enter(&mptsas_global_mutex);
1444 if (mptsas_timeouts_enabled == 0) {
1445 mptsas_scsi_watchdog_tick = ddi_prop_get_int(DDI_DEV_T_ANY,
1446 dip, 0, "scsi-watchdog-tick", DEFAULT_WD_TICK);
1448 mptsas_tick = mptsas_scsi_watchdog_tick *
1449 drv_usectohz((clock_t)1000000);
1451 mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
1452 mptsas_timeouts_enabled = 1;
1454 mutex_exit(&mptsas_global_mutex);
1455 added_watchdog++;
1458 * Initialize PHY info for smhba.
1459 * This requires watchdog to be enabled otherwise if interrupts
1460 * don't work the system will hang.
1462 if (mptsas_smhba_setup(mpt)) {
1463 mptsas_log(mpt, CE_WARN, "mptsas phy initialization "
1464 "failed");
1465 goto fail;
1468 /* Check all dma handles allocated in attach */
1469 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl)
1470 != DDI_SUCCESS) ||
1471 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl)
1472 != DDI_SUCCESS) ||
1473 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl)
1474 != DDI_SUCCESS) ||
1475 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl)
1476 != DDI_SUCCESS) ||
1477 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl)
1478 != DDI_SUCCESS) ||
1479 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl)
1480 != DDI_SUCCESS)) {
1481 goto fail;
1484 /* Check all acc handles allocated in attach */
1485 if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
1486 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl)
1487 != DDI_SUCCESS) ||
1488 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl)
1489 != DDI_SUCCESS) ||
1490 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl)
1491 != DDI_SUCCESS) ||
1492 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl)
1493 != DDI_SUCCESS) ||
1494 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl)
1495 != DDI_SUCCESS) ||
1496 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl)
1497 != DDI_SUCCESS) ||
1498 (mptsas_check_acc_handle(mpt->m_config_handle)
1499 != DDI_SUCCESS)) {
1500 goto fail;
1504 * After this point, we are not going to fail the attach.
1507 /* Print message of HBA present */
1508 ddi_report_dev(dip);
1510 /* report idle status to pm framework */
1511 if (mpt->m_options & MPTSAS_OPT_PM) {
1512 (void) pm_idle_component(dip, 0);
1515 return (DDI_SUCCESS);
1517 fail:
1518 mptsas_log(mpt, CE_WARN, "attach failed");
1519 mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
1520 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
1521 if (mpt) {
1522 /* deallocate in reverse order */
1523 if (added_watchdog) {
1524 mptsas_list_del(mpt);
1525 mutex_enter(&mptsas_global_mutex);
1527 if (mptsas_timeout_id && (mptsas_head == NULL)) {
1528 timeout_id_t tid = mptsas_timeout_id;
1529 mptsas_timeouts_enabled = 0;
1530 mptsas_timeout_id = 0;
1531 mutex_exit(&mptsas_global_mutex);
1532 (void) untimeout(tid);
1533 mutex_enter(&mptsas_global_mutex);
1535 mutex_exit(&mptsas_global_mutex);
1538 mptsas_cache_destroy(mpt);
1540 if (smp_attach_setup) {
1541 mptsas_smp_teardown(mpt);
1543 if (hba_attach_setup) {
1544 mptsas_hba_teardown(mpt);
1547 if (mpt->m_targets)
1548 refhash_destroy(mpt->m_targets);
1549 if (mpt->m_smp_targets)
1550 refhash_destroy(mpt->m_smp_targets);
1552 if (mpt->m_active) {
1553 mptsas_free_active_slots(mpt);
1555 if (intr_added) {
1556 mptsas_unregister_intrs(mpt);
1559 if (doneq_thread_create) {
1560 mutex_enter(&mpt->m_doneq_mutex);
1561 doneq_thread_num = mpt->m_doneq_thread_n;
1562 for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1563 mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1564 mpt->m_doneq_thread_id[j].flag &=
1565 (~MPTSAS_DONEQ_THREAD_ACTIVE);
1566 cv_signal(&mpt->m_doneq_thread_id[j].cv);
1567 mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1569 while (mpt->m_doneq_thread_n) {
1570 cv_wait(&mpt->m_doneq_thread_cv,
1571 &mpt->m_doneq_mutex);
1573 for (j = 0; j < doneq_thread_num; j++) {
1574 cv_destroy(&mpt->m_doneq_thread_id[j].cv);
1575 mutex_destroy(&mpt->m_doneq_thread_id[j].mutex);
1577 kmem_free(mpt->m_doneq_thread_id,
1578 sizeof (mptsas_doneq_thread_list_t)
1579 * doneq_thread_num);
1580 mutex_exit(&mpt->m_doneq_mutex);
1581 cv_destroy(&mpt->m_doneq_thread_cv);
1582 mutex_destroy(&mpt->m_doneq_mutex);
1584 if (event_taskq_create) {
1585 ddi_taskq_destroy(mpt->m_event_taskq);
1587 if (dr_taskq_create) {
1588 ddi_taskq_destroy(mpt->m_dr_taskq);
1590 if (mutex_init_done) {
1591 mutex_destroy(&mpt->m_tx_waitq_mutex);
1592 mutex_destroy(&mpt->m_passthru_mutex);
1593 mutex_destroy(&mpt->m_mutex);
1594 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1595 mutex_destroy(
1596 &mpt->m_phy_info[i].smhba_info.phy_mutex);
1598 cv_destroy(&mpt->m_cv);
1599 cv_destroy(&mpt->m_passthru_cv);
1600 cv_destroy(&mpt->m_fw_cv);
1601 cv_destroy(&mpt->m_config_cv);
1602 cv_destroy(&mpt->m_fw_diag_cv);
1605 if (map_setup) {
1606 mptsas_cfg_fini(mpt);
1608 if (config_setup) {
1609 mptsas_config_space_fini(mpt);
1611 mptsas_free_handshake_msg(mpt);
1612 mptsas_hba_fini(mpt);
1614 mptsas_fm_fini(mpt);
1615 ddi_soft_state_free(mptsas_state, instance);
1616 ddi_prop_remove_all(dip);
1618 return (DDI_FAILURE);
1621 static int
1622 mptsas_suspend(dev_info_t *devi)
1624 mptsas_t *mpt, *g;
1625 scsi_hba_tran_t *tran;
1627 if (scsi_hba_iport_unit_address(devi)) {
1628 return (DDI_SUCCESS);
1631 if ((tran = ddi_get_driver_private(devi)) == NULL)
1632 return (DDI_SUCCESS);
1634 mpt = TRAN2MPT(tran);
1635 if (!mpt) {
1636 return (DDI_SUCCESS);
1639 mutex_enter(&mpt->m_mutex);
1641 if (mpt->m_suspended++) {
1642 mutex_exit(&mpt->m_mutex);
1643 return (DDI_SUCCESS);
1647 * Cancel timeout threads for this mpt
1649 if (mpt->m_quiesce_timeid) {
1650 timeout_id_t tid = mpt->m_quiesce_timeid;
1651 mpt->m_quiesce_timeid = 0;
1652 mutex_exit(&mpt->m_mutex);
1653 (void) untimeout(tid);
1654 mutex_enter(&mpt->m_mutex);
1657 if (mpt->m_restart_cmd_timeid) {
1658 timeout_id_t tid = mpt->m_restart_cmd_timeid;
1659 mpt->m_restart_cmd_timeid = 0;
1660 mutex_exit(&mpt->m_mutex);
1661 (void) untimeout(tid);
1662 mutex_enter(&mpt->m_mutex);
1665 mutex_exit(&mpt->m_mutex);
1667 (void) pm_idle_component(mpt->m_dip, 0);
1670 * Cancel watch threads if all mpts suspended
1672 rw_enter(&mptsas_global_rwlock, RW_WRITER);
1673 for (g = mptsas_head; g != NULL; g = g->m_next) {
1674 if (!g->m_suspended)
1675 break;
1677 rw_exit(&mptsas_global_rwlock);
1679 mutex_enter(&mptsas_global_mutex);
1680 if (g == NULL) {
1681 timeout_id_t tid;
1683 mptsas_timeouts_enabled = 0;
1684 if (mptsas_timeout_id) {
1685 tid = mptsas_timeout_id;
1686 mptsas_timeout_id = 0;
1687 mutex_exit(&mptsas_global_mutex);
1688 (void) untimeout(tid);
1689 mutex_enter(&mptsas_global_mutex);
1691 if (mptsas_reset_watch) {
1692 tid = mptsas_reset_watch;
1693 mptsas_reset_watch = 0;
1694 mutex_exit(&mptsas_global_mutex);
1695 (void) untimeout(tid);
1696 mutex_enter(&mptsas_global_mutex);
1699 mutex_exit(&mptsas_global_mutex);
1701 mutex_enter(&mpt->m_mutex);
1704 * If this mpt is not in full power(PM_LEVEL_D0), just return.
1706 if ((mpt->m_options & MPTSAS_OPT_PM) &&
1707 (mpt->m_power_level != PM_LEVEL_D0)) {
1708 mutex_exit(&mpt->m_mutex);
1709 return (DDI_SUCCESS);
1712 /* Disable HBA interrupts in hardware */
1713 MPTSAS_DISABLE_INTR(mpt);
1715 * Send RAID action system shutdown to sync IR
1717 mptsas_raid_action_system_shutdown(mpt);
1719 mutex_exit(&mpt->m_mutex);
1721 /* drain the taskq */
1722 ddi_taskq_wait(mpt->m_event_taskq);
1723 ddi_taskq_wait(mpt->m_dr_taskq);
1725 return (DDI_SUCCESS);
1728 #ifdef __sparc
1729 /*ARGSUSED*/
1730 static int
1731 mptsas_reset(dev_info_t *devi, ddi_reset_cmd_t cmd)
1733 mptsas_t *mpt;
1734 scsi_hba_tran_t *tran;
1737 * If this call is for iport, just return.
1739 if (scsi_hba_iport_unit_address(devi))
1740 return (DDI_SUCCESS);
1742 if ((tran = ddi_get_driver_private(devi)) == NULL)
1743 return (DDI_SUCCESS);
1745 if ((mpt = TRAN2MPT(tran)) == NULL)
1746 return (DDI_SUCCESS);
1749 * Send RAID action system shutdown to sync IR. Disable HBA
1750 * interrupts in hardware first.
1752 MPTSAS_DISABLE_INTR(mpt);
1753 mptsas_raid_action_system_shutdown(mpt);
1755 return (DDI_SUCCESS);
1757 #else /* __sparc */
1759 * quiesce(9E) entry point.
1761 * This function is called when the system is single-threaded at high
1762 * PIL with preemption disabled. Therefore, this function must not be
1763 * blocked.
1765 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
1766 * DDI_FAILURE indicates an error condition and should almost never happen.
1768 static int
1769 mptsas_quiesce(dev_info_t *devi)
1771 mptsas_t *mpt;
1772 scsi_hba_tran_t *tran;
1775 * If this call is for iport, just return.
1777 if (scsi_hba_iport_unit_address(devi))
1778 return (DDI_SUCCESS);
1780 if ((tran = ddi_get_driver_private(devi)) == NULL)
1781 return (DDI_SUCCESS);
1783 if ((mpt = TRAN2MPT(tran)) == NULL)
1784 return (DDI_SUCCESS);
1786 /* Disable HBA interrupts in hardware */
1787 MPTSAS_DISABLE_INTR(mpt);
1788 /* Send RAID action system shutdonw to sync IR */
1789 mptsas_raid_action_system_shutdown(mpt);
1791 return (DDI_SUCCESS);
1793 #endif /* __sparc */
1796 * detach(9E). Remove all device allocations and system resources;
1797 * disable device interrupts.
1798 * Return DDI_SUCCESS if done; DDI_FAILURE if there's a problem.
1800 static int
1801 mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1803 /* CONSTCOND */
1804 ASSERT(NO_COMPETING_THREADS);
1805 NDBG0(("mptsas_detach: dip=0x%p cmd=0x%p", (void *)devi, (void *)cmd));
1807 switch (cmd) {
1808 case DDI_DETACH:
1809 return (mptsas_do_detach(devi));
1811 case DDI_SUSPEND:
1812 return (mptsas_suspend(devi));
1814 default:
1815 return (DDI_FAILURE);
1817 /* NOTREACHED */
1820 static int
1821 mptsas_do_detach(dev_info_t *dip)
1823 mptsas_t *mpt;
1824 scsi_hba_tran_t *tran;
1825 int circ = 0;
1826 int circ1 = 0;
1827 mdi_pathinfo_t *pip = NULL;
1828 int i;
1829 int doneq_thread_num = 0;
1831 NDBG0(("mptsas_do_detach: dip=0x%p", (void *)dip));
1833 if ((tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) == NULL)
1834 return (DDI_FAILURE);
1836 mpt = TRAN2MPT(tran);
1837 if (!mpt) {
1838 return (DDI_FAILURE);
1841 * Still have pathinfo child, should not detach mpt driver
1843 if (scsi_hba_iport_unit_address(dip)) {
1844 if (mpt->m_mpxio_enable) {
1846 * MPxIO enabled for the iport
1848 ndi_devi_enter(scsi_vhci_dip, &circ1);
1849 ndi_devi_enter(dip, &circ);
1850 while (pip = mdi_get_next_client_path(dip, NULL)) {
1851 if (mdi_pi_free(pip, 0) == MDI_SUCCESS) {
1852 continue;
1854 ndi_devi_exit(dip, circ);
1855 ndi_devi_exit(scsi_vhci_dip, circ1);
1856 NDBG12(("detach failed because of "
1857 "outstanding path info"));
1858 return (DDI_FAILURE);
1860 ndi_devi_exit(dip, circ);
1861 ndi_devi_exit(scsi_vhci_dip, circ1);
1862 (void) mdi_phci_unregister(dip, 0);
1865 ddi_prop_remove_all(dip);
1867 return (DDI_SUCCESS);
1870 /* Make sure power level is D0 before accessing registers */
1871 if (mpt->m_options & MPTSAS_OPT_PM) {
1872 (void) pm_busy_component(dip, 0);
1873 if (mpt->m_power_level != PM_LEVEL_D0) {
1874 if (pm_raise_power(dip, 0, PM_LEVEL_D0) !=
1875 DDI_SUCCESS) {
1876 mptsas_log(mpt, CE_WARN,
1877 "mptsas%d: Raise power request failed.",
1878 mpt->m_instance);
1879 (void) pm_idle_component(dip, 0);
1880 return (DDI_FAILURE);
1886 * Send RAID action system shutdown to sync IR. After action, send a
1887 * Message Unit Reset. Since after that DMA resource will be freed,
1888 * set ioc to READY state will avoid HBA initiated DMA operation.
1890 mutex_enter(&mpt->m_mutex);
1891 MPTSAS_DISABLE_INTR(mpt);
1892 mptsas_raid_action_system_shutdown(mpt);
1893 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1894 (void) mptsas_ioc_reset(mpt, FALSE);
1895 mutex_exit(&mpt->m_mutex);
1896 mptsas_rem_intrs(mpt);
1897 ddi_taskq_destroy(mpt->m_event_taskq);
1898 ddi_taskq_destroy(mpt->m_dr_taskq);
1900 if (mpt->m_doneq_thread_n) {
1901 mutex_enter(&mpt->m_doneq_mutex);
1902 doneq_thread_num = mpt->m_doneq_thread_n;
1903 for (i = 0; i < mpt->m_doneq_thread_n; i++) {
1904 mutex_enter(&mpt->m_doneq_thread_id[i].mutex);
1905 mpt->m_doneq_thread_id[i].flag &=
1906 (~MPTSAS_DONEQ_THREAD_ACTIVE);
1907 cv_signal(&mpt->m_doneq_thread_id[i].cv);
1908 mutex_exit(&mpt->m_doneq_thread_id[i].mutex);
1910 while (mpt->m_doneq_thread_n) {
1911 cv_wait(&mpt->m_doneq_thread_cv,
1912 &mpt->m_doneq_mutex);
1914 for (i = 0; i < doneq_thread_num; i++) {
1915 cv_destroy(&mpt->m_doneq_thread_id[i].cv);
1916 mutex_destroy(&mpt->m_doneq_thread_id[i].mutex);
1918 kmem_free(mpt->m_doneq_thread_id,
1919 sizeof (mptsas_doneq_thread_list_t)
1920 * doneq_thread_num);
1921 mutex_exit(&mpt->m_doneq_mutex);
1922 cv_destroy(&mpt->m_doneq_thread_cv);
1923 mutex_destroy(&mpt->m_doneq_mutex);
1926 scsi_hba_reset_notify_tear_down(mpt->m_reset_notify_listf);
1928 mptsas_list_del(mpt);
1931 * Cancel timeout threads for this mpt
1933 mutex_enter(&mpt->m_mutex);
1934 if (mpt->m_quiesce_timeid) {
1935 timeout_id_t tid = mpt->m_quiesce_timeid;
1936 mpt->m_quiesce_timeid = 0;
1937 mutex_exit(&mpt->m_mutex);
1938 (void) untimeout(tid);
1939 mutex_enter(&mpt->m_mutex);
1942 if (mpt->m_restart_cmd_timeid) {
1943 timeout_id_t tid = mpt->m_restart_cmd_timeid;
1944 mpt->m_restart_cmd_timeid = 0;
1945 mutex_exit(&mpt->m_mutex);
1946 (void) untimeout(tid);
1947 mutex_enter(&mpt->m_mutex);
1950 mutex_exit(&mpt->m_mutex);
1953 * last mpt? ... if active, CANCEL watch threads.
1955 mutex_enter(&mptsas_global_mutex);
1956 if (mptsas_head == NULL) {
1957 timeout_id_t tid;
1959 * Clear mptsas_timeouts_enable so that the watch thread
1960 * gets restarted on DDI_ATTACH
1962 mptsas_timeouts_enabled = 0;
1963 if (mptsas_timeout_id) {
1964 tid = mptsas_timeout_id;
1965 mptsas_timeout_id = 0;
1966 mutex_exit(&mptsas_global_mutex);
1967 (void) untimeout(tid);
1968 mutex_enter(&mptsas_global_mutex);
1970 if (mptsas_reset_watch) {
1971 tid = mptsas_reset_watch;
1972 mptsas_reset_watch = 0;
1973 mutex_exit(&mptsas_global_mutex);
1974 (void) untimeout(tid);
1975 mutex_enter(&mptsas_global_mutex);
1978 mutex_exit(&mptsas_global_mutex);
1981 * Delete Phy stats
1983 mptsas_destroy_phy_stats(mpt);
1985 mptsas_destroy_hashes(mpt);
1988 * Delete nt_active.
1990 mutex_enter(&mpt->m_mutex);
1991 mptsas_free_active_slots(mpt);
1992 mutex_exit(&mpt->m_mutex);
1994 /* deallocate everything that was allocated in mptsas_attach */
1995 mptsas_cache_destroy(mpt);
1997 mptsas_hba_fini(mpt);
1998 mptsas_cfg_fini(mpt);
2000 /* Lower the power informing PM Framework */
2001 if (mpt->m_options & MPTSAS_OPT_PM) {
2002 if (pm_lower_power(dip, 0, PM_LEVEL_D3) != DDI_SUCCESS)
2003 mptsas_log(mpt, CE_WARN,
2004 "!mptsas%d: Lower power request failed "
2005 "during detach, ignoring.",
2006 mpt->m_instance);
2009 mutex_destroy(&mpt->m_tx_waitq_mutex);
2010 mutex_destroy(&mpt->m_passthru_mutex);
2011 mutex_destroy(&mpt->m_mutex);
2012 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
2013 mutex_destroy(&mpt->m_phy_info[i].smhba_info.phy_mutex);
2015 cv_destroy(&mpt->m_cv);
2016 cv_destroy(&mpt->m_passthru_cv);
2017 cv_destroy(&mpt->m_fw_cv);
2018 cv_destroy(&mpt->m_config_cv);
2019 cv_destroy(&mpt->m_fw_diag_cv);
2022 mptsas_smp_teardown(mpt);
2023 mptsas_hba_teardown(mpt);
2025 mptsas_config_space_fini(mpt);
2027 mptsas_free_handshake_msg(mpt);
2029 mptsas_fm_fini(mpt);
2030 ddi_soft_state_free(mptsas_state, ddi_get_instance(dip));
2031 ddi_prop_remove_all(dip);
2033 return (DDI_SUCCESS);
2036 static void
2037 mptsas_list_add(mptsas_t *mpt)
2039 rw_enter(&mptsas_global_rwlock, RW_WRITER);
2041 if (mptsas_head == NULL) {
2042 mptsas_head = mpt;
2043 } else {
2044 mptsas_tail->m_next = mpt;
2046 mptsas_tail = mpt;
2047 rw_exit(&mptsas_global_rwlock);
2050 static void
2051 mptsas_list_del(mptsas_t *mpt)
2053 mptsas_t *m;
2055 * Remove device instance from the global linked list
2057 rw_enter(&mptsas_global_rwlock, RW_WRITER);
2058 if (mptsas_head == mpt) {
2059 m = mptsas_head = mpt->m_next;
2060 } else {
2061 for (m = mptsas_head; m != NULL; m = m->m_next) {
2062 if (m->m_next == mpt) {
2063 m->m_next = mpt->m_next;
2064 break;
2067 if (m == NULL) {
2068 mptsas_log(mpt, CE_PANIC, "Not in softc list!");
2072 if (mptsas_tail == mpt) {
2073 mptsas_tail = m;
2075 rw_exit(&mptsas_global_rwlock);
2078 static int
2079 mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size)
2081 ddi_dma_attr_t task_dma_attrs;
2083 mpt->m_hshk_dma_size = 0;
2084 task_dma_attrs = mpt->m_msg_dma_attr;
2085 task_dma_attrs.dma_attr_sgllen = 1;
2086 task_dma_attrs.dma_attr_granular = (uint32_t)(alloc_size);
2088 /* allocate Task Management ddi_dma resources */
2089 if (mptsas_dma_addr_create(mpt, task_dma_attrs,
2090 &mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl, &mpt->m_hshk_memp,
2091 alloc_size, NULL) == FALSE) {
2092 return (DDI_FAILURE);
2094 mpt->m_hshk_dma_size = alloc_size;
2096 return (DDI_SUCCESS);
2099 static void
2100 mptsas_free_handshake_msg(mptsas_t *mpt)
2102 if (mpt->m_hshk_dma_size == 0)
2103 return;
2104 mptsas_dma_addr_destroy(&mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl);
2105 mpt->m_hshk_dma_size = 0;
2108 static int
2109 mptsas_hba_setup(mptsas_t *mpt)
2111 scsi_hba_tran_t *hba_tran;
2112 int tran_flags;
2114 /* Allocate a transport structure */
2115 hba_tran = mpt->m_tran = scsi_hba_tran_alloc(mpt->m_dip,
2116 SCSI_HBA_CANSLEEP);
2117 ASSERT(mpt->m_tran != NULL);
2119 hba_tran->tran_hba_private = mpt;
2120 hba_tran->tran_tgt_private = NULL;
2122 hba_tran->tran_tgt_init = mptsas_scsi_tgt_init;
2123 hba_tran->tran_tgt_free = mptsas_scsi_tgt_free;
2125 hba_tran->tran_start = mptsas_scsi_start;
2126 hba_tran->tran_reset = mptsas_scsi_reset;
2127 hba_tran->tran_abort = mptsas_scsi_abort;
2128 hba_tran->tran_getcap = mptsas_scsi_getcap;
2129 hba_tran->tran_setcap = mptsas_scsi_setcap;
2130 hba_tran->tran_init_pkt = mptsas_scsi_init_pkt;
2131 hba_tran->tran_destroy_pkt = mptsas_scsi_destroy_pkt;
2133 hba_tran->tran_dmafree = mptsas_scsi_dmafree;
2134 hba_tran->tran_sync_pkt = mptsas_scsi_sync_pkt;
2135 hba_tran->tran_reset_notify = mptsas_scsi_reset_notify;
2137 hba_tran->tran_get_bus_addr = mptsas_get_bus_addr;
2138 hba_tran->tran_get_name = mptsas_get_name;
2140 hba_tran->tran_quiesce = mptsas_scsi_quiesce;
2141 hba_tran->tran_unquiesce = mptsas_scsi_unquiesce;
2142 hba_tran->tran_bus_reset = NULL;
2144 hba_tran->tran_add_eventcall = NULL;
2145 hba_tran->tran_get_eventcookie = NULL;
2146 hba_tran->tran_post_event = NULL;
2147 hba_tran->tran_remove_eventcall = NULL;
2149 hba_tran->tran_bus_config = mptsas_bus_config;
2151 hba_tran->tran_interconnect_type = INTERCONNECT_SAS;
2154 * All children of the HBA are iports. We need tran was cloned.
2155 * So we pass the flags to SCSA. SCSI_HBA_TRAN_CLONE will be
2156 * inherited to iport's tran vector.
2158 tran_flags = (SCSI_HBA_HBA | SCSI_HBA_TRAN_CLONE);
2160 if (scsi_hba_attach_setup(mpt->m_dip, &mpt->m_msg_dma_attr,
2161 hba_tran, tran_flags) != DDI_SUCCESS) {
2162 mptsas_log(mpt, CE_WARN, "hba attach setup failed");
2163 scsi_hba_tran_free(hba_tran);
2164 mpt->m_tran = NULL;
2165 return (FALSE);
2167 return (TRUE);
2170 static void
2171 mptsas_hba_teardown(mptsas_t *mpt)
2173 (void) scsi_hba_detach(mpt->m_dip);
2174 if (mpt->m_tran != NULL) {
2175 scsi_hba_tran_free(mpt->m_tran);
2176 mpt->m_tran = NULL;
2180 static void
2181 mptsas_iport_register(mptsas_t *mpt)
2183 int i, j;
2184 mptsas_phymask_t mask = 0x0;
2186 * initial value of mask is 0
2188 mutex_enter(&mpt->m_mutex);
2189 for (i = 0; i < mpt->m_num_phys; i++) {
2190 mptsas_phymask_t phy_mask = 0x0;
2191 char phy_mask_name[MPTSAS_MAX_PHYS];
2192 uint8_t current_port;
2194 if (mpt->m_phy_info[i].attached_devhdl == 0)
2195 continue;
2197 bzero(phy_mask_name, sizeof (phy_mask_name));
2199 current_port = mpt->m_phy_info[i].port_num;
2201 if ((mask & (1 << i)) != 0)
2202 continue;
2204 for (j = 0; j < mpt->m_num_phys; j++) {
2205 if (mpt->m_phy_info[j].attached_devhdl &&
2206 (mpt->m_phy_info[j].port_num == current_port)) {
2207 phy_mask |= (1 << j);
2210 mask = mask | phy_mask;
2212 for (j = 0; j < mpt->m_num_phys; j++) {
2213 if ((phy_mask >> j) & 0x01) {
2214 mpt->m_phy_info[j].phy_mask = phy_mask;
2218 (void) sprintf(phy_mask_name, "%x", phy_mask);
2220 mutex_exit(&mpt->m_mutex);
2222 * register a iport
2224 (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
2225 mutex_enter(&mpt->m_mutex);
2227 mutex_exit(&mpt->m_mutex);
2229 * register a virtual port for RAID volume always
2231 (void) scsi_hba_iport_register(mpt->m_dip, "v0");
2235 static int
2236 mptsas_smp_setup(mptsas_t *mpt)
2238 mpt->m_smptran = smp_hba_tran_alloc(mpt->m_dip);
2239 ASSERT(mpt->m_smptran != NULL);
2240 mpt->m_smptran->smp_tran_hba_private = mpt;
2241 mpt->m_smptran->smp_tran_start = mptsas_smp_start;
2242 if (smp_hba_attach_setup(mpt->m_dip, mpt->m_smptran) != DDI_SUCCESS) {
2243 mptsas_log(mpt, CE_WARN, "smp attach setup failed");
2244 smp_hba_tran_free(mpt->m_smptran);
2245 mpt->m_smptran = NULL;
2246 return (FALSE);
2249 * Initialize smp hash table
2251 mpt->m_smp_targets = refhash_create(MPTSAS_SMP_BUCKET_COUNT,
2252 mptsas_target_addr_hash, mptsas_target_addr_cmp,
2253 mptsas_smp_free, sizeof (mptsas_smp_t),
2254 offsetof(mptsas_smp_t, m_link), offsetof(mptsas_smp_t, m_addr),
2255 KM_SLEEP);
2256 mpt->m_smp_devhdl = 0xFFFF;
2258 return (TRUE);
2261 static void
2262 mptsas_smp_teardown(mptsas_t *mpt)
2264 (void) smp_hba_detach(mpt->m_dip);
2265 if (mpt->m_smptran != NULL) {
2266 smp_hba_tran_free(mpt->m_smptran);
2267 mpt->m_smptran = NULL;
2269 mpt->m_smp_devhdl = 0;
2272 static int
2273 mptsas_cache_create(mptsas_t *mpt)
2275 int instance = mpt->m_instance;
2276 char buf[64];
2279 * create kmem cache for packets
2281 (void) sprintf(buf, "mptsas%d_cache", instance);
2282 mpt->m_kmem_cache = kmem_cache_create(buf,
2283 sizeof (struct mptsas_cmd) + scsi_pkt_size(), 8,
2284 mptsas_kmem_cache_constructor, mptsas_kmem_cache_destructor,
2285 NULL, (void *)mpt, NULL, 0);
2287 if (mpt->m_kmem_cache == NULL) {
2288 mptsas_log(mpt, CE_WARN, "creating kmem cache failed");
2289 return (FALSE);
2293 * create kmem cache for extra SGL frames if SGL cannot
2294 * be accomodated into main request frame.
2296 (void) sprintf(buf, "mptsas%d_cache_frames", instance);
2297 mpt->m_cache_frames = kmem_cache_create(buf,
2298 sizeof (mptsas_cache_frames_t), 8,
2299 mptsas_cache_frames_constructor, mptsas_cache_frames_destructor,
2300 NULL, (void *)mpt, NULL, 0);
2302 if (mpt->m_cache_frames == NULL) {
2303 mptsas_log(mpt, CE_WARN, "creating cache for frames failed");
2304 return (FALSE);
2307 return (TRUE);
2310 static void
2311 mptsas_cache_destroy(mptsas_t *mpt)
2313 /* deallocate in reverse order */
2314 if (mpt->m_cache_frames) {
2315 kmem_cache_destroy(mpt->m_cache_frames);
2316 mpt->m_cache_frames = NULL;
2318 if (mpt->m_kmem_cache) {
2319 kmem_cache_destroy(mpt->m_kmem_cache);
2320 mpt->m_kmem_cache = NULL;
2324 static int
2325 mptsas_power(dev_info_t *dip, int component, int level)
2327 #ifndef __lock_lint
2328 _NOTE(ARGUNUSED(component))
2329 #endif
2330 mptsas_t *mpt;
2331 int rval = DDI_SUCCESS;
2332 int polls = 0;
2333 uint32_t ioc_status;
2335 if (scsi_hba_iport_unit_address(dip) != 0)
2336 return (DDI_SUCCESS);
2338 mpt = ddi_get_soft_state(mptsas_state, ddi_get_instance(dip));
2339 if (mpt == NULL) {
2340 return (DDI_FAILURE);
2343 mutex_enter(&mpt->m_mutex);
2346 * If the device is busy, don't lower its power level
2348 if (mpt->m_busy && (mpt->m_power_level > level)) {
2349 mutex_exit(&mpt->m_mutex);
2350 return (DDI_FAILURE);
2352 switch (level) {
2353 case PM_LEVEL_D0:
2354 NDBG11(("mptsas%d: turning power ON.", mpt->m_instance));
2355 MPTSAS_POWER_ON(mpt);
2357 * Wait up to 30 seconds for IOC to come out of reset.
2359 while (((ioc_status = ddi_get32(mpt->m_datap,
2360 &mpt->m_reg->Doorbell)) &
2361 MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
2362 if (polls++ > 3000) {
2363 break;
2365 delay(drv_usectohz(10000));
2368 * If IOC is not in operational state, try to hard reset it.
2370 if ((ioc_status & MPI2_IOC_STATE_MASK) !=
2371 MPI2_IOC_STATE_OPERATIONAL) {
2372 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
2373 if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
2374 mptsas_log(mpt, CE_WARN,
2375 "mptsas_power: hard reset failed");
2376 mutex_exit(&mpt->m_mutex);
2377 return (DDI_FAILURE);
2380 mpt->m_power_level = PM_LEVEL_D0;
2381 break;
2382 case PM_LEVEL_D3:
2383 NDBG11(("mptsas%d: turning power OFF.", mpt->m_instance));
2384 MPTSAS_POWER_OFF(mpt);
2385 break;
2386 default:
2387 mptsas_log(mpt, CE_WARN, "mptsas%d: unknown power level <%x>.",
2388 mpt->m_instance, level);
2389 rval = DDI_FAILURE;
2390 break;
2392 mutex_exit(&mpt->m_mutex);
2393 return (rval);
2397 * Initialize configuration space and figure out which
2398 * chip and revison of the chip the mpt driver is using.
2400 static int
2401 mptsas_config_space_init(mptsas_t *mpt)
2403 NDBG0(("mptsas_config_space_init"));
2405 if (mpt->m_config_handle != NULL)
2406 return (TRUE);
2408 if (pci_config_setup(mpt->m_dip,
2409 &mpt->m_config_handle) != DDI_SUCCESS) {
2410 mptsas_log(mpt, CE_WARN, "cannot map configuration space.");
2411 return (FALSE);
2415 * This is a workaround for a XMITS ASIC bug which does not
2416 * drive the CBE upper bits.
2418 if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT) &
2419 PCI_STAT_PERROR) {
2420 pci_config_put16(mpt->m_config_handle, PCI_CONF_STAT,
2421 PCI_STAT_PERROR);
2424 mptsas_setup_cmd_reg(mpt);
2427 * Get the chip device id:
2429 mpt->m_devid = pci_config_get16(mpt->m_config_handle, PCI_CONF_DEVID);
2432 * Save the revision.
2434 mpt->m_revid = pci_config_get8(mpt->m_config_handle, PCI_CONF_REVID);
2437 * Save the SubSystem Vendor and Device IDs
2439 mpt->m_svid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBVENID);
2440 mpt->m_ssid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBSYSID);
2443 * Set the latency timer to 0x40 as specified by the upa -> pci
2444 * bridge chip design team. This may be done by the sparc pci
2445 * bus nexus driver, but the driver should make sure the latency
2446 * timer is correct for performance reasons.
2448 pci_config_put8(mpt->m_config_handle, PCI_CONF_LATENCY_TIMER,
2449 MPTSAS_LATENCY_TIMER);
2451 (void) mptsas_get_pci_cap(mpt);
2452 return (TRUE);
2455 static void
2456 mptsas_config_space_fini(mptsas_t *mpt)
2458 if (mpt->m_config_handle != NULL) {
2459 mptsas_disable_bus_master(mpt);
2460 pci_config_teardown(&mpt->m_config_handle);
2461 mpt->m_config_handle = NULL;
2465 static void
2466 mptsas_setup_cmd_reg(mptsas_t *mpt)
2468 ushort_t cmdreg;
2471 * Set the command register to the needed values.
2473 cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2474 cmdreg |= (PCI_COMM_ME | PCI_COMM_SERR_ENABLE |
2475 PCI_COMM_PARITY_DETECT | PCI_COMM_MAE);
2476 cmdreg &= ~PCI_COMM_IO;
2477 pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2480 static void
2481 mptsas_disable_bus_master(mptsas_t *mpt)
2483 ushort_t cmdreg;
2486 * Clear the master enable bit in the PCI command register.
2487 * This prevents any bus mastering activity like DMA.
2489 cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2490 cmdreg &= ~PCI_COMM_ME;
2491 pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2495 mptsas_dma_alloc(mptsas_t *mpt, mptsas_dma_alloc_state_t *dma_statep)
2497 ddi_dma_attr_t attrs;
2499 attrs = mpt->m_io_dma_attr;
2500 attrs.dma_attr_sgllen = 1;
2502 ASSERT(dma_statep != NULL);
2504 if (mptsas_dma_addr_create(mpt, attrs, &dma_statep->handle,
2505 &dma_statep->accessp, &dma_statep->memp, dma_statep->size,
2506 &dma_statep->cookie) == FALSE) {
2507 return (DDI_FAILURE);
2510 return (DDI_SUCCESS);
2513 void
2514 mptsas_dma_free(mptsas_dma_alloc_state_t *dma_statep)
2516 ASSERT(dma_statep != NULL);
2517 mptsas_dma_addr_destroy(&dma_statep->handle, &dma_statep->accessp);
2518 dma_statep->size = 0;
2522 mptsas_do_dma(mptsas_t *mpt, uint32_t size, int var, int (*callback)())
2524 ddi_dma_attr_t attrs;
2525 ddi_dma_handle_t dma_handle;
2526 caddr_t memp;
2527 ddi_acc_handle_t accessp;
2528 int rval;
2530 ASSERT(mutex_owned(&mpt->m_mutex));
2532 attrs = mpt->m_msg_dma_attr;
2533 attrs.dma_attr_sgllen = 1;
2534 attrs.dma_attr_granular = size;
2536 if (mptsas_dma_addr_create(mpt, attrs, &dma_handle,
2537 &accessp, &memp, size, NULL) == FALSE) {
2538 return (DDI_FAILURE);
2541 rval = (*callback) (mpt, memp, var, accessp);
2543 if ((mptsas_check_dma_handle(dma_handle) != DDI_SUCCESS) ||
2544 (mptsas_check_acc_handle(accessp) != DDI_SUCCESS)) {
2545 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2546 rval = DDI_FAILURE;
2549 mptsas_dma_addr_destroy(&dma_handle, &accessp);
2550 return (rval);
2554 static int
2555 mptsas_alloc_request_frames(mptsas_t *mpt)
2557 ddi_dma_attr_t frame_dma_attrs;
2558 caddr_t memp;
2559 ddi_dma_cookie_t cookie;
2560 size_t mem_size;
2563 * re-alloc when it has already alloced
2565 if (mpt->m_dma_req_frame_hdl)
2566 mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2567 &mpt->m_acc_req_frame_hdl);
2570 * The size of the request frame pool is:
2571 * Number of Request Frames * Request Frame Size
2573 mem_size = mpt->m_max_requests * mpt->m_req_frame_size;
2576 * set the DMA attributes. System Request Message Frames must be
2577 * aligned on a 16-byte boundry.
2579 frame_dma_attrs = mpt->m_msg_dma_attr;
2580 frame_dma_attrs.dma_attr_align = 16;
2581 frame_dma_attrs.dma_attr_sgllen = 1;
2584 * allocate the request frame pool.
2586 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2587 &mpt->m_dma_req_frame_hdl, &mpt->m_acc_req_frame_hdl, &memp,
2588 mem_size, &cookie) == FALSE) {
2589 return (DDI_FAILURE);
2593 * Store the request frame memory address. This chip uses this
2594 * address to dma to and from the driver's frame. The second
2595 * address is the address mpt uses to fill in the frame.
2597 mpt->m_req_frame_dma_addr = cookie.dmac_laddress;
2598 mpt->m_req_frame = memp;
2601 * Clear the request frame pool.
2603 bzero(mpt->m_req_frame, mem_size);
2605 return (DDI_SUCCESS);
2608 static int
2609 mptsas_alloc_sense_bufs(mptsas_t *mpt)
2611 ddi_dma_attr_t sense_dma_attrs;
2612 caddr_t memp;
2613 ddi_dma_cookie_t cookie;
2614 size_t mem_size;
2615 int num_extrqsense_bufs;
2618 * re-alloc when it has already alloced
2620 if (mpt->m_dma_req_sense_hdl) {
2621 rmfreemap(mpt->m_erqsense_map);
2622 mptsas_dma_addr_destroy(&mpt->m_dma_req_sense_hdl,
2623 &mpt->m_acc_req_sense_hdl);
2627 * The size of the request sense pool is:
2628 * (Number of Request Frames - 2 ) * Request Sense Size +
2629 * extra memory for extended sense requests.
2631 mem_size = ((mpt->m_max_requests - 2) * mpt->m_req_sense_size) +
2632 mptsas_extreq_sense_bufsize;
2635 * set the DMA attributes. ARQ buffers
2636 * aligned on a 16-byte boundry.
2638 sense_dma_attrs = mpt->m_msg_dma_attr;
2639 sense_dma_attrs.dma_attr_align = 16;
2640 sense_dma_attrs.dma_attr_sgllen = 1;
2643 * allocate the request sense buffer pool.
2645 if (mptsas_dma_addr_create(mpt, sense_dma_attrs,
2646 &mpt->m_dma_req_sense_hdl, &mpt->m_acc_req_sense_hdl, &memp,
2647 mem_size, &cookie) == FALSE) {
2648 return (DDI_FAILURE);
2652 * Store the request sense base memory address. This chip uses this
2653 * address to dma the request sense data. The second
2654 * address is the address mpt uses to access the data.
2655 * The third is the base for the extended rqsense buffers.
2657 mpt->m_req_sense_dma_addr = cookie.dmac_laddress;
2658 mpt->m_req_sense = memp;
2659 memp += (mpt->m_max_requests - 2) * mpt->m_req_sense_size;
2660 mpt->m_extreq_sense = memp;
2663 * The extra memory is divided up into multiples of the base
2664 * buffer size in order to allocate via rmalloc().
2665 * Note that the rmallocmap cannot start at zero!
2667 num_extrqsense_bufs = mptsas_extreq_sense_bufsize /
2668 mpt->m_req_sense_size;
2669 mpt->m_erqsense_map = rmallocmap_wait(num_extrqsense_bufs);
2670 rmfree(mpt->m_erqsense_map, num_extrqsense_bufs, 1);
2673 * Clear the pool.
2675 bzero(mpt->m_req_sense, mem_size);
2677 return (DDI_SUCCESS);
2680 static int
2681 mptsas_alloc_reply_frames(mptsas_t *mpt)
2683 ddi_dma_attr_t frame_dma_attrs;
2684 caddr_t memp;
2685 ddi_dma_cookie_t cookie;
2686 size_t mem_size;
2689 * re-alloc when it has already alloced
2691 if (mpt->m_dma_reply_frame_hdl) {
2692 mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2693 &mpt->m_acc_reply_frame_hdl);
2697 * The size of the reply frame pool is:
2698 * Number of Reply Frames * Reply Frame Size
2700 mem_size = mpt->m_max_replies * mpt->m_reply_frame_size;
2703 * set the DMA attributes. System Reply Message Frames must be
2704 * aligned on a 4-byte boundry. This is the default.
2706 frame_dma_attrs = mpt->m_msg_dma_attr;
2707 frame_dma_attrs.dma_attr_sgllen = 1;
2710 * allocate the reply frame pool
2712 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2713 &mpt->m_dma_reply_frame_hdl, &mpt->m_acc_reply_frame_hdl, &memp,
2714 mem_size, &cookie) == FALSE) {
2715 return (DDI_FAILURE);
2719 * Store the reply frame memory address. This chip uses this
2720 * address to dma to and from the driver's frame. The second
2721 * address is the address mpt uses to process the frame.
2723 mpt->m_reply_frame_dma_addr = cookie.dmac_laddress;
2724 mpt->m_reply_frame = memp;
2727 * Clear the reply frame pool.
2729 bzero(mpt->m_reply_frame, mem_size);
2731 return (DDI_SUCCESS);
2734 static int
2735 mptsas_alloc_free_queue(mptsas_t *mpt)
2737 ddi_dma_attr_t frame_dma_attrs;
2738 caddr_t memp;
2739 ddi_dma_cookie_t cookie;
2740 size_t mem_size;
2743 * re-alloc when it has already alloced
2745 if (mpt->m_dma_free_queue_hdl) {
2746 mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2747 &mpt->m_acc_free_queue_hdl);
2751 * The reply free queue size is:
2752 * Reply Free Queue Depth * 4
2753 * The "4" is the size of one 32 bit address (low part of 64-bit
2754 * address)
2756 mem_size = mpt->m_free_queue_depth * 4;
2759 * set the DMA attributes The Reply Free Queue must be aligned on a
2760 * 16-byte boundry.
2762 frame_dma_attrs = mpt->m_msg_dma_attr;
2763 frame_dma_attrs.dma_attr_align = 16;
2764 frame_dma_attrs.dma_attr_sgllen = 1;
2767 * allocate the reply free queue
2769 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2770 &mpt->m_dma_free_queue_hdl, &mpt->m_acc_free_queue_hdl, &memp,
2771 mem_size, &cookie) == FALSE) {
2772 return (DDI_FAILURE);
2776 * Store the reply free queue memory address. This chip uses this
2777 * address to read from the reply free queue. The second address
2778 * is the address mpt uses to manage the queue.
2780 mpt->m_free_queue_dma_addr = cookie.dmac_laddress;
2781 mpt->m_free_queue = memp;
2784 * Clear the reply free queue memory.
2786 bzero(mpt->m_free_queue, mem_size);
2788 return (DDI_SUCCESS);
2791 static int
2792 mptsas_alloc_post_queue(mptsas_t *mpt)
2794 ddi_dma_attr_t frame_dma_attrs;
2795 caddr_t memp;
2796 ddi_dma_cookie_t cookie;
2797 size_t mem_size;
2800 * re-alloc when it has already alloced
2802 if (mpt->m_dma_post_queue_hdl) {
2803 mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2804 &mpt->m_acc_post_queue_hdl);
2808 * The reply descriptor post queue size is:
2809 * Reply Descriptor Post Queue Depth * 8
2810 * The "8" is the size of each descriptor (8 bytes or 64 bits).
2812 mem_size = mpt->m_post_queue_depth * 8;
2815 * set the DMA attributes. The Reply Descriptor Post Queue must be
2816 * aligned on a 16-byte boundry.
2818 frame_dma_attrs = mpt->m_msg_dma_attr;
2819 frame_dma_attrs.dma_attr_align = 16;
2820 frame_dma_attrs.dma_attr_sgllen = 1;
2823 * allocate the reply post queue
2825 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2826 &mpt->m_dma_post_queue_hdl, &mpt->m_acc_post_queue_hdl, &memp,
2827 mem_size, &cookie) == FALSE) {
2828 return (DDI_FAILURE);
2832 * Store the reply descriptor post queue memory address. This chip
2833 * uses this address to write to the reply descriptor post queue. The
2834 * second address is the address mpt uses to manage the queue.
2836 mpt->m_post_queue_dma_addr = cookie.dmac_laddress;
2837 mpt->m_post_queue = memp;
2840 * Clear the reply post queue memory.
2842 bzero(mpt->m_post_queue, mem_size);
2844 return (DDI_SUCCESS);
2847 static void
2848 mptsas_alloc_reply_args(mptsas_t *mpt)
2850 if (mpt->m_replyh_args == NULL) {
2851 mpt->m_replyh_args = kmem_zalloc(sizeof (m_replyh_arg_t) *
2852 mpt->m_max_replies, KM_SLEEP);
2856 static int
2857 mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2859 mptsas_cache_frames_t *frames = NULL;
2860 if (cmd->cmd_extra_frames == NULL) {
2861 frames = kmem_cache_alloc(mpt->m_cache_frames, KM_NOSLEEP);
2862 if (frames == NULL) {
2863 return (DDI_FAILURE);
2865 cmd->cmd_extra_frames = frames;
2867 return (DDI_SUCCESS);
2870 static void
2871 mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2873 if (cmd->cmd_extra_frames) {
2874 kmem_cache_free(mpt->m_cache_frames,
2875 (void *)cmd->cmd_extra_frames);
2876 cmd->cmd_extra_frames = NULL;
2880 static void
2881 mptsas_cfg_fini(mptsas_t *mpt)
2883 NDBG0(("mptsas_cfg_fini"));
2884 ddi_regs_map_free(&mpt->m_datap);
2887 static void
2888 mptsas_hba_fini(mptsas_t *mpt)
2890 NDBG0(("mptsas_hba_fini"));
2893 * Free up any allocated memory
2895 if (mpt->m_dma_req_frame_hdl) {
2896 mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2897 &mpt->m_acc_req_frame_hdl);
2900 if (mpt->m_dma_req_sense_hdl) {
2901 rmfreemap(mpt->m_erqsense_map);
2902 mptsas_dma_addr_destroy(&mpt->m_dma_req_sense_hdl,
2903 &mpt->m_acc_req_sense_hdl);
2906 if (mpt->m_dma_reply_frame_hdl) {
2907 mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2908 &mpt->m_acc_reply_frame_hdl);
2911 if (mpt->m_dma_free_queue_hdl) {
2912 mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2913 &mpt->m_acc_free_queue_hdl);
2916 if (mpt->m_dma_post_queue_hdl) {
2917 mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2918 &mpt->m_acc_post_queue_hdl);
2921 if (mpt->m_replyh_args != NULL) {
2922 kmem_free(mpt->m_replyh_args, sizeof (m_replyh_arg_t)
2923 * mpt->m_max_replies);
2927 static int
2928 mptsas_name_child(dev_info_t *lun_dip, char *name, int len)
2930 int lun = 0;
2931 char *sas_wwn = NULL;
2932 int phynum = -1;
2933 int reallen = 0;
2935 /* Get the target num */
2936 lun = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip, DDI_PROP_DONTPASS,
2937 LUN_PROP, 0);
2939 if ((phynum = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip,
2940 DDI_PROP_DONTPASS, "sata-phy", -1)) != -1) {
2942 * Stick in the address of form "pPHY,LUN"
2944 reallen = snprintf(name, len, "p%x,%x", phynum, lun);
2945 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, lun_dip,
2946 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &sas_wwn)
2947 == DDI_PROP_SUCCESS) {
2949 * Stick in the address of the form "wWWN,LUN"
2951 reallen = snprintf(name, len, "%s,%x", sas_wwn, lun);
2952 ddi_prop_free(sas_wwn);
2953 } else {
2954 return (DDI_FAILURE);
2957 ASSERT(reallen < len);
2958 if (reallen >= len) {
2959 mptsas_log(0, CE_WARN, "!mptsas_get_name: name parameter "
2960 "length too small, it needs to be %d bytes", reallen + 1);
2962 return (DDI_SUCCESS);
2966 * tran_tgt_init(9E) - target device instance initialization
2968 static int
2969 mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2970 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2972 #ifndef __lock_lint
2973 _NOTE(ARGUNUSED(hba_tran))
2974 #endif
2977 * At this point, the scsi_device structure already exists
2978 * and has been initialized.
2980 * Use this function to allocate target-private data structures,
2981 * if needed by this HBA. Add revised flow-control and queue
2982 * properties for child here, if desired and if you can tell they
2983 * support tagged queueing by now.
2985 mptsas_t *mpt;
2986 int lun = sd->sd_address.a_lun;
2987 mdi_pathinfo_t *pip = NULL;
2988 mptsas_tgt_private_t *tgt_private = NULL;
2989 mptsas_target_t *ptgt = NULL;
2990 char *psas_wwn = NULL;
2991 mptsas_phymask_t phymask = 0;
2992 uint64_t sas_wwn = 0;
2993 mptsas_target_addr_t addr;
2994 mpt = SDEV2MPT(sd);
2996 ASSERT(scsi_hba_iport_unit_address(hba_dip) != 0);
2998 NDBG0(("mptsas_scsi_tgt_init: hbadip=0x%p tgtdip=0x%p lun=%d",
2999 (void *)hba_dip, (void *)tgt_dip, lun));
3001 if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
3002 (void) ndi_merge_node(tgt_dip, mptsas_name_child);
3003 ddi_set_name_addr(tgt_dip, NULL);
3004 return (DDI_FAILURE);
3007 * phymask is 0 means the virtual port for RAID
3009 phymask = (mptsas_phymask_t)ddi_prop_get_int(DDI_DEV_T_ANY, hba_dip, 0,
3010 "phymask", 0);
3011 if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
3012 if ((pip = (void *)(sd->sd_private)) == NULL) {
3014 * Very bad news if this occurs. Somehow scsi_vhci has
3015 * lost the pathinfo node for this target.
3017 return (DDI_NOT_WELL_FORMED);
3020 if (mdi_prop_lookup_int(pip, LUN_PROP, &lun) !=
3021 DDI_PROP_SUCCESS) {
3022 mptsas_log(mpt, CE_WARN, "Get lun property failed\n");
3023 return (DDI_FAILURE);
3026 if (mdi_prop_lookup_string(pip, SCSI_ADDR_PROP_TARGET_PORT,
3027 &psas_wwn) == MDI_SUCCESS) {
3028 if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
3029 sas_wwn = 0;
3031 (void) mdi_prop_free(psas_wwn);
3033 } else {
3034 lun = ddi_prop_get_int(DDI_DEV_T_ANY, tgt_dip,
3035 DDI_PROP_DONTPASS, LUN_PROP, 0);
3036 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip,
3037 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &psas_wwn) ==
3038 DDI_PROP_SUCCESS) {
3039 if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
3040 sas_wwn = 0;
3042 ddi_prop_free(psas_wwn);
3043 } else {
3044 sas_wwn = 0;
3048 ASSERT((sas_wwn != 0) || (phymask != 0));
3049 addr.mta_wwn = sas_wwn;
3050 addr.mta_phymask = phymask;
3051 mutex_enter(&mpt->m_mutex);
3052 ptgt = refhash_lookup(mpt->m_targets, &addr);
3053 mutex_exit(&mpt->m_mutex);
3054 if (ptgt == NULL) {
3055 mptsas_log(mpt, CE_WARN, "!tgt_init: target doesn't exist or "
3056 "gone already! phymask:%x, saswwn %"PRIx64, phymask,
3057 sas_wwn);
3058 return (DDI_FAILURE);
3060 if (hba_tran->tran_tgt_private == NULL) {
3061 tgt_private = kmem_zalloc(sizeof (mptsas_tgt_private_t),
3062 KM_SLEEP);
3063 tgt_private->t_lun = lun;
3064 tgt_private->t_private = ptgt;
3065 hba_tran->tran_tgt_private = tgt_private;
3068 if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
3069 return (DDI_SUCCESS);
3071 mutex_enter(&mpt->m_mutex);
3073 if (ptgt->m_deviceinfo &
3074 (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
3075 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
3076 uchar_t *inq89 = NULL;
3077 int inq89_len = 0x238;
3078 int reallen = 0;
3079 int rval = 0;
3080 struct sata_id *sid = NULL;
3081 char model[SATA_ID_MODEL_LEN + 1];
3082 char fw[SATA_ID_FW_LEN + 1];
3083 char *vid, *pid;
3084 int i;
3086 mutex_exit(&mpt->m_mutex);
3088 * According SCSI/ATA Translation -2 (SAT-2) revision 01a
3089 * chapter 12.4.2 VPD page 89h includes 512 bytes ATA IDENTIFY
3090 * DEVICE data or ATA IDENTIFY PACKET DEVICE data.
3092 inq89 = kmem_zalloc(inq89_len, KM_SLEEP);
3093 rval = mptsas_inquiry(mpt, ptgt, 0, 0x89,
3094 inq89, inq89_len, &reallen, 1);
3096 if (rval != 0) {
3097 if (inq89 != NULL) {
3098 kmem_free(inq89, inq89_len);
3101 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
3102 "0x89 for SATA target:%x failed!", ptgt->m_devhdl);
3103 return (DDI_SUCCESS);
3105 sid = (void *)(&inq89[60]);
3107 swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
3108 swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
3110 model[SATA_ID_MODEL_LEN] = 0;
3111 fw[SATA_ID_FW_LEN] = 0;
3114 * split model into into vid/pid
3116 for (i = 0, pid = model; i < SATA_ID_MODEL_LEN; i++, pid++)
3117 if ((*pid == ' ') || (*pid == '\t'))
3118 break;
3119 if (i < SATA_ID_MODEL_LEN) {
3120 vid = model;
3122 * terminate vid, establish pid
3124 *pid++ = 0;
3125 } else {
3127 * vid will stay "ATA ", the rule is same
3128 * as sata framework implementation.
3130 vid = NULL;
3132 * model is all pid
3134 pid = model;
3138 * override SCSA "inquiry-*" properties
3140 if (vid)
3141 (void) scsi_device_prop_update_inqstring(sd,
3142 INQUIRY_VENDOR_ID, vid, strlen(vid));
3143 if (pid)
3144 (void) scsi_device_prop_update_inqstring(sd,
3145 INQUIRY_PRODUCT_ID, pid, strlen(pid));
3146 (void) scsi_device_prop_update_inqstring(sd,
3147 INQUIRY_REVISION_ID, fw, strlen(fw));
3149 if (inq89 != NULL) {
3150 kmem_free(inq89, inq89_len);
3152 } else {
3153 mutex_exit(&mpt->m_mutex);
3156 return (DDI_SUCCESS);
3159 * tran_tgt_free(9E) - target device instance deallocation
3161 static void
3162 mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
3163 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
3165 #ifndef __lock_lint
3166 _NOTE(ARGUNUSED(hba_dip, tgt_dip, hba_tran, sd))
3167 #endif
3169 mptsas_tgt_private_t *tgt_private = hba_tran->tran_tgt_private;
3171 if (tgt_private != NULL) {
3172 kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
3173 hba_tran->tran_tgt_private = NULL;
3178 * scsi_pkt handling
3180 * Visible to the external world via the transport structure.
3184 * Notes:
3185 * - transport the command to the addressed SCSI target/lun device
3186 * - normal operation is to schedule the command to be transported,
3187 * and return TRAN_ACCEPT if this is successful.
3188 * - if NO_INTR, tran_start must poll device for command completion
3190 static int
3191 mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
3193 #ifndef __lock_lint
3194 _NOTE(ARGUNUSED(ap))
3195 #endif
3196 mptsas_t *mpt = PKT2MPT(pkt);
3197 mptsas_cmd_t *cmd = PKT2CMD(pkt);
3198 int rval;
3199 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3201 NDBG1(("mptsas_scsi_start: pkt=0x%p", (void *)pkt));
3202 ASSERT(ptgt);
3203 if (ptgt == NULL)
3204 return (TRAN_FATAL_ERROR);
3207 * prepare the pkt before taking mutex.
3209 rval = mptsas_prepare_pkt(cmd);
3210 if (rval != TRAN_ACCEPT) {
3211 return (rval);
3215 * Send the command to target/lun, however your HBA requires it.
3216 * If busy, return TRAN_BUSY; if there's some other formatting error
3217 * in the packet, return TRAN_BADPKT; otherwise, fall through to the
3218 * return of TRAN_ACCEPT.
3220 * Remember that access to shared resources, including the mptsas_t
3221 * data structure and the HBA hardware registers, must be protected
3222 * with mutexes, here and everywhere.
3224 * Also remember that at interrupt time, you'll get an argument
3225 * to the interrupt handler which is a pointer to your mptsas_t
3226 * structure; you'll have to remember which commands are outstanding
3227 * and which scsi_pkt is the currently-running command so the
3228 * interrupt handler can refer to the pkt to set completion
3229 * status, call the target driver back through pkt_comp, etc.
3231 * If the instance lock is held by other thread, don't spin to wait
3232 * for it. Instead, queue the cmd and next time when the instance lock
3233 * is not held, accept all the queued cmd. A extra tx_waitq is
3234 * introduced to protect the queue.
3236 * The polled cmd will not be queud and accepted as usual.
3238 * Under the tx_waitq mutex, record whether a thread is draining
3239 * the tx_waitq. An IO requesting thread that finds the instance
3240 * mutex contended appends to the tx_waitq and while holding the
3241 * tx_wait mutex, if the draining flag is not set, sets it and then
3242 * proceeds to spin for the instance mutex. This scheme ensures that
3243 * the last cmd in a burst be processed.
3245 * we enable this feature only when the helper threads are enabled,
3246 * at which we think the loads are heavy.
3248 * per instance mutex m_tx_waitq_mutex is introduced to protect the
3249 * m_tx_waitqtail, m_tx_waitq, m_tx_draining.
3252 if (mpt->m_doneq_thread_n) {
3253 if (mutex_tryenter(&mpt->m_mutex) != 0) {
3254 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3255 mutex_exit(&mpt->m_mutex);
3256 } else if (cmd->cmd_pkt_flags & FLAG_NOINTR) {
3257 mutex_enter(&mpt->m_mutex);
3258 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3259 mutex_exit(&mpt->m_mutex);
3260 } else {
3261 mutex_enter(&mpt->m_tx_waitq_mutex);
3263 * ptgt->m_dr_flag is protected by m_mutex or
3264 * m_tx_waitq_mutex. In this case, m_tx_waitq_mutex
3265 * is acquired.
3267 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3268 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3270 * The command should be allowed to
3271 * retry by returning TRAN_BUSY to
3272 * to stall the I/O's which come from
3273 * scsi_vhci since the device/path is
3274 * in unstable state now.
3276 mutex_exit(&mpt->m_tx_waitq_mutex);
3277 return (TRAN_BUSY);
3278 } else {
3280 * The device is offline, just fail the
3281 * command by returning
3282 * TRAN_FATAL_ERROR.
3284 mutex_exit(&mpt->m_tx_waitq_mutex);
3285 return (TRAN_FATAL_ERROR);
3288 if (mpt->m_tx_draining) {
3289 cmd->cmd_flags |= CFLAG_TXQ;
3290 *mpt->m_tx_waitqtail = cmd;
3291 mpt->m_tx_waitqtail = &cmd->cmd_linkp;
3292 mutex_exit(&mpt->m_tx_waitq_mutex);
3293 } else { /* drain the queue */
3294 mpt->m_tx_draining = 1;
3295 mutex_exit(&mpt->m_tx_waitq_mutex);
3296 mutex_enter(&mpt->m_mutex);
3297 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3298 mutex_exit(&mpt->m_mutex);
3301 } else {
3302 mutex_enter(&mpt->m_mutex);
3304 * ptgt->m_dr_flag is protected by m_mutex or m_tx_waitq_mutex
3305 * in this case, m_mutex is acquired.
3307 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3308 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3310 * commands should be allowed to retry by
3311 * returning TRAN_BUSY to stall the I/O's
3312 * which come from scsi_vhci since the device/
3313 * path is in unstable state now.
3315 mutex_exit(&mpt->m_mutex);
3316 return (TRAN_BUSY);
3317 } else {
3319 * The device is offline, just fail the
3320 * command by returning TRAN_FATAL_ERROR.
3322 mutex_exit(&mpt->m_mutex);
3323 return (TRAN_FATAL_ERROR);
3326 rval = mptsas_accept_pkt(mpt, cmd);
3327 mutex_exit(&mpt->m_mutex);
3330 return (rval);
3334 * Accept all the queued cmds(if any) before accept the current one.
3336 static int
3337 mptsas_accept_txwq_and_pkt(mptsas_t *mpt, mptsas_cmd_t *cmd)
3339 int rval;
3340 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3342 ASSERT(mutex_owned(&mpt->m_mutex));
3344 * The call to mptsas_accept_tx_waitq() must always be performed
3345 * because that is where mpt->m_tx_draining is cleared.
3347 mutex_enter(&mpt->m_tx_waitq_mutex);
3348 mptsas_accept_tx_waitq(mpt);
3349 mutex_exit(&mpt->m_tx_waitq_mutex);
3351 * ptgt->m_dr_flag is protected by m_mutex or m_tx_waitq_mutex
3352 * in this case, m_mutex is acquired.
3354 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3355 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3357 * The command should be allowed to retry by returning
3358 * TRAN_BUSY to stall the I/O's which come from
3359 * scsi_vhci since the device/path is in unstable state
3360 * now.
3362 return (TRAN_BUSY);
3363 } else {
3365 * The device is offline, just fail the command by
3366 * return TRAN_FATAL_ERROR.
3368 return (TRAN_FATAL_ERROR);
3371 rval = mptsas_accept_pkt(mpt, cmd);
3373 return (rval);
3376 static int
3377 mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *cmd)
3379 int rval = TRAN_ACCEPT;
3380 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3382 NDBG1(("mptsas_accept_pkt: cmd=0x%p", (void *)cmd));
3384 ASSERT(mutex_owned(&mpt->m_mutex));
3386 if ((cmd->cmd_flags & CFLAG_PREPARED) == 0) {
3387 rval = mptsas_prepare_pkt(cmd);
3388 if (rval != TRAN_ACCEPT) {
3389 cmd->cmd_flags &= ~CFLAG_TRANFLAG;
3390 return (rval);
3395 * reset the throttle if we were draining
3397 if ((ptgt->m_t_ncmds == 0) &&
3398 (ptgt->m_t_throttle == DRAIN_THROTTLE)) {
3399 NDBG23(("reset throttle"));
3400 ASSERT(ptgt->m_reset_delay == 0);
3401 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
3405 * If HBA is being reset, the DevHandles are being re-initialized,
3406 * which means that they could be invalid even if the target is still
3407 * attached. Check if being reset and if DevHandle is being
3408 * re-initialized. If this is the case, return BUSY so the I/O can be
3409 * retried later.
3411 if ((ptgt->m_devhdl == MPTSAS_INVALID_DEVHDL) && mpt->m_in_reset) {
3412 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
3413 if (cmd->cmd_flags & CFLAG_TXQ) {
3414 mptsas_doneq_add(mpt, cmd);
3415 mptsas_doneq_empty(mpt);
3416 return (rval);
3417 } else {
3418 return (TRAN_BUSY);
3423 * If device handle has already been invalidated, just
3424 * fail the command. In theory, command from scsi_vhci
3425 * client is impossible send down command with invalid
3426 * devhdl since devhdl is set after path offline, target
3427 * driver is not suppose to select a offlined path.
3429 if (ptgt->m_devhdl == MPTSAS_INVALID_DEVHDL) {
3430 NDBG3(("rejecting command, it might because invalid devhdl "
3431 "request."));
3432 mptsas_set_pkt_reason(mpt, cmd, CMD_DEV_GONE, STAT_TERMINATED);
3433 if (cmd->cmd_flags & CFLAG_TXQ) {
3434 mptsas_doneq_add(mpt, cmd);
3435 mptsas_doneq_empty(mpt);
3436 return (rval);
3437 } else {
3438 return (TRAN_FATAL_ERROR);
3442 * The first case is the normal case. mpt gets a command from the
3443 * target driver and starts it.
3444 * Since SMID 0 is reserved and the TM slot is reserved, the actual max
3445 * commands is m_max_requests - 2.
3447 if ((mpt->m_ncmds <= (mpt->m_max_requests - 2)) &&
3448 (ptgt->m_t_throttle > HOLD_THROTTLE) &&
3449 (ptgt->m_t_ncmds < ptgt->m_t_throttle) &&
3450 (ptgt->m_reset_delay == 0) &&
3451 (ptgt->m_t_nwait == 0) &&
3452 ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0)) {
3453 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
3454 (void) mptsas_start_cmd(mpt, cmd);
3455 } else {
3456 mptsas_waitq_add(mpt, cmd);
3458 } else {
3460 * Add this pkt to the work queue
3462 mptsas_waitq_add(mpt, cmd);
3464 if (cmd->cmd_pkt_flags & FLAG_NOINTR) {
3465 (void) mptsas_poll(mpt, cmd, MPTSAS_POLL_TIME);
3468 * Only flush the doneq if this is not a TM
3469 * cmd. For TM cmds the flushing of the
3470 * doneq will be done in those routines.
3472 if ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
3473 mptsas_doneq_empty(mpt);
3477 return (rval);
3481 mptsas_save_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
3483 mptsas_slots_t *slots = mpt->m_active;
3484 uint_t slot, start_rotor;
3485 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3487 ASSERT(MUTEX_HELD(&mpt->m_mutex));
3490 * Account for reserved TM request slot and reserved SMID of 0.
3492 ASSERT(slots->m_n_normal == (mpt->m_max_requests - 2));
3495 * Find the next available slot, beginning at m_rotor. If no slot is
3496 * available, we'll return FALSE to indicate that. This mechanism
3497 * considers only the normal slots, not the reserved slot 0 nor the
3498 * task management slot m_n_normal + 1. The rotor is left to point to
3499 * the normal slot after the one we select, unless we select the last
3500 * normal slot in which case it returns to slot 1.
3502 start_rotor = slots->m_rotor;
3503 do {
3504 slot = slots->m_rotor++;
3505 if (slots->m_rotor > slots->m_n_normal)
3506 slots->m_rotor = 1;
3508 if (slots->m_rotor == start_rotor)
3509 break;
3510 } while (slots->m_slot[slot] != NULL);
3512 if (slots->m_slot[slot] != NULL)
3513 return (FALSE);
3515 ASSERT(slot != 0 && slot <= slots->m_n_normal);
3517 cmd->cmd_slot = slot;
3518 slots->m_slot[slot] = cmd;
3519 mpt->m_ncmds++;
3522 * only increment per target ncmds if this is not a
3523 * command that has no target associated with it (i.e. a
3524 * event acknoledgment)
3526 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
3528 * Expiration time is set in mptsas_start_cmd
3530 ptgt->m_t_ncmds++;
3531 cmd->cmd_active_expiration = 0;
3532 } else {
3534 * Initialize expiration time for passthrough commands,
3536 cmd->cmd_active_expiration = gethrtime() +
3537 (hrtime_t)cmd->cmd_pkt->pkt_time * NANOSEC;
3539 return (TRUE);
3543 * prepare the pkt:
3544 * the pkt may have been resubmitted or just reused so
3545 * initialize some fields and do some checks.
3547 static int
3548 mptsas_prepare_pkt(mptsas_cmd_t *cmd)
3550 struct scsi_pkt *pkt = CMD2PKT(cmd);
3552 NDBG1(("mptsas_prepare_pkt: cmd=0x%p", (void *)cmd));
3555 * Reinitialize some fields that need it; the packet may
3556 * have been resubmitted
3558 pkt->pkt_reason = CMD_CMPLT;
3559 pkt->pkt_state = 0;
3560 pkt->pkt_statistics = 0;
3561 pkt->pkt_resid = 0;
3562 cmd->cmd_age = 0;
3563 cmd->cmd_pkt_flags = pkt->pkt_flags;
3566 * zero status byte.
3568 *(pkt->pkt_scbp) = 0;
3570 if (cmd->cmd_flags & CFLAG_DMAVALID) {
3571 pkt->pkt_resid = cmd->cmd_dmacount;
3574 * consistent packets need to be sync'ed first
3575 * (only for data going out)
3577 if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
3578 (cmd->cmd_flags & CFLAG_DMASEND)) {
3579 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
3580 DDI_DMA_SYNC_FORDEV);
3584 cmd->cmd_flags =
3585 (cmd->cmd_flags & ~(CFLAG_TRANFLAG)) |
3586 CFLAG_PREPARED | CFLAG_IN_TRANSPORT;
3588 return (TRAN_ACCEPT);
3592 * tran_init_pkt(9E) - allocate scsi_pkt(9S) for command
3594 * One of three possibilities:
3595 * - allocate scsi_pkt
3596 * - allocate scsi_pkt and DMA resources
3597 * - allocate DMA resources to an already-allocated pkt
3599 static struct scsi_pkt *
3600 mptsas_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
3601 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
3602 int (*callback)(), caddr_t arg)
3604 mptsas_cmd_t *cmd, *new_cmd;
3605 mptsas_t *mpt = ADDR2MPT(ap);
3606 int failure = 1;
3607 uint_t oldcookiec;
3608 mptsas_target_t *ptgt = NULL;
3609 int rval;
3610 mptsas_tgt_private_t *tgt_private;
3611 int kf;
3613 kf = (callback == SLEEP_FUNC)? KM_SLEEP: KM_NOSLEEP;
3615 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
3616 tran_tgt_private;
3617 ASSERT(tgt_private != NULL);
3618 if (tgt_private == NULL) {
3619 return (NULL);
3621 ptgt = tgt_private->t_private;
3622 ASSERT(ptgt != NULL);
3623 if (ptgt == NULL)
3624 return (NULL);
3625 ap->a_target = ptgt->m_devhdl;
3626 ap->a_lun = tgt_private->t_lun;
3628 ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
3629 #ifdef MPTSAS_TEST_EXTRN_ALLOC
3630 statuslen *= 100; tgtlen *= 4;
3631 #endif
3632 NDBG3(("mptsas_scsi_init_pkt:\n"
3633 "\ttgt=%d in=0x%p bp=0x%p clen=%d slen=%d tlen=%d flags=%x",
3634 ap->a_target, (void *)pkt, (void *)bp,
3635 cmdlen, statuslen, tgtlen, flags));
3638 * Allocate the new packet.
3640 if (pkt == NULL) {
3641 ddi_dma_handle_t save_dma_handle;
3643 cmd = kmem_cache_alloc(mpt->m_kmem_cache, kf);
3645 if (cmd) {
3646 save_dma_handle = cmd->cmd_dmahandle;
3647 bzero(cmd, sizeof (*cmd) + scsi_pkt_size());
3648 cmd->cmd_dmahandle = save_dma_handle;
3650 pkt = (void *)((uchar_t *)cmd +
3651 sizeof (struct mptsas_cmd));
3652 pkt->pkt_ha_private = (opaque_t)cmd;
3653 pkt->pkt_address = *ap;
3654 pkt->pkt_private = (opaque_t)cmd->cmd_pkt_private;
3655 pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb;
3656 pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb;
3657 cmd->cmd_pkt = (struct scsi_pkt *)pkt;
3658 cmd->cmd_cdblen = (uchar_t)cmdlen;
3659 cmd->cmd_scblen = statuslen;
3660 cmd->cmd_rqslen = SENSE_LENGTH;
3661 cmd->cmd_tgt_addr = ptgt;
3662 failure = 0;
3665 if (failure || (cmdlen > sizeof (cmd->cmd_cdb)) ||
3666 (tgtlen > PKT_PRIV_LEN) ||
3667 (statuslen > EXTCMDS_STATUS_SIZE)) {
3668 if (failure == 0) {
3670 * if extern alloc fails, all will be
3671 * deallocated, including cmd
3673 failure = mptsas_pkt_alloc_extern(mpt, cmd,
3674 cmdlen, tgtlen, statuslen, kf);
3676 if (failure) {
3678 * if extern allocation fails, it will
3679 * deallocate the new pkt as well
3681 return (NULL);
3684 new_cmd = cmd;
3686 } else {
3687 cmd = PKT2CMD(pkt);
3688 new_cmd = NULL;
3692 /* grab cmd->cmd_cookiec here as oldcookiec */
3694 oldcookiec = cmd->cmd_cookiec;
3697 * If the dma was broken up into PARTIAL transfers cmd_nwin will be
3698 * greater than 0 and we'll need to grab the next dma window
3701 * SLM-not doing extra command frame right now; may add later
3704 if (cmd->cmd_nwin > 0) {
3707 * Make sure we havn't gone past the the total number
3708 * of windows
3710 if (++cmd->cmd_winindex >= cmd->cmd_nwin) {
3711 return (NULL);
3713 if (ddi_dma_getwin(cmd->cmd_dmahandle, cmd->cmd_winindex,
3714 &cmd->cmd_dma_offset, &cmd->cmd_dma_len,
3715 &cmd->cmd_cookie, &cmd->cmd_cookiec) == DDI_FAILURE) {
3716 return (NULL);
3718 goto get_dma_cookies;
3722 if (flags & PKT_XARQ) {
3723 cmd->cmd_flags |= CFLAG_XARQ;
3727 * DMA resource allocation. This version assumes your
3728 * HBA has some sort of bus-mastering or onboard DMA capability, with a
3729 * scatter-gather list of length MPTSAS_MAX_DMA_SEGS, as given in the
3730 * ddi_dma_attr_t structure and passed to scsi_impl_dmaget.
3732 if (bp && (bp->b_bcount != 0) &&
3733 (cmd->cmd_flags & CFLAG_DMAVALID) == 0) {
3735 int cnt, dma_flags;
3736 mptti_t *dmap; /* ptr to the S/G list */
3739 * Set up DMA memory and position to the next DMA segment.
3741 ASSERT(cmd->cmd_dmahandle != NULL);
3743 if (bp->b_flags & B_READ) {
3744 dma_flags = DDI_DMA_READ;
3745 cmd->cmd_flags &= ~CFLAG_DMASEND;
3746 } else {
3747 dma_flags = DDI_DMA_WRITE;
3748 cmd->cmd_flags |= CFLAG_DMASEND;
3750 if (flags & PKT_CONSISTENT) {
3751 cmd->cmd_flags |= CFLAG_CMDIOPB;
3752 dma_flags |= DDI_DMA_CONSISTENT;
3755 if (flags & PKT_DMA_PARTIAL) {
3756 dma_flags |= DDI_DMA_PARTIAL;
3760 * workaround for byte hole issue on psycho and
3761 * schizo pre 2.1
3763 if ((bp->b_flags & B_READ) && ((bp->b_flags &
3764 (B_PAGEIO|B_REMAPPED)) != B_PAGEIO) &&
3765 ((uintptr_t)bp->b_un.b_addr & 0x7)) {
3766 dma_flags |= DDI_DMA_CONSISTENT;
3769 rval = ddi_dma_buf_bind_handle(cmd->cmd_dmahandle, bp,
3770 dma_flags, callback, arg,
3771 &cmd->cmd_cookie, &cmd->cmd_cookiec);
3772 if (rval == DDI_DMA_PARTIAL_MAP) {
3773 (void) ddi_dma_numwin(cmd->cmd_dmahandle,
3774 &cmd->cmd_nwin);
3775 cmd->cmd_winindex = 0;
3776 (void) ddi_dma_getwin(cmd->cmd_dmahandle,
3777 cmd->cmd_winindex, &cmd->cmd_dma_offset,
3778 &cmd->cmd_dma_len, &cmd->cmd_cookie,
3779 &cmd->cmd_cookiec);
3780 } else if (rval && (rval != DDI_DMA_MAPPED)) {
3781 switch (rval) {
3782 case DDI_DMA_NORESOURCES:
3783 bioerror(bp, 0);
3784 break;
3785 case DDI_DMA_BADATTR:
3786 case DDI_DMA_NOMAPPING:
3787 bioerror(bp, EFAULT);
3788 break;
3789 case DDI_DMA_TOOBIG:
3790 default:
3791 bioerror(bp, EINVAL);
3792 break;
3794 cmd->cmd_flags &= ~CFLAG_DMAVALID;
3795 if (new_cmd) {
3796 mptsas_scsi_destroy_pkt(ap, pkt);
3798 return ((struct scsi_pkt *)NULL);
3801 get_dma_cookies:
3802 cmd->cmd_flags |= CFLAG_DMAVALID;
3803 ASSERT(cmd->cmd_cookiec > 0);
3805 if (cmd->cmd_cookiec > MPTSAS_MAX_CMD_SEGS) {
3806 mptsas_log(mpt, CE_NOTE, "large cookiec received %d\n",
3807 cmd->cmd_cookiec);
3808 bioerror(bp, EINVAL);
3809 if (new_cmd) {
3810 mptsas_scsi_destroy_pkt(ap, pkt);
3812 return ((struct scsi_pkt *)NULL);
3816 * Allocate extra SGL buffer if needed.
3818 if ((cmd->cmd_cookiec > MPTSAS_MAX_FRAME_SGES64(mpt)) &&
3819 (cmd->cmd_extra_frames == NULL)) {
3820 if (mptsas_alloc_extra_sgl_frame(mpt, cmd) ==
3821 DDI_FAILURE) {
3822 mptsas_log(mpt, CE_WARN, "MPT SGL mem alloc "
3823 "failed");
3824 bioerror(bp, ENOMEM);
3825 if (new_cmd) {
3826 mptsas_scsi_destroy_pkt(ap, pkt);
3828 return ((struct scsi_pkt *)NULL);
3833 * Always use scatter-gather transfer
3834 * Use the loop below to store physical addresses of
3835 * DMA segments, from the DMA cookies, into your HBA's
3836 * scatter-gather list.
3837 * We need to ensure we have enough kmem alloc'd
3838 * for the sg entries since we are no longer using an
3839 * array inside mptsas_cmd_t.
3841 * We check cmd->cmd_cookiec against oldcookiec so
3842 * the scatter-gather list is correctly allocated
3845 if (oldcookiec != cmd->cmd_cookiec) {
3846 if (cmd->cmd_sg != (mptti_t *)NULL) {
3847 kmem_free(cmd->cmd_sg, sizeof (mptti_t) *
3848 oldcookiec);
3849 cmd->cmd_sg = NULL;
3853 if (cmd->cmd_sg == (mptti_t *)NULL) {
3854 cmd->cmd_sg = kmem_alloc((size_t)(sizeof (mptti_t)*
3855 cmd->cmd_cookiec), kf);
3857 if (cmd->cmd_sg == (mptti_t *)NULL) {
3858 mptsas_log(mpt, CE_WARN,
3859 "unable to kmem_alloc enough memory "
3860 "for scatter/gather list");
3862 * if we have an ENOMEM condition we need to behave
3863 * the same way as the rest of this routine
3866 bioerror(bp, ENOMEM);
3867 if (new_cmd) {
3868 mptsas_scsi_destroy_pkt(ap, pkt);
3870 return ((struct scsi_pkt *)NULL);
3874 dmap = cmd->cmd_sg;
3876 ASSERT(cmd->cmd_cookie.dmac_size != 0);
3879 * store the first segment into the S/G list
3881 dmap->count = cmd->cmd_cookie.dmac_size;
3882 dmap->addr.address64.Low = (uint32_t)
3883 (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3884 dmap->addr.address64.High = (uint32_t)
3885 (cmd->cmd_cookie.dmac_laddress >> 32);
3888 * dmacount counts the size of the dma for this window
3889 * (if partial dma is being used). totaldmacount
3890 * keeps track of the total amount of dma we have
3891 * transferred for all the windows (needed to calculate
3892 * the resid value below).
3894 cmd->cmd_dmacount = cmd->cmd_cookie.dmac_size;
3895 cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3898 * We already stored the first DMA scatter gather segment,
3899 * start at 1 if we need to store more.
3901 for (cnt = 1; cnt < cmd->cmd_cookiec; cnt++) {
3903 * Get next DMA cookie
3905 ddi_dma_nextcookie(cmd->cmd_dmahandle,
3906 &cmd->cmd_cookie);
3907 dmap++;
3909 cmd->cmd_dmacount += cmd->cmd_cookie.dmac_size;
3910 cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3913 * store the segment parms into the S/G list
3915 dmap->count = cmd->cmd_cookie.dmac_size;
3916 dmap->addr.address64.Low = (uint32_t)
3917 (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3918 dmap->addr.address64.High = (uint32_t)
3919 (cmd->cmd_cookie.dmac_laddress >> 32);
3923 * If this was partially allocated we set the resid
3924 * the amount of data NOT transferred in this window
3925 * If there is only one window, the resid will be 0
3927 pkt->pkt_resid = (bp->b_bcount - cmd->cmd_totaldmacount);
3928 NDBG3(("mptsas_scsi_init_pkt: cmd_dmacount=%d.",
3929 cmd->cmd_dmacount));
3931 return (pkt);
3935 * tran_destroy_pkt(9E) - scsi_pkt(9s) deallocation
3937 * Notes:
3938 * - also frees DMA resources if allocated
3939 * - implicit DMA synchonization
3941 static void
3942 mptsas_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3944 mptsas_cmd_t *cmd = PKT2CMD(pkt);
3945 mptsas_t *mpt = ADDR2MPT(ap);
3947 NDBG3(("mptsas_scsi_destroy_pkt: target=%d pkt=0x%p",
3948 ap->a_target, (void *)pkt));
3950 if (cmd->cmd_flags & CFLAG_DMAVALID) {
3951 (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
3952 cmd->cmd_flags &= ~CFLAG_DMAVALID;
3955 if (cmd->cmd_sg) {
3956 kmem_free(cmd->cmd_sg, sizeof (mptti_t) * cmd->cmd_cookiec);
3957 cmd->cmd_sg = NULL;
3960 mptsas_free_extra_sgl_frame(mpt, cmd);
3962 if ((cmd->cmd_flags &
3963 (CFLAG_FREE | CFLAG_CDBEXTERN | CFLAG_PRIVEXTERN |
3964 CFLAG_SCBEXTERN)) == 0) {
3965 cmd->cmd_flags = CFLAG_FREE;
3966 kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
3967 } else {
3968 mptsas_pkt_destroy_extern(mpt, cmd);
3973 * kmem cache constructor and destructor:
3974 * When constructing, we bzero the cmd and allocate the dma handle
3975 * When destructing, just free the dma handle
3977 static int
3978 mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags)
3980 mptsas_cmd_t *cmd = buf;
3981 mptsas_t *mpt = cdrarg;
3982 int (*callback)(caddr_t);
3984 callback = (kmflags == KM_SLEEP)? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
3986 NDBG4(("mptsas_kmem_cache_constructor"));
3989 * allocate a dma handle
3991 if ((ddi_dma_alloc_handle(mpt->m_dip, &mpt->m_io_dma_attr, callback,
3992 NULL, &cmd->cmd_dmahandle)) != DDI_SUCCESS) {
3993 cmd->cmd_dmahandle = NULL;
3994 return (-1);
3996 return (0);
3999 static void
4000 mptsas_kmem_cache_destructor(void *buf, void *cdrarg)
4002 #ifndef __lock_lint
4003 _NOTE(ARGUNUSED(cdrarg))
4004 #endif
4005 mptsas_cmd_t *cmd = buf;
4007 NDBG4(("mptsas_kmem_cache_destructor"));
4009 if (cmd->cmd_dmahandle) {
4010 ddi_dma_free_handle(&cmd->cmd_dmahandle);
4011 cmd->cmd_dmahandle = NULL;
4015 static int
4016 mptsas_cache_frames_constructor(void *buf, void *cdrarg, int kmflags)
4018 mptsas_cache_frames_t *p = buf;
4019 mptsas_t *mpt = cdrarg;
4020 ddi_dma_attr_t frame_dma_attr;
4021 size_t mem_size, alloc_len;
4022 ddi_dma_cookie_t cookie;
4023 uint_t ncookie;
4024 int (*callback)(caddr_t) = (kmflags == KM_SLEEP)
4025 ? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
4027 frame_dma_attr = mpt->m_msg_dma_attr;
4028 frame_dma_attr.dma_attr_align = 0x10;
4029 frame_dma_attr.dma_attr_sgllen = 1;
4031 if (ddi_dma_alloc_handle(mpt->m_dip, &frame_dma_attr, callback, NULL,
4032 &p->m_dma_hdl) != DDI_SUCCESS) {
4033 mptsas_log(mpt, CE_WARN, "Unable to allocate dma handle for"
4034 " extra SGL.");
4035 return (DDI_FAILURE);
4038 mem_size = (mpt->m_max_request_frames - 1) * mpt->m_req_frame_size;
4040 if (ddi_dma_mem_alloc(p->m_dma_hdl, mem_size, &mpt->m_dev_acc_attr,
4041 DDI_DMA_CONSISTENT, callback, NULL, (caddr_t *)&p->m_frames_addr,
4042 &alloc_len, &p->m_acc_hdl) != DDI_SUCCESS) {
4043 ddi_dma_free_handle(&p->m_dma_hdl);
4044 p->m_dma_hdl = NULL;
4045 mptsas_log(mpt, CE_WARN, "Unable to allocate dma memory for"
4046 " extra SGL.");
4047 return (DDI_FAILURE);
4050 if (ddi_dma_addr_bind_handle(p->m_dma_hdl, NULL, p->m_frames_addr,
4051 alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, callback, NULL,
4052 &cookie, &ncookie) != DDI_DMA_MAPPED) {
4053 (void) ddi_dma_mem_free(&p->m_acc_hdl);
4054 ddi_dma_free_handle(&p->m_dma_hdl);
4055 p->m_dma_hdl = NULL;
4056 mptsas_log(mpt, CE_WARN, "Unable to bind DMA resources for"
4057 " extra SGL");
4058 return (DDI_FAILURE);
4062 * Store the SGL memory address. This chip uses this
4063 * address to dma to and from the driver. The second
4064 * address is the address mpt uses to fill in the SGL.
4066 p->m_phys_addr = cookie.dmac_laddress;
4068 return (DDI_SUCCESS);
4071 static void
4072 mptsas_cache_frames_destructor(void *buf, void *cdrarg)
4074 #ifndef __lock_lint
4075 _NOTE(ARGUNUSED(cdrarg))
4076 #endif
4077 mptsas_cache_frames_t *p = buf;
4078 if (p->m_dma_hdl != NULL) {
4079 (void) ddi_dma_unbind_handle(p->m_dma_hdl);
4080 (void) ddi_dma_mem_free(&p->m_acc_hdl);
4081 ddi_dma_free_handle(&p->m_dma_hdl);
4082 p->m_phys_addr = NULL;
4083 p->m_frames_addr = NULL;
4084 p->m_dma_hdl = NULL;
4085 p->m_acc_hdl = NULL;
4091 * Figure out if we need to use a different method for the request
4092 * sense buffer and allocate from the map if necessary.
4094 static boolean_t
4095 mptsas_cmdarqsize(mptsas_t *mpt, mptsas_cmd_t *cmd, size_t senselength, int kf)
4097 if (senselength > mpt->m_req_sense_size) {
4098 unsigned long i;
4100 /* Sense length is limited to an 8 bit value in MPI Spec. */
4101 if (senselength > 255)
4102 senselength = 255;
4103 cmd->cmd_extrqschunks = (senselength +
4104 (mpt->m_req_sense_size - 1))/mpt->m_req_sense_size;
4105 i = (kf == KM_SLEEP ? rmalloc_wait : rmalloc)
4106 (mpt->m_erqsense_map, cmd->cmd_extrqschunks);
4108 if (i == 0)
4109 return (B_FALSE);
4111 cmd->cmd_extrqslen = (uint16_t)senselength;
4112 cmd->cmd_extrqsidx = i - 1;
4113 cmd->cmd_arq_buf = mpt->m_extreq_sense +
4114 (cmd->cmd_extrqsidx * mpt->m_req_sense_size);
4115 } else {
4116 cmd->cmd_rqslen = (uchar_t)senselength;
4119 return (B_TRUE);
4123 * allocate and deallocate external pkt space (ie. not part of mptsas_cmd)
4124 * for non-standard length cdb, pkt_private, status areas
4125 * if allocation fails, then deallocate all external space and the pkt
4127 /* ARGSUSED */
4128 static int
4129 mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
4130 int cmdlen, int tgtlen, int statuslen, int kf)
4132 caddr_t cdbp, scbp, tgt;
4134 NDBG3(("mptsas_pkt_alloc_extern: "
4135 "cmd=0x%p cmdlen=%d tgtlen=%d statuslen=%d kf=%x",
4136 (void *)cmd, cmdlen, tgtlen, statuslen, kf));
4138 tgt = cdbp = scbp = NULL;
4139 cmd->cmd_scblen = statuslen;
4140 cmd->cmd_privlen = (uchar_t)tgtlen;
4142 if (cmdlen > sizeof (cmd->cmd_cdb)) {
4143 if ((cdbp = kmem_zalloc((size_t)cmdlen, kf)) == NULL) {
4144 goto fail;
4146 cmd->cmd_pkt->pkt_cdbp = (opaque_t)cdbp;
4147 cmd->cmd_flags |= CFLAG_CDBEXTERN;
4149 if (tgtlen > PKT_PRIV_LEN) {
4150 if ((tgt = kmem_zalloc((size_t)tgtlen, kf)) == NULL) {
4151 goto fail;
4153 cmd->cmd_flags |= CFLAG_PRIVEXTERN;
4154 cmd->cmd_pkt->pkt_private = tgt;
4156 if (statuslen > EXTCMDS_STATUS_SIZE) {
4157 if ((scbp = kmem_zalloc((size_t)statuslen, kf)) == NULL) {
4158 goto fail;
4160 cmd->cmd_flags |= CFLAG_SCBEXTERN;
4161 cmd->cmd_pkt->pkt_scbp = (opaque_t)scbp;
4163 /* allocate sense data buf for DMA */
4164 if (mptsas_cmdarqsize(mpt, cmd, statuslen -
4165 MPTSAS_GET_ITEM_OFF(struct scsi_arq_status, sts_sensedata),
4166 kf) == B_FALSE)
4167 goto fail;
4169 return (0);
4170 fail:
4171 mptsas_pkt_destroy_extern(mpt, cmd);
4172 return (1);
4176 * deallocate external pkt space and deallocate the pkt
4178 static void
4179 mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd)
4181 NDBG3(("mptsas_pkt_destroy_extern: cmd=0x%p", (void *)cmd));
4183 if (cmd->cmd_flags & CFLAG_FREE) {
4184 mptsas_log(mpt, CE_PANIC,
4185 "mptsas_pkt_destroy_extern: freeing free packet");
4186 _NOTE(NOT_REACHED)
4187 /* NOTREACHED */
4189 if (cmd->cmd_extrqslen != 0) {
4190 rmfree(mpt->m_erqsense_map, cmd->cmd_extrqschunks,
4191 cmd->cmd_extrqsidx + 1);
4193 if (cmd->cmd_flags & CFLAG_CDBEXTERN) {
4194 kmem_free(cmd->cmd_pkt->pkt_cdbp, (size_t)cmd->cmd_cdblen);
4196 if (cmd->cmd_flags & CFLAG_SCBEXTERN) {
4197 kmem_free(cmd->cmd_pkt->pkt_scbp, (size_t)cmd->cmd_scblen);
4199 if (cmd->cmd_flags & CFLAG_PRIVEXTERN) {
4200 kmem_free(cmd->cmd_pkt->pkt_private, (size_t)cmd->cmd_privlen);
4202 cmd->cmd_flags = CFLAG_FREE;
4203 kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
4207 * tran_sync_pkt(9E) - explicit DMA synchronization
4209 /*ARGSUSED*/
4210 static void
4211 mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
4213 mptsas_cmd_t *cmd = PKT2CMD(pkt);
4215 NDBG3(("mptsas_scsi_sync_pkt: target=%d, pkt=0x%p",
4216 ap->a_target, (void *)pkt));
4218 if (cmd->cmd_dmahandle) {
4219 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4220 (cmd->cmd_flags & CFLAG_DMASEND) ?
4221 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
4226 * tran_dmafree(9E) - deallocate DMA resources allocated for command
4228 /*ARGSUSED*/
4229 static void
4230 mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
4232 mptsas_cmd_t *cmd = PKT2CMD(pkt);
4233 mptsas_t *mpt = ADDR2MPT(ap);
4235 NDBG3(("mptsas_scsi_dmafree: target=%d pkt=0x%p",
4236 ap->a_target, (void *)pkt));
4238 if (cmd->cmd_flags & CFLAG_DMAVALID) {
4239 (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
4240 cmd->cmd_flags &= ~CFLAG_DMAVALID;
4243 mptsas_free_extra_sgl_frame(mpt, cmd);
4246 static void
4247 mptsas_pkt_comp(struct scsi_pkt *pkt, mptsas_cmd_t *cmd)
4249 if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
4250 (!(cmd->cmd_flags & CFLAG_DMASEND))) {
4251 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4252 DDI_DMA_SYNC_FORCPU);
4254 (*pkt->pkt_comp)(pkt);
4257 static void
4258 mptsas_sge_mainframe(mptsas_cmd_t *cmd, pMpi2SCSIIORequest_t frame,
4259 ddi_acc_handle_t acc_hdl, uint_t cookiec, uint32_t end_flags)
4261 pMpi2SGESimple64_t sge;
4262 mptti_t *dmap;
4263 uint32_t flags;
4265 dmap = cmd->cmd_sg;
4267 sge = (pMpi2SGESimple64_t)(&frame->SGL);
4268 while (cookiec--) {
4269 ddi_put32(acc_hdl,
4270 &sge->Address.Low, dmap->addr.address64.Low);
4271 ddi_put32(acc_hdl,
4272 &sge->Address.High, dmap->addr.address64.High);
4273 ddi_put32(acc_hdl, &sge->FlagsLength,
4274 dmap->count);
4275 flags = ddi_get32(acc_hdl, &sge->FlagsLength);
4276 flags |= ((uint32_t)
4277 (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4278 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4279 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4280 MPI2_SGE_FLAGS_SHIFT);
4283 * If this is the last cookie, we set the flags
4284 * to indicate so
4286 if (cookiec == 0) {
4287 flags |= end_flags;
4289 if (cmd->cmd_flags & CFLAG_DMASEND) {
4290 flags |= (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4291 MPI2_SGE_FLAGS_SHIFT);
4292 } else {
4293 flags |= (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4294 MPI2_SGE_FLAGS_SHIFT);
4296 ddi_put32(acc_hdl, &sge->FlagsLength, flags);
4297 dmap++;
4298 sge++;
4302 static void
4303 mptsas_sge_chain(mptsas_t *mpt, mptsas_cmd_t *cmd,
4304 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4306 pMpi2SGESimple64_t sge;
4307 pMpi2SGEChain64_t sgechain;
4308 uint64_t nframe_phys_addr;
4309 uint_t cookiec;
4310 mptti_t *dmap;
4311 uint32_t flags;
4314 * Save the number of entries in the DMA
4315 * Scatter/Gather list
4317 cookiec = cmd->cmd_cookiec;
4320 * Hereby we start to deal with multiple frames.
4321 * The process is as follows:
4322 * 1. Determine how many frames are needed for SGL element
4323 * storage; Note that all frames are stored in contiguous
4324 * memory space and in 64-bit DMA mode each element is
4325 * 3 double-words (12 bytes) long.
4326 * 2. Fill up the main frame. We need to do this separately
4327 * since it contains the SCSI IO request header and needs
4328 * dedicated processing. Note that the last 4 double-words
4329 * of the SCSI IO header is for SGL element storage
4330 * (MPI2_SGE_IO_UNION).
4331 * 3. Fill the chain element in the main frame, so the DMA
4332 * engine can use the following frames.
4333 * 4. Enter a loop to fill the remaining frames. Note that the
4334 * last frame contains no chain element. The remaining
4335 * frames go into the mpt SGL buffer allocated on the fly,
4336 * not immediately following the main message frame, as in
4337 * Gen1.
4338 * Some restrictions:
4339 * 1. For 64-bit DMA, the simple element and chain element
4340 * are both of 3 double-words (12 bytes) in size, even
4341 * though all frames are stored in the first 4G of mem
4342 * range and the higher 32-bits of the address are always 0.
4343 * 2. On some controllers (like the 1064/1068), a frame can
4344 * hold SGL elements with the last 1 or 2 double-words
4345 * (4 or 8 bytes) un-used. On these controllers, we should
4346 * recognize that there's not enough room for another SGL
4347 * element and move the sge pointer to the next frame.
4349 int i, j, k, l, frames, sgemax;
4350 int temp;
4351 uint8_t chainflags;
4352 uint16_t chainlength;
4353 mptsas_cache_frames_t *p;
4356 * Sgemax is the number of SGE's that will fit
4357 * each extra frame and frames is total
4358 * number of frames we'll need. 1 sge entry per
4359 * frame is reseverd for the chain element thus the -1 below.
4361 sgemax = ((mpt->m_req_frame_size / sizeof (MPI2_SGE_SIMPLE64))
4362 - 1);
4363 temp = (cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) / sgemax;
4366 * A little check to see if we need to round up the number
4367 * of frames we need
4369 if ((cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) - (temp *
4370 sgemax) > 1) {
4371 frames = (temp + 1);
4372 } else {
4373 frames = temp;
4375 dmap = cmd->cmd_sg;
4376 sge = (pMpi2SGESimple64_t)(&frame->SGL);
4379 * First fill in the main frame
4381 j = MPTSAS_MAX_FRAME_SGES64(mpt) - 1;
4382 mptsas_sge_mainframe(cmd, frame, acc_hdl, j,
4383 ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4384 MPI2_SGE_FLAGS_SHIFT));
4385 dmap += j;
4386 sge += j;
4387 j++;
4390 * Fill in the chain element in the main frame.
4391 * About calculation on ChainOffset:
4392 * 1. Struct msg_scsi_io_request has 4 double-words (16 bytes)
4393 * in the end reserved for SGL element storage
4394 * (MPI2_SGE_IO_UNION); we should count it in our
4395 * calculation. See its definition in the header file.
4396 * 2. Constant j is the counter of the current SGL element
4397 * that will be processed, and (j - 1) is the number of
4398 * SGL elements that have been processed (stored in the
4399 * main frame).
4400 * 3. ChainOffset value should be in units of double-words (4
4401 * bytes) so the last value should be divided by 4.
4403 ddi_put8(acc_hdl, &frame->ChainOffset,
4404 (sizeof (MPI2_SCSI_IO_REQUEST) -
4405 sizeof (MPI2_SGE_IO_UNION) +
4406 (j - 1) * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4407 sgechain = (pMpi2SGEChain64_t)sge;
4408 chainflags = (MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4409 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4410 MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4411 ddi_put8(acc_hdl, &sgechain->Flags, chainflags);
4414 * The size of the next frame is the accurate size of space
4415 * (in bytes) used to store the SGL elements. j is the counter
4416 * of SGL elements. (j - 1) is the number of SGL elements that
4417 * have been processed (stored in frames).
4419 if (frames >= 2) {
4420 ASSERT(mpt->m_req_frame_size >= sizeof (MPI2_SGE_SIMPLE64));
4421 chainlength = mpt->m_req_frame_size /
4422 sizeof (MPI2_SGE_SIMPLE64) *
4423 sizeof (MPI2_SGE_SIMPLE64);
4424 } else {
4425 chainlength = ((cookiec - (j - 1)) *
4426 sizeof (MPI2_SGE_SIMPLE64));
4429 p = cmd->cmd_extra_frames;
4431 ddi_put16(acc_hdl, &sgechain->Length, chainlength);
4432 ddi_put32(acc_hdl, &sgechain->Address.Low, p->m_phys_addr);
4433 ddi_put32(acc_hdl, &sgechain->Address.High, p->m_phys_addr >> 32);
4436 * If there are more than 2 frames left we have to
4437 * fill in the next chain offset to the location of
4438 * the chain element in the next frame.
4439 * sgemax is the number of simple elements in an extra
4440 * frame. Note that the value NextChainOffset should be
4441 * in double-words (4 bytes).
4443 if (frames >= 2) {
4444 ddi_put8(acc_hdl, &sgechain->NextChainOffset,
4445 (sgemax * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4446 } else {
4447 ddi_put8(acc_hdl, &sgechain->NextChainOffset, 0);
4451 * Jump to next frame;
4452 * Starting here, chain buffers go into the per command SGL.
4453 * This buffer is allocated when chain buffers are needed.
4455 sge = (pMpi2SGESimple64_t)p->m_frames_addr;
4456 i = cookiec;
4459 * Start filling in frames with SGE's. If we
4460 * reach the end of frame and still have SGE's
4461 * to fill we need to add a chain element and
4462 * use another frame. j will be our counter
4463 * for what cookie we are at and i will be
4464 * the total cookiec. k is the current frame
4466 for (k = 1; k <= frames; k++) {
4467 for (l = 1; (l <= (sgemax + 1)) && (j <= i); j++, l++) {
4470 * If we have reached the end of frame
4471 * and we have more SGE's to fill in
4472 * we have to fill the final entry
4473 * with a chain element and then
4474 * continue to the next frame
4476 if ((l == (sgemax + 1)) && (k != frames)) {
4477 sgechain = (pMpi2SGEChain64_t)sge;
4478 j--;
4479 chainflags = (
4480 MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4481 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4482 MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4483 ddi_put8(p->m_acc_hdl,
4484 &sgechain->Flags, chainflags);
4486 * k is the frame counter and (k + 1)
4487 * is the number of the next frame.
4488 * Note that frames are in contiguous
4489 * memory space.
4491 nframe_phys_addr = p->m_phys_addr +
4492 (mpt->m_req_frame_size * k);
4493 ddi_put32(p->m_acc_hdl,
4494 &sgechain->Address.Low,
4495 nframe_phys_addr);
4496 ddi_put32(p->m_acc_hdl,
4497 &sgechain->Address.High,
4498 nframe_phys_addr >> 32);
4501 * If there are more than 2 frames left
4502 * we have to next chain offset to
4503 * the location of the chain element
4504 * in the next frame and fill in the
4505 * length of the next chain
4507 if ((frames - k) >= 2) {
4508 ddi_put8(p->m_acc_hdl,
4509 &sgechain->NextChainOffset,
4510 (sgemax *
4511 sizeof (MPI2_SGE_SIMPLE64))
4512 >> 2);
4513 ddi_put16(p->m_acc_hdl,
4514 &sgechain->Length,
4515 mpt->m_req_frame_size /
4516 sizeof (MPI2_SGE_SIMPLE64) *
4517 sizeof (MPI2_SGE_SIMPLE64));
4518 } else {
4520 * This is the last frame. Set
4521 * the NextChainOffset to 0 and
4522 * Length is the total size of
4523 * all remaining simple elements
4525 ddi_put8(p->m_acc_hdl,
4526 &sgechain->NextChainOffset,
4528 ddi_put16(p->m_acc_hdl,
4529 &sgechain->Length,
4530 (cookiec - j) *
4531 sizeof (MPI2_SGE_SIMPLE64));
4534 /* Jump to the next frame */
4535 sge = (pMpi2SGESimple64_t)
4536 ((char *)p->m_frames_addr +
4537 (int)mpt->m_req_frame_size * k);
4539 continue;
4542 ddi_put32(p->m_acc_hdl,
4543 &sge->Address.Low,
4544 dmap->addr.address64.Low);
4545 ddi_put32(p->m_acc_hdl,
4546 &sge->Address.High,
4547 dmap->addr.address64.High);
4548 ddi_put32(p->m_acc_hdl,
4549 &sge->FlagsLength, dmap->count);
4550 flags = ddi_get32(p->m_acc_hdl,
4551 &sge->FlagsLength);
4552 flags |= ((uint32_t)(
4553 MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4554 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4555 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4556 MPI2_SGE_FLAGS_SHIFT);
4559 * If we are at the end of the frame and
4560 * there is another frame to fill in
4561 * we set the last simple element as last
4562 * element
4564 if ((l == sgemax) && (k != frames)) {
4565 flags |= ((uint32_t)
4566 (MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4567 MPI2_SGE_FLAGS_SHIFT);
4571 * If this is the final cookie we
4572 * indicate it by setting the flags
4574 if (j == i) {
4575 flags |= ((uint32_t)
4576 (MPI2_SGE_FLAGS_LAST_ELEMENT |
4577 MPI2_SGE_FLAGS_END_OF_BUFFER |
4578 MPI2_SGE_FLAGS_END_OF_LIST) <<
4579 MPI2_SGE_FLAGS_SHIFT);
4581 if (cmd->cmd_flags & CFLAG_DMASEND) {
4582 flags |=
4583 (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4584 MPI2_SGE_FLAGS_SHIFT);
4585 } else {
4586 flags |=
4587 (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4588 MPI2_SGE_FLAGS_SHIFT);
4590 ddi_put32(p->m_acc_hdl,
4591 &sge->FlagsLength, flags);
4592 dmap++;
4593 sge++;
4598 * Sync DMA with the chain buffers that were just created
4600 (void) ddi_dma_sync(p->m_dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
4603 static void
4604 mptsas_ieee_sge_mainframe(mptsas_cmd_t *cmd, pMpi2SCSIIORequest_t frame,
4605 ddi_acc_handle_t acc_hdl, uint_t cookiec, uint8_t end_flag)
4607 pMpi2IeeeSgeSimple64_t ieeesge;
4608 mptti_t *dmap;
4609 uint8_t flags;
4611 dmap = cmd->cmd_sg;
4613 NDBG1(("mptsas_ieee_sge_mainframe: cookiec=%d, %s", cookiec,
4614 cmd->cmd_flags & CFLAG_DMASEND?"Out":"In"));
4616 ieeesge = (pMpi2IeeeSgeSimple64_t)(&frame->SGL);
4617 while (cookiec--) {
4618 ddi_put32(acc_hdl,
4619 &ieeesge->Address.Low, dmap->addr.address64.Low);
4620 ddi_put32(acc_hdl,
4621 &ieeesge->Address.High, dmap->addr.address64.High);
4622 ddi_put32(acc_hdl, &ieeesge->Length,
4623 dmap->count);
4624 NDBG1(("mptsas_ieee_sge_mainframe: len=%d", dmap->count));
4625 flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
4626 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4629 * If this is the last cookie, we set the flags
4630 * to indicate so
4632 if (cookiec == 0) {
4633 flags |= end_flag;
4636 ddi_put8(acc_hdl, &ieeesge->Flags, flags);
4637 dmap++;
4638 ieeesge++;
4642 static void
4643 mptsas_ieee_sge_chain(mptsas_t *mpt, mptsas_cmd_t *cmd,
4644 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4646 pMpi2IeeeSgeSimple64_t ieeesge;
4647 pMpi25IeeeSgeChain64_t ieeesgechain;
4648 uint64_t nframe_phys_addr;
4649 uint_t cookiec;
4650 mptti_t *dmap;
4651 uint8_t flags;
4654 * Save the number of entries in the DMA
4655 * Scatter/Gather list
4657 cookiec = cmd->cmd_cookiec;
4659 NDBG1(("mptsas_ieee_sge_chain: cookiec=%d", cookiec));
4662 * Hereby we start to deal with multiple frames.
4663 * The process is as follows:
4664 * 1. Determine how many frames are needed for SGL element
4665 * storage; Note that all frames are stored in contiguous
4666 * memory space and in 64-bit DMA mode each element is
4667 * 4 double-words (16 bytes) long.
4668 * 2. Fill up the main frame. We need to do this separately
4669 * since it contains the SCSI IO request header and needs
4670 * dedicated processing. Note that the last 4 double-words
4671 * of the SCSI IO header is for SGL element storage
4672 * (MPI2_SGE_IO_UNION).
4673 * 3. Fill the chain element in the main frame, so the DMA
4674 * engine can use the following frames.
4675 * 4. Enter a loop to fill the remaining frames. Note that the
4676 * last frame contains no chain element. The remaining
4677 * frames go into the mpt SGL buffer allocated on the fly,
4678 * not immediately following the main message frame, as in
4679 * Gen1.
4680 * Restrictions:
4681 * For 64-bit DMA, the simple element and chain element
4682 * are both of 4 double-words (16 bytes) in size, even
4683 * though all frames are stored in the first 4G of mem
4684 * range and the higher 32-bits of the address are always 0.
4686 int i, j, k, l, frames, sgemax;
4687 int temp;
4688 uint8_t chainflags;
4689 uint32_t chainlength;
4690 mptsas_cache_frames_t *p;
4693 * Sgemax is the number of SGE's that will fit
4694 * each extra frame and frames is total
4695 * number of frames we'll need. 1 sge entry per
4696 * frame is reseverd for the chain element thus the -1 below.
4698 sgemax = ((mpt->m_req_frame_size / sizeof (MPI2_IEEE_SGE_SIMPLE64))
4699 - 1);
4700 temp = (cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) / sgemax;
4703 * A little check to see if we need to round up the number
4704 * of frames we need
4706 if ((cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) - (temp *
4707 sgemax) > 1) {
4708 frames = (temp + 1);
4709 } else {
4710 frames = temp;
4712 NDBG1(("mptsas_ieee_sge_chain: temp=%d, frames=%d", temp, frames));
4713 dmap = cmd->cmd_sg;
4714 ieeesge = (pMpi2IeeeSgeSimple64_t)(&frame->SGL);
4717 * First fill in the main frame
4719 j = MPTSAS_MAX_FRAME_SGES64(mpt) - 1;
4720 mptsas_ieee_sge_mainframe(cmd, frame, acc_hdl, j, 0);
4721 dmap += j;
4722 ieeesge += j;
4723 j++;
4726 * Fill in the chain element in the main frame.
4727 * About calculation on ChainOffset:
4728 * 1. Struct msg_scsi_io_request has 4 double-words (16 bytes)
4729 * in the end reserved for SGL element storage
4730 * (MPI2_SGE_IO_UNION); we should count it in our
4731 * calculation. See its definition in the header file.
4732 * 2. Constant j is the counter of the current SGL element
4733 * that will be processed, and (j - 1) is the number of
4734 * SGL elements that have been processed (stored in the
4735 * main frame).
4736 * 3. ChainOffset value should be in units of quad-words (16
4737 * bytes) so the last value should be divided by 16.
4739 ddi_put8(acc_hdl, &frame->ChainOffset,
4740 (sizeof (MPI2_SCSI_IO_REQUEST) -
4741 sizeof (MPI2_SGE_IO_UNION) +
4742 (j - 1) * sizeof (MPI2_IEEE_SGE_SIMPLE64)) >> 4);
4743 ieeesgechain = (pMpi25IeeeSgeChain64_t)ieeesge;
4744 chainflags = (MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
4745 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4746 ddi_put8(acc_hdl, &ieeesgechain->Flags, chainflags);
4749 * The size of the next frame is the accurate size of space
4750 * (in bytes) used to store the SGL elements. j is the counter
4751 * of SGL elements. (j - 1) is the number of SGL elements that
4752 * have been processed (stored in frames).
4754 if (frames >= 2) {
4755 ASSERT(mpt->m_req_frame_size >=
4756 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4757 chainlength = mpt->m_req_frame_size /
4758 sizeof (MPI2_IEEE_SGE_SIMPLE64) *
4759 sizeof (MPI2_IEEE_SGE_SIMPLE64);
4760 } else {
4761 chainlength = ((cookiec - (j - 1)) *
4762 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4765 p = cmd->cmd_extra_frames;
4767 ddi_put32(acc_hdl, &ieeesgechain->Length, chainlength);
4768 ddi_put32(acc_hdl, &ieeesgechain->Address.Low, p->m_phys_addr);
4769 ddi_put32(acc_hdl, &ieeesgechain->Address.High, p->m_phys_addr >> 32);
4772 * If there are more than 2 frames left we have to
4773 * fill in the next chain offset to the location of
4774 * the chain element in the next frame.
4775 * sgemax is the number of simple elements in an extra
4776 * frame. Note that the value NextChainOffset should be
4777 * in double-words (4 bytes).
4779 if (frames >= 2) {
4780 ddi_put8(acc_hdl, &ieeesgechain->NextChainOffset,
4781 (sgemax * sizeof (MPI2_IEEE_SGE_SIMPLE64)) >> 4);
4782 } else {
4783 ddi_put8(acc_hdl, &ieeesgechain->NextChainOffset, 0);
4787 * Jump to next frame;
4788 * Starting here, chain buffers go into the per command SGL.
4789 * This buffer is allocated when chain buffers are needed.
4791 ieeesge = (pMpi2IeeeSgeSimple64_t)p->m_frames_addr;
4792 i = cookiec;
4795 * Start filling in frames with SGE's. If we
4796 * reach the end of frame and still have SGE's
4797 * to fill we need to add a chain element and
4798 * use another frame. j will be our counter
4799 * for what cookie we are at and i will be
4800 * the total cookiec. k is the current frame
4802 for (k = 1; k <= frames; k++) {
4803 for (l = 1; (l <= (sgemax + 1)) && (j <= i); j++, l++) {
4806 * If we have reached the end of frame
4807 * and we have more SGE's to fill in
4808 * we have to fill the final entry
4809 * with a chain element and then
4810 * continue to the next frame
4812 if ((l == (sgemax + 1)) && (k != frames)) {
4813 ieeesgechain = (pMpi25IeeeSgeChain64_t)ieeesge;
4814 j--;
4815 chainflags =
4816 MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
4817 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
4818 ddi_put8(p->m_acc_hdl,
4819 &ieeesgechain->Flags, chainflags);
4821 * k is the frame counter and (k + 1)
4822 * is the number of the next frame.
4823 * Note that frames are in contiguous
4824 * memory space.
4826 nframe_phys_addr = p->m_phys_addr +
4827 (mpt->m_req_frame_size * k);
4828 ddi_put32(p->m_acc_hdl,
4829 &ieeesgechain->Address.Low,
4830 nframe_phys_addr);
4831 ddi_put32(p->m_acc_hdl,
4832 &ieeesgechain->Address.High,
4833 nframe_phys_addr >> 32);
4836 * If there are more than 2 frames left
4837 * we have to next chain offset to
4838 * the location of the chain element
4839 * in the next frame and fill in the
4840 * length of the next chain
4842 if ((frames - k) >= 2) {
4843 ddi_put8(p->m_acc_hdl,
4844 &ieeesgechain->NextChainOffset,
4845 (sgemax *
4846 sizeof (MPI2_IEEE_SGE_SIMPLE64))
4847 >> 4);
4848 ASSERT(mpt->m_req_frame_size >=
4849 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4850 ddi_put32(p->m_acc_hdl,
4851 &ieeesgechain->Length,
4852 mpt->m_req_frame_size /
4853 sizeof (MPI2_IEEE_SGE_SIMPLE64) *
4854 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4855 } else {
4857 * This is the last frame. Set
4858 * the NextChainOffset to 0 and
4859 * Length is the total size of
4860 * all remaining simple elements
4862 ddi_put8(p->m_acc_hdl,
4863 &ieeesgechain->NextChainOffset,
4865 ddi_put32(p->m_acc_hdl,
4866 &ieeesgechain->Length,
4867 (cookiec - j) *
4868 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4871 /* Jump to the next frame */
4872 ieeesge = (pMpi2IeeeSgeSimple64_t)
4873 ((char *)p->m_frames_addr +
4874 (int)mpt->m_req_frame_size * k);
4876 continue;
4879 ddi_put32(p->m_acc_hdl,
4880 &ieeesge->Address.Low,
4881 dmap->addr.address64.Low);
4882 ddi_put32(p->m_acc_hdl,
4883 &ieeesge->Address.High,
4884 dmap->addr.address64.High);
4885 ddi_put32(p->m_acc_hdl,
4886 &ieeesge->Length, dmap->count);
4887 flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
4888 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4891 * If we are at the end of the frame and
4892 * there is another frame to fill in
4893 * do we need to do anything?
4894 * if ((l == sgemax) && (k != frames)) {
4899 * If this is the final cookie set end of list.
4901 if (j == i) {
4902 flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
4905 ddi_put8(p->m_acc_hdl, &ieeesge->Flags, flags);
4906 dmap++;
4907 ieeesge++;
4912 * Sync DMA with the chain buffers that were just created
4914 (void) ddi_dma_sync(p->m_dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
4917 static void
4918 mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd, uint32_t *control,
4919 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4921 ASSERT(cmd->cmd_flags & CFLAG_DMAVALID);
4923 NDBG1(("mptsas_sge_setup: cookiec=%d", cmd->cmd_cookiec));
4926 * Set read/write bit in control.
4928 if (cmd->cmd_flags & CFLAG_DMASEND) {
4929 *control |= MPI2_SCSIIO_CONTROL_WRITE;
4930 } else {
4931 *control |= MPI2_SCSIIO_CONTROL_READ;
4934 ddi_put32(acc_hdl, &frame->DataLength, cmd->cmd_dmacount);
4937 * We have 4 cases here. First where we can fit all the
4938 * SG elements into the main frame, and the case
4939 * where we can't. The SG element is also different when using
4940 * MPI2.5 interface.
4941 * If we have more cookies than we can attach to a frame
4942 * we will need to use a chain element to point
4943 * a location of memory where the rest of the S/G
4944 * elements reside.
4946 if (cmd->cmd_cookiec <= MPTSAS_MAX_FRAME_SGES64(mpt)) {
4947 if (mpt->m_MPI25) {
4948 mptsas_ieee_sge_mainframe(cmd, frame, acc_hdl,
4949 cmd->cmd_cookiec,
4950 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
4951 } else {
4952 mptsas_sge_mainframe(cmd, frame, acc_hdl,
4953 cmd->cmd_cookiec,
4954 ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT
4955 | MPI2_SGE_FLAGS_END_OF_BUFFER
4956 | MPI2_SGE_FLAGS_END_OF_LIST) <<
4957 MPI2_SGE_FLAGS_SHIFT));
4959 } else {
4960 if (mpt->m_MPI25) {
4961 mptsas_ieee_sge_chain(mpt, cmd, frame, acc_hdl);
4962 } else {
4963 mptsas_sge_chain(mpt, cmd, frame, acc_hdl);
4969 * Interrupt handling
4970 * Utility routine. Poll for status of a command sent to HBA
4971 * without interrupts (a FLAG_NOINTR command).
4974 mptsas_poll(mptsas_t *mpt, mptsas_cmd_t *poll_cmd, int polltime)
4976 int rval = TRUE;
4978 NDBG5(("mptsas_poll: cmd=0x%p", (void *)poll_cmd));
4980 if ((poll_cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
4981 mptsas_restart_hba(mpt);
4985 * Wait, using drv_usecwait(), long enough for the command to
4986 * reasonably return from the target if the target isn't
4987 * "dead". A polled command may well be sent from scsi_poll, and
4988 * there are retries built in to scsi_poll if the transport
4989 * accepted the packet (TRAN_ACCEPT). scsi_poll waits 1 second
4990 * and retries the transport up to scsi_poll_busycnt times
4991 * (currently 60) if
4992 * 1. pkt_reason is CMD_INCOMPLETE and pkt_state is 0, or
4993 * 2. pkt_reason is CMD_CMPLT and *pkt_scbp has STATUS_BUSY
4995 * limit the waiting to avoid a hang in the event that the
4996 * cmd never gets started but we are still receiving interrupts
4998 while (!(poll_cmd->cmd_flags & CFLAG_FINISHED)) {
4999 if (mptsas_wait_intr(mpt, polltime) == FALSE) {
5000 NDBG5(("mptsas_poll: command incomplete"));
5001 rval = FALSE;
5002 break;
5006 if (rval == FALSE) {
5009 * this isn't supposed to happen, the hba must be wedged
5010 * Mark this cmd as a timeout.
5012 mptsas_set_pkt_reason(mpt, poll_cmd, CMD_TIMEOUT,
5013 (STAT_TIMEOUT|STAT_ABORTED));
5015 if (poll_cmd->cmd_queued == FALSE) {
5017 NDBG5(("mptsas_poll: not on waitq"));
5019 poll_cmd->cmd_pkt->pkt_state |=
5020 (STATE_GOT_BUS|STATE_GOT_TARGET|STATE_SENT_CMD);
5021 } else {
5023 /* find and remove it from the waitq */
5024 NDBG5(("mptsas_poll: delete from waitq"));
5025 mptsas_waitq_delete(mpt, poll_cmd);
5029 mptsas_fma_check(mpt, poll_cmd);
5030 NDBG5(("mptsas_poll: done"));
5031 return (rval);
5035 * Used for polling cmds and TM function
5037 static int
5038 mptsas_wait_intr(mptsas_t *mpt, int polltime)
5040 int cnt;
5041 pMpi2ReplyDescriptorsUnion_t reply_desc_union;
5042 uint32_t int_mask;
5044 NDBG5(("mptsas_wait_intr"));
5046 mpt->m_polled_intr = 1;
5049 * Get the current interrupt mask and disable interrupts. When
5050 * re-enabling ints, set mask to saved value.
5052 int_mask = ddi_get32(mpt->m_datap, &mpt->m_reg->HostInterruptMask);
5053 MPTSAS_DISABLE_INTR(mpt);
5056 * Keep polling for at least (polltime * 1000) seconds
5058 for (cnt = 0; cnt < polltime; cnt++) {
5059 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5060 DDI_DMA_SYNC_FORCPU);
5062 reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
5063 MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
5065 if (ddi_get32(mpt->m_acc_post_queue_hdl,
5066 &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
5067 ddi_get32(mpt->m_acc_post_queue_hdl,
5068 &reply_desc_union->Words.High) == 0xFFFFFFFF) {
5069 drv_usecwait(1000);
5070 continue;
5074 * The reply is valid, process it according to its
5075 * type.
5077 mptsas_process_intr(mpt, reply_desc_union);
5079 if (++mpt->m_post_index == mpt->m_post_queue_depth) {
5080 mpt->m_post_index = 0;
5084 * Update the global reply index
5086 ddi_put32(mpt->m_datap,
5087 &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
5088 mpt->m_polled_intr = 0;
5091 * Re-enable interrupts and quit.
5093 ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask,
5094 int_mask);
5095 return (TRUE);
5100 * Clear polling flag, re-enable interrupts and quit.
5102 mpt->m_polled_intr = 0;
5103 ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask, int_mask);
5104 return (FALSE);
5107 static void
5108 mptsas_handle_scsi_io_success(mptsas_t *mpt,
5109 pMpi2ReplyDescriptorsUnion_t reply_desc)
5111 pMpi2SCSIIOSuccessReplyDescriptor_t scsi_io_success;
5112 uint16_t SMID;
5113 mptsas_slots_t *slots = mpt->m_active;
5114 mptsas_cmd_t *cmd = NULL;
5115 struct scsi_pkt *pkt;
5117 ASSERT(mutex_owned(&mpt->m_mutex));
5119 scsi_io_success = (pMpi2SCSIIOSuccessReplyDescriptor_t)reply_desc;
5120 SMID = ddi_get16(mpt->m_acc_post_queue_hdl, &scsi_io_success->SMID);
5123 * This is a success reply so just complete the IO. First, do a sanity
5124 * check on the SMID. The final slot is used for TM requests, which
5125 * would not come into this reply handler.
5127 if ((SMID == 0) || (SMID > slots->m_n_normal)) {
5128 mptsas_log(mpt, CE_WARN, "?Received invalid SMID of %d\n",
5129 SMID);
5130 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5131 return;
5134 cmd = slots->m_slot[SMID];
5137 * print warning and return if the slot is empty
5139 if (cmd == NULL) {
5140 mptsas_log(mpt, CE_WARN, "?NULL command for successful SCSI IO "
5141 "in slot %d", SMID);
5142 return;
5145 pkt = CMD2PKT(cmd);
5146 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD |
5147 STATE_GOT_STATUS);
5148 if (cmd->cmd_flags & CFLAG_DMAVALID) {
5149 pkt->pkt_state |= STATE_XFERRED_DATA;
5151 pkt->pkt_resid = 0;
5153 if (cmd->cmd_flags & CFLAG_PASSTHRU) {
5154 cmd->cmd_flags |= CFLAG_FINISHED;
5155 cv_broadcast(&mpt->m_passthru_cv);
5156 return;
5157 } else {
5158 mptsas_remove_cmd(mpt, cmd);
5161 if (cmd->cmd_flags & CFLAG_RETRY) {
5163 * The target returned QFULL or busy, do not add tihs
5164 * pkt to the doneq since the hba will retry
5165 * this cmd.
5167 * The pkt has already been resubmitted in
5168 * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
5169 * Remove this cmd_flag here.
5171 cmd->cmd_flags &= ~CFLAG_RETRY;
5172 } else {
5173 mptsas_doneq_add(mpt, cmd);
5177 static void
5178 mptsas_handle_address_reply(mptsas_t *mpt,
5179 pMpi2ReplyDescriptorsUnion_t reply_desc)
5181 pMpi2AddressReplyDescriptor_t address_reply;
5182 pMPI2DefaultReply_t reply;
5183 mptsas_fw_diagnostic_buffer_t *pBuffer;
5184 uint32_t reply_addr, reply_frame_dma_baseaddr;
5185 uint16_t SMID, iocstatus;
5186 mptsas_slots_t *slots = mpt->m_active;
5187 mptsas_cmd_t *cmd = NULL;
5188 uint8_t function, buffer_type;
5189 m_replyh_arg_t *args;
5190 int reply_frame_no;
5192 ASSERT(mutex_owned(&mpt->m_mutex));
5194 address_reply = (pMpi2AddressReplyDescriptor_t)reply_desc;
5195 reply_addr = ddi_get32(mpt->m_acc_post_queue_hdl,
5196 &address_reply->ReplyFrameAddress);
5197 SMID = ddi_get16(mpt->m_acc_post_queue_hdl, &address_reply->SMID);
5200 * If reply frame is not in the proper range we should ignore this
5201 * message and exit the interrupt handler.
5203 reply_frame_dma_baseaddr = mpt->m_reply_frame_dma_addr & 0xffffffffu;
5204 if ((reply_addr < reply_frame_dma_baseaddr) ||
5205 (reply_addr >= (reply_frame_dma_baseaddr +
5206 (mpt->m_reply_frame_size * mpt->m_max_replies))) ||
5207 ((reply_addr - reply_frame_dma_baseaddr) %
5208 mpt->m_reply_frame_size != 0)) {
5209 mptsas_log(mpt, CE_WARN, "?Received invalid reply frame "
5210 "address 0x%x\n", reply_addr);
5211 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5212 return;
5215 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
5216 DDI_DMA_SYNC_FORCPU);
5217 reply = (pMPI2DefaultReply_t)(mpt->m_reply_frame + (reply_addr -
5218 reply_frame_dma_baseaddr));
5219 function = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->Function);
5221 NDBG31(("mptsas_handle_address_reply: function 0x%x, reply_addr=0x%x",
5222 function, reply_addr));
5225 * don't get slot information and command for events since these values
5226 * don't exist
5228 if ((function != MPI2_FUNCTION_EVENT_NOTIFICATION) &&
5229 (function != MPI2_FUNCTION_DIAG_BUFFER_POST)) {
5231 * This could be a TM reply, which use the last allocated SMID,
5232 * so allow for that.
5234 if ((SMID == 0) || (SMID > (slots->m_n_normal + 1))) {
5235 mptsas_log(mpt, CE_WARN, "?Received invalid SMID of "
5236 "%d\n", SMID);
5237 ddi_fm_service_impact(mpt->m_dip,
5238 DDI_SERVICE_UNAFFECTED);
5239 return;
5242 cmd = slots->m_slot[SMID];
5245 * print warning and return if the slot is empty
5247 if (cmd == NULL) {
5248 mptsas_log(mpt, CE_WARN, "?NULL command for address "
5249 "reply in slot %d", SMID);
5250 return;
5252 if ((cmd->cmd_flags &
5253 (CFLAG_PASSTHRU | CFLAG_CONFIG | CFLAG_FW_DIAG))) {
5254 cmd->cmd_rfm = reply_addr;
5255 cmd->cmd_flags |= CFLAG_FINISHED;
5256 cv_broadcast(&mpt->m_passthru_cv);
5257 cv_broadcast(&mpt->m_config_cv);
5258 cv_broadcast(&mpt->m_fw_diag_cv);
5259 return;
5260 } else if (!(cmd->cmd_flags & CFLAG_FW_CMD)) {
5261 mptsas_remove_cmd(mpt, cmd);
5263 NDBG31(("\t\tmptsas_process_intr: slot=%d", SMID));
5266 * Depending on the function, we need to handle
5267 * the reply frame (and cmd) differently.
5269 switch (function) {
5270 case MPI2_FUNCTION_SCSI_IO_REQUEST:
5271 mptsas_check_scsi_io_error(mpt, (pMpi2SCSIIOReply_t)reply, cmd);
5272 break;
5273 case MPI2_FUNCTION_SCSI_TASK_MGMT:
5274 cmd->cmd_rfm = reply_addr;
5275 mptsas_check_task_mgt(mpt, (pMpi2SCSIManagementReply_t)reply,
5276 cmd);
5277 break;
5278 case MPI2_FUNCTION_FW_DOWNLOAD:
5279 cmd->cmd_flags |= CFLAG_FINISHED;
5280 cv_signal(&mpt->m_fw_cv);
5281 break;
5282 case MPI2_FUNCTION_EVENT_NOTIFICATION:
5283 reply_frame_no = (reply_addr - reply_frame_dma_baseaddr) /
5284 mpt->m_reply_frame_size;
5285 args = &mpt->m_replyh_args[reply_frame_no];
5286 args->mpt = (void *)mpt;
5287 args->rfm = reply_addr;
5290 * Record the event if its type is enabled in
5291 * this mpt instance by ioctl.
5293 mptsas_record_event(args);
5296 * Handle time critical events
5297 * NOT_RESPONDING/ADDED only now
5299 if (mptsas_handle_event_sync(args) == DDI_SUCCESS) {
5301 * Would not return main process,
5302 * just let taskq resolve ack action
5303 * and ack would be sent in taskq thread
5305 NDBG20(("send mptsas_handle_event_sync success"));
5308 if (mpt->m_in_reset) {
5309 NDBG20(("dropping event received during reset"));
5310 return;
5313 if ((ddi_taskq_dispatch(mpt->m_event_taskq, mptsas_handle_event,
5314 (void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
5315 mptsas_log(mpt, CE_WARN, "No memory available"
5316 "for dispatch taskq");
5318 * Return the reply frame to the free queue.
5320 ddi_put32(mpt->m_acc_free_queue_hdl,
5321 &((uint32_t *)(void *)
5322 mpt->m_free_queue)[mpt->m_free_index], reply_addr);
5323 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
5324 DDI_DMA_SYNC_FORDEV);
5325 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
5326 mpt->m_free_index = 0;
5329 ddi_put32(mpt->m_datap,
5330 &mpt->m_reg->ReplyFreeHostIndex, mpt->m_free_index);
5332 return;
5333 case MPI2_FUNCTION_DIAG_BUFFER_POST:
5335 * If SMID is 0, this implies that the reply is due to a
5336 * release function with a status that the buffer has been
5337 * released. Set the buffer flags accordingly.
5339 if (SMID == 0) {
5340 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
5341 &reply->IOCStatus);
5342 buffer_type = ddi_get8(mpt->m_acc_reply_frame_hdl,
5343 &(((pMpi2DiagBufferPostReply_t)reply)->BufferType));
5344 if (iocstatus == MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED) {
5345 pBuffer =
5346 &mpt->m_fw_diag_buffer_list[buffer_type];
5347 pBuffer->valid_data = TRUE;
5348 pBuffer->owned_by_firmware = FALSE;
5349 pBuffer->immediate = FALSE;
5351 } else {
5353 * Normal handling of diag post reply with SMID.
5355 cmd = slots->m_slot[SMID];
5358 * print warning and return if the slot is empty
5360 if (cmd == NULL) {
5361 mptsas_log(mpt, CE_WARN, "?NULL command for "
5362 "address reply in slot %d", SMID);
5363 return;
5365 cmd->cmd_rfm = reply_addr;
5366 cmd->cmd_flags |= CFLAG_FINISHED;
5367 cv_broadcast(&mpt->m_fw_diag_cv);
5369 return;
5370 default:
5371 mptsas_log(mpt, CE_WARN, "Unknown function 0x%x ", function);
5372 break;
5376 * Return the reply frame to the free queue.
5378 ddi_put32(mpt->m_acc_free_queue_hdl,
5379 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
5380 reply_addr);
5381 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
5382 DDI_DMA_SYNC_FORDEV);
5383 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
5384 mpt->m_free_index = 0;
5386 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
5387 mpt->m_free_index);
5389 if (cmd->cmd_flags & CFLAG_FW_CMD)
5390 return;
5392 if (cmd->cmd_flags & CFLAG_RETRY) {
5394 * The target returned QFULL or busy, do not add this
5395 * pkt to the doneq since the hba will retry
5396 * this cmd.
5398 * The pkt has already been resubmitted in
5399 * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
5400 * Remove this cmd_flag here.
5402 cmd->cmd_flags &= ~CFLAG_RETRY;
5403 } else {
5404 mptsas_doneq_add(mpt, cmd);
5408 #ifdef MPTSAS_DEBUG
5409 static uint8_t mptsas_last_sense[256];
5410 #endif
5412 static void
5413 mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
5414 mptsas_cmd_t *cmd)
5416 uint8_t scsi_status, scsi_state;
5417 uint16_t ioc_status, cmd_rqs_len;
5418 uint32_t xferred, sensecount, responsedata, loginfo = 0;
5419 struct scsi_pkt *pkt;
5420 struct scsi_arq_status *arqstat;
5421 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
5422 uint8_t *sensedata = NULL;
5423 uint64_t sas_wwn;
5424 uint8_t phy;
5425 char wwn_str[MPTSAS_WWN_STRLEN];
5427 scsi_status = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIStatus);
5428 ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5429 scsi_state = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIState);
5430 xferred = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->TransferCount);
5431 sensecount = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->SenseCount);
5432 responsedata = ddi_get32(mpt->m_acc_reply_frame_hdl,
5433 &reply->ResponseInfo);
5435 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
5436 sas_wwn = ptgt->m_addr.mta_wwn;
5437 phy = ptgt->m_phynum;
5438 if (sas_wwn == 0) {
5439 (void) sprintf(wwn_str, "p%x", phy);
5440 } else {
5441 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
5443 loginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
5444 &reply->IOCLogInfo);
5445 mptsas_log(mpt, CE_NOTE,
5446 "?Log info 0x%x received for target %d %s.\n"
5447 "\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5448 loginfo, Tgt(cmd), wwn_str, scsi_status, ioc_status,
5449 scsi_state);
5452 NDBG31(("\t\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5453 scsi_status, ioc_status, scsi_state));
5455 pkt = CMD2PKT(cmd);
5456 *(pkt->pkt_scbp) = scsi_status;
5458 if (loginfo == 0x31170000) {
5460 * if loginfo PL_LOGINFO_CODE_IO_DEVICE_MISSING_DELAY_RETRY
5461 * 0x31170000 comes, that means the device missing delay
5462 * is in progressing, the command need retry later.
5464 *(pkt->pkt_scbp) = STATUS_BUSY;
5465 return;
5468 if ((scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS) &&
5469 ((ioc_status & MPI2_IOCSTATUS_MASK) ==
5470 MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE)) {
5471 pkt->pkt_reason = CMD_INCOMPLETE;
5472 pkt->pkt_state |= STATE_GOT_BUS;
5473 if (ptgt->m_reset_delay == 0) {
5474 mptsas_set_throttle(mpt, ptgt,
5475 DRAIN_THROTTLE);
5477 return;
5480 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
5481 responsedata &= 0x000000FF;
5482 if (responsedata & MPTSAS_SCSI_RESPONSE_CODE_TLR_OFF) {
5483 mptsas_log(mpt, CE_NOTE, "Do not support the TLR\n");
5484 pkt->pkt_reason = CMD_TLR_OFF;
5485 return;
5490 switch (scsi_status) {
5491 case MPI2_SCSI_STATUS_CHECK_CONDITION:
5492 pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5493 arqstat = (void*)(pkt->pkt_scbp);
5494 arqstat->sts_rqpkt_status = *((struct scsi_status *)
5495 (pkt->pkt_scbp));
5496 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET |
5497 STATE_SENT_CMD | STATE_GOT_STATUS | STATE_ARQ_DONE);
5498 if (cmd->cmd_flags & CFLAG_XARQ) {
5499 pkt->pkt_state |= STATE_XARQ_DONE;
5501 if (pkt->pkt_resid != cmd->cmd_dmacount) {
5502 pkt->pkt_state |= STATE_XFERRED_DATA;
5504 arqstat->sts_rqpkt_reason = pkt->pkt_reason;
5505 arqstat->sts_rqpkt_state = pkt->pkt_state;
5506 arqstat->sts_rqpkt_state |= STATE_XFERRED_DATA;
5507 arqstat->sts_rqpkt_statistics = pkt->pkt_statistics;
5508 sensedata = (uint8_t *)&arqstat->sts_sensedata;
5509 cmd_rqs_len = cmd->cmd_extrqslen ?
5510 cmd->cmd_extrqslen : cmd->cmd_rqslen;
5511 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
5512 DDI_DMA_SYNC_FORKERNEL);
5513 #ifdef MPTSAS_DEBUG
5514 bcopy(cmd->cmd_arq_buf, mptsas_last_sense,
5515 ((cmd_rqs_len >= sizeof (mptsas_last_sense)) ?
5516 sizeof (mptsas_last_sense):cmd_rqs_len));
5517 #endif
5518 bcopy((uchar_t *)cmd->cmd_arq_buf, sensedata,
5519 ((cmd_rqs_len >= sensecount) ? sensecount :
5520 cmd_rqs_len));
5521 arqstat->sts_rqpkt_resid = (cmd_rqs_len - sensecount);
5522 cmd->cmd_flags |= CFLAG_CMDARQ;
5524 * Set proper status for pkt if autosense was valid
5526 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
5527 struct scsi_status zero_status = { 0 };
5528 arqstat->sts_rqpkt_status = zero_status;
5532 * ASC=0x47 is parity error
5533 * ASC=0x48 is initiator detected error received
5535 if ((scsi_sense_key(sensedata) == KEY_ABORTED_COMMAND) &&
5536 ((scsi_sense_asc(sensedata) == 0x47) ||
5537 (scsi_sense_asc(sensedata) == 0x48))) {
5538 mptsas_log(mpt, CE_NOTE, "Aborted_command!");
5542 * ASC/ASCQ=0x3F/0x0E means report_luns data changed
5543 * ASC/ASCQ=0x25/0x00 means invalid lun
5545 if (((scsi_sense_key(sensedata) == KEY_UNIT_ATTENTION) &&
5546 (scsi_sense_asc(sensedata) == 0x3F) &&
5547 (scsi_sense_ascq(sensedata) == 0x0E)) ||
5548 ((scsi_sense_key(sensedata) == KEY_ILLEGAL_REQUEST) &&
5549 (scsi_sense_asc(sensedata) == 0x25) &&
5550 (scsi_sense_ascq(sensedata) == 0x00))) {
5551 mptsas_topo_change_list_t *topo_node = NULL;
5553 topo_node = kmem_zalloc(
5554 sizeof (mptsas_topo_change_list_t),
5555 KM_NOSLEEP);
5556 if (topo_node == NULL) {
5557 mptsas_log(mpt, CE_NOTE, "No memory"
5558 "resource for handle SAS dynamic"
5559 "reconfigure.\n");
5560 break;
5562 topo_node->mpt = mpt;
5563 topo_node->event = MPTSAS_DR_EVENT_RECONFIG_TARGET;
5564 topo_node->un.phymask = ptgt->m_addr.mta_phymask;
5565 topo_node->devhdl = ptgt->m_devhdl;
5566 topo_node->object = (void *)ptgt;
5567 topo_node->flags = MPTSAS_TOPO_FLAG_LUN_ASSOCIATED;
5569 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
5570 mptsas_handle_dr,
5571 (void *)topo_node,
5572 DDI_NOSLEEP)) != DDI_SUCCESS) {
5573 kmem_free(topo_node,
5574 sizeof (mptsas_topo_change_list_t));
5575 mptsas_log(mpt, CE_NOTE, "mptsas start taskq"
5576 "for handle SAS dynamic reconfigure"
5577 "failed. \n");
5580 break;
5581 case MPI2_SCSI_STATUS_GOOD:
5582 switch (ioc_status & MPI2_IOCSTATUS_MASK) {
5583 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
5584 pkt->pkt_reason = CMD_DEV_GONE;
5585 pkt->pkt_state |= STATE_GOT_BUS;
5586 if (ptgt->m_reset_delay == 0) {
5587 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5589 NDBG31(("lost disk for target%d, command:%x",
5590 Tgt(cmd), pkt->pkt_cdbp[0]));
5591 break;
5592 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
5593 NDBG31(("data overrun: xferred=%d", xferred));
5594 NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5595 pkt->pkt_reason = CMD_DATA_OVR;
5596 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5597 | STATE_SENT_CMD | STATE_GOT_STATUS
5598 | STATE_XFERRED_DATA);
5599 pkt->pkt_resid = 0;
5600 break;
5601 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
5602 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
5603 NDBG31(("data underrun: xferred=%d", xferred));
5604 NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5605 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5606 | STATE_SENT_CMD | STATE_GOT_STATUS);
5607 pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5608 if (pkt->pkt_resid != cmd->cmd_dmacount) {
5609 pkt->pkt_state |= STATE_XFERRED_DATA;
5611 break;
5612 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
5613 if (cmd->cmd_active_expiration <= gethrtime()) {
5615 * When timeout requested, propagate
5616 * proper reason and statistics to
5617 * target drivers.
5619 mptsas_set_pkt_reason(mpt, cmd, CMD_TIMEOUT,
5620 STAT_BUS_RESET | STAT_TIMEOUT);
5621 } else {
5622 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
5623 STAT_BUS_RESET);
5625 break;
5626 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
5627 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
5628 mptsas_set_pkt_reason(mpt,
5629 cmd, CMD_RESET, STAT_DEV_RESET);
5630 break;
5631 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
5632 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
5633 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET);
5634 mptsas_set_pkt_reason(mpt,
5635 cmd, CMD_TERMINATED, STAT_TERMINATED);
5636 break;
5637 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
5638 case MPI2_IOCSTATUS_BUSY:
5640 * set throttles to drain
5642 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
5643 ptgt = refhash_next(mpt->m_targets, ptgt)) {
5644 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5648 * retry command
5650 cmd->cmd_flags |= CFLAG_RETRY;
5651 cmd->cmd_pkt_flags |= FLAG_HEAD;
5653 (void) mptsas_accept_pkt(mpt, cmd);
5654 break;
5655 default:
5656 mptsas_log(mpt, CE_WARN,
5657 "unknown ioc_status = %x\n", ioc_status);
5658 mptsas_log(mpt, CE_CONT, "scsi_state = %x, transfer "
5659 "count = %x, scsi_status = %x", scsi_state,
5660 xferred, scsi_status);
5661 break;
5663 break;
5664 case MPI2_SCSI_STATUS_TASK_SET_FULL:
5665 mptsas_handle_qfull(mpt, cmd);
5666 break;
5667 case MPI2_SCSI_STATUS_BUSY:
5668 NDBG31(("scsi_status busy received"));
5669 break;
5670 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
5671 NDBG31(("scsi_status reservation conflict received"));
5672 break;
5673 default:
5674 mptsas_log(mpt, CE_WARN, "scsi_status=%x, ioc_status=%x\n",
5675 scsi_status, ioc_status);
5676 mptsas_log(mpt, CE_WARN,
5677 "mptsas_process_intr: invalid scsi status\n");
5678 break;
5682 static void
5683 mptsas_check_task_mgt(mptsas_t *mpt, pMpi2SCSIManagementReply_t reply,
5684 mptsas_cmd_t *cmd)
5686 uint8_t task_type;
5687 uint16_t ioc_status;
5688 uint32_t log_info;
5689 uint16_t dev_handle;
5690 struct scsi_pkt *pkt = CMD2PKT(cmd);
5692 task_type = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->TaskType);
5693 ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5694 log_info = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->IOCLogInfo);
5695 dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->DevHandle);
5697 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5698 mptsas_log(mpt, CE_WARN, "mptsas_check_task_mgt: Task 0x%x "
5699 "failed. IOCStatus=0x%x IOCLogInfo=0x%x target=%d\n",
5700 task_type, ioc_status, log_info, dev_handle);
5701 pkt->pkt_reason = CMD_INCOMPLETE;
5702 return;
5705 switch (task_type) {
5706 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
5707 case MPI2_SCSITASKMGMT_TASKTYPE_CLEAR_TASK_SET:
5708 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
5709 case MPI2_SCSITASKMGMT_TASKTYPE_CLR_ACA:
5710 case MPI2_SCSITASKMGMT_TASKTYPE_QRY_TASK_SET:
5711 case MPI2_SCSITASKMGMT_TASKTYPE_QRY_UNIT_ATTENTION:
5712 break;
5713 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
5714 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
5715 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
5717 * Check for invalid DevHandle of 0 in case application
5718 * sends bad command. DevHandle of 0 could cause problems.
5720 if (dev_handle == 0) {
5721 mptsas_log(mpt, CE_WARN, "!Can't flush target with"
5722 " DevHandle of 0.");
5723 } else {
5724 mptsas_flush_target(mpt, dev_handle, Lun(cmd),
5725 task_type);
5727 break;
5728 default:
5729 mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
5730 task_type);
5731 mptsas_log(mpt, CE_WARN, "ioc status = %x", ioc_status);
5732 break;
5736 static void
5737 mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg)
5739 mptsas_t *mpt = arg->mpt;
5740 uint64_t t = arg->t;
5741 mptsas_cmd_t *cmd;
5742 struct scsi_pkt *pkt;
5743 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
5745 mutex_enter(&item->mutex);
5746 while (item->flag & MPTSAS_DONEQ_THREAD_ACTIVE) {
5747 if (!item->doneq) {
5748 cv_wait(&item->cv, &item->mutex);
5750 pkt = NULL;
5751 if ((cmd = mptsas_doneq_thread_rm(mpt, t)) != NULL) {
5752 cmd->cmd_flags |= CFLAG_COMPLETED;
5753 pkt = CMD2PKT(cmd);
5755 mutex_exit(&item->mutex);
5756 if (pkt) {
5757 mptsas_pkt_comp(pkt, cmd);
5759 mutex_enter(&item->mutex);
5761 mutex_exit(&item->mutex);
5762 mutex_enter(&mpt->m_doneq_mutex);
5763 mpt->m_doneq_thread_n--;
5764 cv_broadcast(&mpt->m_doneq_thread_cv);
5765 mutex_exit(&mpt->m_doneq_mutex);
5770 * mpt interrupt handler.
5772 static uint_t
5773 mptsas_intr(caddr_t arg1, caddr_t arg2)
5775 mptsas_t *mpt = (void *)arg1;
5776 pMpi2ReplyDescriptorsUnion_t reply_desc_union;
5777 uchar_t did_reply = FALSE;
5779 NDBG1(("mptsas_intr: arg1 0x%p arg2 0x%p", (void *)arg1, (void *)arg2));
5781 mutex_enter(&mpt->m_mutex);
5784 * If interrupts are shared by two channels then check whether this
5785 * interrupt is genuinely for this channel by making sure first the
5786 * chip is in high power state.
5788 if ((mpt->m_options & MPTSAS_OPT_PM) &&
5789 (mpt->m_power_level != PM_LEVEL_D0)) {
5790 mutex_exit(&mpt->m_mutex);
5791 return (DDI_INTR_UNCLAIMED);
5795 * If polling, interrupt was triggered by some shared interrupt because
5796 * IOC interrupts are disabled during polling, so polling routine will
5797 * handle any replies. Considering this, if polling is happening,
5798 * return with interrupt unclaimed.
5800 if (mpt->m_polled_intr) {
5801 mutex_exit(&mpt->m_mutex);
5802 mptsas_log(mpt, CE_WARN, "mpt_sas: Unclaimed interrupt");
5803 return (DDI_INTR_UNCLAIMED);
5807 * Read the istat register.
5809 if ((INTPENDING(mpt)) != 0) {
5811 * read fifo until empty.
5813 #ifndef __lock_lint
5814 _NOTE(CONSTCOND)
5815 #endif
5816 while (TRUE) {
5817 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5818 DDI_DMA_SYNC_FORCPU);
5819 reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
5820 MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
5822 if (ddi_get32(mpt->m_acc_post_queue_hdl,
5823 &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
5824 ddi_get32(mpt->m_acc_post_queue_hdl,
5825 &reply_desc_union->Words.High) == 0xFFFFFFFF) {
5826 break;
5830 * The reply is valid, process it according to its
5831 * type. Also, set a flag for updating the reply index
5832 * after they've all been processed.
5834 did_reply = TRUE;
5836 mptsas_process_intr(mpt, reply_desc_union);
5839 * Increment post index and roll over if needed.
5841 if (++mpt->m_post_index == mpt->m_post_queue_depth) {
5842 mpt->m_post_index = 0;
5847 * Update the global reply index if at least one reply was
5848 * processed.
5850 if (did_reply) {
5851 ddi_put32(mpt->m_datap,
5852 &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
5854 } else {
5855 mutex_exit(&mpt->m_mutex);
5856 return (DDI_INTR_UNCLAIMED);
5858 NDBG1(("mptsas_intr complete"));
5861 * If no helper threads are created, process the doneq in ISR. If
5862 * helpers are created, use the doneq length as a metric to measure the
5863 * load on the interrupt CPU. If it is long enough, which indicates the
5864 * load is heavy, then we deliver the IO completions to the helpers.
5865 * This measurement has some limitations, although it is simple and
5866 * straightforward and works well for most of the cases at present.
5868 if (!mpt->m_doneq_thread_n ||
5869 (mpt->m_doneq_len <= mpt->m_doneq_length_threshold)) {
5870 mptsas_doneq_empty(mpt);
5871 } else {
5872 mptsas_deliver_doneq_thread(mpt);
5876 * If there are queued cmd, start them now.
5878 if (mpt->m_waitq != NULL) {
5879 mptsas_restart_waitq(mpt);
5882 mutex_exit(&mpt->m_mutex);
5883 return (DDI_INTR_CLAIMED);
5886 static void
5887 mptsas_process_intr(mptsas_t *mpt,
5888 pMpi2ReplyDescriptorsUnion_t reply_desc_union)
5890 uint8_t reply_type;
5892 ASSERT(mutex_owned(&mpt->m_mutex));
5895 * The reply is valid, process it according to its
5896 * type. Also, set a flag for updated the reply index
5897 * after they've all been processed.
5899 reply_type = ddi_get8(mpt->m_acc_post_queue_hdl,
5900 &reply_desc_union->Default.ReplyFlags);
5901 reply_type &= MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
5902 if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
5903 reply_type == MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS) {
5904 mptsas_handle_scsi_io_success(mpt, reply_desc_union);
5905 } else if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
5906 mptsas_handle_address_reply(mpt, reply_desc_union);
5907 } else {
5908 mptsas_log(mpt, CE_WARN, "?Bad reply type %x", reply_type);
5909 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5913 * Clear the reply descriptor for re-use and increment
5914 * index.
5916 ddi_put64(mpt->m_acc_post_queue_hdl,
5917 &((uint64_t *)(void *)mpt->m_post_queue)[mpt->m_post_index],
5918 0xFFFFFFFFFFFFFFFF);
5919 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5920 DDI_DMA_SYNC_FORDEV);
5924 * handle qfull condition
5926 static void
5927 mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd)
5929 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
5931 if ((++cmd->cmd_qfull_retries > ptgt->m_qfull_retries) ||
5932 (ptgt->m_qfull_retries == 0)) {
5934 * We have exhausted the retries on QFULL, or,
5935 * the target driver has indicated that it
5936 * wants to handle QFULL itself by setting
5937 * qfull-retries capability to 0. In either case
5938 * we want the target driver's QFULL handling
5939 * to kick in. We do this by having pkt_reason
5940 * as CMD_CMPLT and pkt_scbp as STATUS_QFULL.
5942 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5943 } else {
5944 if (ptgt->m_reset_delay == 0) {
5945 ptgt->m_t_throttle =
5946 max((ptgt->m_t_ncmds - 2), 0);
5949 cmd->cmd_pkt_flags |= FLAG_HEAD;
5950 cmd->cmd_flags &= ~(CFLAG_TRANFLAG);
5951 cmd->cmd_flags |= CFLAG_RETRY;
5953 (void) mptsas_accept_pkt(mpt, cmd);
5956 * when target gives queue full status with no commands
5957 * outstanding (m_t_ncmds == 0), throttle is set to 0
5958 * (HOLD_THROTTLE), and the queue full handling start
5959 * (see psarc/1994/313); if there are commands outstanding,
5960 * throttle is set to (m_t_ncmds - 2)
5962 if (ptgt->m_t_throttle == HOLD_THROTTLE) {
5964 * By setting throttle to QFULL_THROTTLE, we
5965 * avoid submitting new commands and in
5966 * mptsas_restart_cmd find out slots which need
5967 * their throttles to be cleared.
5969 mptsas_set_throttle(mpt, ptgt, QFULL_THROTTLE);
5970 if (mpt->m_restart_cmd_timeid == 0) {
5971 mpt->m_restart_cmd_timeid =
5972 timeout(mptsas_restart_cmd, mpt,
5973 ptgt->m_qfull_retry_interval);
5979 mptsas_phymask_t
5980 mptsas_physport_to_phymask(mptsas_t *mpt, uint8_t physport)
5982 mptsas_phymask_t phy_mask = 0;
5983 uint8_t i = 0;
5985 NDBG20(("mptsas%d physport_to_phymask enter", mpt->m_instance));
5987 ASSERT(mutex_owned(&mpt->m_mutex));
5990 * If physport is 0xFF, this is a RAID volume. Use phymask of 0.
5992 if (physport == 0xFF) {
5993 return (0);
5996 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
5997 if (mpt->m_phy_info[i].attached_devhdl &&
5998 (mpt->m_phy_info[i].phy_mask != 0) &&
5999 (mpt->m_phy_info[i].port_num == physport)) {
6000 phy_mask = mpt->m_phy_info[i].phy_mask;
6001 break;
6004 NDBG20(("mptsas%d physport_to_phymask:physport :%x phymask :%x, ",
6005 mpt->m_instance, physport, phy_mask));
6006 return (phy_mask);
6010 * mpt free device handle after device gone, by use of passthrough
6012 static int
6013 mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl)
6015 Mpi2SasIoUnitControlRequest_t req;
6016 Mpi2SasIoUnitControlReply_t rep;
6017 int ret;
6019 ASSERT(mutex_owned(&mpt->m_mutex));
6022 * Need to compose a SAS IO Unit Control request message
6023 * and call mptsas_do_passthru() function
6025 bzero(&req, sizeof (req));
6026 bzero(&rep, sizeof (rep));
6028 req.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
6029 req.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
6030 req.DevHandle = LE_16(devhdl);
6032 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
6033 sizeof (req), sizeof (rep), NULL, 0, NULL, 0, 60, FKIOCTL);
6034 if (ret != 0) {
6035 cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
6036 "Control error %d", ret);
6037 return (DDI_FAILURE);
6040 /* do passthrough success, check the ioc status */
6041 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
6042 cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
6043 "Control IOCStatus %d", LE_16(rep.IOCStatus));
6044 return (DDI_FAILURE);
6047 return (DDI_SUCCESS);
6050 static void
6051 mptsas_update_phymask(mptsas_t *mpt)
6053 mptsas_phymask_t mask = 0, phy_mask;
6054 char *phy_mask_name;
6055 uint8_t current_port;
6056 int i, j;
6058 NDBG20(("mptsas%d update phymask ", mpt->m_instance));
6060 ASSERT(mutex_owned(&mpt->m_mutex));
6062 (void) mptsas_get_sas_io_unit_page(mpt);
6064 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6066 for (i = 0; i < mpt->m_num_phys; i++) {
6067 phy_mask = 0x00;
6069 if (mpt->m_phy_info[i].attached_devhdl == 0)
6070 continue;
6072 bzero(phy_mask_name, sizeof (phy_mask_name));
6074 current_port = mpt->m_phy_info[i].port_num;
6076 if ((mask & (1 << i)) != 0)
6077 continue;
6079 for (j = 0; j < mpt->m_num_phys; j++) {
6080 if (mpt->m_phy_info[j].attached_devhdl &&
6081 (mpt->m_phy_info[j].port_num == current_port)) {
6082 phy_mask |= (1 << j);
6085 mask = mask | phy_mask;
6087 for (j = 0; j < mpt->m_num_phys; j++) {
6088 if ((phy_mask >> j) & 0x01) {
6089 mpt->m_phy_info[j].phy_mask = phy_mask;
6093 (void) sprintf(phy_mask_name, "%x", phy_mask);
6095 mutex_exit(&mpt->m_mutex);
6097 * register a iport, if the port has already been existed
6098 * SCSA will do nothing and just return.
6100 (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
6101 mutex_enter(&mpt->m_mutex);
6103 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6104 NDBG20(("mptsas%d update phymask return", mpt->m_instance));
6108 * mptsas_handle_dr is a task handler for DR, the DR action includes:
6109 * 1. Directly attched Device Added/Removed.
6110 * 2. Expander Device Added/Removed.
6111 * 3. Indirectly Attached Device Added/Expander.
6112 * 4. LUNs of a existing device status change.
6113 * 5. RAID volume created/deleted.
6114 * 6. Member of RAID volume is released because of RAID deletion.
6115 * 7. Physical disks are removed because of RAID creation.
6117 static void
6118 mptsas_handle_dr(void *args) {
6119 mptsas_topo_change_list_t *topo_node = NULL;
6120 mptsas_topo_change_list_t *save_node = NULL;
6121 mptsas_t *mpt;
6122 dev_info_t *parent = NULL;
6123 mptsas_phymask_t phymask = 0;
6124 char *phy_mask_name;
6125 uint8_t flags = 0, physport = 0xff;
6126 uint8_t port_update = 0;
6127 uint_t event;
6129 topo_node = (mptsas_topo_change_list_t *)args;
6131 mpt = topo_node->mpt;
6132 event = topo_node->event;
6133 flags = topo_node->flags;
6135 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6137 NDBG20(("mptsas%d handle_dr enter", mpt->m_instance));
6139 switch (event) {
6140 case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6141 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6142 (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE) ||
6143 (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6145 * Direct attached or expander attached device added
6146 * into system or a Phys Disk that is being unhidden.
6148 port_update = 1;
6150 break;
6151 case MPTSAS_DR_EVENT_RECONFIG_SMP:
6153 * New expander added into system, it must be the head
6154 * of topo_change_list_t
6156 port_update = 1;
6157 break;
6158 default:
6159 port_update = 0;
6160 break;
6163 * All cases port_update == 1 may cause initiator port form change
6165 mutex_enter(&mpt->m_mutex);
6166 if (mpt->m_port_chng && port_update) {
6168 * mpt->m_port_chng flag indicates some PHYs of initiator
6169 * port have changed to online. So when expander added or
6170 * directly attached device online event come, we force to
6171 * update port information by issueing SAS IO Unit Page and
6172 * update PHYMASKs.
6174 (void) mptsas_update_phymask(mpt);
6175 mpt->m_port_chng = 0;
6178 mutex_exit(&mpt->m_mutex);
6179 while (topo_node) {
6180 phymask = 0;
6181 if (parent == NULL) {
6182 physport = topo_node->un.physport;
6183 event = topo_node->event;
6184 flags = topo_node->flags;
6185 if (event & (MPTSAS_DR_EVENT_OFFLINE_TARGET |
6186 MPTSAS_DR_EVENT_OFFLINE_SMP)) {
6188 * For all offline events, phymask is known
6190 phymask = topo_node->un.phymask;
6191 goto find_parent;
6193 if (event & MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6194 goto handle_topo_change;
6196 if (flags & MPTSAS_TOPO_FLAG_LUN_ASSOCIATED) {
6197 phymask = topo_node->un.phymask;
6198 goto find_parent;
6201 if ((flags ==
6202 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) &&
6203 (event == MPTSAS_DR_EVENT_RECONFIG_TARGET)) {
6205 * There is no any field in IR_CONFIG_CHANGE
6206 * event indicate physport/phynum, let's get
6207 * parent after SAS Device Page0 request.
6209 goto handle_topo_change;
6212 mutex_enter(&mpt->m_mutex);
6213 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6215 * If the direct attached device added or a
6216 * phys disk is being unhidden, argument
6217 * physport actually is PHY#, so we have to get
6218 * phymask according PHY#.
6220 physport = mpt->m_phy_info[physport].port_num;
6224 * Translate physport to phymask so that we can search
6225 * parent dip.
6227 phymask = mptsas_physport_to_phymask(mpt,
6228 physport);
6229 mutex_exit(&mpt->m_mutex);
6231 find_parent:
6232 bzero(phy_mask_name, MPTSAS_MAX_PHYS);
6234 * For RAID topology change node, write the iport name
6235 * as v0.
6237 if (flags & MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6238 (void) sprintf(phy_mask_name, "v0");
6239 } else {
6241 * phymask can bo 0 if the drive has been
6242 * pulled by the time an add event is
6243 * processed. If phymask is 0, just skip this
6244 * event and continue.
6246 if (phymask == 0) {
6247 mutex_enter(&mpt->m_mutex);
6248 save_node = topo_node;
6249 topo_node = topo_node->next;
6250 ASSERT(save_node);
6251 kmem_free(save_node,
6252 sizeof (mptsas_topo_change_list_t));
6253 mutex_exit(&mpt->m_mutex);
6255 parent = NULL;
6256 continue;
6258 (void) sprintf(phy_mask_name, "%x", phymask);
6260 parent = scsi_hba_iport_find(mpt->m_dip,
6261 phy_mask_name);
6262 if (parent == NULL) {
6263 mptsas_log(mpt, CE_WARN, "Failed to find an "
6264 "iport, should not happen!");
6265 goto out;
6269 ASSERT(parent);
6270 handle_topo_change:
6272 mutex_enter(&mpt->m_mutex);
6274 * If HBA is being reset, don't perform operations depending
6275 * on the IOC. We must free the topo list, however.
6277 if (!mpt->m_in_reset)
6278 mptsas_handle_topo_change(topo_node, parent);
6279 else
6280 NDBG20(("skipping topo change received during reset"));
6281 save_node = topo_node;
6282 topo_node = topo_node->next;
6283 ASSERT(save_node);
6284 kmem_free(save_node, sizeof (mptsas_topo_change_list_t));
6285 mutex_exit(&mpt->m_mutex);
6287 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6288 (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) ||
6289 (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED)) {
6291 * If direct attached device associated, make sure
6292 * reset the parent before start the next one. But
6293 * all devices associated with expander shares the
6294 * parent. Also, reset parent if this is for RAID.
6296 parent = NULL;
6299 out:
6300 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6303 static void
6304 mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
6305 dev_info_t *parent)
6307 mptsas_target_t *ptgt = NULL;
6308 mptsas_smp_t *psmp = NULL;
6309 mptsas_t *mpt = (void *)topo_node->mpt;
6310 uint16_t devhdl;
6311 uint16_t attached_devhdl;
6312 uint64_t sas_wwn = 0;
6313 int rval = 0;
6314 uint32_t page_address;
6315 uint8_t phy, flags;
6316 char *addr = NULL;
6317 dev_info_t *lundip;
6318 int circ = 0, circ1 = 0;
6319 char attached_wwnstr[MPTSAS_WWN_STRLEN];
6321 NDBG20(("mptsas%d handle_topo_change enter, devhdl 0x%x,"
6322 "event 0x%x, flags 0x%x", mpt->m_instance, topo_node->devhdl,
6323 topo_node->event, topo_node->flags));
6325 ASSERT(mutex_owned(&mpt->m_mutex));
6327 switch (topo_node->event) {
6328 case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6330 char *phy_mask_name;
6331 mptsas_phymask_t phymask = 0;
6333 if (topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6335 * Get latest RAID info.
6337 (void) mptsas_get_raid_info(mpt);
6338 ptgt = refhash_linear_search(mpt->m_targets,
6339 mptsas_target_eval_devhdl, &topo_node->devhdl);
6340 if (ptgt == NULL)
6341 break;
6342 } else {
6343 ptgt = (void *)topo_node->object;
6346 if (ptgt == NULL) {
6348 * If a Phys Disk was deleted, RAID info needs to be
6349 * updated to reflect the new topology.
6351 (void) mptsas_get_raid_info(mpt);
6354 * Get sas device page 0 by DevHandle to make sure if
6355 * SSP/SATA end device exist.
6357 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
6358 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
6359 topo_node->devhdl;
6361 rval = mptsas_get_target_device_info(mpt, page_address,
6362 &devhdl, &ptgt);
6363 if (rval == DEV_INFO_WRONG_DEVICE_TYPE) {
6364 mptsas_log(mpt, CE_NOTE,
6365 "mptsas_handle_topo_change: target %d is "
6366 "not a SAS/SATA device. \n",
6367 topo_node->devhdl);
6368 } else if (rval == DEV_INFO_FAIL_ALLOC) {
6369 mptsas_log(mpt, CE_NOTE,
6370 "mptsas_handle_topo_change: could not "
6371 "allocate memory. \n");
6374 * If rval is DEV_INFO_PHYS_DISK than there is nothing
6375 * else to do, just leave.
6377 if (rval != DEV_INFO_SUCCESS) {
6378 return;
6382 ASSERT(ptgt->m_devhdl == topo_node->devhdl);
6384 mutex_exit(&mpt->m_mutex);
6385 flags = topo_node->flags;
6387 if (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) {
6388 phymask = ptgt->m_addr.mta_phymask;
6389 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6390 (void) sprintf(phy_mask_name, "%x", phymask);
6391 parent = scsi_hba_iport_find(mpt->m_dip,
6392 phy_mask_name);
6393 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6394 if (parent == NULL) {
6395 mptsas_log(mpt, CE_WARN, "Failed to find a "
6396 "iport for PD, should not happen!");
6397 mutex_enter(&mpt->m_mutex);
6398 break;
6402 if (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6403 ndi_devi_enter(parent, &circ1);
6404 (void) mptsas_config_raid(parent, topo_node->devhdl,
6405 &lundip);
6406 ndi_devi_exit(parent, circ1);
6407 } else {
6409 * hold nexus for bus configure
6411 ndi_devi_enter(scsi_vhci_dip, &circ);
6412 ndi_devi_enter(parent, &circ1);
6413 rval = mptsas_config_target(parent, ptgt);
6415 * release nexus for bus configure
6417 ndi_devi_exit(parent, circ1);
6418 ndi_devi_exit(scsi_vhci_dip, circ);
6421 * Add parent's props for SMHBA support
6423 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6424 bzero(attached_wwnstr,
6425 sizeof (attached_wwnstr));
6426 (void) sprintf(attached_wwnstr, "w%016"PRIx64,
6427 ptgt->m_addr.mta_wwn);
6428 if (ddi_prop_update_string(DDI_DEV_T_NONE,
6429 parent,
6430 SCSI_ADDR_PROP_ATTACHED_PORT,
6431 attached_wwnstr)
6432 != DDI_PROP_SUCCESS) {
6433 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6434 parent,
6435 SCSI_ADDR_PROP_ATTACHED_PORT);
6436 mptsas_log(mpt, CE_WARN, "Failed to"
6437 "attached-port props");
6438 return;
6440 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6441 MPTSAS_NUM_PHYS, 1) !=
6442 DDI_PROP_SUCCESS) {
6443 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6444 parent, MPTSAS_NUM_PHYS);
6445 mptsas_log(mpt, CE_WARN, "Failed to"
6446 " create num-phys props");
6447 return;
6451 * Update PHY info for smhba
6453 mutex_enter(&mpt->m_mutex);
6454 if (mptsas_smhba_phy_init(mpt)) {
6455 mutex_exit(&mpt->m_mutex);
6456 mptsas_log(mpt, CE_WARN, "mptsas phy"
6457 " update failed");
6458 return;
6460 mutex_exit(&mpt->m_mutex);
6463 * topo_node->un.physport is really the PHY#
6464 * for direct attached devices
6466 mptsas_smhba_set_one_phy_props(mpt, parent,
6467 topo_node->un.physport, &attached_devhdl);
6469 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6470 MPTSAS_VIRTUAL_PORT, 0) !=
6471 DDI_PROP_SUCCESS) {
6472 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6473 parent, MPTSAS_VIRTUAL_PORT);
6474 mptsas_log(mpt, CE_WARN,
6475 "mptsas virtual-port"
6476 "port prop update failed");
6477 return;
6481 mutex_enter(&mpt->m_mutex);
6483 NDBG20(("mptsas%d handle_topo_change to online devhdl:%x, "
6484 "phymask:%x.", mpt->m_instance, ptgt->m_devhdl,
6485 ptgt->m_addr.mta_phymask));
6486 break;
6488 case MPTSAS_DR_EVENT_OFFLINE_TARGET:
6490 devhdl = topo_node->devhdl;
6491 ptgt = refhash_linear_search(mpt->m_targets,
6492 mptsas_target_eval_devhdl, &devhdl);
6493 if (ptgt == NULL)
6494 break;
6496 sas_wwn = ptgt->m_addr.mta_wwn;
6497 phy = ptgt->m_phynum;
6499 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
6501 if (sas_wwn) {
6502 (void) sprintf(addr, "w%016"PRIx64, sas_wwn);
6503 } else {
6504 (void) sprintf(addr, "p%x", phy);
6506 ASSERT(ptgt->m_devhdl == devhdl);
6508 if ((topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) ||
6509 (topo_node->flags ==
6510 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6512 * Get latest RAID info if RAID volume status changes
6513 * or Phys Disk status changes
6515 (void) mptsas_get_raid_info(mpt);
6518 * Abort all outstanding command on the device
6520 rval = mptsas_do_scsi_reset(mpt, devhdl);
6521 if (rval) {
6522 NDBG20(("mptsas%d handle_topo_change to reset target "
6523 "before offline devhdl:%x, phymask:%x, rval:%x",
6524 mpt->m_instance, ptgt->m_devhdl,
6525 ptgt->m_addr.mta_phymask, rval));
6528 mutex_exit(&mpt->m_mutex);
6530 ndi_devi_enter(scsi_vhci_dip, &circ);
6531 ndi_devi_enter(parent, &circ1);
6532 rval = mptsas_offline_target(parent, addr);
6533 ndi_devi_exit(parent, circ1);
6534 ndi_devi_exit(scsi_vhci_dip, circ);
6535 NDBG20(("mptsas%d handle_topo_change to offline devhdl:%x, "
6536 "phymask:%x, rval:%x", mpt->m_instance,
6537 ptgt->m_devhdl, ptgt->m_addr.mta_phymask, rval));
6539 kmem_free(addr, SCSI_MAXNAMELEN);
6542 * Clear parent's props for SMHBA support
6544 flags = topo_node->flags;
6545 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6546 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6547 if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6548 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6549 DDI_PROP_SUCCESS) {
6550 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6551 SCSI_ADDR_PROP_ATTACHED_PORT);
6552 mptsas_log(mpt, CE_WARN, "mptsas attached port "
6553 "prop update failed");
6554 break;
6556 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6557 MPTSAS_NUM_PHYS, 0) !=
6558 DDI_PROP_SUCCESS) {
6559 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6560 MPTSAS_NUM_PHYS);
6561 mptsas_log(mpt, CE_WARN, "mptsas num phys "
6562 "prop update failed");
6563 break;
6565 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6566 MPTSAS_VIRTUAL_PORT, 1) !=
6567 DDI_PROP_SUCCESS) {
6568 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6569 MPTSAS_VIRTUAL_PORT);
6570 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6571 "prop update failed");
6572 break;
6576 mutex_enter(&mpt->m_mutex);
6577 ptgt->m_led_status = 0;
6578 (void) mptsas_flush_led_status(mpt, ptgt);
6579 if (rval == DDI_SUCCESS) {
6580 refhash_remove(mpt->m_targets, ptgt);
6581 ptgt = NULL;
6582 } else {
6584 * clean DR_INTRANSITION flag to allow I/O down to
6585 * PHCI driver since failover finished.
6586 * Invalidate the devhdl
6588 ptgt->m_devhdl = MPTSAS_INVALID_DEVHDL;
6589 ptgt->m_tgt_unconfigured = 0;
6590 mutex_enter(&mpt->m_tx_waitq_mutex);
6591 ptgt->m_dr_flag = MPTSAS_DR_INACTIVE;
6592 mutex_exit(&mpt->m_tx_waitq_mutex);
6596 * Send SAS IO Unit Control to free the dev handle
6598 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6599 (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE)) {
6600 rval = mptsas_free_devhdl(mpt, devhdl);
6602 NDBG20(("mptsas%d handle_topo_change to remove "
6603 "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6604 rval));
6607 break;
6609 case MPTSAS_TOPO_FLAG_REMOVE_HANDLE:
6611 devhdl = topo_node->devhdl;
6613 * If this is the remove handle event, do a reset first.
6615 if (topo_node->event == MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6616 rval = mptsas_do_scsi_reset(mpt, devhdl);
6617 if (rval) {
6618 NDBG20(("mpt%d reset target before remove "
6619 "devhdl:%x, rval:%x", mpt->m_instance,
6620 devhdl, rval));
6625 * Send SAS IO Unit Control to free the dev handle
6627 rval = mptsas_free_devhdl(mpt, devhdl);
6628 NDBG20(("mptsas%d handle_topo_change to remove "
6629 "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6630 rval));
6631 break;
6633 case MPTSAS_DR_EVENT_RECONFIG_SMP:
6635 mptsas_smp_t smp;
6636 dev_info_t *smpdip;
6638 devhdl = topo_node->devhdl;
6640 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
6641 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)devhdl;
6642 rval = mptsas_get_sas_expander_page0(mpt, page_address, &smp);
6643 if (rval != DDI_SUCCESS) {
6644 mptsas_log(mpt, CE_WARN, "failed to online smp, "
6645 "handle %x", devhdl);
6646 return;
6649 psmp = mptsas_smp_alloc(mpt, &smp);
6650 if (psmp == NULL) {
6651 return;
6654 mutex_exit(&mpt->m_mutex);
6655 ndi_devi_enter(parent, &circ1);
6656 (void) mptsas_online_smp(parent, psmp, &smpdip);
6657 ndi_devi_exit(parent, circ1);
6659 mutex_enter(&mpt->m_mutex);
6660 break;
6662 case MPTSAS_DR_EVENT_OFFLINE_SMP:
6664 devhdl = topo_node->devhdl;
6665 uint32_t dev_info;
6667 psmp = refhash_linear_search(mpt->m_smp_targets,
6668 mptsas_smp_eval_devhdl, &devhdl);
6669 if (psmp == NULL)
6670 break;
6672 * The mptsas_smp_t data is released only if the dip is offlined
6673 * successfully.
6675 mutex_exit(&mpt->m_mutex);
6677 ndi_devi_enter(parent, &circ1);
6678 rval = mptsas_offline_smp(parent, psmp, NDI_DEVI_REMOVE);
6679 ndi_devi_exit(parent, circ1);
6681 dev_info = psmp->m_deviceinfo;
6682 if ((dev_info & DEVINFO_DIRECT_ATTACHED) ==
6683 DEVINFO_DIRECT_ATTACHED) {
6684 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6685 MPTSAS_VIRTUAL_PORT, 1) !=
6686 DDI_PROP_SUCCESS) {
6687 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6688 MPTSAS_VIRTUAL_PORT);
6689 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6690 "prop update failed");
6691 return;
6694 * Check whether the smp connected to the iport,
6696 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6697 MPTSAS_NUM_PHYS, 0) !=
6698 DDI_PROP_SUCCESS) {
6699 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6700 MPTSAS_NUM_PHYS);
6701 mptsas_log(mpt, CE_WARN, "mptsas num phys"
6702 "prop update failed");
6703 return;
6706 * Clear parent's attached-port props
6708 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6709 if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6710 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6711 DDI_PROP_SUCCESS) {
6712 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6713 SCSI_ADDR_PROP_ATTACHED_PORT);
6714 mptsas_log(mpt, CE_WARN, "mptsas attached port "
6715 "prop update failed");
6716 return;
6720 mutex_enter(&mpt->m_mutex);
6721 NDBG20(("mptsas%d handle_topo_change to remove devhdl:%x, "
6722 "rval:%x", mpt->m_instance, psmp->m_devhdl, rval));
6723 if (rval == DDI_SUCCESS) {
6724 refhash_remove(mpt->m_smp_targets, psmp);
6725 } else {
6726 psmp->m_devhdl = MPTSAS_INVALID_DEVHDL;
6729 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6731 break;
6733 default:
6734 return;
6739 * Record the event if its type is enabled in mpt instance by ioctl.
6741 static void
6742 mptsas_record_event(void *args)
6744 m_replyh_arg_t *replyh_arg;
6745 pMpi2EventNotificationReply_t eventreply;
6746 uint32_t event, rfm;
6747 mptsas_t *mpt;
6748 int i, j;
6749 uint16_t event_data_len;
6750 boolean_t sendAEN = FALSE;
6752 replyh_arg = (m_replyh_arg_t *)args;
6753 rfm = replyh_arg->rfm;
6754 mpt = replyh_arg->mpt;
6756 eventreply = (pMpi2EventNotificationReply_t)
6757 (mpt->m_reply_frame + (rfm -
6758 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
6759 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6763 * Generate a system event to let anyone who cares know that a
6764 * LOG_ENTRY_ADDED event has occurred. This is sent no matter what the
6765 * event mask is set to.
6767 if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
6768 sendAEN = TRUE;
6772 * Record the event only if it is not masked. Determine which dword
6773 * and bit of event mask to test.
6775 i = (uint8_t)(event / 32);
6776 j = (uint8_t)(event % 32);
6777 if ((i < 4) && ((1 << j) & mpt->m_event_mask[i])) {
6778 i = mpt->m_event_index;
6779 mpt->m_events[i].Type = event;
6780 mpt->m_events[i].Number = ++mpt->m_event_number;
6781 bzero(mpt->m_events[i].Data, MPTSAS_MAX_EVENT_DATA_LENGTH * 4);
6782 event_data_len = ddi_get16(mpt->m_acc_reply_frame_hdl,
6783 &eventreply->EventDataLength);
6785 if (event_data_len > 0) {
6787 * Limit data to size in m_event entry
6789 if (event_data_len > MPTSAS_MAX_EVENT_DATA_LENGTH) {
6790 event_data_len = MPTSAS_MAX_EVENT_DATA_LENGTH;
6792 for (j = 0; j < event_data_len; j++) {
6793 mpt->m_events[i].Data[j] =
6794 ddi_get32(mpt->m_acc_reply_frame_hdl,
6795 &(eventreply->EventData[j]));
6799 * check for index wrap-around
6801 if (++i == MPTSAS_EVENT_QUEUE_SIZE) {
6802 i = 0;
6804 mpt->m_event_index = (uint8_t)i;
6807 * Set flag to send the event.
6809 sendAEN = TRUE;
6814 * Generate a system event if flag is set to let anyone who cares know
6815 * that an event has occurred.
6817 if (sendAEN) {
6818 (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
6819 "SAS", NULL, NULL, DDI_NOSLEEP);
6823 #define SMP_RESET_IN_PROGRESS MPI2_EVENT_SAS_TOPO_LR_SMP_RESET_IN_PROGRESS
6825 * handle sync events from ioc in interrupt
6826 * return value:
6827 * DDI_SUCCESS: The event is handled by this func
6828 * DDI_FAILURE: Event is not handled
6830 static int
6831 mptsas_handle_event_sync(void *args)
6833 m_replyh_arg_t *replyh_arg;
6834 pMpi2EventNotificationReply_t eventreply;
6835 uint32_t event, rfm;
6836 mptsas_t *mpt;
6837 uint_t iocstatus;
6839 replyh_arg = (m_replyh_arg_t *)args;
6840 rfm = replyh_arg->rfm;
6841 mpt = replyh_arg->mpt;
6843 ASSERT(mutex_owned(&mpt->m_mutex));
6845 eventreply = (pMpi2EventNotificationReply_t)
6846 (mpt->m_reply_frame + (rfm -
6847 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
6848 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6850 if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
6851 &eventreply->IOCStatus)) {
6852 if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
6853 mptsas_log(mpt, CE_WARN,
6854 "!mptsas_handle_event_sync: event 0x%x, "
6855 "IOCStatus=0x%x, "
6856 "IOCLogInfo=0x%x", event, iocstatus,
6857 ddi_get32(mpt->m_acc_reply_frame_hdl,
6858 &eventreply->IOCLogInfo));
6859 } else {
6860 mptsas_log(mpt, CE_WARN,
6861 "mptsas_handle_event_sync: event 0x%x, "
6862 "IOCStatus=0x%x, "
6863 "(IOCLogInfo=0x%x)", event, iocstatus,
6864 ddi_get32(mpt->m_acc_reply_frame_hdl,
6865 &eventreply->IOCLogInfo));
6870 * figure out what kind of event we got and handle accordingly
6872 switch (event) {
6873 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6875 pMpi2EventDataSasTopologyChangeList_t sas_topo_change_list;
6876 uint8_t num_entries, expstatus, phy;
6877 uint8_t phystatus, physport, state, i;
6878 uint8_t start_phy_num, link_rate;
6879 uint16_t dev_handle, reason_code;
6880 uint16_t enc_handle, expd_handle;
6881 char string[80], curr[80], prev[80];
6882 mptsas_topo_change_list_t *topo_head = NULL;
6883 mptsas_topo_change_list_t *topo_tail = NULL;
6884 mptsas_topo_change_list_t *topo_node = NULL;
6885 mptsas_target_t *ptgt;
6886 mptsas_smp_t *psmp;
6887 uint8_t flags = 0, exp_flag;
6888 smhba_info_t *pSmhba = NULL;
6890 NDBG20(("mptsas_handle_event_sync: SAS topology change"));
6892 sas_topo_change_list = (pMpi2EventDataSasTopologyChangeList_t)
6893 eventreply->EventData;
6895 enc_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6896 &sas_topo_change_list->EnclosureHandle);
6897 expd_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6898 &sas_topo_change_list->ExpanderDevHandle);
6899 num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
6900 &sas_topo_change_list->NumEntries);
6901 start_phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
6902 &sas_topo_change_list->StartPhyNum);
6903 expstatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6904 &sas_topo_change_list->ExpStatus);
6905 physport = ddi_get8(mpt->m_acc_reply_frame_hdl,
6906 &sas_topo_change_list->PhysicalPort);
6908 string[0] = 0;
6909 if (expd_handle) {
6910 flags = MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED;
6911 switch (expstatus) {
6912 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
6913 (void) sprintf(string, " added");
6915 * New expander device added
6917 mpt->m_port_chng = 1;
6918 topo_node = kmem_zalloc(
6919 sizeof (mptsas_topo_change_list_t),
6920 KM_SLEEP);
6921 topo_node->mpt = mpt;
6922 topo_node->event = MPTSAS_DR_EVENT_RECONFIG_SMP;
6923 topo_node->un.physport = physport;
6924 topo_node->devhdl = expd_handle;
6925 topo_node->flags = flags;
6926 topo_node->object = NULL;
6927 if (topo_head == NULL) {
6928 topo_head = topo_tail = topo_node;
6929 } else {
6930 topo_tail->next = topo_node;
6931 topo_tail = topo_node;
6933 break;
6934 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
6935 (void) sprintf(string, " not responding, "
6936 "removed");
6937 psmp = refhash_linear_search(mpt->m_smp_targets,
6938 mptsas_smp_eval_devhdl, &expd_handle);
6939 if (psmp == NULL)
6940 break;
6942 topo_node = kmem_zalloc(
6943 sizeof (mptsas_topo_change_list_t),
6944 KM_SLEEP);
6945 topo_node->mpt = mpt;
6946 topo_node->un.phymask =
6947 psmp->m_addr.mta_phymask;
6948 topo_node->event = MPTSAS_DR_EVENT_OFFLINE_SMP;
6949 topo_node->devhdl = expd_handle;
6950 topo_node->flags = flags;
6951 topo_node->object = NULL;
6952 if (topo_head == NULL) {
6953 topo_head = topo_tail = topo_node;
6954 } else {
6955 topo_tail->next = topo_node;
6956 topo_tail = topo_node;
6958 break;
6959 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
6960 break;
6961 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
6962 (void) sprintf(string, " not responding, "
6963 "delaying removal");
6964 break;
6965 default:
6966 break;
6968 } else {
6969 flags = MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE;
6972 NDBG20(("SAS TOPOLOGY CHANGE for enclosure %x expander %x%s\n",
6973 enc_handle, expd_handle, string));
6974 for (i = 0; i < num_entries; i++) {
6975 phy = i + start_phy_num;
6976 phystatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6977 &sas_topo_change_list->PHY[i].PhyStatus);
6978 dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6979 &sas_topo_change_list->PHY[i].AttachedDevHandle);
6980 reason_code = phystatus & MPI2_EVENT_SAS_TOPO_RC_MASK;
6982 * Filter out processing of Phy Vacant Status unless
6983 * the reason code is "Not Responding". Process all
6984 * other combinations of Phy Status and Reason Codes.
6986 if ((phystatus &
6987 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) &&
6988 (reason_code !=
6989 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)) {
6990 continue;
6992 curr[0] = 0;
6993 prev[0] = 0;
6994 string[0] = 0;
6995 switch (reason_code) {
6996 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
6998 NDBG20(("mptsas%d phy %d physical_port %d "
6999 "dev_handle %d added", mpt->m_instance, phy,
7000 physport, dev_handle));
7001 link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
7002 &sas_topo_change_list->PHY[i].LinkRate);
7003 state = (link_rate &
7004 MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
7005 MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
7006 switch (state) {
7007 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7008 (void) sprintf(curr, "is disabled");
7009 break;
7010 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7011 (void) sprintf(curr, "is offline, "
7012 "failed speed negotiation");
7013 break;
7014 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7015 (void) sprintf(curr, "SATA OOB "
7016 "complete");
7017 break;
7018 case SMP_RESET_IN_PROGRESS:
7019 (void) sprintf(curr, "SMP reset in "
7020 "progress");
7021 break;
7022 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7023 (void) sprintf(curr, "is online at "
7024 "1.5 Gbps");
7025 break;
7026 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7027 (void) sprintf(curr, "is online at 3.0 "
7028 "Gbps");
7029 break;
7030 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7031 (void) sprintf(curr, "is online at 6.0 "
7032 "Gbps");
7033 break;
7034 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7035 (void) sprintf(curr,
7036 "is online at 12.0 Gbps");
7037 break;
7038 default:
7039 (void) sprintf(curr, "state is "
7040 "unknown");
7041 break;
7044 * New target device added into the system.
7045 * Set association flag according to if an
7046 * expander is used or not.
7048 exp_flag =
7049 MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
7050 if (flags ==
7051 MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
7052 flags = exp_flag;
7054 topo_node = kmem_zalloc(
7055 sizeof (mptsas_topo_change_list_t),
7056 KM_SLEEP);
7057 topo_node->mpt = mpt;
7058 topo_node->event =
7059 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7060 if (expd_handle == 0) {
7062 * Per MPI 2, if expander dev handle
7063 * is 0, it's a directly attached
7064 * device. So driver use PHY to decide
7065 * which iport is associated
7067 physport = phy;
7068 mpt->m_port_chng = 1;
7070 topo_node->un.physport = physport;
7071 topo_node->devhdl = dev_handle;
7072 topo_node->flags = flags;
7073 topo_node->object = NULL;
7074 if (topo_head == NULL) {
7075 topo_head = topo_tail = topo_node;
7076 } else {
7077 topo_tail->next = topo_node;
7078 topo_tail = topo_node;
7080 break;
7082 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
7084 NDBG20(("mptsas%d phy %d physical_port %d "
7085 "dev_handle %d removed", mpt->m_instance,
7086 phy, physport, dev_handle));
7088 * Set association flag according to if an
7089 * expander is used or not.
7091 exp_flag =
7092 MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
7093 if (flags ==
7094 MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
7095 flags = exp_flag;
7098 * Target device is removed from the system
7099 * Before the device is really offline from
7100 * from system.
7102 ptgt = refhash_linear_search(mpt->m_targets,
7103 mptsas_target_eval_devhdl, &dev_handle);
7105 * If ptgt is NULL here, it means that the
7106 * DevHandle is not in the hash table. This is
7107 * reasonable sometimes. For example, if a
7108 * disk was pulled, then added, then pulled
7109 * again, the disk will not have been put into
7110 * the hash table because the add event will
7111 * have an invalid phymask. BUT, this does not
7112 * mean that the DevHandle is invalid. The
7113 * controller will still have a valid DevHandle
7114 * that must be removed. To do this, use the
7115 * MPTSAS_TOPO_FLAG_REMOVE_HANDLE event.
7117 if (ptgt == NULL) {
7118 topo_node = kmem_zalloc(
7119 sizeof (mptsas_topo_change_list_t),
7120 KM_SLEEP);
7121 topo_node->mpt = mpt;
7122 topo_node->un.phymask = 0;
7123 topo_node->event =
7124 MPTSAS_TOPO_FLAG_REMOVE_HANDLE;
7125 topo_node->devhdl = dev_handle;
7126 topo_node->flags = flags;
7127 topo_node->object = NULL;
7128 if (topo_head == NULL) {
7129 topo_head = topo_tail =
7130 topo_node;
7131 } else {
7132 topo_tail->next = topo_node;
7133 topo_tail = topo_node;
7135 break;
7139 * Update DR flag immediately avoid I/O failure
7140 * before failover finish. Pay attention to the
7141 * mutex protect, we need grab m_tx_waitq_mutex
7142 * during set m_dr_flag because we won't add
7143 * the following command into waitq, instead,
7144 * we need return TRAN_BUSY in the tran_start
7145 * context.
7147 mutex_enter(&mpt->m_tx_waitq_mutex);
7148 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7149 mutex_exit(&mpt->m_tx_waitq_mutex);
7151 topo_node = kmem_zalloc(
7152 sizeof (mptsas_topo_change_list_t),
7153 KM_SLEEP);
7154 topo_node->mpt = mpt;
7155 topo_node->un.phymask =
7156 ptgt->m_addr.mta_phymask;
7157 topo_node->event =
7158 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7159 topo_node->devhdl = dev_handle;
7160 topo_node->flags = flags;
7161 topo_node->object = NULL;
7162 if (topo_head == NULL) {
7163 topo_head = topo_tail = topo_node;
7164 } else {
7165 topo_tail->next = topo_node;
7166 topo_tail = topo_node;
7168 break;
7170 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
7171 link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
7172 &sas_topo_change_list->PHY[i].LinkRate);
7173 state = (link_rate &
7174 MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
7175 MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
7176 pSmhba = &mpt->m_phy_info[i].smhba_info;
7177 pSmhba->negotiated_link_rate = state;
7178 switch (state) {
7179 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7180 (void) sprintf(curr, "is disabled");
7181 mptsas_smhba_log_sysevent(mpt,
7182 ESC_SAS_PHY_EVENT,
7183 SAS_PHY_REMOVE,
7184 &mpt->m_phy_info[i].smhba_info);
7185 mpt->m_phy_info[i].smhba_info.
7186 negotiated_link_rate
7187 = 0x1;
7188 break;
7189 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7190 (void) sprintf(curr, "is offline, "
7191 "failed speed negotiation");
7192 mptsas_smhba_log_sysevent(mpt,
7193 ESC_SAS_PHY_EVENT,
7194 SAS_PHY_OFFLINE,
7195 &mpt->m_phy_info[i].smhba_info);
7196 break;
7197 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7198 (void) sprintf(curr, "SATA OOB "
7199 "complete");
7200 break;
7201 case SMP_RESET_IN_PROGRESS:
7202 (void) sprintf(curr, "SMP reset in "
7203 "progress");
7204 break;
7205 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7206 (void) sprintf(curr, "is online at "
7207 "1.5 Gbps");
7208 if ((expd_handle == 0) &&
7209 (enc_handle == 1)) {
7210 mpt->m_port_chng = 1;
7212 mptsas_smhba_log_sysevent(mpt,
7213 ESC_SAS_PHY_EVENT,
7214 SAS_PHY_ONLINE,
7215 &mpt->m_phy_info[i].smhba_info);
7216 break;
7217 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7218 (void) sprintf(curr, "is online at 3.0 "
7219 "Gbps");
7220 if ((expd_handle == 0) &&
7221 (enc_handle == 1)) {
7222 mpt->m_port_chng = 1;
7224 mptsas_smhba_log_sysevent(mpt,
7225 ESC_SAS_PHY_EVENT,
7226 SAS_PHY_ONLINE,
7227 &mpt->m_phy_info[i].smhba_info);
7228 break;
7229 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7230 (void) sprintf(curr, "is online at "
7231 "6.0 Gbps");
7232 if ((expd_handle == 0) &&
7233 (enc_handle == 1)) {
7234 mpt->m_port_chng = 1;
7236 mptsas_smhba_log_sysevent(mpt,
7237 ESC_SAS_PHY_EVENT,
7238 SAS_PHY_ONLINE,
7239 &mpt->m_phy_info[i].smhba_info);
7240 break;
7241 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7242 (void) sprintf(curr, "is online at "
7243 "12.0 Gbps");
7244 if ((expd_handle == 0) &&
7245 (enc_handle == 1)) {
7246 mpt->m_port_chng = 1;
7248 mptsas_smhba_log_sysevent(mpt,
7249 ESC_SAS_PHY_EVENT,
7250 SAS_PHY_ONLINE,
7251 &mpt->m_phy_info[i].smhba_info);
7252 break;
7253 default:
7254 (void) sprintf(curr, "state is "
7255 "unknown");
7256 break;
7259 state = (link_rate &
7260 MPI2_EVENT_SAS_TOPO_LR_PREV_MASK) >>
7261 MPI2_EVENT_SAS_TOPO_LR_PREV_SHIFT;
7262 switch (state) {
7263 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7264 (void) sprintf(prev, ", was disabled");
7265 break;
7266 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7267 (void) sprintf(prev, ", was offline, "
7268 "failed speed negotiation");
7269 break;
7270 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7271 (void) sprintf(prev, ", was SATA OOB "
7272 "complete");
7273 break;
7274 case SMP_RESET_IN_PROGRESS:
7275 (void) sprintf(prev, ", was SMP reset "
7276 "in progress");
7277 break;
7278 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7279 (void) sprintf(prev, ", was online at "
7280 "1.5 Gbps");
7281 break;
7282 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7283 (void) sprintf(prev, ", was online at "
7284 "3.0 Gbps");
7285 break;
7286 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7287 (void) sprintf(prev, ", was online at "
7288 "6.0 Gbps");
7289 break;
7290 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7291 (void) sprintf(prev, ", was online at "
7292 "12.0 Gbps");
7293 break;
7294 default:
7295 break;
7297 (void) sprintf(&string[strlen(string)], "link "
7298 "changed, ");
7299 break;
7300 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
7301 continue;
7302 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
7303 (void) sprintf(&string[strlen(string)],
7304 "target not responding, delaying "
7305 "removal");
7306 break;
7308 NDBG20(("mptsas%d phy %d DevHandle %x, %s%s%s\n",
7309 mpt->m_instance, phy, dev_handle, string, curr,
7310 prev));
7312 if (topo_head != NULL) {
7314 * Launch DR taskq to handle topology change
7316 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7317 mptsas_handle_dr, (void *)topo_head,
7318 DDI_NOSLEEP)) != DDI_SUCCESS) {
7319 while (topo_head != NULL) {
7320 topo_node = topo_head;
7321 topo_head = topo_head->next;
7322 kmem_free(topo_node,
7323 sizeof (mptsas_topo_change_list_t));
7325 mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7326 "for handle SAS DR event failed. \n");
7329 break;
7331 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7333 Mpi2EventDataIrConfigChangeList_t *irChangeList;
7334 mptsas_topo_change_list_t *topo_head = NULL;
7335 mptsas_topo_change_list_t *topo_tail = NULL;
7336 mptsas_topo_change_list_t *topo_node = NULL;
7337 mptsas_target_t *ptgt;
7338 uint8_t num_entries, i, reason;
7339 uint16_t volhandle, diskhandle;
7341 irChangeList = (pMpi2EventDataIrConfigChangeList_t)
7342 eventreply->EventData;
7343 num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
7344 &irChangeList->NumElements);
7346 NDBG20(("mptsas%d IR_CONFIGURATION_CHANGE_LIST event received",
7347 mpt->m_instance));
7349 for (i = 0; i < num_entries; i++) {
7350 reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
7351 &irChangeList->ConfigElement[i].ReasonCode);
7352 volhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7353 &irChangeList->ConfigElement[i].VolDevHandle);
7354 diskhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7355 &irChangeList->ConfigElement[i].PhysDiskDevHandle);
7357 switch (reason) {
7358 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
7359 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
7361 NDBG20(("mptsas %d volume added\n",
7362 mpt->m_instance));
7364 topo_node = kmem_zalloc(
7365 sizeof (mptsas_topo_change_list_t),
7366 KM_SLEEP);
7368 topo_node->mpt = mpt;
7369 topo_node->event =
7370 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7371 topo_node->un.physport = 0xff;
7372 topo_node->devhdl = volhandle;
7373 topo_node->flags =
7374 MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7375 topo_node->object = NULL;
7376 if (topo_head == NULL) {
7377 topo_head = topo_tail = topo_node;
7378 } else {
7379 topo_tail->next = topo_node;
7380 topo_tail = topo_node;
7382 break;
7384 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
7385 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
7387 NDBG20(("mptsas %d volume deleted\n",
7388 mpt->m_instance));
7389 ptgt = refhash_linear_search(mpt->m_targets,
7390 mptsas_target_eval_devhdl, &volhandle);
7391 if (ptgt == NULL)
7392 break;
7395 * Clear any flags related to volume
7397 (void) mptsas_delete_volume(mpt, volhandle);
7400 * Update DR flag immediately avoid I/O failure
7402 mutex_enter(&mpt->m_tx_waitq_mutex);
7403 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7404 mutex_exit(&mpt->m_tx_waitq_mutex);
7406 topo_node = kmem_zalloc(
7407 sizeof (mptsas_topo_change_list_t),
7408 KM_SLEEP);
7409 topo_node->mpt = mpt;
7410 topo_node->un.phymask =
7411 ptgt->m_addr.mta_phymask;
7412 topo_node->event =
7413 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7414 topo_node->devhdl = volhandle;
7415 topo_node->flags =
7416 MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7417 topo_node->object = (void *)ptgt;
7418 if (topo_head == NULL) {
7419 topo_head = topo_tail = topo_node;
7420 } else {
7421 topo_tail->next = topo_node;
7422 topo_tail = topo_node;
7424 break;
7426 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
7427 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
7429 ptgt = refhash_linear_search(mpt->m_targets,
7430 mptsas_target_eval_devhdl, &diskhandle);
7431 if (ptgt == NULL)
7432 break;
7435 * Update DR flag immediately avoid I/O failure
7437 mutex_enter(&mpt->m_tx_waitq_mutex);
7438 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7439 mutex_exit(&mpt->m_tx_waitq_mutex);
7441 topo_node = kmem_zalloc(
7442 sizeof (mptsas_topo_change_list_t),
7443 KM_SLEEP);
7444 topo_node->mpt = mpt;
7445 topo_node->un.phymask =
7446 ptgt->m_addr.mta_phymask;
7447 topo_node->event =
7448 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7449 topo_node->devhdl = diskhandle;
7450 topo_node->flags =
7451 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7452 topo_node->object = (void *)ptgt;
7453 if (topo_head == NULL) {
7454 topo_head = topo_tail = topo_node;
7455 } else {
7456 topo_tail->next = topo_node;
7457 topo_tail = topo_node;
7459 break;
7461 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
7462 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
7465 * The physical drive is released by a IR
7466 * volume. But we cannot get the the physport
7467 * or phynum from the event data, so we only
7468 * can get the physport/phynum after SAS
7469 * Device Page0 request for the devhdl.
7471 topo_node = kmem_zalloc(
7472 sizeof (mptsas_topo_change_list_t),
7473 KM_SLEEP);
7474 topo_node->mpt = mpt;
7475 topo_node->un.phymask = 0;
7476 topo_node->event =
7477 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7478 topo_node->devhdl = diskhandle;
7479 topo_node->flags =
7480 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7481 topo_node->object = NULL;
7482 mpt->m_port_chng = 1;
7483 if (topo_head == NULL) {
7484 topo_head = topo_tail = topo_node;
7485 } else {
7486 topo_tail->next = topo_node;
7487 topo_tail = topo_node;
7489 break;
7491 default:
7492 break;
7496 if (topo_head != NULL) {
7498 * Launch DR taskq to handle topology change
7500 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7501 mptsas_handle_dr, (void *)topo_head,
7502 DDI_NOSLEEP)) != DDI_SUCCESS) {
7503 while (topo_head != NULL) {
7504 topo_node = topo_head;
7505 topo_head = topo_head->next;
7506 kmem_free(topo_node,
7507 sizeof (mptsas_topo_change_list_t));
7509 mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7510 "for handle SAS DR event failed. \n");
7513 break;
7515 default:
7516 return (DDI_FAILURE);
7519 return (DDI_SUCCESS);
7523 * handle events from ioc
7525 static void
7526 mptsas_handle_event(void *args)
7528 m_replyh_arg_t *replyh_arg;
7529 pMpi2EventNotificationReply_t eventreply;
7530 uint32_t event, iocloginfo, rfm;
7531 uint32_t status;
7532 uint8_t port;
7533 mptsas_t *mpt;
7534 uint_t iocstatus;
7536 replyh_arg = (m_replyh_arg_t *)args;
7537 rfm = replyh_arg->rfm;
7538 mpt = replyh_arg->mpt;
7540 mutex_enter(&mpt->m_mutex);
7542 * If HBA is being reset, drop incoming event.
7544 if (mpt->m_in_reset) {
7545 NDBG20(("dropping event received prior to reset"));
7546 mutex_exit(&mpt->m_mutex);
7547 return;
7550 eventreply = (pMpi2EventNotificationReply_t)
7551 (mpt->m_reply_frame + (rfm -
7552 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
7553 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
7555 if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
7556 &eventreply->IOCStatus)) {
7557 if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
7558 mptsas_log(mpt, CE_WARN,
7559 "!mptsas_handle_event: IOCStatus=0x%x, "
7560 "IOCLogInfo=0x%x", iocstatus,
7561 ddi_get32(mpt->m_acc_reply_frame_hdl,
7562 &eventreply->IOCLogInfo));
7563 } else {
7564 mptsas_log(mpt, CE_WARN,
7565 "mptsas_handle_event: IOCStatus=0x%x, "
7566 "IOCLogInfo=0x%x", iocstatus,
7567 ddi_get32(mpt->m_acc_reply_frame_hdl,
7568 &eventreply->IOCLogInfo));
7573 * figure out what kind of event we got and handle accordingly
7575 switch (event) {
7576 case MPI2_EVENT_LOG_ENTRY_ADDED:
7577 break;
7578 case MPI2_EVENT_LOG_DATA:
7579 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7580 &eventreply->IOCLogInfo);
7581 NDBG20(("mptsas %d log info %x received.\n", mpt->m_instance,
7582 iocloginfo));
7583 break;
7584 case MPI2_EVENT_STATE_CHANGE:
7585 NDBG20(("mptsas%d state change.", mpt->m_instance));
7586 break;
7587 case MPI2_EVENT_HARD_RESET_RECEIVED:
7588 NDBG20(("mptsas%d event change.", mpt->m_instance));
7589 break;
7590 case MPI2_EVENT_SAS_DISCOVERY:
7592 MPI2_EVENT_DATA_SAS_DISCOVERY *sasdiscovery;
7593 char string[80];
7594 uint8_t rc;
7596 sasdiscovery =
7597 (pMpi2EventDataSasDiscovery_t)eventreply->EventData;
7599 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7600 &sasdiscovery->ReasonCode);
7601 port = ddi_get8(mpt->m_acc_reply_frame_hdl,
7602 &sasdiscovery->PhysicalPort);
7603 status = ddi_get32(mpt->m_acc_reply_frame_hdl,
7604 &sasdiscovery->DiscoveryStatus);
7606 string[0] = 0;
7607 switch (rc) {
7608 case MPI2_EVENT_SAS_DISC_RC_STARTED:
7609 (void) sprintf(string, "STARTING");
7610 break;
7611 case MPI2_EVENT_SAS_DISC_RC_COMPLETED:
7612 (void) sprintf(string, "COMPLETED");
7613 break;
7614 default:
7615 (void) sprintf(string, "UNKNOWN");
7616 break;
7619 NDBG20(("SAS DISCOVERY is %s for port %d, status %x", string,
7620 port, status));
7622 break;
7624 case MPI2_EVENT_EVENT_CHANGE:
7625 NDBG20(("mptsas%d event change.", mpt->m_instance));
7626 break;
7627 case MPI2_EVENT_TASK_SET_FULL:
7629 pMpi2EventDataTaskSetFull_t taskfull;
7631 taskfull = (pMpi2EventDataTaskSetFull_t)eventreply->EventData;
7633 NDBG20(("TASK_SET_FULL received for mptsas%d, depth %d\n",
7634 mpt->m_instance, ddi_get16(mpt->m_acc_reply_frame_hdl,
7635 &taskfull->CurrentDepth)));
7636 break;
7638 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7641 * SAS TOPOLOGY CHANGE LIST Event has already been handled
7642 * in mptsas_handle_event_sync() of interrupt context
7644 break;
7646 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7648 pMpi2EventDataSasEnclDevStatusChange_t encstatus;
7649 uint8_t rc;
7650 char string[80];
7652 encstatus = (pMpi2EventDataSasEnclDevStatusChange_t)
7653 eventreply->EventData;
7655 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7656 &encstatus->ReasonCode);
7657 switch (rc) {
7658 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
7659 (void) sprintf(string, "added");
7660 break;
7661 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
7662 (void) sprintf(string, ", not responding");
7663 break;
7664 default:
7665 break;
7667 NDBG20(("mptsas%d ENCLOSURE STATUS CHANGE for enclosure "
7668 "%x%s\n", mpt->m_instance,
7669 ddi_get16(mpt->m_acc_reply_frame_hdl,
7670 &encstatus->EnclosureHandle), string));
7671 break;
7675 * MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE is handled by
7676 * mptsas_handle_event_sync,in here just send ack message.
7678 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7680 pMpi2EventDataSasDeviceStatusChange_t statuschange;
7681 uint8_t rc;
7682 uint16_t devhdl;
7683 uint64_t wwn = 0;
7684 uint32_t wwn_lo, wwn_hi;
7686 statuschange = (pMpi2EventDataSasDeviceStatusChange_t)
7687 eventreply->EventData;
7688 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7689 &statuschange->ReasonCode);
7690 wwn_lo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7691 (uint32_t *)(void *)&statuschange->SASAddress);
7692 wwn_hi = ddi_get32(mpt->m_acc_reply_frame_hdl,
7693 (uint32_t *)(void *)&statuschange->SASAddress + 1);
7694 wwn = ((uint64_t)wwn_hi << 32) | wwn_lo;
7695 devhdl = ddi_get16(mpt->m_acc_reply_frame_hdl,
7696 &statuschange->DevHandle);
7698 NDBG13(("MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE wwn is %"PRIx64,
7699 wwn));
7701 switch (rc) {
7702 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
7703 NDBG20(("SMART data received, ASC/ASCQ = %02x/%02x",
7704 ddi_get8(mpt->m_acc_reply_frame_hdl,
7705 &statuschange->ASC),
7706 ddi_get8(mpt->m_acc_reply_frame_hdl,
7707 &statuschange->ASCQ)));
7708 break;
7710 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
7711 NDBG20(("Device not supported"));
7712 break;
7714 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
7715 NDBG20(("IOC internally generated the Target Reset "
7716 "for devhdl:%x", devhdl));
7717 break;
7719 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
7720 NDBG20(("IOC's internally generated Target Reset "
7721 "completed for devhdl:%x", devhdl));
7722 break;
7724 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
7725 NDBG20(("IOC internally generated Abort Task"));
7726 break;
7728 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
7729 NDBG20(("IOC's internally generated Abort Task "
7730 "completed"));
7731 break;
7733 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
7734 NDBG20(("IOC internally generated Abort Task Set"));
7735 break;
7737 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
7738 NDBG20(("IOC internally generated Clear Task Set"));
7739 break;
7741 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
7742 NDBG20(("IOC internally generated Query Task"));
7743 break;
7745 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
7746 NDBG20(("Device sent an Asynchronous Notification"));
7747 break;
7749 default:
7750 break;
7752 break;
7754 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7757 * IR TOPOLOGY CHANGE LIST Event has already been handled
7758 * in mpt_handle_event_sync() of interrupt context
7760 break;
7762 case MPI2_EVENT_IR_OPERATION_STATUS:
7764 Mpi2EventDataIrOperationStatus_t *irOpStatus;
7765 char reason_str[80];
7766 uint8_t rc, percent;
7767 uint16_t handle;
7769 irOpStatus = (pMpi2EventDataIrOperationStatus_t)
7770 eventreply->EventData;
7771 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7772 &irOpStatus->RAIDOperation);
7773 percent = ddi_get8(mpt->m_acc_reply_frame_hdl,
7774 &irOpStatus->PercentComplete);
7775 handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7776 &irOpStatus->VolDevHandle);
7778 switch (rc) {
7779 case MPI2_EVENT_IR_RAIDOP_RESYNC:
7780 (void) sprintf(reason_str, "resync");
7781 break;
7782 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
7783 (void) sprintf(reason_str, "online capacity "
7784 "expansion");
7785 break;
7786 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
7787 (void) sprintf(reason_str, "consistency check");
7788 break;
7789 default:
7790 (void) sprintf(reason_str, "unknown reason %x",
7791 rc);
7794 NDBG20(("mptsas%d raid operational status: (%s)"
7795 "\thandle(0x%04x), percent complete(%d)\n",
7796 mpt->m_instance, reason_str, handle, percent));
7797 break;
7799 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7801 pMpi2EventDataSasBroadcastPrimitive_t sas_broadcast;
7802 uint8_t phy_num;
7803 uint8_t primitive;
7805 sas_broadcast = (pMpi2EventDataSasBroadcastPrimitive_t)
7806 eventreply->EventData;
7808 phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
7809 &sas_broadcast->PhyNum);
7810 primitive = ddi_get8(mpt->m_acc_reply_frame_hdl,
7811 &sas_broadcast->Primitive);
7813 switch (primitive) {
7814 case MPI2_EVENT_PRIMITIVE_CHANGE:
7815 mptsas_smhba_log_sysevent(mpt,
7816 ESC_SAS_HBA_PORT_BROADCAST,
7817 SAS_PORT_BROADCAST_CHANGE,
7818 &mpt->m_phy_info[phy_num].smhba_info);
7819 break;
7820 case MPI2_EVENT_PRIMITIVE_SES:
7821 mptsas_smhba_log_sysevent(mpt,
7822 ESC_SAS_HBA_PORT_BROADCAST,
7823 SAS_PORT_BROADCAST_SES,
7824 &mpt->m_phy_info[phy_num].smhba_info);
7825 break;
7826 case MPI2_EVENT_PRIMITIVE_EXPANDER:
7827 mptsas_smhba_log_sysevent(mpt,
7828 ESC_SAS_HBA_PORT_BROADCAST,
7829 SAS_PORT_BROADCAST_D01_4,
7830 &mpt->m_phy_info[phy_num].smhba_info);
7831 break;
7832 case MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT:
7833 mptsas_smhba_log_sysevent(mpt,
7834 ESC_SAS_HBA_PORT_BROADCAST,
7835 SAS_PORT_BROADCAST_D04_7,
7836 &mpt->m_phy_info[phy_num].smhba_info);
7837 break;
7838 case MPI2_EVENT_PRIMITIVE_RESERVED3:
7839 mptsas_smhba_log_sysevent(mpt,
7840 ESC_SAS_HBA_PORT_BROADCAST,
7841 SAS_PORT_BROADCAST_D16_7,
7842 &mpt->m_phy_info[phy_num].smhba_info);
7843 break;
7844 case MPI2_EVENT_PRIMITIVE_RESERVED4:
7845 mptsas_smhba_log_sysevent(mpt,
7846 ESC_SAS_HBA_PORT_BROADCAST,
7847 SAS_PORT_BROADCAST_D29_7,
7848 &mpt->m_phy_info[phy_num].smhba_info);
7849 break;
7850 case MPI2_EVENT_PRIMITIVE_CHANGE0_RESERVED:
7851 mptsas_smhba_log_sysevent(mpt,
7852 ESC_SAS_HBA_PORT_BROADCAST,
7853 SAS_PORT_BROADCAST_D24_0,
7854 &mpt->m_phy_info[phy_num].smhba_info);
7855 break;
7856 case MPI2_EVENT_PRIMITIVE_CHANGE1_RESERVED:
7857 mptsas_smhba_log_sysevent(mpt,
7858 ESC_SAS_HBA_PORT_BROADCAST,
7859 SAS_PORT_BROADCAST_D27_4,
7860 &mpt->m_phy_info[phy_num].smhba_info);
7861 break;
7862 default:
7863 NDBG16(("mptsas%d: unknown BROADCAST PRIMITIVE"
7864 " %x received",
7865 mpt->m_instance, primitive));
7866 break;
7868 NDBG16(("mptsas%d sas broadcast primitive: "
7869 "\tprimitive(0x%04x), phy(%d) complete\n",
7870 mpt->m_instance, primitive, phy_num));
7871 break;
7873 case MPI2_EVENT_IR_VOLUME:
7875 Mpi2EventDataIrVolume_t *irVolume;
7876 uint16_t devhandle;
7877 uint32_t state;
7878 int config, vol;
7879 uint8_t found = FALSE;
7881 irVolume = (pMpi2EventDataIrVolume_t)eventreply->EventData;
7882 state = ddi_get32(mpt->m_acc_reply_frame_hdl,
7883 &irVolume->NewValue);
7884 devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7885 &irVolume->VolDevHandle);
7887 NDBG20(("EVENT_IR_VOLUME event is received"));
7890 * Get latest RAID info and then find the DevHandle for this
7891 * event in the configuration. If the DevHandle is not found
7892 * just exit the event.
7894 (void) mptsas_get_raid_info(mpt);
7895 for (config = 0; (config < mpt->m_num_raid_configs) &&
7896 (!found); config++) {
7897 for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
7898 if (mpt->m_raidconfig[config].m_raidvol[vol].
7899 m_raidhandle == devhandle) {
7900 found = TRUE;
7901 break;
7905 if (!found) {
7906 break;
7909 switch (irVolume->ReasonCode) {
7910 case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
7912 uint32_t i;
7913 mpt->m_raidconfig[config].m_raidvol[vol].m_settings =
7914 state;
7916 i = state & MPI2_RAIDVOL0_SETTING_MASK_WRITE_CACHING;
7917 mptsas_log(mpt, CE_NOTE, " Volume %d settings changed"
7918 ", auto-config of hot-swap drives is %s"
7919 ", write caching is %s"
7920 ", hot-spare pool mask is %02x\n",
7921 vol, state &
7922 MPI2_RAIDVOL0_SETTING_AUTO_CONFIG_HSWAP_DISABLE
7923 ? "disabled" : "enabled",
7924 i == MPI2_RAIDVOL0_SETTING_UNCHANGED
7925 ? "controlled by member disks" :
7926 i == MPI2_RAIDVOL0_SETTING_DISABLE_WRITE_CACHING
7927 ? "disabled" :
7928 i == MPI2_RAIDVOL0_SETTING_ENABLE_WRITE_CACHING
7929 ? "enabled" :
7930 "incorrectly set",
7931 (state >> 16) & 0xff);
7932 break;
7934 case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
7936 mpt->m_raidconfig[config].m_raidvol[vol].m_state =
7937 (uint8_t)state;
7939 mptsas_log(mpt, CE_NOTE,
7940 "Volume %d is now %s\n", vol,
7941 state == MPI2_RAID_VOL_STATE_OPTIMAL
7942 ? "optimal" :
7943 state == MPI2_RAID_VOL_STATE_DEGRADED
7944 ? "degraded" :
7945 state == MPI2_RAID_VOL_STATE_ONLINE
7946 ? "online" :
7947 state == MPI2_RAID_VOL_STATE_INITIALIZING
7948 ? "initializing" :
7949 state == MPI2_RAID_VOL_STATE_FAILED
7950 ? "failed" :
7951 state == MPI2_RAID_VOL_STATE_MISSING
7952 ? "missing" :
7953 "state unknown");
7954 break;
7956 case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
7958 mpt->m_raidconfig[config].m_raidvol[vol].
7959 m_statusflags = state;
7961 mptsas_log(mpt, CE_NOTE,
7962 " Volume %d is now %s%s%s%s%s%s%s%s%s\n",
7963 vol,
7964 state & MPI2_RAIDVOL0_STATUS_FLAG_ENABLED
7965 ? ", enabled" : ", disabled",
7966 state & MPI2_RAIDVOL0_STATUS_FLAG_QUIESCED
7967 ? ", quiesced" : "",
7968 state & MPI2_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE
7969 ? ", inactive" : ", active",
7970 state &
7971 MPI2_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL
7972 ? ", bad block table is full" : "",
7973 state &
7974 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS
7975 ? ", resync in progress" : "",
7976 state & MPI2_RAIDVOL0_STATUS_FLAG_BACKGROUND_INIT
7977 ? ", background initialization in progress" : "",
7978 state &
7979 MPI2_RAIDVOL0_STATUS_FLAG_CAPACITY_EXPANSION
7980 ? ", capacity expansion in progress" : "",
7981 state &
7982 MPI2_RAIDVOL0_STATUS_FLAG_CONSISTENCY_CHECK
7983 ? ", consistency check in progress" : "",
7984 state & MPI2_RAIDVOL0_STATUS_FLAG_DATA_SCRUB
7985 ? ", data scrub in progress" : "");
7986 break;
7988 default:
7989 break;
7991 break;
7993 case MPI2_EVENT_IR_PHYSICAL_DISK:
7995 Mpi2EventDataIrPhysicalDisk_t *irPhysDisk;
7996 uint16_t devhandle, enchandle, slot;
7997 uint32_t status, state;
7998 uint8_t physdisknum, reason;
8000 irPhysDisk = (Mpi2EventDataIrPhysicalDisk_t *)
8001 eventreply->EventData;
8002 physdisknum = ddi_get8(mpt->m_acc_reply_frame_hdl,
8003 &irPhysDisk->PhysDiskNum);
8004 devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
8005 &irPhysDisk->PhysDiskDevHandle);
8006 enchandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
8007 &irPhysDisk->EnclosureHandle);
8008 slot = ddi_get16(mpt->m_acc_reply_frame_hdl,
8009 &irPhysDisk->Slot);
8010 state = ddi_get32(mpt->m_acc_reply_frame_hdl,
8011 &irPhysDisk->NewValue);
8012 reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
8013 &irPhysDisk->ReasonCode);
8015 NDBG20(("EVENT_IR_PHYSICAL_DISK event is received"));
8017 switch (reason) {
8018 case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
8019 mptsas_log(mpt, CE_NOTE,
8020 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8021 "for enclosure with handle 0x%x is now in hot "
8022 "spare pool %d",
8023 physdisknum, devhandle, slot, enchandle,
8024 (state >> 16) & 0xff);
8025 break;
8027 case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
8028 status = state;
8029 mptsas_log(mpt, CE_NOTE,
8030 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8031 "for enclosure with handle 0x%x is now "
8032 "%s%s%s%s%s\n", physdisknum, devhandle, slot,
8033 enchandle,
8034 status & MPI2_PHYSDISK0_STATUS_FLAG_INACTIVE_VOLUME
8035 ? ", inactive" : ", active",
8036 status & MPI2_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC
8037 ? ", out of sync" : "",
8038 status & MPI2_PHYSDISK0_STATUS_FLAG_QUIESCED
8039 ? ", quiesced" : "",
8040 status &
8041 MPI2_PHYSDISK0_STATUS_FLAG_WRITE_CACHE_ENABLED
8042 ? ", write cache enabled" : "",
8043 status & MPI2_PHYSDISK0_STATUS_FLAG_OCE_TARGET
8044 ? ", capacity expansion target" : "");
8045 break;
8047 case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
8048 mptsas_log(mpt, CE_NOTE,
8049 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8050 "for enclosure with handle 0x%x is now %s\n",
8051 physdisknum, devhandle, slot, enchandle,
8052 state == MPI2_RAID_PD_STATE_OPTIMAL
8053 ? "optimal" :
8054 state == MPI2_RAID_PD_STATE_REBUILDING
8055 ? "rebuilding" :
8056 state == MPI2_RAID_PD_STATE_DEGRADED
8057 ? "degraded" :
8058 state == MPI2_RAID_PD_STATE_HOT_SPARE
8059 ? "a hot spare" :
8060 state == MPI2_RAID_PD_STATE_ONLINE
8061 ? "online" :
8062 state == MPI2_RAID_PD_STATE_OFFLINE
8063 ? "offline" :
8064 state == MPI2_RAID_PD_STATE_NOT_COMPATIBLE
8065 ? "not compatible" :
8066 state == MPI2_RAID_PD_STATE_NOT_CONFIGURED
8067 ? "not configured" :
8068 "state unknown");
8069 break;
8071 break;
8073 default:
8074 NDBG20(("mptsas%d: unknown event %x received",
8075 mpt->m_instance, event));
8076 break;
8080 * Return the reply frame to the free queue.
8082 ddi_put32(mpt->m_acc_free_queue_hdl,
8083 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index], rfm);
8084 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
8085 DDI_DMA_SYNC_FORDEV);
8086 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
8087 mpt->m_free_index = 0;
8089 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
8090 mpt->m_free_index);
8091 mutex_exit(&mpt->m_mutex);
8095 * invoked from timeout() to restart qfull cmds with throttle == 0
8097 static void
8098 mptsas_restart_cmd(void *arg)
8100 mptsas_t *mpt = arg;
8101 mptsas_target_t *ptgt = NULL;
8103 mutex_enter(&mpt->m_mutex);
8105 mpt->m_restart_cmd_timeid = 0;
8107 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
8108 ptgt = refhash_next(mpt->m_targets, ptgt)) {
8109 if (ptgt->m_reset_delay == 0) {
8110 if (ptgt->m_t_throttle == QFULL_THROTTLE) {
8111 mptsas_set_throttle(mpt, ptgt,
8112 MAX_THROTTLE);
8116 mptsas_restart_hba(mpt);
8117 mutex_exit(&mpt->m_mutex);
8120 void
8121 mptsas_remove_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
8123 int slot;
8124 mptsas_slots_t *slots = mpt->m_active;
8125 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8127 ASSERT(cmd != NULL);
8128 ASSERT(cmd->cmd_queued == FALSE);
8131 * Task Management cmds are removed in their own routines. Also,
8132 * we don't want to modify timeout based on TM cmds.
8134 if (cmd->cmd_flags & CFLAG_TM_CMD) {
8135 return;
8138 slot = cmd->cmd_slot;
8141 * remove the cmd.
8143 if (cmd == slots->m_slot[slot]) {
8144 NDBG31(("mptsas_remove_cmd: removing cmd=0x%p, flags "
8145 "0x%x", (void *)cmd, cmd->cmd_flags));
8146 slots->m_slot[slot] = NULL;
8147 mpt->m_ncmds--;
8150 * only decrement per target ncmds if command
8151 * has a target associated with it.
8153 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
8154 ptgt->m_t_ncmds--;
8156 * reset throttle if we just ran an untagged command
8157 * to a tagged target
8159 if ((ptgt->m_t_ncmds == 0) &&
8160 ((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0)) {
8161 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8165 * Remove this command from the active queue.
8167 if (cmd->cmd_active_expiration != 0) {
8168 TAILQ_REMOVE(&ptgt->m_active_cmdq, cmd,
8169 cmd_active_link);
8170 cmd->cmd_active_expiration = 0;
8176 * This is all we need to do for ioc commands.
8178 if (cmd->cmd_flags & CFLAG_CMDIOC) {
8179 mptsas_return_to_pool(mpt, cmd);
8180 return;
8183 ASSERT(cmd != slots->m_slot[cmd->cmd_slot]);
8187 * accept all cmds on the tx_waitq if any and then
8188 * start a fresh request from the top of the device queue.
8190 * since there are always cmds queued on the tx_waitq, and rare cmds on
8191 * the instance waitq, so this function should not be invoked in the ISR,
8192 * the mptsas_restart_waitq() is invoked in the ISR instead. otherwise, the
8193 * burden belongs to the IO dispatch CPUs is moved the interrupt CPU.
8195 static void
8196 mptsas_restart_hba(mptsas_t *mpt)
8198 ASSERT(mutex_owned(&mpt->m_mutex));
8200 mutex_enter(&mpt->m_tx_waitq_mutex);
8201 if (mpt->m_tx_waitq) {
8202 mptsas_accept_tx_waitq(mpt);
8204 mutex_exit(&mpt->m_tx_waitq_mutex);
8205 mptsas_restart_waitq(mpt);
8209 * start a fresh request from the top of the device queue
8211 static void
8212 mptsas_restart_waitq(mptsas_t *mpt)
8214 mptsas_cmd_t *cmd, *next_cmd;
8215 mptsas_target_t *ptgt = NULL;
8217 NDBG1(("mptsas_restart_waitq: mpt=0x%p", (void *)mpt));
8219 ASSERT(mutex_owned(&mpt->m_mutex));
8222 * If there is a reset delay, don't start any cmds. Otherwise, start
8223 * as many cmds as possible.
8224 * Since SMID 0 is reserved and the TM slot is reserved, the actual max
8225 * commands is m_max_requests - 2.
8227 cmd = mpt->m_waitq;
8229 while (cmd != NULL) {
8230 next_cmd = cmd->cmd_linkp;
8231 if (cmd->cmd_flags & CFLAG_PASSTHRU) {
8232 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8234 * passthru command get slot need
8235 * set CFLAG_PREPARED.
8237 cmd->cmd_flags |= CFLAG_PREPARED;
8238 mptsas_waitq_delete(mpt, cmd);
8239 mptsas_start_passthru(mpt, cmd);
8241 cmd = next_cmd;
8242 continue;
8244 if (cmd->cmd_flags & CFLAG_CONFIG) {
8245 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8247 * Send the config page request and delete it
8248 * from the waitq.
8250 cmd->cmd_flags |= CFLAG_PREPARED;
8251 mptsas_waitq_delete(mpt, cmd);
8252 mptsas_start_config_page_access(mpt, cmd);
8254 cmd = next_cmd;
8255 continue;
8257 if (cmd->cmd_flags & CFLAG_FW_DIAG) {
8258 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8260 * Send the FW Diag request and delete if from
8261 * the waitq.
8263 cmd->cmd_flags |= CFLAG_PREPARED;
8264 mptsas_waitq_delete(mpt, cmd);
8265 mptsas_start_diag(mpt, cmd);
8267 cmd = next_cmd;
8268 continue;
8271 ptgt = cmd->cmd_tgt_addr;
8272 if (ptgt && (ptgt->m_t_throttle == DRAIN_THROTTLE) &&
8273 (ptgt->m_t_ncmds == 0)) {
8274 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8276 if ((mpt->m_ncmds <= (mpt->m_max_requests - 2)) &&
8277 (ptgt && (ptgt->m_reset_delay == 0)) &&
8278 (ptgt && (ptgt->m_t_ncmds <
8279 ptgt->m_t_throttle))) {
8280 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8281 mptsas_waitq_delete(mpt, cmd);
8282 (void) mptsas_start_cmd(mpt, cmd);
8285 cmd = next_cmd;
8289 * Cmds are queued if tran_start() doesn't get the m_mutexlock(no wait).
8290 * Accept all those queued cmds before new cmd is accept so that the
8291 * cmds are sent in order.
8293 static void
8294 mptsas_accept_tx_waitq(mptsas_t *mpt)
8296 mptsas_cmd_t *cmd;
8298 ASSERT(mutex_owned(&mpt->m_mutex));
8299 ASSERT(mutex_owned(&mpt->m_tx_waitq_mutex));
8302 * A Bus Reset could occur at any time and flush the tx_waitq,
8303 * so we cannot count on the tx_waitq to contain even one cmd.
8304 * And when the m_tx_waitq_mutex is released and run
8305 * mptsas_accept_pkt(), the tx_waitq may be flushed.
8307 cmd = mpt->m_tx_waitq;
8308 for (;;) {
8309 if ((cmd = mpt->m_tx_waitq) == NULL) {
8310 mpt->m_tx_draining = 0;
8311 break;
8313 if ((mpt->m_tx_waitq = cmd->cmd_linkp) == NULL) {
8314 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
8316 cmd->cmd_linkp = NULL;
8317 mutex_exit(&mpt->m_tx_waitq_mutex);
8318 if (mptsas_accept_pkt(mpt, cmd) != TRAN_ACCEPT)
8319 cmn_err(CE_WARN, "mpt: mptsas_accept_tx_waitq: failed "
8320 "to accept cmd on queue\n");
8321 mutex_enter(&mpt->m_tx_waitq_mutex);
8327 * mpt tag type lookup
8329 static char mptsas_tag_lookup[] =
8330 {0, MSG_HEAD_QTAG, MSG_ORDERED_QTAG, 0, MSG_SIMPLE_QTAG};
8332 static int
8333 mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
8335 struct scsi_pkt *pkt = CMD2PKT(cmd);
8336 uint32_t control = 0;
8337 caddr_t mem, arsbuf;
8338 pMpi2SCSIIORequest_t io_request;
8339 ddi_dma_handle_t dma_hdl = mpt->m_dma_req_frame_hdl;
8340 ddi_acc_handle_t acc_hdl = mpt->m_acc_req_frame_hdl;
8341 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8342 uint16_t SMID, io_flags = 0;
8343 uint8_t ars_size;
8344 uint64_t request_desc;
8345 uint32_t ars_dmaaddrlow;
8346 mptsas_cmd_t *c;
8348 NDBG1(("mptsas_start_cmd: cmd=0x%p, flags 0x%x", (void *)cmd,
8349 cmd->cmd_flags));
8352 * Set SMID and increment index. Rollover to 1 instead of 0 if index
8353 * is at the max. 0 is an invalid SMID, so we call the first index 1.
8355 SMID = cmd->cmd_slot;
8358 * It is possible for back to back device reset to
8359 * happen before the reset delay has expired. That's
8360 * ok, just let the device reset go out on the bus.
8362 if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8363 ASSERT(ptgt->m_reset_delay == 0);
8367 * if a non-tagged cmd is submitted to an active tagged target
8368 * then drain before submitting this cmd; SCSI-2 allows RQSENSE
8369 * to be untagged
8371 if (((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0) &&
8372 (ptgt->m_t_ncmds > 1) &&
8373 ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) &&
8374 (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE)) {
8375 if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8376 NDBG23(("target=%d, untagged cmd, start draining\n",
8377 ptgt->m_devhdl));
8379 if (ptgt->m_reset_delay == 0) {
8380 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
8383 mptsas_remove_cmd(mpt, cmd);
8384 cmd->cmd_pkt_flags |= FLAG_HEAD;
8385 mptsas_waitq_add(mpt, cmd);
8387 return (DDI_FAILURE);
8391 * Set correct tag bits.
8393 if (cmd->cmd_pkt_flags & FLAG_TAGMASK) {
8394 switch (mptsas_tag_lookup[((cmd->cmd_pkt_flags &
8395 FLAG_TAGMASK) >> 12)]) {
8396 case MSG_SIMPLE_QTAG:
8397 control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8398 break;
8399 case MSG_HEAD_QTAG:
8400 control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
8401 break;
8402 case MSG_ORDERED_QTAG:
8403 control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
8404 break;
8405 default:
8406 mptsas_log(mpt, CE_WARN, "mpt: Invalid tag type\n");
8407 break;
8409 } else {
8410 if (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
8411 ptgt->m_t_throttle = 1;
8413 control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8416 if (cmd->cmd_pkt_flags & FLAG_TLR) {
8417 control |= MPI2_SCSIIO_CONTROL_TLR_ON;
8420 mem = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
8421 io_request = (pMpi2SCSIIORequest_t)mem;
8422 if (cmd->cmd_extrqslen != 0) {
8424 * Mapping of the buffer was done in mptsas_pkt_alloc_extern().
8425 * Calculate the DMA address with the same offset.
8427 arsbuf = cmd->cmd_arq_buf;
8428 ars_size = cmd->cmd_extrqslen;
8429 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
8430 ((uintptr_t)arsbuf - (uintptr_t)mpt->m_req_sense)) &
8431 0xffffffffu;
8432 } else {
8433 arsbuf = mpt->m_req_sense + (mpt->m_req_sense_size * (SMID-1));
8434 cmd->cmd_arq_buf = arsbuf;
8435 ars_size = mpt->m_req_sense_size;
8436 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
8437 (mpt->m_req_sense_size * (SMID-1))) &
8438 0xffffffffu;
8440 bzero(io_request, sizeof (Mpi2SCSIIORequest_t));
8441 bzero(arsbuf, ars_size);
8443 ddi_put8(acc_hdl, &io_request->SGLOffset0, offsetof
8444 (MPI2_SCSI_IO_REQUEST, SGL) / 4);
8445 mptsas_init_std_hdr(acc_hdl, io_request, ptgt->m_devhdl, Lun(cmd), 0,
8446 MPI2_FUNCTION_SCSI_IO_REQUEST);
8448 (void) ddi_rep_put8(acc_hdl, (uint8_t *)pkt->pkt_cdbp,
8449 io_request->CDB.CDB32, cmd->cmd_cdblen, DDI_DEV_AUTOINCR);
8451 io_flags = cmd->cmd_cdblen;
8452 if (mptsas_use_fastpath &&
8453 ptgt->m_io_flags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) {
8454 io_flags |= MPI25_SCSIIO_IOFLAGS_FAST_PATH;
8455 request_desc = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
8456 } else {
8457 request_desc = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
8459 ddi_put16(acc_hdl, &io_request->IoFlags, io_flags);
8461 * setup the Scatter/Gather DMA list for this request
8463 if (cmd->cmd_cookiec > 0) {
8464 mptsas_sge_setup(mpt, cmd, &control, io_request, acc_hdl);
8465 } else {
8466 ddi_put32(acc_hdl, &io_request->SGL.MpiSimple.FlagsLength,
8467 ((uint32_t)MPI2_SGE_FLAGS_LAST_ELEMENT |
8468 MPI2_SGE_FLAGS_END_OF_BUFFER |
8469 MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
8470 MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
8474 * save ARQ information
8476 ddi_put8(acc_hdl, &io_request->SenseBufferLength, ars_size);
8477 ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress, ars_dmaaddrlow);
8479 ddi_put32(acc_hdl, &io_request->Control, control);
8481 NDBG31(("starting message=%d(0x%p), with cmd=0x%p",
8482 SMID, (void *)io_request, (void *)cmd));
8484 (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
8485 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
8486 DDI_DMA_SYNC_FORDEV);
8489 * Build request descriptor and write it to the request desc post reg.
8491 request_desc |= (SMID << 16);
8492 request_desc |= (uint64_t)ptgt->m_devhdl << 48;
8493 MPTSAS_START_CMD(mpt, request_desc);
8496 * Start timeout.
8498 cmd->cmd_active_expiration =
8499 gethrtime() + (hrtime_t)pkt->pkt_time * NANOSEC;
8500 #ifdef MPTSAS_TEST
8502 * Force timeouts to happen immediately.
8504 if (mptsas_test_timeouts)
8505 cmd->cmd_active_expiration = gethrtime();
8506 #endif
8507 c = TAILQ_FIRST(&ptgt->m_active_cmdq);
8508 if (c == NULL ||
8509 c->cmd_active_expiration < cmd->cmd_active_expiration) {
8511 * Common case is that this is the last pending expiration
8512 * (or queue is empty). Insert at head of the queue.
8514 TAILQ_INSERT_HEAD(&ptgt->m_active_cmdq, cmd, cmd_active_link);
8515 } else {
8517 * Queue is not empty and first element expires later than
8518 * this command. Search for element expiring sooner.
8520 while ((c = TAILQ_NEXT(c, cmd_active_link)) != NULL) {
8521 if (c->cmd_active_expiration <
8522 cmd->cmd_active_expiration) {
8523 TAILQ_INSERT_BEFORE(c, cmd, cmd_active_link);
8524 break;
8527 if (c == NULL) {
8529 * No element found expiring sooner, append to
8530 * non-empty queue.
8532 TAILQ_INSERT_TAIL(&ptgt->m_active_cmdq, cmd,
8533 cmd_active_link);
8537 if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
8538 (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
8539 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8540 return (DDI_FAILURE);
8542 return (DDI_SUCCESS);
8546 * Select a helper thread to handle current doneq
8548 static void
8549 mptsas_deliver_doneq_thread(mptsas_t *mpt)
8551 uint64_t t, i;
8552 uint32_t min = 0xffffffff;
8553 mptsas_doneq_thread_list_t *item;
8555 for (i = 0; i < mpt->m_doneq_thread_n; i++) {
8556 item = &mpt->m_doneq_thread_id[i];
8558 * If the completed command on help thread[i] less than
8559 * doneq_thread_threshold, then pick the thread[i]. Otherwise
8560 * pick a thread which has least completed command.
8563 mutex_enter(&item->mutex);
8564 if (item->len < mpt->m_doneq_thread_threshold) {
8565 t = i;
8566 mutex_exit(&item->mutex);
8567 break;
8569 if (item->len < min) {
8570 min = item->len;
8571 t = i;
8573 mutex_exit(&item->mutex);
8575 mutex_enter(&mpt->m_doneq_thread_id[t].mutex);
8576 mptsas_doneq_mv(mpt, t);
8577 cv_signal(&mpt->m_doneq_thread_id[t].cv);
8578 mutex_exit(&mpt->m_doneq_thread_id[t].mutex);
8582 * move the current global doneq to the doneq of thead[t]
8584 static void
8585 mptsas_doneq_mv(mptsas_t *mpt, uint64_t t)
8587 mptsas_cmd_t *cmd;
8588 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
8590 ASSERT(mutex_owned(&item->mutex));
8591 while ((cmd = mpt->m_doneq) != NULL) {
8592 if ((mpt->m_doneq = cmd->cmd_linkp) == NULL) {
8593 mpt->m_donetail = &mpt->m_doneq;
8595 cmd->cmd_linkp = NULL;
8596 *item->donetail = cmd;
8597 item->donetail = &cmd->cmd_linkp;
8598 mpt->m_doneq_len--;
8599 item->len++;
8603 void
8604 mptsas_fma_check(mptsas_t *mpt, mptsas_cmd_t *cmd)
8606 struct scsi_pkt *pkt = CMD2PKT(cmd);
8608 /* Check all acc and dma handles */
8609 if ((mptsas_check_acc_handle(mpt->m_datap) !=
8610 DDI_SUCCESS) ||
8611 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
8612 DDI_SUCCESS) ||
8613 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl) !=
8614 DDI_SUCCESS) ||
8615 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
8616 DDI_SUCCESS) ||
8617 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
8618 DDI_SUCCESS) ||
8619 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
8620 DDI_SUCCESS) ||
8621 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
8622 DDI_SUCCESS) ||
8623 (mptsas_check_acc_handle(mpt->m_config_handle) !=
8624 DDI_SUCCESS)) {
8625 ddi_fm_service_impact(mpt->m_dip,
8626 DDI_SERVICE_UNAFFECTED);
8627 ddi_fm_acc_err_clear(mpt->m_config_handle,
8628 DDI_FME_VER0);
8629 pkt->pkt_reason = CMD_TRAN_ERR;
8630 pkt->pkt_statistics = 0;
8632 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
8633 DDI_SUCCESS) ||
8634 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl) !=
8635 DDI_SUCCESS) ||
8636 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
8637 DDI_SUCCESS) ||
8638 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
8639 DDI_SUCCESS) ||
8640 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
8641 DDI_SUCCESS) ||
8642 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
8643 DDI_SUCCESS)) {
8644 ddi_fm_service_impact(mpt->m_dip,
8645 DDI_SERVICE_UNAFFECTED);
8646 pkt->pkt_reason = CMD_TRAN_ERR;
8647 pkt->pkt_statistics = 0;
8649 if (cmd->cmd_dmahandle &&
8650 (mptsas_check_dma_handle(cmd->cmd_dmahandle) != DDI_SUCCESS)) {
8651 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8652 pkt->pkt_reason = CMD_TRAN_ERR;
8653 pkt->pkt_statistics = 0;
8655 if ((cmd->cmd_extra_frames &&
8656 ((mptsas_check_dma_handle(cmd->cmd_extra_frames->m_dma_hdl) !=
8657 DDI_SUCCESS) ||
8658 (mptsas_check_acc_handle(cmd->cmd_extra_frames->m_acc_hdl) !=
8659 DDI_SUCCESS)))) {
8660 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8661 pkt->pkt_reason = CMD_TRAN_ERR;
8662 pkt->pkt_statistics = 0;
8667 * These routines manipulate the queue of commands that
8668 * are waiting for their completion routines to be called.
8669 * The queue is usually in FIFO order but on an MP system
8670 * it's possible for the completion routines to get out
8671 * of order. If that's a problem you need to add a global
8672 * mutex around the code that calls the completion routine
8673 * in the interrupt handler.
8675 static void
8676 mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8678 struct scsi_pkt *pkt = CMD2PKT(cmd);
8680 NDBG31(("mptsas_doneq_add: cmd=0x%p", (void *)cmd));
8682 ASSERT((cmd->cmd_flags & CFLAG_COMPLETED) == 0);
8683 cmd->cmd_linkp = NULL;
8684 cmd->cmd_flags |= CFLAG_FINISHED;
8685 cmd->cmd_flags &= ~CFLAG_IN_TRANSPORT;
8687 mptsas_fma_check(mpt, cmd);
8690 * only add scsi pkts that have completion routines to
8691 * the doneq. no intr cmds do not have callbacks.
8693 if (pkt && (pkt->pkt_comp)) {
8694 *mpt->m_donetail = cmd;
8695 mpt->m_donetail = &cmd->cmd_linkp;
8696 mpt->m_doneq_len++;
8700 static mptsas_cmd_t *
8701 mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t)
8703 mptsas_cmd_t *cmd;
8704 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
8706 /* pop one off the done queue */
8707 if ((cmd = item->doneq) != NULL) {
8708 /* if the queue is now empty fix the tail pointer */
8709 NDBG31(("mptsas_doneq_thread_rm: cmd=0x%p", (void *)cmd));
8710 if ((item->doneq = cmd->cmd_linkp) == NULL) {
8711 item->donetail = &item->doneq;
8713 cmd->cmd_linkp = NULL;
8714 item->len--;
8716 return (cmd);
8719 static void
8720 mptsas_doneq_empty(mptsas_t *mpt)
8722 if (mpt->m_doneq && !mpt->m_in_callback) {
8723 mptsas_cmd_t *cmd, *next;
8724 struct scsi_pkt *pkt;
8726 mpt->m_in_callback = 1;
8727 cmd = mpt->m_doneq;
8728 mpt->m_doneq = NULL;
8729 mpt->m_donetail = &mpt->m_doneq;
8730 mpt->m_doneq_len = 0;
8732 mutex_exit(&mpt->m_mutex);
8734 * run the completion routines of all the
8735 * completed commands
8737 while (cmd != NULL) {
8738 next = cmd->cmd_linkp;
8739 cmd->cmd_linkp = NULL;
8740 /* run this command's completion routine */
8741 cmd->cmd_flags |= CFLAG_COMPLETED;
8742 pkt = CMD2PKT(cmd);
8743 mptsas_pkt_comp(pkt, cmd);
8744 cmd = next;
8746 mutex_enter(&mpt->m_mutex);
8747 mpt->m_in_callback = 0;
8752 * These routines manipulate the target's queue of pending requests
8754 void
8755 mptsas_waitq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8757 NDBG7(("mptsas_waitq_add: cmd=0x%p", (void *)cmd));
8758 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8759 cmd->cmd_queued = TRUE;
8760 if (ptgt)
8761 ptgt->m_t_nwait++;
8762 if (cmd->cmd_pkt_flags & FLAG_HEAD) {
8763 if ((cmd->cmd_linkp = mpt->m_waitq) == NULL) {
8764 mpt->m_waitqtail = &cmd->cmd_linkp;
8766 mpt->m_waitq = cmd;
8767 } else {
8768 cmd->cmd_linkp = NULL;
8769 *(mpt->m_waitqtail) = cmd;
8770 mpt->m_waitqtail = &cmd->cmd_linkp;
8774 static mptsas_cmd_t *
8775 mptsas_waitq_rm(mptsas_t *mpt)
8777 mptsas_cmd_t *cmd;
8778 mptsas_target_t *ptgt;
8779 NDBG7(("mptsas_waitq_rm"));
8781 MPTSAS_WAITQ_RM(mpt, cmd);
8783 NDBG7(("mptsas_waitq_rm: cmd=0x%p", (void *)cmd));
8784 if (cmd) {
8785 ptgt = cmd->cmd_tgt_addr;
8786 if (ptgt) {
8787 ptgt->m_t_nwait--;
8788 ASSERT(ptgt->m_t_nwait >= 0);
8791 return (cmd);
8795 * remove specified cmd from the middle of the wait queue.
8797 static void
8798 mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd)
8800 mptsas_cmd_t *prevp = mpt->m_waitq;
8801 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8803 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8804 (void *)mpt, (void *)cmd));
8805 if (ptgt) {
8806 ptgt->m_t_nwait--;
8807 ASSERT(ptgt->m_t_nwait >= 0);
8810 if (prevp == cmd) {
8811 if ((mpt->m_waitq = cmd->cmd_linkp) == NULL)
8812 mpt->m_waitqtail = &mpt->m_waitq;
8814 cmd->cmd_linkp = NULL;
8815 cmd->cmd_queued = FALSE;
8816 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8817 (void *)mpt, (void *)cmd));
8818 return;
8821 while (prevp != NULL) {
8822 if (prevp->cmd_linkp == cmd) {
8823 if ((prevp->cmd_linkp = cmd->cmd_linkp) == NULL)
8824 mpt->m_waitqtail = &prevp->cmd_linkp;
8826 cmd->cmd_linkp = NULL;
8827 cmd->cmd_queued = FALSE;
8828 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8829 (void *)mpt, (void *)cmd));
8830 return;
8832 prevp = prevp->cmd_linkp;
8834 cmn_err(CE_PANIC, "mpt: mptsas_waitq_delete: queue botch");
8837 static mptsas_cmd_t *
8838 mptsas_tx_waitq_rm(mptsas_t *mpt)
8840 mptsas_cmd_t *cmd;
8841 NDBG7(("mptsas_tx_waitq_rm"));
8843 MPTSAS_TX_WAITQ_RM(mpt, cmd);
8845 NDBG7(("mptsas_tx_waitq_rm: cmd=0x%p", (void *)cmd));
8847 return (cmd);
8851 * remove specified cmd from the middle of the tx_waitq.
8853 static void
8854 mptsas_tx_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd)
8856 mptsas_cmd_t *prevp = mpt->m_tx_waitq;
8858 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8859 (void *)mpt, (void *)cmd));
8861 if (prevp == cmd) {
8862 if ((mpt->m_tx_waitq = cmd->cmd_linkp) == NULL)
8863 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
8865 cmd->cmd_linkp = NULL;
8866 cmd->cmd_queued = FALSE;
8867 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8868 (void *)mpt, (void *)cmd));
8869 return;
8872 while (prevp != NULL) {
8873 if (prevp->cmd_linkp == cmd) {
8874 if ((prevp->cmd_linkp = cmd->cmd_linkp) == NULL)
8875 mpt->m_tx_waitqtail = &prevp->cmd_linkp;
8877 cmd->cmd_linkp = NULL;
8878 cmd->cmd_queued = FALSE;
8879 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8880 (void *)mpt, (void *)cmd));
8881 return;
8883 prevp = prevp->cmd_linkp;
8885 cmn_err(CE_PANIC, "mpt: mptsas_tx_waitq_delete: queue botch");
8889 * device and bus reset handling
8891 * Notes:
8892 * - RESET_ALL: reset the controller
8893 * - RESET_TARGET: reset the target specified in scsi_address
8895 static int
8896 mptsas_scsi_reset(struct scsi_address *ap, int level)
8898 mptsas_t *mpt = ADDR2MPT(ap);
8899 int rval;
8900 mptsas_tgt_private_t *tgt_private;
8901 mptsas_target_t *ptgt = NULL;
8903 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->tran_tgt_private;
8904 ptgt = tgt_private->t_private;
8905 if (ptgt == NULL) {
8906 return (FALSE);
8908 NDBG22(("mptsas_scsi_reset: target=%d level=%d", ptgt->m_devhdl,
8909 level));
8911 mutex_enter(&mpt->m_mutex);
8913 * if we are not in panic set up a reset delay for this target
8915 if (!ddi_in_panic()) {
8916 mptsas_setup_bus_reset_delay(mpt);
8917 } else {
8918 drv_usecwait(mpt->m_scsi_reset_delay * 1000);
8920 rval = mptsas_do_scsi_reset(mpt, ptgt->m_devhdl);
8921 mutex_exit(&mpt->m_mutex);
8924 * The transport layer expect to only see TRUE and
8925 * FALSE. Therefore, we will adjust the return value
8926 * if mptsas_do_scsi_reset returns FAILED.
8928 if (rval == FAILED)
8929 rval = FALSE;
8930 return (rval);
8933 static int
8934 mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl)
8936 int rval = FALSE;
8937 uint8_t config, disk;
8939 ASSERT(mutex_owned(&mpt->m_mutex));
8941 if (mptsas_debug_resets) {
8942 mptsas_log(mpt, CE_WARN, "mptsas_do_scsi_reset: target=%d",
8943 devhdl);
8947 * Issue a Target Reset message to the target specified but not to a
8948 * disk making up a raid volume. Just look through the RAID config
8949 * Phys Disk list of DevHandles. If the target's DevHandle is in this
8950 * list, then don't reset this target.
8952 for (config = 0; config < mpt->m_num_raid_configs; config++) {
8953 for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
8954 if (devhdl == mpt->m_raidconfig[config].
8955 m_physdisk_devhdl[disk]) {
8956 return (TRUE);
8961 rval = mptsas_ioc_task_management(mpt,
8962 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, devhdl, 0, NULL, 0, 0);
8964 mptsas_doneq_empty(mpt);
8965 return (rval);
8968 static int
8969 mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
8970 void (*callback)(caddr_t), caddr_t arg)
8972 mptsas_t *mpt = ADDR2MPT(ap);
8974 NDBG22(("mptsas_scsi_reset_notify: tgt=%d", ap->a_target));
8976 return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
8977 &mpt->m_mutex, &mpt->m_reset_notify_listf));
8980 static int
8981 mptsas_get_name(struct scsi_device *sd, char *name, int len)
8983 dev_info_t *lun_dip = NULL;
8985 ASSERT(sd != NULL);
8986 ASSERT(name != NULL);
8987 lun_dip = sd->sd_dev;
8988 ASSERT(lun_dip != NULL);
8990 if (mptsas_name_child(lun_dip, name, len) == DDI_SUCCESS) {
8991 return (1);
8992 } else {
8993 return (0);
8997 static int
8998 mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len)
9000 return (mptsas_get_name(sd, name, len));
9003 void
9004 mptsas_set_throttle(mptsas_t *mpt, mptsas_target_t *ptgt, int what)
9007 NDBG25(("mptsas_set_throttle: throttle=%x", what));
9010 * if the bus is draining/quiesced, no changes to the throttles
9011 * are allowed. Not allowing change of throttles during draining
9012 * limits error recovery but will reduce draining time
9014 * all throttles should have been set to HOLD_THROTTLE
9016 if (mpt->m_softstate & (MPTSAS_SS_QUIESCED | MPTSAS_SS_DRAINING)) {
9017 return;
9020 if (what == HOLD_THROTTLE) {
9021 ptgt->m_t_throttle = HOLD_THROTTLE;
9022 } else if (ptgt->m_reset_delay == 0) {
9023 ptgt->m_t_throttle = what;
9028 * Clean up from a device reset.
9029 * For the case of target reset, this function clears the waitq of all
9030 * commands for a particular target. For the case of abort task set, this
9031 * function clears the waitq of all commonds for a particular target/lun.
9033 static void
9034 mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun, uint8_t tasktype)
9036 mptsas_slots_t *slots = mpt->m_active;
9037 mptsas_cmd_t *cmd, *next_cmd;
9038 int slot;
9039 uchar_t reason;
9040 uint_t stat;
9041 hrtime_t timestamp;
9043 NDBG25(("mptsas_flush_target: target=%d lun=%d", target, lun));
9045 timestamp = gethrtime();
9048 * Make sure the I/O Controller has flushed all cmds
9049 * that are associated with this target for a target reset
9050 * and target/lun for abort task set.
9051 * Account for TM requests, which use the last SMID.
9053 for (slot = 0; slot <= mpt->m_active->m_n_normal; slot++) {
9054 if ((cmd = slots->m_slot[slot]) == NULL)
9055 continue;
9056 reason = CMD_RESET;
9057 stat = STAT_DEV_RESET;
9058 switch (tasktype) {
9059 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9060 if (Tgt(cmd) == target) {
9061 if (cmd->cmd_active_expiration <= timestamp) {
9063 * When timeout requested, propagate
9064 * proper reason and statistics to
9065 * target drivers.
9067 reason = CMD_TIMEOUT;
9068 stat |= STAT_TIMEOUT;
9070 NDBG25(("mptsas_flush_target discovered non-"
9071 "NULL cmd in slot %d, tasktype 0x%x", slot,
9072 tasktype));
9073 mptsas_dump_cmd(mpt, cmd);
9074 mptsas_remove_cmd(mpt, cmd);
9075 mptsas_set_pkt_reason(mpt, cmd, reason, stat);
9076 mptsas_doneq_add(mpt, cmd);
9078 break;
9079 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9080 reason = CMD_ABORTED;
9081 stat = STAT_ABORTED;
9082 /*FALLTHROUGH*/
9083 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9084 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9086 NDBG25(("mptsas_flush_target discovered non-"
9087 "NULL cmd in slot %d, tasktype 0x%x", slot,
9088 tasktype));
9089 mptsas_dump_cmd(mpt, cmd);
9090 mptsas_remove_cmd(mpt, cmd);
9091 mptsas_set_pkt_reason(mpt, cmd, reason,
9092 stat);
9093 mptsas_doneq_add(mpt, cmd);
9095 break;
9096 default:
9097 break;
9102 * Flush the waitq and tx_waitq of this target's cmds
9104 cmd = mpt->m_waitq;
9106 reason = CMD_RESET;
9107 stat = STAT_DEV_RESET;
9109 switch (tasktype) {
9110 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9111 while (cmd != NULL) {
9112 next_cmd = cmd->cmd_linkp;
9113 if (Tgt(cmd) == target) {
9114 mptsas_waitq_delete(mpt, cmd);
9115 mptsas_set_pkt_reason(mpt, cmd,
9116 reason, stat);
9117 mptsas_doneq_add(mpt, cmd);
9119 cmd = next_cmd;
9121 mutex_enter(&mpt->m_tx_waitq_mutex);
9122 cmd = mpt->m_tx_waitq;
9123 while (cmd != NULL) {
9124 next_cmd = cmd->cmd_linkp;
9125 if (Tgt(cmd) == target) {
9126 mptsas_tx_waitq_delete(mpt, cmd);
9127 mutex_exit(&mpt->m_tx_waitq_mutex);
9128 mptsas_set_pkt_reason(mpt, cmd,
9129 reason, stat);
9130 mptsas_doneq_add(mpt, cmd);
9131 mutex_enter(&mpt->m_tx_waitq_mutex);
9133 cmd = next_cmd;
9135 mutex_exit(&mpt->m_tx_waitq_mutex);
9136 break;
9137 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9138 reason = CMD_ABORTED;
9139 stat = STAT_ABORTED;
9140 /*FALLTHROUGH*/
9141 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9142 while (cmd != NULL) {
9143 next_cmd = cmd->cmd_linkp;
9144 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9145 mptsas_waitq_delete(mpt, cmd);
9146 mptsas_set_pkt_reason(mpt, cmd,
9147 reason, stat);
9148 mptsas_doneq_add(mpt, cmd);
9150 cmd = next_cmd;
9152 mutex_enter(&mpt->m_tx_waitq_mutex);
9153 cmd = mpt->m_tx_waitq;
9154 while (cmd != NULL) {
9155 next_cmd = cmd->cmd_linkp;
9156 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9157 mptsas_tx_waitq_delete(mpt, cmd);
9158 mutex_exit(&mpt->m_tx_waitq_mutex);
9159 mptsas_set_pkt_reason(mpt, cmd,
9160 reason, stat);
9161 mptsas_doneq_add(mpt, cmd);
9162 mutex_enter(&mpt->m_tx_waitq_mutex);
9164 cmd = next_cmd;
9166 mutex_exit(&mpt->m_tx_waitq_mutex);
9167 break;
9168 default:
9169 mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
9170 tasktype);
9171 break;
9176 * Clean up hba state, abort all outstanding command and commands in waitq
9177 * reset timeout of all targets.
9179 static void
9180 mptsas_flush_hba(mptsas_t *mpt)
9182 mptsas_slots_t *slots = mpt->m_active;
9183 mptsas_cmd_t *cmd;
9184 int slot;
9186 NDBG25(("mptsas_flush_hba"));
9189 * The I/O Controller should have already sent back
9190 * all commands via the scsi I/O reply frame. Make
9191 * sure all commands have been flushed.
9192 * Account for TM request, which use the last SMID.
9194 for (slot = 0; slot <= mpt->m_active->m_n_normal; slot++) {
9195 if ((cmd = slots->m_slot[slot]) == NULL)
9196 continue;
9198 if (cmd->cmd_flags & CFLAG_CMDIOC) {
9200 * Need to make sure to tell everyone that might be
9201 * waiting on this command that it's going to fail. If
9202 * we get here, this command will never timeout because
9203 * the active command table is going to be re-allocated,
9204 * so there will be nothing to check against a time out.
9205 * Instead, mark the command as failed due to reset.
9207 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
9208 STAT_BUS_RESET);
9209 if ((cmd->cmd_flags &
9210 (CFLAG_PASSTHRU | CFLAG_CONFIG | CFLAG_FW_DIAG))) {
9211 cmd->cmd_flags |= CFLAG_FINISHED;
9212 cv_broadcast(&mpt->m_passthru_cv);
9213 cv_broadcast(&mpt->m_config_cv);
9214 cv_broadcast(&mpt->m_fw_diag_cv);
9216 continue;
9219 NDBG25(("mptsas_flush_hba discovered non-NULL cmd in slot %d",
9220 slot));
9221 mptsas_dump_cmd(mpt, cmd);
9223 mptsas_remove_cmd(mpt, cmd);
9224 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9225 mptsas_doneq_add(mpt, cmd);
9229 * Flush the waitq.
9231 while ((cmd = mptsas_waitq_rm(mpt)) != NULL) {
9232 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9233 if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
9234 (cmd->cmd_flags & CFLAG_CONFIG) ||
9235 (cmd->cmd_flags & CFLAG_FW_DIAG)) {
9236 cmd->cmd_flags |= CFLAG_FINISHED;
9237 cv_broadcast(&mpt->m_passthru_cv);
9238 cv_broadcast(&mpt->m_config_cv);
9239 cv_broadcast(&mpt->m_fw_diag_cv);
9240 } else {
9241 mptsas_doneq_add(mpt, cmd);
9246 * Flush the tx_waitq
9248 mutex_enter(&mpt->m_tx_waitq_mutex);
9249 while ((cmd = mptsas_tx_waitq_rm(mpt)) != NULL) {
9250 mutex_exit(&mpt->m_tx_waitq_mutex);
9251 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9252 mptsas_doneq_add(mpt, cmd);
9253 mutex_enter(&mpt->m_tx_waitq_mutex);
9255 mutex_exit(&mpt->m_tx_waitq_mutex);
9258 * Drain the taskqs prior to reallocating resources.
9260 mutex_exit(&mpt->m_mutex);
9261 ddi_taskq_wait(mpt->m_event_taskq);
9262 ddi_taskq_wait(mpt->m_dr_taskq);
9263 mutex_enter(&mpt->m_mutex);
9267 * set pkt_reason and OR in pkt_statistics flag
9269 static void
9270 mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd, uchar_t reason,
9271 uint_t stat)
9273 #ifndef __lock_lint
9274 _NOTE(ARGUNUSED(mpt))
9275 #endif
9277 NDBG25(("mptsas_set_pkt_reason: cmd=0x%p reason=%x stat=%x",
9278 (void *)cmd, reason, stat));
9280 if (cmd) {
9281 if (cmd->cmd_pkt->pkt_reason == CMD_CMPLT) {
9282 cmd->cmd_pkt->pkt_reason = reason;
9284 cmd->cmd_pkt->pkt_statistics |= stat;
9288 static void
9289 mptsas_start_watch_reset_delay()
9291 NDBG22(("mptsas_start_watch_reset_delay"));
9293 mutex_enter(&mptsas_global_mutex);
9294 if (mptsas_reset_watch == NULL && mptsas_timeouts_enabled) {
9295 mptsas_reset_watch = timeout(mptsas_watch_reset_delay, NULL,
9296 drv_usectohz((clock_t)
9297 MPTSAS_WATCH_RESET_DELAY_TICK * 1000));
9298 ASSERT(mptsas_reset_watch != NULL);
9300 mutex_exit(&mptsas_global_mutex);
9303 static void
9304 mptsas_setup_bus_reset_delay(mptsas_t *mpt)
9306 mptsas_target_t *ptgt = NULL;
9308 ASSERT(MUTEX_HELD(&mpt->m_mutex));
9310 NDBG22(("mptsas_setup_bus_reset_delay"));
9311 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9312 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9313 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
9314 ptgt->m_reset_delay = mpt->m_scsi_reset_delay;
9317 mptsas_start_watch_reset_delay();
9321 * mptsas_watch_reset_delay(_subr) is invoked by timeout() and checks every
9322 * mpt instance for active reset delays
9324 static void
9325 mptsas_watch_reset_delay(void *arg)
9327 #ifndef __lock_lint
9328 _NOTE(ARGUNUSED(arg))
9329 #endif
9331 mptsas_t *mpt;
9332 int not_done = 0;
9334 NDBG22(("mptsas_watch_reset_delay"));
9336 mutex_enter(&mptsas_global_mutex);
9337 mptsas_reset_watch = 0;
9338 mutex_exit(&mptsas_global_mutex);
9339 rw_enter(&mptsas_global_rwlock, RW_READER);
9340 for (mpt = mptsas_head; mpt != NULL; mpt = mpt->m_next) {
9341 if (mpt->m_tran == 0) {
9342 continue;
9344 mutex_enter(&mpt->m_mutex);
9345 not_done += mptsas_watch_reset_delay_subr(mpt);
9346 mutex_exit(&mpt->m_mutex);
9348 rw_exit(&mptsas_global_rwlock);
9350 if (not_done) {
9351 mptsas_start_watch_reset_delay();
9355 static int
9356 mptsas_watch_reset_delay_subr(mptsas_t *mpt)
9358 int done = 0;
9359 int restart = 0;
9360 mptsas_target_t *ptgt = NULL;
9362 NDBG22(("mptsas_watch_reset_delay_subr: mpt=0x%p", (void *)mpt));
9364 ASSERT(mutex_owned(&mpt->m_mutex));
9366 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9367 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9368 if (ptgt->m_reset_delay != 0) {
9369 ptgt->m_reset_delay -=
9370 MPTSAS_WATCH_RESET_DELAY_TICK;
9371 if (ptgt->m_reset_delay <= 0) {
9372 ptgt->m_reset_delay = 0;
9373 mptsas_set_throttle(mpt, ptgt,
9374 MAX_THROTTLE);
9375 restart++;
9376 } else {
9377 done = -1;
9382 if (restart > 0) {
9383 mptsas_restart_hba(mpt);
9385 return (done);
9388 #ifdef MPTSAS_TEST
9389 static void
9390 mptsas_test_reset(mptsas_t *mpt, int target)
9392 mptsas_target_t *ptgt = NULL;
9394 if (mptsas_rtest == target) {
9395 if (mptsas_do_scsi_reset(mpt, target) == TRUE) {
9396 mptsas_rtest = -1;
9398 if (mptsas_rtest == -1) {
9399 NDBG22(("mptsas_test_reset success"));
9403 #endif
9406 * abort handling:
9408 * Notes:
9409 * - if pkt is not NULL, abort just that command
9410 * - if pkt is NULL, abort all outstanding commands for target
9412 static int
9413 mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
9415 mptsas_t *mpt = ADDR2MPT(ap);
9416 int rval;
9417 mptsas_tgt_private_t *tgt_private;
9418 int target, lun;
9420 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
9421 tran_tgt_private;
9422 ASSERT(tgt_private != NULL);
9423 target = tgt_private->t_private->m_devhdl;
9424 lun = tgt_private->t_lun;
9426 NDBG23(("mptsas_scsi_abort: target=%d.%d", target, lun));
9428 mutex_enter(&mpt->m_mutex);
9429 rval = mptsas_do_scsi_abort(mpt, target, lun, pkt);
9430 mutex_exit(&mpt->m_mutex);
9431 return (rval);
9434 static int
9435 mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun, struct scsi_pkt *pkt)
9437 mptsas_cmd_t *sp = NULL;
9438 mptsas_slots_t *slots = mpt->m_active;
9439 int rval = FALSE;
9441 ASSERT(mutex_owned(&mpt->m_mutex));
9444 * Abort the command pkt on the target/lun in ap. If pkt is
9445 * NULL, abort all outstanding commands on that target/lun.
9446 * If you can abort them, return 1, else return 0.
9447 * Each packet that's aborted should be sent back to the target
9448 * driver through the callback routine, with pkt_reason set to
9449 * CMD_ABORTED.
9451 * abort cmd pkt on HBA hardware; clean out of outstanding
9452 * command lists, etc.
9454 if (pkt != NULL) {
9455 /* abort the specified packet */
9456 sp = PKT2CMD(pkt);
9458 if (sp->cmd_queued) {
9459 NDBG23(("mptsas_do_scsi_abort: queued sp=0x%p aborted",
9460 (void *)sp));
9461 mptsas_waitq_delete(mpt, sp);
9462 mptsas_set_pkt_reason(mpt, sp, CMD_ABORTED,
9463 STAT_ABORTED);
9464 mptsas_doneq_add(mpt, sp);
9465 rval = TRUE;
9466 goto done;
9470 * Have mpt firmware abort this command
9473 if (slots->m_slot[sp->cmd_slot] != NULL) {
9474 rval = mptsas_ioc_task_management(mpt,
9475 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, target,
9476 lun, NULL, 0, 0);
9479 * The transport layer expects only TRUE and FALSE.
9480 * Therefore, if mptsas_ioc_task_management returns
9481 * FAILED we will return FALSE.
9483 if (rval == FAILED)
9484 rval = FALSE;
9485 goto done;
9490 * If pkt is NULL then abort task set
9492 rval = mptsas_ioc_task_management(mpt,
9493 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, target, lun, NULL, 0, 0);
9496 * The transport layer expects only TRUE and FALSE.
9497 * Therefore, if mptsas_ioc_task_management returns
9498 * FAILED we will return FALSE.
9500 if (rval == FAILED)
9501 rval = FALSE;
9503 #ifdef MPTSAS_TEST
9504 if (rval && mptsas_test_stop) {
9505 debug_enter("mptsas_do_scsi_abort");
9507 #endif
9509 done:
9510 mptsas_doneq_empty(mpt);
9511 return (rval);
9515 * capability handling:
9516 * (*tran_getcap). Get the capability named, and return its value.
9518 static int
9519 mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly)
9521 mptsas_t *mpt = ADDR2MPT(ap);
9522 int ckey;
9523 int rval = FALSE;
9525 NDBG24(("mptsas_scsi_getcap: target=%d, cap=%s tgtonly=%x",
9526 ap->a_target, cap, tgtonly));
9528 mutex_enter(&mpt->m_mutex);
9530 if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9531 mutex_exit(&mpt->m_mutex);
9532 return (UNDEFINED);
9535 switch (ckey) {
9536 case SCSI_CAP_DMA_MAX:
9537 rval = (int)mpt->m_msg_dma_attr.dma_attr_maxxfer;
9538 break;
9539 case SCSI_CAP_ARQ:
9540 rval = TRUE;
9541 break;
9542 case SCSI_CAP_MSG_OUT:
9543 case SCSI_CAP_PARITY:
9544 case SCSI_CAP_UNTAGGED_QING:
9545 rval = TRUE;
9546 break;
9547 case SCSI_CAP_TAGGED_QING:
9548 rval = TRUE;
9549 break;
9550 case SCSI_CAP_RESET_NOTIFICATION:
9551 rval = TRUE;
9552 break;
9553 case SCSI_CAP_LINKED_CMDS:
9554 rval = FALSE;
9555 break;
9556 case SCSI_CAP_QFULL_RETRIES:
9557 rval = ((mptsas_tgt_private_t *)(ap->a_hba_tran->
9558 tran_tgt_private))->t_private->m_qfull_retries;
9559 break;
9560 case SCSI_CAP_QFULL_RETRY_INTERVAL:
9561 rval = drv_hztousec(((mptsas_tgt_private_t *)
9562 (ap->a_hba_tran->tran_tgt_private))->
9563 t_private->m_qfull_retry_interval) / 1000;
9564 break;
9565 case SCSI_CAP_CDB_LEN:
9566 rval = CDB_GROUP4;
9567 break;
9568 case SCSI_CAP_INTERCONNECT_TYPE:
9569 rval = INTERCONNECT_SAS;
9570 break;
9571 case SCSI_CAP_TRAN_LAYER_RETRIES:
9572 if (mpt->m_ioc_capabilities &
9573 MPI2_IOCFACTS_CAPABILITY_TLR)
9574 rval = TRUE;
9575 else
9576 rval = FALSE;
9577 break;
9578 default:
9579 rval = UNDEFINED;
9580 break;
9583 NDBG24(("mptsas_scsi_getcap: %s, rval=%x", cap, rval));
9585 mutex_exit(&mpt->m_mutex);
9586 return (rval);
9590 * (*tran_setcap). Set the capability named to the value given.
9592 static int
9593 mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value, int tgtonly)
9595 mptsas_t *mpt = ADDR2MPT(ap);
9596 int ckey;
9597 int rval = FALSE;
9599 NDBG24(("mptsas_scsi_setcap: target=%d, cap=%s value=%x tgtonly=%x",
9600 ap->a_target, cap, value, tgtonly));
9602 if (!tgtonly) {
9603 return (rval);
9606 mutex_enter(&mpt->m_mutex);
9608 if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9609 mutex_exit(&mpt->m_mutex);
9610 return (UNDEFINED);
9613 switch (ckey) {
9614 case SCSI_CAP_DMA_MAX:
9615 case SCSI_CAP_MSG_OUT:
9616 case SCSI_CAP_PARITY:
9617 case SCSI_CAP_INITIATOR_ID:
9618 case SCSI_CAP_LINKED_CMDS:
9619 case SCSI_CAP_UNTAGGED_QING:
9620 case SCSI_CAP_RESET_NOTIFICATION:
9622 * None of these are settable via
9623 * the capability interface.
9625 break;
9626 case SCSI_CAP_ARQ:
9628 * We cannot turn off arq so return false if asked to
9630 if (value) {
9631 rval = TRUE;
9632 } else {
9633 rval = FALSE;
9635 break;
9636 case SCSI_CAP_TAGGED_QING:
9637 mptsas_set_throttle(mpt, ((mptsas_tgt_private_t *)
9638 (ap->a_hba_tran->tran_tgt_private))->t_private,
9639 MAX_THROTTLE);
9640 rval = TRUE;
9641 break;
9642 case SCSI_CAP_QFULL_RETRIES:
9643 ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9644 t_private->m_qfull_retries = (uchar_t)value;
9645 rval = TRUE;
9646 break;
9647 case SCSI_CAP_QFULL_RETRY_INTERVAL:
9648 ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9649 t_private->m_qfull_retry_interval =
9650 drv_usectohz(value * 1000);
9651 rval = TRUE;
9652 break;
9653 default:
9654 rval = UNDEFINED;
9655 break;
9657 mutex_exit(&mpt->m_mutex);
9658 return (rval);
9662 * Utility routine for mptsas_ifsetcap/ifgetcap
9664 /*ARGSUSED*/
9665 static int
9666 mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp)
9668 NDBG24(("mptsas_scsi_capchk: cap=%s", cap));
9670 if (!cap)
9671 return (FALSE);
9673 *cidxp = scsi_hba_lookup_capstr(cap);
9674 return (TRUE);
9677 static int
9678 mptsas_alloc_active_slots(mptsas_t *mpt, int flag)
9680 mptsas_slots_t *old_active = mpt->m_active;
9681 mptsas_slots_t *new_active;
9682 size_t size;
9685 * if there are active commands, then we cannot
9686 * change size of active slots array.
9688 ASSERT(mpt->m_ncmds == 0);
9690 size = MPTSAS_SLOTS_SIZE(mpt);
9691 new_active = kmem_zalloc(size, flag);
9692 if (new_active == NULL) {
9693 NDBG1(("new active alloc failed"));
9694 return (-1);
9697 * Since SMID 0 is reserved and the TM slot is reserved, the
9698 * number of slots that can be used at any one time is
9699 * m_max_requests - 2.
9701 new_active->m_n_normal = (mpt->m_max_requests - 2);
9702 new_active->m_size = size;
9703 new_active->m_rotor = 1;
9704 if (old_active)
9705 mptsas_free_active_slots(mpt);
9706 mpt->m_active = new_active;
9708 return (0);
9711 static void
9712 mptsas_free_active_slots(mptsas_t *mpt)
9714 mptsas_slots_t *active = mpt->m_active;
9715 size_t size;
9717 if (active == NULL)
9718 return;
9719 size = active->m_size;
9720 kmem_free(active, size);
9721 mpt->m_active = NULL;
9725 * Error logging, printing, and debug print routines.
9727 static char *mptsas_label = "mpt_sas";
9729 /*PRINTFLIKE3*/
9730 void
9731 mptsas_log(mptsas_t *mpt, int level, char *fmt, ...)
9733 dev_info_t *dev;
9734 va_list ap;
9736 if (mpt) {
9737 dev = mpt->m_dip;
9738 } else {
9739 dev = 0;
9742 mutex_enter(&mptsas_log_mutex);
9744 va_start(ap, fmt);
9745 (void) vsprintf(mptsas_log_buf, fmt, ap);
9746 va_end(ap);
9748 if (level == CE_CONT) {
9749 scsi_log(dev, mptsas_label, level, "%s\n", mptsas_log_buf);
9750 } else {
9751 scsi_log(dev, mptsas_label, level, "%s", mptsas_log_buf);
9754 mutex_exit(&mptsas_log_mutex);
9757 #ifdef MPTSAS_DEBUG
9759 * Use a circular buffer to log messages to private memory.
9760 * Increment idx atomically to minimize risk to miss lines.
9761 * It's fast and does not hold up the proceedings too much.
9763 static const size_t mptsas_dbglog_linecnt = MPTSAS_DBGLOG_LINECNT;
9764 static const size_t mptsas_dbglog_linelen = MPTSAS_DBGLOG_LINELEN;
9765 static char mptsas_dbglog_bufs[MPTSAS_DBGLOG_LINECNT][MPTSAS_DBGLOG_LINELEN];
9766 static uint32_t mptsas_dbglog_idx = 0;
9768 /*PRINTFLIKE1*/
9769 void
9770 mptsas_debug_log(char *fmt, ...)
9772 va_list ap;
9773 uint32_t idx;
9775 idx = atomic_inc_32_nv(&mptsas_dbglog_idx) &
9776 (mptsas_dbglog_linecnt - 1);
9778 va_start(ap, fmt);
9779 (void) vsnprintf(mptsas_dbglog_bufs[idx],
9780 mptsas_dbglog_linelen, fmt, ap);
9781 va_end(ap);
9784 /*PRINTFLIKE1*/
9785 void
9786 mptsas_printf(char *fmt, ...)
9788 dev_info_t *dev = 0;
9789 va_list ap;
9791 mutex_enter(&mptsas_log_mutex);
9793 va_start(ap, fmt);
9794 (void) vsprintf(mptsas_log_buf, fmt, ap);
9795 va_end(ap);
9797 #ifdef PROM_PRINTF
9798 prom_printf("%s:\t%s\n", mptsas_label, mptsas_log_buf);
9799 #else
9800 scsi_log(dev, mptsas_label, CE_CONT, "!%s\n", mptsas_log_buf);
9801 #endif
9802 mutex_exit(&mptsas_log_mutex);
9804 #endif
9807 * timeout handling
9809 static void
9810 mptsas_watch(void *arg)
9812 #ifndef __lock_lint
9813 _NOTE(ARGUNUSED(arg))
9814 #endif
9816 mptsas_t *mpt;
9817 uint32_t doorbell;
9819 NDBG30(("mptsas_watch"));
9821 rw_enter(&mptsas_global_rwlock, RW_READER);
9822 for (mpt = mptsas_head; mpt != (mptsas_t *)NULL; mpt = mpt->m_next) {
9824 mutex_enter(&mpt->m_mutex);
9826 /* Skip device if not powered on */
9827 if (mpt->m_options & MPTSAS_OPT_PM) {
9828 if (mpt->m_power_level == PM_LEVEL_D0) {
9829 (void) pm_busy_component(mpt->m_dip, 0);
9830 mpt->m_busy = 1;
9831 } else {
9832 mutex_exit(&mpt->m_mutex);
9833 continue;
9838 * Check if controller is in a FAULT state. If so, reset it.
9840 doorbell = ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell);
9841 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
9842 doorbell &= MPI2_DOORBELL_DATA_MASK;
9843 mptsas_log(mpt, CE_WARN, "MPT Firmware Fault, "
9844 "code: %04x", doorbell);
9845 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
9846 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
9847 mptsas_log(mpt, CE_WARN, "Reset failed"
9848 "after fault was detected");
9853 * For now, always call mptsas_watchsubr.
9855 mptsas_watchsubr(mpt);
9857 if (mpt->m_options & MPTSAS_OPT_PM) {
9858 mpt->m_busy = 0;
9859 (void) pm_idle_component(mpt->m_dip, 0);
9862 mutex_exit(&mpt->m_mutex);
9864 rw_exit(&mptsas_global_rwlock);
9866 mutex_enter(&mptsas_global_mutex);
9867 if (mptsas_timeouts_enabled)
9868 mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
9869 mutex_exit(&mptsas_global_mutex);
9872 static void
9873 mptsas_watchsubr(mptsas_t *mpt)
9875 int i;
9876 mptsas_cmd_t *cmd;
9877 mptsas_target_t *ptgt = NULL;
9878 hrtime_t timestamp = gethrtime();
9880 ASSERT(MUTEX_HELD(&mpt->m_mutex));
9882 NDBG30(("mptsas_watchsubr: mpt=0x%p", (void *)mpt));
9884 #ifdef MPTSAS_TEST
9885 if (mptsas_enable_untagged) {
9886 mptsas_test_untagged++;
9888 #endif
9891 * Check for commands stuck in active slot
9892 * Account for TM requests, which use the last SMID.
9894 for (i = 0; i <= mpt->m_active->m_n_normal; i++) {
9895 if ((cmd = mpt->m_active->m_slot[i]) != NULL) {
9896 if (cmd->cmd_active_expiration <= timestamp) {
9897 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
9899 * There seems to be a command stuck
9900 * in the active slot. Drain throttle.
9902 mptsas_set_throttle(mpt,
9903 cmd->cmd_tgt_addr,
9904 DRAIN_THROTTLE);
9905 } else if (cmd->cmd_flags &
9906 (CFLAG_PASSTHRU | CFLAG_CONFIG |
9907 CFLAG_FW_DIAG)) {
9909 * passthrough command timeout
9911 cmd->cmd_flags |= (CFLAG_FINISHED |
9912 CFLAG_TIMEOUT);
9913 cv_broadcast(&mpt->m_passthru_cv);
9914 cv_broadcast(&mpt->m_config_cv);
9915 cv_broadcast(&mpt->m_fw_diag_cv);
9921 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9922 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9924 * If we were draining due to a qfull condition,
9925 * go back to full throttle.
9927 if ((ptgt->m_t_throttle < MAX_THROTTLE) &&
9928 (ptgt->m_t_throttle > HOLD_THROTTLE) &&
9929 (ptgt->m_t_ncmds < ptgt->m_t_throttle)) {
9930 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
9931 mptsas_restart_hba(mpt);
9934 cmd = TAILQ_LAST(&ptgt->m_active_cmdq, mptsas_active_cmdq);
9935 if (cmd == NULL)
9936 continue;
9938 if (cmd->cmd_active_expiration <= timestamp) {
9940 * Earliest command timeout expired. Drain throttle.
9942 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
9945 * Check for remaining commands.
9947 cmd = TAILQ_FIRST(&ptgt->m_active_cmdq);
9948 if (cmd->cmd_active_expiration > timestamp) {
9950 * Wait for remaining commands to complete or
9951 * time out.
9953 NDBG23(("command timed out, pending drain"));
9954 continue;
9958 * All command timeouts expired.
9960 mptsas_log(mpt, CE_NOTE, "Timeout of %d seconds "
9961 "expired with %d commands on target %d lun %d.",
9962 cmd->cmd_pkt->pkt_time, ptgt->m_t_ncmds,
9963 ptgt->m_devhdl, Lun(cmd));
9965 mptsas_cmd_timeout(mpt, ptgt);
9966 } else if (cmd->cmd_active_expiration <=
9967 timestamp + (hrtime_t)mptsas_scsi_watchdog_tick * NANOSEC) {
9968 NDBG23(("pending timeout"));
9969 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
9975 * timeout recovery
9977 static void
9978 mptsas_cmd_timeout(mptsas_t *mpt, mptsas_target_t *ptgt)
9980 uint16_t devhdl;
9981 uint64_t sas_wwn;
9982 uint8_t phy;
9983 char wwn_str[MPTSAS_WWN_STRLEN];
9985 devhdl = ptgt->m_devhdl;
9986 sas_wwn = ptgt->m_addr.mta_wwn;
9987 phy = ptgt->m_phynum;
9988 if (sas_wwn == 0) {
9989 (void) sprintf(wwn_str, "p%x", phy);
9990 } else {
9991 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
9994 NDBG29(("mptsas_cmd_timeout: target=%d", devhdl));
9995 mptsas_log(mpt, CE_WARN, "Disconnected command timeout for "
9996 "target %d %s, enclosure %u", devhdl, wwn_str,
9997 ptgt->m_enclosure);
10000 * Abort all outstanding commands on the device.
10002 NDBG29(("mptsas_cmd_timeout: device reset"));
10003 if (mptsas_do_scsi_reset(mpt, devhdl) != TRUE) {
10004 mptsas_log(mpt, CE_WARN, "Target %d reset for command timeout "
10005 "recovery failed!", devhdl);
10010 * Device / Hotplug control
10012 static int
10013 mptsas_scsi_quiesce(dev_info_t *dip)
10015 mptsas_t *mpt;
10016 scsi_hba_tran_t *tran;
10018 tran = ddi_get_driver_private(dip);
10019 if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10020 return (-1);
10022 return (mptsas_quiesce_bus(mpt));
10025 static int
10026 mptsas_scsi_unquiesce(dev_info_t *dip)
10028 mptsas_t *mpt;
10029 scsi_hba_tran_t *tran;
10031 tran = ddi_get_driver_private(dip);
10032 if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10033 return (-1);
10035 return (mptsas_unquiesce_bus(mpt));
10038 static int
10039 mptsas_quiesce_bus(mptsas_t *mpt)
10041 mptsas_target_t *ptgt = NULL;
10043 NDBG28(("mptsas_quiesce_bus"));
10044 mutex_enter(&mpt->m_mutex);
10046 /* Set all the throttles to zero */
10047 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10048 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10049 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10052 /* If there are any outstanding commands in the queue */
10053 if (mpt->m_ncmds) {
10054 mpt->m_softstate |= MPTSAS_SS_DRAINING;
10055 mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10056 mpt, (MPTSAS_QUIESCE_TIMEOUT * drv_usectohz(1000000)));
10057 if (cv_wait_sig(&mpt->m_cv, &mpt->m_mutex) == 0) {
10059 * Quiesce has been interrupted
10061 mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10062 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10063 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10064 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10066 mptsas_restart_hba(mpt);
10067 if (mpt->m_quiesce_timeid != 0) {
10068 timeout_id_t tid = mpt->m_quiesce_timeid;
10069 mpt->m_quiesce_timeid = 0;
10070 mutex_exit(&mpt->m_mutex);
10071 (void) untimeout(tid);
10072 return (-1);
10074 mutex_exit(&mpt->m_mutex);
10075 return (-1);
10076 } else {
10077 /* Bus has been quiesced */
10078 ASSERT(mpt->m_quiesce_timeid == 0);
10079 mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10080 mpt->m_softstate |= MPTSAS_SS_QUIESCED;
10081 mutex_exit(&mpt->m_mutex);
10082 return (0);
10085 /* Bus was not busy - QUIESCED */
10086 mutex_exit(&mpt->m_mutex);
10088 return (0);
10091 static int
10092 mptsas_unquiesce_bus(mptsas_t *mpt)
10094 mptsas_target_t *ptgt = NULL;
10096 NDBG28(("mptsas_unquiesce_bus"));
10097 mutex_enter(&mpt->m_mutex);
10098 mpt->m_softstate &= ~MPTSAS_SS_QUIESCED;
10099 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10100 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10101 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10103 mptsas_restart_hba(mpt);
10104 mutex_exit(&mpt->m_mutex);
10105 return (0);
10108 static void
10109 mptsas_ncmds_checkdrain(void *arg)
10111 mptsas_t *mpt = arg;
10112 mptsas_target_t *ptgt = NULL;
10114 mutex_enter(&mpt->m_mutex);
10115 if (mpt->m_softstate & MPTSAS_SS_DRAINING) {
10116 mpt->m_quiesce_timeid = 0;
10117 if (mpt->m_ncmds == 0) {
10118 /* Command queue has been drained */
10119 cv_signal(&mpt->m_cv);
10120 } else {
10122 * The throttle may have been reset because
10123 * of a SCSI bus reset
10125 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10126 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10127 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10130 mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10131 mpt, (MPTSAS_QUIESCE_TIMEOUT *
10132 drv_usectohz(1000000)));
10135 mutex_exit(&mpt->m_mutex);
10138 /*ARGSUSED*/
10139 static void
10140 mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
10142 int i;
10143 uint8_t *cp = (uchar_t *)cmd->cmd_pkt->pkt_cdbp;
10144 char buf[128];
10146 buf[0] = '\0';
10147 NDBG25(("?Cmd (0x%p) dump for Target %d Lun %d:\n", (void *)cmd,
10148 Tgt(cmd), Lun(cmd)));
10149 (void) sprintf(&buf[0], "\tcdb=[");
10150 for (i = 0; i < (int)cmd->cmd_cdblen; i++) {
10151 (void) sprintf(&buf[strlen(buf)], " 0x%x", *cp++);
10153 (void) sprintf(&buf[strlen(buf)], " ]");
10154 NDBG25(("?%s\n", buf));
10155 NDBG25(("?pkt_flags=0x%x pkt_statistics=0x%x pkt_state=0x%x\n",
10156 cmd->cmd_pkt->pkt_flags, cmd->cmd_pkt->pkt_statistics,
10157 cmd->cmd_pkt->pkt_state));
10158 NDBG25(("?pkt_scbp=0x%x cmd_flags=0x%x\n", cmd->cmd_pkt->pkt_scbp ?
10159 *(cmd->cmd_pkt->pkt_scbp) : 0, cmd->cmd_flags));
10162 static void
10163 mptsas_passthru_sge(ddi_acc_handle_t acc_hdl, mptsas_pt_request_t *pt,
10164 pMpi2SGESimple64_t sgep)
10166 uint32_t sge_flags;
10167 uint32_t data_size, dataout_size;
10168 ddi_dma_cookie_t data_cookie;
10169 ddi_dma_cookie_t dataout_cookie;
10171 data_size = pt->data_size;
10172 dataout_size = pt->dataout_size;
10173 data_cookie = pt->data_cookie;
10174 dataout_cookie = pt->dataout_cookie;
10176 if (dataout_size) {
10177 sge_flags = dataout_size |
10178 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10179 MPI2_SGE_FLAGS_END_OF_BUFFER |
10180 MPI2_SGE_FLAGS_HOST_TO_IOC |
10181 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10182 MPI2_SGE_FLAGS_SHIFT);
10183 ddi_put32(acc_hdl, &sgep->FlagsLength, sge_flags);
10184 ddi_put32(acc_hdl, &sgep->Address.Low,
10185 (uint32_t)(dataout_cookie.dmac_laddress &
10186 0xffffffffull));
10187 ddi_put32(acc_hdl, &sgep->Address.High,
10188 (uint32_t)(dataout_cookie.dmac_laddress
10189 >> 32));
10190 sgep++;
10192 sge_flags = data_size;
10193 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10194 MPI2_SGE_FLAGS_LAST_ELEMENT |
10195 MPI2_SGE_FLAGS_END_OF_BUFFER |
10196 MPI2_SGE_FLAGS_END_OF_LIST |
10197 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10198 MPI2_SGE_FLAGS_SHIFT);
10199 if (pt->direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10200 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
10201 MPI2_SGE_FLAGS_SHIFT);
10202 } else {
10203 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
10204 MPI2_SGE_FLAGS_SHIFT);
10206 ddi_put32(acc_hdl, &sgep->FlagsLength,
10207 sge_flags);
10208 ddi_put32(acc_hdl, &sgep->Address.Low,
10209 (uint32_t)(data_cookie.dmac_laddress &
10210 0xffffffffull));
10211 ddi_put32(acc_hdl, &sgep->Address.High,
10212 (uint32_t)(data_cookie.dmac_laddress >> 32));
10215 static void
10216 mptsas_passthru_ieee_sge(ddi_acc_handle_t acc_hdl, mptsas_pt_request_t *pt,
10217 pMpi2IeeeSgeSimple64_t ieeesgep)
10219 uint8_t sge_flags;
10220 uint32_t data_size, dataout_size;
10221 ddi_dma_cookie_t data_cookie;
10222 ddi_dma_cookie_t dataout_cookie;
10224 data_size = pt->data_size;
10225 dataout_size = pt->dataout_size;
10226 data_cookie = pt->data_cookie;
10227 dataout_cookie = pt->dataout_cookie;
10229 sge_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
10230 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
10231 if (dataout_size) {
10232 ddi_put32(acc_hdl, &ieeesgep->Length, dataout_size);
10233 ddi_put32(acc_hdl, &ieeesgep->Address.Low,
10234 (uint32_t)(dataout_cookie.dmac_laddress &
10235 0xffffffffull));
10236 ddi_put32(acc_hdl, &ieeesgep->Address.High,
10237 (uint32_t)(dataout_cookie.dmac_laddress >> 32));
10238 ddi_put8(acc_hdl, &ieeesgep->Flags, sge_flags);
10239 ieeesgep++;
10241 sge_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
10242 ddi_put32(acc_hdl, &ieeesgep->Length, data_size);
10243 ddi_put32(acc_hdl, &ieeesgep->Address.Low,
10244 (uint32_t)(data_cookie.dmac_laddress & 0xffffffffull));
10245 ddi_put32(acc_hdl, &ieeesgep->Address.High,
10246 (uint32_t)(data_cookie.dmac_laddress >> 32));
10247 ddi_put8(acc_hdl, &ieeesgep->Flags, sge_flags);
10250 static void
10251 mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd)
10253 caddr_t memp;
10254 pMPI2RequestHeader_t request_hdrp;
10255 struct scsi_pkt *pkt = cmd->cmd_pkt;
10256 mptsas_pt_request_t *pt = pkt->pkt_ha_private;
10257 uint32_t request_size;
10258 uint32_t i;
10259 uint64_t request_desc = 0;
10260 uint8_t desc_type;
10261 uint16_t SMID;
10262 uint8_t *request, function;
10263 ddi_dma_handle_t dma_hdl = mpt->m_dma_req_frame_hdl;
10264 ddi_acc_handle_t acc_hdl = mpt->m_acc_req_frame_hdl;
10266 desc_type = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
10268 request = pt->request;
10269 request_size = pt->request_size;
10271 SMID = cmd->cmd_slot;
10274 * Store the passthrough message in memory location
10275 * corresponding to our slot number
10277 memp = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
10278 request_hdrp = (pMPI2RequestHeader_t)memp;
10279 bzero(memp, mpt->m_req_frame_size);
10281 for (i = 0; i < request_size; i++) {
10282 bcopy(request + i, memp + i, 1);
10285 NDBG15(("mptsas_start_passthru: Func 0x%x, MsgFlags 0x%x, "
10286 "size=%d, in %d, out %d, SMID %d", request_hdrp->Function,
10287 request_hdrp->MsgFlags, request_size,
10288 pt->data_size, pt->dataout_size, SMID));
10291 * Add an SGE, even if the length is zero.
10293 if (mpt->m_MPI25 && pt->simple == 0) {
10294 mptsas_passthru_ieee_sge(acc_hdl, pt,
10295 (pMpi2IeeeSgeSimple64_t)
10296 ((uint8_t *)request_hdrp + pt->sgl_offset));
10297 } else {
10298 mptsas_passthru_sge(acc_hdl, pt,
10299 (pMpi2SGESimple64_t)
10300 ((uint8_t *)request_hdrp + pt->sgl_offset));
10303 function = request_hdrp->Function;
10304 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10305 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10306 pMpi2SCSIIORequest_t scsi_io_req;
10307 caddr_t arsbuf;
10308 uint8_t ars_size;
10309 uint32_t ars_dmaaddrlow;
10311 NDBG15(("mptsas_start_passthru: Is SCSI IO Req"));
10312 scsi_io_req = (pMpi2SCSIIORequest_t)request_hdrp;
10314 if (cmd->cmd_extrqslen != 0) {
10316 * Mapping of the buffer was done in
10317 * mptsas_do_passthru().
10318 * Calculate the DMA address with the same offset.
10320 arsbuf = cmd->cmd_arq_buf;
10321 ars_size = cmd->cmd_extrqslen;
10322 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
10323 ((uintptr_t)arsbuf - (uintptr_t)mpt->m_req_sense)) &
10324 0xffffffffu;
10325 } else {
10326 arsbuf = mpt->m_req_sense +
10327 (mpt->m_req_sense_size * (SMID-1));
10328 cmd->cmd_arq_buf = arsbuf;
10329 ars_size = mpt->m_req_sense_size;
10330 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
10331 (mpt->m_req_sense_size * (SMID-1))) &
10332 0xffffffffu;
10334 bzero(arsbuf, ars_size);
10336 ddi_put8(acc_hdl, &scsi_io_req->SenseBufferLength, ars_size);
10337 ddi_put32(acc_hdl, &scsi_io_req->SenseBufferLowAddress,
10338 ars_dmaaddrlow);
10341 * Put SGE for data and data_out buffer at the end of
10342 * scsi_io_request message header.(64 bytes in total)
10343 * Set SGLOffset0 value
10345 ddi_put8(acc_hdl, &scsi_io_req->SGLOffset0,
10346 offsetof(MPI2_SCSI_IO_REQUEST, SGL) / 4);
10349 * Setup descriptor info. RAID passthrough must use the
10350 * default request descriptor which is already set, so if this
10351 * is a SCSI IO request, change the descriptor to SCSI IO.
10353 if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
10354 desc_type = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
10355 request_desc = ((uint64_t)ddi_get16(acc_hdl,
10356 &scsi_io_req->DevHandle) << 48);
10358 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
10359 DDI_DMA_SYNC_FORDEV);
10363 * We must wait till the message has been completed before
10364 * beginning the next message so we wait for this one to
10365 * finish.
10367 (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
10368 request_desc |= (SMID << 16) + desc_type;
10369 cmd->cmd_rfm = NULL;
10370 MPTSAS_START_CMD(mpt, request_desc);
10371 if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
10372 (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
10373 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
10377 typedef void (mptsas_pre_f)(mptsas_t *, mptsas_pt_request_t *);
10378 static mptsas_pre_f mpi_pre_ioc_facts;
10379 static mptsas_pre_f mpi_pre_port_facts;
10380 static mptsas_pre_f mpi_pre_fw_download;
10381 static mptsas_pre_f mpi_pre_fw_25_download;
10382 static mptsas_pre_f mpi_pre_fw_upload;
10383 static mptsas_pre_f mpi_pre_fw_25_upload;
10384 static mptsas_pre_f mpi_pre_sata_passthrough;
10385 static mptsas_pre_f mpi_pre_smp_passthrough;
10386 static mptsas_pre_f mpi_pre_config;
10387 static mptsas_pre_f mpi_pre_sas_io_unit_control;
10388 static mptsas_pre_f mpi_pre_scsi_io_req;
10391 * Prepare the pt for a SAS2 FW_DOWNLOAD request.
10393 static void
10394 mpi_pre_fw_download(mptsas_t *mpt, mptsas_pt_request_t *pt)
10396 pMpi2FWDownloadTCSGE_t tcsge;
10397 pMpi2FWDownloadRequest req;
10400 * If SAS3, call separate function.
10402 if (mpt->m_MPI25) {
10403 mpi_pre_fw_25_download(mpt, pt);
10404 return;
10408 * User requests should come in with the Transaction
10409 * context element where the SGL will go. Putting the
10410 * SGL after that seems to work, but don't really know
10411 * why. Other drivers tend to create an extra SGL and
10412 * refer to the TCE through that.
10414 req = (pMpi2FWDownloadRequest)pt->request;
10415 tcsge = (pMpi2FWDownloadTCSGE_t)&req->SGL;
10416 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10417 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10418 mptsas_log(mpt, CE_WARN, "FW Download tce invalid!");
10421 pt->sgl_offset = offsetof(MPI2_FW_DOWNLOAD_REQUEST, SGL) +
10422 sizeof (*tcsge);
10423 if (pt->request_size != pt->sgl_offset)
10424 NDBG15(("mpi_pre_fw_download(): Incorrect req size, "
10425 "0x%x, should be 0x%x, dataoutsz 0x%x",
10426 (int)pt->request_size, (int)pt->sgl_offset,
10427 (int)pt->dataout_size));
10428 if (pt->data_size < sizeof (MPI2_FW_DOWNLOAD_REPLY))
10429 NDBG15(("mpi_pre_fw_download(): Incorrect rep size, "
10430 "0x%x, should be 0x%x", pt->data_size,
10431 (int)sizeof (MPI2_FW_DOWNLOAD_REPLY)));
10435 * Prepare the pt for a SAS3 FW_DOWNLOAD request.
10437 static void
10438 mpi_pre_fw_25_download(mptsas_t *mpt, mptsas_pt_request_t *pt)
10440 pMpi2FWDownloadTCSGE_t tcsge;
10441 pMpi2FWDownloadRequest req2;
10442 pMpi25FWDownloadRequest req25;
10445 * User requests should come in with the Transaction
10446 * context element where the SGL will go. The new firmware
10447 * Doesn't use TCE and has space in the main request for
10448 * this information. So move to the right place.
10450 req2 = (pMpi2FWDownloadRequest)pt->request;
10451 req25 = (pMpi25FWDownloadRequest)pt->request;
10452 tcsge = (pMpi2FWDownloadTCSGE_t)&req2->SGL;
10453 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10454 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10455 mptsas_log(mpt, CE_WARN, "FW Download tce invalid!");
10457 req25->ImageOffset = tcsge->ImageOffset;
10458 req25->ImageSize = tcsge->ImageSize;
10460 pt->sgl_offset = offsetof(MPI25_FW_DOWNLOAD_REQUEST, SGL);
10461 if (pt->request_size != pt->sgl_offset)
10462 NDBG15(("mpi_pre_fw_25_download(): Incorrect req size, "
10463 "0x%x, should be 0x%x, dataoutsz 0x%x",
10464 pt->request_size, pt->sgl_offset,
10465 pt->dataout_size));
10466 if (pt->data_size < sizeof (MPI2_FW_DOWNLOAD_REPLY))
10467 NDBG15(("mpi_pre_fw_25_download(): Incorrect rep size, "
10468 "0x%x, should be 0x%x", pt->data_size,
10469 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10473 * Prepare the pt for a SAS2 FW_UPLOAD request.
10475 static void
10476 mpi_pre_fw_upload(mptsas_t *mpt, mptsas_pt_request_t *pt)
10478 pMpi2FWUploadTCSGE_t tcsge;
10479 pMpi2FWUploadRequest_t req;
10482 * If SAS3, call separate function.
10484 if (mpt->m_MPI25) {
10485 mpi_pre_fw_25_upload(mpt, pt);
10486 return;
10490 * User requests should come in with the Transaction
10491 * context element where the SGL will go. Putting the
10492 * SGL after that seems to work, but don't really know
10493 * why. Other drivers tend to create an extra SGL and
10494 * refer to the TCE through that.
10496 req = (pMpi2FWUploadRequest_t)pt->request;
10497 tcsge = (pMpi2FWUploadTCSGE_t)&req->SGL;
10498 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10499 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10500 mptsas_log(mpt, CE_WARN, "FW Upload tce invalid!");
10503 pt->sgl_offset = offsetof(MPI2_FW_UPLOAD_REQUEST, SGL) +
10504 sizeof (*tcsge);
10505 if (pt->request_size != pt->sgl_offset)
10506 NDBG15(("mpi_pre_fw_upload(): Incorrect req size, "
10507 "0x%x, should be 0x%x, dataoutsz 0x%x",
10508 pt->request_size, pt->sgl_offset,
10509 pt->dataout_size));
10510 if (pt->data_size < sizeof (MPI2_FW_UPLOAD_REPLY))
10511 NDBG15(("mpi_pre_fw_upload(): Incorrect rep size, "
10512 "0x%x, should be 0x%x", pt->data_size,
10513 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10517 * Prepare the pt a SAS3 FW_UPLOAD request.
10519 static void
10520 mpi_pre_fw_25_upload(mptsas_t *mpt, mptsas_pt_request_t *pt)
10522 pMpi2FWUploadTCSGE_t tcsge;
10523 pMpi2FWUploadRequest_t req2;
10524 pMpi25FWUploadRequest_t req25;
10527 * User requests should come in with the Transaction
10528 * context element where the SGL will go. The new firmware
10529 * Doesn't use TCE and has space in the main request for
10530 * this information. So move to the right place.
10532 req2 = (pMpi2FWUploadRequest_t)pt->request;
10533 req25 = (pMpi25FWUploadRequest_t)pt->request;
10534 tcsge = (pMpi2FWUploadTCSGE_t)&req2->SGL;
10535 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10536 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10537 mptsas_log(mpt, CE_WARN, "FW Upload tce invalid!");
10539 req25->ImageOffset = tcsge->ImageOffset;
10540 req25->ImageSize = tcsge->ImageSize;
10542 pt->sgl_offset = offsetof(MPI25_FW_UPLOAD_REQUEST, SGL);
10543 if (pt->request_size != pt->sgl_offset)
10544 NDBG15(("mpi_pre_fw_25_upload(): Incorrect req size, "
10545 "0x%x, should be 0x%x, dataoutsz 0x%x",
10546 pt->request_size, pt->sgl_offset,
10547 pt->dataout_size));
10548 if (pt->data_size < sizeof (MPI2_FW_UPLOAD_REPLY))
10549 NDBG15(("mpi_pre_fw_25_upload(): Incorrect rep size, "
10550 "0x%x, should be 0x%x", pt->data_size,
10551 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10555 * Prepare the pt for an IOC_FACTS request.
10557 static void
10558 mpi_pre_ioc_facts(mptsas_t *mpt, mptsas_pt_request_t *pt)
10560 #ifndef __lock_lint
10561 _NOTE(ARGUNUSED(mpt))
10562 #endif
10563 if (pt->request_size != sizeof (MPI2_IOC_FACTS_REQUEST))
10564 NDBG15(("mpi_pre_ioc_facts(): Incorrect req size, "
10565 "0x%x, should be 0x%x, dataoutsz 0x%x",
10566 pt->request_size,
10567 (int)sizeof (MPI2_IOC_FACTS_REQUEST),
10568 pt->dataout_size));
10569 if (pt->data_size != sizeof (MPI2_IOC_FACTS_REPLY))
10570 NDBG15(("mpi_pre_ioc_facts(): Incorrect rep size, "
10571 "0x%x, should be 0x%x", pt->data_size,
10572 (int)sizeof (MPI2_IOC_FACTS_REPLY)));
10573 pt->sgl_offset = (uint16_t)pt->request_size;
10577 * Prepare the pt for a PORT_FACTS request.
10579 static void
10580 mpi_pre_port_facts(mptsas_t *mpt, mptsas_pt_request_t *pt)
10582 #ifndef __lock_lint
10583 _NOTE(ARGUNUSED(mpt))
10584 #endif
10585 if (pt->request_size != sizeof (MPI2_PORT_FACTS_REQUEST))
10586 NDBG15(("mpi_pre_port_facts(): Incorrect req size, "
10587 "0x%x, should be 0x%x, dataoutsz 0x%x",
10588 pt->request_size,
10589 (int)sizeof (MPI2_PORT_FACTS_REQUEST),
10590 pt->dataout_size));
10591 if (pt->data_size != sizeof (MPI2_PORT_FACTS_REPLY))
10592 NDBG15(("mpi_pre_port_facts(): Incorrect rep size, "
10593 "0x%x, should be 0x%x", pt->data_size,
10594 (int)sizeof (MPI2_PORT_FACTS_REPLY)));
10595 pt->sgl_offset = (uint16_t)pt->request_size;
10599 * Prepare pt for a SATA_PASSTHROUGH request.
10601 static void
10602 mpi_pre_sata_passthrough(mptsas_t *mpt, mptsas_pt_request_t *pt)
10604 #ifndef __lock_lint
10605 _NOTE(ARGUNUSED(mpt))
10606 #endif
10607 pt->sgl_offset = offsetof(MPI2_SATA_PASSTHROUGH_REQUEST, SGL);
10608 if (pt->request_size != pt->sgl_offset)
10609 NDBG15(("mpi_pre_sata_passthrough(): Incorrect req size, "
10610 "0x%x, should be 0x%x, dataoutsz 0x%x",
10611 pt->request_size, pt->sgl_offset,
10612 pt->dataout_size));
10613 if (pt->data_size != sizeof (MPI2_SATA_PASSTHROUGH_REPLY))
10614 NDBG15(("mpi_pre_sata_passthrough(): Incorrect rep size, "
10615 "0x%x, should be 0x%x", pt->data_size,
10616 (int)sizeof (MPI2_SATA_PASSTHROUGH_REPLY)));
10619 static void
10620 mpi_pre_smp_passthrough(mptsas_t *mpt, mptsas_pt_request_t *pt)
10622 #ifndef __lock_lint
10623 _NOTE(ARGUNUSED(mpt))
10624 #endif
10625 pt->sgl_offset = offsetof(MPI2_SMP_PASSTHROUGH_REQUEST, SGL);
10626 if (pt->request_size != pt->sgl_offset)
10627 NDBG15(("mpi_pre_smp_passthrough(): Incorrect req size, "
10628 "0x%x, should be 0x%x, dataoutsz 0x%x",
10629 pt->request_size, pt->sgl_offset,
10630 pt->dataout_size));
10631 if (pt->data_size != sizeof (MPI2_SMP_PASSTHROUGH_REPLY))
10632 NDBG15(("mpi_pre_smp_passthrough(): Incorrect rep size, "
10633 "0x%x, should be 0x%x", pt->data_size,
10634 (int)sizeof (MPI2_SMP_PASSTHROUGH_REPLY)));
10638 * Prepare pt for a CONFIG request.
10640 static void
10641 mpi_pre_config(mptsas_t *mpt, mptsas_pt_request_t *pt)
10643 #ifndef __lock_lint
10644 _NOTE(ARGUNUSED(mpt))
10645 #endif
10646 pt->sgl_offset = offsetof(MPI2_CONFIG_REQUEST, PageBufferSGE);
10647 if (pt->request_size != pt->sgl_offset)
10648 NDBG15(("mpi_pre_config(): Incorrect req size, 0x%x, "
10649 "should be 0x%x, dataoutsz 0x%x", pt->request_size,
10650 pt->sgl_offset, pt->dataout_size));
10651 if (pt->data_size != sizeof (MPI2_CONFIG_REPLY))
10652 NDBG15(("mpi_pre_config(): Incorrect rep size, 0x%x, "
10653 "should be 0x%x", pt->data_size,
10654 (int)sizeof (MPI2_CONFIG_REPLY)));
10655 pt->simple = 1;
10659 * Prepare pt for a SCSI_IO_REQ request.
10661 static void
10662 mpi_pre_scsi_io_req(mptsas_t *mpt, mptsas_pt_request_t *pt)
10664 #ifndef __lock_lint
10665 _NOTE(ARGUNUSED(mpt))
10666 #endif
10667 pt->sgl_offset = offsetof(MPI2_SCSI_IO_REQUEST, SGL);
10668 if (pt->request_size != pt->sgl_offset)
10669 NDBG15(("mpi_pre_config(): Incorrect req size, 0x%x, "
10670 "should be 0x%x, dataoutsz 0x%x", pt->request_size,
10671 pt->sgl_offset,
10672 pt->dataout_size));
10673 if (pt->data_size != sizeof (MPI2_SCSI_IO_REPLY))
10674 NDBG15(("mpi_pre_config(): Incorrect rep size, 0x%x, "
10675 "should be 0x%x", pt->data_size,
10676 (int)sizeof (MPI2_SCSI_IO_REPLY)));
10680 * Prepare the mptsas_cmd for a SAS_IO_UNIT_CONTROL request.
10682 static void
10683 mpi_pre_sas_io_unit_control(mptsas_t *mpt, mptsas_pt_request_t *pt)
10685 #ifndef __lock_lint
10686 _NOTE(ARGUNUSED(mpt))
10687 #endif
10688 pt->sgl_offset = (uint16_t)pt->request_size;
10692 * A set of functions to prepare an mptsas_cmd for the various
10693 * supported requests.
10695 static struct mptsas_func {
10696 U8 Function;
10697 char *Name;
10698 mptsas_pre_f *f_pre;
10699 } mptsas_func_list[] = {
10700 { MPI2_FUNCTION_IOC_FACTS, "IOC_FACTS", mpi_pre_ioc_facts },
10701 { MPI2_FUNCTION_PORT_FACTS, "PORT_FACTS", mpi_pre_port_facts },
10702 { MPI2_FUNCTION_FW_DOWNLOAD, "FW_DOWNLOAD", mpi_pre_fw_download },
10703 { MPI2_FUNCTION_FW_UPLOAD, "FW_UPLOAD", mpi_pre_fw_upload },
10704 { MPI2_FUNCTION_SATA_PASSTHROUGH, "SATA_PASSTHROUGH",
10705 mpi_pre_sata_passthrough },
10706 { MPI2_FUNCTION_SMP_PASSTHROUGH, "SMP_PASSTHROUGH",
10707 mpi_pre_smp_passthrough},
10708 { MPI2_FUNCTION_SCSI_IO_REQUEST, "SCSI_IO_REQUEST",
10709 mpi_pre_scsi_io_req},
10710 { MPI2_FUNCTION_CONFIG, "CONFIG", mpi_pre_config},
10711 { MPI2_FUNCTION_SAS_IO_UNIT_CONTROL, "SAS_IO_UNIT_CONTROL",
10712 mpi_pre_sas_io_unit_control },
10713 { 0xFF, NULL, NULL } /* list end */
10716 static void
10717 mptsas_prep_sgl_offset(mptsas_t *mpt, mptsas_pt_request_t *pt)
10719 pMPI2RequestHeader_t hdr;
10720 struct mptsas_func *f;
10722 hdr = (pMPI2RequestHeader_t)pt->request;
10724 for (f = mptsas_func_list; f->f_pre != NULL; f++) {
10725 if (hdr->Function == f->Function) {
10726 f->f_pre(mpt, pt);
10727 NDBG15(("mptsas_prep_sgl_offset: Function %s,"
10728 " sgl_offset 0x%x", f->Name,
10729 pt->sgl_offset));
10730 return;
10733 NDBG15(("mptsas_prep_sgl_offset: Unknown Function 0x%02x,"
10734 " returning req_size 0x%x for sgl_offset",
10735 hdr->Function, pt->request_size));
10736 pt->sgl_offset = (uint16_t)pt->request_size;
10740 static int
10741 mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
10742 uint8_t *data, uint32_t request_size, uint32_t reply_size,
10743 uint32_t data_size, uint32_t direction, uint8_t *dataout,
10744 uint32_t dataout_size, short timeout, int mode)
10746 mptsas_pt_request_t pt;
10747 mptsas_dma_alloc_state_t data_dma_state;
10748 mptsas_dma_alloc_state_t dataout_dma_state;
10749 caddr_t memp;
10750 mptsas_cmd_t *cmd = NULL;
10751 struct scsi_pkt *pkt;
10752 uint32_t reply_len = 0, sense_len = 0;
10753 pMPI2RequestHeader_t request_hdrp;
10754 pMPI2RequestHeader_t request_msg;
10755 pMPI2DefaultReply_t reply_msg;
10756 Mpi2SCSIIOReply_t rep_msg;
10757 int rvalue;
10758 int i, status = 0, pt_flags = 0, rv = 0;
10759 uint8_t function;
10761 ASSERT(mutex_owned(&mpt->m_mutex));
10763 reply_msg = (pMPI2DefaultReply_t)(&rep_msg);
10764 bzero(reply_msg, sizeof (MPI2_DEFAULT_REPLY));
10765 request_msg = kmem_zalloc(request_size, KM_SLEEP);
10767 mutex_exit(&mpt->m_mutex);
10769 * copy in the request buffer since it could be used by
10770 * another thread when the pt request into waitq
10772 if (ddi_copyin(request, request_msg, request_size, mode)) {
10773 mutex_enter(&mpt->m_mutex);
10774 status = EFAULT;
10775 mptsas_log(mpt, CE_WARN, "failed to copy request data");
10776 goto out;
10778 NDBG27(("mptsas_do_passthru: mode 0x%x, size 0x%x, Func 0x%x",
10779 mode, request_size, request_msg->Function));
10780 mutex_enter(&mpt->m_mutex);
10782 function = request_msg->Function;
10783 if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
10784 pMpi2SCSITaskManagementRequest_t task;
10785 task = (pMpi2SCSITaskManagementRequest_t)request_msg;
10786 mptsas_setup_bus_reset_delay(mpt);
10787 rv = mptsas_ioc_task_management(mpt, task->TaskType,
10788 task->DevHandle, (int)task->LUN[1], reply, reply_size,
10789 mode);
10791 if (rv != TRUE) {
10792 status = EIO;
10793 mptsas_log(mpt, CE_WARN, "task management failed");
10795 goto out;
10798 if (data_size != 0) {
10799 data_dma_state.size = data_size;
10800 if (mptsas_dma_alloc(mpt, &data_dma_state) != DDI_SUCCESS) {
10801 status = ENOMEM;
10802 mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10803 "resource");
10804 goto out;
10806 pt_flags |= MPTSAS_DATA_ALLOCATED;
10807 if (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10808 mutex_exit(&mpt->m_mutex);
10809 for (i = 0; i < data_size; i++) {
10810 if (ddi_copyin(data + i, (uint8_t *)
10811 data_dma_state.memp + i, 1, mode)) {
10812 mutex_enter(&mpt->m_mutex);
10813 status = EFAULT;
10814 mptsas_log(mpt, CE_WARN, "failed to "
10815 "copy read data");
10816 goto out;
10819 mutex_enter(&mpt->m_mutex);
10821 } else {
10822 bzero(&data_dma_state, sizeof (data_dma_state));
10825 if (dataout_size != 0) {
10826 dataout_dma_state.size = dataout_size;
10827 if (mptsas_dma_alloc(mpt, &dataout_dma_state) != DDI_SUCCESS) {
10828 status = ENOMEM;
10829 mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10830 "resource");
10831 goto out;
10833 pt_flags |= MPTSAS_DATAOUT_ALLOCATED;
10834 mutex_exit(&mpt->m_mutex);
10835 for (i = 0; i < dataout_size; i++) {
10836 if (ddi_copyin(dataout + i, (uint8_t *)
10837 dataout_dma_state.memp + i, 1, mode)) {
10838 mutex_enter(&mpt->m_mutex);
10839 mptsas_log(mpt, CE_WARN, "failed to copy out"
10840 " data");
10841 status = EFAULT;
10842 goto out;
10845 mutex_enter(&mpt->m_mutex);
10846 } else {
10847 bzero(&dataout_dma_state, sizeof (dataout_dma_state));
10850 if ((rvalue = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
10851 status = EAGAIN;
10852 mptsas_log(mpt, CE_NOTE, "event ack command pool is full");
10853 goto out;
10855 pt_flags |= MPTSAS_REQUEST_POOL_CMD;
10857 bzero((caddr_t)cmd, sizeof (*cmd));
10858 bzero((caddr_t)pkt, scsi_pkt_size());
10859 bzero((caddr_t)&pt, sizeof (pt));
10861 cmd->ioc_cmd_slot = (uint32_t)(rvalue);
10863 pt.request = (uint8_t *)request_msg;
10864 pt.direction = direction;
10865 pt.simple = 0;
10866 pt.request_size = request_size;
10867 pt.data_size = data_size;
10868 pt.dataout_size = dataout_size;
10869 pt.data_cookie = data_dma_state.cookie;
10870 pt.dataout_cookie = dataout_dma_state.cookie;
10871 mptsas_prep_sgl_offset(mpt, &pt);
10874 * Form a blank cmd/pkt to store the acknowledgement message
10876 pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb[0];
10877 pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb;
10878 pkt->pkt_ha_private = (opaque_t)&pt;
10879 pkt->pkt_flags = FLAG_HEAD;
10880 pkt->pkt_time = timeout;
10881 cmd->cmd_pkt = pkt;
10882 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_PASSTHRU;
10884 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10885 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10886 uint8_t com, cdb_group_id;
10887 boolean_t ret;
10889 pkt->pkt_cdbp = ((pMpi2SCSIIORequest_t)request_msg)->CDB.CDB32;
10890 com = pkt->pkt_cdbp[0];
10891 cdb_group_id = CDB_GROUPID(com);
10892 switch (cdb_group_id) {
10893 case CDB_GROUPID_0: cmd->cmd_cdblen = CDB_GROUP0; break;
10894 case CDB_GROUPID_1: cmd->cmd_cdblen = CDB_GROUP1; break;
10895 case CDB_GROUPID_2: cmd->cmd_cdblen = CDB_GROUP2; break;
10896 case CDB_GROUPID_4: cmd->cmd_cdblen = CDB_GROUP4; break;
10897 case CDB_GROUPID_5: cmd->cmd_cdblen = CDB_GROUP5; break;
10898 default:
10899 NDBG27(("mptsas_do_passthru: SCSI_IO, reserved "
10900 "CDBGROUP 0x%x requested!", cdb_group_id));
10901 break;
10904 reply_len = sizeof (MPI2_SCSI_IO_REPLY);
10905 sense_len = reply_size - reply_len;
10906 ret = mptsas_cmdarqsize(mpt, cmd, sense_len, KM_SLEEP);
10907 VERIFY(ret == B_TRUE);
10908 } else {
10909 reply_len = reply_size;
10910 sense_len = 0;
10913 NDBG27(("mptsas_do_passthru: %s, dsz 0x%x, dosz 0x%x, replen 0x%x, "
10914 "snslen 0x%x",
10915 (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE)?"Write":"Read",
10916 data_size, dataout_size, reply_len, sense_len));
10919 * Save the command in a slot
10921 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
10923 * Once passthru command get slot, set cmd_flags
10924 * CFLAG_PREPARED.
10926 cmd->cmd_flags |= CFLAG_PREPARED;
10927 mptsas_start_passthru(mpt, cmd);
10928 } else {
10929 mptsas_waitq_add(mpt, cmd);
10932 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
10933 cv_wait(&mpt->m_passthru_cv, &mpt->m_mutex);
10936 NDBG27(("mptsas_do_passthru: Cmd complete, flags 0x%x, rfm 0x%x "
10937 "pktreason 0x%x", cmd->cmd_flags, cmd->cmd_rfm,
10938 pkt->pkt_reason));
10940 if (cmd->cmd_flags & CFLAG_PREPARED) {
10941 memp = mpt->m_req_frame + (mpt->m_req_frame_size *
10942 cmd->cmd_slot);
10943 request_hdrp = (pMPI2RequestHeader_t)memp;
10946 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
10947 status = ETIMEDOUT;
10948 mptsas_log(mpt, CE_WARN, "passthrough command timeout");
10949 pt_flags |= MPTSAS_CMD_TIMEOUT;
10950 goto out;
10953 if (cmd->cmd_rfm) {
10955 * cmd_rfm is zero means the command reply is a CONTEXT
10956 * reply and no PCI Write to post the free reply SMFA
10957 * because no reply message frame is used.
10958 * cmd_rfm is non-zero means the reply is a ADDRESS
10959 * reply and reply message frame is used.
10961 pt_flags |= MPTSAS_ADDRESS_REPLY;
10962 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
10963 DDI_DMA_SYNC_FORCPU);
10964 reply_msg = (pMPI2DefaultReply_t)
10965 (mpt->m_reply_frame + (cmd->cmd_rfm -
10966 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
10969 mptsas_fma_check(mpt, cmd);
10970 if (pkt->pkt_reason == CMD_TRAN_ERR) {
10971 status = EAGAIN;
10972 mptsas_log(mpt, CE_WARN, "passthru fma error");
10973 goto out;
10975 if (pkt->pkt_reason == CMD_RESET) {
10976 status = EAGAIN;
10977 mptsas_log(mpt, CE_WARN, "ioc reset abort passthru");
10978 goto out;
10981 if (pkt->pkt_reason == CMD_INCOMPLETE) {
10982 status = EIO;
10983 mptsas_log(mpt, CE_WARN, "passthrough command incomplete");
10984 goto out;
10987 mutex_exit(&mpt->m_mutex);
10988 if (cmd->cmd_flags & CFLAG_PREPARED) {
10989 function = request_hdrp->Function;
10990 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10991 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10992 reply_len = sizeof (MPI2_SCSI_IO_REPLY);
10993 sense_len = cmd->cmd_extrqslen ?
10994 min(sense_len, cmd->cmd_extrqslen) :
10995 min(sense_len, cmd->cmd_rqslen);
10996 } else {
10997 reply_len = reply_size;
10998 sense_len = 0;
11001 for (i = 0; i < reply_len; i++) {
11002 if (ddi_copyout((uint8_t *)reply_msg + i, reply + i, 1,
11003 mode)) {
11004 mutex_enter(&mpt->m_mutex);
11005 status = EFAULT;
11006 mptsas_log(mpt, CE_WARN, "failed to copy out "
11007 "reply data");
11008 goto out;
11011 for (i = 0; i < sense_len; i++) {
11012 if (ddi_copyout((uint8_t *)request_hdrp + 64 + i,
11013 reply + reply_len + i, 1, mode)) {
11014 mutex_enter(&mpt->m_mutex);
11015 status = EFAULT;
11016 mptsas_log(mpt, CE_WARN, "failed to copy out "
11017 "sense data");
11018 goto out;
11023 if (data_size) {
11024 if (direction != MPTSAS_PASS_THRU_DIRECTION_WRITE) {
11025 (void) ddi_dma_sync(data_dma_state.handle, 0, 0,
11026 DDI_DMA_SYNC_FORCPU);
11027 for (i = 0; i < data_size; i++) {
11028 if (ddi_copyout((uint8_t *)(
11029 data_dma_state.memp + i), data + i, 1,
11030 mode)) {
11031 mutex_enter(&mpt->m_mutex);
11032 status = EFAULT;
11033 mptsas_log(mpt, CE_WARN, "failed to "
11034 "copy out the reply data");
11035 goto out;
11040 mutex_enter(&mpt->m_mutex);
11041 out:
11043 * Put the reply frame back on the free queue, increment the free
11044 * index, and write the new index to the free index register. But only
11045 * if this reply is an ADDRESS reply.
11047 if (pt_flags & MPTSAS_ADDRESS_REPLY) {
11048 ddi_put32(mpt->m_acc_free_queue_hdl,
11049 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11050 cmd->cmd_rfm);
11051 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11052 DDI_DMA_SYNC_FORDEV);
11053 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11054 mpt->m_free_index = 0;
11056 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11057 mpt->m_free_index);
11059 if (cmd) {
11060 if (cmd->cmd_extrqslen != 0) {
11061 rmfree(mpt->m_erqsense_map, cmd->cmd_extrqschunks,
11062 cmd->cmd_extrqsidx + 1);
11064 if (cmd->cmd_flags & CFLAG_PREPARED) {
11065 mptsas_remove_cmd(mpt, cmd);
11066 pt_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11069 if (pt_flags & MPTSAS_REQUEST_POOL_CMD)
11070 mptsas_return_to_pool(mpt, cmd);
11071 if (pt_flags & MPTSAS_DATA_ALLOCATED) {
11072 if (mptsas_check_dma_handle(data_dma_state.handle) !=
11073 DDI_SUCCESS) {
11074 ddi_fm_service_impact(mpt->m_dip,
11075 DDI_SERVICE_UNAFFECTED);
11076 status = EFAULT;
11078 mptsas_dma_free(&data_dma_state);
11080 if (pt_flags & MPTSAS_DATAOUT_ALLOCATED) {
11081 if (mptsas_check_dma_handle(dataout_dma_state.handle) !=
11082 DDI_SUCCESS) {
11083 ddi_fm_service_impact(mpt->m_dip,
11084 DDI_SERVICE_UNAFFECTED);
11085 status = EFAULT;
11087 mptsas_dma_free(&dataout_dma_state);
11089 if (pt_flags & MPTSAS_CMD_TIMEOUT) {
11090 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
11091 mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
11094 if (request_msg)
11095 kmem_free(request_msg, request_size);
11096 NDBG27(("mptsas_do_passthru: Done status 0x%x", status));
11098 return (status);
11101 static int
11102 mptsas_pass_thru(mptsas_t *mpt, mptsas_pass_thru_t *data, int mode)
11105 * If timeout is 0, set timeout to default of 60 seconds.
11107 if (data->Timeout == 0) {
11108 data->Timeout = MPTSAS_PASS_THRU_TIME_DEFAULT;
11111 if (((data->DataSize == 0) &&
11112 (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_NONE)) ||
11113 ((data->DataSize != 0) &&
11114 ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_READ) ||
11115 (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_WRITE) ||
11116 ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) &&
11117 (data->DataOutSize != 0))))) {
11118 if (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) {
11119 data->DataDirection = MPTSAS_PASS_THRU_DIRECTION_READ;
11120 } else {
11121 data->DataOutSize = 0;
11124 * Send passthru request messages
11126 return (mptsas_do_passthru(mpt,
11127 (uint8_t *)((uintptr_t)data->PtrRequest),
11128 (uint8_t *)((uintptr_t)data->PtrReply),
11129 (uint8_t *)((uintptr_t)data->PtrData),
11130 data->RequestSize, data->ReplySize,
11131 data->DataSize, data->DataDirection,
11132 (uint8_t *)((uintptr_t)data->PtrDataOut),
11133 data->DataOutSize, data->Timeout, mode));
11134 } else {
11135 return (EINVAL);
11139 static uint8_t
11140 mptsas_get_fw_diag_buffer_number(mptsas_t *mpt, uint32_t unique_id)
11142 uint8_t index;
11144 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
11145 if (mpt->m_fw_diag_buffer_list[index].unique_id == unique_id) {
11146 return (index);
11150 return (MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND);
11153 static void
11154 mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd)
11156 pMpi2DiagBufferPostRequest_t pDiag_post_msg;
11157 pMpi2DiagReleaseRequest_t pDiag_release_msg;
11158 struct scsi_pkt *pkt = cmd->cmd_pkt;
11159 mptsas_diag_request_t *diag = pkt->pkt_ha_private;
11160 uint32_t i;
11161 uint64_t request_desc;
11163 ASSERT(mutex_owned(&mpt->m_mutex));
11166 * Form the diag message depending on the post or release function.
11168 if (diag->function == MPI2_FUNCTION_DIAG_BUFFER_POST) {
11169 pDiag_post_msg = (pMpi2DiagBufferPostRequest_t)
11170 (mpt->m_req_frame + (mpt->m_req_frame_size *
11171 cmd->cmd_slot));
11172 bzero(pDiag_post_msg, mpt->m_req_frame_size);
11173 ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->Function,
11174 diag->function);
11175 ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->BufferType,
11176 diag->pBuffer->buffer_type);
11177 ddi_put8(mpt->m_acc_req_frame_hdl,
11178 &pDiag_post_msg->ExtendedType,
11179 diag->pBuffer->extended_type);
11180 ddi_put32(mpt->m_acc_req_frame_hdl,
11181 &pDiag_post_msg->BufferLength,
11182 diag->pBuffer->buffer_data.size);
11183 for (i = 0; i < (sizeof (pDiag_post_msg->ProductSpecific) / 4);
11184 i++) {
11185 ddi_put32(mpt->m_acc_req_frame_hdl,
11186 &pDiag_post_msg->ProductSpecific[i],
11187 diag->pBuffer->product_specific[i]);
11189 ddi_put32(mpt->m_acc_req_frame_hdl,
11190 &pDiag_post_msg->BufferAddress.Low,
11191 (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
11192 & 0xffffffffull));
11193 ddi_put32(mpt->m_acc_req_frame_hdl,
11194 &pDiag_post_msg->BufferAddress.High,
11195 (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
11196 >> 32));
11197 } else {
11198 pDiag_release_msg = (pMpi2DiagReleaseRequest_t)
11199 (mpt->m_req_frame + (mpt->m_req_frame_size *
11200 cmd->cmd_slot));
11201 bzero(pDiag_release_msg, mpt->m_req_frame_size);
11202 ddi_put8(mpt->m_acc_req_frame_hdl,
11203 &pDiag_release_msg->Function, diag->function);
11204 ddi_put8(mpt->m_acc_req_frame_hdl,
11205 &pDiag_release_msg->BufferType,
11206 diag->pBuffer->buffer_type);
11210 * Send the message
11212 (void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
11213 DDI_DMA_SYNC_FORDEV);
11214 request_desc = (cmd->cmd_slot << 16) +
11215 MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
11216 cmd->cmd_rfm = NULL;
11217 MPTSAS_START_CMD(mpt, request_desc);
11218 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
11219 DDI_SUCCESS) ||
11220 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
11221 DDI_SUCCESS)) {
11222 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
11226 static int
11227 mptsas_post_fw_diag_buffer(mptsas_t *mpt,
11228 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
11230 mptsas_diag_request_t diag;
11231 int status, slot_num, post_flags = 0;
11232 mptsas_cmd_t *cmd = NULL;
11233 struct scsi_pkt *pkt;
11234 pMpi2DiagBufferPostReply_t reply;
11235 uint16_t iocstatus;
11236 uint32_t iocloginfo, transfer_length;
11239 * If buffer is not enabled, just leave.
11241 *return_code = MPTSAS_FW_DIAG_ERROR_POST_FAILED;
11242 if (!pBuffer->enabled) {
11243 status = DDI_FAILURE;
11244 goto out;
11248 * Clear some flags initially.
11250 pBuffer->force_release = FALSE;
11251 pBuffer->valid_data = FALSE;
11252 pBuffer->owned_by_firmware = FALSE;
11255 * Get a cmd buffer from the cmd buffer pool
11257 if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
11258 status = DDI_FAILURE;
11259 mptsas_log(mpt, CE_NOTE, "command pool is full: Post FW Diag");
11260 goto out;
11262 post_flags |= MPTSAS_REQUEST_POOL_CMD;
11264 bzero((caddr_t)cmd, sizeof (*cmd));
11265 bzero((caddr_t)pkt, scsi_pkt_size());
11267 cmd->ioc_cmd_slot = (uint32_t)(slot_num);
11269 diag.pBuffer = pBuffer;
11270 diag.function = MPI2_FUNCTION_DIAG_BUFFER_POST;
11273 * Form a blank cmd/pkt to store the acknowledgement message
11275 pkt->pkt_ha_private = (opaque_t)&diag;
11276 pkt->pkt_flags = FLAG_HEAD;
11277 pkt->pkt_time = 60;
11278 cmd->cmd_pkt = pkt;
11279 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_FW_DIAG;
11282 * Save the command in a slot
11284 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
11286 * Once passthru command get slot, set cmd_flags
11287 * CFLAG_PREPARED.
11289 cmd->cmd_flags |= CFLAG_PREPARED;
11290 mptsas_start_diag(mpt, cmd);
11291 } else {
11292 mptsas_waitq_add(mpt, cmd);
11295 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
11296 cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
11299 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
11300 status = DDI_FAILURE;
11301 mptsas_log(mpt, CE_WARN, "Post FW Diag command timeout");
11302 goto out;
11306 * cmd_rfm points to the reply message if a reply was given. Check the
11307 * IOCStatus to make sure everything went OK with the FW diag request
11308 * and set buffer flags.
11310 if (cmd->cmd_rfm) {
11311 post_flags |= MPTSAS_ADDRESS_REPLY;
11312 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
11313 DDI_DMA_SYNC_FORCPU);
11314 reply = (pMpi2DiagBufferPostReply_t)(mpt->m_reply_frame +
11315 (cmd->cmd_rfm -
11316 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
11319 * Get the reply message data
11321 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
11322 &reply->IOCStatus);
11323 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
11324 &reply->IOCLogInfo);
11325 transfer_length = ddi_get32(mpt->m_acc_reply_frame_hdl,
11326 &reply->TransferLength);
11329 * If post failed quit.
11331 if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
11332 status = DDI_FAILURE;
11333 NDBG13(("post FW Diag Buffer failed: IOCStatus=0x%x, "
11334 "IOCLogInfo=0x%x, TransferLength=0x%x", iocstatus,
11335 iocloginfo, transfer_length));
11336 goto out;
11340 * Post was successful.
11342 pBuffer->valid_data = TRUE;
11343 pBuffer->owned_by_firmware = TRUE;
11344 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11345 status = DDI_SUCCESS;
11348 out:
11350 * Put the reply frame back on the free queue, increment the free
11351 * index, and write the new index to the free index register. But only
11352 * if this reply is an ADDRESS reply.
11354 if (post_flags & MPTSAS_ADDRESS_REPLY) {
11355 ddi_put32(mpt->m_acc_free_queue_hdl,
11356 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11357 cmd->cmd_rfm);
11358 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11359 DDI_DMA_SYNC_FORDEV);
11360 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11361 mpt->m_free_index = 0;
11363 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11364 mpt->m_free_index);
11366 if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
11367 mptsas_remove_cmd(mpt, cmd);
11368 post_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11370 if (post_flags & MPTSAS_REQUEST_POOL_CMD) {
11371 mptsas_return_to_pool(mpt, cmd);
11374 return (status);
11377 static int
11378 mptsas_release_fw_diag_buffer(mptsas_t *mpt,
11379 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
11380 uint32_t diag_type)
11382 mptsas_diag_request_t diag;
11383 int status, slot_num, rel_flags = 0;
11384 mptsas_cmd_t *cmd = NULL;
11385 struct scsi_pkt *pkt;
11386 pMpi2DiagReleaseReply_t reply;
11387 uint16_t iocstatus;
11388 uint32_t iocloginfo;
11391 * If buffer is not enabled, just leave.
11393 *return_code = MPTSAS_FW_DIAG_ERROR_RELEASE_FAILED;
11394 if (!pBuffer->enabled) {
11395 mptsas_log(mpt, CE_NOTE, "This buffer type is not supported "
11396 "by the IOC");
11397 status = DDI_FAILURE;
11398 goto out;
11402 * Clear some flags initially.
11404 pBuffer->force_release = FALSE;
11405 pBuffer->valid_data = FALSE;
11406 pBuffer->owned_by_firmware = FALSE;
11409 * Get a cmd buffer from the cmd buffer pool
11411 if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
11412 status = DDI_FAILURE;
11413 mptsas_log(mpt, CE_NOTE, "command pool is full: Release FW "
11414 "Diag");
11415 goto out;
11417 rel_flags |= MPTSAS_REQUEST_POOL_CMD;
11419 bzero((caddr_t)cmd, sizeof (*cmd));
11420 bzero((caddr_t)pkt, scsi_pkt_size());
11422 cmd->ioc_cmd_slot = (uint32_t)(slot_num);
11424 diag.pBuffer = pBuffer;
11425 diag.function = MPI2_FUNCTION_DIAG_RELEASE;
11428 * Form a blank cmd/pkt to store the acknowledgement message
11430 pkt->pkt_ha_private = (opaque_t)&diag;
11431 pkt->pkt_flags = FLAG_HEAD;
11432 pkt->pkt_time = 60;
11433 cmd->cmd_pkt = pkt;
11434 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_FW_DIAG;
11437 * Save the command in a slot
11439 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
11441 * Once passthru command get slot, set cmd_flags
11442 * CFLAG_PREPARED.
11444 cmd->cmd_flags |= CFLAG_PREPARED;
11445 mptsas_start_diag(mpt, cmd);
11446 } else {
11447 mptsas_waitq_add(mpt, cmd);
11450 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
11451 cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
11454 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
11455 status = DDI_FAILURE;
11456 mptsas_log(mpt, CE_WARN, "Release FW Diag command timeout");
11457 goto out;
11461 * cmd_rfm points to the reply message if a reply was given. Check the
11462 * IOCStatus to make sure everything went OK with the FW diag request
11463 * and set buffer flags.
11465 if (cmd->cmd_rfm) {
11466 rel_flags |= MPTSAS_ADDRESS_REPLY;
11467 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
11468 DDI_DMA_SYNC_FORCPU);
11469 reply = (pMpi2DiagReleaseReply_t)(mpt->m_reply_frame +
11470 (cmd->cmd_rfm -
11471 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
11474 * Get the reply message data
11476 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
11477 &reply->IOCStatus);
11478 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
11479 &reply->IOCLogInfo);
11482 * If release failed quit.
11484 if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) ||
11485 pBuffer->owned_by_firmware) {
11486 status = DDI_FAILURE;
11487 NDBG13(("release FW Diag Buffer failed: "
11488 "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
11489 iocloginfo));
11490 goto out;
11494 * Release was successful.
11496 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11497 status = DDI_SUCCESS;
11500 * If this was for an UNREGISTER diag type command, clear the
11501 * unique ID.
11503 if (diag_type == MPTSAS_FW_DIAG_TYPE_UNREGISTER) {
11504 pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11508 out:
11510 * Put the reply frame back on the free queue, increment the free
11511 * index, and write the new index to the free index register. But only
11512 * if this reply is an ADDRESS reply.
11514 if (rel_flags & MPTSAS_ADDRESS_REPLY) {
11515 ddi_put32(mpt->m_acc_free_queue_hdl,
11516 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11517 cmd->cmd_rfm);
11518 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11519 DDI_DMA_SYNC_FORDEV);
11520 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11521 mpt->m_free_index = 0;
11523 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11524 mpt->m_free_index);
11526 if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
11527 mptsas_remove_cmd(mpt, cmd);
11528 rel_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11530 if (rel_flags & MPTSAS_REQUEST_POOL_CMD) {
11531 mptsas_return_to_pool(mpt, cmd);
11534 return (status);
11537 static int
11538 mptsas_diag_register(mptsas_t *mpt, mptsas_fw_diag_register_t *diag_register,
11539 uint32_t *return_code)
11541 mptsas_fw_diagnostic_buffer_t *pBuffer;
11542 uint8_t extended_type, buffer_type, i;
11543 uint32_t buffer_size;
11544 uint32_t unique_id;
11545 int status;
11547 ASSERT(mutex_owned(&mpt->m_mutex));
11549 extended_type = diag_register->ExtendedType;
11550 buffer_type = diag_register->BufferType;
11551 buffer_size = diag_register->RequestedBufferSize;
11552 unique_id = diag_register->UniqueId;
11555 * Check for valid buffer type
11557 if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
11558 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11559 return (DDI_FAILURE);
11563 * Get the current buffer and look up the unique ID. The unique ID
11564 * should not be found. If it is, the ID is already in use.
11566 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11567 pBuffer = &mpt->m_fw_diag_buffer_list[buffer_type];
11568 if (i != MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11569 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11570 return (DDI_FAILURE);
11574 * The buffer's unique ID should not be registered yet, and the given
11575 * unique ID cannot be 0.
11577 if ((pBuffer->unique_id != MPTSAS_FW_DIAG_INVALID_UID) ||
11578 (unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11579 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11580 return (DDI_FAILURE);
11584 * If this buffer is already posted as immediate, just change owner.
11586 if (pBuffer->immediate && pBuffer->owned_by_firmware &&
11587 (pBuffer->unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11588 pBuffer->immediate = FALSE;
11589 pBuffer->unique_id = unique_id;
11590 return (DDI_SUCCESS);
11594 * Post a new buffer after checking if it's enabled. The DMA buffer
11595 * that is allocated will be contiguous (sgl_len = 1).
11597 if (!pBuffer->enabled) {
11598 *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11599 return (DDI_FAILURE);
11601 bzero(&pBuffer->buffer_data, sizeof (mptsas_dma_alloc_state_t));
11602 pBuffer->buffer_data.size = buffer_size;
11603 if (mptsas_dma_alloc(mpt, &pBuffer->buffer_data) != DDI_SUCCESS) {
11604 mptsas_log(mpt, CE_WARN, "failed to alloc DMA resource for "
11605 "diag buffer: size = %d bytes", buffer_size);
11606 *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11607 return (DDI_FAILURE);
11611 * Copy the given info to the diag buffer and post the buffer.
11613 pBuffer->buffer_type = buffer_type;
11614 pBuffer->immediate = FALSE;
11615 if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
11616 for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
11617 i++) {
11618 pBuffer->product_specific[i] =
11619 diag_register->ProductSpecific[i];
11622 pBuffer->extended_type = extended_type;
11623 pBuffer->unique_id = unique_id;
11624 status = mptsas_post_fw_diag_buffer(mpt, pBuffer, return_code);
11626 if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11627 DDI_SUCCESS) {
11628 mptsas_log(mpt, CE_WARN, "Check of DMA handle failed in "
11629 "mptsas_diag_register.");
11630 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
11631 status = DDI_FAILURE;
11635 * In case there was a failure, free the DMA buffer.
11637 if (status == DDI_FAILURE) {
11638 mptsas_dma_free(&pBuffer->buffer_data);
11641 return (status);
11644 static int
11645 mptsas_diag_unregister(mptsas_t *mpt,
11646 mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
11648 mptsas_fw_diagnostic_buffer_t *pBuffer;
11649 uint8_t i;
11650 uint32_t unique_id;
11651 int status;
11653 ASSERT(mutex_owned(&mpt->m_mutex));
11655 unique_id = diag_unregister->UniqueId;
11658 * Get the current buffer and look up the unique ID. The unique ID
11659 * should be there.
11661 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11662 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11663 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11664 return (DDI_FAILURE);
11667 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11670 * Try to release the buffer from FW before freeing it. If release
11671 * fails, don't free the DMA buffer in case FW tries to access it
11672 * later. If buffer is not owned by firmware, can't release it.
11674 if (!pBuffer->owned_by_firmware) {
11675 status = DDI_SUCCESS;
11676 } else {
11677 status = mptsas_release_fw_diag_buffer(mpt, pBuffer,
11678 return_code, MPTSAS_FW_DIAG_TYPE_UNREGISTER);
11682 * At this point, return the current status no matter what happens with
11683 * the DMA buffer.
11685 pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11686 if (status == DDI_SUCCESS) {
11687 if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11688 DDI_SUCCESS) {
11689 mptsas_log(mpt, CE_WARN, "Check of DMA handle failed "
11690 "in mptsas_diag_unregister.");
11691 ddi_fm_service_impact(mpt->m_dip,
11692 DDI_SERVICE_UNAFFECTED);
11694 mptsas_dma_free(&pBuffer->buffer_data);
11697 return (status);
11700 static int
11701 mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
11702 uint32_t *return_code)
11704 mptsas_fw_diagnostic_buffer_t *pBuffer;
11705 uint8_t i;
11706 uint32_t unique_id;
11708 ASSERT(mutex_owned(&mpt->m_mutex));
11710 unique_id = diag_query->UniqueId;
11713 * If ID is valid, query on ID.
11714 * If ID is invalid, query on buffer type.
11716 if (unique_id == MPTSAS_FW_DIAG_INVALID_UID) {
11717 i = diag_query->BufferType;
11718 if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
11719 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11720 return (DDI_FAILURE);
11722 } else {
11723 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11724 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11725 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11726 return (DDI_FAILURE);
11731 * Fill query structure with the diag buffer info.
11733 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11734 diag_query->BufferType = pBuffer->buffer_type;
11735 diag_query->ExtendedType = pBuffer->extended_type;
11736 if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
11737 for (i = 0; i < (sizeof (diag_query->ProductSpecific) / 4);
11738 i++) {
11739 diag_query->ProductSpecific[i] =
11740 pBuffer->product_specific[i];
11743 diag_query->TotalBufferSize = pBuffer->buffer_data.size;
11744 diag_query->DriverAddedBufferSize = 0;
11745 diag_query->UniqueId = pBuffer->unique_id;
11746 diag_query->ApplicationFlags = 0;
11747 diag_query->DiagnosticFlags = 0;
11750 * Set/Clear application flags
11752 if (pBuffer->immediate) {
11753 diag_query->ApplicationFlags &= ~MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11754 } else {
11755 diag_query->ApplicationFlags |= MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11757 if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
11758 diag_query->ApplicationFlags |=
11759 MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11760 } else {
11761 diag_query->ApplicationFlags &=
11762 ~MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11764 if (pBuffer->owned_by_firmware) {
11765 diag_query->ApplicationFlags |=
11766 MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11767 } else {
11768 diag_query->ApplicationFlags &=
11769 ~MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11772 return (DDI_SUCCESS);
11775 static int
11776 mptsas_diag_read_buffer(mptsas_t *mpt,
11777 mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
11778 uint32_t *return_code, int ioctl_mode)
11780 mptsas_fw_diagnostic_buffer_t *pBuffer;
11781 uint8_t i, *pData;
11782 uint32_t unique_id, byte;
11783 int status;
11785 ASSERT(mutex_owned(&mpt->m_mutex));
11787 unique_id = diag_read_buffer->UniqueId;
11790 * Get the current buffer and look up the unique ID. The unique ID
11791 * should be there.
11793 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11794 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11795 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11796 return (DDI_FAILURE);
11799 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11802 * Make sure requested read is within limits
11804 if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
11805 pBuffer->buffer_data.size) {
11806 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11807 return (DDI_FAILURE);
11811 * Copy the requested data from DMA to the diag_read_buffer. The DMA
11812 * buffer that was allocated is one contiguous buffer.
11814 pData = (uint8_t *)(pBuffer->buffer_data.memp +
11815 diag_read_buffer->StartingOffset);
11816 (void) ddi_dma_sync(pBuffer->buffer_data.handle, 0, 0,
11817 DDI_DMA_SYNC_FORCPU);
11818 for (byte = 0; byte < diag_read_buffer->BytesToRead; byte++) {
11819 if (ddi_copyout(pData + byte, ioctl_buf + byte, 1, ioctl_mode)
11820 != 0) {
11821 return (DDI_FAILURE);
11824 diag_read_buffer->Status = 0;
11827 * Set or clear the Force Release flag.
11829 if (pBuffer->force_release) {
11830 diag_read_buffer->Flags |= MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11831 } else {
11832 diag_read_buffer->Flags &= ~MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11836 * If buffer is to be reregistered, make sure it's not already owned by
11837 * firmware first.
11839 status = DDI_SUCCESS;
11840 if (!pBuffer->owned_by_firmware) {
11841 if (diag_read_buffer->Flags & MPTSAS_FW_DIAG_FLAG_REREGISTER) {
11842 status = mptsas_post_fw_diag_buffer(mpt, pBuffer,
11843 return_code);
11847 return (status);
11850 static int
11851 mptsas_diag_release(mptsas_t *mpt, mptsas_fw_diag_release_t *diag_release,
11852 uint32_t *return_code)
11854 mptsas_fw_diagnostic_buffer_t *pBuffer;
11855 uint8_t i;
11856 uint32_t unique_id;
11857 int status;
11859 ASSERT(mutex_owned(&mpt->m_mutex));
11861 unique_id = diag_release->UniqueId;
11864 * Get the current buffer and look up the unique ID. The unique ID
11865 * should be there.
11867 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11868 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11869 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11870 return (DDI_FAILURE);
11873 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11876 * If buffer is not owned by firmware, it's already been released.
11878 if (!pBuffer->owned_by_firmware) {
11879 *return_code = MPTSAS_FW_DIAG_ERROR_ALREADY_RELEASED;
11880 return (DDI_FAILURE);
11884 * Release the buffer.
11886 status = mptsas_release_fw_diag_buffer(mpt, pBuffer, return_code,
11887 MPTSAS_FW_DIAG_TYPE_RELEASE);
11888 return (status);
11891 static int
11892 mptsas_do_diag_action(mptsas_t *mpt, uint32_t action, uint8_t *diag_action,
11893 uint32_t length, uint32_t *return_code, int ioctl_mode)
11895 mptsas_fw_diag_register_t diag_register;
11896 mptsas_fw_diag_unregister_t diag_unregister;
11897 mptsas_fw_diag_query_t diag_query;
11898 mptsas_diag_read_buffer_t diag_read_buffer;
11899 mptsas_fw_diag_release_t diag_release;
11900 int status = DDI_SUCCESS;
11901 uint32_t original_return_code, read_buf_len;
11903 ASSERT(mutex_owned(&mpt->m_mutex));
11905 original_return_code = *return_code;
11906 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11908 switch (action) {
11909 case MPTSAS_FW_DIAG_TYPE_REGISTER:
11910 if (!length) {
11911 *return_code =
11912 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11913 status = DDI_FAILURE;
11914 break;
11916 if (ddi_copyin(diag_action, &diag_register,
11917 sizeof (diag_register), ioctl_mode) != 0) {
11918 return (DDI_FAILURE);
11920 status = mptsas_diag_register(mpt, &diag_register,
11921 return_code);
11922 break;
11924 case MPTSAS_FW_DIAG_TYPE_UNREGISTER:
11925 if (length < sizeof (diag_unregister)) {
11926 *return_code =
11927 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11928 status = DDI_FAILURE;
11929 break;
11931 if (ddi_copyin(diag_action, &diag_unregister,
11932 sizeof (diag_unregister), ioctl_mode) != 0) {
11933 return (DDI_FAILURE);
11935 status = mptsas_diag_unregister(mpt, &diag_unregister,
11936 return_code);
11937 break;
11939 case MPTSAS_FW_DIAG_TYPE_QUERY:
11940 if (length < sizeof (diag_query)) {
11941 *return_code =
11942 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11943 status = DDI_FAILURE;
11944 break;
11946 if (ddi_copyin(diag_action, &diag_query,
11947 sizeof (diag_query), ioctl_mode) != 0) {
11948 return (DDI_FAILURE);
11950 status = mptsas_diag_query(mpt, &diag_query,
11951 return_code);
11952 if (status == DDI_SUCCESS) {
11953 if (ddi_copyout(&diag_query, diag_action,
11954 sizeof (diag_query), ioctl_mode) != 0) {
11955 return (DDI_FAILURE);
11958 break;
11960 case MPTSAS_FW_DIAG_TYPE_READ_BUFFER:
11961 if (ddi_copyin(diag_action, &diag_read_buffer,
11962 sizeof (diag_read_buffer) - 4, ioctl_mode) != 0) {
11963 return (DDI_FAILURE);
11965 read_buf_len = sizeof (diag_read_buffer) -
11966 sizeof (diag_read_buffer.DataBuffer) +
11967 diag_read_buffer.BytesToRead;
11968 if (length < read_buf_len) {
11969 *return_code =
11970 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11971 status = DDI_FAILURE;
11972 break;
11974 status = mptsas_diag_read_buffer(mpt,
11975 &diag_read_buffer, diag_action +
11976 sizeof (diag_read_buffer) - 4, return_code,
11977 ioctl_mode);
11978 if (status == DDI_SUCCESS) {
11979 if (ddi_copyout(&diag_read_buffer, diag_action,
11980 sizeof (diag_read_buffer) - 4, ioctl_mode)
11981 != 0) {
11982 return (DDI_FAILURE);
11985 break;
11987 case MPTSAS_FW_DIAG_TYPE_RELEASE:
11988 if (length < sizeof (diag_release)) {
11989 *return_code =
11990 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11991 status = DDI_FAILURE;
11992 break;
11994 if (ddi_copyin(diag_action, &diag_release,
11995 sizeof (diag_release), ioctl_mode) != 0) {
11996 return (DDI_FAILURE);
11998 status = mptsas_diag_release(mpt, &diag_release,
11999 return_code);
12000 break;
12002 default:
12003 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
12004 status = DDI_FAILURE;
12005 break;
12008 if ((status == DDI_FAILURE) &&
12009 (original_return_code == MPTSAS_FW_DIAG_NEW) &&
12010 (*return_code != MPTSAS_FW_DIAG_ERROR_SUCCESS)) {
12011 status = DDI_SUCCESS;
12014 return (status);
12017 static int
12018 mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *user_data, int mode)
12020 int status;
12021 mptsas_diag_action_t driver_data;
12023 ASSERT(mutex_owned(&mpt->m_mutex));
12026 * Copy the user data to a driver data buffer.
12028 if (ddi_copyin(user_data, &driver_data, sizeof (mptsas_diag_action_t),
12029 mode) == 0) {
12031 * Send diag action request if Action is valid
12033 if (driver_data.Action == MPTSAS_FW_DIAG_TYPE_REGISTER ||
12034 driver_data.Action == MPTSAS_FW_DIAG_TYPE_UNREGISTER ||
12035 driver_data.Action == MPTSAS_FW_DIAG_TYPE_QUERY ||
12036 driver_data.Action == MPTSAS_FW_DIAG_TYPE_READ_BUFFER ||
12037 driver_data.Action == MPTSAS_FW_DIAG_TYPE_RELEASE) {
12038 status = mptsas_do_diag_action(mpt, driver_data.Action,
12039 (void *)(uintptr_t)driver_data.PtrDiagAction,
12040 driver_data.Length, &driver_data.ReturnCode,
12041 mode);
12042 if (status == DDI_SUCCESS) {
12043 if (ddi_copyout(&driver_data.ReturnCode,
12044 &user_data->ReturnCode,
12045 sizeof (user_data->ReturnCode), mode)
12046 != 0) {
12047 status = EFAULT;
12048 } else {
12049 status = 0;
12051 } else {
12052 status = EIO;
12054 } else {
12055 status = EINVAL;
12057 } else {
12058 status = EFAULT;
12061 return (status);
12065 * This routine handles the "event query" ioctl.
12067 static int
12068 mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data, int mode,
12069 int *rval)
12071 int status;
12072 mptsas_event_query_t driverdata;
12073 uint8_t i;
12075 driverdata.Entries = MPTSAS_EVENT_QUEUE_SIZE;
12077 mutex_enter(&mpt->m_mutex);
12078 for (i = 0; i < 4; i++) {
12079 driverdata.Types[i] = mpt->m_event_mask[i];
12081 mutex_exit(&mpt->m_mutex);
12083 if (ddi_copyout(&driverdata, data, sizeof (driverdata), mode) != 0) {
12084 status = EFAULT;
12085 } else {
12086 *rval = MPTIOCTL_STATUS_GOOD;
12087 status = 0;
12090 return (status);
12094 * This routine handles the "event enable" ioctl.
12096 static int
12097 mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data, int mode,
12098 int *rval)
12100 int status;
12101 mptsas_event_enable_t driverdata;
12102 uint8_t i;
12104 if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
12105 mutex_enter(&mpt->m_mutex);
12106 for (i = 0; i < 4; i++) {
12107 mpt->m_event_mask[i] = driverdata.Types[i];
12109 mutex_exit(&mpt->m_mutex);
12111 *rval = MPTIOCTL_STATUS_GOOD;
12112 status = 0;
12113 } else {
12114 status = EFAULT;
12116 return (status);
12120 * This routine handles the "event report" ioctl.
12122 static int
12123 mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data, int mode,
12124 int *rval)
12126 int status;
12127 mptsas_event_report_t driverdata;
12129 mutex_enter(&mpt->m_mutex);
12131 if (ddi_copyin(&data->Size, &driverdata.Size, sizeof (driverdata.Size),
12132 mode) == 0) {
12133 if (driverdata.Size >= sizeof (mpt->m_events)) {
12134 if (ddi_copyout(mpt->m_events, data->Events,
12135 sizeof (mpt->m_events), mode) != 0) {
12136 status = EFAULT;
12137 } else {
12138 if (driverdata.Size > sizeof (mpt->m_events)) {
12139 driverdata.Size =
12140 sizeof (mpt->m_events);
12141 if (ddi_copyout(&driverdata.Size,
12142 &data->Size,
12143 sizeof (driverdata.Size),
12144 mode) != 0) {
12145 status = EFAULT;
12146 } else {
12147 *rval = MPTIOCTL_STATUS_GOOD;
12148 status = 0;
12150 } else {
12151 *rval = MPTIOCTL_STATUS_GOOD;
12152 status = 0;
12155 } else {
12156 *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
12157 status = 0;
12159 } else {
12160 status = EFAULT;
12163 mutex_exit(&mpt->m_mutex);
12164 return (status);
12167 static void
12168 mptsas_lookup_pci_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
12170 int *reg_data;
12171 uint_t reglen;
12174 * Lookup the 'reg' property and extract the other data
12176 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
12177 DDI_PROP_DONTPASS, "reg", &reg_data, &reglen) ==
12178 DDI_PROP_SUCCESS) {
12180 * Extract the PCI data from the 'reg' property first DWORD.
12181 * The entry looks like the following:
12182 * First DWORD:
12183 * Bits 0 - 7 8-bit Register number
12184 * Bits 8 - 10 3-bit Function number
12185 * Bits 11 - 15 5-bit Device number
12186 * Bits 16 - 23 8-bit Bus number
12187 * Bits 24 - 25 2-bit Address Space type identifier
12190 adapter_data->PciInformation.u.bits.BusNumber =
12191 (reg_data[0] & 0x00FF0000) >> 16;
12192 adapter_data->PciInformation.u.bits.DeviceNumber =
12193 (reg_data[0] & 0x0000F800) >> 11;
12194 adapter_data->PciInformation.u.bits.FunctionNumber =
12195 (reg_data[0] & 0x00000700) >> 8;
12196 ddi_prop_free((void *)reg_data);
12197 } else {
12199 * If we can't determine the PCI data then we fill in FF's for
12200 * the data to indicate this.
12202 adapter_data->PCIDeviceHwId = 0xFFFFFFFF;
12203 adapter_data->MpiPortNumber = 0xFFFFFFFF;
12204 adapter_data->PciInformation.u.AsDWORD = 0xFFFFFFFF;
12208 * Saved in the mpt->m_fwversion
12210 adapter_data->MpiFirmwareVersion = mpt->m_fwversion;
12213 static void
12214 mptsas_read_adapter_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
12216 char *driver_verstr = MPTSAS_MOD_STRING;
12218 mptsas_lookup_pci_data(mpt, adapter_data);
12219 adapter_data->AdapterType = mpt->m_MPI25 ?
12220 MPTIOCTL_ADAPTER_TYPE_SAS3 :
12221 MPTIOCTL_ADAPTER_TYPE_SAS2;
12222 adapter_data->PCIDeviceHwId = (uint32_t)mpt->m_devid;
12223 adapter_data->PCIDeviceHwRev = (uint32_t)mpt->m_revid;
12224 adapter_data->SubSystemId = (uint32_t)mpt->m_ssid;
12225 adapter_data->SubsystemVendorId = (uint32_t)mpt->m_svid;
12226 (void) strcpy((char *)&adapter_data->DriverVersion[0], driver_verstr);
12227 adapter_data->BiosVersion = 0;
12228 (void) mptsas_get_bios_page3(mpt, &adapter_data->BiosVersion);
12231 static void
12232 mptsas_read_pci_info(mptsas_t *mpt, mptsas_pci_info_t *pci_info)
12234 int *reg_data, i;
12235 uint_t reglen;
12238 * Lookup the 'reg' property and extract the other data
12240 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
12241 DDI_PROP_DONTPASS, "reg", &reg_data, &reglen) ==
12242 DDI_PROP_SUCCESS) {
12244 * Extract the PCI data from the 'reg' property first DWORD.
12245 * The entry looks like the following:
12246 * First DWORD:
12247 * Bits 8 - 10 3-bit Function number
12248 * Bits 11 - 15 5-bit Device number
12249 * Bits 16 - 23 8-bit Bus number
12251 pci_info->BusNumber = (reg_data[0] & 0x00FF0000) >> 16;
12252 pci_info->DeviceNumber = (reg_data[0] & 0x0000F800) >> 11;
12253 pci_info->FunctionNumber = (reg_data[0] & 0x00000700) >> 8;
12254 ddi_prop_free((void *)reg_data);
12255 } else {
12257 * If we can't determine the PCI info then we fill in FF's for
12258 * the data to indicate this.
12260 pci_info->BusNumber = 0xFFFFFFFF;
12261 pci_info->DeviceNumber = 0xFF;
12262 pci_info->FunctionNumber = 0xFF;
12266 * Now get the interrupt vector and the pci header. The vector can
12267 * only be 0 right now. The header is the first 256 bytes of config
12268 * space.
12270 pci_info->InterruptVector = 0;
12271 for (i = 0; i < sizeof (pci_info->PciHeader); i++) {
12272 pci_info->PciHeader[i] = pci_config_get8(mpt->m_config_handle,
12277 static int
12278 mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data, int mode)
12280 int status = 0;
12281 mptsas_reg_access_t driverdata;
12283 mutex_enter(&mpt->m_mutex);
12284 if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
12285 switch (driverdata.Command) {
12287 * IO access is not supported.
12289 case REG_IO_READ:
12290 case REG_IO_WRITE:
12291 mptsas_log(mpt, CE_WARN, "IO access is not "
12292 "supported. Use memory access.");
12293 status = EINVAL;
12294 break;
12296 case REG_MEM_READ:
12297 driverdata.RegData = ddi_get32(mpt->m_datap,
12298 (uint32_t *)(void *)mpt->m_reg +
12299 driverdata.RegOffset);
12300 if (ddi_copyout(&driverdata.RegData,
12301 &data->RegData,
12302 sizeof (driverdata.RegData), mode) != 0) {
12303 mptsas_log(mpt, CE_WARN, "Register "
12304 "Read Failed");
12305 status = EFAULT;
12307 break;
12309 case REG_MEM_WRITE:
12310 ddi_put32(mpt->m_datap,
12311 (uint32_t *)(void *)mpt->m_reg +
12312 driverdata.RegOffset,
12313 driverdata.RegData);
12314 break;
12316 default:
12317 status = EINVAL;
12318 break;
12320 } else {
12321 status = EFAULT;
12324 mutex_exit(&mpt->m_mutex);
12325 return (status);
12328 static int
12329 led_control(mptsas_t *mpt, intptr_t data, int mode)
12331 int ret = 0;
12332 mptsas_led_control_t lc;
12333 mptsas_target_t *ptgt;
12335 if (ddi_copyin((void *)data, &lc, sizeof (lc), mode) != 0) {
12336 return (EFAULT);
12339 if ((lc.Command != MPTSAS_LEDCTL_FLAG_SET &&
12340 lc.Command != MPTSAS_LEDCTL_FLAG_GET) ||
12341 lc.Led < MPTSAS_LEDCTL_LED_MIN ||
12342 lc.Led > MPTSAS_LEDCTL_LED_MAX ||
12343 (lc.Command == MPTSAS_LEDCTL_FLAG_SET && lc.LedStatus != 0 &&
12344 lc.LedStatus != 1)) {
12345 return (EINVAL);
12348 if ((lc.Command == MPTSAS_LEDCTL_FLAG_SET && (mode & FWRITE) == 0) ||
12349 (lc.Command == MPTSAS_LEDCTL_FLAG_GET && (mode & FREAD) == 0))
12350 return (EACCES);
12352 /* Locate the target we're interrogating... */
12353 mutex_enter(&mpt->m_mutex);
12354 ptgt = refhash_linear_search(mpt->m_targets,
12355 mptsas_target_eval_slot, &lc);
12356 if (ptgt == NULL) {
12357 /* We could not find a target for that enclosure/slot. */
12358 mutex_exit(&mpt->m_mutex);
12359 return (ENOENT);
12362 if (lc.Command == MPTSAS_LEDCTL_FLAG_SET) {
12363 /* Update our internal LED state. */
12364 ptgt->m_led_status &= ~(1 << (lc.Led - 1));
12365 ptgt->m_led_status |= lc.LedStatus << (lc.Led - 1);
12367 /* Flush it to the controller. */
12368 ret = mptsas_flush_led_status(mpt, ptgt);
12369 mutex_exit(&mpt->m_mutex);
12370 return (ret);
12373 /* Return our internal LED state. */
12374 lc.LedStatus = (ptgt->m_led_status >> (lc.Led - 1)) & 1;
12375 mutex_exit(&mpt->m_mutex);
12377 if (ddi_copyout(&lc, (void *)data, sizeof (lc), mode) != 0) {
12378 return (EFAULT);
12381 return (0);
12384 static int
12385 get_disk_info(mptsas_t *mpt, intptr_t data, int mode)
12387 uint16_t i = 0;
12388 uint16_t count = 0;
12389 int ret = 0;
12390 mptsas_target_t *ptgt;
12391 mptsas_disk_info_t *di;
12392 STRUCT_DECL(mptsas_get_disk_info, gdi);
12394 if ((mode & FREAD) == 0)
12395 return (EACCES);
12397 STRUCT_INIT(gdi, get_udatamodel());
12399 if (ddi_copyin((void *)data, STRUCT_BUF(gdi), STRUCT_SIZE(gdi),
12400 mode) != 0) {
12401 return (EFAULT);
12404 /* Find out how many targets there are. */
12405 mutex_enter(&mpt->m_mutex);
12406 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12407 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12408 count++;
12410 mutex_exit(&mpt->m_mutex);
12413 * If we haven't been asked to copy out information on each target,
12414 * then just return the count.
12416 STRUCT_FSET(gdi, DiskCount, count);
12417 if (STRUCT_FGETP(gdi, PtrDiskInfoArray) == NULL)
12418 goto copy_out;
12421 * If we haven't been given a large enough buffer to copy out into,
12422 * let the caller know.
12424 if (STRUCT_FGET(gdi, DiskInfoArraySize) <
12425 count * sizeof (mptsas_disk_info_t)) {
12426 ret = ENOSPC;
12427 goto copy_out;
12430 di = kmem_zalloc(count * sizeof (mptsas_disk_info_t), KM_SLEEP);
12432 mutex_enter(&mpt->m_mutex);
12433 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12434 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12435 if (i >= count) {
12437 * The number of targets changed while we weren't
12438 * looking, so give up.
12440 refhash_rele(mpt->m_targets, ptgt);
12441 mutex_exit(&mpt->m_mutex);
12442 kmem_free(di, count * sizeof (mptsas_disk_info_t));
12443 return (EAGAIN);
12445 di[i].Instance = mpt->m_instance;
12446 di[i].Enclosure = ptgt->m_enclosure;
12447 di[i].Slot = ptgt->m_slot_num;
12448 di[i].SasAddress = ptgt->m_addr.mta_wwn;
12449 i++;
12451 mutex_exit(&mpt->m_mutex);
12452 STRUCT_FSET(gdi, DiskCount, i);
12454 /* Copy out the disk information to the caller. */
12455 if (ddi_copyout((void *)di, STRUCT_FGETP(gdi, PtrDiskInfoArray),
12456 i * sizeof (mptsas_disk_info_t), mode) != 0) {
12457 ret = EFAULT;
12460 kmem_free(di, count * sizeof (mptsas_disk_info_t));
12462 copy_out:
12463 if (ddi_copyout(STRUCT_BUF(gdi), (void *)data, STRUCT_SIZE(gdi),
12464 mode) != 0) {
12465 ret = EFAULT;
12468 return (ret);
12471 static int
12472 mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp,
12473 int *rval)
12475 int status = 0;
12476 mptsas_t *mpt;
12477 mptsas_update_flash_t flashdata;
12478 mptsas_pass_thru_t passthru_data;
12479 mptsas_adapter_data_t adapter_data;
12480 mptsas_pci_info_t pci_info;
12481 int copylen;
12483 int iport_flag = 0;
12484 dev_info_t *dip = NULL;
12485 mptsas_phymask_t phymask = 0;
12486 struct devctl_iocdata *dcp = NULL;
12487 char *addr = NULL;
12488 mptsas_target_t *ptgt = NULL;
12490 *rval = MPTIOCTL_STATUS_GOOD;
12491 if (secpolicy_sys_config(credp, B_FALSE) != 0) {
12492 return (EPERM);
12495 mpt = ddi_get_soft_state(mptsas_state, MINOR2INST(getminor(dev)));
12496 if (mpt == NULL) {
12498 * Called from iport node, get the states
12500 iport_flag = 1;
12501 dip = mptsas_get_dip_from_dev(dev, &phymask);
12502 if (dip == NULL) {
12503 return (ENXIO);
12505 mpt = DIP2MPT(dip);
12507 /* Make sure power level is D0 before accessing registers */
12508 mutex_enter(&mpt->m_mutex);
12509 if (mpt->m_options & MPTSAS_OPT_PM) {
12510 (void) pm_busy_component(mpt->m_dip, 0);
12511 if (mpt->m_power_level != PM_LEVEL_D0) {
12512 mutex_exit(&mpt->m_mutex);
12513 if (pm_raise_power(mpt->m_dip, 0, PM_LEVEL_D0) !=
12514 DDI_SUCCESS) {
12515 mptsas_log(mpt, CE_WARN,
12516 "mptsas%d: mptsas_ioctl: Raise power "
12517 "request failed.", mpt->m_instance);
12518 (void) pm_idle_component(mpt->m_dip, 0);
12519 return (ENXIO);
12521 } else {
12522 mutex_exit(&mpt->m_mutex);
12524 } else {
12525 mutex_exit(&mpt->m_mutex);
12528 if (iport_flag) {
12529 status = scsi_hba_ioctl(dev, cmd, data, mode, credp, rval);
12530 if (status != 0) {
12531 goto out;
12534 * The following code control the OK2RM LED, it doesn't affect
12535 * the ioctl return status.
12537 if ((cmd == DEVCTL_DEVICE_ONLINE) ||
12538 (cmd == DEVCTL_DEVICE_OFFLINE)) {
12539 if (ndi_dc_allochdl((void *)data, &dcp) !=
12540 NDI_SUCCESS) {
12541 goto out;
12543 addr = ndi_dc_getaddr(dcp);
12544 ptgt = mptsas_addr_to_ptgt(mpt, addr, phymask);
12545 if (ptgt == NULL) {
12546 NDBG14(("mptsas_ioctl led control: tgt %s not "
12547 "found", addr));
12548 ndi_dc_freehdl(dcp);
12549 goto out;
12551 mutex_enter(&mpt->m_mutex);
12552 if (cmd == DEVCTL_DEVICE_ONLINE) {
12553 ptgt->m_tgt_unconfigured = 0;
12554 } else if (cmd == DEVCTL_DEVICE_OFFLINE) {
12555 ptgt->m_tgt_unconfigured = 1;
12557 if (cmd == DEVCTL_DEVICE_OFFLINE) {
12558 ptgt->m_led_status |=
12559 (1 << (MPTSAS_LEDCTL_LED_OK2RM - 1));
12560 } else {
12561 ptgt->m_led_status &=
12562 ~(1 << (MPTSAS_LEDCTL_LED_OK2RM - 1));
12564 (void) mptsas_flush_led_status(mpt, ptgt);
12565 mutex_exit(&mpt->m_mutex);
12566 ndi_dc_freehdl(dcp);
12568 goto out;
12570 switch (cmd) {
12571 case MPTIOCTL_GET_DISK_INFO:
12572 status = get_disk_info(mpt, data, mode);
12573 break;
12574 case MPTIOCTL_LED_CONTROL:
12575 status = led_control(mpt, data, mode);
12576 break;
12577 case MPTIOCTL_UPDATE_FLASH:
12578 if (ddi_copyin((void *)data, &flashdata,
12579 sizeof (struct mptsas_update_flash), mode)) {
12580 status = EFAULT;
12581 break;
12584 mutex_enter(&mpt->m_mutex);
12585 if (mptsas_update_flash(mpt,
12586 (caddr_t)(long)flashdata.PtrBuffer,
12587 flashdata.ImageSize, flashdata.ImageType, mode)) {
12588 status = EFAULT;
12592 * Reset the chip to start using the new
12593 * firmware. Reset if failed also.
12595 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12596 if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
12597 status = EFAULT;
12599 mutex_exit(&mpt->m_mutex);
12600 break;
12601 case MPTIOCTL_PASS_THRU:
12603 * The user has requested to pass through a command to
12604 * be executed by the MPT firmware. Call our routine
12605 * which does this. Only allow one passthru IOCTL at
12606 * one time. Other threads will block on
12607 * m_passthru_mutex, which is of adaptive variant.
12609 if (ddi_copyin((void *)data, &passthru_data,
12610 sizeof (mptsas_pass_thru_t), mode)) {
12611 status = EFAULT;
12612 break;
12614 mutex_enter(&mpt->m_passthru_mutex);
12615 mutex_enter(&mpt->m_mutex);
12616 status = mptsas_pass_thru(mpt, &passthru_data, mode);
12617 mutex_exit(&mpt->m_mutex);
12618 mutex_exit(&mpt->m_passthru_mutex);
12620 break;
12621 case MPTIOCTL_GET_ADAPTER_DATA:
12623 * The user has requested to read adapter data. Call
12624 * our routine which does this.
12626 bzero(&adapter_data, sizeof (mptsas_adapter_data_t));
12627 if (ddi_copyin((void *)data, (void *)&adapter_data,
12628 sizeof (mptsas_adapter_data_t), mode)) {
12629 status = EFAULT;
12630 break;
12632 if (adapter_data.StructureLength >=
12633 sizeof (mptsas_adapter_data_t)) {
12634 adapter_data.StructureLength = (uint32_t)
12635 sizeof (mptsas_adapter_data_t);
12636 copylen = sizeof (mptsas_adapter_data_t);
12637 mutex_enter(&mpt->m_mutex);
12638 mptsas_read_adapter_data(mpt, &adapter_data);
12639 mutex_exit(&mpt->m_mutex);
12640 } else {
12641 adapter_data.StructureLength = (uint32_t)
12642 sizeof (mptsas_adapter_data_t);
12643 copylen = sizeof (adapter_data.StructureLength);
12644 *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
12646 if (ddi_copyout((void *)(&adapter_data), (void *)data,
12647 copylen, mode) != 0) {
12648 status = EFAULT;
12650 break;
12651 case MPTIOCTL_GET_PCI_INFO:
12653 * The user has requested to read pci info. Call
12654 * our routine which does this.
12656 bzero(&pci_info, sizeof (mptsas_pci_info_t));
12657 mutex_enter(&mpt->m_mutex);
12658 mptsas_read_pci_info(mpt, &pci_info);
12659 mutex_exit(&mpt->m_mutex);
12660 if (ddi_copyout((void *)(&pci_info), (void *)data,
12661 sizeof (mptsas_pci_info_t), mode) != 0) {
12662 status = EFAULT;
12664 break;
12665 case MPTIOCTL_RESET_ADAPTER:
12666 mutex_enter(&mpt->m_mutex);
12667 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12668 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
12669 mptsas_log(mpt, CE_WARN, "reset adapter IOCTL "
12670 "failed");
12671 status = EFAULT;
12673 mutex_exit(&mpt->m_mutex);
12674 break;
12675 case MPTIOCTL_DIAG_ACTION:
12677 * The user has done a diag buffer action. Call our
12678 * routine which does this. Only allow one diag action
12679 * at one time.
12681 mutex_enter(&mpt->m_mutex);
12682 if (mpt->m_diag_action_in_progress) {
12683 mutex_exit(&mpt->m_mutex);
12684 return (EBUSY);
12686 mpt->m_diag_action_in_progress = 1;
12687 status = mptsas_diag_action(mpt,
12688 (mptsas_diag_action_t *)data, mode);
12689 mpt->m_diag_action_in_progress = 0;
12690 mutex_exit(&mpt->m_mutex);
12691 break;
12692 case MPTIOCTL_EVENT_QUERY:
12694 * The user has done an event query. Call our routine
12695 * which does this.
12697 status = mptsas_event_query(mpt,
12698 (mptsas_event_query_t *)data, mode, rval);
12699 break;
12700 case MPTIOCTL_EVENT_ENABLE:
12702 * The user has done an event enable. Call our routine
12703 * which does this.
12705 status = mptsas_event_enable(mpt,
12706 (mptsas_event_enable_t *)data, mode, rval);
12707 break;
12708 case MPTIOCTL_EVENT_REPORT:
12710 * The user has done an event report. Call our routine
12711 * which does this.
12713 status = mptsas_event_report(mpt,
12714 (mptsas_event_report_t *)data, mode, rval);
12715 break;
12716 case MPTIOCTL_REG_ACCESS:
12718 * The user has requested register access. Call our
12719 * routine which does this.
12721 status = mptsas_reg_access(mpt,
12722 (mptsas_reg_access_t *)data, mode);
12723 break;
12724 default:
12725 status = scsi_hba_ioctl(dev, cmd, data, mode, credp,
12726 rval);
12727 break;
12730 out:
12731 return (status);
12735 mptsas_restart_ioc(mptsas_t *mpt)
12737 int rval = DDI_SUCCESS;
12738 mptsas_target_t *ptgt = NULL;
12740 ASSERT(mutex_owned(&mpt->m_mutex));
12743 * Set a flag telling I/O path that we're processing a reset. This is
12744 * needed because after the reset is complete, the hash table still
12745 * needs to be rebuilt. If I/Os are started before the hash table is
12746 * rebuilt, I/O errors will occur. This flag allows I/Os to be marked
12747 * so that they can be retried.
12749 mpt->m_in_reset = TRUE;
12752 * Set all throttles to HOLD
12754 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12755 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12756 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
12760 * Disable interrupts
12762 MPTSAS_DISABLE_INTR(mpt);
12765 * Abort all commands: outstanding commands, commands in waitq and
12766 * tx_waitq.
12768 mptsas_flush_hba(mpt);
12771 * Reinitialize the chip.
12773 if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
12774 rval = DDI_FAILURE;
12778 * Enable interrupts again
12780 MPTSAS_ENABLE_INTR(mpt);
12783 * If mptsas_init_chip was successful, update the driver data.
12785 if (rval == DDI_SUCCESS) {
12786 mptsas_update_driver_data(mpt);
12790 * Reset the throttles
12792 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12793 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12794 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
12797 mptsas_doneq_empty(mpt);
12798 mptsas_restart_hba(mpt);
12800 if (rval != DDI_SUCCESS) {
12801 mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
12802 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
12806 * Clear the reset flag so that I/Os can continue.
12808 mpt->m_in_reset = FALSE;
12810 return (rval);
12813 static int
12814 mptsas_init_chip(mptsas_t *mpt, int first_time)
12816 ddi_dma_cookie_t cookie;
12817 uint32_t i;
12818 int rval;
12821 * Check to see if the firmware image is valid
12823 if (ddi_get32(mpt->m_datap, &mpt->m_reg->HostDiagnostic) &
12824 MPI2_DIAG_FLASH_BAD_SIG) {
12825 mptsas_log(mpt, CE_WARN, "mptsas bad flash signature!");
12826 goto fail;
12830 * Reset the chip
12832 rval = mptsas_ioc_reset(mpt, first_time);
12833 if (rval == MPTSAS_RESET_FAIL) {
12834 mptsas_log(mpt, CE_WARN, "hard reset failed!");
12835 goto fail;
12838 if ((rval == MPTSAS_SUCCESS_MUR) && (!first_time)) {
12839 goto mur;
12842 * Setup configuration space
12844 if (mptsas_config_space_init(mpt) == FALSE) {
12845 mptsas_log(mpt, CE_WARN, "mptsas_config_space_init "
12846 "failed!");
12847 goto fail;
12851 * IOC facts can change after a diag reset so all buffers that are
12852 * based on these numbers must be de-allocated and re-allocated. Get
12853 * new IOC facts each time chip is initialized.
12855 if (mptsas_ioc_get_facts(mpt) == DDI_FAILURE) {
12856 mptsas_log(mpt, CE_WARN, "mptsas_ioc_get_facts failed");
12857 goto fail;
12860 mpt->m_targets = refhash_create(MPTSAS_TARGET_BUCKET_COUNT,
12861 mptsas_target_addr_hash, mptsas_target_addr_cmp,
12862 mptsas_target_free, sizeof (mptsas_target_t),
12863 offsetof(mptsas_target_t, m_link),
12864 offsetof(mptsas_target_t, m_addr), KM_SLEEP);
12866 if (mptsas_alloc_active_slots(mpt, KM_SLEEP)) {
12867 goto fail;
12870 * Allocate request message frames, reply free queue, reply descriptor
12871 * post queue, and reply message frames using latest IOC facts.
12873 if (mptsas_alloc_request_frames(mpt) == DDI_FAILURE) {
12874 mptsas_log(mpt, CE_WARN, "mptsas_alloc_request_frames failed");
12875 goto fail;
12877 if (mptsas_alloc_sense_bufs(mpt) == DDI_FAILURE) {
12878 mptsas_log(mpt, CE_WARN, "mptsas_alloc_sense_bufs failed");
12879 goto fail;
12881 if (mptsas_alloc_free_queue(mpt) == DDI_FAILURE) {
12882 mptsas_log(mpt, CE_WARN, "mptsas_alloc_free_queue failed!");
12883 goto fail;
12885 if (mptsas_alloc_post_queue(mpt) == DDI_FAILURE) {
12886 mptsas_log(mpt, CE_WARN, "mptsas_alloc_post_queue failed!");
12887 goto fail;
12889 if (mptsas_alloc_reply_frames(mpt) == DDI_FAILURE) {
12890 mptsas_log(mpt, CE_WARN, "mptsas_alloc_reply_frames failed!");
12891 goto fail;
12894 mur:
12896 * Re-Initialize ioc to operational state
12898 if (mptsas_ioc_init(mpt) == DDI_FAILURE) {
12899 mptsas_log(mpt, CE_WARN, "mptsas_ioc_init failed");
12900 goto fail;
12903 mptsas_alloc_reply_args(mpt);
12906 * Initialize reply post index. Reply free index is initialized after
12907 * the next loop.
12909 mpt->m_post_index = 0;
12912 * Initialize the Reply Free Queue with the physical addresses of our
12913 * reply frames.
12915 cookie.dmac_address = mpt->m_reply_frame_dma_addr & 0xffffffffu;
12916 for (i = 0; i < mpt->m_max_replies; i++) {
12917 ddi_put32(mpt->m_acc_free_queue_hdl,
12918 &((uint32_t *)(void *)mpt->m_free_queue)[i],
12919 cookie.dmac_address);
12920 cookie.dmac_address += mpt->m_reply_frame_size;
12922 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
12923 DDI_DMA_SYNC_FORDEV);
12926 * Initialize the reply free index to one past the last frame on the
12927 * queue. This will signify that the queue is empty to start with.
12929 mpt->m_free_index = i;
12930 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex, i);
12933 * Initialize the reply post queue to 0xFFFFFFFF,0xFFFFFFFF's.
12935 for (i = 0; i < mpt->m_post_queue_depth; i++) {
12936 ddi_put64(mpt->m_acc_post_queue_hdl,
12937 &((uint64_t *)(void *)mpt->m_post_queue)[i],
12938 0xFFFFFFFFFFFFFFFF);
12940 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
12941 DDI_DMA_SYNC_FORDEV);
12944 * Enable ports
12946 if (mptsas_ioc_enable_port(mpt) == DDI_FAILURE) {
12947 mptsas_log(mpt, CE_WARN, "mptsas_ioc_enable_port failed");
12948 goto fail;
12952 * enable events
12954 if (mptsas_ioc_enable_event_notification(mpt)) {
12955 mptsas_log(mpt, CE_WARN,
12956 "mptsas_ioc_enable_event_notification failed");
12957 goto fail;
12961 * We need checks in attach and these.
12962 * chip_init is called in mult. places
12965 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
12966 DDI_SUCCESS) ||
12967 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl) !=
12968 DDI_SUCCESS) ||
12969 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
12970 DDI_SUCCESS) ||
12971 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
12972 DDI_SUCCESS) ||
12973 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
12974 DDI_SUCCESS) ||
12975 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
12976 DDI_SUCCESS)) {
12977 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
12978 goto fail;
12981 /* Check all acc handles */
12982 if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
12983 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
12984 DDI_SUCCESS) ||
12985 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl) !=
12986 DDI_SUCCESS) ||
12987 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
12988 DDI_SUCCESS) ||
12989 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
12990 DDI_SUCCESS) ||
12991 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
12992 DDI_SUCCESS) ||
12993 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
12994 DDI_SUCCESS) ||
12995 (mptsas_check_acc_handle(mpt->m_config_handle) !=
12996 DDI_SUCCESS)) {
12997 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
12998 goto fail;
13001 return (DDI_SUCCESS);
13003 fail:
13004 return (DDI_FAILURE);
13007 static int
13008 mptsas_get_pci_cap(mptsas_t *mpt)
13010 ushort_t caps_ptr, cap, cap_count;
13012 if (mpt->m_config_handle == NULL)
13013 return (FALSE);
13015 * Check if capabilities list is supported and if so,
13016 * get initial capabilities pointer and clear bits 0,1.
13018 if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT)
13019 & PCI_STAT_CAP) {
13020 caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
13021 PCI_CONF_CAP_PTR), 4);
13022 } else {
13023 caps_ptr = PCI_CAP_NEXT_PTR_NULL;
13027 * Walk capabilities if supported.
13029 for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
13032 * Check that we haven't exceeded the maximum number of
13033 * capabilities and that the pointer is in a valid range.
13035 if (++cap_count > 48) {
13036 mptsas_log(mpt, CE_WARN,
13037 "too many device capabilities.\n");
13038 break;
13040 if (caps_ptr < 64) {
13041 mptsas_log(mpt, CE_WARN,
13042 "capabilities pointer 0x%x out of range.\n",
13043 caps_ptr);
13044 break;
13048 * Get next capability and check that it is valid.
13049 * For now, we only support power management.
13051 cap = pci_config_get8(mpt->m_config_handle, caps_ptr);
13052 switch (cap) {
13053 case PCI_CAP_ID_PM:
13054 mptsas_log(mpt, CE_NOTE,
13055 "?mptsas%d supports power management.\n",
13056 mpt->m_instance);
13057 mpt->m_options |= MPTSAS_OPT_PM;
13059 /* Save PMCSR offset */
13060 mpt->m_pmcsr_offset = caps_ptr + PCI_PMCSR;
13061 break;
13063 * The following capabilities are valid. Any others
13064 * will cause a message to be logged.
13066 case PCI_CAP_ID_VPD:
13067 case PCI_CAP_ID_MSI:
13068 case PCI_CAP_ID_PCIX:
13069 case PCI_CAP_ID_PCI_E:
13070 case PCI_CAP_ID_MSI_X:
13071 break;
13072 default:
13073 mptsas_log(mpt, CE_NOTE,
13074 "?mptsas%d unrecognized capability "
13075 "0x%x.\n", mpt->m_instance, cap);
13076 break;
13080 * Get next capabilities pointer and clear bits 0,1.
13082 caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
13083 (caps_ptr + PCI_CAP_NEXT_PTR)), 4);
13085 return (TRUE);
13088 static int
13089 mptsas_init_pm(mptsas_t *mpt)
13091 char pmc_name[16];
13092 char *pmc[] = {
13093 NULL,
13094 "0=Off (PCI D3 State)",
13095 "3=On (PCI D0 State)",
13096 NULL
13098 uint16_t pmcsr_stat;
13100 if (mptsas_get_pci_cap(mpt) == FALSE) {
13101 return (DDI_FAILURE);
13104 * If PCI's capability does not support PM, then don't need
13105 * to registe the pm-components
13107 if (!(mpt->m_options & MPTSAS_OPT_PM))
13108 return (DDI_SUCCESS);
13110 * If power management is supported by this chip, create
13111 * pm-components property for the power management framework
13113 (void) sprintf(pmc_name, "NAME=mptsas%d", mpt->m_instance);
13114 pmc[0] = pmc_name;
13115 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, mpt->m_dip,
13116 "pm-components", pmc, 3) != DDI_PROP_SUCCESS) {
13117 mpt->m_options &= ~MPTSAS_OPT_PM;
13118 mptsas_log(mpt, CE_WARN,
13119 "mptsas%d: pm-component property creation failed.",
13120 mpt->m_instance);
13121 return (DDI_FAILURE);
13125 * Power on device.
13127 (void) pm_busy_component(mpt->m_dip, 0);
13128 pmcsr_stat = pci_config_get16(mpt->m_config_handle,
13129 mpt->m_pmcsr_offset);
13130 if ((pmcsr_stat & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_D0) {
13131 mptsas_log(mpt, CE_WARN, "mptsas%d: Power up the device",
13132 mpt->m_instance);
13133 pci_config_put16(mpt->m_config_handle, mpt->m_pmcsr_offset,
13134 PCI_PMCSR_D0);
13136 if (pm_power_has_changed(mpt->m_dip, 0, PM_LEVEL_D0) != DDI_SUCCESS) {
13137 mptsas_log(mpt, CE_WARN, "pm_power_has_changed failed");
13138 return (DDI_FAILURE);
13140 mpt->m_power_level = PM_LEVEL_D0;
13142 * Set pm idle delay.
13144 mpt->m_pm_idle_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
13145 mpt->m_dip, 0, "mptsas-pm-idle-delay", MPTSAS_PM_IDLE_TIMEOUT);
13147 return (DDI_SUCCESS);
13150 static int
13151 mptsas_register_intrs(mptsas_t *mpt)
13153 dev_info_t *dip;
13154 int intr_types;
13156 dip = mpt->m_dip;
13158 /* Get supported interrupt types */
13159 if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
13160 mptsas_log(mpt, CE_WARN, "ddi_intr_get_supported_types "
13161 "failed\n");
13162 return (FALSE);
13165 NDBG6(("ddi_intr_get_supported_types() returned: 0x%x", intr_types));
13168 * Try MSI, but fall back to FIXED
13170 if (mptsas_enable_msi && (intr_types & DDI_INTR_TYPE_MSI)) {
13171 if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_MSI) == DDI_SUCCESS) {
13172 NDBG0(("Using MSI interrupt type"));
13173 mpt->m_intr_type = DDI_INTR_TYPE_MSI;
13174 return (TRUE);
13177 if (intr_types & DDI_INTR_TYPE_FIXED) {
13178 if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_FIXED) == DDI_SUCCESS) {
13179 NDBG0(("Using FIXED interrupt type"));
13180 mpt->m_intr_type = DDI_INTR_TYPE_FIXED;
13181 return (TRUE);
13182 } else {
13183 NDBG0(("FIXED interrupt registration failed"));
13184 return (FALSE);
13188 return (FALSE);
13191 static void
13192 mptsas_unregister_intrs(mptsas_t *mpt)
13194 mptsas_rem_intrs(mpt);
13198 * mptsas_add_intrs:
13200 * Register FIXED or MSI interrupts.
13202 static int
13203 mptsas_add_intrs(mptsas_t *mpt, int intr_type)
13205 dev_info_t *dip = mpt->m_dip;
13206 int avail, actual, count = 0;
13207 int i, flag, ret;
13209 NDBG6(("mptsas_add_intrs:interrupt type 0x%x", intr_type));
13211 /* Get number of interrupts */
13212 ret = ddi_intr_get_nintrs(dip, intr_type, &count);
13213 if ((ret != DDI_SUCCESS) || (count <= 0)) {
13214 mptsas_log(mpt, CE_WARN, "ddi_intr_get_nintrs() failed, "
13215 "ret %d count %d\n", ret, count);
13217 return (DDI_FAILURE);
13220 /* Get number of available interrupts */
13221 ret = ddi_intr_get_navail(dip, intr_type, &avail);
13222 if ((ret != DDI_SUCCESS) || (avail == 0)) {
13223 mptsas_log(mpt, CE_WARN, "ddi_intr_get_navail() failed, "
13224 "ret %d avail %d\n", ret, avail);
13226 return (DDI_FAILURE);
13229 if (avail < count) {
13230 mptsas_log(mpt, CE_NOTE, "ddi_intr_get_nvail returned %d, "
13231 "navail() returned %d", count, avail);
13234 /* Mpt only have one interrupt routine */
13235 if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
13236 count = 1;
13239 /* Allocate an array of interrupt handles */
13240 mpt->m_intr_size = count * sizeof (ddi_intr_handle_t);
13241 mpt->m_htable = kmem_alloc(mpt->m_intr_size, KM_SLEEP);
13243 flag = DDI_INTR_ALLOC_NORMAL;
13245 /* call ddi_intr_alloc() */
13246 ret = ddi_intr_alloc(dip, mpt->m_htable, intr_type, 0,
13247 count, &actual, flag);
13249 if ((ret != DDI_SUCCESS) || (actual == 0)) {
13250 mptsas_log(mpt, CE_WARN, "ddi_intr_alloc() failed, ret %d\n",
13251 ret);
13252 kmem_free(mpt->m_htable, mpt->m_intr_size);
13253 return (DDI_FAILURE);
13256 /* use interrupt count returned or abort? */
13257 if (actual < count) {
13258 mptsas_log(mpt, CE_NOTE, "Requested: %d, Received: %d\n",
13259 count, actual);
13262 mpt->m_intr_cnt = actual;
13265 * Get priority for first msi, assume remaining are all the same
13267 if ((ret = ddi_intr_get_pri(mpt->m_htable[0],
13268 &mpt->m_intr_pri)) != DDI_SUCCESS) {
13269 mptsas_log(mpt, CE_WARN, "ddi_intr_get_pri() failed %d\n", ret);
13271 /* Free already allocated intr */
13272 for (i = 0; i < actual; i++) {
13273 (void) ddi_intr_free(mpt->m_htable[i]);
13276 kmem_free(mpt->m_htable, mpt->m_intr_size);
13277 return (DDI_FAILURE);
13280 /* Test for high level mutex */
13281 if (mpt->m_intr_pri >= ddi_intr_get_hilevel_pri()) {
13282 mptsas_log(mpt, CE_WARN, "mptsas_add_intrs: "
13283 "Hi level interrupt not supported\n");
13285 /* Free already allocated intr */
13286 for (i = 0; i < actual; i++) {
13287 (void) ddi_intr_free(mpt->m_htable[i]);
13290 kmem_free(mpt->m_htable, mpt->m_intr_size);
13291 return (DDI_FAILURE);
13294 /* Call ddi_intr_add_handler() */
13295 for (i = 0; i < actual; i++) {
13296 if ((ret = ddi_intr_add_handler(mpt->m_htable[i], mptsas_intr,
13297 (caddr_t)mpt, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
13298 mptsas_log(mpt, CE_WARN, "ddi_intr_add_handler() "
13299 "failed %d\n", ret);
13301 /* Free already allocated intr */
13302 for (i = 0; i < actual; i++) {
13303 (void) ddi_intr_free(mpt->m_htable[i]);
13306 kmem_free(mpt->m_htable, mpt->m_intr_size);
13307 return (DDI_FAILURE);
13311 if ((ret = ddi_intr_get_cap(mpt->m_htable[0], &mpt->m_intr_cap))
13312 != DDI_SUCCESS) {
13313 mptsas_log(mpt, CE_WARN, "ddi_intr_get_cap() failed %d\n", ret);
13315 /* Free already allocated intr */
13316 for (i = 0; i < actual; i++) {
13317 (void) ddi_intr_free(mpt->m_htable[i]);
13320 kmem_free(mpt->m_htable, mpt->m_intr_size);
13321 return (DDI_FAILURE);
13325 * Enable interrupts
13327 if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
13328 /* Call ddi_intr_block_enable() for MSI interrupts */
13329 (void) ddi_intr_block_enable(mpt->m_htable, mpt->m_intr_cnt);
13330 } else {
13331 /* Call ddi_intr_enable for MSI or FIXED interrupts */
13332 for (i = 0; i < mpt->m_intr_cnt; i++) {
13333 (void) ddi_intr_enable(mpt->m_htable[i]);
13336 return (DDI_SUCCESS);
13340 * mptsas_rem_intrs:
13342 * Unregister FIXED or MSI interrupts
13344 static void
13345 mptsas_rem_intrs(mptsas_t *mpt)
13347 int i;
13349 NDBG6(("mptsas_rem_intrs"));
13351 /* Disable all interrupts */
13352 if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
13353 /* Call ddi_intr_block_disable() */
13354 (void) ddi_intr_block_disable(mpt->m_htable, mpt->m_intr_cnt);
13355 } else {
13356 for (i = 0; i < mpt->m_intr_cnt; i++) {
13357 (void) ddi_intr_disable(mpt->m_htable[i]);
13361 /* Call ddi_intr_remove_handler() */
13362 for (i = 0; i < mpt->m_intr_cnt; i++) {
13363 (void) ddi_intr_remove_handler(mpt->m_htable[i]);
13364 (void) ddi_intr_free(mpt->m_htable[i]);
13367 kmem_free(mpt->m_htable, mpt->m_intr_size);
13371 * The IO fault service error handling callback function
13373 /*ARGSUSED*/
13374 static int
13375 mptsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
13378 * as the driver can always deal with an error in any dma or
13379 * access handle, we can just return the fme_status value.
13381 pci_ereport_post(dip, err, NULL);
13382 return (err->fme_status);
13386 * mptsas_fm_init - initialize fma capabilities and register with IO
13387 * fault services.
13389 static void
13390 mptsas_fm_init(mptsas_t *mpt)
13393 * Need to change iblock to priority for new MSI intr
13395 ddi_iblock_cookie_t fm_ibc;
13397 /* Only register with IO Fault Services if we have some capability */
13398 if (mpt->m_fm_capabilities) {
13399 /* Adjust access and dma attributes for FMA */
13400 mpt->m_reg_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
13401 mpt->m_msg_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
13402 mpt->m_io_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
13405 * Register capabilities with IO Fault Services.
13406 * mpt->m_fm_capabilities will be updated to indicate
13407 * capabilities actually supported (not requested.)
13409 ddi_fm_init(mpt->m_dip, &mpt->m_fm_capabilities, &fm_ibc);
13412 * Initialize pci ereport capabilities if ereport
13413 * capable (should always be.)
13415 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
13416 DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13417 pci_ereport_setup(mpt->m_dip);
13421 * Register error callback if error callback capable.
13423 if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13424 ddi_fm_handler_register(mpt->m_dip,
13425 mptsas_fm_error_cb, (void *) mpt);
13431 * mptsas_fm_fini - Releases fma capabilities and un-registers with IO
13432 * fault services.
13435 static void
13436 mptsas_fm_fini(mptsas_t *mpt)
13438 /* Only unregister FMA capabilities if registered */
13439 if (mpt->m_fm_capabilities) {
13442 * Un-register error callback if error callback capable.
13445 if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13446 ddi_fm_handler_unregister(mpt->m_dip);
13450 * Release any resources allocated by pci_ereport_setup()
13453 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
13454 DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13455 pci_ereport_teardown(mpt->m_dip);
13458 /* Unregister from IO Fault Services */
13459 ddi_fm_fini(mpt->m_dip);
13461 /* Adjust access and dma attributes for FMA */
13462 mpt->m_reg_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
13463 mpt->m_msg_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
13464 mpt->m_io_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
13470 mptsas_check_acc_handle(ddi_acc_handle_t handle)
13472 ddi_fm_error_t de;
13474 if (handle == NULL)
13475 return (DDI_FAILURE);
13476 ddi_fm_acc_err_get(handle, &de, DDI_FME_VER0);
13477 return (de.fme_status);
13481 mptsas_check_dma_handle(ddi_dma_handle_t handle)
13483 ddi_fm_error_t de;
13485 if (handle == NULL)
13486 return (DDI_FAILURE);
13487 ddi_fm_dma_err_get(handle, &de, DDI_FME_VER0);
13488 return (de.fme_status);
13491 void
13492 mptsas_fm_ereport(mptsas_t *mpt, char *detail)
13494 uint64_t ena;
13495 char buf[FM_MAX_CLASS];
13497 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
13498 ena = fm_ena_generate(0, FM_ENA_FMT1);
13499 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities)) {
13500 ddi_fm_ereport_post(mpt->m_dip, buf, ena, DDI_NOSLEEP,
13501 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
13505 static int
13506 mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
13507 uint16_t *dev_handle, mptsas_target_t **pptgt)
13509 int rval;
13510 uint32_t dev_info;
13511 uint64_t sas_wwn;
13512 mptsas_phymask_t phymask;
13513 uint8_t physport, phynum, config, disk;
13514 uint64_t devicename;
13515 uint16_t pdev_hdl;
13516 mptsas_target_t *tmp_tgt = NULL;
13517 uint16_t bay_num, enclosure, io_flags;
13519 ASSERT(*pptgt == NULL);
13521 rval = mptsas_get_sas_device_page0(mpt, page_address, dev_handle,
13522 &sas_wwn, &dev_info, &physport, &phynum, &pdev_hdl,
13523 &bay_num, &enclosure, &io_flags);
13524 if (rval != DDI_SUCCESS) {
13525 rval = DEV_INFO_FAIL_PAGE0;
13526 return (rval);
13529 if ((dev_info & (MPI2_SAS_DEVICE_INFO_SSP_TARGET |
13530 MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13531 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) == NULL) {
13532 rval = DEV_INFO_WRONG_DEVICE_TYPE;
13533 return (rval);
13537 * Check if the dev handle is for a Phys Disk. If so, set return value
13538 * and exit. Don't add Phys Disks to hash.
13540 for (config = 0; config < mpt->m_num_raid_configs; config++) {
13541 for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
13542 if (*dev_handle == mpt->m_raidconfig[config].
13543 m_physdisk_devhdl[disk]) {
13544 rval = DEV_INFO_PHYS_DISK;
13545 return (rval);
13551 * Get SATA Device Name from SAS device page0 for
13552 * sata device, if device name doesn't exist, set mta_wwn to
13553 * 0 for direct attached SATA. For the device behind the expander
13554 * we still can use STP address assigned by expander.
13556 if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13557 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
13558 mutex_exit(&mpt->m_mutex);
13559 /* alloc a tmp_tgt to send the cmd */
13560 tmp_tgt = kmem_zalloc(sizeof (struct mptsas_target),
13561 KM_SLEEP);
13562 tmp_tgt->m_devhdl = *dev_handle;
13563 tmp_tgt->m_deviceinfo = dev_info;
13564 tmp_tgt->m_qfull_retries = QFULL_RETRIES;
13565 tmp_tgt->m_qfull_retry_interval =
13566 drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
13567 tmp_tgt->m_t_throttle = MAX_THROTTLE;
13568 devicename = mptsas_get_sata_guid(mpt, tmp_tgt, 0);
13569 kmem_free(tmp_tgt, sizeof (struct mptsas_target));
13570 mutex_enter(&mpt->m_mutex);
13571 if (devicename != 0 && (((devicename >> 56) & 0xf0) == 0x50)) {
13572 sas_wwn = devicename;
13573 } else if (dev_info & MPI2_SAS_DEVICE_INFO_DIRECT_ATTACH) {
13574 sas_wwn = 0;
13578 phymask = mptsas_physport_to_phymask(mpt, physport);
13579 *pptgt = mptsas_tgt_alloc(mpt, *dev_handle, sas_wwn,
13580 dev_info, phymask, phynum);
13581 if (*pptgt == NULL) {
13582 mptsas_log(mpt, CE_WARN, "Failed to allocated target"
13583 "structure!");
13584 rval = DEV_INFO_FAIL_ALLOC;
13585 return (rval);
13587 (*pptgt)->m_io_flags = io_flags;
13588 (*pptgt)->m_enclosure = enclosure;
13589 (*pptgt)->m_slot_num = bay_num;
13590 return (DEV_INFO_SUCCESS);
13593 uint64_t
13594 mptsas_get_sata_guid(mptsas_t *mpt, mptsas_target_t *ptgt, int lun)
13596 uint64_t sata_guid = 0, *pwwn = NULL;
13597 int target = ptgt->m_devhdl;
13598 uchar_t *inq83 = NULL;
13599 int inq83_len = 0xFF;
13600 uchar_t *dblk = NULL;
13601 int inq83_retry = 3;
13602 int rval = DDI_FAILURE;
13604 inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
13606 inq83_retry:
13607 rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
13608 inq83_len, NULL, 1);
13609 if (rval != DDI_SUCCESS) {
13610 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
13611 "0x83 for target:%x, lun:%x failed!", target, lun);
13612 goto out;
13614 /* According to SAT2, the first descriptor is logic unit name */
13615 dblk = &inq83[4];
13616 if ((dblk[1] & 0x30) != 0) {
13617 mptsas_log(mpt, CE_WARN, "!Descriptor is not lun associated.");
13618 goto out;
13620 pwwn = (uint64_t *)(void *)(&dblk[4]);
13621 if ((dblk[4] & 0xf0) == 0x50) {
13622 sata_guid = BE_64(*pwwn);
13623 goto out;
13624 } else if (dblk[4] == 'A') {
13625 NDBG20(("SATA drive has no NAA format GUID."));
13626 goto out;
13627 } else {
13628 /* The data is not ready, wait and retry */
13629 inq83_retry--;
13630 if (inq83_retry <= 0) {
13631 goto out;
13633 NDBG20(("The GUID is not ready, retry..."));
13634 delay(1 * drv_usectohz(1000000));
13635 goto inq83_retry;
13637 out:
13638 kmem_free(inq83, inq83_len);
13639 return (sata_guid);
13642 static int
13643 mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun, uchar_t page,
13644 unsigned char *buf, int len, int *reallen, uchar_t evpd)
13646 uchar_t cdb[CDB_GROUP0];
13647 struct scsi_address ap;
13648 struct buf *data_bp = NULL;
13649 int resid = 0;
13650 int ret = DDI_FAILURE;
13652 ASSERT(len <= 0xffff);
13654 ap.a_target = MPTSAS_INVALID_DEVHDL;
13655 ap.a_lun = (uchar_t)(lun);
13656 ap.a_hba_tran = mpt->m_tran;
13658 data_bp = scsi_alloc_consistent_buf(&ap,
13659 (struct buf *)NULL, len, B_READ, NULL_FUNC, NULL);
13660 if (data_bp == NULL) {
13661 return (ret);
13663 bzero(cdb, CDB_GROUP0);
13664 cdb[0] = SCMD_INQUIRY;
13665 cdb[1] = evpd;
13666 cdb[2] = page;
13667 cdb[3] = (len & 0xff00) >> 8;
13668 cdb[4] = (len & 0x00ff);
13669 cdb[5] = 0;
13671 ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP0, data_bp,
13672 &resid);
13673 if (ret == DDI_SUCCESS) {
13674 if (reallen) {
13675 *reallen = len - resid;
13677 bcopy((caddr_t)data_bp->b_un.b_addr, buf, len);
13679 if (data_bp) {
13680 scsi_free_consistent_buf(data_bp);
13682 return (ret);
13685 static int
13686 mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
13687 mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
13688 int *resid)
13690 struct scsi_pkt *pktp = NULL;
13691 scsi_hba_tran_t *tran_clone = NULL;
13692 mptsas_tgt_private_t *tgt_private = NULL;
13693 int ret = DDI_FAILURE;
13696 * scsi_hba_tran_t->tran_tgt_private is used to pass the address
13697 * information to scsi_init_pkt, allocate a scsi_hba_tran structure
13698 * to simulate the cmds from sd
13700 tran_clone = kmem_alloc(
13701 sizeof (scsi_hba_tran_t), KM_SLEEP);
13702 if (tran_clone == NULL) {
13703 goto out;
13705 bcopy((caddr_t)mpt->m_tran,
13706 (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
13707 tgt_private = kmem_alloc(
13708 sizeof (mptsas_tgt_private_t), KM_SLEEP);
13709 if (tgt_private == NULL) {
13710 goto out;
13712 tgt_private->t_lun = ap->a_lun;
13713 tgt_private->t_private = ptgt;
13714 tran_clone->tran_tgt_private = tgt_private;
13715 ap->a_hba_tran = tran_clone;
13717 pktp = scsi_init_pkt(ap, (struct scsi_pkt *)NULL,
13718 data_bp, cdblen, sizeof (struct scsi_arq_status),
13719 0, PKT_CONSISTENT, NULL, NULL);
13720 if (pktp == NULL) {
13721 goto out;
13723 bcopy(cdb, pktp->pkt_cdbp, cdblen);
13724 pktp->pkt_flags = FLAG_NOPARITY;
13725 if (scsi_poll(pktp) < 0) {
13726 goto out;
13728 if (((struct scsi_status *)pktp->pkt_scbp)->sts_chk) {
13729 goto out;
13731 if (resid != NULL) {
13732 *resid = pktp->pkt_resid;
13735 ret = DDI_SUCCESS;
13736 out:
13737 if (pktp) {
13738 scsi_destroy_pkt(pktp);
13740 if (tran_clone) {
13741 kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
13743 if (tgt_private) {
13744 kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
13746 return (ret);
13748 static int
13749 mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy, int *lun)
13751 char *cp = NULL;
13752 char *ptr = NULL;
13753 size_t s = 0;
13754 char *wwid_str = NULL;
13755 char *lun_str = NULL;
13756 long lunnum;
13757 long phyid = -1;
13758 int rc = DDI_FAILURE;
13760 ptr = name;
13761 ASSERT(ptr[0] == 'w' || ptr[0] == 'p');
13762 ptr++;
13763 if ((cp = strchr(ptr, ',')) == NULL) {
13764 return (DDI_FAILURE);
13767 wwid_str = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13768 s = (uintptr_t)cp - (uintptr_t)ptr;
13770 bcopy(ptr, wwid_str, s);
13771 wwid_str[s] = '\0';
13773 ptr = ++cp;
13775 if ((cp = strchr(ptr, '\0')) == NULL) {
13776 goto out;
13778 lun_str = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13779 s = (uintptr_t)cp - (uintptr_t)ptr;
13781 bcopy(ptr, lun_str, s);
13782 lun_str[s] = '\0';
13784 if (name[0] == 'p') {
13785 rc = ddi_strtol(wwid_str, NULL, 0x10, &phyid);
13786 } else {
13787 rc = scsi_wwnstr_to_wwn(wwid_str, wwid);
13789 if (rc != DDI_SUCCESS)
13790 goto out;
13792 if (phyid != -1) {
13793 ASSERT(phyid < MPTSAS_MAX_PHYS);
13794 *phy = (uint8_t)phyid;
13796 rc = ddi_strtol(lun_str, NULL, 0x10, &lunnum);
13797 if (rc != 0)
13798 goto out;
13800 *lun = (int)lunnum;
13801 rc = DDI_SUCCESS;
13802 out:
13803 if (wwid_str)
13804 kmem_free(wwid_str, SCSI_MAXNAMELEN);
13805 if (lun_str)
13806 kmem_free(lun_str, SCSI_MAXNAMELEN);
13808 return (rc);
13812 * mptsas_parse_smp_name() is to parse sas wwn string
13813 * which format is "wWWN"
13815 static int
13816 mptsas_parse_smp_name(char *name, uint64_t *wwn)
13818 char *ptr = name;
13820 if (*ptr != 'w') {
13821 return (DDI_FAILURE);
13824 ptr++;
13825 if (scsi_wwnstr_to_wwn(ptr, wwn)) {
13826 return (DDI_FAILURE);
13828 return (DDI_SUCCESS);
13831 static int
13832 mptsas_bus_config(dev_info_t *pdip, uint_t flag,
13833 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
13835 int ret = NDI_FAILURE;
13836 int circ = 0;
13837 int circ1 = 0;
13838 mptsas_t *mpt;
13839 char *ptr = NULL;
13840 char *devnm = NULL;
13841 uint64_t wwid = 0;
13842 uint8_t phy = 0xFF;
13843 int lun = 0;
13844 uint_t mflags = flag;
13845 int bconfig = TRUE;
13847 if (scsi_hba_iport_unit_address(pdip) == 0) {
13848 return (DDI_FAILURE);
13851 mpt = DIP2MPT(pdip);
13852 if (!mpt) {
13853 return (DDI_FAILURE);
13856 * Hold the nexus across the bus_config
13858 ndi_devi_enter(scsi_vhci_dip, &circ);
13859 ndi_devi_enter(pdip, &circ1);
13860 switch (op) {
13861 case BUS_CONFIG_ONE:
13862 /* parse wwid/target name out of name given */
13863 if ((ptr = strchr((char *)arg, '@')) == NULL) {
13864 ret = NDI_FAILURE;
13865 break;
13867 ptr++;
13868 if (strncmp((char *)arg, "smp", 3) == 0) {
13870 * This is a SMP target device
13872 ret = mptsas_parse_smp_name(ptr, &wwid);
13873 if (ret != DDI_SUCCESS) {
13874 ret = NDI_FAILURE;
13875 break;
13877 ret = mptsas_config_smp(pdip, wwid, childp);
13878 } else if ((ptr[0] == 'w') || (ptr[0] == 'p')) {
13880 * OBP could pass down a non-canonical form
13881 * bootpath without LUN part when LUN is 0.
13882 * So driver need adjust the string.
13884 if (strchr(ptr, ',') == NULL) {
13885 devnm = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13886 (void) sprintf(devnm, "%s,0", (char *)arg);
13887 ptr = strchr(devnm, '@');
13888 ptr++;
13892 * The device path is wWWID format and the device
13893 * is not SMP target device.
13895 ret = mptsas_parse_address(ptr, &wwid, &phy, &lun);
13896 if (ret != DDI_SUCCESS) {
13897 ret = NDI_FAILURE;
13898 break;
13900 *childp = NULL;
13901 if (ptr[0] == 'w') {
13902 ret = mptsas_config_one_addr(pdip, wwid,
13903 lun, childp);
13904 } else if (ptr[0] == 'p') {
13905 ret = mptsas_config_one_phy(pdip, phy, lun,
13906 childp);
13910 * If this is CD/DVD device in OBP path, the
13911 * ndi_busop_bus_config can be skipped as config one
13912 * operation is done above.
13914 if ((ret == NDI_SUCCESS) && (*childp != NULL) &&
13915 (strcmp(ddi_node_name(*childp), "cdrom") == 0) &&
13916 (strncmp((char *)arg, "disk", 4) == 0)) {
13917 bconfig = FALSE;
13918 ndi_hold_devi(*childp);
13920 } else {
13921 ret = NDI_FAILURE;
13922 break;
13926 * DDI group instructed us to use this flag.
13928 mflags |= NDI_MDI_FALLBACK;
13929 break;
13930 case BUS_CONFIG_DRIVER:
13931 case BUS_CONFIG_ALL:
13932 mptsas_config_all(pdip);
13933 ret = NDI_SUCCESS;
13934 break;
13937 if ((ret == NDI_SUCCESS) && bconfig) {
13938 ret = ndi_busop_bus_config(pdip, mflags, op,
13939 (devnm == NULL) ? arg : devnm, childp, 0);
13942 ndi_devi_exit(pdip, circ1);
13943 ndi_devi_exit(scsi_vhci_dip, circ);
13944 if (devnm != NULL)
13945 kmem_free(devnm, SCSI_MAXNAMELEN);
13946 return (ret);
13949 static int
13950 mptsas_probe_lun(dev_info_t *pdip, int lun, dev_info_t **dip,
13951 mptsas_target_t *ptgt)
13953 int rval = DDI_FAILURE;
13954 struct scsi_inquiry *sd_inq = NULL;
13955 mptsas_t *mpt = DIP2MPT(pdip);
13957 sd_inq = (struct scsi_inquiry *)kmem_alloc(SUN_INQSIZE, KM_SLEEP);
13959 rval = mptsas_inquiry(mpt, ptgt, lun, 0, (uchar_t *)sd_inq,
13960 SUN_INQSIZE, 0, (uchar_t)0);
13962 if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
13963 rval = mptsas_create_lun(pdip, sd_inq, dip, ptgt, lun);
13964 } else {
13965 rval = DDI_FAILURE;
13968 kmem_free(sd_inq, SUN_INQSIZE);
13969 return (rval);
13972 static int
13973 mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
13974 dev_info_t **lundip)
13976 int rval;
13977 mptsas_t *mpt = DIP2MPT(pdip);
13978 int phymask;
13979 mptsas_target_t *ptgt = NULL;
13982 * Get the physical port associated to the iport
13984 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
13985 "phymask", 0);
13987 ptgt = mptsas_wwid_to_ptgt(mpt, phymask, sasaddr);
13988 if (ptgt == NULL) {
13990 * didn't match any device by searching
13992 return (DDI_FAILURE);
13995 * If the LUN already exists and the status is online,
13996 * we just return the pointer to dev_info_t directly.
13997 * For the mdi_pathinfo node, we'll handle it in
13998 * mptsas_create_virt_lun()
13999 * TODO should be also in mptsas_handle_dr
14002 *lundip = mptsas_find_child_addr(pdip, sasaddr, lun);
14003 if (*lundip != NULL) {
14005 * TODO Another senario is, we hotplug the same disk
14006 * on the same slot, the devhdl changed, is this
14007 * possible?
14008 * tgt_private->t_private != ptgt
14010 if (sasaddr != ptgt->m_addr.mta_wwn) {
14012 * The device has changed although the devhdl is the
14013 * same (Enclosure mapping mode, change drive on the
14014 * same slot)
14016 return (DDI_FAILURE);
14018 return (DDI_SUCCESS);
14021 if (phymask == 0) {
14023 * Configure IR volume
14025 rval = mptsas_config_raid(pdip, ptgt->m_devhdl, lundip);
14026 return (rval);
14028 rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
14030 return (rval);
14033 static int
14034 mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
14035 dev_info_t **lundip)
14037 int rval;
14038 mptsas_t *mpt = DIP2MPT(pdip);
14039 mptsas_phymask_t phymask;
14040 mptsas_target_t *ptgt = NULL;
14043 * Get the physical port associated to the iport
14045 phymask = (mptsas_phymask_t)ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14046 "phymask", 0);
14048 ptgt = mptsas_phy_to_tgt(mpt, phymask, phy);
14049 if (ptgt == NULL) {
14051 * didn't match any device by searching
14053 return (DDI_FAILURE);
14057 * If the LUN already exists and the status is online,
14058 * we just return the pointer to dev_info_t directly.
14059 * For the mdi_pathinfo node, we'll handle it in
14060 * mptsas_create_virt_lun().
14063 *lundip = mptsas_find_child_phy(pdip, phy);
14064 if (*lundip != NULL) {
14065 return (DDI_SUCCESS);
14068 rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
14070 return (rval);
14073 static int
14074 mptsas_retrieve_lundata(int lun_cnt, uint8_t *buf, uint16_t *lun_num,
14075 uint8_t *lun_addr_type)
14077 uint32_t lun_idx = 0;
14079 ASSERT(lun_num != NULL);
14080 ASSERT(lun_addr_type != NULL);
14082 lun_idx = (lun_cnt + 1) * MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
14083 /* determine report luns addressing type */
14084 switch (buf[lun_idx] & MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) {
14086 * Vendors in the field have been found to be concatenating
14087 * bus/target/lun to equal the complete lun value instead
14088 * of switching to flat space addressing
14090 /* 00b - peripheral device addressing method */
14091 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_PERIPHERAL:
14092 /* FALLTHRU */
14093 /* 10b - logical unit addressing method */
14094 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_LOGICAL_UNIT:
14095 /* FALLTHRU */
14096 /* 01b - flat space addressing method */
14097 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_FLAT_SPACE:
14098 /* byte0 bit0-5=msb lun byte1 bit0-7=lsb lun */
14099 *lun_addr_type = (buf[lun_idx] &
14100 MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) >> 6;
14101 *lun_num = (buf[lun_idx] & 0x3F) << 8;
14102 *lun_num |= buf[lun_idx + 1];
14103 return (DDI_SUCCESS);
14104 default:
14105 return (DDI_FAILURE);
14109 static int
14110 mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt)
14112 struct buf *repluns_bp = NULL;
14113 struct scsi_address ap;
14114 uchar_t cdb[CDB_GROUP5];
14115 int ret = DDI_FAILURE;
14116 int retry = 0;
14117 int lun_list_len = 0;
14118 uint16_t lun_num = 0;
14119 uint8_t lun_addr_type = 0;
14120 uint32_t lun_cnt = 0;
14121 uint32_t lun_total = 0;
14122 dev_info_t *cdip = NULL;
14123 uint16_t *saved_repluns = NULL;
14124 char *buffer = NULL;
14125 int buf_len = 128;
14126 mptsas_t *mpt = DIP2MPT(pdip);
14127 uint64_t sas_wwn = 0;
14128 uint8_t phy = 0xFF;
14129 uint32_t dev_info = 0;
14131 mutex_enter(&mpt->m_mutex);
14132 sas_wwn = ptgt->m_addr.mta_wwn;
14133 phy = ptgt->m_phynum;
14134 dev_info = ptgt->m_deviceinfo;
14135 mutex_exit(&mpt->m_mutex);
14137 if (sas_wwn == 0) {
14139 * It's a SATA without Device Name
14140 * So don't try multi-LUNs
14142 if (mptsas_find_child_phy(pdip, phy)) {
14143 return (DDI_SUCCESS);
14144 } else {
14146 * need configure and create node
14148 return (DDI_FAILURE);
14153 * WWN (SAS address or Device Name exist)
14155 if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
14156 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
14158 * SATA device with Device Name
14159 * So don't try multi-LUNs
14161 if (mptsas_find_child_addr(pdip, sas_wwn, 0)) {
14162 return (DDI_SUCCESS);
14163 } else {
14164 return (DDI_FAILURE);
14168 do {
14169 ap.a_target = MPTSAS_INVALID_DEVHDL;
14170 ap.a_lun = 0;
14171 ap.a_hba_tran = mpt->m_tran;
14172 repluns_bp = scsi_alloc_consistent_buf(&ap,
14173 (struct buf *)NULL, buf_len, B_READ, NULL_FUNC, NULL);
14174 if (repluns_bp == NULL) {
14175 retry++;
14176 continue;
14178 bzero(cdb, CDB_GROUP5);
14179 cdb[0] = SCMD_REPORT_LUNS;
14180 cdb[6] = (buf_len & 0xff000000) >> 24;
14181 cdb[7] = (buf_len & 0x00ff0000) >> 16;
14182 cdb[8] = (buf_len & 0x0000ff00) >> 8;
14183 cdb[9] = (buf_len & 0x000000ff);
14185 ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP5,
14186 repluns_bp, NULL);
14187 if (ret != DDI_SUCCESS) {
14188 scsi_free_consistent_buf(repluns_bp);
14189 retry++;
14190 continue;
14192 lun_list_len = BE_32(*(int *)((void *)(
14193 repluns_bp->b_un.b_addr)));
14194 if (buf_len >= lun_list_len + 8) {
14195 ret = DDI_SUCCESS;
14196 break;
14198 scsi_free_consistent_buf(repluns_bp);
14199 buf_len = lun_list_len + 8;
14201 } while (retry < 3);
14203 if (ret != DDI_SUCCESS)
14204 return (ret);
14205 buffer = (char *)repluns_bp->b_un.b_addr;
14207 * find out the number of luns returned by the SCSI ReportLun call
14208 * and allocate buffer space
14210 lun_total = lun_list_len / MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
14211 saved_repluns = kmem_zalloc(sizeof (uint16_t) * lun_total, KM_SLEEP);
14212 if (saved_repluns == NULL) {
14213 scsi_free_consistent_buf(repluns_bp);
14214 return (DDI_FAILURE);
14216 for (lun_cnt = 0; lun_cnt < lun_total; lun_cnt++) {
14217 if (mptsas_retrieve_lundata(lun_cnt, (uint8_t *)(buffer),
14218 &lun_num, &lun_addr_type) != DDI_SUCCESS) {
14219 continue;
14221 saved_repluns[lun_cnt] = lun_num;
14222 if (cdip = mptsas_find_child_addr(pdip, sas_wwn, lun_num))
14223 ret = DDI_SUCCESS;
14224 else
14225 ret = mptsas_probe_lun(pdip, lun_num, &cdip,
14226 ptgt);
14227 if ((ret == DDI_SUCCESS) && (cdip != NULL)) {
14228 (void) ndi_prop_remove(DDI_DEV_T_NONE, cdip,
14229 MPTSAS_DEV_GONE);
14232 mptsas_offline_missed_luns(pdip, saved_repluns, lun_total, ptgt);
14233 kmem_free(saved_repluns, sizeof (uint16_t) * lun_total);
14234 scsi_free_consistent_buf(repluns_bp);
14235 return (DDI_SUCCESS);
14238 static int
14239 mptsas_config_raid(dev_info_t *pdip, uint16_t target, dev_info_t **dip)
14241 int rval = DDI_FAILURE;
14242 struct scsi_inquiry *sd_inq = NULL;
14243 mptsas_t *mpt = DIP2MPT(pdip);
14244 mptsas_target_t *ptgt = NULL;
14246 mutex_enter(&mpt->m_mutex);
14247 ptgt = refhash_linear_search(mpt->m_targets,
14248 mptsas_target_eval_devhdl, &target);
14249 mutex_exit(&mpt->m_mutex);
14250 if (ptgt == NULL) {
14251 mptsas_log(mpt, CE_WARN, "Volume with VolDevHandle of 0x%x "
14252 "not found.", target);
14253 return (rval);
14256 sd_inq = (struct scsi_inquiry *)kmem_alloc(SUN_INQSIZE, KM_SLEEP);
14257 rval = mptsas_inquiry(mpt, ptgt, 0, 0, (uchar_t *)sd_inq,
14258 SUN_INQSIZE, 0, (uchar_t)0);
14260 if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
14261 rval = mptsas_create_phys_lun(pdip, sd_inq, NULL, dip, ptgt,
14263 } else {
14264 rval = DDI_FAILURE;
14267 kmem_free(sd_inq, SUN_INQSIZE);
14268 return (rval);
14272 * configure all RAID volumes for virtual iport
14274 static void
14275 mptsas_config_all_viport(dev_info_t *pdip)
14277 mptsas_t *mpt = DIP2MPT(pdip);
14278 int config, vol;
14279 int target;
14280 dev_info_t *lundip = NULL;
14283 * Get latest RAID info and search for any Volume DevHandles. If any
14284 * are found, configure the volume.
14286 mutex_enter(&mpt->m_mutex);
14287 for (config = 0; config < mpt->m_num_raid_configs; config++) {
14288 for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
14289 if (mpt->m_raidconfig[config].m_raidvol[vol].m_israid
14290 == 1) {
14291 target = mpt->m_raidconfig[config].
14292 m_raidvol[vol].m_raidhandle;
14293 mutex_exit(&mpt->m_mutex);
14294 (void) mptsas_config_raid(pdip, target,
14295 &lundip);
14296 mutex_enter(&mpt->m_mutex);
14300 mutex_exit(&mpt->m_mutex);
14303 static void
14304 mptsas_offline_missed_luns(dev_info_t *pdip, uint16_t *repluns,
14305 int lun_cnt, mptsas_target_t *ptgt)
14307 dev_info_t *child = NULL, *savechild = NULL;
14308 mdi_pathinfo_t *pip = NULL, *savepip = NULL;
14309 uint64_t sas_wwn, wwid;
14310 uint8_t phy;
14311 int lun;
14312 int i;
14313 int find;
14314 char *addr;
14315 char *nodename;
14316 mptsas_t *mpt = DIP2MPT(pdip);
14318 mutex_enter(&mpt->m_mutex);
14319 wwid = ptgt->m_addr.mta_wwn;
14320 mutex_exit(&mpt->m_mutex);
14322 child = ddi_get_child(pdip);
14323 while (child) {
14324 find = 0;
14325 savechild = child;
14326 child = ddi_get_next_sibling(child);
14328 nodename = ddi_node_name(savechild);
14329 if (strcmp(nodename, "smp") == 0) {
14330 continue;
14333 addr = ddi_get_name_addr(savechild);
14334 if (addr == NULL) {
14335 continue;
14338 if (mptsas_parse_address(addr, &sas_wwn, &phy, &lun) !=
14339 DDI_SUCCESS) {
14340 continue;
14343 if (wwid == sas_wwn) {
14344 for (i = 0; i < lun_cnt; i++) {
14345 if (repluns[i] == lun) {
14346 find = 1;
14347 break;
14350 } else {
14351 continue;
14353 if (find == 0) {
14355 * The lun has not been there already
14357 (void) mptsas_offline_lun(pdip, savechild, NULL,
14358 NDI_DEVI_REMOVE);
14362 pip = mdi_get_next_client_path(pdip, NULL);
14363 while (pip) {
14364 find = 0;
14365 savepip = pip;
14366 addr = MDI_PI(pip)->pi_addr;
14368 pip = mdi_get_next_client_path(pdip, pip);
14370 if (addr == NULL) {
14371 continue;
14374 if (mptsas_parse_address(addr, &sas_wwn, &phy,
14375 &lun) != DDI_SUCCESS) {
14376 continue;
14379 if (sas_wwn == wwid) {
14380 for (i = 0; i < lun_cnt; i++) {
14381 if (repluns[i] == lun) {
14382 find = 1;
14383 break;
14386 } else {
14387 continue;
14390 if (find == 0) {
14392 * The lun has not been there already
14394 (void) mptsas_offline_lun(pdip, NULL, savepip,
14395 NDI_DEVI_REMOVE);
14400 void
14401 mptsas_update_hashtab(struct mptsas *mpt)
14403 uint32_t page_address;
14404 int rval = 0;
14405 uint16_t dev_handle;
14406 mptsas_target_t *ptgt = NULL;
14407 mptsas_smp_t smp_node;
14410 * Get latest RAID info.
14412 (void) mptsas_get_raid_info(mpt);
14414 dev_handle = mpt->m_smp_devhdl;
14415 for (; mpt->m_done_traverse_smp == 0; ) {
14416 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
14417 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)dev_handle;
14418 if (mptsas_get_sas_expander_page0(mpt, page_address, &smp_node)
14419 != DDI_SUCCESS) {
14420 break;
14422 mpt->m_smp_devhdl = dev_handle = smp_node.m_devhdl;
14423 (void) mptsas_smp_alloc(mpt, &smp_node);
14427 * Config target devices
14429 dev_handle = mpt->m_dev_handle;
14432 * Do loop to get sas device page 0 by GetNextHandle till the
14433 * the last handle. If the sas device is a SATA/SSP target,
14434 * we try to config it.
14436 for (; mpt->m_done_traverse_dev == 0; ) {
14437 ptgt = NULL;
14438 page_address =
14439 (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
14440 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
14441 (uint32_t)dev_handle;
14442 rval = mptsas_get_target_device_info(mpt, page_address,
14443 &dev_handle, &ptgt);
14444 if ((rval == DEV_INFO_FAIL_PAGE0) ||
14445 (rval == DEV_INFO_FAIL_ALLOC)) {
14446 break;
14449 mpt->m_dev_handle = dev_handle;
14454 void
14455 mptsas_update_driver_data(struct mptsas *mpt)
14457 mptsas_target_t *tp;
14458 mptsas_smp_t *sp;
14460 ASSERT(MUTEX_HELD(&mpt->m_mutex));
14463 * TODO after hard reset, update the driver data structures
14464 * 1. update port/phymask mapping table mpt->m_phy_info
14465 * 2. invalid all the entries in hash table
14466 * m_devhdl = 0xffff and m_deviceinfo = 0
14467 * 3. call sas_device_page/expander_page to update hash table
14469 mptsas_update_phymask(mpt);
14471 * Invalid the existing entries
14473 * XXX - It seems like we should just delete everything here. We are
14474 * holding the lock and are about to refresh all the targets in both
14475 * hashes anyway. Given the path we're in, what outstanding async
14476 * event could possibly be trying to reference one of these things
14477 * without taking the lock, and how would that be useful anyway?
14479 for (tp = refhash_first(mpt->m_targets); tp != NULL;
14480 tp = refhash_next(mpt->m_targets, tp)) {
14481 tp->m_devhdl = MPTSAS_INVALID_DEVHDL;
14482 tp->m_deviceinfo = 0;
14483 tp->m_dr_flag = MPTSAS_DR_INACTIVE;
14485 for (sp = refhash_first(mpt->m_smp_targets); sp != NULL;
14486 sp = refhash_next(mpt->m_smp_targets, sp)) {
14487 sp->m_devhdl = MPTSAS_INVALID_DEVHDL;
14488 sp->m_deviceinfo = 0;
14490 mpt->m_done_traverse_dev = 0;
14491 mpt->m_done_traverse_smp = 0;
14492 mpt->m_dev_handle = mpt->m_smp_devhdl = MPTSAS_INVALID_DEVHDL;
14493 mptsas_update_hashtab(mpt);
14496 static void
14497 mptsas_config_all(dev_info_t *pdip)
14499 dev_info_t *smpdip = NULL;
14500 mptsas_t *mpt = DIP2MPT(pdip);
14501 int phymask = 0;
14502 mptsas_phymask_t phy_mask;
14503 mptsas_target_t *ptgt = NULL;
14504 mptsas_smp_t *psmp;
14507 * Get the phymask associated to the iport
14509 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14510 "phymask", 0);
14513 * Enumerate RAID volumes here (phymask == 0).
14515 if (phymask == 0) {
14516 mptsas_config_all_viport(pdip);
14517 return;
14520 mutex_enter(&mpt->m_mutex);
14522 if (!mpt->m_done_traverse_dev || !mpt->m_done_traverse_smp) {
14523 mptsas_update_hashtab(mpt);
14526 for (psmp = refhash_first(mpt->m_smp_targets); psmp != NULL;
14527 psmp = refhash_next(mpt->m_smp_targets, psmp)) {
14528 phy_mask = psmp->m_addr.mta_phymask;
14529 if (phy_mask == phymask) {
14530 smpdip = NULL;
14531 mutex_exit(&mpt->m_mutex);
14532 (void) mptsas_online_smp(pdip, psmp, &smpdip);
14533 mutex_enter(&mpt->m_mutex);
14537 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
14538 ptgt = refhash_next(mpt->m_targets, ptgt)) {
14539 phy_mask = ptgt->m_addr.mta_phymask;
14540 if (phy_mask == phymask) {
14541 mutex_exit(&mpt->m_mutex);
14542 (void) mptsas_config_target(pdip, ptgt);
14543 mutex_enter(&mpt->m_mutex);
14546 mutex_exit(&mpt->m_mutex);
14549 static int
14550 mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt)
14552 int rval = DDI_FAILURE;
14553 dev_info_t *tdip;
14555 rval = mptsas_config_luns(pdip, ptgt);
14556 if (rval != DDI_SUCCESS) {
14558 * The return value means the SCMD_REPORT_LUNS
14559 * did not execute successfully. The target maybe
14560 * doesn't support such command.
14562 rval = mptsas_probe_lun(pdip, 0, &tdip, ptgt);
14564 return (rval);
14568 * Return fail if not all the childs/paths are freed.
14569 * if there is any path under the HBA, the return value will be always fail
14570 * because we didn't call mdi_pi_free for path
14572 static int
14573 mptsas_offline_target(dev_info_t *pdip, char *name)
14575 dev_info_t *child = NULL, *prechild = NULL;
14576 mdi_pathinfo_t *pip = NULL, *savepip = NULL;
14577 int tmp_rval, rval = DDI_SUCCESS;
14578 char *addr, *cp;
14579 size_t s;
14580 mptsas_t *mpt = DIP2MPT(pdip);
14582 child = ddi_get_child(pdip);
14583 while (child) {
14584 addr = ddi_get_name_addr(child);
14585 prechild = child;
14586 child = ddi_get_next_sibling(child);
14588 if (addr == NULL) {
14589 continue;
14591 if ((cp = strchr(addr, ',')) == NULL) {
14592 continue;
14595 s = (uintptr_t)cp - (uintptr_t)addr;
14597 if (strncmp(addr, name, s) != 0) {
14598 continue;
14601 tmp_rval = mptsas_offline_lun(pdip, prechild, NULL,
14602 NDI_DEVI_REMOVE);
14603 if (tmp_rval != DDI_SUCCESS) {
14604 rval = DDI_FAILURE;
14605 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
14606 prechild, MPTSAS_DEV_GONE) !=
14607 DDI_PROP_SUCCESS) {
14608 mptsas_log(mpt, CE_WARN, "mptsas driver "
14609 "unable to create property for "
14610 "SAS %s (MPTSAS_DEV_GONE)", addr);
14615 pip = mdi_get_next_client_path(pdip, NULL);
14616 while (pip) {
14617 addr = MDI_PI(pip)->pi_addr;
14618 savepip = pip;
14619 pip = mdi_get_next_client_path(pdip, pip);
14620 if (addr == NULL) {
14621 continue;
14624 if ((cp = strchr(addr, ',')) == NULL) {
14625 continue;
14628 s = (uintptr_t)cp - (uintptr_t)addr;
14630 if (strncmp(addr, name, s) != 0) {
14631 continue;
14634 (void) mptsas_offline_lun(pdip, NULL, savepip,
14635 NDI_DEVI_REMOVE);
14637 * driver will not invoke mdi_pi_free, so path will not
14638 * be freed forever, return DDI_FAILURE.
14640 rval = DDI_FAILURE;
14642 return (rval);
14645 static int
14646 mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
14647 mdi_pathinfo_t *rpip, uint_t flags)
14649 int rval = DDI_FAILURE;
14650 char *devname;
14651 dev_info_t *cdip, *parent;
14653 if (rpip != NULL) {
14654 parent = scsi_vhci_dip;
14655 cdip = mdi_pi_get_client(rpip);
14656 } else if (rdip != NULL) {
14657 parent = pdip;
14658 cdip = rdip;
14659 } else {
14660 return (DDI_FAILURE);
14664 * Make sure node is attached otherwise
14665 * it won't have related cache nodes to
14666 * clean up. i_ddi_devi_attached is
14667 * similiar to i_ddi_node_state(cdip) >=
14668 * DS_ATTACHED.
14670 if (i_ddi_devi_attached(cdip)) {
14672 /* Get full devname */
14673 devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14674 (void) ddi_deviname(cdip, devname);
14675 /* Clean cache */
14676 (void) devfs_clean(parent, devname + 1,
14677 DV_CLEAN_FORCE);
14678 kmem_free(devname, MAXNAMELEN + 1);
14680 if (rpip != NULL) {
14681 if (MDI_PI_IS_OFFLINE(rpip)) {
14682 rval = DDI_SUCCESS;
14683 } else {
14684 rval = mdi_pi_offline(rpip, 0);
14686 } else {
14687 rval = ndi_devi_offline(cdip, flags);
14690 return (rval);
14693 static dev_info_t *
14694 mptsas_find_smp_child(dev_info_t *parent, char *str_wwn)
14696 dev_info_t *child = NULL;
14697 char *smp_wwn = NULL;
14699 child = ddi_get_child(parent);
14700 while (child) {
14701 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
14702 DDI_PROP_DONTPASS, SMP_WWN, &smp_wwn)
14703 != DDI_SUCCESS) {
14704 child = ddi_get_next_sibling(child);
14705 continue;
14708 if (strcmp(smp_wwn, str_wwn) == 0) {
14709 ddi_prop_free(smp_wwn);
14710 break;
14712 child = ddi_get_next_sibling(child);
14713 ddi_prop_free(smp_wwn);
14715 return (child);
14718 static int
14719 mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node, uint_t flags)
14721 int rval = DDI_FAILURE;
14722 char *devname;
14723 char wwn_str[MPTSAS_WWN_STRLEN];
14724 dev_info_t *cdip;
14726 (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_addr.mta_wwn);
14728 cdip = mptsas_find_smp_child(pdip, wwn_str);
14730 if (cdip == NULL)
14731 return (DDI_SUCCESS);
14734 * Make sure node is attached otherwise
14735 * it won't have related cache nodes to
14736 * clean up. i_ddi_devi_attached is
14737 * similiar to i_ddi_node_state(cdip) >=
14738 * DS_ATTACHED.
14740 if (i_ddi_devi_attached(cdip)) {
14742 /* Get full devname */
14743 devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14744 (void) ddi_deviname(cdip, devname);
14745 /* Clean cache */
14746 (void) devfs_clean(pdip, devname + 1,
14747 DV_CLEAN_FORCE);
14748 kmem_free(devname, MAXNAMELEN + 1);
14751 rval = ndi_devi_offline(cdip, flags);
14753 return (rval);
14756 static dev_info_t *
14757 mptsas_find_child(dev_info_t *pdip, char *name)
14759 dev_info_t *child = NULL;
14760 char *rname = NULL;
14761 int rval = DDI_FAILURE;
14763 rname = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14765 child = ddi_get_child(pdip);
14766 while (child) {
14767 rval = mptsas_name_child(child, rname, SCSI_MAXNAMELEN);
14768 if (rval != DDI_SUCCESS) {
14769 child = ddi_get_next_sibling(child);
14770 bzero(rname, SCSI_MAXNAMELEN);
14771 continue;
14774 if (strcmp(rname, name) == 0) {
14775 break;
14777 child = ddi_get_next_sibling(child);
14778 bzero(rname, SCSI_MAXNAMELEN);
14781 kmem_free(rname, SCSI_MAXNAMELEN);
14783 return (child);
14787 static dev_info_t *
14788 mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr, int lun)
14790 dev_info_t *child = NULL;
14791 char *name = NULL;
14792 char *addr = NULL;
14794 name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14795 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14796 (void) sprintf(name, "%016"PRIx64, sasaddr);
14797 (void) sprintf(addr, "w%s,%x", name, lun);
14798 child = mptsas_find_child(pdip, addr);
14799 kmem_free(name, SCSI_MAXNAMELEN);
14800 kmem_free(addr, SCSI_MAXNAMELEN);
14801 return (child);
14804 static dev_info_t *
14805 mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy)
14807 dev_info_t *child;
14808 char *addr;
14810 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14811 (void) sprintf(addr, "p%x,0", phy);
14812 child = mptsas_find_child(pdip, addr);
14813 kmem_free(addr, SCSI_MAXNAMELEN);
14814 return (child);
14817 static mdi_pathinfo_t *
14818 mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy)
14820 mdi_pathinfo_t *path;
14821 char *addr = NULL;
14823 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14824 (void) sprintf(addr, "p%x,0", phy);
14825 path = mdi_pi_find(pdip, NULL, addr);
14826 kmem_free(addr, SCSI_MAXNAMELEN);
14827 return (path);
14830 static mdi_pathinfo_t *
14831 mptsas_find_path_addr(dev_info_t *parent, uint64_t sasaddr, int lun)
14833 mdi_pathinfo_t *path;
14834 char *name = NULL;
14835 char *addr = NULL;
14837 name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14838 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14839 (void) sprintf(name, "%016"PRIx64, sasaddr);
14840 (void) sprintf(addr, "w%s,%x", name, lun);
14841 path = mdi_pi_find(parent, NULL, addr);
14842 kmem_free(name, SCSI_MAXNAMELEN);
14843 kmem_free(addr, SCSI_MAXNAMELEN);
14845 return (path);
14848 static int
14849 mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
14850 dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
14852 int i = 0;
14853 uchar_t *inq83 = NULL;
14854 int inq83_len1 = 0xFF;
14855 int inq83_len = 0;
14856 int rval = DDI_FAILURE;
14857 ddi_devid_t devid;
14858 char *guid = NULL;
14859 int target = ptgt->m_devhdl;
14860 mdi_pathinfo_t *pip = NULL;
14861 mptsas_t *mpt = DIP2MPT(pdip);
14864 * For DVD/CD ROM and tape devices and optical
14865 * devices, we won't try to enumerate them under
14866 * scsi_vhci, so no need to try page83
14868 if (sd_inq && (sd_inq->inq_dtype == DTYPE_RODIRECT ||
14869 sd_inq->inq_dtype == DTYPE_OPTICAL ||
14870 sd_inq->inq_dtype == DTYPE_ESI))
14871 goto create_lun;
14874 * The LCA returns good SCSI status, but corrupt page 83 data the first
14875 * time it is queried. The solution is to keep trying to request page83
14876 * and verify the GUID is not (DDI_NOT_WELL_FORMED) in
14877 * mptsas_inq83_retry_timeout seconds. If the timeout expires, driver
14878 * give up to get VPD page at this stage and fail the enumeration.
14881 inq83 = kmem_zalloc(inq83_len1, KM_SLEEP);
14883 for (i = 0; i < mptsas_inq83_retry_timeout; i++) {
14884 rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
14885 inq83_len1, &inq83_len, 1);
14886 if (rval != 0) {
14887 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
14888 "0x83 for target:%x, lun:%x failed!", target, lun);
14889 if (mptsas_physical_bind_failed_page_83 != B_FALSE)
14890 goto create_lun;
14891 goto out;
14894 * create DEVID from inquiry data
14896 if ((rval = ddi_devid_scsi_encode(
14897 DEVID_SCSI_ENCODE_VERSION_LATEST, NULL, (uchar_t *)sd_inq,
14898 sizeof (struct scsi_inquiry), NULL, 0, inq83,
14899 (size_t)inq83_len, &devid)) == DDI_SUCCESS) {
14901 * extract GUID from DEVID
14903 guid = ddi_devid_to_guid(devid);
14906 * Do not enable MPXIO if the strlen(guid) is greater
14907 * than MPTSAS_MAX_GUID_LEN, this constrain would be
14908 * handled by framework later.
14910 if (guid && (strlen(guid) > MPTSAS_MAX_GUID_LEN)) {
14911 ddi_devid_free_guid(guid);
14912 guid = NULL;
14913 if (mpt->m_mpxio_enable == TRUE) {
14914 mptsas_log(mpt, CE_NOTE, "!Target:%x, "
14915 "lun:%x doesn't have a valid GUID, "
14916 "multipathing for this drive is "
14917 "not enabled", target, lun);
14922 * devid no longer needed
14924 ddi_devid_free(devid);
14925 break;
14926 } else if (rval == DDI_NOT_WELL_FORMED) {
14928 * return value of ddi_devid_scsi_encode equal to
14929 * DDI_NOT_WELL_FORMED means DEVID_RETRY, it worth
14930 * to retry inquiry page 0x83 and get GUID.
14932 NDBG20(("Not well formed devid, retry..."));
14933 delay(1 * drv_usectohz(1000000));
14934 continue;
14935 } else {
14936 mptsas_log(mpt, CE_WARN, "!Encode devid failed for "
14937 "path target:%x, lun:%x", target, lun);
14938 rval = DDI_FAILURE;
14939 goto create_lun;
14943 if (i == mptsas_inq83_retry_timeout) {
14944 mptsas_log(mpt, CE_WARN, "!Repeated page83 requests timeout "
14945 "for path target:%x, lun:%x", target, lun);
14948 rval = DDI_FAILURE;
14950 create_lun:
14951 if ((guid != NULL) && (mpt->m_mpxio_enable == TRUE)) {
14952 rval = mptsas_create_virt_lun(pdip, sd_inq, guid, lun_dip, &pip,
14953 ptgt, lun);
14955 if (rval != DDI_SUCCESS) {
14956 rval = mptsas_create_phys_lun(pdip, sd_inq, guid, lun_dip,
14957 ptgt, lun);
14960 out:
14961 if (guid != NULL) {
14963 * guid no longer needed
14965 ddi_devid_free_guid(guid);
14967 if (inq83 != NULL)
14968 kmem_free(inq83, inq83_len1);
14969 return (rval);
14972 static int
14973 mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *inq, char *guid,
14974 dev_info_t **lun_dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt, int lun)
14976 int target;
14977 char *nodename = NULL;
14978 char **compatible = NULL;
14979 int ncompatible = 0;
14980 int mdi_rtn = MDI_FAILURE;
14981 int rval = DDI_FAILURE;
14982 char *old_guid = NULL;
14983 mptsas_t *mpt = DIP2MPT(pdip);
14984 char *lun_addr = NULL;
14985 char *wwn_str = NULL;
14986 char *attached_wwn_str = NULL;
14987 char *component = NULL;
14988 uint8_t phy = 0xFF;
14989 uint64_t sas_wwn;
14990 int64_t lun64 = 0;
14991 uint32_t devinfo;
14992 uint16_t dev_hdl;
14993 uint16_t pdev_hdl;
14994 uint64_t dev_sas_wwn;
14995 uint64_t pdev_sas_wwn;
14996 uint32_t pdev_info;
14997 uint8_t physport;
14998 uint8_t phy_id;
14999 uint32_t page_address;
15000 uint16_t bay_num, enclosure, io_flags;
15001 char pdev_wwn_str[MPTSAS_WWN_STRLEN];
15002 uint32_t dev_info;
15004 mutex_enter(&mpt->m_mutex);
15005 target = ptgt->m_devhdl;
15006 sas_wwn = ptgt->m_addr.mta_wwn;
15007 devinfo = ptgt->m_deviceinfo;
15008 phy = ptgt->m_phynum;
15009 mutex_exit(&mpt->m_mutex);
15011 if (sas_wwn) {
15012 *pip = mptsas_find_path_addr(pdip, sas_wwn, lun);
15013 } else {
15014 *pip = mptsas_find_path_phy(pdip, phy);
15017 if (*pip != NULL) {
15018 *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
15019 ASSERT(*lun_dip != NULL);
15020 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, *lun_dip,
15021 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
15022 MDI_CLIENT_GUID_PROP, &old_guid) == DDI_SUCCESS) {
15023 if (strncmp(guid, old_guid, strlen(guid)) == 0) {
15025 * Same path back online again.
15027 (void) ddi_prop_free(old_guid);
15028 if ((!MDI_PI_IS_ONLINE(*pip)) &&
15029 (!MDI_PI_IS_STANDBY(*pip)) &&
15030 (ptgt->m_tgt_unconfigured == 0)) {
15031 rval = mdi_pi_online(*pip, 0);
15032 mutex_enter(&mpt->m_mutex);
15033 ptgt->m_led_status = 0;
15034 (void) mptsas_flush_led_status(mpt,
15035 ptgt);
15036 mutex_exit(&mpt->m_mutex);
15037 } else {
15038 rval = DDI_SUCCESS;
15040 if (rval != DDI_SUCCESS) {
15041 mptsas_log(mpt, CE_WARN, "path:target: "
15042 "%x, lun:%x online failed!", target,
15043 lun);
15044 *pip = NULL;
15045 *lun_dip = NULL;
15047 return (rval);
15048 } else {
15050 * The GUID of the LUN has changed which maybe
15051 * because customer mapped another volume to the
15052 * same LUN.
15054 mptsas_log(mpt, CE_WARN, "The GUID of the "
15055 "target:%x, lun:%x was changed, maybe "
15056 "because someone mapped another volume "
15057 "to the same LUN", target, lun);
15058 (void) ddi_prop_free(old_guid);
15059 if (!MDI_PI_IS_OFFLINE(*pip)) {
15060 rval = mdi_pi_offline(*pip, 0);
15061 if (rval != MDI_SUCCESS) {
15062 mptsas_log(mpt, CE_WARN, "path:"
15063 "target:%x, lun:%x offline "
15064 "failed!", target, lun);
15065 *pip = NULL;
15066 *lun_dip = NULL;
15067 return (DDI_FAILURE);
15070 if (mdi_pi_free(*pip, 0) != MDI_SUCCESS) {
15071 mptsas_log(mpt, CE_WARN, "path:target:"
15072 "%x, lun:%x free failed!", target,
15073 lun);
15074 *pip = NULL;
15075 *lun_dip = NULL;
15076 return (DDI_FAILURE);
15079 } else {
15080 mptsas_log(mpt, CE_WARN, "Can't get client-guid "
15081 "property for path:target:%x, lun:%x", target, lun);
15082 *pip = NULL;
15083 *lun_dip = NULL;
15084 return (DDI_FAILURE);
15087 scsi_hba_nodename_compatible_get(inq, NULL,
15088 inq->inq_dtype, NULL, &nodename, &compatible, &ncompatible);
15091 * if nodename can't be determined then print a message and skip it
15093 if (nodename == NULL) {
15094 mptsas_log(mpt, CE_WARN, "mptsas driver found no compatible "
15095 "driver for target%d lun %d dtype:0x%02x", target, lun,
15096 inq->inq_dtype);
15097 return (DDI_FAILURE);
15100 wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
15101 /* The property is needed by MPAPI */
15102 (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
15104 lun_addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
15105 if (guid) {
15106 (void) sprintf(lun_addr, "w%s,%x", wwn_str, lun);
15107 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15108 } else {
15109 (void) sprintf(lun_addr, "p%x,%x", phy, lun);
15110 (void) sprintf(wwn_str, "p%x", phy);
15113 mdi_rtn = mdi_pi_alloc_compatible(pdip, nodename,
15114 guid, lun_addr, compatible, ncompatible,
15115 0, pip);
15116 if (mdi_rtn == MDI_SUCCESS) {
15118 if (mdi_prop_update_string(*pip, MDI_GUID,
15119 guid) != DDI_SUCCESS) {
15120 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15121 "create prop for target %d lun %d (MDI_GUID)",
15122 target, lun);
15123 mdi_rtn = MDI_FAILURE;
15124 goto virt_create_done;
15127 if (mdi_prop_update_int(*pip, LUN_PROP,
15128 lun) != DDI_SUCCESS) {
15129 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15130 "create prop for target %d lun %d (LUN_PROP)",
15131 target, lun);
15132 mdi_rtn = MDI_FAILURE;
15133 goto virt_create_done;
15135 lun64 = (int64_t)lun;
15136 if (mdi_prop_update_int64(*pip, LUN64_PROP,
15137 lun64) != DDI_SUCCESS) {
15138 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15139 "create prop for target %d (LUN64_PROP)",
15140 target);
15141 mdi_rtn = MDI_FAILURE;
15142 goto virt_create_done;
15144 if (mdi_prop_update_string_array(*pip, "compatible",
15145 compatible, ncompatible) !=
15146 DDI_PROP_SUCCESS) {
15147 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15148 "create prop for target %d lun %d (COMPATIBLE)",
15149 target, lun);
15150 mdi_rtn = MDI_FAILURE;
15151 goto virt_create_done;
15153 if (sas_wwn && (mdi_prop_update_string(*pip,
15154 SCSI_ADDR_PROP_TARGET_PORT, wwn_str) != DDI_PROP_SUCCESS)) {
15155 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15156 "create prop for target %d lun %d "
15157 "(target-port)", target, lun);
15158 mdi_rtn = MDI_FAILURE;
15159 goto virt_create_done;
15160 } else if ((sas_wwn == 0) && (mdi_prop_update_int(*pip,
15161 "sata-phy", phy) != DDI_PROP_SUCCESS)) {
15163 * Direct attached SATA device without DeviceName
15165 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15166 "create prop for SAS target %d lun %d "
15167 "(sata-phy)", target, lun);
15168 mdi_rtn = MDI_FAILURE;
15169 goto virt_create_done;
15171 mutex_enter(&mpt->m_mutex);
15173 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15174 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15175 (uint32_t)ptgt->m_devhdl;
15176 rval = mptsas_get_sas_device_page0(mpt, page_address,
15177 &dev_hdl, &dev_sas_wwn, &dev_info, &physport,
15178 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15179 if (rval != DDI_SUCCESS) {
15180 mutex_exit(&mpt->m_mutex);
15181 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15182 "parent device for handle %d", page_address);
15183 mdi_rtn = MDI_FAILURE;
15184 goto virt_create_done;
15187 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15188 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
15189 rval = mptsas_get_sas_device_page0(mpt, page_address,
15190 &dev_hdl, &pdev_sas_wwn, &pdev_info, &physport,
15191 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15192 if (rval != DDI_SUCCESS) {
15193 mutex_exit(&mpt->m_mutex);
15194 mptsas_log(mpt, CE_WARN, "mptsas unable to get"
15195 "device info for handle %d", page_address);
15196 mdi_rtn = MDI_FAILURE;
15197 goto virt_create_done;
15200 mutex_exit(&mpt->m_mutex);
15203 * If this device direct attached to the controller
15204 * set the attached-port to the base wwid
15206 if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15207 != DEVINFO_DIRECT_ATTACHED) {
15208 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15209 pdev_sas_wwn);
15210 } else {
15212 * Update the iport's attached-port to guid
15214 if (sas_wwn == 0) {
15215 (void) sprintf(wwn_str, "p%x", phy);
15216 } else {
15217 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15219 if (ddi_prop_update_string(DDI_DEV_T_NONE,
15220 pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15221 DDI_PROP_SUCCESS) {
15222 mptsas_log(mpt, CE_WARN,
15223 "mptsas unable to create "
15224 "property for iport target-port"
15225 " %s (sas_wwn)",
15226 wwn_str);
15227 mdi_rtn = MDI_FAILURE;
15228 goto virt_create_done;
15231 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15232 mpt->un.m_base_wwid);
15235 if (mdi_prop_update_string(*pip,
15236 SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
15237 DDI_PROP_SUCCESS) {
15238 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15239 "property for iport attached-port %s (sas_wwn)",
15240 attached_wwn_str);
15241 mdi_rtn = MDI_FAILURE;
15242 goto virt_create_done;
15246 if (inq->inq_dtype == 0) {
15247 component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15249 * set obp path for pathinfo
15251 (void) snprintf(component, MAXPATHLEN,
15252 "disk@%s", lun_addr);
15254 if (mdi_pi_pathname_obp_set(*pip, component) !=
15255 DDI_SUCCESS) {
15256 mptsas_log(mpt, CE_WARN, "mpt_sas driver "
15257 "unable to set obp-path for object %s",
15258 component);
15259 mdi_rtn = MDI_FAILURE;
15260 goto virt_create_done;
15264 *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
15265 if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
15266 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
15267 if ((ndi_prop_update_int(DDI_DEV_T_NONE, *lun_dip,
15268 "pm-capable", 1)) !=
15269 DDI_PROP_SUCCESS) {
15270 mptsas_log(mpt, CE_WARN, "mptsas driver"
15271 "failed to create pm-capable "
15272 "property, target %d", target);
15273 mdi_rtn = MDI_FAILURE;
15274 goto virt_create_done;
15278 * Create the phy-num property
15280 if (mdi_prop_update_int(*pip, "phy-num",
15281 ptgt->m_phynum) != DDI_SUCCESS) {
15282 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15283 "create phy-num property for target %d lun %d",
15284 target, lun);
15285 mdi_rtn = MDI_FAILURE;
15286 goto virt_create_done;
15288 NDBG20(("new path:%s onlining,", MDI_PI(*pip)->pi_addr));
15289 mdi_rtn = mdi_pi_online(*pip, 0);
15290 if (mdi_rtn == MDI_SUCCESS) {
15291 mutex_enter(&mpt->m_mutex);
15292 ptgt->m_led_status = 0;
15293 (void) mptsas_flush_led_status(mpt, ptgt);
15294 mutex_exit(&mpt->m_mutex);
15296 if (mdi_rtn == MDI_NOT_SUPPORTED) {
15297 mdi_rtn = MDI_FAILURE;
15299 virt_create_done:
15300 if (*pip && mdi_rtn != MDI_SUCCESS) {
15301 (void) mdi_pi_free(*pip, 0);
15302 *pip = NULL;
15303 *lun_dip = NULL;
15307 scsi_hba_nodename_compatible_free(nodename, compatible);
15308 if (lun_addr != NULL) {
15309 kmem_free(lun_addr, SCSI_MAXNAMELEN);
15311 if (wwn_str != NULL) {
15312 kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
15314 if (component != NULL) {
15315 kmem_free(component, MAXPATHLEN);
15318 return ((mdi_rtn == MDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15321 static int
15322 mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *inq,
15323 char *guid, dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
15325 int target;
15326 int rval;
15327 int ndi_rtn = NDI_FAILURE;
15328 uint64_t be_sas_wwn;
15329 char *nodename = NULL;
15330 char **compatible = NULL;
15331 int ncompatible = 0;
15332 int instance = 0;
15333 mptsas_t *mpt = DIP2MPT(pdip);
15334 char *wwn_str = NULL;
15335 char *component = NULL;
15336 char *attached_wwn_str = NULL;
15337 uint8_t phy = 0xFF;
15338 uint64_t sas_wwn;
15339 uint32_t devinfo;
15340 uint16_t dev_hdl;
15341 uint16_t pdev_hdl;
15342 uint64_t pdev_sas_wwn;
15343 uint64_t dev_sas_wwn;
15344 uint32_t pdev_info;
15345 uint8_t physport;
15346 uint8_t phy_id;
15347 uint32_t page_address;
15348 uint16_t bay_num, enclosure, io_flags;
15349 char pdev_wwn_str[MPTSAS_WWN_STRLEN];
15350 uint32_t dev_info;
15351 int64_t lun64 = 0;
15353 mutex_enter(&mpt->m_mutex);
15354 target = ptgt->m_devhdl;
15355 sas_wwn = ptgt->m_addr.mta_wwn;
15356 devinfo = ptgt->m_deviceinfo;
15357 phy = ptgt->m_phynum;
15358 mutex_exit(&mpt->m_mutex);
15361 * generate compatible property with binding-set "mpt"
15363 scsi_hba_nodename_compatible_get(inq, NULL, inq->inq_dtype, NULL,
15364 &nodename, &compatible, &ncompatible);
15367 * if nodename can't be determined then print a message and skip it
15369 if (nodename == NULL) {
15370 mptsas_log(mpt, CE_WARN, "mptsas found no compatible driver "
15371 "for target %d lun %d", target, lun);
15372 return (DDI_FAILURE);
15375 ndi_rtn = ndi_devi_alloc(pdip, nodename,
15376 DEVI_SID_NODEID, lun_dip);
15379 * if lun alloc success, set props
15381 if (ndi_rtn == NDI_SUCCESS) {
15383 if (ndi_prop_update_int(DDI_DEV_T_NONE,
15384 *lun_dip, LUN_PROP, lun) !=
15385 DDI_PROP_SUCCESS) {
15386 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15387 "property for target %d lun %d (LUN_PROP)",
15388 target, lun);
15389 ndi_rtn = NDI_FAILURE;
15390 goto phys_create_done;
15393 lun64 = (int64_t)lun;
15394 if (ndi_prop_update_int64(DDI_DEV_T_NONE,
15395 *lun_dip, LUN64_PROP, lun64) !=
15396 DDI_PROP_SUCCESS) {
15397 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15398 "property for target %d lun64 %d (LUN64_PROP)",
15399 target, lun);
15400 ndi_rtn = NDI_FAILURE;
15401 goto phys_create_done;
15403 if (ndi_prop_update_string_array(DDI_DEV_T_NONE,
15404 *lun_dip, "compatible", compatible, ncompatible)
15405 != DDI_PROP_SUCCESS) {
15406 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15407 "property for target %d lun %d (COMPATIBLE)",
15408 target, lun);
15409 ndi_rtn = NDI_FAILURE;
15410 goto phys_create_done;
15414 * We need the SAS WWN for non-multipath devices, so
15415 * we'll use the same property as that multipathing
15416 * devices need to present for MPAPI. If we don't have
15417 * a WWN (e.g. parallel SCSI), don't create the prop.
15419 wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
15420 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15421 if (sas_wwn && ndi_prop_update_string(DDI_DEV_T_NONE,
15422 *lun_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str)
15423 != DDI_PROP_SUCCESS) {
15424 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15425 "create property for SAS target %d lun %d "
15426 "(target-port)", target, lun);
15427 ndi_rtn = NDI_FAILURE;
15428 goto phys_create_done;
15431 be_sas_wwn = BE_64(sas_wwn);
15432 if (sas_wwn && ndi_prop_update_byte_array(
15433 DDI_DEV_T_NONE, *lun_dip, "port-wwn",
15434 (uchar_t *)&be_sas_wwn, 8) != DDI_PROP_SUCCESS) {
15435 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15436 "create property for SAS target %d lun %d "
15437 "(port-wwn)", target, lun);
15438 ndi_rtn = NDI_FAILURE;
15439 goto phys_create_done;
15440 } else if ((sas_wwn == 0) && (ndi_prop_update_int(
15441 DDI_DEV_T_NONE, *lun_dip, "sata-phy", phy) !=
15442 DDI_PROP_SUCCESS)) {
15444 * Direct attached SATA device without DeviceName
15446 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15447 "create property for SAS target %d lun %d "
15448 "(sata-phy)", target, lun);
15449 ndi_rtn = NDI_FAILURE;
15450 goto phys_create_done;
15453 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
15454 *lun_dip, SAS_PROP) != DDI_PROP_SUCCESS) {
15455 mptsas_log(mpt, CE_WARN, "mptsas unable to"
15456 "create property for SAS target %d lun %d"
15457 " (SAS_PROP)", target, lun);
15458 ndi_rtn = NDI_FAILURE;
15459 goto phys_create_done;
15461 if (guid && (ndi_prop_update_string(DDI_DEV_T_NONE,
15462 *lun_dip, NDI_GUID, guid) != DDI_SUCCESS)) {
15463 mptsas_log(mpt, CE_WARN, "mptsas unable "
15464 "to create guid property for target %d "
15465 "lun %d", target, lun);
15466 ndi_rtn = NDI_FAILURE;
15467 goto phys_create_done;
15471 * The following code is to set properties for SM-HBA support,
15472 * it doesn't apply to RAID volumes
15474 if (ptgt->m_addr.mta_phymask == 0)
15475 goto phys_raid_lun;
15477 mutex_enter(&mpt->m_mutex);
15479 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15480 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15481 (uint32_t)ptgt->m_devhdl;
15482 rval = mptsas_get_sas_device_page0(mpt, page_address,
15483 &dev_hdl, &dev_sas_wwn, &dev_info,
15484 &physport, &phy_id, &pdev_hdl,
15485 &bay_num, &enclosure, &io_flags);
15486 if (rval != DDI_SUCCESS) {
15487 mutex_exit(&mpt->m_mutex);
15488 mptsas_log(mpt, CE_WARN, "mptsas unable to get"
15489 "parent device for handle %d.", page_address);
15490 ndi_rtn = NDI_FAILURE;
15491 goto phys_create_done;
15494 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15495 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
15496 rval = mptsas_get_sas_device_page0(mpt, page_address,
15497 &dev_hdl, &pdev_sas_wwn, &pdev_info, &physport,
15498 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15499 if (rval != DDI_SUCCESS) {
15500 mutex_exit(&mpt->m_mutex);
15501 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15502 "device for handle %d.", page_address);
15503 ndi_rtn = NDI_FAILURE;
15504 goto phys_create_done;
15507 mutex_exit(&mpt->m_mutex);
15510 * If this device direct attached to the controller
15511 * set the attached-port to the base wwid
15513 if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15514 != DEVINFO_DIRECT_ATTACHED) {
15515 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15516 pdev_sas_wwn);
15517 } else {
15519 * Update the iport's attached-port to guid
15521 if (sas_wwn == 0) {
15522 (void) sprintf(wwn_str, "p%x", phy);
15523 } else {
15524 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15526 if (ddi_prop_update_string(DDI_DEV_T_NONE,
15527 pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15528 DDI_PROP_SUCCESS) {
15529 mptsas_log(mpt, CE_WARN,
15530 "mptsas unable to create "
15531 "property for iport target-port"
15532 " %s (sas_wwn)",
15533 wwn_str);
15534 ndi_rtn = NDI_FAILURE;
15535 goto phys_create_done;
15538 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15539 mpt->un.m_base_wwid);
15542 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15543 *lun_dip, SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
15544 DDI_PROP_SUCCESS) {
15545 mptsas_log(mpt, CE_WARN,
15546 "mptsas unable to create "
15547 "property for iport attached-port %s (sas_wwn)",
15548 attached_wwn_str);
15549 ndi_rtn = NDI_FAILURE;
15550 goto phys_create_done;
15553 if (IS_SATA_DEVICE(dev_info)) {
15554 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15555 *lun_dip, MPTSAS_VARIANT, "sata") !=
15556 DDI_PROP_SUCCESS) {
15557 mptsas_log(mpt, CE_WARN,
15558 "mptsas unable to create "
15559 "property for device variant ");
15560 ndi_rtn = NDI_FAILURE;
15561 goto phys_create_done;
15565 if (IS_ATAPI_DEVICE(dev_info)) {
15566 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15567 *lun_dip, MPTSAS_VARIANT, "atapi") !=
15568 DDI_PROP_SUCCESS) {
15569 mptsas_log(mpt, CE_WARN,
15570 "mptsas unable to create "
15571 "property for device variant ");
15572 ndi_rtn = NDI_FAILURE;
15573 goto phys_create_done;
15577 phys_raid_lun:
15579 * if this is a SAS controller, and the target is a SATA
15580 * drive, set the 'pm-capable' property for sd and if on
15581 * an OPL platform, also check if this is an ATAPI
15582 * device.
15584 instance = ddi_get_instance(mpt->m_dip);
15585 if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
15586 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
15587 NDBG2(("mptsas%d: creating pm-capable property, "
15588 "target %d", instance, target));
15590 if ((ndi_prop_update_int(DDI_DEV_T_NONE,
15591 *lun_dip, "pm-capable", 1)) !=
15592 DDI_PROP_SUCCESS) {
15593 mptsas_log(mpt, CE_WARN, "mptsas "
15594 "failed to create pm-capable "
15595 "property, target %d", target);
15596 ndi_rtn = NDI_FAILURE;
15597 goto phys_create_done;
15602 if ((inq->inq_dtype == 0) || (inq->inq_dtype == 5)) {
15604 * add 'obp-path' properties for devinfo
15606 bzero(wwn_str, sizeof (wwn_str));
15607 (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
15608 component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15609 if (guid) {
15610 (void) snprintf(component, MAXPATHLEN,
15611 "disk@w%s,%x", wwn_str, lun);
15612 } else {
15613 (void) snprintf(component, MAXPATHLEN,
15614 "disk@p%x,%x", phy, lun);
15616 if (ddi_pathname_obp_set(*lun_dip, component)
15617 != DDI_SUCCESS) {
15618 mptsas_log(mpt, CE_WARN, "mpt_sas driver "
15619 "unable to set obp-path for SAS "
15620 "object %s", component);
15621 ndi_rtn = NDI_FAILURE;
15622 goto phys_create_done;
15626 * Create the phy-num property for non-raid disk
15628 if (ptgt->m_addr.mta_phymask != 0) {
15629 if (ndi_prop_update_int(DDI_DEV_T_NONE,
15630 *lun_dip, "phy-num", ptgt->m_phynum) !=
15631 DDI_PROP_SUCCESS) {
15632 mptsas_log(mpt, CE_WARN, "mptsas driver "
15633 "failed to create phy-num property for "
15634 "target %d", target);
15635 ndi_rtn = NDI_FAILURE;
15636 goto phys_create_done;
15639 phys_create_done:
15641 * If props were setup ok, online the lun
15643 if (ndi_rtn == NDI_SUCCESS) {
15645 * Try to online the new node
15647 ndi_rtn = ndi_devi_online(*lun_dip, NDI_ONLINE_ATTACH);
15649 if (ndi_rtn == NDI_SUCCESS) {
15650 mutex_enter(&mpt->m_mutex);
15651 ptgt->m_led_status = 0;
15652 (void) mptsas_flush_led_status(mpt, ptgt);
15653 mutex_exit(&mpt->m_mutex);
15657 * If success set rtn flag, else unwire alloc'd lun
15659 if (ndi_rtn != NDI_SUCCESS) {
15660 NDBG12(("mptsas driver unable to online "
15661 "target %d lun %d", target, lun));
15662 ndi_prop_remove_all(*lun_dip);
15663 (void) ndi_devi_free(*lun_dip);
15664 *lun_dip = NULL;
15668 scsi_hba_nodename_compatible_free(nodename, compatible);
15670 if (wwn_str != NULL) {
15671 kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
15673 if (component != NULL) {
15674 kmem_free(component, MAXPATHLEN);
15678 return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15681 static int
15682 mptsas_probe_smp(dev_info_t *pdip, uint64_t wwn)
15684 mptsas_t *mpt = DIP2MPT(pdip);
15685 struct smp_device smp_sd;
15687 /* XXX An HBA driver should not be allocating an smp_device. */
15688 bzero(&smp_sd, sizeof (struct smp_device));
15689 smp_sd.smp_sd_address.smp_a_hba_tran = mpt->m_smptran;
15690 bcopy(&wwn, smp_sd.smp_sd_address.smp_a_wwn, SAS_WWN_BYTE_SIZE);
15692 if (smp_probe(&smp_sd) != DDI_PROBE_SUCCESS)
15693 return (NDI_FAILURE);
15694 return (NDI_SUCCESS);
15697 static int
15698 mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn, dev_info_t **smp_dip)
15700 mptsas_t *mpt = DIP2MPT(pdip);
15701 mptsas_smp_t *psmp = NULL;
15702 int rval;
15703 int phymask;
15706 * Get the physical port associated to the iport
15707 * PHYMASK TODO
15709 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
15710 "phymask", 0);
15712 * Find the smp node in hash table with specified sas address and
15713 * physical port
15715 psmp = mptsas_wwid_to_psmp(mpt, phymask, sas_wwn);
15716 if (psmp == NULL) {
15717 return (DDI_FAILURE);
15720 rval = mptsas_online_smp(pdip, psmp, smp_dip);
15722 return (rval);
15725 static int
15726 mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
15727 dev_info_t **smp_dip)
15729 char wwn_str[MPTSAS_WWN_STRLEN];
15730 char attached_wwn_str[MPTSAS_WWN_STRLEN];
15731 int ndi_rtn = NDI_FAILURE;
15732 int rval = 0;
15733 mptsas_smp_t dev_info;
15734 uint32_t page_address;
15735 mptsas_t *mpt = DIP2MPT(pdip);
15736 uint16_t dev_hdl;
15737 uint64_t sas_wwn;
15738 uint64_t smp_sas_wwn;
15739 uint8_t physport;
15740 uint8_t phy_id;
15741 uint16_t pdev_hdl;
15742 uint8_t numphys = 0;
15743 uint16_t i = 0;
15744 char phymask[MPTSAS_MAX_PHYS];
15745 char *iport = NULL;
15746 mptsas_phymask_t phy_mask = 0;
15747 uint16_t attached_devhdl;
15748 uint16_t bay_num, enclosure, io_flags;
15750 (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_addr.mta_wwn);
15753 * Probe smp device, prevent the node of removed device from being
15754 * configured succesfully
15756 if (mptsas_probe_smp(pdip, smp_node->m_addr.mta_wwn) != NDI_SUCCESS) {
15757 return (DDI_FAILURE);
15760 if ((*smp_dip = mptsas_find_smp_child(pdip, wwn_str)) != NULL) {
15761 return (DDI_SUCCESS);
15764 ndi_rtn = ndi_devi_alloc(pdip, "smp", DEVI_SID_NODEID, smp_dip);
15767 * if lun alloc success, set props
15769 if (ndi_rtn == NDI_SUCCESS) {
15771 * Set the flavor of the child to be SMP flavored
15773 ndi_flavor_set(*smp_dip, SCSA_FLAVOR_SMP);
15775 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15776 *smp_dip, SMP_WWN, wwn_str) !=
15777 DDI_PROP_SUCCESS) {
15778 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15779 "property for smp device %s (sas_wwn)",
15780 wwn_str);
15781 ndi_rtn = NDI_FAILURE;
15782 goto smp_create_done;
15784 (void) sprintf(wwn_str, "w%"PRIx64, smp_node->m_addr.mta_wwn);
15785 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15786 *smp_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str) !=
15787 DDI_PROP_SUCCESS) {
15788 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15789 "property for iport target-port %s (sas_wwn)",
15790 wwn_str);
15791 ndi_rtn = NDI_FAILURE;
15792 goto smp_create_done;
15795 mutex_enter(&mpt->m_mutex);
15797 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
15798 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | smp_node->m_devhdl;
15799 rval = mptsas_get_sas_expander_page0(mpt, page_address,
15800 &dev_info);
15801 if (rval != DDI_SUCCESS) {
15802 mutex_exit(&mpt->m_mutex);
15803 mptsas_log(mpt, CE_WARN,
15804 "mptsas unable to get expander "
15805 "parent device info for %x", page_address);
15806 ndi_rtn = NDI_FAILURE;
15807 goto smp_create_done;
15810 smp_node->m_pdevhdl = dev_info.m_pdevhdl;
15811 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15812 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15813 (uint32_t)dev_info.m_pdevhdl;
15814 rval = mptsas_get_sas_device_page0(mpt, page_address,
15815 &dev_hdl, &sas_wwn, &smp_node->m_pdevinfo, &physport,
15816 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15817 if (rval != DDI_SUCCESS) {
15818 mutex_exit(&mpt->m_mutex);
15819 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15820 "device info for %x", page_address);
15821 ndi_rtn = NDI_FAILURE;
15822 goto smp_create_done;
15825 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15826 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15827 (uint32_t)dev_info.m_devhdl;
15828 rval = mptsas_get_sas_device_page0(mpt, page_address,
15829 &dev_hdl, &smp_sas_wwn, &smp_node->m_deviceinfo,
15830 &physport, &phy_id, &pdev_hdl, &bay_num, &enclosure,
15831 &io_flags);
15832 if (rval != DDI_SUCCESS) {
15833 mutex_exit(&mpt->m_mutex);
15834 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15835 "device info for %x", page_address);
15836 ndi_rtn = NDI_FAILURE;
15837 goto smp_create_done;
15839 mutex_exit(&mpt->m_mutex);
15842 * If this smp direct attached to the controller
15843 * set the attached-port to the base wwid
15845 if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15846 != DEVINFO_DIRECT_ATTACHED) {
15847 (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15848 sas_wwn);
15849 } else {
15850 (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15851 mpt->un.m_base_wwid);
15854 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15855 *smp_dip, SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwn_str) !=
15856 DDI_PROP_SUCCESS) {
15857 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15858 "property for smp attached-port %s (sas_wwn)",
15859 attached_wwn_str);
15860 ndi_rtn = NDI_FAILURE;
15861 goto smp_create_done;
15864 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
15865 *smp_dip, SMP_PROP) != DDI_PROP_SUCCESS) {
15866 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15867 "create property for SMP %s (SMP_PROP) ",
15868 wwn_str);
15869 ndi_rtn = NDI_FAILURE;
15870 goto smp_create_done;
15874 * check the smp to see whether it direct
15875 * attached to the controller
15877 if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15878 != DEVINFO_DIRECT_ATTACHED) {
15879 goto smp_create_done;
15881 numphys = ddi_prop_get_int(DDI_DEV_T_ANY, pdip,
15882 DDI_PROP_DONTPASS, MPTSAS_NUM_PHYS, -1);
15883 if (numphys > 0) {
15884 goto smp_create_done;
15887 * this iport is an old iport, we need to
15888 * reconfig the props for it.
15890 if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15891 MPTSAS_VIRTUAL_PORT, 0) !=
15892 DDI_PROP_SUCCESS) {
15893 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15894 MPTSAS_VIRTUAL_PORT);
15895 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
15896 "prop update failed");
15897 goto smp_create_done;
15900 mutex_enter(&mpt->m_mutex);
15901 numphys = 0;
15902 iport = ddi_get_name_addr(pdip);
15903 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15904 bzero(phymask, sizeof (phymask));
15905 (void) sprintf(phymask,
15906 "%x", mpt->m_phy_info[i].phy_mask);
15907 if (strcmp(phymask, iport) == 0) {
15908 phy_mask = mpt->m_phy_info[i].phy_mask;
15909 break;
15913 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15914 if ((phy_mask >> i) & 0x01) {
15915 numphys++;
15919 * Update PHY info for smhba
15921 if (mptsas_smhba_phy_init(mpt)) {
15922 mutex_exit(&mpt->m_mutex);
15923 mptsas_log(mpt, CE_WARN, "mptsas phy update "
15924 "failed");
15925 goto smp_create_done;
15927 mutex_exit(&mpt->m_mutex);
15929 mptsas_smhba_set_all_phy_props(mpt, pdip, numphys, phy_mask,
15930 &attached_devhdl);
15932 if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15933 MPTSAS_NUM_PHYS, numphys) !=
15934 DDI_PROP_SUCCESS) {
15935 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15936 MPTSAS_NUM_PHYS);
15937 mptsas_log(mpt, CE_WARN, "mptsas update "
15938 "num phys props failed");
15939 goto smp_create_done;
15942 * Add parent's props for SMHBA support
15944 if (ddi_prop_update_string(DDI_DEV_T_NONE, pdip,
15945 SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15946 DDI_PROP_SUCCESS) {
15947 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15948 SCSI_ADDR_PROP_ATTACHED_PORT);
15949 mptsas_log(mpt, CE_WARN, "mptsas update iport"
15950 "attached-port failed");
15951 goto smp_create_done;
15954 smp_create_done:
15956 * If props were setup ok, online the lun
15958 if (ndi_rtn == NDI_SUCCESS) {
15960 * Try to online the new node
15962 ndi_rtn = ndi_devi_online(*smp_dip, NDI_ONLINE_ATTACH);
15966 * If success set rtn flag, else unwire alloc'd lun
15968 if (ndi_rtn != NDI_SUCCESS) {
15969 NDBG12(("mptsas unable to online "
15970 "SMP target %s", wwn_str));
15971 ndi_prop_remove_all(*smp_dip);
15972 (void) ndi_devi_free(*smp_dip);
15976 return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15979 /* smp transport routine */
15980 static int mptsas_smp_start(struct smp_pkt *smp_pkt)
15982 uint64_t wwn;
15983 Mpi2SmpPassthroughRequest_t req;
15984 Mpi2SmpPassthroughReply_t rep;
15985 uint32_t direction = 0;
15986 mptsas_t *mpt;
15987 int ret;
15988 uint64_t tmp64;
15990 mpt = (mptsas_t *)smp_pkt->smp_pkt_address->
15991 smp_a_hba_tran->smp_tran_hba_private;
15993 bcopy(smp_pkt->smp_pkt_address->smp_a_wwn, &wwn, SAS_WWN_BYTE_SIZE);
15995 * Need to compose a SMP request message
15996 * and call mptsas_do_passthru() function
15998 bzero(&req, sizeof (req));
15999 bzero(&rep, sizeof (rep));
16000 req.PassthroughFlags = 0;
16001 req.PhysicalPort = 0xff;
16002 req.ChainOffset = 0;
16003 req.Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
16005 if ((smp_pkt->smp_pkt_reqsize & 0xffff0000ul) != 0) {
16006 smp_pkt->smp_pkt_reason = ERANGE;
16007 return (DDI_FAILURE);
16009 req.RequestDataLength = LE_16((uint16_t)(smp_pkt->smp_pkt_reqsize - 4));
16011 req.MsgFlags = 0;
16012 tmp64 = LE_64(wwn);
16013 bcopy(&tmp64, &req.SASAddress, SAS_WWN_BYTE_SIZE);
16014 if (smp_pkt->smp_pkt_rspsize > 0) {
16015 direction |= MPTSAS_PASS_THRU_DIRECTION_READ;
16017 if (smp_pkt->smp_pkt_reqsize > 0) {
16018 direction |= MPTSAS_PASS_THRU_DIRECTION_WRITE;
16021 mutex_enter(&mpt->m_mutex);
16022 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep,
16023 (uint8_t *)smp_pkt->smp_pkt_rsp,
16024 offsetof(Mpi2SmpPassthroughRequest_t, SGL), sizeof (rep),
16025 smp_pkt->smp_pkt_rspsize - 4, direction,
16026 (uint8_t *)smp_pkt->smp_pkt_req, smp_pkt->smp_pkt_reqsize - 4,
16027 smp_pkt->smp_pkt_timeout, FKIOCTL);
16028 mutex_exit(&mpt->m_mutex);
16029 if (ret != 0) {
16030 cmn_err(CE_WARN, "smp_start do passthru error %d", ret);
16031 smp_pkt->smp_pkt_reason = (uchar_t)(ret);
16032 return (DDI_FAILURE);
16034 /* do passthrough success, check the smp status */
16035 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
16036 switch (LE_16(rep.IOCStatus)) {
16037 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
16038 smp_pkt->smp_pkt_reason = ENODEV;
16039 break;
16040 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
16041 smp_pkt->smp_pkt_reason = EOVERFLOW;
16042 break;
16043 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
16044 smp_pkt->smp_pkt_reason = EIO;
16045 break;
16046 default:
16047 mptsas_log(mpt, CE_NOTE, "smp_start: get unknown ioc"
16048 "status:%x", LE_16(rep.IOCStatus));
16049 smp_pkt->smp_pkt_reason = EIO;
16050 break;
16052 return (DDI_FAILURE);
16054 if (rep.SASStatus != MPI2_SASSTATUS_SUCCESS) {
16055 mptsas_log(mpt, CE_NOTE, "smp_start: get error SAS status:%x",
16056 rep.SASStatus);
16057 smp_pkt->smp_pkt_reason = EIO;
16058 return (DDI_FAILURE);
16061 return (DDI_SUCCESS);
16065 * If we didn't get a match, we need to get sas page0 for each device, and
16066 * untill we get a match. If failed, return NULL
16068 static mptsas_target_t *
16069 mptsas_phy_to_tgt(mptsas_t *mpt, mptsas_phymask_t phymask, uint8_t phy)
16071 int i, j = 0;
16072 int rval = 0;
16073 uint16_t cur_handle;
16074 uint32_t page_address;
16075 mptsas_target_t *ptgt = NULL;
16078 * PHY named device must be direct attached and attaches to
16079 * narrow port, if the iport is not parent of the device which
16080 * we are looking for.
16082 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
16083 if ((1 << i) & phymask)
16084 j++;
16087 if (j > 1)
16088 return (NULL);
16091 * Must be a narrow port and single device attached to the narrow port
16092 * So the physical port num of device which is equal to the iport's
16093 * port num is the device what we are looking for.
16096 if (mpt->m_phy_info[phy].phy_mask != phymask)
16097 return (NULL);
16099 mutex_enter(&mpt->m_mutex);
16101 ptgt = refhash_linear_search(mpt->m_targets, mptsas_target_eval_nowwn,
16102 &phy);
16103 if (ptgt != NULL) {
16104 mutex_exit(&mpt->m_mutex);
16105 return (ptgt);
16108 if (mpt->m_done_traverse_dev) {
16109 mutex_exit(&mpt->m_mutex);
16110 return (NULL);
16113 /* If didn't get a match, come here */
16114 cur_handle = mpt->m_dev_handle;
16115 for (; ; ) {
16116 ptgt = NULL;
16117 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
16118 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)cur_handle;
16119 rval = mptsas_get_target_device_info(mpt, page_address,
16120 &cur_handle, &ptgt);
16121 if ((rval == DEV_INFO_FAIL_PAGE0) ||
16122 (rval == DEV_INFO_FAIL_ALLOC)) {
16123 break;
16125 if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
16126 (rval == DEV_INFO_PHYS_DISK)) {
16127 continue;
16129 mpt->m_dev_handle = cur_handle;
16131 if ((ptgt->m_addr.mta_wwn == 0) && (ptgt->m_phynum == phy)) {
16132 break;
16136 mutex_exit(&mpt->m_mutex);
16137 return (ptgt);
16141 * The ptgt->m_addr.mta_wwn contains the wwid for each disk.
16142 * For Raid volumes, we need to check m_raidvol[x].m_raidwwid
16143 * If we didn't get a match, we need to get sas page0 for each device, and
16144 * untill we get a match
16145 * If failed, return NULL
16147 static mptsas_target_t *
16148 mptsas_wwid_to_ptgt(mptsas_t *mpt, mptsas_phymask_t phymask, uint64_t wwid)
16150 int rval = 0;
16151 uint16_t cur_handle;
16152 uint32_t page_address;
16153 mptsas_target_t *tmp_tgt = NULL;
16154 mptsas_target_addr_t addr;
16156 addr.mta_wwn = wwid;
16157 addr.mta_phymask = phymask;
16158 mutex_enter(&mpt->m_mutex);
16159 tmp_tgt = refhash_lookup(mpt->m_targets, &addr);
16160 if (tmp_tgt != NULL) {
16161 mutex_exit(&mpt->m_mutex);
16162 return (tmp_tgt);
16165 if (phymask == 0) {
16167 * It's IR volume
16169 rval = mptsas_get_raid_info(mpt);
16170 if (rval) {
16171 tmp_tgt = refhash_lookup(mpt->m_targets, &addr);
16173 mutex_exit(&mpt->m_mutex);
16174 return (tmp_tgt);
16177 if (mpt->m_done_traverse_dev) {
16178 mutex_exit(&mpt->m_mutex);
16179 return (NULL);
16182 /* If didn't get a match, come here */
16183 cur_handle = mpt->m_dev_handle;
16184 for (;;) {
16185 tmp_tgt = NULL;
16186 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
16187 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | cur_handle;
16188 rval = mptsas_get_target_device_info(mpt, page_address,
16189 &cur_handle, &tmp_tgt);
16190 if ((rval == DEV_INFO_FAIL_PAGE0) ||
16191 (rval == DEV_INFO_FAIL_ALLOC)) {
16192 tmp_tgt = NULL;
16193 break;
16195 if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
16196 (rval == DEV_INFO_PHYS_DISK)) {
16197 continue;
16199 mpt->m_dev_handle = cur_handle;
16200 if ((tmp_tgt->m_addr.mta_wwn) &&
16201 (tmp_tgt->m_addr.mta_wwn == wwid) &&
16202 (tmp_tgt->m_addr.mta_phymask == phymask)) {
16203 break;
16207 mutex_exit(&mpt->m_mutex);
16208 return (tmp_tgt);
16211 static mptsas_smp_t *
16212 mptsas_wwid_to_psmp(mptsas_t *mpt, mptsas_phymask_t phymask, uint64_t wwid)
16214 int rval = 0;
16215 uint16_t cur_handle;
16216 uint32_t page_address;
16217 mptsas_smp_t smp_node, *psmp = NULL;
16218 mptsas_target_addr_t addr;
16220 addr.mta_wwn = wwid;
16221 addr.mta_phymask = phymask;
16222 mutex_enter(&mpt->m_mutex);
16223 psmp = refhash_lookup(mpt->m_smp_targets, &addr);
16224 if (psmp != NULL) {
16225 mutex_exit(&mpt->m_mutex);
16226 return (psmp);
16229 if (mpt->m_done_traverse_smp) {
16230 mutex_exit(&mpt->m_mutex);
16231 return (NULL);
16234 /* If didn't get a match, come here */
16235 cur_handle = mpt->m_smp_devhdl;
16236 for (;;) {
16237 psmp = NULL;
16238 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
16239 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)cur_handle;
16240 rval = mptsas_get_sas_expander_page0(mpt, page_address,
16241 &smp_node);
16242 if (rval != DDI_SUCCESS) {
16243 break;
16245 mpt->m_smp_devhdl = cur_handle = smp_node.m_devhdl;
16246 psmp = mptsas_smp_alloc(mpt, &smp_node);
16247 ASSERT(psmp);
16248 if ((psmp->m_addr.mta_wwn) && (psmp->m_addr.mta_wwn == wwid) &&
16249 (psmp->m_addr.mta_phymask == phymask)) {
16250 break;
16254 mutex_exit(&mpt->m_mutex);
16255 return (psmp);
16258 mptsas_target_t *
16259 mptsas_tgt_alloc(mptsas_t *mpt, uint16_t devhdl, uint64_t wwid,
16260 uint32_t devinfo, mptsas_phymask_t phymask, uint8_t phynum)
16262 mptsas_target_t *tmp_tgt = NULL;
16263 mptsas_target_addr_t addr;
16265 addr.mta_wwn = wwid;
16266 addr.mta_phymask = phymask;
16267 tmp_tgt = refhash_lookup(mpt->m_targets, &addr);
16268 if (tmp_tgt != NULL) {
16269 NDBG20(("Hash item already exist"));
16270 tmp_tgt->m_deviceinfo = devinfo;
16271 tmp_tgt->m_devhdl = devhdl; /* XXX - duplicate? */
16272 return (tmp_tgt);
16274 tmp_tgt = kmem_zalloc(sizeof (struct mptsas_target), KM_SLEEP);
16275 if (tmp_tgt == NULL) {
16276 cmn_err(CE_WARN, "Fatal, allocated tgt failed");
16277 return (NULL);
16279 tmp_tgt->m_devhdl = devhdl;
16280 tmp_tgt->m_addr.mta_wwn = wwid;
16281 tmp_tgt->m_deviceinfo = devinfo;
16282 tmp_tgt->m_addr.mta_phymask = phymask;
16283 tmp_tgt->m_phynum = phynum;
16284 /* Initialized the tgt structure */
16285 tmp_tgt->m_qfull_retries = QFULL_RETRIES;
16286 tmp_tgt->m_qfull_retry_interval =
16287 drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
16288 tmp_tgt->m_t_throttle = MAX_THROTTLE;
16289 TAILQ_INIT(&tmp_tgt->m_active_cmdq);
16291 refhash_insert(mpt->m_targets, tmp_tgt);
16293 return (tmp_tgt);
16296 static void
16297 mptsas_smp_target_copy(mptsas_smp_t *src, mptsas_smp_t *dst)
16299 dst->m_devhdl = src->m_devhdl;
16300 dst->m_deviceinfo = src->m_deviceinfo;
16301 dst->m_pdevhdl = src->m_pdevhdl;
16302 dst->m_pdevinfo = src->m_pdevinfo;
16305 static mptsas_smp_t *
16306 mptsas_smp_alloc(mptsas_t *mpt, mptsas_smp_t *data)
16308 mptsas_target_addr_t addr;
16309 mptsas_smp_t *ret_data;
16311 addr.mta_wwn = data->m_addr.mta_wwn;
16312 addr.mta_phymask = data->m_addr.mta_phymask;
16313 ret_data = refhash_lookup(mpt->m_smp_targets, &addr);
16315 * If there's already a matching SMP target, update its fields
16316 * in place. Since the address is not changing, it's safe to do
16317 * this. We cannot just bcopy() here because the structure we've
16318 * been given has invalid hash links.
16320 if (ret_data != NULL) {
16321 mptsas_smp_target_copy(data, ret_data);
16322 return (ret_data);
16325 ret_data = kmem_alloc(sizeof (mptsas_smp_t), KM_SLEEP);
16326 bcopy(data, ret_data, sizeof (mptsas_smp_t));
16327 refhash_insert(mpt->m_smp_targets, ret_data);
16328 return (ret_data);
16332 * Functions for SGPIO LED support
16334 static dev_info_t *
16335 mptsas_get_dip_from_dev(dev_t dev, mptsas_phymask_t *phymask)
16337 dev_info_t *dip;
16338 int prop;
16339 dip = e_ddi_hold_devi_by_dev(dev, 0);
16340 if (dip == NULL)
16341 return (dip);
16342 prop = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
16343 "phymask", 0);
16344 *phymask = (mptsas_phymask_t)prop;
16345 ddi_release_devi(dip);
16346 return (dip);
16348 static mptsas_target_t *
16349 mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr, mptsas_phymask_t phymask)
16351 uint8_t phynum;
16352 uint64_t wwn;
16353 int lun;
16354 mptsas_target_t *ptgt = NULL;
16356 if (mptsas_parse_address(addr, &wwn, &phynum, &lun) != DDI_SUCCESS) {
16357 return (NULL);
16359 if (addr[0] == 'w') {
16360 ptgt = mptsas_wwid_to_ptgt(mpt, (int)phymask, wwn);
16361 } else {
16362 ptgt = mptsas_phy_to_tgt(mpt, (int)phymask, phynum);
16364 return (ptgt);
16367 static int
16368 mptsas_flush_led_status(mptsas_t *mpt, mptsas_target_t *ptgt)
16370 uint32_t slotstatus = 0;
16372 /* Build an MPI2 Slot Status based on our view of the world */
16373 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_IDENT - 1)))
16374 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_IDENTIFY_REQUEST;
16375 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_FAIL - 1)))
16376 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
16377 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_OK2RM - 1)))
16378 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_REQUEST_REMOVE;
16380 /* Write it to the controller */
16381 NDBG14(("mptsas_ioctl: set LED status %x for slot %x",
16382 slotstatus, ptgt->m_slot_num));
16383 return (mptsas_send_sep(mpt, ptgt, &slotstatus,
16384 MPI2_SEP_REQ_ACTION_WRITE_STATUS));
16388 * send sep request, use enclosure/slot addressing
16390 static int
16391 mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
16392 uint32_t *status, uint8_t act)
16394 Mpi2SepRequest_t req;
16395 Mpi2SepReply_t rep;
16396 int ret;
16398 ASSERT(mutex_owned(&mpt->m_mutex));
16401 * We only support SEP control of directly-attached targets, in which
16402 * case the "SEP" we're talking to is a virtual one contained within
16403 * the HBA itself. This is necessary because DA targets typically have
16404 * no other mechanism for LED control. Targets for which a separate
16405 * enclosure service processor exists should be controlled via ses(7d)
16406 * or sgen(7d). Furthermore, since such requests can time out, they
16407 * should be made in user context rather than in response to
16408 * asynchronous fabric changes.
16410 * In addition, we do not support this operation for RAID volumes,
16411 * since there is no slot associated with them.
16413 if (!(ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED) ||
16414 ptgt->m_addr.mta_phymask == 0) {
16415 return (ENOTTY);
16418 bzero(&req, sizeof (req));
16419 bzero(&rep, sizeof (rep));
16421 req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
16422 req.Action = act;
16423 req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
16424 req.EnclosureHandle = LE_16(ptgt->m_enclosure);
16425 req.Slot = LE_16(ptgt->m_slot_num);
16426 if (act == MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16427 req.SlotStatus = LE_32(*status);
16429 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
16430 sizeof (req), sizeof (rep), NULL, 0, NULL, 0, 60, FKIOCTL);
16431 if (ret != 0) {
16432 mptsas_log(mpt, CE_NOTE, "mptsas_send_sep: passthru SEP "
16433 "Processor Request message error %d", ret);
16434 return (ret);
16436 /* do passthrough success, check the ioc status */
16437 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
16438 mptsas_log(mpt, CE_NOTE, "send_sep act %x: ioc "
16439 "status:%x loginfo %x", act, LE_16(rep.IOCStatus),
16440 LE_32(rep.IOCLogInfo));
16441 switch (LE_16(rep.IOCStatus) & MPI2_IOCSTATUS_MASK) {
16442 case MPI2_IOCSTATUS_INVALID_FUNCTION:
16443 case MPI2_IOCSTATUS_INVALID_VPID:
16444 case MPI2_IOCSTATUS_INVALID_FIELD:
16445 case MPI2_IOCSTATUS_INVALID_STATE:
16446 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
16447 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
16448 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
16449 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
16450 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
16451 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
16452 return (EINVAL);
16453 case MPI2_IOCSTATUS_BUSY:
16454 return (EBUSY);
16455 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
16456 return (EAGAIN);
16457 case MPI2_IOCSTATUS_INVALID_SGL:
16458 case MPI2_IOCSTATUS_INTERNAL_ERROR:
16459 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
16460 default:
16461 return (EIO);
16464 if (act != MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16465 *status = LE_32(rep.SlotStatus);
16468 return (0);
16472 mptsas_dma_addr_create(mptsas_t *mpt, ddi_dma_attr_t dma_attr,
16473 ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp, caddr_t *dma_memp,
16474 uint32_t alloc_size, ddi_dma_cookie_t *cookiep)
16476 ddi_dma_cookie_t new_cookie;
16477 size_t alloc_len;
16478 uint_t ncookie;
16480 if (cookiep == NULL)
16481 cookiep = &new_cookie;
16483 if (ddi_dma_alloc_handle(mpt->m_dip, &dma_attr, DDI_DMA_SLEEP,
16484 NULL, dma_hdp) != DDI_SUCCESS) {
16485 return (FALSE);
16488 if (ddi_dma_mem_alloc(*dma_hdp, alloc_size, &mpt->m_dev_acc_attr,
16489 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, dma_memp, &alloc_len,
16490 acc_hdp) != DDI_SUCCESS) {
16491 ddi_dma_free_handle(dma_hdp);
16492 *dma_hdp = NULL;
16493 return (FALSE);
16496 if (ddi_dma_addr_bind_handle(*dma_hdp, NULL, *dma_memp, alloc_len,
16497 (DDI_DMA_RDWR | DDI_DMA_CONSISTENT), DDI_DMA_SLEEP, NULL,
16498 cookiep, &ncookie) != DDI_DMA_MAPPED) {
16499 (void) ddi_dma_mem_free(acc_hdp);
16500 ddi_dma_free_handle(dma_hdp);
16501 *dma_hdp = NULL;
16502 return (FALSE);
16505 return (TRUE);
16508 void
16509 mptsas_dma_addr_destroy(ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp)
16511 if (*dma_hdp == NULL)
16512 return;
16514 (void) ddi_dma_unbind_handle(*dma_hdp);
16515 (void) ddi_dma_mem_free(acc_hdp);
16516 ddi_dma_free_handle(dma_hdp);
16517 *dma_hdp = NULL;