preprocessor cleanup: __sparc
[unleashed/tickless.git] / usr / src / uts / common / io / scsi / adapters / mpt_sas / mptsas.c
blob97c047b9d29fa01cca836eaf0e850df4ec4714d0
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 2016 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(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/sata/sata_hba.h>
80 #include <sys/scsi/generic/sas.h>
81 #include <sys/scsi/impl/scsi_sas.h>
83 #pragma pack(1)
84 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_type.h>
85 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2.h>
86 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_cnfg.h>
87 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_init.h>
88 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_ioc.h>
89 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_sas.h>
90 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_tool.h>
91 #include <sys/scsi/adapters/mpt_sas/mpi/mpi2_raid.h>
92 #pragma pack()
95 * private header files.
98 #include <sys/scsi/impl/scsi_reset_notify.h>
99 #include <sys/scsi/adapters/mpt_sas/mptsas_var.h>
100 #include <sys/scsi/adapters/mpt_sas/mptsas_ioctl.h>
101 #include <sys/scsi/adapters/mpt_sas/mptsas_smhba.h>
102 #include <sys/scsi/adapters/mpt_sas/mptsas_hash.h>
103 #include <sys/raidioctl.h>
105 #include <sys/fs/dv_node.h> /* devfs_clean */
108 * FMA header files
110 #include <sys/ddifm.h>
111 #include <sys/fm/protocol.h>
112 #include <sys/fm/util.h>
113 #include <sys/fm/io/ddi.h>
116 * autoconfiguration data and routines.
118 static int mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
119 static int mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
120 static int mptsas_power(dev_info_t *dip, int component, int level);
123 * cb_ops function
125 static int mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
126 cred_t *credp, int *rval);
127 static int mptsas_quiesce(dev_info_t *devi);
130 * Resource initilaization for hardware
132 static void mptsas_setup_cmd_reg(mptsas_t *mpt);
133 static void mptsas_disable_bus_master(mptsas_t *mpt);
134 static void mptsas_hba_fini(mptsas_t *mpt);
135 static void mptsas_cfg_fini(mptsas_t *mptsas_blkp);
136 static int mptsas_hba_setup(mptsas_t *mpt);
137 static void mptsas_hba_teardown(mptsas_t *mpt);
138 static int mptsas_config_space_init(mptsas_t *mpt);
139 static void mptsas_config_space_fini(mptsas_t *mpt);
140 static void mptsas_iport_register(mptsas_t *mpt);
141 static int mptsas_smp_setup(mptsas_t *mpt);
142 static void mptsas_smp_teardown(mptsas_t *mpt);
143 static int mptsas_cache_create(mptsas_t *mpt);
144 static void mptsas_cache_destroy(mptsas_t *mpt);
145 static int mptsas_alloc_request_frames(mptsas_t *mpt);
146 static int mptsas_alloc_sense_bufs(mptsas_t *mpt);
147 static int mptsas_alloc_reply_frames(mptsas_t *mpt);
148 static int mptsas_alloc_free_queue(mptsas_t *mpt);
149 static int mptsas_alloc_post_queue(mptsas_t *mpt);
150 static void mptsas_alloc_reply_args(mptsas_t *mpt);
151 static int mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
152 static void mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd);
153 static int mptsas_init_chip(mptsas_t *mpt, int first_time);
156 * SCSA function prototypes
158 static int mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
159 static int mptsas_scsi_reset(struct scsi_address *ap, int level);
160 static int mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
161 static int mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly);
162 static int mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value,
163 int tgtonly);
164 static void mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt);
165 static struct scsi_pkt *mptsas_scsi_init_pkt(struct scsi_address *ap,
166 struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
167 int tgtlen, int flags, int (*callback)(), caddr_t arg);
168 static void mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
169 static void mptsas_scsi_destroy_pkt(struct scsi_address *ap,
170 struct scsi_pkt *pkt);
171 static int mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
172 scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
173 static void mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
174 scsi_hba_tran_t *hba_tran, struct scsi_device *sd);
175 static int mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
176 void (*callback)(caddr_t), caddr_t arg);
177 static int mptsas_get_name(struct scsi_device *sd, char *name, int len);
178 static int mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len);
179 static int mptsas_scsi_quiesce(dev_info_t *dip);
180 static int mptsas_scsi_unquiesce(dev_info_t *dip);
181 static int mptsas_bus_config(dev_info_t *pdip, uint_t flags,
182 ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
185 * SMP functions
187 static int mptsas_smp_start(struct smp_pkt *smp_pkt);
190 * internal function prototypes.
192 static void mptsas_list_add(mptsas_t *mpt);
193 static void mptsas_list_del(mptsas_t *mpt);
195 static int mptsas_quiesce_bus(mptsas_t *mpt);
196 static int mptsas_unquiesce_bus(mptsas_t *mpt);
198 static int mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size);
199 static void mptsas_free_handshake_msg(mptsas_t *mpt);
201 static void mptsas_ncmds_checkdrain(void *arg);
203 static int mptsas_prepare_pkt(mptsas_cmd_t *cmd);
204 static int mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *sp);
205 static int mptsas_accept_txwq_and_pkt(mptsas_t *mpt, mptsas_cmd_t *sp);
206 static void mptsas_accept_tx_waitq(mptsas_t *mpt);
208 static int mptsas_do_detach(dev_info_t *dev);
209 static int mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl);
210 static int mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun,
211 struct scsi_pkt *pkt);
212 static int mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp);
214 static void mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd);
215 static void mptsas_handle_event(void *args);
216 static int mptsas_handle_event_sync(void *args);
217 static void mptsas_handle_dr(void *args);
218 static void mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
219 dev_info_t *pdip);
221 static void mptsas_restart_cmd(void *);
223 static void mptsas_flush_hba(mptsas_t *mpt);
224 static void mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun,
225 uint8_t tasktype);
226 static void mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd,
227 uchar_t reason, uint_t stat);
229 static uint_t mptsas_intr(caddr_t arg1, caddr_t arg2);
230 static void mptsas_process_intr(mptsas_t *mpt,
231 pMpi2ReplyDescriptorsUnion_t reply_desc_union);
232 static void mptsas_handle_scsi_io_success(mptsas_t *mpt,
233 pMpi2ReplyDescriptorsUnion_t reply_desc);
234 static void mptsas_handle_address_reply(mptsas_t *mpt,
235 pMpi2ReplyDescriptorsUnion_t reply_desc);
236 static int mptsas_wait_intr(mptsas_t *mpt, int polltime);
237 static void mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd,
238 uint32_t *control, pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl);
240 static void mptsas_watch(void *arg);
241 static void mptsas_watchsubr(mptsas_t *mpt);
242 static void mptsas_cmd_timeout(mptsas_t *mpt, mptsas_target_t *ptgt);
244 static void mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd);
245 static int mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
246 uint8_t *data, uint32_t request_size, uint32_t reply_size,
247 uint32_t data_size, uint32_t direction, uint8_t *dataout,
248 uint32_t dataout_size, short timeout, int mode);
249 static int mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl);
251 static uint8_t mptsas_get_fw_diag_buffer_number(mptsas_t *mpt,
252 uint32_t unique_id);
253 static void mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd);
254 static int mptsas_post_fw_diag_buffer(mptsas_t *mpt,
255 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code);
256 static int mptsas_release_fw_diag_buffer(mptsas_t *mpt,
257 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
258 uint32_t diag_type);
259 static int mptsas_diag_register(mptsas_t *mpt,
260 mptsas_fw_diag_register_t *diag_register, uint32_t *return_code);
261 static int mptsas_diag_unregister(mptsas_t *mpt,
262 mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code);
263 static int mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
264 uint32_t *return_code);
265 static int mptsas_diag_read_buffer(mptsas_t *mpt,
266 mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
267 uint32_t *return_code, int ioctl_mode);
268 static int mptsas_diag_release(mptsas_t *mpt,
269 mptsas_fw_diag_release_t *diag_release, uint32_t *return_code);
270 static int mptsas_do_diag_action(mptsas_t *mpt, uint32_t action,
271 uint8_t *diag_action, uint32_t length, uint32_t *return_code,
272 int ioctl_mode);
273 static int mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *data,
274 int mode);
276 static int mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
277 int cmdlen, int tgtlen, int statuslen, int kf);
278 static void mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd);
280 static int mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags);
281 static void mptsas_kmem_cache_destructor(void *buf, void *cdrarg);
283 static int mptsas_cache_frames_constructor(void *buf, void *cdrarg,
284 int kmflags);
285 static void mptsas_cache_frames_destructor(void *buf, void *cdrarg);
287 static void mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
288 mptsas_cmd_t *cmd);
289 static void mptsas_check_task_mgt(mptsas_t *mpt,
290 pMpi2SCSIManagementReply_t reply, mptsas_cmd_t *cmd);
291 static int mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
292 mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
293 int *resid);
295 static int mptsas_alloc_active_slots(mptsas_t *mpt, int flag);
296 static void mptsas_free_active_slots(mptsas_t *mpt);
297 static int mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
299 static void mptsas_restart_hba(mptsas_t *mpt);
300 static void mptsas_restart_waitq(mptsas_t *mpt);
302 static void mptsas_deliver_doneq_thread(mptsas_t *mpt);
303 static void mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd);
304 static void mptsas_doneq_mv(mptsas_t *mpt, uint64_t t);
306 static mptsas_cmd_t *mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t);
307 static void mptsas_doneq_empty(mptsas_t *mpt);
308 static void mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg);
310 static mptsas_cmd_t *mptsas_waitq_rm(mptsas_t *mpt);
311 static void mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd);
312 static mptsas_cmd_t *mptsas_tx_waitq_rm(mptsas_t *mpt);
313 static void mptsas_tx_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd);
316 static void mptsas_start_watch_reset_delay();
317 static void mptsas_setup_bus_reset_delay(mptsas_t *mpt);
318 static void mptsas_watch_reset_delay(void *arg);
319 static int mptsas_watch_reset_delay_subr(mptsas_t *mpt);
322 * helper functions
324 static void mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd);
326 static dev_info_t *mptsas_find_child(dev_info_t *pdip, char *name);
327 static dev_info_t *mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy);
328 static dev_info_t *mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr,
329 int lun);
330 static mdi_pathinfo_t *mptsas_find_path_addr(dev_info_t *pdip, uint64_t sasaddr,
331 int lun);
332 static mdi_pathinfo_t *mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy);
333 static dev_info_t *mptsas_find_smp_child(dev_info_t *pdip, char *str_wwn);
335 static int mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy,
336 int *lun);
337 static int mptsas_parse_smp_name(char *name, uint64_t *wwn);
339 static mptsas_target_t *mptsas_phy_to_tgt(mptsas_t *mpt,
340 mptsas_phymask_t phymask, uint8_t phy);
341 static mptsas_target_t *mptsas_wwid_to_ptgt(mptsas_t *mpt,
342 mptsas_phymask_t phymask, uint64_t wwid);
343 static mptsas_smp_t *mptsas_wwid_to_psmp(mptsas_t *mpt,
344 mptsas_phymask_t phymask, uint64_t wwid);
346 static int mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun,
347 uchar_t page, unsigned char *buf, int len, int *rlen, uchar_t evpd);
349 static int mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
350 uint16_t *handle, mptsas_target_t **pptgt);
351 static void mptsas_update_phymask(mptsas_t *mpt);
353 static int mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
354 uint32_t *status, uint8_t cmd);
355 static dev_info_t *mptsas_get_dip_from_dev(dev_t dev,
356 mptsas_phymask_t *phymask);
357 static mptsas_target_t *mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr,
358 mptsas_phymask_t phymask);
359 static int mptsas_flush_led_status(mptsas_t *mpt, mptsas_target_t *ptgt);
363 * Enumeration / DR functions
365 static void mptsas_config_all(dev_info_t *pdip);
366 static int mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
367 dev_info_t **lundip);
368 static int mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
369 dev_info_t **lundip);
371 static int mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt);
372 static int mptsas_offline_target(dev_info_t *pdip, char *name);
374 static int mptsas_config_raid(dev_info_t *pdip, uint16_t target,
375 dev_info_t **dip);
377 static int mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt);
378 static int mptsas_probe_lun(dev_info_t *pdip, int lun,
379 dev_info_t **dip, mptsas_target_t *ptgt);
381 static int mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
382 dev_info_t **dip, mptsas_target_t *ptgt, int lun);
384 static int mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
385 char *guid, dev_info_t **dip, mptsas_target_t *ptgt, int lun);
386 static int mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *sd,
387 char *guid, dev_info_t **dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt,
388 int lun);
390 static void mptsas_offline_missed_luns(dev_info_t *pdip,
391 uint16_t *repluns, int lun_cnt, mptsas_target_t *ptgt);
392 static int mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
393 mdi_pathinfo_t *rpip, uint_t flags);
395 static int mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn,
396 dev_info_t **smp_dip);
397 static int mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
398 uint_t flags);
400 static int mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data,
401 int mode, int *rval);
402 static int mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data,
403 int mode, int *rval);
404 static int mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data,
405 int mode, int *rval);
406 static void mptsas_record_event(void *args);
407 static int mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data,
408 int mode);
410 mptsas_target_t *mptsas_tgt_alloc(refhash_t *, uint16_t, uint64_t,
411 uint32_t, mptsas_phymask_t, uint8_t);
412 static mptsas_smp_t *mptsas_smp_alloc(mptsas_t *, mptsas_smp_t *);
413 static int mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
414 dev_info_t **smp_dip);
417 * Power management functions
419 static int mptsas_get_pci_cap(mptsas_t *mpt);
420 static int mptsas_init_pm(mptsas_t *mpt);
423 * MPT MSI tunable:
425 * By default MSI is enabled on all supported platforms.
427 boolean_t mptsas_enable_msi = B_TRUE;
428 boolean_t mptsas_physical_bind_failed_page_83 = B_FALSE;
431 * Global switch for use of MPI2.5 FAST PATH.
432 * We don't really know what FAST PATH actually does, so if it is suspected
433 * to cause problems it can be turned off by setting this variable to B_FALSE.
435 boolean_t mptsas_use_fastpath = B_TRUE;
437 static int mptsas_register_intrs(mptsas_t *);
438 static void mptsas_unregister_intrs(mptsas_t *);
439 static int mptsas_add_intrs(mptsas_t *, int);
440 static void mptsas_rem_intrs(mptsas_t *);
443 * FMA Prototypes
445 static void mptsas_fm_init(mptsas_t *mpt);
446 static void mptsas_fm_fini(mptsas_t *mpt);
447 static int mptsas_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void *);
449 extern pri_t minclsyspri, maxclsyspri;
452 * This device is created by the SCSI pseudo nexus driver (SCSI vHCI). It is
453 * under this device that the paths to a physical device are created when
454 * MPxIO is used.
456 extern dev_info_t *scsi_vhci_dip;
459 * Tunable timeout value for Inquiry VPD page 0x83
460 * By default the value is 30 seconds.
462 int mptsas_inq83_retry_timeout = 30;
465 * This is used to allocate memory for message frame storage, not for
466 * data I/O DMA. All message frames must be stored in the first 4G of
467 * physical memory.
469 ddi_dma_attr_t mptsas_dma_attrs = {
470 DMA_ATTR_V0, /* attribute layout version */
471 0x0ull, /* address low - should be 0 (longlong) */
472 0xffffffffull, /* address high - 32-bit max range */
473 0x00ffffffull, /* count max - max DMA object size */
474 4, /* allocation alignment requirements */
475 0x78, /* burstsizes - binary encoded values */
476 1, /* minxfer - gran. of DMA engine */
477 0x00ffffffull, /* maxxfer - gran. of DMA engine */
478 0xffffffffull, /* max segment size (DMA boundary) */
479 MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length */
480 512, /* granularity - device transfer size */
481 0 /* flags, set to 0 */
485 * This is used for data I/O DMA memory allocation. (full 64-bit DMA
486 * physical addresses are supported.)
488 ddi_dma_attr_t mptsas_dma_attrs64 = {
489 DMA_ATTR_V0, /* attribute layout version */
490 0x0ull, /* address low - should be 0 (longlong) */
491 0xffffffffffffffffull, /* address high - 64-bit max */
492 0x00ffffffull, /* count max - max DMA object size */
493 4, /* allocation alignment requirements */
494 0x78, /* burstsizes - binary encoded values */
495 1, /* minxfer - gran. of DMA engine */
496 0x00ffffffull, /* maxxfer - gran. of DMA engine */
497 0xffffffffull, /* max segment size (DMA boundary) */
498 MPTSAS_MAX_DMA_SEGS, /* scatter/gather list length */
499 512, /* granularity - device transfer size */
500 0 /* flags, set to 0 */
503 ddi_device_acc_attr_t mptsas_dev_attr = {
504 DDI_DEVICE_ATTR_V1,
505 DDI_STRUCTURE_LE_ACC,
506 DDI_STRICTORDER_ACC,
507 DDI_DEFAULT_ACC
510 static struct cb_ops mptsas_cb_ops = {
511 scsi_hba_open, /* open */
512 scsi_hba_close, /* close */
513 nodev, /* strategy */
514 nodev, /* print */
515 nodev, /* dump */
516 nodev, /* read */
517 nodev, /* write */
518 mptsas_ioctl, /* ioctl */
519 nodev, /* devmap */
520 nodev, /* mmap */
521 nodev, /* segmap */
522 nochpoll, /* chpoll */
523 ddi_prop_op, /* cb_prop_op */
524 NULL, /* streamtab */
525 D_MP, /* cb_flag */
526 CB_REV, /* rev */
527 nodev, /* aread */
528 nodev /* awrite */
531 static struct dev_ops mptsas_ops = {
532 DEVO_REV, /* devo_rev, */
533 0, /* refcnt */
534 ddi_no_info, /* info */
535 nulldev, /* identify */
536 nulldev, /* probe */
537 mptsas_attach, /* attach */
538 mptsas_detach, /* detach */
539 nodev, /* reset */
540 &mptsas_cb_ops, /* driver operations */
541 NULL, /* bus operations */
542 mptsas_power, /* power management */
543 mptsas_quiesce /* quiesce */
547 #define MPTSAS_MOD_STRING "MPTSAS HBA Driver 00.00.00.24"
549 static struct modldrv modldrv = {
550 &mod_driverops, /* Type of module. This one is a driver */
551 MPTSAS_MOD_STRING, /* Name of the module. */
552 &mptsas_ops, /* driver ops */
555 static struct modlinkage modlinkage = {
556 MODREV_1, &modldrv, NULL
558 #define TARGET_PROP "target"
559 #define LUN_PROP "lun"
560 #define LUN64_PROP "lun64"
561 #define SAS_PROP "sas-mpt"
562 #define MDI_GUID "wwn"
563 #define NDI_GUID "guid"
564 #define MPTSAS_DEV_GONE "mptsas_dev_gone"
567 * Local static data
569 #if defined(MPTSAS_DEBUG)
571 * Flags to indicate which debug messages are to be printed and which go to the
572 * debug log ring buffer. Default is to not print anything, and to log
573 * everything except the watchsubr() output which normally happens every second.
575 uint32_t mptsas_debugprt_flags = 0x0;
576 uint32_t mptsas_debuglog_flags = ~(1U << 30);
577 #endif /* defined(MPTSAS_DEBUG) */
578 uint32_t mptsas_debug_resets = 0;
580 static kmutex_t mptsas_global_mutex;
581 static void *mptsas_state; /* soft state ptr */
582 static krwlock_t mptsas_global_rwlock;
584 static kmutex_t mptsas_log_mutex;
585 static char mptsas_log_buf[256];
586 _NOTE(MUTEX_PROTECTS_DATA(mptsas_log_mutex, mptsas_log_buf))
588 static mptsas_t *mptsas_head, *mptsas_tail;
589 static clock_t mptsas_scsi_watchdog_tick;
590 static clock_t mptsas_tick;
591 static timeout_id_t mptsas_reset_watch;
592 static timeout_id_t mptsas_timeout_id;
593 static int mptsas_timeouts_enabled = 0;
596 * Default length for extended auto request sense buffers.
597 * All sense buffers need to be under the same alloc because there
598 * is only one common top 32bits (of 64bits) address register.
599 * Most requests only require 32 bytes, but some request >256.
600 * We use rmalloc()/rmfree() on this additional memory to manage the
601 * "extended" requests.
603 int mptsas_extreq_sense_bufsize = 256*64;
606 * We believe that all software resrictions of having to run with DMA
607 * attributes to limit allocation to the first 4G are removed.
608 * However, this flag remains to enable quick switchback should suspicious
609 * problems emerge.
610 * Note that scsi_alloc_consistent_buf() does still adhere to allocating
611 * 32 bit addressable memory, but we can cope if that is changed now.
613 int mptsas_use_64bit_msgaddr = 1;
616 * SM - HBA statics
618 char *mptsas_driver_rev = MPTSAS_MOD_STRING;
620 #ifdef MPTSAS_DEBUG
621 void debug_enter(char *);
622 #endif
625 * Notes:
626 * - scsi_hba_init(9F) initializes SCSI HBA modules
627 * - must call scsi_hba_fini(9F) if modload() fails
630 _init(void)
632 int status;
633 /* CONSTCOND */
634 ASSERT(NO_COMPETING_THREADS);
636 NDBG0(("_init"));
638 status = ddi_soft_state_init(&mptsas_state, MPTSAS_SIZE,
639 MPTSAS_INITIAL_SOFT_SPACE);
640 if (status != 0) {
641 return (status);
644 if ((status = scsi_hba_init(&modlinkage)) != 0) {
645 ddi_soft_state_fini(&mptsas_state);
646 return (status);
649 mutex_init(&mptsas_global_mutex, NULL, MUTEX_DRIVER, NULL);
650 rw_init(&mptsas_global_rwlock, NULL, RW_DRIVER, NULL);
651 mutex_init(&mptsas_log_mutex, NULL, MUTEX_DRIVER, NULL);
653 if ((status = mod_install(&modlinkage)) != 0) {
654 mutex_destroy(&mptsas_log_mutex);
655 rw_destroy(&mptsas_global_rwlock);
656 mutex_destroy(&mptsas_global_mutex);
657 ddi_soft_state_fini(&mptsas_state);
658 scsi_hba_fini(&modlinkage);
661 return (status);
665 * Notes:
666 * - scsi_hba_fini(9F) uninitializes SCSI HBA modules
669 _fini(void)
671 int status;
672 /* CONSTCOND */
673 ASSERT(NO_COMPETING_THREADS);
675 NDBG0(("_fini"));
677 if ((status = mod_remove(&modlinkage)) == 0) {
678 ddi_soft_state_fini(&mptsas_state);
679 scsi_hba_fini(&modlinkage);
680 mutex_destroy(&mptsas_global_mutex);
681 rw_destroy(&mptsas_global_rwlock);
682 mutex_destroy(&mptsas_log_mutex);
684 return (status);
688 * The loadable-module _info(9E) entry point
691 _info(struct modinfo *modinfop)
693 /* CONSTCOND */
694 ASSERT(NO_COMPETING_THREADS);
695 NDBG0(("mptsas _info"));
697 return (mod_info(&modlinkage, modinfop));
700 static int
701 mptsas_target_eval_devhdl(const void *op, void *arg)
703 uint16_t dh = *(uint16_t *)arg;
704 const mptsas_target_t *tp = op;
706 return ((int)tp->m_devhdl - (int)dh);
709 static int
710 mptsas_target_eval_slot(const void *op, void *arg)
712 mptsas_led_control_t *lcp = arg;
713 const mptsas_target_t *tp = op;
715 if (tp->m_enclosure != lcp->Enclosure)
716 return ((int)tp->m_enclosure - (int)lcp->Enclosure);
718 return ((int)tp->m_slot_num - (int)lcp->Slot);
721 static int
722 mptsas_target_eval_nowwn(const void *op, void *arg)
724 uint8_t phy = *(uint8_t *)arg;
725 const mptsas_target_t *tp = op;
727 if (tp->m_addr.mta_wwn != 0)
728 return (-1);
730 return ((int)tp->m_phynum - (int)phy);
733 static int
734 mptsas_smp_eval_devhdl(const void *op, void *arg)
736 uint16_t dh = *(uint16_t *)arg;
737 const mptsas_smp_t *sp = op;
739 return ((int)sp->m_devhdl - (int)dh);
742 static uint64_t
743 mptsas_target_addr_hash(const void *tp)
745 const mptsas_target_addr_t *tap = tp;
747 return ((tap->mta_wwn & 0xffffffffffffULL) |
748 ((uint64_t)tap->mta_phymask << 48));
751 static int
752 mptsas_target_addr_cmp(const void *a, const void *b)
754 const mptsas_target_addr_t *aap = a;
755 const mptsas_target_addr_t *bap = b;
757 if (aap->mta_wwn < bap->mta_wwn)
758 return (-1);
759 if (aap->mta_wwn > bap->mta_wwn)
760 return (1);
761 return ((int)bap->mta_phymask - (int)aap->mta_phymask);
764 static uint64_t
765 mptsas_tmp_target_hash(const void *tp)
767 return ((uint64_t)(uintptr_t)tp);
770 static int
771 mptsas_tmp_target_cmp(const void *a, const void *b)
773 if (a > b)
774 return (1);
775 if (b < a)
776 return (-1);
778 return (0);
781 static void
782 mptsas_target_free(void *op)
784 kmem_free(op, sizeof (mptsas_target_t));
787 static void
788 mptsas_smp_free(void *op)
790 kmem_free(op, sizeof (mptsas_smp_t));
793 static void
794 mptsas_destroy_hashes(mptsas_t *mpt)
796 mptsas_target_t *tp;
797 mptsas_smp_t *sp;
799 for (tp = refhash_first(mpt->m_targets); tp != NULL;
800 tp = refhash_next(mpt->m_targets, tp)) {
801 refhash_remove(mpt->m_targets, tp);
803 for (sp = refhash_first(mpt->m_smp_targets); sp != NULL;
804 sp = refhash_next(mpt->m_smp_targets, sp)) {
805 refhash_remove(mpt->m_smp_targets, sp);
807 refhash_destroy(mpt->m_tmp_targets);
808 refhash_destroy(mpt->m_targets);
809 refhash_destroy(mpt->m_smp_targets);
810 mpt->m_targets = NULL;
811 mpt->m_smp_targets = NULL;
814 static int
815 mptsas_iport_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
817 dev_info_t *pdip;
818 mptsas_t *mpt;
819 scsi_hba_tran_t *hba_tran;
820 char *iport = NULL;
821 char phymask[MPTSAS_MAX_PHYS];
822 mptsas_phymask_t phy_mask = 0;
823 int dynamic_port = 0;
824 uint32_t page_address;
825 char initiator_wwnstr[MPTSAS_WWN_STRLEN];
826 int rval = DDI_FAILURE;
827 int i = 0;
828 uint8_t numphys = 0;
829 uint8_t phy_id;
830 uint8_t phy_port = 0;
831 uint16_t attached_devhdl = 0;
832 uint32_t dev_info;
833 uint64_t attached_sas_wwn;
834 uint16_t dev_hdl;
835 uint16_t pdev_hdl;
836 uint16_t bay_num, enclosure, io_flags;
837 char attached_wwnstr[MPTSAS_WWN_STRLEN];
839 /* CONSTCOND */
840 ASSERT(NO_COMPETING_THREADS);
842 switch (cmd) {
843 case DDI_ATTACH:
844 break;
846 case DDI_RESUME:
848 * If this a scsi-iport node, nothing to do here.
850 return (DDI_SUCCESS);
852 default:
853 return (DDI_FAILURE);
856 pdip = ddi_get_parent(dip);
858 if ((hba_tran = ndi_flavorv_get(pdip, SCSA_FLAVOR_SCSI_DEVICE)) ==
859 NULL) {
860 cmn_err(CE_WARN, "Failed attach iport because fail to "
861 "get tran vector for the HBA node");
862 return (DDI_FAILURE);
865 mpt = TRAN2MPT(hba_tran);
866 ASSERT(mpt != NULL);
867 if (mpt == NULL)
868 return (DDI_FAILURE);
870 if ((hba_tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) ==
871 NULL) {
872 mptsas_log(mpt, CE_WARN, "Failed attach iport because fail to "
873 "get tran vector for the iport node");
874 return (DDI_FAILURE);
878 * Overwrite parent's tran_hba_private to iport's tran vector
880 hba_tran->tran_hba_private = mpt;
882 ddi_report_dev(dip);
885 * Get SAS address for initiator port according dev_handle
887 iport = ddi_get_name_addr(dip);
888 if (iport && strncmp(iport, "v0", 2) == 0) {
889 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
890 MPTSAS_VIRTUAL_PORT, 1) !=
891 DDI_PROP_SUCCESS) {
892 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
893 MPTSAS_VIRTUAL_PORT);
894 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
895 "prop update failed");
896 return (DDI_FAILURE);
898 return (DDI_SUCCESS);
901 mutex_enter(&mpt->m_mutex);
902 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
903 bzero(phymask, sizeof (phymask));
904 (void) sprintf(phymask,
905 "%x", mpt->m_phy_info[i].phy_mask);
906 if (strcmp(phymask, iport) == 0) {
907 break;
911 if (i == MPTSAS_MAX_PHYS) {
912 mptsas_log(mpt, CE_WARN, "Failed attach port %s because port"
913 "seems not exist", iport);
914 mutex_exit(&mpt->m_mutex);
915 return (DDI_FAILURE);
918 phy_mask = mpt->m_phy_info[i].phy_mask;
920 if (mpt->m_phy_info[i].port_flags & AUTO_PORT_CONFIGURATION)
921 dynamic_port = 1;
922 else
923 dynamic_port = 0;
926 * Update PHY info for smhba
928 if (mptsas_smhba_phy_init(mpt)) {
929 mutex_exit(&mpt->m_mutex);
930 mptsas_log(mpt, CE_WARN, "mptsas phy update "
931 "failed");
932 return (DDI_FAILURE);
935 mutex_exit(&mpt->m_mutex);
937 numphys = 0;
938 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
939 if ((phy_mask >> i) & 0x01) {
940 numphys++;
944 bzero(initiator_wwnstr, sizeof (initiator_wwnstr));
945 (void) sprintf(initiator_wwnstr, "w%016"PRIx64,
946 mpt->un.m_base_wwid);
948 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
949 SCSI_ADDR_PROP_INITIATOR_PORT, initiator_wwnstr) !=
950 DDI_PROP_SUCCESS) {
951 (void) ddi_prop_remove(DDI_DEV_T_NONE,
952 dip, SCSI_ADDR_PROP_INITIATOR_PORT);
953 mptsas_log(mpt, CE_WARN, "mptsas Initiator port "
954 "prop update failed");
955 return (DDI_FAILURE);
957 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
958 MPTSAS_NUM_PHYS, numphys) !=
959 DDI_PROP_SUCCESS) {
960 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, MPTSAS_NUM_PHYS);
961 return (DDI_FAILURE);
964 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
965 "phymask", phy_mask) !=
966 DDI_PROP_SUCCESS) {
967 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "phymask");
968 mptsas_log(mpt, CE_WARN, "mptsas phy mask "
969 "prop update failed");
970 return (DDI_FAILURE);
973 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
974 "dynamic-port", dynamic_port) !=
975 DDI_PROP_SUCCESS) {
976 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "dynamic-port");
977 mptsas_log(mpt, CE_WARN, "mptsas dynamic port "
978 "prop update failed");
979 return (DDI_FAILURE);
981 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip,
982 MPTSAS_VIRTUAL_PORT, 0) !=
983 DDI_PROP_SUCCESS) {
984 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
985 MPTSAS_VIRTUAL_PORT);
986 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
987 "prop update failed");
988 return (DDI_FAILURE);
990 mptsas_smhba_set_all_phy_props(mpt, dip, numphys, phy_mask,
991 &attached_devhdl);
993 mutex_enter(&mpt->m_mutex);
994 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
995 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)attached_devhdl;
996 rval = mptsas_get_sas_device_page0(mpt, page_address, &dev_hdl,
997 &attached_sas_wwn, &dev_info, &phy_port, &phy_id,
998 &pdev_hdl, &bay_num, &enclosure, &io_flags);
999 if (rval != DDI_SUCCESS) {
1000 mptsas_log(mpt, CE_WARN,
1001 "Failed to get device page0 for handle:%d",
1002 attached_devhdl);
1003 mutex_exit(&mpt->m_mutex);
1004 return (DDI_FAILURE);
1007 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1008 bzero(phymask, sizeof (phymask));
1009 (void) sprintf(phymask, "%x", mpt->m_phy_info[i].phy_mask);
1010 if (strcmp(phymask, iport) == 0) {
1011 (void) sprintf(&mpt->m_phy_info[i].smhba_info.path[0],
1012 "%x",
1013 mpt->m_phy_info[i].phy_mask);
1016 mutex_exit(&mpt->m_mutex);
1018 bzero(attached_wwnstr, sizeof (attached_wwnstr));
1019 (void) sprintf(attached_wwnstr, "w%016"PRIx64,
1020 attached_sas_wwn);
1021 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip,
1022 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
1023 DDI_PROP_SUCCESS) {
1024 (void) ddi_prop_remove(DDI_DEV_T_NONE,
1025 dip, SCSI_ADDR_PROP_ATTACHED_PORT);
1026 return (DDI_FAILURE);
1029 /* Create kstats for each phy on this iport */
1031 mptsas_create_phy_stats(mpt, iport, dip);
1034 * register sas hba iport with mdi (MPxIO/vhci)
1036 if (mdi_phci_register(MDI_HCI_CLASS_SCSI,
1037 dip, 0) == MDI_SUCCESS) {
1038 mpt->m_mpxio_enable = TRUE;
1040 return (DDI_SUCCESS);
1044 * Notes:
1045 * Set up all device state and allocate data structures,
1046 * mutexes, condition variables, etc. for device operation.
1047 * Add interrupts needed.
1048 * Return DDI_SUCCESS if device is ready, else return DDI_FAILURE.
1050 static int
1051 mptsas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1053 mptsas_t *mpt = NULL;
1054 int instance, i, j;
1055 int doneq_thread_num;
1056 char intr_added = 0;
1057 char map_setup = 0;
1058 char config_setup = 0;
1059 char hba_attach_setup = 0;
1060 char smp_attach_setup = 0;
1061 char mutex_init_done = 0;
1062 char event_taskq_create = 0;
1063 char dr_taskq_create = 0;
1064 char doneq_thread_create = 0;
1065 char added_watchdog = 0;
1066 scsi_hba_tran_t *hba_tran;
1067 uint_t mem_bar = MEM_SPACE;
1068 int rval = DDI_FAILURE;
1070 /* CONSTCOND */
1071 ASSERT(NO_COMPETING_THREADS);
1073 if (scsi_hba_iport_unit_address(dip)) {
1074 return (mptsas_iport_attach(dip, cmd));
1077 switch (cmd) {
1078 case DDI_ATTACH:
1079 break;
1081 case DDI_RESUME:
1082 if ((hba_tran = ddi_get_driver_private(dip)) == NULL)
1083 return (DDI_FAILURE);
1085 mpt = TRAN2MPT(hba_tran);
1087 if (!mpt) {
1088 return (DDI_FAILURE);
1092 * Reset hardware and softc to "no outstanding commands"
1093 * Note that a check condition can result on first command
1094 * to a target.
1096 mutex_enter(&mpt->m_mutex);
1099 * raise power.
1101 if (mpt->m_options & MPTSAS_OPT_PM) {
1102 mutex_exit(&mpt->m_mutex);
1103 (void) pm_busy_component(dip, 0);
1104 rval = pm_power_has_changed(dip, 0, PM_LEVEL_D0);
1105 if (rval == DDI_SUCCESS) {
1106 mutex_enter(&mpt->m_mutex);
1107 } else {
1109 * The pm_raise_power() call above failed,
1110 * and that can only occur if we were unable
1111 * to reset the hardware. This is probably
1112 * due to unhealty hardware, and because
1113 * important filesystems(such as the root
1114 * filesystem) could be on the attached disks,
1115 * it would not be a good idea to continue,
1116 * as we won't be entirely certain we are
1117 * writing correct data. So we panic() here
1118 * to not only prevent possible data corruption,
1119 * but to give developers or end users a hope
1120 * of identifying and correcting any problems.
1122 fm_panic("mptsas could not reset hardware "
1123 "during resume");
1127 mpt->m_suspended = 0;
1130 * Reinitialize ioc
1132 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1133 if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
1134 mutex_exit(&mpt->m_mutex);
1135 if (mpt->m_options & MPTSAS_OPT_PM) {
1136 (void) pm_idle_component(dip, 0);
1138 fm_panic("mptsas init chip fail during resume");
1141 * mptsas_update_driver_data needs interrupts so enable them
1142 * first.
1144 MPTSAS_ENABLE_INTR(mpt);
1145 mptsas_update_driver_data(mpt);
1147 /* start requests, if possible */
1148 mptsas_restart_hba(mpt);
1150 mutex_exit(&mpt->m_mutex);
1153 * Restart watch thread
1155 mutex_enter(&mptsas_global_mutex);
1156 if (mptsas_timeout_id == 0) {
1157 mptsas_timeout_id = timeout(mptsas_watch, NULL,
1158 mptsas_tick);
1159 mptsas_timeouts_enabled = 1;
1161 mutex_exit(&mptsas_global_mutex);
1163 /* report idle status to pm framework */
1164 if (mpt->m_options & MPTSAS_OPT_PM) {
1165 (void) pm_idle_component(dip, 0);
1168 return (DDI_SUCCESS);
1170 default:
1171 return (DDI_FAILURE);
1175 instance = ddi_get_instance(dip);
1178 * Allocate softc information.
1180 if (ddi_soft_state_zalloc(mptsas_state, instance) != DDI_SUCCESS) {
1181 mptsas_log(NULL, CE_WARN,
1182 "mptsas%d: cannot allocate soft state", instance);
1183 goto fail;
1186 mpt = ddi_get_soft_state(mptsas_state, instance);
1188 if (mpt == NULL) {
1189 mptsas_log(NULL, CE_WARN,
1190 "mptsas%d: cannot get soft state", instance);
1191 goto fail;
1194 /* Indicate that we are 'sizeof (scsi_*(9S))' clean. */
1195 scsi_size_clean(dip);
1197 mpt->m_dip = dip;
1198 mpt->m_instance = instance;
1200 /* Make a per-instance copy of the structures */
1201 mpt->m_io_dma_attr = mptsas_dma_attrs64;
1202 if (mptsas_use_64bit_msgaddr) {
1203 mpt->m_msg_dma_attr = mptsas_dma_attrs64;
1204 } else {
1205 mpt->m_msg_dma_attr = mptsas_dma_attrs;
1207 mpt->m_reg_acc_attr = mptsas_dev_attr;
1208 mpt->m_dev_acc_attr = mptsas_dev_attr;
1211 * Size of individual request sense buffer
1213 mpt->m_req_sense_size = EXTCMDS_STATUS_SIZE;
1216 * Initialize FMA
1218 mpt->m_fm_capabilities = ddi_getprop(DDI_DEV_T_ANY, mpt->m_dip,
1219 DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
1220 DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
1221 DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
1223 mptsas_fm_init(mpt);
1225 if (mptsas_alloc_handshake_msg(mpt,
1226 sizeof (Mpi2SCSITaskManagementRequest_t)) == DDI_FAILURE) {
1227 mptsas_log(mpt, CE_WARN, "cannot initialize handshake msg.");
1228 goto fail;
1232 * Setup configuration space
1234 if (mptsas_config_space_init(mpt) == FALSE) {
1235 mptsas_log(mpt, CE_WARN, "mptsas_config_space_init failed");
1236 goto fail;
1238 config_setup++;
1240 if (ddi_regs_map_setup(dip, mem_bar, (caddr_t *)&mpt->m_reg,
1241 0, 0, &mpt->m_reg_acc_attr, &mpt->m_datap) != DDI_SUCCESS) {
1242 mptsas_log(mpt, CE_WARN, "map setup failed");
1243 goto fail;
1245 map_setup++;
1248 * A taskq is created for dealing with the event handler
1250 if ((mpt->m_event_taskq = ddi_taskq_create(dip, "mptsas_event_taskq",
1251 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1252 mptsas_log(mpt, CE_NOTE, "ddi_taskq_create failed");
1253 goto fail;
1255 event_taskq_create++;
1258 * A taskq is created for dealing with dr events
1260 if ((mpt->m_dr_taskq = ddi_taskq_create(dip,
1261 "mptsas_dr_taskq",
1262 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1263 mptsas_log(mpt, CE_NOTE, "ddi_taskq_create for discovery "
1264 "failed");
1265 goto fail;
1267 dr_taskq_create++;
1269 mpt->m_doneq_thread_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1270 0, "mptsas_doneq_thread_threshold_prop", 10);
1271 mpt->m_doneq_length_threshold = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1272 0, "mptsas_doneq_length_threshold_prop", 8);
1273 mpt->m_doneq_thread_n = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1274 0, "mptsas_doneq_thread_n_prop", 8);
1276 if (mpt->m_doneq_thread_n) {
1277 cv_init(&mpt->m_doneq_thread_cv, NULL, CV_DRIVER, NULL);
1278 mutex_init(&mpt->m_doneq_mutex, NULL, MUTEX_DRIVER, NULL);
1280 mutex_enter(&mpt->m_doneq_mutex);
1281 mpt->m_doneq_thread_id =
1282 kmem_zalloc(sizeof (mptsas_doneq_thread_list_t)
1283 * mpt->m_doneq_thread_n, KM_SLEEP);
1285 for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1286 cv_init(&mpt->m_doneq_thread_id[j].cv, NULL,
1287 CV_DRIVER, NULL);
1288 mutex_init(&mpt->m_doneq_thread_id[j].mutex, NULL,
1289 MUTEX_DRIVER, NULL);
1290 mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1291 mpt->m_doneq_thread_id[j].flag |=
1292 MPTSAS_DONEQ_THREAD_ACTIVE;
1293 mpt->m_doneq_thread_id[j].arg.mpt = mpt;
1294 mpt->m_doneq_thread_id[j].arg.t = j;
1295 mpt->m_doneq_thread_id[j].threadp =
1296 thread_create(NULL, 0, mptsas_doneq_thread,
1297 &mpt->m_doneq_thread_id[j].arg,
1298 0, &p0, TS_RUN, minclsyspri);
1299 mpt->m_doneq_thread_id[j].donetail =
1300 &mpt->m_doneq_thread_id[j].doneq;
1301 mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1303 mutex_exit(&mpt->m_doneq_mutex);
1304 doneq_thread_create++;
1308 * Disable hardware interrupt since we're not ready to
1309 * handle it yet.
1311 MPTSAS_DISABLE_INTR(mpt);
1312 if (mptsas_register_intrs(mpt) == FALSE)
1313 goto fail;
1314 intr_added++;
1316 /* Initialize mutex used in interrupt handler */
1317 mutex_init(&mpt->m_mutex, NULL, MUTEX_DRIVER,
1318 DDI_INTR_PRI(mpt->m_intr_pri));
1319 mutex_init(&mpt->m_passthru_mutex, NULL, MUTEX_DRIVER, NULL);
1320 mutex_init(&mpt->m_tx_waitq_mutex, NULL, MUTEX_DRIVER,
1321 DDI_INTR_PRI(mpt->m_intr_pri));
1322 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1323 mutex_init(&mpt->m_phy_info[i].smhba_info.phy_mutex,
1324 NULL, MUTEX_DRIVER,
1325 DDI_INTR_PRI(mpt->m_intr_pri));
1328 cv_init(&mpt->m_cv, NULL, CV_DRIVER, NULL);
1329 cv_init(&mpt->m_passthru_cv, NULL, CV_DRIVER, NULL);
1330 cv_init(&mpt->m_fw_cv, NULL, CV_DRIVER, NULL);
1331 cv_init(&mpt->m_config_cv, NULL, CV_DRIVER, NULL);
1332 cv_init(&mpt->m_fw_diag_cv, NULL, CV_DRIVER, NULL);
1333 cv_init(&mpt->m_extreq_sense_refcount_cv, NULL, CV_DRIVER, NULL);
1334 mutex_init_done++;
1336 mutex_enter(&mpt->m_mutex);
1338 * Initialize power management component
1340 if (mpt->m_options & MPTSAS_OPT_PM) {
1341 if (mptsas_init_pm(mpt)) {
1342 mutex_exit(&mpt->m_mutex);
1343 mptsas_log(mpt, CE_WARN, "mptsas pm initialization "
1344 "failed");
1345 goto fail;
1350 * Initialize chip using Message Unit Reset, if allowed
1352 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1353 if (mptsas_init_chip(mpt, TRUE) == DDI_FAILURE) {
1354 mutex_exit(&mpt->m_mutex);
1355 mptsas_log(mpt, CE_WARN, "mptsas chip initialization failed");
1356 goto fail;
1359 mpt->m_targets = refhash_create(MPTSAS_TARGET_BUCKET_COUNT,
1360 mptsas_target_addr_hash, mptsas_target_addr_cmp,
1361 mptsas_target_free, sizeof (mptsas_target_t),
1362 offsetof(mptsas_target_t, m_link),
1363 offsetof(mptsas_target_t, m_addr), KM_SLEEP);
1366 * The refhash for temporary targets uses the address of the target
1367 * struct itself as tag, so the tag offset is 0. See the implementation
1368 * of mptsas_tmp_target_hash() and mptsas_tmp_target_cmp().
1370 mpt->m_tmp_targets = refhash_create(MPTSAS_TMP_TARGET_BUCKET_COUNT,
1371 mptsas_tmp_target_hash, mptsas_tmp_target_cmp,
1372 mptsas_target_free, sizeof (mptsas_target_t),
1373 offsetof(mptsas_target_t, m_link), 0, KM_SLEEP);
1376 * Fill in the phy_info structure and get the base WWID
1378 if (mptsas_get_manufacture_page5(mpt) == DDI_FAILURE) {
1379 mptsas_log(mpt, CE_WARN,
1380 "mptsas_get_manufacture_page5 failed!");
1381 goto fail;
1384 if (mptsas_get_sas_io_unit_page_hndshk(mpt)) {
1385 mptsas_log(mpt, CE_WARN,
1386 "mptsas_get_sas_io_unit_page_hndshk failed!");
1387 goto fail;
1390 if (mptsas_get_manufacture_page0(mpt) == DDI_FAILURE) {
1391 mptsas_log(mpt, CE_WARN,
1392 "mptsas_get_manufacture_page0 failed!");
1393 goto fail;
1396 mutex_exit(&mpt->m_mutex);
1399 * Register the iport for multiple port HBA
1401 mptsas_iport_register(mpt);
1404 * initialize SCSI HBA transport structure
1406 if (mptsas_hba_setup(mpt) == FALSE)
1407 goto fail;
1408 hba_attach_setup++;
1410 if (mptsas_smp_setup(mpt) == FALSE)
1411 goto fail;
1412 smp_attach_setup++;
1414 if (mptsas_cache_create(mpt) == FALSE)
1415 goto fail;
1417 mpt->m_scsi_reset_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
1418 dip, 0, "scsi-reset-delay", SCSI_DEFAULT_RESET_DELAY);
1419 if (mpt->m_scsi_reset_delay == 0) {
1420 mptsas_log(mpt, CE_NOTE,
1421 "scsi_reset_delay of 0 is not recommended,"
1422 " resetting to SCSI_DEFAULT_RESET_DELAY\n");
1423 mpt->m_scsi_reset_delay = SCSI_DEFAULT_RESET_DELAY;
1427 * Initialize the wait and done FIFO queue
1429 mpt->m_donetail = &mpt->m_doneq;
1430 mpt->m_waitqtail = &mpt->m_waitq;
1431 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
1432 mpt->m_tx_draining = 0;
1435 * ioc cmd queue initialize
1437 mpt->m_ioc_event_cmdtail = &mpt->m_ioc_event_cmdq;
1438 mpt->m_dev_handle = 0xFFFF;
1440 MPTSAS_ENABLE_INTR(mpt);
1443 * enable event notification
1445 mutex_enter(&mpt->m_mutex);
1446 if (mptsas_ioc_enable_event_notification(mpt)) {
1447 mutex_exit(&mpt->m_mutex);
1448 goto fail;
1450 mutex_exit(&mpt->m_mutex);
1453 * used for mptsas_watch
1455 mptsas_list_add(mpt);
1457 mutex_enter(&mptsas_global_mutex);
1458 if (mptsas_timeouts_enabled == 0) {
1459 mptsas_scsi_watchdog_tick = ddi_prop_get_int(DDI_DEV_T_ANY,
1460 dip, 0, "scsi-watchdog-tick", DEFAULT_WD_TICK);
1462 mptsas_tick = mptsas_scsi_watchdog_tick *
1463 drv_usectohz((clock_t)1000000);
1465 mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
1466 mptsas_timeouts_enabled = 1;
1468 mutex_exit(&mptsas_global_mutex);
1469 added_watchdog++;
1472 * Initialize PHY info for smhba.
1473 * This requires watchdog to be enabled otherwise if interrupts
1474 * don't work the system will hang.
1476 if (mptsas_smhba_setup(mpt)) {
1477 mptsas_log(mpt, CE_WARN, "mptsas phy initialization "
1478 "failed");
1479 goto fail;
1482 /* Check all dma handles allocated in attach */
1483 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl)
1484 != DDI_SUCCESS) ||
1485 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl)
1486 != DDI_SUCCESS) ||
1487 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl)
1488 != DDI_SUCCESS) ||
1489 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl)
1490 != DDI_SUCCESS) ||
1491 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl)
1492 != DDI_SUCCESS) ||
1493 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl)
1494 != DDI_SUCCESS)) {
1495 goto fail;
1498 /* Check all acc handles allocated in attach */
1499 if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
1500 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl)
1501 != DDI_SUCCESS) ||
1502 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl)
1503 != DDI_SUCCESS) ||
1504 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl)
1505 != DDI_SUCCESS) ||
1506 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl)
1507 != DDI_SUCCESS) ||
1508 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl)
1509 != DDI_SUCCESS) ||
1510 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl)
1511 != DDI_SUCCESS) ||
1512 (mptsas_check_acc_handle(mpt->m_config_handle)
1513 != DDI_SUCCESS)) {
1514 goto fail;
1518 * After this point, we are not going to fail the attach.
1521 /* Print message of HBA present */
1522 ddi_report_dev(dip);
1524 /* report idle status to pm framework */
1525 if (mpt->m_options & MPTSAS_OPT_PM) {
1526 (void) pm_idle_component(dip, 0);
1529 return (DDI_SUCCESS);
1531 fail:
1532 mptsas_log(mpt, CE_WARN, "attach failed");
1533 mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
1534 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
1535 if (mpt) {
1536 /* deallocate in reverse order */
1537 if (added_watchdog) {
1538 mptsas_list_del(mpt);
1539 mutex_enter(&mptsas_global_mutex);
1541 if (mptsas_timeout_id && (mptsas_head == NULL)) {
1542 timeout_id_t tid = mptsas_timeout_id;
1543 mptsas_timeouts_enabled = 0;
1544 mptsas_timeout_id = 0;
1545 mutex_exit(&mptsas_global_mutex);
1546 (void) untimeout(tid);
1547 mutex_enter(&mptsas_global_mutex);
1549 mutex_exit(&mptsas_global_mutex);
1552 mptsas_cache_destroy(mpt);
1554 if (smp_attach_setup) {
1555 mptsas_smp_teardown(mpt);
1557 if (hba_attach_setup) {
1558 mptsas_hba_teardown(mpt);
1561 if (mpt->m_tmp_targets)
1562 refhash_destroy(mpt->m_tmp_targets);
1563 if (mpt->m_targets)
1564 refhash_destroy(mpt->m_targets);
1565 if (mpt->m_smp_targets)
1566 refhash_destroy(mpt->m_smp_targets);
1568 if (mpt->m_active) {
1569 mptsas_free_active_slots(mpt);
1571 if (intr_added) {
1572 mptsas_unregister_intrs(mpt);
1575 if (doneq_thread_create) {
1576 mutex_enter(&mpt->m_doneq_mutex);
1577 doneq_thread_num = mpt->m_doneq_thread_n;
1578 for (j = 0; j < mpt->m_doneq_thread_n; j++) {
1579 mutex_enter(&mpt->m_doneq_thread_id[j].mutex);
1580 mpt->m_doneq_thread_id[j].flag &=
1581 (~MPTSAS_DONEQ_THREAD_ACTIVE);
1582 cv_signal(&mpt->m_doneq_thread_id[j].cv);
1583 mutex_exit(&mpt->m_doneq_thread_id[j].mutex);
1585 while (mpt->m_doneq_thread_n) {
1586 cv_wait(&mpt->m_doneq_thread_cv,
1587 &mpt->m_doneq_mutex);
1589 for (j = 0; j < doneq_thread_num; j++) {
1590 cv_destroy(&mpt->m_doneq_thread_id[j].cv);
1591 mutex_destroy(&mpt->m_doneq_thread_id[j].mutex);
1593 kmem_free(mpt->m_doneq_thread_id,
1594 sizeof (mptsas_doneq_thread_list_t)
1595 * doneq_thread_num);
1596 mutex_exit(&mpt->m_doneq_mutex);
1597 cv_destroy(&mpt->m_doneq_thread_cv);
1598 mutex_destroy(&mpt->m_doneq_mutex);
1600 if (event_taskq_create) {
1601 ddi_taskq_destroy(mpt->m_event_taskq);
1603 if (dr_taskq_create) {
1604 ddi_taskq_destroy(mpt->m_dr_taskq);
1606 if (mutex_init_done) {
1607 mutex_destroy(&mpt->m_tx_waitq_mutex);
1608 mutex_destroy(&mpt->m_passthru_mutex);
1609 mutex_destroy(&mpt->m_mutex);
1610 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1611 mutex_destroy(
1612 &mpt->m_phy_info[i].smhba_info.phy_mutex);
1614 cv_destroy(&mpt->m_cv);
1615 cv_destroy(&mpt->m_passthru_cv);
1616 cv_destroy(&mpt->m_fw_cv);
1617 cv_destroy(&mpt->m_config_cv);
1618 cv_destroy(&mpt->m_fw_diag_cv);
1619 cv_destroy(&mpt->m_extreq_sense_refcount_cv);
1622 if (map_setup) {
1623 mptsas_cfg_fini(mpt);
1625 if (config_setup) {
1626 mptsas_config_space_fini(mpt);
1628 mptsas_free_handshake_msg(mpt);
1629 mptsas_hba_fini(mpt);
1631 mptsas_fm_fini(mpt);
1632 ddi_soft_state_free(mptsas_state, instance);
1633 ddi_prop_remove_all(dip);
1635 return (DDI_FAILURE);
1638 static int
1639 mptsas_suspend(dev_info_t *devi)
1641 mptsas_t *mpt, *g;
1642 scsi_hba_tran_t *tran;
1644 if (scsi_hba_iport_unit_address(devi)) {
1645 return (DDI_SUCCESS);
1648 if ((tran = ddi_get_driver_private(devi)) == NULL)
1649 return (DDI_SUCCESS);
1651 mpt = TRAN2MPT(tran);
1652 if (!mpt) {
1653 return (DDI_SUCCESS);
1656 mutex_enter(&mpt->m_mutex);
1658 if (mpt->m_suspended++) {
1659 mutex_exit(&mpt->m_mutex);
1660 return (DDI_SUCCESS);
1664 * Cancel timeout threads for this mpt
1666 if (mpt->m_quiesce_timeid) {
1667 timeout_id_t tid = mpt->m_quiesce_timeid;
1668 mpt->m_quiesce_timeid = 0;
1669 mutex_exit(&mpt->m_mutex);
1670 (void) untimeout(tid);
1671 mutex_enter(&mpt->m_mutex);
1674 if (mpt->m_restart_cmd_timeid) {
1675 timeout_id_t tid = mpt->m_restart_cmd_timeid;
1676 mpt->m_restart_cmd_timeid = 0;
1677 mutex_exit(&mpt->m_mutex);
1678 (void) untimeout(tid);
1679 mutex_enter(&mpt->m_mutex);
1682 mutex_exit(&mpt->m_mutex);
1684 (void) pm_idle_component(mpt->m_dip, 0);
1687 * Cancel watch threads if all mpts suspended
1689 rw_enter(&mptsas_global_rwlock, RW_WRITER);
1690 for (g = mptsas_head; g != NULL; g = g->m_next) {
1691 if (!g->m_suspended)
1692 break;
1694 rw_exit(&mptsas_global_rwlock);
1696 mutex_enter(&mptsas_global_mutex);
1697 if (g == NULL) {
1698 timeout_id_t tid;
1700 mptsas_timeouts_enabled = 0;
1701 if (mptsas_timeout_id) {
1702 tid = mptsas_timeout_id;
1703 mptsas_timeout_id = 0;
1704 mutex_exit(&mptsas_global_mutex);
1705 (void) untimeout(tid);
1706 mutex_enter(&mptsas_global_mutex);
1708 if (mptsas_reset_watch) {
1709 tid = mptsas_reset_watch;
1710 mptsas_reset_watch = 0;
1711 mutex_exit(&mptsas_global_mutex);
1712 (void) untimeout(tid);
1713 mutex_enter(&mptsas_global_mutex);
1716 mutex_exit(&mptsas_global_mutex);
1718 mutex_enter(&mpt->m_mutex);
1721 * If this mpt is not in full power(PM_LEVEL_D0), just return.
1723 if ((mpt->m_options & MPTSAS_OPT_PM) &&
1724 (mpt->m_power_level != PM_LEVEL_D0)) {
1725 mutex_exit(&mpt->m_mutex);
1726 return (DDI_SUCCESS);
1729 /* Disable HBA interrupts in hardware */
1730 MPTSAS_DISABLE_INTR(mpt);
1732 * Send RAID action system shutdown to sync IR
1734 mptsas_raid_action_system_shutdown(mpt);
1736 mutex_exit(&mpt->m_mutex);
1738 /* drain the taskq */
1739 ddi_taskq_wait(mpt->m_event_taskq);
1740 ddi_taskq_wait(mpt->m_dr_taskq);
1742 return (DDI_SUCCESS);
1746 * quiesce(9E) entry point.
1748 * This function is called when the system is single-threaded at high
1749 * PIL with preemption disabled. Therefore, this function must not be
1750 * blocked.
1752 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
1753 * DDI_FAILURE indicates an error condition and should almost never happen.
1755 static int
1756 mptsas_quiesce(dev_info_t *devi)
1758 mptsas_t *mpt;
1759 scsi_hba_tran_t *tran;
1762 * If this call is for iport, just return.
1764 if (scsi_hba_iport_unit_address(devi))
1765 return (DDI_SUCCESS);
1767 if ((tran = ddi_get_driver_private(devi)) == NULL)
1768 return (DDI_SUCCESS);
1770 if ((mpt = TRAN2MPT(tran)) == NULL)
1771 return (DDI_SUCCESS);
1773 /* Disable HBA interrupts in hardware */
1774 MPTSAS_DISABLE_INTR(mpt);
1775 /* Send RAID action system shutdonw to sync IR */
1776 mptsas_raid_action_system_shutdown(mpt);
1778 return (DDI_SUCCESS);
1782 * detach(9E). Remove all device allocations and system resources;
1783 * disable device interrupts.
1784 * Return DDI_SUCCESS if done; DDI_FAILURE if there's a problem.
1786 static int
1787 mptsas_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1789 /* CONSTCOND */
1790 ASSERT(NO_COMPETING_THREADS);
1791 NDBG0(("mptsas_detach: dip=0x%p cmd=0x%p", (void *)devi, (void *)cmd));
1793 switch (cmd) {
1794 case DDI_DETACH:
1795 return (mptsas_do_detach(devi));
1797 case DDI_SUSPEND:
1798 return (mptsas_suspend(devi));
1800 default:
1801 return (DDI_FAILURE);
1803 /* NOTREACHED */
1806 static int
1807 mptsas_do_detach(dev_info_t *dip)
1809 mptsas_t *mpt;
1810 scsi_hba_tran_t *tran;
1811 int circ = 0;
1812 int circ1 = 0;
1813 mdi_pathinfo_t *pip = NULL;
1814 int i;
1815 int doneq_thread_num = 0;
1817 NDBG0(("mptsas_do_detach: dip=0x%p", (void *)dip));
1819 if ((tran = ndi_flavorv_get(dip, SCSA_FLAVOR_SCSI_DEVICE)) == NULL)
1820 return (DDI_FAILURE);
1822 mpt = TRAN2MPT(tran);
1823 if (!mpt) {
1824 return (DDI_FAILURE);
1827 * Still have pathinfo child, should not detach mpt driver
1829 if (scsi_hba_iport_unit_address(dip)) {
1830 if (mpt->m_mpxio_enable) {
1832 * MPxIO enabled for the iport
1834 ndi_devi_enter(scsi_vhci_dip, &circ1);
1835 ndi_devi_enter(dip, &circ);
1836 while (pip = mdi_get_next_client_path(dip, NULL)) {
1837 if (mdi_pi_free(pip, 0) == MDI_SUCCESS) {
1838 continue;
1840 ndi_devi_exit(dip, circ);
1841 ndi_devi_exit(scsi_vhci_dip, circ1);
1842 NDBG12(("detach failed because of "
1843 "outstanding path info"));
1844 return (DDI_FAILURE);
1846 ndi_devi_exit(dip, circ);
1847 ndi_devi_exit(scsi_vhci_dip, circ1);
1848 (void) mdi_phci_unregister(dip, 0);
1851 ddi_prop_remove_all(dip);
1853 return (DDI_SUCCESS);
1856 /* Make sure power level is D0 before accessing registers */
1857 if (mpt->m_options & MPTSAS_OPT_PM) {
1858 (void) pm_busy_component(dip, 0);
1859 if (mpt->m_power_level != PM_LEVEL_D0) {
1860 if (pm_raise_power(dip, 0, PM_LEVEL_D0) !=
1861 DDI_SUCCESS) {
1862 mptsas_log(mpt, CE_WARN,
1863 "mptsas%d: Raise power request failed.",
1864 mpt->m_instance);
1865 (void) pm_idle_component(dip, 0);
1866 return (DDI_FAILURE);
1872 * Send RAID action system shutdown to sync IR. After action, send a
1873 * Message Unit Reset. Since after that DMA resource will be freed,
1874 * set ioc to READY state will avoid HBA initiated DMA operation.
1876 mutex_enter(&mpt->m_mutex);
1877 MPTSAS_DISABLE_INTR(mpt);
1878 mptsas_raid_action_system_shutdown(mpt);
1879 mpt->m_softstate |= MPTSAS_SS_MSG_UNIT_RESET;
1880 (void) mptsas_ioc_reset(mpt, FALSE);
1881 mutex_exit(&mpt->m_mutex);
1882 mptsas_rem_intrs(mpt);
1883 ddi_taskq_destroy(mpt->m_event_taskq);
1884 ddi_taskq_destroy(mpt->m_dr_taskq);
1886 if (mpt->m_doneq_thread_n) {
1887 mutex_enter(&mpt->m_doneq_mutex);
1888 doneq_thread_num = mpt->m_doneq_thread_n;
1889 for (i = 0; i < mpt->m_doneq_thread_n; i++) {
1890 mutex_enter(&mpt->m_doneq_thread_id[i].mutex);
1891 mpt->m_doneq_thread_id[i].flag &=
1892 (~MPTSAS_DONEQ_THREAD_ACTIVE);
1893 cv_signal(&mpt->m_doneq_thread_id[i].cv);
1894 mutex_exit(&mpt->m_doneq_thread_id[i].mutex);
1896 while (mpt->m_doneq_thread_n) {
1897 cv_wait(&mpt->m_doneq_thread_cv,
1898 &mpt->m_doneq_mutex);
1900 for (i = 0; i < doneq_thread_num; i++) {
1901 cv_destroy(&mpt->m_doneq_thread_id[i].cv);
1902 mutex_destroy(&mpt->m_doneq_thread_id[i].mutex);
1904 kmem_free(mpt->m_doneq_thread_id,
1905 sizeof (mptsas_doneq_thread_list_t)
1906 * doneq_thread_num);
1907 mutex_exit(&mpt->m_doneq_mutex);
1908 cv_destroy(&mpt->m_doneq_thread_cv);
1909 mutex_destroy(&mpt->m_doneq_mutex);
1912 scsi_hba_reset_notify_tear_down(mpt->m_reset_notify_listf);
1914 mptsas_list_del(mpt);
1917 * Cancel timeout threads for this mpt
1919 mutex_enter(&mpt->m_mutex);
1920 if (mpt->m_quiesce_timeid) {
1921 timeout_id_t tid = mpt->m_quiesce_timeid;
1922 mpt->m_quiesce_timeid = 0;
1923 mutex_exit(&mpt->m_mutex);
1924 (void) untimeout(tid);
1925 mutex_enter(&mpt->m_mutex);
1928 if (mpt->m_restart_cmd_timeid) {
1929 timeout_id_t tid = mpt->m_restart_cmd_timeid;
1930 mpt->m_restart_cmd_timeid = 0;
1931 mutex_exit(&mpt->m_mutex);
1932 (void) untimeout(tid);
1933 mutex_enter(&mpt->m_mutex);
1936 mutex_exit(&mpt->m_mutex);
1939 * last mpt? ... if active, CANCEL watch threads.
1941 mutex_enter(&mptsas_global_mutex);
1942 if (mptsas_head == NULL) {
1943 timeout_id_t tid;
1945 * Clear mptsas_timeouts_enable so that the watch thread
1946 * gets restarted on DDI_ATTACH
1948 mptsas_timeouts_enabled = 0;
1949 if (mptsas_timeout_id) {
1950 tid = mptsas_timeout_id;
1951 mptsas_timeout_id = 0;
1952 mutex_exit(&mptsas_global_mutex);
1953 (void) untimeout(tid);
1954 mutex_enter(&mptsas_global_mutex);
1956 if (mptsas_reset_watch) {
1957 tid = mptsas_reset_watch;
1958 mptsas_reset_watch = 0;
1959 mutex_exit(&mptsas_global_mutex);
1960 (void) untimeout(tid);
1961 mutex_enter(&mptsas_global_mutex);
1964 mutex_exit(&mptsas_global_mutex);
1967 * Delete Phy stats
1969 mptsas_destroy_phy_stats(mpt);
1971 mptsas_destroy_hashes(mpt);
1974 * Delete nt_active.
1976 mutex_enter(&mpt->m_mutex);
1977 mptsas_free_active_slots(mpt);
1978 mutex_exit(&mpt->m_mutex);
1980 /* deallocate everything that was allocated in mptsas_attach */
1981 mptsas_cache_destroy(mpt);
1983 mptsas_hba_fini(mpt);
1984 mptsas_cfg_fini(mpt);
1986 /* Lower the power informing PM Framework */
1987 if (mpt->m_options & MPTSAS_OPT_PM) {
1988 if (pm_lower_power(dip, 0, PM_LEVEL_D3) != DDI_SUCCESS)
1989 mptsas_log(mpt, CE_WARN,
1990 "!mptsas%d: Lower power request failed "
1991 "during detach, ignoring.",
1992 mpt->m_instance);
1995 mutex_destroy(&mpt->m_tx_waitq_mutex);
1996 mutex_destroy(&mpt->m_passthru_mutex);
1997 mutex_destroy(&mpt->m_mutex);
1998 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
1999 mutex_destroy(&mpt->m_phy_info[i].smhba_info.phy_mutex);
2001 cv_destroy(&mpt->m_cv);
2002 cv_destroy(&mpt->m_passthru_cv);
2003 cv_destroy(&mpt->m_fw_cv);
2004 cv_destroy(&mpt->m_config_cv);
2005 cv_destroy(&mpt->m_fw_diag_cv);
2006 cv_destroy(&mpt->m_extreq_sense_refcount_cv);
2008 mptsas_smp_teardown(mpt);
2009 mptsas_hba_teardown(mpt);
2011 mptsas_config_space_fini(mpt);
2013 mptsas_free_handshake_msg(mpt);
2015 mptsas_fm_fini(mpt);
2016 ddi_soft_state_free(mptsas_state, ddi_get_instance(dip));
2017 ddi_prop_remove_all(dip);
2019 return (DDI_SUCCESS);
2022 static void
2023 mptsas_list_add(mptsas_t *mpt)
2025 rw_enter(&mptsas_global_rwlock, RW_WRITER);
2027 if (mptsas_head == NULL) {
2028 mptsas_head = mpt;
2029 } else {
2030 mptsas_tail->m_next = mpt;
2032 mptsas_tail = mpt;
2033 rw_exit(&mptsas_global_rwlock);
2036 static void
2037 mptsas_list_del(mptsas_t *mpt)
2039 mptsas_t *m;
2041 * Remove device instance from the global linked list
2043 rw_enter(&mptsas_global_rwlock, RW_WRITER);
2044 if (mptsas_head == mpt) {
2045 m = mptsas_head = mpt->m_next;
2046 } else {
2047 for (m = mptsas_head; m != NULL; m = m->m_next) {
2048 if (m->m_next == mpt) {
2049 m->m_next = mpt->m_next;
2050 break;
2053 if (m == NULL) {
2054 mptsas_log(mpt, CE_PANIC, "Not in softc list!");
2058 if (mptsas_tail == mpt) {
2059 mptsas_tail = m;
2061 rw_exit(&mptsas_global_rwlock);
2064 static int
2065 mptsas_alloc_handshake_msg(mptsas_t *mpt, size_t alloc_size)
2067 ddi_dma_attr_t task_dma_attrs;
2069 mpt->m_hshk_dma_size = 0;
2070 task_dma_attrs = mpt->m_msg_dma_attr;
2071 task_dma_attrs.dma_attr_sgllen = 1;
2072 task_dma_attrs.dma_attr_granular = (uint32_t)(alloc_size);
2074 /* allocate Task Management ddi_dma resources */
2075 if (mptsas_dma_addr_create(mpt, task_dma_attrs,
2076 &mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl, &mpt->m_hshk_memp,
2077 alloc_size, NULL) == FALSE) {
2078 return (DDI_FAILURE);
2080 mpt->m_hshk_dma_size = alloc_size;
2082 return (DDI_SUCCESS);
2085 static void
2086 mptsas_free_handshake_msg(mptsas_t *mpt)
2088 if (mpt->m_hshk_dma_size == 0)
2089 return;
2090 mptsas_dma_addr_destroy(&mpt->m_hshk_dma_hdl, &mpt->m_hshk_acc_hdl);
2091 mpt->m_hshk_dma_size = 0;
2094 static int
2095 mptsas_hba_setup(mptsas_t *mpt)
2097 scsi_hba_tran_t *hba_tran;
2098 int tran_flags;
2100 /* Allocate a transport structure */
2101 hba_tran = mpt->m_tran = scsi_hba_tran_alloc(mpt->m_dip,
2102 SCSI_HBA_CANSLEEP);
2103 ASSERT(mpt->m_tran != NULL);
2105 hba_tran->tran_hba_private = mpt;
2106 hba_tran->tran_tgt_private = NULL;
2108 hba_tran->tran_tgt_init = mptsas_scsi_tgt_init;
2109 hba_tran->tran_tgt_free = mptsas_scsi_tgt_free;
2111 hba_tran->tran_start = mptsas_scsi_start;
2112 hba_tran->tran_reset = mptsas_scsi_reset;
2113 hba_tran->tran_abort = mptsas_scsi_abort;
2114 hba_tran->tran_getcap = mptsas_scsi_getcap;
2115 hba_tran->tran_setcap = mptsas_scsi_setcap;
2116 hba_tran->tran_init_pkt = mptsas_scsi_init_pkt;
2117 hba_tran->tran_destroy_pkt = mptsas_scsi_destroy_pkt;
2119 hba_tran->tran_dmafree = mptsas_scsi_dmafree;
2120 hba_tran->tran_sync_pkt = mptsas_scsi_sync_pkt;
2121 hba_tran->tran_reset_notify = mptsas_scsi_reset_notify;
2123 hba_tran->tran_get_bus_addr = mptsas_get_bus_addr;
2124 hba_tran->tran_get_name = mptsas_get_name;
2126 hba_tran->tran_quiesce = mptsas_scsi_quiesce;
2127 hba_tran->tran_unquiesce = mptsas_scsi_unquiesce;
2128 hba_tran->tran_bus_reset = NULL;
2130 hba_tran->tran_add_eventcall = NULL;
2131 hba_tran->tran_get_eventcookie = NULL;
2132 hba_tran->tran_post_event = NULL;
2133 hba_tran->tran_remove_eventcall = NULL;
2135 hba_tran->tran_bus_config = mptsas_bus_config;
2137 hba_tran->tran_interconnect_type = INTERCONNECT_SAS;
2140 * All children of the HBA are iports. We need tran was cloned.
2141 * So we pass the flags to SCSA. SCSI_HBA_TRAN_CLONE will be
2142 * inherited to iport's tran vector.
2144 tran_flags = (SCSI_HBA_HBA | SCSI_HBA_TRAN_CLONE);
2146 if (scsi_hba_attach_setup(mpt->m_dip, &mpt->m_msg_dma_attr,
2147 hba_tran, tran_flags) != DDI_SUCCESS) {
2148 mptsas_log(mpt, CE_WARN, "hba attach setup failed");
2149 scsi_hba_tran_free(hba_tran);
2150 mpt->m_tran = NULL;
2151 return (FALSE);
2153 return (TRUE);
2156 static void
2157 mptsas_hba_teardown(mptsas_t *mpt)
2159 (void) scsi_hba_detach(mpt->m_dip);
2160 if (mpt->m_tran != NULL) {
2161 scsi_hba_tran_free(mpt->m_tran);
2162 mpt->m_tran = NULL;
2166 static void
2167 mptsas_iport_register(mptsas_t *mpt)
2169 int i, j;
2170 mptsas_phymask_t mask = 0x0;
2172 * initial value of mask is 0
2174 mutex_enter(&mpt->m_mutex);
2175 for (i = 0; i < mpt->m_num_phys; i++) {
2176 mptsas_phymask_t phy_mask = 0x0;
2177 char phy_mask_name[MPTSAS_MAX_PHYS];
2178 uint8_t current_port;
2180 if (mpt->m_phy_info[i].attached_devhdl == 0)
2181 continue;
2183 bzero(phy_mask_name, sizeof (phy_mask_name));
2185 current_port = mpt->m_phy_info[i].port_num;
2187 if ((mask & (1 << i)) != 0)
2188 continue;
2190 for (j = 0; j < mpt->m_num_phys; j++) {
2191 if (mpt->m_phy_info[j].attached_devhdl &&
2192 (mpt->m_phy_info[j].port_num == current_port)) {
2193 phy_mask |= (1 << j);
2196 mask = mask | phy_mask;
2198 for (j = 0; j < mpt->m_num_phys; j++) {
2199 if ((phy_mask >> j) & 0x01) {
2200 mpt->m_phy_info[j].phy_mask = phy_mask;
2204 (void) sprintf(phy_mask_name, "%x", phy_mask);
2206 mutex_exit(&mpt->m_mutex);
2208 * register a iport
2210 (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
2211 mutex_enter(&mpt->m_mutex);
2213 mutex_exit(&mpt->m_mutex);
2215 * register a virtual port for RAID volume always
2217 (void) scsi_hba_iport_register(mpt->m_dip, "v0");
2221 static int
2222 mptsas_smp_setup(mptsas_t *mpt)
2224 mpt->m_smptran = smp_hba_tran_alloc(mpt->m_dip);
2225 ASSERT(mpt->m_smptran != NULL);
2226 mpt->m_smptran->smp_tran_hba_private = mpt;
2227 mpt->m_smptran->smp_tran_start = mptsas_smp_start;
2228 if (smp_hba_attach_setup(mpt->m_dip, mpt->m_smptran) != DDI_SUCCESS) {
2229 mptsas_log(mpt, CE_WARN, "smp attach setup failed");
2230 smp_hba_tran_free(mpt->m_smptran);
2231 mpt->m_smptran = NULL;
2232 return (FALSE);
2235 * Initialize smp hash table
2237 mpt->m_smp_targets = refhash_create(MPTSAS_SMP_BUCKET_COUNT,
2238 mptsas_target_addr_hash, mptsas_target_addr_cmp,
2239 mptsas_smp_free, sizeof (mptsas_smp_t),
2240 offsetof(mptsas_smp_t, m_link), offsetof(mptsas_smp_t, m_addr),
2241 KM_SLEEP);
2242 mpt->m_smp_devhdl = 0xFFFF;
2244 return (TRUE);
2247 static void
2248 mptsas_smp_teardown(mptsas_t *mpt)
2250 (void) smp_hba_detach(mpt->m_dip);
2251 if (mpt->m_smptran != NULL) {
2252 smp_hba_tran_free(mpt->m_smptran);
2253 mpt->m_smptran = NULL;
2255 mpt->m_smp_devhdl = 0;
2258 static int
2259 mptsas_cache_create(mptsas_t *mpt)
2261 int instance = mpt->m_instance;
2262 char buf[64];
2265 * create kmem cache for packets
2267 (void) sprintf(buf, "mptsas%d_cache", instance);
2268 mpt->m_kmem_cache = kmem_cache_create(buf,
2269 sizeof (struct mptsas_cmd) + scsi_pkt_size(), 8,
2270 mptsas_kmem_cache_constructor, mptsas_kmem_cache_destructor,
2271 NULL, (void *)mpt, NULL, 0);
2273 if (mpt->m_kmem_cache == NULL) {
2274 mptsas_log(mpt, CE_WARN, "creating kmem cache failed");
2275 return (FALSE);
2279 * create kmem cache for extra SGL frames if SGL cannot
2280 * be accomodated into main request frame.
2282 (void) sprintf(buf, "mptsas%d_cache_frames", instance);
2283 mpt->m_cache_frames = kmem_cache_create(buf,
2284 sizeof (mptsas_cache_frames_t), 8,
2285 mptsas_cache_frames_constructor, mptsas_cache_frames_destructor,
2286 NULL, (void *)mpt, NULL, 0);
2288 if (mpt->m_cache_frames == NULL) {
2289 mptsas_log(mpt, CE_WARN, "creating cache for frames failed");
2290 return (FALSE);
2293 return (TRUE);
2296 static void
2297 mptsas_cache_destroy(mptsas_t *mpt)
2299 /* deallocate in reverse order */
2300 if (mpt->m_cache_frames) {
2301 kmem_cache_destroy(mpt->m_cache_frames);
2302 mpt->m_cache_frames = NULL;
2304 if (mpt->m_kmem_cache) {
2305 kmem_cache_destroy(mpt->m_kmem_cache);
2306 mpt->m_kmem_cache = NULL;
2310 static int
2311 mptsas_power(dev_info_t *dip, int component, int level)
2313 _NOTE(ARGUNUSED(component))
2315 mptsas_t *mpt;
2316 int rval = DDI_SUCCESS;
2317 int polls = 0;
2318 uint32_t ioc_status;
2320 if (scsi_hba_iport_unit_address(dip) != 0)
2321 return (DDI_SUCCESS);
2323 mpt = ddi_get_soft_state(mptsas_state, ddi_get_instance(dip));
2324 if (mpt == NULL) {
2325 return (DDI_FAILURE);
2328 mutex_enter(&mpt->m_mutex);
2331 * If the device is busy, don't lower its power level
2333 if (mpt->m_busy && (mpt->m_power_level > level)) {
2334 mutex_exit(&mpt->m_mutex);
2335 return (DDI_FAILURE);
2337 switch (level) {
2338 case PM_LEVEL_D0:
2339 NDBG11(("mptsas%d: turning power ON.", mpt->m_instance));
2340 MPTSAS_POWER_ON(mpt);
2342 * Wait up to 30 seconds for IOC to come out of reset.
2344 while (((ioc_status = ddi_get32(mpt->m_datap,
2345 &mpt->m_reg->Doorbell)) &
2346 MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
2347 if (polls++ > 3000) {
2348 break;
2350 delay(drv_usectohz(10000));
2353 * If IOC is not in operational state, try to hard reset it.
2355 if ((ioc_status & MPI2_IOC_STATE_MASK) !=
2356 MPI2_IOC_STATE_OPERATIONAL) {
2357 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
2358 if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
2359 mptsas_log(mpt, CE_WARN,
2360 "mptsas_power: hard reset failed");
2361 mutex_exit(&mpt->m_mutex);
2362 return (DDI_FAILURE);
2365 mpt->m_power_level = PM_LEVEL_D0;
2366 break;
2367 case PM_LEVEL_D3:
2368 NDBG11(("mptsas%d: turning power OFF.", mpt->m_instance));
2369 MPTSAS_POWER_OFF(mpt);
2370 break;
2371 default:
2372 mptsas_log(mpt, CE_WARN, "mptsas%d: unknown power level <%x>.",
2373 mpt->m_instance, level);
2374 rval = DDI_FAILURE;
2375 break;
2377 mutex_exit(&mpt->m_mutex);
2378 return (rval);
2382 * Initialize configuration space and figure out which
2383 * chip and revison of the chip the mpt driver is using.
2385 static int
2386 mptsas_config_space_init(mptsas_t *mpt)
2388 NDBG0(("mptsas_config_space_init"));
2390 if (mpt->m_config_handle != NULL)
2391 return (TRUE);
2393 if (pci_config_setup(mpt->m_dip,
2394 &mpt->m_config_handle) != DDI_SUCCESS) {
2395 mptsas_log(mpt, CE_WARN, "cannot map configuration space.");
2396 return (FALSE);
2400 * This is a workaround for a XMITS ASIC bug which does not
2401 * drive the CBE upper bits.
2403 if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT) &
2404 PCI_STAT_PERROR) {
2405 pci_config_put16(mpt->m_config_handle, PCI_CONF_STAT,
2406 PCI_STAT_PERROR);
2409 mptsas_setup_cmd_reg(mpt);
2412 * Get the chip device id:
2414 mpt->m_devid = pci_config_get16(mpt->m_config_handle, PCI_CONF_DEVID);
2417 * Save the revision.
2419 mpt->m_revid = pci_config_get8(mpt->m_config_handle, PCI_CONF_REVID);
2422 * Save the SubSystem Vendor and Device IDs
2424 mpt->m_svid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBVENID);
2425 mpt->m_ssid = pci_config_get16(mpt->m_config_handle, PCI_CONF_SUBSYSID);
2428 * Set the latency timer to 0x40 as specified by the upa -> pci
2429 * bridge chip design team. This may be done by the sparc pci
2430 * bus nexus driver, but the driver should make sure the latency
2431 * timer is correct for performance reasons.
2433 pci_config_put8(mpt->m_config_handle, PCI_CONF_LATENCY_TIMER,
2434 MPTSAS_LATENCY_TIMER);
2436 (void) mptsas_get_pci_cap(mpt);
2437 return (TRUE);
2440 static void
2441 mptsas_config_space_fini(mptsas_t *mpt)
2443 if (mpt->m_config_handle != NULL) {
2444 mptsas_disable_bus_master(mpt);
2445 pci_config_teardown(&mpt->m_config_handle);
2446 mpt->m_config_handle = NULL;
2450 static void
2451 mptsas_setup_cmd_reg(mptsas_t *mpt)
2453 ushort_t cmdreg;
2456 * Set the command register to the needed values.
2458 cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2459 cmdreg |= (PCI_COMM_ME | PCI_COMM_SERR_ENABLE |
2460 PCI_COMM_PARITY_DETECT | PCI_COMM_MAE);
2461 cmdreg &= ~PCI_COMM_IO;
2462 pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2465 static void
2466 mptsas_disable_bus_master(mptsas_t *mpt)
2468 ushort_t cmdreg;
2471 * Clear the master enable bit in the PCI command register.
2472 * This prevents any bus mastering activity like DMA.
2474 cmdreg = pci_config_get16(mpt->m_config_handle, PCI_CONF_COMM);
2475 cmdreg &= ~PCI_COMM_ME;
2476 pci_config_put16(mpt->m_config_handle, PCI_CONF_COMM, cmdreg);
2480 mptsas_dma_alloc(mptsas_t *mpt, mptsas_dma_alloc_state_t *dma_statep)
2482 ddi_dma_attr_t attrs;
2484 attrs = mpt->m_io_dma_attr;
2485 attrs.dma_attr_sgllen = 1;
2487 ASSERT(dma_statep != NULL);
2489 if (mptsas_dma_addr_create(mpt, attrs, &dma_statep->handle,
2490 &dma_statep->accessp, &dma_statep->memp, dma_statep->size,
2491 &dma_statep->cookie) == FALSE) {
2492 return (DDI_FAILURE);
2495 return (DDI_SUCCESS);
2498 void
2499 mptsas_dma_free(mptsas_dma_alloc_state_t *dma_statep)
2501 ASSERT(dma_statep != NULL);
2502 mptsas_dma_addr_destroy(&dma_statep->handle, &dma_statep->accessp);
2503 dma_statep->size = 0;
2507 mptsas_do_dma(mptsas_t *mpt, uint32_t size, int var, int (*callback)())
2509 ddi_dma_attr_t attrs;
2510 ddi_dma_handle_t dma_handle;
2511 caddr_t memp;
2512 ddi_acc_handle_t accessp;
2513 int rval;
2515 ASSERT(mutex_owned(&mpt->m_mutex));
2517 attrs = mpt->m_msg_dma_attr;
2518 attrs.dma_attr_sgllen = 1;
2519 attrs.dma_attr_granular = size;
2521 if (mptsas_dma_addr_create(mpt, attrs, &dma_handle,
2522 &accessp, &memp, size, NULL) == FALSE) {
2523 return (DDI_FAILURE);
2526 rval = (*callback) (mpt, memp, var, accessp);
2528 if ((mptsas_check_dma_handle(dma_handle) != DDI_SUCCESS) ||
2529 (mptsas_check_acc_handle(accessp) != DDI_SUCCESS)) {
2530 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
2531 rval = DDI_FAILURE;
2534 mptsas_dma_addr_destroy(&dma_handle, &accessp);
2535 return (rval);
2539 static int
2540 mptsas_alloc_request_frames(mptsas_t *mpt)
2542 ddi_dma_attr_t frame_dma_attrs;
2543 caddr_t memp;
2544 ddi_dma_cookie_t cookie;
2545 size_t mem_size;
2548 * re-alloc when it has already alloced
2550 if (mpt->m_dma_req_frame_hdl)
2551 mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2552 &mpt->m_acc_req_frame_hdl);
2555 * The size of the request frame pool is:
2556 * Number of Request Frames * Request Frame Size
2558 mem_size = mpt->m_max_requests * mpt->m_req_frame_size;
2561 * set the DMA attributes. System Request Message Frames must be
2562 * aligned on a 16-byte boundry.
2564 frame_dma_attrs = mpt->m_msg_dma_attr;
2565 frame_dma_attrs.dma_attr_align = 16;
2566 frame_dma_attrs.dma_attr_sgllen = 1;
2569 * allocate the request frame pool.
2571 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2572 &mpt->m_dma_req_frame_hdl, &mpt->m_acc_req_frame_hdl, &memp,
2573 mem_size, &cookie) == FALSE) {
2574 return (DDI_FAILURE);
2578 * Store the request frame memory address. This chip uses this
2579 * address to dma to and from the driver's frame. The second
2580 * address is the address mpt uses to fill in the frame.
2582 mpt->m_req_frame_dma_addr = cookie.dmac_laddress;
2583 mpt->m_req_frame = memp;
2586 * Clear the request frame pool.
2588 bzero(mpt->m_req_frame, mem_size);
2590 return (DDI_SUCCESS);
2593 static int
2594 mptsas_alloc_sense_bufs(mptsas_t *mpt)
2596 ddi_dma_attr_t sense_dma_attrs;
2597 caddr_t memp;
2598 ddi_dma_cookie_t cookie;
2599 size_t mem_size;
2600 int num_extrqsense_bufs;
2602 ASSERT(mpt->m_extreq_sense_refcount == 0);
2605 * re-alloc when it has already alloced
2607 if (mpt->m_dma_req_sense_hdl) {
2608 rmfreemap(mpt->m_erqsense_map);
2609 mptsas_dma_addr_destroy(&mpt->m_dma_req_sense_hdl,
2610 &mpt->m_acc_req_sense_hdl);
2614 * The size of the request sense pool is:
2615 * (Number of Request Frames - 2 ) * Request Sense Size +
2616 * extra memory for extended sense requests.
2618 mem_size = ((mpt->m_max_requests - 2) * mpt->m_req_sense_size) +
2619 mptsas_extreq_sense_bufsize;
2622 * set the DMA attributes. ARQ buffers
2623 * aligned on a 16-byte boundry.
2625 sense_dma_attrs = mpt->m_msg_dma_attr;
2626 sense_dma_attrs.dma_attr_align = 16;
2627 sense_dma_attrs.dma_attr_sgllen = 1;
2630 * allocate the request sense buffer pool.
2632 if (mptsas_dma_addr_create(mpt, sense_dma_attrs,
2633 &mpt->m_dma_req_sense_hdl, &mpt->m_acc_req_sense_hdl, &memp,
2634 mem_size, &cookie) == FALSE) {
2635 return (DDI_FAILURE);
2639 * Store the request sense base memory address. This chip uses this
2640 * address to dma the request sense data. The second
2641 * address is the address mpt uses to access the data.
2642 * The third is the base for the extended rqsense buffers.
2644 mpt->m_req_sense_dma_addr = cookie.dmac_laddress;
2645 mpt->m_req_sense = memp;
2646 memp += (mpt->m_max_requests - 2) * mpt->m_req_sense_size;
2647 mpt->m_extreq_sense = memp;
2650 * The extra memory is divided up into multiples of the base
2651 * buffer size in order to allocate via rmalloc().
2652 * Note that the rmallocmap cannot start at zero!
2654 num_extrqsense_bufs = mptsas_extreq_sense_bufsize /
2655 mpt->m_req_sense_size;
2656 mpt->m_erqsense_map = rmallocmap_wait(num_extrqsense_bufs);
2657 rmfree(mpt->m_erqsense_map, num_extrqsense_bufs, 1);
2660 * Clear the pool.
2662 bzero(mpt->m_req_sense, mem_size);
2664 return (DDI_SUCCESS);
2667 static int
2668 mptsas_alloc_reply_frames(mptsas_t *mpt)
2670 ddi_dma_attr_t frame_dma_attrs;
2671 caddr_t memp;
2672 ddi_dma_cookie_t cookie;
2673 size_t mem_size;
2676 * re-alloc when it has already alloced
2678 if (mpt->m_dma_reply_frame_hdl) {
2679 mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2680 &mpt->m_acc_reply_frame_hdl);
2684 * The size of the reply frame pool is:
2685 * Number of Reply Frames * Reply Frame Size
2687 mem_size = mpt->m_max_replies * mpt->m_reply_frame_size;
2690 * set the DMA attributes. System Reply Message Frames must be
2691 * aligned on a 4-byte boundry. This is the default.
2693 frame_dma_attrs = mpt->m_msg_dma_attr;
2694 frame_dma_attrs.dma_attr_sgllen = 1;
2697 * allocate the reply frame pool
2699 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2700 &mpt->m_dma_reply_frame_hdl, &mpt->m_acc_reply_frame_hdl, &memp,
2701 mem_size, &cookie) == FALSE) {
2702 return (DDI_FAILURE);
2706 * Store the reply frame memory address. This chip uses this
2707 * address to dma to and from the driver's frame. The second
2708 * address is the address mpt uses to process the frame.
2710 mpt->m_reply_frame_dma_addr = cookie.dmac_laddress;
2711 mpt->m_reply_frame = memp;
2714 * Clear the reply frame pool.
2716 bzero(mpt->m_reply_frame, mem_size);
2718 return (DDI_SUCCESS);
2721 static int
2722 mptsas_alloc_free_queue(mptsas_t *mpt)
2724 ddi_dma_attr_t frame_dma_attrs;
2725 caddr_t memp;
2726 ddi_dma_cookie_t cookie;
2727 size_t mem_size;
2730 * re-alloc when it has already alloced
2732 if (mpt->m_dma_free_queue_hdl) {
2733 mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2734 &mpt->m_acc_free_queue_hdl);
2738 * The reply free queue size is:
2739 * Reply Free Queue Depth * 4
2740 * The "4" is the size of one 32 bit address (low part of 64-bit
2741 * address)
2743 mem_size = mpt->m_free_queue_depth * 4;
2746 * set the DMA attributes The Reply Free Queue must be aligned on a
2747 * 16-byte boundry.
2749 frame_dma_attrs = mpt->m_msg_dma_attr;
2750 frame_dma_attrs.dma_attr_align = 16;
2751 frame_dma_attrs.dma_attr_sgllen = 1;
2754 * allocate the reply free queue
2756 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2757 &mpt->m_dma_free_queue_hdl, &mpt->m_acc_free_queue_hdl, &memp,
2758 mem_size, &cookie) == FALSE) {
2759 return (DDI_FAILURE);
2763 * Store the reply free queue memory address. This chip uses this
2764 * address to read from the reply free queue. The second address
2765 * is the address mpt uses to manage the queue.
2767 mpt->m_free_queue_dma_addr = cookie.dmac_laddress;
2768 mpt->m_free_queue = memp;
2771 * Clear the reply free queue memory.
2773 bzero(mpt->m_free_queue, mem_size);
2775 return (DDI_SUCCESS);
2778 static int
2779 mptsas_alloc_post_queue(mptsas_t *mpt)
2781 ddi_dma_attr_t frame_dma_attrs;
2782 caddr_t memp;
2783 ddi_dma_cookie_t cookie;
2784 size_t mem_size;
2787 * re-alloc when it has already alloced
2789 if (mpt->m_dma_post_queue_hdl) {
2790 mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2791 &mpt->m_acc_post_queue_hdl);
2795 * The reply descriptor post queue size is:
2796 * Reply Descriptor Post Queue Depth * 8
2797 * The "8" is the size of each descriptor (8 bytes or 64 bits).
2799 mem_size = mpt->m_post_queue_depth * 8;
2802 * set the DMA attributes. The Reply Descriptor Post Queue must be
2803 * aligned on a 16-byte boundry.
2805 frame_dma_attrs = mpt->m_msg_dma_attr;
2806 frame_dma_attrs.dma_attr_align = 16;
2807 frame_dma_attrs.dma_attr_sgllen = 1;
2810 * allocate the reply post queue
2812 if (mptsas_dma_addr_create(mpt, frame_dma_attrs,
2813 &mpt->m_dma_post_queue_hdl, &mpt->m_acc_post_queue_hdl, &memp,
2814 mem_size, &cookie) == FALSE) {
2815 return (DDI_FAILURE);
2819 * Store the reply descriptor post queue memory address. This chip
2820 * uses this address to write to the reply descriptor post queue. The
2821 * second address is the address mpt uses to manage the queue.
2823 mpt->m_post_queue_dma_addr = cookie.dmac_laddress;
2824 mpt->m_post_queue = memp;
2827 * Clear the reply post queue memory.
2829 bzero(mpt->m_post_queue, mem_size);
2831 return (DDI_SUCCESS);
2834 static void
2835 mptsas_alloc_reply_args(mptsas_t *mpt)
2837 if (mpt->m_replyh_args == NULL) {
2838 mpt->m_replyh_args = kmem_zalloc(sizeof (m_replyh_arg_t) *
2839 mpt->m_max_replies, KM_SLEEP);
2843 static int
2844 mptsas_alloc_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2846 mptsas_cache_frames_t *frames = NULL;
2847 if (cmd->cmd_extra_frames == NULL) {
2848 frames = kmem_cache_alloc(mpt->m_cache_frames, KM_NOSLEEP);
2849 if (frames == NULL) {
2850 return (DDI_FAILURE);
2852 cmd->cmd_extra_frames = frames;
2854 return (DDI_SUCCESS);
2857 static void
2858 mptsas_free_extra_sgl_frame(mptsas_t *mpt, mptsas_cmd_t *cmd)
2860 if (cmd->cmd_extra_frames) {
2861 kmem_cache_free(mpt->m_cache_frames,
2862 (void *)cmd->cmd_extra_frames);
2863 cmd->cmd_extra_frames = NULL;
2867 static void
2868 mptsas_cfg_fini(mptsas_t *mpt)
2870 NDBG0(("mptsas_cfg_fini"));
2871 ddi_regs_map_free(&mpt->m_datap);
2874 static void
2875 mptsas_hba_fini(mptsas_t *mpt)
2877 NDBG0(("mptsas_hba_fini"));
2880 * Free up any allocated memory
2882 if (mpt->m_dma_req_frame_hdl) {
2883 mptsas_dma_addr_destroy(&mpt->m_dma_req_frame_hdl,
2884 &mpt->m_acc_req_frame_hdl);
2887 if (mpt->m_dma_req_sense_hdl) {
2888 rmfreemap(mpt->m_erqsense_map);
2889 mptsas_dma_addr_destroy(&mpt->m_dma_req_sense_hdl,
2890 &mpt->m_acc_req_sense_hdl);
2893 if (mpt->m_dma_reply_frame_hdl) {
2894 mptsas_dma_addr_destroy(&mpt->m_dma_reply_frame_hdl,
2895 &mpt->m_acc_reply_frame_hdl);
2898 if (mpt->m_dma_free_queue_hdl) {
2899 mptsas_dma_addr_destroy(&mpt->m_dma_free_queue_hdl,
2900 &mpt->m_acc_free_queue_hdl);
2903 if (mpt->m_dma_post_queue_hdl) {
2904 mptsas_dma_addr_destroy(&mpt->m_dma_post_queue_hdl,
2905 &mpt->m_acc_post_queue_hdl);
2908 if (mpt->m_replyh_args != NULL) {
2909 kmem_free(mpt->m_replyh_args, sizeof (m_replyh_arg_t)
2910 * mpt->m_max_replies);
2914 static int
2915 mptsas_name_child(dev_info_t *lun_dip, char *name, int len)
2917 int lun = 0;
2918 char *sas_wwn = NULL;
2919 int phynum = -1;
2920 int reallen = 0;
2922 /* Get the target num */
2923 lun = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip, DDI_PROP_DONTPASS,
2924 LUN_PROP, 0);
2926 if ((phynum = ddi_prop_get_int(DDI_DEV_T_ANY, lun_dip,
2927 DDI_PROP_DONTPASS, "sata-phy", -1)) != -1) {
2929 * Stick in the address of form "pPHY,LUN"
2931 reallen = snprintf(name, len, "p%x,%x", phynum, lun);
2932 } else if (ddi_prop_lookup_string(DDI_DEV_T_ANY, lun_dip,
2933 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &sas_wwn)
2934 == DDI_PROP_SUCCESS) {
2936 * Stick in the address of the form "wWWN,LUN"
2938 reallen = snprintf(name, len, "%s,%x", sas_wwn, lun);
2939 ddi_prop_free(sas_wwn);
2940 } else {
2941 return (DDI_FAILURE);
2944 ASSERT(reallen < len);
2945 if (reallen >= len) {
2946 mptsas_log(0, CE_WARN, "!mptsas_get_name: name parameter "
2947 "length too small, it needs to be %d bytes", reallen + 1);
2949 return (DDI_SUCCESS);
2953 * tran_tgt_init(9E) - target device instance initialization
2955 static int
2956 mptsas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
2957 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
2959 _NOTE(ARGUNUSED(hba_tran))
2962 * At this point, the scsi_device structure already exists
2963 * and has been initialized.
2965 * Use this function to allocate target-private data structures,
2966 * if needed by this HBA. Add revised flow-control and queue
2967 * properties for child here, if desired and if you can tell they
2968 * support tagged queueing by now.
2970 mptsas_t *mpt;
2971 int lun = sd->sd_address.a_lun;
2972 mdi_pathinfo_t *pip = NULL;
2973 mptsas_tgt_private_t *tgt_private = NULL;
2974 mptsas_target_t *ptgt = NULL;
2975 char *psas_wwn = NULL;
2976 mptsas_phymask_t phymask = 0;
2977 uint64_t sas_wwn = 0;
2978 mptsas_target_addr_t addr;
2979 mpt = SDEV2MPT(sd);
2981 ASSERT(scsi_hba_iport_unit_address(hba_dip) != 0);
2983 NDBG0(("mptsas_scsi_tgt_init: hbadip=0x%p tgtdip=0x%p lun=%d",
2984 (void *)hba_dip, (void *)tgt_dip, lun));
2986 if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
2987 (void) ndi_merge_node(tgt_dip, mptsas_name_child);
2988 ddi_set_name_addr(tgt_dip, NULL);
2989 return (DDI_FAILURE);
2992 * phymask is 0 means the virtual port for RAID
2994 phymask = (mptsas_phymask_t)ddi_prop_get_int(DDI_DEV_T_ANY, hba_dip, 0,
2995 "phymask", 0);
2996 if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
2997 if ((pip = (void *)(sd->sd_private)) == NULL) {
2999 * Very bad news if this occurs. Somehow scsi_vhci has
3000 * lost the pathinfo node for this target.
3002 return (DDI_NOT_WELL_FORMED);
3005 if (mdi_prop_lookup_int(pip, LUN_PROP, &lun) !=
3006 DDI_PROP_SUCCESS) {
3007 mptsas_log(mpt, CE_WARN, "Get lun property failed\n");
3008 return (DDI_FAILURE);
3011 if (mdi_prop_lookup_string(pip, SCSI_ADDR_PROP_TARGET_PORT,
3012 &psas_wwn) == MDI_SUCCESS) {
3013 if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
3014 sas_wwn = 0;
3016 (void) mdi_prop_free(psas_wwn);
3018 } else {
3019 lun = ddi_prop_get_int(DDI_DEV_T_ANY, tgt_dip,
3020 DDI_PROP_DONTPASS, LUN_PROP, 0);
3021 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip,
3022 DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET_PORT, &psas_wwn) ==
3023 DDI_PROP_SUCCESS) {
3024 if (scsi_wwnstr_to_wwn(psas_wwn, &sas_wwn)) {
3025 sas_wwn = 0;
3027 ddi_prop_free(psas_wwn);
3028 } else {
3029 sas_wwn = 0;
3033 ASSERT((sas_wwn != 0) || (phymask != 0));
3034 addr.mta_wwn = sas_wwn;
3035 addr.mta_phymask = phymask;
3036 mutex_enter(&mpt->m_mutex);
3037 ptgt = refhash_lookup(mpt->m_targets, &addr);
3038 mutex_exit(&mpt->m_mutex);
3039 if (ptgt == NULL) {
3040 mptsas_log(mpt, CE_WARN, "!tgt_init: target doesn't exist or "
3041 "gone already! phymask:%x, saswwn %"PRIx64, phymask,
3042 sas_wwn);
3043 return (DDI_FAILURE);
3045 if (hba_tran->tran_tgt_private == NULL) {
3046 tgt_private = kmem_zalloc(sizeof (mptsas_tgt_private_t),
3047 KM_SLEEP);
3048 tgt_private->t_lun = lun;
3049 tgt_private->t_private = ptgt;
3050 hba_tran->tran_tgt_private = tgt_private;
3053 if (mdi_component_is_client(tgt_dip, NULL) == MDI_SUCCESS) {
3054 return (DDI_SUCCESS);
3056 mutex_enter(&mpt->m_mutex);
3058 if (ptgt->m_deviceinfo &
3059 (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
3060 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
3061 uchar_t *inq89 = NULL;
3062 int inq89_len = 0x238;
3063 int reallen = 0;
3064 int rval = 0;
3065 struct sata_id *sid = NULL;
3066 char model[SATA_ID_MODEL_LEN + 1];
3067 char fw[SATA_ID_FW_LEN + 1];
3068 char *vid, *pid;
3070 mutex_exit(&mpt->m_mutex);
3072 * According SCSI/ATA Translation -2 (SAT-2) revision 01a
3073 * chapter 12.4.2 VPD page 89h includes 512 bytes ATA IDENTIFY
3074 * DEVICE data or ATA IDENTIFY PACKET DEVICE data.
3076 inq89 = kmem_zalloc(inq89_len, KM_SLEEP);
3077 rval = mptsas_inquiry(mpt, ptgt, 0, 0x89,
3078 inq89, inq89_len, &reallen, 1);
3080 if (rval != 0) {
3081 if (inq89 != NULL) {
3082 kmem_free(inq89, inq89_len);
3085 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
3086 "0x89 for SATA target:%x failed!", ptgt->m_devhdl);
3087 return (DDI_SUCCESS);
3089 sid = (void *)(&inq89[60]);
3091 swab(sid->ai_model, model, SATA_ID_MODEL_LEN);
3092 swab(sid->ai_fw, fw, SATA_ID_FW_LEN);
3094 model[SATA_ID_MODEL_LEN] = 0;
3095 fw[SATA_ID_FW_LEN] = 0;
3097 sata_split_model(model, &vid, &pid);
3100 * override SCSA "inquiry-*" properties
3102 if (vid)
3103 (void) scsi_device_prop_update_inqstring(sd,
3104 INQUIRY_VENDOR_ID, vid, strlen(vid));
3105 if (pid)
3106 (void) scsi_device_prop_update_inqstring(sd,
3107 INQUIRY_PRODUCT_ID, pid, strlen(pid));
3108 (void) scsi_device_prop_update_inqstring(sd,
3109 INQUIRY_REVISION_ID, fw, strlen(fw));
3111 if (inq89 != NULL) {
3112 kmem_free(inq89, inq89_len);
3114 } else {
3115 mutex_exit(&mpt->m_mutex);
3118 return (DDI_SUCCESS);
3121 * tran_tgt_free(9E) - target device instance deallocation
3123 static void
3124 mptsas_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
3125 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
3127 _NOTE(ARGUNUSED(hba_dip, tgt_dip, hba_tran, sd))
3129 mptsas_tgt_private_t *tgt_private = hba_tran->tran_tgt_private;
3131 if (tgt_private != NULL) {
3132 kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
3133 hba_tran->tran_tgt_private = NULL;
3138 * scsi_pkt handling
3140 * Visible to the external world via the transport structure.
3144 * Notes:
3145 * - transport the command to the addressed SCSI target/lun device
3146 * - normal operation is to schedule the command to be transported,
3147 * and return TRAN_ACCEPT if this is successful.
3148 * - if NO_INTR, tran_start must poll device for command completion
3150 static int
3151 mptsas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
3153 _NOTE(ARGUNUSED(ap))
3155 mptsas_t *mpt = PKT2MPT(pkt);
3156 mptsas_cmd_t *cmd = PKT2CMD(pkt);
3157 int rval;
3158 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3160 NDBG1(("mptsas_scsi_start: pkt=0x%p", (void *)pkt));
3161 ASSERT(ptgt);
3162 if (ptgt == NULL)
3163 return (TRAN_FATAL_ERROR);
3166 * prepare the pkt before taking mutex.
3168 rval = mptsas_prepare_pkt(cmd);
3169 if (rval != TRAN_ACCEPT) {
3170 return (rval);
3174 * Send the command to target/lun, however your HBA requires it.
3175 * If busy, return TRAN_BUSY; if there's some other formatting error
3176 * in the packet, return TRAN_BADPKT; otherwise, fall through to the
3177 * return of TRAN_ACCEPT.
3179 * Remember that access to shared resources, including the mptsas_t
3180 * data structure and the HBA hardware registers, must be protected
3181 * with mutexes, here and everywhere.
3183 * Also remember that at interrupt time, you'll get an argument
3184 * to the interrupt handler which is a pointer to your mptsas_t
3185 * structure; you'll have to remember which commands are outstanding
3186 * and which scsi_pkt is the currently-running command so the
3187 * interrupt handler can refer to the pkt to set completion
3188 * status, call the target driver back through pkt_comp, etc.
3190 * If the instance lock is held by other thread, don't spin to wait
3191 * for it. Instead, queue the cmd and next time when the instance lock
3192 * is not held, accept all the queued cmd. A extra tx_waitq is
3193 * introduced to protect the queue.
3195 * The polled cmd will not be queud and accepted as usual.
3197 * Under the tx_waitq mutex, record whether a thread is draining
3198 * the tx_waitq. An IO requesting thread that finds the instance
3199 * mutex contended appends to the tx_waitq and while holding the
3200 * tx_wait mutex, if the draining flag is not set, sets it and then
3201 * proceeds to spin for the instance mutex. This scheme ensures that
3202 * the last cmd in a burst be processed.
3204 * we enable this feature only when the helper threads are enabled,
3205 * at which we think the loads are heavy.
3207 * per instance mutex m_tx_waitq_mutex is introduced to protect the
3208 * m_tx_waitqtail, m_tx_waitq, m_tx_draining.
3211 if (mpt->m_doneq_thread_n) {
3212 if (mutex_tryenter(&mpt->m_mutex) != 0) {
3213 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3214 mutex_exit(&mpt->m_mutex);
3215 } else if (cmd->cmd_pkt_flags & FLAG_NOINTR) {
3216 mutex_enter(&mpt->m_mutex);
3217 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3218 mutex_exit(&mpt->m_mutex);
3219 } else {
3220 mutex_enter(&mpt->m_tx_waitq_mutex);
3222 * ptgt->m_dr_flag is protected by m_mutex or
3223 * m_tx_waitq_mutex. In this case, m_tx_waitq_mutex
3224 * is acquired.
3226 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3227 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3229 * The command should be allowed to
3230 * retry by returning TRAN_BUSY to
3231 * to stall the I/O's which come from
3232 * scsi_vhci since the device/path is
3233 * in unstable state now.
3235 mutex_exit(&mpt->m_tx_waitq_mutex);
3236 return (TRAN_BUSY);
3237 } else {
3239 * The device is offline, just fail the
3240 * command by returning
3241 * TRAN_FATAL_ERROR.
3243 mutex_exit(&mpt->m_tx_waitq_mutex);
3244 return (TRAN_FATAL_ERROR);
3247 if (mpt->m_tx_draining) {
3248 cmd->cmd_flags |= CFLAG_TXQ;
3249 *mpt->m_tx_waitqtail = cmd;
3250 mpt->m_tx_waitqtail = &cmd->cmd_linkp;
3251 mutex_exit(&mpt->m_tx_waitq_mutex);
3252 } else { /* drain the queue */
3253 mpt->m_tx_draining = 1;
3254 mutex_exit(&mpt->m_tx_waitq_mutex);
3255 mutex_enter(&mpt->m_mutex);
3256 rval = mptsas_accept_txwq_and_pkt(mpt, cmd);
3257 mutex_exit(&mpt->m_mutex);
3260 } else {
3261 mutex_enter(&mpt->m_mutex);
3263 * ptgt->m_dr_flag is protected by m_mutex or m_tx_waitq_mutex
3264 * in this case, m_mutex is acquired.
3266 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3267 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3269 * commands should be allowed to retry by
3270 * returning TRAN_BUSY to stall the I/O's
3271 * which come from scsi_vhci since the device/
3272 * path is in unstable state now.
3274 mutex_exit(&mpt->m_mutex);
3275 return (TRAN_BUSY);
3276 } else {
3278 * The device is offline, just fail the
3279 * command by returning TRAN_FATAL_ERROR.
3281 mutex_exit(&mpt->m_mutex);
3282 return (TRAN_FATAL_ERROR);
3285 rval = mptsas_accept_pkt(mpt, cmd);
3286 mutex_exit(&mpt->m_mutex);
3289 return (rval);
3293 * Accept all the queued cmds(if any) before accept the current one.
3295 static int
3296 mptsas_accept_txwq_and_pkt(mptsas_t *mpt, mptsas_cmd_t *cmd)
3298 int rval;
3299 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3301 ASSERT(mutex_owned(&mpt->m_mutex));
3303 * The call to mptsas_accept_tx_waitq() must always be performed
3304 * because that is where mpt->m_tx_draining is cleared.
3306 mutex_enter(&mpt->m_tx_waitq_mutex);
3307 mptsas_accept_tx_waitq(mpt);
3308 mutex_exit(&mpt->m_tx_waitq_mutex);
3310 * ptgt->m_dr_flag is protected by m_mutex or m_tx_waitq_mutex
3311 * in this case, m_mutex is acquired.
3313 if (ptgt->m_dr_flag == MPTSAS_DR_INTRANSITION) {
3314 if (cmd->cmd_pkt_flags & FLAG_NOQUEUE) {
3316 * The command should be allowed to retry by returning
3317 * TRAN_BUSY to stall the I/O's which come from
3318 * scsi_vhci since the device/path is in unstable state
3319 * now.
3321 return (TRAN_BUSY);
3322 } else {
3324 * The device is offline, just fail the command by
3325 * return TRAN_FATAL_ERROR.
3327 return (TRAN_FATAL_ERROR);
3330 rval = mptsas_accept_pkt(mpt, cmd);
3332 return (rval);
3335 static int
3336 mptsas_accept_pkt(mptsas_t *mpt, mptsas_cmd_t *cmd)
3338 int rval = TRAN_ACCEPT;
3339 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3341 NDBG1(("mptsas_accept_pkt: cmd=0x%p", (void *)cmd));
3343 ASSERT(mutex_owned(&mpt->m_mutex));
3345 if ((cmd->cmd_flags & CFLAG_PREPARED) == 0) {
3346 rval = mptsas_prepare_pkt(cmd);
3347 if (rval != TRAN_ACCEPT) {
3348 cmd->cmd_flags &= ~CFLAG_TRANFLAG;
3349 return (rval);
3354 * reset the throttle if we were draining
3356 if ((ptgt->m_t_ncmds == 0) &&
3357 (ptgt->m_t_throttle == DRAIN_THROTTLE)) {
3358 NDBG23(("reset throttle"));
3359 ASSERT(ptgt->m_reset_delay == 0);
3360 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
3364 * If HBA is being reset, the DevHandles are being re-initialized,
3365 * which means that they could be invalid even if the target is still
3366 * attached. Check if being reset and if DevHandle is being
3367 * re-initialized. If this is the case, return BUSY so the I/O can be
3368 * retried later.
3370 if ((ptgt->m_devhdl == MPTSAS_INVALID_DEVHDL) && mpt->m_in_reset) {
3371 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
3372 if (cmd->cmd_flags & CFLAG_TXQ) {
3373 mptsas_doneq_add(mpt, cmd);
3374 mptsas_doneq_empty(mpt);
3375 return (rval);
3376 } else {
3377 return (TRAN_BUSY);
3382 * If device handle has already been invalidated, just
3383 * fail the command. In theory, command from scsi_vhci
3384 * client is impossible send down command with invalid
3385 * devhdl since devhdl is set after path offline, target
3386 * driver is not suppose to select a offlined path.
3388 if (ptgt->m_devhdl == MPTSAS_INVALID_DEVHDL) {
3389 NDBG3(("rejecting command, it might because invalid devhdl "
3390 "request."));
3391 mptsas_set_pkt_reason(mpt, cmd, CMD_DEV_GONE, STAT_TERMINATED);
3392 if (cmd->cmd_flags & CFLAG_TXQ) {
3393 mptsas_doneq_add(mpt, cmd);
3394 mptsas_doneq_empty(mpt);
3395 return (rval);
3396 } else {
3397 return (TRAN_FATAL_ERROR);
3401 * The first case is the normal case. mpt gets a command from the
3402 * target driver and starts it.
3403 * Since SMID 0 is reserved and the TM slot is reserved, the actual max
3404 * commands is m_max_requests - 2.
3406 if ((mpt->m_ncmds <= (mpt->m_max_requests - 2)) &&
3407 (ptgt->m_t_throttle > HOLD_THROTTLE) &&
3408 (ptgt->m_t_ncmds < ptgt->m_t_throttle) &&
3409 (ptgt->m_reset_delay == 0) &&
3410 (ptgt->m_t_nwait == 0) &&
3411 ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0)) {
3412 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
3413 (void) mptsas_start_cmd(mpt, cmd);
3414 } else {
3415 mptsas_waitq_add(mpt, cmd);
3417 } else {
3419 * Add this pkt to the work queue
3421 mptsas_waitq_add(mpt, cmd);
3423 if (cmd->cmd_pkt_flags & FLAG_NOINTR) {
3424 (void) mptsas_poll(mpt, cmd, MPTSAS_POLL_TIME);
3427 * Only flush the doneq if this is not a TM
3428 * cmd. For TM cmds the flushing of the
3429 * doneq will be done in those routines.
3431 if ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
3432 mptsas_doneq_empty(mpt);
3436 return (rval);
3440 mptsas_save_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
3442 mptsas_slots_t *slots = mpt->m_active;
3443 uint_t slot, start_rotor;
3444 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
3446 ASSERT(MUTEX_HELD(&mpt->m_mutex));
3449 * Account for reserved TM request slot and reserved SMID of 0.
3451 ASSERT(slots->m_n_normal == (mpt->m_max_requests - 2));
3454 * Find the next available slot, beginning at m_rotor. If no slot is
3455 * available, we'll return FALSE to indicate that. This mechanism
3456 * considers only the normal slots, not the reserved slot 0 nor the
3457 * task management slot m_n_normal + 1. The rotor is left to point to
3458 * the normal slot after the one we select, unless we select the last
3459 * normal slot in which case it returns to slot 1.
3461 start_rotor = slots->m_rotor;
3462 do {
3463 slot = slots->m_rotor++;
3464 if (slots->m_rotor > slots->m_n_normal)
3465 slots->m_rotor = 1;
3467 if (slots->m_rotor == start_rotor)
3468 break;
3469 } while (slots->m_slot[slot] != NULL);
3471 if (slots->m_slot[slot] != NULL)
3472 return (FALSE);
3474 ASSERT(slot != 0 && slot <= slots->m_n_normal);
3476 cmd->cmd_slot = slot;
3477 slots->m_slot[slot] = cmd;
3478 mpt->m_ncmds++;
3481 * only increment per target ncmds if this is not a
3482 * command that has no target associated with it (i.e. a
3483 * event acknoledgment)
3485 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
3487 * Expiration time is set in mptsas_start_cmd
3489 ptgt->m_t_ncmds++;
3490 cmd->cmd_active_expiration = 0;
3491 } else {
3493 * Initialize expiration time for passthrough commands,
3495 cmd->cmd_active_expiration = gethrtime() +
3496 (hrtime_t)cmd->cmd_pkt->pkt_time * NANOSEC;
3498 return (TRUE);
3502 * prepare the pkt:
3503 * the pkt may have been resubmitted or just reused so
3504 * initialize some fields and do some checks.
3506 static int
3507 mptsas_prepare_pkt(mptsas_cmd_t *cmd)
3509 struct scsi_pkt *pkt = CMD2PKT(cmd);
3511 NDBG1(("mptsas_prepare_pkt: cmd=0x%p", (void *)cmd));
3514 * Reinitialize some fields that need it; the packet may
3515 * have been resubmitted
3517 pkt->pkt_reason = CMD_CMPLT;
3518 pkt->pkt_state = 0;
3519 pkt->pkt_statistics = 0;
3520 pkt->pkt_resid = 0;
3521 cmd->cmd_age = 0;
3522 cmd->cmd_pkt_flags = pkt->pkt_flags;
3525 * zero status byte.
3527 *(pkt->pkt_scbp) = 0;
3529 if (cmd->cmd_flags & CFLAG_DMAVALID) {
3530 pkt->pkt_resid = cmd->cmd_dmacount;
3533 * consistent packets need to be sync'ed first
3534 * (only for data going out)
3536 if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
3537 (cmd->cmd_flags & CFLAG_DMASEND)) {
3538 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
3539 DDI_DMA_SYNC_FORDEV);
3543 cmd->cmd_flags =
3544 (cmd->cmd_flags & ~(CFLAG_TRANFLAG)) |
3545 CFLAG_PREPARED | CFLAG_IN_TRANSPORT;
3547 return (TRAN_ACCEPT);
3551 * tran_init_pkt(9E) - allocate scsi_pkt(9S) for command
3553 * One of three possibilities:
3554 * - allocate scsi_pkt
3555 * - allocate scsi_pkt and DMA resources
3556 * - allocate DMA resources to an already-allocated pkt
3558 static struct scsi_pkt *
3559 mptsas_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
3560 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags,
3561 int (*callback)(), caddr_t arg)
3563 mptsas_cmd_t *cmd, *new_cmd;
3564 mptsas_t *mpt = ADDR2MPT(ap);
3565 uint_t oldcookiec;
3566 mptsas_target_t *ptgt = NULL;
3567 int rval;
3568 mptsas_tgt_private_t *tgt_private;
3569 int kf;
3571 kf = (callback == SLEEP_FUNC)? KM_SLEEP: KM_NOSLEEP;
3573 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
3574 tran_tgt_private;
3575 ASSERT(tgt_private != NULL);
3576 if (tgt_private == NULL) {
3577 return (NULL);
3579 ptgt = tgt_private->t_private;
3580 ASSERT(ptgt != NULL);
3581 if (ptgt == NULL)
3582 return (NULL);
3583 ap->a_target = ptgt->m_devhdl;
3584 ap->a_lun = tgt_private->t_lun;
3586 ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
3587 #ifdef MPTSAS_TEST_EXTRN_ALLOC
3588 statuslen *= 100; tgtlen *= 4;
3589 #endif
3590 NDBG3(("mptsas_scsi_init_pkt:\n"
3591 "\ttgt=%d in=0x%p bp=0x%p clen=%d slen=%d tlen=%d flags=%x",
3592 ap->a_target, (void *)pkt, (void *)bp,
3593 cmdlen, statuslen, tgtlen, flags));
3596 * Allocate the new packet.
3598 if (pkt == NULL) {
3599 ddi_dma_handle_t save_dma_handle;
3601 cmd = kmem_cache_alloc(mpt->m_kmem_cache, kf);
3602 if (cmd == NULL)
3603 return (NULL);
3605 save_dma_handle = cmd->cmd_dmahandle;
3606 bzero(cmd, sizeof (*cmd) + scsi_pkt_size());
3607 cmd->cmd_dmahandle = save_dma_handle;
3609 pkt = (void *)((uchar_t *)cmd +
3610 sizeof (struct mptsas_cmd));
3611 pkt->pkt_ha_private = (opaque_t)cmd;
3612 pkt->pkt_address = *ap;
3613 pkt->pkt_private = (opaque_t)cmd->cmd_pkt_private;
3614 pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb;
3615 pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb;
3616 cmd->cmd_pkt = (struct scsi_pkt *)pkt;
3617 cmd->cmd_cdblen = (uchar_t)cmdlen;
3618 cmd->cmd_scblen = statuslen;
3619 cmd->cmd_rqslen = SENSE_LENGTH;
3620 cmd->cmd_tgt_addr = ptgt;
3622 if ((cmdlen > sizeof (cmd->cmd_cdb)) ||
3623 (tgtlen > PKT_PRIV_LEN) ||
3624 (statuslen > EXTCMDS_STATUS_SIZE)) {
3625 int failure;
3628 * We are going to allocate external packet space which
3629 * might include the sense data buffer for DMA so we
3630 * need to increase the reference counter here. In a
3631 * case the HBA is in reset we just simply free the
3632 * allocated packet and bail out.
3634 mutex_enter(&mpt->m_mutex);
3635 if (mpt->m_in_reset) {
3636 mutex_exit(&mpt->m_mutex);
3638 cmd->cmd_flags = CFLAG_FREE;
3639 kmem_cache_free(mpt->m_kmem_cache, cmd);
3640 return (NULL);
3642 mpt->m_extreq_sense_refcount++;
3643 ASSERT(mpt->m_extreq_sense_refcount > 0);
3644 mutex_exit(&mpt->m_mutex);
3647 * if extern alloc fails, all will be
3648 * deallocated, including cmd
3650 failure = mptsas_pkt_alloc_extern(mpt, cmd,
3651 cmdlen, tgtlen, statuslen, kf);
3653 if (failure != 0 || cmd->cmd_extrqslen == 0) {
3655 * If the external packet space allocation
3656 * failed, or we didn't allocate the sense
3657 * data buffer for DMA we need to decrease the
3658 * reference counter.
3660 mutex_enter(&mpt->m_mutex);
3661 ASSERT(mpt->m_extreq_sense_refcount > 0);
3662 mpt->m_extreq_sense_refcount--;
3663 if (mpt->m_extreq_sense_refcount == 0)
3664 cv_broadcast(
3665 &mpt->m_extreq_sense_refcount_cv);
3666 mutex_exit(&mpt->m_mutex);
3668 if (failure != 0) {
3670 * if extern allocation fails, it will
3671 * deallocate the new pkt as well
3673 return (NULL);
3677 new_cmd = cmd;
3679 } else {
3680 cmd = PKT2CMD(pkt);
3681 new_cmd = NULL;
3685 /* grab cmd->cmd_cookiec here as oldcookiec */
3687 oldcookiec = cmd->cmd_cookiec;
3690 * If the dma was broken up into PARTIAL transfers cmd_nwin will be
3691 * greater than 0 and we'll need to grab the next dma window
3694 * SLM-not doing extra command frame right now; may add later
3697 if (cmd->cmd_nwin > 0) {
3700 * Make sure we havn't gone past the the total number
3701 * of windows
3703 if (++cmd->cmd_winindex >= cmd->cmd_nwin) {
3704 return (NULL);
3706 if (ddi_dma_getwin(cmd->cmd_dmahandle, cmd->cmd_winindex,
3707 &cmd->cmd_dma_offset, &cmd->cmd_dma_len,
3708 &cmd->cmd_cookie, &cmd->cmd_cookiec) == DDI_FAILURE) {
3709 return (NULL);
3711 goto get_dma_cookies;
3715 if (flags & PKT_XARQ) {
3716 cmd->cmd_flags |= CFLAG_XARQ;
3720 * DMA resource allocation. This version assumes your
3721 * HBA has some sort of bus-mastering or onboard DMA capability, with a
3722 * scatter-gather list of length MPTSAS_MAX_DMA_SEGS, as given in the
3723 * ddi_dma_attr_t structure and passed to scsi_impl_dmaget.
3725 if (bp && (bp->b_bcount != 0) &&
3726 (cmd->cmd_flags & CFLAG_DMAVALID) == 0) {
3728 int cnt, dma_flags;
3729 mptti_t *dmap; /* ptr to the S/G list */
3732 * Set up DMA memory and position to the next DMA segment.
3734 ASSERT(cmd->cmd_dmahandle != NULL);
3736 if (bp->b_flags & B_READ) {
3737 dma_flags = DDI_DMA_READ;
3738 cmd->cmd_flags &= ~CFLAG_DMASEND;
3739 } else {
3740 dma_flags = DDI_DMA_WRITE;
3741 cmd->cmd_flags |= CFLAG_DMASEND;
3743 if (flags & PKT_CONSISTENT) {
3744 cmd->cmd_flags |= CFLAG_CMDIOPB;
3745 dma_flags |= DDI_DMA_CONSISTENT;
3748 if (flags & PKT_DMA_PARTIAL) {
3749 dma_flags |= DDI_DMA_PARTIAL;
3753 * workaround for byte hole issue on psycho and
3754 * schizo pre 2.1
3756 if ((bp->b_flags & B_READ) && ((bp->b_flags &
3757 (B_PAGEIO|B_REMAPPED)) != B_PAGEIO) &&
3758 ((uintptr_t)bp->b_un.b_addr & 0x7)) {
3759 dma_flags |= DDI_DMA_CONSISTENT;
3762 rval = ddi_dma_buf_bind_handle(cmd->cmd_dmahandle, bp,
3763 dma_flags, callback, arg,
3764 &cmd->cmd_cookie, &cmd->cmd_cookiec);
3765 if (rval == DDI_DMA_PARTIAL_MAP) {
3766 (void) ddi_dma_numwin(cmd->cmd_dmahandle,
3767 &cmd->cmd_nwin);
3768 cmd->cmd_winindex = 0;
3769 (void) ddi_dma_getwin(cmd->cmd_dmahandle,
3770 cmd->cmd_winindex, &cmd->cmd_dma_offset,
3771 &cmd->cmd_dma_len, &cmd->cmd_cookie,
3772 &cmd->cmd_cookiec);
3773 } else if (rval && (rval != DDI_DMA_MAPPED)) {
3774 switch (rval) {
3775 case DDI_DMA_NORESOURCES:
3776 bioerror(bp, 0);
3777 break;
3778 case DDI_DMA_BADATTR:
3779 case DDI_DMA_NOMAPPING:
3780 bioerror(bp, EFAULT);
3781 break;
3782 case DDI_DMA_TOOBIG:
3783 default:
3784 bioerror(bp, EINVAL);
3785 break;
3787 cmd->cmd_flags &= ~CFLAG_DMAVALID;
3788 if (new_cmd) {
3789 mptsas_scsi_destroy_pkt(ap, pkt);
3791 return (NULL);
3794 get_dma_cookies:
3795 cmd->cmd_flags |= CFLAG_DMAVALID;
3796 ASSERT(cmd->cmd_cookiec > 0);
3798 if (cmd->cmd_cookiec > MPTSAS_MAX_CMD_SEGS) {
3799 mptsas_log(mpt, CE_NOTE, "large cookiec received %d\n",
3800 cmd->cmd_cookiec);
3801 bioerror(bp, EINVAL);
3802 if (new_cmd) {
3803 mptsas_scsi_destroy_pkt(ap, pkt);
3805 return (NULL);
3809 * Allocate extra SGL buffer if needed.
3811 if ((cmd->cmd_cookiec > MPTSAS_MAX_FRAME_SGES64(mpt)) &&
3812 (cmd->cmd_extra_frames == NULL)) {
3813 if (mptsas_alloc_extra_sgl_frame(mpt, cmd) ==
3814 DDI_FAILURE) {
3815 mptsas_log(mpt, CE_WARN, "MPT SGL mem alloc "
3816 "failed");
3817 bioerror(bp, ENOMEM);
3818 if (new_cmd) {
3819 mptsas_scsi_destroy_pkt(ap, pkt);
3821 return (NULL);
3826 * Always use scatter-gather transfer
3827 * Use the loop below to store physical addresses of
3828 * DMA segments, from the DMA cookies, into your HBA's
3829 * scatter-gather list.
3830 * We need to ensure we have enough kmem alloc'd
3831 * for the sg entries since we are no longer using an
3832 * array inside mptsas_cmd_t.
3834 * We check cmd->cmd_cookiec against oldcookiec so
3835 * the scatter-gather list is correctly allocated
3838 if (oldcookiec != cmd->cmd_cookiec) {
3839 if (cmd->cmd_sg != (mptti_t *)NULL) {
3840 kmem_free(cmd->cmd_sg, sizeof (mptti_t) *
3841 oldcookiec);
3842 cmd->cmd_sg = NULL;
3846 if (cmd->cmd_sg == (mptti_t *)NULL) {
3847 cmd->cmd_sg = kmem_alloc((size_t)(sizeof (mptti_t)*
3848 cmd->cmd_cookiec), kf);
3850 if (cmd->cmd_sg == (mptti_t *)NULL) {
3851 mptsas_log(mpt, CE_WARN,
3852 "unable to kmem_alloc enough memory "
3853 "for scatter/gather list");
3855 * if we have an ENOMEM condition we need to behave
3856 * the same way as the rest of this routine
3859 bioerror(bp, ENOMEM);
3860 if (new_cmd) {
3861 mptsas_scsi_destroy_pkt(ap, pkt);
3863 return (NULL);
3867 dmap = cmd->cmd_sg;
3869 ASSERT(cmd->cmd_cookie.dmac_size != 0);
3872 * store the first segment into the S/G list
3874 dmap->count = cmd->cmd_cookie.dmac_size;
3875 dmap->addr.address64.Low = (uint32_t)
3876 (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3877 dmap->addr.address64.High = (uint32_t)
3878 (cmd->cmd_cookie.dmac_laddress >> 32);
3881 * dmacount counts the size of the dma for this window
3882 * (if partial dma is being used). totaldmacount
3883 * keeps track of the total amount of dma we have
3884 * transferred for all the windows (needed to calculate
3885 * the resid value below).
3887 cmd->cmd_dmacount = cmd->cmd_cookie.dmac_size;
3888 cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3891 * We already stored the first DMA scatter gather segment,
3892 * start at 1 if we need to store more.
3894 for (cnt = 1; cnt < cmd->cmd_cookiec; cnt++) {
3896 * Get next DMA cookie
3898 ddi_dma_nextcookie(cmd->cmd_dmahandle,
3899 &cmd->cmd_cookie);
3900 dmap++;
3902 cmd->cmd_dmacount += cmd->cmd_cookie.dmac_size;
3903 cmd->cmd_totaldmacount += cmd->cmd_cookie.dmac_size;
3906 * store the segment parms into the S/G list
3908 dmap->count = cmd->cmd_cookie.dmac_size;
3909 dmap->addr.address64.Low = (uint32_t)
3910 (cmd->cmd_cookie.dmac_laddress & 0xffffffffull);
3911 dmap->addr.address64.High = (uint32_t)
3912 (cmd->cmd_cookie.dmac_laddress >> 32);
3916 * If this was partially allocated we set the resid
3917 * the amount of data NOT transferred in this window
3918 * If there is only one window, the resid will be 0
3920 pkt->pkt_resid = (bp->b_bcount - cmd->cmd_totaldmacount);
3921 NDBG3(("mptsas_scsi_init_pkt: cmd_dmacount=%d.",
3922 cmd->cmd_dmacount));
3924 return (pkt);
3928 * tran_destroy_pkt(9E) - scsi_pkt(9s) deallocation
3930 * Notes:
3931 * - also frees DMA resources if allocated
3932 * - implicit DMA synchonization
3934 static void
3935 mptsas_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
3937 mptsas_cmd_t *cmd = PKT2CMD(pkt);
3938 mptsas_t *mpt = ADDR2MPT(ap);
3940 NDBG3(("mptsas_scsi_destroy_pkt: target=%d pkt=0x%p",
3941 ap->a_target, (void *)pkt));
3943 if (cmd->cmd_flags & CFLAG_DMAVALID) {
3944 (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
3945 cmd->cmd_flags &= ~CFLAG_DMAVALID;
3948 if (cmd->cmd_sg) {
3949 kmem_free(cmd->cmd_sg, sizeof (mptti_t) * cmd->cmd_cookiec);
3950 cmd->cmd_sg = NULL;
3953 mptsas_free_extra_sgl_frame(mpt, cmd);
3955 if ((cmd->cmd_flags &
3956 (CFLAG_FREE | CFLAG_CDBEXTERN | CFLAG_PRIVEXTERN |
3957 CFLAG_SCBEXTERN)) == 0) {
3958 cmd->cmd_flags = CFLAG_FREE;
3959 kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
3960 } else {
3961 boolean_t extrqslen = cmd->cmd_extrqslen != 0;
3963 mptsas_pkt_destroy_extern(mpt, cmd);
3966 * If the packet had the sense data buffer for DMA allocated we
3967 * need to decrease the reference counter.
3969 if (extrqslen) {
3970 mutex_enter(&mpt->m_mutex);
3971 ASSERT(mpt->m_extreq_sense_refcount > 0);
3972 mpt->m_extreq_sense_refcount--;
3973 if (mpt->m_extreq_sense_refcount == 0)
3974 cv_broadcast(&mpt->m_extreq_sense_refcount_cv);
3975 mutex_exit(&mpt->m_mutex);
3981 * kmem cache constructor and destructor:
3982 * When constructing, we bzero the cmd and allocate the dma handle
3983 * When destructing, just free the dma handle
3985 static int
3986 mptsas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags)
3988 mptsas_cmd_t *cmd = buf;
3989 mptsas_t *mpt = cdrarg;
3990 int (*callback)(caddr_t);
3992 callback = (kmflags == KM_SLEEP)? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
3994 NDBG4(("mptsas_kmem_cache_constructor"));
3997 * allocate a dma handle
3999 if ((ddi_dma_alloc_handle(mpt->m_dip, &mpt->m_io_dma_attr, callback,
4000 NULL, &cmd->cmd_dmahandle)) != DDI_SUCCESS) {
4001 cmd->cmd_dmahandle = NULL;
4002 return (-1);
4004 return (0);
4007 static void
4008 mptsas_kmem_cache_destructor(void *buf, void *cdrarg)
4010 _NOTE(ARGUNUSED(cdrarg))
4012 mptsas_cmd_t *cmd = buf;
4014 NDBG4(("mptsas_kmem_cache_destructor"));
4016 if (cmd->cmd_dmahandle) {
4017 ddi_dma_free_handle(&cmd->cmd_dmahandle);
4018 cmd->cmd_dmahandle = NULL;
4022 static int
4023 mptsas_cache_frames_constructor(void *buf, void *cdrarg, int kmflags)
4025 mptsas_cache_frames_t *p = buf;
4026 mptsas_t *mpt = cdrarg;
4027 ddi_dma_attr_t frame_dma_attr;
4028 size_t mem_size, alloc_len;
4029 ddi_dma_cookie_t cookie;
4030 uint_t ncookie;
4031 int (*callback)(caddr_t) = (kmflags == KM_SLEEP)
4032 ? DDI_DMA_SLEEP: DDI_DMA_DONTWAIT;
4034 frame_dma_attr = mpt->m_msg_dma_attr;
4035 frame_dma_attr.dma_attr_align = 0x10;
4036 frame_dma_attr.dma_attr_sgllen = 1;
4038 if (ddi_dma_alloc_handle(mpt->m_dip, &frame_dma_attr, callback, NULL,
4039 &p->m_dma_hdl) != DDI_SUCCESS) {
4040 mptsas_log(mpt, CE_WARN, "Unable to allocate dma handle for"
4041 " extra SGL.");
4042 return (DDI_FAILURE);
4045 mem_size = (mpt->m_max_request_frames - 1) * mpt->m_req_frame_size;
4047 if (ddi_dma_mem_alloc(p->m_dma_hdl, mem_size, &mpt->m_dev_acc_attr,
4048 DDI_DMA_CONSISTENT, callback, NULL, (caddr_t *)&p->m_frames_addr,
4049 &alloc_len, &p->m_acc_hdl) != DDI_SUCCESS) {
4050 ddi_dma_free_handle(&p->m_dma_hdl);
4051 p->m_dma_hdl = NULL;
4052 mptsas_log(mpt, CE_WARN, "Unable to allocate dma memory for"
4053 " extra SGL.");
4054 return (DDI_FAILURE);
4057 if (ddi_dma_addr_bind_handle(p->m_dma_hdl, NULL, p->m_frames_addr,
4058 alloc_len, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, callback, NULL,
4059 &cookie, &ncookie) != DDI_DMA_MAPPED) {
4060 (void) ddi_dma_mem_free(&p->m_acc_hdl);
4061 ddi_dma_free_handle(&p->m_dma_hdl);
4062 p->m_dma_hdl = NULL;
4063 mptsas_log(mpt, CE_WARN, "Unable to bind DMA resources for"
4064 " extra SGL");
4065 return (DDI_FAILURE);
4069 * Store the SGL memory address. This chip uses this
4070 * address to dma to and from the driver. The second
4071 * address is the address mpt uses to fill in the SGL.
4073 p->m_phys_addr = cookie.dmac_laddress;
4075 return (DDI_SUCCESS);
4078 static void
4079 mptsas_cache_frames_destructor(void *buf, void *cdrarg)
4081 _NOTE(ARGUNUSED(cdrarg))
4083 mptsas_cache_frames_t *p = buf;
4084 if (p->m_dma_hdl != NULL) {
4085 (void) ddi_dma_unbind_handle(p->m_dma_hdl);
4086 (void) ddi_dma_mem_free(&p->m_acc_hdl);
4087 ddi_dma_free_handle(&p->m_dma_hdl);
4088 p->m_phys_addr = (uintptr_t)NULL;
4089 p->m_frames_addr = NULL;
4090 p->m_dma_hdl = NULL;
4091 p->m_acc_hdl = NULL;
4097 * Figure out if we need to use a different method for the request
4098 * sense buffer and allocate from the map if necessary.
4100 static boolean_t
4101 mptsas_cmdarqsize(mptsas_t *mpt, mptsas_cmd_t *cmd, size_t senselength, int kf)
4103 if (senselength > mpt->m_req_sense_size) {
4104 unsigned long i;
4106 /* Sense length is limited to an 8 bit value in MPI Spec. */
4107 if (senselength > 255)
4108 senselength = 255;
4109 cmd->cmd_extrqschunks = (senselength +
4110 (mpt->m_req_sense_size - 1))/mpt->m_req_sense_size;
4111 i = (kf == KM_SLEEP ? rmalloc_wait : rmalloc)
4112 (mpt->m_erqsense_map, cmd->cmd_extrqschunks);
4114 if (i == 0)
4115 return (B_FALSE);
4117 cmd->cmd_extrqslen = (uint16_t)senselength;
4118 cmd->cmd_extrqsidx = i - 1;
4119 cmd->cmd_arq_buf = mpt->m_extreq_sense +
4120 (cmd->cmd_extrqsidx * mpt->m_req_sense_size);
4121 } else {
4122 cmd->cmd_rqslen = (uchar_t)senselength;
4125 return (B_TRUE);
4129 * allocate and deallocate external pkt space (ie. not part of mptsas_cmd)
4130 * for non-standard length cdb, pkt_private, status areas
4131 * if allocation fails, then deallocate all external space and the pkt
4133 /* ARGSUSED */
4134 static int
4135 mptsas_pkt_alloc_extern(mptsas_t *mpt, mptsas_cmd_t *cmd,
4136 int cmdlen, int tgtlen, int statuslen, int kf)
4138 caddr_t cdbp, scbp, tgt;
4140 NDBG3(("mptsas_pkt_alloc_extern: "
4141 "cmd=0x%p cmdlen=%d tgtlen=%d statuslen=%d kf=%x",
4142 (void *)cmd, cmdlen, tgtlen, statuslen, kf));
4144 tgt = cdbp = scbp = NULL;
4145 cmd->cmd_scblen = statuslen;
4146 cmd->cmd_privlen = (uchar_t)tgtlen;
4148 if (cmdlen > sizeof (cmd->cmd_cdb)) {
4149 if ((cdbp = kmem_zalloc((size_t)cmdlen, kf)) == NULL) {
4150 goto fail;
4152 cmd->cmd_pkt->pkt_cdbp = (opaque_t)cdbp;
4153 cmd->cmd_flags |= CFLAG_CDBEXTERN;
4155 if (tgtlen > PKT_PRIV_LEN) {
4156 if ((tgt = kmem_zalloc((size_t)tgtlen, kf)) == NULL) {
4157 goto fail;
4159 cmd->cmd_flags |= CFLAG_PRIVEXTERN;
4160 cmd->cmd_pkt->pkt_private = tgt;
4162 if (statuslen > EXTCMDS_STATUS_SIZE) {
4163 if ((scbp = kmem_zalloc((size_t)statuslen, kf)) == NULL) {
4164 goto fail;
4166 cmd->cmd_flags |= CFLAG_SCBEXTERN;
4167 cmd->cmd_pkt->pkt_scbp = (opaque_t)scbp;
4169 /* allocate sense data buf for DMA */
4170 if (mptsas_cmdarqsize(mpt, cmd, statuslen -
4171 MPTSAS_GET_ITEM_OFF(struct scsi_arq_status, sts_sensedata),
4172 kf) == B_FALSE)
4173 goto fail;
4175 return (0);
4176 fail:
4177 mptsas_pkt_destroy_extern(mpt, cmd);
4178 return (1);
4182 * deallocate external pkt space and deallocate the pkt
4184 static void
4185 mptsas_pkt_destroy_extern(mptsas_t *mpt, mptsas_cmd_t *cmd)
4187 NDBG3(("mptsas_pkt_destroy_extern: cmd=0x%p", (void *)cmd));
4189 if (cmd->cmd_flags & CFLAG_FREE) {
4190 mptsas_log(mpt, CE_PANIC,
4191 "mptsas_pkt_destroy_extern: freeing free packet");
4192 _NOTE(NOT_REACHED)
4193 /* NOTREACHED */
4195 if (cmd->cmd_extrqslen != 0) {
4196 rmfree(mpt->m_erqsense_map, cmd->cmd_extrqschunks,
4197 cmd->cmd_extrqsidx + 1);
4199 if (cmd->cmd_flags & CFLAG_CDBEXTERN) {
4200 kmem_free(cmd->cmd_pkt->pkt_cdbp, (size_t)cmd->cmd_cdblen);
4202 if (cmd->cmd_flags & CFLAG_SCBEXTERN) {
4203 kmem_free(cmd->cmd_pkt->pkt_scbp, (size_t)cmd->cmd_scblen);
4205 if (cmd->cmd_flags & CFLAG_PRIVEXTERN) {
4206 kmem_free(cmd->cmd_pkt->pkt_private, (size_t)cmd->cmd_privlen);
4208 cmd->cmd_flags = CFLAG_FREE;
4209 kmem_cache_free(mpt->m_kmem_cache, (void *)cmd);
4213 * tran_sync_pkt(9E) - explicit DMA synchronization
4215 /*ARGSUSED*/
4216 static void
4217 mptsas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
4219 mptsas_cmd_t *cmd = PKT2CMD(pkt);
4221 NDBG3(("mptsas_scsi_sync_pkt: target=%d, pkt=0x%p",
4222 ap->a_target, (void *)pkt));
4224 if (cmd->cmd_dmahandle) {
4225 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4226 (cmd->cmd_flags & CFLAG_DMASEND) ?
4227 DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU);
4232 * tran_dmafree(9E) - deallocate DMA resources allocated for command
4234 /*ARGSUSED*/
4235 static void
4236 mptsas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
4238 mptsas_cmd_t *cmd = PKT2CMD(pkt);
4239 mptsas_t *mpt = ADDR2MPT(ap);
4241 NDBG3(("mptsas_scsi_dmafree: target=%d pkt=0x%p",
4242 ap->a_target, (void *)pkt));
4244 if (cmd->cmd_flags & CFLAG_DMAVALID) {
4245 (void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
4246 cmd->cmd_flags &= ~CFLAG_DMAVALID;
4249 mptsas_free_extra_sgl_frame(mpt, cmd);
4252 static void
4253 mptsas_pkt_comp(struct scsi_pkt *pkt, mptsas_cmd_t *cmd)
4255 if ((cmd->cmd_flags & CFLAG_CMDIOPB) &&
4256 (!(cmd->cmd_flags & CFLAG_DMASEND))) {
4257 (void) ddi_dma_sync(cmd->cmd_dmahandle, 0, 0,
4258 DDI_DMA_SYNC_FORCPU);
4260 (*pkt->pkt_comp)(pkt);
4263 static void
4264 mptsas_sge_mainframe(mptsas_cmd_t *cmd, pMpi2SCSIIORequest_t frame,
4265 ddi_acc_handle_t acc_hdl, uint_t cookiec, uint32_t end_flags)
4267 pMpi2SGESimple64_t sge;
4268 mptti_t *dmap;
4269 uint32_t flags;
4271 dmap = cmd->cmd_sg;
4273 sge = (pMpi2SGESimple64_t)(&frame->SGL);
4274 while (cookiec--) {
4275 ddi_put32(acc_hdl,
4276 &sge->Address.Low, dmap->addr.address64.Low);
4277 ddi_put32(acc_hdl,
4278 &sge->Address.High, dmap->addr.address64.High);
4279 ddi_put32(acc_hdl, &sge->FlagsLength,
4280 dmap->count);
4281 flags = ddi_get32(acc_hdl, &sge->FlagsLength);
4282 flags |= ((uint32_t)
4283 (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4284 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4285 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4286 MPI2_SGE_FLAGS_SHIFT);
4289 * If this is the last cookie, we set the flags
4290 * to indicate so
4292 if (cookiec == 0) {
4293 flags |= end_flags;
4295 if (cmd->cmd_flags & CFLAG_DMASEND) {
4296 flags |= (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4297 MPI2_SGE_FLAGS_SHIFT);
4298 } else {
4299 flags |= (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4300 MPI2_SGE_FLAGS_SHIFT);
4302 ddi_put32(acc_hdl, &sge->FlagsLength, flags);
4303 dmap++;
4304 sge++;
4308 static void
4309 mptsas_sge_chain(mptsas_t *mpt, mptsas_cmd_t *cmd,
4310 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4312 pMpi2SGESimple64_t sge;
4313 pMpi2SGEChain64_t sgechain;
4314 uint64_t nframe_phys_addr;
4315 uint_t cookiec;
4316 mptti_t *dmap;
4317 uint32_t flags;
4320 * Save the number of entries in the DMA
4321 * Scatter/Gather list
4323 cookiec = cmd->cmd_cookiec;
4326 * Hereby we start to deal with multiple frames.
4327 * The process is as follows:
4328 * 1. Determine how many frames are needed for SGL element
4329 * storage; Note that all frames are stored in contiguous
4330 * memory space and in 64-bit DMA mode each element is
4331 * 3 double-words (12 bytes) long.
4332 * 2. Fill up the main frame. We need to do this separately
4333 * since it contains the SCSI IO request header and needs
4334 * dedicated processing. Note that the last 4 double-words
4335 * of the SCSI IO header is for SGL element storage
4336 * (MPI2_SGE_IO_UNION).
4337 * 3. Fill the chain element in the main frame, so the DMA
4338 * engine can use the following frames.
4339 * 4. Enter a loop to fill the remaining frames. Note that the
4340 * last frame contains no chain element. The remaining
4341 * frames go into the mpt SGL buffer allocated on the fly,
4342 * not immediately following the main message frame, as in
4343 * Gen1.
4344 * Some restrictions:
4345 * 1. For 64-bit DMA, the simple element and chain element
4346 * are both of 3 double-words (12 bytes) in size, even
4347 * though all frames are stored in the first 4G of mem
4348 * range and the higher 32-bits of the address are always 0.
4349 * 2. On some controllers (like the 1064/1068), a frame can
4350 * hold SGL elements with the last 1 or 2 double-words
4351 * (4 or 8 bytes) un-used. On these controllers, we should
4352 * recognize that there's not enough room for another SGL
4353 * element and move the sge pointer to the next frame.
4355 int i, j, k, l, frames, sgemax;
4356 int temp;
4357 uint8_t chainflags;
4358 uint16_t chainlength;
4359 mptsas_cache_frames_t *p;
4362 * Sgemax is the number of SGE's that will fit
4363 * each extra frame and frames is total
4364 * number of frames we'll need. 1 sge entry per
4365 * frame is reseverd for the chain element thus the -1 below.
4367 sgemax = ((mpt->m_req_frame_size / sizeof (MPI2_SGE_SIMPLE64))
4368 - 1);
4369 temp = (cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) / sgemax;
4372 * A little check to see if we need to round up the number
4373 * of frames we need
4375 if ((cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) - (temp *
4376 sgemax) > 1) {
4377 frames = (temp + 1);
4378 } else {
4379 frames = temp;
4381 dmap = cmd->cmd_sg;
4382 sge = (pMpi2SGESimple64_t)(&frame->SGL);
4385 * First fill in the main frame
4387 j = MPTSAS_MAX_FRAME_SGES64(mpt) - 1;
4388 mptsas_sge_mainframe(cmd, frame, acc_hdl, j,
4389 ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4390 MPI2_SGE_FLAGS_SHIFT));
4391 dmap += j;
4392 sge += j;
4393 j++;
4396 * Fill in the chain element in the main frame.
4397 * About calculation on ChainOffset:
4398 * 1. Struct msg_scsi_io_request has 4 double-words (16 bytes)
4399 * in the end reserved for SGL element storage
4400 * (MPI2_SGE_IO_UNION); we should count it in our
4401 * calculation. See its definition in the header file.
4402 * 2. Constant j is the counter of the current SGL element
4403 * that will be processed, and (j - 1) is the number of
4404 * SGL elements that have been processed (stored in the
4405 * main frame).
4406 * 3. ChainOffset value should be in units of double-words (4
4407 * bytes) so the last value should be divided by 4.
4409 ddi_put8(acc_hdl, &frame->ChainOffset,
4410 (sizeof (MPI2_SCSI_IO_REQUEST) -
4411 sizeof (MPI2_SGE_IO_UNION) +
4412 (j - 1) * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4413 sgechain = (pMpi2SGEChain64_t)sge;
4414 chainflags = (MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4415 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4416 MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4417 ddi_put8(acc_hdl, &sgechain->Flags, chainflags);
4420 * The size of the next frame is the accurate size of space
4421 * (in bytes) used to store the SGL elements. j is the counter
4422 * of SGL elements. (j - 1) is the number of SGL elements that
4423 * have been processed (stored in frames).
4425 if (frames >= 2) {
4426 ASSERT(mpt->m_req_frame_size >= sizeof (MPI2_SGE_SIMPLE64));
4427 chainlength = mpt->m_req_frame_size /
4428 sizeof (MPI2_SGE_SIMPLE64) *
4429 sizeof (MPI2_SGE_SIMPLE64);
4430 } else {
4431 chainlength = ((cookiec - (j - 1)) *
4432 sizeof (MPI2_SGE_SIMPLE64));
4435 p = cmd->cmd_extra_frames;
4437 ddi_put16(acc_hdl, &sgechain->Length, chainlength);
4438 ddi_put32(acc_hdl, &sgechain->Address.Low, p->m_phys_addr);
4439 ddi_put32(acc_hdl, &sgechain->Address.High, p->m_phys_addr >> 32);
4442 * If there are more than 2 frames left we have to
4443 * fill in the next chain offset to the location of
4444 * the chain element in the next frame.
4445 * sgemax is the number of simple elements in an extra
4446 * frame. Note that the value NextChainOffset should be
4447 * in double-words (4 bytes).
4449 if (frames >= 2) {
4450 ddi_put8(acc_hdl, &sgechain->NextChainOffset,
4451 (sgemax * sizeof (MPI2_SGE_SIMPLE64)) >> 2);
4452 } else {
4453 ddi_put8(acc_hdl, &sgechain->NextChainOffset, 0);
4457 * Jump to next frame;
4458 * Starting here, chain buffers go into the per command SGL.
4459 * This buffer is allocated when chain buffers are needed.
4461 sge = (pMpi2SGESimple64_t)p->m_frames_addr;
4462 i = cookiec;
4465 * Start filling in frames with SGE's. If we
4466 * reach the end of frame and still have SGE's
4467 * to fill we need to add a chain element and
4468 * use another frame. j will be our counter
4469 * for what cookie we are at and i will be
4470 * the total cookiec. k is the current frame
4472 for (k = 1; k <= frames; k++) {
4473 for (l = 1; (l <= (sgemax + 1)) && (j <= i); j++, l++) {
4476 * If we have reached the end of frame
4477 * and we have more SGE's to fill in
4478 * we have to fill the final entry
4479 * with a chain element and then
4480 * continue to the next frame
4482 if ((l == (sgemax + 1)) && (k != frames)) {
4483 sgechain = (pMpi2SGEChain64_t)sge;
4484 j--;
4485 chainflags = (
4486 MPI2_SGE_FLAGS_CHAIN_ELEMENT |
4487 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4488 MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
4489 ddi_put8(p->m_acc_hdl,
4490 &sgechain->Flags, chainflags);
4492 * k is the frame counter and (k + 1)
4493 * is the number of the next frame.
4494 * Note that frames are in contiguous
4495 * memory space.
4497 nframe_phys_addr = p->m_phys_addr +
4498 (mpt->m_req_frame_size * k);
4499 ddi_put32(p->m_acc_hdl,
4500 &sgechain->Address.Low,
4501 nframe_phys_addr);
4502 ddi_put32(p->m_acc_hdl,
4503 &sgechain->Address.High,
4504 nframe_phys_addr >> 32);
4507 * If there are more than 2 frames left
4508 * we have to next chain offset to
4509 * the location of the chain element
4510 * in the next frame and fill in the
4511 * length of the next chain
4513 if ((frames - k) >= 2) {
4514 ddi_put8(p->m_acc_hdl,
4515 &sgechain->NextChainOffset,
4516 (sgemax *
4517 sizeof (MPI2_SGE_SIMPLE64))
4518 >> 2);
4519 ddi_put16(p->m_acc_hdl,
4520 &sgechain->Length,
4521 mpt->m_req_frame_size /
4522 sizeof (MPI2_SGE_SIMPLE64) *
4523 sizeof (MPI2_SGE_SIMPLE64));
4524 } else {
4526 * This is the last frame. Set
4527 * the NextChainOffset to 0 and
4528 * Length is the total size of
4529 * all remaining simple elements
4531 ddi_put8(p->m_acc_hdl,
4532 &sgechain->NextChainOffset,
4534 ddi_put16(p->m_acc_hdl,
4535 &sgechain->Length,
4536 (cookiec - j) *
4537 sizeof (MPI2_SGE_SIMPLE64));
4540 /* Jump to the next frame */
4541 sge = (pMpi2SGESimple64_t)
4542 ((char *)p->m_frames_addr +
4543 (int)mpt->m_req_frame_size * k);
4545 continue;
4548 ddi_put32(p->m_acc_hdl,
4549 &sge->Address.Low,
4550 dmap->addr.address64.Low);
4551 ddi_put32(p->m_acc_hdl,
4552 &sge->Address.High,
4553 dmap->addr.address64.High);
4554 ddi_put32(p->m_acc_hdl,
4555 &sge->FlagsLength, dmap->count);
4556 flags = ddi_get32(p->m_acc_hdl,
4557 &sge->FlagsLength);
4558 flags |= ((uint32_t)(
4559 MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
4560 MPI2_SGE_FLAGS_SYSTEM_ADDRESS |
4561 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
4562 MPI2_SGE_FLAGS_SHIFT);
4565 * If we are at the end of the frame and
4566 * there is another frame to fill in
4567 * we set the last simple element as last
4568 * element
4570 if ((l == sgemax) && (k != frames)) {
4571 flags |= ((uint32_t)
4572 (MPI2_SGE_FLAGS_LAST_ELEMENT) <<
4573 MPI2_SGE_FLAGS_SHIFT);
4577 * If this is the final cookie we
4578 * indicate it by setting the flags
4580 if (j == i) {
4581 flags |= ((uint32_t)
4582 (MPI2_SGE_FLAGS_LAST_ELEMENT |
4583 MPI2_SGE_FLAGS_END_OF_BUFFER |
4584 MPI2_SGE_FLAGS_END_OF_LIST) <<
4585 MPI2_SGE_FLAGS_SHIFT);
4587 if (cmd->cmd_flags & CFLAG_DMASEND) {
4588 flags |=
4589 (MPI2_SGE_FLAGS_HOST_TO_IOC <<
4590 MPI2_SGE_FLAGS_SHIFT);
4591 } else {
4592 flags |=
4593 (MPI2_SGE_FLAGS_IOC_TO_HOST <<
4594 MPI2_SGE_FLAGS_SHIFT);
4596 ddi_put32(p->m_acc_hdl,
4597 &sge->FlagsLength, flags);
4598 dmap++;
4599 sge++;
4604 * Sync DMA with the chain buffers that were just created
4606 (void) ddi_dma_sync(p->m_dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
4609 static void
4610 mptsas_ieee_sge_mainframe(mptsas_cmd_t *cmd, pMpi2SCSIIORequest_t frame,
4611 ddi_acc_handle_t acc_hdl, uint_t cookiec, uint8_t end_flag)
4613 pMpi2IeeeSgeSimple64_t ieeesge;
4614 mptti_t *dmap;
4615 uint8_t flags;
4617 dmap = cmd->cmd_sg;
4619 NDBG1(("mptsas_ieee_sge_mainframe: cookiec=%d, %s", cookiec,
4620 cmd->cmd_flags & CFLAG_DMASEND?"Out":"In"));
4622 ieeesge = (pMpi2IeeeSgeSimple64_t)(&frame->SGL);
4623 while (cookiec--) {
4624 ddi_put32(acc_hdl,
4625 &ieeesge->Address.Low, dmap->addr.address64.Low);
4626 ddi_put32(acc_hdl,
4627 &ieeesge->Address.High, dmap->addr.address64.High);
4628 ddi_put32(acc_hdl, &ieeesge->Length,
4629 dmap->count);
4630 NDBG1(("mptsas_ieee_sge_mainframe: len=%d", dmap->count));
4631 flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
4632 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4635 * If this is the last cookie, we set the flags
4636 * to indicate so
4638 if (cookiec == 0) {
4639 flags |= end_flag;
4642 ddi_put8(acc_hdl, &ieeesge->Flags, flags);
4643 dmap++;
4644 ieeesge++;
4648 static void
4649 mptsas_ieee_sge_chain(mptsas_t *mpt, mptsas_cmd_t *cmd,
4650 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4652 pMpi2IeeeSgeSimple64_t ieeesge;
4653 pMpi25IeeeSgeChain64_t ieeesgechain;
4654 uint64_t nframe_phys_addr;
4655 uint_t cookiec;
4656 mptti_t *dmap;
4657 uint8_t flags;
4660 * Save the number of entries in the DMA
4661 * Scatter/Gather list
4663 cookiec = cmd->cmd_cookiec;
4665 NDBG1(("mptsas_ieee_sge_chain: cookiec=%d", cookiec));
4668 * Hereby we start to deal with multiple frames.
4669 * The process is as follows:
4670 * 1. Determine how many frames are needed for SGL element
4671 * storage; Note that all frames are stored in contiguous
4672 * memory space and in 64-bit DMA mode each element is
4673 * 4 double-words (16 bytes) long.
4674 * 2. Fill up the main frame. We need to do this separately
4675 * since it contains the SCSI IO request header and needs
4676 * dedicated processing. Note that the last 4 double-words
4677 * of the SCSI IO header is for SGL element storage
4678 * (MPI2_SGE_IO_UNION).
4679 * 3. Fill the chain element in the main frame, so the DMA
4680 * engine can use the following frames.
4681 * 4. Enter a loop to fill the remaining frames. Note that the
4682 * last frame contains no chain element. The remaining
4683 * frames go into the mpt SGL buffer allocated on the fly,
4684 * not immediately following the main message frame, as in
4685 * Gen1.
4686 * Restrictions:
4687 * For 64-bit DMA, the simple element and chain element
4688 * are both of 4 double-words (16 bytes) in size, even
4689 * though all frames are stored in the first 4G of mem
4690 * range and the higher 32-bits of the address are always 0.
4692 int i, j, k, l, frames, sgemax;
4693 int temp;
4694 uint8_t chainflags;
4695 uint32_t chainlength;
4696 mptsas_cache_frames_t *p;
4699 * Sgemax is the number of SGE's that will fit
4700 * each extra frame and frames is total
4701 * number of frames we'll need. 1 sge entry per
4702 * frame is reseverd for the chain element thus the -1 below.
4704 sgemax = ((mpt->m_req_frame_size / sizeof (MPI2_IEEE_SGE_SIMPLE64))
4705 - 1);
4706 temp = (cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) / sgemax;
4709 * A little check to see if we need to round up the number
4710 * of frames we need
4712 if ((cookiec - (MPTSAS_MAX_FRAME_SGES64(mpt) - 1)) - (temp *
4713 sgemax) > 1) {
4714 frames = (temp + 1);
4715 } else {
4716 frames = temp;
4718 NDBG1(("mptsas_ieee_sge_chain: temp=%d, frames=%d", temp, frames));
4719 dmap = cmd->cmd_sg;
4720 ieeesge = (pMpi2IeeeSgeSimple64_t)(&frame->SGL);
4723 * First fill in the main frame
4725 j = MPTSAS_MAX_FRAME_SGES64(mpt) - 1;
4726 mptsas_ieee_sge_mainframe(cmd, frame, acc_hdl, j, 0);
4727 dmap += j;
4728 ieeesge += j;
4729 j++;
4732 * Fill in the chain element in the main frame.
4733 * About calculation on ChainOffset:
4734 * 1. Struct msg_scsi_io_request has 4 double-words (16 bytes)
4735 * in the end reserved for SGL element storage
4736 * (MPI2_SGE_IO_UNION); we should count it in our
4737 * calculation. See its definition in the header file.
4738 * 2. Constant j is the counter of the current SGL element
4739 * that will be processed, and (j - 1) is the number of
4740 * SGL elements that have been processed (stored in the
4741 * main frame).
4742 * 3. ChainOffset value should be in units of quad-words (16
4743 * bytes) so the last value should be divided by 16.
4745 ddi_put8(acc_hdl, &frame->ChainOffset,
4746 (sizeof (MPI2_SCSI_IO_REQUEST) -
4747 sizeof (MPI2_SGE_IO_UNION) +
4748 (j - 1) * sizeof (MPI2_IEEE_SGE_SIMPLE64)) >> 4);
4749 ieeesgechain = (pMpi25IeeeSgeChain64_t)ieeesge;
4750 chainflags = (MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
4751 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4752 ddi_put8(acc_hdl, &ieeesgechain->Flags, chainflags);
4755 * The size of the next frame is the accurate size of space
4756 * (in bytes) used to store the SGL elements. j is the counter
4757 * of SGL elements. (j - 1) is the number of SGL elements that
4758 * have been processed (stored in frames).
4760 if (frames >= 2) {
4761 ASSERT(mpt->m_req_frame_size >=
4762 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4763 chainlength = mpt->m_req_frame_size /
4764 sizeof (MPI2_IEEE_SGE_SIMPLE64) *
4765 sizeof (MPI2_IEEE_SGE_SIMPLE64);
4766 } else {
4767 chainlength = ((cookiec - (j - 1)) *
4768 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4771 p = cmd->cmd_extra_frames;
4773 ddi_put32(acc_hdl, &ieeesgechain->Length, chainlength);
4774 ddi_put32(acc_hdl, &ieeesgechain->Address.Low, p->m_phys_addr);
4775 ddi_put32(acc_hdl, &ieeesgechain->Address.High, p->m_phys_addr >> 32);
4778 * If there are more than 2 frames left we have to
4779 * fill in the next chain offset to the location of
4780 * the chain element in the next frame.
4781 * sgemax is the number of simple elements in an extra
4782 * frame. Note that the value NextChainOffset should be
4783 * in double-words (4 bytes).
4785 if (frames >= 2) {
4786 ddi_put8(acc_hdl, &ieeesgechain->NextChainOffset,
4787 (sgemax * sizeof (MPI2_IEEE_SGE_SIMPLE64)) >> 4);
4788 } else {
4789 ddi_put8(acc_hdl, &ieeesgechain->NextChainOffset, 0);
4793 * Jump to next frame;
4794 * Starting here, chain buffers go into the per command SGL.
4795 * This buffer is allocated when chain buffers are needed.
4797 ieeesge = (pMpi2IeeeSgeSimple64_t)p->m_frames_addr;
4798 i = cookiec;
4801 * Start filling in frames with SGE's. If we
4802 * reach the end of frame and still have SGE's
4803 * to fill we need to add a chain element and
4804 * use another frame. j will be our counter
4805 * for what cookie we are at and i will be
4806 * the total cookiec. k is the current frame
4808 for (k = 1; k <= frames; k++) {
4809 for (l = 1; (l <= (sgemax + 1)) && (j <= i); j++, l++) {
4812 * If we have reached the end of frame
4813 * and we have more SGE's to fill in
4814 * we have to fill the final entry
4815 * with a chain element and then
4816 * continue to the next frame
4818 if ((l == (sgemax + 1)) && (k != frames)) {
4819 ieeesgechain = (pMpi25IeeeSgeChain64_t)ieeesge;
4820 j--;
4821 chainflags =
4822 MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
4823 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
4824 ddi_put8(p->m_acc_hdl,
4825 &ieeesgechain->Flags, chainflags);
4827 * k is the frame counter and (k + 1)
4828 * is the number of the next frame.
4829 * Note that frames are in contiguous
4830 * memory space.
4832 nframe_phys_addr = p->m_phys_addr +
4833 (mpt->m_req_frame_size * k);
4834 ddi_put32(p->m_acc_hdl,
4835 &ieeesgechain->Address.Low,
4836 nframe_phys_addr);
4837 ddi_put32(p->m_acc_hdl,
4838 &ieeesgechain->Address.High,
4839 nframe_phys_addr >> 32);
4842 * If there are more than 2 frames left
4843 * we have to next chain offset to
4844 * the location of the chain element
4845 * in the next frame and fill in the
4846 * length of the next chain
4848 if ((frames - k) >= 2) {
4849 ddi_put8(p->m_acc_hdl,
4850 &ieeesgechain->NextChainOffset,
4851 (sgemax *
4852 sizeof (MPI2_IEEE_SGE_SIMPLE64))
4853 >> 4);
4854 ASSERT(mpt->m_req_frame_size >=
4855 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4856 ddi_put32(p->m_acc_hdl,
4857 &ieeesgechain->Length,
4858 mpt->m_req_frame_size /
4859 sizeof (MPI2_IEEE_SGE_SIMPLE64) *
4860 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4861 } else {
4863 * This is the last frame. Set
4864 * the NextChainOffset to 0 and
4865 * Length is the total size of
4866 * all remaining simple elements
4868 ddi_put8(p->m_acc_hdl,
4869 &ieeesgechain->NextChainOffset,
4871 ddi_put32(p->m_acc_hdl,
4872 &ieeesgechain->Length,
4873 (cookiec - j) *
4874 sizeof (MPI2_IEEE_SGE_SIMPLE64));
4877 /* Jump to the next frame */
4878 ieeesge = (pMpi2IeeeSgeSimple64_t)
4879 ((char *)p->m_frames_addr +
4880 (int)mpt->m_req_frame_size * k);
4882 continue;
4885 ddi_put32(p->m_acc_hdl,
4886 &ieeesge->Address.Low,
4887 dmap->addr.address64.Low);
4888 ddi_put32(p->m_acc_hdl,
4889 &ieeesge->Address.High,
4890 dmap->addr.address64.High);
4891 ddi_put32(p->m_acc_hdl,
4892 &ieeesge->Length, dmap->count);
4893 flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
4894 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
4897 * If we are at the end of the frame and
4898 * there is another frame to fill in
4899 * do we need to do anything?
4900 * if ((l == sgemax) && (k != frames)) {
4905 * If this is the final cookie set end of list.
4907 if (j == i) {
4908 flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
4911 ddi_put8(p->m_acc_hdl, &ieeesge->Flags, flags);
4912 dmap++;
4913 ieeesge++;
4918 * Sync DMA with the chain buffers that were just created
4920 (void) ddi_dma_sync(p->m_dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
4923 static void
4924 mptsas_sge_setup(mptsas_t *mpt, mptsas_cmd_t *cmd, uint32_t *control,
4925 pMpi2SCSIIORequest_t frame, ddi_acc_handle_t acc_hdl)
4927 ASSERT(cmd->cmd_flags & CFLAG_DMAVALID);
4929 NDBG1(("mptsas_sge_setup: cookiec=%d", cmd->cmd_cookiec));
4932 * Set read/write bit in control.
4934 if (cmd->cmd_flags & CFLAG_DMASEND) {
4935 *control |= MPI2_SCSIIO_CONTROL_WRITE;
4936 } else {
4937 *control |= MPI2_SCSIIO_CONTROL_READ;
4940 ddi_put32(acc_hdl, &frame->DataLength, cmd->cmd_dmacount);
4943 * We have 4 cases here. First where we can fit all the
4944 * SG elements into the main frame, and the case
4945 * where we can't. The SG element is also different when using
4946 * MPI2.5 interface.
4947 * If we have more cookies than we can attach to a frame
4948 * we will need to use a chain element to point
4949 * a location of memory where the rest of the S/G
4950 * elements reside.
4952 if (cmd->cmd_cookiec <= MPTSAS_MAX_FRAME_SGES64(mpt)) {
4953 if (mpt->m_MPI25) {
4954 mptsas_ieee_sge_mainframe(cmd, frame, acc_hdl,
4955 cmd->cmd_cookiec,
4956 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
4957 } else {
4958 mptsas_sge_mainframe(cmd, frame, acc_hdl,
4959 cmd->cmd_cookiec,
4960 ((uint32_t)(MPI2_SGE_FLAGS_LAST_ELEMENT
4961 | MPI2_SGE_FLAGS_END_OF_BUFFER
4962 | MPI2_SGE_FLAGS_END_OF_LIST) <<
4963 MPI2_SGE_FLAGS_SHIFT));
4965 } else {
4966 if (mpt->m_MPI25) {
4967 mptsas_ieee_sge_chain(mpt, cmd, frame, acc_hdl);
4968 } else {
4969 mptsas_sge_chain(mpt, cmd, frame, acc_hdl);
4975 * Interrupt handling
4976 * Utility routine. Poll for status of a command sent to HBA
4977 * without interrupts (a FLAG_NOINTR command).
4980 mptsas_poll(mptsas_t *mpt, mptsas_cmd_t *poll_cmd, int polltime)
4982 int rval = TRUE;
4984 NDBG5(("mptsas_poll: cmd=0x%p", (void *)poll_cmd));
4986 if ((poll_cmd->cmd_flags & CFLAG_TM_CMD) == 0) {
4987 mptsas_restart_hba(mpt);
4991 * Wait, using drv_usecwait(), long enough for the command to
4992 * reasonably return from the target if the target isn't
4993 * "dead". A polled command may well be sent from scsi_poll, and
4994 * there are retries built in to scsi_poll if the transport
4995 * accepted the packet (TRAN_ACCEPT). scsi_poll waits 1 second
4996 * and retries the transport up to scsi_poll_busycnt times
4997 * (currently 60) if
4998 * 1. pkt_reason is CMD_INCOMPLETE and pkt_state is 0, or
4999 * 2. pkt_reason is CMD_CMPLT and *pkt_scbp has STATUS_BUSY
5001 * limit the waiting to avoid a hang in the event that the
5002 * cmd never gets started but we are still receiving interrupts
5004 while (!(poll_cmd->cmd_flags & CFLAG_FINISHED)) {
5005 if (mptsas_wait_intr(mpt, polltime) == FALSE) {
5006 NDBG5(("mptsas_poll: command incomplete"));
5007 rval = FALSE;
5008 break;
5012 if (rval == FALSE) {
5015 * this isn't supposed to happen, the hba must be wedged
5016 * Mark this cmd as a timeout.
5018 mptsas_set_pkt_reason(mpt, poll_cmd, CMD_TIMEOUT,
5019 (STAT_TIMEOUT|STAT_ABORTED));
5021 if (poll_cmd->cmd_queued == FALSE) {
5023 NDBG5(("mptsas_poll: not on waitq"));
5025 poll_cmd->cmd_pkt->pkt_state |=
5026 (STATE_GOT_BUS|STATE_GOT_TARGET|STATE_SENT_CMD);
5027 } else {
5029 /* find and remove it from the waitq */
5030 NDBG5(("mptsas_poll: delete from waitq"));
5031 mptsas_waitq_delete(mpt, poll_cmd);
5035 mptsas_fma_check(mpt, poll_cmd);
5036 NDBG5(("mptsas_poll: done"));
5037 return (rval);
5041 * Used for polling cmds and TM function
5043 static int
5044 mptsas_wait_intr(mptsas_t *mpt, int polltime)
5046 int cnt;
5047 pMpi2ReplyDescriptorsUnion_t reply_desc_union;
5048 uint32_t int_mask;
5050 NDBG5(("mptsas_wait_intr"));
5052 mpt->m_polled_intr = 1;
5055 * Get the current interrupt mask and disable interrupts. When
5056 * re-enabling ints, set mask to saved value.
5058 int_mask = ddi_get32(mpt->m_datap, &mpt->m_reg->HostInterruptMask);
5059 MPTSAS_DISABLE_INTR(mpt);
5062 * Keep polling for at least (polltime * 1000) seconds
5064 for (cnt = 0; cnt < polltime; cnt++) {
5065 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5066 DDI_DMA_SYNC_FORCPU);
5068 reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
5069 MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
5071 if (ddi_get32(mpt->m_acc_post_queue_hdl,
5072 &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
5073 ddi_get32(mpt->m_acc_post_queue_hdl,
5074 &reply_desc_union->Words.High) == 0xFFFFFFFF) {
5075 drv_usecwait(1000);
5076 continue;
5080 * The reply is valid, process it according to its
5081 * type.
5083 mptsas_process_intr(mpt, reply_desc_union);
5085 if (++mpt->m_post_index == mpt->m_post_queue_depth) {
5086 mpt->m_post_index = 0;
5090 * Update the global reply index
5092 ddi_put32(mpt->m_datap,
5093 &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
5094 mpt->m_polled_intr = 0;
5097 * Re-enable interrupts and quit.
5099 ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask,
5100 int_mask);
5101 return (TRUE);
5106 * Clear polling flag, re-enable interrupts and quit.
5108 mpt->m_polled_intr = 0;
5109 ddi_put32(mpt->m_datap, &mpt->m_reg->HostInterruptMask, int_mask);
5110 return (FALSE);
5113 static void
5114 mptsas_handle_scsi_io_success(mptsas_t *mpt,
5115 pMpi2ReplyDescriptorsUnion_t reply_desc)
5117 pMpi2SCSIIOSuccessReplyDescriptor_t scsi_io_success;
5118 uint16_t SMID;
5119 mptsas_slots_t *slots = mpt->m_active;
5120 mptsas_cmd_t *cmd = NULL;
5121 struct scsi_pkt *pkt;
5123 ASSERT(mutex_owned(&mpt->m_mutex));
5125 scsi_io_success = (pMpi2SCSIIOSuccessReplyDescriptor_t)reply_desc;
5126 SMID = ddi_get16(mpt->m_acc_post_queue_hdl, &scsi_io_success->SMID);
5129 * This is a success reply so just complete the IO. First, do a sanity
5130 * check on the SMID. The final slot is used for TM requests, which
5131 * would not come into this reply handler.
5133 if ((SMID == 0) || (SMID > slots->m_n_normal)) {
5134 mptsas_log(mpt, CE_WARN, "?Received invalid SMID of %d\n",
5135 SMID);
5136 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5137 return;
5140 cmd = slots->m_slot[SMID];
5143 * print warning and return if the slot is empty
5145 if (cmd == NULL) {
5146 mptsas_log(mpt, CE_WARN, "?NULL command for successful SCSI IO "
5147 "in slot %d", SMID);
5148 return;
5151 pkt = CMD2PKT(cmd);
5152 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET | STATE_SENT_CMD |
5153 STATE_GOT_STATUS);
5154 if (cmd->cmd_flags & CFLAG_DMAVALID) {
5155 pkt->pkt_state |= STATE_XFERRED_DATA;
5157 pkt->pkt_resid = 0;
5159 if (cmd->cmd_flags & CFLAG_PASSTHRU) {
5160 cmd->cmd_flags |= CFLAG_FINISHED;
5161 cv_broadcast(&mpt->m_passthru_cv);
5162 return;
5163 } else {
5164 mptsas_remove_cmd(mpt, cmd);
5167 if (cmd->cmd_flags & CFLAG_RETRY) {
5169 * The target returned QFULL or busy, do not add tihs
5170 * pkt to the doneq since the hba will retry
5171 * this cmd.
5173 * The pkt has already been resubmitted in
5174 * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
5175 * Remove this cmd_flag here.
5177 cmd->cmd_flags &= ~CFLAG_RETRY;
5178 } else {
5179 mptsas_doneq_add(mpt, cmd);
5183 static void
5184 mptsas_handle_address_reply(mptsas_t *mpt,
5185 pMpi2ReplyDescriptorsUnion_t reply_desc)
5187 pMpi2AddressReplyDescriptor_t address_reply;
5188 pMPI2DefaultReply_t reply;
5189 mptsas_fw_diagnostic_buffer_t *pBuffer;
5190 uint32_t reply_addr, reply_frame_dma_baseaddr;
5191 uint16_t SMID, iocstatus;
5192 mptsas_slots_t *slots = mpt->m_active;
5193 mptsas_cmd_t *cmd = NULL;
5194 uint8_t function, buffer_type;
5195 m_replyh_arg_t *args;
5196 int reply_frame_no;
5198 ASSERT(mutex_owned(&mpt->m_mutex));
5200 address_reply = (pMpi2AddressReplyDescriptor_t)reply_desc;
5201 reply_addr = ddi_get32(mpt->m_acc_post_queue_hdl,
5202 &address_reply->ReplyFrameAddress);
5203 SMID = ddi_get16(mpt->m_acc_post_queue_hdl, &address_reply->SMID);
5206 * If reply frame is not in the proper range we should ignore this
5207 * message and exit the interrupt handler.
5209 reply_frame_dma_baseaddr = mpt->m_reply_frame_dma_addr & 0xffffffffu;
5210 if ((reply_addr < reply_frame_dma_baseaddr) ||
5211 (reply_addr >= (reply_frame_dma_baseaddr +
5212 (mpt->m_reply_frame_size * mpt->m_max_replies))) ||
5213 ((reply_addr - reply_frame_dma_baseaddr) %
5214 mpt->m_reply_frame_size != 0)) {
5215 mptsas_log(mpt, CE_WARN, "?Received invalid reply frame "
5216 "address 0x%x\n", reply_addr);
5217 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5218 return;
5221 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
5222 DDI_DMA_SYNC_FORCPU);
5223 reply = (pMPI2DefaultReply_t)(mpt->m_reply_frame + (reply_addr -
5224 reply_frame_dma_baseaddr));
5225 function = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->Function);
5227 NDBG31(("mptsas_handle_address_reply: function 0x%x, reply_addr=0x%x",
5228 function, reply_addr));
5231 * don't get slot information and command for events since these values
5232 * don't exist
5234 if ((function != MPI2_FUNCTION_EVENT_NOTIFICATION) &&
5235 (function != MPI2_FUNCTION_DIAG_BUFFER_POST)) {
5237 * This could be a TM reply, which use the last allocated SMID,
5238 * so allow for that.
5240 if ((SMID == 0) || (SMID > (slots->m_n_normal + 1))) {
5241 mptsas_log(mpt, CE_WARN, "?Received invalid SMID of "
5242 "%d\n", SMID);
5243 ddi_fm_service_impact(mpt->m_dip,
5244 DDI_SERVICE_UNAFFECTED);
5245 return;
5248 cmd = slots->m_slot[SMID];
5251 * print warning and return if the slot is empty
5253 if (cmd == NULL) {
5254 mptsas_log(mpt, CE_WARN, "?NULL command for address "
5255 "reply in slot %d", SMID);
5256 return;
5258 if ((cmd->cmd_flags &
5259 (CFLAG_PASSTHRU | CFLAG_CONFIG | CFLAG_FW_DIAG))) {
5260 cmd->cmd_rfm = reply_addr;
5261 cmd->cmd_flags |= CFLAG_FINISHED;
5262 cv_broadcast(&mpt->m_passthru_cv);
5263 cv_broadcast(&mpt->m_config_cv);
5264 cv_broadcast(&mpt->m_fw_diag_cv);
5265 return;
5266 } else if (!(cmd->cmd_flags & CFLAG_FW_CMD)) {
5267 mptsas_remove_cmd(mpt, cmd);
5269 NDBG31(("\t\tmptsas_process_intr: slot=%d", SMID));
5272 * Depending on the function, we need to handle
5273 * the reply frame (and cmd) differently.
5275 switch (function) {
5276 case MPI2_FUNCTION_SCSI_IO_REQUEST:
5277 mptsas_check_scsi_io_error(mpt, (pMpi2SCSIIOReply_t)reply, cmd);
5278 break;
5279 case MPI2_FUNCTION_SCSI_TASK_MGMT:
5280 cmd->cmd_rfm = reply_addr;
5281 mptsas_check_task_mgt(mpt, (pMpi2SCSIManagementReply_t)reply,
5282 cmd);
5283 break;
5284 case MPI2_FUNCTION_FW_DOWNLOAD:
5285 cmd->cmd_flags |= CFLAG_FINISHED;
5286 cv_signal(&mpt->m_fw_cv);
5287 break;
5288 case MPI2_FUNCTION_EVENT_NOTIFICATION:
5289 reply_frame_no = (reply_addr - reply_frame_dma_baseaddr) /
5290 mpt->m_reply_frame_size;
5291 args = &mpt->m_replyh_args[reply_frame_no];
5292 args->mpt = (void *)mpt;
5293 args->rfm = reply_addr;
5296 * Record the event if its type is enabled in
5297 * this mpt instance by ioctl.
5299 mptsas_record_event(args);
5302 * Handle time critical events
5303 * NOT_RESPONDING/ADDED only now
5305 if (mptsas_handle_event_sync(args) == DDI_SUCCESS) {
5307 * Would not return main process,
5308 * just let taskq resolve ack action
5309 * and ack would be sent in taskq thread
5311 NDBG20(("send mptsas_handle_event_sync success"));
5314 if (mpt->m_in_reset) {
5315 NDBG20(("dropping event received during reset"));
5316 return;
5319 if ((ddi_taskq_dispatch(mpt->m_event_taskq, mptsas_handle_event,
5320 (void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
5321 mptsas_log(mpt, CE_WARN, "No memory available"
5322 "for dispatch taskq");
5324 * Return the reply frame to the free queue.
5326 ddi_put32(mpt->m_acc_free_queue_hdl,
5327 &((uint32_t *)(void *)
5328 mpt->m_free_queue)[mpt->m_free_index], reply_addr);
5329 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
5330 DDI_DMA_SYNC_FORDEV);
5331 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
5332 mpt->m_free_index = 0;
5335 ddi_put32(mpt->m_datap,
5336 &mpt->m_reg->ReplyFreeHostIndex, mpt->m_free_index);
5338 return;
5339 case MPI2_FUNCTION_DIAG_BUFFER_POST:
5341 * If SMID is 0, this implies that the reply is due to a
5342 * release function with a status that the buffer has been
5343 * released. Set the buffer flags accordingly.
5345 if (SMID == 0) {
5346 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
5347 &reply->IOCStatus);
5348 buffer_type = ddi_get8(mpt->m_acc_reply_frame_hdl,
5349 &(((pMpi2DiagBufferPostReply_t)reply)->BufferType));
5350 if (iocstatus == MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED) {
5351 pBuffer =
5352 &mpt->m_fw_diag_buffer_list[buffer_type];
5353 pBuffer->valid_data = TRUE;
5354 pBuffer->owned_by_firmware = FALSE;
5355 pBuffer->immediate = FALSE;
5357 } else {
5359 * Normal handling of diag post reply with SMID.
5361 cmd = slots->m_slot[SMID];
5364 * print warning and return if the slot is empty
5366 if (cmd == NULL) {
5367 mptsas_log(mpt, CE_WARN, "?NULL command for "
5368 "address reply in slot %d", SMID);
5369 return;
5371 cmd->cmd_rfm = reply_addr;
5372 cmd->cmd_flags |= CFLAG_FINISHED;
5373 cv_broadcast(&mpt->m_fw_diag_cv);
5375 return;
5376 default:
5377 mptsas_log(mpt, CE_WARN, "Unknown function 0x%x ", function);
5378 break;
5382 * Return the reply frame to the free queue.
5384 ddi_put32(mpt->m_acc_free_queue_hdl,
5385 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
5386 reply_addr);
5387 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
5388 DDI_DMA_SYNC_FORDEV);
5389 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
5390 mpt->m_free_index = 0;
5392 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
5393 mpt->m_free_index);
5395 if (cmd->cmd_flags & CFLAG_FW_CMD)
5396 return;
5398 if (cmd->cmd_flags & CFLAG_RETRY) {
5400 * The target returned QFULL or busy, do not add this
5401 * pkt to the doneq since the hba will retry
5402 * this cmd.
5404 * The pkt has already been resubmitted in
5405 * mptsas_handle_qfull() or in mptsas_check_scsi_io_error().
5406 * Remove this cmd_flag here.
5408 cmd->cmd_flags &= ~CFLAG_RETRY;
5409 } else {
5410 mptsas_doneq_add(mpt, cmd);
5414 #ifdef MPTSAS_DEBUG
5415 static uint8_t mptsas_last_sense[256];
5416 #endif
5418 static void
5419 mptsas_check_scsi_io_error(mptsas_t *mpt, pMpi2SCSIIOReply_t reply,
5420 mptsas_cmd_t *cmd)
5422 uint8_t scsi_status, scsi_state;
5423 uint16_t ioc_status, cmd_rqs_len;
5424 uint32_t xferred, sensecount, responsedata, loginfo = 0;
5425 struct scsi_pkt *pkt;
5426 struct scsi_arq_status *arqstat;
5427 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
5428 uint8_t *sensedata = NULL;
5429 uint64_t sas_wwn;
5430 uint8_t phy;
5431 char wwn_str[MPTSAS_WWN_STRLEN];
5433 scsi_status = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIStatus);
5434 ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5435 scsi_state = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->SCSIState);
5436 xferred = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->TransferCount);
5437 sensecount = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->SenseCount);
5438 responsedata = ddi_get32(mpt->m_acc_reply_frame_hdl,
5439 &reply->ResponseInfo);
5441 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
5442 sas_wwn = ptgt->m_addr.mta_wwn;
5443 phy = ptgt->m_phynum;
5444 if (sas_wwn == 0) {
5445 (void) sprintf(wwn_str, "p%x", phy);
5446 } else {
5447 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
5449 loginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
5450 &reply->IOCLogInfo);
5451 mptsas_log(mpt, CE_NOTE,
5452 "?Log info 0x%x received for target %d %s.\n"
5453 "\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5454 loginfo, Tgt(cmd), wwn_str, scsi_status, ioc_status,
5455 scsi_state);
5458 NDBG31(("\t\tscsi_status=0x%x, ioc_status=0x%x, scsi_state=0x%x",
5459 scsi_status, ioc_status, scsi_state));
5461 pkt = CMD2PKT(cmd);
5462 *(pkt->pkt_scbp) = scsi_status;
5464 if (loginfo == 0x31170000) {
5466 * if loginfo PL_LOGINFO_CODE_IO_DEVICE_MISSING_DELAY_RETRY
5467 * 0x31170000 comes, that means the device missing delay
5468 * is in progressing, the command need retry later.
5470 *(pkt->pkt_scbp) = STATUS_BUSY;
5471 return;
5474 if ((scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS) &&
5475 ((ioc_status & MPI2_IOCSTATUS_MASK) ==
5476 MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE)) {
5477 pkt->pkt_reason = CMD_INCOMPLETE;
5478 pkt->pkt_state |= STATE_GOT_BUS;
5479 if (ptgt->m_reset_delay == 0) {
5480 mptsas_set_throttle(mpt, ptgt,
5481 DRAIN_THROTTLE);
5483 return;
5486 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
5487 responsedata &= 0x000000FF;
5488 if (responsedata & MPTSAS_SCSI_RESPONSE_CODE_TLR_OFF) {
5489 mptsas_log(mpt, CE_NOTE, "Do not support the TLR\n");
5490 pkt->pkt_reason = CMD_TLR_OFF;
5491 return;
5496 switch (scsi_status) {
5497 case MPI2_SCSI_STATUS_CHECK_CONDITION:
5498 pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5499 arqstat = (void*)(pkt->pkt_scbp);
5500 arqstat->sts_rqpkt_status = *((struct scsi_status *)
5501 (pkt->pkt_scbp));
5502 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET |
5503 STATE_SENT_CMD | STATE_GOT_STATUS | STATE_ARQ_DONE);
5504 if (cmd->cmd_flags & CFLAG_XARQ) {
5505 pkt->pkt_state |= STATE_XARQ_DONE;
5507 if (pkt->pkt_resid != cmd->cmd_dmacount) {
5508 pkt->pkt_state |= STATE_XFERRED_DATA;
5510 arqstat->sts_rqpkt_reason = pkt->pkt_reason;
5511 arqstat->sts_rqpkt_state = pkt->pkt_state;
5512 arqstat->sts_rqpkt_state |= STATE_XFERRED_DATA;
5513 arqstat->sts_rqpkt_statistics = pkt->pkt_statistics;
5514 sensedata = (uint8_t *)&arqstat->sts_sensedata;
5515 cmd_rqs_len = cmd->cmd_extrqslen ?
5516 cmd->cmd_extrqslen : cmd->cmd_rqslen;
5517 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
5518 DDI_DMA_SYNC_FORKERNEL);
5519 #ifdef MPTSAS_DEBUG
5520 bcopy(cmd->cmd_arq_buf, mptsas_last_sense,
5521 ((cmd_rqs_len >= sizeof (mptsas_last_sense)) ?
5522 sizeof (mptsas_last_sense):cmd_rqs_len));
5523 #endif
5524 bcopy((uchar_t *)cmd->cmd_arq_buf, sensedata,
5525 ((cmd_rqs_len >= sensecount) ? sensecount :
5526 cmd_rqs_len));
5527 arqstat->sts_rqpkt_resid = (cmd_rqs_len - sensecount);
5528 cmd->cmd_flags |= CFLAG_CMDARQ;
5530 * Set proper status for pkt if autosense was valid
5532 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
5533 struct scsi_status zero_status = { 0 };
5534 arqstat->sts_rqpkt_status = zero_status;
5538 * ASC=0x47 is parity error
5539 * ASC=0x48 is initiator detected error received
5541 if ((scsi_sense_key(sensedata) == KEY_ABORTED_COMMAND) &&
5542 ((scsi_sense_asc(sensedata) == 0x47) ||
5543 (scsi_sense_asc(sensedata) == 0x48))) {
5544 mptsas_log(mpt, CE_NOTE, "Aborted_command!");
5548 * ASC/ASCQ=0x3F/0x0E means report_luns data changed
5549 * ASC/ASCQ=0x25/0x00 means invalid lun
5551 if (((scsi_sense_key(sensedata) == KEY_UNIT_ATTENTION) &&
5552 (scsi_sense_asc(sensedata) == 0x3F) &&
5553 (scsi_sense_ascq(sensedata) == 0x0E)) ||
5554 ((scsi_sense_key(sensedata) == KEY_ILLEGAL_REQUEST) &&
5555 (scsi_sense_asc(sensedata) == 0x25) &&
5556 (scsi_sense_ascq(sensedata) == 0x00))) {
5557 mptsas_topo_change_list_t *topo_node = NULL;
5559 topo_node = kmem_zalloc(
5560 sizeof (mptsas_topo_change_list_t),
5561 KM_NOSLEEP);
5562 if (topo_node == NULL) {
5563 mptsas_log(mpt, CE_NOTE, "No memory"
5564 "resource for handle SAS dynamic"
5565 "reconfigure.\n");
5566 break;
5568 topo_node->mpt = mpt;
5569 topo_node->event = MPTSAS_DR_EVENT_RECONFIG_TARGET;
5570 topo_node->un.phymask = ptgt->m_addr.mta_phymask;
5571 topo_node->devhdl = ptgt->m_devhdl;
5572 topo_node->object = (void *)ptgt;
5573 topo_node->flags = MPTSAS_TOPO_FLAG_LUN_ASSOCIATED;
5575 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
5576 mptsas_handle_dr,
5577 (void *)topo_node,
5578 DDI_NOSLEEP)) != DDI_SUCCESS) {
5579 kmem_free(topo_node,
5580 sizeof (mptsas_topo_change_list_t));
5581 mptsas_log(mpt, CE_NOTE, "mptsas start taskq"
5582 "for handle SAS dynamic reconfigure"
5583 "failed. \n");
5586 break;
5587 case MPI2_SCSI_STATUS_GOOD:
5588 switch (ioc_status & MPI2_IOCSTATUS_MASK) {
5589 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
5590 pkt->pkt_reason = CMD_DEV_GONE;
5591 pkt->pkt_state |= STATE_GOT_BUS;
5592 if (ptgt->m_reset_delay == 0) {
5593 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5595 NDBG31(("lost disk for target%d, command:%x",
5596 Tgt(cmd), pkt->pkt_cdbp[0]));
5597 break;
5598 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
5599 NDBG31(("data overrun: xferred=%d", xferred));
5600 NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5601 pkt->pkt_reason = CMD_DATA_OVR;
5602 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5603 | STATE_SENT_CMD | STATE_GOT_STATUS
5604 | STATE_XFERRED_DATA);
5605 pkt->pkt_resid = 0;
5606 break;
5607 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
5608 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
5609 NDBG31(("data underrun: xferred=%d", xferred));
5610 NDBG31(("dmacount=%d", cmd->cmd_dmacount));
5611 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET
5612 | STATE_SENT_CMD | STATE_GOT_STATUS);
5613 pkt->pkt_resid = (cmd->cmd_dmacount - xferred);
5614 if (pkt->pkt_resid != cmd->cmd_dmacount) {
5615 pkt->pkt_state |= STATE_XFERRED_DATA;
5617 break;
5618 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
5619 if (cmd->cmd_active_expiration <= gethrtime()) {
5621 * When timeout requested, propagate
5622 * proper reason and statistics to
5623 * target drivers.
5625 mptsas_set_pkt_reason(mpt, cmd, CMD_TIMEOUT,
5626 STAT_BUS_RESET | STAT_TIMEOUT);
5627 } else {
5628 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
5629 STAT_BUS_RESET);
5631 break;
5632 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
5633 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
5634 mptsas_set_pkt_reason(mpt,
5635 cmd, CMD_RESET, STAT_DEV_RESET);
5636 break;
5637 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
5638 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
5639 pkt->pkt_state |= (STATE_GOT_BUS | STATE_GOT_TARGET);
5640 mptsas_set_pkt_reason(mpt,
5641 cmd, CMD_TERMINATED, STAT_TERMINATED);
5642 break;
5643 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
5644 case MPI2_IOCSTATUS_BUSY:
5646 * set throttles to drain
5648 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
5649 ptgt = refhash_next(mpt->m_targets, ptgt)) {
5650 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5654 * retry command
5656 cmd->cmd_flags |= CFLAG_RETRY;
5657 cmd->cmd_pkt_flags |= FLAG_HEAD;
5659 (void) mptsas_accept_pkt(mpt, cmd);
5660 break;
5661 default:
5662 mptsas_log(mpt, CE_WARN,
5663 "unknown ioc_status = %x\n", ioc_status);
5664 mptsas_log(mpt, CE_CONT, "scsi_state = %x, transfer "
5665 "count = %x, scsi_status = %x", scsi_state,
5666 xferred, scsi_status);
5667 break;
5669 break;
5670 case MPI2_SCSI_STATUS_TASK_SET_FULL:
5671 mptsas_handle_qfull(mpt, cmd);
5672 break;
5673 case MPI2_SCSI_STATUS_BUSY:
5674 NDBG31(("scsi_status busy received"));
5675 break;
5676 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
5677 NDBG31(("scsi_status reservation conflict received"));
5678 break;
5679 default:
5680 mptsas_log(mpt, CE_WARN, "scsi_status=%x, ioc_status=%x\n",
5681 scsi_status, ioc_status);
5682 mptsas_log(mpt, CE_WARN,
5683 "mptsas_process_intr: invalid scsi status\n");
5684 break;
5688 static void
5689 mptsas_check_task_mgt(mptsas_t *mpt, pMpi2SCSIManagementReply_t reply,
5690 mptsas_cmd_t *cmd)
5692 uint8_t task_type;
5693 uint16_t ioc_status;
5694 uint32_t log_info;
5695 uint16_t dev_handle;
5696 struct scsi_pkt *pkt = CMD2PKT(cmd);
5698 task_type = ddi_get8(mpt->m_acc_reply_frame_hdl, &reply->TaskType);
5699 ioc_status = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->IOCStatus);
5700 log_info = ddi_get32(mpt->m_acc_reply_frame_hdl, &reply->IOCLogInfo);
5701 dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl, &reply->DevHandle);
5703 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5704 mptsas_log(mpt, CE_WARN, "mptsas_check_task_mgt: Task 0x%x "
5705 "failed. IOCStatus=0x%x IOCLogInfo=0x%x target=%d\n",
5706 task_type, ioc_status, log_info, dev_handle);
5707 pkt->pkt_reason = CMD_INCOMPLETE;
5708 return;
5711 switch (task_type) {
5712 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
5713 case MPI2_SCSITASKMGMT_TASKTYPE_CLEAR_TASK_SET:
5714 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
5715 case MPI2_SCSITASKMGMT_TASKTYPE_CLR_ACA:
5716 case MPI2_SCSITASKMGMT_TASKTYPE_QRY_TASK_SET:
5717 case MPI2_SCSITASKMGMT_TASKTYPE_QRY_UNIT_ATTENTION:
5718 break;
5719 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
5720 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
5721 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
5723 * Check for invalid DevHandle of 0 in case application
5724 * sends bad command. DevHandle of 0 could cause problems.
5726 if (dev_handle == 0) {
5727 mptsas_log(mpt, CE_WARN, "!Can't flush target with"
5728 " DevHandle of 0.");
5729 } else {
5730 mptsas_flush_target(mpt, dev_handle, Lun(cmd),
5731 task_type);
5733 break;
5734 default:
5735 mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
5736 task_type);
5737 mptsas_log(mpt, CE_WARN, "ioc status = %x", ioc_status);
5738 break;
5742 static void
5743 mptsas_doneq_thread(mptsas_doneq_thread_arg_t *arg)
5745 mptsas_t *mpt = arg->mpt;
5746 uint64_t t = arg->t;
5747 mptsas_cmd_t *cmd;
5748 struct scsi_pkt *pkt;
5749 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
5751 mutex_enter(&item->mutex);
5752 while (item->flag & MPTSAS_DONEQ_THREAD_ACTIVE) {
5753 if (!item->doneq) {
5754 cv_wait(&item->cv, &item->mutex);
5756 pkt = NULL;
5757 if ((cmd = mptsas_doneq_thread_rm(mpt, t)) != NULL) {
5758 cmd->cmd_flags |= CFLAG_COMPLETED;
5759 pkt = CMD2PKT(cmd);
5761 mutex_exit(&item->mutex);
5762 if (pkt) {
5763 mptsas_pkt_comp(pkt, cmd);
5765 mutex_enter(&item->mutex);
5767 mutex_exit(&item->mutex);
5768 mutex_enter(&mpt->m_doneq_mutex);
5769 mpt->m_doneq_thread_n--;
5770 cv_broadcast(&mpt->m_doneq_thread_cv);
5771 mutex_exit(&mpt->m_doneq_mutex);
5776 * mpt interrupt handler.
5778 static uint_t
5779 mptsas_intr(caddr_t arg1, caddr_t arg2)
5781 mptsas_t *mpt = (void *)arg1;
5782 pMpi2ReplyDescriptorsUnion_t reply_desc_union;
5783 uchar_t did_reply = FALSE;
5785 NDBG1(("mptsas_intr: arg1 0x%p arg2 0x%p", (void *)arg1, (void *)arg2));
5787 mutex_enter(&mpt->m_mutex);
5790 * If interrupts are shared by two channels then check whether this
5791 * interrupt is genuinely for this channel by making sure first the
5792 * chip is in high power state.
5794 if ((mpt->m_options & MPTSAS_OPT_PM) &&
5795 (mpt->m_power_level != PM_LEVEL_D0)) {
5796 mutex_exit(&mpt->m_mutex);
5797 return (DDI_INTR_UNCLAIMED);
5801 * If polling, interrupt was triggered by some shared interrupt because
5802 * IOC interrupts are disabled during polling, so polling routine will
5803 * handle any replies. Considering this, if polling is happening,
5804 * return with interrupt unclaimed.
5806 if (mpt->m_polled_intr) {
5807 mutex_exit(&mpt->m_mutex);
5808 mptsas_log(mpt, CE_WARN, "mpt_sas: Unclaimed interrupt");
5809 return (DDI_INTR_UNCLAIMED);
5813 * Read the istat register.
5815 if ((INTPENDING(mpt)) != 0) {
5817 * read fifo until empty.
5819 _NOTE(CONSTCOND)
5821 while (TRUE) {
5822 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5823 DDI_DMA_SYNC_FORCPU);
5824 reply_desc_union = (pMpi2ReplyDescriptorsUnion_t)
5825 MPTSAS_GET_NEXT_REPLY(mpt, mpt->m_post_index);
5827 if (ddi_get32(mpt->m_acc_post_queue_hdl,
5828 &reply_desc_union->Words.Low) == 0xFFFFFFFF ||
5829 ddi_get32(mpt->m_acc_post_queue_hdl,
5830 &reply_desc_union->Words.High) == 0xFFFFFFFF) {
5831 break;
5835 * The reply is valid, process it according to its
5836 * type. Also, set a flag for updating the reply index
5837 * after they've all been processed.
5839 did_reply = TRUE;
5841 mptsas_process_intr(mpt, reply_desc_union);
5844 * Increment post index and roll over if needed.
5846 if (++mpt->m_post_index == mpt->m_post_queue_depth) {
5847 mpt->m_post_index = 0;
5852 * Update the global reply index if at least one reply was
5853 * processed.
5855 if (did_reply) {
5856 ddi_put32(mpt->m_datap,
5857 &mpt->m_reg->ReplyPostHostIndex, mpt->m_post_index);
5859 } else {
5860 mutex_exit(&mpt->m_mutex);
5861 return (DDI_INTR_UNCLAIMED);
5863 NDBG1(("mptsas_intr complete"));
5866 * If no helper threads are created, process the doneq in ISR. If
5867 * helpers are created, use the doneq length as a metric to measure the
5868 * load on the interrupt CPU. If it is long enough, which indicates the
5869 * load is heavy, then we deliver the IO completions to the helpers.
5870 * This measurement has some limitations, although it is simple and
5871 * straightforward and works well for most of the cases at present.
5873 if (!mpt->m_doneq_thread_n ||
5874 (mpt->m_doneq_len <= mpt->m_doneq_length_threshold)) {
5875 mptsas_doneq_empty(mpt);
5876 } else {
5877 mptsas_deliver_doneq_thread(mpt);
5881 * If there are queued cmd, start them now.
5883 if (mpt->m_waitq != NULL) {
5884 mptsas_restart_waitq(mpt);
5887 mutex_exit(&mpt->m_mutex);
5888 return (DDI_INTR_CLAIMED);
5891 static void
5892 mptsas_process_intr(mptsas_t *mpt,
5893 pMpi2ReplyDescriptorsUnion_t reply_desc_union)
5895 uint8_t reply_type;
5897 ASSERT(mutex_owned(&mpt->m_mutex));
5900 * The reply is valid, process it according to its
5901 * type. Also, set a flag for updated the reply index
5902 * after they've all been processed.
5904 reply_type = ddi_get8(mpt->m_acc_post_queue_hdl,
5905 &reply_desc_union->Default.ReplyFlags);
5906 reply_type &= MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
5907 if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
5908 reply_type == MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS) {
5909 mptsas_handle_scsi_io_success(mpt, reply_desc_union);
5910 } else if (reply_type == MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
5911 mptsas_handle_address_reply(mpt, reply_desc_union);
5912 } else {
5913 mptsas_log(mpt, CE_WARN, "?Bad reply type %x", reply_type);
5914 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
5918 * Clear the reply descriptor for re-use and increment
5919 * index.
5921 ddi_put64(mpt->m_acc_post_queue_hdl,
5922 &((uint64_t *)(void *)mpt->m_post_queue)[mpt->m_post_index],
5923 0xFFFFFFFFFFFFFFFF);
5924 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
5925 DDI_DMA_SYNC_FORDEV);
5929 * handle qfull condition
5931 static void
5932 mptsas_handle_qfull(mptsas_t *mpt, mptsas_cmd_t *cmd)
5934 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
5936 if ((++cmd->cmd_qfull_retries > ptgt->m_qfull_retries) ||
5937 (ptgt->m_qfull_retries == 0)) {
5939 * We have exhausted the retries on QFULL, or,
5940 * the target driver has indicated that it
5941 * wants to handle QFULL itself by setting
5942 * qfull-retries capability to 0. In either case
5943 * we want the target driver's QFULL handling
5944 * to kick in. We do this by having pkt_reason
5945 * as CMD_CMPLT and pkt_scbp as STATUS_QFULL.
5947 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
5948 } else {
5949 if (ptgt->m_reset_delay == 0) {
5950 ptgt->m_t_throttle =
5951 max((ptgt->m_t_ncmds - 2), 0);
5954 cmd->cmd_pkt_flags |= FLAG_HEAD;
5955 cmd->cmd_flags &= ~(CFLAG_TRANFLAG);
5956 cmd->cmd_flags |= CFLAG_RETRY;
5958 (void) mptsas_accept_pkt(mpt, cmd);
5961 * when target gives queue full status with no commands
5962 * outstanding (m_t_ncmds == 0), throttle is set to 0
5963 * (HOLD_THROTTLE), and the queue full handling start
5964 * (see psarc/1994/313); if there are commands outstanding,
5965 * throttle is set to (m_t_ncmds - 2)
5967 if (ptgt->m_t_throttle == HOLD_THROTTLE) {
5969 * By setting throttle to QFULL_THROTTLE, we
5970 * avoid submitting new commands and in
5971 * mptsas_restart_cmd find out slots which need
5972 * their throttles to be cleared.
5974 mptsas_set_throttle(mpt, ptgt, QFULL_THROTTLE);
5975 if (mpt->m_restart_cmd_timeid == 0) {
5976 mpt->m_restart_cmd_timeid =
5977 timeout(mptsas_restart_cmd, mpt,
5978 ptgt->m_qfull_retry_interval);
5984 mptsas_phymask_t
5985 mptsas_physport_to_phymask(mptsas_t *mpt, uint8_t physport)
5987 mptsas_phymask_t phy_mask = 0;
5988 uint8_t i = 0;
5990 NDBG20(("mptsas%d physport_to_phymask enter", mpt->m_instance));
5992 ASSERT(mutex_owned(&mpt->m_mutex));
5995 * If physport is 0xFF, this is a RAID volume. Use phymask of 0.
5997 if (physport == 0xFF) {
5998 return (0);
6001 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
6002 if (mpt->m_phy_info[i].attached_devhdl &&
6003 (mpt->m_phy_info[i].phy_mask != 0) &&
6004 (mpt->m_phy_info[i].port_num == physport)) {
6005 phy_mask = mpt->m_phy_info[i].phy_mask;
6006 break;
6009 NDBG20(("mptsas%d physport_to_phymask:physport :%x phymask :%x, ",
6010 mpt->m_instance, physport, phy_mask));
6011 return (phy_mask);
6015 * mpt free device handle after device gone, by use of passthrough
6017 static int
6018 mptsas_free_devhdl(mptsas_t *mpt, uint16_t devhdl)
6020 Mpi2SasIoUnitControlRequest_t req;
6021 Mpi2SasIoUnitControlReply_t rep;
6022 int ret;
6024 ASSERT(mutex_owned(&mpt->m_mutex));
6027 * Need to compose a SAS IO Unit Control request message
6028 * and call mptsas_do_passthru() function
6030 bzero(&req, sizeof (req));
6031 bzero(&rep, sizeof (rep));
6033 req.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
6034 req.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
6035 req.DevHandle = LE_16(devhdl);
6037 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
6038 sizeof (req), sizeof (rep), 0, 0, NULL, 0, 60, FKIOCTL);
6039 if (ret != 0) {
6040 cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
6041 "Control error %d", ret);
6042 return (DDI_FAILURE);
6045 /* do passthrough success, check the ioc status */
6046 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
6047 cmn_err(CE_WARN, "mptsas_free_devhdl: passthru SAS IO Unit "
6048 "Control IOCStatus %d", LE_16(rep.IOCStatus));
6049 return (DDI_FAILURE);
6052 return (DDI_SUCCESS);
6055 static void
6056 mptsas_update_phymask(mptsas_t *mpt)
6058 mptsas_phymask_t mask = 0, phy_mask;
6059 char *phy_mask_name;
6060 uint8_t current_port;
6061 int i, j;
6063 NDBG20(("mptsas%d update phymask ", mpt->m_instance));
6065 ASSERT(mutex_owned(&mpt->m_mutex));
6067 (void) mptsas_get_sas_io_unit_page(mpt);
6069 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6071 for (i = 0; i < mpt->m_num_phys; i++) {
6072 phy_mask = 0x00;
6074 if (mpt->m_phy_info[i].attached_devhdl == 0)
6075 continue;
6077 bzero(phy_mask_name, sizeof (phy_mask_name));
6079 current_port = mpt->m_phy_info[i].port_num;
6081 if ((mask & (1 << i)) != 0)
6082 continue;
6084 for (j = 0; j < mpt->m_num_phys; j++) {
6085 if (mpt->m_phy_info[j].attached_devhdl &&
6086 (mpt->m_phy_info[j].port_num == current_port)) {
6087 phy_mask |= (1 << j);
6090 mask = mask | phy_mask;
6092 for (j = 0; j < mpt->m_num_phys; j++) {
6093 if ((phy_mask >> j) & 0x01) {
6094 mpt->m_phy_info[j].phy_mask = phy_mask;
6098 (void) sprintf(phy_mask_name, "%x", phy_mask);
6100 mutex_exit(&mpt->m_mutex);
6102 * register a iport, if the port has already been existed
6103 * SCSA will do nothing and just return.
6105 (void) scsi_hba_iport_register(mpt->m_dip, phy_mask_name);
6106 mutex_enter(&mpt->m_mutex);
6108 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6109 NDBG20(("mptsas%d update phymask return", mpt->m_instance));
6113 * mptsas_handle_dr is a task handler for DR, the DR action includes:
6114 * 1. Directly attched Device Added/Removed.
6115 * 2. Expander Device Added/Removed.
6116 * 3. Indirectly Attached Device Added/Expander.
6117 * 4. LUNs of a existing device status change.
6118 * 5. RAID volume created/deleted.
6119 * 6. Member of RAID volume is released because of RAID deletion.
6120 * 7. Physical disks are removed because of RAID creation.
6122 static void
6123 mptsas_handle_dr(void *args)
6125 mptsas_topo_change_list_t *topo_node = NULL;
6126 mptsas_topo_change_list_t *save_node = NULL;
6127 mptsas_t *mpt;
6128 dev_info_t *parent = NULL;
6129 mptsas_phymask_t phymask = 0;
6130 char *phy_mask_name;
6131 uint8_t flags = 0, physport = 0xff;
6132 uint8_t port_update = 0;
6133 uint_t event;
6135 topo_node = (mptsas_topo_change_list_t *)args;
6137 mpt = topo_node->mpt;
6138 event = topo_node->event;
6139 flags = topo_node->flags;
6141 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6143 NDBG20(("mptsas%d handle_dr enter", mpt->m_instance));
6145 switch (event) {
6146 case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6147 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6148 (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE) ||
6149 (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6151 * Direct attached or expander attached device added
6152 * into system or a Phys Disk that is being unhidden.
6154 port_update = 1;
6156 break;
6157 case MPTSAS_DR_EVENT_RECONFIG_SMP:
6159 * New expander added into system, it must be the head
6160 * of topo_change_list_t
6162 port_update = 1;
6163 break;
6164 default:
6165 port_update = 0;
6166 break;
6169 * All cases port_update == 1 may cause initiator port form change
6171 mutex_enter(&mpt->m_mutex);
6172 if (mpt->m_port_chng && port_update) {
6174 * mpt->m_port_chng flag indicates some PHYs of initiator
6175 * port have changed to online. So when expander added or
6176 * directly attached device online event come, we force to
6177 * update port information by issueing SAS IO Unit Page and
6178 * update PHYMASKs.
6180 (void) mptsas_update_phymask(mpt);
6181 mpt->m_port_chng = 0;
6184 mutex_exit(&mpt->m_mutex);
6185 while (topo_node) {
6186 phymask = 0;
6187 if (parent == NULL) {
6188 physport = topo_node->un.physport;
6189 event = topo_node->event;
6190 flags = topo_node->flags;
6191 if (event & (MPTSAS_DR_EVENT_OFFLINE_TARGET |
6192 MPTSAS_DR_EVENT_OFFLINE_SMP)) {
6194 * For all offline events, phymask is known
6196 phymask = topo_node->un.phymask;
6197 goto find_parent;
6199 if (event & MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6200 goto handle_topo_change;
6202 if (flags & MPTSAS_TOPO_FLAG_LUN_ASSOCIATED) {
6203 phymask = topo_node->un.phymask;
6204 goto find_parent;
6207 if ((flags ==
6208 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) &&
6209 (event == MPTSAS_DR_EVENT_RECONFIG_TARGET)) {
6211 * There is no any field in IR_CONFIG_CHANGE
6212 * event indicate physport/phynum, let's get
6213 * parent after SAS Device Page0 request.
6215 goto handle_topo_change;
6218 mutex_enter(&mpt->m_mutex);
6219 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6221 * If the direct attached device added or a
6222 * phys disk is being unhidden, argument
6223 * physport actually is PHY#, so we have to get
6224 * phymask according PHY#.
6226 physport = mpt->m_phy_info[physport].port_num;
6230 * Translate physport to phymask so that we can search
6231 * parent dip.
6233 phymask = mptsas_physport_to_phymask(mpt,
6234 physport);
6235 mutex_exit(&mpt->m_mutex);
6237 find_parent:
6238 bzero(phy_mask_name, MPTSAS_MAX_PHYS);
6240 * For RAID topology change node, write the iport name
6241 * as v0.
6243 if (flags & MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6244 (void) sprintf(phy_mask_name, "v0");
6245 } else {
6247 * phymask can bo 0 if the drive has been
6248 * pulled by the time an add event is
6249 * processed. If phymask is 0, just skip this
6250 * event and continue.
6252 if (phymask == 0) {
6253 mutex_enter(&mpt->m_mutex);
6254 save_node = topo_node;
6255 topo_node = topo_node->next;
6256 ASSERT(save_node);
6257 kmem_free(save_node,
6258 sizeof (mptsas_topo_change_list_t));
6259 mutex_exit(&mpt->m_mutex);
6261 parent = NULL;
6262 continue;
6264 (void) sprintf(phy_mask_name, "%x", phymask);
6266 parent = scsi_hba_iport_find(mpt->m_dip,
6267 phy_mask_name);
6268 if (parent == NULL) {
6269 mptsas_log(mpt, CE_WARN, "Failed to find an "
6270 "iport, should not happen!");
6271 goto out;
6275 ASSERT(parent);
6276 handle_topo_change:
6278 mutex_enter(&mpt->m_mutex);
6280 * If HBA is being reset, don't perform operations depending
6281 * on the IOC. We must free the topo list, however.
6283 if (!mpt->m_in_reset)
6284 mptsas_handle_topo_change(topo_node, parent);
6285 else
6286 NDBG20(("skipping topo change received during reset"));
6287 save_node = topo_node;
6288 topo_node = topo_node->next;
6289 ASSERT(save_node);
6290 kmem_free(save_node, sizeof (mptsas_topo_change_list_t));
6291 mutex_exit(&mpt->m_mutex);
6293 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6294 (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) ||
6295 (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED)) {
6297 * If direct attached device associated, make sure
6298 * reset the parent before start the next one. But
6299 * all devices associated with expander shares the
6300 * parent. Also, reset parent if this is for RAID.
6302 parent = NULL;
6305 out:
6306 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6309 static void
6310 mptsas_handle_topo_change(mptsas_topo_change_list_t *topo_node,
6311 dev_info_t *parent)
6313 mptsas_target_t *ptgt = NULL;
6314 mptsas_smp_t *psmp = NULL;
6315 mptsas_t *mpt = (void *)topo_node->mpt;
6316 uint16_t devhdl;
6317 uint16_t attached_devhdl;
6318 uint64_t sas_wwn = 0;
6319 int rval = 0;
6320 uint32_t page_address;
6321 uint8_t phy, flags;
6322 char *addr = NULL;
6323 dev_info_t *lundip;
6324 int circ = 0, circ1 = 0;
6325 char attached_wwnstr[MPTSAS_WWN_STRLEN];
6327 NDBG20(("mptsas%d handle_topo_change enter, devhdl 0x%x,"
6328 "event 0x%x, flags 0x%x", mpt->m_instance, topo_node->devhdl,
6329 topo_node->event, topo_node->flags));
6331 ASSERT(mutex_owned(&mpt->m_mutex));
6333 switch (topo_node->event) {
6334 case MPTSAS_DR_EVENT_RECONFIG_TARGET:
6336 char *phy_mask_name;
6337 mptsas_phymask_t phymask = 0;
6339 if (topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6341 * Get latest RAID info.
6343 (void) mptsas_get_raid_info(mpt);
6344 ptgt = refhash_linear_search(mpt->m_targets,
6345 mptsas_target_eval_devhdl, &topo_node->devhdl);
6346 if (ptgt == NULL)
6347 break;
6348 } else {
6349 ptgt = (void *)topo_node->object;
6352 if (ptgt == NULL) {
6354 * If a Phys Disk was deleted, RAID info needs to be
6355 * updated to reflect the new topology.
6357 (void) mptsas_get_raid_info(mpt);
6360 * Get sas device page 0 by DevHandle to make sure if
6361 * SSP/SATA end device exist.
6363 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
6364 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
6365 topo_node->devhdl;
6367 rval = mptsas_get_target_device_info(mpt, page_address,
6368 &devhdl, &ptgt);
6369 if (rval == DEV_INFO_WRONG_DEVICE_TYPE) {
6370 mptsas_log(mpt, CE_NOTE,
6371 "mptsas_handle_topo_change: target %d is "
6372 "not a SAS/SATA device. \n",
6373 topo_node->devhdl);
6374 } else if (rval == DEV_INFO_FAIL_ALLOC) {
6375 mptsas_log(mpt, CE_NOTE,
6376 "mptsas_handle_topo_change: could not "
6377 "allocate memory. \n");
6378 } else if (rval == DEV_INFO_FAIL_GUID) {
6379 mptsas_log(mpt, CE_NOTE,
6380 "mptsas_handle_topo_change: could not "
6381 "get SATA GUID for target %d. \n",
6382 topo_node->devhdl);
6385 * If rval is DEV_INFO_PHYS_DISK or indicates failure
6386 * then there is nothing else to do, just leave.
6388 if (rval != DEV_INFO_SUCCESS) {
6389 return;
6393 ASSERT(ptgt->m_devhdl == topo_node->devhdl);
6395 mutex_exit(&mpt->m_mutex);
6396 flags = topo_node->flags;
6398 if (flags == MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED) {
6399 phymask = ptgt->m_addr.mta_phymask;
6400 phy_mask_name = kmem_zalloc(MPTSAS_MAX_PHYS, KM_SLEEP);
6401 (void) sprintf(phy_mask_name, "%x", phymask);
6402 parent = scsi_hba_iport_find(mpt->m_dip,
6403 phy_mask_name);
6404 kmem_free(phy_mask_name, MPTSAS_MAX_PHYS);
6405 if (parent == NULL) {
6406 mptsas_log(mpt, CE_WARN, "Failed to find a "
6407 "iport for PD, should not happen!");
6408 mutex_enter(&mpt->m_mutex);
6409 break;
6413 if (flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) {
6414 ndi_devi_enter(parent, &circ1);
6415 (void) mptsas_config_raid(parent, topo_node->devhdl,
6416 &lundip);
6417 ndi_devi_exit(parent, circ1);
6418 } else {
6420 * hold nexus for bus configure
6422 ndi_devi_enter(scsi_vhci_dip, &circ);
6423 ndi_devi_enter(parent, &circ1);
6424 rval = mptsas_config_target(parent, ptgt);
6426 * release nexus for bus configure
6428 ndi_devi_exit(parent, circ1);
6429 ndi_devi_exit(scsi_vhci_dip, circ);
6432 * Add parent's props for SMHBA support
6434 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6435 bzero(attached_wwnstr,
6436 sizeof (attached_wwnstr));
6437 (void) sprintf(attached_wwnstr, "w%016"PRIx64,
6438 ptgt->m_addr.mta_wwn);
6439 if (ddi_prop_update_string(DDI_DEV_T_NONE,
6440 parent,
6441 SCSI_ADDR_PROP_ATTACHED_PORT,
6442 attached_wwnstr)
6443 != DDI_PROP_SUCCESS) {
6444 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6445 parent,
6446 SCSI_ADDR_PROP_ATTACHED_PORT);
6447 mptsas_log(mpt, CE_WARN, "Failed to"
6448 "attached-port props");
6449 return;
6451 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6452 MPTSAS_NUM_PHYS, 1) !=
6453 DDI_PROP_SUCCESS) {
6454 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6455 parent, MPTSAS_NUM_PHYS);
6456 mptsas_log(mpt, CE_WARN, "Failed to"
6457 " create num-phys props");
6458 return;
6462 * Update PHY info for smhba
6464 mutex_enter(&mpt->m_mutex);
6465 if (mptsas_smhba_phy_init(mpt)) {
6466 mutex_exit(&mpt->m_mutex);
6467 mptsas_log(mpt, CE_WARN, "mptsas phy"
6468 " update failed");
6469 return;
6471 mutex_exit(&mpt->m_mutex);
6474 * topo_node->un.physport is really the PHY#
6475 * for direct attached devices
6477 mptsas_smhba_set_one_phy_props(mpt, parent,
6478 topo_node->un.physport, &attached_devhdl);
6480 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6481 MPTSAS_VIRTUAL_PORT, 0) !=
6482 DDI_PROP_SUCCESS) {
6483 (void) ddi_prop_remove(DDI_DEV_T_NONE,
6484 parent, MPTSAS_VIRTUAL_PORT);
6485 mptsas_log(mpt, CE_WARN,
6486 "mptsas virtual-port"
6487 "port prop update failed");
6488 return;
6492 mutex_enter(&mpt->m_mutex);
6494 NDBG20(("mptsas%d handle_topo_change to online devhdl:%x, "
6495 "phymask:%x.", mpt->m_instance, ptgt->m_devhdl,
6496 ptgt->m_addr.mta_phymask));
6497 break;
6499 case MPTSAS_DR_EVENT_OFFLINE_TARGET:
6501 devhdl = topo_node->devhdl;
6502 ptgt = refhash_linear_search(mpt->m_targets,
6503 mptsas_target_eval_devhdl, &devhdl);
6504 if (ptgt == NULL)
6505 break;
6507 sas_wwn = ptgt->m_addr.mta_wwn;
6508 phy = ptgt->m_phynum;
6510 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
6512 if (sas_wwn) {
6513 (void) sprintf(addr, "w%016"PRIx64, sas_wwn);
6514 } else {
6515 (void) sprintf(addr, "p%x", phy);
6517 ASSERT(ptgt->m_devhdl == devhdl);
6519 if ((topo_node->flags == MPTSAS_TOPO_FLAG_RAID_ASSOCIATED) ||
6520 (topo_node->flags ==
6521 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED)) {
6523 * Get latest RAID info if RAID volume status changes
6524 * or Phys Disk status changes
6526 (void) mptsas_get_raid_info(mpt);
6529 * Abort all outstanding command on the device
6531 rval = mptsas_do_scsi_reset(mpt, devhdl);
6532 if (rval) {
6533 NDBG20(("mptsas%d handle_topo_change to reset target "
6534 "before offline devhdl:%x, phymask:%x, rval:%x",
6535 mpt->m_instance, ptgt->m_devhdl,
6536 ptgt->m_addr.mta_phymask, rval));
6539 mutex_exit(&mpt->m_mutex);
6541 ndi_devi_enter(scsi_vhci_dip, &circ);
6542 ndi_devi_enter(parent, &circ1);
6543 rval = mptsas_offline_target(parent, addr);
6544 ndi_devi_exit(parent, circ1);
6545 ndi_devi_exit(scsi_vhci_dip, circ);
6546 NDBG20(("mptsas%d handle_topo_change to offline devhdl:%x, "
6547 "phymask:%x, rval:%x", mpt->m_instance,
6548 ptgt->m_devhdl, ptgt->m_addr.mta_phymask, rval));
6550 kmem_free(addr, SCSI_MAXNAMELEN);
6553 * Clear parent's props for SMHBA support
6555 flags = topo_node->flags;
6556 if (flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) {
6557 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6558 if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6559 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6560 DDI_PROP_SUCCESS) {
6561 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6562 SCSI_ADDR_PROP_ATTACHED_PORT);
6563 mptsas_log(mpt, CE_WARN, "mptsas attached port "
6564 "prop update failed");
6565 break;
6567 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6568 MPTSAS_NUM_PHYS, 0) !=
6569 DDI_PROP_SUCCESS) {
6570 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6571 MPTSAS_NUM_PHYS);
6572 mptsas_log(mpt, CE_WARN, "mptsas num phys "
6573 "prop update failed");
6574 break;
6576 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6577 MPTSAS_VIRTUAL_PORT, 1) !=
6578 DDI_PROP_SUCCESS) {
6579 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6580 MPTSAS_VIRTUAL_PORT);
6581 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6582 "prop update failed");
6583 break;
6587 mutex_enter(&mpt->m_mutex);
6588 ptgt->m_led_status = 0;
6589 (void) mptsas_flush_led_status(mpt, ptgt);
6590 if (rval == DDI_SUCCESS) {
6591 refhash_remove(mpt->m_targets, ptgt);
6592 ptgt = NULL;
6593 } else {
6595 * clean DR_INTRANSITION flag to allow I/O down to
6596 * PHCI driver since failover finished.
6597 * Invalidate the devhdl
6599 ptgt->m_devhdl = MPTSAS_INVALID_DEVHDL;
6600 ptgt->m_tgt_unconfigured = 0;
6601 mutex_enter(&mpt->m_tx_waitq_mutex);
6602 ptgt->m_dr_flag = MPTSAS_DR_INACTIVE;
6603 mutex_exit(&mpt->m_tx_waitq_mutex);
6607 * Send SAS IO Unit Control to free the dev handle
6609 if ((flags == MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE) ||
6610 (flags == MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE)) {
6611 rval = mptsas_free_devhdl(mpt, devhdl);
6613 NDBG20(("mptsas%d handle_topo_change to remove "
6614 "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6615 rval));
6618 break;
6620 case MPTSAS_TOPO_FLAG_REMOVE_HANDLE:
6622 devhdl = topo_node->devhdl;
6624 * If this is the remove handle event, do a reset first.
6626 if (topo_node->event == MPTSAS_TOPO_FLAG_REMOVE_HANDLE) {
6627 rval = mptsas_do_scsi_reset(mpt, devhdl);
6628 if (rval) {
6629 NDBG20(("mpt%d reset target before remove "
6630 "devhdl:%x, rval:%x", mpt->m_instance,
6631 devhdl, rval));
6636 * Send SAS IO Unit Control to free the dev handle
6638 rval = mptsas_free_devhdl(mpt, devhdl);
6639 NDBG20(("mptsas%d handle_topo_change to remove "
6640 "devhdl:%x, rval:%x", mpt->m_instance, devhdl,
6641 rval));
6642 break;
6644 case MPTSAS_DR_EVENT_RECONFIG_SMP:
6646 mptsas_smp_t smp;
6647 dev_info_t *smpdip;
6649 devhdl = topo_node->devhdl;
6651 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
6652 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)devhdl;
6653 rval = mptsas_get_sas_expander_page0(mpt, page_address, &smp);
6654 if (rval != DDI_SUCCESS) {
6655 mptsas_log(mpt, CE_WARN, "failed to online smp, "
6656 "handle %x", devhdl);
6657 return;
6660 psmp = mptsas_smp_alloc(mpt, &smp);
6661 if (psmp == NULL) {
6662 return;
6665 mutex_exit(&mpt->m_mutex);
6666 ndi_devi_enter(parent, &circ1);
6667 (void) mptsas_online_smp(parent, psmp, &smpdip);
6668 ndi_devi_exit(parent, circ1);
6670 mutex_enter(&mpt->m_mutex);
6671 break;
6673 case MPTSAS_DR_EVENT_OFFLINE_SMP:
6675 devhdl = topo_node->devhdl;
6676 uint32_t dev_info;
6678 psmp = refhash_linear_search(mpt->m_smp_targets,
6679 mptsas_smp_eval_devhdl, &devhdl);
6680 if (psmp == NULL)
6681 break;
6683 * The mptsas_smp_t data is released only if the dip is offlined
6684 * successfully.
6686 mutex_exit(&mpt->m_mutex);
6688 ndi_devi_enter(parent, &circ1);
6689 rval = mptsas_offline_smp(parent, psmp, NDI_DEVI_REMOVE);
6690 ndi_devi_exit(parent, circ1);
6692 dev_info = psmp->m_deviceinfo;
6693 if ((dev_info & DEVINFO_DIRECT_ATTACHED) ==
6694 DEVINFO_DIRECT_ATTACHED) {
6695 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6696 MPTSAS_VIRTUAL_PORT, 1) !=
6697 DDI_PROP_SUCCESS) {
6698 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6699 MPTSAS_VIRTUAL_PORT);
6700 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
6701 "prop update failed");
6702 return;
6705 * Check whether the smp connected to the iport,
6707 if (ddi_prop_update_int(DDI_DEV_T_NONE, parent,
6708 MPTSAS_NUM_PHYS, 0) !=
6709 DDI_PROP_SUCCESS) {
6710 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6711 MPTSAS_NUM_PHYS);
6712 mptsas_log(mpt, CE_WARN, "mptsas num phys"
6713 "prop update failed");
6714 return;
6717 * Clear parent's attached-port props
6719 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6720 if (ddi_prop_update_string(DDI_DEV_T_NONE, parent,
6721 SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwnstr) !=
6722 DDI_PROP_SUCCESS) {
6723 (void) ddi_prop_remove(DDI_DEV_T_NONE, parent,
6724 SCSI_ADDR_PROP_ATTACHED_PORT);
6725 mptsas_log(mpt, CE_WARN, "mptsas attached port "
6726 "prop update failed");
6727 return;
6731 mutex_enter(&mpt->m_mutex);
6732 NDBG20(("mptsas%d handle_topo_change to remove devhdl:%x, "
6733 "rval:%x", mpt->m_instance, psmp->m_devhdl, rval));
6734 if (rval == DDI_SUCCESS) {
6735 refhash_remove(mpt->m_smp_targets, psmp);
6736 } else {
6737 psmp->m_devhdl = MPTSAS_INVALID_DEVHDL;
6740 bzero(attached_wwnstr, sizeof (attached_wwnstr));
6742 break;
6744 default:
6745 return;
6750 * Record the event if its type is enabled in mpt instance by ioctl.
6752 static void
6753 mptsas_record_event(void *args)
6755 m_replyh_arg_t *replyh_arg;
6756 pMpi2EventNotificationReply_t eventreply;
6757 uint32_t event, rfm;
6758 mptsas_t *mpt;
6759 int i, j;
6760 uint16_t event_data_len;
6761 boolean_t sendAEN = FALSE;
6763 replyh_arg = (m_replyh_arg_t *)args;
6764 rfm = replyh_arg->rfm;
6765 mpt = replyh_arg->mpt;
6767 eventreply = (pMpi2EventNotificationReply_t)
6768 (mpt->m_reply_frame + (rfm -
6769 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
6770 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6774 * Generate a system event to let anyone who cares know that a
6775 * LOG_ENTRY_ADDED event has occurred. This is sent no matter what the
6776 * event mask is set to.
6778 if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
6779 sendAEN = TRUE;
6783 * Record the event only if it is not masked. Determine which dword
6784 * and bit of event mask to test.
6786 i = (uint8_t)(event / 32);
6787 j = (uint8_t)(event % 32);
6788 if ((i < 4) && ((1 << j) & mpt->m_event_mask[i])) {
6789 i = mpt->m_event_index;
6790 mpt->m_events[i].Type = event;
6791 mpt->m_events[i].Number = ++mpt->m_event_number;
6792 bzero(mpt->m_events[i].Data, MPTSAS_MAX_EVENT_DATA_LENGTH * 4);
6793 event_data_len = ddi_get16(mpt->m_acc_reply_frame_hdl,
6794 &eventreply->EventDataLength);
6796 if (event_data_len > 0) {
6798 * Limit data to size in m_event entry
6800 if (event_data_len > MPTSAS_MAX_EVENT_DATA_LENGTH) {
6801 event_data_len = MPTSAS_MAX_EVENT_DATA_LENGTH;
6803 for (j = 0; j < event_data_len; j++) {
6804 mpt->m_events[i].Data[j] =
6805 ddi_get32(mpt->m_acc_reply_frame_hdl,
6806 &(eventreply->EventData[j]));
6810 * check for index wrap-around
6812 if (++i == MPTSAS_EVENT_QUEUE_SIZE) {
6813 i = 0;
6815 mpt->m_event_index = (uint8_t)i;
6818 * Set flag to send the event.
6820 sendAEN = TRUE;
6825 * Generate a system event if flag is set to let anyone who cares know
6826 * that an event has occurred.
6828 if (sendAEN) {
6829 (void) ddi_log_sysevent(mpt->m_dip, DDI_VENDOR_LSI, "MPT_SAS",
6830 "SAS", NULL, NULL, DDI_NOSLEEP);
6834 #define SMP_RESET_IN_PROGRESS MPI2_EVENT_SAS_TOPO_LR_SMP_RESET_IN_PROGRESS
6836 * handle sync events from ioc in interrupt
6837 * return value:
6838 * DDI_SUCCESS: The event is handled by this func
6839 * DDI_FAILURE: Event is not handled
6841 static int
6842 mptsas_handle_event_sync(void *args)
6844 m_replyh_arg_t *replyh_arg;
6845 pMpi2EventNotificationReply_t eventreply;
6846 uint32_t event, rfm;
6847 mptsas_t *mpt;
6848 uint_t iocstatus;
6850 replyh_arg = (m_replyh_arg_t *)args;
6851 rfm = replyh_arg->rfm;
6852 mpt = replyh_arg->mpt;
6854 ASSERT(mutex_owned(&mpt->m_mutex));
6856 eventreply = (pMpi2EventNotificationReply_t)
6857 (mpt->m_reply_frame + (rfm -
6858 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
6859 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
6861 if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
6862 &eventreply->IOCStatus)) {
6863 if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
6864 mptsas_log(mpt, CE_WARN,
6865 "!mptsas_handle_event_sync: event 0x%x, "
6866 "IOCStatus=0x%x, "
6867 "IOCLogInfo=0x%x", event, iocstatus,
6868 ddi_get32(mpt->m_acc_reply_frame_hdl,
6869 &eventreply->IOCLogInfo));
6870 } else {
6871 mptsas_log(mpt, CE_WARN,
6872 "mptsas_handle_event_sync: event 0x%x, "
6873 "IOCStatus=0x%x, "
6874 "(IOCLogInfo=0x%x)", event, iocstatus,
6875 ddi_get32(mpt->m_acc_reply_frame_hdl,
6876 &eventreply->IOCLogInfo));
6881 * figure out what kind of event we got and handle accordingly
6883 switch (event) {
6884 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6886 pMpi2EventDataSasTopologyChangeList_t sas_topo_change_list;
6887 uint8_t num_entries, expstatus, phy;
6888 uint8_t phystatus, physport, state, i;
6889 uint8_t start_phy_num, link_rate;
6890 uint16_t dev_handle, reason_code;
6891 uint16_t enc_handle, expd_handle;
6892 char string[80], curr[80], prev[80];
6893 mptsas_topo_change_list_t *topo_head = NULL;
6894 mptsas_topo_change_list_t *topo_tail = NULL;
6895 mptsas_topo_change_list_t *topo_node = NULL;
6896 mptsas_target_t *ptgt;
6897 mptsas_smp_t *psmp;
6898 uint8_t flags = 0, exp_flag;
6899 smhba_info_t *pSmhba = NULL;
6901 NDBG20(("mptsas_handle_event_sync: SAS topology change"));
6903 sas_topo_change_list = (pMpi2EventDataSasTopologyChangeList_t)
6904 eventreply->EventData;
6906 enc_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6907 &sas_topo_change_list->EnclosureHandle);
6908 expd_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6909 &sas_topo_change_list->ExpanderDevHandle);
6910 num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
6911 &sas_topo_change_list->NumEntries);
6912 start_phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
6913 &sas_topo_change_list->StartPhyNum);
6914 expstatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6915 &sas_topo_change_list->ExpStatus);
6916 physport = ddi_get8(mpt->m_acc_reply_frame_hdl,
6917 &sas_topo_change_list->PhysicalPort);
6919 string[0] = 0;
6920 if (expd_handle) {
6921 flags = MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED;
6922 switch (expstatus) {
6923 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
6924 (void) sprintf(string, " added");
6926 * New expander device added
6928 mpt->m_port_chng = 1;
6929 topo_node = kmem_zalloc(
6930 sizeof (mptsas_topo_change_list_t),
6931 KM_SLEEP);
6932 topo_node->mpt = mpt;
6933 topo_node->event = MPTSAS_DR_EVENT_RECONFIG_SMP;
6934 topo_node->un.physport = physport;
6935 topo_node->devhdl = expd_handle;
6936 topo_node->flags = flags;
6937 topo_node->object = NULL;
6938 if (topo_head == NULL) {
6939 topo_head = topo_tail = topo_node;
6940 } else {
6941 topo_tail->next = topo_node;
6942 topo_tail = topo_node;
6944 break;
6945 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
6946 (void) sprintf(string, " not responding, "
6947 "removed");
6948 psmp = refhash_linear_search(mpt->m_smp_targets,
6949 mptsas_smp_eval_devhdl, &expd_handle);
6950 if (psmp == NULL)
6951 break;
6953 topo_node = kmem_zalloc(
6954 sizeof (mptsas_topo_change_list_t),
6955 KM_SLEEP);
6956 topo_node->mpt = mpt;
6957 topo_node->un.phymask =
6958 psmp->m_addr.mta_phymask;
6959 topo_node->event = MPTSAS_DR_EVENT_OFFLINE_SMP;
6960 topo_node->devhdl = expd_handle;
6961 topo_node->flags = flags;
6962 topo_node->object = NULL;
6963 if (topo_head == NULL) {
6964 topo_head = topo_tail = topo_node;
6965 } else {
6966 topo_tail->next = topo_node;
6967 topo_tail = topo_node;
6969 break;
6970 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
6971 break;
6972 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
6973 (void) sprintf(string, " not responding, "
6974 "delaying removal");
6975 break;
6976 default:
6977 break;
6979 } else {
6980 flags = MPTSAS_TOPO_FLAG_DIRECT_ATTACHED_DEVICE;
6983 NDBG20(("SAS TOPOLOGY CHANGE for enclosure %x expander %x%s\n",
6984 enc_handle, expd_handle, string));
6985 for (i = 0; i < num_entries; i++) {
6986 phy = i + start_phy_num;
6987 phystatus = ddi_get8(mpt->m_acc_reply_frame_hdl,
6988 &sas_topo_change_list->PHY[i].PhyStatus);
6989 dev_handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
6990 &sas_topo_change_list->PHY[i].AttachedDevHandle);
6991 reason_code = phystatus & MPI2_EVENT_SAS_TOPO_RC_MASK;
6993 * Filter out processing of Phy Vacant Status unless
6994 * the reason code is "Not Responding". Process all
6995 * other combinations of Phy Status and Reason Codes.
6997 if ((phystatus &
6998 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) &&
6999 (reason_code !=
7000 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)) {
7001 continue;
7003 curr[0] = 0;
7004 prev[0] = 0;
7005 string[0] = 0;
7006 switch (reason_code) {
7007 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
7009 NDBG20(("mptsas%d phy %d physical_port %d "
7010 "dev_handle %d added", mpt->m_instance, phy,
7011 physport, dev_handle));
7012 link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
7013 &sas_topo_change_list->PHY[i].LinkRate);
7014 state = (link_rate &
7015 MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
7016 MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
7017 switch (state) {
7018 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7019 (void) sprintf(curr, "is disabled");
7020 break;
7021 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7022 (void) sprintf(curr, "is offline, "
7023 "failed speed negotiation");
7024 break;
7025 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7026 (void) sprintf(curr, "SATA OOB "
7027 "complete");
7028 break;
7029 case SMP_RESET_IN_PROGRESS:
7030 (void) sprintf(curr, "SMP reset in "
7031 "progress");
7032 break;
7033 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7034 (void) sprintf(curr, "is online at "
7035 "1.5 Gbps");
7036 break;
7037 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7038 (void) sprintf(curr, "is online at 3.0 "
7039 "Gbps");
7040 break;
7041 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7042 (void) sprintf(curr, "is online at 6.0 "
7043 "Gbps");
7044 break;
7045 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7046 (void) sprintf(curr,
7047 "is online at 12.0 Gbps");
7048 break;
7049 default:
7050 (void) sprintf(curr, "state is "
7051 "unknown");
7052 break;
7055 * New target device added into the system.
7056 * Set association flag according to if an
7057 * expander is used or not.
7059 exp_flag =
7060 MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
7061 if (flags ==
7062 MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
7063 flags = exp_flag;
7065 topo_node = kmem_zalloc(
7066 sizeof (mptsas_topo_change_list_t),
7067 KM_SLEEP);
7068 topo_node->mpt = mpt;
7069 topo_node->event =
7070 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7071 if (expd_handle == 0) {
7073 * Per MPI 2, if expander dev handle
7074 * is 0, it's a directly attached
7075 * device. So driver use PHY to decide
7076 * which iport is associated
7078 physport = phy;
7079 mpt->m_port_chng = 1;
7081 topo_node->un.physport = physport;
7082 topo_node->devhdl = dev_handle;
7083 topo_node->flags = flags;
7084 topo_node->object = NULL;
7085 if (topo_head == NULL) {
7086 topo_head = topo_tail = topo_node;
7087 } else {
7088 topo_tail->next = topo_node;
7089 topo_tail = topo_node;
7091 break;
7093 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
7095 NDBG20(("mptsas%d phy %d physical_port %d "
7096 "dev_handle %d removed", mpt->m_instance,
7097 phy, physport, dev_handle));
7099 * Set association flag according to if an
7100 * expander is used or not.
7102 exp_flag =
7103 MPTSAS_TOPO_FLAG_EXPANDER_ATTACHED_DEVICE;
7104 if (flags ==
7105 MPTSAS_TOPO_FLAG_EXPANDER_ASSOCIATED) {
7106 flags = exp_flag;
7109 * Target device is removed from the system
7110 * Before the device is really offline from
7111 * from system.
7113 ptgt = refhash_linear_search(mpt->m_targets,
7114 mptsas_target_eval_devhdl, &dev_handle);
7116 * If ptgt is NULL here, it means that the
7117 * DevHandle is not in the hash table. This is
7118 * reasonable sometimes. For example, if a
7119 * disk was pulled, then added, then pulled
7120 * again, the disk will not have been put into
7121 * the hash table because the add event will
7122 * have an invalid phymask. BUT, this does not
7123 * mean that the DevHandle is invalid. The
7124 * controller will still have a valid DevHandle
7125 * that must be removed. To do this, use the
7126 * MPTSAS_TOPO_FLAG_REMOVE_HANDLE event.
7128 if (ptgt == NULL) {
7129 topo_node = kmem_zalloc(
7130 sizeof (mptsas_topo_change_list_t),
7131 KM_SLEEP);
7132 topo_node->mpt = mpt;
7133 topo_node->un.phymask = 0;
7134 topo_node->event =
7135 MPTSAS_TOPO_FLAG_REMOVE_HANDLE;
7136 topo_node->devhdl = dev_handle;
7137 topo_node->flags = flags;
7138 topo_node->object = NULL;
7139 if (topo_head == NULL) {
7140 topo_head = topo_tail =
7141 topo_node;
7142 } else {
7143 topo_tail->next = topo_node;
7144 topo_tail = topo_node;
7146 break;
7150 * Update DR flag immediately avoid I/O failure
7151 * before failover finish. Pay attention to the
7152 * mutex protect, we need grab m_tx_waitq_mutex
7153 * during set m_dr_flag because we won't add
7154 * the following command into waitq, instead,
7155 * we need return TRAN_BUSY in the tran_start
7156 * context.
7158 mutex_enter(&mpt->m_tx_waitq_mutex);
7159 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7160 mutex_exit(&mpt->m_tx_waitq_mutex);
7162 topo_node = kmem_zalloc(
7163 sizeof (mptsas_topo_change_list_t),
7164 KM_SLEEP);
7165 topo_node->mpt = mpt;
7166 topo_node->un.phymask =
7167 ptgt->m_addr.mta_phymask;
7168 topo_node->event =
7169 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7170 topo_node->devhdl = dev_handle;
7171 topo_node->flags = flags;
7172 topo_node->object = NULL;
7173 if (topo_head == NULL) {
7174 topo_head = topo_tail = topo_node;
7175 } else {
7176 topo_tail->next = topo_node;
7177 topo_tail = topo_node;
7179 break;
7181 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
7182 link_rate = ddi_get8(mpt->m_acc_reply_frame_hdl,
7183 &sas_topo_change_list->PHY[i].LinkRate);
7184 state = (link_rate &
7185 MPI2_EVENT_SAS_TOPO_LR_CURRENT_MASK) >>
7186 MPI2_EVENT_SAS_TOPO_LR_CURRENT_SHIFT;
7187 pSmhba = &mpt->m_phy_info[i].smhba_info;
7188 pSmhba->negotiated_link_rate = state;
7189 switch (state) {
7190 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7191 (void) sprintf(curr, "is disabled");
7192 mptsas_smhba_log_sysevent(mpt,
7193 ESC_SAS_PHY_EVENT,
7194 SAS_PHY_REMOVE,
7195 &mpt->m_phy_info[i].smhba_info);
7196 mpt->m_phy_info[i].smhba_info.
7197 negotiated_link_rate
7198 = 0x1;
7199 break;
7200 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7201 (void) sprintf(curr, "is offline, "
7202 "failed speed negotiation");
7203 mptsas_smhba_log_sysevent(mpt,
7204 ESC_SAS_PHY_EVENT,
7205 SAS_PHY_OFFLINE,
7206 &mpt->m_phy_info[i].smhba_info);
7207 break;
7208 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7209 (void) sprintf(curr, "SATA OOB "
7210 "complete");
7211 break;
7212 case SMP_RESET_IN_PROGRESS:
7213 (void) sprintf(curr, "SMP reset in "
7214 "progress");
7215 break;
7216 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7217 (void) sprintf(curr, "is online at "
7218 "1.5 Gbps");
7219 if ((expd_handle == 0) &&
7220 (enc_handle == 1)) {
7221 mpt->m_port_chng = 1;
7223 mptsas_smhba_log_sysevent(mpt,
7224 ESC_SAS_PHY_EVENT,
7225 SAS_PHY_ONLINE,
7226 &mpt->m_phy_info[i].smhba_info);
7227 break;
7228 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7229 (void) sprintf(curr, "is online at 3.0 "
7230 "Gbps");
7231 if ((expd_handle == 0) &&
7232 (enc_handle == 1)) {
7233 mpt->m_port_chng = 1;
7235 mptsas_smhba_log_sysevent(mpt,
7236 ESC_SAS_PHY_EVENT,
7237 SAS_PHY_ONLINE,
7238 &mpt->m_phy_info[i].smhba_info);
7239 break;
7240 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7241 (void) sprintf(curr, "is online at "
7242 "6.0 Gbps");
7243 if ((expd_handle == 0) &&
7244 (enc_handle == 1)) {
7245 mpt->m_port_chng = 1;
7247 mptsas_smhba_log_sysevent(mpt,
7248 ESC_SAS_PHY_EVENT,
7249 SAS_PHY_ONLINE,
7250 &mpt->m_phy_info[i].smhba_info);
7251 break;
7252 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7253 (void) sprintf(curr, "is online at "
7254 "12.0 Gbps");
7255 if ((expd_handle == 0) &&
7256 (enc_handle == 1)) {
7257 mpt->m_port_chng = 1;
7259 mptsas_smhba_log_sysevent(mpt,
7260 ESC_SAS_PHY_EVENT,
7261 SAS_PHY_ONLINE,
7262 &mpt->m_phy_info[i].smhba_info);
7263 break;
7264 default:
7265 (void) sprintf(curr, "state is "
7266 "unknown");
7267 break;
7270 state = (link_rate &
7271 MPI2_EVENT_SAS_TOPO_LR_PREV_MASK) >>
7272 MPI2_EVENT_SAS_TOPO_LR_PREV_SHIFT;
7273 switch (state) {
7274 case MPI2_EVENT_SAS_TOPO_LR_PHY_DISABLED:
7275 (void) sprintf(prev, ", was disabled");
7276 break;
7277 case MPI2_EVENT_SAS_TOPO_LR_NEGOTIATION_FAILED:
7278 (void) sprintf(prev, ", was offline, "
7279 "failed speed negotiation");
7280 break;
7281 case MPI2_EVENT_SAS_TOPO_LR_SATA_OOB_COMPLETE:
7282 (void) sprintf(prev, ", was SATA OOB "
7283 "complete");
7284 break;
7285 case SMP_RESET_IN_PROGRESS:
7286 (void) sprintf(prev, ", was SMP reset "
7287 "in progress");
7288 break;
7289 case MPI2_EVENT_SAS_TOPO_LR_RATE_1_5:
7290 (void) sprintf(prev, ", was online at "
7291 "1.5 Gbps");
7292 break;
7293 case MPI2_EVENT_SAS_TOPO_LR_RATE_3_0:
7294 (void) sprintf(prev, ", was online at "
7295 "3.0 Gbps");
7296 break;
7297 case MPI2_EVENT_SAS_TOPO_LR_RATE_6_0:
7298 (void) sprintf(prev, ", was online at "
7299 "6.0 Gbps");
7300 break;
7301 case MPI25_EVENT_SAS_TOPO_LR_RATE_12_0:
7302 (void) sprintf(prev, ", was online at "
7303 "12.0 Gbps");
7304 break;
7305 default:
7306 break;
7308 (void) sprintf(&string[strlen(string)], "link "
7309 "changed, ");
7310 break;
7311 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
7312 continue;
7313 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
7314 (void) sprintf(&string[strlen(string)],
7315 "target not responding, delaying "
7316 "removal");
7317 break;
7319 NDBG20(("mptsas%d phy %d DevHandle %x, %s%s%s\n",
7320 mpt->m_instance, phy, dev_handle, string, curr,
7321 prev));
7323 if (topo_head != NULL) {
7325 * Launch DR taskq to handle topology change
7327 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7328 mptsas_handle_dr, (void *)topo_head,
7329 DDI_NOSLEEP)) != DDI_SUCCESS) {
7330 while (topo_head != NULL) {
7331 topo_node = topo_head;
7332 topo_head = topo_head->next;
7333 kmem_free(topo_node,
7334 sizeof (mptsas_topo_change_list_t));
7336 mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7337 "for handle SAS DR event failed. \n");
7340 break;
7342 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7344 Mpi2EventDataIrConfigChangeList_t *irChangeList;
7345 mptsas_topo_change_list_t *topo_head = NULL;
7346 mptsas_topo_change_list_t *topo_tail = NULL;
7347 mptsas_topo_change_list_t *topo_node = NULL;
7348 mptsas_target_t *ptgt;
7349 uint8_t num_entries, i, reason;
7350 uint16_t volhandle, diskhandle;
7352 irChangeList = (pMpi2EventDataIrConfigChangeList_t)
7353 eventreply->EventData;
7354 num_entries = ddi_get8(mpt->m_acc_reply_frame_hdl,
7355 &irChangeList->NumElements);
7357 NDBG20(("mptsas%d IR_CONFIGURATION_CHANGE_LIST event received",
7358 mpt->m_instance));
7360 for (i = 0; i < num_entries; i++) {
7361 reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
7362 &irChangeList->ConfigElement[i].ReasonCode);
7363 volhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7364 &irChangeList->ConfigElement[i].VolDevHandle);
7365 diskhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7366 &irChangeList->ConfigElement[i].PhysDiskDevHandle);
7368 switch (reason) {
7369 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
7370 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
7372 NDBG20(("mptsas %d volume added\n",
7373 mpt->m_instance));
7375 topo_node = kmem_zalloc(
7376 sizeof (mptsas_topo_change_list_t),
7377 KM_SLEEP);
7379 topo_node->mpt = mpt;
7380 topo_node->event =
7381 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7382 topo_node->un.physport = 0xff;
7383 topo_node->devhdl = volhandle;
7384 topo_node->flags =
7385 MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7386 topo_node->object = NULL;
7387 if (topo_head == NULL) {
7388 topo_head = topo_tail = topo_node;
7389 } else {
7390 topo_tail->next = topo_node;
7391 topo_tail = topo_node;
7393 break;
7395 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
7396 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
7398 NDBG20(("mptsas %d volume deleted\n",
7399 mpt->m_instance));
7400 ptgt = refhash_linear_search(mpt->m_targets,
7401 mptsas_target_eval_devhdl, &volhandle);
7402 if (ptgt == NULL)
7403 break;
7406 * Clear any flags related to volume
7408 (void) mptsas_delete_volume(mpt, volhandle);
7411 * Update DR flag immediately avoid I/O failure
7413 mutex_enter(&mpt->m_tx_waitq_mutex);
7414 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7415 mutex_exit(&mpt->m_tx_waitq_mutex);
7417 topo_node = kmem_zalloc(
7418 sizeof (mptsas_topo_change_list_t),
7419 KM_SLEEP);
7420 topo_node->mpt = mpt;
7421 topo_node->un.phymask =
7422 ptgt->m_addr.mta_phymask;
7423 topo_node->event =
7424 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7425 topo_node->devhdl = volhandle;
7426 topo_node->flags =
7427 MPTSAS_TOPO_FLAG_RAID_ASSOCIATED;
7428 topo_node->object = (void *)ptgt;
7429 if (topo_head == NULL) {
7430 topo_head = topo_tail = topo_node;
7431 } else {
7432 topo_tail->next = topo_node;
7433 topo_tail = topo_node;
7435 break;
7437 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
7438 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
7440 ptgt = refhash_linear_search(mpt->m_targets,
7441 mptsas_target_eval_devhdl, &diskhandle);
7442 if (ptgt == NULL)
7443 break;
7446 * Update DR flag immediately avoid I/O failure
7448 mutex_enter(&mpt->m_tx_waitq_mutex);
7449 ptgt->m_dr_flag = MPTSAS_DR_INTRANSITION;
7450 mutex_exit(&mpt->m_tx_waitq_mutex);
7452 topo_node = kmem_zalloc(
7453 sizeof (mptsas_topo_change_list_t),
7454 KM_SLEEP);
7455 topo_node->mpt = mpt;
7456 topo_node->un.phymask =
7457 ptgt->m_addr.mta_phymask;
7458 topo_node->event =
7459 MPTSAS_DR_EVENT_OFFLINE_TARGET;
7460 topo_node->devhdl = diskhandle;
7461 topo_node->flags =
7462 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7463 topo_node->object = (void *)ptgt;
7464 if (topo_head == NULL) {
7465 topo_head = topo_tail = topo_node;
7466 } else {
7467 topo_tail->next = topo_node;
7468 topo_tail = topo_node;
7470 break;
7472 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
7473 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
7476 * The physical drive is released by a IR
7477 * volume. But we cannot get the the physport
7478 * or phynum from the event data, so we only
7479 * can get the physport/phynum after SAS
7480 * Device Page0 request for the devhdl.
7482 topo_node = kmem_zalloc(
7483 sizeof (mptsas_topo_change_list_t),
7484 KM_SLEEP);
7485 topo_node->mpt = mpt;
7486 topo_node->un.phymask = 0;
7487 topo_node->event =
7488 MPTSAS_DR_EVENT_RECONFIG_TARGET;
7489 topo_node->devhdl = diskhandle;
7490 topo_node->flags =
7491 MPTSAS_TOPO_FLAG_RAID_PHYSDRV_ASSOCIATED;
7492 topo_node->object = NULL;
7493 mpt->m_port_chng = 1;
7494 if (topo_head == NULL) {
7495 topo_head = topo_tail = topo_node;
7496 } else {
7497 topo_tail->next = topo_node;
7498 topo_tail = topo_node;
7500 break;
7502 default:
7503 break;
7507 if (topo_head != NULL) {
7509 * Launch DR taskq to handle topology change
7511 if ((ddi_taskq_dispatch(mpt->m_dr_taskq,
7512 mptsas_handle_dr, (void *)topo_head,
7513 DDI_NOSLEEP)) != DDI_SUCCESS) {
7514 while (topo_head != NULL) {
7515 topo_node = topo_head;
7516 topo_head = topo_head->next;
7517 kmem_free(topo_node,
7518 sizeof (mptsas_topo_change_list_t));
7520 mptsas_log(mpt, CE_NOTE, "mptsas start taskq "
7521 "for handle SAS DR event failed. \n");
7524 break;
7526 default:
7527 return (DDI_FAILURE);
7530 return (DDI_SUCCESS);
7534 * handle events from ioc
7536 static void
7537 mptsas_handle_event(void *args)
7539 m_replyh_arg_t *replyh_arg;
7540 pMpi2EventNotificationReply_t eventreply;
7541 uint32_t event, iocloginfo, rfm;
7542 uint32_t status;
7543 uint8_t port;
7544 mptsas_t *mpt;
7545 uint_t iocstatus;
7547 replyh_arg = (m_replyh_arg_t *)args;
7548 rfm = replyh_arg->rfm;
7549 mpt = replyh_arg->mpt;
7551 mutex_enter(&mpt->m_mutex);
7553 * If HBA is being reset, drop incoming event.
7555 if (mpt->m_in_reset) {
7556 NDBG20(("dropping event received prior to reset"));
7557 mutex_exit(&mpt->m_mutex);
7558 return;
7561 eventreply = (pMpi2EventNotificationReply_t)
7562 (mpt->m_reply_frame + (rfm -
7563 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
7564 event = ddi_get16(mpt->m_acc_reply_frame_hdl, &eventreply->Event);
7566 if (iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
7567 &eventreply->IOCStatus)) {
7568 if (iocstatus == MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
7569 mptsas_log(mpt, CE_WARN,
7570 "!mptsas_handle_event: IOCStatus=0x%x, "
7571 "IOCLogInfo=0x%x", iocstatus,
7572 ddi_get32(mpt->m_acc_reply_frame_hdl,
7573 &eventreply->IOCLogInfo));
7574 } else {
7575 mptsas_log(mpt, CE_WARN,
7576 "mptsas_handle_event: IOCStatus=0x%x, "
7577 "IOCLogInfo=0x%x", iocstatus,
7578 ddi_get32(mpt->m_acc_reply_frame_hdl,
7579 &eventreply->IOCLogInfo));
7584 * figure out what kind of event we got and handle accordingly
7586 switch (event) {
7587 case MPI2_EVENT_LOG_ENTRY_ADDED:
7588 break;
7589 case MPI2_EVENT_LOG_DATA:
7590 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7591 &eventreply->IOCLogInfo);
7592 NDBG20(("mptsas %d log info %x received.\n", mpt->m_instance,
7593 iocloginfo));
7594 break;
7595 case MPI2_EVENT_STATE_CHANGE:
7596 NDBG20(("mptsas%d state change.", mpt->m_instance));
7597 break;
7598 case MPI2_EVENT_HARD_RESET_RECEIVED:
7599 NDBG20(("mptsas%d event change.", mpt->m_instance));
7600 break;
7601 case MPI2_EVENT_SAS_DISCOVERY:
7603 MPI2_EVENT_DATA_SAS_DISCOVERY *sasdiscovery;
7604 char string[80];
7605 uint8_t rc;
7607 sasdiscovery =
7608 (pMpi2EventDataSasDiscovery_t)eventreply->EventData;
7610 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7611 &sasdiscovery->ReasonCode);
7612 port = ddi_get8(mpt->m_acc_reply_frame_hdl,
7613 &sasdiscovery->PhysicalPort);
7614 status = ddi_get32(mpt->m_acc_reply_frame_hdl,
7615 &sasdiscovery->DiscoveryStatus);
7617 string[0] = 0;
7618 switch (rc) {
7619 case MPI2_EVENT_SAS_DISC_RC_STARTED:
7620 (void) sprintf(string, "STARTING");
7621 break;
7622 case MPI2_EVENT_SAS_DISC_RC_COMPLETED:
7623 (void) sprintf(string, "COMPLETED");
7624 break;
7625 default:
7626 (void) sprintf(string, "UNKNOWN");
7627 break;
7630 NDBG20(("SAS DISCOVERY is %s for port %d, status %x", string,
7631 port, status));
7633 break;
7635 case MPI2_EVENT_EVENT_CHANGE:
7636 NDBG20(("mptsas%d event change.", mpt->m_instance));
7637 break;
7638 case MPI2_EVENT_TASK_SET_FULL:
7640 pMpi2EventDataTaskSetFull_t taskfull;
7642 taskfull = (pMpi2EventDataTaskSetFull_t)eventreply->EventData;
7644 NDBG20(("TASK_SET_FULL received for mptsas%d, depth %d\n",
7645 mpt->m_instance, ddi_get16(mpt->m_acc_reply_frame_hdl,
7646 &taskfull->CurrentDepth)));
7647 break;
7649 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7652 * SAS TOPOLOGY CHANGE LIST Event has already been handled
7653 * in mptsas_handle_event_sync() of interrupt context
7655 break;
7657 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7659 pMpi2EventDataSasEnclDevStatusChange_t encstatus;
7660 uint8_t rc;
7661 char string[80];
7663 encstatus = (pMpi2EventDataSasEnclDevStatusChange_t)
7664 eventreply->EventData;
7666 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7667 &encstatus->ReasonCode);
7668 switch (rc) {
7669 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
7670 (void) sprintf(string, "added");
7671 break;
7672 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
7673 (void) sprintf(string, ", not responding");
7674 break;
7675 default:
7676 break;
7678 NDBG20(("mptsas%d ENCLOSURE STATUS CHANGE for enclosure "
7679 "%x%s\n", mpt->m_instance,
7680 ddi_get16(mpt->m_acc_reply_frame_hdl,
7681 &encstatus->EnclosureHandle), string));
7682 break;
7686 * MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE is handled by
7687 * mptsas_handle_event_sync,in here just send ack message.
7689 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7691 pMpi2EventDataSasDeviceStatusChange_t statuschange;
7692 uint8_t rc;
7693 uint16_t devhdl;
7694 uint64_t wwn = 0;
7695 uint32_t wwn_lo, wwn_hi;
7697 statuschange = (pMpi2EventDataSasDeviceStatusChange_t)
7698 eventreply->EventData;
7699 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7700 &statuschange->ReasonCode);
7701 wwn_lo = ddi_get32(mpt->m_acc_reply_frame_hdl,
7702 (uint32_t *)(void *)&statuschange->SASAddress);
7703 wwn_hi = ddi_get32(mpt->m_acc_reply_frame_hdl,
7704 (uint32_t *)(void *)&statuschange->SASAddress + 1);
7705 wwn = ((uint64_t)wwn_hi << 32) | wwn_lo;
7706 devhdl = ddi_get16(mpt->m_acc_reply_frame_hdl,
7707 &statuschange->DevHandle);
7709 NDBG13(("MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE wwn is %"PRIx64,
7710 wwn));
7712 switch (rc) {
7713 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
7714 NDBG20(("SMART data received, ASC/ASCQ = %02x/%02x",
7715 ddi_get8(mpt->m_acc_reply_frame_hdl,
7716 &statuschange->ASC),
7717 ddi_get8(mpt->m_acc_reply_frame_hdl,
7718 &statuschange->ASCQ)));
7719 break;
7721 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
7722 NDBG20(("Device not supported"));
7723 break;
7725 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
7726 NDBG20(("IOC internally generated the Target Reset "
7727 "for devhdl:%x", devhdl));
7728 break;
7730 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
7731 NDBG20(("IOC's internally generated Target Reset "
7732 "completed for devhdl:%x", devhdl));
7733 break;
7735 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
7736 NDBG20(("IOC internally generated Abort Task"));
7737 break;
7739 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
7740 NDBG20(("IOC's internally generated Abort Task "
7741 "completed"));
7742 break;
7744 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
7745 NDBG20(("IOC internally generated Abort Task Set"));
7746 break;
7748 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
7749 NDBG20(("IOC internally generated Clear Task Set"));
7750 break;
7752 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
7753 NDBG20(("IOC internally generated Query Task"));
7754 break;
7756 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
7757 NDBG20(("Device sent an Asynchronous Notification"));
7758 break;
7760 default:
7761 break;
7763 break;
7765 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7768 * IR TOPOLOGY CHANGE LIST Event has already been handled
7769 * in mpt_handle_event_sync() of interrupt context
7771 break;
7773 case MPI2_EVENT_IR_OPERATION_STATUS:
7775 Mpi2EventDataIrOperationStatus_t *irOpStatus;
7776 char reason_str[80];
7777 uint8_t rc, percent;
7778 uint16_t handle;
7780 irOpStatus = (pMpi2EventDataIrOperationStatus_t)
7781 eventreply->EventData;
7782 rc = ddi_get8(mpt->m_acc_reply_frame_hdl,
7783 &irOpStatus->RAIDOperation);
7784 percent = ddi_get8(mpt->m_acc_reply_frame_hdl,
7785 &irOpStatus->PercentComplete);
7786 handle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7787 &irOpStatus->VolDevHandle);
7789 switch (rc) {
7790 case MPI2_EVENT_IR_RAIDOP_RESYNC:
7791 (void) sprintf(reason_str, "resync");
7792 break;
7793 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
7794 (void) sprintf(reason_str, "online capacity "
7795 "expansion");
7796 break;
7797 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
7798 (void) sprintf(reason_str, "consistency check");
7799 break;
7800 default:
7801 (void) sprintf(reason_str, "unknown reason %x",
7802 rc);
7805 NDBG20(("mptsas%d raid operational status: (%s)"
7806 "\thandle(0x%04x), percent complete(%d)\n",
7807 mpt->m_instance, reason_str, handle, percent));
7808 break;
7810 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7812 pMpi2EventDataSasBroadcastPrimitive_t sas_broadcast;
7813 uint8_t phy_num;
7814 uint8_t primitive;
7816 sas_broadcast = (pMpi2EventDataSasBroadcastPrimitive_t)
7817 eventreply->EventData;
7819 phy_num = ddi_get8(mpt->m_acc_reply_frame_hdl,
7820 &sas_broadcast->PhyNum);
7821 primitive = ddi_get8(mpt->m_acc_reply_frame_hdl,
7822 &sas_broadcast->Primitive);
7824 switch (primitive) {
7825 case MPI2_EVENT_PRIMITIVE_CHANGE:
7826 mptsas_smhba_log_sysevent(mpt,
7827 ESC_SAS_HBA_PORT_BROADCAST,
7828 SAS_PORT_BROADCAST_CHANGE,
7829 &mpt->m_phy_info[phy_num].smhba_info);
7830 break;
7831 case MPI2_EVENT_PRIMITIVE_SES:
7832 mptsas_smhba_log_sysevent(mpt,
7833 ESC_SAS_HBA_PORT_BROADCAST,
7834 SAS_PORT_BROADCAST_SES,
7835 &mpt->m_phy_info[phy_num].smhba_info);
7836 break;
7837 case MPI2_EVENT_PRIMITIVE_EXPANDER:
7838 mptsas_smhba_log_sysevent(mpt,
7839 ESC_SAS_HBA_PORT_BROADCAST,
7840 SAS_PORT_BROADCAST_D01_4,
7841 &mpt->m_phy_info[phy_num].smhba_info);
7842 break;
7843 case MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT:
7844 mptsas_smhba_log_sysevent(mpt,
7845 ESC_SAS_HBA_PORT_BROADCAST,
7846 SAS_PORT_BROADCAST_D04_7,
7847 &mpt->m_phy_info[phy_num].smhba_info);
7848 break;
7849 case MPI2_EVENT_PRIMITIVE_RESERVED3:
7850 mptsas_smhba_log_sysevent(mpt,
7851 ESC_SAS_HBA_PORT_BROADCAST,
7852 SAS_PORT_BROADCAST_D16_7,
7853 &mpt->m_phy_info[phy_num].smhba_info);
7854 break;
7855 case MPI2_EVENT_PRIMITIVE_RESERVED4:
7856 mptsas_smhba_log_sysevent(mpt,
7857 ESC_SAS_HBA_PORT_BROADCAST,
7858 SAS_PORT_BROADCAST_D29_7,
7859 &mpt->m_phy_info[phy_num].smhba_info);
7860 break;
7861 case MPI2_EVENT_PRIMITIVE_CHANGE0_RESERVED:
7862 mptsas_smhba_log_sysevent(mpt,
7863 ESC_SAS_HBA_PORT_BROADCAST,
7864 SAS_PORT_BROADCAST_D24_0,
7865 &mpt->m_phy_info[phy_num].smhba_info);
7866 break;
7867 case MPI2_EVENT_PRIMITIVE_CHANGE1_RESERVED:
7868 mptsas_smhba_log_sysevent(mpt,
7869 ESC_SAS_HBA_PORT_BROADCAST,
7870 SAS_PORT_BROADCAST_D27_4,
7871 &mpt->m_phy_info[phy_num].smhba_info);
7872 break;
7873 default:
7874 NDBG16(("mptsas%d: unknown BROADCAST PRIMITIVE"
7875 " %x received",
7876 mpt->m_instance, primitive));
7877 break;
7879 NDBG16(("mptsas%d sas broadcast primitive: "
7880 "\tprimitive(0x%04x), phy(%d) complete\n",
7881 mpt->m_instance, primitive, phy_num));
7882 break;
7884 case MPI2_EVENT_IR_VOLUME:
7886 Mpi2EventDataIrVolume_t *irVolume;
7887 uint16_t devhandle;
7888 uint32_t state;
7889 int config, vol;
7890 uint8_t found = FALSE;
7892 irVolume = (pMpi2EventDataIrVolume_t)eventreply->EventData;
7893 state = ddi_get32(mpt->m_acc_reply_frame_hdl,
7894 &irVolume->NewValue);
7895 devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
7896 &irVolume->VolDevHandle);
7898 NDBG20(("EVENT_IR_VOLUME event is received"));
7901 * Get latest RAID info and then find the DevHandle for this
7902 * event in the configuration. If the DevHandle is not found
7903 * just exit the event.
7905 (void) mptsas_get_raid_info(mpt);
7906 for (config = 0; (config < mpt->m_num_raid_configs) &&
7907 (!found); config++) {
7908 for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
7909 if (mpt->m_raidconfig[config].m_raidvol[vol].
7910 m_raidhandle == devhandle) {
7911 found = TRUE;
7912 break;
7916 if (!found) {
7917 break;
7920 switch (irVolume->ReasonCode) {
7921 case MPI2_EVENT_IR_VOLUME_RC_SETTINGS_CHANGED:
7923 uint32_t i;
7924 mpt->m_raidconfig[config].m_raidvol[vol].m_settings =
7925 state;
7927 i = state & MPI2_RAIDVOL0_SETTING_MASK_WRITE_CACHING;
7928 mptsas_log(mpt, CE_NOTE, " Volume %d settings changed"
7929 ", auto-config of hot-swap drives is %s"
7930 ", write caching is %s"
7931 ", hot-spare pool mask is %02x\n",
7932 vol, state &
7933 MPI2_RAIDVOL0_SETTING_AUTO_CONFIG_HSWAP_DISABLE
7934 ? "disabled" : "enabled",
7935 i == MPI2_RAIDVOL0_SETTING_UNCHANGED
7936 ? "controlled by member disks" :
7937 i == MPI2_RAIDVOL0_SETTING_DISABLE_WRITE_CACHING
7938 ? "disabled" :
7939 i == MPI2_RAIDVOL0_SETTING_ENABLE_WRITE_CACHING
7940 ? "enabled" :
7941 "incorrectly set",
7942 (state >> 16) & 0xff);
7943 break;
7945 case MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED:
7947 mpt->m_raidconfig[config].m_raidvol[vol].m_state =
7948 (uint8_t)state;
7950 mptsas_log(mpt, CE_NOTE,
7951 "Volume %d is now %s\n", vol,
7952 state == MPI2_RAID_VOL_STATE_OPTIMAL
7953 ? "optimal" :
7954 state == MPI2_RAID_VOL_STATE_DEGRADED
7955 ? "degraded" :
7956 state == MPI2_RAID_VOL_STATE_ONLINE
7957 ? "online" :
7958 state == MPI2_RAID_VOL_STATE_INITIALIZING
7959 ? "initializing" :
7960 state == MPI2_RAID_VOL_STATE_FAILED
7961 ? "failed" :
7962 state == MPI2_RAID_VOL_STATE_MISSING
7963 ? "missing" :
7964 "state unknown");
7965 break;
7967 case MPI2_EVENT_IR_VOLUME_RC_STATUS_FLAGS_CHANGED:
7969 mpt->m_raidconfig[config].m_raidvol[vol].
7970 m_statusflags = state;
7972 mptsas_log(mpt, CE_NOTE,
7973 " Volume %d is now %s%s%s%s%s%s%s%s%s\n",
7974 vol,
7975 state & MPI2_RAIDVOL0_STATUS_FLAG_ENABLED
7976 ? ", enabled" : ", disabled",
7977 state & MPI2_RAIDVOL0_STATUS_FLAG_QUIESCED
7978 ? ", quiesced" : "",
7979 state & MPI2_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE
7980 ? ", inactive" : ", active",
7981 state &
7982 MPI2_RAIDVOL0_STATUS_FLAG_BAD_BLOCK_TABLE_FULL
7983 ? ", bad block table is full" : "",
7984 state &
7985 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS
7986 ? ", resync in progress" : "",
7987 state & MPI2_RAIDVOL0_STATUS_FLAG_BACKGROUND_INIT
7988 ? ", background initialization in progress" : "",
7989 state &
7990 MPI2_RAIDVOL0_STATUS_FLAG_CAPACITY_EXPANSION
7991 ? ", capacity expansion in progress" : "",
7992 state &
7993 MPI2_RAIDVOL0_STATUS_FLAG_CONSISTENCY_CHECK
7994 ? ", consistency check in progress" : "",
7995 state & MPI2_RAIDVOL0_STATUS_FLAG_DATA_SCRUB
7996 ? ", data scrub in progress" : "");
7997 break;
7999 default:
8000 break;
8002 break;
8004 case MPI2_EVENT_IR_PHYSICAL_DISK:
8006 Mpi2EventDataIrPhysicalDisk_t *irPhysDisk;
8007 uint16_t devhandle, enchandle, slot;
8008 uint32_t status, state;
8009 uint8_t physdisknum, reason;
8011 irPhysDisk = (Mpi2EventDataIrPhysicalDisk_t *)
8012 eventreply->EventData;
8013 physdisknum = ddi_get8(mpt->m_acc_reply_frame_hdl,
8014 &irPhysDisk->PhysDiskNum);
8015 devhandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
8016 &irPhysDisk->PhysDiskDevHandle);
8017 enchandle = ddi_get16(mpt->m_acc_reply_frame_hdl,
8018 &irPhysDisk->EnclosureHandle);
8019 slot = ddi_get16(mpt->m_acc_reply_frame_hdl,
8020 &irPhysDisk->Slot);
8021 state = ddi_get32(mpt->m_acc_reply_frame_hdl,
8022 &irPhysDisk->NewValue);
8023 reason = ddi_get8(mpt->m_acc_reply_frame_hdl,
8024 &irPhysDisk->ReasonCode);
8026 NDBG20(("EVENT_IR_PHYSICAL_DISK event is received"));
8028 switch (reason) {
8029 case MPI2_EVENT_IR_PHYSDISK_RC_SETTINGS_CHANGED:
8030 mptsas_log(mpt, CE_NOTE,
8031 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8032 "for enclosure with handle 0x%x is now in hot "
8033 "spare pool %d",
8034 physdisknum, devhandle, slot, enchandle,
8035 (state >> 16) & 0xff);
8036 break;
8038 case MPI2_EVENT_IR_PHYSDISK_RC_STATUS_FLAGS_CHANGED:
8039 status = state;
8040 mptsas_log(mpt, CE_NOTE,
8041 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8042 "for enclosure with handle 0x%x is now "
8043 "%s%s%s%s%s\n", physdisknum, devhandle, slot,
8044 enchandle,
8045 status & MPI2_PHYSDISK0_STATUS_FLAG_INACTIVE_VOLUME
8046 ? ", inactive" : ", active",
8047 status & MPI2_PHYSDISK0_STATUS_FLAG_OUT_OF_SYNC
8048 ? ", out of sync" : "",
8049 status & MPI2_PHYSDISK0_STATUS_FLAG_QUIESCED
8050 ? ", quiesced" : "",
8051 status &
8052 MPI2_PHYSDISK0_STATUS_FLAG_WRITE_CACHE_ENABLED
8053 ? ", write cache enabled" : "",
8054 status & MPI2_PHYSDISK0_STATUS_FLAG_OCE_TARGET
8055 ? ", capacity expansion target" : "");
8056 break;
8058 case MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED:
8059 mptsas_log(mpt, CE_NOTE,
8060 " PhysDiskNum %d with DevHandle 0x%x in slot %d "
8061 "for enclosure with handle 0x%x is now %s\n",
8062 physdisknum, devhandle, slot, enchandle,
8063 state == MPI2_RAID_PD_STATE_OPTIMAL
8064 ? "optimal" :
8065 state == MPI2_RAID_PD_STATE_REBUILDING
8066 ? "rebuilding" :
8067 state == MPI2_RAID_PD_STATE_DEGRADED
8068 ? "degraded" :
8069 state == MPI2_RAID_PD_STATE_HOT_SPARE
8070 ? "a hot spare" :
8071 state == MPI2_RAID_PD_STATE_ONLINE
8072 ? "online" :
8073 state == MPI2_RAID_PD_STATE_OFFLINE
8074 ? "offline" :
8075 state == MPI2_RAID_PD_STATE_NOT_COMPATIBLE
8076 ? "not compatible" :
8077 state == MPI2_RAID_PD_STATE_NOT_CONFIGURED
8078 ? "not configured" :
8079 "state unknown");
8080 break;
8082 break;
8084 default:
8085 NDBG20(("mptsas%d: unknown event %x received",
8086 mpt->m_instance, event));
8087 break;
8091 * Return the reply frame to the free queue.
8093 ddi_put32(mpt->m_acc_free_queue_hdl,
8094 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index], rfm);
8095 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
8096 DDI_DMA_SYNC_FORDEV);
8097 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
8098 mpt->m_free_index = 0;
8100 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
8101 mpt->m_free_index);
8102 mutex_exit(&mpt->m_mutex);
8106 * invoked from timeout() to restart qfull cmds with throttle == 0
8108 static void
8109 mptsas_restart_cmd(void *arg)
8111 mptsas_t *mpt = arg;
8112 mptsas_target_t *ptgt = NULL;
8114 mutex_enter(&mpt->m_mutex);
8116 mpt->m_restart_cmd_timeid = 0;
8118 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
8119 ptgt = refhash_next(mpt->m_targets, ptgt)) {
8120 if (ptgt->m_reset_delay == 0) {
8121 if (ptgt->m_t_throttle == QFULL_THROTTLE) {
8122 mptsas_set_throttle(mpt, ptgt,
8123 MAX_THROTTLE);
8127 mptsas_restart_hba(mpt);
8128 mutex_exit(&mpt->m_mutex);
8131 void
8132 mptsas_remove_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
8134 int slot;
8135 mptsas_slots_t *slots = mpt->m_active;
8136 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8138 ASSERT(cmd != NULL);
8139 ASSERT(cmd->cmd_queued == FALSE);
8142 * Task Management cmds are removed in their own routines. Also,
8143 * we don't want to modify timeout based on TM cmds.
8145 if (cmd->cmd_flags & CFLAG_TM_CMD) {
8146 return;
8149 slot = cmd->cmd_slot;
8152 * remove the cmd.
8154 if (cmd == slots->m_slot[slot]) {
8155 NDBG31(("mptsas_remove_cmd: removing cmd=0x%p, flags "
8156 "0x%x", (void *)cmd, cmd->cmd_flags));
8157 slots->m_slot[slot] = NULL;
8158 mpt->m_ncmds--;
8161 * only decrement per target ncmds if command
8162 * has a target associated with it.
8164 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
8165 ptgt->m_t_ncmds--;
8167 * reset throttle if we just ran an untagged command
8168 * to a tagged target
8170 if ((ptgt->m_t_ncmds == 0) &&
8171 ((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0)) {
8172 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8176 * Remove this command from the active queue.
8178 if (cmd->cmd_active_expiration != 0) {
8179 TAILQ_REMOVE(&ptgt->m_active_cmdq, cmd,
8180 cmd_active_link);
8181 cmd->cmd_active_expiration = 0;
8187 * This is all we need to do for ioc commands.
8189 if (cmd->cmd_flags & CFLAG_CMDIOC) {
8190 mptsas_return_to_pool(mpt, cmd);
8191 return;
8194 ASSERT(cmd != slots->m_slot[cmd->cmd_slot]);
8198 * accept all cmds on the tx_waitq if any and then
8199 * start a fresh request from the top of the device queue.
8201 * since there are always cmds queued on the tx_waitq, and rare cmds on
8202 * the instance waitq, so this function should not be invoked in the ISR,
8203 * the mptsas_restart_waitq() is invoked in the ISR instead. otherwise, the
8204 * burden belongs to the IO dispatch CPUs is moved the interrupt CPU.
8206 static void
8207 mptsas_restart_hba(mptsas_t *mpt)
8209 ASSERT(mutex_owned(&mpt->m_mutex));
8211 mutex_enter(&mpt->m_tx_waitq_mutex);
8212 if (mpt->m_tx_waitq) {
8213 mptsas_accept_tx_waitq(mpt);
8215 mutex_exit(&mpt->m_tx_waitq_mutex);
8216 mptsas_restart_waitq(mpt);
8220 * start a fresh request from the top of the device queue
8222 static void
8223 mptsas_restart_waitq(mptsas_t *mpt)
8225 mptsas_cmd_t *cmd, *next_cmd;
8226 mptsas_target_t *ptgt = NULL;
8228 NDBG1(("mptsas_restart_waitq: mpt=0x%p", (void *)mpt));
8230 ASSERT(mutex_owned(&mpt->m_mutex));
8233 * If there is a reset delay, don't start any cmds. Otherwise, start
8234 * as many cmds as possible.
8235 * Since SMID 0 is reserved and the TM slot is reserved, the actual max
8236 * commands is m_max_requests - 2.
8238 cmd = mpt->m_waitq;
8240 while (cmd != NULL) {
8241 next_cmd = cmd->cmd_linkp;
8242 if (cmd->cmd_flags & CFLAG_PASSTHRU) {
8243 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8245 * passthru command get slot need
8246 * set CFLAG_PREPARED.
8248 cmd->cmd_flags |= CFLAG_PREPARED;
8249 mptsas_waitq_delete(mpt, cmd);
8250 mptsas_start_passthru(mpt, cmd);
8252 cmd = next_cmd;
8253 continue;
8255 if (cmd->cmd_flags & CFLAG_CONFIG) {
8256 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8258 * Send the config page request and delete it
8259 * from the waitq.
8261 cmd->cmd_flags |= CFLAG_PREPARED;
8262 mptsas_waitq_delete(mpt, cmd);
8263 mptsas_start_config_page_access(mpt, cmd);
8265 cmd = next_cmd;
8266 continue;
8268 if (cmd->cmd_flags & CFLAG_FW_DIAG) {
8269 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8271 * Send the FW Diag request and delete if from
8272 * the waitq.
8274 cmd->cmd_flags |= CFLAG_PREPARED;
8275 mptsas_waitq_delete(mpt, cmd);
8276 mptsas_start_diag(mpt, cmd);
8278 cmd = next_cmd;
8279 continue;
8282 ptgt = cmd->cmd_tgt_addr;
8283 if (ptgt && (ptgt->m_t_throttle == DRAIN_THROTTLE) &&
8284 (ptgt->m_t_ncmds == 0)) {
8285 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
8287 if ((mpt->m_ncmds <= (mpt->m_max_requests - 2)) &&
8288 (ptgt && (ptgt->m_reset_delay == 0)) &&
8289 (ptgt && (ptgt->m_t_ncmds <
8290 ptgt->m_t_throttle))) {
8291 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
8292 mptsas_waitq_delete(mpt, cmd);
8293 (void) mptsas_start_cmd(mpt, cmd);
8296 cmd = next_cmd;
8300 * Cmds are queued if tran_start() doesn't get the m_mutexlock(no wait).
8301 * Accept all those queued cmds before new cmd is accept so that the
8302 * cmds are sent in order.
8304 static void
8305 mptsas_accept_tx_waitq(mptsas_t *mpt)
8307 mptsas_cmd_t *cmd;
8309 ASSERT(mutex_owned(&mpt->m_mutex));
8310 ASSERT(mutex_owned(&mpt->m_tx_waitq_mutex));
8313 * A Bus Reset could occur at any time and flush the tx_waitq,
8314 * so we cannot count on the tx_waitq to contain even one cmd.
8315 * And when the m_tx_waitq_mutex is released and run
8316 * mptsas_accept_pkt(), the tx_waitq may be flushed.
8318 cmd = mpt->m_tx_waitq;
8319 for (;;) {
8320 if ((cmd = mpt->m_tx_waitq) == NULL) {
8321 mpt->m_tx_draining = 0;
8322 break;
8324 if ((mpt->m_tx_waitq = cmd->cmd_linkp) == NULL) {
8325 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
8327 cmd->cmd_linkp = NULL;
8328 mutex_exit(&mpt->m_tx_waitq_mutex);
8329 if (mptsas_accept_pkt(mpt, cmd) != TRAN_ACCEPT)
8330 cmn_err(CE_WARN, "mpt: mptsas_accept_tx_waitq: failed "
8331 "to accept cmd on queue\n");
8332 mutex_enter(&mpt->m_tx_waitq_mutex);
8338 * mpt tag type lookup
8340 static char mptsas_tag_lookup[] =
8341 {0, MSG_HEAD_QTAG, MSG_ORDERED_QTAG, 0, MSG_SIMPLE_QTAG};
8343 static int
8344 mptsas_start_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
8346 struct scsi_pkt *pkt = CMD2PKT(cmd);
8347 uint32_t control = 0;
8348 caddr_t mem, arsbuf;
8349 pMpi2SCSIIORequest_t io_request;
8350 ddi_dma_handle_t dma_hdl = mpt->m_dma_req_frame_hdl;
8351 ddi_acc_handle_t acc_hdl = mpt->m_acc_req_frame_hdl;
8352 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8353 uint16_t SMID, io_flags = 0;
8354 uint8_t ars_size;
8355 uint64_t request_desc;
8356 uint32_t ars_dmaaddrlow;
8357 mptsas_cmd_t *c;
8359 NDBG1(("mptsas_start_cmd: cmd=0x%p, flags 0x%x", (void *)cmd,
8360 cmd->cmd_flags));
8363 * Set SMID and increment index. Rollover to 1 instead of 0 if index
8364 * is at the max. 0 is an invalid SMID, so we call the first index 1.
8366 SMID = cmd->cmd_slot;
8369 * It is possible for back to back device reset to
8370 * happen before the reset delay has expired. That's
8371 * ok, just let the device reset go out on the bus.
8373 if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8374 ASSERT(ptgt->m_reset_delay == 0);
8378 * if a non-tagged cmd is submitted to an active tagged target
8379 * then drain before submitting this cmd; SCSI-2 allows RQSENSE
8380 * to be untagged
8382 if (((cmd->cmd_pkt_flags & FLAG_TAGMASK) == 0) &&
8383 (ptgt->m_t_ncmds > 1) &&
8384 ((cmd->cmd_flags & CFLAG_TM_CMD) == 0) &&
8385 (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE)) {
8386 if ((cmd->cmd_pkt_flags & FLAG_NOINTR) == 0) {
8387 NDBG23(("target=%d, untagged cmd, start draining\n",
8388 ptgt->m_devhdl));
8390 if (ptgt->m_reset_delay == 0) {
8391 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
8394 mptsas_remove_cmd(mpt, cmd);
8395 cmd->cmd_pkt_flags |= FLAG_HEAD;
8396 mptsas_waitq_add(mpt, cmd);
8398 return (DDI_FAILURE);
8402 * Set correct tag bits.
8404 if (cmd->cmd_pkt_flags & FLAG_TAGMASK) {
8405 switch (mptsas_tag_lookup[((cmd->cmd_pkt_flags &
8406 FLAG_TAGMASK) >> 12)]) {
8407 case MSG_SIMPLE_QTAG:
8408 control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8409 break;
8410 case MSG_HEAD_QTAG:
8411 control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
8412 break;
8413 case MSG_ORDERED_QTAG:
8414 control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
8415 break;
8416 default:
8417 mptsas_log(mpt, CE_WARN, "mpt: Invalid tag type\n");
8418 break;
8420 } else {
8421 if (*(cmd->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
8422 ptgt->m_t_throttle = 1;
8424 control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
8427 if (cmd->cmd_pkt_flags & FLAG_TLR) {
8428 control |= MPI2_SCSIIO_CONTROL_TLR_ON;
8431 mem = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
8432 io_request = (pMpi2SCSIIORequest_t)mem;
8433 if (cmd->cmd_extrqslen != 0) {
8435 * Mapping of the buffer was done in mptsas_pkt_alloc_extern().
8436 * Calculate the DMA address with the same offset.
8438 arsbuf = cmd->cmd_arq_buf;
8439 ars_size = cmd->cmd_extrqslen;
8440 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
8441 ((uintptr_t)arsbuf - (uintptr_t)mpt->m_req_sense)) &
8442 0xffffffffu;
8443 } else {
8444 arsbuf = mpt->m_req_sense + (mpt->m_req_sense_size * (SMID-1));
8445 cmd->cmd_arq_buf = arsbuf;
8446 ars_size = mpt->m_req_sense_size;
8447 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
8448 (mpt->m_req_sense_size * (SMID-1))) &
8449 0xffffffffu;
8451 bzero(io_request, sizeof (Mpi2SCSIIORequest_t));
8452 bzero(arsbuf, ars_size);
8454 ddi_put8(acc_hdl, &io_request->SGLOffset0, offsetof
8455 (MPI2_SCSI_IO_REQUEST, SGL) / 4);
8456 mptsas_init_std_hdr(acc_hdl, io_request, ptgt->m_devhdl, Lun(cmd), 0,
8457 MPI2_FUNCTION_SCSI_IO_REQUEST);
8459 (void) ddi_rep_put8(acc_hdl, (uint8_t *)pkt->pkt_cdbp,
8460 io_request->CDB.CDB32, cmd->cmd_cdblen, DDI_DEV_AUTOINCR);
8462 io_flags = cmd->cmd_cdblen;
8463 if (mptsas_use_fastpath &&
8464 ptgt->m_io_flags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) {
8465 io_flags |= MPI25_SCSIIO_IOFLAGS_FAST_PATH;
8466 request_desc = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
8467 } else {
8468 request_desc = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
8470 ddi_put16(acc_hdl, &io_request->IoFlags, io_flags);
8472 * setup the Scatter/Gather DMA list for this request
8474 if (cmd->cmd_cookiec > 0) {
8475 mptsas_sge_setup(mpt, cmd, &control, io_request, acc_hdl);
8476 } else {
8477 ddi_put32(acc_hdl, &io_request->SGL.MpiSimple.FlagsLength,
8478 ((uint32_t)MPI2_SGE_FLAGS_LAST_ELEMENT |
8479 MPI2_SGE_FLAGS_END_OF_BUFFER |
8480 MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
8481 MPI2_SGE_FLAGS_END_OF_LIST) << MPI2_SGE_FLAGS_SHIFT);
8485 * save ARQ information
8487 ddi_put8(acc_hdl, &io_request->SenseBufferLength, ars_size);
8488 ddi_put32(acc_hdl, &io_request->SenseBufferLowAddress, ars_dmaaddrlow);
8490 ddi_put32(acc_hdl, &io_request->Control, control);
8492 NDBG31(("starting message=%d(0x%p), with cmd=0x%p",
8493 SMID, (void *)io_request, (void *)cmd));
8495 (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
8496 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
8497 DDI_DMA_SYNC_FORDEV);
8500 * Build request descriptor and write it to the request desc post reg.
8502 request_desc |= (SMID << 16);
8503 request_desc |= (uint64_t)ptgt->m_devhdl << 48;
8504 MPTSAS_START_CMD(mpt, request_desc);
8507 * Start timeout.
8509 cmd->cmd_active_expiration =
8510 gethrtime() + (hrtime_t)pkt->pkt_time * NANOSEC;
8511 #ifdef MPTSAS_TEST
8513 * Force timeouts to happen immediately.
8515 if (mptsas_test_timeouts)
8516 cmd->cmd_active_expiration = gethrtime();
8517 #endif
8518 c = TAILQ_FIRST(&ptgt->m_active_cmdq);
8519 if (c == NULL ||
8520 c->cmd_active_expiration < cmd->cmd_active_expiration) {
8522 * Common case is that this is the last pending expiration
8523 * (or queue is empty). Insert at head of the queue.
8525 TAILQ_INSERT_HEAD(&ptgt->m_active_cmdq, cmd, cmd_active_link);
8526 } else {
8528 * Queue is not empty and first element expires later than
8529 * this command. Search for element expiring sooner.
8531 while ((c = TAILQ_NEXT(c, cmd_active_link)) != NULL) {
8532 if (c->cmd_active_expiration <
8533 cmd->cmd_active_expiration) {
8534 TAILQ_INSERT_BEFORE(c, cmd, cmd_active_link);
8535 break;
8538 if (c == NULL) {
8540 * No element found expiring sooner, append to
8541 * non-empty queue.
8543 TAILQ_INSERT_TAIL(&ptgt->m_active_cmdq, cmd,
8544 cmd_active_link);
8548 if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
8549 (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
8550 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8551 return (DDI_FAILURE);
8553 return (DDI_SUCCESS);
8557 * Select a helper thread to handle current doneq
8559 static void
8560 mptsas_deliver_doneq_thread(mptsas_t *mpt)
8562 uint64_t t, i;
8563 uint32_t min = 0xffffffff;
8564 mptsas_doneq_thread_list_t *item;
8566 for (i = 0; i < mpt->m_doneq_thread_n; i++) {
8567 item = &mpt->m_doneq_thread_id[i];
8569 * If the completed command on help thread[i] less than
8570 * doneq_thread_threshold, then pick the thread[i]. Otherwise
8571 * pick a thread which has least completed command.
8574 mutex_enter(&item->mutex);
8575 if (item->len < mpt->m_doneq_thread_threshold) {
8576 t = i;
8577 mutex_exit(&item->mutex);
8578 break;
8580 if (item->len < min) {
8581 min = item->len;
8582 t = i;
8584 mutex_exit(&item->mutex);
8586 mutex_enter(&mpt->m_doneq_thread_id[t].mutex);
8587 mptsas_doneq_mv(mpt, t);
8588 cv_signal(&mpt->m_doneq_thread_id[t].cv);
8589 mutex_exit(&mpt->m_doneq_thread_id[t].mutex);
8593 * move the current global doneq to the doneq of thead[t]
8595 static void
8596 mptsas_doneq_mv(mptsas_t *mpt, uint64_t t)
8598 mptsas_cmd_t *cmd;
8599 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
8601 ASSERT(mutex_owned(&item->mutex));
8602 while ((cmd = mpt->m_doneq) != NULL) {
8603 if ((mpt->m_doneq = cmd->cmd_linkp) == NULL) {
8604 mpt->m_donetail = &mpt->m_doneq;
8606 cmd->cmd_linkp = NULL;
8607 *item->donetail = cmd;
8608 item->donetail = &cmd->cmd_linkp;
8609 mpt->m_doneq_len--;
8610 item->len++;
8614 void
8615 mptsas_fma_check(mptsas_t *mpt, mptsas_cmd_t *cmd)
8617 struct scsi_pkt *pkt = CMD2PKT(cmd);
8619 /* Check all acc and dma handles */
8620 if ((mptsas_check_acc_handle(mpt->m_datap) !=
8621 DDI_SUCCESS) ||
8622 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
8623 DDI_SUCCESS) ||
8624 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl) !=
8625 DDI_SUCCESS) ||
8626 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
8627 DDI_SUCCESS) ||
8628 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
8629 DDI_SUCCESS) ||
8630 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
8631 DDI_SUCCESS) ||
8632 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
8633 DDI_SUCCESS) ||
8634 (mptsas_check_acc_handle(mpt->m_config_handle) !=
8635 DDI_SUCCESS)) {
8636 ddi_fm_service_impact(mpt->m_dip,
8637 DDI_SERVICE_UNAFFECTED);
8638 ddi_fm_acc_err_clear(mpt->m_config_handle,
8639 DDI_FME_VER0);
8640 pkt->pkt_reason = CMD_TRAN_ERR;
8641 pkt->pkt_statistics = 0;
8643 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
8644 DDI_SUCCESS) ||
8645 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl) !=
8646 DDI_SUCCESS) ||
8647 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
8648 DDI_SUCCESS) ||
8649 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
8650 DDI_SUCCESS) ||
8651 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
8652 DDI_SUCCESS) ||
8653 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
8654 DDI_SUCCESS)) {
8655 ddi_fm_service_impact(mpt->m_dip,
8656 DDI_SERVICE_UNAFFECTED);
8657 pkt->pkt_reason = CMD_TRAN_ERR;
8658 pkt->pkt_statistics = 0;
8660 if (cmd->cmd_dmahandle &&
8661 (mptsas_check_dma_handle(cmd->cmd_dmahandle) != DDI_SUCCESS)) {
8662 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8663 pkt->pkt_reason = CMD_TRAN_ERR;
8664 pkt->pkt_statistics = 0;
8666 if ((cmd->cmd_extra_frames &&
8667 ((mptsas_check_dma_handle(cmd->cmd_extra_frames->m_dma_hdl) !=
8668 DDI_SUCCESS) ||
8669 (mptsas_check_acc_handle(cmd->cmd_extra_frames->m_acc_hdl) !=
8670 DDI_SUCCESS)))) {
8671 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
8672 pkt->pkt_reason = CMD_TRAN_ERR;
8673 pkt->pkt_statistics = 0;
8678 * These routines manipulate the queue of commands that
8679 * are waiting for their completion routines to be called.
8680 * The queue is usually in FIFO order but on an MP system
8681 * it's possible for the completion routines to get out
8682 * of order. If that's a problem you need to add a global
8683 * mutex around the code that calls the completion routine
8684 * in the interrupt handler.
8686 static void
8687 mptsas_doneq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8689 struct scsi_pkt *pkt = CMD2PKT(cmd);
8691 NDBG31(("mptsas_doneq_add: cmd=0x%p", (void *)cmd));
8693 ASSERT((cmd->cmd_flags & CFLAG_COMPLETED) == 0);
8694 cmd->cmd_linkp = NULL;
8695 cmd->cmd_flags |= CFLAG_FINISHED;
8696 cmd->cmd_flags &= ~CFLAG_IN_TRANSPORT;
8698 mptsas_fma_check(mpt, cmd);
8701 * only add scsi pkts that have completion routines to
8702 * the doneq. no intr cmds do not have callbacks.
8704 if (pkt && (pkt->pkt_comp)) {
8705 *mpt->m_donetail = cmd;
8706 mpt->m_donetail = &cmd->cmd_linkp;
8707 mpt->m_doneq_len++;
8711 static mptsas_cmd_t *
8712 mptsas_doneq_thread_rm(mptsas_t *mpt, uint64_t t)
8714 mptsas_cmd_t *cmd;
8715 mptsas_doneq_thread_list_t *item = &mpt->m_doneq_thread_id[t];
8717 /* pop one off the done queue */
8718 if ((cmd = item->doneq) != NULL) {
8719 /* if the queue is now empty fix the tail pointer */
8720 NDBG31(("mptsas_doneq_thread_rm: cmd=0x%p", (void *)cmd));
8721 if ((item->doneq = cmd->cmd_linkp) == NULL) {
8722 item->donetail = &item->doneq;
8724 cmd->cmd_linkp = NULL;
8725 item->len--;
8727 return (cmd);
8730 static void
8731 mptsas_doneq_empty(mptsas_t *mpt)
8733 if (mpt->m_doneq && !mpt->m_in_callback) {
8734 mptsas_cmd_t *cmd, *next;
8735 struct scsi_pkt *pkt;
8737 mpt->m_in_callback = 1;
8738 cmd = mpt->m_doneq;
8739 mpt->m_doneq = NULL;
8740 mpt->m_donetail = &mpt->m_doneq;
8741 mpt->m_doneq_len = 0;
8743 mutex_exit(&mpt->m_mutex);
8745 * run the completion routines of all the
8746 * completed commands
8748 while (cmd != NULL) {
8749 next = cmd->cmd_linkp;
8750 cmd->cmd_linkp = NULL;
8751 /* run this command's completion routine */
8752 cmd->cmd_flags |= CFLAG_COMPLETED;
8753 pkt = CMD2PKT(cmd);
8754 mptsas_pkt_comp(pkt, cmd);
8755 cmd = next;
8757 mutex_enter(&mpt->m_mutex);
8758 mpt->m_in_callback = 0;
8763 * These routines manipulate the target's queue of pending requests
8765 void
8766 mptsas_waitq_add(mptsas_t *mpt, mptsas_cmd_t *cmd)
8768 NDBG7(("mptsas_waitq_add: cmd=0x%p", (void *)cmd));
8769 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8770 cmd->cmd_queued = TRUE;
8771 if (ptgt)
8772 ptgt->m_t_nwait++;
8773 if (cmd->cmd_pkt_flags & FLAG_HEAD) {
8774 if ((cmd->cmd_linkp = mpt->m_waitq) == NULL) {
8775 mpt->m_waitqtail = &cmd->cmd_linkp;
8777 mpt->m_waitq = cmd;
8778 } else {
8779 cmd->cmd_linkp = NULL;
8780 *(mpt->m_waitqtail) = cmd;
8781 mpt->m_waitqtail = &cmd->cmd_linkp;
8785 static mptsas_cmd_t *
8786 mptsas_waitq_rm(mptsas_t *mpt)
8788 mptsas_cmd_t *cmd;
8789 mptsas_target_t *ptgt;
8790 NDBG7(("mptsas_waitq_rm"));
8792 MPTSAS_WAITQ_RM(mpt, cmd);
8794 NDBG7(("mptsas_waitq_rm: cmd=0x%p", (void *)cmd));
8795 if (cmd) {
8796 ptgt = cmd->cmd_tgt_addr;
8797 if (ptgt) {
8798 ptgt->m_t_nwait--;
8799 ASSERT(ptgt->m_t_nwait >= 0);
8802 return (cmd);
8806 * remove specified cmd from the middle of the wait queue.
8808 static void
8809 mptsas_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd)
8811 mptsas_cmd_t *prevp = mpt->m_waitq;
8812 mptsas_target_t *ptgt = cmd->cmd_tgt_addr;
8814 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8815 (void *)mpt, (void *)cmd));
8816 if (ptgt) {
8817 ptgt->m_t_nwait--;
8818 ASSERT(ptgt->m_t_nwait >= 0);
8821 if (prevp == cmd) {
8822 if ((mpt->m_waitq = cmd->cmd_linkp) == NULL)
8823 mpt->m_waitqtail = &mpt->m_waitq;
8825 cmd->cmd_linkp = NULL;
8826 cmd->cmd_queued = FALSE;
8827 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8828 (void *)mpt, (void *)cmd));
8829 return;
8832 while (prevp != NULL) {
8833 if (prevp->cmd_linkp == cmd) {
8834 if ((prevp->cmd_linkp = cmd->cmd_linkp) == NULL)
8835 mpt->m_waitqtail = &prevp->cmd_linkp;
8837 cmd->cmd_linkp = NULL;
8838 cmd->cmd_queued = FALSE;
8839 NDBG7(("mptsas_waitq_delete: mpt=0x%p cmd=0x%p",
8840 (void *)mpt, (void *)cmd));
8841 return;
8843 prevp = prevp->cmd_linkp;
8845 cmn_err(CE_PANIC, "mpt: mptsas_waitq_delete: queue botch");
8848 static mptsas_cmd_t *
8849 mptsas_tx_waitq_rm(mptsas_t *mpt)
8851 mptsas_cmd_t *cmd;
8852 NDBG7(("mptsas_tx_waitq_rm"));
8854 MPTSAS_TX_WAITQ_RM(mpt, cmd);
8856 NDBG7(("mptsas_tx_waitq_rm: cmd=0x%p", (void *)cmd));
8858 return (cmd);
8862 * remove specified cmd from the middle of the tx_waitq.
8864 static void
8865 mptsas_tx_waitq_delete(mptsas_t *mpt, mptsas_cmd_t *cmd)
8867 mptsas_cmd_t *prevp = mpt->m_tx_waitq;
8869 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8870 (void *)mpt, (void *)cmd));
8872 if (prevp == cmd) {
8873 if ((mpt->m_tx_waitq = cmd->cmd_linkp) == NULL)
8874 mpt->m_tx_waitqtail = &mpt->m_tx_waitq;
8876 cmd->cmd_linkp = NULL;
8877 cmd->cmd_queued = FALSE;
8878 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8879 (void *)mpt, (void *)cmd));
8880 return;
8883 while (prevp != NULL) {
8884 if (prevp->cmd_linkp == cmd) {
8885 if ((prevp->cmd_linkp = cmd->cmd_linkp) == NULL)
8886 mpt->m_tx_waitqtail = &prevp->cmd_linkp;
8888 cmd->cmd_linkp = NULL;
8889 cmd->cmd_queued = FALSE;
8890 NDBG7(("mptsas_tx_waitq_delete: mpt=0x%p cmd=0x%p",
8891 (void *)mpt, (void *)cmd));
8892 return;
8894 prevp = prevp->cmd_linkp;
8896 cmn_err(CE_PANIC, "mpt: mptsas_tx_waitq_delete: queue botch");
8900 * device and bus reset handling
8902 * Notes:
8903 * - RESET_ALL: reset the controller
8904 * - RESET_TARGET: reset the target specified in scsi_address
8906 static int
8907 mptsas_scsi_reset(struct scsi_address *ap, int level)
8909 mptsas_t *mpt = ADDR2MPT(ap);
8910 int rval;
8911 mptsas_tgt_private_t *tgt_private;
8912 mptsas_target_t *ptgt = NULL;
8914 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->tran_tgt_private;
8915 ptgt = tgt_private->t_private;
8916 if (ptgt == NULL) {
8917 return (FALSE);
8919 NDBG22(("mptsas_scsi_reset: target=%d level=%d", ptgt->m_devhdl,
8920 level));
8922 mutex_enter(&mpt->m_mutex);
8924 * if we are not in panic set up a reset delay for this target
8926 if (!ddi_in_panic()) {
8927 mptsas_setup_bus_reset_delay(mpt);
8928 } else {
8929 drv_usecwait(mpt->m_scsi_reset_delay * 1000);
8931 rval = mptsas_do_scsi_reset(mpt, ptgt->m_devhdl);
8932 mutex_exit(&mpt->m_mutex);
8935 * The transport layer expect to only see TRUE and
8936 * FALSE. Therefore, we will adjust the return value
8937 * if mptsas_do_scsi_reset returns FAILED.
8939 if (rval == FAILED)
8940 rval = FALSE;
8941 return (rval);
8944 static int
8945 mptsas_do_scsi_reset(mptsas_t *mpt, uint16_t devhdl)
8947 int rval = FALSE;
8948 uint8_t config, disk;
8950 ASSERT(mutex_owned(&mpt->m_mutex));
8952 if (mptsas_debug_resets) {
8953 mptsas_log(mpt, CE_WARN, "mptsas_do_scsi_reset: target=%d",
8954 devhdl);
8958 * Issue a Target Reset message to the target specified but not to a
8959 * disk making up a raid volume. Just look through the RAID config
8960 * Phys Disk list of DevHandles. If the target's DevHandle is in this
8961 * list, then don't reset this target.
8963 for (config = 0; config < mpt->m_num_raid_configs; config++) {
8964 for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
8965 if (devhdl == mpt->m_raidconfig[config].
8966 m_physdisk_devhdl[disk]) {
8967 return (TRUE);
8972 rval = mptsas_ioc_task_management(mpt,
8973 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, devhdl, 0, NULL, 0, 0);
8975 mptsas_doneq_empty(mpt);
8976 return (rval);
8979 static int
8980 mptsas_scsi_reset_notify(struct scsi_address *ap, int flag,
8981 void (*callback)(caddr_t), caddr_t arg)
8983 mptsas_t *mpt = ADDR2MPT(ap);
8985 NDBG22(("mptsas_scsi_reset_notify: tgt=%d", ap->a_target));
8987 return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
8988 &mpt->m_mutex, &mpt->m_reset_notify_listf));
8991 static int
8992 mptsas_get_name(struct scsi_device *sd, char *name, int len)
8994 dev_info_t *lun_dip = NULL;
8996 ASSERT(sd != NULL);
8997 ASSERT(name != NULL);
8998 lun_dip = sd->sd_dev;
8999 ASSERT(lun_dip != NULL);
9001 if (mptsas_name_child(lun_dip, name, len) == DDI_SUCCESS) {
9002 return (1);
9003 } else {
9004 return (0);
9008 static int
9009 mptsas_get_bus_addr(struct scsi_device *sd, char *name, int len)
9011 return (mptsas_get_name(sd, name, len));
9014 void
9015 mptsas_set_throttle(mptsas_t *mpt, mptsas_target_t *ptgt, int what)
9018 NDBG25(("mptsas_set_throttle: throttle=%x", what));
9021 * if the bus is draining/quiesced, no changes to the throttles
9022 * are allowed. Not allowing change of throttles during draining
9023 * limits error recovery but will reduce draining time
9025 * all throttles should have been set to HOLD_THROTTLE
9027 if (mpt->m_softstate & (MPTSAS_SS_QUIESCED | MPTSAS_SS_DRAINING)) {
9028 return;
9031 if (what == HOLD_THROTTLE) {
9032 ptgt->m_t_throttle = HOLD_THROTTLE;
9033 } else if (ptgt->m_reset_delay == 0) {
9034 ptgt->m_t_throttle = what;
9039 * Clean up from a device reset.
9040 * For the case of target reset, this function clears the waitq of all
9041 * commands for a particular target. For the case of abort task set, this
9042 * function clears the waitq of all commonds for a particular target/lun.
9044 static void
9045 mptsas_flush_target(mptsas_t *mpt, ushort_t target, int lun, uint8_t tasktype)
9047 mptsas_slots_t *slots = mpt->m_active;
9048 mptsas_cmd_t *cmd, *next_cmd;
9049 int slot;
9050 uchar_t reason;
9051 uint_t stat;
9052 hrtime_t timestamp;
9054 NDBG25(("mptsas_flush_target: target=%d lun=%d", target, lun));
9056 timestamp = gethrtime();
9059 * Make sure the I/O Controller has flushed all cmds
9060 * that are associated with this target for a target reset
9061 * and target/lun for abort task set.
9062 * Account for TM requests, which use the last SMID.
9064 for (slot = 0; slot <= mpt->m_active->m_n_normal; slot++) {
9065 if ((cmd = slots->m_slot[slot]) == NULL)
9066 continue;
9067 reason = CMD_RESET;
9068 stat = STAT_DEV_RESET;
9069 switch (tasktype) {
9070 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9071 if (Tgt(cmd) == target) {
9072 if (cmd->cmd_active_expiration <= timestamp) {
9074 * When timeout requested, propagate
9075 * proper reason and statistics to
9076 * target drivers.
9078 reason = CMD_TIMEOUT;
9079 stat |= STAT_TIMEOUT;
9081 NDBG25(("mptsas_flush_target discovered non-"
9082 "NULL cmd in slot %d, tasktype 0x%x", slot,
9083 tasktype));
9084 mptsas_dump_cmd(mpt, cmd);
9085 mptsas_remove_cmd(mpt, cmd);
9086 mptsas_set_pkt_reason(mpt, cmd, reason, stat);
9087 mptsas_doneq_add(mpt, cmd);
9089 break;
9090 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9091 reason = CMD_ABORTED;
9092 stat = STAT_ABORTED;
9093 /*FALLTHROUGH*/
9094 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9095 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9097 NDBG25(("mptsas_flush_target discovered non-"
9098 "NULL cmd in slot %d, tasktype 0x%x", slot,
9099 tasktype));
9100 mptsas_dump_cmd(mpt, cmd);
9101 mptsas_remove_cmd(mpt, cmd);
9102 mptsas_set_pkt_reason(mpt, cmd, reason,
9103 stat);
9104 mptsas_doneq_add(mpt, cmd);
9106 break;
9107 default:
9108 break;
9113 * Flush the waitq and tx_waitq of this target's cmds
9115 cmd = mpt->m_waitq;
9117 reason = CMD_RESET;
9118 stat = STAT_DEV_RESET;
9120 switch (tasktype) {
9121 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
9122 while (cmd != NULL) {
9123 next_cmd = cmd->cmd_linkp;
9124 if (Tgt(cmd) == target) {
9125 mptsas_waitq_delete(mpt, cmd);
9126 mptsas_set_pkt_reason(mpt, cmd,
9127 reason, stat);
9128 mptsas_doneq_add(mpt, cmd);
9130 cmd = next_cmd;
9132 mutex_enter(&mpt->m_tx_waitq_mutex);
9133 cmd = mpt->m_tx_waitq;
9134 while (cmd != NULL) {
9135 next_cmd = cmd->cmd_linkp;
9136 if (Tgt(cmd) == target) {
9137 mptsas_tx_waitq_delete(mpt, cmd);
9138 mutex_exit(&mpt->m_tx_waitq_mutex);
9139 mptsas_set_pkt_reason(mpt, cmd,
9140 reason, stat);
9141 mptsas_doneq_add(mpt, cmd);
9142 mutex_enter(&mpt->m_tx_waitq_mutex);
9144 cmd = next_cmd;
9146 mutex_exit(&mpt->m_tx_waitq_mutex);
9147 break;
9148 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
9149 reason = CMD_ABORTED;
9150 stat = STAT_ABORTED;
9151 /*FALLTHROUGH*/
9152 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
9153 while (cmd != NULL) {
9154 next_cmd = cmd->cmd_linkp;
9155 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9156 mptsas_waitq_delete(mpt, cmd);
9157 mptsas_set_pkt_reason(mpt, cmd,
9158 reason, stat);
9159 mptsas_doneq_add(mpt, cmd);
9161 cmd = next_cmd;
9163 mutex_enter(&mpt->m_tx_waitq_mutex);
9164 cmd = mpt->m_tx_waitq;
9165 while (cmd != NULL) {
9166 next_cmd = cmd->cmd_linkp;
9167 if ((Tgt(cmd) == target) && (Lun(cmd) == lun)) {
9168 mptsas_tx_waitq_delete(mpt, cmd);
9169 mutex_exit(&mpt->m_tx_waitq_mutex);
9170 mptsas_set_pkt_reason(mpt, cmd,
9171 reason, stat);
9172 mptsas_doneq_add(mpt, cmd);
9173 mutex_enter(&mpt->m_tx_waitq_mutex);
9175 cmd = next_cmd;
9177 mutex_exit(&mpt->m_tx_waitq_mutex);
9178 break;
9179 default:
9180 mptsas_log(mpt, CE_WARN, "Unknown task management type %d.",
9181 tasktype);
9182 break;
9187 * Clean up hba state, abort all outstanding command and commands in waitq
9188 * reset timeout of all targets.
9190 static void
9191 mptsas_flush_hba(mptsas_t *mpt)
9193 mptsas_slots_t *slots = mpt->m_active;
9194 mptsas_cmd_t *cmd;
9195 int slot;
9197 NDBG25(("mptsas_flush_hba"));
9200 * The I/O Controller should have already sent back
9201 * all commands via the scsi I/O reply frame. Make
9202 * sure all commands have been flushed.
9203 * Account for TM request, which use the last SMID.
9205 for (slot = 0; slot <= mpt->m_active->m_n_normal; slot++) {
9206 if ((cmd = slots->m_slot[slot]) == NULL)
9207 continue;
9209 if (cmd->cmd_flags & CFLAG_CMDIOC) {
9211 * Need to make sure to tell everyone that might be
9212 * waiting on this command that it's going to fail. If
9213 * we get here, this command will never timeout because
9214 * the active command table is going to be re-allocated,
9215 * so there will be nothing to check against a time out.
9216 * Instead, mark the command as failed due to reset.
9218 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET,
9219 STAT_BUS_RESET);
9220 if ((cmd->cmd_flags &
9221 (CFLAG_PASSTHRU | CFLAG_CONFIG | CFLAG_FW_DIAG))) {
9222 cmd->cmd_flags |= CFLAG_FINISHED;
9223 cv_broadcast(&mpt->m_passthru_cv);
9224 cv_broadcast(&mpt->m_config_cv);
9225 cv_broadcast(&mpt->m_fw_diag_cv);
9227 continue;
9230 NDBG25(("mptsas_flush_hba discovered non-NULL cmd in slot %d",
9231 slot));
9232 mptsas_dump_cmd(mpt, cmd);
9234 mptsas_remove_cmd(mpt, cmd);
9235 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9236 mptsas_doneq_add(mpt, cmd);
9240 * Flush the waitq.
9242 while ((cmd = mptsas_waitq_rm(mpt)) != NULL) {
9243 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9244 if ((cmd->cmd_flags & CFLAG_PASSTHRU) ||
9245 (cmd->cmd_flags & CFLAG_CONFIG) ||
9246 (cmd->cmd_flags & CFLAG_FW_DIAG)) {
9247 cmd->cmd_flags |= CFLAG_FINISHED;
9248 cv_broadcast(&mpt->m_passthru_cv);
9249 cv_broadcast(&mpt->m_config_cv);
9250 cv_broadcast(&mpt->m_fw_diag_cv);
9251 } else {
9252 mptsas_doneq_add(mpt, cmd);
9257 * Flush the tx_waitq
9259 mutex_enter(&mpt->m_tx_waitq_mutex);
9260 while ((cmd = mptsas_tx_waitq_rm(mpt)) != NULL) {
9261 mutex_exit(&mpt->m_tx_waitq_mutex);
9262 mptsas_set_pkt_reason(mpt, cmd, CMD_RESET, STAT_BUS_RESET);
9263 mptsas_doneq_add(mpt, cmd);
9264 mutex_enter(&mpt->m_tx_waitq_mutex);
9266 mutex_exit(&mpt->m_tx_waitq_mutex);
9269 * Drain the taskqs prior to reallocating resources. The thread
9270 * passing through here could be launched from either (dr)
9271 * or (event) taskqs so only wait on the 'other' queue since
9272 * waiting on 'this' queue is a deadlock condition.
9274 mutex_exit(&mpt->m_mutex);
9275 if (!taskq_member((taskq_t *)mpt->m_event_taskq, curthread))
9276 ddi_taskq_wait(mpt->m_event_taskq);
9277 if (!taskq_member((taskq_t *)mpt->m_dr_taskq, curthread))
9278 ddi_taskq_wait(mpt->m_dr_taskq);
9280 mutex_enter(&mpt->m_mutex);
9284 * set pkt_reason and OR in pkt_statistics flag
9286 static void
9287 mptsas_set_pkt_reason(mptsas_t *mpt, mptsas_cmd_t *cmd, uchar_t reason,
9288 uint_t stat)
9290 _NOTE(ARGUNUSED(mpt))
9292 NDBG25(("mptsas_set_pkt_reason: cmd=0x%p reason=%x stat=%x",
9293 (void *)cmd, reason, stat));
9295 if (cmd) {
9296 if (cmd->cmd_pkt->pkt_reason == CMD_CMPLT) {
9297 cmd->cmd_pkt->pkt_reason = reason;
9299 cmd->cmd_pkt->pkt_statistics |= stat;
9303 static void
9304 mptsas_start_watch_reset_delay()
9306 NDBG22(("mptsas_start_watch_reset_delay"));
9308 mutex_enter(&mptsas_global_mutex);
9309 if (mptsas_reset_watch == NULL && mptsas_timeouts_enabled) {
9310 mptsas_reset_watch = timeout(mptsas_watch_reset_delay, NULL,
9311 drv_usectohz((clock_t)
9312 MPTSAS_WATCH_RESET_DELAY_TICK * 1000));
9313 ASSERT(mptsas_reset_watch != NULL);
9315 mutex_exit(&mptsas_global_mutex);
9318 static void
9319 mptsas_setup_bus_reset_delay(mptsas_t *mpt)
9321 mptsas_target_t *ptgt = NULL;
9323 ASSERT(MUTEX_HELD(&mpt->m_mutex));
9325 NDBG22(("mptsas_setup_bus_reset_delay"));
9326 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9327 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9328 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
9329 ptgt->m_reset_delay = mpt->m_scsi_reset_delay;
9332 mptsas_start_watch_reset_delay();
9336 * mptsas_watch_reset_delay(_subr) is invoked by timeout() and checks every
9337 * mpt instance for active reset delays
9339 static void
9340 mptsas_watch_reset_delay(void *arg)
9342 _NOTE(ARGUNUSED(arg))
9344 mptsas_t *mpt;
9345 int not_done = 0;
9347 NDBG22(("mptsas_watch_reset_delay"));
9349 mutex_enter(&mptsas_global_mutex);
9350 mptsas_reset_watch = 0;
9351 mutex_exit(&mptsas_global_mutex);
9352 rw_enter(&mptsas_global_rwlock, RW_READER);
9353 for (mpt = mptsas_head; mpt != NULL; mpt = mpt->m_next) {
9354 if (mpt->m_tran == 0) {
9355 continue;
9357 mutex_enter(&mpt->m_mutex);
9358 not_done += mptsas_watch_reset_delay_subr(mpt);
9359 mutex_exit(&mpt->m_mutex);
9361 rw_exit(&mptsas_global_rwlock);
9363 if (not_done) {
9364 mptsas_start_watch_reset_delay();
9368 static int
9369 mptsas_watch_reset_delay_subr(mptsas_t *mpt)
9371 int done = 0;
9372 int restart = 0;
9373 mptsas_target_t *ptgt = NULL;
9375 NDBG22(("mptsas_watch_reset_delay_subr: mpt=0x%p", (void *)mpt));
9377 ASSERT(mutex_owned(&mpt->m_mutex));
9379 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9380 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9381 if (ptgt->m_reset_delay != 0) {
9382 ptgt->m_reset_delay -=
9383 MPTSAS_WATCH_RESET_DELAY_TICK;
9384 if (ptgt->m_reset_delay <= 0) {
9385 ptgt->m_reset_delay = 0;
9386 mptsas_set_throttle(mpt, ptgt,
9387 MAX_THROTTLE);
9388 restart++;
9389 } else {
9390 done = -1;
9395 if (restart > 0) {
9396 mptsas_restart_hba(mpt);
9398 return (done);
9401 #ifdef MPTSAS_TEST
9402 static void
9403 mptsas_test_reset(mptsas_t *mpt, int target)
9405 mptsas_target_t *ptgt = NULL;
9407 if (mptsas_rtest == target) {
9408 if (mptsas_do_scsi_reset(mpt, target) == TRUE) {
9409 mptsas_rtest = -1;
9411 if (mptsas_rtest == -1) {
9412 NDBG22(("mptsas_test_reset success"));
9416 #endif
9419 * abort handling:
9421 * Notes:
9422 * - if pkt is not NULL, abort just that command
9423 * - if pkt is NULL, abort all outstanding commands for target
9425 static int
9426 mptsas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
9428 mptsas_t *mpt = ADDR2MPT(ap);
9429 int rval;
9430 mptsas_tgt_private_t *tgt_private;
9431 int target, lun;
9433 tgt_private = (mptsas_tgt_private_t *)ap->a_hba_tran->
9434 tran_tgt_private;
9435 ASSERT(tgt_private != NULL);
9436 target = tgt_private->t_private->m_devhdl;
9437 lun = tgt_private->t_lun;
9439 NDBG23(("mptsas_scsi_abort: target=%d.%d", target, lun));
9441 mutex_enter(&mpt->m_mutex);
9442 rval = mptsas_do_scsi_abort(mpt, target, lun, pkt);
9443 mutex_exit(&mpt->m_mutex);
9444 return (rval);
9447 static int
9448 mptsas_do_scsi_abort(mptsas_t *mpt, int target, int lun, struct scsi_pkt *pkt)
9450 mptsas_cmd_t *sp = NULL;
9451 mptsas_slots_t *slots = mpt->m_active;
9452 int rval = FALSE;
9454 ASSERT(mutex_owned(&mpt->m_mutex));
9457 * Abort the command pkt on the target/lun in ap. If pkt is
9458 * NULL, abort all outstanding commands on that target/lun.
9459 * If you can abort them, return 1, else return 0.
9460 * Each packet that's aborted should be sent back to the target
9461 * driver through the callback routine, with pkt_reason set to
9462 * CMD_ABORTED.
9464 * abort cmd pkt on HBA hardware; clean out of outstanding
9465 * command lists, etc.
9467 if (pkt != NULL) {
9468 /* abort the specified packet */
9469 sp = PKT2CMD(pkt);
9471 if (sp->cmd_queued) {
9472 NDBG23(("mptsas_do_scsi_abort: queued sp=0x%p aborted",
9473 (void *)sp));
9474 mptsas_waitq_delete(mpt, sp);
9475 mptsas_set_pkt_reason(mpt, sp, CMD_ABORTED,
9476 STAT_ABORTED);
9477 mptsas_doneq_add(mpt, sp);
9478 rval = TRUE;
9479 goto done;
9483 * Have mpt firmware abort this command
9486 if (slots->m_slot[sp->cmd_slot] != NULL) {
9487 rval = mptsas_ioc_task_management(mpt,
9488 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, target,
9489 lun, NULL, 0, 0);
9492 * The transport layer expects only TRUE and FALSE.
9493 * Therefore, if mptsas_ioc_task_management returns
9494 * FAILED we will return FALSE.
9496 if (rval == FAILED)
9497 rval = FALSE;
9498 goto done;
9503 * If pkt is NULL then abort task set
9505 rval = mptsas_ioc_task_management(mpt,
9506 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, target, lun, NULL, 0, 0);
9509 * The transport layer expects only TRUE and FALSE.
9510 * Therefore, if mptsas_ioc_task_management returns
9511 * FAILED we will return FALSE.
9513 if (rval == FAILED)
9514 rval = FALSE;
9516 #ifdef MPTSAS_TEST
9517 if (rval && mptsas_test_stop) {
9518 debug_enter("mptsas_do_scsi_abort");
9520 #endif
9522 done:
9523 mptsas_doneq_empty(mpt);
9524 return (rval);
9528 * capability handling:
9529 * (*tran_getcap). Get the capability named, and return its value.
9531 static int
9532 mptsas_scsi_getcap(struct scsi_address *ap, char *cap, int tgtonly)
9534 mptsas_t *mpt = ADDR2MPT(ap);
9535 int ckey;
9536 int rval = FALSE;
9538 NDBG24(("mptsas_scsi_getcap: target=%d, cap=%s tgtonly=%x",
9539 ap->a_target, cap, tgtonly));
9541 mutex_enter(&mpt->m_mutex);
9543 if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9544 mutex_exit(&mpt->m_mutex);
9545 return (UNDEFINED);
9548 switch (ckey) {
9549 case SCSI_CAP_DMA_MAX:
9550 rval = (int)mpt->m_msg_dma_attr.dma_attr_maxxfer;
9551 break;
9552 case SCSI_CAP_ARQ:
9553 rval = TRUE;
9554 break;
9555 case SCSI_CAP_MSG_OUT:
9556 case SCSI_CAP_PARITY:
9557 case SCSI_CAP_UNTAGGED_QING:
9558 rval = TRUE;
9559 break;
9560 case SCSI_CAP_TAGGED_QING:
9561 rval = TRUE;
9562 break;
9563 case SCSI_CAP_RESET_NOTIFICATION:
9564 rval = TRUE;
9565 break;
9566 case SCSI_CAP_LINKED_CMDS:
9567 rval = FALSE;
9568 break;
9569 case SCSI_CAP_QFULL_RETRIES:
9570 rval = ((mptsas_tgt_private_t *)(ap->a_hba_tran->
9571 tran_tgt_private))->t_private->m_qfull_retries;
9572 break;
9573 case SCSI_CAP_QFULL_RETRY_INTERVAL:
9574 rval = drv_hztousec(((mptsas_tgt_private_t *)
9575 (ap->a_hba_tran->tran_tgt_private))->
9576 t_private->m_qfull_retry_interval) / 1000;
9577 break;
9578 case SCSI_CAP_CDB_LEN:
9579 rval = CDB_GROUP4;
9580 break;
9581 case SCSI_CAP_INTERCONNECT_TYPE:
9582 rval = INTERCONNECT_SAS;
9583 break;
9584 case SCSI_CAP_TRAN_LAYER_RETRIES:
9585 if (mpt->m_ioc_capabilities &
9586 MPI2_IOCFACTS_CAPABILITY_TLR)
9587 rval = TRUE;
9588 else
9589 rval = FALSE;
9590 break;
9591 default:
9592 rval = UNDEFINED;
9593 break;
9596 NDBG24(("mptsas_scsi_getcap: %s, rval=%x", cap, rval));
9598 mutex_exit(&mpt->m_mutex);
9599 return (rval);
9603 * (*tran_setcap). Set the capability named to the value given.
9605 static int
9606 mptsas_scsi_setcap(struct scsi_address *ap, char *cap, int value, int tgtonly)
9608 mptsas_t *mpt = ADDR2MPT(ap);
9609 int ckey;
9610 int rval = FALSE;
9612 NDBG24(("mptsas_scsi_setcap: target=%d, cap=%s value=%x tgtonly=%x",
9613 ap->a_target, cap, value, tgtonly));
9615 if (!tgtonly) {
9616 return (rval);
9619 mutex_enter(&mpt->m_mutex);
9621 if ((mptsas_scsi_capchk(cap, tgtonly, &ckey)) != TRUE) {
9622 mutex_exit(&mpt->m_mutex);
9623 return (UNDEFINED);
9626 switch (ckey) {
9627 case SCSI_CAP_DMA_MAX:
9628 case SCSI_CAP_MSG_OUT:
9629 case SCSI_CAP_PARITY:
9630 case SCSI_CAP_INITIATOR_ID:
9631 case SCSI_CAP_LINKED_CMDS:
9632 case SCSI_CAP_UNTAGGED_QING:
9633 case SCSI_CAP_RESET_NOTIFICATION:
9635 * None of these are settable via
9636 * the capability interface.
9638 break;
9639 case SCSI_CAP_ARQ:
9641 * We cannot turn off arq so return false if asked to
9643 if (value) {
9644 rval = TRUE;
9645 } else {
9646 rval = FALSE;
9648 break;
9649 case SCSI_CAP_TAGGED_QING:
9650 mptsas_set_throttle(mpt, ((mptsas_tgt_private_t *)
9651 (ap->a_hba_tran->tran_tgt_private))->t_private,
9652 MAX_THROTTLE);
9653 rval = TRUE;
9654 break;
9655 case SCSI_CAP_QFULL_RETRIES:
9656 ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9657 t_private->m_qfull_retries = (uchar_t)value;
9658 rval = TRUE;
9659 break;
9660 case SCSI_CAP_QFULL_RETRY_INTERVAL:
9661 ((mptsas_tgt_private_t *)(ap->a_hba_tran->tran_tgt_private))->
9662 t_private->m_qfull_retry_interval =
9663 drv_usectohz(value * 1000);
9664 rval = TRUE;
9665 break;
9666 default:
9667 rval = UNDEFINED;
9668 break;
9670 mutex_exit(&mpt->m_mutex);
9671 return (rval);
9675 * Utility routine for mptsas_ifsetcap/ifgetcap
9677 /*ARGSUSED*/
9678 static int
9679 mptsas_scsi_capchk(char *cap, int tgtonly, int *cidxp)
9681 NDBG24(("mptsas_scsi_capchk: cap=%s", cap));
9683 if (!cap)
9684 return (FALSE);
9686 *cidxp = scsi_hba_lookup_capstr(cap);
9687 return (TRUE);
9690 static int
9691 mptsas_alloc_active_slots(mptsas_t *mpt, int flag)
9693 mptsas_slots_t *old_active = mpt->m_active;
9694 mptsas_slots_t *new_active;
9695 size_t size;
9698 * if there are active commands, then we cannot
9699 * change size of active slots array.
9701 ASSERT(mpt->m_ncmds == 0);
9703 size = MPTSAS_SLOTS_SIZE(mpt);
9704 new_active = kmem_zalloc(size, flag);
9705 if (new_active == NULL) {
9706 NDBG1(("new active alloc failed"));
9707 return (-1);
9710 * Since SMID 0 is reserved and the TM slot is reserved, the
9711 * number of slots that can be used at any one time is
9712 * m_max_requests - 2.
9714 new_active->m_n_normal = (mpt->m_max_requests - 2);
9715 new_active->m_size = size;
9716 new_active->m_rotor = 1;
9717 if (old_active)
9718 mptsas_free_active_slots(mpt);
9719 mpt->m_active = new_active;
9721 return (0);
9724 static void
9725 mptsas_free_active_slots(mptsas_t *mpt)
9727 mptsas_slots_t *active = mpt->m_active;
9728 size_t size;
9730 if (active == NULL)
9731 return;
9732 size = active->m_size;
9733 kmem_free(active, size);
9734 mpt->m_active = NULL;
9738 * Error logging, printing, and debug print routines.
9740 static char *mptsas_label = "mpt_sas";
9742 /*PRINTFLIKE3*/
9743 void
9744 mptsas_log(mptsas_t *mpt, int level, char *fmt, ...)
9746 dev_info_t *dev;
9747 va_list ap;
9749 if (mpt) {
9750 dev = mpt->m_dip;
9751 } else {
9752 dev = 0;
9755 mutex_enter(&mptsas_log_mutex);
9757 va_start(ap, fmt);
9758 (void) vsprintf(mptsas_log_buf, fmt, ap);
9759 va_end(ap);
9761 if (level == CE_CONT) {
9762 scsi_log(dev, mptsas_label, level, "%s\n", mptsas_log_buf);
9763 } else {
9764 scsi_log(dev, mptsas_label, level, "%s", mptsas_log_buf);
9767 mutex_exit(&mptsas_log_mutex);
9770 #ifdef MPTSAS_DEBUG
9772 * Use a circular buffer to log messages to private memory.
9773 * Increment idx atomically to minimize risk to miss lines.
9774 * It's fast and does not hold up the proceedings too much.
9776 static const size_t mptsas_dbglog_linecnt = MPTSAS_DBGLOG_LINECNT;
9777 static const size_t mptsas_dbglog_linelen = MPTSAS_DBGLOG_LINELEN;
9778 static char mptsas_dbglog_bufs[MPTSAS_DBGLOG_LINECNT][MPTSAS_DBGLOG_LINELEN];
9779 static uint32_t mptsas_dbglog_idx = 0;
9781 /*PRINTFLIKE1*/
9782 void
9783 mptsas_debug_log(char *fmt, ...)
9785 va_list ap;
9786 uint32_t idx;
9788 idx = atomic_inc_32_nv(&mptsas_dbglog_idx) &
9789 (mptsas_dbglog_linecnt - 1);
9791 va_start(ap, fmt);
9792 (void) vsnprintf(mptsas_dbglog_bufs[idx],
9793 mptsas_dbglog_linelen, fmt, ap);
9794 va_end(ap);
9797 /*PRINTFLIKE1*/
9798 void
9799 mptsas_printf(char *fmt, ...)
9801 dev_info_t *dev = 0;
9802 va_list ap;
9804 mutex_enter(&mptsas_log_mutex);
9806 va_start(ap, fmt);
9807 (void) vsprintf(mptsas_log_buf, fmt, ap);
9808 va_end(ap);
9810 #ifdef PROM_PRINTF
9811 prom_printf("%s:\t%s\n", mptsas_label, mptsas_log_buf);
9812 #else
9813 scsi_log(dev, mptsas_label, CE_CONT, "!%s\n", mptsas_log_buf);
9814 #endif
9815 mutex_exit(&mptsas_log_mutex);
9817 #endif
9820 * timeout handling
9822 static void
9823 mptsas_watch(void *arg)
9825 _NOTE(ARGUNUSED(arg))
9827 mptsas_t *mpt;
9828 uint32_t doorbell;
9830 NDBG30(("mptsas_watch"));
9832 rw_enter(&mptsas_global_rwlock, RW_READER);
9833 for (mpt = mptsas_head; mpt != (mptsas_t *)NULL; mpt = mpt->m_next) {
9835 mutex_enter(&mpt->m_mutex);
9837 /* Skip device if not powered on */
9838 if (mpt->m_options & MPTSAS_OPT_PM) {
9839 if (mpt->m_power_level == PM_LEVEL_D0) {
9840 (void) pm_busy_component(mpt->m_dip, 0);
9841 mpt->m_busy = 1;
9842 } else {
9843 mutex_exit(&mpt->m_mutex);
9844 continue;
9849 * Check if controller is in a FAULT state. If so, reset it.
9851 doorbell = ddi_get32(mpt->m_datap, &mpt->m_reg->Doorbell);
9852 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
9853 doorbell &= MPI2_DOORBELL_DATA_MASK;
9854 mptsas_log(mpt, CE_WARN, "MPT Firmware Fault, "
9855 "code: %04x", doorbell);
9856 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
9857 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
9858 mptsas_log(mpt, CE_WARN, "Reset failed"
9859 "after fault was detected");
9864 * For now, always call mptsas_watchsubr.
9866 mptsas_watchsubr(mpt);
9868 if (mpt->m_options & MPTSAS_OPT_PM) {
9869 mpt->m_busy = 0;
9870 (void) pm_idle_component(mpt->m_dip, 0);
9873 mutex_exit(&mpt->m_mutex);
9875 rw_exit(&mptsas_global_rwlock);
9877 mutex_enter(&mptsas_global_mutex);
9878 if (mptsas_timeouts_enabled)
9879 mptsas_timeout_id = timeout(mptsas_watch, NULL, mptsas_tick);
9880 mutex_exit(&mptsas_global_mutex);
9883 static void
9884 mptsas_watchsubr_tgt(mptsas_t *mpt, mptsas_target_t *ptgt, hrtime_t timestamp)
9886 mptsas_cmd_t *cmd;
9889 * If we were draining due to a qfull condition,
9890 * go back to full throttle.
9892 if ((ptgt->m_t_throttle < MAX_THROTTLE) &&
9893 (ptgt->m_t_throttle > HOLD_THROTTLE) &&
9894 (ptgt->m_t_ncmds < ptgt->m_t_throttle)) {
9895 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
9896 mptsas_restart_hba(mpt);
9899 cmd = TAILQ_LAST(&ptgt->m_active_cmdq, mptsas_active_cmdq);
9900 if (cmd == NULL)
9901 return;
9903 if (cmd->cmd_active_expiration <= timestamp) {
9905 * Earliest command timeout expired. Drain throttle.
9907 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
9910 * Check for remaining commands.
9912 cmd = TAILQ_FIRST(&ptgt->m_active_cmdq);
9913 if (cmd->cmd_active_expiration > timestamp) {
9915 * Wait for remaining commands to complete or
9916 * time out.
9918 NDBG23(("command timed out, pending drain"));
9919 return;
9923 * All command timeouts expired.
9925 mptsas_log(mpt, CE_NOTE, "Timeout of %d seconds "
9926 "expired with %d commands on target %d lun %d.",
9927 cmd->cmd_pkt->pkt_time, ptgt->m_t_ncmds,
9928 ptgt->m_devhdl, Lun(cmd));
9930 mptsas_cmd_timeout(mpt, ptgt);
9931 } else if (cmd->cmd_active_expiration <=
9932 timestamp + (hrtime_t)mptsas_scsi_watchdog_tick * NANOSEC) {
9933 NDBG23(("pending timeout"));
9934 mptsas_set_throttle(mpt, ptgt, DRAIN_THROTTLE);
9938 static void
9939 mptsas_watchsubr(mptsas_t *mpt)
9941 int i;
9942 mptsas_cmd_t *cmd;
9943 mptsas_target_t *ptgt = NULL;
9944 hrtime_t timestamp = gethrtime();
9946 ASSERT(MUTEX_HELD(&mpt->m_mutex));
9948 NDBG30(("mptsas_watchsubr: mpt=0x%p", (void *)mpt));
9950 #ifdef MPTSAS_TEST
9951 if (mptsas_enable_untagged) {
9952 mptsas_test_untagged++;
9954 #endif
9957 * Check for commands stuck in active slot
9958 * Account for TM requests, which use the last SMID.
9960 for (i = 0; i <= mpt->m_active->m_n_normal; i++) {
9961 if ((cmd = mpt->m_active->m_slot[i]) != NULL) {
9962 if (cmd->cmd_active_expiration <= timestamp) {
9963 if ((cmd->cmd_flags & CFLAG_CMDIOC) == 0) {
9965 * There seems to be a command stuck
9966 * in the active slot. Drain throttle.
9968 mptsas_set_throttle(mpt,
9969 cmd->cmd_tgt_addr,
9970 DRAIN_THROTTLE);
9971 } else if (cmd->cmd_flags &
9972 (CFLAG_PASSTHRU | CFLAG_CONFIG |
9973 CFLAG_FW_DIAG)) {
9975 * passthrough command timeout
9977 cmd->cmd_flags |= (CFLAG_FINISHED |
9978 CFLAG_TIMEOUT);
9979 cv_broadcast(&mpt->m_passthru_cv);
9980 cv_broadcast(&mpt->m_config_cv);
9981 cv_broadcast(&mpt->m_fw_diag_cv);
9987 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
9988 ptgt = refhash_next(mpt->m_targets, ptgt)) {
9989 mptsas_watchsubr_tgt(mpt, ptgt, timestamp);
9992 for (ptgt = refhash_first(mpt->m_tmp_targets); ptgt != NULL;
9993 ptgt = refhash_next(mpt->m_tmp_targets, ptgt)) {
9994 mptsas_watchsubr_tgt(mpt, ptgt, timestamp);
9999 * timeout recovery
10001 static void
10002 mptsas_cmd_timeout(mptsas_t *mpt, mptsas_target_t *ptgt)
10004 uint16_t devhdl;
10005 uint64_t sas_wwn;
10006 uint8_t phy;
10007 char wwn_str[MPTSAS_WWN_STRLEN];
10009 devhdl = ptgt->m_devhdl;
10010 sas_wwn = ptgt->m_addr.mta_wwn;
10011 phy = ptgt->m_phynum;
10012 if (sas_wwn == 0) {
10013 (void) sprintf(wwn_str, "p%x", phy);
10014 } else {
10015 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
10018 NDBG29(("mptsas_cmd_timeout: target=%d", devhdl));
10019 mptsas_log(mpt, CE_WARN, "Disconnected command timeout for "
10020 "target %d %s, enclosure %u", devhdl, wwn_str,
10021 ptgt->m_enclosure);
10024 * Abort all outstanding commands on the device.
10026 NDBG29(("mptsas_cmd_timeout: device reset"));
10027 if (mptsas_do_scsi_reset(mpt, devhdl) != TRUE) {
10028 mptsas_log(mpt, CE_WARN, "Target %d reset for command timeout "
10029 "recovery failed!", devhdl);
10034 * Device / Hotplug control
10036 static int
10037 mptsas_scsi_quiesce(dev_info_t *dip)
10039 mptsas_t *mpt;
10040 scsi_hba_tran_t *tran;
10042 tran = ddi_get_driver_private(dip);
10043 if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10044 return (-1);
10046 return (mptsas_quiesce_bus(mpt));
10049 static int
10050 mptsas_scsi_unquiesce(dev_info_t *dip)
10052 mptsas_t *mpt;
10053 scsi_hba_tran_t *tran;
10055 tran = ddi_get_driver_private(dip);
10056 if (tran == NULL || (mpt = TRAN2MPT(tran)) == NULL)
10057 return (-1);
10059 return (mptsas_unquiesce_bus(mpt));
10062 static int
10063 mptsas_quiesce_bus(mptsas_t *mpt)
10065 mptsas_target_t *ptgt = NULL;
10067 NDBG28(("mptsas_quiesce_bus"));
10068 mutex_enter(&mpt->m_mutex);
10070 /* Set all the throttles to zero */
10071 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10072 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10073 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10076 /* If there are any outstanding commands in the queue */
10077 if (mpt->m_ncmds) {
10078 mpt->m_softstate |= MPTSAS_SS_DRAINING;
10079 mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10080 mpt, (MPTSAS_QUIESCE_TIMEOUT * drv_usectohz(1000000)));
10081 if (cv_wait_sig(&mpt->m_cv, &mpt->m_mutex) == 0) {
10083 * Quiesce has been interrupted
10085 mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10086 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10087 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10088 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10090 mptsas_restart_hba(mpt);
10091 if (mpt->m_quiesce_timeid != 0) {
10092 timeout_id_t tid = mpt->m_quiesce_timeid;
10093 mpt->m_quiesce_timeid = 0;
10094 mutex_exit(&mpt->m_mutex);
10095 (void) untimeout(tid);
10096 return (-1);
10098 mutex_exit(&mpt->m_mutex);
10099 return (-1);
10100 } else {
10101 /* Bus has been quiesced */
10102 ASSERT(mpt->m_quiesce_timeid == 0);
10103 mpt->m_softstate &= ~MPTSAS_SS_DRAINING;
10104 mpt->m_softstate |= MPTSAS_SS_QUIESCED;
10105 mutex_exit(&mpt->m_mutex);
10106 return (0);
10109 /* Bus was not busy - QUIESCED */
10110 mutex_exit(&mpt->m_mutex);
10112 return (0);
10115 static int
10116 mptsas_unquiesce_bus(mptsas_t *mpt)
10118 mptsas_target_t *ptgt = NULL;
10120 NDBG28(("mptsas_unquiesce_bus"));
10121 mutex_enter(&mpt->m_mutex);
10122 mpt->m_softstate &= ~MPTSAS_SS_QUIESCED;
10123 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10124 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10125 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
10127 mptsas_restart_hba(mpt);
10128 mutex_exit(&mpt->m_mutex);
10129 return (0);
10132 static void
10133 mptsas_ncmds_checkdrain(void *arg)
10135 mptsas_t *mpt = arg;
10136 mptsas_target_t *ptgt = NULL;
10138 mutex_enter(&mpt->m_mutex);
10139 if (mpt->m_softstate & MPTSAS_SS_DRAINING) {
10140 mpt->m_quiesce_timeid = 0;
10141 if (mpt->m_ncmds == 0) {
10142 /* Command queue has been drained */
10143 cv_signal(&mpt->m_cv);
10144 } else {
10146 * The throttle may have been reset because
10147 * of a SCSI bus reset
10149 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
10150 ptgt = refhash_next(mpt->m_targets, ptgt)) {
10151 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
10154 mpt->m_quiesce_timeid = timeout(mptsas_ncmds_checkdrain,
10155 mpt, (MPTSAS_QUIESCE_TIMEOUT *
10156 drv_usectohz(1000000)));
10159 mutex_exit(&mpt->m_mutex);
10162 /*ARGSUSED*/
10163 static void
10164 mptsas_dump_cmd(mptsas_t *mpt, mptsas_cmd_t *cmd)
10166 int i;
10167 uint8_t *cp = (uchar_t *)cmd->cmd_pkt->pkt_cdbp;
10168 char buf[128];
10170 buf[0] = '\0';
10171 NDBG25(("?Cmd (0x%p) dump for Target %d Lun %d:\n", (void *)cmd,
10172 Tgt(cmd), Lun(cmd)));
10173 (void) sprintf(&buf[0], "\tcdb=[");
10174 for (i = 0; i < (int)cmd->cmd_cdblen; i++) {
10175 (void) sprintf(&buf[strlen(buf)], " 0x%x", *cp++);
10177 (void) sprintf(&buf[strlen(buf)], " ]");
10178 NDBG25(("?%s\n", buf));
10179 NDBG25(("?pkt_flags=0x%x pkt_statistics=0x%x pkt_state=0x%x\n",
10180 cmd->cmd_pkt->pkt_flags, cmd->cmd_pkt->pkt_statistics,
10181 cmd->cmd_pkt->pkt_state));
10182 NDBG25(("?pkt_scbp=0x%x cmd_flags=0x%x\n", cmd->cmd_pkt->pkt_scbp ?
10183 *(cmd->cmd_pkt->pkt_scbp) : 0, cmd->cmd_flags));
10186 static void
10187 mptsas_passthru_sge(ddi_acc_handle_t acc_hdl, mptsas_pt_request_t *pt,
10188 pMpi2SGESimple64_t sgep)
10190 uint32_t sge_flags;
10191 uint32_t data_size, dataout_size;
10192 ddi_dma_cookie_t data_cookie;
10193 ddi_dma_cookie_t dataout_cookie;
10195 data_size = pt->data_size;
10196 dataout_size = pt->dataout_size;
10197 data_cookie = pt->data_cookie;
10198 dataout_cookie = pt->dataout_cookie;
10200 if (dataout_size) {
10201 sge_flags = dataout_size |
10202 ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10203 MPI2_SGE_FLAGS_END_OF_BUFFER |
10204 MPI2_SGE_FLAGS_HOST_TO_IOC |
10205 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10206 MPI2_SGE_FLAGS_SHIFT);
10207 ddi_put32(acc_hdl, &sgep->FlagsLength, sge_flags);
10208 ddi_put32(acc_hdl, &sgep->Address.Low,
10209 (uint32_t)(dataout_cookie.dmac_laddress &
10210 0xffffffffull));
10211 ddi_put32(acc_hdl, &sgep->Address.High,
10212 (uint32_t)(dataout_cookie.dmac_laddress
10213 >> 32));
10214 sgep++;
10216 sge_flags = data_size;
10217 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
10218 MPI2_SGE_FLAGS_LAST_ELEMENT |
10219 MPI2_SGE_FLAGS_END_OF_BUFFER |
10220 MPI2_SGE_FLAGS_END_OF_LIST |
10221 MPI2_SGE_FLAGS_64_BIT_ADDRESSING) <<
10222 MPI2_SGE_FLAGS_SHIFT);
10223 if (pt->direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10224 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_HOST_TO_IOC) <<
10225 MPI2_SGE_FLAGS_SHIFT);
10226 } else {
10227 sge_flags |= ((uint32_t)(MPI2_SGE_FLAGS_IOC_TO_HOST) <<
10228 MPI2_SGE_FLAGS_SHIFT);
10230 ddi_put32(acc_hdl, &sgep->FlagsLength,
10231 sge_flags);
10232 ddi_put32(acc_hdl, &sgep->Address.Low,
10233 (uint32_t)(data_cookie.dmac_laddress &
10234 0xffffffffull));
10235 ddi_put32(acc_hdl, &sgep->Address.High,
10236 (uint32_t)(data_cookie.dmac_laddress >> 32));
10239 static void
10240 mptsas_passthru_ieee_sge(ddi_acc_handle_t acc_hdl, mptsas_pt_request_t *pt,
10241 pMpi2IeeeSgeSimple64_t ieeesgep)
10243 uint8_t sge_flags;
10244 uint32_t data_size, dataout_size;
10245 ddi_dma_cookie_t data_cookie;
10246 ddi_dma_cookie_t dataout_cookie;
10248 data_size = pt->data_size;
10249 dataout_size = pt->dataout_size;
10250 data_cookie = pt->data_cookie;
10251 dataout_cookie = pt->dataout_cookie;
10253 sge_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
10254 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR);
10255 if (dataout_size) {
10256 ddi_put32(acc_hdl, &ieeesgep->Length, dataout_size);
10257 ddi_put32(acc_hdl, &ieeesgep->Address.Low,
10258 (uint32_t)(dataout_cookie.dmac_laddress &
10259 0xffffffffull));
10260 ddi_put32(acc_hdl, &ieeesgep->Address.High,
10261 (uint32_t)(dataout_cookie.dmac_laddress >> 32));
10262 ddi_put8(acc_hdl, &ieeesgep->Flags, sge_flags);
10263 ieeesgep++;
10265 sge_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
10266 ddi_put32(acc_hdl, &ieeesgep->Length, data_size);
10267 ddi_put32(acc_hdl, &ieeesgep->Address.Low,
10268 (uint32_t)(data_cookie.dmac_laddress & 0xffffffffull));
10269 ddi_put32(acc_hdl, &ieeesgep->Address.High,
10270 (uint32_t)(data_cookie.dmac_laddress >> 32));
10271 ddi_put8(acc_hdl, &ieeesgep->Flags, sge_flags);
10274 static void
10275 mptsas_start_passthru(mptsas_t *mpt, mptsas_cmd_t *cmd)
10277 caddr_t memp;
10278 pMPI2RequestHeader_t request_hdrp;
10279 struct scsi_pkt *pkt = cmd->cmd_pkt;
10280 mptsas_pt_request_t *pt = pkt->pkt_ha_private;
10281 uint32_t request_size;
10282 uint32_t i;
10283 uint64_t request_desc = 0;
10284 uint8_t desc_type;
10285 uint16_t SMID;
10286 uint8_t *request, function;
10287 ddi_dma_handle_t dma_hdl = mpt->m_dma_req_frame_hdl;
10288 ddi_acc_handle_t acc_hdl = mpt->m_acc_req_frame_hdl;
10290 desc_type = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
10292 request = pt->request;
10293 request_size = pt->request_size;
10295 SMID = cmd->cmd_slot;
10298 * Store the passthrough message in memory location
10299 * corresponding to our slot number
10301 memp = mpt->m_req_frame + (mpt->m_req_frame_size * SMID);
10302 request_hdrp = (pMPI2RequestHeader_t)memp;
10303 bzero(memp, mpt->m_req_frame_size);
10305 for (i = 0; i < request_size; i++) {
10306 bcopy(request + i, memp + i, 1);
10309 NDBG15(("mptsas_start_passthru: Func 0x%x, MsgFlags 0x%x, "
10310 "size=%d, in %d, out %d, SMID %d", request_hdrp->Function,
10311 request_hdrp->MsgFlags, request_size,
10312 pt->data_size, pt->dataout_size, SMID));
10315 * Add an SGE, even if the length is zero.
10317 if (mpt->m_MPI25 && pt->simple == 0) {
10318 mptsas_passthru_ieee_sge(acc_hdl, pt,
10319 (pMpi2IeeeSgeSimple64_t)
10320 ((uint8_t *)request_hdrp + pt->sgl_offset));
10321 } else {
10322 mptsas_passthru_sge(acc_hdl, pt,
10323 (pMpi2SGESimple64_t)
10324 ((uint8_t *)request_hdrp + pt->sgl_offset));
10327 function = request_hdrp->Function;
10328 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10329 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10330 pMpi2SCSIIORequest_t scsi_io_req;
10331 caddr_t arsbuf;
10332 uint8_t ars_size;
10333 uint32_t ars_dmaaddrlow;
10335 NDBG15(("mptsas_start_passthru: Is SCSI IO Req"));
10336 scsi_io_req = (pMpi2SCSIIORequest_t)request_hdrp;
10338 if (cmd->cmd_extrqslen != 0) {
10340 * Mapping of the buffer was done in
10341 * mptsas_do_passthru().
10342 * Calculate the DMA address with the same offset.
10344 arsbuf = cmd->cmd_arq_buf;
10345 ars_size = cmd->cmd_extrqslen;
10346 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
10347 ((uintptr_t)arsbuf - (uintptr_t)mpt->m_req_sense)) &
10348 0xffffffffu;
10349 } else {
10350 arsbuf = mpt->m_req_sense +
10351 (mpt->m_req_sense_size * (SMID-1));
10352 cmd->cmd_arq_buf = arsbuf;
10353 ars_size = mpt->m_req_sense_size;
10354 ars_dmaaddrlow = (mpt->m_req_sense_dma_addr +
10355 (mpt->m_req_sense_size * (SMID-1))) &
10356 0xffffffffu;
10358 bzero(arsbuf, ars_size);
10360 ddi_put8(acc_hdl, &scsi_io_req->SenseBufferLength, ars_size);
10361 ddi_put32(acc_hdl, &scsi_io_req->SenseBufferLowAddress,
10362 ars_dmaaddrlow);
10365 * Put SGE for data and data_out buffer at the end of
10366 * scsi_io_request message header.(64 bytes in total)
10367 * Set SGLOffset0 value
10369 ddi_put8(acc_hdl, &scsi_io_req->SGLOffset0,
10370 offsetof(MPI2_SCSI_IO_REQUEST, SGL) / 4);
10373 * Setup descriptor info. RAID passthrough must use the
10374 * default request descriptor which is already set, so if this
10375 * is a SCSI IO request, change the descriptor to SCSI IO.
10377 if (function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
10378 desc_type = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
10379 request_desc = ((uint64_t)ddi_get16(acc_hdl,
10380 &scsi_io_req->DevHandle) << 48);
10382 (void) ddi_dma_sync(mpt->m_dma_req_sense_hdl, 0, 0,
10383 DDI_DMA_SYNC_FORDEV);
10387 * We must wait till the message has been completed before
10388 * beginning the next message so we wait for this one to
10389 * finish.
10391 (void) ddi_dma_sync(dma_hdl, 0, 0, DDI_DMA_SYNC_FORDEV);
10392 request_desc |= (SMID << 16) + desc_type;
10393 cmd->cmd_rfm = 0;
10394 MPTSAS_START_CMD(mpt, request_desc);
10395 if ((mptsas_check_dma_handle(dma_hdl) != DDI_SUCCESS) ||
10396 (mptsas_check_acc_handle(acc_hdl) != DDI_SUCCESS)) {
10397 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
10401 typedef void (mptsas_pre_f)(mptsas_t *, mptsas_pt_request_t *);
10402 static mptsas_pre_f mpi_pre_ioc_facts;
10403 static mptsas_pre_f mpi_pre_port_facts;
10404 static mptsas_pre_f mpi_pre_fw_download;
10405 static mptsas_pre_f mpi_pre_fw_25_download;
10406 static mptsas_pre_f mpi_pre_fw_upload;
10407 static mptsas_pre_f mpi_pre_fw_25_upload;
10408 static mptsas_pre_f mpi_pre_sata_passthrough;
10409 static mptsas_pre_f mpi_pre_smp_passthrough;
10410 static mptsas_pre_f mpi_pre_config;
10411 static mptsas_pre_f mpi_pre_sas_io_unit_control;
10412 static mptsas_pre_f mpi_pre_scsi_io_req;
10415 * Prepare the pt for a SAS2 FW_DOWNLOAD request.
10417 static void
10418 mpi_pre_fw_download(mptsas_t *mpt, mptsas_pt_request_t *pt)
10420 pMpi2FWDownloadTCSGE_t tcsge;
10421 pMpi2FWDownloadRequest req;
10424 * If SAS3, call separate function.
10426 if (mpt->m_MPI25) {
10427 mpi_pre_fw_25_download(mpt, pt);
10428 return;
10432 * User requests should come in with the Transaction
10433 * context element where the SGL will go. Putting the
10434 * SGL after that seems to work, but don't really know
10435 * why. Other drivers tend to create an extra SGL and
10436 * refer to the TCE through that.
10438 req = (pMpi2FWDownloadRequest)pt->request;
10439 tcsge = (pMpi2FWDownloadTCSGE_t)&req->SGL;
10440 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10441 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10442 mptsas_log(mpt, CE_WARN, "FW Download tce invalid!");
10445 pt->sgl_offset = offsetof(MPI2_FW_DOWNLOAD_REQUEST, SGL) +
10446 sizeof (*tcsge);
10447 if (pt->request_size != pt->sgl_offset)
10448 NDBG15(("mpi_pre_fw_download(): Incorrect req size, "
10449 "0x%x, should be 0x%x, dataoutsz 0x%x",
10450 (int)pt->request_size, (int)pt->sgl_offset,
10451 (int)pt->dataout_size));
10452 if (pt->data_size < sizeof (MPI2_FW_DOWNLOAD_REPLY))
10453 NDBG15(("mpi_pre_fw_download(): Incorrect rep size, "
10454 "0x%x, should be 0x%x", pt->data_size,
10455 (int)sizeof (MPI2_FW_DOWNLOAD_REPLY)));
10459 * Prepare the pt for a SAS3 FW_DOWNLOAD request.
10461 static void
10462 mpi_pre_fw_25_download(mptsas_t *mpt, mptsas_pt_request_t *pt)
10464 pMpi2FWDownloadTCSGE_t tcsge;
10465 pMpi2FWDownloadRequest req2;
10466 pMpi25FWDownloadRequest req25;
10469 * User requests should come in with the Transaction
10470 * context element where the SGL will go. The new firmware
10471 * Doesn't use TCE and has space in the main request for
10472 * this information. So move to the right place.
10474 req2 = (pMpi2FWDownloadRequest)pt->request;
10475 req25 = (pMpi25FWDownloadRequest)pt->request;
10476 tcsge = (pMpi2FWDownloadTCSGE_t)&req2->SGL;
10477 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10478 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10479 mptsas_log(mpt, CE_WARN, "FW Download tce invalid!");
10481 req25->ImageOffset = tcsge->ImageOffset;
10482 req25->ImageSize = tcsge->ImageSize;
10484 pt->sgl_offset = offsetof(MPI25_FW_DOWNLOAD_REQUEST, SGL);
10485 if (pt->request_size != pt->sgl_offset)
10486 NDBG15(("mpi_pre_fw_25_download(): Incorrect req size, "
10487 "0x%x, should be 0x%x, dataoutsz 0x%x",
10488 pt->request_size, pt->sgl_offset,
10489 pt->dataout_size));
10490 if (pt->data_size < sizeof (MPI2_FW_DOWNLOAD_REPLY))
10491 NDBG15(("mpi_pre_fw_25_download(): Incorrect rep size, "
10492 "0x%x, should be 0x%x", pt->data_size,
10493 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10497 * Prepare the pt for a SAS2 FW_UPLOAD request.
10499 static void
10500 mpi_pre_fw_upload(mptsas_t *mpt, mptsas_pt_request_t *pt)
10502 pMpi2FWUploadTCSGE_t tcsge;
10503 pMpi2FWUploadRequest_t req;
10506 * If SAS3, call separate function.
10508 if (mpt->m_MPI25) {
10509 mpi_pre_fw_25_upload(mpt, pt);
10510 return;
10514 * User requests should come in with the Transaction
10515 * context element where the SGL will go. Putting the
10516 * SGL after that seems to work, but don't really know
10517 * why. Other drivers tend to create an extra SGL and
10518 * refer to the TCE through that.
10520 req = (pMpi2FWUploadRequest_t)pt->request;
10521 tcsge = (pMpi2FWUploadTCSGE_t)&req->SGL;
10522 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10523 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10524 mptsas_log(mpt, CE_WARN, "FW Upload tce invalid!");
10527 pt->sgl_offset = offsetof(MPI2_FW_UPLOAD_REQUEST, SGL) +
10528 sizeof (*tcsge);
10529 if (pt->request_size != pt->sgl_offset)
10530 NDBG15(("mpi_pre_fw_upload(): Incorrect req size, "
10531 "0x%x, should be 0x%x, dataoutsz 0x%x",
10532 pt->request_size, pt->sgl_offset,
10533 pt->dataout_size));
10534 if (pt->data_size < sizeof (MPI2_FW_UPLOAD_REPLY))
10535 NDBG15(("mpi_pre_fw_upload(): Incorrect rep size, "
10536 "0x%x, should be 0x%x", pt->data_size,
10537 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10541 * Prepare the pt a SAS3 FW_UPLOAD request.
10543 static void
10544 mpi_pre_fw_25_upload(mptsas_t *mpt, mptsas_pt_request_t *pt)
10546 pMpi2FWUploadTCSGE_t tcsge;
10547 pMpi2FWUploadRequest_t req2;
10548 pMpi25FWUploadRequest_t req25;
10551 * User requests should come in with the Transaction
10552 * context element where the SGL will go. The new firmware
10553 * Doesn't use TCE and has space in the main request for
10554 * this information. So move to the right place.
10556 req2 = (pMpi2FWUploadRequest_t)pt->request;
10557 req25 = (pMpi25FWUploadRequest_t)pt->request;
10558 tcsge = (pMpi2FWUploadTCSGE_t)&req2->SGL;
10559 if (tcsge->ContextSize != 0 || tcsge->DetailsLength != 12 ||
10560 tcsge->Flags != MPI2_SGE_FLAGS_TRANSACTION_ELEMENT) {
10561 mptsas_log(mpt, CE_WARN, "FW Upload tce invalid!");
10563 req25->ImageOffset = tcsge->ImageOffset;
10564 req25->ImageSize = tcsge->ImageSize;
10566 pt->sgl_offset = offsetof(MPI25_FW_UPLOAD_REQUEST, SGL);
10567 if (pt->request_size != pt->sgl_offset)
10568 NDBG15(("mpi_pre_fw_25_upload(): Incorrect req size, "
10569 "0x%x, should be 0x%x, dataoutsz 0x%x",
10570 pt->request_size, pt->sgl_offset,
10571 pt->dataout_size));
10572 if (pt->data_size < sizeof (MPI2_FW_UPLOAD_REPLY))
10573 NDBG15(("mpi_pre_fw_25_upload(): Incorrect rep size, "
10574 "0x%x, should be 0x%x", pt->data_size,
10575 (int)sizeof (MPI2_FW_UPLOAD_REPLY)));
10579 * Prepare the pt for an IOC_FACTS request.
10581 static void
10582 mpi_pre_ioc_facts(mptsas_t *mpt, mptsas_pt_request_t *pt)
10584 _NOTE(ARGUNUSED(mpt))
10586 if (pt->request_size != sizeof (MPI2_IOC_FACTS_REQUEST))
10587 NDBG15(("mpi_pre_ioc_facts(): Incorrect req size, "
10588 "0x%x, should be 0x%x, dataoutsz 0x%x",
10589 pt->request_size,
10590 (int)sizeof (MPI2_IOC_FACTS_REQUEST),
10591 pt->dataout_size));
10592 if (pt->data_size != sizeof (MPI2_IOC_FACTS_REPLY))
10593 NDBG15(("mpi_pre_ioc_facts(): Incorrect rep size, "
10594 "0x%x, should be 0x%x", pt->data_size,
10595 (int)sizeof (MPI2_IOC_FACTS_REPLY)));
10596 pt->sgl_offset = (uint16_t)pt->request_size;
10600 * Prepare the pt for a PORT_FACTS request.
10602 static void
10603 mpi_pre_port_facts(mptsas_t *mpt, mptsas_pt_request_t *pt)
10605 _NOTE(ARGUNUSED(mpt))
10607 if (pt->request_size != sizeof (MPI2_PORT_FACTS_REQUEST))
10608 NDBG15(("mpi_pre_port_facts(): Incorrect req size, "
10609 "0x%x, should be 0x%x, dataoutsz 0x%x",
10610 pt->request_size,
10611 (int)sizeof (MPI2_PORT_FACTS_REQUEST),
10612 pt->dataout_size));
10613 if (pt->data_size != sizeof (MPI2_PORT_FACTS_REPLY))
10614 NDBG15(("mpi_pre_port_facts(): Incorrect rep size, "
10615 "0x%x, should be 0x%x", pt->data_size,
10616 (int)sizeof (MPI2_PORT_FACTS_REPLY)));
10617 pt->sgl_offset = (uint16_t)pt->request_size;
10621 * Prepare pt for a SATA_PASSTHROUGH request.
10623 static void
10624 mpi_pre_sata_passthrough(mptsas_t *mpt, mptsas_pt_request_t *pt)
10626 _NOTE(ARGUNUSED(mpt))
10628 pt->sgl_offset = offsetof(MPI2_SATA_PASSTHROUGH_REQUEST, SGL);
10629 if (pt->request_size != pt->sgl_offset)
10630 NDBG15(("mpi_pre_sata_passthrough(): Incorrect req size, "
10631 "0x%x, should be 0x%x, dataoutsz 0x%x",
10632 pt->request_size, pt->sgl_offset,
10633 pt->dataout_size));
10634 if (pt->data_size != sizeof (MPI2_SATA_PASSTHROUGH_REPLY))
10635 NDBG15(("mpi_pre_sata_passthrough(): Incorrect rep size, "
10636 "0x%x, should be 0x%x", pt->data_size,
10637 (int)sizeof (MPI2_SATA_PASSTHROUGH_REPLY)));
10640 static void
10641 mpi_pre_smp_passthrough(mptsas_t *mpt, mptsas_pt_request_t *pt)
10643 _NOTE(ARGUNUSED(mpt))
10645 pt->sgl_offset = offsetof(MPI2_SMP_PASSTHROUGH_REQUEST, SGL);
10646 if (pt->request_size != pt->sgl_offset)
10647 NDBG15(("mpi_pre_smp_passthrough(): Incorrect req size, "
10648 "0x%x, should be 0x%x, dataoutsz 0x%x",
10649 pt->request_size, pt->sgl_offset,
10650 pt->dataout_size));
10651 if (pt->data_size != sizeof (MPI2_SMP_PASSTHROUGH_REPLY))
10652 NDBG15(("mpi_pre_smp_passthrough(): Incorrect rep size, "
10653 "0x%x, should be 0x%x", pt->data_size,
10654 (int)sizeof (MPI2_SMP_PASSTHROUGH_REPLY)));
10658 * Prepare pt for a CONFIG request.
10660 static void
10661 mpi_pre_config(mptsas_t *mpt, mptsas_pt_request_t *pt)
10663 _NOTE(ARGUNUSED(mpt))
10665 pt->sgl_offset = offsetof(MPI2_CONFIG_REQUEST, PageBufferSGE);
10666 if (pt->request_size != pt->sgl_offset)
10667 NDBG15(("mpi_pre_config(): Incorrect req size, 0x%x, "
10668 "should be 0x%x, dataoutsz 0x%x", pt->request_size,
10669 pt->sgl_offset, pt->dataout_size));
10670 if (pt->data_size != sizeof (MPI2_CONFIG_REPLY))
10671 NDBG15(("mpi_pre_config(): Incorrect rep size, 0x%x, "
10672 "should be 0x%x", pt->data_size,
10673 (int)sizeof (MPI2_CONFIG_REPLY)));
10674 pt->simple = 1;
10678 * Prepare pt for a SCSI_IO_REQ request.
10680 static void
10681 mpi_pre_scsi_io_req(mptsas_t *mpt, mptsas_pt_request_t *pt)
10683 _NOTE(ARGUNUSED(mpt))
10685 pt->sgl_offset = offsetof(MPI2_SCSI_IO_REQUEST, SGL);
10686 if (pt->request_size != pt->sgl_offset)
10687 NDBG15(("mpi_pre_config(): Incorrect req size, 0x%x, "
10688 "should be 0x%x, dataoutsz 0x%x", pt->request_size,
10689 pt->sgl_offset,
10690 pt->dataout_size));
10691 if (pt->data_size != sizeof (MPI2_SCSI_IO_REPLY))
10692 NDBG15(("mpi_pre_config(): Incorrect rep size, 0x%x, "
10693 "should be 0x%x", pt->data_size,
10694 (int)sizeof (MPI2_SCSI_IO_REPLY)));
10698 * Prepare the mptsas_cmd for a SAS_IO_UNIT_CONTROL request.
10700 static void
10701 mpi_pre_sas_io_unit_control(mptsas_t *mpt, mptsas_pt_request_t *pt)
10703 _NOTE(ARGUNUSED(mpt))
10705 pt->sgl_offset = (uint16_t)pt->request_size;
10709 * A set of functions to prepare an mptsas_cmd for the various
10710 * supported requests.
10712 static struct mptsas_func {
10713 U8 Function;
10714 char *Name;
10715 mptsas_pre_f *f_pre;
10716 } mptsas_func_list[] = {
10717 { MPI2_FUNCTION_IOC_FACTS, "IOC_FACTS", mpi_pre_ioc_facts },
10718 { MPI2_FUNCTION_PORT_FACTS, "PORT_FACTS", mpi_pre_port_facts },
10719 { MPI2_FUNCTION_FW_DOWNLOAD, "FW_DOWNLOAD", mpi_pre_fw_download },
10720 { MPI2_FUNCTION_FW_UPLOAD, "FW_UPLOAD", mpi_pre_fw_upload },
10721 { MPI2_FUNCTION_SATA_PASSTHROUGH, "SATA_PASSTHROUGH",
10722 mpi_pre_sata_passthrough },
10723 { MPI2_FUNCTION_SMP_PASSTHROUGH, "SMP_PASSTHROUGH",
10724 mpi_pre_smp_passthrough},
10725 { MPI2_FUNCTION_SCSI_IO_REQUEST, "SCSI_IO_REQUEST",
10726 mpi_pre_scsi_io_req},
10727 { MPI2_FUNCTION_CONFIG, "CONFIG", mpi_pre_config},
10728 { MPI2_FUNCTION_SAS_IO_UNIT_CONTROL, "SAS_IO_UNIT_CONTROL",
10729 mpi_pre_sas_io_unit_control },
10730 { 0xFF, NULL, NULL } /* list end */
10733 static void
10734 mptsas_prep_sgl_offset(mptsas_t *mpt, mptsas_pt_request_t *pt)
10736 pMPI2RequestHeader_t hdr;
10737 struct mptsas_func *f;
10739 hdr = (pMPI2RequestHeader_t)pt->request;
10741 for (f = mptsas_func_list; f->f_pre != NULL; f++) {
10742 if (hdr->Function == f->Function) {
10743 f->f_pre(mpt, pt);
10744 NDBG15(("mptsas_prep_sgl_offset: Function %s,"
10745 " sgl_offset 0x%x", f->Name,
10746 pt->sgl_offset));
10747 return;
10750 NDBG15(("mptsas_prep_sgl_offset: Unknown Function 0x%02x,"
10751 " returning req_size 0x%x for sgl_offset",
10752 hdr->Function, pt->request_size));
10753 pt->sgl_offset = (uint16_t)pt->request_size;
10757 static int
10758 mptsas_do_passthru(mptsas_t *mpt, uint8_t *request, uint8_t *reply,
10759 uint8_t *data, uint32_t request_size, uint32_t reply_size,
10760 uint32_t data_size, uint32_t direction, uint8_t *dataout,
10761 uint32_t dataout_size, short timeout, int mode)
10763 mptsas_pt_request_t pt;
10764 mptsas_dma_alloc_state_t data_dma_state;
10765 mptsas_dma_alloc_state_t dataout_dma_state;
10766 caddr_t memp;
10767 mptsas_cmd_t *cmd = NULL;
10768 struct scsi_pkt *pkt;
10769 uint32_t reply_len = 0, sense_len = 0;
10770 pMPI2RequestHeader_t request_hdrp;
10771 pMPI2RequestHeader_t request_msg;
10772 pMPI2DefaultReply_t reply_msg;
10773 Mpi2SCSIIOReply_t rep_msg;
10774 int rvalue;
10775 int i, status = 0, pt_flags = 0, rv = 0;
10776 uint8_t function;
10778 ASSERT(mutex_owned(&mpt->m_mutex));
10780 reply_msg = (pMPI2DefaultReply_t)(&rep_msg);
10781 bzero(reply_msg, sizeof (MPI2_DEFAULT_REPLY));
10782 request_msg = kmem_zalloc(request_size, KM_SLEEP);
10784 mutex_exit(&mpt->m_mutex);
10786 * copy in the request buffer since it could be used by
10787 * another thread when the pt request into waitq
10789 if (ddi_copyin(request, request_msg, request_size, mode)) {
10790 mutex_enter(&mpt->m_mutex);
10791 status = EFAULT;
10792 mptsas_log(mpt, CE_WARN, "failed to copy request data");
10793 goto out;
10795 NDBG27(("mptsas_do_passthru: mode 0x%x, size 0x%x, Func 0x%x",
10796 mode, request_size, request_msg->Function));
10797 mutex_enter(&mpt->m_mutex);
10799 function = request_msg->Function;
10800 if (function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
10801 pMpi2SCSITaskManagementRequest_t task;
10802 task = (pMpi2SCSITaskManagementRequest_t)request_msg;
10803 mptsas_setup_bus_reset_delay(mpt);
10804 rv = mptsas_ioc_task_management(mpt, task->TaskType,
10805 task->DevHandle, (int)task->LUN[1], reply, reply_size,
10806 mode);
10808 if (rv != TRUE) {
10809 status = EIO;
10810 mptsas_log(mpt, CE_WARN, "task management failed");
10812 goto out;
10815 if (data_size != 0) {
10816 data_dma_state.size = data_size;
10817 if (mptsas_dma_alloc(mpt, &data_dma_state) != DDI_SUCCESS) {
10818 status = ENOMEM;
10819 mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10820 "resource");
10821 goto out;
10823 pt_flags |= MPTSAS_DATA_ALLOCATED;
10824 if (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE) {
10825 mutex_exit(&mpt->m_mutex);
10826 for (i = 0; i < data_size; i++) {
10827 if (ddi_copyin(data + i, (uint8_t *)
10828 data_dma_state.memp + i, 1, mode)) {
10829 mutex_enter(&mpt->m_mutex);
10830 status = EFAULT;
10831 mptsas_log(mpt, CE_WARN, "failed to "
10832 "copy read data");
10833 goto out;
10836 mutex_enter(&mpt->m_mutex);
10838 } else {
10839 bzero(&data_dma_state, sizeof (data_dma_state));
10842 if (dataout_size != 0) {
10843 dataout_dma_state.size = dataout_size;
10844 if (mptsas_dma_alloc(mpt, &dataout_dma_state) != DDI_SUCCESS) {
10845 status = ENOMEM;
10846 mptsas_log(mpt, CE_WARN, "failed to alloc DMA "
10847 "resource");
10848 goto out;
10850 pt_flags |= MPTSAS_DATAOUT_ALLOCATED;
10851 mutex_exit(&mpt->m_mutex);
10852 for (i = 0; i < dataout_size; i++) {
10853 if (ddi_copyin(dataout + i, (uint8_t *)
10854 dataout_dma_state.memp + i, 1, mode)) {
10855 mutex_enter(&mpt->m_mutex);
10856 mptsas_log(mpt, CE_WARN, "failed to copy out"
10857 " data");
10858 status = EFAULT;
10859 goto out;
10862 mutex_enter(&mpt->m_mutex);
10863 } else {
10864 bzero(&dataout_dma_state, sizeof (dataout_dma_state));
10867 if ((rvalue = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
10868 status = EAGAIN;
10869 mptsas_log(mpt, CE_NOTE, "event ack command pool is full");
10870 goto out;
10872 pt_flags |= MPTSAS_REQUEST_POOL_CMD;
10874 bzero((caddr_t)cmd, sizeof (*cmd));
10875 bzero((caddr_t)pkt, scsi_pkt_size());
10876 bzero((caddr_t)&pt, sizeof (pt));
10878 cmd->ioc_cmd_slot = (uint32_t)(rvalue);
10880 pt.request = (uint8_t *)request_msg;
10881 pt.direction = direction;
10882 pt.simple = 0;
10883 pt.request_size = request_size;
10884 pt.data_size = data_size;
10885 pt.dataout_size = dataout_size;
10886 pt.data_cookie = data_dma_state.cookie;
10887 pt.dataout_cookie = dataout_dma_state.cookie;
10888 mptsas_prep_sgl_offset(mpt, &pt);
10891 * Form a blank cmd/pkt to store the acknowledgement message
10893 pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb[0];
10894 pkt->pkt_scbp = (opaque_t)&cmd->cmd_scb;
10895 pkt->pkt_ha_private = (opaque_t)&pt;
10896 pkt->pkt_flags = FLAG_HEAD;
10897 pkt->pkt_time = timeout;
10898 cmd->cmd_pkt = pkt;
10899 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_PASSTHRU;
10901 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
10902 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
10903 uint8_t com, cdb_group_id;
10904 boolean_t ret;
10906 pkt->pkt_cdbp = ((pMpi2SCSIIORequest_t)request_msg)->CDB.CDB32;
10907 com = pkt->pkt_cdbp[0];
10908 cdb_group_id = CDB_GROUPID(com);
10909 switch (cdb_group_id) {
10910 case CDB_GROUPID_0: cmd->cmd_cdblen = CDB_GROUP0; break;
10911 case CDB_GROUPID_1: cmd->cmd_cdblen = CDB_GROUP1; break;
10912 case CDB_GROUPID_2: cmd->cmd_cdblen = CDB_GROUP2; break;
10913 case CDB_GROUPID_4: cmd->cmd_cdblen = CDB_GROUP4; break;
10914 case CDB_GROUPID_5: cmd->cmd_cdblen = CDB_GROUP5; break;
10915 default:
10916 NDBG27(("mptsas_do_passthru: SCSI_IO, reserved "
10917 "CDBGROUP 0x%x requested!", cdb_group_id));
10918 break;
10921 reply_len = sizeof (MPI2_SCSI_IO_REPLY);
10922 sense_len = reply_size - reply_len;
10923 ret = mptsas_cmdarqsize(mpt, cmd, sense_len, KM_SLEEP);
10924 VERIFY(ret == B_TRUE);
10925 } else {
10926 reply_len = reply_size;
10927 sense_len = 0;
10930 NDBG27(("mptsas_do_passthru: %s, dsz 0x%x, dosz 0x%x, replen 0x%x, "
10931 "snslen 0x%x",
10932 (direction == MPTSAS_PASS_THRU_DIRECTION_WRITE)?"Write":"Read",
10933 data_size, dataout_size, reply_len, sense_len));
10936 * Save the command in a slot
10938 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
10940 * Once passthru command get slot, set cmd_flags
10941 * CFLAG_PREPARED.
10943 cmd->cmd_flags |= CFLAG_PREPARED;
10944 mptsas_start_passthru(mpt, cmd);
10945 } else {
10946 mptsas_waitq_add(mpt, cmd);
10949 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
10950 cv_wait(&mpt->m_passthru_cv, &mpt->m_mutex);
10953 NDBG27(("mptsas_do_passthru: Cmd complete, flags 0x%x, rfm 0x%x "
10954 "pktreason 0x%x", cmd->cmd_flags, cmd->cmd_rfm,
10955 pkt->pkt_reason));
10957 if (cmd->cmd_flags & CFLAG_PREPARED) {
10958 memp = mpt->m_req_frame + (mpt->m_req_frame_size *
10959 cmd->cmd_slot);
10960 request_hdrp = (pMPI2RequestHeader_t)memp;
10963 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
10964 status = ETIMEDOUT;
10965 mptsas_log(mpt, CE_WARN, "passthrough command timeout");
10966 pt_flags |= MPTSAS_CMD_TIMEOUT;
10967 goto out;
10970 if (cmd->cmd_rfm) {
10972 * cmd_rfm is zero means the command reply is a CONTEXT
10973 * reply and no PCI Write to post the free reply SMFA
10974 * because no reply message frame is used.
10975 * cmd_rfm is non-zero means the reply is a ADDRESS
10976 * reply and reply message frame is used.
10978 pt_flags |= MPTSAS_ADDRESS_REPLY;
10979 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
10980 DDI_DMA_SYNC_FORCPU);
10981 reply_msg = (pMPI2DefaultReply_t)
10982 (mpt->m_reply_frame + (cmd->cmd_rfm -
10983 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
10986 mptsas_fma_check(mpt, cmd);
10987 if (pkt->pkt_reason == CMD_TRAN_ERR) {
10988 status = EAGAIN;
10989 mptsas_log(mpt, CE_WARN, "passthru fma error");
10990 goto out;
10992 if (pkt->pkt_reason == CMD_RESET) {
10993 status = EAGAIN;
10994 mptsas_log(mpt, CE_WARN, "ioc reset abort passthru");
10995 goto out;
10998 if (pkt->pkt_reason == CMD_INCOMPLETE) {
10999 status = EIO;
11000 mptsas_log(mpt, CE_WARN, "passthrough command incomplete");
11001 goto out;
11004 mutex_exit(&mpt->m_mutex);
11005 if (cmd->cmd_flags & CFLAG_PREPARED) {
11006 function = request_hdrp->Function;
11007 if ((function == MPI2_FUNCTION_SCSI_IO_REQUEST) ||
11008 (function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
11009 reply_len = sizeof (MPI2_SCSI_IO_REPLY);
11010 sense_len = cmd->cmd_extrqslen ?
11011 min(sense_len, cmd->cmd_extrqslen) :
11012 min(sense_len, cmd->cmd_rqslen);
11013 } else {
11014 reply_len = reply_size;
11015 sense_len = 0;
11018 for (i = 0; i < reply_len; i++) {
11019 if (ddi_copyout((uint8_t *)reply_msg + i, reply + i, 1,
11020 mode)) {
11021 mutex_enter(&mpt->m_mutex);
11022 status = EFAULT;
11023 mptsas_log(mpt, CE_WARN, "failed to copy out "
11024 "reply data");
11025 goto out;
11028 for (i = 0; i < sense_len; i++) {
11029 if (ddi_copyout((uint8_t *)request_hdrp + 64 + i,
11030 reply + reply_len + i, 1, mode)) {
11031 mutex_enter(&mpt->m_mutex);
11032 status = EFAULT;
11033 mptsas_log(mpt, CE_WARN, "failed to copy out "
11034 "sense data");
11035 goto out;
11040 if (data_size) {
11041 if (direction != MPTSAS_PASS_THRU_DIRECTION_WRITE) {
11042 (void) ddi_dma_sync(data_dma_state.handle, 0, 0,
11043 DDI_DMA_SYNC_FORCPU);
11044 for (i = 0; i < data_size; i++) {
11045 if (ddi_copyout((uint8_t *)(
11046 data_dma_state.memp + i), data + i, 1,
11047 mode)) {
11048 mutex_enter(&mpt->m_mutex);
11049 status = EFAULT;
11050 mptsas_log(mpt, CE_WARN, "failed to "
11051 "copy out the reply data");
11052 goto out;
11057 mutex_enter(&mpt->m_mutex);
11058 out:
11060 * Put the reply frame back on the free queue, increment the free
11061 * index, and write the new index to the free index register. But only
11062 * if this reply is an ADDRESS reply.
11064 if (pt_flags & MPTSAS_ADDRESS_REPLY) {
11065 ddi_put32(mpt->m_acc_free_queue_hdl,
11066 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11067 cmd->cmd_rfm);
11068 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11069 DDI_DMA_SYNC_FORDEV);
11070 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11071 mpt->m_free_index = 0;
11073 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11074 mpt->m_free_index);
11076 if (cmd) {
11077 if (cmd->cmd_extrqslen != 0) {
11078 rmfree(mpt->m_erqsense_map, cmd->cmd_extrqschunks,
11079 cmd->cmd_extrqsidx + 1);
11081 if (cmd->cmd_flags & CFLAG_PREPARED) {
11082 mptsas_remove_cmd(mpt, cmd);
11083 pt_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11086 if (pt_flags & MPTSAS_REQUEST_POOL_CMD)
11087 mptsas_return_to_pool(mpt, cmd);
11088 if (pt_flags & MPTSAS_DATA_ALLOCATED) {
11089 if (mptsas_check_dma_handle(data_dma_state.handle) !=
11090 DDI_SUCCESS) {
11091 ddi_fm_service_impact(mpt->m_dip,
11092 DDI_SERVICE_UNAFFECTED);
11093 status = EFAULT;
11095 mptsas_dma_free(&data_dma_state);
11097 if (pt_flags & MPTSAS_DATAOUT_ALLOCATED) {
11098 if (mptsas_check_dma_handle(dataout_dma_state.handle) !=
11099 DDI_SUCCESS) {
11100 ddi_fm_service_impact(mpt->m_dip,
11101 DDI_SERVICE_UNAFFECTED);
11102 status = EFAULT;
11104 mptsas_dma_free(&dataout_dma_state);
11106 if (pt_flags & MPTSAS_CMD_TIMEOUT) {
11107 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
11108 mptsas_log(mpt, CE_WARN, "mptsas_restart_ioc failed");
11111 if (request_msg)
11112 kmem_free(request_msg, request_size);
11113 NDBG27(("mptsas_do_passthru: Done status 0x%x", status));
11115 return (status);
11118 static int
11119 mptsas_pass_thru(mptsas_t *mpt, mptsas_pass_thru_t *data, int mode)
11122 * If timeout is 0, set timeout to default of 60 seconds.
11124 if (data->Timeout == 0) {
11125 data->Timeout = MPTSAS_PASS_THRU_TIME_DEFAULT;
11128 if (((data->DataSize == 0) &&
11129 (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_NONE)) ||
11130 ((data->DataSize != 0) &&
11131 ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_READ) ||
11132 (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_WRITE) ||
11133 ((data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) &&
11134 (data->DataOutSize != 0))))) {
11135 if (data->DataDirection == MPTSAS_PASS_THRU_DIRECTION_BOTH) {
11136 data->DataDirection = MPTSAS_PASS_THRU_DIRECTION_READ;
11137 } else {
11138 data->DataOutSize = 0;
11141 * Send passthru request messages
11143 return (mptsas_do_passthru(mpt,
11144 (uint8_t *)((uintptr_t)data->PtrRequest),
11145 (uint8_t *)((uintptr_t)data->PtrReply),
11146 (uint8_t *)((uintptr_t)data->PtrData),
11147 data->RequestSize, data->ReplySize,
11148 data->DataSize, data->DataDirection,
11149 (uint8_t *)((uintptr_t)data->PtrDataOut),
11150 data->DataOutSize, data->Timeout, mode));
11151 } else {
11152 return (EINVAL);
11156 static uint8_t
11157 mptsas_get_fw_diag_buffer_number(mptsas_t *mpt, uint32_t unique_id)
11159 uint8_t index;
11161 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
11162 if (mpt->m_fw_diag_buffer_list[index].unique_id == unique_id) {
11163 return (index);
11167 return (MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND);
11170 static void
11171 mptsas_start_diag(mptsas_t *mpt, mptsas_cmd_t *cmd)
11173 pMpi2DiagBufferPostRequest_t pDiag_post_msg;
11174 pMpi2DiagReleaseRequest_t pDiag_release_msg;
11175 struct scsi_pkt *pkt = cmd->cmd_pkt;
11176 mptsas_diag_request_t *diag = pkt->pkt_ha_private;
11177 uint32_t i;
11178 uint64_t request_desc;
11180 ASSERT(mutex_owned(&mpt->m_mutex));
11183 * Form the diag message depending on the post or release function.
11185 if (diag->function == MPI2_FUNCTION_DIAG_BUFFER_POST) {
11186 pDiag_post_msg = (pMpi2DiagBufferPostRequest_t)
11187 (mpt->m_req_frame + (mpt->m_req_frame_size *
11188 cmd->cmd_slot));
11189 bzero(pDiag_post_msg, mpt->m_req_frame_size);
11190 ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->Function,
11191 diag->function);
11192 ddi_put8(mpt->m_acc_req_frame_hdl, &pDiag_post_msg->BufferType,
11193 diag->pBuffer->buffer_type);
11194 ddi_put8(mpt->m_acc_req_frame_hdl,
11195 &pDiag_post_msg->ExtendedType,
11196 diag->pBuffer->extended_type);
11197 ddi_put32(mpt->m_acc_req_frame_hdl,
11198 &pDiag_post_msg->BufferLength,
11199 diag->pBuffer->buffer_data.size);
11200 for (i = 0; i < (sizeof (pDiag_post_msg->ProductSpecific) / 4);
11201 i++) {
11202 ddi_put32(mpt->m_acc_req_frame_hdl,
11203 &pDiag_post_msg->ProductSpecific[i],
11204 diag->pBuffer->product_specific[i]);
11206 ddi_put32(mpt->m_acc_req_frame_hdl,
11207 &pDiag_post_msg->BufferAddress.Low,
11208 (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
11209 & 0xffffffffull));
11210 ddi_put32(mpt->m_acc_req_frame_hdl,
11211 &pDiag_post_msg->BufferAddress.High,
11212 (uint32_t)(diag->pBuffer->buffer_data.cookie.dmac_laddress
11213 >> 32));
11214 } else {
11215 pDiag_release_msg = (pMpi2DiagReleaseRequest_t)
11216 (mpt->m_req_frame + (mpt->m_req_frame_size *
11217 cmd->cmd_slot));
11218 bzero(pDiag_release_msg, mpt->m_req_frame_size);
11219 ddi_put8(mpt->m_acc_req_frame_hdl,
11220 &pDiag_release_msg->Function, diag->function);
11221 ddi_put8(mpt->m_acc_req_frame_hdl,
11222 &pDiag_release_msg->BufferType,
11223 diag->pBuffer->buffer_type);
11227 * Send the message
11229 (void) ddi_dma_sync(mpt->m_dma_req_frame_hdl, 0, 0,
11230 DDI_DMA_SYNC_FORDEV);
11231 request_desc = (cmd->cmd_slot << 16) +
11232 MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
11233 cmd->cmd_rfm = 0;
11234 MPTSAS_START_CMD(mpt, request_desc);
11235 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
11236 DDI_SUCCESS) ||
11237 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
11238 DDI_SUCCESS)) {
11239 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
11243 static int
11244 mptsas_post_fw_diag_buffer(mptsas_t *mpt,
11245 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code)
11247 mptsas_diag_request_t diag;
11248 int status, slot_num, post_flags = 0;
11249 mptsas_cmd_t *cmd = NULL;
11250 struct scsi_pkt *pkt;
11251 pMpi2DiagBufferPostReply_t reply;
11252 uint16_t iocstatus;
11253 uint32_t iocloginfo, transfer_length;
11256 * If buffer is not enabled, just leave.
11258 *return_code = MPTSAS_FW_DIAG_ERROR_POST_FAILED;
11259 if (!pBuffer->enabled) {
11260 status = DDI_FAILURE;
11261 goto out;
11265 * Clear some flags initially.
11267 pBuffer->force_release = FALSE;
11268 pBuffer->valid_data = FALSE;
11269 pBuffer->owned_by_firmware = FALSE;
11272 * Get a cmd buffer from the cmd buffer pool
11274 if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
11275 status = DDI_FAILURE;
11276 mptsas_log(mpt, CE_NOTE, "command pool is full: Post FW Diag");
11277 goto out;
11279 post_flags |= MPTSAS_REQUEST_POOL_CMD;
11281 bzero((caddr_t)cmd, sizeof (*cmd));
11282 bzero((caddr_t)pkt, scsi_pkt_size());
11284 cmd->ioc_cmd_slot = (uint32_t)(slot_num);
11286 diag.pBuffer = pBuffer;
11287 diag.function = MPI2_FUNCTION_DIAG_BUFFER_POST;
11290 * Form a blank cmd/pkt to store the acknowledgement message
11292 pkt->pkt_ha_private = (opaque_t)&diag;
11293 pkt->pkt_flags = FLAG_HEAD;
11294 pkt->pkt_time = 60;
11295 cmd->cmd_pkt = pkt;
11296 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_FW_DIAG;
11299 * Save the command in a slot
11301 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
11303 * Once passthru command get slot, set cmd_flags
11304 * CFLAG_PREPARED.
11306 cmd->cmd_flags |= CFLAG_PREPARED;
11307 mptsas_start_diag(mpt, cmd);
11308 } else {
11309 mptsas_waitq_add(mpt, cmd);
11312 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
11313 cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
11316 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
11317 status = DDI_FAILURE;
11318 mptsas_log(mpt, CE_WARN, "Post FW Diag command timeout");
11319 goto out;
11323 * cmd_rfm points to the reply message if a reply was given. Check the
11324 * IOCStatus to make sure everything went OK with the FW diag request
11325 * and set buffer flags.
11327 if (cmd->cmd_rfm) {
11328 post_flags |= MPTSAS_ADDRESS_REPLY;
11329 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
11330 DDI_DMA_SYNC_FORCPU);
11331 reply = (pMpi2DiagBufferPostReply_t)(mpt->m_reply_frame +
11332 (cmd->cmd_rfm -
11333 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
11336 * Get the reply message data
11338 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
11339 &reply->IOCStatus);
11340 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
11341 &reply->IOCLogInfo);
11342 transfer_length = ddi_get32(mpt->m_acc_reply_frame_hdl,
11343 &reply->TransferLength);
11346 * If post failed quit.
11348 if (iocstatus != MPI2_IOCSTATUS_SUCCESS) {
11349 status = DDI_FAILURE;
11350 NDBG13(("post FW Diag Buffer failed: IOCStatus=0x%x, "
11351 "IOCLogInfo=0x%x, TransferLength=0x%x", iocstatus,
11352 iocloginfo, transfer_length));
11353 goto out;
11357 * Post was successful.
11359 pBuffer->valid_data = TRUE;
11360 pBuffer->owned_by_firmware = TRUE;
11361 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11362 status = DDI_SUCCESS;
11365 out:
11367 * Put the reply frame back on the free queue, increment the free
11368 * index, and write the new index to the free index register. But only
11369 * if this reply is an ADDRESS reply.
11371 if (post_flags & MPTSAS_ADDRESS_REPLY) {
11372 ddi_put32(mpt->m_acc_free_queue_hdl,
11373 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11374 cmd->cmd_rfm);
11375 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11376 DDI_DMA_SYNC_FORDEV);
11377 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11378 mpt->m_free_index = 0;
11380 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11381 mpt->m_free_index);
11383 if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
11384 mptsas_remove_cmd(mpt, cmd);
11385 post_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11387 if (post_flags & MPTSAS_REQUEST_POOL_CMD) {
11388 mptsas_return_to_pool(mpt, cmd);
11391 return (status);
11394 static int
11395 mptsas_release_fw_diag_buffer(mptsas_t *mpt,
11396 mptsas_fw_diagnostic_buffer_t *pBuffer, uint32_t *return_code,
11397 uint32_t diag_type)
11399 mptsas_diag_request_t diag;
11400 int status, slot_num, rel_flags = 0;
11401 mptsas_cmd_t *cmd = NULL;
11402 struct scsi_pkt *pkt;
11403 pMpi2DiagReleaseReply_t reply;
11404 uint16_t iocstatus;
11405 uint32_t iocloginfo;
11408 * If buffer is not enabled, just leave.
11410 *return_code = MPTSAS_FW_DIAG_ERROR_RELEASE_FAILED;
11411 if (!pBuffer->enabled) {
11412 mptsas_log(mpt, CE_NOTE, "This buffer type is not supported "
11413 "by the IOC");
11414 status = DDI_FAILURE;
11415 goto out;
11419 * Clear some flags initially.
11421 pBuffer->force_release = FALSE;
11422 pBuffer->valid_data = FALSE;
11423 pBuffer->owned_by_firmware = FALSE;
11426 * Get a cmd buffer from the cmd buffer pool
11428 if ((slot_num = (mptsas_request_from_pool(mpt, &cmd, &pkt))) == -1) {
11429 status = DDI_FAILURE;
11430 mptsas_log(mpt, CE_NOTE, "command pool is full: Release FW "
11431 "Diag");
11432 goto out;
11434 rel_flags |= MPTSAS_REQUEST_POOL_CMD;
11436 bzero((caddr_t)cmd, sizeof (*cmd));
11437 bzero((caddr_t)pkt, scsi_pkt_size());
11439 cmd->ioc_cmd_slot = (uint32_t)(slot_num);
11441 diag.pBuffer = pBuffer;
11442 diag.function = MPI2_FUNCTION_DIAG_RELEASE;
11445 * Form a blank cmd/pkt to store the acknowledgement message
11447 pkt->pkt_ha_private = (opaque_t)&diag;
11448 pkt->pkt_flags = FLAG_HEAD;
11449 pkt->pkt_time = 60;
11450 cmd->cmd_pkt = pkt;
11451 cmd->cmd_flags = CFLAG_CMDIOC | CFLAG_FW_DIAG;
11454 * Save the command in a slot
11456 if (mptsas_save_cmd(mpt, cmd) == TRUE) {
11458 * Once passthru command get slot, set cmd_flags
11459 * CFLAG_PREPARED.
11461 cmd->cmd_flags |= CFLAG_PREPARED;
11462 mptsas_start_diag(mpt, cmd);
11463 } else {
11464 mptsas_waitq_add(mpt, cmd);
11467 while ((cmd->cmd_flags & CFLAG_FINISHED) == 0) {
11468 cv_wait(&mpt->m_fw_diag_cv, &mpt->m_mutex);
11471 if (cmd->cmd_flags & CFLAG_TIMEOUT) {
11472 status = DDI_FAILURE;
11473 mptsas_log(mpt, CE_WARN, "Release FW Diag command timeout");
11474 goto out;
11478 * cmd_rfm points to the reply message if a reply was given. Check the
11479 * IOCStatus to make sure everything went OK with the FW diag request
11480 * and set buffer flags.
11482 if (cmd->cmd_rfm) {
11483 rel_flags |= MPTSAS_ADDRESS_REPLY;
11484 (void) ddi_dma_sync(mpt->m_dma_reply_frame_hdl, 0, 0,
11485 DDI_DMA_SYNC_FORCPU);
11486 reply = (pMpi2DiagReleaseReply_t)(mpt->m_reply_frame +
11487 (cmd->cmd_rfm -
11488 (mpt->m_reply_frame_dma_addr & 0xffffffffu)));
11491 * Get the reply message data
11493 iocstatus = ddi_get16(mpt->m_acc_reply_frame_hdl,
11494 &reply->IOCStatus);
11495 iocloginfo = ddi_get32(mpt->m_acc_reply_frame_hdl,
11496 &reply->IOCLogInfo);
11499 * If release failed quit.
11501 if ((iocstatus != MPI2_IOCSTATUS_SUCCESS) ||
11502 pBuffer->owned_by_firmware) {
11503 status = DDI_FAILURE;
11504 NDBG13(("release FW Diag Buffer failed: "
11505 "IOCStatus=0x%x, IOCLogInfo=0x%x", iocstatus,
11506 iocloginfo));
11507 goto out;
11511 * Release was successful.
11513 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11514 status = DDI_SUCCESS;
11517 * If this was for an UNREGISTER diag type command, clear the
11518 * unique ID.
11520 if (diag_type == MPTSAS_FW_DIAG_TYPE_UNREGISTER) {
11521 pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11525 out:
11527 * Put the reply frame back on the free queue, increment the free
11528 * index, and write the new index to the free index register. But only
11529 * if this reply is an ADDRESS reply.
11531 if (rel_flags & MPTSAS_ADDRESS_REPLY) {
11532 ddi_put32(mpt->m_acc_free_queue_hdl,
11533 &((uint32_t *)(void *)mpt->m_free_queue)[mpt->m_free_index],
11534 cmd->cmd_rfm);
11535 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
11536 DDI_DMA_SYNC_FORDEV);
11537 if (++mpt->m_free_index == mpt->m_free_queue_depth) {
11538 mpt->m_free_index = 0;
11540 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex,
11541 mpt->m_free_index);
11543 if (cmd && (cmd->cmd_flags & CFLAG_PREPARED)) {
11544 mptsas_remove_cmd(mpt, cmd);
11545 rel_flags &= (~MPTSAS_REQUEST_POOL_CMD);
11547 if (rel_flags & MPTSAS_REQUEST_POOL_CMD) {
11548 mptsas_return_to_pool(mpt, cmd);
11551 return (status);
11554 static int
11555 mptsas_diag_register(mptsas_t *mpt, mptsas_fw_diag_register_t *diag_register,
11556 uint32_t *return_code)
11558 mptsas_fw_diagnostic_buffer_t *pBuffer;
11559 uint8_t extended_type, buffer_type, i;
11560 uint32_t buffer_size;
11561 uint32_t unique_id;
11562 int status;
11564 ASSERT(mutex_owned(&mpt->m_mutex));
11566 extended_type = diag_register->ExtendedType;
11567 buffer_type = diag_register->BufferType;
11568 buffer_size = diag_register->RequestedBufferSize;
11569 unique_id = diag_register->UniqueId;
11572 * Check for valid buffer type
11574 if (buffer_type >= MPI2_DIAG_BUF_TYPE_COUNT) {
11575 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11576 return (DDI_FAILURE);
11580 * Get the current buffer and look up the unique ID. The unique ID
11581 * should not be found. If it is, the ID is already in use.
11583 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11584 pBuffer = &mpt->m_fw_diag_buffer_list[buffer_type];
11585 if (i != MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11586 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11587 return (DDI_FAILURE);
11591 * The buffer's unique ID should not be registered yet, and the given
11592 * unique ID cannot be 0.
11594 if ((pBuffer->unique_id != MPTSAS_FW_DIAG_INVALID_UID) ||
11595 (unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11596 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11597 return (DDI_FAILURE);
11601 * If this buffer is already posted as immediate, just change owner.
11603 if (pBuffer->immediate && pBuffer->owned_by_firmware &&
11604 (pBuffer->unique_id == MPTSAS_FW_DIAG_INVALID_UID)) {
11605 pBuffer->immediate = FALSE;
11606 pBuffer->unique_id = unique_id;
11607 return (DDI_SUCCESS);
11611 * Post a new buffer after checking if it's enabled. The DMA buffer
11612 * that is allocated will be contiguous (sgl_len = 1).
11614 if (!pBuffer->enabled) {
11615 *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11616 return (DDI_FAILURE);
11618 bzero(&pBuffer->buffer_data, sizeof (mptsas_dma_alloc_state_t));
11619 pBuffer->buffer_data.size = buffer_size;
11620 if (mptsas_dma_alloc(mpt, &pBuffer->buffer_data) != DDI_SUCCESS) {
11621 mptsas_log(mpt, CE_WARN, "failed to alloc DMA resource for "
11622 "diag buffer: size = %d bytes", buffer_size);
11623 *return_code = MPTSAS_FW_DIAG_ERROR_NO_BUFFER;
11624 return (DDI_FAILURE);
11628 * Copy the given info to the diag buffer and post the buffer.
11630 pBuffer->buffer_type = buffer_type;
11631 pBuffer->immediate = FALSE;
11632 if (buffer_type == MPI2_DIAG_BUF_TYPE_TRACE) {
11633 for (i = 0; i < (sizeof (pBuffer->product_specific) / 4);
11634 i++) {
11635 pBuffer->product_specific[i] =
11636 diag_register->ProductSpecific[i];
11639 pBuffer->extended_type = extended_type;
11640 pBuffer->unique_id = unique_id;
11641 status = mptsas_post_fw_diag_buffer(mpt, pBuffer, return_code);
11643 if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11644 DDI_SUCCESS) {
11645 mptsas_log(mpt, CE_WARN, "Check of DMA handle failed in "
11646 "mptsas_diag_register.");
11647 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
11648 status = DDI_FAILURE;
11652 * In case there was a failure, free the DMA buffer.
11654 if (status == DDI_FAILURE) {
11655 mptsas_dma_free(&pBuffer->buffer_data);
11658 return (status);
11661 static int
11662 mptsas_diag_unregister(mptsas_t *mpt,
11663 mptsas_fw_diag_unregister_t *diag_unregister, uint32_t *return_code)
11665 mptsas_fw_diagnostic_buffer_t *pBuffer;
11666 uint8_t i;
11667 uint32_t unique_id;
11668 int status;
11670 ASSERT(mutex_owned(&mpt->m_mutex));
11672 unique_id = diag_unregister->UniqueId;
11675 * Get the current buffer and look up the unique ID. The unique ID
11676 * should be there.
11678 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11679 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11680 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11681 return (DDI_FAILURE);
11684 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11687 * Try to release the buffer from FW before freeing it. If release
11688 * fails, don't free the DMA buffer in case FW tries to access it
11689 * later. If buffer is not owned by firmware, can't release it.
11691 if (!pBuffer->owned_by_firmware) {
11692 status = DDI_SUCCESS;
11693 } else {
11694 status = mptsas_release_fw_diag_buffer(mpt, pBuffer,
11695 return_code, MPTSAS_FW_DIAG_TYPE_UNREGISTER);
11699 * At this point, return the current status no matter what happens with
11700 * the DMA buffer.
11702 pBuffer->unique_id = MPTSAS_FW_DIAG_INVALID_UID;
11703 if (status == DDI_SUCCESS) {
11704 if (mptsas_check_dma_handle(pBuffer->buffer_data.handle) !=
11705 DDI_SUCCESS) {
11706 mptsas_log(mpt, CE_WARN, "Check of DMA handle failed "
11707 "in mptsas_diag_unregister.");
11708 ddi_fm_service_impact(mpt->m_dip,
11709 DDI_SERVICE_UNAFFECTED);
11711 mptsas_dma_free(&pBuffer->buffer_data);
11714 return (status);
11717 static int
11718 mptsas_diag_query(mptsas_t *mpt, mptsas_fw_diag_query_t *diag_query,
11719 uint32_t *return_code)
11721 mptsas_fw_diagnostic_buffer_t *pBuffer;
11722 uint8_t i;
11723 uint32_t unique_id;
11725 ASSERT(mutex_owned(&mpt->m_mutex));
11727 unique_id = diag_query->UniqueId;
11730 * If ID is valid, query on ID.
11731 * If ID is invalid, query on buffer type.
11733 if (unique_id == MPTSAS_FW_DIAG_INVALID_UID) {
11734 i = diag_query->BufferType;
11735 if (i >= MPI2_DIAG_BUF_TYPE_COUNT) {
11736 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11737 return (DDI_FAILURE);
11739 } else {
11740 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11741 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11742 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11743 return (DDI_FAILURE);
11748 * Fill query structure with the diag buffer info.
11750 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11751 diag_query->BufferType = pBuffer->buffer_type;
11752 diag_query->ExtendedType = pBuffer->extended_type;
11753 if (diag_query->BufferType == MPI2_DIAG_BUF_TYPE_TRACE) {
11754 for (i = 0; i < (sizeof (diag_query->ProductSpecific) / 4);
11755 i++) {
11756 diag_query->ProductSpecific[i] =
11757 pBuffer->product_specific[i];
11760 diag_query->TotalBufferSize = pBuffer->buffer_data.size;
11761 diag_query->DriverAddedBufferSize = 0;
11762 diag_query->UniqueId = pBuffer->unique_id;
11763 diag_query->ApplicationFlags = 0;
11764 diag_query->DiagnosticFlags = 0;
11767 * Set/Clear application flags
11769 if (pBuffer->immediate) {
11770 diag_query->ApplicationFlags &= ~MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11771 } else {
11772 diag_query->ApplicationFlags |= MPTSAS_FW_DIAG_FLAG_APP_OWNED;
11774 if (pBuffer->valid_data || pBuffer->owned_by_firmware) {
11775 diag_query->ApplicationFlags |=
11776 MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11777 } else {
11778 diag_query->ApplicationFlags &=
11779 ~MPTSAS_FW_DIAG_FLAG_BUFFER_VALID;
11781 if (pBuffer->owned_by_firmware) {
11782 diag_query->ApplicationFlags |=
11783 MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11784 } else {
11785 diag_query->ApplicationFlags &=
11786 ~MPTSAS_FW_DIAG_FLAG_FW_BUFFER_ACCESS;
11789 return (DDI_SUCCESS);
11792 static int
11793 mptsas_diag_read_buffer(mptsas_t *mpt,
11794 mptsas_diag_read_buffer_t *diag_read_buffer, uint8_t *ioctl_buf,
11795 uint32_t *return_code, int ioctl_mode)
11797 mptsas_fw_diagnostic_buffer_t *pBuffer;
11798 uint8_t i, *pData;
11799 uint32_t unique_id, byte;
11800 int status;
11802 ASSERT(mutex_owned(&mpt->m_mutex));
11804 unique_id = diag_read_buffer->UniqueId;
11807 * Get the current buffer and look up the unique ID. The unique ID
11808 * should be there.
11810 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11811 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11812 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11813 return (DDI_FAILURE);
11816 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11819 * Make sure requested read is within limits
11821 if (diag_read_buffer->StartingOffset + diag_read_buffer->BytesToRead >
11822 pBuffer->buffer_data.size) {
11823 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11824 return (DDI_FAILURE);
11828 * Copy the requested data from DMA to the diag_read_buffer. The DMA
11829 * buffer that was allocated is one contiguous buffer.
11831 pData = (uint8_t *)(pBuffer->buffer_data.memp +
11832 diag_read_buffer->StartingOffset);
11833 (void) ddi_dma_sync(pBuffer->buffer_data.handle, 0, 0,
11834 DDI_DMA_SYNC_FORCPU);
11835 for (byte = 0; byte < diag_read_buffer->BytesToRead; byte++) {
11836 if (ddi_copyout(pData + byte, ioctl_buf + byte, 1, ioctl_mode)
11837 != 0) {
11838 return (DDI_FAILURE);
11841 diag_read_buffer->Status = 0;
11844 * Set or clear the Force Release flag.
11846 if (pBuffer->force_release) {
11847 diag_read_buffer->Flags |= MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11848 } else {
11849 diag_read_buffer->Flags &= ~MPTSAS_FW_DIAG_FLAG_FORCE_RELEASE;
11853 * If buffer is to be reregistered, make sure it's not already owned by
11854 * firmware first.
11856 status = DDI_SUCCESS;
11857 if (!pBuffer->owned_by_firmware) {
11858 if (diag_read_buffer->Flags & MPTSAS_FW_DIAG_FLAG_REREGISTER) {
11859 status = mptsas_post_fw_diag_buffer(mpt, pBuffer,
11860 return_code);
11864 return (status);
11867 static int
11868 mptsas_diag_release(mptsas_t *mpt, mptsas_fw_diag_release_t *diag_release,
11869 uint32_t *return_code)
11871 mptsas_fw_diagnostic_buffer_t *pBuffer;
11872 uint8_t i;
11873 uint32_t unique_id;
11874 int status;
11876 ASSERT(mutex_owned(&mpt->m_mutex));
11878 unique_id = diag_release->UniqueId;
11881 * Get the current buffer and look up the unique ID. The unique ID
11882 * should be there.
11884 i = mptsas_get_fw_diag_buffer_number(mpt, unique_id);
11885 if (i == MPTSAS_FW_DIAGNOSTIC_UID_NOT_FOUND) {
11886 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_UID;
11887 return (DDI_FAILURE);
11890 pBuffer = &mpt->m_fw_diag_buffer_list[i];
11893 * If buffer is not owned by firmware, it's already been released.
11895 if (!pBuffer->owned_by_firmware) {
11896 *return_code = MPTSAS_FW_DIAG_ERROR_ALREADY_RELEASED;
11897 return (DDI_FAILURE);
11901 * Release the buffer.
11903 status = mptsas_release_fw_diag_buffer(mpt, pBuffer, return_code,
11904 MPTSAS_FW_DIAG_TYPE_RELEASE);
11905 return (status);
11908 static int
11909 mptsas_do_diag_action(mptsas_t *mpt, uint32_t action, uint8_t *diag_action,
11910 uint32_t length, uint32_t *return_code, int ioctl_mode)
11912 mptsas_fw_diag_register_t diag_register;
11913 mptsas_fw_diag_unregister_t diag_unregister;
11914 mptsas_fw_diag_query_t diag_query;
11915 mptsas_diag_read_buffer_t diag_read_buffer;
11916 mptsas_fw_diag_release_t diag_release;
11917 int status = DDI_SUCCESS;
11918 uint32_t original_return_code, read_buf_len;
11920 ASSERT(mutex_owned(&mpt->m_mutex));
11922 original_return_code = *return_code;
11923 *return_code = MPTSAS_FW_DIAG_ERROR_SUCCESS;
11925 switch (action) {
11926 case MPTSAS_FW_DIAG_TYPE_REGISTER:
11927 if (!length) {
11928 *return_code =
11929 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11930 status = DDI_FAILURE;
11931 break;
11933 if (ddi_copyin(diag_action, &diag_register,
11934 sizeof (diag_register), ioctl_mode) != 0) {
11935 return (DDI_FAILURE);
11937 status = mptsas_diag_register(mpt, &diag_register,
11938 return_code);
11939 break;
11941 case MPTSAS_FW_DIAG_TYPE_UNREGISTER:
11942 if (length < sizeof (diag_unregister)) {
11943 *return_code =
11944 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11945 status = DDI_FAILURE;
11946 break;
11948 if (ddi_copyin(diag_action, &diag_unregister,
11949 sizeof (diag_unregister), ioctl_mode) != 0) {
11950 return (DDI_FAILURE);
11952 status = mptsas_diag_unregister(mpt, &diag_unregister,
11953 return_code);
11954 break;
11956 case MPTSAS_FW_DIAG_TYPE_QUERY:
11957 if (length < sizeof (diag_query)) {
11958 *return_code =
11959 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11960 status = DDI_FAILURE;
11961 break;
11963 if (ddi_copyin(diag_action, &diag_query,
11964 sizeof (diag_query), ioctl_mode) != 0) {
11965 return (DDI_FAILURE);
11967 status = mptsas_diag_query(mpt, &diag_query,
11968 return_code);
11969 if (status == DDI_SUCCESS) {
11970 if (ddi_copyout(&diag_query, diag_action,
11971 sizeof (diag_query), ioctl_mode) != 0) {
11972 return (DDI_FAILURE);
11975 break;
11977 case MPTSAS_FW_DIAG_TYPE_READ_BUFFER:
11978 if (ddi_copyin(diag_action, &diag_read_buffer,
11979 sizeof (diag_read_buffer) - 4, ioctl_mode) != 0) {
11980 return (DDI_FAILURE);
11982 read_buf_len = sizeof (diag_read_buffer) -
11983 sizeof (diag_read_buffer.DataBuffer) +
11984 diag_read_buffer.BytesToRead;
11985 if (length < read_buf_len) {
11986 *return_code =
11987 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
11988 status = DDI_FAILURE;
11989 break;
11991 status = mptsas_diag_read_buffer(mpt,
11992 &diag_read_buffer, diag_action +
11993 sizeof (diag_read_buffer) - 4, return_code,
11994 ioctl_mode);
11995 if (status == DDI_SUCCESS) {
11996 if (ddi_copyout(&diag_read_buffer, diag_action,
11997 sizeof (diag_read_buffer) - 4, ioctl_mode)
11998 != 0) {
11999 return (DDI_FAILURE);
12002 break;
12004 case MPTSAS_FW_DIAG_TYPE_RELEASE:
12005 if (length < sizeof (diag_release)) {
12006 *return_code =
12007 MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
12008 status = DDI_FAILURE;
12009 break;
12011 if (ddi_copyin(diag_action, &diag_release,
12012 sizeof (diag_release), ioctl_mode) != 0) {
12013 return (DDI_FAILURE);
12015 status = mptsas_diag_release(mpt, &diag_release,
12016 return_code);
12017 break;
12019 default:
12020 *return_code = MPTSAS_FW_DIAG_ERROR_INVALID_PARAMETER;
12021 status = DDI_FAILURE;
12022 break;
12025 if ((status == DDI_FAILURE) &&
12026 (original_return_code == MPTSAS_FW_DIAG_NEW) &&
12027 (*return_code != MPTSAS_FW_DIAG_ERROR_SUCCESS)) {
12028 status = DDI_SUCCESS;
12031 return (status);
12034 static int
12035 mptsas_diag_action(mptsas_t *mpt, mptsas_diag_action_t *user_data, int mode)
12037 int status;
12038 mptsas_diag_action_t driver_data;
12040 ASSERT(mutex_owned(&mpt->m_mutex));
12043 * Copy the user data to a driver data buffer.
12045 if (ddi_copyin(user_data, &driver_data, sizeof (mptsas_diag_action_t),
12046 mode) == 0) {
12048 * Send diag action request if Action is valid
12050 if (driver_data.Action == MPTSAS_FW_DIAG_TYPE_REGISTER ||
12051 driver_data.Action == MPTSAS_FW_DIAG_TYPE_UNREGISTER ||
12052 driver_data.Action == MPTSAS_FW_DIAG_TYPE_QUERY ||
12053 driver_data.Action == MPTSAS_FW_DIAG_TYPE_READ_BUFFER ||
12054 driver_data.Action == MPTSAS_FW_DIAG_TYPE_RELEASE) {
12055 status = mptsas_do_diag_action(mpt, driver_data.Action,
12056 (void *)(uintptr_t)driver_data.PtrDiagAction,
12057 driver_data.Length, &driver_data.ReturnCode,
12058 mode);
12059 if (status == DDI_SUCCESS) {
12060 if (ddi_copyout(&driver_data.ReturnCode,
12061 &user_data->ReturnCode,
12062 sizeof (user_data->ReturnCode), mode)
12063 != 0) {
12064 status = EFAULT;
12065 } else {
12066 status = 0;
12068 } else {
12069 status = EIO;
12071 } else {
12072 status = EINVAL;
12074 } else {
12075 status = EFAULT;
12078 return (status);
12082 * This routine handles the "event query" ioctl.
12084 static int
12085 mptsas_event_query(mptsas_t *mpt, mptsas_event_query_t *data, int mode,
12086 int *rval)
12088 int status;
12089 mptsas_event_query_t driverdata;
12090 uint8_t i;
12092 driverdata.Entries = MPTSAS_EVENT_QUEUE_SIZE;
12094 mutex_enter(&mpt->m_mutex);
12095 for (i = 0; i < 4; i++) {
12096 driverdata.Types[i] = mpt->m_event_mask[i];
12098 mutex_exit(&mpt->m_mutex);
12100 if (ddi_copyout(&driverdata, data, sizeof (driverdata), mode) != 0) {
12101 status = EFAULT;
12102 } else {
12103 *rval = MPTIOCTL_STATUS_GOOD;
12104 status = 0;
12107 return (status);
12111 * This routine handles the "event enable" ioctl.
12113 static int
12114 mptsas_event_enable(mptsas_t *mpt, mptsas_event_enable_t *data, int mode,
12115 int *rval)
12117 int status;
12118 mptsas_event_enable_t driverdata;
12119 uint8_t i;
12121 if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
12122 mutex_enter(&mpt->m_mutex);
12123 for (i = 0; i < 4; i++) {
12124 mpt->m_event_mask[i] = driverdata.Types[i];
12126 mutex_exit(&mpt->m_mutex);
12128 *rval = MPTIOCTL_STATUS_GOOD;
12129 status = 0;
12130 } else {
12131 status = EFAULT;
12133 return (status);
12137 * This routine handles the "event report" ioctl.
12139 static int
12140 mptsas_event_report(mptsas_t *mpt, mptsas_event_report_t *data, int mode,
12141 int *rval)
12143 int status;
12144 mptsas_event_report_t driverdata;
12146 mutex_enter(&mpt->m_mutex);
12148 if (ddi_copyin(&data->Size, &driverdata.Size, sizeof (driverdata.Size),
12149 mode) == 0) {
12150 if (driverdata.Size >= sizeof (mpt->m_events)) {
12151 if (ddi_copyout(mpt->m_events, data->Events,
12152 sizeof (mpt->m_events), mode) != 0) {
12153 status = EFAULT;
12154 } else {
12155 if (driverdata.Size > sizeof (mpt->m_events)) {
12156 driverdata.Size =
12157 sizeof (mpt->m_events);
12158 if (ddi_copyout(&driverdata.Size,
12159 &data->Size,
12160 sizeof (driverdata.Size),
12161 mode) != 0) {
12162 status = EFAULT;
12163 } else {
12164 *rval = MPTIOCTL_STATUS_GOOD;
12165 status = 0;
12167 } else {
12168 *rval = MPTIOCTL_STATUS_GOOD;
12169 status = 0;
12172 } else {
12173 *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
12174 status = 0;
12176 } else {
12177 status = EFAULT;
12180 mutex_exit(&mpt->m_mutex);
12181 return (status);
12184 static void
12185 mptsas_lookup_pci_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
12187 int *reg_data;
12188 uint_t reglen;
12191 * Lookup the 'reg' property and extract the other data
12193 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
12194 DDI_PROP_DONTPASS, "reg", &reg_data, &reglen) ==
12195 DDI_PROP_SUCCESS) {
12197 * Extract the PCI data from the 'reg' property first DWORD.
12198 * The entry looks like the following:
12199 * First DWORD:
12200 * Bits 0 - 7 8-bit Register number
12201 * Bits 8 - 10 3-bit Function number
12202 * Bits 11 - 15 5-bit Device number
12203 * Bits 16 - 23 8-bit Bus number
12204 * Bits 24 - 25 2-bit Address Space type identifier
12207 adapter_data->PciInformation.u.bits.BusNumber =
12208 (reg_data[0] & 0x00FF0000) >> 16;
12209 adapter_data->PciInformation.u.bits.DeviceNumber =
12210 (reg_data[0] & 0x0000F800) >> 11;
12211 adapter_data->PciInformation.u.bits.FunctionNumber =
12212 (reg_data[0] & 0x00000700) >> 8;
12213 ddi_prop_free((void *)reg_data);
12214 } else {
12216 * If we can't determine the PCI data then we fill in FF's for
12217 * the data to indicate this.
12219 adapter_data->PCIDeviceHwId = 0xFFFFFFFF;
12220 adapter_data->MpiPortNumber = 0xFFFFFFFF;
12221 adapter_data->PciInformation.u.AsDWORD = 0xFFFFFFFF;
12225 * Saved in the mpt->m_fwversion
12227 adapter_data->MpiFirmwareVersion = mpt->m_fwversion;
12230 static void
12231 mptsas_read_adapter_data(mptsas_t *mpt, mptsas_adapter_data_t *adapter_data)
12233 char *driver_verstr = MPTSAS_MOD_STRING;
12235 mptsas_lookup_pci_data(mpt, adapter_data);
12236 adapter_data->AdapterType = mpt->m_MPI25 ?
12237 MPTIOCTL_ADAPTER_TYPE_SAS3 :
12238 MPTIOCTL_ADAPTER_TYPE_SAS2;
12239 adapter_data->PCIDeviceHwId = (uint32_t)mpt->m_devid;
12240 adapter_data->PCIDeviceHwRev = (uint32_t)mpt->m_revid;
12241 adapter_data->SubSystemId = (uint32_t)mpt->m_ssid;
12242 adapter_data->SubsystemVendorId = (uint32_t)mpt->m_svid;
12243 (void) strcpy((char *)&adapter_data->DriverVersion[0], driver_verstr);
12244 adapter_data->BiosVersion = 0;
12245 (void) mptsas_get_bios_page3(mpt, &adapter_data->BiosVersion);
12248 static void
12249 mptsas_read_pci_info(mptsas_t *mpt, mptsas_pci_info_t *pci_info)
12251 int *reg_data, i;
12252 uint_t reglen;
12255 * Lookup the 'reg' property and extract the other data
12257 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, mpt->m_dip,
12258 DDI_PROP_DONTPASS, "reg", &reg_data, &reglen) ==
12259 DDI_PROP_SUCCESS) {
12261 * Extract the PCI data from the 'reg' property first DWORD.
12262 * The entry looks like the following:
12263 * First DWORD:
12264 * Bits 8 - 10 3-bit Function number
12265 * Bits 11 - 15 5-bit Device number
12266 * Bits 16 - 23 8-bit Bus number
12268 pci_info->BusNumber = (reg_data[0] & 0x00FF0000) >> 16;
12269 pci_info->DeviceNumber = (reg_data[0] & 0x0000F800) >> 11;
12270 pci_info->FunctionNumber = (reg_data[0] & 0x00000700) >> 8;
12271 ddi_prop_free((void *)reg_data);
12272 } else {
12274 * If we can't determine the PCI info then we fill in FF's for
12275 * the data to indicate this.
12277 pci_info->BusNumber = 0xFFFFFFFF;
12278 pci_info->DeviceNumber = 0xFF;
12279 pci_info->FunctionNumber = 0xFF;
12283 * Now get the interrupt vector and the pci header. The vector can
12284 * only be 0 right now. The header is the first 256 bytes of config
12285 * space.
12287 pci_info->InterruptVector = 0;
12288 for (i = 0; i < sizeof (pci_info->PciHeader); i++) {
12289 pci_info->PciHeader[i] = pci_config_get8(mpt->m_config_handle,
12294 static int
12295 mptsas_reg_access(mptsas_t *mpt, mptsas_reg_access_t *data, int mode)
12297 int status = 0;
12298 mptsas_reg_access_t driverdata;
12300 mutex_enter(&mpt->m_mutex);
12301 if (ddi_copyin(data, &driverdata, sizeof (driverdata), mode) == 0) {
12302 switch (driverdata.Command) {
12304 * IO access is not supported.
12306 case REG_IO_READ:
12307 case REG_IO_WRITE:
12308 mptsas_log(mpt, CE_WARN, "IO access is not "
12309 "supported. Use memory access.");
12310 status = EINVAL;
12311 break;
12313 case REG_MEM_READ:
12314 driverdata.RegData = ddi_get32(mpt->m_datap,
12315 (uint32_t *)(void *)mpt->m_reg +
12316 driverdata.RegOffset);
12317 if (ddi_copyout(&driverdata.RegData,
12318 &data->RegData,
12319 sizeof (driverdata.RegData), mode) != 0) {
12320 mptsas_log(mpt, CE_WARN, "Register "
12321 "Read Failed");
12322 status = EFAULT;
12324 break;
12326 case REG_MEM_WRITE:
12327 ddi_put32(mpt->m_datap,
12328 (uint32_t *)(void *)mpt->m_reg +
12329 driverdata.RegOffset,
12330 driverdata.RegData);
12331 break;
12333 default:
12334 status = EINVAL;
12335 break;
12337 } else {
12338 status = EFAULT;
12341 mutex_exit(&mpt->m_mutex);
12342 return (status);
12345 static int
12346 led_control(mptsas_t *mpt, intptr_t data, int mode)
12348 int ret = 0;
12349 mptsas_led_control_t lc;
12350 mptsas_target_t *ptgt;
12352 if (ddi_copyin((void *)data, &lc, sizeof (lc), mode) != 0) {
12353 return (EFAULT);
12356 if ((lc.Command != MPTSAS_LEDCTL_FLAG_SET &&
12357 lc.Command != MPTSAS_LEDCTL_FLAG_GET) ||
12358 lc.Led < MPTSAS_LEDCTL_LED_MIN ||
12359 lc.Led > MPTSAS_LEDCTL_LED_MAX ||
12360 (lc.Command == MPTSAS_LEDCTL_FLAG_SET && lc.LedStatus != 0 &&
12361 lc.LedStatus != 1)) {
12362 return (EINVAL);
12365 if ((lc.Command == MPTSAS_LEDCTL_FLAG_SET && (mode & FWRITE) == 0) ||
12366 (lc.Command == MPTSAS_LEDCTL_FLAG_GET && (mode & FREAD) == 0))
12367 return (EACCES);
12369 /* Locate the target we're interrogating... */
12370 mutex_enter(&mpt->m_mutex);
12371 ptgt = refhash_linear_search(mpt->m_targets,
12372 mptsas_target_eval_slot, &lc);
12373 if (ptgt == NULL) {
12374 /* We could not find a target for that enclosure/slot. */
12375 mutex_exit(&mpt->m_mutex);
12376 return (ENOENT);
12379 if (lc.Command == MPTSAS_LEDCTL_FLAG_SET) {
12380 /* Update our internal LED state. */
12381 ptgt->m_led_status &= ~(1 << (lc.Led - 1));
12382 ptgt->m_led_status |= lc.LedStatus << (lc.Led - 1);
12384 /* Flush it to the controller. */
12385 ret = mptsas_flush_led_status(mpt, ptgt);
12386 mutex_exit(&mpt->m_mutex);
12387 return (ret);
12390 /* Return our internal LED state. */
12391 lc.LedStatus = (ptgt->m_led_status >> (lc.Led - 1)) & 1;
12392 mutex_exit(&mpt->m_mutex);
12394 if (ddi_copyout(&lc, (void *)data, sizeof (lc), mode) != 0) {
12395 return (EFAULT);
12398 return (0);
12401 static int
12402 get_disk_info(mptsas_t *mpt, intptr_t data, int mode)
12404 uint16_t i = 0;
12405 uint16_t count = 0;
12406 int ret = 0;
12407 mptsas_target_t *ptgt;
12408 mptsas_disk_info_t *di;
12409 STRUCT_DECL(mptsas_get_disk_info, gdi);
12411 if ((mode & FREAD) == 0)
12412 return (EACCES);
12414 STRUCT_INIT(gdi, get_udatamodel());
12416 if (ddi_copyin((void *)data, STRUCT_BUF(gdi), STRUCT_SIZE(gdi),
12417 mode) != 0) {
12418 return (EFAULT);
12421 /* Find out how many targets there are. */
12422 mutex_enter(&mpt->m_mutex);
12423 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12424 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12425 count++;
12427 mutex_exit(&mpt->m_mutex);
12430 * If we haven't been asked to copy out information on each target,
12431 * then just return the count.
12433 STRUCT_FSET(gdi, DiskCount, count);
12434 if (STRUCT_FGETP(gdi, PtrDiskInfoArray) == NULL)
12435 goto copy_out;
12438 * If we haven't been given a large enough buffer to copy out into,
12439 * let the caller know.
12441 if (STRUCT_FGET(gdi, DiskInfoArraySize) <
12442 count * sizeof (mptsas_disk_info_t)) {
12443 ret = ENOSPC;
12444 goto copy_out;
12447 di = kmem_zalloc(count * sizeof (mptsas_disk_info_t), KM_SLEEP);
12449 mutex_enter(&mpt->m_mutex);
12450 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12451 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12452 if (i >= count) {
12454 * The number of targets changed while we weren't
12455 * looking, so give up.
12457 refhash_rele(mpt->m_targets, ptgt);
12458 mutex_exit(&mpt->m_mutex);
12459 kmem_free(di, count * sizeof (mptsas_disk_info_t));
12460 return (EAGAIN);
12462 di[i].Instance = mpt->m_instance;
12463 di[i].Enclosure = ptgt->m_enclosure;
12464 di[i].Slot = ptgt->m_slot_num;
12465 di[i].SasAddress = ptgt->m_addr.mta_wwn;
12466 i++;
12468 mutex_exit(&mpt->m_mutex);
12469 STRUCT_FSET(gdi, DiskCount, i);
12471 /* Copy out the disk information to the caller. */
12472 if (ddi_copyout((void *)di, STRUCT_FGETP(gdi, PtrDiskInfoArray),
12473 i * sizeof (mptsas_disk_info_t), mode) != 0) {
12474 ret = EFAULT;
12477 kmem_free(di, count * sizeof (mptsas_disk_info_t));
12479 copy_out:
12480 if (ddi_copyout(STRUCT_BUF(gdi), (void *)data, STRUCT_SIZE(gdi),
12481 mode) != 0) {
12482 ret = EFAULT;
12485 return (ret);
12488 static int
12489 mptsas_ioctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp,
12490 int *rval)
12492 int status = 0;
12493 mptsas_t *mpt;
12494 mptsas_update_flash_t flashdata;
12495 mptsas_pass_thru_t passthru_data;
12496 mptsas_adapter_data_t adapter_data;
12497 mptsas_pci_info_t pci_info;
12498 int copylen;
12500 int iport_flag = 0;
12501 dev_info_t *dip = NULL;
12502 mptsas_phymask_t phymask = 0;
12503 struct devctl_iocdata *dcp = NULL;
12504 char *addr = NULL;
12505 mptsas_target_t *ptgt = NULL;
12507 *rval = MPTIOCTL_STATUS_GOOD;
12508 if (secpolicy_sys_config(credp, B_FALSE) != 0) {
12509 return (EPERM);
12512 mpt = ddi_get_soft_state(mptsas_state, MINOR2INST(getminor(dev)));
12513 if (mpt == NULL) {
12515 * Called from iport node, get the states
12517 iport_flag = 1;
12518 dip = mptsas_get_dip_from_dev(dev, &phymask);
12519 if (dip == NULL) {
12520 return (ENXIO);
12522 mpt = DIP2MPT(dip);
12524 /* Make sure power level is D0 before accessing registers */
12525 mutex_enter(&mpt->m_mutex);
12526 if (mpt->m_options & MPTSAS_OPT_PM) {
12527 (void) pm_busy_component(mpt->m_dip, 0);
12528 if (mpt->m_power_level != PM_LEVEL_D0) {
12529 mutex_exit(&mpt->m_mutex);
12530 if (pm_raise_power(mpt->m_dip, 0, PM_LEVEL_D0) !=
12531 DDI_SUCCESS) {
12532 mptsas_log(mpt, CE_WARN,
12533 "mptsas%d: mptsas_ioctl: Raise power "
12534 "request failed.", mpt->m_instance);
12535 (void) pm_idle_component(mpt->m_dip, 0);
12536 return (ENXIO);
12538 } else {
12539 mutex_exit(&mpt->m_mutex);
12541 } else {
12542 mutex_exit(&mpt->m_mutex);
12545 if (iport_flag) {
12546 status = scsi_hba_ioctl(dev, cmd, data, mode, credp, rval);
12547 if (status != 0) {
12548 goto out;
12551 * The following code control the OK2RM LED, it doesn't affect
12552 * the ioctl return status.
12554 if ((cmd == DEVCTL_DEVICE_ONLINE) ||
12555 (cmd == DEVCTL_DEVICE_OFFLINE)) {
12556 if (ndi_dc_allochdl((void *)data, &dcp) !=
12557 NDI_SUCCESS) {
12558 goto out;
12560 addr = ndi_dc_getaddr(dcp);
12561 ptgt = mptsas_addr_to_ptgt(mpt, addr, phymask);
12562 if (ptgt == NULL) {
12563 NDBG14(("mptsas_ioctl led control: tgt %s not "
12564 "found", addr));
12565 ndi_dc_freehdl(dcp);
12566 goto out;
12568 mutex_enter(&mpt->m_mutex);
12569 if (cmd == DEVCTL_DEVICE_ONLINE) {
12570 ptgt->m_tgt_unconfigured = 0;
12571 } else if (cmd == DEVCTL_DEVICE_OFFLINE) {
12572 ptgt->m_tgt_unconfigured = 1;
12574 if (cmd == DEVCTL_DEVICE_OFFLINE) {
12575 ptgt->m_led_status |=
12576 (1 << (MPTSAS_LEDCTL_LED_OK2RM - 1));
12577 } else {
12578 ptgt->m_led_status &=
12579 ~(1 << (MPTSAS_LEDCTL_LED_OK2RM - 1));
12581 (void) mptsas_flush_led_status(mpt, ptgt);
12582 mutex_exit(&mpt->m_mutex);
12583 ndi_dc_freehdl(dcp);
12585 goto out;
12587 switch (cmd) {
12588 case MPTIOCTL_GET_DISK_INFO:
12589 status = get_disk_info(mpt, data, mode);
12590 break;
12591 case MPTIOCTL_LED_CONTROL:
12592 status = led_control(mpt, data, mode);
12593 break;
12594 case MPTIOCTL_UPDATE_FLASH:
12595 if (ddi_copyin((void *)data, &flashdata,
12596 sizeof (struct mptsas_update_flash), mode)) {
12597 status = EFAULT;
12598 break;
12601 mutex_enter(&mpt->m_mutex);
12602 if (mptsas_update_flash(mpt,
12603 (caddr_t)(long)flashdata.PtrBuffer,
12604 flashdata.ImageSize, flashdata.ImageType, mode)) {
12605 status = EFAULT;
12609 * Reset the chip to start using the new
12610 * firmware. Reset if failed also.
12612 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12613 if (mptsas_restart_ioc(mpt) == DDI_FAILURE) {
12614 status = EFAULT;
12616 mutex_exit(&mpt->m_mutex);
12617 break;
12618 case MPTIOCTL_PASS_THRU:
12620 * The user has requested to pass through a command to
12621 * be executed by the MPT firmware. Call our routine
12622 * which does this. Only allow one passthru IOCTL at
12623 * one time. Other threads will block on
12624 * m_passthru_mutex, which is of adaptive variant.
12626 if (ddi_copyin((void *)data, &passthru_data,
12627 sizeof (mptsas_pass_thru_t), mode)) {
12628 status = EFAULT;
12629 break;
12631 mutex_enter(&mpt->m_passthru_mutex);
12632 mutex_enter(&mpt->m_mutex);
12633 status = mptsas_pass_thru(mpt, &passthru_data, mode);
12634 mutex_exit(&mpt->m_mutex);
12635 mutex_exit(&mpt->m_passthru_mutex);
12637 break;
12638 case MPTIOCTL_GET_ADAPTER_DATA:
12640 * The user has requested to read adapter data. Call
12641 * our routine which does this.
12643 bzero(&adapter_data, sizeof (mptsas_adapter_data_t));
12644 if (ddi_copyin((void *)data, (void *)&adapter_data,
12645 sizeof (mptsas_adapter_data_t), mode)) {
12646 status = EFAULT;
12647 break;
12649 if (adapter_data.StructureLength >=
12650 sizeof (mptsas_adapter_data_t)) {
12651 adapter_data.StructureLength = (uint32_t)
12652 sizeof (mptsas_adapter_data_t);
12653 copylen = sizeof (mptsas_adapter_data_t);
12654 mutex_enter(&mpt->m_mutex);
12655 mptsas_read_adapter_data(mpt, &adapter_data);
12656 mutex_exit(&mpt->m_mutex);
12657 } else {
12658 adapter_data.StructureLength = (uint32_t)
12659 sizeof (mptsas_adapter_data_t);
12660 copylen = sizeof (adapter_data.StructureLength);
12661 *rval = MPTIOCTL_STATUS_LEN_TOO_SHORT;
12663 if (ddi_copyout((void *)(&adapter_data), (void *)data,
12664 copylen, mode) != 0) {
12665 status = EFAULT;
12667 break;
12668 case MPTIOCTL_GET_PCI_INFO:
12670 * The user has requested to read pci info. Call
12671 * our routine which does this.
12673 bzero(&pci_info, sizeof (mptsas_pci_info_t));
12674 mutex_enter(&mpt->m_mutex);
12675 mptsas_read_pci_info(mpt, &pci_info);
12676 mutex_exit(&mpt->m_mutex);
12677 if (ddi_copyout((void *)(&pci_info), (void *)data,
12678 sizeof (mptsas_pci_info_t), mode) != 0) {
12679 status = EFAULT;
12681 break;
12682 case MPTIOCTL_RESET_ADAPTER:
12683 mutex_enter(&mpt->m_mutex);
12684 mpt->m_softstate &= ~MPTSAS_SS_MSG_UNIT_RESET;
12685 if ((mptsas_restart_ioc(mpt)) == DDI_FAILURE) {
12686 mptsas_log(mpt, CE_WARN, "reset adapter IOCTL "
12687 "failed");
12688 status = EFAULT;
12690 mutex_exit(&mpt->m_mutex);
12691 break;
12692 case MPTIOCTL_DIAG_ACTION:
12694 * The user has done a diag buffer action. Call our
12695 * routine which does this. Only allow one diag action
12696 * at one time.
12698 mutex_enter(&mpt->m_mutex);
12699 if (mpt->m_diag_action_in_progress) {
12700 mutex_exit(&mpt->m_mutex);
12701 return (EBUSY);
12703 mpt->m_diag_action_in_progress = 1;
12704 status = mptsas_diag_action(mpt,
12705 (mptsas_diag_action_t *)data, mode);
12706 mpt->m_diag_action_in_progress = 0;
12707 mutex_exit(&mpt->m_mutex);
12708 break;
12709 case MPTIOCTL_EVENT_QUERY:
12711 * The user has done an event query. Call our routine
12712 * which does this.
12714 status = mptsas_event_query(mpt,
12715 (mptsas_event_query_t *)data, mode, rval);
12716 break;
12717 case MPTIOCTL_EVENT_ENABLE:
12719 * The user has done an event enable. Call our routine
12720 * which does this.
12722 status = mptsas_event_enable(mpt,
12723 (mptsas_event_enable_t *)data, mode, rval);
12724 break;
12725 case MPTIOCTL_EVENT_REPORT:
12727 * The user has done an event report. Call our routine
12728 * which does this.
12730 status = mptsas_event_report(mpt,
12731 (mptsas_event_report_t *)data, mode, rval);
12732 break;
12733 case MPTIOCTL_REG_ACCESS:
12735 * The user has requested register access. Call our
12736 * routine which does this.
12738 status = mptsas_reg_access(mpt,
12739 (mptsas_reg_access_t *)data, mode);
12740 break;
12741 default:
12742 status = scsi_hba_ioctl(dev, cmd, data, mode, credp,
12743 rval);
12744 break;
12747 out:
12748 return (status);
12752 mptsas_restart_ioc(mptsas_t *mpt)
12754 int rval = DDI_SUCCESS;
12755 mptsas_target_t *ptgt = NULL;
12757 ASSERT(mutex_owned(&mpt->m_mutex));
12760 * Set a flag telling I/O path that we're processing a reset. This is
12761 * needed because after the reset is complete, the hash table still
12762 * needs to be rebuilt. If I/Os are started before the hash table is
12763 * rebuilt, I/O errors will occur. This flag allows I/Os to be marked
12764 * so that they can be retried.
12766 mpt->m_in_reset = TRUE;
12769 * Wait until all the allocated sense data buffers for DMA are freed.
12771 while (mpt->m_extreq_sense_refcount > 0)
12772 cv_wait(&mpt->m_extreq_sense_refcount_cv, &mpt->m_mutex);
12775 * Set all throttles to HOLD
12777 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12778 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12779 mptsas_set_throttle(mpt, ptgt, HOLD_THROTTLE);
12783 * Disable interrupts
12785 MPTSAS_DISABLE_INTR(mpt);
12788 * Abort all commands: outstanding commands, commands in waitq and
12789 * tx_waitq.
12791 mptsas_flush_hba(mpt);
12794 * Reinitialize the chip.
12796 if (mptsas_init_chip(mpt, FALSE) == DDI_FAILURE) {
12797 rval = DDI_FAILURE;
12801 * Enable interrupts again
12803 MPTSAS_ENABLE_INTR(mpt);
12806 * If mptsas_init_chip was successful, update the driver data.
12808 if (rval == DDI_SUCCESS) {
12809 mptsas_update_driver_data(mpt);
12813 * Reset the throttles
12815 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
12816 ptgt = refhash_next(mpt->m_targets, ptgt)) {
12817 mptsas_set_throttle(mpt, ptgt, MAX_THROTTLE);
12820 mptsas_doneq_empty(mpt);
12821 mptsas_restart_hba(mpt);
12823 if (rval != DDI_SUCCESS) {
12824 mptsas_fm_ereport(mpt, DDI_FM_DEVICE_NO_RESPONSE);
12825 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_LOST);
12829 * Clear the reset flag so that I/Os can continue.
12831 mpt->m_in_reset = FALSE;
12833 return (rval);
12836 static int
12837 mptsas_init_chip(mptsas_t *mpt, int first_time)
12839 ddi_dma_cookie_t cookie;
12840 uint32_t i;
12841 int rval;
12844 * Check to see if the firmware image is valid
12846 if (ddi_get32(mpt->m_datap, &mpt->m_reg->HostDiagnostic) &
12847 MPI2_DIAG_FLASH_BAD_SIG) {
12848 mptsas_log(mpt, CE_WARN, "mptsas bad flash signature!");
12849 goto fail;
12853 * Reset the chip
12855 rval = mptsas_ioc_reset(mpt, first_time);
12856 if (rval == MPTSAS_RESET_FAIL) {
12857 mptsas_log(mpt, CE_WARN, "hard reset failed!");
12858 goto fail;
12861 if ((rval == MPTSAS_SUCCESS_MUR) && (!first_time)) {
12862 goto mur;
12865 * Setup configuration space
12867 if (mptsas_config_space_init(mpt) == FALSE) {
12868 mptsas_log(mpt, CE_WARN, "mptsas_config_space_init "
12869 "failed!");
12870 goto fail;
12874 * IOC facts can change after a diag reset so all buffers that are
12875 * based on these numbers must be de-allocated and re-allocated. Get
12876 * new IOC facts each time chip is initialized.
12878 if (mptsas_ioc_get_facts(mpt) == DDI_FAILURE) {
12879 mptsas_log(mpt, CE_WARN, "mptsas_ioc_get_facts failed");
12880 goto fail;
12883 if (mptsas_alloc_active_slots(mpt, KM_SLEEP)) {
12884 goto fail;
12887 * Allocate request message frames, reply free queue, reply descriptor
12888 * post queue, and reply message frames using latest IOC facts.
12890 if (mptsas_alloc_request_frames(mpt) == DDI_FAILURE) {
12891 mptsas_log(mpt, CE_WARN, "mptsas_alloc_request_frames failed");
12892 goto fail;
12894 if (mptsas_alloc_sense_bufs(mpt) == DDI_FAILURE) {
12895 mptsas_log(mpt, CE_WARN, "mptsas_alloc_sense_bufs failed");
12896 goto fail;
12898 if (mptsas_alloc_free_queue(mpt) == DDI_FAILURE) {
12899 mptsas_log(mpt, CE_WARN, "mptsas_alloc_free_queue failed!");
12900 goto fail;
12902 if (mptsas_alloc_post_queue(mpt) == DDI_FAILURE) {
12903 mptsas_log(mpt, CE_WARN, "mptsas_alloc_post_queue failed!");
12904 goto fail;
12906 if (mptsas_alloc_reply_frames(mpt) == DDI_FAILURE) {
12907 mptsas_log(mpt, CE_WARN, "mptsas_alloc_reply_frames failed!");
12908 goto fail;
12911 mur:
12913 * Re-Initialize ioc to operational state
12915 if (mptsas_ioc_init(mpt) == DDI_FAILURE) {
12916 mptsas_log(mpt, CE_WARN, "mptsas_ioc_init failed");
12917 goto fail;
12920 mptsas_alloc_reply_args(mpt);
12923 * Initialize reply post index. Reply free index is initialized after
12924 * the next loop.
12926 mpt->m_post_index = 0;
12929 * Initialize the Reply Free Queue with the physical addresses of our
12930 * reply frames.
12932 cookie.dmac_address = mpt->m_reply_frame_dma_addr & 0xffffffffu;
12933 for (i = 0; i < mpt->m_max_replies; i++) {
12934 ddi_put32(mpt->m_acc_free_queue_hdl,
12935 &((uint32_t *)(void *)mpt->m_free_queue)[i],
12936 cookie.dmac_address);
12937 cookie.dmac_address += mpt->m_reply_frame_size;
12939 (void) ddi_dma_sync(mpt->m_dma_free_queue_hdl, 0, 0,
12940 DDI_DMA_SYNC_FORDEV);
12943 * Initialize the reply free index to one past the last frame on the
12944 * queue. This will signify that the queue is empty to start with.
12946 mpt->m_free_index = i;
12947 ddi_put32(mpt->m_datap, &mpt->m_reg->ReplyFreeHostIndex, i);
12950 * Initialize the reply post queue to 0xFFFFFFFF,0xFFFFFFFF's.
12952 for (i = 0; i < mpt->m_post_queue_depth; i++) {
12953 ddi_put64(mpt->m_acc_post_queue_hdl,
12954 &((uint64_t *)(void *)mpt->m_post_queue)[i],
12955 0xFFFFFFFFFFFFFFFF);
12957 (void) ddi_dma_sync(mpt->m_dma_post_queue_hdl, 0, 0,
12958 DDI_DMA_SYNC_FORDEV);
12961 * Enable ports
12963 if (mptsas_ioc_enable_port(mpt) == DDI_FAILURE) {
12964 mptsas_log(mpt, CE_WARN, "mptsas_ioc_enable_port failed");
12965 goto fail;
12969 * enable events
12971 if (mptsas_ioc_enable_event_notification(mpt)) {
12972 mptsas_log(mpt, CE_WARN,
12973 "mptsas_ioc_enable_event_notification failed");
12974 goto fail;
12978 * We need checks in attach and these.
12979 * chip_init is called in mult. places
12982 if ((mptsas_check_dma_handle(mpt->m_dma_req_frame_hdl) !=
12983 DDI_SUCCESS) ||
12984 (mptsas_check_dma_handle(mpt->m_dma_req_sense_hdl) !=
12985 DDI_SUCCESS) ||
12986 (mptsas_check_dma_handle(mpt->m_dma_reply_frame_hdl) !=
12987 DDI_SUCCESS) ||
12988 (mptsas_check_dma_handle(mpt->m_dma_free_queue_hdl) !=
12989 DDI_SUCCESS) ||
12990 (mptsas_check_dma_handle(mpt->m_dma_post_queue_hdl) !=
12991 DDI_SUCCESS) ||
12992 (mptsas_check_dma_handle(mpt->m_hshk_dma_hdl) !=
12993 DDI_SUCCESS)) {
12994 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
12995 goto fail;
12998 /* Check all acc handles */
12999 if ((mptsas_check_acc_handle(mpt->m_datap) != DDI_SUCCESS) ||
13000 (mptsas_check_acc_handle(mpt->m_acc_req_frame_hdl) !=
13001 DDI_SUCCESS) ||
13002 (mptsas_check_acc_handle(mpt->m_acc_req_sense_hdl) !=
13003 DDI_SUCCESS) ||
13004 (mptsas_check_acc_handle(mpt->m_acc_reply_frame_hdl) !=
13005 DDI_SUCCESS) ||
13006 (mptsas_check_acc_handle(mpt->m_acc_free_queue_hdl) !=
13007 DDI_SUCCESS) ||
13008 (mptsas_check_acc_handle(mpt->m_acc_post_queue_hdl) !=
13009 DDI_SUCCESS) ||
13010 (mptsas_check_acc_handle(mpt->m_hshk_acc_hdl) !=
13011 DDI_SUCCESS) ||
13012 (mptsas_check_acc_handle(mpt->m_config_handle) !=
13013 DDI_SUCCESS)) {
13014 ddi_fm_service_impact(mpt->m_dip, DDI_SERVICE_UNAFFECTED);
13015 goto fail;
13018 return (DDI_SUCCESS);
13020 fail:
13021 return (DDI_FAILURE);
13024 static int
13025 mptsas_get_pci_cap(mptsas_t *mpt)
13027 ushort_t caps_ptr, cap, cap_count;
13029 if (mpt->m_config_handle == NULL)
13030 return (FALSE);
13032 * Check if capabilities list is supported and if so,
13033 * get initial capabilities pointer and clear bits 0,1.
13035 if (pci_config_get16(mpt->m_config_handle, PCI_CONF_STAT)
13036 & PCI_STAT_CAP) {
13037 caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
13038 PCI_CONF_CAP_PTR), 4);
13039 } else {
13040 caps_ptr = PCI_CAP_NEXT_PTR_NULL;
13044 * Walk capabilities if supported.
13046 for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
13049 * Check that we haven't exceeded the maximum number of
13050 * capabilities and that the pointer is in a valid range.
13052 if (++cap_count > 48) {
13053 mptsas_log(mpt, CE_WARN,
13054 "too many device capabilities.\n");
13055 break;
13057 if (caps_ptr < 64) {
13058 mptsas_log(mpt, CE_WARN,
13059 "capabilities pointer 0x%x out of range.\n",
13060 caps_ptr);
13061 break;
13065 * Get next capability and check that it is valid.
13066 * For now, we only support power management.
13068 cap = pci_config_get8(mpt->m_config_handle, caps_ptr);
13069 switch (cap) {
13070 case PCI_CAP_ID_PM:
13071 mptsas_log(mpt, CE_NOTE,
13072 "?mptsas%d supports power management.\n",
13073 mpt->m_instance);
13074 mpt->m_options |= MPTSAS_OPT_PM;
13076 /* Save PMCSR offset */
13077 mpt->m_pmcsr_offset = caps_ptr + PCI_PMCSR;
13078 break;
13080 * The following capabilities are valid. Any others
13081 * will cause a message to be logged.
13083 case PCI_CAP_ID_VPD:
13084 case PCI_CAP_ID_MSI:
13085 case PCI_CAP_ID_PCIX:
13086 case PCI_CAP_ID_PCI_E:
13087 case PCI_CAP_ID_MSI_X:
13088 break;
13089 default:
13090 mptsas_log(mpt, CE_NOTE,
13091 "?mptsas%d unrecognized capability "
13092 "0x%x.\n", mpt->m_instance, cap);
13093 break;
13097 * Get next capabilities pointer and clear bits 0,1.
13099 caps_ptr = P2ALIGN(pci_config_get8(mpt->m_config_handle,
13100 (caps_ptr + PCI_CAP_NEXT_PTR)), 4);
13102 return (TRUE);
13105 static int
13106 mptsas_init_pm(mptsas_t *mpt)
13108 char pmc_name[16];
13109 char *pmc[] = {
13110 NULL,
13111 "0=Off (PCI D3 State)",
13112 "3=On (PCI D0 State)",
13113 NULL
13115 uint16_t pmcsr_stat;
13117 if (mptsas_get_pci_cap(mpt) == FALSE) {
13118 return (DDI_FAILURE);
13121 * If PCI's capability does not support PM, then don't need
13122 * to registe the pm-components
13124 if (!(mpt->m_options & MPTSAS_OPT_PM))
13125 return (DDI_SUCCESS);
13127 * If power management is supported by this chip, create
13128 * pm-components property for the power management framework
13130 (void) sprintf(pmc_name, "NAME=mptsas%d", mpt->m_instance);
13131 pmc[0] = pmc_name;
13132 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, mpt->m_dip,
13133 "pm-components", pmc, 3) != DDI_PROP_SUCCESS) {
13134 mpt->m_options &= ~MPTSAS_OPT_PM;
13135 mptsas_log(mpt, CE_WARN,
13136 "mptsas%d: pm-component property creation failed.",
13137 mpt->m_instance);
13138 return (DDI_FAILURE);
13142 * Power on device.
13144 (void) pm_busy_component(mpt->m_dip, 0);
13145 pmcsr_stat = pci_config_get16(mpt->m_config_handle,
13146 mpt->m_pmcsr_offset);
13147 if ((pmcsr_stat & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_D0) {
13148 mptsas_log(mpt, CE_WARN, "mptsas%d: Power up the device",
13149 mpt->m_instance);
13150 pci_config_put16(mpt->m_config_handle, mpt->m_pmcsr_offset,
13151 PCI_PMCSR_D0);
13153 if (pm_power_has_changed(mpt->m_dip, 0, PM_LEVEL_D0) != DDI_SUCCESS) {
13154 mptsas_log(mpt, CE_WARN, "pm_power_has_changed failed");
13155 return (DDI_FAILURE);
13157 mpt->m_power_level = PM_LEVEL_D0;
13159 * Set pm idle delay.
13161 mpt->m_pm_idle_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
13162 mpt->m_dip, 0, "mptsas-pm-idle-delay", MPTSAS_PM_IDLE_TIMEOUT);
13164 return (DDI_SUCCESS);
13167 static int
13168 mptsas_register_intrs(mptsas_t *mpt)
13170 dev_info_t *dip;
13171 int intr_types;
13173 dip = mpt->m_dip;
13175 /* Get supported interrupt types */
13176 if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
13177 mptsas_log(mpt, CE_WARN, "ddi_intr_get_supported_types "
13178 "failed\n");
13179 return (FALSE);
13182 NDBG6(("ddi_intr_get_supported_types() returned: 0x%x", intr_types));
13185 * Try MSI, but fall back to FIXED
13187 if (mptsas_enable_msi && (intr_types & DDI_INTR_TYPE_MSI)) {
13188 if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_MSI) == DDI_SUCCESS) {
13189 NDBG0(("Using MSI interrupt type"));
13190 mpt->m_intr_type = DDI_INTR_TYPE_MSI;
13191 return (TRUE);
13194 if (intr_types & DDI_INTR_TYPE_FIXED) {
13195 if (mptsas_add_intrs(mpt, DDI_INTR_TYPE_FIXED) == DDI_SUCCESS) {
13196 NDBG0(("Using FIXED interrupt type"));
13197 mpt->m_intr_type = DDI_INTR_TYPE_FIXED;
13198 return (TRUE);
13199 } else {
13200 NDBG0(("FIXED interrupt registration failed"));
13201 return (FALSE);
13205 return (FALSE);
13208 static void
13209 mptsas_unregister_intrs(mptsas_t *mpt)
13211 mptsas_rem_intrs(mpt);
13215 * mptsas_add_intrs:
13217 * Register FIXED or MSI interrupts.
13219 static int
13220 mptsas_add_intrs(mptsas_t *mpt, int intr_type)
13222 dev_info_t *dip = mpt->m_dip;
13223 int avail, actual, count = 0;
13224 int i, flag, ret;
13226 NDBG6(("mptsas_add_intrs:interrupt type 0x%x", intr_type));
13228 /* Get number of interrupts */
13229 ret = ddi_intr_get_nintrs(dip, intr_type, &count);
13230 if ((ret != DDI_SUCCESS) || (count <= 0)) {
13231 mptsas_log(mpt, CE_WARN, "ddi_intr_get_nintrs() failed, "
13232 "ret %d count %d\n", ret, count);
13234 return (DDI_FAILURE);
13237 /* Get number of available interrupts */
13238 ret = ddi_intr_get_navail(dip, intr_type, &avail);
13239 if ((ret != DDI_SUCCESS) || (avail == 0)) {
13240 mptsas_log(mpt, CE_WARN, "ddi_intr_get_navail() failed, "
13241 "ret %d avail %d\n", ret, avail);
13243 return (DDI_FAILURE);
13246 if (avail < count) {
13247 mptsas_log(mpt, CE_NOTE, "ddi_intr_get_nvail returned %d, "
13248 "navail() returned %d", count, avail);
13251 /* Mpt only have one interrupt routine */
13252 if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
13253 count = 1;
13256 /* Allocate an array of interrupt handles */
13257 mpt->m_intr_size = count * sizeof (ddi_intr_handle_t);
13258 mpt->m_htable = kmem_alloc(mpt->m_intr_size, KM_SLEEP);
13260 flag = DDI_INTR_ALLOC_NORMAL;
13262 /* call ddi_intr_alloc() */
13263 ret = ddi_intr_alloc(dip, mpt->m_htable, intr_type, 0,
13264 count, &actual, flag);
13266 if ((ret != DDI_SUCCESS) || (actual == 0)) {
13267 mptsas_log(mpt, CE_WARN, "ddi_intr_alloc() failed, ret %d\n",
13268 ret);
13269 kmem_free(mpt->m_htable, mpt->m_intr_size);
13270 return (DDI_FAILURE);
13273 /* use interrupt count returned or abort? */
13274 if (actual < count) {
13275 mptsas_log(mpt, CE_NOTE, "Requested: %d, Received: %d\n",
13276 count, actual);
13279 mpt->m_intr_cnt = actual;
13282 * Get priority for first msi, assume remaining are all the same
13284 if ((ret = ddi_intr_get_pri(mpt->m_htable[0],
13285 &mpt->m_intr_pri)) != DDI_SUCCESS) {
13286 mptsas_log(mpt, CE_WARN, "ddi_intr_get_pri() failed %d\n", ret);
13288 /* Free already allocated intr */
13289 for (i = 0; i < actual; i++) {
13290 (void) ddi_intr_free(mpt->m_htable[i]);
13293 kmem_free(mpt->m_htable, mpt->m_intr_size);
13294 return (DDI_FAILURE);
13297 /* Test for high level mutex */
13298 if (mpt->m_intr_pri >= ddi_intr_get_hilevel_pri()) {
13299 mptsas_log(mpt, CE_WARN, "mptsas_add_intrs: "
13300 "Hi level interrupt not supported\n");
13302 /* Free already allocated intr */
13303 for (i = 0; i < actual; i++) {
13304 (void) ddi_intr_free(mpt->m_htable[i]);
13307 kmem_free(mpt->m_htable, mpt->m_intr_size);
13308 return (DDI_FAILURE);
13311 /* Call ddi_intr_add_handler() */
13312 for (i = 0; i < actual; i++) {
13313 if ((ret = ddi_intr_add_handler(mpt->m_htable[i], mptsas_intr,
13314 (caddr_t)mpt, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
13315 mptsas_log(mpt, CE_WARN, "ddi_intr_add_handler() "
13316 "failed %d\n", ret);
13318 /* Free already allocated intr */
13319 for (i = 0; i < actual; i++) {
13320 (void) ddi_intr_free(mpt->m_htable[i]);
13323 kmem_free(mpt->m_htable, mpt->m_intr_size);
13324 return (DDI_FAILURE);
13328 if ((ret = ddi_intr_get_cap(mpt->m_htable[0], &mpt->m_intr_cap))
13329 != DDI_SUCCESS) {
13330 mptsas_log(mpt, CE_WARN, "ddi_intr_get_cap() failed %d\n", ret);
13332 /* Free already allocated intr */
13333 for (i = 0; i < actual; i++) {
13334 (void) ddi_intr_free(mpt->m_htable[i]);
13337 kmem_free(mpt->m_htable, mpt->m_intr_size);
13338 return (DDI_FAILURE);
13342 * Enable interrupts
13344 if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
13345 /* Call ddi_intr_block_enable() for MSI interrupts */
13346 (void) ddi_intr_block_enable(mpt->m_htable, mpt->m_intr_cnt);
13347 } else {
13348 /* Call ddi_intr_enable for MSI or FIXED interrupts */
13349 for (i = 0; i < mpt->m_intr_cnt; i++) {
13350 (void) ddi_intr_enable(mpt->m_htable[i]);
13353 return (DDI_SUCCESS);
13357 * mptsas_rem_intrs:
13359 * Unregister FIXED or MSI interrupts
13361 static void
13362 mptsas_rem_intrs(mptsas_t *mpt)
13364 int i;
13366 NDBG6(("mptsas_rem_intrs"));
13368 /* Disable all interrupts */
13369 if (mpt->m_intr_cap & DDI_INTR_FLAG_BLOCK) {
13370 /* Call ddi_intr_block_disable() */
13371 (void) ddi_intr_block_disable(mpt->m_htable, mpt->m_intr_cnt);
13372 } else {
13373 for (i = 0; i < mpt->m_intr_cnt; i++) {
13374 (void) ddi_intr_disable(mpt->m_htable[i]);
13378 /* Call ddi_intr_remove_handler() */
13379 for (i = 0; i < mpt->m_intr_cnt; i++) {
13380 (void) ddi_intr_remove_handler(mpt->m_htable[i]);
13381 (void) ddi_intr_free(mpt->m_htable[i]);
13384 kmem_free(mpt->m_htable, mpt->m_intr_size);
13388 * The IO fault service error handling callback function
13390 /*ARGSUSED*/
13391 static int
13392 mptsas_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
13395 * as the driver can always deal with an error in any dma or
13396 * access handle, we can just return the fme_status value.
13398 pci_ereport_post(dip, err, NULL);
13399 return (err->fme_status);
13403 * mptsas_fm_init - initialize fma capabilities and register with IO
13404 * fault services.
13406 static void
13407 mptsas_fm_init(mptsas_t *mpt)
13410 * Need to change iblock to priority for new MSI intr
13412 ddi_iblock_cookie_t fm_ibc;
13414 /* Only register with IO Fault Services if we have some capability */
13415 if (mpt->m_fm_capabilities) {
13416 /* Adjust access and dma attributes for FMA */
13417 mpt->m_reg_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
13418 mpt->m_msg_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
13419 mpt->m_io_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
13422 * Register capabilities with IO Fault Services.
13423 * mpt->m_fm_capabilities will be updated to indicate
13424 * capabilities actually supported (not requested.)
13426 ddi_fm_init(mpt->m_dip, &mpt->m_fm_capabilities, &fm_ibc);
13429 * Initialize pci ereport capabilities if ereport
13430 * capable (should always be.)
13432 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
13433 DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13434 pci_ereport_setup(mpt->m_dip);
13438 * Register error callback if error callback capable.
13440 if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13441 ddi_fm_handler_register(mpt->m_dip,
13442 mptsas_fm_error_cb, (void *) mpt);
13448 * mptsas_fm_fini - Releases fma capabilities and un-registers with IO
13449 * fault services.
13452 static void
13453 mptsas_fm_fini(mptsas_t *mpt)
13455 /* Only unregister FMA capabilities if registered */
13456 if (mpt->m_fm_capabilities) {
13459 * Un-register error callback if error callback capable.
13462 if (DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13463 ddi_fm_handler_unregister(mpt->m_dip);
13467 * Release any resources allocated by pci_ereport_setup()
13470 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities) ||
13471 DDI_FM_ERRCB_CAP(mpt->m_fm_capabilities)) {
13472 pci_ereport_teardown(mpt->m_dip);
13475 /* Unregister from IO Fault Services */
13476 ddi_fm_fini(mpt->m_dip);
13478 /* Adjust access and dma attributes for FMA */
13479 mpt->m_reg_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
13480 mpt->m_msg_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
13481 mpt->m_io_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
13487 mptsas_check_acc_handle(ddi_acc_handle_t handle)
13489 ddi_fm_error_t de;
13491 if (handle == NULL)
13492 return (DDI_FAILURE);
13493 ddi_fm_acc_err_get(handle, &de, DDI_FME_VER0);
13494 return (de.fme_status);
13498 mptsas_check_dma_handle(ddi_dma_handle_t handle)
13500 ddi_fm_error_t de;
13502 if (handle == NULL)
13503 return (DDI_FAILURE);
13504 ddi_fm_dma_err_get(handle, &de, DDI_FME_VER0);
13505 return (de.fme_status);
13508 void
13509 mptsas_fm_ereport(mptsas_t *mpt, char *detail)
13511 uint64_t ena;
13512 char buf[FM_MAX_CLASS];
13514 (void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
13515 ena = fm_ena_generate(0, FM_ENA_FMT1);
13516 if (DDI_FM_EREPORT_CAP(mpt->m_fm_capabilities)) {
13517 ddi_fm_ereport_post(mpt->m_dip, buf, ena, DDI_NOSLEEP,
13518 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
13522 static int
13523 mptsas_get_target_device_info(mptsas_t *mpt, uint32_t page_address,
13524 uint16_t *dev_handle, mptsas_target_t **pptgt)
13526 int rval;
13527 uint32_t dev_info;
13528 uint64_t sas_wwn;
13529 mptsas_phymask_t phymask;
13530 uint8_t physport, phynum, config, disk;
13531 uint64_t devicename;
13532 uint16_t pdev_hdl;
13533 mptsas_target_t *tmp_tgt = NULL;
13534 uint16_t bay_num, enclosure, io_flags;
13536 ASSERT(*pptgt == NULL);
13538 rval = mptsas_get_sas_device_page0(mpt, page_address, dev_handle,
13539 &sas_wwn, &dev_info, &physport, &phynum, &pdev_hdl,
13540 &bay_num, &enclosure, &io_flags);
13541 if (rval != DDI_SUCCESS) {
13542 rval = DEV_INFO_FAIL_PAGE0;
13543 return (rval);
13546 if ((dev_info & (MPI2_SAS_DEVICE_INFO_SSP_TARGET |
13547 MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13548 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) == 0) {
13549 rval = DEV_INFO_WRONG_DEVICE_TYPE;
13550 return (rval);
13554 * Check if the dev handle is for a Phys Disk. If so, set return value
13555 * and exit. Don't add Phys Disks to hash.
13557 for (config = 0; config < mpt->m_num_raid_configs; config++) {
13558 for (disk = 0; disk < MPTSAS_MAX_DISKS_IN_CONFIG; disk++) {
13559 if (*dev_handle == mpt->m_raidconfig[config].
13560 m_physdisk_devhdl[disk]) {
13561 rval = DEV_INFO_PHYS_DISK;
13562 return (rval);
13568 * Get SATA Device Name from SAS device page0 for
13569 * sata device, if device name doesn't exist, set mta_wwn to
13570 * 0 for direct attached SATA. For the device behind the expander
13571 * we still can use STP address assigned by expander.
13573 if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
13574 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
13575 /* alloc a temporary target to send the cmd to */
13576 tmp_tgt = mptsas_tgt_alloc(mpt->m_tmp_targets, *dev_handle,
13577 0, dev_info, 0, 0);
13578 mutex_exit(&mpt->m_mutex);
13580 devicename = mptsas_get_sata_guid(mpt, tmp_tgt, 0);
13582 if (devicename == -1) {
13583 mutex_enter(&mpt->m_mutex);
13584 refhash_remove(mpt->m_tmp_targets, tmp_tgt);
13585 rval = DEV_INFO_FAIL_GUID;
13586 return (rval);
13589 if (devicename != 0 && (((devicename >> 56) & 0xf0) == 0x50)) {
13590 sas_wwn = devicename;
13591 } else if (dev_info & MPI2_SAS_DEVICE_INFO_DIRECT_ATTACH) {
13592 sas_wwn = 0;
13595 mutex_enter(&mpt->m_mutex);
13596 refhash_remove(mpt->m_tmp_targets, tmp_tgt);
13599 phymask = mptsas_physport_to_phymask(mpt, physport);
13600 *pptgt = mptsas_tgt_alloc(mpt->m_targets, *dev_handle, sas_wwn,
13601 dev_info, phymask, phynum);
13602 if (*pptgt == NULL) {
13603 mptsas_log(mpt, CE_WARN, "Failed to allocated target"
13604 "structure!");
13605 rval = DEV_INFO_FAIL_ALLOC;
13606 return (rval);
13608 (*pptgt)->m_io_flags = io_flags;
13609 (*pptgt)->m_enclosure = enclosure;
13610 (*pptgt)->m_slot_num = bay_num;
13611 return (DEV_INFO_SUCCESS);
13614 uint64_t
13615 mptsas_get_sata_guid(mptsas_t *mpt, mptsas_target_t *ptgt, int lun)
13617 uint64_t sata_guid = 0, *pwwn = NULL;
13618 int target = ptgt->m_devhdl;
13619 uchar_t *inq83 = NULL;
13620 int inq83_len = 0xFF;
13621 uchar_t *dblk = NULL;
13622 int inq83_retry = 3;
13623 int rval = DDI_FAILURE;
13625 inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
13627 inq83_retry:
13628 rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
13629 inq83_len, NULL, 1);
13630 if (rval != DDI_SUCCESS) {
13631 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
13632 "0x83 for target:%x, lun:%x failed!", target, lun);
13633 sata_guid = -1;
13634 goto out;
13636 /* According to SAT2, the first descriptor is logic unit name */
13637 dblk = &inq83[4];
13638 if ((dblk[1] & 0x30) != 0) {
13639 mptsas_log(mpt, CE_WARN, "!Descriptor is not lun associated.");
13640 goto out;
13642 pwwn = (uint64_t *)(void *)(&dblk[4]);
13643 if ((dblk[4] & 0xf0) == 0x50) {
13644 sata_guid = BE_64(*pwwn);
13645 goto out;
13646 } else if (dblk[4] == 'A') {
13647 NDBG20(("SATA drive has no NAA format GUID."));
13648 goto out;
13649 } else {
13650 /* The data is not ready, wait and retry */
13651 inq83_retry--;
13652 if (inq83_retry <= 0) {
13653 goto out;
13655 NDBG20(("The GUID is not ready, retry..."));
13656 ddi_sleep(1);
13657 goto inq83_retry;
13659 out:
13660 kmem_free(inq83, inq83_len);
13661 return (sata_guid);
13664 static int
13665 mptsas_inquiry(mptsas_t *mpt, mptsas_target_t *ptgt, int lun, uchar_t page,
13666 unsigned char *buf, int len, int *reallen, uchar_t evpd)
13668 uchar_t cdb[CDB_GROUP0];
13669 struct scsi_address ap;
13670 struct buf *data_bp = NULL;
13671 int resid = 0;
13672 int ret = DDI_FAILURE;
13674 ASSERT(len <= 0xffff);
13676 ap.a_target = MPTSAS_INVALID_DEVHDL;
13677 ap.a_lun = (uchar_t)(lun);
13678 ap.a_hba_tran = mpt->m_tran;
13680 data_bp = scsi_alloc_consistent_buf(&ap,
13681 NULL, len, B_READ, NULL_FUNC, NULL);
13682 if (data_bp == NULL) {
13683 return (ret);
13685 bzero(cdb, CDB_GROUP0);
13686 cdb[0] = SCMD_INQUIRY;
13687 cdb[1] = evpd;
13688 cdb[2] = page;
13689 cdb[3] = (len & 0xff00) >> 8;
13690 cdb[4] = (len & 0x00ff);
13691 cdb[5] = 0;
13693 ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP0, data_bp,
13694 &resid);
13695 if (ret == DDI_SUCCESS) {
13696 if (reallen) {
13697 *reallen = len - resid;
13699 bcopy((caddr_t)data_bp->b_un.b_addr, buf, len);
13701 if (data_bp) {
13702 scsi_free_consistent_buf(data_bp);
13704 return (ret);
13707 static int
13708 mptsas_send_scsi_cmd(mptsas_t *mpt, struct scsi_address *ap,
13709 mptsas_target_t *ptgt, uchar_t *cdb, int cdblen, struct buf *data_bp,
13710 int *resid)
13712 struct scsi_pkt *pktp = NULL;
13713 scsi_hba_tran_t *tran_clone = NULL;
13714 mptsas_tgt_private_t *tgt_private = NULL;
13715 int ret = DDI_FAILURE;
13718 * scsi_hba_tran_t->tran_tgt_private is used to pass the address
13719 * information to scsi_init_pkt, allocate a scsi_hba_tran structure
13720 * to simulate the cmds from sd
13722 tran_clone = kmem_alloc(
13723 sizeof (scsi_hba_tran_t), KM_SLEEP);
13724 if (tran_clone == NULL) {
13725 goto out;
13727 bcopy((caddr_t)mpt->m_tran,
13728 (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
13729 tgt_private = kmem_alloc(
13730 sizeof (mptsas_tgt_private_t), KM_SLEEP);
13731 if (tgt_private == NULL) {
13732 goto out;
13734 tgt_private->t_lun = ap->a_lun;
13735 tgt_private->t_private = ptgt;
13736 tran_clone->tran_tgt_private = tgt_private;
13737 ap->a_hba_tran = tran_clone;
13739 pktp = scsi_init_pkt(ap, NULL,
13740 data_bp, cdblen, sizeof (struct scsi_arq_status),
13741 0, PKT_CONSISTENT, NULL, NULL);
13742 if (pktp == NULL) {
13743 goto out;
13745 bcopy(cdb, pktp->pkt_cdbp, cdblen);
13746 pktp->pkt_flags = FLAG_NOPARITY;
13747 if (scsi_poll(pktp) < 0) {
13748 goto out;
13750 if (((struct scsi_status *)pktp->pkt_scbp)->sts_chk) {
13751 goto out;
13753 if (resid != NULL) {
13754 *resid = pktp->pkt_resid;
13757 ret = DDI_SUCCESS;
13758 out:
13759 if (pktp) {
13760 scsi_destroy_pkt(pktp);
13762 if (tran_clone) {
13763 kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
13765 if (tgt_private) {
13766 kmem_free(tgt_private, sizeof (mptsas_tgt_private_t));
13768 return (ret);
13770 static int
13771 mptsas_parse_address(char *name, uint64_t *wwid, uint8_t *phy, int *lun)
13773 char *cp = NULL;
13774 char *ptr = NULL;
13775 size_t s = 0;
13776 char *wwid_str = NULL;
13777 char *lun_str = NULL;
13778 long lunnum;
13779 long phyid = -1;
13780 int rc = DDI_FAILURE;
13782 ptr = name;
13783 ASSERT(ptr[0] == 'w' || ptr[0] == 'p');
13784 ptr++;
13785 if ((cp = strchr(ptr, ',')) == NULL) {
13786 return (DDI_FAILURE);
13789 wwid_str = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13790 s = (uintptr_t)cp - (uintptr_t)ptr;
13792 bcopy(ptr, wwid_str, s);
13793 wwid_str[s] = '\0';
13795 ptr = ++cp;
13797 if ((cp = strchr(ptr, '\0')) == NULL) {
13798 goto out;
13800 lun_str = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13801 s = (uintptr_t)cp - (uintptr_t)ptr;
13803 bcopy(ptr, lun_str, s);
13804 lun_str[s] = '\0';
13806 if (name[0] == 'p') {
13807 rc = ddi_strtol(wwid_str, NULL, 0x10, &phyid);
13808 } else {
13809 rc = scsi_wwnstr_to_wwn(wwid_str, wwid);
13811 if (rc != DDI_SUCCESS)
13812 goto out;
13814 if (phyid != -1) {
13815 ASSERT(phyid < MPTSAS_MAX_PHYS);
13816 *phy = (uint8_t)phyid;
13818 rc = ddi_strtol(lun_str, NULL, 0x10, &lunnum);
13819 if (rc != 0)
13820 goto out;
13822 *lun = (int)lunnum;
13823 rc = DDI_SUCCESS;
13824 out:
13825 if (wwid_str)
13826 kmem_free(wwid_str, SCSI_MAXNAMELEN);
13827 if (lun_str)
13828 kmem_free(lun_str, SCSI_MAXNAMELEN);
13830 return (rc);
13834 * mptsas_parse_smp_name() is to parse sas wwn string
13835 * which format is "wWWN"
13837 static int
13838 mptsas_parse_smp_name(char *name, uint64_t *wwn)
13840 char *ptr = name;
13842 if (*ptr != 'w') {
13843 return (DDI_FAILURE);
13846 ptr++;
13847 if (scsi_wwnstr_to_wwn(ptr, wwn)) {
13848 return (DDI_FAILURE);
13850 return (DDI_SUCCESS);
13853 static int
13854 mptsas_bus_config(dev_info_t *pdip, uint_t flag,
13855 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
13857 int ret = NDI_FAILURE;
13858 int circ = 0;
13859 int circ1 = 0;
13860 mptsas_t *mpt;
13861 char *ptr = NULL;
13862 char *devnm = NULL;
13863 uint64_t wwid = 0;
13864 uint8_t phy = 0xFF;
13865 int lun = 0;
13866 uint_t mflags = flag;
13867 int bconfig = TRUE;
13869 if (scsi_hba_iport_unit_address(pdip) == 0) {
13870 return (DDI_FAILURE);
13873 mpt = DIP2MPT(pdip);
13874 if (!mpt) {
13875 return (DDI_FAILURE);
13878 * Hold the nexus across the bus_config
13880 ndi_devi_enter(scsi_vhci_dip, &circ);
13881 ndi_devi_enter(pdip, &circ1);
13882 switch (op) {
13883 case BUS_CONFIG_ONE:
13884 /* parse wwid/target name out of name given */
13885 if ((ptr = strchr((char *)arg, '@')) == NULL) {
13886 ret = NDI_FAILURE;
13887 break;
13889 ptr++;
13890 if (strncmp((char *)arg, "smp", 3) == 0) {
13892 * This is a SMP target device
13894 ret = mptsas_parse_smp_name(ptr, &wwid);
13895 if (ret != DDI_SUCCESS) {
13896 ret = NDI_FAILURE;
13897 break;
13899 ret = mptsas_config_smp(pdip, wwid, childp);
13900 } else if ((ptr[0] == 'w') || (ptr[0] == 'p')) {
13902 * OBP could pass down a non-canonical form
13903 * bootpath without LUN part when LUN is 0.
13904 * So driver need adjust the string.
13906 if (strchr(ptr, ',') == NULL) {
13907 devnm = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
13908 (void) sprintf(devnm, "%s,0", (char *)arg);
13909 ptr = strchr(devnm, '@');
13910 ptr++;
13914 * The device path is wWWID format and the device
13915 * is not SMP target device.
13917 ret = mptsas_parse_address(ptr, &wwid, &phy, &lun);
13918 if (ret != DDI_SUCCESS) {
13919 ret = NDI_FAILURE;
13920 break;
13922 *childp = NULL;
13923 if (ptr[0] == 'w') {
13924 ret = mptsas_config_one_addr(pdip, wwid,
13925 lun, childp);
13926 } else if (ptr[0] == 'p') {
13927 ret = mptsas_config_one_phy(pdip, phy, lun,
13928 childp);
13932 * If this is CD/DVD device in OBP path, the
13933 * ndi_busop_bus_config can be skipped as config one
13934 * operation is done above.
13936 if ((ret == NDI_SUCCESS) && (*childp != NULL) &&
13937 (strcmp(ddi_node_name(*childp), "cdrom") == 0) &&
13938 (strncmp((char *)arg, "disk", 4) == 0)) {
13939 bconfig = FALSE;
13940 ndi_hold_devi(*childp);
13942 } else {
13943 ret = NDI_FAILURE;
13944 break;
13948 * DDI group instructed us to use this flag.
13950 mflags |= NDI_MDI_FALLBACK;
13951 break;
13952 case BUS_CONFIG_DRIVER:
13953 case BUS_CONFIG_ALL:
13954 mptsas_config_all(pdip);
13955 ret = NDI_SUCCESS;
13956 break;
13959 if ((ret == NDI_SUCCESS) && bconfig) {
13960 ret = ndi_busop_bus_config(pdip, mflags, op,
13961 (devnm == NULL) ? arg : devnm, childp, 0);
13964 ndi_devi_exit(pdip, circ1);
13965 ndi_devi_exit(scsi_vhci_dip, circ);
13966 if (devnm != NULL)
13967 kmem_free(devnm, SCSI_MAXNAMELEN);
13968 return (ret);
13971 static int
13972 mptsas_probe_lun(dev_info_t *pdip, int lun, dev_info_t **dip,
13973 mptsas_target_t *ptgt)
13975 int rval = DDI_FAILURE;
13976 struct scsi_inquiry *sd_inq = NULL;
13977 mptsas_t *mpt = DIP2MPT(pdip);
13979 sd_inq = kmem_alloc(SUN_INQSIZE, KM_SLEEP);
13981 rval = mptsas_inquiry(mpt, ptgt, lun, 0, (uchar_t *)sd_inq,
13982 SUN_INQSIZE, 0, (uchar_t)0);
13984 if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
13985 rval = mptsas_create_lun(pdip, sd_inq, dip, ptgt, lun);
13986 } else {
13987 rval = DDI_FAILURE;
13990 kmem_free(sd_inq, SUN_INQSIZE);
13991 return (rval);
13994 static int
13995 mptsas_config_one_addr(dev_info_t *pdip, uint64_t sasaddr, int lun,
13996 dev_info_t **lundip)
13998 int rval;
13999 mptsas_t *mpt = DIP2MPT(pdip);
14000 int phymask;
14001 mptsas_target_t *ptgt = NULL;
14004 * Get the physical port associated to the iport
14006 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14007 "phymask", 0);
14009 ptgt = mptsas_wwid_to_ptgt(mpt, phymask, sasaddr);
14010 if (ptgt == NULL) {
14012 * didn't match any device by searching
14014 return (DDI_FAILURE);
14017 * If the LUN already exists and the status is online,
14018 * we just return the pointer to dev_info_t directly.
14019 * For the mdi_pathinfo node, we'll handle it in
14020 * mptsas_create_virt_lun()
14021 * TODO should be also in mptsas_handle_dr
14024 *lundip = mptsas_find_child_addr(pdip, sasaddr, lun);
14025 if (*lundip != NULL) {
14027 * TODO Another senario is, we hotplug the same disk
14028 * on the same slot, the devhdl changed, is this
14029 * possible?
14030 * tgt_private->t_private != ptgt
14032 if (sasaddr != ptgt->m_addr.mta_wwn) {
14034 * The device has changed although the devhdl is the
14035 * same (Enclosure mapping mode, change drive on the
14036 * same slot)
14038 return (DDI_FAILURE);
14040 return (DDI_SUCCESS);
14043 if (phymask == 0) {
14045 * Configure IR volume
14047 rval = mptsas_config_raid(pdip, ptgt->m_devhdl, lundip);
14048 return (rval);
14050 rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
14052 return (rval);
14055 static int
14056 mptsas_config_one_phy(dev_info_t *pdip, uint8_t phy, int lun,
14057 dev_info_t **lundip)
14059 int rval;
14060 mptsas_t *mpt = DIP2MPT(pdip);
14061 mptsas_phymask_t phymask;
14062 mptsas_target_t *ptgt = NULL;
14065 * Get the physical port associated to the iport
14067 phymask = (mptsas_phymask_t)ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14068 "phymask", 0);
14070 ptgt = mptsas_phy_to_tgt(mpt, phymask, phy);
14071 if (ptgt == NULL) {
14073 * didn't match any device by searching
14075 return (DDI_FAILURE);
14079 * If the LUN already exists and the status is online,
14080 * we just return the pointer to dev_info_t directly.
14081 * For the mdi_pathinfo node, we'll handle it in
14082 * mptsas_create_virt_lun().
14085 *lundip = mptsas_find_child_phy(pdip, phy);
14086 if (*lundip != NULL) {
14087 return (DDI_SUCCESS);
14090 rval = mptsas_probe_lun(pdip, lun, lundip, ptgt);
14092 return (rval);
14095 static int
14096 mptsas_retrieve_lundata(int lun_cnt, uint8_t *buf, uint16_t *lun_num,
14097 uint8_t *lun_addr_type)
14099 uint32_t lun_idx = 0;
14101 ASSERT(lun_num != NULL);
14102 ASSERT(lun_addr_type != NULL);
14104 lun_idx = (lun_cnt + 1) * MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
14105 /* determine report luns addressing type */
14106 switch (buf[lun_idx] & MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) {
14108 * Vendors in the field have been found to be concatenating
14109 * bus/target/lun to equal the complete lun value instead
14110 * of switching to flat space addressing
14112 /* 00b - peripheral device addressing method */
14113 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_PERIPHERAL:
14114 /* FALLTHRU */
14115 /* 10b - logical unit addressing method */
14116 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_LOGICAL_UNIT:
14117 /* FALLTHRU */
14118 /* 01b - flat space addressing method */
14119 case MPTSAS_SCSI_REPORTLUNS_ADDRESS_FLAT_SPACE:
14120 /* byte0 bit0-5=msb lun byte1 bit0-7=lsb lun */
14121 *lun_addr_type = (buf[lun_idx] &
14122 MPTSAS_SCSI_REPORTLUNS_ADDRESS_MASK) >> 6;
14123 *lun_num = (buf[lun_idx] & 0x3F) << 8;
14124 *lun_num |= buf[lun_idx + 1];
14125 return (DDI_SUCCESS);
14126 default:
14127 return (DDI_FAILURE);
14131 static int
14132 mptsas_config_luns(dev_info_t *pdip, mptsas_target_t *ptgt)
14134 struct buf *repluns_bp = NULL;
14135 struct scsi_address ap;
14136 uchar_t cdb[CDB_GROUP5];
14137 int ret = DDI_FAILURE;
14138 int retry = 0;
14139 int lun_list_len = 0;
14140 uint16_t lun_num = 0;
14141 uint8_t lun_addr_type = 0;
14142 uint32_t lun_cnt = 0;
14143 uint32_t lun_total = 0;
14144 dev_info_t *cdip = NULL;
14145 uint16_t *saved_repluns = NULL;
14146 char *buffer = NULL;
14147 int buf_len = 128;
14148 mptsas_t *mpt = DIP2MPT(pdip);
14149 uint64_t sas_wwn = 0;
14150 uint8_t phy = 0xFF;
14151 uint32_t dev_info = 0;
14153 mutex_enter(&mpt->m_mutex);
14154 sas_wwn = ptgt->m_addr.mta_wwn;
14155 phy = ptgt->m_phynum;
14156 dev_info = ptgt->m_deviceinfo;
14157 mutex_exit(&mpt->m_mutex);
14159 if (sas_wwn == 0) {
14161 * It's a SATA without Device Name
14162 * So don't try multi-LUNs
14164 if (mptsas_find_child_phy(pdip, phy)) {
14165 return (DDI_SUCCESS);
14166 } else {
14168 * need configure and create node
14170 return (DDI_FAILURE);
14175 * WWN (SAS address or Device Name exist)
14177 if (dev_info & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
14178 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
14180 * SATA device with Device Name
14181 * So don't try multi-LUNs
14183 if (mptsas_find_child_addr(pdip, sas_wwn, 0)) {
14184 return (DDI_SUCCESS);
14185 } else {
14186 return (DDI_FAILURE);
14190 do {
14191 ap.a_target = MPTSAS_INVALID_DEVHDL;
14192 ap.a_lun = 0;
14193 ap.a_hba_tran = mpt->m_tran;
14194 repluns_bp = scsi_alloc_consistent_buf(&ap,
14195 NULL, buf_len, B_READ, NULL_FUNC, NULL);
14196 if (repluns_bp == NULL) {
14197 retry++;
14198 continue;
14200 bzero(cdb, CDB_GROUP5);
14201 cdb[0] = SCMD_REPORT_LUNS;
14202 cdb[6] = (buf_len & 0xff000000) >> 24;
14203 cdb[7] = (buf_len & 0x00ff0000) >> 16;
14204 cdb[8] = (buf_len & 0x0000ff00) >> 8;
14205 cdb[9] = (buf_len & 0x000000ff);
14207 ret = mptsas_send_scsi_cmd(mpt, &ap, ptgt, &cdb[0], CDB_GROUP5,
14208 repluns_bp, NULL);
14209 if (ret != DDI_SUCCESS) {
14210 scsi_free_consistent_buf(repluns_bp);
14211 retry++;
14212 continue;
14214 lun_list_len = BE_32(*(int *)((void *)(
14215 repluns_bp->b_un.b_addr)));
14216 if (buf_len >= lun_list_len + 8) {
14217 ret = DDI_SUCCESS;
14218 break;
14220 scsi_free_consistent_buf(repluns_bp);
14221 buf_len = lun_list_len + 8;
14223 } while (retry < 3);
14225 if (ret != DDI_SUCCESS)
14226 return (ret);
14227 buffer = (char *)repluns_bp->b_un.b_addr;
14229 * find out the number of luns returned by the SCSI ReportLun call
14230 * and allocate buffer space
14232 lun_total = lun_list_len / MPTSAS_SCSI_REPORTLUNS_ADDRESS_SIZE;
14233 saved_repluns = kmem_zalloc(sizeof (uint16_t) * lun_total, KM_SLEEP);
14234 if (saved_repluns == NULL) {
14235 scsi_free_consistent_buf(repluns_bp);
14236 return (DDI_FAILURE);
14238 for (lun_cnt = 0; lun_cnt < lun_total; lun_cnt++) {
14239 if (mptsas_retrieve_lundata(lun_cnt, (uint8_t *)(buffer),
14240 &lun_num, &lun_addr_type) != DDI_SUCCESS) {
14241 continue;
14243 saved_repluns[lun_cnt] = lun_num;
14244 if (cdip = mptsas_find_child_addr(pdip, sas_wwn, lun_num))
14245 ret = DDI_SUCCESS;
14246 else
14247 ret = mptsas_probe_lun(pdip, lun_num, &cdip,
14248 ptgt);
14249 if ((ret == DDI_SUCCESS) && (cdip != NULL)) {
14250 (void) ndi_prop_remove(DDI_DEV_T_NONE, cdip,
14251 MPTSAS_DEV_GONE);
14254 mptsas_offline_missed_luns(pdip, saved_repluns, lun_total, ptgt);
14255 kmem_free(saved_repluns, sizeof (uint16_t) * lun_total);
14256 scsi_free_consistent_buf(repluns_bp);
14257 return (DDI_SUCCESS);
14260 static int
14261 mptsas_config_raid(dev_info_t *pdip, uint16_t target, dev_info_t **dip)
14263 int rval = DDI_FAILURE;
14264 struct scsi_inquiry *sd_inq = NULL;
14265 mptsas_t *mpt = DIP2MPT(pdip);
14266 mptsas_target_t *ptgt = NULL;
14268 mutex_enter(&mpt->m_mutex);
14269 ptgt = refhash_linear_search(mpt->m_targets,
14270 mptsas_target_eval_devhdl, &target);
14271 mutex_exit(&mpt->m_mutex);
14272 if (ptgt == NULL) {
14273 mptsas_log(mpt, CE_WARN, "Volume with VolDevHandle of 0x%x "
14274 "not found.", target);
14275 return (rval);
14278 sd_inq = kmem_alloc(SUN_INQSIZE, KM_SLEEP);
14279 rval = mptsas_inquiry(mpt, ptgt, 0, 0, (uchar_t *)sd_inq,
14280 SUN_INQSIZE, 0, (uchar_t)0);
14282 if ((rval == DDI_SUCCESS) && MPTSAS_VALID_LUN(sd_inq)) {
14283 rval = mptsas_create_phys_lun(pdip, sd_inq, NULL, dip, ptgt,
14285 } else {
14286 rval = DDI_FAILURE;
14289 kmem_free(sd_inq, SUN_INQSIZE);
14290 return (rval);
14294 * configure all RAID volumes for virtual iport
14296 static void
14297 mptsas_config_all_viport(dev_info_t *pdip)
14299 mptsas_t *mpt = DIP2MPT(pdip);
14300 int config, vol;
14301 int target;
14302 dev_info_t *lundip = NULL;
14305 * Get latest RAID info and search for any Volume DevHandles. If any
14306 * are found, configure the volume.
14308 mutex_enter(&mpt->m_mutex);
14309 for (config = 0; config < mpt->m_num_raid_configs; config++) {
14310 for (vol = 0; vol < MPTSAS_MAX_RAIDVOLS; vol++) {
14311 if (mpt->m_raidconfig[config].m_raidvol[vol].m_israid
14312 == 1) {
14313 target = mpt->m_raidconfig[config].
14314 m_raidvol[vol].m_raidhandle;
14315 mutex_exit(&mpt->m_mutex);
14316 (void) mptsas_config_raid(pdip, target,
14317 &lundip);
14318 mutex_enter(&mpt->m_mutex);
14322 mutex_exit(&mpt->m_mutex);
14325 static void
14326 mptsas_offline_missed_luns(dev_info_t *pdip, uint16_t *repluns,
14327 int lun_cnt, mptsas_target_t *ptgt)
14329 dev_info_t *child = NULL, *savechild = NULL;
14330 mdi_pathinfo_t *pip = NULL, *savepip = NULL;
14331 uint64_t sas_wwn, wwid;
14332 uint8_t phy;
14333 int lun;
14334 int i;
14335 int find;
14336 char *addr;
14337 char *nodename;
14338 mptsas_t *mpt = DIP2MPT(pdip);
14340 mutex_enter(&mpt->m_mutex);
14341 wwid = ptgt->m_addr.mta_wwn;
14342 mutex_exit(&mpt->m_mutex);
14344 child = ddi_get_child(pdip);
14345 while (child) {
14346 find = 0;
14347 savechild = child;
14348 child = ddi_get_next_sibling(child);
14350 nodename = ddi_node_name(savechild);
14351 if (strcmp(nodename, "smp") == 0) {
14352 continue;
14355 addr = ddi_get_name_addr(savechild);
14356 if (addr == NULL) {
14357 continue;
14360 if (mptsas_parse_address(addr, &sas_wwn, &phy, &lun) !=
14361 DDI_SUCCESS) {
14362 continue;
14365 if (wwid == sas_wwn) {
14366 for (i = 0; i < lun_cnt; i++) {
14367 if (repluns[i] == lun) {
14368 find = 1;
14369 break;
14372 } else {
14373 continue;
14375 if (find == 0) {
14377 * The lun has not been there already
14379 (void) mptsas_offline_lun(pdip, savechild, NULL,
14380 NDI_DEVI_REMOVE);
14384 pip = mdi_get_next_client_path(pdip, NULL);
14385 while (pip) {
14386 find = 0;
14387 savepip = pip;
14388 addr = MDI_PI(pip)->pi_addr;
14390 pip = mdi_get_next_client_path(pdip, pip);
14392 if (addr == NULL) {
14393 continue;
14396 if (mptsas_parse_address(addr, &sas_wwn, &phy,
14397 &lun) != DDI_SUCCESS) {
14398 continue;
14401 if (sas_wwn == wwid) {
14402 for (i = 0; i < lun_cnt; i++) {
14403 if (repluns[i] == lun) {
14404 find = 1;
14405 break;
14408 } else {
14409 continue;
14412 if (find == 0) {
14414 * The lun has not been there already
14416 (void) mptsas_offline_lun(pdip, NULL, savepip,
14417 NDI_DEVI_REMOVE);
14422 void
14423 mptsas_update_hashtab(struct mptsas *mpt)
14425 uint32_t page_address;
14426 int rval = 0;
14427 uint16_t dev_handle;
14428 mptsas_target_t *ptgt = NULL;
14429 mptsas_smp_t smp_node;
14432 * Get latest RAID info.
14434 (void) mptsas_get_raid_info(mpt);
14436 dev_handle = mpt->m_smp_devhdl;
14437 for (; mpt->m_done_traverse_smp == 0; ) {
14438 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
14439 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)dev_handle;
14440 if (mptsas_get_sas_expander_page0(mpt, page_address, &smp_node)
14441 != DDI_SUCCESS) {
14442 break;
14444 mpt->m_smp_devhdl = dev_handle = smp_node.m_devhdl;
14445 (void) mptsas_smp_alloc(mpt, &smp_node);
14449 * Config target devices
14451 dev_handle = mpt->m_dev_handle;
14454 * Do loop to get sas device page 0 by GetNextHandle till the
14455 * the last handle. If the sas device is a SATA/SSP target,
14456 * we try to config it.
14458 for (; mpt->m_done_traverse_dev == 0; ) {
14459 ptgt = NULL;
14460 page_address =
14461 (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
14462 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
14463 (uint32_t)dev_handle;
14464 rval = mptsas_get_target_device_info(mpt, page_address,
14465 &dev_handle, &ptgt);
14466 if ((rval == DEV_INFO_FAIL_PAGE0) ||
14467 (rval == DEV_INFO_FAIL_ALLOC) ||
14468 (rval == DEV_INFO_FAIL_GUID)) {
14469 break;
14472 mpt->m_dev_handle = dev_handle;
14477 void
14478 mptsas_update_driver_data(struct mptsas *mpt)
14480 mptsas_target_t *tp;
14481 mptsas_smp_t *sp;
14483 ASSERT(MUTEX_HELD(&mpt->m_mutex));
14486 * TODO after hard reset, update the driver data structures
14487 * 1. update port/phymask mapping table mpt->m_phy_info
14488 * 2. invalid all the entries in hash table
14489 * m_devhdl = 0xffff and m_deviceinfo = 0
14490 * 3. call sas_device_page/expander_page to update hash table
14492 mptsas_update_phymask(mpt);
14495 * Remove all the devhdls for existing entries but leave their
14496 * addresses alone. In update_hashtab() below, we'll find all
14497 * targets that are still present and reassociate them with
14498 * their potentially new devhdls. Leaving the targets around in
14499 * this fashion allows them to be used on the tx waitq even
14500 * while IOC reset is occurring.
14502 for (tp = refhash_first(mpt->m_targets); tp != NULL;
14503 tp = refhash_next(mpt->m_targets, tp)) {
14504 tp->m_devhdl = MPTSAS_INVALID_DEVHDL;
14505 tp->m_deviceinfo = 0;
14506 tp->m_dr_flag = MPTSAS_DR_INACTIVE;
14508 for (sp = refhash_first(mpt->m_smp_targets); sp != NULL;
14509 sp = refhash_next(mpt->m_smp_targets, sp)) {
14510 sp->m_devhdl = MPTSAS_INVALID_DEVHDL;
14511 sp->m_deviceinfo = 0;
14513 mpt->m_done_traverse_dev = 0;
14514 mpt->m_done_traverse_smp = 0;
14515 mpt->m_dev_handle = mpt->m_smp_devhdl = MPTSAS_INVALID_DEVHDL;
14516 mptsas_update_hashtab(mpt);
14519 static void
14520 mptsas_config_all(dev_info_t *pdip)
14522 dev_info_t *smpdip = NULL;
14523 mptsas_t *mpt = DIP2MPT(pdip);
14524 int phymask = 0;
14525 mptsas_phymask_t phy_mask;
14526 mptsas_target_t *ptgt = NULL;
14527 mptsas_smp_t *psmp;
14530 * Get the phymask associated to the iport
14532 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
14533 "phymask", 0);
14536 * Enumerate RAID volumes here (phymask == 0).
14538 if (phymask == 0) {
14539 mptsas_config_all_viport(pdip);
14540 return;
14543 mutex_enter(&mpt->m_mutex);
14545 if (!mpt->m_done_traverse_dev || !mpt->m_done_traverse_smp) {
14546 mptsas_update_hashtab(mpt);
14549 for (psmp = refhash_first(mpt->m_smp_targets); psmp != NULL;
14550 psmp = refhash_next(mpt->m_smp_targets, psmp)) {
14551 phy_mask = psmp->m_addr.mta_phymask;
14552 if (phy_mask == phymask) {
14553 smpdip = NULL;
14554 mutex_exit(&mpt->m_mutex);
14555 (void) mptsas_online_smp(pdip, psmp, &smpdip);
14556 mutex_enter(&mpt->m_mutex);
14560 for (ptgt = refhash_first(mpt->m_targets); ptgt != NULL;
14561 ptgt = refhash_next(mpt->m_targets, ptgt)) {
14562 phy_mask = ptgt->m_addr.mta_phymask;
14563 if (phy_mask == phymask) {
14564 mutex_exit(&mpt->m_mutex);
14565 (void) mptsas_config_target(pdip, ptgt);
14566 mutex_enter(&mpt->m_mutex);
14569 mutex_exit(&mpt->m_mutex);
14572 static int
14573 mptsas_config_target(dev_info_t *pdip, mptsas_target_t *ptgt)
14575 int rval = DDI_FAILURE;
14576 dev_info_t *tdip;
14578 rval = mptsas_config_luns(pdip, ptgt);
14579 if (rval != DDI_SUCCESS) {
14581 * The return value means the SCMD_REPORT_LUNS
14582 * did not execute successfully. The target maybe
14583 * doesn't support such command.
14585 rval = mptsas_probe_lun(pdip, 0, &tdip, ptgt);
14587 return (rval);
14591 * Return fail if not all the childs/paths are freed.
14592 * if there is any path under the HBA, the return value will be always fail
14593 * because we didn't call mdi_pi_free for path
14595 static int
14596 mptsas_offline_target(dev_info_t *pdip, char *name)
14598 dev_info_t *child = NULL, *prechild = NULL;
14599 mdi_pathinfo_t *pip = NULL, *savepip = NULL;
14600 int tmp_rval, rval = DDI_SUCCESS;
14601 char *addr, *cp;
14602 size_t s;
14603 mptsas_t *mpt = DIP2MPT(pdip);
14605 child = ddi_get_child(pdip);
14606 while (child) {
14607 addr = ddi_get_name_addr(child);
14608 prechild = child;
14609 child = ddi_get_next_sibling(child);
14611 if (addr == NULL) {
14612 continue;
14614 if ((cp = strchr(addr, ',')) == NULL) {
14615 continue;
14618 s = (uintptr_t)cp - (uintptr_t)addr;
14620 if (strncmp(addr, name, s) != 0) {
14621 continue;
14624 tmp_rval = mptsas_offline_lun(pdip, prechild, NULL,
14625 NDI_DEVI_REMOVE);
14626 if (tmp_rval != DDI_SUCCESS) {
14627 rval = DDI_FAILURE;
14628 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
14629 prechild, MPTSAS_DEV_GONE) !=
14630 DDI_PROP_SUCCESS) {
14631 mptsas_log(mpt, CE_WARN, "mptsas driver "
14632 "unable to create property for "
14633 "SAS %s (MPTSAS_DEV_GONE)", addr);
14638 pip = mdi_get_next_client_path(pdip, NULL);
14639 while (pip) {
14640 addr = MDI_PI(pip)->pi_addr;
14641 savepip = pip;
14642 pip = mdi_get_next_client_path(pdip, pip);
14643 if (addr == NULL) {
14644 continue;
14647 if ((cp = strchr(addr, ',')) == NULL) {
14648 continue;
14651 s = (uintptr_t)cp - (uintptr_t)addr;
14653 if (strncmp(addr, name, s) != 0) {
14654 continue;
14657 (void) mptsas_offline_lun(pdip, NULL, savepip,
14658 NDI_DEVI_REMOVE);
14660 * driver will not invoke mdi_pi_free, so path will not
14661 * be freed forever, return DDI_FAILURE.
14663 rval = DDI_FAILURE;
14665 return (rval);
14668 static int
14669 mptsas_offline_lun(dev_info_t *pdip, dev_info_t *rdip,
14670 mdi_pathinfo_t *rpip, uint_t flags)
14672 int rval = DDI_FAILURE;
14673 char *devname;
14674 dev_info_t *cdip, *parent;
14676 if (rpip != NULL) {
14677 parent = scsi_vhci_dip;
14678 cdip = mdi_pi_get_client(rpip);
14679 } else if (rdip != NULL) {
14680 parent = pdip;
14681 cdip = rdip;
14682 } else {
14683 return (DDI_FAILURE);
14687 * Make sure node is attached otherwise
14688 * it won't have related cache nodes to
14689 * clean up. i_ddi_devi_attached is
14690 * similiar to i_ddi_node_state(cdip) >=
14691 * DS_ATTACHED.
14693 if (i_ddi_devi_attached(cdip)) {
14695 /* Get full devname */
14696 devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14697 (void) ddi_deviname(cdip, devname);
14698 /* Clean cache */
14699 (void) devfs_clean(parent, devname + 1,
14700 DV_CLEAN_FORCE);
14701 kmem_free(devname, MAXNAMELEN + 1);
14703 if (rpip != NULL) {
14704 if (MDI_PI_IS_OFFLINE(rpip)) {
14705 rval = DDI_SUCCESS;
14706 } else {
14707 rval = mdi_pi_offline(rpip, 0);
14709 } else {
14710 rval = ndi_devi_offline(cdip, flags);
14713 return (rval);
14716 static dev_info_t *
14717 mptsas_find_smp_child(dev_info_t *parent, char *str_wwn)
14719 dev_info_t *child = NULL;
14720 char *smp_wwn = NULL;
14722 child = ddi_get_child(parent);
14723 while (child) {
14724 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
14725 DDI_PROP_DONTPASS, SMP_WWN, &smp_wwn)
14726 != DDI_SUCCESS) {
14727 child = ddi_get_next_sibling(child);
14728 continue;
14731 if (strcmp(smp_wwn, str_wwn) == 0) {
14732 ddi_prop_free(smp_wwn);
14733 break;
14735 child = ddi_get_next_sibling(child);
14736 ddi_prop_free(smp_wwn);
14738 return (child);
14741 static int
14742 mptsas_offline_smp(dev_info_t *pdip, mptsas_smp_t *smp_node, uint_t flags)
14744 int rval = DDI_FAILURE;
14745 char *devname;
14746 char wwn_str[MPTSAS_WWN_STRLEN];
14747 dev_info_t *cdip;
14749 (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_addr.mta_wwn);
14751 cdip = mptsas_find_smp_child(pdip, wwn_str);
14753 if (cdip == NULL)
14754 return (DDI_SUCCESS);
14757 * Make sure node is attached otherwise
14758 * it won't have related cache nodes to
14759 * clean up. i_ddi_devi_attached is
14760 * similiar to i_ddi_node_state(cdip) >=
14761 * DS_ATTACHED.
14763 if (i_ddi_devi_attached(cdip)) {
14765 /* Get full devname */
14766 devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
14767 (void) ddi_deviname(cdip, devname);
14768 /* Clean cache */
14769 (void) devfs_clean(pdip, devname + 1,
14770 DV_CLEAN_FORCE);
14771 kmem_free(devname, MAXNAMELEN + 1);
14774 rval = ndi_devi_offline(cdip, flags);
14776 return (rval);
14779 static dev_info_t *
14780 mptsas_find_child(dev_info_t *pdip, char *name)
14782 dev_info_t *child = NULL;
14783 char *rname = NULL;
14784 int rval = DDI_FAILURE;
14786 rname = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14788 child = ddi_get_child(pdip);
14789 while (child) {
14790 rval = mptsas_name_child(child, rname, SCSI_MAXNAMELEN);
14791 if (rval != DDI_SUCCESS) {
14792 child = ddi_get_next_sibling(child);
14793 bzero(rname, SCSI_MAXNAMELEN);
14794 continue;
14797 if (strcmp(rname, name) == 0) {
14798 break;
14800 child = ddi_get_next_sibling(child);
14801 bzero(rname, SCSI_MAXNAMELEN);
14804 kmem_free(rname, SCSI_MAXNAMELEN);
14806 return (child);
14810 static dev_info_t *
14811 mptsas_find_child_addr(dev_info_t *pdip, uint64_t sasaddr, int lun)
14813 dev_info_t *child = NULL;
14814 char *name = NULL;
14815 char *addr = NULL;
14817 name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14818 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14819 (void) sprintf(name, "%016"PRIx64, sasaddr);
14820 (void) sprintf(addr, "w%s,%x", name, lun);
14821 child = mptsas_find_child(pdip, addr);
14822 kmem_free(name, SCSI_MAXNAMELEN);
14823 kmem_free(addr, SCSI_MAXNAMELEN);
14824 return (child);
14827 static dev_info_t *
14828 mptsas_find_child_phy(dev_info_t *pdip, uint8_t phy)
14830 dev_info_t *child;
14831 char *addr;
14833 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14834 (void) sprintf(addr, "p%x,0", phy);
14835 child = mptsas_find_child(pdip, addr);
14836 kmem_free(addr, SCSI_MAXNAMELEN);
14837 return (child);
14840 static mdi_pathinfo_t *
14841 mptsas_find_path_phy(dev_info_t *pdip, uint8_t phy)
14843 mdi_pathinfo_t *path;
14844 char *addr = NULL;
14846 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14847 (void) sprintf(addr, "p%x,0", phy);
14848 path = mdi_pi_find(pdip, NULL, addr);
14849 kmem_free(addr, SCSI_MAXNAMELEN);
14850 return (path);
14853 static mdi_pathinfo_t *
14854 mptsas_find_path_addr(dev_info_t *parent, uint64_t sasaddr, int lun)
14856 mdi_pathinfo_t *path;
14857 char *name = NULL;
14858 char *addr = NULL;
14860 name = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14861 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
14862 (void) sprintf(name, "%016"PRIx64, sasaddr);
14863 (void) sprintf(addr, "w%s,%x", name, lun);
14864 path = mdi_pi_find(parent, NULL, addr);
14865 kmem_free(name, SCSI_MAXNAMELEN);
14866 kmem_free(addr, SCSI_MAXNAMELEN);
14868 return (path);
14871 static int
14872 mptsas_create_lun(dev_info_t *pdip, struct scsi_inquiry *sd_inq,
14873 dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
14875 int i = 0;
14876 uchar_t *inq83 = NULL;
14877 int inq83_len1 = 0xFF;
14878 int inq83_len = 0;
14879 int rval = DDI_FAILURE;
14880 ddi_devid_t devid;
14881 char *guid = NULL;
14882 int target = ptgt->m_devhdl;
14883 mdi_pathinfo_t *pip = NULL;
14884 mptsas_t *mpt = DIP2MPT(pdip);
14887 * For DVD/CD ROM and tape devices and optical
14888 * devices, we won't try to enumerate them under
14889 * scsi_vhci, so no need to try page83
14891 if (sd_inq && (sd_inq->inq_dtype == DTYPE_RODIRECT ||
14892 sd_inq->inq_dtype == DTYPE_OPTICAL ||
14893 sd_inq->inq_dtype == DTYPE_ESI))
14894 goto create_lun;
14897 * The LCA returns good SCSI status, but corrupt page 83 data the first
14898 * time it is queried. The solution is to keep trying to request page83
14899 * and verify the GUID is not (DDI_NOT_WELL_FORMED) in
14900 * mptsas_inq83_retry_timeout seconds. If the timeout expires, driver
14901 * give up to get VPD page at this stage and fail the enumeration.
14904 inq83 = kmem_zalloc(inq83_len1, KM_SLEEP);
14906 for (i = 0; i < mptsas_inq83_retry_timeout; i++) {
14907 rval = mptsas_inquiry(mpt, ptgt, lun, 0x83, inq83,
14908 inq83_len1, &inq83_len, 1);
14909 if (rval != 0) {
14910 mptsas_log(mpt, CE_WARN, "!mptsas request inquiry page "
14911 "0x83 for target:%x, lun:%x failed!", target, lun);
14912 if (mptsas_physical_bind_failed_page_83 != B_FALSE)
14913 goto create_lun;
14914 goto out;
14917 * create DEVID from inquiry data
14919 if ((rval = ddi_devid_scsi_encode(
14920 DEVID_SCSI_ENCODE_VERSION_LATEST, NULL, (uchar_t *)sd_inq,
14921 sizeof (struct scsi_inquiry), NULL, 0, inq83,
14922 (size_t)inq83_len, &devid)) == DDI_SUCCESS) {
14924 * extract GUID from DEVID
14926 guid = ddi_devid_to_guid(devid);
14929 * Do not enable MPXIO if the strlen(guid) is greater
14930 * than MPTSAS_MAX_GUID_LEN, this constrain would be
14931 * handled by framework later.
14933 if (guid && (strlen(guid) > MPTSAS_MAX_GUID_LEN)) {
14934 ddi_devid_free_guid(guid);
14935 guid = NULL;
14936 if (mpt->m_mpxio_enable == TRUE) {
14937 mptsas_log(mpt, CE_NOTE, "!Target:%x, "
14938 "lun:%x doesn't have a valid GUID, "
14939 "multipathing for this drive is "
14940 "not enabled", target, lun);
14945 * devid no longer needed
14947 ddi_devid_free(devid);
14948 break;
14949 } else if (rval == DDI_NOT_WELL_FORMED) {
14951 * return value of ddi_devid_scsi_encode equal to
14952 * DDI_NOT_WELL_FORMED means DEVID_RETRY, it worth
14953 * to retry inquiry page 0x83 and get GUID.
14955 NDBG20(("Not well formed devid, retry..."));
14956 ddi_sleep(1);
14957 continue;
14958 } else {
14959 mptsas_log(mpt, CE_WARN, "!Encode devid failed for "
14960 "path target:%x, lun:%x", target, lun);
14961 rval = DDI_FAILURE;
14962 goto create_lun;
14966 if (i == mptsas_inq83_retry_timeout) {
14967 mptsas_log(mpt, CE_WARN, "!Repeated page83 requests timeout "
14968 "for path target:%x, lun:%x", target, lun);
14971 rval = DDI_FAILURE;
14973 create_lun:
14974 if ((guid != NULL) && (mpt->m_mpxio_enable == TRUE)) {
14975 rval = mptsas_create_virt_lun(pdip, sd_inq, guid, lun_dip, &pip,
14976 ptgt, lun);
14978 if (rval != DDI_SUCCESS) {
14979 rval = mptsas_create_phys_lun(pdip, sd_inq, guid, lun_dip,
14980 ptgt, lun);
14983 out:
14984 if (guid != NULL) {
14986 * guid no longer needed
14988 ddi_devid_free_guid(guid);
14990 if (inq83 != NULL)
14991 kmem_free(inq83, inq83_len1);
14992 return (rval);
14995 static int
14996 mptsas_create_virt_lun(dev_info_t *pdip, struct scsi_inquiry *inq, char *guid,
14997 dev_info_t **lun_dip, mdi_pathinfo_t **pip, mptsas_target_t *ptgt, int lun)
14999 int target;
15000 char *nodename = NULL;
15001 char **compatible = NULL;
15002 int ncompatible = 0;
15003 int mdi_rtn = MDI_FAILURE;
15004 int rval = DDI_FAILURE;
15005 char *old_guid = NULL;
15006 mptsas_t *mpt = DIP2MPT(pdip);
15007 char *lun_addr = NULL;
15008 char *wwn_str = NULL;
15009 char *attached_wwn_str = NULL;
15010 char *component = NULL;
15011 uint8_t phy = 0xFF;
15012 uint64_t sas_wwn;
15013 int64_t lun64 = 0;
15014 uint32_t devinfo;
15015 uint16_t dev_hdl;
15016 uint16_t pdev_hdl;
15017 uint64_t dev_sas_wwn;
15018 uint64_t pdev_sas_wwn;
15019 uint32_t pdev_info;
15020 uint8_t physport;
15021 uint8_t phy_id;
15022 uint32_t page_address;
15023 uint16_t bay_num, enclosure, io_flags;
15024 char pdev_wwn_str[MPTSAS_WWN_STRLEN];
15025 uint32_t dev_info;
15027 mutex_enter(&mpt->m_mutex);
15028 target = ptgt->m_devhdl;
15029 sas_wwn = ptgt->m_addr.mta_wwn;
15030 devinfo = ptgt->m_deviceinfo;
15031 phy = ptgt->m_phynum;
15032 mutex_exit(&mpt->m_mutex);
15034 if (sas_wwn) {
15035 *pip = mptsas_find_path_addr(pdip, sas_wwn, lun);
15036 } else {
15037 *pip = mptsas_find_path_phy(pdip, phy);
15040 if (*pip != NULL) {
15041 *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
15042 ASSERT(*lun_dip != NULL);
15043 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, *lun_dip,
15044 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
15045 MDI_CLIENT_GUID_PROP, &old_guid) == DDI_SUCCESS) {
15046 if (strncmp(guid, old_guid, strlen(guid)) == 0) {
15048 * Same path back online again.
15050 (void) ddi_prop_free(old_guid);
15051 if ((!MDI_PI_IS_ONLINE(*pip)) &&
15052 (!MDI_PI_IS_STANDBY(*pip)) &&
15053 (ptgt->m_tgt_unconfigured == 0)) {
15054 rval = mdi_pi_online(*pip, 0);
15055 mutex_enter(&mpt->m_mutex);
15056 ptgt->m_led_status = 0;
15057 (void) mptsas_flush_led_status(mpt,
15058 ptgt);
15059 mutex_exit(&mpt->m_mutex);
15060 } else {
15061 rval = DDI_SUCCESS;
15063 if (rval != DDI_SUCCESS) {
15064 mptsas_log(mpt, CE_WARN, "path:target: "
15065 "%x, lun:%x online failed!", target,
15066 lun);
15067 *pip = NULL;
15068 *lun_dip = NULL;
15070 return (rval);
15071 } else {
15073 * The GUID of the LUN has changed which maybe
15074 * because customer mapped another volume to the
15075 * same LUN.
15077 mptsas_log(mpt, CE_WARN, "The GUID of the "
15078 "target:%x, lun:%x was changed, maybe "
15079 "because someone mapped another volume "
15080 "to the same LUN", target, lun);
15081 (void) ddi_prop_free(old_guid);
15082 if (!MDI_PI_IS_OFFLINE(*pip)) {
15083 rval = mdi_pi_offline(*pip, 0);
15084 if (rval != MDI_SUCCESS) {
15085 mptsas_log(mpt, CE_WARN, "path:"
15086 "target:%x, lun:%x offline "
15087 "failed!", target, lun);
15088 *pip = NULL;
15089 *lun_dip = NULL;
15090 return (DDI_FAILURE);
15093 if (mdi_pi_free(*pip, 0) != MDI_SUCCESS) {
15094 mptsas_log(mpt, CE_WARN, "path:target:"
15095 "%x, lun:%x free failed!", target,
15096 lun);
15097 *pip = NULL;
15098 *lun_dip = NULL;
15099 return (DDI_FAILURE);
15102 } else {
15103 mptsas_log(mpt, CE_WARN, "Can't get client-guid "
15104 "property for path:target:%x, lun:%x", target, lun);
15105 *pip = NULL;
15106 *lun_dip = NULL;
15107 return (DDI_FAILURE);
15110 scsi_hba_nodename_compatible_get(inq, NULL,
15111 inq->inq_dtype, NULL, &nodename, &compatible, &ncompatible);
15114 * if nodename can't be determined then print a message and skip it
15116 if (nodename == NULL) {
15117 mptsas_log(mpt, CE_WARN, "mptsas driver found no compatible "
15118 "driver for target%d lun %d dtype:0x%02x", target, lun,
15119 inq->inq_dtype);
15120 return (DDI_FAILURE);
15123 wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
15124 /* The property is needed by MPAPI */
15125 (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
15127 lun_addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
15128 if (guid) {
15129 (void) sprintf(lun_addr, "w%s,%x", wwn_str, lun);
15130 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15131 } else {
15132 (void) sprintf(lun_addr, "p%x,%x", phy, lun);
15133 (void) sprintf(wwn_str, "p%x", phy);
15136 mdi_rtn = mdi_pi_alloc_compatible(pdip, nodename,
15137 guid, lun_addr, compatible, ncompatible,
15138 0, pip);
15139 if (mdi_rtn == MDI_SUCCESS) {
15141 if (mdi_prop_update_string(*pip, MDI_GUID,
15142 guid) != DDI_SUCCESS) {
15143 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15144 "create prop for target %d lun %d (MDI_GUID)",
15145 target, lun);
15146 mdi_rtn = MDI_FAILURE;
15147 goto virt_create_done;
15150 if (mdi_prop_update_int(*pip, LUN_PROP,
15151 lun) != DDI_SUCCESS) {
15152 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15153 "create prop for target %d lun %d (LUN_PROP)",
15154 target, lun);
15155 mdi_rtn = MDI_FAILURE;
15156 goto virt_create_done;
15158 lun64 = (int64_t)lun;
15159 if (mdi_prop_update_int64(*pip, LUN64_PROP,
15160 lun64) != DDI_SUCCESS) {
15161 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15162 "create prop for target %d (LUN64_PROP)",
15163 target);
15164 mdi_rtn = MDI_FAILURE;
15165 goto virt_create_done;
15167 if (mdi_prop_update_string_array(*pip, "compatible",
15168 compatible, ncompatible) !=
15169 DDI_PROP_SUCCESS) {
15170 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15171 "create prop for target %d lun %d (COMPATIBLE)",
15172 target, lun);
15173 mdi_rtn = MDI_FAILURE;
15174 goto virt_create_done;
15176 if (sas_wwn && (mdi_prop_update_string(*pip,
15177 SCSI_ADDR_PROP_TARGET_PORT, wwn_str) != DDI_PROP_SUCCESS)) {
15178 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15179 "create prop for target %d lun %d "
15180 "(target-port)", target, lun);
15181 mdi_rtn = MDI_FAILURE;
15182 goto virt_create_done;
15183 } else if ((sas_wwn == 0) && (mdi_prop_update_int(*pip,
15184 "sata-phy", phy) != DDI_PROP_SUCCESS)) {
15186 * Direct attached SATA device without DeviceName
15188 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15189 "create prop for SAS target %d lun %d "
15190 "(sata-phy)", target, lun);
15191 mdi_rtn = MDI_FAILURE;
15192 goto virt_create_done;
15194 mutex_enter(&mpt->m_mutex);
15196 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15197 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15198 (uint32_t)ptgt->m_devhdl;
15199 rval = mptsas_get_sas_device_page0(mpt, page_address,
15200 &dev_hdl, &dev_sas_wwn, &dev_info, &physport,
15201 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15202 if (rval != DDI_SUCCESS) {
15203 mutex_exit(&mpt->m_mutex);
15204 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15205 "parent device for handle %d", page_address);
15206 mdi_rtn = MDI_FAILURE;
15207 goto virt_create_done;
15210 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15211 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
15212 rval = mptsas_get_sas_device_page0(mpt, page_address,
15213 &dev_hdl, &pdev_sas_wwn, &pdev_info, &physport,
15214 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15215 if (rval != DDI_SUCCESS) {
15216 mutex_exit(&mpt->m_mutex);
15217 mptsas_log(mpt, CE_WARN, "mptsas unable to get"
15218 "device info for handle %d", page_address);
15219 mdi_rtn = MDI_FAILURE;
15220 goto virt_create_done;
15223 mutex_exit(&mpt->m_mutex);
15226 * If this device direct attached to the controller
15227 * set the attached-port to the base wwid
15229 if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15230 != DEVINFO_DIRECT_ATTACHED) {
15231 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15232 pdev_sas_wwn);
15233 } else {
15235 * Update the iport's attached-port to guid
15237 if (sas_wwn == 0) {
15238 (void) sprintf(wwn_str, "p%x", phy);
15239 } else {
15240 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15242 if (ddi_prop_update_string(DDI_DEV_T_NONE,
15243 pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15244 DDI_PROP_SUCCESS) {
15245 mptsas_log(mpt, CE_WARN,
15246 "mptsas unable to create "
15247 "property for iport target-port"
15248 " %s (sas_wwn)",
15249 wwn_str);
15250 mdi_rtn = MDI_FAILURE;
15251 goto virt_create_done;
15254 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15255 mpt->un.m_base_wwid);
15258 if (mdi_prop_update_string(*pip,
15259 SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
15260 DDI_PROP_SUCCESS) {
15261 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15262 "property for iport attached-port %s (sas_wwn)",
15263 attached_wwn_str);
15264 mdi_rtn = MDI_FAILURE;
15265 goto virt_create_done;
15269 if (inq->inq_dtype == 0) {
15270 component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15272 * set obp path for pathinfo
15274 (void) snprintf(component, MAXPATHLEN,
15275 "disk@%s", lun_addr);
15277 if (mdi_pi_pathname_obp_set(*pip, component) !=
15278 DDI_SUCCESS) {
15279 mptsas_log(mpt, CE_WARN, "mpt_sas driver "
15280 "unable to set obp-path for object %s",
15281 component);
15282 mdi_rtn = MDI_FAILURE;
15283 goto virt_create_done;
15287 *lun_dip = MDI_PI(*pip)->pi_client->ct_dip;
15288 if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
15289 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
15290 if ((ndi_prop_update_int(DDI_DEV_T_NONE, *lun_dip,
15291 "pm-capable", 1)) !=
15292 DDI_PROP_SUCCESS) {
15293 mptsas_log(mpt, CE_WARN, "mptsas driver"
15294 "failed to create pm-capable "
15295 "property, target %d", target);
15296 mdi_rtn = MDI_FAILURE;
15297 goto virt_create_done;
15301 * Create the phy-num property
15303 if (mdi_prop_update_int(*pip, "phy-num",
15304 ptgt->m_phynum) != DDI_SUCCESS) {
15305 mptsas_log(mpt, CE_WARN, "mptsas driver unable to "
15306 "create phy-num property for target %d lun %d",
15307 target, lun);
15308 mdi_rtn = MDI_FAILURE;
15309 goto virt_create_done;
15311 NDBG20(("new path:%s onlining,", MDI_PI(*pip)->pi_addr));
15312 mdi_rtn = mdi_pi_online(*pip, 0);
15313 if (mdi_rtn == MDI_SUCCESS) {
15314 mutex_enter(&mpt->m_mutex);
15315 ptgt->m_led_status = 0;
15316 (void) mptsas_flush_led_status(mpt, ptgt);
15317 mutex_exit(&mpt->m_mutex);
15319 if (mdi_rtn == MDI_NOT_SUPPORTED) {
15320 mdi_rtn = MDI_FAILURE;
15322 virt_create_done:
15323 if (*pip && mdi_rtn != MDI_SUCCESS) {
15324 (void) mdi_pi_free(*pip, 0);
15325 *pip = NULL;
15326 *lun_dip = NULL;
15330 scsi_hba_nodename_compatible_free(nodename, compatible);
15331 if (lun_addr != NULL) {
15332 kmem_free(lun_addr, SCSI_MAXNAMELEN);
15334 if (wwn_str != NULL) {
15335 kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
15337 if (component != NULL) {
15338 kmem_free(component, MAXPATHLEN);
15341 return ((mdi_rtn == MDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15344 static int
15345 mptsas_create_phys_lun(dev_info_t *pdip, struct scsi_inquiry *inq,
15346 char *guid, dev_info_t **lun_dip, mptsas_target_t *ptgt, int lun)
15348 int target;
15349 int rval;
15350 int ndi_rtn = NDI_FAILURE;
15351 uint64_t be_sas_wwn;
15352 char *nodename = NULL;
15353 char **compatible = NULL;
15354 int ncompatible = 0;
15355 int instance = 0;
15356 mptsas_t *mpt = DIP2MPT(pdip);
15357 char *wwn_str = NULL;
15358 char *component = NULL;
15359 char *attached_wwn_str = NULL;
15360 uint8_t phy = 0xFF;
15361 uint64_t sas_wwn;
15362 uint32_t devinfo;
15363 uint16_t dev_hdl;
15364 uint16_t pdev_hdl;
15365 uint64_t pdev_sas_wwn;
15366 uint64_t dev_sas_wwn;
15367 uint32_t pdev_info;
15368 uint8_t physport;
15369 uint8_t phy_id;
15370 uint32_t page_address;
15371 uint16_t bay_num, enclosure, io_flags;
15372 char pdev_wwn_str[MPTSAS_WWN_STRLEN];
15373 uint32_t dev_info;
15374 int64_t lun64 = 0;
15376 mutex_enter(&mpt->m_mutex);
15377 target = ptgt->m_devhdl;
15378 sas_wwn = ptgt->m_addr.mta_wwn;
15379 devinfo = ptgt->m_deviceinfo;
15380 phy = ptgt->m_phynum;
15381 mutex_exit(&mpt->m_mutex);
15384 * generate compatible property with binding-set "mpt"
15386 scsi_hba_nodename_compatible_get(inq, NULL, inq->inq_dtype, NULL,
15387 &nodename, &compatible, &ncompatible);
15390 * if nodename can't be determined then print a message and skip it
15392 if (nodename == NULL) {
15393 mptsas_log(mpt, CE_WARN, "mptsas found no compatible driver "
15394 "for target %d lun %d", target, lun);
15395 return (DDI_FAILURE);
15398 ndi_rtn = ndi_devi_alloc(pdip, nodename,
15399 DEVI_SID_NODEID, lun_dip);
15402 * if lun alloc success, set props
15404 if (ndi_rtn == NDI_SUCCESS) {
15406 if (ndi_prop_update_int(DDI_DEV_T_NONE,
15407 *lun_dip, LUN_PROP, lun) !=
15408 DDI_PROP_SUCCESS) {
15409 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15410 "property for target %d lun %d (LUN_PROP)",
15411 target, lun);
15412 ndi_rtn = NDI_FAILURE;
15413 goto phys_create_done;
15416 lun64 = (int64_t)lun;
15417 if (ndi_prop_update_int64(DDI_DEV_T_NONE,
15418 *lun_dip, LUN64_PROP, lun64) !=
15419 DDI_PROP_SUCCESS) {
15420 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15421 "property for target %d lun64 %d (LUN64_PROP)",
15422 target, lun);
15423 ndi_rtn = NDI_FAILURE;
15424 goto phys_create_done;
15426 if (ndi_prop_update_string_array(DDI_DEV_T_NONE,
15427 *lun_dip, "compatible", compatible, ncompatible)
15428 != DDI_PROP_SUCCESS) {
15429 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15430 "property for target %d lun %d (COMPATIBLE)",
15431 target, lun);
15432 ndi_rtn = NDI_FAILURE;
15433 goto phys_create_done;
15437 * We need the SAS WWN for non-multipath devices, so
15438 * we'll use the same property as that multipathing
15439 * devices need to present for MPAPI. If we don't have
15440 * a WWN (e.g. parallel SCSI), don't create the prop.
15442 wwn_str = kmem_zalloc(MPTSAS_WWN_STRLEN, KM_SLEEP);
15443 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15444 if (sas_wwn && ndi_prop_update_string(DDI_DEV_T_NONE,
15445 *lun_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str)
15446 != DDI_PROP_SUCCESS) {
15447 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15448 "create property for SAS target %d lun %d "
15449 "(target-port)", target, lun);
15450 ndi_rtn = NDI_FAILURE;
15451 goto phys_create_done;
15454 be_sas_wwn = BE_64(sas_wwn);
15455 if (sas_wwn && ndi_prop_update_byte_array(
15456 DDI_DEV_T_NONE, *lun_dip, "port-wwn",
15457 (uchar_t *)&be_sas_wwn, 8) != DDI_PROP_SUCCESS) {
15458 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15459 "create property for SAS target %d lun %d "
15460 "(port-wwn)", target, lun);
15461 ndi_rtn = NDI_FAILURE;
15462 goto phys_create_done;
15463 } else if ((sas_wwn == 0) && (ndi_prop_update_int(
15464 DDI_DEV_T_NONE, *lun_dip, "sata-phy", phy) !=
15465 DDI_PROP_SUCCESS)) {
15467 * Direct attached SATA device without DeviceName
15469 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15470 "create property for SAS target %d lun %d "
15471 "(sata-phy)", target, lun);
15472 ndi_rtn = NDI_FAILURE;
15473 goto phys_create_done;
15476 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
15477 *lun_dip, SAS_PROP) != DDI_PROP_SUCCESS) {
15478 mptsas_log(mpt, CE_WARN, "mptsas unable to"
15479 "create property for SAS target %d lun %d"
15480 " (SAS_PROP)", target, lun);
15481 ndi_rtn = NDI_FAILURE;
15482 goto phys_create_done;
15484 if (guid && (ndi_prop_update_string(DDI_DEV_T_NONE,
15485 *lun_dip, NDI_GUID, guid) != DDI_SUCCESS)) {
15486 mptsas_log(mpt, CE_WARN, "mptsas unable "
15487 "to create guid property for target %d "
15488 "lun %d", target, lun);
15489 ndi_rtn = NDI_FAILURE;
15490 goto phys_create_done;
15494 * The following code is to set properties for SM-HBA support,
15495 * it doesn't apply to RAID volumes
15497 if (ptgt->m_addr.mta_phymask == 0)
15498 goto phys_raid_lun;
15500 mutex_enter(&mpt->m_mutex);
15502 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15503 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15504 (uint32_t)ptgt->m_devhdl;
15505 rval = mptsas_get_sas_device_page0(mpt, page_address,
15506 &dev_hdl, &dev_sas_wwn, &dev_info,
15507 &physport, &phy_id, &pdev_hdl,
15508 &bay_num, &enclosure, &io_flags);
15509 if (rval != DDI_SUCCESS) {
15510 mutex_exit(&mpt->m_mutex);
15511 mptsas_log(mpt, CE_WARN, "mptsas unable to get"
15512 "parent device for handle %d.", page_address);
15513 ndi_rtn = NDI_FAILURE;
15514 goto phys_create_done;
15517 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15518 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)pdev_hdl;
15519 rval = mptsas_get_sas_device_page0(mpt, page_address,
15520 &dev_hdl, &pdev_sas_wwn, &pdev_info, &physport,
15521 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15522 if (rval != DDI_SUCCESS) {
15523 mutex_exit(&mpt->m_mutex);
15524 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15525 "device for handle %d.", page_address);
15526 ndi_rtn = NDI_FAILURE;
15527 goto phys_create_done;
15530 mutex_exit(&mpt->m_mutex);
15533 * If this device direct attached to the controller
15534 * set the attached-port to the base wwid
15536 if ((ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15537 != DEVINFO_DIRECT_ATTACHED) {
15538 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15539 pdev_sas_wwn);
15540 } else {
15542 * Update the iport's attached-port to guid
15544 if (sas_wwn == 0) {
15545 (void) sprintf(wwn_str, "p%x", phy);
15546 } else {
15547 (void) sprintf(wwn_str, "w%016"PRIx64, sas_wwn);
15549 if (ddi_prop_update_string(DDI_DEV_T_NONE,
15550 pdip, SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15551 DDI_PROP_SUCCESS) {
15552 mptsas_log(mpt, CE_WARN,
15553 "mptsas unable to create "
15554 "property for iport target-port"
15555 " %s (sas_wwn)",
15556 wwn_str);
15557 ndi_rtn = NDI_FAILURE;
15558 goto phys_create_done;
15561 (void) sprintf(pdev_wwn_str, "w%016"PRIx64,
15562 mpt->un.m_base_wwid);
15565 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15566 *lun_dip, SCSI_ADDR_PROP_ATTACHED_PORT, pdev_wwn_str) !=
15567 DDI_PROP_SUCCESS) {
15568 mptsas_log(mpt, CE_WARN,
15569 "mptsas unable to create "
15570 "property for iport attached-port %s (sas_wwn)",
15571 attached_wwn_str);
15572 ndi_rtn = NDI_FAILURE;
15573 goto phys_create_done;
15576 if (IS_SATA_DEVICE(dev_info)) {
15577 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15578 *lun_dip, MPTSAS_VARIANT, "sata") !=
15579 DDI_PROP_SUCCESS) {
15580 mptsas_log(mpt, CE_WARN,
15581 "mptsas unable to create "
15582 "property for device variant ");
15583 ndi_rtn = NDI_FAILURE;
15584 goto phys_create_done;
15588 if (IS_ATAPI_DEVICE(dev_info)) {
15589 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15590 *lun_dip, MPTSAS_VARIANT, "atapi") !=
15591 DDI_PROP_SUCCESS) {
15592 mptsas_log(mpt, CE_WARN,
15593 "mptsas unable to create "
15594 "property for device variant ");
15595 ndi_rtn = NDI_FAILURE;
15596 goto phys_create_done;
15600 phys_raid_lun:
15602 * if this is a SAS controller, and the target is a SATA
15603 * drive, set the 'pm-capable' property for sd and if on
15604 * an OPL platform, also check if this is an ATAPI
15605 * device.
15607 instance = ddi_get_instance(mpt->m_dip);
15608 if (devinfo & (MPI2_SAS_DEVICE_INFO_SATA_DEVICE |
15609 MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE)) {
15610 NDBG2(("mptsas%d: creating pm-capable property, "
15611 "target %d", instance, target));
15613 if ((ndi_prop_update_int(DDI_DEV_T_NONE,
15614 *lun_dip, "pm-capable", 1)) !=
15615 DDI_PROP_SUCCESS) {
15616 mptsas_log(mpt, CE_WARN, "mptsas "
15617 "failed to create pm-capable "
15618 "property, target %d", target);
15619 ndi_rtn = NDI_FAILURE;
15620 goto phys_create_done;
15625 if ((inq->inq_dtype == 0) || (inq->inq_dtype == 5)) {
15627 * add 'obp-path' properties for devinfo
15629 bzero(wwn_str, sizeof (wwn_str));
15630 (void) sprintf(wwn_str, "%016"PRIx64, sas_wwn);
15631 component = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15632 if (guid) {
15633 (void) snprintf(component, MAXPATHLEN,
15634 "disk@w%s,%x", wwn_str, lun);
15635 } else {
15636 (void) snprintf(component, MAXPATHLEN,
15637 "disk@p%x,%x", phy, lun);
15639 if (ddi_pathname_obp_set(*lun_dip, component)
15640 != DDI_SUCCESS) {
15641 mptsas_log(mpt, CE_WARN, "mpt_sas driver "
15642 "unable to set obp-path for SAS "
15643 "object %s", component);
15644 ndi_rtn = NDI_FAILURE;
15645 goto phys_create_done;
15649 * Create the phy-num property for non-raid disk
15651 if (ptgt->m_addr.mta_phymask != 0) {
15652 if (ndi_prop_update_int(DDI_DEV_T_NONE,
15653 *lun_dip, "phy-num", ptgt->m_phynum) !=
15654 DDI_PROP_SUCCESS) {
15655 mptsas_log(mpt, CE_WARN, "mptsas driver "
15656 "failed to create phy-num property for "
15657 "target %d", target);
15658 ndi_rtn = NDI_FAILURE;
15659 goto phys_create_done;
15662 phys_create_done:
15664 * If props were setup ok, online the lun
15666 if (ndi_rtn == NDI_SUCCESS) {
15668 * Try to online the new node
15670 ndi_rtn = ndi_devi_online(*lun_dip, NDI_ONLINE_ATTACH);
15672 if (ndi_rtn == NDI_SUCCESS) {
15673 mutex_enter(&mpt->m_mutex);
15674 ptgt->m_led_status = 0;
15675 (void) mptsas_flush_led_status(mpt, ptgt);
15676 mutex_exit(&mpt->m_mutex);
15680 * If success set rtn flag, else unwire alloc'd lun
15682 if (ndi_rtn != NDI_SUCCESS) {
15683 NDBG12(("mptsas driver unable to online "
15684 "target %d lun %d", target, lun));
15685 ndi_prop_remove_all(*lun_dip);
15686 (void) ndi_devi_free(*lun_dip);
15687 *lun_dip = NULL;
15691 scsi_hba_nodename_compatible_free(nodename, compatible);
15693 if (wwn_str != NULL) {
15694 kmem_free(wwn_str, MPTSAS_WWN_STRLEN);
15696 if (component != NULL) {
15697 kmem_free(component, MAXPATHLEN);
15701 return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
15704 static int
15705 mptsas_probe_smp(dev_info_t *pdip, uint64_t wwn)
15707 mptsas_t *mpt = DIP2MPT(pdip);
15708 struct smp_device smp_sd;
15710 /* XXX An HBA driver should not be allocating an smp_device. */
15711 bzero(&smp_sd, sizeof (struct smp_device));
15712 smp_sd.smp_sd_address.smp_a_hba_tran = mpt->m_smptran;
15713 bcopy(&wwn, smp_sd.smp_sd_address.smp_a_wwn, SAS_WWN_BYTE_SIZE);
15715 if (smp_probe(&smp_sd) != DDI_PROBE_SUCCESS)
15716 return (NDI_FAILURE);
15717 return (NDI_SUCCESS);
15720 static int
15721 mptsas_config_smp(dev_info_t *pdip, uint64_t sas_wwn, dev_info_t **smp_dip)
15723 mptsas_t *mpt = DIP2MPT(pdip);
15724 mptsas_smp_t *psmp = NULL;
15725 int rval;
15726 int phymask;
15729 * Get the physical port associated to the iport
15730 * PHYMASK TODO
15732 phymask = ddi_prop_get_int(DDI_DEV_T_ANY, pdip, 0,
15733 "phymask", 0);
15735 * Find the smp node in hash table with specified sas address and
15736 * physical port
15738 psmp = mptsas_wwid_to_psmp(mpt, phymask, sas_wwn);
15739 if (psmp == NULL) {
15740 return (DDI_FAILURE);
15743 rval = mptsas_online_smp(pdip, psmp, smp_dip);
15745 return (rval);
15748 static int
15749 mptsas_online_smp(dev_info_t *pdip, mptsas_smp_t *smp_node,
15750 dev_info_t **smp_dip)
15752 char wwn_str[MPTSAS_WWN_STRLEN];
15753 char attached_wwn_str[MPTSAS_WWN_STRLEN];
15754 int ndi_rtn = NDI_FAILURE;
15755 int rval = 0;
15756 mptsas_smp_t dev_info;
15757 uint32_t page_address;
15758 mptsas_t *mpt = DIP2MPT(pdip);
15759 uint16_t dev_hdl;
15760 uint64_t sas_wwn;
15761 uint64_t smp_sas_wwn;
15762 uint8_t physport;
15763 uint8_t phy_id;
15764 uint16_t pdev_hdl;
15765 uint8_t numphys = 0;
15766 uint16_t i = 0;
15767 char phymask[MPTSAS_MAX_PHYS];
15768 char *iport = NULL;
15769 mptsas_phymask_t phy_mask = 0;
15770 uint16_t attached_devhdl;
15771 uint16_t bay_num, enclosure, io_flags;
15773 (void) sprintf(wwn_str, "%"PRIx64, smp_node->m_addr.mta_wwn);
15776 * Probe smp device, prevent the node of removed device from being
15777 * configured succesfully
15779 if (mptsas_probe_smp(pdip, smp_node->m_addr.mta_wwn) != NDI_SUCCESS) {
15780 return (DDI_FAILURE);
15783 if ((*smp_dip = mptsas_find_smp_child(pdip, wwn_str)) != NULL) {
15784 return (DDI_SUCCESS);
15787 ndi_rtn = ndi_devi_alloc(pdip, "smp", DEVI_SID_NODEID, smp_dip);
15790 * if lun alloc success, set props
15792 if (ndi_rtn == NDI_SUCCESS) {
15794 * Set the flavor of the child to be SMP flavored
15796 ndi_flavor_set(*smp_dip, SCSA_FLAVOR_SMP);
15798 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15799 *smp_dip, SMP_WWN, wwn_str) !=
15800 DDI_PROP_SUCCESS) {
15801 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15802 "property for smp device %s (sas_wwn)",
15803 wwn_str);
15804 ndi_rtn = NDI_FAILURE;
15805 goto smp_create_done;
15807 (void) sprintf(wwn_str, "w%"PRIx64, smp_node->m_addr.mta_wwn);
15808 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15809 *smp_dip, SCSI_ADDR_PROP_TARGET_PORT, wwn_str) !=
15810 DDI_PROP_SUCCESS) {
15811 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15812 "property for iport target-port %s (sas_wwn)",
15813 wwn_str);
15814 ndi_rtn = NDI_FAILURE;
15815 goto smp_create_done;
15818 mutex_enter(&mpt->m_mutex);
15820 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_HNDL &
15821 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | smp_node->m_devhdl;
15822 rval = mptsas_get_sas_expander_page0(mpt, page_address,
15823 &dev_info);
15824 if (rval != DDI_SUCCESS) {
15825 mutex_exit(&mpt->m_mutex);
15826 mptsas_log(mpt, CE_WARN,
15827 "mptsas unable to get expander "
15828 "parent device info for %x", page_address);
15829 ndi_rtn = NDI_FAILURE;
15830 goto smp_create_done;
15833 smp_node->m_pdevhdl = dev_info.m_pdevhdl;
15834 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15835 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15836 (uint32_t)dev_info.m_pdevhdl;
15837 rval = mptsas_get_sas_device_page0(mpt, page_address,
15838 &dev_hdl, &sas_wwn, &smp_node->m_pdevinfo, &physport,
15839 &phy_id, &pdev_hdl, &bay_num, &enclosure, &io_flags);
15840 if (rval != DDI_SUCCESS) {
15841 mutex_exit(&mpt->m_mutex);
15842 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15843 "device info for %x", page_address);
15844 ndi_rtn = NDI_FAILURE;
15845 goto smp_create_done;
15848 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_HANDLE &
15849 MPI2_SAS_DEVICE_PGAD_FORM_MASK) |
15850 (uint32_t)dev_info.m_devhdl;
15851 rval = mptsas_get_sas_device_page0(mpt, page_address,
15852 &dev_hdl, &smp_sas_wwn, &smp_node->m_deviceinfo,
15853 &physport, &phy_id, &pdev_hdl, &bay_num, &enclosure,
15854 &io_flags);
15855 if (rval != DDI_SUCCESS) {
15856 mutex_exit(&mpt->m_mutex);
15857 mptsas_log(mpt, CE_WARN, "mptsas unable to get "
15858 "device info for %x", page_address);
15859 ndi_rtn = NDI_FAILURE;
15860 goto smp_create_done;
15862 mutex_exit(&mpt->m_mutex);
15865 * If this smp direct attached to the controller
15866 * set the attached-port to the base wwid
15868 if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15869 != DEVINFO_DIRECT_ATTACHED) {
15870 (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15871 sas_wwn);
15872 } else {
15873 (void) sprintf(attached_wwn_str, "w%016"PRIx64,
15874 mpt->un.m_base_wwid);
15877 if (ndi_prop_update_string(DDI_DEV_T_NONE,
15878 *smp_dip, SCSI_ADDR_PROP_ATTACHED_PORT, attached_wwn_str) !=
15879 DDI_PROP_SUCCESS) {
15880 mptsas_log(mpt, CE_WARN, "mptsas unable to create "
15881 "property for smp attached-port %s (sas_wwn)",
15882 attached_wwn_str);
15883 ndi_rtn = NDI_FAILURE;
15884 goto smp_create_done;
15887 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
15888 *smp_dip, SMP_PROP) != DDI_PROP_SUCCESS) {
15889 mptsas_log(mpt, CE_WARN, "mptsas unable to "
15890 "create property for SMP %s (SMP_PROP) ",
15891 wwn_str);
15892 ndi_rtn = NDI_FAILURE;
15893 goto smp_create_done;
15897 * check the smp to see whether it direct
15898 * attached to the controller
15900 if ((smp_node->m_deviceinfo & DEVINFO_DIRECT_ATTACHED)
15901 != DEVINFO_DIRECT_ATTACHED) {
15902 goto smp_create_done;
15904 numphys = ddi_prop_get_int(DDI_DEV_T_ANY, pdip,
15905 DDI_PROP_DONTPASS, MPTSAS_NUM_PHYS, -1);
15906 if (numphys > 0) {
15907 goto smp_create_done;
15910 * this iport is an old iport, we need to
15911 * reconfig the props for it.
15913 if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15914 MPTSAS_VIRTUAL_PORT, 0) !=
15915 DDI_PROP_SUCCESS) {
15916 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15917 MPTSAS_VIRTUAL_PORT);
15918 mptsas_log(mpt, CE_WARN, "mptsas virtual port "
15919 "prop update failed");
15920 goto smp_create_done;
15923 mutex_enter(&mpt->m_mutex);
15924 numphys = 0;
15925 iport = ddi_get_name_addr(pdip);
15926 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15927 bzero(phymask, sizeof (phymask));
15928 (void) sprintf(phymask,
15929 "%x", mpt->m_phy_info[i].phy_mask);
15930 if (strcmp(phymask, iport) == 0) {
15931 phy_mask = mpt->m_phy_info[i].phy_mask;
15932 break;
15936 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
15937 if ((phy_mask >> i) & 0x01) {
15938 numphys++;
15942 * Update PHY info for smhba
15944 if (mptsas_smhba_phy_init(mpt)) {
15945 mutex_exit(&mpt->m_mutex);
15946 mptsas_log(mpt, CE_WARN, "mptsas phy update "
15947 "failed");
15948 goto smp_create_done;
15950 mutex_exit(&mpt->m_mutex);
15952 mptsas_smhba_set_all_phy_props(mpt, pdip, numphys, phy_mask,
15953 &attached_devhdl);
15955 if (ddi_prop_update_int(DDI_DEV_T_NONE, pdip,
15956 MPTSAS_NUM_PHYS, numphys) !=
15957 DDI_PROP_SUCCESS) {
15958 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15959 MPTSAS_NUM_PHYS);
15960 mptsas_log(mpt, CE_WARN, "mptsas update "
15961 "num phys props failed");
15962 goto smp_create_done;
15965 * Add parent's props for SMHBA support
15967 if (ddi_prop_update_string(DDI_DEV_T_NONE, pdip,
15968 SCSI_ADDR_PROP_ATTACHED_PORT, wwn_str) !=
15969 DDI_PROP_SUCCESS) {
15970 (void) ddi_prop_remove(DDI_DEV_T_NONE, pdip,
15971 SCSI_ADDR_PROP_ATTACHED_PORT);
15972 mptsas_log(mpt, CE_WARN, "mptsas update iport"
15973 "attached-port failed");
15974 goto smp_create_done;
15977 smp_create_done:
15979 * If props were setup ok, online the lun
15981 if (ndi_rtn == NDI_SUCCESS) {
15983 * Try to online the new node
15985 ndi_rtn = ndi_devi_online(*smp_dip, NDI_ONLINE_ATTACH);
15989 * If success set rtn flag, else unwire alloc'd lun
15991 if (ndi_rtn != NDI_SUCCESS) {
15992 NDBG12(("mptsas unable to online "
15993 "SMP target %s", wwn_str));
15994 ndi_prop_remove_all(*smp_dip);
15995 (void) ndi_devi_free(*smp_dip);
15999 return ((ndi_rtn == NDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
16002 /* smp transport routine */
16003 static int mptsas_smp_start(struct smp_pkt *smp_pkt)
16005 uint64_t wwn;
16006 Mpi2SmpPassthroughRequest_t req;
16007 Mpi2SmpPassthroughReply_t rep;
16008 uint32_t direction = 0;
16009 mptsas_t *mpt;
16010 int ret;
16011 uint64_t tmp64;
16013 mpt = (mptsas_t *)smp_pkt->smp_pkt_address->
16014 smp_a_hba_tran->smp_tran_hba_private;
16016 bcopy(smp_pkt->smp_pkt_address->smp_a_wwn, &wwn, SAS_WWN_BYTE_SIZE);
16018 * Need to compose a SMP request message
16019 * and call mptsas_do_passthru() function
16021 bzero(&req, sizeof (req));
16022 bzero(&rep, sizeof (rep));
16023 req.PassthroughFlags = 0;
16024 req.PhysicalPort = 0xff;
16025 req.ChainOffset = 0;
16026 req.Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
16028 if ((smp_pkt->smp_pkt_reqsize & 0xffff0000ul) != 0) {
16029 smp_pkt->smp_pkt_reason = ERANGE;
16030 return (DDI_FAILURE);
16032 req.RequestDataLength = LE_16((uint16_t)(smp_pkt->smp_pkt_reqsize - 4));
16034 req.MsgFlags = 0;
16035 tmp64 = LE_64(wwn);
16036 bcopy(&tmp64, &req.SASAddress, SAS_WWN_BYTE_SIZE);
16037 if (smp_pkt->smp_pkt_rspsize > 0) {
16038 direction |= MPTSAS_PASS_THRU_DIRECTION_READ;
16040 if (smp_pkt->smp_pkt_reqsize > 0) {
16041 direction |= MPTSAS_PASS_THRU_DIRECTION_WRITE;
16044 mutex_enter(&mpt->m_mutex);
16045 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep,
16046 (uint8_t *)smp_pkt->smp_pkt_rsp,
16047 offsetof(Mpi2SmpPassthroughRequest_t, SGL), sizeof (rep),
16048 smp_pkt->smp_pkt_rspsize - 4, direction,
16049 (uint8_t *)smp_pkt->smp_pkt_req, smp_pkt->smp_pkt_reqsize - 4,
16050 smp_pkt->smp_pkt_timeout, FKIOCTL);
16051 mutex_exit(&mpt->m_mutex);
16052 if (ret != 0) {
16053 cmn_err(CE_WARN, "smp_start do passthru error %d", ret);
16054 smp_pkt->smp_pkt_reason = (uchar_t)(ret);
16055 return (DDI_FAILURE);
16057 /* do passthrough success, check the smp status */
16058 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
16059 switch (LE_16(rep.IOCStatus)) {
16060 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
16061 smp_pkt->smp_pkt_reason = ENODEV;
16062 break;
16063 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
16064 smp_pkt->smp_pkt_reason = EOVERFLOW;
16065 break;
16066 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
16067 smp_pkt->smp_pkt_reason = EIO;
16068 break;
16069 default:
16070 mptsas_log(mpt, CE_NOTE, "smp_start: get unknown ioc"
16071 "status:%x", LE_16(rep.IOCStatus));
16072 smp_pkt->smp_pkt_reason = EIO;
16073 break;
16075 return (DDI_FAILURE);
16077 if (rep.SASStatus != MPI2_SASSTATUS_SUCCESS) {
16078 mptsas_log(mpt, CE_NOTE, "smp_start: get error SAS status:%x",
16079 rep.SASStatus);
16080 smp_pkt->smp_pkt_reason = EIO;
16081 return (DDI_FAILURE);
16084 return (DDI_SUCCESS);
16088 * If we didn't get a match, we need to get sas page0 for each device, and
16089 * untill we get a match. If failed, return NULL
16091 static mptsas_target_t *
16092 mptsas_phy_to_tgt(mptsas_t *mpt, mptsas_phymask_t phymask, uint8_t phy)
16094 int i, j = 0;
16095 int rval = 0;
16096 uint16_t cur_handle;
16097 uint32_t page_address;
16098 mptsas_target_t *ptgt = NULL;
16101 * PHY named device must be direct attached and attaches to
16102 * narrow port, if the iport is not parent of the device which
16103 * we are looking for.
16105 for (i = 0; i < MPTSAS_MAX_PHYS; i++) {
16106 if ((1 << i) & phymask)
16107 j++;
16110 if (j > 1)
16111 return (NULL);
16114 * Must be a narrow port and single device attached to the narrow port
16115 * So the physical port num of device which is equal to the iport's
16116 * port num is the device what we are looking for.
16119 if (mpt->m_phy_info[phy].phy_mask != phymask)
16120 return (NULL);
16122 mutex_enter(&mpt->m_mutex);
16124 ptgt = refhash_linear_search(mpt->m_targets, mptsas_target_eval_nowwn,
16125 &phy);
16126 if (ptgt != NULL) {
16127 mutex_exit(&mpt->m_mutex);
16128 return (ptgt);
16131 if (mpt->m_done_traverse_dev) {
16132 mutex_exit(&mpt->m_mutex);
16133 return (NULL);
16136 /* If didn't get a match, come here */
16137 cur_handle = mpt->m_dev_handle;
16138 for (; ; ) {
16139 ptgt = NULL;
16140 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
16141 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | (uint32_t)cur_handle;
16142 rval = mptsas_get_target_device_info(mpt, page_address,
16143 &cur_handle, &ptgt);
16144 if ((rval == DEV_INFO_FAIL_PAGE0) ||
16145 (rval == DEV_INFO_FAIL_ALLOC) ||
16146 (rval == DEV_INFO_FAIL_GUID)) {
16147 break;
16149 if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
16150 (rval == DEV_INFO_PHYS_DISK)) {
16151 continue;
16153 mpt->m_dev_handle = cur_handle;
16155 if ((ptgt->m_addr.mta_wwn == 0) && (ptgt->m_phynum == phy)) {
16156 break;
16160 mutex_exit(&mpt->m_mutex);
16161 return (ptgt);
16165 * The ptgt->m_addr.mta_wwn contains the wwid for each disk.
16166 * For Raid volumes, we need to check m_raidvol[x].m_raidwwid
16167 * If we didn't get a match, we need to get sas page0 for each device, and
16168 * untill we get a match
16169 * If failed, return NULL
16171 static mptsas_target_t *
16172 mptsas_wwid_to_ptgt(mptsas_t *mpt, mptsas_phymask_t phymask, uint64_t wwid)
16174 int rval = 0;
16175 uint16_t cur_handle;
16176 uint32_t page_address;
16177 mptsas_target_t *tmp_tgt = NULL;
16178 mptsas_target_addr_t addr;
16180 addr.mta_wwn = wwid;
16181 addr.mta_phymask = phymask;
16182 mutex_enter(&mpt->m_mutex);
16183 tmp_tgt = refhash_lookup(mpt->m_targets, &addr);
16184 if (tmp_tgt != NULL) {
16185 mutex_exit(&mpt->m_mutex);
16186 return (tmp_tgt);
16189 if (phymask == 0) {
16191 * It's IR volume
16193 rval = mptsas_get_raid_info(mpt);
16194 if (rval) {
16195 tmp_tgt = refhash_lookup(mpt->m_targets, &addr);
16197 mutex_exit(&mpt->m_mutex);
16198 return (tmp_tgt);
16201 if (mpt->m_done_traverse_dev) {
16202 mutex_exit(&mpt->m_mutex);
16203 return (NULL);
16206 /* If didn't get a match, come here */
16207 cur_handle = mpt->m_dev_handle;
16208 for (;;) {
16209 tmp_tgt = NULL;
16210 page_address = (MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE &
16211 MPI2_SAS_DEVICE_PGAD_FORM_MASK) | cur_handle;
16212 rval = mptsas_get_target_device_info(mpt, page_address,
16213 &cur_handle, &tmp_tgt);
16214 if ((rval == DEV_INFO_FAIL_PAGE0) ||
16215 (rval == DEV_INFO_FAIL_ALLOC) ||
16216 (rval == DEV_INFO_FAIL_GUID)) {
16217 tmp_tgt = NULL;
16218 break;
16220 if ((rval == DEV_INFO_WRONG_DEVICE_TYPE) ||
16221 (rval == DEV_INFO_PHYS_DISK)) {
16222 continue;
16224 mpt->m_dev_handle = cur_handle;
16225 if ((tmp_tgt->m_addr.mta_wwn) &&
16226 (tmp_tgt->m_addr.mta_wwn == wwid) &&
16227 (tmp_tgt->m_addr.mta_phymask == phymask)) {
16228 break;
16232 mutex_exit(&mpt->m_mutex);
16233 return (tmp_tgt);
16236 static mptsas_smp_t *
16237 mptsas_wwid_to_psmp(mptsas_t *mpt, mptsas_phymask_t phymask, uint64_t wwid)
16239 int rval = 0;
16240 uint16_t cur_handle;
16241 uint32_t page_address;
16242 mptsas_smp_t smp_node, *psmp = NULL;
16243 mptsas_target_addr_t addr;
16245 addr.mta_wwn = wwid;
16246 addr.mta_phymask = phymask;
16247 mutex_enter(&mpt->m_mutex);
16248 psmp = refhash_lookup(mpt->m_smp_targets, &addr);
16249 if (psmp != NULL) {
16250 mutex_exit(&mpt->m_mutex);
16251 return (psmp);
16254 if (mpt->m_done_traverse_smp) {
16255 mutex_exit(&mpt->m_mutex);
16256 return (NULL);
16259 /* If didn't get a match, come here */
16260 cur_handle = mpt->m_smp_devhdl;
16261 for (;;) {
16262 psmp = NULL;
16263 page_address = (MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL &
16264 MPI2_SAS_EXPAND_PGAD_FORM_MASK) | (uint32_t)cur_handle;
16265 rval = mptsas_get_sas_expander_page0(mpt, page_address,
16266 &smp_node);
16267 if (rval != DDI_SUCCESS) {
16268 break;
16270 mpt->m_smp_devhdl = cur_handle = smp_node.m_devhdl;
16271 psmp = mptsas_smp_alloc(mpt, &smp_node);
16272 ASSERT(psmp);
16273 if ((psmp->m_addr.mta_wwn) && (psmp->m_addr.mta_wwn == wwid) &&
16274 (psmp->m_addr.mta_phymask == phymask)) {
16275 break;
16279 mutex_exit(&mpt->m_mutex);
16280 return (psmp);
16283 mptsas_target_t *
16284 mptsas_tgt_alloc(refhash_t *refhash, uint16_t devhdl, uint64_t wwid,
16285 uint32_t devinfo, mptsas_phymask_t phymask, uint8_t phynum)
16287 mptsas_target_t *tmp_tgt = NULL;
16288 mptsas_target_addr_t addr;
16290 addr.mta_wwn = wwid;
16291 addr.mta_phymask = phymask;
16292 tmp_tgt = refhash_lookup(refhash, &addr);
16293 if (tmp_tgt != NULL) {
16294 NDBG20(("Hash item already exist"));
16295 tmp_tgt->m_deviceinfo = devinfo;
16296 tmp_tgt->m_devhdl = devhdl; /* XXX - duplicate? */
16297 return (tmp_tgt);
16299 tmp_tgt = kmem_zalloc(sizeof (struct mptsas_target), KM_SLEEP);
16300 if (tmp_tgt == NULL) {
16301 cmn_err(CE_WARN, "Fatal, allocated tgt failed");
16302 return (NULL);
16304 tmp_tgt->m_devhdl = devhdl;
16305 tmp_tgt->m_addr.mta_wwn = wwid;
16306 tmp_tgt->m_deviceinfo = devinfo;
16307 tmp_tgt->m_addr.mta_phymask = phymask;
16308 tmp_tgt->m_phynum = phynum;
16309 /* Initialized the tgt structure */
16310 tmp_tgt->m_qfull_retries = QFULL_RETRIES;
16311 tmp_tgt->m_qfull_retry_interval =
16312 drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
16313 tmp_tgt->m_t_throttle = MAX_THROTTLE;
16314 TAILQ_INIT(&tmp_tgt->m_active_cmdq);
16316 refhash_insert(refhash, tmp_tgt);
16318 return (tmp_tgt);
16321 static void
16322 mptsas_smp_target_copy(mptsas_smp_t *src, mptsas_smp_t *dst)
16324 dst->m_devhdl = src->m_devhdl;
16325 dst->m_deviceinfo = src->m_deviceinfo;
16326 dst->m_pdevhdl = src->m_pdevhdl;
16327 dst->m_pdevinfo = src->m_pdevinfo;
16330 static mptsas_smp_t *
16331 mptsas_smp_alloc(mptsas_t *mpt, mptsas_smp_t *data)
16333 mptsas_target_addr_t addr;
16334 mptsas_smp_t *ret_data;
16336 addr.mta_wwn = data->m_addr.mta_wwn;
16337 addr.mta_phymask = data->m_addr.mta_phymask;
16338 ret_data = refhash_lookup(mpt->m_smp_targets, &addr);
16340 * If there's already a matching SMP target, update its fields
16341 * in place. Since the address is not changing, it's safe to do
16342 * this. We cannot just bcopy() here because the structure we've
16343 * been given has invalid hash links.
16345 if (ret_data != NULL) {
16346 mptsas_smp_target_copy(data, ret_data);
16347 return (ret_data);
16350 ret_data = kmem_alloc(sizeof (mptsas_smp_t), KM_SLEEP);
16351 bcopy(data, ret_data, sizeof (mptsas_smp_t));
16352 refhash_insert(mpt->m_smp_targets, ret_data);
16353 return (ret_data);
16357 * Functions for SGPIO LED support
16359 static dev_info_t *
16360 mptsas_get_dip_from_dev(dev_t dev, mptsas_phymask_t *phymask)
16362 dev_info_t *dip;
16363 int prop;
16364 dip = e_ddi_hold_devi_by_dev(dev, 0);
16365 if (dip == NULL)
16366 return (dip);
16367 prop = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
16368 "phymask", 0);
16369 *phymask = (mptsas_phymask_t)prop;
16370 ddi_release_devi(dip);
16371 return (dip);
16373 static mptsas_target_t *
16374 mptsas_addr_to_ptgt(mptsas_t *mpt, char *addr, mptsas_phymask_t phymask)
16376 uint8_t phynum;
16377 uint64_t wwn;
16378 int lun;
16379 mptsas_target_t *ptgt = NULL;
16381 if (mptsas_parse_address(addr, &wwn, &phynum, &lun) != DDI_SUCCESS) {
16382 return (NULL);
16384 if (addr[0] == 'w') {
16385 ptgt = mptsas_wwid_to_ptgt(mpt, (int)phymask, wwn);
16386 } else {
16387 ptgt = mptsas_phy_to_tgt(mpt, (int)phymask, phynum);
16389 return (ptgt);
16392 static int
16393 mptsas_flush_led_status(mptsas_t *mpt, mptsas_target_t *ptgt)
16395 uint32_t slotstatus = 0;
16397 /* Build an MPI2 Slot Status based on our view of the world */
16398 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_IDENT - 1)))
16399 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_IDENTIFY_REQUEST;
16400 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_FAIL - 1)))
16401 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
16402 if (ptgt->m_led_status & (1 << (MPTSAS_LEDCTL_LED_OK2RM - 1)))
16403 slotstatus |= MPI2_SEP_REQ_SLOTSTATUS_REQUEST_REMOVE;
16405 /* Write it to the controller */
16406 NDBG14(("mptsas_ioctl: set LED status %x for slot %x",
16407 slotstatus, ptgt->m_slot_num));
16408 return (mptsas_send_sep(mpt, ptgt, &slotstatus,
16409 MPI2_SEP_REQ_ACTION_WRITE_STATUS));
16413 * send sep request, use enclosure/slot addressing
16415 static int
16416 mptsas_send_sep(mptsas_t *mpt, mptsas_target_t *ptgt,
16417 uint32_t *status, uint8_t act)
16419 Mpi2SepRequest_t req;
16420 Mpi2SepReply_t rep;
16421 int ret;
16423 ASSERT(mutex_owned(&mpt->m_mutex));
16426 * We only support SEP control of directly-attached targets, in which
16427 * case the "SEP" we're talking to is a virtual one contained within
16428 * the HBA itself. This is necessary because DA targets typically have
16429 * no other mechanism for LED control. Targets for which a separate
16430 * enclosure service processor exists should be controlled via ses(7d)
16431 * or sgen(7d). Furthermore, since such requests can time out, they
16432 * should be made in user context rather than in response to
16433 * asynchronous fabric changes.
16435 * In addition, we do not support this operation for RAID volumes,
16436 * since there is no slot associated with them.
16438 if (!(ptgt->m_deviceinfo & DEVINFO_DIRECT_ATTACHED) ||
16439 ptgt->m_addr.mta_phymask == 0) {
16440 return (ENOTTY);
16443 bzero(&req, sizeof (req));
16444 bzero(&rep, sizeof (rep));
16446 req.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
16447 req.Action = act;
16448 req.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
16449 req.EnclosureHandle = LE_16(ptgt->m_enclosure);
16450 req.Slot = LE_16(ptgt->m_slot_num);
16451 if (act == MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16452 req.SlotStatus = LE_32(*status);
16454 ret = mptsas_do_passthru(mpt, (uint8_t *)&req, (uint8_t *)&rep, NULL,
16455 sizeof (req), sizeof (rep), 0, 0, NULL, 0, 60, FKIOCTL);
16456 if (ret != 0) {
16457 mptsas_log(mpt, CE_NOTE, "mptsas_send_sep: passthru SEP "
16458 "Processor Request message error %d", ret);
16459 return (ret);
16461 /* do passthrough success, check the ioc status */
16462 if (LE_16(rep.IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
16463 mptsas_log(mpt, CE_NOTE, "send_sep act %x: ioc "
16464 "status:%x loginfo %x", act, LE_16(rep.IOCStatus),
16465 LE_32(rep.IOCLogInfo));
16466 switch (LE_16(rep.IOCStatus) & MPI2_IOCSTATUS_MASK) {
16467 case MPI2_IOCSTATUS_INVALID_FUNCTION:
16468 case MPI2_IOCSTATUS_INVALID_VPID:
16469 case MPI2_IOCSTATUS_INVALID_FIELD:
16470 case MPI2_IOCSTATUS_INVALID_STATE:
16471 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
16472 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
16473 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
16474 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
16475 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
16476 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
16477 return (EINVAL);
16478 case MPI2_IOCSTATUS_BUSY:
16479 return (EBUSY);
16480 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
16481 return (EAGAIN);
16482 case MPI2_IOCSTATUS_INVALID_SGL:
16483 case MPI2_IOCSTATUS_INTERNAL_ERROR:
16484 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
16485 default:
16486 return (EIO);
16489 if (act != MPI2_SEP_REQ_ACTION_WRITE_STATUS) {
16490 *status = LE_32(rep.SlotStatus);
16493 return (0);
16497 mptsas_dma_addr_create(mptsas_t *mpt, ddi_dma_attr_t dma_attr,
16498 ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp, caddr_t *dma_memp,
16499 uint32_t alloc_size, ddi_dma_cookie_t *cookiep)
16501 ddi_dma_cookie_t new_cookie;
16502 size_t alloc_len;
16503 uint_t ncookie;
16505 if (cookiep == NULL)
16506 cookiep = &new_cookie;
16508 if (ddi_dma_alloc_handle(mpt->m_dip, &dma_attr, DDI_DMA_SLEEP,
16509 NULL, dma_hdp) != DDI_SUCCESS) {
16510 return (FALSE);
16513 if (ddi_dma_mem_alloc(*dma_hdp, alloc_size, &mpt->m_dev_acc_attr,
16514 DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, dma_memp, &alloc_len,
16515 acc_hdp) != DDI_SUCCESS) {
16516 ddi_dma_free_handle(dma_hdp);
16517 *dma_hdp = NULL;
16518 return (FALSE);
16521 if (ddi_dma_addr_bind_handle(*dma_hdp, NULL, *dma_memp, alloc_len,
16522 (DDI_DMA_RDWR | DDI_DMA_CONSISTENT), DDI_DMA_SLEEP, NULL,
16523 cookiep, &ncookie) != DDI_DMA_MAPPED) {
16524 (void) ddi_dma_mem_free(acc_hdp);
16525 ddi_dma_free_handle(dma_hdp);
16526 *dma_hdp = NULL;
16527 return (FALSE);
16530 return (TRUE);
16533 void
16534 mptsas_dma_addr_destroy(ddi_dma_handle_t *dma_hdp, ddi_acc_handle_t *acc_hdp)
16536 if (*dma_hdp == NULL)
16537 return;
16539 (void) ddi_dma_unbind_handle(*dma_hdp);
16540 (void) ddi_dma_mem_free(acc_hdp);
16541 ddi_dma_free_handle(dma_hdp);
16542 *dma_hdp = NULL;