[SCSI] libsas: enforce eh strategy handlers only in eh context
[linux-2.6.git] / drivers / scsi / libsas / sas_discover.c
blobb031d238eb7bf60b4ca03922b0ed5e778846dcd5
1 /*
2 * Serial Attached SCSI (SAS) Discover process
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <linux/scatterlist.h>
26 #include <linux/slab.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_eh.h>
29 #include "sas_internal.h"
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_sas.h>
33 #include <scsi/sas_ata.h>
34 #include "../scsi_sas_internal.h"
36 /* ---------- Basic task processing for discovery purposes ---------- */
38 void sas_init_dev(struct domain_device *dev)
40 switch (dev->dev_type) {
41 case SAS_END_DEV:
42 INIT_LIST_HEAD(&dev->ssp_dev.eh_list_node);
43 break;
44 case EDGE_DEV:
45 case FANOUT_DEV:
46 INIT_LIST_HEAD(&dev->ex_dev.children);
47 mutex_init(&dev->ex_dev.cmd_mutex);
48 break;
49 case SATA_DEV:
50 case SATA_PM:
51 case SATA_PM_PORT:
52 case SATA_PENDING:
53 INIT_LIST_HEAD(&dev->sata_dev.children);
54 break;
55 default:
56 break;
60 /* ---------- Domain device discovery ---------- */
62 /**
63 * sas_get_port_device -- Discover devices which caused port creation
64 * @port: pointer to struct sas_port of interest
66 * Devices directly attached to a HA port, have no parent. This is
67 * how we know they are (domain) "root" devices. All other devices
68 * do, and should have their "parent" pointer set appropriately as
69 * soon as a child device is discovered.
71 static int sas_get_port_device(struct asd_sas_port *port)
73 struct asd_sas_phy *phy;
74 struct sas_rphy *rphy;
75 struct domain_device *dev;
76 int rc = -ENODEV;
78 dev = sas_alloc_device();
79 if (!dev)
80 return -ENOMEM;
82 spin_lock_irq(&port->phy_list_lock);
83 if (list_empty(&port->phy_list)) {
84 spin_unlock_irq(&port->phy_list_lock);
85 sas_put_device(dev);
86 return -ENODEV;
88 phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
89 spin_lock(&phy->frame_rcvd_lock);
90 memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
91 (size_t)phy->frame_rcvd_size));
92 spin_unlock(&phy->frame_rcvd_lock);
93 spin_unlock_irq(&port->phy_list_lock);
95 if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
96 struct dev_to_host_fis *fis =
97 (struct dev_to_host_fis *) dev->frame_rcvd;
98 if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
99 fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
100 && (fis->device & ~0x10) == 0)
101 dev->dev_type = SATA_PM;
102 else
103 dev->dev_type = SATA_DEV;
104 dev->tproto = SAS_PROTOCOL_SATA;
105 } else {
106 struct sas_identify_frame *id =
107 (struct sas_identify_frame *) dev->frame_rcvd;
108 dev->dev_type = id->dev_type;
109 dev->iproto = id->initiator_bits;
110 dev->tproto = id->target_bits;
113 sas_init_dev(dev);
115 dev->port = port;
116 switch (dev->dev_type) {
117 case SATA_DEV:
118 rc = sas_ata_init(dev);
119 if (rc) {
120 rphy = NULL;
121 break;
123 /* fall through */
124 case SAS_END_DEV:
125 rphy = sas_end_device_alloc(port->port);
126 break;
127 case EDGE_DEV:
128 rphy = sas_expander_alloc(port->port,
129 SAS_EDGE_EXPANDER_DEVICE);
130 break;
131 case FANOUT_DEV:
132 rphy = sas_expander_alloc(port->port,
133 SAS_FANOUT_EXPANDER_DEVICE);
134 break;
135 default:
136 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
137 rphy = NULL;
138 break;
141 if (!rphy) {
142 sas_put_device(dev);
143 return rc;
146 rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
147 memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
148 sas_fill_in_rphy(dev, rphy);
149 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
150 port->port_dev = dev;
151 dev->linkrate = port->linkrate;
152 dev->min_linkrate = port->linkrate;
153 dev->max_linkrate = port->linkrate;
154 dev->pathways = port->num_phys;
155 memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
156 memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
157 memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
158 port->disc.max_level = 0;
159 sas_device_set_phy(dev, port->port);
161 dev->rphy = rphy;
162 get_device(&dev->rphy->dev);
164 if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEV)
165 list_add_tail(&dev->disco_list_node, &port->disco_list);
166 else {
167 spin_lock_irq(&port->dev_list_lock);
168 list_add_tail(&dev->dev_list_node, &port->dev_list);
169 spin_unlock_irq(&port->dev_list_lock);
172 spin_lock_irq(&port->phy_list_lock);
173 list_for_each_entry(phy, &port->phy_list, port_phy_el)
174 sas_phy_set_target(phy, dev);
175 spin_unlock_irq(&port->phy_list_lock);
177 return 0;
180 /* ---------- Discover and Revalidate ---------- */
182 int sas_notify_lldd_dev_found(struct domain_device *dev)
184 int res = 0;
185 struct sas_ha_struct *sas_ha = dev->port->ha;
186 struct Scsi_Host *shost = sas_ha->core.shost;
187 struct sas_internal *i = to_sas_internal(shost->transportt);
189 if (i->dft->lldd_dev_found) {
190 res = i->dft->lldd_dev_found(dev);
191 if (res) {
192 printk("sas: driver on pcidev %s cannot handle "
193 "device %llx, error:%d\n",
194 dev_name(sas_ha->dev),
195 SAS_ADDR(dev->sas_addr), res);
197 kref_get(&dev->kref);
199 return res;
203 void sas_notify_lldd_dev_gone(struct domain_device *dev)
205 struct sas_ha_struct *sas_ha = dev->port->ha;
206 struct Scsi_Host *shost = sas_ha->core.shost;
207 struct sas_internal *i = to_sas_internal(shost->transportt);
209 if (i->dft->lldd_dev_gone) {
210 i->dft->lldd_dev_gone(dev);
211 sas_put_device(dev);
215 static void sas_probe_devices(struct work_struct *work)
217 struct domain_device *dev, *n;
218 struct sas_discovery_event *ev = to_sas_discovery_event(work);
219 struct asd_sas_port *port = ev->port;
221 clear_bit(DISCE_PROBE, &port->disc.pending);
223 /* devices must be domain members before link recovery and probe */
224 list_for_each_entry(dev, &port->disco_list, disco_list_node) {
225 spin_lock_irq(&port->dev_list_lock);
226 list_add_tail(&dev->dev_list_node, &port->dev_list);
227 spin_unlock_irq(&port->dev_list_lock);
230 sas_probe_sata(port);
232 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
233 int err;
235 err = sas_rphy_add(dev->rphy);
236 if (err)
237 sas_fail_probe(dev, __func__, err);
238 else
239 list_del_init(&dev->disco_list_node);
244 * sas_discover_end_dev -- discover an end device (SSP, etc)
245 * @end: pointer to domain device of interest
247 * See comment in sas_discover_sata().
249 int sas_discover_end_dev(struct domain_device *dev)
251 int res;
253 res = sas_notify_lldd_dev_found(dev);
254 if (res)
255 return res;
256 sas_discover_event(dev->port, DISCE_PROBE);
258 return 0;
261 /* ---------- Device registration and unregistration ---------- */
263 void sas_free_device(struct kref *kref)
265 struct domain_device *dev = container_of(kref, typeof(*dev), kref);
267 put_device(&dev->rphy->dev);
268 dev->rphy = NULL;
270 if (dev->parent)
271 sas_put_device(dev->parent);
273 sas_port_put_phy(dev->phy);
274 dev->phy = NULL;
276 /* remove the phys and ports, everything else should be gone */
277 if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV)
278 kfree(dev->ex_dev.ex_phy);
280 if (dev_is_sata(dev) && dev->sata_dev.ap) {
281 ata_sas_port_destroy(dev->sata_dev.ap);
282 dev->sata_dev.ap = NULL;
285 kfree(dev);
288 static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
290 struct sas_ha_struct *ha = port->ha;
292 sas_notify_lldd_dev_gone(dev);
293 if (!dev->parent)
294 dev->port->port_dev = NULL;
295 else
296 list_del_init(&dev->siblings);
298 spin_lock_irq(&port->dev_list_lock);
299 list_del_init(&dev->dev_list_node);
300 if (dev_is_sata(dev))
301 sas_ata_end_eh(dev->sata_dev.ap);
302 spin_unlock_irq(&port->dev_list_lock);
304 spin_lock_irq(&ha->lock);
305 if (dev->dev_type == SAS_END_DEV &&
306 !list_empty(&dev->ssp_dev.eh_list_node)) {
307 list_del_init(&dev->ssp_dev.eh_list_node);
308 ha->eh_active--;
310 spin_unlock_irq(&ha->lock);
312 sas_put_device(dev);
315 static void sas_destruct_devices(struct work_struct *work)
317 struct domain_device *dev, *n;
318 struct sas_discovery_event *ev = to_sas_discovery_event(work);
319 struct asd_sas_port *port = ev->port;
321 clear_bit(DISCE_DESTRUCT, &port->disc.pending);
323 list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
324 list_del_init(&dev->disco_list_node);
326 sas_remove_children(&dev->rphy->dev);
327 sas_rphy_delete(dev->rphy);
328 sas_unregister_common_dev(port, dev);
332 void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
334 if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
335 !list_empty(&dev->disco_list_node)) {
336 /* this rphy never saw sas_rphy_add */
337 list_del_init(&dev->disco_list_node);
338 sas_rphy_free(dev->rphy);
339 sas_unregister_common_dev(port, dev);
340 return;
343 if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
344 sas_rphy_unlink(dev->rphy);
345 list_move_tail(&dev->disco_list_node, &port->destroy_list);
346 sas_discover_event(dev->port, DISCE_DESTRUCT);
350 void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
352 struct domain_device *dev, *n;
354 list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
355 if (gone)
356 set_bit(SAS_DEV_GONE, &dev->state);
357 sas_unregister_dev(port, dev);
360 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
361 sas_unregister_dev(port, dev);
363 port->port->rphy = NULL;
367 void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
369 struct sas_ha_struct *ha;
370 struct sas_phy *new_phy;
372 if (!dev)
373 return;
375 ha = dev->port->ha;
376 new_phy = sas_port_get_phy(port);
378 /* pin and record last seen phy */
379 spin_lock_irq(&ha->phy_port_lock);
380 if (new_phy) {
381 sas_port_put_phy(dev->phy);
382 dev->phy = new_phy;
384 spin_unlock_irq(&ha->phy_port_lock);
387 /* ---------- Discovery and Revalidation ---------- */
390 * sas_discover_domain -- discover the domain
391 * @port: port to the domain of interest
393 * NOTE: this process _must_ quit (return) as soon as any connection
394 * errors are encountered. Connection recovery is done elsewhere.
395 * Discover process only interrogates devices in order to discover the
396 * domain.
398 static void sas_discover_domain(struct work_struct *work)
400 struct domain_device *dev;
401 int error = 0;
402 struct sas_discovery_event *ev = to_sas_discovery_event(work);
403 struct asd_sas_port *port = ev->port;
405 clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
407 if (port->port_dev)
408 return;
410 error = sas_get_port_device(port);
411 if (error)
412 return;
413 dev = port->port_dev;
415 SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
416 task_pid_nr(current));
418 switch (dev->dev_type) {
419 case SAS_END_DEV:
420 error = sas_discover_end_dev(dev);
421 break;
422 case EDGE_DEV:
423 case FANOUT_DEV:
424 error = sas_discover_root_expander(dev);
425 break;
426 case SATA_DEV:
427 case SATA_PM:
428 #ifdef CONFIG_SCSI_SAS_ATA
429 error = sas_discover_sata(dev);
430 break;
431 #else
432 SAS_DPRINTK("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
433 /* Fall through */
434 #endif
435 default:
436 error = -ENXIO;
437 SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
438 break;
441 if (error) {
442 sas_rphy_free(dev->rphy);
443 list_del_init(&dev->disco_list_node);
444 spin_lock_irq(&port->dev_list_lock);
445 list_del_init(&dev->dev_list_node);
446 spin_unlock_irq(&port->dev_list_lock);
448 sas_put_device(dev);
449 port->port_dev = NULL;
452 SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
453 task_pid_nr(current), error);
456 static void sas_revalidate_domain(struct work_struct *work)
458 int res = 0;
459 struct sas_discovery_event *ev = to_sas_discovery_event(work);
460 struct asd_sas_port *port = ev->port;
461 struct sas_ha_struct *ha = port->ha;
463 /* prevent revalidation from finding sata links in recovery */
464 mutex_lock(&ha->disco_mutex);
465 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
466 SAS_DPRINTK("REVALIDATION DEFERRED on port %d, pid:%d\n",
467 port->id, task_pid_nr(current));
468 goto out;
471 clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
473 SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
474 task_pid_nr(current));
476 if (port->port_dev)
477 res = sas_ex_revalidate_domain(port->port_dev);
479 SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
480 port->id, task_pid_nr(current), res);
481 out:
482 mutex_unlock(&ha->disco_mutex);
485 /* ---------- Events ---------- */
487 static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
489 /* chained work is not subject to SA_HA_DRAINING or
490 * SAS_HA_REGISTERED, because it is either submitted in the
491 * workqueue, or known to be submitted from a context that is
492 * not racing against draining
494 scsi_queue_work(ha->core.shost, &sw->work);
497 static void sas_chain_event(int event, unsigned long *pending,
498 struct sas_work *sw,
499 struct sas_ha_struct *ha)
501 if (!test_and_set_bit(event, pending)) {
502 unsigned long flags;
504 spin_lock_irqsave(&ha->lock, flags);
505 sas_chain_work(ha, sw);
506 spin_unlock_irqrestore(&ha->lock, flags);
510 int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
512 struct sas_discovery *disc;
514 if (!port)
515 return 0;
516 disc = &port->disc;
518 BUG_ON(ev >= DISC_NUM_EVENTS);
520 sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
522 return 0;
526 * sas_init_disc -- initialize the discovery struct in the port
527 * @port: pointer to struct port
529 * Called when the ports are being initialized.
531 void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
533 int i;
535 static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
536 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
537 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
538 [DISCE_PROBE] = sas_probe_devices,
539 [DISCE_DESTRUCT] = sas_destruct_devices,
542 disc->pending = 0;
543 for (i = 0; i < DISC_NUM_EVENTS; i++) {
544 INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
545 disc->disc_work[i].port = port;