Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/comstar/stmf/stmf.c
+++ new/usr/src/uts/common/io/comstar/stmf/stmf.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
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 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright 2012, Nexenta Systems, Inc. All rights reserved.
26 26 * Copyright (c) 2013 by Delphix. All rights reserved.
27 27 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 28 */
29 29
30 30 #include <sys/conf.h>
31 31 #include <sys/file.h>
32 32 #include <sys/ddi.h>
33 33 #include <sys/sunddi.h>
34 34 #include <sys/modctl.h>
35 35 #include <sys/scsi/scsi.h>
36 36 #include <sys/scsi/generic/persist.h>
37 37 #include <sys/scsi/impl/scsi_reset_notify.h>
38 38 #include <sys/disp.h>
39 39 #include <sys/byteorder.h>
40 40 #include <sys/atomic.h>
41 41 #include <sys/ethernet.h>
42 42 #include <sys/sdt.h>
43 43 #include <sys/nvpair.h>
44 44 #include <sys/zone.h>
45 45 #include <sys/id_space.h>
46 46
47 47 #include <sys/stmf.h>
48 48 #include <sys/lpif.h>
49 49 #include <sys/portif.h>
50 50 #include <sys/stmf_ioctl.h>
51 51 #include <sys/pppt_ic_if.h>
52 52
53 53 #include "stmf_impl.h"
54 54 #include "lun_map.h"
55 55 #include "stmf_state.h"
56 56 #include "stmf_stats.h"
57 57
58 58 /*
59 59 * Lock order:
60 60 * stmf_state_lock --> ilport_lock/iss_lockp --> ilu_task_lock
61 61 */
62 62
63 63 static uint64_t stmf_session_counter = 0;
64 64 static uint16_t stmf_rtpid_counter = 0;
65 65 /* start messages at 1 */
66 66 static uint64_t stmf_proxy_msg_id = 1;
67 67 #define MSG_ID_TM_BIT 0x8000000000000000
68 68 #define ALIGNED_TO_8BYTE_BOUNDARY(i) (((i) + 7) & ~7)
69 69
70 70 /*
71 71 * When stmf_io_deadman_enabled is set to B_TRUE, we check that finishing up
72 72 * I/O operations on an offlining LU doesn't take longer than stmf_io_deadman
73 73 * seconds. If it does, we trigger a panic to inform the user of hung I/O
74 74 * blocking us for too long.
75 75 */
76 76 boolean_t stmf_io_deadman_enabled = B_TRUE;
77 77 int stmf_io_deadman = 1000; /* seconds */
78 78
79 79 struct stmf_svc_clocks;
80 80
81 81 static int stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
82 82 static int stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
83 83 static int stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
84 84 void **result);
85 85 static int stmf_open(dev_t *devp, int flag, int otype, cred_t *credp);
86 86 static int stmf_close(dev_t dev, int flag, int otype, cred_t *credp);
87 87 static int stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
88 88 cred_t *credp, int *rval);
89 89 static int stmf_get_stmf_state(stmf_state_desc_t *std);
90 90 static int stmf_set_stmf_state(stmf_state_desc_t *std);
91 91 static void stmf_abort_task_offline(scsi_task_t *task, int offline_lu,
92 92 char *info);
93 93 static int stmf_set_alua_state(stmf_alua_state_desc_t *alua_state);
94 94 static void stmf_get_alua_state(stmf_alua_state_desc_t *alua_state);
95 95
96 96 static void stmf_task_audit(stmf_i_scsi_task_t *itask,
97 97 task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf);
98 98
99 99 static boolean_t stmf_base16_str_to_binary(char *c, int dplen, uint8_t *dp);
100 100 static char stmf_ctoi(char c);
101 101 stmf_xfer_data_t *stmf_prepare_tpgs_data(uint8_t ilu_alua);
102 102 void stmf_svc_init();
103 103 stmf_status_t stmf_svc_fini();
104 104 void stmf_svc(void *arg);
105 105 static void stmf_wait_ilu_tasks_finish(stmf_i_lu_t *ilu);
106 106 void stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info);
107 107 static void stmf_svc_kill_obj_requests(void *obj);
108 108 static void stmf_svc_timeout(struct stmf_svc_clocks *);
109 109 void stmf_check_freetask();
110 110 void stmf_abort_target_reset(scsi_task_t *task);
111 111 stmf_status_t stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task,
112 112 int target_reset);
113 113 void stmf_target_reset_poll(struct scsi_task *task);
114 114 void stmf_handle_lun_reset(scsi_task_t *task);
115 115 void stmf_handle_target_reset(scsi_task_t *task);
116 116 void stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off);
117 117 int stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
118 118 uint32_t *err_ret);
119 119 int stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi);
120 120 int stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
121 121 uint32_t *err_ret);
122 122 void stmf_delete_ppd(stmf_pp_data_t *ppd);
123 123 void stmf_delete_all_ppds();
124 124 void stmf_trace_clear();
125 125 void stmf_worker_init();
126 126 stmf_status_t stmf_worker_fini();
127 127 void stmf_worker_mgmt();
128 128 void stmf_worker_task(void *arg);
129 129 static void stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss);
130 130 static stmf_status_t stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg,
131 131 uint32_t type);
132 132 static stmf_status_t stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg);
133 133 static stmf_status_t stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg);
134 134 static stmf_status_t stmf_ic_rx_status(stmf_ic_status_msg_t *msg);
135 135 static stmf_status_t stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg);
136 136 void stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s);
137 137
138 138 /* pppt modhandle */
139 139 ddi_modhandle_t pppt_mod;
140 140
141 141 /* pppt modload imported functions */
142 142 stmf_ic_reg_port_msg_alloc_func_t ic_reg_port_msg_alloc;
143 143 stmf_ic_dereg_port_msg_alloc_func_t ic_dereg_port_msg_alloc;
144 144 stmf_ic_reg_lun_msg_alloc_func_t ic_reg_lun_msg_alloc;
145 145 stmf_ic_dereg_lun_msg_alloc_func_t ic_dereg_lun_msg_alloc;
146 146 stmf_ic_lun_active_msg_alloc_func_t ic_lun_active_msg_alloc;
147 147 stmf_ic_scsi_cmd_msg_alloc_func_t ic_scsi_cmd_msg_alloc;
148 148 stmf_ic_scsi_data_xfer_done_msg_alloc_func_t ic_scsi_data_xfer_done_msg_alloc;
149 149 stmf_ic_session_create_msg_alloc_func_t ic_session_reg_msg_alloc;
150 150 stmf_ic_session_destroy_msg_alloc_func_t ic_session_dereg_msg_alloc;
151 151 stmf_ic_tx_msg_func_t ic_tx_msg;
152 152 stmf_ic_msg_free_func_t ic_msg_free;
153 153
154 154 static void stmf_itl_task_start(stmf_i_scsi_task_t *itask);
155 155 static void stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask);
156 156 static void stmf_itl_task_done(stmf_i_scsi_task_t *itask);
157 157
158 158 static void stmf_lport_xfer_start(stmf_i_scsi_task_t *itask,
159 159 stmf_data_buf_t *dbuf);
160 160 static void stmf_lport_xfer_done(stmf_i_scsi_task_t *itask,
161 161 stmf_data_buf_t *dbuf);
162 162
163 163 static void stmf_update_kstat_lu_q(scsi_task_t *, void());
164 164 static void stmf_update_kstat_lport_q(scsi_task_t *, void());
165 165 static void stmf_update_kstat_lu_io(scsi_task_t *, stmf_data_buf_t *);
166 166 static void stmf_update_kstat_lport_io(scsi_task_t *, stmf_data_buf_t *);
167 167
168 168 static int stmf_irport_compare(const void *void_irport1,
169 169 const void *void_irport2);
170 170 static stmf_i_remote_port_t *stmf_irport_create(scsi_devid_desc_t *rport_devid);
171 171 static void stmf_irport_destroy(stmf_i_remote_port_t *irport);
172 172 static stmf_i_remote_port_t *stmf_irport_register(
173 173 scsi_devid_desc_t *rport_devid);
174 174 static stmf_i_remote_port_t *stmf_irport_lookup_locked(
175 175 scsi_devid_desc_t *rport_devid);
176 176 static void stmf_irport_deregister(stmf_i_remote_port_t *irport);
177 177
178 178 extern struct mod_ops mod_driverops;
179 179
180 180 /* =====[ Tunables ]===== */
181 181 /* Internal tracing */
182 182 volatile int stmf_trace_on = 1;
183 183 volatile int stmf_trace_buf_size = (1 * 1024 * 1024);
184 184 /*
185 185 * The reason default task timeout is 75 is because we want the
186 186 * host to timeout 1st and mostly host timeout is 60 seconds.
187 187 */
188 188 volatile int stmf_default_task_timeout = 75;
189 189 /*
190 190 * Setting this to one means, you are responsible for config load and keeping
191 191 * things in sync with persistent database.
192 192 */
193 193 volatile int stmf_allow_modunload = 0;
194 194
195 195 volatile int stmf_max_nworkers = 256;
196 196 volatile int stmf_min_nworkers = 4;
197 197 volatile int stmf_worker_scale_down_delay = 20;
198 198
199 199 /* === [ Debugging and fault injection ] === */
200 200 #ifdef DEBUG
201 201 volatile uint32_t stmf_drop_task_counter = 0;
202 202 volatile uint32_t stmf_drop_buf_counter = 0;
203 203
204 204 #endif
205 205
206 206 stmf_state_t stmf_state;
207 207 static stmf_lu_t *dlun0;
208 208
209 209 static uint8_t stmf_first_zero[] =
210 210 { 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0xff };
211 211 static uint8_t stmf_first_one[] =
212 212 { 0xff, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 };
213 213
214 214 static kmutex_t trace_buf_lock;
215 215 static int trace_buf_size;
216 216 static int trace_buf_curndx;
217 217 caddr_t stmf_trace_buf;
218 218
219 219 static enum {
220 220 STMF_WORKERS_DISABLED = 0,
221 221 STMF_WORKERS_ENABLING,
222 222 STMF_WORKERS_ENABLED
223 223 } stmf_workers_state = STMF_WORKERS_DISABLED;
224 224 static int stmf_i_max_nworkers;
225 225 static int stmf_i_min_nworkers;
226 226 static int stmf_nworkers_cur; /* # of workers currently running */
227 227 static int stmf_nworkers_needed; /* # of workers need to be running */
228 228 static int stmf_worker_sel_counter = 0;
229 229 static uint32_t stmf_cur_ntasks = 0;
230 230 static clock_t stmf_wm_last = 0;
231 231 /*
232 232 * This is equal to stmf_nworkers_cur while we are increasing # workers and
233 233 * stmf_nworkers_needed while we are decreasing the worker count.
234 234 */
235 235 static int stmf_nworkers_accepting_cmds;
236 236 static stmf_worker_t *stmf_workers = NULL;
237 237 static clock_t stmf_worker_mgmt_delay = 2;
238 238 static clock_t stmf_worker_scale_down_timer = 0;
239 239 static int stmf_worker_scale_down_qd = 0;
240 240
241 241 static struct cb_ops stmf_cb_ops = {
242 242 stmf_open, /* open */
243 243 stmf_close, /* close */
244 244 nodev, /* strategy */
245 245 nodev, /* print */
246 246 nodev, /* dump */
247 247 nodev, /* read */
248 248 nodev, /* write */
249 249 stmf_ioctl, /* ioctl */
250 250 nodev, /* devmap */
251 251 nodev, /* mmap */
252 252 nodev, /* segmap */
253 253 nochpoll, /* chpoll */
254 254 ddi_prop_op, /* cb_prop_op */
255 255 0, /* streamtab */
256 256 D_NEW | D_MP, /* cb_flag */
257 257 CB_REV, /* rev */
258 258 nodev, /* aread */
259 259 nodev /* awrite */
260 260 };
261 261
262 262 static struct dev_ops stmf_ops = {
263 263 DEVO_REV,
264 264 0,
265 265 stmf_getinfo,
266 266 nulldev, /* identify */
267 267 nulldev, /* probe */
268 268 stmf_attach,
269 269 stmf_detach,
270 270 nodev, /* reset */
271 271 &stmf_cb_ops,
272 272 NULL, /* bus_ops */
273 273 NULL /* power */
274 274 };
275 275
276 276 #define STMF_NAME "COMSTAR STMF"
↓ open down ↓ |
276 lines elided |
↑ open up ↑ |
277 277 #define STMF_MODULE_NAME "stmf"
278 278
279 279 static struct modldrv modldrv = {
280 280 &mod_driverops,
281 281 STMF_NAME,
282 282 &stmf_ops
283 283 };
284 284
285 285 static struct modlinkage modlinkage = {
286 286 MODREV_1,
287 - &modldrv,
288 - NULL
287 + { &modldrv, NULL }
289 288 };
290 289
291 290 int
292 291 _init(void)
293 292 {
294 293 int ret;
295 294
296 295 ret = mod_install(&modlinkage);
297 296 if (ret)
298 297 return (ret);
299 298 stmf_trace_buf = kmem_zalloc(stmf_trace_buf_size, KM_SLEEP);
300 299 trace_buf_size = stmf_trace_buf_size;
301 300 trace_buf_curndx = 0;
302 301 mutex_init(&trace_buf_lock, NULL, MUTEX_DRIVER, 0);
303 302 bzero(&stmf_state, sizeof (stmf_state_t));
304 303 /* STMF service is off by default */
305 304 stmf_state.stmf_service_running = 0;
306 305 /* default lu/lport states are online */
307 306 stmf_state.stmf_default_lu_state = STMF_STATE_ONLINE;
308 307 stmf_state.stmf_default_lport_state = STMF_STATE_ONLINE;
309 308 mutex_init(&stmf_state.stmf_lock, NULL, MUTEX_DRIVER, NULL);
310 309 cv_init(&stmf_state.stmf_cv, NULL, CV_DRIVER, NULL);
311 310 stmf_session_counter = (uint64_t)ddi_get_lbolt();
312 311 avl_create(&stmf_state.stmf_irportlist,
313 312 stmf_irport_compare, sizeof (stmf_i_remote_port_t),
314 313 offsetof(stmf_i_remote_port_t, irport_ln));
315 314 stmf_state.stmf_ilport_inst_space =
316 315 id_space_create("lport-instances", 0, MAX_ILPORT);
317 316 stmf_state.stmf_irport_inst_space =
318 317 id_space_create("rport-instances", 0, MAX_IRPORT);
319 318 stmf_view_init();
320 319 stmf_svc_init();
321 320 stmf_dlun_init();
322 321 return (ret);
323 322 }
324 323
325 324 int
326 325 _fini(void)
327 326 {
328 327 int ret;
329 328 stmf_i_remote_port_t *irport;
330 329 void *avl_dest_cookie = NULL;
331 330
332 331 if (stmf_state.stmf_service_running)
333 332 return (EBUSY);
334 333 if ((!stmf_allow_modunload) &&
335 334 (stmf_state.stmf_config_state != STMF_CONFIG_NONE)) {
336 335 return (EBUSY);
337 336 }
338 337 if (stmf_state.stmf_nlps || stmf_state.stmf_npps) {
339 338 return (EBUSY);
340 339 }
341 340 if (stmf_dlun_fini() != STMF_SUCCESS)
342 341 return (EBUSY);
343 342 if (stmf_worker_fini() != STMF_SUCCESS) {
344 343 stmf_dlun_init();
345 344 return (EBUSY);
346 345 }
347 346 if (stmf_svc_fini() != STMF_SUCCESS) {
348 347 stmf_dlun_init();
349 348 stmf_worker_init();
350 349 return (EBUSY);
351 350 }
352 351
353 352 ret = mod_remove(&modlinkage);
354 353 if (ret) {
355 354 stmf_svc_init();
356 355 stmf_dlun_init();
357 356 stmf_worker_init();
358 357 return (ret);
359 358 }
360 359
361 360 stmf_view_clear_config();
362 361
363 362 while ((irport = avl_destroy_nodes(&stmf_state.stmf_irportlist,
364 363 &avl_dest_cookie)) != NULL)
365 364 stmf_irport_destroy(irport);
366 365 avl_destroy(&stmf_state.stmf_irportlist);
367 366 id_space_destroy(stmf_state.stmf_ilport_inst_space);
368 367 id_space_destroy(stmf_state.stmf_irport_inst_space);
369 368
370 369 kmem_free(stmf_trace_buf, stmf_trace_buf_size);
371 370 mutex_destroy(&trace_buf_lock);
372 371 mutex_destroy(&stmf_state.stmf_lock);
373 372 cv_destroy(&stmf_state.stmf_cv);
374 373 return (ret);
375 374 }
376 375
377 376 int
378 377 _info(struct modinfo *modinfop)
379 378 {
380 379 return (mod_info(&modlinkage, modinfop));
381 380 }
382 381
383 382 /* ARGSUSED */
384 383 static int
385 384 stmf_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
386 385 {
387 386 switch (cmd) {
388 387 case DDI_INFO_DEVT2DEVINFO:
389 388 *result = stmf_state.stmf_dip;
390 389 break;
391 390 case DDI_INFO_DEVT2INSTANCE:
392 391 *result =
393 392 (void *)(uintptr_t)ddi_get_instance(stmf_state.stmf_dip);
394 393 break;
395 394 default:
396 395 return (DDI_FAILURE);
397 396 }
398 397
399 398 return (DDI_SUCCESS);
400 399 }
401 400
402 401 static int
403 402 stmf_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
404 403 {
405 404 switch (cmd) {
406 405 case DDI_ATTACH:
407 406 stmf_state.stmf_dip = dip;
408 407
409 408 if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0,
410 409 DDI_NT_STMF, 0) != DDI_SUCCESS) {
411 410 break;
412 411 }
413 412 ddi_report_dev(dip);
414 413 return (DDI_SUCCESS);
415 414 }
416 415
417 416 return (DDI_FAILURE);
418 417 }
419 418
420 419 static int
421 420 stmf_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
422 421 {
423 422 switch (cmd) {
424 423 case DDI_DETACH:
425 424 ddi_remove_minor_node(dip, 0);
426 425 return (DDI_SUCCESS);
427 426 }
428 427
429 428 return (DDI_FAILURE);
430 429 }
431 430
432 431 /* ARGSUSED */
433 432 static int
434 433 stmf_open(dev_t *devp, int flag, int otype, cred_t *credp)
435 434 {
436 435 mutex_enter(&stmf_state.stmf_lock);
437 436 if (stmf_state.stmf_exclusive_open) {
438 437 mutex_exit(&stmf_state.stmf_lock);
439 438 return (EBUSY);
440 439 }
441 440 if (flag & FEXCL) {
442 441 if (stmf_state.stmf_opened) {
443 442 mutex_exit(&stmf_state.stmf_lock);
444 443 return (EBUSY);
445 444 }
446 445 stmf_state.stmf_exclusive_open = 1;
447 446 }
448 447 stmf_state.stmf_opened = 1;
449 448 mutex_exit(&stmf_state.stmf_lock);
450 449 return (0);
451 450 }
452 451
453 452 /* ARGSUSED */
454 453 static int
455 454 stmf_close(dev_t dev, int flag, int otype, cred_t *credp)
456 455 {
457 456 mutex_enter(&stmf_state.stmf_lock);
458 457 stmf_state.stmf_opened = 0;
459 458 if (stmf_state.stmf_exclusive_open &&
460 459 (stmf_state.stmf_config_state != STMF_CONFIG_INIT_DONE)) {
461 460 stmf_state.stmf_config_state = STMF_CONFIG_NONE;
462 461 stmf_delete_all_ppds();
463 462 stmf_view_clear_config();
464 463 stmf_view_init();
465 464 }
466 465 stmf_state.stmf_exclusive_open = 0;
467 466 mutex_exit(&stmf_state.stmf_lock);
468 467 return (0);
469 468 }
470 469
471 470 int
472 471 stmf_copyin_iocdata(intptr_t data, int mode, stmf_iocdata_t **iocd,
473 472 void **ibuf, void **obuf)
474 473 {
475 474 int ret;
476 475
477 476 *ibuf = NULL;
478 477 *obuf = NULL;
479 478 *iocd = kmem_zalloc(sizeof (stmf_iocdata_t), KM_SLEEP);
480 479
481 480 ret = ddi_copyin((void *)data, *iocd, sizeof (stmf_iocdata_t), mode);
482 481 if (ret)
483 482 return (EFAULT);
484 483 if ((*iocd)->stmf_version != STMF_VERSION_1) {
485 484 ret = EINVAL;
486 485 goto copyin_iocdata_done;
487 486 }
488 487 if ((*iocd)->stmf_ibuf_size) {
489 488 *ibuf = kmem_zalloc((*iocd)->stmf_ibuf_size, KM_SLEEP);
490 489 ret = ddi_copyin((void *)((unsigned long)(*iocd)->stmf_ibuf),
491 490 *ibuf, (*iocd)->stmf_ibuf_size, mode);
492 491 }
493 492 if ((*iocd)->stmf_obuf_size)
494 493 *obuf = kmem_zalloc((*iocd)->stmf_obuf_size, KM_SLEEP);
495 494
496 495 if (ret == 0)
497 496 return (0);
498 497 ret = EFAULT;
499 498 copyin_iocdata_done:;
500 499 if (*obuf) {
501 500 kmem_free(*obuf, (*iocd)->stmf_obuf_size);
502 501 *obuf = NULL;
503 502 }
504 503 if (*ibuf) {
505 504 kmem_free(*ibuf, (*iocd)->stmf_ibuf_size);
506 505 *ibuf = NULL;
507 506 }
508 507 kmem_free(*iocd, sizeof (stmf_iocdata_t));
509 508 return (ret);
510 509 }
511 510
512 511 int
513 512 stmf_copyout_iocdata(intptr_t data, int mode, stmf_iocdata_t *iocd, void *obuf)
514 513 {
515 514 int ret;
516 515
517 516 if (iocd->stmf_obuf_size) {
518 517 ret = ddi_copyout(obuf, (void *)(unsigned long)iocd->stmf_obuf,
519 518 iocd->stmf_obuf_size, mode);
520 519 if (ret)
521 520 return (EFAULT);
522 521 }
523 522 ret = ddi_copyout(iocd, (void *)data, sizeof (stmf_iocdata_t), mode);
524 523 if (ret)
525 524 return (EFAULT);
526 525 return (0);
527 526 }
528 527
529 528 /* ARGSUSED */
530 529 static int
531 530 stmf_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
532 531 cred_t *credp, int *rval)
533 532 {
534 533 stmf_iocdata_t *iocd;
535 534 void *ibuf = NULL, *obuf = NULL;
536 535 slist_lu_t *luid_list;
537 536 slist_target_port_t *lportid_list;
538 537 stmf_i_lu_t *ilu;
539 538 stmf_i_local_port_t *ilport;
540 539 stmf_i_scsi_session_t *iss;
541 540 slist_scsi_session_t *iss_list;
542 541 sioc_lu_props_t *lup;
543 542 sioc_target_port_props_t *lportp;
544 543 stmf_ppioctl_data_t *ppi, *ppi_out = NULL;
545 544 uint64_t *ppi_token = NULL;
546 545 uint8_t *p_id, *id;
547 546 stmf_state_desc_t *std;
548 547 stmf_status_t ctl_ret;
549 548 stmf_state_change_info_t ssi;
550 549 int ret = 0;
551 550 uint32_t n;
552 551 int i;
553 552 stmf_group_op_data_t *grp_entry;
554 553 stmf_group_name_t *grpname;
555 554 stmf_view_op_entry_t *ve;
556 555 stmf_id_type_t idtype;
557 556 stmf_id_data_t *id_entry;
558 557 stmf_id_list_t *id_list;
559 558 stmf_view_entry_t *view_entry;
560 559 stmf_set_props_t *stmf_set_props;
561 560 uint32_t veid;
562 561 if ((cmd & 0xff000000) != STMF_IOCTL) {
563 562 return (ENOTTY);
564 563 }
565 564
566 565 if (drv_priv(credp) != 0) {
567 566 return (EPERM);
568 567 }
569 568
570 569 ret = stmf_copyin_iocdata(data, mode, &iocd, &ibuf, &obuf);
571 570 if (ret)
572 571 return (ret);
573 572 iocd->stmf_error = 0;
574 573
575 574 switch (cmd) {
576 575 case STMF_IOCTL_LU_LIST:
577 576 /* retrieves both registered/unregistered */
578 577 mutex_enter(&stmf_state.stmf_lock);
579 578 id_list = &stmf_state.stmf_luid_list;
580 579 n = min(id_list->id_count,
581 580 (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
582 581 iocd->stmf_obuf_max_nentries = id_list->id_count;
583 582 luid_list = (slist_lu_t *)obuf;
584 583 id_entry = id_list->idl_head;
585 584 for (i = 0; i < n; i++) {
586 585 bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
587 586 id_entry = id_entry->id_next;
588 587 }
589 588
590 589 n = iocd->stmf_obuf_size/sizeof (slist_lu_t);
591 590 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
592 591 id = (uint8_t *)ilu->ilu_lu->lu_id;
593 592 if (stmf_lookup_id(id_list, 16, id + 4) == NULL) {
594 593 iocd->stmf_obuf_max_nentries++;
595 594 if (i < n) {
596 595 bcopy(id + 4, luid_list[i].lu_guid,
597 596 sizeof (slist_lu_t));
598 597 i++;
599 598 }
600 599 }
601 600 }
602 601 iocd->stmf_obuf_nentries = i;
603 602 mutex_exit(&stmf_state.stmf_lock);
604 603 break;
605 604
606 605 case STMF_IOCTL_REG_LU_LIST:
607 606 mutex_enter(&stmf_state.stmf_lock);
608 607 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlus;
609 608 n = min(stmf_state.stmf_nlus,
610 609 (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
611 610 iocd->stmf_obuf_nentries = n;
612 611 ilu = stmf_state.stmf_ilulist;
613 612 luid_list = (slist_lu_t *)obuf;
614 613 for (i = 0; i < n; i++) {
615 614 uint8_t *id;
616 615 id = (uint8_t *)ilu->ilu_lu->lu_id;
617 616 bcopy(id + 4, luid_list[i].lu_guid, 16);
618 617 ilu = ilu->ilu_next;
619 618 }
620 619 mutex_exit(&stmf_state.stmf_lock);
621 620 break;
622 621
623 622 case STMF_IOCTL_VE_LU_LIST:
624 623 mutex_enter(&stmf_state.stmf_lock);
625 624 id_list = &stmf_state.stmf_luid_list;
626 625 n = min(id_list->id_count,
627 626 (iocd->stmf_obuf_size)/sizeof (slist_lu_t));
628 627 iocd->stmf_obuf_max_nentries = id_list->id_count;
629 628 iocd->stmf_obuf_nentries = n;
630 629 luid_list = (slist_lu_t *)obuf;
631 630 id_entry = id_list->idl_head;
632 631 for (i = 0; i < n; i++) {
633 632 bcopy(id_entry->id_data, luid_list[i].lu_guid, 16);
634 633 id_entry = id_entry->id_next;
635 634 }
636 635 mutex_exit(&stmf_state.stmf_lock);
637 636 break;
638 637
639 638 case STMF_IOCTL_TARGET_PORT_LIST:
640 639 mutex_enter(&stmf_state.stmf_lock);
641 640 iocd->stmf_obuf_max_nentries = stmf_state.stmf_nlports;
642 641 n = min(stmf_state.stmf_nlports,
643 642 (iocd->stmf_obuf_size)/sizeof (slist_target_port_t));
644 643 iocd->stmf_obuf_nentries = n;
645 644 ilport = stmf_state.stmf_ilportlist;
646 645 lportid_list = (slist_target_port_t *)obuf;
647 646 for (i = 0; i < n; i++) {
648 647 uint8_t *id;
649 648 id = (uint8_t *)ilport->ilport_lport->lport_id;
650 649 bcopy(id, lportid_list[i].target, id[3] + 4);
651 650 ilport = ilport->ilport_next;
652 651 }
653 652 mutex_exit(&stmf_state.stmf_lock);
654 653 break;
655 654
656 655 case STMF_IOCTL_SESSION_LIST:
657 656 p_id = (uint8_t *)ibuf;
658 657 if ((p_id == NULL) || (iocd->stmf_ibuf_size < 4) ||
659 658 (iocd->stmf_ibuf_size < (p_id[3] + 4))) {
660 659 ret = EINVAL;
661 660 break;
662 661 }
663 662 mutex_enter(&stmf_state.stmf_lock);
664 663 for (ilport = stmf_state.stmf_ilportlist; ilport; ilport =
665 664 ilport->ilport_next) {
666 665 uint8_t *id;
667 666 id = (uint8_t *)ilport->ilport_lport->lport_id;
668 667 if ((p_id[3] == id[3]) &&
669 668 (bcmp(p_id + 4, id + 4, id[3]) == 0)) {
670 669 break;
671 670 }
672 671 }
673 672 if (ilport == NULL) {
674 673 mutex_exit(&stmf_state.stmf_lock);
675 674 ret = ENOENT;
676 675 break;
677 676 }
678 677 iocd->stmf_obuf_max_nentries = ilport->ilport_nsessions;
679 678 n = min(ilport->ilport_nsessions,
680 679 (iocd->stmf_obuf_size)/sizeof (slist_scsi_session_t));
681 680 iocd->stmf_obuf_nentries = n;
682 681 iss = ilport->ilport_ss_list;
683 682 iss_list = (slist_scsi_session_t *)obuf;
684 683 for (i = 0; i < n; i++) {
685 684 uint8_t *id;
686 685 id = (uint8_t *)iss->iss_ss->ss_rport_id;
687 686 bcopy(id, iss_list[i].initiator, id[3] + 4);
688 687 iss_list[i].creation_time = (uint32_t)
689 688 iss->iss_creation_time;
690 689 if (iss->iss_ss->ss_rport_alias) {
691 690 (void) strncpy(iss_list[i].alias,
692 691 iss->iss_ss->ss_rport_alias, 255);
693 692 iss_list[i].alias[255] = 0;
694 693 } else {
695 694 iss_list[i].alias[0] = 0;
696 695 }
697 696 iss = iss->iss_next;
698 697 }
699 698 mutex_exit(&stmf_state.stmf_lock);
700 699 break;
701 700
702 701 case STMF_IOCTL_GET_LU_PROPERTIES:
703 702 p_id = (uint8_t *)ibuf;
704 703 if ((iocd->stmf_ibuf_size < 16) ||
705 704 (iocd->stmf_obuf_size < sizeof (sioc_lu_props_t)) ||
706 705 (p_id[0] == 0)) {
707 706 ret = EINVAL;
708 707 break;
709 708 }
710 709 mutex_enter(&stmf_state.stmf_lock);
711 710 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
712 711 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
713 712 break;
714 713 }
715 714 if (ilu == NULL) {
716 715 mutex_exit(&stmf_state.stmf_lock);
717 716 ret = ENOENT;
718 717 break;
719 718 }
720 719 lup = (sioc_lu_props_t *)obuf;
721 720 bcopy(ilu->ilu_lu->lu_id->ident, lup->lu_guid, 16);
722 721 lup->lu_state = ilu->ilu_state & 0x0f;
723 722 lup->lu_present = 1; /* XXX */
724 723 (void) strncpy(lup->lu_provider_name,
725 724 ilu->ilu_lu->lu_lp->lp_name, 255);
726 725 lup->lu_provider_name[254] = 0;
727 726 if (ilu->ilu_lu->lu_alias) {
728 727 (void) strncpy(lup->lu_alias,
729 728 ilu->ilu_lu->lu_alias, 255);
730 729 lup->lu_alias[255] = 0;
731 730 } else {
732 731 lup->lu_alias[0] = 0;
733 732 }
734 733 mutex_exit(&stmf_state.stmf_lock);
735 734 break;
736 735
737 736 case STMF_IOCTL_GET_TARGET_PORT_PROPERTIES:
738 737 p_id = (uint8_t *)ibuf;
739 738 if ((p_id == NULL) ||
740 739 (iocd->stmf_ibuf_size < (p_id[3] + 4)) ||
741 740 (iocd->stmf_obuf_size <
742 741 sizeof (sioc_target_port_props_t))) {
743 742 ret = EINVAL;
744 743 break;
745 744 }
746 745 mutex_enter(&stmf_state.stmf_lock);
747 746 for (ilport = stmf_state.stmf_ilportlist; ilport;
748 747 ilport = ilport->ilport_next) {
749 748 uint8_t *id;
750 749 id = (uint8_t *)ilport->ilport_lport->lport_id;
751 750 if ((p_id[3] == id[3]) &&
752 751 (bcmp(p_id+4, id+4, id[3]) == 0))
753 752 break;
754 753 }
755 754 if (ilport == NULL) {
756 755 mutex_exit(&stmf_state.stmf_lock);
757 756 ret = ENOENT;
758 757 break;
759 758 }
760 759 lportp = (sioc_target_port_props_t *)obuf;
761 760 bcopy(ilport->ilport_lport->lport_id, lportp->tgt_id,
762 761 ilport->ilport_lport->lport_id->ident_length + 4);
763 762 lportp->tgt_state = ilport->ilport_state & 0x0f;
764 763 lportp->tgt_present = 1; /* XXX */
765 764 (void) strncpy(lportp->tgt_provider_name,
766 765 ilport->ilport_lport->lport_pp->pp_name, 255);
767 766 lportp->tgt_provider_name[254] = 0;
768 767 if (ilport->ilport_lport->lport_alias) {
769 768 (void) strncpy(lportp->tgt_alias,
770 769 ilport->ilport_lport->lport_alias, 255);
771 770 lportp->tgt_alias[255] = 0;
772 771 } else {
773 772 lportp->tgt_alias[0] = 0;
774 773 }
775 774 mutex_exit(&stmf_state.stmf_lock);
776 775 break;
777 776
778 777 case STMF_IOCTL_SET_STMF_STATE:
779 778 if ((ibuf == NULL) ||
780 779 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
781 780 ret = EINVAL;
782 781 break;
783 782 }
784 783 ret = stmf_set_stmf_state((stmf_state_desc_t *)ibuf);
785 784 break;
786 785
787 786 case STMF_IOCTL_GET_STMF_STATE:
788 787 if ((obuf == NULL) ||
789 788 (iocd->stmf_obuf_size < sizeof (stmf_state_desc_t))) {
790 789 ret = EINVAL;
791 790 break;
792 791 }
793 792 ret = stmf_get_stmf_state((stmf_state_desc_t *)obuf);
794 793 break;
795 794
796 795 case STMF_IOCTL_SET_ALUA_STATE:
797 796 if ((ibuf == NULL) ||
798 797 (iocd->stmf_ibuf_size < sizeof (stmf_alua_state_desc_t))) {
799 798 ret = EINVAL;
800 799 break;
801 800 }
802 801 ret = stmf_set_alua_state((stmf_alua_state_desc_t *)ibuf);
803 802 break;
804 803
805 804 case STMF_IOCTL_GET_ALUA_STATE:
806 805 if ((obuf == NULL) ||
807 806 (iocd->stmf_obuf_size < sizeof (stmf_alua_state_desc_t))) {
808 807 ret = EINVAL;
809 808 break;
810 809 }
811 810 stmf_get_alua_state((stmf_alua_state_desc_t *)obuf);
812 811 break;
813 812
814 813 case STMF_IOCTL_SET_LU_STATE:
815 814 ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
816 815 ssi.st_additional_info = NULL;
817 816 std = (stmf_state_desc_t *)ibuf;
818 817 if ((ibuf == NULL) ||
819 818 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
820 819 ret = EINVAL;
821 820 break;
822 821 }
823 822 p_id = std->ident;
824 823 mutex_enter(&stmf_state.stmf_lock);
825 824 if (stmf_state.stmf_inventory_locked) {
826 825 mutex_exit(&stmf_state.stmf_lock);
827 826 ret = EBUSY;
828 827 break;
829 828 }
830 829 for (ilu = stmf_state.stmf_ilulist; ilu; ilu = ilu->ilu_next) {
831 830 if (bcmp(p_id, ilu->ilu_lu->lu_id->ident, 16) == 0)
832 831 break;
833 832 }
834 833 if (ilu == NULL) {
835 834 mutex_exit(&stmf_state.stmf_lock);
836 835 ret = ENOENT;
837 836 break;
838 837 }
839 838 stmf_state.stmf_inventory_locked = 1;
840 839 mutex_exit(&stmf_state.stmf_lock);
841 840 cmd = (std->state == STMF_STATE_ONLINE) ? STMF_CMD_LU_ONLINE :
842 841 STMF_CMD_LU_OFFLINE;
843 842 ctl_ret = stmf_ctl(cmd, (void *)ilu->ilu_lu, &ssi);
844 843 if (ctl_ret == STMF_ALREADY)
845 844 ret = 0;
846 845 else if (ctl_ret == STMF_BUSY)
847 846 ret = EBUSY;
848 847 else if (ctl_ret != STMF_SUCCESS)
849 848 ret = EIO;
850 849 mutex_enter(&stmf_state.stmf_lock);
851 850 stmf_state.stmf_inventory_locked = 0;
852 851 mutex_exit(&stmf_state.stmf_lock);
853 852 break;
854 853
855 854 case STMF_IOCTL_SET_STMF_PROPS:
856 855 if ((ibuf == NULL) ||
857 856 (iocd->stmf_ibuf_size < sizeof (stmf_set_props_t))) {
858 857 ret = EINVAL;
859 858 break;
860 859 }
861 860 stmf_set_props = (stmf_set_props_t *)ibuf;
862 861 mutex_enter(&stmf_state.stmf_lock);
863 862 if ((stmf_set_props->default_lu_state_value ==
864 863 STMF_STATE_OFFLINE) ||
865 864 (stmf_set_props->default_lu_state_value ==
866 865 STMF_STATE_ONLINE)) {
867 866 stmf_state.stmf_default_lu_state =
868 867 stmf_set_props->default_lu_state_value;
869 868 }
870 869 if ((stmf_set_props->default_target_state_value ==
871 870 STMF_STATE_OFFLINE) ||
872 871 (stmf_set_props->default_target_state_value ==
873 872 STMF_STATE_ONLINE)) {
874 873 stmf_state.stmf_default_lport_state =
875 874 stmf_set_props->default_target_state_value;
876 875 }
877 876
878 877 mutex_exit(&stmf_state.stmf_lock);
879 878 break;
880 879
881 880 case STMF_IOCTL_SET_TARGET_PORT_STATE:
882 881 ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
883 882 ssi.st_additional_info = NULL;
884 883 std = (stmf_state_desc_t *)ibuf;
885 884 if ((ibuf == NULL) ||
886 885 (iocd->stmf_ibuf_size < sizeof (stmf_state_desc_t))) {
887 886 ret = EINVAL;
888 887 break;
889 888 }
890 889 p_id = std->ident;
891 890 mutex_enter(&stmf_state.stmf_lock);
892 891 if (stmf_state.stmf_inventory_locked) {
893 892 mutex_exit(&stmf_state.stmf_lock);
894 893 ret = EBUSY;
895 894 break;
896 895 }
897 896 for (ilport = stmf_state.stmf_ilportlist; ilport;
898 897 ilport = ilport->ilport_next) {
899 898 uint8_t *id;
900 899 id = (uint8_t *)ilport->ilport_lport->lport_id;
901 900 if ((id[3] == p_id[3]) &&
902 901 (bcmp(id+4, p_id+4, id[3]) == 0)) {
903 902 break;
904 903 }
905 904 }
906 905 if (ilport == NULL) {
907 906 mutex_exit(&stmf_state.stmf_lock);
908 907 ret = ENOENT;
909 908 break;
910 909 }
911 910 stmf_state.stmf_inventory_locked = 1;
912 911 mutex_exit(&stmf_state.stmf_lock);
913 912 cmd = (std->state == STMF_STATE_ONLINE) ?
914 913 STMF_CMD_LPORT_ONLINE : STMF_CMD_LPORT_OFFLINE;
915 914 ctl_ret = stmf_ctl(cmd, (void *)ilport->ilport_lport, &ssi);
916 915 if (ctl_ret == STMF_ALREADY)
917 916 ret = 0;
918 917 else if (ctl_ret == STMF_BUSY)
919 918 ret = EBUSY;
920 919 else if (ctl_ret != STMF_SUCCESS)
921 920 ret = EIO;
922 921 mutex_enter(&stmf_state.stmf_lock);
923 922 stmf_state.stmf_inventory_locked = 0;
924 923 mutex_exit(&stmf_state.stmf_lock);
925 924 break;
926 925
927 926 case STMF_IOCTL_ADD_HG_ENTRY:
928 927 idtype = STMF_ID_TYPE_HOST;
929 928 /* FALLTHROUGH */
930 929 case STMF_IOCTL_ADD_TG_ENTRY:
931 930 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
932 931 ret = EACCES;
933 932 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
934 933 break;
935 934 }
936 935 if (cmd == STMF_IOCTL_ADD_TG_ENTRY) {
937 936 idtype = STMF_ID_TYPE_TARGET;
938 937 }
939 938 grp_entry = (stmf_group_op_data_t *)ibuf;
940 939 if ((ibuf == NULL) ||
941 940 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
942 941 ret = EINVAL;
943 942 break;
944 943 }
945 944 if (grp_entry->group.name[0] == '*') {
946 945 ret = EINVAL;
947 946 break; /* not allowed */
948 947 }
949 948 mutex_enter(&stmf_state.stmf_lock);
950 949 ret = stmf_add_group_member(grp_entry->group.name,
951 950 grp_entry->group.name_size,
952 951 grp_entry->ident + 4,
953 952 grp_entry->ident[3],
954 953 idtype,
955 954 &iocd->stmf_error);
956 955 mutex_exit(&stmf_state.stmf_lock);
957 956 break;
958 957 case STMF_IOCTL_REMOVE_HG_ENTRY:
959 958 idtype = STMF_ID_TYPE_HOST;
960 959 /* FALLTHROUGH */
961 960 case STMF_IOCTL_REMOVE_TG_ENTRY:
962 961 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
963 962 ret = EACCES;
964 963 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
965 964 break;
966 965 }
967 966 if (cmd == STMF_IOCTL_REMOVE_TG_ENTRY) {
968 967 idtype = STMF_ID_TYPE_TARGET;
969 968 }
970 969 grp_entry = (stmf_group_op_data_t *)ibuf;
971 970 if ((ibuf == NULL) ||
972 971 (iocd->stmf_ibuf_size < sizeof (stmf_group_op_data_t))) {
973 972 ret = EINVAL;
974 973 break;
975 974 }
976 975 if (grp_entry->group.name[0] == '*') {
977 976 ret = EINVAL;
978 977 break; /* not allowed */
979 978 }
980 979 mutex_enter(&stmf_state.stmf_lock);
981 980 ret = stmf_remove_group_member(grp_entry->group.name,
982 981 grp_entry->group.name_size,
983 982 grp_entry->ident + 4,
984 983 grp_entry->ident[3],
985 984 idtype,
986 985 &iocd->stmf_error);
987 986 mutex_exit(&stmf_state.stmf_lock);
988 987 break;
989 988 case STMF_IOCTL_CREATE_HOST_GROUP:
990 989 idtype = STMF_ID_TYPE_HOST_GROUP;
991 990 /* FALLTHROUGH */
992 991 case STMF_IOCTL_CREATE_TARGET_GROUP:
993 992 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
994 993 ret = EACCES;
995 994 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
996 995 break;
997 996 }
998 997 grpname = (stmf_group_name_t *)ibuf;
999 998
1000 999 if (cmd == STMF_IOCTL_CREATE_TARGET_GROUP)
1001 1000 idtype = STMF_ID_TYPE_TARGET_GROUP;
1002 1001 if ((ibuf == NULL) ||
1003 1002 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1004 1003 ret = EINVAL;
1005 1004 break;
1006 1005 }
1007 1006 if (grpname->name[0] == '*') {
1008 1007 ret = EINVAL;
1009 1008 break; /* not allowed */
1010 1009 }
1011 1010 mutex_enter(&stmf_state.stmf_lock);
1012 1011 ret = stmf_add_group(grpname->name,
1013 1012 grpname->name_size, idtype, &iocd->stmf_error);
1014 1013 mutex_exit(&stmf_state.stmf_lock);
1015 1014 break;
1016 1015 case STMF_IOCTL_REMOVE_HOST_GROUP:
1017 1016 idtype = STMF_ID_TYPE_HOST_GROUP;
1018 1017 /* FALLTHROUGH */
1019 1018 case STMF_IOCTL_REMOVE_TARGET_GROUP:
1020 1019 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1021 1020 ret = EACCES;
1022 1021 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1023 1022 break;
1024 1023 }
1025 1024 grpname = (stmf_group_name_t *)ibuf;
1026 1025 if (cmd == STMF_IOCTL_REMOVE_TARGET_GROUP)
1027 1026 idtype = STMF_ID_TYPE_TARGET_GROUP;
1028 1027 if ((ibuf == NULL) ||
1029 1028 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1030 1029 ret = EINVAL;
1031 1030 break;
1032 1031 }
1033 1032 if (grpname->name[0] == '*') {
1034 1033 ret = EINVAL;
1035 1034 break; /* not allowed */
1036 1035 }
1037 1036 mutex_enter(&stmf_state.stmf_lock);
1038 1037 ret = stmf_remove_group(grpname->name,
1039 1038 grpname->name_size, idtype, &iocd->stmf_error);
1040 1039 mutex_exit(&stmf_state.stmf_lock);
1041 1040 break;
1042 1041 case STMF_IOCTL_VALIDATE_VIEW:
1043 1042 case STMF_IOCTL_ADD_VIEW_ENTRY:
1044 1043 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1045 1044 ret = EACCES;
1046 1045 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1047 1046 break;
1048 1047 }
1049 1048 ve = (stmf_view_op_entry_t *)ibuf;
1050 1049 if ((ibuf == NULL) ||
1051 1050 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
1052 1051 ret = EINVAL;
1053 1052 break;
1054 1053 }
1055 1054 if (!ve->ve_lu_number_valid)
1056 1055 ve->ve_lu_nbr[2] = 0xFF;
1057 1056 if (ve->ve_all_hosts) {
1058 1057 ve->ve_host_group.name[0] = '*';
1059 1058 ve->ve_host_group.name_size = 1;
1060 1059 }
1061 1060 if (ve->ve_all_targets) {
1062 1061 ve->ve_target_group.name[0] = '*';
1063 1062 ve->ve_target_group.name_size = 1;
1064 1063 }
1065 1064 if (ve->ve_ndx_valid)
1066 1065 veid = ve->ve_ndx;
1067 1066 else
1068 1067 veid = 0xffffffff;
1069 1068 mutex_enter(&stmf_state.stmf_lock);
1070 1069 if (cmd == STMF_IOCTL_ADD_VIEW_ENTRY) {
1071 1070 ret = stmf_add_ve(ve->ve_host_group.name,
1072 1071 ve->ve_host_group.name_size,
1073 1072 ve->ve_target_group.name,
1074 1073 ve->ve_target_group.name_size,
1075 1074 ve->ve_guid,
1076 1075 &veid,
1077 1076 ve->ve_lu_nbr,
1078 1077 &iocd->stmf_error);
1079 1078 } else { /* STMF_IOCTL_VALIDATE_VIEW */
1080 1079 ret = stmf_validate_lun_ve(ve->ve_host_group.name,
1081 1080 ve->ve_host_group.name_size,
1082 1081 ve->ve_target_group.name,
1083 1082 ve->ve_target_group.name_size,
1084 1083 ve->ve_lu_nbr,
1085 1084 &iocd->stmf_error);
1086 1085 }
1087 1086 mutex_exit(&stmf_state.stmf_lock);
1088 1087 if (ret == 0 &&
1089 1088 (!ve->ve_ndx_valid || !ve->ve_lu_number_valid) &&
1090 1089 iocd->stmf_obuf_size >= sizeof (stmf_view_op_entry_t)) {
1091 1090 stmf_view_op_entry_t *ve_ret =
1092 1091 (stmf_view_op_entry_t *)obuf;
1093 1092 iocd->stmf_obuf_nentries = 1;
1094 1093 iocd->stmf_obuf_max_nentries = 1;
1095 1094 if (!ve->ve_ndx_valid) {
1096 1095 ve_ret->ve_ndx = veid;
1097 1096 ve_ret->ve_ndx_valid = 1;
1098 1097 }
1099 1098 if (!ve->ve_lu_number_valid) {
1100 1099 ve_ret->ve_lu_number_valid = 1;
1101 1100 bcopy(ve->ve_lu_nbr, ve_ret->ve_lu_nbr, 8);
1102 1101 }
1103 1102 }
1104 1103 break;
1105 1104 case STMF_IOCTL_REMOVE_VIEW_ENTRY:
1106 1105 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1107 1106 ret = EACCES;
1108 1107 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1109 1108 break;
1110 1109 }
1111 1110 ve = (stmf_view_op_entry_t *)ibuf;
1112 1111 if ((ibuf == NULL) ||
1113 1112 (iocd->stmf_ibuf_size < sizeof (stmf_view_op_entry_t))) {
1114 1113 ret = EINVAL;
1115 1114 break;
1116 1115 }
1117 1116 if (!ve->ve_ndx_valid) {
1118 1117 ret = EINVAL;
1119 1118 break;
1120 1119 }
1121 1120 mutex_enter(&stmf_state.stmf_lock);
1122 1121 ret = stmf_remove_ve_by_id(ve->ve_guid, ve->ve_ndx,
1123 1122 &iocd->stmf_error);
1124 1123 mutex_exit(&stmf_state.stmf_lock);
1125 1124 break;
1126 1125 case STMF_IOCTL_GET_HG_LIST:
1127 1126 id_list = &stmf_state.stmf_hg_list;
1128 1127 /* FALLTHROUGH */
1129 1128 case STMF_IOCTL_GET_TG_LIST:
1130 1129 if (cmd == STMF_IOCTL_GET_TG_LIST)
1131 1130 id_list = &stmf_state.stmf_tg_list;
1132 1131 mutex_enter(&stmf_state.stmf_lock);
1133 1132 iocd->stmf_obuf_max_nentries = id_list->id_count;
1134 1133 n = min(id_list->id_count,
1135 1134 (iocd->stmf_obuf_size)/sizeof (stmf_group_name_t));
1136 1135 iocd->stmf_obuf_nentries = n;
1137 1136 id_entry = id_list->idl_head;
1138 1137 grpname = (stmf_group_name_t *)obuf;
1139 1138 for (i = 0; i < n; i++) {
1140 1139 if (id_entry->id_data[0] == '*') {
1141 1140 if (iocd->stmf_obuf_nentries > 0) {
1142 1141 iocd->stmf_obuf_nentries--;
1143 1142 }
1144 1143 id_entry = id_entry->id_next;
1145 1144 continue;
1146 1145 }
1147 1146 grpname->name_size = id_entry->id_data_size;
1148 1147 bcopy(id_entry->id_data, grpname->name,
1149 1148 id_entry->id_data_size);
1150 1149 grpname++;
1151 1150 id_entry = id_entry->id_next;
1152 1151 }
1153 1152 mutex_exit(&stmf_state.stmf_lock);
1154 1153 break;
1155 1154 case STMF_IOCTL_GET_HG_ENTRIES:
1156 1155 id_list = &stmf_state.stmf_hg_list;
1157 1156 /* FALLTHROUGH */
1158 1157 case STMF_IOCTL_GET_TG_ENTRIES:
1159 1158 grpname = (stmf_group_name_t *)ibuf;
1160 1159 if ((ibuf == NULL) ||
1161 1160 (iocd->stmf_ibuf_size < sizeof (stmf_group_name_t))) {
1162 1161 ret = EINVAL;
1163 1162 break;
1164 1163 }
1165 1164 if (cmd == STMF_IOCTL_GET_TG_ENTRIES) {
1166 1165 id_list = &stmf_state.stmf_tg_list;
1167 1166 }
1168 1167 mutex_enter(&stmf_state.stmf_lock);
1169 1168 id_entry = stmf_lookup_id(id_list, grpname->name_size,
1170 1169 grpname->name);
1171 1170 if (!id_entry)
1172 1171 ret = ENODEV;
1173 1172 else {
1174 1173 stmf_ge_ident_t *grp_entry;
1175 1174 id_list = (stmf_id_list_t *)id_entry->id_impl_specific;
1176 1175 iocd->stmf_obuf_max_nentries = id_list->id_count;
1177 1176 n = min(id_list->id_count,
1178 1177 iocd->stmf_obuf_size/sizeof (stmf_ge_ident_t));
1179 1178 iocd->stmf_obuf_nentries = n;
1180 1179 id_entry = id_list->idl_head;
1181 1180 grp_entry = (stmf_ge_ident_t *)obuf;
1182 1181 for (i = 0; i < n; i++) {
1183 1182 bcopy(id_entry->id_data, grp_entry->ident,
1184 1183 id_entry->id_data_size);
1185 1184 grp_entry->ident_size = id_entry->id_data_size;
1186 1185 id_entry = id_entry->id_next;
1187 1186 grp_entry++;
1188 1187 }
1189 1188 }
1190 1189 mutex_exit(&stmf_state.stmf_lock);
1191 1190 break;
1192 1191
1193 1192 case STMF_IOCTL_GET_VE_LIST:
1194 1193 n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1195 1194 mutex_enter(&stmf_state.stmf_lock);
1196 1195 ve = (stmf_view_op_entry_t *)obuf;
1197 1196 for (id_entry = stmf_state.stmf_luid_list.idl_head;
1198 1197 id_entry; id_entry = id_entry->id_next) {
1199 1198 for (view_entry = (stmf_view_entry_t *)
1200 1199 id_entry->id_impl_specific; view_entry;
1201 1200 view_entry = view_entry->ve_next) {
1202 1201 iocd->stmf_obuf_max_nentries++;
1203 1202 if (iocd->stmf_obuf_nentries >= n)
1204 1203 continue;
1205 1204 ve->ve_ndx_valid = 1;
1206 1205 ve->ve_ndx = view_entry->ve_id;
1207 1206 ve->ve_lu_number_valid = 1;
1208 1207 bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1209 1208 bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1210 1209 view_entry->ve_luid->id_data_size);
1211 1210 if (view_entry->ve_hg->id_data[0] == '*') {
1212 1211 ve->ve_all_hosts = 1;
1213 1212 } else {
1214 1213 bcopy(view_entry->ve_hg->id_data,
1215 1214 ve->ve_host_group.name,
1216 1215 view_entry->ve_hg->id_data_size);
1217 1216 ve->ve_host_group.name_size =
1218 1217 view_entry->ve_hg->id_data_size;
1219 1218 }
1220 1219
1221 1220 if (view_entry->ve_tg->id_data[0] == '*') {
1222 1221 ve->ve_all_targets = 1;
1223 1222 } else {
1224 1223 bcopy(view_entry->ve_tg->id_data,
1225 1224 ve->ve_target_group.name,
1226 1225 view_entry->ve_tg->id_data_size);
1227 1226 ve->ve_target_group.name_size =
1228 1227 view_entry->ve_tg->id_data_size;
1229 1228 }
1230 1229 ve++;
1231 1230 iocd->stmf_obuf_nentries++;
1232 1231 }
1233 1232 }
1234 1233 mutex_exit(&stmf_state.stmf_lock);
1235 1234 break;
1236 1235
1237 1236 case STMF_IOCTL_LU_VE_LIST:
1238 1237 p_id = (uint8_t *)ibuf;
1239 1238 if ((iocd->stmf_ibuf_size != 16) ||
1240 1239 (iocd->stmf_obuf_size < sizeof (stmf_view_op_entry_t))) {
1241 1240 ret = EINVAL;
1242 1241 break;
1243 1242 }
1244 1243
1245 1244 n = iocd->stmf_obuf_size/sizeof (stmf_view_op_entry_t);
1246 1245 mutex_enter(&stmf_state.stmf_lock);
1247 1246 ve = (stmf_view_op_entry_t *)obuf;
1248 1247 for (id_entry = stmf_state.stmf_luid_list.idl_head;
1249 1248 id_entry; id_entry = id_entry->id_next) {
1250 1249 if (bcmp(id_entry->id_data, p_id, 16) != 0)
1251 1250 continue;
1252 1251 for (view_entry = (stmf_view_entry_t *)
1253 1252 id_entry->id_impl_specific; view_entry;
1254 1253 view_entry = view_entry->ve_next) {
1255 1254 iocd->stmf_obuf_max_nentries++;
1256 1255 if (iocd->stmf_obuf_nentries >= n)
1257 1256 continue;
1258 1257 ve->ve_ndx_valid = 1;
1259 1258 ve->ve_ndx = view_entry->ve_id;
1260 1259 ve->ve_lu_number_valid = 1;
1261 1260 bcopy(view_entry->ve_lun, ve->ve_lu_nbr, 8);
1262 1261 bcopy(view_entry->ve_luid->id_data, ve->ve_guid,
1263 1262 view_entry->ve_luid->id_data_size);
1264 1263 if (view_entry->ve_hg->id_data[0] == '*') {
1265 1264 ve->ve_all_hosts = 1;
1266 1265 } else {
1267 1266 bcopy(view_entry->ve_hg->id_data,
1268 1267 ve->ve_host_group.name,
1269 1268 view_entry->ve_hg->id_data_size);
1270 1269 ve->ve_host_group.name_size =
1271 1270 view_entry->ve_hg->id_data_size;
1272 1271 }
1273 1272
1274 1273 if (view_entry->ve_tg->id_data[0] == '*') {
1275 1274 ve->ve_all_targets = 1;
1276 1275 } else {
1277 1276 bcopy(view_entry->ve_tg->id_data,
1278 1277 ve->ve_target_group.name,
1279 1278 view_entry->ve_tg->id_data_size);
1280 1279 ve->ve_target_group.name_size =
1281 1280 view_entry->ve_tg->id_data_size;
1282 1281 }
1283 1282 ve++;
1284 1283 iocd->stmf_obuf_nentries++;
1285 1284 }
1286 1285 break;
1287 1286 }
1288 1287 mutex_exit(&stmf_state.stmf_lock);
1289 1288 break;
1290 1289
1291 1290 case STMF_IOCTL_LOAD_PP_DATA:
1292 1291 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1293 1292 ret = EACCES;
1294 1293 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1295 1294 break;
1296 1295 }
1297 1296 ppi = (stmf_ppioctl_data_t *)ibuf;
1298 1297 if ((ppi == NULL) ||
1299 1298 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1300 1299 ret = EINVAL;
1301 1300 break;
1302 1301 }
1303 1302 /* returned token */
1304 1303 ppi_token = (uint64_t *)obuf;
1305 1304 if ((ppi_token == NULL) ||
1306 1305 (iocd->stmf_obuf_size < sizeof (uint64_t))) {
1307 1306 ret = EINVAL;
1308 1307 break;
1309 1308 }
1310 1309 ret = stmf_load_ppd_ioctl(ppi, ppi_token, &iocd->stmf_error);
1311 1310 break;
1312 1311
1313 1312 case STMF_IOCTL_GET_PP_DATA:
1314 1313 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1315 1314 ret = EACCES;
1316 1315 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1317 1316 break;
1318 1317 }
1319 1318 ppi = (stmf_ppioctl_data_t *)ibuf;
1320 1319 if (ppi == NULL ||
1321 1320 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1322 1321 ret = EINVAL;
1323 1322 break;
1324 1323 }
1325 1324 ppi_out = (stmf_ppioctl_data_t *)obuf;
1326 1325 if ((ppi_out == NULL) ||
1327 1326 (iocd->stmf_obuf_size < sizeof (stmf_ppioctl_data_t))) {
1328 1327 ret = EINVAL;
1329 1328 break;
1330 1329 }
1331 1330 ret = stmf_get_ppd_ioctl(ppi, ppi_out, &iocd->stmf_error);
1332 1331 break;
1333 1332
1334 1333 case STMF_IOCTL_CLEAR_PP_DATA:
1335 1334 if (stmf_state.stmf_config_state == STMF_CONFIG_NONE) {
1336 1335 ret = EACCES;
1337 1336 iocd->stmf_error = STMF_IOCERR_UPDATE_NEED_CFG_INIT;
1338 1337 break;
1339 1338 }
1340 1339 ppi = (stmf_ppioctl_data_t *)ibuf;
1341 1340 if ((ppi == NULL) ||
1342 1341 (iocd->stmf_ibuf_size < sizeof (stmf_ppioctl_data_t))) {
1343 1342 ret = EINVAL;
1344 1343 break;
1345 1344 }
1346 1345 ret = stmf_delete_ppd_ioctl(ppi);
1347 1346 break;
1348 1347
1349 1348 case STMF_IOCTL_CLEAR_TRACE:
1350 1349 stmf_trace_clear();
1351 1350 break;
1352 1351
1353 1352 case STMF_IOCTL_ADD_TRACE:
1354 1353 if (iocd->stmf_ibuf_size && ibuf) {
1355 1354 ((uint8_t *)ibuf)[iocd->stmf_ibuf_size - 1] = 0;
1356 1355 stmf_trace("\nstradm", "%s\n", ibuf);
1357 1356 }
1358 1357 break;
1359 1358
1360 1359 case STMF_IOCTL_GET_TRACE_POSITION:
1361 1360 if (obuf && (iocd->stmf_obuf_size > 3)) {
1362 1361 mutex_enter(&trace_buf_lock);
1363 1362 *((int *)obuf) = trace_buf_curndx;
1364 1363 mutex_exit(&trace_buf_lock);
1365 1364 } else {
1366 1365 ret = EINVAL;
1367 1366 }
1368 1367 break;
1369 1368
1370 1369 case STMF_IOCTL_GET_TRACE:
1371 1370 if ((iocd->stmf_obuf_size == 0) || (iocd->stmf_ibuf_size < 4)) {
1372 1371 ret = EINVAL;
1373 1372 break;
1374 1373 }
1375 1374 i = *((int *)ibuf);
1376 1375 if ((i > trace_buf_size) || ((i + iocd->stmf_obuf_size) >
1377 1376 trace_buf_size)) {
1378 1377 ret = EINVAL;
1379 1378 break;
1380 1379 }
1381 1380 mutex_enter(&trace_buf_lock);
1382 1381 bcopy(stmf_trace_buf + i, obuf, iocd->stmf_obuf_size);
1383 1382 mutex_exit(&trace_buf_lock);
1384 1383 break;
1385 1384
1386 1385 default:
1387 1386 ret = ENOTTY;
1388 1387 }
1389 1388
1390 1389 if (ret == 0) {
1391 1390 ret = stmf_copyout_iocdata(data, mode, iocd, obuf);
1392 1391 } else if (iocd->stmf_error) {
1393 1392 (void) stmf_copyout_iocdata(data, mode, iocd, obuf);
1394 1393 }
1395 1394 if (obuf) {
1396 1395 kmem_free(obuf, iocd->stmf_obuf_size);
1397 1396 obuf = NULL;
1398 1397 }
1399 1398 if (ibuf) {
1400 1399 kmem_free(ibuf, iocd->stmf_ibuf_size);
1401 1400 ibuf = NULL;
1402 1401 }
1403 1402 kmem_free(iocd, sizeof (stmf_iocdata_t));
1404 1403 return (ret);
1405 1404 }
1406 1405
1407 1406 static int
1408 1407 stmf_get_service_state()
1409 1408 {
1410 1409 stmf_i_local_port_t *ilport;
1411 1410 stmf_i_lu_t *ilu;
1412 1411 int online = 0;
1413 1412 int offline = 0;
1414 1413 int onlining = 0;
1415 1414 int offlining = 0;
1416 1415
1417 1416 ASSERT(mutex_owned(&stmf_state.stmf_lock));
1418 1417 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1419 1418 ilport = ilport->ilport_next) {
1420 1419 if (ilport->ilport_state == STMF_STATE_OFFLINE)
1421 1420 offline++;
1422 1421 else if (ilport->ilport_state == STMF_STATE_ONLINE)
1423 1422 online++;
1424 1423 else if (ilport->ilport_state == STMF_STATE_ONLINING)
1425 1424 onlining++;
1426 1425 else if (ilport->ilport_state == STMF_STATE_OFFLINING)
1427 1426 offlining++;
1428 1427 }
1429 1428
1430 1429 for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1431 1430 ilu = ilu->ilu_next) {
1432 1431 if (ilu->ilu_state == STMF_STATE_OFFLINE)
1433 1432 offline++;
1434 1433 else if (ilu->ilu_state == STMF_STATE_ONLINE)
1435 1434 online++;
1436 1435 else if (ilu->ilu_state == STMF_STATE_ONLINING)
1437 1436 onlining++;
1438 1437 else if (ilu->ilu_state == STMF_STATE_OFFLINING)
1439 1438 offlining++;
1440 1439 }
1441 1440
1442 1441 if (stmf_state.stmf_service_running) {
1443 1442 if (onlining)
1444 1443 return (STMF_STATE_ONLINING);
1445 1444 else
1446 1445 return (STMF_STATE_ONLINE);
1447 1446 }
1448 1447
1449 1448 if (offlining) {
1450 1449 return (STMF_STATE_OFFLINING);
1451 1450 }
1452 1451
1453 1452 return (STMF_STATE_OFFLINE);
1454 1453 }
1455 1454
1456 1455 static int
1457 1456 stmf_set_stmf_state(stmf_state_desc_t *std)
1458 1457 {
1459 1458 stmf_i_local_port_t *ilport;
1460 1459 stmf_i_lu_t *ilu;
1461 1460 stmf_state_change_info_t ssi;
1462 1461 int svc_state;
1463 1462
1464 1463 ssi.st_rflags = STMF_RFLAG_USER_REQUEST;
1465 1464 ssi.st_additional_info = NULL;
1466 1465
1467 1466 mutex_enter(&stmf_state.stmf_lock);
1468 1467 if (!stmf_state.stmf_exclusive_open) {
1469 1468 mutex_exit(&stmf_state.stmf_lock);
1470 1469 return (EACCES);
1471 1470 }
1472 1471
1473 1472 if (stmf_state.stmf_inventory_locked) {
1474 1473 mutex_exit(&stmf_state.stmf_lock);
1475 1474 return (EBUSY);
1476 1475 }
1477 1476
1478 1477 if ((std->state != STMF_STATE_ONLINE) &&
1479 1478 (std->state != STMF_STATE_OFFLINE)) {
1480 1479 mutex_exit(&stmf_state.stmf_lock);
1481 1480 return (EINVAL);
1482 1481 }
1483 1482
1484 1483 svc_state = stmf_get_service_state();
1485 1484 if ((svc_state == STMF_STATE_OFFLINING) ||
1486 1485 (svc_state == STMF_STATE_ONLINING)) {
1487 1486 mutex_exit(&stmf_state.stmf_lock);
1488 1487 return (EBUSY);
1489 1488 }
1490 1489
1491 1490 if (svc_state == STMF_STATE_OFFLINE) {
1492 1491 if (std->config_state == STMF_CONFIG_INIT) {
1493 1492 if (std->state != STMF_STATE_OFFLINE) {
1494 1493 mutex_exit(&stmf_state.stmf_lock);
1495 1494 return (EINVAL);
1496 1495 }
1497 1496 stmf_state.stmf_config_state = STMF_CONFIG_INIT;
1498 1497 stmf_delete_all_ppds();
1499 1498 stmf_view_clear_config();
1500 1499 stmf_view_init();
1501 1500 mutex_exit(&stmf_state.stmf_lock);
1502 1501 return (0);
1503 1502 }
1504 1503 if ((stmf_state.stmf_config_state == STMF_CONFIG_INIT) ||
1505 1504 (stmf_state.stmf_config_state == STMF_CONFIG_NONE)) {
1506 1505 if (std->config_state != STMF_CONFIG_INIT_DONE) {
1507 1506 mutex_exit(&stmf_state.stmf_lock);
1508 1507 return (EINVAL);
1509 1508 }
1510 1509 stmf_state.stmf_config_state = STMF_CONFIG_INIT_DONE;
1511 1510 }
1512 1511 if (std->state == STMF_STATE_OFFLINE) {
1513 1512 mutex_exit(&stmf_state.stmf_lock);
1514 1513 return (0);
1515 1514 }
1516 1515 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT) {
1517 1516 mutex_exit(&stmf_state.stmf_lock);
1518 1517 return (EINVAL);
1519 1518 }
1520 1519 stmf_state.stmf_inventory_locked = 1;
1521 1520 stmf_state.stmf_service_running = 1;
1522 1521 mutex_exit(&stmf_state.stmf_lock);
1523 1522
1524 1523 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1525 1524 ilport = ilport->ilport_next) {
1526 1525 if (stmf_state.stmf_default_lport_state !=
1527 1526 STMF_STATE_ONLINE)
1528 1527 continue;
1529 1528 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE,
1530 1529 ilport->ilport_lport, &ssi);
1531 1530 }
1532 1531
1533 1532 for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1534 1533 ilu = ilu->ilu_next) {
1535 1534 if (stmf_state.stmf_default_lu_state !=
1536 1535 STMF_STATE_ONLINE)
1537 1536 continue;
1538 1537 (void) stmf_ctl(STMF_CMD_LU_ONLINE, ilu->ilu_lu, &ssi);
1539 1538 }
1540 1539 mutex_enter(&stmf_state.stmf_lock);
1541 1540 stmf_state.stmf_inventory_locked = 0;
1542 1541 mutex_exit(&stmf_state.stmf_lock);
1543 1542 return (0);
1544 1543 }
1545 1544
1546 1545 /* svc_state is STMF_STATE_ONLINE here */
1547 1546 if ((std->state != STMF_STATE_OFFLINE) ||
1548 1547 (std->config_state == STMF_CONFIG_INIT)) {
1549 1548 mutex_exit(&stmf_state.stmf_lock);
1550 1549 return (EACCES);
1551 1550 }
1552 1551
1553 1552 stmf_state.stmf_inventory_locked = 1;
1554 1553 stmf_state.stmf_service_running = 0;
1555 1554
1556 1555 mutex_exit(&stmf_state.stmf_lock);
1557 1556 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1558 1557 ilport = ilport->ilport_next) {
1559 1558 if (ilport->ilport_state != STMF_STATE_ONLINE)
1560 1559 continue;
1561 1560 (void) stmf_ctl(STMF_CMD_LPORT_OFFLINE,
1562 1561 ilport->ilport_lport, &ssi);
1563 1562 }
1564 1563
1565 1564 for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
1566 1565 ilu = ilu->ilu_next) {
1567 1566 if (ilu->ilu_state != STMF_STATE_ONLINE)
1568 1567 continue;
1569 1568 (void) stmf_ctl(STMF_CMD_LU_OFFLINE, ilu->ilu_lu, &ssi);
1570 1569 }
1571 1570 mutex_enter(&stmf_state.stmf_lock);
1572 1571 stmf_state.stmf_inventory_locked = 0;
1573 1572 mutex_exit(&stmf_state.stmf_lock);
1574 1573 return (0);
1575 1574 }
1576 1575
1577 1576 static int
1578 1577 stmf_get_stmf_state(stmf_state_desc_t *std)
1579 1578 {
1580 1579 mutex_enter(&stmf_state.stmf_lock);
1581 1580 std->state = stmf_get_service_state();
1582 1581 std->config_state = stmf_state.stmf_config_state;
1583 1582 mutex_exit(&stmf_state.stmf_lock);
1584 1583
1585 1584 return (0);
1586 1585 }
1587 1586
1588 1587 /*
1589 1588 * handles registration message from pppt for a logical unit
1590 1589 */
1591 1590 stmf_status_t
1592 1591 stmf_ic_lu_reg(stmf_ic_reg_dereg_lun_msg_t *msg, uint32_t type)
1593 1592 {
1594 1593 stmf_i_lu_provider_t *ilp;
1595 1594 stmf_lu_provider_t *lp;
1596 1595 mutex_enter(&stmf_state.stmf_lock);
1597 1596 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
1598 1597 if (strcmp(msg->icrl_lu_provider_name,
1599 1598 ilp->ilp_lp->lp_name) == 0) {
1600 1599 lp = ilp->ilp_lp;
1601 1600 mutex_exit(&stmf_state.stmf_lock);
1602 1601 lp->lp_proxy_msg(msg->icrl_lun_id, msg->icrl_cb_arg,
1603 1602 msg->icrl_cb_arg_len, type);
1604 1603 return (STMF_SUCCESS);
1605 1604 }
1606 1605 }
1607 1606 mutex_exit(&stmf_state.stmf_lock);
1608 1607 return (STMF_SUCCESS);
1609 1608 }
1610 1609
1611 1610 /*
1612 1611 * handles de-registration message from pppt for a logical unit
1613 1612 */
1614 1613 stmf_status_t
1615 1614 stmf_ic_lu_dereg(stmf_ic_reg_dereg_lun_msg_t *msg)
1616 1615 {
1617 1616 stmf_i_lu_provider_t *ilp;
1618 1617 stmf_lu_provider_t *lp;
1619 1618 mutex_enter(&stmf_state.stmf_lock);
1620 1619 for (ilp = stmf_state.stmf_ilplist; ilp != NULL; ilp = ilp->ilp_next) {
1621 1620 if (strcmp(msg->icrl_lu_provider_name,
1622 1621 ilp->ilp_lp->lp_name) == 0) {
1623 1622 lp = ilp->ilp_lp;
1624 1623 mutex_exit(&stmf_state.stmf_lock);
1625 1624 lp->lp_proxy_msg(msg->icrl_lun_id, NULL, 0,
1626 1625 STMF_MSG_LU_DEREGISTER);
1627 1626 return (STMF_SUCCESS);
1628 1627 }
1629 1628 }
1630 1629 mutex_exit(&stmf_state.stmf_lock);
1631 1630 return (STMF_SUCCESS);
1632 1631 }
1633 1632
1634 1633 /*
1635 1634 * helper function to find a task that matches a task_msgid
1636 1635 */
1637 1636 scsi_task_t *
1638 1637 find_task_from_msgid(uint8_t *lu_id, stmf_ic_msgid_t task_msgid)
1639 1638 {
1640 1639 stmf_i_lu_t *ilu;
1641 1640 stmf_i_scsi_task_t *itask;
1642 1641
1643 1642 mutex_enter(&stmf_state.stmf_lock);
1644 1643 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
1645 1644 if (bcmp(lu_id, ilu->ilu_lu->lu_id->ident, 16) == 0) {
1646 1645 break;
1647 1646 }
1648 1647 }
1649 1648
1650 1649 if (ilu == NULL) {
1651 1650 mutex_exit(&stmf_state.stmf_lock);
1652 1651 return (NULL);
1653 1652 }
1654 1653
1655 1654 mutex_enter(&ilu->ilu_task_lock);
1656 1655 for (itask = ilu->ilu_tasks; itask != NULL;
1657 1656 itask = itask->itask_lu_next) {
1658 1657 if (itask->itask_flags & (ITASK_IN_FREE_LIST |
1659 1658 ITASK_BEING_ABORTED)) {
1660 1659 continue;
1661 1660 }
1662 1661 if (itask->itask_proxy_msg_id == task_msgid) {
1663 1662 break;
1664 1663 }
1665 1664 }
1666 1665 mutex_exit(&ilu->ilu_task_lock);
1667 1666 mutex_exit(&stmf_state.stmf_lock);
1668 1667
1669 1668 if (itask != NULL) {
1670 1669 return (itask->itask_task);
1671 1670 } else {
1672 1671 /* task not found. Likely already aborted. */
1673 1672 return (NULL);
1674 1673 }
1675 1674 }
1676 1675
1677 1676 /*
1678 1677 * message received from pppt/ic
1679 1678 */
1680 1679 stmf_status_t
1681 1680 stmf_msg_rx(stmf_ic_msg_t *msg)
1682 1681 {
1683 1682 mutex_enter(&stmf_state.stmf_lock);
1684 1683 if (stmf_state.stmf_alua_state != 1) {
1685 1684 mutex_exit(&stmf_state.stmf_lock);
1686 1685 cmn_err(CE_WARN, "stmf alua state is disabled");
1687 1686 ic_msg_free(msg);
1688 1687 return (STMF_FAILURE);
1689 1688 }
1690 1689 mutex_exit(&stmf_state.stmf_lock);
1691 1690
1692 1691 switch (msg->icm_msg_type) {
1693 1692 case STMF_ICM_REGISTER_LUN:
1694 1693 (void) stmf_ic_lu_reg(
1695 1694 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
1696 1695 STMF_MSG_LU_REGISTER);
1697 1696 break;
1698 1697 case STMF_ICM_LUN_ACTIVE:
1699 1698 (void) stmf_ic_lu_reg(
1700 1699 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg,
1701 1700 STMF_MSG_LU_ACTIVE);
1702 1701 break;
1703 1702 case STMF_ICM_DEREGISTER_LUN:
1704 1703 (void) stmf_ic_lu_dereg(
1705 1704 (stmf_ic_reg_dereg_lun_msg_t *)msg->icm_msg);
1706 1705 break;
1707 1706 case STMF_ICM_SCSI_DATA:
1708 1707 (void) stmf_ic_rx_scsi_data(
1709 1708 (stmf_ic_scsi_data_msg_t *)msg->icm_msg);
1710 1709 break;
1711 1710 case STMF_ICM_SCSI_STATUS:
1712 1711 (void) stmf_ic_rx_scsi_status(
1713 1712 (stmf_ic_scsi_status_msg_t *)msg->icm_msg);
1714 1713 break;
1715 1714 case STMF_ICM_STATUS:
1716 1715 (void) stmf_ic_rx_status(
1717 1716 (stmf_ic_status_msg_t *)msg->icm_msg);
1718 1717 break;
1719 1718 default:
1720 1719 cmn_err(CE_WARN, "unknown message received %d",
1721 1720 msg->icm_msg_type);
1722 1721 ic_msg_free(msg);
1723 1722 return (STMF_FAILURE);
1724 1723 }
1725 1724 ic_msg_free(msg);
1726 1725 return (STMF_SUCCESS);
1727 1726 }
1728 1727
1729 1728 stmf_status_t
1730 1729 stmf_ic_rx_status(stmf_ic_status_msg_t *msg)
1731 1730 {
1732 1731 stmf_i_local_port_t *ilport;
1733 1732
1734 1733 if (msg->ics_msg_type != STMF_ICM_REGISTER_PROXY_PORT) {
1735 1734 /* for now, ignore other message status */
1736 1735 return (STMF_SUCCESS);
1737 1736 }
1738 1737
1739 1738 if (msg->ics_status != STMF_SUCCESS) {
1740 1739 return (STMF_SUCCESS);
1741 1740 }
1742 1741
1743 1742 mutex_enter(&stmf_state.stmf_lock);
1744 1743 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
1745 1744 ilport = ilport->ilport_next) {
1746 1745 if (msg->ics_msgid == ilport->ilport_reg_msgid) {
1747 1746 ilport->ilport_proxy_registered = 1;
1748 1747 break;
1749 1748 }
1750 1749 }
1751 1750 mutex_exit(&stmf_state.stmf_lock);
1752 1751 return (STMF_SUCCESS);
1753 1752 }
1754 1753
1755 1754 /*
1756 1755 * handles scsi status message from pppt
1757 1756 */
1758 1757 stmf_status_t
1759 1758 stmf_ic_rx_scsi_status(stmf_ic_scsi_status_msg_t *msg)
1760 1759 {
1761 1760 scsi_task_t *task;
1762 1761
1763 1762 /* is this a task management command */
1764 1763 if (msg->icss_task_msgid & MSG_ID_TM_BIT) {
1765 1764 return (STMF_SUCCESS);
1766 1765 }
1767 1766
1768 1767 task = find_task_from_msgid(msg->icss_lun_id, msg->icss_task_msgid);
1769 1768
1770 1769 if (task == NULL) {
1771 1770 return (STMF_SUCCESS);
1772 1771 }
1773 1772
1774 1773 task->task_scsi_status = msg->icss_status;
1775 1774 task->task_sense_data = msg->icss_sense;
1776 1775 task->task_sense_length = msg->icss_sense_len;
1777 1776 (void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
1778 1777
1779 1778 return (STMF_SUCCESS);
1780 1779 }
1781 1780
1782 1781 /*
1783 1782 * handles scsi data message from pppt
1784 1783 */
1785 1784 stmf_status_t
1786 1785 stmf_ic_rx_scsi_data(stmf_ic_scsi_data_msg_t *msg)
1787 1786 {
1788 1787 stmf_i_scsi_task_t *itask;
1789 1788 scsi_task_t *task;
1790 1789 stmf_xfer_data_t *xd = NULL;
1791 1790 stmf_data_buf_t *dbuf;
1792 1791 uint32_t sz, minsz, xd_sz, asz;
1793 1792
1794 1793 /* is this a task management command */
1795 1794 if (msg->icsd_task_msgid & MSG_ID_TM_BIT) {
1796 1795 return (STMF_SUCCESS);
1797 1796 }
1798 1797
1799 1798 task = find_task_from_msgid(msg->icsd_lun_id, msg->icsd_task_msgid);
1800 1799 if (task == NULL) {
1801 1800 stmf_ic_msg_t *ic_xfer_done_msg = NULL;
1802 1801 static uint64_t data_msg_id;
1803 1802 stmf_status_t ic_ret = STMF_FAILURE;
1804 1803 mutex_enter(&stmf_state.stmf_lock);
1805 1804 data_msg_id = stmf_proxy_msg_id++;
1806 1805 mutex_exit(&stmf_state.stmf_lock);
1807 1806 /*
1808 1807 * send xfer done status to pppt
1809 1808 * for now, set the session id to 0 as we cannot
1810 1809 * ascertain it since we cannot find the task
1811 1810 */
1812 1811 ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
1813 1812 msg->icsd_task_msgid, 0, STMF_FAILURE, data_msg_id);
1814 1813 if (ic_xfer_done_msg) {
1815 1814 ic_ret = ic_tx_msg(ic_xfer_done_msg);
1816 1815 if (ic_ret != STMF_IC_MSG_SUCCESS) {
1817 1816 cmn_err(CE_WARN, "unable to xmit proxy msg");
1818 1817 }
1819 1818 }
1820 1819 return (STMF_FAILURE);
1821 1820 }
1822 1821
1823 1822 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
1824 1823 dbuf = itask->itask_proxy_dbuf;
1825 1824
1826 1825 task->task_cmd_xfer_length += msg->icsd_data_len;
1827 1826
1828 1827 if (task->task_additional_flags &
1829 1828 TASK_AF_NO_EXPECTED_XFER_LENGTH) {
1830 1829 task->task_expected_xfer_length =
1831 1830 task->task_cmd_xfer_length;
1832 1831 }
1833 1832
1834 1833 sz = min(task->task_expected_xfer_length,
1835 1834 task->task_cmd_xfer_length);
1836 1835
1837 1836 xd_sz = msg->icsd_data_len;
1838 1837 asz = xd_sz + sizeof (*xd) - 4;
1839 1838 xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
1840 1839
1841 1840 if (xd == NULL) {
1842 1841 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
1843 1842 STMF_ALLOC_FAILURE, NULL);
1844 1843 return (STMF_FAILURE);
1845 1844 }
1846 1845
1847 1846 xd->alloc_size = asz;
1848 1847 xd->size_left = xd_sz;
1849 1848 bcopy(msg->icsd_data, xd->buf, xd_sz);
1850 1849
1851 1850 sz = min(sz, xd->size_left);
1852 1851 xd->size_left = sz;
1853 1852 minsz = min(512, sz);
1854 1853
1855 1854 if (dbuf == NULL)
1856 1855 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
1857 1856 if (dbuf == NULL) {
1858 1857 kmem_free(xd, xd->alloc_size);
1859 1858 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
1860 1859 STMF_ALLOC_FAILURE, NULL);
1861 1860 return (STMF_FAILURE);
1862 1861 }
1863 1862 dbuf->db_lu_private = xd;
1864 1863 dbuf->db_relative_offset = task->task_nbytes_transferred;
1865 1864 stmf_xd_to_dbuf(dbuf, 0);
1866 1865
1867 1866 dbuf->db_flags = DB_DIRECTION_TO_RPORT;
1868 1867 (void) stmf_xfer_data(task, dbuf, 0);
1869 1868 return (STMF_SUCCESS);
1870 1869 }
1871 1870
1872 1871 stmf_status_t
1873 1872 stmf_proxy_scsi_cmd(scsi_task_t *task, stmf_data_buf_t *dbuf)
1874 1873 {
1875 1874 stmf_i_scsi_task_t *itask =
1876 1875 (stmf_i_scsi_task_t *)task->task_stmf_private;
1877 1876 stmf_i_local_port_t *ilport =
1878 1877 (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
1879 1878 stmf_ic_msg_t *ic_cmd_msg;
1880 1879 stmf_ic_msg_status_t ic_ret;
1881 1880 stmf_status_t ret = STMF_FAILURE;
1882 1881
1883 1882 if (stmf_state.stmf_alua_state != 1) {
1884 1883 cmn_err(CE_WARN, "stmf alua state is disabled");
1885 1884 return (STMF_FAILURE);
1886 1885 }
1887 1886
1888 1887 if (ilport->ilport_proxy_registered == 0) {
1889 1888 return (STMF_FAILURE);
1890 1889 }
1891 1890
1892 1891 mutex_enter(&stmf_state.stmf_lock);
1893 1892 itask->itask_proxy_msg_id = stmf_proxy_msg_id++;
1894 1893 mutex_exit(&stmf_state.stmf_lock);
1895 1894 itask->itask_proxy_dbuf = dbuf;
1896 1895
1897 1896 /*
1898 1897 * stmf will now take over the task handling for this task
1899 1898 * but it still needs to be treated differently from other
1900 1899 * default handled tasks, hence the ITASK_PROXY_TASK.
1901 1900 * If this is a task management function, we're really just
1902 1901 * duping the command to the peer. Set the TM bit so that
1903 1902 * we can recognize this on return since we won't be completing
1904 1903 * the proxied task in that case.
1905 1904 */
1906 1905 if (task->task_mgmt_function) {
1907 1906 itask->itask_proxy_msg_id |= MSG_ID_TM_BIT;
1908 1907 } else {
1909 1908 uint32_t new, old;
1910 1909 do {
1911 1910 new = old = itask->itask_flags;
1912 1911 if (new & ITASK_BEING_ABORTED)
1913 1912 return (STMF_FAILURE);
1914 1913 new |= ITASK_DEFAULT_HANDLING | ITASK_PROXY_TASK;
1915 1914 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
1916 1915 }
1917 1916 if (dbuf) {
1918 1917 ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1919 1918 task, dbuf->db_data_size, dbuf->db_sglist[0].seg_addr,
1920 1919 itask->itask_proxy_msg_id);
1921 1920 } else {
1922 1921 ic_cmd_msg = ic_scsi_cmd_msg_alloc(itask->itask_proxy_msg_id,
1923 1922 task, 0, NULL, itask->itask_proxy_msg_id);
1924 1923 }
1925 1924 if (ic_cmd_msg) {
1926 1925 ic_ret = ic_tx_msg(ic_cmd_msg);
1927 1926 if (ic_ret == STMF_IC_MSG_SUCCESS) {
1928 1927 ret = STMF_SUCCESS;
1929 1928 }
1930 1929 }
1931 1930 return (ret);
1932 1931 }
1933 1932
1934 1933
1935 1934 stmf_status_t
1936 1935 pppt_modload()
1937 1936 {
1938 1937 int error;
1939 1938
1940 1939 if (pppt_mod == NULL && ((pppt_mod =
1941 1940 ddi_modopen("drv/pppt", KRTLD_MODE_FIRST, &error)) == NULL)) {
1942 1941 cmn_err(CE_WARN, "Unable to load pppt");
1943 1942 return (STMF_FAILURE);
1944 1943 }
1945 1944
1946 1945 if (ic_reg_port_msg_alloc == NULL && ((ic_reg_port_msg_alloc =
1947 1946 (stmf_ic_reg_port_msg_alloc_func_t)
1948 1947 ddi_modsym(pppt_mod, "stmf_ic_reg_port_msg_alloc",
1949 1948 &error)) == NULL)) {
1950 1949 cmn_err(CE_WARN,
1951 1950 "Unable to find symbol - stmf_ic_reg_port_msg_alloc");
1952 1951 return (STMF_FAILURE);
1953 1952 }
1954 1953
1955 1954
1956 1955 if (ic_dereg_port_msg_alloc == NULL && ((ic_dereg_port_msg_alloc =
1957 1956 (stmf_ic_dereg_port_msg_alloc_func_t)
1958 1957 ddi_modsym(pppt_mod, "stmf_ic_dereg_port_msg_alloc",
1959 1958 &error)) == NULL)) {
1960 1959 cmn_err(CE_WARN,
1961 1960 "Unable to find symbol - stmf_ic_dereg_port_msg_alloc");
1962 1961 return (STMF_FAILURE);
1963 1962 }
1964 1963
1965 1964 if (ic_reg_lun_msg_alloc == NULL && ((ic_reg_lun_msg_alloc =
1966 1965 (stmf_ic_reg_lun_msg_alloc_func_t)
1967 1966 ddi_modsym(pppt_mod, "stmf_ic_reg_lun_msg_alloc",
1968 1967 &error)) == NULL)) {
1969 1968 cmn_err(CE_WARN,
1970 1969 "Unable to find symbol - stmf_ic_reg_lun_msg_alloc");
1971 1970 return (STMF_FAILURE);
1972 1971 }
1973 1972
1974 1973 if (ic_lun_active_msg_alloc == NULL && ((ic_lun_active_msg_alloc =
1975 1974 (stmf_ic_lun_active_msg_alloc_func_t)
1976 1975 ddi_modsym(pppt_mod, "stmf_ic_lun_active_msg_alloc",
1977 1976 &error)) == NULL)) {
1978 1977 cmn_err(CE_WARN,
1979 1978 "Unable to find symbol - stmf_ic_lun_active_msg_alloc");
1980 1979 return (STMF_FAILURE);
1981 1980 }
1982 1981
1983 1982 if (ic_dereg_lun_msg_alloc == NULL && ((ic_dereg_lun_msg_alloc =
1984 1983 (stmf_ic_dereg_lun_msg_alloc_func_t)
1985 1984 ddi_modsym(pppt_mod, "stmf_ic_dereg_lun_msg_alloc",
1986 1985 &error)) == NULL)) {
1987 1986 cmn_err(CE_WARN,
1988 1987 "Unable to find symbol - stmf_ic_dereg_lun_msg_alloc");
1989 1988 return (STMF_FAILURE);
1990 1989 }
1991 1990
1992 1991 if (ic_scsi_cmd_msg_alloc == NULL && ((ic_scsi_cmd_msg_alloc =
1993 1992 (stmf_ic_scsi_cmd_msg_alloc_func_t)
1994 1993 ddi_modsym(pppt_mod, "stmf_ic_scsi_cmd_msg_alloc",
1995 1994 &error)) == NULL)) {
1996 1995 cmn_err(CE_WARN,
1997 1996 "Unable to find symbol - stmf_ic_scsi_cmd_msg_alloc");
1998 1997 return (STMF_FAILURE);
1999 1998 }
2000 1999
2001 2000 if (ic_scsi_data_xfer_done_msg_alloc == NULL &&
2002 2001 ((ic_scsi_data_xfer_done_msg_alloc =
2003 2002 (stmf_ic_scsi_data_xfer_done_msg_alloc_func_t)
2004 2003 ddi_modsym(pppt_mod, "stmf_ic_scsi_data_xfer_done_msg_alloc",
2005 2004 &error)) == NULL)) {
2006 2005 cmn_err(CE_WARN,
2007 2006 "Unable to find symbol -"
2008 2007 "stmf_ic_scsi_data_xfer_done_msg_alloc");
2009 2008 return (STMF_FAILURE);
2010 2009 }
2011 2010
2012 2011 if (ic_session_reg_msg_alloc == NULL &&
2013 2012 ((ic_session_reg_msg_alloc =
2014 2013 (stmf_ic_session_create_msg_alloc_func_t)
2015 2014 ddi_modsym(pppt_mod, "stmf_ic_session_create_msg_alloc",
2016 2015 &error)) == NULL)) {
2017 2016 cmn_err(CE_WARN,
2018 2017 "Unable to find symbol -"
2019 2018 "stmf_ic_session_create_msg_alloc");
2020 2019 return (STMF_FAILURE);
2021 2020 }
2022 2021
2023 2022 if (ic_session_dereg_msg_alloc == NULL &&
2024 2023 ((ic_session_dereg_msg_alloc =
2025 2024 (stmf_ic_session_destroy_msg_alloc_func_t)
2026 2025 ddi_modsym(pppt_mod, "stmf_ic_session_destroy_msg_alloc",
2027 2026 &error)) == NULL)) {
2028 2027 cmn_err(CE_WARN,
2029 2028 "Unable to find symbol -"
2030 2029 "stmf_ic_session_destroy_msg_alloc");
2031 2030 return (STMF_FAILURE);
2032 2031 }
2033 2032
2034 2033 if (ic_tx_msg == NULL && ((ic_tx_msg =
2035 2034 (stmf_ic_tx_msg_func_t)ddi_modsym(pppt_mod, "stmf_ic_tx_msg",
2036 2035 &error)) == NULL)) {
2037 2036 cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_tx_msg");
2038 2037 return (STMF_FAILURE);
2039 2038 }
2040 2039
2041 2040 if (ic_msg_free == NULL && ((ic_msg_free =
2042 2041 (stmf_ic_msg_free_func_t)ddi_modsym(pppt_mod, "stmf_ic_msg_free",
2043 2042 &error)) == NULL)) {
2044 2043 cmn_err(CE_WARN, "Unable to find symbol - stmf_ic_msg_free");
2045 2044 return (STMF_FAILURE);
2046 2045 }
2047 2046 return (STMF_SUCCESS);
2048 2047 }
2049 2048
2050 2049 static void
2051 2050 stmf_get_alua_state(stmf_alua_state_desc_t *alua_state)
2052 2051 {
2053 2052 mutex_enter(&stmf_state.stmf_lock);
2054 2053 alua_state->alua_node = stmf_state.stmf_alua_node;
2055 2054 alua_state->alua_state = stmf_state.stmf_alua_state;
2056 2055 mutex_exit(&stmf_state.stmf_lock);
2057 2056 }
2058 2057
2059 2058
2060 2059 static int
2061 2060 stmf_set_alua_state(stmf_alua_state_desc_t *alua_state)
2062 2061 {
2063 2062 stmf_i_local_port_t *ilport;
2064 2063 stmf_i_lu_t *ilu;
2065 2064 stmf_lu_t *lu;
2066 2065 stmf_ic_msg_status_t ic_ret;
2067 2066 stmf_ic_msg_t *ic_reg_lun, *ic_reg_port;
2068 2067 stmf_local_port_t *lport;
2069 2068 int ret = 0;
2070 2069
2071 2070 if (alua_state->alua_state > 1 || alua_state->alua_node > 1) {
2072 2071 return (EINVAL);
2073 2072 }
2074 2073
2075 2074 mutex_enter(&stmf_state.stmf_lock);
2076 2075 if (alua_state->alua_state == 1) {
2077 2076 if (pppt_modload() == STMF_FAILURE) {
2078 2077 ret = EIO;
2079 2078 goto err;
2080 2079 }
2081 2080 if (alua_state->alua_node != 0) {
2082 2081 /* reset existing rtpids to new base */
2083 2082 stmf_rtpid_counter = 255;
2084 2083 }
2085 2084 stmf_state.stmf_alua_node = alua_state->alua_node;
2086 2085 stmf_state.stmf_alua_state = 1;
2087 2086 /* register existing local ports with ppp */
2088 2087 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2089 2088 ilport = ilport->ilport_next) {
2090 2089 /* skip standby ports and non-alua participants */
2091 2090 if (ilport->ilport_standby == 1 ||
2092 2091 ilport->ilport_alua == 0) {
2093 2092 continue;
2094 2093 }
2095 2094 if (alua_state->alua_node != 0) {
2096 2095 ilport->ilport_rtpid =
2097 2096 atomic_inc_16_nv(&stmf_rtpid_counter);
2098 2097 }
2099 2098 lport = ilport->ilport_lport;
2100 2099 ic_reg_port = ic_reg_port_msg_alloc(
2101 2100 lport->lport_id, ilport->ilport_rtpid,
2102 2101 0, NULL, stmf_proxy_msg_id);
2103 2102 if (ic_reg_port) {
2104 2103 ic_ret = ic_tx_msg(ic_reg_port);
2105 2104 if (ic_ret == STMF_IC_MSG_SUCCESS) {
2106 2105 ilport->ilport_reg_msgid =
2107 2106 stmf_proxy_msg_id++;
2108 2107 } else {
2109 2108 cmn_err(CE_WARN,
2110 2109 "error on port registration "
2111 2110 "port - %s",
2112 2111 ilport->ilport_kstat_tgt_name);
2113 2112 }
2114 2113 }
2115 2114 }
2116 2115 /* register existing logical units */
2117 2116 for (ilu = stmf_state.stmf_ilulist; ilu != NULL;
2118 2117 ilu = ilu->ilu_next) {
2119 2118 if (ilu->ilu_access != STMF_LU_ACTIVE) {
2120 2119 continue;
2121 2120 }
2122 2121 /* register with proxy module */
2123 2122 lu = ilu->ilu_lu;
2124 2123 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2125 2124 lu->lu_lp->lp_alua_support) {
2126 2125 ilu->ilu_alua = 1;
2127 2126 /* allocate the register message */
2128 2127 ic_reg_lun = ic_reg_lun_msg_alloc(
2129 2128 lu->lu_id->ident, lu->lu_lp->lp_name,
2130 2129 lu->lu_proxy_reg_arg_len,
2131 2130 (uint8_t *)lu->lu_proxy_reg_arg,
2132 2131 stmf_proxy_msg_id);
2133 2132 /* send the message */
2134 2133 if (ic_reg_lun) {
2135 2134 ic_ret = ic_tx_msg(ic_reg_lun);
2136 2135 if (ic_ret == STMF_IC_MSG_SUCCESS) {
2137 2136 stmf_proxy_msg_id++;
2138 2137 }
2139 2138 }
2140 2139 }
2141 2140 }
2142 2141 } else {
2143 2142 stmf_state.stmf_alua_state = 0;
2144 2143 }
2145 2144
2146 2145 err:
2147 2146 mutex_exit(&stmf_state.stmf_lock);
2148 2147 return (ret);
2149 2148 }
2150 2149
2151 2150
2152 2151 typedef struct {
2153 2152 void *bp; /* back pointer from internal struct to main struct */
2154 2153 int alloc_size;
2155 2154 } __istmf_t;
2156 2155
2157 2156 typedef struct {
2158 2157 __istmf_t *fp; /* Framework private */
2159 2158 void *cp; /* Caller private */
2160 2159 void *ss; /* struct specific */
2161 2160 } __stmf_t;
2162 2161
2163 2162 static struct {
2164 2163 int shared;
2165 2164 int fw_private;
2166 2165 } stmf_sizes[] = { { 0, 0 },
2167 2166 { GET_STRUCT_SIZE(stmf_lu_provider_t),
2168 2167 GET_STRUCT_SIZE(stmf_i_lu_provider_t) },
2169 2168 { GET_STRUCT_SIZE(stmf_port_provider_t),
2170 2169 GET_STRUCT_SIZE(stmf_i_port_provider_t) },
2171 2170 { GET_STRUCT_SIZE(stmf_local_port_t),
2172 2171 GET_STRUCT_SIZE(stmf_i_local_port_t) },
2173 2172 { GET_STRUCT_SIZE(stmf_lu_t),
2174 2173 GET_STRUCT_SIZE(stmf_i_lu_t) },
2175 2174 { GET_STRUCT_SIZE(stmf_scsi_session_t),
2176 2175 GET_STRUCT_SIZE(stmf_i_scsi_session_t) },
2177 2176 { GET_STRUCT_SIZE(scsi_task_t),
2178 2177 GET_STRUCT_SIZE(stmf_i_scsi_task_t) },
2179 2178 { GET_STRUCT_SIZE(stmf_data_buf_t),
2180 2179 GET_STRUCT_SIZE(__istmf_t) },
2181 2180 { GET_STRUCT_SIZE(stmf_dbuf_store_t),
2182 2181 GET_STRUCT_SIZE(__istmf_t) }
2183 2182
2184 2183 };
2185 2184
2186 2185 void *
2187 2186 stmf_alloc(stmf_struct_id_t struct_id, int additional_size, int flags)
2188 2187 {
2189 2188 int stmf_size;
2190 2189 int kmem_flag;
2191 2190 __stmf_t *sh;
2192 2191
2193 2192 if ((struct_id == 0) || (struct_id >= STMF_MAX_STRUCT_IDS))
2194 2193 return (NULL);
2195 2194
2196 2195 if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
2197 2196 kmem_flag = KM_NOSLEEP;
2198 2197 } else {
2199 2198 kmem_flag = KM_SLEEP;
2200 2199 }
2201 2200
2202 2201 additional_size = (additional_size + 7) & (~7);
2203 2202 stmf_size = stmf_sizes[struct_id].shared +
2204 2203 stmf_sizes[struct_id].fw_private + additional_size;
2205 2204
2206 2205 if (flags & AF_DONTZERO)
2207 2206 sh = (__stmf_t *)kmem_alloc(stmf_size, kmem_flag);
2208 2207 else
2209 2208 sh = (__stmf_t *)kmem_zalloc(stmf_size, kmem_flag);
2210 2209
2211 2210 if (sh == NULL)
2212 2211 return (NULL);
2213 2212
2214 2213 /*
2215 2214 * In principle, the implementation inside stmf_alloc should not
2216 2215 * be changed anyway. But the original order of framework private
2217 2216 * data and caller private data does not support sglist in the caller
2218 2217 * private data.
2219 2218 * To work around this, the memory segments of framework private
2220 2219 * data and caller private data are re-ordered here.
2221 2220 * A better solution is to provide a specific interface to allocate
2222 2221 * the sglist, then we will not need this workaround any more.
2223 2222 * But before the new interface is available, the memory segment
2224 2223 * ordering should be kept as is.
2225 2224 */
2226 2225 sh->cp = GET_BYTE_OFFSET(sh, stmf_sizes[struct_id].shared);
2227 2226 sh->fp = (__istmf_t *)GET_BYTE_OFFSET(sh,
2228 2227 stmf_sizes[struct_id].shared + additional_size);
2229 2228
2230 2229 sh->fp->bp = sh;
2231 2230 /* Just store the total size instead of storing additional size */
2232 2231 sh->fp->alloc_size = stmf_size;
2233 2232
2234 2233 return (sh);
2235 2234 }
2236 2235
2237 2236 void
2238 2237 stmf_free(void *ptr)
2239 2238 {
2240 2239 __stmf_t *sh = (__stmf_t *)ptr;
2241 2240
2242 2241 /*
2243 2242 * So far we dont need any struct specific processing. If such
2244 2243 * a need ever arises, then store the struct id in the framework
2245 2244 * private section and get it here as sh->fp->struct_id.
2246 2245 */
2247 2246 kmem_free(ptr, sh->fp->alloc_size);
2248 2247 }
2249 2248
2250 2249 /*
2251 2250 * Given a pointer to stmf_lu_t, verifies if this lu is registered with the
2252 2251 * framework and returns a pointer to framework private data for the lu.
2253 2252 * Returns NULL if the lu was not found.
2254 2253 */
2255 2254 stmf_i_lu_t *
2256 2255 stmf_lookup_lu(stmf_lu_t *lu)
2257 2256 {
2258 2257 stmf_i_lu_t *ilu;
2259 2258 ASSERT(mutex_owned(&stmf_state.stmf_lock));
2260 2259
2261 2260 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2262 2261 if (ilu->ilu_lu == lu)
2263 2262 return (ilu);
2264 2263 }
2265 2264 return (NULL);
2266 2265 }
2267 2266
2268 2267 /*
2269 2268 * Given a pointer to stmf_local_port_t, verifies if this lport is registered
2270 2269 * with the framework and returns a pointer to framework private data for
2271 2270 * the lport.
2272 2271 * Returns NULL if the lport was not found.
2273 2272 */
2274 2273 stmf_i_local_port_t *
2275 2274 stmf_lookup_lport(stmf_local_port_t *lport)
2276 2275 {
2277 2276 stmf_i_local_port_t *ilport;
2278 2277 ASSERT(mutex_owned(&stmf_state.stmf_lock));
2279 2278
2280 2279 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
2281 2280 ilport = ilport->ilport_next) {
2282 2281 if (ilport->ilport_lport == lport)
2283 2282 return (ilport);
2284 2283 }
2285 2284 return (NULL);
2286 2285 }
2287 2286
2288 2287 stmf_status_t
2289 2288 stmf_register_lu_provider(stmf_lu_provider_t *lp)
2290 2289 {
2291 2290 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2292 2291 stmf_pp_data_t *ppd;
2293 2292 uint32_t cb_flags;
2294 2293
2295 2294 if (lp->lp_lpif_rev != LPIF_REV_1 && lp->lp_lpif_rev != LPIF_REV_2)
2296 2295 return (STMF_FAILURE);
2297 2296
2298 2297 mutex_enter(&stmf_state.stmf_lock);
2299 2298 ilp->ilp_next = stmf_state.stmf_ilplist;
2300 2299 stmf_state.stmf_ilplist = ilp;
2301 2300 stmf_state.stmf_nlps++;
2302 2301
2303 2302 /* See if we need to do a callback */
2304 2303 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2305 2304 if (strcmp(ppd->ppd_name, lp->lp_name) == 0) {
2306 2305 break;
2307 2306 }
2308 2307 }
2309 2308 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2310 2309 goto rlp_bail_out;
2311 2310 }
2312 2311 ilp->ilp_ppd = ppd;
2313 2312 ppd->ppd_provider = ilp;
2314 2313 if (lp->lp_cb == NULL)
2315 2314 goto rlp_bail_out;
2316 2315 ilp->ilp_cb_in_progress = 1;
2317 2316 cb_flags = STMF_PCB_PREG_COMPLETE;
2318 2317 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2319 2318 cb_flags |= STMF_PCB_STMF_ONLINING;
2320 2319 mutex_exit(&stmf_state.stmf_lock);
2321 2320 lp->lp_cb(lp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2322 2321 mutex_enter(&stmf_state.stmf_lock);
2323 2322 ilp->ilp_cb_in_progress = 0;
2324 2323
2325 2324 rlp_bail_out:
2326 2325 mutex_exit(&stmf_state.stmf_lock);
2327 2326
2328 2327 return (STMF_SUCCESS);
2329 2328 }
2330 2329
2331 2330 stmf_status_t
2332 2331 stmf_deregister_lu_provider(stmf_lu_provider_t *lp)
2333 2332 {
2334 2333 stmf_i_lu_provider_t **ppilp;
2335 2334 stmf_i_lu_provider_t *ilp = (stmf_i_lu_provider_t *)lp->lp_stmf_private;
2336 2335
2337 2336 mutex_enter(&stmf_state.stmf_lock);
2338 2337 if (ilp->ilp_nlus || ilp->ilp_cb_in_progress) {
2339 2338 mutex_exit(&stmf_state.stmf_lock);
2340 2339 return (STMF_BUSY);
2341 2340 }
2342 2341 for (ppilp = &stmf_state.stmf_ilplist; *ppilp != NULL;
2343 2342 ppilp = &((*ppilp)->ilp_next)) {
2344 2343 if (*ppilp == ilp) {
2345 2344 *ppilp = ilp->ilp_next;
2346 2345 stmf_state.stmf_nlps--;
2347 2346 if (ilp->ilp_ppd) {
2348 2347 ilp->ilp_ppd->ppd_provider = NULL;
2349 2348 ilp->ilp_ppd = NULL;
2350 2349 }
2351 2350 mutex_exit(&stmf_state.stmf_lock);
2352 2351 return (STMF_SUCCESS);
2353 2352 }
2354 2353 }
2355 2354 mutex_exit(&stmf_state.stmf_lock);
2356 2355 return (STMF_NOT_FOUND);
2357 2356 }
2358 2357
2359 2358 stmf_status_t
2360 2359 stmf_register_port_provider(stmf_port_provider_t *pp)
2361 2360 {
2362 2361 stmf_i_port_provider_t *ipp =
2363 2362 (stmf_i_port_provider_t *)pp->pp_stmf_private;
2364 2363 stmf_pp_data_t *ppd;
2365 2364 uint32_t cb_flags;
2366 2365
2367 2366 if (pp->pp_portif_rev != PORTIF_REV_1)
2368 2367 return (STMF_FAILURE);
2369 2368
2370 2369 mutex_enter(&stmf_state.stmf_lock);
2371 2370 ipp->ipp_next = stmf_state.stmf_ipplist;
2372 2371 stmf_state.stmf_ipplist = ipp;
2373 2372 stmf_state.stmf_npps++;
2374 2373 /* See if we need to do a callback */
2375 2374 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2376 2375 if (strcmp(ppd->ppd_name, pp->pp_name) == 0) {
2377 2376 break;
2378 2377 }
2379 2378 }
2380 2379 if ((ppd == NULL) || (ppd->ppd_nv == NULL)) {
2381 2380 goto rpp_bail_out;
2382 2381 }
2383 2382 ipp->ipp_ppd = ppd;
2384 2383 ppd->ppd_provider = ipp;
2385 2384 if (pp->pp_cb == NULL)
2386 2385 goto rpp_bail_out;
2387 2386 ipp->ipp_cb_in_progress = 1;
2388 2387 cb_flags = STMF_PCB_PREG_COMPLETE;
2389 2388 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2390 2389 cb_flags |= STMF_PCB_STMF_ONLINING;
2391 2390 mutex_exit(&stmf_state.stmf_lock);
2392 2391 pp->pp_cb(pp, STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2393 2392 mutex_enter(&stmf_state.stmf_lock);
2394 2393 ipp->ipp_cb_in_progress = 0;
2395 2394
2396 2395 rpp_bail_out:
2397 2396 mutex_exit(&stmf_state.stmf_lock);
2398 2397
2399 2398 return (STMF_SUCCESS);
2400 2399 }
2401 2400
2402 2401 stmf_status_t
2403 2402 stmf_deregister_port_provider(stmf_port_provider_t *pp)
2404 2403 {
2405 2404 stmf_i_port_provider_t *ipp =
2406 2405 (stmf_i_port_provider_t *)pp->pp_stmf_private;
2407 2406 stmf_i_port_provider_t **ppipp;
2408 2407
2409 2408 mutex_enter(&stmf_state.stmf_lock);
2410 2409 if (ipp->ipp_npps || ipp->ipp_cb_in_progress) {
2411 2410 mutex_exit(&stmf_state.stmf_lock);
2412 2411 return (STMF_BUSY);
2413 2412 }
2414 2413 for (ppipp = &stmf_state.stmf_ipplist; *ppipp != NULL;
2415 2414 ppipp = &((*ppipp)->ipp_next)) {
2416 2415 if (*ppipp == ipp) {
2417 2416 *ppipp = ipp->ipp_next;
2418 2417 stmf_state.stmf_npps--;
2419 2418 if (ipp->ipp_ppd) {
2420 2419 ipp->ipp_ppd->ppd_provider = NULL;
2421 2420 ipp->ipp_ppd = NULL;
2422 2421 }
2423 2422 mutex_exit(&stmf_state.stmf_lock);
2424 2423 return (STMF_SUCCESS);
2425 2424 }
2426 2425 }
2427 2426 mutex_exit(&stmf_state.stmf_lock);
2428 2427 return (STMF_NOT_FOUND);
2429 2428 }
2430 2429
2431 2430 int
2432 2431 stmf_load_ppd_ioctl(stmf_ppioctl_data_t *ppi, uint64_t *ppi_token,
2433 2432 uint32_t *err_ret)
2434 2433 {
2435 2434 stmf_i_port_provider_t *ipp;
2436 2435 stmf_i_lu_provider_t *ilp;
2437 2436 stmf_pp_data_t *ppd;
2438 2437 nvlist_t *nv;
2439 2438 int s;
2440 2439 int ret;
2441 2440
2442 2441 *err_ret = 0;
2443 2442
2444 2443 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2445 2444 return (EINVAL);
2446 2445 }
2447 2446
2448 2447 mutex_enter(&stmf_state.stmf_lock);
2449 2448 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2450 2449 if (ppi->ppi_lu_provider) {
2451 2450 if (!ppd->ppd_lu_provider)
2452 2451 continue;
2453 2452 } else if (ppi->ppi_port_provider) {
2454 2453 if (!ppd->ppd_port_provider)
2455 2454 continue;
2456 2455 }
2457 2456 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2458 2457 break;
2459 2458 }
2460 2459
2461 2460 if (ppd == NULL) {
2462 2461 /* New provider */
2463 2462 s = strlen(ppi->ppi_name);
2464 2463 if (s > 254) {
2465 2464 mutex_exit(&stmf_state.stmf_lock);
2466 2465 return (EINVAL);
2467 2466 }
2468 2467 s += sizeof (stmf_pp_data_t) - 7;
2469 2468
2470 2469 ppd = kmem_zalloc(s, KM_NOSLEEP);
2471 2470 if (ppd == NULL) {
2472 2471 mutex_exit(&stmf_state.stmf_lock);
2473 2472 return (ENOMEM);
2474 2473 }
2475 2474 ppd->ppd_alloc_size = s;
2476 2475 (void) strcpy(ppd->ppd_name, ppi->ppi_name);
2477 2476
2478 2477 /* See if this provider already exists */
2479 2478 if (ppi->ppi_lu_provider) {
2480 2479 ppd->ppd_lu_provider = 1;
2481 2480 for (ilp = stmf_state.stmf_ilplist; ilp != NULL;
2482 2481 ilp = ilp->ilp_next) {
2483 2482 if (strcmp(ppi->ppi_name,
2484 2483 ilp->ilp_lp->lp_name) == 0) {
2485 2484 ppd->ppd_provider = ilp;
2486 2485 ilp->ilp_ppd = ppd;
2487 2486 break;
2488 2487 }
2489 2488 }
2490 2489 } else {
2491 2490 ppd->ppd_port_provider = 1;
2492 2491 for (ipp = stmf_state.stmf_ipplist; ipp != NULL;
2493 2492 ipp = ipp->ipp_next) {
2494 2493 if (strcmp(ppi->ppi_name,
2495 2494 ipp->ipp_pp->pp_name) == 0) {
2496 2495 ppd->ppd_provider = ipp;
2497 2496 ipp->ipp_ppd = ppd;
2498 2497 break;
2499 2498 }
2500 2499 }
2501 2500 }
2502 2501
2503 2502 /* Link this ppd in */
2504 2503 ppd->ppd_next = stmf_state.stmf_ppdlist;
2505 2504 stmf_state.stmf_ppdlist = ppd;
2506 2505 }
2507 2506
2508 2507 /*
2509 2508 * User is requesting that the token be checked.
2510 2509 * If there was another set after the user's get
2511 2510 * it's an error
2512 2511 */
2513 2512 if (ppi->ppi_token_valid) {
2514 2513 if (ppi->ppi_token != ppd->ppd_token) {
2515 2514 *err_ret = STMF_IOCERR_PPD_UPDATED;
2516 2515 mutex_exit(&stmf_state.stmf_lock);
2517 2516 return (EINVAL);
2518 2517 }
2519 2518 }
2520 2519
2521 2520 if ((ret = nvlist_unpack((char *)ppi->ppi_data,
2522 2521 (size_t)ppi->ppi_data_size, &nv, KM_NOSLEEP)) != 0) {
2523 2522 mutex_exit(&stmf_state.stmf_lock);
2524 2523 return (ret);
2525 2524 }
2526 2525
2527 2526 /* Free any existing lists and add this one to the ppd */
2528 2527 nvlist_free(ppd->ppd_nv);
2529 2528 ppd->ppd_nv = nv;
2530 2529
2531 2530 /* set the token for writes */
2532 2531 ppd->ppd_token++;
2533 2532 /* return token to caller */
2534 2533 if (ppi_token) {
2535 2534 *ppi_token = ppd->ppd_token;
2536 2535 }
2537 2536
2538 2537 /* If there is a provider registered, do the notifications */
2539 2538 if (ppd->ppd_provider) {
2540 2539 uint32_t cb_flags = 0;
2541 2540
2542 2541 if (stmf_state.stmf_config_state == STMF_CONFIG_INIT)
2543 2542 cb_flags |= STMF_PCB_STMF_ONLINING;
2544 2543 if (ppi->ppi_lu_provider) {
2545 2544 ilp = (stmf_i_lu_provider_t *)ppd->ppd_provider;
2546 2545 if (ilp->ilp_lp->lp_cb == NULL)
2547 2546 goto bail_out;
2548 2547 ilp->ilp_cb_in_progress = 1;
2549 2548 mutex_exit(&stmf_state.stmf_lock);
2550 2549 ilp->ilp_lp->lp_cb(ilp->ilp_lp,
2551 2550 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2552 2551 mutex_enter(&stmf_state.stmf_lock);
2553 2552 ilp->ilp_cb_in_progress = 0;
2554 2553 } else {
2555 2554 ipp = (stmf_i_port_provider_t *)ppd->ppd_provider;
2556 2555 if (ipp->ipp_pp->pp_cb == NULL)
2557 2556 goto bail_out;
2558 2557 ipp->ipp_cb_in_progress = 1;
2559 2558 mutex_exit(&stmf_state.stmf_lock);
2560 2559 ipp->ipp_pp->pp_cb(ipp->ipp_pp,
2561 2560 STMF_PROVIDER_DATA_UPDATED, ppd->ppd_nv, cb_flags);
2562 2561 mutex_enter(&stmf_state.stmf_lock);
2563 2562 ipp->ipp_cb_in_progress = 0;
2564 2563 }
2565 2564 }
2566 2565
2567 2566 bail_out:
2568 2567 mutex_exit(&stmf_state.stmf_lock);
2569 2568
2570 2569 return (0);
2571 2570 }
2572 2571
2573 2572 void
2574 2573 stmf_delete_ppd(stmf_pp_data_t *ppd)
2575 2574 {
2576 2575 stmf_pp_data_t **pppd;
2577 2576
2578 2577 ASSERT(mutex_owned(&stmf_state.stmf_lock));
2579 2578 if (ppd->ppd_provider) {
2580 2579 if (ppd->ppd_lu_provider) {
2581 2580 ((stmf_i_lu_provider_t *)
2582 2581 ppd->ppd_provider)->ilp_ppd = NULL;
2583 2582 } else {
2584 2583 ((stmf_i_port_provider_t *)
2585 2584 ppd->ppd_provider)->ipp_ppd = NULL;
2586 2585 }
2587 2586 ppd->ppd_provider = NULL;
2588 2587 }
2589 2588
2590 2589 for (pppd = &stmf_state.stmf_ppdlist; *pppd != NULL;
2591 2590 pppd = &((*pppd)->ppd_next)) {
2592 2591 if (*pppd == ppd)
2593 2592 break;
2594 2593 }
2595 2594
2596 2595 if (*pppd == NULL)
2597 2596 return;
2598 2597
2599 2598 *pppd = ppd->ppd_next;
2600 2599 nvlist_free(ppd->ppd_nv);
2601 2600
2602 2601 kmem_free(ppd, ppd->ppd_alloc_size);
2603 2602 }
2604 2603
2605 2604 int
2606 2605 stmf_delete_ppd_ioctl(stmf_ppioctl_data_t *ppi)
2607 2606 {
2608 2607 stmf_pp_data_t *ppd;
2609 2608 int ret = ENOENT;
2610 2609
2611 2610 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2612 2611 return (EINVAL);
2613 2612 }
2614 2613
2615 2614 mutex_enter(&stmf_state.stmf_lock);
2616 2615
2617 2616 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2618 2617 if (ppi->ppi_lu_provider) {
2619 2618 if (!ppd->ppd_lu_provider)
2620 2619 continue;
2621 2620 } else if (ppi->ppi_port_provider) {
2622 2621 if (!ppd->ppd_port_provider)
2623 2622 continue;
2624 2623 }
2625 2624 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2626 2625 break;
2627 2626 }
2628 2627
2629 2628 if (ppd) {
2630 2629 ret = 0;
2631 2630 stmf_delete_ppd(ppd);
2632 2631 }
2633 2632 mutex_exit(&stmf_state.stmf_lock);
2634 2633
2635 2634 return (ret);
2636 2635 }
2637 2636
2638 2637 int
2639 2638 stmf_get_ppd_ioctl(stmf_ppioctl_data_t *ppi, stmf_ppioctl_data_t *ppi_out,
2640 2639 uint32_t *err_ret)
2641 2640 {
2642 2641 stmf_pp_data_t *ppd;
2643 2642 size_t req_size;
2644 2643 int ret = ENOENT;
2645 2644 char *bufp = (char *)ppi_out->ppi_data;
2646 2645
2647 2646 if ((ppi->ppi_lu_provider + ppi->ppi_port_provider) != 1) {
2648 2647 return (EINVAL);
2649 2648 }
2650 2649
2651 2650 mutex_enter(&stmf_state.stmf_lock);
2652 2651
2653 2652 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = ppd->ppd_next) {
2654 2653 if (ppi->ppi_lu_provider) {
2655 2654 if (!ppd->ppd_lu_provider)
2656 2655 continue;
2657 2656 } else if (ppi->ppi_port_provider) {
2658 2657 if (!ppd->ppd_port_provider)
2659 2658 continue;
2660 2659 }
2661 2660 if (strncmp(ppi->ppi_name, ppd->ppd_name, 254) == 0)
2662 2661 break;
2663 2662 }
2664 2663
2665 2664 if (ppd && ppd->ppd_nv) {
2666 2665 ppi_out->ppi_token = ppd->ppd_token;
2667 2666 if ((ret = nvlist_size(ppd->ppd_nv, &req_size,
2668 2667 NV_ENCODE_XDR)) != 0) {
2669 2668 goto done;
2670 2669 }
2671 2670 ppi_out->ppi_data_size = req_size;
2672 2671 if (req_size > ppi->ppi_data_size) {
2673 2672 *err_ret = STMF_IOCERR_INSUFFICIENT_BUF;
2674 2673 ret = EINVAL;
2675 2674 goto done;
2676 2675 }
2677 2676
2678 2677 if ((ret = nvlist_pack(ppd->ppd_nv, &bufp, &req_size,
2679 2678 NV_ENCODE_XDR, 0)) != 0) {
2680 2679 goto done;
2681 2680 }
2682 2681 ret = 0;
2683 2682 }
2684 2683
2685 2684 done:
2686 2685 mutex_exit(&stmf_state.stmf_lock);
2687 2686
2688 2687 return (ret);
2689 2688 }
2690 2689
2691 2690 void
2692 2691 stmf_delete_all_ppds()
2693 2692 {
2694 2693 stmf_pp_data_t *ppd, *nppd;
2695 2694
2696 2695 ASSERT(mutex_owned(&stmf_state.stmf_lock));
2697 2696 for (ppd = stmf_state.stmf_ppdlist; ppd != NULL; ppd = nppd) {
2698 2697 nppd = ppd->ppd_next;
2699 2698 stmf_delete_ppd(ppd);
2700 2699 }
2701 2700 }
2702 2701
2703 2702 /*
2704 2703 * 16 is the max string length of a protocol_ident, increase
2705 2704 * the size if needed.
2706 2705 */
2707 2706 #define STMF_KSTAT_LU_SZ (STMF_GUID_INPUT + 1 + 256)
2708 2707 #define STMF_KSTAT_TGT_SZ (256 * 2 + 16)
2709 2708
2710 2709 /*
2711 2710 * This array matches the Protocol Identifier in stmf_ioctl.h
2712 2711 */
2713 2712 #define MAX_PROTO_STR_LEN 32
2714 2713
2715 2714 char *protocol_ident[PROTOCOL_ANY] = {
2716 2715 "Fibre Channel",
2717 2716 "Parallel SCSI",
2718 2717 "SSA",
2719 2718 "IEEE_1394",
2720 2719 "SRP",
2721 2720 "iSCSI",
2722 2721 "SAS",
2723 2722 "ADT",
2724 2723 "ATAPI",
2725 2724 "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN", "UNKNOWN"
2726 2725 };
2727 2726
2728 2727 /*
2729 2728 * Update the lun wait/run queue count
2730 2729 */
2731 2730 static void
2732 2731 stmf_update_kstat_lu_q(scsi_task_t *task, void func())
2733 2732 {
2734 2733 stmf_i_lu_t *ilu;
2735 2734 kstat_io_t *kip;
2736 2735
2737 2736 if (task->task_lu == dlun0)
2738 2737 return;
2739 2738 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2740 2739 if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2741 2740 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2742 2741 if (kip != NULL) {
2743 2742 func(kip);
2744 2743 }
2745 2744 }
2746 2745 }
2747 2746
2748 2747 /*
2749 2748 * Update the target(lport) wait/run queue count
2750 2749 */
2751 2750 static void
2752 2751 stmf_update_kstat_lport_q(scsi_task_t *task, void func())
2753 2752 {
2754 2753 stmf_i_local_port_t *ilp;
2755 2754 kstat_io_t *kip;
2756 2755
2757 2756 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2758 2757 if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2759 2758 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2760 2759 if (kip != NULL) {
2761 2760 mutex_enter(ilp->ilport_kstat_io->ks_lock);
2762 2761 func(kip);
2763 2762 mutex_exit(ilp->ilport_kstat_io->ks_lock);
2764 2763 }
2765 2764 }
2766 2765 }
2767 2766
2768 2767 static void
2769 2768 stmf_update_kstat_lport_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2770 2769 {
2771 2770 stmf_i_local_port_t *ilp;
2772 2771 kstat_io_t *kip;
2773 2772
2774 2773 ilp = (stmf_i_local_port_t *)task->task_lport->lport_stmf_private;
2775 2774 if (ilp != NULL && ilp->ilport_kstat_io != NULL) {
2776 2775 kip = KSTAT_IO_PTR(ilp->ilport_kstat_io);
2777 2776 if (kip != NULL) {
2778 2777 mutex_enter(ilp->ilport_kstat_io->ks_lock);
2779 2778 STMF_UPDATE_KSTAT_IO(kip, dbuf);
2780 2779 mutex_exit(ilp->ilport_kstat_io->ks_lock);
2781 2780 }
2782 2781 }
2783 2782 }
2784 2783
2785 2784 static void
2786 2785 stmf_update_kstat_lu_io(scsi_task_t *task, stmf_data_buf_t *dbuf)
2787 2786 {
2788 2787 stmf_i_lu_t *ilu;
2789 2788 kstat_io_t *kip;
2790 2789
2791 2790 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
2792 2791 if (ilu != NULL && ilu->ilu_kstat_io != NULL) {
2793 2792 kip = KSTAT_IO_PTR(ilu->ilu_kstat_io);
2794 2793 if (kip != NULL) {
2795 2794 mutex_enter(ilu->ilu_kstat_io->ks_lock);
2796 2795 STMF_UPDATE_KSTAT_IO(kip, dbuf);
2797 2796 mutex_exit(ilu->ilu_kstat_io->ks_lock);
2798 2797 }
2799 2798 }
2800 2799 }
2801 2800
2802 2801 static void
2803 2802 stmf_create_kstat_lu(stmf_i_lu_t *ilu)
2804 2803 {
2805 2804 char ks_nm[KSTAT_STRLEN];
2806 2805 stmf_kstat_lu_info_t *ks_lu;
2807 2806
2808 2807 /* create kstat lun info */
2809 2808 ks_lu = (stmf_kstat_lu_info_t *)kmem_zalloc(STMF_KSTAT_LU_SZ,
2810 2809 KM_NOSLEEP);
2811 2810 if (ks_lu == NULL) {
2812 2811 cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2813 2812 return;
2814 2813 }
2815 2814
2816 2815 bzero(ks_nm, sizeof (ks_nm));
2817 2816 (void) sprintf(ks_nm, "stmf_lu_%"PRIxPTR"", (uintptr_t)ilu);
2818 2817 if ((ilu->ilu_kstat_info = kstat_create(STMF_MODULE_NAME, 0,
2819 2818 ks_nm, "misc", KSTAT_TYPE_NAMED,
2820 2819 sizeof (stmf_kstat_lu_info_t) / sizeof (kstat_named_t),
2821 2820 KSTAT_FLAG_VIRTUAL)) == NULL) {
2822 2821 kmem_free(ks_lu, STMF_KSTAT_LU_SZ);
2823 2822 cmn_err(CE_WARN, "STMF: kstat_create lu failed");
2824 2823 return;
2825 2824 }
2826 2825
2827 2826 ilu->ilu_kstat_info->ks_data_size = STMF_KSTAT_LU_SZ;
2828 2827 ilu->ilu_kstat_info->ks_data = ks_lu;
2829 2828
2830 2829 kstat_named_init(&ks_lu->i_lun_guid, "lun-guid",
2831 2830 KSTAT_DATA_STRING);
2832 2831 kstat_named_init(&ks_lu->i_lun_alias, "lun-alias",
2833 2832 KSTAT_DATA_STRING);
2834 2833
2835 2834 /* convert guid to hex string */
2836 2835 int i;
2837 2836 uint8_t *p = ilu->ilu_lu->lu_id->ident;
2838 2837 bzero(ilu->ilu_ascii_hex_guid, sizeof (ilu->ilu_ascii_hex_guid));
2839 2838 for (i = 0; i < STMF_GUID_INPUT / 2; i++) {
2840 2839 (void) sprintf(&ilu->ilu_ascii_hex_guid[i * 2], "%02x", p[i]);
2841 2840 }
2842 2841 kstat_named_setstr(&ks_lu->i_lun_guid,
2843 2842 (const char *)ilu->ilu_ascii_hex_guid);
2844 2843 kstat_named_setstr(&ks_lu->i_lun_alias,
2845 2844 (const char *)ilu->ilu_lu->lu_alias);
2846 2845 kstat_install(ilu->ilu_kstat_info);
2847 2846
2848 2847 /* create kstat lun io */
2849 2848 bzero(ks_nm, sizeof (ks_nm));
2850 2849 (void) sprintf(ks_nm, "stmf_lu_io_%"PRIxPTR"", (uintptr_t)ilu);
2851 2850 if ((ilu->ilu_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2852 2851 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2853 2852 cmn_err(CE_WARN, "STMF: kstat_create lu_io failed");
2854 2853 return;
2855 2854 }
2856 2855 mutex_init(&ilu->ilu_kstat_lock, NULL, MUTEX_DRIVER, 0);
2857 2856 ilu->ilu_kstat_io->ks_lock = &ilu->ilu_kstat_lock;
2858 2857 kstat_install(ilu->ilu_kstat_io);
2859 2858 }
2860 2859
2861 2860 static void
2862 2861 stmf_create_kstat_lport(stmf_i_local_port_t *ilport)
2863 2862 {
2864 2863 char ks_nm[KSTAT_STRLEN];
2865 2864 stmf_kstat_tgt_info_t *ks_tgt;
2866 2865 int id, len;
2867 2866
2868 2867 /* create kstat lport info */
2869 2868 ks_tgt = (stmf_kstat_tgt_info_t *)kmem_zalloc(STMF_KSTAT_TGT_SZ,
2870 2869 KM_NOSLEEP);
2871 2870 if (ks_tgt == NULL) {
2872 2871 cmn_err(CE_WARN, "STMF: kmem_zalloc failed");
2873 2872 return;
2874 2873 }
2875 2874
2876 2875 bzero(ks_nm, sizeof (ks_nm));
2877 2876 (void) sprintf(ks_nm, "stmf_tgt_%"PRIxPTR"", (uintptr_t)ilport);
2878 2877 if ((ilport->ilport_kstat_info = kstat_create(STMF_MODULE_NAME,
2879 2878 0, ks_nm, "misc", KSTAT_TYPE_NAMED,
2880 2879 sizeof (stmf_kstat_tgt_info_t) / sizeof (kstat_named_t),
2881 2880 KSTAT_FLAG_VIRTUAL)) == NULL) {
2882 2881 kmem_free(ks_tgt, STMF_KSTAT_TGT_SZ);
2883 2882 cmn_err(CE_WARN, "STMF: kstat_create target failed");
2884 2883 return;
2885 2884 }
2886 2885
2887 2886 ilport->ilport_kstat_info->ks_data_size = STMF_KSTAT_TGT_SZ;
2888 2887 ilport->ilport_kstat_info->ks_data = ks_tgt;
2889 2888
2890 2889 kstat_named_init(&ks_tgt->i_tgt_name, "target-name",
2891 2890 KSTAT_DATA_STRING);
2892 2891 kstat_named_init(&ks_tgt->i_tgt_alias, "target-alias",
2893 2892 KSTAT_DATA_STRING);
2894 2893 kstat_named_init(&ks_tgt->i_protocol, "protocol",
2895 2894 KSTAT_DATA_STRING);
2896 2895
2897 2896 /* ident might not be null terminated */
2898 2897 len = ilport->ilport_lport->lport_id->ident_length;
2899 2898 bcopy(ilport->ilport_lport->lport_id->ident,
2900 2899 ilport->ilport_kstat_tgt_name, len);
2901 2900 ilport->ilport_kstat_tgt_name[len + 1] = NULL;
2902 2901 kstat_named_setstr(&ks_tgt->i_tgt_name,
2903 2902 (const char *)ilport->ilport_kstat_tgt_name);
2904 2903 kstat_named_setstr(&ks_tgt->i_tgt_alias,
2905 2904 (const char *)ilport->ilport_lport->lport_alias);
2906 2905 /* protocol */
2907 2906 if ((id = ilport->ilport_lport->lport_id->protocol_id) > PROTOCOL_ANY) {
2908 2907 cmn_err(CE_WARN, "STMF: protocol_id out of bound");
2909 2908 id = PROTOCOL_ANY;
2910 2909 }
2911 2910 kstat_named_setstr(&ks_tgt->i_protocol, protocol_ident[id]);
2912 2911 kstat_install(ilport->ilport_kstat_info);
2913 2912
2914 2913 /* create kstat lport io */
2915 2914 bzero(ks_nm, sizeof (ks_nm));
2916 2915 (void) sprintf(ks_nm, "stmf_tgt_io_%"PRIxPTR"", (uintptr_t)ilport);
2917 2916 if ((ilport->ilport_kstat_io = kstat_create(STMF_MODULE_NAME, 0,
2918 2917 ks_nm, "io", KSTAT_TYPE_IO, 1, 0)) == NULL) {
2919 2918 cmn_err(CE_WARN, "STMF: kstat_create target_io failed");
2920 2919 return;
2921 2920 }
2922 2921 mutex_init(&ilport->ilport_kstat_lock, NULL, MUTEX_DRIVER, 0);
2923 2922 ilport->ilport_kstat_io->ks_lock = &ilport->ilport_kstat_lock;
2924 2923 kstat_install(ilport->ilport_kstat_io);
2925 2924 }
2926 2925
2927 2926 /*
2928 2927 * set the asymmetric access state for a logical unit
2929 2928 * caller is responsible for establishing SCSI unit attention on
2930 2929 * state change
2931 2930 */
2932 2931 stmf_status_t
2933 2932 stmf_set_lu_access(stmf_lu_t *lu, uint8_t access_state)
2934 2933 {
2935 2934 stmf_i_lu_t *ilu;
2936 2935 uint8_t *p1, *p2;
2937 2936
2938 2937 if ((access_state != STMF_LU_STANDBY) &&
2939 2938 (access_state != STMF_LU_ACTIVE)) {
2940 2939 return (STMF_INVALID_ARG);
2941 2940 }
2942 2941
2943 2942 p1 = &lu->lu_id->ident[0];
2944 2943 mutex_enter(&stmf_state.stmf_lock);
2945 2944 if (stmf_state.stmf_inventory_locked) {
2946 2945 mutex_exit(&stmf_state.stmf_lock);
2947 2946 return (STMF_BUSY);
2948 2947 }
2949 2948
2950 2949 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
2951 2950 p2 = &ilu->ilu_lu->lu_id->ident[0];
2952 2951 if (bcmp(p1, p2, 16) == 0) {
2953 2952 break;
2954 2953 }
2955 2954 }
2956 2955
2957 2956 if (!ilu) {
2958 2957 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
2959 2958 } else {
2960 2959 /*
2961 2960 * We're changing access state on an existing logical unit
2962 2961 * Send the proxy registration message for this logical unit
2963 2962 * if we're in alua mode.
2964 2963 * If the requested state is STMF_LU_ACTIVE, we want to register
2965 2964 * this logical unit.
2966 2965 * If the requested state is STMF_LU_STANDBY, we're going to
2967 2966 * abort all tasks for this logical unit.
2968 2967 */
2969 2968 if (stmf_state.stmf_alua_state == 1 &&
2970 2969 access_state == STMF_LU_ACTIVE) {
2971 2970 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
2972 2971 stmf_ic_msg_t *ic_reg_lun;
2973 2972 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
2974 2973 lu->lu_lp->lp_alua_support) {
2975 2974 ilu->ilu_alua = 1;
2976 2975 /* allocate the register message */
2977 2976 ic_reg_lun = ic_lun_active_msg_alloc(p1,
2978 2977 lu->lu_lp->lp_name,
2979 2978 lu->lu_proxy_reg_arg_len,
2980 2979 (uint8_t *)lu->lu_proxy_reg_arg,
2981 2980 stmf_proxy_msg_id);
2982 2981 /* send the message */
2983 2982 if (ic_reg_lun) {
2984 2983 ic_ret = ic_tx_msg(ic_reg_lun);
2985 2984 if (ic_ret == STMF_IC_MSG_SUCCESS) {
2986 2985 stmf_proxy_msg_id++;
2987 2986 }
2988 2987 }
2989 2988 }
2990 2989 } else if (stmf_state.stmf_alua_state == 1 &&
2991 2990 access_state == STMF_LU_STANDBY) {
2992 2991 /* abort all tasks for this lu */
2993 2992 stmf_task_lu_killall(lu, NULL, STMF_ABORTED);
2994 2993 }
2995 2994 }
2996 2995
2997 2996 ilu->ilu_access = access_state;
2998 2997
2999 2998 mutex_exit(&stmf_state.stmf_lock);
3000 2999 return (STMF_SUCCESS);
3001 3000 }
3002 3001
3003 3002
3004 3003 stmf_status_t
3005 3004 stmf_register_lu(stmf_lu_t *lu)
3006 3005 {
3007 3006 stmf_i_lu_t *ilu;
3008 3007 uint8_t *p1, *p2;
3009 3008 stmf_state_change_info_t ssci;
3010 3009 stmf_id_data_t *luid;
3011 3010
3012 3011 if ((lu->lu_id->ident_type != ID_TYPE_NAA) ||
3013 3012 (lu->lu_id->ident_length != 16) ||
3014 3013 ((lu->lu_id->ident[0] & 0xf0) != 0x60)) {
3015 3014 return (STMF_INVALID_ARG);
3016 3015 }
3017 3016 p1 = &lu->lu_id->ident[0];
3018 3017 mutex_enter(&stmf_state.stmf_lock);
3019 3018 if (stmf_state.stmf_inventory_locked) {
3020 3019 mutex_exit(&stmf_state.stmf_lock);
3021 3020 return (STMF_BUSY);
3022 3021 }
3023 3022
3024 3023 for (ilu = stmf_state.stmf_ilulist; ilu != NULL; ilu = ilu->ilu_next) {
3025 3024 p2 = &ilu->ilu_lu->lu_id->ident[0];
3026 3025 if (bcmp(p1, p2, 16) == 0) {
3027 3026 mutex_exit(&stmf_state.stmf_lock);
3028 3027 return (STMF_ALREADY);
3029 3028 }
3030 3029 }
3031 3030
3032 3031 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3033 3032 luid = stmf_lookup_id(&stmf_state.stmf_luid_list,
3034 3033 lu->lu_id->ident_length, lu->lu_id->ident);
3035 3034 if (luid) {
3036 3035 luid->id_pt_to_object = (void *)ilu;
3037 3036 ilu->ilu_luid = luid;
3038 3037 }
3039 3038 ilu->ilu_alias = NULL;
3040 3039
3041 3040 ilu->ilu_next = stmf_state.stmf_ilulist;
3042 3041 ilu->ilu_prev = NULL;
3043 3042 if (ilu->ilu_next)
3044 3043 ilu->ilu_next->ilu_prev = ilu;
3045 3044 stmf_state.stmf_ilulist = ilu;
3046 3045 stmf_state.stmf_nlus++;
3047 3046 if (lu->lu_lp) {
3048 3047 ((stmf_i_lu_provider_t *)
3049 3048 (lu->lu_lp->lp_stmf_private))->ilp_nlus++;
3050 3049 }
3051 3050 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
3052 3051 STMF_EVENT_ALLOC_HANDLE(ilu->ilu_event_hdl);
3053 3052 cv_init(&ilu->ilu_offline_pending_cv, NULL, CV_DRIVER, NULL);
3054 3053 stmf_create_kstat_lu(ilu);
3055 3054 /*
3056 3055 * register with proxy module if available and logical unit
3057 3056 * is in active state
3058 3057 */
3059 3058 if (stmf_state.stmf_alua_state == 1 &&
3060 3059 ilu->ilu_access == STMF_LU_ACTIVE) {
3061 3060 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3062 3061 stmf_ic_msg_t *ic_reg_lun;
3063 3062 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3064 3063 lu->lu_lp->lp_alua_support) {
3065 3064 ilu->ilu_alua = 1;
3066 3065 /* allocate the register message */
3067 3066 ic_reg_lun = ic_reg_lun_msg_alloc(p1,
3068 3067 lu->lu_lp->lp_name, lu->lu_proxy_reg_arg_len,
3069 3068 (uint8_t *)lu->lu_proxy_reg_arg, stmf_proxy_msg_id);
3070 3069 /* send the message */
3071 3070 if (ic_reg_lun) {
3072 3071 ic_ret = ic_tx_msg(ic_reg_lun);
3073 3072 if (ic_ret == STMF_IC_MSG_SUCCESS) {
3074 3073 stmf_proxy_msg_id++;
3075 3074 }
3076 3075 }
3077 3076 }
3078 3077 }
3079 3078 mutex_exit(&stmf_state.stmf_lock);
3080 3079
3081 3080 /* check the default state for lu */
3082 3081 if (stmf_state.stmf_default_lu_state == STMF_STATE_OFFLINE) {
3083 3082 ilu->ilu_prev_state = STMF_STATE_OFFLINE;
3084 3083 } else {
3085 3084 ilu->ilu_prev_state = STMF_STATE_ONLINE;
3086 3085 if (stmf_state.stmf_service_running) {
3087 3086 ssci.st_rflags = 0;
3088 3087 ssci.st_additional_info = NULL;
3089 3088 (void) stmf_ctl(STMF_CMD_LU_ONLINE, lu, &ssci);
3090 3089 }
3091 3090 }
3092 3091
3093 3092 /* XXX: Generate event */
3094 3093 return (STMF_SUCCESS);
3095 3094 }
3096 3095
3097 3096 stmf_status_t
3098 3097 stmf_deregister_lu(stmf_lu_t *lu)
3099 3098 {
3100 3099 stmf_i_lu_t *ilu;
3101 3100
3102 3101 mutex_enter(&stmf_state.stmf_lock);
3103 3102 if (stmf_state.stmf_inventory_locked) {
3104 3103 mutex_exit(&stmf_state.stmf_lock);
3105 3104 return (STMF_BUSY);
3106 3105 }
3107 3106 ilu = stmf_lookup_lu(lu);
3108 3107 if (ilu == NULL) {
3109 3108 mutex_exit(&stmf_state.stmf_lock);
3110 3109 return (STMF_INVALID_ARG);
3111 3110 }
3112 3111 if (ilu->ilu_state == STMF_STATE_OFFLINE) {
3113 3112 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
3114 3113 while (ilu->ilu_flags & ILU_STALL_DEREGISTER) {
3115 3114 cv_wait(&stmf_state.stmf_cv, &stmf_state.stmf_lock);
3116 3115 }
3117 3116 if (ilu->ilu_ntasks) {
3118 3117 stmf_i_scsi_task_t *itask, *nitask;
3119 3118
3120 3119 nitask = ilu->ilu_tasks;
3121 3120 do {
3122 3121 itask = nitask;
3123 3122 nitask = itask->itask_lu_next;
3124 3123 lu->lu_task_free(itask->itask_task);
3125 3124 stmf_free(itask->itask_task);
3126 3125 } while (nitask != NULL);
3127 3126
3128 3127 ilu->ilu_tasks = ilu->ilu_free_tasks = NULL;
3129 3128 ilu->ilu_ntasks = ilu->ilu_ntasks_free = 0;
3130 3129 }
3131 3130 /* de-register with proxy if available */
3132 3131 if (ilu->ilu_access == STMF_LU_ACTIVE &&
3133 3132 stmf_state.stmf_alua_state == 1) {
3134 3133 /* de-register with proxy module */
3135 3134 stmf_ic_msg_status_t ic_ret = STMF_IC_MSG_SUCCESS;
3136 3135 stmf_ic_msg_t *ic_dereg_lun;
3137 3136 if (lu->lu_lp && lu->lu_lp->lp_lpif_rev == LPIF_REV_2 &&
3138 3137 lu->lu_lp->lp_alua_support) {
3139 3138 ilu->ilu_alua = 1;
3140 3139 /* allocate the de-register message */
3141 3140 ic_dereg_lun = ic_dereg_lun_msg_alloc(
3142 3141 lu->lu_id->ident, lu->lu_lp->lp_name, 0,
3143 3142 NULL, stmf_proxy_msg_id);
3144 3143 /* send the message */
3145 3144 if (ic_dereg_lun) {
3146 3145 ic_ret = ic_tx_msg(ic_dereg_lun);
3147 3146 if (ic_ret == STMF_IC_MSG_SUCCESS) {
3148 3147 stmf_proxy_msg_id++;
3149 3148 }
3150 3149 }
3151 3150 }
3152 3151 }
3153 3152
3154 3153 if (ilu->ilu_next)
3155 3154 ilu->ilu_next->ilu_prev = ilu->ilu_prev;
3156 3155 if (ilu->ilu_prev)
3157 3156 ilu->ilu_prev->ilu_next = ilu->ilu_next;
3158 3157 else
3159 3158 stmf_state.stmf_ilulist = ilu->ilu_next;
3160 3159 stmf_state.stmf_nlus--;
3161 3160
3162 3161 if (ilu == stmf_state.stmf_svc_ilu_draining) {
3163 3162 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
3164 3163 }
3165 3164 if (ilu == stmf_state.stmf_svc_ilu_timing) {
3166 3165 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
3167 3166 }
3168 3167 if (lu->lu_lp) {
3169 3168 ((stmf_i_lu_provider_t *)
3170 3169 (lu->lu_lp->lp_stmf_private))->ilp_nlus--;
3171 3170 }
3172 3171 if (ilu->ilu_luid) {
3173 3172 ((stmf_id_data_t *)ilu->ilu_luid)->id_pt_to_object =
3174 3173 NULL;
3175 3174 ilu->ilu_luid = NULL;
3176 3175 }
3177 3176 STMF_EVENT_FREE_HANDLE(ilu->ilu_event_hdl);
3178 3177 } else {
3179 3178 mutex_exit(&stmf_state.stmf_lock);
3180 3179 return (STMF_BUSY);
3181 3180 }
3182 3181 if (ilu->ilu_kstat_info) {
3183 3182 kmem_free(ilu->ilu_kstat_info->ks_data, STMF_KSTAT_LU_SZ);
3184 3183 kstat_delete(ilu->ilu_kstat_info);
3185 3184 }
3186 3185 if (ilu->ilu_kstat_io) {
3187 3186 kstat_delete(ilu->ilu_kstat_io);
3188 3187 mutex_destroy(&ilu->ilu_kstat_lock);
3189 3188 }
3190 3189 cv_destroy(&ilu->ilu_offline_pending_cv);
3191 3190 mutex_exit(&stmf_state.stmf_lock);
3192 3191 return (STMF_SUCCESS);
3193 3192 }
3194 3193
3195 3194 void
3196 3195 stmf_set_port_standby(stmf_local_port_t *lport, uint16_t rtpid)
3197 3196 {
3198 3197 stmf_i_local_port_t *ilport =
3199 3198 (stmf_i_local_port_t *)lport->lport_stmf_private;
3200 3199 ilport->ilport_rtpid = rtpid;
3201 3200 ilport->ilport_standby = 1;
3202 3201 }
3203 3202
3204 3203 void
3205 3204 stmf_set_port_alua(stmf_local_port_t *lport)
3206 3205 {
3207 3206 stmf_i_local_port_t *ilport =
3208 3207 (stmf_i_local_port_t *)lport->lport_stmf_private;
3209 3208 ilport->ilport_alua = 1;
3210 3209 }
3211 3210
3212 3211 stmf_status_t
3213 3212 stmf_register_local_port(stmf_local_port_t *lport)
3214 3213 {
3215 3214 stmf_i_local_port_t *ilport;
3216 3215 stmf_state_change_info_t ssci;
3217 3216 int start_workers = 0;
3218 3217
3219 3218 mutex_enter(&stmf_state.stmf_lock);
3220 3219 if (stmf_state.stmf_inventory_locked) {
3221 3220 mutex_exit(&stmf_state.stmf_lock);
3222 3221 return (STMF_BUSY);
3223 3222 }
3224 3223 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3225 3224 rw_init(&ilport->ilport_lock, NULL, RW_DRIVER, NULL);
3226 3225
3227 3226 ilport->ilport_instance =
3228 3227 id_alloc_nosleep(stmf_state.stmf_ilport_inst_space);
3229 3228 if (ilport->ilport_instance == -1) {
3230 3229 mutex_exit(&stmf_state.stmf_lock);
3231 3230 return (STMF_FAILURE);
3232 3231 }
3233 3232 ilport->ilport_next = stmf_state.stmf_ilportlist;
3234 3233 ilport->ilport_prev = NULL;
3235 3234 if (ilport->ilport_next)
3236 3235 ilport->ilport_next->ilport_prev = ilport;
3237 3236 stmf_state.stmf_ilportlist = ilport;
3238 3237 stmf_state.stmf_nlports++;
3239 3238 if (lport->lport_pp) {
3240 3239 ((stmf_i_port_provider_t *)
3241 3240 (lport->lport_pp->pp_stmf_private))->ipp_npps++;
3242 3241 }
3243 3242 ilport->ilport_tg =
3244 3243 stmf_lookup_group_for_target(lport->lport_id->ident,
3245 3244 lport->lport_id->ident_length);
3246 3245
3247 3246 /*
3248 3247 * rtpid will/must be set if this is a standby port
3249 3248 * only register ports that are not standby (proxy) ports
3250 3249 * and ports that are alua participants (ilport_alua == 1)
3251 3250 */
3252 3251 if (ilport->ilport_standby == 0) {
3253 3252 ilport->ilport_rtpid = atomic_inc_16_nv(&stmf_rtpid_counter);
3254 3253 }
3255 3254
3256 3255 if (stmf_state.stmf_alua_state == 1 &&
3257 3256 ilport->ilport_standby == 0 &&
3258 3257 ilport->ilport_alua == 1) {
3259 3258 stmf_ic_msg_t *ic_reg_port;
3260 3259 stmf_ic_msg_status_t ic_ret;
3261 3260 stmf_local_port_t *lport;
3262 3261 lport = ilport->ilport_lport;
3263 3262 ic_reg_port = ic_reg_port_msg_alloc(
3264 3263 lport->lport_id, ilport->ilport_rtpid,
3265 3264 0, NULL, stmf_proxy_msg_id);
3266 3265 if (ic_reg_port) {
3267 3266 ic_ret = ic_tx_msg(ic_reg_port);
3268 3267 if (ic_ret == STMF_IC_MSG_SUCCESS) {
3269 3268 ilport->ilport_reg_msgid = stmf_proxy_msg_id++;
3270 3269 } else {
3271 3270 cmn_err(CE_WARN, "error on port registration "
3272 3271 "port - %s", ilport->ilport_kstat_tgt_name);
3273 3272 }
3274 3273 }
3275 3274 }
3276 3275 STMF_EVENT_ALLOC_HANDLE(ilport->ilport_event_hdl);
3277 3276 stmf_create_kstat_lport(ilport);
3278 3277 if (stmf_workers_state == STMF_WORKERS_DISABLED) {
3279 3278 stmf_workers_state = STMF_WORKERS_ENABLING;
3280 3279 start_workers = 1;
3281 3280 }
3282 3281 mutex_exit(&stmf_state.stmf_lock);
3283 3282
3284 3283 if (start_workers)
3285 3284 stmf_worker_init();
3286 3285
3287 3286 /* the default state of LPORT */
3288 3287
3289 3288 if (stmf_state.stmf_default_lport_state == STMF_STATE_OFFLINE) {
3290 3289 ilport->ilport_prev_state = STMF_STATE_OFFLINE;
3291 3290 } else {
3292 3291 ilport->ilport_prev_state = STMF_STATE_ONLINE;
3293 3292 if (stmf_state.stmf_service_running) {
3294 3293 ssci.st_rflags = 0;
3295 3294 ssci.st_additional_info = NULL;
3296 3295 (void) stmf_ctl(STMF_CMD_LPORT_ONLINE, lport, &ssci);
3297 3296 }
3298 3297 }
3299 3298
3300 3299 /* XXX: Generate event */
3301 3300 return (STMF_SUCCESS);
3302 3301 }
3303 3302
3304 3303 stmf_status_t
3305 3304 stmf_deregister_local_port(stmf_local_port_t *lport)
3306 3305 {
3307 3306 stmf_i_local_port_t *ilport;
3308 3307
3309 3308 mutex_enter(&stmf_state.stmf_lock);
3310 3309 if (stmf_state.stmf_inventory_locked) {
3311 3310 mutex_exit(&stmf_state.stmf_lock);
3312 3311 return (STMF_BUSY);
3313 3312 }
3314 3313
3315 3314 /* dequeue all object requests from active queue */
3316 3315 stmf_svc_kill_obj_requests(lport);
3317 3316
3318 3317 ilport = (stmf_i_local_port_t *)lport->lport_stmf_private;
3319 3318
3320 3319 /*
3321 3320 * deregister ports that are not standby (proxy)
3322 3321 */
3323 3322 if (stmf_state.stmf_alua_state == 1 &&
3324 3323 ilport->ilport_standby == 0 &&
3325 3324 ilport->ilport_alua == 1) {
3326 3325 stmf_ic_msg_t *ic_dereg_port;
3327 3326 stmf_ic_msg_status_t ic_ret;
3328 3327 ic_dereg_port = ic_dereg_port_msg_alloc(
3329 3328 lport->lport_id, 0, NULL, stmf_proxy_msg_id);
3330 3329 if (ic_dereg_port) {
3331 3330 ic_ret = ic_tx_msg(ic_dereg_port);
3332 3331 if (ic_ret == STMF_IC_MSG_SUCCESS) {
3333 3332 stmf_proxy_msg_id++;
3334 3333 }
3335 3334 }
3336 3335 }
3337 3336
3338 3337 if (ilport->ilport_nsessions == 0) {
3339 3338 if (ilport->ilport_next)
3340 3339 ilport->ilport_next->ilport_prev = ilport->ilport_prev;
3341 3340 if (ilport->ilport_prev)
3342 3341 ilport->ilport_prev->ilport_next = ilport->ilport_next;
3343 3342 else
3344 3343 stmf_state.stmf_ilportlist = ilport->ilport_next;
3345 3344 id_free(stmf_state.stmf_ilport_inst_space,
3346 3345 ilport->ilport_instance);
3347 3346 rw_destroy(&ilport->ilport_lock);
3348 3347 stmf_state.stmf_nlports--;
3349 3348 if (lport->lport_pp) {
3350 3349 ((stmf_i_port_provider_t *)
3351 3350 (lport->lport_pp->pp_stmf_private))->ipp_npps--;
3352 3351 }
3353 3352 ilport->ilport_tg = NULL;
3354 3353 STMF_EVENT_FREE_HANDLE(ilport->ilport_event_hdl);
3355 3354 } else {
3356 3355 mutex_exit(&stmf_state.stmf_lock);
3357 3356 return (STMF_BUSY);
3358 3357 }
3359 3358 if (ilport->ilport_kstat_info) {
3360 3359 kmem_free(ilport->ilport_kstat_info->ks_data,
3361 3360 STMF_KSTAT_TGT_SZ);
3362 3361 kstat_delete(ilport->ilport_kstat_info);
3363 3362 }
3364 3363 if (ilport->ilport_kstat_io) {
3365 3364 kstat_delete(ilport->ilport_kstat_io);
3366 3365 mutex_destroy(&ilport->ilport_kstat_lock);
3367 3366 }
3368 3367 mutex_exit(&stmf_state.stmf_lock);
3369 3368 return (STMF_SUCCESS);
3370 3369 }
3371 3370
3372 3371 /*
3373 3372 * Rport id/instance mappings remain valid until STMF is unloaded
3374 3373 */
3375 3374 static int
3376 3375 stmf_irport_compare(const void *void_irport1, const void *void_irport2)
3377 3376 {
3378 3377 const stmf_i_remote_port_t *irport1 = void_irport1;
3379 3378 const stmf_i_remote_port_t *irport2 = void_irport2;
3380 3379 int result;
3381 3380
3382 3381 /* Sort by code set then ident */
3383 3382 if (irport1->irport_id->code_set <
3384 3383 irport2->irport_id->code_set) {
3385 3384 return (-1);
3386 3385 } else if (irport1->irport_id->code_set >
3387 3386 irport2->irport_id->code_set) {
3388 3387 return (1);
3389 3388 }
3390 3389
3391 3390 /* Next by ident length */
3392 3391 if (irport1->irport_id->ident_length <
3393 3392 irport2->irport_id->ident_length) {
3394 3393 return (-1);
3395 3394 } else if (irport1->irport_id->ident_length >
3396 3395 irport2->irport_id->ident_length) {
3397 3396 return (1);
3398 3397 }
3399 3398
3400 3399 /* Code set and ident length both match, now compare idents */
3401 3400 result = memcmp(irport1->irport_id->ident,
3402 3401 irport2->irport_id->ident,
3403 3402 irport1->irport_id->ident_length);
3404 3403
3405 3404 if (result < 0) {
3406 3405 return (-1);
3407 3406 } else if (result > 0) {
3408 3407 return (1);
3409 3408 }
3410 3409
3411 3410 return (0);
3412 3411 }
3413 3412
3414 3413 static stmf_i_remote_port_t *
3415 3414 stmf_irport_create(scsi_devid_desc_t *rport_devid)
3416 3415 {
3417 3416 int alloc_len;
3418 3417 stmf_i_remote_port_t *irport;
3419 3418
3420 3419 /*
3421 3420 * Lookup will bump the refcnt if there's an existing rport
3422 3421 * context for this identifier.
3423 3422 */
3424 3423 ASSERT(mutex_owned(&stmf_state.stmf_lock));
3425 3424
3426 3425 alloc_len = sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3427 3426 rport_devid->ident_length - 1;
3428 3427 irport = kmem_zalloc(alloc_len, KM_NOSLEEP);
3429 3428 if (irport == NULL) {
3430 3429 return (NULL);
3431 3430 }
3432 3431
3433 3432 irport->irport_instance =
3434 3433 id_alloc_nosleep(stmf_state.stmf_irport_inst_space);
3435 3434 if (irport->irport_instance == -1) {
3436 3435 kmem_free(irport, alloc_len);
3437 3436 return (NULL);
3438 3437 }
3439 3438
3440 3439 irport->irport_id =
3441 3440 (struct scsi_devid_desc *)(irport + 1); /* Ptr. Arith. */
3442 3441 bcopy(rport_devid, irport->irport_id,
3443 3442 sizeof (scsi_devid_desc_t) + rport_devid->ident_length - 1);
3444 3443 irport->irport_refcnt = 1;
3445 3444 mutex_init(&irport->irport_mutex, NULL, MUTEX_DEFAULT, NULL);
3446 3445
3447 3446 return (irport);
3448 3447 }
3449 3448
3450 3449 static void
3451 3450 stmf_irport_destroy(stmf_i_remote_port_t *irport)
3452 3451 {
3453 3452 id_free(stmf_state.stmf_irport_inst_space, irport->irport_instance);
3454 3453 mutex_destroy(&irport->irport_mutex);
3455 3454 kmem_free(irport, sizeof (*irport) + sizeof (scsi_devid_desc_t) +
3456 3455 irport->irport_id->ident_length - 1);
3457 3456 }
3458 3457
3459 3458 static stmf_i_remote_port_t *
3460 3459 stmf_irport_register(scsi_devid_desc_t *rport_devid)
3461 3460 {
3462 3461 stmf_i_remote_port_t *irport;
3463 3462
3464 3463 mutex_enter(&stmf_state.stmf_lock);
3465 3464
3466 3465 /*
3467 3466 * Lookup will bump the refcnt if there's an existing rport
3468 3467 * context for this identifier.
3469 3468 */
3470 3469 if ((irport = stmf_irport_lookup_locked(rport_devid)) != NULL) {
3471 3470 mutex_exit(&stmf_state.stmf_lock);
3472 3471 return (irport);
3473 3472 }
3474 3473
3475 3474 irport = stmf_irport_create(rport_devid);
3476 3475 if (irport == NULL) {
3477 3476 mutex_exit(&stmf_state.stmf_lock);
3478 3477 return (NULL);
3479 3478 }
3480 3479
3481 3480 avl_add(&stmf_state.stmf_irportlist, irport);
3482 3481 mutex_exit(&stmf_state.stmf_lock);
3483 3482
3484 3483 return (irport);
3485 3484 }
3486 3485
3487 3486 static stmf_i_remote_port_t *
3488 3487 stmf_irport_lookup_locked(scsi_devid_desc_t *rport_devid)
3489 3488 {
3490 3489 stmf_i_remote_port_t *irport;
3491 3490 stmf_i_remote_port_t tmp_irport;
3492 3491
3493 3492 ASSERT(mutex_owned(&stmf_state.stmf_lock));
3494 3493 tmp_irport.irport_id = rport_devid;
3495 3494 irport = avl_find(&stmf_state.stmf_irportlist, &tmp_irport, NULL);
3496 3495 if (irport != NULL) {
3497 3496 mutex_enter(&irport->irport_mutex);
3498 3497 irport->irport_refcnt++;
3499 3498 mutex_exit(&irport->irport_mutex);
3500 3499 }
3501 3500
3502 3501 return (irport);
3503 3502 }
3504 3503
3505 3504 static void
3506 3505 stmf_irport_deregister(stmf_i_remote_port_t *irport)
3507 3506 {
3508 3507 /*
3509 3508 * If we were actually going to remove unreferenced remote ports
3510 3509 * we would want to acquire stmf_state.stmf_lock before getting
3511 3510 * the irport mutex.
3512 3511 *
3513 3512 * Instead we're just going to leave it there even if unreferenced.
3514 3513 */
3515 3514 mutex_enter(&irport->irport_mutex);
3516 3515 irport->irport_refcnt--;
3517 3516 mutex_exit(&irport->irport_mutex);
3518 3517 }
3519 3518
3520 3519 /*
3521 3520 * Port provider has to make sure that register/deregister session and
3522 3521 * port are serialized calls.
3523 3522 */
3524 3523 stmf_status_t
3525 3524 stmf_register_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3526 3525 {
3527 3526 stmf_i_scsi_session_t *iss;
3528 3527 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3529 3528 lport->lport_stmf_private;
3530 3529 uint8_t lun[8];
3531 3530
3532 3531 /*
3533 3532 * Port state has to be online to register a scsi session. It is
3534 3533 * possible that we started an offline operation and a new SCSI
3535 3534 * session started at the same time (in that case also we are going
3536 3535 * to fail the registeration). But any other state is simply
3537 3536 * a bad port provider implementation.
3538 3537 */
3539 3538 if (ilport->ilport_state != STMF_STATE_ONLINE) {
3540 3539 if (ilport->ilport_state != STMF_STATE_OFFLINING) {
3541 3540 stmf_trace(lport->lport_alias, "Port is trying to "
3542 3541 "register a session while the state is neither "
3543 3542 "online nor offlining");
3544 3543 }
3545 3544 return (STMF_FAILURE);
3546 3545 }
3547 3546 bzero(lun, 8);
3548 3547 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3549 3548 if ((iss->iss_irport = stmf_irport_register(ss->ss_rport_id)) == NULL) {
3550 3549 stmf_trace(lport->lport_alias, "Could not register "
3551 3550 "remote port during session registration");
3552 3551 return (STMF_FAILURE);
3553 3552 }
3554 3553
3555 3554 iss->iss_flags |= ISS_BEING_CREATED;
3556 3555
3557 3556 if (ss->ss_rport == NULL) {
3558 3557 iss->iss_flags |= ISS_NULL_TPTID;
3559 3558 ss->ss_rport = stmf_scsilib_devid_to_remote_port(
3560 3559 ss->ss_rport_id);
3561 3560 if (ss->ss_rport == NULL) {
3562 3561 iss->iss_flags &= ~(ISS_NULL_TPTID | ISS_BEING_CREATED);
3563 3562 stmf_trace(lport->lport_alias, "Device id to "
3564 3563 "remote port conversion failed");
3565 3564 return (STMF_FAILURE);
3566 3565 }
3567 3566 } else {
3568 3567 if (!stmf_scsilib_tptid_validate(ss->ss_rport->rport_tptid,
3569 3568 ss->ss_rport->rport_tptid_sz, NULL)) {
3570 3569 iss->iss_flags &= ~ISS_BEING_CREATED;
3571 3570 stmf_trace(lport->lport_alias, "Remote port "
3572 3571 "transport id validation failed");
3573 3572 return (STMF_FAILURE);
3574 3573 }
3575 3574 }
3576 3575
3577 3576 /* sessions use the ilport_lock. No separate lock is required */
3578 3577 iss->iss_lockp = &ilport->ilport_lock;
3579 3578
3580 3579 if (iss->iss_sm != NULL)
3581 3580 cmn_err(CE_PANIC, "create lun map called with non NULL map");
3582 3581 iss->iss_sm = (stmf_lun_map_t *)kmem_zalloc(sizeof (stmf_lun_map_t),
3583 3582 KM_SLEEP);
3584 3583
3585 3584 mutex_enter(&stmf_state.stmf_lock);
3586 3585 rw_enter(&ilport->ilport_lock, RW_WRITER);
3587 3586 (void) stmf_session_create_lun_map(ilport, iss);
3588 3587 ilport->ilport_nsessions++;
3589 3588 iss->iss_next = ilport->ilport_ss_list;
3590 3589 ilport->ilport_ss_list = iss;
3591 3590 rw_exit(&ilport->ilport_lock);
3592 3591 mutex_exit(&stmf_state.stmf_lock);
3593 3592
3594 3593 iss->iss_creation_time = ddi_get_time();
3595 3594 ss->ss_session_id = atomic_inc_64_nv(&stmf_session_counter);
3596 3595 iss->iss_flags &= ~ISS_BEING_CREATED;
3597 3596 /* XXX should we remove ISS_LUN_INVENTORY_CHANGED on new session? */
3598 3597 iss->iss_flags &= ~ISS_LUN_INVENTORY_CHANGED;
3599 3598 DTRACE_PROBE2(session__online, stmf_local_port_t *, lport,
3600 3599 stmf_scsi_session_t *, ss);
3601 3600 return (STMF_SUCCESS);
3602 3601 }
3603 3602
3604 3603 void
3605 3604 stmf_deregister_scsi_session(stmf_local_port_t *lport, stmf_scsi_session_t *ss)
3606 3605 {
3607 3606 stmf_i_local_port_t *ilport = (stmf_i_local_port_t *)
3608 3607 lport->lport_stmf_private;
3609 3608 stmf_i_scsi_session_t *iss, **ppss;
3610 3609 int found = 0;
3611 3610 stmf_ic_msg_t *ic_session_dereg;
3612 3611 stmf_status_t ic_ret = STMF_FAILURE;
3613 3612
3614 3613 DTRACE_PROBE2(session__offline, stmf_local_port_t *, lport,
3615 3614 stmf_scsi_session_t *, ss);
3616 3615
3617 3616 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3618 3617 if (ss->ss_rport_alias) {
3619 3618 ss->ss_rport_alias = NULL;
3620 3619 }
3621 3620
3622 3621 try_dereg_ss_again:
3623 3622 mutex_enter(&stmf_state.stmf_lock);
3624 3623 atomic_and_32(&iss->iss_flags,
3625 3624 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
3626 3625 if (iss->iss_flags & ISS_EVENT_ACTIVE) {
3627 3626 mutex_exit(&stmf_state.stmf_lock);
3628 3627 delay(1);
3629 3628 goto try_dereg_ss_again;
3630 3629 }
3631 3630
3632 3631 /* dereg proxy session if not standby port */
3633 3632 if (stmf_state.stmf_alua_state == 1 &&
3634 3633 ilport->ilport_standby == 0 &&
3635 3634 ilport->ilport_alua == 1) {
3636 3635 ic_session_dereg = ic_session_dereg_msg_alloc(
3637 3636 ss, stmf_proxy_msg_id);
3638 3637 if (ic_session_dereg) {
3639 3638 ic_ret = ic_tx_msg(ic_session_dereg);
3640 3639 if (ic_ret == STMF_IC_MSG_SUCCESS) {
3641 3640 stmf_proxy_msg_id++;
3642 3641 }
3643 3642 }
3644 3643 }
3645 3644
3646 3645 rw_enter(&ilport->ilport_lock, RW_WRITER);
3647 3646 for (ppss = &ilport->ilport_ss_list; *ppss != NULL;
3648 3647 ppss = &((*ppss)->iss_next)) {
3649 3648 if (iss == (*ppss)) {
3650 3649 *ppss = (*ppss)->iss_next;
3651 3650 found = 1;
3652 3651 break;
3653 3652 }
3654 3653 }
3655 3654 if (!found) {
3656 3655 cmn_err(CE_PANIC, "Deregister session called for non existent"
3657 3656 " session");
3658 3657 }
3659 3658 ilport->ilport_nsessions--;
3660 3659
3661 3660 stmf_irport_deregister(iss->iss_irport);
3662 3661 (void) stmf_session_destroy_lun_map(ilport, iss);
3663 3662 rw_exit(&ilport->ilport_lock);
3664 3663 mutex_exit(&stmf_state.stmf_lock);
3665 3664
3666 3665 if (iss->iss_flags & ISS_NULL_TPTID) {
3667 3666 stmf_remote_port_free(ss->ss_rport);
3668 3667 }
3669 3668 }
3670 3669
3671 3670 stmf_i_scsi_session_t *
3672 3671 stmf_session_id_to_issptr(uint64_t session_id, int stay_locked)
3673 3672 {
3674 3673 stmf_i_local_port_t *ilport;
3675 3674 stmf_i_scsi_session_t *iss;
3676 3675
3677 3676 mutex_enter(&stmf_state.stmf_lock);
3678 3677 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3679 3678 ilport = ilport->ilport_next) {
3680 3679 rw_enter(&ilport->ilport_lock, RW_WRITER);
3681 3680 for (iss = ilport->ilport_ss_list; iss != NULL;
3682 3681 iss = iss->iss_next) {
3683 3682 if (iss->iss_ss->ss_session_id == session_id) {
3684 3683 if (!stay_locked)
3685 3684 rw_exit(&ilport->ilport_lock);
3686 3685 mutex_exit(&stmf_state.stmf_lock);
3687 3686 return (iss);
3688 3687 }
3689 3688 }
3690 3689 rw_exit(&ilport->ilport_lock);
3691 3690 }
3692 3691 mutex_exit(&stmf_state.stmf_lock);
3693 3692 return (NULL);
3694 3693 }
3695 3694
3696 3695 void
3697 3696 stmf_release_itl_handle(stmf_lu_t *lu, stmf_itl_data_t *itl)
3698 3697 {
3699 3698 stmf_itl_data_t **itlpp;
3700 3699 stmf_i_lu_t *ilu;
3701 3700
3702 3701 ASSERT(itl->itl_flags & STMF_ITL_BEING_TERMINATED);
3703 3702
3704 3703 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3705 3704 mutex_enter(&ilu->ilu_task_lock);
3706 3705 for (itlpp = &ilu->ilu_itl_list; (*itlpp) != NULL;
3707 3706 itlpp = &(*itlpp)->itl_next) {
3708 3707 if ((*itlpp) == itl)
3709 3708 break;
3710 3709 }
3711 3710 ASSERT((*itlpp) != NULL);
3712 3711 *itlpp = itl->itl_next;
3713 3712 mutex_exit(&ilu->ilu_task_lock);
3714 3713 lu->lu_abort(lu, STMF_LU_ITL_HANDLE_REMOVED, itl->itl_handle,
3715 3714 (uint32_t)itl->itl_hdlrm_reason);
3716 3715
3717 3716 kmem_free(itl, sizeof (*itl));
3718 3717 }
3719 3718
3720 3719 stmf_status_t
3721 3720 stmf_register_itl_handle(stmf_lu_t *lu, uint8_t *lun,
3722 3721 stmf_scsi_session_t *ss, uint64_t session_id, void *itl_handle)
3723 3722 {
3724 3723 stmf_itl_data_t *itl;
3725 3724 stmf_i_scsi_session_t *iss;
3726 3725 stmf_lun_map_ent_t *lun_map_ent;
3727 3726 stmf_i_lu_t *ilu;
3728 3727 uint16_t n;
3729 3728
3730 3729 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3731 3730 if (ss == NULL) {
3732 3731 iss = stmf_session_id_to_issptr(session_id, 1);
3733 3732 if (iss == NULL)
3734 3733 return (STMF_NOT_FOUND);
3735 3734 } else {
3736 3735 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3737 3736 }
3738 3737
3739 3738 mutex_enter(&stmf_state.stmf_lock);
3740 3739 rw_enter(iss->iss_lockp, RW_WRITER);
3741 3740 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
3742 3741 lun_map_ent = (stmf_lun_map_ent_t *)
3743 3742 stmf_get_ent_from_map(iss->iss_sm, n);
3744 3743 if ((lun_map_ent == NULL) || (lun_map_ent->ent_lu != lu)) {
3745 3744 rw_exit(iss->iss_lockp);
3746 3745 mutex_exit(&stmf_state.stmf_lock);
3747 3746 return (STMF_NOT_FOUND);
3748 3747 }
3749 3748 if (lun_map_ent->ent_itl_datap != NULL) {
3750 3749 rw_exit(iss->iss_lockp);
3751 3750 mutex_exit(&stmf_state.stmf_lock);
3752 3751 return (STMF_ALREADY);
3753 3752 }
3754 3753
3755 3754 itl = (stmf_itl_data_t *)kmem_zalloc(sizeof (*itl), KM_NOSLEEP);
3756 3755 if (itl == NULL) {
3757 3756 rw_exit(iss->iss_lockp);
3758 3757 mutex_exit(&stmf_state.stmf_lock);
3759 3758 return (STMF_ALLOC_FAILURE);
3760 3759 }
3761 3760
3762 3761 itl->itl_ilu = ilu;
3763 3762 itl->itl_session = iss;
3764 3763 itl->itl_counter = 1;
3765 3764 itl->itl_lun = n;
3766 3765 itl->itl_handle = itl_handle;
3767 3766
3768 3767 mutex_enter(&ilu->ilu_task_lock);
3769 3768 itl->itl_next = ilu->ilu_itl_list;
3770 3769 ilu->ilu_itl_list = itl;
3771 3770 mutex_exit(&ilu->ilu_task_lock);
3772 3771 lun_map_ent->ent_itl_datap = itl;
3773 3772 rw_exit(iss->iss_lockp);
3774 3773 mutex_exit(&stmf_state.stmf_lock);
3775 3774
3776 3775 return (STMF_SUCCESS);
3777 3776 }
3778 3777
3779 3778 void
3780 3779 stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl, uint8_t hdlrm_reason)
3781 3780 {
3782 3781 uint8_t old, new;
3783 3782
3784 3783 do {
3785 3784 old = new = itl->itl_flags;
3786 3785 if (old & STMF_ITL_BEING_TERMINATED)
3787 3786 return;
3788 3787 new |= STMF_ITL_BEING_TERMINATED;
3789 3788 } while (atomic_cas_8(&itl->itl_flags, old, new) != old);
3790 3789 itl->itl_hdlrm_reason = hdlrm_reason;
3791 3790
3792 3791 ASSERT(itl->itl_counter);
3793 3792
3794 3793 if (atomic_dec_32_nv(&itl->itl_counter))
3795 3794 return;
3796 3795
3797 3796 stmf_release_itl_handle(lu, itl);
3798 3797 }
3799 3798
3800 3799 stmf_status_t
3801 3800 stmf_deregister_all_lu_itl_handles(stmf_lu_t *lu)
3802 3801 {
3803 3802 stmf_i_lu_t *ilu;
3804 3803 stmf_i_local_port_t *ilport;
3805 3804 stmf_i_scsi_session_t *iss;
3806 3805 stmf_lun_map_t *lm;
3807 3806 stmf_lun_map_ent_t *ent;
3808 3807 uint32_t nmaps, nu;
3809 3808 stmf_itl_data_t **itl_list;
3810 3809 int i;
3811 3810
3812 3811 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
3813 3812
3814 3813 dereg_itl_start:;
3815 3814 nmaps = ilu->ilu_ref_cnt;
3816 3815 if (nmaps == 0)
3817 3816 return (STMF_NOT_FOUND);
3818 3817 itl_list = (stmf_itl_data_t **)kmem_zalloc(
3819 3818 nmaps * sizeof (stmf_itl_data_t *), KM_SLEEP);
3820 3819 mutex_enter(&stmf_state.stmf_lock);
3821 3820 if (nmaps != ilu->ilu_ref_cnt) {
3822 3821 /* Something changed, start all over */
3823 3822 mutex_exit(&stmf_state.stmf_lock);
3824 3823 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
3825 3824 goto dereg_itl_start;
3826 3825 }
3827 3826 nu = 0;
3828 3827 for (ilport = stmf_state.stmf_ilportlist; ilport != NULL;
3829 3828 ilport = ilport->ilport_next) {
3830 3829 rw_enter(&ilport->ilport_lock, RW_WRITER);
3831 3830 for (iss = ilport->ilport_ss_list; iss != NULL;
3832 3831 iss = iss->iss_next) {
3833 3832 lm = iss->iss_sm;
3834 3833 if (!lm)
3835 3834 continue;
3836 3835 for (i = 0; i < lm->lm_nentries; i++) {
3837 3836 if (lm->lm_plus[i] == NULL)
3838 3837 continue;
3839 3838 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
3840 3839 if ((ent->ent_lu == lu) &&
3841 3840 (ent->ent_itl_datap)) {
3842 3841 itl_list[nu++] = ent->ent_itl_datap;
3843 3842 ent->ent_itl_datap = NULL;
3844 3843 if (nu == nmaps) {
3845 3844 rw_exit(&ilport->ilport_lock);
3846 3845 goto dai_scan_done;
3847 3846 }
3848 3847 }
3849 3848 } /* lun table for a session */
3850 3849 } /* sessions */
3851 3850 rw_exit(&ilport->ilport_lock);
3852 3851 } /* ports */
3853 3852
3854 3853 dai_scan_done:
3855 3854 mutex_exit(&stmf_state.stmf_lock);
3856 3855
3857 3856 for (i = 0; i < nu; i++) {
3858 3857 stmf_do_itl_dereg(lu, itl_list[i],
3859 3858 STMF_ITL_REASON_DEREG_REQUEST);
3860 3859 }
3861 3860 kmem_free(itl_list, nmaps * sizeof (stmf_itl_data_t *));
3862 3861
3863 3862 return (STMF_SUCCESS);
3864 3863 }
3865 3864
3866 3865 stmf_status_t
3867 3866 stmf_get_itl_handle(stmf_lu_t *lu, uint8_t *lun, stmf_scsi_session_t *ss,
3868 3867 uint64_t session_id, void **itl_handle_retp)
3869 3868 {
3870 3869 stmf_i_scsi_session_t *iss;
3871 3870 stmf_lun_map_ent_t *ent;
3872 3871 stmf_lun_map_t *lm;
3873 3872 stmf_status_t ret;
3874 3873 int i;
3875 3874 uint16_t n;
3876 3875
3877 3876 if (ss == NULL) {
3878 3877 iss = stmf_session_id_to_issptr(session_id, 1);
3879 3878 if (iss == NULL)
3880 3879 return (STMF_NOT_FOUND);
3881 3880 } else {
3882 3881 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
3883 3882 rw_enter(iss->iss_lockp, RW_WRITER);
3884 3883 }
3885 3884
3886 3885 ent = NULL;
3887 3886 if (lun == NULL) {
3888 3887 lm = iss->iss_sm;
3889 3888 for (i = 0; i < lm->lm_nentries; i++) {
3890 3889 if (lm->lm_plus[i] == NULL)
3891 3890 continue;
3892 3891 ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
3893 3892 if (ent->ent_lu == lu)
3894 3893 break;
3895 3894 }
3896 3895 } else {
3897 3896 n = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
3898 3897 ent = (stmf_lun_map_ent_t *)
3899 3898 stmf_get_ent_from_map(iss->iss_sm, n);
3900 3899 if (lu && (ent->ent_lu != lu))
3901 3900 ent = NULL;
3902 3901 }
3903 3902 if (ent && ent->ent_itl_datap) {
3904 3903 *itl_handle_retp = ent->ent_itl_datap->itl_handle;
3905 3904 ret = STMF_SUCCESS;
3906 3905 } else {
3907 3906 ret = STMF_NOT_FOUND;
3908 3907 }
3909 3908
3910 3909 rw_exit(iss->iss_lockp);
3911 3910 return (ret);
3912 3911 }
3913 3912
3914 3913 stmf_data_buf_t *
3915 3914 stmf_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
3916 3915 uint32_t flags)
3917 3916 {
3918 3917 stmf_i_scsi_task_t *itask =
3919 3918 (stmf_i_scsi_task_t *)task->task_stmf_private;
3920 3919 stmf_local_port_t *lport = task->task_lport;
3921 3920 stmf_data_buf_t *dbuf;
3922 3921 uint8_t ndx;
3923 3922
3924 3923 ndx = stmf_first_zero[itask->itask_allocated_buf_map];
3925 3924 if (ndx == 0xff)
3926 3925 return (NULL);
3927 3926 dbuf = itask->itask_dbufs[ndx] = lport->lport_ds->ds_alloc_data_buf(
3928 3927 task, size, pminsize, flags);
3929 3928 if (dbuf) {
3930 3929 task->task_cur_nbufs++;
3931 3930 itask->itask_allocated_buf_map |= (1 << ndx);
3932 3931 dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
3933 3932 dbuf->db_handle = ndx;
3934 3933 return (dbuf);
3935 3934 }
3936 3935
3937 3936 return (NULL);
3938 3937 }
3939 3938
3940 3939 stmf_status_t
3941 3940 stmf_setup_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t flags)
3942 3941 {
3943 3942 stmf_i_scsi_task_t *itask =
3944 3943 (stmf_i_scsi_task_t *)task->task_stmf_private;
3945 3944 stmf_local_port_t *lport = task->task_lport;
3946 3945 uint8_t ndx;
3947 3946 stmf_status_t ret;
3948 3947
3949 3948 ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
3950 3949 ASSERT(lport->lport_ds->ds_setup_dbuf != NULL);
3951 3950 ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
3952 3951
3953 3952 if ((task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF) == 0)
3954 3953 return (STMF_FAILURE);
3955 3954 if (lport->lport_ds->ds_setup_dbuf == NULL)
3956 3955 return (STMF_FAILURE);
3957 3956
3958 3957 ndx = stmf_first_zero[itask->itask_allocated_buf_map];
3959 3958 if (ndx == 0xff)
3960 3959 return (STMF_FAILURE);
3961 3960 ret = lport->lport_ds->ds_setup_dbuf(task, dbuf, flags);
3962 3961 if (ret == STMF_FAILURE)
3963 3962 return (STMF_FAILURE);
3964 3963 itask->itask_dbufs[ndx] = dbuf;
3965 3964 task->task_cur_nbufs++;
3966 3965 itask->itask_allocated_buf_map |= (1 << ndx);
3967 3966 dbuf->db_handle = ndx;
3968 3967
3969 3968 return (STMF_SUCCESS);
3970 3969 }
3971 3970
3972 3971 void
3973 3972 stmf_teardown_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
3974 3973 {
3975 3974 stmf_i_scsi_task_t *itask =
3976 3975 (stmf_i_scsi_task_t *)task->task_stmf_private;
3977 3976 stmf_local_port_t *lport = task->task_lport;
3978 3977
3979 3978 ASSERT(task->task_additional_flags & TASK_AF_ACCEPT_LU_DBUF);
3980 3979 ASSERT(lport->lport_ds->ds_teardown_dbuf != NULL);
3981 3980 ASSERT(dbuf->db_flags & DB_LU_DATA_BUF);
3982 3981
3983 3982 itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
3984 3983 task->task_cur_nbufs--;
3985 3984 lport->lport_ds->ds_teardown_dbuf(lport->lport_ds, dbuf);
3986 3985 }
3987 3986
3988 3987 void
3989 3988 stmf_free_dbuf(scsi_task_t *task, stmf_data_buf_t *dbuf)
3990 3989 {
3991 3990 stmf_i_scsi_task_t *itask =
3992 3991 (stmf_i_scsi_task_t *)task->task_stmf_private;
3993 3992 stmf_local_port_t *lport = task->task_lport;
3994 3993
3995 3994 itask->itask_allocated_buf_map &= ~(1 << dbuf->db_handle);
3996 3995 task->task_cur_nbufs--;
3997 3996 lport->lport_ds->ds_free_data_buf(lport->lport_ds, dbuf);
3998 3997 }
3999 3998
4000 3999 stmf_data_buf_t *
4001 4000 stmf_handle_to_buf(scsi_task_t *task, uint8_t h)
4002 4001 {
4003 4002 stmf_i_scsi_task_t *itask;
4004 4003
4005 4004 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4006 4005 if (h > 3)
4007 4006 return (NULL);
4008 4007 return (itask->itask_dbufs[h]);
4009 4008 }
4010 4009
4011 4010 /* ARGSUSED */
4012 4011 struct scsi_task *
4013 4012 stmf_task_alloc(struct stmf_local_port *lport, stmf_scsi_session_t *ss,
4014 4013 uint8_t *lun, uint16_t cdb_length_in, uint16_t ext_id)
4015 4014 {
4016 4015 stmf_lu_t *lu;
4017 4016 stmf_i_scsi_session_t *iss;
4018 4017 stmf_i_lu_t *ilu;
4019 4018 stmf_i_scsi_task_t *itask;
4020 4019 stmf_i_scsi_task_t **ppitask;
4021 4020 scsi_task_t *task;
4022 4021 uint8_t *l;
4023 4022 stmf_lun_map_ent_t *lun_map_ent;
4024 4023 uint16_t cdb_length;
4025 4024 uint16_t luNbr;
4026 4025 uint8_t new_task = 0;
4027 4026
4028 4027 /*
4029 4028 * We allocate 7 extra bytes for CDB to provide a cdb pointer which
4030 4029 * is guaranteed to be 8 byte aligned. Some LU providers like OSD
4031 4030 * depend upon this alignment.
4032 4031 */
4033 4032 if (cdb_length_in >= 16)
4034 4033 cdb_length = cdb_length_in + 7;
4035 4034 else
4036 4035 cdb_length = 16 + 7;
4037 4036 iss = (stmf_i_scsi_session_t *)ss->ss_stmf_private;
4038 4037 luNbr = ((uint16_t)lun[1] | (((uint16_t)(lun[0] & 0x3F)) << 8));
4039 4038 rw_enter(iss->iss_lockp, RW_READER);
4040 4039 lun_map_ent =
4041 4040 (stmf_lun_map_ent_t *)stmf_get_ent_from_map(iss->iss_sm, luNbr);
4042 4041 if (!lun_map_ent) {
4043 4042 lu = dlun0;
4044 4043 } else {
4045 4044 lu = lun_map_ent->ent_lu;
4046 4045 }
4047 4046 ilu = lu->lu_stmf_private;
4048 4047 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4049 4048 rw_exit(iss->iss_lockp);
4050 4049 return (NULL);
4051 4050 }
4052 4051 ASSERT(lu == dlun0 || (ilu->ilu_state != STMF_STATE_OFFLINING &&
4053 4052 ilu->ilu_state != STMF_STATE_OFFLINE));
4054 4053 do {
4055 4054 if (ilu->ilu_free_tasks == NULL) {
4056 4055 new_task = 1;
4057 4056 break;
4058 4057 }
4059 4058 mutex_enter(&ilu->ilu_task_lock);
4060 4059 for (ppitask = &ilu->ilu_free_tasks; (*ppitask != NULL) &&
4061 4060 ((*ppitask)->itask_cdb_buf_size < cdb_length);
4062 4061 ppitask = &((*ppitask)->itask_lu_free_next))
4063 4062 ;
4064 4063 if (*ppitask) {
4065 4064 itask = *ppitask;
4066 4065 *ppitask = (*ppitask)->itask_lu_free_next;
4067 4066 ilu->ilu_ntasks_free--;
4068 4067 if (ilu->ilu_ntasks_free < ilu->ilu_ntasks_min_free)
4069 4068 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4070 4069 } else {
4071 4070 new_task = 1;
4072 4071 }
4073 4072 mutex_exit(&ilu->ilu_task_lock);
4074 4073 /* CONSTCOND */
4075 4074 } while (0);
4076 4075
4077 4076 if (!new_task) {
4078 4077 /*
4079 4078 * Save the task_cdb pointer and zero per cmd fields.
4080 4079 * We know the task_cdb_length is large enough by task
4081 4080 * selection process above.
4082 4081 */
4083 4082 uint8_t *save_cdb;
4084 4083 uintptr_t t_start, t_end;
4085 4084
4086 4085 task = itask->itask_task;
4087 4086 save_cdb = task->task_cdb; /* save */
4088 4087 t_start = (uintptr_t)&task->task_flags;
4089 4088 t_end = (uintptr_t)&task->task_extended_cmd;
4090 4089 bzero((void *)t_start, (size_t)(t_end - t_start));
4091 4090 task->task_cdb = save_cdb; /* restore */
4092 4091 itask->itask_ncmds = 0;
4093 4092 } else {
4094 4093 task = (scsi_task_t *)stmf_alloc(STMF_STRUCT_SCSI_TASK,
4095 4094 cdb_length, AF_FORCE_NOSLEEP);
4096 4095 if (task == NULL) {
4097 4096 rw_exit(iss->iss_lockp);
4098 4097 return (NULL);
4099 4098 }
4100 4099 task->task_lu = lu;
4101 4100 l = task->task_lun_no;
4102 4101 l[0] = lun[0];
4103 4102 l[1] = lun[1];
4104 4103 l[2] = lun[2];
4105 4104 l[3] = lun[3];
4106 4105 l[4] = lun[4];
4107 4106 l[5] = lun[5];
4108 4107 l[6] = lun[6];
4109 4108 l[7] = lun[7];
4110 4109 task->task_cdb = (uint8_t *)task->task_port_private;
4111 4110 if ((ulong_t)(task->task_cdb) & 7ul) {
4112 4111 task->task_cdb = (uint8_t *)(((ulong_t)
4113 4112 (task->task_cdb) + 7ul) & ~(7ul));
4114 4113 }
4115 4114 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4116 4115 itask->itask_cdb_buf_size = cdb_length;
4117 4116 mutex_init(&itask->itask_audit_mutex, NULL, MUTEX_DRIVER, NULL);
4118 4117 }
4119 4118 task->task_session = ss;
4120 4119 task->task_lport = lport;
4121 4120 task->task_cdb_length = cdb_length_in;
4122 4121 itask->itask_flags = ITASK_IN_TRANSITION;
4123 4122 itask->itask_waitq_time = 0;
4124 4123 itask->itask_lu_read_time = itask->itask_lu_write_time = 0;
4125 4124 itask->itask_lport_read_time = itask->itask_lport_write_time = 0;
4126 4125 itask->itask_read_xfer = itask->itask_write_xfer = 0;
4127 4126 itask->itask_audit_index = 0;
4128 4127
4129 4128 if (new_task) {
4130 4129 if (lu->lu_task_alloc(task) != STMF_SUCCESS) {
4131 4130 rw_exit(iss->iss_lockp);
4132 4131 stmf_free(task);
4133 4132 return (NULL);
4134 4133 }
4135 4134 mutex_enter(&ilu->ilu_task_lock);
4136 4135 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4137 4136 mutex_exit(&ilu->ilu_task_lock);
4138 4137 rw_exit(iss->iss_lockp);
4139 4138 stmf_free(task);
4140 4139 return (NULL);
4141 4140 }
4142 4141 itask->itask_lu_next = ilu->ilu_tasks;
4143 4142 if (ilu->ilu_tasks)
4144 4143 ilu->ilu_tasks->itask_lu_prev = itask;
4145 4144 ilu->ilu_tasks = itask;
4146 4145 /* kmem_zalloc automatically makes itask->itask_lu_prev NULL */
4147 4146 ilu->ilu_ntasks++;
4148 4147 mutex_exit(&ilu->ilu_task_lock);
4149 4148 }
4150 4149
4151 4150 itask->itask_ilu_task_cntr = ilu->ilu_cur_task_cntr;
4152 4151 atomic_inc_32(itask->itask_ilu_task_cntr);
4153 4152 itask->itask_start_time = ddi_get_lbolt();
4154 4153
4155 4154 if ((lun_map_ent != NULL) && ((itask->itask_itl_datap =
4156 4155 lun_map_ent->ent_itl_datap) != NULL)) {
4157 4156 atomic_inc_32(&itask->itask_itl_datap->itl_counter);
4158 4157 task->task_lu_itl_handle = itask->itask_itl_datap->itl_handle;
4159 4158 } else {
4160 4159 itask->itask_itl_datap = NULL;
4161 4160 task->task_lu_itl_handle = NULL;
4162 4161 }
4163 4162
4164 4163 rw_exit(iss->iss_lockp);
4165 4164 return (task);
4166 4165 }
4167 4166
4168 4167 static void
4169 4168 stmf_task_lu_free(scsi_task_t *task, stmf_i_scsi_session_t *iss)
4170 4169 {
4171 4170 stmf_i_scsi_task_t *itask =
4172 4171 (stmf_i_scsi_task_t *)task->task_stmf_private;
4173 4172 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4174 4173
4175 4174 ASSERT(rw_lock_held(iss->iss_lockp));
4176 4175 itask->itask_flags = ITASK_IN_FREE_LIST;
4177 4176 itask->itask_proxy_msg_id = 0;
4178 4177 mutex_enter(&ilu->ilu_task_lock);
4179 4178 itask->itask_lu_free_next = ilu->ilu_free_tasks;
4180 4179 ilu->ilu_free_tasks = itask;
4181 4180 ilu->ilu_ntasks_free++;
4182 4181 if (ilu->ilu_ntasks == ilu->ilu_ntasks_free)
4183 4182 cv_signal(&ilu->ilu_offline_pending_cv);
4184 4183 mutex_exit(&ilu->ilu_task_lock);
4185 4184 atomic_dec_32(itask->itask_ilu_task_cntr);
4186 4185 }
4187 4186
4188 4187 void
4189 4188 stmf_task_lu_check_freelist(stmf_i_lu_t *ilu)
4190 4189 {
4191 4190 uint32_t num_to_release, ndx;
4192 4191 stmf_i_scsi_task_t *itask;
4193 4192 stmf_lu_t *lu = ilu->ilu_lu;
4194 4193
4195 4194 ASSERT(ilu->ilu_ntasks_min_free <= ilu->ilu_ntasks_free);
4196 4195
4197 4196 /* free half of the minimal free of the free tasks */
4198 4197 num_to_release = (ilu->ilu_ntasks_min_free + 1) / 2;
4199 4198 if (!num_to_release) {
4200 4199 return;
4201 4200 }
4202 4201 for (ndx = 0; ndx < num_to_release; ndx++) {
4203 4202 mutex_enter(&ilu->ilu_task_lock);
4204 4203 itask = ilu->ilu_free_tasks;
4205 4204 if (itask == NULL) {
4206 4205 mutex_exit(&ilu->ilu_task_lock);
4207 4206 break;
4208 4207 }
4209 4208 ilu->ilu_free_tasks = itask->itask_lu_free_next;
4210 4209 ilu->ilu_ntasks_free--;
4211 4210 mutex_exit(&ilu->ilu_task_lock);
4212 4211
4213 4212 lu->lu_task_free(itask->itask_task);
4214 4213 mutex_enter(&ilu->ilu_task_lock);
4215 4214 if (itask->itask_lu_next)
4216 4215 itask->itask_lu_next->itask_lu_prev =
4217 4216 itask->itask_lu_prev;
4218 4217 if (itask->itask_lu_prev)
4219 4218 itask->itask_lu_prev->itask_lu_next =
4220 4219 itask->itask_lu_next;
4221 4220 else
4222 4221 ilu->ilu_tasks = itask->itask_lu_next;
4223 4222
4224 4223 ilu->ilu_ntasks--;
4225 4224 mutex_exit(&ilu->ilu_task_lock);
4226 4225 stmf_free(itask->itask_task);
4227 4226 }
4228 4227 }
4229 4228
4230 4229 /*
4231 4230 * Called with stmf_lock held
4232 4231 */
4233 4232 void
4234 4233 stmf_check_freetask()
4235 4234 {
4236 4235 stmf_i_lu_t *ilu;
4237 4236 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000);
4238 4237
4239 4238 /* stmf_svc_ilu_draining may get changed after stmf_lock is released */
4240 4239 while ((ilu = stmf_state.stmf_svc_ilu_draining) != NULL) {
4241 4240 stmf_state.stmf_svc_ilu_draining = ilu->ilu_next;
4242 4241 if (!ilu->ilu_ntasks_min_free) {
4243 4242 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4244 4243 continue;
4245 4244 }
4246 4245 ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4247 4246 mutex_exit(&stmf_state.stmf_lock);
4248 4247 stmf_task_lu_check_freelist(ilu);
4249 4248 /*
4250 4249 * we do not care about the accuracy of
4251 4250 * ilu_ntasks_min_free, so we don't lock here
4252 4251 */
4253 4252 ilu->ilu_ntasks_min_free = ilu->ilu_ntasks_free;
4254 4253 mutex_enter(&stmf_state.stmf_lock);
4255 4254 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4256 4255 cv_broadcast(&stmf_state.stmf_cv);
4257 4256 if (ddi_get_lbolt() >= endtime)
4258 4257 break;
4259 4258 }
4260 4259 }
4261 4260
4262 4261 void
4263 4262 stmf_do_ilu_timeouts(stmf_i_lu_t *ilu)
4264 4263 {
4265 4264 clock_t l = ddi_get_lbolt();
4266 4265 clock_t ps = drv_usectohz(1000000);
4267 4266 stmf_i_scsi_task_t *itask;
4268 4267 scsi_task_t *task;
4269 4268 uint32_t to;
4270 4269
4271 4270 mutex_enter(&ilu->ilu_task_lock);
4272 4271 for (itask = ilu->ilu_tasks; itask != NULL;
4273 4272 itask = itask->itask_lu_next) {
4274 4273 if (itask->itask_flags & (ITASK_IN_FREE_LIST |
4275 4274 ITASK_BEING_ABORTED)) {
4276 4275 continue;
4277 4276 }
4278 4277 task = itask->itask_task;
4279 4278 if (task->task_timeout == 0)
4280 4279 to = stmf_default_task_timeout;
4281 4280 else
4282 4281 to = task->task_timeout;
4283 4282 if ((itask->itask_start_time + (to * ps)) > l)
4284 4283 continue;
4285 4284 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
4286 4285 STMF_TIMEOUT, NULL);
4287 4286 }
4288 4287 mutex_exit(&ilu->ilu_task_lock);
4289 4288 }
4290 4289
4291 4290 /*
4292 4291 * Called with stmf_lock held
4293 4292 */
4294 4293 void
4295 4294 stmf_check_ilu_timing()
4296 4295 {
4297 4296 stmf_i_lu_t *ilu;
4298 4297 clock_t endtime = ddi_get_lbolt() + drv_usectohz(10000);
4299 4298
4300 4299 /* stmf_svc_ilu_timing may get changed after stmf_lock is released */
4301 4300 while ((ilu = stmf_state.stmf_svc_ilu_timing) != NULL) {
4302 4301 stmf_state.stmf_svc_ilu_timing = ilu->ilu_next;
4303 4302 if (ilu->ilu_cur_task_cntr == (&ilu->ilu_task_cntr1)) {
4304 4303 if (ilu->ilu_task_cntr2 == 0) {
4305 4304 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr2;
4306 4305 continue;
4307 4306 }
4308 4307 } else {
4309 4308 if (ilu->ilu_task_cntr1 == 0) {
4310 4309 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
4311 4310 continue;
4312 4311 }
4313 4312 }
4314 4313 /*
4315 4314 * If we are here then it means that there is some slowdown
4316 4315 * in tasks on this lu. We need to check.
4317 4316 */
4318 4317 ilu->ilu_flags |= ILU_STALL_DEREGISTER;
4319 4318 mutex_exit(&stmf_state.stmf_lock);
4320 4319 stmf_do_ilu_timeouts(ilu);
4321 4320 mutex_enter(&stmf_state.stmf_lock);
4322 4321 ilu->ilu_flags &= ~ILU_STALL_DEREGISTER;
4323 4322 cv_broadcast(&stmf_state.stmf_cv);
4324 4323 if (ddi_get_lbolt() >= endtime)
4325 4324 break;
4326 4325 }
4327 4326 }
4328 4327
4329 4328 /*
4330 4329 * Kills all tasks on a lu except tm_task
4331 4330 */
4332 4331 void
4333 4332 stmf_task_lu_killall(stmf_lu_t *lu, scsi_task_t *tm_task, stmf_status_t s)
4334 4333 {
4335 4334 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
4336 4335 stmf_i_scsi_task_t *itask;
4337 4336
4338 4337 mutex_enter(&ilu->ilu_task_lock);
4339 4338
4340 4339 for (itask = ilu->ilu_tasks; itask != NULL;
4341 4340 itask = itask->itask_lu_next) {
4342 4341 if (itask->itask_flags & ITASK_IN_FREE_LIST)
4343 4342 continue;
4344 4343 if (itask->itask_task == tm_task)
4345 4344 continue;
4346 4345 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->itask_task, s, NULL);
4347 4346 }
4348 4347 mutex_exit(&ilu->ilu_task_lock);
4349 4348 }
4350 4349
4351 4350 void
4352 4351 stmf_free_task_bufs(stmf_i_scsi_task_t *itask, stmf_local_port_t *lport)
4353 4352 {
4354 4353 int i;
4355 4354 uint8_t map;
4356 4355
4357 4356 if ((map = itask->itask_allocated_buf_map) == 0)
4358 4357 return;
4359 4358 for (i = 0; i < 4; i++) {
4360 4359 if (map & 1) {
4361 4360 stmf_data_buf_t *dbuf;
4362 4361
4363 4362 dbuf = itask->itask_dbufs[i];
4364 4363 if (dbuf->db_xfer_start_timestamp) {
4365 4364 stmf_lport_xfer_done(itask, dbuf);
4366 4365 }
4367 4366 if (dbuf->db_flags & DB_LU_DATA_BUF) {
4368 4367 /*
4369 4368 * LU needs to clean up buffer.
4370 4369 * LU is required to free the buffer
4371 4370 * in the xfer_done handler.
4372 4371 */
4373 4372 scsi_task_t *task = itask->itask_task;
4374 4373 stmf_lu_t *lu = task->task_lu;
4375 4374
4376 4375 lu->lu_dbuf_free(task, dbuf);
4377 4376 ASSERT(((itask->itask_allocated_buf_map>>i)
4378 4377 & 1) == 0); /* must be gone */
4379 4378 } else {
4380 4379 ASSERT(dbuf->db_lu_private == NULL);
4381 4380 dbuf->db_lu_private = NULL;
4382 4381 lport->lport_ds->ds_free_data_buf(
4383 4382 lport->lport_ds, dbuf);
4384 4383 }
4385 4384 }
4386 4385 map >>= 1;
4387 4386 }
4388 4387 itask->itask_allocated_buf_map = 0;
4389 4388 }
4390 4389
4391 4390 void
4392 4391 stmf_task_free(scsi_task_t *task)
4393 4392 {
4394 4393 stmf_local_port_t *lport = task->task_lport;
4395 4394 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4396 4395 task->task_stmf_private;
4397 4396 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
4398 4397 task->task_session->ss_stmf_private;
4399 4398
4400 4399 stmf_task_audit(itask, TE_TASK_FREE, CMD_OR_IOF_NA, NULL);
4401 4400
4402 4401 stmf_free_task_bufs(itask, lport);
4403 4402 stmf_itl_task_done(itask);
4404 4403 DTRACE_PROBE2(stmf__task__end, scsi_task_t *, task,
4405 4404 hrtime_t,
4406 4405 itask->itask_done_timestamp - itask->itask_start_timestamp);
4407 4406 if (itask->itask_itl_datap) {
4408 4407 if (atomic_dec_32_nv(&itask->itask_itl_datap->itl_counter) ==
4409 4408 0) {
4410 4409 stmf_release_itl_handle(task->task_lu,
4411 4410 itask->itask_itl_datap);
4412 4411 }
4413 4412 }
4414 4413
4415 4414 rw_enter(iss->iss_lockp, RW_READER);
4416 4415 lport->lport_task_free(task);
4417 4416 if (itask->itask_worker) {
4418 4417 atomic_dec_32(&stmf_cur_ntasks);
4419 4418 atomic_dec_32(&itask->itask_worker->worker_ref_count);
4420 4419 }
4421 4420 /*
4422 4421 * After calling stmf_task_lu_free, the task pointer can no longer
4423 4422 * be trusted.
4424 4423 */
4425 4424 stmf_task_lu_free(task, iss);
4426 4425 rw_exit(iss->iss_lockp);
4427 4426 }
4428 4427
4429 4428 void
4430 4429 stmf_post_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
4431 4430 {
4432 4431 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
4433 4432 task->task_stmf_private;
4434 4433 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
4435 4434 int nv;
4436 4435 uint32_t old, new;
4437 4436 uint32_t ct;
4438 4437 stmf_worker_t *w, *w1;
4439 4438 uint8_t tm;
4440 4439
4441 4440 if (task->task_max_nbufs > 4)
4442 4441 task->task_max_nbufs = 4;
4443 4442 task->task_cur_nbufs = 0;
4444 4443 /* Latest value of currently running tasks */
4445 4444 ct = atomic_inc_32_nv(&stmf_cur_ntasks);
4446 4445
4447 4446 /* Select the next worker using round robin */
4448 4447 nv = (int)atomic_inc_32_nv((uint32_t *)&stmf_worker_sel_counter);
4449 4448 if (nv >= stmf_nworkers_accepting_cmds) {
4450 4449 int s = nv;
4451 4450 do {
4452 4451 nv -= stmf_nworkers_accepting_cmds;
4453 4452 } while (nv >= stmf_nworkers_accepting_cmds);
4454 4453 if (nv < 0)
4455 4454 nv = 0;
4456 4455 /* Its ok if this cas fails */
4457 4456 (void) atomic_cas_32((uint32_t *)&stmf_worker_sel_counter,
4458 4457 s, nv);
4459 4458 }
4460 4459 w = &stmf_workers[nv];
4461 4460
4462 4461 /*
4463 4462 * A worker can be pinned by interrupt. So select the next one
4464 4463 * if it has lower load.
4465 4464 */
4466 4465 if ((nv + 1) >= stmf_nworkers_accepting_cmds) {
4467 4466 w1 = stmf_workers;
4468 4467 } else {
4469 4468 w1 = &stmf_workers[nv + 1];
4470 4469 }
4471 4470 if (w1->worker_queue_depth < w->worker_queue_depth)
4472 4471 w = w1;
4473 4472
4474 4473 mutex_enter(&w->worker_lock);
4475 4474 if (((w->worker_flags & STMF_WORKER_STARTED) == 0) ||
4476 4475 (w->worker_flags & STMF_WORKER_TERMINATE)) {
4477 4476 /*
4478 4477 * Maybe we are in the middle of a change. Just go to
4479 4478 * the 1st worker.
4480 4479 */
4481 4480 mutex_exit(&w->worker_lock);
4482 4481 w = stmf_workers;
4483 4482 mutex_enter(&w->worker_lock);
4484 4483 }
4485 4484 itask->itask_worker = w;
4486 4485 /*
4487 4486 * Track max system load inside the worker as we already have the
4488 4487 * worker lock (no point implementing another lock). The service
4489 4488 * thread will do the comparisons and figure out the max overall
4490 4489 * system load.
4491 4490 */
4492 4491 if (w->worker_max_sys_qdepth_pu < ct)
4493 4492 w->worker_max_sys_qdepth_pu = ct;
4494 4493
4495 4494 do {
4496 4495 old = new = itask->itask_flags;
4497 4496 new |= ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE;
4498 4497 if (task->task_mgmt_function) {
4499 4498 tm = task->task_mgmt_function;
4500 4499 if ((tm == TM_TARGET_RESET) ||
4501 4500 (tm == TM_TARGET_COLD_RESET) ||
4502 4501 (tm == TM_TARGET_WARM_RESET)) {
4503 4502 new |= ITASK_DEFAULT_HANDLING;
4504 4503 }
4505 4504 } else if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
4506 4505 new |= ITASK_DEFAULT_HANDLING;
4507 4506 }
4508 4507 new &= ~ITASK_IN_TRANSITION;
4509 4508 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4510 4509
4511 4510 stmf_itl_task_start(itask);
4512 4511
4513 4512 itask->itask_worker_next = NULL;
4514 4513 if (w->worker_task_tail) {
4515 4514 w->worker_task_tail->itask_worker_next = itask;
4516 4515 } else {
4517 4516 w->worker_task_head = itask;
4518 4517 }
4519 4518 w->worker_task_tail = itask;
4520 4519 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4521 4520 w->worker_max_qdepth_pu = w->worker_queue_depth;
4522 4521 }
4523 4522 /* Measure task waitq time */
4524 4523 itask->itask_waitq_enter_timestamp = gethrtime();
4525 4524 atomic_inc_32(&w->worker_ref_count);
4526 4525 itask->itask_cmd_stack[0] = ITASK_CMD_NEW_TASK;
4527 4526 itask->itask_ncmds = 1;
4528 4527 stmf_task_audit(itask, TE_TASK_START, CMD_OR_IOF_NA, dbuf);
4529 4528 if (dbuf) {
4530 4529 itask->itask_allocated_buf_map = 1;
4531 4530 itask->itask_dbufs[0] = dbuf;
4532 4531 dbuf->db_handle = 0;
4533 4532 } else {
4534 4533 itask->itask_allocated_buf_map = 0;
4535 4534 itask->itask_dbufs[0] = NULL;
4536 4535 }
4537 4536
4538 4537 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0) {
4539 4538 w->worker_signal_timestamp = gethrtime();
4540 4539 DTRACE_PROBE2(worker__signal, stmf_worker_t *, w,
4541 4540 scsi_task_t *, task);
4542 4541 cv_signal(&w->worker_cv);
4543 4542 }
4544 4543 mutex_exit(&w->worker_lock);
4545 4544
4546 4545 /*
4547 4546 * This can only happen if during stmf_task_alloc(), ILU_RESET_ACTIVE
4548 4547 * was set between checking of ILU_RESET_ACTIVE and clearing of the
4549 4548 * ITASK_IN_FREE_LIST flag. Take care of these "sneaked-in" tasks here.
4550 4549 */
4551 4550 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
4552 4551 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ABORTED, NULL);
4553 4552 }
4554 4553 }
4555 4554
4556 4555 static void
4557 4556 stmf_task_audit(stmf_i_scsi_task_t *itask,
4558 4557 task_audit_event_t te, uint32_t cmd_or_iof, stmf_data_buf_t *dbuf)
4559 4558 {
4560 4559 stmf_task_audit_rec_t *ar;
4561 4560
4562 4561 mutex_enter(&itask->itask_audit_mutex);
4563 4562 ar = &itask->itask_audit_records[itask->itask_audit_index++];
4564 4563 itask->itask_audit_index &= (ITASK_TASK_AUDIT_DEPTH - 1);
4565 4564 ar->ta_event = te;
4566 4565 ar->ta_cmd_or_iof = cmd_or_iof;
4567 4566 ar->ta_itask_flags = itask->itask_flags;
4568 4567 ar->ta_dbuf = dbuf;
4569 4568 gethrestime(&ar->ta_timestamp);
4570 4569 mutex_exit(&itask->itask_audit_mutex);
4571 4570 }
4572 4571
4573 4572
4574 4573 /*
4575 4574 * ++++++++++++++ ABORT LOGIC ++++++++++++++++++++
4576 4575 * Once ITASK_BEING_ABORTED is set, ITASK_KNOWN_TO_LU can be reset already
4577 4576 * i.e. before ITASK_BEING_ABORTED being set. But if it was not, it cannot
4578 4577 * be reset until the LU explicitly calls stmf_task_lu_aborted(). Of course
4579 4578 * the LU will make this call only if we call the LU's abort entry point.
4580 4579 * we will only call that entry point if ITASK_KNOWN_TO_LU was set.
4581 4580 *
4582 4581 * Same logic applies for the port.
4583 4582 *
4584 4583 * Also ITASK_BEING_ABORTED will not be allowed to set if both KNOWN_TO_LU
4585 4584 * and KNOWN_TO_TGT_PORT are reset.
4586 4585 *
4587 4586 * +++++++++++++++++++++++++++++++++++++++++++++++
4588 4587 */
4589 4588
4590 4589 stmf_status_t
4591 4590 stmf_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
4592 4591 {
4593 4592 stmf_status_t ret = STMF_SUCCESS;
4594 4593
4595 4594 stmf_i_scsi_task_t *itask =
4596 4595 (stmf_i_scsi_task_t *)task->task_stmf_private;
4597 4596
4598 4597 stmf_task_audit(itask, TE_XFER_START, ioflags, dbuf);
4599 4598
4600 4599 if (ioflags & STMF_IOF_LU_DONE) {
4601 4600 uint32_t new, old;
4602 4601 do {
4603 4602 new = old = itask->itask_flags;
4604 4603 if (new & ITASK_BEING_ABORTED)
4605 4604 return (STMF_ABORTED);
4606 4605 new &= ~ITASK_KNOWN_TO_LU;
4607 4606 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4608 4607 }
4609 4608 if (itask->itask_flags & ITASK_BEING_ABORTED)
4610 4609 return (STMF_ABORTED);
4611 4610 #ifdef DEBUG
4612 4611 if (!(ioflags & STMF_IOF_STATS_ONLY) && stmf_drop_buf_counter > 0) {
4613 4612 if (atomic_dec_32_nv(&stmf_drop_buf_counter) == 1)
4614 4613 return (STMF_SUCCESS);
4615 4614 }
4616 4615 #endif
4617 4616
4618 4617 stmf_update_kstat_lu_io(task, dbuf);
4619 4618 stmf_update_kstat_lport_io(task, dbuf);
4620 4619 stmf_lport_xfer_start(itask, dbuf);
4621 4620 if (ioflags & STMF_IOF_STATS_ONLY) {
4622 4621 stmf_lport_xfer_done(itask, dbuf);
4623 4622 return (STMF_SUCCESS);
4624 4623 }
4625 4624
4626 4625 dbuf->db_flags |= DB_LPORT_XFER_ACTIVE;
4627 4626 ret = task->task_lport->lport_xfer_data(task, dbuf, ioflags);
4628 4627
4629 4628 /*
4630 4629 * Port provider may have already called the buffer callback in
4631 4630 * which case dbuf->db_xfer_start_timestamp will be 0.
4632 4631 */
4633 4632 if (ret != STMF_SUCCESS) {
4634 4633 dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4635 4634 if (dbuf->db_xfer_start_timestamp != 0)
4636 4635 stmf_lport_xfer_done(itask, dbuf);
4637 4636 }
4638 4637
4639 4638 return (ret);
4640 4639 }
4641 4640
4642 4641 void
4643 4642 stmf_data_xfer_done(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t iof)
4644 4643 {
4645 4644 stmf_i_scsi_task_t *itask =
4646 4645 (stmf_i_scsi_task_t *)task->task_stmf_private;
4647 4646 stmf_i_local_port_t *ilport;
4648 4647 stmf_worker_t *w = itask->itask_worker;
4649 4648 uint32_t new, old;
4650 4649 uint8_t update_queue_flags, free_it, queue_it;
4651 4650
4652 4651 stmf_lport_xfer_done(itask, dbuf);
4653 4652
4654 4653 stmf_task_audit(itask, TE_XFER_DONE, iof, dbuf);
4655 4654
4656 4655 /* Guard against unexpected completions from the lport */
4657 4656 if (dbuf->db_flags & DB_LPORT_XFER_ACTIVE) {
4658 4657 dbuf->db_flags &= ~DB_LPORT_XFER_ACTIVE;
4659 4658 } else {
4660 4659 /*
4661 4660 * This should never happen.
4662 4661 */
4663 4662 ilport = task->task_lport->lport_stmf_private;
4664 4663 ilport->ilport_unexpected_comp++;
4665 4664 cmn_err(CE_PANIC, "Unexpected xfer completion task %p dbuf %p",
4666 4665 (void *)task, (void *)dbuf);
4667 4666 return;
4668 4667 }
4669 4668
4670 4669 mutex_enter(&w->worker_lock);
4671 4670 do {
4672 4671 new = old = itask->itask_flags;
4673 4672 if (old & ITASK_BEING_ABORTED) {
4674 4673 mutex_exit(&w->worker_lock);
4675 4674 return;
4676 4675 }
4677 4676 free_it = 0;
4678 4677 if (iof & STMF_IOF_LPORT_DONE) {
4679 4678 new &= ~ITASK_KNOWN_TO_TGT_PORT;
4680 4679 task->task_completion_status = dbuf->db_xfer_status;
4681 4680 free_it = 1;
4682 4681 }
4683 4682 /*
4684 4683 * If the task is known to LU then queue it. But if
4685 4684 * it is already queued (multiple completions) then
4686 4685 * just update the buffer information by grabbing the
4687 4686 * worker lock. If the task is not known to LU,
4688 4687 * completed/aborted, then see if we need to
4689 4688 * free this task.
4690 4689 */
4691 4690 if (old & ITASK_KNOWN_TO_LU) {
4692 4691 free_it = 0;
4693 4692 update_queue_flags = 1;
4694 4693 if (old & ITASK_IN_WORKER_QUEUE) {
4695 4694 queue_it = 0;
4696 4695 } else {
4697 4696 queue_it = 1;
4698 4697 new |= ITASK_IN_WORKER_QUEUE;
4699 4698 }
4700 4699 } else {
4701 4700 update_queue_flags = 0;
4702 4701 queue_it = 0;
4703 4702 }
4704 4703 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4705 4704
4706 4705 if (update_queue_flags) {
4707 4706 uint8_t cmd = (dbuf->db_handle << 5) | ITASK_CMD_DATA_XFER_DONE;
4708 4707
4709 4708 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
4710 4709 itask->itask_cmd_stack[itask->itask_ncmds++] = cmd;
4711 4710 if (queue_it) {
4712 4711 itask->itask_worker_next = NULL;
4713 4712 if (w->worker_task_tail) {
4714 4713 w->worker_task_tail->itask_worker_next = itask;
4715 4714 } else {
4716 4715 w->worker_task_head = itask;
4717 4716 }
4718 4717 w->worker_task_tail = itask;
4719 4718 /* Measure task waitq time */
4720 4719 itask->itask_waitq_enter_timestamp = gethrtime();
4721 4720 if (++(w->worker_queue_depth) >
4722 4721 w->worker_max_qdepth_pu) {
4723 4722 w->worker_max_qdepth_pu = w->worker_queue_depth;
4724 4723 }
4725 4724 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4726 4725 cv_signal(&w->worker_cv);
4727 4726 }
4728 4727 }
4729 4728 mutex_exit(&w->worker_lock);
4730 4729
4731 4730 if (free_it) {
4732 4731 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4733 4732 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4734 4733 ITASK_BEING_ABORTED)) == 0) {
4735 4734 stmf_task_free(task);
4736 4735 }
4737 4736 }
4738 4737 }
4739 4738
4740 4739 stmf_status_t
4741 4740 stmf_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
4742 4741 {
4743 4742 DTRACE_PROBE1(scsi__send__status, scsi_task_t *, task);
4744 4743
4745 4744 stmf_i_scsi_task_t *itask =
4746 4745 (stmf_i_scsi_task_t *)task->task_stmf_private;
4747 4746
4748 4747 stmf_task_audit(itask, TE_SEND_STATUS, ioflags, NULL);
4749 4748
4750 4749 if (ioflags & STMF_IOF_LU_DONE) {
4751 4750 uint32_t new, old;
4752 4751 do {
4753 4752 new = old = itask->itask_flags;
4754 4753 if (new & ITASK_BEING_ABORTED)
4755 4754 return (STMF_ABORTED);
4756 4755 new &= ~ITASK_KNOWN_TO_LU;
4757 4756 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4758 4757 }
4759 4758
4760 4759 if (!(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT)) {
4761 4760 return (STMF_SUCCESS);
4762 4761 }
4763 4762
4764 4763 if (itask->itask_flags & ITASK_BEING_ABORTED)
4765 4764 return (STMF_ABORTED);
4766 4765
4767 4766 if (task->task_additional_flags & TASK_AF_NO_EXPECTED_XFER_LENGTH) {
4768 4767 task->task_status_ctrl = 0;
4769 4768 task->task_resid = 0;
4770 4769 } else if (task->task_cmd_xfer_length >
4771 4770 task->task_expected_xfer_length) {
4772 4771 task->task_status_ctrl = TASK_SCTRL_OVER;
4773 4772 task->task_resid = task->task_cmd_xfer_length -
4774 4773 task->task_expected_xfer_length;
4775 4774 } else if (task->task_nbytes_transferred <
4776 4775 task->task_expected_xfer_length) {
4777 4776 task->task_status_ctrl = TASK_SCTRL_UNDER;
4778 4777 task->task_resid = task->task_expected_xfer_length -
4779 4778 task->task_nbytes_transferred;
4780 4779 } else {
4781 4780 task->task_status_ctrl = 0;
4782 4781 task->task_resid = 0;
4783 4782 }
4784 4783 return (task->task_lport->lport_send_status(task, ioflags));
4785 4784 }
4786 4785
4787 4786 void
4788 4787 stmf_send_status_done(scsi_task_t *task, stmf_status_t s, uint32_t iof)
4789 4788 {
4790 4789 stmf_i_scsi_task_t *itask =
4791 4790 (stmf_i_scsi_task_t *)task->task_stmf_private;
4792 4791 stmf_worker_t *w = itask->itask_worker;
4793 4792 uint32_t new, old;
4794 4793 uint8_t free_it, queue_it;
4795 4794
4796 4795 stmf_task_audit(itask, TE_SEND_STATUS_DONE, iof, NULL);
4797 4796
4798 4797 mutex_enter(&w->worker_lock);
4799 4798 do {
4800 4799 new = old = itask->itask_flags;
4801 4800 if (old & ITASK_BEING_ABORTED) {
4802 4801 mutex_exit(&w->worker_lock);
4803 4802 return;
4804 4803 }
4805 4804 free_it = 0;
4806 4805 if (iof & STMF_IOF_LPORT_DONE) {
4807 4806 new &= ~ITASK_KNOWN_TO_TGT_PORT;
4808 4807 free_it = 1;
4809 4808 }
4810 4809 /*
4811 4810 * If the task is known to LU then queue it. But if
4812 4811 * it is already queued (multiple completions) then
4813 4812 * just update the buffer information by grabbing the
4814 4813 * worker lock. If the task is not known to LU,
4815 4814 * completed/aborted, then see if we need to
4816 4815 * free this task.
4817 4816 */
4818 4817 if (old & ITASK_KNOWN_TO_LU) {
4819 4818 free_it = 0;
4820 4819 queue_it = 1;
4821 4820 if (old & ITASK_IN_WORKER_QUEUE) {
4822 4821 cmn_err(CE_PANIC, "status completion received"
4823 4822 " when task is already in worker queue "
4824 4823 " task = %p", (void *)task);
4825 4824 }
4826 4825 new |= ITASK_IN_WORKER_QUEUE;
4827 4826 } else {
4828 4827 queue_it = 0;
4829 4828 }
4830 4829 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4831 4830 task->task_completion_status = s;
4832 4831
4833 4832
4834 4833 if (queue_it) {
4835 4834 ASSERT(itask->itask_ncmds < ITASK_MAX_NCMDS);
4836 4835 itask->itask_cmd_stack[itask->itask_ncmds++] =
4837 4836 ITASK_CMD_STATUS_DONE;
4838 4837 itask->itask_worker_next = NULL;
4839 4838 if (w->worker_task_tail) {
4840 4839 w->worker_task_tail->itask_worker_next = itask;
4841 4840 } else {
4842 4841 w->worker_task_head = itask;
4843 4842 }
4844 4843 w->worker_task_tail = itask;
4845 4844 /* Measure task waitq time */
4846 4845 itask->itask_waitq_enter_timestamp = gethrtime();
4847 4846 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4848 4847 w->worker_max_qdepth_pu = w->worker_queue_depth;
4849 4848 }
4850 4849 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4851 4850 cv_signal(&w->worker_cv);
4852 4851 }
4853 4852 mutex_exit(&w->worker_lock);
4854 4853
4855 4854 if (free_it) {
4856 4855 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4857 4856 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4858 4857 ITASK_BEING_ABORTED)) == 0) {
4859 4858 stmf_task_free(task);
4860 4859 } else {
4861 4860 cmn_err(CE_PANIC, "LU is done with the task but LPORT "
4862 4861 " is not done, itask %p itask_flags %x",
4863 4862 (void *)itask, itask->itask_flags);
4864 4863 }
4865 4864 }
4866 4865 }
4867 4866
4868 4867 void
4869 4868 stmf_task_lu_done(scsi_task_t *task)
4870 4869 {
4871 4870 stmf_i_scsi_task_t *itask =
4872 4871 (stmf_i_scsi_task_t *)task->task_stmf_private;
4873 4872 stmf_worker_t *w = itask->itask_worker;
4874 4873 uint32_t new, old;
4875 4874
4876 4875 mutex_enter(&w->worker_lock);
4877 4876 do {
4878 4877 new = old = itask->itask_flags;
4879 4878 if (old & ITASK_BEING_ABORTED) {
4880 4879 mutex_exit(&w->worker_lock);
4881 4880 return;
4882 4881 }
4883 4882 if (old & ITASK_IN_WORKER_QUEUE) {
4884 4883 cmn_err(CE_PANIC, "task_lu_done received"
4885 4884 " when task is in worker queue "
4886 4885 " task = %p", (void *)task);
4887 4886 }
4888 4887 new &= ~ITASK_KNOWN_TO_LU;
4889 4888 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4890 4889
4891 4890 mutex_exit(&w->worker_lock);
4892 4891
4893 4892 if ((itask->itask_flags & (ITASK_KNOWN_TO_LU |
4894 4893 ITASK_KNOWN_TO_TGT_PORT | ITASK_IN_WORKER_QUEUE |
4895 4894 ITASK_BEING_ABORTED)) == 0) {
4896 4895 stmf_task_free(task);
4897 4896 } else {
4898 4897 cmn_err(CE_PANIC, "stmf_lu_done should be the last stage but "
4899 4898 " the task is still not done, task = %p", (void *)task);
4900 4899 }
4901 4900 }
4902 4901
4903 4902 void
4904 4903 stmf_queue_task_for_abort(scsi_task_t *task, stmf_status_t s)
4905 4904 {
4906 4905 stmf_i_scsi_task_t *itask =
4907 4906 (stmf_i_scsi_task_t *)task->task_stmf_private;
4908 4907 stmf_worker_t *w;
4909 4908 uint32_t old, new;
4910 4909
4911 4910 stmf_task_audit(itask, TE_TASK_ABORT, CMD_OR_IOF_NA, NULL);
4912 4911
4913 4912 do {
4914 4913 old = new = itask->itask_flags;
4915 4914 if ((old & ITASK_BEING_ABORTED) ||
4916 4915 ((old & (ITASK_KNOWN_TO_TGT_PORT |
4917 4916 ITASK_KNOWN_TO_LU)) == 0)) {
4918 4917 return;
4919 4918 }
4920 4919 new |= ITASK_BEING_ABORTED;
4921 4920 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4922 4921 task->task_completion_status = s;
4923 4922 itask->itask_start_time = ddi_get_lbolt();
4924 4923
4925 4924 if (((w = itask->itask_worker) == NULL) ||
4926 4925 (itask->itask_flags & ITASK_IN_TRANSITION)) {
4927 4926 return;
4928 4927 }
4929 4928
4930 4929 /* Queue it and get out */
4931 4930 mutex_enter(&w->worker_lock);
4932 4931 if (itask->itask_flags & ITASK_IN_WORKER_QUEUE) {
4933 4932 mutex_exit(&w->worker_lock);
4934 4933 return;
4935 4934 }
4936 4935 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
4937 4936 itask->itask_worker_next = NULL;
4938 4937 if (w->worker_task_tail) {
4939 4938 w->worker_task_tail->itask_worker_next = itask;
4940 4939 } else {
4941 4940 w->worker_task_head = itask;
4942 4941 }
4943 4942 w->worker_task_tail = itask;
4944 4943 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
4945 4944 w->worker_max_qdepth_pu = w->worker_queue_depth;
4946 4945 }
4947 4946 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
4948 4947 cv_signal(&w->worker_cv);
4949 4948 mutex_exit(&w->worker_lock);
4950 4949 }
4951 4950
4952 4951 void
4953 4952 stmf_abort(int abort_cmd, scsi_task_t *task, stmf_status_t s, void *arg)
4954 4953 {
4955 4954 stmf_i_scsi_task_t *itask = NULL;
4956 4955 uint32_t old, new, f, rf;
4957 4956
4958 4957 DTRACE_PROBE2(scsi__task__abort, scsi_task_t *, task,
4959 4958 stmf_status_t, s);
4960 4959
4961 4960 switch (abort_cmd) {
4962 4961 case STMF_QUEUE_ABORT_LU:
4963 4962 stmf_task_lu_killall((stmf_lu_t *)arg, task, s);
4964 4963 return;
4965 4964 case STMF_QUEUE_TASK_ABORT:
4966 4965 stmf_queue_task_for_abort(task, s);
4967 4966 return;
4968 4967 case STMF_REQUEUE_TASK_ABORT_LPORT:
4969 4968 rf = ITASK_TGT_PORT_ABORT_CALLED;
4970 4969 f = ITASK_KNOWN_TO_TGT_PORT;
4971 4970 break;
4972 4971 case STMF_REQUEUE_TASK_ABORT_LU:
4973 4972 rf = ITASK_LU_ABORT_CALLED;
4974 4973 f = ITASK_KNOWN_TO_LU;
4975 4974 break;
4976 4975 default:
4977 4976 return;
4978 4977 }
4979 4978 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
4980 4979 f |= ITASK_BEING_ABORTED | rf;
4981 4980 do {
4982 4981 old = new = itask->itask_flags;
4983 4982 if ((old & f) != f) {
4984 4983 return;
4985 4984 }
4986 4985 new &= ~rf;
4987 4986 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
4988 4987 }
4989 4988
4990 4989 void
4991 4990 stmf_task_lu_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
4992 4991 {
4993 4992 char info[STMF_CHANGE_INFO_LEN];
4994 4993 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task);
4995 4994 unsigned long long st;
4996 4995
4997 4996 stmf_task_audit(itask, TE_TASK_LU_ABORTED, iof, NULL);
4998 4997
4999 4998 st = s; /* gcc fix */
5000 4999 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5001 5000 (void) snprintf(info, sizeof (info),
5002 5001 "task %p, lu failed to abort ret=%llx", (void *)task, st);
5003 5002 } else if ((iof & STMF_IOF_LU_DONE) == 0) {
5004 5003 (void) snprintf(info, sizeof (info),
5005 5004 "Task aborted but LU is not finished, task ="
5006 5005 "%p, s=%llx, iof=%x", (void *)task, st, iof);
5007 5006 } else {
5008 5007 /*
5009 5008 * LU abort successfully
5010 5009 */
5011 5010 atomic_and_32(&itask->itask_flags, ~ITASK_KNOWN_TO_LU);
5012 5011 return;
5013 5012 }
5014 5013
5015 5014 stmf_abort_task_offline(task, 1, info);
5016 5015 }
5017 5016
5018 5017 void
5019 5018 stmf_task_lport_aborted(scsi_task_t *task, stmf_status_t s, uint32_t iof)
5020 5019 {
5021 5020 char info[STMF_CHANGE_INFO_LEN];
5022 5021 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task);
5023 5022 unsigned long long st;
5024 5023 uint32_t old, new;
5025 5024
5026 5025 stmf_task_audit(itask, TE_TASK_LPORT_ABORTED, iof, NULL);
5027 5026
5028 5027 st = s;
5029 5028 if ((s != STMF_ABORT_SUCCESS) && (s != STMF_NOT_FOUND)) {
5030 5029 (void) snprintf(info, sizeof (info),
5031 5030 "task %p, tgt port failed to abort ret=%llx", (void *)task,
5032 5031 st);
5033 5032 } else if ((iof & STMF_IOF_LPORT_DONE) == 0) {
5034 5033 (void) snprintf(info, sizeof (info),
5035 5034 "Task aborted but tgt port is not finished, "
5036 5035 "task=%p, s=%llx, iof=%x", (void *)task, st, iof);
5037 5036 } else {
5038 5037 /*
5039 5038 * LPORT abort successfully
5040 5039 */
5041 5040 do {
5042 5041 old = new = itask->itask_flags;
5043 5042 if (!(old & ITASK_KNOWN_TO_TGT_PORT))
5044 5043 return;
5045 5044 new &= ~ITASK_KNOWN_TO_TGT_PORT;
5046 5045 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5047 5046 return;
5048 5047 }
5049 5048
5050 5049 stmf_abort_task_offline(task, 0, info);
5051 5050 }
5052 5051
5053 5052 stmf_status_t
5054 5053 stmf_task_poll_lu(scsi_task_t *task, uint32_t timeout)
5055 5054 {
5056 5055 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5057 5056 task->task_stmf_private;
5058 5057 stmf_worker_t *w = itask->itask_worker;
5059 5058 int i;
5060 5059
5061 5060 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_LU);
5062 5061 mutex_enter(&w->worker_lock);
5063 5062 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5064 5063 mutex_exit(&w->worker_lock);
5065 5064 return (STMF_BUSY);
5066 5065 }
5067 5066 for (i = 0; i < itask->itask_ncmds; i++) {
5068 5067 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LU) {
5069 5068 mutex_exit(&w->worker_lock);
5070 5069 return (STMF_SUCCESS);
5071 5070 }
5072 5071 }
5073 5072 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LU;
5074 5073 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5075 5074 itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5076 5075 } else {
5077 5076 clock_t t = drv_usectohz(timeout * 1000);
5078 5077 if (t == 0)
5079 5078 t = 1;
5080 5079 itask->itask_poll_timeout = ddi_get_lbolt() + t;
5081 5080 }
5082 5081 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5083 5082 itask->itask_worker_next = NULL;
5084 5083 if (w->worker_task_tail) {
5085 5084 w->worker_task_tail->itask_worker_next = itask;
5086 5085 } else {
5087 5086 w->worker_task_head = itask;
5088 5087 }
5089 5088 w->worker_task_tail = itask;
5090 5089 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5091 5090 w->worker_max_qdepth_pu = w->worker_queue_depth;
5092 5091 }
5093 5092 atomic_or_32(&itask->itask_flags, ITASK_IN_WORKER_QUEUE);
5094 5093 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5095 5094 cv_signal(&w->worker_cv);
5096 5095 }
5097 5096 mutex_exit(&w->worker_lock);
5098 5097 return (STMF_SUCCESS);
5099 5098 }
5100 5099
5101 5100 stmf_status_t
5102 5101 stmf_task_poll_lport(scsi_task_t *task, uint32_t timeout)
5103 5102 {
5104 5103 stmf_i_scsi_task_t *itask = (stmf_i_scsi_task_t *)
5105 5104 task->task_stmf_private;
5106 5105 stmf_worker_t *w = itask->itask_worker;
5107 5106 int i;
5108 5107
5109 5108 ASSERT(itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT);
5110 5109 mutex_enter(&w->worker_lock);
5111 5110 if (itask->itask_ncmds >= ITASK_MAX_NCMDS) {
5112 5111 mutex_exit(&w->worker_lock);
5113 5112 return (STMF_BUSY);
5114 5113 }
5115 5114 for (i = 0; i < itask->itask_ncmds; i++) {
5116 5115 if (itask->itask_cmd_stack[i] == ITASK_CMD_POLL_LPORT) {
5117 5116 mutex_exit(&w->worker_lock);
5118 5117 return (STMF_SUCCESS);
5119 5118 }
5120 5119 }
5121 5120 itask->itask_cmd_stack[itask->itask_ncmds++] = ITASK_CMD_POLL_LPORT;
5122 5121 if (timeout == ITASK_DEFAULT_POLL_TIMEOUT) {
5123 5122 itask->itask_poll_timeout = ddi_get_lbolt() + 1;
5124 5123 } else {
5125 5124 clock_t t = drv_usectohz(timeout * 1000);
5126 5125 if (t == 0)
5127 5126 t = 1;
5128 5127 itask->itask_poll_timeout = ddi_get_lbolt() + t;
5129 5128 }
5130 5129 if ((itask->itask_flags & ITASK_IN_WORKER_QUEUE) == 0) {
5131 5130 itask->itask_worker_next = NULL;
5132 5131 if (w->worker_task_tail) {
5133 5132 w->worker_task_tail->itask_worker_next = itask;
5134 5133 } else {
5135 5134 w->worker_task_head = itask;
5136 5135 }
5137 5136 w->worker_task_tail = itask;
5138 5137 if (++(w->worker_queue_depth) > w->worker_max_qdepth_pu) {
5139 5138 w->worker_max_qdepth_pu = w->worker_queue_depth;
5140 5139 }
5141 5140 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
5142 5141 cv_signal(&w->worker_cv);
5143 5142 }
5144 5143 mutex_exit(&w->worker_lock);
5145 5144 return (STMF_SUCCESS);
5146 5145 }
5147 5146
5148 5147 void
5149 5148 stmf_do_task_abort(scsi_task_t *task)
5150 5149 {
5151 5150 stmf_i_scsi_task_t *itask = TASK_TO_ITASK(task);
5152 5151 stmf_lu_t *lu;
5153 5152 stmf_local_port_t *lport;
5154 5153 unsigned long long ret;
5155 5154 uint32_t old, new;
5156 5155 uint8_t call_lu_abort, call_port_abort;
5157 5156 char info[STMF_CHANGE_INFO_LEN];
5158 5157
5159 5158 lu = task->task_lu;
5160 5159 lport = task->task_lport;
5161 5160 do {
5162 5161 old = new = itask->itask_flags;
5163 5162 if ((old & (ITASK_KNOWN_TO_LU | ITASK_LU_ABORT_CALLED)) ==
5164 5163 ITASK_KNOWN_TO_LU) {
5165 5164 new |= ITASK_LU_ABORT_CALLED;
5166 5165 call_lu_abort = 1;
5167 5166 } else {
5168 5167 call_lu_abort = 0;
5169 5168 }
5170 5169 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5171 5170
5172 5171 if (call_lu_abort) {
5173 5172 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0) {
5174 5173 ret = lu->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5175 5174 } else {
5176 5175 ret = dlun0->lu_abort(lu, STMF_LU_ABORT_TASK, task, 0);
5177 5176 }
5178 5177 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5179 5178 stmf_task_lu_aborted(task, ret, STMF_IOF_LU_DONE);
5180 5179 } else if (ret == STMF_BUSY) {
5181 5180 atomic_and_32(&itask->itask_flags,
5182 5181 ~ITASK_LU_ABORT_CALLED);
5183 5182 } else if (ret != STMF_SUCCESS) {
5184 5183 (void) snprintf(info, sizeof (info),
5185 5184 "Abort failed by LU %p, ret %llx", (void *)lu, ret);
5186 5185 stmf_abort_task_offline(task, 1, info);
5187 5186 }
5188 5187 } else if (itask->itask_flags & ITASK_KNOWN_TO_LU) {
5189 5188 if (ddi_get_lbolt() > (itask->itask_start_time +
5190 5189 STMF_SEC2TICK(lu->lu_abort_timeout?
5191 5190 lu->lu_abort_timeout : ITASK_DEFAULT_ABORT_TIMEOUT))) {
5192 5191 (void) snprintf(info, sizeof (info),
5193 5192 "lu abort timed out");
5194 5193 stmf_abort_task_offline(itask->itask_task, 1, info);
5195 5194 }
5196 5195 }
5197 5196
5198 5197 do {
5199 5198 old = new = itask->itask_flags;
5200 5199 if ((old & (ITASK_KNOWN_TO_TGT_PORT |
5201 5200 ITASK_TGT_PORT_ABORT_CALLED)) == ITASK_KNOWN_TO_TGT_PORT) {
5202 5201 new |= ITASK_TGT_PORT_ABORT_CALLED;
5203 5202 call_port_abort = 1;
5204 5203 } else {
5205 5204 call_port_abort = 0;
5206 5205 }
5207 5206 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
5208 5207 if (call_port_abort) {
5209 5208 ret = lport->lport_abort(lport, STMF_LPORT_ABORT_TASK, task, 0);
5210 5209 if ((ret == STMF_ABORT_SUCCESS) || (ret == STMF_NOT_FOUND)) {
5211 5210 stmf_task_lport_aborted(task, ret, STMF_IOF_LPORT_DONE);
5212 5211 } else if (ret == STMF_BUSY) {
5213 5212 atomic_and_32(&itask->itask_flags,
5214 5213 ~ITASK_TGT_PORT_ABORT_CALLED);
5215 5214 } else if (ret != STMF_SUCCESS) {
5216 5215 (void) snprintf(info, sizeof (info),
5217 5216 "Abort failed by tgt port %p ret %llx",
5218 5217 (void *)lport, ret);
5219 5218 stmf_abort_task_offline(task, 0, info);
5220 5219 }
5221 5220 } else if (itask->itask_flags & ITASK_KNOWN_TO_TGT_PORT) {
5222 5221 if (ddi_get_lbolt() > (itask->itask_start_time +
5223 5222 STMF_SEC2TICK(lport->lport_abort_timeout?
5224 5223 lport->lport_abort_timeout :
5225 5224 ITASK_DEFAULT_ABORT_TIMEOUT))) {
5226 5225 (void) snprintf(info, sizeof (info),
5227 5226 "lport abort timed out");
5228 5227 stmf_abort_task_offline(itask->itask_task, 0, info);
5229 5228 }
5230 5229 }
5231 5230 }
5232 5231
5233 5232 stmf_status_t
5234 5233 stmf_ctl(int cmd, void *obj, void *arg)
5235 5234 {
5236 5235 stmf_status_t ret;
5237 5236 stmf_i_lu_t *ilu;
5238 5237 stmf_i_local_port_t *ilport;
5239 5238 stmf_state_change_info_t *ssci = (stmf_state_change_info_t *)arg;
5240 5239
5241 5240 mutex_enter(&stmf_state.stmf_lock);
5242 5241 ret = STMF_INVALID_ARG;
5243 5242 if (cmd & STMF_CMD_LU_OP) {
5244 5243 ilu = stmf_lookup_lu((stmf_lu_t *)obj);
5245 5244 if (ilu == NULL) {
5246 5245 goto stmf_ctl_lock_exit;
5247 5246 }
5248 5247 DTRACE_PROBE3(lu__state__change,
5249 5248 stmf_lu_t *, ilu->ilu_lu,
5250 5249 int, cmd, stmf_state_change_info_t *, ssci);
5251 5250 } else if (cmd & STMF_CMD_LPORT_OP) {
5252 5251 ilport = stmf_lookup_lport((stmf_local_port_t *)obj);
5253 5252 if (ilport == NULL) {
5254 5253 goto stmf_ctl_lock_exit;
5255 5254 }
5256 5255 DTRACE_PROBE3(lport__state__change,
5257 5256 stmf_local_port_t *, ilport->ilport_lport,
5258 5257 int, cmd, stmf_state_change_info_t *, ssci);
5259 5258 } else {
5260 5259 goto stmf_ctl_lock_exit;
5261 5260 }
5262 5261
5263 5262 switch (cmd) {
5264 5263 case STMF_CMD_LU_ONLINE:
5265 5264 switch (ilu->ilu_state) {
5266 5265 case STMF_STATE_OFFLINE:
5267 5266 ret = STMF_SUCCESS;
5268 5267 break;
5269 5268 case STMF_STATE_ONLINE:
5270 5269 case STMF_STATE_ONLINING:
5271 5270 ret = STMF_ALREADY;
5272 5271 break;
5273 5272 case STMF_STATE_OFFLINING:
5274 5273 ret = STMF_BUSY;
5275 5274 break;
5276 5275 default:
5277 5276 ret = STMF_BADSTATE;
5278 5277 break;
5279 5278 }
5280 5279 if (ret != STMF_SUCCESS)
5281 5280 goto stmf_ctl_lock_exit;
5282 5281
5283 5282 ilu->ilu_state = STMF_STATE_ONLINING;
5284 5283 mutex_exit(&stmf_state.stmf_lock);
5285 5284 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5286 5285 break;
5287 5286
5288 5287 case STMF_CMD_LU_ONLINE_COMPLETE:
5289 5288 if (ilu->ilu_state != STMF_STATE_ONLINING) {
5290 5289 ret = STMF_BADSTATE;
5291 5290 goto stmf_ctl_lock_exit;
5292 5291 }
5293 5292 if (((stmf_change_status_t *)arg)->st_completion_status ==
5294 5293 STMF_SUCCESS) {
5295 5294 ilu->ilu_state = STMF_STATE_ONLINE;
5296 5295 mutex_exit(&stmf_state.stmf_lock);
5297 5296 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5298 5297 STMF_ACK_LU_ONLINE_COMPLETE, arg);
5299 5298 mutex_enter(&stmf_state.stmf_lock);
5300 5299 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5301 5300 } else {
5302 5301 /* XXX: should throw a meesage an record more data */
5303 5302 ilu->ilu_state = STMF_STATE_OFFLINE;
5304 5303 }
5305 5304 ret = STMF_SUCCESS;
5306 5305 goto stmf_ctl_lock_exit;
5307 5306
5308 5307 case STMF_CMD_LU_OFFLINE:
5309 5308 switch (ilu->ilu_state) {
5310 5309 case STMF_STATE_ONLINE:
5311 5310 ret = STMF_SUCCESS;
5312 5311 break;
5313 5312 case STMF_STATE_OFFLINE:
5314 5313 case STMF_STATE_OFFLINING:
5315 5314 ret = STMF_ALREADY;
5316 5315 break;
5317 5316 case STMF_STATE_ONLINING:
5318 5317 ret = STMF_BUSY;
5319 5318 break;
5320 5319 default:
5321 5320 ret = STMF_BADSTATE;
5322 5321 break;
5323 5322 }
5324 5323 if (ret != STMF_SUCCESS)
5325 5324 goto stmf_ctl_lock_exit;
5326 5325 ilu->ilu_state = STMF_STATE_OFFLINING;
5327 5326 mutex_exit(&stmf_state.stmf_lock);
5328 5327 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5329 5328 break;
5330 5329
5331 5330 case STMF_CMD_LU_OFFLINE_COMPLETE:
5332 5331 if (ilu->ilu_state != STMF_STATE_OFFLINING) {
5333 5332 ret = STMF_BADSTATE;
5334 5333 goto stmf_ctl_lock_exit;
5335 5334 }
5336 5335 if (((stmf_change_status_t *)arg)->st_completion_status ==
5337 5336 STMF_SUCCESS) {
5338 5337 ilu->ilu_state = STMF_STATE_OFFLINE;
5339 5338 mutex_exit(&stmf_state.stmf_lock);
5340 5339 ((stmf_lu_t *)obj)->lu_ctl((stmf_lu_t *)obj,
5341 5340 STMF_ACK_LU_OFFLINE_COMPLETE, arg);
5342 5341 mutex_enter(&stmf_state.stmf_lock);
5343 5342 } else {
5344 5343 ilu->ilu_state = STMF_STATE_ONLINE;
5345 5344 stmf_add_lu_to_active_sessions((stmf_lu_t *)obj);
5346 5345 }
5347 5346 mutex_exit(&stmf_state.stmf_lock);
5348 5347 break;
5349 5348
5350 5349 /*
5351 5350 * LPORT_ONLINE/OFFLINE has nothing to do with link offline/online.
5352 5351 * It's related with hardware disable/enable.
5353 5352 */
5354 5353 case STMF_CMD_LPORT_ONLINE:
5355 5354 switch (ilport->ilport_state) {
5356 5355 case STMF_STATE_OFFLINE:
5357 5356 ret = STMF_SUCCESS;
5358 5357 break;
5359 5358 case STMF_STATE_ONLINE:
5360 5359 case STMF_STATE_ONLINING:
5361 5360 ret = STMF_ALREADY;
5362 5361 break;
5363 5362 case STMF_STATE_OFFLINING:
5364 5363 ret = STMF_BUSY;
5365 5364 break;
5366 5365 default:
5367 5366 ret = STMF_BADSTATE;
5368 5367 break;
5369 5368 }
5370 5369 if (ret != STMF_SUCCESS)
5371 5370 goto stmf_ctl_lock_exit;
5372 5371
5373 5372 /*
5374 5373 * Only user request can recover the port from the
5375 5374 * FORCED_OFFLINE state
5376 5375 */
5377 5376 if (ilport->ilport_flags & ILPORT_FORCED_OFFLINE) {
5378 5377 if (!(ssci->st_rflags & STMF_RFLAG_USER_REQUEST)) {
5379 5378 ret = STMF_FAILURE;
5380 5379 goto stmf_ctl_lock_exit;
5381 5380 }
5382 5381 }
5383 5382
5384 5383 /*
5385 5384 * Avoid too frequent request to online
5386 5385 */
5387 5386 if (ssci->st_rflags & STMF_RFLAG_USER_REQUEST) {
5388 5387 ilport->ilport_online_times = 0;
5389 5388 ilport->ilport_avg_interval = 0;
5390 5389 }
5391 5390 if ((ilport->ilport_avg_interval < STMF_AVG_ONLINE_INTERVAL) &&
5392 5391 (ilport->ilport_online_times >= 4)) {
5393 5392 ret = STMF_FAILURE;
5394 5393 ilport->ilport_flags |= ILPORT_FORCED_OFFLINE;
5395 5394 stmf_trace(NULL, "stmf_ctl: too frequent request to "
5396 5395 "online the port");
5397 5396 cmn_err(CE_WARN, "stmf_ctl: too frequent request to "
5398 5397 "online the port, set FORCED_OFFLINE now");
5399 5398 goto stmf_ctl_lock_exit;
5400 5399 }
5401 5400 if (ilport->ilport_online_times > 0) {
5402 5401 if (ilport->ilport_online_times == 1) {
5403 5402 ilport->ilport_avg_interval = ddi_get_lbolt() -
5404 5403 ilport->ilport_last_online_clock;
5405 5404 } else {
5406 5405 ilport->ilport_avg_interval =
5407 5406 (ilport->ilport_avg_interval +
5408 5407 ddi_get_lbolt() -
5409 5408 ilport->ilport_last_online_clock) >> 1;
5410 5409 }
5411 5410 }
5412 5411 ilport->ilport_last_online_clock = ddi_get_lbolt();
5413 5412 ilport->ilport_online_times++;
5414 5413
5415 5414 /*
5416 5415 * Submit online service request
5417 5416 */
5418 5417 ilport->ilport_flags &= ~ILPORT_FORCED_OFFLINE;
5419 5418 ilport->ilport_state = STMF_STATE_ONLINING;
5420 5419 mutex_exit(&stmf_state.stmf_lock);
5421 5420 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5422 5421 break;
5423 5422
5424 5423 case STMF_CMD_LPORT_ONLINE_COMPLETE:
5425 5424 if (ilport->ilport_state != STMF_STATE_ONLINING) {
5426 5425 ret = STMF_BADSTATE;
5427 5426 goto stmf_ctl_lock_exit;
5428 5427 }
5429 5428 if (((stmf_change_status_t *)arg)->st_completion_status ==
5430 5429 STMF_SUCCESS) {
5431 5430 ilport->ilport_state = STMF_STATE_ONLINE;
5432 5431 mutex_exit(&stmf_state.stmf_lock);
5433 5432 ((stmf_local_port_t *)obj)->lport_ctl(
5434 5433 (stmf_local_port_t *)obj,
5435 5434 STMF_ACK_LPORT_ONLINE_COMPLETE, arg);
5436 5435 mutex_enter(&stmf_state.stmf_lock);
5437 5436 } else {
5438 5437 ilport->ilport_state = STMF_STATE_OFFLINE;
5439 5438 }
5440 5439 ret = STMF_SUCCESS;
5441 5440 goto stmf_ctl_lock_exit;
5442 5441
5443 5442 case STMF_CMD_LPORT_OFFLINE:
5444 5443 switch (ilport->ilport_state) {
5445 5444 case STMF_STATE_ONLINE:
5446 5445 ret = STMF_SUCCESS;
5447 5446 break;
5448 5447 case STMF_STATE_OFFLINE:
5449 5448 case STMF_STATE_OFFLINING:
5450 5449 ret = STMF_ALREADY;
5451 5450 break;
5452 5451 case STMF_STATE_ONLINING:
5453 5452 ret = STMF_BUSY;
5454 5453 break;
5455 5454 default:
5456 5455 ret = STMF_BADSTATE;
5457 5456 break;
5458 5457 }
5459 5458 if (ret != STMF_SUCCESS)
5460 5459 goto stmf_ctl_lock_exit;
5461 5460
5462 5461 ilport->ilport_state = STMF_STATE_OFFLINING;
5463 5462 mutex_exit(&stmf_state.stmf_lock);
5464 5463 stmf_svc_queue(cmd, obj, (stmf_state_change_info_t *)arg);
5465 5464 break;
5466 5465
5467 5466 case STMF_CMD_LPORT_OFFLINE_COMPLETE:
5468 5467 if (ilport->ilport_state != STMF_STATE_OFFLINING) {
5469 5468 ret = STMF_BADSTATE;
5470 5469 goto stmf_ctl_lock_exit;
5471 5470 }
5472 5471 if (((stmf_change_status_t *)arg)->st_completion_status ==
5473 5472 STMF_SUCCESS) {
5474 5473 ilport->ilport_state = STMF_STATE_OFFLINE;
5475 5474 mutex_exit(&stmf_state.stmf_lock);
5476 5475 ((stmf_local_port_t *)obj)->lport_ctl(
5477 5476 (stmf_local_port_t *)obj,
5478 5477 STMF_ACK_LPORT_OFFLINE_COMPLETE, arg);
5479 5478 mutex_enter(&stmf_state.stmf_lock);
5480 5479 } else {
5481 5480 ilport->ilport_state = STMF_STATE_ONLINE;
5482 5481 }
5483 5482 mutex_exit(&stmf_state.stmf_lock);
5484 5483 break;
5485 5484
5486 5485 default:
5487 5486 cmn_err(CE_WARN, "Invalid ctl cmd received %x", cmd);
5488 5487 ret = STMF_INVALID_ARG;
5489 5488 goto stmf_ctl_lock_exit;
5490 5489 }
5491 5490
5492 5491 return (STMF_SUCCESS);
5493 5492
5494 5493 stmf_ctl_lock_exit:;
5495 5494 mutex_exit(&stmf_state.stmf_lock);
5496 5495 return (ret);
5497 5496 }
5498 5497
5499 5498 /* ARGSUSED */
5500 5499 stmf_status_t
5501 5500 stmf_info_impl(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5502 5501 uint32_t *bufsizep)
5503 5502 {
5504 5503 return (STMF_NOT_SUPPORTED);
5505 5504 }
5506 5505
5507 5506 /* ARGSUSED */
5508 5507 stmf_status_t
5509 5508 stmf_info(uint32_t cmd, void *arg1, void *arg2, uint8_t *buf,
5510 5509 uint32_t *bufsizep)
5511 5510 {
5512 5511 uint32_t cl = SI_GET_CLASS(cmd);
5513 5512
5514 5513 if (cl == SI_STMF) {
5515 5514 return (stmf_info_impl(cmd, arg1, arg2, buf, bufsizep));
5516 5515 }
5517 5516 if (cl == SI_LPORT) {
5518 5517 return (((stmf_local_port_t *)arg1)->lport_info(cmd, arg1,
5519 5518 arg2, buf, bufsizep));
5520 5519 } else if (cl == SI_LU) {
5521 5520 return (((stmf_lu_t *)arg1)->lu_info(cmd, arg1, arg2, buf,
5522 5521 bufsizep));
5523 5522 }
5524 5523
5525 5524 return (STMF_NOT_SUPPORTED);
5526 5525 }
5527 5526
5528 5527 /*
5529 5528 * Used by port providers. pwwn is 8 byte wwn, sdid is the devid used by
5530 5529 * stmf to register local ports. The ident should have 20 bytes in buffer
5531 5530 * space to convert the wwn to "wwn.xxxxxxxxxxxxxxxx" string.
5532 5531 */
5533 5532 void
5534 5533 stmf_wwn_to_devid_desc(scsi_devid_desc_t *sdid, uint8_t *wwn,
5535 5534 uint8_t protocol_id)
5536 5535 {
5537 5536 char wwn_str[20+1];
5538 5537
5539 5538 sdid->protocol_id = protocol_id;
5540 5539 sdid->piv = 1;
5541 5540 sdid->code_set = CODE_SET_ASCII;
5542 5541 sdid->association = ID_IS_TARGET_PORT;
5543 5542 sdid->ident_length = 20;
5544 5543 /* Convert wwn value to "wwn.XXXXXXXXXXXXXXXX" format */
5545 5544 (void) snprintf(wwn_str, sizeof (wwn_str),
5546 5545 "wwn.%02X%02X%02X%02X%02X%02X%02X%02X",
5547 5546 wwn[0], wwn[1], wwn[2], wwn[3], wwn[4], wwn[5], wwn[6], wwn[7]);
5548 5547 bcopy(wwn_str, (char *)sdid->ident, 20);
5549 5548 }
5550 5549
5551 5550
5552 5551 stmf_xfer_data_t *
5553 5552 stmf_prepare_tpgs_data(uint8_t ilu_alua)
5554 5553 {
5555 5554 stmf_xfer_data_t *xd;
5556 5555 stmf_i_local_port_t *ilport;
5557 5556 uint8_t *p;
5558 5557 uint32_t sz, asz, nports = 0, nports_standby = 0;
5559 5558
5560 5559 mutex_enter(&stmf_state.stmf_lock);
5561 5560 /* check if any ports are standby and create second group */
5562 5561 for (ilport = stmf_state.stmf_ilportlist; ilport;
5563 5562 ilport = ilport->ilport_next) {
5564 5563 if (ilport->ilport_standby == 1) {
5565 5564 nports_standby++;
5566 5565 } else {
5567 5566 nports++;
5568 5567 }
5569 5568 }
5570 5569
5571 5570 /* The spec only allows for 255 ports to be reported per group */
5572 5571 nports = min(nports, 255);
5573 5572 nports_standby = min(nports_standby, 255);
5574 5573 sz = (nports * 4) + 12;
5575 5574 if (nports_standby && ilu_alua) {
5576 5575 sz += (nports_standby * 4) + 8;
5577 5576 }
5578 5577 asz = sz + sizeof (*xd) - 4;
5579 5578 xd = (stmf_xfer_data_t *)kmem_zalloc(asz, KM_NOSLEEP);
5580 5579 if (xd == NULL) {
5581 5580 mutex_exit(&stmf_state.stmf_lock);
5582 5581 return (NULL);
5583 5582 }
5584 5583 xd->alloc_size = asz;
5585 5584 xd->size_left = sz;
5586 5585
5587 5586 p = xd->buf;
5588 5587
5589 5588 *((uint32_t *)p) = BE_32(sz - 4);
5590 5589 p += 4;
5591 5590 p[0] = 0x80; /* PREF */
5592 5591 p[1] = 5; /* AO_SUP, S_SUP */
5593 5592 if (stmf_state.stmf_alua_node == 1) {
5594 5593 p[3] = 1; /* Group 1 */
5595 5594 } else {
5596 5595 p[3] = 0; /* Group 0 */
5597 5596 }
5598 5597 p[7] = nports & 0xff;
5599 5598 p += 8;
5600 5599 for (ilport = stmf_state.stmf_ilportlist; ilport;
5601 5600 ilport = ilport->ilport_next) {
5602 5601 if (ilport->ilport_standby == 1) {
5603 5602 continue;
5604 5603 }
5605 5604 ((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5606 5605 p += 4;
5607 5606 }
5608 5607 if (nports_standby && ilu_alua) {
5609 5608 p[0] = 0x02; /* Non PREF, Standby */
5610 5609 p[1] = 5; /* AO_SUP, S_SUP */
5611 5610 if (stmf_state.stmf_alua_node == 1) {
5612 5611 p[3] = 0; /* Group 0 */
5613 5612 } else {
5614 5613 p[3] = 1; /* Group 1 */
5615 5614 }
5616 5615 p[7] = nports_standby & 0xff;
5617 5616 p += 8;
5618 5617 for (ilport = stmf_state.stmf_ilportlist; ilport;
5619 5618 ilport = ilport->ilport_next) {
5620 5619 if (ilport->ilport_standby == 0) {
5621 5620 continue;
5622 5621 }
5623 5622 ((uint16_t *)p)[1] = BE_16(ilport->ilport_rtpid);
5624 5623 p += 4;
5625 5624 }
5626 5625 }
5627 5626
5628 5627 mutex_exit(&stmf_state.stmf_lock);
5629 5628
5630 5629 return (xd);
5631 5630 }
5632 5631
5633 5632 struct scsi_devid_desc *
5634 5633 stmf_scsilib_get_devid_desc(uint16_t rtpid)
5635 5634 {
5636 5635 scsi_devid_desc_t *devid = NULL;
5637 5636 stmf_i_local_port_t *ilport;
5638 5637
5639 5638 mutex_enter(&stmf_state.stmf_lock);
5640 5639
5641 5640 for (ilport = stmf_state.stmf_ilportlist; ilport;
5642 5641 ilport = ilport->ilport_next) {
5643 5642 if (ilport->ilport_rtpid == rtpid) {
5644 5643 scsi_devid_desc_t *id = ilport->ilport_lport->lport_id;
5645 5644 uint32_t id_sz = sizeof (scsi_devid_desc_t) +
5646 5645 id->ident_length;
5647 5646 devid = (scsi_devid_desc_t *)kmem_zalloc(id_sz,
5648 5647 KM_NOSLEEP);
5649 5648 if (devid != NULL) {
5650 5649 bcopy(id, devid, id_sz);
5651 5650 }
5652 5651 break;
5653 5652 }
5654 5653 }
5655 5654
5656 5655 mutex_exit(&stmf_state.stmf_lock);
5657 5656 return (devid);
5658 5657 }
5659 5658
5660 5659 uint16_t
5661 5660 stmf_scsilib_get_lport_rtid(struct scsi_devid_desc *devid)
5662 5661 {
5663 5662 stmf_i_local_port_t *ilport;
5664 5663 scsi_devid_desc_t *id;
5665 5664 uint16_t rtpid = 0;
5666 5665
5667 5666 mutex_enter(&stmf_state.stmf_lock);
5668 5667 for (ilport = stmf_state.stmf_ilportlist; ilport;
5669 5668 ilport = ilport->ilport_next) {
5670 5669 id = ilport->ilport_lport->lport_id;
5671 5670 if ((devid->ident_length == id->ident_length) &&
5672 5671 (memcmp(devid->ident, id->ident, id->ident_length) == 0)) {
5673 5672 rtpid = ilport->ilport_rtpid;
5674 5673 break;
5675 5674 }
5676 5675 }
5677 5676 mutex_exit(&stmf_state.stmf_lock);
5678 5677 return (rtpid);
5679 5678 }
5680 5679
5681 5680 static uint16_t stmf_lu_id_gen_number = 0;
5682 5681
5683 5682 stmf_status_t
5684 5683 stmf_scsilib_uniq_lu_id(uint32_t company_id, scsi_devid_desc_t *lu_id)
5685 5684 {
5686 5685 return (stmf_scsilib_uniq_lu_id2(company_id, 0, lu_id));
5687 5686 }
5688 5687
5689 5688 stmf_status_t
5690 5689 stmf_scsilib_uniq_lu_id2(uint32_t company_id, uint32_t host_id,
5691 5690 scsi_devid_desc_t *lu_id)
5692 5691 {
5693 5692 uint8_t *p;
5694 5693 struct timeval32 timestamp32;
5695 5694 uint32_t *t = (uint32_t *)×tamp32;
5696 5695 struct ether_addr mac;
5697 5696 uint8_t *e = (uint8_t *)&mac;
5698 5697 int hid = (int)host_id;
5699 5698 uint16_t gen_number;
5700 5699
5701 5700 if (company_id == COMPANY_ID_NONE)
5702 5701 company_id = COMPANY_ID_SUN;
5703 5702
5704 5703 if (lu_id->ident_length != 0x10)
5705 5704 return (STMF_INVALID_ARG);
5706 5705
5707 5706 p = (uint8_t *)lu_id;
5708 5707
5709 5708 gen_number = atomic_inc_16_nv(&stmf_lu_id_gen_number);
5710 5709
5711 5710 p[0] = 0xf1; p[1] = 3; p[2] = 0; p[3] = 0x10;
5712 5711 p[4] = ((company_id >> 20) & 0xf) | 0x60;
5713 5712 p[5] = (company_id >> 12) & 0xff;
5714 5713 p[6] = (company_id >> 4) & 0xff;
5715 5714 p[7] = (company_id << 4) & 0xf0;
5716 5715 if (hid == 0 && !localetheraddr((struct ether_addr *)NULL, &mac)) {
5717 5716 hid = BE_32((int)zone_get_hostid(NULL));
5718 5717 }
5719 5718 if (hid != 0) {
5720 5719 e[0] = (hid >> 24) & 0xff;
5721 5720 e[1] = (hid >> 16) & 0xff;
5722 5721 e[2] = (hid >> 8) & 0xff;
5723 5722 e[3] = hid & 0xff;
5724 5723 e[4] = e[5] = 0;
5725 5724 }
5726 5725 bcopy(e, p+8, 6);
5727 5726 uniqtime32(×tamp32);
5728 5727 *t = BE_32(*t);
5729 5728 bcopy(t, p+14, 4);
5730 5729 p[18] = (gen_number >> 8) & 0xff;
5731 5730 p[19] = gen_number & 0xff;
5732 5731
5733 5732 return (STMF_SUCCESS);
5734 5733 }
5735 5734
5736 5735 /*
5737 5736 * saa is sense key, ASC, ASCQ
5738 5737 */
5739 5738 void
5740 5739 stmf_scsilib_send_status(scsi_task_t *task, uint8_t st, uint32_t saa)
5741 5740 {
5742 5741 uint8_t sd[18];
5743 5742 task->task_scsi_status = st;
5744 5743 if (st == 2) {
5745 5744 bzero(sd, 18);
5746 5745 sd[0] = 0x70;
5747 5746 sd[2] = (saa >> 16) & 0xf;
5748 5747 sd[7] = 10;
5749 5748 sd[12] = (saa >> 8) & 0xff;
5750 5749 sd[13] = saa & 0xff;
5751 5750 task->task_sense_data = sd;
5752 5751 task->task_sense_length = 18;
5753 5752 } else {
5754 5753 task->task_sense_data = NULL;
5755 5754 task->task_sense_length = 0;
5756 5755 }
5757 5756 (void) stmf_send_scsi_status(task, STMF_IOF_LU_DONE);
5758 5757 }
5759 5758
5760 5759 uint32_t
5761 5760 stmf_scsilib_prepare_vpd_page83(scsi_task_t *task, uint8_t *page,
5762 5761 uint32_t page_len, uint8_t byte0, uint32_t vpd_mask)
5763 5762 {
5764 5763 uint8_t *p = NULL;
5765 5764 uint8_t small_buf[32];
5766 5765 uint32_t sz = 0;
5767 5766 uint32_t n = 4;
5768 5767 uint32_t m = 0;
5769 5768 uint32_t last_bit = 0;
5770 5769
5771 5770 if (page_len < 4)
5772 5771 return (0);
5773 5772 if (page_len > 65535)
5774 5773 page_len = 65535;
5775 5774
5776 5775 page[0] = byte0;
5777 5776 page[1] = 0x83;
5778 5777
5779 5778 /* CONSTCOND */
5780 5779 while (1) {
5781 5780 m += sz;
5782 5781 if (sz && (page_len > n)) {
5783 5782 uint32_t copysz;
5784 5783 copysz = page_len > (n + sz) ? sz : page_len - n;
5785 5784 bcopy(p, page + n, copysz);
5786 5785 n += copysz;
5787 5786 }
5788 5787 vpd_mask &= ~last_bit;
5789 5788 if (vpd_mask == 0)
5790 5789 break;
5791 5790
5792 5791 if (vpd_mask & STMF_VPD_LU_ID) {
5793 5792 last_bit = STMF_VPD_LU_ID;
5794 5793 sz = task->task_lu->lu_id->ident_length + 4;
5795 5794 p = (uint8_t *)task->task_lu->lu_id;
5796 5795 continue;
5797 5796 } else if (vpd_mask & STMF_VPD_TARGET_ID) {
5798 5797 last_bit = STMF_VPD_TARGET_ID;
5799 5798 sz = task->task_lport->lport_id->ident_length + 4;
5800 5799 p = (uint8_t *)task->task_lport->lport_id;
5801 5800 continue;
5802 5801 } else if (vpd_mask & STMF_VPD_TP_GROUP) {
5803 5802 stmf_i_local_port_t *ilport;
5804 5803 last_bit = STMF_VPD_TP_GROUP;
5805 5804 p = small_buf;
5806 5805 bzero(p, 8);
5807 5806 p[0] = 1;
5808 5807 p[1] = 0x15;
5809 5808 p[3] = 4;
5810 5809 ilport = (stmf_i_local_port_t *)
5811 5810 task->task_lport->lport_stmf_private;
5812 5811 /*
5813 5812 * If we're in alua mode, group 1 contains all alua
5814 5813 * participating ports and all standby ports
5815 5814 * > 255. Otherwise, if we're in alua mode, any local
5816 5815 * ports (non standby/pppt) are also in group 1 if the
5817 5816 * alua node is 1. Otherwise the group is 0.
5818 5817 */
5819 5818 if ((stmf_state.stmf_alua_state &&
5820 5819 (ilport->ilport_alua || ilport->ilport_standby) &&
5821 5820 ilport->ilport_rtpid > 255) ||
5822 5821 (stmf_state.stmf_alua_node == 1 &&
5823 5822 ilport->ilport_standby != 1)) {
5824 5823 p[7] = 1; /* Group 1 */
5825 5824 }
5826 5825 sz = 8;
5827 5826 continue;
5828 5827 } else if (vpd_mask & STMF_VPD_RELATIVE_TP_ID) {
5829 5828 stmf_i_local_port_t *ilport;
5830 5829
5831 5830 last_bit = STMF_VPD_RELATIVE_TP_ID;
5832 5831 p = small_buf;
5833 5832 bzero(p, 8);
5834 5833 p[0] = 1;
5835 5834 p[1] = 0x14;
5836 5835 p[3] = 4;
5837 5836 ilport = (stmf_i_local_port_t *)
5838 5837 task->task_lport->lport_stmf_private;
5839 5838 p[6] = (ilport->ilport_rtpid >> 8) & 0xff;
5840 5839 p[7] = ilport->ilport_rtpid & 0xff;
5841 5840 sz = 8;
5842 5841 continue;
5843 5842 } else {
5844 5843 cmn_err(CE_WARN, "Invalid vpd_mask");
5845 5844 break;
5846 5845 }
5847 5846 }
5848 5847
5849 5848 page[2] = (m >> 8) & 0xff;
5850 5849 page[3] = m & 0xff;
5851 5850
5852 5851 return (n);
5853 5852 }
5854 5853
5855 5854 void
5856 5855 stmf_scsilib_handle_report_tpgs(scsi_task_t *task, stmf_data_buf_t *dbuf)
5857 5856 {
5858 5857 stmf_i_scsi_task_t *itask =
5859 5858 (stmf_i_scsi_task_t *)task->task_stmf_private;
5860 5859 stmf_i_lu_t *ilu =
5861 5860 (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5862 5861 stmf_xfer_data_t *xd;
5863 5862 uint32_t sz, minsz;
5864 5863
5865 5864 itask->itask_flags |= ITASK_DEFAULT_HANDLING;
5866 5865 task->task_cmd_xfer_length =
5867 5866 ((((uint32_t)task->task_cdb[6]) << 24) |
5868 5867 (((uint32_t)task->task_cdb[7]) << 16) |
5869 5868 (((uint32_t)task->task_cdb[8]) << 8) |
5870 5869 ((uint32_t)task->task_cdb[9]));
5871 5870
5872 5871 if (task->task_additional_flags &
5873 5872 TASK_AF_NO_EXPECTED_XFER_LENGTH) {
5874 5873 task->task_expected_xfer_length =
5875 5874 task->task_cmd_xfer_length;
5876 5875 }
5877 5876
5878 5877 if (task->task_cmd_xfer_length == 0) {
5879 5878 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
5880 5879 return;
5881 5880 }
5882 5881 if (task->task_cmd_xfer_length < 4) {
5883 5882 stmf_scsilib_send_status(task, STATUS_CHECK,
5884 5883 STMF_SAA_INVALID_FIELD_IN_CDB);
5885 5884 return;
5886 5885 }
5887 5886
5888 5887 sz = min(task->task_expected_xfer_length,
5889 5888 task->task_cmd_xfer_length);
5890 5889
5891 5890 xd = stmf_prepare_tpgs_data(ilu->ilu_alua);
5892 5891
5893 5892 if (xd == NULL) {
5894 5893 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5895 5894 STMF_ALLOC_FAILURE, NULL);
5896 5895 return;
5897 5896 }
5898 5897
5899 5898 sz = min(sz, xd->size_left);
5900 5899 xd->size_left = sz;
5901 5900 minsz = min(512, sz);
5902 5901
5903 5902 if (dbuf == NULL)
5904 5903 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
5905 5904 if (dbuf == NULL) {
5906 5905 kmem_free(xd, xd->alloc_size);
5907 5906 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
5908 5907 STMF_ALLOC_FAILURE, NULL);
5909 5908 return;
5910 5909 }
5911 5910 dbuf->db_lu_private = xd;
5912 5911 stmf_xd_to_dbuf(dbuf, 1);
5913 5912
5914 5913 dbuf->db_flags = DB_DIRECTION_TO_RPORT;
5915 5914 (void) stmf_xfer_data(task, dbuf, 0);
5916 5915
5917 5916 }
5918 5917
5919 5918 void
5920 5919 stmf_scsilib_handle_task_mgmt(scsi_task_t *task)
5921 5920 {
5922 5921
5923 5922 switch (task->task_mgmt_function) {
5924 5923 /*
5925 5924 * For now we will abort all I/Os on the LU in case of ABORT_TASK_SET
5926 5925 * and ABORT_TASK. But unlike LUN_RESET we will not reset LU state
5927 5926 * in these cases. This needs to be changed to abort only the required
5928 5927 * set.
5929 5928 */
5930 5929 case TM_ABORT_TASK:
5931 5930 case TM_ABORT_TASK_SET:
5932 5931 case TM_CLEAR_TASK_SET:
5933 5932 case TM_LUN_RESET:
5934 5933 stmf_handle_lun_reset(task);
5935 5934 /* issue the reset to the proxy node as well */
5936 5935 if (stmf_state.stmf_alua_state == 1) {
5937 5936 (void) stmf_proxy_scsi_cmd(task, NULL);
5938 5937 }
5939 5938 return;
5940 5939 case TM_TARGET_RESET:
5941 5940 case TM_TARGET_COLD_RESET:
5942 5941 case TM_TARGET_WARM_RESET:
5943 5942 stmf_handle_target_reset(task);
5944 5943 return;
5945 5944 default:
5946 5945 /* We dont support this task mgmt function */
5947 5946 stmf_scsilib_send_status(task, STATUS_CHECK,
5948 5947 STMF_SAA_INVALID_FIELD_IN_CMD_IU);
5949 5948 return;
5950 5949 }
5951 5950 }
5952 5951
5953 5952 void
5954 5953 stmf_handle_lun_reset(scsi_task_t *task)
5955 5954 {
5956 5955 stmf_i_scsi_task_t *itask;
5957 5956 stmf_i_lu_t *ilu;
5958 5957
5959 5958 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
5960 5959 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
5961 5960
5962 5961 /*
5963 5962 * To sync with target reset, grab this lock. The LU is not going
5964 5963 * anywhere as there is atleast one task pending (this task).
5965 5964 */
5966 5965 mutex_enter(&stmf_state.stmf_lock);
5967 5966
5968 5967 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
5969 5968 mutex_exit(&stmf_state.stmf_lock);
5970 5969 stmf_scsilib_send_status(task, STATUS_CHECK,
5971 5970 STMF_SAA_OPERATION_IN_PROGRESS);
5972 5971 return;
5973 5972 }
5974 5973 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
5975 5974 mutex_exit(&stmf_state.stmf_lock);
5976 5975
5977 5976 /*
5978 5977 * Mark this task as the one causing LU reset so that we know who
5979 5978 * was responsible for setting the ILU_RESET_ACTIVE. In case this
5980 5979 * task itself gets aborted, we will clear ILU_RESET_ACTIVE.
5981 5980 */
5982 5981 itask->itask_flags |= ITASK_DEFAULT_HANDLING | ITASK_CAUSING_LU_RESET;
5983 5982
5984 5983 /* Initiatiate abort on all commands on this LU except this one */
5985 5984 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED, task->task_lu);
5986 5985
5987 5986 /* Start polling on this task */
5988 5987 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
5989 5988 != STMF_SUCCESS) {
5990 5989 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
5991 5990 NULL);
5992 5991 return;
5993 5992 }
5994 5993 }
5995 5994
5996 5995 void
5997 5996 stmf_handle_target_reset(scsi_task_t *task)
5998 5997 {
5999 5998 stmf_i_scsi_task_t *itask;
6000 5999 stmf_i_lu_t *ilu;
6001 6000 stmf_i_scsi_session_t *iss;
6002 6001 stmf_lun_map_t *lm;
6003 6002 stmf_lun_map_ent_t *lm_ent;
6004 6003 int i, lf;
6005 6004
6006 6005 itask = (stmf_i_scsi_task_t *)task->task_stmf_private;
6007 6006 iss = (stmf_i_scsi_session_t *)task->task_session->ss_stmf_private;
6008 6007 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6009 6008
6010 6009 /*
6011 6010 * To sync with LUN reset, grab this lock. The session is not going
6012 6011 * anywhere as there is atleast one task pending (this task).
6013 6012 */
6014 6013 mutex_enter(&stmf_state.stmf_lock);
6015 6014
6016 6015 /* Grab the session lock as a writer to prevent any changes in it */
6017 6016 rw_enter(iss->iss_lockp, RW_WRITER);
6018 6017
6019 6018 if (iss->iss_flags & ISS_RESET_ACTIVE) {
6020 6019 rw_exit(iss->iss_lockp);
6021 6020 mutex_exit(&stmf_state.stmf_lock);
6022 6021 stmf_scsilib_send_status(task, STATUS_CHECK,
6023 6022 STMF_SAA_OPERATION_IN_PROGRESS);
6024 6023 return;
6025 6024 }
6026 6025 atomic_or_32(&iss->iss_flags, ISS_RESET_ACTIVE);
6027 6026
6028 6027 /*
6029 6028 * Now go through each LUN in this session and make sure all of them
6030 6029 * can be reset.
6031 6030 */
6032 6031 lm = iss->iss_sm;
6033 6032 for (i = 0, lf = 0; i < lm->lm_nentries; i++) {
6034 6033 if (lm->lm_plus[i] == NULL)
6035 6034 continue;
6036 6035 lf++;
6037 6036 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6038 6037 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6039 6038 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6040 6039 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6041 6040 rw_exit(iss->iss_lockp);
6042 6041 mutex_exit(&stmf_state.stmf_lock);
6043 6042 stmf_scsilib_send_status(task, STATUS_CHECK,
6044 6043 STMF_SAA_OPERATION_IN_PROGRESS);
6045 6044 return;
6046 6045 }
6047 6046 }
6048 6047 if (lf == 0) {
6049 6048 /* No luns in this session */
6050 6049 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6051 6050 rw_exit(iss->iss_lockp);
6052 6051 mutex_exit(&stmf_state.stmf_lock);
6053 6052 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6054 6053 return;
6055 6054 }
6056 6055
6057 6056 /* ok, start the damage */
6058 6057 itask->itask_flags |= ITASK_DEFAULT_HANDLING |
6059 6058 ITASK_CAUSING_TARGET_RESET;
6060 6059 for (i = 0; i < lm->lm_nentries; i++) {
6061 6060 if (lm->lm_plus[i] == NULL)
6062 6061 continue;
6063 6062 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6064 6063 ilu = (stmf_i_lu_t *)(lm_ent->ent_lu->lu_stmf_private);
6065 6064 atomic_or_32(&ilu->ilu_flags, ILU_RESET_ACTIVE);
6066 6065 }
6067 6066
6068 6067 for (i = 0; i < lm->lm_nentries; i++) {
6069 6068 if (lm->lm_plus[i] == NULL)
6070 6069 continue;
6071 6070 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6072 6071 stmf_abort(STMF_QUEUE_ABORT_LU, task, STMF_ABORTED,
6073 6072 lm_ent->ent_lu);
6074 6073 }
6075 6074
6076 6075 rw_exit(iss->iss_lockp);
6077 6076 mutex_exit(&stmf_state.stmf_lock);
6078 6077
6079 6078 /* Start polling on this task */
6080 6079 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6081 6080 != STMF_SUCCESS) {
6082 6081 stmf_abort(STMF_QUEUE_TASK_ABORT, task, STMF_ALLOC_FAILURE,
6083 6082 NULL);
6084 6083 return;
6085 6084 }
6086 6085 }
6087 6086
6088 6087 int
6089 6088 stmf_handle_cmd_during_ic(stmf_i_scsi_task_t *itask)
6090 6089 {
6091 6090 scsi_task_t *task = itask->itask_task;
6092 6091 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6093 6092 task->task_session->ss_stmf_private;
6094 6093
6095 6094 rw_enter(iss->iss_lockp, RW_WRITER);
6096 6095 if (((iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) == 0) ||
6097 6096 (task->task_cdb[0] == SCMD_INQUIRY)) {
6098 6097 rw_exit(iss->iss_lockp);
6099 6098 return (0);
6100 6099 }
6101 6100 atomic_and_32(&iss->iss_flags,
6102 6101 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6103 6102 rw_exit(iss->iss_lockp);
6104 6103
6105 6104 if (task->task_cdb[0] == SCMD_REPORT_LUNS) {
6106 6105 return (0);
6107 6106 }
6108 6107 stmf_scsilib_send_status(task, STATUS_CHECK,
6109 6108 STMF_SAA_REPORT_LUN_DATA_HAS_CHANGED);
6110 6109 return (1);
6111 6110 }
6112 6111
6113 6112 void
6114 6113 stmf_worker_init()
6115 6114 {
6116 6115 uint32_t i;
6117 6116
6118 6117 /* Make local copy of global tunables */
6119 6118 stmf_i_max_nworkers = stmf_max_nworkers;
6120 6119 stmf_i_min_nworkers = stmf_min_nworkers;
6121 6120
6122 6121 ASSERT(stmf_workers == NULL);
6123 6122 if (stmf_i_min_nworkers < 4) {
6124 6123 stmf_i_min_nworkers = 4;
6125 6124 }
6126 6125 if (stmf_i_max_nworkers < stmf_i_min_nworkers) {
6127 6126 stmf_i_max_nworkers = stmf_i_min_nworkers;
6128 6127 }
6129 6128 stmf_workers = (stmf_worker_t *)kmem_zalloc(
6130 6129 sizeof (stmf_worker_t) * stmf_i_max_nworkers, KM_SLEEP);
6131 6130 for (i = 0; i < stmf_i_max_nworkers; i++) {
6132 6131 stmf_worker_t *w = &stmf_workers[i];
6133 6132 mutex_init(&w->worker_lock, NULL, MUTEX_DRIVER, NULL);
6134 6133 cv_init(&w->worker_cv, NULL, CV_DRIVER, NULL);
6135 6134 }
6136 6135 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6137 6136 stmf_workers_state = STMF_WORKERS_ENABLED;
6138 6137
6139 6138 /* Workers will be started by stmf_worker_mgmt() */
6140 6139
6141 6140 /* Lets wait for atleast one worker to start */
6142 6141 while (stmf_nworkers_cur == 0)
6143 6142 delay(drv_usectohz(20 * 1000));
6144 6143 stmf_worker_mgmt_delay = drv_usectohz(3 * 1000 * 1000);
6145 6144 }
6146 6145
6147 6146 stmf_status_t
6148 6147 stmf_worker_fini()
6149 6148 {
6150 6149 int i;
6151 6150 clock_t sb;
6152 6151
6153 6152 if (stmf_workers_state == STMF_WORKERS_DISABLED)
6154 6153 return (STMF_SUCCESS);
6155 6154 ASSERT(stmf_workers);
6156 6155 stmf_workers_state = STMF_WORKERS_DISABLED;
6157 6156 stmf_worker_mgmt_delay = drv_usectohz(20 * 1000);
6158 6157 cv_signal(&stmf_state.stmf_cv);
6159 6158
6160 6159 sb = ddi_get_lbolt() + drv_usectohz(10 * 1000 * 1000);
6161 6160 /* Wait for all the threads to die */
6162 6161 while (stmf_nworkers_cur != 0) {
6163 6162 if (ddi_get_lbolt() > sb) {
6164 6163 stmf_workers_state = STMF_WORKERS_ENABLED;
6165 6164 return (STMF_BUSY);
6166 6165 }
6167 6166 delay(drv_usectohz(100 * 1000));
6168 6167 }
6169 6168 for (i = 0; i < stmf_i_max_nworkers; i++) {
6170 6169 stmf_worker_t *w = &stmf_workers[i];
6171 6170 mutex_destroy(&w->worker_lock);
6172 6171 cv_destroy(&w->worker_cv);
6173 6172 }
6174 6173 kmem_free(stmf_workers, sizeof (stmf_worker_t) * stmf_i_max_nworkers);
6175 6174 stmf_workers = NULL;
6176 6175
6177 6176 return (STMF_SUCCESS);
6178 6177 }
6179 6178
6180 6179 void
6181 6180 stmf_worker_task(void *arg)
6182 6181 {
6183 6182 stmf_worker_t *w;
6184 6183 stmf_i_scsi_session_t *iss;
6185 6184 scsi_task_t *task;
6186 6185 stmf_i_scsi_task_t *itask;
6187 6186 stmf_data_buf_t *dbuf;
6188 6187 stmf_lu_t *lu;
6189 6188 clock_t wait_timer = 0;
6190 6189 clock_t wait_ticks, wait_delta = 0;
6191 6190 uint32_t old, new;
6192 6191 uint8_t curcmd;
6193 6192 uint8_t abort_free;
6194 6193 uint8_t wait_queue;
6195 6194 uint8_t dec_qdepth;
6196 6195
6197 6196 w = (stmf_worker_t *)arg;
6198 6197 wait_ticks = drv_usectohz(10000);
6199 6198
6200 6199 DTRACE_PROBE1(worker__create, stmf_worker_t, w);
6201 6200 mutex_enter(&w->worker_lock);
6202 6201 w->worker_flags |= STMF_WORKER_STARTED | STMF_WORKER_ACTIVE;
6203 6202 stmf_worker_loop:;
6204 6203 if ((w->worker_ref_count == 0) &&
6205 6204 (w->worker_flags & STMF_WORKER_TERMINATE)) {
6206 6205 w->worker_flags &= ~(STMF_WORKER_STARTED |
6207 6206 STMF_WORKER_ACTIVE | STMF_WORKER_TERMINATE);
6208 6207 w->worker_tid = NULL;
6209 6208 mutex_exit(&w->worker_lock);
6210 6209 DTRACE_PROBE1(worker__destroy, stmf_worker_t, w);
6211 6210 thread_exit();
6212 6211 }
6213 6212 /* CONSTCOND */
6214 6213 while (1) {
6215 6214 dec_qdepth = 0;
6216 6215 if (wait_timer && (ddi_get_lbolt() >= wait_timer)) {
6217 6216 wait_timer = 0;
6218 6217 wait_delta = 0;
6219 6218 if (w->worker_wait_head) {
6220 6219 ASSERT(w->worker_wait_tail);
6221 6220 if (w->worker_task_head == NULL)
6222 6221 w->worker_task_head =
6223 6222 w->worker_wait_head;
6224 6223 else
6225 6224 w->worker_task_tail->itask_worker_next =
6226 6225 w->worker_wait_head;
6227 6226 w->worker_task_tail = w->worker_wait_tail;
6228 6227 w->worker_wait_head = w->worker_wait_tail =
6229 6228 NULL;
6230 6229 }
6231 6230 }
6232 6231 if ((itask = w->worker_task_head) == NULL) {
6233 6232 break;
6234 6233 }
6235 6234 task = itask->itask_task;
6236 6235 DTRACE_PROBE2(worker__active, stmf_worker_t, w,
6237 6236 scsi_task_t *, task);
6238 6237 w->worker_task_head = itask->itask_worker_next;
6239 6238 if (w->worker_task_head == NULL)
6240 6239 w->worker_task_tail = NULL;
6241 6240
6242 6241 wait_queue = 0;
6243 6242 abort_free = 0;
6244 6243 if (itask->itask_ncmds > 0) {
6245 6244 curcmd = itask->itask_cmd_stack[itask->itask_ncmds - 1];
6246 6245 } else {
6247 6246 ASSERT(itask->itask_flags & ITASK_BEING_ABORTED);
6248 6247 }
6249 6248 do {
6250 6249 old = itask->itask_flags;
6251 6250 if (old & ITASK_BEING_ABORTED) {
6252 6251 itask->itask_ncmds = 1;
6253 6252 curcmd = itask->itask_cmd_stack[0] =
6254 6253 ITASK_CMD_ABORT;
6255 6254 goto out_itask_flag_loop;
6256 6255 } else if ((curcmd & ITASK_CMD_MASK) ==
6257 6256 ITASK_CMD_NEW_TASK) {
6258 6257 /*
6259 6258 * set ITASK_KSTAT_IN_RUNQ, this flag
6260 6259 * will not reset until task completed
6261 6260 */
6262 6261 new = old | ITASK_KNOWN_TO_LU |
6263 6262 ITASK_KSTAT_IN_RUNQ;
6264 6263 } else {
6265 6264 goto out_itask_flag_loop;
6266 6265 }
6267 6266 } while (atomic_cas_32(&itask->itask_flags, old, new) != old);
6268 6267
6269 6268 out_itask_flag_loop:
6270 6269
6271 6270 /*
6272 6271 * Decide if this task needs to go to a queue and/or if
6273 6272 * we can decrement the itask_cmd_stack.
6274 6273 */
6275 6274 if (curcmd == ITASK_CMD_ABORT) {
6276 6275 if (itask->itask_flags & (ITASK_KNOWN_TO_LU |
6277 6276 ITASK_KNOWN_TO_TGT_PORT)) {
6278 6277 wait_queue = 1;
6279 6278 } else {
6280 6279 abort_free = 1;
6281 6280 }
6282 6281 } else if ((curcmd & ITASK_CMD_POLL) &&
6283 6282 (itask->itask_poll_timeout > ddi_get_lbolt())) {
6284 6283 wait_queue = 1;
6285 6284 }
6286 6285
6287 6286 if (wait_queue) {
6288 6287 itask->itask_worker_next = NULL;
6289 6288 if (w->worker_wait_tail) {
6290 6289 w->worker_wait_tail->itask_worker_next = itask;
6291 6290 } else {
6292 6291 w->worker_wait_head = itask;
6293 6292 }
6294 6293 w->worker_wait_tail = itask;
6295 6294 if (wait_timer == 0) {
6296 6295 wait_timer = ddi_get_lbolt() + wait_ticks;
6297 6296 wait_delta = wait_ticks;
6298 6297 }
6299 6298 } else if ((--(itask->itask_ncmds)) != 0) {
6300 6299 itask->itask_worker_next = NULL;
6301 6300 if (w->worker_task_tail) {
6302 6301 w->worker_task_tail->itask_worker_next = itask;
6303 6302 } else {
6304 6303 w->worker_task_head = itask;
6305 6304 }
6306 6305 w->worker_task_tail = itask;
6307 6306 } else {
6308 6307 atomic_and_32(&itask->itask_flags,
6309 6308 ~ITASK_IN_WORKER_QUEUE);
6310 6309 /*
6311 6310 * This is where the queue depth should go down by
6312 6311 * one but we delay that on purpose to account for
6313 6312 * the call into the provider. The actual decrement
6314 6313 * happens after the worker has done its job.
6315 6314 */
6316 6315 dec_qdepth = 1;
6317 6316 itask->itask_waitq_time +=
6318 6317 gethrtime() - itask->itask_waitq_enter_timestamp;
6319 6318 }
6320 6319
6321 6320 /* We made it here means we are going to call LU */
6322 6321 if ((itask->itask_flags & ITASK_DEFAULT_HANDLING) == 0)
6323 6322 lu = task->task_lu;
6324 6323 else
6325 6324 lu = dlun0;
6326 6325 dbuf = itask->itask_dbufs[ITASK_CMD_BUF_NDX(curcmd)];
6327 6326 mutex_exit(&w->worker_lock);
6328 6327 curcmd &= ITASK_CMD_MASK;
6329 6328 stmf_task_audit(itask, TE_PROCESS_CMD, curcmd, dbuf);
6330 6329 switch (curcmd) {
6331 6330 case ITASK_CMD_NEW_TASK:
6332 6331 iss = (stmf_i_scsi_session_t *)
6333 6332 task->task_session->ss_stmf_private;
6334 6333 stmf_itl_lu_new_task(itask);
6335 6334 if (iss->iss_flags & ISS_LUN_INVENTORY_CHANGED) {
6336 6335 if (stmf_handle_cmd_during_ic(itask))
6337 6336 break;
6338 6337 }
6339 6338 #ifdef DEBUG
6340 6339 if (stmf_drop_task_counter > 0) {
6341 6340 if (atomic_dec_32_nv(&stmf_drop_task_counter)
6342 6341 == 1)
6343 6342 break;
6344 6343 }
6345 6344 #endif
6346 6345 DTRACE_PROBE1(scsi__task__start, scsi_task_t *, task);
6347 6346 lu->lu_new_task(task, dbuf);
6348 6347 break;
6349 6348 case ITASK_CMD_DATA_XFER_DONE:
6350 6349 lu->lu_dbuf_xfer_done(task, dbuf);
6351 6350 break;
6352 6351 case ITASK_CMD_STATUS_DONE:
6353 6352 lu->lu_send_status_done(task);
6354 6353 break;
6355 6354 case ITASK_CMD_ABORT:
6356 6355 if (abort_free) {
6357 6356 stmf_task_free(task);
6358 6357 } else {
6359 6358 stmf_do_task_abort(task);
6360 6359 }
6361 6360 break;
6362 6361 case ITASK_CMD_POLL_LU:
6363 6362 if (!wait_queue) {
6364 6363 lu->lu_task_poll(task);
6365 6364 }
6366 6365 break;
6367 6366 case ITASK_CMD_POLL_LPORT:
6368 6367 if (!wait_queue)
6369 6368 task->task_lport->lport_task_poll(task);
6370 6369 break;
6371 6370 case ITASK_CMD_SEND_STATUS:
6372 6371 /* case ITASK_CMD_XFER_DATA: */
6373 6372 break;
6374 6373 }
6375 6374 mutex_enter(&w->worker_lock);
6376 6375 if (dec_qdepth) {
6377 6376 w->worker_queue_depth--;
6378 6377 }
6379 6378 }
6380 6379 if ((w->worker_flags & STMF_WORKER_TERMINATE) && (wait_timer == 0)) {
6381 6380 if (w->worker_ref_count == 0)
6382 6381 goto stmf_worker_loop;
6383 6382 else {
6384 6383 wait_timer = ddi_get_lbolt() + 1;
6385 6384 wait_delta = 1;
6386 6385 }
6387 6386 }
6388 6387 w->worker_flags &= ~STMF_WORKER_ACTIVE;
6389 6388 if (wait_timer) {
6390 6389 DTRACE_PROBE1(worker__timed__sleep, stmf_worker_t, w);
6391 6390 (void) cv_reltimedwait(&w->worker_cv, &w->worker_lock,
6392 6391 wait_delta, TR_CLOCK_TICK);
6393 6392 } else {
6394 6393 DTRACE_PROBE1(worker__sleep, stmf_worker_t, w);
6395 6394 cv_wait(&w->worker_cv, &w->worker_lock);
6396 6395 }
6397 6396 DTRACE_PROBE1(worker__wakeup, stmf_worker_t, w);
6398 6397 w->worker_flags |= STMF_WORKER_ACTIVE;
6399 6398 goto stmf_worker_loop;
6400 6399 }
6401 6400
6402 6401 void
6403 6402 stmf_worker_mgmt()
6404 6403 {
6405 6404 int i;
6406 6405 int workers_needed;
6407 6406 uint32_t qd;
6408 6407 clock_t tps, d = 0;
6409 6408 uint32_t cur_max_ntasks = 0;
6410 6409 stmf_worker_t *w;
6411 6410
6412 6411 /* Check if we are trying to increase the # of threads */
6413 6412 for (i = stmf_nworkers_cur; i < stmf_nworkers_needed; i++) {
6414 6413 if (stmf_workers[i].worker_flags & STMF_WORKER_STARTED) {
6415 6414 stmf_nworkers_cur++;
6416 6415 stmf_nworkers_accepting_cmds++;
6417 6416 } else {
6418 6417 /* Wait for transition to complete */
6419 6418 return;
6420 6419 }
6421 6420 }
6422 6421 /* Check if we are trying to decrease the # of workers */
6423 6422 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6424 6423 if ((stmf_workers[i].worker_flags & STMF_WORKER_STARTED) == 0) {
6425 6424 stmf_nworkers_cur--;
6426 6425 /*
6427 6426 * stmf_nworkers_accepting_cmds has already been
6428 6427 * updated by the request to reduce the # of workers.
6429 6428 */
6430 6429 } else {
6431 6430 /* Wait for transition to complete */
6432 6431 return;
6433 6432 }
6434 6433 }
6435 6434 /* Check if we are being asked to quit */
6436 6435 if (stmf_workers_state != STMF_WORKERS_ENABLED) {
6437 6436 if (stmf_nworkers_cur) {
6438 6437 workers_needed = 0;
6439 6438 goto worker_mgmt_trigger_change;
6440 6439 }
6441 6440 return;
6442 6441 }
6443 6442 /* Check if we are starting */
6444 6443 if (stmf_nworkers_cur < stmf_i_min_nworkers) {
6445 6444 workers_needed = stmf_i_min_nworkers;
6446 6445 goto worker_mgmt_trigger_change;
6447 6446 }
6448 6447
6449 6448 tps = drv_usectohz(1 * 1000 * 1000);
6450 6449 if ((stmf_wm_last != 0) &&
6451 6450 ((d = ddi_get_lbolt() - stmf_wm_last) > tps)) {
6452 6451 qd = 0;
6453 6452 for (i = 0; i < stmf_nworkers_accepting_cmds; i++) {
6454 6453 qd += stmf_workers[i].worker_max_qdepth_pu;
6455 6454 stmf_workers[i].worker_max_qdepth_pu = 0;
6456 6455 if (stmf_workers[i].worker_max_sys_qdepth_pu >
6457 6456 cur_max_ntasks) {
6458 6457 cur_max_ntasks =
6459 6458 stmf_workers[i].worker_max_sys_qdepth_pu;
6460 6459 }
6461 6460 stmf_workers[i].worker_max_sys_qdepth_pu = 0;
6462 6461 }
6463 6462 }
6464 6463 stmf_wm_last = ddi_get_lbolt();
6465 6464 if (d <= tps) {
6466 6465 /* still ramping up */
6467 6466 return;
6468 6467 }
6469 6468 /* max qdepth cannot be more than max tasks */
6470 6469 if (qd > cur_max_ntasks)
6471 6470 qd = cur_max_ntasks;
6472 6471
6473 6472 /* See if we have more workers */
6474 6473 if (qd < stmf_nworkers_accepting_cmds) {
6475 6474 /*
6476 6475 * Since we dont reduce the worker count right away, monitor
6477 6476 * the highest load during the scale_down_delay.
6478 6477 */
6479 6478 if (qd > stmf_worker_scale_down_qd)
6480 6479 stmf_worker_scale_down_qd = qd;
6481 6480 if (stmf_worker_scale_down_timer == 0) {
6482 6481 stmf_worker_scale_down_timer = ddi_get_lbolt() +
6483 6482 drv_usectohz(stmf_worker_scale_down_delay *
6484 6483 1000 * 1000);
6485 6484 return;
6486 6485 }
6487 6486 if (ddi_get_lbolt() < stmf_worker_scale_down_timer) {
6488 6487 return;
6489 6488 }
6490 6489 /* Its time to reduce the workers */
6491 6490 if (stmf_worker_scale_down_qd < stmf_i_min_nworkers)
6492 6491 stmf_worker_scale_down_qd = stmf_i_min_nworkers;
6493 6492 if (stmf_worker_scale_down_qd > stmf_i_max_nworkers)
6494 6493 stmf_worker_scale_down_qd = stmf_i_max_nworkers;
6495 6494 if (stmf_worker_scale_down_qd == stmf_nworkers_cur)
6496 6495 return;
6497 6496 workers_needed = stmf_worker_scale_down_qd;
6498 6497 stmf_worker_scale_down_qd = 0;
6499 6498 goto worker_mgmt_trigger_change;
6500 6499 }
6501 6500 stmf_worker_scale_down_qd = 0;
6502 6501 stmf_worker_scale_down_timer = 0;
6503 6502 if (qd > stmf_i_max_nworkers)
6504 6503 qd = stmf_i_max_nworkers;
6505 6504 if (qd < stmf_i_min_nworkers)
6506 6505 qd = stmf_i_min_nworkers;
6507 6506 if (qd == stmf_nworkers_cur)
6508 6507 return;
6509 6508 workers_needed = qd;
6510 6509 goto worker_mgmt_trigger_change;
6511 6510
6512 6511 /* NOTREACHED */
6513 6512 return;
6514 6513
6515 6514 worker_mgmt_trigger_change:
6516 6515 ASSERT(workers_needed != stmf_nworkers_cur);
6517 6516 if (workers_needed > stmf_nworkers_cur) {
6518 6517 stmf_nworkers_needed = workers_needed;
6519 6518 for (i = stmf_nworkers_cur; i < workers_needed; i++) {
6520 6519 w = &stmf_workers[i];
6521 6520 w->worker_tid = thread_create(NULL, 0, stmf_worker_task,
6522 6521 (void *)&stmf_workers[i], 0, &p0, TS_RUN,
6523 6522 minclsyspri);
6524 6523 }
6525 6524 return;
6526 6525 }
6527 6526 /* At this point we know that we are decreasing the # of workers */
6528 6527 stmf_nworkers_accepting_cmds = workers_needed;
6529 6528 stmf_nworkers_needed = workers_needed;
6530 6529 /* Signal the workers that its time to quit */
6531 6530 for (i = (stmf_nworkers_cur - 1); i >= stmf_nworkers_needed; i--) {
6532 6531 w = &stmf_workers[i];
6533 6532 ASSERT(w && (w->worker_flags & STMF_WORKER_STARTED));
6534 6533 mutex_enter(&w->worker_lock);
6535 6534 w->worker_flags |= STMF_WORKER_TERMINATE;
6536 6535 if ((w->worker_flags & STMF_WORKER_ACTIVE) == 0)
6537 6536 cv_signal(&w->worker_cv);
6538 6537 mutex_exit(&w->worker_lock);
6539 6538 }
6540 6539 }
6541 6540
6542 6541 /*
6543 6542 * Fills out a dbuf from stmf_xfer_data_t (contained in the db_lu_private).
6544 6543 * If all the data has been filled out, frees the xd and makes
6545 6544 * db_lu_private NULL.
6546 6545 */
6547 6546 void
6548 6547 stmf_xd_to_dbuf(stmf_data_buf_t *dbuf, int set_rel_off)
6549 6548 {
6550 6549 stmf_xfer_data_t *xd;
6551 6550 uint8_t *p;
6552 6551 int i;
6553 6552 uint32_t s;
6554 6553
6555 6554 xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6556 6555 dbuf->db_data_size = 0;
6557 6556 if (set_rel_off)
6558 6557 dbuf->db_relative_offset = xd->size_done;
6559 6558 for (i = 0; i < dbuf->db_sglist_length; i++) {
6560 6559 s = min(xd->size_left, dbuf->db_sglist[i].seg_length);
6561 6560 p = &xd->buf[xd->size_done];
6562 6561 bcopy(p, dbuf->db_sglist[i].seg_addr, s);
6563 6562 xd->size_left -= s;
6564 6563 xd->size_done += s;
6565 6564 dbuf->db_data_size += s;
6566 6565 if (xd->size_left == 0) {
6567 6566 kmem_free(xd, xd->alloc_size);
6568 6567 dbuf->db_lu_private = NULL;
6569 6568 return;
6570 6569 }
6571 6570 }
6572 6571 }
6573 6572
6574 6573 /* ARGSUSED */
6575 6574 stmf_status_t
6576 6575 stmf_dlun0_task_alloc(scsi_task_t *task)
6577 6576 {
6578 6577 return (STMF_SUCCESS);
6579 6578 }
6580 6579
6581 6580 void
6582 6581 stmf_dlun0_new_task(scsi_task_t *task, stmf_data_buf_t *dbuf)
6583 6582 {
6584 6583 uint8_t *cdbp = (uint8_t *)&task->task_cdb[0];
6585 6584 stmf_i_scsi_session_t *iss;
6586 6585 uint32_t sz, minsz;
6587 6586 uint8_t *p;
6588 6587 stmf_xfer_data_t *xd;
6589 6588 uint8_t inq_page_length = 31;
6590 6589
6591 6590 if (task->task_mgmt_function) {
6592 6591 stmf_scsilib_handle_task_mgmt(task);
6593 6592 return;
6594 6593 }
6595 6594
6596 6595 switch (cdbp[0]) {
6597 6596 case SCMD_INQUIRY:
6598 6597 /*
6599 6598 * Basic protocol checks. In addition, only reply to
6600 6599 * standard inquiry. Otherwise, the LU provider needs
6601 6600 * to respond.
6602 6601 */
6603 6602
6604 6603 if (cdbp[2] || (cdbp[1] & 1) || cdbp[5]) {
6605 6604 stmf_scsilib_send_status(task, STATUS_CHECK,
6606 6605 STMF_SAA_INVALID_FIELD_IN_CDB);
6607 6606 return;
6608 6607 }
6609 6608
6610 6609 task->task_cmd_xfer_length =
6611 6610 (((uint32_t)cdbp[3]) << 8) | cdbp[4];
6612 6611
6613 6612 if (task->task_additional_flags &
6614 6613 TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6615 6614 task->task_expected_xfer_length =
6616 6615 task->task_cmd_xfer_length;
6617 6616 }
6618 6617
6619 6618 sz = min(task->task_expected_xfer_length,
6620 6619 min(36, task->task_cmd_xfer_length));
6621 6620 minsz = 36;
6622 6621
6623 6622 if (sz == 0) {
6624 6623 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6625 6624 return;
6626 6625 }
6627 6626
6628 6627 if (dbuf && (dbuf->db_sglist[0].seg_length < 36)) {
6629 6628 /*
6630 6629 * Ignore any preallocated dbuf if the size is less
6631 6630 * than 36. It will be freed during the task_free.
6632 6631 */
6633 6632 dbuf = NULL;
6634 6633 }
6635 6634 if (dbuf == NULL)
6636 6635 dbuf = stmf_alloc_dbuf(task, minsz, &minsz, 0);
6637 6636 if ((dbuf == NULL) || (dbuf->db_sglist[0].seg_length < sz)) {
6638 6637 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6639 6638 STMF_ALLOC_FAILURE, NULL);
6640 6639 return;
6641 6640 }
6642 6641 dbuf->db_lu_private = NULL;
6643 6642
6644 6643 p = dbuf->db_sglist[0].seg_addr;
6645 6644
6646 6645 /*
6647 6646 * Standard inquiry handling only.
6648 6647 */
6649 6648
6650 6649 bzero(p, inq_page_length + 5);
6651 6650
6652 6651 p[0] = DPQ_SUPPORTED | DTYPE_UNKNOWN;
6653 6652 p[2] = 5;
6654 6653 p[3] = 0x12;
6655 6654 p[4] = inq_page_length;
6656 6655 p[6] = 0x80;
6657 6656
6658 6657 (void) strncpy((char *)p+8, "SUN ", 8);
6659 6658 (void) strncpy((char *)p+16, "COMSTAR ", 16);
6660 6659 (void) strncpy((char *)p+32, "1.0 ", 4);
6661 6660
6662 6661 dbuf->db_data_size = sz;
6663 6662 dbuf->db_relative_offset = 0;
6664 6663 dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6665 6664 (void) stmf_xfer_data(task, dbuf, 0);
6666 6665
6667 6666 return;
6668 6667
6669 6668 case SCMD_REPORT_LUNS:
6670 6669 task->task_cmd_xfer_length =
6671 6670 ((((uint32_t)task->task_cdb[6]) << 24) |
6672 6671 (((uint32_t)task->task_cdb[7]) << 16) |
6673 6672 (((uint32_t)task->task_cdb[8]) << 8) |
6674 6673 ((uint32_t)task->task_cdb[9]));
6675 6674
6676 6675 if (task->task_additional_flags &
6677 6676 TASK_AF_NO_EXPECTED_XFER_LENGTH) {
6678 6677 task->task_expected_xfer_length =
6679 6678 task->task_cmd_xfer_length;
6680 6679 }
6681 6680
6682 6681 sz = min(task->task_expected_xfer_length,
6683 6682 task->task_cmd_xfer_length);
6684 6683
6685 6684 if (sz < 16) {
6686 6685 stmf_scsilib_send_status(task, STATUS_CHECK,
6687 6686 STMF_SAA_INVALID_FIELD_IN_CDB);
6688 6687 return;
6689 6688 }
6690 6689
6691 6690 iss = (stmf_i_scsi_session_t *)
6692 6691 task->task_session->ss_stmf_private;
6693 6692 rw_enter(iss->iss_lockp, RW_WRITER);
6694 6693 xd = stmf_session_prepare_report_lun_data(iss->iss_sm);
6695 6694 rw_exit(iss->iss_lockp);
6696 6695
6697 6696 if (xd == NULL) {
6698 6697 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6699 6698 STMF_ALLOC_FAILURE, NULL);
6700 6699 return;
6701 6700 }
6702 6701
6703 6702 sz = min(sz, xd->size_left);
6704 6703 xd->size_left = sz;
6705 6704 minsz = min(512, sz);
6706 6705
6707 6706 if (dbuf == NULL)
6708 6707 dbuf = stmf_alloc_dbuf(task, sz, &minsz, 0);
6709 6708 if (dbuf == NULL) {
6710 6709 kmem_free(xd, xd->alloc_size);
6711 6710 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6712 6711 STMF_ALLOC_FAILURE, NULL);
6713 6712 return;
6714 6713 }
6715 6714 dbuf->db_lu_private = xd;
6716 6715 stmf_xd_to_dbuf(dbuf, 1);
6717 6716
6718 6717 atomic_and_32(&iss->iss_flags,
6719 6718 ~(ISS_LUN_INVENTORY_CHANGED | ISS_GOT_INITIAL_LUNS));
6720 6719 dbuf->db_flags = DB_DIRECTION_TO_RPORT;
6721 6720 (void) stmf_xfer_data(task, dbuf, 0);
6722 6721 return;
6723 6722 }
6724 6723
6725 6724 stmf_scsilib_send_status(task, STATUS_CHECK, STMF_SAA_INVALID_OPCODE);
6726 6725 }
6727 6726
6728 6727 void
6729 6728 stmf_dlun0_dbuf_done(scsi_task_t *task, stmf_data_buf_t *dbuf)
6730 6729 {
6731 6730 stmf_i_scsi_task_t *itask =
6732 6731 (stmf_i_scsi_task_t *)task->task_stmf_private;
6733 6732
6734 6733 if (dbuf->db_xfer_status != STMF_SUCCESS) {
6735 6734 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6736 6735 dbuf->db_xfer_status, NULL);
6737 6736 return;
6738 6737 }
6739 6738 task->task_nbytes_transferred += dbuf->db_data_size;
6740 6739 if (dbuf->db_lu_private) {
6741 6740 /* There is more */
6742 6741 stmf_xd_to_dbuf(dbuf, 1);
6743 6742 (void) stmf_xfer_data(task, dbuf, 0);
6744 6743 return;
6745 6744 }
6746 6745
6747 6746 stmf_free_dbuf(task, dbuf);
6748 6747 /*
6749 6748 * If this is a proxy task, it will need to be completed from the
6750 6749 * proxy port provider. This message lets pppt know that the xfer
6751 6750 * is complete. When we receive the status from pppt, we will
6752 6751 * then relay that status back to the lport.
6753 6752 */
6754 6753 if (itask->itask_flags & ITASK_PROXY_TASK) {
6755 6754 stmf_ic_msg_t *ic_xfer_done_msg = NULL;
6756 6755 stmf_status_t ic_ret = STMF_FAILURE;
6757 6756 uint64_t session_msg_id;
6758 6757 mutex_enter(&stmf_state.stmf_lock);
6759 6758 session_msg_id = stmf_proxy_msg_id++;
6760 6759 mutex_exit(&stmf_state.stmf_lock);
6761 6760 /* send xfer done status to pppt */
6762 6761 ic_xfer_done_msg = ic_scsi_data_xfer_done_msg_alloc(
6763 6762 itask->itask_proxy_msg_id,
6764 6763 task->task_session->ss_session_id,
6765 6764 STMF_SUCCESS, session_msg_id);
6766 6765 if (ic_xfer_done_msg) {
6767 6766 ic_ret = ic_tx_msg(ic_xfer_done_msg);
6768 6767 if (ic_ret != STMF_IC_MSG_SUCCESS) {
6769 6768 cmn_err(CE_WARN, "unable to xmit session msg");
6770 6769 }
6771 6770 }
6772 6771 /* task will be completed from pppt */
6773 6772 return;
6774 6773 }
6775 6774 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6776 6775 }
6777 6776
6778 6777 /* ARGSUSED */
6779 6778 void
6780 6779 stmf_dlun0_status_done(scsi_task_t *task)
6781 6780 {
6782 6781 }
6783 6782
6784 6783 /* ARGSUSED */
6785 6784 void
6786 6785 stmf_dlun0_task_free(scsi_task_t *task)
6787 6786 {
6788 6787 }
6789 6788
6790 6789 /* ARGSUSED */
6791 6790 stmf_status_t
6792 6791 stmf_dlun0_abort(struct stmf_lu *lu, int abort_cmd, void *arg, uint32_t flags)
6793 6792 {
6794 6793 scsi_task_t *task = (scsi_task_t *)arg;
6795 6794 stmf_i_scsi_task_t *itask =
6796 6795 (stmf_i_scsi_task_t *)task->task_stmf_private;
6797 6796 stmf_i_lu_t *ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
6798 6797 int i;
6799 6798 uint8_t map;
6800 6799
6801 6800 if ((task->task_mgmt_function) && (itask->itask_flags &
6802 6801 (ITASK_CAUSING_LU_RESET | ITASK_CAUSING_TARGET_RESET))) {
6803 6802 switch (task->task_mgmt_function) {
6804 6803 case TM_ABORT_TASK:
6805 6804 case TM_ABORT_TASK_SET:
6806 6805 case TM_CLEAR_TASK_SET:
6807 6806 case TM_LUN_RESET:
6808 6807 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6809 6808 break;
6810 6809 case TM_TARGET_RESET:
6811 6810 case TM_TARGET_COLD_RESET:
6812 6811 case TM_TARGET_WARM_RESET:
6813 6812 stmf_abort_target_reset(task);
6814 6813 break;
6815 6814 }
6816 6815 return (STMF_ABORT_SUCCESS);
6817 6816 }
6818 6817
6819 6818 /*
6820 6819 * OK so its not a task mgmt. Make sure we free any xd sitting
6821 6820 * inside any dbuf.
6822 6821 */
6823 6822 if ((map = itask->itask_allocated_buf_map) != 0) {
6824 6823 for (i = 0; i < 4; i++) {
6825 6824 if ((map & 1) &&
6826 6825 ((itask->itask_dbufs[i])->db_lu_private)) {
6827 6826 stmf_xfer_data_t *xd;
6828 6827 stmf_data_buf_t *dbuf;
6829 6828
6830 6829 dbuf = itask->itask_dbufs[i];
6831 6830 xd = (stmf_xfer_data_t *)dbuf->db_lu_private;
6832 6831 dbuf->db_lu_private = NULL;
6833 6832 kmem_free(xd, xd->alloc_size);
6834 6833 }
6835 6834 map >>= 1;
6836 6835 }
6837 6836 }
6838 6837 return (STMF_ABORT_SUCCESS);
6839 6838 }
6840 6839
6841 6840 void
6842 6841 stmf_dlun0_task_poll(struct scsi_task *task)
6843 6842 {
6844 6843 /* Right now we only do this for handling task management functions */
6845 6844 ASSERT(task->task_mgmt_function);
6846 6845
6847 6846 switch (task->task_mgmt_function) {
6848 6847 case TM_ABORT_TASK:
6849 6848 case TM_ABORT_TASK_SET:
6850 6849 case TM_CLEAR_TASK_SET:
6851 6850 case TM_LUN_RESET:
6852 6851 (void) stmf_lun_reset_poll(task->task_lu, task, 0);
6853 6852 return;
6854 6853 case TM_TARGET_RESET:
6855 6854 case TM_TARGET_COLD_RESET:
6856 6855 case TM_TARGET_WARM_RESET:
6857 6856 stmf_target_reset_poll(task);
6858 6857 return;
6859 6858 }
6860 6859 }
6861 6860
6862 6861 /* ARGSUSED */
6863 6862 void
6864 6863 stmf_dlun0_ctl(struct stmf_lu *lu, int cmd, void *arg)
6865 6864 {
6866 6865 /* This function will never be called */
6867 6866 cmn_err(CE_WARN, "stmf_dlun0_ctl called with cmd %x", cmd);
6868 6867 }
6869 6868
6870 6869 void
6871 6870 stmf_dlun_init()
6872 6871 {
6873 6872 stmf_i_lu_t *ilu;
6874 6873
6875 6874 dlun0 = stmf_alloc(STMF_STRUCT_STMF_LU, 0, 0);
6876 6875 dlun0->lu_task_alloc = stmf_dlun0_task_alloc;
6877 6876 dlun0->lu_new_task = stmf_dlun0_new_task;
6878 6877 dlun0->lu_dbuf_xfer_done = stmf_dlun0_dbuf_done;
6879 6878 dlun0->lu_send_status_done = stmf_dlun0_status_done;
6880 6879 dlun0->lu_task_free = stmf_dlun0_task_free;
6881 6880 dlun0->lu_abort = stmf_dlun0_abort;
6882 6881 dlun0->lu_task_poll = stmf_dlun0_task_poll;
6883 6882 dlun0->lu_ctl = stmf_dlun0_ctl;
6884 6883
6885 6884 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
6886 6885 ilu->ilu_cur_task_cntr = &ilu->ilu_task_cntr1;
6887 6886 }
6888 6887
6889 6888 stmf_status_t
6890 6889 stmf_dlun_fini()
6891 6890 {
6892 6891 stmf_i_lu_t *ilu;
6893 6892
6894 6893 ilu = (stmf_i_lu_t *)dlun0->lu_stmf_private;
6895 6894
6896 6895 ASSERT(ilu->ilu_ntasks == ilu->ilu_ntasks_free);
6897 6896 if (ilu->ilu_ntasks) {
6898 6897 stmf_i_scsi_task_t *itask, *nitask;
6899 6898
6900 6899 nitask = ilu->ilu_tasks;
6901 6900 do {
6902 6901 itask = nitask;
6903 6902 nitask = itask->itask_lu_next;
6904 6903 dlun0->lu_task_free(itask->itask_task);
6905 6904 stmf_free(itask->itask_task);
6906 6905 } while (nitask != NULL);
6907 6906
6908 6907 }
6909 6908 stmf_free(dlun0);
6910 6909 return (STMF_SUCCESS);
6911 6910 }
6912 6911
6913 6912 void
6914 6913 stmf_abort_target_reset(scsi_task_t *task)
6915 6914 {
6916 6915 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
6917 6916 task->task_session->ss_stmf_private;
6918 6917 stmf_lun_map_t *lm;
6919 6918 stmf_lun_map_ent_t *lm_ent;
6920 6919 stmf_i_lu_t *ilu;
6921 6920 int i;
6922 6921
6923 6922 rw_enter(iss->iss_lockp, RW_READER);
6924 6923 lm = iss->iss_sm;
6925 6924 for (i = 0; i < lm->lm_nentries; i++) {
6926 6925 if (lm->lm_plus[i] == NULL)
6927 6926 continue;
6928 6927 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
6929 6928 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
6930 6929 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
6931 6930 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6932 6931 }
6933 6932 }
6934 6933 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
6935 6934 rw_exit(iss->iss_lockp);
6936 6935 }
6937 6936
6938 6937 /*
6939 6938 * The return value is only used by function managing target reset.
6940 6939 */
6941 6940 stmf_status_t
6942 6941 stmf_lun_reset_poll(stmf_lu_t *lu, struct scsi_task *task, int target_reset)
6943 6942 {
6944 6943 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
6945 6944 int ntasks_pending;
6946 6945
6947 6946 ntasks_pending = ilu->ilu_ntasks - ilu->ilu_ntasks_free;
6948 6947 /*
6949 6948 * This function is also used during Target reset. The idea is that
6950 6949 * once all the commands are aborted, call the LU's reset entry
6951 6950 * point (abort entry point with a reset flag). But if this Task
6952 6951 * mgmt is running on this LU then all the tasks cannot be aborted.
6953 6952 * one task (this task) will still be running which is OK.
6954 6953 */
6955 6954 if ((ntasks_pending == 0) || ((task->task_lu == lu) &&
6956 6955 (ntasks_pending == 1))) {
6957 6956 stmf_status_t ret;
6958 6957
6959 6958 if ((task->task_mgmt_function == TM_LUN_RESET) ||
6960 6959 (task->task_mgmt_function == TM_TARGET_RESET) ||
6961 6960 (task->task_mgmt_function == TM_TARGET_WARM_RESET) ||
6962 6961 (task->task_mgmt_function == TM_TARGET_COLD_RESET)) {
6963 6962 ret = lu->lu_abort(lu, STMF_LU_RESET_STATE, task, 0);
6964 6963 } else {
6965 6964 ret = STMF_SUCCESS;
6966 6965 }
6967 6966 if (ret == STMF_SUCCESS) {
6968 6967 atomic_and_32(&ilu->ilu_flags, ~ILU_RESET_ACTIVE);
6969 6968 }
6970 6969 if (target_reset) {
6971 6970 return (ret);
6972 6971 }
6973 6972 if (ret == STMF_SUCCESS) {
6974 6973 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
6975 6974 return (ret);
6976 6975 }
6977 6976 if (ret != STMF_BUSY) {
6978 6977 stmf_abort(STMF_QUEUE_TASK_ABORT, task, ret, NULL);
6979 6978 return (ret);
6980 6979 }
6981 6980 }
6982 6981
6983 6982 if (target_reset) {
6984 6983 /* Tell target reset polling code that we are not done */
6985 6984 return (STMF_BUSY);
6986 6985 }
6987 6986
6988 6987 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
6989 6988 != STMF_SUCCESS) {
6990 6989 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
6991 6990 STMF_ALLOC_FAILURE, NULL);
6992 6991 return (STMF_SUCCESS);
6993 6992 }
6994 6993
6995 6994 return (STMF_SUCCESS);
6996 6995 }
6997 6996
6998 6997 void
6999 6998 stmf_target_reset_poll(struct scsi_task *task)
7000 6999 {
7001 7000 stmf_i_scsi_session_t *iss = (stmf_i_scsi_session_t *)
7002 7001 task->task_session->ss_stmf_private;
7003 7002 stmf_lun_map_t *lm;
7004 7003 stmf_lun_map_ent_t *lm_ent;
7005 7004 stmf_i_lu_t *ilu;
7006 7005 stmf_status_t ret;
7007 7006 int i;
7008 7007 int not_done = 0;
7009 7008
7010 7009 ASSERT(iss->iss_flags & ISS_RESET_ACTIVE);
7011 7010
7012 7011 rw_enter(iss->iss_lockp, RW_READER);
7013 7012 lm = iss->iss_sm;
7014 7013 for (i = 0; i < lm->lm_nentries; i++) {
7015 7014 if (lm->lm_plus[i] == NULL)
7016 7015 continue;
7017 7016 lm_ent = (stmf_lun_map_ent_t *)lm->lm_plus[i];
7018 7017 ilu = (stmf_i_lu_t *)lm_ent->ent_lu->lu_stmf_private;
7019 7018 if (ilu->ilu_flags & ILU_RESET_ACTIVE) {
7020 7019 rw_exit(iss->iss_lockp);
7021 7020 ret = stmf_lun_reset_poll(lm_ent->ent_lu, task, 1);
7022 7021 rw_enter(iss->iss_lockp, RW_READER);
7023 7022 if (ret == STMF_SUCCESS)
7024 7023 continue;
7025 7024 not_done = 1;
7026 7025 if (ret != STMF_BUSY) {
7027 7026 rw_exit(iss->iss_lockp);
7028 7027 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7029 7028 STMF_ABORTED, NULL);
7030 7029 return;
7031 7030 }
7032 7031 }
7033 7032 }
7034 7033 rw_exit(iss->iss_lockp);
7035 7034
7036 7035 if (not_done) {
7037 7036 if (stmf_task_poll_lu(task, ITASK_DEFAULT_POLL_TIMEOUT)
7038 7037 != STMF_SUCCESS) {
7039 7038 stmf_abort(STMF_QUEUE_TASK_ABORT, task,
7040 7039 STMF_ALLOC_FAILURE, NULL);
7041 7040 return;
7042 7041 }
7043 7042 return;
7044 7043 }
7045 7044
7046 7045 atomic_and_32(&iss->iss_flags, ~ISS_RESET_ACTIVE);
7047 7046
7048 7047 stmf_scsilib_send_status(task, STATUS_GOOD, 0);
7049 7048 }
7050 7049
7051 7050 stmf_status_t
7052 7051 stmf_lu_add_event(stmf_lu_t *lu, int eventid)
7053 7052 {
7054 7053 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7055 7054
7056 7055 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7057 7056 return (STMF_INVALID_ARG);
7058 7057 }
7059 7058
7060 7059 STMF_EVENT_ADD(ilu->ilu_event_hdl, eventid);
7061 7060 return (STMF_SUCCESS);
7062 7061 }
7063 7062
7064 7063 stmf_status_t
7065 7064 stmf_lu_remove_event(stmf_lu_t *lu, int eventid)
7066 7065 {
7067 7066 stmf_i_lu_t *ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7068 7067
7069 7068 if (eventid == STMF_EVENT_ALL) {
7070 7069 STMF_EVENT_CLEAR_ALL(ilu->ilu_event_hdl);
7071 7070 return (STMF_SUCCESS);
7072 7071 }
7073 7072
7074 7073 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7075 7074 return (STMF_INVALID_ARG);
7076 7075 }
7077 7076
7078 7077 STMF_EVENT_REMOVE(ilu->ilu_event_hdl, eventid);
7079 7078 return (STMF_SUCCESS);
7080 7079 }
7081 7080
7082 7081 stmf_status_t
7083 7082 stmf_lport_add_event(stmf_local_port_t *lport, int eventid)
7084 7083 {
7085 7084 stmf_i_local_port_t *ilport =
7086 7085 (stmf_i_local_port_t *)lport->lport_stmf_private;
7087 7086
7088 7087 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7089 7088 return (STMF_INVALID_ARG);
7090 7089 }
7091 7090
7092 7091 STMF_EVENT_ADD(ilport->ilport_event_hdl, eventid);
7093 7092 return (STMF_SUCCESS);
7094 7093 }
7095 7094
7096 7095 stmf_status_t
7097 7096 stmf_lport_remove_event(stmf_local_port_t *lport, int eventid)
7098 7097 {
7099 7098 stmf_i_local_port_t *ilport =
7100 7099 (stmf_i_local_port_t *)lport->lport_stmf_private;
7101 7100
7102 7101 if (eventid == STMF_EVENT_ALL) {
7103 7102 STMF_EVENT_CLEAR_ALL(ilport->ilport_event_hdl);
7104 7103 return (STMF_SUCCESS);
7105 7104 }
7106 7105
7107 7106 if ((eventid < 0) || (eventid >= STMF_MAX_NUM_EVENTS)) {
7108 7107 return (STMF_INVALID_ARG);
7109 7108 }
7110 7109
7111 7110 STMF_EVENT_REMOVE(ilport->ilport_event_hdl, eventid);
7112 7111 return (STMF_SUCCESS);
7113 7112 }
7114 7113
7115 7114 void
7116 7115 stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid, void *arg, uint32_t flags)
7117 7116 {
7118 7117 if (STMF_EVENT_ENABLED(ilu->ilu_event_hdl, eventid) &&
7119 7118 (ilu->ilu_lu->lu_event_handler != NULL)) {
7120 7119 ilu->ilu_lu->lu_event_handler(ilu->ilu_lu, eventid, arg, flags);
7121 7120 }
7122 7121 }
7123 7122
7124 7123 void
7125 7124 stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid, void *arg,
7126 7125 uint32_t flags)
7127 7126 {
7128 7127 if (STMF_EVENT_ENABLED(ilport->ilport_event_hdl, eventid) &&
7129 7128 (ilport->ilport_lport->lport_event_handler != NULL)) {
7130 7129 ilport->ilport_lport->lport_event_handler(
7131 7130 ilport->ilport_lport, eventid, arg, flags);
7132 7131 }
7133 7132 }
7134 7133
7135 7134 /*
7136 7135 * With the possibility of having multiple itl sessions pointing to the
7137 7136 * same itl_kstat_info, the ilu_kstat_lock mutex is used to synchronize
7138 7137 * the kstat update of the ilu_kstat_io, itl_kstat_taskq and itl_kstat_lu_xfer
7139 7138 * statistics.
7140 7139 */
7141 7140 void
7142 7141 stmf_itl_task_start(stmf_i_scsi_task_t *itask)
7143 7142 {
7144 7143 stmf_itl_data_t *itl = itask->itask_itl_datap;
7145 7144 scsi_task_t *task = itask->itask_task;
7146 7145 stmf_i_lu_t *ilu;
7147 7146
7148 7147 if (itl == NULL || task->task_lu == dlun0)
7149 7148 return;
7150 7149 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7151 7150 itask->itask_start_timestamp = gethrtime();
7152 7151 if (ilu->ilu_kstat_io != NULL) {
7153 7152 mutex_enter(ilu->ilu_kstat_io->ks_lock);
7154 7153 stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_enter);
7155 7154 mutex_exit(ilu->ilu_kstat_io->ks_lock);
7156 7155 }
7157 7156
7158 7157 stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_enter);
7159 7158 }
7160 7159
7161 7160 void
7162 7161 stmf_itl_lu_new_task(stmf_i_scsi_task_t *itask)
7163 7162 {
7164 7163 stmf_itl_data_t *itl = itask->itask_itl_datap;
7165 7164 scsi_task_t *task = itask->itask_task;
7166 7165 stmf_i_lu_t *ilu;
7167 7166
7168 7167 if (itl == NULL || task->task_lu == dlun0)
7169 7168 return;
7170 7169 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7171 7170 if (ilu->ilu_kstat_io != NULL) {
7172 7171 mutex_enter(ilu->ilu_kstat_io->ks_lock);
7173 7172 stmf_update_kstat_lu_q(itask->itask_task, kstat_waitq_to_runq);
7174 7173 mutex_exit(ilu->ilu_kstat_io->ks_lock);
7175 7174 }
7176 7175
7177 7176 stmf_update_kstat_lport_q(itask->itask_task, kstat_waitq_to_runq);
7178 7177 }
7179 7178
7180 7179 void
7181 7180 stmf_itl_task_done(stmf_i_scsi_task_t *itask)
7182 7181 {
7183 7182 stmf_itl_data_t *itl = itask->itask_itl_datap;
7184 7183 scsi_task_t *task = itask->itask_task;
7185 7184 stmf_i_lu_t *ilu;
7186 7185
7187 7186 itask->itask_done_timestamp = gethrtime();
7188 7187
7189 7188 if (itl == NULL || task->task_lu == dlun0)
7190 7189 return;
7191 7190 ilu = (stmf_i_lu_t *)task->task_lu->lu_stmf_private;
7192 7191
7193 7192 if (ilu->ilu_kstat_io == NULL)
7194 7193 return;
7195 7194
7196 7195 mutex_enter(ilu->ilu_kstat_io->ks_lock);
7197 7196
7198 7197 if (itask->itask_flags & ITASK_KSTAT_IN_RUNQ) {
7199 7198 stmf_update_kstat_lu_q(task, kstat_runq_exit);
7200 7199 mutex_exit(ilu->ilu_kstat_io->ks_lock);
7201 7200 stmf_update_kstat_lport_q(task, kstat_runq_exit);
7202 7201 } else {
7203 7202 stmf_update_kstat_lu_q(task, kstat_waitq_exit);
7204 7203 mutex_exit(ilu->ilu_kstat_io->ks_lock);
7205 7204 stmf_update_kstat_lport_q(task, kstat_waitq_exit);
7206 7205 }
7207 7206 }
7208 7207
7209 7208 static void
7210 7209 stmf_lport_xfer_start(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7211 7210 {
7212 7211 stmf_itl_data_t *itl = itask->itask_itl_datap;
7213 7212
7214 7213 if (itl == NULL)
7215 7214 return;
7216 7215
7217 7216 DTRACE_PROBE2(scsi__xfer__start, scsi_task_t *, itask->itask_task,
7218 7217 stmf_data_buf_t *, dbuf);
7219 7218
7220 7219 dbuf->db_xfer_start_timestamp = gethrtime();
7221 7220 }
7222 7221
7223 7222 static void
7224 7223 stmf_lport_xfer_done(stmf_i_scsi_task_t *itask, stmf_data_buf_t *dbuf)
7225 7224 {
7226 7225 stmf_itl_data_t *itl = itask->itask_itl_datap;
7227 7226 hrtime_t elapsed_time;
7228 7227 uint64_t xfer_size;
7229 7228
7230 7229 if (itl == NULL)
7231 7230 return;
7232 7231
7233 7232 xfer_size = (dbuf->db_xfer_status == STMF_SUCCESS) ?
7234 7233 dbuf->db_data_size : 0;
7235 7234
7236 7235 elapsed_time = gethrtime() - dbuf->db_xfer_start_timestamp;
7237 7236 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
7238 7237 atomic_add_64((uint64_t *)&itask->itask_lport_read_time,
7239 7238 elapsed_time);
7240 7239 atomic_add_64((uint64_t *)&itask->itask_read_xfer,
7241 7240 xfer_size);
7242 7241 } else {
7243 7242 atomic_add_64((uint64_t *)&itask->itask_lport_write_time,
7244 7243 elapsed_time);
7245 7244 atomic_add_64((uint64_t *)&itask->itask_write_xfer,
7246 7245 xfer_size);
7247 7246 }
7248 7247
7249 7248 DTRACE_PROBE3(scsi__xfer__end, scsi_task_t *, itask->itask_task,
7250 7249 stmf_data_buf_t *, dbuf, hrtime_t, elapsed_time);
7251 7250
7252 7251 dbuf->db_xfer_start_timestamp = 0;
7253 7252 }
7254 7253
7255 7254 void
7256 7255 stmf_svc_init()
7257 7256 {
7258 7257 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7259 7258 return;
7260 7259 stmf_state.stmf_svc_tailp = &stmf_state.stmf_svc_active;
7261 7260 stmf_state.stmf_svc_taskq = ddi_taskq_create(0, "STMF_SVC_TASKQ", 1,
7262 7261 TASKQ_DEFAULTPRI, 0);
7263 7262 (void) ddi_taskq_dispatch(stmf_state.stmf_svc_taskq,
7264 7263 stmf_svc, 0, DDI_SLEEP);
7265 7264 }
7266 7265
7267 7266 stmf_status_t
7268 7267 stmf_svc_fini()
7269 7268 {
7270 7269 uint32_t i;
7271 7270
7272 7271 mutex_enter(&stmf_state.stmf_lock);
7273 7272 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED) {
7274 7273 stmf_state.stmf_svc_flags |= STMF_SVC_TERMINATE;
7275 7274 cv_signal(&stmf_state.stmf_cv);
7276 7275 }
7277 7276 mutex_exit(&stmf_state.stmf_lock);
7278 7277
7279 7278 /* Wait for 5 seconds */
7280 7279 for (i = 0; i < 500; i++) {
7281 7280 if (stmf_state.stmf_svc_flags & STMF_SVC_STARTED)
7282 7281 delay(drv_usectohz(10000));
7283 7282 else
7284 7283 break;
7285 7284 }
7286 7285 if (i == 500)
7287 7286 return (STMF_BUSY);
7288 7287
7289 7288 ddi_taskq_destroy(stmf_state.stmf_svc_taskq);
7290 7289
7291 7290 return (STMF_SUCCESS);
7292 7291 }
7293 7292
7294 7293 struct stmf_svc_clocks {
7295 7294 clock_t drain_start, drain_next;
7296 7295 clock_t timing_start, timing_next;
7297 7296 clock_t worker_delay;
7298 7297 };
7299 7298
7300 7299 /* ARGSUSED */
7301 7300 void
7302 7301 stmf_svc(void *arg)
7303 7302 {
7304 7303 stmf_svc_req_t *req;
7305 7304 stmf_lu_t *lu;
7306 7305 stmf_i_lu_t *ilu;
7307 7306 stmf_local_port_t *lport;
7308 7307 struct stmf_svc_clocks clks = { 0 };
7309 7308
7310 7309 mutex_enter(&stmf_state.stmf_lock);
7311 7310 stmf_state.stmf_svc_flags |= STMF_SVC_STARTED | STMF_SVC_ACTIVE;
7312 7311
7313 7312 while (!(stmf_state.stmf_svc_flags & STMF_SVC_TERMINATE)) {
7314 7313 if (stmf_state.stmf_svc_active == NULL) {
7315 7314 stmf_svc_timeout(&clks);
7316 7315 continue;
7317 7316 }
7318 7317
7319 7318 /*
7320 7319 * Pop the front request from the active list. After this,
7321 7320 * the request will no longer be referenced by global state,
7322 7321 * so it should be safe to access it without holding the
7323 7322 * stmf state lock.
7324 7323 */
7325 7324 req = stmf_state.stmf_svc_active;
7326 7325 stmf_state.stmf_svc_active = req->svc_next;
7327 7326
7328 7327 if (stmf_state.stmf_svc_active == NULL)
7329 7328 stmf_state.stmf_svc_tailp = &stmf_state.stmf_svc_active;
7330 7329
7331 7330 switch (req->svc_cmd) {
7332 7331 case STMF_CMD_LPORT_ONLINE:
7333 7332 /* Fallthrough */
7334 7333 case STMF_CMD_LPORT_OFFLINE:
7335 7334 mutex_exit(&stmf_state.stmf_lock);
7336 7335 lport = (stmf_local_port_t *)req->svc_obj;
7337 7336 lport->lport_ctl(lport, req->svc_cmd, &req->svc_info);
7338 7337 break;
7339 7338 case STMF_CMD_LU_ONLINE:
7340 7339 mutex_exit(&stmf_state.stmf_lock);
7341 7340 lu = (stmf_lu_t *)req->svc_obj;
7342 7341 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7343 7342 break;
7344 7343 case STMF_CMD_LU_OFFLINE:
7345 7344 /* Remove all mappings of this LU */
7346 7345 stmf_session_lu_unmapall((stmf_lu_t *)req->svc_obj);
7347 7346 /* Kill all the pending I/Os for this LU */
7348 7347 mutex_exit(&stmf_state.stmf_lock);
7349 7348 stmf_task_lu_killall((stmf_lu_t *)req->svc_obj, NULL,
7350 7349 STMF_ABORTED);
7351 7350 lu = (stmf_lu_t *)req->svc_obj;
7352 7351 ilu = (stmf_i_lu_t *)lu->lu_stmf_private;
7353 7352 stmf_wait_ilu_tasks_finish(ilu);
7354 7353 lu->lu_ctl(lu, req->svc_cmd, &req->svc_info);
7355 7354 break;
7356 7355 default:
7357 7356 cmn_err(CE_PANIC, "stmf_svc: unknown cmd %d",
7358 7357 req->svc_cmd);
7359 7358 }
7360 7359
7361 7360 kmem_free(req, req->svc_req_alloc_size);
7362 7361 mutex_enter(&stmf_state.stmf_lock);
7363 7362 }
7364 7363
7365 7364 stmf_state.stmf_svc_flags &= ~(STMF_SVC_STARTED | STMF_SVC_ACTIVE);
7366 7365 mutex_exit(&stmf_state.stmf_lock);
7367 7366 }
7368 7367
7369 7368 static void
7370 7369 stmf_svc_timeout(struct stmf_svc_clocks *clks)
7371 7370 {
7372 7371 clock_t td;
7373 7372 stmf_i_local_port_t *ilport, *next_ilport;
7374 7373 stmf_i_scsi_session_t *iss;
7375 7374
7376 7375 ASSERT(mutex_owned(&stmf_state.stmf_lock));
7377 7376
7378 7377 td = drv_usectohz(20000);
7379 7378
7380 7379 /* Do timeouts */
7381 7380 if (stmf_state.stmf_nlus &&
7382 7381 ((!clks->timing_next) || (ddi_get_lbolt() >= clks->timing_next))) {
7383 7382 if (!stmf_state.stmf_svc_ilu_timing) {
7384 7383 /* we are starting a new round */
7385 7384 stmf_state.stmf_svc_ilu_timing =
7386 7385 stmf_state.stmf_ilulist;
7387 7386 clks->timing_start = ddi_get_lbolt();
7388 7387 }
7389 7388
7390 7389 stmf_check_ilu_timing();
7391 7390 if (!stmf_state.stmf_svc_ilu_timing) {
7392 7391 /* we finished a complete round */
7393 7392 clks->timing_next =
7394 7393 clks->timing_start + drv_usectohz(5*1000*1000);
7395 7394 } else {
7396 7395 /* we still have some ilu items to check */
7397 7396 clks->timing_next =
7398 7397 ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7399 7398 }
7400 7399
7401 7400 if (stmf_state.stmf_svc_active)
7402 7401 return;
7403 7402 }
7404 7403
7405 7404 /* Check if there are free tasks to clear */
7406 7405 if (stmf_state.stmf_nlus &&
7407 7406 ((!clks->drain_next) || (ddi_get_lbolt() >= clks->drain_next))) {
7408 7407 if (!stmf_state.stmf_svc_ilu_draining) {
7409 7408 /* we are starting a new round */
7410 7409 stmf_state.stmf_svc_ilu_draining =
7411 7410 stmf_state.stmf_ilulist;
7412 7411 clks->drain_start = ddi_get_lbolt();
7413 7412 }
7414 7413
7415 7414 stmf_check_freetask();
7416 7415 if (!stmf_state.stmf_svc_ilu_draining) {
7417 7416 /* we finished a complete round */
7418 7417 clks->drain_next =
7419 7418 clks->drain_start + drv_usectohz(10*1000*1000);
7420 7419 } else {
7421 7420 /* we still have some ilu items to check */
7422 7421 clks->drain_next =
7423 7422 ddi_get_lbolt() + drv_usectohz(1*1000*1000);
7424 7423 }
7425 7424
7426 7425 if (stmf_state.stmf_svc_active)
7427 7426 return;
7428 7427 }
7429 7428
7430 7429 /* Check if we need to run worker_mgmt */
7431 7430 if (ddi_get_lbolt() > clks->worker_delay) {
7432 7431 stmf_worker_mgmt();
7433 7432 clks->worker_delay = ddi_get_lbolt() +
7434 7433 stmf_worker_mgmt_delay;
7435 7434 }
7436 7435
7437 7436 /* Check if any active session got its 1st LUN */
7438 7437 if (stmf_state.stmf_process_initial_luns) {
7439 7438 int stmf_level = 0;
7440 7439 int port_level;
7441 7440
7442 7441 for (ilport = stmf_state.stmf_ilportlist; ilport;
7443 7442 ilport = next_ilport) {
7444 7443 int ilport_lock_held;
7445 7444 next_ilport = ilport->ilport_next;
7446 7445
7447 7446 if ((ilport->ilport_flags &
7448 7447 ILPORT_SS_GOT_INITIAL_LUNS) == 0)
7449 7448 continue;
7450 7449
7451 7450 port_level = 0;
7452 7451 rw_enter(&ilport->ilport_lock, RW_READER);
7453 7452 ilport_lock_held = 1;
7454 7453
7455 7454 for (iss = ilport->ilport_ss_list; iss;
7456 7455 iss = iss->iss_next) {
7457 7456 if ((iss->iss_flags &
7458 7457 ISS_GOT_INITIAL_LUNS) == 0)
7459 7458 continue;
7460 7459
7461 7460 port_level++;
7462 7461 stmf_level++;
7463 7462 atomic_and_32(&iss->iss_flags,
7464 7463 ~ISS_GOT_INITIAL_LUNS);
7465 7464 atomic_or_32(&iss->iss_flags,
7466 7465 ISS_EVENT_ACTIVE);
7467 7466 rw_exit(&ilport->ilport_lock);
7468 7467 ilport_lock_held = 0;
7469 7468 mutex_exit(&stmf_state.stmf_lock);
7470 7469 stmf_generate_lport_event(ilport,
7471 7470 LPORT_EVENT_INITIAL_LUN_MAPPED,
7472 7471 iss->iss_ss, 0);
7473 7472 atomic_and_32(&iss->iss_flags,
7474 7473 ~ISS_EVENT_ACTIVE);
7475 7474 mutex_enter(&stmf_state.stmf_lock);
7476 7475 /*
7477 7476 * scan all the ilports again as the
7478 7477 * ilport list might have changed.
7479 7478 */
7480 7479 next_ilport = stmf_state.stmf_ilportlist;
7481 7480 break;
7482 7481 }
7483 7482
7484 7483 if (port_level == 0)
7485 7484 atomic_and_32(&ilport->ilport_flags,
7486 7485 ~ILPORT_SS_GOT_INITIAL_LUNS);
7487 7486 /* drop the lock if we are holding it. */
7488 7487 if (ilport_lock_held == 1)
7489 7488 rw_exit(&ilport->ilport_lock);
7490 7489
7491 7490 /* Max 4 session at a time */
7492 7491 if (stmf_level >= 4)
7493 7492 break;
7494 7493 }
7495 7494
7496 7495 if (stmf_level == 0)
7497 7496 stmf_state.stmf_process_initial_luns = 0;
7498 7497 }
7499 7498
7500 7499 stmf_state.stmf_svc_flags &= ~STMF_SVC_ACTIVE;
7501 7500 (void) cv_reltimedwait(&stmf_state.stmf_cv,
7502 7501 &stmf_state.stmf_lock, td, TR_CLOCK_TICK);
7503 7502 stmf_state.stmf_svc_flags |= STMF_SVC_ACTIVE;
7504 7503 }
7505 7504
7506 7505 /*
7507 7506 * Waits for ongoing I/O tasks to finish on an LU in preparation for
7508 7507 * the LU's offlining. The LU should already be in an Offlining state
7509 7508 * (otherwise I/O to the LU might never end). There is an additional
7510 7509 * enforcement of this via a deadman timer check.
7511 7510 */
7512 7511 static void
7513 7512 stmf_wait_ilu_tasks_finish(stmf_i_lu_t *ilu)
7514 7513 {
7515 7514 clock_t start, now, deadline;
7516 7515
7517 7516 start = now = ddi_get_lbolt();
7518 7517 deadline = start + drv_usectohz(stmf_io_deadman * 1000000llu);
7519 7518 mutex_enter(&ilu->ilu_task_lock);
7520 7519 while (ilu->ilu_ntasks != ilu->ilu_ntasks_free) {
7521 7520 (void) cv_timedwait(&ilu->ilu_offline_pending_cv,
7522 7521 &ilu->ilu_task_lock, deadline);
7523 7522 now = ddi_get_lbolt();
7524 7523 if (now > deadline) {
7525 7524 if (stmf_io_deadman_enabled) {
7526 7525 cmn_err(CE_PANIC, "stmf_svc: I/O deadman hit "
7527 7526 "on STMF_CMD_LU_OFFLINE after %d seconds",
7528 7527 stmf_io_deadman);
7529 7528 } else {
7530 7529 /* keep on spinning */
7531 7530 deadline = now + drv_usectohz(stmf_io_deadman *
7532 7531 1000000llu);
7533 7532 }
7534 7533 }
7535 7534 }
7536 7535 mutex_exit(&ilu->ilu_task_lock);
7537 7536 DTRACE_PROBE1(deadman__timeout__wait, clock_t, now - start);
7538 7537 }
7539 7538
7540 7539 void
7541 7540 stmf_svc_queue(int cmd, void *obj, stmf_state_change_info_t *info)
7542 7541 {
7543 7542 stmf_svc_req_t *req;
7544 7543 int s;
7545 7544
7546 7545 ASSERT(!mutex_owned(&stmf_state.stmf_lock));
7547 7546 s = sizeof (stmf_svc_req_t);
7548 7547 if (info->st_additional_info) {
7549 7548 s += strlen(info->st_additional_info) + 1;
7550 7549 }
7551 7550 req = kmem_zalloc(s, KM_SLEEP);
7552 7551
7553 7552 req->svc_cmd = cmd;
7554 7553 req->svc_obj = obj;
7555 7554 req->svc_info.st_rflags = info->st_rflags;
7556 7555 if (info->st_additional_info) {
7557 7556 req->svc_info.st_additional_info = (char *)(GET_BYTE_OFFSET(req,
7558 7557 sizeof (stmf_svc_req_t)));
7559 7558 (void) strcpy(req->svc_info.st_additional_info,
7560 7559 info->st_additional_info);
7561 7560 }
7562 7561 req->svc_req_alloc_size = s;
7563 7562 req->svc_next = NULL;
7564 7563
7565 7564 mutex_enter(&stmf_state.stmf_lock);
7566 7565 *stmf_state.stmf_svc_tailp = req;
7567 7566 stmf_state.stmf_svc_tailp = &req->svc_next;
7568 7567 if ((stmf_state.stmf_svc_flags & STMF_SVC_ACTIVE) == 0) {
7569 7568 cv_signal(&stmf_state.stmf_cv);
7570 7569 }
7571 7570 mutex_exit(&stmf_state.stmf_lock);
7572 7571 }
7573 7572
7574 7573 static void
7575 7574 stmf_svc_kill_obj_requests(void *obj)
7576 7575 {
7577 7576 stmf_svc_req_t *prev_req = NULL;
7578 7577 stmf_svc_req_t *next_req;
7579 7578 stmf_svc_req_t *req;
7580 7579
7581 7580 ASSERT(mutex_owned(&stmf_state.stmf_lock));
7582 7581
7583 7582 for (req = stmf_state.stmf_svc_active; req != NULL; req = next_req) {
7584 7583 next_req = req->svc_next;
7585 7584
7586 7585 if (req->svc_obj == obj) {
7587 7586 if (prev_req != NULL)
7588 7587 prev_req->svc_next = next_req;
7589 7588 else
7590 7589 stmf_state.stmf_svc_active = next_req;
7591 7590
7592 7591 if (next_req == NULL)
7593 7592 stmf_state.stmf_svc_tailp = (prev_req != NULL) ?
7594 7593 &prev_req->svc_next :
7595 7594 &stmf_state.stmf_svc_active;
7596 7595
7597 7596 kmem_free(req, req->svc_req_alloc_size);
7598 7597 } else {
7599 7598 prev_req = req;
7600 7599 }
7601 7600 }
7602 7601 }
7603 7602
7604 7603 void
7605 7604 stmf_trace(caddr_t ident, const char *fmt, ...)
7606 7605 {
7607 7606 va_list args;
7608 7607 char tbuf[160];
7609 7608 int len;
7610 7609
7611 7610 if (!stmf_trace_on)
7612 7611 return;
7613 7612 len = snprintf(tbuf, 158, "%s:%07lu: ", ident ? ident : "",
7614 7613 ddi_get_lbolt());
7615 7614 va_start(args, fmt);
7616 7615 len += vsnprintf(tbuf + len, 158 - len, fmt, args);
7617 7616 va_end(args);
7618 7617
7619 7618 if (len > 158) {
7620 7619 len = 158;
7621 7620 }
7622 7621 tbuf[len++] = '\n';
7623 7622 tbuf[len] = 0;
7624 7623
7625 7624 mutex_enter(&trace_buf_lock);
7626 7625 bcopy(tbuf, &stmf_trace_buf[trace_buf_curndx], len+1);
7627 7626 trace_buf_curndx += len;
7628 7627 if (trace_buf_curndx > (trace_buf_size - 320))
7629 7628 trace_buf_curndx = 0;
7630 7629 mutex_exit(&trace_buf_lock);
7631 7630 }
7632 7631
7633 7632 void
7634 7633 stmf_trace_clear()
7635 7634 {
7636 7635 if (!stmf_trace_on)
7637 7636 return;
7638 7637 mutex_enter(&trace_buf_lock);
7639 7638 trace_buf_curndx = 0;
7640 7639 if (trace_buf_size > 0)
7641 7640 stmf_trace_buf[0] = 0;
7642 7641 mutex_exit(&trace_buf_lock);
7643 7642 }
7644 7643
7645 7644 static void
7646 7645 stmf_abort_task_offline(scsi_task_t *task, int offline_lu, char *info)
7647 7646 {
7648 7647 stmf_state_change_info_t change_info;
7649 7648 void *ctl_private;
7650 7649 uint32_t ctl_cmd;
7651 7650 int msg = 0;
7652 7651
7653 7652 stmf_trace("FROM STMF", "abort_task_offline called for %s: %s",
7654 7653 offline_lu ? "LU" : "LPORT", info ? info : "no additional info");
7655 7654 change_info.st_additional_info = info;
7656 7655 if (offline_lu) {
7657 7656 change_info.st_rflags = STMF_RFLAG_RESET |
7658 7657 STMF_RFLAG_LU_ABORT;
7659 7658 ctl_private = task->task_lu;
7660 7659 if (((stmf_i_lu_t *)
7661 7660 task->task_lu->lu_stmf_private)->ilu_state ==
7662 7661 STMF_STATE_ONLINE) {
7663 7662 msg = 1;
7664 7663 }
7665 7664 ctl_cmd = STMF_CMD_LU_OFFLINE;
7666 7665 } else {
7667 7666 change_info.st_rflags = STMF_RFLAG_RESET |
7668 7667 STMF_RFLAG_LPORT_ABORT;
7669 7668 ctl_private = task->task_lport;
7670 7669 if (((stmf_i_local_port_t *)
7671 7670 task->task_lport->lport_stmf_private)->ilport_state ==
7672 7671 STMF_STATE_ONLINE) {
7673 7672 msg = 1;
7674 7673 }
7675 7674 ctl_cmd = STMF_CMD_LPORT_OFFLINE;
7676 7675 }
7677 7676
7678 7677 if (msg) {
7679 7678 stmf_trace(0, "Calling stmf_ctl to offline %s : %s",
7680 7679 offline_lu ? "LU" : "LPORT", info ? info :
7681 7680 "<no additional info>");
7682 7681 }
7683 7682 (void) stmf_ctl(ctl_cmd, ctl_private, &change_info);
7684 7683 }
7685 7684
7686 7685 static char
7687 7686 stmf_ctoi(char c)
7688 7687 {
7689 7688 if ((c >= '0') && (c <= '9'))
7690 7689 c -= '0';
7691 7690 else if ((c >= 'A') && (c <= 'F'))
7692 7691 c = c - 'A' + 10;
7693 7692 else if ((c >= 'a') && (c <= 'f'))
7694 7693 c = c - 'a' + 10;
7695 7694 else
7696 7695 c = -1;
7697 7696 return (c);
7698 7697 }
7699 7698
7700 7699 /* Convert from Hex value in ASCII format to the equivalent bytes */
7701 7700 static boolean_t
7702 7701 stmf_base16_str_to_binary(char *c, int dplen, uint8_t *dp)
7703 7702 {
7704 7703 int ii;
7705 7704
7706 7705 for (ii = 0; ii < dplen; ii++) {
7707 7706 char nibble1, nibble2;
7708 7707 char enc_char = *c++;
7709 7708 nibble1 = stmf_ctoi(enc_char);
7710 7709
7711 7710 enc_char = *c++;
7712 7711 nibble2 = stmf_ctoi(enc_char);
7713 7712 if (nibble1 == -1 || nibble2 == -1)
7714 7713 return (B_FALSE);
7715 7714
7716 7715 dp[ii] = (nibble1 << 4) | nibble2;
7717 7716 }
7718 7717 return (B_TRUE);
7719 7718 }
7720 7719
7721 7720 boolean_t
7722 7721 stmf_scsilib_tptid_validate(scsi_transport_id_t *tptid, uint32_t total_sz,
7723 7722 uint16_t *tptid_sz)
7724 7723 {
7725 7724 uint16_t tpd_len = SCSI_TPTID_SIZE;
7726 7725
7727 7726 if (tptid_sz)
7728 7727 *tptid_sz = 0;
7729 7728 if (total_sz < sizeof (scsi_transport_id_t))
7730 7729 return (B_FALSE);
7731 7730
7732 7731 switch (tptid->protocol_id) {
7733 7732
7734 7733 case PROTOCOL_FIBRE_CHANNEL:
7735 7734 /* FC Transport ID validation checks. SPC3 rev23, Table 284 */
7736 7735 if (total_sz < tpd_len || tptid->format_code != 0)
7737 7736 return (B_FALSE);
7738 7737 break;
7739 7738
7740 7739 case PROTOCOL_iSCSI:
7741 7740 {
7742 7741 iscsi_transport_id_t *iscsiid;
7743 7742 uint16_t adn_len, name_len;
7744 7743
7745 7744 /* Check for valid format code, SPC3 rev 23 Table 288 */
7746 7745 if ((total_sz < tpd_len) ||
7747 7746 (tptid->format_code != 0 && tptid->format_code != 1))
7748 7747 return (B_FALSE);
7749 7748
7750 7749 iscsiid = (iscsi_transport_id_t *)tptid;
7751 7750 adn_len = READ_SCSI16(iscsiid->add_len, uint16_t);
7752 7751 tpd_len = sizeof (iscsi_transport_id_t) + adn_len - 1;
7753 7752
7754 7753 /*
7755 7754 * iSCSI Transport ID validation checks.
7756 7755 * As per SPC3 rev 23 Section 7.5.4.6 and Table 289 & Table 290
7757 7756 */
7758 7757 if (adn_len < 20 || (adn_len % 4 != 0))
7759 7758 return (B_FALSE);
7760 7759
7761 7760 name_len = strnlen(iscsiid->iscsi_name, adn_len);
7762 7761 if (name_len == 0 || name_len >= adn_len)
7763 7762 return (B_FALSE);
7764 7763
7765 7764 /* If the format_code is 1 check for ISID seperator */
7766 7765 if ((tptid->format_code == 1) && (strstr(iscsiid->iscsi_name,
7767 7766 SCSI_TPTID_ISCSI_ISID_SEPERATOR) == NULL))
7768 7767 return (B_FALSE);
7769 7768
7770 7769 }
7771 7770 break;
7772 7771
7773 7772 case PROTOCOL_SRP:
7774 7773 /* SRP Transport ID validation checks. SPC3 rev23, Table 287 */
7775 7774 if (total_sz < tpd_len || tptid->format_code != 0)
7776 7775 return (B_FALSE);
7777 7776 break;
7778 7777
7779 7778 case PROTOCOL_PARALLEL_SCSI:
7780 7779 case PROTOCOL_SSA:
7781 7780 case PROTOCOL_IEEE_1394:
7782 7781 case PROTOCOL_SAS:
7783 7782 case PROTOCOL_ADT:
7784 7783 case PROTOCOL_ATAPI:
7785 7784 default:
7786 7785 {
7787 7786 stmf_dflt_scsi_tptid_t *dflttpd;
7788 7787
7789 7788 tpd_len = sizeof (stmf_dflt_scsi_tptid_t);
7790 7789 if (total_sz < tpd_len)
7791 7790 return (B_FALSE);
7792 7791 dflttpd = (stmf_dflt_scsi_tptid_t *)tptid;
7793 7792 tpd_len = tpd_len + SCSI_READ16(&dflttpd->ident_len) - 1;
7794 7793 if (total_sz < tpd_len)
7795 7794 return (B_FALSE);
7796 7795 }
7797 7796 break;
7798 7797 }
7799 7798 if (tptid_sz)
7800 7799 *tptid_sz = tpd_len;
7801 7800 return (B_TRUE);
7802 7801 }
7803 7802
7804 7803 boolean_t
7805 7804 stmf_scsilib_tptid_compare(scsi_transport_id_t *tpd1,
7806 7805 scsi_transport_id_t *tpd2)
7807 7806 {
7808 7807 if ((tpd1->protocol_id != tpd2->protocol_id) ||
7809 7808 (tpd1->format_code != tpd2->format_code))
7810 7809 return (B_FALSE);
7811 7810
7812 7811 switch (tpd1->protocol_id) {
7813 7812
7814 7813 case PROTOCOL_iSCSI:
7815 7814 {
7816 7815 iscsi_transport_id_t *iscsitpd1, *iscsitpd2;
7817 7816 uint16_t len;
7818 7817
7819 7818 iscsitpd1 = (iscsi_transport_id_t *)tpd1;
7820 7819 iscsitpd2 = (iscsi_transport_id_t *)tpd2;
7821 7820 len = SCSI_READ16(&iscsitpd1->add_len);
7822 7821 if ((memcmp(iscsitpd1->add_len, iscsitpd2->add_len, 2) != 0) ||
7823 7822 (memcmp(iscsitpd1->iscsi_name, iscsitpd2->iscsi_name, len)
7824 7823 != 0))
7825 7824 return (B_FALSE);
7826 7825 }
7827 7826 break;
7828 7827
7829 7828 case PROTOCOL_SRP:
7830 7829 {
7831 7830 scsi_srp_transport_id_t *srptpd1, *srptpd2;
7832 7831
7833 7832 srptpd1 = (scsi_srp_transport_id_t *)tpd1;
7834 7833 srptpd2 = (scsi_srp_transport_id_t *)tpd2;
7835 7834 if (memcmp(srptpd1->srp_name, srptpd2->srp_name,
7836 7835 sizeof (srptpd1->srp_name)) != 0)
7837 7836 return (B_FALSE);
7838 7837 }
7839 7838 break;
7840 7839
7841 7840 case PROTOCOL_FIBRE_CHANNEL:
7842 7841 {
7843 7842 scsi_fc_transport_id_t *fctpd1, *fctpd2;
7844 7843
7845 7844 fctpd1 = (scsi_fc_transport_id_t *)tpd1;
7846 7845 fctpd2 = (scsi_fc_transport_id_t *)tpd2;
7847 7846 if (memcmp(fctpd1->port_name, fctpd2->port_name,
7848 7847 sizeof (fctpd1->port_name)) != 0)
7849 7848 return (B_FALSE);
7850 7849 }
7851 7850 break;
7852 7851
7853 7852 case PROTOCOL_PARALLEL_SCSI:
7854 7853 case PROTOCOL_SSA:
7855 7854 case PROTOCOL_IEEE_1394:
7856 7855 case PROTOCOL_SAS:
7857 7856 case PROTOCOL_ADT:
7858 7857 case PROTOCOL_ATAPI:
7859 7858 default:
7860 7859 {
7861 7860 stmf_dflt_scsi_tptid_t *dflt1, *dflt2;
7862 7861 uint16_t len;
7863 7862
7864 7863 dflt1 = (stmf_dflt_scsi_tptid_t *)tpd1;
7865 7864 dflt2 = (stmf_dflt_scsi_tptid_t *)tpd2;
7866 7865 len = SCSI_READ16(&dflt1->ident_len);
7867 7866 if ((memcmp(dflt1->ident_len, dflt2->ident_len, 2) != 0) ||
7868 7867 (memcmp(dflt1->ident, dflt2->ident, len) != 0))
7869 7868 return (B_FALSE);
7870 7869 }
7871 7870 break;
7872 7871 }
7873 7872 return (B_TRUE);
7874 7873 }
7875 7874
7876 7875 /*
7877 7876 * Changes devid_desc to corresponding TransportID format
7878 7877 * Returns :- pointer to stmf_remote_port_t
7879 7878 * Note :- Allocates continous memory for stmf_remote_port_t and TransportID,
7880 7879 * This memory need to be freed when this remote_port is no longer
7881 7880 * used.
7882 7881 */
7883 7882 stmf_remote_port_t *
7884 7883 stmf_scsilib_devid_to_remote_port(scsi_devid_desc_t *devid)
7885 7884 {
7886 7885 struct scsi_fc_transport_id *fc_tpd;
7887 7886 struct iscsi_transport_id *iscsi_tpd;
7888 7887 struct scsi_srp_transport_id *srp_tpd;
7889 7888 struct stmf_dflt_scsi_tptid *dflt_tpd;
7890 7889 uint16_t ident_len, sz = 0;
7891 7890 stmf_remote_port_t *rpt = NULL;
7892 7891
7893 7892 ident_len = devid->ident_length;
7894 7893 ASSERT(ident_len);
7895 7894 switch (devid->protocol_id) {
7896 7895 case PROTOCOL_FIBRE_CHANNEL:
7897 7896 sz = sizeof (scsi_fc_transport_id_t);
7898 7897 rpt = stmf_remote_port_alloc(sz);
7899 7898 rpt->rport_tptid->format_code = 0;
7900 7899 rpt->rport_tptid->protocol_id = devid->protocol_id;
7901 7900 fc_tpd = (scsi_fc_transport_id_t *)rpt->rport_tptid;
7902 7901 /*
7903 7902 * convert from "wwn.xxxxxxxxxxxxxxxx" to 8-byte binary
7904 7903 * skip first 4 byte for "wwn."
7905 7904 */
7906 7905 ASSERT(strncmp("wwn.", (char *)devid->ident, 4) == 0);
7907 7906 if ((ident_len < SCSI_TPTID_FC_PORT_NAME_SIZE * 2 + 4) ||
7908 7907 !stmf_base16_str_to_binary((char *)devid->ident + 4,
7909 7908 SCSI_TPTID_FC_PORT_NAME_SIZE, fc_tpd->port_name))
7910 7909 goto devid_to_remote_port_fail;
7911 7910 break;
7912 7911
7913 7912 case PROTOCOL_iSCSI:
7914 7913 sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (iscsi_transport_id_t) +
7915 7914 ident_len - 1);
7916 7915 rpt = stmf_remote_port_alloc(sz);
7917 7916 rpt->rport_tptid->format_code = 0;
7918 7917 rpt->rport_tptid->protocol_id = devid->protocol_id;
7919 7918 iscsi_tpd = (iscsi_transport_id_t *)rpt->rport_tptid;
7920 7919 SCSI_WRITE16(iscsi_tpd->add_len, ident_len);
7921 7920 (void) memcpy(iscsi_tpd->iscsi_name, devid->ident, ident_len);
7922 7921 break;
7923 7922
7924 7923 case PROTOCOL_SRP:
7925 7924 sz = sizeof (scsi_srp_transport_id_t);
7926 7925 rpt = stmf_remote_port_alloc(sz);
7927 7926 rpt->rport_tptid->format_code = 0;
7928 7927 rpt->rport_tptid->protocol_id = devid->protocol_id;
7929 7928 srp_tpd = (scsi_srp_transport_id_t *)rpt->rport_tptid;
7930 7929 /*
7931 7930 * convert from "eui.xxxxxxxxxxxxxxx" to 8-byte binary
7932 7931 * skip first 4 byte for "eui."
7933 7932 * Assume 8-byte initiator-extension part of srp_name is NOT
7934 7933 * stored in devid and hence will be set as zero
7935 7934 */
7936 7935 ASSERT(strncmp("eui.", (char *)devid->ident, 4) == 0);
7937 7936 if ((ident_len < (SCSI_TPTID_SRP_PORT_NAME_LEN - 8) * 2 + 4) ||
7938 7937 !stmf_base16_str_to_binary((char *)devid->ident+4,
7939 7938 SCSI_TPTID_SRP_PORT_NAME_LEN, srp_tpd->srp_name))
7940 7939 goto devid_to_remote_port_fail;
7941 7940 break;
7942 7941
7943 7942 case PROTOCOL_PARALLEL_SCSI:
7944 7943 case PROTOCOL_SSA:
7945 7944 case PROTOCOL_IEEE_1394:
7946 7945 case PROTOCOL_SAS:
7947 7946 case PROTOCOL_ADT:
7948 7947 case PROTOCOL_ATAPI:
7949 7948 default :
7950 7949 ident_len = devid->ident_length;
7951 7950 sz = ALIGNED_TO_8BYTE_BOUNDARY(sizeof (stmf_dflt_scsi_tptid_t) +
7952 7951 ident_len - 1);
7953 7952 rpt = stmf_remote_port_alloc(sz);
7954 7953 rpt->rport_tptid->format_code = 0;
7955 7954 rpt->rport_tptid->protocol_id = devid->protocol_id;
7956 7955 dflt_tpd = (stmf_dflt_scsi_tptid_t *)rpt->rport_tptid;
7957 7956 SCSI_WRITE16(dflt_tpd->ident_len, ident_len);
7958 7957 (void) memcpy(dflt_tpd->ident, devid->ident, ident_len);
7959 7958 break;
7960 7959 }
7961 7960 return (rpt);
7962 7961
7963 7962 devid_to_remote_port_fail:
7964 7963 stmf_remote_port_free(rpt);
7965 7964 return (NULL);
7966 7965
7967 7966 }
7968 7967
7969 7968 stmf_remote_port_t *
7970 7969 stmf_remote_port_alloc(uint16_t tptid_sz) {
7971 7970 stmf_remote_port_t *rpt;
7972 7971 rpt = (stmf_remote_port_t *)kmem_zalloc(
7973 7972 sizeof (stmf_remote_port_t) + tptid_sz, KM_SLEEP);
7974 7973 rpt->rport_tptid_sz = tptid_sz;
7975 7974 rpt->rport_tptid = (scsi_transport_id_t *)(rpt + 1);
7976 7975 return (rpt);
7977 7976 }
7978 7977
7979 7978 void
7980 7979 stmf_remote_port_free(stmf_remote_port_t *rpt)
7981 7980 {
7982 7981 /*
7983 7982 * Note: stmf_scsilib_devid_to_remote_port() function allocates
7984 7983 * remote port structures for all transports in the same way, So
7985 7984 * it is safe to deallocate it in a protocol independent manner.
7986 7985 * If any of the allocation method changes, corresponding changes
7987 7986 * need to be made here too.
7988 7987 */
7989 7988 kmem_free(rpt, sizeof (stmf_remote_port_t) + rpt->rport_tptid_sz);
7990 7989 }
↓ open down ↓ |
7692 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX