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> |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 24 | #include <linux/mempool.h> |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 26 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <scsi/scsi.h> |
| 28 | #include <scsi/scsi_host.h> |
| 29 | #include <scsi/scsi_device.h> |
| 30 | #include <scsi/scsi_transport.h> |
| 31 | #include <scsi/scsi_transport_iscsi.h> |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 32 | #include <scsi/iscsi_if.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 34 | #define ISCSI_SESSION_ATTRS 11 |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 35 | #define ISCSI_CONN_ATTRS 10 |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 36 | #define ISCSI_HOST_ATTRS 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | struct iscsi_internal { |
| 39 | struct scsi_transport_template t; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 40 | struct iscsi_transport *iscsi_transport; |
| 41 | struct list_head list; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 42 | struct class_device cdev; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 43 | |
| 44 | struct class_device_attribute *host_attrs[ISCSI_HOST_ATTRS + 1]; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 45 | struct transport_container conn_cont; |
| 46 | struct class_device_attribute *conn_attrs[ISCSI_CONN_ATTRS + 1]; |
| 47 | struct transport_container session_cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 51 | static int iscsi_session_nr; /* sysfs session id for next new session */ |
| 52 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 53 | /* |
| 54 | * list of registered transports and lock that must |
| 55 | * be held while accessing list. The iscsi_transport_lock must |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 56 | * be acquired after the rx_queue_mutex. |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 57 | */ |
| 58 | static LIST_HEAD(iscsi_transports); |
| 59 | static DEFINE_SPINLOCK(iscsi_transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 61 | #define to_iscsi_internal(tmpl) \ |
| 62 | container_of(tmpl, struct iscsi_internal, t) |
| 63 | |
| 64 | #define cdev_to_iscsi_internal(_cdev) \ |
| 65 | container_of(_cdev, struct iscsi_internal, cdev) |
| 66 | |
| 67 | static void iscsi_transport_release(struct class_device *cdev) |
| 68 | { |
| 69 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); |
| 70 | kfree(priv); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * iscsi_transport_class represents the iscsi_transports that are |
| 75 | * registered. |
| 76 | */ |
| 77 | static struct class iscsi_transport_class = { |
| 78 | .name = "iscsi_transport", |
| 79 | .release = iscsi_transport_release, |
| 80 | }; |
| 81 | |
| 82 | static ssize_t |
| 83 | show_transport_handle(struct class_device *cdev, char *buf) |
| 84 | { |
| 85 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); |
Mike Christie | 762e2bf | 2005-09-12 21:01:46 -0500 | [diff] [blame] | 86 | 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] | 87 | } |
| 88 | static CLASS_DEVICE_ATTR(handle, S_IRUGO, show_transport_handle, NULL); |
| 89 | |
| 90 | #define show_transport_attr(name, format) \ |
| 91 | static ssize_t \ |
| 92 | show_transport_##name(struct class_device *cdev, char *buf) \ |
| 93 | { \ |
| 94 | struct iscsi_internal *priv = cdev_to_iscsi_internal(cdev); \ |
| 95 | return sprintf(buf, format"\n", priv->iscsi_transport->name); \ |
| 96 | } \ |
| 97 | static CLASS_DEVICE_ATTR(name, S_IRUGO, show_transport_##name, NULL); |
| 98 | |
| 99 | show_transport_attr(caps, "0x%x"); |
| 100 | show_transport_attr(max_lun, "%d"); |
| 101 | show_transport_attr(max_conn, "%d"); |
| 102 | show_transport_attr(max_cmd_len, "%d"); |
| 103 | |
| 104 | static struct attribute *iscsi_transport_attrs[] = { |
| 105 | &class_device_attr_handle.attr, |
| 106 | &class_device_attr_caps.attr, |
| 107 | &class_device_attr_max_lun.attr, |
| 108 | &class_device_attr_max_conn.attr, |
| 109 | &class_device_attr_max_cmd_len.attr, |
| 110 | NULL, |
| 111 | }; |
| 112 | |
| 113 | static struct attribute_group iscsi_transport_group = { |
| 114 | .attrs = iscsi_transport_attrs, |
| 115 | }; |
| 116 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 117 | static int iscsi_setup_host(struct transport_container *tc, struct device *dev, |
| 118 | struct class_device *cdev) |
| 119 | { |
| 120 | struct Scsi_Host *shost = dev_to_shost(dev); |
| 121 | struct iscsi_host *ihost = shost->shost_data; |
| 122 | |
| 123 | memset(ihost, 0, sizeof(*ihost)); |
| 124 | INIT_LIST_HEAD(&ihost->sessions); |
| 125 | mutex_init(&ihost->mutex); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static DECLARE_TRANSPORT_CLASS(iscsi_host_class, |
| 130 | "iscsi_host", |
| 131 | iscsi_setup_host, |
| 132 | NULL, |
| 133 | NULL); |
| 134 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 135 | static DECLARE_TRANSPORT_CLASS(iscsi_session_class, |
| 136 | "iscsi_session", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | NULL, |
| 138 | NULL, |
| 139 | NULL); |
| 140 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 141 | static DECLARE_TRANSPORT_CLASS(iscsi_connection_class, |
| 142 | "iscsi_connection", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | NULL, |
| 144 | NULL, |
| 145 | NULL); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 146 | |
| 147 | static struct sock *nls; |
| 148 | static int daemon_pid; |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 149 | static DEFINE_MUTEX(rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 150 | |
| 151 | struct mempool_zone { |
| 152 | mempool_t *pool; |
| 153 | atomic_t allocated; |
| 154 | int size; |
| 155 | int hiwat; |
| 156 | struct list_head freequeue; |
| 157 | spinlock_t freelock; |
| 158 | }; |
| 159 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 160 | static struct mempool_zone *z_reply; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | /* |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 163 | * Z_MAX_* - actual mempool size allocated at the mempool_zone_init() time |
| 164 | * Z_HIWAT_* - zone's high watermark when if_error bit will be set to -ENOMEM |
| 165 | * so daemon will notice OOM on NETLINK tranposrt level and will |
| 166 | * be able to predict or change operational behavior |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 168 | #define Z_MAX_REPLY 8 |
| 169 | #define Z_HIWAT_REPLY 6 |
| 170 | #define Z_MAX_PDU 8 |
| 171 | #define Z_HIWAT_PDU 6 |
| 172 | #define Z_MAX_ERROR 16 |
| 173 | #define Z_HIWAT_ERROR 12 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 175 | static LIST_HEAD(sesslist); |
| 176 | static DEFINE_SPINLOCK(sesslock); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 177 | static LIST_HEAD(connlist); |
| 178 | static DEFINE_SPINLOCK(connlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 180 | static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn) |
| 181 | { |
| 182 | struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent); |
| 183 | return sess->sid; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Returns the matching session to a given sid |
| 188 | */ |
| 189 | static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid) |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 190 | { |
| 191 | unsigned long flags; |
| 192 | struct iscsi_cls_session *sess; |
| 193 | |
| 194 | spin_lock_irqsave(&sesslock, flags); |
| 195 | list_for_each_entry(sess, &sesslist, sess_list) { |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 196 | if (sess->sid == sid) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 197 | spin_unlock_irqrestore(&sesslock, flags); |
| 198 | return sess; |
| 199 | } |
| 200 | } |
| 201 | spin_unlock_irqrestore(&sesslock, flags); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 205 | /* |
| 206 | * Returns the matching connection to a given sid / cid tuple |
| 207 | */ |
| 208 | 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] | 209 | { |
| 210 | unsigned long flags; |
| 211 | struct iscsi_cls_conn *conn; |
| 212 | |
| 213 | spin_lock_irqsave(&connlock, flags); |
| 214 | list_for_each_entry(conn, &connlist, conn_list) { |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 215 | if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 216 | spin_unlock_irqrestore(&connlock, flags); |
| 217 | return conn; |
| 218 | } |
| 219 | } |
| 220 | spin_unlock_irqrestore(&connlock, flags); |
| 221 | return NULL; |
| 222 | } |
| 223 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 224 | /* |
| 225 | * The following functions can be used by LLDs that allocate |
| 226 | * their own scsi_hosts or by software iscsi LLDs |
| 227 | */ |
| 228 | static void iscsi_session_release(struct device *dev) |
| 229 | { |
| 230 | struct iscsi_cls_session *session = iscsi_dev_to_session(dev); |
| 231 | struct iscsi_transport *transport = session->transport; |
| 232 | struct Scsi_Host *shost; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 234 | shost = iscsi_session_to_shost(session); |
| 235 | scsi_host_put(shost); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 236 | kfree(session->targetname); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 237 | kfree(session); |
| 238 | module_put(transport->owner); |
| 239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 241 | static int iscsi_is_session_dev(const struct device *dev) |
| 242 | { |
| 243 | return dev->release == iscsi_session_release; |
| 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 246 | static int iscsi_user_scan(struct Scsi_Host *shost, uint channel, |
| 247 | uint id, uint lun) |
| 248 | { |
| 249 | struct iscsi_host *ihost = shost->shost_data; |
| 250 | struct iscsi_cls_session *session; |
| 251 | |
| 252 | mutex_lock(&ihost->mutex); |
| 253 | list_for_each_entry(session, &ihost->sessions, host_list) { |
| 254 | if ((channel == SCAN_WILD_CARD || |
| 255 | channel == session->channel) && |
| 256 | (id == SCAN_WILD_CARD || id == session->target_id)) |
| 257 | scsi_scan_target(&session->dev, session->channel, |
| 258 | session->target_id, lun, 1); |
| 259 | } |
| 260 | mutex_unlock(&ihost->mutex); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static void session_recovery_timedout(void *data) |
| 266 | { |
| 267 | struct iscsi_cls_session *session = data; |
| 268 | |
| 269 | dev_printk(KERN_INFO, &session->dev, "session recovery timed out " |
| 270 | "after %d secs\n", session->recovery_tmo); |
| 271 | |
| 272 | if (session->transport->session_recovery_timedout) |
| 273 | session->transport->session_recovery_timedout(session); |
| 274 | |
| 275 | scsi_target_unblock(&session->dev); |
| 276 | } |
| 277 | |
| 278 | void iscsi_unblock_session(struct iscsi_cls_session *session) |
| 279 | { |
| 280 | if (!cancel_delayed_work(&session->recovery_work)) |
| 281 | flush_scheduled_work(); |
| 282 | scsi_target_unblock(&session->dev); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(iscsi_unblock_session); |
| 285 | |
| 286 | void iscsi_block_session(struct iscsi_cls_session *session) |
| 287 | { |
| 288 | scsi_target_block(&session->dev); |
| 289 | schedule_delayed_work(&session->recovery_work, |
| 290 | session->recovery_tmo * HZ); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(iscsi_block_session); |
| 293 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 294 | /** |
| 295 | * iscsi_create_session - create iscsi class session |
| 296 | * @shost: scsi host |
| 297 | * @transport: iscsi transport |
| 298 | * |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 299 | * This can be called from a LLD or iscsi_transport. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 300 | **/ |
| 301 | struct iscsi_cls_session * |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 302 | iscsi_create_session(struct Scsi_Host *shost, |
| 303 | struct iscsi_transport *transport, int channel) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 304 | { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 305 | struct iscsi_host *ihost; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 306 | struct iscsi_cls_session *session; |
| 307 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 309 | if (!try_module_get(transport->owner)) |
| 310 | return NULL; |
| 311 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 312 | session = kzalloc(sizeof(*session) + transport->sessiondata_size, |
| 313 | GFP_KERNEL); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 314 | if (!session) |
| 315 | goto module_put; |
| 316 | session->transport = transport; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 317 | session->recovery_tmo = 120; |
| 318 | INIT_WORK(&session->recovery_work, session_recovery_timedout, session); |
| 319 | INIT_LIST_HEAD(&session->host_list); |
| 320 | INIT_LIST_HEAD(&session->sess_list); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 321 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 322 | if (transport->sessiondata_size) |
| 323 | session->dd_data = &session[1]; |
| 324 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 325 | /* this is released in the dev's release function */ |
| 326 | scsi_host_get(shost); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 327 | ihost = shost->shost_data; |
| 328 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 329 | session->sid = iscsi_session_nr++; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 330 | session->channel = channel; |
| 331 | session->target_id = ihost->next_target_id++; |
| 332 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 333 | snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", |
| 334 | session->sid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 335 | session->dev.parent = &shost->shost_gendev; |
| 336 | session->dev.release = iscsi_session_release; |
| 337 | err = device_register(&session->dev); |
| 338 | if (err) { |
| 339 | dev_printk(KERN_ERR, &session->dev, "iscsi: could not " |
| 340 | "register session's dev\n"); |
| 341 | goto free_session; |
| 342 | } |
| 343 | transport_register_device(&session->dev); |
| 344 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 345 | mutex_lock(&ihost->mutex); |
| 346 | list_add(&session->host_list, &ihost->sessions); |
| 347 | mutex_unlock(&ihost->mutex); |
| 348 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 349 | return session; |
| 350 | |
| 351 | free_session: |
| 352 | kfree(session); |
| 353 | module_put: |
| 354 | module_put(transport->owner); |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | EXPORT_SYMBOL_GPL(iscsi_create_session); |
| 359 | |
| 360 | /** |
| 361 | * iscsi_destroy_session - destroy iscsi session |
| 362 | * @session: iscsi_session |
| 363 | * |
| 364 | * Can be called by a LLD or iscsi_transport. There must not be |
| 365 | * any running connections. |
| 366 | **/ |
| 367 | int iscsi_destroy_session(struct iscsi_cls_session *session) |
| 368 | { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 369 | struct Scsi_Host *shost = iscsi_session_to_shost(session); |
| 370 | struct iscsi_host *ihost = shost->shost_data; |
| 371 | |
| 372 | if (!cancel_delayed_work(&session->recovery_work)) |
| 373 | flush_scheduled_work(); |
| 374 | |
| 375 | mutex_lock(&ihost->mutex); |
| 376 | list_del(&session->host_list); |
| 377 | mutex_unlock(&ihost->mutex); |
| 378 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 379 | transport_unregister_device(&session->dev); |
| 380 | device_unregister(&session->dev); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | EXPORT_SYMBOL_GPL(iscsi_destroy_session); |
| 385 | |
| 386 | static void iscsi_conn_release(struct device *dev) |
| 387 | { |
| 388 | struct iscsi_cls_conn *conn = iscsi_dev_to_conn(dev); |
| 389 | struct device *parent = conn->dev.parent; |
| 390 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 391 | kfree(conn->persistent_address); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 392 | kfree(conn); |
| 393 | put_device(parent); |
| 394 | } |
| 395 | |
| 396 | static int iscsi_is_conn_dev(const struct device *dev) |
| 397 | { |
| 398 | return dev->release == iscsi_conn_release; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * iscsi_create_conn - create iscsi class connection |
| 403 | * @session: iscsi cls session |
| 404 | * @cid: connection id |
| 405 | * |
| 406 | * This can be called from a LLD or iscsi_transport. The connection |
| 407 | * is child of the session so cid must be unique for all connections |
| 408 | * on the session. |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 409 | * |
| 410 | * Since we do not support MCS, cid will normally be zero. In some cases |
| 411 | * for software iscsi we could be trying to preallocate a connection struct |
| 412 | * in which case there could be two connection structs and cid would be |
| 413 | * non-zero. |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 414 | **/ |
| 415 | struct iscsi_cls_conn * |
| 416 | iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid) |
| 417 | { |
| 418 | struct iscsi_transport *transport = session->transport; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 419 | struct iscsi_cls_conn *conn; |
| 420 | int err; |
| 421 | |
| 422 | conn = kzalloc(sizeof(*conn) + transport->conndata_size, GFP_KERNEL); |
| 423 | if (!conn) |
| 424 | return NULL; |
| 425 | |
| 426 | if (transport->conndata_size) |
| 427 | conn->dd_data = &conn[1]; |
| 428 | |
| 429 | INIT_LIST_HEAD(&conn->conn_list); |
| 430 | conn->transport = transport; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 431 | conn->cid = cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 432 | |
| 433 | /* this is released in the dev's release function */ |
| 434 | if (!get_device(&session->dev)) |
| 435 | goto free_conn; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 436 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 437 | snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u", |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 438 | session->sid, cid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 439 | conn->dev.parent = &session->dev; |
| 440 | conn->dev.release = iscsi_conn_release; |
| 441 | err = device_register(&conn->dev); |
| 442 | if (err) { |
| 443 | dev_printk(KERN_ERR, &conn->dev, "iscsi: could not register " |
| 444 | "connection's dev\n"); |
| 445 | goto release_parent_ref; |
| 446 | } |
| 447 | transport_register_device(&conn->dev); |
| 448 | return conn; |
| 449 | |
| 450 | release_parent_ref: |
| 451 | put_device(&session->dev); |
| 452 | free_conn: |
| 453 | kfree(conn); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | EXPORT_SYMBOL_GPL(iscsi_create_conn); |
| 458 | |
| 459 | /** |
| 460 | * iscsi_destroy_conn - destroy iscsi class connection |
| 461 | * @session: iscsi cls session |
| 462 | * |
| 463 | * This can be called from a LLD or iscsi_transport. |
| 464 | **/ |
| 465 | int iscsi_destroy_conn(struct iscsi_cls_conn *conn) |
| 466 | { |
| 467 | transport_unregister_device(&conn->dev); |
| 468 | device_unregister(&conn->dev); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | EXPORT_SYMBOL_GPL(iscsi_destroy_conn); |
| 473 | |
| 474 | /* |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 475 | * iscsi interface functions |
| 476 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 477 | static struct iscsi_internal * |
| 478 | iscsi_if_transport_lookup(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 480 | struct iscsi_internal *priv; |
| 481 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 483 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 484 | list_for_each_entry(priv, &iscsi_transports, list) { |
| 485 | if (tt == priv->iscsi_transport) { |
| 486 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 487 | return priv; |
| 488 | } |
| 489 | } |
| 490 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 491 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 494 | static inline struct list_head *skb_to_lh(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 496 | return (struct list_head *)&skb->cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 498 | |
| 499 | static void* |
Mike Christie | 28e5554 | 2006-02-01 21:07:11 -0600 | [diff] [blame] | 500 | mempool_zone_alloc_skb(gfp_t gfp_mask, void *pool_data) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 501 | { |
| 502 | struct mempool_zone *zone = pool_data; |
| 503 | |
| 504 | return alloc_skb(zone->size, gfp_mask); |
| 505 | } |
| 506 | |
| 507 | static void |
| 508 | mempool_zone_free_skb(void *element, void *pool_data) |
| 509 | { |
| 510 | kfree_skb(element); |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | mempool_zone_complete(struct mempool_zone *zone) |
| 515 | { |
| 516 | unsigned long flags; |
| 517 | struct list_head *lh, *n; |
| 518 | |
| 519 | spin_lock_irqsave(&zone->freelock, flags); |
| 520 | list_for_each_safe(lh, n, &zone->freequeue) { |
| 521 | struct sk_buff *skb = (struct sk_buff *)((char *)lh - |
| 522 | offsetof(struct sk_buff, cb)); |
| 523 | if (!skb_shared(skb)) { |
| 524 | list_del(skb_to_lh(skb)); |
| 525 | mempool_free(skb, zone->pool); |
| 526 | atomic_dec(&zone->allocated); |
| 527 | } |
| 528 | } |
| 529 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 530 | } |
| 531 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 532 | static struct mempool_zone * |
| 533 | mempool_zone_init(unsigned max, unsigned size, unsigned hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 534 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 535 | struct mempool_zone *zp; |
| 536 | |
| 537 | zp = kzalloc(sizeof(*zp), GFP_KERNEL); |
| 538 | if (!zp) |
| 539 | return NULL; |
| 540 | |
Mike Christie | 142e301 | 2006-02-01 21:06:58 -0600 | [diff] [blame] | 541 | zp->size = size; |
| 542 | zp->hiwat = hiwat; |
| 543 | INIT_LIST_HEAD(&zp->freequeue); |
| 544 | spin_lock_init(&zp->freelock); |
| 545 | atomic_set(&zp->allocated, 0); |
| 546 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 547 | zp->pool = mempool_create(max, mempool_zone_alloc_skb, |
| 548 | mempool_zone_free_skb, zp); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 549 | if (!zp->pool) { |
| 550 | kfree(zp); |
| 551 | return NULL; |
| 552 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 553 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 554 | return zp; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 557 | static void mempool_zone_destroy(struct mempool_zone *zp) |
| 558 | { |
| 559 | mempool_destroy(zp->pool); |
| 560 | kfree(zp); |
| 561 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 562 | |
| 563 | static struct sk_buff* |
| 564 | mempool_zone_get_skb(struct mempool_zone *zone) |
| 565 | { |
| 566 | struct sk_buff *skb; |
| 567 | |
| 568 | skb = mempool_alloc(zone->pool, GFP_ATOMIC); |
| 569 | if (skb) |
| 570 | atomic_inc(&zone->allocated); |
| 571 | return skb; |
| 572 | } |
| 573 | |
| 574 | static int |
| 575 | iscsi_unicast_skb(struct mempool_zone *zone, struct sk_buff *skb) |
| 576 | { |
| 577 | unsigned long flags; |
| 578 | int rc; |
| 579 | |
| 580 | skb_get(skb); |
| 581 | rc = netlink_unicast(nls, skb, daemon_pid, MSG_DONTWAIT); |
| 582 | if (rc < 0) { |
| 583 | mempool_free(skb, zone->pool); |
| 584 | printk(KERN_ERR "iscsi: can not unicast skb (%d)\n", rc); |
| 585 | return rc; |
| 586 | } |
| 587 | |
| 588 | spin_lock_irqsave(&zone->freelock, flags); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 589 | INIT_LIST_HEAD(skb_to_lh(skb)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 590 | list_add(skb_to_lh(skb), &zone->freequeue); |
| 591 | spin_unlock_irqrestore(&zone->freelock, flags); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 596 | 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] | 597 | char *data, uint32_t data_size) |
| 598 | { |
| 599 | struct nlmsghdr *nlh; |
| 600 | struct sk_buff *skb; |
| 601 | struct iscsi_uevent *ev; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 602 | char *pdu; |
| 603 | int len = NLMSG_SPACE(sizeof(*ev) + sizeof(struct iscsi_hdr) + |
| 604 | data_size); |
| 605 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 606 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 607 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 608 | skb = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 609 | if (!skb) { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 610 | iscsi_conn_error(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 611 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not deliver " |
| 612 | "control PDU: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 613 | return -ENOMEM; |
| 614 | } |
| 615 | |
| 616 | nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
| 617 | ev = NLMSG_DATA(nlh); |
| 618 | memset(ev, 0, sizeof(*ev)); |
| 619 | ev->transport_handle = iscsi_handle(conn->transport); |
| 620 | ev->type = ISCSI_KEVENT_RECV_PDU; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 621 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 622 | ev->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 623 | ev->r.recv_req.cid = conn->cid; |
| 624 | ev->r.recv_req.sid = iscsi_conn_get_sid(conn); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 625 | pdu = (char*)ev + sizeof(*ev); |
| 626 | memcpy(pdu, hdr, sizeof(struct iscsi_hdr)); |
| 627 | memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size); |
| 628 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 629 | return iscsi_unicast_skb(conn->z_pdu, skb); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 630 | } |
| 631 | EXPORT_SYMBOL_GPL(iscsi_recv_pdu); |
| 632 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 633 | 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] | 634 | { |
| 635 | struct nlmsghdr *nlh; |
| 636 | struct sk_buff *skb; |
| 637 | struct iscsi_uevent *ev; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 638 | int len = NLMSG_SPACE(sizeof(*ev)); |
| 639 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 640 | mempool_zone_complete(conn->z_error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 641 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 642 | skb = mempool_zone_get_skb(conn->z_error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 643 | if (!skb) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 644 | dev_printk(KERN_ERR, &conn->dev, "iscsi: gracefully ignored " |
| 645 | "conn error (%d)\n", error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
| 649 | nlh = __nlmsg_put(skb, daemon_pid, 0, 0, (len - sizeof(*nlh)), 0); |
| 650 | ev = NLMSG_DATA(nlh); |
| 651 | ev->transport_handle = iscsi_handle(conn->transport); |
| 652 | ev->type = ISCSI_KEVENT_CONN_ERROR; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 653 | if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 654 | ev->iferror = -ENOMEM; |
| 655 | ev->r.connerror.error = error; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 656 | ev->r.connerror.cid = conn->cid; |
| 657 | ev->r.connerror.sid = iscsi_conn_get_sid(conn); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 658 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 659 | iscsi_unicast_skb(conn->z_error, skb); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 660 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 661 | dev_printk(KERN_INFO, &conn->dev, "iscsi: detected conn error (%d)\n", |
| 662 | error); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 663 | } |
| 664 | EXPORT_SYMBOL_GPL(iscsi_conn_error); |
| 665 | |
| 666 | static int |
| 667 | iscsi_if_send_reply(int pid, int seq, int type, int done, int multi, |
| 668 | void *payload, int size) |
| 669 | { |
| 670 | struct sk_buff *skb; |
| 671 | struct nlmsghdr *nlh; |
| 672 | int len = NLMSG_SPACE(size); |
| 673 | int flags = multi ? NLM_F_MULTI : 0; |
| 674 | int t = done ? NLMSG_DONE : type; |
| 675 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 676 | mempool_zone_complete(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 677 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 678 | skb = mempool_zone_get_skb(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 679 | /* |
| 680 | * FIXME: |
| 681 | * user is supposed to react on iferror == -ENOMEM; |
| 682 | * see iscsi_if_rx(). |
| 683 | */ |
| 684 | BUG_ON(!skb); |
| 685 | |
| 686 | nlh = __nlmsg_put(skb, pid, seq, t, (len - sizeof(*nlh)), 0); |
| 687 | nlh->nlmsg_flags = flags; |
| 688 | memcpy(NLMSG_DATA(nlh), payload, size); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 689 | return iscsi_unicast_skb(z_reply, skb); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static int |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 693 | iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 694 | { |
| 695 | struct iscsi_uevent *ev = NLMSG_DATA(nlh); |
| 696 | struct iscsi_stats *stats; |
| 697 | struct sk_buff *skbstat; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 698 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 699 | struct nlmsghdr *nlhstat; |
| 700 | struct iscsi_uevent *evstat; |
| 701 | int len = NLMSG_SPACE(sizeof(*ev) + |
| 702 | sizeof(struct iscsi_stats) + |
| 703 | sizeof(struct iscsi_stats_custom) * |
| 704 | ISCSI_STATS_CUSTOM_MAX); |
| 705 | int err = 0; |
| 706 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 707 | 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] | 708 | if (!conn) |
| 709 | return -EEXIST; |
| 710 | |
| 711 | do { |
| 712 | int actual_size; |
| 713 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 714 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 715 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 716 | skbstat = mempool_zone_get_skb(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 717 | if (!skbstat) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 718 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not " |
| 719 | "deliver stats: OOM\n"); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 720 | return -ENOMEM; |
| 721 | } |
| 722 | |
| 723 | nlhstat = __nlmsg_put(skbstat, daemon_pid, 0, 0, |
| 724 | (len - sizeof(*nlhstat)), 0); |
| 725 | evstat = NLMSG_DATA(nlhstat); |
| 726 | memset(evstat, 0, sizeof(*evstat)); |
| 727 | evstat->transport_handle = iscsi_handle(conn->transport); |
| 728 | evstat->type = nlh->nlmsg_type; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 729 | if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 730 | evstat->iferror = -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 731 | evstat->u.get_stats.cid = |
| 732 | ev->u.get_stats.cid; |
| 733 | evstat->u.get_stats.sid = |
| 734 | ev->u.get_stats.sid; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 735 | stats = (struct iscsi_stats *) |
| 736 | ((char*)evstat + sizeof(*evstat)); |
| 737 | memset(stats, 0, sizeof(*stats)); |
| 738 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 739 | transport->get_stats(conn, stats); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 740 | actual_size = NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 741 | sizeof(struct iscsi_stats) + |
| 742 | sizeof(struct iscsi_stats_custom) * |
| 743 | stats->custom_length); |
| 744 | actual_size -= sizeof(*nlhstat); |
| 745 | actual_size = NLMSG_LENGTH(actual_size); |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 746 | skb_trim(skbstat, NLMSG_ALIGN(actual_size)); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 747 | nlhstat->nlmsg_len = actual_size; |
| 748 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 749 | err = iscsi_unicast_skb(conn->z_pdu, skbstat); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 750 | } while (err < 0 && err != -ECONNREFUSED); |
| 751 | |
| 752 | return err; |
| 753 | } |
| 754 | |
| 755 | static int |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 756 | iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev) |
| 757 | { |
| 758 | struct iscsi_transport *transport = priv->iscsi_transport; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 759 | struct iscsi_cls_session *session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 760 | unsigned long flags; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 761 | uint32_t hostno; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 762 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 763 | session = transport->create_session(transport, &priv->t, |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 764 | ev->u.c_session.initial_cmdsn, |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 765 | &hostno); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 766 | if (!session) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 767 | return -ENOMEM; |
| 768 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 769 | spin_lock_irqsave(&sesslock, flags); |
| 770 | list_add(&session->sess_list, &sesslist); |
| 771 | spin_unlock_irqrestore(&sesslock, flags); |
| 772 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 773 | ev->r.c_session_ret.host_no = hostno; |
| 774 | ev->r.c_session_ret.sid = session->sid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static int |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 779 | iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 780 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 781 | struct iscsi_cls_conn *conn; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 782 | struct iscsi_cls_session *session; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 783 | unsigned long flags; |
| 784 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 785 | session = iscsi_session_lookup(ev->u.c_conn.sid); |
| 786 | if (!session) { |
| 787 | printk(KERN_ERR "iscsi: invalid session %d\n", |
| 788 | ev->u.c_conn.sid); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 789 | return -EINVAL; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 790 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 791 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 792 | conn = transport->create_conn(session, ev->u.c_conn.cid); |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 793 | if (!conn) { |
| 794 | printk(KERN_ERR "iscsi: couldn't create a new " |
| 795 | "connection for session %d\n", |
| 796 | session->sid); |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 797 | return -ENOMEM; |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 798 | } |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 799 | |
| 800 | conn->z_pdu = mempool_zone_init(Z_MAX_PDU, |
| 801 | NLMSG_SPACE(sizeof(struct iscsi_uevent) + |
| 802 | sizeof(struct iscsi_hdr) + |
| 803 | DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH), |
| 804 | Z_HIWAT_PDU); |
| 805 | if (!conn->z_pdu) { |
| 806 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 807 | "pdu zone for new conn\n"); |
| 808 | goto destroy_conn; |
| 809 | } |
| 810 | |
| 811 | conn->z_error = mempool_zone_init(Z_MAX_ERROR, |
| 812 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), |
| 813 | Z_HIWAT_ERROR); |
| 814 | if (!conn->z_error) { |
| 815 | dev_printk(KERN_ERR, &conn->dev, "iscsi: can not allocate " |
| 816 | "error zone for new conn\n"); |
| 817 | goto free_pdu_pool; |
| 818 | } |
| 819 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 820 | ev->r.c_conn_ret.sid = session->sid; |
| 821 | ev->r.c_conn_ret.cid = conn->cid; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 822 | |
| 823 | spin_lock_irqsave(&connlock, flags); |
| 824 | list_add(&conn->conn_list, &connlist); |
| 825 | conn->active = 1; |
| 826 | spin_unlock_irqrestore(&connlock, flags); |
| 827 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 828 | return 0; |
| 829 | |
| 830 | free_pdu_pool: |
| 831 | mempool_zone_destroy(conn->z_pdu); |
| 832 | destroy_conn: |
| 833 | if (transport->destroy_conn) |
| 834 | transport->destroy_conn(conn->dd_data); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 835 | return -ENOMEM; |
| 836 | } |
| 837 | |
| 838 | static int |
| 839 | iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 840 | { |
| 841 | unsigned long flags; |
| 842 | struct iscsi_cls_conn *conn; |
| 843 | struct mempool_zone *z_error, *z_pdu; |
| 844 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 845 | 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] | 846 | if (!conn) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 847 | return -EINVAL; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 848 | spin_lock_irqsave(&connlock, flags); |
| 849 | conn->active = 0; |
| 850 | list_del(&conn->conn_list); |
| 851 | spin_unlock_irqrestore(&connlock, flags); |
| 852 | |
| 853 | z_pdu = conn->z_pdu; |
| 854 | z_error = conn->z_error; |
| 855 | |
| 856 | if (transport->destroy_conn) |
| 857 | transport->destroy_conn(conn); |
| 858 | |
| 859 | mempool_zone_destroy(z_pdu); |
| 860 | mempool_zone_destroy(z_error); |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 865 | static void |
| 866 | iscsi_copy_param(struct iscsi_uevent *ev, uint32_t *value, char *data) |
| 867 | { |
| 868 | if (ev->u.set_param.len != sizeof(uint32_t)) |
| 869 | BUG(); |
| 870 | memcpy(value, data, min_t(uint32_t, sizeof(uint32_t), |
| 871 | ev->u.set_param.len)); |
| 872 | } |
| 873 | |
| 874 | static int |
| 875 | iscsi_set_param(struct iscsi_transport *transport, struct iscsi_uevent *ev) |
| 876 | { |
| 877 | char *data = (char*)ev + sizeof(*ev); |
| 878 | struct iscsi_cls_conn *conn; |
| 879 | struct iscsi_cls_session *session; |
| 880 | int err = 0; |
| 881 | uint32_t value = 0; |
| 882 | |
| 883 | session = iscsi_session_lookup(ev->u.set_param.sid); |
| 884 | conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid); |
| 885 | if (!conn || !session) |
| 886 | return -EINVAL; |
| 887 | |
| 888 | switch (ev->u.set_param.param) { |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 889 | case ISCSI_PARAM_SESS_RECOVERY_TMO: |
| 890 | iscsi_copy_param(ev, &value, data); |
| 891 | if (value != 0) |
| 892 | session->recovery_tmo = value; |
| 893 | break; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 894 | case ISCSI_PARAM_TARGET_NAME: |
| 895 | /* this should not change between logins */ |
| 896 | if (session->targetname) |
| 897 | return 0; |
| 898 | |
| 899 | session->targetname = kstrdup(data, GFP_KERNEL); |
| 900 | if (!session->targetname) |
| 901 | return -ENOMEM; |
| 902 | break; |
| 903 | case ISCSI_PARAM_TPGT: |
| 904 | iscsi_copy_param(ev, &value, data); |
| 905 | session->tpgt = value; |
| 906 | break; |
| 907 | case ISCSI_PARAM_PERSISTENT_PORT: |
| 908 | iscsi_copy_param(ev, &value, data); |
| 909 | conn->persistent_port = value; |
| 910 | break; |
| 911 | case ISCSI_PARAM_PERSISTENT_ADDRESS: |
| 912 | /* |
| 913 | * this is the address returned in discovery so it should |
| 914 | * not change between logins. |
| 915 | */ |
| 916 | if (conn->persistent_address) |
| 917 | return 0; |
| 918 | |
| 919 | conn->persistent_address = kstrdup(data, GFP_KERNEL); |
| 920 | if (!conn->persistent_address) |
| 921 | return -ENOMEM; |
| 922 | break; |
| 923 | default: |
| 924 | iscsi_copy_param(ev, &value, data); |
| 925 | err = transport->set_param(conn, ev->u.set_param.param, value); |
| 926 | } |
| 927 | |
| 928 | return err; |
| 929 | } |
| 930 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 931 | static int |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 932 | iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 933 | { |
| 934 | int err = 0; |
| 935 | struct iscsi_uevent *ev = NLMSG_DATA(nlh); |
| 936 | struct iscsi_transport *transport = NULL; |
| 937 | struct iscsi_internal *priv; |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 938 | struct iscsi_cls_session *session; |
| 939 | struct iscsi_cls_conn *conn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 940 | unsigned long flags; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 941 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 942 | priv = iscsi_if_transport_lookup(iscsi_ptr(ev->transport_handle)); |
| 943 | if (!priv) |
| 944 | return -EINVAL; |
| 945 | transport = priv->iscsi_transport; |
| 946 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 947 | if (!try_module_get(transport->owner)) |
| 948 | return -EINVAL; |
| 949 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 950 | switch (nlh->nlmsg_type) { |
| 951 | case ISCSI_UEVENT_CREATE_SESSION: |
| 952 | err = iscsi_if_create_session(priv, ev); |
| 953 | break; |
| 954 | case ISCSI_UEVENT_DESTROY_SESSION: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 955 | session = iscsi_session_lookup(ev->u.d_session.sid); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 956 | if (session) { |
| 957 | spin_lock_irqsave(&sesslock, flags); |
| 958 | list_del(&session->sess_list); |
| 959 | spin_unlock_irqrestore(&sesslock, flags); |
| 960 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 961 | transport->destroy_session(session); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame^] | 962 | } else |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 963 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 964 | break; |
| 965 | case ISCSI_UEVENT_CREATE_CONN: |
| 966 | err = iscsi_if_create_conn(transport, ev); |
| 967 | break; |
| 968 | case ISCSI_UEVENT_DESTROY_CONN: |
| 969 | err = iscsi_if_destroy_conn(transport, ev); |
| 970 | break; |
| 971 | case ISCSI_UEVENT_BIND_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 972 | session = iscsi_session_lookup(ev->u.b_conn.sid); |
| 973 | 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] | 974 | |
| 975 | if (session && conn) |
| 976 | ev->r.retcode = transport->bind_conn(session, conn, |
| 977 | ev->u.b_conn.transport_fd, |
| 978 | ev->u.b_conn.is_leading); |
| 979 | else |
| 980 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 981 | break; |
| 982 | case ISCSI_UEVENT_SET_PARAM: |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 983 | err = iscsi_set_param(transport, ev); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 984 | break; |
| 985 | case ISCSI_UEVENT_START_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 986 | 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] | 987 | if (conn) |
| 988 | ev->r.retcode = transport->start_conn(conn); |
| 989 | else |
| 990 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 991 | break; |
| 992 | case ISCSI_UEVENT_STOP_CONN: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 993 | 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] | 994 | if (conn) |
| 995 | transport->stop_conn(conn, ev->u.stop_conn.flag); |
| 996 | else |
| 997 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 998 | break; |
| 999 | case ISCSI_UEVENT_SEND_PDU: |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1000 | 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] | 1001 | if (conn) |
| 1002 | ev->r.retcode = transport->send_pdu(conn, |
| 1003 | (struct iscsi_hdr*)((char*)ev + sizeof(*ev)), |
| 1004 | (char*)ev + sizeof(*ev) + ev->u.send_pdu.hdr_size, |
| 1005 | ev->u.send_pdu.data_size); |
| 1006 | else |
| 1007 | err = -EINVAL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | case ISCSI_UEVENT_GET_STATS: |
Mike Christie | 5b940ad | 2006-02-01 21:06:56 -0600 | [diff] [blame] | 1010 | err = iscsi_if_get_stats(transport, nlh); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1011 | break; |
| 1012 | default: |
| 1013 | err = -EINVAL; |
| 1014 | break; |
| 1015 | } |
| 1016 | |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1017 | module_put(transport->owner); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1018 | return err; |
| 1019 | } |
| 1020 | |
Mike Christie | b5c7a12 | 2006-04-06 21:13:33 -0500 | [diff] [blame] | 1021 | /* |
| 1022 | * Get message from skb (based on rtnetlink_rcv_skb). Each message is |
| 1023 | * processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or |
| 1024 | * invalid creds are discarded silently. |
| 1025 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1026 | static void |
| 1027 | iscsi_if_rx(struct sock *sk, int len) |
| 1028 | { |
| 1029 | struct sk_buff *skb; |
| 1030 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1031 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1032 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1033 | if (NETLINK_CREDS(skb)->uid) { |
| 1034 | skb_pull(skb, skb->len); |
| 1035 | goto free_skb; |
| 1036 | } |
| 1037 | daemon_pid = NETLINK_CREDS(skb)->pid; |
| 1038 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1039 | while (skb->len >= NLMSG_SPACE(0)) { |
| 1040 | int err; |
| 1041 | uint32_t rlen; |
| 1042 | struct nlmsghdr *nlh; |
| 1043 | struct iscsi_uevent *ev; |
| 1044 | |
| 1045 | nlh = (struct nlmsghdr *)skb->data; |
| 1046 | if (nlh->nlmsg_len < sizeof(*nlh) || |
| 1047 | skb->len < nlh->nlmsg_len) { |
| 1048 | break; |
| 1049 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1050 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1051 | ev = NLMSG_DATA(nlh); |
| 1052 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 1053 | if (rlen > skb->len) |
| 1054 | rlen = skb->len; |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1055 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1056 | err = iscsi_if_recv_msg(skb, nlh); |
| 1057 | if (err) { |
| 1058 | ev->type = ISCSI_KEVENT_IF_ERROR; |
| 1059 | ev->iferror = err; |
| 1060 | } |
| 1061 | do { |
| 1062 | /* |
| 1063 | * special case for GET_STATS: |
| 1064 | * on success - sending reply and stats from |
| 1065 | * inside of if_recv_msg(), |
| 1066 | * on error - fall through. |
| 1067 | */ |
| 1068 | if (ev->type == ISCSI_UEVENT_GET_STATS && !err) |
| 1069 | break; |
| 1070 | err = iscsi_if_send_reply( |
| 1071 | NETLINK_CREDS(skb)->pid, nlh->nlmsg_seq, |
| 1072 | nlh->nlmsg_type, 0, 0, ev, sizeof(*ev)); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1073 | if (atomic_read(&z_reply->allocated) >= |
| 1074 | z_reply->hiwat) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1075 | ev->iferror = -ENOMEM; |
| 1076 | } while (err < 0 && err != -ECONNREFUSED); |
| 1077 | skb_pull(skb, rlen); |
| 1078 | } |
Mike Christie | ee7f8e4 | 2006-02-01 21:07:01 -0600 | [diff] [blame] | 1079 | free_skb: |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1080 | kfree_skb(skb); |
| 1081 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1082 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1085 | #define iscsi_cdev_to_conn(_cdev) \ |
| 1086 | iscsi_dev_to_conn(_cdev->dev) |
| 1087 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1088 | #define ISCSI_CLASS_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 1089 | struct class_device_attribute class_device_attr_##_prefix##_##_name = \ |
| 1090 | __ATTR(_name,_mode,_show,_store) |
| 1091 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1092 | /* |
| 1093 | * iSCSI connection attrs |
| 1094 | */ |
| 1095 | #define iscsi_conn_int_attr_show(param, format) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | static ssize_t \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1097 | show_conn_int_param_##param(struct class_device *cdev, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1099 | uint32_t value = 0; \ |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1100 | struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \ |
| 1101 | struct iscsi_transport *t = conn->transport; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | \ |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1103 | t->get_conn_param(conn, param, &value); \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1104 | return snprintf(buf, 20, format"\n", value); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1107 | #define iscsi_conn_int_attr(field, param, format) \ |
| 1108 | iscsi_conn_int_attr_show(param, format) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1109 | static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_int_param_##param, \ |
| 1110 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1112 | iscsi_conn_int_attr(max_recv_dlength, ISCSI_PARAM_MAX_RECV_DLENGTH, "%u"); |
| 1113 | iscsi_conn_int_attr(max_xmit_dlength, ISCSI_PARAM_MAX_XMIT_DLENGTH, "%u"); |
| 1114 | iscsi_conn_int_attr(header_digest, ISCSI_PARAM_HDRDGST_EN, "%d"); |
| 1115 | iscsi_conn_int_attr(data_digest, ISCSI_PARAM_DATADGST_EN, "%d"); |
| 1116 | iscsi_conn_int_attr(ifmarker, ISCSI_PARAM_IFMARKER_EN, "%d"); |
| 1117 | iscsi_conn_int_attr(ofmarker, ISCSI_PARAM_OFMARKER_EN, "%d"); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1118 | iscsi_conn_int_attr(persistent_port, ISCSI_PARAM_PERSISTENT_PORT, "%d"); |
| 1119 | iscsi_conn_int_attr(port, ISCSI_PARAM_CONN_PORT, "%d"); |
| 1120 | |
| 1121 | #define iscsi_conn_str_attr_show(param) \ |
| 1122 | static ssize_t \ |
| 1123 | show_conn_str_param_##param(struct class_device *cdev, char *buf) \ |
| 1124 | { \ |
| 1125 | struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \ |
| 1126 | struct iscsi_transport *t = conn->transport; \ |
| 1127 | return t->get_conn_str_param(conn, param, buf); \ |
| 1128 | } |
| 1129 | |
| 1130 | #define iscsi_conn_str_attr(field, param) \ |
| 1131 | iscsi_conn_str_attr_show(param) \ |
| 1132 | static ISCSI_CLASS_ATTR(conn, field, S_IRUGO, show_conn_str_param_##param, \ |
| 1133 | NULL); |
| 1134 | |
| 1135 | iscsi_conn_str_attr(persistent_address, ISCSI_PARAM_PERSISTENT_ADDRESS); |
| 1136 | iscsi_conn_str_attr(address, ISCSI_PARAM_CONN_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1138 | #define iscsi_cdev_to_session(_cdev) \ |
| 1139 | iscsi_dev_to_session(_cdev->dev) |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /* |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1142 | * iSCSI session attrs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | */ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1144 | #define iscsi_session_int_attr_show(param, format) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | static ssize_t \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1146 | show_session_int_param_##param(struct class_device *cdev, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | { \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1148 | uint32_t value = 0; \ |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1149 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \ |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1150 | struct iscsi_transport *t = session->transport; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | \ |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1152 | t->get_session_param(session, param, &value); \ |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1153 | return snprintf(buf, 20, format"\n", value); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1156 | #define iscsi_session_int_attr(field, param, format) \ |
| 1157 | iscsi_session_int_attr_show(param, format) \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1158 | static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_int_param_##param, \ |
| 1159 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1161 | iscsi_session_int_attr(initial_r2t, ISCSI_PARAM_INITIAL_R2T_EN, "%d"); |
| 1162 | iscsi_session_int_attr(max_outstanding_r2t, ISCSI_PARAM_MAX_R2T, "%hu"); |
| 1163 | iscsi_session_int_attr(immediate_data, ISCSI_PARAM_IMM_DATA_EN, "%d"); |
| 1164 | iscsi_session_int_attr(first_burst_len, ISCSI_PARAM_FIRST_BURST, "%u"); |
| 1165 | iscsi_session_int_attr(max_burst_len, ISCSI_PARAM_MAX_BURST, "%u"); |
| 1166 | iscsi_session_int_attr(data_pdu_in_order, ISCSI_PARAM_PDU_INORDER_EN, "%d"); |
| 1167 | iscsi_session_int_attr(data_seq_in_order, ISCSI_PARAM_DATASEQ_INORDER_EN, "%d"); |
| 1168 | iscsi_session_int_attr(erl, ISCSI_PARAM_ERL, "%d"); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1169 | iscsi_session_int_attr(tpgt, ISCSI_PARAM_TPGT, "%d"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1171 | #define iscsi_session_str_attr_show(param) \ |
| 1172 | static ssize_t \ |
| 1173 | show_session_str_param_##param(struct class_device *cdev, char *buf) \ |
| 1174 | { \ |
| 1175 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \ |
| 1176 | struct iscsi_transport *t = session->transport; \ |
| 1177 | return t->get_session_str_param(session, param, buf); \ |
| 1178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1180 | #define iscsi_session_str_attr(field, param) \ |
| 1181 | iscsi_session_str_attr_show(param) \ |
| 1182 | static ISCSI_CLASS_ATTR(sess, field, S_IRUGO, show_session_str_param_##param, \ |
| 1183 | NULL); |
| 1184 | |
| 1185 | iscsi_session_str_attr(targetname, ISCSI_PARAM_TARGET_NAME); |
| 1186 | |
| 1187 | /* |
| 1188 | * Private session and conn attrs. userspace uses several iscsi values |
| 1189 | * to identify each session between reboots. Some of these values may not |
| 1190 | * be present in the iscsi_transport/LLD driver becuase userspace handles |
| 1191 | * login (and failback for login redirect) so for these type of drivers |
| 1192 | * the class manages the attrs and values for the iscsi_transport/LLD |
| 1193 | */ |
| 1194 | #define iscsi_priv_session_attr_show(field, format) \ |
| 1195 | static ssize_t \ |
| 1196 | show_priv_session_##field(struct class_device *cdev, char *buf) \ |
| 1197 | { \ |
| 1198 | struct iscsi_cls_session *session = iscsi_cdev_to_session(cdev); \ |
| 1199 | return sprintf(buf, format"\n", session->field); \ |
| 1200 | } |
| 1201 | |
| 1202 | #define iscsi_priv_session_attr(field, format) \ |
| 1203 | iscsi_priv_session_attr_show(field, format) \ |
| 1204 | static ISCSI_CLASS_ATTR(priv_sess, field, S_IRUGO, show_priv_session_##field, \ |
| 1205 | NULL) |
| 1206 | iscsi_priv_session_attr(targetname, "%s"); |
| 1207 | iscsi_priv_session_attr(tpgt, "%d"); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1208 | iscsi_priv_session_attr(recovery_tmo, "%d"); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1209 | |
| 1210 | #define iscsi_priv_conn_attr_show(field, format) \ |
| 1211 | static ssize_t \ |
| 1212 | show_priv_conn_##field(struct class_device *cdev, char *buf) \ |
| 1213 | { \ |
| 1214 | struct iscsi_cls_conn *conn = iscsi_cdev_to_conn(cdev); \ |
| 1215 | return sprintf(buf, format"\n", conn->field); \ |
| 1216 | } |
| 1217 | |
| 1218 | #define iscsi_priv_conn_attr(field, format) \ |
| 1219 | iscsi_priv_conn_attr_show(field, format) \ |
| 1220 | static ISCSI_CLASS_ATTR(priv_conn, field, S_IRUGO, show_priv_conn_##field, \ |
| 1221 | NULL) |
| 1222 | iscsi_priv_conn_attr(persistent_address, "%s"); |
| 1223 | iscsi_priv_conn_attr(persistent_port, "%d"); |
| 1224 | |
| 1225 | #define SETUP_PRIV_SESSION_RD_ATTR(field) \ |
| 1226 | do { \ |
| 1227 | priv->session_attrs[count] = &class_device_attr_priv_sess_##field; \ |
| 1228 | count++; \ |
| 1229 | } while (0) |
| 1230 | |
| 1231 | #define SETUP_SESSION_RD_ATTR(field, param_flag) \ |
| 1232 | do { \ |
| 1233 | if (tt->param_mask & param_flag) { \ |
| 1234 | priv->session_attrs[count] = &class_device_attr_sess_##field; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | count++; \ |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1236 | } \ |
| 1237 | } while (0) |
| 1238 | |
| 1239 | #define SETUP_PRIV_CONN_RD_ATTR(field) \ |
| 1240 | do { \ |
| 1241 | priv->conn_attrs[count] = &class_device_attr_priv_conn_##field; \ |
| 1242 | count++; \ |
| 1243 | } while (0) |
| 1244 | |
| 1245 | #define SETUP_CONN_RD_ATTR(field, param_flag) \ |
| 1246 | do { \ |
| 1247 | if (tt->param_mask & param_flag) { \ |
| 1248 | priv->conn_attrs[count] = &class_device_attr_conn_##field; \ |
| 1249 | count++; \ |
| 1250 | } \ |
| 1251 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1253 | static int iscsi_session_match(struct attribute_container *cont, |
| 1254 | struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1256 | struct iscsi_cls_session *session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | struct Scsi_Host *shost; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1258 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1260 | if (!iscsi_is_session_dev(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | return 0; |
| 1262 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1263 | session = iscsi_dev_to_session(dev); |
| 1264 | shost = iscsi_session_to_shost(session); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1265 | if (!shost->transportt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return 0; |
| 1267 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1268 | priv = to_iscsi_internal(shost->transportt); |
| 1269 | if (priv->session_cont.ac.class != &iscsi_session_class.class) |
| 1270 | return 0; |
| 1271 | |
| 1272 | return &priv->session_cont.ac == cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1275 | static int iscsi_conn_match(struct attribute_container *cont, |
| 1276 | struct device *dev) |
| 1277 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1278 | struct iscsi_cls_session *session; |
| 1279 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1280 | struct Scsi_Host *shost; |
| 1281 | struct iscsi_internal *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1283 | if (!iscsi_is_conn_dev(dev)) |
| 1284 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1286 | conn = iscsi_dev_to_conn(dev); |
| 1287 | session = iscsi_dev_to_session(conn->dev.parent); |
| 1288 | shost = iscsi_session_to_shost(session); |
| 1289 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1290 | if (!shost->transportt) |
| 1291 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1293 | priv = to_iscsi_internal(shost->transportt); |
| 1294 | if (priv->conn_cont.ac.class != &iscsi_connection_class.class) |
| 1295 | return 0; |
| 1296 | |
| 1297 | return &priv->conn_cont.ac == cont; |
| 1298 | } |
| 1299 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1300 | static int iscsi_host_match(struct attribute_container *cont, |
| 1301 | struct device *dev) |
| 1302 | { |
| 1303 | struct Scsi_Host *shost; |
| 1304 | struct iscsi_internal *priv; |
| 1305 | |
| 1306 | if (!scsi_is_host_device(dev)) |
| 1307 | return 0; |
| 1308 | |
| 1309 | shost = dev_to_shost(dev); |
| 1310 | if (!shost->transportt || |
| 1311 | shost->transportt->host_attrs.ac.class != &iscsi_host_class.class) |
| 1312 | return 0; |
| 1313 | |
| 1314 | priv = to_iscsi_internal(shost->transportt); |
| 1315 | return &priv->t.host_attrs.ac == cont; |
| 1316 | } |
| 1317 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1318 | struct scsi_transport_template * |
| 1319 | iscsi_register_transport(struct iscsi_transport *tt) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1320 | { |
| 1321 | struct iscsi_internal *priv; |
| 1322 | unsigned long flags; |
| 1323 | int count = 0, err; |
| 1324 | |
| 1325 | BUG_ON(!tt); |
| 1326 | |
| 1327 | priv = iscsi_if_transport_lookup(tt); |
| 1328 | if (priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1329 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1330 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 1331 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1332 | if (!priv) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1333 | return NULL; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1334 | INIT_LIST_HEAD(&priv->list); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1335 | priv->iscsi_transport = tt; |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1336 | priv->t.user_scan = iscsi_user_scan; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1337 | |
| 1338 | priv->cdev.class = &iscsi_transport_class; |
| 1339 | snprintf(priv->cdev.class_id, BUS_ID_SIZE, "%s", tt->name); |
| 1340 | err = class_device_register(&priv->cdev); |
| 1341 | if (err) |
| 1342 | goto free_priv; |
| 1343 | |
| 1344 | err = sysfs_create_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1345 | if (err) |
| 1346 | goto unregister_cdev; |
| 1347 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1348 | /* host parameters */ |
| 1349 | priv->t.host_attrs.ac.attrs = &priv->host_attrs[0]; |
| 1350 | priv->t.host_attrs.ac.class = &iscsi_host_class.class; |
| 1351 | priv->t.host_attrs.ac.match = iscsi_host_match; |
| 1352 | priv->t.host_size = sizeof(struct iscsi_host); |
| 1353 | priv->host_attrs[0] = NULL; |
| 1354 | transport_container_register(&priv->t.host_attrs); |
| 1355 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1356 | /* connection parameters */ |
| 1357 | priv->conn_cont.ac.attrs = &priv->conn_attrs[0]; |
| 1358 | priv->conn_cont.ac.class = &iscsi_connection_class.class; |
| 1359 | priv->conn_cont.ac.match = iscsi_conn_match; |
| 1360 | transport_container_register(&priv->conn_cont); |
| 1361 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1362 | SETUP_CONN_RD_ATTR(max_recv_dlength, ISCSI_MAX_RECV_DLENGTH); |
| 1363 | SETUP_CONN_RD_ATTR(max_xmit_dlength, ISCSI_MAX_XMIT_DLENGTH); |
| 1364 | SETUP_CONN_RD_ATTR(header_digest, ISCSI_HDRDGST_EN); |
| 1365 | SETUP_CONN_RD_ATTR(data_digest, ISCSI_DATADGST_EN); |
| 1366 | SETUP_CONN_RD_ATTR(ifmarker, ISCSI_IFMARKER_EN); |
| 1367 | SETUP_CONN_RD_ATTR(ofmarker, ISCSI_OFMARKER_EN); |
| 1368 | SETUP_CONN_RD_ATTR(address, ISCSI_CONN_ADDRESS); |
| 1369 | SETUP_CONN_RD_ATTR(port, ISCSI_CONN_PORT); |
| 1370 | |
| 1371 | if (tt->param_mask & ISCSI_PERSISTENT_ADDRESS) |
| 1372 | SETUP_CONN_RD_ATTR(persistent_address, ISCSI_PERSISTENT_ADDRESS); |
| 1373 | else |
| 1374 | SETUP_PRIV_CONN_RD_ATTR(persistent_address); |
| 1375 | |
| 1376 | if (tt->param_mask & ISCSI_PERSISTENT_PORT) |
| 1377 | SETUP_CONN_RD_ATTR(persistent_port, ISCSI_PERSISTENT_PORT); |
| 1378 | else |
| 1379 | SETUP_PRIV_CONN_RD_ATTR(persistent_port); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1380 | |
| 1381 | BUG_ON(count > ISCSI_CONN_ATTRS); |
| 1382 | priv->conn_attrs[count] = NULL; |
| 1383 | count = 0; |
| 1384 | |
| 1385 | /* session parameters */ |
| 1386 | priv->session_cont.ac.attrs = &priv->session_attrs[0]; |
| 1387 | priv->session_cont.ac.class = &iscsi_session_class.class; |
| 1388 | priv->session_cont.ac.match = iscsi_session_match; |
| 1389 | transport_container_register(&priv->session_cont); |
| 1390 | |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1391 | SETUP_SESSION_RD_ATTR(initial_r2t, ISCSI_INITIAL_R2T_EN); |
| 1392 | SETUP_SESSION_RD_ATTR(max_outstanding_r2t, ISCSI_MAX_R2T); |
| 1393 | SETUP_SESSION_RD_ATTR(immediate_data, ISCSI_IMM_DATA_EN); |
| 1394 | SETUP_SESSION_RD_ATTR(first_burst_len, ISCSI_FIRST_BURST); |
| 1395 | SETUP_SESSION_RD_ATTR(max_burst_len, ISCSI_MAX_BURST); |
| 1396 | SETUP_SESSION_RD_ATTR(data_pdu_in_order, ISCSI_PDU_INORDER_EN); |
| 1397 | SETUP_SESSION_RD_ATTR(data_seq_in_order, ISCSI_DATASEQ_INORDER_EN); |
| 1398 | SETUP_SESSION_RD_ATTR(erl, ISCSI_ERL); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1399 | SETUP_PRIV_SESSION_RD_ATTR(recovery_tmo); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1400 | |
| 1401 | if (tt->param_mask & ISCSI_TARGET_NAME) |
| 1402 | SETUP_SESSION_RD_ATTR(targetname, ISCSI_TARGET_NAME); |
| 1403 | else |
| 1404 | SETUP_PRIV_SESSION_RD_ATTR(targetname); |
| 1405 | |
| 1406 | if (tt->param_mask & ISCSI_TPGT) |
| 1407 | SETUP_SESSION_RD_ATTR(tpgt, ISCSI_TPGT); |
| 1408 | else |
| 1409 | SETUP_PRIV_SESSION_RD_ATTR(tpgt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
| 1411 | BUG_ON(count > ISCSI_SESSION_ATTRS); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1412 | priv->session_attrs[count] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1414 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1415 | list_add(&priv->list, &iscsi_transports); |
| 1416 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1418 | printk(KERN_NOTICE "iscsi: registered transport (%s)\n", tt->name); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1419 | return &priv->t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1421 | unregister_cdev: |
| 1422 | class_device_unregister(&priv->cdev); |
| 1423 | free_priv: |
| 1424 | kfree(priv); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1425 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1427 | EXPORT_SYMBOL_GPL(iscsi_register_transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1429 | int iscsi_unregister_transport(struct iscsi_transport *tt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1431 | struct iscsi_internal *priv; |
| 1432 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1434 | BUG_ON(!tt); |
| 1435 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1436 | mutex_lock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1437 | |
| 1438 | priv = iscsi_if_transport_lookup(tt); |
| 1439 | BUG_ON (!priv); |
| 1440 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1441 | spin_lock_irqsave(&iscsi_transport_lock, flags); |
| 1442 | list_del(&priv->list); |
| 1443 | spin_unlock_irqrestore(&iscsi_transport_lock, flags); |
| 1444 | |
| 1445 | transport_container_unregister(&priv->conn_cont); |
| 1446 | transport_container_unregister(&priv->session_cont); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1447 | transport_container_unregister(&priv->t.host_attrs); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1448 | |
| 1449 | sysfs_remove_group(&priv->cdev.kobj, &iscsi_transport_group); |
| 1450 | class_device_unregister(&priv->cdev); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1451 | mutex_unlock(&rx_queue_mutex); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | EXPORT_SYMBOL_GPL(iscsi_unregister_transport); |
| 1456 | |
| 1457 | static int |
| 1458 | iscsi_rcv_nl_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1459 | { |
| 1460 | struct netlink_notify *n = ptr; |
| 1461 | |
| 1462 | if (event == NETLINK_URELEASE && |
| 1463 | n->protocol == NETLINK_ISCSI && n->pid) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1464 | struct iscsi_cls_conn *conn; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1465 | unsigned long flags; |
| 1466 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1467 | mempool_zone_complete(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1468 | spin_lock_irqsave(&connlock, flags); |
| 1469 | list_for_each_entry(conn, &connlist, conn_list) { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1470 | mempool_zone_complete(conn->z_error); |
| 1471 | mempool_zone_complete(conn->z_pdu); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1472 | } |
| 1473 | spin_unlock_irqrestore(&connlock, flags); |
| 1474 | } |
| 1475 | |
| 1476 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1479 | static struct notifier_block iscsi_nl_notifier = { |
| 1480 | .notifier_call = iscsi_rcv_nl_event, |
| 1481 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | static __init int iscsi_transport_init(void) |
| 1484 | { |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1485 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1487 | err = class_register(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | if (err) |
| 1489 | return err; |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1490 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1491 | err = transport_class_register(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1492 | if (err) |
| 1493 | goto unregister_transport_class; |
| 1494 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1495 | err = transport_class_register(&iscsi_connection_class); |
| 1496 | if (err) |
| 1497 | goto unregister_host_class; |
| 1498 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1499 | err = transport_class_register(&iscsi_session_class); |
| 1500 | if (err) |
| 1501 | goto unregister_conn_class; |
| 1502 | |
| 1503 | err = netlink_register_notifier(&iscsi_nl_notifier); |
| 1504 | if (err) |
| 1505 | goto unregister_session_class; |
| 1506 | |
James Bottomley | 2290d2b | 2005-09-08 10:14:11 -0500 | [diff] [blame] | 1507 | nls = netlink_kernel_create(NETLINK_ISCSI, 1, iscsi_if_rx, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1508 | THIS_MODULE); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1509 | if (!nls) { |
| 1510 | err = -ENOBUFS; |
| 1511 | goto unregister_notifier; |
| 1512 | } |
| 1513 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1514 | z_reply = mempool_zone_init(Z_MAX_REPLY, |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1515 | NLMSG_SPACE(sizeof(struct iscsi_uevent)), Z_HIWAT_REPLY); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1516 | if (z_reply) |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1517 | return 0; |
| 1518 | |
| 1519 | sock_release(nls->sk_socket); |
| 1520 | unregister_notifier: |
| 1521 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1522 | unregister_session_class: |
| 1523 | transport_class_unregister(&iscsi_session_class); |
| 1524 | unregister_conn_class: |
| 1525 | transport_class_unregister(&iscsi_connection_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1526 | unregister_host_class: |
| 1527 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1528 | unregister_transport_class: |
| 1529 | class_unregister(&iscsi_transport_class); |
| 1530 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void __exit iscsi_transport_exit(void) |
| 1534 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1535 | mempool_zone_destroy(z_reply); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1536 | sock_release(nls->sk_socket); |
| 1537 | netlink_unregister_notifier(&iscsi_nl_notifier); |
| 1538 | transport_class_unregister(&iscsi_connection_class); |
| 1539 | transport_class_unregister(&iscsi_session_class); |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1540 | transport_class_unregister(&iscsi_host_class); |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1541 | class_unregister(&iscsi_transport_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | module_init(iscsi_transport_init); |
| 1545 | module_exit(iscsi_transport_exit); |
| 1546 | |
Alex Aizman | 0896b75 | 2005-08-04 19:33:07 -0700 | [diff] [blame] | 1547 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
| 1548 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
| 1549 | "Alex Aizman <itn780@yahoo.com>"); |
| 1550 | MODULE_DESCRIPTION("iSCSI Transport Interface"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | MODULE_LICENSE("GPL"); |