Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/comstar/port/iscsit/iscsit.c
+++ new/usr/src/uts/common/io/comstar/port/iscsit/iscsit.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 * Copyright 2014, 2015 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 #include <sys/cpuvar.h>
28 28 #include <sys/types.h>
29 29 #include <sys/conf.h>
30 30 #include <sys/stat.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/sysmacros.h>
36 36 #include <sys/socket.h>
37 37 #include <sys/strsubr.h>
38 38 #include <sys/nvpair.h>
39 39
40 40 #include <sys/stmf.h>
41 41 #include <sys/stmf_ioctl.h>
42 42 #include <sys/portif.h>
43 43 #include <sys/idm/idm.h>
44 44 #include <sys/idm/idm_conn_sm.h>
45 45
46 46 #include "iscsit_isns.h"
47 47 #include "iscsit.h"
48 48
49 49 #define ISCSIT_VERSION BUILD_DATE "-1.18dev"
50 50 #define ISCSIT_NAME_VERSION "COMSTAR ISCSIT v" ISCSIT_VERSION
51 51
52 52 /*
53 53 * DDI entry points.
54 54 */
55 55 static int iscsit_drv_attach(dev_info_t *, ddi_attach_cmd_t);
56 56 static int iscsit_drv_detach(dev_info_t *, ddi_detach_cmd_t);
57 57 static int iscsit_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
58 58 static int iscsit_drv_open(dev_t *, int, int, cred_t *);
59 59 static int iscsit_drv_close(dev_t, int, int, cred_t *);
60 60 static boolean_t iscsit_drv_busy(void);
61 61 static int iscsit_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
62 62
63 63 extern struct mod_ops mod_miscops;
64 64
65 65
66 66 static struct cb_ops iscsit_cb_ops = {
67 67 iscsit_drv_open, /* cb_open */
68 68 iscsit_drv_close, /* cb_close */
69 69 nodev, /* cb_strategy */
70 70 nodev, /* cb_print */
71 71 nodev, /* cb_dump */
72 72 nodev, /* cb_read */
73 73 nodev, /* cb_write */
74 74 iscsit_drv_ioctl, /* cb_ioctl */
75 75 nodev, /* cb_devmap */
76 76 nodev, /* cb_mmap */
77 77 nodev, /* cb_segmap */
78 78 nochpoll, /* cb_chpoll */
79 79 ddi_prop_op, /* cb_prop_op */
80 80 NULL, /* cb_streamtab */
81 81 D_MP, /* cb_flag */
82 82 CB_REV, /* cb_rev */
83 83 nodev, /* cb_aread */
84 84 nodev, /* cb_awrite */
85 85 };
86 86
87 87 static struct dev_ops iscsit_dev_ops = {
88 88 DEVO_REV, /* devo_rev */
89 89 0, /* devo_refcnt */
90 90 iscsit_drv_getinfo, /* devo_getinfo */
91 91 nulldev, /* devo_identify */
92 92 nulldev, /* devo_probe */
93 93 iscsit_drv_attach, /* devo_attach */
94 94 iscsit_drv_detach, /* devo_detach */
95 95 nodev, /* devo_reset */
96 96 &iscsit_cb_ops, /* devo_cb_ops */
97 97 NULL, /* devo_bus_ops */
98 98 NULL, /* devo_power */
99 99 ddi_quiesce_not_needed, /* quiesce */
↓ open down ↓ |
99 lines elided |
↑ open up ↑ |
100 100 };
101 101
102 102 static struct modldrv modldrv = {
103 103 &mod_driverops,
104 104 "iSCSI Target",
105 105 &iscsit_dev_ops,
106 106 };
107 107
108 108 static struct modlinkage modlinkage = {
109 109 MODREV_1,
110 - &modldrv,
111 - NULL,
110 + { &modldrv, NULL }
112 111 };
113 112
114 113
115 114 iscsit_global_t iscsit_global;
116 115
117 116 kmem_cache_t *iscsit_status_pdu_cache;
118 117
119 118 boolean_t iscsit_sm_logging = B_FALSE;
120 119
121 120 kmutex_t login_sm_session_mutex;
122 121
123 122 static idm_status_t iscsit_init(dev_info_t *dip);
124 123 static idm_status_t iscsit_enable_svc(iscsit_hostinfo_t *hostinfo);
125 124 static void iscsit_disable_svc(void);
126 125
127 126 static int
128 127 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu);
129 128
130 129 static void
131 130 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu);
132 131
133 132 static idm_pdu_t *
134 133 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn);
135 134
136 135 static void
137 136 iscsit_process_pdu_in_queue(iscsit_sess_t *ist);
138 137
139 138 static void
140 139 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist);
141 140
142 141 static void
143 142 iscsit_rxpdu_queue_monitor(void *arg);
144 143
145 144 static void
146 145 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu);
147 146
148 147 static void
149 148 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu);
150 149
151 150 static void
152 151 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
153 152
154 153 static void
155 154 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
156 155
157 156 static void
158 157 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
159 158
160 159 void
161 160 iscsit_pdu_op_text_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
162 161
163 162 static void
164 163 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
165 164
166 165 int iscsit_cmd_window();
167 166
168 167 static int
169 168 iscsit_sna_lt(uint32_t sn1, uint32_t sn2);
170 169
171 170 void
172 171 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
173 172
174 173 static void
175 174 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu);
176 175
177 176 static void
178 177 iscsit_deferred(void *rx_pdu_void);
179 178
180 179 static idm_status_t
181 180 iscsit_conn_accept(idm_conn_t *ic);
182 181
183 182 static idm_status_t
184 183 iscsit_ffp_enabled(idm_conn_t *ic);
185 184
186 185 static idm_status_t
187 186 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class);
188 187
189 188 static idm_status_t
190 189 iscsit_conn_lost(idm_conn_t *ic);
191 190
192 191 static idm_status_t
193 192 iscsit_conn_destroy(idm_conn_t *ic);
194 193
195 194 static stmf_data_buf_t *
196 195 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
197 196 uint32_t flags);
198 197
199 198 static void
200 199 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf);
201 200
202 201 static void
203 202 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status);
204 203
205 204 static void
206 205 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status);
207 206
208 207 static void
209 208 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status);
210 209
211 210 static stmf_status_t
212 211 iscsit_idm_to_stmf(idm_status_t idmrc);
213 212
214 213 static iscsit_task_t *
215 214 iscsit_task_alloc(iscsit_conn_t *ict);
216 215
217 216 static void
218 217 iscsit_task_free(iscsit_task_t *itask);
219 218
220 219 static iscsit_task_t *
221 220 iscsit_tm_task_alloc(iscsit_conn_t *ict);
222 221
223 222 static void
224 223 iscsit_tm_task_free(iscsit_task_t *itask);
225 224
226 225 static idm_status_t
227 226 iscsit_task_start(iscsit_task_t *itask);
228 227
229 228 static void
230 229 iscsit_task_done(iscsit_task_t *itask);
231 230
232 231 static int
233 232 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags);
234 233
235 234 static void
236 235 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags);
237 236
238 237 static it_cfg_status_t
239 238 iscsit_config_merge(it_config_t *cfg);
240 239
241 240 static idm_status_t
242 241 iscsit_login_fail(idm_conn_t *ic);
243 242
244 243 static boolean_t iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn);
245 244 static void iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
246 245 uint8_t response, uint8_t cmd_status);
247 246 static void iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu,
248 247 uint8_t tm_status);
249 248
250 249 /*
251 250 * MC/S: Out-of-order commands are staged on a session-wide wait
252 251 * queue until a system-tunable threshold is reached. A separate
253 252 * thread is used to scan the staging queue on all the session,
254 253 * If a delayed PDU does not arrive within a timeout, the target
255 254 * will advance to the staged PDU that is next in sequence, skipping
256 255 * over the missing PDU(s) to go past a hole in the sequence.
257 256 */
258 257 volatile int rxpdu_queue_threshold = ISCSIT_RXPDU_QUEUE_THRESHOLD;
259 258
260 259 static kmutex_t iscsit_rxpdu_queue_monitor_mutex;
261 260 kthread_t *iscsit_rxpdu_queue_monitor_thr_id;
262 261 static kt_did_t iscsit_rxpdu_queue_monitor_thr_did;
263 262 static boolean_t iscsit_rxpdu_queue_monitor_thr_running;
264 263 static kcondvar_t iscsit_rxpdu_queue_monitor_cv;
265 264
266 265 int
267 266 _init(void)
268 267 {
269 268 int rc;
270 269
271 270 rw_init(&iscsit_global.global_rwlock, NULL, RW_DRIVER, NULL);
272 271 mutex_init(&iscsit_global.global_state_mutex, NULL,
273 272 MUTEX_DRIVER, NULL);
274 273 iscsit_global.global_svc_state = ISE_DETACHED;
275 274
276 275 mutex_init(&iscsit_rxpdu_queue_monitor_mutex, NULL,
277 276 MUTEX_DRIVER, NULL);
278 277 mutex_init(&login_sm_session_mutex, NULL, MUTEX_DRIVER, NULL);
279 278 iscsit_rxpdu_queue_monitor_thr_id = NULL;
280 279 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
281 280 cv_init(&iscsit_rxpdu_queue_monitor_cv, NULL, CV_DEFAULT, NULL);
282 281
283 282 if ((rc = mod_install(&modlinkage)) != 0) {
284 283 mutex_destroy(&iscsit_global.global_state_mutex);
285 284 rw_destroy(&iscsit_global.global_rwlock);
286 285 return (rc);
287 286 }
288 287
289 288 return (rc);
290 289 }
291 290
292 291 int
293 292 _info(struct modinfo *modinfop)
294 293 {
295 294 return (mod_info(&modlinkage, modinfop));
296 295 }
297 296
298 297 int
299 298 _fini(void)
300 299 {
301 300 int rc;
302 301
303 302 rc = mod_remove(&modlinkage);
304 303
305 304 if (rc == 0) {
306 305 mutex_destroy(&iscsit_rxpdu_queue_monitor_mutex);
307 306 mutex_destroy(&login_sm_session_mutex);
308 307 cv_destroy(&iscsit_rxpdu_queue_monitor_cv);
309 308 mutex_destroy(&iscsit_global.global_state_mutex);
310 309 rw_destroy(&iscsit_global.global_rwlock);
311 310 }
312 311
313 312 return (rc);
314 313 }
315 314
316 315 /*
317 316 * DDI entry points.
318 317 */
319 318
320 319 /* ARGSUSED */
321 320 static int
322 321 iscsit_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
323 322 void **result)
324 323 {
325 324 ulong_t instance = getminor((dev_t)arg);
326 325
327 326 switch (cmd) {
328 327 case DDI_INFO_DEVT2DEVINFO:
329 328 *result = iscsit_global.global_dip;
330 329 return (DDI_SUCCESS);
331 330
332 331 case DDI_INFO_DEVT2INSTANCE:
333 332 *result = (void *)instance;
334 333 return (DDI_SUCCESS);
335 334
336 335 default:
337 336 break;
338 337 }
339 338
340 339 return (DDI_FAILURE);
341 340 }
342 341
343 342 static int
344 343 iscsit_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
345 344 {
346 345 if (cmd != DDI_ATTACH) {
347 346 return (DDI_FAILURE);
348 347 }
349 348
350 349 if (ddi_get_instance(dip) != 0) {
351 350 /* we only allow instance 0 to attach */
352 351 return (DDI_FAILURE);
353 352 }
354 353
355 354 /* create the minor node */
356 355 if (ddi_create_minor_node(dip, ISCSIT_MODNAME, S_IFCHR, 0,
357 356 DDI_PSEUDO, 0) != DDI_SUCCESS) {
358 357 cmn_err(CE_WARN, "iscsit_drv_attach: "
359 358 "failed creating minor node");
360 359 return (DDI_FAILURE);
361 360 }
362 361
363 362 if (iscsit_init(dip) != IDM_STATUS_SUCCESS) {
364 363 cmn_err(CE_WARN, "iscsit_drv_attach: "
365 364 "failed to initialize");
366 365 ddi_remove_minor_node(dip, NULL);
367 366 return (DDI_FAILURE);
368 367 }
369 368
370 369 iscsit_global.global_svc_state = ISE_DISABLED;
371 370 iscsit_global.global_dip = dip;
372 371
373 372 return (DDI_SUCCESS);
374 373 }
375 374
376 375 /*ARGSUSED*/
377 376 static int
378 377 iscsit_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
379 378 {
380 379 if (cmd != DDI_DETACH)
381 380 return (DDI_FAILURE);
382 381
383 382 /*
384 383 * drv_detach is called in a context that owns the
385 384 * device node for the /dev/pseudo device. If this thread blocks
386 385 * for any resource, other threads that need the /dev/pseudo device
387 386 * may end up in a deadlock with this thread.Hence, we use a
388 387 * separate lock just for the structures that drv_detach needs
389 388 * to access.
390 389 */
391 390 mutex_enter(&iscsit_global.global_state_mutex);
392 391 if (iscsit_drv_busy()) {
393 392 mutex_exit(&iscsit_global.global_state_mutex);
394 393 return (EBUSY);
395 394 }
396 395
397 396 iscsit_global.global_dip = NULL;
398 397 ddi_remove_minor_node(dip, NULL);
399 398
400 399 ldi_ident_release(iscsit_global.global_li);
401 400 iscsit_global.global_svc_state = ISE_DETACHED;
402 401
403 402 mutex_exit(&iscsit_global.global_state_mutex);
404 403
405 404 return (DDI_SUCCESS);
406 405 }
407 406
408 407 /*ARGSUSED*/
409 408 static int
410 409 iscsit_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
411 410 {
412 411 return (0);
413 412 }
414 413
415 414 /* ARGSUSED */
416 415 static int
417 416 iscsit_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
418 417 {
419 418 return (0);
420 419 }
421 420
422 421 static boolean_t
423 422 iscsit_drv_busy(void)
424 423 {
425 424 ASSERT(MUTEX_HELD(&iscsit_global.global_state_mutex));
426 425
427 426 switch (iscsit_global.global_svc_state) {
428 427 case ISE_DISABLED:
429 428 case ISE_DETACHED:
430 429 return (B_FALSE);
431 430 default:
432 431 return (B_TRUE);
433 432 }
434 433 /* NOTREACHED */
435 434 }
436 435
437 436 /* ARGSUSED */
438 437 static int
439 438 iscsit_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flag, cred_t *cred,
440 439 int *retval)
441 440 {
442 441 iscsit_ioc_set_config_t setcfg;
443 442 iscsit_ioc_set_config32_t setcfg32;
444 443 char *cfg_pnvlist = NULL;
445 444 nvlist_t *cfg_nvlist = NULL;
446 445 it_config_t *cfg = NULL;
447 446 idm_status_t idmrc;
448 447 int rc = 0;
449 448
450 449 if (drv_priv(cred) != 0) {
451 450 return (EPERM);
452 451 }
453 452
454 453 mutex_enter(&iscsit_global.global_state_mutex);
455 454
456 455 /*
457 456 * Validate ioctl requests against global service state
458 457 */
459 458 switch (iscsit_global.global_svc_state) {
460 459 case ISE_ENABLED:
461 460 if (cmd == ISCSIT_IOC_DISABLE_SVC) {
462 461 iscsit_global.global_svc_state = ISE_DISABLING;
463 462 } else if (cmd == ISCSIT_IOC_ENABLE_SVC) {
464 463 /* Already enabled */
465 464 mutex_exit(&iscsit_global.global_state_mutex);
466 465 return (0);
467 466 } else {
468 467 iscsit_global.global_svc_state = ISE_BUSY;
469 468 }
470 469 break;
471 470 case ISE_DISABLED:
472 471 if (cmd == ISCSIT_IOC_ENABLE_SVC) {
473 472 iscsit_global.global_svc_state = ISE_ENABLING;
474 473 } else if (cmd == ISCSIT_IOC_DISABLE_SVC) {
475 474 /* Already disabled */
476 475 mutex_exit(&iscsit_global.global_state_mutex);
477 476 return (0);
478 477 } else {
479 478 rc = EFAULT;
480 479 }
481 480 break;
482 481 case ISE_BUSY:
483 482 case ISE_ENABLING:
484 483 case ISE_DISABLING:
485 484 rc = EAGAIN;
486 485 break;
487 486 case ISE_DETACHED:
488 487 default:
489 488 rc = EFAULT;
490 489 break;
491 490 }
492 491
493 492 mutex_exit(&iscsit_global.global_state_mutex);
494 493 if (rc != 0)
495 494 return (rc);
496 495
497 496 /* Handle ioctl request (enable/disable have already been handled) */
498 497 switch (cmd) {
499 498 case ISCSIT_IOC_SET_CONFIG:
500 499 /* Any errors must set state back to ISE_ENABLED */
501 500 switch (ddi_model_convert_from(flag & FMODELS)) {
502 501 case DDI_MODEL_ILP32:
503 502 if (ddi_copyin((void *)argp, &setcfg32,
504 503 sizeof (iscsit_ioc_set_config32_t), flag) != 0) {
505 504 rc = EFAULT;
506 505 goto cleanup;
507 506 }
508 507
509 508 setcfg.set_cfg_pnvlist =
510 509 (char *)((uintptr_t)setcfg32.set_cfg_pnvlist);
511 510 setcfg.set_cfg_vers = setcfg32.set_cfg_vers;
512 511 setcfg.set_cfg_pnvlist_len =
513 512 setcfg32.set_cfg_pnvlist_len;
514 513 break;
515 514 case DDI_MODEL_NONE:
516 515 if (ddi_copyin((void *)argp, &setcfg,
517 516 sizeof (iscsit_ioc_set_config_t), flag) != 0) {
518 517 rc = EFAULT;
519 518 goto cleanup;
520 519 }
521 520 break;
522 521 default:
523 522 rc = EFAULT;
524 523 goto cleanup;
525 524 }
526 525
527 526 /* Check API version */
528 527 if (setcfg.set_cfg_vers != ISCSIT_API_VERS0) {
529 528 rc = EINVAL;
530 529 goto cleanup;
531 530 }
532 531
533 532 /* Config is in packed nvlist format so unpack it */
534 533 cfg_pnvlist = kmem_alloc(setcfg.set_cfg_pnvlist_len,
535 534 KM_SLEEP);
536 535 ASSERT(cfg_pnvlist != NULL);
537 536
538 537 if (ddi_copyin(setcfg.set_cfg_pnvlist, cfg_pnvlist,
539 538 setcfg.set_cfg_pnvlist_len, flag) != 0) {
540 539 rc = EFAULT;
541 540 goto cleanup;
542 541 }
543 542
544 543 rc = nvlist_unpack(cfg_pnvlist, setcfg.set_cfg_pnvlist_len,
545 544 &cfg_nvlist, KM_SLEEP);
546 545 if (rc != 0) {
547 546 goto cleanup;
548 547 }
549 548
550 549 /* Translate nvlist */
551 550 rc = it_nv_to_config(cfg_nvlist, &cfg);
552 551 if (rc != 0) {
553 552 cmn_err(CE_WARN, "Configuration is invalid");
554 553 goto cleanup;
555 554 }
556 555
557 556 /* Update config */
558 557 rc = iscsit_config_merge(cfg);
559 558 /* FALLTHROUGH */
560 559
561 560 cleanup:
562 561 if (cfg)
563 562 it_config_free_cmn(cfg);
564 563 if (cfg_pnvlist)
565 564 kmem_free(cfg_pnvlist, setcfg.set_cfg_pnvlist_len);
566 565 nvlist_free(cfg_nvlist);
567 566
568 567 /*
569 568 * Now that the reconfig is complete set our state back to
570 569 * enabled.
571 570 */
572 571 mutex_enter(&iscsit_global.global_state_mutex);
573 572 iscsit_global.global_svc_state = ISE_ENABLED;
574 573 mutex_exit(&iscsit_global.global_state_mutex);
575 574 break;
576 575 case ISCSIT_IOC_ENABLE_SVC: {
577 576 iscsit_hostinfo_t hostinfo;
578 577
579 578 if (ddi_copyin((void *)argp, &hostinfo.length,
580 579 sizeof (hostinfo.length), flag) != 0) {
581 580 mutex_enter(&iscsit_global.global_state_mutex);
582 581 iscsit_global.global_svc_state = ISE_DISABLED;
583 582 mutex_exit(&iscsit_global.global_state_mutex);
584 583 return (EFAULT);
585 584 }
586 585
587 586 if (hostinfo.length > sizeof (hostinfo.fqhn))
588 587 hostinfo.length = sizeof (hostinfo.fqhn);
589 588
590 589 if (ddi_copyin((void *)((caddr_t)argp +
591 590 sizeof (hostinfo.length)), &hostinfo.fqhn,
592 591 hostinfo.length, flag) != 0) {
593 592 mutex_enter(&iscsit_global.global_state_mutex);
594 593 iscsit_global.global_svc_state = ISE_DISABLED;
595 594 mutex_exit(&iscsit_global.global_state_mutex);
596 595 return (EFAULT);
597 596 }
598 597
599 598 idmrc = iscsit_enable_svc(&hostinfo);
600 599 mutex_enter(&iscsit_global.global_state_mutex);
601 600 if (idmrc == IDM_STATUS_SUCCESS) {
602 601 iscsit_global.global_svc_state = ISE_ENABLED;
603 602 } else {
604 603 rc = EIO;
605 604 iscsit_global.global_svc_state = ISE_DISABLED;
606 605 }
607 606 mutex_exit(&iscsit_global.global_state_mutex);
608 607 break;
609 608 }
610 609 case ISCSIT_IOC_DISABLE_SVC:
611 610 iscsit_disable_svc();
612 611 mutex_enter(&iscsit_global.global_state_mutex);
613 612 iscsit_global.global_svc_state = ISE_DISABLED;
614 613 mutex_exit(&iscsit_global.global_state_mutex);
615 614 break;
616 615
617 616 default:
618 617 rc = EINVAL;
619 618 mutex_enter(&iscsit_global.global_state_mutex);
620 619 iscsit_global.global_svc_state = ISE_ENABLED;
621 620 mutex_exit(&iscsit_global.global_state_mutex);
622 621 }
623 622
624 623 return (rc);
625 624 }
626 625
627 626 static idm_status_t
628 627 iscsit_init(dev_info_t *dip)
629 628 {
630 629 int rc;
631 630
632 631 rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
633 632 ASSERT(rc == 0); /* Failure indicates invalid argument */
634 633
635 634 iscsit_global.global_svc_state = ISE_DISABLED;
636 635
637 636 return (IDM_STATUS_SUCCESS);
638 637 }
639 638
640 639 /*
641 640 * iscsit_enable_svc
642 641 *
643 642 * registers all the configured targets and target portals with STMF
644 643 */
645 644 static idm_status_t
646 645 iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
647 646 {
648 647 stmf_port_provider_t *pp;
649 648 stmf_dbuf_store_t *dbuf_store;
650 649 boolean_t did_iscsit_isns_init;
651 650 idm_status_t retval = IDM_STATUS_SUCCESS;
652 651
653 652 ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
654 653
655 654 /*
656 655 * Make sure that can tell if we have partially allocated
657 656 * in case we need to exit and tear down anything allocated.
658 657 */
659 658 iscsit_global.global_tsih_pool = NULL;
660 659 iscsit_global.global_dbuf_store = NULL;
661 660 iscsit_status_pdu_cache = NULL;
662 661 pp = NULL;
663 662 iscsit_global.global_pp = NULL;
664 663 iscsit_global.global_default_tpg = NULL;
665 664 did_iscsit_isns_init = B_FALSE;
666 665 iscsit_global.global_dispatch_taskq = NULL;
667 666
668 667 /* Setup remaining fields in iscsit_global_t */
669 668 idm_refcnt_init(&iscsit_global.global_refcnt,
670 669 &iscsit_global);
671 670
672 671 avl_create(&iscsit_global.global_discovery_sessions,
673 672 iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
674 673 offsetof(iscsit_sess_t, ist_tgt_ln));
675 674
676 675 avl_create(&iscsit_global.global_target_list,
677 676 iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
678 677 offsetof(iscsit_tgt_t, target_global_ln));
679 678
680 679 list_create(&iscsit_global.global_deleted_target_list,
681 680 sizeof (iscsit_tgt_t),
682 681 offsetof(iscsit_tgt_t, target_global_deleted_ln));
683 682
684 683 avl_create(&iscsit_global.global_tpg_list,
685 684 iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
686 685 offsetof(iscsit_tpg_t, tpg_global_ln));
687 686
688 687 avl_create(&iscsit_global.global_ini_list,
689 688 iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
690 689 offsetof(iscsit_ini_t, ini_global_ln));
691 690
692 691 iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
693 692 (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
694 693 VM_SLEEP | VMC_IDENTIFIER);
695 694
696 695 /*
697 696 * Setup STMF dbuf store. Our buffers are bound to a specific
698 697 * connection so we really can't let STMF cache buffers for us.
699 698 * Consequently we'll just allocate one global buffer store.
700 699 */
701 700 dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
702 701 if (dbuf_store == NULL) {
703 702 retval = IDM_STATUS_FAIL;
704 703 goto tear_down_and_return;
705 704 }
706 705 dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
707 706 dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
708 707 dbuf_store->ds_port_private = NULL;
709 708 iscsit_global.global_dbuf_store = dbuf_store;
710 709
711 710 /* Status PDU cache */
712 711 iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
713 712 sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
714 713 &iscsit_status_pdu_constructor,
715 714 NULL, NULL, NULL, NULL, KM_SLEEP);
716 715
717 716 /* Default TPG and portal */
718 717 iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
719 718 if (iscsit_global.global_default_tpg == NULL) {
720 719 retval = IDM_STATUS_FAIL;
721 720 goto tear_down_and_return;
722 721 }
723 722
724 723 /* initialize isns client */
725 724 (void) iscsit_isns_init(hostinfo);
726 725 did_iscsit_isns_init = B_TRUE;
727 726
728 727 /* Register port provider */
729 728 pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
730 729 if (pp == NULL) {
731 730 retval = IDM_STATUS_FAIL;
732 731 goto tear_down_and_return;
733 732 }
734 733
735 734 pp->pp_portif_rev = PORTIF_REV_1;
736 735 pp->pp_instance = 0;
737 736 pp->pp_name = ISCSIT_MODNAME;
738 737 pp->pp_cb = iscsit_pp_cb;
739 738
740 739 iscsit_global.global_pp = pp;
741 740
742 741
743 742 if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
744 743 retval = IDM_STATUS_FAIL;
745 744 goto tear_down_and_return;
746 745 }
747 746
748 747 iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
749 748 1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
750 749
751 750 /* Scan staged PDUs, meaningful in MC/S situations */
752 751 iscsit_rxpdu_queue_monitor_start();
753 752
754 753 return (IDM_STATUS_SUCCESS);
755 754
756 755 tear_down_and_return:
757 756
758 757 if (iscsit_global.global_dispatch_taskq) {
759 758 taskq_destroy(iscsit_global.global_dispatch_taskq);
760 759 iscsit_global.global_dispatch_taskq = NULL;
761 760 }
762 761
763 762 if (did_iscsit_isns_init)
764 763 iscsit_isns_fini();
765 764
766 765 if (iscsit_global.global_default_tpg) {
767 766 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
768 767 iscsit_global.global_default_tpg = NULL;
769 768 }
770 769
771 770 if (iscsit_global.global_pp)
772 771 iscsit_global.global_pp = NULL;
773 772
774 773 if (pp)
775 774 stmf_free(pp);
776 775
777 776 if (iscsit_status_pdu_cache) {
778 777 kmem_cache_destroy(iscsit_status_pdu_cache);
779 778 iscsit_status_pdu_cache = NULL;
780 779 }
781 780
782 781 if (iscsit_global.global_dbuf_store) {
783 782 stmf_free(iscsit_global.global_dbuf_store);
784 783 iscsit_global.global_dbuf_store = NULL;
785 784 }
786 785
787 786 if (iscsit_global.global_tsih_pool) {
788 787 vmem_destroy(iscsit_global.global_tsih_pool);
789 788 iscsit_global.global_tsih_pool = NULL;
790 789 }
791 790
792 791 avl_destroy(&iscsit_global.global_ini_list);
793 792 avl_destroy(&iscsit_global.global_tpg_list);
794 793 list_destroy(&iscsit_global.global_deleted_target_list);
795 794 avl_destroy(&iscsit_global.global_target_list);
796 795 avl_destroy(&iscsit_global.global_discovery_sessions);
797 796
798 797 idm_refcnt_destroy(&iscsit_global.global_refcnt);
799 798
800 799 return (retval);
801 800 }
802 801
803 802 /*
804 803 * iscsit_disable_svc
805 804 *
806 805 * clean up all existing connections and deregister targets from STMF
807 806 */
808 807 static void
809 808 iscsit_disable_svc(void)
810 809 {
811 810 iscsit_sess_t *sess;
812 811
813 812 ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
814 813
815 814 iscsit_rxpdu_queue_monitor_stop();
816 815
817 816 /* tear down discovery sessions */
818 817 for (sess = avl_first(&iscsit_global.global_discovery_sessions);
819 818 sess != NULL;
820 819 sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
821 820 iscsit_sess_close(sess);
822 821
823 822 /*
824 823 * Passing NULL to iscsit_config_merge tells it to go to an empty
825 824 * config.
826 825 */
827 826 (void) iscsit_config_merge(NULL);
828 827
829 828 /*
830 829 * Wait until there are no more global references
831 830 */
832 831 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
833 832 idm_refcnt_destroy(&iscsit_global.global_refcnt);
834 833
835 834 /*
836 835 * Default TPG must be destroyed after global_refcnt is 0.
837 836 */
838 837 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
839 838
840 839 avl_destroy(&iscsit_global.global_discovery_sessions);
841 840 list_destroy(&iscsit_global.global_deleted_target_list);
842 841 avl_destroy(&iscsit_global.global_target_list);
843 842 avl_destroy(&iscsit_global.global_tpg_list);
844 843 avl_destroy(&iscsit_global.global_ini_list);
845 844
846 845 taskq_destroy(iscsit_global.global_dispatch_taskq);
847 846
848 847 iscsit_isns_fini();
849 848
850 849 stmf_free(iscsit_global.global_dbuf_store);
851 850 iscsit_global.global_dbuf_store = NULL;
852 851
853 852 (void) stmf_deregister_port_provider(iscsit_global.global_pp);
854 853 stmf_free(iscsit_global.global_pp);
855 854 iscsit_global.global_pp = NULL;
856 855
857 856 kmem_cache_destroy(iscsit_status_pdu_cache);
858 857 iscsit_status_pdu_cache = NULL;
859 858
860 859 vmem_destroy(iscsit_global.global_tsih_pool);
861 860 iscsit_global.global_tsih_pool = NULL;
862 861 }
863 862
864 863 void
865 864 iscsit_global_hold()
866 865 {
867 866 /*
868 867 * To take out a global hold, we must either own the global
869 868 * state mutex or we must be running inside of an ioctl that
870 869 * has set the global state to ISE_BUSY, ISE_DISABLING, or
871 870 * ISE_ENABLING. We don't track the "owner" for these flags,
872 871 * so just checking if they are set is enough for now.
873 872 */
874 873 ASSERT((iscsit_global.global_svc_state == ISE_ENABLING) ||
875 874 (iscsit_global.global_svc_state == ISE_DISABLING) ||
876 875 (iscsit_global.global_svc_state == ISE_BUSY) ||
877 876 MUTEX_HELD(&iscsit_global.global_state_mutex));
878 877
879 878 idm_refcnt_hold(&iscsit_global.global_refcnt);
880 879 }
881 880
882 881 void
883 882 iscsit_global_rele()
884 883 {
885 884 idm_refcnt_rele(&iscsit_global.global_refcnt);
886 885 }
887 886
888 887 void
889 888 iscsit_global_wait_ref()
890 889 {
891 890 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
892 891 }
893 892
894 893 /*
895 894 * IDM callbacks
896 895 */
897 896
898 897 /*ARGSUSED*/
899 898 void
900 899 iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
901 900 {
902 901 iscsit_conn_t *ict = ic->ic_handle;
903 902 switch (IDM_PDU_OPCODE(rx_pdu)) {
904 903 case ISCSI_OP_SCSI_CMD:
905 904 ASSERT(0); /* Shouldn't happen */
906 905 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
907 906 break;
908 907 case ISCSI_OP_SNACK_CMD:
909 908 /*
910 909 * We'll need to handle this when we support ERL1/2. For
911 910 * now we treat it as a protocol error.
912 911 */
913 912 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
914 913 idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
915 914 break;
916 915 case ISCSI_OP_SCSI_TASK_MGT_MSG:
917 916 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
918 917 iscsit_set_cmdsn(ict, rx_pdu);
919 918 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
920 919 }
921 920 break;
922 921 case ISCSI_OP_NOOP_OUT:
923 922 case ISCSI_OP_LOGIN_CMD:
924 923 case ISCSI_OP_TEXT_CMD:
925 924 case ISCSI_OP_LOGOUT_CMD:
926 925 /*
927 926 * If/when we switch to userland processing these PDU's
928 927 * will be handled by iscsitd.
929 928 */
930 929 iscsit_deferred_dispatch(rx_pdu);
931 930 break;
932 931 default:
933 932 /* Protocol error */
934 933 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
935 934 idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
936 935 break;
937 936 }
938 937 }
939 938
940 939 /*ARGSUSED*/
941 940 void
942 941 iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
943 942 {
944 943 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
945 944 }
946 945
947 946 /*
948 947 * iscsit_rx_scsi_rsp -- cause the connection to be closed if response rx'd
949 948 *
950 949 * A target sends an SCSI Response PDU, it should never receive one.
951 950 * This has been seen when running the Codemonicon suite of tests which
952 951 * does negative testing of the protocol. If such a condition occurs using
953 952 * a normal initiator it most likely means there's data corruption in the
954 953 * header and that's grounds for dropping the connection as well.
955 954 */
956 955 void
957 956 iscsit_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *rx_pdu)
958 957 {
959 958 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
960 959 idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
961 960 }
962 961
963 962 void
964 963 iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
965 964 {
966 965 iscsit_task_t *itask = idt->idt_private;
967 966
968 967 switch (status) {
969 968 case IDM_STATUS_SUSPENDED:
970 969 break;
971 970 case IDM_STATUS_ABORTED:
972 971 mutex_enter(&itask->it_mutex);
973 972 itask->it_aborted = B_TRUE;
974 973 /*
975 974 * We rely on the fact that STMF tracks outstanding
976 975 * buffer transfers and will free all of our buffers
977 976 * before freeing the task so we don't need to
978 977 * explicitly free the buffers from iscsit/idm
979 978 */
980 979 if (itask->it_stmf_abort) {
981 980 mutex_exit(&itask->it_mutex);
982 981 /*
983 982 * Task is no longer active
984 983 */
985 984 iscsit_task_done(itask);
986 985
987 986 /*
988 987 * STMF has already asked for this task to be aborted
989 988 *
990 989 * STMF specification is wrong... says to return
991 990 * STMF_ABORTED, the code actually looks for
992 991 * STMF_ABORT_SUCCESS.
993 992 */
994 993 stmf_task_lport_aborted(itask->it_stmf_task,
995 994 STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
996 995 return;
997 996 } else {
998 997 mutex_exit(&itask->it_mutex);
999 998 /*
1000 999 * Tell STMF to stop processing the task.
1001 1000 */
1002 1001 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
1003 1002 STMF_ABORTED, NULL);
1004 1003 return;
1005 1004 }
1006 1005 /*NOTREACHED*/
1007 1006 default:
1008 1007 ASSERT(0);
1009 1008 }
1010 1009 }
1011 1010
1012 1011 /*ARGSUSED*/
1013 1012 idm_status_t
1014 1013 iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
1015 1014 uintptr_t data)
1016 1015 {
1017 1016 idm_status_t rc = IDM_STATUS_SUCCESS;
1018 1017
1019 1018 /*
1020 1019 * IDM client notifications will never occur at interrupt level
1021 1020 * since they are generated from the connection state machine which
1022 1021 * running on taskq threads.
1023 1022 *
1024 1023 */
1025 1024 switch (icn) {
1026 1025 case CN_CONNECT_ACCEPT:
1027 1026 rc = iscsit_conn_accept(ic); /* No data */
1028 1027 break;
1029 1028 case CN_FFP_ENABLED:
1030 1029 rc = iscsit_ffp_enabled(ic); /* No data */
1031 1030 break;
1032 1031 case CN_FFP_DISABLED:
1033 1032 /*
1034 1033 * Data indicates whether this was the result of an
1035 1034 * explicit logout request.
1036 1035 */
1037 1036 rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
1038 1037 break;
1039 1038 case CN_CONNECT_LOST:
1040 1039 rc = iscsit_conn_lost(ic);
1041 1040 break;
1042 1041 case CN_CONNECT_DESTROY:
1043 1042 rc = iscsit_conn_destroy(ic);
1044 1043 break;
1045 1044 case CN_LOGIN_FAIL:
1046 1045 /*
1047 1046 * Force the login state machine to completion
1048 1047 */
1049 1048 rc = iscsit_login_fail(ic);
1050 1049 break;
1051 1050 default:
1052 1051 rc = IDM_STATUS_REJECT;
1053 1052 break;
1054 1053 }
1055 1054
1056 1055 return (rc);
1057 1056 }
1058 1057
1059 1058 /*
1060 1059 * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
1061 1060 * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
1062 1061 * flag is set in the pdu flags field. The StatSN is connection-wide and is
1063 1062 * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
1064 1063 * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
1065 1064 */
1066 1065 void
1067 1066 iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
1068 1067 {
1069 1068 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1070 1069 iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
1071 1070 iscsit_task_t *itask = NULL;
1072 1071 scsi_task_t *task = NULL;
1073 1072
1074 1073 mutex_enter(&ict->ict_statsn_mutex);
1075 1074 rsp->statsn = htonl(ict->ict_statsn);
1076 1075 if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
1077 1076 ict->ict_statsn++;
1078 1077 mutex_exit(&ict->ict_statsn_mutex);
1079 1078
1080 1079 /*
1081 1080 * The last SCSI Data PDU passed for a command may also contain the
1082 1081 * status if the status indicates termination with no expections, i.e.
1083 1082 * no sense data or response involved. If the command completes with
1084 1083 * an error, then the response and sense data will be sent in a
1085 1084 * separate iSCSI Response PDU.
1086 1085 */
1087 1086 if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
1088 1087 itask = idm_task->idt_private;
1089 1088 task = itask->it_stmf_task;
1090 1089
1091 1090 rsp->cmd_status = task->task_scsi_status;
1092 1091 rsp->flags |= ISCSI_FLAG_DATA_STATUS;
1093 1092 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1094 1093 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1095 1094 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1096 1095 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1097 1096 }
1098 1097 rsp->residual_count = htonl(task->task_resid);
1099 1098
1100 1099 /*
1101 1100 * Removing the task from the session task list
1102 1101 * just before the status is sent in the last
1103 1102 * Data PDU transfer
1104 1103 */
1105 1104 iscsit_task_done(itask);
1106 1105 }
1107 1106 }
1108 1107
1109 1108 void
1110 1109 iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
1111 1110 {
1112 1111 iscsit_task_t *itask = idm_task->idt_private;
1113 1112 iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
1114 1113
1115 1114 /*
1116 1115 * We acquired iscsit_sess_t.ist_sn_mutex in iscsit_xfer_scsi_data
1117 1116 */
1118 1117 ASSERT(MUTEX_HELD(&itask->it_ict->ict_sess->ist_sn_mutex));
1119 1118 /*
1120 1119 * On incoming data, the target transfer tag and Lun is only
1121 1120 * provided by the target if the A bit is set, Since the target
1122 1121 * does not currently support Error Recovery Level 1, the A
1123 1122 * bit is never set.
1124 1123 */
1125 1124 dh->opcode = opcode;
1126 1125 dh->itt = itask->it_itt;
1127 1126 dh->ttt = ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_SCSI_DATA_RSP) ?
1128 1127 ISCSI_RSVD_TASK_TAG : itask->it_ttt;
1129 1128
1130 1129 dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1131 1130 dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1132 1131
1133 1132 /*
1134 1133 * IDM must set:
1135 1134 *
1136 1135 * data.flags and rtt.flags
1137 1136 * data.dlength
1138 1137 * data.datasn
1139 1138 * data.offset
1140 1139 * statsn, residual_count and cmd_status (for phase collapse)
1141 1140 * rtt.rttsn
1142 1141 * rtt.data_offset
1143 1142 * rtt.data_length
1144 1143 */
1145 1144 }
1146 1145
1147 1146 void
1148 1147 iscsit_keepalive(idm_conn_t *ic)
1149 1148 {
1150 1149 idm_pdu_t *nop_in_pdu;
1151 1150 iscsi_nop_in_hdr_t *nop_in;
1152 1151 iscsit_conn_t *ict = ic->ic_handle;
1153 1152
1154 1153 /*
1155 1154 * IDM noticed the connection has been idle for too long so it's
1156 1155 * time to provoke some activity. Build and transmit an iSCSI
1157 1156 * nop-in PDU -- when the initiator responds it will be counted
1158 1157 * as "activity" and keep the connection alive.
1159 1158 *
1160 1159 * We don't actually care about the response here at the iscsit level
1161 1160 * so we will just throw it away without looking at it when it arrives.
1162 1161 */
1163 1162 nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
1164 1163 idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
1165 1164 nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
1166 1165 bzero(nop_in, sizeof (*nop_in));
1167 1166 nop_in->opcode = ISCSI_OP_NOOP_IN;
1168 1167 nop_in->flags = ISCSI_FLAG_FINAL;
1169 1168 nop_in->itt = ISCSI_RSVD_TASK_TAG;
1170 1169 /*
1171 1170 * When the target sends a NOP-In as a Ping, the target transfer tag
1172 1171 * is set to a valid (not reserved) value and the initiator task tag
1173 1172 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
1174 1173 * will always contain the next sequence number but the StatSN for the
1175 1174 * connection is not advanced after this PDU is sent.
1176 1175 */
1177 1176 nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
1178 1177 /*
1179 1178 * This works because we don't currently allocate ttt's anywhere else
1180 1179 * in iscsit so as long as we stay out of IDM's range we are safe.
1181 1180 * If we need to allocate ttt's for other PDU's in the future this will
1182 1181 * need to be improved.
1183 1182 */
1184 1183 mutex_enter(&ict->ict_mutex);
1185 1184 nop_in->ttt = ict->ict_keepalive_ttt;
1186 1185 ict->ict_keepalive_ttt++;
1187 1186 if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
1188 1187 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
1189 1188 mutex_exit(&ict->ict_mutex);
1190 1189
1191 1190 iscsit_pdu_tx(nop_in_pdu);
1192 1191 }
1193 1192
1194 1193 static idm_status_t
1195 1194 iscsit_conn_accept(idm_conn_t *ic)
1196 1195 {
1197 1196 iscsit_conn_t *ict;
1198 1197
1199 1198 /*
1200 1199 * We need to get a global hold here to ensure that the service
1201 1200 * doesn't get shutdown prior to establishing a session. This
1202 1201 * gets released in iscsit_conn_destroy().
1203 1202 */
1204 1203 mutex_enter(&iscsit_global.global_state_mutex);
1205 1204 if (iscsit_global.global_svc_state != ISE_ENABLED) {
1206 1205 mutex_exit(&iscsit_global.global_state_mutex);
1207 1206 return (IDM_STATUS_FAIL);
1208 1207 }
1209 1208 iscsit_global_hold();
1210 1209 mutex_exit(&iscsit_global.global_state_mutex);
1211 1210
1212 1211 /*
1213 1212 * Allocate an associated iscsit structure to represent this
1214 1213 * connection. We shouldn't really create a session until we
1215 1214 * get the first login PDU.
1216 1215 */
1217 1216 ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1218 1217
1219 1218 ict->ict_ic = ic;
1220 1219 ict->ict_statsn = 1;
1221 1220 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1222 1221 ic->ic_handle = ict;
1223 1222 mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
1224 1223 mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1225 1224 idm_refcnt_init(&ict->ict_refcnt, ict);
1226 1225
1227 1226 /*
1228 1227 * Initialize login state machine
1229 1228 */
1230 1229 if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1231 1230 iscsit_global_rele();
1232 1231 /*
1233 1232 * Cleanup the ict after idm notifies us about this failure
1234 1233 */
1235 1234 return (IDM_STATUS_FAIL);
1236 1235 }
1237 1236
1238 1237 return (IDM_STATUS_SUCCESS);
1239 1238 }
1240 1239
1241 1240 idm_status_t
1242 1241 iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1243 1242 {
1244 1243 idm_status_t result;
1245 1244
1246 1245 /*
1247 1246 * Note in new connection state that this connection is
1248 1247 * reinstating an existing connection.
1249 1248 */
1250 1249 new_ict->ict_reinstating = B_TRUE;
1251 1250 new_ict->ict_reinstate_conn = reinstate_ict;
1252 1251 new_ict->ict_statsn = reinstate_ict->ict_statsn;
1253 1252
1254 1253 /*
1255 1254 * Now generate connection state machine event to existing connection
1256 1255 * so that it starts the cleanup process.
1257 1256 */
1258 1257 result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1259 1258 new_ict->ict_ic);
1260 1259
1261 1260 return (result);
1262 1261 }
1263 1262
1264 1263 void
1265 1264 iscsit_conn_hold(iscsit_conn_t *ict)
1266 1265 {
1267 1266 idm_refcnt_hold(&ict->ict_refcnt);
1268 1267 }
1269 1268
1270 1269 void
1271 1270 iscsit_conn_rele(iscsit_conn_t *ict)
1272 1271 {
1273 1272 idm_refcnt_rele(&ict->ict_refcnt);
1274 1273 }
1275 1274
1276 1275 void
1277 1276 iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1278 1277 {
1279 1278 idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1280 1279 }
1281 1280
1282 1281 void
1283 1282 iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1284 1283 {
1285 1284 idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1286 1285 }
1287 1286
1288 1287 static idm_status_t
1289 1288 iscsit_login_fail(idm_conn_t *ic)
1290 1289 {
1291 1290 iscsit_conn_t *ict = ic->ic_handle;
1292 1291
1293 1292 /* Generate login state machine event */
1294 1293 iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1295 1294
1296 1295 return (IDM_STATUS_SUCCESS);
1297 1296 }
1298 1297
1299 1298 static idm_status_t
1300 1299 iscsit_ffp_enabled(idm_conn_t *ic)
1301 1300 {
1302 1301 iscsit_conn_t *ict = ic->ic_handle;
1303 1302
1304 1303 /* Generate session state machine event */
1305 1304 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1306 1305
1307 1306 return (IDM_STATUS_SUCCESS);
1308 1307 }
1309 1308
1310 1309 static idm_status_t
1311 1310 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1312 1311 {
1313 1312 iscsit_conn_t *ict = ic->ic_handle;
1314 1313
1315 1314 /* Generate session state machine event */
1316 1315 switch (disable_class) {
1317 1316 case FD_CONN_FAIL:
1318 1317 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1319 1318 break;
1320 1319 case FD_CONN_LOGOUT:
1321 1320 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1322 1321 break;
1323 1322 case FD_SESS_LOGOUT:
1324 1323 iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1325 1324 break;
1326 1325 default:
1327 1326 ASSERT(0);
1328 1327 }
1329 1328
1330 1329 return (IDM_STATUS_SUCCESS);
1331 1330 }
1332 1331
1333 1332 static idm_status_t
1334 1333 iscsit_conn_lost(idm_conn_t *ic)
1335 1334 {
1336 1335 iscsit_conn_t *ict = ic->ic_handle;
1337 1336 iscsit_sess_t *ist = ict->ict_sess;
1338 1337 iscsit_cbuf_t *cbuf;
1339 1338 idm_pdu_t *rx_pdu;
1340 1339 int i;
1341 1340
1342 1341 mutex_enter(&ict->ict_mutex);
1343 1342 ict->ict_lost = B_TRUE;
1344 1343 mutex_exit(&ict->ict_mutex);
1345 1344 /*
1346 1345 * scrub the staging queue for all PDUs on this connection
1347 1346 */
1348 1347 if (ist != NULL) {
1349 1348 mutex_enter(&ist->ist_sn_mutex);
1350 1349 for (cbuf = ist->ist_rxpdu_queue, i = 0;
1351 1350 ((cbuf->cb_num_elems > 0) && (i < ISCSIT_RXPDU_QUEUE_LEN));
1352 1351 i++) {
1353 1352 if (((rx_pdu = cbuf->cb_buffer[i]) != NULL) &&
1354 1353 (rx_pdu->isp_ic == ic)) {
1355 1354 /* conn is lost, drop the pdu */
1356 1355 DTRACE_PROBE3(scrubbing__staging__queue,
1357 1356 iscsit_sess_t *, ist, idm_conn_t *, ic,
1358 1357 idm_pdu_t *, rx_pdu);
1359 1358 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
1360 1359 cbuf->cb_buffer[i] = NULL;
1361 1360 cbuf->cb_num_elems--;
1362 1361 iscsit_conn_dispatch_rele(ict);
1363 1362 }
1364 1363 }
1365 1364 mutex_exit(&ist->ist_sn_mutex);
1366 1365 }
1367 1366 /*
1368 1367 * Make sure there aren't any PDU's transitioning from the receive
1369 1368 * handler to the dispatch taskq.
1370 1369 */
1371 1370 idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1372 1371
1373 1372 return (IDM_STATUS_SUCCESS);
1374 1373 }
1375 1374
1376 1375 static idm_status_t
1377 1376 iscsit_conn_destroy(idm_conn_t *ic)
1378 1377 {
1379 1378 iscsit_conn_t *ict = ic->ic_handle;
1380 1379
1381 1380 mutex_enter(&ict->ict_mutex);
1382 1381 ict->ict_destroyed = B_TRUE;
1383 1382 mutex_exit(&ict->ict_mutex);
1384 1383
1385 1384 /* Generate session state machine event */
1386 1385 if (ict->ict_sess != NULL) {
1387 1386 /*
1388 1387 * Session state machine will call iscsit_conn_destroy_done()
1389 1388 * when it has removed references to this connection.
1390 1389 */
1391 1390 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1392 1391 }
1393 1392
1394 1393 idm_refcnt_wait_ref(&ict->ict_refcnt);
1395 1394 /*
1396 1395 * The session state machine does not need to post
1397 1396 * events to IDM any longer, so it is safe to set
1398 1397 * the idm connection reference to NULL
1399 1398 */
1400 1399 ict->ict_ic = NULL;
1401 1400
1402 1401 /* Reap the login state machine */
1403 1402 iscsit_login_sm_fini(ict);
1404 1403
1405 1404 /* Clean up any text command remnants */
1406 1405 iscsit_text_cmd_fini(ict);
1407 1406
1408 1407 mutex_destroy(&ict->ict_mutex);
1409 1408 idm_refcnt_destroy(&ict->ict_refcnt);
1410 1409 kmem_free(ict, sizeof (*ict));
1411 1410
1412 1411 iscsit_global_rele();
1413 1412
1414 1413 return (IDM_STATUS_SUCCESS);
1415 1414 }
1416 1415
1417 1416 void
1418 1417 iscsit_conn_logout(iscsit_conn_t *ict)
1419 1418 {
1420 1419 /*
1421 1420 * If the iscsi connection is active, then
1422 1421 * logout the IDM connection by sending a
1423 1422 * CE_LOGOUT_SESSION_SUCCESS, else, no action
1424 1423 * needs to be taken because the connection
1425 1424 * is already in the teardown process.
1426 1425 */
1427 1426 mutex_enter(&ict->ict_mutex);
1428 1427 if (ict->ict_lost == B_FALSE && ict->ict_destroyed == B_FALSE) {
1429 1428 idm_conn_event(ict->ict_ic, CE_LOGOUT_SESSION_SUCCESS, NULL);
1430 1429 }
1431 1430 mutex_exit(&ict->ict_mutex);
1432 1431 }
1433 1432
1434 1433 /*
1435 1434 * STMF-related functions
1436 1435 *
1437 1436 * iSCSI to STMF mapping
1438 1437 *
1439 1438 * Session == ?
1440 1439 * Connection == bound to local port but not itself a local port
1441 1440 * Target
1442 1441 * Target portal (group?) == local port (really but we're not going to do this)
1443 1442 * iscsit needs to map connections to local ports (whatever we decide
1444 1443 * they are)
1445 1444 * Target == ?
1446 1445 */
1447 1446
1448 1447 /*ARGSUSED*/
1449 1448 static stmf_data_buf_t *
1450 1449 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1451 1450 uint32_t flags)
1452 1451 {
1453 1452 iscsit_task_t *itask = task->task_port_private;
1454 1453 idm_buf_t *idm_buffer;
1455 1454 iscsit_buf_t *ibuf;
1456 1455 stmf_data_buf_t *result;
1457 1456 uint32_t bsize;
1458 1457
1459 1458 /*
1460 1459 * If the requested size is larger than MaxBurstLength and the
1461 1460 * given pminsize is also larger than MaxBurstLength, then the
1462 1461 * allocation fails (dbuf = NULL) and pminsize is modified to
1463 1462 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
1464 1463 * this function with the corrected values for transfer.
1465 1464 */
1466 1465 ASSERT(pminsize);
1467 1466 if (size <= itask->it_ict->ict_op.op_max_burst_length) {
1468 1467 bsize = size;
1469 1468 } else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
1470 1469 bsize = itask->it_ict->ict_op.op_max_burst_length;
1471 1470 } else {
1472 1471 *pminsize = itask->it_ict->ict_op.op_max_burst_length;
1473 1472 return (NULL);
1474 1473 }
1475 1474
1476 1475 /* Alloc buffer */
1477 1476 idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1478 1477 if (idm_buffer != NULL) {
1479 1478 result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1480 1479 sizeof (iscsit_buf_t), 0);
1481 1480 if (result != NULL) {
1482 1481 /* Fill in stmf_data_buf_t */
1483 1482 ibuf = result->db_port_private;
1484 1483 ibuf->ibuf_idm_buf = idm_buffer;
1485 1484 ibuf->ibuf_stmf_buf = result;
1486 1485 ibuf->ibuf_is_immed = B_FALSE;
1487 1486 result->db_flags = DB_DONT_CACHE;
1488 1487 result->db_buf_size = bsize;
1489 1488 result->db_data_size = bsize;
1490 1489 result->db_sglist_length = 1;
1491 1490 result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1492 1491 result->db_sglist[0].seg_length =
1493 1492 idm_buffer->idb_buflen;
1494 1493 return (result);
1495 1494 }
1496 1495
1497 1496 /* Couldn't get the stmf_data_buf_t so free the buffer */
1498 1497 idm_buf_free(idm_buffer);
1499 1498 }
1500 1499
1501 1500 return (NULL);
1502 1501 }
1503 1502
1504 1503 /*ARGSUSED*/
1505 1504 static void
1506 1505 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1507 1506 {
1508 1507 iscsit_buf_t *ibuf = dbuf->db_port_private;
1509 1508
1510 1509 if (ibuf->ibuf_is_immed) {
1511 1510 /*
1512 1511 * The iscsit_buf_t structure itself will be freed with its
1513 1512 * associated task. Here we just need to free the PDU that
1514 1513 * held the immediate data.
1515 1514 */
1516 1515 idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1517 1516 ibuf->ibuf_immed_data_pdu = 0;
1518 1517 } else {
1519 1518 idm_buf_free(ibuf->ibuf_idm_buf);
1520 1519 stmf_free(dbuf);
1521 1520 }
1522 1521 }
1523 1522
1524 1523 /*ARGSUSED*/
1525 1524 stmf_status_t
1526 1525 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1527 1526 uint32_t ioflags)
1528 1527 {
1529 1528 iscsit_task_t *iscsit_task = task->task_port_private;
1530 1529 iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1531 1530 iscsit_buf_t *ibuf = dbuf->db_port_private;
1532 1531 int idm_rc;
1533 1532
1534 1533 /*
1535 1534 * If we are aborting then we can ignore this request
1536 1535 */
1537 1536 if (iscsit_task->it_stmf_abort) {
1538 1537 return (STMF_SUCCESS);
1539 1538 }
1540 1539
1541 1540 /*
1542 1541 * If it's not immediate data then start the transfer
1543 1542 */
1544 1543 ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1545 1544 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
1546 1545 /*
1547 1546 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
1548 1547 * the port provider to phase-collapse, i.e. send the status
1549 1548 * along with the final data PDU for the command. The port
1550 1549 * provider passes this request to the transport layer by
1551 1550 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
1552 1551 */
1553 1552 if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
1554 1553 iscsit_task->it_idm_task->idt_flags |=
1555 1554 IDM_TASK_PHASECOLLAPSE_REQ;
1556 1555 /*
1557 1556 * IDM will call iscsit_build_hdr so lock now to serialize
1558 1557 * access to the SN values. We need to lock here to enforce
1559 1558 * lock ordering
1560 1559 */
1561 1560 mutex_enter(&ict_sess->ist_sn_mutex);
1562 1561 idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1563 1562 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1564 1563 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1565 1564 mutex_exit(&ict_sess->ist_sn_mutex);
1566 1565
1567 1566 return (iscsit_idm_to_stmf(idm_rc));
1568 1567 } else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
1569 1568 /* Grab the SN lock (see comment above) */
1570 1569 mutex_enter(&ict_sess->ist_sn_mutex);
1571 1570 idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1572 1571 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1573 1572 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1574 1573 mutex_exit(&ict_sess->ist_sn_mutex);
1575 1574
1576 1575 return (iscsit_idm_to_stmf(idm_rc));
1577 1576 }
1578 1577
1579 1578 /* What are we supposed to do if there is no direction? */
1580 1579 return (STMF_INVALID_ARG);
1581 1580 }
1582 1581
1583 1582 static void
1584 1583 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1585 1584 {
1586 1585 iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1587 1586 stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1588 1587
1589 1588 dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1590 1589
1591 1590 /*
1592 1591 * If the task has been aborted then we don't need to call STMF
1593 1592 */
1594 1593 if (itask->it_stmf_abort) {
1595 1594 return;
1596 1595 }
1597 1596
1598 1597 /*
1599 1598 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
1600 1599 * for a successful command contains the status as requested by
1601 1600 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
1602 1601 * transport does not support phase-collapse. So pretend we are
1603 1602 * COMSTAR and send the status in a separate PDU now.
1604 1603 */
1605 1604 if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
1606 1605 /*
1607 1606 * Mark task complete and notify COMSTAR
1608 1607 * that the status has been sent.
1609 1608 */
1610 1609 itask->it_idm_task->idt_state = TASK_COMPLETE;
1611 1610 stmf_send_status_done(itask->it_stmf_task,
1612 1611 iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
1613 1612 } else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1614 1613 status == IDM_STATUS_SUCCESS) {
1615 1614
1616 1615 /*
1617 1616 * The iscsi target port provider - for iSER, emulates the
1618 1617 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
1619 1618 * it sends the status in a separate PDU after the data
1620 1619 * transfer. In this case the port provider should first
1621 1620 * call stmf_data_xfer_done() to mark the transfer complete
1622 1621 * and then send the status. Although STMF will free the
1623 1622 * buffer at the time the task is freed, even if the transfer
1624 1623 * is not marked complete, this behavior makes statistics
1625 1624 * gathering and task state tracking more difficult than it
1626 1625 * needs to be.
1627 1626 */
1628 1627 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1629 1628 if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
1630 1629 != STMF_SUCCESS) {
1631 1630 stmf_send_status_done(itask->it_stmf_task,
1632 1631 STMF_FAILURE, STMF_IOF_LPORT_DONE);
1633 1632 }
1634 1633 } else {
1635 1634 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1636 1635 /* don't touch dbuf after stmf_data_xfer_done */
1637 1636 }
1638 1637 }
1639 1638
1640 1639
1641 1640 /*ARGSUSED*/
1642 1641 stmf_status_t
1643 1642 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1644 1643 {
1645 1644 iscsit_task_t *itask = task->task_port_private;
1646 1645 iscsi_scsi_rsp_hdr_t *rsp;
1647 1646 idm_pdu_t *pdu;
1648 1647 int resp_datalen;
1649 1648
1650 1649 /*
1651 1650 * If this task is aborted then we don't need to respond.
1652 1651 */
1653 1652 if (itask->it_stmf_abort) {
1654 1653 return (STMF_SUCCESS);
1655 1654 }
1656 1655
1657 1656 /*
1658 1657 * If this is a task management status, handle it elsewhere.
1659 1658 */
1660 1659 if (task->task_mgmt_function != TM_NONE) {
1661 1660 /*
1662 1661 * Don't wait for the PDU completion to tell STMF
1663 1662 * the task is done -- it doesn't really matter and
1664 1663 * it makes life complicated if STMF later asks us to
1665 1664 * abort the request and we don't know whether the
1666 1665 * status has been sent or not.
1667 1666 */
1668 1667 itask->it_tm_responded = B_TRUE;
1669 1668 iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1670 1669 (task->task_completion_status == STMF_SUCCESS) ?
1671 1670 SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1672 1671 stmf_send_status_done(task, STMF_SUCCESS,
1673 1672 STMF_IOF_LPORT_DONE);
1674 1673 return (STMF_SUCCESS);
1675 1674 }
1676 1675
1677 1676 /*
1678 1677 * Remove the task from the session task list
1679 1678 */
1680 1679 iscsit_task_done(itask);
1681 1680
1682 1681 /*
1683 1682 * Send status
1684 1683 */
1685 1684 mutex_enter(&itask->it_idm_task->idt_mutex);
1686 1685 if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1687 1686 (task->task_completion_status == STMF_SUCCESS) &&
1688 1687 (task->task_sense_length == 0) &&
1689 1688 (task->task_resid == 0)) {
1690 1689 itask->it_idm_task->idt_state = TASK_COMPLETE;
1691 1690 /* PDU callback releases task hold */
1692 1691 idm_task_hold(itask->it_idm_task);
1693 1692 mutex_exit(&itask->it_idm_task->idt_mutex);
1694 1693 /*
1695 1694 * Fast path. Cached status PDU's are already
1696 1695 * initialized. We just need to fill in
1697 1696 * connection and task information. StatSN is
1698 1697 * incremented by 1 for every status sent a
1699 1698 * connection.
1700 1699 */
1701 1700 pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1702 1701 pdu->isp_ic = itask->it_ict->ict_ic;
1703 1702 pdu->isp_private = itask;
1704 1703 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1705 1704
1706 1705 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1707 1706 rsp->itt = itask->it_itt;
1708 1707 /*
1709 1708 * ExpDataSN is the number of R2T and Data-In (read)
1710 1709 * PDUs the target has sent for the SCSI command.
1711 1710 *
1712 1711 * Since there is no support for bidirectional transfer
1713 1712 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
1714 1713 * both is valid at any time
1715 1714 */
1716 1715 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1717 1716 htonl(itask->it_idm_task->idt_exp_datasn):
1718 1717 htonl(itask->it_idm_task->idt_exp_rttsn);
1719 1718 rsp->cmd_status = task->task_scsi_status;
1720 1719 iscsit_pdu_tx(pdu);
1721 1720 return (STMF_SUCCESS);
1722 1721 } else {
1723 1722 if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1724 1723 mutex_exit(&itask->it_idm_task->idt_mutex);
1725 1724 return (STMF_FAILURE);
1726 1725 }
1727 1726 itask->it_idm_task->idt_state = TASK_COMPLETE;
1728 1727 /* PDU callback releases task hold */
1729 1728 idm_task_hold(itask->it_idm_task);
1730 1729 mutex_exit(&itask->it_idm_task->idt_mutex);
1731 1730
1732 1731 resp_datalen = (task->task_sense_length == 0) ? 0 :
1733 1732 (task->task_sense_length + sizeof (uint16_t));
1734 1733
1735 1734 pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1736 1735 idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1737 1736 iscsit_send_status_done);
1738 1737 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1739 1738
1740 1739 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1741 1740 bzero(rsp, sizeof (*rsp));
1742 1741 rsp->opcode = ISCSI_OP_SCSI_RSP;
1743 1742
1744 1743 rsp->flags = ISCSI_FLAG_FINAL;
1745 1744 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1746 1745 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1747 1746 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1748 1747 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1749 1748 }
1750 1749
1751 1750 rsp->bi_residual_count = 0;
1752 1751 rsp->residual_count = htonl(task->task_resid);
1753 1752 rsp->itt = itask->it_itt;
1754 1753 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
1755 1754 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1756 1755 htonl(itask->it_idm_task->idt_exp_datasn):
1757 1756 htonl(itask->it_idm_task->idt_exp_rttsn);
1758 1757 rsp->cmd_status = task->task_scsi_status;
1759 1758 if (task->task_sense_length != 0) {
1760 1759 /*
1761 1760 * Add a byte to provide the sense length in
1762 1761 * the response
1763 1762 */
1764 1763 *(uint16_t *)((void *)pdu->isp_data) =
1765 1764 htons(task->task_sense_length);
1766 1765 bcopy(task->task_sense_data,
1767 1766 (uint8_t *)pdu->isp_data +
1768 1767 sizeof (uint16_t),
1769 1768 task->task_sense_length);
1770 1769 hton24(rsp->dlength, resp_datalen);
1771 1770 }
1772 1771
1773 1772 DTRACE_PROBE5(iscsi__scsi__response,
1774 1773 iscsit_conn_t *, itask->it_ict,
1775 1774 uint8_t, rsp->response,
1776 1775 uint8_t, rsp->cmd_status,
1777 1776 idm_pdu_t *, pdu,
1778 1777 scsi_task_t *, task);
1779 1778
1780 1779 iscsit_pdu_tx(pdu);
1781 1780
1782 1781 return (STMF_SUCCESS);
1783 1782 }
1784 1783 }
1785 1784
1786 1785 /*ARGSUSED*/
1787 1786 static void
1788 1787 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1789 1788 {
1790 1789 iscsit_task_t *itask;
1791 1790 boolean_t aborted;
1792 1791
1793 1792 itask = pdu->isp_private;
1794 1793 aborted = itask->it_stmf_abort;
1795 1794
1796 1795 /*
1797 1796 * After releasing the hold the task may be freed at any time so
1798 1797 * don't touch it.
1799 1798 */
1800 1799 idm_task_rele(itask->it_idm_task);
1801 1800 if (!aborted) {
1802 1801 stmf_send_status_done(itask->it_stmf_task,
1803 1802 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1804 1803 }
1805 1804 kmem_cache_free(iscsit_status_pdu_cache, pdu);
1806 1805 }
1807 1806
1808 1807 /*ARGSUSED*/
1809 1808 static void
1810 1809 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1811 1810 {
1812 1811 iscsit_task_t *itask;
1813 1812 boolean_t aborted;
1814 1813
1815 1814 itask = pdu->isp_private;
1816 1815 aborted = itask->it_stmf_abort;
1817 1816
1818 1817 /*
1819 1818 * After releasing the hold the task may be freed at any time so
1820 1819 * don't touch it.
1821 1820 */
1822 1821 idm_task_rele(itask->it_idm_task);
1823 1822 if (!aborted) {
1824 1823 stmf_send_status_done(itask->it_stmf_task,
1825 1824 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1826 1825 }
1827 1826 idm_pdu_free(pdu);
1828 1827 }
1829 1828
1830 1829
1831 1830 void
1832 1831 iscsit_lport_task_free(scsi_task_t *task)
1833 1832 {
1834 1833 iscsit_task_t *itask = task->task_port_private;
1835 1834
1836 1835 /* We only call idm_task_start for regular tasks, not task management */
1837 1836 if (task->task_mgmt_function == TM_NONE) {
1838 1837 idm_task_done(itask->it_idm_task);
1839 1838 iscsit_task_free(itask);
1840 1839 return;
1841 1840 } else {
1842 1841 iscsit_tm_task_free(itask);
1843 1842 }
1844 1843 }
1845 1844
1846 1845 /*ARGSUSED*/
1847 1846 stmf_status_t
1848 1847 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1849 1848 {
1850 1849 scsi_task_t *st = (scsi_task_t *)arg;
1851 1850 iscsit_task_t *iscsit_task;
1852 1851 idm_task_t *idt;
1853 1852
1854 1853 /*
1855 1854 * If this is a task management request then there's really not much to
1856 1855 * do.
1857 1856 */
1858 1857 if (st->task_mgmt_function != TM_NONE) {
1859 1858 return (STMF_ABORT_SUCCESS);
1860 1859 }
1861 1860
1862 1861 /*
1863 1862 * Regular task, start cleaning up
1864 1863 */
1865 1864 iscsit_task = st->task_port_private;
1866 1865 idt = iscsit_task->it_idm_task;
1867 1866 mutex_enter(&iscsit_task->it_mutex);
1868 1867 iscsit_task->it_stmf_abort = B_TRUE;
1869 1868 if (iscsit_task->it_aborted) {
1870 1869 mutex_exit(&iscsit_task->it_mutex);
1871 1870 /*
1872 1871 * Task is no longer active
1873 1872 */
1874 1873 iscsit_task_done(iscsit_task);
1875 1874
1876 1875 /*
1877 1876 * STMF specification is wrong... says to return
1878 1877 * STMF_ABORTED, the code actually looks for
1879 1878 * STMF_ABORT_SUCCESS.
1880 1879 */
1881 1880 return (STMF_ABORT_SUCCESS);
1882 1881 } else {
1883 1882 mutex_exit(&iscsit_task->it_mutex);
1884 1883 /*
1885 1884 * Call IDM to abort the task. Due to a variety of
1886 1885 * circumstances the task may already be in the process of
1887 1886 * aborting.
1888 1887 * We'll let IDM worry about rationalizing all that except
1889 1888 * for one particular instance. If the state of the task
1890 1889 * is TASK_COMPLETE, we need to indicate to the framework
1891 1890 * that we are in fact done. This typically happens with
1892 1891 * framework-initiated task management type requests
1893 1892 * (e.g. abort task).
1894 1893 */
1895 1894 if (idt->idt_state == TASK_COMPLETE) {
1896 1895 idm_refcnt_wait_ref(&idt->idt_refcnt);
1897 1896 return (STMF_ABORT_SUCCESS);
1898 1897 } else {
1899 1898 idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT);
1900 1899 return (STMF_SUCCESS);
1901 1900 }
1902 1901 }
1903 1902
1904 1903 /*NOTREACHED*/
1905 1904 }
1906 1905
1907 1906 /*ARGSUSED*/
1908 1907 void
1909 1908 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1910 1909 {
1911 1910 iscsit_tgt_t *iscsit_tgt;
1912 1911
1913 1912 ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1914 1913 (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1915 1914 (cmd == STMF_CMD_LPORT_OFFLINE) ||
1916 1915 (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1917 1916
1918 1917 iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1919 1918
1920 1919 switch (cmd) {
1921 1920 case STMF_CMD_LPORT_ONLINE:
1922 1921 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1923 1922 break;
1924 1923 case STMF_CMD_LPORT_OFFLINE:
1925 1924 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1926 1925 break;
1927 1926 case STMF_ACK_LPORT_ONLINE_COMPLETE:
1928 1927 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1929 1928 break;
1930 1929 case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1931 1930 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1932 1931 break;
1933 1932
1934 1933 default:
1935 1934 break;
1936 1935 }
1937 1936 }
1938 1937
1939 1938 static stmf_status_t
1940 1939 iscsit_idm_to_stmf(idm_status_t idmrc)
1941 1940 {
1942 1941 switch (idmrc) {
1943 1942 case IDM_STATUS_SUCCESS:
1944 1943 return (STMF_SUCCESS);
1945 1944 default:
1946 1945 return (STMF_FAILURE);
1947 1946 }
1948 1947 /*NOTREACHED*/
1949 1948 }
1950 1949
1951 1950 void
1952 1951 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1953 1952 {
1954 1953 iscsit_conn_t *ict = ic->ic_handle;
1955 1954
1956 1955 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
1957 1956 iscsit_post_scsi_cmd(ic, rx_pdu);
1958 1957 }
1959 1958 iscsit_process_pdu_in_queue(ict->ict_sess);
1960 1959 }
1961 1960
1962 1961 /*
1963 1962 * ISCSI protocol
1964 1963 */
1965 1964
1966 1965 void
1967 1966 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1968 1967 {
1969 1968 iscsit_conn_t *ict;
1970 1969 iscsit_task_t *itask;
1971 1970 scsi_task_t *task;
1972 1971 iscsit_buf_t *ibuf;
1973 1972 iscsi_scsi_cmd_hdr_t *iscsi_scsi =
1974 1973 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1975 1974 iscsi_addl_hdr_t *ahs_hdr;
1976 1975 uint16_t addl_cdb_len = 0;
1977 1976
1978 1977 ict = ic->ic_handle;
1979 1978
1980 1979 itask = iscsit_task_alloc(ict);
1981 1980 if (itask == NULL) {
1982 1981 /* Finish processing request */
1983 1982 iscsit_set_cmdsn(ict, rx_pdu);
1984 1983
1985 1984 iscsit_send_direct_scsi_resp(ict, rx_pdu,
1986 1985 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1987 1986 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1988 1987 return;
1989 1988 }
1990 1989
1991 1990 /*
1992 1991 * Note CmdSN and ITT in task. IDM will have already validated this
1993 1992 * request against the connection state so we don't need to check
1994 1993 * that (the connection may have changed state in the meantime but
1995 1994 * we will catch that when we try to send a response)
1996 1995 */
1997 1996 itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
1998 1997 itask->it_itt = iscsi_scsi->itt;
1999 1998
2000 1999 /*
2001 2000 * Check for extended CDB AHS
2002 2001 */
2003 2002 if (iscsi_scsi->hlength > 0) {
2004 2003 ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
2005 2004 addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
2006 2005 ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
2007 2006 if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
2008 2007 iscsi_scsi->hlength) {
2009 2008 /* Mangled header info, drop it */
2010 2009 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2011 2010 return;
2012 2011 }
2013 2012 }
2014 2013
2015 2014 ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
2016 2015
2017 2016 /*
2018 2017 * Add task to session list. This function will also check to
2019 2018 * ensure that the task does not already exist.
2020 2019 */
2021 2020 if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
2022 2021 /*
2023 2022 * Task exists, free all resources and reject. Don't
2024 2023 * update expcmdsn in this case because RFC 3720 says
2025 2024 * "The CmdSN of the rejected command PDU (if it is a
2026 2025 * non-immediate command) MUST NOT be considered received
2027 2026 * by the target (i.e., a command sequence gap must be
2028 2027 * assumed for the CmdSN), even though the CmdSN of the
2029 2028 * rejected command PDU may be reliably ascertained. Upon
2030 2029 * receiving the Reject, the initiator MUST plug the CmdSN
2031 2030 * gap in order to continue to use the session. The gap
2032 2031 * may be plugged either by transmitting a command PDU
2033 2032 * with the same CmdSN, or by aborting the task (see section
2034 2033 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
2035 2034 */
2036 2035 iscsit_task_free(itask);
2037 2036 iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
2038 2037 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2039 2038 return;
2040 2039 }
2041 2040
2042 2041 /* Update sequence numbers */
2043 2042 iscsit_set_cmdsn(ict, rx_pdu);
2044 2043
2045 2044 /*
2046 2045 * Allocate STMF task
2047 2046 */
2048 2047 itask->it_stmf_task = stmf_task_alloc(
2049 2048 itask->it_ict->ict_sess->ist_lport,
2050 2049 itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
2051 2050 16 + addl_cdb_len, 0);
2052 2051 if (itask->it_stmf_task == NULL) {
2053 2052 /*
2054 2053 * Either stmf really couldn't get memory for a task or,
2055 2054 * more likely, the LU is currently in reset. Either way
2056 2055 * we have no choice but to fail the request.
2057 2056 */
2058 2057 iscsit_task_done(itask);
2059 2058 iscsit_task_free(itask);
2060 2059 iscsit_send_direct_scsi_resp(ict, rx_pdu,
2061 2060 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
2062 2061 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2063 2062 return;
2064 2063 }
2065 2064
2066 2065 task = itask->it_stmf_task;
2067 2066 task->task_port_private = itask;
2068 2067
2069 2068 bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
2070 2069
2071 2070 /*
2072 2071 * iSCSI and Comstar use the same values. Should we rely on this
2073 2072 * or translate them bit-wise?
2074 2073 */
2075 2074
2076 2075 task->task_flags =
2077 2076 (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
2078 2077 ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
2079 2078 ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
2080 2079
2081 2080 switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
2082 2081 case ISCSI_ATTR_UNTAGGED:
2083 2082 break;
2084 2083 case ISCSI_ATTR_SIMPLE:
2085 2084 task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
2086 2085 break;
2087 2086 case ISCSI_ATTR_ORDERED:
2088 2087 task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
2089 2088 break;
2090 2089 case ISCSI_ATTR_HEAD_OF_QUEUE:
2091 2090 task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
2092 2091 break;
2093 2092 case ISCSI_ATTR_ACA:
2094 2093 task->task_additional_flags |= TF_ATTR_ACA;
2095 2094 break;
2096 2095 default:
2097 2096 /* Protocol error but just take it, treat as untagged */
2098 2097 break;
2099 2098 }
2100 2099
2101 2100
2102 2101 task->task_additional_flags = 0;
2103 2102 task->task_priority = 0;
2104 2103 task->task_mgmt_function = TM_NONE;
2105 2104
2106 2105 /*
2107 2106 * This "task_max_nbufs" doesn't map well to BIDI. We probably need
2108 2107 * parameter for each direction. "MaxOutstandingR2T" may very well
2109 2108 * be set to one which could prevent us from doing simultaneous
2110 2109 * transfers in each direction.
2111 2110 */
2112 2111 task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
2113 2112 ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
2114 2113 task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
2115 2114 task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
2116 2115
2117 2116 /* Copy CDB */
2118 2117 bcopy(iscsi_scsi->scb, task->task_cdb, 16);
2119 2118 if (addl_cdb_len > 0) {
2120 2119 bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
2121 2120 }
2122 2121
2123 2122 DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
2124 2123 iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
2125 2124 scsi_task_t *, task);
2126 2125
2127 2126 /*
2128 2127 * Copy the transport header into the task handle from the PDU
2129 2128 * handle. The transport header describes this task's remote tagged
2130 2129 * buffer.
2131 2130 */
2132 2131 if (rx_pdu->isp_transport_hdrlen != 0) {
2133 2132 bcopy(rx_pdu->isp_transport_hdr,
2134 2133 itask->it_idm_task->idt_transport_hdr,
2135 2134 rx_pdu->isp_transport_hdrlen);
2136 2135 }
2137 2136
2138 2137 /*
2139 2138 * Tell IDM about our new active task
2140 2139 */
2141 2140 idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
2142 2141
2143 2142 /*
2144 2143 * If we have any immediate data then setup the immediate buffer
2145 2144 * context that comes with the task
2146 2145 */
2147 2146 if (rx_pdu->isp_datalen) {
2148 2147 ibuf = itask->it_immed_data;
2149 2148 ibuf->ibuf_immed_data_pdu = rx_pdu;
2150 2149 ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
2151 2150 ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
2152 2151 ibuf->ibuf_stmf_buf->db_relative_offset = 0;
2153 2152 ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
2154 2153 rx_pdu->isp_datalen;
2155 2154 ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
2156 2155
2157 2156 DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
2158 2157 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2159 2158 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2160 2159 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2161 2160 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2162 2161
2163 2162 /*
2164 2163 * For immediate data transfer, there is no callback from
2165 2164 * stmf to indicate that the initial burst of data is
2166 2165 * transferred successfully. In some cases, the task can
2167 2166 * get freed before execution returns from stmf_post_task.
2168 2167 * Although this xfer-start/done probe accurately tracks
2169 2168 * the size of the transfer, it does only provide a best
2170 2169 * effort on the timing of the transfer.
2171 2170 */
2172 2171 DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2173 2172 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2174 2173 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2175 2174 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2176 2175 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2177 2176 stmf_post_task(task, ibuf->ibuf_stmf_buf);
2178 2177 } else {
2179 2178
2180 2179 stmf_post_task(task, NULL);
2181 2180 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2182 2181 }
2183 2182 }
2184 2183
2185 2184 void
2186 2185 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2187 2186 {
2188 2187 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2189 2188
2190 2189 /*
2191 2190 * If this isn't a login packet, we need a session. Otherwise
2192 2191 * this is a protocol error (perhaps one IDM should've caught?).
2193 2192 */
2194 2193 if (IDM_PDU_OPCODE(rx_pdu) != ISCSI_OP_LOGIN_CMD &&
2195 2194 ict->ict_sess == NULL) {
2196 2195 DTRACE_PROBE2(iscsi__idm__deferred__no__session,
2197 2196 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
2198 2197 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2199 2198 return;
2200 2199 }
2201 2200
2202 2201 /*
2203 2202 * If the connection has been lost then ignore new PDU's
2204 2203 */
2205 2204 mutex_enter(&ict->ict_mutex);
2206 2205 if (ict->ict_lost) {
2207 2206 mutex_exit(&ict->ict_mutex);
2208 2207 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2209 2208 return;
2210 2209 }
2211 2210
2212 2211 /*
2213 2212 * Grab a hold on the connection to prevent it from going away
2214 2213 * between now and when the taskq function is called.
2215 2214 */
2216 2215 iscsit_conn_dispatch_hold(ict);
2217 2216 mutex_exit(&ict->ict_mutex);
2218 2217
2219 2218 taskq_dispatch_ent(iscsit_global.global_dispatch_taskq,
2220 2219 iscsit_deferred, rx_pdu, 0, &rx_pdu->isp_tqent);
2221 2220 }
2222 2221
2223 2222 static void
2224 2223 iscsit_deferred(void *rx_pdu_void)
2225 2224 {
2226 2225 idm_pdu_t *rx_pdu = rx_pdu_void;
2227 2226 idm_conn_t *ic = rx_pdu->isp_ic;
2228 2227 iscsit_conn_t *ict = ic->ic_handle;
2229 2228
2230 2229 /*
2231 2230 * NOP and Task Management Commands can be marked for immediate
2232 2231 * delivery. Commands marked as 'Immediate' are to be considered
2233 2232 * for execution as soon as they arrive on the target. So these
2234 2233 * should not be checked for sequence order and put in a queue.
2235 2234 * The CmdSN is not advanced for Immediate Commands.
2236 2235 */
2237 2236 switch (IDM_PDU_OPCODE(rx_pdu)) {
2238 2237 case ISCSI_OP_NOOP_OUT:
2239 2238 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2240 2239 iscsit_set_cmdsn(ict, rx_pdu);
2241 2240 iscsit_pdu_op_noop(ict, rx_pdu);
2242 2241 }
2243 2242 break;
2244 2243 case ISCSI_OP_LOGIN_CMD:
2245 2244 iscsit_pdu_op_login_cmd(ict, rx_pdu);
2246 2245 iscsit_conn_dispatch_rele(ict);
2247 2246 return;
2248 2247 case ISCSI_OP_TEXT_CMD:
2249 2248 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2250 2249 iscsit_set_cmdsn(ict, rx_pdu);
2251 2250 iscsit_pdu_op_text_cmd(ict, rx_pdu);
2252 2251 }
2253 2252 break;
2254 2253 case ISCSI_OP_LOGOUT_CMD:
2255 2254 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2256 2255 iscsit_set_cmdsn(ict, rx_pdu);
2257 2256 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2258 2257 }
2259 2258 break;
2260 2259 default:
2261 2260 /* Protocol error. IDM should have caught this */
2262 2261 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2263 2262 ASSERT(0);
2264 2263 break;
2265 2264 }
2266 2265 /*
2267 2266 * Check if there are other PDUs in the session staging queue
2268 2267 * waiting to be posted to SCSI layer.
2269 2268 */
2270 2269 iscsit_process_pdu_in_queue(ict->ict_sess);
2271 2270
2272 2271 iscsit_conn_dispatch_rele(ict);
2273 2272 }
2274 2273
2275 2274 static void
2276 2275 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2277 2276 uint8_t response, uint8_t cmd_status)
2278 2277 {
2279 2278 idm_pdu_t *rsp_pdu;
2280 2279 idm_conn_t *ic;
2281 2280 iscsi_scsi_rsp_hdr_t *resp;
2282 2281 iscsi_scsi_cmd_hdr_t *req =
2283 2282 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2284 2283
2285 2284 ic = ict->ict_ic;
2286 2285
2287 2286 rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2288 2287 idm_pdu_init(rsp_pdu, ic, NULL, NULL);
2289 2288 /*
2290 2289 * StatSN is incremented by 1 for every response sent on
2291 2290 * a connection except for responses sent as a result of
2292 2291 * a retry or SNACK
2293 2292 */
2294 2293 rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2295 2294
2296 2295 resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2297 2296
2298 2297 resp->opcode = ISCSI_OP_SCSI_RSP;
2299 2298 resp->flags = ISCSI_FLAG_FINAL;
2300 2299 resp->response = response;
2301 2300 resp->cmd_status = cmd_status;
2302 2301 resp->itt = req->itt;
2303 2302 if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
2304 2303 (req->data_length != 0) &&
2305 2304 ((req->flags & ISCSI_FLAG_CMD_READ) ||
2306 2305 (req->flags & ISCSI_FLAG_CMD_WRITE))) {
2307 2306 resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
2308 2307 resp->residual_count = req->data_length;
2309 2308 }
2310 2309
2311 2310 DTRACE_PROBE4(iscsi__scsi__direct__response,
2312 2311 iscsit_conn_t *, ict,
2313 2312 uint8_t, resp->response,
2314 2313 uint8_t, resp->cmd_status,
2315 2314 idm_pdu_t *, rsp_pdu);
2316 2315
2317 2316 iscsit_pdu_tx(rsp_pdu);
2318 2317 }
2319 2318
2320 2319 void
2321 2320 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2322 2321 {
2323 2322 iscsi_scsi_task_mgt_rsp_hdr_t *tm_resp;
2324 2323
2325 2324 /*
2326 2325 * The target must take note of the last-sent StatSN.
2327 2326 * The StatSN is to be incremented after sending a
2328 2327 * task management response. Digest recovery can only
2329 2328 * work if StatSN is incremented.
2330 2329 */
2331 2330 tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2332 2331 tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2333 2332 tm_resp->response = tm_status;
2334 2333
2335 2334 DTRACE_PROBE3(iscsi__scsi__tm__response,
2336 2335 iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
2337 2336 uint8_t, tm_resp->response,
2338 2337 idm_pdu_t *, tm_resp_pdu);
2339 2338 iscsit_pdu_tx(tm_resp_pdu);
2340 2339 }
2341 2340
2342 2341 void
2343 2342 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2344 2343 {
2345 2344 idm_pdu_t *tm_resp_pdu;
2346 2345 iscsit_task_t *itask;
2347 2346 iscsit_task_t *tm_itask;
2348 2347 scsi_task_t *task;
2349 2348 iscsi_scsi_task_mgt_hdr_t *iscsi_tm =
2350 2349 (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2351 2350 iscsi_scsi_task_mgt_rsp_hdr_t *iscsi_tm_rsp =
2352 2351 (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2353 2352 uint32_t rtt, cmdsn, refcmdsn;
2354 2353 uint8_t tm_func;
2355 2354
2356 2355 /*
2357 2356 * Setup response PDU (response field will get filled in later)
2358 2357 */
2359 2358 tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2360 2359 if (tm_resp_pdu == NULL) {
2361 2360 /* Can't respond, just drop it */
2362 2361 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2363 2362 return;
2364 2363 }
2365 2364 idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2366 2365 iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2367 2366 bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2368 2367 iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2369 2368 iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2370 2369 iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2371 2370
2372 2371 /*
2373 2372 * Figure out what we're being asked to do.
2374 2373 */
2375 2374 DTRACE_PROBE4(iscsi__scsi__tm__request,
2376 2375 iscsit_conn_t *, ict,
2377 2376 uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
2378 2377 uint32_t, iscsi_tm->rtt,
2379 2378 idm_pdu_t *, rx_pdu);
2380 2379 switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2381 2380 case ISCSI_TM_FUNC_ABORT_TASK:
2382 2381 /*
2383 2382 * STMF doesn't currently support the "abort task" task
2384 2383 * management command although it does support aborting
2385 2384 * an individual task. We'll get STMF to abort the task
2386 2385 * for us but handle the details of the task management
2387 2386 * command ourselves.
2388 2387 *
2389 2388 * Find the task associated with the referenced task tag.
2390 2389 */
2391 2390 rtt = iscsi_tm->rtt;
2392 2391 itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2393 2392 (uintptr_t)rtt);
2394 2393
2395 2394 if (itask == NULL) {
2396 2395 cmdsn = ntohl(iscsi_tm->cmdsn);
2397 2396 refcmdsn = ntohl(iscsi_tm->refcmdsn);
2398 2397
2399 2398 /*
2400 2399 * Task was not found. But the SCSI command could be
2401 2400 * on the rxpdu wait queue. If RefCmdSN is within
2402 2401 * the CmdSN window and less than CmdSN of the TM
2403 2402 * function, return "Function Complete". Otherwise,
2404 2403 * return "Task Does Not Exist".
2405 2404 */
2406 2405
2407 2406 if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2408 2407 iscsit_sna_lt(refcmdsn, cmdsn)) {
2409 2408 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2410 2409 if (iscsit_remove_pdu_from_queue(
2411 2410 ict->ict_sess, refcmdsn)) {
2412 2411 iscsit_conn_dispatch_rele(ict);
2413 2412 }
2414 2413 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2415 2414 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2416 2415 SCSI_TCP_TM_RESP_COMPLETE);
2417 2416 } else {
2418 2417 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2419 2418 SCSI_TCP_TM_RESP_NO_TASK);
2420 2419 }
2421 2420 } else {
2422 2421
2423 2422 /*
2424 2423 * Tell STMF to abort the task. This will do no harm
2425 2424 * if the task is already complete.
2426 2425 */
2427 2426 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2428 2427 STMF_ABORTED, NULL);
2429 2428
2430 2429 /*
2431 2430 * Make sure the task hasn't already completed
2432 2431 */
2433 2432 mutex_enter(&itask->it_idm_task->idt_mutex);
2434 2433 if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2435 2434 (itask->it_idm_task->idt_state == TASK_IDLE)) {
2436 2435 /*
2437 2436 * Task is complete, return "Task Does Not
2438 2437 * Exist"
2439 2438 */
2440 2439 mutex_exit(&itask->it_idm_task->idt_mutex);
2441 2440 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2442 2441 SCSI_TCP_TM_RESP_NO_TASK);
2443 2442 } else {
2444 2443 /*
2445 2444 * STMF is now aborting the task, return
2446 2445 * "Function Complete"
2447 2446 */
2448 2447 mutex_exit(&itask->it_idm_task->idt_mutex);
2449 2448 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2450 2449 SCSI_TCP_TM_RESP_COMPLETE);
2451 2450 }
2452 2451 idm_task_rele(itask->it_idm_task);
2453 2452 }
2454 2453 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2455 2454 return;
2456 2455
2457 2456 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2458 2457 tm_func = TM_ABORT_TASK_SET;
2459 2458 break;
2460 2459
2461 2460 case ISCSI_TM_FUNC_CLEAR_ACA:
2462 2461 tm_func = TM_CLEAR_ACA;
2463 2462 break;
2464 2463
2465 2464 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2466 2465 tm_func = TM_CLEAR_TASK_SET;
2467 2466 break;
2468 2467
2469 2468 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2470 2469 tm_func = TM_LUN_RESET;
2471 2470 break;
2472 2471
2473 2472 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2474 2473 tm_func = TM_TARGET_WARM_RESET;
2475 2474 break;
2476 2475
2477 2476 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2478 2477 tm_func = TM_TARGET_COLD_RESET;
2479 2478 break;
2480 2479
2481 2480 case ISCSI_TM_FUNC_TASK_REASSIGN:
2482 2481 /*
2483 2482 * We do not currently support allegiance reassignment. When
2484 2483 * we start supporting ERL1+, we will need to.
2485 2484 */
2486 2485 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2487 2486 SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2488 2487 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2489 2488 return;
2490 2489
2491 2490 default:
2492 2491 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2493 2492 SCSI_TCP_TM_RESP_REJECTED);
2494 2493 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2495 2494 return;
2496 2495 }
2497 2496
2498 2497 tm_itask = iscsit_tm_task_alloc(ict);
2499 2498 if (tm_itask == NULL) {
2500 2499 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2501 2500 SCSI_TCP_TM_RESP_REJECTED);
2502 2501 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2503 2502 return;
2504 2503 }
2505 2504
2506 2505
2507 2506 task = stmf_task_alloc(ict->ict_sess->ist_lport,
2508 2507 ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2509 2508 0, STMF_TASK_EXT_NONE);
2510 2509 if (task == NULL) {
2511 2510 /*
2512 2511 * If this happens, either the LU is in reset, couldn't
2513 2512 * get memory, or some other condition in which we simply
2514 2513 * can't complete this request. It would be nice to return
2515 2514 * an error code like "busy" but the closest we have is
2516 2515 * "rejected".
2517 2516 */
2518 2517 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2519 2518 SCSI_TCP_TM_RESP_REJECTED);
2520 2519 iscsit_tm_task_free(tm_itask);
2521 2520 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2522 2521 return;
2523 2522 }
2524 2523
2525 2524 tm_itask->it_tm_pdu = tm_resp_pdu;
2526 2525 tm_itask->it_stmf_task = task;
2527 2526 task->task_port_private = tm_itask;
2528 2527 task->task_mgmt_function = tm_func;
2529 2528 task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2530 2529 task->task_priority = 0;
2531 2530 task->task_max_nbufs = STMF_BUFS_MAX;
2532 2531 task->task_cmd_seq_no = iscsi_tm->itt;
2533 2532 task->task_expected_xfer_length = 0;
2534 2533
2535 2534 stmf_post_task(task, NULL);
2536 2535 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2537 2536 }
2538 2537
2539 2538 static void
2540 2539 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2541 2540 {
2542 2541 iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2543 2542 iscsi_nop_in_hdr_t *in;
2544 2543 int resp_datalen;
2545 2544 idm_pdu_t *resp;
2546 2545
2547 2546 /* Ignore the response from initiator */
2548 2547 if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
2549 2548 (out->ttt != ISCSI_RSVD_TASK_TAG)) {
2550 2549 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2551 2550 return;
2552 2551 }
2553 2552
2554 2553 /* Allocate a PDU to respond */
2555 2554 resp_datalen = ntoh24(out->dlength);
2556 2555 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2557 2556 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2558 2557 if (resp_datalen > 0) {
2559 2558 bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2560 2559 }
2561 2560
2562 2561 /*
2563 2562 * When sending a NOP-In as a response to a NOP-Out from the initiator,
2564 2563 * the target must respond with the same initiator task tag that was
2565 2564 * provided in the NOP-Out request, the target transfer tag must be
2566 2565 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
2567 2566 * status sequence number. The StatSN for the connection is advanced
2568 2567 * after this PDU is sent.
2569 2568 */
2570 2569 in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2571 2570 bzero(in, sizeof (*in));
2572 2571 in->opcode = ISCSI_OP_NOOP_IN;
2573 2572 in->flags = ISCSI_FLAG_FINAL;
2574 2573 bcopy(out->lun, in->lun, 8);
2575 2574 in->itt = out->itt;
2576 2575 in->ttt = ISCSI_RSVD_TASK_TAG;
2577 2576 hton24(in->dlength, resp_datalen);
2578 2577 resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2579 2578 /* Any other field in resp to be set? */
2580 2579 iscsit_pdu_tx(resp);
2581 2580 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2582 2581 }
2583 2582
2584 2583 static void
2585 2584 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2586 2585 {
2587 2586
2588 2587 /*
2589 2588 * Submit PDU to login state machine. State machine will free the
2590 2589 * PDU.
2591 2590 */
2592 2591 iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2593 2592 }
2594 2593
2595 2594 void
2596 2595 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2597 2596 {
2598 2597 iscsi_logout_hdr_t *logout_req =
2599 2598 (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2600 2599 iscsi_logout_rsp_hdr_t *logout_rsp;
2601 2600 idm_pdu_t *resp;
2602 2601
2603 2602 /* Allocate a PDU to respond */
2604 2603 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2605 2604 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2606 2605 /*
2607 2606 * The StatSN is to be sent to the initiator,
2608 2607 * it is not required to increment the number
2609 2608 * as the connection is terminating.
2610 2609 */
2611 2610 resp->isp_flags |= IDM_PDU_SET_STATSN;
2612 2611 /*
2613 2612 * Logout results in the immediate termination of all tasks except
2614 2613 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY. The
2615 2614 * connection state machine will drive this task cleanup automatically
2616 2615 * so we don't need to handle that here.
2617 2616 */
2618 2617 logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2619 2618 bzero(logout_rsp, sizeof (*logout_rsp));
2620 2619 logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2621 2620 logout_rsp->flags = ISCSI_FLAG_FINAL;
2622 2621 logout_rsp->itt = logout_req->itt;
2623 2622 if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2624 2623 ISCSI_LOGOUT_REASON_RECOVERY) {
2625 2624 logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2626 2625 } else {
2627 2626 logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2628 2627 }
2629 2628
2630 2629 iscsit_pdu_tx(resp);
2631 2630 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2632 2631 }
2633 2632
2634 2633 /*
2635 2634 * Calculate the number of outstanding commands we can process
2636 2635 */
2637 2636 int
2638 2637 iscsit_cmd_window()
2639 2638 {
2640 2639 /*
2641 2640 * Instead of using a pre-defined constant for the command window,
2642 2641 * it should be made confiurable and dynamic. With MC/S, sequence
2643 2642 * numbers will be used up at a much faster rate than with SC/S.
2644 2643 */
2645 2644 return (ISCSIT_MAX_WINDOW);
2646 2645 }
2647 2646
2648 2647 /*
2649 2648 * Set local registers based on incoming PDU
2650 2649 */
2651 2650 void
2652 2651 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2653 2652 {
2654 2653 iscsit_sess_t *ist;
2655 2654 iscsi_scsi_cmd_hdr_t *req;
2656 2655
2657 2656 ist = ict->ict_sess;
2658 2657
2659 2658 req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2660 2659 if (req->opcode & ISCSI_OP_IMMEDIATE) {
2661 2660 /* no cmdsn increment for immediate PDUs */
2662 2661 return;
2663 2662 }
2664 2663
2665 2664 /* Ensure that the ExpCmdSN advances in an orderly manner */
2666 2665 mutex_enter(&ist->ist_sn_mutex);
2667 2666 ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2668 2667 ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2669 2668 mutex_exit(&ist->ist_sn_mutex);
2670 2669 }
2671 2670
2672 2671 /*
2673 2672 * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2674 2673 */
2675 2674 void
2676 2675 iscsit_pdu_tx(idm_pdu_t *pdu)
2677 2676 {
2678 2677 iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
2679 2678 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2680 2679 iscsit_sess_t *ist = ict->ict_sess;
2681 2680
2682 2681 /*
2683 2682 * The command sequence numbers are session-wide and must stay
2684 2683 * consistent across the transfer, so protect the cmdsn with a
2685 2684 * mutex lock on the session. The status sequence number will
2686 2685 * be updated just before the transport layer transmits the PDU.
2687 2686 */
2688 2687
2689 2688 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2690 2689 /* Set ExpCmdSN and MaxCmdSN */
2691 2690 rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
2692 2691 rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2693 2692 idm_pdu_tx(pdu);
2694 2693 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2695 2694 }
2696 2695
2697 2696 /*
2698 2697 * Internal functions
2699 2698 */
2700 2699
2701 2700 void
2702 2701 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2703 2702 {
2704 2703 idm_pdu_t *abt;
2705 2704 iscsi_async_evt_hdr_t *async_abt;
2706 2705
2707 2706 /*
2708 2707 * Get a PDU to build the abort request.
2709 2708 */
2710 2709 abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2711 2710 if (abt == NULL) {
2712 2711 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2713 2712 return;
2714 2713 }
2715 2714
2716 2715 /*
2717 2716 * A asynchronous message is sent by the target to request a logout.
2718 2717 * The StatSN for the connection is advanced after the PDU is sent
2719 2718 * to allow for initiator and target state synchronization.
2720 2719 */
2721 2720 idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2722 2721 abt->isp_datalen = 0;
2723 2722 abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2724 2723
2725 2724 async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2726 2725 bzero(async_abt, sizeof (*async_abt));
2727 2726 async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2728 2727 async_abt->async_event = event;
2729 2728 async_abt->flags = ISCSI_FLAG_FINAL;
2730 2729 async_abt->rsvd4[0] = 0xff;
2731 2730 async_abt->rsvd4[1] = 0xff;
2732 2731 async_abt->rsvd4[2] = 0xff;
2733 2732 async_abt->rsvd4[3] = 0xff;
2734 2733
2735 2734 switch (event) {
2736 2735 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2737 2736 async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2738 2737 break;
2739 2738 case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2740 2739 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2741 2740 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2742 2741 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2743 2742 default:
2744 2743 ASSERT(0);
2745 2744 }
2746 2745
2747 2746 iscsit_pdu_tx(abt);
2748 2747 }
2749 2748
2750 2749 void
2751 2750 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
2752 2751 {
2753 2752 idm_pdu_t *reject_pdu;
2754 2753 iscsi_reject_rsp_hdr_t *reject;
2755 2754
2756 2755 /*
2757 2756 * Get a PDU to build the abort request.
2758 2757 */
2759 2758 reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
2760 2759 rejected_pdu->isp_hdrlen);
2761 2760 if (reject_pdu == NULL) {
2762 2761 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2763 2762 return;
2764 2763 }
2765 2764 idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
2766 2765 /* StatSN is advanced after a Reject PDU */
2767 2766 reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2768 2767 reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
2769 2768 bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
2770 2769 rejected_pdu->isp_hdrlen);
2771 2770
2772 2771 reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
2773 2772 bzero(reject, sizeof (*reject));
2774 2773 reject->opcode = ISCSI_OP_REJECT_MSG;
2775 2774 reject->reason = reason;
2776 2775 reject->flags = ISCSI_FLAG_FINAL;
2777 2776 hton24(reject->dlength, rejected_pdu->isp_hdrlen);
2778 2777 reject->must_be_ff[0] = 0xff;
2779 2778 reject->must_be_ff[1] = 0xff;
2780 2779 reject->must_be_ff[2] = 0xff;
2781 2780 reject->must_be_ff[3] = 0xff;
2782 2781
2783 2782 iscsit_pdu_tx(reject_pdu);
2784 2783 }
2785 2784
2786 2785
2787 2786 static iscsit_task_t *
2788 2787 iscsit_task_alloc(iscsit_conn_t *ict)
2789 2788 {
2790 2789 iscsit_task_t *itask;
2791 2790 iscsit_buf_t *immed_ibuf;
2792 2791
2793 2792 /*
2794 2793 * Possible items to pre-alloc if we cache iscsit_task_t's:
2795 2794 *
2796 2795 * Status PDU w/ sense buffer
2797 2796 * stmf_data_buf_t for immediate data
2798 2797 */
2799 2798 itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2800 2799 sizeof (stmf_data_buf_t), KM_NOSLEEP);
2801 2800 if (itask != NULL) {
2802 2801 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2803 2802 itask->it_aborted = itask->it_stmf_abort =
2804 2803 itask->it_tm_task = 0;
2805 2804
2806 2805 immed_ibuf = (iscsit_buf_t *)(itask + 1);
2807 2806 bzero(immed_ibuf, sizeof (*immed_ibuf));
2808 2807 immed_ibuf->ibuf_is_immed = B_TRUE;
2809 2808 immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2810 2809
2811 2810 bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2812 2811 immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2813 2812 immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2814 2813 immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2815 2814 DB_DONT_CACHE;
2816 2815 itask->it_immed_data = immed_ibuf;
2817 2816 itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2818 2817 if (itask->it_idm_task != NULL) {
2819 2818 itask->it_idm_task->idt_private = itask;
2820 2819 itask->it_ict = ict;
2821 2820 itask->it_ttt = itask->it_idm_task->idt_tt;
2822 2821 return (itask);
2823 2822 } else {
2824 2823 kmem_free(itask, sizeof (iscsit_task_t) +
2825 2824 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2826 2825 }
2827 2826 }
2828 2827
2829 2828 return (NULL);
2830 2829 }
2831 2830
2832 2831 static void
2833 2832 iscsit_task_free(iscsit_task_t *itask)
2834 2833 {
2835 2834 idm_task_free(itask->it_idm_task);
2836 2835 mutex_destroy(&itask->it_mutex);
2837 2836 kmem_free(itask, sizeof (iscsit_task_t) +
2838 2837 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2839 2838 }
2840 2839
2841 2840 static iscsit_task_t *
2842 2841 iscsit_tm_task_alloc(iscsit_conn_t *ict)
2843 2842 {
2844 2843 iscsit_task_t *itask;
2845 2844
2846 2845 itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2847 2846 if (itask != NULL) {
2848 2847 idm_conn_hold(ict->ict_ic);
2849 2848 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2850 2849 itask->it_aborted = itask->it_stmf_abort =
2851 2850 itask->it_tm_responded = 0;
2852 2851 itask->it_tm_pdu = NULL;
2853 2852 itask->it_tm_task = 1;
2854 2853 itask->it_ict = ict;
2855 2854 }
2856 2855
2857 2856 return (itask);
2858 2857 }
2859 2858
2860 2859 static void
2861 2860 iscsit_tm_task_free(iscsit_task_t *itask)
2862 2861 {
2863 2862 /*
2864 2863 * If we responded then the call to idm_pdu_complete will free the
2865 2864 * PDU. Otherwise we got aborted before the TM function could
2866 2865 * complete and we need to free the PDU explicitly.
2867 2866 */
2868 2867 if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2869 2868 idm_pdu_free(itask->it_tm_pdu);
2870 2869 idm_conn_rele(itask->it_ict->ict_ic);
2871 2870 mutex_destroy(&itask->it_mutex);
2872 2871 kmem_free(itask, sizeof (iscsit_task_t));
2873 2872 }
2874 2873
2875 2874 static idm_status_t
2876 2875 iscsit_task_start(iscsit_task_t *itask)
2877 2876 {
2878 2877 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2879 2878 avl_index_t where;
2880 2879
2881 2880 /*
2882 2881 * Sanity check the ITT and ensure that this task does not already
2883 2882 * exist. If not then add the task to the session task list.
2884 2883 */
2885 2884 mutex_enter(&ist->ist_mutex);
2886 2885 mutex_enter(&itask->it_mutex);
2887 2886 itask->it_active = 1;
2888 2887 if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
2889 2888 /* New task, add to AVL */
2890 2889 avl_insert(&ist->ist_task_list, itask, where);
2891 2890 mutex_exit(&itask->it_mutex);
2892 2891 mutex_exit(&ist->ist_mutex);
2893 2892 return (IDM_STATUS_SUCCESS);
2894 2893 }
2895 2894 mutex_exit(&itask->it_mutex);
2896 2895 mutex_exit(&ist->ist_mutex);
2897 2896
2898 2897 return (IDM_STATUS_REJECT);
2899 2898 }
2900 2899
2901 2900 static void
2902 2901 iscsit_task_done(iscsit_task_t *itask)
2903 2902 {
2904 2903 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2905 2904
2906 2905 mutex_enter(&ist->ist_mutex);
2907 2906 mutex_enter(&itask->it_mutex);
2908 2907 if (itask->it_active) {
2909 2908 avl_remove(&ist->ist_task_list, itask);
2910 2909 itask->it_active = 0;
2911 2910 }
2912 2911 mutex_exit(&itask->it_mutex);
2913 2912 mutex_exit(&ist->ist_mutex);
2914 2913 }
2915 2914
2916 2915 /*
2917 2916 * iscsit status PDU cache
2918 2917 */
2919 2918
2920 2919 /*ARGSUSED*/
2921 2920 static int
2922 2921 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2923 2922 {
2924 2923 idm_pdu_t *pdu = pdu_void;
2925 2924 iscsi_scsi_rsp_hdr_t *rsp;
2926 2925
2927 2926 bzero(pdu, sizeof (idm_pdu_t));
2928 2927 pdu->isp_callback = iscsit_send_good_status_done;
2929 2928 pdu->isp_magic = IDM_PDU_MAGIC;
2930 2929 pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2931 2930 pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2932 2931
2933 2932 /* Setup status response */
2934 2933 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2935 2934 bzero(rsp, sizeof (*rsp));
2936 2935 rsp->opcode = ISCSI_OP_SCSI_RSP;
2937 2936 rsp->flags = ISCSI_FLAG_FINAL;
2938 2937 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2939 2938
2940 2939 return (0);
2941 2940 }
2942 2941
2943 2942 /*
2944 2943 * iscsit private data handler
2945 2944 */
2946 2945
2947 2946 /*ARGSUSED*/
2948 2947 static void
2949 2948 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2950 2949 {
2951 2950 it_config_t *cfg;
2952 2951 nvlist_t *nvl;
2953 2952 iscsit_service_enabled_t old_state;
2954 2953
2955 2954 if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2956 2955 return;
2957 2956 }
2958 2957
2959 2958 nvl = (nvlist_t *)arg;
2960 2959
2961 2960 /* Translate nvlist */
2962 2961 if (it_nv_to_config(nvl, &cfg) != 0) {
2963 2962 cmn_err(CE_WARN, "Configuration is invalid");
2964 2963 return;
2965 2964 }
2966 2965
2967 2966 /* Check that no iSCSI ioctl is currently running */
2968 2967 mutex_enter(&iscsit_global.global_state_mutex);
2969 2968 old_state = iscsit_global.global_svc_state;
2970 2969 switch (iscsit_global.global_svc_state) {
2971 2970 case ISE_ENABLED:
2972 2971 case ISE_DISABLED:
2973 2972 iscsit_global.global_svc_state = ISE_BUSY;
2974 2973 break;
2975 2974 case ISE_ENABLING:
2976 2975 /*
2977 2976 * It is OK for the iscsit_pp_cb to be called from inside of
2978 2977 * an iSCSI ioctl only if we are currently executing inside
2979 2978 * of stmf_register_port_provider.
2980 2979 */
2981 2980 ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0);
2982 2981 break;
2983 2982 default:
2984 2983 cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state"
2985 2984 " is not ENABLED(0x%x) -- ignoring",
2986 2985 iscsit_global.global_svc_state);
2987 2986 mutex_exit(&iscsit_global.global_state_mutex);
2988 2987 it_config_free_cmn(cfg);
2989 2988 return;
2990 2989 }
2991 2990 mutex_exit(&iscsit_global.global_state_mutex);
2992 2991
2993 2992 /* Update config */
2994 2993 (void) iscsit_config_merge(cfg);
2995 2994
2996 2995 it_config_free_cmn(cfg);
2997 2996
2998 2997 /* Restore old iSCSI driver global state */
2999 2998 mutex_enter(&iscsit_global.global_state_mutex);
3000 2999 ASSERT(iscsit_global.global_svc_state == ISE_BUSY ||
3001 3000 iscsit_global.global_svc_state == ISE_ENABLING);
3002 3001 iscsit_global.global_svc_state = old_state;
3003 3002 mutex_exit(&iscsit_global.global_state_mutex);
3004 3003 }
3005 3004
3006 3005
3007 3006 static it_cfg_status_t
3008 3007 iscsit_config_merge(it_config_t *in_cfg)
3009 3008 {
3010 3009 it_cfg_status_t status;
3011 3010 it_config_t *cfg;
3012 3011 it_config_t tmp_cfg;
3013 3012 list_t tpg_del_list;
3014 3013
3015 3014 if (in_cfg) {
3016 3015 cfg = in_cfg;
3017 3016 } else {
3018 3017 /* Make empty config */
3019 3018 bzero(&tmp_cfg, sizeof (tmp_cfg));
3020 3019 cfg = &tmp_cfg;
3021 3020 }
3022 3021
3023 3022 list_create(&tpg_del_list, sizeof (iscsit_tpg_t),
3024 3023 offsetof(iscsit_tpg_t, tpg_delete_ln));
3025 3024
3026 3025 /*
3027 3026 * Update targets, initiator contexts, target portal groups,
3028 3027 * and iSNS client
3029 3028 */
3030 3029 ISCSIT_GLOBAL_LOCK(RW_WRITER);
3031 3030 if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
3032 3031 != 0) ||
3033 3032 ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
3034 3033 ((status = iscsit_config_merge_ini(cfg)) != 0) ||
3035 3034 ((status = isnst_config_merge(cfg)) != 0)) {
3036 3035 ISCSIT_GLOBAL_UNLOCK();
3037 3036 return (status);
3038 3037 }
3039 3038
3040 3039 /* Update other global config parameters */
3041 3040 if (iscsit_global.global_props) {
3042 3041 nvlist_free(iscsit_global.global_props);
3043 3042 iscsit_global.global_props = NULL;
3044 3043 }
3045 3044 if (in_cfg) {
3046 3045 (void) nvlist_dup(cfg->config_global_properties,
3047 3046 &iscsit_global.global_props, KM_SLEEP);
3048 3047 }
3049 3048 ISCSIT_GLOBAL_UNLOCK();
3050 3049
3051 3050 iscsit_config_destroy_tpgs(&tpg_del_list);
3052 3051
3053 3052 list_destroy(&tpg_del_list);
3054 3053
3055 3054 return (ITCFG_SUCCESS);
3056 3055 }
3057 3056
3058 3057 /*
3059 3058 * iscsit_sna_lt[e]
3060 3059 *
3061 3060 * Compare serial numbers using serial number arithmetic as defined in
3062 3061 * RFC 1982.
3063 3062 *
3064 3063 * NOTE: This code is duplicated in the isns server. It ought to be common.
3065 3064 */
3066 3065
3067 3066 static int
3068 3067 iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
3069 3068 {
3070 3069 return ((sn1 != sn2) &&
3071 3070 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3072 3071 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3073 3072 }
3074 3073
3075 3074 static int
3076 3075 iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
3077 3076 {
3078 3077 return ((sn1 == sn2) ||
3079 3078 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3080 3079 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3081 3080 }
3082 3081
3083 3082
3084 3083 static boolean_t
3085 3084 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
3086 3085 {
3087 3086 iscsit_sess_t *ist = ict->ict_sess;
3088 3087 int rval = B_TRUE;
3089 3088
3090 3089 ist = ict->ict_sess;
3091 3090
3092 3091 mutex_enter(&ist->ist_sn_mutex);
3093 3092
3094 3093 /*
3095 3094 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
3096 3095 * greater than ist_expcmdsn, it's not in the window.
3097 3096 */
3098 3097
3099 3098 if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
3100 3099 !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
3101 3100 rval = B_FALSE;
3102 3101 }
3103 3102
3104 3103 mutex_exit(&ist->ist_sn_mutex);
3105 3104
3106 3105 return (rval);
3107 3106 }
3108 3107
3109 3108 /*
3110 3109 * iscsit_check_cmdsn_and_queue
3111 3110 *
3112 3111 * Independent of the order in which the iSCSI target receives non-immediate
3113 3112 * command PDU across the entire session and any multiple connections within
3114 3113 * the session, the target must deliver the commands to the SCSI layer in
3115 3114 * CmdSN order. So out-of-order non-immediate commands are queued up on a
3116 3115 * session-wide wait queue. Duplicate commands are ignored.
3117 3116 *
3118 3117 */
3119 3118 static int
3120 3119 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu)
3121 3120 {
3122 3121 idm_conn_t *ic = rx_pdu->isp_ic;
3123 3122 iscsit_conn_t *ict = ic->ic_handle;
3124 3123 iscsit_sess_t *ist = ict->ict_sess;
3125 3124 iscsi_scsi_cmd_hdr_t *hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
3126 3125
3127 3126 mutex_enter(&ist->ist_sn_mutex);
3128 3127 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
3129 3128 /* do not queue, handle it immediately */
3130 3129 DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist,
3131 3130 idm_pdu_t *, rx_pdu);
3132 3131 mutex_exit(&ist->ist_sn_mutex);
3133 3132 return (ISCSIT_CMDSN_EQ_EXPCMDSN);
3134 3133 }
3135 3134 if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn))) {
3136 3135 /*
3137 3136 * Out-of-order commands (cmdSN higher than ExpCmdSN)
3138 3137 * are staged on a fixed-size circular buffer until
3139 3138 * the missing command is delivered to the SCSI layer.
3140 3139 * Irrespective of the order of insertion into the
3141 3140 * staging queue, the commands are processed out of the
3142 3141 * queue in cmdSN order only.
3143 3142 */
3144 3143 rx_pdu->isp_queue_time = gethrtime();
3145 3144 iscsit_add_pdu_to_queue(ist, rx_pdu);
3146 3145 mutex_exit(&ist->ist_sn_mutex);
3147 3146 return (ISCSIT_CMDSN_GT_EXPCMDSN);
3148 3147 } else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn)) {
3149 3148 DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist,
3150 3149 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
3151 3150 mutex_exit(&ist->ist_sn_mutex);
3152 3151 return (ISCSIT_CMDSN_LT_EXPCMDSN);
3153 3152 } else {
3154 3153 mutex_exit(&ist->ist_sn_mutex);
3155 3154 return (ISCSIT_CMDSN_EQ_EXPCMDSN);
3156 3155 }
3157 3156 }
3158 3157
3159 3158 /*
3160 3159 * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by
3161 3160 * their cmdsn value. The length of the array is kept above the
3162 3161 * maximum window size. The window keeps the cmdsn within a range
3163 3162 * such that there are no collisons. e.g. the assumption is that
3164 3163 * the windowing checks make it impossible to receive PDUs that
3165 3164 * index into the same location in the array.
3166 3165 */
3167 3166 static void
3168 3167 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu)
3169 3168 {
3170 3169 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3171 3170 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3172 3171 uint32_t cmdsn =
3173 3172 ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn;
3174 3173 uint32_t index;
3175 3174
3176 3175 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3177 3176 /*
3178 3177 * If the connection is being torn down, then
3179 3178 * don't add the PDU to the staging queue
3180 3179 */
3181 3180 mutex_enter(&ict->ict_mutex);
3182 3181 if (ict->ict_lost) {
3183 3182 mutex_exit(&ict->ict_mutex);
3184 3183 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3185 3184 return;
3186 3185 }
3187 3186 iscsit_conn_dispatch_hold(ict);
3188 3187 mutex_exit(&ict->ict_mutex);
3189 3188
3190 3189 index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN;
3191 3190 /*
3192 3191 * In the normal case, assuming that the Initiator is not
3193 3192 * buggy and that we don't have packet duplication occuring,
3194 3193 * the entry in the array will be NULL. However, we may have
3195 3194 * received a duplicate PDU with cmdsn > expsn , and in that
3196 3195 * case we just ignore this PDU -- the previously received one
3197 3196 * remains queued for processing. We need to be careful not
3198 3197 * to leak this one however.
3199 3198 */
3200 3199 if (cbuf->cb_buffer[index] != NULL) {
3201 3200 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3202 3201 } else {
3203 3202 cbuf->cb_buffer[index] = rx_pdu;
3204 3203 cbuf->cb_num_elems++;
3205 3204 }
3206 3205 }
3207 3206
3208 3207 static idm_pdu_t *
3209 3208 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn)
3210 3209 {
3211 3210 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3212 3211 idm_pdu_t *pdu = NULL;
3213 3212 uint32_t index;
3214 3213
3215 3214 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3216 3215 index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3217 3216 if ((pdu = cbuf->cb_buffer[index]) != NULL) {
3218 3217 ASSERT(cmdsn ==
3219 3218 ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn));
3220 3219 cbuf->cb_buffer[index] = NULL;
3221 3220 cbuf->cb_num_elems--;
3222 3221 return (pdu);
3223 3222 }
3224 3223 return (NULL);
3225 3224 }
3226 3225
3227 3226 /*
3228 3227 * iscsit_process_pdu_in_queue() finds the next pdu in sequence
3229 3228 * and posts it to the SCSI layer
3230 3229 */
3231 3230 static void
3232 3231 iscsit_process_pdu_in_queue(iscsit_sess_t *ist)
3233 3232 {
3234 3233 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3235 3234 idm_pdu_t *pdu = NULL;
3236 3235 uint32_t expcmdsn;
3237 3236
3238 3237 for (;;) {
3239 3238 mutex_enter(&ist->ist_sn_mutex);
3240 3239 if (cbuf->cb_num_elems == 0) {
3241 3240 mutex_exit(&ist->ist_sn_mutex);
3242 3241 break;
3243 3242 }
3244 3243 expcmdsn = ist->ist_expcmdsn;
3245 3244 if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn))
3246 3245 == NULL) {
3247 3246 mutex_exit(&ist->ist_sn_mutex);
3248 3247 break;
3249 3248 }
3250 3249 mutex_exit(&ist->ist_sn_mutex);
3251 3250 iscsit_post_staged_pdu(pdu);
3252 3251 }
3253 3252 }
3254 3253
3255 3254 static void
3256 3255 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu)
3257 3256 {
3258 3257 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3259 3258
3260 3259 /* Post the PDU to the SCSI layer */
3261 3260 switch (IDM_PDU_OPCODE(rx_pdu)) {
3262 3261 case ISCSI_OP_NOOP_OUT:
3263 3262 iscsit_set_cmdsn(ict, rx_pdu);
3264 3263 iscsit_pdu_op_noop(ict, rx_pdu);
3265 3264 break;
3266 3265 case ISCSI_OP_TEXT_CMD:
3267 3266 iscsit_set_cmdsn(ict, rx_pdu);
3268 3267 iscsit_pdu_op_text_cmd(ict, rx_pdu);
3269 3268 break;
3270 3269 case ISCSI_OP_SCSI_TASK_MGT_MSG:
3271 3270 iscsit_set_cmdsn(ict, rx_pdu);
3272 3271 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
3273 3272 break;
3274 3273 case ISCSI_OP_SCSI_CMD:
3275 3274 /* cmdSN will be incremented after creating itask */
3276 3275 iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu);
3277 3276 break;
3278 3277 case ISCSI_OP_LOGOUT_CMD:
3279 3278 iscsit_set_cmdsn(ict, rx_pdu);
3280 3279 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
3281 3280 break;
3282 3281 default:
3283 3282 /* No other PDUs should be placed on the queue */
3284 3283 ASSERT(0);
3285 3284 }
3286 3285 iscsit_conn_dispatch_rele(ict); /* release hold on the conn */
3287 3286 }
3288 3287
3289 3288 /* ARGSUSED */
3290 3289 void
3291 3290 iscsit_rxpdu_queue_monitor_start(void)
3292 3291 {
3293 3292 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3294 3293 if (iscsit_rxpdu_queue_monitor_thr_running) {
3295 3294 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3296 3295 return;
3297 3296 }
3298 3297 iscsit_rxpdu_queue_monitor_thr_id =
3299 3298 thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL,
3300 3299 0, &p0, TS_RUN, minclsyspri);
3301 3300 while (!iscsit_rxpdu_queue_monitor_thr_running) {
3302 3301 cv_wait(&iscsit_rxpdu_queue_monitor_cv,
3303 3302 &iscsit_rxpdu_queue_monitor_mutex);
3304 3303 }
3305 3304 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3306 3305
3307 3306 }
3308 3307
3309 3308 /* ARGSUSED */
3310 3309 void
3311 3310 iscsit_rxpdu_queue_monitor_stop(void)
3312 3311 {
3313 3312 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3314 3313 if (iscsit_rxpdu_queue_monitor_thr_running) {
3315 3314 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
3316 3315 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3317 3316 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3318 3317
3319 3318 thread_join(iscsit_rxpdu_queue_monitor_thr_did);
3320 3319 return;
3321 3320 }
3322 3321 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3323 3322 }
3324 3323
3325 3324 /*
3326 3325 * A separate thread is used to scan the staging queue on all the
3327 3326 * sessions, If a delayed PDU does not arrive within a timeout, the
3328 3327 * target will advance to the staged PDU that is next in sequence
3329 3328 * and exceeded the threshold wait time. It is up to the initiator
3330 3329 * to note that the target has not acknowledged a particular cmdsn
3331 3330 * and take appropriate action.
3332 3331 */
3333 3332 /* ARGSUSED */
3334 3333 static void
3335 3334 iscsit_rxpdu_queue_monitor(void *arg)
3336 3335 {
3337 3336 iscsit_tgt_t *tgt;
3338 3337 iscsit_sess_t *ist;
3339 3338
3340 3339 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3341 3340 iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did;
3342 3341 iscsit_rxpdu_queue_monitor_thr_running = B_TRUE;
3343 3342 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3344 3343
3345 3344 while (iscsit_rxpdu_queue_monitor_thr_running) {
3346 3345 ISCSIT_GLOBAL_LOCK(RW_READER);
3347 3346 for (tgt = avl_first(&iscsit_global.global_target_list);
3348 3347 tgt != NULL;
3349 3348 tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) {
3350 3349 mutex_enter(&tgt->target_mutex);
3351 3350 for (ist = avl_first(&tgt->target_sess_list);
3352 3351 ist != NULL;
3353 3352 ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
3354 3353
3355 3354 iscsit_rxpdu_queue_monitor_session(ist);
3356 3355 }
3357 3356 mutex_exit(&tgt->target_mutex);
3358 3357 }
3359 3358 ISCSIT_GLOBAL_UNLOCK();
3360 3359 if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) {
3361 3360 break;
3362 3361 }
3363 3362 (void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv,
3364 3363 &iscsit_rxpdu_queue_monitor_mutex,
3365 3364 ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000),
3366 3365 TR_CLOCK_TICK);
3367 3366 }
3368 3367 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3369 3368 thread_exit();
3370 3369 }
3371 3370
3372 3371 static void
3373 3372 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist)
3374 3373 {
3375 3374 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3376 3375 idm_pdu_t *next_pdu = NULL;
3377 3376 uint32_t index, next_cmdsn, i;
3378 3377
3379 3378 /*
3380 3379 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn.
3381 3380 * Starting with the expcmdsn, iterate over the staged PDUs to find
3382 3381 * the next PDU with a wait time greater than the threshold. If found
3383 3382 * advance the staged PDU to the SCSI layer, skipping over the missing
3384 3383 * PDU(s) to get past the hole in the command sequence. It is up to
3385 3384 * the initiator to note that the target has not acknowledged a cmdsn
3386 3385 * and take appropriate action.
3387 3386 *
3388 3387 * Since the PDU(s) arrive in any random order, it is possible that
3389 3388 * that the actual wait time for a particular PDU is much longer than
3390 3389 * the defined threshold. e.g. Consider a case where commands are sent
3391 3390 * over 4 different connections, and cmdsn = 1004 arrives first, then
3392 3391 * 1003, and 1002 and 1001 are lost due to a connection failure.
3393 3392 * So now 1003 is waiting for 1002 to be delivered, and although the
3394 3393 * wait time of 1004 > wait time of 1003, only 1003 will be considered
3395 3394 * by the monitor thread. 1004 will be automatically processed by
3396 3395 * iscsit_process_pdu_in_queue() once the scan is complete and the
3397 3396 * expcmdsn becomes current.
3398 3397 */
3399 3398 mutex_enter(&ist->ist_sn_mutex);
3400 3399 cbuf = ist->ist_rxpdu_queue;
3401 3400 if (cbuf->cb_num_elems == 0) {
3402 3401 mutex_exit(&ist->ist_sn_mutex);
3403 3402 return;
3404 3403 }
3405 3404 for (next_pdu = NULL, i = 0; ; i++) {
3406 3405 next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */
3407 3406 index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3408 3407 if ((next_pdu = cbuf->cb_buffer[index]) != NULL) {
3409 3408 /*
3410 3409 * If the PDU wait time has not exceeded threshold
3411 3410 * stop scanning the staging queue until the timer
3412 3411 * fires again
3413 3412 */
3414 3413 if ((gethrtime() - next_pdu->isp_queue_time)
3415 3414 < (rxpdu_queue_threshold * NANOSEC)) {
3416 3415 mutex_exit(&ist->ist_sn_mutex);
3417 3416 return;
3418 3417 }
3419 3418 /*
3420 3419 * Remove the next PDU from the queue and post it
3421 3420 * to the SCSI layer, skipping over the missing
3422 3421 * PDU. Stop scanning the staging queue until
3423 3422 * the monitor timer fires again
3424 3423 */
3425 3424 (void) iscsit_remove_pdu_from_queue(ist, next_cmdsn);
3426 3425 mutex_exit(&ist->ist_sn_mutex);
3427 3426 DTRACE_PROBE3(advanced__to__blocked__cmdsn,
3428 3427 iscsit_sess_t *, ist, idm_pdu_t *, next_pdu,
3429 3428 uint32_t, next_cmdsn);
3430 3429 iscsit_post_staged_pdu(next_pdu);
3431 3430 /* Deliver any subsequent PDUs immediately */
3432 3431 iscsit_process_pdu_in_queue(ist);
3433 3432 return;
3434 3433 }
3435 3434 /*
3436 3435 * Skipping over i PDUs, e.g. a case where commands 1001 and
3437 3436 * 1002 are lost in the network, skip over both and post 1003
3438 3437 * expcmdsn then becomes 1004 at the end of the scan.
3439 3438 */
3440 3439 DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist,
3441 3440 uint32_t, next_cmdsn);
3442 3441 }
3443 3442 /*
3444 3443 * following the assumption, staged cmdsn >= expcmdsn, this statement
3445 3444 * is never reached.
3446 3445 */
3447 3446 }
↓ open down ↓ |
3326 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX