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