Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[deliverable/linux.git] / drivers / scsi / libsas / sas_discover.c
CommitLineData
2908d778
JB
1/*
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
2908d778 25#include <linux/scatterlist.h>
5a0e3ad6 26#include <linux/slab.h>
2908d778
JB
27#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>
756f173f 33#include <scsi/sas_ata.h>
2908d778
JB
34#include "../scsi_sas_internal.h"
35
36/* ---------- Basic task processing for discovery purposes ---------- */
37
38void sas_init_dev(struct domain_device *dev)
39{
89d3cf6a
JS
40 switch (dev->dev_type) {
41 case SAS_END_DEV:
5db45bdc 42 INIT_LIST_HEAD(&dev->ssp_dev.eh_list_node);
89d3cf6a
JS
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;
89d3cf6a
JS
49 default:
50 break;
51 }
2908d778
JB
52}
53
2908d778
JB
54/* ---------- 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{
2908d778
JB
67 struct asd_sas_phy *phy;
68 struct sas_rphy *rphy;
69 struct domain_device *dev;
b2024459 70 int rc = -ENODEV;
2908d778 71
735f7d2f 72 dev = sas_alloc_device();
2908d778
JB
73 if (!dev)
74 return -ENOMEM;
75
899fcf40 76 spin_lock_irq(&port->phy_list_lock);
2908d778 77 if (list_empty(&port->phy_list)) {
899fcf40 78 spin_unlock_irq(&port->phy_list_lock);
735f7d2f 79 sas_put_device(dev);
2908d778
JB
80 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);
899fcf40 87 spin_unlock_irq(&port->phy_list_lock);
2908d778
JB
88
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;
5929faf3 98 dev->tproto = SAS_PROTOCOL_SATA;
2908d778
JB
99 } 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
b2024459 109 dev->port = port;
2908d778 110 switch (dev->dev_type) {
fa1c1e8f 111 case SATA_DEV:
b2024459
DW
112 rc = sas_ata_init(dev);
113 if (rc) {
114 rphy = NULL;
115 break;
116 }
117 /* fall through */
118 case SAS_END_DEV:
2908d778
JB
119 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;
2908d778
JB
129 default:
130 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
131 rphy = NULL;
132 break;
133 }
134
135 if (!rphy) {
735f7d2f 136 sas_put_device(dev);
b2024459 137 return rc;
2908d778 138 }
899fcf40 139
2908d778
JB
140 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;
2908d778
JB
145 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;
f41a0c44 153 sas_device_set_phy(dev, port->port);
2908d778
JB
154
155 dev->rphy = rphy;
9487669f 156 get_device(&dev->rphy->dev);
87c8331f 157
92625f9b 158 if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEV)
87c8331f
DW
159 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 }
2908d778 165
ec236e52
DW
166 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
2908d778
JB
171 return 0;
172}
173
174/* ---------- Discover and Revalidate ---------- */
175
2908d778
JB
176int 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",
71610f55 188 dev_name(sas_ha->dev),
2908d778
JB
189 SAS_ADDR(dev->sas_addr), res);
190 }
735f7d2f 191 kref_get(&dev->kref);
2908d778
JB
192 }
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
735f7d2f 203 if (i->dft->lldd_dev_gone) {
2908d778 204 i->dft->lldd_dev_gone(dev);
735f7d2f
DW
205 sas_put_device(dev);
206 }
2908d778
JB
207}
208
92625f9b
DW
209static void sas_probe_devices(struct work_struct *work)
210{
211 struct domain_device *dev, *n;
22b9153f 212 struct sas_discovery_event *ev = to_sas_discovery_event(work);
92625f9b 213 struct asd_sas_port *port = ev->port;
2908d778 214
92625f9b
DW
215 clear_bit(DISCE_PROBE, &port->disc.pending);
216
9508a66f
DW
217 /* devices must be domain members before link recovery and probe */
218 list_for_each_entry(dev, &port->disco_list, disco_list_node) {
92625f9b
DW
219 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);
9508a66f 222 }
92625f9b 223
9508a66f 224 sas_probe_sata(port);
92625f9b 225
9508a66f
DW
226 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
227 int err;
228
229 err = sas_rphy_add(dev->rphy);
230 if (err)
231 sas_fail_probe(dev, __func__, err);
232 else
92625f9b
DW
233 list_del_init(&dev->disco_list_node);
234 }
235}
2908d778
JB
236
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)
92625f9b
DW
249 return res;
250 sas_discover_event(dev->port, DISCE_PROBE);
2908d778 251
2908d778 252 return 0;
2908d778
JB
253}
254
255/* ---------- Device registration and unregistration ---------- */
256
735f7d2f
DW
257void sas_free_device(struct kref *kref)
258{
259 struct domain_device *dev = container_of(kref, typeof(*dev), kref);
260
9487669f
DW
261 put_device(&dev->rphy->dev);
262 dev->rphy = NULL;
263
735f7d2f
DW
264 if (dev->parent)
265 sas_put_device(dev->parent);
266
f41a0c44
DW
267 sas_port_put_phy(dev->phy);
268 dev->phy = NULL;
269
735f7d2f
DW
270 /* 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
8abda4d2
DW
274 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
735f7d2f
DW
279 kfree(dev);
280}
281
1a34c064 282static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
2908d778 283{
5db45bdc
DW
284 struct sas_ha_struct *ha = port->ha;
285
2908d778
JB
286 sas_notify_lldd_dev_gone(dev);
287 if (!dev->parent)
288 dev->port->port_dev = NULL;
289 else
290 list_del_init(&dev->siblings);
1a34c064
DW
291
292 spin_lock_irq(&port->dev_list_lock);
2908d778 293 list_del_init(&dev->dev_list_node);
e4a9c373
DW
294 if (dev_is_sata(dev))
295 sas_ata_end_eh(dev->sata_dev.ap);
1a34c064 296 spin_unlock_irq(&port->dev_list_lock);
735f7d2f 297
5db45bdc
DW
298 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
735f7d2f 306 sas_put_device(dev);
2908d778
JB
307}
308
87c8331f 309static void sas_destruct_devices(struct work_struct *work)
2908d778 310{
87c8331f 311 struct domain_device *dev, *n;
22b9153f 312 struct sas_discovery_event *ev = to_sas_discovery_event(work);
87c8331f
DW
313 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
2908d778
JB
320 sas_remove_children(&dev->rphy->dev);
321 sas_rphy_delete(dev->rphy);
87c8331f 322 sas_unregister_common_dev(port, dev);
87c8331f
DW
323 }
324}
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);
87c8331f 333 sas_unregister_common_dev(port, dev);
9487669f 334 return;
87c8331f
DW
335 }
336
9487669f 337 if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
87c8331f
DW
338 sas_rphy_unlink(dev->rphy);
339 list_move_tail(&dev->disco_list_node, &port->destroy_list);
340 sas_discover_event(dev->port, DISCE_DESTRUCT);
2908d778 341 }
2908d778
JB
342}
343
7d05919a 344void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
2908d778
JB
345{
346 struct domain_device *dev, *n;
347
7d05919a
DW
348 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);
1a34c064 351 sas_unregister_dev(port, dev);
7d05919a
DW
352 }
353
87c8331f
DW
354 list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
355 sas_unregister_dev(port, dev);
2908d778
JB
356
357 port->port->rphy = NULL;
358
359}
360
f41a0c44
DW
361void 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
2908d778
JB
381/* ---------- 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 */
c4028958 392static void sas_discover_domain(struct work_struct *work)
2908d778 393{
6f63caae 394 struct domain_device *dev;
2908d778 395 int error = 0;
22b9153f 396 struct sas_discovery_event *ev = to_sas_discovery_event(work);
c4028958 397 struct asd_sas_port *port = ev->port;
2908d778 398
b15ebe0b 399 clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
2908d778
JB
400
401 if (port->port_dev)
6f63caae
DW
402 return;
403
404 error = sas_get_port_device(port);
405 if (error)
406 return;
407 dev = port->port_dev;
2908d778
JB
408
409 SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
ba25f9dc 410 task_pid_nr(current));
2908d778 411
6f63caae 412 switch (dev->dev_type) {
2908d778 413 case SAS_END_DEV:
6f63caae 414 error = sas_discover_end_dev(dev);
2908d778
JB
415 break;
416 case EDGE_DEV:
417 case FANOUT_DEV:
6f63caae 418 error = sas_discover_root_expander(dev);
2908d778
JB
419 break;
420 case SATA_DEV:
421 case SATA_PM:
15c73d5a 422#ifdef CONFIG_SCSI_SAS_ATA
6f63caae 423 error = sas_discover_sata(dev);
2908d778 424 break;
15c73d5a
JB
425#else
426 SAS_DPRINTK("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
427 /* Fall through */
b9142174 428#endif
2908d778 429 default:
b9142174 430 error = -ENXIO;
6f63caae 431 SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
2908d778
JB
432 break;
433 }
434
435 if (error) {
6f63caae 436 sas_rphy_free(dev->rphy);
87c8331f 437 list_del_init(&dev->disco_list_node);
9d720d82 438 spin_lock_irq(&port->dev_list_lock);
6f63caae 439 list_del_init(&dev->dev_list_node);
9d720d82 440 spin_unlock_irq(&port->dev_list_lock);
88808398 441
735f7d2f 442 sas_put_device(dev);
2908d778
JB
443 port->port_dev = NULL;
444 }
445
446 SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
ba25f9dc 447 task_pid_nr(current), error);
2908d778
JB
448}
449
c4028958 450static void sas_revalidate_domain(struct work_struct *work)
2908d778
JB
451{
452 int res = 0;
22b9153f 453 struct sas_discovery_event *ev = to_sas_discovery_event(work);
c4028958 454 struct asd_sas_port *port = ev->port;
87c8331f
DW
455 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 }
2908d778 464
b15ebe0b 465 clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
2908d778
JB
466
467 SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
ba25f9dc 468 task_pid_nr(current));
87c8331f 469
2908d778
JB
470 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",
ba25f9dc 474 port->id, task_pid_nr(current), res);
87c8331f
DW
475 out:
476 mutex_unlock(&ha->disco_mutex);
2908d778
JB
477}
478
479/* ---------- Events ---------- */
480
22b9153f 481static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
b1124cd3 482{
22b9153f
DW
483 /* 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);
b1124cd3
DW
489}
490
491static void sas_chain_event(int event, unsigned long *pending,
22b9153f 492 struct sas_work *sw,
b1124cd3
DW
493 struct sas_ha_struct *ha)
494{
495 if (!test_and_set_bit(event, pending)) {
496 unsigned long flags;
497
e4a9c373 498 spin_lock_irqsave(&ha->lock, flags);
22b9153f 499 sas_chain_work(ha, sw);
e4a9c373 500 spin_unlock_irqrestore(&ha->lock, flags);
b1124cd3
DW
501 }
502}
503
2908d778
JB
504int 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
b1124cd3 514 sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
2908d778
JB
515
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
c4028958 529 static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
2908d778
JB
530 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
531 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
92625f9b 532 [DISCE_PROBE] = sas_probe_devices,
87c8331f 533 [DISCE_DESTRUCT] = sas_destruct_devices,
2908d778
JB
534 };
535
2908d778 536 disc->pending = 0;
c4028958 537 for (i = 0; i < DISC_NUM_EVENTS; i++) {
22b9153f 538 INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
c4028958
DH
539 disc->disc_work[i].port = port;
540 }
2908d778 541}
This page took 0.54657 seconds and 5 git commands to generate.