blob: 3e9dc1a84358a540dd5ad4324b63fd4f9bf98039 [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
2 * Serial Attached SCSI (SAS) Discover process
3 *
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6 *
7 * This file is licensed under GPLv2.
8 *
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.
13 *
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
James Bottomley2908d772006-08-29 09:22:51 -050025#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
James Bottomley2908d772006-08-29 09:22:51 -050027#include <scsi/scsi_host.h>
28#include <scsi/scsi_eh.h>
29#include "sas_internal.h"
30
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_transport_sas.h>
Dan Williams756f1732011-11-17 17:59:48 -080033#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050034#include "../scsi_sas_internal.h"
35
36/* ---------- Basic task processing for discovery purposes ---------- */
37
38void sas_init_dev(struct domain_device *dev)
39{
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000040 switch (dev->dev_type) {
41 case SAS_END_DEV:
Dan Williams5db45bd2012-06-21 23:30:48 -070042 INIT_LIST_HEAD(&dev->ssp_dev.eh_list_node);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000043 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;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000049 default:
50 break;
51 }
James Bottomley2908d772006-08-29 09:22:51 -050052}
53
James Bottomley2908d772006-08-29 09:22:51 -050054/* ---------- Domain device discovery ---------- */
55
56/**
57 * sas_get_port_device -- Discover devices which caused port creation
58 * @port: pointer to struct sas_port of interest
59 *
60 * Devices directly attached to a HA port, have no parent. This is
61 * how we know they are (domain) "root" devices. All other devices
62 * do, and should have their "parent" pointer set appropriately as
63 * soon as a child device is discovered.
64 */
65static int sas_get_port_device(struct asd_sas_port *port)
66{
James Bottomley2908d772006-08-29 09:22:51 -050067 struct asd_sas_phy *phy;
68 struct sas_rphy *rphy;
69 struct domain_device *dev;
Dan Williamsb2024452012-03-21 21:09:07 -070070 int rc = -ENODEV;
James Bottomley2908d772006-08-29 09:22:51 -050071
Dan Williams735f7d22011-11-17 17:59:47 -080072 dev = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -050073 if (!dev)
74 return -ENOMEM;
75
Dan Williams899fcf42012-01-28 17:24:40 -080076 spin_lock_irq(&port->phy_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -050077 if (list_empty(&port->phy_list)) {
Dan Williams899fcf42012-01-28 17:24:40 -080078 spin_unlock_irq(&port->phy_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -080079 sas_put_device(dev);
James Bottomley2908d772006-08-29 09:22:51 -050080 return -ENODEV;
81 }
82 phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
83 spin_lock(&phy->frame_rcvd_lock);
84 memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
85 (size_t)phy->frame_rcvd_size));
86 spin_unlock(&phy->frame_rcvd_lock);
Dan Williams899fcf42012-01-28 17:24:40 -080087 spin_unlock_irq(&port->phy_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -050088
89 if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
90 struct dev_to_host_fis *fis =
91 (struct dev_to_host_fis *) dev->frame_rcvd;
92 if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
93 fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
94 && (fis->device & ~0x10) == 0)
95 dev->dev_type = SATA_PM;
96 else
97 dev->dev_type = SATA_DEV;
Darrick J. Wong5929faf2007-11-05 11:51:17 -080098 dev->tproto = SAS_PROTOCOL_SATA;
James Bottomley2908d772006-08-29 09:22:51 -050099 } else {
100 struct sas_identify_frame *id =
101 (struct sas_identify_frame *) dev->frame_rcvd;
102 dev->dev_type = id->dev_type;
103 dev->iproto = id->initiator_bits;
104 dev->tproto = id->target_bits;
105 }
106
107 sas_init_dev(dev);
108
Dan Williamsb2024452012-03-21 21:09:07 -0700109 dev->port = port;
James Bottomley2908d772006-08-29 09:22:51 -0500110 switch (dev->dev_type) {
Darrick J. Wongfa1c1e82006-08-10 19:19:47 -0700111 case SATA_DEV:
Dan Williamsb2024452012-03-21 21:09:07 -0700112 rc = sas_ata_init(dev);
113 if (rc) {
114 rphy = NULL;
115 break;
116 }
117 /* fall through */
118 case SAS_END_DEV:
James Bottomley2908d772006-08-29 09:22:51 -0500119 rphy = sas_end_device_alloc(port->port);
120 break;
121 case EDGE_DEV:
122 rphy = sas_expander_alloc(port->port,
123 SAS_EDGE_EXPANDER_DEVICE);
124 break;
125 case FANOUT_DEV:
126 rphy = sas_expander_alloc(port->port,
127 SAS_FANOUT_EXPANDER_DEVICE);
128 break;
James Bottomley2908d772006-08-29 09:22:51 -0500129 default:
130 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
131 rphy = NULL;
132 break;
133 }
134
135 if (!rphy) {
Dan Williams735f7d22011-11-17 17:59:47 -0800136 sas_put_device(dev);
Dan Williamsb2024452012-03-21 21:09:07 -0700137 return rc;
James Bottomley2908d772006-08-29 09:22:51 -0500138 }
Dan Williams899fcf42012-01-28 17:24:40 -0800139
James Bottomley2908d772006-08-29 09:22:51 -0500140 rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
141 memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
142 sas_fill_in_rphy(dev, rphy);
143 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
144 port->port_dev = dev;
James Bottomley2908d772006-08-29 09:22:51 -0500145 dev->linkrate = port->linkrate;
146 dev->min_linkrate = port->linkrate;
147 dev->max_linkrate = port->linkrate;
148 dev->pathways = port->num_phys;
149 memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
150 memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
151 memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
152 port->disc.max_level = 0;
Dan Williamsf41a0c42011-12-21 21:33:17 -0800153 sas_device_set_phy(dev, port->port);
James Bottomley2908d772006-08-29 09:22:51 -0500154
155 dev->rphy = rphy;
Dan Williams94876692012-03-20 10:53:24 -0700156 get_device(&dev->rphy->dev);
Dan Williams87c83312011-11-17 17:59:51 -0800157
Dan Williams92625f92012-01-18 20:14:01 -0800158 if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEV)
Dan Williams87c83312011-11-17 17:59:51 -0800159 list_add_tail(&dev->disco_list_node, &port->disco_list);
160 else {
161 spin_lock_irq(&port->dev_list_lock);
162 list_add_tail(&dev->dev_list_node, &port->dev_list);
163 spin_unlock_irq(&port->dev_list_lock);
164 }
James Bottomley2908d772006-08-29 09:22:51 -0500165
Dan Williamsec236e522012-03-12 11:38:26 -0700166 spin_lock_irq(&port->phy_list_lock);
167 list_for_each_entry(phy, &port->phy_list, port_phy_el)
168 sas_phy_set_target(phy, dev);
169 spin_unlock_irq(&port->phy_list_lock);
170
James Bottomley2908d772006-08-29 09:22:51 -0500171 return 0;
172}
173
174/* ---------- Discover and Revalidate ---------- */
175
James Bottomley2908d772006-08-29 09:22:51 -0500176int sas_notify_lldd_dev_found(struct domain_device *dev)
177{
178 int res = 0;
179 struct sas_ha_struct *sas_ha = dev->port->ha;
180 struct Scsi_Host *shost = sas_ha->core.shost;
181 struct sas_internal *i = to_sas_internal(shost->transportt);
182
183 if (i->dft->lldd_dev_found) {
184 res = i->dft->lldd_dev_found(dev);
185 if (res) {
186 printk("sas: driver on pcidev %s cannot handle "
187 "device %llx, error:%d\n",
Kay Sievers71610f52008-12-03 22:41:36 +0100188 dev_name(sas_ha->dev),
James Bottomley2908d772006-08-29 09:22:51 -0500189 SAS_ADDR(dev->sas_addr), res);
190 }
Dan Williams735f7d22011-11-17 17:59:47 -0800191 kref_get(&dev->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500192 }
193 return res;
194}
195
196
197void sas_notify_lldd_dev_gone(struct domain_device *dev)
198{
199 struct sas_ha_struct *sas_ha = dev->port->ha;
200 struct Scsi_Host *shost = sas_ha->core.shost;
201 struct sas_internal *i = to_sas_internal(shost->transportt);
202
Dan Williams735f7d22011-11-17 17:59:47 -0800203 if (i->dft->lldd_dev_gone) {
James Bottomley2908d772006-08-29 09:22:51 -0500204 i->dft->lldd_dev_gone(dev);
Dan Williams735f7d22011-11-17 17:59:47 -0800205 sas_put_device(dev);
206 }
James Bottomley2908d772006-08-29 09:22:51 -0500207}
208
Dan Williams92625f92012-01-18 20:14:01 -0800209static void sas_probe_devices(struct work_struct *work)
210{
211 struct domain_device *dev, *n;
Dan Williams22b91532012-03-09 11:00:06 -0800212 struct sas_discovery_event *ev = to_sas_discovery_event(work);
Dan Williams92625f92012-01-18 20:14:01 -0800213 struct asd_sas_port *port = ev->port;
James Bottomley2908d772006-08-29 09:22:51 -0500214
Dan Williams92625f92012-01-18 20:14:01 -0800215 clear_bit(DISCE_PROBE, &port->disc.pending);
216
Dan Williams9508a662012-01-18 20:47:01 -0800217 /* devices must be domain members before link recovery and probe */
218 list_for_each_entry(dev, &port->disco_list, disco_list_node) {
Dan Williams92625f92012-01-18 20:14:01 -0800219 spin_lock_irq(&port->dev_list_lock);
220 list_add_tail(&dev->dev_list_node, &port->dev_list);
221 spin_unlock_irq(&port->dev_list_lock);
Dan Williams9508a662012-01-18 20:47:01 -0800222 }
223
224 sas_probe_sata(port);
225
226 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
227 int err;
Dan Williams92625f92012-01-18 20:14:01 -0800228
229 err = sas_rphy_add(dev->rphy);
Dan Williams9508a662012-01-18 20:47:01 -0800230 if (err)
231 sas_fail_probe(dev, __func__, err);
232 else
Dan Williams92625f92012-01-18 20:14:01 -0800233 list_del_init(&dev->disco_list_node);
234 }
235}
James Bottomley2908d772006-08-29 09:22:51 -0500236
237/**
238 * sas_discover_end_dev -- discover an end device (SSP, etc)
239 * @end: pointer to domain device of interest
240 *
241 * See comment in sas_discover_sata().
242 */
243int sas_discover_end_dev(struct domain_device *dev)
244{
245 int res;
246
247 res = sas_notify_lldd_dev_found(dev);
248 if (res)
Dan Williams92625f92012-01-18 20:14:01 -0800249 return res;
250 sas_discover_event(dev->port, DISCE_PROBE);
James Bottomley2908d772006-08-29 09:22:51 -0500251
James Bottomley2908d772006-08-29 09:22:51 -0500252 return 0;
James Bottomley2908d772006-08-29 09:22:51 -0500253}
254
255/* ---------- Device registration and unregistration ---------- */
256
Dan Williams735f7d22011-11-17 17:59:47 -0800257void sas_free_device(struct kref *kref)
258{
259 struct domain_device *dev = container_of(kref, typeof(*dev), kref);
260
Dan Williams94876692012-03-20 10:53:24 -0700261 put_device(&dev->rphy->dev);
262 dev->rphy = NULL;
263
Dan Williams735f7d22011-11-17 17:59:47 -0800264 if (dev->parent)
265 sas_put_device(dev->parent);
266
Dan Williamsf41a0c42011-12-21 21:33:17 -0800267 sas_port_put_phy(dev->phy);
268 dev->phy = NULL;
269
Dan Williams735f7d22011-11-17 17:59:47 -0800270 /* remove the phys and ports, everything else should be gone */
271 if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV)
272 kfree(dev->ex_dev.ex_phy);
273
Dan Williams8abda4d2012-01-10 15:14:09 -0800274 if (dev_is_sata(dev) && dev->sata_dev.ap) {
275 ata_sas_port_destroy(dev->sata_dev.ap);
276 dev->sata_dev.ap = NULL;
277 }
278
Dan Williams735f7d22011-11-17 17:59:47 -0800279 kfree(dev);
280}
281
Dan Williams1a34c062011-09-21 22:05:34 -0700282static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -0500283{
Dan Williams5db45bd2012-06-21 23:30:48 -0700284 struct sas_ha_struct *ha = port->ha;
285
James Bottomley2908d772006-08-29 09:22:51 -0500286 sas_notify_lldd_dev_gone(dev);
287 if (!dev->parent)
288 dev->port->port_dev = NULL;
289 else
290 list_del_init(&dev->siblings);
Dan Williams1a34c062011-09-21 22:05:34 -0700291
292 spin_lock_irq(&port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500293 list_del_init(&dev->dev_list_node);
Dan Williamse4a9c372012-06-21 23:25:27 -0700294 if (dev_is_sata(dev))
295 sas_ata_end_eh(dev->sata_dev.ap);
Dan Williams1a34c062011-09-21 22:05:34 -0700296 spin_unlock_irq(&port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800297
Dan Williams5db45bd2012-06-21 23:30:48 -0700298 spin_lock_irq(&ha->lock);
299 if (dev->dev_type == SAS_END_DEV &&
300 !list_empty(&dev->ssp_dev.eh_list_node)) {
301 list_del_init(&dev->ssp_dev.eh_list_node);
302 ha->eh_active--;
303 }
304 spin_unlock_irq(&ha->lock);
305
Dan Williams735f7d22011-11-17 17:59:47 -0800306 sas_put_device(dev);
James Bottomley2908d772006-08-29 09:22:51 -0500307}
308
Dan Williams87c83312011-11-17 17:59:51 -0800309static void sas_destruct_devices(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500310{
Dan Williams87c83312011-11-17 17:59:51 -0800311 struct domain_device *dev, *n;
Dan Williams22b91532012-03-09 11:00:06 -0800312 struct sas_discovery_event *ev = to_sas_discovery_event(work);
Dan Williams87c83312011-11-17 17:59:51 -0800313 struct asd_sas_port *port = ev->port;
314
315 clear_bit(DISCE_DESTRUCT, &port->disc.pending);
316
317 list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
318 list_del_init(&dev->disco_list_node);
319
James Bottomley2908d772006-08-29 09:22:51 -0500320 sas_remove_children(&dev->rphy->dev);
321 sas_rphy_delete(dev->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800322 sas_unregister_common_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500323 }
Dan Williams87c83312011-11-17 17:59:51 -0800324}
325
326void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
327{
328 if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
329 !list_empty(&dev->disco_list_node)) {
330 /* this rphy never saw sas_rphy_add */
331 list_del_init(&dev->disco_list_node);
332 sas_rphy_free(dev->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800333 sas_unregister_common_dev(port, dev);
Dan Williams94876692012-03-20 10:53:24 -0700334 return;
Dan Williams87c83312011-11-17 17:59:51 -0800335 }
336
Dan Williams94876692012-03-20 10:53:24 -0700337 if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
Dan Williams87c83312011-11-17 17:59:51 -0800338 sas_rphy_unlink(dev->rphy);
339 list_move_tail(&dev->disco_list_node, &port->destroy_list);
340 sas_discover_event(dev->port, DISCE_DESTRUCT);
341 }
James Bottomley2908d772006-08-29 09:22:51 -0500342}
343
Dan Williams7d059192012-01-10 14:39:13 -0800344void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
James Bottomley2908d772006-08-29 09:22:51 -0500345{
346 struct domain_device *dev, *n;
347
Dan Williams7d059192012-01-10 14:39:13 -0800348 list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
349 if (gone)
350 set_bit(SAS_DEV_GONE, &dev->state);
Dan Williams1a34c062011-09-21 22:05:34 -0700351 sas_unregister_dev(port, dev);
Dan Williams7d059192012-01-10 14:39:13 -0800352 }
353
Dan Williams87c83312011-11-17 17:59:51 -0800354 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
355 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -0500356
357 port->port->rphy = NULL;
358
359}
360
Dan Williamsf41a0c42011-12-21 21:33:17 -0800361void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
362{
363 struct sas_ha_struct *ha;
364 struct sas_phy *new_phy;
365
366 if (!dev)
367 return;
368
369 ha = dev->port->ha;
370 new_phy = sas_port_get_phy(port);
371
372 /* pin and record last seen phy */
373 spin_lock_irq(&ha->phy_port_lock);
374 if (new_phy) {
375 sas_port_put_phy(dev->phy);
376 dev->phy = new_phy;
377 }
378 spin_unlock_irq(&ha->phy_port_lock);
379}
380
James Bottomley2908d772006-08-29 09:22:51 -0500381/* ---------- Discovery and Revalidation ---------- */
382
383/**
384 * sas_discover_domain -- discover the domain
385 * @port: port to the domain of interest
386 *
387 * NOTE: this process _must_ quit (return) as soon as any connection
388 * errors are encountered. Connection recovery is done elsewhere.
389 * Discover process only interrogates devices in order to discover the
390 * domain.
391 */
David Howellsc4028952006-11-22 14:57:56 +0000392static void sas_discover_domain(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500393{
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800394 struct domain_device *dev;
James Bottomley2908d772006-08-29 09:22:51 -0500395 int error = 0;
Dan Williams22b91532012-03-09 11:00:06 -0800396 struct sas_discovery_event *ev = to_sas_discovery_event(work);
David Howellsc4028952006-11-22 14:57:56 +0000397 struct asd_sas_port *port = ev->port;
James Bottomley2908d772006-08-29 09:22:51 -0500398
Dan Williamsb15ebe02011-11-17 17:59:49 -0800399 clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
James Bottomley2908d772006-08-29 09:22:51 -0500400
401 if (port->port_dev)
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800402 return;
403
404 error = sas_get_port_device(port);
405 if (error)
406 return;
407 dev = port->port_dev;
James Bottomley2908d772006-08-29 09:22:51 -0500408
409 SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700410 task_pid_nr(current));
James Bottomley2908d772006-08-29 09:22:51 -0500411
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800412 switch (dev->dev_type) {
James Bottomley2908d772006-08-29 09:22:51 -0500413 case SAS_END_DEV:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800414 error = sas_discover_end_dev(dev);
James Bottomley2908d772006-08-29 09:22:51 -0500415 break;
416 case EDGE_DEV:
417 case FANOUT_DEV:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800418 error = sas_discover_root_expander(dev);
James Bottomley2908d772006-08-29 09:22:51 -0500419 break;
420 case SATA_DEV:
421 case SATA_PM:
James Bottomley15c73d52008-03-26 09:26:13 -0700422#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800423 error = sas_discover_sata(dev);
James Bottomley2908d772006-08-29 09:22:51 -0500424 break;
James Bottomley15c73d52008-03-26 09:26:13 -0700425#else
426 SAS_DPRINTK("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
427 /* Fall through */
James Bottomleyb9142172007-07-22 13:15:55 -0500428#endif
James Bottomley2908d772006-08-29 09:22:51 -0500429 default:
James Bottomleyb9142172007-07-22 13:15:55 -0500430 error = -ENXIO;
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800431 SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
James Bottomley2908d772006-08-29 09:22:51 -0500432 break;
433 }
434
435 if (error) {
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800436 sas_rphy_free(dev->rphy);
Dan Williams87c83312011-11-17 17:59:51 -0800437 list_del_init(&dev->disco_list_node);
James Bottomley9d720d82007-07-16 13:15:51 -0500438 spin_lock_irq(&port->dev_list_lock);
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800439 list_del_init(&dev->dev_list_node);
James Bottomley9d720d82007-07-16 13:15:51 -0500440 spin_unlock_irq(&port->dev_list_lock);
Darrick J. Wong88808392007-01-11 14:14:49 -0800441
Dan Williams735f7d22011-11-17 17:59:47 -0800442 sas_put_device(dev);
James Bottomley2908d772006-08-29 09:22:51 -0500443 port->port_dev = NULL;
444 }
445
446 SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700447 task_pid_nr(current), error);
James Bottomley2908d772006-08-29 09:22:51 -0500448}
449
David Howellsc4028952006-11-22 14:57:56 +0000450static void sas_revalidate_domain(struct work_struct *work)
James Bottomley2908d772006-08-29 09:22:51 -0500451{
452 int res = 0;
Dan Williams22b91532012-03-09 11:00:06 -0800453 struct sas_discovery_event *ev = to_sas_discovery_event(work);
David Howellsc4028952006-11-22 14:57:56 +0000454 struct asd_sas_port *port = ev->port;
Dan Williams87c83312011-11-17 17:59:51 -0800455 struct sas_ha_struct *ha = port->ha;
456
457 /* prevent revalidation from finding sata links in recovery */
458 mutex_lock(&ha->disco_mutex);
459 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
460 SAS_DPRINTK("REVALIDATION DEFERRED on port %d, pid:%d\n",
461 port->id, task_pid_nr(current));
462 goto out;
463 }
James Bottomley2908d772006-08-29 09:22:51 -0500464
Dan Williamsb15ebe02011-11-17 17:59:49 -0800465 clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
James Bottomley2908d772006-08-29 09:22:51 -0500466
467 SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700468 task_pid_nr(current));
Dan Williams87c83312011-11-17 17:59:51 -0800469
James Bottomley2908d772006-08-29 09:22:51 -0500470 if (port->port_dev)
471 res = sas_ex_revalidate_domain(port->port_dev);
472
473 SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700474 port->id, task_pid_nr(current), res);
Dan Williams87c83312011-11-17 17:59:51 -0800475 out:
476 mutex_unlock(&ha->disco_mutex);
James Bottomley2908d772006-08-29 09:22:51 -0500477}
478
479/* ---------- Events ---------- */
480
Dan Williams22b91532012-03-09 11:00:06 -0800481static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
Dan Williamsb1124cd2011-12-19 16:42:34 -0800482{
Dan Williams22b91532012-03-09 11:00:06 -0800483 /* chained work is not subject to SA_HA_DRAINING or
484 * SAS_HA_REGISTERED, because it is either submitted in the
485 * workqueue, or known to be submitted from a context that is
486 * not racing against draining
487 */
488 scsi_queue_work(ha->core.shost, &sw->work);
Dan Williamsb1124cd2011-12-19 16:42:34 -0800489}
490
491static void sas_chain_event(int event, unsigned long *pending,
Dan Williams22b91532012-03-09 11:00:06 -0800492 struct sas_work *sw,
Dan Williamsb1124cd2011-12-19 16:42:34 -0800493 struct sas_ha_struct *ha)
494{
495 if (!test_and_set_bit(event, pending)) {
496 unsigned long flags;
497
Dan Williamse4a9c372012-06-21 23:25:27 -0700498 spin_lock_irqsave(&ha->lock, flags);
Dan Williams22b91532012-03-09 11:00:06 -0800499 sas_chain_work(ha, sw);
Dan Williamse4a9c372012-06-21 23:25:27 -0700500 spin_unlock_irqrestore(&ha->lock, flags);
Dan Williamsb1124cd2011-12-19 16:42:34 -0800501 }
502}
503
James Bottomley2908d772006-08-29 09:22:51 -0500504int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
505{
506 struct sas_discovery *disc;
507
508 if (!port)
509 return 0;
510 disc = &port->disc;
511
512 BUG_ON(ev >= DISC_NUM_EVENTS);
513
Dan Williamsb1124cd2011-12-19 16:42:34 -0800514 sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
James Bottomley2908d772006-08-29 09:22:51 -0500515
516 return 0;
517}
518
519/**
520 * sas_init_disc -- initialize the discovery struct in the port
521 * @port: pointer to struct port
522 *
523 * Called when the ports are being initialized.
524 */
525void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
526{
527 int i;
528
David Howellsc4028952006-11-22 14:57:56 +0000529 static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
James Bottomley2908d772006-08-29 09:22:51 -0500530 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
531 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
Dan Williams92625f92012-01-18 20:14:01 -0800532 [DISCE_PROBE] = sas_probe_devices,
Dan Williams87c83312011-11-17 17:59:51 -0800533 [DISCE_DESTRUCT] = sas_destruct_devices,
James Bottomley2908d772006-08-29 09:22:51 -0500534 };
535
James Bottomley2908d772006-08-29 09:22:51 -0500536 disc->pending = 0;
David Howellsc4028952006-11-22 14:57:56 +0000537 for (i = 0; i < DISC_NUM_EVENTS; i++) {
Dan Williams22b91532012-03-09 11:00:06 -0800538 INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
David Howellsc4028952006-11-22 14:57:56 +0000539 disc->disc_work[i].port = port;
540 }
James Bottomley2908d772006-08-29 09:22:51 -0500541}