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