blob: 0d7b4e79415c6b141256e7f91328c12d2a86c56d [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 Christie4cd49ea2007-12-13 12:43:38 -060033#define ISCSI_SESSION_ATTRS 18
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
131 snprintf(ihost->unbind_workq_name, KOBJ_NAME_LEN, "iscsi_unbind_%d",
132 shost->host_no);
133 ihost->unbind_workq = create_singlethread_workqueue(
134 ihost->unbind_workq_name);
135 if (!ihost->unbind_workq)
136 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
146 destroy_workqueue(ihost->unbind_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 */
224static void iscsi_session_release(struct device *dev)
225{
226 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600227 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Mike Christie7b8631b2006-01-13 18:05:50 -0600229 shost = iscsi_session_to_shost(session);
230 scsi_host_put(shost);
231 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mike Christie7b8631b2006-01-13 18:05:50 -0600234static int iscsi_is_session_dev(const struct device *dev)
235{
236 return dev->release == iscsi_session_release;
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Mike Christie30a6c652006-04-06 21:13:39 -0500239static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
240 uint id, uint lun)
241{
242 struct iscsi_host *ihost = shost->shost_data;
243 struct iscsi_cls_session *session;
244
245 mutex_lock(&ihost->mutex);
246 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500247 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500248 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500249 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500250 session->target_id, lun, 1);
251 }
252 mutex_unlock(&ihost->mutex);
253
254 return 0;
255}
256
David Howellsc4028952006-11-22 14:57:56 +0000257static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500258{
David Howellsc4028952006-11-22 14:57:56 +0000259 struct iscsi_cls_session *session =
260 container_of(work, struct iscsi_cls_session,
261 recovery_work.work);
Mike Christie30a6c652006-04-06 21:13:39 -0500262
Or Gerlitzbe2df722006-05-02 19:46:43 -0500263 dev_printk(KERN_INFO, &session->dev, "iscsi: session recovery timed "
264 "out after %d secs\n", session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500265
266 if (session->transport->session_recovery_timedout)
267 session->transport->session_recovery_timedout(session);
268
269 scsi_target_unblock(&session->dev);
270}
271
272void iscsi_unblock_session(struct iscsi_cls_session *session)
273{
274 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600275 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500276 scsi_target_unblock(&session->dev);
277}
278EXPORT_SYMBOL_GPL(iscsi_unblock_session);
279
280void iscsi_block_session(struct iscsi_cls_session *session)
281{
282 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600283 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
284 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500285}
286EXPORT_SYMBOL_GPL(iscsi_block_session);
287
Mike Christie26974782007-12-13 12:43:29 -0600288static void __iscsi_unbind_session(struct work_struct *work)
289{
290 struct iscsi_cls_session *session =
291 container_of(work, struct iscsi_cls_session,
292 unbind_work);
293 struct Scsi_Host *shost = iscsi_session_to_shost(session);
294 struct iscsi_host *ihost = shost->shost_data;
295
296 /* Prevent new scans and make sure scanning is not in progress */
297 mutex_lock(&ihost->mutex);
298 if (list_empty(&session->host_list)) {
299 mutex_unlock(&ihost->mutex);
300 return;
301 }
302 list_del_init(&session->host_list);
303 mutex_unlock(&ihost->mutex);
304
305 scsi_remove_target(&session->dev);
306 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
307}
308
309static int iscsi_unbind_session(struct iscsi_cls_session *session)
310{
311 struct Scsi_Host *shost = iscsi_session_to_shost(session);
312 struct iscsi_host *ihost = shost->shost_data;
313
314 return queue_work(ihost->unbind_workq, &session->unbind_work);
315}
316
Mike Christie7b8631b2006-01-13 18:05:50 -0600317struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500318iscsi_alloc_session(struct Scsi_Host *shost,
319 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600320{
321 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Mike Christieb5c7a122006-04-06 21:13:33 -0500323 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
324 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600325 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500326 return NULL;
327
Mike Christie7b8631b2006-01-13 18:05:50 -0600328 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500329 session->recovery_tmo = 120;
David Howellsc4028952006-11-22 14:57:56 +0000330 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500331 INIT_LIST_HEAD(&session->host_list);
332 INIT_LIST_HEAD(&session->sess_list);
Mike Christie26974782007-12-13 12:43:29 -0600333 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600334
Mike Christie6a8a0d32006-06-28 12:00:31 -0500335 /* this is released in the dev's release function */
336 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500337 session->dev.parent = &shost->shost_gendev;
338 session->dev.release = iscsi_session_release;
339 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500340 if (transport->sessiondata_size)
341 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500342 return session;
343}
344EXPORT_SYMBOL_GPL(iscsi_alloc_session);
345
Mike Christie6a8a0d32006-06-28 12:00:31 -0500346int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500347{
348 struct Scsi_Host *shost = iscsi_session_to_shost(session);
349 struct iscsi_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600350 unsigned long flags;
Mike Christie8434aa82006-06-28 12:00:30 -0500351 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500352
Mike Christie30a6c652006-04-06 21:13:39 -0500353 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600354 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie6a8a0d32006-06-28 12:00:31 -0500355 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500356
Mike Christieb5c7a122006-04-06 21:13:33 -0500357 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
358 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500359 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600360 if (err) {
361 dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
362 "register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500363 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600364 }
365 transport_register_device(&session->dev);
366
Mike Christie26974782007-12-13 12:43:29 -0600367 spin_lock_irqsave(&sesslock, flags);
368 list_add(&session->sess_list, &sesslist);
369 spin_unlock_irqrestore(&sesslock, flags);
370
Mike Christie30a6c652006-04-06 21:13:39 -0500371 mutex_lock(&ihost->mutex);
372 list_add(&session->host_list, &ihost->sessions);
373 mutex_unlock(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600374
375 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500376 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500377
Mike Christie8434aa82006-06-28 12:00:30 -0500378release_host:
379 scsi_host_put(shost);
380 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600381}
Mike Christie8434aa82006-06-28 12:00:30 -0500382EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600383
384/**
Mike Christie8434aa82006-06-28 12:00:30 -0500385 * iscsi_create_session - create iscsi class session
386 * @shost: scsi host
387 * @transport: iscsi transport
Rob Landleyeb448202007-11-03 13:30:39 -0500388 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600389 *
Mike Christie8434aa82006-06-28 12:00:30 -0500390 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500391 */
Mike Christie8434aa82006-06-28 12:00:30 -0500392struct iscsi_cls_session *
393iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500394 struct iscsi_transport *transport,
395 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500396{
397 struct iscsi_cls_session *session;
398
399 session = iscsi_alloc_session(shost, transport);
400 if (!session)
401 return NULL;
402
Mike Christie6a8a0d32006-06-28 12:00:31 -0500403 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500404 iscsi_free_session(session);
405 return NULL;
406 }
407 return session;
408}
409EXPORT_SYMBOL_GPL(iscsi_create_session);
410
Mike Christie26974782007-12-13 12:43:29 -0600411static void iscsi_conn_release(struct device *dev)
412{
413 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
414 struct device *parent = conn->dev.parent;
415
416 kfree(conn);
417 put_device(parent);
418}
419
420static int iscsi_is_conn_dev(const struct device *dev)
421{
422 return dev->release == iscsi_conn_release;
423}
424
425static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
426{
427 if (!iscsi_is_conn_dev(dev))
428 return 0;
429 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
430}
431
Mike Christie8434aa82006-06-28 12:00:30 -0500432void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600433{
Mike Christie30a6c652006-04-06 21:13:39 -0500434 struct Scsi_Host *shost = iscsi_session_to_shost(session);
435 struct iscsi_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600436 unsigned long flags;
437 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500438
Mike Christie26974782007-12-13 12:43:29 -0600439 spin_lock_irqsave(&sesslock, flags);
440 list_del(&session->sess_list);
441 spin_unlock_irqrestore(&sesslock, flags);
442
443 /*
444 * If we are blocked let commands flow again. The lld or iscsi
445 * layer should set up the queuecommand to fail commands.
446 */
447 iscsi_unblock_session(session);
448 iscsi_unbind_session(session);
449 /*
450 * If the session dropped while removing devices then we need to make
451 * sure it is not blocked
452 */
Mike Christie30a6c652006-04-06 21:13:39 -0500453 if (!cancel_delayed_work(&session->recovery_work))
Mike Christied8bf5412007-12-13 12:43:27 -0600454 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600455 flush_workqueue(ihost->unbind_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500456
Mike Christie26974782007-12-13 12:43:29 -0600457 /* hw iscsi may not have removed all connections from session */
458 err = device_for_each_child(&session->dev, NULL,
459 iscsi_iter_destroy_conn_fn);
460 if (err)
461 dev_printk(KERN_ERR, &session->dev, "iscsi: Could not delete "
462 "all connections for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500463
Mike Christie7b8631b2006-01-13 18:05:50 -0600464 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500465 device_del(&session->dev);
466}
467EXPORT_SYMBOL_GPL(iscsi_remove_session);
468
469void iscsi_free_session(struct iscsi_cls_session *session)
470{
Mike Christie26974782007-12-13 12:43:29 -0600471 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500472 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600473}
Mike Christie8434aa82006-06-28 12:00:30 -0500474EXPORT_SYMBOL_GPL(iscsi_free_session);
475
476/**
477 * iscsi_destroy_session - destroy iscsi session
478 * @session: iscsi_session
479 *
480 * Can be called by a LLD or iscsi_transport. There must not be
481 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500482 */
Mike Christie8434aa82006-06-28 12:00:30 -0500483int iscsi_destroy_session(struct iscsi_cls_session *session)
484{
485 iscsi_remove_session(session);
486 iscsi_free_session(session);
487 return 0;
488}
Mike Christie7b8631b2006-01-13 18:05:50 -0600489EXPORT_SYMBOL_GPL(iscsi_destroy_session);
490
Mike Christie7b8631b2006-01-13 18:05:50 -0600491/**
492 * iscsi_create_conn - create iscsi class connection
493 * @session: iscsi cls session
494 * @cid: connection id
495 *
496 * This can be called from a LLD or iscsi_transport. The connection
497 * is child of the session so cid must be unique for all connections
498 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500499 *
500 * Since we do not support MCS, cid will normally be zero. In some cases
501 * for software iscsi we could be trying to preallocate a connection struct
502 * in which case there could be two connection structs and cid would be
503 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500504 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600505struct iscsi_cls_conn *
506iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
507{
508 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600509 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600510 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600511 int err;
512
513 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
514 if (!conn)
515 return NULL;
516
517 if (transport->conndata_size)
518 conn->dd_data = &conn[1];
519
520 INIT_LIST_HEAD(&conn->conn_list);
521 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500522 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600523
524 /* this is released in the dev's release function */
525 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400526 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500527
Mike Christie7b8631b2006-01-13 18:05:50 -0600528 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500529 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600530 conn->dev.parent = &session->dev;
531 conn->dev.release = iscsi_conn_release;
532 err = device_register(&conn->dev);
533 if (err) {
534 dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register "
535 "connection's dev\n");
536 goto release_parent_ref;
537 }
538 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600539
540 spin_lock_irqsave(&connlock, flags);
541 list_add(&conn->conn_list, &connlist);
542 conn->active = 1;
543 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600544 return conn;
545
546release_parent_ref:
547 put_device(&session->dev);
548free_conn:
549 kfree(conn);
550 return NULL;
551}
552
553EXPORT_SYMBOL_GPL(iscsi_create_conn);
554
555/**
556 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500557 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600558 *
Mike Christie26974782007-12-13 12:43:29 -0600559 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500560 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600561int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
562{
Mike Christie26974782007-12-13 12:43:29 -0600563 unsigned long flags;
564
565 spin_lock_irqsave(&connlock, flags);
566 conn->active = 0;
567 list_del(&conn->conn_list);
568 spin_unlock_irqrestore(&connlock, flags);
569
Mike Christie7b8631b2006-01-13 18:05:50 -0600570 transport_unregister_device(&conn->dev);
571 device_unregister(&conn->dev);
572 return 0;
573}
Mike Christie7b8631b2006-01-13 18:05:50 -0600574EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
575
576/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600577 * iscsi interface functions
578 */
Alex Aizman0896b752005-08-04 19:33:07 -0700579static struct iscsi_internal *
580iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Alex Aizman0896b752005-08-04 19:33:07 -0700582 struct iscsi_internal *priv;
583 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Alex Aizman0896b752005-08-04 19:33:07 -0700585 spin_lock_irqsave(&iscsi_transport_lock, flags);
586 list_for_each_entry(priv, &iscsi_transports, list) {
587 if (tt == priv->iscsi_transport) {
588 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
589 return priv;
590 }
591 }
592 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
593 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Alex Aizman0896b752005-08-04 19:33:07 -0700596static int
Mike Christie43a145a2006-10-16 18:09:38 -0400597iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500598{
Mike Christie53cb8a12006-06-28 12:00:32 -0500599 int rc;
600
Mike Christie9aaa2b42006-07-24 15:47:34 -0500601 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500602 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500603 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
604 return rc;
605 }
606
Mike Christie53cb8a12006-06-28 12:00:32 -0500607 return 0;
608}
609
610static int
Mike Christie43a145a2006-10-16 18:09:38 -0400611iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700612{
Alex Aizman0896b752005-08-04 19:33:07 -0700613 int rc;
614
Mike Christie790f39a2006-05-18 20:31:39 -0500615 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700616 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700617 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
618 return rc;
619 }
620
Alex Aizman0896b752005-08-04 19:33:07 -0700621 return 0;
622}
623
Mike Christie7b7232f2006-02-01 21:06:49 -0600624int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700625 char *data, uint32_t data_size)
626{
627 struct nlmsghdr *nlh;
628 struct sk_buff *skb;
629 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700630 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500631 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700632 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
633 data_size);
634
Mike Christie790f39a2006-05-18 20:31:39 -0500635 priv = iscsi_if_transport_lookup(conn->transport);
636 if (!priv)
637 return -EINVAL;
638
Mike Christie43a145a2006-10-16 18:09:38 -0400639 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700640 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600641 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7b8631b2006-01-13 18:05:50 -0600642 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver "
643 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700644 return -ENOMEM;
645 }
646
Mike Christie790f39a2006-05-18 20:31:39 -0500647 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700648 ev = NLMSG_DATA(nlh);
649 memset(ev, 0, sizeof(*ev));
650 ev->transport_handle = iscsi_handle(conn->transport);
651 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500652 ev->r.recv_req.cid = conn->cid;
653 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700654 pdu = (char*)ev + sizeof(*ev);
655 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
656 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
657
Mike Christie43a145a2006-10-16 18:09:38 -0400658 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700659}
660EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
661
Mike Christie7b7232f2006-02-01 21:06:49 -0600662void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700663{
664 struct nlmsghdr *nlh;
665 struct sk_buff *skb;
666 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500667 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700668 int len = NLMSG_SPACE(sizeof(*ev));
669
Mike Christie790f39a2006-05-18 20:31:39 -0500670 priv = iscsi_if_transport_lookup(conn->transport);
671 if (!priv)
672 return;
673
Mike Christie43a145a2006-10-16 18:09:38 -0400674 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700675 if (!skb) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600676 dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored "
677 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700678 return;
679 }
680
Mike Christie790f39a2006-05-18 20:31:39 -0500681 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700682 ev = NLMSG_DATA(nlh);
683 ev->transport_handle = iscsi_handle(conn->transport);
684 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700685 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500686 ev->r.connerror.cid = conn->cid;
687 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700688
Mike Christie43a145a2006-10-16 18:09:38 -0400689 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700690
Mike Christie7b8631b2006-01-13 18:05:50 -0600691 dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n",
692 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700693}
694EXPORT_SYMBOL_GPL(iscsi_conn_error);
695
696static int
697iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
698 void *payload, int size)
699{
700 struct sk_buff *skb;
701 struct nlmsghdr *nlh;
702 int len = NLMSG_SPACE(size);
703 int flags = multi ? NLM_F_MULTI : 0;
704 int t = done ? NLMSG_DONE : type;
705
Mike Christie43a145a2006-10-16 18:09:38 -0400706 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500707 if (!skb) {
708 printk(KERN_ERR "Could not allocate skb to send reply.\n");
709 return -ENOMEM;
710 }
Alex Aizman0896b752005-08-04 19:33:07 -0700711
712 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
713 nlh->nlmsg_flags = flags;
714 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400715 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700716}
717
718static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600719iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700720{
721 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
722 struct iscsi_stats *stats;
723 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600724 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700725 struct nlmsghdr *nlhstat;
726 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500727 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700728 int len = NLMSG_SPACE(sizeof(*ev) +
729 sizeof(struct iscsi_stats) +
730 sizeof(struct iscsi_stats_custom) *
731 ISCSI_STATS_CUSTOM_MAX);
732 int err = 0;
733
Mike Christie790f39a2006-05-18 20:31:39 -0500734 priv = iscsi_if_transport_lookup(transport);
735 if (!priv)
736 return -EINVAL;
737
Mike Christieb5c7a122006-04-06 21:13:33 -0500738 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700739 if (!conn)
740 return -EEXIST;
741
742 do {
743 int actual_size;
744
Mike Christie43a145a2006-10-16 18:09:38 -0400745 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700746 if (!skbstat) {
Mike Christie7b8631b2006-01-13 18:05:50 -0600747 dev_printk(KERN_ERR, &conn->dev, "iscsi: can not "
748 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700749 return -ENOMEM;
750 }
751
Mike Christie790f39a2006-05-18 20:31:39 -0500752 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700753 (len - sizeof(*nlhstat)), 0);
754 evstat = NLMSG_DATA(nlhstat);
755 memset(evstat, 0, sizeof(*evstat));
756 evstat->transport_handle = iscsi_handle(conn->transport);
757 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -0500758 evstat->u.get_stats.cid =
759 ev->u.get_stats.cid;
760 evstat->u.get_stats.sid =
761 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700762 stats = (struct iscsi_stats *)
763 ((char*)evstat + sizeof(*evstat));
764 memset(stats, 0, sizeof(*stats));
765
Mike Christie7b7232f2006-02-01 21:06:49 -0600766 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700767 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
768 sizeof(struct iscsi_stats) +
769 sizeof(struct iscsi_stats_custom) *
770 stats->custom_length);
771 actual_size -= sizeof(*nlhstat);
772 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600773 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700774 nlhstat->nlmsg_len = actual_size;
775
Mike Christie43a145a2006-10-16 18:09:38 -0400776 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700777 } while (err < 0 && err != -ECONNREFUSED);
778
779 return err;
780}
781
Mike Christie53cb8a12006-06-28 12:00:32 -0500782/**
Mike Christie26974782007-12-13 12:43:29 -0600783 * iscsi_session_event - send session destr. completion event
784 * @session: iscsi class session
785 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -0500786 */
Mike Christie26974782007-12-13 12:43:29 -0600787int iscsi_session_event(struct iscsi_cls_session *session,
788 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -0500789{
790 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -0500791 struct Scsi_Host *shost;
792 struct iscsi_uevent *ev;
793 struct sk_buff *skb;
794 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -0500795 int rc, len = NLMSG_SPACE(sizeof(*ev));
796
Mike Christie26974782007-12-13 12:43:29 -0600797 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -0500798 if (!priv)
799 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -0500800 shost = iscsi_session_to_shost(session);
801
Mike Christie43a145a2006-10-16 18:09:38 -0400802 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500803 if (!skb) {
Mike Christie26974782007-12-13 12:43:29 -0600804 dev_printk(KERN_ERR, &session->dev, "Cannot notify userspace "
805 "of session event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500806 return -ENOMEM;
807 }
808
809 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
810 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -0600811 ev->transport_handle = iscsi_handle(session->transport);
812
813 ev->type = event;
814 switch (event) {
815 case ISCSI_KEVENT_DESTROY_SESSION:
816 ev->r.d_session.host_no = shost->host_no;
817 ev->r.d_session.sid = session->sid;
818 break;
819 case ISCSI_KEVENT_CREATE_SESSION:
820 ev->r.c_session_ret.host_no = shost->host_no;
821 ev->r.c_session_ret.sid = session->sid;
822 break;
823 case ISCSI_KEVENT_UNBIND_SESSION:
824 ev->r.unbind_session.host_no = shost->host_no;
825 ev->r.unbind_session.sid = session->sid;
826 break;
827 default:
828 dev_printk(KERN_ERR, &session->dev, "Invalid event %u.\n",
829 event);
830 kfree_skb(skb);
831 return -EINVAL;
832 }
Mike Christie53cb8a12006-06-28 12:00:32 -0500833
834 /*
835 * this will occur if the daemon is not up, so we just warn
836 * the user and when the daemon is restarted it will handle it
837 */
Mike Christie43a145a2006-10-16 18:09:38 -0400838 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500839 if (rc < 0)
Mike Christie26974782007-12-13 12:43:29 -0600840 dev_printk(KERN_ERR, &session->dev, "Cannot notify userspace "
841 "of session event %u. Check iscsi daemon\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500842 return rc;
843}
Mike Christie26974782007-12-13 12:43:29 -0600844EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500845
Alex Aizman0896b752005-08-04 19:33:07 -0700846static int
Mike Christie7b8631b2006-01-13 18:05:50 -0600847iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
848{
849 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -0600850 struct iscsi_cls_session *session;
Mike Christieb5c7a122006-04-06 21:13:33 -0500851 uint32_t hostno;
Mike Christie7b8631b2006-01-13 18:05:50 -0600852
Mike Christie7996a772006-04-06 21:13:41 -0500853 session = transport->create_session(transport, &priv->t,
Mike Christie15482712007-05-30 12:57:19 -0500854 ev->u.c_session.cmds_max,
855 ev->u.c_session.queue_depth,
Mike Christie7b7232f2006-02-01 21:06:49 -0600856 ev->u.c_session.initial_cmdsn,
Mike Christieb5c7a122006-04-06 21:13:33 -0500857 &hostno);
Mike Christie7b7232f2006-02-01 21:06:49 -0600858 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600859 return -ENOMEM;
860
Mike Christieb5c7a122006-04-06 21:13:33 -0500861 ev->r.c_session_ret.host_no = hostno;
862 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600863 return 0;
864}
865
866static int
Mike Christie7b7232f2006-02-01 21:06:49 -0600867iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -0600868{
Mike Christie7b8631b2006-01-13 18:05:50 -0600869 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -0600870 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -0600871
Mike Christieb5c7a122006-04-06 21:13:33 -0500872 session = iscsi_session_lookup(ev->u.c_conn.sid);
873 if (!session) {
874 printk(KERN_ERR "iscsi: invalid session %d\n",
875 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600876 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -0500877 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600878
Mike Christie7b7232f2006-02-01 21:06:49 -0600879 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -0500880 if (!conn) {
881 printk(KERN_ERR "iscsi: couldn't create a new "
882 "connection for session %d\n",
883 session->sid);
Mike Christie7b7232f2006-02-01 21:06:49 -0600884 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -0500885 }
Mike Christie7b8631b2006-01-13 18:05:50 -0600886
Mike Christieb5c7a122006-04-06 21:13:33 -0500887 ev->r.c_conn_ret.sid = session->sid;
888 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600889 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -0600890}
891
892static int
893iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
894{
Mike Christie7b8631b2006-01-13 18:05:50 -0600895 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -0600896
Mike Christieb5c7a122006-04-06 21:13:33 -0500897 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600898 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -0600899 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -0600900
Mike Christie7b8631b2006-01-13 18:05:50 -0600901 if (transport->destroy_conn)
902 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -0600903 return 0;
904}
905
Mike Christiefd7255f2006-04-06 21:13:36 -0500906static int
907iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
908{
909 char *data = (char*)ev + sizeof(*ev);
910 struct iscsi_cls_conn *conn;
911 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -0500912 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -0500913
914 session = iscsi_session_lookup(ev->u.set_param.sid);
915 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
916 if (!conn || !session)
917 return -EINVAL;
918
919 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -0500920 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -0500921 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -0500922 if (value != 0)
923 session->recovery_tmo = value;
924 break;
Mike Christiefd7255f2006-04-06 21:13:36 -0500925 default:
Mike Christiea54a52c2006-06-28 12:00:23 -0500926 err = transport->set_param(conn, ev->u.set_param.param,
927 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -0500928 }
929
930 return err;
931}
932
Mike Christie7b8631b2006-01-13 18:05:50 -0600933static int
Or Gerlitz264faaa2006-05-02 19:46:36 -0500934iscsi_if_transport_ep(struct iscsi_transport *transport,
935 struct iscsi_uevent *ev, int msg_type)
936{
937 struct sockaddr *dst_addr;
938 int rc = 0;
939
940 switch (msg_type) {
941 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
942 if (!transport->ep_connect)
943 return -EINVAL;
944
945 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
946 rc = transport->ep_connect(dst_addr,
947 ev->u.ep_connect.non_blocking,
948 &ev->r.ep_connect_ret.handle);
949 break;
950 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
951 if (!transport->ep_poll)
952 return -EINVAL;
953
954 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
955 ev->u.ep_poll.timeout_ms);
956 break;
957 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
958 if (!transport->ep_disconnect)
959 return -EINVAL;
960
961 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
962 break;
963 }
964 return rc;
965}
966
967static int
Mike Christie01cb2252006-06-28 12:00:22 -0500968iscsi_tgt_dscvr(struct iscsi_transport *transport,
969 struct iscsi_uevent *ev)
970{
Mike Christie2174a042007-05-30 12:57:10 -0500971 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -0500972 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -0500973 int err;
Mike Christie01cb2252006-06-28 12:00:22 -0500974
975 if (!transport->tgt_dscvr)
976 return -EINVAL;
977
Mike Christie2174a042007-05-30 12:57:10 -0500978 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
979 if (IS_ERR(shost)) {
980 printk(KERN_ERR "target discovery could not find host no %u\n",
981 ev->u.tgt_dscvr.host_no);
982 return -ENODEV;
983 }
984
985
Mike Christie01cb2252006-06-28 12:00:22 -0500986 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -0500987 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
988 ev->u.tgt_dscvr.enable, dst_addr);
989 scsi_host_put(shost);
990 return err;
Mike Christie01cb2252006-06-28 12:00:22 -0500991}
992
993static int
Mike Christie1d9bf132007-05-30 12:57:11 -0500994iscsi_set_host_param(struct iscsi_transport *transport,
995 struct iscsi_uevent *ev)
996{
997 char *data = (char*)ev + sizeof(*ev);
998 struct Scsi_Host *shost;
999 int err;
1000
1001 if (!transport->set_host_param)
1002 return -ENOSYS;
1003
1004 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1005 if (IS_ERR(shost)) {
1006 printk(KERN_ERR "set_host_param could not find host no %u\n",
1007 ev->u.set_host_param.host_no);
1008 return -ENODEV;
1009 }
1010
1011 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1012 data, ev->u.set_host_param.len);
1013 scsi_host_put(shost);
1014 return err;
1015}
1016
1017static int
Alex Aizman0896b752005-08-04 19:33:07 -07001018iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1019{
1020 int err = 0;
1021 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1022 struct iscsi_transport *transport = NULL;
1023 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001024 struct iscsi_cls_session *session;
1025 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001026
Alex Aizman0896b752005-08-04 19:33:07 -07001027 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1028 if (!priv)
1029 return -EINVAL;
1030 transport = priv->iscsi_transport;
1031
Mike Christie7b7232f2006-02-01 21:06:49 -06001032 if (!try_module_get(transport->owner))
1033 return -EINVAL;
1034
Mike Christie790f39a2006-05-18 20:31:39 -05001035 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1036
Alex Aizman0896b752005-08-04 19:33:07 -07001037 switch (nlh->nlmsg_type) {
1038 case ISCSI_UEVENT_CREATE_SESSION:
1039 err = iscsi_if_create_session(priv, ev);
1040 break;
1041 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001042 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001043 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001044 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001045 else
1046 err = -EINVAL;
1047 break;
1048 case ISCSI_UEVENT_UNBIND_SESSION:
1049 session = iscsi_session_lookup(ev->u.d_session.sid);
1050 if (session)
1051 iscsi_unbind_session(session);
1052 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001053 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001054 break;
1055 case ISCSI_UEVENT_CREATE_CONN:
1056 err = iscsi_if_create_conn(transport, ev);
1057 break;
1058 case ISCSI_UEVENT_DESTROY_CONN:
1059 err = iscsi_if_destroy_conn(transport, ev);
1060 break;
1061 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001062 session = iscsi_session_lookup(ev->u.b_conn.sid);
1063 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001064
1065 if (session && conn)
1066 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001067 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001068 ev->u.b_conn.is_leading);
1069 else
1070 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001071 break;
1072 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001073 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001074 break;
1075 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001076 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001077 if (conn)
1078 ev->r.retcode = transport->start_conn(conn);
1079 else
1080 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001081 break;
1082 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001083 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001084 if (conn)
1085 transport->stop_conn(conn, ev->u.stop_conn.flag);
1086 else
1087 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001088 break;
1089 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001090 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001091 if (conn)
1092 ev->r.retcode = transport->send_pdu(conn,
1093 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1094 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1095 ev->u.send_pdu.data_size);
1096 else
1097 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001098 break;
1099 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001100 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001101 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001102 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1103 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1104 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1105 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1106 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001107 case ISCSI_UEVENT_TGT_DSCVR:
1108 err = iscsi_tgt_dscvr(transport, ev);
1109 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001110 case ISCSI_UEVENT_SET_HOST_PARAM:
1111 err = iscsi_set_host_param(transport, ev);
1112 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001113 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001114 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001115 break;
1116 }
1117
Mike Christie7b7232f2006-02-01 21:06:49 -06001118 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001119 return err;
1120}
1121
Mike Christieb5c7a122006-04-06 21:13:33 -05001122/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001123 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1124 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001125 */
Alex Aizman0896b752005-08-04 19:33:07 -07001126static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001127iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001128{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001129 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001130 while (skb->len >= NLMSG_SPACE(0)) {
1131 int err;
1132 uint32_t rlen;
1133 struct nlmsghdr *nlh;
1134 struct iscsi_uevent *ev;
1135
1136 nlh = nlmsg_hdr(skb);
1137 if (nlh->nlmsg_len < sizeof(*nlh) ||
1138 skb->len < nlh->nlmsg_len) {
1139 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001140 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001141
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001142 ev = NLMSG_DATA(nlh);
1143 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1144 if (rlen > skb->len)
1145 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001146
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001147 err = iscsi_if_recv_msg(skb, nlh);
1148 if (err) {
1149 ev->type = ISCSI_KEVENT_IF_ERROR;
1150 ev->iferror = err;
1151 }
1152 do {
1153 /*
1154 * special case for GET_STATS:
1155 * on success - sending reply and stats from
1156 * inside of if_recv_msg(),
1157 * on error - fall through.
1158 */
1159 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001160 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001161 err = iscsi_if_send_reply(
1162 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1163 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1164 } while (err < 0 && err != -ECONNREFUSED);
1165 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001166 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001167 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001168}
1169
Mike Christie7b8631b2006-01-13 18:05:50 -06001170#define iscsi_cdev_to_conn(_cdev) \
1171 iscsi_dev_to_conn(_cdev->dev)
1172
Mike Christiefd7255f2006-04-06 21:13:36 -05001173#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
1174struct class_device_attribute class_device_attr_##_prefix##_##_name = \
1175 __ATTR(_name,_mode,_show,_store)
1176
Alex Aizman0896b752005-08-04 19:33:07 -07001177/*
1178 * iSCSI connection attrs
1179 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001180#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001182show_conn_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001183{ \
1184 struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \
1185 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001186 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001187}
1188
Mike Christiea54a52c2006-06-28 12:00:23 -05001189#define iscsi_conn_attr(field, param) \
1190 iscsi_conn_attr_show(param) \
1191static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001192 NULL);
1193
Mike Christiea54a52c2006-06-28 12:00:23 -05001194iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1195iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1196iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1197iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1198iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1199iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1200iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1201iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1202iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1203iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1204iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001205iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1206iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Mike Christie7b8631b2006-01-13 18:05:50 -06001208#define iscsi_cdev_to_session(_cdev) \
1209 iscsi_dev_to_session(_cdev->dev)
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/*
Alex Aizman0896b752005-08-04 19:33:07 -07001212 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Mike Christieb2c64162007-05-30 12:57:16 -05001214#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001216show_session_param_##param(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001217{ \
1218 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \
1219 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001220 \
1221 if (perm && !capable(CAP_SYS_ADMIN)) \
1222 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001223 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001224}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Mike Christieb2c64162007-05-30 12:57:16 -05001226#define iscsi_session_attr(field, param, perm) \
1227 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001228static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001229 NULL);
1230
Mike Christieb2c64162007-05-30 12:57:16 -05001231iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1232iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1233iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1234iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1235iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1236iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1237iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1238iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1239iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1240iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1241iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1242iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1243iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1244iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001245iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1246iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1247iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001248
Mike Christiefd7255f2006-04-06 21:13:36 -05001249#define iscsi_priv_session_attr_show(field, format) \
1250static ssize_t \
Mike Christiea54a52c2006-06-28 12:00:23 -05001251show_priv_session_##field(struct class_device *cdev, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001252{ \
Mike Christiea54a52c2006-06-28 12:00:23 -05001253 struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev);\
Mike Christiefd7255f2006-04-06 21:13:36 -05001254 return sprintf(buf, format"\n", session->field); \
1255}
1256
1257#define iscsi_priv_session_attr(field, format) \
1258 iscsi_priv_session_attr_show(field, format) \
1259static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1260 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001261iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001262
Mike Christie1819dc82007-05-30 12:57:08 -05001263/*
1264 * iSCSI host attrs
1265 */
1266#define iscsi_host_attr_show(param) \
1267static ssize_t \
1268show_host_param_##param(struct class_device *cdev, char *buf) \
1269{ \
1270 struct Scsi_Host *shost = transport_class_to_shost(cdev); \
1271 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1272 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1273}
1274
1275#define iscsi_host_attr(field, param) \
1276 iscsi_host_attr_show(param) \
1277static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1278 NULL);
1279
Mike Christied8196ed2007-05-30 12:57:25 -05001280iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001281iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001282iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001283iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001284
Mike Christiefd7255f2006-04-06 21:13:36 -05001285#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1286do { \
1287 priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \
1288 count++; \
1289} while (0)
1290
Mike Christiea54a52c2006-06-28 12:00:23 -05001291
Mike Christiefd7255f2006-04-06 21:13:36 -05001292#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1293do { \
1294 if (tt->param_mask & param_flag) { \
1295 priv->session_attrs[count] = &class_device_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001297 } \
1298} while (0)
1299
Mike Christiefd7255f2006-04-06 21:13:36 -05001300#define SETUP_CONN_RD_ATTR(field, param_flag) \
1301do { \
1302 if (tt->param_mask & param_flag) { \
1303 priv->conn_attrs[count] = &class_device_attr_conn_##field; \
1304 count++; \
1305 } \
1306} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Mike Christie1819dc82007-05-30 12:57:08 -05001308#define SETUP_HOST_RD_ATTR(field, param_flag) \
1309do { \
1310 if (tt->host_param_mask & param_flag) { \
1311 priv->host_attrs[count] = &class_device_attr_host_##field; \
1312 count++; \
1313 } \
1314} while (0)
1315
Alex Aizman0896b752005-08-04 19:33:07 -07001316static int iscsi_session_match(struct attribute_container *cont,
1317 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Mike Christie7b8631b2006-01-13 18:05:50 -06001319 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001321 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Alex Aizman0896b752005-08-04 19:33:07 -07001323 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325
Mike Christie7b8631b2006-01-13 18:05:50 -06001326 session = iscsi_dev_to_session(dev);
1327 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001328 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330
Alex Aizman0896b752005-08-04 19:33:07 -07001331 priv = to_iscsi_internal(shost->transportt);
1332 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1333 return 0;
1334
1335 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Alex Aizman0896b752005-08-04 19:33:07 -07001338static int iscsi_conn_match(struct attribute_container *cont,
1339 struct device *dev)
1340{
Mike Christie7b8631b2006-01-13 18:05:50 -06001341 struct iscsi_cls_session *session;
1342 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001343 struct Scsi_Host *shost;
1344 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Alex Aizman0896b752005-08-04 19:33:07 -07001346 if (!iscsi_is_conn_dev(dev))
1347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Mike Christie7b8631b2006-01-13 18:05:50 -06001349 conn = iscsi_dev_to_conn(dev);
1350 session = iscsi_dev_to_session(conn->dev.parent);
1351 shost = iscsi_session_to_shost(session);
1352
Alex Aizman0896b752005-08-04 19:33:07 -07001353 if (!shost->transportt)
1354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Alex Aizman0896b752005-08-04 19:33:07 -07001356 priv = to_iscsi_internal(shost->transportt);
1357 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1358 return 0;
1359
1360 return &priv->conn_cont.ac == cont;
1361}
1362
Mike Christie30a6c652006-04-06 21:13:39 -05001363static int iscsi_host_match(struct attribute_container *cont,
1364 struct device *dev)
1365{
1366 struct Scsi_Host *shost;
1367 struct iscsi_internal *priv;
1368
1369 if (!scsi_is_host_device(dev))
1370 return 0;
1371
1372 shost = dev_to_shost(dev);
1373 if (!shost->transportt ||
1374 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1375 return 0;
1376
1377 priv = to_iscsi_internal(shost->transportt);
1378 return &priv->t.host_attrs.ac == cont;
1379}
1380
Mike Christie7b8631b2006-01-13 18:05:50 -06001381struct scsi_transport_template *
1382iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001383{
1384 struct iscsi_internal *priv;
1385 unsigned long flags;
1386 int count = 0, err;
1387
1388 BUG_ON(!tt);
1389
1390 priv = iscsi_if_transport_lookup(tt);
1391 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001392 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001393
Jes Sorensen24669f752006-01-16 10:31:18 -05001394 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001395 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001396 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001397 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001398 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001399 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001400 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001401
1402 priv->cdev.class = &iscsi_transport_class;
1403 snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name);
1404 err = class_device_register(&priv->cdev);
1405 if (err)
1406 goto free_priv;
1407
1408 err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group);
1409 if (err)
1410 goto unregister_cdev;
1411
Mike Christie30a6c652006-04-06 21:13:39 -05001412 /* host parameters */
1413 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1414 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1415 priv->t.host_attrs.ac.match = iscsi_host_match;
1416 priv->t.host_size = sizeof(struct iscsi_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001417 transport_container_register(&priv->t.host_attrs);
1418
Mike Christied8196ed2007-05-30 12:57:25 -05001419 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001420 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001421 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001422 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001423 BUG_ON(count > ISCSI_HOST_ATTRS);
1424 priv->host_attrs[count] = NULL;
1425 count = 0;
1426
Alex Aizman0896b752005-08-04 19:33:07 -07001427 /* connection parameters */
1428 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1429 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1430 priv->conn_cont.ac.match = iscsi_conn_match;
1431 transport_container_register(&priv->conn_cont);
1432
Mike Christiefd7255f2006-04-06 21:13:36 -05001433 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1434 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1435 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1436 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1437 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1438 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1439 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1440 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001441 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001442 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1443 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001444 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1445 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001446
1447 BUG_ON(count > ISCSI_CONN_ATTRS);
1448 priv->conn_attrs[count] = NULL;
1449 count = 0;
1450
1451 /* session parameters */
1452 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1453 priv->session_cont.ac.class = &iscsi_session_class.class;
1454 priv->session_cont.ac.match = iscsi_session_match;
1455 transport_container_register(&priv->session_cont);
1456
Mike Christiefd7255f2006-04-06 21:13:36 -05001457 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1458 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1459 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1460 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1461 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1462 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1463 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1464 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001465 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1466 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001467 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1468 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1469 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1470 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001471 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001472 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1473 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie30a6c652006-04-06 21:13:39 -05001474 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christiefd7255f2006-04-06 21:13:36 -05001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001477 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alex Aizman0896b752005-08-04 19:33:07 -07001479 spin_lock_irqsave(&iscsi_transport_lock, flags);
1480 list_add(&priv->list, &iscsi_transports);
1481 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Alex Aizman0896b752005-08-04 19:33:07 -07001483 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001484 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alex Aizman0896b752005-08-04 19:33:07 -07001486unregister_cdev:
1487 class_device_unregister(&priv->cdev);
1488free_priv:
1489 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001490 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
Alex Aizman0896b752005-08-04 19:33:07 -07001492EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alex Aizman0896b752005-08-04 19:33:07 -07001494int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Alex Aizman0896b752005-08-04 19:33:07 -07001496 struct iscsi_internal *priv;
1497 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Alex Aizman0896b752005-08-04 19:33:07 -07001499 BUG_ON(!tt);
1500
Arjan van de Ven0b950672006-01-11 13:16:10 +01001501 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001502
1503 priv = iscsi_if_transport_lookup(tt);
1504 BUG_ON (!priv);
1505
Alex Aizman0896b752005-08-04 19:33:07 -07001506 spin_lock_irqsave(&iscsi_transport_lock, flags);
1507 list_del(&priv->list);
1508 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1509
1510 transport_container_unregister(&priv->conn_cont);
1511 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001512 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001513
1514 sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group);
1515 class_device_unregister(&priv->cdev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001516 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001517
1518 return 0;
1519}
1520EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static __init int iscsi_transport_init(void)
1523{
Alex Aizman0896b752005-08-04 19:33:07 -07001524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Meelis Roos09492602006-12-17 12:10:26 -06001526 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001527 ISCSI_TRANSPORT_VERSION);
1528
Mike Christie41be1442007-02-28 17:32:18 -06001529 atomic_set(&iscsi_session_nr, 0);
1530
Alex Aizman0896b752005-08-04 19:33:07 -07001531 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (err)
1533 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001534
Mike Christie30a6c652006-04-06 21:13:39 -05001535 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001536 if (err)
1537 goto unregister_transport_class;
1538
Mike Christie30a6c652006-04-06 21:13:39 -05001539 err = transport_class_register(&iscsi_connection_class);
1540 if (err)
1541 goto unregister_host_class;
1542
Alex Aizman0896b752005-08-04 19:33:07 -07001543 err = transport_class_register(&iscsi_session_class);
1544 if (err)
1545 goto unregister_conn_class;
1546
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001547 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001548 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001549 if (!nls) {
1550 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001551 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001552 }
1553
Mike Christied8bf5412007-12-13 12:43:27 -06001554 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1555 if (!iscsi_eh_timer_workq)
1556 goto release_nls;
1557
Mike Christie43a145a2006-10-16 18:09:38 -04001558 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001559
Mike Christied8bf5412007-12-13 12:43:27 -06001560release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001561 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001562unregister_session_class:
1563 transport_class_unregister(&iscsi_session_class);
1564unregister_conn_class:
1565 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001566unregister_host_class:
1567 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001568unregister_transport_class:
1569 class_unregister(&iscsi_transport_class);
1570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static void __exit iscsi_transport_exit(void)
1574{
Mike Christied8bf5412007-12-13 12:43:27 -06001575 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001576 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001577 transport_class_unregister(&iscsi_connection_class);
1578 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001579 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001580 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583module_init(iscsi_transport_init);
1584module_exit(iscsi_transport_exit);
1585
Alex Aizman0896b752005-08-04 19:33:07 -07001586MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1587 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1588 "Alex Aizman <itn780@yahoo.com>");
1589MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001591MODULE_VERSION(ISCSI_TRANSPORT_VERSION);