blob: af88955d0ec1edec9b04b38cbe5f0f92b415ab24 [file] [log] [blame]
Alex Aizman0896b752005-08-04 19:33:07 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * iSCSI transport class definitions
3 *
4 * Copyright (C) IBM Corporation, 2004
Alex Aizman0896b752005-08-04 19:33:07 -07005 * Copyright (C) Mike Christie, 2004 - 2005
6 * Copyright (C) Dmitry Yusupov, 2004 - 2005
7 * Copyright (C) Alex Aizman, 2004 - 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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>
Arjan van de Ven0b950672006-01-11 13:16:10 +010024#include <linux/mutex.h>
Alex Aizman0896b752005-08-04 19:33:07 -070025#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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>
Alex Aizman0896b752005-08-04 19:33:07 -070031#include <scsi/iscsi_if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Mike Christie6eabafb2008-01-31 13:36:43 -060033#define ISCSI_SESSION_ATTRS 19
Mike Christie8d2860b2006-05-02 19:46:47 -050034#define ISCSI_CONN_ATTRS 11
Mike Christied8196ed2007-05-30 12:57:25 -050035#define ISCSI_HOST_ATTRS 4
Mike Christie10396232007-12-13 12:43:43 -060036#define ISCSI_TRANSPORT_VERSION "2.0-867"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct iscsi_internal {
Mike Christie790f39a2006-05-18 20:31:39 -050039 int daemon_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct scsi_transport_template t;
Alex Aizman0896b752005-08-04 19:33:07 -070041 struct iscsi_transport *iscsi_transport;
42 struct list_head list;
Alex Aizman0896b752005-08-04 19:33:07 -070043 struct class_device cdev;
Mike Christie30a6c652006-04-06 21:13:39 -050044
45 struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070046 struct transport_container conn_cont;
47 struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
48 struct transport_container session_cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Mike Christie41be1442007-02-28 17:32:18 -060052static atomic_t iscsi_session_nr; /* sysfs session id for next new session */
Mike Christied8bf5412007-12-13 12:43:27 -060053static struct workqueue_struct *iscsi_eh_timer_workq;
Mike Christieb5c7a122006-04-06 21:13:33 -050054
Alex Aizman0896b752005-08-04 19:33:07 -070055/*
56 * list of registered transports and lock that must
57 * be held while accessing list. The iscsi_transport_lock must
Arjan van de Ven0b950672006-01-11 13:16:10 +010058 * be acquired after the rx_queue_mutex.
Alex Aizman0896b752005-08-04 19:33:07 -070059 */
60static LIST_HEAD(iscsi_transports);
61static DEFINE_SPINLOCK(iscsi_transport_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Alex Aizman0896b752005-08-04 19:33:07 -070063#define to_iscsi_internal(tmpl) \
64 container_of(tmpl, struct iscsi_internal, t)
65
66#define cdev_to_iscsi_internal(_cdev) \
67 container_of(_cdev, struct iscsi_internal, cdev)
68
69static void iscsi_transport_release(struct class_device *cdev)
70{
71 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
72 kfree(priv);
73}
74
75/*
76 * iscsi_transport_class represents the iscsi_transports that are
77 * registered.
78 */
79static struct class iscsi_transport_class = {
80 .name = "iscsi_transport",
81 .release = iscsi_transport_release,
82};
83
84static ssize_t
85show_transport_handle(struct class_device *cdev, char *buf)
86{
87 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev);
Mike Christie762e2bf2005-09-12 21:01:46 -050088 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070089}
90static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
91
92#define show_transport_attr(name, format) \
93static ssize_t \
94show_transport_##name(struct class_device *cdev, char *buf) \
95{ \
96 struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \
97 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
98} \
99static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
100
101show_transport_attr(caps, "0x%x");
102show_transport_attr(max_lun, "%d");
103show_transport_attr(max_conn, "%d");
104show_transport_attr(max_cmd_len, "%d");
105
106static struct attribute *iscsi_transport_attrs[] = {
107 &class_device_attr_handle.attr,
108 &class_device_attr_caps.attr,
109 &class_device_attr_max_lun.attr,
110 &class_device_attr_max_conn.attr,
111 &class_device_attr_max_cmd_len.attr,
112 NULL,
113};
114
115static struct attribute_group iscsi_transport_group = {
116 .attrs = iscsi_transport_attrs,
117};
118
Mike Christie26974782007-12-13 12:43:29 -0600119
120
Mike Christie30a6c652006-04-06 21:13:39 -0500121static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
122 struct class_device *cdev)
123{
124 struct Scsi_Host *shost = dev_to_shost(dev);
125 struct iscsi_host *ihost = shost->shost_data;
126
127 memset(ihost, 0, sizeof(*ihost));
128 INIT_LIST_HEAD(&ihost->sessions);
129 mutex_init(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600130
Mike Christiebd976f62008-01-31 13:36:46 -0600131 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
Mike Christie26974782007-12-13 12:43:29 -0600132 shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600133 ihost->scan_workq = create_singlethread_workqueue(
134 ihost->scan_workq_name);
135 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600136 return -ENOMEM;
137 return 0;
138}
139
140static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
141 struct class_device *cdev)
142{
143 struct Scsi_Host *shost = dev_to_shost(dev);
144 struct iscsi_host *ihost = shost->shost_data;
145
Mike Christiebd976f62008-01-31 13:36:46 -0600146 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500147 return 0;
148}
149
150static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
151 "iscsi_host",
152 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600153 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500154 NULL);
155
Alex Aizman0896b752005-08-04 19:33:07 -0700156static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
157 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 NULL,
159 NULL,
160 NULL);
161
Alex Aizman0896b752005-08-04 19:33:07 -0700162static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
163 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 NULL,
165 NULL,
166 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700167
168static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100169static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700170
Mike Christie7b7232f2006-02-01 21:06:49 -0600171static LIST_HEAD(sesslist);
172static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700173static LIST_HEAD(connlist);
174static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Mike Christieb5c7a122006-04-06 21:13:33 -0500176static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
177{
178 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
179 return sess->sid;
180}
181
182/*
183 * Returns the matching session to a given sid
184 */
185static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600186{
187 unsigned long flags;
188 struct iscsi_cls_session *sess;
189
190 spin_lock_irqsave(&sesslock, flags);
191 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500192 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600193 spin_unlock_irqrestore(&sesslock, flags);
194 return sess;
195 }
196 }
197 spin_unlock_irqrestore(&sesslock, flags);
198 return NULL;
199}
200
Mike Christieb5c7a122006-04-06 21:13:33 -0500201/*
202 * Returns the matching connection to a given sid / cid tuple
203 */
204static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600205{
206 unsigned long flags;
207 struct iscsi_cls_conn *conn;
208
209 spin_lock_irqsave(&connlock, flags);
210 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500211 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600212 spin_unlock_irqrestore(&connlock, flags);
213 return conn;
214 }
215 }
216 spin_unlock_irqrestore(&connlock, flags);
217 return NULL;
218}
219
Mike Christie7b8631b2006-01-13 18:05:50 -0600220/*
221 * The following functions can be used by LLDs that allocate
222 * their own scsi_hosts or by software iscsi LLDs
223 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600224static struct {
225 int value;
226 char *name;
227} iscsi_session_state_names[] = {
228 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
229 { ISCSI_SESSION_FAILED, "FAILED" },
230 { ISCSI_SESSION_FREE, "FREE" },
231};
232
233const char *iscsi_session_state_name(int state)
234{
235 int i;
236 char *name = NULL;
237
238 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
239 if (iscsi_session_state_names[i].value == state) {
240 name = iscsi_session_state_names[i].name;
241 break;
242 }
243 }
244 return name;
245}
246
247int iscsi_session_chkready(struct iscsi_cls_session *session)
248{
249 unsigned long flags;
250 int err;
251
252 spin_lock_irqsave(&session->lock, flags);
253 switch (session->state) {
254 case ISCSI_SESSION_LOGGED_IN:
255 err = 0;
256 break;
257 case ISCSI_SESSION_FAILED:
258 err = DID_IMM_RETRY << 16;
259 break;
260 case ISCSI_SESSION_FREE:
261 err = DID_NO_CONNECT << 16;
262 break;
263 default:
264 err = DID_NO_CONNECT << 16;
265 break;
266 }
267 spin_unlock_irqrestore(&session->lock, flags);
268 return err;
269}
270EXPORT_SYMBOL_GPL(iscsi_session_chkready);
271
Mike Christie7b8631b2006-01-13 18:05:50 -0600272static void iscsi_session_release(struct device *dev)
273{
274 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600275 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Mike Christie7b8631b2006-01-13 18:05:50 -0600277 shost = iscsi_session_to_shost(session);
278 scsi_host_put(shost);
279 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Mike Christie7b8631b2006-01-13 18:05:50 -0600282static int iscsi_is_session_dev(const struct device *dev)
283{
284 return dev->release == iscsi_session_release;
285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Mike Christie30a6c652006-04-06 21:13:39 -0500287static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
288 uint id, uint lun)
289{
290 struct iscsi_host *ihost = shost->shost_data;
291 struct iscsi_cls_session *session;
292
293 mutex_lock(&ihost->mutex);
294 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500295 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500296 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500297 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500298 session->target_id, lun, 1);
299 }
300 mutex_unlock(&ihost->mutex);
301
302 return 0;
303}
304
Mike Christiebd976f62008-01-31 13:36:46 -0600305static void iscsi_scan_session(struct work_struct *work)
306{
307 struct iscsi_cls_session *session =
308 container_of(work, struct iscsi_cls_session, scan_work);
309 unsigned long flags;
310
311 spin_lock_irqsave(&session->lock, flags);
312 if (session->state != ISCSI_SESSION_LOGGED_IN) {
313 spin_unlock_irqrestore(&session->lock, flags);
314 return;
315 }
316 spin_unlock_irqrestore(&session->lock, flags);
317
318 scsi_scan_target(&session->dev, 0, session->target_id,
319 SCAN_WILD_CARD, 1);
320}
321
David Howellsc4028952006-11-22 14:57:56 +0000322static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500323{
David Howellsc4028952006-11-22 14:57:56 +0000324 struct iscsi_cls_session *session =
325 container_of(work, struct iscsi_cls_session,
326 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600327 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500328
Or Gerlitzbe2df722006-05-02 19:46:43 -0500329 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
330 "out after %d secs\n", session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500331
Mike Christie6eabafb2008-01-31 13:36:43 -0600332 spin_lock_irqsave(&session->lock, flags);
333 switch (session->state) {
334 case ISCSI_SESSION_FAILED:
335 session->state = ISCSI_SESSION_FREE;
336 break;
337 case ISCSI_SESSION_LOGGED_IN:
338 case ISCSI_SESSION_FREE:
339 /* we raced with the unblock's flush */
340 spin_unlock_irqrestore(&session->lock, flags);
341 return;
342 }
343 spin_unlock_irqrestore(&session->lock, flags);
344
Mike Christie30a6c652006-04-06 21:13:39 -0500345 if (session->transport->session_recovery_timedout)
346 session->transport->session_recovery_timedout(session);
347
348 scsi_target_unblock(&session->dev);
349}
350
Mike Christie6eabafb2008-01-31 13:36:43 -0600351void __iscsi_unblock_session(struct iscsi_cls_session *session)
Mike Christie30a6c652006-04-06 21:13:39 -0500352{
353 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600354 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500355 scsi_target_unblock(&session->dev);
356}
Mike Christie6eabafb2008-01-31 13:36:43 -0600357
358void iscsi_unblock_session(struct iscsi_cls_session *session)
359{
Mike Christiebd976f62008-01-31 13:36:46 -0600360 struct Scsi_Host *shost = iscsi_session_to_shost(session);
361 struct iscsi_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600362 unsigned long flags;
363
364 spin_lock_irqsave(&session->lock, flags);
365 session->state = ISCSI_SESSION_LOGGED_IN;
366 spin_unlock_irqrestore(&session->lock, flags);
367
368 __iscsi_unblock_session(session);
Mike Christiebd976f62008-01-31 13:36:46 -0600369 queue_work(ihost->scan_workq, &session->scan_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600370}
Mike Christie30a6c652006-04-06 21:13:39 -0500371EXPORT_SYMBOL_GPL(iscsi_unblock_session);
372
373void iscsi_block_session(struct iscsi_cls_session *session)
374{
Mike Christie6eabafb2008-01-31 13:36:43 -0600375 unsigned long flags;
376
377 spin_lock_irqsave(&session->lock, flags);
378 session->state = ISCSI_SESSION_FAILED;
379 spin_unlock_irqrestore(&session->lock, flags);
380
Mike Christie30a6c652006-04-06 21:13:39 -0500381 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600382 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
383 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500384}
385EXPORT_SYMBOL_GPL(iscsi_block_session);
386
Mike Christie26974782007-12-13 12:43:29 -0600387static void __iscsi_unbind_session(struct work_struct *work)
388{
389 struct iscsi_cls_session *session =
390 container_of(work, struct iscsi_cls_session,
391 unbind_work);
392 struct Scsi_Host *shost = iscsi_session_to_shost(session);
393 struct iscsi_host *ihost = shost->shost_data;
394
395 /* Prevent new scans and make sure scanning is not in progress */
396 mutex_lock(&ihost->mutex);
397 if (list_empty(&session->host_list)) {
398 mutex_unlock(&ihost->mutex);
399 return;
400 }
401 list_del_init(&session->host_list);
402 mutex_unlock(&ihost->mutex);
403
404 scsi_remove_target(&session->dev);
405 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
406}
407
408static int iscsi_unbind_session(struct iscsi_cls_session *session)
409{
410 struct Scsi_Host *shost = iscsi_session_to_shost(session);
411 struct iscsi_host *ihost = shost->shost_data;
412
Mike Christiebd976f62008-01-31 13:36:46 -0600413 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600414}
415
Mike Christie7b8631b2006-01-13 18:05:50 -0600416struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500417iscsi_alloc_session(struct Scsi_Host *shost,
418 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600419{
420 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Mike Christieb5c7a122006-04-06 21:13:33 -0500422 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
423 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600424 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500425 return NULL;
426
Mike Christie7b8631b2006-01-13 18:05:50 -0600427 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500428 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600429 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000430 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500431 INIT_LIST_HEAD(&session->host_list);
432 INIT_LIST_HEAD(&session->sess_list);
Mike Christie26974782007-12-13 12:43:29 -0600433 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600434 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600435 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600436
Mike Christie6a8a0d32006-06-28 12:00:31 -0500437 /* this is released in the dev's release function */
438 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500439 session->dev.parent = &shost->shost_gendev;
440 session->dev.release = iscsi_session_release;
441 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500442 if (transport->sessiondata_size)
443 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500444 return session;
445}
446EXPORT_SYMBOL_GPL(iscsi_alloc_session);
447
Mike Christie6a8a0d32006-06-28 12:00:31 -0500448int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500449{
450 struct Scsi_Host *shost = iscsi_session_to_shost(session);
451 struct iscsi_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600452 unsigned long flags;
Mike Christie8434aa82006-06-28 12:00:30 -0500453 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500454
Mike Christie30a6c652006-04-06 21:13:39 -0500455 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600456 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie6a8a0d32006-06-28 12:00:31 -0500457 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500458
Mike Christieb5c7a122006-04-06 21:13:33 -0500459 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
460 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500461 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600462 if (err) {
463 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
464 "register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500465 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600466 }
467 transport_register_device(&session->dev);
468
Mike Christie26974782007-12-13 12:43:29 -0600469 spin_lock_irqsave(&sesslock, flags);
470 list_add(&session->sess_list, &sesslist);
471 spin_unlock_irqrestore(&sesslock, flags);
472
Mike Christie30a6c652006-04-06 21:13:39 -0500473 mutex_lock(&ihost->mutex);
474 list_add(&session->host_list, &ihost->sessions);
475 mutex_unlock(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600476
477 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500478 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500479
Mike Christie8434aa82006-06-28 12:00:30 -0500480release_host:
481 scsi_host_put(shost);
482 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600483}
Mike Christie8434aa82006-06-28 12:00:30 -0500484EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600485
486/**
Mike Christie8434aa82006-06-28 12:00:30 -0500487 * iscsi_create_session - create iscsi class session
488 * @shost: scsi host
489 * @transport: iscsi transport
Rob Landleyeb448202007-11-03 13:30:39 -0500490 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600491 *
Mike Christie8434aa82006-06-28 12:00:30 -0500492 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500493 */
Mike Christie8434aa82006-06-28 12:00:30 -0500494struct iscsi_cls_session *
495iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500496 struct iscsi_transport *transport,
497 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500498{
499 struct iscsi_cls_session *session;
500
501 session = iscsi_alloc_session(shost, transport);
502 if (!session)
503 return NULL;
504
Mike Christie6a8a0d32006-06-28 12:00:31 -0500505 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500506 iscsi_free_session(session);
507 return NULL;
508 }
509 return session;
510}
511EXPORT_SYMBOL_GPL(iscsi_create_session);
512
Mike Christie26974782007-12-13 12:43:29 -0600513static void iscsi_conn_release(struct device *dev)
514{
515 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
516 struct device *parent = conn->dev.parent;
517
518 kfree(conn);
519 put_device(parent);
520}
521
522static int iscsi_is_conn_dev(const struct device *dev)
523{
524 return dev->release == iscsi_conn_release;
525}
526
527static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
528{
529 if (!iscsi_is_conn_dev(dev))
530 return 0;
531 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
532}
533
Mike Christie8434aa82006-06-28 12:00:30 -0500534void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600535{
Mike Christie30a6c652006-04-06 21:13:39 -0500536 struct Scsi_Host *shost = iscsi_session_to_shost(session);
537 struct iscsi_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600538 unsigned long flags;
539 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500540
Mike Christie26974782007-12-13 12:43:29 -0600541 spin_lock_irqsave(&sesslock, flags);
542 list_del(&session->sess_list);
543 spin_unlock_irqrestore(&sesslock, flags);
544
545 /*
546 * If we are blocked let commands flow again. The lld or iscsi
547 * layer should set up the queuecommand to fail commands.
548 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600549 spin_lock_irqsave(&session->lock, flags);
550 session->state = ISCSI_SESSION_FREE;
551 spin_unlock_irqrestore(&session->lock, flags);
552 __iscsi_unblock_session(session);
Mike Christie26974782007-12-13 12:43:29 -0600553 iscsi_unbind_session(session);
Mike Christiebd976f62008-01-31 13:36:46 -0600554
555 /* flush running scans */
556 flush_workqueue(ihost->scan_workq);
Mike Christie26974782007-12-13 12:43:29 -0600557 /*
558 * If the session dropped while removing devices then we need to make
559 * sure it is not blocked
560 */
Mike Christie30a6c652006-04-06 21:13:39 -0500561 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600562 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500563
Mike Christie26974782007-12-13 12:43:29 -0600564 /* hw iscsi may not have removed all connections from session */
565 err = device_for_each_child(&session->dev, NULL,
566 iscsi_iter_destroy_conn_fn);
567 if (err)
568 dev_printk(KERN_ERR, &session->dev, "iscsi: Could not delete "
569 "all connections for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500570
Mike Christie7b8631b2006-01-13 18:05:50 -0600571 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500572 device_del(&session->dev);
573}
574EXPORT_SYMBOL_GPL(iscsi_remove_session);
575
576void iscsi_free_session(struct iscsi_cls_session *session)
577{
Mike Christie26974782007-12-13 12:43:29 -0600578 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500579 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600580}
Mike Christie8434aa82006-06-28 12:00:30 -0500581EXPORT_SYMBOL_GPL(iscsi_free_session);
582
583/**
584 * iscsi_destroy_session - destroy iscsi session
585 * @session: iscsi_session
586 *
587 * Can be called by a LLD or iscsi_transport. There must not be
588 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500589 */
Mike Christie8434aa82006-06-28 12:00:30 -0500590int iscsi_destroy_session(struct iscsi_cls_session *session)
591{
592 iscsi_remove_session(session);
593 iscsi_free_session(session);
594 return 0;
595}
Mike Christie7b8631b2006-01-13 18:05:50 -0600596EXPORT_SYMBOL_GPL(iscsi_destroy_session);
597
Mike Christie7b8631b2006-01-13 18:05:50 -0600598/**
599 * iscsi_create_conn - create iscsi class connection
600 * @session: iscsi cls session
601 * @cid: connection id
602 *
603 * This can be called from a LLD or iscsi_transport. The connection
604 * is child of the session so cid must be unique for all connections
605 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500606 *
607 * Since we do not support MCS, cid will normally be zero. In some cases
608 * for software iscsi we could be trying to preallocate a connection struct
609 * in which case there could be two connection structs and cid would be
610 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500611 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600612struct iscsi_cls_conn *
613iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
614{
615 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600616 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600617 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600618 int err;
619
620 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
621 if (!conn)
622 return NULL;
623
624 if (transport->conndata_size)
625 conn->dd_data = &conn[1];
626
627 INIT_LIST_HEAD(&conn->conn_list);
628 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500629 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600630
631 /* this is released in the dev's release function */
632 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400633 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500634
Mike Christie7b8631b2006-01-13 18:05:50 -0600635 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500636 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600637 conn->dev.parent = &session->dev;
638 conn->dev.release = iscsi_conn_release;
639 err = device_register(&conn->dev);
640 if (err) {
641 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
642 "connection's dev\n");
643 goto release_parent_ref;
644 }
645 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600646
647 spin_lock_irqsave(&connlock, flags);
648 list_add(&conn->conn_list, &connlist);
649 conn->active = 1;
650 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600651 return conn;
652
653release_parent_ref:
654 put_device(&session->dev);
655free_conn:
656 kfree(conn);
657 return NULL;
658}
659
660EXPORT_SYMBOL_GPL(iscsi_create_conn);
661
662/**
663 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500664 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600665 *
Mike Christie26974782007-12-13 12:43:29 -0600666 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500667 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600668int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
669{
Mike Christie26974782007-12-13 12:43:29 -0600670 unsigned long flags;
671
672 spin_lock_irqsave(&connlock, flags);
673 conn->active = 0;
674 list_del(&conn->conn_list);
675 spin_unlock_irqrestore(&connlock, flags);
676
Mike Christie7b8631b2006-01-13 18:05:50 -0600677 transport_unregister_device(&conn->dev);
678 device_unregister(&conn->dev);
679 return 0;
680}
Mike Christie7b8631b2006-01-13 18:05:50 -0600681EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
682
683/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600684 * iscsi interface functions
685 */
Alex Aizman0896b752005-08-04 19:33:07 -0700686static struct iscsi_internal *
687iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Alex Aizman0896b752005-08-04 19:33:07 -0700689 struct iscsi_internal *priv;
690 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Alex Aizman0896b752005-08-04 19:33:07 -0700692 spin_lock_irqsave(&iscsi_transport_lock, flags);
693 list_for_each_entry(priv, &iscsi_transports, list) {
694 if (tt == priv->iscsi_transport) {
695 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
696 return priv;
697 }
698 }
699 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
700 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alex Aizman0896b752005-08-04 19:33:07 -0700703static int
Mike Christie43a145a2006-10-16 18:09:38 -0400704iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500705{
Mike Christie53cb8a12006-06-28 12:00:32 -0500706 int rc;
707
Mike Christie9aaa2b42006-07-24 15:47:34 -0500708 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500709 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500710 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
711 return rc;
712 }
713
Mike Christie53cb8a12006-06-28 12:00:32 -0500714 return 0;
715}
716
717static int
Mike Christie43a145a2006-10-16 18:09:38 -0400718iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700719{
Alex Aizman0896b752005-08-04 19:33:07 -0700720 int rc;
721
Mike Christie790f39a2006-05-18 20:31:39 -0500722 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700723 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700724 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
725 return rc;
726 }
727
Alex Aizman0896b752005-08-04 19:33:07 -0700728 return 0;
729}
730
Mike Christie7b7232f2006-02-01 21:06:49 -0600731int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700732 char *data, uint32_t data_size)
733{
734 struct nlmsghdr *nlh;
735 struct sk_buff *skb;
736 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700737 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500738 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700739 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
740 data_size);
741
Mike Christie790f39a2006-05-18 20:31:39 -0500742 priv = iscsi_if_transport_lookup(conn->transport);
743 if (!priv)
744 return -EINVAL;
745
Mike Christie43a145a2006-10-16 18:09:38 -0400746 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700747 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600748 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600749 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
750 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700751 return -ENOMEM;
752 }
753
Mike Christie790f39a2006-05-18 20:31:39 -0500754 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700755 ev = NLMSG_DATA(nlh);
756 memset(ev, 0, sizeof(*ev));
757 ev->transport_handle = iscsi_handle(conn->transport);
758 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500759 ev->r.recv_req.cid = conn->cid;
760 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700761 pdu = (char*)ev + sizeof(*ev);
762 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
763 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
764
Mike Christie43a145a2006-10-16 18:09:38 -0400765 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700766}
767EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
768
Mike Christie7b7232f2006-02-01 21:06:49 -0600769void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700770{
Mike Christie6eabafb2008-01-31 13:36:43 -0600771 struct iscsi_cls_session *session = iscsi_conn_to_session(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700772 struct nlmsghdr *nlh;
773 struct sk_buff *skb;
774 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500775 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700776 int len = NLMSG_SPACE(sizeof(*ev));
Mike Christie6eabafb2008-01-31 13:36:43 -0600777 unsigned long flags;
Alex Aizman0896b752005-08-04 19:33:07 -0700778
Mike Christie790f39a2006-05-18 20:31:39 -0500779 priv = iscsi_if_transport_lookup(conn->transport);
780 if (!priv)
781 return;
782
Mike Christie6eabafb2008-01-31 13:36:43 -0600783 spin_lock_irqsave(&session->lock, flags);
784 if (session->state == ISCSI_SESSION_LOGGED_IN)
785 session->state = ISCSI_SESSION_FAILED;
786 spin_unlock_irqrestore(&session->lock, flags);
787
Mike Christie43a145a2006-10-16 18:09:38 -0400788 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700789 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600790 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
791 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700792 return;
793 }
794
Mike Christie790f39a2006-05-18 20:31:39 -0500795 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700796 ev = NLMSG_DATA(nlh);
797 ev->transport_handle = iscsi_handle(conn->transport);
798 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700799 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500800 ev->r.connerror.cid = conn->cid;
801 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700802
Mike Christie43a145a2006-10-16 18:09:38 -0400803 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700804
Mike Christie7b8631b2006-01-13 18:05:50 -0600805 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
806 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700807}
808EXPORT_SYMBOL_GPL(iscsi_conn_error);
809
810static int
811iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
812 void *payload, int size)
813{
814 struct sk_buff *skb;
815 struct nlmsghdr *nlh;
816 int len = NLMSG_SPACE(size);
817 int flags = multi ? NLM_F_MULTI : 0;
818 int t = done ? NLMSG_DONE : type;
819
Mike Christie43a145a2006-10-16 18:09:38 -0400820 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500821 if (!skb) {
822 printk(KERN_ERR "Could not allocate skb to send reply.\n");
823 return -ENOMEM;
824 }
Alex Aizman0896b752005-08-04 19:33:07 -0700825
826 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
827 nlh->nlmsg_flags = flags;
828 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400829 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700830}
831
832static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600833iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700834{
835 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
836 struct iscsi_stats *stats;
837 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600838 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700839 struct nlmsghdr *nlhstat;
840 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500841 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700842 int len = NLMSG_SPACE(sizeof(*ev) +
843 sizeof(struct iscsi_stats) +
844 sizeof(struct iscsi_stats_custom) *
845 ISCSI_STATS_CUSTOM_MAX);
846 int err = 0;
847
Mike Christie790f39a2006-05-18 20:31:39 -0500848 priv = iscsi_if_transport_lookup(transport);
849 if (!priv)
850 return -EINVAL;
851
Mike Christieb5c7a122006-04-06 21:13:33 -0500852 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700853 if (!conn)
854 return -EEXIST;
855
856 do {
857 int actual_size;
858
Mike Christie43a145a2006-10-16 18:09:38 -0400859 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700860 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600861 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
862 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700863 return -ENOMEM;
864 }
865
Mike Christie790f39a2006-05-18 20:31:39 -0500866 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700867 (len - sizeof(*nlhstat)), 0);
868 evstat = NLMSG_DATA(nlhstat);
869 memset(evstat, 0, sizeof(*evstat));
870 evstat->transport_handle = iscsi_handle(conn->transport);
871 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -0500872 evstat->u.get_stats.cid =
873 ev->u.get_stats.cid;
874 evstat->u.get_stats.sid =
875 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700876 stats = (struct iscsi_stats *)
877 ((char*)evstat + sizeof(*evstat));
878 memset(stats, 0, sizeof(*stats));
879
Mike Christie7b7232f2006-02-01 21:06:49 -0600880 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700881 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
882 sizeof(struct iscsi_stats) +
883 sizeof(struct iscsi_stats_custom) *
884 stats->custom_length);
885 actual_size -= sizeof(*nlhstat);
886 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600887 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700888 nlhstat->nlmsg_len = actual_size;
889
Mike Christie43a145a2006-10-16 18:09:38 -0400890 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700891 } while (err < 0 && err != -ECONNREFUSED);
892
893 return err;
894}
895
Mike Christie53cb8a12006-06-28 12:00:32 -0500896/**
Mike Christie26974782007-12-13 12:43:29 -0600897 * iscsi_session_event - send session destr. completion event
898 * @session: iscsi class session
899 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -0500900 */
Mike Christie26974782007-12-13 12:43:29 -0600901int iscsi_session_event(struct iscsi_cls_session *session,
902 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -0500903{
904 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -0500905 struct Scsi_Host *shost;
906 struct iscsi_uevent *ev;
907 struct sk_buff *skb;
908 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -0500909 int rc, len = NLMSG_SPACE(sizeof(*ev));
910
Mike Christie26974782007-12-13 12:43:29 -0600911 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -0500912 if (!priv)
913 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -0500914 shost = iscsi_session_to_shost(session);
915
Mike Christie43a145a2006-10-16 18:09:38 -0400916 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500917 if (!skb) {
Mike Christie26974782007-12-13 12:43:29 -0600918 dev_printk(KERN_ERR, &session->dev, "Cannot notify userspace "
919 "of session event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500920 return -ENOMEM;
921 }
922
923 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
924 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -0600925 ev->transport_handle = iscsi_handle(session->transport);
926
927 ev->type = event;
928 switch (event) {
929 case ISCSI_KEVENT_DESTROY_SESSION:
930 ev->r.d_session.host_no = shost->host_no;
931 ev->r.d_session.sid = session->sid;
932 break;
933 case ISCSI_KEVENT_CREATE_SESSION:
934 ev->r.c_session_ret.host_no = shost->host_no;
935 ev->r.c_session_ret.sid = session->sid;
936 break;
937 case ISCSI_KEVENT_UNBIND_SESSION:
938 ev->r.unbind_session.host_no = shost->host_no;
939 ev->r.unbind_session.sid = session->sid;
940 break;
941 default:
942 dev_printk(KERN_ERR, &session->dev, "Invalid event %u.\n",
943 event);
944 kfree_skb(skb);
945 return -EINVAL;
946 }
Mike Christie53cb8a12006-06-28 12:00:32 -0500947
948 /*
949 * this will occur if the daemon is not up, so we just warn
950 * the user and when the daemon is restarted it will handle it
951 */
Mike Christie43a145a2006-10-16 18:09:38 -0400952 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500953 if (rc < 0)
Mike Christie26974782007-12-13 12:43:29 -0600954 dev_printk(KERN_ERR, &session->dev, "Cannot notify userspace "
955 "of session event %u. Check iscsi daemon\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500956 return rc;
957}
Mike Christie26974782007-12-13 12:43:29 -0600958EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500959
Alex Aizman0896b752005-08-04 19:33:07 -0700960static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600961iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
962{
963 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600964 struct iscsi_cls_session *session;
Mike Christieb5c7a122006-04-06 21:13:33 -0500965 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600966
Mike Christie7996a772006-04-06 21:13:41 -0500967 session = transport->create_session(transport, &priv->t,
Mike Christie15482712007-05-30 12:57:19 -0500968 ev->u.c_session.cmds_max,
969 ev->u.c_session.queue_depth,
Mike Christie7b7232f2006-02-01 21:06:49 -0600970 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500971 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600972 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600973 return -ENOMEM;
974
Mike Christieb5c7a122006-04-06 21:13:33 -0500975 ev->r.c_session_ret.host_no = hostno;
976 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600977 return 0;
978}
979
980static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600981iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600982{
Mike Christie7b8631b2006-01-13 18:05:50 -0600983 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600984 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600985
Mike Christieb5c7a122006-04-06 21:13:33 -0500986 session = iscsi_session_lookup(ev->u.c_conn.sid);
987 if (!session) {
988 printk(KERN_ERR "iscsi: invalid session %d\n",
989 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600990 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500991 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600992
Mike Christie7b7232f2006-02-01 21:06:49 -0600993 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500994 if (!conn) {
995 printk(KERN_ERR "iscsi: couldn't create a new "
996 "connection for session %d\n",
997 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600998 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500999 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001000
Mike Christieb5c7a122006-04-06 21:13:33 -05001001 ev->r.c_conn_ret.sid = session->sid;
1002 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001003 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001004}
1005
1006static int
1007iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1008{
Mike Christie7b8631b2006-01-13 18:05:50 -06001009 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001010
Mike Christieb5c7a122006-04-06 21:13:33 -05001011 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001012 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001013 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001014
Mike Christie7b8631b2006-01-13 18:05:50 -06001015 if (transport->destroy_conn)
1016 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001017 return 0;
1018}
1019
Mike Christiefd7255f2006-04-06 21:13:36 -05001020static int
1021iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1022{
1023 char *data = (char*)ev + sizeof(*ev);
1024 struct iscsi_cls_conn *conn;
1025 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001026 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001027
1028 session = iscsi_session_lookup(ev->u.set_param.sid);
1029 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1030 if (!conn || !session)
1031 return -EINVAL;
1032
1033 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001034 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001035 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001036 if (value != 0)
1037 session->recovery_tmo = value;
1038 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001039 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001040 err = transport->set_param(conn, ev->u.set_param.param,
1041 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001042 }
1043
1044 return err;
1045}
1046
Mike Christie7b8631b2006-01-13 18:05:50 -06001047static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001048iscsi_if_transport_ep(struct iscsi_transport *transport,
1049 struct iscsi_uevent *ev, int msg_type)
1050{
1051 struct sockaddr *dst_addr;
1052 int rc = 0;
1053
1054 switch (msg_type) {
1055 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1056 if (!transport->ep_connect)
1057 return -EINVAL;
1058
1059 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1060 rc = transport->ep_connect(dst_addr,
1061 ev->u.ep_connect.non_blocking,
1062 &ev->r.ep_connect_ret.handle);
1063 break;
1064 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1065 if (!transport->ep_poll)
1066 return -EINVAL;
1067
1068 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1069 ev->u.ep_poll.timeout_ms);
1070 break;
1071 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1072 if (!transport->ep_disconnect)
1073 return -EINVAL;
1074
1075 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1076 break;
1077 }
1078 return rc;
1079}
1080
1081static int
Mike Christie01cb2252006-06-28 12:00:22 -05001082iscsi_tgt_dscvr(struct iscsi_transport *transport,
1083 struct iscsi_uevent *ev)
1084{
Mike Christie2174a042007-05-30 12:57:10 -05001085 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001086 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001087 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001088
1089 if (!transport->tgt_dscvr)
1090 return -EINVAL;
1091
Mike Christie2174a042007-05-30 12:57:10 -05001092 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1093 if (IS_ERR(shost)) {
1094 printk(KERN_ERR "target discovery could not find host no %u\n",
1095 ev->u.tgt_dscvr.host_no);
1096 return -ENODEV;
1097 }
1098
1099
Mike Christie01cb2252006-06-28 12:00:22 -05001100 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001101 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1102 ev->u.tgt_dscvr.enable, dst_addr);
1103 scsi_host_put(shost);
1104 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001105}
1106
1107static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001108iscsi_set_host_param(struct iscsi_transport *transport,
1109 struct iscsi_uevent *ev)
1110{
1111 char *data = (char*)ev + sizeof(*ev);
1112 struct Scsi_Host *shost;
1113 int err;
1114
1115 if (!transport->set_host_param)
1116 return -ENOSYS;
1117
1118 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1119 if (IS_ERR(shost)) {
1120 printk(KERN_ERR "set_host_param could not find host no %u\n",
1121 ev->u.set_host_param.host_no);
1122 return -ENODEV;
1123 }
1124
1125 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1126 data, ev->u.set_host_param.len);
1127 scsi_host_put(shost);
1128 return err;
1129}
1130
1131static int
Alex Aizman0896b752005-08-04 19:33:07 -07001132iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1133{
1134 int err = 0;
1135 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1136 struct iscsi_transport *transport = NULL;
1137 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001138 struct iscsi_cls_session *session;
1139 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001140
Alex Aizman0896b752005-08-04 19:33:07 -07001141 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1142 if (!priv)
1143 return -EINVAL;
1144 transport = priv->iscsi_transport;
1145
Mike Christie7b7232f2006-02-01 21:06:49 -06001146 if (!try_module_get(transport->owner))
1147 return -EINVAL;
1148
Mike Christie790f39a2006-05-18 20:31:39 -05001149 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1150
Alex Aizman0896b752005-08-04 19:33:07 -07001151 switch (nlh->nlmsg_type) {
1152 case ISCSI_UEVENT_CREATE_SESSION:
1153 err = iscsi_if_create_session(priv, ev);
1154 break;
1155 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001156 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001157 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001158 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001159 else
1160 err = -EINVAL;
1161 break;
1162 case ISCSI_UEVENT_UNBIND_SESSION:
1163 session = iscsi_session_lookup(ev->u.d_session.sid);
1164 if (session)
1165 iscsi_unbind_session(session);
1166 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001167 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001168 break;
1169 case ISCSI_UEVENT_CREATE_CONN:
1170 err = iscsi_if_create_conn(transport, ev);
1171 break;
1172 case ISCSI_UEVENT_DESTROY_CONN:
1173 err = iscsi_if_destroy_conn(transport, ev);
1174 break;
1175 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001176 session = iscsi_session_lookup(ev->u.b_conn.sid);
1177 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001178
1179 if (session && conn)
1180 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001181 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001182 ev->u.b_conn.is_leading);
1183 else
1184 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001185 break;
1186 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001187 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001188 break;
1189 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001190 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001191 if (conn)
1192 ev->r.retcode = transport->start_conn(conn);
1193 else
1194 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001195 break;
1196 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001197 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001198 if (conn)
1199 transport->stop_conn(conn, ev->u.stop_conn.flag);
1200 else
1201 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001202 break;
1203 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001204 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001205 if (conn)
1206 ev->r.retcode = transport->send_pdu(conn,
1207 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1208 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1209 ev->u.send_pdu.data_size);
1210 else
1211 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001212 break;
1213 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001214 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001215 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001216 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1217 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1218 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1219 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1220 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001221 case ISCSI_UEVENT_TGT_DSCVR:
1222 err = iscsi_tgt_dscvr(transport, ev);
1223 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001224 case ISCSI_UEVENT_SET_HOST_PARAM:
1225 err = iscsi_set_host_param(transport, ev);
1226 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001227 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001228 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001229 break;
1230 }
1231
Mike Christie7b7232f2006-02-01 21:06:49 -06001232 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001233 return err;
1234}
1235
Mike Christieb5c7a122006-04-06 21:13:33 -05001236/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001237 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1238 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001239 */
Alex Aizman0896b752005-08-04 19:33:07 -07001240static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001241iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001242{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001243 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001244 while (skb->len >= NLMSG_SPACE(0)) {
1245 int err;
1246 uint32_t rlen;
1247 struct nlmsghdr *nlh;
1248 struct iscsi_uevent *ev;
1249
1250 nlh = nlmsg_hdr(skb);
1251 if (nlh->nlmsg_len < sizeof(*nlh) ||
1252 skb->len < nlh->nlmsg_len) {
1253 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001254 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001255
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001256 ev = NLMSG_DATA(nlh);
1257 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1258 if (rlen > skb->len)
1259 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001260
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001261 err = iscsi_if_recv_msg(skb, nlh);
1262 if (err) {
1263 ev->type = ISCSI_KEVENT_IF_ERROR;
1264 ev->iferror = err;
1265 }
1266 do {
1267 /*
1268 * special case for GET_STATS:
1269 * on success - sending reply and stats from
1270 * inside of if_recv_msg(),
1271 * on error - fall through.
1272 */
1273 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001274 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001275 err = iscsi_if_send_reply(
1276 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1277 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1278 } while (err < 0 && err != -ECONNREFUSED);
1279 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001280 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001281 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001282}
1283
Mike Christie7b8631b2006-01-13 18:05:50 -06001284#define iscsi_cdev_to_conn(_cdev) \
1285 iscsi_dev_to_conn(_cdev->dev)
1286
Mike Christiefd7255f2006-04-06 21:13:36 -05001287#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1288struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1289 __ATTR(_name,_mode,_show,_store)
1290
Alex Aizman0896b752005-08-04 19:33:07 -07001291/*
1292 * iSCSI connection attrs
1293 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001294#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001296show_conn_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001297{ \
1298 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1299 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001300 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001301}
1302
Mike Christiea54a52c2006-06-28 12:00:23 -05001303#define iscsi_conn_attr(field, param) \
1304 iscsi_conn_attr_show(param) \
1305static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001306 NULL);
1307
Mike Christiea54a52c2006-06-28 12:00:23 -05001308iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1309iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1310iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1311iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1312iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1313iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1314iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1315iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1316iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1317iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1318iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001319iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1320iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Mike Christie7b8631b2006-01-13 18:05:50 -06001322#define iscsi_cdev_to_session(_cdev) \
1323 iscsi_dev_to_session(_cdev->dev)
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
Alex Aizman0896b752005-08-04 19:33:07 -07001326 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Mike Christieb2c64162007-05-30 12:57:16 -05001328#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001330show_session_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001331{ \
1332 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1333 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001334 \
1335 if (perm && !capable(CAP_SYS_ADMIN)) \
1336 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001337 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Mike Christieb2c64162007-05-30 12:57:16 -05001340#define iscsi_session_attr(field, param, perm) \
1341 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001342static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001343 NULL);
1344
Mike Christieb2c64162007-05-30 12:57:16 -05001345iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1346iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1347iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1348iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1349iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1350iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1351iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1352iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1353iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1354iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1355iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1356iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1357iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1358iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001359iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1360iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1361iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001362
Mike Christie6eabafb2008-01-31 13:36:43 -06001363static ssize_t
1364show_priv_session_state(struct class_device *cdev, char *buf)
1365{
1366 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);
1367 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1368}
1369static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1370 NULL);
1371
Mike Christiefd7255f2006-04-06 21:13:36 -05001372#define iscsi_priv_session_attr_show(field, format) \
1373static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001374show_priv_session_##field(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001375{ \
Mike Christiea54a52c2006-06-28 12:00:23 -05001376 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
Mike Christiefd7255f2006-04-06 21:13:36 -05001377 return sprintf(buf, format"\n", session->field); \
1378}
1379
1380#define iscsi_priv_session_attr(field, format) \
1381 iscsi_priv_session_attr_show(field, format) \
1382static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1383 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001384iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001385
Mike Christie1819dc82007-05-30 12:57:08 -05001386/*
1387 * iSCSI host attrs
1388 */
1389#define iscsi_host_attr_show(param) \
1390static ssize_t \
1391show_host_param_##param(struct class_device *cdev, char *buf) \
1392{ \
1393 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1394 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1395 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1396}
1397
1398#define iscsi_host_attr(field, param) \
1399 iscsi_host_attr_show(param) \
1400static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1401 NULL);
1402
Mike Christied8196ed2007-05-30 12:57:25 -05001403iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001404iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001405iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001406iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001407
Mike Christiefd7255f2006-04-06 21:13:36 -05001408#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1409do { \
1410 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1411 count++; \
1412} while (0)
1413
Mike Christiea54a52c2006-06-28 12:00:23 -05001414
Mike Christiefd7255f2006-04-06 21:13:36 -05001415#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1416do { \
1417 if (tt->param_mask & param_flag) { \
1418 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001420 } \
1421} while (0)
1422
Mike Christiefd7255f2006-04-06 21:13:36 -05001423#define SETUP_CONN_RD_ATTR(field, param_flag) \
1424do { \
1425 if (tt->param_mask & param_flag) { \
1426 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1427 count++; \
1428 } \
1429} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Mike Christie1819dc82007-05-30 12:57:08 -05001431#define SETUP_HOST_RD_ATTR(field, param_flag) \
1432do { \
1433 if (tt->host_param_mask & param_flag) { \
1434 priv->host_attrs[count] = &class_device_attr_host_##field; \
1435 count++; \
1436 } \
1437} while (0)
1438
Alex Aizman0896b752005-08-04 19:33:07 -07001439static int iscsi_session_match(struct attribute_container *cont,
1440 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Mike Christie7b8631b2006-01-13 18:05:50 -06001442 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001444 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alex Aizman0896b752005-08-04 19:33:07 -07001446 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448
Mike Christie7b8631b2006-01-13 18:05:50 -06001449 session = iscsi_dev_to_session(dev);
1450 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001451 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453
Alex Aizman0896b752005-08-04 19:33:07 -07001454 priv = to_iscsi_internal(shost->transportt);
1455 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1456 return 0;
1457
1458 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Alex Aizman0896b752005-08-04 19:33:07 -07001461static int iscsi_conn_match(struct attribute_container *cont,
1462 struct device *dev)
1463{
Mike Christie7b8631b2006-01-13 18:05:50 -06001464 struct iscsi_cls_session *session;
1465 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001466 struct Scsi_Host *shost;
1467 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Alex Aizman0896b752005-08-04 19:33:07 -07001469 if (!iscsi_is_conn_dev(dev))
1470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Mike Christie7b8631b2006-01-13 18:05:50 -06001472 conn = iscsi_dev_to_conn(dev);
1473 session = iscsi_dev_to_session(conn->dev.parent);
1474 shost = iscsi_session_to_shost(session);
1475
Alex Aizman0896b752005-08-04 19:33:07 -07001476 if (!shost->transportt)
1477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alex Aizman0896b752005-08-04 19:33:07 -07001479 priv = to_iscsi_internal(shost->transportt);
1480 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1481 return 0;
1482
1483 return &priv->conn_cont.ac == cont;
1484}
1485
Mike Christie30a6c652006-04-06 21:13:39 -05001486static int iscsi_host_match(struct attribute_container *cont,
1487 struct device *dev)
1488{
1489 struct Scsi_Host *shost;
1490 struct iscsi_internal *priv;
1491
1492 if (!scsi_is_host_device(dev))
1493 return 0;
1494
1495 shost = dev_to_shost(dev);
1496 if (!shost->transportt ||
1497 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1498 return 0;
1499
1500 priv = to_iscsi_internal(shost->transportt);
1501 return &priv->t.host_attrs.ac == cont;
1502}
1503
Mike Christie7b8631b2006-01-13 18:05:50 -06001504struct scsi_transport_template *
1505iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001506{
1507 struct iscsi_internal *priv;
1508 unsigned long flags;
1509 int count = 0, err;
1510
1511 BUG_ON(!tt);
1512
1513 priv = iscsi_if_transport_lookup(tt);
1514 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001515 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001516
Jes Sorensen24669f752006-01-16 10:31:18 -05001517 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001518 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001519 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001520 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001521 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001522 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001523 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001524
1525 priv->cdev.class = &iscsi_transport_class;
1526 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1527 err = class_device_register(&priv->cdev);
1528 if (err)
1529 goto free_priv;
1530
1531 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1532 if (err)
1533 goto unregister_cdev;
1534
Mike Christie30a6c652006-04-06 21:13:39 -05001535 /* host parameters */
1536 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1537 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1538 priv->t.host_attrs.ac.match = iscsi_host_match;
1539 priv->t.host_size = sizeof(struct iscsi_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001540 transport_container_register(&priv->t.host_attrs);
1541
Mike Christied8196ed2007-05-30 12:57:25 -05001542 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001543 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001544 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001545 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001546 BUG_ON(count > ISCSI_HOST_ATTRS);
1547 priv->host_attrs[count] = NULL;
1548 count = 0;
1549
Alex Aizman0896b752005-08-04 19:33:07 -07001550 /* connection parameters */
1551 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1552 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1553 priv->conn_cont.ac.match = iscsi_conn_match;
1554 transport_container_register(&priv->conn_cont);
1555
Mike Christiefd7255f2006-04-06 21:13:36 -05001556 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1557 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1558 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1559 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1560 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1561 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1562 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1563 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001564 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001565 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1566 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001567 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1568 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001569
1570 BUG_ON(count > ISCSI_CONN_ATTRS);
1571 priv->conn_attrs[count] = NULL;
1572 count = 0;
1573
1574 /* session parameters */
1575 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1576 priv->session_cont.ac.class = &iscsi_session_class.class;
1577 priv->session_cont.ac.match = iscsi_session_match;
1578 transport_container_register(&priv->session_cont);
1579
Mike Christiefd7255f2006-04-06 21:13:36 -05001580 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1581 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1582 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1583 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1584 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1585 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1586 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1587 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001588 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1589 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001590 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1591 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1592 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1593 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001594 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001595 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1596 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie30a6c652006-04-06 21:13:39 -05001597 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001598 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001601 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Alex Aizman0896b752005-08-04 19:33:07 -07001603 spin_lock_irqsave(&iscsi_transport_lock, flags);
1604 list_add(&priv->list, &iscsi_transports);
1605 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Alex Aizman0896b752005-08-04 19:33:07 -07001607 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001608 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Alex Aizman0896b752005-08-04 19:33:07 -07001610unregister_cdev:
1611 class_device_unregister(&priv->cdev);
1612free_priv:
1613 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
Alex Aizman0896b752005-08-04 19:33:07 -07001616EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Alex Aizman0896b752005-08-04 19:33:07 -07001618int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Alex Aizman0896b752005-08-04 19:33:07 -07001620 struct iscsi_internal *priv;
1621 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Alex Aizman0896b752005-08-04 19:33:07 -07001623 BUG_ON(!tt);
1624
Arjan van de Ven0b950672006-01-11 13:16:10 +01001625 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001626
1627 priv = iscsi_if_transport_lookup(tt);
1628 BUG_ON (!priv);
1629
Alex Aizman0896b752005-08-04 19:33:07 -07001630 spin_lock_irqsave(&iscsi_transport_lock, flags);
1631 list_del(&priv->list);
1632 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1633
1634 transport_container_unregister(&priv->conn_cont);
1635 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001636 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001637
1638 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1639 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001640 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001641
1642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646static __init int iscsi_transport_init(void)
1647{
Alex Aizman0896b752005-08-04 19:33:07 -07001648 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Meelis Roos09492602006-12-17 12:10:26 -06001650 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001651 ISCSI_TRANSPORT_VERSION);
1652
Mike Christie41be1442007-02-28 17:32:18 -06001653 atomic_set(&iscsi_session_nr, 0);
1654
Alex Aizman0896b752005-08-04 19:33:07 -07001655 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (err)
1657 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001658
Mike Christie30a6c652006-04-06 21:13:39 -05001659 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001660 if (err)
1661 goto unregister_transport_class;
1662
Mike Christie30a6c652006-04-06 21:13:39 -05001663 err = transport_class_register(&iscsi_connection_class);
1664 if (err)
1665 goto unregister_host_class;
1666
Alex Aizman0896b752005-08-04 19:33:07 -07001667 err = transport_class_register(&iscsi_session_class);
1668 if (err)
1669 goto unregister_conn_class;
1670
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001671 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001672 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001673 if (!nls) {
1674 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001675 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001676 }
1677
Mike Christied8bf5412007-12-13 12:43:27 -06001678 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1679 if (!iscsi_eh_timer_workq)
1680 goto release_nls;
1681
Mike Christie43a145a2006-10-16 18:09:38 -04001682 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001683
Mike Christied8bf5412007-12-13 12:43:27 -06001684release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001685 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001686unregister_session_class:
1687 transport_class_unregister(&iscsi_session_class);
1688unregister_conn_class:
1689 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001690unregister_host_class:
1691 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001692unregister_transport_class:
1693 class_unregister(&iscsi_transport_class);
1694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697static void __exit iscsi_transport_exit(void)
1698{
Mike Christied8bf5412007-12-13 12:43:27 -06001699 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001700 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001701 transport_class_unregister(&iscsi_connection_class);
1702 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001703 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001704 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707module_init(iscsi_transport_init);
1708module_exit(iscsi_transport_exit);
1709
Alex Aizman0896b752005-08-04 19:33:07 -07001710MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1711 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1712 "Alex Aizman <itn780@yahoo.com>");
1713MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001715MODULE_VERSION(ISCSI_TRANSPORT_VERSION);