Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 2 | * This file is part of the zfcp device driver for |
| 3 | * FCP adapters for IBM System z9 and zSeries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 5 | * (C) Copyright IBM Corp. 2002, 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 22 | /* |
| 23 | * Driver authors: |
| 24 | * Martin Peschke (originator of the driver) |
| 25 | * Raimund Schroeder |
| 26 | * Aron Zeh |
| 27 | * Wolfgang Taphorn |
| 28 | * Stefan Bader |
| 29 | * Heiko Carstens (kernel 2.6 port of the driver) |
| 30 | * Andreas Herrmann |
| 31 | * Maxim Shchetynin |
| 32 | * Volker Sameske |
| 33 | * Ralph Wuerthner |
| 34 | */ |
| 35 | |
Christof Schmitt | 45633fd | 2008-06-10 18:20:55 +0200 | [diff] [blame^] | 36 | #include <linux/miscdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "zfcp_ext.h" |
| 38 | |
| 39 | /* accumulated log level (module parameter) */ |
| 40 | static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS; |
| 41 | static char *device; |
| 42 | /*********************** FUNCTION PROTOTYPES *********************************/ |
| 43 | |
| 44 | /* written against the module interface */ |
| 45 | static int __init zfcp_module_init(void); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /*********************** KERNEL/MODULE PARAMETERS ***************************/ |
| 48 | |
| 49 | /* declare driver module init/cleanup functions */ |
| 50 | module_init(zfcp_module_init); |
| 51 | |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 52 | MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | MODULE_DESCRIPTION |
Andreas Herrmann | 4a9d2d8 | 2006-05-22 18:14:08 +0200 | [diff] [blame] | 54 | ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | MODULE_LICENSE("GPL"); |
| 56 | |
| 6f71d9b | 2005-04-10 23:04:28 -0500 | [diff] [blame] | 57 | module_param(device, charp, 0400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | MODULE_PARM_DESC(device, "specify initial device"); |
| 59 | |
| 6f71d9b | 2005-04-10 23:04:28 -0500 | [diff] [blame] | 60 | module_param(loglevel, uint, 0400); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | MODULE_PARM_DESC(loglevel, |
| 62 | "log levels, 8 nibbles: " |
| 63 | "FC ERP QDIO CIO Config FSF SCSI Other, " |
| 64 | "levels: 0=none 1=normal 2=devel 3=trace"); |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /****************************************************************/ |
| 67 | /************** Functions without logging ***********************/ |
| 68 | /****************************************************************/ |
| 69 | |
| 70 | void |
| 71 | _zfcp_hex_dump(char *addr, int count) |
| 72 | { |
| 73 | int i; |
| 74 | for (i = 0; i < count; i++) { |
| 75 | printk("%02x", addr[i]); |
| 76 | if ((i % 4) == 3) |
| 77 | printk(" "); |
| 78 | if ((i % 32) == 31) |
| 79 | printk("\n"); |
| 80 | } |
| 81 | if (((i-1) % 32) != 31) |
| 82 | printk("\n"); |
| 83 | } |
| 84 | |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 85 | |
| 86 | /****************************************************************/ |
| 87 | /****** Functions to handle the request ID hash table ********/ |
| 88 | /****************************************************************/ |
| 89 | |
| 90 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF |
| 91 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 92 | static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 93 | { |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 94 | int idx; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 95 | |
| 96 | adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head), |
| 97 | GFP_KERNEL); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 98 | if (!adapter->req_list) |
| 99 | return -ENOMEM; |
| 100 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 101 | for (idx = 0; idx < REQUEST_LIST_SIZE; idx++) |
| 102 | INIT_LIST_HEAD(&adapter->req_list[idx]); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static void zfcp_reqlist_free(struct zfcp_adapter *adapter) |
| 107 | { |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 108 | kfree(adapter->req_list); |
| 109 | } |
| 110 | |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 111 | int zfcp_reqlist_isempty(struct zfcp_adapter *adapter) |
| 112 | { |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 113 | unsigned int idx; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 114 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 115 | for (idx = 0; idx < REQUEST_LIST_SIZE; idx++) |
| 116 | if (!list_empty(&adapter->req_list[idx])) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 117 | return 0; |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | #undef ZFCP_LOG_AREA |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /****************************************************************/ |
| 124 | /************** Uncategorised Functions *************************/ |
| 125 | /****************************************************************/ |
| 126 | |
| 127 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /** |
| 130 | * zfcp_device_setup - setup function |
| 131 | * @str: pointer to parameter string |
| 132 | * |
| 133 | * Parse "device=..." parameter string. |
| 134 | */ |
| 135 | static int __init |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 136 | zfcp_device_setup(char *devstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 138 | char *tmp, *str; |
| 139 | size_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 141 | if (!devstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 144 | len = strlen(devstr) + 1; |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 145 | str = kmalloc(len, GFP_KERNEL); |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 146 | if (!str) |
| 147 | goto err_out; |
| 148 | memcpy(str, devstr, len); |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | tmp = strchr(str, ','); |
| 151 | if (!tmp) |
| 152 | goto err_out; |
| 153 | *tmp++ = '\0'; |
| 154 | strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE); |
| 155 | zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0'; |
| 156 | |
| 157 | zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0); |
| 158 | if (*tmp++ != ',') |
| 159 | goto err_out; |
| 160 | if (*tmp == '\0') |
| 161 | goto err_out; |
| 162 | |
| 163 | zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0); |
| 164 | if (*tmp != '\0') |
| 165 | goto err_out; |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 166 | kfree(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 1; |
| 168 | |
| 169 | err_out: |
| 170 | ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str); |
Andreas Herrmann | cd8a383 | 2005-06-13 13:22:25 +0200 | [diff] [blame] | 171 | kfree(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static void __init |
| 176 | zfcp_init_device_configure(void) |
| 177 | { |
| 178 | struct zfcp_adapter *adapter; |
| 179 | struct zfcp_port *port; |
| 180 | struct zfcp_unit *unit; |
| 181 | |
| 182 | down(&zfcp_data.config_sema); |
| 183 | read_lock_irq(&zfcp_data.config_lock); |
| 184 | adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid); |
| 185 | if (adapter) |
| 186 | zfcp_adapter_get(adapter); |
| 187 | read_unlock_irq(&zfcp_data.config_lock); |
| 188 | |
| 189 | if (adapter == NULL) |
| 190 | goto out_adapter; |
| 191 | port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0); |
| 192 | if (!port) |
| 193 | goto out_port; |
| 194 | unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun); |
| 195 | if (!unit) |
| 196 | goto out_unit; |
| 197 | up(&zfcp_data.config_sema); |
| 198 | ccw_device_set_online(adapter->ccw_device); |
| 199 | zfcp_erp_wait(adapter); |
| 200 | down(&zfcp_data.config_sema); |
| 201 | zfcp_unit_put(unit); |
| 202 | out_unit: |
| 203 | zfcp_port_put(port); |
| 204 | out_port: |
| 205 | zfcp_adapter_put(adapter); |
| 206 | out_adapter: |
| 207 | up(&zfcp_data.config_sema); |
| 208 | return; |
| 209 | } |
| 210 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 211 | static int calc_alignment(int size) |
| 212 | { |
| 213 | int align = 1; |
| 214 | |
| 215 | if (!size) |
| 216 | return 0; |
| 217 | |
| 218 | while ((size - align) > 0) |
| 219 | align <<= 1; |
| 220 | |
| 221 | return align; |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | static int __init |
| 225 | zfcp_module_init(void) |
| 226 | { |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 227 | int retval = -ENOMEM; |
| 228 | int size, align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 230 | size = sizeof(struct zfcp_fsf_req_qtcb); |
| 231 | align = calc_alignment(size); |
| 232 | zfcp_data.fsf_req_qtcb_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 233 | kmem_cache_create("zfcp_fsf", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 234 | if (!zfcp_data.fsf_req_qtcb_cache) |
| 235 | goto out; |
| 236 | |
| 237 | size = sizeof(struct fsf_status_read_buffer); |
| 238 | align = calc_alignment(size); |
| 239 | zfcp_data.sr_buffer_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 240 | kmem_cache_create("zfcp_sr", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 241 | if (!zfcp_data.sr_buffer_cache) |
| 242 | goto out_sr_cache; |
| 243 | |
| 244 | size = sizeof(struct zfcp_gid_pn_data); |
| 245 | align = calc_alignment(size); |
| 246 | zfcp_data.gid_pn_cache = |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 247 | kmem_cache_create("zfcp_gid", size, align, 0, NULL); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 248 | if (!zfcp_data.gid_pn_cache) |
| 249 | goto out_gid_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | atomic_set(&zfcp_data.loglevel, loglevel); |
| 252 | |
| 253 | /* initialize adapter list */ |
| 254 | INIT_LIST_HEAD(&zfcp_data.adapter_list_head); |
| 255 | |
| 256 | /* initialize adapters to be removed list head */ |
| 257 | INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh); |
| 258 | |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 259 | zfcp_data.scsi_transport_template = |
| 260 | fc_attach_transport(&zfcp_transport_functions); |
| 261 | if (!zfcp_data.scsi_transport_template) |
| 262 | goto out_transport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | retval = misc_register(&zfcp_cfdc_misc); |
| 265 | if (retval != 0) { |
| 266 | ZFCP_LOG_INFO("registration of misc device " |
| 267 | "zfcp_cfdc failed\n"); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 268 | goto out_misc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Initialise proc semaphores */ |
| 272 | sema_init(&zfcp_data.config_sema, 1); |
| 273 | |
| 274 | /* initialise configuration rw lock */ |
| 275 | rwlock_init(&zfcp_data.config_lock); |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* setup dynamic I/O */ |
| 278 | retval = zfcp_ccw_register(); |
| 279 | if (retval) { |
| 280 | ZFCP_LOG_NORMAL("registration with common I/O layer failed\n"); |
| 281 | goto out_ccw_register; |
| 282 | } |
| 283 | |
| 284 | if (zfcp_device_setup(device)) |
| 285 | zfcp_init_device_configure(); |
| 286 | |
| 287 | goto out; |
| 288 | |
| 289 | out_ccw_register: |
| 290 | misc_deregister(&zfcp_cfdc_misc); |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 291 | out_misc: |
| 292 | fc_release_transport(zfcp_data.scsi_transport_template); |
| 293 | out_transport: |
| 294 | kmem_cache_destroy(zfcp_data.gid_pn_cache); |
| 295 | out_gid_cache: |
| 296 | kmem_cache_destroy(zfcp_data.sr_buffer_cache); |
| 297 | out_sr_cache: |
| 298 | kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | out: |
| 300 | return retval; |
| 301 | } |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | #undef ZFCP_LOG_AREA |
| 304 | |
| 305 | /****************************************************************/ |
| 306 | /****** Functions for configuration/set-up of structures ********/ |
| 307 | /****************************************************************/ |
| 308 | |
| 309 | #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG |
| 310 | |
| 311 | /** |
| 312 | * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN |
| 313 | * @port: pointer to port to search for unit |
| 314 | * @fcp_lun: FCP LUN to search for |
| 315 | * Traverse list of all units of a port and return pointer to a unit |
| 316 | * with the given FCP LUN. |
| 317 | */ |
| 318 | struct zfcp_unit * |
| 319 | zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun) |
| 320 | { |
| 321 | struct zfcp_unit *unit; |
| 322 | int found = 0; |
| 323 | |
| 324 | list_for_each_entry(unit, &port->unit_list_head, list) { |
| 325 | if ((unit->fcp_lun == fcp_lun) && |
| 326 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) |
| 327 | { |
| 328 | found = 1; |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | return found ? unit : NULL; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn |
| 337 | * @adapter: pointer to adapter to search for port |
| 338 | * @wwpn: wwpn to search for |
| 339 | * Traverse list of all ports of an adapter and return pointer to a port |
| 340 | * with the given wwpn. |
| 341 | */ |
| 342 | struct zfcp_port * |
| 343 | zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn) |
| 344 | { |
| 345 | struct zfcp_port *port; |
| 346 | int found = 0; |
| 347 | |
| 348 | list_for_each_entry(port, &adapter->port_list_head, list) { |
| 349 | if ((port->wwpn == wwpn) && |
| 350 | !(atomic_read(&port->status) & |
| 351 | (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) { |
| 352 | found = 1; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | return found ? port : NULL; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * zfcp_get_port_by_did - find port in port list of adapter by d_id |
| 361 | * @adapter: pointer to adapter to search for port |
| 362 | * @d_id: d_id to search for |
| 363 | * Traverse list of all ports of an adapter and return pointer to a port |
| 364 | * with the given d_id. |
| 365 | */ |
| 366 | struct zfcp_port * |
| 367 | zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id) |
| 368 | { |
| 369 | struct zfcp_port *port; |
| 370 | int found = 0; |
| 371 | |
| 372 | list_for_each_entry(port, &adapter->port_list_head, list) { |
| 373 | if ((port->d_id == d_id) && |
| 374 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status)) |
| 375 | { |
| 376 | found = 1; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | return found ? port : NULL; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id |
| 385 | * @bus_id: bus_id to search for |
| 386 | * Traverse list of all adapters and return pointer to an adapter |
| 387 | * with the given bus_id. |
| 388 | */ |
| 389 | struct zfcp_adapter * |
| 390 | zfcp_get_adapter_by_busid(char *bus_id) |
| 391 | { |
| 392 | struct zfcp_adapter *adapter; |
| 393 | int found = 0; |
| 394 | |
| 395 | list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) { |
| 396 | if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter), |
| 397 | BUS_ID_SIZE) == 0) && |
| 398 | !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, |
| 399 | &adapter->status)){ |
| 400 | found = 1; |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | return found ? adapter : NULL; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * zfcp_unit_enqueue - enqueue unit to unit list of a port. |
| 409 | * @port: pointer to port where unit is added |
| 410 | * @fcp_lun: FCP LUN of unit to be enqueued |
| 411 | * Return: pointer to enqueued unit on success, NULL on error |
| 412 | * Locks: config_sema must be held to serialize changes to the unit list |
| 413 | * |
| 414 | * Sets up some unit internal structures and creates sysfs entry. |
| 415 | */ |
| 416 | struct zfcp_unit * |
| 417 | zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun) |
| 418 | { |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 419 | struct zfcp_unit *unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * check that there is no unit with this FCP_LUN already in list |
| 423 | * and enqueue it. |
| 424 | * Note: Unlike for the adapter and the port, this is an error |
| 425 | */ |
| 426 | read_lock_irq(&zfcp_data.config_lock); |
| 427 | unit = zfcp_get_unit_by_lun(port, fcp_lun); |
| 428 | read_unlock_irq(&zfcp_data.config_lock); |
| 429 | if (unit) |
| 430 | return NULL; |
| 431 | |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 432 | unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (!unit) |
| 434 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | /* initialise reference count stuff */ |
| 437 | atomic_set(&unit->refcount, 0); |
| 438 | init_waitqueue_head(&unit->remove_wq); |
| 439 | |
| 440 | unit->port = port; |
| 441 | unit->fcp_lun = fcp_lun; |
| 442 | |
| 443 | /* setup for sysfs registration */ |
| 444 | snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun); |
| 445 | unit->sysfs_device.parent = &port->sysfs_device; |
| 446 | unit->sysfs_device.release = zfcp_sysfs_unit_release; |
| 447 | dev_set_drvdata(&unit->sysfs_device, unit); |
| 448 | |
| 449 | /* mark unit unusable as long as sysfs registration is not complete */ |
| 450 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); |
| 451 | |
Christof Schmitt | c961585 | 2008-05-06 11:00:05 +0200 | [diff] [blame] | 452 | spin_lock_init(&unit->latencies.lock); |
| 453 | unit->latencies.write.channel.min = 0xFFFFFFFF; |
| 454 | unit->latencies.write.fabric.min = 0xFFFFFFFF; |
| 455 | unit->latencies.read.channel.min = 0xFFFFFFFF; |
| 456 | unit->latencies.read.fabric.min = 0xFFFFFFFF; |
| 457 | unit->latencies.cmd.channel.min = 0xFFFFFFFF; |
| 458 | unit->latencies.cmd.fabric.min = 0xFFFFFFFF; |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (device_register(&unit->sysfs_device)) { |
| 461 | kfree(unit); |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) { |
| 466 | device_unregister(&unit->sysfs_device); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | zfcp_unit_get(unit); |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 471 | unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | write_lock_irq(&zfcp_data.config_lock); |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 474 | list_add_tail(&unit->list, &port->unit_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status); |
| 476 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status); |
| 477 | write_unlock_irq(&zfcp_data.config_lock); |
| 478 | |
| 479 | port->units++; |
| 480 | zfcp_port_get(port); |
| 481 | |
| 482 | return unit; |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | zfcp_unit_dequeue(struct zfcp_unit *unit) |
| 487 | { |
| 488 | zfcp_unit_wait(unit); |
| 489 | write_lock_irq(&zfcp_data.config_lock); |
| 490 | list_del(&unit->list); |
| 491 | write_unlock_irq(&zfcp_data.config_lock); |
| 492 | unit->port->units--; |
| 493 | zfcp_port_put(unit->port); |
| 494 | zfcp_sysfs_unit_remove_files(&unit->sysfs_device); |
| 495 | device_unregister(&unit->sysfs_device); |
| 496 | } |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | /* |
| 499 | * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI |
| 500 | * commands. |
Swen Schillig | 41fa2ad | 2007-09-07 09:15:31 +0200 | [diff] [blame] | 501 | * It also genrates fcp-nameserver request/response buffer and unsolicited |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | * status read fsf_req buffers. |
| 503 | * |
| 504 | * locks: must only be called with zfcp_data.config_sema taken |
| 505 | */ |
| 506 | static int |
| 507 | zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter) |
| 508 | { |
| 509 | adapter->pool.fsf_req_erp = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 510 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR, |
| 511 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 512 | if (!adapter->pool.fsf_req_erp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return -ENOMEM; |
| 514 | |
| 515 | adapter->pool.fsf_req_scsi = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 516 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR, |
| 517 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 518 | if (!adapter->pool.fsf_req_scsi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return -ENOMEM; |
| 520 | |
| 521 | adapter->pool.fsf_req_abort = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 522 | mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR, |
| 523 | zfcp_data.fsf_req_qtcb_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 524 | if (!adapter->pool.fsf_req_abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return -ENOMEM; |
| 526 | |
| 527 | adapter->pool.fsf_req_status_read = |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 528 | mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR, |
| 529 | sizeof(struct zfcp_fsf_req)); |
| 530 | if (!adapter->pool.fsf_req_status_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | return -ENOMEM; |
| 532 | |
| 533 | adapter->pool.data_status_read = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 534 | mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR, |
| 535 | zfcp_data.sr_buffer_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 536 | if (!adapter->pool.data_status_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return -ENOMEM; |
| 538 | |
| 539 | adapter->pool.data_gid_pn = |
Heiko Carstens | dd52e0e | 2006-09-18 22:28:49 +0200 | [diff] [blame] | 540 | mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR, |
| 541 | zfcp_data.gid_pn_cache); |
Matthew Dobson | 0eaae62a | 2006-03-26 01:37:47 -0800 | [diff] [blame] | 542 | if (!adapter->pool.data_gid_pn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return -ENOMEM; |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * zfcp_free_low_mem_buffers - free memory pools of an adapter |
| 550 | * @adapter: pointer to zfcp_adapter for which memory pools should be freed |
| 551 | * locking: zfcp_data.config_sema must be held |
| 552 | */ |
| 553 | static void |
| 554 | zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter) |
| 555 | { |
| 556 | if (adapter->pool.fsf_req_erp) |
| 557 | mempool_destroy(adapter->pool.fsf_req_erp); |
| 558 | if (adapter->pool.fsf_req_scsi) |
| 559 | mempool_destroy(adapter->pool.fsf_req_scsi); |
| 560 | if (adapter->pool.fsf_req_abort) |
| 561 | mempool_destroy(adapter->pool.fsf_req_abort); |
| 562 | if (adapter->pool.fsf_req_status_read) |
| 563 | mempool_destroy(adapter->pool.fsf_req_status_read); |
| 564 | if (adapter->pool.data_status_read) |
| 565 | mempool_destroy(adapter->pool.data_status_read); |
| 566 | if (adapter->pool.data_gid_pn) |
| 567 | mempool_destroy(adapter->pool.data_gid_pn); |
| 568 | } |
| 569 | |
Heiko Carstens | 763968e | 2007-05-10 15:45:46 +0200 | [diff] [blame] | 570 | static void zfcp_dummy_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
| 572 | return; |
| 573 | } |
| 574 | |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 575 | int zfcp_status_read_refill(struct zfcp_adapter *adapter) |
| 576 | { |
| 577 | while (atomic_read(&adapter->stat_miss) > 0) |
| 578 | if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL)) |
| 579 | break; |
| 580 | else |
| 581 | atomic_dec(&adapter->stat_miss); |
| 582 | |
| 583 | if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) { |
| 584 | zfcp_erp_adapter_reopen(adapter, 0, 103, NULL); |
| 585 | return 1; |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static void _zfcp_status_read_scheduler(struct work_struct *work) |
| 591 | { |
| 592 | zfcp_status_read_refill(container_of(work, struct zfcp_adapter, |
| 593 | stat_work)); |
| 594 | } |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | /* |
| 597 | * Enqueues an adapter at the end of the adapter list in the driver data. |
| 598 | * All adapter internal structures are set up. |
| 599 | * Proc-fs entries are also created. |
| 600 | * |
| 601 | * returns: 0 if a new adapter was successfully enqueued |
| 602 | * ZFCP_KNOWN if an adapter with this devno was already present |
| 603 | * -ENOMEM if alloc failed |
| 604 | * locks: config_sema must be held to serialise changes to the adapter list |
| 605 | */ |
| 606 | struct zfcp_adapter * |
| 607 | zfcp_adapter_enqueue(struct ccw_device *ccw_device) |
| 608 | { |
| 609 | int retval = 0; |
| 610 | struct zfcp_adapter *adapter; |
| 611 | |
| 612 | /* |
Swen Schillig | 41fa2ad | 2007-09-07 09:15:31 +0200 | [diff] [blame] | 613 | * Note: It is safe to release the list_lock, as any list changes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * are protected by the config_sema, which must be held to get here |
| 615 | */ |
| 616 | |
| 617 | /* try to allocate new adapter data structure (zeroed) */ |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 618 | adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (!adapter) { |
| 620 | ZFCP_LOG_INFO("error: allocation of base adapter " |
| 621 | "structure failed\n"); |
| 622 | goto out; |
| 623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
| 625 | ccw_device->handler = NULL; |
| 626 | |
| 627 | /* save ccw_device pointer */ |
| 628 | adapter->ccw_device = ccw_device; |
| 629 | |
| 630 | retval = zfcp_qdio_allocate_queues(adapter); |
| 631 | if (retval) |
| 632 | goto queues_alloc_failed; |
| 633 | |
| 634 | retval = zfcp_qdio_allocate(adapter); |
| 635 | if (retval) |
| 636 | goto qdio_allocate_failed; |
| 637 | |
| 638 | retval = zfcp_allocate_low_mem_buffers(adapter); |
| 639 | if (retval) { |
| 640 | ZFCP_LOG_INFO("error: pool allocation failed\n"); |
| 641 | goto failed_low_mem_buffers; |
| 642 | } |
| 643 | |
| 644 | /* initialise reference count stuff */ |
| 645 | atomic_set(&adapter->refcount, 0); |
| 646 | init_waitqueue_head(&adapter->remove_wq); |
| 647 | |
| 648 | /* initialise list of ports */ |
| 649 | INIT_LIST_HEAD(&adapter->port_list_head); |
| 650 | |
| 651 | /* initialise list of ports to be removed */ |
| 652 | INIT_LIST_HEAD(&adapter->port_remove_lh); |
| 653 | |
| 654 | /* initialize list of fsf requests */ |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 655 | spin_lock_init(&adapter->req_list_lock); |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 656 | retval = zfcp_reqlist_alloc(adapter); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 657 | if (retval) { |
| 658 | ZFCP_LOG_INFO("request list initialization failed\n"); |
| 659 | goto failed_low_mem_buffers; |
| 660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 662 | /* initialize debug locks */ |
| 663 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 664 | spin_lock_init(&adapter->hba_dbf_lock); |
| 665 | spin_lock_init(&adapter->san_dbf_lock); |
| 666 | spin_lock_init(&adapter->scsi_dbf_lock); |
Martin Peschke | d79a83d | 2008-03-27 14:22:00 +0100 | [diff] [blame] | 667 | spin_lock_init(&adapter->rec_dbf_lock); |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 668 | |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 669 | retval = zfcp_adapter_debug_register(adapter); |
| 670 | if (retval) |
| 671 | goto debug_register_failed; |
| 672 | |
Heiko Carstens | c48a29d | 2005-12-01 02:46:32 +0100 | [diff] [blame] | 673 | /* initialize error recovery stuff */ |
| 674 | |
| 675 | rwlock_init(&adapter->erp_lock); |
| 676 | sema_init(&adapter->erp_ready_sem, 0); |
| 677 | INIT_LIST_HEAD(&adapter->erp_ready_head); |
| 678 | INIT_LIST_HEAD(&adapter->erp_running_head); |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* initialize abort lock */ |
| 681 | rwlock_init(&adapter->abort_lock); |
| 682 | |
| 683 | /* initialise some erp stuff */ |
| 684 | init_waitqueue_head(&adapter->erp_thread_wqh); |
| 685 | init_waitqueue_head(&adapter->erp_done_wqh); |
| 686 | |
| 687 | /* initialize lock of associated request queue */ |
| 688 | rwlock_init(&adapter->request_queue.queue_lock); |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 689 | INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* mark adapter unusable as long as sysfs registration is not complete */ |
| 692 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | dev_set_drvdata(&ccw_device->dev, adapter); |
| 695 | |
| 696 | if (zfcp_sysfs_adapter_create_files(&ccw_device->dev)) |
| 697 | goto sysfs_failed; |
| 698 | |
| 699 | adapter->generic_services.parent = &adapter->ccw_device->dev; |
| 700 | adapter->generic_services.release = zfcp_dummy_release; |
| 701 | snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE, |
| 702 | "generic_services"); |
| 703 | |
| 704 | if (device_register(&adapter->generic_services)) |
| 705 | goto generic_services_failed; |
| 706 | |
| 707 | /* put allocated adapter at list tail */ |
| 708 | write_lock_irq(&zfcp_data.config_lock); |
| 709 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status); |
| 710 | list_add_tail(&adapter->list, &zfcp_data.adapter_list_head); |
| 711 | write_unlock_irq(&zfcp_data.config_lock); |
| 712 | |
| 713 | zfcp_data.adapters++; |
| 714 | |
| 715 | goto out; |
| 716 | |
| 717 | generic_services_failed: |
| 718 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); |
| 719 | sysfs_failed: |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 720 | zfcp_adapter_debug_unregister(adapter); |
| 721 | debug_register_failed: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | dev_set_drvdata(&ccw_device->dev, NULL); |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 723 | zfcp_reqlist_free(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | failed_low_mem_buffers: |
| 725 | zfcp_free_low_mem_buffers(adapter); |
| 726 | if (qdio_free(ccw_device) != 0) |
| 727 | ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n", |
| 728 | zfcp_get_busid_by_adapter(adapter)); |
| 729 | qdio_allocate_failed: |
| 730 | zfcp_qdio_free_queues(adapter); |
| 731 | queues_alloc_failed: |
| 732 | kfree(adapter); |
| 733 | adapter = NULL; |
| 734 | out: |
| 735 | return adapter; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * returns: 0 - struct zfcp_adapter data structure successfully removed |
| 740 | * !0 - struct zfcp_adapter data structure could not be removed |
| 741 | * (e.g. still used) |
| 742 | * locks: adapter list write lock is assumed to be held by caller |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | */ |
| 744 | void |
| 745 | zfcp_adapter_dequeue(struct zfcp_adapter *adapter) |
| 746 | { |
| 747 | int retval = 0; |
| 748 | unsigned long flags; |
| 749 | |
Swen Schillig | d26ab06 | 2008-05-19 12:17:37 +0200 | [diff] [blame] | 750 | cancel_work_sync(&adapter->stat_work); |
Michael Loehr | 9f28745 | 2007-05-09 11:01:24 +0200 | [diff] [blame] | 751 | zfcp_adapter_scsi_unregister(adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | device_unregister(&adapter->generic_services); |
| 753 | zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev); |
| 754 | dev_set_drvdata(&adapter->ccw_device->dev, NULL); |
| 755 | /* sanity check: no pending FSF requests */ |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 756 | spin_lock_irqsave(&adapter->req_list_lock, flags); |
| 757 | retval = zfcp_reqlist_isempty(adapter); |
| 758 | spin_unlock_irqrestore(&adapter->req_list_lock, flags); |
| 759 | if (!retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, " |
| 761 | "%i requests outstanding\n", |
| 762 | zfcp_get_busid_by_adapter(adapter), adapter, |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 763 | atomic_read(&adapter->reqs_active)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | retval = -EBUSY; |
| 765 | goto out; |
| 766 | } |
| 767 | |
Christof Schmitt | ff17a29 | 2007-08-28 09:31:41 +0200 | [diff] [blame] | 768 | zfcp_adapter_debug_unregister(adapter); |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | /* remove specified adapter data structure from list */ |
| 771 | write_lock_irq(&zfcp_data.config_lock); |
| 772 | list_del(&adapter->list); |
| 773 | write_unlock_irq(&zfcp_data.config_lock); |
| 774 | |
| 775 | /* decrease number of adapters in list */ |
| 776 | zfcp_data.adapters--; |
| 777 | |
| 778 | ZFCP_LOG_TRACE("adapter %s (%p) removed from list, " |
| 779 | "%i adapters still in list\n", |
| 780 | zfcp_get_busid_by_adapter(adapter), |
| 781 | adapter, zfcp_data.adapters); |
| 782 | |
| 783 | retval = qdio_free(adapter->ccw_device); |
| 784 | if (retval) |
| 785 | ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n", |
| 786 | zfcp_get_busid_by_adapter(adapter)); |
| 787 | |
| 788 | zfcp_free_low_mem_buffers(adapter); |
| 789 | /* free memory of adapter data structure and queues */ |
| 790 | zfcp_qdio_free_queues(adapter); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 791 | zfcp_reqlist_free(adapter); |
Andreas Herrmann | f6cd94b | 2006-01-05 09:59:34 +0100 | [diff] [blame] | 792 | kfree(adapter->fc_stats); |
| 793 | kfree(adapter->stats_reset_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | ZFCP_LOG_TRACE("freeing adapter structure\n"); |
| 795 | kfree(adapter); |
| 796 | out: |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * zfcp_port_enqueue - enqueue port to port list of adapter |
| 802 | * @adapter: adapter where remote port is added |
| 803 | * @wwpn: WWPN of the remote port to be enqueued |
| 804 | * @status: initial status for the port |
| 805 | * @d_id: destination id of the remote port to be enqueued |
| 806 | * Return: pointer to enqueued port on success, NULL on error |
| 807 | * Locks: config_sema must be held to serialize changes to the port list |
| 808 | * |
| 809 | * All port internal structures are set up and the sysfs entry is generated. |
| 810 | * d_id is used to enqueue ports with a well known address like the Directory |
| 811 | * Service for nameserver lookup. |
| 812 | */ |
| 813 | struct zfcp_port * |
| 814 | zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status, |
| 815 | u32 d_id) |
| 816 | { |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 817 | struct zfcp_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | int check_wwpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
| 820 | check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | /* |
| 822 | * check that there is no port with this WWPN already in list |
| 823 | */ |
| 824 | if (check_wwpn) { |
| 825 | read_lock_irq(&zfcp_data.config_lock); |
| 826 | port = zfcp_get_port_by_wwpn(adapter, wwpn); |
| 827 | read_unlock_irq(&zfcp_data.config_lock); |
| 828 | if (port) |
| 829 | return NULL; |
| 830 | } |
| 831 | |
Andreas Herrmann | ec4081c | 2006-05-22 18:17:30 +0200 | [diff] [blame] | 832 | port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | if (!port) |
| 834 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | /* initialise reference count stuff */ |
| 837 | atomic_set(&port->refcount, 0); |
| 838 | init_waitqueue_head(&port->remove_wq); |
| 839 | |
| 840 | INIT_LIST_HEAD(&port->unit_list_head); |
| 841 | INIT_LIST_HEAD(&port->unit_remove_lh); |
| 842 | |
| 843 | port->adapter = adapter; |
| 844 | |
| 845 | if (check_wwpn) |
| 846 | port->wwpn = wwpn; |
| 847 | |
| 848 | atomic_set_mask(status, &port->status); |
| 849 | |
| 850 | /* setup for sysfs registration */ |
| 851 | if (status & ZFCP_STATUS_PORT_WKA) { |
| 852 | switch (d_id) { |
| 853 | case ZFCP_DID_DIRECTORY_SERVICE: |
| 854 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 855 | "directory"); |
| 856 | break; |
| 857 | case ZFCP_DID_MANAGEMENT_SERVICE: |
| 858 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 859 | "management"); |
| 860 | break; |
| 861 | case ZFCP_DID_KEY_DISTRIBUTION_SERVICE: |
| 862 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 863 | "key_distribution"); |
| 864 | break; |
| 865 | case ZFCP_DID_ALIAS_SERVICE: |
| 866 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 867 | "alias"); |
| 868 | break; |
| 869 | case ZFCP_DID_TIME_SERVICE: |
| 870 | snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE, |
| 871 | "time"); |
| 872 | break; |
| 873 | default: |
| 874 | kfree(port); |
| 875 | return NULL; |
| 876 | } |
| 877 | port->d_id = d_id; |
| 878 | port->sysfs_device.parent = &adapter->generic_services; |
| 879 | } else { |
| 880 | snprintf(port->sysfs_device.bus_id, |
| 881 | BUS_ID_SIZE, "0x%016llx", wwpn); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 882 | port->sysfs_device.parent = &adapter->ccw_device->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
| 884 | port->sysfs_device.release = zfcp_sysfs_port_release; |
| 885 | dev_set_drvdata(&port->sysfs_device, port); |
| 886 | |
| 887 | /* mark port unusable as long as sysfs registration is not complete */ |
| 888 | atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); |
| 889 | |
| 890 | if (device_register(&port->sysfs_device)) { |
| 891 | kfree(port); |
| 892 | return NULL; |
| 893 | } |
| 894 | |
| 895 | if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) { |
| 896 | device_unregister(&port->sysfs_device); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | zfcp_port_get(port); |
| 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | write_lock_irq(&zfcp_data.config_lock); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 903 | list_add_tail(&port->list, &adapter->port_list_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status); |
| 905 | atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status); |
| 906 | if (d_id == ZFCP_DID_DIRECTORY_SERVICE) |
| 907 | if (!adapter->nameserver_port) |
| 908 | adapter->nameserver_port = port; |
| 909 | adapter->ports++; |
| 910 | write_unlock_irq(&zfcp_data.config_lock); |
| 911 | |
| 912 | zfcp_adapter_get(adapter); |
| 913 | |
| 914 | return port; |
| 915 | } |
| 916 | |
| 917 | void |
| 918 | zfcp_port_dequeue(struct zfcp_port *port) |
| 919 | { |
| 920 | zfcp_port_wait(port); |
| 921 | write_lock_irq(&zfcp_data.config_lock); |
| 922 | list_del(&port->list); |
| 923 | port->adapter->ports--; |
| 924 | write_unlock_irq(&zfcp_data.config_lock); |
Andreas Herrmann | 3859f6a | 2005-08-27 11:07:54 -0700 | [diff] [blame] | 925 | if (port->rport) |
Heiko Carstens | 20b1730 | 2005-08-28 13:22:37 -0700 | [diff] [blame] | 926 | fc_remote_port_delete(port->rport); |
| 927 | port->rport = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | zfcp_adapter_put(port->adapter); |
| 929 | zfcp_sysfs_port_remove_files(&port->sysfs_device, |
| 930 | atomic_read(&port->status)); |
| 931 | device_unregister(&port->sysfs_device); |
| 932 | } |
| 933 | |
| 934 | /* Enqueues a nameserver port */ |
| 935 | int |
| 936 | zfcp_nameserver_enqueue(struct zfcp_adapter *adapter) |
| 937 | { |
| 938 | struct zfcp_port *port; |
| 939 | |
| 940 | port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA, |
| 941 | ZFCP_DID_DIRECTORY_SERVICE); |
| 942 | if (!port) { |
| 943 | ZFCP_LOG_INFO("error: enqueue of nameserver port for " |
| 944 | "adapter %s failed\n", |
| 945 | zfcp_get_busid_by_adapter(adapter)); |
| 946 | return -ENXIO; |
| 947 | } |
| 948 | zfcp_port_put(port); |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
Christof Schmitt | 45633fd | 2008-06-10 18:20:55 +0200 | [diff] [blame^] | 953 | void zfcp_sg_free_table(struct scatterlist *sg, int count) |
| 954 | { |
| 955 | int i; |
| 956 | |
| 957 | for (i = 0; i < count; i++, sg++) |
| 958 | if (sg) |
| 959 | free_page((unsigned long) sg_virt(sg)); |
| 960 | else |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | int zfcp_sg_setup_table(struct scatterlist *sg, int count) |
| 965 | { |
| 966 | void *addr; |
| 967 | int i; |
| 968 | |
| 969 | sg_init_table(sg, count); |
| 970 | for (i = 0; i < count; i++, sg++) { |
| 971 | addr = (void *) get_zeroed_page(GFP_KERNEL); |
| 972 | if (!addr) { |
| 973 | zfcp_sg_free_table(sg, i); |
| 974 | return -ENOMEM; |
| 975 | } |
| 976 | sg_set_buf(sg, addr, PAGE_SIZE); |
| 977 | } |
| 978 | return 0; |
| 979 | } |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | #undef ZFCP_LOG_AREA |