aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libsas
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libsas')
-rw-r--r--drivers/scsi/libsas/Kconfig39
-rw-r--r--drivers/scsi/libsas/Makefile36
-rw-r--r--drivers/scsi/libsas/sas_discover.c749
-rw-r--r--drivers/scsi/libsas/sas_dump.c76
-rw-r--r--drivers/scsi/libsas/sas_dump.h42
-rw-r--r--drivers/scsi/libsas/sas_event.c75
-rw-r--r--drivers/scsi/libsas/sas_expander.c1862
-rw-r--r--drivers/scsi/libsas/sas_init.c227
-rw-r--r--drivers/scsi/libsas/sas_internal.h146
-rw-r--r--drivers/scsi/libsas/sas_phy.c157
-rw-r--r--drivers/scsi/libsas/sas_port.c279
-rw-r--r--drivers/scsi/libsas/sas_scsi_host.c786
12 files changed, 4474 insertions, 0 deletions
diff --git a/drivers/scsi/libsas/Kconfig b/drivers/scsi/libsas/Kconfig
new file mode 100644
index 000000000000..aafdc92f8312
--- /dev/null
+++ b/drivers/scsi/libsas/Kconfig
@@ -0,0 +1,39 @@
1#
2# Kernel configuration file for the SAS Class
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; version 2 of the
12# License.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22# USA
23#
24
25config SCSI_SAS_LIBSAS
26 tristate "SAS Domain Transport Attributes"
27 depends on SCSI
28 select SCSI_SAS_ATTRS
29 help
30 This provides transport specific helpers for SAS drivers which
31 use the domain device construct (like the aic94xxx).
32
33config SCSI_SAS_LIBSAS_DEBUG
34 bool "Compile the SAS Domain Transport Attributes in debug mode"
35 default y
36 depends on SCSI_SAS_LIBSAS
37 help
38 Compiles the SAS Layer in debug mode. In debug mode, the
39 SAS Layer prints diagnostic and debug messages.
diff --git a/drivers/scsi/libsas/Makefile b/drivers/scsi/libsas/Makefile
new file mode 100644
index 000000000000..44d972a3b4bd
--- /dev/null
+++ b/drivers/scsi/libsas/Makefile
@@ -0,0 +1,36 @@
1#
2# Kernel Makefile for the libsas helpers
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; version 2 of the
12# License.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22# USA
23
24ifeq ($(CONFIG_SCSI_SAS_LIBSAS_DEBUG),y)
25 EXTRA_CFLAGS += -DSAS_DEBUG
26endif
27
28obj-$(CONFIG_SCSI_SAS_LIBSAS) += libsas.o
29libsas-y += sas_init.o \
30 sas_phy.o \
31 sas_port.o \
32 sas_event.o \
33 sas_dump.o \
34 sas_discover.o \
35 sas_expander.o \
36 sas_scsi_host.o
diff --git a/drivers/scsi/libsas/sas_discover.c b/drivers/scsi/libsas/sas_discover.c
new file mode 100644
index 000000000000..d977bd492d8d
--- /dev/null
+++ b/drivers/scsi/libsas/sas_discover.c
@@ -0,0 +1,749 @@
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
25#include <linux/pci.h>
26#include <linux/scatterlist.h>
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>
33#include "../scsi_sas_internal.h"
34
35/* ---------- Basic task processing for discovery purposes ---------- */
36
37void sas_init_dev(struct domain_device *dev)
38{
39 INIT_LIST_HEAD(&dev->siblings);
40 INIT_LIST_HEAD(&dev->dev_list_node);
41 switch (dev->dev_type) {
42 case SAS_END_DEV:
43 break;
44 case EDGE_DEV:
45 case FANOUT_DEV:
46 INIT_LIST_HEAD(&dev->ex_dev.children);
47 break;
48 case SATA_DEV:
49 case SATA_PM:
50 case SATA_PM_PORT:
51 INIT_LIST_HEAD(&dev->sata_dev.children);
52 break;
53 default:
54 break;
55 }
56}
57
58static void sas_task_timedout(unsigned long _task)
59{
60 struct sas_task *task = (void *) _task;
61 unsigned long flags;
62
63 spin_lock_irqsave(&task->task_state_lock, flags);
64 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
65 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
66 spin_unlock_irqrestore(&task->task_state_lock, flags);
67
68 complete(&task->completion);
69}
70
71static void sas_disc_task_done(struct sas_task *task)
72{
73 if (!del_timer(&task->timer))
74 return;
75 complete(&task->completion);
76}
77
78#define SAS_DEV_TIMEOUT 10
79
80/**
81 * sas_execute_task -- Basic task processing for discovery
82 * @task: the task to be executed
83 * @buffer: pointer to buffer to do I/O
84 * @size: size of @buffer
85 * @pci_dma_dir: PCI_DMA_...
86 */
87static int sas_execute_task(struct sas_task *task, void *buffer, int size,
88 int pci_dma_dir)
89{
90 int res = 0;
91 struct scatterlist *scatter = NULL;
92 struct task_status_struct *ts = &task->task_status;
93 int num_scatter = 0;
94 int retries = 0;
95 struct sas_internal *i =
96 to_sas_internal(task->dev->port->ha->core.shost->transportt);
97
98 if (pci_dma_dir != PCI_DMA_NONE) {
99 scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
100 if (!scatter)
101 goto out;
102
103 sg_init_one(scatter, buffer, size);
104 num_scatter = 1;
105 }
106
107 task->task_proto = task->dev->tproto;
108 task->scatter = scatter;
109 task->num_scatter = num_scatter;
110 task->total_xfer_len = size;
111 task->data_dir = pci_dma_dir;
112 task->task_done = sas_disc_task_done;
113
114 for (retries = 0; retries < 5; retries++) {
115 task->task_state_flags = SAS_TASK_STATE_PENDING;
116 init_completion(&task->completion);
117
118 task->timer.data = (unsigned long) task;
119 task->timer.function = sas_task_timedout;
120 task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
121 add_timer(&task->timer);
122
123 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
124 if (res) {
125 del_timer(&task->timer);
126 SAS_DPRINTK("executing SAS discovery task failed:%d\n",
127 res);
128 goto ex_err;
129 }
130 wait_for_completion(&task->completion);
131 res = -ETASK;
132 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
133 int res2;
134 SAS_DPRINTK("task aborted, flags:0x%x\n",
135 task->task_state_flags);
136 res2 = i->dft->lldd_abort_task(task);
137 SAS_DPRINTK("came back from abort task\n");
138 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
139 if (res2 == TMF_RESP_FUNC_COMPLETE)
140 continue; /* Retry the task */
141 else
142 goto ex_err;
143 }
144 }
145 if (task->task_status.stat == SAM_BUSY ||
146 task->task_status.stat == SAM_TASK_SET_FULL ||
147 task->task_status.stat == SAS_QUEUE_FULL) {
148 SAS_DPRINTK("task: q busy, sleeping...\n");
149 schedule_timeout_interruptible(HZ);
150 } else if (task->task_status.stat == SAM_CHECK_COND) {
151 struct scsi_sense_hdr shdr;
152
153 if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
154 &shdr)) {
155 SAS_DPRINTK("couldn't normalize sense\n");
156 continue;
157 }
158 if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
159 (shdr.sense_key == 2 && shdr.asc == 4 &&
160 shdr.ascq == 1)) {
161 SAS_DPRINTK("device %016llx LUN: %016llx "
162 "powering up or not ready yet, "
163 "sleeping...\n",
164 SAS_ADDR(task->dev->sas_addr),
165 SAS_ADDR(task->ssp_task.LUN));
166
167 schedule_timeout_interruptible(5*HZ);
168 } else if (shdr.sense_key == 1) {
169 res = 0;
170 break;
171 } else if (shdr.sense_key == 5) {
172 break;
173 } else {
174 SAS_DPRINTK("dev %016llx LUN: %016llx "
175 "sense key:0x%x ASC:0x%x ASCQ:0x%x"
176 "\n",
177 SAS_ADDR(task->dev->sas_addr),
178 SAS_ADDR(task->ssp_task.LUN),
179 shdr.sense_key,
180 shdr.asc, shdr.ascq);
181 }
182 } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
183 task->task_status.stat != SAM_GOOD) {
184 SAS_DPRINTK("task finished with resp:0x%x, "
185 "stat:0x%x\n",
186 task->task_status.resp,
187 task->task_status.stat);
188 goto ex_err;
189 } else {
190 res = 0;
191 break;
192 }
193 }
194ex_err:
195 if (pci_dma_dir != PCI_DMA_NONE)
196 kfree(scatter);
197out:
198 return res;
199}
200
201/* ---------- Domain device discovery ---------- */
202
203/**
204 * sas_get_port_device -- Discover devices which caused port creation
205 * @port: pointer to struct sas_port of interest
206 *
207 * Devices directly attached to a HA port, have no parent. This is
208 * how we know they are (domain) "root" devices. All other devices
209 * do, and should have their "parent" pointer set appropriately as
210 * soon as a child device is discovered.
211 */
212static int sas_get_port_device(struct asd_sas_port *port)
213{
214 unsigned long flags;
215 struct asd_sas_phy *phy;
216 struct sas_rphy *rphy;
217 struct domain_device *dev;
218
219 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
220 if (!dev)
221 return -ENOMEM;
222
223 spin_lock_irqsave(&port->phy_list_lock, flags);
224 if (list_empty(&port->phy_list)) {
225 spin_unlock_irqrestore(&port->phy_list_lock, flags);
226 kfree(dev);
227 return -ENODEV;
228 }
229 phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
230 spin_lock(&phy->frame_rcvd_lock);
231 memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
232 (size_t)phy->frame_rcvd_size));
233 spin_unlock(&phy->frame_rcvd_lock);
234 spin_unlock_irqrestore(&port->phy_list_lock, flags);
235
236 if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
237 struct dev_to_host_fis *fis =
238 (struct dev_to_host_fis *) dev->frame_rcvd;
239 if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
240 fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
241 && (fis->device & ~0x10) == 0)
242 dev->dev_type = SATA_PM;
243 else
244 dev->dev_type = SATA_DEV;
245 dev->tproto = SATA_PROTO;
246 } else {
247 struct sas_identify_frame *id =
248 (struct sas_identify_frame *) dev->frame_rcvd;
249 dev->dev_type = id->dev_type;
250 dev->iproto = id->initiator_bits;
251 dev->tproto = id->target_bits;
252 }
253
254 sas_init_dev(dev);
255
256 switch (dev->dev_type) {
257 case SAS_END_DEV:
258 rphy = sas_end_device_alloc(port->port);
259 break;
260 case EDGE_DEV:
261 rphy = sas_expander_alloc(port->port,
262 SAS_EDGE_EXPANDER_DEVICE);
263 break;
264 case FANOUT_DEV:
265 rphy = sas_expander_alloc(port->port,
266 SAS_FANOUT_EXPANDER_DEVICE);
267 break;
268 case SATA_DEV:
269 default:
270 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
271 rphy = NULL;
272 break;
273 }
274
275 if (!rphy) {
276 kfree(dev);
277 return -ENODEV;
278 }
279 rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
280 memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
281 sas_fill_in_rphy(dev, rphy);
282 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
283 port->port_dev = dev;
284 dev->port = port;
285 dev->linkrate = port->linkrate;
286 dev->min_linkrate = port->linkrate;
287 dev->max_linkrate = port->linkrate;
288 dev->pathways = port->num_phys;
289 memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
290 memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
291 memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
292 port->disc.max_level = 0;
293
294 dev->rphy = rphy;
295 spin_lock(&port->dev_list_lock);
296 list_add_tail(&dev->dev_list_node, &port->dev_list);
297 spin_unlock(&port->dev_list_lock);
298
299 return 0;
300}
301
302/* ---------- Discover and Revalidate ---------- */
303
304/* ---------- SATA ---------- */
305
306static void sas_get_ata_command_set(struct domain_device *dev)
307{
308 struct dev_to_host_fis *fis =
309 (struct dev_to_host_fis *) dev->frame_rcvd;
310
311 if ((fis->sector_count == 1 && /* ATA */
312 fis->lbal == 1 &&
313 fis->lbam == 0 &&
314 fis->lbah == 0 &&
315 fis->device == 0)
316 ||
317 (fis->sector_count == 0 && /* CE-ATA (mATA) */
318 fis->lbal == 0 &&
319 fis->lbam == 0xCE &&
320 fis->lbah == 0xAA &&
321 (fis->device & ~0x10) == 0))
322
323 dev->sata_dev.command_set = ATA_COMMAND_SET;
324
325 else if ((fis->interrupt_reason == 1 && /* ATAPI */
326 fis->lbal == 1 &&
327 fis->byte_count_low == 0x14 &&
328 fis->byte_count_high == 0xEB &&
329 (fis->device & ~0x10) == 0))
330
331 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
332
333 else if ((fis->sector_count == 1 && /* SEMB */
334 fis->lbal == 1 &&
335 fis->lbam == 0x3C &&
336 fis->lbah == 0xC3 &&
337 fis->device == 0)
338 ||
339 (fis->interrupt_reason == 1 && /* SATA PM */
340 fis->lbal == 1 &&
341 fis->byte_count_low == 0x69 &&
342 fis->byte_count_high == 0x96 &&
343 (fis->device & ~0x10) == 0))
344
345 /* Treat it as a superset? */
346 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
347}
348
349/**
350 * sas_issue_ata_cmd -- Basic SATA command processing for discovery
351 * @dev: the device to send the command to
352 * @command: the command register
353 * @features: the features register
354 * @buffer: pointer to buffer to do I/O
355 * @size: size of @buffer
356 * @pci_dma_dir: PCI_DMA_...
357 */
358static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
359 u8 features, void *buffer, int size,
360 int pci_dma_dir)
361{
362 int res = 0;
363 struct sas_task *task;
364 struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
365 &dev->frame_rcvd[0];
366
367 res = -ENOMEM;
368 task = sas_alloc_task(GFP_KERNEL);
369 if (!task)
370 goto out;
371
372 task->dev = dev;
373
374 task->ata_task.fis.command = command;
375 task->ata_task.fis.features = features;
376 task->ata_task.fis.device = d2h_fis->device;
377 task->ata_task.retry_count = 1;
378
379 res = sas_execute_task(task, buffer, size, pci_dma_dir);
380
381 sas_free_task(task);
382out:
383 return res;
384}
385
386static void sas_sata_propagate_sas_addr(struct domain_device *dev)
387{
388 unsigned long flags;
389 struct asd_sas_port *port = dev->port;
390 struct asd_sas_phy *phy;
391
392 BUG_ON(dev->parent);
393
394 memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
395 spin_lock_irqsave(&port->phy_list_lock, flags);
396 list_for_each_entry(phy, &port->phy_list, port_phy_el)
397 memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
398 spin_unlock_irqrestore(&port->phy_list_lock, flags);
399}
400
401#define ATA_IDENTIFY_DEV 0xEC
402#define ATA_IDENTIFY_PACKET_DEV 0xA1
403#define ATA_SET_FEATURES 0xEF
404#define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
405
406/**
407 * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
408 * @dev: STP/SATA device of interest (ATA/ATAPI)
409 *
410 * The LLDD has already been notified of this device, so that we can
411 * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY
412 * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
413 * performance for this device.
414 */
415static int sas_discover_sata_dev(struct domain_device *dev)
416{
417 int res;
418 __le16 *identify_x;
419 u8 command;
420
421 identify_x = kzalloc(512, GFP_KERNEL);
422 if (!identify_x)
423 return -ENOMEM;
424
425 if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
426 dev->sata_dev.identify_device = identify_x;
427 command = ATA_IDENTIFY_DEV;
428 } else {
429 dev->sata_dev.identify_packet_device = identify_x;
430 command = ATA_IDENTIFY_PACKET_DEV;
431 }
432
433 res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
434 PCI_DMA_FROMDEVICE);
435 if (res)
436 goto out_err;
437
438 /* lives on the media? */
439 if (le16_to_cpu(identify_x[0]) & 4) {
440 /* incomplete response */
441 SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
442 "dev %llx\n", SAS_ADDR(dev->sas_addr));
443 if (!le16_to_cpu(identify_x[83] & (1<<6)))
444 goto cont1;
445 res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
446 ATA_FEATURE_PUP_STBY_SPIN_UP,
447 NULL, 0, PCI_DMA_NONE);
448 if (res)
449 goto cont1;
450
451 schedule_timeout_interruptible(5*HZ); /* More time? */
452 res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
453 PCI_DMA_FROMDEVICE);
454 if (res)
455 goto out_err;
456 }
457cont1:
458 /* Get WWN */
459 if (dev->port->oob_mode != SATA_OOB_MODE) {
460 memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
461 SAS_ADDR_SIZE);
462 } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
463 (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
464 == 0x5000) {
465 int i;
466
467 for (i = 0; i < 4; i++) {
468 dev->sas_addr[2*i] =
469 (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
470 dev->sas_addr[2*i+1] =
471 le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
472 }
473 }
474 sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
475 if (!dev->parent)
476 sas_sata_propagate_sas_addr(dev);
477
478 /* XXX Hint: register this SATA device with SATL.
479 When this returns, dev->sata_dev->lu is alive and
480 present.
481 sas_satl_register_dev(dev);
482 */
483 return 0;
484out_err:
485 dev->sata_dev.identify_packet_device = NULL;
486 dev->sata_dev.identify_device = NULL;
487 kfree(identify_x);
488 return res;
489}
490
491static int sas_discover_sata_pm(struct domain_device *dev)
492{
493 return -ENODEV;
494}
495
496int sas_notify_lldd_dev_found(struct domain_device *dev)
497{
498 int res = 0;
499 struct sas_ha_struct *sas_ha = dev->port->ha;
500 struct Scsi_Host *shost = sas_ha->core.shost;
501 struct sas_internal *i = to_sas_internal(shost->transportt);
502
503 if (i->dft->lldd_dev_found) {
504 res = i->dft->lldd_dev_found(dev);
505 if (res) {
506 printk("sas: driver on pcidev %s cannot handle "
507 "device %llx, error:%d\n",
508 pci_name(sas_ha->pcidev),
509 SAS_ADDR(dev->sas_addr), res);
510 }
511 }
512 return res;
513}
514
515
516void sas_notify_lldd_dev_gone(struct domain_device *dev)
517{
518 struct sas_ha_struct *sas_ha = dev->port->ha;
519 struct Scsi_Host *shost = sas_ha->core.shost;
520 struct sas_internal *i = to_sas_internal(shost->transportt);
521
522 if (i->dft->lldd_dev_gone)
523 i->dft->lldd_dev_gone(dev);
524}
525
526/* ---------- Common/dispatchers ---------- */
527
528/**
529 * sas_discover_sata -- discover an STP/SATA domain device
530 * @dev: pointer to struct domain_device of interest
531 *
532 * First we notify the LLDD of this device, so we can send frames to
533 * it. Then depending on the type of device we call the appropriate
534 * discover functions. Once device discover is done, we notify the
535 * LLDD so that it can fine-tune its parameters for the device, by
536 * removing it and then adding it. That is, the second time around,
537 * the driver would have certain fields, that it is looking at, set.
538 * Finally we initialize the kobj so that the device can be added to
539 * the system at registration time. Devices directly attached to a HA
540 * port, have no parents. All other devices do, and should have their
541 * "parent" pointer set appropriately before calling this function.
542 */
543int sas_discover_sata(struct domain_device *dev)
544{
545 int res;
546
547 sas_get_ata_command_set(dev);
548
549 res = sas_notify_lldd_dev_found(dev);
550 if (res)
551 return res;
552
553 switch (dev->dev_type) {
554 case SATA_DEV:
555 res = sas_discover_sata_dev(dev);
556 break;
557 case SATA_PM:
558 res = sas_discover_sata_pm(dev);
559 break;
560 default:
561 break;
562 }
563
564 sas_notify_lldd_dev_gone(dev);
565 if (!res) {
566 sas_notify_lldd_dev_found(dev);
567 }
568 return res;
569}
570
571/**
572 * sas_discover_end_dev -- discover an end device (SSP, etc)
573 * @end: pointer to domain device of interest
574 *
575 * See comment in sas_discover_sata().
576 */
577int sas_discover_end_dev(struct domain_device *dev)
578{
579 int res;
580
581 res = sas_notify_lldd_dev_found(dev);
582 if (res)
583 return res;
584
585 res = sas_rphy_add(dev->rphy);
586 if (res)
587 goto out_err;
588
589 /* do this to get the end device port attributes which will have
590 * been scanned in sas_rphy_add */
591 sas_notify_lldd_dev_gone(dev);
592 sas_notify_lldd_dev_found(dev);
593
594 return 0;
595
596out_err:
597 sas_notify_lldd_dev_gone(dev);
598 return res;
599}
600
601/* ---------- Device registration and unregistration ---------- */
602
603static inline void sas_unregister_common_dev(struct domain_device *dev)
604{
605 sas_notify_lldd_dev_gone(dev);
606 if (!dev->parent)
607 dev->port->port_dev = NULL;
608 else
609 list_del_init(&dev->siblings);
610 list_del_init(&dev->dev_list_node);
611}
612
613void sas_unregister_dev(struct domain_device *dev)
614{
615 if (dev->rphy) {
616 sas_remove_children(&dev->rphy->dev);
617 sas_rphy_delete(dev->rphy);
618 dev->rphy = NULL;
619 }
620 if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
621 /* remove the phys and ports, everything else should be gone */
622 kfree(dev->ex_dev.ex_phy);
623 dev->ex_dev.ex_phy = NULL;
624 }
625 sas_unregister_common_dev(dev);
626}
627
628void sas_unregister_domain_devices(struct asd_sas_port *port)
629{
630 struct domain_device *dev, *n;
631
632 list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
633 sas_unregister_dev(dev);
634
635 port->port->rphy = NULL;
636
637}
638
639/* ---------- Discovery and Revalidation ---------- */
640
641/**
642 * sas_discover_domain -- discover the domain
643 * @port: port to the domain of interest
644 *
645 * NOTE: this process _must_ quit (return) as soon as any connection
646 * errors are encountered. Connection recovery is done elsewhere.
647 * Discover process only interrogates devices in order to discover the
648 * domain.
649 */
650static void sas_discover_domain(void *data)
651{
652 int error = 0;
653 struct asd_sas_port *port = data;
654
655 sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
656 &port->disc.pending);
657
658 if (port->port_dev)
659 return ;
660 else {
661 error = sas_get_port_device(port);
662 if (error)
663 return;
664 }
665
666 SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
667 current->pid);
668
669 switch (port->port_dev->dev_type) {
670 case SAS_END_DEV:
671 error = sas_discover_end_dev(port->port_dev);
672 break;
673 case EDGE_DEV:
674 case FANOUT_DEV:
675 error = sas_discover_root_expander(port->port_dev);
676 break;
677 case SATA_DEV:
678 case SATA_PM:
679 error = sas_discover_sata(port->port_dev);
680 break;
681 default:
682 SAS_DPRINTK("unhandled device %d\n", port->port_dev->dev_type);
683 break;
684 }
685
686 if (error) {
687 kfree(port->port_dev); /* not kobject_register-ed yet */
688 port->port_dev = NULL;
689 }
690
691 SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
692 current->pid, error);
693}
694
695static void sas_revalidate_domain(void *data)
696{
697 int res = 0;
698 struct asd_sas_port *port = data;
699
700 sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
701 &port->disc.pending);
702
703 SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
704 current->pid);
705 if (port->port_dev)
706 res = sas_ex_revalidate_domain(port->port_dev);
707
708 SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
709 port->id, current->pid, res);
710}
711
712/* ---------- Events ---------- */
713
714int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
715{
716 struct sas_discovery *disc;
717
718 if (!port)
719 return 0;
720 disc = &port->disc;
721
722 BUG_ON(ev >= DISC_NUM_EVENTS);
723
724 sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
725 &disc->disc_work[ev], port->ha->core.shost);
726
727 return 0;
728}
729
730/**
731 * sas_init_disc -- initialize the discovery struct in the port
732 * @port: pointer to struct port
733 *
734 * Called when the ports are being initialized.
735 */
736void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
737{
738 int i;
739
740 static void (*sas_event_fns[DISC_NUM_EVENTS])(void *) = {
741 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
742 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
743 };
744
745 spin_lock_init(&disc->disc_event_lock);
746 disc->pending = 0;
747 for (i = 0; i < DISC_NUM_EVENTS; i++)
748 INIT_WORK(&disc->disc_work[i], sas_event_fns[i], port);
749}
diff --git a/drivers/scsi/libsas/sas_dump.c b/drivers/scsi/libsas/sas_dump.c
new file mode 100644
index 000000000000..f1246d2c9bef
--- /dev/null
+++ b/drivers/scsi/libsas/sas_dump.c
@@ -0,0 +1,76 @@
1/*
2 * Serial Attached SCSI (SAS) Dump/Debugging routines
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
25#include "sas_dump.h"
26
27#ifdef SAS_DEBUG
28
29static const char *sas_hae_str[] = {
30 [0] = "HAE_RESET",
31};
32
33static const char *sas_porte_str[] = {
34 [0] = "PORTE_BYTES_DMAED",
35 [1] = "PORTE_BROADCAST_RCVD",
36 [2] = "PORTE_LINK_RESET_ERR",
37 [3] = "PORTE_TIMER_EVENT",
38 [4] = "PORTE_HARD_RESET",
39};
40
41static const char *sas_phye_str[] = {
42 [0] = "PHYE_LOSS_OF_SIGNAL",
43 [1] = "PHYE_OOB_DONE",
44 [2] = "PHYE_OOB_ERROR",
45 [3] = "PHYE_SPINUP_HOLD",
46};
47
48void sas_dprint_porte(int phyid, enum port_event pe)
49{
50 SAS_DPRINTK("phy%d: port event: %s\n", phyid, sas_porte_str[pe]);
51}
52void sas_dprint_phye(int phyid, enum phy_event pe)
53{
54 SAS_DPRINTK("phy%d: phy event: %s\n", phyid, sas_phye_str[pe]);
55}
56
57void sas_dprint_hae(struct sas_ha_struct *sas_ha, enum ha_event he)
58{
59 SAS_DPRINTK("ha %s: %s event\n", pci_name(sas_ha->pcidev),
60 sas_hae_str[he]);
61}
62
63void sas_dump_port(struct asd_sas_port *port)
64{
65 SAS_DPRINTK("port%d: class:0x%x\n", port->id, port->class);
66 SAS_DPRINTK("port%d: sas_addr:%llx\n", port->id,
67 SAS_ADDR(port->sas_addr));
68 SAS_DPRINTK("port%d: attached_sas_addr:%llx\n", port->id,
69 SAS_ADDR(port->attached_sas_addr));
70 SAS_DPRINTK("port%d: iproto:0x%x\n", port->id, port->iproto);
71 SAS_DPRINTK("port%d: tproto:0x%x\n", port->id, port->tproto);
72 SAS_DPRINTK("port%d: oob_mode:0x%x\n", port->id, port->oob_mode);
73 SAS_DPRINTK("port%d: num_phys:%d\n", port->id, port->num_phys);
74}
75
76#endif /* SAS_DEBUG */
diff --git a/drivers/scsi/libsas/sas_dump.h b/drivers/scsi/libsas/sas_dump.h
new file mode 100644
index 000000000000..47b45d4f5258
--- /dev/null
+++ b/drivers/scsi/libsas/sas_dump.h
@@ -0,0 +1,42 @@
1/*
2 * Serial Attached SCSI (SAS) Dump/Debugging routines header file
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
25#include "sas_internal.h"
26
27#ifdef SAS_DEBUG
28
29void sas_dprint_porte(int phyid, enum port_event pe);
30void sas_dprint_phye(int phyid, enum phy_event pe);
31void sas_dprint_hae(struct sas_ha_struct *sas_ha, enum ha_event he);
32void sas_dump_port(struct asd_sas_port *port);
33
34#else /* SAS_DEBUG */
35
36static inline void sas_dprint_porte(int phyid, enum port_event pe) { }
37static inline void sas_dprint_phye(int phyid, enum phy_event pe) { }
38static inline void sas_dprint_hae(struct sas_ha_struct *sas_ha,
39 enum ha_event he) { }
40static inline void sas_dump_port(struct asd_sas_port *port) { }
41
42#endif /* SAS_DEBUG */
diff --git a/drivers/scsi/libsas/sas_event.c b/drivers/scsi/libsas/sas_event.c
new file mode 100644
index 000000000000..19110ed1c89c
--- /dev/null
+++ b/drivers/scsi/libsas/sas_event.c
@@ -0,0 +1,75 @@
1/*
2 * Serial Attached SCSI (SAS) Event processing
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
25#include <scsi/scsi_host.h>
26#include "sas_internal.h"
27#include "sas_dump.h"
28
29static void notify_ha_event(struct sas_ha_struct *sas_ha, enum ha_event event)
30{
31 BUG_ON(event >= HA_NUM_EVENTS);
32
33 sas_queue_event(event, &sas_ha->event_lock, &sas_ha->pending,
34 &sas_ha->ha_events[event], sas_ha->core.shost);
35}
36
37static void notify_port_event(struct asd_sas_phy *phy, enum port_event event)
38{
39 struct sas_ha_struct *ha = phy->ha;
40
41 BUG_ON(event >= PORT_NUM_EVENTS);
42
43 sas_queue_event(event, &ha->event_lock, &phy->port_events_pending,
44 &phy->port_events[event], ha->core.shost);
45}
46
47static void notify_phy_event(struct asd_sas_phy *phy, enum phy_event event)
48{
49 struct sas_ha_struct *ha = phy->ha;
50
51 BUG_ON(event >= PHY_NUM_EVENTS);
52
53 sas_queue_event(event, &ha->event_lock, &phy->phy_events_pending,
54 &phy->phy_events[event], ha->core.shost);
55}
56
57int sas_init_events(struct sas_ha_struct *sas_ha)
58{
59 static void (*sas_ha_event_fns[HA_NUM_EVENTS])(void *) = {
60 [HAE_RESET] = sas_hae_reset,
61 };
62
63 int i;
64
65 spin_lock_init(&sas_ha->event_lock);
66
67 for (i = 0; i < HA_NUM_EVENTS; i++)
68 INIT_WORK(&sas_ha->ha_events[i], sas_ha_event_fns[i], sas_ha);
69
70 sas_ha->notify_ha_event = notify_ha_event;
71 sas_ha->notify_port_event = notify_port_event;
72 sas_ha->notify_phy_event = notify_phy_event;
73
74 return 0;
75}
diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
new file mode 100644
index 000000000000..b653a263f76a
--- /dev/null
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -0,0 +1,1862 @@
1/*
2 * Serial Attached SCSI (SAS) Expander discovery and configuration
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
25#include <linux/pci.h>
26#include <linux/scatterlist.h>
27
28#include "sas_internal.h"
29
30#include <scsi/scsi_transport.h>
31#include <scsi/scsi_transport_sas.h>
32#include "../scsi_sas_internal.h"
33
34static int sas_discover_expander(struct domain_device *dev);
35static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36static int sas_configure_phy(struct domain_device *dev, int phy_id,
37 u8 *sas_addr, int include);
38static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
39
40#if 0
41/* FIXME: smp needs to migrate into the sas class */
42static ssize_t smp_portal_read(struct kobject *, char *, loff_t, size_t);
43static ssize_t smp_portal_write(struct kobject *, char *, loff_t, size_t);
44#endif
45
46/* ---------- SMP task management ---------- */
47
48static void smp_task_timedout(unsigned long _task)
49{
50 struct sas_task *task = (void *) _task;
51 unsigned long flags;
52
53 spin_lock_irqsave(&task->task_state_lock, flags);
54 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
55 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
56 spin_unlock_irqrestore(&task->task_state_lock, flags);
57
58 complete(&task->completion);
59}
60
61static void smp_task_done(struct sas_task *task)
62{
63 if (!del_timer(&task->timer))
64 return;
65 complete(&task->completion);
66}
67
68/* Give it some long enough timeout. In seconds. */
69#define SMP_TIMEOUT 10
70
71static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
72 void *resp, int resp_size)
73{
74 int res;
75 struct sas_task *task = sas_alloc_task(GFP_KERNEL);
76 struct sas_internal *i =
77 to_sas_internal(dev->port->ha->core.shost->transportt);
78
79 if (!task)
80 return -ENOMEM;
81
82 task->dev = dev;
83 task->task_proto = dev->tproto;
84 sg_init_one(&task->smp_task.smp_req, req, req_size);
85 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
86
87 task->task_done = smp_task_done;
88
89 task->timer.data = (unsigned long) task;
90 task->timer.function = smp_task_timedout;
91 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
92 add_timer(&task->timer);
93
94 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
95
96 if (res) {
97 del_timer(&task->timer);
98 SAS_DPRINTK("executing SMP task failed:%d\n", res);
99 goto ex_err;
100 }
101
102 wait_for_completion(&task->completion);
103 res = -ETASK;
104 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
105 SAS_DPRINTK("smp task timed out or aborted\n");
106 i->dft->lldd_abort_task(task);
107 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
108 SAS_DPRINTK("SMP task aborted and not done\n");
109 goto ex_err;
110 }
111 }
112 if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAM_GOOD)
114 res = 0;
115 else
116 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
117 "status 0x%x\n", __FUNCTION__,
118 SAS_ADDR(dev->sas_addr),
119 task->task_status.resp,
120 task->task_status.stat);
121ex_err:
122 sas_free_task(task);
123 return res;
124}
125
126/* ---------- Allocations ---------- */
127
128static inline void *alloc_smp_req(int size)
129{
130 u8 *p = kzalloc(size, GFP_KERNEL);
131 if (p)
132 p[0] = SMP_REQUEST;
133 return p;
134}
135
136static inline void *alloc_smp_resp(int size)
137{
138 return kzalloc(size, GFP_KERNEL);
139}
140
141/* ---------- Expander configuration ---------- */
142
143static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
144 void *disc_resp)
145{
146 struct expander_device *ex = &dev->ex_dev;
147 struct ex_phy *phy = &ex->ex_phy[phy_id];
148 struct smp_resp *resp = disc_resp;
149 struct discover_resp *dr = &resp->disc;
150 struct sas_rphy *rphy = dev->rphy;
151 int rediscover = (phy->phy != NULL);
152
153 if (!rediscover) {
154 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
155
156 /* FIXME: error_handling */
157 BUG_ON(!phy->phy);
158 }
159
160 switch (resp->result) {
161 case SMP_RESP_PHY_VACANT:
162 phy->phy_state = PHY_VACANT;
163 return;
164 default:
165 phy->phy_state = PHY_NOT_PRESENT;
166 return;
167 case SMP_RESP_FUNC_ACC:
168 phy->phy_state = PHY_EMPTY; /* do not know yet */
169 break;
170 }
171
172 phy->phy_id = phy_id;
173 phy->attached_dev_type = dr->attached_dev_type;
174 phy->linkrate = dr->linkrate;
175 phy->attached_sata_host = dr->attached_sata_host;
176 phy->attached_sata_dev = dr->attached_sata_dev;
177 phy->attached_sata_ps = dr->attached_sata_ps;
178 phy->attached_iproto = dr->iproto << 1;
179 phy->attached_tproto = dr->tproto << 1;
180 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
181 phy->attached_phy_id = dr->attached_phy_id;
182 phy->phy_change_count = dr->change_count;
183 phy->routing_attr = dr->routing_attr;
184 phy->virtual = dr->virtual;
185 phy->last_da_index = -1;
186
187 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
188 phy->phy->identify.target_port_protocols = phy->attached_tproto;
189 phy->phy->identify.phy_identifier = phy_id;
190 phy->phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
191 phy->phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
192 phy->phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
193 phy->phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
194 switch (phy->linkrate) {
195 case PHY_LINKRATE_1_5:
196 phy->phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
197 break;
198 case PHY_LINKRATE_3:
199 phy->phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
200 break;
201 case PHY_LINKRATE_6:
202 phy->phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
203 break;
204 default:
205 phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
206 break;
207 }
208
209 if (!rediscover)
210 sas_phy_add(phy->phy);
211
212 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
213 SAS_ADDR(dev->sas_addr), phy->phy_id,
214 phy->routing_attr == TABLE_ROUTING ? 'T' :
215 phy->routing_attr == DIRECT_ROUTING ? 'D' :
216 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
217 SAS_ADDR(phy->attached_sas_addr));
218
219 return;
220}
221
222#define DISCOVER_REQ_SIZE 16
223#define DISCOVER_RESP_SIZE 56
224
225static int sas_ex_phy_discover(struct domain_device *dev, int single)
226{
227 struct expander_device *ex = &dev->ex_dev;
228 int res = 0;
229 u8 *disc_req;
230 u8 *disc_resp;
231
232 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
233 if (!disc_req)
234 return -ENOMEM;
235
236 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
237 if (!disc_resp) {
238 kfree(disc_req);
239 return -ENOMEM;
240 }
241
242 disc_req[1] = SMP_DISCOVER;
243
244 if (0 <= single && single < ex->num_phys) {
245 disc_req[9] = single;
246 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
247 disc_resp, DISCOVER_RESP_SIZE);
248 if (res)
249 goto out_err;
250 sas_set_ex_phy(dev, single, disc_resp);
251 } else {
252 int i;
253
254 for (i = 0; i < ex->num_phys; i++) {
255 disc_req[9] = i;
256 res = smp_execute_task(dev, disc_req,
257 DISCOVER_REQ_SIZE, disc_resp,
258 DISCOVER_RESP_SIZE);
259 if (res)
260 goto out_err;
261 sas_set_ex_phy(dev, i, disc_resp);
262 }
263 }
264out_err:
265 kfree(disc_resp);
266 kfree(disc_req);
267 return res;
268}
269
270static int sas_expander_discover(struct domain_device *dev)
271{
272 struct expander_device *ex = &dev->ex_dev;
273 int res = -ENOMEM;
274
275 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
276 if (!ex->ex_phy)
277 return -ENOMEM;
278
279 res = sas_ex_phy_discover(dev, -1);
280 if (res)
281 goto out_err;
282
283 return 0;
284 out_err:
285 kfree(ex->ex_phy);
286 ex->ex_phy = NULL;
287 return res;
288}
289
290#define MAX_EXPANDER_PHYS 128
291
292static void ex_assign_report_general(struct domain_device *dev,
293 struct smp_resp *resp)
294{
295 struct report_general_resp *rg = &resp->rg;
296
297 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
298 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
299 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
300 dev->ex_dev.conf_route_table = rg->conf_route_table;
301 dev->ex_dev.configuring = rg->configuring;
302 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
303}
304
305#define RG_REQ_SIZE 8
306#define RG_RESP_SIZE 32
307
308static int sas_ex_general(struct domain_device *dev)
309{
310 u8 *rg_req;
311 struct smp_resp *rg_resp;
312 int res;
313 int i;
314
315 rg_req = alloc_smp_req(RG_REQ_SIZE);
316 if (!rg_req)
317 return -ENOMEM;
318
319 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
320 if (!rg_resp) {
321 kfree(rg_req);
322 return -ENOMEM;
323 }
324
325 rg_req[1] = SMP_REPORT_GENERAL;
326
327 for (i = 0; i < 5; i++) {
328 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
329 RG_RESP_SIZE);
330
331 if (res) {
332 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
333 SAS_ADDR(dev->sas_addr), res);
334 goto out;
335 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
336 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
337 SAS_ADDR(dev->sas_addr), rg_resp->result);
338 res = rg_resp->result;
339 goto out;
340 }
341
342 ex_assign_report_general(dev, rg_resp);
343
344 if (dev->ex_dev.configuring) {
345 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
346 SAS_ADDR(dev->sas_addr));
347 schedule_timeout_interruptible(5*HZ);
348 } else
349 break;
350 }
351out:
352 kfree(rg_req);
353 kfree(rg_resp);
354 return res;
355}
356
357static void ex_assign_manuf_info(struct domain_device *dev, void
358 *_mi_resp)
359{
360 u8 *mi_resp = _mi_resp;
361 struct sas_rphy *rphy = dev->rphy;
362 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
363
364 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
365 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
366 memcpy(edev->product_rev, mi_resp + 36,
367 SAS_EXPANDER_PRODUCT_REV_LEN);
368
369 if (mi_resp[8] & 1) {
370 memcpy(edev->component_vendor_id, mi_resp + 40,
371 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
372 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
373 edev->component_revision_id = mi_resp[50];
374 }
375}
376
377#define MI_REQ_SIZE 8
378#define MI_RESP_SIZE 64
379
380static int sas_ex_manuf_info(struct domain_device *dev)
381{
382 u8 *mi_req;
383 u8 *mi_resp;
384 int res;
385
386 mi_req = alloc_smp_req(MI_REQ_SIZE);
387 if (!mi_req)
388 return -ENOMEM;
389
390 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
391 if (!mi_resp) {
392 kfree(mi_req);
393 return -ENOMEM;
394 }
395
396 mi_req[1] = SMP_REPORT_MANUF_INFO;
397
398 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
399 if (res) {
400 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
401 SAS_ADDR(dev->sas_addr), res);
402 goto out;
403 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
404 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
405 SAS_ADDR(dev->sas_addr), mi_resp[2]);
406 goto out;
407 }
408
409 ex_assign_manuf_info(dev, mi_resp);
410out:
411 kfree(mi_req);
412 kfree(mi_resp);
413 return res;
414}
415
416#define PC_REQ_SIZE 44
417#define PC_RESP_SIZE 8
418
419int sas_smp_phy_control(struct domain_device *dev, int phy_id,
420 enum phy_func phy_func)
421{
422 u8 *pc_req;
423 u8 *pc_resp;
424 int res;
425
426 pc_req = alloc_smp_req(PC_REQ_SIZE);
427 if (!pc_req)
428 return -ENOMEM;
429
430 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
431 if (!pc_resp) {
432 kfree(pc_req);
433 return -ENOMEM;
434 }
435
436 pc_req[1] = SMP_PHY_CONTROL;
437 pc_req[9] = phy_id;
438 pc_req[10]= phy_func;
439
440 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
441
442 kfree(pc_resp);
443 kfree(pc_req);
444 return res;
445}
446
447static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
448{
449 struct expander_device *ex = &dev->ex_dev;
450 struct ex_phy *phy = &ex->ex_phy[phy_id];
451
452 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE);
453 phy->linkrate = PHY_DISABLED;
454}
455
456static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
457{
458 struct expander_device *ex = &dev->ex_dev;
459 int i;
460
461 for (i = 0; i < ex->num_phys; i++) {
462 struct ex_phy *phy = &ex->ex_phy[i];
463
464 if (phy->phy_state == PHY_VACANT ||
465 phy->phy_state == PHY_NOT_PRESENT)
466 continue;
467
468 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
469 sas_ex_disable_phy(dev, i);
470 }
471}
472
473static int sas_dev_present_in_domain(struct asd_sas_port *port,
474 u8 *sas_addr)
475{
476 struct domain_device *dev;
477
478 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
479 return 1;
480 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
481 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
482 return 1;
483 }
484 return 0;
485}
486
487#define RPEL_REQ_SIZE 16
488#define RPEL_RESP_SIZE 32
489int sas_smp_get_phy_events(struct sas_phy *phy)
490{
491 int res;
492 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
493 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
494 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
495 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
496
497 if (!resp)
498 return -ENOMEM;
499
500 req[1] = SMP_REPORT_PHY_ERR_LOG;
501 req[9] = phy->number;
502
503 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
504 resp, RPEL_RESP_SIZE);
505
506 if (!res)
507 goto out;
508
509 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
510 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
511 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
512 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
513
514 out:
515 kfree(resp);
516 return res;
517
518}
519
520#define RPS_REQ_SIZE 16
521#define RPS_RESP_SIZE 60
522
523static int sas_get_report_phy_sata(struct domain_device *dev,
524 int phy_id,
525 struct smp_resp *rps_resp)
526{
527 int res;
528 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
529
530 if (!rps_req)
531 return -ENOMEM;
532
533 rps_req[1] = SMP_REPORT_PHY_SATA;
534 rps_req[9] = phy_id;
535
536 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
537 rps_resp, RPS_RESP_SIZE);
538
539 kfree(rps_req);
540 return 0;
541}
542
543static void sas_ex_get_linkrate(struct domain_device *parent,
544 struct domain_device *child,
545 struct ex_phy *parent_phy)
546{
547 struct expander_device *parent_ex = &parent->ex_dev;
548 struct sas_port *port;
549 int i;
550
551 child->pathways = 0;
552
553 port = parent_phy->port;
554
555 for (i = 0; i < parent_ex->num_phys; i++) {
556 struct ex_phy *phy = &parent_ex->ex_phy[i];
557
558 if (phy->phy_state == PHY_VACANT ||
559 phy->phy_state == PHY_NOT_PRESENT)
560 continue;
561
562 if (SAS_ADDR(phy->attached_sas_addr) ==
563 SAS_ADDR(child->sas_addr)) {
564
565 child->min_linkrate = min(parent->min_linkrate,
566 phy->linkrate);
567 child->max_linkrate = max(parent->max_linkrate,
568 phy->linkrate);
569 child->pathways++;
570 sas_port_add_phy(port, phy->phy);
571 }
572 }
573 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
574 child->pathways = min(child->pathways, parent->pathways);
575}
576
577static struct domain_device *sas_ex_discover_end_dev(
578 struct domain_device *parent, int phy_id)
579{
580 struct expander_device *parent_ex = &parent->ex_dev;
581 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
582 struct domain_device *child = NULL;
583 struct sas_rphy *rphy;
584 int res;
585
586 if (phy->attached_sata_host || phy->attached_sata_ps)
587 return NULL;
588
589 child = kzalloc(sizeof(*child), GFP_KERNEL);
590 if (!child)
591 return NULL;
592
593 child->parent = parent;
594 child->port = parent->port;
595 child->iproto = phy->attached_iproto;
596 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
597 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
598 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
599 BUG_ON(!phy->port);
600 /* FIXME: better error handling*/
601 BUG_ON(sas_port_add(phy->port) != 0);
602 sas_ex_get_linkrate(parent, child, phy);
603
604 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
605 child->dev_type = SATA_DEV;
606 if (phy->attached_tproto & SAS_PROTO_STP)
607 child->tproto = phy->attached_tproto;
608 if (phy->attached_sata_dev)
609 child->tproto |= SATA_DEV;
610 res = sas_get_report_phy_sata(parent, phy_id,
611 &child->sata_dev.rps_resp);
612 if (res) {
613 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
614 "0x%x\n", SAS_ADDR(parent->sas_addr),
615 phy_id, res);
616 kfree(child);
617 return NULL;
618 }
619 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
620 sizeof(struct dev_to_host_fis));
621 sas_init_dev(child);
622 res = sas_discover_sata(child);
623 if (res) {
624 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
625 "%016llx:0x%x returned 0x%x\n",
626 SAS_ADDR(child->sas_addr),
627 SAS_ADDR(parent->sas_addr), phy_id, res);
628 kfree(child);
629 return NULL;
630 }
631 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
632 child->dev_type = SAS_END_DEV;
633 rphy = sas_end_device_alloc(phy->port);
634 /* FIXME: error handling */
635 BUG_ON(!rphy);
636 child->tproto = phy->attached_tproto;
637 sas_init_dev(child);
638
639 child->rphy = rphy;
640 sas_fill_in_rphy(child, rphy);
641
642 spin_lock(&parent->port->dev_list_lock);
643 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
644 spin_unlock(&parent->port->dev_list_lock);
645
646 res = sas_discover_end_dev(child);
647 if (res) {
648 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
649 "at %016llx:0x%x returned 0x%x\n",
650 SAS_ADDR(child->sas_addr),
651 SAS_ADDR(parent->sas_addr), phy_id, res);
652 /* FIXME: this kfrees list elements without removing them */
653 //kfree(child);
654 return NULL;
655 }
656 } else {
657 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
658 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
659 phy_id);
660 }
661
662 list_add_tail(&child->siblings, &parent_ex->children);
663 return child;
664}
665
666static struct domain_device *sas_ex_discover_expander(
667 struct domain_device *parent, int phy_id)
668{
669 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
670 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
671 struct domain_device *child = NULL;
672 struct sas_rphy *rphy;
673 struct sas_expander_device *edev;
674 struct asd_sas_port *port;
675 int res;
676
677 if (phy->routing_attr == DIRECT_ROUTING) {
678 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
679 "allowed\n",
680 SAS_ADDR(parent->sas_addr), phy_id,
681 SAS_ADDR(phy->attached_sas_addr),
682 phy->attached_phy_id);
683 return NULL;
684 }
685 child = kzalloc(sizeof(*child), GFP_KERNEL);
686 if (!child)
687 return NULL;
688
689 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
690 /* FIXME: better error handling */
691 BUG_ON(sas_port_add(phy->port) != 0);
692
693
694 switch (phy->attached_dev_type) {
695 case EDGE_DEV:
696 rphy = sas_expander_alloc(phy->port,
697 SAS_EDGE_EXPANDER_DEVICE);
698 break;
699 case FANOUT_DEV:
700 rphy = sas_expander_alloc(phy->port,
701 SAS_FANOUT_EXPANDER_DEVICE);
702 break;
703 default:
704 rphy = NULL; /* shut gcc up */
705 BUG();
706 }
707 port = parent->port;
708 child->rphy = rphy;
709 edev = rphy_to_expander_device(rphy);
710 child->dev_type = phy->attached_dev_type;
711 child->parent = parent;
712 child->port = port;
713 child->iproto = phy->attached_iproto;
714 child->tproto = phy->attached_tproto;
715 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
716 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
717 sas_ex_get_linkrate(parent, child, phy);
718 edev->level = parent_ex->level + 1;
719 parent->port->disc.max_level = max(parent->port->disc.max_level,
720 edev->level);
721 sas_init_dev(child);
722 sas_fill_in_rphy(child, rphy);
723 sas_rphy_add(rphy);
724
725 spin_lock(&parent->port->dev_list_lock);
726 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
727 spin_unlock(&parent->port->dev_list_lock);
728
729 res = sas_discover_expander(child);
730 if (res) {
731 kfree(child);
732 return NULL;
733 }
734 list_add_tail(&child->siblings, &parent->ex_dev.children);
735 return child;
736}
737
738static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
739{
740 struct expander_device *ex = &dev->ex_dev;
741 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
742 struct domain_device *child = NULL;
743 int res = 0;
744
745 /* Phy state */
746 if (ex_phy->linkrate == PHY_SPINUP_HOLD) {
747 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET))
748 res = sas_ex_phy_discover(dev, phy_id);
749 if (res)
750 return res;
751 }
752
753 /* Parent and domain coherency */
754 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
755 SAS_ADDR(dev->port->sas_addr))) {
756 sas_add_parent_port(dev, phy_id);
757 return 0;
758 }
759 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
760 SAS_ADDR(dev->parent->sas_addr))) {
761 sas_add_parent_port(dev, phy_id);
762 if (ex_phy->routing_attr == TABLE_ROUTING)
763 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
764 return 0;
765 }
766
767 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
768 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
769
770 if (ex_phy->attached_dev_type == NO_DEVICE) {
771 if (ex_phy->routing_attr == DIRECT_ROUTING) {
772 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
773 sas_configure_routing(dev, ex_phy->attached_sas_addr);
774 }
775 return 0;
776 } else if (ex_phy->linkrate == PHY_LINKRATE_UNKNOWN)
777 return 0;
778
779 if (ex_phy->attached_dev_type != SAS_END_DEV &&
780 ex_phy->attached_dev_type != FANOUT_DEV &&
781 ex_phy->attached_dev_type != EDGE_DEV) {
782 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
783 "phy 0x%x\n", ex_phy->attached_dev_type,
784 SAS_ADDR(dev->sas_addr),
785 phy_id);
786 return 0;
787 }
788
789 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
790 if (res) {
791 SAS_DPRINTK("configure routing for dev %016llx "
792 "reported 0x%x. Forgotten\n",
793 SAS_ADDR(ex_phy->attached_sas_addr), res);
794 sas_disable_routing(dev, ex_phy->attached_sas_addr);
795 return res;
796 }
797
798 switch (ex_phy->attached_dev_type) {
799 case SAS_END_DEV:
800 child = sas_ex_discover_end_dev(dev, phy_id);
801 break;
802 case FANOUT_DEV:
803 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
804 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
805 "attached to ex %016llx phy 0x%x\n",
806 SAS_ADDR(ex_phy->attached_sas_addr),
807 ex_phy->attached_phy_id,
808 SAS_ADDR(dev->sas_addr),
809 phy_id);
810 sas_ex_disable_phy(dev, phy_id);
811 break;
812 } else
813 memcpy(dev->port->disc.fanout_sas_addr,
814 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
815 /* fallthrough */
816 case EDGE_DEV:
817 child = sas_ex_discover_expander(dev, phy_id);
818 break;
819 default:
820 break;
821 }
822
823 if (child) {
824 int i;
825
826 for (i = 0; i < ex->num_phys; i++) {
827 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
828 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
829 continue;
830
831 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
832 SAS_ADDR(child->sas_addr))
833 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
834 }
835 }
836
837 return res;
838}
839
840static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
841{
842 struct expander_device *ex = &dev->ex_dev;
843 int i;
844
845 for (i = 0; i < ex->num_phys; i++) {
846 struct ex_phy *phy = &ex->ex_phy[i];
847
848 if (phy->phy_state == PHY_VACANT ||
849 phy->phy_state == PHY_NOT_PRESENT)
850 continue;
851
852 if ((phy->attached_dev_type == EDGE_DEV ||
853 phy->attached_dev_type == FANOUT_DEV) &&
854 phy->routing_attr == SUBTRACTIVE_ROUTING) {
855
856 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
857
858 return 1;
859 }
860 }
861 return 0;
862}
863
864static int sas_check_level_subtractive_boundary(struct domain_device *dev)
865{
866 struct expander_device *ex = &dev->ex_dev;
867 struct domain_device *child;
868 u8 sub_addr[8] = {0, };
869
870 list_for_each_entry(child, &ex->children, siblings) {
871 if (child->dev_type != EDGE_DEV &&
872 child->dev_type != FANOUT_DEV)
873 continue;
874 if (sub_addr[0] == 0) {
875 sas_find_sub_addr(child, sub_addr);
876 continue;
877 } else {
878 u8 s2[8];
879
880 if (sas_find_sub_addr(child, s2) &&
881 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
882
883 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
884 "diverges from subtractive "
885 "boundary %016llx\n",
886 SAS_ADDR(dev->sas_addr),
887 SAS_ADDR(child->sas_addr),
888 SAS_ADDR(s2),
889 SAS_ADDR(sub_addr));
890
891 sas_ex_disable_port(child, s2);
892 }
893 }
894 }
895 return 0;
896}
897/**
898 * sas_ex_discover_devices -- discover devices attached to this expander
899 * dev: pointer to the expander domain device
900 * single: if you want to do a single phy, else set to -1;
901 *
902 * Configure this expander for use with its devices and register the
903 * devices of this expander.
904 */
905static int sas_ex_discover_devices(struct domain_device *dev, int single)
906{
907 struct expander_device *ex = &dev->ex_dev;
908 int i = 0, end = ex->num_phys;
909 int res = 0;
910
911 if (0 <= single && single < end) {
912 i = single;
913 end = i+1;
914 }
915
916 for ( ; i < end; i++) {
917 struct ex_phy *ex_phy = &ex->ex_phy[i];
918
919 if (ex_phy->phy_state == PHY_VACANT ||
920 ex_phy->phy_state == PHY_NOT_PRESENT ||
921 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
922 continue;
923
924 switch (ex_phy->linkrate) {
925 case PHY_DISABLED:
926 case PHY_RESET_PROBLEM:
927 case PHY_PORT_SELECTOR:
928 continue;
929 default:
930 res = sas_ex_discover_dev(dev, i);
931 if (res)
932 break;
933 continue;
934 }
935 }
936
937 if (!res)
938 sas_check_level_subtractive_boundary(dev);
939
940 return res;
941}
942
943static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
944{
945 struct expander_device *ex = &dev->ex_dev;
946 int i;
947 u8 *sub_sas_addr = NULL;
948
949 if (dev->dev_type != EDGE_DEV)
950 return 0;
951
952 for (i = 0; i < ex->num_phys; i++) {
953 struct ex_phy *phy = &ex->ex_phy[i];
954
955 if (phy->phy_state == PHY_VACANT ||
956 phy->phy_state == PHY_NOT_PRESENT)
957 continue;
958
959 if ((phy->attached_dev_type == FANOUT_DEV ||
960 phy->attached_dev_type == EDGE_DEV) &&
961 phy->routing_attr == SUBTRACTIVE_ROUTING) {
962
963 if (!sub_sas_addr)
964 sub_sas_addr = &phy->attached_sas_addr[0];
965 else if (SAS_ADDR(sub_sas_addr) !=
966 SAS_ADDR(phy->attached_sas_addr)) {
967
968 SAS_DPRINTK("ex %016llx phy 0x%x "
969 "diverges(%016llx) on subtractive "
970 "boundary(%016llx). Disabled\n",
971 SAS_ADDR(dev->sas_addr), i,
972 SAS_ADDR(phy->attached_sas_addr),
973 SAS_ADDR(sub_sas_addr));
974 sas_ex_disable_phy(dev, i);
975 }
976 }
977 }
978 return 0;
979}
980
981static void sas_print_parent_topology_bug(struct domain_device *child,
982 struct ex_phy *parent_phy,
983 struct ex_phy *child_phy)
984{
985 static const char ra_char[] = {
986 [DIRECT_ROUTING] = 'D',
987 [SUBTRACTIVE_ROUTING] = 'S',
988 [TABLE_ROUTING] = 'T',
989 };
990 static const char *ex_type[] = {
991 [EDGE_DEV] = "edge",
992 [FANOUT_DEV] = "fanout",
993 };
994 struct domain_device *parent = child->parent;
995
996 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
997 "has %c:%c routing link!\n",
998
999 ex_type[parent->dev_type],
1000 SAS_ADDR(parent->sas_addr),
1001 parent_phy->phy_id,
1002
1003 ex_type[child->dev_type],
1004 SAS_ADDR(child->sas_addr),
1005 child_phy->phy_id,
1006
1007 ra_char[parent_phy->routing_attr],
1008 ra_char[child_phy->routing_attr]);
1009}
1010
1011static int sas_check_eeds(struct domain_device *child,
1012 struct ex_phy *parent_phy,
1013 struct ex_phy *child_phy)
1014{
1015 int res = 0;
1016 struct domain_device *parent = child->parent;
1017
1018 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1019 res = -ENODEV;
1020 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1021 "phy S:0x%x, while there is a fanout ex %016llx\n",
1022 SAS_ADDR(parent->sas_addr),
1023 parent_phy->phy_id,
1024 SAS_ADDR(child->sas_addr),
1025 child_phy->phy_id,
1026 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1027 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1028 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1029 SAS_ADDR_SIZE);
1030 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1031 SAS_ADDR_SIZE);
1032 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1033 SAS_ADDR(parent->sas_addr)) ||
1034 (SAS_ADDR(parent->port->disc.eeds_a) ==
1035 SAS_ADDR(child->sas_addr)))
1036 &&
1037 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1038 SAS_ADDR(parent->sas_addr)) ||
1039 (SAS_ADDR(parent->port->disc.eeds_b) ==
1040 SAS_ADDR(child->sas_addr))))
1041 ;
1042 else {
1043 res = -ENODEV;
1044 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1045 "phy 0x%x link forms a third EEDS!\n",
1046 SAS_ADDR(parent->sas_addr),
1047 parent_phy->phy_id,
1048 SAS_ADDR(child->sas_addr),
1049 child_phy->phy_id);
1050 }
1051
1052 return res;
1053}
1054
1055/* Here we spill over 80 columns. It is intentional.
1056 */
1057static int sas_check_parent_topology(struct domain_device *child)
1058{
1059 struct expander_device *child_ex = &child->ex_dev;
1060 struct expander_device *parent_ex;
1061 int i;
1062 int res = 0;
1063
1064 if (!child->parent)
1065 return 0;
1066
1067 if (child->parent->dev_type != EDGE_DEV &&
1068 child->parent->dev_type != FANOUT_DEV)
1069 return 0;
1070
1071 parent_ex = &child->parent->ex_dev;
1072
1073 for (i = 0; i < parent_ex->num_phys; i++) {
1074 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1075 struct ex_phy *child_phy;
1076
1077 if (parent_phy->phy_state == PHY_VACANT ||
1078 parent_phy->phy_state == PHY_NOT_PRESENT)
1079 continue;
1080
1081 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1082 continue;
1083
1084 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1085
1086 switch (child->parent->dev_type) {
1087 case EDGE_DEV:
1088 if (child->dev_type == FANOUT_DEV) {
1089 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1090 child_phy->routing_attr != TABLE_ROUTING) {
1091 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1092 res = -ENODEV;
1093 }
1094 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1095 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1096 res = sas_check_eeds(child, parent_phy, child_phy);
1097 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1098 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1099 res = -ENODEV;
1100 }
1101 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1102 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1103 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1104 res = -ENODEV;
1105 }
1106 break;
1107 case FANOUT_DEV:
1108 if (parent_phy->routing_attr != TABLE_ROUTING ||
1109 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1110 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1111 res = -ENODEV;
1112 }
1113 break;
1114 default:
1115 break;
1116 }
1117 }
1118
1119 return res;
1120}
1121
1122#define RRI_REQ_SIZE 16
1123#define RRI_RESP_SIZE 44
1124
1125static int sas_configure_present(struct domain_device *dev, int phy_id,
1126 u8 *sas_addr, int *index, int *present)
1127{
1128 int i, res = 0;
1129 struct expander_device *ex = &dev->ex_dev;
1130 struct ex_phy *phy = &ex->ex_phy[phy_id];
1131 u8 *rri_req;
1132 u8 *rri_resp;
1133
1134 *present = 0;
1135 *index = 0;
1136
1137 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1138 if (!rri_req)
1139 return -ENOMEM;
1140
1141 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1142 if (!rri_resp) {
1143 kfree(rri_req);
1144 return -ENOMEM;
1145 }
1146
1147 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1148 rri_req[9] = phy_id;
1149
1150 for (i = 0; i < ex->max_route_indexes ; i++) {
1151 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1152 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1153 RRI_RESP_SIZE);
1154 if (res)
1155 goto out;
1156 res = rri_resp[2];
1157 if (res == SMP_RESP_NO_INDEX) {
1158 SAS_DPRINTK("overflow of indexes: dev %016llx "
1159 "phy 0x%x index 0x%x\n",
1160 SAS_ADDR(dev->sas_addr), phy_id, i);
1161 goto out;
1162 } else if (res != SMP_RESP_FUNC_ACC) {
1163 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1164 "result 0x%x\n", __FUNCTION__,
1165 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1166 goto out;
1167 }
1168 if (SAS_ADDR(sas_addr) != 0) {
1169 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1170 *index = i;
1171 if ((rri_resp[12] & 0x80) == 0x80)
1172 *present = 0;
1173 else
1174 *present = 1;
1175 goto out;
1176 } else if (SAS_ADDR(rri_resp+16) == 0) {
1177 *index = i;
1178 *present = 0;
1179 goto out;
1180 }
1181 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1182 phy->last_da_index < i) {
1183 phy->last_da_index = i;
1184 *index = i;
1185 *present = 0;
1186 goto out;
1187 }
1188 }
1189 res = -1;
1190out:
1191 kfree(rri_req);
1192 kfree(rri_resp);
1193 return res;
1194}
1195
1196#define CRI_REQ_SIZE 44
1197#define CRI_RESP_SIZE 8
1198
1199static int sas_configure_set(struct domain_device *dev, int phy_id,
1200 u8 *sas_addr, int index, int include)
1201{
1202 int res;
1203 u8 *cri_req;
1204 u8 *cri_resp;
1205
1206 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1207 if (!cri_req)
1208 return -ENOMEM;
1209
1210 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1211 if (!cri_resp) {
1212 kfree(cri_req);
1213 return -ENOMEM;
1214 }
1215
1216 cri_req[1] = SMP_CONF_ROUTE_INFO;
1217 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1218 cri_req[9] = phy_id;
1219 if (SAS_ADDR(sas_addr) == 0 || !include)
1220 cri_req[12] |= 0x80;
1221 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1222
1223 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1224 CRI_RESP_SIZE);
1225 if (res)
1226 goto out;
1227 res = cri_resp[2];
1228 if (res == SMP_RESP_NO_INDEX) {
1229 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1230 "index 0x%x\n",
1231 SAS_ADDR(dev->sas_addr), phy_id, index);
1232 }
1233out:
1234 kfree(cri_req);
1235 kfree(cri_resp);
1236 return res;
1237}
1238
1239static int sas_configure_phy(struct domain_device *dev, int phy_id,
1240 u8 *sas_addr, int include)
1241{
1242 int index;
1243 int present;
1244 int res;
1245
1246 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1247 if (res)
1248 return res;
1249 if (include ^ present)
1250 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1251
1252 return res;
1253}
1254
1255/**
1256 * sas_configure_parent -- configure routing table of parent
1257 * parent: parent expander
1258 * child: child expander
1259 * sas_addr: SAS port identifier of device directly attached to child
1260 */
1261static int sas_configure_parent(struct domain_device *parent,
1262 struct domain_device *child,
1263 u8 *sas_addr, int include)
1264{
1265 struct expander_device *ex_parent = &parent->ex_dev;
1266 int res = 0;
1267 int i;
1268
1269 if (parent->parent) {
1270 res = sas_configure_parent(parent->parent, parent, sas_addr,
1271 include);
1272 if (res)
1273 return res;
1274 }
1275
1276 if (ex_parent->conf_route_table == 0) {
1277 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1278 SAS_ADDR(parent->sas_addr));
1279 return 0;
1280 }
1281
1282 for (i = 0; i < ex_parent->num_phys; i++) {
1283 struct ex_phy *phy = &ex_parent->ex_phy[i];
1284
1285 if ((phy->routing_attr == TABLE_ROUTING) &&
1286 (SAS_ADDR(phy->attached_sas_addr) ==
1287 SAS_ADDR(child->sas_addr))) {
1288 res = sas_configure_phy(parent, i, sas_addr, include);
1289 if (res)
1290 return res;
1291 }
1292 }
1293
1294 return res;
1295}
1296
1297/**
1298 * sas_configure_routing -- configure routing
1299 * dev: expander device
1300 * sas_addr: port identifier of device directly attached to the expander device
1301 */
1302static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1303{
1304 if (dev->parent)
1305 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1306 return 0;
1307}
1308
1309static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1310{
1311 if (dev->parent)
1312 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1313 return 0;
1314}
1315
1316#if 0
1317#define SMP_BIN_ATTR_NAME "smp_portal"
1318
1319static void sas_ex_smp_hook(struct domain_device *dev)
1320{
1321 struct expander_device *ex_dev = &dev->ex_dev;
1322 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1323
1324 memset(bin_attr, 0, sizeof(*bin_attr));
1325
1326 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1327 bin_attr->attr.owner = THIS_MODULE;
1328 bin_attr->attr.mode = 0600;
1329
1330 bin_attr->size = 0;
1331 bin_attr->private = NULL;
1332 bin_attr->read = smp_portal_read;
1333 bin_attr->write= smp_portal_write;
1334 bin_attr->mmap = NULL;
1335
1336 ex_dev->smp_portal_pid = -1;
1337 init_MUTEX(&ex_dev->smp_sema);
1338}
1339#endif
1340
1341/**
1342 * sas_discover_expander -- expander discovery
1343 * @ex: pointer to expander domain device
1344 *
1345 * See comment in sas_discover_sata().
1346 */
1347static int sas_discover_expander(struct domain_device *dev)
1348{
1349 int res;
1350
1351 res = sas_notify_lldd_dev_found(dev);
1352 if (res)
1353 return res;
1354
1355 res = sas_ex_general(dev);
1356 if (res)
1357 goto out_err;
1358 res = sas_ex_manuf_info(dev);
1359 if (res)
1360 goto out_err;
1361
1362 res = sas_expander_discover(dev);
1363 if (res) {
1364 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1365 SAS_ADDR(dev->sas_addr), res);
1366 goto out_err;
1367 }
1368
1369 sas_check_ex_subtractive_boundary(dev);
1370 res = sas_check_parent_topology(dev);
1371 if (res)
1372 goto out_err;
1373 return 0;
1374out_err:
1375 sas_notify_lldd_dev_gone(dev);
1376 return res;
1377}
1378
1379static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1380{
1381 int res = 0;
1382 struct domain_device *dev;
1383
1384 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1385 if (dev->dev_type == EDGE_DEV ||
1386 dev->dev_type == FANOUT_DEV) {
1387 struct sas_expander_device *ex =
1388 rphy_to_expander_device(dev->rphy);
1389
1390 if (level == ex->level)
1391 res = sas_ex_discover_devices(dev, -1);
1392 else if (level > 0)
1393 res = sas_ex_discover_devices(port->port_dev, -1);
1394
1395 }
1396 }
1397
1398 return res;
1399}
1400
1401static int sas_ex_bfs_disc(struct asd_sas_port *port)
1402{
1403 int res;
1404 int level;
1405
1406 do {
1407 level = port->disc.max_level;
1408 res = sas_ex_level_discovery(port, level);
1409 mb();
1410 } while (level < port->disc.max_level);
1411
1412 return res;
1413}
1414
1415int sas_discover_root_expander(struct domain_device *dev)
1416{
1417 int res;
1418 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1419
1420 sas_rphy_add(dev->rphy);
1421
1422 ex->level = dev->port->disc.max_level; /* 0 */
1423 res = sas_discover_expander(dev);
1424 if (!res)
1425 sas_ex_bfs_disc(dev->port);
1426
1427 return res;
1428}
1429
1430/* ---------- Domain revalidation ---------- */
1431
1432static int sas_get_phy_discover(struct domain_device *dev,
1433 int phy_id, struct smp_resp *disc_resp)
1434{
1435 int res;
1436 u8 *disc_req;
1437
1438 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1439 if (!disc_req)
1440 return -ENOMEM;
1441
1442 disc_req[1] = SMP_DISCOVER;
1443 disc_req[9] = phy_id;
1444
1445 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1446 disc_resp, DISCOVER_RESP_SIZE);
1447 if (res)
1448 goto out;
1449 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1450 res = disc_resp->result;
1451 goto out;
1452 }
1453out:
1454 kfree(disc_req);
1455 return res;
1456}
1457
1458static int sas_get_phy_change_count(struct domain_device *dev,
1459 int phy_id, int *pcc)
1460{
1461 int res;
1462 struct smp_resp *disc_resp;
1463
1464 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1465 if (!disc_resp)
1466 return -ENOMEM;
1467
1468 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1469 if (!res)
1470 *pcc = disc_resp->disc.change_count;
1471
1472 kfree(disc_resp);
1473 return res;
1474}
1475
1476static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1477 int phy_id, u8 *attached_sas_addr)
1478{
1479 int res;
1480 struct smp_resp *disc_resp;
1481 struct discover_resp *dr;
1482
1483 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1484 if (!disc_resp)
1485 return -ENOMEM;
1486 dr = &disc_resp->disc;
1487
1488 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1489 if (!res) {
1490 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1491 if (dr->attached_dev_type == 0)
1492 memset(attached_sas_addr, 0, 8);
1493 }
1494 kfree(disc_resp);
1495 return res;
1496}
1497
1498static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1499 int from_phy)
1500{
1501 struct expander_device *ex = &dev->ex_dev;
1502 int res = 0;
1503 int i;
1504
1505 for (i = from_phy; i < ex->num_phys; i++) {
1506 int phy_change_count = 0;
1507
1508 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1509 if (res)
1510 goto out;
1511 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1512 ex->ex_phy[i].phy_change_count = phy_change_count;
1513 *phy_id = i;
1514 return 0;
1515 }
1516 }
1517out:
1518 return res;
1519}
1520
1521static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1522{
1523 int res;
1524 u8 *rg_req;
1525 struct smp_resp *rg_resp;
1526
1527 rg_req = alloc_smp_req(RG_REQ_SIZE);
1528 if (!rg_req)
1529 return -ENOMEM;
1530
1531 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1532 if (!rg_resp) {
1533 kfree(rg_req);
1534 return -ENOMEM;
1535 }
1536
1537 rg_req[1] = SMP_REPORT_GENERAL;
1538
1539 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1540 RG_RESP_SIZE);
1541 if (res)
1542 goto out;
1543 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1544 res = rg_resp->result;
1545 goto out;
1546 }
1547
1548 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1549out:
1550 kfree(rg_resp);
1551 kfree(rg_req);
1552 return res;
1553}
1554
1555static int sas_find_bcast_dev(struct domain_device *dev,
1556 struct domain_device **src_dev)
1557{
1558 struct expander_device *ex = &dev->ex_dev;
1559 int ex_change_count = -1;
1560 int res;
1561
1562 res = sas_get_ex_change_count(dev, &ex_change_count);
1563 if (res)
1564 goto out;
1565 if (ex_change_count != -1 &&
1566 ex_change_count != ex->ex_change_count) {
1567 *src_dev = dev;
1568 ex->ex_change_count = ex_change_count;
1569 } else {
1570 struct domain_device *ch;
1571
1572 list_for_each_entry(ch, &ex->children, siblings) {
1573 if (ch->dev_type == EDGE_DEV ||
1574 ch->dev_type == FANOUT_DEV) {
1575 res = sas_find_bcast_dev(ch, src_dev);
1576 if (src_dev)
1577 return res;
1578 }
1579 }
1580 }
1581out:
1582 return res;
1583}
1584
1585static void sas_unregister_ex_tree(struct domain_device *dev)
1586{
1587 struct expander_device *ex = &dev->ex_dev;
1588 struct domain_device *child, *n;
1589
1590 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1591 if (child->dev_type == EDGE_DEV ||
1592 child->dev_type == FANOUT_DEV)
1593 sas_unregister_ex_tree(child);
1594 else
1595 sas_unregister_dev(child);
1596 }
1597 sas_unregister_dev(dev);
1598}
1599
1600static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1601 int phy_id)
1602{
1603 struct expander_device *ex_dev = &parent->ex_dev;
1604 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1605 struct domain_device *child, *n;
1606
1607 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1608 if (SAS_ADDR(child->sas_addr) ==
1609 SAS_ADDR(phy->attached_sas_addr)) {
1610 if (child->dev_type == EDGE_DEV ||
1611 child->dev_type == FANOUT_DEV)
1612 sas_unregister_ex_tree(child);
1613 else
1614 sas_unregister_dev(child);
1615 break;
1616 }
1617 }
1618 sas_disable_routing(parent, phy->attached_sas_addr);
1619 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1620 sas_port_delete_phy(phy->port, phy->phy);
1621 if (phy->port->num_phys == 0)
1622 sas_port_delete(phy->port);
1623 phy->port = NULL;
1624}
1625
1626static int sas_discover_bfs_by_root_level(struct domain_device *root,
1627 const int level)
1628{
1629 struct expander_device *ex_root = &root->ex_dev;
1630 struct domain_device *child;
1631 int res = 0;
1632
1633 list_for_each_entry(child, &ex_root->children, siblings) {
1634 if (child->dev_type == EDGE_DEV ||
1635 child->dev_type == FANOUT_DEV) {
1636 struct sas_expander_device *ex =
1637 rphy_to_expander_device(child->rphy);
1638
1639 if (level > ex->level)
1640 res = sas_discover_bfs_by_root_level(child,
1641 level);
1642 else if (level == ex->level)
1643 res = sas_ex_discover_devices(child, -1);
1644 }
1645 }
1646 return res;
1647}
1648
1649static int sas_discover_bfs_by_root(struct domain_device *dev)
1650{
1651 int res;
1652 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1653 int level = ex->level+1;
1654
1655 res = sas_ex_discover_devices(dev, -1);
1656 if (res)
1657 goto out;
1658 do {
1659 res = sas_discover_bfs_by_root_level(dev, level);
1660 mb();
1661 level += 1;
1662 } while (level <= dev->port->disc.max_level);
1663out:
1664 return res;
1665}
1666
1667static int sas_discover_new(struct domain_device *dev, int phy_id)
1668{
1669 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1670 struct domain_device *child;
1671 int res;
1672
1673 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1674 SAS_ADDR(dev->sas_addr), phy_id);
1675 res = sas_ex_phy_discover(dev, phy_id);
1676 if (res)
1677 goto out;
1678 res = sas_ex_discover_devices(dev, phy_id);
1679 if (res)
1680 goto out;
1681 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1682 if (SAS_ADDR(child->sas_addr) ==
1683 SAS_ADDR(ex_phy->attached_sas_addr)) {
1684 if (child->dev_type == EDGE_DEV ||
1685 child->dev_type == FANOUT_DEV)
1686 res = sas_discover_bfs_by_root(child);
1687 break;
1688 }
1689 }
1690out:
1691 return res;
1692}
1693
1694static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1695{
1696 struct expander_device *ex = &dev->ex_dev;
1697 struct ex_phy *phy = &ex->ex_phy[phy_id];
1698 u8 attached_sas_addr[8];
1699 int res;
1700
1701 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1702 switch (res) {
1703 case SMP_RESP_NO_PHY:
1704 phy->phy_state = PHY_NOT_PRESENT;
1705 sas_unregister_devs_sas_addr(dev, phy_id);
1706 goto out; break;
1707 case SMP_RESP_PHY_VACANT:
1708 phy->phy_state = PHY_VACANT;
1709 sas_unregister_devs_sas_addr(dev, phy_id);
1710 goto out; break;
1711 case SMP_RESP_FUNC_ACC:
1712 break;
1713 }
1714
1715 if (SAS_ADDR(attached_sas_addr) == 0) {
1716 phy->phy_state = PHY_EMPTY;
1717 sas_unregister_devs_sas_addr(dev, phy_id);
1718 } else if (SAS_ADDR(attached_sas_addr) ==
1719 SAS_ADDR(phy->attached_sas_addr)) {
1720 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1721 SAS_ADDR(dev->sas_addr), phy_id);
1722 } else
1723 res = sas_discover_new(dev, phy_id);
1724out:
1725 return res;
1726}
1727
1728static int sas_rediscover(struct domain_device *dev, const int phy_id)
1729{
1730 struct expander_device *ex = &dev->ex_dev;
1731 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1732 int res = 0;
1733 int i;
1734
1735 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1736 SAS_ADDR(dev->sas_addr), phy_id);
1737
1738 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1739 for (i = 0; i < ex->num_phys; i++) {
1740 struct ex_phy *phy = &ex->ex_phy[i];
1741
1742 if (i == phy_id)
1743 continue;
1744 if (SAS_ADDR(phy->attached_sas_addr) ==
1745 SAS_ADDR(changed_phy->attached_sas_addr)) {
1746 SAS_DPRINTK("phy%d part of wide port with "
1747 "phy%d\n", phy_id, i);
1748 goto out;
1749 }
1750 }
1751 res = sas_rediscover_dev(dev, phy_id);
1752 } else
1753 res = sas_discover_new(dev, phy_id);
1754out:
1755 return res;
1756}
1757
1758/**
1759 * sas_revalidate_domain -- revalidate the domain
1760 * @port: port to the domain of interest
1761 *
1762 * NOTE: this process _must_ quit (return) as soon as any connection
1763 * errors are encountered. Connection recovery is done elsewhere.
1764 * Discover process only interrogates devices in order to discover the
1765 * domain.
1766 */
1767int sas_ex_revalidate_domain(struct domain_device *port_dev)
1768{
1769 int res;
1770 struct domain_device *dev = NULL;
1771
1772 res = sas_find_bcast_dev(port_dev, &dev);
1773 if (res)
1774 goto out;
1775 if (dev) {
1776 struct expander_device *ex = &dev->ex_dev;
1777 int i = 0, phy_id;
1778
1779 do {
1780 phy_id = -1;
1781 res = sas_find_bcast_phy(dev, &phy_id, i);
1782 if (phy_id == -1)
1783 break;
1784 res = sas_rediscover(dev, phy_id);
1785 i = phy_id + 1;
1786 } while (i < ex->num_phys);
1787 }
1788out:
1789 return res;
1790}
1791
1792#if 0
1793/* ---------- SMP portal ---------- */
1794
1795static ssize_t smp_portal_write(struct kobject *kobj, char *buf, loff_t offs,
1796 size_t size)
1797{
1798 struct domain_device *dev = to_dom_device(kobj);
1799 struct expander_device *ex = &dev->ex_dev;
1800
1801 if (offs != 0)
1802 return -EFBIG;
1803 else if (size == 0)
1804 return 0;
1805
1806 down_interruptible(&ex->smp_sema);
1807 if (ex->smp_req)
1808 kfree(ex->smp_req);
1809 ex->smp_req = kzalloc(size, GFP_USER);
1810 if (!ex->smp_req) {
1811 up(&ex->smp_sema);
1812 return -ENOMEM;
1813 }
1814 memcpy(ex->smp_req, buf, size);
1815 ex->smp_req_size = size;
1816 ex->smp_portal_pid = current->pid;
1817 up(&ex->smp_sema);
1818
1819 return size;
1820}
1821
1822static ssize_t smp_portal_read(struct kobject *kobj, char *buf, loff_t offs,
1823 size_t size)
1824{
1825 struct domain_device *dev = to_dom_device(kobj);
1826 struct expander_device *ex = &dev->ex_dev;
1827 u8 *smp_resp;
1828 int res = -EINVAL;
1829
1830 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1831 * it should be 0.
1832 */
1833
1834 down_interruptible(&ex->smp_sema);
1835 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1836 goto out;
1837
1838 res = 0;
1839 if (size == 0)
1840 goto out;
1841
1842 res = -ENOMEM;
1843 smp_resp = alloc_smp_resp(size);
1844 if (!smp_resp)
1845 goto out;
1846 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1847 smp_resp, size);
1848 if (!res) {
1849 memcpy(buf, smp_resp, size);
1850 res = size;
1851 }
1852
1853 kfree(smp_resp);
1854out:
1855 kfree(ex->smp_req);
1856 ex->smp_req = NULL;
1857 ex->smp_req_size = 0;
1858 ex->smp_portal_pid = -1;
1859 up(&ex->smp_sema);
1860 return res;
1861}
1862#endif
diff --git a/drivers/scsi/libsas/sas_init.c b/drivers/scsi/libsas/sas_init.c
new file mode 100644
index 000000000000..b961664b8106
--- /dev/null
+++ b/drivers/scsi/libsas/sas_init.c
@@ -0,0 +1,227 @@
1/*
2 * Serial Attached SCSI (SAS) Transport Layer initialization
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/spinlock.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34
35#include "sas_internal.h"
36
37#include "../scsi_sas_internal.h"
38
39kmem_cache_t *sas_task_cache;
40
41/*------------ SAS addr hash -----------*/
42void sas_hash_addr(u8 *hashed, const u8 *sas_addr)
43{
44 const u32 poly = 0x00DB2777;
45 u32 r = 0;
46 int i;
47
48 for (i = 0; i < 8; i++) {
49 int b;
50 for (b = 7; b >= 0; b--) {
51 r <<= 1;
52 if ((1 << b) & sas_addr[i]) {
53 if (!(r & 0x01000000))
54 r ^= poly;
55 } else if (r & 0x01000000)
56 r ^= poly;
57 }
58 }
59
60 hashed[0] = (r >> 16) & 0xFF;
61 hashed[1] = (r >> 8) & 0xFF ;
62 hashed[2] = r & 0xFF;
63}
64
65
66/* ---------- HA events ---------- */
67
68void sas_hae_reset(void *data)
69{
70 struct sas_ha_struct *ha = data;
71
72 sas_begin_event(HAE_RESET, &ha->event_lock,
73 &ha->pending);
74}
75
76int sas_register_ha(struct sas_ha_struct *sas_ha)
77{
78 int error = 0;
79
80 spin_lock_init(&sas_ha->phy_port_lock);
81 sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr);
82
83 if (sas_ha->lldd_queue_size == 0)
84 sas_ha->lldd_queue_size = 1;
85 else if (sas_ha->lldd_queue_size == -1)
86 sas_ha->lldd_queue_size = 128; /* Sanity */
87
88 error = sas_register_phys(sas_ha);
89 if (error) {
90 printk(KERN_NOTICE "couldn't register sas phys:%d\n", error);
91 return error;
92 }
93
94 error = sas_register_ports(sas_ha);
95 if (error) {
96 printk(KERN_NOTICE "couldn't register sas ports:%d\n", error);
97 goto Undo_phys;
98 }
99
100 error = sas_init_events(sas_ha);
101 if (error) {
102 printk(KERN_NOTICE "couldn't start event thread:%d\n", error);
103 goto Undo_ports;
104 }
105
106 if (sas_ha->lldd_max_execute_num > 1) {
107 error = sas_init_queue(sas_ha);
108 if (error) {
109 printk(KERN_NOTICE "couldn't start queue thread:%d, "
110 "running in direct mode\n", error);
111 sas_ha->lldd_max_execute_num = 1;
112 }
113 }
114
115 return 0;
116
117Undo_ports:
118 sas_unregister_ports(sas_ha);
119Undo_phys:
120
121 return error;
122}
123
124int sas_unregister_ha(struct sas_ha_struct *sas_ha)
125{
126 if (sas_ha->lldd_max_execute_num > 1) {
127 sas_shutdown_queue(sas_ha);
128 }
129
130 sas_unregister_ports(sas_ha);
131
132 return 0;
133}
134
135static int sas_get_linkerrors(struct sas_phy *phy)
136{
137 if (scsi_is_sas_phy_local(phy))
138 /* FIXME: we have no local phy stats
139 * gathering at this time */
140 return -EINVAL;
141
142 return sas_smp_get_phy_events(phy);
143}
144
145static int sas_phy_reset(struct sas_phy *phy, int hard_reset)
146{
147 int ret;
148 enum phy_func reset_type;
149
150 if (hard_reset)
151 reset_type = PHY_FUNC_HARD_RESET;
152 else
153 reset_type = PHY_FUNC_LINK_RESET;
154
155 if (scsi_is_sas_phy_local(phy)) {
156 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
157 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
158 struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number];
159 struct sas_internal *i =
160 to_sas_internal(sas_ha->core.shost->transportt);
161
162 ret = i->dft->lldd_control_phy(asd_phy, reset_type);
163 } else {
164 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
165 struct domain_device *ddev = sas_find_dev_by_rphy(rphy);
166 ret = sas_smp_phy_control(ddev, phy->number, reset_type);
167 }
168 return ret;
169}
170
171static struct sas_function_template sft = {
172 .phy_reset = sas_phy_reset,
173 .get_linkerrors = sas_get_linkerrors,
174};
175
176struct scsi_transport_template *
177sas_domain_attach_transport(struct sas_domain_function_template *dft)
178{
179 struct scsi_transport_template *stt = sas_attach_transport(&sft);
180 struct sas_internal *i;
181
182 if (!stt)
183 return stt;
184
185 i = to_sas_internal(stt);
186 i->dft = dft;
187 stt->create_work_queue = 1;
188 stt->eh_timed_out = sas_scsi_timed_out;
189 stt->eh_strategy_handler = sas_scsi_recover_host;
190
191 return stt;
192}
193EXPORT_SYMBOL_GPL(sas_domain_attach_transport);
194
195
196void sas_domain_release_transport(struct scsi_transport_template *stt)
197{
198 sas_release_transport(stt);
199}
200EXPORT_SYMBOL_GPL(sas_domain_release_transport);
201
202/* ---------- SAS Class register/unregister ---------- */
203
204static int __init sas_class_init(void)
205{
206 sas_task_cache = kmem_cache_create("sas_task", sizeof(struct sas_task),
207 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
208 if (!sas_task_cache)
209 return -ENOMEM;
210
211 return 0;
212}
213
214static void __exit sas_class_exit(void)
215{
216 kmem_cache_destroy(sas_task_cache);
217}
218
219MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>");
220MODULE_DESCRIPTION("SAS Transport Layer");
221MODULE_LICENSE("GPL v2");
222
223module_init(sas_class_init);
224module_exit(sas_class_exit);
225
226EXPORT_SYMBOL_GPL(sas_register_ha);
227EXPORT_SYMBOL_GPL(sas_unregister_ha);
diff --git a/drivers/scsi/libsas/sas_internal.h b/drivers/scsi/libsas/sas_internal.h
new file mode 100644
index 000000000000..89c397680846
--- /dev/null
+++ b/drivers/scsi/libsas/sas_internal.h
@@ -0,0 +1,146 @@
1/*
2 * Serial Attached SCSI (SAS) class internal header file
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#ifndef _SAS_INTERNAL_H_
27#define _SAS_INTERNAL_H_
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_host.h>
31#include <scsi/scsi_transport_sas.h>
32#include <scsi/libsas.h>
33
34#define sas_printk(fmt, ...) printk(KERN_NOTICE "sas: " fmt, ## __VA_ARGS__)
35
36#ifdef SAS_DEBUG
37#define SAS_DPRINTK(fmt, ...) printk(KERN_NOTICE "sas: " fmt, ## __VA_ARGS__)
38#else
39#define SAS_DPRINTK(fmt, ...)
40#endif
41
42void sas_scsi_recover_host(struct Scsi_Host *shost);
43
44int sas_show_class(enum sas_class class, char *buf);
45int sas_show_proto(enum sas_proto proto, char *buf);
46int sas_show_linkrate(enum sas_phy_linkrate linkrate, char *buf);
47int sas_show_oob_mode(enum sas_oob_mode oob_mode, char *buf);
48
49int sas_register_phys(struct sas_ha_struct *sas_ha);
50void sas_unregister_phys(struct sas_ha_struct *sas_ha);
51
52int sas_register_ports(struct sas_ha_struct *sas_ha);
53void sas_unregister_ports(struct sas_ha_struct *sas_ha);
54
55enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *);
56
57int sas_init_queue(struct sas_ha_struct *sas_ha);
58int sas_init_events(struct sas_ha_struct *sas_ha);
59void sas_shutdown_queue(struct sas_ha_struct *sas_ha);
60
61void sas_deform_port(struct asd_sas_phy *phy);
62
63void sas_porte_bytes_dmaed(void *);
64void sas_porte_broadcast_rcvd(void *);
65void sas_porte_link_reset_err(void *);
66void sas_porte_timer_event(void *);
67void sas_porte_hard_reset(void *);
68
69int sas_notify_lldd_dev_found(struct domain_device *);
70void sas_notify_lldd_dev_gone(struct domain_device *);
71
72int sas_smp_phy_control(struct domain_device *dev, int phy_id,
73 enum phy_func phy_func);
74int sas_smp_get_phy_events(struct sas_phy *phy);
75
76struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy);
77
78void sas_hae_reset(void *);
79
80static inline void sas_queue_event(int event, spinlock_t *lock,
81 unsigned long *pending,
82 struct work_struct *work,
83 struct Scsi_Host *shost)
84{
85 unsigned long flags;
86
87 spin_lock_irqsave(lock, flags);
88 if (test_bit(event, pending)) {
89 spin_unlock_irqrestore(lock, flags);
90 return;
91 }
92 __set_bit(event, pending);
93 spin_unlock_irqrestore(lock, flags);
94 scsi_queue_work(shost, work);
95}
96
97static inline void sas_begin_event(int event, spinlock_t *lock,
98 unsigned long *pending)
99{
100 unsigned long flags;
101
102 spin_lock_irqsave(lock, flags);
103 __clear_bit(event, pending);
104 spin_unlock_irqrestore(lock, flags);
105}
106
107static inline void sas_fill_in_rphy(struct domain_device *dev,
108 struct sas_rphy *rphy)
109{
110 rphy->identify.sas_address = SAS_ADDR(dev->sas_addr);
111 rphy->identify.initiator_port_protocols = dev->iproto;
112 rphy->identify.target_port_protocols = dev->tproto;
113 switch (dev->dev_type) {
114 case SATA_DEV:
115 /* FIXME: need sata device type */
116 case SAS_END_DEV:
117 rphy->identify.device_type = SAS_END_DEVICE;
118 break;
119 case EDGE_DEV:
120 rphy->identify.device_type = SAS_EDGE_EXPANDER_DEVICE;
121 break;
122 case FANOUT_DEV:
123 rphy->identify.device_type = SAS_FANOUT_EXPANDER_DEVICE;
124 break;
125 default:
126 rphy->identify.device_type = SAS_PHY_UNUSED;
127 break;
128 }
129}
130
131static inline void sas_add_parent_port(struct domain_device *dev, int phy_id)
132{
133 struct expander_device *ex = &dev->ex_dev;
134 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
135
136 if (!ex->parent_port) {
137 ex->parent_port = sas_port_alloc(&dev->rphy->dev, phy_id);
138 /* FIXME: error handling */
139 BUG_ON(!ex->parent_port);
140 BUG_ON(sas_port_add(ex->parent_port));
141 sas_port_mark_backlink(ex->parent_port);
142 }
143 sas_port_add_phy(ex->parent_port, ex_phy->phy);
144}
145
146#endif /* _SAS_INTERNAL_H_ */
diff --git a/drivers/scsi/libsas/sas_phy.c b/drivers/scsi/libsas/sas_phy.c
new file mode 100644
index 000000000000..024ab00e70d2
--- /dev/null
+++ b/drivers/scsi/libsas/sas_phy.c
@@ -0,0 +1,157 @@
1/*
2 * Serial Attached SCSI (SAS) Phy class
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
25#include "sas_internal.h"
26#include <scsi/scsi_host.h>
27#include <scsi/scsi_transport.h>
28#include <scsi/scsi_transport_sas.h>
29#include "../scsi_sas_internal.h"
30
31/* ---------- Phy events ---------- */
32
33static void sas_phye_loss_of_signal(void *data)
34{
35 struct asd_sas_phy *phy = data;
36
37 sas_begin_event(PHYE_LOSS_OF_SIGNAL, &phy->ha->event_lock,
38 &phy->phy_events_pending);
39 phy->error = 0;
40 sas_deform_port(phy);
41}
42
43static void sas_phye_oob_done(void *data)
44{
45 struct asd_sas_phy *phy = data;
46
47 sas_begin_event(PHYE_OOB_DONE, &phy->ha->event_lock,
48 &phy->phy_events_pending);
49 phy->error = 0;
50}
51
52static void sas_phye_oob_error(void *data)
53{
54 struct asd_sas_phy *phy = data;
55 struct sas_ha_struct *sas_ha = phy->ha;
56 struct asd_sas_port *port = phy->port;
57 struct sas_internal *i =
58 to_sas_internal(sas_ha->core.shost->transportt);
59
60 sas_begin_event(PHYE_OOB_ERROR, &phy->ha->event_lock,
61 &phy->phy_events_pending);
62
63 sas_deform_port(phy);
64
65 if (!port && phy->enabled && i->dft->lldd_control_phy) {
66 phy->error++;
67 switch (phy->error) {
68 case 1:
69 case 2:
70 i->dft->lldd_control_phy(phy, PHY_FUNC_HARD_RESET);
71 break;
72 case 3:
73 default:
74 phy->error = 0;
75 phy->enabled = 0;
76 i->dft->lldd_control_phy(phy, PHY_FUNC_DISABLE);
77 break;
78 }
79 }
80}
81
82static void sas_phye_spinup_hold(void *data)
83{
84 struct asd_sas_phy *phy = data;
85 struct sas_ha_struct *sas_ha = phy->ha;
86 struct sas_internal *i =
87 to_sas_internal(sas_ha->core.shost->transportt);
88
89 sas_begin_event(PHYE_SPINUP_HOLD, &phy->ha->event_lock,
90 &phy->phy_events_pending);
91
92 phy->error = 0;
93 i->dft->lldd_control_phy(phy, PHY_FUNC_RELEASE_SPINUP_HOLD);
94}
95
96/* ---------- Phy class registration ---------- */
97
98int sas_register_phys(struct sas_ha_struct *sas_ha)
99{
100 int i;
101
102 static void (*sas_phy_event_fns[PHY_NUM_EVENTS])(void *) = {
103 [PHYE_LOSS_OF_SIGNAL] = sas_phye_loss_of_signal,
104 [PHYE_OOB_DONE] = sas_phye_oob_done,
105 [PHYE_OOB_ERROR] = sas_phye_oob_error,
106 [PHYE_SPINUP_HOLD] = sas_phye_spinup_hold,
107 };
108
109 static void (*sas_port_event_fns[PORT_NUM_EVENTS])(void *) = {
110 [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
111 [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
112 [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
113 [PORTE_TIMER_EVENT] = sas_porte_timer_event,
114 [PORTE_HARD_RESET] = sas_porte_hard_reset,
115 };
116
117 /* Now register the phys. */
118 for (i = 0; i < sas_ha->num_phys; i++) {
119 int k;
120 struct asd_sas_phy *phy = sas_ha->sas_phy[i];
121
122 phy->error = 0;
123 INIT_LIST_HEAD(&phy->port_phy_el);
124 for (k = 0; k < PORT_NUM_EVENTS; k++)
125 INIT_WORK(&phy->port_events[k], sas_port_event_fns[k],
126 phy);
127
128 for (k = 0; k < PHY_NUM_EVENTS; k++)
129 INIT_WORK(&phy->phy_events[k], sas_phy_event_fns[k],
130 phy);
131 phy->port = NULL;
132 phy->ha = sas_ha;
133 spin_lock_init(&phy->frame_rcvd_lock);
134 spin_lock_init(&phy->sas_prim_lock);
135 phy->frame_rcvd_size = 0;
136
137 phy->phy = sas_phy_alloc(&sas_ha->core.shost->shost_gendev,
138 i);
139 if (!phy->phy)
140 return -ENOMEM;
141
142 phy->phy->identify.initiator_port_protocols =
143 phy->iproto;
144 phy->phy->identify.target_port_protocols = phy->tproto;
145 phy->phy->identify.sas_address = SAS_ADDR(sas_ha->sas_addr);
146 phy->phy->identify.phy_identifier = i;
147 phy->phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
148 phy->phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
149 phy->phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
150 phy->phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
151 phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
152
153 sas_phy_add(phy->phy);
154 }
155
156 return 0;
157}
diff --git a/drivers/scsi/libsas/sas_port.c b/drivers/scsi/libsas/sas_port.c
new file mode 100644
index 000000000000..253cdcf306a2
--- /dev/null
+++ b/drivers/scsi/libsas/sas_port.c
@@ -0,0 +1,279 @@
1/*
2 * Serial Attached SCSI (SAS) Port class
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
25#include "sas_internal.h"
26
27#include <scsi/scsi_transport.h>
28#include <scsi/scsi_transport_sas.h>
29#include "../scsi_sas_internal.h"
30
31/**
32 * sas_form_port -- add this phy to a port
33 * @phy: the phy of interest
34 *
35 * This function adds this phy to an existing port, thus creating a wide
36 * port, or it creates a port and adds the phy to the port.
37 */
38static void sas_form_port(struct asd_sas_phy *phy)
39{
40 int i;
41 struct sas_ha_struct *sas_ha = phy->ha;
42 struct asd_sas_port *port = phy->port;
43 struct sas_internal *si =
44 to_sas_internal(sas_ha->core.shost->transportt);
45
46 if (port) {
47 if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
48 SAS_ADDR_SIZE) == 0)
49 sas_deform_port(phy);
50 else {
51 SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
52 __FUNCTION__, phy->id, phy->port->id,
53 phy->port->num_phys);
54 return;
55 }
56 }
57
58 /* find a port */
59 spin_lock(&sas_ha->phy_port_lock);
60 for (i = 0; i < sas_ha->num_phys; i++) {
61 port = sas_ha->sas_port[i];
62 spin_lock(&port->phy_list_lock);
63 if (*(u64 *) port->sas_addr &&
64 memcmp(port->attached_sas_addr,
65 phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
66 port->num_phys > 0) {
67 /* wide port */
68 SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
69 port->id);
70 break;
71 } else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
72 memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
73 break;
74 }
75 spin_unlock(&port->phy_list_lock);
76 }
77
78 if (i >= sas_ha->num_phys) {
79 printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
80 __FUNCTION__);
81 spin_unlock(&sas_ha->phy_port_lock);
82 return;
83 }
84
85 /* add the phy to the port */
86 list_add_tail(&phy->port_phy_el, &port->phy_list);
87 phy->port = port;
88 port->num_phys++;
89 port->phy_mask |= (1U << phy->id);
90
91 if (!port->phy)
92 port->phy = phy->phy;
93
94 SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
95 port->id, port->phy_mask);
96
97 if (*(u64 *)port->attached_sas_addr == 0) {
98 port->class = phy->class;
99 memcpy(port->attached_sas_addr, phy->attached_sas_addr,
100 SAS_ADDR_SIZE);
101 port->iproto = phy->iproto;
102 port->tproto = phy->tproto;
103 port->oob_mode = phy->oob_mode;
104 port->linkrate = phy->linkrate;
105 } else
106 port->linkrate = max(port->linkrate, phy->linkrate);
107 spin_unlock(&port->phy_list_lock);
108 spin_unlock(&sas_ha->phy_port_lock);
109
110 if (!port->port) {
111 port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
112 BUG_ON(!port->port);
113 sas_port_add(port->port);
114 }
115 sas_port_add_phy(port->port, phy->phy);
116
117 if (port->port_dev)
118 port->port_dev->pathways = port->num_phys;
119
120 /* Tell the LLDD about this port formation. */
121 if (si->dft->lldd_port_formed)
122 si->dft->lldd_port_formed(phy);
123
124 sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
125}
126
127/**
128 * sas_deform_port -- remove this phy from the port it belongs to
129 * @phy: the phy of interest
130 *
131 * This is called when the physical link to the other phy has been
132 * lost (on this phy), in Event thread context. We cannot delay here.
133 */
134void sas_deform_port(struct asd_sas_phy *phy)
135{
136 struct sas_ha_struct *sas_ha = phy->ha;
137 struct asd_sas_port *port = phy->port;
138 struct sas_internal *si =
139 to_sas_internal(sas_ha->core.shost->transportt);
140
141 if (!port)
142 return; /* done by a phy event */
143
144 if (port->port_dev)
145 port->port_dev->pathways--;
146
147 if (port->num_phys == 1) {
148 sas_unregister_domain_devices(port);
149 sas_port_delete(port->port);
150 port->port = NULL;
151 } else
152 sas_port_delete_phy(port->port, phy->phy);
153
154
155 if (si->dft->lldd_port_deformed)
156 si->dft->lldd_port_deformed(phy);
157
158 spin_lock(&sas_ha->phy_port_lock);
159 spin_lock(&port->phy_list_lock);
160
161 list_del_init(&phy->port_phy_el);
162 phy->port = NULL;
163 port->num_phys--;
164 port->phy_mask &= ~(1U << phy->id);
165
166 if (port->num_phys == 0) {
167 INIT_LIST_HEAD(&port->phy_list);
168 memset(port->sas_addr, 0, SAS_ADDR_SIZE);
169 memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
170 port->class = 0;
171 port->iproto = 0;
172 port->tproto = 0;
173 port->oob_mode = 0;
174 port->phy_mask = 0;
175 }
176 spin_unlock(&port->phy_list_lock);
177 spin_unlock(&sas_ha->phy_port_lock);
178
179 return;
180}
181
182/* ---------- SAS port events ---------- */
183
184void sas_porte_bytes_dmaed(void *data)
185{
186 struct asd_sas_phy *phy = data;
187
188 sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
189 &phy->port_events_pending);
190
191 sas_form_port(phy);
192}
193
194void sas_porte_broadcast_rcvd(void *data)
195{
196 unsigned long flags;
197 u32 prim;
198 struct asd_sas_phy *phy = data;
199
200 sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
201 &phy->port_events_pending);
202
203 spin_lock_irqsave(&phy->sas_prim_lock, flags);
204 prim = phy->sas_prim;
205 spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
206
207 SAS_DPRINTK("broadcast received: %d\n", prim);
208 sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
209}
210
211void sas_porte_link_reset_err(void *data)
212{
213 struct asd_sas_phy *phy = data;
214
215 sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
216 &phy->port_events_pending);
217
218 sas_deform_port(phy);
219}
220
221void sas_porte_timer_event(void *data)
222{
223 struct asd_sas_phy *phy = data;
224
225 sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
226 &phy->port_events_pending);
227
228 sas_deform_port(phy);
229}
230
231void sas_porte_hard_reset(void *data)
232{
233 struct asd_sas_phy *phy = data;
234
235 sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
236 &phy->port_events_pending);
237
238 sas_deform_port(phy);
239}
240
241/* ---------- SAS port registration ---------- */
242
243static void sas_init_port(struct asd_sas_port *port,
244 struct sas_ha_struct *sas_ha, int i)
245{
246 port->id = i;
247 INIT_LIST_HEAD(&port->dev_list);
248 spin_lock_init(&port->phy_list_lock);
249 INIT_LIST_HEAD(&port->phy_list);
250 port->num_phys = 0;
251 port->phy_mask = 0;
252 port->ha = sas_ha;
253
254 spin_lock_init(&port->dev_list_lock);
255}
256
257int sas_register_ports(struct sas_ha_struct *sas_ha)
258{
259 int i;
260
261 /* initialize the ports and discovery */
262 for (i = 0; i < sas_ha->num_phys; i++) {
263 struct asd_sas_port *port = sas_ha->sas_port[i];
264
265 sas_init_port(port, sas_ha, i);
266 sas_init_disc(&port->disc, port);
267 }
268 return 0;
269}
270
271void sas_unregister_ports(struct sas_ha_struct *sas_ha)
272{
273 int i;
274
275 for (i = 0; i < sas_ha->num_phys; i++)
276 if (sas_ha->sas_phy[i]->port)
277 sas_deform_port(sas_ha->sas_phy[i]);
278
279}
diff --git a/drivers/scsi/libsas/sas_scsi_host.c b/drivers/scsi/libsas/sas_scsi_host.c
new file mode 100644
index 000000000000..43e0e4e36934
--- /dev/null
+++ b/drivers/scsi/libsas/sas_scsi_host.c
@@ -0,0 +1,786 @@
1/*
2 * Serial Attached SCSI (SAS) class SCSI Host glue.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 */
25
26#include "sas_internal.h"
27
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi.h>
32#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34#include "../scsi_sas_internal.h"
35
36#include <linux/err.h>
37#include <linux/blkdev.h>
38#include <linux/scatterlist.h>
39
40/* ---------- SCSI Host glue ---------- */
41
42#define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
43#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
44
45static void sas_scsi_task_done(struct sas_task *task)
46{
47 struct task_status_struct *ts = &task->task_status;
48 struct scsi_cmnd *sc = task->uldd_task;
49 unsigned ts_flags = task->task_state_flags;
50 int hs = 0, stat = 0;
51
52 if (unlikely(!sc)) {
53 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
54 list_del_init(&task->list);
55 sas_free_task(task);
56 return;
57 }
58
59 if (ts->resp == SAS_TASK_UNDELIVERED) {
60 /* transport error */
61 hs = DID_NO_CONNECT;
62 } else { /* ts->resp == SAS_TASK_COMPLETE */
63 /* task delivered, what happened afterwards? */
64 switch (ts->stat) {
65 case SAS_DEV_NO_RESPONSE:
66 case SAS_INTERRUPTED:
67 case SAS_PHY_DOWN:
68 case SAS_NAK_R_ERR:
69 case SAS_OPEN_TO:
70 hs = DID_NO_CONNECT;
71 break;
72 case SAS_DATA_UNDERRUN:
73 sc->resid = ts->residual;
74 if (sc->request_bufflen - sc->resid < sc->underflow)
75 hs = DID_ERROR;
76 break;
77 case SAS_DATA_OVERRUN:
78 hs = DID_ERROR;
79 break;
80 case SAS_QUEUE_FULL:
81 hs = DID_SOFT_ERROR; /* retry */
82 break;
83 case SAS_DEVICE_UNKNOWN:
84 hs = DID_BAD_TARGET;
85 break;
86 case SAS_SG_ERR:
87 hs = DID_PARITY;
88 break;
89 case SAS_OPEN_REJECT:
90 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
91 hs = DID_SOFT_ERROR; /* retry */
92 else
93 hs = DID_ERROR;
94 break;
95 case SAS_PROTO_RESPONSE:
96 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
97 "task; please report this\n",
98 task->dev->port->ha->sas_ha_name);
99 break;
100 case SAS_ABORTED_TASK:
101 hs = DID_ABORT;
102 break;
103 case SAM_CHECK_COND:
104 memcpy(sc->sense_buffer, ts->buf,
105 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
106 stat = SAM_CHECK_COND;
107 break;
108 default:
109 stat = ts->stat;
110 break;
111 }
112 }
113 ASSIGN_SAS_TASK(sc, NULL);
114 sc->result = (hs << 16) | stat;
115 list_del_init(&task->list);
116 sas_free_task(task);
117 /* This is very ugly but this is how SCSI Core works. */
118 if (ts_flags & SAS_TASK_STATE_ABORTED)
119 scsi_finish_command(sc);
120 else
121 sc->scsi_done(sc);
122}
123
124static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
125{
126 enum task_attribute ta = TASK_ATTR_SIMPLE;
127 if (cmd->request && blk_rq_tagged(cmd->request)) {
128 if (cmd->device->ordered_tags &&
129 (cmd->request->flags & REQ_HARDBARRIER))
130 ta = TASK_ATTR_HOQ;
131 }
132 return ta;
133}
134
135static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
136 struct domain_device *dev,
137 unsigned long gfp_flags)
138{
139 struct sas_task *task = sas_alloc_task(gfp_flags);
140 struct scsi_lun lun;
141
142 if (!task)
143 return NULL;
144
145 *(u32 *)cmd->sense_buffer = 0;
146 task->uldd_task = cmd;
147 ASSIGN_SAS_TASK(cmd, task);
148
149 task->dev = dev;
150 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
151
152 task->ssp_task.retry_count = 1;
153 int_to_scsilun(cmd->device->lun, &lun);
154 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
155 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
156 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
157
158 task->scatter = cmd->request_buffer;
159 task->num_scatter = cmd->use_sg;
160 task->total_xfer_len = cmd->request_bufflen;
161 task->data_dir = cmd->sc_data_direction;
162
163 task->task_done = sas_scsi_task_done;
164
165 return task;
166}
167
168static int sas_queue_up(struct sas_task *task)
169{
170 struct sas_ha_struct *sas_ha = task->dev->port->ha;
171 struct scsi_core *core = &sas_ha->core;
172 unsigned long flags;
173 LIST_HEAD(list);
174
175 spin_lock_irqsave(&core->task_queue_lock, flags);
176 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
177 spin_unlock_irqrestore(&core->task_queue_lock, flags);
178 return -SAS_QUEUE_FULL;
179 }
180 list_add_tail(&task->list, &core->task_queue);
181 core->task_queue_size += 1;
182 spin_unlock_irqrestore(&core->task_queue_lock, flags);
183 up(&core->queue_thread_sema);
184
185 return 0;
186}
187
188/**
189 * sas_queuecommand -- Enqueue a command for processing
190 * @parameters: See SCSI Core documentation
191 *
192 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
193 * call us without holding an IRQ spinlock...
194 */
195int sas_queuecommand(struct scsi_cmnd *cmd,
196 void (*scsi_done)(struct scsi_cmnd *))
197{
198 int res = 0;
199 struct domain_device *dev = cmd_to_domain_dev(cmd);
200 struct Scsi_Host *host = cmd->device->host;
201 struct sas_internal *i = to_sas_internal(host->transportt);
202
203 spin_unlock_irq(host->host_lock);
204
205 {
206 struct sas_ha_struct *sas_ha = dev->port->ha;
207 struct sas_task *task;
208
209 res = -ENOMEM;
210 task = sas_create_task(cmd, dev, GFP_ATOMIC);
211 if (!task)
212 goto out;
213
214 cmd->scsi_done = scsi_done;
215 /* Queue up, Direct Mode or Task Collector Mode. */
216 if (sas_ha->lldd_max_execute_num < 2)
217 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
218 else
219 res = sas_queue_up(task);
220
221 /* Examine */
222 if (res) {
223 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
224 ASSIGN_SAS_TASK(cmd, NULL);
225 sas_free_task(task);
226 if (res == -SAS_QUEUE_FULL) {
227 cmd->result = DID_SOFT_ERROR << 16; /* retry */
228 res = 0;
229 scsi_done(cmd);
230 }
231 goto out;
232 }
233 }
234out:
235 spin_lock_irq(host->host_lock);
236 return res;
237}
238
239static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
240{
241 struct scsi_cmnd *cmd, *n;
242
243 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
244 if (cmd == my_cmd)
245 list_del_init(&cmd->eh_entry);
246 }
247}
248
249static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
250 struct domain_device *dev)
251{
252 struct scsi_cmnd *cmd, *n;
253
254 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
255 struct domain_device *x = cmd_to_domain_dev(cmd);
256
257 if (x == dev)
258 list_del_init(&cmd->eh_entry);
259 }
260}
261
262static void sas_scsi_clear_queue_port(struct list_head *error_q,
263 struct asd_sas_port *port)
264{
265 struct scsi_cmnd *cmd, *n;
266
267 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
268 struct domain_device *dev = cmd_to_domain_dev(cmd);
269 struct asd_sas_port *x = dev->port;
270
271 if (x == port)
272 list_del_init(&cmd->eh_entry);
273 }
274}
275
276enum task_disposition {
277 TASK_IS_DONE,
278 TASK_IS_ABORTED,
279 TASK_IS_AT_LU,
280 TASK_IS_NOT_AT_LU,
281};
282
283static enum task_disposition sas_scsi_find_task(struct sas_task *task)
284{
285 struct sas_ha_struct *ha = task->dev->port->ha;
286 unsigned long flags;
287 int i, res;
288 struct sas_internal *si =
289 to_sas_internal(task->dev->port->ha->core.shost->transportt);
290
291 if (ha->lldd_max_execute_num > 1) {
292 struct scsi_core *core = &ha->core;
293 struct sas_task *t, *n;
294
295 spin_lock_irqsave(&core->task_queue_lock, flags);
296 list_for_each_entry_safe(t, n, &core->task_queue, list) {
297 if (task == t) {
298 list_del_init(&t->list);
299 spin_unlock_irqrestore(&core->task_queue_lock,
300 flags);
301 SAS_DPRINTK("%s: task 0x%p aborted from "
302 "task_queue\n",
303 __FUNCTION__, task);
304 return TASK_IS_ABORTED;
305 }
306 }
307 spin_unlock_irqrestore(&core->task_queue_lock, flags);
308 }
309
310 for (i = 0; i < 5; i++) {
311 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
312 res = si->dft->lldd_abort_task(task);
313
314 spin_lock_irqsave(&task->task_state_lock, flags);
315 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
316 spin_unlock_irqrestore(&task->task_state_lock, flags);
317 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
318 task);
319 return TASK_IS_DONE;
320 }
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322
323 if (res == TMF_RESP_FUNC_COMPLETE) {
324 SAS_DPRINTK("%s: task 0x%p is aborted\n",
325 __FUNCTION__, task);
326 return TASK_IS_ABORTED;
327 } else if (si->dft->lldd_query_task) {
328 SAS_DPRINTK("%s: querying task 0x%p\n",
329 __FUNCTION__, task);
330 res = si->dft->lldd_query_task(task);
331 if (res == TMF_RESP_FUNC_SUCC) {
332 SAS_DPRINTK("%s: task 0x%p at LU\n",
333 __FUNCTION__, task);
334 return TASK_IS_AT_LU;
335 } else if (res == TMF_RESP_FUNC_COMPLETE) {
336 SAS_DPRINTK("%s: task 0x%p not at LU\n",
337 __FUNCTION__, task);
338 return TASK_IS_NOT_AT_LU;
339 }
340 }
341 }
342 return res;
343}
344
345static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
346{
347 int res = TMF_RESP_FUNC_FAILED;
348 struct scsi_lun lun;
349 struct sas_internal *i =
350 to_sas_internal(dev->port->ha->core.shost->transportt);
351
352 int_to_scsilun(cmd->device->lun, &lun);
353
354 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
355 SAS_ADDR(dev->sas_addr),
356 cmd->device->lun);
357
358 if (i->dft->lldd_abort_task_set)
359 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
360
361 if (res == TMF_RESP_FUNC_FAILED) {
362 if (i->dft->lldd_clear_task_set)
363 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
364 }
365
366 if (res == TMF_RESP_FUNC_FAILED) {
367 if (i->dft->lldd_lu_reset)
368 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
369 }
370
371 return res;
372}
373
374static int sas_recover_I_T(struct domain_device *dev)
375{
376 int res = TMF_RESP_FUNC_FAILED;
377 struct sas_internal *i =
378 to_sas_internal(dev->port->ha->core.shost->transportt);
379
380 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
381 SAS_ADDR(dev->sas_addr));
382
383 if (i->dft->lldd_I_T_nexus_reset)
384 res = i->dft->lldd_I_T_nexus_reset(dev);
385
386 return res;
387}
388
389void sas_scsi_recover_host(struct Scsi_Host *shost)
390{
391 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
392 unsigned long flags;
393 LIST_HEAD(error_q);
394 struct scsi_cmnd *cmd, *n;
395 enum task_disposition res = TASK_IS_DONE;
396 int tmf_resp;
397 struct sas_internal *i = to_sas_internal(shost->transportt);
398
399 spin_lock_irqsave(shost->host_lock, flags);
400 list_splice_init(&shost->eh_cmd_q, &error_q);
401 spin_unlock_irqrestore(shost->host_lock, flags);
402
403 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
404
405 /* All tasks on this list were marked SAS_TASK_STATE_ABORTED
406 * by sas_scsi_timed_out() callback.
407 */
408Again:
409 SAS_DPRINTK("going over list...\n");
410 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
411 struct sas_task *task = TO_SAS_TASK(cmd);
412
413 SAS_DPRINTK("trying to find task 0x%p\n", task);
414 list_del_init(&cmd->eh_entry);
415 res = sas_scsi_find_task(task);
416
417 cmd->eh_eflags = 0;
418 shost->host_failed--;
419
420 switch (res) {
421 case TASK_IS_DONE:
422 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
423 task);
424 task->task_done(task);
425 continue;
426 case TASK_IS_ABORTED:
427 SAS_DPRINTK("%s: task 0x%p is aborted\n",
428 __FUNCTION__, task);
429 task->task_done(task);
430 continue;
431 case TASK_IS_AT_LU:
432 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
433 tmf_resp = sas_recover_lu(task->dev, cmd);
434 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
435 SAS_DPRINTK("dev %016llx LU %x is "
436 "recovered\n",
437 SAS_ADDR(task->dev),
438 cmd->device->lun);
439 task->task_done(task);
440 sas_scsi_clear_queue_lu(&error_q, cmd);
441 goto Again;
442 }
443 /* fallthrough */
444 case TASK_IS_NOT_AT_LU:
445 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
446 task);
447 tmf_resp = sas_recover_I_T(task->dev);
448 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
449 SAS_DPRINTK("I_T %016llx recovered\n",
450 SAS_ADDR(task->dev->sas_addr));
451 task->task_done(task);
452 sas_scsi_clear_queue_I_T(&error_q, task->dev);
453 goto Again;
454 }
455 /* Hammer time :-) */
456 if (i->dft->lldd_clear_nexus_port) {
457 struct asd_sas_port *port = task->dev->port;
458 SAS_DPRINTK("clearing nexus for port:%d\n",
459 port->id);
460 res = i->dft->lldd_clear_nexus_port(port);
461 if (res == TMF_RESP_FUNC_COMPLETE) {
462 SAS_DPRINTK("clear nexus port:%d "
463 "succeeded\n", port->id);
464 task->task_done(task);
465 sas_scsi_clear_queue_port(&error_q,
466 port);
467 goto Again;
468 }
469 }
470 if (i->dft->lldd_clear_nexus_ha) {
471 SAS_DPRINTK("clear nexus ha\n");
472 res = i->dft->lldd_clear_nexus_ha(ha);
473 if (res == TMF_RESP_FUNC_COMPLETE) {
474 SAS_DPRINTK("clear nexus ha "
475 "succeeded\n");
476 task->task_done(task);
477 goto out;
478 }
479 }
480 /* If we are here -- this means that no amount
481 * of effort could recover from errors. Quite
482 * possibly the HA just disappeared.
483 */
484 SAS_DPRINTK("error from device %llx, LUN %x "
485 "couldn't be recovered in any way\n",
486 SAS_ADDR(task->dev->sas_addr),
487 cmd->device->lun);
488
489 task->task_done(task);
490 goto clear_q;
491 }
492 }
493out:
494 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
495 return;
496clear_q:
497 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
498 list_for_each_entry_safe(cmd, n, &error_q, eh_entry) {
499 struct sas_task *task = TO_SAS_TASK(cmd);
500 list_del_init(&cmd->eh_entry);
501 task->task_done(task);
502 }
503}
504
505enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
506{
507 struct sas_task *task = TO_SAS_TASK(cmd);
508 unsigned long flags;
509
510 if (!task) {
511 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
512 cmd, task);
513 return EH_HANDLED;
514 }
515
516 spin_lock_irqsave(&task->task_state_lock, flags);
517 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
518 spin_unlock_irqrestore(&task->task_state_lock, flags);
519 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
520 cmd, task);
521 return EH_HANDLED;
522 }
523 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
524 spin_unlock_irqrestore(&task->task_state_lock, flags);
525
526 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
527 cmd, task);
528
529 return EH_NOT_HANDLED;
530}
531
532struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
533{
534 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
535 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
536 struct domain_device *found_dev = NULL;
537 int i;
538
539 spin_lock(&ha->phy_port_lock);
540 for (i = 0; i < ha->num_phys; i++) {
541 struct asd_sas_port *port = ha->sas_port[i];
542 struct domain_device *dev;
543
544 spin_lock(&port->dev_list_lock);
545 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
546 if (rphy == dev->rphy) {
547 found_dev = dev;
548 spin_unlock(&port->dev_list_lock);
549 goto found;
550 }
551 }
552 spin_unlock(&port->dev_list_lock);
553 }
554 found:
555 spin_unlock(&ha->phy_port_lock);
556
557 return found_dev;
558}
559
560static inline struct domain_device *sas_find_target(struct scsi_target *starget)
561{
562 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
563
564 return sas_find_dev_by_rphy(rphy);
565}
566
567int sas_target_alloc(struct scsi_target *starget)
568{
569 struct domain_device *found_dev = sas_find_target(starget);
570
571 if (!found_dev)
572 return -ENODEV;
573
574 starget->hostdata = found_dev;
575 return 0;
576}
577
578#define SAS_DEF_QD 32
579#define SAS_MAX_QD 64
580
581int sas_slave_configure(struct scsi_device *scsi_dev)
582{
583 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
584 struct sas_ha_struct *sas_ha;
585
586 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
587
588 sas_ha = dev->port->ha;
589
590 sas_read_port_mode_page(scsi_dev);
591
592 if (scsi_dev->tagged_supported) {
593 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
594 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
595 } else {
596 SAS_DPRINTK("device %llx, LUN %x doesn't support "
597 "TCQ\n", SAS_ADDR(dev->sas_addr),
598 scsi_dev->lun);
599 scsi_dev->tagged_supported = 0;
600 scsi_set_tag_type(scsi_dev, 0);
601 scsi_deactivate_tcq(scsi_dev, 1);
602 }
603
604 return 0;
605}
606
607void sas_slave_destroy(struct scsi_device *scsi_dev)
608{
609}
610
611int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
612{
613 int res = min(new_depth, SAS_MAX_QD);
614
615 if (scsi_dev->tagged_supported)
616 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
617 res);
618 else {
619 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
620 sas_printk("device %llx LUN %x queue depth changed to 1\n",
621 SAS_ADDR(dev->sas_addr),
622 scsi_dev->lun);
623 scsi_adjust_queue_depth(scsi_dev, 0, 1);
624 res = 1;
625 }
626
627 return res;
628}
629
630int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
631{
632 if (!scsi_dev->tagged_supported)
633 return 0;
634
635 scsi_deactivate_tcq(scsi_dev, 1);
636
637 scsi_set_tag_type(scsi_dev, qt);
638 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
639
640 return qt;
641}
642
643int sas_bios_param(struct scsi_device *scsi_dev,
644 struct block_device *bdev,
645 sector_t capacity, int *hsc)
646{
647 hsc[0] = 255;
648 hsc[1] = 63;
649 sector_div(capacity, 255*63);
650 hsc[2] = capacity;
651
652 return 0;
653}
654
655/* ---------- Task Collector Thread implementation ---------- */
656
657static void sas_queue(struct sas_ha_struct *sas_ha)
658{
659 struct scsi_core *core = &sas_ha->core;
660 unsigned long flags;
661 LIST_HEAD(q);
662 int can_queue;
663 int res;
664 struct sas_internal *i = to_sas_internal(core->shost->transportt);
665
666 spin_lock_irqsave(&core->task_queue_lock, flags);
667 while (!core->queue_thread_kill &&
668 !list_empty(&core->task_queue)) {
669
670 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
671 if (can_queue >= 0) {
672 can_queue = core->task_queue_size;
673 list_splice_init(&core->task_queue, &q);
674 } else {
675 struct list_head *a, *n;
676
677 can_queue = sas_ha->lldd_queue_size;
678 list_for_each_safe(a, n, &core->task_queue) {
679 list_move_tail(a, &q);
680 if (--can_queue == 0)
681 break;
682 }
683 can_queue = sas_ha->lldd_queue_size;
684 }
685 core->task_queue_size -= can_queue;
686 spin_unlock_irqrestore(&core->task_queue_lock, flags);
687 {
688 struct sas_task *task = list_entry(q.next,
689 struct sas_task,
690 list);
691 list_del_init(&q);
692 res = i->dft->lldd_execute_task(task, can_queue,
693 GFP_KERNEL);
694 if (unlikely(res))
695 __list_add(&q, task->list.prev, &task->list);
696 }
697 spin_lock_irqsave(&core->task_queue_lock, flags);
698 if (res) {
699 list_splice_init(&q, &core->task_queue); /*at head*/
700 core->task_queue_size += can_queue;
701 }
702 }
703 spin_unlock_irqrestore(&core->task_queue_lock, flags);
704}
705
706static DECLARE_COMPLETION(queue_th_comp);
707
708/**
709 * sas_queue_thread -- The Task Collector thread
710 * @_sas_ha: pointer to struct sas_ha
711 */
712static int sas_queue_thread(void *_sas_ha)
713{
714 struct sas_ha_struct *sas_ha = _sas_ha;
715 struct scsi_core *core = &sas_ha->core;
716
717 daemonize("sas_queue_%d", core->shost->host_no);
718 current->flags |= PF_NOFREEZE;
719
720 complete(&queue_th_comp);
721
722 while (1) {
723 down_interruptible(&core->queue_thread_sema);
724 sas_queue(sas_ha);
725 if (core->queue_thread_kill)
726 break;
727 }
728
729 complete(&queue_th_comp);
730
731 return 0;
732}
733
734int sas_init_queue(struct sas_ha_struct *sas_ha)
735{
736 int res;
737 struct scsi_core *core = &sas_ha->core;
738
739 spin_lock_init(&core->task_queue_lock);
740 core->task_queue_size = 0;
741 INIT_LIST_HEAD(&core->task_queue);
742 init_MUTEX_LOCKED(&core->queue_thread_sema);
743
744 res = kernel_thread(sas_queue_thread, sas_ha, 0);
745 if (res >= 0)
746 wait_for_completion(&queue_th_comp);
747
748 return res < 0 ? res : 0;
749}
750
751void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
752{
753 unsigned long flags;
754 struct scsi_core *core = &sas_ha->core;
755 struct sas_task *task, *n;
756
757 init_completion(&queue_th_comp);
758 core->queue_thread_kill = 1;
759 up(&core->queue_thread_sema);
760 wait_for_completion(&queue_th_comp);
761
762 if (!list_empty(&core->task_queue))
763 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
764 SAS_ADDR(sas_ha->sas_addr));
765
766 spin_lock_irqsave(&core->task_queue_lock, flags);
767 list_for_each_entry_safe(task, n, &core->task_queue, list) {
768 struct scsi_cmnd *cmd = task->uldd_task;
769
770 list_del_init(&task->list);
771
772 ASSIGN_SAS_TASK(cmd, NULL);
773 sas_free_task(task);
774 cmd->result = DID_ABORT << 16;
775 cmd->scsi_done(cmd);
776 }
777 spin_unlock_irqrestore(&core->task_queue_lock, flags);
778}
779
780EXPORT_SYMBOL_GPL(sas_queuecommand);
781EXPORT_SYMBOL_GPL(sas_target_alloc);
782EXPORT_SYMBOL_GPL(sas_slave_configure);
783EXPORT_SYMBOL_GPL(sas_slave_destroy);
784EXPORT_SYMBOL_GPL(sas_change_queue_depth);
785EXPORT_SYMBOL_GPL(sas_change_queue_type);
786EXPORT_SYMBOL_GPL(sas_bios_param);