blob: 68a80a00f73ff05ebe53769aef631dfb88bf141e [file] [log] [blame]
James Bottomley2908d772006-08-29 09:22:51 -05001/*
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
James Bottomley2908d772006-08-29 09:22:51 -050025#include <linux/scatterlist.h>
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +090026#include <linux/blkdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
James Bottomley2908d772006-08-29 09:22:51 -050028
29#include "sas_internal.h"
30
Dan Williamsb52df412011-11-30 23:23:33 -080031#include <scsi/sas_ata.h>
James Bottomley2908d772006-08-29 09:22:51 -050032#include <scsi/scsi_transport.h>
33#include <scsi/scsi_transport_sas.h>
34#include "../scsi_sas_internal.h"
35
36static int sas_discover_expander(struct domain_device *dev);
37static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38static int sas_configure_phy(struct domain_device *dev, int phy_id,
39 u8 *sas_addr, int include);
40static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
41
James Bottomley2908d772006-08-29 09:22:51 -050042/* ---------- SMP task management ---------- */
43
44static void smp_task_timedout(unsigned long _task)
45{
46 struct sas_task *task = (void *) _task;
47 unsigned long flags;
48
49 spin_lock_irqsave(&task->task_state_lock, flags);
50 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
51 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
52 spin_unlock_irqrestore(&task->task_state_lock, flags);
53
54 complete(&task->completion);
55}
56
57static void smp_task_done(struct sas_task *task)
58{
59 if (!del_timer(&task->timer))
60 return;
61 complete(&task->completion);
62}
63
64/* Give it some long enough timeout. In seconds. */
65#define SMP_TIMEOUT 10
66
67static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68 void *resp, int resp_size)
69{
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070070 int res, retry;
71 struct sas_task *task = NULL;
James Bottomley2908d772006-08-29 09:22:51 -050072 struct sas_internal *i =
73 to_sas_internal(dev->port->ha->core.shost->transportt);
74
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000075 mutex_lock(&dev->ex_dev.cmd_mutex);
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070076 for (retry = 0; retry < 3; retry++) {
Dan Williams3a9c5562011-12-21 15:19:56 -080077 if (test_bit(SAS_DEV_GONE, &dev->state)) {
78 res = -ECOMM;
79 break;
80 }
81
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070082 task = sas_alloc_task(GFP_KERNEL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +000083 if (!task) {
84 res = -ENOMEM;
85 break;
86 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070087 task->dev = dev;
88 task->task_proto = dev->tproto;
89 sg_init_one(&task->smp_task.smp_req, req, req_size);
90 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
James Bottomley2908d772006-08-29 09:22:51 -050091
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070092 task->task_done = smp_task_done;
James Bottomley2908d772006-08-29 09:22:51 -050093
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070094 task->timer.data = (unsigned long) task;
95 task->timer.function = smp_task_timedout;
96 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
97 add_timer(&task->timer);
James Bottomley2908d772006-08-29 09:22:51 -050098
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070099 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
James Bottomley2908d772006-08-29 09:22:51 -0500100
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700101 if (res) {
102 del_timer(&task->timer);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000104 break;
James Bottomley2908d772006-08-29 09:22:51 -0500105 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700106
107 wait_for_completion(&task->completion);
James Bottomley32e8ae32007-12-30 12:37:31 -0600108 res = -ECOMM;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700109 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
110 SAS_DPRINTK("smp task timed out or aborted\n");
111 i->dft->lldd_abort_task(task);
112 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
113 SAS_DPRINTK("SMP task aborted and not done\n");
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000114 break;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700115 }
116 }
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500118 task->task_status.stat == SAM_STAT_GOOD) {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700119 res = 0;
120 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000121 }
122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123 task->task_status.stat == SAS_DATA_UNDERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600124 /* no error, but return the number of bytes of
125 * underrun */
126 res = task->task_status.residual;
127 break;
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000128 }
129 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130 task->task_status.stat == SAS_DATA_OVERRUN) {
James Bottomley2d4b63e2007-12-29 11:49:53 -0600131 res = -EMSGSIZE;
132 break;
Dan Williams36a39942011-11-17 17:59:54 -0800133 }
134 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135 task->task_status.stat == SAS_DEVICE_UNKNOWN)
136 break;
137 else {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700138 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700139 "status 0x%x\n", __func__,
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700140 SAS_ADDR(dev->sas_addr),
141 task->task_status.resp,
142 task->task_status.stat);
143 sas_free_task(task);
144 task = NULL;
145 }
James Bottomley2908d772006-08-29 09:22:51 -0500146 }
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000147 mutex_unlock(&dev->ex_dev.cmd_mutex);
148
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700149 BUG_ON(retry == 3 && task != NULL);
Jeff Skirvin89d3cf62011-11-16 09:44:13 +0000150 sas_free_task(task);
James Bottomley2908d772006-08-29 09:22:51 -0500151 return res;
152}
153
154/* ---------- Allocations ---------- */
155
156static inline void *alloc_smp_req(int size)
157{
158 u8 *p = kzalloc(size, GFP_KERNEL);
159 if (p)
160 p[0] = SMP_REQUEST;
161 return p;
162}
163
164static inline void *alloc_smp_resp(int size)
165{
166 return kzalloc(size, GFP_KERNEL);
167}
168
169/* ---------- Expander configuration ---------- */
170
171static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
172 void *disc_resp)
173{
174 struct expander_device *ex = &dev->ex_dev;
175 struct ex_phy *phy = &ex->ex_phy[phy_id];
176 struct smp_resp *resp = disc_resp;
177 struct discover_resp *dr = &resp->disc;
178 struct sas_rphy *rphy = dev->rphy;
179 int rediscover = (phy->phy != NULL);
180
181 if (!rediscover) {
182 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
183
184 /* FIXME: error_handling */
185 BUG_ON(!phy->phy);
186 }
187
188 switch (resp->result) {
189 case SMP_RESP_PHY_VACANT:
190 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800191 break;
James Bottomley2908d772006-08-29 09:22:51 -0500192 default:
193 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800194 break;
James Bottomley2908d772006-08-29 09:22:51 -0500195 case SMP_RESP_FUNC_ACC:
196 phy->phy_state = PHY_EMPTY; /* do not know yet */
197 break;
198 }
199
200 phy->phy_id = phy_id;
201 phy->attached_dev_type = dr->attached_dev_type;
202 phy->linkrate = dr->linkrate;
203 phy->attached_sata_host = dr->attached_sata_host;
204 phy->attached_sata_dev = dr->attached_sata_dev;
205 phy->attached_sata_ps = dr->attached_sata_ps;
206 phy->attached_iproto = dr->iproto << 1;
207 phy->attached_tproto = dr->tproto << 1;
208 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
209 phy->attached_phy_id = dr->attached_phy_id;
210 phy->phy_change_count = dr->change_count;
211 phy->routing_attr = dr->routing_attr;
212 phy->virtual = dr->virtual;
213 phy->last_da_index = -1;
214
Jack Wangbb041a02011-09-23 14:32:32 +0800215 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
216 phy->phy->identify.device_type = phy->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500217 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
218 phy->phy->identify.target_port_protocols = phy->attached_tproto;
219 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500220 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
221 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
222 phy->phy->minimum_linkrate = dr->pmin_linkrate;
223 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500224 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500225
226 if (!rediscover)
Jack Wang2bc72c92010-10-06 16:05:35 +0800227 if (sas_phy_add(phy->phy)) {
228 sas_phy_free(phy->phy);
229 return;
230 }
James Bottomley2908d772006-08-29 09:22:51 -0500231
232 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
233 SAS_ADDR(dev->sas_addr), phy->phy_id,
234 phy->routing_attr == TABLE_ROUTING ? 'T' :
235 phy->routing_attr == DIRECT_ROUTING ? 'D' :
236 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
237 SAS_ADDR(phy->attached_sas_addr));
238
239 return;
240}
241
Dan Williamsb52df412011-11-30 23:23:33 -0800242/* check if we have an existing attached ata device on this expander phy */
Dan Williams81c757b2011-12-02 16:07:01 -0800243struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
Dan Williamsb52df412011-11-30 23:23:33 -0800244{
245 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
246 struct domain_device *dev;
247 struct sas_rphy *rphy;
248
249 if (!ex_phy->port)
250 return NULL;
251
252 rphy = ex_phy->port->rphy;
253 if (!rphy)
254 return NULL;
255
256 dev = sas_find_dev_by_rphy(rphy);
257
258 if (dev && dev_is_sata(dev))
259 return dev;
260
261 return NULL;
262}
263
James Bottomley2908d772006-08-29 09:22:51 -0500264#define DISCOVER_REQ_SIZE 16
265#define DISCOVER_RESP_SIZE 56
266
James Bottomley1acce192006-08-22 12:39:19 -0500267static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
268 u8 *disc_resp, int single)
269{
Dan Williamsb52df412011-11-30 23:23:33 -0800270 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
James Bottomley1acce192006-08-22 12:39:19 -0500271 int i, res;
272
273 disc_req[9] = single;
274 for (i = 1 ; i < 3; i++) {
275 struct discover_resp *dr;
276
277 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
278 disc_resp, DISCOVER_RESP_SIZE);
279 if (res)
280 return res;
James Bottomley1acce192006-08-22 12:39:19 -0500281 dr = &((struct smp_resp *)disc_resp)->disc;
jack_wang183ce892011-02-19 18:20:53 +0800282 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
283 SAS_ADDR_SIZE) == 0) {
284 sas_printk("Found loopback topology, just ignore it!\n");
285 return 0;
286 }
Dan Williamsb52df412011-11-30 23:23:33 -0800287
288 /* This is detecting a failure to transmit initial
289 * dev to host FIS as described in section J.5 of
290 * sas-2 r16
291 */
James Bottomley1acce192006-08-22 12:39:19 -0500292 if (!(dr->attached_dev_type == 0 &&
293 dr->attached_sata_dev))
294 break;
Dan Williamsb52df412011-11-30 23:23:33 -0800295
296 /* In order to generate the dev to host FIS, we send a
297 * link reset to the expander port. If a device was
298 * previously detected on this port we ask libata to
299 * manage the reset and link recovery.
300 */
301 if (ata_dev) {
302 sas_ata_schedule_reset(ata_dev);
303 break;
304 }
James Bottomley38e2f032006-09-07 15:52:09 -0500305 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
James Bottomley1acce192006-08-22 12:39:19 -0500306 /* Wait for the reset to trigger the negotiation */
307 msleep(500);
308 }
309 sas_set_ex_phy(dev, single, disc_resp);
310 return 0;
311}
312
James Bottomley2908d772006-08-29 09:22:51 -0500313static int sas_ex_phy_discover(struct domain_device *dev, int single)
314{
315 struct expander_device *ex = &dev->ex_dev;
316 int res = 0;
317 u8 *disc_req;
318 u8 *disc_resp;
319
320 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
321 if (!disc_req)
322 return -ENOMEM;
323
324 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
325 if (!disc_resp) {
326 kfree(disc_req);
327 return -ENOMEM;
328 }
329
330 disc_req[1] = SMP_DISCOVER;
331
332 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500333 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500334 } else {
335 int i;
336
337 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500338 res = sas_ex_phy_discover_helper(dev, disc_req,
339 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500340 if (res)
341 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500342 }
343 }
344out_err:
345 kfree(disc_resp);
346 kfree(disc_req);
347 return res;
348}
349
350static int sas_expander_discover(struct domain_device *dev)
351{
352 struct expander_device *ex = &dev->ex_dev;
353 int res = -ENOMEM;
354
355 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
356 if (!ex->ex_phy)
357 return -ENOMEM;
358
359 res = sas_ex_phy_discover(dev, -1);
360 if (res)
361 goto out_err;
362
363 return 0;
364 out_err:
365 kfree(ex->ex_phy);
366 ex->ex_phy = NULL;
367 return res;
368}
369
370#define MAX_EXPANDER_PHYS 128
371
372static void ex_assign_report_general(struct domain_device *dev,
373 struct smp_resp *resp)
374{
375 struct report_general_resp *rg = &resp->rg;
376
377 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
378 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
379 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
Luben Tuikovffaac8f2011-09-22 09:41:36 -0700380 dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley2908d772006-08-29 09:22:51 -0500381 dev->ex_dev.conf_route_table = rg->conf_route_table;
382 dev->ex_dev.configuring = rg->configuring;
383 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
384}
385
386#define RG_REQ_SIZE 8
387#define RG_RESP_SIZE 32
388
389static int sas_ex_general(struct domain_device *dev)
390{
391 u8 *rg_req;
392 struct smp_resp *rg_resp;
393 int res;
394 int i;
395
396 rg_req = alloc_smp_req(RG_REQ_SIZE);
397 if (!rg_req)
398 return -ENOMEM;
399
400 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
401 if (!rg_resp) {
402 kfree(rg_req);
403 return -ENOMEM;
404 }
405
406 rg_req[1] = SMP_REPORT_GENERAL;
407
408 for (i = 0; i < 5; i++) {
409 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
410 RG_RESP_SIZE);
411
412 if (res) {
413 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
414 SAS_ADDR(dev->sas_addr), res);
415 goto out;
416 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
417 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
418 SAS_ADDR(dev->sas_addr), rg_resp->result);
419 res = rg_resp->result;
420 goto out;
421 }
422
423 ex_assign_report_general(dev, rg_resp);
424
425 if (dev->ex_dev.configuring) {
426 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
427 SAS_ADDR(dev->sas_addr));
428 schedule_timeout_interruptible(5*HZ);
429 } else
430 break;
431 }
432out:
433 kfree(rg_req);
434 kfree(rg_resp);
435 return res;
436}
437
438static void ex_assign_manuf_info(struct domain_device *dev, void
439 *_mi_resp)
440{
441 u8 *mi_resp = _mi_resp;
442 struct sas_rphy *rphy = dev->rphy;
443 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
444
445 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
446 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
447 memcpy(edev->product_rev, mi_resp + 36,
448 SAS_EXPANDER_PRODUCT_REV_LEN);
449
450 if (mi_resp[8] & 1) {
451 memcpy(edev->component_vendor_id, mi_resp + 40,
452 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
453 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
454 edev->component_revision_id = mi_resp[50];
455 }
456}
457
458#define MI_REQ_SIZE 8
459#define MI_RESP_SIZE 64
460
461static int sas_ex_manuf_info(struct domain_device *dev)
462{
463 u8 *mi_req;
464 u8 *mi_resp;
465 int res;
466
467 mi_req = alloc_smp_req(MI_REQ_SIZE);
468 if (!mi_req)
469 return -ENOMEM;
470
471 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
472 if (!mi_resp) {
473 kfree(mi_req);
474 return -ENOMEM;
475 }
476
477 mi_req[1] = SMP_REPORT_MANUF_INFO;
478
479 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
480 if (res) {
481 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
482 SAS_ADDR(dev->sas_addr), res);
483 goto out;
484 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
485 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
486 SAS_ADDR(dev->sas_addr), mi_resp[2]);
487 goto out;
488 }
489
490 ex_assign_manuf_info(dev, mi_resp);
491out:
492 kfree(mi_req);
493 kfree(mi_resp);
494 return res;
495}
496
497#define PC_REQ_SIZE 44
498#define PC_RESP_SIZE 8
499
500int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500501 enum phy_func phy_func,
502 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500503{
504 u8 *pc_req;
505 u8 *pc_resp;
506 int res;
507
508 pc_req = alloc_smp_req(PC_REQ_SIZE);
509 if (!pc_req)
510 return -ENOMEM;
511
512 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
513 if (!pc_resp) {
514 kfree(pc_req);
515 return -ENOMEM;
516 }
517
518 pc_req[1] = SMP_PHY_CONTROL;
519 pc_req[9] = phy_id;
520 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500521 if (rates) {
522 pc_req[32] = rates->minimum_linkrate << 4;
523 pc_req[33] = rates->maximum_linkrate << 4;
524 }
James Bottomley2908d772006-08-29 09:22:51 -0500525
526 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
527
528 kfree(pc_resp);
529 kfree(pc_req);
530 return res;
531}
532
533static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
534{
535 struct expander_device *ex = &dev->ex_dev;
536 struct ex_phy *phy = &ex->ex_phy[phy_id];
537
James Bottomleya01e70e2006-09-06 19:28:07 -0500538 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500539 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500540}
541
542static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
543{
544 struct expander_device *ex = &dev->ex_dev;
545 int i;
546
547 for (i = 0; i < ex->num_phys; i++) {
548 struct ex_phy *phy = &ex->ex_phy[i];
549
550 if (phy->phy_state == PHY_VACANT ||
551 phy->phy_state == PHY_NOT_PRESENT)
552 continue;
553
554 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
555 sas_ex_disable_phy(dev, i);
556 }
557}
558
559static int sas_dev_present_in_domain(struct asd_sas_port *port,
560 u8 *sas_addr)
561{
562 struct domain_device *dev;
563
564 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
565 return 1;
566 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
567 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
568 return 1;
569 }
570 return 0;
571}
572
573#define RPEL_REQ_SIZE 16
574#define RPEL_RESP_SIZE 32
575int sas_smp_get_phy_events(struct sas_phy *phy)
576{
577 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200578 u8 *req;
579 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500580 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
581 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500582
Jesper Juhl92631fa2007-07-28 01:13:33 +0200583 req = alloc_smp_req(RPEL_REQ_SIZE);
584 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500585 return -ENOMEM;
586
Jesper Juhl92631fa2007-07-28 01:13:33 +0200587 resp = alloc_smp_resp(RPEL_RESP_SIZE);
588 if (!resp) {
589 kfree(req);
590 return -ENOMEM;
591 }
592
James Bottomley2908d772006-08-29 09:22:51 -0500593 req[1] = SMP_REPORT_PHY_ERR_LOG;
594 req[9] = phy->number;
595
596 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
597 resp, RPEL_RESP_SIZE);
598
599 if (!res)
600 goto out;
601
602 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
603 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
604 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
605 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
606
607 out:
608 kfree(resp);
609 return res;
610
611}
612
James Bottomleyb9142172007-07-22 13:15:55 -0500613#ifdef CONFIG_SCSI_SAS_ATA
614
James Bottomley2908d772006-08-29 09:22:51 -0500615#define RPS_REQ_SIZE 16
616#define RPS_RESP_SIZE 60
617
618static int sas_get_report_phy_sata(struct domain_device *dev,
619 int phy_id,
620 struct smp_resp *rps_resp)
621{
622 int res;
623 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500624 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500625
626 if (!rps_req)
627 return -ENOMEM;
628
629 rps_req[1] = SMP_REPORT_PHY_SATA;
630 rps_req[9] = phy_id;
631
632 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
633 rps_resp, RPS_RESP_SIZE);
634
James Bottomley1acce192006-08-22 12:39:19 -0500635 /* 0x34 is the FIS type for the D2H fis. There's a potential
636 * standards cockup here. sas-2 explicitly specifies the FIS
637 * should be encoded so that FIS type is in resp[24].
638 * However, some expanders endian reverse this. Undo the
639 * reversal here */
640 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
641 int i;
642
643 for (i = 0; i < 5; i++) {
644 int j = 24 + (i*4);
645 u8 a, b;
646 a = resp[j + 0];
647 b = resp[j + 1];
648 resp[j + 0] = resp[j + 3];
649 resp[j + 1] = resp[j + 2];
650 resp[j + 2] = b;
651 resp[j + 3] = a;
652 }
653 }
654
James Bottomley2908d772006-08-29 09:22:51 -0500655 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500656 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500657}
James Bottomleyb9142172007-07-22 13:15:55 -0500658#endif
James Bottomley2908d772006-08-29 09:22:51 -0500659
660static void sas_ex_get_linkrate(struct domain_device *parent,
661 struct domain_device *child,
662 struct ex_phy *parent_phy)
663{
664 struct expander_device *parent_ex = &parent->ex_dev;
665 struct sas_port *port;
666 int i;
667
668 child->pathways = 0;
669
670 port = parent_phy->port;
671
672 for (i = 0; i < parent_ex->num_phys; i++) {
673 struct ex_phy *phy = &parent_ex->ex_phy[i];
674
675 if (phy->phy_state == PHY_VACANT ||
676 phy->phy_state == PHY_NOT_PRESENT)
677 continue;
678
679 if (SAS_ADDR(phy->attached_sas_addr) ==
680 SAS_ADDR(child->sas_addr)) {
681
682 child->min_linkrate = min(parent->min_linkrate,
683 phy->linkrate);
684 child->max_linkrate = max(parent->max_linkrate,
685 phy->linkrate);
686 child->pathways++;
687 sas_port_add_phy(port, phy->phy);
688 }
689 }
690 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
691 child->pathways = min(child->pathways, parent->pathways);
692}
693
694static struct domain_device *sas_ex_discover_end_dev(
695 struct domain_device *parent, int phy_id)
696{
697 struct expander_device *parent_ex = &parent->ex_dev;
698 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
699 struct domain_device *child = NULL;
700 struct sas_rphy *rphy;
701 int res;
702
703 if (phy->attached_sata_host || phy->attached_sata_ps)
704 return NULL;
705
Dan Williams735f7d22011-11-17 17:59:47 -0800706 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500707 if (!child)
708 return NULL;
709
Dan Williams735f7d22011-11-17 17:59:47 -0800710 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500711 child->parent = parent;
712 child->port = parent->port;
713 child->iproto = phy->attached_iproto;
714 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
715 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600716 if (!phy->port) {
717 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
718 if (unlikely(!phy->port))
719 goto out_err;
720 if (unlikely(sas_port_add(phy->port) != 0)) {
721 sas_port_free(phy->port);
722 goto out_err;
723 }
724 }
James Bottomley2908d772006-08-29 09:22:51 -0500725 sas_ex_get_linkrate(parent, child, phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800726 sas_device_set_phy(child, phy->port);
James Bottomley2908d772006-08-29 09:22:51 -0500727
James Bottomleyb9142172007-07-22 13:15:55 -0500728#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800729 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
James Bottomley2908d772006-08-29 09:22:51 -0500730 child->dev_type = SATA_DEV;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800731 if (phy->attached_tproto & SAS_PROTOCOL_STP)
James Bottomley2908d772006-08-29 09:22:51 -0500732 child->tproto = phy->attached_tproto;
733 if (phy->attached_sata_dev)
734 child->tproto |= SATA_DEV;
735 res = sas_get_report_phy_sata(parent, phy_id,
736 &child->sata_dev.rps_resp);
737 if (res) {
738 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
739 "0x%x\n", SAS_ADDR(parent->sas_addr),
740 phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600741 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500742 }
743 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
744 sizeof(struct dev_to_host_fis));
James Bottomley1acce192006-08-22 12:39:19 -0500745
746 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500747 if (unlikely(!rphy))
748 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500749
James Bottomley2908d772006-08-29 09:22:51 -0500750 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500751
752 child->rphy = rphy;
753
Dan Williams87c83312011-11-17 17:59:51 -0800754 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500755
James Bottomley2908d772006-08-29 09:22:51 -0500756 res = sas_discover_sata(child);
757 if (res) {
758 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
759 "%016llx:0x%x returned 0x%x\n",
760 SAS_ADDR(child->sas_addr),
761 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500762 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500763 }
James Bottomleyb9142172007-07-22 13:15:55 -0500764 } else
765#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800766 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500767 child->dev_type = SAS_END_DEV;
768 rphy = sas_end_device_alloc(phy->port);
769 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600770 if (unlikely(!rphy))
771 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500772 child->tproto = phy->attached_tproto;
773 sas_init_dev(child);
774
775 child->rphy = rphy;
776 sas_fill_in_rphy(child, rphy);
777
James Bottomley9d720d82007-07-16 13:15:51 -0500778 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500779 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500780 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500781
782 res = sas_discover_end_dev(child);
783 if (res) {
784 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
785 "at %016llx:0x%x returned 0x%x\n",
786 SAS_ADDR(child->sas_addr),
787 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600788 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500789 }
790 } else {
791 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
792 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
793 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500794 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500795 }
796
797 list_add_tail(&child->siblings, &parent_ex->children);
798 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600799
800 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800801 sas_rphy_free(child->rphy);
802 child->rphy = NULL;
Dan Williams1a34c062011-09-21 22:05:34 -0700803
Dan Williams87c83312011-11-17 17:59:51 -0800804 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700805 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600806 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700807 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600808 out_free:
809 sas_port_delete(phy->port);
810 out_err:
811 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800812 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600813 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500814}
815
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800816/* See if this phy is part of a wide port */
817static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
818{
819 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
820 int i;
821
822 for (i = 0; i < parent->ex_dev.num_phys; i++) {
823 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
824
825 if (ephy == phy)
826 continue;
827
828 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
829 SAS_ADDR_SIZE) && ephy->port) {
830 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800831 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800832 phy->phy_state = PHY_DEVICE_DISCOVERED;
833 return 0;
834 }
835 }
836
837 return -ENODEV;
838}
839
James Bottomley2908d772006-08-29 09:22:51 -0500840static struct domain_device *sas_ex_discover_expander(
841 struct domain_device *parent, int phy_id)
842{
843 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
844 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
845 struct domain_device *child = NULL;
846 struct sas_rphy *rphy;
847 struct sas_expander_device *edev;
848 struct asd_sas_port *port;
849 int res;
850
851 if (phy->routing_attr == DIRECT_ROUTING) {
852 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
853 "allowed\n",
854 SAS_ADDR(parent->sas_addr), phy_id,
855 SAS_ADDR(phy->attached_sas_addr),
856 phy->attached_phy_id);
857 return NULL;
858 }
Dan Williams735f7d22011-11-17 17:59:47 -0800859 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500860 if (!child)
861 return NULL;
862
863 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
864 /* FIXME: better error handling */
865 BUG_ON(sas_port_add(phy->port) != 0);
866
867
868 switch (phy->attached_dev_type) {
869 case EDGE_DEV:
870 rphy = sas_expander_alloc(phy->port,
871 SAS_EDGE_EXPANDER_DEVICE);
872 break;
873 case FANOUT_DEV:
874 rphy = sas_expander_alloc(phy->port,
875 SAS_FANOUT_EXPANDER_DEVICE);
876 break;
877 default:
878 rphy = NULL; /* shut gcc up */
879 BUG();
880 }
881 port = parent->port;
882 child->rphy = rphy;
883 edev = rphy_to_expander_device(rphy);
884 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800885 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500886 child->parent = parent;
887 child->port = port;
888 child->iproto = phy->attached_iproto;
889 child->tproto = phy->attached_tproto;
890 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
891 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
892 sas_ex_get_linkrate(parent, child, phy);
893 edev->level = parent_ex->level + 1;
894 parent->port->disc.max_level = max(parent->port->disc.max_level,
895 edev->level);
896 sas_init_dev(child);
897 sas_fill_in_rphy(child, rphy);
898 sas_rphy_add(rphy);
899
James Bottomley9d720d82007-07-16 13:15:51 -0500900 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500901 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500902 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500903
904 res = sas_discover_expander(child);
905 if (res) {
Luben Tuikov5911e962011-07-26 23:10:48 -0700906 spin_lock_irq(&parent->port->dev_list_lock);
907 list_del(&child->dev_list_node);
908 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800909 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500910 return NULL;
911 }
912 list_add_tail(&child->siblings, &parent->ex_dev.children);
913 return child;
914}
915
916static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
917{
918 struct expander_device *ex = &dev->ex_dev;
919 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
920 struct domain_device *child = NULL;
921 int res = 0;
922
923 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500924 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500925 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500926 res = sas_ex_phy_discover(dev, phy_id);
927 if (res)
928 return res;
929 }
930
931 /* Parent and domain coherency */
932 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
933 SAS_ADDR(dev->port->sas_addr))) {
934 sas_add_parent_port(dev, phy_id);
935 return 0;
936 }
937 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
938 SAS_ADDR(dev->parent->sas_addr))) {
939 sas_add_parent_port(dev, phy_id);
940 if (ex_phy->routing_attr == TABLE_ROUTING)
941 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
942 return 0;
943 }
944
945 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
946 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
947
948 if (ex_phy->attached_dev_type == NO_DEVICE) {
949 if (ex_phy->routing_attr == DIRECT_ROUTING) {
950 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
951 sas_configure_routing(dev, ex_phy->attached_sas_addr);
952 }
953 return 0;
James Bottomley88edf742006-09-06 17:36:13 -0500954 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -0500955 return 0;
956
957 if (ex_phy->attached_dev_type != SAS_END_DEV &&
958 ex_phy->attached_dev_type != FANOUT_DEV &&
959 ex_phy->attached_dev_type != EDGE_DEV) {
960 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
961 "phy 0x%x\n", ex_phy->attached_dev_type,
962 SAS_ADDR(dev->sas_addr),
963 phy_id);
964 return 0;
965 }
966
967 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
968 if (res) {
969 SAS_DPRINTK("configure routing for dev %016llx "
970 "reported 0x%x. Forgotten\n",
971 SAS_ADDR(ex_phy->attached_sas_addr), res);
972 sas_disable_routing(dev, ex_phy->attached_sas_addr);
973 return res;
974 }
975
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800976 res = sas_ex_join_wide_port(dev, phy_id);
977 if (!res) {
978 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
979 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
980 return res;
981 }
982
James Bottomley2908d772006-08-29 09:22:51 -0500983 switch (ex_phy->attached_dev_type) {
984 case SAS_END_DEV:
985 child = sas_ex_discover_end_dev(dev, phy_id);
986 break;
987 case FANOUT_DEV:
988 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
989 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
990 "attached to ex %016llx phy 0x%x\n",
991 SAS_ADDR(ex_phy->attached_sas_addr),
992 ex_phy->attached_phy_id,
993 SAS_ADDR(dev->sas_addr),
994 phy_id);
995 sas_ex_disable_phy(dev, phy_id);
996 break;
997 } else
998 memcpy(dev->port->disc.fanout_sas_addr,
999 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1000 /* fallthrough */
1001 case EDGE_DEV:
1002 child = sas_ex_discover_expander(dev, phy_id);
1003 break;
1004 default:
1005 break;
1006 }
1007
1008 if (child) {
1009 int i;
1010
1011 for (i = 0; i < ex->num_phys; i++) {
1012 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1013 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1014 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001015 /*
1016 * Due to races, the phy might not get added to the
1017 * wide port, so we add the phy to the wide port here.
1018 */
James Bottomley2908d772006-08-29 09:22:51 -05001019 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001020 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001021 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001022 res = sas_ex_join_wide_port(dev, i);
1023 if (!res)
1024 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1025 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1026
1027 }
James Bottomley2908d772006-08-29 09:22:51 -05001028 }
1029 }
1030
1031 return res;
1032}
1033
1034static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1035{
1036 struct expander_device *ex = &dev->ex_dev;
1037 int i;
1038
1039 for (i = 0; i < ex->num_phys; i++) {
1040 struct ex_phy *phy = &ex->ex_phy[i];
1041
1042 if (phy->phy_state == PHY_VACANT ||
1043 phy->phy_state == PHY_NOT_PRESENT)
1044 continue;
1045
1046 if ((phy->attached_dev_type == EDGE_DEV ||
1047 phy->attached_dev_type == FANOUT_DEV) &&
1048 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1049
1050 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1051
1052 return 1;
1053 }
1054 }
1055 return 0;
1056}
1057
1058static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1059{
1060 struct expander_device *ex = &dev->ex_dev;
1061 struct domain_device *child;
1062 u8 sub_addr[8] = {0, };
1063
1064 list_for_each_entry(child, &ex->children, siblings) {
1065 if (child->dev_type != EDGE_DEV &&
1066 child->dev_type != FANOUT_DEV)
1067 continue;
1068 if (sub_addr[0] == 0) {
1069 sas_find_sub_addr(child, sub_addr);
1070 continue;
1071 } else {
1072 u8 s2[8];
1073
1074 if (sas_find_sub_addr(child, s2) &&
1075 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1076
1077 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1078 "diverges from subtractive "
1079 "boundary %016llx\n",
1080 SAS_ADDR(dev->sas_addr),
1081 SAS_ADDR(child->sas_addr),
1082 SAS_ADDR(s2),
1083 SAS_ADDR(sub_addr));
1084
1085 sas_ex_disable_port(child, s2);
1086 }
1087 }
1088 }
1089 return 0;
1090}
1091/**
1092 * sas_ex_discover_devices -- discover devices attached to this expander
1093 * dev: pointer to the expander domain device
1094 * single: if you want to do a single phy, else set to -1;
1095 *
1096 * Configure this expander for use with its devices and register the
1097 * devices of this expander.
1098 */
1099static int sas_ex_discover_devices(struct domain_device *dev, int single)
1100{
1101 struct expander_device *ex = &dev->ex_dev;
1102 int i = 0, end = ex->num_phys;
1103 int res = 0;
1104
1105 if (0 <= single && single < end) {
1106 i = single;
1107 end = i+1;
1108 }
1109
1110 for ( ; i < end; i++) {
1111 struct ex_phy *ex_phy = &ex->ex_phy[i];
1112
1113 if (ex_phy->phy_state == PHY_VACANT ||
1114 ex_phy->phy_state == PHY_NOT_PRESENT ||
1115 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1116 continue;
1117
1118 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001119 case SAS_PHY_DISABLED:
1120 case SAS_PHY_RESET_PROBLEM:
1121 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001122 continue;
1123 default:
1124 res = sas_ex_discover_dev(dev, i);
1125 if (res)
1126 break;
1127 continue;
1128 }
1129 }
1130
1131 if (!res)
1132 sas_check_level_subtractive_boundary(dev);
1133
1134 return res;
1135}
1136
1137static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1138{
1139 struct expander_device *ex = &dev->ex_dev;
1140 int i;
1141 u8 *sub_sas_addr = NULL;
1142
1143 if (dev->dev_type != EDGE_DEV)
1144 return 0;
1145
1146 for (i = 0; i < ex->num_phys; i++) {
1147 struct ex_phy *phy = &ex->ex_phy[i];
1148
1149 if (phy->phy_state == PHY_VACANT ||
1150 phy->phy_state == PHY_NOT_PRESENT)
1151 continue;
1152
1153 if ((phy->attached_dev_type == FANOUT_DEV ||
1154 phy->attached_dev_type == EDGE_DEV) &&
1155 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1156
1157 if (!sub_sas_addr)
1158 sub_sas_addr = &phy->attached_sas_addr[0];
1159 else if (SAS_ADDR(sub_sas_addr) !=
1160 SAS_ADDR(phy->attached_sas_addr)) {
1161
1162 SAS_DPRINTK("ex %016llx phy 0x%x "
1163 "diverges(%016llx) on subtractive "
1164 "boundary(%016llx). Disabled\n",
1165 SAS_ADDR(dev->sas_addr), i,
1166 SAS_ADDR(phy->attached_sas_addr),
1167 SAS_ADDR(sub_sas_addr));
1168 sas_ex_disable_phy(dev, i);
1169 }
1170 }
1171 }
1172 return 0;
1173}
1174
1175static void sas_print_parent_topology_bug(struct domain_device *child,
1176 struct ex_phy *parent_phy,
1177 struct ex_phy *child_phy)
1178{
1179 static const char ra_char[] = {
1180 [DIRECT_ROUTING] = 'D',
1181 [SUBTRACTIVE_ROUTING] = 'S',
1182 [TABLE_ROUTING] = 'T',
1183 };
1184 static const char *ex_type[] = {
1185 [EDGE_DEV] = "edge",
1186 [FANOUT_DEV] = "fanout",
1187 };
1188 struct domain_device *parent = child->parent;
1189
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001190 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1191 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001192
1193 ex_type[parent->dev_type],
1194 SAS_ADDR(parent->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001195 parent->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001196 parent_phy->phy_id,
1197
1198 ex_type[child->dev_type],
1199 SAS_ADDR(child->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001200 child->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001201 child_phy->phy_id,
1202
1203 ra_char[parent_phy->routing_attr],
1204 ra_char[child_phy->routing_attr]);
1205}
1206
1207static int sas_check_eeds(struct domain_device *child,
1208 struct ex_phy *parent_phy,
1209 struct ex_phy *child_phy)
1210{
1211 int res = 0;
1212 struct domain_device *parent = child->parent;
1213
1214 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1215 res = -ENODEV;
1216 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1217 "phy S:0x%x, while there is a fanout ex %016llx\n",
1218 SAS_ADDR(parent->sas_addr),
1219 parent_phy->phy_id,
1220 SAS_ADDR(child->sas_addr),
1221 child_phy->phy_id,
1222 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1223 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1224 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1225 SAS_ADDR_SIZE);
1226 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1227 SAS_ADDR_SIZE);
1228 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1229 SAS_ADDR(parent->sas_addr)) ||
1230 (SAS_ADDR(parent->port->disc.eeds_a) ==
1231 SAS_ADDR(child->sas_addr)))
1232 &&
1233 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1234 SAS_ADDR(parent->sas_addr)) ||
1235 (SAS_ADDR(parent->port->disc.eeds_b) ==
1236 SAS_ADDR(child->sas_addr))))
1237 ;
1238 else {
1239 res = -ENODEV;
1240 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1241 "phy 0x%x link forms a third EEDS!\n",
1242 SAS_ADDR(parent->sas_addr),
1243 parent_phy->phy_id,
1244 SAS_ADDR(child->sas_addr),
1245 child_phy->phy_id);
1246 }
1247
1248 return res;
1249}
1250
1251/* Here we spill over 80 columns. It is intentional.
1252 */
1253static int sas_check_parent_topology(struct domain_device *child)
1254{
1255 struct expander_device *child_ex = &child->ex_dev;
1256 struct expander_device *parent_ex;
1257 int i;
1258 int res = 0;
1259
1260 if (!child->parent)
1261 return 0;
1262
1263 if (child->parent->dev_type != EDGE_DEV &&
1264 child->parent->dev_type != FANOUT_DEV)
1265 return 0;
1266
1267 parent_ex = &child->parent->ex_dev;
1268
1269 for (i = 0; i < parent_ex->num_phys; i++) {
1270 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1271 struct ex_phy *child_phy;
1272
1273 if (parent_phy->phy_state == PHY_VACANT ||
1274 parent_phy->phy_state == PHY_NOT_PRESENT)
1275 continue;
1276
1277 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1278 continue;
1279
1280 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1281
1282 switch (child->parent->dev_type) {
1283 case EDGE_DEV:
1284 if (child->dev_type == FANOUT_DEV) {
1285 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1286 child_phy->routing_attr != TABLE_ROUTING) {
1287 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1288 res = -ENODEV;
1289 }
1290 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1291 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1292 res = sas_check_eeds(child, parent_phy, child_phy);
1293 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1294 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1295 res = -ENODEV;
1296 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001297 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1298 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1299 (child_phy->routing_attr == TABLE_ROUTING &&
1300 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1301 /* All good */;
1302 } else {
1303 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1304 res = -ENODEV;
1305 }
James Bottomley2908d772006-08-29 09:22:51 -05001306 }
1307 break;
1308 case FANOUT_DEV:
1309 if (parent_phy->routing_attr != TABLE_ROUTING ||
1310 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1311 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1312 res = -ENODEV;
1313 }
1314 break;
1315 default:
1316 break;
1317 }
1318 }
1319
1320 return res;
1321}
1322
1323#define RRI_REQ_SIZE 16
1324#define RRI_RESP_SIZE 44
1325
1326static int sas_configure_present(struct domain_device *dev, int phy_id,
1327 u8 *sas_addr, int *index, int *present)
1328{
1329 int i, res = 0;
1330 struct expander_device *ex = &dev->ex_dev;
1331 struct ex_phy *phy = &ex->ex_phy[phy_id];
1332 u8 *rri_req;
1333 u8 *rri_resp;
1334
1335 *present = 0;
1336 *index = 0;
1337
1338 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1339 if (!rri_req)
1340 return -ENOMEM;
1341
1342 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1343 if (!rri_resp) {
1344 kfree(rri_req);
1345 return -ENOMEM;
1346 }
1347
1348 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1349 rri_req[9] = phy_id;
1350
1351 for (i = 0; i < ex->max_route_indexes ; i++) {
1352 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1353 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1354 RRI_RESP_SIZE);
1355 if (res)
1356 goto out;
1357 res = rri_resp[2];
1358 if (res == SMP_RESP_NO_INDEX) {
1359 SAS_DPRINTK("overflow of indexes: dev %016llx "
1360 "phy 0x%x index 0x%x\n",
1361 SAS_ADDR(dev->sas_addr), phy_id, i);
1362 goto out;
1363 } else if (res != SMP_RESP_FUNC_ACC) {
1364 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001365 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001366 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1367 goto out;
1368 }
1369 if (SAS_ADDR(sas_addr) != 0) {
1370 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1371 *index = i;
1372 if ((rri_resp[12] & 0x80) == 0x80)
1373 *present = 0;
1374 else
1375 *present = 1;
1376 goto out;
1377 } else if (SAS_ADDR(rri_resp+16) == 0) {
1378 *index = i;
1379 *present = 0;
1380 goto out;
1381 }
1382 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1383 phy->last_da_index < i) {
1384 phy->last_da_index = i;
1385 *index = i;
1386 *present = 0;
1387 goto out;
1388 }
1389 }
1390 res = -1;
1391out:
1392 kfree(rri_req);
1393 kfree(rri_resp);
1394 return res;
1395}
1396
1397#define CRI_REQ_SIZE 44
1398#define CRI_RESP_SIZE 8
1399
1400static int sas_configure_set(struct domain_device *dev, int phy_id,
1401 u8 *sas_addr, int index, int include)
1402{
1403 int res;
1404 u8 *cri_req;
1405 u8 *cri_resp;
1406
1407 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1408 if (!cri_req)
1409 return -ENOMEM;
1410
1411 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1412 if (!cri_resp) {
1413 kfree(cri_req);
1414 return -ENOMEM;
1415 }
1416
1417 cri_req[1] = SMP_CONF_ROUTE_INFO;
1418 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1419 cri_req[9] = phy_id;
1420 if (SAS_ADDR(sas_addr) == 0 || !include)
1421 cri_req[12] |= 0x80;
1422 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1423
1424 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1425 CRI_RESP_SIZE);
1426 if (res)
1427 goto out;
1428 res = cri_resp[2];
1429 if (res == SMP_RESP_NO_INDEX) {
1430 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1431 "index 0x%x\n",
1432 SAS_ADDR(dev->sas_addr), phy_id, index);
1433 }
1434out:
1435 kfree(cri_req);
1436 kfree(cri_resp);
1437 return res;
1438}
1439
1440static int sas_configure_phy(struct domain_device *dev, int phy_id,
1441 u8 *sas_addr, int include)
1442{
1443 int index;
1444 int present;
1445 int res;
1446
1447 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1448 if (res)
1449 return res;
1450 if (include ^ present)
1451 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1452
1453 return res;
1454}
1455
1456/**
1457 * sas_configure_parent -- configure routing table of parent
1458 * parent: parent expander
1459 * child: child expander
1460 * sas_addr: SAS port identifier of device directly attached to child
1461 */
1462static int sas_configure_parent(struct domain_device *parent,
1463 struct domain_device *child,
1464 u8 *sas_addr, int include)
1465{
1466 struct expander_device *ex_parent = &parent->ex_dev;
1467 int res = 0;
1468 int i;
1469
1470 if (parent->parent) {
1471 res = sas_configure_parent(parent->parent, parent, sas_addr,
1472 include);
1473 if (res)
1474 return res;
1475 }
1476
1477 if (ex_parent->conf_route_table == 0) {
1478 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1479 SAS_ADDR(parent->sas_addr));
1480 return 0;
1481 }
1482
1483 for (i = 0; i < ex_parent->num_phys; i++) {
1484 struct ex_phy *phy = &ex_parent->ex_phy[i];
1485
1486 if ((phy->routing_attr == TABLE_ROUTING) &&
1487 (SAS_ADDR(phy->attached_sas_addr) ==
1488 SAS_ADDR(child->sas_addr))) {
1489 res = sas_configure_phy(parent, i, sas_addr, include);
1490 if (res)
1491 return res;
1492 }
1493 }
1494
1495 return res;
1496}
1497
1498/**
1499 * sas_configure_routing -- configure routing
1500 * dev: expander device
1501 * sas_addr: port identifier of device directly attached to the expander device
1502 */
1503static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1504{
1505 if (dev->parent)
1506 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1507 return 0;
1508}
1509
1510static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1511{
1512 if (dev->parent)
1513 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1514 return 0;
1515}
1516
James Bottomley2908d772006-08-29 09:22:51 -05001517/**
1518 * sas_discover_expander -- expander discovery
1519 * @ex: pointer to expander domain device
1520 *
1521 * See comment in sas_discover_sata().
1522 */
1523static int sas_discover_expander(struct domain_device *dev)
1524{
1525 int res;
1526
1527 res = sas_notify_lldd_dev_found(dev);
1528 if (res)
1529 return res;
1530
1531 res = sas_ex_general(dev);
1532 if (res)
1533 goto out_err;
1534 res = sas_ex_manuf_info(dev);
1535 if (res)
1536 goto out_err;
1537
1538 res = sas_expander_discover(dev);
1539 if (res) {
1540 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1541 SAS_ADDR(dev->sas_addr), res);
1542 goto out_err;
1543 }
1544
1545 sas_check_ex_subtractive_boundary(dev);
1546 res = sas_check_parent_topology(dev);
1547 if (res)
1548 goto out_err;
1549 return 0;
1550out_err:
1551 sas_notify_lldd_dev_gone(dev);
1552 return res;
1553}
1554
1555static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1556{
1557 int res = 0;
1558 struct domain_device *dev;
1559
1560 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1561 if (dev->dev_type == EDGE_DEV ||
1562 dev->dev_type == FANOUT_DEV) {
1563 struct sas_expander_device *ex =
1564 rphy_to_expander_device(dev->rphy);
1565
1566 if (level == ex->level)
1567 res = sas_ex_discover_devices(dev, -1);
1568 else if (level > 0)
1569 res = sas_ex_discover_devices(port->port_dev, -1);
1570
1571 }
1572 }
1573
1574 return res;
1575}
1576
1577static int sas_ex_bfs_disc(struct asd_sas_port *port)
1578{
1579 int res;
1580 int level;
1581
1582 do {
1583 level = port->disc.max_level;
1584 res = sas_ex_level_discovery(port, level);
1585 mb();
1586 } while (level < port->disc.max_level);
1587
1588 return res;
1589}
1590
1591int sas_discover_root_expander(struct domain_device *dev)
1592{
1593 int res;
1594 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1595
Darrick J. Wongbf451202007-01-11 14:14:52 -08001596 res = sas_rphy_add(dev->rphy);
1597 if (res)
1598 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001599
1600 ex->level = dev->port->disc.max_level; /* 0 */
1601 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001602 if (res)
1603 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001604
Darrick J. Wongbf451202007-01-11 14:14:52 -08001605 sas_ex_bfs_disc(dev->port);
1606
1607 return res;
1608
1609out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001610 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001611out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001612 return res;
1613}
1614
1615/* ---------- Domain revalidation ---------- */
1616
1617static int sas_get_phy_discover(struct domain_device *dev,
1618 int phy_id, struct smp_resp *disc_resp)
1619{
1620 int res;
1621 u8 *disc_req;
1622
1623 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1624 if (!disc_req)
1625 return -ENOMEM;
1626
1627 disc_req[1] = SMP_DISCOVER;
1628 disc_req[9] = phy_id;
1629
1630 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1631 disc_resp, DISCOVER_RESP_SIZE);
1632 if (res)
1633 goto out;
1634 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1635 res = disc_resp->result;
1636 goto out;
1637 }
1638out:
1639 kfree(disc_req);
1640 return res;
1641}
1642
1643static int sas_get_phy_change_count(struct domain_device *dev,
1644 int phy_id, int *pcc)
1645{
1646 int res;
1647 struct smp_resp *disc_resp;
1648
1649 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1650 if (!disc_resp)
1651 return -ENOMEM;
1652
1653 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1654 if (!res)
1655 *pcc = disc_resp->disc.change_count;
1656
1657 kfree(disc_resp);
1658 return res;
1659}
1660
Dan Williams36a39942011-11-17 17:59:54 -08001661int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1662 u8 *attached_sas_addr)
James Bottomley2908d772006-08-29 09:22:51 -05001663{
1664 int res;
1665 struct smp_resp *disc_resp;
1666 struct discover_resp *dr;
1667
1668 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1669 if (!disc_resp)
1670 return -ENOMEM;
1671 dr = &disc_resp->disc;
1672
1673 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1674 if (!res) {
1675 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1676 if (dr->attached_dev_type == 0)
1677 memset(attached_sas_addr, 0, 8);
1678 }
1679 kfree(disc_resp);
1680 return res;
1681}
1682
1683static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001684 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001685{
1686 struct expander_device *ex = &dev->ex_dev;
1687 int res = 0;
1688 int i;
1689
1690 for (i = from_phy; i < ex->num_phys; i++) {
1691 int phy_change_count = 0;
1692
1693 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1694 if (res)
1695 goto out;
1696 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001697 if (update)
1698 ex->ex_phy[i].phy_change_count =
1699 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001700 *phy_id = i;
1701 return 0;
1702 }
1703 }
1704out:
1705 return res;
1706}
1707
1708static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1709{
1710 int res;
1711 u8 *rg_req;
1712 struct smp_resp *rg_resp;
1713
1714 rg_req = alloc_smp_req(RG_REQ_SIZE);
1715 if (!rg_req)
1716 return -ENOMEM;
1717
1718 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1719 if (!rg_resp) {
1720 kfree(rg_req);
1721 return -ENOMEM;
1722 }
1723
1724 rg_req[1] = SMP_REPORT_GENERAL;
1725
1726 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1727 RG_RESP_SIZE);
1728 if (res)
1729 goto out;
1730 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1731 res = rg_resp->result;
1732 goto out;
1733 }
1734
1735 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1736out:
1737 kfree(rg_resp);
1738 kfree(rg_req);
1739 return res;
1740}
Tom Peng19252de2009-07-17 16:02:04 +08001741/**
1742 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1743 * @dev:domain device to be detect.
1744 * @src_dev: the device which originated BROADCAST(CHANGE).
1745 *
1746 * Add self-configuration expander suport. Suppose two expander cascading,
1747 * when the first level expander is self-configuring, hotplug the disks in
1748 * second level expander, BROADCAST(CHANGE) will not only be originated
1749 * in the second level expander, but also be originated in the first level
1750 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1751 * expander changed count in two level expanders will all increment at least
1752 * once, but the phy which chang count has changed is the source device which
1753 * we concerned.
1754 */
James Bottomley2908d772006-08-29 09:22:51 -05001755
1756static int sas_find_bcast_dev(struct domain_device *dev,
1757 struct domain_device **src_dev)
1758{
1759 struct expander_device *ex = &dev->ex_dev;
1760 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001761 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001762 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001763 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001764
1765 res = sas_get_ex_change_count(dev, &ex_change_count);
1766 if (res)
1767 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001768 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1769 /* Just detect if this expander phys phy change count changed,
1770 * in order to determine if this expander originate BROADCAST,
1771 * and do not update phy change count field in our structure.
1772 */
1773 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1774 if (phy_id != -1) {
1775 *src_dev = dev;
1776 ex->ex_change_count = ex_change_count;
1777 SAS_DPRINTK("Expander phy change count has changed\n");
1778 return res;
1779 } else
1780 SAS_DPRINTK("Expander phys DID NOT change\n");
1781 }
1782 list_for_each_entry(ch, &ex->children, siblings) {
1783 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1784 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001785 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001786 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001787 }
1788 }
1789out:
1790 return res;
1791}
1792
Dan Williams1a34c062011-09-21 22:05:34 -07001793static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001794{
1795 struct expander_device *ex = &dev->ex_dev;
1796 struct domain_device *child, *n;
1797
1798 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001799 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001800 if (child->dev_type == EDGE_DEV ||
1801 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001802 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001803 else
Dan Williams1a34c062011-09-21 22:05:34 -07001804 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001805 }
Dan Williams1a34c062011-09-21 22:05:34 -07001806 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001807}
1808
1809static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001810 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001811{
1812 struct expander_device *ex_dev = &parent->ex_dev;
1813 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
Dan Williamsf41a0c42011-12-21 21:33:17 -08001814 struct domain_device *child, *n, *found = NULL;
Tom Peng19252de2009-07-17 16:02:04 +08001815 if (last) {
1816 list_for_each_entry_safe(child, n,
1817 &ex_dev->children, siblings) {
1818 if (SAS_ADDR(child->sas_addr) ==
1819 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001820 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001821 if (child->dev_type == EDGE_DEV ||
1822 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001823 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001824 else
Dan Williams1a34c062011-09-21 22:05:34 -07001825 sas_unregister_dev(parent->port, child);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001826 found = child;
Tom Peng19252de2009-07-17 16:02:04 +08001827 break;
1828 }
James Bottomley2908d772006-08-29 09:22:51 -05001829 }
Tom Peng19252de2009-07-17 16:02:04 +08001830 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001831 }
James Bottomley2908d772006-08-29 09:22:51 -05001832 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001833 if (phy->port) {
1834 sas_port_delete_phy(phy->port, phy->phy);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001835 sas_device_set_phy(found, phy->port);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001836 if (phy->port->num_phys == 0)
1837 sas_port_delete(phy->port);
1838 phy->port = NULL;
1839 }
James Bottomley2908d772006-08-29 09:22:51 -05001840}
1841
1842static int sas_discover_bfs_by_root_level(struct domain_device *root,
1843 const int level)
1844{
1845 struct expander_device *ex_root = &root->ex_dev;
1846 struct domain_device *child;
1847 int res = 0;
1848
1849 list_for_each_entry(child, &ex_root->children, siblings) {
1850 if (child->dev_type == EDGE_DEV ||
1851 child->dev_type == FANOUT_DEV) {
1852 struct sas_expander_device *ex =
1853 rphy_to_expander_device(child->rphy);
1854
1855 if (level > ex->level)
1856 res = sas_discover_bfs_by_root_level(child,
1857 level);
1858 else if (level == ex->level)
1859 res = sas_ex_discover_devices(child, -1);
1860 }
1861 }
1862 return res;
1863}
1864
1865static int sas_discover_bfs_by_root(struct domain_device *dev)
1866{
1867 int res;
1868 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1869 int level = ex->level+1;
1870
1871 res = sas_ex_discover_devices(dev, -1);
1872 if (res)
1873 goto out;
1874 do {
1875 res = sas_discover_bfs_by_root_level(dev, level);
1876 mb();
1877 level += 1;
1878 } while (level <= dev->port->disc.max_level);
1879out:
1880 return res;
1881}
1882
1883static int sas_discover_new(struct domain_device *dev, int phy_id)
1884{
1885 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1886 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001887 bool found = false;
1888 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001889
1890 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1891 SAS_ADDR(dev->sas_addr), phy_id);
1892 res = sas_ex_phy_discover(dev, phy_id);
1893 if (res)
1894 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001895 /* to support the wide port inserted */
1896 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1897 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1898 if (i == phy_id)
1899 continue;
1900 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1901 SAS_ADDR(ex_phy->attached_sas_addr)) {
1902 found = true;
1903 break;
1904 }
1905 }
1906 if (found) {
1907 sas_ex_join_wide_port(dev, phy_id);
1908 return 0;
1909 }
James Bottomley2908d772006-08-29 09:22:51 -05001910 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001911 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001912 goto out;
1913 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1914 if (SAS_ADDR(child->sas_addr) ==
1915 SAS_ADDR(ex_phy->attached_sas_addr)) {
1916 if (child->dev_type == EDGE_DEV ||
1917 child->dev_type == FANOUT_DEV)
1918 res = sas_discover_bfs_by_root(child);
1919 break;
1920 }
1921 }
1922out:
1923 return res;
1924}
1925
Tom Peng19252de2009-07-17 16:02:04 +08001926static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001927{
1928 struct expander_device *ex = &dev->ex_dev;
1929 struct ex_phy *phy = &ex->ex_phy[phy_id];
1930 u8 attached_sas_addr[8];
1931 int res;
1932
1933 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1934 switch (res) {
1935 case SMP_RESP_NO_PHY:
1936 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08001937 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001938 goto out; break;
1939 case SMP_RESP_PHY_VACANT:
1940 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08001941 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001942 goto out; break;
1943 case SMP_RESP_FUNC_ACC:
1944 break;
1945 }
1946
1947 if (SAS_ADDR(attached_sas_addr) == 0) {
1948 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08001949 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001950 } else if (SAS_ADDR(attached_sas_addr) ==
1951 SAS_ADDR(phy->attached_sas_addr)) {
1952 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1953 SAS_ADDR(dev->sas_addr), phy_id);
James Bottomleya01e70e2006-09-06 19:28:07 -05001954 sas_ex_phy_discover(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001955 } else
1956 res = sas_discover_new(dev, phy_id);
1957out:
1958 return res;
1959}
1960
Tom Peng19252de2009-07-17 16:02:04 +08001961/**
1962 * sas_rediscover - revalidate the domain.
1963 * @dev:domain device to be detect.
1964 * @phy_id: the phy id will be detected.
1965 *
1966 * NOTE: this process _must_ quit (return) as soon as any connection
1967 * errors are encountered. Connection recovery is done elsewhere.
1968 * Discover process only interrogates devices in order to discover the
1969 * domain.For plugging out, we un-register the device only when it is
1970 * the last phy in the port, for other phys in this port, we just delete it
1971 * from the port.For inserting, we do discovery when it is the
1972 * first phy,for other phys in this port, we add it to the port to
1973 * forming the wide-port.
1974 */
James Bottomley2908d772006-08-29 09:22:51 -05001975static int sas_rediscover(struct domain_device *dev, const int phy_id)
1976{
1977 struct expander_device *ex = &dev->ex_dev;
1978 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1979 int res = 0;
1980 int i;
Tom Peng19252de2009-07-17 16:02:04 +08001981 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05001982
1983 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1984 SAS_ADDR(dev->sas_addr), phy_id);
1985
1986 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1987 for (i = 0; i < ex->num_phys; i++) {
1988 struct ex_phy *phy = &ex->ex_phy[i];
1989
1990 if (i == phy_id)
1991 continue;
1992 if (SAS_ADDR(phy->attached_sas_addr) ==
1993 SAS_ADDR(changed_phy->attached_sas_addr)) {
1994 SAS_DPRINTK("phy%d part of wide port with "
1995 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08001996 last = false;
1997 break;
James Bottomley2908d772006-08-29 09:22:51 -05001998 }
1999 }
Tom Peng19252de2009-07-17 16:02:04 +08002000 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05002001 } else
2002 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05002003 return res;
2004}
2005
2006/**
2007 * sas_revalidate_domain -- revalidate the domain
2008 * @port: port to the domain of interest
2009 *
2010 * NOTE: this process _must_ quit (return) as soon as any connection
2011 * errors are encountered. Connection recovery is done elsewhere.
2012 * Discover process only interrogates devices in order to discover the
2013 * domain.
2014 */
2015int sas_ex_revalidate_domain(struct domain_device *port_dev)
2016{
2017 int res;
2018 struct domain_device *dev = NULL;
2019
2020 res = sas_find_bcast_dev(port_dev, &dev);
2021 if (res)
2022 goto out;
2023 if (dev) {
2024 struct expander_device *ex = &dev->ex_dev;
2025 int i = 0, phy_id;
2026
2027 do {
2028 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002029 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002030 if (phy_id == -1)
2031 break;
2032 res = sas_rediscover(dev, phy_id);
2033 i = phy_id + 1;
2034 } while (i < ex->num_phys);
2035 }
2036out:
2037 return res;
2038}
2039
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002040int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2041 struct request *req)
2042{
2043 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002044 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002045 struct request *rsp = req->next_rq;
2046
2047 if (!rsp) {
2048 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002049 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002050 return -EINVAL;
2051 }
2052
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002053 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002054 if (!rphy)
2055 return sas_smp_host_handler(shost, req, rsp);
2056
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002057 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002058
2059 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2060 type != SAS_FANOUT_EXPANDER_DEVICE) {
2061 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002062 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002063 return -EINVAL;
2064 }
2065
2066 dev = sas_find_dev_by_rphy(rphy);
2067 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002068 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002069 return -EINVAL;
2070 }
2071
2072 /* do we need to support multiple segments? */
2073 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2074 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002075 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2076 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002077 return -EINVAL;
2078 }
2079
Tejun Heob0790412009-05-07 22:24:42 +09002080 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2081 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002082 if (ret > 0) {
2083 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002084 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002085 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002086 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002087 } else if (ret == 0) {
2088 rsp->resid_len = 0;
2089 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002090 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002091
2092 return ret;
2093}