1 /*
2  * iSCSI transport class definitions
3  *
4  * Copyright (C) IBM Corporation, 2004
5  * Copyright (C) Mike Christie, 2004 - 2005
6  * Copyright (C) Dmitry Yusupov, 2004 - 2005
7  * Copyright (C) Alex Aizman, 2004 - 2005
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <net/tcp.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_iscsi.h>
31 #include <scsi/iscsi_if.h>
32 
33 #define ISCSI_SESSION_ATTRS 19
34 #define ISCSI_CONN_ATTRS 13
35 #define ISCSI_HOST_ATTRS 4
36 #define ISCSI_TRANSPORT_VERSION "2.0-869"
37 
38 struct iscsi_internal {
39 	int daemon_pid;
40 	struct scsi_transport_template t;
41 	struct iscsi_transport *iscsi_transport;
42 	struct list_head list;
43 	struct device dev;
44 
45 	struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
46 	struct transport_container conn_cont;
47 	struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 	struct transport_container session_cont;
49 	struct device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
50 };
51 
52 static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
53 static struct workqueue_struct *iscsi_eh_timer_workq;
54 
55 /*
56  * list of registered transports and lock that must
57  * be held while accessing list. The iscsi_transport_lock must
58  * be acquired after the rx_queue_mutex.
59  */
60 static LIST_HEAD(iscsi_transports);
61 static DEFINE_SPINLOCK(iscsi_transport_lock);
62 
63 #define to_iscsi_internal(tmpl) \
64 	container_of(tmpl, struct iscsi_internal, t)
65 
66 #define dev_to_iscsi_internal(_dev) \
67 	container_of(_dev, struct iscsi_internal, dev)
68 
69 static void iscsi_transport_release(struct device *dev)
70 {
71 	struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
72 	kfree(priv);
73 }
74 
75 /*
76  * iscsi_transport_class represents the iscsi_transports that are
77  * registered.
78  */
79 static struct class iscsi_transport_class = {
80 	.name = "iscsi_transport",
81 	.dev_release = iscsi_transport_release,
82 };
83 
84 static ssize_t
85 show_transport_handle(struct device *dev, struct device_attribute *attr,
86 		      char *buf)
87 {
88 	struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
89 	return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
90 }
91 static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
92 
93 #define show_transport_attr(name, format)				\
94 static ssize_t								\
95 show_transport_##name(struct device *dev, 				\
96 		      struct device_attribute *attr,char *buf)		\
97 {									\
98 	struct iscsi_internal *priv = dev_to_iscsi_internal(dev);	\
99 	return sprintf(buf, format"\n", priv->iscsi_transport->name);	\
100 }									\
101 static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
102 
103 show_transport_attr(caps, "0x%x");
104 show_transport_attr(max_lun, "%d");
105 show_transport_attr(max_conn, "%d");
106 show_transport_attr(max_cmd_len, "%d");
107 
108 static struct attribute *iscsi_transport_attrs[] = {
109 	&dev_attr_handle.attr,
110 	&dev_attr_caps.attr,
111 	&dev_attr_max_lun.attr,
112 	&dev_attr_max_conn.attr,
113 	&dev_attr_max_cmd_len.attr,
114 	NULL,
115 };
116 
117 static struct attribute_group iscsi_transport_group = {
118 	.attrs = iscsi_transport_attrs,
119 };
120 
121 
122 
123 static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
124 			    struct device *cdev)
125 {
126 	struct Scsi_Host *shost = dev_to_shost(dev);
127 	struct iscsi_host *ihost = shost->shost_data;
128 
129 	memset(ihost, 0, sizeof(*ihost));
130 	INIT_LIST_HEAD(&ihost->sessions);
131 	mutex_init(&ihost->mutex);
132 	atomic_set(&ihost->nr_scans, 0);
133 
134 	snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
135 		shost->host_no);
136 	ihost->scan_workq = create_singlethread_workqueue(
137 						ihost->scan_workq_name);
138 	if (!ihost->scan_workq)
139 		return -ENOMEM;
140 	return 0;
141 }
142 
143 static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
144 			     struct device *cdev)
145 {
146 	struct Scsi_Host *shost = dev_to_shost(dev);
147 	struct iscsi_host *ihost = shost->shost_data;
148 
149 	destroy_workqueue(ihost->scan_workq);
150 	return 0;
151 }
152 
153 static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
154 			       "iscsi_host",
155 			       iscsi_setup_host,
156 			       iscsi_remove_host,
157 			       NULL);
158 
159 static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
160 			       "iscsi_session",
161 			       NULL,
162 			       NULL,
163 			       NULL);
164 
165 static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
166 			       "iscsi_connection",
167 			       NULL,
168 			       NULL,
169 			       NULL);
170 
171 static struct sock *nls;
172 static DEFINE_MUTEX(rx_queue_mutex);
173 
174 static LIST_HEAD(sesslist);
175 static DEFINE_SPINLOCK(sesslock);
176 static LIST_HEAD(connlist);
177 static DEFINE_SPINLOCK(connlock);
178 
179 static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
180 {
181 	struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
182 	return sess->sid;
183 }
184 
185 /*
186  * Returns the matching session to a given sid
187  */
188 static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
189 {
190 	unsigned long flags;
191 	struct iscsi_cls_session *sess;
192 
193 	spin_lock_irqsave(&sesslock, flags);
194 	list_for_each_entry(sess, &sesslist, sess_list) {
195 		if (sess->sid == sid) {
196 			spin_unlock_irqrestore(&sesslock, flags);
197 			return sess;
198 		}
199 	}
200 	spin_unlock_irqrestore(&sesslock, flags);
201 	return NULL;
202 }
203 
204 /*
205  * Returns the matching connection to a given sid / cid tuple
206  */
207 static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
208 {
209 	unsigned long flags;
210 	struct iscsi_cls_conn *conn;
211 
212 	spin_lock_irqsave(&connlock, flags);
213 	list_for_each_entry(conn, &connlist, conn_list) {
214 		if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
215 			spin_unlock_irqrestore(&connlock, flags);
216 			return conn;
217 		}
218 	}
219 	spin_unlock_irqrestore(&connlock, flags);
220 	return NULL;
221 }
222 
223 /*
224  * The following functions can be used by LLDs that allocate
225  * their own scsi_hosts or by software iscsi LLDs
226  */
227 static struct {
228 	int value;
229 	char *name;
230 } iscsi_session_state_names[] = {
231 	{ ISCSI_SESSION_LOGGED_IN,	"LOGGED_IN" },
232 	{ ISCSI_SESSION_FAILED,		"FAILED" },
233 	{ ISCSI_SESSION_FREE,		"FREE" },
234 };
235 
236 static const char *iscsi_session_state_name(int state)
237 {
238 	int i;
239 	char *name = NULL;
240 
241 	for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
242 		if (iscsi_session_state_names[i].value == state) {
243 			name = iscsi_session_state_names[i].name;
244 			break;
245 		}
246 	}
247 	return name;
248 }
249 
250 int iscsi_session_chkready(struct iscsi_cls_session *session)
251 {
252 	unsigned long flags;
253 	int err;
254 
255 	spin_lock_irqsave(&session->lock, flags);
256 	switch (session->state) {
257 	case ISCSI_SESSION_LOGGED_IN:
258 		err = 0;
259 		break;
260 	case ISCSI_SESSION_FAILED:
261 		err = DID_IMM_RETRY << 16;
262 		break;
263 	case ISCSI_SESSION_FREE:
264 		err = DID_NO_CONNECT << 16;
265 		break;
266 	default:
267 		err = DID_NO_CONNECT << 16;
268 		break;
269 	}
270 	spin_unlock_irqrestore(&session->lock, flags);
271 	return err;
272 }
273 EXPORT_SYMBOL_GPL(iscsi_session_chkready);
274 
275 static void iscsi_session_release(struct device *dev)
276 {
277 	struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
278 	struct Scsi_Host *shost;
279 
280 	shost = iscsi_session_to_shost(session);
281 	scsi_host_put(shost);
282 	kfree(session);
283 }
284 
285 static int iscsi_is_session_dev(const struct device *dev)
286 {
287 	return dev->release == iscsi_session_release;
288 }
289 
290 /**
291  * iscsi_scan_finished - helper to report when running scans are done
292  * @shost: scsi host
293  * @time: scan run time
294  *
295  * This function can be used by drives like qla4xxx to report to the scsi
296  * layer when the scans it kicked off at module load time are done.
297  */
298 int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
299 {
300 	struct iscsi_host *ihost = shost->shost_data;
301 	/*
302 	 * qla4xxx will have kicked off some session unblocks before calling
303 	 * scsi_scan_host, so just wait for them to complete.
304 	 */
305 	return !atomic_read(&ihost->nr_scans);
306 }
307 EXPORT_SYMBOL_GPL(iscsi_scan_finished);
308 
309 static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
310 			   uint id, uint lun)
311 {
312 	struct iscsi_host *ihost = shost->shost_data;
313 	struct iscsi_cls_session *session;
314 
315 	mutex_lock(&ihost->mutex);
316 	list_for_each_entry(session, &ihost->sessions, host_list) {
317 		if ((channel == SCAN_WILD_CARD || channel == 0) &&
318 		    (id == SCAN_WILD_CARD || id == session->target_id))
319 			scsi_scan_target(&session->dev, 0,
320 					 session->target_id, lun, 1);
321 	}
322 	mutex_unlock(&ihost->mutex);
323 
324 	return 0;
325 }
326 
327 static void iscsi_scan_session(struct work_struct *work)
328 {
329 	struct iscsi_cls_session *session =
330 			container_of(work, struct iscsi_cls_session, scan_work);
331 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
332 	struct iscsi_host *ihost = shost->shost_data;
333 	unsigned long flags;
334 
335 	spin_lock_irqsave(&session->lock, flags);
336 	if (session->state != ISCSI_SESSION_LOGGED_IN) {
337 		spin_unlock_irqrestore(&session->lock, flags);
338 		goto done;
339 	}
340 	spin_unlock_irqrestore(&session->lock, flags);
341 
342 	scsi_scan_target(&session->dev, 0, session->target_id,
343 			 SCAN_WILD_CARD, 1);
344 done:
345 	atomic_dec(&ihost->nr_scans);
346 }
347 
348 static void session_recovery_timedout(struct work_struct *work)
349 {
350 	struct iscsi_cls_session *session =
351 		container_of(work, struct iscsi_cls_session,
352 			     recovery_work.work);
353 	unsigned long flags;
354 
355 	iscsi_cls_session_printk(KERN_INFO, session,
356 				 "session recovery timed out after %d secs\n",
357 				 session->recovery_tmo);
358 
359 	spin_lock_irqsave(&session->lock, flags);
360 	switch (session->state) {
361 	case ISCSI_SESSION_FAILED:
362 		session->state = ISCSI_SESSION_FREE;
363 		break;
364 	case ISCSI_SESSION_LOGGED_IN:
365 	case ISCSI_SESSION_FREE:
366 		/* we raced with the unblock's flush */
367 		spin_unlock_irqrestore(&session->lock, flags);
368 		return;
369 	}
370 	spin_unlock_irqrestore(&session->lock, flags);
371 
372 	if (session->transport->session_recovery_timedout)
373 		session->transport->session_recovery_timedout(session);
374 
375 	scsi_target_unblock(&session->dev);
376 }
377 
378 static void __iscsi_unblock_session(struct work_struct *work)
379 {
380 	struct iscsi_cls_session *session =
381 			container_of(work, struct iscsi_cls_session,
382 				     unblock_work);
383 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
384 	struct iscsi_host *ihost = shost->shost_data;
385 	unsigned long flags;
386 
387 	/*
388 	 * The recovery and unblock work get run from the same workqueue,
389 	 * so try to cancel it if it was going to run after this unblock.
390 	 */
391 	cancel_delayed_work(&session->recovery_work);
392 	spin_lock_irqsave(&session->lock, flags);
393 	session->state = ISCSI_SESSION_LOGGED_IN;
394 	spin_unlock_irqrestore(&session->lock, flags);
395 	/* start IO */
396 	scsi_target_unblock(&session->dev);
397 	/*
398 	 * Only do kernel scanning if the driver is properly hooked into
399 	 * the async scanning code (drivers like iscsi_tcp do login and
400 	 * scanning from userspace).
401 	 */
402 	if (shost->hostt->scan_finished) {
403 		if (queue_work(ihost->scan_workq, &session->scan_work))
404 			atomic_inc(&ihost->nr_scans);
405 	}
406 }
407 
408 /**
409  * iscsi_unblock_session - set a session as logged in and start IO.
410  * @session: iscsi session
411  *
412  * Mark a session as ready to accept IO.
413  */
414 void iscsi_unblock_session(struct iscsi_cls_session *session)
415 {
416 	queue_work(iscsi_eh_timer_workq, &session->unblock_work);
417 	/*
418 	 * make sure all the events have completed before tell the driver
419 	 * it is safe
420 	 */
421 	flush_workqueue(iscsi_eh_timer_workq);
422 }
423 EXPORT_SYMBOL_GPL(iscsi_unblock_session);
424 
425 static void __iscsi_block_session(struct work_struct *work)
426 {
427 	struct iscsi_cls_session *session =
428 			container_of(work, struct iscsi_cls_session,
429 				     block_work);
430 	unsigned long flags;
431 
432 	spin_lock_irqsave(&session->lock, flags);
433 	session->state = ISCSI_SESSION_FAILED;
434 	spin_unlock_irqrestore(&session->lock, flags);
435 	scsi_target_block(&session->dev);
436 	queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
437 			   session->recovery_tmo * HZ);
438 }
439 
440 void iscsi_block_session(struct iscsi_cls_session *session)
441 {
442 	queue_work(iscsi_eh_timer_workq, &session->block_work);
443 }
444 EXPORT_SYMBOL_GPL(iscsi_block_session);
445 
446 static void __iscsi_unbind_session(struct work_struct *work)
447 {
448 	struct iscsi_cls_session *session =
449 			container_of(work, struct iscsi_cls_session,
450 				     unbind_work);
451 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
452 	struct iscsi_host *ihost = shost->shost_data;
453 
454 	/* Prevent new scans and make sure scanning is not in progress */
455 	mutex_lock(&ihost->mutex);
456 	if (list_empty(&session->host_list)) {
457 		mutex_unlock(&ihost->mutex);
458 		return;
459 	}
460 	list_del_init(&session->host_list);
461 	mutex_unlock(&ihost->mutex);
462 
463 	scsi_remove_target(&session->dev);
464 	iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
465 }
466 
467 static int iscsi_unbind_session(struct iscsi_cls_session *session)
468 {
469 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
470 	struct iscsi_host *ihost = shost->shost_data;
471 
472 	return queue_work(ihost->scan_workq, &session->unbind_work);
473 }
474 
475 struct iscsi_cls_session *
476 iscsi_alloc_session(struct Scsi_Host *shost,
477 		    struct iscsi_transport *transport)
478 {
479 	struct iscsi_cls_session *session;
480 
481 	session = kzalloc(sizeof(*session) + transport->sessiondata_size,
482 			  GFP_KERNEL);
483 	if (!session)
484 		return NULL;
485 
486 	session->transport = transport;
487 	session->recovery_tmo = 120;
488 	session->state = ISCSI_SESSION_FREE;
489 	INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
490 	INIT_LIST_HEAD(&session->host_list);
491 	INIT_LIST_HEAD(&session->sess_list);
492 	INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
493 	INIT_WORK(&session->block_work, __iscsi_block_session);
494 	INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
495 	INIT_WORK(&session->scan_work, iscsi_scan_session);
496 	spin_lock_init(&session->lock);
497 
498 	/* this is released in the dev's release function */
499 	scsi_host_get(shost);
500 	session->dev.parent = &shost->shost_gendev;
501 	session->dev.release = iscsi_session_release;
502 	device_initialize(&session->dev);
503 	if (transport->sessiondata_size)
504 		session->dd_data = &session[1];
505 	return session;
506 }
507 EXPORT_SYMBOL_GPL(iscsi_alloc_session);
508 
509 int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
510 {
511 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
512 	struct iscsi_host *ihost;
513 	unsigned long flags;
514 	int err;
515 
516 	ihost = shost->shost_data;
517 	session->sid = atomic_add_return(1, &iscsi_session_nr);
518 	session->target_id = target_id;
519 
520 	snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
521 		 session->sid);
522 	err = device_add(&session->dev);
523 	if (err) {
524 		iscsi_cls_session_printk(KERN_ERR, session,
525 					 "could not register session's dev\n");
526 		goto release_host;
527 	}
528 	transport_register_device(&session->dev);
529 
530 	spin_lock_irqsave(&sesslock, flags);
531 	list_add(&session->sess_list, &sesslist);
532 	spin_unlock_irqrestore(&sesslock, flags);
533 
534 	mutex_lock(&ihost->mutex);
535 	list_add(&session->host_list, &ihost->sessions);
536 	mutex_unlock(&ihost->mutex);
537 
538 	iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
539 	return 0;
540 
541 release_host:
542 	scsi_host_put(shost);
543 	return err;
544 }
545 EXPORT_SYMBOL_GPL(iscsi_add_session);
546 
547 /**
548  * iscsi_create_session - create iscsi class session
549  * @shost: scsi host
550  * @transport: iscsi transport
551  * @target_id: which target
552  *
553  * This can be called from a LLD or iscsi_transport.
554  */
555 struct iscsi_cls_session *
556 iscsi_create_session(struct Scsi_Host *shost,
557 		     struct iscsi_transport *transport,
558 		     unsigned int target_id)
559 {
560 	struct iscsi_cls_session *session;
561 
562 	session = iscsi_alloc_session(shost, transport);
563 	if (!session)
564 		return NULL;
565 
566 	if (iscsi_add_session(session, target_id)) {
567 		iscsi_free_session(session);
568 		return NULL;
569 	}
570 	return session;
571 }
572 EXPORT_SYMBOL_GPL(iscsi_create_session);
573 
574 static void iscsi_conn_release(struct device *dev)
575 {
576 	struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
577 	struct device *parent = conn->dev.parent;
578 
579 	kfree(conn);
580 	put_device(parent);
581 }
582 
583 static int iscsi_is_conn_dev(const struct device *dev)
584 {
585 	return dev->release == iscsi_conn_release;
586 }
587 
588 static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
589 {
590 	if (!iscsi_is_conn_dev(dev))
591 		return 0;
592 	return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
593 }
594 
595 void iscsi_remove_session(struct iscsi_cls_session *session)
596 {
597 	struct Scsi_Host *shost = iscsi_session_to_shost(session);
598 	struct iscsi_host *ihost = shost->shost_data;
599 	unsigned long flags;
600 	int err;
601 
602 	spin_lock_irqsave(&sesslock, flags);
603 	list_del(&session->sess_list);
604 	spin_unlock_irqrestore(&sesslock, flags);
605 
606 	/* make sure there are no blocks/unblocks queued */
607 	flush_workqueue(iscsi_eh_timer_workq);
608 	/* make sure the timedout callout is not running */
609 	if (!cancel_delayed_work(&session->recovery_work))
610 		flush_workqueue(iscsi_eh_timer_workq);
611 	/*
612 	 * If we are blocked let commands flow again. The lld or iscsi
613 	 * layer should set up the queuecommand to fail commands.
614 	 * We assume that LLD will not be calling block/unblock while
615 	 * removing the session.
616 	 */
617 	spin_lock_irqsave(&session->lock, flags);
618 	session->state = ISCSI_SESSION_FREE;
619 	spin_unlock_irqrestore(&session->lock, flags);
620 
621 	scsi_target_unblock(&session->dev);
622 	/* flush running scans then delete devices */
623 	flush_workqueue(ihost->scan_workq);
624 	__iscsi_unbind_session(&session->unbind_work);
625 
626 	/* hw iscsi may not have removed all connections from session */
627 	err = device_for_each_child(&session->dev, NULL,
628 				    iscsi_iter_destroy_conn_fn);
629 	if (err)
630 		iscsi_cls_session_printk(KERN_ERR, session,
631 					 "Could not delete all connections "
632 					 "for session. Error %d.\n", err);
633 
634 	transport_unregister_device(&session->dev);
635 	device_del(&session->dev);
636 }
637 EXPORT_SYMBOL_GPL(iscsi_remove_session);
638 
639 void iscsi_free_session(struct iscsi_cls_session *session)
640 {
641 	iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
642 	put_device(&session->dev);
643 }
644 EXPORT_SYMBOL_GPL(iscsi_free_session);
645 
646 /**
647  * iscsi_destroy_session - destroy iscsi session
648  * @session: iscsi_session
649  *
650  * Can be called by a LLD or iscsi_transport. There must not be
651  * any running connections.
652  */
653 int iscsi_destroy_session(struct iscsi_cls_session *session)
654 {
655 	iscsi_remove_session(session);
656 	iscsi_free_session(session);
657 	return 0;
658 }
659 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
660 
661 /**
662  * iscsi_create_conn - create iscsi class connection
663  * @session: iscsi cls session
664  * @cid: connection id
665  *
666  * This can be called from a LLD or iscsi_transport. The connection
667  * is child of the session so cid must be unique for all connections
668  * on the session.
669  *
670  * Since we do not support MCS, cid will normally be zero. In some cases
671  * for software iscsi we could be trying to preallocate a connection struct
672  * in which case there could be two connection structs and cid would be
673  * non-zero.
674  */
675 struct iscsi_cls_conn *
676 iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
677 {
678 	struct iscsi_transport *transport = session->transport;
679 	struct iscsi_cls_conn *conn;
680 	unsigned long flags;
681 	int err;
682 
683 	conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
684 	if (!conn)
685 		return NULL;
686 
687 	if (transport->conndata_size)
688 		conn->dd_data = &conn[1];
689 
690 	INIT_LIST_HEAD(&conn->conn_list);
691 	conn->transport = transport;
692 	conn->cid = cid;
693 
694 	/* this is released in the dev's release function */
695 	if (!get_device(&session->dev))
696 		goto free_conn;
697 
698 	snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
699 		 session->sid, cid);
700 	conn->dev.parent = &session->dev;
701 	conn->dev.release = iscsi_conn_release;
702 	err = device_register(&conn->dev);
703 	if (err) {
704 		iscsi_cls_session_printk(KERN_ERR, session, "could not "
705 					 "register connection's dev\n");
706 		goto release_parent_ref;
707 	}
708 	transport_register_device(&conn->dev);
709 
710 	spin_lock_irqsave(&connlock, flags);
711 	list_add(&conn->conn_list, &connlist);
712 	conn->active = 1;
713 	spin_unlock_irqrestore(&connlock, flags);
714 	return conn;
715 
716 release_parent_ref:
717 	put_device(&session->dev);
718 free_conn:
719 	kfree(conn);
720 	return NULL;
721 }
722 
723 EXPORT_SYMBOL_GPL(iscsi_create_conn);
724 
725 /**
726  * iscsi_destroy_conn - destroy iscsi class connection
727  * @conn: iscsi cls session
728  *
729  * This can be called from a LLD or iscsi_transport.
730  */
731 int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
732 {
733 	unsigned long flags;
734 
735 	spin_lock_irqsave(&connlock, flags);
736 	conn->active = 0;
737 	list_del(&conn->conn_list);
738 	spin_unlock_irqrestore(&connlock, flags);
739 
740 	transport_unregister_device(&conn->dev);
741 	device_unregister(&conn->dev);
742 	return 0;
743 }
744 EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
745 
746 /*
747  * iscsi interface functions
748  */
749 static struct iscsi_internal *
750 iscsi_if_transport_lookup(struct iscsi_transport *tt)
751 {
752 	struct iscsi_internal *priv;
753 	unsigned long flags;
754 
755 	spin_lock_irqsave(&iscsi_transport_lock, flags);
756 	list_for_each_entry(priv, &iscsi_transports, list) {
757 		if (tt == priv->iscsi_transport) {
758 			spin_unlock_irqrestore(&iscsi_transport_lock, flags);
759 			return priv;
760 		}
761 	}
762 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
763 	return NULL;
764 }
765 
766 static int
767 iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
768 {
769 	int rc;
770 
771 	rc = netlink_broadcast(nls, skb, 0, 1, gfp);
772 	if (rc < 0) {
773 		printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
774 		return rc;
775 	}
776 
777 	return 0;
778 }
779 
780 static int
781 iscsi_unicast_skb(struct sk_buff *skb, int pid)
782 {
783 	int rc;
784 
785 	rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
786 	if (rc < 0) {
787 		printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
788 		return rc;
789 	}
790 
791 	return 0;
792 }
793 
794 int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
795 		   char *data, uint32_t data_size)
796 {
797 	struct nlmsghdr	*nlh;
798 	struct sk_buff *skb;
799 	struct iscsi_uevent *ev;
800 	char *pdu;
801 	struct iscsi_internal *priv;
802 	int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
803 			      data_size);
804 
805 	priv = iscsi_if_transport_lookup(conn->transport);
806 	if (!priv)
807 		return -EINVAL;
808 
809 	skb = alloc_skb(len, GFP_ATOMIC);
810 	if (!skb) {
811 		iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
812 		iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
813 				      "control PDU: OOM\n");
814 		return -ENOMEM;
815 	}
816 
817 	nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
818 	ev = NLMSG_DATA(nlh);
819 	memset(ev, 0, sizeof(*ev));
820 	ev->transport_handle = iscsi_handle(conn->transport);
821 	ev->type = ISCSI_KEVENT_RECV_PDU;
822 	ev->r.recv_req.cid = conn->cid;
823 	ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
824 	pdu = (char*)ev + sizeof(*ev);
825 	memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
826 	memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
827 
828 	return iscsi_unicast_skb(skb, priv->daemon_pid);
829 }
830 EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
831 
832 void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
833 {
834 	struct nlmsghdr	*nlh;
835 	struct sk_buff	*skb;
836 	struct iscsi_uevent *ev;
837 	struct iscsi_internal *priv;
838 	int len = NLMSG_SPACE(sizeof(*ev));
839 
840 	priv = iscsi_if_transport_lookup(conn->transport);
841 	if (!priv)
842 		return;
843 
844 	skb = alloc_skb(len, GFP_ATOMIC);
845 	if (!skb) {
846 		iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
847 				      "conn error (%d)\n", error);
848 		return;
849 	}
850 
851 	nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
852 	ev = NLMSG_DATA(nlh);
853 	ev->transport_handle = iscsi_handle(conn->transport);
854 	ev->type = ISCSI_KEVENT_CONN_ERROR;
855 	ev->r.connerror.error = error;
856 	ev->r.connerror.cid = conn->cid;
857 	ev->r.connerror.sid = iscsi_conn_get_sid(conn);
858 
859 	iscsi_broadcast_skb(skb, GFP_ATOMIC);
860 
861 	iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
862 			      error);
863 }
864 EXPORT_SYMBOL_GPL(iscsi_conn_error);
865 
866 static int
867 iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
868 		      void *payload, int size)
869 {
870 	struct sk_buff	*skb;
871 	struct nlmsghdr	*nlh;
872 	int len = NLMSG_SPACE(size);
873 	int flags = multi ? NLM_F_MULTI : 0;
874 	int t = done ? NLMSG_DONE : type;
875 
876 	skb = alloc_skb(len, GFP_ATOMIC);
877 	if (!skb) {
878 		printk(KERN_ERR "Could not allocate skb to send reply.\n");
879 		return -ENOMEM;
880 	}
881 
882 	nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
883 	nlh->nlmsg_flags = flags;
884 	memcpy(NLMSG_DATA(nlh), payload, size);
885 	return iscsi_unicast_skb(skb, pid);
886 }
887 
888 static int
889 iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
890 {
891 	struct iscsi_uevent *ev = NLMSG_DATA(nlh);
892 	struct iscsi_stats *stats;
893 	struct sk_buff *skbstat;
894 	struct iscsi_cls_conn *conn;
895 	struct nlmsghdr	*nlhstat;
896 	struct iscsi_uevent *evstat;
897 	struct iscsi_internal *priv;
898 	int len = NLMSG_SPACE(sizeof(*ev) +
899 			      sizeof(struct iscsi_stats) +
900 			      sizeof(struct iscsi_stats_custom) *
901 			      ISCSI_STATS_CUSTOM_MAX);
902 	int err = 0;
903 
904 	priv = iscsi_if_transport_lookup(transport);
905 	if (!priv)
906 		return -EINVAL;
907 
908 	conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
909 	if (!conn)
910 		return -EEXIST;
911 
912 	do {
913 		int actual_size;
914 
915 		skbstat = alloc_skb(len, GFP_ATOMIC);
916 		if (!skbstat) {
917 			iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
918 					      "deliver stats: OOM\n");
919 			return -ENOMEM;
920 		}
921 
922 		nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
923 				      (len - sizeof(*nlhstat)), 0);
924 		evstat = NLMSG_DATA(nlhstat);
925 		memset(evstat, 0, sizeof(*evstat));
926 		evstat->transport_handle = iscsi_handle(conn->transport);
927 		evstat->type = nlh->nlmsg_type;
928 		evstat->u.get_stats.cid =
929 			ev->u.get_stats.cid;
930 		evstat->u.get_stats.sid =
931 			ev->u.get_stats.sid;
932 		stats = (struct iscsi_stats *)
933 			((char*)evstat + sizeof(*evstat));
934 		memset(stats, 0, sizeof(*stats));
935 
936 		transport->get_stats(conn, stats);
937 		actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
938 					  sizeof(struct iscsi_stats) +
939 					  sizeof(struct iscsi_stats_custom) *
940 					  stats->custom_length);
941 		actual_size -= sizeof(*nlhstat);
942 		actual_size = NLMSG_LENGTH(actual_size);
943 		skb_trim(skbstat, NLMSG_ALIGN(actual_size));
944 		nlhstat->nlmsg_len = actual_size;
945 
946 		err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
947 	} while (err < 0 && err != -ECONNREFUSED);
948 
949 	return err;
950 }
951 
952 /**
953  * iscsi_session_event - send session destr. completion event
954  * @session: iscsi class session
955  * @event: type of event
956  */
957 int iscsi_session_event(struct iscsi_cls_session *session,
958 			enum iscsi_uevent_e event)
959 {
960 	struct iscsi_internal *priv;
961 	struct Scsi_Host *shost;
962 	struct iscsi_uevent *ev;
963 	struct sk_buff  *skb;
964 	struct nlmsghdr *nlh;
965 	int rc, len = NLMSG_SPACE(sizeof(*ev));
966 
967 	priv = iscsi_if_transport_lookup(session->transport);
968 	if (!priv)
969 		return -EINVAL;
970 	shost = iscsi_session_to_shost(session);
971 
972 	skb = alloc_skb(len, GFP_KERNEL);
973 	if (!skb) {
974 		iscsi_cls_session_printk(KERN_ERR, session,
975 					 "Cannot notify userspace of session "
976 					 "event %u\n", event);
977 		return -ENOMEM;
978 	}
979 
980 	nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
981 	ev = NLMSG_DATA(nlh);
982 	ev->transport_handle = iscsi_handle(session->transport);
983 
984 	ev->type = event;
985 	switch (event) {
986 	case ISCSI_KEVENT_DESTROY_SESSION:
987 		ev->r.d_session.host_no = shost->host_no;
988 		ev->r.d_session.sid = session->sid;
989 		break;
990 	case ISCSI_KEVENT_CREATE_SESSION:
991 		ev->r.c_session_ret.host_no = shost->host_no;
992 		ev->r.c_session_ret.sid = session->sid;
993 		break;
994 	case ISCSI_KEVENT_UNBIND_SESSION:
995 		ev->r.unbind_session.host_no = shost->host_no;
996 		ev->r.unbind_session.sid = session->sid;
997 		break;
998 	default:
999 		iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1000 					 "%u.\n", event);
1001 		kfree_skb(skb);
1002 		return -EINVAL;
1003 	}
1004 
1005 	/*
1006 	 * this will occur if the daemon is not up, so we just warn
1007 	 * the user and when the daemon is restarted it will handle it
1008 	 */
1009 	rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
1010 	if (rc < 0)
1011 		iscsi_cls_session_printk(KERN_ERR, session,
1012 					 "Cannot notify userspace of session "
1013 					 "event %u. Check iscsi daemon\n",
1014 					 event);
1015 	return rc;
1016 }
1017 EXPORT_SYMBOL_GPL(iscsi_session_event);
1018 
1019 static int
1020 iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
1021 {
1022 	struct iscsi_transport *transport = priv->iscsi_transport;
1023 	struct iscsi_cls_session *session;
1024 	uint32_t hostno;
1025 
1026 	session = transport->create_session(transport, &priv->t,
1027 					    ev->u.c_session.cmds_max,
1028 					    ev->u.c_session.queue_depth,
1029 					    ev->u.c_session.initial_cmdsn,
1030 					    &hostno);
1031 	if (!session)
1032 		return -ENOMEM;
1033 
1034 	ev->r.c_session_ret.host_no = hostno;
1035 	ev->r.c_session_ret.sid = session->sid;
1036 	return 0;
1037 }
1038 
1039 static int
1040 iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1041 {
1042 	struct iscsi_cls_conn *conn;
1043 	struct iscsi_cls_session *session;
1044 
1045 	session = iscsi_session_lookup(ev->u.c_conn.sid);
1046 	if (!session) {
1047 		printk(KERN_ERR "iscsi: invalid session %d.\n",
1048 		       ev->u.c_conn.sid);
1049 		return -EINVAL;
1050 	}
1051 
1052 	conn = transport->create_conn(session, ev->u.c_conn.cid);
1053 	if (!conn) {
1054 		iscsi_cls_session_printk(KERN_ERR, session,
1055 					 "couldn't create a new connection.");
1056 		return -ENOMEM;
1057 	}
1058 
1059 	ev->r.c_conn_ret.sid = session->sid;
1060 	ev->r.c_conn_ret.cid = conn->cid;
1061 	return 0;
1062 }
1063 
1064 static int
1065 iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1066 {
1067 	struct iscsi_cls_conn *conn;
1068 
1069 	conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
1070 	if (!conn)
1071 		return -EINVAL;
1072 
1073 	if (transport->destroy_conn)
1074 		transport->destroy_conn(conn);
1075 	return 0;
1076 }
1077 
1078 static int
1079 iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1080 {
1081 	char *data = (char*)ev + sizeof(*ev);
1082 	struct iscsi_cls_conn *conn;
1083 	struct iscsi_cls_session *session;
1084 	int err = 0, value = 0;
1085 
1086 	session = iscsi_session_lookup(ev->u.set_param.sid);
1087 	conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1088 	if (!conn || !session)
1089 		return -EINVAL;
1090 
1091 	switch (ev->u.set_param.param) {
1092 	case ISCSI_PARAM_SESS_RECOVERY_TMO:
1093 		sscanf(data, "%d", &value);
1094 		if (value != 0)
1095 			session->recovery_tmo = value;
1096 		break;
1097 	default:
1098 		err = transport->set_param(conn, ev->u.set_param.param,
1099 					   data, ev->u.set_param.len);
1100 	}
1101 
1102 	return err;
1103 }
1104 
1105 static int
1106 iscsi_if_transport_ep(struct iscsi_transport *transport,
1107 		      struct iscsi_uevent *ev, int msg_type)
1108 {
1109 	struct sockaddr *dst_addr;
1110 	int rc = 0;
1111 
1112 	switch (msg_type) {
1113 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1114 		if (!transport->ep_connect)
1115 			return -EINVAL;
1116 
1117 		dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1118 		rc = transport->ep_connect(dst_addr,
1119 					   ev->u.ep_connect.non_blocking,
1120 					   &ev->r.ep_connect_ret.handle);
1121 		break;
1122 	case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1123 		if (!transport->ep_poll)
1124 			return -EINVAL;
1125 
1126 		ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1127 						   ev->u.ep_poll.timeout_ms);
1128 		break;
1129 	case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1130 		if (!transport->ep_disconnect)
1131 			return -EINVAL;
1132 
1133 		transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1134 		break;
1135 	}
1136 	return rc;
1137 }
1138 
1139 static int
1140 iscsi_tgt_dscvr(struct iscsi_transport *transport,
1141 		struct iscsi_uevent *ev)
1142 {
1143 	struct Scsi_Host *shost;
1144 	struct sockaddr *dst_addr;
1145 	int err;
1146 
1147 	if (!transport->tgt_dscvr)
1148 		return -EINVAL;
1149 
1150 	shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1151 	if (IS_ERR(shost)) {
1152 		printk(KERN_ERR "target discovery could not find host no %u\n",
1153 		       ev->u.tgt_dscvr.host_no);
1154 		return -ENODEV;
1155 	}
1156 
1157 
1158 	dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1159 	err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1160 				   ev->u.tgt_dscvr.enable, dst_addr);
1161 	scsi_host_put(shost);
1162 	return err;
1163 }
1164 
1165 static int
1166 iscsi_set_host_param(struct iscsi_transport *transport,
1167 		     struct iscsi_uevent *ev)
1168 {
1169 	char *data = (char*)ev + sizeof(*ev);
1170 	struct Scsi_Host *shost;
1171 	int err;
1172 
1173 	if (!transport->set_host_param)
1174 		return -ENOSYS;
1175 
1176 	shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1177 	if (IS_ERR(shost)) {
1178 		printk(KERN_ERR "set_host_param could not find host no %u\n",
1179 		       ev->u.set_host_param.host_no);
1180 		return -ENODEV;
1181 	}
1182 
1183 	err = transport->set_host_param(shost, ev->u.set_host_param.param,
1184 					data, ev->u.set_host_param.len);
1185 	scsi_host_put(shost);
1186 	return err;
1187 }
1188 
1189 static int
1190 iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1191 {
1192 	int err = 0;
1193 	struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1194 	struct iscsi_transport *transport = NULL;
1195 	struct iscsi_internal *priv;
1196 	struct iscsi_cls_session *session;
1197 	struct iscsi_cls_conn *conn;
1198 
1199 	priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1200 	if (!priv)
1201 		return -EINVAL;
1202 	transport = priv->iscsi_transport;
1203 
1204 	if (!try_module_get(transport->owner))
1205 		return -EINVAL;
1206 
1207 	priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1208 
1209 	switch (nlh->nlmsg_type) {
1210 	case ISCSI_UEVENT_CREATE_SESSION:
1211 		err = iscsi_if_create_session(priv, ev);
1212 		break;
1213 	case ISCSI_UEVENT_DESTROY_SESSION:
1214 		session = iscsi_session_lookup(ev->u.d_session.sid);
1215 		if (session)
1216 			transport->destroy_session(session);
1217 		else
1218 			err = -EINVAL;
1219 		break;
1220 	case ISCSI_UEVENT_UNBIND_SESSION:
1221 		session = iscsi_session_lookup(ev->u.d_session.sid);
1222 		if (session)
1223 			iscsi_unbind_session(session);
1224 		else
1225 			err = -EINVAL;
1226 		break;
1227 	case ISCSI_UEVENT_CREATE_CONN:
1228 		err = iscsi_if_create_conn(transport, ev);
1229 		break;
1230 	case ISCSI_UEVENT_DESTROY_CONN:
1231 		err = iscsi_if_destroy_conn(transport, ev);
1232 		break;
1233 	case ISCSI_UEVENT_BIND_CONN:
1234 		session = iscsi_session_lookup(ev->u.b_conn.sid);
1235 		conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
1236 
1237 		if (session && conn)
1238 			ev->r.retcode =	transport->bind_conn(session, conn,
1239 					ev->u.b_conn.transport_eph,
1240 					ev->u.b_conn.is_leading);
1241 		else
1242 			err = -EINVAL;
1243 		break;
1244 	case ISCSI_UEVENT_SET_PARAM:
1245 		err = iscsi_set_param(transport, ev);
1246 		break;
1247 	case ISCSI_UEVENT_START_CONN:
1248 		conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
1249 		if (conn)
1250 			ev->r.retcode = transport->start_conn(conn);
1251 		else
1252 			err = -EINVAL;
1253 		break;
1254 	case ISCSI_UEVENT_STOP_CONN:
1255 		conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
1256 		if (conn)
1257 			transport->stop_conn(conn, ev->u.stop_conn.flag);
1258 		else
1259 			err = -EINVAL;
1260 		break;
1261 	case ISCSI_UEVENT_SEND_PDU:
1262 		conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
1263 		if (conn)
1264 			ev->r.retcode =	transport->send_pdu(conn,
1265 				(struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1266 				(char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1267 				ev->u.send_pdu.data_size);
1268 		else
1269 			err = -EINVAL;
1270 		break;
1271 	case ISCSI_UEVENT_GET_STATS:
1272 		err = iscsi_if_get_stats(transport, nlh);
1273 		break;
1274 	case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1275 	case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1276 	case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1277 		err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1278 		break;
1279 	case ISCSI_UEVENT_TGT_DSCVR:
1280 		err = iscsi_tgt_dscvr(transport, ev);
1281 		break;
1282 	case ISCSI_UEVENT_SET_HOST_PARAM:
1283 		err = iscsi_set_host_param(transport, ev);
1284 		break;
1285 	default:
1286 		err = -ENOSYS;
1287 		break;
1288 	}
1289 
1290 	module_put(transport->owner);
1291 	return err;
1292 }
1293 
1294 /*
1295  * Get message from skb.  Each message is processed by iscsi_if_recv_msg.
1296  * Malformed skbs with wrong lengths or invalid creds are not processed.
1297  */
1298 static void
1299 iscsi_if_rx(struct sk_buff *skb)
1300 {
1301 	mutex_lock(&rx_queue_mutex);
1302 	while (skb->len >= NLMSG_SPACE(0)) {
1303 		int err;
1304 		uint32_t rlen;
1305 		struct nlmsghdr	*nlh;
1306 		struct iscsi_uevent *ev;
1307 
1308 		nlh = nlmsg_hdr(skb);
1309 		if (nlh->nlmsg_len < sizeof(*nlh) ||
1310 		    skb->len < nlh->nlmsg_len) {
1311 			break;
1312 		}
1313 
1314 		ev = NLMSG_DATA(nlh);
1315 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1316 		if (rlen > skb->len)
1317 			rlen = skb->len;
1318 
1319 		err = iscsi_if_recv_msg(skb, nlh);
1320 		if (err) {
1321 			ev->type = ISCSI_KEVENT_IF_ERROR;
1322 			ev->iferror = err;
1323 		}
1324 		do {
1325 			/*
1326 			 * special case for GET_STATS:
1327 			 * on success - sending reply and stats from
1328 			 * inside of if_recv_msg(),
1329 			 * on error - fall through.
1330 			 */
1331 			if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
1332 				break;
1333 			err = iscsi_if_send_reply(
1334 				NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1335 				nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1336 		} while (err < 0 && err != -ECONNREFUSED);
1337 		skb_pull(skb, rlen);
1338 	}
1339 	mutex_unlock(&rx_queue_mutex);
1340 }
1341 
1342 #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store)		\
1343 struct device_attribute dev_attr_##_prefix##_##_name =	\
1344 	__ATTR(_name,_mode,_show,_store)
1345 
1346 /*
1347  * iSCSI connection attrs
1348  */
1349 #define iscsi_conn_attr_show(param)					\
1350 static ssize_t								\
1351 show_conn_param_##param(struct device *dev, 				\
1352 			struct device_attribute *attr, char *buf)	\
1353 {									\
1354 	struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent);	\
1355 	struct iscsi_transport *t = conn->transport;			\
1356 	return t->get_conn_param(conn, param, buf);			\
1357 }
1358 
1359 #define iscsi_conn_attr(field, param)					\
1360 	iscsi_conn_attr_show(param)					\
1361 static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param,	\
1362 			NULL);
1363 
1364 iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1365 iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1366 iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1367 iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1368 iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1369 iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1370 iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1371 iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1372 iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1373 iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1374 iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
1375 iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1376 iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
1377 
1378 /*
1379  * iSCSI session attrs
1380  */
1381 #define iscsi_session_attr_show(param, perm)				\
1382 static ssize_t								\
1383 show_session_param_##param(struct device *dev,				\
1384 			   struct device_attribute *attr, char *buf)	\
1385 {									\
1386 	struct iscsi_cls_session *session = 				\
1387 		iscsi_dev_to_session(dev->parent);			\
1388 	struct iscsi_transport *t = session->transport;			\
1389 									\
1390 	if (perm && !capable(CAP_SYS_ADMIN))				\
1391 		return -EACCES;						\
1392 	return t->get_session_param(session, param, buf);		\
1393 }
1394 
1395 #define iscsi_session_attr(field, param, perm)				\
1396 	iscsi_session_attr_show(param, perm)				\
1397 static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
1398 			NULL);
1399 
1400 iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1401 iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1402 iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1403 iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1404 iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1405 iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1406 iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1407 iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1408 iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1409 iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1410 iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1411 iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1412 iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1413 iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
1414 iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1415 iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1416 iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
1417 
1418 static ssize_t
1419 show_priv_session_state(struct device *dev, struct device_attribute *attr,
1420 			char *buf)
1421 {
1422 	struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
1423 	return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1424 }
1425 static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1426 			NULL);
1427 
1428 #define iscsi_priv_session_attr_show(field, format)			\
1429 static ssize_t								\
1430 show_priv_session_##field(struct device *dev, 				\
1431 			  struct device_attribute *attr, char *buf)	\
1432 {									\
1433 	struct iscsi_cls_session *session = 				\
1434 			iscsi_dev_to_session(dev->parent);		\
1435 	return sprintf(buf, format"\n", session->field);		\
1436 }
1437 
1438 #define iscsi_priv_session_attr(field, format)				\
1439 	iscsi_priv_session_attr_show(field, format)			\
1440 static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1441 			NULL)
1442 iscsi_priv_session_attr(recovery_tmo, "%d");
1443 
1444 /*
1445  * iSCSI host attrs
1446  */
1447 #define iscsi_host_attr_show(param)					\
1448 static ssize_t								\
1449 show_host_param_##param(struct device *dev, 				\
1450 			struct device_attribute *attr, char *buf)	\
1451 {									\
1452 	struct Scsi_Host *shost = transport_class_to_shost(dev);	\
1453 	struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1454 	return priv->iscsi_transport->get_host_param(shost, param, buf); \
1455 }
1456 
1457 #define iscsi_host_attr(field, param)					\
1458 	iscsi_host_attr_show(param)					\
1459 static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param,	\
1460 			NULL);
1461 
1462 iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
1463 iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
1464 iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
1465 iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
1466 
1467 #define SETUP_PRIV_SESSION_RD_ATTR(field)				\
1468 do {									\
1469 	priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
1470 	count++;							\
1471 } while (0)
1472 
1473 
1474 #define SETUP_SESSION_RD_ATTR(field, param_flag)			\
1475 do {									\
1476 	if (tt->param_mask & param_flag) {				\
1477 		priv->session_attrs[count] = &dev_attr_sess_##field; \
1478 		count++;						\
1479 	}								\
1480 } while (0)
1481 
1482 #define SETUP_CONN_RD_ATTR(field, param_flag)				\
1483 do {									\
1484 	if (tt->param_mask & param_flag) {				\
1485 		priv->conn_attrs[count] = &dev_attr_conn_##field; \
1486 		count++;						\
1487 	}								\
1488 } while (0)
1489 
1490 #define SETUP_HOST_RD_ATTR(field, param_flag)				\
1491 do {									\
1492 	if (tt->host_param_mask & param_flag) {				\
1493 		priv->host_attrs[count] = &dev_attr_host_##field; \
1494 		count++;						\
1495 	}								\
1496 } while (0)
1497 
1498 static int iscsi_session_match(struct attribute_container *cont,
1499 			   struct device *dev)
1500 {
1501 	struct iscsi_cls_session *session;
1502 	struct Scsi_Host *shost;
1503 	struct iscsi_internal *priv;
1504 
1505 	if (!iscsi_is_session_dev(dev))
1506 		return 0;
1507 
1508 	session = iscsi_dev_to_session(dev);
1509 	shost = iscsi_session_to_shost(session);
1510 	if (!shost->transportt)
1511 		return 0;
1512 
1513 	priv = to_iscsi_internal(shost->transportt);
1514 	if (priv->session_cont.ac.class != &iscsi_session_class.class)
1515 		return 0;
1516 
1517 	return &priv->session_cont.ac == cont;
1518 }
1519 
1520 static int iscsi_conn_match(struct attribute_container *cont,
1521 			   struct device *dev)
1522 {
1523 	struct iscsi_cls_session *session;
1524 	struct iscsi_cls_conn *conn;
1525 	struct Scsi_Host *shost;
1526 	struct iscsi_internal *priv;
1527 
1528 	if (!iscsi_is_conn_dev(dev))
1529 		return 0;
1530 
1531 	conn = iscsi_dev_to_conn(dev);
1532 	session = iscsi_dev_to_session(conn->dev.parent);
1533 	shost = iscsi_session_to_shost(session);
1534 
1535 	if (!shost->transportt)
1536 		return 0;
1537 
1538 	priv = to_iscsi_internal(shost->transportt);
1539 	if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1540 		return 0;
1541 
1542 	return &priv->conn_cont.ac == cont;
1543 }
1544 
1545 static int iscsi_host_match(struct attribute_container *cont,
1546 			    struct device *dev)
1547 {
1548 	struct Scsi_Host *shost;
1549 	struct iscsi_internal *priv;
1550 
1551 	if (!scsi_is_host_device(dev))
1552 		return 0;
1553 
1554 	shost = dev_to_shost(dev);
1555 	if (!shost->transportt  ||
1556 	    shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1557 		return 0;
1558 
1559         priv = to_iscsi_internal(shost->transportt);
1560         return &priv->t.host_attrs.ac == cont;
1561 }
1562 
1563 struct scsi_transport_template *
1564 iscsi_register_transport(struct iscsi_transport *tt)
1565 {
1566 	struct iscsi_internal *priv;
1567 	unsigned long flags;
1568 	int count = 0, err;
1569 
1570 	BUG_ON(!tt);
1571 
1572 	priv = iscsi_if_transport_lookup(tt);
1573 	if (priv)
1574 		return NULL;
1575 
1576 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1577 	if (!priv)
1578 		return NULL;
1579 	INIT_LIST_HEAD(&priv->list);
1580 	priv->daemon_pid = -1;
1581 	priv->iscsi_transport = tt;
1582 	priv->t.user_scan = iscsi_user_scan;
1583 
1584 	priv->dev.class = &iscsi_transport_class;
1585 	snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1586 	err = device_register(&priv->dev);
1587 	if (err)
1588 		goto free_priv;
1589 
1590 	err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
1591 	if (err)
1592 		goto unregister_dev;
1593 
1594 	/* host parameters */
1595 	priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1596 	priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1597 	priv->t.host_attrs.ac.match = iscsi_host_match;
1598 	priv->t.host_size = sizeof(struct iscsi_host);
1599 	transport_container_register(&priv->t.host_attrs);
1600 
1601 	SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
1602 	SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
1603 	SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
1604 	SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
1605 	BUG_ON(count > ISCSI_HOST_ATTRS);
1606 	priv->host_attrs[count] = NULL;
1607 	count = 0;
1608 
1609 	/* connection parameters */
1610 	priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1611 	priv->conn_cont.ac.class = &iscsi_connection_class.class;
1612 	priv->conn_cont.ac.match = iscsi_conn_match;
1613 	transport_container_register(&priv->conn_cont);
1614 
1615 	SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1616 	SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1617 	SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1618 	SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1619 	SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1620 	SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1621 	SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1622 	SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
1623 	SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
1624 	SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1625 	SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
1626 	SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1627 	SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
1628 
1629 	BUG_ON(count > ISCSI_CONN_ATTRS);
1630 	priv->conn_attrs[count] = NULL;
1631 	count = 0;
1632 
1633 	/* session parameters */
1634 	priv->session_cont.ac.attrs = &priv->session_attrs[0];
1635 	priv->session_cont.ac.class = &iscsi_session_class.class;
1636 	priv->session_cont.ac.match = iscsi_session_match;
1637 	transport_container_register(&priv->session_cont);
1638 
1639 	SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1640 	SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1641 	SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1642 	SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1643 	SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1644 	SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1645 	SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1646 	SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
1647 	SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1648 	SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
1649 	SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1650 	SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1651 	SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1652 	SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
1653 	SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
1654 	SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1655 	SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
1656 	SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
1657 	SETUP_PRIV_SESSION_RD_ATTR(state);
1658 
1659 	BUG_ON(count > ISCSI_SESSION_ATTRS);
1660 	priv->session_attrs[count] = NULL;
1661 
1662 	spin_lock_irqsave(&iscsi_transport_lock, flags);
1663 	list_add(&priv->list, &iscsi_transports);
1664 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1665 
1666 	printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
1667 	return &priv->t;
1668 
1669 unregister_dev:
1670 	device_unregister(&priv->dev);
1671 free_priv:
1672 	kfree(priv);
1673 	return NULL;
1674 }
1675 EXPORT_SYMBOL_GPL(iscsi_register_transport);
1676 
1677 int iscsi_unregister_transport(struct iscsi_transport *tt)
1678 {
1679 	struct iscsi_internal *priv;
1680 	unsigned long flags;
1681 
1682 	BUG_ON(!tt);
1683 
1684 	mutex_lock(&rx_queue_mutex);
1685 
1686 	priv = iscsi_if_transport_lookup(tt);
1687 	BUG_ON (!priv);
1688 
1689 	spin_lock_irqsave(&iscsi_transport_lock, flags);
1690 	list_del(&priv->list);
1691 	spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1692 
1693 	transport_container_unregister(&priv->conn_cont);
1694 	transport_container_unregister(&priv->session_cont);
1695 	transport_container_unregister(&priv->t.host_attrs);
1696 
1697 	sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1698 	device_unregister(&priv->dev);
1699 	mutex_unlock(&rx_queue_mutex);
1700 
1701 	return 0;
1702 }
1703 EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1704 
1705 static __init int iscsi_transport_init(void)
1706 {
1707 	int err;
1708 
1709 	printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
1710 		ISCSI_TRANSPORT_VERSION);
1711 
1712 	atomic_set(&iscsi_session_nr, 0);
1713 
1714 	err = class_register(&iscsi_transport_class);
1715 	if (err)
1716 		return err;
1717 
1718 	err = transport_class_register(&iscsi_host_class);
1719 	if (err)
1720 		goto unregister_transport_class;
1721 
1722 	err = transport_class_register(&iscsi_connection_class);
1723 	if (err)
1724 		goto unregister_host_class;
1725 
1726 	err = transport_class_register(&iscsi_session_class);
1727 	if (err)
1728 		goto unregister_conn_class;
1729 
1730 	nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
1731 			THIS_MODULE);
1732 	if (!nls) {
1733 		err = -ENOBUFS;
1734 		goto unregister_session_class;
1735 	}
1736 
1737 	iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1738 	if (!iscsi_eh_timer_workq)
1739 		goto release_nls;
1740 
1741 	return 0;
1742 
1743 release_nls:
1744 	netlink_kernel_release(nls);
1745 unregister_session_class:
1746 	transport_class_unregister(&iscsi_session_class);
1747 unregister_conn_class:
1748 	transport_class_unregister(&iscsi_connection_class);
1749 unregister_host_class:
1750 	transport_class_unregister(&iscsi_host_class);
1751 unregister_transport_class:
1752 	class_unregister(&iscsi_transport_class);
1753 	return err;
1754 }
1755 
1756 static void __exit iscsi_transport_exit(void)
1757 {
1758 	destroy_workqueue(iscsi_eh_timer_workq);
1759 	netlink_kernel_release(nls);
1760 	transport_class_unregister(&iscsi_connection_class);
1761 	transport_class_unregister(&iscsi_session_class);
1762 	transport_class_unregister(&iscsi_host_class);
1763 	class_unregister(&iscsi_transport_class);
1764 }
1765 
1766 module_init(iscsi_transport_init);
1767 module_exit(iscsi_transport_exit);
1768 
1769 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1770 	      "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1771 	      "Alex Aizman <itn780@yahoo.com>");
1772 MODULE_DESCRIPTION("iSCSI Transport Interface");
1773 MODULE_LICENSE("GPL");
1774 MODULE_VERSION(ISCSI_TRANSPORT_VERSION);
1775