blob: 5577a60bec4e02df60656e53eaf788d1fdd43e71 [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 Christiec238c3b2008-01-31 13:36:51 -060034#define ISCSI_CONN_ATTRS 13
Mike Christied8196ed2007-05-30 12:57:25 -050035#define ISCSI_HOST_ATTRS 4
Mike Christie31ed0bf2008-02-26 12:35:23 -060036#define ISCSI_TRANSPORT_VERSION "2.0-869"
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;
Tony Jonesee959b02008-02-22 00:13:36 +010043 struct device dev;
Mike Christie30a6c652006-04-06 21:13:39 -050044
Tony Jonesee959b02008-02-22 00:13:36 +010045 struct device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070046 struct transport_container conn_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010047 struct device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1];
Alex Aizman0896b752005-08-04 19:33:07 -070048 struct transport_container session_cont;
Tony Jonesee959b02008-02-22 00:13:36 +010049 struct 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
Tony Jonesee959b02008-02-22 00:13:36 +010066#define dev_to_iscsi_internal(_dev) \
67 container_of(_dev, struct iscsi_internal, dev)
Alex Aizman0896b752005-08-04 19:33:07 -070068
Tony Jonesee959b02008-02-22 00:13:36 +010069static void iscsi_transport_release(struct device *dev)
Alex Aizman0896b752005-08-04 19:33:07 -070070{
Tony Jonesee959b02008-02-22 00:13:36 +010071 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Alex Aizman0896b752005-08-04 19:33:07 -070072 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",
Tony Jonesee959b02008-02-22 00:13:36 +010081 .dev_release = iscsi_transport_release,
Alex Aizman0896b752005-08-04 19:33:07 -070082};
83
84static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +010085show_transport_handle(struct device *dev, struct device_attribute *attr,
86 char *buf)
Alex Aizman0896b752005-08-04 19:33:07 -070087{
Tony Jonesee959b02008-02-22 00:13:36 +010088 struct iscsi_internal *priv = dev_to_iscsi_internal(dev);
Mike Christie762e2bf2005-09-12 21:01:46 -050089 return sprintf(buf, "%llu\n", (unsigned long long)iscsi_handle(priv->iscsi_transport));
Alex Aizman0896b752005-08-04 19:33:07 -070090}
Tony Jonesee959b02008-02-22 00:13:36 +010091static DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -070092
93#define show_transport_attr(name, format) \
94static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +010095show_transport_##name(struct device *dev, \
96 struct device_attribute *attr,char *buf) \
Alex Aizman0896b752005-08-04 19:33:07 -070097{ \
Tony Jonesee959b02008-02-22 00:13:36 +010098 struct iscsi_internal *priv = dev_to_iscsi_internal(dev); \
Alex Aizman0896b752005-08-04 19:33:07 -070099 return sprintf(buf, format"\n", priv->iscsi_transport->name); \
100} \
Tony Jonesee959b02008-02-22 00:13:36 +0100101static DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700102
103show_transport_attr(caps, "0x%x");
104show_transport_attr(max_lun, "%d");
Alex Aizman0896b752005-08-04 19:33:07 -0700105
106static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100107 &dev_attr_handle.attr,
108 &dev_attr_caps.attr,
109 &dev_attr_max_lun.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700110 NULL,
111};
112
113static struct attribute_group iscsi_transport_group = {
114 .attrs = iscsi_transport_attrs,
115};
116
Mike Christie30a6c652006-04-06 21:13:39 -0500117static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100118 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500119{
120 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500121 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500122
123 memset(ihost, 0, sizeof(*ihost));
124 INIT_LIST_HEAD(&ihost->sessions);
125 mutex_init(&ihost->mutex);
Mike Christie8aae18a2008-01-31 13:36:48 -0600126 atomic_set(&ihost->nr_scans, 0);
Mike Christie26974782007-12-13 12:43:29 -0600127
Mike Christiebd976f62008-01-31 13:36:46 -0600128 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
Mike Christie26974782007-12-13 12:43:29 -0600129 shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600130 ihost->scan_workq = create_singlethread_workqueue(
131 ihost->scan_workq_name);
132 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600133 return -ENOMEM;
134 return 0;
135}
136
137static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100138 struct device *cdev)
Mike Christie26974782007-12-13 12:43:29 -0600139{
140 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500141 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600142
Mike Christiebd976f62008-01-31 13:36:46 -0600143 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500144 return 0;
145}
146
147static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
148 "iscsi_host",
149 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600150 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500151 NULL);
152
Alex Aizman0896b752005-08-04 19:33:07 -0700153static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
154 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 NULL,
156 NULL,
157 NULL);
158
Alex Aizman0896b752005-08-04 19:33:07 -0700159static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
160 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 NULL,
162 NULL,
163 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700164
165static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100166static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700167
Mike Christie7b7232f2006-02-01 21:06:49 -0600168static LIST_HEAD(sesslist);
169static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700170static LIST_HEAD(connlist);
171static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Mike Christieb5c7a122006-04-06 21:13:33 -0500173static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
174{
175 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
176 return sess->sid;
177}
178
179/*
180 * Returns the matching session to a given sid
181 */
182static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600183{
184 unsigned long flags;
185 struct iscsi_cls_session *sess;
186
187 spin_lock_irqsave(&sesslock, flags);
188 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500189 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600190 spin_unlock_irqrestore(&sesslock, flags);
191 return sess;
192 }
193 }
194 spin_unlock_irqrestore(&sesslock, flags);
195 return NULL;
196}
197
Mike Christieb5c7a122006-04-06 21:13:33 -0500198/*
199 * Returns the matching connection to a given sid / cid tuple
200 */
201static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600202{
203 unsigned long flags;
204 struct iscsi_cls_conn *conn;
205
206 spin_lock_irqsave(&connlock, flags);
207 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500208 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600209 spin_unlock_irqrestore(&connlock, flags);
210 return conn;
211 }
212 }
213 spin_unlock_irqrestore(&connlock, flags);
214 return NULL;
215}
216
Mike Christie7b8631b2006-01-13 18:05:50 -0600217/*
218 * The following functions can be used by LLDs that allocate
219 * their own scsi_hosts or by software iscsi LLDs
220 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600221static struct {
222 int value;
223 char *name;
224} iscsi_session_state_names[] = {
225 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
226 { ISCSI_SESSION_FAILED, "FAILED" },
227 { ISCSI_SESSION_FREE, "FREE" },
228};
229
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200230static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600231{
232 int i;
233 char *name = NULL;
234
235 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
236 if (iscsi_session_state_names[i].value == state) {
237 name = iscsi_session_state_names[i].name;
238 break;
239 }
240 }
241 return name;
242}
243
244int iscsi_session_chkready(struct iscsi_cls_session *session)
245{
246 unsigned long flags;
247 int err;
248
249 spin_lock_irqsave(&session->lock, flags);
250 switch (session->state) {
251 case ISCSI_SESSION_LOGGED_IN:
252 err = 0;
253 break;
254 case ISCSI_SESSION_FAILED:
255 err = DID_IMM_RETRY << 16;
256 break;
257 case ISCSI_SESSION_FREE:
258 err = DID_NO_CONNECT << 16;
259 break;
260 default:
261 err = DID_NO_CONNECT << 16;
262 break;
263 }
264 spin_unlock_irqrestore(&session->lock, flags);
265 return err;
266}
267EXPORT_SYMBOL_GPL(iscsi_session_chkready);
268
Mike Christie7b8631b2006-01-13 18:05:50 -0600269static void iscsi_session_release(struct device *dev)
270{
271 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600272 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Mike Christie7b8631b2006-01-13 18:05:50 -0600274 shost = iscsi_session_to_shost(session);
275 scsi_host_put(shost);
276 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Mike Christie7b8631b2006-01-13 18:05:50 -0600279static int iscsi_is_session_dev(const struct device *dev)
280{
281 return dev->release == iscsi_session_release;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Mike Christie8aae18a2008-01-31 13:36:48 -0600284/**
285 * iscsi_scan_finished - helper to report when running scans are done
286 * @shost: scsi host
287 * @time: scan run time
288 *
289 * This function can be used by drives like qla4xxx to report to the scsi
290 * layer when the scans it kicked off at module load time are done.
291 */
292int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
293{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500294 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600295 /*
296 * qla4xxx will have kicked off some session unblocks before calling
297 * scsi_scan_host, so just wait for them to complete.
298 */
299 return !atomic_read(&ihost->nr_scans);
300}
301EXPORT_SYMBOL_GPL(iscsi_scan_finished);
302
Mike Christie30a6c652006-04-06 21:13:39 -0500303static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
304 uint id, uint lun)
305{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500306 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500307 struct iscsi_cls_session *session;
308
309 mutex_lock(&ihost->mutex);
310 list_for_each_entry(session, &ihost->sessions, host_list) {
Mike Christiee6f3b632006-06-28 12:00:29 -0500311 if ((channel == SCAN_WILD_CARD || channel == 0) &&
Mike Christie30a6c652006-04-06 21:13:39 -0500312 (id == SCAN_WILD_CARD || id == session->target_id))
Mike Christiee6f3b632006-06-28 12:00:29 -0500313 scsi_scan_target(&session->dev, 0,
Mike Christie30a6c652006-04-06 21:13:39 -0500314 session->target_id, lun, 1);
315 }
316 mutex_unlock(&ihost->mutex);
317
318 return 0;
319}
320
Mike Christiebd976f62008-01-31 13:36:46 -0600321static void iscsi_scan_session(struct work_struct *work)
322{
323 struct iscsi_cls_session *session =
324 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600325 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500326 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600327 unsigned long flags;
328
329 spin_lock_irqsave(&session->lock, flags);
330 if (session->state != ISCSI_SESSION_LOGGED_IN) {
331 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie8aae18a2008-01-31 13:36:48 -0600332 goto done;
Mike Christiebd976f62008-01-31 13:36:46 -0600333 }
334 spin_unlock_irqrestore(&session->lock, flags);
335
336 scsi_scan_target(&session->dev, 0, session->target_id,
337 SCAN_WILD_CARD, 1);
Mike Christie8aae18a2008-01-31 13:36:48 -0600338done:
339 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600340}
341
David Howellsc4028952006-11-22 14:57:56 +0000342static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500343{
David Howellsc4028952006-11-22 14:57:56 +0000344 struct iscsi_cls_session *session =
345 container_of(work, struct iscsi_cls_session,
346 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600347 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500348
Mike Christie322d7392008-01-31 13:36:52 -0600349 iscsi_cls_session_printk(KERN_INFO, session,
350 "session recovery timed out after %d secs\n",
351 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500352
Mike Christie6eabafb2008-01-31 13:36:43 -0600353 spin_lock_irqsave(&session->lock, flags);
354 switch (session->state) {
355 case ISCSI_SESSION_FAILED:
356 session->state = ISCSI_SESSION_FREE;
357 break;
358 case ISCSI_SESSION_LOGGED_IN:
359 case ISCSI_SESSION_FREE:
360 /* we raced with the unblock's flush */
361 spin_unlock_irqrestore(&session->lock, flags);
362 return;
363 }
364 spin_unlock_irqrestore(&session->lock, flags);
365
Mike Christie30a6c652006-04-06 21:13:39 -0500366 if (session->transport->session_recovery_timedout)
367 session->transport->session_recovery_timedout(session);
368
369 scsi_target_unblock(&session->dev);
370}
371
Mike Christie45ab33b2008-03-04 13:26:55 -0600372static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500373{
Mike Christie45ab33b2008-03-04 13:26:55 -0600374 struct iscsi_cls_session *session =
375 container_of(work, struct iscsi_cls_session,
376 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600377 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500378 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600379 unsigned long flags;
380
Mike Christie45ab33b2008-03-04 13:26:55 -0600381 /*
382 * The recovery and unblock work get run from the same workqueue,
383 * so try to cancel it if it was going to run after this unblock.
384 */
385 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600386 spin_lock_irqsave(&session->lock, flags);
387 session->state = ISCSI_SESSION_LOGGED_IN;
388 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600389 /* start IO */
390 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600391 /*
392 * Only do kernel scanning if the driver is properly hooked into
393 * the async scanning code (drivers like iscsi_tcp do login and
394 * scanning from userspace).
395 */
396 if (shost->hostt->scan_finished) {
397 if (queue_work(ihost->scan_workq, &session->scan_work))
398 atomic_inc(&ihost->nr_scans);
399 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600400}
Mike Christie45ab33b2008-03-04 13:26:55 -0600401
402/**
403 * iscsi_unblock_session - set a session as logged in and start IO.
404 * @session: iscsi session
405 *
406 * Mark a session as ready to accept IO.
407 */
408void iscsi_unblock_session(struct iscsi_cls_session *session)
409{
410 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
411 /*
412 * make sure all the events have completed before tell the driver
413 * it is safe
414 */
415 flush_workqueue(iscsi_eh_timer_workq);
416}
Mike Christie30a6c652006-04-06 21:13:39 -0500417EXPORT_SYMBOL_GPL(iscsi_unblock_session);
418
Mike Christie45ab33b2008-03-04 13:26:55 -0600419static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500420{
Mike Christie45ab33b2008-03-04 13:26:55 -0600421 struct iscsi_cls_session *session =
422 container_of(work, struct iscsi_cls_session,
423 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600424 unsigned long flags;
425
426 spin_lock_irqsave(&session->lock, flags);
427 session->state = ISCSI_SESSION_FAILED;
428 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500429 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600430 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
431 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500432}
Mike Christie45ab33b2008-03-04 13:26:55 -0600433
434void iscsi_block_session(struct iscsi_cls_session *session)
435{
436 queue_work(iscsi_eh_timer_workq, &session->block_work);
437}
Mike Christie30a6c652006-04-06 21:13:39 -0500438EXPORT_SYMBOL_GPL(iscsi_block_session);
439
Mike Christie26974782007-12-13 12:43:29 -0600440static void __iscsi_unbind_session(struct work_struct *work)
441{
442 struct iscsi_cls_session *session =
443 container_of(work, struct iscsi_cls_session,
444 unbind_work);
445 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500446 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600447
448 /* Prevent new scans and make sure scanning is not in progress */
449 mutex_lock(&ihost->mutex);
450 if (list_empty(&session->host_list)) {
451 mutex_unlock(&ihost->mutex);
452 return;
453 }
454 list_del_init(&session->host_list);
455 mutex_unlock(&ihost->mutex);
456
457 scsi_remove_target(&session->dev);
458 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
459}
460
461static int iscsi_unbind_session(struct iscsi_cls_session *session)
462{
463 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500464 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600465
Mike Christiebd976f62008-01-31 13:36:46 -0600466 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600467}
468
Mike Christie7b8631b2006-01-13 18:05:50 -0600469struct iscsi_cls_session *
Mike Christie8434aa82006-06-28 12:00:30 -0500470iscsi_alloc_session(struct Scsi_Host *shost,
471 struct iscsi_transport *transport)
Mike Christie7b8631b2006-01-13 18:05:50 -0600472{
473 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Mike Christieb5c7a122006-04-06 21:13:33 -0500475 session = kzalloc(sizeof(*session) + transport->sessiondata_size,
476 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600477 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500478 return NULL;
479
Mike Christie7b8631b2006-01-13 18:05:50 -0600480 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500481 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600482 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000483 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500484 INIT_LIST_HEAD(&session->host_list);
485 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600486 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
487 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600488 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600489 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600490 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600491
Mike Christie6a8a0d32006-06-28 12:00:31 -0500492 /* this is released in the dev's release function */
493 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500494 session->dev.parent = &shost->shost_gendev;
495 session->dev.release = iscsi_session_release;
496 device_initialize(&session->dev);
Mike Christieb5c7a122006-04-06 21:13:33 -0500497 if (transport->sessiondata_size)
498 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500499 return session;
500}
501EXPORT_SYMBOL_GPL(iscsi_alloc_session);
502
Mike Christie6a8a0d32006-06-28 12:00:31 -0500503int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500504{
505 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500506 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600507 unsigned long flags;
Mike Christie8434aa82006-06-28 12:00:30 -0500508 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500509
Mike Christie30a6c652006-04-06 21:13:39 -0500510 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600511 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie6a8a0d32006-06-28 12:00:31 -0500512 session->target_id = target_id;
Mike Christie30a6c652006-04-06 21:13:39 -0500513
Mike Christieb5c7a122006-04-06 21:13:33 -0500514 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
515 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500516 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600517 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600518 iscsi_cls_session_printk(KERN_ERR, session,
519 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500520 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600521 }
522 transport_register_device(&session->dev);
523
Mike Christie26974782007-12-13 12:43:29 -0600524 spin_lock_irqsave(&sesslock, flags);
525 list_add(&session->sess_list, &sesslist);
526 spin_unlock_irqrestore(&sesslock, flags);
527
Mike Christie30a6c652006-04-06 21:13:39 -0500528 mutex_lock(&ihost->mutex);
529 list_add(&session->host_list, &ihost->sessions);
530 mutex_unlock(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600531
532 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500533 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500534
Mike Christie8434aa82006-06-28 12:00:30 -0500535release_host:
536 scsi_host_put(shost);
537 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600538}
Mike Christie8434aa82006-06-28 12:00:30 -0500539EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600540
541/**
Mike Christie8434aa82006-06-28 12:00:30 -0500542 * iscsi_create_session - create iscsi class session
543 * @shost: scsi host
544 * @transport: iscsi transport
Rob Landleyeb448202007-11-03 13:30:39 -0500545 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600546 *
Mike Christie8434aa82006-06-28 12:00:30 -0500547 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500548 */
Mike Christie8434aa82006-06-28 12:00:30 -0500549struct iscsi_cls_session *
550iscsi_create_session(struct Scsi_Host *shost,
Mike Christie6a8a0d32006-06-28 12:00:31 -0500551 struct iscsi_transport *transport,
552 unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500553{
554 struct iscsi_cls_session *session;
555
556 session = iscsi_alloc_session(shost, transport);
557 if (!session)
558 return NULL;
559
Mike Christie6a8a0d32006-06-28 12:00:31 -0500560 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500561 iscsi_free_session(session);
562 return NULL;
563 }
564 return session;
565}
566EXPORT_SYMBOL_GPL(iscsi_create_session);
567
Mike Christie26974782007-12-13 12:43:29 -0600568static void iscsi_conn_release(struct device *dev)
569{
570 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
571 struct device *parent = conn->dev.parent;
572
573 kfree(conn);
574 put_device(parent);
575}
576
577static int iscsi_is_conn_dev(const struct device *dev)
578{
579 return dev->release == iscsi_conn_release;
580}
581
582static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
583{
584 if (!iscsi_is_conn_dev(dev))
585 return 0;
586 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
587}
588
Mike Christie8434aa82006-06-28 12:00:30 -0500589void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600590{
Mike Christie30a6c652006-04-06 21:13:39 -0500591 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500592 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600593 unsigned long flags;
594 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500595
Mike Christie26974782007-12-13 12:43:29 -0600596 spin_lock_irqsave(&sesslock, flags);
597 list_del(&session->sess_list);
598 spin_unlock_irqrestore(&sesslock, flags);
599
Mike Christie45ab33b2008-03-04 13:26:55 -0600600 /* make sure there are no blocks/unblocks queued */
601 flush_workqueue(iscsi_eh_timer_workq);
602 /* make sure the timedout callout is not running */
603 if (!cancel_delayed_work(&session->recovery_work))
604 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600605 /*
606 * If we are blocked let commands flow again. The lld or iscsi
607 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600608 * We assume that LLD will not be calling block/unblock while
609 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600610 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600611 spin_lock_irqsave(&session->lock, flags);
612 session->state = ISCSI_SESSION_FREE;
613 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600614
Mike Christie45ab33b2008-03-04 13:26:55 -0600615 scsi_target_unblock(&session->dev);
616 /* flush running scans then delete devices */
Mike Christiebd976f62008-01-31 13:36:46 -0600617 flush_workqueue(ihost->scan_workq);
Mike Christie45ab33b2008-03-04 13:26:55 -0600618 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500619
Mike Christie26974782007-12-13 12:43:29 -0600620 /* hw iscsi may not have removed all connections from session */
621 err = device_for_each_child(&session->dev, NULL,
622 iscsi_iter_destroy_conn_fn);
623 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600624 iscsi_cls_session_printk(KERN_ERR, session,
625 "Could not delete all connections "
626 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500627
Mike Christie7b8631b2006-01-13 18:05:50 -0600628 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500629 device_del(&session->dev);
630}
631EXPORT_SYMBOL_GPL(iscsi_remove_session);
632
633void iscsi_free_session(struct iscsi_cls_session *session)
634{
Mike Christie26974782007-12-13 12:43:29 -0600635 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500636 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600637}
Mike Christie8434aa82006-06-28 12:00:30 -0500638EXPORT_SYMBOL_GPL(iscsi_free_session);
639
640/**
641 * iscsi_destroy_session - destroy iscsi session
642 * @session: iscsi_session
643 *
644 * Can be called by a LLD or iscsi_transport. There must not be
645 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500646 */
Mike Christie8434aa82006-06-28 12:00:30 -0500647int iscsi_destroy_session(struct iscsi_cls_session *session)
648{
649 iscsi_remove_session(session);
650 iscsi_free_session(session);
651 return 0;
652}
Mike Christie7b8631b2006-01-13 18:05:50 -0600653EXPORT_SYMBOL_GPL(iscsi_destroy_session);
654
Mike Christie7b8631b2006-01-13 18:05:50 -0600655/**
656 * iscsi_create_conn - create iscsi class connection
657 * @session: iscsi cls session
658 * @cid: connection id
659 *
660 * This can be called from a LLD or iscsi_transport. The connection
661 * is child of the session so cid must be unique for all connections
662 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500663 *
664 * Since we do not support MCS, cid will normally be zero. In some cases
665 * for software iscsi we could be trying to preallocate a connection struct
666 * in which case there could be two connection structs and cid would be
667 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500668 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600669struct iscsi_cls_conn *
670iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
671{
672 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600673 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600674 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600675 int err;
676
677 conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL);
678 if (!conn)
679 return NULL;
680
681 if (transport->conndata_size)
682 conn->dd_data = &conn[1];
683
684 INIT_LIST_HEAD(&conn->conn_list);
685 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500686 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600687
688 /* this is released in the dev's release function */
689 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400690 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500691
Mike Christie7b8631b2006-01-13 18:05:50 -0600692 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500693 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600694 conn->dev.parent = &session->dev;
695 conn->dev.release = iscsi_conn_release;
696 err = device_register(&conn->dev);
697 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600698 iscsi_cls_session_printk(KERN_ERR, session, "could not "
699 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600700 goto release_parent_ref;
701 }
702 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600703
704 spin_lock_irqsave(&connlock, flags);
705 list_add(&conn->conn_list, &connlist);
706 conn->active = 1;
707 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600708 return conn;
709
710release_parent_ref:
711 put_device(&session->dev);
712free_conn:
713 kfree(conn);
714 return NULL;
715}
716
717EXPORT_SYMBOL_GPL(iscsi_create_conn);
718
719/**
720 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500721 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600722 *
Mike Christie26974782007-12-13 12:43:29 -0600723 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500724 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600725int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
726{
Mike Christie26974782007-12-13 12:43:29 -0600727 unsigned long flags;
728
729 spin_lock_irqsave(&connlock, flags);
730 conn->active = 0;
731 list_del(&conn->conn_list);
732 spin_unlock_irqrestore(&connlock, flags);
733
Mike Christie7b8631b2006-01-13 18:05:50 -0600734 transport_unregister_device(&conn->dev);
735 device_unregister(&conn->dev);
736 return 0;
737}
Mike Christie7b8631b2006-01-13 18:05:50 -0600738EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
739
740/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600741 * iscsi interface functions
742 */
Alex Aizman0896b752005-08-04 19:33:07 -0700743static struct iscsi_internal *
744iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Alex Aizman0896b752005-08-04 19:33:07 -0700746 struct iscsi_internal *priv;
747 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Alex Aizman0896b752005-08-04 19:33:07 -0700749 spin_lock_irqsave(&iscsi_transport_lock, flags);
750 list_for_each_entry(priv, &iscsi_transports, list) {
751 if (tt == priv->iscsi_transport) {
752 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
753 return priv;
754 }
755 }
756 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
757 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alex Aizman0896b752005-08-04 19:33:07 -0700760static int
Mike Christie43a145a2006-10-16 18:09:38 -0400761iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500762{
Mike Christie53cb8a12006-06-28 12:00:32 -0500763 int rc;
764
Mike Christie9aaa2b42006-07-24 15:47:34 -0500765 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500766 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500767 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
768 return rc;
769 }
770
Mike Christie53cb8a12006-06-28 12:00:32 -0500771 return 0;
772}
773
774static int
Mike Christie43a145a2006-10-16 18:09:38 -0400775iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700776{
Alex Aizman0896b752005-08-04 19:33:07 -0700777 int rc;
778
Mike Christie790f39a2006-05-18 20:31:39 -0500779 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700780 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700781 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
782 return rc;
783 }
784
Alex Aizman0896b752005-08-04 19:33:07 -0700785 return 0;
786}
787
Mike Christie7b7232f2006-02-01 21:06:49 -0600788int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700789 char *data, uint32_t data_size)
790{
791 struct nlmsghdr *nlh;
792 struct sk_buff *skb;
793 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700794 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500795 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700796 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
797 data_size);
798
Mike Christie790f39a2006-05-18 20:31:39 -0500799 priv = iscsi_if_transport_lookup(conn->transport);
800 if (!priv)
801 return -EINVAL;
802
Mike Christie43a145a2006-10-16 18:09:38 -0400803 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700804 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600805 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -0600806 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
807 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700808 return -ENOMEM;
809 }
810
Mike Christie790f39a2006-05-18 20:31:39 -0500811 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700812 ev = NLMSG_DATA(nlh);
813 memset(ev, 0, sizeof(*ev));
814 ev->transport_handle = iscsi_handle(conn->transport);
815 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500816 ev->r.recv_req.cid = conn->cid;
817 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700818 pdu = (char*)ev + sizeof(*ev);
819 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
820 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
821
Mike Christie43a145a2006-10-16 18:09:38 -0400822 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700823}
824EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
825
Mike Christie7b7232f2006-02-01 21:06:49 -0600826void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700827{
828 struct nlmsghdr *nlh;
829 struct sk_buff *skb;
830 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500831 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700832 int len = NLMSG_SPACE(sizeof(*ev));
833
Mike Christie790f39a2006-05-18 20:31:39 -0500834 priv = iscsi_if_transport_lookup(conn->transport);
835 if (!priv)
836 return;
837
Mike Christie43a145a2006-10-16 18:09:38 -0400838 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700839 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -0600840 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
841 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700842 return;
843 }
844
Mike Christie790f39a2006-05-18 20:31:39 -0500845 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700846 ev = NLMSG_DATA(nlh);
847 ev->transport_handle = iscsi_handle(conn->transport);
848 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700849 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500850 ev->r.connerror.cid = conn->cid;
851 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700852
Mike Christie43a145a2006-10-16 18:09:38 -0400853 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700854
Mike Christie322d7392008-01-31 13:36:52 -0600855 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
856 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700857}
858EXPORT_SYMBOL_GPL(iscsi_conn_error);
859
860static int
861iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
862 void *payload, int size)
863{
864 struct sk_buff *skb;
865 struct nlmsghdr *nlh;
866 int len = NLMSG_SPACE(size);
867 int flags = multi ? NLM_F_MULTI : 0;
868 int t = done ? NLMSG_DONE : type;
869
Mike Christie43a145a2006-10-16 18:09:38 -0400870 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500871 if (!skb) {
872 printk(KERN_ERR "Could not allocate skb to send reply.\n");
873 return -ENOMEM;
874 }
Alex Aizman0896b752005-08-04 19:33:07 -0700875
876 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
877 nlh->nlmsg_flags = flags;
878 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400879 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700880}
881
882static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600883iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700884{
885 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
886 struct iscsi_stats *stats;
887 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600888 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700889 struct nlmsghdr *nlhstat;
890 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500891 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700892 int len = NLMSG_SPACE(sizeof(*ev) +
893 sizeof(struct iscsi_stats) +
894 sizeof(struct iscsi_stats_custom) *
895 ISCSI_STATS_CUSTOM_MAX);
896 int err = 0;
897
Mike Christie790f39a2006-05-18 20:31:39 -0500898 priv = iscsi_if_transport_lookup(transport);
899 if (!priv)
900 return -EINVAL;
901
Mike Christieb5c7a122006-04-06 21:13:33 -0500902 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700903 if (!conn)
904 return -EEXIST;
905
906 do {
907 int actual_size;
908
Mike Christie43a145a2006-10-16 18:09:38 -0400909 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700910 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -0600911 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
912 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700913 return -ENOMEM;
914 }
915
Mike Christie790f39a2006-05-18 20:31:39 -0500916 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -0700917 (len - sizeof(*nlhstat)), 0);
918 evstat = NLMSG_DATA(nlhstat);
919 memset(evstat, 0, sizeof(*evstat));
920 evstat->transport_handle = iscsi_handle(conn->transport);
921 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -0500922 evstat->u.get_stats.cid =
923 ev->u.get_stats.cid;
924 evstat->u.get_stats.sid =
925 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -0700926 stats = (struct iscsi_stats *)
927 ((char*)evstat + sizeof(*evstat));
928 memset(stats, 0, sizeof(*stats));
929
Mike Christie7b7232f2006-02-01 21:06:49 -0600930 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -0700931 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
932 sizeof(struct iscsi_stats) +
933 sizeof(struct iscsi_stats_custom) *
934 stats->custom_length);
935 actual_size -= sizeof(*nlhstat);
936 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -0600937 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -0700938 nlhstat->nlmsg_len = actual_size;
939
Mike Christie43a145a2006-10-16 18:09:38 -0400940 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700941 } while (err < 0 && err != -ECONNREFUSED);
942
943 return err;
944}
945
Mike Christie53cb8a12006-06-28 12:00:32 -0500946/**
Mike Christie26974782007-12-13 12:43:29 -0600947 * iscsi_session_event - send session destr. completion event
948 * @session: iscsi class session
949 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -0500950 */
Mike Christie26974782007-12-13 12:43:29 -0600951int iscsi_session_event(struct iscsi_cls_session *session,
952 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -0500953{
954 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -0500955 struct Scsi_Host *shost;
956 struct iscsi_uevent *ev;
957 struct sk_buff *skb;
958 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -0500959 int rc, len = NLMSG_SPACE(sizeof(*ev));
960
Mike Christie26974782007-12-13 12:43:29 -0600961 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -0500962 if (!priv)
963 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -0500964 shost = iscsi_session_to_shost(session);
965
Mike Christie43a145a2006-10-16 18:09:38 -0400966 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -0500967 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -0600968 iscsi_cls_session_printk(KERN_ERR, session,
969 "Cannot notify userspace of session "
970 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -0500971 return -ENOMEM;
972 }
973
974 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
975 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -0600976 ev->transport_handle = iscsi_handle(session->transport);
977
978 ev->type = event;
979 switch (event) {
980 case ISCSI_KEVENT_DESTROY_SESSION:
981 ev->r.d_session.host_no = shost->host_no;
982 ev->r.d_session.sid = session->sid;
983 break;
984 case ISCSI_KEVENT_CREATE_SESSION:
985 ev->r.c_session_ret.host_no = shost->host_no;
986 ev->r.c_session_ret.sid = session->sid;
987 break;
988 case ISCSI_KEVENT_UNBIND_SESSION:
989 ev->r.unbind_session.host_no = shost->host_no;
990 ev->r.unbind_session.sid = session->sid;
991 break;
992 default:
Mike Christie322d7392008-01-31 13:36:52 -0600993 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
994 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -0600995 kfree_skb(skb);
996 return -EINVAL;
997 }
Mike Christie53cb8a12006-06-28 12:00:32 -0500998
999 /*
1000 * this will occur if the daemon is not up, so we just warn
1001 * the user and when the daemon is restarted it will handle it
1002 */
Mike Christie43a145a2006-10-16 18:09:38 -04001003 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001004 if (rc < 0)
Mike Christie322d7392008-01-31 13:36:52 -06001005 iscsi_cls_session_printk(KERN_ERR, session,
1006 "Cannot notify userspace of session "
1007 "event %u. Check iscsi daemon\n",
1008 event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001009 return rc;
1010}
Mike Christie26974782007-12-13 12:43:29 -06001011EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001012
Alex Aizman0896b752005-08-04 19:33:07 -07001013static int
Mike Christie40753ca2008-05-21 15:53:56 -05001014iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev,
1015 uint32_t host_no, uint32_t initial_cmdsn,
1016 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001017{
1018 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001019 struct iscsi_cls_session *session;
Mike Christie40753ca2008-05-21 15:53:56 -05001020 struct Scsi_Host *shost = NULL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001021
Mike Christie40753ca2008-05-21 15:53:56 -05001022 /*
1023 * Software iscsi allocates a host per session, but
1024 * offload drivers (and possibly iser one day) allocate a host per
1025 * hba/nic/rnic. Offload will match a host here, but software will
1026 * return a new hostno after the create_session callback has returned.
1027 */
1028 if (host_no != UINT_MAX) {
1029 shost = scsi_host_lookup(host_no);
1030 if (IS_ERR(shost)) {
1031 printk(KERN_ERR "Could not find host no %u to "
1032 "create session\n", host_no);
1033 return -ENODEV;
1034 }
1035 }
1036
1037 session = transport->create_session(transport, &priv->t, shost,
1038 cmds_max, queue_depth,
1039 initial_cmdsn, &host_no);
1040 if (shost)
1041 scsi_host_put(shost);
Mike Christie7b7232f2006-02-01 21:06:49 -06001042 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001043 return -ENOMEM;
1044
Mike Christie40753ca2008-05-21 15:53:56 -05001045 ev->r.c_session_ret.host_no = host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001046 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001047 return 0;
1048}
1049
1050static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001051iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001052{
Mike Christie7b8631b2006-01-13 18:05:50 -06001053 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001054 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001055
Mike Christieb5c7a122006-04-06 21:13:33 -05001056 session = iscsi_session_lookup(ev->u.c_conn.sid);
1057 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001058 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001059 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001060 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001061 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001062
Mike Christie7b7232f2006-02-01 21:06:49 -06001063 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001064 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001065 iscsi_cls_session_printk(KERN_ERR, session,
1066 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001067 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001068 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001069
Mike Christieb5c7a122006-04-06 21:13:33 -05001070 ev->r.c_conn_ret.sid = session->sid;
1071 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001072 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001073}
1074
1075static int
1076iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1077{
Mike Christie7b8631b2006-01-13 18:05:50 -06001078 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001079
Mike Christieb5c7a122006-04-06 21:13:33 -05001080 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001081 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001082 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001083
Mike Christie7b8631b2006-01-13 18:05:50 -06001084 if (transport->destroy_conn)
1085 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001086 return 0;
1087}
1088
Mike Christiefd7255f2006-04-06 21:13:36 -05001089static int
1090iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1091{
1092 char *data = (char*)ev + sizeof(*ev);
1093 struct iscsi_cls_conn *conn;
1094 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001095 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001096
1097 session = iscsi_session_lookup(ev->u.set_param.sid);
1098 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1099 if (!conn || !session)
1100 return -EINVAL;
1101
1102 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001103 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001104 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001105 if (value != 0)
1106 session->recovery_tmo = value;
1107 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001108 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001109 err = transport->set_param(conn, ev->u.set_param.param,
1110 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001111 }
1112
1113 return err;
1114}
1115
Mike Christie7b8631b2006-01-13 18:05:50 -06001116static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001117iscsi_if_transport_ep(struct iscsi_transport *transport,
1118 struct iscsi_uevent *ev, int msg_type)
1119{
1120 struct sockaddr *dst_addr;
1121 int rc = 0;
1122
1123 switch (msg_type) {
1124 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1125 if (!transport->ep_connect)
1126 return -EINVAL;
1127
1128 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1129 rc = transport->ep_connect(dst_addr,
1130 ev->u.ep_connect.non_blocking,
1131 &ev->r.ep_connect_ret.handle);
1132 break;
1133 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1134 if (!transport->ep_poll)
1135 return -EINVAL;
1136
1137 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1138 ev->u.ep_poll.timeout_ms);
1139 break;
1140 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1141 if (!transport->ep_disconnect)
1142 return -EINVAL;
1143
1144 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1145 break;
1146 }
1147 return rc;
1148}
1149
1150static int
Mike Christie01cb2252006-06-28 12:00:22 -05001151iscsi_tgt_dscvr(struct iscsi_transport *transport,
1152 struct iscsi_uevent *ev)
1153{
Mike Christie2174a042007-05-30 12:57:10 -05001154 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001155 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001156 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001157
1158 if (!transport->tgt_dscvr)
1159 return -EINVAL;
1160
Mike Christie2174a042007-05-30 12:57:10 -05001161 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1162 if (IS_ERR(shost)) {
1163 printk(KERN_ERR "target discovery could not find host no %u\n",
1164 ev->u.tgt_dscvr.host_no);
1165 return -ENODEV;
1166 }
1167
1168
Mike Christie01cb2252006-06-28 12:00:22 -05001169 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001170 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1171 ev->u.tgt_dscvr.enable, dst_addr);
1172 scsi_host_put(shost);
1173 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001174}
1175
1176static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001177iscsi_set_host_param(struct iscsi_transport *transport,
1178 struct iscsi_uevent *ev)
1179{
1180 char *data = (char*)ev + sizeof(*ev);
1181 struct Scsi_Host *shost;
1182 int err;
1183
1184 if (!transport->set_host_param)
1185 return -ENOSYS;
1186
1187 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1188 if (IS_ERR(shost)) {
1189 printk(KERN_ERR "set_host_param could not find host no %u\n",
1190 ev->u.set_host_param.host_no);
1191 return -ENODEV;
1192 }
1193
1194 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1195 data, ev->u.set_host_param.len);
1196 scsi_host_put(shost);
1197 return err;
1198}
1199
1200static int
Alex Aizman0896b752005-08-04 19:33:07 -07001201iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1202{
1203 int err = 0;
Mike Christie40753ca2008-05-21 15:53:56 -05001204 uint32_t host_no = UINT_MAX;
Alex Aizman0896b752005-08-04 19:33:07 -07001205 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1206 struct iscsi_transport *transport = NULL;
1207 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001208 struct iscsi_cls_session *session;
1209 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001210
Alex Aizman0896b752005-08-04 19:33:07 -07001211 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1212 if (!priv)
1213 return -EINVAL;
1214 transport = priv->iscsi_transport;
1215
Mike Christie7b7232f2006-02-01 21:06:49 -06001216 if (!try_module_get(transport->owner))
1217 return -EINVAL;
1218
Mike Christie790f39a2006-05-18 20:31:39 -05001219 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1220
Alex Aizman0896b752005-08-04 19:33:07 -07001221 switch (nlh->nlmsg_type) {
1222 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christie40753ca2008-05-21 15:53:56 -05001223 err = iscsi_if_create_session(priv, ev, host_no,
1224 ev->u.c_session.initial_cmdsn,
1225 ev->u.c_session.cmds_max,
1226 ev->u.c_session.queue_depth);
1227 break;
1228 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1229 err = iscsi_if_create_session(priv, ev,
1230 ev->u.c_bound_session.host_no,
1231 ev->u.c_bound_session.initial_cmdsn,
1232 ev->u.c_bound_session.cmds_max,
1233 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001234 break;
1235 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001236 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001237 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001238 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001239 else
1240 err = -EINVAL;
1241 break;
1242 case ISCSI_UEVENT_UNBIND_SESSION:
1243 session = iscsi_session_lookup(ev->u.d_session.sid);
1244 if (session)
1245 iscsi_unbind_session(session);
1246 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001247 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001248 break;
1249 case ISCSI_UEVENT_CREATE_CONN:
1250 err = iscsi_if_create_conn(transport, ev);
1251 break;
1252 case ISCSI_UEVENT_DESTROY_CONN:
1253 err = iscsi_if_destroy_conn(transport, ev);
1254 break;
1255 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001256 session = iscsi_session_lookup(ev->u.b_conn.sid);
1257 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001258
1259 if (session && conn)
1260 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001261 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001262 ev->u.b_conn.is_leading);
1263 else
1264 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001265 break;
1266 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001267 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001268 break;
1269 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001270 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001271 if (conn)
1272 ev->r.retcode = transport->start_conn(conn);
1273 else
1274 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001275 break;
1276 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001277 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001278 if (conn)
1279 transport->stop_conn(conn, ev->u.stop_conn.flag);
1280 else
1281 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001282 break;
1283 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001284 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001285 if (conn)
1286 ev->r.retcode = transport->send_pdu(conn,
1287 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1288 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1289 ev->u.send_pdu.data_size);
1290 else
1291 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001292 break;
1293 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001294 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001295 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001296 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1297 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1298 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1299 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1300 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001301 case ISCSI_UEVENT_TGT_DSCVR:
1302 err = iscsi_tgt_dscvr(transport, ev);
1303 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001304 case ISCSI_UEVENT_SET_HOST_PARAM:
1305 err = iscsi_set_host_param(transport, ev);
1306 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001307 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001308 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001309 break;
1310 }
1311
Mike Christie7b7232f2006-02-01 21:06:49 -06001312 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001313 return err;
1314}
1315
Mike Christieb5c7a122006-04-06 21:13:33 -05001316/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001317 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1318 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001319 */
Alex Aizman0896b752005-08-04 19:33:07 -07001320static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001321iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001322{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001323 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001324 while (skb->len >= NLMSG_SPACE(0)) {
1325 int err;
1326 uint32_t rlen;
1327 struct nlmsghdr *nlh;
1328 struct iscsi_uevent *ev;
1329
1330 nlh = nlmsg_hdr(skb);
1331 if (nlh->nlmsg_len < sizeof(*nlh) ||
1332 skb->len < nlh->nlmsg_len) {
1333 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001334 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001335
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001336 ev = NLMSG_DATA(nlh);
1337 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1338 if (rlen > skb->len)
1339 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001340
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001341 err = iscsi_if_recv_msg(skb, nlh);
1342 if (err) {
1343 ev->type = ISCSI_KEVENT_IF_ERROR;
1344 ev->iferror = err;
1345 }
1346 do {
1347 /*
1348 * special case for GET_STATS:
1349 * on success - sending reply and stats from
1350 * inside of if_recv_msg(),
1351 * on error - fall through.
1352 */
1353 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001354 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001355 err = iscsi_if_send_reply(
1356 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1357 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1358 } while (err < 0 && err != -ECONNREFUSED);
1359 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001360 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001361 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001362}
1363
Mike Christiefd7255f2006-04-06 21:13:36 -05001364#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001365struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001366 __ATTR(_name,_mode,_show,_store)
1367
Alex Aizman0896b752005-08-04 19:33:07 -07001368/*
1369 * iSCSI connection attrs
1370 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001371#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001373show_conn_param_##param(struct device *dev, \
1374 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001375{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001376 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001377 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001378 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001379}
1380
Mike Christiea54a52c2006-06-28 12:00:23 -05001381#define iscsi_conn_attr(field, param) \
1382 iscsi_conn_attr_show(param) \
1383static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001384 NULL);
1385
Mike Christiea54a52c2006-06-28 12:00:23 -05001386iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1387iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1388iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1389iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1390iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1391iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1392iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1393iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1394iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1395iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1396iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001397iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1398iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400/*
Alex Aizman0896b752005-08-04 19:33:07 -07001401 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Mike Christieb2c64162007-05-30 12:57:16 -05001403#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001405show_session_param_##param(struct device *dev, \
1406 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001407{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001408 struct iscsi_cls_session *session = \
1409 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001410 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001411 \
1412 if (perm && !capable(CAP_SYS_ADMIN)) \
1413 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001414 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Mike Christieb2c64162007-05-30 12:57:16 -05001417#define iscsi_session_attr(field, param, perm) \
1418 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001419static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001420 NULL);
1421
Mike Christieb2c64162007-05-30 12:57:16 -05001422iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1423iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1424iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1425iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1426iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1427iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1428iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1429iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1430iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1431iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1432iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1433iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1434iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1435iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001436iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1437iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1438iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001439
Mike Christie6eabafb2008-01-31 13:36:43 -06001440static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001441show_priv_session_state(struct device *dev, struct device_attribute *attr,
1442 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001443{
Tony Jonesee959b02008-02-22 00:13:36 +01001444 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001445 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1446}
1447static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1448 NULL);
1449
Mike Christiefd7255f2006-04-06 21:13:36 -05001450#define iscsi_priv_session_attr_show(field, format) \
1451static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001452show_priv_session_##field(struct device *dev, \
1453 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001454{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001455 struct iscsi_cls_session *session = \
1456 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001457 return sprintf(buf, format"\n", session->field); \
1458}
1459
1460#define iscsi_priv_session_attr(field, format) \
1461 iscsi_priv_session_attr_show(field, format) \
1462static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1463 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001464iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001465
Mike Christie1819dc82007-05-30 12:57:08 -05001466/*
1467 * iSCSI host attrs
1468 */
1469#define iscsi_host_attr_show(param) \
1470static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001471show_host_param_##param(struct device *dev, \
1472 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001473{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001474 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001475 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1476 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1477}
1478
1479#define iscsi_host_attr(field, param) \
1480 iscsi_host_attr_show(param) \
1481static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1482 NULL);
1483
Mike Christied8196ed2007-05-30 12:57:25 -05001484iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001485iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001486iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001487iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001488
Mike Christiefd7255f2006-04-06 21:13:36 -05001489#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1490do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001491 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001492 count++; \
1493} while (0)
1494
Mike Christiea54a52c2006-06-28 12:00:23 -05001495
Mike Christiefd7255f2006-04-06 21:13:36 -05001496#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1497do { \
1498 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001499 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001501 } \
1502} while (0)
1503
Mike Christiefd7255f2006-04-06 21:13:36 -05001504#define SETUP_CONN_RD_ATTR(field, param_flag) \
1505do { \
1506 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001507 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001508 count++; \
1509 } \
1510} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Mike Christie1819dc82007-05-30 12:57:08 -05001512#define SETUP_HOST_RD_ATTR(field, param_flag) \
1513do { \
1514 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001515 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001516 count++; \
1517 } \
1518} while (0)
1519
Alex Aizman0896b752005-08-04 19:33:07 -07001520static int iscsi_session_match(struct attribute_container *cont,
1521 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Mike Christie7b8631b2006-01-13 18:05:50 -06001523 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001525 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Alex Aizman0896b752005-08-04 19:33:07 -07001527 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return 0;
1529
Mike Christie7b8631b2006-01-13 18:05:50 -06001530 session = iscsi_dev_to_session(dev);
1531 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001532 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return 0;
1534
Alex Aizman0896b752005-08-04 19:33:07 -07001535 priv = to_iscsi_internal(shost->transportt);
1536 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1537 return 0;
1538
1539 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Alex Aizman0896b752005-08-04 19:33:07 -07001542static int iscsi_conn_match(struct attribute_container *cont,
1543 struct device *dev)
1544{
Mike Christie7b8631b2006-01-13 18:05:50 -06001545 struct iscsi_cls_session *session;
1546 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001547 struct Scsi_Host *shost;
1548 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Alex Aizman0896b752005-08-04 19:33:07 -07001550 if (!iscsi_is_conn_dev(dev))
1551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Mike Christie7b8631b2006-01-13 18:05:50 -06001553 conn = iscsi_dev_to_conn(dev);
1554 session = iscsi_dev_to_session(conn->dev.parent);
1555 shost = iscsi_session_to_shost(session);
1556
Alex Aizman0896b752005-08-04 19:33:07 -07001557 if (!shost->transportt)
1558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Alex Aizman0896b752005-08-04 19:33:07 -07001560 priv = to_iscsi_internal(shost->transportt);
1561 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1562 return 0;
1563
1564 return &priv->conn_cont.ac == cont;
1565}
1566
Mike Christie30a6c652006-04-06 21:13:39 -05001567static int iscsi_host_match(struct attribute_container *cont,
1568 struct device *dev)
1569{
1570 struct Scsi_Host *shost;
1571 struct iscsi_internal *priv;
1572
1573 if (!scsi_is_host_device(dev))
1574 return 0;
1575
1576 shost = dev_to_shost(dev);
1577 if (!shost->transportt ||
1578 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1579 return 0;
1580
1581 priv = to_iscsi_internal(shost->transportt);
1582 return &priv->t.host_attrs.ac == cont;
1583}
1584
Mike Christie7b8631b2006-01-13 18:05:50 -06001585struct scsi_transport_template *
1586iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001587{
1588 struct iscsi_internal *priv;
1589 unsigned long flags;
1590 int count = 0, err;
1591
1592 BUG_ON(!tt);
1593
1594 priv = iscsi_if_transport_lookup(tt);
1595 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001596 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001597
Jes Sorensen24669f752006-01-16 10:31:18 -05001598 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001599 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001600 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001601 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001602 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001603 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001604 priv->t.user_scan = iscsi_user_scan;
Alex Aizman0896b752005-08-04 19:33:07 -07001605
Tony Jonesee959b02008-02-22 00:13:36 +01001606 priv->dev.class = &iscsi_transport_class;
1607 snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1608 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001609 if (err)
1610 goto free_priv;
1611
Tony Jonesee959b02008-02-22 00:13:36 +01001612 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001613 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001614 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001615
Mike Christie30a6c652006-04-06 21:13:39 -05001616 /* host parameters */
1617 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1618 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1619 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001620 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001621 transport_container_register(&priv->t.host_attrs);
1622
Mike Christied8196ed2007-05-30 12:57:25 -05001623 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001624 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001625 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001626 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001627 BUG_ON(count > ISCSI_HOST_ATTRS);
1628 priv->host_attrs[count] = NULL;
1629 count = 0;
1630
Alex Aizman0896b752005-08-04 19:33:07 -07001631 /* connection parameters */
1632 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1633 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1634 priv->conn_cont.ac.match = iscsi_conn_match;
1635 transport_container_register(&priv->conn_cont);
1636
Mike Christiefd7255f2006-04-06 21:13:36 -05001637 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1638 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1639 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1640 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1641 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1642 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1643 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1644 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001645 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001646 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1647 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001648 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1649 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001650
1651 BUG_ON(count > ISCSI_CONN_ATTRS);
1652 priv->conn_attrs[count] = NULL;
1653 count = 0;
1654
1655 /* session parameters */
1656 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1657 priv->session_cont.ac.class = &iscsi_session_class.class;
1658 priv->session_cont.ac.match = iscsi_session_match;
1659 transport_container_register(&priv->session_cont);
1660
Mike Christiefd7255f2006-04-06 21:13:36 -05001661 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1662 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1663 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1664 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1665 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1666 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1667 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1668 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001669 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1670 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001671 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1672 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1673 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1674 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001675 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001676 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1677 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie30a6c652006-04-06 21:13:39 -05001678 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001679 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001682 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Alex Aizman0896b752005-08-04 19:33:07 -07001684 spin_lock_irqsave(&iscsi_transport_lock, flags);
1685 list_add(&priv->list, &iscsi_transports);
1686 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Alex Aizman0896b752005-08-04 19:33:07 -07001688 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001689 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Tony Jonesee959b02008-02-22 00:13:36 +01001691unregister_dev:
1692 device_unregister(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001693free_priv:
1694 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001695 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
Alex Aizman0896b752005-08-04 19:33:07 -07001697EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Alex Aizman0896b752005-08-04 19:33:07 -07001699int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Alex Aizman0896b752005-08-04 19:33:07 -07001701 struct iscsi_internal *priv;
1702 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Alex Aizman0896b752005-08-04 19:33:07 -07001704 BUG_ON(!tt);
1705
Arjan van de Ven0b950672006-01-11 13:16:10 +01001706 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001707
1708 priv = iscsi_if_transport_lookup(tt);
1709 BUG_ON (!priv);
1710
Alex Aizman0896b752005-08-04 19:33:07 -07001711 spin_lock_irqsave(&iscsi_transport_lock, flags);
1712 list_del(&priv->list);
1713 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1714
1715 transport_container_unregister(&priv->conn_cont);
1716 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001717 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001718
Tony Jonesee959b02008-02-22 00:13:36 +01001719 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1720 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001721 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001722
1723 return 0;
1724}
1725EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static __init int iscsi_transport_init(void)
1728{
Alex Aizman0896b752005-08-04 19:33:07 -07001729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Meelis Roos09492602006-12-17 12:10:26 -06001731 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001732 ISCSI_TRANSPORT_VERSION);
1733
Mike Christie41be1442007-02-28 17:32:18 -06001734 atomic_set(&iscsi_session_nr, 0);
1735
Alex Aizman0896b752005-08-04 19:33:07 -07001736 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (err)
1738 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001739
Mike Christie30a6c652006-04-06 21:13:39 -05001740 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001741 if (err)
1742 goto unregister_transport_class;
1743
Mike Christie30a6c652006-04-06 21:13:39 -05001744 err = transport_class_register(&iscsi_connection_class);
1745 if (err)
1746 goto unregister_host_class;
1747
Alex Aizman0896b752005-08-04 19:33:07 -07001748 err = transport_class_register(&iscsi_session_class);
1749 if (err)
1750 goto unregister_conn_class;
1751
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001752 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001753 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001754 if (!nls) {
1755 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001756 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001757 }
1758
Mike Christied8bf5412007-12-13 12:43:27 -06001759 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1760 if (!iscsi_eh_timer_workq)
1761 goto release_nls;
1762
Mike Christie43a145a2006-10-16 18:09:38 -04001763 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001764
Mike Christied8bf5412007-12-13 12:43:27 -06001765release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001766 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001767unregister_session_class:
1768 transport_class_unregister(&iscsi_session_class);
1769unregister_conn_class:
1770 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001771unregister_host_class:
1772 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001773unregister_transport_class:
1774 class_unregister(&iscsi_transport_class);
1775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778static void __exit iscsi_transport_exit(void)
1779{
Mike Christied8bf5412007-12-13 12:43:27 -06001780 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001781 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001782 transport_class_unregister(&iscsi_connection_class);
1783 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001784 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001785 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
1788module_init(iscsi_transport_init);
1789module_exit(iscsi_transport_exit);
1790
Alex Aizman0896b752005-08-04 19:33:07 -07001791MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1792 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1793 "Alex Aizman <itn780@yahoo.com>");
1794MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001796MODULE_VERSION(ISCSI_TRANSPORT_VERSION);