iscsi_ioctl.c revision 1.26 1 /* $NetBSD: iscsi_ioctl.c,v 1.26 2017/06/24 11:31:26 mlelstv Exp $ */
2
3 /*-
4 * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "iscsi_globals.h"
33
34 #include <sys/file.h>
35 #include <sys/filedesc.h>
36 #include <sys/proc.h>
37
38 #ifndef ISCSI_MINIMAL
39 #include <uvm/uvm.h>
40 #include <uvm/uvm_pmap.h>
41 #endif
42
43 static kmutex_t iscsi_cleanup_mtx;
44 static kcondvar_t iscsi_cleanup_cv;
45 static kcondvar_t iscsi_event_cv;
46 static struct lwp *iscsi_cleanproc = NULL;
47
48 static uint16_t current_id = 0; /* Global session ID counter */
49
50 /* list of event handlers */
51 static event_handler_list_t event_handlers =
52 TAILQ_HEAD_INITIALIZER(event_handlers);
53
54 static connection_list_t iscsi_timeout_conn_list =
55 TAILQ_HEAD_INITIALIZER(iscsi_timeout_conn_list);
56
57 static ccb_list_t iscsi_timeout_ccb_list =
58 TAILQ_HEAD_INITIALIZER(iscsi_timeout_ccb_list);
59
60 static session_list_t iscsi_cleanups_list =
61 TAILQ_HEAD_INITIALIZER(iscsi_cleanups_list);
62
63 static connection_list_t iscsi_cleanupc_list =
64 TAILQ_HEAD_INITIALIZER(iscsi_cleanupc_list);
65
66 static uint32_t handler_id = 0; /* Handler ID counter */
67
68 /* -------------------------------------------------------------------------- */
69
70 /* Event management functions */
71
72 /*
73 * find_handler:
74 * Search the event handler list for the given ID.
75 *
76 * Parameter:
77 * id The handler ID.
78 *
79 * Returns:
80 * Pointer to handler if found, else NULL.
81 */
82
83
84 static event_handler_t *
85 find_handler(uint32_t id)
86 {
87 event_handler_t *curr;
88
89 KASSERT(mutex_owned(&iscsi_cleanup_mtx));
90
91 TAILQ_FOREACH(curr, &event_handlers, link)
92 if (curr->id == id)
93 break;
94
95 return curr;
96 }
97
98
99 /*
100 * register_event:
101 * Create event handler entry, return ID.
102 *
103 * Parameter:
104 * par The parameter.
105 */
106
107 static void
108 register_event(iscsi_register_event_parameters_t *par)
109 {
110 event_handler_t *handler;
111 int was_empty;
112
113 handler = malloc(sizeof(event_handler_t), M_DEVBUF, M_WAITOK | M_ZERO);
114 if (handler == NULL) {
115 DEBOUT(("No mem for event handler\n"));
116 par->status = ISCSI_STATUS_NO_RESOURCES;
117 return;
118 }
119
120 TAILQ_INIT(&handler->events);
121
122 mutex_enter(&iscsi_cleanup_mtx);
123 /* create a unique ID */
124 do {
125 ++handler_id;
126 } while (!handler_id || find_handler(handler_id) != NULL);
127 par->event_id = handler->id = handler_id;
128
129 was_empty = TAILQ_FIRST(&event_handlers) == NULL;
130 TAILQ_INSERT_TAIL(&event_handlers, handler, link);
131 if (was_empty)
132 iscsi_notify_cleanup();
133 mutex_exit(&iscsi_cleanup_mtx);
134
135 par->status = ISCSI_STATUS_SUCCESS;
136 DEB(5, ("Register Event OK, ID %d\n", par->event_id));
137 }
138
139
140 /*
141 * deregister_event:
142 * Destroy handler entry and any waiting events, wake up waiter.
143 *
144 * Parameter:
145 * par The parameter.
146 */
147
148 static void
149 deregister_event(iscsi_register_event_parameters_t *par)
150 {
151 event_handler_t *handler;
152 event_t *evt;
153
154 mutex_enter(&iscsi_cleanup_mtx);
155 handler = find_handler(par->event_id);
156 if (handler == NULL) {
157 mutex_exit(&iscsi_cleanup_mtx);
158 DEB(1, ("Deregister Event ID %d not found\n", par->event_id));
159 par->status = ISCSI_STATUS_INVALID_EVENT_ID;
160 return;
161 }
162
163 TAILQ_REMOVE(&event_handlers, handler, link);
164 if (handler->waiter != NULL) {
165 handler->waiter->status = ISCSI_STATUS_EVENT_DEREGISTERED;
166 cv_broadcast(&iscsi_event_cv);
167 }
168
169 while ((evt = TAILQ_FIRST(&handler->events)) != NULL) {
170 TAILQ_REMOVE(&handler->events, evt, link);
171 free(evt, M_TEMP);
172 }
173 mutex_exit(&iscsi_cleanup_mtx);
174
175 free(handler, M_DEVBUF);
176 par->status = ISCSI_STATUS_SUCCESS;
177 DEB(5, ("Deregister Event ID %d complete\n", par->event_id));
178 }
179
180
181 /*
182 * check_event:
183 * Return first queued event. Optionally wait for arrival of event.
184 *
185 * Parameter:
186 * par The parameter.
187 * wait Wait for event if true
188 */
189
190 static void
191 check_event(iscsi_wait_event_parameters_t *par, bool wait)
192 {
193 event_handler_t *handler;
194 event_t *evt;
195 int rc;
196
197 mutex_enter(&iscsi_cleanup_mtx);
198 handler = find_handler(par->event_id);
199 if (handler == NULL) {
200 mutex_exit(&iscsi_cleanup_mtx);
201 DEBOUT(("Wait Event ID %d not found\n", par->event_id));
202 par->status = ISCSI_STATUS_INVALID_EVENT_ID;
203 return;
204 }
205 if (handler->waiter != NULL) {
206 mutex_exit(&iscsi_cleanup_mtx);
207 DEBOUT(("Wait Event ID %d already waiting\n", par->event_id));
208 par->status = ISCSI_STATUS_EVENT_WAITING;
209 return;
210 }
211 par->status = ISCSI_STATUS_SUCCESS;
212 DEB(99, ("Wait Event ID %d\n", par->event_id));
213
214 do {
215 evt = TAILQ_FIRST(&handler->events);
216 if (evt != NULL) {
217 TAILQ_REMOVE(&handler->events, evt, link);
218 } else {
219 if (!wait) {
220 par->status = ISCSI_STATUS_LIST_EMPTY;
221 return;
222 }
223 if (par->status != ISCSI_STATUS_SUCCESS) {
224 return;
225 }
226 handler->waiter = par;
227 rc = cv_wait_sig(&iscsi_event_cv, &iscsi_cleanup_mtx);
228 if (rc) {
229 mutex_exit(&iscsi_cleanup_mtx);
230 par->status = ISCSI_STATUS_LIST_EMPTY;
231 return;
232 }
233 }
234 } while (evt == NULL);
235 mutex_exit(&iscsi_cleanup_mtx);
236
237 par->connection_id = evt->connection_id;
238 par->session_id = evt->session_id;
239 par->event_kind = evt->event_kind;
240 par->reason = evt->reason;
241
242 free(evt, M_TEMP);
243 }
244
245 /*
246 * add_event
247 * Adds an event entry to each registered handler queue.
248 * Note that events are simply duplicated because we expect the number of
249 * handlers to be very small, usually 1 (the daemon).
250 *
251 * Parameters:
252 * kind The event kind
253 * sid The ID of the affected session
254 * cid The ID of the affected connection
255 * reason The reason code
256 */
257
258 void
259 add_event(iscsi_event_t kind, uint32_t sid, uint32_t cid, uint32_t reason)
260 {
261 event_handler_t *curr;
262 event_t *evt;
263
264 DEB(9, ("Add_event kind %d, sid %d, cid %d, reason %d\n",
265 kind, sid, cid, reason));
266
267 mutex_enter(&iscsi_cleanup_mtx);
268 TAILQ_FOREACH(curr, &event_handlers, link) {
269 evt = malloc(sizeof(*evt), M_TEMP, M_NOWAIT);
270 if (evt == NULL) {
271 DEBOUT(("Cannot allocate event\n"));
272 break;
273 }
274 evt->event_kind = kind;
275 evt->session_id = sid;
276 evt->connection_id = cid;
277 evt->reason = reason;
278
279 TAILQ_INSERT_TAIL(&curr->events, evt, link);
280 if (curr->waiter != NULL) {
281 curr->waiter = NULL;
282 cv_broadcast(&iscsi_event_cv);
283 }
284 }
285 mutex_exit(&iscsi_cleanup_mtx);
286 }
287
288
289 /*
290 * check_event_handlers
291 * Checks for dead event handlers. A dead event handler would deplete
292 * memory over time, so we have to make sure someone at the other
293 * end is actively monitoring events.
294 * This function is called every 30 seconds or so (less frequent if there
295 * is other activity for the cleanup thread to deal with) to go through
296 * the list of handlers and check whether the first element in the event
297 * list has changed at all. If not, the event is deregistered.
298 * Note that this will not detect dead handlers if no events are pending,
299 * but we don't care as long as events don't accumulate in the list.
300 *
301 */
302
303 static void
304 check_event_handlers(void)
305 {
306 event_handler_t *curr, *next;
307 event_t *evt;
308
309 KASSERT(mutex_owned(&iscsi_cleanup_mtx));
310
311 for (curr = TAILQ_FIRST(&event_handlers); curr != NULL; curr = next) {
312 next = TAILQ_NEXT(curr, link);
313 evt = TAILQ_FIRST(&curr->events);
314
315 if (evt != NULL && evt == curr->first_in_list) {
316 DEBOUT(("Found Dead Event Handler %d, removing\n", curr->id));
317
318 TAILQ_REMOVE(&event_handlers, curr, link);
319 while ((evt = TAILQ_FIRST(&curr->events)) != NULL) {
320 TAILQ_REMOVE(&curr->events, evt, link);
321 free(evt, M_TEMP);
322 }
323 free(curr, M_DEVBUF);
324 } else
325 curr->first_in_list = evt;
326 }
327 }
328
329
330 /* -------------------------------------------------------------------------- */
331
332 /*
333 * get_socket:
334 * Get the file pointer from the socket handle passed into login.
335 *
336 * Parameter:
337 * fdes IN: The socket handle
338 * fpp OUT: The pointer to the resulting file pointer
339 *
340 * Returns: 0 on success, else an error code.
341 *
342 */
343
344 static int
345 get_socket(int fdes, struct file **fpp)
346 {
347 struct file *fp;
348
349 if ((fp = fd_getfile(fdes)) == NULL) {
350 return EBADF;
351 }
352 if (fp->f_type != DTYPE_SOCKET) {
353 return ENOTSOCK;
354 }
355
356 /* Add the reference */
357 mutex_enter(&fp->f_lock);
358 fp->f_count++;
359 mutex_exit(&fp->f_lock);
360
361 *fpp = fp;
362 return 0;
363 }
364
365 /*
366 * release_socket:
367 * Release the file pointer from the socket handle passed into login.
368 *
369 * Parameter:
370 * fp IN: The pointer to the resulting file pointer
371 *
372 */
373
374 static void
375 release_socket(struct file *fp)
376 {
377 /* Add the reference */
378 mutex_enter(&fp->f_lock);
379 fp->f_count--;
380 mutex_exit(&fp->f_lock);
381 }
382
383
384 /*
385 * find_session:
386 * Find a session by ID.
387 *
388 * Parameter: the session ID
389 *
390 * Returns: The pointer to the session (or NULL if not found)
391 */
392
393 session_t *
394 find_session(uint32_t id)
395 {
396 session_t *curr;
397
398 KASSERT(mutex_owned(&iscsi_cleanup_mtx));
399
400 TAILQ_FOREACH(curr, &iscsi_sessions, sessions)
401 if (curr->id == id) {
402 break;
403 }
404 return curr;
405 }
406
407
408 /*
409 * find_connection:
410 * Find a connection by ID.
411 *
412 * Parameter: the session pointer and the connection ID
413 *
414 * Returns: The pointer to the connection (or NULL if not found)
415 */
416
417 connection_t *
418 find_connection(session_t *session, uint32_t id)
419 {
420 connection_t *curr;
421
422 KASSERT(mutex_owned(&iscsi_cleanup_mtx));
423
424 TAILQ_FOREACH(curr, &session->conn_list, connections)
425 if (curr->id == id) {
426 break;
427 }
428 return curr;
429 }
430
431 /*
432 * ref_session:
433 * Reference a session
434 *
435 * Session cannot be release until reference count reaches zero
436 *
437 * Returns: 1 if reference counter would overflow
438 */
439
440 int
441 ref_session(session_t *session)
442 {
443 int rc = 1;
444
445 mutex_enter(&iscsi_cleanup_mtx);
446 KASSERT(session != NULL);
447 if (session->refcount <= CCBS_PER_SESSION) {
448 session->refcount++;
449 rc = 0;
450 }
451 mutex_exit(&iscsi_cleanup_mtx);
452
453 return rc;
454 }
455
456 /*
457 * unref_session:
458 * Unreference a session
459 *
460 * Release session reference, trigger cleanup
461 */
462
463 void
464 unref_session(session_t *session)
465 {
466
467 mutex_enter(&session->lock);
468 KASSERT(session != NULL);
469 KASSERT(session->refcount > 0);
470 if (--session->refcount == 0)
471 cv_broadcast(&session->sess_cv);
472 mutex_exit(&session->lock);
473 }
474
475
476 /*
477 * kill_connection:
478 * Terminate the connection as gracefully as possible.
479 *
480 * Parameter:
481 * conn The connection to terminate
482 * status The status code for the connection's "terminating" field
483 * logout The logout reason code
484 * recover Attempt to recover connection
485 */
486
487 void
488 kill_connection(connection_t *conn, uint32_t status, int logout, bool recover)
489 {
490 session_t *sess = conn->session;
491 int terminating;
492
493 DEBC(conn, 1, ("Kill_connection: terminating=%d, status=%d, logout=%d, "
494 "state=%d\n",
495 conn->terminating, status, logout, conn->state));
496
497 mutex_enter(&iscsi_cleanup_mtx);
498 if (recover &&
499 !conn->destroy &&
500 conn->recover > MAX_RECOVERY_ATTEMPTS) {
501 DEBC(conn, 1,
502 ("Kill_connection: Too many recovery attempts, destroying\n"));
503 conn->destroy = TRUE;
504 }
505
506 if (!recover || conn->destroy) {
507
508 if (conn->in_session) {
509 conn->in_session = FALSE;
510 TAILQ_REMOVE(&sess->conn_list, conn, connections);
511 sess->mru_connection = TAILQ_FIRST(&sess->conn_list);
512 }
513
514 if (!conn->destroy) {
515 DEBC(conn, 1, ("Kill_connection setting destroy flag\n"));
516 conn->destroy = TRUE;
517 }
518 }
519
520 terminating = conn->terminating;
521 if (!terminating)
522 conn->terminating = status;
523
524 /* Don't recurse */
525 if (terminating) {
526 mutex_exit(&iscsi_cleanup_mtx);
527
528 KASSERT(conn->state != ST_FULL_FEATURE);
529 DEBC(conn, 1, ("Kill_connection exiting (already terminating)\n"));
530 goto done;
531 }
532
533 if (conn->state == ST_FULL_FEATURE) {
534 sess->active_connections--;
535 conn->state = ST_WINDING_DOWN;
536
537 /* If this is the last connection and ERL < 2, reset TSIH */
538 if (!sess->active_connections && sess->ErrorRecoveryLevel < 2)
539 sess->TSIH = 0;
540
541 /* Don't try to log out if the socket is broken or we're in the middle */
542 /* of logging in */
543 if (logout >= 0) {
544 if (sess->ErrorRecoveryLevel < 2 &&
545 logout == RECOVER_CONNECTION) {
546 logout = LOGOUT_CONNECTION;
547 }
548 if (!sess->active_connections &&
549 logout == LOGOUT_CONNECTION) {
550 logout = LOGOUT_SESSION;
551 }
552 mutex_exit(&iscsi_cleanup_mtx);
553
554 connection_timeout_start(conn, CONNECTION_TIMEOUT);
555
556 if (!send_logout(conn, conn, logout, FALSE)) {
557 conn->terminating = ISCSI_STATUS_SUCCESS;
558 return;
559 }
560 /*
561 * if the logout request was successfully sent,
562 * the logout response handler will do the rest
563 * of the termination processing. If the logout
564 * doesn't get a response, we'll get back in here
565 * once the timeout hits.
566 */
567
568 mutex_enter(&iscsi_cleanup_mtx);
569 }
570
571 }
572
573 conn->state = ST_SETTLING;
574 mutex_exit(&iscsi_cleanup_mtx);
575
576 done:
577 /* let send thread take over next step of cleanup */
578 mutex_enter(&conn->lock);
579 cv_broadcast(&conn->conn_cv);
580 mutex_exit(&conn->lock);
581
582 DEBC(conn, 5, ("kill_connection returns\n"));
583 }
584
585
586 /*
587 * kill_session:
588 * Terminate the session as gracefully as possible.
589 *
590 * Parameter:
591 * session Session to terminate
592 * status The status code for the termination
593 * logout The logout reason code
594
595 */
596
597 void
598 kill_session(session_t *session, uint32_t status, int logout, bool recover)
599 {
600 connection_t *conn;
601
602 DEB(1, ("ISCSI: kill_session %d, status %d, logout %d, recover %d\n",
603 session->id, status, logout, recover));
604
605 mutex_enter(&iscsi_cleanup_mtx);
606 if (session->terminating) {
607 mutex_exit(&iscsi_cleanup_mtx);
608
609 DEB(5, ("Session is being killed with status %d\n",session->terminating));
610 return;
611 }
612
613 /*
614 * don't do anything if session isn't established yet, termination will be
615 * handled elsewhere
616 */
617 if (session->sessions.tqe_next == NULL && session->sessions.tqe_prev == NULL) {
618 mutex_exit(&iscsi_cleanup_mtx);
619
620 DEB(5, ("Session is being killed which is not yet established\n"));
621 return;
622 }
623
624 if (recover) {
625 mutex_exit(&iscsi_cleanup_mtx);
626
627 /*
628 * Only recover when there's just one active connection left.
629 * Otherwise we get in all sorts of timing problems, and it doesn't
630 * make much sense anyway to recover when the other side has
631 * requested that we kill a multipathed session.
632 */
633 if (session->active_connections == 1) {
634 conn = assign_connection(session, FALSE);
635 if (conn != NULL)
636 kill_connection(conn, status, logout, TRUE);
637 }
638 return;
639 }
640
641 if (session->refcount > 0) {
642 mutex_exit(&iscsi_cleanup_mtx);
643
644 DEB(5, ("Session is being killed while in use (refcnt = %d)\n",
645 session->refcount));
646 return;
647 }
648
649 /* Remove session from global list */
650 session->terminating = status;
651 TAILQ_REMOVE(&iscsi_sessions, session, sessions);
652 session->sessions.tqe_next = NULL;
653 session->sessions.tqe_prev = NULL;
654
655 mutex_exit(&iscsi_cleanup_mtx);
656
657 /* kill all connections */
658 while ((conn = TAILQ_FIRST(&session->conn_list)) != NULL) {
659 kill_connection(conn, status, logout, FALSE);
660 logout = NO_LOGOUT;
661 }
662 }
663
664
665 /*
666 * create_connection:
667 * Create and init the necessary framework for a connection:
668 * Alloc the connection structure itself
669 * Copy connection parameters
670 * Create the send and receive threads
671 * And finally, log in.
672 *
673 * Parameter:
674 * par IN/OUT: The login parameters
675 * session IN: The owning session
676 * l IN: The lwp pointer of the caller
677 *
678 * Returns: 0 on success
679 * >0 on failure, connection structure deleted
680 * <0 on failure, connection is still terminating
681 */
682
683 static int
684 create_connection(iscsi_login_parameters_t *par, session_t *session,
685 struct lwp *l)
686 {
687 connection_t *connection;
688 int rc;
689
690 DEB(1, ("Create Connection for Session %d\n", session->id));
691
692 if (session->MaxConnections &&
693 session->active_connections >= session->MaxConnections) {
694 DEBOUT(("Too many connections (max = %d, curr = %d)\n",
695 session->MaxConnections, session->active_connections));
696 par->status = ISCSI_STATUS_MAXED_CONNECTIONS;
697 return EIO;
698 }
699
700 connection = malloc(sizeof(*connection), M_DEVBUF, M_WAITOK | M_ZERO);
701 if (connection == NULL) {
702 DEBOUT(("No mem for connection\n"));
703 par->status = ISCSI_STATUS_NO_RESOURCES;
704 return EIO;
705 }
706
707 mutex_enter(&iscsi_cleanup_mtx);
708 /* create a unique ID */
709 do {
710 ++session->conn_id;
711 } while (!session->conn_id ||
712 find_connection(session, session->conn_id) != NULL);
713 par->connection_id = connection->id = session->conn_id;
714 mutex_exit(&iscsi_cleanup_mtx);
715 DEB(99, ("Connection ID = %d\n", connection->id));
716
717 connection->session = session;
718
719 TAILQ_INIT(&connection->ccbs_waiting);
720 TAILQ_INIT(&connection->pdus_to_send);
721 TAILQ_INIT(&connection->pdu_pool);
722
723 mutex_init(&connection->lock, MUTEX_DEFAULT, IPL_BIO);
724 cv_init(&connection->conn_cv, "conn");
725 cv_init(&connection->pdu_cv, "pdupool");
726 cv_init(&connection->ccb_cv, "ccbwait");
727 cv_init(&connection->idle_cv, "idle");
728
729 callout_init(&connection->timeout, CALLOUT_MPSAFE);
730 callout_setfunc(&connection->timeout, connection_timeout_co, connection);
731 connection->idle_timeout_val = CONNECTION_IDLE_TIMEOUT;
732
733 init_sernum(&connection->StatSN_buf);
734 create_pdus(connection);
735
736 if ((rc = get_socket(par->socket, &connection->sock)) != 0) {
737 DEBOUT(("Invalid socket %d\n", par->socket));
738
739 callout_destroy(&connection->timeout);
740 cv_destroy(&connection->idle_cv);
741 cv_destroy(&connection->ccb_cv);
742 cv_destroy(&connection->pdu_cv);
743 cv_destroy(&connection->conn_cv);
744 mutex_destroy(&connection->lock);
745 free(connection, M_DEVBUF);
746 par->status = ISCSI_STATUS_INVALID_SOCKET;
747 return rc;
748 }
749 DEBC(connection, 1, ("get_socket: par_sock=%d, fdesc=%p\n",
750 par->socket, connection->sock));
751
752 /* close the file descriptor */
753 fd_close(par->socket);
754
755 connection->threadobj = l;
756 connection->login_par = par;
757
758 DEB(5, ("Creating receive thread\n"));
759 if ((rc = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, iscsi_rcv_thread,
760 connection, &connection->rcvproc,
761 "ConnRcv")) != 0) {
762 DEBOUT(("Can't create rcv thread (rc %d)\n", rc));
763
764 release_socket(connection->sock);
765 callout_destroy(&connection->timeout);
766 cv_destroy(&connection->idle_cv);
767 cv_destroy(&connection->ccb_cv);
768 cv_destroy(&connection->pdu_cv);
769 cv_destroy(&connection->conn_cv);
770 mutex_destroy(&connection->lock);
771 free(connection, M_DEVBUF);
772 par->status = ISCSI_STATUS_NO_RESOURCES;
773 return rc;
774 }
775 DEB(5, ("Creating send thread\n"));
776 if ((rc = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, iscsi_send_thread,
777 connection, &connection->sendproc,
778 "ConnSend")) != 0) {
779 DEBOUT(("Can't create send thread (rc %d)\n", rc));
780
781 connection->terminating = ISCSI_STATUS_NO_RESOURCES;
782
783 /*
784 * We must close the socket here to force the receive
785 * thread to wake up
786 */
787 DEBC(connection, 1,
788 ("Closing Socket %p\n", connection->sock));
789 mutex_enter(&connection->sock->f_lock);
790 connection->sock->f_count += 1;
791 mutex_exit(&connection->sock->f_lock);
792 closef(connection->sock);
793
794 /* give receive thread time to exit */
795 kpause("settle", false, 2 * hz, NULL);
796
797 release_socket(connection->sock);
798 callout_destroy(&connection->timeout);
799 cv_destroy(&connection->idle_cv);
800 cv_destroy(&connection->ccb_cv);
801 cv_destroy(&connection->pdu_cv);
802 cv_destroy(&connection->conn_cv);
803 mutex_destroy(&connection->lock);
804 free(connection, M_DEVBUF);
805 par->status = ISCSI_STATUS_NO_RESOURCES;
806 return rc;
807 }
808
809 /*
810 * At this point, each thread will tie 'sock' into its own file descriptor
811 * tables w/o increasing the use count - they will inherit the use
812 * increments performed in get_socket().
813 */
814
815 if ((rc = send_login(connection)) != 0) {
816 DEBC(connection, 0, ("Login failed (rc %d)\n", rc));
817 /* Don't attempt to recover, there seems to be something amiss */
818 kill_connection(connection, rc, NO_LOGOUT, FALSE);
819 par->status = rc;
820 return -1;
821 }
822
823 mutex_enter(&iscsi_cleanup_mtx);
824 if (session->terminating) {
825 mutex_exit(&iscsi_cleanup_mtx);
826 DEBC(connection, 0, ("Session terminating\n"));
827 kill_connection(connection, rc, NO_LOGOUT, FALSE);
828 par->status = session->terminating;
829 return -1;
830 }
831 connection->state = ST_FULL_FEATURE;
832 TAILQ_INSERT_TAIL(&session->conn_list, connection, connections);
833 connection->in_session = TRUE;
834 session->total_connections++;
835 session->active_connections++;
836 session->mru_connection = connection;
837 mutex_exit(&iscsi_cleanup_mtx);
838
839 DEBC(connection, 5, ("Connection created successfully!\n"));
840 return 0;
841 }
842
843
844 /*
845 * recreate_connection:
846 * Revive dead connection
847 *
848 * Parameter:
849 * par IN/OUT: The login parameters
850 * conn IN: The connection
851 * l IN: The lwp pointer of the caller
852 *
853 * Returns: 0 on success
854 * >0 on failure, connection structure deleted
855 * <0 on failure, connection is still terminating
856 */
857
858 static int
859 recreate_connection(iscsi_login_parameters_t *par, session_t *session,
860 connection_t *connection, struct lwp *l)
861 {
862 int rc;
863 ccb_t *ccb;
864 ccb_list_t old_waiting;
865 pdu_t *pdu;
866 uint32_t sn;
867
868 DEB(1, ("ReCreate Connection %d for Session %d, ERL=%d\n",
869 connection->id, connection->session->id,
870 connection->session->ErrorRecoveryLevel));
871
872 if (session->MaxConnections &&
873 session->active_connections >= session->MaxConnections) {
874 DEBOUT(("Too many connections (max = %d, curr = %d)\n",
875 session->MaxConnections, session->active_connections));
876 par->status = ISCSI_STATUS_MAXED_CONNECTIONS;
877 return EIO;
878 }
879
880 /* close old socket */
881 if (connection->sock != NULL) {
882 closef(connection->sock);
883 connection->sock = NULL;
884 }
885
886 if ((rc = get_socket(par->socket, &connection->sock)) != 0) {
887 DEBOUT(("Invalid socket %d\n", par->socket));
888 par->status = ISCSI_STATUS_INVALID_SOCKET;
889 return rc;
890 }
891 DEBC(connection, 1, ("get_socket: par_sock=%d, fdesc=%p\n",
892 par->socket, connection->sock));
893
894 /* close the file descriptor */
895 fd_close(par->socket);
896
897 connection->threadobj = l;
898 connection->login_par = par;
899 connection->terminating = ISCSI_STATUS_SUCCESS;
900 connection->recover++;
901 connection->num_timeouts = 0;
902 connection->state = ST_SEC_NEG;
903 connection->HeaderDigest = 0;
904 connection->DataDigest = 0;
905
906 session->active_connections++;
907
908 TAILQ_INIT(&old_waiting);
909
910 mutex_enter(&connection->lock);
911 while ((ccb = TAILQ_FIRST(&connection->ccbs_waiting)) != NULL) {
912 suspend_ccb(ccb, FALSE);
913 TAILQ_INSERT_TAIL(&old_waiting, ccb, chain);
914 }
915 init_sernum(&connection->StatSN_buf);
916 cv_broadcast(&connection->idle_cv);
917 mutex_exit(&connection->lock);
918
919 if ((rc = send_login(connection)) != 0) {
920 DEBOUT(("Login failed (rc %d)\n", rc));
921 while ((ccb = TAILQ_FIRST(&old_waiting)) != NULL) {
922 TAILQ_REMOVE(&old_waiting, ccb, chain);
923 wake_ccb(ccb, rc);
924 }
925 /* Don't attempt to recover, there seems to be something amiss */
926 kill_connection(connection, rc, NO_LOGOUT, FALSE);
927 par->status = rc;
928 return -1;
929 }
930
931 DEBC(connection, 9, ("Re-Login successful\n"));
932 par->status = ISCSI_STATUS_SUCCESS;
933
934 connection->state = ST_FULL_FEATURE;
935 session->mru_connection = connection;
936
937 while ((ccb = TAILQ_FIRST(&old_waiting)) != NULL) {
938 TAILQ_REMOVE(&old_waiting, ccb, chain);
939 mutex_enter(&connection->lock);
940 suspend_ccb(ccb, TRUE);
941 mutex_exit(&connection->lock);
942
943 rc = send_task_management(connection, ccb, NULL, TASK_REASSIGN);
944 /* if we get an error on reassign, restart the original request */
945 if (rc && ccb->pdu_waiting != NULL) {
946 mutex_enter(&session->lock);
947 if (sn_a_lt_b(ccb->CmdSN, session->ExpCmdSN)) {
948 pdu = ccb->pdu_waiting;
949 sn = get_sernum(session, pdu);
950
951 /* update CmdSN */
952 DEBC(connection, 0, ("Resend ccb %p (%d) - updating CmdSN old %u, new %u\n",
953 ccb, rc, ccb->CmdSN, sn));
954 ccb->CmdSN = sn;
955 pdu->pdu.p.command.CmdSN = htonl(ccb->CmdSN);
956 } else {
957 DEBC(connection, 0, ("Resend ccb %p (%d) - CmdSN %u\n",
958 ccb, rc, ccb->CmdSN));
959 }
960 mutex_exit(&session->lock);
961 resend_pdu(ccb);
962 } else {
963 DEBC(connection, 0, ("Resend ccb %p (%d) CmdSN %u - reassigned\n",
964 ccb, rc, ccb->CmdSN));
965 ccb_timeout_start(ccb, COMMAND_TIMEOUT);
966 }
967 }
968
969 mutex_enter(&session->lock);
970 cv_broadcast(&session->sess_cv);
971 mutex_exit(&session->lock);
972
973 DEBC(connection, 0, ("Connection ReCreated successfully - status %d\n",
974 par->status));
975
976 return 0;
977 }
978
979 /* -------------------------------------------------------------------------- */
980
981 /*
982 * check_login_pars:
983 * Check the parameters passed into login/add_connection
984 * for validity and consistency.
985 *
986 * Parameter:
987 * par The login parameters
988 *
989 * Returns: 0 on success, else an error code.
990 */
991
992 static int
993 check_login_pars(iscsi_login_parameters_t *par)
994 {
995 int i, n;
996
997 if (par->is_present.auth_info) {
998 /* check consistency of authentication parameters */
999
1000 if (par->auth_info.auth_number > ISCSI_AUTH_OPTIONS) {
1001 DEBOUT(("Auth number invalid: %d\n", par->auth_info.auth_number));
1002 return ISCSI_STATUS_PARAMETER_INVALID;
1003 }
1004
1005 if (par->auth_info.auth_number > 2) {
1006 DEBOUT(("Auth number invalid: %d\n", par->auth_info.auth_number));
1007 return ISCSI_STATUS_NOTIMPL;
1008 }
1009
1010 for (i = 0, n = 0; i < par->auth_info.auth_number; i++) {
1011 #if 0
1012 if (par->auth_info.auth_type[i] < ISCSI_AUTH_None) {
1013 DEBOUT(("Auth type invalid: %d\n",
1014 par->auth_info.auth_type[i]));
1015 return ISCSI_STATUS_PARAMETER_INVALID;
1016 }
1017 #endif
1018 if (par->auth_info.auth_type[i] > ISCSI_AUTH_CHAP) {
1019 DEBOUT(("Auth type invalid: %d\n",
1020 par->auth_info.auth_type[i]));
1021 return ISCSI_STATUS_NOTIMPL;
1022 }
1023 n = max(n, par->auth_info.auth_type[i]);
1024 }
1025 if (n) {
1026 if (!par->is_present.password ||
1027 (par->auth_info.mutual_auth &&
1028 !par->is_present.target_password)) {
1029 DEBOUT(("Password missing\n"));
1030 return ISCSI_STATUS_PARAMETER_MISSING;
1031 }
1032 /* Note: Default for user-name is initiator name */
1033 }
1034 }
1035 if (par->login_type != ISCSI_LOGINTYPE_DISCOVERY &&
1036 !par->is_present.TargetName) {
1037 DEBOUT(("Target name missing, login type %d\n", par->login_type));
1038 return ISCSI_STATUS_PARAMETER_MISSING;
1039 }
1040 if (par->is_present.MaxRecvDataSegmentLength) {
1041 if (par->MaxRecvDataSegmentLength < 512 ||
1042 par->MaxRecvDataSegmentLength > 0xffffff) {
1043 DEBOUT(("MaxRecvDataSegmentLength invalid: %d\n",
1044 par->MaxRecvDataSegmentLength));
1045 return ISCSI_STATUS_PARAMETER_INVALID;
1046 }
1047 }
1048 return 0;
1049 }
1050
1051
1052 /*
1053 * login:
1054 * Handle the login ioctl - Create a session:
1055 * Alloc the session structure
1056 * Copy session parameters
1057 * And call create_connection to establish the connection.
1058 *
1059 * Parameter:
1060 * par IN/OUT: The login parameters
1061 * l IN: The lwp pointer of the caller
1062 */
1063
1064 static void
1065 login(iscsi_login_parameters_t *par, struct lwp *l, device_t dev)
1066 {
1067 session_t *session;
1068 int rc;
1069
1070 DEB(99, ("ISCSI: login\n"));
1071
1072 if (!iscsi_InitiatorName[0]) {
1073 DEB(1, ("No Initiator Name\n"));
1074 par->status = ISCSI_STATUS_NO_INITIATOR_NAME;
1075 return;
1076 }
1077
1078 if ((par->status = check_login_pars(par)) != 0)
1079 return;
1080
1081 /* alloc the session */
1082 session = malloc(sizeof(*session), M_DEVBUF, M_WAITOK | M_ZERO);
1083 if (session == NULL) {
1084 DEBOUT(("No mem for session\n"));
1085 par->status = ISCSI_STATUS_NO_RESOURCES;
1086 return;
1087 }
1088 TAILQ_INIT(&session->conn_list);
1089 TAILQ_INIT(&session->ccb_pool);
1090
1091 mutex_init(&session->lock, MUTEX_DEFAULT, IPL_BIO);
1092 cv_init(&session->sess_cv, "session");
1093 cv_init(&session->ccb_cv, "ccb");
1094
1095 mutex_enter(&iscsi_cleanup_mtx);
1096 /* create a unique ID */
1097 do {
1098 ++current_id;
1099 } while (!current_id || find_session(current_id) != NULL);
1100 par->session_id = session->id = current_id;
1101 mutex_exit(&iscsi_cleanup_mtx);
1102
1103 create_ccbs(session);
1104 session->login_type = par->login_type;
1105 session->CmdSN = 1;
1106
1107 if ((rc = create_connection(par, session, l)) != 0) {
1108 if (rc > 0) {
1109 destroy_ccbs(session);
1110 cv_destroy(&session->ccb_cv);
1111 cv_destroy(&session->sess_cv);
1112 mutex_destroy(&session->lock);
1113 free(session, M_DEVBUF);
1114 }
1115 return;
1116 }
1117
1118 mutex_enter(&iscsi_cleanup_mtx);
1119 TAILQ_INSERT_HEAD(&iscsi_sessions, session, sessions);
1120 mutex_exit(&iscsi_cleanup_mtx);
1121
1122 /* Session established, map LUNs? */
1123 if (par->login_type == ISCSI_LOGINTYPE_MAP) {
1124 copyinstr(par->TargetName, session->tgtname,
1125 sizeof(session->tgtname), NULL);
1126 DEB(1, ("Login: map session %d\n", session->id));
1127 if (!map_session(session, dev)) {
1128 DEB(1, ("Login: map session %d failed\n", session->id));
1129 kill_session(session, ISCSI_STATUS_MAP_FAILED,
1130 LOGOUT_SESSION, FALSE);
1131 par->status = ISCSI_STATUS_MAP_FAILED;
1132 return;
1133 }
1134 }
1135 }
1136
1137
1138 /*
1139 * logout:
1140 * Handle the logout ioctl - Kill a session.
1141 *
1142 * Parameter:
1143 * par IN/OUT: The login parameters
1144 */
1145
1146 static void
1147 logout(iscsi_logout_parameters_t *par)
1148 {
1149 session_t *session;
1150
1151 DEB(5, ("ISCSI: logout session %d\n", par->session_id));
1152
1153 mutex_enter(&iscsi_cleanup_mtx);
1154 if ((session = find_session(par->session_id)) == NULL) {
1155 mutex_exit(&iscsi_cleanup_mtx);
1156 DEBOUT(("Session %d not found\n", par->session_id));
1157 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1158 return;
1159 }
1160 mutex_exit(&iscsi_cleanup_mtx);
1161 /* If the session exists, this always succeeds */
1162 par->status = ISCSI_STATUS_SUCCESS;
1163
1164 kill_session(session, ISCSI_STATUS_LOGOUT, LOGOUT_SESSION, FALSE);
1165 }
1166
1167
1168 /*
1169 * add_connection:
1170 * Handle the add_connection ioctl.
1171 *
1172 * Parameter:
1173 * par IN/OUT: The login parameters
1174 * l IN: The lwp pointer of the caller
1175 */
1176
1177 static void
1178 add_connection(iscsi_login_parameters_t *par, struct lwp *l)
1179 {
1180 session_t *session;
1181
1182 DEB(5, ("ISCSI: add_connection to session %d\n", par->session_id));
1183
1184 mutex_enter(&iscsi_cleanup_mtx);
1185 if ((session = find_session(par->session_id)) == NULL) {
1186 mutex_exit(&iscsi_cleanup_mtx);
1187 DEBOUT(("Session %d not found\n", par->session_id));
1188 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1189 return;
1190 }
1191 mutex_exit(&iscsi_cleanup_mtx);
1192 if ((par->status = check_login_pars(par)) == 0) {
1193 create_connection(par, session, l);
1194 }
1195 }
1196
1197
1198 /*
1199 * remove_connection:
1200 * Handle the remove_connection ioctl.
1201 *
1202 * Parameter:
1203 * par IN/OUT: The remove parameters
1204 */
1205
1206 static void
1207 remove_connection(iscsi_remove_parameters_t *par)
1208 {
1209 connection_t *conn;
1210 session_t *session;
1211
1212 DEB(5, ("ISCSI: remove_connection %d from session %d\n",
1213 par->connection_id, par->session_id));
1214
1215 mutex_enter(&iscsi_cleanup_mtx);
1216 if ((session = find_session(par->session_id)) == NULL) {
1217 mutex_exit(&iscsi_cleanup_mtx);
1218 DEBOUT(("Session %d not found\n", par->session_id));
1219 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1220 return;
1221 }
1222
1223 if ((conn = find_connection(session, par->connection_id)) == NULL) {
1224 mutex_exit(&iscsi_cleanup_mtx);
1225 DEBOUT(("Connection %d not found in session %d\n",
1226 par->connection_id, par->session_id));
1227
1228 par->status = ISCSI_STATUS_INVALID_CONNECTION_ID;
1229 } else {
1230 mutex_exit(&iscsi_cleanup_mtx);
1231 kill_connection(conn, ISCSI_STATUS_LOGOUT, LOGOUT_CONNECTION,
1232 FALSE);
1233 par->status = ISCSI_STATUS_SUCCESS;
1234 }
1235 }
1236
1237
1238 /*
1239 * restore_connection:
1240 * Handle the restore_connection ioctl.
1241 *
1242 * Parameter:
1243 * par IN/OUT: The login parameters
1244 * l IN: The lwp pointer of the caller
1245 */
1246
1247 static void
1248 restore_connection(iscsi_login_parameters_t *par, struct lwp *l)
1249 {
1250 session_t *session;
1251 connection_t *connection;
1252
1253 DEB(1, ("ISCSI: restore_connection %d of session %d\n",
1254 par->connection_id, par->session_id));
1255
1256 mutex_enter(&iscsi_cleanup_mtx);
1257 if ((session = find_session(par->session_id)) == NULL) {
1258 mutex_exit(&iscsi_cleanup_mtx);
1259 DEBOUT(("Session %d not found\n", par->session_id));
1260 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1261 return;
1262 }
1263
1264 if ((connection = find_connection(session, par->connection_id)) == NULL) {
1265 mutex_exit(&iscsi_cleanup_mtx);
1266 DEBOUT(("Connection %d not found in session %d\n",
1267 par->connection_id, par->session_id));
1268 par->status = ISCSI_STATUS_INVALID_CONNECTION_ID;
1269 return;
1270 }
1271 mutex_exit(&iscsi_cleanup_mtx);
1272
1273 if ((par->status = check_login_pars(par)) == 0) {
1274 recreate_connection(par, session, connection, l);
1275 }
1276 }
1277
1278
1279 #ifndef ISCSI_MINIMAL
1280
1281 /*
1282 * map_databuf:
1283 * Map user-supplied data buffer into kernel space.
1284 *
1285 * Parameter:
1286 * p IN: The proc pointer of the caller
1287 * buf IN/OUT: The virtual address of the buffer, modified
1288 * on exit to reflect kernel VA.
1289 * datalen IN: The size of the data buffer
1290 *
1291 * Returns:
1292 * An ISCSI status code on error, else 0.
1293 */
1294
1295 uint32_t
1296 map_databuf(struct proc *p, void **buf, uint32_t datalen)
1297 {
1298 vaddr_t kva, databuf, offs;
1299 int error;
1300
1301 /* page align address */
1302 databuf = (vaddr_t) * buf & ~PAGE_MASK;
1303 /* offset of VA into page */
1304 offs = (vaddr_t) * buf & PAGE_MASK;
1305 /* round to full page including offset */
1306 datalen = (datalen + offs + PAGE_MASK) & ~PAGE_MASK;
1307
1308 /* Do some magic to the vm space reference count (copied from "copyin_proc") */
1309 if ((p->p_sflag & PS_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
1310 return ISCSI_STATUS_NO_RESOURCES;
1311 }
1312 p->p_vmspace->vm_refcnt++;
1313
1314 /* this is lifted from uvm_io */
1315 error = uvm_map_extract(&p->p_vmspace->vm_map, databuf, datalen,
1316 kernel_map, &kva,
1317 UVM_EXTRACT_QREF | UVM_EXTRACT_CONTIG |
1318 UVM_EXTRACT_FIXPROT);
1319 if (error) {
1320 DEBOUT(("uvm_map_extract failed, error = %d\n", error));
1321 return ISCSI_STATUS_NO_RESOURCES;
1322 }
1323 /* add offset back into kernel VA */
1324 *buf = (void *) (kva + offs);
1325
1326 return 0;
1327 }
1328
1329
1330 /*
1331 * unmap_databuf:
1332 * Remove kernel space mapping of data buffer.
1333 *
1334 * Parameter:
1335 * p IN: The proc pointer of the caller
1336 * buf IN: The kernel virtual address of the buffer
1337 * datalen IN: The size of the data buffer
1338 *
1339 * Returns:
1340 * An ISCSI status code on error, else 0.
1341 */
1342
1343 void
1344 unmap_databuf(struct proc *p, void *buf, uint32_t datalen)
1345 {
1346 struct vm_map_entry *dead_entries;
1347 vaddr_t databuf;
1348
1349 /* round to full page */
1350 datalen = (datalen + ((uintptr_t) buf & PAGE_MASK) + PAGE_MASK) & ~PAGE_MASK;
1351 /* page align address */
1352 databuf = (vaddr_t) buf & ~PAGE_MASK;
1353
1354 /* following code lifted almost verbatim from uvm_io.c */
1355 vm_map_lock(kernel_map);
1356 uvm_unmap_remove(kernel_map, databuf, databuf + datalen, &dead_entries,
1357 0);
1358 vm_map_unlock(kernel_map);
1359 if (dead_entries != NULL) {
1360 uvm_unmap_detach(dead_entries, AMAP_REFALL);
1361 }
1362 /* this apparently reverses the magic to the vm ref count, from copyin_proc */
1363 uvmspace_free(p->p_vmspace);
1364 }
1365
1366
1367 /*
1368 * io_command:
1369 * Handle the io_command ioctl.
1370 *
1371 * Parameter:
1372 * par IN/OUT: The iocommand parameters
1373 * l IN: The lwp pointer of the caller
1374 */
1375
1376 static void
1377 io_command(iscsi_iocommand_parameters_t *par, struct lwp *l)
1378 {
1379 uint32_t datalen = par->req.datalen;
1380 void *databuf = par->req.databuf;
1381 session_t *session;
1382
1383 DEB(9, ("ISCSI: io_command, SID=%d, lun=%" PRIu64 "\n", par->session_id, par->lun));
1384 mutex_enter(&iscsi_cleanup_mtx);
1385 if ((session = find_session(par->session_id)) == NULL) {
1386 mutex_exit(&iscsi_cleanup_mtx);
1387 DEBOUT(("Session %d not found\n", par->session_id));
1388 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1389 return;
1390 }
1391 mutex_exit(&iscsi_cleanup_mtx);
1392
1393 par->req.senselen_used = 0;
1394 par->req.datalen_used = 0;
1395 par->req.error = 0;
1396 par->req.status = 0;
1397 par->req.retsts = SCCMD_UNKNOWN; /* init to failure code */
1398
1399 if (par->req.cmdlen > 16 || par->req.senselen > sizeof(par->req.sense)) {
1400 par->status = ISCSI_STATUS_PARAMETER_INVALID;
1401 return;
1402 }
1403
1404 if (datalen && (par->status = map_databuf(l->l_proc,
1405 &par->req.databuf, datalen)) != 0) {
1406 return;
1407 }
1408 par->status = send_io_command(session, par->lun, &par->req,
1409 par->options.immediate, par->connection_id);
1410
1411 if (datalen) {
1412 unmap_databuf(l->l_proc, par->req.databuf, datalen);
1413 par->req.databuf = databuf; /* restore original addr */
1414 }
1415
1416 switch (par->status) {
1417 case ISCSI_STATUS_SUCCESS:
1418 par->req.retsts = SCCMD_OK;
1419 break;
1420
1421 case ISCSI_STATUS_TARGET_BUSY:
1422 par->req.retsts = SCCMD_BUSY;
1423 break;
1424
1425 case ISCSI_STATUS_TIMEOUT:
1426 case ISCSI_STATUS_SOCKET_ERROR:
1427 par->req.retsts = SCCMD_TIMEOUT;
1428 break;
1429
1430 default:
1431 par->req.retsts = (par->req.senselen_used) ? SCCMD_SENSE
1432 : SCCMD_UNKNOWN;
1433 break;
1434 }
1435 }
1436 #endif
1437
1438 /*
1439 * send_targets:
1440 * Handle the send_targets ioctl.
1441 * Note: If the passed buffer is too small to hold the complete response,
1442 * the response is kept in the session structure so it can be
1443 * retrieved with the next call to this function without having to go to
1444 * the target again. Once the complete response has been retrieved, it
1445 * is discarded.
1446 *
1447 * Parameter:
1448 * par IN/OUT: The send_targets parameters
1449 */
1450
1451 static void
1452 send_targets(iscsi_send_targets_parameters_t *par)
1453 {
1454 int rc;
1455 uint32_t rlen, cplen;
1456 session_t *session;
1457
1458 mutex_enter(&iscsi_cleanup_mtx);
1459 if ((session = find_session(par->session_id)) == NULL) {
1460 mutex_exit(&iscsi_cleanup_mtx);
1461 DEBOUT(("Session %d not found\n", par->session_id));
1462 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1463 return;
1464 }
1465 mutex_exit(&iscsi_cleanup_mtx);
1466
1467 DEB(9, ("ISCSI: send_targets, rsp_size=%d; Saved list: %p\n",
1468 par->response_size, session->target_list));
1469
1470 if (session->target_list == NULL) {
1471 rc = send_send_targets(session, par->key);
1472 if (rc) {
1473 par->status = rc;
1474 return;
1475 }
1476 }
1477 rlen = session->target_list_len;
1478 par->response_total = rlen;
1479 cplen = min(par->response_size, rlen);
1480 if (cplen) {
1481 copyout(session->target_list, par->response_buffer, cplen);
1482 }
1483 par->response_used = cplen;
1484
1485 /* If all of the response was copied, don't keep it around */
1486 if (rlen && par->response_used == rlen) {
1487 free(session->target_list, M_TEMP);
1488 session->target_list = NULL;
1489 }
1490
1491 par->status = ISCSI_STATUS_SUCCESS;
1492 }
1493
1494
1495 /*
1496 * set_node_name:
1497 * Handle the set_node_name ioctl.
1498 *
1499 * Parameter:
1500 * par IN/OUT: The set_node_name parameters
1501 */
1502
1503 static void
1504 set_node_name(iscsi_set_node_name_parameters_t *par)
1505 {
1506
1507 if (strlen(par->InitiatorName) >= ISCSI_STRING_LENGTH ||
1508 strlen(par->InitiatorAlias) >= ISCSI_STRING_LENGTH) {
1509 DEBOUT(("*** set_node_name string too long!\n"));
1510 par->status = ISCSI_STATUS_PARAMETER_INVALID;
1511 return;
1512 }
1513 strlcpy(iscsi_InitiatorName, par->InitiatorName, sizeof(iscsi_InitiatorName));
1514 strlcpy(iscsi_InitiatorAlias, par->InitiatorAlias, sizeof(iscsi_InitiatorAlias));
1515 memcpy(&iscsi_InitiatorISID, par->ISID, 6);
1516 DEB(5, ("ISCSI: set_node_name, ISID A=%x, B=%x, C=%x, D=%x\n",
1517 iscsi_InitiatorISID.ISID_A, iscsi_InitiatorISID.ISID_B,
1518 iscsi_InitiatorISID.ISID_C, iscsi_InitiatorISID.ISID_D));
1519
1520 if (!iscsi_InitiatorISID.ISID_A && !iscsi_InitiatorISID.ISID_B &&
1521 !iscsi_InitiatorISID.ISID_C && !iscsi_InitiatorISID.ISID_D) {
1522 iscsi_InitiatorISID.ISID_A = T_FORMAT_EN;
1523 iscsi_InitiatorISID.ISID_B = htons(0x1);
1524 iscsi_InitiatorISID.ISID_C = 0x37;
1525 iscsi_InitiatorISID.ISID_D = 0;
1526 }
1527
1528 par->status = ISCSI_STATUS_SUCCESS;
1529 }
1530
1531
1532 /*
1533 * connection_status:
1534 * Handle the connection_status ioctl.
1535 *
1536 * Parameter:
1537 * par IN/OUT: The status parameters
1538 */
1539
1540 static void
1541 connection_status(iscsi_conn_status_parameters_t *par)
1542 {
1543 connection_t *conn;
1544 session_t *session;
1545
1546 mutex_enter(&iscsi_cleanup_mtx);
1547 if ((session = find_session(par->session_id)) == NULL) {
1548 mutex_exit(&iscsi_cleanup_mtx);
1549 par->status = ISCSI_STATUS_INVALID_SESSION_ID;
1550 return;
1551 }
1552
1553 if (par->connection_id) {
1554 conn = find_connection(session, par->connection_id);
1555 } else {
1556 conn = TAILQ_FIRST(&session->conn_list);
1557 }
1558 par->status = (conn == NULL) ? ISCSI_STATUS_INVALID_CONNECTION_ID :
1559 ISCSI_STATUS_SUCCESS;
1560 mutex_exit(&iscsi_cleanup_mtx);
1561 DEB(9, ("ISCSI: connection_status, session %d connection %d --> %d\n",
1562 par->session_id, par->connection_id, par->status));
1563 }
1564
1565
1566 /*
1567 * get_version:
1568 * Handle the get_version ioctl.
1569 *
1570 * Parameter:
1571 * par IN/OUT: The version parameters
1572 */
1573
1574 static void
1575 get_version(iscsi_get_version_parameters_t *par)
1576 {
1577 par->status = ISCSI_STATUS_SUCCESS;
1578 par->interface_version = INTERFACE_VERSION;
1579 par->major = VERSION_MAJOR;
1580 par->minor = VERSION_MINOR;
1581 strlcpy(par->version_string, VERSION_STRING,
1582 sizeof(par->version_string));
1583 }
1584
1585
1586 /* -------------------------------------------------------------------- */
1587
1588 /*
1589 * kill_all_sessions:
1590 * Terminate all sessions (called when the driver unloads).
1591 */
1592
1593 int
1594 kill_all_sessions(void)
1595 {
1596 session_t *sess;
1597 int rc = 0;
1598
1599 mutex_enter(&iscsi_cleanup_mtx);
1600 while ((sess = TAILQ_FIRST(&iscsi_sessions)) != NULL) {
1601 mutex_exit(&iscsi_cleanup_mtx);
1602 kill_session(sess, ISCSI_STATUS_DRIVER_UNLOAD, LOGOUT_SESSION,
1603 FALSE);
1604 mutex_enter(&iscsi_cleanup_mtx);
1605 }
1606 if (TAILQ_FIRST(&iscsi_sessions) != NULL) {
1607 DEBOUT(("Failed to kill all sessions\n"));
1608 rc = EBUSY;
1609 }
1610 mutex_exit(&iscsi_cleanup_mtx);
1611
1612 return rc;
1613 }
1614
1615 /*
1616 * handle_connection_error:
1617 * Deal with a problem during send or receive.
1618 *
1619 * Parameter:
1620 * conn The connection the problem is associated with
1621 * status The status code to insert into any unfinished CCBs
1622 * dologout Whether Logout should be attempted
1623 */
1624
1625 void
1626 handle_connection_error(connection_t *conn, uint32_t status, int dologout)
1627 {
1628
1629 DEBC(conn, 0, ("*** Connection Error, status=%d, logout=%d, state=%d\n",
1630 status, dologout, conn->state));
1631
1632 if (!conn->terminating && conn->state <= ST_LOGOUT_SENT) {
1633 /* if we get an error while winding down, escalate it */
1634 if (dologout >= 0 && conn->state >= ST_WINDING_DOWN) {
1635 dologout = NO_LOGOUT;
1636 }
1637 kill_connection(conn, status, dologout, TRUE);
1638 }
1639 }
1640
1641 /*
1642 * remove a connection from session and add to the cleanup list
1643 */
1644 void
1645 add_connection_cleanup(connection_t *conn)
1646 {
1647 session_t *sess;
1648
1649 mutex_enter(&iscsi_cleanup_mtx);
1650 if (conn->in_session) {
1651 sess = conn->session;
1652 conn->in_session = FALSE;
1653 conn->session = NULL;
1654 TAILQ_REMOVE(&sess->conn_list, conn, connections);
1655 sess->mru_connection = TAILQ_FIRST(&sess->conn_list);
1656 }
1657 TAILQ_INSERT_TAIL(&iscsi_cleanupc_list, conn, connections);
1658 iscsi_notify_cleanup();
1659 mutex_exit(&iscsi_cleanup_mtx);
1660 }
1661
1662 /*
1663 * callout wrappers for timeouts, the work is done by the cleanup thread
1664 */
1665 void
1666 connection_timeout_co(void *par)
1667 {
1668 connection_t *conn = par;
1669
1670 mutex_enter(&iscsi_cleanup_mtx);
1671 conn->timedout = TOUT_QUEUED;
1672 TAILQ_INSERT_TAIL(&iscsi_timeout_conn_list, conn, tchain);
1673 iscsi_notify_cleanup();
1674 mutex_exit(&iscsi_cleanup_mtx);
1675 }
1676
1677 void
1678 connection_timeout_start(connection_t *conn, int ticks)
1679 {
1680 mutex_enter(&iscsi_cleanup_mtx);
1681 if (conn->timedout != TOUT_QUEUED) {
1682 conn->timedout = TOUT_ARMED;
1683 callout_schedule(&conn->timeout, ticks);
1684 }
1685 mutex_exit(&iscsi_cleanup_mtx);
1686 }
1687
1688 void
1689 connection_timeout_stop(connection_t *conn)
1690 {
1691 callout_stop(&conn->timeout);
1692 mutex_enter(&iscsi_cleanup_mtx);
1693 if (conn->timedout == TOUT_QUEUED) {
1694 TAILQ_REMOVE(&iscsi_timeout_conn_list, conn, tchain);
1695 conn->timedout = TOUT_NONE;
1696 }
1697 if (curlwp != iscsi_cleanproc) {
1698 while (conn->timedout == TOUT_BUSY)
1699 kpause("connbusy", false, 1, &iscsi_cleanup_mtx);
1700 }
1701 mutex_exit(&iscsi_cleanup_mtx);
1702 }
1703
1704 void
1705 ccb_timeout_co(void *par)
1706 {
1707 ccb_t *ccb = par;
1708
1709 mutex_enter(&iscsi_cleanup_mtx);
1710 ccb->timedout = TOUT_QUEUED;
1711 TAILQ_INSERT_TAIL(&iscsi_timeout_ccb_list, ccb, tchain);
1712 iscsi_notify_cleanup();
1713 mutex_exit(&iscsi_cleanup_mtx);
1714 }
1715
1716 void
1717 ccb_timeout_start(ccb_t *ccb, int ticks)
1718 {
1719 mutex_enter(&iscsi_cleanup_mtx);
1720 if (ccb->timedout != TOUT_QUEUED) {
1721 ccb->timedout = TOUT_ARMED;
1722 callout_schedule(&ccb->timeout, ticks);
1723 }
1724 mutex_exit(&iscsi_cleanup_mtx);
1725 }
1726
1727 void
1728 ccb_timeout_stop(ccb_t *ccb)
1729 {
1730 callout_stop(&ccb->timeout);
1731 mutex_enter(&iscsi_cleanup_mtx);
1732 if (ccb->timedout == TOUT_QUEUED) {
1733 TAILQ_REMOVE(&iscsi_timeout_ccb_list, ccb, tchain);
1734 ccb->timedout = TOUT_NONE;
1735 }
1736 if (curlwp != iscsi_cleanproc) {
1737 while (ccb->timedout == TOUT_BUSY)
1738 kpause("ccbbusy", false, 1, &iscsi_cleanup_mtx);
1739 }
1740 mutex_exit(&iscsi_cleanup_mtx);
1741 }
1742
1743 /*
1744 * iscsi_cleanup_thread
1745 * Global thread to handle connection and session cleanup after termination.
1746 */
1747
1748 static void
1749 iscsi_cleanup_thread(void *par)
1750 {
1751 int s, rc;
1752 session_t *sess, *nxts;
1753 connection_t *conn, *nxtc;
1754 ccb_t *ccb;
1755
1756 mutex_enter(&iscsi_cleanup_mtx);
1757 while (iscsi_num_send_threads || !iscsi_detaching ||
1758 !TAILQ_EMPTY(&iscsi_cleanupc_list) || !TAILQ_EMPTY(&iscsi_cleanups_list)) {
1759 TAILQ_FOREACH_SAFE(conn, &iscsi_cleanupc_list, connections, nxtc) {
1760
1761 TAILQ_REMOVE(&iscsi_cleanupc_list, conn, connections);
1762 mutex_exit(&iscsi_cleanup_mtx);
1763
1764 sess = conn->session;
1765
1766 /*
1767 * This implies that connection cleanup only runs when
1768 * the send/recv threads have been killed
1769 */
1770 DEBC(conn, 5, ("Cleanup: Waiting for threads to exit\n"));
1771 while (conn->sendproc || conn->rcvproc)
1772 kpause("threads", false, hz, NULL);
1773
1774 for (s=1; conn->usecount > 0 && s < 3; ++s)
1775 kpause("usecount", false, hz, NULL);
1776
1777 if (conn->usecount > 0) {
1778 DEBC(conn, 5, ("Cleanup: %d CCBs busy\n", conn->usecount));
1779 /* retry later */
1780 mutex_enter(&iscsi_cleanup_mtx);
1781 TAILQ_INSERT_HEAD(&iscsi_cleanupc_list, conn, connections);
1782 continue;
1783 }
1784
1785 KASSERT(!conn->in_session);
1786
1787 callout_halt(&conn->timeout, &iscsi_cleanup_mtx);
1788 closef(conn->sock);
1789 callout_destroy(&conn->timeout);
1790 cv_destroy(&conn->idle_cv);
1791 cv_destroy(&conn->ccb_cv);
1792 cv_destroy(&conn->pdu_cv);
1793 cv_destroy(&conn->conn_cv);
1794 mutex_destroy(&conn->lock);
1795 free(conn, M_DEVBUF);
1796
1797 mutex_enter(&iscsi_cleanup_mtx);
1798
1799 if (--sess->total_connections == 0) {
1800 DEB(1, ("Cleanup: session %d\n", sess->id));
1801 if (!sess->terminating) {
1802 sess->terminating = ISCSI_CONNECTION_TERMINATED;
1803 KASSERT(sess->sessions.tqe_prev != NULL);
1804 TAILQ_REMOVE(&iscsi_sessions, sess, sessions);
1805 sess->sessions.tqe_next = NULL;
1806 sess->sessions.tqe_prev = NULL;
1807 }
1808 KASSERT(sess->sessions.tqe_prev == NULL);
1809 TAILQ_INSERT_HEAD(&iscsi_cleanups_list, sess, sessions);
1810 }
1811 }
1812
1813 TAILQ_FOREACH_SAFE(sess, &iscsi_cleanups_list, sessions, nxts) {
1814 if (sess->refcount > 0)
1815 continue;
1816 TAILQ_REMOVE(&iscsi_cleanups_list, sess, sessions);
1817 sess->sessions.tqe_next = NULL;
1818 sess->sessions.tqe_prev = NULL;
1819 mutex_exit(&iscsi_cleanup_mtx);
1820
1821 DEB(1, ("Cleanup: Unmap session %d\n", sess->id));
1822 if (unmap_session(sess) == 0) {
1823 DEB(1, ("Cleanup: Unmap session %d failed\n", sess->id));
1824 mutex_enter(&iscsi_cleanup_mtx);
1825 TAILQ_INSERT_HEAD(&iscsi_cleanups_list, sess, sessions);
1826 continue;
1827 }
1828
1829 if (sess->target_list != NULL)
1830 free(sess->target_list, M_TEMP);
1831
1832 /* notify event handlers of session shutdown */
1833 add_event(ISCSI_SESSION_TERMINATED, sess->id, 0, sess->terminating);
1834 DEB(1, ("Cleanup: session ended %d\n", sess->id));
1835
1836 destroy_ccbs(sess);
1837 cv_destroy(&sess->ccb_cv);
1838 cv_destroy(&sess->sess_cv);
1839 mutex_destroy(&sess->lock);
1840 free(sess, M_DEVBUF);
1841
1842 mutex_enter(&iscsi_cleanup_mtx);
1843 }
1844
1845 /* handle ccb timeouts */
1846 while ((ccb = TAILQ_FIRST(&iscsi_timeout_ccb_list)) != NULL) {
1847 TAILQ_REMOVE(&iscsi_timeout_ccb_list, ccb, tchain);
1848 KASSERT(ccb->timedout == TOUT_QUEUED);
1849 ccb->timedout = TOUT_BUSY;
1850 mutex_exit(&iscsi_cleanup_mtx);
1851 ccb_timeout(ccb);
1852 mutex_enter(&iscsi_cleanup_mtx);
1853 if (ccb->timedout == TOUT_BUSY)
1854 ccb->timedout = TOUT_NONE;
1855 }
1856
1857 /* handle connection timeouts */
1858 while ((conn = TAILQ_FIRST(&iscsi_timeout_conn_list)) != NULL) {
1859 TAILQ_REMOVE(&iscsi_timeout_conn_list, conn, tchain);
1860 KASSERT(conn->timedout == TOUT_QUEUED);
1861 conn->timedout = TOUT_BUSY;
1862 mutex_exit(&iscsi_cleanup_mtx);
1863 connection_timeout(conn);
1864 mutex_enter(&iscsi_cleanup_mtx);
1865 if (conn->timedout == TOUT_BUSY)
1866 conn->timedout = TOUT_NONE;
1867 }
1868
1869 /* Go to sleep, but wake up every 30 seconds to
1870 * check for dead event handlers */
1871 rc = cv_timedwait(&iscsi_cleanup_cv, &iscsi_cleanup_mtx,
1872 (TAILQ_FIRST(&event_handlers)) ? 120 * hz : 0);
1873
1874 /* if timed out, not woken up */
1875 if (rc == EWOULDBLOCK)
1876 check_event_handlers();
1877 }
1878 mutex_exit(&iscsi_cleanup_mtx);
1879
1880 add_event(ISCSI_DRIVER_TERMINATING, 0, 0, ISCSI_STATUS_DRIVER_UNLOAD);
1881
1882 /*
1883 * Wait for all event handlers to deregister, but don't wait more
1884 * than 1 minute (assume registering app has died if it takes longer).
1885 */
1886 mutex_enter(&iscsi_cleanup_mtx);
1887 for (s = 0; TAILQ_FIRST(&event_handlers) != NULL && s < 60; s++)
1888 kpause("waiteventclr", true, hz, &iscsi_cleanup_mtx);
1889 mutex_exit(&iscsi_cleanup_mtx);
1890
1891 iscsi_cleanproc = NULL;
1892 DEB(5, ("Cleanup thread exits\n"));
1893 kthread_exit(0);
1894 }
1895
1896 void
1897 iscsi_init_cleanup(void)
1898 {
1899
1900 mutex_init(&iscsi_cleanup_mtx, MUTEX_DEFAULT, IPL_BIO);
1901 cv_init(&iscsi_cleanup_cv, "cleanup");
1902 cv_init(&iscsi_event_cv, "iscsievtwait");
1903
1904 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, iscsi_cleanup_thread,
1905 NULL, &iscsi_cleanproc, "iscsi_cleanup") != 0) {
1906 panic("Can't create cleanup thread!");
1907 }
1908 }
1909
1910 int
1911 iscsi_destroy_cleanup(void)
1912 {
1913
1914 iscsi_detaching = true;
1915 mutex_enter(&iscsi_cleanup_mtx);
1916 while (iscsi_cleanproc != NULL) {
1917 iscsi_notify_cleanup();
1918 kpause("detach_wait", false, hz, &iscsi_cleanup_mtx);
1919 }
1920 mutex_exit(&iscsi_cleanup_mtx);
1921
1922 cv_destroy(&iscsi_event_cv);
1923 cv_destroy(&iscsi_cleanup_cv);
1924 mutex_destroy(&iscsi_cleanup_mtx);
1925
1926 return 0;
1927 }
1928
1929 void
1930 iscsi_notify_cleanup(void)
1931 {
1932 KASSERT(mutex_owned(&iscsi_cleanup_mtx));
1933
1934 cv_signal(&iscsi_cleanup_cv);
1935 }
1936
1937
1938 /* -------------------------------------------------------------------- */
1939
1940 /*
1941 * iscsi_ioctl:
1942 * Driver ioctl entry.
1943 *
1944 * Parameter:
1945 * file File structure
1946 * cmd The ioctl Command
1947 * addr IN/OUT: The command parameter
1948 * flag Flags (ignored)
1949 * l IN: The lwp object of the caller
1950 */
1951
1952 int
1953 iscsiioctl(struct file *fp, u_long cmd, void *addr)
1954 {
1955 struct lwp *l = curlwp;
1956 struct iscsifd *d = fp->f_iscsi;
1957
1958 DEB(1, ("ISCSI Ioctl cmd = %x\n", (int) cmd));
1959
1960 switch (cmd) {
1961 case ISCSI_GET_VERSION:
1962 get_version((iscsi_get_version_parameters_t *) addr);
1963 break;
1964
1965 case ISCSI_LOGIN:
1966 login((iscsi_login_parameters_t *) addr, l, d->dev);
1967 break;
1968
1969 case ISCSI_ADD_CONNECTION:
1970 add_connection((iscsi_login_parameters_t *) addr, l);
1971 break;
1972
1973 case ISCSI_RESTORE_CONNECTION:
1974 restore_connection((iscsi_login_parameters_t *) addr, l);
1975 break;
1976
1977 case ISCSI_LOGOUT:
1978 logout((iscsi_logout_parameters_t *) addr);
1979 break;
1980
1981 case ISCSI_REMOVE_CONNECTION:
1982 remove_connection((iscsi_remove_parameters_t *) addr);
1983 break;
1984
1985 #ifndef ISCSI_MINIMAL
1986 case ISCSI_IO_COMMAND:
1987 io_command((iscsi_iocommand_parameters_t *) addr, l);
1988 break;
1989 #endif
1990
1991 case ISCSI_SEND_TARGETS:
1992 send_targets((iscsi_send_targets_parameters_t *) addr);
1993 break;
1994
1995 case ISCSI_SET_NODE_NAME:
1996 set_node_name((iscsi_set_node_name_parameters_t *) addr);
1997 break;
1998
1999 case ISCSI_CONNECTION_STATUS:
2000 connection_status((iscsi_conn_status_parameters_t *) addr);
2001 break;
2002
2003 case ISCSI_REGISTER_EVENT:
2004 register_event((iscsi_register_event_parameters_t *) addr);
2005 break;
2006
2007 case ISCSI_DEREGISTER_EVENT:
2008 deregister_event((iscsi_register_event_parameters_t *) addr);
2009 break;
2010
2011 case ISCSI_WAIT_EVENT:
2012 check_event((iscsi_wait_event_parameters_t *) addr, TRUE);
2013 break;
2014
2015 case ISCSI_POLL_EVENT:
2016 check_event((iscsi_wait_event_parameters_t *) addr, FALSE);
2017 break;
2018
2019 default:
2020 DEBOUT(("Invalid IO-Control Code\n"));
2021 return ENOTTY;
2022 }
2023
2024 /*
2025 * NOTE: We return 0 even if the function fails as long as the ioctl code
2026 * is good, so the status code is copied back to the caller.
2027 */
2028 return 0;
2029 }
2030