blob: ac9d298f54e7f8390bb1a0ad42f04a82aad119d3 [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");
Alex Aizman0896b752005-08-04 19:33:07 -0700104
105static struct attribute *iscsi_transport_attrs[] = {
Tony Jonesee959b02008-02-22 00:13:36 +0100106 &dev_attr_handle.attr,
107 &dev_attr_caps.attr,
Alex Aizman0896b752005-08-04 19:33:07 -0700108 NULL,
109};
110
111static struct attribute_group iscsi_transport_group = {
112 .attrs = iscsi_transport_attrs,
113};
114
Mike Christie30a6c652006-04-06 21:13:39 -0500115static int iscsi_setup_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100116 struct device *cdev)
Mike Christie30a6c652006-04-06 21:13:39 -0500117{
118 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500119 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500120
121 memset(ihost, 0, sizeof(*ihost));
Mike Christie8aae18a2008-01-31 13:36:48 -0600122 atomic_set(&ihost->nr_scans, 0);
Mike Christie79706342008-05-21 15:54:12 -0500123 mutex_init(&ihost->mutex);
Mike Christie26974782007-12-13 12:43:29 -0600124
Mike Christiebd976f62008-01-31 13:36:46 -0600125 snprintf(ihost->scan_workq_name, KOBJ_NAME_LEN, "iscsi_scan_%d",
Mike Christie26974782007-12-13 12:43:29 -0600126 shost->host_no);
Mike Christiebd976f62008-01-31 13:36:46 -0600127 ihost->scan_workq = create_singlethread_workqueue(
128 ihost->scan_workq_name);
129 if (!ihost->scan_workq)
Mike Christie26974782007-12-13 12:43:29 -0600130 return -ENOMEM;
131 return 0;
132}
133
134static int iscsi_remove_host(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +0100135 struct device *cdev)
Mike Christie26974782007-12-13 12:43:29 -0600136{
137 struct Scsi_Host *shost = dev_to_shost(dev);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500138 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600139
Mike Christiebd976f62008-01-31 13:36:46 -0600140 destroy_workqueue(ihost->scan_workq);
Mike Christie30a6c652006-04-06 21:13:39 -0500141 return 0;
142}
143
144static DECLARE_TRANSPORT_CLASS(iscsi_host_class,
145 "iscsi_host",
146 iscsi_setup_host,
Mike Christie26974782007-12-13 12:43:29 -0600147 iscsi_remove_host,
Mike Christie30a6c652006-04-06 21:13:39 -0500148 NULL);
149
Alex Aizman0896b752005-08-04 19:33:07 -0700150static DECLARE_TRANSPORT_CLASS(iscsi_session_class,
151 "iscsi_session",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 NULL,
153 NULL,
154 NULL);
155
Alex Aizman0896b752005-08-04 19:33:07 -0700156static DECLARE_TRANSPORT_CLASS(iscsi_connection_class,
157 "iscsi_connection",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 NULL,
159 NULL,
160 NULL);
Alex Aizman0896b752005-08-04 19:33:07 -0700161
162static struct sock *nls;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100163static DEFINE_MUTEX(rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -0700164
Mike Christie7b7232f2006-02-01 21:06:49 -0600165static LIST_HEAD(sesslist);
166static DEFINE_SPINLOCK(sesslock);
Alex Aizman0896b752005-08-04 19:33:07 -0700167static LIST_HEAD(connlist);
168static DEFINE_SPINLOCK(connlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Mike Christieb5c7a122006-04-06 21:13:33 -0500170static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
171{
172 struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
173 return sess->sid;
174}
175
176/*
177 * Returns the matching session to a given sid
178 */
179static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600180{
181 unsigned long flags;
182 struct iscsi_cls_session *sess;
183
184 spin_lock_irqsave(&sesslock, flags);
185 list_for_each_entry(sess, &sesslist, sess_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500186 if (sess->sid == sid) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600187 spin_unlock_irqrestore(&sesslock, flags);
188 return sess;
189 }
190 }
191 spin_unlock_irqrestore(&sesslock, flags);
192 return NULL;
193}
194
Mike Christieb5c7a122006-04-06 21:13:33 -0500195/*
196 * Returns the matching connection to a given sid / cid tuple
197 */
198static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
Mike Christie7b7232f2006-02-01 21:06:49 -0600199{
200 unsigned long flags;
201 struct iscsi_cls_conn *conn;
202
203 spin_lock_irqsave(&connlock, flags);
204 list_for_each_entry(conn, &connlist, conn_list) {
Mike Christieb5c7a122006-04-06 21:13:33 -0500205 if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600206 spin_unlock_irqrestore(&connlock, flags);
207 return conn;
208 }
209 }
210 spin_unlock_irqrestore(&connlock, flags);
211 return NULL;
212}
213
Mike Christie7b8631b2006-01-13 18:05:50 -0600214/*
215 * The following functions can be used by LLDs that allocate
216 * their own scsi_hosts or by software iscsi LLDs
217 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600218static struct {
219 int value;
220 char *name;
221} iscsi_session_state_names[] = {
222 { ISCSI_SESSION_LOGGED_IN, "LOGGED_IN" },
223 { ISCSI_SESSION_FAILED, "FAILED" },
224 { ISCSI_SESSION_FREE, "FREE" },
225};
226
Adrian Bunk3b0f2082008-02-13 23:30:17 +0200227static const char *iscsi_session_state_name(int state)
Mike Christie6eabafb2008-01-31 13:36:43 -0600228{
229 int i;
230 char *name = NULL;
231
232 for (i = 0; i < ARRAY_SIZE(iscsi_session_state_names); i++) {
233 if (iscsi_session_state_names[i].value == state) {
234 name = iscsi_session_state_names[i].name;
235 break;
236 }
237 }
238 return name;
239}
240
241int iscsi_session_chkready(struct iscsi_cls_session *session)
242{
243 unsigned long flags;
244 int err;
245
246 spin_lock_irqsave(&session->lock, flags);
247 switch (session->state) {
248 case ISCSI_SESSION_LOGGED_IN:
249 err = 0;
250 break;
251 case ISCSI_SESSION_FAILED:
252 err = DID_IMM_RETRY << 16;
253 break;
254 case ISCSI_SESSION_FREE:
255 err = DID_NO_CONNECT << 16;
256 break;
257 default:
258 err = DID_NO_CONNECT << 16;
259 break;
260 }
261 spin_unlock_irqrestore(&session->lock, flags);
262 return err;
263}
264EXPORT_SYMBOL_GPL(iscsi_session_chkready);
265
Mike Christie7b8631b2006-01-13 18:05:50 -0600266static void iscsi_session_release(struct device *dev)
267{
268 struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600269 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Mike Christie7b8631b2006-01-13 18:05:50 -0600271 shost = iscsi_session_to_shost(session);
272 scsi_host_put(shost);
273 kfree(session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600274}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Mike Christie7b8631b2006-01-13 18:05:50 -0600276static int iscsi_is_session_dev(const struct device *dev)
277{
278 return dev->release == iscsi_session_release;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Mike Christiea4804cd2008-05-21 15:54:00 -0500281static int iscsi_iter_session_fn(struct device *dev, void *data)
282{
283 void (* fn) (struct iscsi_cls_session *) = data;
284
285 if (!iscsi_is_session_dev(dev))
286 return 0;
287 fn(iscsi_dev_to_session(dev));
288 return 0;
289}
290
291void iscsi_host_for_each_session(struct Scsi_Host *shost,
292 void (*fn)(struct iscsi_cls_session *))
293{
294 device_for_each_child(&shost->shost_gendev, fn,
295 iscsi_iter_session_fn);
296}
297EXPORT_SYMBOL_GPL(iscsi_host_for_each_session);
298
Mike Christie8aae18a2008-01-31 13:36:48 -0600299/**
300 * iscsi_scan_finished - helper to report when running scans are done
301 * @shost: scsi host
302 * @time: scan run time
303 *
304 * This function can be used by drives like qla4xxx to report to the scsi
305 * layer when the scans it kicked off at module load time are done.
306 */
307int iscsi_scan_finished(struct Scsi_Host *shost, unsigned long time)
308{
Mike Christie32c6e1b2008-05-21 15:53:58 -0500309 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie8aae18a2008-01-31 13:36:48 -0600310 /*
311 * qla4xxx will have kicked off some session unblocks before calling
312 * scsi_scan_host, so just wait for them to complete.
313 */
314 return !atomic_read(&ihost->nr_scans);
315}
316EXPORT_SYMBOL_GPL(iscsi_scan_finished);
317
Mike Christie79706342008-05-21 15:54:12 -0500318struct iscsi_scan_data {
319 unsigned int channel;
320 unsigned int id;
321 unsigned int lun;
322};
323
324static int iscsi_user_scan_session(struct device *dev, void *data)
325{
326 struct iscsi_scan_data *scan_data = data;
327 struct iscsi_cls_session *session;
328 struct Scsi_Host *shost;
329 struct iscsi_cls_host *ihost;
330 unsigned long flags;
331 unsigned int id;
332
333 if (!iscsi_is_session_dev(dev))
334 return 0;
335
336 session = iscsi_dev_to_session(dev);
337 shost = iscsi_session_to_shost(session);
338 ihost = shost->shost_data;
339
340 mutex_lock(&ihost->mutex);
341 spin_lock_irqsave(&session->lock, flags);
342 if (session->state != ISCSI_SESSION_LOGGED_IN) {
343 spin_unlock_irqrestore(&session->lock, flags);
344 mutex_unlock(&ihost->mutex);
345 return 0;
346 }
347 id = session->target_id;
348 spin_unlock_irqrestore(&session->lock, flags);
349
350 if (id != ISCSI_MAX_TARGET) {
351 if ((scan_data->channel == SCAN_WILD_CARD ||
352 scan_data->channel == 0) &&
353 (scan_data->id == SCAN_WILD_CARD ||
354 scan_data->id == id))
355 scsi_scan_target(&session->dev, 0, id,
356 scan_data->lun, 1);
357 }
358 mutex_unlock(&ihost->mutex);
359 return 0;
360}
361
Mike Christie30a6c652006-04-06 21:13:39 -0500362static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
363 uint id, uint lun)
364{
Mike Christie79706342008-05-21 15:54:12 -0500365 struct iscsi_scan_data scan_data;
Mike Christie30a6c652006-04-06 21:13:39 -0500366
Mike Christie79706342008-05-21 15:54:12 -0500367 scan_data.channel = channel;
368 scan_data.id = id;
369 scan_data.lun = lun;
Mike Christie30a6c652006-04-06 21:13:39 -0500370
Mike Christie79706342008-05-21 15:54:12 -0500371 return device_for_each_child(&shost->shost_gendev, &scan_data,
372 iscsi_user_scan_session);
Mike Christie30a6c652006-04-06 21:13:39 -0500373}
374
Mike Christiebd976f62008-01-31 13:36:46 -0600375static void iscsi_scan_session(struct work_struct *work)
376{
377 struct iscsi_cls_session *session =
378 container_of(work, struct iscsi_cls_session, scan_work);
Mike Christie8aae18a2008-01-31 13:36:48 -0600379 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500380 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500381 struct iscsi_scan_data scan_data;
Mike Christiebd976f62008-01-31 13:36:46 -0600382
Mike Christie79706342008-05-21 15:54:12 -0500383 scan_data.channel = 0;
384 scan_data.id = SCAN_WILD_CARD;
385 scan_data.lun = SCAN_WILD_CARD;
Mike Christiebd976f62008-01-31 13:36:46 -0600386
Mike Christie79706342008-05-21 15:54:12 -0500387 iscsi_user_scan_session(&session->dev, &scan_data);
Mike Christie8aae18a2008-01-31 13:36:48 -0600388 atomic_dec(&ihost->nr_scans);
Mike Christiebd976f62008-01-31 13:36:46 -0600389}
390
David Howellsc4028952006-11-22 14:57:56 +0000391static void session_recovery_timedout(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500392{
David Howellsc4028952006-11-22 14:57:56 +0000393 struct iscsi_cls_session *session =
394 container_of(work, struct iscsi_cls_session,
395 recovery_work.work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600396 unsigned long flags;
Mike Christie30a6c652006-04-06 21:13:39 -0500397
Mike Christie322d7392008-01-31 13:36:52 -0600398 iscsi_cls_session_printk(KERN_INFO, session,
399 "session recovery timed out after %d secs\n",
400 session->recovery_tmo);
Mike Christie30a6c652006-04-06 21:13:39 -0500401
Mike Christie6eabafb2008-01-31 13:36:43 -0600402 spin_lock_irqsave(&session->lock, flags);
403 switch (session->state) {
404 case ISCSI_SESSION_FAILED:
405 session->state = ISCSI_SESSION_FREE;
406 break;
407 case ISCSI_SESSION_LOGGED_IN:
408 case ISCSI_SESSION_FREE:
409 /* we raced with the unblock's flush */
410 spin_unlock_irqrestore(&session->lock, flags);
411 return;
412 }
413 spin_unlock_irqrestore(&session->lock, flags);
414
Mike Christie30a6c652006-04-06 21:13:39 -0500415 if (session->transport->session_recovery_timedout)
416 session->transport->session_recovery_timedout(session);
417
418 scsi_target_unblock(&session->dev);
419}
420
Mike Christie45ab33b2008-03-04 13:26:55 -0600421static void __iscsi_unblock_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500422{
Mike Christie45ab33b2008-03-04 13:26:55 -0600423 struct iscsi_cls_session *session =
424 container_of(work, struct iscsi_cls_session,
425 unblock_work);
Mike Christiebd976f62008-01-31 13:36:46 -0600426 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500427 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie6eabafb2008-01-31 13:36:43 -0600428 unsigned long flags;
429
Mike Christie45ab33b2008-03-04 13:26:55 -0600430 /*
431 * The recovery and unblock work get run from the same workqueue,
432 * so try to cancel it if it was going to run after this unblock.
433 */
434 cancel_delayed_work(&session->recovery_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600435 spin_lock_irqsave(&session->lock, flags);
436 session->state = ISCSI_SESSION_LOGGED_IN;
437 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie45ab33b2008-03-04 13:26:55 -0600438 /* start IO */
439 scsi_target_unblock(&session->dev);
Mike Christie8aae18a2008-01-31 13:36:48 -0600440 /*
441 * Only do kernel scanning if the driver is properly hooked into
442 * the async scanning code (drivers like iscsi_tcp do login and
443 * scanning from userspace).
444 */
445 if (shost->hostt->scan_finished) {
446 if (queue_work(ihost->scan_workq, &session->scan_work))
447 atomic_inc(&ihost->nr_scans);
448 }
Mike Christie6eabafb2008-01-31 13:36:43 -0600449}
Mike Christie45ab33b2008-03-04 13:26:55 -0600450
451/**
452 * iscsi_unblock_session - set a session as logged in and start IO.
453 * @session: iscsi session
454 *
455 * Mark a session as ready to accept IO.
456 */
457void iscsi_unblock_session(struct iscsi_cls_session *session)
458{
459 queue_work(iscsi_eh_timer_workq, &session->unblock_work);
460 /*
461 * make sure all the events have completed before tell the driver
462 * it is safe
463 */
464 flush_workqueue(iscsi_eh_timer_workq);
465}
Mike Christie30a6c652006-04-06 21:13:39 -0500466EXPORT_SYMBOL_GPL(iscsi_unblock_session);
467
Mike Christie45ab33b2008-03-04 13:26:55 -0600468static void __iscsi_block_session(struct work_struct *work)
Mike Christie30a6c652006-04-06 21:13:39 -0500469{
Mike Christie45ab33b2008-03-04 13:26:55 -0600470 struct iscsi_cls_session *session =
471 container_of(work, struct iscsi_cls_session,
472 block_work);
Mike Christie6eabafb2008-01-31 13:36:43 -0600473 unsigned long flags;
474
475 spin_lock_irqsave(&session->lock, flags);
476 session->state = ISCSI_SESSION_FAILED;
477 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie30a6c652006-04-06 21:13:39 -0500478 scsi_target_block(&session->dev);
Mike Christied8bf5412007-12-13 12:43:27 -0600479 queue_delayed_work(iscsi_eh_timer_workq, &session->recovery_work,
480 session->recovery_tmo * HZ);
Mike Christie30a6c652006-04-06 21:13:39 -0500481}
Mike Christie45ab33b2008-03-04 13:26:55 -0600482
483void iscsi_block_session(struct iscsi_cls_session *session)
484{
485 queue_work(iscsi_eh_timer_workq, &session->block_work);
486}
Mike Christie30a6c652006-04-06 21:13:39 -0500487EXPORT_SYMBOL_GPL(iscsi_block_session);
488
Mike Christie26974782007-12-13 12:43:29 -0600489static void __iscsi_unbind_session(struct work_struct *work)
490{
491 struct iscsi_cls_session *session =
492 container_of(work, struct iscsi_cls_session,
493 unbind_work);
494 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500495 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie79706342008-05-21 15:54:12 -0500496 unsigned long flags;
Mike Christie26974782007-12-13 12:43:29 -0600497
498 /* Prevent new scans and make sure scanning is not in progress */
499 mutex_lock(&ihost->mutex);
Mike Christie79706342008-05-21 15:54:12 -0500500 spin_lock_irqsave(&session->lock, flags);
501 if (session->target_id == ISCSI_MAX_TARGET) {
502 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600503 mutex_unlock(&ihost->mutex);
504 return;
505 }
Mike Christie79706342008-05-21 15:54:12 -0500506 session->target_id = ISCSI_MAX_TARGET;
507 spin_unlock_irqrestore(&session->lock, flags);
Mike Christie26974782007-12-13 12:43:29 -0600508 mutex_unlock(&ihost->mutex);
509
510 scsi_remove_target(&session->dev);
511 iscsi_session_event(session, ISCSI_KEVENT_UNBIND_SESSION);
512}
513
514static int iscsi_unbind_session(struct iscsi_cls_session *session)
515{
516 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500517 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600518
Mike Christiebd976f62008-01-31 13:36:46 -0600519 return queue_work(ihost->scan_workq, &session->unbind_work);
Mike Christie26974782007-12-13 12:43:29 -0600520}
521
Mike Christie7b8631b2006-01-13 18:05:50 -0600522struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500523iscsi_alloc_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
524 int dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600525{
526 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Mike Christie5d91e202008-05-21 15:54:01 -0500528 session = kzalloc(sizeof(*session) + dd_size,
Mike Christieb5c7a122006-04-06 21:13:33 -0500529 GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600530 if (!session)
Mike Christief53a88d2006-06-28 12:00:27 -0500531 return NULL;
532
Mike Christie7b8631b2006-01-13 18:05:50 -0600533 session->transport = transport;
Mike Christie30a6c652006-04-06 21:13:39 -0500534 session->recovery_tmo = 120;
Mike Christie6eabafb2008-01-31 13:36:43 -0600535 session->state = ISCSI_SESSION_FREE;
David Howellsc4028952006-11-22 14:57:56 +0000536 INIT_DELAYED_WORK(&session->recovery_work, session_recovery_timedout);
Mike Christie30a6c652006-04-06 21:13:39 -0500537 INIT_LIST_HEAD(&session->sess_list);
Mike Christie45ab33b2008-03-04 13:26:55 -0600538 INIT_WORK(&session->unblock_work, __iscsi_unblock_session);
539 INIT_WORK(&session->block_work, __iscsi_block_session);
Mike Christie26974782007-12-13 12:43:29 -0600540 INIT_WORK(&session->unbind_work, __iscsi_unbind_session);
Mike Christiebd976f62008-01-31 13:36:46 -0600541 INIT_WORK(&session->scan_work, iscsi_scan_session);
Mike Christie6eabafb2008-01-31 13:36:43 -0600542 spin_lock_init(&session->lock);
Mike Christie7b8631b2006-01-13 18:05:50 -0600543
Mike Christie6a8a0d32006-06-28 12:00:31 -0500544 /* this is released in the dev's release function */
545 scsi_host_get(shost);
Mike Christie8434aa82006-06-28 12:00:30 -0500546 session->dev.parent = &shost->shost_gendev;
547 session->dev.release = iscsi_session_release;
548 device_initialize(&session->dev);
Mike Christie5d91e202008-05-21 15:54:01 -0500549 if (dd_size)
Mike Christieb5c7a122006-04-06 21:13:33 -0500550 session->dd_data = &session[1];
Mike Christie8434aa82006-06-28 12:00:30 -0500551 return session;
552}
553EXPORT_SYMBOL_GPL(iscsi_alloc_session);
554
Mike Christie79706342008-05-21 15:54:12 -0500555static int iscsi_get_next_target_id(struct device *dev, void *data)
556{
557 struct iscsi_cls_session *session;
558 unsigned long flags;
559 int err = 0;
560
561 if (!iscsi_is_session_dev(dev))
562 return 0;
563
564 session = iscsi_dev_to_session(dev);
565 spin_lock_irqsave(&session->lock, flags);
566 if (*((unsigned int *) data) == session->target_id)
567 err = -EEXIST;
568 spin_unlock_irqrestore(&session->lock, flags);
569 return err;
570}
571
Mike Christie6a8a0d32006-06-28 12:00:31 -0500572int iscsi_add_session(struct iscsi_cls_session *session, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500573{
574 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500575 struct iscsi_cls_host *ihost;
Mike Christie26974782007-12-13 12:43:29 -0600576 unsigned long flags;
Mike Christie79706342008-05-21 15:54:12 -0500577 unsigned int id = target_id;
Mike Christie8434aa82006-06-28 12:00:30 -0500578 int err;
Mike Christieb5c7a122006-04-06 21:13:33 -0500579
Mike Christie30a6c652006-04-06 21:13:39 -0500580 ihost = shost->shost_data;
Mike Christie41be1442007-02-28 17:32:18 -0600581 session->sid = atomic_add_return(1, &iscsi_session_nr);
Mike Christie79706342008-05-21 15:54:12 -0500582
583 if (id == ISCSI_MAX_TARGET) {
584 for (id = 0; id < ISCSI_MAX_TARGET; id++) {
585 err = device_for_each_child(&shost->shost_gendev, &id,
586 iscsi_get_next_target_id);
587 if (!err)
588 break;
589 }
590
591 if (id == ISCSI_MAX_TARGET) {
592 iscsi_cls_session_printk(KERN_ERR, session,
593 "Too many iscsi targets. Max "
594 "number of targets is %d.\n",
595 ISCSI_MAX_TARGET - 1);
596 goto release_host;
597 }
598 }
599 session->target_id = id;
Mike Christie30a6c652006-04-06 21:13:39 -0500600
Mike Christieb5c7a122006-04-06 21:13:33 -0500601 snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
602 session->sid);
Mike Christie8434aa82006-06-28 12:00:30 -0500603 err = device_add(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600604 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600605 iscsi_cls_session_printk(KERN_ERR, session,
606 "could not register session's dev\n");
Mike Christie8434aa82006-06-28 12:00:30 -0500607 goto release_host;
Mike Christie7b8631b2006-01-13 18:05:50 -0600608 }
609 transport_register_device(&session->dev);
610
Mike Christie26974782007-12-13 12:43:29 -0600611 spin_lock_irqsave(&sesslock, flags);
612 list_add(&session->sess_list, &sesslist);
613 spin_unlock_irqrestore(&sesslock, flags);
614
Mike Christie26974782007-12-13 12:43:29 -0600615 iscsi_session_event(session, ISCSI_KEVENT_CREATE_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500616 return 0;
Mike Christie30a6c652006-04-06 21:13:39 -0500617
Mike Christie8434aa82006-06-28 12:00:30 -0500618release_host:
619 scsi_host_put(shost);
620 return err;
Mike Christie7b8631b2006-01-13 18:05:50 -0600621}
Mike Christie8434aa82006-06-28 12:00:30 -0500622EXPORT_SYMBOL_GPL(iscsi_add_session);
Mike Christie7b8631b2006-01-13 18:05:50 -0600623
624/**
Mike Christie8434aa82006-06-28 12:00:30 -0500625 * iscsi_create_session - create iscsi class session
626 * @shost: scsi host
627 * @transport: iscsi transport
Mike Christie5d91e202008-05-21 15:54:01 -0500628 * @dd_size: private driver data size
Rob Landleyeb448202007-11-03 13:30:39 -0500629 * @target_id: which target
Mike Christie7b8631b2006-01-13 18:05:50 -0600630 *
Mike Christie8434aa82006-06-28 12:00:30 -0500631 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500632 */
Mike Christie8434aa82006-06-28 12:00:30 -0500633struct iscsi_cls_session *
Mike Christie5d91e202008-05-21 15:54:01 -0500634iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport,
635 int dd_size, unsigned int target_id)
Mike Christie8434aa82006-06-28 12:00:30 -0500636{
637 struct iscsi_cls_session *session;
638
Mike Christie5d91e202008-05-21 15:54:01 -0500639 session = iscsi_alloc_session(shost, transport, dd_size);
Mike Christie8434aa82006-06-28 12:00:30 -0500640 if (!session)
641 return NULL;
642
Mike Christie6a8a0d32006-06-28 12:00:31 -0500643 if (iscsi_add_session(session, target_id)) {
Mike Christie8434aa82006-06-28 12:00:30 -0500644 iscsi_free_session(session);
645 return NULL;
646 }
647 return session;
648}
649EXPORT_SYMBOL_GPL(iscsi_create_session);
650
Mike Christie26974782007-12-13 12:43:29 -0600651static void iscsi_conn_release(struct device *dev)
652{
653 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev);
654 struct device *parent = conn->dev.parent;
655
656 kfree(conn);
657 put_device(parent);
658}
659
660static int iscsi_is_conn_dev(const struct device *dev)
661{
662 return dev->release == iscsi_conn_release;
663}
664
665static int iscsi_iter_destroy_conn_fn(struct device *dev, void *data)
666{
667 if (!iscsi_is_conn_dev(dev))
668 return 0;
669 return iscsi_destroy_conn(iscsi_dev_to_conn(dev));
670}
671
Mike Christie8434aa82006-06-28 12:00:30 -0500672void iscsi_remove_session(struct iscsi_cls_session *session)
Mike Christie7b8631b2006-01-13 18:05:50 -0600673{
Mike Christie30a6c652006-04-06 21:13:39 -0500674 struct Scsi_Host *shost = iscsi_session_to_shost(session);
Mike Christie32c6e1b2008-05-21 15:53:58 -0500675 struct iscsi_cls_host *ihost = shost->shost_data;
Mike Christie26974782007-12-13 12:43:29 -0600676 unsigned long flags;
677 int err;
Mike Christie30a6c652006-04-06 21:13:39 -0500678
Mike Christie26974782007-12-13 12:43:29 -0600679 spin_lock_irqsave(&sesslock, flags);
680 list_del(&session->sess_list);
681 spin_unlock_irqrestore(&sesslock, flags);
682
Mike Christie45ab33b2008-03-04 13:26:55 -0600683 /* make sure there are no blocks/unblocks queued */
684 flush_workqueue(iscsi_eh_timer_workq);
685 /* make sure the timedout callout is not running */
686 if (!cancel_delayed_work(&session->recovery_work))
687 flush_workqueue(iscsi_eh_timer_workq);
Mike Christie26974782007-12-13 12:43:29 -0600688 /*
689 * If we are blocked let commands flow again. The lld or iscsi
690 * layer should set up the queuecommand to fail commands.
Mike Christie45ab33b2008-03-04 13:26:55 -0600691 * We assume that LLD will not be calling block/unblock while
692 * removing the session.
Mike Christie26974782007-12-13 12:43:29 -0600693 */
Mike Christie6eabafb2008-01-31 13:36:43 -0600694 spin_lock_irqsave(&session->lock, flags);
695 session->state = ISCSI_SESSION_FREE;
696 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiebd976f62008-01-31 13:36:46 -0600697
Mike Christie45ab33b2008-03-04 13:26:55 -0600698 scsi_target_unblock(&session->dev);
699 /* flush running scans then delete devices */
Mike Christiebd976f62008-01-31 13:36:46 -0600700 flush_workqueue(ihost->scan_workq);
Mike Christie45ab33b2008-03-04 13:26:55 -0600701 __iscsi_unbind_session(&session->unbind_work);
Mike Christie30a6c652006-04-06 21:13:39 -0500702
Mike Christie26974782007-12-13 12:43:29 -0600703 /* hw iscsi may not have removed all connections from session */
704 err = device_for_each_child(&session->dev, NULL,
705 iscsi_iter_destroy_conn_fn);
706 if (err)
Mike Christie322d7392008-01-31 13:36:52 -0600707 iscsi_cls_session_printk(KERN_ERR, session,
708 "Could not delete all connections "
709 "for session. Error %d.\n", err);
Mike Christie8434aa82006-06-28 12:00:30 -0500710
Mike Christie7b8631b2006-01-13 18:05:50 -0600711 transport_unregister_device(&session->dev);
Mike Christie8434aa82006-06-28 12:00:30 -0500712 device_del(&session->dev);
713}
714EXPORT_SYMBOL_GPL(iscsi_remove_session);
715
716void iscsi_free_session(struct iscsi_cls_session *session)
717{
Mike Christie26974782007-12-13 12:43:29 -0600718 iscsi_session_event(session, ISCSI_KEVENT_DESTROY_SESSION);
Mike Christie8434aa82006-06-28 12:00:30 -0500719 put_device(&session->dev);
Mike Christie7b8631b2006-01-13 18:05:50 -0600720}
Mike Christie8434aa82006-06-28 12:00:30 -0500721EXPORT_SYMBOL_GPL(iscsi_free_session);
722
723/**
724 * iscsi_destroy_session - destroy iscsi session
725 * @session: iscsi_session
726 *
727 * Can be called by a LLD or iscsi_transport. There must not be
728 * any running connections.
Rob Landleyeb448202007-11-03 13:30:39 -0500729 */
Mike Christie8434aa82006-06-28 12:00:30 -0500730int iscsi_destroy_session(struct iscsi_cls_session *session)
731{
732 iscsi_remove_session(session);
733 iscsi_free_session(session);
734 return 0;
735}
Mike Christie7b8631b2006-01-13 18:05:50 -0600736EXPORT_SYMBOL_GPL(iscsi_destroy_session);
737
Mike Christie7b8631b2006-01-13 18:05:50 -0600738/**
739 * iscsi_create_conn - create iscsi class connection
740 * @session: iscsi cls session
Mike Christie5d91e202008-05-21 15:54:01 -0500741 * @dd_size: private driver data size
Mike Christie7b8631b2006-01-13 18:05:50 -0600742 * @cid: connection id
743 *
744 * This can be called from a LLD or iscsi_transport. The connection
745 * is child of the session so cid must be unique for all connections
746 * on the session.
Mike Christieb5c7a122006-04-06 21:13:33 -0500747 *
748 * Since we do not support MCS, cid will normally be zero. In some cases
749 * for software iscsi we could be trying to preallocate a connection struct
750 * in which case there could be two connection structs and cid would be
751 * non-zero.
Rob Landleyeb448202007-11-03 13:30:39 -0500752 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600753struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -0500754iscsi_create_conn(struct iscsi_cls_session *session, int dd_size, uint32_t cid)
Mike Christie7b8631b2006-01-13 18:05:50 -0600755{
756 struct iscsi_transport *transport = session->transport;
Mike Christie7b8631b2006-01-13 18:05:50 -0600757 struct iscsi_cls_conn *conn;
Mike Christie26974782007-12-13 12:43:29 -0600758 unsigned long flags;
Mike Christie7b8631b2006-01-13 18:05:50 -0600759 int err;
760
Mike Christie5d91e202008-05-21 15:54:01 -0500761 conn = kzalloc(sizeof(*conn) + dd_size, GFP_KERNEL);
Mike Christie7b8631b2006-01-13 18:05:50 -0600762 if (!conn)
763 return NULL;
Mike Christie5d91e202008-05-21 15:54:01 -0500764 if (dd_size)
Mike Christie7b8631b2006-01-13 18:05:50 -0600765 conn->dd_data = &conn[1];
766
767 INIT_LIST_HEAD(&conn->conn_list);
768 conn->transport = transport;
Mike Christieb5c7a122006-04-06 21:13:33 -0500769 conn->cid = cid;
Mike Christie7b8631b2006-01-13 18:05:50 -0600770
771 /* this is released in the dev's release function */
772 if (!get_device(&session->dev))
Mike Christie43a145a2006-10-16 18:09:38 -0400773 goto free_conn;
Mike Christieb5c7a122006-04-06 21:13:33 -0500774
Mike Christie7b8631b2006-01-13 18:05:50 -0600775 snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
Mike Christieb5c7a122006-04-06 21:13:33 -0500776 session->sid, cid);
Mike Christie7b8631b2006-01-13 18:05:50 -0600777 conn->dev.parent = &session->dev;
778 conn->dev.release = iscsi_conn_release;
779 err = device_register(&conn->dev);
780 if (err) {
Mike Christie322d7392008-01-31 13:36:52 -0600781 iscsi_cls_session_printk(KERN_ERR, session, "could not "
782 "register connection's dev\n");
Mike Christie7b8631b2006-01-13 18:05:50 -0600783 goto release_parent_ref;
784 }
785 transport_register_device(&conn->dev);
Mike Christie26974782007-12-13 12:43:29 -0600786
787 spin_lock_irqsave(&connlock, flags);
788 list_add(&conn->conn_list, &connlist);
789 conn->active = 1;
790 spin_unlock_irqrestore(&connlock, flags);
Mike Christie7b8631b2006-01-13 18:05:50 -0600791 return conn;
792
793release_parent_ref:
794 put_device(&session->dev);
795free_conn:
796 kfree(conn);
797 return NULL;
798}
799
800EXPORT_SYMBOL_GPL(iscsi_create_conn);
801
802/**
803 * iscsi_destroy_conn - destroy iscsi class connection
Rob Landleyeb448202007-11-03 13:30:39 -0500804 * @conn: iscsi cls session
Mike Christie7b8631b2006-01-13 18:05:50 -0600805 *
Mike Christie26974782007-12-13 12:43:29 -0600806 * This can be called from a LLD or iscsi_transport.
Rob Landleyeb448202007-11-03 13:30:39 -0500807 */
Mike Christie7b8631b2006-01-13 18:05:50 -0600808int iscsi_destroy_conn(struct iscsi_cls_conn *conn)
809{
Mike Christie26974782007-12-13 12:43:29 -0600810 unsigned long flags;
811
812 spin_lock_irqsave(&connlock, flags);
813 conn->active = 0;
814 list_del(&conn->conn_list);
815 spin_unlock_irqrestore(&connlock, flags);
816
Mike Christie7b8631b2006-01-13 18:05:50 -0600817 transport_unregister_device(&conn->dev);
818 device_unregister(&conn->dev);
819 return 0;
820}
Mike Christie7b8631b2006-01-13 18:05:50 -0600821EXPORT_SYMBOL_GPL(iscsi_destroy_conn);
822
823/*
Mike Christie7b8631b2006-01-13 18:05:50 -0600824 * iscsi interface functions
825 */
Alex Aizman0896b752005-08-04 19:33:07 -0700826static struct iscsi_internal *
827iscsi_if_transport_lookup(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Alex Aizman0896b752005-08-04 19:33:07 -0700829 struct iscsi_internal *priv;
830 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Alex Aizman0896b752005-08-04 19:33:07 -0700832 spin_lock_irqsave(&iscsi_transport_lock, flags);
833 list_for_each_entry(priv, &iscsi_transports, list) {
834 if (tt == priv->iscsi_transport) {
835 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
836 return priv;
837 }
838 }
839 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
840 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Alex Aizman0896b752005-08-04 19:33:07 -0700843static int
Mike Christie43a145a2006-10-16 18:09:38 -0400844iscsi_broadcast_skb(struct sk_buff *skb, gfp_t gfp)
Mike Christie53cb8a12006-06-28 12:00:32 -0500845{
Mike Christie53cb8a12006-06-28 12:00:32 -0500846 int rc;
847
Mike Christie9aaa2b42006-07-24 15:47:34 -0500848 rc = netlink_broadcast(nls, skb, 0, 1, gfp);
Mike Christie53cb8a12006-06-28 12:00:32 -0500849 if (rc < 0) {
Mike Christie53cb8a12006-06-28 12:00:32 -0500850 printk(KERN_ERR "iscsi: can not broadcast skb (%d)\n", rc);
851 return rc;
852 }
853
Mike Christie53cb8a12006-06-28 12:00:32 -0500854 return 0;
855}
856
857static int
Mike Christie43a145a2006-10-16 18:09:38 -0400858iscsi_unicast_skb(struct sk_buff *skb, int pid)
Alex Aizman0896b752005-08-04 19:33:07 -0700859{
Alex Aizman0896b752005-08-04 19:33:07 -0700860 int rc;
861
Mike Christie790f39a2006-05-18 20:31:39 -0500862 rc = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
Alex Aizman0896b752005-08-04 19:33:07 -0700863 if (rc < 0) {
Alex Aizman0896b752005-08-04 19:33:07 -0700864 printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc);
865 return rc;
866 }
867
Alex Aizman0896b752005-08-04 19:33:07 -0700868 return 0;
869}
870
Mike Christie7b7232f2006-02-01 21:06:49 -0600871int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
Alex Aizman0896b752005-08-04 19:33:07 -0700872 char *data, uint32_t data_size)
873{
874 struct nlmsghdr *nlh;
875 struct sk_buff *skb;
876 struct iscsi_uevent *ev;
Alex Aizman0896b752005-08-04 19:33:07 -0700877 char *pdu;
Mike Christie790f39a2006-05-18 20:31:39 -0500878 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700879 int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) +
880 data_size);
881
Mike Christie790f39a2006-05-18 20:31:39 -0500882 priv = iscsi_if_transport_lookup(conn->transport);
883 if (!priv)
884 return -EINVAL;
885
Mike Christie43a145a2006-10-16 18:09:38 -0400886 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700887 if (!skb) {
Mike Christie7b7232f2006-02-01 21:06:49 -0600888 iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie322d7392008-01-31 13:36:52 -0600889 iscsi_cls_conn_printk(KERN_ERR, conn, "can not deliver "
890 "control PDU: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700891 return -ENOMEM;
892 }
893
Mike Christie790f39a2006-05-18 20:31:39 -0500894 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700895 ev = NLMSG_DATA(nlh);
896 memset(ev, 0, sizeof(*ev));
897 ev->transport_handle = iscsi_handle(conn->transport);
898 ev->type = ISCSI_KEVENT_RECV_PDU;
Mike Christieb5c7a122006-04-06 21:13:33 -0500899 ev->r.recv_req.cid = conn->cid;
900 ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700901 pdu = (char*)ev + sizeof(*ev);
902 memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
903 memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
904
Mike Christie43a145a2006-10-16 18:09:38 -0400905 return iscsi_unicast_skb(skb, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700906}
907EXPORT_SYMBOL_GPL(iscsi_recv_pdu);
908
Mike Christie7b7232f2006-02-01 21:06:49 -0600909void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
Alex Aizman0896b752005-08-04 19:33:07 -0700910{
911 struct nlmsghdr *nlh;
912 struct sk_buff *skb;
913 struct iscsi_uevent *ev;
Mike Christie790f39a2006-05-18 20:31:39 -0500914 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700915 int len = NLMSG_SPACE(sizeof(*ev));
916
Mike Christie790f39a2006-05-18 20:31:39 -0500917 priv = iscsi_if_transport_lookup(conn->transport);
918 if (!priv)
919 return;
920
Mike Christie43a145a2006-10-16 18:09:38 -0400921 skb = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700922 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -0600923 iscsi_cls_conn_printk(KERN_ERR, conn, "gracefully ignored "
924 "conn error (%d)\n", error);
Alex Aizman0896b752005-08-04 19:33:07 -0700925 return;
926 }
927
Mike Christie790f39a2006-05-18 20:31:39 -0500928 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
Alex Aizman0896b752005-08-04 19:33:07 -0700929 ev = NLMSG_DATA(nlh);
930 ev->transport_handle = iscsi_handle(conn->transport);
931 ev->type = ISCSI_KEVENT_CONN_ERROR;
Alex Aizman0896b752005-08-04 19:33:07 -0700932 ev->r.connerror.error = error;
Mike Christieb5c7a122006-04-06 21:13:33 -0500933 ev->r.connerror.cid = conn->cid;
934 ev->r.connerror.sid = iscsi_conn_get_sid(conn);
Alex Aizman0896b752005-08-04 19:33:07 -0700935
Mike Christie43a145a2006-10-16 18:09:38 -0400936 iscsi_broadcast_skb(skb, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700937
Mike Christie322d7392008-01-31 13:36:52 -0600938 iscsi_cls_conn_printk(KERN_INFO, conn, "detected conn error (%d)\n",
939 error);
Alex Aizman0896b752005-08-04 19:33:07 -0700940}
941EXPORT_SYMBOL_GPL(iscsi_conn_error);
942
943static int
944iscsi_if_send_reply(int pid, int seq, int type, int done, int multi,
945 void *payload, int size)
946{
947 struct sk_buff *skb;
948 struct nlmsghdr *nlh;
949 int len = NLMSG_SPACE(size);
950 int flags = multi ? NLM_F_MULTI : 0;
951 int t = done ? NLMSG_DONE : type;
952
Mike Christie43a145a2006-10-16 18:09:38 -0400953 skb = alloc_skb(len, GFP_ATOMIC);
Mike Christie239a7dc2007-05-30 12:57:07 -0500954 if (!skb) {
955 printk(KERN_ERR "Could not allocate skb to send reply.\n");
956 return -ENOMEM;
957 }
Alex Aizman0896b752005-08-04 19:33:07 -0700958
959 nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0);
960 nlh->nlmsg_flags = flags;
961 memcpy(NLMSG_DATA(nlh), payload, size);
Mike Christie43a145a2006-10-16 18:09:38 -0400962 return iscsi_unicast_skb(skb, pid);
Alex Aizman0896b752005-08-04 19:33:07 -0700963}
964
965static int
Mike Christie5b940ad2006-02-01 21:06:56 -0600966iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
Alex Aizman0896b752005-08-04 19:33:07 -0700967{
968 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
969 struct iscsi_stats *stats;
970 struct sk_buff *skbstat;
Mike Christie7b8631b2006-01-13 18:05:50 -0600971 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -0700972 struct nlmsghdr *nlhstat;
973 struct iscsi_uevent *evstat;
Mike Christie790f39a2006-05-18 20:31:39 -0500974 struct iscsi_internal *priv;
Alex Aizman0896b752005-08-04 19:33:07 -0700975 int len = NLMSG_SPACE(sizeof(*ev) +
976 sizeof(struct iscsi_stats) +
977 sizeof(struct iscsi_stats_custom) *
978 ISCSI_STATS_CUSTOM_MAX);
979 int err = 0;
980
Mike Christie790f39a2006-05-18 20:31:39 -0500981 priv = iscsi_if_transport_lookup(transport);
982 if (!priv)
983 return -EINVAL;
984
Mike Christieb5c7a122006-04-06 21:13:33 -0500985 conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
Alex Aizman0896b752005-08-04 19:33:07 -0700986 if (!conn)
987 return -EEXIST;
988
989 do {
990 int actual_size;
991
Mike Christie43a145a2006-10-16 18:09:38 -0400992 skbstat = alloc_skb(len, GFP_ATOMIC);
Alex Aizman0896b752005-08-04 19:33:07 -0700993 if (!skbstat) {
Mike Christie322d7392008-01-31 13:36:52 -0600994 iscsi_cls_conn_printk(KERN_ERR, conn, "can not "
995 "deliver stats: OOM\n");
Alex Aizman0896b752005-08-04 19:33:07 -0700996 return -ENOMEM;
997 }
998
Mike Christie790f39a2006-05-18 20:31:39 -0500999 nlhstat = __nlmsg_put(skbstat, priv->daemon_pid, 0, 0,
Alex Aizman0896b752005-08-04 19:33:07 -07001000 (len - sizeof(*nlhstat)), 0);
1001 evstat = NLMSG_DATA(nlhstat);
1002 memset(evstat, 0, sizeof(*evstat));
1003 evstat->transport_handle = iscsi_handle(conn->transport);
1004 evstat->type = nlh->nlmsg_type;
Mike Christieb5c7a122006-04-06 21:13:33 -05001005 evstat->u.get_stats.cid =
1006 ev->u.get_stats.cid;
1007 evstat->u.get_stats.sid =
1008 ev->u.get_stats.sid;
Alex Aizman0896b752005-08-04 19:33:07 -07001009 stats = (struct iscsi_stats *)
1010 ((char*)evstat + sizeof(*evstat));
1011 memset(stats, 0, sizeof(*stats));
1012
Mike Christie7b7232f2006-02-01 21:06:49 -06001013 transport->get_stats(conn, stats);
Alex Aizman0896b752005-08-04 19:33:07 -07001014 actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) +
1015 sizeof(struct iscsi_stats) +
1016 sizeof(struct iscsi_stats_custom) *
1017 stats->custom_length);
1018 actual_size -= sizeof(*nlhstat);
1019 actual_size = NLMSG_LENGTH(actual_size);
Mike Christie5b940ad2006-02-01 21:06:56 -06001020 skb_trim(skbstat, NLMSG_ALIGN(actual_size));
Alex Aizman0896b752005-08-04 19:33:07 -07001021 nlhstat->nlmsg_len = actual_size;
1022
Mike Christie43a145a2006-10-16 18:09:38 -04001023 err = iscsi_unicast_skb(skbstat, priv->daemon_pid);
Alex Aizman0896b752005-08-04 19:33:07 -07001024 } while (err < 0 && err != -ECONNREFUSED);
1025
1026 return err;
1027}
1028
Mike Christie53cb8a12006-06-28 12:00:32 -05001029/**
Mike Christie26974782007-12-13 12:43:29 -06001030 * iscsi_session_event - send session destr. completion event
1031 * @session: iscsi class session
1032 * @event: type of event
Rob Landleyeb448202007-11-03 13:30:39 -05001033 */
Mike Christie26974782007-12-13 12:43:29 -06001034int iscsi_session_event(struct iscsi_cls_session *session,
1035 enum iscsi_uevent_e event)
Mike Christie53cb8a12006-06-28 12:00:32 -05001036{
1037 struct iscsi_internal *priv;
Mike Christie53cb8a12006-06-28 12:00:32 -05001038 struct Scsi_Host *shost;
1039 struct iscsi_uevent *ev;
1040 struct sk_buff *skb;
1041 struct nlmsghdr *nlh;
Mike Christie53cb8a12006-06-28 12:00:32 -05001042 int rc, len = NLMSG_SPACE(sizeof(*ev));
1043
Mike Christie26974782007-12-13 12:43:29 -06001044 priv = iscsi_if_transport_lookup(session->transport);
Mike Christie53cb8a12006-06-28 12:00:32 -05001045 if (!priv)
1046 return -EINVAL;
Mike Christie53cb8a12006-06-28 12:00:32 -05001047 shost = iscsi_session_to_shost(session);
1048
Mike Christie43a145a2006-10-16 18:09:38 -04001049 skb = alloc_skb(len, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001050 if (!skb) {
Mike Christie322d7392008-01-31 13:36:52 -06001051 iscsi_cls_session_printk(KERN_ERR, session,
1052 "Cannot notify userspace of session "
1053 "event %u\n", event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001054 return -ENOMEM;
1055 }
1056
1057 nlh = __nlmsg_put(skb, priv->daemon_pid, 0, 0, (len - sizeof(*nlh)), 0);
1058 ev = NLMSG_DATA(nlh);
Mike Christie26974782007-12-13 12:43:29 -06001059 ev->transport_handle = iscsi_handle(session->transport);
1060
1061 ev->type = event;
1062 switch (event) {
1063 case ISCSI_KEVENT_DESTROY_SESSION:
1064 ev->r.d_session.host_no = shost->host_no;
1065 ev->r.d_session.sid = session->sid;
1066 break;
1067 case ISCSI_KEVENT_CREATE_SESSION:
1068 ev->r.c_session_ret.host_no = shost->host_no;
1069 ev->r.c_session_ret.sid = session->sid;
1070 break;
1071 case ISCSI_KEVENT_UNBIND_SESSION:
1072 ev->r.unbind_session.host_no = shost->host_no;
1073 ev->r.unbind_session.sid = session->sid;
1074 break;
1075 default:
Mike Christie322d7392008-01-31 13:36:52 -06001076 iscsi_cls_session_printk(KERN_ERR, session, "Invalid event "
1077 "%u.\n", event);
Mike Christie26974782007-12-13 12:43:29 -06001078 kfree_skb(skb);
1079 return -EINVAL;
1080 }
Mike Christie53cb8a12006-06-28 12:00:32 -05001081
1082 /*
1083 * this will occur if the daemon is not up, so we just warn
1084 * the user and when the daemon is restarted it will handle it
1085 */
Mike Christie43a145a2006-10-16 18:09:38 -04001086 rc = iscsi_broadcast_skb(skb, GFP_KERNEL);
Mike Christie53cb8a12006-06-28 12:00:32 -05001087 if (rc < 0)
Mike Christie322d7392008-01-31 13:36:52 -06001088 iscsi_cls_session_printk(KERN_ERR, session,
1089 "Cannot notify userspace of session "
1090 "event %u. Check iscsi daemon\n",
1091 event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001092 return rc;
1093}
Mike Christie26974782007-12-13 12:43:29 -06001094EXPORT_SYMBOL_GPL(iscsi_session_event);
Mike Christie53cb8a12006-06-28 12:00:32 -05001095
Alex Aizman0896b752005-08-04 19:33:07 -07001096static int
Mike Christie40753ca2008-05-21 15:53:56 -05001097iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev,
1098 uint32_t host_no, uint32_t initial_cmdsn,
1099 uint16_t cmds_max, uint16_t queue_depth)
Mike Christie7b8631b2006-01-13 18:05:50 -06001100{
1101 struct iscsi_transport *transport = priv->iscsi_transport;
Mike Christie7b7232f2006-02-01 21:06:49 -06001102 struct iscsi_cls_session *session;
Mike Christie40753ca2008-05-21 15:53:56 -05001103 struct Scsi_Host *shost = NULL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001104
Mike Christie40753ca2008-05-21 15:53:56 -05001105 /*
1106 * Software iscsi allocates a host per session, but
1107 * offload drivers (and possibly iser one day) allocate a host per
1108 * hba/nic/rnic. Offload will match a host here, but software will
1109 * return a new hostno after the create_session callback has returned.
1110 */
1111 if (host_no != UINT_MAX) {
1112 shost = scsi_host_lookup(host_no);
1113 if (IS_ERR(shost)) {
1114 printk(KERN_ERR "Could not find host no %u to "
1115 "create session\n", host_no);
1116 return -ENODEV;
1117 }
1118 }
1119
Mike Christie75613522008-05-21 15:53:59 -05001120 session = transport->create_session(shost, cmds_max, queue_depth,
Mike Christie40753ca2008-05-21 15:53:56 -05001121 initial_cmdsn, &host_no);
1122 if (shost)
1123 scsi_host_put(shost);
Mike Christie7b7232f2006-02-01 21:06:49 -06001124 if (!session)
Mike Christie7b8631b2006-01-13 18:05:50 -06001125 return -ENOMEM;
1126
Mike Christie40753ca2008-05-21 15:53:56 -05001127 ev->r.c_session_ret.host_no = host_no;
Mike Christieb5c7a122006-04-06 21:13:33 -05001128 ev->r.c_session_ret.sid = session->sid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001129 return 0;
1130}
1131
1132static int
Mike Christie7b7232f2006-02-01 21:06:49 -06001133iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
Mike Christie7b8631b2006-01-13 18:05:50 -06001134{
Mike Christie7b8631b2006-01-13 18:05:50 -06001135 struct iscsi_cls_conn *conn;
Mike Christie7b7232f2006-02-01 21:06:49 -06001136 struct iscsi_cls_session *session;
Mike Christie7b8631b2006-01-13 18:05:50 -06001137
Mike Christieb5c7a122006-04-06 21:13:33 -05001138 session = iscsi_session_lookup(ev->u.c_conn.sid);
1139 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001140 printk(KERN_ERR "iscsi: invalid session %d.\n",
Mike Christieb5c7a122006-04-06 21:13:33 -05001141 ev->u.c_conn.sid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001142 return -EINVAL;
Mike Christieb5c7a122006-04-06 21:13:33 -05001143 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001144
Mike Christie7b7232f2006-02-01 21:06:49 -06001145 conn = transport->create_conn(session, ev->u.c_conn.cid);
Mike Christieb5c7a122006-04-06 21:13:33 -05001146 if (!conn) {
Mike Christie322d7392008-01-31 13:36:52 -06001147 iscsi_cls_session_printk(KERN_ERR, session,
1148 "couldn't create a new connection.");
Mike Christie7b7232f2006-02-01 21:06:49 -06001149 return -ENOMEM;
Mike Christieb5c7a122006-04-06 21:13:33 -05001150 }
Mike Christie7b8631b2006-01-13 18:05:50 -06001151
Mike Christieb5c7a122006-04-06 21:13:33 -05001152 ev->r.c_conn_ret.sid = session->sid;
1153 ev->r.c_conn_ret.cid = conn->cid;
Mike Christie7b8631b2006-01-13 18:05:50 -06001154 return 0;
Mike Christie7b8631b2006-01-13 18:05:50 -06001155}
1156
1157static int
1158iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1159{
Mike Christie7b8631b2006-01-13 18:05:50 -06001160 struct iscsi_cls_conn *conn;
Mike Christie7b8631b2006-01-13 18:05:50 -06001161
Mike Christieb5c7a122006-04-06 21:13:33 -05001162 conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
Mike Christie7b8631b2006-01-13 18:05:50 -06001163 if (!conn)
Mike Christie7b8631b2006-01-13 18:05:50 -06001164 return -EINVAL;
Mike Christie7b8631b2006-01-13 18:05:50 -06001165
Mike Christie7b8631b2006-01-13 18:05:50 -06001166 if (transport->destroy_conn)
1167 transport->destroy_conn(conn);
Mike Christie7b8631b2006-01-13 18:05:50 -06001168 return 0;
1169}
1170
Mike Christiefd7255f2006-04-06 21:13:36 -05001171static int
1172iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev)
1173{
1174 char *data = (char*)ev + sizeof(*ev);
1175 struct iscsi_cls_conn *conn;
1176 struct iscsi_cls_session *session;
Mike Christiea54a52c2006-06-28 12:00:23 -05001177 int err = 0, value = 0;
Mike Christiefd7255f2006-04-06 21:13:36 -05001178
1179 session = iscsi_session_lookup(ev->u.set_param.sid);
1180 conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
1181 if (!conn || !session)
1182 return -EINVAL;
1183
1184 switch (ev->u.set_param.param) {
Mike Christie30a6c652006-04-06 21:13:39 -05001185 case ISCSI_PARAM_SESS_RECOVERY_TMO:
Mike Christiea54a52c2006-06-28 12:00:23 -05001186 sscanf(data, "%d", &value);
Mike Christie30a6c652006-04-06 21:13:39 -05001187 if (value != 0)
1188 session->recovery_tmo = value;
1189 break;
Mike Christiefd7255f2006-04-06 21:13:36 -05001190 default:
Mike Christiea54a52c2006-06-28 12:00:23 -05001191 err = transport->set_param(conn, ev->u.set_param.param,
1192 data, ev->u.set_param.len);
Mike Christiefd7255f2006-04-06 21:13:36 -05001193 }
1194
1195 return err;
1196}
1197
Mike Christie7b8631b2006-01-13 18:05:50 -06001198static int
Or Gerlitz264faaa2006-05-02 19:46:36 -05001199iscsi_if_transport_ep(struct iscsi_transport *transport,
1200 struct iscsi_uevent *ev, int msg_type)
1201{
1202 struct sockaddr *dst_addr;
1203 int rc = 0;
1204
1205 switch (msg_type) {
1206 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1207 if (!transport->ep_connect)
1208 return -EINVAL;
1209
1210 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
1211 rc = transport->ep_connect(dst_addr,
1212 ev->u.ep_connect.non_blocking,
1213 &ev->r.ep_connect_ret.handle);
1214 break;
1215 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1216 if (!transport->ep_poll)
1217 return -EINVAL;
1218
1219 ev->r.retcode = transport->ep_poll(ev->u.ep_poll.ep_handle,
1220 ev->u.ep_poll.timeout_ms);
1221 break;
1222 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1223 if (!transport->ep_disconnect)
1224 return -EINVAL;
1225
1226 transport->ep_disconnect(ev->u.ep_disconnect.ep_handle);
1227 break;
1228 }
1229 return rc;
1230}
1231
1232static int
Mike Christie01cb2252006-06-28 12:00:22 -05001233iscsi_tgt_dscvr(struct iscsi_transport *transport,
1234 struct iscsi_uevent *ev)
1235{
Mike Christie2174a042007-05-30 12:57:10 -05001236 struct Scsi_Host *shost;
Mike Christie01cb2252006-06-28 12:00:22 -05001237 struct sockaddr *dst_addr;
Mike Christie2174a042007-05-30 12:57:10 -05001238 int err;
Mike Christie01cb2252006-06-28 12:00:22 -05001239
1240 if (!transport->tgt_dscvr)
1241 return -EINVAL;
1242
Mike Christie2174a042007-05-30 12:57:10 -05001243 shost = scsi_host_lookup(ev->u.tgt_dscvr.host_no);
1244 if (IS_ERR(shost)) {
1245 printk(KERN_ERR "target discovery could not find host no %u\n",
1246 ev->u.tgt_dscvr.host_no);
1247 return -ENODEV;
1248 }
1249
1250
Mike Christie01cb2252006-06-28 12:00:22 -05001251 dst_addr = (struct sockaddr *)((char*)ev + sizeof(*ev));
Mike Christie2174a042007-05-30 12:57:10 -05001252 err = transport->tgt_dscvr(shost, ev->u.tgt_dscvr.type,
1253 ev->u.tgt_dscvr.enable, dst_addr);
1254 scsi_host_put(shost);
1255 return err;
Mike Christie01cb2252006-06-28 12:00:22 -05001256}
1257
1258static int
Mike Christie1d9bf132007-05-30 12:57:11 -05001259iscsi_set_host_param(struct iscsi_transport *transport,
1260 struct iscsi_uevent *ev)
1261{
1262 char *data = (char*)ev + sizeof(*ev);
1263 struct Scsi_Host *shost;
1264 int err;
1265
1266 if (!transport->set_host_param)
1267 return -ENOSYS;
1268
1269 shost = scsi_host_lookup(ev->u.set_host_param.host_no);
1270 if (IS_ERR(shost)) {
1271 printk(KERN_ERR "set_host_param could not find host no %u\n",
1272 ev->u.set_host_param.host_no);
1273 return -ENODEV;
1274 }
1275
1276 err = transport->set_host_param(shost, ev->u.set_host_param.param,
1277 data, ev->u.set_host_param.len);
1278 scsi_host_put(shost);
1279 return err;
1280}
1281
1282static int
Alex Aizman0896b752005-08-04 19:33:07 -07001283iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1284{
1285 int err = 0;
Mike Christie40753ca2008-05-21 15:53:56 -05001286 uint32_t host_no = UINT_MAX;
Alex Aizman0896b752005-08-04 19:33:07 -07001287 struct iscsi_uevent *ev = NLMSG_DATA(nlh);
1288 struct iscsi_transport *transport = NULL;
1289 struct iscsi_internal *priv;
Mike Christie7b7232f2006-02-01 21:06:49 -06001290 struct iscsi_cls_session *session;
1291 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001292
Alex Aizman0896b752005-08-04 19:33:07 -07001293 priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle));
1294 if (!priv)
1295 return -EINVAL;
1296 transport = priv->iscsi_transport;
1297
Mike Christie7b7232f2006-02-01 21:06:49 -06001298 if (!try_module_get(transport->owner))
1299 return -EINVAL;
1300
Mike Christie790f39a2006-05-18 20:31:39 -05001301 priv->daemon_pid = NETLINK_CREDS(skb)->pid;
1302
Alex Aizman0896b752005-08-04 19:33:07 -07001303 switch (nlh->nlmsg_type) {
1304 case ISCSI_UEVENT_CREATE_SESSION:
Mike Christie40753ca2008-05-21 15:53:56 -05001305 err = iscsi_if_create_session(priv, ev, host_no,
1306 ev->u.c_session.initial_cmdsn,
1307 ev->u.c_session.cmds_max,
1308 ev->u.c_session.queue_depth);
1309 break;
1310 case ISCSI_UEVENT_CREATE_BOUND_SESSION:
1311 err = iscsi_if_create_session(priv, ev,
1312 ev->u.c_bound_session.host_no,
1313 ev->u.c_bound_session.initial_cmdsn,
1314 ev->u.c_bound_session.cmds_max,
1315 ev->u.c_bound_session.queue_depth);
Alex Aizman0896b752005-08-04 19:33:07 -07001316 break;
1317 case ISCSI_UEVENT_DESTROY_SESSION:
Mike Christieb5c7a122006-04-06 21:13:33 -05001318 session = iscsi_session_lookup(ev->u.d_session.sid);
Mike Christie26974782007-12-13 12:43:29 -06001319 if (session)
Mike Christie7b7232f2006-02-01 21:06:49 -06001320 transport->destroy_session(session);
Mike Christie26974782007-12-13 12:43:29 -06001321 else
1322 err = -EINVAL;
1323 break;
1324 case ISCSI_UEVENT_UNBIND_SESSION:
1325 session = iscsi_session_lookup(ev->u.d_session.sid);
1326 if (session)
1327 iscsi_unbind_session(session);
1328 else
Mike Christie7b7232f2006-02-01 21:06:49 -06001329 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001330 break;
1331 case ISCSI_UEVENT_CREATE_CONN:
1332 err = iscsi_if_create_conn(transport, ev);
1333 break;
1334 case ISCSI_UEVENT_DESTROY_CONN:
1335 err = iscsi_if_destroy_conn(transport, ev);
1336 break;
1337 case ISCSI_UEVENT_BIND_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001338 session = iscsi_session_lookup(ev->u.b_conn.sid);
1339 conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001340
1341 if (session && conn)
1342 ev->r.retcode = transport->bind_conn(session, conn,
Or Gerlitz264faaa2006-05-02 19:46:36 -05001343 ev->u.b_conn.transport_eph,
Mike Christie7b7232f2006-02-01 21:06:49 -06001344 ev->u.b_conn.is_leading);
1345 else
1346 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001347 break;
1348 case ISCSI_UEVENT_SET_PARAM:
Mike Christiefd7255f2006-04-06 21:13:36 -05001349 err = iscsi_set_param(transport, ev);
Alex Aizman0896b752005-08-04 19:33:07 -07001350 break;
1351 case ISCSI_UEVENT_START_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001352 conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001353 if (conn)
1354 ev->r.retcode = transport->start_conn(conn);
1355 else
1356 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001357 break;
1358 case ISCSI_UEVENT_STOP_CONN:
Mike Christieb5c7a122006-04-06 21:13:33 -05001359 conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001360 if (conn)
1361 transport->stop_conn(conn, ev->u.stop_conn.flag);
1362 else
1363 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001364 break;
1365 case ISCSI_UEVENT_SEND_PDU:
Mike Christieb5c7a122006-04-06 21:13:33 -05001366 conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
Mike Christie7b7232f2006-02-01 21:06:49 -06001367 if (conn)
1368 ev->r.retcode = transport->send_pdu(conn,
1369 (struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
1370 (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size,
1371 ev->u.send_pdu.data_size);
1372 else
1373 err = -EINVAL;
Alex Aizman0896b752005-08-04 19:33:07 -07001374 break;
1375 case ISCSI_UEVENT_GET_STATS:
Mike Christie5b940ad2006-02-01 21:06:56 -06001376 err = iscsi_if_get_stats(transport, nlh);
Alex Aizman0896b752005-08-04 19:33:07 -07001377 break;
Or Gerlitz264faaa2006-05-02 19:46:36 -05001378 case ISCSI_UEVENT_TRANSPORT_EP_CONNECT:
1379 case ISCSI_UEVENT_TRANSPORT_EP_POLL:
1380 case ISCSI_UEVENT_TRANSPORT_EP_DISCONNECT:
1381 err = iscsi_if_transport_ep(transport, ev, nlh->nlmsg_type);
1382 break;
Mike Christie01cb2252006-06-28 12:00:22 -05001383 case ISCSI_UEVENT_TGT_DSCVR:
1384 err = iscsi_tgt_dscvr(transport, ev);
1385 break;
Mike Christie1d9bf132007-05-30 12:57:11 -05001386 case ISCSI_UEVENT_SET_HOST_PARAM:
1387 err = iscsi_set_host_param(transport, ev);
1388 break;
Alex Aizman0896b752005-08-04 19:33:07 -07001389 default:
Mike Christie1d9bf132007-05-30 12:57:11 -05001390 err = -ENOSYS;
Alex Aizman0896b752005-08-04 19:33:07 -07001391 break;
1392 }
1393
Mike Christie7b7232f2006-02-01 21:06:49 -06001394 module_put(transport->owner);
Alex Aizman0896b752005-08-04 19:33:07 -07001395 return err;
1396}
1397
Mike Christieb5c7a122006-04-06 21:13:33 -05001398/*
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001399 * Get message from skb. Each message is processed by iscsi_if_recv_msg.
1400 * Malformed skbs with wrong lengths or invalid creds are not processed.
Mike Christieb5c7a122006-04-06 21:13:33 -05001401 */
Alex Aizman0896b752005-08-04 19:33:07 -07001402static void
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001403iscsi_if_rx(struct sk_buff *skb)
Alex Aizman0896b752005-08-04 19:33:07 -07001404{
Arjan van de Ven0b950672006-01-11 13:16:10 +01001405 mutex_lock(&rx_queue_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001406 while (skb->len >= NLMSG_SPACE(0)) {
1407 int err;
1408 uint32_t rlen;
1409 struct nlmsghdr *nlh;
1410 struct iscsi_uevent *ev;
1411
1412 nlh = nlmsg_hdr(skb);
1413 if (nlh->nlmsg_len < sizeof(*nlh) ||
1414 skb->len < nlh->nlmsg_len) {
1415 break;
Mike Christieee7f8e42006-02-01 21:07:01 -06001416 }
Mike Christieee7f8e42006-02-01 21:07:01 -06001417
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001418 ev = NLMSG_DATA(nlh);
1419 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1420 if (rlen > skb->len)
1421 rlen = skb->len;
Alex Aizman0896b752005-08-04 19:33:07 -07001422
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001423 err = iscsi_if_recv_msg(skb, nlh);
1424 if (err) {
1425 ev->type = ISCSI_KEVENT_IF_ERROR;
1426 ev->iferror = err;
1427 }
1428 do {
1429 /*
1430 * special case for GET_STATS:
1431 * on success - sending reply and stats from
1432 * inside of if_recv_msg(),
1433 * on error - fall through.
1434 */
1435 if (ev->type == ISCSI_UEVENT_GET_STATS && !err)
Alex Aizman0896b752005-08-04 19:33:07 -07001436 break;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001437 err = iscsi_if_send_reply(
1438 NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq,
1439 nlh->nlmsg_type, 0, 0, ev, sizeof(*ev));
1440 } while (err < 0 && err != -ECONNREFUSED);
1441 skb_pull(skb, rlen);
Alex Aizman0896b752005-08-04 19:33:07 -07001442 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001443 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001444}
1445
Mike Christiefd7255f2006-04-06 21:13:36 -05001446#define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \
Tony Jonesee959b02008-02-22 00:13:36 +01001447struct device_attribute dev_attr_##_prefix##_##_name = \
Mike Christiefd7255f2006-04-06 21:13:36 -05001448 __ATTR(_name,_mode,_show,_store)
1449
Alex Aizman0896b752005-08-04 19:33:07 -07001450/*
1451 * iSCSI connection attrs
1452 */
Mike Christiea54a52c2006-06-28 12:00:23 -05001453#define iscsi_conn_attr_show(param) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001455show_conn_param_##param(struct device *dev, \
1456 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001457{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001458 struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001459 struct iscsi_transport *t = conn->transport; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001460 return t->get_conn_param(conn, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001461}
1462
Mike Christiea54a52c2006-06-28 12:00:23 -05001463#define iscsi_conn_attr(field, param) \
1464 iscsi_conn_attr_show(param) \
1465static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001466 NULL);
1467
Mike Christiea54a52c2006-06-28 12:00:23 -05001468iscsi_conn_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH);
1469iscsi_conn_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH);
1470iscsi_conn_attr(header_digest, ISCSI_PARAM_HDRDGST_EN);
1471iscsi_conn_attr(data_digest, ISCSI_PARAM_DATADGST_EN);
1472iscsi_conn_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN);
1473iscsi_conn_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN);
1474iscsi_conn_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT);
1475iscsi_conn_attr(port, ISCSI_PARAM_CONN_PORT);
1476iscsi_conn_attr(exp_statsn, ISCSI_PARAM_EXP_STATSN);
1477iscsi_conn_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS);
1478iscsi_conn_attr(address, ISCSI_PARAM_CONN_ADDRESS);
Mike Christief6d51802007-12-13 12:43:30 -06001479iscsi_conn_attr(ping_tmo, ISCSI_PARAM_PING_TMO);
1480iscsi_conn_attr(recv_tmo, ISCSI_PARAM_RECV_TMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482/*
Alex Aizman0896b752005-08-04 19:33:07 -07001483 * iSCSI session attrs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 */
Mike Christieb2c64162007-05-30 12:57:16 -05001485#define iscsi_session_attr_show(param, perm) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001487show_session_param_##param(struct device *dev, \
1488 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001489{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001490 struct iscsi_cls_session *session = \
1491 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001492 struct iscsi_transport *t = session->transport; \
Mike Christieb2c64162007-05-30 12:57:16 -05001493 \
1494 if (perm && !capable(CAP_SYS_ADMIN)) \
1495 return -EACCES; \
Mike Christiea54a52c2006-06-28 12:00:23 -05001496 return t->get_session_param(session, param, buf); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Mike Christieb2c64162007-05-30 12:57:16 -05001499#define iscsi_session_attr(field, param, perm) \
1500 iscsi_session_attr_show(param, perm) \
Mike Christiea54a52c2006-06-28 12:00:23 -05001501static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_param_##param, \
Mike Christiefd7255f2006-04-06 21:13:36 -05001502 NULL);
1503
Mike Christieb2c64162007-05-30 12:57:16 -05001504iscsi_session_attr(targetname, ISCSI_PARAM_TARGET_NAME, 0);
1505iscsi_session_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, 0);
1506iscsi_session_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, 0);
1507iscsi_session_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, 0);
1508iscsi_session_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, 0);
1509iscsi_session_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, 0);
1510iscsi_session_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, 0);
1511iscsi_session_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, 0);
1512iscsi_session_attr(erl, ISCSI_PARAM_ERL, 0);
1513iscsi_session_attr(tpgt, ISCSI_PARAM_TPGT, 0);
1514iscsi_session_attr(username, ISCSI_PARAM_USERNAME, 1);
1515iscsi_session_attr(username_in, ISCSI_PARAM_USERNAME_IN, 1);
1516iscsi_session_attr(password, ISCSI_PARAM_PASSWORD, 1);
1517iscsi_session_attr(password_in, ISCSI_PARAM_PASSWORD_IN, 1);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001518iscsi_session_attr(fast_abort, ISCSI_PARAM_FAST_ABORT, 0);
1519iscsi_session_attr(abort_tmo, ISCSI_PARAM_ABORT_TMO, 0);
1520iscsi_session_attr(lu_reset_tmo, ISCSI_PARAM_LU_RESET_TMO, 0);
Mike Christiefd7255f2006-04-06 21:13:36 -05001521
Mike Christie6eabafb2008-01-31 13:36:43 -06001522static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +01001523show_priv_session_state(struct device *dev, struct device_attribute *attr,
1524 char *buf)
Mike Christie6eabafb2008-01-31 13:36:43 -06001525{
Tony Jonesee959b02008-02-22 00:13:36 +01001526 struct iscsi_cls_session *session = iscsi_dev_to_session(dev->parent);
Mike Christie6eabafb2008-01-31 13:36:43 -06001527 return sprintf(buf, "%s\n", iscsi_session_state_name(session->state));
1528}
1529static ISCSI_CLASS_ATTR(priv_sess, state, S_IRUGO, show_priv_session_state,
1530 NULL);
1531
Mike Christiefd7255f2006-04-06 21:13:36 -05001532#define iscsi_priv_session_attr_show(field, format) \
1533static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001534show_priv_session_##field(struct device *dev, \
1535 struct device_attribute *attr, char *buf) \
Mike Christiefd7255f2006-04-06 21:13:36 -05001536{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001537 struct iscsi_cls_session *session = \
1538 iscsi_dev_to_session(dev->parent); \
Mike Christiefd7255f2006-04-06 21:13:36 -05001539 return sprintf(buf, format"\n", session->field); \
1540}
1541
1542#define iscsi_priv_session_attr(field, format) \
1543 iscsi_priv_session_attr_show(field, format) \
1544static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \
1545 NULL)
Mike Christie30a6c652006-04-06 21:13:39 -05001546iscsi_priv_session_attr(recovery_tmo, "%d");
Mike Christiefd7255f2006-04-06 21:13:36 -05001547
Mike Christie1819dc82007-05-30 12:57:08 -05001548/*
1549 * iSCSI host attrs
1550 */
1551#define iscsi_host_attr_show(param) \
1552static ssize_t \
Tony Jonesee959b02008-02-22 00:13:36 +01001553show_host_param_##param(struct device *dev, \
1554 struct device_attribute *attr, char *buf) \
Mike Christie1819dc82007-05-30 12:57:08 -05001555{ \
Tony Jonesee959b02008-02-22 00:13:36 +01001556 struct Scsi_Host *shost = transport_class_to_shost(dev); \
Mike Christie1819dc82007-05-30 12:57:08 -05001557 struct iscsi_internal *priv = to_iscsi_internal(shost->transportt); \
1558 return priv->iscsi_transport->get_host_param(shost, param, buf); \
1559}
1560
1561#define iscsi_host_attr(field, param) \
1562 iscsi_host_attr_show(param) \
1563static ISCSI_CLASS_ATTR(host, field, S_IRUGO, show_host_param_##param, \
1564 NULL);
1565
Mike Christied8196ed2007-05-30 12:57:25 -05001566iscsi_host_attr(netdev, ISCSI_HOST_PARAM_NETDEV_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001567iscsi_host_attr(hwaddress, ISCSI_HOST_PARAM_HWADDRESS);
Mike Christied8196ed2007-05-30 12:57:25 -05001568iscsi_host_attr(ipaddress, ISCSI_HOST_PARAM_IPADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001569iscsi_host_attr(initiatorname, ISCSI_HOST_PARAM_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001570
Mike Christiefd7255f2006-04-06 21:13:36 -05001571#define SETUP_PRIV_SESSION_RD_ATTR(field) \
1572do { \
Tony Jonesee959b02008-02-22 00:13:36 +01001573 priv->session_attrs[count] = &dev_attr_priv_sess_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001574 count++; \
1575} while (0)
1576
Mike Christiea54a52c2006-06-28 12:00:23 -05001577
Mike Christiefd7255f2006-04-06 21:13:36 -05001578#define SETUP_SESSION_RD_ATTR(field, param_flag) \
1579do { \
1580 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001581 priv->session_attrs[count] = &dev_attr_sess_##field; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 count++; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001583 } \
1584} while (0)
1585
Mike Christiefd7255f2006-04-06 21:13:36 -05001586#define SETUP_CONN_RD_ATTR(field, param_flag) \
1587do { \
1588 if (tt->param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001589 priv->conn_attrs[count] = &dev_attr_conn_##field; \
Mike Christiefd7255f2006-04-06 21:13:36 -05001590 count++; \
1591 } \
1592} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Mike Christie1819dc82007-05-30 12:57:08 -05001594#define SETUP_HOST_RD_ATTR(field, param_flag) \
1595do { \
1596 if (tt->host_param_mask & param_flag) { \
Tony Jonesee959b02008-02-22 00:13:36 +01001597 priv->host_attrs[count] = &dev_attr_host_##field; \
Mike Christie1819dc82007-05-30 12:57:08 -05001598 count++; \
1599 } \
1600} while (0)
1601
Alex Aizman0896b752005-08-04 19:33:07 -07001602static int iscsi_session_match(struct attribute_container *cont,
1603 struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Mike Christie7b8631b2006-01-13 18:05:50 -06001605 struct iscsi_cls_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct Scsi_Host *shost;
Alex Aizman0896b752005-08-04 19:33:07 -07001607 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Alex Aizman0896b752005-08-04 19:33:07 -07001609 if (!iscsi_is_session_dev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return 0;
1611
Mike Christie7b8631b2006-01-13 18:05:50 -06001612 session = iscsi_dev_to_session(dev);
1613 shost = iscsi_session_to_shost(session);
Alex Aizman0896b752005-08-04 19:33:07 -07001614 if (!shost->transportt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616
Alex Aizman0896b752005-08-04 19:33:07 -07001617 priv = to_iscsi_internal(shost->transportt);
1618 if (priv->session_cont.ac.class != &iscsi_session_class.class)
1619 return 0;
1620
1621 return &priv->session_cont.ac == cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
Alex Aizman0896b752005-08-04 19:33:07 -07001624static int iscsi_conn_match(struct attribute_container *cont,
1625 struct device *dev)
1626{
Mike Christie7b8631b2006-01-13 18:05:50 -06001627 struct iscsi_cls_session *session;
1628 struct iscsi_cls_conn *conn;
Alex Aizman0896b752005-08-04 19:33:07 -07001629 struct Scsi_Host *shost;
1630 struct iscsi_internal *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Alex Aizman0896b752005-08-04 19:33:07 -07001632 if (!iscsi_is_conn_dev(dev))
1633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Mike Christie7b8631b2006-01-13 18:05:50 -06001635 conn = iscsi_dev_to_conn(dev);
1636 session = iscsi_dev_to_session(conn->dev.parent);
1637 shost = iscsi_session_to_shost(session);
1638
Alex Aizman0896b752005-08-04 19:33:07 -07001639 if (!shost->transportt)
1640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Alex Aizman0896b752005-08-04 19:33:07 -07001642 priv = to_iscsi_internal(shost->transportt);
1643 if (priv->conn_cont.ac.class != &iscsi_connection_class.class)
1644 return 0;
1645
1646 return &priv->conn_cont.ac == cont;
1647}
1648
Mike Christie30a6c652006-04-06 21:13:39 -05001649static int iscsi_host_match(struct attribute_container *cont,
1650 struct device *dev)
1651{
1652 struct Scsi_Host *shost;
1653 struct iscsi_internal *priv;
1654
1655 if (!scsi_is_host_device(dev))
1656 return 0;
1657
1658 shost = dev_to_shost(dev);
1659 if (!shost->transportt ||
1660 shost->transportt->host_attrs.ac.class != &iscsi_host_class.class)
1661 return 0;
1662
1663 priv = to_iscsi_internal(shost->transportt);
1664 return &priv->t.host_attrs.ac == cont;
1665}
1666
Mike Christie7b8631b2006-01-13 18:05:50 -06001667struct scsi_transport_template *
1668iscsi_register_transport(struct iscsi_transport *tt)
Alex Aizman0896b752005-08-04 19:33:07 -07001669{
1670 struct iscsi_internal *priv;
1671 unsigned long flags;
1672 int count = 0, err;
1673
1674 BUG_ON(!tt);
1675
1676 priv = iscsi_if_transport_lookup(tt);
1677 if (priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001678 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001679
Jes Sorensen24669f752006-01-16 10:31:18 -05001680 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Alex Aizman0896b752005-08-04 19:33:07 -07001681 if (!priv)
Mike Christie7b8631b2006-01-13 18:05:50 -06001682 return NULL;
Alex Aizman0896b752005-08-04 19:33:07 -07001683 INIT_LIST_HEAD(&priv->list);
Mike Christie53cb8a12006-06-28 12:00:32 -05001684 priv->daemon_pid = -1;
Alex Aizman0896b752005-08-04 19:33:07 -07001685 priv->iscsi_transport = tt;
Mike Christie30a6c652006-04-06 21:13:39 -05001686 priv->t.user_scan = iscsi_user_scan;
Mike Christiea4804cd2008-05-21 15:54:00 -05001687 if (!(tt->caps & CAP_DATA_PATH_OFFLOAD))
1688 priv->t.create_work_queue = 1;
Alex Aizman0896b752005-08-04 19:33:07 -07001689
Tony Jonesee959b02008-02-22 00:13:36 +01001690 priv->dev.class = &iscsi_transport_class;
1691 snprintf(priv->dev.bus_id, BUS_ID_SIZE, "%s", tt->name);
1692 err = device_register(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001693 if (err)
1694 goto free_priv;
1695
Tony Jonesee959b02008-02-22 00:13:36 +01001696 err = sysfs_create_group(&priv->dev.kobj, &iscsi_transport_group);
Alex Aizman0896b752005-08-04 19:33:07 -07001697 if (err)
Tony Jonesee959b02008-02-22 00:13:36 +01001698 goto unregister_dev;
Alex Aizman0896b752005-08-04 19:33:07 -07001699
Mike Christie30a6c652006-04-06 21:13:39 -05001700 /* host parameters */
1701 priv->t.host_attrs.ac.attrs = &priv->host_attrs[0];
1702 priv->t.host_attrs.ac.class = &iscsi_host_class.class;
1703 priv->t.host_attrs.ac.match = iscsi_host_match;
Mike Christie32c6e1b2008-05-21 15:53:58 -05001704 priv->t.host_size = sizeof(struct iscsi_cls_host);
Mike Christie30a6c652006-04-06 21:13:39 -05001705 transport_container_register(&priv->t.host_attrs);
1706
Mike Christied8196ed2007-05-30 12:57:25 -05001707 SETUP_HOST_RD_ATTR(netdev, ISCSI_HOST_NETDEV_NAME);
Mike Christie22236962007-05-30 12:57:24 -05001708 SETUP_HOST_RD_ATTR(ipaddress, ISCSI_HOST_IPADDRESS);
Mike Christie1819dc82007-05-30 12:57:08 -05001709 SETUP_HOST_RD_ATTR(hwaddress, ISCSI_HOST_HWADDRESS);
Mike Christie8ad57812007-05-30 12:57:13 -05001710 SETUP_HOST_RD_ATTR(initiatorname, ISCSI_HOST_INITIATOR_NAME);
Mike Christie1819dc82007-05-30 12:57:08 -05001711 BUG_ON(count > ISCSI_HOST_ATTRS);
1712 priv->host_attrs[count] = NULL;
1713 count = 0;
1714
Alex Aizman0896b752005-08-04 19:33:07 -07001715 /* connection parameters */
1716 priv->conn_cont.ac.attrs = &priv->conn_attrs[0];
1717 priv->conn_cont.ac.class = &iscsi_connection_class.class;
1718 priv->conn_cont.ac.match = iscsi_conn_match;
1719 transport_container_register(&priv->conn_cont);
1720
Mike Christiefd7255f2006-04-06 21:13:36 -05001721 SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH);
1722 SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH);
1723 SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN);
1724 SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN);
1725 SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN);
1726 SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN);
1727 SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS);
1728 SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT);
Mike Christie8d2860b2006-05-02 19:46:47 -05001729 SETUP_CONN_RD_ATTR(exp_statsn, ISCSI_EXP_STATSN);
Mike Christiea54a52c2006-06-28 12:00:23 -05001730 SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS);
1731 SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT);
Mike Christief6d51802007-12-13 12:43:30 -06001732 SETUP_CONN_RD_ATTR(ping_tmo, ISCSI_PING_TMO);
1733 SETUP_CONN_RD_ATTR(recv_tmo, ISCSI_RECV_TMO);
Alex Aizman0896b752005-08-04 19:33:07 -07001734
1735 BUG_ON(count > ISCSI_CONN_ATTRS);
1736 priv->conn_attrs[count] = NULL;
1737 count = 0;
1738
1739 /* session parameters */
1740 priv->session_cont.ac.attrs = &priv->session_attrs[0];
1741 priv->session_cont.ac.class = &iscsi_session_class.class;
1742 priv->session_cont.ac.match = iscsi_session_match;
1743 transport_container_register(&priv->session_cont);
1744
Mike Christiefd7255f2006-04-06 21:13:36 -05001745 SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN);
1746 SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T);
1747 SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN);
1748 SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST);
1749 SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST);
1750 SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN);
1751 SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN);
1752 SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL);
Mike Christiea54a52c2006-06-28 12:00:23 -05001753 SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME);
1754 SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT);
Mike Christieb2c64162007-05-30 12:57:16 -05001755 SETUP_SESSION_RD_ATTR(password, ISCSI_USERNAME);
1756 SETUP_SESSION_RD_ATTR(password_in, ISCSI_USERNAME_IN);
1757 SETUP_SESSION_RD_ATTR(username, ISCSI_PASSWORD);
1758 SETUP_SESSION_RD_ATTR(username_in, ISCSI_PASSWORD_IN);
Mike Christie843c0a82007-12-13 12:43:20 -06001759 SETUP_SESSION_RD_ATTR(fast_abort, ISCSI_FAST_ABORT);
Mike Christie4cd49ea2007-12-13 12:43:38 -06001760 SETUP_SESSION_RD_ATTR(abort_tmo, ISCSI_ABORT_TMO);
1761 SETUP_SESSION_RD_ATTR(lu_reset_tmo,ISCSI_LU_RESET_TMO);
Mike Christie30a6c652006-04-06 21:13:39 -05001762 SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo);
Mike Christie6eabafb2008-01-31 13:36:43 -06001763 SETUP_PRIV_SESSION_RD_ATTR(state);
Mike Christiefd7255f2006-04-06 21:13:36 -05001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 BUG_ON(count > ISCSI_SESSION_ATTRS);
Alex Aizman0896b752005-08-04 19:33:07 -07001766 priv->session_attrs[count] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Alex Aizman0896b752005-08-04 19:33:07 -07001768 spin_lock_irqsave(&iscsi_transport_lock, flags);
1769 list_add(&priv->list, &iscsi_transports);
1770 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Alex Aizman0896b752005-08-04 19:33:07 -07001772 printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name);
Mike Christie7b8631b2006-01-13 18:05:50 -06001773 return &priv->t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Tony Jonesee959b02008-02-22 00:13:36 +01001775unregister_dev:
1776 device_unregister(&priv->dev);
Alex Aizman0896b752005-08-04 19:33:07 -07001777free_priv:
1778 kfree(priv);
Mike Christie7b8631b2006-01-13 18:05:50 -06001779 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
Alex Aizman0896b752005-08-04 19:33:07 -07001781EXPORT_SYMBOL_GPL(iscsi_register_transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Alex Aizman0896b752005-08-04 19:33:07 -07001783int iscsi_unregister_transport(struct iscsi_transport *tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Alex Aizman0896b752005-08-04 19:33:07 -07001785 struct iscsi_internal *priv;
1786 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Alex Aizman0896b752005-08-04 19:33:07 -07001788 BUG_ON(!tt);
1789
Arjan van de Ven0b950672006-01-11 13:16:10 +01001790 mutex_lock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001791
1792 priv = iscsi_if_transport_lookup(tt);
1793 BUG_ON (!priv);
1794
Alex Aizman0896b752005-08-04 19:33:07 -07001795 spin_lock_irqsave(&iscsi_transport_lock, flags);
1796 list_del(&priv->list);
1797 spin_unlock_irqrestore(&iscsi_transport_lock, flags);
1798
1799 transport_container_unregister(&priv->conn_cont);
1800 transport_container_unregister(&priv->session_cont);
Mike Christie30a6c652006-04-06 21:13:39 -05001801 transport_container_unregister(&priv->t.host_attrs);
Alex Aizman0896b752005-08-04 19:33:07 -07001802
Tony Jonesee959b02008-02-22 00:13:36 +01001803 sysfs_remove_group(&priv->dev.kobj, &iscsi_transport_group);
1804 device_unregister(&priv->dev);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001805 mutex_unlock(&rx_queue_mutex);
Alex Aizman0896b752005-08-04 19:33:07 -07001806
1807 return 0;
1808}
1809EXPORT_SYMBOL_GPL(iscsi_unregister_transport);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811static __init int iscsi_transport_init(void)
1812{
Alex Aizman0896b752005-08-04 19:33:07 -07001813 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Meelis Roos09492602006-12-17 12:10:26 -06001815 printk(KERN_INFO "Loading iSCSI transport class v%s.\n",
Mike Christief4246b32006-07-24 15:47:54 -05001816 ISCSI_TRANSPORT_VERSION);
1817
Mike Christie41be1442007-02-28 17:32:18 -06001818 atomic_set(&iscsi_session_nr, 0);
1819
Alex Aizman0896b752005-08-04 19:33:07 -07001820 err = class_register(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (err)
1822 return err;
Alex Aizman0896b752005-08-04 19:33:07 -07001823
Mike Christie30a6c652006-04-06 21:13:39 -05001824 err = transport_class_register(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001825 if (err)
1826 goto unregister_transport_class;
1827
Mike Christie30a6c652006-04-06 21:13:39 -05001828 err = transport_class_register(&iscsi_connection_class);
1829 if (err)
1830 goto unregister_host_class;
1831
Alex Aizman0896b752005-08-04 19:33:07 -07001832 err = transport_class_register(&iscsi_session_class);
1833 if (err)
1834 goto unregister_conn_class;
1835
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001836 nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, NULL,
Mike Christie7b8631b2006-01-13 18:05:50 -06001837 THIS_MODULE);
Alex Aizman0896b752005-08-04 19:33:07 -07001838 if (!nls) {
1839 err = -ENOBUFS;
Mike Christie43a145a2006-10-16 18:09:38 -04001840 goto unregister_session_class;
Alex Aizman0896b752005-08-04 19:33:07 -07001841 }
1842
Mike Christied8bf5412007-12-13 12:43:27 -06001843 iscsi_eh_timer_workq = create_singlethread_workqueue("iscsi_eh");
1844 if (!iscsi_eh_timer_workq)
1845 goto release_nls;
1846
Mike Christie43a145a2006-10-16 18:09:38 -04001847 return 0;
Alex Aizman0896b752005-08-04 19:33:07 -07001848
Mike Christied8bf5412007-12-13 12:43:27 -06001849release_nls:
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001850 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001851unregister_session_class:
1852 transport_class_unregister(&iscsi_session_class);
1853unregister_conn_class:
1854 transport_class_unregister(&iscsi_connection_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001855unregister_host_class:
1856 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001857unregister_transport_class:
1858 class_unregister(&iscsi_transport_class);
1859 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
1862static void __exit iscsi_transport_exit(void)
1863{
Mike Christied8bf5412007-12-13 12:43:27 -06001864 destroy_workqueue(iscsi_eh_timer_workq);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001865 netlink_kernel_release(nls);
Alex Aizman0896b752005-08-04 19:33:07 -07001866 transport_class_unregister(&iscsi_connection_class);
1867 transport_class_unregister(&iscsi_session_class);
Mike Christie30a6c652006-04-06 21:13:39 -05001868 transport_class_unregister(&iscsi_host_class);
Alex Aizman0896b752005-08-04 19:33:07 -07001869 class_unregister(&iscsi_transport_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872module_init(iscsi_transport_init);
1873module_exit(iscsi_transport_exit);
1874
Alex Aizman0896b752005-08-04 19:33:07 -07001875MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
1876 "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
1877 "Alex Aizman <itn780@yahoo.com>");
1878MODULE_DESCRIPTION("iSCSI Transport Interface");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879MODULE_LICENSE("GPL");
Mike Christief4246b32006-07-24 15:47:54 -05001880MODULE_VERSION(ISCSI_TRANSPORT_VERSION);