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