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