Print this page
4888 Undocument dma_req(9s)
4884 EOF scsi_hba_attach
4886 EOF ddi_dmae_getlim
4887 EOF ddi_iomin
4634 undocument scsi_hba_attach() and ddi_dma_lim(9s)
4630 clean stale references to ddi_iopb_alloc and ddi_iopb_free
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/scsi/impl/scsi_hba.c
+++ new/usr/src/uts/common/io/scsi/impl/scsi_hba.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24 - * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
24 + * Copyright 2014 Garrett D'Amore <garrett@damore.org>
25 25 */
26 26
27 27 #include <sys/note.h>
28 28
29 29 /*
30 30 * Generic SCSI Host Bus Adapter interface implementation
31 31 */
32 32 #include <sys/scsi/scsi.h>
33 33 #include <sys/scsi/generic/sas.h>
34 34 #include <sys/file.h>
35 35 #include <sys/disp.h> /* for minclsyspri */
36 36 #include <sys/ddi_impldefs.h>
37 37 #include <sys/ndi_impldefs.h>
38 38 #include <sys/sunndi.h>
39 39 #include <sys/ddi.h>
40 40 #include <sys/sunmdi.h>
41 41 #include <sys/mdi_impldefs.h>
42 42 #include <sys/callb.h>
43 43 #include <sys/epm.h>
44 44 #include <sys/damap.h>
45 45 #include <sys/time.h>
46 46 #include <sys/sunldi.h>
47 47 #include <sys/fm/protocol.h>
48 48
49 49 extern struct scsi_pkt *scsi_init_cache_pkt(struct scsi_address *,
50 50 struct scsi_pkt *, struct buf *, int, int, int, int,
51 51 int (*)(caddr_t), caddr_t);
52 52 extern void scsi_free_cache_pkt(struct scsi_address *, struct scsi_pkt *);
53 53 extern void scsi_cache_dmafree(struct scsi_address *, struct scsi_pkt *);
54 54 extern void scsi_sync_cache_pkt(struct scsi_address *, struct scsi_pkt *);
55 55 extern int modrootloaded;
56 56
57 57 /*
58 58 * Round up all allocations so that we can guarantee
59 59 * long-long alignment. This is the same alignment
60 60 * provided by kmem_alloc().
61 61 */
62 62 #define ROUNDUP(x) (((x) + 0x07) & ~0x07)
63 63
64 64 /* Magic number to track correct allocations in wrappers */
65 65 #define PKT_WRAPPER_MAGIC 0xa110ced /* alloced correctly */
66 66
67 67 kmutex_t scsi_flag_nointr_mutex;
68 68 kcondvar_t scsi_flag_nointr_cv;
69 69 kmutex_t scsi_log_mutex;
70 70
71 71 /* asynchronous probe barrier deletion data structures */
72 72 static kmutex_t scsi_hba_barrier_mutex;
73 73 static kcondvar_t scsi_hba_barrier_cv;
74 74 static struct scsi_hba_barrier {
75 75 struct scsi_hba_barrier *barrier_next;
76 76 clock_t barrier_endtime;
77 77 dev_info_t *barrier_probe;
78 78 } *scsi_hba_barrier_list;
79 79 static int scsi_hba_devi_is_barrier(dev_info_t *probe);
80 80 static void scsi_hba_barrier_tran_tgt_free(dev_info_t *probe);
81 81 static void scsi_hba_barrier_add(dev_info_t *probe, int seconds);
82 82 static int scsi_hba_remove_node(dev_info_t *child);
83 83 static void scsi_hba_barrier_daemon(void *arg);
84 84
85 85 /* LUN-change ASC/ASCQ processing data structures (stage1 and stage2) */
86 86 static kmutex_t scsi_lunchg1_mutex;
87 87 static kcondvar_t scsi_lunchg1_cv;
88 88 static struct scsi_pkt *scsi_lunchg1_list;
89 89 static void scsi_lunchg1_daemon(void *arg);
90 90 static kmutex_t scsi_lunchg2_mutex;
91 91 static kcondvar_t scsi_lunchg2_cv;
92 92 static struct scsi_lunchg2 {
93 93 struct scsi_lunchg2 *lunchg2_next;
94 94 char *lunchg2_path;
95 95 } *scsi_lunchg2_list;
96 96 static void scsi_lunchg2_daemon(void *arg);
97 97
98 98 static int scsi_findchild(dev_info_t *self, char *name, char *addr,
99 99 int init, dev_info_t **dchildp, mdi_pathinfo_t **pchildp, int *ppi);
100 100
101 101 /* return value defines for scsi_findchild */
102 102 #define CHILD_TYPE_NONE 0
103 103 #define CHILD_TYPE_DEVINFO 1
104 104 #define CHILD_TYPE_PATHINFO 2
105 105
106 106 /*
107 107 * Enumeration code path currently being followed. SE_BUSCONFIG results in
108 108 * DEVI_SID_NODEID, and SE_HP (hotplug) results in DEVI_SID_HP_NODEID.
109 109 *
110 110 * Since hotplug enumeration is based on information obtained from hardware
111 111 * (tgtmap/report_lun) the type/severity of enumeration error messages is
112 112 * sometimes based SE_HP (indirectly via ndi_dev_is_hotplug_node()). By
113 113 * convention, these messages are all produced by scsi_enumeration_failed().
114 114 */
115 115 typedef enum { SE_BUSCONFIG = 0, SE_HP = 1 } scsi_enum_t;
116 116
117 117 /* compatible properties of driver to use during probe/enumeration operations */
118 118 static char *compatible_probe = "scsa,probe";
119 119 static char *compatible_nodev = "scsa,nodev";
120 120 static char *scsi_probe_ascii[] = SCSIPROBE_ASCII;
121 121
122 122 /* number of LUNs we attempt to get on the first SCMD_REPORT_LUNS command */
123 123 int scsi_lunrpt_default_max = 256;
124 124 int scsi_lunrpt_timeout = 3; /* seconds */
125 125
126 126 /*
127 127 * Only enumerate one lun if reportluns fails on a SCSI_VERSION_3 device
128 128 * (tunable based on calling context).
129 129 */
130 130 int scsi_lunrpt_failed_do1lun = (1 << SE_HP);
131 131
132 132 /* 'scsi-binding-set' value for legacy enumerated 'spi' transports */
133 133 char *scsi_binding_set_spi = "spi";
134 134
135 135 /* enable NDI_DEVI_DEBUG for bus_[un]config operations */
136 136 int scsi_hba_bus_config_debug = 0;
137 137
138 138 /* DEBUG: enable NDI_DEVI_REMOVE for bus_unconfig of dynamic node */
139 139 int scsi_hba_bus_unconfig_remove = 0;
140 140
141 141 /* number of probe serilization messages */
142 142 int scsi_hba_wait_msg = 5;
143 143
144 144 /*
145 145 * Establish the timeout used to cache (in the probe node) the fact that the
146 146 * device does not exist. This replaces the target specific probe cache.
147 147 */
148 148 int scsi_hba_barrier_timeout = (60); /* seconds */
149 149
150 150 #ifdef DEBUG
151 151 int scsi_hba_bus_config_failure_msg = 0;
152 152 int scsi_hba_bus_config_failure_dbg = 0;
153 153 int scsi_hba_bus_config_success_msg = 0;
154 154 int scsi_hba_bus_config_success_dbg = 0;
155 155 #endif /* DEBUG */
156 156
157 157 /*
158 158 * Structure for scsi_hba_iportmap_* implementation/wrap.
159 159 */
160 160 typedef struct impl_scsi_iportmap {
161 161 dev_info_t *iportmap_hba_dip;
162 162 damap_t *iportmap_dam;
163 163 int iportmap_create_window;
164 164 uint64_t iportmap_create_time; /* clock64_t */
165 165 int iportmap_create_csync_usec;
166 166 int iportmap_settle_usec;
167 167 int iportmap_sync_cnt;
168 168 } impl_scsi_iportmap_t;
169 169
170 170 /*
171 171 * Structure for scsi_hba_tgtmap_* implementation/wrap.
172 172 *
173 173 * Every call to scsi_hba_tgtmap_set_begin will increment tgtmap_reports,
174 174 * and a call to scsi_hba_tgtmap_set_end will reset tgtmap_reports to zero.
175 175 * If, in scsi_hba_tgtmap_set_begin, we detect a tgtmap_reports value of
176 176 * scsi_hba_tgtmap_reports_max we produce a message to indicate that
177 177 * the caller is never completing an observation (i.e. we are not making
178 178 * any forward progress). If this message occurs, it indicates that the
179 179 * solaris hotplug ramifications at the target and lun level are no longer
180 180 * tracking.
181 181 *
182 182 * NOTE: LUNMAPSIZE OK for now, but should be dynamic in reportlun code.
183 183 */
184 184 typedef struct impl_scsi_tgtmap {
185 185 scsi_hba_tran_t *tgtmap_tran;
186 186 int tgtmap_reports; /* _begin, no _end */
187 187 int tgtmap_noisy;
188 188 scsi_tgt_activate_cb_t tgtmap_activate_cb;
189 189 scsi_tgt_deactivate_cb_t tgtmap_deactivate_cb;
190 190 void *tgtmap_mappriv;
191 191 damap_t *tgtmap_dam[SCSI_TGT_NTYPES];
192 192 int tgtmap_create_window;
193 193 uint64_t tgtmap_create_time; /* clock64_t */
194 194 int tgtmap_create_csync_usec;
195 195 int tgtmap_settle_usec;
196 196 int tgtmap_sync_cnt;
197 197 } impl_scsi_tgtmap_t;
198 198 #define LUNMAPSIZE 256 /* 256 LUNs/target */
199 199
200 200 /* Produce warning if number of begins without an end exceed this value */
201 201 int scsi_hba_tgtmap_reports_max = 256;
202 202
203 203 static int scsi_tgtmap_sync(scsi_hba_tgtmap_t *, int);
204 204
205 205 /* Default settle_usec damap_sync factor */
206 206 int scsi_hba_map_settle_f = 10;
207 207
208 208
209 209 /* Prototype for static dev_ops devo_*() functions */
210 210 static int scsi_hba_info(
211 211 dev_info_t *self,
212 212 ddi_info_cmd_t infocmd,
213 213 void *arg,
214 214 void **result);
215 215
216 216 /* Prototypes for static bus_ops bus_*() functions */
217 217 static int scsi_hba_bus_ctl(
218 218 dev_info_t *self,
219 219 dev_info_t *child,
220 220 ddi_ctl_enum_t op,
221 221 void *arg,
222 222 void *result);
223 223
224 224 static int scsi_hba_map_fault(
225 225 dev_info_t *self,
226 226 dev_info_t *child,
227 227 struct hat *hat,
228 228 struct seg *seg,
229 229 caddr_t addr,
230 230 struct devpage *dp,
231 231 pfn_t pfn,
232 232 uint_t prot,
233 233 uint_t lock);
234 234
235 235 static int scsi_hba_get_eventcookie(
236 236 dev_info_t *self,
237 237 dev_info_t *child,
238 238 char *name,
239 239 ddi_eventcookie_t *eventp);
240 240
241 241 static int scsi_hba_add_eventcall(
242 242 dev_info_t *self,
243 243 dev_info_t *child,
244 244 ddi_eventcookie_t event,
245 245 void (*callback)(
246 246 dev_info_t *dip,
247 247 ddi_eventcookie_t event,
248 248 void *arg,
249 249 void *bus_impldata),
250 250 void *arg,
251 251 ddi_callback_id_t *cb_id);
252 252
253 253 static int scsi_hba_remove_eventcall(
254 254 dev_info_t *self,
255 255 ddi_callback_id_t id);
256 256
257 257 static int scsi_hba_post_event(
258 258 dev_info_t *self,
259 259 dev_info_t *child,
260 260 ddi_eventcookie_t event,
261 261 void *bus_impldata);
262 262
263 263 static int scsi_hba_bus_config(
264 264 dev_info_t *self,
265 265 uint_t flags,
266 266 ddi_bus_config_op_t op,
267 267 void *arg,
268 268 dev_info_t **childp);
269 269
270 270 static int scsi_hba_bus_unconfig(
271 271 dev_info_t *self,
272 272 uint_t flags,
273 273 ddi_bus_config_op_t op,
274 274 void *arg);
275 275
276 276 static int scsi_hba_fm_init_child(
277 277 dev_info_t *self,
278 278 dev_info_t *child,
279 279 int cap,
280 280 ddi_iblock_cookie_t *ibc);
281 281
282 282 static int scsi_hba_bus_power(
283 283 dev_info_t *self,
284 284 void *impl_arg,
285 285 pm_bus_power_op_t op,
286 286 void *arg,
287 287 void *result);
288 288
289 289 /* bus_ops vector for SCSI HBA's. */
290 290 static struct bus_ops scsi_hba_busops = {
291 291 BUSO_REV,
292 292 nullbusmap, /* bus_map */
293 293 NULL, /* bus_get_intrspec */
294 294 NULL, /* bus_add_intrspec */
295 295 NULL, /* bus_remove_intrspec */
296 296 scsi_hba_map_fault, /* bus_map_fault */
297 297 NULL, /* bus_dma_map */
298 298 ddi_dma_allochdl, /* bus_dma_allochdl */
299 299 ddi_dma_freehdl, /* bus_dma_freehdl */
300 300 ddi_dma_bindhdl, /* bus_dma_bindhdl */
301 301 ddi_dma_unbindhdl, /* bus_unbindhdl */
302 302 ddi_dma_flush, /* bus_dma_flush */
303 303 ddi_dma_win, /* bus_dma_win */
304 304 ddi_dma_mctl, /* bus_dma_ctl */
305 305 scsi_hba_bus_ctl, /* bus_ctl */
306 306 ddi_bus_prop_op, /* bus_prop_op */
307 307 scsi_hba_get_eventcookie, /* bus_get_eventcookie */
308 308 scsi_hba_add_eventcall, /* bus_add_eventcall */
309 309 scsi_hba_remove_eventcall, /* bus_remove_eventcall */
310 310 scsi_hba_post_event, /* bus_post_event */
311 311 NULL, /* bus_intr_ctl */
312 312 scsi_hba_bus_config, /* bus_config */
313 313 scsi_hba_bus_unconfig, /* bus_unconfig */
314 314 scsi_hba_fm_init_child, /* bus_fm_init */
315 315 NULL, /* bus_fm_fini */
316 316 NULL, /* bus_fm_access_enter */
317 317 NULL, /* bus_fm_access_exit */
318 318 scsi_hba_bus_power /* bus_power */
319 319 };
320 320
321 321 /* cb_ops for hotplug :devctl and :scsi support */
322 322 static struct cb_ops scsi_hba_cbops = {
323 323 scsi_hba_open,
324 324 scsi_hba_close,
325 325 nodev, /* strategy */
326 326 nodev, /* print */
327 327 nodev, /* dump */
328 328 nodev, /* read */
329 329 nodev, /* write */
330 330 scsi_hba_ioctl, /* ioctl */
331 331 nodev, /* devmap */
332 332 nodev, /* mmap */
333 333 nodev, /* segmap */
334 334 nochpoll, /* poll */
335 335 ddi_prop_op, /* prop_op */
336 336 NULL, /* stream */
337 337 D_NEW|D_MP|D_HOTPLUG, /* cb_flag */
338 338 CB_REV, /* rev */
339 339 nodev, /* int (*cb_aread)() */
340 340 nodev /* int (*cb_awrite)() */
341 341 };
342 342
343 343 /* Prototypes for static scsi_hba.c/SCSA private lunmap interfaces */
344 344 static int scsi_lunmap_create(
345 345 dev_info_t *self,
346 346 impl_scsi_tgtmap_t *tgtmap,
347 347 char *tgt_addr);
348 348 static void scsi_lunmap_destroy(
349 349 dev_info_t *self,
350 350 impl_scsi_tgtmap_t *tgtmap,
351 351 char *tgt_addr);
352 352 static void scsi_lunmap_set_begin(
353 353 dev_info_t *self,
354 354 damap_t *lundam);
355 355 static int scsi_lunmap_set_add(
356 356 dev_info_t *self,
357 357 damap_t *lundam,
358 358 char *taddr,
359 359 scsi_lun64_t lun_num,
360 360 int lun_sfunc);
361 361 static void scsi_lunmap_set_end(
362 362 dev_info_t *self,
363 363 damap_t *lundam);
364 364
365 365 /* Prototypes for static misc. scsi_hba.c private bus_config interfaces */
366 366 static int scsi_hba_bus_config_iports(dev_info_t *self, uint_t flags,
367 367 ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
368 368 static int scsi_hba_bus_config_spi(dev_info_t *self, uint_t flags,
369 369 ddi_bus_config_op_t op, void *arg, dev_info_t **childp);
370 370 static dev_info_t *scsi_hba_bus_config_port(dev_info_t *self,
371 371 char *nameaddr, scsi_enum_t se);
372 372
373 373 #ifdef sparc
374 374 static int scsi_hba_bus_config_prom_node(dev_info_t *self, uint_t flags,
375 375 void *arg, dev_info_t **childp);
376 376 #endif /* sparc */
377 377
378 378
379 379 /*
380 380 * SCSI_HBA_LOG is used for all messages. A logging level is specified when
381 381 * generating a message. Some levels correspond directly to cmn_err levels,
382 382 * some are associated with increasing levels diagnostic/debug output (LOG1-4),
383 383 * and others are associated with specific levels of interface (LOGMAP).
384 384 * For _LOG() messages, a __func__ prefix will identify the function origin
385 385 * of the message. For _LOG_NF messages, there is no function prefix or
386 386 * self/child context. Filtering of messages is provided based on logging
387 387 * level, but messages with cmn_err logging level and messages generated
388 388 * generated with _LOG_NF() are never filtered.
389 389 *
390 390 * For debugging, more complete information can be displayed with each message
391 391 * (full device path and pointer values) by adjusting scsi_hba_log_info.
392 392 */
393 393 /* logging levels */
394 394 #define SCSI_HBA_LOGCONT CE_CONT
395 395 #define SCSI_HBA_LOGNOTE CE_NOTE
396 396 #define SCSI_HBA_LOGWARN CE_WARN
397 397 #define SCSI_HBA_LOGPANIC CE_PANIC
398 398 #define SCSI_HBA_LOGIGNORE CE_IGNORE
399 399 #define SCSI_HBA_LOG_CE_MASK 0x0000000F /* no filter for these levels */
400 400 #define SCSI_HBA_LOG1 0x00000010 /* DIAG1 level enable */
401 401 #define SCSI_HBA_LOG2 0x00000020 /* DIAG2 level enable */
402 402 #define SCSI_HBA_LOG3 0x00000040 /* DIAG3 level enable */
403 403 #define SCSI_HBA_LOG4 0x00000080 /* DIAG4 level enable */
404 404 #define SCSI_HBA_LOGMAPPHY 0x00000100 /* MAPPHY level enable */
405 405 #define SCSI_HBA_LOGMAPIPT 0x00000200 /* MAPIPT level enable */
406 406 #define SCSI_HBA_LOGMAPTGT 0x00000400 /* MAPTGT level enable */
407 407 #define SCSI_HBA_LOGMAPLUN 0x00000800 /* MAPLUN level enable */
408 408 #define SCSI_HBA_LOGMAPCFG 0x00001000 /* MAPCFG level enable */
409 409 #define SCSI_HBA_LOGMAPUNCFG 0x00002000 /* MAPUNCFG level enable */
410 410 #define SCSI_HBA_LOGTRACE 0x00010000 /* TRACE enable */
411 411 #if (CE_CONT | CE_NOTE | CE_WARN | CE_PANIC | CE_IGNORE) > SCSI_HBA_LOG_CE_MASK
412 412 Error, problem with CE_ definitions
413 413 #endif
414 414
415 415 /*
416 416 * Tunable log message augmentation and filters: filters do not apply to
417 417 * SCSI_HBA_LOG_CE_MASK level messages or LOG_NF() messages.
418 418 *
419 419 * An example set of /etc/system tunings to simplify debug a SCSA pHCI HBA
420 420 * driver called "pmcs", including "scsi_vhci" operation, by capturing
421 421 * log information in the system log might be:
422 422 *
423 423 * echo "set scsi:scsi_hba_log_filter_level=0x3ff0" >> /etc/system
424 424 * echo "set scsi:scsi_hba_log_filter_phci=\"pmcs\"" >> /etc/system
425 425 * echo "set scsi:scsi_hba_log_filter_vhci=\"scsi_vhci\"" >> /etc/system
426 426 *
427 427 * To capture information on just HBA-SCSAv3 *map operation, use
428 428 * echo "set scsi:scsi_hba_log_filter_level=0x3f10" >> /etc/system
429 429 *
430 430 * For debugging an HBA driver, you may also want to set:
431 431 *
432 432 * echo "set scsi:scsi_hba_log_align=1" >> /etc/system
433 433 * echo "set scsi:scsi_hba_log_mt_disable=0x6" >> /etc/system
434 434 * echo "set mtc_off=1" >> /etc/system
435 435 * echo "set mdi_mtc_off=1" >> /etc/system
436 436 * echo "set scsi:scsi_hba_log_fcif=0" >> /etc/system
437 437 */
438 438 int scsi_hba_log_filter_level =
439 439 SCSI_HBA_LOG1 |
440 440 0;
441 441 char *scsi_hba_log_filter_phci = "\0\0\0\0\0\0\0\0\0\0\0\0";
442 442 char *scsi_hba_log_filter_vhci = "\0\0\0\0\0\0\0\0\0\0\0\0";
443 443 int scsi_hba_log_align = 0; /* NOTE: will not cause truncation */
444 444 int scsi_hba_log_fcif = '!'; /* "^!?" first char in format */
445 445 /* NOTE: iff level > SCSI_HBA_LOG1 */
446 446 /* '\0'0x00 -> console and system log */
447 447 /* '^' 0x5e -> console_only */
448 448 /* '!' 0x21 -> system log only */
449 449 /* '?' 0x2F -> See cmn_err(9F) */
450 450 int scsi_hba_log_info = /* augmentation: extra info output */
451 451 (0 << 0) | /* 0x0001: process information */
452 452 (0 << 1) | /* 0x0002: full /devices path */
453 453 (0 << 2); /* 0x0004: devinfo pointer */
454 454
455 455 int scsi_hba_log_mt_disable =
456 456 /* SCSI_ENUMERATION_MT_LUN_DISABLE | (ie 0x02) */
457 457 /* SCSI_ENUMERATION_MT_TARGET_DISABLE | (ie 0x04) */
458 458 0;
459 459
460 460 /* static data for HBA logging subsystem */
461 461 static kmutex_t scsi_hba_log_mutex;
462 462 static char scsi_hba_log_i[512];
463 463 static char scsi_hba_log_buf[512];
464 464 static char scsi_hba_fmt[512];
465 465
466 466 /* Macros to use in scsi_hba.c source code below */
467 467 #define SCSI_HBA_LOG(x) scsi_hba_log x
468 468 #define _LOG(level) SCSI_HBA_LOG##level, __func__
469 469 #define _MAP(map) SCSI_HBA_LOGMAP##map, __func__
470 470 #define _LOG_NF(level) SCSI_HBA_LOG##level, NULL, NULL, NULL
471 471 #define _LOG_TRACE _LOG(TRACE)
472 472 #define _LOGLUN _MAP(LUN)
473 473 #define _LOGTGT _MAP(TGT)
474 474 #define _LOGIPT _MAP(IPT)
475 475 #define _LOGPHY _MAP(PHY)
476 476 #define _LOGCFG _MAP(CFG)
477 477 #define _LOGUNCFG _MAP(UNCFG)
478 478
479 479 /*PRINTFLIKE5*/
480 480 static void
481 481 scsi_hba_log(int level, const char *func, dev_info_t *self, dev_info_t *child,
482 482 const char *fmt, ...)
483 483 {
484 484 va_list ap;
485 485 int clevel;
486 486 int align;
487 487 char *info;
488 488 char *f;
489 489 char *ua;
490 490
491 491 /* derive self from child's parent */
492 492 if ((self == NULL) && child)
493 493 self = ddi_get_parent(child);
494 494
495 495 /* no filtering of SCSI_HBA_LOG_CE_MASK or LOG_NF messages */
496 496 if (((level & SCSI_HBA_LOG_CE_MASK) != level) && (func != NULL)) {
497 497 /* scsi_hba_log_filter_level: filter on level as bitmask */
498 498 if ((level & scsi_hba_log_filter_level) == 0)
499 499 return;
500 500
501 501 /* scsi_hba_log_filter_phci/vhci: on name of driver */
502 502 if (*scsi_hba_log_filter_phci &&
503 503 ((self == NULL) ||
504 504 (ddi_driver_name(self) == NULL) ||
505 505 strcmp(ddi_driver_name(self), scsi_hba_log_filter_phci))) {
506 506 /* does not match pHCI, check vHCI */
507 507 if (*scsi_hba_log_filter_vhci &&
508 508 ((self == NULL) ||
509 509 (ddi_driver_name(self) == NULL) ||
510 510 strcmp(ddi_driver_name(self),
511 511 scsi_hba_log_filter_vhci))) {
512 512 /* does not match vHCI */
513 513 return;
514 514 }
515 515 }
516 516
517 517
518 518 /* passed filters, determine align */
519 519 align = scsi_hba_log_align;
520 520
521 521 /* shorten func for filtered output */
522 522 if (strncmp(func, "scsi_hba_", 9) == 0)
523 523 func += 9;
524 524 if (strncmp(func, "scsi_", 5) == 0)
525 525 func += 5;
526 526 } else {
527 527 /* don't align output that is never filtered */
528 528 align = 0;
529 529 }
530 530
531 531 /* determine the cmn_err form from the level */
532 532 clevel = ((level & SCSI_HBA_LOG_CE_MASK) == level) ? level : CE_CONT;
533 533
534 534 /* protect common buffers used to format output */
535 535 mutex_enter(&scsi_hba_log_mutex);
536 536
537 537 /* skip special first characters, we add them back below */
538 538 f = (char *)fmt;
539 539 if (*f && strchr("^!?", *f))
540 540 f++;
541 541 va_start(ap, fmt);
542 542 (void) vsprintf(scsi_hba_log_buf, f, ap);
543 543 va_end(ap);
544 544
545 545 /* augment message with 'information' */
546 546 info = scsi_hba_log_i;
547 547 *info = '\0';
548 548 if ((scsi_hba_log_info & 0x0001) && curproc && PTOU(curproc)->u_comm) {
549 549 (void) sprintf(info, "%s[%d]%p ",
550 550 PTOU(curproc)->u_comm, curproc->p_pid, (void *)curthread);
551 551 info += strlen(info);
552 552 }
553 553 if (self) {
554 554 if ((scsi_hba_log_info & 0x0004) && (child || self)) {
555 555 (void) sprintf(info, "%p ",
556 556 (void *)(child ? child : self));
557 557 info += strlen(info);
558 558 }
559 559 if (scsi_hba_log_info & 0x0002) {
560 560 (void) ddi_pathname(child ? child : self, info);
561 561 (void) strcat(info, " ");
562 562 info += strlen(info);
563 563 }
564 564
565 565 /* always provide 'default' information about self &child */
566 566 (void) sprintf(info, "%s%d ", ddi_driver_name(self),
567 567 ddi_get_instance(self));
568 568 info += strlen(info);
569 569 if (child) {
570 570 ua = ddi_get_name_addr(child);
571 571 (void) sprintf(info, "%s@%s ",
572 572 ddi_node_name(child), (ua && *ua) ? ua : "");
573 573 info += strlen(info);
574 574 }
575 575 }
576 576
577 577 /* turn off alignment if truncation would occur */
578 578 if (align && ((strlen(func) > 18) || (strlen(scsi_hba_log_i) > 36)))
579 579 align = 0;
580 580
581 581 /* adjust for aligned output */
582 582 if (align) {
583 583 if (func == NULL)
584 584 func = "";
585 585 /* remove trailing blank with align output */
586 586 if ((info != scsi_hba_log_i) && (*(info -1) == '\b'))
587 587 *(info - 1) = '\0';
588 588 }
589 589
590 590 /* special "first character in format" must be in format itself */
591 591 f = scsi_hba_fmt;
592 592 if (fmt[0] && strchr("^!?", fmt[0]))
593 593 *f++ = fmt[0];
594 594 else if (scsi_hba_log_fcif && (level > SCSI_HBA_LOG1))
595 595 *f++ = (char)scsi_hba_log_fcif; /* add global fcif */
596 596 if (align)
597 597 (void) sprintf(f, "%s", "%-18.18s: %36.36s: %s%s");
598 598 else
599 599 (void) sprintf(f, "%s", func ? "%s: %s%s%s" : "%s%s%s");
600 600
601 601 if (func)
602 602 cmn_err(clevel, scsi_hba_fmt, func, scsi_hba_log_i,
603 603 scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
604 604 else
605 605 cmn_err(clevel, scsi_hba_fmt, scsi_hba_log_i,
606 606 scsi_hba_log_buf, clevel == CE_CONT ? "\n" : "");
607 607 mutex_exit(&scsi_hba_log_mutex);
608 608 }
609 609
610 610 int scsi_enumeration_failed_panic = 0;
611 611 int scsi_enumeration_failed_hotplug = 1;
612 612
613 613 static void
614 614 scsi_enumeration_failed(dev_info_t *child, scsi_enum_t se,
615 615 char *arg, char *when)
616 616 {
617 617 /* If 'se' is -1 the 'se' value comes from child. */
618 618 if (se == -1) {
619 619 ASSERT(child);
620 620 se = ndi_dev_is_hotplug_node(child) ? SE_HP : SE_BUSCONFIG;
621 621 }
622 622
623 623 if (scsi_enumeration_failed_panic) {
624 624 /* set scsi_enumeration_failed_panic to debug */
625 625 SCSI_HBA_LOG((_LOG(PANIC), NULL, child,
626 626 "%s%senumeration failed during %s",
627 627 arg ? arg : "", arg ? " " : "", when));
628 628 } else if (scsi_enumeration_failed_hotplug && (se == SE_HP)) {
629 629 /* set scsi_enumeration_failed_hotplug for console messages */
630 630 SCSI_HBA_LOG((_LOG(WARN), NULL, child,
631 631 "%s%senumeration failed during %s",
632 632 arg ? arg : "", arg ? " " : "", when));
633 633 } else {
634 634 /* default */
635 635 SCSI_HBA_LOG((_LOG(2), NULL, child,
636 636 "%s%senumeration failed during %s",
637 637 arg ? arg : "", arg ? " " : "", when));
638 638 }
639 639 }
640 640
641 641 /*
642 642 * scsi_hba version of [nm]di_devi_enter/[nm]di_devi_exit that detects if HBA
643 643 * is a PHCI, and chooses mdi/ndi locking implementation.
644 644 */
645 645 static void
646 646 scsi_hba_devi_enter(dev_info_t *self, int *circp)
647 647 {
648 648 if (MDI_PHCI(self))
649 649 mdi_devi_enter(self, circp);
650 650 else
651 651 ndi_devi_enter(self, circp);
652 652 }
653 653
654 654 static int
655 655 scsi_hba_devi_tryenter(dev_info_t *self, int *circp)
656 656 {
657 657 if (MDI_PHCI(self))
658 658 return (mdi_devi_tryenter(self, circp));
659 659 else
660 660 return (ndi_devi_tryenter(self, circp));
661 661 }
662 662
663 663 static void
664 664 scsi_hba_devi_exit(dev_info_t *self, int circ)
665 665 {
666 666 if (MDI_PHCI(self))
667 667 mdi_devi_exit(self, circ);
668 668 else
669 669 ndi_devi_exit(self, circ);
670 670 }
671 671
672 672 static void
673 673 scsi_hba_devi_enter_phci(dev_info_t *self, int *circp)
674 674 {
675 675 if (MDI_PHCI(self))
676 676 mdi_devi_enter_phci(self, circp);
677 677 }
678 678
679 679 static void
680 680 scsi_hba_devi_exit_phci(dev_info_t *self, int circ)
681 681 {
682 682 if (MDI_PHCI(self))
683 683 mdi_devi_exit_phci(self, circ);
684 684 }
685 685
686 686 static int
687 687 scsi_hba_dev_is_sid(dev_info_t *child)
688 688 {
689 689 /*
690 690 * Use ndi_dev_is_persistent_node instead of ddi_dev_is_sid to avoid
691 691 * any possible locking issues in mixed nexus devctl code (like usb).
692 692 */
693 693 return (ndi_dev_is_persistent_node(child));
694 694 }
695 695
696 696 /*
697 697 * Called from _init() when loading "scsi" module
698 698 */
699 699 void
700 700 scsi_initialize_hba_interface()
701 701 {
702 702 SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
703 703
704 704 /* We need "scsiprobe" and "scsinodev" as an alias or a driver. */
705 705 if (ddi_name_to_major(compatible_probe) == DDI_MAJOR_T_NONE) {
706 706 SCSI_HBA_LOG((_LOG_NF(WARN), "failed to resolve '%s' "
707 707 "driver alias, defaulting to 'nulldriver'",
708 708 compatible_probe));
709 709
710 710 /* If no "nulldriver" driver nothing will work... */
711 711 compatible_probe = "nulldriver";
712 712 if (ddi_name_to_major(compatible_probe) == DDI_MAJOR_T_NONE)
713 713 SCSI_HBA_LOG((_LOG_NF(WARN), "no probe '%s' driver, "
714 714 "system misconfigured", compatible_probe));
715 715 }
716 716 if (ddi_name_to_major(compatible_nodev) == DDI_MAJOR_T_NONE) {
717 717 SCSI_HBA_LOG((_LOG_NF(WARN), "failed to resolve '%s' "
718 718 "driver alias, defaulting to 'nulldriver'",
719 719 compatible_nodev));
720 720
721 721 /* If no "nulldriver" driver nothing will work... */
722 722 compatible_nodev = "nulldriver";
723 723 if (ddi_name_to_major(compatible_nodev) == DDI_MAJOR_T_NONE)
724 724 SCSI_HBA_LOG((_LOG_NF(WARN), "no nodev '%s' driver, "
725 725 "system misconfigured", compatible_nodev));
726 726 }
727 727
728 728 /*
729 729 * Verify our special node name "probe" will not be used in other ways.
730 730 * Don't expect things to work if they are.
731 731 */
732 732 if (ddi_major_to_name(ddi_name_to_major("probe")))
733 733 SCSI_HBA_LOG((_LOG_NF(WARN),
734 734 "driver already using special node name 'probe'"));
735 735
736 736 mutex_init(&scsi_log_mutex, NULL, MUTEX_DRIVER, NULL);
737 737 mutex_init(&scsi_flag_nointr_mutex, NULL, MUTEX_DRIVER, NULL);
738 738 cv_init(&scsi_flag_nointr_cv, NULL, CV_DRIVER, NULL);
739 739 mutex_init(&scsi_hba_log_mutex, NULL, MUTEX_DRIVER, NULL);
740 740
741 741 /* initialize the asynchronous barrier deletion daemon */
742 742 mutex_init(&scsi_hba_barrier_mutex, NULL, MUTEX_DRIVER, NULL);
743 743 cv_init(&scsi_hba_barrier_cv, NULL, CV_DRIVER, NULL);
744 744 (void) thread_create(NULL, 0,
745 745 (void (*)())scsi_hba_barrier_daemon, NULL,
746 746 0, &p0, TS_RUN, minclsyspri);
747 747
748 748 /* initialize lun change ASC/ASCQ processing daemon (stage1 & stage2) */
749 749 mutex_init(&scsi_lunchg1_mutex, NULL, MUTEX_DRIVER, NULL);
750 750 cv_init(&scsi_lunchg1_cv, NULL, CV_DRIVER, NULL);
751 751 (void) thread_create(NULL, 0,
752 752 (void (*)())scsi_lunchg1_daemon, NULL,
753 753 0, &p0, TS_RUN, minclsyspri);
754 754 mutex_init(&scsi_lunchg2_mutex, NULL, MUTEX_DRIVER, NULL);
755 755 cv_init(&scsi_lunchg2_cv, NULL, CV_DRIVER, NULL);
756 756 (void) thread_create(NULL, 0,
757 757 (void (*)())scsi_lunchg2_daemon, NULL,
758 758 0, &p0, TS_RUN, minclsyspri);
759 759 }
760 760
761 761 int
762 762 scsi_hba_pkt_constructor(void *buf, void *arg, int kmflag)
763 763 {
764 764 struct scsi_pkt_cache_wrapper *pktw;
765 765 struct scsi_pkt *pkt;
766 766 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
767 767 int pkt_len;
768 768 char *ptr;
769 769
770 770 /*
771 771 * allocate a chunk of memory for the following:
772 772 * scsi_pkt
773 773 * pcw_* fields
774 774 * pkt_ha_private
775 775 * pkt_cdbp, if needed
776 776 * (pkt_private always null)
777 777 * pkt_scbp, if needed
778 778 */
779 779 pkt_len = tran->tran_hba_len + sizeof (struct scsi_pkt_cache_wrapper);
780 780 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
781 781 pkt_len += DEFAULT_CDBLEN;
782 782 if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
783 783 pkt_len += DEFAULT_SCBLEN;
784 784 bzero(buf, pkt_len);
785 785
786 786 ptr = buf;
787 787 pktw = buf;
788 788 ptr += sizeof (struct scsi_pkt_cache_wrapper);
789 789 pkt = &(pktw->pcw_pkt);
790 790 pkt->pkt_ha_private = (opaque_t)ptr;
791 791
792 792 pktw->pcw_magic = PKT_WRAPPER_MAGIC; /* alloced correctly */
793 793 /*
794 794 * keep track of the granularity at the time this handle was
795 795 * allocated
796 796 */
797 797 pktw->pcw_granular = tran->tran_dma_attr.dma_attr_granular;
798 798
799 799 if (ddi_dma_alloc_handle(tran->tran_hba_dip, &tran->tran_dma_attr,
800 800 kmflag == KM_SLEEP ? SLEEP_FUNC: NULL_FUNC, NULL,
801 801 &pkt->pkt_handle) != DDI_SUCCESS) {
802 802
803 803 return (-1);
804 804 }
805 805 ptr += tran->tran_hba_len;
806 806 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) {
807 807 pkt->pkt_cdbp = (opaque_t)ptr;
808 808 ptr += DEFAULT_CDBLEN;
809 809 }
810 810 pkt->pkt_private = NULL;
811 811 if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
812 812 pkt->pkt_scbp = (opaque_t)ptr;
813 813 if (tran->tran_pkt_constructor)
814 814 return ((*tran->tran_pkt_constructor)(pkt, arg, kmflag));
815 815 else
816 816 return (0);
817 817 }
818 818
819 819 #define P_TO_TRAN(pkt) ((pkt)->pkt_address.a_hba_tran)
820 820
821 821 void
822 822 scsi_hba_pkt_destructor(void *buf, void *arg)
823 823 {
824 824 struct scsi_pkt_cache_wrapper *pktw = buf;
825 825 struct scsi_pkt *pkt = &(pktw->pcw_pkt);
826 826 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
827 827
828 828 ASSERT(pktw->pcw_magic == PKT_WRAPPER_MAGIC);
829 829 ASSERT((pktw->pcw_flags & PCW_BOUND) == 0);
830 830 if (tran->tran_pkt_destructor)
831 831 (*tran->tran_pkt_destructor)(pkt, arg);
832 832
833 833 /* make sure nobody messed with our pointers */
834 834 ASSERT(pkt->pkt_ha_private == (opaque_t)((char *)pkt +
835 835 sizeof (struct scsi_pkt_cache_wrapper)));
836 836 ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_SCB) == 0) ||
837 837 (pkt->pkt_scbp == (opaque_t)((char *)pkt +
838 838 tran->tran_hba_len +
839 839 (((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ?
840 840 0 : DEFAULT_CDBLEN) +
841 841 DEFAULT_PRIVLEN + sizeof (struct scsi_pkt_cache_wrapper))));
842 842 ASSERT(((tran->tran_hba_flags & SCSI_HBA_TRAN_CDB) == 0) ||
843 843 (pkt->pkt_cdbp == (opaque_t)((char *)pkt +
844 844 tran->tran_hba_len +
845 845 sizeof (struct scsi_pkt_cache_wrapper))));
846 846 ASSERT(pkt->pkt_handle);
847 847 ddi_dma_free_handle(&pkt->pkt_handle);
848 848 pkt->pkt_handle = NULL;
849 849 pkt->pkt_numcookies = 0;
850 850 pktw->pcw_total_xfer = 0;
851 851 pktw->pcw_totalwin = 0;
852 852 pktw->pcw_curwin = 0;
853 853 }
854 854
855 855 /*
856 856 * Called by an HBA from _init() to plumb in common SCSA bus_ops and
857 857 * cb_ops for the HBA's :devctl and :scsi minor nodes.
858 858 */
859 859 int
860 860 scsi_hba_init(struct modlinkage *modlp)
861 861 {
862 862 struct dev_ops *hba_dev_ops;
863 863
864 864 SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
865 865
866 866 /*
867 867 * Get a pointer to the dev_ops structure of the HBA and plumb our
868 868 * bus_ops vector into the HBA's dev_ops structure.
869 869 */
870 870 hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
871 871 ASSERT(hba_dev_ops->devo_bus_ops == NULL);
872 872 hba_dev_ops->devo_bus_ops = &scsi_hba_busops;
873 873
874 874 /*
875 875 * Plumb our cb_ops vector into the HBA's dev_ops structure to
876 876 * provide getinfo and hotplugging ioctl support if the HBA driver
877 877 * does not already provide this support.
878 878 */
879 879 if (hba_dev_ops->devo_cb_ops == NULL) {
880 880 hba_dev_ops->devo_cb_ops = &scsi_hba_cbops;
881 881 }
882 882 if (hba_dev_ops->devo_cb_ops->cb_open == scsi_hba_open) {
883 883 ASSERT(hba_dev_ops->devo_cb_ops->cb_close == scsi_hba_close);
884 884 hba_dev_ops->devo_getinfo = scsi_hba_info;
885 885 }
886 886 return (0);
887 887 }
888 888
889 889 /*
890 890 * Called by an HBA attach(9E) to allocate a scsi_hba_tran(9S) structure. An
891 891 * HBA driver will then initialize the structure and then call
892 892 * scsi_hba_attach_setup(9F).
893 893 */
894 894 /*ARGSUSED*/
895 895 scsi_hba_tran_t *
896 896 scsi_hba_tran_alloc(
897 897 dev_info_t *self,
898 898 int flags)
899 899 {
900 900 scsi_hba_tran_t *tran;
901 901
902 902 SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
903 903
904 904 /* allocate SCSA flavors for self */
905 905 ndi_flavorv_alloc(self, SCSA_NFLAVORS);
906 906
907 907 tran = kmem_zalloc(sizeof (scsi_hba_tran_t),
908 908 (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
909 909
910 910 if (tran) {
911 911 tran->tran_interconnect_type = INTERCONNECT_PARALLEL;
912 912
913 913 /*
914 914 * HBA driver called scsi_hba_tran_alloc(), so tran structure
915 915 * is proper size and unused/newer fields are zero.
916 916 *
917 917 * NOTE: We use SCSA_HBA_SCSA_TA as an obtuse form of
918 918 * versioning to detect old HBA drivers that do not use
919 919 * scsi_hba_tran_alloc, and would present garbage data
920 920 * (instead of valid/zero data) for newer tran fields.
921 921 */
922 922 tran->tran_hba_flags |= SCSI_HBA_SCSA_TA;
923 923 }
924 924
925 925 return (tran);
926 926 }
927 927
928 928 /*
929 929 * Called by an HBA to free a scsi_hba_tran structure
930 930 */
931 931 void
932 932 scsi_hba_tran_free(
933 933 scsi_hba_tran_t *tran)
934 934 {
935 935 SCSI_HBA_LOG((_LOG_TRACE, tran->tran_hba_dip, NULL, __func__));
936 936
937 937 kmem_free(tran, sizeof (scsi_hba_tran_t));
938 938 }
939 939
940 940 int
941 941 scsi_tran_ext_alloc(
942 942 scsi_hba_tran_t *tran,
943 943 size_t length,
944 944 int flags)
945 945 {
946 946 void *tran_ext;
947 947 int ret = DDI_FAILURE;
948 948
949 949 tran_ext = kmem_zalloc(length,
950 950 (flags & SCSI_HBA_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP);
951 951 if (tran_ext != NULL) {
952 952 tran->tran_extension = tran_ext;
953 953 ret = DDI_SUCCESS;
954 954 }
955 955 return (ret);
956 956 }
957 957
958 958 void
↓ open down ↓ |
924 lines elided |
↑ open up ↑ |
959 959 scsi_tran_ext_free(
960 960 scsi_hba_tran_t *tran,
961 961 size_t length)
962 962 {
963 963 if (tran->tran_extension != NULL) {
964 964 kmem_free(tran->tran_extension, length);
965 965 tran->tran_extension = NULL;
966 966 }
967 967 }
968 968
969 -/*
970 - * Obsolete: Called by an HBA to attach an instance of the driver
971 - * Implement this older interface in terms of the new.
972 - */
973 -/*ARGSUSED*/
974 -int
975 -scsi_hba_attach(
976 - dev_info_t *self,
977 - ddi_dma_lim_t *hba_lim,
978 - scsi_hba_tran_t *tran,
979 - int flags,
980 - void *hba_options)
981 -{
982 - ddi_dma_attr_t hba_dma_attr;
983 -
984 - bzero(&hba_dma_attr, sizeof (ddi_dma_attr_t));
985 - hba_dma_attr.dma_attr_burstsizes = hba_lim->dlim_burstsizes;
986 - hba_dma_attr.dma_attr_minxfer = hba_lim->dlim_minxfer;
987 -
988 - return (scsi_hba_attach_setup(self, &hba_dma_attr, tran, flags));
989 -}
990 -
991 969 /*
992 970 * Common nexus teardown code: used by both scsi_hba_detach() on SCSA HBA node
993 971 * and iport_postdetach_tran_scsi_device() on a SCSA HBA iport node (and for
994 972 * failure cleanup). Undo scsa_nexus_setup in reverse order.
995 973 *
996 974 * NOTE: Since we are in the Solaris IO framework, we can depend on
997 975 * undocumented cleanup operations performed by other parts of the framework:
998 976 * like detach_node() calling ddi_prop_remove_all() and
999 977 * ddi_remove_minor_node(,NULL).
1000 978 */
1001 979 static void
1002 980 scsa_nexus_teardown(dev_info_t *self, scsi_hba_tran_t *tran)
1003 981 {
1004 982 /* Teardown FMA. */
1005 983 if (tran->tran_hba_flags & SCSI_HBA_SCSA_FM) {
1006 984 ddi_fm_fini(self);
1007 985 tran->tran_hba_flags &= ~SCSI_HBA_SCSA_FM;
1008 986 }
1009 987 }
1010 988
1011 989 /*
1012 990 * Common nexus setup code: used by both scsi_hba_attach_setup() on SCSA HBA
1013 991 * node and iport_preattach_tran_scsi_device() on a SCSA HBA iport node.
1014 992 *
1015 993 * This code makes no assumptions about tran use by scsi_device children.
1016 994 */
1017 995 static int
1018 996 scsa_nexus_setup(dev_info_t *self, scsi_hba_tran_t *tran)
1019 997 {
1020 998 int capable;
1021 999 int scsa_minor;
1022 1000
1023 1001 /*
1024 1002 * NOTE: SCSA maintains an 'fm-capable' domain, in tran_fm_capable,
1025 1003 * that is not dependent (limited by) the capabilities of its parents.
1026 1004 * For example a devinfo node in a branch that is not
1027 1005 * DDI_FM_EREPORT_CAPABLE may report as capable, via tran_fm_capable,
1028 1006 * to its scsi_device children.
1029 1007 *
1030 1008 * Get 'fm-capable' property from driver.conf, if present. If not
1031 1009 * present, default to the scsi_fm_capable global (which has
1032 1010 * DDI_FM_EREPORT_CAPABLE set by default).
1033 1011 */
1034 1012 if (tran->tran_fm_capable == DDI_FM_NOT_CAPABLE)
1035 1013 tran->tran_fm_capable = ddi_prop_get_int(DDI_DEV_T_ANY, self,
1036 1014 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1037 1015 "fm-capable", scsi_fm_capable);
1038 1016
1039 1017 /*
1040 1018 * If an HBA is *not* doing its own fma support by calling
1041 1019 * ddi_fm_init() prior to scsi_hba_attach_setup(), we provide a minimal
1042 1020 * common SCSA implementation so that scsi_device children can generate
1043 1021 * ereports via scsi_fm_ereport_post(). We use ddi_fm_capable() to
1044 1022 * detect an HBA calling ddi_fm_init() prior to scsi_hba_attach_setup().
1045 1023 */
1046 1024 if (tran->tran_fm_capable &&
1047 1025 (ddi_fm_capable(self) == DDI_FM_NOT_CAPABLE)) {
1048 1026 /*
1049 1027 * We are capable of something, pass our capabilities up the
1050 1028 * tree, but use a local variable so our parent can't limit
1051 1029 * our capabilities (we don't want our parent to clear
1052 1030 * DDI_FM_EREPORT_CAPABLE).
1053 1031 *
1054 1032 * NOTE: iblock cookies are not important because scsi HBAs
1055 1033 * always interrupt below LOCK_LEVEL.
1056 1034 */
1057 1035 capable = tran->tran_fm_capable;
1058 1036 ddi_fm_init(self, &capable, NULL);
1059 1037
1060 1038 /*
1061 1039 * Set SCSI_HBA_SCSA_FM bit to mark us as using the common
1062 1040 * minimal SCSA fm implementation - we called ddi_fm_init(),
1063 1041 * so we are responsible for calling ddi_fm_fini() in
1064 1042 * scsi_hba_detach().
1065 1043 *
1066 1044 * NOTE: if ddi_fm_init fails to establish handle, SKIP cleanup.
1067 1045 */
1068 1046 if (DEVI(self)->devi_fmhdl)
1069 1047 tran->tran_hba_flags |= SCSI_HBA_SCSA_FM;
1070 1048 }
1071 1049
1072 1050 /* If SCSA responsible for for minor nodes, create :devctl minor. */
1073 1051 scsa_minor = (ddi_get_driver(self)->devo_cb_ops->cb_open ==
1074 1052 scsi_hba_open) ? 1 : 0;
1075 1053 if (scsa_minor && ((ddi_create_minor_node(self, "devctl", S_IFCHR,
1076 1054 INST2DEVCTL(ddi_get_instance(self)), DDI_NT_SCSI_NEXUS, 0) !=
1077 1055 DDI_SUCCESS))) {
1078 1056 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1079 1057 "can't create :devctl minor node"));
1080 1058 goto fail;
1081 1059 }
1082 1060
1083 1061 return (DDI_SUCCESS);
1084 1062
1085 1063 fail: scsa_nexus_teardown(self, tran);
1086 1064 return (DDI_FAILURE);
1087 1065 }
1088 1066
1089 1067 /*
1090 1068 * Common tran teardown code: used by iport_postdetach_tran_scsi_device() on a
1091 1069 * SCSA HBA iport node and (possibly) by scsi_hba_detach() on SCSA HBA node
1092 1070 * (and for failure cleanup). Undo scsa_tran_setup in reverse order.
1093 1071 *
1094 1072 * NOTE: Since we are in the Solaris IO framework, we can depend on
1095 1073 * undocumented cleanup operations performed by other parts of the framework:
1096 1074 * like detach_node() calling ddi_prop_remove_all() and
1097 1075 * ddi_remove_minor_node(,NULL).
1098 1076 */
1099 1077 static void
1100 1078 scsa_tran_teardown(dev_info_t *self, scsi_hba_tran_t *tran)
1101 1079 {
1102 1080 tran->tran_iport_dip = NULL;
1103 1081
1104 1082 /* Teardown pHCI registration */
1105 1083 if (tran->tran_hba_flags & SCSI_HBA_SCSA_PHCI) {
1106 1084 (void) mdi_phci_unregister(self, 0);
1107 1085 tran->tran_hba_flags &= ~SCSI_HBA_SCSA_PHCI;
1108 1086 }
1109 1087 }
1110 1088
1111 1089 /*
1112 1090 * Common tran setup code: used by iport_preattach_tran_scsi_device() on a
1113 1091 * SCSA HBA iport node and (possibly) by scsi_hba_attach_setup() on SCSA HBA
1114 1092 * node.
1115 1093 */
1116 1094 static int
1117 1095 scsa_tran_setup(dev_info_t *self, scsi_hba_tran_t *tran)
1118 1096 {
1119 1097 int scsa_minor;
1120 1098 int id;
1121 1099 char *scsi_binding_set;
1122 1100 static const char *interconnect[] = INTERCONNECT_TYPE_ASCII;
1123 1101
1124 1102 SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
1125 1103
1126 1104 /* If SCSA responsible for for minor nodes, create ":scsi" */
1127 1105 scsa_minor = (ddi_get_driver(self)->devo_cb_ops->cb_open ==
1128 1106 scsi_hba_open) ? 1 : 0;
1129 1107 if (scsa_minor && (ddi_create_minor_node(self, "scsi", S_IFCHR,
1130 1108 INST2SCSI(ddi_get_instance(self)),
1131 1109 DDI_NT_SCSI_ATTACHMENT_POINT, 0) != DDI_SUCCESS)) {
1132 1110 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1133 1111 "can't create :scsi minor node"));
1134 1112 goto fail;
1135 1113 }
1136 1114
1137 1115 /*
1138 1116 * If the property does not already exist on self then see if we can
1139 1117 * pull it from further up the tree and define it on self. If the
1140 1118 * property does not exist above (including options.conf) then use the
1141 1119 * default value specified (global variable). We pull things down from
1142 1120 * above for faster "DDI_PROP_NOTPROM | DDI_PROP_DONTPASS" runtime
1143 1121 * access.
1144 1122 *
1145 1123 * Future: Should we avoid creating properties when value == global?
1146 1124 */
1147 1125 #define CONFIG_INT_PROP(s, p, dv) { \
1148 1126 if ((ddi_prop_exists(DDI_DEV_T_ANY, s, \
1149 1127 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, p) == 0) && \
1150 1128 (ndi_prop_update_int(DDI_DEV_T_NONE, s, p, \
1151 1129 ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(s), \
1152 1130 DDI_PROP_NOTPROM, p, dv)) != DDI_PROP_SUCCESS)) \
1153 1131 SCSI_HBA_LOG((_LOG(WARN), NULL, s, \
1154 1132 "can't create property '%s'", p)); \
1155 1133 }
1156 1134
1157 1135 /* Decorate with scsi configuration properties */
1158 1136 CONFIG_INT_PROP(self, "scsi-enumeration", scsi_enumeration);
1159 1137 CONFIG_INT_PROP(self, "scsi-options", scsi_options);
1160 1138 CONFIG_INT_PROP(self, "scsi-reset-delay", scsi_reset_delay);
1161 1139 CONFIG_INT_PROP(self, "scsi-watchdog-tick", scsi_watchdog_tick);
1162 1140 CONFIG_INT_PROP(self, "scsi-selection-timeout", scsi_selection_timeout);
1163 1141 CONFIG_INT_PROP(self, "scsi-tag-age-limit", scsi_tag_age_limit);
1164 1142
1165 1143 /*
1166 1144 * Pull down the scsi-initiator-id from further up the tree, or as
1167 1145 * defined by OBP. Place on node for faster access. NOTE: there is
1168 1146 * some confusion about what the name of the property should be.
1169 1147 */
1170 1148 id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0, "initiator-id", -1);
1171 1149 if (id == -1)
1172 1150 id = ddi_prop_get_int(DDI_DEV_T_ANY, self, 0,
1173 1151 "scsi-initiator-id", -1);
1174 1152 if (id != -1)
1175 1153 CONFIG_INT_PROP(self, "scsi-initiator-id", id);
1176 1154
1177 1155 /*
1178 1156 * If we are responsible for tran allocation, establish
1179 1157 * 'initiator-interconnect-type'.
1180 1158 */
1181 1159 if ((tran->tran_hba_flags & SCSI_HBA_SCSA_TA) &&
1182 1160 (tran->tran_interconnect_type > 0) &&
1183 1161 (tran->tran_interconnect_type < INTERCONNECT_MAX)) {
1184 1162 if (ndi_prop_update_string(DDI_DEV_T_NONE, self,
1185 1163 "initiator-interconnect-type",
1186 1164 (char *)interconnect[tran->tran_interconnect_type])
1187 1165 != DDI_PROP_SUCCESS) {
1188 1166 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1189 1167 "failed to establish "
1190 1168 "'initiator-interconnect-type'"));
1191 1169 goto fail;
1192 1170 }
1193 1171 }
1194 1172
1195 1173 /*
1196 1174 * The 'scsi-binding-set' property can be defined in driver.conf
1197 1175 * files of legacy drivers on an as-needed basis. If 'scsi-binding-set'
1198 1176 * is not driver.conf defined, and the HBA is not implementing its own
1199 1177 * private bus_config, we define scsi-binding-set to the default
1200 1178 * 'spi' legacy value.
1201 1179 *
1202 1180 * NOTE: This default 'spi' value will be deleted if an HBA driver
1203 1181 * ends up using the scsi_hba_tgtmap_create() enumeration services.
1204 1182 *
1205 1183 * NOTE: If we were ever to decide to derive 'scsi-binding-set' from
1206 1184 * the IEEE-1275 'device_type' property then this is where that code
1207 1185 * should go - there is not enough consistency in 'device_type' to do
1208 1186 * this correctly at this point in time.
1209 1187 */
1210 1188 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
1211 1189 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-binding-set",
1212 1190 &scsi_binding_set) == DDI_PROP_SUCCESS) {
1213 1191 SCSI_HBA_LOG((_LOG(2), NULL, self,
1214 1192 "external 'scsi-binding-set' \"%s\"", scsi_binding_set));
1215 1193 ddi_prop_free(scsi_binding_set);
1216 1194 } else if (scsi_binding_set_spi &&
1217 1195 ((tran->tran_bus_config == NULL) ||
1218 1196 (tran->tran_bus_config == scsi_hba_bus_config_spi))) {
1219 1197 if (ndi_prop_update_string(DDI_DEV_T_NONE, self,
1220 1198 "scsi-binding-set", scsi_binding_set_spi) !=
1221 1199 DDI_PROP_SUCCESS) {
1222 1200 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1223 1201 "failed to establish 'scsi_binding_set' default"));
1224 1202 goto fail;
1225 1203 }
1226 1204 SCSI_HBA_LOG((_LOG(2), NULL, self,
1227 1205 "default 'scsi-binding-set' \"%s\"", scsi_binding_set_spi));
1228 1206 } else
1229 1207 SCSI_HBA_LOG((_LOG(2), NULL, self,
1230 1208 "no 'scsi-binding-set'"));
1231 1209
1232 1210 /*
1233 1211 * If SCSI_HBA_TRAN_PHCI is set, take care of pHCI registration of the
1234 1212 * initiator.
1235 1213 */
1236 1214 if ((tran->tran_hba_flags & SCSI_HBA_TRAN_PHCI) &&
1237 1215 (mdi_phci_register(MDI_HCI_CLASS_SCSI, self, 0) == MDI_SUCCESS))
1238 1216 tran->tran_hba_flags |= SCSI_HBA_SCSA_PHCI;
1239 1217
1240 1218 /* NOTE: tran_hba_dip is for DMA operation at the HBA node level */
1241 1219 tran->tran_iport_dip = self; /* for iport association */
1242 1220 return (DDI_SUCCESS);
1243 1221
1244 1222 fail: scsa_tran_teardown(self, tran);
1245 1223 return (DDI_FAILURE);
1246 1224 }
1247 1225
1248 1226 /*
1249 1227 * Called by a SCSA HBA driver to attach an instance of the driver to
1250 1228 * SCSA HBA node enumerated by PCI.
1251 1229 */
1252 1230 int
1253 1231 scsi_hba_attach_setup(
1254 1232 dev_info_t *self,
1255 1233 ddi_dma_attr_t *hba_dma_attr,
1256 1234 scsi_hba_tran_t *tran,
1257 1235 int flags)
1258 1236 {
1259 1237 int len;
1260 1238 char cache_name[96];
1261 1239
1262 1240 SCSI_HBA_LOG((_LOG_TRACE, self, NULL, __func__));
1263 1241
1264 1242 /*
1265 1243 * Verify that we are a driver so other code does not need to
1266 1244 * check for NULL ddi_get_driver() result.
1267 1245 */
1268 1246 if (ddi_get_driver(self) == NULL)
1269 1247 return (DDI_FAILURE);
1270 1248
1271 1249 /*
1272 1250 * Verify that we are called on a SCSA HBA node (function enumerated
1273 1251 * by PCI), not on an iport node.
1274 1252 */
1275 1253 ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1276 1254 if (scsi_hba_iport_unit_address(self))
1277 1255 return (DDI_FAILURE); /* self can't be an iport */
1278 1256
1279 1257 /* Caller must provide the tran. */
1280 1258 ASSERT(tran);
1281 1259 if (tran == NULL)
1282 1260 return (DDI_FAILURE);
1283 1261
1284 1262 /*
1285 1263 * Verify correct scsi_hba_tran_t form:
1286 1264 *
1287 1265 * o Both or none of tran_get_name/tran_get_addr.
1288 1266 * NOTE: Older SCSA HBA drivers for SCSI transports with addressing
1289 1267 * that did not fit the SPI "struct scsi_address" model were required
1290 1268 * to implement tran_get_name and tran_get_addr. This is no longer
1291 1269 * true - modern transport drivers should now use common SCSA
1292 1270 * enumeration services. The SCSA enumeration code will represent
1293 1271 * the unit-address using well-known address properties
1294 1272 * (SCSI_ADDR_PROP_TARGET_PORT, SCSI_ADDR_PROP_LUN64) during
1295 1273 * devinfo/pathinfo node creation. The HBA driver can obtain values
1296 1274 * using scsi_device_prop_lookup_*() from its tran_tgt_init(9E).
1297 1275 *
1298 1276 */
1299 1277 if ((tran->tran_get_name == NULL) ^ (tran->tran_get_bus_addr == NULL)) {
1300 1278 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
1301 1279 "should support both or neither: "
1302 1280 "tran_get_name, tran_get_bus_addr"));
1303 1281 return (DDI_FAILURE);
1304 1282 }
1305 1283
1306 1284 /*
1307 1285 * Establish the devinfo context of this tran structure, preserving
1308 1286 * knowledge of how the tran was allocated.
1309 1287 */
1310 1288 tran->tran_hba_dip = self; /* for DMA */
1311 1289 tran->tran_hba_flags = (flags & ~SCSI_HBA_SCSA_TA) |
1312 1290 (tran->tran_hba_flags & SCSI_HBA_SCSA_TA);
1313 1291
1314 1292 /* Establish flavor of transport (and ddi_get_driver_private()) */
1315 1293 ndi_flavorv_set(self, SCSA_FLAVOR_SCSI_DEVICE, tran);
1316 1294
1317 1295 /*
1318 1296 * Note: We only need dma_attr_minxfer and dma_attr_burstsizes
1319 1297 * from the DMA attributes. scsi_hba_attach(9f) only guarantees
1320 1298 * that these two fields are initialized properly. If this
1321 1299 * changes, be sure to revisit the implementation of
1322 1300 * scsi_hba_attach(9F).
1323 1301 */
1324 1302 (void) memcpy(&tran->tran_dma_attr, hba_dma_attr,
1325 1303 sizeof (ddi_dma_attr_t));
1326 1304
1327 1305 /* Create tran_setup_pkt(9E) kmem_cache. */
1328 1306 if (tran->tran_setup_pkt) {
1329 1307 ASSERT(tran->tran_init_pkt == NULL);
1330 1308 ASSERT(tran->tran_destroy_pkt == NULL);
1331 1309 if (tran->tran_init_pkt || tran->tran_destroy_pkt)
1332 1310 goto fail;
1333 1311
1334 1312 tran->tran_init_pkt = scsi_init_cache_pkt;
1335 1313 tran->tran_destroy_pkt = scsi_free_cache_pkt;
1336 1314 tran->tran_sync_pkt = scsi_sync_cache_pkt;
1337 1315 tran->tran_dmafree = scsi_cache_dmafree;
1338 1316
1339 1317 len = sizeof (struct scsi_pkt_cache_wrapper);
1340 1318 len += ROUNDUP(tran->tran_hba_len);
1341 1319 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CDB)
1342 1320 len += ROUNDUP(DEFAULT_CDBLEN);
1343 1321 if (tran->tran_hba_flags & SCSI_HBA_TRAN_SCB)
1344 1322 len += ROUNDUP(DEFAULT_SCBLEN);
1345 1323
1346 1324 (void) snprintf(cache_name, sizeof (cache_name),
1347 1325 "pkt_cache_%s_%d", ddi_driver_name(self),
1348 1326 ddi_get_instance(self));
1349 1327
1350 1328 tran->tran_pkt_cache_ptr = kmem_cache_create(
1351 1329 cache_name, len, 8, scsi_hba_pkt_constructor,
1352 1330 scsi_hba_pkt_destructor, NULL, tran, NULL, 0);
1353 1331 }
1354 1332
1355 1333 /* Perform node setup independent of initiator role */
1356 1334 if (scsa_nexus_setup(self, tran) != DDI_SUCCESS)
1357 1335 goto fail;
1358 1336
1359 1337 /*
1360 1338 * The SCSI_HBA_HBA flag is passed to scsi_hba_attach_setup when the
1361 1339 * HBA driver knows that *all* children of the SCSA HBA node will be
1362 1340 * 'iports'. If the SCSA HBA node can have iport children and also
1363 1341 * function as an initiator for xxx_device children then it should
1364 1342 * not specify SCSI_HBA_HBA in its scsi_hba_attach_setup call. An
1365 1343 * HBA driver that does not manage iports should not set SCSA_HBA_HBA.
1366 1344 */
1367 1345 if (tran->tran_hba_flags & SCSI_HBA_HBA) {
1368 1346 /*
1369 1347 * Set the 'ddi-config-driver-node' property on the nexus
1370 1348 * node that notify attach_driver_nodes() to configure all
1371 1349 * immediate children so that nodes which bind to the
1372 1350 * same driver as parent are able to be added into per-driver
1373 1351 * list.
1374 1352 */
1375 1353 if (ndi_prop_create_boolean(DDI_DEV_T_NONE,
1376 1354 self, "ddi-config-driver-node") != DDI_PROP_SUCCESS)
1377 1355 goto fail;
1378 1356 } else {
1379 1357 if (scsa_tran_setup(self, tran) != DDI_SUCCESS)
1380 1358 goto fail;
1381 1359 }
1382 1360
1383 1361 return (DDI_SUCCESS);
1384 1362
1385 1363 fail: (void) scsi_hba_detach(self);
1386 1364 return (DDI_FAILURE);
1387 1365 }
1388 1366
1389 1367 /*
1390 1368 * Called by an HBA to detach an instance of the driver. This may be called
1391 1369 * for SCSA HBA nodes and for SCSA iport nodes.
1392 1370 */
1393 1371 int
1394 1372 scsi_hba_detach(dev_info_t *self)
1395 1373 {
1396 1374 scsi_hba_tran_t *tran;
1397 1375
1398 1376 ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1399 1377 if (scsi_hba_iport_unit_address(self))
1400 1378 return (DDI_FAILURE); /* self can't be an iport */
1401 1379
1402 1380 /* Check all error return conditions upfront */
1403 1381 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1404 1382 ASSERT(tran);
1405 1383 if (tran == NULL)
1406 1384 return (DDI_FAILURE);
1407 1385
1408 1386 ASSERT(tran->tran_open_flag == 0);
1409 1387 if (tran->tran_open_flag)
1410 1388 return (DDI_FAILURE);
1411 1389
1412 1390 if (!(tran->tran_hba_flags & SCSI_HBA_HBA))
1413 1391 scsa_tran_teardown(self, tran);
1414 1392 scsa_nexus_teardown(self, tran);
1415 1393
1416 1394 /* Teardown tran_setup_pkt(9E) kmem_cache. */
1417 1395 if (tran->tran_pkt_cache_ptr) {
1418 1396 kmem_cache_destroy(tran->tran_pkt_cache_ptr);
1419 1397 tran->tran_pkt_cache_ptr = NULL;
1420 1398 }
1421 1399
1422 1400 (void) memset(&tran->tran_dma_attr, 0, sizeof (ddi_dma_attr_t));
1423 1401
1424 1402 /* Teardown flavor of transport (and ddi_get_driver_private()) */
1425 1403 ndi_flavorv_set(self, SCSA_FLAVOR_SCSI_DEVICE, NULL);
1426 1404
1427 1405 tran->tran_hba_dip = NULL;
1428 1406
1429 1407 return (DDI_SUCCESS);
1430 1408 }
1431 1409
1432 1410
1433 1411 /*
1434 1412 * Called by an HBA from _fini()
1435 1413 */
1436 1414 void
1437 1415 scsi_hba_fini(struct modlinkage *modlp)
1438 1416 {
1439 1417 struct dev_ops *hba_dev_ops;
1440 1418
1441 1419 SCSI_HBA_LOG((_LOG_TRACE, NULL, NULL, __func__));
1442 1420
1443 1421 /* Get the devops structure of this module and clear bus_ops vector. */
1444 1422 hba_dev_ops = ((struct modldrv *)(modlp->ml_linkage[0]))->drv_dev_ops;
1445 1423
1446 1424 if (hba_dev_ops->devo_cb_ops == &scsi_hba_cbops)
1447 1425 hba_dev_ops->devo_cb_ops = NULL;
1448 1426
1449 1427 if (hba_dev_ops->devo_getinfo == scsi_hba_info)
1450 1428 hba_dev_ops->devo_getinfo = NULL;
1451 1429
1452 1430 hba_dev_ops->devo_bus_ops = (struct bus_ops *)NULL;
1453 1431 }
1454 1432
1455 1433 /*
1456 1434 * SAS specific functions
1457 1435 */
1458 1436 smp_hba_tran_t *
1459 1437 smp_hba_tran_alloc(dev_info_t *self)
1460 1438 {
1461 1439 /* allocate SCSA flavors for self */
1462 1440 ndi_flavorv_alloc(self, SCSA_NFLAVORS);
1463 1441 return (kmem_zalloc(sizeof (smp_hba_tran_t), KM_SLEEP));
1464 1442 }
1465 1443
1466 1444 void
1467 1445 smp_hba_tran_free(smp_hba_tran_t *tran)
1468 1446 {
1469 1447 kmem_free(tran, sizeof (smp_hba_tran_t));
1470 1448 }
1471 1449
1472 1450 int
1473 1451 smp_hba_attach_setup(
1474 1452 dev_info_t *self,
1475 1453 smp_hba_tran_t *tran)
1476 1454 {
1477 1455 ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1478 1456 if (scsi_hba_iport_unit_address(self))
1479 1457 return (DDI_FAILURE); /* self can't be an iport */
1480 1458
1481 1459 /*
1482 1460 * The owner of the this devinfo_t was responsible
1483 1461 * for informing the framework already about
1484 1462 * additional flavors.
1485 1463 */
1486 1464 ndi_flavorv_set(self, SCSA_FLAVOR_SMP, tran);
1487 1465 return (DDI_SUCCESS);
1488 1466 }
1489 1467
1490 1468 int
1491 1469 smp_hba_detach(dev_info_t *self)
1492 1470 {
1493 1471 ASSERT(scsi_hba_iport_unit_address(self) == NULL);
1494 1472 if (scsi_hba_iport_unit_address(self))
1495 1473 return (DDI_FAILURE); /* self can't be an iport */
1496 1474
1497 1475 ndi_flavorv_set(self, SCSA_FLAVOR_SMP, NULL);
1498 1476 return (DDI_SUCCESS);
1499 1477 }
1500 1478
1501 1479 /*
1502 1480 * SMP child flavored functions
1503 1481 */
1504 1482 static int
1505 1483 smp_busctl_ua(dev_info_t *child, char *addr, int maxlen)
1506 1484 {
1507 1485 char *tport;
1508 1486 char *wwn;
1509 1487
1510 1488 /* limit ndi_devi_findchild_by_callback to expected flavor */
1511 1489 if (ndi_flavor_get(child) != SCSA_FLAVOR_SMP)
1512 1490 return (DDI_FAILURE);
1513 1491
1514 1492 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1515 1493 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1516 1494 SCSI_ADDR_PROP_TARGET_PORT, &tport) == DDI_SUCCESS) {
1517 1495 (void) snprintf(addr, maxlen, "%s", tport);
1518 1496 ddi_prop_free(tport);
1519 1497 return (DDI_SUCCESS);
1520 1498 }
1521 1499
1522 1500 /*
1523 1501 * NOTE: the following code should be deleted when mpt is changed to
1524 1502 * use SCSI_ADDR_PROP_TARGET_PORT instead of SMP_WWN.
1525 1503 */
1526 1504 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1527 1505 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1528 1506 SMP_WWN, &wwn) == DDI_SUCCESS) {
1529 1507 (void) snprintf(addr, maxlen, "w%s", wwn);
1530 1508 ddi_prop_free(wwn);
1531 1509 return (DDI_SUCCESS);
1532 1510 }
1533 1511 return (DDI_FAILURE);
1534 1512 }
1535 1513
1536 1514 static int
1537 1515 smp_busctl_reportdev(dev_info_t *child)
1538 1516 {
1539 1517 dev_info_t *self = ddi_get_parent(child);
1540 1518 char *tport;
1541 1519 char *wwn;
1542 1520
1543 1521 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1544 1522 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1545 1523 SCSI_ADDR_PROP_TARGET_PORT, &tport) == DDI_SUCCESS) {
1546 1524 SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: target-port %s",
1547 1525 ddi_driver_name(child), ddi_get_instance(child),
1548 1526 ddi_driver_name(self), ddi_get_instance(self), tport));
1549 1527 ddi_prop_free(tport);
1550 1528 return (DDI_SUCCESS);
1551 1529 }
1552 1530
1553 1531 /*
1554 1532 * NOTE: the following code should be deleted when mpt is changed to
1555 1533 * use SCSI_ADDR_PROP_TARGET_PORT instead of SMP_WWN.
1556 1534 */
1557 1535 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1558 1536 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1559 1537 SMP_WWN, &wwn) == DDI_SUCCESS) {
1560 1538 SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: wwn %s",
1561 1539 ddi_driver_name(child), ddi_get_instance(child),
1562 1540 ddi_driver_name(self), ddi_get_instance(self), wwn));
1563 1541 ddi_prop_free(wwn);
1564 1542 return (DDI_SUCCESS);
1565 1543 }
1566 1544 return (DDI_FAILURE);
1567 1545 }
1568 1546
1569 1547 static int
1570 1548 smp_busctl_initchild(dev_info_t *child)
1571 1549 {
1572 1550 dev_info_t *self = ddi_get_parent(child);
1573 1551 smp_hba_tran_t *tran;
1574 1552 dev_info_t *dup;
1575 1553 char addr[SCSI_MAXNAMELEN];
1576 1554 struct smp_device *smp_sd;
1577 1555 uint64_t wwn;
1578 1556
1579 1557 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SMP);
1580 1558 ASSERT(tran);
1581 1559 if (tran == NULL)
1582 1560 return (DDI_FAILURE);
1583 1561
1584 1562 if (smp_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS)
1585 1563 return (DDI_NOT_WELL_FORMED);
1586 1564 if (scsi_wwnstr_to_wwn(addr, &wwn))
1587 1565 return (DDI_NOT_WELL_FORMED);
1588 1566
1589 1567 /* Prevent duplicate nodes. */
1590 1568 dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
1591 1569 smp_busctl_ua);
1592 1570 if (dup) {
1593 1571 ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_SMP);
1594 1572 if (ndi_flavor_get(dup) != SCSA_FLAVOR_SMP) {
1595 1573 SCSI_HBA_LOG((_LOG(1), NULL, child,
1596 1574 "init failed: %s@%s: not SMP flavored",
1597 1575 ddi_node_name(child), addr));
1598 1576 return (DDI_FAILURE);
1599 1577 }
1600 1578 if (dup != child) {
1601 1579 SCSI_HBA_LOG((_LOG(4), NULL, child,
1602 1580 "init failed: %s@%s: detected duplicate %p",
1603 1581 ddi_node_name(child), addr, (void *)dup));
1604 1582 return (DDI_FAILURE);
1605 1583 }
1606 1584 }
1607 1585
1608 1586
1609 1587 /* set the node @addr string */
1610 1588 ddi_set_name_addr(child, addr);
1611 1589
1612 1590 /* Allocate and initialize smp_device. */
1613 1591 smp_sd = kmem_zalloc(sizeof (struct smp_device), KM_SLEEP);
1614 1592 smp_sd->smp_sd_dev = child;
1615 1593 smp_sd->smp_sd_address.smp_a_hba_tran = tran;
1616 1594 bcopy(&wwn, smp_sd->smp_sd_address.smp_a_wwn, SAS_WWN_BYTE_SIZE);
1617 1595
1618 1596 ddi_set_driver_private(child, smp_sd);
1619 1597
1620 1598 if (tran->smp_tran_init && ((*tran->smp_tran_init)(self, child,
1621 1599 tran, smp_sd) != DDI_SUCCESS)) {
1622 1600 kmem_free(smp_sd, sizeof (struct smp_device));
1623 1601 scsi_enumeration_failed(child, -1, NULL, "smp_tran_init");
1624 1602 ddi_set_driver_private(child, NULL);
1625 1603 ddi_set_name_addr(child, NULL);
1626 1604 return (DDI_FAILURE);
1627 1605 }
1628 1606
1629 1607 return (DDI_SUCCESS);
1630 1608 }
1631 1609
1632 1610 /*ARGSUSED*/
1633 1611 static int
1634 1612 smp_busctl_uninitchild(dev_info_t *child)
1635 1613 {
1636 1614 dev_info_t *self = ddi_get_parent(child);
1637 1615 struct smp_device *smp_sd = ddi_get_driver_private(child);
1638 1616 smp_hba_tran_t *tran;
1639 1617
1640 1618 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SMP);
1641 1619 ASSERT(smp_sd && tran);
1642 1620 if ((smp_sd == NULL) || (tran == NULL))
1643 1621 return (DDI_FAILURE);
1644 1622
1645 1623 if (tran->smp_tran_free)
1646 1624 (*tran->smp_tran_free) (self, child, tran, smp_sd);
1647 1625
1648 1626 kmem_free(smp_sd, sizeof (*smp_sd));
1649 1627 ddi_set_driver_private(child, NULL);
1650 1628 ddi_set_name_addr(child, NULL);
1651 1629 return (DDI_SUCCESS);
1652 1630 }
1653 1631
1654 1632 /* Find an "smp" child at the specified address. */
1655 1633 static dev_info_t *
1656 1634 smp_findchild(dev_info_t *self, char *addr)
1657 1635 {
1658 1636 dev_info_t *child;
1659 1637
1660 1638 /* Search "smp" devinfo child at specified address. */
1661 1639 ASSERT(self && DEVI_BUSY_OWNED(self) && addr);
1662 1640 for (child = ddi_get_child(self); child;
1663 1641 child = ddi_get_next_sibling(child)) {
1664 1642 /* skip non-"smp" nodes */
1665 1643 if (ndi_flavor_get(child) != SCSA_FLAVOR_SMP)
1666 1644 continue;
1667 1645
1668 1646 /* Attempt initchild to establish unit-address */
1669 1647 if (i_ddi_node_state(child) < DS_INITIALIZED)
1670 1648 (void) ddi_initchild(self, child);
1671 1649
1672 1650 /* Verify state and non-NULL unit-address. */
1673 1651 if ((i_ddi_node_state(child) < DS_INITIALIZED) ||
1674 1652 (ddi_get_name_addr(child) == NULL))
1675 1653 continue;
1676 1654
1677 1655 /* Return "smp" child if unit-address matches. */
1678 1656 if (strcmp(ddi_get_name_addr(child), addr) == 0)
1679 1657 return (child);
1680 1658 }
1681 1659 return (NULL);
1682 1660 }
1683 1661
1684 1662 /*
1685 1663 * Search for "smp" child of self at the specified address. If found, online
1686 1664 * and return with a hold. Unlike general SCSI configuration, we can assume
1687 1665 * the the device is actually there when we are called (i.e., device is
1688 1666 * created by hotplug, not by bus_config).
1689 1667 */
1690 1668 int
1691 1669 smp_hba_bus_config(dev_info_t *self, char *addr, dev_info_t **childp)
1692 1670 {
1693 1671 dev_info_t *child;
1694 1672 int circ;
1695 1673
1696 1674 ASSERT(self && addr && childp);
1697 1675 *childp = NULL;
1698 1676
1699 1677 /* Search for "smp" child. */
1700 1678 scsi_hba_devi_enter(self, &circ);
1701 1679 if ((child = smp_findchild(self, addr)) == NULL) {
1702 1680 scsi_hba_devi_exit(self, circ);
1703 1681 return (NDI_FAILURE);
1704 1682 }
1705 1683
1706 1684 /* Attempt online. */
1707 1685 if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
1708 1686 scsi_hba_devi_exit(self, circ);
1709 1687 return (NDI_FAILURE);
1710 1688 }
1711 1689
1712 1690 /* On success, return with active hold. */
1713 1691 ndi_hold_devi(child);
1714 1692 scsi_hba_devi_exit(self, circ);
1715 1693 *childp = child;
1716 1694 return (NDI_SUCCESS);
1717 1695 }
1718 1696
1719 1697
1720 1698
1721 1699 /* Create "smp" child devinfo node at specified unit-address. */
1722 1700 int
1723 1701 smp_hba_bus_config_taddr(dev_info_t *self, char *addr)
1724 1702 {
1725 1703 dev_info_t *child;
1726 1704 int circ;
1727 1705
1728 1706 /*
1729 1707 * NOTE: If we ever uses a generic node name (.vs. a driver name)
1730 1708 * or define a 'compatible' property, this code will need to use
1731 1709 * a 'probe' node (ala scsi_device support) to obtain identity
1732 1710 * information from the device.
1733 1711 */
1734 1712
1735 1713 /* Search for "smp" child. */
1736 1714 scsi_hba_devi_enter(self, &circ);
1737 1715 child = smp_findchild(self, addr);
1738 1716 if (child) {
1739 1717 /* Child exists, note if this was a new reinsert. */
1740 1718 if (ndi_devi_device_insert(child))
1741 1719 SCSI_HBA_LOG((_LOGCFG, self, NULL,
1742 1720 "devinfo smp@%s device_reinsert", addr));
1743 1721
1744 1722 scsi_hba_devi_exit(self, circ);
1745 1723 return (NDI_SUCCESS);
1746 1724 }
1747 1725
1748 1726 /* Allocate "smp" child devinfo node and establish flavor of child. */
1749 1727 ndi_devi_alloc_sleep(self, "smp", DEVI_SID_HP_NODEID, &child);
1750 1728 ASSERT(child);
1751 1729 ndi_flavor_set(child, SCSA_FLAVOR_SMP);
1752 1730
1753 1731 /* Add unit-address property to child. */
1754 1732 if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
1755 1733 SCSI_ADDR_PROP_TARGET_PORT, addr) != DDI_PROP_SUCCESS) {
1756 1734 (void) ndi_devi_free(child);
1757 1735 scsi_hba_devi_exit(self, circ);
1758 1736 return (NDI_FAILURE);
1759 1737 }
1760 1738
1761 1739 /* Attempt to online the new "smp" node. */
1762 1740 (void) ndi_devi_online(child, 0);
1763 1741
1764 1742 scsi_hba_devi_exit(self, circ);
1765 1743 return (NDI_SUCCESS);
1766 1744 }
1767 1745
1768 1746 /*
1769 1747 * Wrapper to scsi_ua_get which takes a devinfo argument instead of a
1770 1748 * scsi_device structure.
1771 1749 */
1772 1750 static int
1773 1751 scsi_busctl_ua(dev_info_t *child, char *addr, int maxlen)
1774 1752 {
1775 1753 struct scsi_device *sd;
1776 1754
1777 1755 /* limit ndi_devi_findchild_by_callback to expected flavor */
1778 1756 if (ndi_flavor_get(child) != SCSA_FLAVOR_SCSI_DEVICE)
1779 1757 return (DDI_FAILURE);
1780 1758
1781 1759 /* nodes are named by tran_get_name or default "tgt,lun" */
1782 1760 sd = ddi_get_driver_private(child);
1783 1761 if (sd && (scsi_ua_get(sd, addr, maxlen) == 1))
1784 1762 return (DDI_SUCCESS);
1785 1763
1786 1764 return (DDI_FAILURE);
1787 1765 }
1788 1766
1789 1767 static int
1790 1768 scsi_busctl_reportdev(dev_info_t *child)
1791 1769 {
1792 1770 dev_info_t *self = ddi_get_parent(child);
1793 1771 struct scsi_device *sd = ddi_get_driver_private(child);
1794 1772 scsi_hba_tran_t *tran;
1795 1773 char ua[SCSI_MAXNAMELEN];
1796 1774 char ra[SCSI_MAXNAMELEN];
1797 1775
1798 1776 SCSI_HBA_LOG((_LOG_TRACE, NULL, child, __func__));
1799 1777
1800 1778 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1801 1779 ASSERT(tran && sd);
1802 1780 if ((tran == NULL) || (sd == NULL))
1803 1781 return (DDI_FAILURE);
1804 1782
1805 1783 /* get the unit_address and bus_addr information */
1806 1784 if ((scsi_ua_get(sd, ua, sizeof (ua)) == 0) ||
1807 1785 (scsi_ua_get_reportdev(sd, ra, sizeof (ra)) == 0)) {
1808 1786 SCSI_HBA_LOG((_LOG(WARN), NULL, child, "REPORTDEV failure"));
1809 1787 return (DDI_FAILURE);
1810 1788 }
1811 1789
1812 1790 if (tran->tran_get_name == NULL)
1813 1791 SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: %s",
1814 1792 ddi_driver_name(child), ddi_get_instance(child),
1815 1793 ddi_driver_name(self), ddi_get_instance(self), ra));
1816 1794 else if (*ra)
1817 1795 SCSI_HBA_LOG((_LOG_NF(CONT),
1818 1796 "?%s%d at %s%d: unit-address %s: %s",
1819 1797 ddi_driver_name(child), ddi_get_instance(child),
1820 1798 ddi_driver_name(self), ddi_get_instance(self), ua, ra));
1821 1799 else
1822 1800 SCSI_HBA_LOG((_LOG_NF(CONT),
1823 1801 "?%s%d at %s%d: unit-address %s",
1824 1802 ddi_driver_name(child), ddi_get_instance(child),
1825 1803 ddi_driver_name(self), ddi_get_instance(self), ua));
1826 1804
1827 1805 return (DDI_SUCCESS);
1828 1806 }
1829 1807
1830 1808
1831 1809 /*
1832 1810 * scsi_busctl_initchild is called to initialize the SCSA transport for
1833 1811 * communication with a particular child scsi target device. Successful
1834 1812 * initialization requires properties on the node which describe the address
1835 1813 * of the target device. If the address of the target device can't be
1836 1814 * determined from properties then DDI_NOT_WELL_FORMED is returned. Nodes that
1837 1815 * are DDI_NOT_WELL_FORMED are considered an implementation artifact and
1838 1816 * are hidden from devinfo snapshots by calling ndi_devi_set_hidden().
1839 1817 * The child may be one of the following types of devinfo nodes:
1840 1818 *
1841 1819 * OBP node:
1842 1820 * OBP does not enumerate target devices attached a SCSI bus. These
1843 1821 * template/stub/wild-card nodes are a legacy artifact for support of old
1844 1822 * driver loading methods. Since they have no properties,
1845 1823 * DDI_NOT_WELL_FORMED will be returned.
1846 1824 *
1847 1825 * SID node:
1848 1826 * The node may be either a:
1849 1827 * o probe/barrier SID node
1850 1828 * o a dynamic SID target node
1851 1829 *
1852 1830 * driver.conf node: The situation for this nexus is different than most.
1853 1831 * Typically a driver.conf node definition is used to either define a
1854 1832 * new child devinfo node or to further decorate (via merge) a SID
1855 1833 * child with properties. In our case we use the nodes for *both*
1856 1834 * purposes.
1857 1835 *
1858 1836 * In both the SID node and driver.conf node cases we must form the nodes
1859 1837 * "@addr" from the well-known scsi(9P) device unit-address properties on
1860 1838 * the node.
1861 1839 *
1862 1840 * For HBA drivers that implement the deprecated tran_get_name interface,
1863 1841 * "@addr" construction involves having that driver interpret properties via
1864 1842 * scsi_busctl_ua -> scsi_ua_get -> tran_get_name: there is no
1865 1843 * requirement for the property names to be well-known.
1866 1844 *
1867 1845 * NOTE: We don't currently support "merge". When this support is added a
1868 1846 * specific property, like "unit-address", should *always* identify a
1869 1847 * driver.conf node that needs to be merged into a specific SID node. When
1870 1848 * enumeration is enabled, a .conf node without the "unit-address" property
1871 1849 * should be ignored. The best way to establish the "unit-address" property
1872 1850 * would be to have the system assign parent= and unit-address= from an
1873 1851 * instance=# driver.conf entry (by using the instance tree).
1874 1852 */
1875 1853 static int
1876 1854 scsi_busctl_initchild(dev_info_t *child)
1877 1855 {
1878 1856 dev_info_t *self = ddi_get_parent(child);
1879 1857 dev_info_t *dup;
1880 1858 scsi_hba_tran_t *tran;
1881 1859 struct scsi_device *sd;
1882 1860 scsi_hba_tran_t *tran_clone;
1883 1861 char *class;
1884 1862 int tgt;
1885 1863 int lun;
1886 1864 int sfunc;
1887 1865 int err = DDI_FAILURE;
1888 1866 char addr[SCSI_MAXNAMELEN];
1889 1867
1890 1868 ASSERT(DEVI_BUSY_OWNED(self));
1891 1869 SCSI_HBA_LOG((_LOG(4), NULL, child, "init begin"));
1892 1870
1893 1871 /*
1894 1872 * For a driver like fp with multiple upper-layer-protocols
1895 1873 * it is possible for scsi_hba_init in _init to plumb SCSA
1896 1874 * and have the load of fcp (which does scsi_hba_attach_setup)
1897 1875 * to fail. In this case we may get here with a NULL hba.
1898 1876 */
1899 1877 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
1900 1878 if (tran == NULL)
1901 1879 return (DDI_NOT_WELL_FORMED);
1902 1880
1903 1881 /*
1904 1882 * OBP may create template/stub/wild-card nodes for legacy driver
1905 1883 * loading methods. These nodes have no properties, so we lack the
1906 1884 * addressing properties to initchild them. Hide the node and return
1907 1885 * DDI_NOT_WELL_FORMED.
1908 1886 *
1909 1887 * Future: define/use a ndi_devi_has_properties(dip) type interface.
1910 1888 *
1911 1889 * NOTE: It would be nice if we could delete these ill formed nodes by
1912 1890 * implementing a DDI_NOT_WELL_FORMED_DELETE return code. This can't
1913 1891 * be done until leadville debug code removes its dependencies
1914 1892 * on the devinfo still being present after a failed ndi_devi_online.
1915 1893 */
1916 1894 if ((DEVI(child)->devi_hw_prop_ptr == NULL) &&
1917 1895 (DEVI(child)->devi_drv_prop_ptr == NULL) &&
1918 1896 (DEVI(child)->devi_sys_prop_ptr == NULL)) {
1919 1897 SCSI_HBA_LOG((_LOG(4), NULL, child,
1920 1898 "init failed: no properties"));
1921 1899 ndi_devi_set_hidden(child);
1922 1900 return (DDI_NOT_WELL_FORMED);
1923 1901 }
1924 1902
1925 1903 /* get legacy SPI addressing properties */
1926 1904 if ((tgt = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1927 1905 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1928 1906 SCSI_ADDR_PROP_TARGET, -1)) == -1) {
1929 1907 tgt = 0;
1930 1908 /*
1931 1909 * A driver.conf node for merging always has a target= property,
1932 1910 * even if it is just a dummy that does not contain the real
1933 1911 * target address. However drivers that register devids may
1934 1912 * create stub driver.conf nodes without a target= property so
1935 1913 * that pathological devid resolution works. Hide the stub
1936 1914 * node and return DDI_NOT_WELL_FORMED.
1937 1915 */
1938 1916 if (!scsi_hba_dev_is_sid(child)) {
1939 1917 SCSI_HBA_LOG((_LOG(4), NULL, child,
1940 1918 "init failed: stub .conf node"));
1941 1919 ndi_devi_set_hidden(child);
1942 1920 return (DDI_NOT_WELL_FORMED);
1943 1921 }
1944 1922 }
1945 1923 lun = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1946 1924 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SCSI_ADDR_PROP_LUN, 0);
1947 1925 sfunc = ddi_prop_get_int(DDI_DEV_T_ANY, child,
1948 1926 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SCSI_ADDR_PROP_SFUNC, -1);
1949 1927
1950 1928 /*
1951 1929 * The scsi_address structure may not specify all the addressing
1952 1930 * information. For an old HBA that doesn't support tran_get_name
1953 1931 * (most pre-SCSI-3 HBAs) the scsi_address structure is still used,
1954 1932 * so the target property must exist and the LUN must be < 256.
1955 1933 */
1956 1934 if ((tran->tran_get_name == NULL) &&
1957 1935 ((tgt >= USHRT_MAX) || (lun >= 256))) {
1958 1936 SCSI_HBA_LOG((_LOG(1), NULL, child,
1959 1937 "init failed: illegal/missing properties"));
1960 1938 ndi_devi_set_hidden(child);
1961 1939 return (DDI_NOT_WELL_FORMED);
1962 1940 }
1963 1941
1964 1942 /*
1965 1943 * We need to initialize a fair amount of our environment to invoke
1966 1944 * tran_get_name (via scsi_busctl_ua and scsi_ua_get) to
1967 1945 * produce the "@addr" name from addressing properties. Allocate and
1968 1946 * initialize scsi device structure.
1969 1947 */
1970 1948 sd = kmem_zalloc(sizeof (struct scsi_device), KM_SLEEP);
1971 1949 mutex_init(&sd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
1972 1950 sd->sd_dev = child;
1973 1951 sd->sd_pathinfo = NULL;
1974 1952 sd->sd_uninit_prevent = 0;
1975 1953 ddi_set_driver_private(child, sd);
1976 1954
1977 1955 if (tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
1978 1956 /*
1979 1957 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to
1980 1958 * scsi_device in the scsi_address structure. This allows an
1981 1959 * HBA driver to find its per-scsi_device private data
1982 1960 * (accessible to the HBA given just the scsi_address by using
1983 1961 * scsi_address_device(9F)/scsi_device_hba_private_get(9F)).
1984 1962 */
1985 1963 sd->sd_address.a.a_sd = sd;
1986 1964 tran_clone = NULL;
1987 1965 } else {
1988 1966 /*
1989 1967 * Initialize the scsi_address so that a SCSI-2 target driver
1990 1968 * talking to a SCSI-2 device on a SCSI-3 bus (spi) continues
1991 1969 * to work. We skew the secondary function value so that we
1992 1970 * can tell from the address structure if we are processing
1993 1971 * a secondary function request.
1994 1972 */
1995 1973 sd->sd_address.a_target = (ushort_t)tgt;
1996 1974 sd->sd_address.a_lun = (uchar_t)lun;
1997 1975 if (sfunc == -1)
1998 1976 sd->sd_address.a_sublun = (uchar_t)0;
1999 1977 else
2000 1978 sd->sd_address.a_sublun = (uchar_t)sfunc + 1;
2001 1979
2002 1980 /*
2003 1981 * NOTE: Don't limit LUNs to scsi_options value because a
2004 1982 * scsi_device discovered via SPI dynamic enumeration might
2005 1983 * still support SCMD_REPORT_LUNS.
2006 1984 */
2007 1985
2008 1986 /*
2009 1987 * Deprecated: Use SCSI_HBA_ADDR_COMPLEX:
2010 1988 * Clone transport structure if requested. Cloning allows
2011 1989 * an HBA to maintain target-specific information if
2012 1990 * necessary, such as target addressing information that
2013 1991 * does not adhere to the scsi_address structure format.
2014 1992 */
2015 1993 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
2016 1994 tran_clone = kmem_alloc(
2017 1995 sizeof (scsi_hba_tran_t), KM_SLEEP);
2018 1996 bcopy((caddr_t)tran,
2019 1997 (caddr_t)tran_clone, sizeof (scsi_hba_tran_t));
2020 1998 tran = tran_clone;
2021 1999 tran->tran_sd = sd;
2022 2000 } else {
2023 2001 tran_clone = NULL;
2024 2002 ASSERT(tran->tran_sd == NULL);
2025 2003 }
2026 2004 }
2027 2005
2028 2006 /* establish scsi_address pointer to the HBA's tran structure */
2029 2007 sd->sd_address.a_hba_tran = tran;
2030 2008
2031 2009 /*
2032 2010 * This is a grotty hack that allows direct-access (non-scsa) drivers
2033 2011 * (like chs, ata, and mlx which all make cmdk children) to put its
2034 2012 * own vector in the 'a_hba_tran' field. When all the drivers that do
2035 2013 * this are fixed, please remove this hack.
2036 2014 *
2037 2015 * NOTE: This hack is also shows up in the DEVP_TO_TRAN implementation
2038 2016 * in scsi_confsubr.c.
2039 2017 */
2040 2018 sd->sd_tran_safe = tran;
2041 2019
2042 2020 /*
2043 2021 * If the class property is not already established, set it to "scsi".
2044 2022 * This is done so that parent= driver.conf nodes have class.
2045 2023 */
2046 2024 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2047 2025 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "class",
2048 2026 &class) == DDI_PROP_SUCCESS) {
2049 2027 ddi_prop_free(class);
2050 2028 } else if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
2051 2029 "class", "scsi") != DDI_PROP_SUCCESS) {
2052 2030 SCSI_HBA_LOG((_LOG(2), NULL, child, "init failed: class"));
2053 2031 ndi_devi_set_hidden(child);
2054 2032 err = DDI_NOT_WELL_FORMED;
2055 2033 goto failure;
2056 2034 }
2057 2035
2058 2036 /* Establish the @addr name of the child. */
2059 2037 *addr = '\0';
2060 2038 if (scsi_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS) {
2061 2039 /*
2062 2040 * Some driver.conf files add bogus target properties (relative
2063 2041 * to their nexus representation of target) to their stub
2064 2042 * nodes, causing the check above to not filter them.
2065 2043 */
2066 2044 SCSI_HBA_LOG((_LOG(3), NULL, child,
2067 2045 "init failed: scsi_busctl_ua call"));
2068 2046 ndi_devi_set_hidden(child);
2069 2047 err = DDI_NOT_WELL_FORMED;
2070 2048 goto failure;
2071 2049 }
2072 2050 if (*addr == '\0') {
2073 2051 SCSI_HBA_LOG((_LOG(2), NULL, child, "init failed: ua"));
2074 2052 ndi_devi_set_hidden(child);
2075 2053 err = DDI_NOT_WELL_FORMED;
2076 2054 goto failure;
2077 2055 }
2078 2056
2079 2057 /* Prevent duplicate nodes. */
2080 2058 dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
2081 2059 scsi_busctl_ua);
2082 2060 if (dup) {
2083 2061 ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_SCSI_DEVICE);
2084 2062 if (ndi_flavor_get(dup) != SCSA_FLAVOR_SCSI_DEVICE) {
2085 2063 SCSI_HBA_LOG((_LOG(1), NULL, child,
2086 2064 "init failed: %s@%s: not SCSI_DEVICE flavored",
2087 2065 ddi_node_name(child), addr));
2088 2066 goto failure;
2089 2067 }
2090 2068 if (dup != child) {
2091 2069 SCSI_HBA_LOG((_LOG(4), NULL, child,
2092 2070 "init failed: %s@%s: detected duplicate %p",
2093 2071 ddi_node_name(child), addr, (void *)dup));
2094 2072 goto failure;
2095 2073 }
2096 2074 }
2097 2075
2098 2076 /* set the node @addr string */
2099 2077 ddi_set_name_addr(child, addr);
2100 2078
2101 2079 /* call HBA's target init entry point if it exists */
2102 2080 if (tran->tran_tgt_init != NULL) {
2103 2081 SCSI_HBA_LOG((_LOG(4), NULL, child, "init tran_tgt_init"));
2104 2082 sd->sd_tran_tgt_free_done = 0;
2105 2083 if ((*tran->tran_tgt_init)
2106 2084 (self, child, tran, sd) != DDI_SUCCESS) {
2107 2085 scsi_enumeration_failed(child, -1, NULL,
2108 2086 "tran_tgt_init");
2109 2087 goto failure;
2110 2088 }
2111 2089 }
2112 2090
2113 2091 SCSI_HBA_LOG((_LOG(3), NULL, child, "init successful"));
2114 2092 return (DDI_SUCCESS);
2115 2093
2116 2094 failure:
2117 2095 if (tran_clone)
2118 2096 kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
2119 2097 mutex_destroy(&sd->sd_mutex);
2120 2098 kmem_free(sd, sizeof (*sd));
2121 2099 ddi_set_driver_private(child, NULL);
2122 2100 ddi_set_name_addr(child, NULL);
2123 2101
2124 2102 return (err); /* remove the node */
2125 2103 }
2126 2104
2127 2105 static int
2128 2106 scsi_busctl_uninitchild(dev_info_t *child)
2129 2107 {
2130 2108 dev_info_t *self = ddi_get_parent(child);
2131 2109 struct scsi_device *sd = ddi_get_driver_private(child);
2132 2110 scsi_hba_tran_t *tran;
2133 2111 scsi_hba_tran_t *tran_clone;
2134 2112
2135 2113 ASSERT(DEVI_BUSY_OWNED(self));
2136 2114
2137 2115 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
2138 2116 ASSERT(tran && sd);
2139 2117 if ((tran == NULL) || (sd == NULL))
2140 2118 return (DDI_FAILURE);
2141 2119
2142 2120 /*
2143 2121 * We use sd_uninit_prevent to avoid uninitializing barrier/probe
2144 2122 * nodes that are still in use. Since barrier/probe nodes are not
2145 2123 * attached we can't prevent their state demotion via ndi_hold_devi.
2146 2124 */
2147 2125 if (sd->sd_uninit_prevent) {
2148 2126 SCSI_HBA_LOG((_LOG(2), NULL, child, "uninit prevented"));
2149 2127 return (DDI_FAILURE);
2150 2128 }
2151 2129
2152 2130 /*
2153 2131 * Don't uninitialize a client node if it still has paths.
2154 2132 */
2155 2133 if (MDI_CLIENT(child) && mdi_client_get_path_count(child)) {
2156 2134 SCSI_HBA_LOG((_LOG(2), NULL, child,
2157 2135 "uninit prevented, client has paths"));
2158 2136 return (DDI_FAILURE);
2159 2137 }
2160 2138
2161 2139 SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit begin"));
2162 2140
2163 2141 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
2164 2142 tran_clone = sd->sd_address.a_hba_tran;
2165 2143
2166 2144 /* ... grotty hack, involving sd_tran_safe, continued. */
2167 2145 if (tran_clone != sd->sd_tran_safe) {
2168 2146 tran_clone = sd->sd_tran_safe;
2169 2147 #ifdef DEBUG
2170 2148 /*
2171 2149 * Complain so things get fixed and hack can, at
2172 2150 * some point in time, be removed.
2173 2151 */
2174 2152 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2175 2153 "'%s' is corrupting a_hba_tran", sd->sd_dev ?
2176 2154 ddi_driver_name(sd->sd_dev) : "unknown_driver"));
2177 2155 #endif /* DEBUG */
2178 2156 }
2179 2157
2180 2158 ASSERT(tran_clone->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
2181 2159 ASSERT(tran_clone->tran_sd == sd);
2182 2160 tran = tran_clone;
2183 2161 } else {
2184 2162 tran_clone = NULL;
2185 2163 ASSERT(tran->tran_sd == NULL);
2186 2164 }
2187 2165
2188 2166 /*
2189 2167 * To simplify host adapter drivers we guarantee that multiple
2190 2168 * tran_tgt_init(9E) calls of the same unit address are never
2191 2169 * active at the same time. This requires that we always call
2192 2170 * tran_tgt_free on probe/barrier nodes directly prior to
2193 2171 * uninitchild.
2194 2172 *
2195 2173 * NOTE: To correctly support SCSI_HBA_TRAN_CLONE, we must use
2196 2174 * the (possibly cloned) hba_tran pointer from the scsi_device
2197 2175 * instead of hba_tran.
2198 2176 */
2199 2177 if (tran->tran_tgt_free) {
2200 2178 if (!sd->sd_tran_tgt_free_done) {
2201 2179 SCSI_HBA_LOG((_LOG(4), NULL, child,
2202 2180 "uninit tran_tgt_free"));
2203 2181 (*tran->tran_tgt_free) (self, child, tran, sd);
2204 2182 sd->sd_tran_tgt_free_done = 1;
2205 2183 } else {
2206 2184 SCSI_HBA_LOG((_LOG(4), NULL, child,
2207 2185 "uninit tran_tgt_free already done"));
2208 2186 }
2209 2187 }
2210 2188
2211 2189 /*
2212 2190 * If a inquiry data is still allocated (by scsi_probe()) we
2213 2191 * free the allocation here. This keeps scsi_inq valid for the
2214 2192 * same duration as the corresponding inquiry properties. It
2215 2193 * also allows a tran_tgt_init() implementation that establishes
2216 2194 * sd_inq to deal with deallocation in its tran_tgt_free
2217 2195 * (setting sd_inq back to NULL) without upsetting the
2218 2196 * framework. Moving the inquiry free here also allows setting
2219 2197 * of sd_uninit_prevent to preserve the data for lun0 based
2220 2198 * scsi_get_device_type_scsi_options() calls.
2221 2199 */
2222 2200 if (sd->sd_inq) {
2223 2201 kmem_free(sd->sd_inq, SUN_INQSIZE);
2224 2202 sd->sd_inq = (struct scsi_inquiry *)NULL;
2225 2203 }
2226 2204
2227 2205 mutex_destroy(&sd->sd_mutex);
2228 2206 if (tran_clone)
2229 2207 kmem_free(tran_clone, sizeof (scsi_hba_tran_t));
2230 2208 kmem_free(sd, sizeof (*sd));
2231 2209
2232 2210 ddi_set_driver_private(child, NULL);
2233 2211 SCSI_HBA_LOG((_LOG(3), NULL, child, "uninit complete"));
2234 2212 ddi_set_name_addr(child, NULL);
2235 2213 return (DDI_SUCCESS);
2236 2214 }
2237 2215
2238 2216 static int
2239 2217 iport_busctl_ua(dev_info_t *child, char *addr, int maxlen)
2240 2218 {
2241 2219 char *iport_ua;
2242 2220
2243 2221 /* limit ndi_devi_findchild_by_callback to expected flavor */
2244 2222 if (ndi_flavor_get(child) != SCSA_FLAVOR_IPORT)
2245 2223 return (DDI_FAILURE);
2246 2224
2247 2225 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2248 2226 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2249 2227 SCSI_ADDR_PROP_IPORTUA, &iport_ua) != DDI_SUCCESS) {
2250 2228 return (DDI_FAILURE);
2251 2229 }
2252 2230
2253 2231 (void) snprintf(addr, maxlen, "%s", iport_ua);
2254 2232 ddi_prop_free(iport_ua);
2255 2233 return (DDI_SUCCESS);
2256 2234 }
2257 2235
2258 2236 static int
2259 2237 iport_busctl_reportdev(dev_info_t *child)
2260 2238 {
2261 2239 dev_info_t *self = ddi_get_parent(child);
2262 2240 char *iport_ua;
2263 2241 char *initiator_port = NULL;
2264 2242
2265 2243 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2266 2244 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2267 2245 SCSI_ADDR_PROP_IPORTUA, &iport_ua) != DDI_SUCCESS)
2268 2246 return (DDI_FAILURE);
2269 2247
2270 2248 (void) ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
2271 2249 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
2272 2250 SCSI_ADDR_PROP_INITIATOR_PORT, &initiator_port);
2273 2251
2274 2252 if (initiator_port) {
2275 2253 SCSI_HBA_LOG((_LOG_NF(CONT),
2276 2254 "?%s%d at %s%d: %s %s %s %s",
2277 2255 ddi_driver_name(child), ddi_get_instance(child),
2278 2256 ddi_driver_name(self), ddi_get_instance(self),
2279 2257 SCSI_ADDR_PROP_INITIATOR_PORT, initiator_port,
2280 2258 SCSI_ADDR_PROP_IPORTUA, iport_ua));
2281 2259 ddi_prop_free(initiator_port);
2282 2260 } else {
2283 2261 SCSI_HBA_LOG((_LOG_NF(CONT), "?%s%d at %s%d: %s %s",
2284 2262 ddi_driver_name(child), ddi_get_instance(child),
2285 2263 ddi_driver_name(self), ddi_get_instance(self),
2286 2264 SCSI_ADDR_PROP_IPORTUA, iport_ua));
2287 2265 }
2288 2266 ddi_prop_free(iport_ua);
2289 2267 return (DDI_SUCCESS);
2290 2268 }
2291 2269
2292 2270 /* initchild SCSA iport 'child' node */
2293 2271 static int
2294 2272 iport_busctl_initchild(dev_info_t *child)
2295 2273 {
2296 2274 dev_info_t *self = ddi_get_parent(child);
2297 2275 dev_info_t *dup = NULL;
2298 2276 char addr[SCSI_MAXNAMELEN];
2299 2277
2300 2278 if (iport_busctl_ua(child, addr, sizeof (addr)) != DDI_SUCCESS)
2301 2279 return (DDI_NOT_WELL_FORMED);
2302 2280
2303 2281 /* Prevent duplicate nodes. */
2304 2282 dup = ndi_devi_findchild_by_callback(self, ddi_node_name(child), addr,
2305 2283 iport_busctl_ua);
2306 2284 if (dup) {
2307 2285 ASSERT(ndi_flavor_get(dup) == SCSA_FLAVOR_IPORT);
2308 2286 if (ndi_flavor_get(dup) != SCSA_FLAVOR_IPORT) {
2309 2287 SCSI_HBA_LOG((_LOG(1), NULL, child,
2310 2288 "init failed: %s@%s: not IPORT flavored",
2311 2289 ddi_node_name(child), addr));
2312 2290 return (DDI_FAILURE);
2313 2291 }
2314 2292 if (dup != child) {
2315 2293 SCSI_HBA_LOG((_LOG(4), NULL, child,
2316 2294 "init failed: %s@%s: detected duplicate %p",
2317 2295 ddi_node_name(child), addr, (void *)dup));
2318 2296 return (DDI_FAILURE);
2319 2297 }
2320 2298 }
2321 2299
2322 2300 /* set the node @addr string */
2323 2301 ddi_set_name_addr(child, addr);
2324 2302
2325 2303 return (DDI_SUCCESS);
2326 2304 }
2327 2305
2328 2306 /* uninitchild SCSA iport 'child' node */
2329 2307 static int
2330 2308 iport_busctl_uninitchild(dev_info_t *child)
2331 2309 {
2332 2310 ddi_set_name_addr(child, NULL);
2333 2311 return (DDI_SUCCESS);
2334 2312 }
2335 2313
2336 2314 /* Uninitialize scsi_device flavor of transport on SCSA iport 'child' node. */
2337 2315 static void
2338 2316 iport_postdetach_tran_scsi_device(dev_info_t *child)
2339 2317 {
2340 2318 scsi_hba_tran_t *tran;
2341 2319
2342 2320 tran = ndi_flavorv_get(child, SCSA_FLAVOR_SCSI_DEVICE);
2343 2321 if (tran == NULL)
2344 2322 return;
2345 2323
2346 2324 scsa_tran_teardown(child, tran);
2347 2325 scsa_nexus_teardown(child, tran);
2348 2326
2349 2327 ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, NULL);
2350 2328 scsi_hba_tran_free(tran);
2351 2329 }
2352 2330
2353 2331 /* Initialize scsi_device flavor of transport on SCSA iport 'child' node. */
2354 2332 static void
2355 2333 iport_preattach_tran_scsi_device(dev_info_t *child)
2356 2334 {
2357 2335 dev_info_t *hba = ddi_get_parent(child);
2358 2336 scsi_hba_tran_t *htran;
2359 2337 scsi_hba_tran_t *tran;
2360 2338
2361 2339 /* parent HBA node scsi_device tran is required */
2362 2340 htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SCSI_DEVICE);
2363 2341 ASSERT(htran);
2364 2342
2365 2343 /* Allocate iport child's scsi_device transport vector */
2366 2344 tran = scsi_hba_tran_alloc(child, SCSI_HBA_CANSLEEP);
2367 2345 ASSERT(tran);
2368 2346
2369 2347 /* Structure-copy scsi_device transport of HBA to iport. */
2370 2348 *tran = *htran;
2371 2349
2372 2350 /*
2373 2351 * Reset scsi_device transport fields not shared with the
2374 2352 * parent, and not established below.
2375 2353 */
2376 2354 tran->tran_open_flag = 0;
2377 2355 tran->tran_hba_private = NULL;
2378 2356
2379 2357 /* Establish the devinfo context of this tran structure. */
2380 2358 tran->tran_iport_dip = child;
2381 2359
2382 2360 /* Clear SCSI_HBA_SCSA flags (except TA) */
2383 2361 tran->tran_hba_flags &=
2384 2362 ~(SCSI_HBA_SCSA_FM | SCSI_HBA_SCSA_PHCI); /* clear parent state */
2385 2363 tran->tran_hba_flags |= SCSI_HBA_SCSA_TA; /* always TA */
2386 2364 tran->tran_hba_flags &= ~SCSI_HBA_HBA; /* never HBA */
2387 2365
2388 2366 /* Establish flavor of transport (and ddi_get_driver_private()) */
2389 2367 ndi_flavorv_set(child, SCSA_FLAVOR_SCSI_DEVICE, tran);
2390 2368
2391 2369 /* Setup iport node */
2392 2370 if ((scsa_nexus_setup(child, tran) != DDI_SUCCESS) ||
2393 2371 (scsa_tran_setup(child, tran) != DDI_SUCCESS))
2394 2372 iport_postdetach_tran_scsi_device(child);
2395 2373 }
2396 2374
2397 2375 /* Uninitialize smp_device flavor of transport on SCSA iport 'child' node. */
2398 2376 static void
2399 2377 iport_postdetach_tran_smp_device(dev_info_t *child)
2400 2378 {
2401 2379 smp_hba_tran_t *tran;
2402 2380
2403 2381 tran = ndi_flavorv_get(child, SCSA_FLAVOR_SMP);
2404 2382 if (tran == NULL)
2405 2383 return;
2406 2384
2407 2385 ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
2408 2386 smp_hba_tran_free(tran);
2409 2387 }
2410 2388
2411 2389 /* Initialize smp_device flavor of transport on SCSA iport 'child' node. */
2412 2390 static void
2413 2391 iport_preattach_tran_smp_device(dev_info_t *child)
2414 2392 {
2415 2393 dev_info_t *hba = ddi_get_parent(child);
2416 2394 smp_hba_tran_t *htran;
2417 2395 smp_hba_tran_t *tran;
2418 2396
2419 2397 /* parent HBA node smp_device tran is optional */
2420 2398 htran = ndi_flavorv_get(hba, SCSA_FLAVOR_SMP);
2421 2399 if (htran == NULL) {
2422 2400 ndi_flavorv_set(child, SCSA_FLAVOR_SMP, NULL);
2423 2401 return;
2424 2402 }
2425 2403
2426 2404 /* Allocate iport child's smp_device transport vector */
2427 2405 tran = smp_hba_tran_alloc(child);
2428 2406
2429 2407 /* Structure-copy smp_device transport of HBA to iport. */
2430 2408 *tran = *htran;
2431 2409
2432 2410 /* Establish flavor of transport */
2433 2411 ndi_flavorv_set(child, SCSA_FLAVOR_SMP, tran);
2434 2412 }
2435 2413
2436 2414 /*
2437 2415 * Generic bus_ctl operations for SCSI HBA's,
2438 2416 * hiding the busctl interface from the HBA.
2439 2417 */
2440 2418 /*ARGSUSED*/
2441 2419 static int
2442 2420 scsi_hba_bus_ctl(
2443 2421 dev_info_t *self,
2444 2422 dev_info_t *child,
2445 2423 ddi_ctl_enum_t op,
2446 2424 void *arg,
2447 2425 void *result)
2448 2426 {
2449 2427 int child_flavor = 0;
2450 2428 int val;
2451 2429 ddi_dma_attr_t *attr;
2452 2430 scsi_hba_tran_t *tran;
2453 2431 struct attachspec *as;
2454 2432 struct detachspec *ds;
2455 2433
2456 2434 /* For some ops, child is 'arg'. */
2457 2435 if ((op == DDI_CTLOPS_INITCHILD) || (op == DDI_CTLOPS_UNINITCHILD))
2458 2436 child = (dev_info_t *)arg;
2459 2437
2460 2438 /* Determine the flavor of the child: scsi, smp, iport */
2461 2439 child_flavor = ndi_flavor_get(child);
2462 2440
2463 2441 switch (op) {
2464 2442 case DDI_CTLOPS_INITCHILD:
2465 2443 switch (child_flavor) {
2466 2444 case SCSA_FLAVOR_SCSI_DEVICE:
2467 2445 return (scsi_busctl_initchild(child));
2468 2446 case SCSA_FLAVOR_SMP:
2469 2447 return (smp_busctl_initchild(child));
2470 2448 case SCSA_FLAVOR_IPORT:
2471 2449 return (iport_busctl_initchild(child));
2472 2450 default:
2473 2451 return (DDI_FAILURE);
2474 2452 }
2475 2453 /* NOTREACHED */
2476 2454
2477 2455 case DDI_CTLOPS_UNINITCHILD:
2478 2456 switch (child_flavor) {
2479 2457 case SCSA_FLAVOR_SCSI_DEVICE:
2480 2458 return (scsi_busctl_uninitchild(child));
2481 2459 case SCSA_FLAVOR_SMP:
2482 2460 return (smp_busctl_uninitchild(child));
2483 2461 case SCSA_FLAVOR_IPORT:
2484 2462 return (iport_busctl_uninitchild(child));
2485 2463 default:
2486 2464 return (DDI_FAILURE);
2487 2465 }
2488 2466 /* NOTREACHED */
2489 2467
2490 2468 case DDI_CTLOPS_REPORTDEV:
2491 2469 switch (child_flavor) {
2492 2470 case SCSA_FLAVOR_SCSI_DEVICE:
2493 2471 return (scsi_busctl_reportdev(child));
2494 2472 case SCSA_FLAVOR_SMP:
2495 2473 return (smp_busctl_reportdev(child));
2496 2474 case SCSA_FLAVOR_IPORT:
2497 2475 return (iport_busctl_reportdev(child));
2498 2476 default:
2499 2477 return (DDI_FAILURE);
2500 2478 }
2501 2479 /* NOTREACHED */
2502 2480
2503 2481 case DDI_CTLOPS_ATTACH:
2504 2482 as = (struct attachspec *)arg;
2505 2483
2506 2484 if (child_flavor != SCSA_FLAVOR_IPORT)
2507 2485 return (DDI_SUCCESS);
2508 2486
2509 2487 /* iport processing */
2510 2488 if (as->when == DDI_PRE) {
2511 2489 /* setup pre attach(9E) */
2512 2490 iport_preattach_tran_scsi_device(child);
2513 2491 iport_preattach_tran_smp_device(child);
2514 2492 } else if ((as->when == DDI_POST) &&
2515 2493 (as->result != DDI_SUCCESS)) {
2516 2494 /* cleanup if attach(9E) failed */
2517 2495 iport_postdetach_tran_scsi_device(child);
2518 2496 iport_postdetach_tran_smp_device(child);
2519 2497 }
2520 2498 return (DDI_SUCCESS);
2521 2499
2522 2500 case DDI_CTLOPS_DETACH:
2523 2501 ds = (struct detachspec *)arg;
2524 2502
2525 2503 if (child_flavor != SCSA_FLAVOR_IPORT)
2526 2504 return (DDI_SUCCESS);
2527 2505
2528 2506 /* iport processing */
2529 2507 if ((ds->when == DDI_POST) &&
2530 2508 (ds->result == DDI_SUCCESS)) {
2531 2509 /* cleanup if detach(9E) was successful */
2532 2510 iport_postdetach_tran_scsi_device(child);
2533 2511 iport_postdetach_tran_smp_device(child);
2534 2512 }
2535 2513 return (DDI_SUCCESS);
2536 2514
2537 2515 case DDI_CTLOPS_IOMIN:
2538 2516 tran = ddi_get_driver_private(self);
2539 2517 ASSERT(tran);
2540 2518 if (tran == NULL)
2541 2519 return (DDI_FAILURE);
2542 2520
2543 2521 /*
2544 2522 * The 'arg' value of nonzero indicates 'streaming'
2545 2523 * mode. If in streaming mode, pick the largest
2546 2524 * of our burstsizes available and say that that
2547 2525 * is our minimum value (modulo what minxfer is).
2548 2526 */
2549 2527 attr = &tran->tran_dma_attr;
2550 2528 val = *((int *)result);
2551 2529 val = maxbit(val, attr->dma_attr_minxfer);
2552 2530 *((int *)result) = maxbit(val, ((intptr_t)arg ?
2553 2531 (1<<ddi_ffs(attr->dma_attr_burstsizes)-1) :
2554 2532 (1<<(ddi_fls(attr->dma_attr_burstsizes)-1))));
2555 2533
2556 2534 return (ddi_ctlops(self, child, op, arg, result));
2557 2535
2558 2536 case DDI_CTLOPS_SIDDEV:
2559 2537 return (ndi_dev_is_persistent_node(child) ?
2560 2538 DDI_SUCCESS : DDI_FAILURE);
2561 2539
2562 2540 case DDI_CTLOPS_POWER:
2563 2541 return (DDI_SUCCESS);
2564 2542
2565 2543 /*
2566 2544 * These ops correspond to functions that "shouldn't" be called
2567 2545 * by a SCSI target driver. So we whine when we're called.
2568 2546 */
2569 2547 case DDI_CTLOPS_DMAPMAPC:
2570 2548 case DDI_CTLOPS_REPORTINT:
2571 2549 case DDI_CTLOPS_REGSIZE:
2572 2550 case DDI_CTLOPS_NREGS:
2573 2551 case DDI_CTLOPS_SLAVEONLY:
2574 2552 case DDI_CTLOPS_AFFINITY:
2575 2553 case DDI_CTLOPS_POKE:
2576 2554 case DDI_CTLOPS_PEEK:
2577 2555 SCSI_HBA_LOG((_LOG(WARN), self, NULL, "invalid op (%d)", op));
2578 2556 return (DDI_FAILURE);
2579 2557
2580 2558 /* Everything else we pass up */
2581 2559 case DDI_CTLOPS_PTOB:
2582 2560 case DDI_CTLOPS_BTOP:
2583 2561 case DDI_CTLOPS_BTOPR:
2584 2562 case DDI_CTLOPS_DVMAPAGESIZE:
2585 2563 default:
2586 2564 return (ddi_ctlops(self, child, op, arg, result));
2587 2565 }
2588 2566 /* NOTREACHED */
2589 2567 }
2590 2568
2591 2569 /*
2592 2570 * Private wrapper for scsi_pkt's allocated via scsi_hba_pkt_alloc()
2593 2571 */
2594 2572 struct scsi_pkt_wrapper {
2595 2573 struct scsi_pkt scsi_pkt;
2596 2574 int pkt_wrapper_magic;
2597 2575 int pkt_wrapper_len;
2598 2576 };
2599 2577
2600 2578 #if !defined(lint)
2601 2579 _NOTE(SCHEME_PROTECTS_DATA("unique per thread", scsi_pkt_wrapper))
2602 2580 _NOTE(SCHEME_PROTECTS_DATA("Unshared Data", dev_ops))
2603 2581 #endif
2604 2582
2605 2583 /*
2606 2584 * Called by an HBA to allocate a scsi_pkt
2607 2585 */
2608 2586 /*ARGSUSED*/
2609 2587 struct scsi_pkt *
2610 2588 scsi_hba_pkt_alloc(
2611 2589 dev_info_t *self,
2612 2590 struct scsi_address *ap,
2613 2591 int cmdlen,
2614 2592 int statuslen,
2615 2593 int tgtlen,
2616 2594 int hbalen,
2617 2595 int (*callback)(caddr_t arg),
2618 2596 caddr_t arg)
2619 2597 {
2620 2598 struct scsi_pkt *pkt;
2621 2599 struct scsi_pkt_wrapper *hba_pkt;
2622 2600 caddr_t p;
2623 2601 int acmdlen, astatuslen, atgtlen, ahbalen;
2624 2602 int pktlen;
2625 2603
2626 2604 /* Sanity check */
2627 2605 if (callback != SLEEP_FUNC && callback != NULL_FUNC)
2628 2606 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2629 2607 "callback must be SLEEP_FUNC or NULL_FUNC"));
2630 2608
2631 2609 /*
2632 2610 * Round up so everything gets allocated on long-word boundaries
2633 2611 */
2634 2612 acmdlen = ROUNDUP(cmdlen);
2635 2613 astatuslen = ROUNDUP(statuslen);
2636 2614 atgtlen = ROUNDUP(tgtlen);
2637 2615 ahbalen = ROUNDUP(hbalen);
2638 2616 pktlen = sizeof (struct scsi_pkt_wrapper) +
2639 2617 acmdlen + astatuslen + atgtlen + ahbalen;
2640 2618
2641 2619 hba_pkt = kmem_zalloc(pktlen,
2642 2620 (callback == SLEEP_FUNC) ? KM_SLEEP : KM_NOSLEEP);
2643 2621 if (hba_pkt == NULL) {
2644 2622 ASSERT(callback == NULL_FUNC);
2645 2623 return (NULL);
2646 2624 }
2647 2625
2648 2626 /*
2649 2627 * Set up our private info on this pkt
2650 2628 */
2651 2629 hba_pkt->pkt_wrapper_len = pktlen;
2652 2630 hba_pkt->pkt_wrapper_magic = PKT_WRAPPER_MAGIC; /* alloced correctly */
2653 2631 pkt = &hba_pkt->scsi_pkt;
2654 2632
2655 2633 /*
2656 2634 * Set up pointers to private data areas, cdb, and status.
2657 2635 */
2658 2636 p = (caddr_t)(hba_pkt + 1);
2659 2637 if (hbalen > 0) {
2660 2638 pkt->pkt_ha_private = (opaque_t)p;
2661 2639 p += ahbalen;
2662 2640 }
2663 2641 if (tgtlen > 0) {
2664 2642 pkt->pkt_private = (opaque_t)p;
2665 2643 p += atgtlen;
2666 2644 }
2667 2645 if (statuslen > 0) {
2668 2646 pkt->pkt_scbp = (uchar_t *)p;
2669 2647 p += astatuslen;
2670 2648 }
2671 2649 if (cmdlen > 0) {
2672 2650 pkt->pkt_cdbp = (uchar_t *)p;
2673 2651 }
2674 2652
2675 2653 /*
2676 2654 * Initialize the pkt's scsi_address
2677 2655 */
2678 2656 pkt->pkt_address = *ap;
2679 2657
2680 2658 /*
2681 2659 * NB: It may not be safe for drivers, esp target drivers, to depend
2682 2660 * on the following fields being set until all the scsi_pkt
2683 2661 * allocation violations discussed in scsi_pkt.h are all resolved.
2684 2662 */
2685 2663 pkt->pkt_cdblen = cmdlen;
2686 2664 pkt->pkt_tgtlen = tgtlen;
2687 2665 pkt->pkt_scblen = statuslen;
2688 2666
2689 2667 return (pkt);
2690 2668 }
2691 2669
2692 2670 /*
2693 2671 * Called by an HBA to free a scsi_pkt
2694 2672 */
2695 2673 /*ARGSUSED*/
2696 2674 void
2697 2675 scsi_hba_pkt_free(
2698 2676 struct scsi_address *ap,
2699 2677 struct scsi_pkt *pkt)
2700 2678 {
2701 2679 kmem_free(pkt, ((struct scsi_pkt_wrapper *)pkt)->pkt_wrapper_len);
2702 2680 }
2703 2681
2704 2682 /*
2705 2683 * Return 1 if the scsi_pkt used a proper allocator.
2706 2684 *
2707 2685 * The DDI does not allow a driver to allocate it's own scsi_pkt(9S), a
2708 2686 * driver should not have *any* compiled in dependencies on "sizeof (struct
2709 2687 * scsi_pkt)". While this has been the case for many years, a number of
2710 2688 * drivers have still not been fixed. This function can be used to detect
2711 2689 * improperly allocated scsi_pkt structures, and produce messages identifying
2712 2690 * drivers that need to be fixed.
2713 2691 *
2714 2692 * While drivers in violation are being fixed, this function can also
2715 2693 * be used by the framework to detect packets that violated allocation
2716 2694 * rules.
2717 2695 *
2718 2696 * NB: It is possible, but very unlikely, for this code to return a false
2719 2697 * positive (finding correct magic, but for wrong reasons). Careful
2720 2698 * consideration is needed for callers using this interface to condition
2721 2699 * access to newer scsi_pkt fields (those after pkt_reason).
2722 2700 *
2723 2701 * NB: As an aid to minimizing the amount of work involved in 'fixing' legacy
2724 2702 * drivers that violate scsi_*(9S) allocation rules, private
2725 2703 * scsi_pkt_size()/scsi_size_clean() functions are available (see their
2726 2704 * implementation for details).
2727 2705 *
2728 2706 * *** Non-legacy use of scsi_pkt_size() is discouraged. ***
2729 2707 *
2730 2708 * NB: When supporting broken HBA drivers is not longer a concern, this
2731 2709 * code should be removed.
2732 2710 */
2733 2711 int
2734 2712 scsi_pkt_allocated_correctly(struct scsi_pkt *pkt)
2735 2713 {
2736 2714 struct scsi_pkt_wrapper *hba_pkt = (struct scsi_pkt_wrapper *)pkt;
2737 2715 int magic;
2738 2716 major_t major;
2739 2717 #ifdef DEBUG
2740 2718 int *pspwm, *pspcwm;
2741 2719
2742 2720 /*
2743 2721 * We are getting scsi packets from two 'correct' wrapper schemes,
2744 2722 * make sure we are looking at the same place in both to detect
2745 2723 * proper allocation.
2746 2724 */
2747 2725 pspwm = &((struct scsi_pkt_wrapper *)0)->pkt_wrapper_magic;
2748 2726 pspcwm = &((struct scsi_pkt_cache_wrapper *)0)->pcw_magic;
2749 2727 ASSERT(pspwm == pspcwm);
2750 2728 #endif /* DEBUG */
2751 2729
2752 2730
2753 2731 /*
2754 2732 * Check to see if driver is scsi_size_clean(), assume it
2755 2733 * is using the scsi_pkt_size() interface everywhere it needs to
2756 2734 * if the driver indicates it is scsi_size_clean().
2757 2735 */
2758 2736 major = ddi_driver_major(P_TO_TRAN(pkt)->tran_hba_dip);
2759 2737 if (devnamesp[major].dn_flags & DN_SCSI_SIZE_CLEAN)
2760 2738 return (1); /* ok */
2761 2739
2762 2740 /*
2763 2741 * Special case crossing a page boundary. If the scsi_pkt was not
2764 2742 * allocated correctly, then across a page boundary we have a
2765 2743 * fault hazard.
2766 2744 */
2767 2745 if ((((uintptr_t)(&hba_pkt->scsi_pkt)) & MMU_PAGEMASK) ==
2768 2746 (((uintptr_t)(&hba_pkt->pkt_wrapper_magic)) & MMU_PAGEMASK)) {
2769 2747 /* fastpath, no cross-page hazard */
2770 2748 magic = hba_pkt->pkt_wrapper_magic;
2771 2749 } else {
2772 2750 /* add protection for cross-page hazard */
2773 2751 if (ddi_peek32((dev_info_t *)NULL,
2774 2752 &hba_pkt->pkt_wrapper_magic, &magic) == DDI_FAILURE) {
2775 2753 return (0); /* violation */
2776 2754 }
2777 2755 }
2778 2756
2779 2757 /* properly allocated packet always has correct magic */
2780 2758 return ((magic == PKT_WRAPPER_MAGIC) ? 1 : 0);
2781 2759 }
2782 2760
2783 2761 /*
2784 2762 * Private interfaces to simplify conversion of legacy drivers so they don't
2785 2763 * depend on scsi_*(9S) size. Instead of using these private interface, HBA
2786 2764 * drivers should use DDI sanctioned allocation methods:
2787 2765 *
2788 2766 * scsi_pkt Use scsi_hba_pkt_alloc(9F), or implement
2789 2767 * tran_setup_pkt(9E).
2790 2768 *
2791 2769 * scsi_device You are doing something strange/special, a scsi_device
2792 2770 * structure should only be allocated by scsi_hba.c
2793 2771 * initchild code or scsi_vhci.c code.
2794 2772 *
2795 2773 * scsi_hba_tran Use scsi_hba_tran_alloc(9F).
2796 2774 */
2797 2775 size_t
2798 2776 scsi_pkt_size()
2799 2777 {
2800 2778 return (sizeof (struct scsi_pkt));
2801 2779 }
2802 2780
2803 2781 size_t
2804 2782 scsi_hba_tran_size()
2805 2783 {
2806 2784 return (sizeof (scsi_hba_tran_t));
2807 2785 }
2808 2786
2809 2787 size_t
2810 2788 scsi_device_size()
2811 2789 {
2812 2790 return (sizeof (struct scsi_device));
2813 2791 }
2814 2792
2815 2793 /*
2816 2794 * Legacy compliance to scsi_pkt(9S) allocation rules through use of
2817 2795 * scsi_pkt_size() is detected by the 'scsi-size-clean' driver.conf property
2818 2796 * or an HBA driver calling to scsi_size_clean() from attach(9E). A driver
2819 2797 * developer should only indicate that a legacy driver is clean after using
2820 2798 * SCSI_SIZE_CLEAN_VERIFY to ensure compliance (see scsi_pkt.h).
2821 2799 */
2822 2800 void
2823 2801 scsi_size_clean(dev_info_t *self)
2824 2802 {
2825 2803 major_t major;
2826 2804 struct devnames *dnp;
2827 2805
2828 2806 ASSERT(self);
2829 2807 major = ddi_driver_major(self);
2830 2808 ASSERT(major < devcnt);
2831 2809 if (major >= devcnt) {
2832 2810 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
2833 2811 "scsi_pkt_size: bogus major: %d", major));
2834 2812 return;
2835 2813 }
2836 2814
2837 2815 /* Set DN_SCSI_SIZE_CLEAN flag in dn_flags. */
2838 2816 dnp = &devnamesp[major];
2839 2817 if ((dnp->dn_flags & DN_SCSI_SIZE_CLEAN) == 0) {
2840 2818 LOCK_DEV_OPS(&dnp->dn_lock);
2841 2819 dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
2842 2820 UNLOCK_DEV_OPS(&dnp->dn_lock);
2843 2821 }
2844 2822 }
2845 2823
2846 2824
2847 2825 /*
2848 2826 * Called by an HBA to map strings to capability indices
2849 2827 */
2850 2828 int
2851 2829 scsi_hba_lookup_capstr(
2852 2830 char *capstr)
2853 2831 {
2854 2832 /*
2855 2833 * Capability strings: only add entries to mask the legacy
2856 2834 * '_' vs. '-' misery. All new capabilities should use '-',
2857 2835 * and be captured be added to SCSI_CAP_ASCII.
2858 2836 */
2859 2837 static struct cap_strings {
2860 2838 char *cap_string;
2861 2839 int cap_index;
2862 2840 } cap_strings[] = {
2863 2841 { "dma_max", SCSI_CAP_DMA_MAX },
2864 2842 { "msg_out", SCSI_CAP_MSG_OUT },
2865 2843 { "wide_xfer", SCSI_CAP_WIDE_XFER },
2866 2844 { NULL, 0 }
2867 2845 };
2868 2846 static char *cap_ascii[] = SCSI_CAP_ASCII;
2869 2847 char **cap;
2870 2848 int i;
2871 2849 struct cap_strings *cp;
2872 2850
2873 2851 for (cap = cap_ascii, i = 0; *cap != NULL; cap++, i++)
2874 2852 if (strcmp(*cap, capstr) == 0)
2875 2853 return (i);
2876 2854
2877 2855 for (cp = cap_strings; cp->cap_string != NULL; cp++)
2878 2856 if (strcmp(cp->cap_string, capstr) == 0)
2879 2857 return (cp->cap_index);
2880 2858
2881 2859 return (-1);
2882 2860 }
2883 2861
2884 2862 /*
2885 2863 * Called by an HBA to determine if the system is in 'panic' state.
2886 2864 */
2887 2865 int
2888 2866 scsi_hba_in_panic()
2889 2867 {
2890 2868 return (panicstr != NULL);
2891 2869 }
2892 2870
2893 2871 /*
2894 2872 * If a SCSI target driver attempts to mmap memory,
2895 2873 * the buck stops here.
2896 2874 */
2897 2875 /*ARGSUSED*/
2898 2876 static int
2899 2877 scsi_hba_map_fault(
2900 2878 dev_info_t *self,
2901 2879 dev_info_t *child,
2902 2880 struct hat *hat,
2903 2881 struct seg *seg,
2904 2882 caddr_t addr,
2905 2883 struct devpage *dp,
2906 2884 pfn_t pfn,
2907 2885 uint_t prot,
2908 2886 uint_t lock)
2909 2887 {
2910 2888 return (DDI_FAILURE);
2911 2889 }
2912 2890
2913 2891 static int
2914 2892 scsi_hba_get_eventcookie(
2915 2893 dev_info_t *self,
2916 2894 dev_info_t *child,
2917 2895 char *name,
2918 2896 ddi_eventcookie_t *eventp)
2919 2897 {
2920 2898 scsi_hba_tran_t *tran;
2921 2899
2922 2900 tran = ddi_get_driver_private(self);
2923 2901 if (tran->tran_get_eventcookie &&
2924 2902 ((*tran->tran_get_eventcookie)(self,
2925 2903 child, name, eventp) == DDI_SUCCESS)) {
2926 2904 return (DDI_SUCCESS);
2927 2905 }
2928 2906
2929 2907 return (ndi_busop_get_eventcookie(self, child, name, eventp));
2930 2908 }
2931 2909
2932 2910 static int
2933 2911 scsi_hba_add_eventcall(
2934 2912 dev_info_t *self,
2935 2913 dev_info_t *child,
2936 2914 ddi_eventcookie_t event,
2937 2915 void (*callback)(
2938 2916 dev_info_t *self,
2939 2917 ddi_eventcookie_t event,
2940 2918 void *arg,
2941 2919 void *bus_impldata),
2942 2920 void *arg,
2943 2921 ddi_callback_id_t *cb_id)
2944 2922 {
2945 2923 scsi_hba_tran_t *tran;
2946 2924
2947 2925 tran = ddi_get_driver_private(self);
2948 2926 if (tran->tran_add_eventcall &&
2949 2927 ((*tran->tran_add_eventcall)(self, child,
2950 2928 event, callback, arg, cb_id) == DDI_SUCCESS)) {
2951 2929 return (DDI_SUCCESS);
2952 2930 }
2953 2931
2954 2932 return (DDI_FAILURE);
2955 2933 }
2956 2934
2957 2935 static int
2958 2936 scsi_hba_remove_eventcall(dev_info_t *self, ddi_callback_id_t cb_id)
2959 2937 {
2960 2938 scsi_hba_tran_t *tran;
2961 2939 ASSERT(cb_id);
2962 2940
2963 2941 tran = ddi_get_driver_private(self);
2964 2942 if (tran->tran_remove_eventcall &&
2965 2943 ((*tran->tran_remove_eventcall)(
2966 2944 self, cb_id) == DDI_SUCCESS)) {
2967 2945 return (DDI_SUCCESS);
2968 2946 }
2969 2947
2970 2948 return (DDI_FAILURE);
2971 2949 }
2972 2950
2973 2951 static int
2974 2952 scsi_hba_post_event(
2975 2953 dev_info_t *self,
2976 2954 dev_info_t *child,
2977 2955 ddi_eventcookie_t event,
2978 2956 void *bus_impldata)
2979 2957 {
2980 2958 scsi_hba_tran_t *tran;
2981 2959
2982 2960 tran = ddi_get_driver_private(self);
2983 2961 if (tran->tran_post_event &&
2984 2962 ((*tran->tran_post_event)(self,
2985 2963 child, event, bus_impldata) == DDI_SUCCESS)) {
2986 2964 return (DDI_SUCCESS);
2987 2965 }
2988 2966
2989 2967 return (DDI_FAILURE);
2990 2968 }
2991 2969
2992 2970 /*
2993 2971 * Default getinfo(9e) for scsi_hba
2994 2972 */
2995 2973 /* ARGSUSED */
2996 2974 static int
2997 2975 scsi_hba_info(dev_info_t *self, ddi_info_cmd_t infocmd, void *arg,
2998 2976 void **result)
2999 2977 {
3000 2978 int error = DDI_SUCCESS;
3001 2979
3002 2980 switch (infocmd) {
3003 2981 case DDI_INFO_DEVT2INSTANCE:
3004 2982 *result = (void *)(intptr_t)(MINOR2INST(getminor((dev_t)arg)));
3005 2983 break;
3006 2984 default:
3007 2985 error = DDI_FAILURE;
3008 2986 }
3009 2987 return (error);
3010 2988 }
3011 2989
3012 2990 /*
3013 2991 * Default open and close routine for scsi_hba
3014 2992 */
3015 2993 /* ARGSUSED */
3016 2994 int
3017 2995 scsi_hba_open(dev_t *devp, int flags, int otyp, cred_t *credp)
3018 2996 {
3019 2997 dev_info_t *self;
3020 2998 scsi_hba_tran_t *tran;
3021 2999 int rv = 0;
3022 3000
3023 3001 if (otyp != OTYP_CHR)
3024 3002 return (EINVAL);
3025 3003
3026 3004 if ((self = e_ddi_hold_devi_by_dev(*devp, 0)) == NULL)
3027 3005 return (ENXIO);
3028 3006
3029 3007 tran = ddi_get_driver_private(self);
3030 3008 if (tran == NULL) {
3031 3009 ddi_release_devi(self);
3032 3010 return (ENXIO);
3033 3011 }
3034 3012
3035 3013 /*
3036 3014 * tran_open_flag bit field:
3037 3015 * 0: closed
3038 3016 * 1: shared open by minor at bit position
3039 3017 * 1 at 31st bit: exclusive open
3040 3018 */
3041 3019 mutex_enter(&(tran->tran_open_lock));
3042 3020 if (flags & FEXCL) {
3043 3021 if (tran->tran_open_flag != 0) {
3044 3022 rv = EBUSY; /* already open */
3045 3023 } else {
3046 3024 tran->tran_open_flag = TRAN_OPEN_EXCL;
3047 3025 }
3048 3026 } else {
3049 3027 if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
3050 3028 rv = EBUSY; /* already excl. open */
3051 3029 } else {
3052 3030 int minor = getminor(*devp) & TRAN_MINOR_MASK;
3053 3031 tran->tran_open_flag |= (1 << minor);
3054 3032 /*
3055 3033 * Ensure that the last framework reserved minor
3056 3034 * is unused. Otherwise, the exclusive open
3057 3035 * mechanism may break.
3058 3036 */
3059 3037 ASSERT(minor != 31);
3060 3038 }
3061 3039 }
3062 3040 mutex_exit(&(tran->tran_open_lock));
3063 3041
3064 3042 ddi_release_devi(self);
3065 3043 return (rv);
3066 3044 }
3067 3045
3068 3046 /* ARGSUSED */
3069 3047 int
3070 3048 scsi_hba_close(dev_t dev, int flag, int otyp, cred_t *credp)
3071 3049 {
3072 3050 dev_info_t *self;
3073 3051 scsi_hba_tran_t *tran;
3074 3052
3075 3053 if (otyp != OTYP_CHR)
3076 3054 return (EINVAL);
3077 3055
3078 3056 if ((self = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
3079 3057 return (ENXIO);
3080 3058
3081 3059 tran = ddi_get_driver_private(self);
3082 3060 if (tran == NULL) {
3083 3061 ddi_release_devi(self);
3084 3062 return (ENXIO);
3085 3063 }
3086 3064
3087 3065 mutex_enter(&(tran->tran_open_lock));
3088 3066 if (tran->tran_open_flag == TRAN_OPEN_EXCL) {
3089 3067 tran->tran_open_flag = 0;
3090 3068 } else {
3091 3069 int minor = getminor(dev) & TRAN_MINOR_MASK;
3092 3070 tran->tran_open_flag &= ~(1 << minor);
3093 3071 }
3094 3072 mutex_exit(&(tran->tran_open_lock));
3095 3073
3096 3074 ddi_release_devi(self);
3097 3075 return (0);
3098 3076 }
3099 3077
3100 3078 /*
3101 3079 * standard ioctl commands for SCSI hotplugging
3102 3080 */
3103 3081 /* ARGSUSED */
3104 3082 int
3105 3083 scsi_hba_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
3106 3084 int *rvalp)
3107 3085 {
3108 3086 dev_info_t *self;
3109 3087 struct devctl_iocdata *dcp = NULL;
3110 3088 dev_info_t *child = NULL;
3111 3089 mdi_pathinfo_t *path = NULL;
3112 3090 struct scsi_device *sd;
3113 3091 scsi_hba_tran_t *tran;
3114 3092 uint_t bus_state;
3115 3093 int rv = 0;
3116 3094 int circ;
3117 3095 char *name;
3118 3096 char *addr;
3119 3097
3120 3098 self = e_ddi_hold_devi_by_dev(dev, 0);
3121 3099 if (self == NULL) {
3122 3100 rv = ENXIO;
3123 3101 goto out;
3124 3102 }
3125 3103
3126 3104 tran = ddi_get_driver_private(self);
3127 3105 if (tran == NULL) {
3128 3106 rv = ENXIO;
3129 3107 goto out;
3130 3108 }
3131 3109
3132 3110 /* Ioctls for which the generic implementation suffices. */
3133 3111 switch (cmd) {
3134 3112 case DEVCTL_BUS_GETSTATE:
3135 3113 rv = ndi_devctl_ioctl(self, cmd, arg, mode, 0);
3136 3114 goto out;
3137 3115 }
3138 3116
3139 3117 /* read devctl ioctl data */
3140 3118 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) {
3141 3119 rv = EFAULT;
3142 3120 goto out;
3143 3121 }
3144 3122
3145 3123 /* Ioctls that require child identification */
3146 3124 switch (cmd) {
3147 3125 case DEVCTL_DEVICE_GETSTATE:
3148 3126 case DEVCTL_DEVICE_ONLINE:
3149 3127 case DEVCTL_DEVICE_OFFLINE:
3150 3128 case DEVCTL_DEVICE_REMOVE:
3151 3129 case DEVCTL_DEVICE_RESET:
3152 3130 name = ndi_dc_getname(dcp);
3153 3131 addr = ndi_dc_getaddr(dcp);
3154 3132 if ((name == NULL) || (addr == NULL)) {
3155 3133 rv = EINVAL;
3156 3134 goto out;
3157 3135 }
3158 3136
3159 3137 /*
3160 3138 * Find child with name@addr - might find a devinfo
3161 3139 * child (child), a pathinfo child (path), or nothing.
3162 3140 */
3163 3141 scsi_hba_devi_enter(self, &circ);
3164 3142
3165 3143 (void) scsi_findchild(self, name, addr, 1, &child, &path, NULL);
3166 3144 if (path) {
3167 3145 /* Found a pathinfo */
3168 3146 ASSERT(path && (child == NULL));
3169 3147 mdi_hold_path(path);
3170 3148 scsi_hba_devi_exit_phci(self, circ);
3171 3149 sd = NULL;
3172 3150 } else if (child) {
3173 3151 /* Found a devinfo */
3174 3152 ASSERT(child && (path == NULL));
3175 3153
3176 3154 /* verify scsi_device of child */
3177 3155 if (ndi_flavor_get(child) == SCSA_FLAVOR_SCSI_DEVICE)
3178 3156 sd = ddi_get_driver_private(child);
3179 3157 else
3180 3158 sd = NULL;
3181 3159 } else {
3182 3160 ASSERT((path == NULL) && (child == NULL));
3183 3161 scsi_hba_devi_exit(self, circ);
3184 3162 rv = ENXIO; /* found nothing */
3185 3163 goto out;
3186 3164 }
3187 3165 break;
3188 3166
3189 3167 case DEVCTL_BUS_RESETALL: /* ioctl that operate on any child */
3190 3168 /*
3191 3169 * Find a child's scsi_address so we can invoke tran_reset.
3192 3170 *
3193 3171 * Future: If no child exists, we could fake a child. This will
3194 3172 * be a enhancement for the future - for now, we fall back to
3195 3173 * BUS_RESET.
3196 3174 */
3197 3175 scsi_hba_devi_enter(self, &circ);
3198 3176 child = ddi_get_child(self);
3199 3177 sd = NULL;
3200 3178 while (child) {
3201 3179 /* verify scsi_device of child */
3202 3180 if (ndi_flavor_get(child) == SCSA_FLAVOR_SCSI_DEVICE)
3203 3181 sd = ddi_get_driver_private(child);
3204 3182 if (sd != NULL) {
3205 3183 /*
3206 3184 * NOTE: node has a scsi_device structure, so
3207 3185 * it must be initialized.
3208 3186 */
3209 3187 ndi_hold_devi(child);
3210 3188 break;
3211 3189 }
3212 3190 child = ddi_get_next_sibling(child);
3213 3191 }
3214 3192 scsi_hba_devi_exit(self, circ);
3215 3193 break;
3216 3194 }
3217 3195
3218 3196 switch (cmd) {
3219 3197 case DEVCTL_DEVICE_GETSTATE:
3220 3198 if (path) {
3221 3199 if (mdi_dc_return_dev_state(path, dcp) != MDI_SUCCESS)
3222 3200 rv = EFAULT;
3223 3201 } else if (child) {
3224 3202 if (ndi_dc_return_dev_state(child, dcp) != NDI_SUCCESS)
3225 3203 rv = EFAULT;
3226 3204 } else {
3227 3205 rv = ENXIO;
3228 3206 }
3229 3207 break;
3230 3208
3231 3209 case DEVCTL_DEVICE_RESET:
3232 3210 if (sd == NULL) {
3233 3211 rv = ENOTTY;
3234 3212 break;
3235 3213 }
3236 3214 if (tran->tran_reset == NULL) {
3237 3215 rv = ENOTSUP;
3238 3216 break;
3239 3217 }
3240 3218
3241 3219 /* Start with the small stick */
3242 3220 if (scsi_reset(&sd->sd_address, RESET_LUN) == 1)
3243 3221 break; /* LUN reset worked */
3244 3222 if (scsi_reset(&sd->sd_address, RESET_TARGET) != 1)
3245 3223 rv = EIO; /* Target reset failed */
3246 3224 break;
3247 3225
3248 3226 case DEVCTL_BUS_QUIESCE:
3249 3227 if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
3250 3228 (bus_state == BUS_QUIESCED))
3251 3229 rv = EALREADY;
3252 3230 else if (tran->tran_quiesce == NULL)
3253 3231 rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3254 3232 else if (tran->tran_quiesce(self) != 0)
3255 3233 rv = EIO;
3256 3234 else if (ndi_set_bus_state(self, BUS_QUIESCED) != NDI_SUCCESS)
3257 3235 rv = EIO;
3258 3236 break;
3259 3237
3260 3238 case DEVCTL_BUS_UNQUIESCE:
3261 3239 if ((ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS) &&
3262 3240 (bus_state == BUS_ACTIVE))
3263 3241 rv = EALREADY;
3264 3242 else if (tran->tran_unquiesce == NULL)
3265 3243 rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3266 3244 else if (tran->tran_unquiesce(self) != 0)
3267 3245 rv = EIO;
3268 3246 else if (ndi_set_bus_state(self, BUS_ACTIVE) != NDI_SUCCESS)
3269 3247 rv = EIO;
3270 3248 break;
3271 3249
3272 3250 case DEVCTL_BUS_RESET:
3273 3251 if (tran->tran_bus_reset == NULL)
3274 3252 rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3275 3253 else if (tran->tran_bus_reset(self, RESET_BUS) != 1)
3276 3254 rv = EIO;
3277 3255 break;
3278 3256
3279 3257 case DEVCTL_BUS_RESETALL:
3280 3258 if ((sd != NULL) &&
3281 3259 (scsi_reset(&sd->sd_address, RESET_ALL) == 1)) {
3282 3260 break; /* reset all worked */
3283 3261 }
3284 3262 if (tran->tran_bus_reset == NULL) {
3285 3263 rv = ENOTSUP; /* man ioctl(7I) says ENOTTY */
3286 3264 break;
3287 3265 }
3288 3266 if (tran->tran_bus_reset(self, RESET_BUS) != 1)
3289 3267 rv = EIO; /* bus reset failed */
3290 3268 break;
3291 3269
3292 3270 case DEVCTL_BUS_CONFIGURE:
3293 3271 if (ndi_devi_config(self, NDI_DEVFS_CLEAN | NDI_DEVI_PERSIST |
3294 3272 NDI_CONFIG_REPROBE) != NDI_SUCCESS) {
3295 3273 rv = EIO;
3296 3274 }
3297 3275 break;
3298 3276
3299 3277 case DEVCTL_BUS_UNCONFIGURE:
3300 3278 if (ndi_devi_unconfig(self,
3301 3279 NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) != NDI_SUCCESS) {
3302 3280 rv = EBUSY;
3303 3281 }
3304 3282 break;
3305 3283
3306 3284 case DEVCTL_DEVICE_ONLINE:
3307 3285 ASSERT(child || path);
3308 3286 if (path) {
3309 3287 if (mdi_pi_online(path, NDI_USER_REQ) != MDI_SUCCESS)
3310 3288 rv = EIO;
3311 3289 } else {
3312 3290 if (ndi_devi_online(child, 0) != NDI_SUCCESS)
3313 3291 rv = EIO;
3314 3292 }
3315 3293 break;
3316 3294
3317 3295 case DEVCTL_DEVICE_OFFLINE:
3318 3296 ASSERT(child || path);
3319 3297 if (sd != NULL)
3320 3298 (void) scsi_clear_task_set(&sd->sd_address);
3321 3299 if (path) {
3322 3300 if (mdi_pi_offline(path, NDI_USER_REQ) != MDI_SUCCESS)
3323 3301 rv = EIO;
3324 3302 } else {
3325 3303 if (ndi_devi_offline(child,
3326 3304 NDI_DEVFS_CLEAN) != NDI_SUCCESS)
3327 3305 rv = EIO;
3328 3306 }
3329 3307 break;
3330 3308
3331 3309 case DEVCTL_DEVICE_REMOVE:
3332 3310 ASSERT(child || path);
3333 3311 if (sd != NULL)
3334 3312 (void) scsi_clear_task_set(&sd->sd_address);
3335 3313 if (path) {
3336 3314 /* NOTE: don't pass NDI_DEVI_REMOVE to mdi_pi_offline */
3337 3315 if (mdi_pi_offline(path, NDI_USER_REQ) == MDI_SUCCESS) {
3338 3316 scsi_hba_devi_enter_phci(self, &circ);
3339 3317 mdi_rele_path(path);
3340 3318
3341 3319 /* ... here is the DEVICE_REMOVE part. */
3342 3320 (void) mdi_pi_free(path, 0);
3343 3321 path = NULL;
3344 3322 } else {
3345 3323 rv = EIO;
3346 3324 }
3347 3325 } else {
3348 3326 if (ndi_devi_offline(child,
3349 3327 NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) != NDI_SUCCESS)
3350 3328 rv = EIO;
3351 3329 }
3352 3330 break;
3353 3331
3354 3332 default:
3355 3333 ASSERT(dcp != NULL);
3356 3334 rv = ENOTTY;
3357 3335 break;
3358 3336 }
3359 3337
3360 3338 /* all done -- clean up and return */
3361 3339 out:
3362 3340 /* release hold on what we found */
3363 3341 if (path) {
3364 3342 scsi_hba_devi_enter_phci(self, &circ);
3365 3343 mdi_rele_path(path);
3366 3344 }
3367 3345 if (path || child)
3368 3346 scsi_hba_devi_exit(self, circ);
3369 3347
3370 3348 if (dcp)
3371 3349 ndi_dc_freehdl(dcp);
3372 3350
3373 3351 if (self)
3374 3352 ddi_release_devi(self);
3375 3353
3376 3354 *rvalp = rv;
3377 3355
3378 3356 return (rv);
3379 3357 }
3380 3358
3381 3359 /*ARGSUSED*/
3382 3360 static int
3383 3361 scsi_hba_fm_init_child(dev_info_t *self, dev_info_t *child, int cap,
3384 3362 ddi_iblock_cookie_t *ibc)
3385 3363 {
3386 3364 scsi_hba_tran_t *tran = ddi_get_driver_private(self);
3387 3365
3388 3366 return (tran ? tran->tran_fm_capable : scsi_fm_capable);
3389 3367 }
3390 3368
3391 3369 static int
3392 3370 scsi_hba_bus_power(dev_info_t *self, void *impl_arg, pm_bus_power_op_t op,
3393 3371 void *arg, void *result)
3394 3372 {
3395 3373 scsi_hba_tran_t *tran;
3396 3374
3397 3375 tran = ddi_get_driver_private(self);
3398 3376 if (tran && tran->tran_bus_power) {
3399 3377 return (tran->tran_bus_power(self, impl_arg,
3400 3378 op, arg, result));
3401 3379 }
3402 3380
3403 3381 return (pm_busop_bus_power(self, impl_arg, op, arg, result));
3404 3382 }
3405 3383
3406 3384 /*
3407 3385 * Return the lun64 value from a address string: "addr,lun[,sfunc]". Either
3408 3386 * the lun is after the first ',' or the entire address string is the lun.
3409 3387 * Return SCSI_LUN64_ILLEGAL if the format is incorrect. A lun64 is at most
3410 3388 * 16 hex digits long.
3411 3389 *
3412 3390 * If the address string specified has incorrect syntax (busconfig one of
3413 3391 * bogus /devices path) then scsi_addr_to_lun64 can return SCSI_LUN64_ILLEGAL.
3414 3392 */
3415 3393 static scsi_lun64_t
3416 3394 scsi_addr_to_lun64(char *addr)
3417 3395 {
3418 3396 scsi_lun64_t lun64;
3419 3397 char *s;
3420 3398 int i;
3421 3399
3422 3400 if (addr) {
3423 3401 s = strchr(addr, ','); /* "addr,lun" */
3424 3402 if (s)
3425 3403 s++; /* skip ',', at lun */
3426 3404 else
3427 3405 s = addr; /* "lun" */
3428 3406
3429 3407 for (lun64 = 0, i = 0; *s && (i < 16); s++, i++) {
3430 3408 if (*s >= '0' && *s <= '9')
3431 3409 lun64 = (lun64 << 4) + (*s - '0');
3432 3410 else if (*s >= 'A' && *s <= 'F')
3433 3411 lun64 = (lun64 << 4) + 10 + (*s - 'A');
3434 3412 else if (*s >= 'a' && *s <= 'f')
3435 3413 lun64 = (lun64 << 4) + 10 + (*s - 'a');
3436 3414 else
3437 3415 break;
3438 3416 }
3439 3417 if (*s && (*s != ',')) /* [,sfunc] is OK */
3440 3418 lun64 = SCSI_LUN64_ILLEGAL;
3441 3419 } else
3442 3420 lun64 = SCSI_LUN64_ILLEGAL;
3443 3421
3444 3422 if (lun64 == SCSI_LUN64_ILLEGAL)
3445 3423 SCSI_HBA_LOG((_LOG(2), NULL, NULL,
3446 3424 "addr_to_lun64 %s lun %" PRIlun64,
3447 3425 addr ? addr : "NULL", lun64));
3448 3426 return (lun64);
3449 3427 }
3450 3428
3451 3429 /*
3452 3430 * Return the sfunc value from a address string: "addr,lun[,sfunc]". Either the
3453 3431 * sfunc is after the second ',' or the entire address string is the sfunc.
3454 3432 * Return -1 if there is only one ',' in the address string or the string is
3455 3433 * invalid. An sfunc is at most two hex digits long.
3456 3434 */
3457 3435 static int
3458 3436 scsi_addr_to_sfunc(char *addr)
3459 3437 {
3460 3438 int sfunc;
3461 3439 char *s;
3462 3440 int i;
3463 3441
3464 3442 if (addr) {
3465 3443 s = strchr(addr, ','); /* "addr,lun" */
3466 3444 if (s) {
3467 3445 s++; /* skip ',', at lun */
3468 3446 s = strchr(s, ','); /* "lun,sfunc" */
3469 3447 if (s == NULL)
3470 3448 return (-1); /* no ",sfunc" */
3471 3449 s++; /* skip ',', at sfunc */
3472 3450 } else
3473 3451 s = addr; /* "sfunc" */
3474 3452
3475 3453 for (sfunc = 0, i = 0; *s && (i < 2); s++, i++) {
3476 3454 if (*s >= '0' && *s <= '9')
3477 3455 sfunc = (sfunc << 4) + (*s - '0');
3478 3456 else if (*s >= 'A' && *s <= 'F')
3479 3457 sfunc = (sfunc << 4) + 10 + (*s - 'A');
3480 3458 else if (*s >= 'a' && *s <= 'f')
3481 3459 sfunc = (sfunc << 4) + 10 + (*s - 'a');
3482 3460 else
3483 3461 break;
3484 3462 }
3485 3463 if (*s)
3486 3464 sfunc = -1; /* illegal */
3487 3465 } else
3488 3466 sfunc = -1;
3489 3467 return (sfunc);
3490 3468 }
3491 3469
3492 3470 /*
3493 3471 * Convert scsi ascii string data to NULL terminated (semi) legal IEEE 1275
3494 3472 * "compatible" (name) property form.
3495 3473 *
3496 3474 * For ASCII INQUIRY data, a one-way conversion algorithm is needed to take
3497 3475 * SCSI_ASCII (20h - 7Eh) to a 1275-like compatible form. The 1275 spec allows
3498 3476 * letters, digits, one ",", and ". _ + -", all limited by a maximum 31
3499 3477 * character length. Since ", ." are used as separators in the compatible
3500 3478 * string itself, they are converted to "_". All SCSI_ASCII characters that
3501 3479 * are illegal in 1275, as well as any illegal SCSI_ASCII characters
3502 3480 * encountered, are converted to "_". To reduce length, trailing blanks are
3503 3481 * trimmed from SCSI_ASCII fields prior to conversion.
3504 3482 *
3505 3483 * Example: SCSI_ASCII "ST32550W SUN2.1G" -> "ST32550W_SUN2_1G"
3506 3484 *
3507 3485 * NOTE: the 1275 string form is always less than or equal to the scsi form.
3508 3486 */
3509 3487 static char *
3510 3488 string_scsi_to_1275(char *s_1275, char *s_scsi, int len)
3511 3489 {
3512 3490 (void) strncpy(s_1275, s_scsi, len);
3513 3491 s_1275[len--] = '\0';
3514 3492
3515 3493 while (len >= 0) {
3516 3494 if (s_1275[len] == ' ')
3517 3495 s_1275[len--] = '\0'; /* trim trailing " " */
3518 3496 else
3519 3497 break;
3520 3498 }
3521 3499
3522 3500 while (len >= 0) {
3523 3501 if (((s_1275[len] >= 'a') && (s_1275[len] <= 'z')) ||
3524 3502 ((s_1275[len] >= 'A') && (s_1275[len] <= 'Z')) ||
3525 3503 ((s_1275[len] >= '0') && (s_1275[len] <= '9')) ||
3526 3504 (s_1275[len] == '_') ||
3527 3505 (s_1275[len] == '+') ||
3528 3506 (s_1275[len] == '-'))
3529 3507 len--; /* legal 1275 */
3530 3508 else
3531 3509 s_1275[len--] = '_'; /* illegal SCSI_ASCII | 1275 */
3532 3510 }
3533 3511
3534 3512 return (s_1275);
3535 3513 }
3536 3514
3537 3515 /*
3538 3516 * Given the inquiry data, binding_set, and dtype_node for a scsi device,
3539 3517 * return the nodename and compatible property for the device. The "compatible"
3540 3518 * concept comes from IEEE-1275. The compatible information is returned is in
3541 3519 * the correct form for direct use defining the "compatible" string array
3542 3520 * property. Internally, "compatible" is also used to determine the nodename
3543 3521 * to return.
3544 3522 *
3545 3523 * This function is provided as a separate entry point for use by drivers that
3546 3524 * currently issue their own non-SCSA inquiry command and perform their own
3547 3525 * node creation based their own private compiled in tables. Converting these
3548 3526 * drivers to use this interface provides a quick easy way of obtaining
3549 3527 * consistency as well as the flexibility associated with the 1275 techniques.
3550 3528 *
3551 3529 * The dtype_node is passed as a separate argument (instead of having the
3552 3530 * implementation use inq_dtype). It indicates that information about
3553 3531 * a secondary function embedded service should be produced.
3554 3532 *
3555 3533 * Callers must always use scsi_hba_nodename_compatible_free, even if
3556 3534 * *nodenamep is null, to free the nodename and compatible information
3557 3535 * when done.
3558 3536 *
3559 3537 * If a nodename can't be determined then **compatiblep will point to a
3560 3538 * diagnostic string containing all the compatible forms.
3561 3539 *
3562 3540 * NOTE: some compatible strings may violate the 31 character restriction
3563 3541 * imposed by IEEE-1275. This is not a problem because Solaris does not care
3564 3542 * about this 31 character limit.
3565 3543 *
3566 3544 * Each compatible form belongs to a form-group. The form-groups currently
3567 3545 * defined are generic ("scsiclass"), binding-set ("scsa.b"), and failover
3568 3546 * ("scsa.f").
3569 3547 *
3570 3548 * The following compatible forms, in high to low precedence
3571 3549 * order, are defined for SCSI target device nodes.
3572 3550 *
3573 3551 * scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR (1 *1&2)
3574 3552 * scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR (2 *1)
3575 3553 * scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR (3 *2)
3576 3554 * scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR (4)
3577 3555 * scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP (5 *1&2)
3578 3556 * scsiclass,DDEE.vVVVVVVVV.pPPPPPPPPPPPPPPPP (6 *1)
3579 3557 * scsiclass,DDFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP (7 *2)
3580 3558 * scsiclass,DD.vVVVVVVVV.pPPPPPPPPPPPPPPPP (8)
3581 3559 * scsa,DD.bBBBBBBBB (8.5 *3)
3582 3560 * scsiclass,DDEEFFF (9 *1&2)
3583 3561 * scsiclass,DDEE (10 *1)
3584 3562 * scsiclass,DDFFF (11 *2)
3585 3563 * scsiclass,DD (12)
3586 3564 * scsa.fFFF (12.5 *4)
3587 3565 * scsiclass (13)
3588 3566 *
3589 3567 * *1 only produced on a secondary function node
3590 3568 * *2 only produced when generic form-group flags exist.
3591 3569 * *3 only produced when binding-set form-group legacy support is needed
3592 3570 * *4 only produced when failover form-group flags exist.
3593 3571 *
3594 3572 * where:
3595 3573 *
3596 3574 * v is the letter 'v'. Denotest the
3597 3575 * beginning of VVVVVVVV.
3598 3576 *
3599 3577 * VVVVVVVV Translated scsi_vendor.
3600 3578 *
3601 3579 * p is the letter 'p'. Denotes the
3602 3580 * beginning of PPPPPPPPPPPPPPPP.
3603 3581 *
3604 3582 * PPPPPPPPPPPPPPPP Translated scsi_product.
3605 3583 *
3606 3584 * r is the letter 'r'. Denotes the
3607 3585 * beginning of RRRR.
3608 3586 *
3609 3587 * RRRR Translated scsi_revision.
3610 3588 *
3611 3589 * DD is a two digit ASCII hexadecimal
3612 3590 * number. The value of the two digits is
3613 3591 * based one the SCSI "Peripheral device
3614 3592 * type" command set associated with the
3615 3593 * node. On a primary node this is the
3616 3594 * scsi_dtype of the primary command set,
3617 3595 * on a secondary node this is the
3618 3596 * scsi_dtype associated with the secondary
3619 3597 * function embedded command set.
3620 3598 *
3621 3599 * EE Same encoding used for DD. This form is
3622 3600 * only generated on secondary function
3623 3601 * nodes. The DD secondary function is embedded
3624 3602 * in an EE device.
3625 3603 *
3626 3604 * FFF Concatenation, in alphabetical order,
3627 3605 * of the flag characters within a form-group.
3628 3606 * For a given form-group, the following
3629 3607 * flags are defined.
3630 3608 *
3631 3609 * scsiclass: (generic form-group):
3632 3610 * R Removable_Media: Used when
3633 3611 * inq_rmb is set.
3634 3612 * S SAF-TE device: Used when
3635 3613 * inquiry information indicates
3636 3614 * SAF-TE devices.
3637 3615 *
3638 3616 * scsa.f: (failover form-group):
3639 3617 * E Explicit Target_Port_Group: Used
3640 3618 * when inq_tpgse is set and 'G' is
3641 3619 * alse present.
3642 3620 * G GUID: Used when a GUID can be
3643 3621 * generated for the device.
3644 3622 * I Implicit Target_Port_Group: Used
3645 3623 * when inq_tpgs is set and 'G' is
3646 3624 * also present.
3647 3625 *
3648 3626 * Forms using FFF are only be generated
3649 3627 * if there are applicable flag
3650 3628 * characters.
3651 3629 *
3652 3630 * b is the letter 'b'. Denotes the
3653 3631 * beginning of BBBBBBBB.
3654 3632 *
3655 3633 * BBBBBBBB Binding-set. Operating System Specific:
3656 3634 * scsi-binding-set property of HBA.
3657 3635 */
3658 3636 #define NCOMPAT (1 + (13 + 2) + 1)
3659 3637 #define COMPAT_LONGEST (strlen( \
3660 3638 "scsiclass,DDEEFFF.vVVVVVVVV.pPPPPPPPPPPPPPPPP.rRRRR" + 1))
3661 3639
3662 3640 /*
3663 3641 * Private version with extra device 'identity' arguments to allow code
3664 3642 * to determine GUID FFF support.
3665 3643 */
3666 3644 static void
3667 3645 scsi_hba_ident_nodename_compatible_get(struct scsi_inquiry *inq,
3668 3646 uchar_t *inq80, size_t inq80len, uchar_t *inq83, size_t inq83len,
3669 3647 char *binding_set, int dtype_node, char *compat0,
3670 3648 char **nodenamep, char **drivernamep,
3671 3649 char ***compatiblep, int *ncompatiblep)
3672 3650 {
3673 3651 char vid[sizeof (inq->inq_vid) + 1 ];
3674 3652 char pid[sizeof (inq->inq_pid) + 1];
3675 3653 char rev[sizeof (inq->inq_revision) + 1];
3676 3654 char gf[sizeof ("RS\0")];
3677 3655 char ff[sizeof ("EGI\0")];
3678 3656 int dtype_device;
3679 3657 int ncompat; /* number of compatible */
3680 3658 char **compatp; /* compatible ptrs */
3681 3659 int i;
3682 3660 char *nname; /* nodename */
3683 3661 char *dname; /* driver name */
3684 3662 char **csp;
3685 3663 char *p;
3686 3664 int tlen;
3687 3665 int len;
3688 3666 major_t major;
3689 3667 ddi_devid_t devid;
3690 3668 char *guid;
3691 3669 uchar_t *iqd = (uchar_t *)inq;
3692 3670
3693 3671 /*
3694 3672 * Nodename_aliases: This table was originally designed to be
3695 3673 * implemented via a new nodename_aliases file - a peer to the
3696 3674 * driver_aliases that selects a nodename based on compatible
3697 3675 * forms in much the same say driver_aliases is used to select
3698 3676 * driver bindings from compatible forms. Each compatible form
3699 3677 * is an 'alias'. Until a more general need for a
3700 3678 * nodename_aliases file exists, which may never occur, the
3701 3679 * scsi mappings are described here via a compiled in table.
3702 3680 *
3703 3681 * This table contains nodename mappings for self-identifying
3704 3682 * scsi devices enumerated by the Solaris kernel. For a given
3705 3683 * device, the highest precedence "compatible" form with a
3706 3684 * mapping is used to select the nodename for the device. This
3707 3685 * will typically be a generic nodename, however in some legacy
3708 3686 * compatibility cases a driver nodename mapping may be selected.
3709 3687 *
3710 3688 * Because of possible breakage associated with switching SCSI
3711 3689 * target devices from driver nodenames to generic nodenames,
3712 3690 * we are currently unable to support generic nodenames for all
3713 3691 * SCSI devices (binding-sets). Although /devices paths are
3714 3692 * defined as unstable, avoiding possible breakage is
3715 3693 * important. Some of the newer SCSI transports (USB) already
3716 3694 * use generic nodenames. All new SCSI transports and target
3717 3695 * devices should use generic nodenames. At times this decision
3718 3696 * may be architecture dependent (sparc .vs. intel) based on when
3719 3697 * a transport was supported on a particular architecture.
3720 3698 *
3721 3699 * We provide a base set of generic nodename mappings based on
3722 3700 * scsiclass dtype and higher-precedence driver nodename
3723 3701 * mappings based on scsa "binding-set" to cover legacy
3724 3702 * issues. The binding-set is typically associated with
3725 3703 * "scsi-binding-set" property value of the HBA. The legacy
3726 3704 * mappings are provided independent of whether the driver they
3727 3705 * refer to is installed. This allows a correctly named node
3728 3706 * be created at discovery time, and binding to occur when/if
3729 3707 * an add_drv of the legacy driver occurs.
3730 3708 *
3731 3709 * We also have mappings for legacy SUN hardware that
3732 3710 * misidentifies itself (enclosure services which identify
3733 3711 * themselves as processors). All future hardware should use
3734 3712 * the correct dtype.
3735 3713 *
3736 3714 * As SCSI HBAs are modified to use the SCSA interfaces for
3737 3715 * self-identifying SCSI target devices (PSARC/2004/116) the
3738 3716 * nodename_aliases table (PSARC/2004/420) should be augmented
3739 3717 * with legacy mappings in order to maintain compatibility with
3740 3718 * existing /devices paths, especially for devices that house
3741 3719 * an OS. Failure to do this may cause upgrade problems.
3742 3720 * Additions for new target devices or transports should not
3743 3721 * add scsa binding-set compatible mappings.
3744 3722 */
3745 3723 static struct nodename_aliases {
3746 3724 char *na_nodename; /* nodename */
3747 3725 char *na_alias; /* compatible form match */
3748 3726 } na[] = {
3749 3727 /* # mapping to generic nodenames based on scsi dtype */
3750 3728 {"disk", "scsiclass,00"},
3751 3729 {"tape", "scsiclass,01"},
3752 3730 {"printer", "scsiclass,02"},
3753 3731 {"processor", "scsiclass,03"},
3754 3732 {"worm", "scsiclass,04"},
3755 3733 {"cdrom", "scsiclass,05"},
3756 3734 {"scanner", "scsiclass,06"},
3757 3735 {"optical-disk", "scsiclass,07"},
3758 3736 {"medium-changer", "scsiclass,08"},
3759 3737 {"obsolete", "scsiclass,09"},
3760 3738 {"prepress-a", "scsiclass,0a"},
3761 3739 {"prepress-b", "scsiclass,0b"},
3762 3740 {"array-controller", "scsiclass,0c"},
3763 3741 {"enclosure", "scsiclass,0d"},
3764 3742 {"disk", "scsiclass,0e"},
3765 3743 {"card-reader", "scsiclass,0f"},
3766 3744 {"bridge", "scsiclass,10"},
3767 3745 {"object-store", "scsiclass,11"},
3768 3746 {"reserved", "scsiclass,12"},
3769 3747 {"reserved", "scsiclass,13"},
3770 3748 {"reserved", "scsiclass,14"},
3771 3749 {"reserved", "scsiclass,15"},
3772 3750 {"reserved", "scsiclass,16"},
3773 3751 {"reserved", "scsiclass,17"},
3774 3752 {"reserved", "scsiclass,18"},
3775 3753 {"reserved", "scsiclass,19"},
3776 3754 {"reserved", "scsiclass,1a"},
3777 3755 {"reserved", "scsiclass,1b"},
3778 3756 {"reserved", "scsiclass,1c"},
3779 3757 {"reserved", "scsiclass,1d"},
3780 3758 {"well-known-lun", "scsiclass,1e"},
3781 3759 {"unknown", "scsiclass,1f"},
3782 3760
3783 3761 #ifdef sparc
3784 3762 /* # legacy mapping to driver nodenames for fcp binding-set */
3785 3763 {"ssd", "scsa,00.bfcp"},
3786 3764 {"st", "scsa,01.bfcp"},
3787 3765 {"sgen", "scsa,08.bfcp"},
3788 3766 {"ses", "scsa,0d.bfcp"},
3789 3767
3790 3768 /* # legacy mapping to driver nodenames for vhci binding-set */
3791 3769 {"ssd", "scsa,00.bvhci"},
3792 3770 {"st", "scsa,01.bvhci"},
3793 3771 {"sgen", "scsa,08.bvhci"},
3794 3772 {"ses", "scsa,0d.bvhci"},
3795 3773 #else /* sparc */
3796 3774 /* # for x86 fcp and vhci use generic nodenames */
3797 3775 #endif /* sparc */
3798 3776
3799 3777 /* # legacy mapping to driver nodenames for spi binding-set */
3800 3778 {"sd", "scsa,00.bspi"},
3801 3779 {"sd", "scsa,05.bspi"},
3802 3780 {"sd", "scsa,07.bspi"},
3803 3781 {"st", "scsa,01.bspi"},
3804 3782 {"ses", "scsa,0d.bspi"},
3805 3783
3806 3784 /* # SUN misidentified spi hardware */
3807 3785 {"ses", "scsiclass,03.vSUN.pD2"},
3808 3786 {"ses", "scsiclass,03.vSYMBIOS.pD1000"},
3809 3787
3810 3788 /* # legacy mapping to driver nodenames for atapi binding-set */
3811 3789 {"sd", "scsa,00.batapi"},
3812 3790 {"sd", "scsa,05.batapi"},
3813 3791 {"sd", "scsa,07.batapi"},
3814 3792 {"st", "scsa,01.batapi"},
3815 3793 {"unknown", "scsa,0d.batapi"},
3816 3794
3817 3795 /* # legacy mapping to generic nodenames for usb binding-set */
3818 3796 {"disk", "scsa,05.busb"},
3819 3797 {"disk", "scsa,07.busb"},
3820 3798 {"changer", "scsa,08.busb"},
3821 3799 {"comm", "scsa,09.busb"},
3822 3800 {"array_ctlr", "scsa,0c.busb"},
3823 3801 {"esi", "scsa,0d.busb"},
3824 3802
3825 3803 /*
3826 3804 * mapping nodenames for mpt based on scsi dtype
3827 3805 * for being compatible with the original node names
3828 3806 * under mpt controller
3829 3807 */
3830 3808 {"sd", "scsa,00.bmpt"},
3831 3809 {"sd", "scsa,05.bmpt"},
3832 3810 {"sd", "scsa,07.bmpt"},
3833 3811 {"st", "scsa,01.bmpt"},
3834 3812 {"ses", "scsa,0d.bmpt"},
3835 3813 {"sgen", "scsa,08.bmpt"},
3836 3814 {NULL, NULL}
3837 3815 };
3838 3816 struct nodename_aliases *nap;
3839 3817
3840 3818 /* NOTE: drivernamep can be NULL */
3841 3819 ASSERT(nodenamep && compatiblep && ncompatiblep &&
3842 3820 (binding_set == NULL || (strlen(binding_set) <= 8)));
3843 3821 if ((nodenamep == NULL) || (compatiblep == NULL) ||
3844 3822 (ncompatiblep == NULL))
3845 3823 return;
3846 3824
3847 3825 /*
3848 3826 * In order to reduce runtime we allocate one block of memory that
3849 3827 * contains both the NULL terminated array of pointers to compatible
3850 3828 * forms and the individual compatible strings. This block is
3851 3829 * somewhat larger than needed, but is short lived - it only exists
3852 3830 * until the caller can transfer the information into the "compatible"
3853 3831 * string array property and call scsi_hba_nodename_compatible_free.
3854 3832 */
3855 3833 tlen = NCOMPAT * COMPAT_LONGEST;
3856 3834 compatp = kmem_alloc((NCOMPAT * sizeof (char *)) + tlen, KM_SLEEP);
3857 3835
3858 3836 /* convert inquiry data from SCSI ASCII to 1275 string */
3859 3837 (void) string_scsi_to_1275(vid, inq->inq_vid,
3860 3838 sizeof (inq->inq_vid));
3861 3839 (void) string_scsi_to_1275(pid, inq->inq_pid,
3862 3840 sizeof (inq->inq_pid));
3863 3841 (void) string_scsi_to_1275(rev, inq->inq_revision,
3864 3842 sizeof (inq->inq_revision));
3865 3843 ASSERT((strlen(vid) <= sizeof (inq->inq_vid)) &&
3866 3844 (strlen(pid) <= sizeof (inq->inq_pid)) &&
3867 3845 (strlen(rev) <= sizeof (inq->inq_revision)));
3868 3846
3869 3847 /*
3870 3848 * Form flags in ***ALPHABETICAL*** order within form-group:
3871 3849 *
3872 3850 * NOTE: When adding a new flag to an existing form-group, careful
3873 3851 * consideration must be given to not breaking existing bindings
3874 3852 * based on that form-group.
3875 3853 */
3876 3854
3877 3855 /*
3878 3856 * generic form-group flags
3879 3857 * R removable:
3880 3858 * Set when inq_rmb is set and for well known scsi dtypes. For a
3881 3859 * bus where the entire device is removable (like USB), we expect
3882 3860 * the HBA to intercept the inquiry data and set inq_rmb.
3883 3861 * Since OBP does not distinguish removable media in its generic
3884 3862 * name selection we avoid setting the 'R' flag if the root is not
3885 3863 * yet mounted.
3886 3864 * S SAF-TE device
3887 3865 * Set when the device type is SAT-TE.
3888 3866 */
3889 3867 i = 0;
3890 3868 dtype_device = inq->inq_dtype & DTYPE_MASK;
3891 3869 if (modrootloaded && (inq->inq_rmb ||
3892 3870 (dtype_device == DTYPE_WORM) ||
3893 3871 (dtype_device == DTYPE_RODIRECT) ||
3894 3872 (dtype_device == DTYPE_OPTICAL)))
3895 3873 gf[i++] = 'R'; /* removable */
3896 3874 gf[i] = '\0';
3897 3875
3898 3876 if (modrootloaded &&
3899 3877 (dtype_device == DTYPE_PROCESSOR) &&
3900 3878 (strncmp((char *)&iqd[44], "SAF-TE", 4) == 0))
3901 3879 gf[i++] = 'S';
3902 3880 gf[i] = '\0';
3903 3881
3904 3882 /*
3905 3883 * failover form-group flags
3906 3884 * E Explicit Target_Port_Group_Supported:
3907 3885 * Set for a device that has a GUID if inq_tpgse also set.
3908 3886 * G GUID:
3909 3887 * Set when we have identity information, can determine a devid
3910 3888 * from the identity information, and can generate a guid from
3911 3889 * that devid.
3912 3890 * I Implicit Target_Port_Group_Supported:
3913 3891 * Set for a device that has a GUID if inq_tpgs also set.
3914 3892 */
3915 3893 i = 0;
3916 3894 if ((inq80 || inq83) &&
3917 3895 (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, NULL,
3918 3896 (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
3919 3897 &devid) == DDI_SUCCESS)) {
3920 3898 guid = ddi_devid_to_guid(devid);
3921 3899 ddi_devid_free(devid);
3922 3900 } else
3923 3901 guid = NULL;
3924 3902 if (guid && (inq->inq_tpgs & TPGS_FAILOVER_EXPLICIT))
3925 3903 ff[i++] = 'E'; /* EXPLICIT TPGS */
3926 3904 if (guid)
3927 3905 ff[i++] = 'G'; /* GUID */
3928 3906 if (guid && (inq->inq_tpgs & TPGS_FAILOVER_IMPLICIT))
3929 3907 ff[i++] = 'I'; /* IMPLICIT TPGS */
3930 3908 ff[i] = '\0';
3931 3909 if (guid)
3932 3910 ddi_devid_free_guid(guid);
3933 3911
3934 3912 /*
3935 3913 * Construct all applicable compatible forms. See comment at the
3936 3914 * head of the function for a description of the compatible forms.
3937 3915 */
3938 3916 csp = compatp;
3939 3917 p = (char *)(compatp + NCOMPAT);
3940 3918
3941 3919 /* ( 0) driver (optional, not documented in scsi(4)) */
3942 3920 if (compat0) {
3943 3921 *csp++ = p;
3944 3922 (void) snprintf(p, tlen, "%s", compat0);
3945 3923 len = strlen(p) + 1;
3946 3924 p += len;
3947 3925 tlen -= len;
3948 3926 }
3949 3927
3950 3928 /* ( 1) scsiclass,DDEEFFF.vV.pP.rR */
3951 3929 if ((dtype_device != dtype_node) && *gf && *vid && *pid && *rev) {
3952 3930 *csp++ = p;
3953 3931 (void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s.r%s",
3954 3932 dtype_node, dtype_device, gf, vid, pid, rev);
3955 3933 len = strlen(p) + 1;
3956 3934 p += len;
3957 3935 tlen -= len;
3958 3936 }
3959 3937
3960 3938 /* ( 2) scsiclass,DDEE.vV.pP.rR */
3961 3939 if ((dtype_device != dtype_node) && *vid && *pid && *rev) {
3962 3940 *csp++ = p;
3963 3941 (void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s.r%s",
3964 3942 dtype_node, dtype_device, vid, pid, rev);
3965 3943 len = strlen(p) + 1;
3966 3944 p += len;
3967 3945 tlen -= len;
3968 3946 }
3969 3947
3970 3948 /* ( 3) scsiclass,DDFFF.vV.pP.rR */
3971 3949 if (*gf && *vid && *pid && *rev) {
3972 3950 *csp++ = p;
3973 3951 (void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s.r%s",
3974 3952 dtype_node, gf, vid, pid, rev);
3975 3953 len = strlen(p) + 1;
3976 3954 p += len;
3977 3955 tlen -= len;
3978 3956 }
3979 3957
3980 3958 /* ( 4) scsiclass,DD.vV.pP.rR */
3981 3959 if (*vid && *pid && *rev) {
3982 3960 *csp++ = p;
3983 3961 (void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s.r%s",
3984 3962 dtype_node, vid, pid, rev);
3985 3963 len = strlen(p) + 1;
3986 3964 p += len;
3987 3965 tlen -= len;
3988 3966 }
3989 3967
3990 3968 /* ( 5) scsiclass,DDEEFFF.vV.pP */
3991 3969 if ((dtype_device != dtype_node) && *gf && *vid && *pid) {
3992 3970 *csp++ = p;
3993 3971 (void) snprintf(p, tlen, "scsiclass,%02x%02x%s.v%s.p%s",
3994 3972 dtype_node, dtype_device, gf, vid, pid);
3995 3973 len = strlen(p) + 1;
3996 3974 p += len;
3997 3975 tlen -= len;
3998 3976 }
3999 3977
4000 3978 /* ( 6) scsiclass,DDEE.vV.pP */
4001 3979 if ((dtype_device != dtype_node) && *vid && *pid) {
4002 3980 *csp++ = p;
4003 3981 (void) snprintf(p, tlen, "scsiclass,%02x%02x.v%s.p%s",
4004 3982 dtype_node, dtype_device, vid, pid);
4005 3983 len = strlen(p) + 1;
4006 3984 p += len;
4007 3985 tlen -= len;
4008 3986 }
4009 3987
4010 3988 /* ( 7) scsiclass,DDFFF.vV.pP */
4011 3989 if (*gf && *vid && *pid) {
4012 3990 *csp++ = p;
4013 3991 (void) snprintf(p, tlen, "scsiclass,%02x%s.v%s.p%s",
4014 3992 dtype_node, gf, vid, pid);
4015 3993 len = strlen(p) + 1;
4016 3994 p += len;
4017 3995 tlen -= len;
4018 3996 }
4019 3997
4020 3998 /* ( 8) scsiclass,DD.vV.pP */
4021 3999 if (*vid && *pid) {
4022 4000 *csp++ = p;
4023 4001 (void) snprintf(p, tlen, "scsiclass,%02x.v%s.p%s",
4024 4002 dtype_node, vid, pid);
4025 4003 len = strlen(p) + 1;
4026 4004 p += len;
4027 4005 tlen -= len;
4028 4006 }
4029 4007
4030 4008 /* (8.5) scsa,DD.bB (not documented in scsi(4)) */
4031 4009 if (binding_set) {
4032 4010 *csp++ = p;
4033 4011 (void) snprintf(p, tlen, "scsa,%02x.b%s",
4034 4012 dtype_node, binding_set);
4035 4013 len = strlen(p) + 1;
4036 4014 p += len;
4037 4015 tlen -= len;
4038 4016 }
4039 4017
4040 4018 /* ( 9) scsiclass,DDEEFFF */
4041 4019 if ((dtype_device != dtype_node) && *gf) {
4042 4020 *csp++ = p;
4043 4021 (void) snprintf(p, tlen, "scsiclass,%02x%02x%s",
4044 4022 dtype_node, dtype_device, gf);
4045 4023 len = strlen(p) + 1;
4046 4024 p += len;
4047 4025 tlen -= len;
4048 4026 }
4049 4027
4050 4028 /* (10) scsiclass,DDEE */
4051 4029 if (dtype_device != dtype_node) {
4052 4030 *csp++ = p;
4053 4031 (void) snprintf(p, tlen, "scsiclass,%02x%02x",
4054 4032 dtype_node, dtype_device);
4055 4033 len = strlen(p) + 1;
4056 4034 p += len;
4057 4035 tlen -= len;
4058 4036 }
4059 4037
4060 4038 /* (11) scsiclass,DDFFF */
4061 4039 if (*gf) {
4062 4040 *csp++ = p;
4063 4041 (void) snprintf(p, tlen, "scsiclass,%02x%s",
4064 4042 dtype_node, gf);
4065 4043 len = strlen(p) + 1;
4066 4044 p += len;
4067 4045 tlen -= len;
4068 4046 }
4069 4047
4070 4048 /* (12) scsiclass,DD */
4071 4049 *csp++ = p;
4072 4050 (void) snprintf(p, tlen, "scsiclass,%02x", dtype_node);
4073 4051 len = strlen(p) + 1;
4074 4052 p += len;
4075 4053 tlen -= len;
4076 4054
4077 4055 /* (12.5) scsa.fFFF */
4078 4056 if (*ff) {
4079 4057 *csp++ = p;
4080 4058 (void) snprintf(p, tlen, "scsa.f%s", ff);
4081 4059 len = strlen(p) + 1;
4082 4060 p += len;
4083 4061 tlen -= len;
4084 4062 }
4085 4063
4086 4064 /* (13) scsiclass */
4087 4065 *csp++ = p;
4088 4066 (void) snprintf(p, tlen, "scsiclass");
4089 4067 len = strlen(p) + 1;
4090 4068 p += len;
4091 4069 tlen -= len;
4092 4070 ASSERT(tlen >= 0);
4093 4071
4094 4072 *csp = NULL; /* NULL terminate array of pointers */
4095 4073 ncompat = csp - compatp;
4096 4074
4097 4075 /*
4098 4076 * When determining a nodename, a nodename_aliases specified
4099 4077 * mapping has precedence over using a driver_aliases specified
4100 4078 * driver binding as a nodename.
4101 4079 *
4102 4080 * See if any of the compatible forms have a nodename_aliases
4103 4081 * specified nodename. These mappings are described by
4104 4082 * nodename_aliases entries like:
4105 4083 *
4106 4084 * disk "scsiclass,00"
4107 4085 * enclosure "scsiclass,03.vSYMBIOS.pD1000"
4108 4086 * ssd "scsa,00.bfcp"
4109 4087 *
4110 4088 * All nodename_aliases mappings should idealy be to generic
4111 4089 * names, however a higher precedence legacy mapping to a
4112 4090 * driver name may exist. The highest precedence mapping
4113 4091 * provides the nodename, so legacy driver nodename mappings
4114 4092 * (if they exist) take precedence over generic nodename
4115 4093 * mappings.
4116 4094 */
4117 4095 for (nname = NULL, csp = compatp; (nname == NULL) && *csp; csp++) {
4118 4096 for (nap = na; nap->na_nodename; nap++) {
4119 4097 if (strcmp(*csp, nap->na_alias) == 0) {
4120 4098 nname = nap->na_nodename;
4121 4099 break;
4122 4100 }
4123 4101 }
4124 4102 }
4125 4103
4126 4104 /*
4127 4105 * Determine the driver name based on compatible (which may
4128 4106 * have the passed in compat0 as the first item). The driver_aliases
4129 4107 * file has entries like
4130 4108 *
4131 4109 * sd "scsiclass,00"
4132 4110 *
4133 4111 * that map compatible forms to specific drivers. These entries are
4134 4112 * established by add_drv/update_drv. We use the most specific
4135 4113 * driver binding as the nodename. This matches the eventual
4136 4114 * ddi_driver_compatible_major() binding that will be
4137 4115 * established by bind_node()
4138 4116 */
4139 4117 for (dname = NULL, csp = compatp; *csp; csp++) {
4140 4118 major = ddi_name_to_major(*csp);
4141 4119 if ((major == DDI_MAJOR_T_NONE) ||
4142 4120 (devnamesp[major].dn_flags & DN_DRIVER_REMOVED))
4143 4121 continue;
4144 4122 if (dname = ddi_major_to_name(major))
4145 4123 break;
4146 4124 }
4147 4125
4148 4126 /*
4149 4127 * If no nodename_aliases mapping exists then use the
4150 4128 * driver_aliases specified driver binding as a nodename.
4151 4129 */
4152 4130 if (nname == NULL)
4153 4131 nname = dname;
4154 4132
4155 4133 /* return results */
4156 4134 if (nname) {
4157 4135 *nodenamep = kmem_alloc(strlen(nname) + 1, KM_SLEEP);
4158 4136 (void) strcpy(*nodenamep, nname);
4159 4137 } else {
4160 4138 *nodenamep = NULL;
4161 4139
4162 4140 /*
4163 4141 * If no nodename could be determined return a special
4164 4142 * 'compatible' to be used for a diagnostic message. This
4165 4143 * compatible contains all compatible forms concatenated
4166 4144 * into a single string pointed to by the first element.
4167 4145 */
4168 4146 for (csp = compatp; *(csp + 1); csp++)
4169 4147 *((*csp) + strlen(*csp)) = ' ';
4170 4148 *(compatp + 1) = NULL;
4171 4149 ncompat = 1;
4172 4150
4173 4151 }
4174 4152 if (drivernamep) {
4175 4153 if (dname) {
4176 4154 *drivernamep = kmem_alloc(strlen(dname) + 1, KM_SLEEP);
4177 4155 (void) strcpy(*drivernamep, dname);
4178 4156 } else
4179 4157 *drivernamep = NULL;
4180 4158 }
4181 4159 *compatiblep = compatp;
4182 4160 *ncompatiblep = ncompat;
4183 4161 }
4184 4162
4185 4163 /*
4186 4164 * Free allocations associated with scsi_hba_ident_nodename_compatible_get.
4187 4165 */
4188 4166 static void
4189 4167 scsi_hba_ident_nodename_compatible_free(char *nodename, char *drivername,
4190 4168 char **compatible)
4191 4169 {
4192 4170 if (nodename)
4193 4171 kmem_free(nodename, strlen(nodename) + 1);
4194 4172 if (drivername)
4195 4173 kmem_free(drivername, strlen(drivername) + 1);
4196 4174 if (compatible)
4197 4175 kmem_free(compatible, (NCOMPAT * sizeof (char *)) +
4198 4176 (NCOMPAT * COMPAT_LONGEST));
4199 4177 }
4200 4178
4201 4179 void
4202 4180 scsi_hba_nodename_compatible_get(struct scsi_inquiry *inq,
4203 4181 char *binding_set, int dtype_node, char *compat0,
4204 4182 char **nodenamep, char ***compatiblep, int *ncompatiblep)
4205 4183 {
4206 4184 scsi_hba_ident_nodename_compatible_get(inq,
4207 4185 NULL, 0, NULL, 0, binding_set, dtype_node, compat0, nodenamep,
4208 4186 NULL, compatiblep, ncompatiblep);
4209 4187 }
4210 4188
4211 4189 void
4212 4190 scsi_hba_nodename_compatible_free(char *nodename, char **compatible)
4213 4191 {
4214 4192 scsi_hba_ident_nodename_compatible_free(nodename, NULL, compatible);
4215 4193 }
4216 4194
4217 4195 /* return the unit_address associated with a scsi_device */
4218 4196 char *
4219 4197 scsi_device_unit_address(struct scsi_device *sd)
4220 4198 {
4221 4199 mdi_pathinfo_t *pip;
4222 4200
4223 4201 ASSERT(sd && sd->sd_dev);
4224 4202 if ((sd == NULL) || (sd->sd_dev == NULL))
4225 4203 return (NULL);
4226 4204
4227 4205 pip = (mdi_pathinfo_t *)sd->sd_pathinfo;
4228 4206 if (pip)
4229 4207 return (mdi_pi_get_addr(pip));
4230 4208 else
4231 4209 return (ddi_get_name_addr(sd->sd_dev));
4232 4210 }
4233 4211
4234 4212 /* scsi_device property interfaces */
4235 4213 #define _TYPE_DEFINED(flags) \
4236 4214 (((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) || \
4237 4215 ((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_DEVICE))
4238 4216
4239 4217 #define _DEVICE_PIP(sd, flags) \
4240 4218 ((((flags & SCSI_DEVICE_PROP_TYPE_MSK) == SCSI_DEVICE_PROP_PATH) && \
4241 4219 sd->sd_pathinfo) ? (mdi_pathinfo_t *)sd->sd_pathinfo : NULL)
4242 4220
4243 4221 int
4244 4222 scsi_device_prop_get_int(struct scsi_device *sd, uint_t flags,
4245 4223 char *name, int defval)
4246 4224 {
4247 4225 mdi_pathinfo_t *pip;
4248 4226 int v = defval;
4249 4227 int data;
4250 4228 int rv;
4251 4229
4252 4230 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4253 4231 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4254 4232 !_TYPE_DEFINED(flags))
4255 4233 return (v);
4256 4234
4257 4235 pip = _DEVICE_PIP(sd, flags);
4258 4236 if (pip) {
4259 4237 rv = mdi_prop_lookup_int(pip, name, &data);
4260 4238 if (rv == DDI_PROP_SUCCESS)
4261 4239 v = data;
4262 4240 } else
4263 4241 v = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev,
4264 4242 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, name, v);
4265 4243 return (v);
4266 4244 }
4267 4245
4268 4246
4269 4247 int64_t
4270 4248 scsi_device_prop_get_int64(struct scsi_device *sd, uint_t flags,
4271 4249 char *name, int64_t defval)
4272 4250 {
4273 4251 mdi_pathinfo_t *pip;
4274 4252 int64_t v = defval;
4275 4253 int64_t data;
4276 4254 int rv;
4277 4255
4278 4256 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4279 4257 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4280 4258 !_TYPE_DEFINED(flags))
4281 4259 return (v);
4282 4260
4283 4261 pip = _DEVICE_PIP(sd, flags);
4284 4262 if (pip) {
4285 4263 rv = mdi_prop_lookup_int64(pip, name, &data);
4286 4264 if (rv == DDI_PROP_SUCCESS)
4287 4265 v = data;
4288 4266 } else
4289 4267 v = ddi_prop_get_int64(DDI_DEV_T_ANY, sd->sd_dev,
4290 4268 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, name, v);
4291 4269 return (v);
4292 4270 }
4293 4271
4294 4272 int
4295 4273 scsi_device_prop_lookup_byte_array(struct scsi_device *sd, uint_t flags,
4296 4274 char *name, uchar_t **data, uint_t *nelements)
4297 4275 {
4298 4276 mdi_pathinfo_t *pip;
4299 4277 int rv;
4300 4278
4301 4279 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4302 4280 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4303 4281 !_TYPE_DEFINED(flags))
4304 4282 return (DDI_PROP_INVAL_ARG);
4305 4283
4306 4284 pip = _DEVICE_PIP(sd, flags);
4307 4285 if (pip)
4308 4286 rv = mdi_prop_lookup_byte_array(pip, name, data, nelements);
4309 4287 else
4310 4288 rv = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, sd->sd_dev,
4311 4289 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4312 4290 name, data, nelements);
4313 4291 return (rv);
4314 4292 }
4315 4293
4316 4294 int
4317 4295 scsi_device_prop_lookup_int_array(struct scsi_device *sd, uint_t flags,
4318 4296 char *name, int **data, uint_t *nelements)
4319 4297 {
4320 4298 mdi_pathinfo_t *pip;
4321 4299 int rv;
4322 4300
4323 4301 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4324 4302 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4325 4303 !_TYPE_DEFINED(flags))
4326 4304 return (DDI_PROP_INVAL_ARG);
4327 4305
4328 4306 pip = _DEVICE_PIP(sd, flags);
4329 4307 if (pip)
4330 4308 rv = mdi_prop_lookup_int_array(pip, name, data, nelements);
4331 4309 else
4332 4310 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, sd->sd_dev,
4333 4311 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4334 4312 name, data, nelements);
4335 4313 return (rv);
4336 4314 }
4337 4315
4338 4316
4339 4317 int
4340 4318 scsi_device_prop_lookup_string(struct scsi_device *sd, uint_t flags,
4341 4319 char *name, char **data)
4342 4320 {
4343 4321 mdi_pathinfo_t *pip;
4344 4322 int rv;
4345 4323
4346 4324 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4347 4325 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4348 4326 !_TYPE_DEFINED(flags))
4349 4327 return (DDI_PROP_INVAL_ARG);
4350 4328
4351 4329 pip = _DEVICE_PIP(sd, flags);
4352 4330 if (pip)
4353 4331 rv = mdi_prop_lookup_string(pip, name, data);
4354 4332 else
4355 4333 rv = ddi_prop_lookup_string(DDI_DEV_T_ANY, sd->sd_dev,
4356 4334 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4357 4335 name, data);
4358 4336 return (rv);
4359 4337 }
4360 4338
4361 4339 int
4362 4340 scsi_device_prop_lookup_string_array(struct scsi_device *sd, uint_t flags,
4363 4341 char *name, char ***data, uint_t *nelements)
4364 4342 {
4365 4343 mdi_pathinfo_t *pip;
4366 4344 int rv;
4367 4345
4368 4346 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4369 4347 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4370 4348 !_TYPE_DEFINED(flags))
4371 4349 return (DDI_PROP_INVAL_ARG);
4372 4350
4373 4351 pip = _DEVICE_PIP(sd, flags);
4374 4352 if (pip)
4375 4353 rv = mdi_prop_lookup_string_array(pip, name, data, nelements);
4376 4354 else
4377 4355 rv = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, sd->sd_dev,
4378 4356 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4379 4357 name, data, nelements);
4380 4358 return (rv);
4381 4359 }
4382 4360
4383 4361 int
4384 4362 scsi_device_prop_update_byte_array(struct scsi_device *sd, uint_t flags,
4385 4363 char *name, uchar_t *data, uint_t nelements)
4386 4364 {
4387 4365 mdi_pathinfo_t *pip;
4388 4366 int rv;
4389 4367
4390 4368 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4391 4369 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4392 4370 !_TYPE_DEFINED(flags))
4393 4371 return (DDI_PROP_INVAL_ARG);
4394 4372
4395 4373 pip = _DEVICE_PIP(sd, flags);
4396 4374 if (pip)
4397 4375 rv = mdi_prop_update_byte_array(pip, name, data, nelements);
4398 4376 else
4399 4377 rv = ndi_prop_update_byte_array(DDI_DEV_T_NONE, sd->sd_dev,
4400 4378 name, data, nelements);
4401 4379 return (rv);
4402 4380 }
4403 4381
4404 4382 int
4405 4383 scsi_device_prop_update_int(struct scsi_device *sd, uint_t flags,
4406 4384 char *name, int data)
4407 4385 {
4408 4386 mdi_pathinfo_t *pip;
4409 4387 int rv;
4410 4388
4411 4389 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4412 4390 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4413 4391 !_TYPE_DEFINED(flags))
4414 4392 return (DDI_PROP_INVAL_ARG);
4415 4393
4416 4394 pip = _DEVICE_PIP(sd, flags);
4417 4395 if (pip)
4418 4396 rv = mdi_prop_update_int(pip, name, data);
4419 4397 else
4420 4398 rv = ndi_prop_update_int(DDI_DEV_T_NONE, sd->sd_dev,
4421 4399 name, data);
4422 4400 return (rv);
4423 4401 }
4424 4402
4425 4403 int
4426 4404 scsi_device_prop_update_int64(struct scsi_device *sd, uint_t flags,
4427 4405 char *name, int64_t data)
4428 4406 {
4429 4407 mdi_pathinfo_t *pip;
4430 4408 int rv;
4431 4409
4432 4410 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4433 4411 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4434 4412 !_TYPE_DEFINED(flags))
4435 4413 return (DDI_PROP_INVAL_ARG);
4436 4414
4437 4415 pip = _DEVICE_PIP(sd, flags);
4438 4416 if (pip)
4439 4417 rv = mdi_prop_update_int64(pip, name, data);
4440 4418 else
4441 4419 rv = ndi_prop_update_int64(DDI_DEV_T_NONE, sd->sd_dev,
4442 4420 name, data);
4443 4421 return (rv);
4444 4422 }
4445 4423
4446 4424 int
4447 4425 scsi_device_prop_update_int_array(struct scsi_device *sd, uint_t flags,
4448 4426 char *name, int *data, uint_t nelements)
4449 4427 {
4450 4428 mdi_pathinfo_t *pip;
4451 4429 int rv;
4452 4430
4453 4431 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4454 4432 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4455 4433 !_TYPE_DEFINED(flags))
4456 4434 return (DDI_PROP_INVAL_ARG);
4457 4435
4458 4436 pip = _DEVICE_PIP(sd, flags);
4459 4437 if (pip)
4460 4438 rv = mdi_prop_update_int_array(pip, name, data, nelements);
4461 4439 else
4462 4440 rv = ndi_prop_update_int_array(DDI_DEV_T_NONE, sd->sd_dev,
4463 4441 name, data, nelements);
4464 4442 return (rv);
4465 4443 }
4466 4444
4467 4445 int
4468 4446 scsi_device_prop_update_string(struct scsi_device *sd, uint_t flags,
4469 4447 char *name, char *data)
4470 4448 {
4471 4449 mdi_pathinfo_t *pip;
4472 4450 int rv;
4473 4451
4474 4452 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4475 4453 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4476 4454 !_TYPE_DEFINED(flags))
4477 4455 return (DDI_PROP_INVAL_ARG);
4478 4456
4479 4457 pip = _DEVICE_PIP(sd, flags);
4480 4458 if (pip)
4481 4459 rv = mdi_prop_update_string(pip, name, data);
4482 4460 else
4483 4461 rv = ndi_prop_update_string(DDI_DEV_T_NONE, sd->sd_dev,
4484 4462 name, data);
4485 4463 return (rv);
4486 4464 }
4487 4465
4488 4466 int
4489 4467 scsi_device_prop_update_string_array(struct scsi_device *sd, uint_t flags,
4490 4468 char *name, char **data, uint_t nelements)
4491 4469 {
4492 4470 mdi_pathinfo_t *pip;
4493 4471 int rv;
4494 4472
4495 4473 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4496 4474 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4497 4475 !_TYPE_DEFINED(flags))
4498 4476 return (DDI_PROP_INVAL_ARG);
4499 4477
4500 4478 pip = _DEVICE_PIP(sd, flags);
4501 4479 if (pip)
4502 4480 rv = mdi_prop_update_string_array(pip, name, data, nelements);
4503 4481 else
4504 4482 rv = ndi_prop_update_string_array(DDI_DEV_T_NONE, sd->sd_dev,
4505 4483 name, data, nelements);
4506 4484 return (rv);
4507 4485 }
4508 4486
4509 4487 int
4510 4488 scsi_device_prop_remove(struct scsi_device *sd, uint_t flags, char *name)
4511 4489 {
4512 4490 mdi_pathinfo_t *pip;
4513 4491 int rv;
4514 4492
4515 4493 ASSERT(sd && name && sd->sd_dev && _TYPE_DEFINED(flags));
4516 4494 if ((sd == NULL) || (name == NULL) || (sd->sd_dev == NULL) ||
4517 4495 !_TYPE_DEFINED(flags))
4518 4496 return (DDI_PROP_INVAL_ARG);
4519 4497
4520 4498 pip = _DEVICE_PIP(sd, flags);
4521 4499 if (pip)
4522 4500 rv = mdi_prop_remove(pip, name);
4523 4501 else
4524 4502 rv = ndi_prop_remove(DDI_DEV_T_NONE, sd->sd_dev, name);
4525 4503 return (rv);
4526 4504 }
4527 4505
4528 4506 void
4529 4507 scsi_device_prop_free(struct scsi_device *sd, uint_t flags, void *data)
4530 4508 {
4531 4509 mdi_pathinfo_t *pip;
4532 4510
4533 4511 ASSERT(sd && data && sd->sd_dev && _TYPE_DEFINED(flags));
4534 4512 if ((sd == NULL) || (data == NULL) || (sd->sd_dev == NULL) ||
4535 4513 !_TYPE_DEFINED(flags))
4536 4514 return;
4537 4515
4538 4516 pip = _DEVICE_PIP(sd, flags);
4539 4517 if (pip)
4540 4518 (void) mdi_prop_free(data);
4541 4519 else
4542 4520 ddi_prop_free(data);
4543 4521 }
4544 4522
4545 4523 /* SMP device property interfaces */
4546 4524 int
4547 4525 smp_device_prop_get_int(struct smp_device *smp_sd, char *name, int defval)
4548 4526 {
4549 4527 int v = defval;
4550 4528
4551 4529 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4552 4530 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4553 4531 return (v);
4554 4532
4555 4533 v = ddi_prop_get_int(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4556 4534 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, name, v);
4557 4535 return (v);
4558 4536 }
4559 4537
4560 4538
4561 4539 int64_t
4562 4540 smp_device_prop_get_int64(struct smp_device *smp_sd, char *name, int64_t defval)
4563 4541 {
4564 4542 int64_t v = defval;
4565 4543
4566 4544 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4567 4545 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4568 4546 return (v);
4569 4547
4570 4548 v = ddi_prop_get_int64(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4571 4549 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, name, v);
4572 4550 return (v);
4573 4551 }
4574 4552
4575 4553 int
4576 4554 smp_device_prop_lookup_byte_array(struct smp_device *smp_sd, char *name,
4577 4555 uchar_t **data, uint_t *nelements)
4578 4556 {
4579 4557 int rv;
4580 4558
4581 4559 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4582 4560 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4583 4561 return (DDI_PROP_INVAL_ARG);
4584 4562
4585 4563 rv = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4586 4564 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4587 4565 name, data, nelements);
4588 4566 return (rv);
4589 4567 }
4590 4568
4591 4569 int
4592 4570 smp_device_prop_lookup_int_array(struct smp_device *smp_sd, char *name,
4593 4571 int **data, uint_t *nelements)
4594 4572 {
4595 4573 int rv;
4596 4574
4597 4575 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4598 4576 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4599 4577 return (DDI_PROP_INVAL_ARG);
4600 4578
4601 4579 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4602 4580 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4603 4581 name, data, nelements);
4604 4582 return (rv);
4605 4583 }
4606 4584
4607 4585
4608 4586 int
4609 4587 smp_device_prop_lookup_string(struct smp_device *smp_sd, char *name,
4610 4588 char **data)
4611 4589 {
4612 4590 int rv;
4613 4591
4614 4592 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4615 4593 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4616 4594 return (DDI_PROP_INVAL_ARG);
4617 4595
4618 4596 rv = ddi_prop_lookup_string(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4619 4597 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4620 4598 name, data);
4621 4599 return (rv);
4622 4600 }
4623 4601
4624 4602 int
4625 4603 smp_device_prop_lookup_string_array(struct smp_device *smp_sd, char *name,
4626 4604 char ***data, uint_t *nelements)
4627 4605 {
4628 4606 int rv;
4629 4607
4630 4608 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4631 4609 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4632 4610 return (DDI_PROP_INVAL_ARG);
4633 4611
4634 4612 rv = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, smp_sd->smp_sd_dev,
4635 4613 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
4636 4614 name, data, nelements);
4637 4615 return (rv);
4638 4616 }
4639 4617
4640 4618 int
4641 4619 smp_device_prop_update_byte_array(struct smp_device *smp_sd, char *name,
4642 4620 uchar_t *data, uint_t nelements)
4643 4621 {
4644 4622 int rv;
4645 4623
4646 4624 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4647 4625 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4648 4626 return (DDI_PROP_INVAL_ARG);
4649 4627
4650 4628 rv = ndi_prop_update_byte_array(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4651 4629 name, data, nelements);
4652 4630 return (rv);
4653 4631 }
4654 4632
4655 4633 int
4656 4634 smp_device_prop_update_int(struct smp_device *smp_sd, char *name, int data)
4657 4635 {
4658 4636 int rv;
4659 4637
4660 4638 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4661 4639 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4662 4640 return (DDI_PROP_INVAL_ARG);
4663 4641
4664 4642 rv = ndi_prop_update_int(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4665 4643 name, data);
4666 4644 return (rv);
4667 4645 }
4668 4646
4669 4647 int
4670 4648 smp_device_prop_update_int64(struct smp_device *smp_sd, char *name,
4671 4649 int64_t data)
4672 4650 {
4673 4651 int rv;
4674 4652
4675 4653 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4676 4654 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4677 4655 return (DDI_PROP_INVAL_ARG);
4678 4656
4679 4657 rv = ndi_prop_update_int64(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4680 4658 name, data);
4681 4659 return (rv);
4682 4660 }
4683 4661
4684 4662 int
4685 4663 smp_device_prop_update_int_array(struct smp_device *smp_sd, char *name,
4686 4664 int *data, uint_t nelements)
4687 4665 {
4688 4666 int rv;
4689 4667
4690 4668 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4691 4669 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4692 4670 return (DDI_PROP_INVAL_ARG);
4693 4671
4694 4672 rv = ndi_prop_update_int_array(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4695 4673 name, data, nelements);
4696 4674 return (rv);
4697 4675 }
4698 4676
4699 4677 int
4700 4678 smp_device_prop_update_string(struct smp_device *smp_sd, char *name, char *data)
4701 4679 {
4702 4680 int rv;
4703 4681
4704 4682 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4705 4683 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4706 4684 return (DDI_PROP_INVAL_ARG);
4707 4685
4708 4686 rv = ndi_prop_update_string(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4709 4687 name, data);
4710 4688 return (rv);
4711 4689 }
4712 4690
4713 4691 int
4714 4692 smp_device_prop_update_string_array(struct smp_device *smp_sd, char *name,
4715 4693 char **data, uint_t nelements)
4716 4694 {
4717 4695 int rv;
4718 4696
4719 4697 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4720 4698 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4721 4699 return (DDI_PROP_INVAL_ARG);
4722 4700
4723 4701 rv = ndi_prop_update_string_array(DDI_DEV_T_NONE, smp_sd->smp_sd_dev,
4724 4702 name, data, nelements);
4725 4703 return (rv);
4726 4704 }
4727 4705
4728 4706 int
4729 4707 smp_device_prop_remove(struct smp_device *smp_sd, char *name)
4730 4708 {
4731 4709 int rv;
4732 4710
4733 4711 ASSERT(smp_sd && name && smp_sd->smp_sd_dev);
4734 4712 if ((smp_sd == NULL) || (name == NULL) || (smp_sd->smp_sd_dev == NULL))
4735 4713 return (DDI_PROP_INVAL_ARG);
4736 4714
4737 4715 rv = ndi_prop_remove(DDI_DEV_T_NONE, smp_sd->smp_sd_dev, name);
4738 4716 return (rv);
4739 4717 }
4740 4718
4741 4719 void
4742 4720 smp_device_prop_free(struct smp_device *smp_sd, void *data)
4743 4721 {
4744 4722 ASSERT(smp_sd && data && smp_sd->smp_sd_dev);
4745 4723 if ((smp_sd == NULL) || (data == NULL) || (smp_sd->smp_sd_dev == NULL))
4746 4724 return;
4747 4725
4748 4726 ddi_prop_free(data);
4749 4727 }
4750 4728
4751 4729 /*
4752 4730 * scsi_hba_ua_set: given "unit-address" string, set properties.
4753 4731 *
4754 4732 * Function to set the properties on a devinfo or pathinfo node from
4755 4733 * the "unit-address" part of a "name@unit-address" /devices path 'name'
4756 4734 * string.
4757 4735 *
4758 4736 * This function works in conjunction with scsi_ua_get()/scsi_hba_ua_get()
4759 4737 * (and possibly with an HBA driver's tran_tgt_init() implementation).
4760 4738 */
4761 4739 static int
4762 4740 scsi_hba_ua_set(char *ua, dev_info_t *dchild, mdi_pathinfo_t *pchild)
4763 4741 {
4764 4742 char *p;
4765 4743 int tgt;
4766 4744 char *tgt_port_end;
4767 4745 char *tgt_port;
4768 4746 int tgt_port_len;
4769 4747 int sfunc;
4770 4748 scsi_lun64_t lun64;
4771 4749
4772 4750 /* Caller must choose to decorate devinfo *or* pathinfo */
4773 4751 ASSERT((dchild != NULL) ^ (pchild != NULL));
4774 4752 if (dchild && pchild)
4775 4753 return (0);
4776 4754
4777 4755 /*
4778 4756 * generic implementation based on "tgt,lun[,sfunc]" address form.
4779 4757 * parse hex "tgt" part of "tgt,lun[,sfunc]"
4780 4758 */
4781 4759 p = ua;
4782 4760 tgt_port_end = NULL;
4783 4761 for (tgt = 0; *p && *p != ','; p++) {
4784 4762 if (*p >= '0' && *p <= '9')
4785 4763 tgt = (tgt << 4) + (*p - '0');
4786 4764 else if (*p >= 'a' && *p <= 'f')
4787 4765 tgt = (tgt << 4) + 10 + (*p - 'a');
4788 4766 else
4789 4767 tgt = -1; /* non-numeric */
4790 4768
4791 4769 /*
4792 4770 * if non-numeric or our of range set tgt to -1 and
4793 4771 * skip forward
4794 4772 */
4795 4773 if (tgt < 0) {
4796 4774 tgt = -1;
4797 4775 for (; *p && *p != ','; p++)
4798 4776 ;
4799 4777 break;
4800 4778 }
4801 4779 }
4802 4780 tgt_port_end = p;
4803 4781
4804 4782 /* parse hex ",lun" part of "tgt,lun[,sfunc]" */
4805 4783 if (*p)
4806 4784 p++;
4807 4785 for (lun64 = 0; *p && *p != ','; p++) {
4808 4786 if (*p >= '0' && *p <= '9')
4809 4787 lun64 = (lun64 << 4) + (*p - '0');
4810 4788 else if (*p >= 'a' && *p <= 'f')
4811 4789 lun64 = (lun64 << 4) + 10 + (*p - 'a');
4812 4790 else
4813 4791 return (0);
4814 4792 }
4815 4793
4816 4794 /* parse hex ",sfunc" part of "tgt,lun[,sfunc]" */
4817 4795 if (*p) {
4818 4796 p++;
4819 4797 for (sfunc = 0; *p; p++) {
4820 4798 if (*p >= '0' && *p <= '9')
4821 4799 sfunc = (sfunc << 4) + (*p - '0');
4822 4800 else if (*p >= 'a' && *p <= 'f')
4823 4801 sfunc = (sfunc << 4) + 10 + (*p - 'a');
4824 4802 else
4825 4803 return (0);
4826 4804 }
4827 4805 } else
4828 4806 sfunc = -1;
4829 4807
4830 4808 if (dchild) {
4831 4809 /*
4832 4810 * Decorate a devinfo node with unit address properties.
4833 4811 * This adds the the addressing properties needed to
4834 4812 * DDI_CTLOPS_UNINITCHILD the devinfo node (i.e. perform
4835 4813 * the reverse operation - form unit address from properties).
4836 4814 */
4837 4815 if ((tgt != -1) && (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4838 4816 SCSI_ADDR_PROP_TARGET, tgt) != DDI_PROP_SUCCESS))
4839 4817 return (0);
4840 4818
4841 4819 if (tgt_port_end) {
4842 4820 tgt_port_len = tgt_port_end - ua + 1;
4843 4821 tgt_port = kmem_alloc(tgt_port_len, KM_SLEEP);
4844 4822 (void) strlcpy(tgt_port, ua, tgt_port_len);
4845 4823 if (ndi_prop_update_string(DDI_DEV_T_NONE, dchild,
4846 4824 SCSI_ADDR_PROP_TARGET_PORT, tgt_port) !=
4847 4825 DDI_PROP_SUCCESS) {
4848 4826 kmem_free(tgt_port, tgt_port_len);
4849 4827 return (0);
4850 4828 }
4851 4829 kmem_free(tgt_port, tgt_port_len);
4852 4830 }
4853 4831
4854 4832 /* Set the appropriate lun properties. */
4855 4833 if (lun64 < SCSI_32LUNS_PER_TARGET) {
4856 4834 if (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4857 4835 SCSI_ADDR_PROP_LUN, (int)lun64) != DDI_PROP_SUCCESS)
4858 4836 return (0);
4859 4837 }
4860 4838 if (ndi_prop_update_int64(DDI_DEV_T_NONE, dchild,
4861 4839 SCSI_ADDR_PROP_LUN64, lun64) != DDI_PROP_SUCCESS)
4862 4840 return (0);
4863 4841
4864 4842 /* Set the sfunc property */
4865 4843 if ((sfunc != -1) &&
4866 4844 (ndi_prop_update_int(DDI_DEV_T_NONE, dchild,
4867 4845 SCSI_ADDR_PROP_SFUNC, (int)sfunc) != DDI_PROP_SUCCESS))
4868 4846 return (0);
4869 4847 } else if (pchild) {
4870 4848 /*
4871 4849 * Decorate a pathinfo node with unit address properties.
4872 4850 */
4873 4851 if ((tgt != -1) && (mdi_prop_update_int(pchild,
4874 4852 SCSI_ADDR_PROP_TARGET, tgt) != DDI_PROP_SUCCESS))
4875 4853 return (0);
4876 4854
4877 4855 if (tgt_port_end) {
4878 4856 tgt_port_len = tgt_port_end - ua + 1;
4879 4857 tgt_port = kmem_alloc(tgt_port_len, KM_SLEEP);
4880 4858 (void) strlcpy(tgt_port, ua, tgt_port_len);
4881 4859 if (mdi_prop_update_string(pchild,
4882 4860 SCSI_ADDR_PROP_TARGET_PORT, tgt_port) !=
4883 4861 DDI_PROP_SUCCESS) {
4884 4862 kmem_free(tgt_port, tgt_port_len);
4885 4863 return (0);
4886 4864 }
4887 4865 kmem_free(tgt_port, tgt_port_len);
4888 4866 }
4889 4867
4890 4868 /* Set the appropriate lun properties */
4891 4869 if (lun64 < SCSI_32LUNS_PER_TARGET) {
4892 4870 if (mdi_prop_update_int(pchild, SCSI_ADDR_PROP_LUN,
4893 4871 (int)lun64) != DDI_PROP_SUCCESS)
4894 4872 return (0);
4895 4873 }
4896 4874
4897 4875 if (mdi_prop_update_int64(pchild, SCSI_ADDR_PROP_LUN64,
4898 4876 lun64) != DDI_PROP_SUCCESS)
4899 4877 return (0);
4900 4878
4901 4879 /* Set the sfunc property */
4902 4880 if ((sfunc != -1) &&
4903 4881 (mdi_prop_update_int(pchild,
4904 4882 SCSI_ADDR_PROP_SFUNC, (int)sfunc) != DDI_PROP_SUCCESS))
4905 4883 return (0);
4906 4884 }
4907 4885 return (1);
4908 4886 }
4909 4887
4910 4888 /*
4911 4889 * Private ndi_devi_find/mdi_pi_find implementation - find the child
4912 4890 * dev_info/path_info of self whose phci name matches "name@caddr".
4913 4891 * We have our own implementation because we need to search with both
4914 4892 * forms of sibling lists (dev_info and path_info) and we need to be able
4915 4893 * to search with a NULL name in order to find siblings already associated
4916 4894 * with a given unit-address (same @addr). NOTE: NULL name search will never
4917 4895 * return probe node.
4918 4896 *
4919 4897 * If pchildp is NULL and we find a pathinfo child, we return the client
4920 4898 * devinfo node in *dchildp.
4921 4899 *
4922 4900 * The init flag argument should be clear when called from places where
4923 4901 * recursion could occur (like scsi_busctl_initchild) and when the caller
4924 4902 * has already performed a search for name@addr with init set (performance).
4925 4903 *
4926 4904 * Future: Integrate ndi_devi_findchild_by_callback into scsi_findchild.
4927 4905 */
4928 4906 static int
4929 4907 scsi_findchild(dev_info_t *self, char *name, char *addr, int init,
4930 4908 dev_info_t **dchildp, mdi_pathinfo_t **pchildp, int *ppi)
4931 4909 {
4932 4910 dev_info_t *dchild; /* devinfo child */
4933 4911 mdi_pathinfo_t *pchild; /* pathinfo child */
4934 4912 int found = CHILD_TYPE_NONE;
4935 4913 char *daddr;
4936 4914
4937 4915 ASSERT(self && DEVI_BUSY_OWNED(self));
4938 4916 ASSERT(addr && dchildp);
4939 4917 if ((self == NULL) || (addr == NULL) || (dchildp == NULL))
4940 4918 return (CHILD_TYPE_NONE);
4941 4919
4942 4920 *dchildp = NULL;
4943 4921 if (pchildp)
4944 4922 *pchildp = NULL;
4945 4923 if (ppi)
4946 4924 *ppi = 0;
4947 4925
4948 4926 /* Walk devinfo child list to find a match */
4949 4927 for (dchild = ddi_get_child(self); dchild;
4950 4928 dchild = ddi_get_next_sibling(dchild)) {
4951 4929 if (i_ddi_node_state(dchild) < DS_INITIALIZED)
4952 4930 continue;
4953 4931
4954 4932 daddr = ddi_get_name_addr(dchild);
4955 4933 if (daddr && (strcmp(addr, daddr) == 0) &&
4956 4934 ((name == NULL) ||
4957 4935 (strcmp(name, DEVI(dchild)->devi_node_name) == 0))) {
4958 4936 /*
4959 4937 * If we are asked to find "anything" at a given
4960 4938 * unit-address (name == NULL), we don't realy want
4961 4939 * to find the 'probe' node. The existance of
4962 4940 * a probe node on a 'name == NULL' search should
4963 4941 * fail. This will trigger slow-path code where
4964 4942 * we explicity look for, and synchronize against,
4965 4943 * a node named "probe" at the unit-address.
4966 4944 */
4967 4945 if ((name == NULL) &&
4968 4946 scsi_hba_devi_is_barrier(dchild)) {
4969 4947 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4970 4948 "%s@%s 'probe' devinfo found, skip",
4971 4949 name ? name : "", addr));
4972 4950 continue;
4973 4951 }
4974 4952
4975 4953 /* We have found a match. */
4976 4954 found |= CHILD_TYPE_DEVINFO;
4977 4955 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
4978 4956 "%s@%s devinfo found", name ? name : "", addr));
4979 4957 *dchildp = dchild; /* devinfo found */
4980 4958 break;
4981 4959 }
4982 4960 }
4983 4961
4984 4962 /*
4985 4963 * Walk pathinfo child list to find a match.
4986 4964 *
4987 4965 * NOTE: Unlike devinfo nodes, pathinfo nodes have a string searchable
4988 4966 * unit-address from creation - so there is no need for an 'init'
4989 4967 * search block of code for pathinfo nodes below.
4990 4968 */
4991 4969 pchild = mdi_pi_find(self, NULL, addr);
4992 4970 if (pchild) {
4993 4971 /*
4994 4972 * NOTE: If name specified and we match a pathinfo unit
4995 4973 * address, we don't check the client node name.
4996 4974 */
4997 4975 if (ppi)
4998 4976 *ppi = mdi_pi_get_path_instance(pchild);
4999 4977 found |= CHILD_TYPE_PATHINFO;
5000 4978
5001 4979 if (pchildp) {
5002 4980 SCSI_HBA_LOG((_LOG(4), self, NULL,
5003 4981 "%s pathinfo found", mdi_pi_spathname(pchild)));
5004 4982 *pchildp = pchild; /* pathinfo found */
5005 4983 } else if (*dchildp == NULL) {
5006 4984 /*
5007 4985 * Did not find a devinfo node, found a pathinfo node,
5008 4986 * but caller did not ask us to return a pathinfo node:
5009 4987 * we return the 'client' devinfo node instead (but
5010 4988 * with CHILD_TYPE_PATHINFO 'found' return value).
5011 4989 */
5012 4990 dchild = mdi_pi_get_client(pchild);
5013 4991 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5014 4992 "%s pathinfo found, client switch",
5015 4993 mdi_pi_spathname(pchild)));
5016 4994
5017 4995 /*
5018 4996 * A pathinfo node always has a 'client' devinfo node,
5019 4997 * but we need to ensure that the 'client' is
5020 4998 * initialized and has a scsi_device structure too.
5021 4999 */
5022 5000 ASSERT(dchild);
5023 5001 if (i_ddi_node_state(dchild) < DS_INITIALIZED) {
5024 5002 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5025 5003 "%s found client, initchild",
5026 5004 mdi_pi_spathname(pchild)));
5027 5005 (void) ddi_initchild(ddi_get_parent(dchild),
5028 5006 dchild);
5029 5007 }
5030 5008 if (i_ddi_node_state(dchild) >= DS_INITIALIZED) {
5031 5009 /* client found and initialized */
5032 5010 *dchildp = dchild;
5033 5011 } else {
5034 5012 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5035 5013 "%s found client, but failed initchild",
5036 5014 mdi_pi_spathname(pchild)));
5037 5015 }
5038 5016 }
5039 5017 }
5040 5018
5041 5019 /* Try devinfo again with initchild of uninitialized nodes */
5042 5020 if ((found == CHILD_TYPE_NONE) && init) {
5043 5021 for (dchild = ddi_get_child(self); dchild;
5044 5022 dchild = ddi_get_next_sibling(dchild)) {
5045 5023 /* skip if checked above */
5046 5024 if (i_ddi_node_state(dchild) >= DS_INITIALIZED)
5047 5025 continue;
5048 5026 /* attempt initchild to establish unit-address */
5049 5027 (void) ddi_initchild(self, dchild);
5050 5028 if (i_ddi_node_state(dchild) < DS_INITIALIZED)
5051 5029 continue;
5052 5030 daddr = ddi_get_name_addr(dchild);
5053 5031 if (daddr &&
5054 5032 ((name == NULL) || (strcmp(name,
5055 5033 DEVI(dchild)->devi_node_name) == 0)) &&
5056 5034 (strcmp(addr, daddr) == 0)) {
5057 5035 found |= CHILD_TYPE_DEVINFO;
5058 5036 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5059 5037 "%s@%s devinfo found post initchild",
5060 5038 name ? name : "", addr));
5061 5039 *dchildp = dchild; /* devinfo found */
5062 5040 break; /* node found */
5063 5041 }
5064 5042 }
5065 5043 }
5066 5044
5067 5045 /*
5068 5046 * We should never find devinfo and pathinfo at the same
5069 5047 * unit-address.
5070 5048 */
5071 5049 ASSERT(found != (CHILD_TYPE_DEVINFO | CHILD_TYPE_PATHINFO));
5072 5050 if (found == (CHILD_TYPE_DEVINFO | CHILD_TYPE_PATHINFO)) {
5073 5051 found = CHILD_TYPE_NONE;
5074 5052 *dchildp = NULL;
5075 5053 *pchildp = NULL;
5076 5054 }
5077 5055 return (found);
5078 5056 }
5079 5057
5080 5058 /*
5081 5059 * Given information about a child device (contained on probe node) construct
5082 5060 * and return a pointer to the dynamic SID devinfo node associated with the
5083 5061 * device. In the creation of this SID node a compatible property for the
5084 5062 * device is formed and used to establish a nodename (via
5085 5063 * /etc/nodename_aliases) and to bind a driver (via /etc/driver_aliases).
5086 5064 *
5087 5065 * If this routine is called then we got a response from a device and
5088 5066 * obtained the inquiry data from the device. Some inquiry results indicate
5089 5067 * that the specific LUN we addressed does not exist, and we don't want to
5090 5068 * bind a standard target driver to the node we create. Even though the
5091 5069 * specific LUN is not usable, the framework may still want to bind a
5092 5070 * target driver to the device for internal communication with the device -
5093 5071 * an example would be issuing a report_lun to enumerate other LUNs under a
5094 5072 * DPQ_NEVER LUN0. Another example would be wanting to known that the
5095 5073 * DPQ_NEVER LUN0 device exists in BUS_CONFIG_ONE for non-existent LUN
5096 5074 * caching optimizations. To support this we let the caller specify a
5097 5075 * compatible property (or driver). If LUN0 inquiry data indicates that the
5098 5076 * LUN does not exist then we establish compat0 as the highest precedence(0)
5099 5077 * compatible form. If used, this compat0 driver will never be called on to
5100 5078 * issue external commands to the device.
5101 5079 *
5102 5080 * If no driver binds to the device using driver_alias we establish the driver
5103 5081 * passed in as the node name.
5104 5082 */
5105 5083
5106 5084 extern int e_devid_cache_pathinfo(mdi_pathinfo_t *, ddi_devid_t);
5107 5085
5108 5086 static int
5109 5087 scsi_device_createchild(dev_info_t *self, char *addr, scsi_enum_t se,
5110 5088 struct scsi_device *sdprobe, dev_info_t **dchildp, mdi_pathinfo_t **pchildp)
5111 5089 {
5112 5090 scsi_lun64_t lun64;
5113 5091 int dtype;
5114 5092 int dpq;
5115 5093 int dpq_vu;
5116 5094 int dtype_node;
5117 5095 int lunexists;
5118 5096 char *compat0;
5119 5097 char *nname;
5120 5098 char **compat = NULL;
5121 5099 int ncompat;
5122 5100 dev_info_t *dchild = NULL;
5123 5101 mdi_pathinfo_t *pchild = NULL;
5124 5102 dev_info_t *probe = sdprobe->sd_dev;
5125 5103 struct scsi_inquiry *inq = sdprobe->sd_inq;
5126 5104 uchar_t *inq80 = NULL;
5127 5105 uchar_t *inq83 = NULL;
5128 5106 uint_t inq80len, inq83len;
5129 5107 char *binding_set = NULL;
5130 5108 char *dname = NULL;
5131 5109 ddi_devid_t devid;
5132 5110 int have_devid = 0;
5133 5111 ddi_devid_t cdevid;
5134 5112 int have_cdevid = 0;
5135 5113 char *devid_str;
5136 5114 char *guid = NULL;
5137 5115
5138 5116 ASSERT(self && addr && *addr && DEVI_BUSY_OWNED(self));
5139 5117 ASSERT(dchildp && pchildp);
5140 5118
5141 5119 /*
5142 5120 * Determine the lun and whether the lun exists. We may need to create
5143 5121 * a node for LUN0 (with compat0 driver binding) even if the lun does
5144 5122 * not exist - so we can run report_lun to find additional LUNs.
5145 5123 */
5146 5124 lun64 = scsi_addr_to_lun64(addr);
5147 5125 dtype = inq->inq_dtype & DTYPE_MASK; /* device */
5148 5126 dpq = inq->inq_dtype & DPQ_MASK;
5149 5127 dpq_vu = inq->inq_dtype & DPQ_VUNIQ ? 1 : 0;
5150 5128
5151 5129 dtype_node = scsi_addr_to_sfunc(addr); /* secondary function */
5152 5130 if (dtype_node == -1)
5153 5131 dtype_node = dtype; /* node for device */
5154 5132
5155 5133 lunexists = (dtype != dtype_node) || /* override */
5156 5134 ((dpq_vu == 0) && (dpq == DPQ_POSSIBLE)) || /* ANSII */
5157 5135 (dpq_vu && (lun64 == 0)); /* VU LUN0 */
5158 5136 if (dtype == DTYPE_UNKNOWN)
5159 5137 lunexists = 0;
5160 5138
5161 5139 SCSI_HBA_LOG((_LOG(4), self, NULL,
5162 5140 "@%s dtype %x %x dpq_vu %d dpq %x: %d",
5163 5141 addr, dtype, dtype_node, dpq_vu, dpq, lunexists));
5164 5142
5165 5143 /* A non-existent LUN0 uses compatible_nodev. */
5166 5144 if (lunexists) {
5167 5145 compat0 = NULL; /* compat0 not needed */
5168 5146 } else if (lun64 == 0) {
5169 5147 compat0 = compatible_nodev;
5170 5148 SCSI_HBA_LOG((_LOG(2), self, NULL,
5171 5149 "@%s lun 0 with compat0 %s", addr, compat0));
5172 5150 } else
5173 5151 goto out; /* no node created */
5174 5152
5175 5153 /* Obtain identity information from probe node. */
5176 5154 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, probe,
5177 5155 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "inquiry-page-80",
5178 5156 &inq80, &inq80len) != DDI_PROP_SUCCESS)
5179 5157 inq80 = NULL;
5180 5158 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, probe,
5181 5159 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "inquiry-page-83",
5182 5160 &inq83, &inq83len) != DDI_PROP_SUCCESS)
5183 5161 inq83 = NULL;
5184 5162
5185 5163 /* Get "scsi-binding-set" property (if there is one). */
5186 5164 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
5187 5165 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
5188 5166 "scsi-binding-set", &binding_set) == DDI_PROP_SUCCESS)
5189 5167 SCSI_HBA_LOG((_LOG(2), NULL, probe,
5190 5168 "binding_set '%s'", binding_set));
5191 5169
5192 5170 /* determine the node name and compatible information */
5193 5171 scsi_hba_ident_nodename_compatible_get(inq,
5194 5172 inq80, inq80len, inq83, inq83len, binding_set, dtype_node,
5195 5173 compat0, &nname, &dname, &compat, &ncompat);
5196 5174
5197 5175 if (nname == NULL) {
5198 5176 /*
5199 5177 * We will not be able to create a node because we could not
5200 5178 * determine a node name. Print out a NODRIVER level warning
5201 5179 * message with the compatible forms for the device. Note that
5202 5180 * there may be a driver.conf node that attaches to the device,
5203 5181 * which is why we only produce this warning message for debug
5204 5182 * kernels.
5205 5183 */
5206 5184 SCSI_HBA_LOG((_LOG(1), NULL, self,
5207 5185 "no node_name for device @%s:\n compatible: %s",
5208 5186 addr, *compat));
5209 5187 goto out;
5210 5188 }
5211 5189
5212 5190 /*
5213 5191 * FUTURE: some day we may want an accurate "compatible" on the probe
5214 5192 * node so that vhci_is_dev_supported() in scsi_vhci could, at
5215 5193 * least in part, determine/configure based on "compatible".
5216 5194 *
5217 5195 * if (ndi_prop_update_string_array(DDI_DEV_T_NONE, probe,
5218 5196 * "compatible", compat, ncompat) != DDI_PROP_SUCCESS) {
5219 5197 * SCSI_HBA_LOG((_LOG(3), self, NULL,
5220 5198 * "%s@%s failed probe compatible decoration",
5221 5199 * nname, addr));
5222 5200 * goto out;
5223 5201 * }
5224 5202 */
5225 5203
5226 5204 /* Encode devid from identity information. */
5227 5205 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST, dname,
5228 5206 (uchar_t *)inq, sizeof (*inq), inq80, inq80len, inq83, inq83len,
5229 5207 &devid) == DDI_SUCCESS) {
5230 5208 have_devid = 1;
5231 5209
5232 5210 /* Attempt to form guid from devid. */
5233 5211 guid = ddi_devid_to_guid(devid);
5234 5212
5235 5213 /* Produce string devid for debug. */
5236 5214 devid_str = ddi_devid_str_encode(devid, NULL);
5237 5215 SCSI_HBA_LOG((_LOG(3), self, probe, "devid '%s' guid '%s'",
5238 5216 devid_str ? devid_str : "NULL", guid ? guid : "NULL"));
5239 5217 ddi_devid_str_free(devid_str);
5240 5218 }
5241 5219
5242 5220
5243 5221 /*
5244 5222 * Determine if the device should be enumerated as under the vHCI
5245 5223 * (client node) or under the pHCI. By convention scsi_vhci expects
5246 5224 * the "cinfo" argument identity information to be represented as a
5247 5225 * devinfo node with the needed information (i.e. the pHCI probe node).
5248 5226 */
5249 5227 if ((guid == NULL) ||
5250 5228 (mdi_is_dev_supported(MDI_HCI_CLASS_SCSI, self, sdprobe) !=
5251 5229 MDI_SUCCESS)) {
5252 5230 SCSI_HBA_LOG((_LOG(3), self, probe, "==> devinfo"));
5253 5231
5254 5232 /*
5255 5233 * Enumerate under pHCI:
5256 5234 *
5257 5235 * Create dynamic SID dchild node. No attempt is made to
5258 5236 * transfer information (except the addressing and identity
5259 5237 * information) from the probe node to the dynamic node since
5260 5238 * there may be HBA specific side effects that the framework
5261 5239 * does not known how to transfer.
5262 5240 */
5263 5241 ndi_devi_alloc_sleep(self, nname,
5264 5242 (se == SE_HP) ? DEVI_SID_HP_NODEID : DEVI_SID_NODEID,
5265 5243 &dchild);
5266 5244 ASSERT(dchild);
5267 5245 ndi_flavor_set(dchild, SCSA_FLAVOR_SCSI_DEVICE);
5268 5246
5269 5247 /*
5270 5248 * Decorate new node with addressing properties (via
5271 5249 * scsi_hba_ua_set()), compatible, identity information, and
5272 5250 * class.
5273 5251 */
5274 5252 if ((scsi_hba_ua_set(addr, dchild, NULL) == 0) ||
5275 5253 (ndi_prop_update_string_array(DDI_DEV_T_NONE, dchild,
5276 5254 "compatible", compat, ncompat) != DDI_PROP_SUCCESS) ||
5277 5255 (inq80 && (ndi_prop_update_byte_array(DDI_DEV_T_NONE,
5278 5256 dchild, "inquiry-page-80", inq80, inq80len) !=
5279 5257 DDI_PROP_SUCCESS)) ||
5280 5258 (inq83 && (ndi_prop_update_byte_array(DDI_DEV_T_NONE,
5281 5259 dchild, "inquiry-page-83", inq83, inq83len) !=
5282 5260 DDI_PROP_SUCCESS)) ||
5283 5261 (ndi_prop_update_string(DDI_DEV_T_NONE, dchild,
5284 5262 "class", "scsi") != DDI_PROP_SUCCESS)) {
5285 5263 SCSI_HBA_LOG((_LOG(2), self, NULL,
5286 5264 "devinfo @%s failed decoration", addr));
5287 5265 (void) scsi_hba_remove_node(dchild);
5288 5266 dchild = NULL;
5289 5267 goto out;
5290 5268 }
5291 5269
5292 5270 /* Bind the driver */
5293 5271 if (ndi_devi_bind_driver(dchild, 0) != NDI_SUCCESS) {
5294 5272 /* need to bind in order to register a devid */
5295 5273 SCSI_HBA_LOG((_LOGCFG, NULL, dchild,
5296 5274 "devinfo @%s created, no driver-> "
5297 5275 "no devid_register", addr));
5298 5276 goto out;
5299 5277 }
5300 5278
5301 5279 /* Register devid */
5302 5280 if (have_devid) {
5303 5281 if (ddi_devid_register(dchild, devid) == DDI_FAILURE)
5304 5282 SCSI_HBA_LOG((_LOG(1), NULL, dchild,
5305 5283 "devinfo @%s created, "
5306 5284 "devid register failed", addr));
5307 5285 else
5308 5286 SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5309 5287 "devinfo @%s created with devid", addr));
5310 5288 } else
5311 5289 SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5312 5290 "devinfo @%s created, no devid", addr));
5313 5291 } else {
5314 5292 /*
5315 5293 * Enumerate under vHCI:
5316 5294 *
5317 5295 * Create a pathinfo pchild node.
5318 5296 */
5319 5297 SCSI_HBA_LOG((_LOG(3), self, probe, "==>pathinfo"));
5320 5298
5321 5299 if (mdi_pi_alloc_compatible(self, nname, guid, addr, compat,
5322 5300 ncompat, 0, &pchild) != MDI_SUCCESS) {
5323 5301 SCSI_HBA_LOG((_LOG(2), self, probe,
5324 5302 "pathinfo alloc failed"));
5325 5303 goto out;
5326 5304 }
5327 5305
5328 5306 ASSERT(pchild);
5329 5307 dchild = mdi_pi_get_client(pchild);
5330 5308 ASSERT(dchild);
5331 5309 ndi_flavor_set(dchild, SCSA_FLAVOR_SCSI_DEVICE);
5332 5310
5333 5311 /*
5334 5312 * Decorate new node with addressing properties via
5335 5313 * scsi_hba_ua_set().
5336 5314 */
5337 5315 if (scsi_hba_ua_set(addr, NULL, pchild) == 0) {
5338 5316 SCSI_HBA_LOG((_LOG(1), self, NULL,
5339 5317 "pathinfo %s decoration failed",
5340 5318 mdi_pi_spathname(pchild)));
5341 5319 (void) mdi_pi_free(pchild, 0);
5342 5320 pchild = NULL;
5343 5321 goto out;
5344 5322 }
5345 5323
5346 5324 /* Bind the driver */
5347 5325 if (ndi_devi_bind_driver(dchild, 0) != NDI_SUCCESS) {
5348 5326 /* need to bind in order to register a devid */
5349 5327 SCSI_HBA_LOG((_LOGCFG, self, NULL,
5350 5328 "pathinfo %s created, no client driver-> "
5351 5329 "no devid_register", mdi_pi_spathname(pchild)));
5352 5330 goto out;
5353 5331 }
5354 5332
5355 5333 /* Watch out for inconsistancies in devids. */
5356 5334 if (ddi_devid_get(dchild, &cdevid) == DDI_SUCCESS)
5357 5335 have_cdevid = 1;
5358 5336
5359 5337 if (have_devid && !have_cdevid) {
5360 5338 /* Client does not yet have devid, register ours. */
5361 5339 if (ddi_devid_register(dchild, devid) == DDI_FAILURE)
5362 5340 SCSI_HBA_LOG((_LOG(1), self, NULL,
5363 5341 "pathinfo %s created, "
5364 5342 "devid register failed",
5365 5343 mdi_pi_spathname(pchild)));
5366 5344 else
5367 5345 SCSI_HBA_LOG((_LOG(2), self, NULL,
5368 5346 "pathinfo %s created with devid",
5369 5347 mdi_pi_spathname(pchild)));
5370 5348 } else if (have_devid && have_cdevid) {
5371 5349 /*
5372 5350 * We have devid and client already has devid:
5373 5351 * they must be the same.
5374 5352 */
5375 5353 if (ddi_devid_compare(cdevid, devid) != 0) {
5376 5354 SCSI_HBA_LOG((_LOG(WARN), NULL, dchild,
5377 5355 "mismatched devid on path %s",
5378 5356 mdi_pi_spathname(pchild)));
5379 5357 }
5380 5358 } else if (!have_devid && have_cdevid) {
5381 5359 /*
5382 5360 * Client already has a devid, but we don't:
5383 5361 * we should not have missing devids.
5384 5362 */
5385 5363 SCSI_HBA_LOG((_LOG(WARN), NULL, dchild,
5386 5364 "missing devid on path %s",
5387 5365 mdi_pi_spathname(pchild)));
5388 5366 } else if (!have_cdevid && !have_devid) {
5389 5367 /* devid not supported */
5390 5368 SCSI_HBA_LOG((_LOG(2), self, NULL,
5391 5369 "pathinfo %s created, no devid",
5392 5370 mdi_pi_spathname(pchild)));
5393 5371 }
5394 5372
5395 5373 /*
5396 5374 * The above has registered devid for the device under
5397 5375 * the client node. Now register it under the full pHCI
5398 5376 * path to the device. We'll get an entry equivalent to
5399 5377 * booting with mpxio disabled. This is needed for
5400 5378 * telemetry during enumeration.
5401 5379 */
5402 5380 if (e_devid_cache_pathinfo(pchild, devid) == DDI_SUCCESS) {
5403 5381 SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5404 5382 "pathinfo @%s created with devid", addr));
5405 5383 } else {
5406 5384 SCSI_HBA_LOG((_LOG(1), NULL, dchild,
5407 5385 "pathinfo @%s devid cache failed", addr));
5408 5386 }
5409 5387 }
5410 5388
5411 5389 /* free the node name and compatible information */
5412 5390 out: if (have_devid)
5413 5391 ddi_devid_free(devid);
5414 5392 if (have_cdevid)
5415 5393 ddi_devid_free(cdevid);
5416 5394 if (guid)
5417 5395 ddi_devid_free_guid(guid);
5418 5396 if (compat)
5419 5397 scsi_hba_ident_nodename_compatible_free(nname, dname, compat);
5420 5398 if (inq80)
5421 5399 ddi_prop_free(inq80);
5422 5400 if (inq83)
5423 5401 ddi_prop_free(inq83);
5424 5402 if (binding_set)
5425 5403 ddi_prop_free(binding_set);
5426 5404
5427 5405 /* return child_type results */
5428 5406 if (pchild) {
5429 5407 *dchildp = NULL;
5430 5408 *pchildp = pchild;
5431 5409 return (CHILD_TYPE_PATHINFO);
5432 5410 } else if (dchild) {
5433 5411 *dchildp = dchild;
5434 5412 *pchildp = NULL;
5435 5413 return (CHILD_TYPE_DEVINFO);
5436 5414 }
5437 5415
5438 5416 return (CHILD_TYPE_NONE);
5439 5417 }
5440 5418
5441 5419 /*
5442 5420 * Call scsi_device_createchild and then initchild the new node.
5443 5421 */
5444 5422 static dev_info_t *
5445 5423 scsi_device_configchild(dev_info_t *self, char *addr, scsi_enum_t se,
5446 5424 struct scsi_device *sdprobe, int *circp, int *ppi)
5447 5425 {
5448 5426 int child_type;
5449 5427 dev_info_t *dchild;
5450 5428 mdi_pathinfo_t *pchild;
5451 5429 dev_info_t *child;
5452 5430 int rval;
5453 5431
5454 5432 ASSERT(self && addr && *addr && DEVI_BUSY_OWNED(self));
5455 5433 if (ppi)
5456 5434 *ppi = 0;
5457 5435
5458 5436 child_type = scsi_device_createchild(self, addr, se, sdprobe,
5459 5437 &dchild, &pchild);
5460 5438
5461 5439 /*
5462 5440 * Prevent multiple initialized (tran_tgt_init) nodes associated with
5463 5441 * the same @addr at the same time by calling tran_tgt_free() on the
5464 5442 * probe node prior to promotion of the 'real' node. After the call
5465 5443 * to scsi_hba_barrier_tran_tgt_free(), the HBA no longer has any
5466 5444 * probe node context.
5467 5445 */
5468 5446 scsi_hba_barrier_tran_tgt_free(sdprobe->sd_dev);
5469 5447
5470 5448 switch (child_type) {
5471 5449 case CHILD_TYPE_NONE:
5472 5450 child = NULL;
5473 5451 break;
5474 5452
5475 5453 case CHILD_TYPE_PATHINFO:
5476 5454 /*
5477 5455 * Online pathinfo: Hold the path and exit the pHCI while
5478 5456 * calling mdi_pi_online() to avoid deadlock with power
5479 5457 * management of pHCI.
5480 5458 */
5481 5459 ASSERT(MDI_PHCI(self));
5482 5460 mdi_hold_path(pchild);
5483 5461 scsi_hba_devi_exit_phci(self, *circp);
5484 5462
5485 5463 rval = mdi_pi_online(pchild, 0);
5486 5464
5487 5465 scsi_hba_devi_enter_phci(self, circp);
5488 5466 mdi_rele_path(pchild);
5489 5467
5490 5468 if (rval != MDI_SUCCESS) {
5491 5469 /* pathinfo form of "failed during tran_tgt_init" */
5492 5470 scsi_enumeration_failed(NULL, se,
5493 5471 mdi_pi_spathname(pchild), "path online");
5494 5472 (void) mdi_pi_free(pchild, 0);
5495 5473 return (NULL);
5496 5474 }
5497 5475
5498 5476 /*
5499 5477 * Return the path_instance of the pathinfo node.
5500 5478 *
5501 5479 * NOTE: We assume that sd_inq is not path-specific.
5502 5480 */
5503 5481 if (ppi)
5504 5482 *ppi = mdi_pi_get_path_instance(pchild);
5505 5483
5506 5484
5507 5485 /*
5508 5486 * Fallthrough into CHILD_TYPE_DEVINFO code to promote
5509 5487 * the 'client' devinfo node as a dchild.
5510 5488 */
5511 5489 dchild = mdi_pi_get_client(pchild);
5512 5490 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5513 5491 "pathinfo online successful"));
5514 5492 /* FALLTHROUGH */
5515 5493
5516 5494 case CHILD_TYPE_DEVINFO:
5517 5495 /*
5518 5496 * For now, we ndi_devi_online() the child because some other
5519 5497 * parts of the IO framework, like degenerate devid code,
5520 5498 * depend on bus_config driving nodes to DS_ATTACHED. At some
5521 5499 * point in the future, to keep things light-weight, we would
5522 5500 * like to change the ndi_devi_online call below to be
5523 5501 *
5524 5502 * if (ddi_initchild(self, dchild) != DDI_SUCCESS)
5525 5503 *
5526 5504 * This would promote the node so that framework code could
5527 5505 * find the child with an @addr search, but does not incur
5528 5506 * attach(9E) overhead for BUS_CONFIG_ALL cases where the
5529 5507 * framework is not interested in attach of the node.
5530 5508 *
5531 5509 * NOTE: If the addr specified has incorrect syntax (busconfig
5532 5510 * one of bogus /devices path) then call below can fail.
5533 5511 */
5534 5512 if (ndi_devi_online(dchild, 0) != NDI_SUCCESS) {
5535 5513 SCSI_HBA_LOG((_LOG(2), NULL, dchild,
5536 5514 "devinfo online failed"));
5537 5515
5538 5516 /* failed online does not remove the node */
5539 5517 (void) scsi_hba_remove_node(dchild);
5540 5518 return (NULL);
5541 5519 }
5542 5520 SCSI_HBA_LOG((_LOG(4), NULL, dchild,
5543 5521 "devinfo initchild successful"));
5544 5522 child = dchild;
5545 5523 break;
5546 5524 }
5547 5525 return (child);
5548 5526 }
5549 5527
5550 5528 void
5551 5529 scsi_hba_pkt_comp(struct scsi_pkt *pkt)
5552 5530 {
5553 5531 scsi_hba_tran_t *tran;
5554 5532 uint8_t *sensep;
5555 5533
5556 5534 ASSERT(pkt);
5557 5535
5558 5536 /*
5559 5537 * Catch second call on the same packet before doing anything else.
5560 5538 */
5561 5539 if (pkt->pkt_flags & FLAG_PKT_COMP_CALLED) {
5562 5540 cmn_err(
5563 5541 #ifdef DEBUG
5564 5542 CE_PANIC,
5565 5543 #else
5566 5544 CE_WARN,
5567 5545 #endif
5568 5546 "%s duplicate scsi_hba_pkt_comp(9F) on same scsi_pkt(9S)",
5569 5547 mod_containing_pc(caller()));
5570 5548 }
5571 5549
5572 5550 pkt->pkt_flags |= FLAG_PKT_COMP_CALLED;
5573 5551
5574 5552 if (pkt->pkt_comp == NULL)
5575 5553 return;
5576 5554
5577 5555 /*
5578 5556 * For HBA drivers that implement tran_setup_pkt(9E), if we are
5579 5557 * completing a 'consistent' mode DMA operation then we must
5580 5558 * perform dma_sync prior to calling pkt_comp to ensure that
5581 5559 * the target driver sees the correct data in memory.
5582 5560 */
5583 5561 ASSERT((pkt->pkt_flags & FLAG_NOINTR) == 0);
5584 5562 if (((pkt->pkt_dma_flags & DDI_DMA_CONSISTENT) &&
5585 5563 (pkt->pkt_dma_flags & DDI_DMA_READ)) &&
5586 5564 ((P_TO_TRAN(pkt)->tran_setup_pkt) != NULL)) {
5587 5565 scsi_sync_pkt(pkt);
5588 5566 }
5589 5567
5590 5568 /*
5591 5569 * If the HBA driver is using SCSAv3 scsi_hba_tgtmap_create enumeration
5592 5570 * then we detect the special ASC/ASCQ completion codes that indicate
5593 5571 * that the lun configuration of a target has changed. Since we need to
5594 5572 * be determine scsi_device given scsi_address enbedded in
5595 5573 * scsi_pkt (via scsi_address_device(9F)), we also require use of
5596 5574 * SCSI_HBA_ADDR_COMPLEX.
5597 5575 */
5598 5576 tran = pkt->pkt_address.a_hba_tran;
5599 5577 ASSERT(tran);
5600 5578 if ((tran->tran_tgtmap == NULL) ||
5601 5579 !(tran->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX))
5602 5580 goto comp; /* not using tgtmap */
5603 5581
5604 5582 /*
5605 5583 * Check for lun-change notification and queue the scsi_pkt for
5606 5584 * lunchg1 processing. The 'pkt_comp' call to the target driver
5607 5585 * is part of lunchg1 processing.
5608 5586 */
5609 5587 if ((pkt->pkt_reason == CMD_CMPLT) &&
5610 5588 (((*pkt->pkt_scbp) & STATUS_MASK) == STATUS_CHECK) &&
5611 5589 (pkt->pkt_state & STATE_ARQ_DONE)) {
5612 5590 sensep = (uint8_t *)&(((struct scsi_arq_status *)(uintptr_t)
5613 5591 (pkt->pkt_scbp))->sts_sensedata);
5614 5592 if (((scsi_sense_key(sensep) == KEY_UNIT_ATTENTION) &&
5615 5593 (scsi_sense_asc(sensep) == 0x3f) &&
5616 5594 (scsi_sense_ascq(sensep) == 0x0e)) ||
5617 5595
5618 5596 ((scsi_sense_key(sensep) == KEY_UNIT_ATTENTION) &&
5619 5597 (scsi_sense_asc(sensep) == 0x25) &&
5620 5598 (scsi_sense_ascq(sensep) == 0x00))) {
5621 5599 /*
5622 5600 * The host adaptor is done with the packet, we use
5623 5601 * pkt_stmp stage-temporary to link the packet for
5624 5602 * lunchg1 processing.
5625 5603 *
5626 5604 * NOTE: pkt_ha_private is not available since its use
5627 5605 * extends to tran_teardown_pkt.
5628 5606 */
5629 5607 mutex_enter(&scsi_lunchg1_mutex);
5630 5608 pkt->pkt_stmp = scsi_lunchg1_list;
5631 5609 scsi_lunchg1_list = pkt;
5632 5610 if (pkt->pkt_stmp == NULL)
5633 5611 (void) cv_signal(&scsi_lunchg1_cv);
5634 5612 mutex_exit(&scsi_lunchg1_mutex);
5635 5613 return;
5636 5614 }
5637 5615 }
5638 5616
5639 5617 comp: (*pkt->pkt_comp)(pkt);
5640 5618 }
5641 5619
5642 5620 /*
5643 5621 * return 1 if the specified node is a barrier/probe node
5644 5622 */
5645 5623 static int
5646 5624 scsi_hba_devi_is_barrier(dev_info_t *probe)
5647 5625 {
5648 5626 if (probe && (strcmp(ddi_node_name(probe), "probe") == 0))
5649 5627 return (1);
5650 5628 return (0);
5651 5629 }
5652 5630
5653 5631 /*
5654 5632 * A host adapter driver is easier to write if we prevent multiple initialized
5655 5633 * (tran_tgt_init) scsi_device structures to the same unit-address at the same
5656 5634 * time. We prevent this from occurring all the time during the barrier/probe
5657 5635 * node to real child hand-off by calling scsi_hba_barrier_tran_tgt_free
5658 5636 * on the probe node prior to ddi_inichild of the 'real' node. As part of
5659 5637 * this early tran_tgt_free implementation, we must also call this function
5660 5638 * as we put a probe node on the scsi_hba_barrier_list.
5661 5639 */
5662 5640 static void
5663 5641 scsi_hba_barrier_tran_tgt_free(dev_info_t *probe)
5664 5642 {
5665 5643 struct scsi_device *sdprobe;
5666 5644 dev_info_t *self;
5667 5645 scsi_hba_tran_t *tran;
5668 5646
5669 5647 ASSERT(probe && scsi_hba_devi_is_barrier(probe));
5670 5648
5671 5649 /* Return if we never called tran_tgt_init(9E). */
5672 5650 if (i_ddi_node_state(probe) < DS_INITIALIZED)
5673 5651 return;
5674 5652
5675 5653 sdprobe = ddi_get_driver_private(probe);
5676 5654 self = ddi_get_parent(probe);
5677 5655 ASSERT(sdprobe && self);
5678 5656 tran = ddi_get_driver_private(self);
5679 5657 ASSERT(tran);
5680 5658
5681 5659 if (tran->tran_tgt_free) {
5682 5660 /*
5683 5661 * To correctly support TRAN_CLONE, we need to use the same
5684 5662 * cloned scsi_hba_tran(9S) structure for both tran_tgt_init(9E)
5685 5663 * and tran_tgt_free(9E).
5686 5664 */
5687 5665 if (tran->tran_hba_flags & SCSI_HBA_TRAN_CLONE)
5688 5666 tran = sdprobe->sd_address.a_hba_tran;
5689 5667
5690 5668 if (!sdprobe->sd_tran_tgt_free_done) {
5691 5669 SCSI_HBA_LOG((_LOG(4), NULL, probe,
5692 5670 "tran_tgt_free EARLY"));
5693 5671 (*tran->tran_tgt_free) (self, probe, tran, sdprobe);
5694 5672 sdprobe->sd_tran_tgt_free_done = 1;
5695 5673 } else {
5696 5674 SCSI_HBA_LOG((_LOG(4), NULL, probe,
5697 5675 "tran_tgt_free EARLY already done"));
5698 5676 }
5699 5677 }
5700 5678 }
5701 5679
5702 5680 /*
5703 5681 * Add an entry to the list of barrier nodes to be asynchronously deleted by
5704 5682 * the scsi_hba_barrier_daemon after the specified timeout. Nodes on
5705 5683 * the barrier list are used to implement the bus_config probe cache
5706 5684 * of non-existent devices. The nodes are at DS_INITIALIZED, so their
5707 5685 * @addr is established for searching. Since devi_ref of a DS_INITIALIZED
5708 5686 * node will *not* prevent demotion, demotion is prevented by setting
5709 5687 * sd_uninit_prevent. Devinfo snapshots attempt to attach probe cache
5710 5688 * nodes, and on failure attempt to demote the node (without the participation
5711 5689 * of bus_unconfig) to DS_BOUND - this demotion is prevented via
5712 5690 * sd_uninit_prevent causing any attempted DDI_CTLOPS_UNINITCHILD to fail.
5713 5691 * Probe nodes are bound to nulldriver. The list is sorted by
5714 5692 * expiration time.
5715 5693 *
5716 5694 * NOTE: If we drove a probe node to DS_ATTACHED, we could use ndi_hold_devi()
5717 5695 * to prevent demotion (instead of sd_uninit_prevent).
5718 5696 */
5719 5697 static void
5720 5698 scsi_hba_barrier_add(dev_info_t *probe, int seconds)
5721 5699 {
5722 5700 struct scsi_hba_barrier *nb;
5723 5701 struct scsi_hba_barrier *b;
5724 5702 struct scsi_hba_barrier **bp;
5725 5703 clock_t endtime;
5726 5704
5727 5705 ASSERT(scsi_hba_devi_is_barrier(probe));
5728 5706
5729 5707 /* HBA is no longer responsible for nodes on the barrier list. */
5730 5708 scsi_hba_barrier_tran_tgt_free(probe);
5731 5709 nb = kmem_alloc(sizeof (struct scsi_hba_barrier), KM_SLEEP);
5732 5710 mutex_enter(&scsi_hba_barrier_mutex);
5733 5711 endtime = ddi_get_lbolt() + drv_usectohz(seconds * MICROSEC);
5734 5712 for (bp = &scsi_hba_barrier_list; (b = *bp) != NULL;
5735 5713 bp = &b->barrier_next)
5736 5714 if (b->barrier_endtime > endtime)
5737 5715 break;
5738 5716 nb->barrier_next = *bp;
5739 5717 nb->barrier_endtime = endtime;
5740 5718 nb->barrier_probe = probe;
5741 5719 *bp = nb;
5742 5720 if (bp == &scsi_hba_barrier_list)
5743 5721 (void) cv_signal(&scsi_hba_barrier_cv);
5744 5722 mutex_exit(&scsi_hba_barrier_mutex);
5745 5723 }
5746 5724
5747 5725 /*
5748 5726 * Attempt to remove devinfo node node, return 1 if removed. We
5749 5727 * don't try to remove barrier nodes that have sd_uninit_prevent set
5750 5728 * (even though they should fail device_uninitchild).
5751 5729 */
5752 5730 static int
5753 5731 scsi_hba_remove_node(dev_info_t *child)
5754 5732 {
5755 5733 dev_info_t *self = ddi_get_parent(child);
5756 5734 struct scsi_device *sd;
5757 5735 int circ;
5758 5736 int remove = 1;
5759 5737 int ret = 0;
5760 5738 char na[SCSI_MAXNAMELEN];
5761 5739
5762 5740 scsi_hba_devi_enter(self, &circ);
5763 5741
5764 5742 /* Honor sd_uninit_prevent on barrier nodes */
5765 5743 if (scsi_hba_devi_is_barrier(child)) {
5766 5744 sd = ddi_get_driver_private(child);
5767 5745 if (sd && sd->sd_uninit_prevent)
5768 5746 remove = 0;
5769 5747 }
5770 5748
5771 5749 if (remove) {
5772 5750 (void) ddi_deviname(child, na);
5773 5751 if (ddi_remove_child(child, 0) != DDI_SUCCESS) {
5774 5752 SCSI_HBA_LOG((_LOG(2), NULL, child,
5775 5753 "remove_node failed"));
5776 5754 } else {
5777 5755 child = NULL; /* child is gone */
5778 5756 SCSI_HBA_LOG((_LOG(4), self, NULL,
5779 5757 "remove_node removed %s", *na ? &na[1] : na));
5780 5758 ret = 1;
5781 5759 }
5782 5760 } else {
5783 5761 SCSI_HBA_LOG((_LOG(4), NULL, child, "remove_node prevented"));
5784 5762 }
5785 5763 scsi_hba_devi_exit(self, circ);
5786 5764 return (ret);
5787 5765 }
5788 5766
5789 5767 /*
5790 5768 * The asynchronous barrier deletion daemon. Waits for a barrier timeout
5791 5769 * to expire, then deletes the barrier (removes it as a child).
5792 5770 */
5793 5771 /*ARGSUSED*/
5794 5772 static void
5795 5773 scsi_hba_barrier_daemon(void *arg)
5796 5774 {
5797 5775 struct scsi_hba_barrier *b;
5798 5776 dev_info_t *probe;
5799 5777 callb_cpr_t cprinfo;
5800 5778 int circ;
5801 5779 dev_info_t *self;
5802 5780
5803 5781 CALLB_CPR_INIT(&cprinfo, &scsi_hba_barrier_mutex,
5804 5782 callb_generic_cpr, "scsi_hba_barrier_daemon");
5805 5783 again: mutex_enter(&scsi_hba_barrier_mutex);
5806 5784 for (;;) {
5807 5785 b = scsi_hba_barrier_list;
5808 5786 if (b == NULL) {
5809 5787 /* all barriers expired, wait for barrier_add */
5810 5788 CALLB_CPR_SAFE_BEGIN(&cprinfo);
5811 5789 (void) cv_wait(&scsi_hba_barrier_cv,
5812 5790 &scsi_hba_barrier_mutex);
5813 5791 CALLB_CPR_SAFE_END(&cprinfo, &scsi_hba_barrier_mutex);
5814 5792 } else {
5815 5793 if (ddi_get_lbolt() >= b->barrier_endtime) {
5816 5794 /*
5817 5795 * Drop and retry if ordering issue. Do this
5818 5796 * before calling scsi_hba_remove_node() and
5819 5797 * deadlocking.
5820 5798 */
5821 5799 probe = b->barrier_probe;
5822 5800 self = ddi_get_parent(probe);
5823 5801 if (scsi_hba_devi_tryenter(self, &circ) == 0) {
5824 5802 delay: mutex_exit(&scsi_hba_barrier_mutex);
5825 5803 delay_random(5);
5826 5804 goto again;
5827 5805 }
5828 5806
5829 5807 /* process expired barrier */
5830 5808 if (!scsi_hba_remove_node(probe)) {
5831 5809 /* remove failed, delay and retry */
5832 5810 SCSI_HBA_LOG((_LOG(4), NULL, probe,
5833 5811 "delay expire"));
5834 5812 scsi_hba_devi_exit(self, circ);
5835 5813 goto delay;
5836 5814 }
5837 5815 scsi_hba_barrier_list = b->barrier_next;
5838 5816 kmem_free(b, sizeof (struct scsi_hba_barrier));
5839 5817 scsi_hba_devi_exit(self, circ);
5840 5818 } else {
5841 5819 /* establish timeout for next barrier expire */
5842 5820 (void) cv_timedwait(&scsi_hba_barrier_cv,
5843 5821 &scsi_hba_barrier_mutex,
5844 5822 b->barrier_endtime);
5845 5823 }
5846 5824 }
5847 5825 }
5848 5826 }
5849 5827
5850 5828 /*
5851 5829 * Remove all barriers associated with the specified HBA. This is called
5852 5830 * from from the bus_unconfig implementation to remove probe nodes associated
5853 5831 * with the specified HBA (self) so that probe nodes that have not expired
5854 5832 * will not prevent DR of the HBA.
5855 5833 */
5856 5834 static void
5857 5835 scsi_hba_barrier_purge(dev_info_t *self)
5858 5836 {
5859 5837 struct scsi_hba_barrier **bp;
5860 5838 struct scsi_hba_barrier *b;
5861 5839
5862 5840 mutex_enter(&scsi_hba_barrier_mutex);
5863 5841 for (bp = &scsi_hba_barrier_list; (b = *bp) != NULL; ) {
5864 5842 if (ddi_get_parent(b->barrier_probe) == self) {
5865 5843 if (scsi_hba_remove_node(b->barrier_probe)) {
5866 5844 *bp = b->barrier_next;
5867 5845 kmem_free(b, sizeof (struct scsi_hba_barrier));
5868 5846 } else {
5869 5847 SCSI_HBA_LOG((_LOG(4), NULL, b->barrier_probe,
5870 5848 "skip purge"));
5871 5849 }
5872 5850 } else
5873 5851 bp = &b->barrier_next;
5874 5852 }
5875 5853
5876 5854 mutex_exit(&scsi_hba_barrier_mutex);
5877 5855 }
5878 5856
5879 5857 /*
5880 5858 * LUN-change processing daemons: processing occurs in two stages:
5881 5859 *
5882 5860 * Stage 1: Daemon waits for a lunchg1 queued scsi_pkt, dequeues the pkt,
5883 5861 * forms the path, completes the scsi_pkt (pkt_comp), and
5884 5862 * queues the path for stage 2 processing. The use of stage 1
5885 5863 * avoids issues related to memory allocation in interrupt context
5886 5864 * (scsi_hba_pkt_comp()). We delay the pkt_comp completion until
5887 5865 * after lunchg1 processing forms the path for stage 2 - this is
5888 5866 * done to prevent the target driver from detaching until the
5889 5867 * path formation is complete (driver with outstanding commands
5890 5868 * should not detach).
5891 5869 *
5892 5870 * Stage 2: Daemon waits for a lunchg2 queued request, dequeues the
5893 5871 * request, and opens the path using ldi_open_by_name(). The
5894 5872 * path opened uses a special "@taddr,*" unit address that will
5895 5873 * trigger lun enumeration in scsi_hba_bus_configone(). We
5896 5874 * trigger lun enumeration in stage 2 to avoid problems when
5897 5875 * initial ASC/ASCQ trigger occurs during discovery.
5898 5876 */
5899 5877 /*ARGSUSED*/
5900 5878 static void
5901 5879 scsi_lunchg1_daemon(void *arg)
5902 5880 {
5903 5881 callb_cpr_t cprinfo;
5904 5882 struct scsi_pkt *pkt;
5905 5883 scsi_hba_tran_t *tran;
5906 5884 dev_info_t *self;
5907 5885 struct scsi_device *sd;
5908 5886 char *ua, *p;
5909 5887 char taddr[SCSI_MAXNAMELEN];
5910 5888 char path[MAXPATHLEN];
5911 5889 struct scsi_lunchg2 *lunchg2;
5912 5890
5913 5891 CALLB_CPR_INIT(&cprinfo, &scsi_lunchg1_mutex,
5914 5892 callb_generic_cpr, "scsi_lunchg1_daemon");
5915 5893 mutex_enter(&scsi_lunchg1_mutex);
5916 5894 for (;;) {
5917 5895 pkt = scsi_lunchg1_list;
5918 5896 if (pkt == NULL) {
5919 5897 /* All lunchg1 processing requests serviced, wait. */
5920 5898 CALLB_CPR_SAFE_BEGIN(&cprinfo);
5921 5899 (void) cv_wait(&scsi_lunchg1_cv,
5922 5900 &scsi_lunchg1_mutex);
5923 5901 CALLB_CPR_SAFE_END(&cprinfo, &scsi_lunchg1_mutex);
5924 5902 continue;
5925 5903 }
5926 5904
5927 5905 /* Unlink and perform lunchg1 processing on pkt. */
5928 5906 scsi_lunchg1_list = pkt->pkt_stmp;
5929 5907
5930 5908 /* Determine initiator port (self) from the pkt_address. */
5931 5909 tran = pkt->pkt_address.a_hba_tran;
5932 5910 ASSERT(tran && tran->tran_tgtmap && tran->tran_iport_dip);
5933 5911 self = tran->tran_iport_dip;
5934 5912
5935 5913 /*
5936 5914 * Determine scsi_devie from pkt_address (depends on
5937 5915 * SCSI_HBA_ADDR_COMPLEX).
5938 5916 */
5939 5917 sd = scsi_address_device(&(pkt->pkt_address));
5940 5918 ASSERT(sd);
5941 5919 if (sd == NULL) {
5942 5920 (*pkt->pkt_comp)(pkt);
5943 5921 continue;
5944 5922 }
5945 5923
5946 5924 /* Determine unit-address from scsi_device. */
5947 5925 ua = scsi_device_unit_address(sd);
5948 5926
5949 5927 /* Extract taddr from the unit-address. */
5950 5928 for (p = taddr; (*ua != ',') && (*ua != '\0'); )
5951 5929 *p++ = *ua++;
5952 5930 *p = '\0'; /* NULL terminate taddr */
5953 5931
5954 5932 /*
5955 5933 * Form path using special "@taddr,*" notation to trigger
5956 5934 * lun enumeration.
5957 5935 */
5958 5936 (void) ddi_pathname(self, path);
5959 5937 (void) strcat(path, "/luns@");
5960 5938 (void) strcat(path, taddr);
5961 5939 (void) strcat(path, ",*");
5962 5940
5963 5941 /*
5964 5942 * Now that we have the path, complete the pkt that
5965 5943 * triggered lunchg1 processing.
5966 5944 */
5967 5945 (*pkt->pkt_comp)(pkt);
5968 5946
5969 5947 /* Allocate element for stage2 processing queue. */
5970 5948 lunchg2 = kmem_alloc(sizeof (*lunchg2), KM_SLEEP);
5971 5949 lunchg2->lunchg2_path = strdup(path);
5972 5950
5973 5951 /* Queue and dispatch to stage 2. */
5974 5952 SCSI_HBA_LOG((_LOG(2), self, NULL,
5975 5953 "lunchg stage1: queue %s", lunchg2->lunchg2_path));
5976 5954 mutex_enter(&scsi_lunchg2_mutex);
5977 5955 lunchg2->lunchg2_next = scsi_lunchg2_list;
5978 5956 scsi_lunchg2_list = lunchg2;
5979 5957 if (lunchg2->lunchg2_next == NULL)
5980 5958 (void) cv_signal(&scsi_lunchg2_cv);
5981 5959 mutex_exit(&scsi_lunchg2_mutex);
5982 5960 }
5983 5961 }
5984 5962
5985 5963 /*ARGSUSED*/
5986 5964 static void
5987 5965 scsi_lunchg2_daemon(void *arg)
5988 5966 {
5989 5967 callb_cpr_t cprinfo;
5990 5968 struct scsi_lunchg2 *lunchg2;
5991 5969 ldi_ident_t li;
5992 5970 ldi_handle_t lh;
5993 5971
5994 5972 CALLB_CPR_INIT(&cprinfo, &scsi_lunchg2_mutex,
5995 5973 callb_generic_cpr, "scsi_lunchg2_daemon");
5996 5974
5997 5975 li = ldi_ident_from_anon();
5998 5976 mutex_enter(&scsi_lunchg2_mutex);
5999 5977 for (;;) {
6000 5978 lunchg2 = scsi_lunchg2_list;
6001 5979 if (lunchg2 == NULL) {
6002 5980 /* All lunchg2 processing requests serviced, wait. */
6003 5981 CALLB_CPR_SAFE_BEGIN(&cprinfo);
6004 5982 (void) cv_wait(&scsi_lunchg2_cv,
6005 5983 &scsi_lunchg2_mutex);
6006 5984 CALLB_CPR_SAFE_END(&cprinfo, &scsi_lunchg2_mutex);
6007 5985 continue;
6008 5986 }
6009 5987
6010 5988 /* Unlink and perform lunchg2 processing on pkt. */
6011 5989 scsi_lunchg2_list = lunchg2->lunchg2_next;
6012 5990
6013 5991 /*
6014 5992 * Open and close the path to trigger lun enumeration. We
6015 5993 * don't expect the open to succeed, but we do expect code in
6016 5994 * scsi_hba_bus_configone() to trigger lun enumeration.
6017 5995 */
6018 5996 SCSI_HBA_LOG((_LOG(2), NULL, NULL,
6019 5997 "lunchg stage2: open %s", lunchg2->lunchg2_path));
6020 5998 if (ldi_open_by_name(lunchg2->lunchg2_path,
6021 5999 FREAD, kcred, &lh, li) == 0)
6022 6000 (void) ldi_close(lh, FREAD, kcred);
6023 6001
6024 6002 /* Free path and linked element. */
6025 6003 strfree(lunchg2->lunchg2_path);
6026 6004 kmem_free(lunchg2, sizeof (*lunchg2));
6027 6005 }
6028 6006 }
6029 6007
6030 6008 /*
6031 6009 * Enumerate a child at the specified @addr. If a device exists @addr then
6032 6010 * ensure that we have the appropriately named devinfo node for it. Name is
6033 6011 * NULL in the bus_config_all case. This routine has no knowledge of the
6034 6012 * format of an @addr string or associated addressing properties.
6035 6013 *
6036 6014 * The caller must guarantee that there is an open scsi_hba_devi_enter on the
6037 6015 * parent. We return the scsi_device structure for the child device. This
6038 6016 * scsi_device structure is valid until the caller scsi_hba_devi_exit the
6039 6017 * parent. The caller can add do ndi_hold_devi of the child prior to the
6040 6018 * scsi_hba_devi_exit to extend the validity of the child.
6041 6019 *
6042 6020 * In some cases the returned scsi_device structure may be used to drive
6043 6021 * additional SCMD_REPORT_LUNS operations by bus_config_all callers.
6044 6022 *
6045 6023 * The first operation performed is to see if there is a dynamic SID nodes
6046 6024 * already attached at the specified "name@addr". This is the fastpath
6047 6025 * case for resolving a reference to a node that has already been created.
6048 6026 * All other references are serialized for a given @addr prior to probing
6049 6027 * to determine the type of device, if any, at the specified @addr.
6050 6028 * If no device is present then NDI_FAILURE is returned. The fact that a
6051 6029 * device does not exist may be determined via the barrier/probe cache,
6052 6030 * minimizing the probes of non-existent devices.
6053 6031 *
6054 6032 * When there is a device present the dynamic SID node is created based on
6055 6033 * the device found. If a driver.conf node exists for the same @addr it
6056 6034 * will either merge into the dynamic SID node (if the SID node bound to
6057 6035 * that driver), or exist independently. To prevent the actions of one driver
6058 6036 * causing side effects in another, code prevents multiple SID nodes from
6059 6037 * binding to the same "@addr" at the same time. There is autodetach code
6060 6038 * to allow one device to be replaced with another at the same @addr for
6061 6039 * slot addressed SCSI bus implementations (SPI). For compatibility with
6062 6040 * legacy driver.conf behavior, the code does not prevent multiple driver.conf
6063 6041 * nodes from attaching to the same @addr at the same time.
6064 6042 *
6065 6043 * This routine may have the side effect of creating nodes for devices other
6066 6044 * than the one being sought. It is possible that there is a different type of
6067 6045 * target device at that target/lun address than we were asking for. In that
6068 6046 * It is the caller's responsibility to determine whether the device we found,
6069 6047 * if any, at the specified address, is the one it really wanted.
6070 6048 */
6071 6049 static struct scsi_device *
6072 6050 scsi_device_config(dev_info_t *self, char *name, char *addr, scsi_enum_t se,
6073 6051 int *circp, int *ppi)
6074 6052 {
6075 6053 dev_info_t *child = NULL;
6076 6054 dev_info_t *probe = NULL;
6077 6055 struct scsi_device *sdchild;
6078 6056 struct scsi_device *sdprobe;
6079 6057 dev_info_t *dsearch;
6080 6058 mdi_pathinfo_t *psearch;
6081 6059 major_t major;
6082 6060 int sp;
6083 6061 int pi = 0;
6084 6062 int wait_msg = scsi_hba_wait_msg;
6085 6063 int chg;
6086 6064
6087 6065 ASSERT(self && addr && DEVI_BUSY_OWNED(self));
6088 6066
6089 6067 SCSI_HBA_LOG((_LOG(4), self, NULL, "%s@%s wanted",
6090 6068 name ? name : "", addr));
6091 6069
6092 6070 /* playing with "probe" node name is dangerous */
6093 6071 if (name && (strcmp(name, "probe") == 0))
6094 6072 return (NULL);
6095 6073
6096 6074 /*
6097 6075 * NOTE: use 'goto done;' or 'goto fail;'. There should only be one
6098 6076 * 'return' statement from here to the end of the function - the one
6099 6077 * on the last line of the function.
6100 6078 */
6101 6079
6102 6080 /*
6103 6081 * Fastpath: search to see if we are requesting a named SID node that
6104 6082 * already exists (we already created) - probe node does not count.
6105 6083 * scsi_findchild() does not hold the returned devinfo node, but
6106 6084 * this is OK since the caller has a scsi_hba_devi_enter on the
6107 6085 * attached parent HBA (self). The caller is responsible for attaching
6108 6086 * and placing a hold on the child (directly via ndi_hold_devi or
6109 6087 * indirectly via ndi_busop_bus_config) before doing an
6110 6088 * scsi_hba_devi_exit on the parent.
6111 6089 *
6112 6090 * NOTE: This fastpath prevents detecting a driver binding change
6113 6091 * (autodetach) if the same nodename is used for old and new binding.
6114 6092 */
6115 6093 /* first call is with init set */
6116 6094 (void) scsi_findchild(self, name, addr, 1, &dsearch, NULL, &pi);
6117 6095 if (dsearch && scsi_hba_dev_is_sid(dsearch) &&
6118 6096 !scsi_hba_devi_is_barrier(dsearch)) {
6119 6097 SCSI_HBA_LOG((_LOG(4), NULL, dsearch,
6120 6098 "%s@%s devinfo fastpath", name ? name : "", addr));
6121 6099 child = dsearch;
6122 6100 goto done;
6123 6101 }
6124 6102
6125 6103 /*
6126 6104 * Create a barrier devinfo node used to "probe" the device with. We
6127 6105 * need to drive this node to DS_INITIALIZED so that the
6128 6106 * DDI_CTLOPS_INITCHILD has occurred, bringing the SCSA transport to
6129 6107 * a state useable state for issuing our "probe" commands. We establish
6130 6108 * this barrier node with a node name of "probe" and compatible
6131 6109 * property of "scsiprobe". The compatible property must be associated
6132 6110 * in /etc/driver_aliases with a scsi target driver available in the
6133 6111 * root file system (sd).
6134 6112 *
6135 6113 * The "probe" that we perform on the barrier node, after it is
6136 6114 * DS_INITIALIZED, is used to find the information needed to create a
6137 6115 * dynamic devinfo (SID) node. This "probe" is separate from the
6138 6116 * probe(9E) call associated with the transition of a node from
6139 6117 * DS_INITIALIZED to DS_PROBED. The probe(9E) call that eventually
6140 6118 * occurs against the created SID node should find ddi_dev_is_sid and
6141 6119 * just return DDI_PROBE_DONTCARE.
6142 6120 *
6143 6121 * Trying to avoid the use of a barrier node is not a good idea
6144 6122 * because we may have an HBA driver that uses generic bus_config
6145 6123 * (this code) but implements its own DDI_CTLOPS_INITCHILD with side
6146 6124 * effects that we can't duplicate (such as the ATA nexus driver).
6147 6125 *
6148 6126 * The probe/barrier node plays an integral part of the locking scheme.
6149 6127 * The objective is to single thread probes of the same device (same
6150 6128 * @addr) while allowing parallelism for probes of different devices
6151 6129 * with the same parent. At this point we are serialized on our self.
6152 6130 * For parallelism we will need to release our self. Prior to release
6153 6131 * we construct a barrier for probes of the same device to serialize
6154 6132 * against. The "probe@addr" node acts as this barrier. An entering
6155 6133 * thread must wait until the probe node does not exist - it can then
6156 6134 * create and link the probe node - dropping the HBA (self) lock after
6157 6135 * the node is linked and visible (after ddi_initchild). A side effect
6158 6136 * of this is that transports should not "go over the wire" (i.e. do
6159 6137 * things that incur significant delays) until after tran_target_init.
6160 6138 * This means that the first "over the wire" operation should occur
6161 6139 * at tran_target_probe time - when things are running in parallel
6162 6140 * again.
6163 6141 *
6164 6142 * If the probe node exists then another probe with the same @addr is
6165 6143 * in progress, we must wait until there is no probe in progress
6166 6144 * before proceeding, and when we proceed we must continue to hold the
6167 6145 * HBA (self) until we have linked a new probe node as a barrier.
6168 6146 *
6169 6147 * When a device is found to *not* exist, its probe/barrier node may be
6170 6148 * marked with DEVICE_REMOVED with node deletion scheduled for some
6171 6149 * future time (seconds). This asynchronous deletion allows the
6172 6150 * framework to detect repeated requests to the same non-existent
6173 6151 * device and avoid overhead associated with contacting a non-existent
6174 6152 * device again and again.
6175 6153 */
6176 6154 for (;;) {
6177 6155 /*
6178 6156 * Search for probe node - they should only exist as devinfo
6179 6157 * nodes.
6180 6158 */
6181 6159 (void) scsi_findchild(self, "probe", addr,
6182 6160 0, &probe, &psearch, NULL);
6183 6161 if (probe == NULL) {
6184 6162 if (psearch)
6185 6163 SCSI_HBA_LOG((_LOG(2), self,
6186 6164 mdi_pi_get_client(psearch),
6187 6165 "???? @%s 'probe' search found "
6188 6166 "pathinfo: %p", addr, (void *)psearch));
6189 6167 break;
6190 6168 }
6191 6169
6192 6170 /*
6193 6171 * The barrier node may cache the non-existence of a device
6194 6172 * by leaving the barrier node in place (with
6195 6173 * DEVI_DEVICE_REMOVED flag set ) for some amount of time after
6196 6174 * the failure of a probe. This flag is used to fail
6197 6175 * additional probes until the barrier probe node is deleted,
6198 6176 * which will occur from a timeout some time after a failed
6199 6177 * probe. The failed probe will use DEVI_SET_DEVICE_REMOVED
6200 6178 * and schedule probe node deletion from a timeout. The callers
6201 6179 * scsi_hba_devi_exit on the way out of the first failure will
6202 6180 * do the cv_broadcast associated with the cv_wait below - this
6203 6181 * handles threads that wait prior to DEVI_DEVICE_REMOVED being
6204 6182 * set.
6205 6183 */
6206 6184 if (DEVI_IS_DEVICE_REMOVED(probe)) {
6207 6185 SCSI_HBA_LOG((_LOG(3), NULL, probe,
6208 6186 "detected probe DEVICE_REMOVED"));
6209 6187 probe = NULL; /* deletion already scheduled */
6210 6188 goto fail;
6211 6189 }
6212 6190
6213 6191 /*
6214 6192 * Drop the lock on the HBA (self) and wait until the probe in
6215 6193 * progress has completed. A changes in the sibling list from
6216 6194 * removing the probe node will cause cv_wait to return
6217 6195 * (scsi_hba_devi_exit does the cv_broadcast).
6218 6196 */
6219 6197 if (wait_msg) {
6220 6198 wait_msg--;
6221 6199 SCSI_HBA_LOG((_LOG(2), NULL, probe,
6222 6200 "exists, probe already in progress: %s", wait_msg ?
6223 6201 "waiting..." : "last msg, but still waiting..."));
6224 6202 }
6225 6203
6226 6204 /*
6227 6205 * NOTE: we could avoid rare case of one second delay by
6228 6206 * implementing scsi_hba_devi_exit_and_wait based on
6229 6207 * ndi/mdi_devi_exit_and_wait (and consider switching devcfg.c
6230 6208 * code to use these ndi/mdi interfaces too).
6231 6209 */
6232 6210 scsi_hba_devi_exit(self, *circp);
6233 6211 mutex_enter(&DEVI(self)->devi_lock);
6234 6212 (void) cv_timedwait(&DEVI(self)->devi_cv,
6235 6213 &DEVI(self)->devi_lock,
6236 6214 ddi_get_lbolt() + drv_usectohz(MICROSEC));
6237 6215 mutex_exit(&DEVI(self)->devi_lock);
6238 6216 scsi_hba_devi_enter(self, circp);
6239 6217 }
6240 6218 ASSERT(probe == NULL);
6241 6219
6242 6220 /*
6243 6221 * Search to see if we are requesting a SID node that already exists.
6244 6222 * We hold the HBA (self) and there is not another probe in progress at
6245 6223 * the same @addr. scsi_findchild() does not hold the returned
6246 6224 * devinfo node but this is OK since we hold the HBA (self).
6247 6225 */
6248 6226 if (name) {
6249 6227 (void) scsi_findchild(self, name, addr, 1, &dsearch, NULL, &pi);
6250 6228 if (dsearch && scsi_hba_dev_is_sid(dsearch)) {
6251 6229 SCSI_HBA_LOG((_LOG(4), NULL, dsearch,
6252 6230 "%s@%s probe devinfo fastpath",
6253 6231 name ? name : "", addr));
6254 6232 child = dsearch;
6255 6233 goto done;
6256 6234 }
6257 6235 }
6258 6236
6259 6237 /*
6260 6238 * We are looking for a SID node that does not exist or a driver.conf
6261 6239 * node.
6262 6240 *
6263 6241 * To avoid probe side effects, before we probe the device at the
6264 6242 * specified address we need to check to see if there is already an
6265 6243 * initialized child "@addr".
6266 6244 *
6267 6245 * o If we find an initialized SID child and name is NULL or matches
6268 6246 * the name or the name of the attached driver then we return the
6269 6247 * existing node.
6270 6248 *
6271 6249 * o If we find a non-matching SID node, we will attempt to autodetach
6272 6250 * and remove the node in preference to our new node.
6273 6251 *
6274 6252 * o If SID node found does not match and can't be autodetached, we
6275 6253 * fail: we only allow one SID node at an address.
6276 6254 *
6277 6255 * NOTE: This code depends on SID nodes showing up prior to
6278 6256 * driver.conf nodes in the sibling list.
6279 6257 */
6280 6258 for (;;) {
6281 6259 /* first NULL name call is with init set */
6282 6260 (void) scsi_findchild(self, NULL, addr, 1, &dsearch, NULL, &pi);
6283 6261 if (dsearch == NULL)
6284 6262 break;
6285 6263 ASSERT(!scsi_hba_devi_is_barrier(dsearch));
6286 6264
6287 6265 /*
6288 6266 * To detect changes in driver binding that should attempt
6289 6267 * autodetach we determine the major number of the driver
6290 6268 * that should currently be associated with the device based
6291 6269 * on the compatible property.
6292 6270 */
6293 6271 major = DDI_MAJOR_T_NONE;
6294 6272 if (scsi_hba_dev_is_sid(dsearch))
6295 6273 major = ddi_compatible_driver_major(dsearch, NULL);
6296 6274 if ((major == DDI_MAJOR_T_NONE) && (name == NULL))
6297 6275 major = ddi_driver_major(dsearch);
6298 6276
6299 6277 if ((scsi_hba_dev_is_sid(dsearch) ||
6300 6278 (i_ddi_node_state(dsearch) >= DS_INITIALIZED)) &&
6301 6279 ((name == NULL) ||
6302 6280 (strcmp(ddi_node_name(dsearch), name) == 0) ||
6303 6281 (strcmp(ddi_driver_name(dsearch), name) == 0)) &&
6304 6282 (major == ddi_driver_major(dsearch))) {
6305 6283 SCSI_HBA_LOG((_LOG(3), NULL, dsearch,
6306 6284 "already attached @addr"));
6307 6285 child = dsearch;
6308 6286 goto done;
6309 6287 }
6310 6288
6311 6289 if (!scsi_hba_dev_is_sid(dsearch))
6312 6290 break; /* driver.conf node */
6313 6291
6314 6292 /*
6315 6293 * Implement autodetach of SID node for situations like a
6316 6294 * previously "scsinodev" LUN0 coming into existence (or a
6317 6295 * disk/tape on an SPI transport at same addr but never both
6318 6296 * powered on at the same time). Try to autodetach the existing
6319 6297 * SID node @addr. If that works, search again - otherwise fail.
6320 6298 */
6321 6299 SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
6322 6300 "looking for %s@%s: SID @addr exists, autodetach",
6323 6301 name ? name : "", addr));
6324 6302 if (!scsi_hba_remove_node(dsearch)) {
6325 6303 SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
6326 6304 "autodetach @%s failed: fail %s@%s",
6327 6305 addr, name ? name : "", addr));
6328 6306 goto fail;
6329 6307 }
6330 6308 SCSI_HBA_LOG((_LOG(2), self, NULL, "autodetach @%s OK", addr));
6331 6309 }
6332 6310
6333 6311 /*
6334 6312 * We will be creating a new SID node, allocate probe node
6335 6313 * used to find out information about the device located @addr.
6336 6314 * The probe node also acts as a barrier against additional
6337 6315 * configuration at the same address, and in the case of non-existent
6338 6316 * devices it will (for some amount of time) avoid re-learning that
6339 6317 * the device does not exist on every reference. Once the probe
6340 6318 * node is DS_LINKED we can drop the HBA (self).
6341 6319 *
6342 6320 * The probe node is allocated as a hidden node so that it does not
6343 6321 * show up in devinfo snapshots.
6344 6322 */
6345 6323 ndi_devi_alloc_sleep(self, "probe",
6346 6324 (se == SE_HP) ? DEVI_SID_HP_HIDDEN_NODEID : DEVI_SID_HIDDEN_NODEID,
6347 6325 &probe);
6348 6326 ASSERT(probe);
6349 6327 ndi_flavor_set(probe, SCSA_FLAVOR_SCSI_DEVICE);
6350 6328
6351 6329 /*
6352 6330 * Decorate the probe node with the property representation of @addr
6353 6331 * unit-address string prior to initchild so that initchild can
6354 6332 * construct the name of the node from properties and tran_tgt_init
6355 6333 * implementation can determine what LUN is being referenced.
6356 6334 *
6357 6335 * If the addr specified has incorrect syntax (busconfig one of bogus
6358 6336 * /devices path) then scsi_hba_ua_set can fail. If the address
6359 6337 * is not understood by the SCSA HBA driver then this operation will
6360 6338 * work, but tran_tgt_init may still fail (for example the HBA
6361 6339 * driver may not support secondary functions).
6362 6340 */
6363 6341 if (scsi_hba_ua_set(addr, probe, NULL) == 0) {
6364 6342 SCSI_HBA_LOG((_LOG(2), NULL, probe,
6365 6343 "@%s failed scsi_hba_ua_set", addr));
6366 6344 goto fail;
6367 6345 }
6368 6346
6369 6347 /*
6370 6348 * Set the class property to "scsi". This is sufficient to distinguish
6371 6349 * the node for HBAs that have multiple classes of children (like uata
6372 6350 * - which has "dada" class for ATA children and "scsi" class for
6373 6351 * ATAPI children) and may not use our scsi_busctl_initchild()
6374 6352 * implementation. We also add a "compatible" property of "scsiprobe"
6375 6353 * to select the probe driver.
6376 6354 */
6377 6355 if ((ndi_prop_update_string(DDI_DEV_T_NONE, probe,
6378 6356 "class", "scsi") != DDI_PROP_SUCCESS) ||
6379 6357 (ndi_prop_update_string_array(DDI_DEV_T_NONE, probe,
6380 6358 "compatible", &compatible_probe, 1) != DDI_PROP_SUCCESS)) {
6381 6359 SCSI_HBA_LOG((_LOG(1), NULL, probe,
6382 6360 "@%s failed node decoration", addr));
6383 6361 goto fail;
6384 6362 }
6385 6363
6386 6364 /*
6387 6365 * Promote probe node to DS_INITIALIZED so that transport can be used
6388 6366 * for scsi_probe. After this the node is linked and visible as a
6389 6367 * barrier for serialization of other @addr operations.
6390 6368 *
6391 6369 * NOTE: If we attached the probe node, we could get rid of
6392 6370 * uninit_prevent.
6393 6371 */
6394 6372 if (ddi_initchild(self, probe) != DDI_SUCCESS) {
6395 6373 SCSI_HBA_LOG((_LOG(2), NULL, probe,
6396 6374 "@%s failed initchild", addr));
6397 6375
6398 6376 /* probe node will be removed in fail exit path */
6399 6377 goto fail;
6400 6378 }
6401 6379
6402 6380 /* get the scsi_device structure of the probe node */
6403 6381 sdprobe = ddi_get_driver_private(probe);
6404 6382 ASSERT(sdprobe);
6405 6383
6406 6384 /*
6407 6385 * Do scsi_probe. The probe node is linked and visible as a barrier.
6408 6386 * We prevent uninitialization of the probe node and drop our HBA (self)
6409 6387 * while we run scsi_probe() of this "@addr". This allows the framework
6410 6388 * to support multiple scsi_probes for different devices attached to
6411 6389 * the same HBA (self) in parallel. We prevent node demotion of the
6412 6390 * probe node from DS_INITIALIZED by setting sd_uninit_prevent. The
6413 6391 * probe node can not be successfully demoted below DS_INITIALIZED
6414 6392 * (scsi_busctl_uninitchild will fail) until we zero sd_uninit_prevent
6415 6393 * as we are freeing the node via scsi_hba_remove_node(probe).
6416 6394 */
6417 6395 sdprobe->sd_uninit_prevent++;
6418 6396 scsi_hba_devi_exit(self, *circp);
6419 6397 sp = scsi_probe(sdprobe, SLEEP_FUNC);
6420 6398
6421 6399 /* Introduce a small delay here to increase parallelism. */
6422 6400 delay_random(5);
6423 6401
6424 6402 if (sp == SCSIPROBE_EXISTS) {
6425 6403 /*
6426 6404 * For a device that exists, while still running in parallel,
6427 6405 * also get identity information from device. This is done
6428 6406 * separate from scsi_probe/tran_tgt_probe/scsi_hba_probe
6429 6407 * since the probe code path may still be used for HBAs
6430 6408 * that don't use common bus_config services (we don't want
6431 6409 * to expose that code path to a behavior change). This
6432 6410 * operation is called 'identity' to avoid confusion with
6433 6411 * deprecated identify(9E).
6434 6412 *
6435 6413 * Future: We may eventually want to allow HBA customization via
6436 6414 * scsi_identity/tran_tgt_identity/scsi_device_identity, but for
6437 6415 * now we just scsi_device_identity.
6438 6416 *
6439 6417 * The identity operation will establish additional properties
6440 6418 * on the probe node related to device identity:
6441 6419 *
6442 6420 * "inquiry-page-80" byte array of SCSI page 80
6443 6421 * "inquiry-page-83" byte array of SCSI page 83
6444 6422 *
6445 6423 * These properties will be used to generate a devid
6446 6424 * (ddi_devid_scsi_encode) and guid - and to register
6447 6425 * (ddi_devid_register) a devid for the device.
6448 6426 *
6449 6427 * If identify fails (non-zero return), the we had allocation
6450 6428 * problems or the device returned inconsistent results then
6451 6429 * we pretend that device does not exist.
6452 6430 */
6453 6431 if (scsi_device_identity(sdprobe, SLEEP_FUNC)) {
6454 6432 scsi_enumeration_failed(probe, -1, NULL, "identify");
6455 6433 sp = SCSIPROBE_FAILURE;
6456 6434 }
6457 6435
6458 6436 /*
6459 6437 * Future: Is there anything more we can do here to help avoid
6460 6438 * serialization on iport parent during scsi_device attach(9E)?
6461 6439 */
6462 6440 }
6463 6441 scsi_hba_devi_enter(self, circp);
6464 6442 sdprobe->sd_uninit_prevent--;
6465 6443
6466 6444 if (sp != SCSIPROBE_EXISTS) {
6467 6445 scsi_enumeration_failed(probe, -1, NULL, "probe");
6468 6446
6469 6447 if ((se != SE_HP) && scsi_hba_barrier_timeout) {
6470 6448 /*
6471 6449 * Target does not exist. Mark the barrier probe node
6472 6450 * as DEVICE_REMOVED and schedule an asynchronous
6473 6451 * deletion of the node in scsi_hba_barrier_timeout
6474 6452 * seconds. We keep our hold on the probe node
6475 6453 * until we are ready perform the asynchronous node
6476 6454 * deletion.
6477 6455 */
6478 6456 SCSI_HBA_LOG((_LOG(3), NULL, probe,
6479 6457 "set probe DEVICE_REMOVED"));
6480 6458 mutex_enter(&DEVI(probe)->devi_lock);
6481 6459 DEVI_SET_DEVICE_REMOVED(probe);
6482 6460 mutex_exit(&DEVI(probe)->devi_lock);
6483 6461
6484 6462 scsi_hba_barrier_add(probe, scsi_hba_barrier_timeout);
6485 6463 probe = NULL;
6486 6464 }
6487 6465 goto fail;
6488 6466 }
6489 6467
6490 6468 /* Create the child node from the inquiry data in the probe node. */
6491 6469 if ((child = scsi_device_configchild(self, addr, se, sdprobe,
6492 6470 circp, &pi)) == NULL) {
6493 6471 /*
6494 6472 * This may fail because there was no driver binding identified
6495 6473 * via driver_alias. We may still have a conf node.
6496 6474 */
6497 6475 if (name) {
6498 6476 (void) scsi_findchild(self, name, addr,
6499 6477 0, &child, NULL, &pi);
6500 6478 if (child)
6501 6479 SCSI_HBA_LOG((_LOG(2), NULL, child,
6502 6480 "using driver.conf driver binding"));
6503 6481 }
6504 6482 if (child == NULL) {
6505 6483 SCSI_HBA_LOG((_LOG(2), NULL, probe,
6506 6484 "device not configured"));
6507 6485 goto fail;
6508 6486 }
6509 6487 }
6510 6488
6511 6489 /*
6512 6490 * Transfer the inquiry data from the probe node to the child
6513 6491 * SID node to avoid an extra scsi_probe. Callers depend on
6514 6492 * established inquiry data for the returned scsi_device.
6515 6493 */
6516 6494 sdchild = ddi_get_driver_private(child);
6517 6495 if (sdchild && (sdchild->sd_inq == NULL)) {
6518 6496 sdchild->sd_inq = sdprobe->sd_inq;
6519 6497 sdprobe->sd_inq = NULL;
6520 6498 }
6521 6499
6522 6500 /*
6523 6501 * If we are doing a bus_configone and the node we created has the
6524 6502 * wrong node and driver name then switch the return result to a
6525 6503 * driver.conf node with the correct name - if such a node exists.
6526 6504 */
6527 6505 if (name && (strcmp(ddi_node_name(child), name) != 0) &&
6528 6506 (strcmp(ddi_driver_name(child), name) != 0)) {
6529 6507 (void) scsi_findchild(self, name, addr,
6530 6508 0, &dsearch, NULL, &pi);
6531 6509 if (dsearch == NULL) {
6532 6510 SCSI_HBA_LOG((_LOG(2), NULL, child,
6533 6511 "wrong device configured %s@%s", name, addr));
6534 6512 /*
6535 6513 * We can't remove when modrootloaded == 0 in case
6536 6514 * boot-device a uses generic name and
6537 6515 * scsi_hba_nodename_compatible_get() returned a
6538 6516 * legacy binding-set driver oriented name.
6539 6517 */
6540 6518 if (modrootloaded) {
6541 6519 (void) scsi_hba_remove_node(child);
6542 6520 child = NULL;
6543 6521 goto fail;
6544 6522 }
6545 6523 } else {
6546 6524 SCSI_HBA_LOG((_LOG(2), NULL, dsearch,
6547 6525 "device configured, but switching to driver.conf"));
6548 6526 child = dsearch;
6549 6527 }
6550 6528 }
6551 6529
6552 6530 /* get the scsi_device structure from the node */
6553 6531 SCSI_HBA_LOG((_LOG(3), NULL, child, "device configured"));
6554 6532
6555 6533 if (child) {
6556 6534 done: ASSERT(child);
6557 6535 sdchild = ddi_get_driver_private(child);
6558 6536 ASSERT(sdchild);
6559 6537
6560 6538 /*
6561 6539 * We may have ended up here after promotion of a previously
6562 6540 * demoted node, where demotion deleted sd_inq data in
6563 6541 * scsi_busctl_uninitchild. We redo the scsi_probe() to
6564 6542 * reestablish sd_inq. We also want to redo the scsi_probe
6565 6543 * for devices are currently device_isremove in order to
6566 6544 * detect new device_insert.
6567 6545 */
6568 6546 if ((sdchild->sd_inq == NULL) ||
6569 6547 ((pi == NULL) && ndi_devi_device_isremoved(child))) {
6570 6548
6571 6549 /* hotplug_node can only be revived via hotplug. */
6572 6550 if ((se == SE_HP) || !ndi_dev_is_hotplug_node(child)) {
6573 6551 SCSI_HBA_LOG((_LOG(3), NULL, child,
6574 6552 "scsi_probe() demoted devinfo"));
6575 6553
6576 6554 sp = scsi_probe(sdchild, SLEEP_FUNC);
6577 6555
6578 6556 if (sp == SCSIPROBE_EXISTS) {
6579 6557 ASSERT(sdchild->sd_inq);
6580 6558
6581 6559 /*
6582 6560 * Devinfo child exists and we are
6583 6561 * talking to the device, report
6584 6562 * reinsert and note if this was a
6585 6563 * new reinsert.
6586 6564 */
6587 6565 chg = ndi_devi_device_insert(child);
6588 6566 SCSI_HBA_LOG((_LOGCFG, NULL, child,
6589 6567 "devinfo %s@%s device_reinsert%s",
6590 6568 name ? name : "", addr,
6591 6569 chg ? "" : "ed already"));
6592 6570 } else {
6593 6571 scsi_enumeration_failed(child, se,
6594 6572 NULL, "reprobe");
6595 6573
6596 6574 chg = ndi_devi_device_remove(child);
6597 6575 SCSI_HBA_LOG((_LOG(2), NULL, child,
6598 6576 "%s device_remove%s",
6599 6577 (sp > (sizeof (scsi_probe_ascii) /
6600 6578 sizeof (scsi_probe_ascii[0]))) ?
6601 6579 "UNKNOWN" : scsi_probe_ascii[sp],
6602 6580 chg ? "" : "ed already"));
6603 6581
6604 6582 child = NULL;
6605 6583 sdchild = NULL;
6606 6584 }
6607 6585 } else {
6608 6586 SCSI_HBA_LOG((_LOG(2), NULL, child,
6609 6587 "no reprobe"));
6610 6588
6611 6589 child = NULL;
6612 6590 sdchild = NULL;
6613 6591 }
6614 6592 }
6615 6593 } else {
6616 6594 fail: ASSERT(child == NULL);
6617 6595 sdchild = NULL;
6618 6596 }
6619 6597 if (probe) {
6620 6598 /*
6621 6599 * Clean up probe node, destroying node if uninit_prevent
6622 6600 * it is going to zero. Destroying the probe node (deleting
6623 6601 * from the sibling list) will wake up any people waiting on
6624 6602 * the probe node barrier.
6625 6603 */
6626 6604 SCSI_HBA_LOG((_LOG(4), NULL, probe, "remove probe"));
6627 6605 if (!scsi_hba_remove_node(probe)) {
6628 6606 /*
6629 6607 * Probe node removal should not fail, but if it
6630 6608 * does we hand that responsibility over to the
6631 6609 * async barrier deletion thread - other references
6632 6610 * to the same unit-address can hang until the
6633 6611 * probe node delete completes.
6634 6612 */
6635 6613 SCSI_HBA_LOG((_LOG(4), NULL, probe,
6636 6614 "remove probe failed, go async"));
6637 6615 scsi_hba_barrier_add(probe, 1);
6638 6616 }
6639 6617 probe = NULL;
6640 6618 }
6641 6619
6642 6620 /*
6643 6621 * If we successfully resolved via a pathinfo node, we need to find
6644 6622 * the pathinfo node and ensure that it is online (if possible). This
6645 6623 * is done for the case where the device was open when
6646 6624 * scsi_device_unconfig occurred, so mdi_pi_free did not occur. If the
6647 6625 * device has now been reinserted, we want the path back online.
6648 6626 * NOTE: This needs to occur after destruction of the probe node to
6649 6627 * avoid ASSERT related to two nodes at the same unit-address.
6650 6628 */
6651 6629 if (sdchild && pi && (probe == NULL)) {
6652 6630 ASSERT(MDI_PHCI(self));
6653 6631
6654 6632 (void) scsi_findchild(self, NULL, addr,
6655 6633 0, &dsearch, &psearch, NULL);
6656 6634 ASSERT((psearch == NULL) ||
6657 6635 (mdi_pi_get_client(psearch) == child));
6658 6636
6659 6637 if (psearch && mdi_pi_device_isremoved(psearch)) {
6660 6638 /*
6661 6639 * Verify that we can talk to the device, and if
6662 6640 * so note if this is a new device_insert.
6663 6641 *
6664 6642 * NOTE: We depend on mdi_path_select(), when given
6665 6643 * a specific path_instance, to select that path
6666 6644 * even if the path is offline.
6667 6645 *
6668 6646 * NOTE: A Client node is not ndi_dev_is_hotplug_node().
6669 6647 */
6670 6648 if (se == SE_HP) {
6671 6649 SCSI_HBA_LOG((_LOG(3), NULL, child,
6672 6650 "%s scsi_probe() demoted pathinfo",
6673 6651 mdi_pi_spathname(psearch)));
6674 6652
6675 6653 sp = scsi_hba_probe_pi(sdchild, SLEEP_FUNC, pi);
6676 6654
6677 6655 if (sp == SCSIPROBE_EXISTS) {
6678 6656 /*
6679 6657 * Pathinfo child exists and we are
6680 6658 * talking to the device, report
6681 6659 * reinsert and note if this
6682 6660 * was a new reinsert.
6683 6661 */
6684 6662 chg = mdi_pi_device_insert(psearch);
6685 6663 SCSI_HBA_LOG((_LOGCFG, self, NULL,
6686 6664 "pathinfo %s device_reinsert%s",
6687 6665 mdi_pi_spathname(psearch),
6688 6666 chg ? "" : "ed already"));
6689 6667
6690 6668 if (chg)
6691 6669 (void) mdi_pi_online(psearch,
6692 6670 0);
6693 6671
6694 6672 /*
6695 6673 * Report client reinsert and note if
6696 6674 * this was a new reinsert.
6697 6675 */
6698 6676 chg = ndi_devi_device_insert(child);
6699 6677 SCSI_HBA_LOG((_LOGCFG, NULL, child,
6700 6678 "client devinfo %s@%s "
6701 6679 "device_reinsert%s",
6702 6680 name ? name : "", addr,
6703 6681 chg ? "" : "ed already"));
6704 6682 } else {
6705 6683 scsi_enumeration_failed(child, se,
6706 6684 mdi_pi_spathname(psearch),
6707 6685 "reprobe");
6708 6686 child = NULL;
6709 6687 sdchild = NULL;
6710 6688 }
6711 6689
6712 6690 } else {
6713 6691 SCSI_HBA_LOG((_LOG(2), NULL, child,
6714 6692 "%s no reprobe",
6715 6693 mdi_pi_spathname(psearch)));
6716 6694
6717 6695 child = NULL;
6718 6696 sdchild = NULL;
6719 6697 }
6720 6698 }
6721 6699 }
6722 6700
6723 6701 /* If asked for path_instance, return it. */
6724 6702 if (ppi)
6725 6703 *ppi = pi;
6726 6704
6727 6705 return (sdchild);
6728 6706 }
6729 6707
6730 6708 static void
6731 6709 scsi_device_unconfig(dev_info_t *self, char *name, char *addr, int *circp)
6732 6710 {
6733 6711 dev_info_t *child = NULL;
6734 6712 mdi_pathinfo_t *path = NULL;
6735 6713 char *spathname;
6736 6714 int rval;
6737 6715
6738 6716 ASSERT(self && addr && DEVI_BUSY_OWNED(self));
6739 6717
6740 6718 /*
6741 6719 * We have a catch-22. We may have a demoted node that we need to find
6742 6720 * and offline/remove. To find the node if it isn't demoted, we
6743 6721 * use scsi_findchild. If it's demoted, we then use
6744 6722 * ndi_devi_findchild_by_callback.
6745 6723 */
6746 6724 (void) scsi_findchild(self, name, addr, 0, &child, &path, NULL);
6747 6725
6748 6726 if ((child == NULL) && (path == NULL)) {
6749 6727 child = ndi_devi_findchild_by_callback(self, name, addr,
6750 6728 scsi_busctl_ua);
6751 6729 if (child) {
6752 6730 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6753 6731 "devinfo %s@%s found by callback",
6754 6732 name ? name : "", addr));
6755 6733 ASSERT(ndi_flavor_get(child) ==
6756 6734 SCSA_FLAVOR_SCSI_DEVICE);
6757 6735 if (ndi_flavor_get(child) != SCSA_FLAVOR_SCSI_DEVICE) {
6758 6736 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6759 6737 "devinfo %s@%s not SCSI_DEVICE flavored",
6760 6738 name ? name : "", addr));
6761 6739 child = NULL;
6762 6740 }
6763 6741 }
6764 6742 }
6765 6743
6766 6744 if (child) {
6767 6745 ASSERT(child && (path == NULL));
6768 6746
6769 6747 /* Don't unconfig probe nodes. */
6770 6748 if (scsi_hba_devi_is_barrier(child)) {
6771 6749 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6772 6750 "devinfo %s@%s is_barrier, skip",
6773 6751 name ? name : "", addr));
6774 6752 return;
6775 6753 }
6776 6754
6777 6755 /* Attempt to offline/remove the devinfo node */
6778 6756 if (ndi_devi_offline(child,
6779 6757 NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
6780 6758 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6781 6759 "devinfo %s@%s offlined and removed",
6782 6760 name ? name : "", addr));
6783 6761 } else if (ndi_devi_device_remove(child)) {
6784 6762 /* Offline/remove failed, note new device_remove */
6785 6763 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6786 6764 "devinfo %s@%s offline failed, device_remove",
6787 6765 name ? name : "", addr));
6788 6766 }
6789 6767 } else if (path) {
6790 6768 ASSERT(path && (child == NULL));
6791 6769
6792 6770 /*
6793 6771 * Attempt to offline/remove the pathinfo node.
6794 6772 *
6795 6773 * NOTE: mdi_pi_offline of last path will fail if the
6796 6774 * device is open (i.e. the client can't be offlined).
6797 6775 *
6798 6776 * NOTE: For mdi there is no REMOVE flag for mdi_pi_offline().
6799 6777 * When mdi_pi_offline returns MDI_SUCCESS, we are responsible
6800 6778 * for remove via mdi_pi_free().
6801 6779 */
6802 6780 mdi_hold_path(path);
6803 6781 spathname = mdi_pi_spathname(path); /* valid after free */
6804 6782 scsi_hba_devi_exit_phci(self, *circp);
6805 6783 rval = mdi_pi_offline(path, 0);
6806 6784 scsi_hba_devi_enter_phci(self, circp);
6807 6785
6808 6786 /* Note new device_remove */
6809 6787 if (mdi_pi_device_remove(path))
6810 6788 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6811 6789 "pathinfo %s note device_remove", spathname));
6812 6790
6813 6791 mdi_rele_path(path);
6814 6792 if (rval == MDI_SUCCESS) {
6815 6793 (void) mdi_pi_free(path, 0);
6816 6794 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6817 6795 "pathinfo %s offlined, then freed", spathname));
6818 6796 }
6819 6797 } else {
6820 6798 ASSERT((path == NULL) && (child == NULL));
6821 6799
6822 6800 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
6823 6801 "%s@%s not found", name ? name : "", addr));
6824 6802 }
6825 6803 }
6826 6804
6827 6805 /*
6828 6806 * configure the device at the specified "@addr" address.
6829 6807 */
6830 6808 static struct scsi_device *
6831 6809 scsi_hba_bus_configone_addr(dev_info_t *self, char *addr, scsi_enum_t se)
6832 6810 {
6833 6811 int circ;
6834 6812 struct scsi_device *sd;
6835 6813
6836 6814 scsi_hba_devi_enter(self, &circ);
6837 6815 sd = scsi_device_config(self, NULL, addr, se, &circ, NULL);
6838 6816 scsi_hba_devi_exit(self, circ);
6839 6817 return (sd);
6840 6818 }
6841 6819
6842 6820 /*
6843 6821 * unconfigure the device at the specified "@addr" address.
6844 6822 */
6845 6823 static void
6846 6824 scsi_hba_bus_unconfigone_addr(dev_info_t *self, char *addr)
6847 6825 {
6848 6826 int circ;
6849 6827
6850 6828 scsi_hba_devi_enter(self, &circ);
6851 6829 (void) scsi_device_unconfig(self, NULL, addr, &circ);
6852 6830 scsi_hba_devi_exit(self, circ);
6853 6831 }
6854 6832
6855 6833 /*
6856 6834 * The bus_config_all operations are multi-threaded for performance. A
6857 6835 * separate thread per target and per LUN is used. The config handle is used
6858 6836 * to coordinate all the threads at a given level and the config thread data
6859 6837 * contains the required information for a specific thread to identify what it
6860 6838 * is processing and the handle under which this is being processed.
6861 6839 */
6862 6840
6863 6841 /* multi-threaded config handle */
6864 6842 struct scsi_hba_mte_h {
6865 6843 dev_info_t *h_self; /* initiator port */
6866 6844 int h_thr_count;
6867 6845 kmutex_t h_lock;
6868 6846 kcondvar_t h_cv;
6869 6847 };
6870 6848
6871 6849 /* target of 'self' config thread data */
6872 6850 struct scsi_hba_mte_td {
6873 6851 struct scsi_hba_mte_h *td_h;
6874 6852 char *td_taddr; /* target port */
6875 6853 int td_mt;
6876 6854 scsi_enum_t td_se;
6877 6855 };
6878 6856
6879 6857 /* Invoke callback on a vector of taddrs from multiple threads */
6880 6858 static void
6881 6859 scsi_hba_thread_taddrs(dev_info_t *self, char **taddrs, int mt,
6882 6860 scsi_enum_t se, void (*callback)(void *arg))
6883 6861 {
6884 6862 struct scsi_hba_mte_h *h; /* HBA header */
6885 6863 struct scsi_hba_mte_td *td; /* target data */
6886 6864 char **taddr;
6887 6865
6888 6866 /* allocate and initialize the handle */
6889 6867 h = kmem_zalloc(sizeof (*h), KM_SLEEP);
6890 6868 mutex_init(&h->h_lock, NULL, MUTEX_DEFAULT, NULL);
6891 6869 cv_init(&h->h_cv, NULL, CV_DEFAULT, NULL);
6892 6870 h->h_self = self;
6893 6871
6894 6872 /* loop over all the targets */
6895 6873 for (taddr = taddrs; *taddr; taddr++) {
6896 6874 /* allocate a thread data structure for target */
6897 6875 td = kmem_alloc(sizeof (*td), KM_SLEEP);
6898 6876 td->td_h = h;
6899 6877 td->td_taddr = *taddr;
6900 6878 td->td_mt = mt;
6901 6879 td->td_se = se;
6902 6880
6903 6881 /* process the target */
6904 6882 mutex_enter(&h->h_lock);
6905 6883 h->h_thr_count++;
6906 6884 mutex_exit(&h->h_lock);
6907 6885
6908 6886 if (mt & SCSI_ENUMERATION_MT_TARGET_DISABLE)
6909 6887 callback((void *)td);
6910 6888 else
6911 6889 (void) thread_create(NULL, 0, callback, (void *)td,
6912 6890 0, &p0, TS_RUN, minclsyspri);
6913 6891 }
6914 6892
6915 6893 /* wait for all the target threads to complete */
6916 6894 mutex_enter(&h->h_lock);
6917 6895 while (h->h_thr_count > 0)
6918 6896 cv_wait(&h->h_cv, &h->h_lock);
6919 6897 mutex_exit(&h->h_lock);
6920 6898
6921 6899 /* free the handle */
6922 6900 cv_destroy(&h->h_cv);
6923 6901 mutex_destroy(&h->h_lock);
6924 6902 kmem_free(h, sizeof (*h));
6925 6903 }
6926 6904
6927 6905
6928 6906 /* lun/secondary function of lun0 config thread data */
6929 6907 struct scsi_hba_mte_ld {
6930 6908 struct scsi_hba_mte_h *ld_h;
6931 6909 char *ld_taddr; /* target port */
6932 6910 scsi_lun64_t ld_lun64; /* lun */
6933 6911 int ld_sfunc; /* secondary function */
6934 6912 scsi_enum_t ld_se;
6935 6913 };
6936 6914
6937 6915 /*
6938 6916 * Enumerate the LUNs and secondary functions of the specified target. The
6939 6917 * target portion of the "@addr" is already represented as a string in the
6940 6918 * thread data, we add a ",lun" representation to this and perform a
6941 6919 * bus_configone byte of enumeration on that "@addr".
6942 6920 */
6943 6921 static void
6944 6922 scsi_hba_enum_lsf_of_tgt_thr(void *arg)
6945 6923 {
6946 6924 struct scsi_hba_mte_ld *ld = (struct scsi_hba_mte_ld *)arg;
6947 6925 struct scsi_hba_mte_h *h = ld->ld_h;
6948 6926 dev_info_t *self = h->h_self;
6949 6927 char addr[SCSI_MAXNAMELEN];
6950 6928
6951 6929 /* make string form of "@taddr,lun[,sfunc]" and see if it exists */
6952 6930 if (ld->ld_sfunc == -1)
6953 6931 (void) snprintf(addr, sizeof (addr),
6954 6932 "%s,%" PRIx64, ld->ld_taddr, ld->ld_lun64);
6955 6933 else
6956 6934 (void) snprintf(addr, sizeof (addr),
6957 6935 "%s,%" PRIx64 ",%x",
6958 6936 ld->ld_taddr, ld->ld_lun64, ld->ld_sfunc);
6959 6937
6960 6938 /* configure device at that unit-address address */
6961 6939 (void) scsi_hba_bus_configone_addr(self, addr, ld->ld_se);
6962 6940
6963 6941 /* signal completion of this LUN thread to the target */
6964 6942 mutex_enter(&h->h_lock);
6965 6943 if (--h->h_thr_count == 0)
6966 6944 cv_broadcast(&h->h_cv);
6967 6945 mutex_exit(&h->h_lock);
6968 6946
6969 6947 /* free config thread data */
6970 6948 kmem_free(ld, sizeof (*ld));
6971 6949 }
6972 6950
6973 6951 /* Format of SCSI REPORT_LUNS report */
6974 6952 typedef struct scsi_lunrpt {
6975 6953 uchar_t lunrpt_len_msb; /* # LUNs being reported */
6976 6954 uchar_t lunrpt_len_mmsb;
6977 6955 uchar_t lunrpt_len_mlsb;
6978 6956 uchar_t lunrpt_len_lsb;
6979 6957 uchar_t lunrpt_reserved[4];
6980 6958 scsi_lun_t lunrpt_luns[1]; /* LUNs, variable size */
6981 6959 } scsi_lunrpt_t;
6982 6960
6983 6961 /*
6984 6962 * scsi_device_reportluns()
6985 6963 *
6986 6964 * Callers of this routine should ensure that the 'sd0' scsi_device structure
6987 6965 * and 'pi' path_instance specified are associated with a responding LUN0.
6988 6966 * This should not be called for SCSI-1 devices.
6989 6967 *
6990 6968 * To get a LUN report, we must allocate a buffer. To know how big to make the
6991 6969 * buffer, we must know the number of LUNs. To know the number of LUNs, we must
6992 6970 * get a LUN report. We first issue a SCMD_REPORT_LUNS command using a
6993 6971 * reasonably sized buffer that's big enough to report all LUNs for most
6994 6972 * typical devices. If it turns out that we needed a bigger buffer, we attempt
6995 6973 * to allocate a buffer of sufficient size, and reissue the command. If the
6996 6974 * first command succeeds, but the second fails, we return whatever we were
6997 6975 * able to get the first time. We return enough information for the caller to
6998 6976 * tell whether he got all the LUNs or only a subset.
6999 6977 *
7000 6978 * If successful, we allocate an array of scsi_lun_t to hold the results. The
7001 6979 * caller must kmem_free(*lunarrayp, *sizep) when finished with it. Upon
7002 6980 * successful return return value is NDI_SUCCESS and:
7003 6981 *
7004 6982 * *lunarrayp points to the allocated array,
7005 6983 * *nlunsp is the number of valid LUN entries in the array,
7006 6984 * *tlunsp is the total number of LUNs in the target,
7007 6985 * *sizep is the size of the lunarrayp array, which must be freed.
7008 6986 *
7009 6987 * If the *nlunsp is less than *tlunsp, then we were only able to retrieve a
7010 6988 * subset of the total set of LUNs in the target.
7011 6989 */
7012 6990 static int
7013 6991 scsi_device_reportluns(struct scsi_device *sd0, char *taddr, int pi,
7014 6992 scsi_lun_t **lunarrayp, uint32_t *nlunsp, uint32_t *tlunsp, size_t *sizep)
7015 6993 {
7016 6994 struct buf *lunrpt_bp;
7017 6995 struct scsi_pkt *lunrpt_pkt;
7018 6996 scsi_lunrpt_t *lunrpt;
7019 6997 uint32_t bsize;
7020 6998 uint32_t tluns, nluns;
7021 6999 int default_maxluns = scsi_lunrpt_default_max;
7022 7000 dev_info_t *child;
7023 7001
7024 7002 ASSERT(sd0 && lunarrayp && nlunsp && tlunsp && sizep);
7025 7003
7026 7004 /*
7027 7005 * NOTE: child should only be used in SCSI_HBA_LOG context since with
7028 7006 * vHCI enumeration it may be the vHCI 'client' devinfo child instead
7029 7007 * of a child of the 'self' pHCI we are enumerating.
7030 7008 */
7031 7009 child = sd0->sd_dev;
7032 7010
7033 7011 /* first try, look for up to scsi_lunrpt_default_max LUNs */
7034 7012 nluns = default_maxluns;
7035 7013
7036 7014 again: bsize = sizeof (struct scsi_lunrpt) +
7037 7015 ((nluns - 1) * sizeof (struct scsi_lun));
7038 7016
7039 7017 lunrpt_bp = scsi_alloc_consistent_buf(&sd0->sd_address,
7040 7018 (struct buf *)NULL, bsize, B_READ, SLEEP_FUNC, NULL);
7041 7019 if (lunrpt_bp == NULL) {
7042 7020 SCSI_HBA_LOG((_LOG(1), NULL, child, "failed alloc"));
7043 7021 return (NDI_NOMEM);
7044 7022 }
7045 7023
7046 7024 lunrpt_pkt = scsi_init_pkt(&sd0->sd_address,
7047 7025 (struct scsi_pkt *)NULL, lunrpt_bp, CDB_GROUP5,
7048 7026 sizeof (struct scsi_arq_status), 0, PKT_CONSISTENT,
7049 7027 SLEEP_FUNC, NULL);
7050 7028 if (lunrpt_pkt == NULL) {
7051 7029 SCSI_HBA_LOG((_LOG(1), NULL, child, "failed init"));
7052 7030 scsi_free_consistent_buf(lunrpt_bp);
7053 7031 return (NDI_NOMEM);
7054 7032 }
7055 7033
7056 7034 (void) scsi_setup_cdb((union scsi_cdb *)lunrpt_pkt->pkt_cdbp,
7057 7035 SCMD_REPORT_LUNS, 0, bsize, 0);
7058 7036
7059 7037 lunrpt_pkt->pkt_time = scsi_lunrpt_timeout;
7060 7038
7061 7039 /*
7062 7040 * When sd0 is a vHCI scsi device, we need reportlun to be issued
7063 7041 * against a specific LUN0 path_instance that we are enumerating.
7064 7042 */
7065 7043 lunrpt_pkt->pkt_path_instance = pi;
7066 7044 lunrpt_pkt->pkt_flags |= FLAG_PKT_PATH_INSTANCE;
7067 7045
7068 7046 /*
7069 7047 * NOTE: scsi_poll may not allow HBA specific recovery from TRAN_BUSY.
7070 7048 */
7071 7049 if (scsi_poll(lunrpt_pkt) < 0) {
7072 7050 SCSI_HBA_LOG((_LOG(2), NULL, child, "reportlun not supported"));
7073 7051 scsi_destroy_pkt(lunrpt_pkt);
7074 7052 scsi_free_consistent_buf(lunrpt_bp);
7075 7053 return (NDI_FAILURE);
7076 7054 }
7077 7055
7078 7056 scsi_destroy_pkt(lunrpt_pkt);
7079 7057
7080 7058 lunrpt = (scsi_lunrpt_t *)lunrpt_bp->b_un.b_addr;
7081 7059
7082 7060 /* Compute the total number of LUNs in the target */
7083 7061 tluns = (((uint_t)lunrpt->lunrpt_len_msb << 24) |
7084 7062 ((uint_t)lunrpt->lunrpt_len_mmsb << 16) |
7085 7063 ((uint_t)lunrpt->lunrpt_len_mlsb << 8) |
7086 7064 ((uint_t)lunrpt->lunrpt_len_lsb)) >> 3;
7087 7065
7088 7066 if (tluns == 0) {
7089 7067 /* Illegal response -- this target is broken */
7090 7068 SCSI_HBA_LOG((_LOG(1), NULL, child, "illegal tluns of zero"));
7091 7069 scsi_free_consistent_buf(lunrpt_bp);
7092 7070 return (DDI_NOT_WELL_FORMED);
7093 7071 }
7094 7072
7095 7073 if (tluns > nluns) {
7096 7074 /* have more than we allocated space for */
7097 7075 if (nluns == default_maxluns) {
7098 7076 /* first time around, reallocate larger */
7099 7077 scsi_free_consistent_buf(lunrpt_bp);
7100 7078 nluns = tluns;
7101 7079 goto again;
7102 7080 }
7103 7081
7104 7082 /* uh oh, we got a different tluns the second time! */
7105 7083 SCSI_HBA_LOG((_LOG(1), NULL, child,
7106 7084 "tluns changed from %d to %d", nluns, tluns));
7107 7085 } else
7108 7086 nluns = tluns;
7109 7087
7110 7088 /*
7111 7089 * Now we have:
7112 7090 * lunrpt_bp is the buffer we're using;
7113 7091 * tluns is the total number of LUNs the target says it has;
7114 7092 * nluns is the number of LUNs we were able to get into the buffer.
7115 7093 *
7116 7094 * Copy the data out of scarce iopb memory into regular kmem.
7117 7095 * The caller must kmem_free(*lunarrayp, *sizep) when finished with it.
7118 7096 */
7119 7097 *lunarrayp = (scsi_lun_t *)kmem_alloc(
7120 7098 nluns * sizeof (scsi_lun_t), KM_SLEEP);
7121 7099 if (*lunarrayp == NULL) {
7122 7100 SCSI_HBA_LOG((_LOG(1), NULL, child, "NULL lunarray"));
7123 7101 scsi_free_consistent_buf(lunrpt_bp);
7124 7102 return (NDI_NOMEM);
7125 7103 }
7126 7104
7127 7105 *sizep = nluns * sizeof (scsi_lun_t);
7128 7106 *nlunsp = nluns;
7129 7107 *tlunsp = tluns;
7130 7108 bcopy((void *)&lunrpt->lunrpt_luns, (void *)*lunarrayp, *sizep);
7131 7109 scsi_free_consistent_buf(lunrpt_bp);
7132 7110 SCSI_HBA_LOG((_LOG(3), NULL, child,
7133 7111 "@%s,0 path %d: %d/%d luns", taddr, pi, nluns, tluns));
7134 7112 return (NDI_SUCCESS);
7135 7113 }
7136 7114
7137 7115 /*
7138 7116 * Enumerate all the LUNs and secondary functions of the specified 'taddr'
7139 7117 * target port as accessed via 'self' pHCI. Note that sd0 may be associated
7140 7118 * with a child of the vHCI instead of 'self' - in this case the 'pi'
7141 7119 * path_instance is used to ensure that the SCMD_REPORT_LUNS command is issued
7142 7120 * through the 'self' pHCI path.
7143 7121 *
7144 7122 * We multi-thread across all the LUNs and secondary functions and enumerate
7145 7123 * them. Which LUNs exist is based on SCMD_REPORT_LUNS data.
7146 7124 *
7147 7125 * The scsi_device we are called with should be for LUN0 and has been probed.
7148 7126 *
7149 7127 * This function is structured so that an HBA that has a different target
7150 7128 * addressing structure can still use this function to enumerate the its
7151 7129 * LUNs if it uses "taddr,lun" for its LUN space.
7152 7130 *
7153 7131 * We make assumptions about other LUNs associated with the target:
7154 7132 *
7155 7133 * For SCSI-2 and SCSI-3 target we will issue the SCSI report_luns
7156 7134 * command. If this fails or we have a SCSI-1 then the number of
7157 7135 * LUNs is determined based on SCSI_OPTIONS_NLUNS. For a SCSI-1
7158 7136 * target we never probe above LUN 8, even if SCSI_OPTIONS_NLUNS
7159 7137 * indicates we should.
7160 7138 *
7161 7139 * HBA drivers wanting a different set of assumptions should implement their
7162 7140 * own LUN enumeration code.
7163 7141 */
7164 7142 static int
7165 7143 scsi_hba_enum_lsf_of_t(struct scsi_device *sd0,
7166 7144 dev_info_t *self, char *taddr, int pi, int mt, scsi_enum_t se)
7167 7145 {
7168 7146 dev_info_t *child;
7169 7147 scsi_hba_tran_t *tran;
7170 7148 impl_scsi_tgtmap_t *tgtmap;
7171 7149 damap_id_t tgtid;
7172 7150 damap_t *tgtdam;
7173 7151 damap_t *lundam = NULL;
7174 7152 struct scsi_hba_mte_h *h;
7175 7153 struct scsi_hba_mte_ld *ld;
7176 7154 int aver;
7177 7155 scsi_lun_t *lunp = NULL;
7178 7156 int lun;
7179 7157 uint32_t nluns;
7180 7158 uint32_t tluns;
7181 7159 size_t size;
7182 7160 scsi_lun64_t lun64;
7183 7161 int maxluns;
7184 7162
7185 7163 /*
7186 7164 * If LUN0 failed then we have no other LUNs.
7187 7165 *
7188 7166 * NOTE: We need sd_inq to be valid to check ansi version. Since
7189 7167 * scsi_unprobe is now a noop (sd_inq freeded in
7190 7168 * scsi_busctl_uninitchild) sd_inq remains valid even if a target
7191 7169 * driver detach(9E) occurs, resulting in a scsi_unprobe call
7192 7170 * (sd_uninit_prevent keeps sd_inq valid by failing any
7193 7171 * device_uninitchild attempts).
7194 7172 */
7195 7173 ASSERT(sd0 && sd0->sd_uninit_prevent && sd0->sd_dev && sd0->sd_inq);
7196 7174 if ((sd0 == NULL) || (sd0->sd_dev == NULL) || (sd0->sd_inq == NULL)) {
7197 7175 SCSI_HBA_LOG((_LOG(1), NULL, sd0 ? sd0->sd_dev : NULL,
7198 7176 "not setup correctly:%s%s%s",
7199 7177 (sd0 == NULL) ? " device" : "",
7200 7178 (sd0 && (sd0->sd_dev == NULL)) ? " dip" : "",
7201 7179 (sd0 && (sd0->sd_inq == NULL)) ? " inq" : ""));
7202 7180 return (DDI_FAILURE);
7203 7181 }
7204 7182
7205 7183 /*
7206 7184 * NOTE: child should only be used in SCSI_HBA_LOG context since with
7207 7185 * vHCI enumeration it may be the vHCI 'client' devinfo child instead
7208 7186 * of a child of the 'self' pHCI we are enumerating.
7209 7187 */
7210 7188 child = sd0->sd_dev;
7211 7189
7212 7190 /* Determine if we are reporting lun observations into lunmap. */
7213 7191 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
7214 7192 tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
7215 7193 if (tgtmap) {
7216 7194 tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
7217 7195 tgtid = damap_lookup(tgtdam, taddr);
7218 7196 if (tgtid != NODAM) {
7219 7197 lundam = damap_id_priv_get(tgtdam, tgtid);
7220 7198 damap_id_rele(tgtdam, tgtid);
7221 7199 ASSERT(lundam);
7222 7200 }
7223 7201 }
7224 7202
7225 7203 if (lundam) {
7226 7204 /* If using lunmap, start the observation */
7227 7205 scsi_lunmap_set_begin(self, lundam);
7228 7206 } else {
7229 7207 /* allocate and initialize the LUN handle */
7230 7208 h = kmem_zalloc(sizeof (*h), KM_SLEEP);
7231 7209 mutex_init(&h->h_lock, NULL, MUTEX_DEFAULT, NULL);
7232 7210 cv_init(&h->h_cv, NULL, CV_DEFAULT, NULL);
7233 7211 h->h_self = self;
7234 7212 }
7235 7213
7236 7214 /* See if SCMD_REPORT_LUNS works for SCSI-2 and beyond */
7237 7215 aver = sd0->sd_inq->inq_ansi;
7238 7216 if ((aver >= SCSI_VERSION_2) && (scsi_device_reportluns(sd0,
7239 7217 taddr, pi, &lunp, &nluns, &tluns, &size) == NDI_SUCCESS)) {
7240 7218
7241 7219 ASSERT(lunp && (size > 0) && (nluns > 0) && (tluns > 0));
7242 7220
7243 7221 /* loop over the reported LUNs */
7244 7222 SCSI_HBA_LOG((_LOG(2), NULL, child,
7245 7223 "@%s,0 path %d: enumerating %d reported lun%s", taddr, pi,
7246 7224 nluns, nluns > 1 ? "s" : ""));
7247 7225
7248 7226 for (lun = 0; lun < nluns; lun++) {
7249 7227 lun64 = scsi_lun_to_lun64(lunp[lun]);
7250 7228
7251 7229 if (lundam) {
7252 7230 if (scsi_lunmap_set_add(self, lundam,
7253 7231 taddr, lun64, -1) != DDI_SUCCESS) {
7254 7232 SCSI_HBA_LOG((_LOG_NF(WARN),
7255 7233 "@%s,%" PRIx64 " failed to create",
7256 7234 taddr, lun64));
7257 7235 }
7258 7236 } else {
7259 7237 if (lun64 == 0)
7260 7238 continue;
7261 7239
7262 7240 /* allocate a thread data structure for LUN */
7263 7241 ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
7264 7242 ld->ld_h = h;
7265 7243 ld->ld_taddr = taddr;
7266 7244 ld->ld_lun64 = lun64;
7267 7245 ld->ld_sfunc = -1;
7268 7246 ld->ld_se = se;
7269 7247
7270 7248 /* process the LUN */
7271 7249 mutex_enter(&h->h_lock);
7272 7250 h->h_thr_count++;
7273 7251 mutex_exit(&h->h_lock);
7274 7252
7275 7253 if (mt & SCSI_ENUMERATION_MT_LUN_DISABLE)
7276 7254 scsi_hba_enum_lsf_of_tgt_thr(
7277 7255 (void *)ld);
7278 7256 else
7279 7257 (void) thread_create(NULL, 0,
7280 7258 scsi_hba_enum_lsf_of_tgt_thr,
7281 7259 (void *)ld, 0, &p0, TS_RUN,
7282 7260 minclsyspri);
7283 7261 }
7284 7262 }
7285 7263
7286 7264 /* free the LUN array allocated by scsi_device_reportluns */
7287 7265 kmem_free(lunp, size);
7288 7266 } else {
7289 7267 /* Determine the number of LUNs to enumerate. */
7290 7268 maxluns = scsi_get_scsi_maxluns(sd0);
7291 7269
7292 7270 /* Couldn't get SCMD_REPORT_LUNS data */
7293 7271 if (aver >= SCSI_VERSION_3) {
7294 7272 scsi_enumeration_failed(child, se, taddr, "report_lun");
7295 7273
7296 7274 /*
7297 7275 * Based on calling context tunable, only enumerate one
7298 7276 * lun (lun0) if scsi_device_reportluns() fails on a
7299 7277 * SCSI_VERSION_3 or greater device.
7300 7278 */
7301 7279 if (scsi_lunrpt_failed_do1lun & (1 << se))
7302 7280 maxluns = 1;
7303 7281 }
7304 7282
7305 7283 /* loop over possible LUNs, skipping LUN0 */
7306 7284 if (maxluns > 1)
7307 7285 SCSI_HBA_LOG((_LOG(2), NULL, child,
7308 7286 "@%s,0 path %d: enumerating luns 1-%d", taddr, pi,
7309 7287 maxluns - 1));
7310 7288 else
7311 7289 SCSI_HBA_LOG((_LOG(2), NULL, child,
7312 7290 "@%s,0 path %d: enumerating just lun0", taddr, pi));
7313 7291
7314 7292 for (lun64 = 0; lun64 < maxluns; lun64++) {
7315 7293 if (lundam) {
7316 7294 if (scsi_lunmap_set_add(self, lundam,
7317 7295 taddr, lun64, -1) != DDI_SUCCESS) {
7318 7296 SCSI_HBA_LOG((_LOG_NF(WARN),
7319 7297 "@%s,%" PRIx64 " failed to create",
7320 7298 taddr, lun64));
7321 7299 }
7322 7300 } else {
7323 7301 if (lun64 == 0)
7324 7302 continue;
7325 7303
7326 7304 /* allocate a thread data structure for LUN */
7327 7305 ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
7328 7306 ld->ld_h = h;
7329 7307 ld->ld_taddr = taddr;
7330 7308 ld->ld_lun64 = lun64;
7331 7309 ld->ld_sfunc = -1;
7332 7310 ld->ld_se = se;
7333 7311
7334 7312 /* process the LUN */
7335 7313 mutex_enter(&h->h_lock);
7336 7314 h->h_thr_count++;
7337 7315 mutex_exit(&h->h_lock);
7338 7316 if (mt & SCSI_ENUMERATION_MT_LUN_DISABLE)
7339 7317 scsi_hba_enum_lsf_of_tgt_thr(
7340 7318 (void *)ld);
7341 7319 else
7342 7320 (void) thread_create(NULL, 0,
7343 7321 scsi_hba_enum_lsf_of_tgt_thr,
7344 7322 (void *)ld, 0, &p0, TS_RUN,
7345 7323 minclsyspri);
7346 7324 }
7347 7325 }
7348 7326 }
7349 7327
7350 7328 /*
7351 7329 * If we have an embedded service as a secondary function on LUN0 and
7352 7330 * the primary LUN0 function is different than the secondary function
7353 7331 * then enumerate the secondary function. The sfunc value is the dtype
7354 7332 * associated with the embedded service.
7355 7333 *
7356 7334 * inq_encserv: enclosure service and our dtype is not DTYPE_ESI
7357 7335 * or DTYPE_UNKNOWN then create a separate DTYPE_ESI node for
7358 7336 * enclosure service access.
7359 7337 */
7360 7338 ASSERT(sd0->sd_inq);
7361 7339 if (sd0->sd_inq->inq_encserv &&
7362 7340 ((sd0->sd_inq->inq_dtype & DTYPE_MASK) != DTYPE_UNKNOWN) &&
7363 7341 ((sd0->sd_inq->inq_dtype & DTYPE_MASK) != DTYPE_ESI) &&
7364 7342 ((sd0->sd_inq->inq_ansi >= SCSI_VERSION_3))) {
7365 7343 if (lundam) {
7366 7344 if (scsi_lunmap_set_add(self, lundam,
7367 7345 taddr, 0, DTYPE_ESI) != DDI_SUCCESS) {
7368 7346 SCSI_HBA_LOG((_LOG_NF(WARN),
7369 7347 "@%s,0,%x failed to create",
7370 7348 taddr, DTYPE_ESI));
7371 7349 }
7372 7350 } else {
7373 7351 /* allocate a thread data structure for sfunc */
7374 7352 ld = kmem_alloc(sizeof (*ld), KM_SLEEP);
7375 7353 ld->ld_h = h;
7376 7354 ld->ld_taddr = taddr;
7377 7355 ld->ld_lun64 = 0;
7378 7356 ld->ld_sfunc = DTYPE_ESI;
7379 7357 ld->ld_se = se;
7380 7358
7381 7359 /* process the LUN */
7382 7360 mutex_enter(&h->h_lock);
7383 7361 h->h_thr_count++;
7384 7362 mutex_exit(&h->h_lock);
7385 7363 if (mt & SCSI_ENUMERATION_MT_LUN_DISABLE)
7386 7364 scsi_hba_enum_lsf_of_tgt_thr((void *)ld);
7387 7365 else
7388 7366 (void) thread_create(NULL, 0,
7389 7367 scsi_hba_enum_lsf_of_tgt_thr, (void *)ld,
7390 7368 0, &p0, TS_RUN, minclsyspri);
7391 7369 }
7392 7370 }
7393 7371
7394 7372 /*
7395 7373 * Future: Add secondary function support for:
7396 7374 * inq_mchngr (DTYPE_CHANGER)
7397 7375 * inq_sccs (DTYPE_ARRAY_CTRL)
7398 7376 */
7399 7377
7400 7378 if (lundam) {
7401 7379 /* If using lunmap, end the observation */
7402 7380 scsi_lunmap_set_end(self, lundam);
7403 7381 } else {
7404 7382 /* wait for all the LUN threads of this target to complete */
7405 7383 mutex_enter(&h->h_lock);
7406 7384 while (h->h_thr_count > 0)
7407 7385 cv_wait(&h->h_cv, &h->h_lock);
7408 7386 mutex_exit(&h->h_lock);
7409 7387
7410 7388 /* free the target handle */
7411 7389 cv_destroy(&h->h_cv);
7412 7390 mutex_destroy(&h->h_lock);
7413 7391 kmem_free(h, sizeof (*h));
7414 7392 }
7415 7393
7416 7394 return (DDI_SUCCESS);
7417 7395 }
7418 7396
7419 7397 /*
7420 7398 * Enumerate LUN0 and all other LUNs and secondary functions associated with
7421 7399 * the specified target address.
7422 7400 *
7423 7401 * Return NDI_SUCCESS if we might have created a new node.
7424 7402 * Return NDI_FAILURE if we definitely did not create a new node.
7425 7403 */
7426 7404 static int
7427 7405 scsi_hba_bus_config_taddr(dev_info_t *self, char *taddr, int mt, scsi_enum_t se)
7428 7406 {
7429 7407 char addr[SCSI_MAXNAMELEN];
7430 7408 struct scsi_device *sd;
7431 7409 int circ;
7432 7410 int ret;
7433 7411 int pi;
7434 7412
7435 7413 /* See if LUN0 of the specified target exists. */
7436 7414 (void) snprintf(addr, sizeof (addr), "%s,0", taddr);
7437 7415
7438 7416 scsi_hba_devi_enter(self, &circ);
7439 7417 sd = scsi_device_config(self, NULL, addr, se, &circ, &pi);
7440 7418
7441 7419 if (sd) {
7442 7420 /*
7443 7421 * LUN0 exists, enumerate all the other LUNs.
7444 7422 *
7445 7423 * With vHCI enumeration, when 'self' is a pHCI the sd
7446 7424 * scsi_device may be associated with the vHCI 'client'.
7447 7425 * In this case 'pi' is the path_instance needed to
7448 7426 * continue enumeration communication LUN0 via 'self'
7449 7427 * pHCI and specific 'taddr' target address.
7450 7428 *
7451 7429 * We prevent the removal of LUN0 until we are done with
7452 7430 * prevent/allow because we must exit the parent for
7453 7431 * multi-threaded scsi_hba_enum_lsf_of_t().
7454 7432 *
7455 7433 * NOTE: scsi_unprobe is a noop, sd->sd_inq is valid until
7456 7434 * device_uninitchild - so sd_uninit_prevent keeps sd_inq valid
7457 7435 * by failing any device_uninitchild attempts.
7458 7436 */
7459 7437 ret = NDI_SUCCESS;
7460 7438 sd->sd_uninit_prevent++;
7461 7439 scsi_hba_devi_exit(self, circ);
7462 7440
7463 7441 (void) scsi_hba_enum_lsf_of_t(sd, self, taddr, pi, mt, se);
7464 7442
7465 7443 scsi_hba_devi_enter(self, &circ);
7466 7444 sd->sd_uninit_prevent--;
7467 7445 } else
7468 7446 ret = NDI_FAILURE;
7469 7447 scsi_hba_devi_exit(self, circ);
7470 7448 return (ret);
7471 7449 }
7472 7450
7473 7451 /* Config callout from scsi_hba_thread_taddrs */
7474 7452 static void
7475 7453 scsi_hba_taddr_config_thr(void *arg)
7476 7454 {
7477 7455 struct scsi_hba_mte_td *td = (struct scsi_hba_mte_td *)arg;
7478 7456 struct scsi_hba_mte_h *h = td->td_h;
7479 7457
7480 7458 (void) scsi_hba_bus_config_taddr(h->h_self, td->td_taddr,
7481 7459 td->td_mt, td->td_se);
7482 7460
7483 7461 /* signal completion of this target thread to the HBA */
7484 7462 mutex_enter(&h->h_lock);
7485 7463 if (--h->h_thr_count == 0)
7486 7464 cv_broadcast(&h->h_cv);
7487 7465 mutex_exit(&h->h_lock);
7488 7466
7489 7467 /* free config thread data */
7490 7468 kmem_free(td, sizeof (*td));
7491 7469 }
7492 7470
7493 7471 /*
7494 7472 * Enumerate all the children of the specified SCSI parallel interface (spi).
7495 7473 * An HBA associated with a non-parallel scsi bus should be using another bus
7496 7474 * level enumeration implementation (possibly their own) and calling
7497 7475 * scsi_hba_bus_config_taddr to do enumeration of devices associated with a
7498 7476 * particular target address.
7499 7477 *
7500 7478 * On an spi bus the targets are sequentially enumerated based on the
7501 7479 * width of the bus. We also take care to try to skip the HBAs own initiator
7502 7480 * id. See scsi_hba_enum_lsf_of_t() for LUN and secondary function enumeration.
7503 7481 *
7504 7482 * Return NDI_SUCCESS if we might have created a new node.
7505 7483 * Return NDI_FAILURE if we definitely did not create a new node.
7506 7484 *
7507 7485 * Note: At some point we may want to expose this interface in transport.h
7508 7486 * if we find an hba that implements bus_config but still uses spi-like target
7509 7487 * addresses.
7510 7488 */
7511 7489 static int
7512 7490 scsi_hba_bus_configall_spi(dev_info_t *self, int mt)
7513 7491 {
7514 7492 int options;
7515 7493 int ntargets;
7516 7494 int id;
7517 7495 int tgt;
7518 7496 char **taddrs;
7519 7497 char **taddr;
7520 7498 char *tbuf;
7521 7499
7522 7500 /*
7523 7501 * Find the number of targets supported on the bus. Look at the per
7524 7502 * bus scsi-options property on the HBA node and check its
7525 7503 * SCSI_OPTIONS_WIDE setting.
7526 7504 */
7527 7505 options = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7528 7506 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-options", -1);
7529 7507 if ((options != -1) && ((options & SCSI_OPTIONS_WIDE) == 0))
7530 7508 ntargets = NTARGETS; /* 8 */
7531 7509 else
7532 7510 ntargets = NTARGETS_WIDE; /* 16 */
7533 7511
7534 7512 /*
7535 7513 * Find the initiator-id for the HBA so we can skip that. We get the
7536 7514 * cached value on the HBA node, established in scsi_hba_attach_setup.
7537 7515 * If we were unable to determine the id then we rely on the HBA to
7538 7516 * fail gracefully when asked to enumerate itself.
7539 7517 */
7540 7518 id = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7541 7519 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-initiator-id", -1);
7542 7520 if (id > ntargets) {
7543 7521 SCSI_HBA_LOG((_LOG(1), self, NULL,
7544 7522 "'scsi-initiator-id' bogus for %d target bus: %d",
7545 7523 ntargets, id));
7546 7524 id = -1;
7547 7525 }
7548 7526 SCSI_HBA_LOG((_LOG(2), self, NULL,
7549 7527 "enumerating targets 0-%d skip %d", ntargets, id));
7550 7528
7551 7529 /* form vector of target addresses */
7552 7530 taddrs = kmem_zalloc(sizeof (char *) * (ntargets + 1), KM_SLEEP);
7553 7531 for (tgt = 0, taddr = taddrs; tgt < ntargets; tgt++) {
7554 7532 /* skip initiator */
7555 7533 if (tgt == id)
7556 7534 continue;
7557 7535
7558 7536 /* convert to string and enumerate the target address */
7559 7537 tbuf = kmem_alloc(((tgt/16) + 1) + 1, KM_SLEEP);
7560 7538 (void) sprintf(tbuf, "%x", tgt);
7561 7539 ASSERT(strlen(tbuf) == ((tgt/16) + 1));
7562 7540 *taddr++ = tbuf;
7563 7541 }
7564 7542
7565 7543 /* null terminate vector of target addresses */
7566 7544 *taddr = NULL;
7567 7545
7568 7546 /* configure vector of target addresses */
7569 7547 scsi_hba_thread_taddrs(self, taddrs, mt, SE_BUSCONFIG,
7570 7548 scsi_hba_taddr_config_thr);
7571 7549
7572 7550 /* free vector of target addresses */
7573 7551 for (taddr = taddrs; *taddr; taddr++)
7574 7552 kmem_free(*taddr, strlen(*taddr) + 1);
7575 7553 kmem_free(taddrs, sizeof (char *) * (ntargets + 1));
7576 7554 return (NDI_SUCCESS);
7577 7555 }
7578 7556
7579 7557 /*
7580 7558 * Transport independent bus_configone BUS_CONFIG_ONE implementation. Takes
7581 7559 * same arguments, minus op, as scsi_hba_bus_config(), tran_bus_config(),
7582 7560 * and scsi_hba_bus_config_spi().
7583 7561 */
7584 7562 int
7585 7563 scsi_hba_bus_configone(dev_info_t *self, uint_t flags, char *arg,
7586 7564 dev_info_t **childp)
7587 7565 {
7588 7566 int ret;
7589 7567 int circ;
7590 7568 char *name, *addr;
7591 7569 char *lcp;
7592 7570 char sc1, sc2;
7593 7571 char nameaddr[SCSI_MAXNAMELEN];
7594 7572 extern int i_ndi_make_spec_children(dev_info_t *, uint_t);
7595 7573 struct scsi_device *sd0, *sd;
7596 7574 scsi_lun64_t lun64;
7597 7575 int mt;
7598 7576
7599 7577 /* parse_name modifies arg1, we must duplicate "name@addr" */
7600 7578 (void) strcpy(nameaddr, arg);
7601 7579 i_ddi_parse_name(nameaddr, &name, &addr, NULL);
7602 7580
7603 7581 /* verify the form of the node - we need an @addr */
7604 7582 if ((name == NULL) || (addr == NULL) ||
7605 7583 (*name == '\0') || (*addr == '\0')) {
7606 7584 /*
7607 7585 * OBP may create ill formed template/stub/wild-card
7608 7586 * nodes (no @addr) for legacy driver loading methods -
7609 7587 * ignore them.
7610 7588 */
7611 7589 SCSI_HBA_LOG((_LOG(2), self, NULL, "%s ill formed", arg));
7612 7590 return (NDI_FAILURE);
7613 7591 }
7614 7592
7615 7593 /*
7616 7594 * Check to see if this is a non-scsi flavor configuration operation.
7617 7595 */
7618 7596 if (strcmp(name, "smp") == 0) {
7619 7597 /*
7620 7598 * Configure the child, and if we're successful return with
7621 7599 * active hold.
7622 7600 */
7623 7601 return (smp_hba_bus_config(self, addr, childp));
7624 7602 }
7625 7603
7626 7604 /*
7627 7605 * The framework does not ensure the creation of driver.conf
7628 7606 * nodes prior to calling a nexus bus_config. For legacy
7629 7607 * support of driver.conf file nodes we want to create our
7630 7608 * driver.conf file children now so that we can detect if we
7631 7609 * are being asked to bus_configone one of these nodes.
7632 7610 *
7633 7611 * Needing driver.conf file nodes prior to bus config is unique
7634 7612 * to scsi_enumeration mixed mode (legacy driver.conf and
7635 7613 * dynamic SID node) support. There is no general need for the
7636 7614 * framework to make driver.conf children prior to bus_config.
7637 7615 *
7638 7616 * We enter our HBA (self) prior to scsi_device_config, and
7639 7617 * pass it our circ. The scsi_device_config may exit the
7640 7618 * HBA around scsi_probe() operations to allow for parallelism.
7641 7619 * This is done after the probe node "@addr" is available as a
7642 7620 * barrier to prevent parallel probes of the same device. The
7643 7621 * probe node is also configured in a way that it can't be
7644 7622 * removed by the framework until we are done with it.
7645 7623 *
7646 7624 * NOTE: The framework is currently preventing many parallel
7647 7625 * sibling operations (such as attaches), so the parallelism
7648 7626 * we are providing is of marginal use until that is improved.
7649 7627 * The most logical way to solve this would be to have separate
7650 7628 * target and lun nodes. This would be a large change in the
7651 7629 * format of /devices paths and is not being pursued at this
7652 7630 * time. The need for parallelism will become more of an issue
7653 7631 * with top-down attach for mpxio/vhci and for iSCSI support.
7654 7632 * We may want to eventually want a dual mode implementation,
7655 7633 * where the HBA determines if we should construct separate
7656 7634 * target and lun devinfo nodes.
7657 7635 */
7658 7636 scsi_hba_devi_enter(self, &circ);
7659 7637 SCSI_HBA_LOG((_LOG(4), self, NULL, "%s@%s config_one", name, addr));
7660 7638 (void) i_ndi_make_spec_children(self, flags);
7661 7639
7662 7640 /*
7663 7641 * For bus_configone, we make sure that we can find LUN0
7664 7642 * first. This allows the delayed probe/barrier deletion for a
7665 7643 * non-existent LUN0 (if enabled in scsi_device_config) to
7666 7644 * cover all LUNs on the target. This is done to minimize the
7667 7645 * number of independent target selection timeouts that occur
7668 7646 * when a target with many LUNs is no longer accessible
7669 7647 * (powered off). This removes the need for target driver
7670 7648 * probe cache implementations.
7671 7649 *
7672 7650 * This optimization may not be desirable in a pure bridge
7673 7651 * environment where targets on the other side of the bridge
7674 7652 * show up as LUNs to the host. If we ever need to support
7675 7653 * such a configuration then we should consider implementing a
7676 7654 * SCSI_OPTIONS_ILUN0 bit.
7677 7655 *
7678 7656 * NOTE: we are *not* applying any target limitation filtering
7679 7657 * to bus_configone, which means that we are relying on the
7680 7658 * HBA tran_tgt_init entry point invoked by scsi_busctl_initchild
7681 7659 * to fail.
7682 7660 */
7683 7661 sd0 = (struct scsi_device *)-1;
7684 7662 lcp = strchr(addr, ','); /* "addr,lun[,sfunc]" */
7685 7663 if (lcp) {
7686 7664 /*
7687 7665 * With "tgt,lun[,sfunc]" addressing, multiple addressing levels
7688 7666 * have been compressed into single devinfo node unit-address.
7689 7667 * This presents a mismatch - there is no bus_config to discover
7690 7668 * LUNs below a specific target, the only choice is to
7691 7669 * BUS_CONFIG_ALL the HBA. To support BUS_CONFIG_ALL_LUNS below
7692 7670 * a specific target, a bus_configone with lun address of "*"
7693 7671 * triggers lun discovery below a target.
7694 7672 */
7695 7673 if (*(lcp + 1) == '*') {
7696 7674 mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7697 7675 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7698 7676 "scsi-enumeration", scsi_enumeration);
7699 7677 mt |= scsi_hba_log_mt_disable;
7700 7678
7701 7679 SCSI_HBA_LOG((_LOG(2), self, NULL,
7702 7680 "%s@%s lun enumeration triggered", name, addr));
7703 7681 *lcp = '\0'; /* turn ',' into '\0' */
7704 7682 scsi_hba_devi_exit(self, circ);
7705 7683 (void) scsi_hba_bus_config_taddr(self, addr,
7706 7684 mt, SE_BUSCONFIG);
7707 7685 return (NDI_FAILURE);
7708 7686 }
7709 7687
7710 7688 /* convert hex lun number from ascii */
7711 7689 lun64 = scsi_addr_to_lun64(lcp + 1);
7712 7690
7713 7691 if ((lun64 != 0) && (lun64 != SCSI_LUN64_ILLEGAL)) {
7714 7692 /*
7715 7693 * configure ",0" lun first, saving off
7716 7694 * original lun characters.
7717 7695 */
7718 7696 sc1 = *(lcp + 1);
7719 7697 sc2 = *(lcp + 2);
7720 7698 *(lcp + 1) = '0';
7721 7699 *(lcp + 2) = '\0';
7722 7700 sd0 = scsi_device_config(self,
7723 7701 NULL, addr, SE_BUSCONFIG, &circ, NULL);
7724 7702
7725 7703 /* restore original lun */
7726 7704 *(lcp + 1) = sc1;
7727 7705 *(lcp + 2) = sc2;
7728 7706
7729 7707 /*
7730 7708 * Apply maxlun filtering.
7731 7709 *
7732 7710 * Future: We still have the kludged
7733 7711 * scsi_check_ss2_LUN_limit() filtering off
7734 7712 * scsi_probe() to catch bogus driver.conf
7735 7713 * entries.
7736 7714 */
7737 7715 if (sd0 && (lun64 < SCSI_32LUNS_PER_TARGET) &&
7738 7716 (lun64 >= scsi_get_scsi_maxluns(sd0))) {
7739 7717 sd0 = NULL;
7740 7718 SCSI_HBA_LOG((_LOG(4), self, NULL,
7741 7719 "%s@%s filtered", name, addr));
7742 7720 } else
7743 7721 SCSI_HBA_LOG((_LOG(4), self, NULL,
7744 7722 "%s@%s lun 0 %s", name, addr,
7745 7723 sd0 ? "worked" : "failed"));
7746 7724 }
7747 7725 }
7748 7726
7749 7727 /*
7750 7728 * configure the requested device if LUN0 exists or we were
7751 7729 * unable to determine the lun format to determine if LUN0
7752 7730 * exists.
7753 7731 */
7754 7732 if (sd0) {
7755 7733 sd = scsi_device_config(self,
7756 7734 name, addr, SE_BUSCONFIG, &circ, NULL);
7757 7735 } else {
7758 7736 sd = NULL;
7759 7737 SCSI_HBA_LOG((_LOG(2), self, NULL,
7760 7738 "%s@%s no lun 0 or filtered lun", name, addr));
7761 7739 }
7762 7740
7763 7741 /*
7764 7742 * We know what we found, to reduce overhead we finish BUS_CONFIG_ONE
7765 7743 * processing without calling back to the frameworks
7766 7744 * ndi_busop_bus_config (unless we goto framework below).
7767 7745 *
7768 7746 * If the reference is to a driver name and we created a generic name
7769 7747 * (bound to that driver) we will still succeed. This is important
7770 7748 * for correctly resolving old drivername references to device that now
7771 7749 * uses a generic names across the transition to generic naming. This
7772 7750 * is effectively an internal implementation of the NDI_DRIVERNAME flag.
7773 7751 *
7774 7752 * We also need to special case the resolve_pathname OBP boot-device
7775 7753 * case (modrootloaded == 0) where reference is to a generic name but
7776 7754 * we created a legacy driver name node by returning just returning
7777 7755 * the node created.
7778 7756 */
7779 7757 if (sd && sd->sd_dev &&
7780 7758 ((strcmp(ddi_node_name(sd->sd_dev), name) == 0) ||
7781 7759 (strcmp(ddi_driver_name(sd->sd_dev), name) == 0) ||
7782 7760 (modrootloaded == 0)) &&
7783 7761 (ndi_devi_online(sd->sd_dev,
7784 7762 flags & NDI_NO_EVENT) == NDI_SUCCESS)) {
7785 7763
7786 7764 /* device attached, return devinfo node with hold */
7787 7765 ret = NDI_SUCCESS;
7788 7766 *childp = sd->sd_dev;
7789 7767 ndi_hold_devi(sd->sd_dev);
7790 7768 } else {
7791 7769 /*
7792 7770 * In the process of failing we may have added nodes to the HBA
7793 7771 * (self), clearing DEVI_MADE_CHILDREN. To reduce the overhead
7794 7772 * associated with the frameworks reaction to this we clear the
7795 7773 * flag here.
7796 7774 */
7797 7775 mutex_enter(&DEVI(self)->devi_lock);
7798 7776 DEVI(self)->devi_flags &= ~DEVI_MADE_CHILDREN;
7799 7777 mutex_exit(&DEVI(self)->devi_lock);
7800 7778 ret = NDI_FAILURE;
7801 7779
7802 7780 /*
7803 7781 * The framework may still be able to succeed with
7804 7782 * with its GENERIC_PROP code.
7805 7783 */
7806 7784 scsi_hba_devi_exit(self, circ);
7807 7785 if (flags & NDI_DRV_CONF_REPROBE)
7808 7786 flags |= NDI_CONFIG_REPROBE;
7809 7787 flags |= NDI_MDI_FALLBACK; /* devinfo&pathinfo children */
7810 7788 return (ndi_busop_bus_config(self, flags, BUS_CONFIG_ONE,
7811 7789 (void *)arg, childp, 0));
7812 7790 }
7813 7791
7814 7792 scsi_hba_devi_exit(self, circ);
7815 7793 return (ret);
7816 7794 }
7817 7795
7818 7796 /*
7819 7797 * Perform SCSI Parallel Interconnect bus_config
7820 7798 */
7821 7799 static int
7822 7800 scsi_hba_bus_config_spi(dev_info_t *self, uint_t flags,
7823 7801 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7824 7802 {
7825 7803 int ret;
7826 7804 int mt;
7827 7805
7828 7806 /*
7829 7807 * Enumerate scsi target devices: See if we are doing generic dynamic
7830 7808 * enumeration: if driver.conf has not specified the 'scsi-enumeration'
7831 7809 * knob then use the global scsi_enumeration knob.
7832 7810 */
7833 7811 mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7834 7812 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7835 7813 "scsi-enumeration", scsi_enumeration);
7836 7814 mt |= scsi_hba_log_mt_disable;
7837 7815
7838 7816 if ((mt & SCSI_ENUMERATION_ENABLE) == 0) {
7839 7817 /*
7840 7818 * Static driver.conf file enumeration:
7841 7819 *
7842 7820 * Force reprobe for BUS_CONFIG_ONE or when manually
7843 7821 * reconfiguring via devfsadm(1m) to emulate deferred attach.
7844 7822 * Reprobe only discovers driver.conf enumerated nodes, more
7845 7823 * dynamic implementations probably require their own
7846 7824 * bus_config.
7847 7825 */
7848 7826 if ((op == BUS_CONFIG_ONE) || (flags & NDI_DRV_CONF_REPROBE))
7849 7827 flags |= NDI_CONFIG_REPROBE;
7850 7828 flags |= NDI_MDI_FALLBACK; /* devinfo&pathinfo children */
7851 7829 return (ndi_busop_bus_config(self, flags, op, arg, childp, 0));
7852 7830 }
7853 7831
7854 7832 if (scsi_hba_bus_config_debug)
7855 7833 flags |= NDI_DEVI_DEBUG;
7856 7834
7857 7835 /*
7858 7836 * Generic spi dynamic bus config enumeration to discover and enumerate
7859 7837 * the target device nodes we are looking for.
7860 7838 */
7861 7839 switch (op) {
7862 7840 case BUS_CONFIG_ONE: /* enumerate the named child */
7863 7841 ret = scsi_hba_bus_configone(self, flags, (char *)arg, childp);
7864 7842 break;
7865 7843
7866 7844 case BUS_CONFIG_ALL: /* enumerate all children on the bus */
7867 7845 case BUS_CONFIG_DRIVER: /* enumerate all children that bind to driver */
7868 7846 SCSI_HBA_LOG((_LOG(3), self, NULL,
7869 7847 "BUS_CONFIG_%s mt %x",
7870 7848 (op == BUS_CONFIG_ALL) ? "ALL" : "DRIVER", mt));
7871 7849
7872 7850 /*
7873 7851 * Enumerate targets on SCSI parallel interconnect and let the
7874 7852 * framework finish the operation (attach the nodes).
7875 7853 */
7876 7854 if ((ret = scsi_hba_bus_configall_spi(self, mt)) == NDI_SUCCESS)
7877 7855 ret = ndi_busop_bus_config(self, flags, op,
7878 7856 arg, childp, 0);
7879 7857 break;
7880 7858
7881 7859 default:
7882 7860 ret = NDI_FAILURE;
7883 7861 break;
7884 7862 }
7885 7863 return (ret);
7886 7864 }
7887 7865
7888 7866 /*
7889 7867 * Perform SCSI Parallel Interconnect bus_unconfig
7890 7868 */
7891 7869 static int
7892 7870 scsi_hba_bus_unconfig_spi(dev_info_t *self, uint_t flags,
7893 7871 ddi_bus_config_op_t op, void *arg)
7894 7872 {
7895 7873 int mt;
7896 7874 int circ;
7897 7875 int ret;
7898 7876
7899 7877 /*
7900 7878 * See if we are doing generic dynamic enumeration: if driver.conf has
7901 7879 * not specified the 'scsi-enumeration' knob then use the global
7902 7880 * scsi_enumeration knob.
7903 7881 */
7904 7882 mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
7905 7883 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7906 7884 "scsi-enumeration", scsi_enumeration);
7907 7885 mt |= scsi_hba_log_mt_disable;
7908 7886
7909 7887 if ((mt & SCSI_ENUMERATION_ENABLE) == 0)
7910 7888 return (ndi_busop_bus_unconfig(self, flags, op, arg));
7911 7889
7912 7890 if (scsi_hba_bus_config_debug)
7913 7891 flags |= NDI_DEVI_DEBUG;
7914 7892
7915 7893 scsi_hba_devi_enter(self, &circ);
7916 7894 switch (op) {
7917 7895 case BUS_UNCONFIG_ONE:
7918 7896 SCSI_HBA_LOG((_LOG(3), self, NULL,
7919 7897 "unconfig one: %s", (char *)arg));
7920 7898 ret = NDI_SUCCESS;
7921 7899 break;
7922 7900
7923 7901 case BUS_UNCONFIG_ALL:
7924 7902 case BUS_UNCONFIG_DRIVER:
7925 7903 ret = NDI_SUCCESS;
7926 7904 break;
7927 7905
7928 7906 default:
7929 7907 ret = NDI_FAILURE;
7930 7908 break;
7931 7909 }
7932 7910
7933 7911 /* Perform the generic default bus unconfig */
7934 7912 if (ret == NDI_SUCCESS)
7935 7913 ret = ndi_busop_bus_unconfig(self, flags, op, arg);
7936 7914
7937 7915 scsi_hba_devi_exit(self, circ);
7938 7916
7939 7917 return (ret);
7940 7918 }
7941 7919
7942 7920 static int
7943 7921 scsi_hba_bus_config_tgtmap(dev_info_t *self, uint_t flags,
7944 7922 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
7945 7923 {
7946 7924 scsi_hba_tran_t *tran;
7947 7925 impl_scsi_tgtmap_t *tgtmap;
7948 7926 uint64_t tsa = 0; /* clock64_t */
7949 7927 int maxdev;
7950 7928 int sync_usec;
7951 7929 int synced;
7952 7930 int ret = NDI_FAILURE;
7953 7931
7954 7932 if ((op != BUS_CONFIG_ONE) && (op != BUS_CONFIG_ALL) &&
7955 7933 (op != BUS_CONFIG_DRIVER))
7956 7934 goto out;
7957 7935
7958 7936 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
7959 7937 tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
7960 7938 ASSERT(tgtmap);
7961 7939
7962 7940 /*
7963 7941 * MPXIO is never a sure thing (and we have mixed children), so
7964 7942 * set NDI_NDI_FALLBACK so that ndi_busop_bus_config will
7965 7943 * search for both devinfo and pathinfo children.
7966 7944 *
7967 7945 * Future: Remove NDI_MDI_FALLBACK since devcfg.c now looks for
7968 7946 * devinfo/pathinfo children in parallel (instead of old way of
7969 7947 * looking for one form of child and then doing "fallback" to
7970 7948 * look for other form of child).
7971 7949 */
7972 7950 flags |= NDI_MDI_FALLBACK; /* devinfo&pathinfo children */
7973 7951
7974 7952 /*
7975 7953 * If bus_config occurred within the map create-to-hotplug_sync window,
7976 7954 * we need the framework to wait for children that are physicaly
7977 7955 * present at map create time to show up (via tgtmap hotplug config).
7978 7956 *
7979 7957 * The duration of this window is specified by the HBA driver at
7980 7958 * scsi_hba_tgtmap_create(9F) time (during attach(9E)). Its
7981 7959 * 'csync_usec' value is selected based on how long it takes the HBA
7982 7960 * driver to get from map creation to initial observation for something
7983 7961 * already plugged in. Estimate high, a low estimate can result in
7984 7962 * devices not showing up correctly on first reference. The call to
7985 7963 * ndi_busop_bus_config needs a timeout value large enough so that
7986 7964 * the map sync call further down is not a noop (i.e. done against
7987 7965 * an empty map when something is infact plugged in). With
7988 7966 * BUS_CONFIG_ONE, the call to ndi_busop_bus_config will return as
7989 7967 * soon as the desired device is enumerated via hotplug - so we are
7990 7968 * not committed to waiting the entire time.
7991 7969 *
7992 7970 * We are typically outside the window, so timeout is 0.
7993 7971 */
7994 7972 sync_usec = tgtmap->tgtmap_create_csync_usec;
7995 7973 if (tgtmap->tgtmap_create_window) {
7996 7974 tsa = ddi_get_lbolt64() - tgtmap->tgtmap_create_time;
7997 7975 if (tsa < drv_usectohz(sync_usec)) {
7998 7976 tsa = drv_usectohz(sync_usec) - tsa;
7999 7977 ret = ndi_busop_bus_config(self,
8000 7978 flags, op, arg, childp, (clock_t)tsa);
8001 7979 } else
8002 7980 tsa = 0; /* passed window */
8003 7981
8004 7982 /* First one out closes the window. */
8005 7983 tgtmap->tgtmap_create_window = 0;
8006 7984 } else if (op == BUS_CONFIG_ONE)
8007 7985 ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
8008 7986
8009 7987 /* Return if doing a BUS_CONFIG_ONE and we found what we want. */
8010 7988 if ((op == BUS_CONFIG_ONE) && (ret == NDI_SUCCESS))
8011 7989 goto out; /* performance path */
8012 7990
8013 7991 /*
8014 7992 * We sync if we were in the window, on the first bus_config_one, and
8015 7993 * every bus_config_all (or bus_config_driver).
8016 7994 */
8017 7995 if (tsa || (tgtmap->tgtmap_sync_cnt == 0) ||
8018 7996 (op != BUS_CONFIG_ONE)) {
8019 7997 /*
8020 7998 * Sync current observations in the map and look again. We
8021 7999 * place an upper bound on the amount of time we will wait for
8022 8000 * sync to complete to avoid a bad device causing this
8023 8001 * busconfig operation to hang.
8024 8002 *
8025 8003 * We are typically stable, so damap_sync returns immediately.
8026 8004 *
8027 8005 * Max time to wait for sync is settle_usec per possible device.
8028 8006 */
8029 8007 tgtmap->tgtmap_sync_cnt++;
8030 8008 maxdev = damap_size(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]);
8031 8009 maxdev = (maxdev > scsi_hba_map_settle_f) ? maxdev :
8032 8010 scsi_hba_map_settle_f;
8033 8011 sync_usec = maxdev * tgtmap->tgtmap_settle_usec;
8034 8012 synced = scsi_tgtmap_sync((scsi_hba_tgtmap_t *)tgtmap,
8035 8013 sync_usec);
8036 8014 if (!synced)
8037 8015 SCSI_HBA_LOG((_LOGCFG, self, NULL,
8038 8016 "tgtmap_sync timeout"));
8039 8017 } else
8040 8018 synced = -1;
8041 8019
8042 8020 if (op == BUS_CONFIG_ONE)
8043 8021 ret = scsi_hba_bus_configone(self, flags, arg, childp);
8044 8022 else
8045 8023 ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
8046 8024
8047 8025 out:
8048 8026 #ifdef DEBUG
8049 8027 if (ret != NDI_SUCCESS) {
8050 8028 if (scsi_hba_bus_config_failure_msg ||
8051 8029 scsi_hba_bus_config_failure_dbg) {
8052 8030 scsi_hba_bus_config_failure_msg--;
8053 8031 printf("%s%d: bus_config_tgtmap %p failure on %s: "
8054 8032 "%d %d\n",
8055 8033 ddi_driver_name(self), ddi_get_instance(self),
8056 8034 (void *)tgtmap,
8057 8035 (op == BUS_CONFIG_ONE) ? (char *)arg : "ALL",
8058 8036 (int)tsa, synced);
8059 8037 }
8060 8038 if (scsi_hba_bus_config_failure_dbg) {
8061 8039 scsi_hba_bus_config_failure_dbg--;
8062 8040 debug_enter("config_tgtmap failure");
8063 8041 }
8064 8042 } else if (scsi_hba_bus_config_success_msg ||
8065 8043 scsi_hba_bus_config_success_dbg) {
8066 8044 scsi_hba_bus_config_success_msg--;
8067 8045 printf("%s%d: bus_config_tgtmap %p success on %s: %d %d\n",
8068 8046 ddi_driver_name(self), ddi_get_instance(self),
8069 8047 (void *)tgtmap,
8070 8048 (op == BUS_CONFIG_ONE) ? (char *)arg : "ALL",
8071 8049 (int)tsa, synced);
8072 8050 if (scsi_hba_bus_config_success_dbg) {
8073 8051 scsi_hba_bus_config_success_dbg--;
8074 8052 debug_enter("config_tgtmap success");
8075 8053 }
8076 8054 }
8077 8055 #endif /* DEBUG */
8078 8056 return (ret);
8079 8057 }
8080 8058
8081 8059 static int
8082 8060 scsi_hba_bus_unconfig_tgtmap(dev_info_t *self, uint_t flags,
8083 8061 ddi_bus_config_op_t op, void *arg)
8084 8062 {
8085 8063 int ret = NDI_FAILURE;
8086 8064
8087 8065 switch (op) {
8088 8066 case BUS_UNCONFIG_ONE:
8089 8067 case BUS_UNCONFIG_DRIVER:
8090 8068 case BUS_UNCONFIG_ALL:
8091 8069 ret = NDI_SUCCESS;
8092 8070 break;
8093 8071 default:
8094 8072 break;
8095 8073 }
8096 8074
8097 8075 if (ret == NDI_SUCCESS) {
8098 8076 flags &= ~NDI_DEVI_REMOVE;
8099 8077 ret = ndi_busop_bus_unconfig(self, flags, op, arg);
8100 8078 }
8101 8079 return (ret);
8102 8080 }
8103 8081
8104 8082 static int
8105 8083 scsi_hba_bus_config_iportmap(dev_info_t *self, uint_t flags,
8106 8084 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
8107 8085 {
8108 8086 scsi_hba_tran_t *tran;
8109 8087 impl_scsi_iportmap_t *iportmap;
8110 8088 dev_info_t *child;
8111 8089 int circ;
8112 8090 uint64_t tsa = 0; /* clock64_t */
8113 8091 int sync_usec;
8114 8092 int synced;
8115 8093 int ret = NDI_FAILURE;
8116 8094
8117 8095 if ((op != BUS_CONFIG_ONE) && (op != BUS_CONFIG_ALL) &&
8118 8096 (op != BUS_CONFIG_DRIVER))
8119 8097 goto out;
8120 8098
8121 8099 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
8122 8100 iportmap = (impl_scsi_iportmap_t *)tran->tran_iportmap;
8123 8101 ASSERT(iportmap);
8124 8102
8125 8103 /*
8126 8104 * MPXIO is never a sure thing (and we have mixed children), so
8127 8105 * set NDI_NDI_FALLBACK so that ndi_busop_bus_config will
8128 8106 * search for both devinfo and pathinfo children.
8129 8107 *
8130 8108 * Future: Remove NDI_MDI_FALLBACK since devcfg.c now looks for
8131 8109 * devinfo/pathinfo children in parallel (instead of old way of
8132 8110 * looking for one form of child and then doing "fallback" to
8133 8111 * look for other form of child).
8134 8112 */
8135 8113 flags |= NDI_MDI_FALLBACK; /* devinfo&pathinfo children */
8136 8114
8137 8115 /*
8138 8116 * If bus_config occurred within the map create-to-hotplug_sync window,
8139 8117 * we need the framework to wait for children that are physicaly
8140 8118 * present at map create time to show up (via iportmap hotplug config).
8141 8119 *
8142 8120 * The duration of this window is specified by the HBA driver at
8143 8121 * scsi_hba_iportmap_create(9F) time (during attach(9E)). Its
8144 8122 * 'csync_usec' value is selected based on how long it takes the HBA
8145 8123 * driver to get from map creation to initial observation for something
8146 8124 * already plugged in. Estimate high, a low estimate can result in
8147 8125 * devices not showing up correctly on first reference. The call to
8148 8126 * ndi_busop_bus_config needs a timeout value large enough so that
8149 8127 * the map sync call further down is not a noop (i.e. done against
8150 8128 * an empty map when something is infact plugged in). With
8151 8129 * BUS_CONFIG_ONE, the call to ndi_busop_bus_config will return as
8152 8130 * soon as the desired device is enumerated via hotplug - so we are
8153 8131 * not committed to waiting the entire time.
8154 8132 *
8155 8133 * We are typically outside the window, so timeout is 0.
8156 8134 */
8157 8135 sync_usec = iportmap->iportmap_create_csync_usec;
8158 8136 if (iportmap->iportmap_create_window) {
8159 8137 tsa = ddi_get_lbolt64() - iportmap->iportmap_create_time;
8160 8138 if (tsa < drv_usectohz(sync_usec)) {
8161 8139 tsa = drv_usectohz(sync_usec) - tsa;
8162 8140 ret = ndi_busop_bus_config(self,
8163 8141 flags, op, arg, childp, (clock_t)tsa);
8164 8142 } else
8165 8143 tsa = 0; /* passed window */
8166 8144
8167 8145 /* First one out closes the window. */
8168 8146 iportmap->iportmap_create_window = 0;
8169 8147 } else if (op == BUS_CONFIG_ONE)
8170 8148 ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
8171 8149
8172 8150 /* Return if doing a BUS_CONFIG_ONE and we found what we want. */
8173 8151 if ((op == BUS_CONFIG_ONE) && (ret == NDI_SUCCESS))
8174 8152 goto out; /* performance path */
8175 8153
8176 8154 /*
8177 8155 * We sync if we were in the window, on the first bus_config_one, and
8178 8156 * every bus_config_all (or bus_config_driver).
8179 8157 */
8180 8158 if (tsa || (iportmap->iportmap_sync_cnt == 0) ||
8181 8159 (op != BUS_CONFIG_ONE)) {
8182 8160 /*
8183 8161 * Sync current observations in the map and look again. We
8184 8162 * place an upper bound on the amount of time we will wait for
8185 8163 * sync to complete to avoid a bad device causing this
8186 8164 * busconfig operation to hang.
8187 8165 *
8188 8166 * We are typically stable, so damap_sync returns immediately.
8189 8167 *
8190 8168 * Max time to wait for sync is settle_usec times settle factor.
8191 8169 */
8192 8170 iportmap->iportmap_sync_cnt++;
8193 8171 synced = damap_sync(iportmap->iportmap_dam, sync_usec);
8194 8172 if (!synced)
8195 8173 SCSI_HBA_LOG((_LOGCFG, self, NULL,
8196 8174 "iportmap_sync timeout"));
8197 8175 } else
8198 8176 synced = -1;
8199 8177
8200 8178 if (op == BUS_CONFIG_ONE) {
8201 8179 /* create the iport node child */
8202 8180 scsi_hba_devi_enter(self, &circ);
8203 8181 if ((child = scsi_hba_bus_config_port(self, (char *)arg,
8204 8182 SE_BUSCONFIG)) != NULL) {
8205 8183 if (childp) {
8206 8184 ndi_hold_devi(child);
8207 8185 *childp = child;
8208 8186 }
8209 8187 ret = NDI_SUCCESS;
8210 8188 }
8211 8189 scsi_hba_devi_exit(self, circ);
8212 8190 } else
8213 8191 ret = ndi_busop_bus_config(self, flags, op, arg, childp, 0);
8214 8192
8215 8193 out:
8216 8194 #ifdef DEBUG
8217 8195 if (ret != NDI_SUCCESS) {
8218 8196 if (scsi_hba_bus_config_failure_msg ||
8219 8197 scsi_hba_bus_config_failure_dbg) {
8220 8198 scsi_hba_bus_config_failure_msg--;
8221 8199 printf("%s%d: bus_config_iportmap %p failure on %s: "
8222 8200 "%d %d\n",
8223 8201 ddi_driver_name(self), ddi_get_instance(self),
8224 8202 (void *)iportmap,
8225 8203 (op == BUS_CONFIG_ONE) ? (char *)arg : "ALL",
8226 8204 (int)tsa, synced);
8227 8205 }
8228 8206 if (scsi_hba_bus_config_failure_dbg) {
8229 8207 scsi_hba_bus_config_failure_dbg--;
8230 8208 debug_enter("config_iportmap failure");
8231 8209 }
8232 8210 } else if (scsi_hba_bus_config_success_msg ||
8233 8211 scsi_hba_bus_config_success_dbg) {
8234 8212 scsi_hba_bus_config_success_msg--;
8235 8213 printf("%s%d: bus_config_iportmap %p success on %s: %d %d\n",
8236 8214 ddi_driver_name(self), ddi_get_instance(self),
8237 8215 (void *)iportmap,
8238 8216 (op == BUS_CONFIG_ONE) ? (char *)arg : "ALL",
8239 8217 (int)tsa, synced);
8240 8218 if (scsi_hba_bus_config_success_dbg) {
8241 8219 scsi_hba_bus_config_success_dbg--;
8242 8220 debug_enter("config_iportmap success");
8243 8221 }
8244 8222 }
8245 8223 #endif /* DEBUG */
8246 8224 return (ret);
8247 8225 }
8248 8226
8249 8227 static int
8250 8228 scsi_hba_bus_unconfig_iportmap(dev_info_t *self, uint_t flags,
8251 8229 ddi_bus_config_op_t op, void *arg)
8252 8230 {
8253 8231 flags &= ~NDI_DEVI_REMOVE;
8254 8232 return (ndi_busop_bus_unconfig(self, flags, op, arg));
8255 8233 }
8256 8234
8257 8235 /*
8258 8236 * SCSI HBA bus config enumeration entry point. Called via the bus_ops
8259 8237 * bus_config entry point for all SCSA HBA drivers.
8260 8238 *
8261 8239 * o If an HBA implements its own bus_config via tran_bus_config then we
8262 8240 * invoke it. An HBA that implements its own tran_bus_config entry point
8263 8241 * may still call back into common SCSA code bus_config code for:
8264 8242 *
8265 8243 * o SPI bus_config (scsi_hba_bus_spi)
8266 8244 * o LUN and secondary function enumeration (scsi_hba_enum_lsf_of_t()).
8267 8245 * o configuration of a specific device (scsi_device_config).
8268 8246 * o determining 1275 SCSI nodename and compatible property
8269 8247 * (scsi_hba_nodename_compatible_get/_free).
8270 8248 *
8271 8249 * o Otherwise we implement a SCSI parallel interface (spi) bus config.
8272 8250 *
8273 8251 * Return NDI_SUCCESS if we might have created a new node.
8274 8252 * Return NDI_FAILURE if we definitely did not create a new node.
8275 8253 */
8276 8254 static int
8277 8255 scsi_hba_bus_config(dev_info_t *self, uint_t flags,
8278 8256 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
8279 8257 {
8280 8258 scsi_hba_tran_t *tran;
8281 8259 int ret;
8282 8260
8283 8261 /* make sure that we will not disappear */
8284 8262 ASSERT(DEVI(self)->devi_ref);
8285 8263
8286 8264 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
8287 8265 if (tran == NULL) {
8288 8266 /* NULL tran driver.conf config (used by cmdk). */
8289 8267 if ((op == BUS_CONFIG_ONE) || (flags & NDI_DRV_CONF_REPROBE))
8290 8268 flags |= NDI_CONFIG_REPROBE;
8291 8269 return (ndi_busop_bus_config(self, flags, op, arg, childp, 0));
8292 8270 }
8293 8271
8294 8272 /* Check if self is HBA-only node. */
8295 8273 if (tran->tran_hba_flags & SCSI_HBA_HBA) {
8296 8274 /* The bus_config request is to configure iports below HBA. */
8297 8275
8298 8276 #ifdef sparc
8299 8277 /*
8300 8278 * Sparc's 'boot-device' OBP property value lacks an /iport@X/
8301 8279 * component. Prior to the mount of root, we drive a disk@
8302 8280 * BUS_CONFIG_ONE operatino down a level to resolve an
8303 8281 * OBP 'boot-device' path.
8304 8282 *
8305 8283 * Future: Add (modrootloaded == 0) below, and insure that
8306 8284 * all attempts bus_conf of 'bo_name' (in OBP form) occur
8307 8285 * prior to 'modrootloaded = 1;' assignment in vfs_mountroot.
8308 8286 */
8309 8287 if ((op == BUS_CONFIG_ONE) &&
8310 8288 (strncmp((char *)arg, "disk@", strlen("disk@")) == 0)) {
8311 8289 return (scsi_hba_bus_config_prom_node(self,
8312 8290 flags, arg, childp));
8313 8291 }
8314 8292 #endif /* sparc */
8315 8293
8316 8294 if (tran->tran_iportmap) {
8317 8295 /* config based on scsi_hba_iportmap API */
8318 8296 ret = scsi_hba_bus_config_iportmap(self,
8319 8297 flags, op, arg, childp);
8320 8298 } else {
8321 8299 /* config based on 'iport_register' API */
8322 8300 ret = scsi_hba_bus_config_iports(self,
8323 8301 flags, op, arg, childp);
8324 8302 }
8325 8303 return (ret);
8326 8304 }
8327 8305
8328 8306 /* Check to see how the iport/HBA does target/lun bus config. */
8329 8307 if (tran->tran_bus_config) {
8330 8308 /* HBA config based on Sun-private/legacy tran_bus_config */
8331 8309 ret = tran->tran_bus_config(self, flags, op, arg, childp);
8332 8310 } else if (tran->tran_tgtmap) {
8333 8311 /* SCSAv3 config based on scsi_hba_tgtmap_*() API */
8334 8312 ret = scsi_hba_bus_config_tgtmap(self, flags, op, arg, childp);
8335 8313 } else {
8336 8314 /* SCSA config based on SCSI Parallel Interconnect */
8337 8315 ret = scsi_hba_bus_config_spi(self, flags, op, arg, childp);
8338 8316 }
8339 8317 return (ret);
8340 8318 }
8341 8319
8342 8320 /*
8343 8321 * Called via the bus_ops bus_unconfig entry point for SCSI HBA drivers.
8344 8322 */
8345 8323 static int
8346 8324 scsi_hba_bus_unconfig(dev_info_t *self, uint_t flags,
8347 8325 ddi_bus_config_op_t op, void *arg)
8348 8326 {
8349 8327 int circ;
8350 8328 scsi_hba_tran_t *tran;
8351 8329 int ret;
8352 8330
8353 8331 tran = ddi_get_driver_private(self);
8354 8332 if (tran == NULL) {
8355 8333 /* NULL tran driver.conf unconfig (used by cmdk). */
8356 8334 return (ndi_busop_bus_unconfig(self, flags, op, arg));
8357 8335 }
8358 8336
8359 8337 /*
8360 8338 * Purge barrier/probe node children. We do this prior to
8361 8339 * tran_bus_unconfig in case the unconfig implementation calls back
8362 8340 * into the common code at a different enumeration level, such a
8363 8341 * scsi_device_config, which still creates barrier/probe nodes.
8364 8342 */
8365 8343 scsi_hba_devi_enter(self, &circ);
8366 8344 scsi_hba_barrier_purge(self);
8367 8345 scsi_hba_devi_exit(self, circ);
8368 8346
8369 8347 /* DEBUG: for testing, allow bus_unconfig do drive removal. */
8370 8348 if (scsi_hba_bus_unconfig_remove)
8371 8349 flags |= NDI_DEVI_REMOVE;
8372 8350
8373 8351 /* Check if self is HBA-only node. */
8374 8352 if (tran->tran_hba_flags & SCSI_HBA_HBA) {
8375 8353 /* The bus_config request is to unconfigure iports below HBA. */
8376 8354 if (tran->tran_iportmap) {
8377 8355 /* SCSAv3 unconfig based on scsi_hba_iportmap API */
8378 8356 ret = scsi_hba_bus_unconfig_iportmap(self,
8379 8357 flags, op, arg);
8380 8358 } else if (tran->tran_bus_unconfig) {
8381 8359 /* HBA unconfig based on Sun-private/legacy API */
8382 8360 ret = tran->tran_bus_unconfig(self, flags, op, arg);
8383 8361 } else {
8384 8362 /* Standard framework unconfig. */
8385 8363 ret = ndi_busop_bus_unconfig(self, flags, op, arg);
8386 8364 }
8387 8365 return (ret);
8388 8366 }
8389 8367
8390 8368 /* Check to see how the iport/HBA does target/lun bus unconfig. */
8391 8369 if (tran->tran_bus_unconfig) {
8392 8370 /* HBA unconfig based on Sun-private/legacy tran_bus_unconfig */
8393 8371 ret = tran->tran_bus_unconfig(self, flags, op, arg);
8394 8372 } else if (tran->tran_tgtmap) {
8395 8373 /* SCSAv3 unconfig based on scsi_hba_tgtmap_*() API */
8396 8374 ret = scsi_hba_bus_unconfig_tgtmap(self, flags, op, arg);
8397 8375 } else {
8398 8376 /* SCSA unconfig based on SCSI Parallel Interconnect */
8399 8377 ret = scsi_hba_bus_unconfig_spi(self, flags, op, arg);
8400 8378 }
8401 8379 return (ret);
8402 8380 }
8403 8381
8404 8382 static int
8405 8383 scsi_tgtmap_scsi_config(void *arg, damap_t *mapp, damap_id_t tgtid)
8406 8384 {
8407 8385 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
8408 8386 dev_info_t *self = tran->tran_iport_dip;
8409 8387 impl_scsi_tgtmap_t *tgtmap;
8410 8388 char *tgtaddr;
8411 8389 int cfg_status, mt;
8412 8390
8413 8391 tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
8414 8392 tgtaddr = damap_id2addr(mapp, tgtid);
8415 8393
8416 8394 if (scsi_lunmap_create(self, tgtmap, tgtaddr) != DDI_SUCCESS) {
8417 8395 SCSI_HBA_LOG((_LOG_NF(WARN),
8418 8396 "failed to create lunmap for %s", tgtaddr));
8419 8397 }
8420 8398
8421 8399 mt = ddi_prop_get_int(DDI_DEV_T_ANY, self,
8422 8400 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-enumeration",
8423 8401 scsi_enumeration);
8424 8402 mt |= scsi_hba_log_mt_disable;
8425 8403
8426 8404 cfg_status = scsi_hba_bus_config_taddr(self, tgtaddr, mt, SE_HP);
8427 8405 if (cfg_status != NDI_SUCCESS) {
8428 8406 SCSI_HBA_LOG((_LOGCFG, self, NULL, "%s @%s config status %d",
8429 8407 damap_name(mapp), tgtaddr, cfg_status));
8430 8408 scsi_lunmap_destroy(self, tgtmap, tgtaddr);
8431 8409 return (DAM_FAILURE);
8432 8410 }
8433 8411
8434 8412 return (DAM_SUCCESS);
8435 8413 }
8436 8414
8437 8415
8438 8416 static int
8439 8417 scsi_tgtmap_scsi_unconfig(void *arg, damap_t *mapp, damap_id_t tgtid)
8440 8418 {
8441 8419 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
8442 8420 dev_info_t *self = tran->tran_iport_dip;
8443 8421 impl_scsi_tgtmap_t *tgtmap;
8444 8422 char *tgt_addr;
8445 8423
8446 8424 tgtmap = (impl_scsi_tgtmap_t *)tran->tran_tgtmap;
8447 8425 tgt_addr = damap_id2addr(mapp, tgtid);
8448 8426
8449 8427 SCSI_HBA_LOG((_LOGUNCFG, self, NULL, "%s @%s", damap_name(mapp),
8450 8428 tgt_addr));
8451 8429 scsi_lunmap_destroy(self, tgtmap, tgt_addr);
8452 8430 return (DAM_SUCCESS);
8453 8431 }
8454 8432
8455 8433 static int
8456 8434 scsi_tgtmap_smp_config(void *arg, damap_t *mapp, damap_id_t tgtid)
8457 8435 {
8458 8436 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
8459 8437 dev_info_t *self = tran->tran_iport_dip;
8460 8438 char *addr;
8461 8439
8462 8440 addr = damap_id2addr(mapp, tgtid);
8463 8441 SCSI_HBA_LOG((_LOGCFG, self, NULL, "%s @%s", damap_name(mapp), addr));
8464 8442
8465 8443 return ((smp_hba_bus_config_taddr(self, addr) == NDI_SUCCESS) ?
8466 8444 DAM_SUCCESS : DAM_FAILURE);
8467 8445 }
8468 8446
8469 8447 static int
8470 8448 scsi_tgtmap_smp_unconfig(void *arg, damap_t *mapp, damap_id_t tgtid)
8471 8449 {
8472 8450 scsi_hba_tran_t *tran = (scsi_hba_tran_t *)arg;
8473 8451 dev_info_t *self = tran->tran_iport_dip;
8474 8452 char *addr;
8475 8453 dev_info_t *child;
8476 8454 char nameaddr[SCSI_MAXNAMELEN];
8477 8455 int circ;
8478 8456
8479 8457 addr = damap_id2addr(mapp, tgtid);
8480 8458 SCSI_HBA_LOG((_LOGUNCFG, self, NULL, "%s @%s", damap_name(mapp), addr));
8481 8459
8482 8460 (void) snprintf(nameaddr, sizeof (nameaddr), "smp@%s", addr);
8483 8461 scsi_hba_devi_enter(self, &circ);
8484 8462 if ((child = ndi_devi_findchild(self, nameaddr)) == NULL) {
8485 8463 scsi_hba_devi_exit(self, circ);
8486 8464 return (DAM_SUCCESS);
8487 8465 }
8488 8466
8489 8467 if (ndi_devi_offline(child,
8490 8468 NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
8491 8469 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
8492 8470 "devinfo smp@%s offlined and removed", addr));
8493 8471 } else if (ndi_devi_device_remove(child)) {
8494 8472 /* Offline/remove failed, note new device_remove */
8495 8473 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
8496 8474 "devinfo smp@%s offline failed, device_remove",
8497 8475 addr));
8498 8476 }
8499 8477 scsi_hba_devi_exit(self, circ);
8500 8478 return (DAM_SUCCESS);
8501 8479 }
8502 8480
8503 8481 /* ARGSUSED1 */
8504 8482 static void
8505 8483 scsi_tgtmap_smp_activate(void *map_priv, char *tgt_addr, int addrid,
8506 8484 void **tgt_privp)
8507 8485 {
8508 8486 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8509 8487 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8510 8488
8511 8489 if (tgtmap->tgtmap_activate_cb) {
8512 8490 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s activated",
8513 8491 damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE]),
8514 8492 tgt_addr));
8515 8493
8516 8494 (*tgtmap->tgtmap_activate_cb)(tgtmap->tgtmap_mappriv,
8517 8495 tgt_addr, SCSI_TGT_SMP_DEVICE, tgt_privp);
8518 8496 }
8519 8497 }
8520 8498
8521 8499 /* ARGSUSED1 */
8522 8500 static void
8523 8501 scsi_tgtmap_smp_deactivate(void *map_priv, char *tgt_addr, int addrid,
8524 8502 void *tgt_privp, damap_deact_rsn_t damap_rsn)
8525 8503 {
8526 8504 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8527 8505 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8528 8506 boolean_t tgtmap_rereport;
8529 8507 scsi_tgtmap_deact_rsn_t tgtmap_rsn;
8530 8508
8531 8509 if (tgtmap->tgtmap_deactivate_cb) {
8532 8510 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s deactivated %d",
8533 8511 damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE]),
8534 8512 tgt_addr, damap_rsn));
8535 8513
8536 8514 if (damap_rsn == DAMAP_DEACT_RSN_GONE)
8537 8515 tgtmap_rsn = SCSI_TGT_DEACT_RSN_GONE;
8538 8516 else if (damap_rsn == DAMAP_DEACT_RSN_CFG_FAIL)
8539 8517 tgtmap_rsn = SCSI_TGT_DEACT_RSN_CFG_FAIL;
8540 8518 else if (damap_rsn == DAMAP_DEACT_RSN_UNSTBL)
8541 8519 tgtmap_rsn = SCSI_TGT_DEACT_RSN_UNSTBL;
8542 8520 else {
8543 8521 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
8544 8522 "%s @%s deactivated with unknown rsn",
8545 8523 damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE]),
8546 8524 tgt_addr));
8547 8525 return;
8548 8526 }
8549 8527
8550 8528 tgtmap_rereport = (*tgtmap->tgtmap_deactivate_cb)
8551 8529 (tgtmap->tgtmap_mappriv, tgt_addr,
8552 8530 SCSI_TGT_SMP_DEVICE, tgt_privp, tgtmap_rsn);
8553 8531
8554 8532 if ((tgtmap_rsn == SCSI_TGT_DEACT_RSN_CFG_FAIL) &&
8555 8533 (tgtmap_rereport == B_FALSE)) {
8556 8534 SCSI_HBA_LOG((_LOG(WARN), NULL, self,
8557 8535 "%s enumeration failed, no more retries until "
8558 8536 "config change occurs", tgt_addr));
8559 8537 }
8560 8538 }
8561 8539 }
8562 8540
8563 8541 /* ARGSUSED1 */
8564 8542 static void
8565 8543 scsi_tgtmap_scsi_activate(void *map_priv, char *tgt_addr, int addrid,
8566 8544 void **tgt_privp)
8567 8545 {
8568 8546 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8569 8547 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8570 8548
8571 8549 if (tgtmap->tgtmap_activate_cb) {
8572 8550 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s activated",
8573 8551 damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]),
8574 8552 tgt_addr));
8575 8553
8576 8554 (*tgtmap->tgtmap_activate_cb)(tgtmap->tgtmap_mappriv,
8577 8555 tgt_addr, SCSI_TGT_SCSI_DEVICE, tgt_privp);
8578 8556 }
8579 8557 }
8580 8558
8581 8559 /* ARGSUSED1 */
8582 8560 static void
8583 8561 scsi_tgtmap_scsi_deactivate(void *map_priv, char *tgt_addr, int addrid,
8584 8562 void *tgt_privp, damap_deact_rsn_t damap_rsn)
8585 8563 {
8586 8564 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)map_priv;
8587 8565 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8588 8566 boolean_t tgtmap_rereport;
8589 8567 scsi_tgtmap_deact_rsn_t tgtmap_rsn;
8590 8568
8591 8569 if (tgtmap->tgtmap_deactivate_cb) {
8592 8570 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s @%s deactivated %d",
8593 8571 damap_name(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]),
8594 8572 tgt_addr, damap_rsn));
8595 8573
8596 8574 if (damap_rsn == DAMAP_DEACT_RSN_GONE)
8597 8575 tgtmap_rsn = SCSI_TGT_DEACT_RSN_GONE;
8598 8576 else if (damap_rsn == DAMAP_DEACT_RSN_CFG_FAIL)
8599 8577 tgtmap_rsn = SCSI_TGT_DEACT_RSN_CFG_FAIL;
8600 8578 else if (damap_rsn == DAMAP_DEACT_RSN_UNSTBL)
8601 8579 tgtmap_rsn = SCSI_TGT_DEACT_RSN_UNSTBL;
8602 8580 else {
8603 8581 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
8604 8582 "%s @%s deactivated with unknown rsn", damap_name(
8605 8583 tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]),
8606 8584 tgt_addr));
8607 8585 return;
8608 8586 }
8609 8587
8610 8588 tgtmap_rereport = (*tgtmap->tgtmap_deactivate_cb)
8611 8589 (tgtmap->tgtmap_mappriv, tgt_addr,
8612 8590 SCSI_TGT_SCSI_DEVICE, tgt_privp, tgtmap_rsn);
8613 8591
8614 8592 if ((tgtmap_rsn == SCSI_TGT_DEACT_RSN_CFG_FAIL) &&
8615 8593 (tgtmap_rereport == B_FALSE)) {
8616 8594 SCSI_HBA_LOG((_LOG(WARN), NULL, self,
8617 8595 "%s enumeration failed, no more retries until "
8618 8596 "config change occurs", tgt_addr));
8619 8597 }
8620 8598 }
8621 8599 }
8622 8600
8623 8601
8624 8602 int
8625 8603 scsi_hba_tgtmap_create(dev_info_t *self, scsi_tgtmap_mode_t mode,
8626 8604 int csync_usec, int settle_usec, void *tgtmap_priv,
8627 8605 scsi_tgt_activate_cb_t activate_cb, scsi_tgt_deactivate_cb_t deactivate_cb,
8628 8606 scsi_hba_tgtmap_t **handle)
8629 8607 {
8630 8608 scsi_hba_tran_t *tran;
8631 8609 damap_t *mapp;
8632 8610 char context[64];
8633 8611 impl_scsi_tgtmap_t *tgtmap;
8634 8612 damap_rptmode_t rpt_style;
8635 8613 char *scsi_binding_set;
8636 8614 int optflags;
8637 8615
8638 8616 if (self == NULL || csync_usec == 0 ||
8639 8617 settle_usec == 0 || handle == NULL)
8640 8618 return (DDI_FAILURE);
8641 8619
8642 8620 *handle = NULL;
8643 8621
8644 8622 if (scsi_hba_iport_unit_address(self) == NULL)
8645 8623 return (DDI_FAILURE);
8646 8624
8647 8625 switch (mode) {
8648 8626 case SCSI_TM_FULLSET:
8649 8627 rpt_style = DAMAP_REPORT_FULLSET;
8650 8628 break;
8651 8629 case SCSI_TM_PERADDR:
8652 8630 rpt_style = DAMAP_REPORT_PERADDR;
8653 8631 break;
8654 8632 default:
8655 8633 return (DDI_FAILURE);
8656 8634 }
8657 8635
8658 8636 tran = (scsi_hba_tran_t *)ddi_get_driver_private(self);
8659 8637 ASSERT(tran);
8660 8638 if (tran == NULL)
8661 8639 return (DDI_FAILURE);
8662 8640
8663 8641 tgtmap = kmem_zalloc(sizeof (*tgtmap), KM_SLEEP);
8664 8642 tgtmap->tgtmap_tran = tran;
8665 8643 tgtmap->tgtmap_activate_cb = activate_cb;
8666 8644 tgtmap->tgtmap_deactivate_cb = deactivate_cb;
8667 8645 tgtmap->tgtmap_mappriv = tgtmap_priv;
8668 8646
8669 8647 tgtmap->tgtmap_create_window = 1; /* start with window */
8670 8648 tgtmap->tgtmap_create_time = ddi_get_lbolt64();
8671 8649 tgtmap->tgtmap_create_csync_usec = csync_usec;
8672 8650 tgtmap->tgtmap_settle_usec = settle_usec;
8673 8651 tgtmap->tgtmap_sync_cnt = 0;
8674 8652
8675 8653 optflags = (ddi_prop_get_int(DDI_DEV_T_ANY, self,
8676 8654 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-enumeration",
8677 8655 scsi_enumeration) & SCSI_ENUMERATION_MT_TARGET_DISABLE) ?
8678 8656 DAMAP_SERIALCONFIG : DAMAP_MTCONFIG;
8679 8657
8680 8658 (void) snprintf(context, sizeof (context), "%s%d.tgtmap.scsi",
8681 8659 ddi_driver_name(self), ddi_get_instance(self));
8682 8660 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8683 8661 if (damap_create(context, rpt_style, optflags, settle_usec,
8684 8662 tgtmap, scsi_tgtmap_scsi_activate, scsi_tgtmap_scsi_deactivate,
8685 8663 tran, scsi_tgtmap_scsi_config, scsi_tgtmap_scsi_unconfig,
8686 8664 &mapp) != DAM_SUCCESS) {
8687 8665 kmem_free(tgtmap, sizeof (*tgtmap));
8688 8666 return (DDI_FAILURE);
8689 8667 }
8690 8668 tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE] = mapp;
8691 8669
8692 8670 (void) snprintf(context, sizeof (context), "%s%d.tgtmap.smp",
8693 8671 ddi_driver_name(self), ddi_get_instance(self));
8694 8672 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8695 8673 if (damap_create(context, rpt_style, optflags,
8696 8674 settle_usec, tgtmap, scsi_tgtmap_smp_activate,
8697 8675 scsi_tgtmap_smp_deactivate,
8698 8676 tran, scsi_tgtmap_smp_config, scsi_tgtmap_smp_unconfig,
8699 8677 &mapp) != DAM_SUCCESS) {
8700 8678 damap_destroy(tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE]);
8701 8679 kmem_free(tgtmap, sizeof (*tgtmap));
8702 8680 return (DDI_FAILURE);
8703 8681 }
8704 8682 tgtmap->tgtmap_dam[SCSI_TGT_SMP_DEVICE] = mapp;
8705 8683
8706 8684 tran->tran_tgtmap = (scsi_hba_tgtmap_t *)tgtmap;
8707 8685 *handle = (scsi_hba_tgtmap_t *)tgtmap;
8708 8686
8709 8687 /*
8710 8688 * We have now set tran_tgtmap, marking the tran as using tgtmap
8711 8689 * enumeration services. To prevent the generation of legacy spi
8712 8690 * 'binding-set' compatible forms, remove the 'scsi-binding-set'
8713 8691 * property.
8714 8692 */
8715 8693 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, self,
8716 8694 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-binding-set",
8717 8695 &scsi_binding_set) == DDI_PROP_SUCCESS) {
8718 8696 if (strcmp(scsi_binding_set, scsi_binding_set_spi) == 0)
8719 8697 (void) ndi_prop_remove(DDI_DEV_T_NONE, self,
8720 8698 "scsi-binding-set");
8721 8699 ddi_prop_free(scsi_binding_set);
8722 8700 }
8723 8701 return (DDI_SUCCESS);
8724 8702 }
8725 8703
8726 8704 void
8727 8705 scsi_hba_tgtmap_destroy(scsi_hba_tgtmap_t *handle)
8728 8706 {
8729 8707 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8730 8708 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8731 8709 int i;
8732 8710
8733 8711 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8734 8712 if (tgtmap->tgtmap_dam[i]) {
8735 8713 SCSI_HBA_LOG((_LOGTGT, self, NULL,
8736 8714 "%s", damap_name(tgtmap->tgtmap_dam[i])));
8737 8715 damap_destroy(tgtmap->tgtmap_dam[i]);
8738 8716 }
8739 8717 }
8740 8718 kmem_free(tgtmap, sizeof (*tgtmap));
8741 8719 }
8742 8720
8743 8721 /* return 1 if all maps ended up syned */
8744 8722 static int
8745 8723 scsi_tgtmap_sync(scsi_hba_tgtmap_t *handle, int sync_usec)
8746 8724 {
8747 8725 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8748 8726 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8749 8727 int all_synced = 1;
8750 8728 int synced;
8751 8729 int i;
8752 8730
8753 8731 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8754 8732 if (tgtmap->tgtmap_dam[i]) {
8755 8733 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s sync begin",
8756 8734 damap_name(tgtmap->tgtmap_dam[i])));
8757 8735 synced = damap_sync(tgtmap->tgtmap_dam[i], sync_usec);
8758 8736 all_synced &= synced;
8759 8737 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s sync end %d",
8760 8738 damap_name(tgtmap->tgtmap_dam[i]), synced));
8761 8739
8762 8740 }
8763 8741 }
8764 8742 return (all_synced);
8765 8743 }
8766 8744
8767 8745 /* return 1 if all maps ended up empty */
8768 8746 static int
8769 8747 scsi_tgtmap_is_empty(scsi_hba_tgtmap_t *handle)
8770 8748 {
8771 8749 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8772 8750 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8773 8751 int all_empty = 1;
8774 8752 int empty;
8775 8753 int i;
8776 8754
8777 8755 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8778 8756 if (tgtmap->tgtmap_dam[i]) {
8779 8757 empty = damap_is_empty(tgtmap->tgtmap_dam[i]);
8780 8758 all_empty &= empty;
8781 8759 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s is_empty %d",
8782 8760 damap_name(tgtmap->tgtmap_dam[i]), empty));
8783 8761 }
8784 8762 }
8785 8763
8786 8764 return (all_empty);
8787 8765 }
8788 8766
8789 8767 static int
8790 8768 scsi_tgtmap_beginf(scsi_hba_tgtmap_t *handle, boolean_t do_begin)
8791 8769 {
8792 8770 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8793 8771 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8794 8772 char *context;
8795 8773 int rv = DAM_SUCCESS;
8796 8774 int i;
8797 8775
8798 8776 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8799 8777 if (tgtmap->tgtmap_dam[i] == NULL) {
8800 8778 continue;
8801 8779 }
8802 8780
8803 8781 context = damap_name(tgtmap->tgtmap_dam[i]);
8804 8782 if (do_begin == B_TRUE) {
8805 8783 if (i == SCSI_TGT_SCSI_DEVICE) {
8806 8784 /*
8807 8785 * In scsi_device context, so we have the
8808 8786 * 'context' string, diagnose the case where
8809 8787 * the tgtmap caller is failing to make
8810 8788 * forward progress, i.e. the caller is never
8811 8789 * completing an observation by calling
8812 8790 * scsi_hbg_tgtmap_set_end. If this occurs,
8813 8791 * the solaris target/lun state may be out
8814 8792 * of sync with hardware.
8815 8793 */
8816 8794 if (tgtmap->tgtmap_reports++ >=
8817 8795 scsi_hba_tgtmap_reports_max) {
8818 8796 tgtmap->tgtmap_noisy++;
8819 8797 if (tgtmap->tgtmap_noisy == 1) {
8820 8798 SCSI_HBA_LOG((_LOG(WARN),
8821 8799 self, NULL,
8822 8800 "%s: failing tgtmap begin",
8823 8801 context));
8824 8802 }
8825 8803 }
8826 8804 }
8827 8805
8828 8806 rv = damap_addrset_begin(tgtmap->tgtmap_dam[i]);
8829 8807 } else {
8830 8808 rv = damap_addrset_flush(tgtmap->tgtmap_dam[i]);
8831 8809 }
8832 8810
8833 8811 if (rv != DAM_SUCCESS) {
8834 8812 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s FAIL", context));
8835 8813 } else {
8836 8814 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8837 8815 }
8838 8816 }
8839 8817
8840 8818 return ((rv == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8841 8819 }
8842 8820
8843 8821
8844 8822 int
8845 8823 scsi_hba_tgtmap_set_begin(scsi_hba_tgtmap_t *handle)
8846 8824 {
8847 8825 return (scsi_tgtmap_beginf(handle, B_TRUE));
8848 8826 }
8849 8827
8850 8828 int
8851 8829 scsi_hba_tgtmap_set_flush(scsi_hba_tgtmap_t *handle)
8852 8830 {
8853 8831 return (scsi_tgtmap_beginf(handle, B_FALSE));
8854 8832 }
8855 8833
8856 8834 int
8857 8835 scsi_hba_tgtmap_set_add(scsi_hba_tgtmap_t *handle,
8858 8836 scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr, void *tgt_priv)
8859 8837 {
8860 8838 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8861 8839 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8862 8840
8863 8841 if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8864 8842 return (DDI_FAILURE);
8865 8843
8866 8844 SCSI_HBA_LOG((_LOGTGT, self, NULL,
8867 8845 "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8868 8846
8869 8847 return ((damap_addrset_add(tgtmap->tgtmap_dam[tgt_type], tgt_addr,
8870 8848 NULL, NULL, tgt_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8871 8849 }
8872 8850
8873 8851 /*ARGSUSED*/
8874 8852 int
8875 8853 scsi_hba_tgtmap_set_end(scsi_hba_tgtmap_t *handle, uint_t flags)
8876 8854 {
8877 8855 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8878 8856 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8879 8857 char *context;
8880 8858 int rv = DDI_SUCCESS;
8881 8859 int i;
8882 8860
8883 8861 tgtmap->tgtmap_reports = tgtmap->tgtmap_noisy = 0;
8884 8862
8885 8863 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8886 8864 if (tgtmap->tgtmap_dam[i] == NULL)
8887 8865 continue;
8888 8866 context = damap_name(tgtmap->tgtmap_dam[i]);
8889 8867 if (damap_addrset_end(
8890 8868 tgtmap->tgtmap_dam[i], 0) != DAM_SUCCESS) {
8891 8869 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s FAIL", context));
8892 8870 rv = DDI_FAILURE;
8893 8871 continue;
8894 8872 }
8895 8873
8896 8874 SCSI_HBA_LOG((_LOGTGT, self, NULL, "%s", context));
8897 8875 }
8898 8876 return (rv);
8899 8877 }
8900 8878
8901 8879 int
8902 8880 scsi_hba_tgtmap_tgt_add(scsi_hba_tgtmap_t *handle,
8903 8881 scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr, void *tgt_priv)
8904 8882
8905 8883 {
8906 8884 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8907 8885 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8908 8886
8909 8887 if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8910 8888 return (DDI_FAILURE);
8911 8889
8912 8890 SCSI_HBA_LOG((_LOGTGT, self, NULL,
8913 8891 "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8914 8892
8915 8893 return ((damap_addr_add(tgtmap->tgtmap_dam[tgt_type], tgt_addr, NULL,
8916 8894 NULL, tgt_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8917 8895 }
8918 8896
8919 8897 int
8920 8898 scsi_hba_tgtmap_tgt_remove(scsi_hba_tgtmap_t *handle,
8921 8899 scsi_tgtmap_tgt_type_t tgt_type, char *tgt_addr)
8922 8900 {
8923 8901 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8924 8902 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8925 8903
8926 8904 if (tgt_type >= SCSI_TGT_NTYPES || !tgtmap->tgtmap_dam[tgt_type])
8927 8905 return (DDI_FAILURE);
8928 8906
8929 8907 SCSI_HBA_LOG((_LOGTGT, self, NULL,
8930 8908 "%s @%s", damap_name(tgtmap->tgtmap_dam[tgt_type]), tgt_addr));
8931 8909
8932 8910 return ((damap_addr_del(tgtmap->tgtmap_dam[tgt_type],
8933 8911 tgt_addr) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
8934 8912 }
8935 8913
8936 8914 int
8937 8915 scsi_hba_tgtmap_lookup(scsi_hba_tgtmap_t *handle,
8938 8916 char *tgt_addr, scsi_tgtmap_tgt_type_t *r_type)
8939 8917 {
8940 8918 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)handle;
8941 8919 dev_info_t *self = tgtmap->tgtmap_tran->tran_iport_dip;
8942 8920 damap_id_t tgtid;
8943 8921 int i;
8944 8922
8945 8923 for (i = 0; i < SCSI_TGT_NTYPES; i++) {
8946 8924 tgtid = damap_lookup(tgtmap->tgtmap_dam[i], tgt_addr);
8947 8925 if (tgtid != NODAM) {
8948 8926 *r_type = i;
8949 8927 SCSI_HBA_LOG((_LOG(3), self, NULL,
8950 8928 "%s @%s found: type %d",
8951 8929 damap_name(tgtmap->tgtmap_dam[i]), tgt_addr, i));
8952 8930 damap_id_rele(tgtmap->tgtmap_dam[i], tgtid);
8953 8931 return (DDI_SUCCESS);
8954 8932 }
8955 8933 }
8956 8934
8957 8935 SCSI_HBA_LOG((_LOG(3), self, NULL,
8958 8936 "%s%d.tgtmap @%s not found",
8959 8937 ddi_driver_name(self), ddi_get_instance(self), tgt_addr));
8960 8938 return (DDI_FAILURE);
8961 8939 }
8962 8940
8963 8941 /*
8964 8942 * Return the unit-address of an 'iport' node, or NULL for non-iport node.
8965 8943 */
8966 8944 char *
8967 8945 scsi_hba_iport_unit_address(dev_info_t *self)
8968 8946 {
8969 8947 /*
8970 8948 * NOTE: Since 'self' could be a SCSA iport node or a SCSA HBA node,
8971 8949 * we can't use SCSA flavors: the flavor of a SCSA HBA node is not
8972 8950 * established/owned by SCSA, it is established by the nexus that
8973 8951 * created the SCSA HBA node (PCI) as a child.
8974 8952 *
8975 8953 * NOTE: If we want to support a node_name other than "iport" for
8976 8954 * an iport node then we can add support for a "scsa-iport-node-name"
8977 8955 * property on the SCSA HBA node. A SCSA HBA driver would set this
8978 8956 * property on the SCSA HBA node prior to using the iport API.
8979 8957 */
8980 8958 if (strcmp(ddi_node_name(self), "iport") == 0)
8981 8959 return (ddi_get_name_addr(self));
8982 8960 else
8983 8961 return (NULL);
8984 8962 }
8985 8963
8986 8964 /*
8987 8965 * Define a SCSI initiator port (bus/channel) for an HBA card that needs to
8988 8966 * support multiple SCSI ports, but only has a single HBA devinfo node. This
8989 8967 * function should be called from the HBA's attach(9E) implementation (when
8990 8968 * processing the HBA devinfo node attach) after the number of SCSI ports on
8991 8969 * the card is known or when the HBA driver DR handler detects a new port.
8992 8970 * The function returns 0 on failure and 1 on success.
8993 8971 *
8994 8972 * The implementation will add the port value into the "scsi-iports" property
8995 8973 * value maintained on the HBA node as. These properties are used by the generic
8996 8974 * scsi bus_config implementation to dynamicaly enumerate the specified iport
8997 8975 * children. The enumeration code will, on demand, create the appropriate
8998 8976 * iport children with a SCSI_ADDR_PROP_IPORTUA unit address. This node will
8999 8977 * bind to the same driver as the HBA node itself. This means that an HBA
9000 8978 * driver that uses iports should expect probe(9E), attach(9E), and detach(9E)
9001 8979 * calls on the iport children of the HBA. If configuration for all ports was
9002 8980 * already done during HBA node attach, the driver should just return
9003 8981 * DDI_SUCCESS when confronted with an iport node.
9004 8982 *
9005 8983 * A maximum of 32 iport ports are supported per HBA devinfo node.
9006 8984 *
9007 8985 * A NULL "port" can be used to indicate that the framework should enumerate
9008 8986 * target children on the HBA node itself, in addition to enumerating target
9009 8987 * children on any iport nodes declared. There are two reasons that an HBA may
9010 8988 * wish to have target children enumerated on both the HBA node and iport
9011 8989 * node(s):
9012 8990 *
9013 8991 * o If, in the past, HBA hardware had only a single physical port but now
9014 8992 * supports multiple physical ports, the updated driver that supports
9015 8993 * multiple physical ports may want to avoid /devices path upgrade issues
9016 8994 * by enumerating the first physical port under the HBA instead of as a
9017 8995 * iport.
9018 8996 *
9019 8997 * o Some hardware RAID HBA controllers (mlx, chs, etc) support multiple
9020 8998 * SCSI physical ports configured so that various physical devices on
9021 8999 * the physical ports are amalgamated into virtual devices on a virtual
9022 9000 * port. Amalgamated physical devices no longer appear to the host OS
9023 9001 * on the physical ports, but other non-amalgamated devices may still be
9024 9002 * visible on the physical ports. These drivers use a model where the
9025 9003 * physical ports are iport nodes and the HBA node is the virtual port to
9026 9004 * the configured virtual devices.
9027 9005 */
9028 9006 int
9029 9007 scsi_hba_iport_register(dev_info_t *self, char *port)
9030 9008 {
9031 9009 unsigned int ports = 0;
9032 9010 int rval, i;
9033 9011 char **iports, **newiports;
9034 9012
9035 9013 ASSERT(self);
9036 9014 if (self == NULL)
9037 9015 return (DDI_FAILURE);
9038 9016
9039 9017 rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
9040 9018 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
9041 9019 &ports);
9042 9020
9043 9021 if (ports >= SCSI_HBA_MAX_IPORTS) {
9044 9022 ddi_prop_free(iports);
9045 9023 return (DDI_FAILURE);
9046 9024 }
9047 9025
9048 9026 if (rval == DDI_PROP_SUCCESS) {
9049 9027 for (i = 0; i < ports; i++) {
9050 9028 if (strcmp(port, iports[i]) == 0) {
9051 9029 /* iport already registered */
9052 9030 ddi_prop_free(iports);
9053 9031 return (DDI_SUCCESS);
9054 9032 }
9055 9033 }
9056 9034 }
9057 9035
9058 9036 newiports = kmem_alloc((sizeof (char *) * (ports + 1)), KM_SLEEP);
9059 9037
9060 9038 for (i = 0; i < ports; i++) {
9061 9039 newiports[i] = strdup(iports[i]);
9062 9040 }
9063 9041 newiports[ports] = strdup(port);
9064 9042 ports++;
9065 9043
9066 9044 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, self,
9067 9045 "scsi-iports", newiports, ports) != DDI_PROP_SUCCESS) {
9068 9046 SCSI_HBA_LOG((_LOG(WARN), self, NULL,
9069 9047 "failed to establish %s %s",
9070 9048 SCSI_ADDR_PROP_IPORTUA, port));
9071 9049 rval = DDI_FAILURE;
9072 9050 } else {
9073 9051 rval = DDI_SUCCESS;
9074 9052 }
9075 9053
9076 9054 /* If there is iport exist, free property */
9077 9055 if (ports > 1)
9078 9056 ddi_prop_free(iports);
9079 9057 for (i = 0; i < ports; i++) {
9080 9058 strfree(newiports[i]);
9081 9059 }
9082 9060 kmem_free(newiports, (sizeof (char *)) * ports);
9083 9061
9084 9062 return (rval);
9085 9063 }
9086 9064
9087 9065 /*
9088 9066 * Check if the HBA has any scsi_hba_iport_register()ed children.
9089 9067 */
9090 9068 int
9091 9069 scsi_hba_iport_exist(dev_info_t *self)
9092 9070 {
9093 9071 unsigned int ports = 0;
9094 9072 char **iports;
9095 9073 int rval;
9096 9074
9097 9075 rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
9098 9076 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
9099 9077 &ports);
9100 9078
9101 9079 if (rval != DDI_PROP_SUCCESS)
9102 9080 return (0);
9103 9081
9104 9082 /* If there is now at least 1 iport, then iports is valid */
9105 9083 if (ports > 0) {
9106 9084 rval = 1;
9107 9085 } else
9108 9086 rval = 0;
9109 9087 ddi_prop_free(iports);
9110 9088
9111 9089 return (rval);
9112 9090 }
9113 9091
9114 9092 dev_info_t *
9115 9093 scsi_hba_iport_find(dev_info_t *self, char *portnm)
9116 9094 {
9117 9095 char *addr = NULL;
9118 9096 char **iports;
9119 9097 unsigned int num_iports = 0;
9120 9098 int rval = DDI_FAILURE;
9121 9099 int i = 0;
9122 9100 dev_info_t *child = NULL;
9123 9101
9124 9102 /* check to see if this is an HBA that defined scsi iports */
9125 9103 rval = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
9126 9104 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
9127 9105 &num_iports);
9128 9106
9129 9107 if (rval != DDI_SUCCESS) {
9130 9108 return (NULL);
9131 9109 }
9132 9110 ASSERT(num_iports > 0);
9133 9111
9134 9112 /* check to see if this port was registered */
9135 9113 for (i = 0; i < num_iports; i++) {
9136 9114 if (strcmp(iports[i], portnm) == 0)
9137 9115 break;
9138 9116 }
9139 9117
9140 9118 if (i == num_iports) {
9141 9119 child = NULL;
9142 9120 goto out;
9143 9121 }
9144 9122
9145 9123 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
9146 9124 (void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s", portnm);
9147 9125 rval = ndi_devi_config_one(self, addr, &child, NDI_NO_EVENT);
9148 9126 kmem_free(addr, SCSI_MAXNAMELEN);
9149 9127
9150 9128 if (rval != DDI_SUCCESS) {
9151 9129 child = NULL;
9152 9130 }
9153 9131 out:
9154 9132 ddi_prop_free(iports);
9155 9133 return (child);
9156 9134 }
9157 9135
9158 9136 /*
9159 9137 * Search/create the specified iport node
9160 9138 */
9161 9139 static dev_info_t *
9162 9140 scsi_hba_bus_config_port(dev_info_t *self, char *nameaddr, scsi_enum_t se)
9163 9141 {
9164 9142 dev_info_t *child; /* iport child of HBA node */
9165 9143 scsi_hba_tran_t *tran;
9166 9144 char *addr;
9167 9145 char *compat;
9168 9146
9169 9147 /*
9170 9148 * See if the iport node already exists.
9171 9149 */
9172 9150 addr = nameaddr + strlen("iport@");
9173 9151 if (child = ndi_devi_findchild(self, nameaddr)) {
9174 9152 if (ndi_devi_device_isremoved(child)) {
9175 9153 if ((se == SE_HP) || !ndi_dev_is_hotplug_node(child)) {
9176 9154 if (ndi_devi_device_insert(child))
9177 9155 SCSI_HBA_LOG((_LOGCFG, self, NULL,
9178 9156 "devinfo iport@%s device_reinsert",
9179 9157 addr));
9180 9158 } else
9181 9159 return (NULL);
9182 9160 }
9183 9161 return (child);
9184 9162 }
9185 9163
9186 9164
9187 9165 /*
9188 9166 * If config based on scsi_hba_iportmap API, only allow create
9189 9167 * from hotplug.
9190 9168 */
9191 9169 tran = ndi_flavorv_get(self, SCSA_FLAVOR_SCSI_DEVICE);
9192 9170 ASSERT(tran);
9193 9171 if (tran->tran_iportmap && (se != SE_HP))
9194 9172 return (NULL);
9195 9173
9196 9174 /* allocate and initialize a new "iport" node */
9197 9175 ndi_devi_alloc_sleep(self, "iport",
9198 9176 (se == SE_HP) ? DEVI_SID_HP_NODEID : DEVI_SID_NODEID,
9199 9177 &child);
9200 9178 ASSERT(child);
9201 9179 /*
9202 9180 * Set the flavor of the child to be IPORT flavored
9203 9181 */
9204 9182 ndi_flavor_set(child, SCSA_FLAVOR_IPORT);
9205 9183
9206 9184 /*
9207 9185 * Add the SCSI_ADDR_PROP_IPORTUA addressing property for this child.
9208 9186 * This property is used to identify a iport node, and to represent the
9209 9187 * nodes @addr form via node properties.
9210 9188 *
9211 9189 * Add "compatible" property to the "scsi-iport" node to cause it bind
9212 9190 * to the same driver as the HBA driver. Use the "driver" name
9213 9191 * instead of the "binding name" to distinguish from hw node.
9214 9192 *
9215 9193 * Give the HBA a chance, via tran_set_name_prop, to set additional
9216 9194 * iport node properties or to change the "compatible" binding
9217 9195 * prior to init_child.
9218 9196 *
9219 9197 * NOTE: the order of these operations is important so that
9220 9198 * scsi_hba_iport works when called.
9221 9199 */
9222 9200 compat = (char *)ddi_driver_name(self);
9223 9201 if ((ndi_prop_update_string(DDI_DEV_T_NONE, child,
9224 9202 SCSI_ADDR_PROP_IPORTUA, addr) != DDI_PROP_SUCCESS) ||
9225 9203 (ndi_prop_update_string_array(DDI_DEV_T_NONE, child,
9226 9204 "compatible", &compat, 1) != DDI_PROP_SUCCESS) ||
9227 9205 ddi_pathname_obp_set(child, NULL) != DDI_SUCCESS) {
9228 9206 SCSI_HBA_LOG((_LOG_NF(WARN), "%s failed dynamic decoration",
9229 9207 nameaddr));
9230 9208 (void) ddi_remove_child(child, 0);
9231 9209 child = NULL;
9232 9210 } else {
9233 9211 /*
9234 9212 * Online/attach in order to get events so devfsadm will
9235 9213 * create public names.
9236 9214 */
9237 9215 ndi_hold_devi(child);
9238 9216 if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
9239 9217 ndi_rele_devi(child);
9240 9218 ndi_prop_remove_all(child);
9241 9219 (void) ndi_devi_free(child);
9242 9220 child = NULL;
9243 9221 } else
9244 9222 ndi_rele_devi(child);
9245 9223 }
9246 9224
9247 9225 return (child);
9248 9226 }
9249 9227
9250 9228 #ifdef sparc
9251 9229 /*
9252 9230 * Future: When iportmap boot support is added, consider rewriting this to
9253 9231 * perform a scsi_hba_bus_config(BUS_CONFIG_ALL) on self (HBA) followed by
9254 9232 * a scsi_hba_bus_config(BUS_CONFIG_ONE) on each child of self (each iport).
9255 9233 */
9256 9234 /* ARGSUSED */
9257 9235 static int
9258 9236 scsi_hba_bus_config_prom_node(dev_info_t *self, uint_t flags,
9259 9237 void *arg, dev_info_t **childp)
9260 9238 {
9261 9239 char **iports;
9262 9240 int circ, i;
9263 9241 int ret = NDI_FAILURE;
9264 9242 unsigned int num_iports = 0;
9265 9243 dev_info_t *pdip = NULL;
9266 9244 char *addr = NULL;
9267 9245
9268 9246 /* check to see if this is an HBA that defined scsi iports */
9269 9247 ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
9270 9248 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
9271 9249 &num_iports);
9272 9250
9273 9251 if (ret != DDI_SUCCESS) {
9274 9252 return (ret);
9275 9253 }
9276 9254
9277 9255 ASSERT(num_iports > 0);
9278 9256
9279 9257 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
9280 9258
9281 9259 ret = NDI_FAILURE;
9282 9260
9283 9261 scsi_hba_devi_enter(self, &circ);
9284 9262
9285 9263 /* create iport nodes for each scsi port/bus */
9286 9264 for (i = 0; i < num_iports; i++) {
9287 9265 bzero(addr, SCSI_MAXNAMELEN);
9288 9266 /* Prepend the iport name */
9289 9267 (void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
9290 9268 iports[i]);
9291 9269 if (pdip = scsi_hba_bus_config_port(self, addr, SE_BUSCONFIG)) {
9292 9270 if (ndi_busop_bus_config(self, NDI_NO_EVENT,
9293 9271 BUS_CONFIG_ONE, addr, &pdip, 0) !=
9294 9272 NDI_SUCCESS) {
9295 9273 continue;
9296 9274 }
9297 9275 /*
9298 9276 * Try to configure child under iport see wehter
9299 9277 * request node is the child of the iport node
9300 9278 */
9301 9279 if (ndi_devi_config_one(pdip, arg, childp,
9302 9280 NDI_NO_EVENT) == NDI_SUCCESS) {
9303 9281 ret = NDI_SUCCESS;
9304 9282 break;
9305 9283 }
9306 9284 }
9307 9285 }
9308 9286
9309 9287 scsi_hba_devi_exit(self, circ);
9310 9288
9311 9289 kmem_free(addr, SCSI_MAXNAMELEN);
9312 9290
9313 9291 ddi_prop_free(iports);
9314 9292
9315 9293 return (ret);
9316 9294 }
9317 9295 #endif
9318 9296
9319 9297 /*
9320 9298 * Perform iport port/bus bus_config.
9321 9299 */
9322 9300 static int
9323 9301 scsi_hba_bus_config_iports(dev_info_t *self, uint_t flags,
9324 9302 ddi_bus_config_op_t op, void *arg, dev_info_t **childp)
9325 9303 {
9326 9304 char *nameaddr, *addr;
9327 9305 char **iports;
9328 9306 int circ, i;
9329 9307 int ret = NDI_FAILURE;
9330 9308 unsigned int num_iports = 0;
9331 9309
9332 9310 /* check to see if this is an HBA that defined scsi iports */
9333 9311 ret = ddi_prop_lookup_string_array(DDI_DEV_T_ANY, self,
9334 9312 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "scsi-iports", &iports,
9335 9313 &num_iports);
9336 9314
9337 9315 if (ret != DDI_SUCCESS) {
9338 9316 return (ret);
9339 9317 }
9340 9318
9341 9319 ASSERT(num_iports > 0);
9342 9320
9343 9321 scsi_hba_devi_enter(self, &circ);
9344 9322
9345 9323 switch (op) {
9346 9324 case BUS_CONFIG_ONE:
9347 9325 /* return if this operation is not against an iport node */
9348 9326 nameaddr = (char *)arg;
9349 9327 if ((nameaddr == NULL) ||
9350 9328 (strncmp(nameaddr, "iport@", strlen("iport@")) != 0)) {
9351 9329 ret = NDI_FAILURE;
9352 9330 scsi_hba_devi_exit(self, circ);
9353 9331 ddi_prop_free(iports);
9354 9332 return (ret);
9355 9333 }
9356 9334
9357 9335 /* parse the port number from "iport@%s" */
9358 9336 addr = nameaddr + strlen("iport@");
9359 9337
9360 9338 /* check to see if this port was registered */
9361 9339 for (i = 0; i < num_iports; i++) {
9362 9340 if (strcmp((iports[i]), addr) == 0)
9363 9341 break;
9364 9342 }
9365 9343
9366 9344 if (i == num_iports) {
9367 9345 ret = NDI_FAILURE;
9368 9346 break;
9369 9347 }
9370 9348
9371 9349 /* create the iport node child */
9372 9350 if (scsi_hba_bus_config_port(self, nameaddr, SE_BUSCONFIG)) {
9373 9351 ret = NDI_SUCCESS;
9374 9352 }
9375 9353 break;
9376 9354
9377 9355 case BUS_CONFIG_ALL:
9378 9356 case BUS_CONFIG_DRIVER:
9379 9357 addr = kmem_zalloc(SCSI_MAXNAMELEN, KM_SLEEP);
9380 9358 /* create iport nodes for each scsi port/bus */
9381 9359 for (i = 0; i < num_iports; i++) {
9382 9360 bzero(addr, SCSI_MAXNAMELEN);
9383 9361 /* Prepend the iport name */
9384 9362 (void) snprintf(addr, SCSI_MAXNAMELEN, "iport@%s",
9385 9363 iports[i]);
9386 9364 (void) scsi_hba_bus_config_port(self, addr,
9387 9365 SE_BUSCONFIG);
9388 9366 }
9389 9367
9390 9368 kmem_free(addr, SCSI_MAXNAMELEN);
9391 9369 ret = NDI_SUCCESS;
9392 9370 break;
9393 9371 }
9394 9372 if (ret == NDI_SUCCESS) {
9395 9373 #ifdef sparc
9396 9374 /*
9397 9375 * Mask NDI_PROMNAME since PROM doesn't have iport
9398 9376 * node at all.
9399 9377 */
9400 9378 flags &= (~NDI_PROMNAME);
9401 9379 #endif
9402 9380 flags |= NDI_MDI_FALLBACK; /* devinfo&pathinfo children */
9403 9381 ret = ndi_busop_bus_config(self, flags, op,
9404 9382 arg, childp, 0);
9405 9383 }
9406 9384 scsi_hba_devi_exit(self, circ);
9407 9385
9408 9386 ddi_prop_free(iports);
9409 9387
9410 9388 return (ret);
9411 9389 }
9412 9390
9413 9391 static int
9414 9392 scsi_iportmap_config(void *arg, damap_t *mapp, damap_id_t tgtid)
9415 9393 {
9416 9394 dev_info_t *self = (dev_info_t *)arg;
9417 9395 int circ;
9418 9396 char nameaddr[SCSI_MAXNAMELEN];
9419 9397 char *iport_addr;
9420 9398 dev_info_t *childp;
9421 9399
9422 9400 scsi_hba_devi_enter(self, &circ);
9423 9401
9424 9402 iport_addr = damap_id2addr(mapp, tgtid);
9425 9403 SCSI_HBA_LOG((_LOGIPT, self, NULL,
9426 9404 "%s @%s", damap_name(mapp), iport_addr));
9427 9405
9428 9406 (void) snprintf(nameaddr, sizeof (nameaddr), "iport@%s", iport_addr);
9429 9407 childp = scsi_hba_bus_config_port(self, nameaddr, SE_HP);
9430 9408 scsi_hba_devi_exit(self, circ);
9431 9409 return (childp != NULL ? DAM_SUCCESS : DAM_FAILURE);
9432 9410 }
9433 9411
9434 9412 static int
9435 9413 scsi_iportmap_unconfig(void *arg, damap_t *mapp, damap_id_t tgtid)
9436 9414 {
9437 9415 dev_info_t *self = arg;
9438 9416 dev_info_t *childp; /* iport child of HBA node */
9439 9417 int circ, empty;
9440 9418 char *addr;
9441 9419 char nameaddr[SCSI_MAXNAMELEN];
9442 9420 scsi_hba_tran_t *tran;
9443 9421
9444 9422 addr = damap_id2addr(mapp, tgtid);
9445 9423 SCSI_HBA_LOG((_LOGIPT, self, NULL, "%s @%s", damap_name(mapp), addr));
9446 9424
9447 9425 (void) snprintf(nameaddr, sizeof (nameaddr), "iport@%s", addr);
9448 9426 scsi_hba_devi_enter(self, &circ);
9449 9427 if ((childp = ndi_devi_findchild(self, nameaddr)) == NULL) {
9450 9428 scsi_hba_devi_exit(self, circ);
9451 9429 return (DAM_FAILURE);
9452 9430 }
9453 9431
9454 9432 tran = ddi_get_driver_private(childp);
9455 9433 ASSERT(tran);
9456 9434
9457 9435 ndi_hold_devi(childp);
9458 9436 scsi_hba_devi_exit(self, circ);
9459 9437
9460 9438 /*
9461 9439 * A begin/end (clear) against the iport's
9462 9440 * tgtmap will trigger unconfigure of all
9463 9441 * targets on the iport.
9464 9442 *
9465 9443 * Future: This bit of code only works if the
9466 9444 * target map reporting style is are full
9467 9445 * reports and not per-address. Maybe we
9468 9446 * should plan on handling this by
9469 9447 * auto-unconfiguration when destroying the
9470 9448 * target map(s).
9471 9449 */
9472 9450 (void) scsi_hba_tgtmap_set_begin(tran->tran_tgtmap);
9473 9451 (void) scsi_hba_tgtmap_set_end(tran->tran_tgtmap, 0);
9474 9452
9475 9453 /* wait for unconfigure */
9476 9454 (void) scsi_tgtmap_sync(tran->tran_tgtmap, 0);
9477 9455 empty = scsi_tgtmap_is_empty(tran->tran_tgtmap);
9478 9456
9479 9457 scsi_hba_devi_enter(self, &circ);
9480 9458 ndi_rele_devi(childp);
9481 9459
9482 9460 /* If begin/end/sync ends in empty map, offline/remove. */
9483 9461 if (empty) {
9484 9462 if (ndi_devi_offline(childp,
9485 9463 NDI_DEVFS_CLEAN | NDI_DEVI_REMOVE) == DDI_SUCCESS) {
9486 9464 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
9487 9465 "devinfo iport@%s offlined and removed",
9488 9466 addr));
9489 9467 } else if (ndi_devi_device_remove(childp)) {
9490 9468 /* Offline/rem failed, note new device_remove */
9491 9469 SCSI_HBA_LOG((_LOGUNCFG, self, NULL,
9492 9470 "devinfo iport@%s offline failed, "
9493 9471 "device_remove", addr));
9494 9472 }
9495 9473 }
9496 9474 scsi_hba_devi_exit(self, circ);
9497 9475 return (empty ? DAM_SUCCESS : DAM_FAILURE);
9498 9476 }
9499 9477
9500 9478
9501 9479 int
9502 9480 scsi_hba_iportmap_create(dev_info_t *self, int csync_usec, int settle_usec,
9503 9481 scsi_hba_iportmap_t **handle)
9504 9482 {
9505 9483 scsi_hba_tran_t *tran;
9506 9484 damap_t *mapp;
9507 9485 char context[64];
9508 9486 impl_scsi_iportmap_t *iportmap;
9509 9487
9510 9488 if (self == NULL || csync_usec == 0 ||
9511 9489 settle_usec == 0 || handle == NULL)
9512 9490 return (DDI_FAILURE);
9513 9491
9514 9492 *handle = NULL;
9515 9493
9516 9494 if (scsi_hba_iport_unit_address(self) != NULL)
9517 9495 return (DDI_FAILURE);
9518 9496
9519 9497 tran = (scsi_hba_tran_t *)ddi_get_driver_private(self);
9520 9498 ASSERT(tran);
9521 9499 if (tran == NULL)
9522 9500 return (DDI_FAILURE);
9523 9501
9524 9502 (void) snprintf(context, sizeof (context), "%s%d.iportmap",
9525 9503 ddi_driver_name(self), ddi_get_instance(self));
9526 9504
9527 9505 if (damap_create(context, DAMAP_REPORT_PERADDR, DAMAP_SERIALCONFIG,
9528 9506 settle_usec, NULL, NULL, NULL, self,
9529 9507 scsi_iportmap_config, scsi_iportmap_unconfig, &mapp) !=
9530 9508 DAM_SUCCESS) {
9531 9509 return (DDI_FAILURE);
9532 9510 }
9533 9511 iportmap = kmem_zalloc(sizeof (*iportmap), KM_SLEEP);
9534 9512 iportmap->iportmap_hba_dip = self;
9535 9513 iportmap->iportmap_dam = mapp;
9536 9514
9537 9515 iportmap->iportmap_create_window = 1; /* start with window */
9538 9516 iportmap->iportmap_create_time = ddi_get_lbolt64();
9539 9517 iportmap->iportmap_create_csync_usec = csync_usec;
9540 9518 iportmap->iportmap_settle_usec = settle_usec;
9541 9519 iportmap->iportmap_sync_cnt = 0;
9542 9520
9543 9521 tran->tran_iportmap = (scsi_hba_iportmap_t *)iportmap;
9544 9522 *handle = (scsi_hba_iportmap_t *)iportmap;
9545 9523
9546 9524 SCSI_HBA_LOG((_LOGIPT, self, NULL, "%s", damap_name(mapp)));
9547 9525 return (DDI_SUCCESS);
9548 9526 }
9549 9527
9550 9528 void
9551 9529 scsi_hba_iportmap_destroy(scsi_hba_iportmap_t *handle)
9552 9530 {
9553 9531 impl_scsi_iportmap_t *iportmap = (impl_scsi_iportmap_t *)handle;
9554 9532 dev_info_t *self = iportmap->iportmap_hba_dip;
9555 9533
9556 9534 SCSI_HBA_LOG((_LOGIPT, self, NULL,
9557 9535 "%s", damap_name(iportmap->iportmap_dam)));
9558 9536
9559 9537 damap_destroy(iportmap->iportmap_dam);
9560 9538 kmem_free(iportmap, sizeof (*iportmap));
9561 9539 }
9562 9540
9563 9541 int
9564 9542 scsi_hba_iportmap_iport_add(scsi_hba_iportmap_t *handle,
9565 9543 char *iport_addr, void *iport_priv)
9566 9544 {
9567 9545 impl_scsi_iportmap_t *iportmap = (impl_scsi_iportmap_t *)handle;
9568 9546 dev_info_t *self = iportmap->iportmap_hba_dip;
9569 9547
9570 9548 SCSI_HBA_LOG((_LOGIPT, self, NULL,
9571 9549 "%s @%s", damap_name(iportmap->iportmap_dam), iport_addr));
9572 9550
9573 9551 return ((damap_addr_add(iportmap->iportmap_dam, iport_addr, NULL,
9574 9552 NULL, iport_priv) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
9575 9553 }
9576 9554
9577 9555 int
9578 9556 scsi_hba_iportmap_iport_remove(scsi_hba_iportmap_t *handle,
9579 9557 char *iport_addr)
9580 9558 {
9581 9559 impl_scsi_iportmap_t *iportmap = (impl_scsi_iportmap_t *)handle;
9582 9560 dev_info_t *self = iportmap->iportmap_hba_dip;
9583 9561
9584 9562 SCSI_HBA_LOG((_LOGIPT, self, NULL,
9585 9563 "%s @%s", damap_name(iportmap->iportmap_dam), iport_addr));
9586 9564
9587 9565 return ((damap_addr_del(iportmap->iportmap_dam,
9588 9566 iport_addr) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
9589 9567 }
9590 9568
9591 9569 int
9592 9570 scsi_hba_iportmap_lookup(scsi_hba_iportmap_t *handle,
9593 9571 char *iport_addr)
9594 9572 {
9595 9573 impl_scsi_iportmap_t *iportmap = (impl_scsi_iportmap_t *)handle;
9596 9574 dev_info_t *self = iportmap->iportmap_hba_dip;
9597 9575 damap_id_t iportid;
9598 9576
9599 9577 iportid = damap_lookup(iportmap->iportmap_dam, iport_addr);
9600 9578 if (iportid != NODAM) {
9601 9579 SCSI_HBA_LOG((_LOG(3), self, NULL,
9602 9580 "%s @%s found",
9603 9581 damap_name(iportmap->iportmap_dam), iport_addr));
9604 9582 damap_id_rele(iportmap->iportmap_dam, iportid);
9605 9583 return (DDI_SUCCESS);
9606 9584 }
9607 9585
9608 9586 SCSI_HBA_LOG((_LOG(3), self, NULL,
9609 9587 "%s @%s not found",
9610 9588 damap_name(iportmap->iportmap_dam), iport_addr));
9611 9589 return (DDI_FAILURE);
9612 9590 }
9613 9591
9614 9592
9615 9593 static int
9616 9594 scsi_lunmap_config(void *arg, damap_t *lundam, damap_id_t lunid)
9617 9595 {
9618 9596 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)arg;
9619 9597 scsi_hba_tran_t *tran = tgtmap->tgtmap_tran;
9620 9598 dev_info_t *self = tran->tran_iport_dip;
9621 9599 char *addr;
9622 9600
9623 9601 addr = damap_id2addr(lundam, lunid);
9624 9602 SCSI_HBA_LOG((_LOGLUN, self, NULL,
9625 9603 "%s @%s", damap_name(lundam), addr));
9626 9604 if (scsi_hba_bus_configone_addr(self, addr, SE_HP) != NULL)
9627 9605 return (DAM_SUCCESS);
9628 9606 else
9629 9607 return (DAM_FAILURE);
9630 9608 }
9631 9609
9632 9610 static int
9633 9611 scsi_lunmap_unconfig(void *arg, damap_t *lundam, damap_id_t lunid)
9634 9612 {
9635 9613 impl_scsi_tgtmap_t *tgtmap = (impl_scsi_tgtmap_t *)arg;
9636 9614 scsi_hba_tran_t *tran = tgtmap->tgtmap_tran;
9637 9615 dev_info_t *self = tran->tran_iport_dip;
9638 9616 char *addr;
9639 9617
9640 9618 addr = damap_id2addr(lundam, lunid);
9641 9619 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s @%s", damap_name(lundam),
9642 9620 addr));
9643 9621
9644 9622 scsi_hba_bus_unconfigone_addr(self, addr);
9645 9623 return (DAM_SUCCESS);
9646 9624 }
9647 9625
9648 9626 static int
9649 9627 scsi_lunmap_create(dev_info_t *self, impl_scsi_tgtmap_t *tgtmap, char *taddr)
9650 9628 {
9651 9629 char context[64];
9652 9630 damap_t *tgtdam;
9653 9631 damap_id_t tgtid;
9654 9632 damap_t *lundam;
9655 9633 int optflags;
9656 9634
9657 9635 (void) snprintf(context, sizeof (context), "%s%d.%s.lunmap",
9658 9636 ddi_driver_name(self), ddi_get_instance(self), taddr);
9659 9637
9660 9638 tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
9661 9639 tgtid = damap_lookup(tgtdam, taddr);
9662 9640 if (tgtid == NODAM) {
9663 9641 SCSI_HBA_LOG((_LOG(1), self, NULL,
9664 9642 "target %s not found", context));
9665 9643 return (DDI_FAILURE);
9666 9644 }
9667 9645
9668 9646 lundam = damap_id_priv_get(tgtdam, tgtid);
9669 9647 if (lundam) {
9670 9648 SCSI_HBA_LOG((_LOG(1), self, NULL,
9671 9649 "lunmap %s already created", context));
9672 9650 damap_id_rele(tgtdam, tgtid);
9673 9651 return (DDI_FAILURE);
9674 9652 }
9675 9653
9676 9654 optflags = (ddi_prop_get_int(DDI_DEV_T_ANY, self,
9677 9655 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, "scsi-enumeration",
9678 9656 scsi_enumeration) & SCSI_ENUMERATION_MT_LUN_DISABLE) ?
9679 9657 DAMAP_SERIALCONFIG : DAMAP_MTCONFIG;
9680 9658
9681 9659 /* NOTE: expected ref at tgtid/taddr: 2: caller + lookup. */
9682 9660 ASSERT(damap_id_ref(tgtdam, tgtid) == 2);
9683 9661 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s creat, id %d ref %d",
9684 9662 context, tgtid, damap_id_ref(tgtdam, tgtid)));
9685 9663
9686 9664 /* create lundam */
9687 9665 if (damap_create(context, DAMAP_REPORT_FULLSET, optflags, 1,
9688 9666 NULL, NULL, NULL, tgtmap, scsi_lunmap_config, scsi_lunmap_unconfig,
9689 9667 &lundam) != DAM_SUCCESS) {
9690 9668 SCSI_HBA_LOG((_LOG(1), self, NULL,
9691 9669 "%s create failed, id %d ref %d",
9692 9670 context, tgtid, damap_id_ref(tgtdam, tgtid)));
9693 9671 damap_id_rele(tgtdam, tgtid);
9694 9672 return (DDI_FAILURE);
9695 9673 }
9696 9674
9697 9675 /*
9698 9676 * Return with damap_id_hold at tgtid/taddr from damap_lookup to
9699 9677 * account for damap_id_prv_set below.
9700 9678 */
9701 9679 damap_id_priv_set(tgtdam, tgtid, lundam);
9702 9680 return (DDI_SUCCESS);
9703 9681 }
9704 9682
9705 9683 static void
9706 9684 scsi_lunmap_destroy(dev_info_t *self, impl_scsi_tgtmap_t *tgtmap, char *taddr)
9707 9685 {
9708 9686 char context[64];
9709 9687 damap_t *tgtdam;
9710 9688 damap_id_t tgtid;
9711 9689 damap_t *lundam;
9712 9690
9713 9691 (void) snprintf(context, sizeof (context), "%s%d.%s.lunmap",
9714 9692 ddi_driver_name(self), ddi_get_instance(self), taddr);
9715 9693
9716 9694 tgtdam = tgtmap->tgtmap_dam[SCSI_TGT_SCSI_DEVICE];
9717 9695 tgtid = damap_lookup(tgtdam, taddr);
9718 9696 if (tgtid == NODAM) {
9719 9697 SCSI_HBA_LOG((_LOG(1), self, NULL,
9720 9698 "target %s not found", context));
9721 9699 return;
9722 9700 }
9723 9701
9724 9702 lundam = (damap_t *)damap_id_priv_get(tgtdam, tgtid);
9725 9703 if (lundam == NULL) {
9726 9704 damap_id_rele(tgtdam, tgtid); /* from damap_lookup */
9727 9705 SCSI_HBA_LOG((_LOG(1), self, NULL,
9728 9706 "lunmap %s already destroyed", context));
9729 9707 return;
9730 9708 }
9731 9709
9732 9710 /* NOTE: expected ref at tgtid/taddr: 3: priv_set + caller + lookup. */
9733 9711 ASSERT(damap_id_ref(tgtdam, tgtid) == 3);
9734 9712 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s, id %d ref %d",
9735 9713 damap_name(lundam), tgtid, damap_id_ref(tgtdam, tgtid)));
9736 9714
9737 9715 /*
9738 9716 * A begin/end (clear) against a target's lunmap will trigger
9739 9717 * unconfigure of all LUNs on the target.
9740 9718 */
9741 9719 scsi_lunmap_set_begin(self, lundam);
9742 9720 scsi_lunmap_set_end(self, lundam);
9743 9721
9744 9722 SCSI_HBA_LOG((_LOGLUN, self, NULL,
9745 9723 "%s sync begin", damap_name(lundam)));
9746 9724
9747 9725 (void) damap_sync(lundam, 0); /* wait for unconfigure */
9748 9726
9749 9727 SCSI_HBA_LOG((_LOGLUN, self, NULL,
9750 9728 "%s sync end", damap_name(lundam)));
9751 9729
9752 9730 damap_id_priv_set(tgtdam, tgtid, NULL);
9753 9731
9754 9732 /* release hold established by damap_lookup above */
9755 9733 damap_id_rele(tgtdam, tgtid);
9756 9734
9757 9735 /* release hold established since scsi_lunmap_create() */
9758 9736 damap_id_rele(tgtdam, tgtid);
9759 9737
9760 9738 damap_destroy(lundam);
9761 9739 }
9762 9740
9763 9741 static void
9764 9742 scsi_lunmap_set_begin(dev_info_t *self, damap_t *lundam)
9765 9743 {
9766 9744 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s", damap_name(lundam)));
9767 9745
9768 9746 (void) damap_addrset_begin(lundam);
9769 9747 }
9770 9748
9771 9749 static int
9772 9750 scsi_lunmap_set_add(dev_info_t *self, damap_t *lundam,
9773 9751 char *taddr, scsi_lun64_t lun64, int sfunc)
9774 9752 {
9775 9753 char ua[SCSI_MAXNAMELEN];
9776 9754
9777 9755 /* make unit address string form of "@taddr,lun[,sfunc]" */
9778 9756 if (sfunc == -1)
9779 9757 (void) snprintf(ua, sizeof (ua), "%s,%" PRIx64, taddr, lun64);
9780 9758 else
9781 9759 (void) snprintf(ua, sizeof (ua), "%s,%" PRIx64 ",%x",
9782 9760 taddr, lun64, sfunc);
9783 9761
9784 9762 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s @%s", damap_name(lundam), ua));
9785 9763
9786 9764 return ((damap_addrset_add(lundam, ua, NULL, NULL,
9787 9765 NULL) == DAM_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
9788 9766 }
9789 9767
9790 9768 static void
9791 9769 scsi_lunmap_set_end(dev_info_t *self, damap_t *lundam)
9792 9770 {
9793 9771 SCSI_HBA_LOG((_LOGLUN, self, NULL, "%s", damap_name(lundam)));
9794 9772
9795 9773 (void) damap_addrset_end(lundam, 0);
9796 9774 }
9797 9775
9798 9776 int
9799 9777 scsi_lunmap_lookup(dev_info_t *self, damap_t *lundam, char *addr)
9800 9778 {
9801 9779 damap_id_t lunid;
9802 9780
9803 9781 if ((lunid = damap_lookup(lundam, addr)) != NODAM) {
9804 9782 SCSI_HBA_LOG((_LOG(3), self, NULL,
9805 9783 "%s @%s found", damap_name(lundam), addr));
9806 9784 damap_id_rele(lundam, lunid);
9807 9785 return (DDI_SUCCESS);
9808 9786 }
9809 9787
9810 9788 SCSI_HBA_LOG((_LOG(3), self, NULL,
9811 9789 "%s @%s not found", damap_name(lundam), addr));
9812 9790 return (DDI_FAILURE);
9813 9791 }
9814 9792
9815 9793 /*
9816 9794 * phymap implementation
9817 9795 *
9818 9796 * We manage the timed aggregation of phys into a phy map * by creating a
9819 9797 * SAS port construct (based upon 'name' of "local,remote" SAS addresses)
9820 9798 * upon the first link up. As time goes on additional phys may join that port.
9821 9799 * After an appropriate amount of settle time, we trigger the activation
9822 9800 * callback which will then take the resultant bit mask of phys (phymask) in
9823 9801 * the SAS port and use that to call back to the callback function
9824 9802 * provided by the additional caller.
9825 9803 *
9826 9804 * We cross check to make sure that phys only exist in one SAS port at a
9827 9805 * time by having soft state for each phy point back to the created
9828 9806 * SAS port.
9829 9807 *
9830 9808 * NOTE: Make SAS_PHY_UA_LEN max(SAS_PHY_PHYMASK_LEN, SAS_PHY_NAME_LEN)
9831 9809 * so we have enough space if sas_phymap_bitset2phymaskua phymask address
9832 9810 * is already in use, and we end up using port name as unit address.
9833 9811 */
9834 9812 #define SAS_PHY_NAME_FMT "%" PRIx64 ",%" PRIx64
9835 9813 #define SAS_PHY_NAME_LEN (16 + 1 + 16 + 1)
9836 9814 #define SAS_PHY_NPHY (SAS2_PHYNUM_MAX + 1)
9837 9815 #define SAS_PHY_PHYMASK_LEN ((roundup(SAS_PHY_NPHY, 4)) / 4)
9838 9816 #if (SAS_PHY_PHYMASK_LEN > SAS_PHY_NAME_LEN)
9839 9817 #define SAS_PHY_UA_LEN SAS_PHY_PHYMASK_LEN
9840 9818 #else
9841 9819 #define SAS_PHY_UA_LEN SAS_PHY_NAME_LEN
9842 9820 #endif
9843 9821 typedef struct impl_sas_physet { /* needed for name2phys destroy */
9844 9822 struct impl_sas_physet *physet_next;
9845 9823 char *physet_name;
9846 9824 bitset_t *physet_phys;
9847 9825 } impl_sas_physet_t;
9848 9826 typedef struct impl_sas_phymap {
9849 9827 dev_info_t *phymap_self;
9850 9828
9851 9829 kmutex_t phymap_lock;
9852 9830 damap_t *phymap_dam;
9853 9831 void *phymap_phy2name;
9854 9832 ddi_soft_state_bystr *phymap_name2phys; /* bitset */
9855 9833 ddi_soft_state_bystr *phymap_name2ua;
9856 9834 ddi_soft_state_bystr *phymap_ua2name;
9857 9835
9858 9836 /* Noisy phy information - ensure forward progress for noisy phys */
9859 9837 int phymap_phy_max; /* max phy# */
9860 9838 int phymap_reports; /* per period */
9861 9839 int phymap_reports_max; /* scales */
9862 9840 int phymap_phys_noisy; /* detected */
9863 9841
9864 9842 /* These are for callbacks to the consumer. */
9865 9843 sas_phymap_activate_cb_t phymap_acp;
9866 9844 sas_phymap_deactivate_cb_t phymap_dcp;
9867 9845 void *phymap_private;
9868 9846
9869 9847 struct impl_sas_physet *phymap_physets;
9870 9848 } impl_sas_phymap_t;
9871 9849
9872 9850 /* Detect noisy phy: max changes per stabilization period per phy. */
9873 9851 static int sas_phymap_phy_max_factor = 16;
9874 9852
9875 9853 /*
9876 9854 * Convert bitset into a unit-address string. The maximum string length would
9877 9855 * be the maximum number of phys, rounded up by 4 and divided by 4.
9878 9856 */
9879 9857 static void
9880 9858 sas_phymap_bitset2phymaskua(bitset_t *phys, char *buf)
9881 9859 {
9882 9860 char *ptr;
9883 9861 int grp;
9884 9862 int cur;
9885 9863 uint_t bit;
9886 9864
9887 9865 bit = roundup(SAS_PHY_NPHY, 4);
9888 9866 grp = 4;
9889 9867 ptr = buf;
9890 9868 cur = 0;
9891 9869 do {
9892 9870 bit -= 1;
9893 9871 grp -= 1;
9894 9872 if (bitset_in_set(phys, bit)) {
9895 9873 cur |= (1 << grp);
9896 9874 }
9897 9875 if (grp == 0) {
9898 9876 grp = 4;
9899 9877 if (cur || ptr != buf) {
9900 9878 *ptr++ = "0123456789abcdef"[cur];
9901 9879 *ptr = 0;
9902 9880 }
9903 9881 cur = 0;
9904 9882 }
9905 9883 } while (bit != 0);
9906 9884 if (ptr == buf) {
9907 9885 *ptr++ = '0';
9908 9886 *ptr = 0;
9909 9887 }
9910 9888 }
9911 9889
9912 9890 static int
9913 9891 sas_phymap_config(void *arg, damap_t *phydam, damap_id_t phyid)
9914 9892 {
9915 9893 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)arg;
9916 9894 char *context = damap_name(phymap->phymap_dam);
9917 9895 char *damn;
9918 9896 char *name;
9919 9897 bitset_t *phys;
9920 9898 char *ua;
9921 9899 void *ua_priv;
9922 9900
9923 9901 ASSERT(context);
9924 9902
9925 9903 mutex_enter(&phymap->phymap_lock);
9926 9904 phymap->phymap_reports = phymap->phymap_phys_noisy = 0;
9927 9905
9928 9906 /* Get the name ("local,remote" address string) from damap. */
9929 9907 damn = damap_id2addr(phydam, phyid);
9930 9908
9931 9909 /* Get the bitset of phys currently forming the port. */
9932 9910 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, damn);
9933 9911 if (phys == NULL) {
9934 9912 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: no phys",
9935 9913 context, damn));
9936 9914 mutex_exit(&phymap->phymap_lock);
9937 9915 return (DAM_FAILURE);
9938 9916 }
9939 9917
9940 9918 /* allocate, get, and initialize name index of name2ua map */
9941 9919 if (ddi_soft_state_bystr_zalloc(phymap->phymap_name2ua, damn) !=
9942 9920 DDI_SUCCESS) {
9943 9921 SCSI_HBA_LOG((_LOG_NF(WARN),
9944 9922 "%s: %s: failed name2ua alloc", context, damn));
9945 9923 mutex_exit(&phymap->phymap_lock);
9946 9924 return (DAM_FAILURE);
9947 9925 }
9948 9926 if (!(ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, damn))) {
9949 9927 SCSI_HBA_LOG((_LOG_NF(WARN),
9950 9928 "%s: %s: no name2ua", context, damn));
9951 9929 mutex_exit(&phymap->phymap_lock);
9952 9930 return (DAM_FAILURE);
9953 9931 }
9954 9932 sas_phymap_bitset2phymaskua(phys, ua); /* set ua */
9955 9933
9956 9934 /* see if phymask ua index already allocated in ua2name map */
9957 9935 if (name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua)) {
9958 9936 /*
9959 9937 * The 'phymask' sas_phymap_bitset2phymaskua ua is
9960 9938 * already in use. This means that original phys have
9961 9939 * formed into a new port, and that the original port
9962 9940 * still exists (it has migrated to some completely
9963 9941 * different set of phys). In this corner-case we use
9964 9942 * "local,remote" name as a 'temporary' unit address.
9965 9943 * Reset ua in name2ua map.
9966 9944 */
9967 9945 (void) strlcpy(ua, damn, SAS_PHY_NAME_LEN);
9968 9946 name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9969 9947 if (name) {
9970 9948 /* The "local,remote" ua should be new... */
9971 9949 SCSI_HBA_LOG((_LOG_NF(WARN),
9972 9950 "%s: %s ua already configured",
9973 9951 context, ua));
9974 9952 mutex_exit(&phymap->phymap_lock);
9975 9953 return (DAM_SUCCESS);
9976 9954 }
9977 9955 }
9978 9956
9979 9957 /* allocate, get, and init ua index of ua2name map */
9980 9958 if (ddi_soft_state_bystr_zalloc(phymap->phymap_ua2name, ua) !=
9981 9959 DDI_SUCCESS) {
9982 9960 ddi_soft_state_bystr_free(phymap->phymap_name2ua, damn);
9983 9961 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: failed ua2name alloc",
9984 9962 context, damn));
9985 9963 mutex_exit(&phymap->phymap_lock);
9986 9964 return (DAM_FAILURE);
9987 9965 }
9988 9966 name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
9989 9967 if (name == NULL) {
9990 9968 ddi_soft_state_bystr_free(phymap->phymap_name2ua, damn);
9991 9969 SCSI_HBA_LOG((_LOG_NF(WARN),
9992 9970 "%s: %s: no ua2name", context, ua));
9993 9971 mutex_exit(&phymap->phymap_lock);
9994 9972 return (DAM_FAILURE);
9995 9973 }
9996 9974
9997 9975 /* set name in ua2name map */
9998 9976 (void) strlcpy(name, damn, SAS_PHY_NAME_LEN);
9999 9977
10000 9978 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10001 9979 "%s: %s: ua %s: activate", context, damn, ua));
10002 9980
10003 9981 if (phymap->phymap_acp) {
10004 9982 /*
10005 9983 * drop our lock and invoke the activation callback
10006 9984 */
10007 9985 mutex_exit(&phymap->phymap_lock);
10008 9986 ua_priv = NULL;
10009 9987 (phymap->phymap_acp)(phymap->phymap_private, ua, &ua_priv);
10010 9988 mutex_enter(&phymap->phymap_lock);
10011 9989 damap_id_priv_set(phydam, phyid, ua_priv);
10012 9990 }
10013 9991 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10014 9992 "%s: %s: ua %s: activate complete", context, damn, ua));
10015 9993 mutex_exit(&phymap->phymap_lock);
10016 9994 return (DAM_SUCCESS);
10017 9995 }
10018 9996
10019 9997 /*ARGSUSED*/
10020 9998 static int
10021 9999 sas_phymap_unconfig(void *arg, damap_t *phydam, damap_id_t phyid)
10022 10000 {
10023 10001 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)arg;
10024 10002 char *context = damap_name(phymap->phymap_dam);
10025 10003 char *damn;
10026 10004 char *ua;
10027 10005 void *ua_priv;
10028 10006
10029 10007 ASSERT(context);
10030 10008
10031 10009 mutex_enter(&phymap->phymap_lock);
10032 10010 phymap->phymap_reports = phymap->phymap_phys_noisy = 0;
10033 10011
10034 10012 /* Get the name ("local,remote" address string) from damap. */
10035 10013 damn = damap_id2addr(phydam, phyid);
10036 10014
10037 10015 if (!(ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, damn))) {
10038 10016 SCSI_HBA_LOG((_LOG_NF(WARN),
10039 10017 "%s: %s: no name2ua", context, damn));
10040 10018 mutex_exit(&phymap->phymap_lock);
10041 10019 return (DAM_FAILURE);
10042 10020 }
10043 10021
10044 10022 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10045 10023 "%s: %s: ua %s: deactivate", context, damn, ua));
10046 10024 if (phymap->phymap_dcp) {
10047 10025 ua_priv = damap_id_priv_get(phydam, phyid);
10048 10026 mutex_exit(&phymap->phymap_lock);
10049 10027 (phymap->phymap_dcp)(phymap->phymap_private, ua, ua_priv);
10050 10028 mutex_enter(&phymap->phymap_lock);
10051 10029 }
10052 10030 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10053 10031 "%s: %s: ua %s: deactivate complete", context, damn, ua));
10054 10032
10055 10033 /* delete ua<->name mappings */
10056 10034 ddi_soft_state_bystr_free(phymap->phymap_ua2name, ua);
10057 10035 ddi_soft_state_bystr_free(phymap->phymap_name2ua, damn);
10058 10036 mutex_exit(&phymap->phymap_lock);
10059 10037 return (DAM_SUCCESS);
10060 10038 }
10061 10039
10062 10040 int
10063 10041 sas_phymap_create(dev_info_t *self, int settle_usec,
10064 10042 sas_phymap_mode_t mode, void *mode_argument, void *phymap_priv,
10065 10043 sas_phymap_activate_cb_t activate_cb,
10066 10044 sas_phymap_deactivate_cb_t deactivate_cb,
10067 10045 sas_phymap_t **handlep)
10068 10046 {
10069 10047 _NOTE(ARGUNUSED(mode_argument));
10070 10048 char context[64];
10071 10049 impl_sas_phymap_t *phymap;
10072 10050
10073 10051 if (self == NULL || settle_usec == 0 || handlep == NULL)
10074 10052 return (DDI_FAILURE);
10075 10053
10076 10054 if (mode != PHYMAP_MODE_SIMPLE)
10077 10055 return (DDI_FAILURE);
10078 10056
10079 10057 phymap = kmem_zalloc(sizeof (*phymap), KM_SLEEP);
10080 10058 phymap->phymap_self = self;
10081 10059 phymap->phymap_reports_max = 1 * sas_phymap_phy_max_factor;
10082 10060 phymap->phymap_acp = activate_cb;
10083 10061 phymap->phymap_dcp = deactivate_cb;
10084 10062 phymap->phymap_private = phymap_priv;
10085 10063 mutex_init(&phymap->phymap_lock, NULL, MUTEX_DRIVER, NULL);
10086 10064
10087 10065 (void) snprintf(context, sizeof (context), "%s%d.phymap",
10088 10066 ddi_driver_name(self), ddi_get_instance(self));
10089 10067 SCSI_HBA_LOG((_LOGPHY, self, NULL, "%s", context));
10090 10068
10091 10069 if (ddi_soft_state_init(&phymap->phymap_phy2name,
10092 10070 SAS_PHY_NAME_LEN, SAS_PHY_NPHY) != 0)
10093 10071 goto fail;
10094 10072 if (ddi_soft_state_bystr_init(&phymap->phymap_name2phys,
10095 10073 sizeof (bitset_t), SAS_PHY_NPHY) != 0)
10096 10074 goto fail;
10097 10075
10098 10076 if (ddi_soft_state_bystr_init(&phymap->phymap_name2ua,
10099 10077 SAS_PHY_UA_LEN, SAS_PHY_NPHY) != 0)
10100 10078 goto fail;
10101 10079 if (ddi_soft_state_bystr_init(&phymap->phymap_ua2name,
10102 10080 SAS_PHY_NAME_LEN, SAS_PHY_NPHY) != 0)
10103 10081 goto fail;
10104 10082
10105 10083 if (damap_create(context, DAMAP_REPORT_PERADDR, DAMAP_SERIALCONFIG,
10106 10084 settle_usec, NULL, NULL, NULL,
10107 10085 phymap, sas_phymap_config, sas_phymap_unconfig,
10108 10086 &phymap->phymap_dam) != DAM_SUCCESS)
10109 10087 goto fail;
10110 10088
10111 10089
10112 10090 *handlep = (sas_phymap_t *)phymap;
10113 10091 return (DDI_SUCCESS);
10114 10092
10115 10093 fail: sas_phymap_destroy((sas_phymap_t *)phymap);
10116 10094 *handlep = NULL;
10117 10095 return (DDI_FAILURE);
10118 10096 }
10119 10097
10120 10098 void
10121 10099 sas_phymap_destroy(sas_phymap_t *handle)
10122 10100 {
10123 10101 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10124 10102 char *context;
10125 10103 struct impl_sas_physet *physet, *nphyset;
10126 10104 bitset_t *phys;
10127 10105 char *name;
10128 10106
10129 10107 context = phymap->phymap_dam ?
10130 10108 damap_name(phymap->phymap_dam) : "unknown";
10131 10109 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s", context));
10132 10110
10133 10111 if (phymap->phymap_dam)
10134 10112 damap_destroy(phymap->phymap_dam);
10135 10113
10136 10114 /* free the bitsets of allocated physets */
10137 10115 for (physet = phymap->phymap_physets; physet; physet = nphyset) {
10138 10116 nphyset = physet->physet_next;
10139 10117 phys = physet->physet_phys;
10140 10118 name = physet->physet_name;
10141 10119
10142 10120 if (phys)
10143 10121 bitset_fini(phys);
10144 10122 if (name) {
10145 10123 ddi_soft_state_bystr_free(
10146 10124 phymap->phymap_name2phys, name);
10147 10125 strfree(name);
10148 10126 }
10149 10127 kmem_free(physet, sizeof (*physet));
10150 10128 }
10151 10129
10152 10130 /* free the maps */
10153 10131 if (phymap->phymap_ua2name)
10154 10132 ddi_soft_state_bystr_fini(&phymap->phymap_ua2name);
10155 10133 if (phymap->phymap_name2ua)
10156 10134 ddi_soft_state_bystr_fini(&phymap->phymap_name2ua);
10157 10135
10158 10136 if (phymap->phymap_name2phys)
10159 10137 ddi_soft_state_bystr_fini(&phymap->phymap_name2phys);
10160 10138 if (phymap->phymap_phy2name)
10161 10139 ddi_soft_state_fini(&phymap->phymap_phy2name);
10162 10140
10163 10141 mutex_destroy(&phymap->phymap_lock);
10164 10142 kmem_free(phymap, sizeof (*phymap));
10165 10143 }
10166 10144
10167 10145
10168 10146 int
10169 10147 sas_phymap_phy_add(sas_phymap_t *handle,
10170 10148 int phy, uint64_t local, uint64_t remote)
10171 10149 {
10172 10150 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10173 10151 char *context = damap_name(phymap->phymap_dam);
10174 10152 char port[SAS_PHY_NAME_LEN];
10175 10153 char *name;
10176 10154 int phy2name_allocated = 0;
10177 10155 bitset_t *phys;
10178 10156 struct impl_sas_physet *physet;
10179 10157 int rv;
10180 10158
10181 10159 /* Create the SAS port name from the local and remote addresses. */
10182 10160 (void) snprintf(port, SAS_PHY_NAME_LEN, SAS_PHY_NAME_FMT,
10183 10161 local, remote);
10184 10162
10185 10163 mutex_enter(&phymap->phymap_lock);
10186 10164 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s: %s: add phy %d",
10187 10165 context, port, phy));
10188 10166
10189 10167 /* Check for conflict in phy2name map */
10190 10168 name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
10191 10169 if (name) {
10192 10170 if (strcmp(name, port) != 0)
10193 10171 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: add phy %d: "
10194 10172 "already in %s", context, port, phy, name));
10195 10173 else
10196 10174 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: add phy %d: "
10197 10175 "duplicate add", context, port, phy));
10198 10176 mutex_exit(&phymap->phymap_lock);
10199 10177 return (DDI_FAILURE);
10200 10178 }
10201 10179
10202 10180 /* allocate, get, and initialize phy index of phy2name map */
10203 10181 if (ddi_soft_state_zalloc(
10204 10182 phymap->phymap_phy2name, phy) != DDI_SUCCESS) {
10205 10183 SCSI_HBA_LOG((_LOG_NF(WARN),
10206 10184 "%s: %s: failed phy2name alloc", context, port));
10207 10185 goto fail;
10208 10186 }
10209 10187 name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
10210 10188 if (name == NULL) {
10211 10189 SCSI_HBA_LOG((_LOG_NF(WARN),
10212 10190 "%s: %s: no phy2name", context, port));
10213 10191 goto fail;
10214 10192 }
10215 10193 phy2name_allocated = 1;
10216 10194 (void) strlcpy(name, port, SAS_PHY_NAME_LEN); /* set name */
10217 10195
10218 10196 /* Find/alloc, initialize name index of name2phys map */
10219 10197 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
10220 10198 if (phys == NULL) {
10221 10199 if (ddi_soft_state_bystr_zalloc(phymap->phymap_name2phys,
10222 10200 name) != DDI_SUCCESS) {
10223 10201 SCSI_HBA_LOG((_LOG_NF(WARN),
10224 10202 "%s: %s: failed name2phys alloc", context, name));
10225 10203 goto fail;
10226 10204 }
10227 10205 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
10228 10206 if (phys == NULL) {
10229 10207 SCSI_HBA_LOG((_LOG_NF(WARN),
10230 10208 "%s: %s: no name2phys", context, name));
10231 10209 goto fail;
10232 10210 }
10233 10211
10234 10212 /* Initialize bitset of phys. */
10235 10213 bitset_init(phys);
10236 10214 bitset_resize(phys, SAS_PHY_NPHY);
10237 10215
10238 10216 /* Keep a list of information for destroy. */
10239 10217 physet = kmem_zalloc(sizeof (*physet), KM_SLEEP);
10240 10218 physet->physet_name = strdup(name);
10241 10219 physet->physet_phys = phys;
10242 10220 physet->physet_next = phymap->phymap_physets;
10243 10221 phymap->phymap_physets = physet;
10244 10222 }
10245 10223 ASSERT(phys);
10246 10224
10247 10225 /* Reflect 'add' in phys bitset. */
10248 10226 if (bitset_atomic_test_and_add(phys, phy) < 0) {
10249 10227 /* It is an error if the phy was already recorded. */
10250 10228 SCSI_HBA_LOG((_LOG_NF(WARN),
10251 10229 "%s: %s: phy bit %d already in port", context, name, phy));
10252 10230 goto fail;
10253 10231 }
10254 10232
10255 10233 /*
10256 10234 * Check to see if we have a new phy_max for this map, and if so
10257 10235 * scale phymap_reports_max to the new number of phys.
10258 10236 */
10259 10237 if (phy > phymap->phymap_phy_max) {
10260 10238 phymap->phymap_phy_max = phy + 1;
10261 10239 phymap->phymap_reports_max = phymap->phymap_phy_max *
10262 10240 sas_phymap_phy_max_factor;
10263 10241 }
10264 10242
10265 10243 /*
10266 10244 * If we have not reached phymap_reports_max, start/restart the
10267 10245 * activate timer. Otherwise, if phymap->phymap_reports add/rem reports
10268 10246 * ever exceeds phymap_reports_max due to noisy phys, then report the
10269 10247 * noise and force stabilization by stopping reports into the damap.
10270 10248 *
10271 10249 * The first config/unconfig callout out of the damap will reset
10272 10250 * phymap->phymap_reports.
10273 10251 */
10274 10252 rv = DDI_SUCCESS;
10275 10253 if (phymap->phymap_reports++ < phymap->phymap_reports_max) {
10276 10254 if (damap_addr_add(phymap->phymap_dam, name,
10277 10255 NULL, NULL, NULL) == DAM_SUCCESS) {
10278 10256 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10279 10257 "%s: %s: damap_addr_add", context, name));
10280 10258 } else {
10281 10259 SCSI_HBA_LOG((_LOG_NF(WARN),
10282 10260 "%s: %s: damap_addr_add failed", context, name));
10283 10261 rv = DDI_FAILURE;
10284 10262 }
10285 10263 } else {
10286 10264 phymap->phymap_phys_noisy++;
10287 10265 if (phymap->phymap_phys_noisy == 1)
10288 10266 SCSI_HBA_LOG((_LOG_NF(WARN),
10289 10267 "%s: %s: noisy phys", context, name));
10290 10268 }
10291 10269 mutex_exit(&phymap->phymap_lock);
10292 10270 return (rv);
10293 10271
10294 10272 fail: if (phy2name_allocated)
10295 10273 ddi_soft_state_free(phymap->phymap_phy2name, phy);
10296 10274 mutex_exit(&phymap->phymap_lock);
10297 10275 return (DDI_FAILURE);
10298 10276 }
10299 10277
10300 10278 int
10301 10279 sas_phymap_phy_rem(sas_phymap_t *handle, int phy)
10302 10280 {
10303 10281 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10304 10282 char *context = damap_name(phymap->phymap_dam);
10305 10283 char *name;
10306 10284 bitset_t *phys;
10307 10285 int rv = DDI_FAILURE;
10308 10286
10309 10287 ASSERT(context);
10310 10288
10311 10289 mutex_enter(&phymap->phymap_lock);
10312 10290 phymap->phymap_reports++;
10313 10291
10314 10292 /* Find and free phy index of phy2name map */
10315 10293 name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
10316 10294 if (name == NULL) {
10317 10295 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: rem phy %d: never added",
10318 10296 context, phy));
10319 10297 goto fail;
10320 10298 }
10321 10299 /* NOTE: always free phy index of phy2name map before return... */
10322 10300
10323 10301 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL, "%s: %s: rem phy %d",
10324 10302 context, name, phy));
10325 10303
10326 10304 /* Get bitset of phys currently associated with named port. */
10327 10305 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
10328 10306 if (phys == NULL) {
10329 10307 SCSI_HBA_LOG((_LOG_NF(WARN), "%s: %s: name2phys failed",
10330 10308 context, name));
10331 10309 goto fail;
10332 10310 }
10333 10311
10334 10312 /* Reflect 'rem' in phys bitset. */
10335 10313 if (bitset_atomic_test_and_del(phys, phy) < 0) {
10336 10314 /* It is an error if the phy wasn't one of the port's phys. */
10337 10315 SCSI_HBA_LOG((_LOG_NF(WARN),
10338 10316 "%s: %s: phy bit %d not in port", context, name, phy));
10339 10317 goto fail;
10340 10318 }
10341 10319
10342 10320 /* If this was the last phy in the port, start the deactivate timer. */
10343 10321 if (bitset_is_null(phys) &&
10344 10322 (phymap->phymap_reports++ < phymap->phymap_reports_max)) {
10345 10323 if (damap_addr_del(phymap->phymap_dam, name) == DAM_SUCCESS) {
10346 10324 SCSI_HBA_LOG((_LOGPHY, phymap->phymap_self, NULL,
10347 10325 "%s: %s: damap_addr_del", context, name));
10348 10326 } else {
10349 10327 SCSI_HBA_LOG((_LOG_NF(WARN),
10350 10328 "%s: %s: damap_addr_del failure", context, name));
10351 10329 goto fail;
10352 10330 }
10353 10331 }
10354 10332 rv = DDI_SUCCESS;
10355 10333
10356 10334 /* free phy index of phy2name map */
10357 10335 fail: if (name)
10358 10336 ddi_soft_state_free(phymap->phymap_phy2name, phy); /* free */
10359 10337 mutex_exit(&phymap->phymap_lock);
10360 10338 return (rv);
10361 10339 }
10362 10340
10363 10341 char *
10364 10342 sas_phymap_lookup_ua(sas_phymap_t *handle, uint64_t local, uint64_t remote)
10365 10343 {
10366 10344 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10367 10345 char *context = damap_name(phymap->phymap_dam);
10368 10346 char name[SAS_PHY_NAME_LEN];
10369 10347 char *ua;
10370 10348
10371 10349 ASSERT(context);
10372 10350
10373 10351 (void) snprintf(name, SAS_PHY_NAME_LEN, SAS_PHY_NAME_FMT,
10374 10352 local, remote);
10375 10353
10376 10354 mutex_enter(&phymap->phymap_lock);
10377 10355 ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, name);
10378 10356 SCSI_HBA_LOG((_LOG(3), phymap->phymap_self, NULL,
10379 10357 "%s: %s: ua %s", context, name, ua ? ua : "NULL"));
10380 10358 mutex_exit(&phymap->phymap_lock);
10381 10359 return (ua);
10382 10360 }
10383 10361
10384 10362 void *
10385 10363 sas_phymap_lookup_uapriv(sas_phymap_t *handle, char *ua)
10386 10364 {
10387 10365 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10388 10366 char *context = damap_name(phymap->phymap_dam);
10389 10367 char *name;
10390 10368 damap_id_t phyid;
10391 10369 void *ua_priv = NULL;
10392 10370
10393 10371 ASSERT(context);
10394 10372
10395 10373 mutex_enter(&phymap->phymap_lock);
10396 10374 name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
10397 10375 if (name) {
10398 10376 phyid = damap_lookup(phymap->phymap_dam, name);
10399 10377 if (phyid != NODAM) {
10400 10378 ua_priv = damap_id_priv_get(phymap->phymap_dam, phyid);
10401 10379 damap_id_rele(phymap->phymap_dam, phyid);
10402 10380 }
10403 10381 }
10404 10382
10405 10383 SCSI_HBA_LOG((_LOG(3), phymap->phymap_self, NULL,
10406 10384 "%s: %s: ua %s ua_priv %p", context, name,
10407 10385 ua ? ua : "NULL", ua_priv));
10408 10386 mutex_exit(&phymap->phymap_lock);
10409 10387 return (ua_priv);
10410 10388 }
10411 10389
10412 10390 int
10413 10391 sas_phymap_uahasphys(sas_phymap_t *handle, char *ua)
10414 10392 {
10415 10393 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10416 10394 char *name;
10417 10395 bitset_t *phys;
10418 10396 int n = 0;
10419 10397
10420 10398 mutex_enter(&phymap->phymap_lock);
10421 10399 name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
10422 10400 if (name) {
10423 10401 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
10424 10402 if (phys)
10425 10403 n = bitset_is_null(phys) ? 0 : 1;
10426 10404 }
10427 10405 mutex_exit(&phymap->phymap_lock);
10428 10406 return (n);
10429 10407 }
10430 10408
10431 10409 sas_phymap_phys_t *
10432 10410 sas_phymap_ua2phys(sas_phymap_t *handle, char *ua)
10433 10411 {
10434 10412 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10435 10413 char *name;
10436 10414 bitset_t *phys;
10437 10415 bitset_t *cphys = NULL;
10438 10416
10439 10417 mutex_enter(&phymap->phymap_lock);
10440 10418 name = ddi_soft_state_bystr_get(phymap->phymap_ua2name, ua);
10441 10419 if (name == NULL)
10442 10420 goto fail;
10443 10421
10444 10422 phys = ddi_soft_state_bystr_get(phymap->phymap_name2phys, name);
10445 10423 if (phys == NULL)
10446 10424 goto fail;
10447 10425
10448 10426 /* dup the phys and return */
10449 10427 cphys = kmem_alloc(sizeof (*cphys), KM_SLEEP);
10450 10428 bitset_init(cphys);
10451 10429 bitset_resize(cphys, SAS_PHY_NPHY);
10452 10430 bitset_copy(phys, cphys);
10453 10431
10454 10432 fail: mutex_exit(&phymap->phymap_lock);
10455 10433 return ((sas_phymap_phys_t *)cphys);
10456 10434 }
10457 10435
10458 10436 int
10459 10437 sas_phymap_phys_next(sas_phymap_phys_t *phys)
10460 10438 {
10461 10439 bitset_t *cphys = (bitset_t *)phys;
10462 10440 int phy;
10463 10441
10464 10442 phy = bitset_find(cphys);
10465 10443 if (phy != -1)
10466 10444 bitset_del(cphys, phy);
10467 10445 return (phy);
10468 10446 }
10469 10447
10470 10448 void
10471 10449 sas_phymap_phys_free(sas_phymap_phys_t *phys)
10472 10450 {
10473 10451 bitset_t *cphys = (bitset_t *)phys;
10474 10452
10475 10453 if (cphys) {
10476 10454 bitset_fini(cphys);
10477 10455 kmem_free(cphys, sizeof (*cphys));
10478 10456 }
10479 10457 }
10480 10458
10481 10459 char *
10482 10460 sas_phymap_phy2ua(sas_phymap_t *handle, int phy)
10483 10461 {
10484 10462 impl_sas_phymap_t *phymap = (impl_sas_phymap_t *)handle;
10485 10463 char *name;
10486 10464 char *ua;
10487 10465 char *rua = NULL;
10488 10466
10489 10467 mutex_enter(&phymap->phymap_lock);
10490 10468 name = ddi_get_soft_state(phymap->phymap_phy2name, phy);
10491 10469 if (name == NULL)
10492 10470 goto fail;
10493 10471 ua = ddi_soft_state_bystr_get(phymap->phymap_name2ua, name);
10494 10472 if (ua == NULL)
10495 10473 goto fail;
10496 10474
10497 10475 /* dup the ua and return */
10498 10476 rua = strdup(ua);
10499 10477
10500 10478 fail: mutex_exit(&phymap->phymap_lock);
10501 10479 return (rua);
10502 10480 }
10503 10481
10504 10482 void
10505 10483 sas_phymap_ua_free(char *ua)
10506 10484 {
10507 10485 if (ua)
10508 10486 strfree(ua);
10509 10487 }
↓ open down ↓ |
9509 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX