blob: f4894b0f537b4b8489e28d80d280b85eddd73618 [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
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070075 for (retry = 0; retry < 3; retry++) {
76 task = sas_alloc_task(GFP_KERNEL);
77 if (!task)
78 return -ENOMEM;
James Bottomley2908d772006-08-29 09:22:51 -050079
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070080 task->dev = dev;
81 task->task_proto = dev->tproto;
82 sg_init_one(&task->smp_task.smp_req, req, req_size);
83 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
James Bottomley2908d772006-08-29 09:22:51 -050084
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070085 task->task_done = smp_task_done;
James Bottomley2908d772006-08-29 09:22:51 -050086
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070087 task->timer.data = (unsigned long) task;
88 task->timer.function = smp_task_timedout;
89 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
90 add_timer(&task->timer);
James Bottomley2908d772006-08-29 09:22:51 -050091
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070092 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
James Bottomley2908d772006-08-29 09:22:51 -050093
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070094 if (res) {
95 del_timer(&task->timer);
96 SAS_DPRINTK("executing SMP task failed:%d\n", res);
James Bottomley2908d772006-08-29 09:22:51 -050097 goto ex_err;
98 }
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -070099
100 wait_for_completion(&task->completion);
James Bottomley32e8ae32007-12-30 12:37:31 -0600101 res = -ECOMM;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700102 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
103 SAS_DPRINTK("smp task timed out or aborted\n");
104 i->dft->lldd_abort_task(task);
105 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
106 SAS_DPRINTK("SMP task aborted and not done\n");
107 goto ex_err;
108 }
109 }
110 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500111 task->task_status.stat == SAM_STAT_GOOD) {
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700112 res = 0;
113 break;
James Bottomley2d4b63e2007-12-29 11:49:53 -0600114 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
115 task->task_status.stat == SAS_DATA_UNDERRUN) {
116 /* no error, but return the number of bytes of
117 * underrun */
118 res = task->task_status.residual;
119 break;
120 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
121 task->task_status.stat == SAS_DATA_OVERRUN) {
122 res = -EMSGSIZE;
123 break;
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700124 } else {
125 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700126 "status 0x%x\n", __func__,
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700127 SAS_ADDR(dev->sas_addr),
128 task->task_status.resp,
129 task->task_status.stat);
130 sas_free_task(task);
131 task = NULL;
132 }
James Bottomley2908d772006-08-29 09:22:51 -0500133 }
James Bottomley2908d772006-08-29 09:22:51 -0500134ex_err:
malahal@us.ibm.com42961ee2006-10-04 17:34:03 -0700135 BUG_ON(retry == 3 && task != NULL);
136 if (task != NULL) {
137 sas_free_task(task);
138 }
James Bottomley2908d772006-08-29 09:22:51 -0500139 return res;
140}
141
142/* ---------- Allocations ---------- */
143
144static inline void *alloc_smp_req(int size)
145{
146 u8 *p = kzalloc(size, GFP_KERNEL);
147 if (p)
148 p[0] = SMP_REQUEST;
149 return p;
150}
151
152static inline void *alloc_smp_resp(int size)
153{
154 return kzalloc(size, GFP_KERNEL);
155}
156
157/* ---------- Expander configuration ---------- */
158
159static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
160 void *disc_resp)
161{
162 struct expander_device *ex = &dev->ex_dev;
163 struct ex_phy *phy = &ex->ex_phy[phy_id];
164 struct smp_resp *resp = disc_resp;
165 struct discover_resp *dr = &resp->disc;
166 struct sas_rphy *rphy = dev->rphy;
167 int rediscover = (phy->phy != NULL);
168
169 if (!rediscover) {
170 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
171
172 /* FIXME: error_handling */
173 BUG_ON(!phy->phy);
174 }
175
176 switch (resp->result) {
177 case SMP_RESP_PHY_VACANT:
178 phy->phy_state = PHY_VACANT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800179 break;
James Bottomley2908d772006-08-29 09:22:51 -0500180 default:
181 phy->phy_state = PHY_NOT_PRESENT;
Jack Wang2bc72c92010-10-06 16:05:35 +0800182 break;
James Bottomley2908d772006-08-29 09:22:51 -0500183 case SMP_RESP_FUNC_ACC:
184 phy->phy_state = PHY_EMPTY; /* do not know yet */
185 break;
186 }
187
188 phy->phy_id = phy_id;
189 phy->attached_dev_type = dr->attached_dev_type;
190 phy->linkrate = dr->linkrate;
191 phy->attached_sata_host = dr->attached_sata_host;
192 phy->attached_sata_dev = dr->attached_sata_dev;
193 phy->attached_sata_ps = dr->attached_sata_ps;
194 phy->attached_iproto = dr->iproto << 1;
195 phy->attached_tproto = dr->tproto << 1;
196 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
197 phy->attached_phy_id = dr->attached_phy_id;
198 phy->phy_change_count = dr->change_count;
199 phy->routing_attr = dr->routing_attr;
200 phy->virtual = dr->virtual;
201 phy->last_da_index = -1;
202
Jack Wangbb041a02011-09-23 14:32:32 +0800203 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
204 phy->phy->identify.device_type = phy->attached_dev_type;
James Bottomley2908d772006-08-29 09:22:51 -0500205 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
206 phy->phy->identify.target_port_protocols = phy->attached_tproto;
207 phy->phy->identify.phy_identifier = phy_id;
James Bottomleya01e70e2006-09-06 19:28:07 -0500208 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
209 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
210 phy->phy->minimum_linkrate = dr->pmin_linkrate;
211 phy->phy->maximum_linkrate = dr->pmax_linkrate;
James Bottomley88edf742006-09-06 17:36:13 -0500212 phy->phy->negotiated_linkrate = phy->linkrate;
James Bottomley2908d772006-08-29 09:22:51 -0500213
214 if (!rediscover)
Jack Wang2bc72c92010-10-06 16:05:35 +0800215 if (sas_phy_add(phy->phy)) {
216 sas_phy_free(phy->phy);
217 return;
218 }
James Bottomley2908d772006-08-29 09:22:51 -0500219
220 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
221 SAS_ADDR(dev->sas_addr), phy->phy_id,
222 phy->routing_attr == TABLE_ROUTING ? 'T' :
223 phy->routing_attr == DIRECT_ROUTING ? 'D' :
224 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
225 SAS_ADDR(phy->attached_sas_addr));
226
227 return;
228}
229
Dan Williamsb52df412011-11-30 23:23:33 -0800230/* check if we have an existing attached ata device on this expander phy */
231static struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
232{
233 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
234 struct domain_device *dev;
235 struct sas_rphy *rphy;
236
237 if (!ex_phy->port)
238 return NULL;
239
240 rphy = ex_phy->port->rphy;
241 if (!rphy)
242 return NULL;
243
244 dev = sas_find_dev_by_rphy(rphy);
245
246 if (dev && dev_is_sata(dev))
247 return dev;
248
249 return NULL;
250}
251
James Bottomley2908d772006-08-29 09:22:51 -0500252#define DISCOVER_REQ_SIZE 16
253#define DISCOVER_RESP_SIZE 56
254
James Bottomley1acce192006-08-22 12:39:19 -0500255static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
256 u8 *disc_resp, int single)
257{
Dan Williamsb52df412011-11-30 23:23:33 -0800258 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
James Bottomley1acce192006-08-22 12:39:19 -0500259 int i, res;
260
261 disc_req[9] = single;
262 for (i = 1 ; i < 3; i++) {
263 struct discover_resp *dr;
264
265 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
266 disc_resp, DISCOVER_RESP_SIZE);
267 if (res)
268 return res;
James Bottomley1acce192006-08-22 12:39:19 -0500269 dr = &((struct smp_resp *)disc_resp)->disc;
jack_wang183ce892011-02-19 18:20:53 +0800270 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
271 SAS_ADDR_SIZE) == 0) {
272 sas_printk("Found loopback topology, just ignore it!\n");
273 return 0;
274 }
Dan Williamsb52df412011-11-30 23:23:33 -0800275
276 /* This is detecting a failure to transmit initial
277 * dev to host FIS as described in section J.5 of
278 * sas-2 r16
279 */
James Bottomley1acce192006-08-22 12:39:19 -0500280 if (!(dr->attached_dev_type == 0 &&
281 dr->attached_sata_dev))
282 break;
Dan Williamsb52df412011-11-30 23:23:33 -0800283
284 /* In order to generate the dev to host FIS, we send a
285 * link reset to the expander port. If a device was
286 * previously detected on this port we ask libata to
287 * manage the reset and link recovery.
288 */
289 if (ata_dev) {
290 sas_ata_schedule_reset(ata_dev);
291 break;
292 }
James Bottomley38e2f032006-09-07 15:52:09 -0500293 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
James Bottomley1acce192006-08-22 12:39:19 -0500294 /* Wait for the reset to trigger the negotiation */
295 msleep(500);
296 }
297 sas_set_ex_phy(dev, single, disc_resp);
298 return 0;
299}
300
James Bottomley2908d772006-08-29 09:22:51 -0500301static int sas_ex_phy_discover(struct domain_device *dev, int single)
302{
303 struct expander_device *ex = &dev->ex_dev;
304 int res = 0;
305 u8 *disc_req;
306 u8 *disc_resp;
307
308 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
309 if (!disc_req)
310 return -ENOMEM;
311
312 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
313 if (!disc_resp) {
314 kfree(disc_req);
315 return -ENOMEM;
316 }
317
318 disc_req[1] = SMP_DISCOVER;
319
320 if (0 <= single && single < ex->num_phys) {
James Bottomley1acce192006-08-22 12:39:19 -0500321 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley2908d772006-08-29 09:22:51 -0500322 } else {
323 int i;
324
325 for (i = 0; i < ex->num_phys; i++) {
James Bottomley1acce192006-08-22 12:39:19 -0500326 res = sas_ex_phy_discover_helper(dev, disc_req,
327 disc_resp, i);
James Bottomley2908d772006-08-29 09:22:51 -0500328 if (res)
329 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -0500330 }
331 }
332out_err:
333 kfree(disc_resp);
334 kfree(disc_req);
335 return res;
336}
337
338static int sas_expander_discover(struct domain_device *dev)
339{
340 struct expander_device *ex = &dev->ex_dev;
341 int res = -ENOMEM;
342
343 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
344 if (!ex->ex_phy)
345 return -ENOMEM;
346
347 res = sas_ex_phy_discover(dev, -1);
348 if (res)
349 goto out_err;
350
351 return 0;
352 out_err:
353 kfree(ex->ex_phy);
354 ex->ex_phy = NULL;
355 return res;
356}
357
358#define MAX_EXPANDER_PHYS 128
359
360static void ex_assign_report_general(struct domain_device *dev,
361 struct smp_resp *resp)
362{
363 struct report_general_resp *rg = &resp->rg;
364
365 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
366 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
367 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
Luben Tuikovffaac8f2011-09-22 09:41:36 -0700368 dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley2908d772006-08-29 09:22:51 -0500369 dev->ex_dev.conf_route_table = rg->conf_route_table;
370 dev->ex_dev.configuring = rg->configuring;
371 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
372}
373
374#define RG_REQ_SIZE 8
375#define RG_RESP_SIZE 32
376
377static int sas_ex_general(struct domain_device *dev)
378{
379 u8 *rg_req;
380 struct smp_resp *rg_resp;
381 int res;
382 int i;
383
384 rg_req = alloc_smp_req(RG_REQ_SIZE);
385 if (!rg_req)
386 return -ENOMEM;
387
388 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
389 if (!rg_resp) {
390 kfree(rg_req);
391 return -ENOMEM;
392 }
393
394 rg_req[1] = SMP_REPORT_GENERAL;
395
396 for (i = 0; i < 5; i++) {
397 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
398 RG_RESP_SIZE);
399
400 if (res) {
401 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
402 SAS_ADDR(dev->sas_addr), res);
403 goto out;
404 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
405 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
406 SAS_ADDR(dev->sas_addr), rg_resp->result);
407 res = rg_resp->result;
408 goto out;
409 }
410
411 ex_assign_report_general(dev, rg_resp);
412
413 if (dev->ex_dev.configuring) {
414 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
415 SAS_ADDR(dev->sas_addr));
416 schedule_timeout_interruptible(5*HZ);
417 } else
418 break;
419 }
420out:
421 kfree(rg_req);
422 kfree(rg_resp);
423 return res;
424}
425
426static void ex_assign_manuf_info(struct domain_device *dev, void
427 *_mi_resp)
428{
429 u8 *mi_resp = _mi_resp;
430 struct sas_rphy *rphy = dev->rphy;
431 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
432
433 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
434 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
435 memcpy(edev->product_rev, mi_resp + 36,
436 SAS_EXPANDER_PRODUCT_REV_LEN);
437
438 if (mi_resp[8] & 1) {
439 memcpy(edev->component_vendor_id, mi_resp + 40,
440 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
441 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
442 edev->component_revision_id = mi_resp[50];
443 }
444}
445
446#define MI_REQ_SIZE 8
447#define MI_RESP_SIZE 64
448
449static int sas_ex_manuf_info(struct domain_device *dev)
450{
451 u8 *mi_req;
452 u8 *mi_resp;
453 int res;
454
455 mi_req = alloc_smp_req(MI_REQ_SIZE);
456 if (!mi_req)
457 return -ENOMEM;
458
459 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
460 if (!mi_resp) {
461 kfree(mi_req);
462 return -ENOMEM;
463 }
464
465 mi_req[1] = SMP_REPORT_MANUF_INFO;
466
467 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
468 if (res) {
469 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
470 SAS_ADDR(dev->sas_addr), res);
471 goto out;
472 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
473 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
474 SAS_ADDR(dev->sas_addr), mi_resp[2]);
475 goto out;
476 }
477
478 ex_assign_manuf_info(dev, mi_resp);
479out:
480 kfree(mi_req);
481 kfree(mi_resp);
482 return res;
483}
484
485#define PC_REQ_SIZE 44
486#define PC_RESP_SIZE 8
487
488int sas_smp_phy_control(struct domain_device *dev, int phy_id,
James Bottomleya01e70e2006-09-06 19:28:07 -0500489 enum phy_func phy_func,
490 struct sas_phy_linkrates *rates)
James Bottomley2908d772006-08-29 09:22:51 -0500491{
492 u8 *pc_req;
493 u8 *pc_resp;
494 int res;
495
496 pc_req = alloc_smp_req(PC_REQ_SIZE);
497 if (!pc_req)
498 return -ENOMEM;
499
500 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
501 if (!pc_resp) {
502 kfree(pc_req);
503 return -ENOMEM;
504 }
505
506 pc_req[1] = SMP_PHY_CONTROL;
507 pc_req[9] = phy_id;
508 pc_req[10]= phy_func;
James Bottomleya01e70e2006-09-06 19:28:07 -0500509 if (rates) {
510 pc_req[32] = rates->minimum_linkrate << 4;
511 pc_req[33] = rates->maximum_linkrate << 4;
512 }
James Bottomley2908d772006-08-29 09:22:51 -0500513
514 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
515
516 kfree(pc_resp);
517 kfree(pc_req);
518 return res;
519}
520
521static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
522{
523 struct expander_device *ex = &dev->ex_dev;
524 struct ex_phy *phy = &ex->ex_phy[phy_id];
525
James Bottomleya01e70e2006-09-06 19:28:07 -0500526 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
James Bottomley88edf742006-09-06 17:36:13 -0500527 phy->linkrate = SAS_PHY_DISABLED;
James Bottomley2908d772006-08-29 09:22:51 -0500528}
529
530static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
531{
532 struct expander_device *ex = &dev->ex_dev;
533 int i;
534
535 for (i = 0; i < ex->num_phys; i++) {
536 struct ex_phy *phy = &ex->ex_phy[i];
537
538 if (phy->phy_state == PHY_VACANT ||
539 phy->phy_state == PHY_NOT_PRESENT)
540 continue;
541
542 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
543 sas_ex_disable_phy(dev, i);
544 }
545}
546
547static int sas_dev_present_in_domain(struct asd_sas_port *port,
548 u8 *sas_addr)
549{
550 struct domain_device *dev;
551
552 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
553 return 1;
554 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
555 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
556 return 1;
557 }
558 return 0;
559}
560
561#define RPEL_REQ_SIZE 16
562#define RPEL_RESP_SIZE 32
563int sas_smp_get_phy_events(struct sas_phy *phy)
564{
565 int res;
Jesper Juhl92631fa2007-07-28 01:13:33 +0200566 u8 *req;
567 u8 *resp;
James Bottomley2908d772006-08-29 09:22:51 -0500568 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
569 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
James Bottomley2908d772006-08-29 09:22:51 -0500570
Jesper Juhl92631fa2007-07-28 01:13:33 +0200571 req = alloc_smp_req(RPEL_REQ_SIZE);
572 if (!req)
James Bottomley2908d772006-08-29 09:22:51 -0500573 return -ENOMEM;
574
Jesper Juhl92631fa2007-07-28 01:13:33 +0200575 resp = alloc_smp_resp(RPEL_RESP_SIZE);
576 if (!resp) {
577 kfree(req);
578 return -ENOMEM;
579 }
580
James Bottomley2908d772006-08-29 09:22:51 -0500581 req[1] = SMP_REPORT_PHY_ERR_LOG;
582 req[9] = phy->number;
583
584 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
585 resp, RPEL_RESP_SIZE);
586
587 if (!res)
588 goto out;
589
590 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
591 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
592 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
593 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
594
595 out:
596 kfree(resp);
597 return res;
598
599}
600
James Bottomleyb9142172007-07-22 13:15:55 -0500601#ifdef CONFIG_SCSI_SAS_ATA
602
James Bottomley2908d772006-08-29 09:22:51 -0500603#define RPS_REQ_SIZE 16
604#define RPS_RESP_SIZE 60
605
606static int sas_get_report_phy_sata(struct domain_device *dev,
607 int phy_id,
608 struct smp_resp *rps_resp)
609{
610 int res;
611 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
James Bottomley1acce192006-08-22 12:39:19 -0500612 u8 *resp = (u8 *)rps_resp;
James Bottomley2908d772006-08-29 09:22:51 -0500613
614 if (!rps_req)
615 return -ENOMEM;
616
617 rps_req[1] = SMP_REPORT_PHY_SATA;
618 rps_req[9] = phy_id;
619
620 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
621 rps_resp, RPS_RESP_SIZE);
622
James Bottomley1acce192006-08-22 12:39:19 -0500623 /* 0x34 is the FIS type for the D2H fis. There's a potential
624 * standards cockup here. sas-2 explicitly specifies the FIS
625 * should be encoded so that FIS type is in resp[24].
626 * However, some expanders endian reverse this. Undo the
627 * reversal here */
628 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
629 int i;
630
631 for (i = 0; i < 5; i++) {
632 int j = 24 + (i*4);
633 u8 a, b;
634 a = resp[j + 0];
635 b = resp[j + 1];
636 resp[j + 0] = resp[j + 3];
637 resp[j + 1] = resp[j + 2];
638 resp[j + 2] = b;
639 resp[j + 3] = a;
640 }
641 }
642
James Bottomley2908d772006-08-29 09:22:51 -0500643 kfree(rps_req);
James Bottomley1acce192006-08-22 12:39:19 -0500644 return res;
James Bottomley2908d772006-08-29 09:22:51 -0500645}
James Bottomleyb9142172007-07-22 13:15:55 -0500646#endif
James Bottomley2908d772006-08-29 09:22:51 -0500647
648static void sas_ex_get_linkrate(struct domain_device *parent,
649 struct domain_device *child,
650 struct ex_phy *parent_phy)
651{
652 struct expander_device *parent_ex = &parent->ex_dev;
653 struct sas_port *port;
654 int i;
655
656 child->pathways = 0;
657
658 port = parent_phy->port;
659
660 for (i = 0; i < parent_ex->num_phys; i++) {
661 struct ex_phy *phy = &parent_ex->ex_phy[i];
662
663 if (phy->phy_state == PHY_VACANT ||
664 phy->phy_state == PHY_NOT_PRESENT)
665 continue;
666
667 if (SAS_ADDR(phy->attached_sas_addr) ==
668 SAS_ADDR(child->sas_addr)) {
669
670 child->min_linkrate = min(parent->min_linkrate,
671 phy->linkrate);
672 child->max_linkrate = max(parent->max_linkrate,
673 phy->linkrate);
674 child->pathways++;
675 sas_port_add_phy(port, phy->phy);
676 }
677 }
678 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
679 child->pathways = min(child->pathways, parent->pathways);
680}
681
682static struct domain_device *sas_ex_discover_end_dev(
683 struct domain_device *parent, int phy_id)
684{
685 struct expander_device *parent_ex = &parent->ex_dev;
686 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
687 struct domain_device *child = NULL;
688 struct sas_rphy *rphy;
689 int res;
690
691 if (phy->attached_sata_host || phy->attached_sata_ps)
692 return NULL;
693
Dan Williams735f7d22011-11-17 17:59:47 -0800694 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500695 if (!child)
696 return NULL;
697
Dan Williams735f7d22011-11-17 17:59:47 -0800698 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500699 child->parent = parent;
700 child->port = parent->port;
701 child->iproto = phy->attached_iproto;
702 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
703 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
James Bottomley024879e2006-11-15 18:03:07 -0600704 if (!phy->port) {
705 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
706 if (unlikely(!phy->port))
707 goto out_err;
708 if (unlikely(sas_port_add(phy->port) != 0)) {
709 sas_port_free(phy->port);
710 goto out_err;
711 }
712 }
James Bottomley2908d772006-08-29 09:22:51 -0500713 sas_ex_get_linkrate(parent, child, phy);
714
James Bottomleyb9142172007-07-22 13:15:55 -0500715#ifdef CONFIG_SCSI_SAS_ATA
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800716 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
James Bottomley2908d772006-08-29 09:22:51 -0500717 child->dev_type = SATA_DEV;
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800718 if (phy->attached_tproto & SAS_PROTOCOL_STP)
James Bottomley2908d772006-08-29 09:22:51 -0500719 child->tproto = phy->attached_tproto;
720 if (phy->attached_sata_dev)
721 child->tproto |= SATA_DEV;
722 res = sas_get_report_phy_sata(parent, phy_id,
723 &child->sata_dev.rps_resp);
724 if (res) {
725 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
726 "0x%x\n", SAS_ADDR(parent->sas_addr),
727 phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600728 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500729 }
730 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
731 sizeof(struct dev_to_host_fis));
James Bottomley1acce192006-08-22 12:39:19 -0500732
733 rphy = sas_end_device_alloc(phy->port);
James Bottomley528fd552006-10-16 10:57:05 -0500734 if (unlikely(!rphy))
735 goto out_free;
James Bottomley1acce192006-08-22 12:39:19 -0500736
James Bottomley2908d772006-08-29 09:22:51 -0500737 sas_init_dev(child);
James Bottomley1acce192006-08-22 12:39:19 -0500738
739 child->rphy = rphy;
740
Dan Williams87c83312011-11-17 17:59:51 -0800741 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley1acce192006-08-22 12:39:19 -0500742
James Bottomley2908d772006-08-29 09:22:51 -0500743 res = sas_discover_sata(child);
744 if (res) {
745 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
746 "%016llx:0x%x returned 0x%x\n",
747 SAS_ADDR(child->sas_addr),
748 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley1acce192006-08-22 12:39:19 -0500749 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500750 }
James Bottomleyb9142172007-07-22 13:15:55 -0500751 } else
752#endif
Darrick J. Wong5929faf2007-11-05 11:51:17 -0800753 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
James Bottomley2908d772006-08-29 09:22:51 -0500754 child->dev_type = SAS_END_DEV;
755 rphy = sas_end_device_alloc(phy->port);
756 /* FIXME: error handling */
James Bottomley024879e2006-11-15 18:03:07 -0600757 if (unlikely(!rphy))
758 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500759 child->tproto = phy->attached_tproto;
760 sas_init_dev(child);
761
762 child->rphy = rphy;
763 sas_fill_in_rphy(child, rphy);
764
James Bottomley9d720d82007-07-16 13:15:51 -0500765 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500766 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500767 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500768
769 res = sas_discover_end_dev(child);
770 if (res) {
771 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
772 "at %016llx:0x%x returned 0x%x\n",
773 SAS_ADDR(child->sas_addr),
774 SAS_ADDR(parent->sas_addr), phy_id, res);
James Bottomley024879e2006-11-15 18:03:07 -0600775 goto out_list_del;
James Bottomley2908d772006-08-29 09:22:51 -0500776 }
777 } else {
778 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
779 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
780 phy_id);
James Bottomleyb9142172007-07-22 13:15:55 -0500781 goto out_free;
James Bottomley2908d772006-08-29 09:22:51 -0500782 }
783
784 list_add_tail(&child->siblings, &parent_ex->children);
785 return child;
James Bottomley024879e2006-11-15 18:03:07 -0600786
787 out_list_del:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -0800788 sas_rphy_free(child->rphy);
789 child->rphy = NULL;
Dan Williams1a34c062011-09-21 22:05:34 -0700790
Dan Williams87c83312011-11-17 17:59:51 -0800791 list_del(&child->disco_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700792 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600793 list_del(&child->dev_list_node);
Dan Williams1a34c062011-09-21 22:05:34 -0700794 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley024879e2006-11-15 18:03:07 -0600795 out_free:
796 sas_port_delete(phy->port);
797 out_err:
798 phy->port = NULL;
Dan Williams735f7d22011-11-17 17:59:47 -0800799 sas_put_device(child);
James Bottomley024879e2006-11-15 18:03:07 -0600800 return NULL;
James Bottomley2908d772006-08-29 09:22:51 -0500801}
802
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800803/* See if this phy is part of a wide port */
804static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
805{
806 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
807 int i;
808
809 for (i = 0; i < parent->ex_dev.num_phys; i++) {
810 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
811
812 if (ephy == phy)
813 continue;
814
815 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
816 SAS_ADDR_SIZE) && ephy->port) {
817 sas_port_add_phy(ephy->port, phy->phy);
Tom Peng19252de2009-07-17 16:02:04 +0800818 phy->port = ephy->port;
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800819 phy->phy_state = PHY_DEVICE_DISCOVERED;
820 return 0;
821 }
822 }
823
824 return -ENODEV;
825}
826
James Bottomley2908d772006-08-29 09:22:51 -0500827static struct domain_device *sas_ex_discover_expander(
828 struct domain_device *parent, int phy_id)
829{
830 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
831 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
832 struct domain_device *child = NULL;
833 struct sas_rphy *rphy;
834 struct sas_expander_device *edev;
835 struct asd_sas_port *port;
836 int res;
837
838 if (phy->routing_attr == DIRECT_ROUTING) {
839 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
840 "allowed\n",
841 SAS_ADDR(parent->sas_addr), phy_id,
842 SAS_ADDR(phy->attached_sas_addr),
843 phy->attached_phy_id);
844 return NULL;
845 }
Dan Williams735f7d22011-11-17 17:59:47 -0800846 child = sas_alloc_device();
James Bottomley2908d772006-08-29 09:22:51 -0500847 if (!child)
848 return NULL;
849
850 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
851 /* FIXME: better error handling */
852 BUG_ON(sas_port_add(phy->port) != 0);
853
854
855 switch (phy->attached_dev_type) {
856 case EDGE_DEV:
857 rphy = sas_expander_alloc(phy->port,
858 SAS_EDGE_EXPANDER_DEVICE);
859 break;
860 case FANOUT_DEV:
861 rphy = sas_expander_alloc(phy->port,
862 SAS_FANOUT_EXPANDER_DEVICE);
863 break;
864 default:
865 rphy = NULL; /* shut gcc up */
866 BUG();
867 }
868 port = parent->port;
869 child->rphy = rphy;
870 edev = rphy_to_expander_device(rphy);
871 child->dev_type = phy->attached_dev_type;
Dan Williams735f7d22011-11-17 17:59:47 -0800872 kref_get(&parent->kref);
James Bottomley2908d772006-08-29 09:22:51 -0500873 child->parent = parent;
874 child->port = port;
875 child->iproto = phy->attached_iproto;
876 child->tproto = phy->attached_tproto;
877 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
878 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
879 sas_ex_get_linkrate(parent, child, phy);
880 edev->level = parent_ex->level + 1;
881 parent->port->disc.max_level = max(parent->port->disc.max_level,
882 edev->level);
883 sas_init_dev(child);
884 sas_fill_in_rphy(child, rphy);
885 sas_rphy_add(rphy);
886
James Bottomley9d720d82007-07-16 13:15:51 -0500887 spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500888 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
James Bottomley9d720d82007-07-16 13:15:51 -0500889 spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley2908d772006-08-29 09:22:51 -0500890
891 res = sas_discover_expander(child);
892 if (res) {
Luben Tuikov5911e962011-07-26 23:10:48 -0700893 spin_lock_irq(&parent->port->dev_list_lock);
894 list_del(&child->dev_list_node);
895 spin_unlock_irq(&parent->port->dev_list_lock);
Dan Williams735f7d22011-11-17 17:59:47 -0800896 sas_put_device(child);
James Bottomley2908d772006-08-29 09:22:51 -0500897 return NULL;
898 }
899 list_add_tail(&child->siblings, &parent->ex_dev.children);
900 return child;
901}
902
903static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
904{
905 struct expander_device *ex = &dev->ex_dev;
906 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
907 struct domain_device *child = NULL;
908 int res = 0;
909
910 /* Phy state */
James Bottomley88edf742006-09-06 17:36:13 -0500911 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
James Bottomleya01e70e2006-09-06 19:28:07 -0500912 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley2908d772006-08-29 09:22:51 -0500913 res = sas_ex_phy_discover(dev, phy_id);
914 if (res)
915 return res;
916 }
917
918 /* Parent and domain coherency */
919 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
920 SAS_ADDR(dev->port->sas_addr))) {
921 sas_add_parent_port(dev, phy_id);
922 return 0;
923 }
924 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
925 SAS_ADDR(dev->parent->sas_addr))) {
926 sas_add_parent_port(dev, phy_id);
927 if (ex_phy->routing_attr == TABLE_ROUTING)
928 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
929 return 0;
930 }
931
932 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
933 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
934
935 if (ex_phy->attached_dev_type == NO_DEVICE) {
936 if (ex_phy->routing_attr == DIRECT_ROUTING) {
937 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
938 sas_configure_routing(dev, ex_phy->attached_sas_addr);
939 }
940 return 0;
James Bottomley88edf742006-09-06 17:36:13 -0500941 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley2908d772006-08-29 09:22:51 -0500942 return 0;
943
944 if (ex_phy->attached_dev_type != SAS_END_DEV &&
945 ex_phy->attached_dev_type != FANOUT_DEV &&
946 ex_phy->attached_dev_type != EDGE_DEV) {
947 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
948 "phy 0x%x\n", ex_phy->attached_dev_type,
949 SAS_ADDR(dev->sas_addr),
950 phy_id);
951 return 0;
952 }
953
954 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
955 if (res) {
956 SAS_DPRINTK("configure routing for dev %016llx "
957 "reported 0x%x. Forgotten\n",
958 SAS_ADDR(ex_phy->attached_sas_addr), res);
959 sas_disable_routing(dev, ex_phy->attached_sas_addr);
960 return res;
961 }
962
Darrick J. Wong423f7cf2007-01-30 12:07:27 -0800963 res = sas_ex_join_wide_port(dev, phy_id);
964 if (!res) {
965 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
966 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
967 return res;
968 }
969
James Bottomley2908d772006-08-29 09:22:51 -0500970 switch (ex_phy->attached_dev_type) {
971 case SAS_END_DEV:
972 child = sas_ex_discover_end_dev(dev, phy_id);
973 break;
974 case FANOUT_DEV:
975 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
976 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
977 "attached to ex %016llx phy 0x%x\n",
978 SAS_ADDR(ex_phy->attached_sas_addr),
979 ex_phy->attached_phy_id,
980 SAS_ADDR(dev->sas_addr),
981 phy_id);
982 sas_ex_disable_phy(dev, phy_id);
983 break;
984 } else
985 memcpy(dev->port->disc.fanout_sas_addr,
986 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
987 /* fallthrough */
988 case EDGE_DEV:
989 child = sas_ex_discover_expander(dev, phy_id);
990 break;
991 default:
992 break;
993 }
994
995 if (child) {
996 int i;
997
998 for (i = 0; i < ex->num_phys; i++) {
999 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1000 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1001 continue;
Tom Peng19252de2009-07-17 16:02:04 +08001002 /*
1003 * Due to races, the phy might not get added to the
1004 * wide port, so we add the phy to the wide port here.
1005 */
James Bottomley2908d772006-08-29 09:22:51 -05001006 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
Tom Peng19252de2009-07-17 16:02:04 +08001007 SAS_ADDR(child->sas_addr)) {
James Bottomley2908d772006-08-29 09:22:51 -05001008 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
Tom Peng19252de2009-07-17 16:02:04 +08001009 res = sas_ex_join_wide_port(dev, i);
1010 if (!res)
1011 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1012 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1013
1014 }
James Bottomley2908d772006-08-29 09:22:51 -05001015 }
1016 }
1017
1018 return res;
1019}
1020
1021static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1022{
1023 struct expander_device *ex = &dev->ex_dev;
1024 int i;
1025
1026 for (i = 0; i < ex->num_phys; i++) {
1027 struct ex_phy *phy = &ex->ex_phy[i];
1028
1029 if (phy->phy_state == PHY_VACANT ||
1030 phy->phy_state == PHY_NOT_PRESENT)
1031 continue;
1032
1033 if ((phy->attached_dev_type == EDGE_DEV ||
1034 phy->attached_dev_type == FANOUT_DEV) &&
1035 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1036
1037 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1038
1039 return 1;
1040 }
1041 }
1042 return 0;
1043}
1044
1045static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1046{
1047 struct expander_device *ex = &dev->ex_dev;
1048 struct domain_device *child;
1049 u8 sub_addr[8] = {0, };
1050
1051 list_for_each_entry(child, &ex->children, siblings) {
1052 if (child->dev_type != EDGE_DEV &&
1053 child->dev_type != FANOUT_DEV)
1054 continue;
1055 if (sub_addr[0] == 0) {
1056 sas_find_sub_addr(child, sub_addr);
1057 continue;
1058 } else {
1059 u8 s2[8];
1060
1061 if (sas_find_sub_addr(child, s2) &&
1062 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1063
1064 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1065 "diverges from subtractive "
1066 "boundary %016llx\n",
1067 SAS_ADDR(dev->sas_addr),
1068 SAS_ADDR(child->sas_addr),
1069 SAS_ADDR(s2),
1070 SAS_ADDR(sub_addr));
1071
1072 sas_ex_disable_port(child, s2);
1073 }
1074 }
1075 }
1076 return 0;
1077}
1078/**
1079 * sas_ex_discover_devices -- discover devices attached to this expander
1080 * dev: pointer to the expander domain device
1081 * single: if you want to do a single phy, else set to -1;
1082 *
1083 * Configure this expander for use with its devices and register the
1084 * devices of this expander.
1085 */
1086static int sas_ex_discover_devices(struct domain_device *dev, int single)
1087{
1088 struct expander_device *ex = &dev->ex_dev;
1089 int i = 0, end = ex->num_phys;
1090 int res = 0;
1091
1092 if (0 <= single && single < end) {
1093 i = single;
1094 end = i+1;
1095 }
1096
1097 for ( ; i < end; i++) {
1098 struct ex_phy *ex_phy = &ex->ex_phy[i];
1099
1100 if (ex_phy->phy_state == PHY_VACANT ||
1101 ex_phy->phy_state == PHY_NOT_PRESENT ||
1102 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1103 continue;
1104
1105 switch (ex_phy->linkrate) {
James Bottomley88edf742006-09-06 17:36:13 -05001106 case SAS_PHY_DISABLED:
1107 case SAS_PHY_RESET_PROBLEM:
1108 case SAS_SATA_PORT_SELECTOR:
James Bottomley2908d772006-08-29 09:22:51 -05001109 continue;
1110 default:
1111 res = sas_ex_discover_dev(dev, i);
1112 if (res)
1113 break;
1114 continue;
1115 }
1116 }
1117
1118 if (!res)
1119 sas_check_level_subtractive_boundary(dev);
1120
1121 return res;
1122}
1123
1124static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1125{
1126 struct expander_device *ex = &dev->ex_dev;
1127 int i;
1128 u8 *sub_sas_addr = NULL;
1129
1130 if (dev->dev_type != EDGE_DEV)
1131 return 0;
1132
1133 for (i = 0; i < ex->num_phys; i++) {
1134 struct ex_phy *phy = &ex->ex_phy[i];
1135
1136 if (phy->phy_state == PHY_VACANT ||
1137 phy->phy_state == PHY_NOT_PRESENT)
1138 continue;
1139
1140 if ((phy->attached_dev_type == FANOUT_DEV ||
1141 phy->attached_dev_type == EDGE_DEV) &&
1142 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1143
1144 if (!sub_sas_addr)
1145 sub_sas_addr = &phy->attached_sas_addr[0];
1146 else if (SAS_ADDR(sub_sas_addr) !=
1147 SAS_ADDR(phy->attached_sas_addr)) {
1148
1149 SAS_DPRINTK("ex %016llx phy 0x%x "
1150 "diverges(%016llx) on subtractive "
1151 "boundary(%016llx). Disabled\n",
1152 SAS_ADDR(dev->sas_addr), i,
1153 SAS_ADDR(phy->attached_sas_addr),
1154 SAS_ADDR(sub_sas_addr));
1155 sas_ex_disable_phy(dev, i);
1156 }
1157 }
1158 }
1159 return 0;
1160}
1161
1162static void sas_print_parent_topology_bug(struct domain_device *child,
1163 struct ex_phy *parent_phy,
1164 struct ex_phy *child_phy)
1165{
1166 static const char ra_char[] = {
1167 [DIRECT_ROUTING] = 'D',
1168 [SUBTRACTIVE_ROUTING] = 'S',
1169 [TABLE_ROUTING] = 'T',
1170 };
1171 static const char *ex_type[] = {
1172 [EDGE_DEV] = "edge",
1173 [FANOUT_DEV] = "fanout",
1174 };
1175 struct domain_device *parent = child->parent;
1176
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001177 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1178 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
James Bottomley2908d772006-08-29 09:22:51 -05001179
1180 ex_type[parent->dev_type],
1181 SAS_ADDR(parent->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001182 parent->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001183 parent_phy->phy_id,
1184
1185 ex_type[child->dev_type],
1186 SAS_ADDR(child->sas_addr),
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001187 child->ex_dev.t2t_supp,
James Bottomley2908d772006-08-29 09:22:51 -05001188 child_phy->phy_id,
1189
1190 ra_char[parent_phy->routing_attr],
1191 ra_char[child_phy->routing_attr]);
1192}
1193
1194static int sas_check_eeds(struct domain_device *child,
1195 struct ex_phy *parent_phy,
1196 struct ex_phy *child_phy)
1197{
1198 int res = 0;
1199 struct domain_device *parent = child->parent;
1200
1201 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1202 res = -ENODEV;
1203 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1204 "phy S:0x%x, while there is a fanout ex %016llx\n",
1205 SAS_ADDR(parent->sas_addr),
1206 parent_phy->phy_id,
1207 SAS_ADDR(child->sas_addr),
1208 child_phy->phy_id,
1209 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1210 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1211 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1212 SAS_ADDR_SIZE);
1213 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1214 SAS_ADDR_SIZE);
1215 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1216 SAS_ADDR(parent->sas_addr)) ||
1217 (SAS_ADDR(parent->port->disc.eeds_a) ==
1218 SAS_ADDR(child->sas_addr)))
1219 &&
1220 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1221 SAS_ADDR(parent->sas_addr)) ||
1222 (SAS_ADDR(parent->port->disc.eeds_b) ==
1223 SAS_ADDR(child->sas_addr))))
1224 ;
1225 else {
1226 res = -ENODEV;
1227 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1228 "phy 0x%x link forms a third EEDS!\n",
1229 SAS_ADDR(parent->sas_addr),
1230 parent_phy->phy_id,
1231 SAS_ADDR(child->sas_addr),
1232 child_phy->phy_id);
1233 }
1234
1235 return res;
1236}
1237
1238/* Here we spill over 80 columns. It is intentional.
1239 */
1240static int sas_check_parent_topology(struct domain_device *child)
1241{
1242 struct expander_device *child_ex = &child->ex_dev;
1243 struct expander_device *parent_ex;
1244 int i;
1245 int res = 0;
1246
1247 if (!child->parent)
1248 return 0;
1249
1250 if (child->parent->dev_type != EDGE_DEV &&
1251 child->parent->dev_type != FANOUT_DEV)
1252 return 0;
1253
1254 parent_ex = &child->parent->ex_dev;
1255
1256 for (i = 0; i < parent_ex->num_phys; i++) {
1257 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1258 struct ex_phy *child_phy;
1259
1260 if (parent_phy->phy_state == PHY_VACANT ||
1261 parent_phy->phy_state == PHY_NOT_PRESENT)
1262 continue;
1263
1264 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1265 continue;
1266
1267 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1268
1269 switch (child->parent->dev_type) {
1270 case EDGE_DEV:
1271 if (child->dev_type == FANOUT_DEV) {
1272 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1273 child_phy->routing_attr != TABLE_ROUTING) {
1274 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1275 res = -ENODEV;
1276 }
1277 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1278 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1279 res = sas_check_eeds(child, parent_phy, child_phy);
1280 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1281 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1282 res = -ENODEV;
1283 }
Luben Tuikovffaac8f2011-09-22 09:41:36 -07001284 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1285 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1286 (child_phy->routing_attr == TABLE_ROUTING &&
1287 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1288 /* All good */;
1289 } else {
1290 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1291 res = -ENODEV;
1292 }
James Bottomley2908d772006-08-29 09:22:51 -05001293 }
1294 break;
1295 case FANOUT_DEV:
1296 if (parent_phy->routing_attr != TABLE_ROUTING ||
1297 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1298 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1299 res = -ENODEV;
1300 }
1301 break;
1302 default:
1303 break;
1304 }
1305 }
1306
1307 return res;
1308}
1309
1310#define RRI_REQ_SIZE 16
1311#define RRI_RESP_SIZE 44
1312
1313static int sas_configure_present(struct domain_device *dev, int phy_id,
1314 u8 *sas_addr, int *index, int *present)
1315{
1316 int i, res = 0;
1317 struct expander_device *ex = &dev->ex_dev;
1318 struct ex_phy *phy = &ex->ex_phy[phy_id];
1319 u8 *rri_req;
1320 u8 *rri_resp;
1321
1322 *present = 0;
1323 *index = 0;
1324
1325 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1326 if (!rri_req)
1327 return -ENOMEM;
1328
1329 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1330 if (!rri_resp) {
1331 kfree(rri_req);
1332 return -ENOMEM;
1333 }
1334
1335 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1336 rri_req[9] = phy_id;
1337
1338 for (i = 0; i < ex->max_route_indexes ; i++) {
1339 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1340 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1341 RRI_RESP_SIZE);
1342 if (res)
1343 goto out;
1344 res = rri_resp[2];
1345 if (res == SMP_RESP_NO_INDEX) {
1346 SAS_DPRINTK("overflow of indexes: dev %016llx "
1347 "phy 0x%x index 0x%x\n",
1348 SAS_ADDR(dev->sas_addr), phy_id, i);
1349 goto out;
1350 } else if (res != SMP_RESP_FUNC_ACC) {
1351 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001352 "result 0x%x\n", __func__,
James Bottomley2908d772006-08-29 09:22:51 -05001353 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1354 goto out;
1355 }
1356 if (SAS_ADDR(sas_addr) != 0) {
1357 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1358 *index = i;
1359 if ((rri_resp[12] & 0x80) == 0x80)
1360 *present = 0;
1361 else
1362 *present = 1;
1363 goto out;
1364 } else if (SAS_ADDR(rri_resp+16) == 0) {
1365 *index = i;
1366 *present = 0;
1367 goto out;
1368 }
1369 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1370 phy->last_da_index < i) {
1371 phy->last_da_index = i;
1372 *index = i;
1373 *present = 0;
1374 goto out;
1375 }
1376 }
1377 res = -1;
1378out:
1379 kfree(rri_req);
1380 kfree(rri_resp);
1381 return res;
1382}
1383
1384#define CRI_REQ_SIZE 44
1385#define CRI_RESP_SIZE 8
1386
1387static int sas_configure_set(struct domain_device *dev, int phy_id,
1388 u8 *sas_addr, int index, int include)
1389{
1390 int res;
1391 u8 *cri_req;
1392 u8 *cri_resp;
1393
1394 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1395 if (!cri_req)
1396 return -ENOMEM;
1397
1398 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1399 if (!cri_resp) {
1400 kfree(cri_req);
1401 return -ENOMEM;
1402 }
1403
1404 cri_req[1] = SMP_CONF_ROUTE_INFO;
1405 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1406 cri_req[9] = phy_id;
1407 if (SAS_ADDR(sas_addr) == 0 || !include)
1408 cri_req[12] |= 0x80;
1409 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1410
1411 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1412 CRI_RESP_SIZE);
1413 if (res)
1414 goto out;
1415 res = cri_resp[2];
1416 if (res == SMP_RESP_NO_INDEX) {
1417 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1418 "index 0x%x\n",
1419 SAS_ADDR(dev->sas_addr), phy_id, index);
1420 }
1421out:
1422 kfree(cri_req);
1423 kfree(cri_resp);
1424 return res;
1425}
1426
1427static int sas_configure_phy(struct domain_device *dev, int phy_id,
1428 u8 *sas_addr, int include)
1429{
1430 int index;
1431 int present;
1432 int res;
1433
1434 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1435 if (res)
1436 return res;
1437 if (include ^ present)
1438 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1439
1440 return res;
1441}
1442
1443/**
1444 * sas_configure_parent -- configure routing table of parent
1445 * parent: parent expander
1446 * child: child expander
1447 * sas_addr: SAS port identifier of device directly attached to child
1448 */
1449static int sas_configure_parent(struct domain_device *parent,
1450 struct domain_device *child,
1451 u8 *sas_addr, int include)
1452{
1453 struct expander_device *ex_parent = &parent->ex_dev;
1454 int res = 0;
1455 int i;
1456
1457 if (parent->parent) {
1458 res = sas_configure_parent(parent->parent, parent, sas_addr,
1459 include);
1460 if (res)
1461 return res;
1462 }
1463
1464 if (ex_parent->conf_route_table == 0) {
1465 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1466 SAS_ADDR(parent->sas_addr));
1467 return 0;
1468 }
1469
1470 for (i = 0; i < ex_parent->num_phys; i++) {
1471 struct ex_phy *phy = &ex_parent->ex_phy[i];
1472
1473 if ((phy->routing_attr == TABLE_ROUTING) &&
1474 (SAS_ADDR(phy->attached_sas_addr) ==
1475 SAS_ADDR(child->sas_addr))) {
1476 res = sas_configure_phy(parent, i, sas_addr, include);
1477 if (res)
1478 return res;
1479 }
1480 }
1481
1482 return res;
1483}
1484
1485/**
1486 * sas_configure_routing -- configure routing
1487 * dev: expander device
1488 * sas_addr: port identifier of device directly attached to the expander device
1489 */
1490static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1491{
1492 if (dev->parent)
1493 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1494 return 0;
1495}
1496
1497static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1498{
1499 if (dev->parent)
1500 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1501 return 0;
1502}
1503
James Bottomley2908d772006-08-29 09:22:51 -05001504/**
1505 * sas_discover_expander -- expander discovery
1506 * @ex: pointer to expander domain device
1507 *
1508 * See comment in sas_discover_sata().
1509 */
1510static int sas_discover_expander(struct domain_device *dev)
1511{
1512 int res;
1513
1514 res = sas_notify_lldd_dev_found(dev);
1515 if (res)
1516 return res;
1517
1518 res = sas_ex_general(dev);
1519 if (res)
1520 goto out_err;
1521 res = sas_ex_manuf_info(dev);
1522 if (res)
1523 goto out_err;
1524
1525 res = sas_expander_discover(dev);
1526 if (res) {
1527 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1528 SAS_ADDR(dev->sas_addr), res);
1529 goto out_err;
1530 }
1531
1532 sas_check_ex_subtractive_boundary(dev);
1533 res = sas_check_parent_topology(dev);
1534 if (res)
1535 goto out_err;
1536 return 0;
1537out_err:
1538 sas_notify_lldd_dev_gone(dev);
1539 return res;
1540}
1541
1542static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1543{
1544 int res = 0;
1545 struct domain_device *dev;
1546
1547 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1548 if (dev->dev_type == EDGE_DEV ||
1549 dev->dev_type == FANOUT_DEV) {
1550 struct sas_expander_device *ex =
1551 rphy_to_expander_device(dev->rphy);
1552
1553 if (level == ex->level)
1554 res = sas_ex_discover_devices(dev, -1);
1555 else if (level > 0)
1556 res = sas_ex_discover_devices(port->port_dev, -1);
1557
1558 }
1559 }
1560
1561 return res;
1562}
1563
1564static int sas_ex_bfs_disc(struct asd_sas_port *port)
1565{
1566 int res;
1567 int level;
1568
1569 do {
1570 level = port->disc.max_level;
1571 res = sas_ex_level_discovery(port, level);
1572 mb();
1573 } while (level < port->disc.max_level);
1574
1575 return res;
1576}
1577
1578int sas_discover_root_expander(struct domain_device *dev)
1579{
1580 int res;
1581 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1582
Darrick J. Wongbf451202007-01-11 14:14:52 -08001583 res = sas_rphy_add(dev->rphy);
1584 if (res)
1585 goto out_err;
James Bottomley2908d772006-08-29 09:22:51 -05001586
1587 ex->level = dev->port->disc.max_level; /* 0 */
1588 res = sas_discover_expander(dev);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001589 if (res)
1590 goto out_err2;
James Bottomley2908d772006-08-29 09:22:51 -05001591
Darrick J. Wongbf451202007-01-11 14:14:52 -08001592 sas_ex_bfs_disc(dev->port);
1593
1594 return res;
1595
1596out_err2:
Darrick J. Wong6f63caa2007-01-26 14:08:43 -08001597 sas_rphy_remove(dev->rphy);
Darrick J. Wongbf451202007-01-11 14:14:52 -08001598out_err:
James Bottomley2908d772006-08-29 09:22:51 -05001599 return res;
1600}
1601
1602/* ---------- Domain revalidation ---------- */
1603
1604static int sas_get_phy_discover(struct domain_device *dev,
1605 int phy_id, struct smp_resp *disc_resp)
1606{
1607 int res;
1608 u8 *disc_req;
1609
1610 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1611 if (!disc_req)
1612 return -ENOMEM;
1613
1614 disc_req[1] = SMP_DISCOVER;
1615 disc_req[9] = phy_id;
1616
1617 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1618 disc_resp, DISCOVER_RESP_SIZE);
1619 if (res)
1620 goto out;
1621 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1622 res = disc_resp->result;
1623 goto out;
1624 }
1625out:
1626 kfree(disc_req);
1627 return res;
1628}
1629
1630static int sas_get_phy_change_count(struct domain_device *dev,
1631 int phy_id, int *pcc)
1632{
1633 int res;
1634 struct smp_resp *disc_resp;
1635
1636 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1637 if (!disc_resp)
1638 return -ENOMEM;
1639
1640 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1641 if (!res)
1642 *pcc = disc_resp->disc.change_count;
1643
1644 kfree(disc_resp);
1645 return res;
1646}
1647
1648static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1649 int phy_id, u8 *attached_sas_addr)
1650{
1651 int res;
1652 struct smp_resp *disc_resp;
1653 struct discover_resp *dr;
1654
1655 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1656 if (!disc_resp)
1657 return -ENOMEM;
1658 dr = &disc_resp->disc;
1659
1660 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1661 if (!res) {
1662 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1663 if (dr->attached_dev_type == 0)
1664 memset(attached_sas_addr, 0, 8);
1665 }
1666 kfree(disc_resp);
1667 return res;
1668}
1669
1670static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
Tom Peng19252de2009-07-17 16:02:04 +08001671 int from_phy, bool update)
James Bottomley2908d772006-08-29 09:22:51 -05001672{
1673 struct expander_device *ex = &dev->ex_dev;
1674 int res = 0;
1675 int i;
1676
1677 for (i = from_phy; i < ex->num_phys; i++) {
1678 int phy_change_count = 0;
1679
1680 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1681 if (res)
1682 goto out;
1683 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
Tom Peng19252de2009-07-17 16:02:04 +08001684 if (update)
1685 ex->ex_phy[i].phy_change_count =
1686 phy_change_count;
James Bottomley2908d772006-08-29 09:22:51 -05001687 *phy_id = i;
1688 return 0;
1689 }
1690 }
1691out:
1692 return res;
1693}
1694
1695static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1696{
1697 int res;
1698 u8 *rg_req;
1699 struct smp_resp *rg_resp;
1700
1701 rg_req = alloc_smp_req(RG_REQ_SIZE);
1702 if (!rg_req)
1703 return -ENOMEM;
1704
1705 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1706 if (!rg_resp) {
1707 kfree(rg_req);
1708 return -ENOMEM;
1709 }
1710
1711 rg_req[1] = SMP_REPORT_GENERAL;
1712
1713 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1714 RG_RESP_SIZE);
1715 if (res)
1716 goto out;
1717 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1718 res = rg_resp->result;
1719 goto out;
1720 }
1721
1722 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1723out:
1724 kfree(rg_resp);
1725 kfree(rg_req);
1726 return res;
1727}
Tom Peng19252de2009-07-17 16:02:04 +08001728/**
1729 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1730 * @dev:domain device to be detect.
1731 * @src_dev: the device which originated BROADCAST(CHANGE).
1732 *
1733 * Add self-configuration expander suport. Suppose two expander cascading,
1734 * when the first level expander is self-configuring, hotplug the disks in
1735 * second level expander, BROADCAST(CHANGE) will not only be originated
1736 * in the second level expander, but also be originated in the first level
1737 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1738 * expander changed count in two level expanders will all increment at least
1739 * once, but the phy which chang count has changed is the source device which
1740 * we concerned.
1741 */
James Bottomley2908d772006-08-29 09:22:51 -05001742
1743static int sas_find_bcast_dev(struct domain_device *dev,
1744 struct domain_device **src_dev)
1745{
1746 struct expander_device *ex = &dev->ex_dev;
1747 int ex_change_count = -1;
Tom Peng19252de2009-07-17 16:02:04 +08001748 int phy_id = -1;
James Bottomley2908d772006-08-29 09:22:51 -05001749 int res;
Tom Peng19252de2009-07-17 16:02:04 +08001750 struct domain_device *ch;
James Bottomley2908d772006-08-29 09:22:51 -05001751
1752 res = sas_get_ex_change_count(dev, &ex_change_count);
1753 if (res)
1754 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001755 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1756 /* Just detect if this expander phys phy change count changed,
1757 * in order to determine if this expander originate BROADCAST,
1758 * and do not update phy change count field in our structure.
1759 */
1760 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1761 if (phy_id != -1) {
1762 *src_dev = dev;
1763 ex->ex_change_count = ex_change_count;
1764 SAS_DPRINTK("Expander phy change count has changed\n");
1765 return res;
1766 } else
1767 SAS_DPRINTK("Expander phys DID NOT change\n");
1768 }
1769 list_for_each_entry(ch, &ex->children, siblings) {
1770 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1771 res = sas_find_bcast_dev(ch, src_dev);
Mark Salyzyn24926da2011-09-01 06:11:17 -07001772 if (*src_dev)
Tom Peng19252de2009-07-17 16:02:04 +08001773 return res;
James Bottomley2908d772006-08-29 09:22:51 -05001774 }
1775 }
1776out:
1777 return res;
1778}
1779
Dan Williams1a34c062011-09-21 22:05:34 -07001780static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
James Bottomley2908d772006-08-29 09:22:51 -05001781{
1782 struct expander_device *ex = &dev->ex_dev;
1783 struct domain_device *child, *n;
1784
1785 list_for_each_entry_safe(child, n, &ex->children, siblings) {
Dan Williamse1399422012-01-07 08:52:39 +00001786 set_bit(SAS_DEV_GONE, &child->state);
James Bottomley2908d772006-08-29 09:22:51 -05001787 if (child->dev_type == EDGE_DEV ||
1788 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001789 sas_unregister_ex_tree(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001790 else
Dan Williams1a34c062011-09-21 22:05:34 -07001791 sas_unregister_dev(port, child);
James Bottomley2908d772006-08-29 09:22:51 -05001792 }
Dan Williams1a34c062011-09-21 22:05:34 -07001793 sas_unregister_dev(port, dev);
James Bottomley2908d772006-08-29 09:22:51 -05001794}
1795
1796static void sas_unregister_devs_sas_addr(struct domain_device *parent,
Tom Peng19252de2009-07-17 16:02:04 +08001797 int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001798{
1799 struct expander_device *ex_dev = &parent->ex_dev;
1800 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1801 struct domain_device *child, *n;
Tom Peng19252de2009-07-17 16:02:04 +08001802 if (last) {
1803 list_for_each_entry_safe(child, n,
1804 &ex_dev->children, siblings) {
1805 if (SAS_ADDR(child->sas_addr) ==
1806 SAS_ADDR(phy->attached_sas_addr)) {
Dan Williamse1399422012-01-07 08:52:39 +00001807 set_bit(SAS_DEV_GONE, &child->state);
Tom Peng19252de2009-07-17 16:02:04 +08001808 if (child->dev_type == EDGE_DEV ||
1809 child->dev_type == FANOUT_DEV)
Dan Williams1a34c062011-09-21 22:05:34 -07001810 sas_unregister_ex_tree(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001811 else
Dan Williams1a34c062011-09-21 22:05:34 -07001812 sas_unregister_dev(parent->port, child);
Tom Peng19252de2009-07-17 16:02:04 +08001813 break;
1814 }
James Bottomley2908d772006-08-29 09:22:51 -05001815 }
Dan Williamse1399422012-01-07 08:52:39 +00001816 set_bit(SAS_DEV_GONE, &parent->state);
Tom Peng19252de2009-07-17 16:02:04 +08001817 sas_disable_routing(parent, phy->attached_sas_addr);
James Bottomley2908d772006-08-29 09:22:51 -05001818 }
James Bottomley2908d772006-08-29 09:22:51 -05001819 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
Mark Salyzyna73914c2011-09-22 08:32:23 -07001820 if (phy->port) {
1821 sas_port_delete_phy(phy->port, phy->phy);
1822 if (phy->port->num_phys == 0)
1823 sas_port_delete(phy->port);
1824 phy->port = NULL;
1825 }
James Bottomley2908d772006-08-29 09:22:51 -05001826}
1827
1828static int sas_discover_bfs_by_root_level(struct domain_device *root,
1829 const int level)
1830{
1831 struct expander_device *ex_root = &root->ex_dev;
1832 struct domain_device *child;
1833 int res = 0;
1834
1835 list_for_each_entry(child, &ex_root->children, siblings) {
1836 if (child->dev_type == EDGE_DEV ||
1837 child->dev_type == FANOUT_DEV) {
1838 struct sas_expander_device *ex =
1839 rphy_to_expander_device(child->rphy);
1840
1841 if (level > ex->level)
1842 res = sas_discover_bfs_by_root_level(child,
1843 level);
1844 else if (level == ex->level)
1845 res = sas_ex_discover_devices(child, -1);
1846 }
1847 }
1848 return res;
1849}
1850
1851static int sas_discover_bfs_by_root(struct domain_device *dev)
1852{
1853 int res;
1854 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1855 int level = ex->level+1;
1856
1857 res = sas_ex_discover_devices(dev, -1);
1858 if (res)
1859 goto out;
1860 do {
1861 res = sas_discover_bfs_by_root_level(dev, level);
1862 mb();
1863 level += 1;
1864 } while (level <= dev->port->disc.max_level);
1865out:
1866 return res;
1867}
1868
1869static int sas_discover_new(struct domain_device *dev, int phy_id)
1870{
1871 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1872 struct domain_device *child;
Tom Peng19252de2009-07-17 16:02:04 +08001873 bool found = false;
1874 int res, i;
James Bottomley2908d772006-08-29 09:22:51 -05001875
1876 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1877 SAS_ADDR(dev->sas_addr), phy_id);
1878 res = sas_ex_phy_discover(dev, phy_id);
1879 if (res)
1880 goto out;
Tom Peng19252de2009-07-17 16:02:04 +08001881 /* to support the wide port inserted */
1882 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1883 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1884 if (i == phy_id)
1885 continue;
1886 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1887 SAS_ADDR(ex_phy->attached_sas_addr)) {
1888 found = true;
1889 break;
1890 }
1891 }
1892 if (found) {
1893 sas_ex_join_wide_port(dev, phy_id);
1894 return 0;
1895 }
James Bottomley2908d772006-08-29 09:22:51 -05001896 res = sas_ex_discover_devices(dev, phy_id);
Tom Peng19252de2009-07-17 16:02:04 +08001897 if (!res)
James Bottomley2908d772006-08-29 09:22:51 -05001898 goto out;
1899 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1900 if (SAS_ADDR(child->sas_addr) ==
1901 SAS_ADDR(ex_phy->attached_sas_addr)) {
1902 if (child->dev_type == EDGE_DEV ||
1903 child->dev_type == FANOUT_DEV)
1904 res = sas_discover_bfs_by_root(child);
1905 break;
1906 }
1907 }
1908out:
1909 return res;
1910}
1911
Tom Peng19252de2009-07-17 16:02:04 +08001912static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
James Bottomley2908d772006-08-29 09:22:51 -05001913{
1914 struct expander_device *ex = &dev->ex_dev;
1915 struct ex_phy *phy = &ex->ex_phy[phy_id];
1916 u8 attached_sas_addr[8];
1917 int res;
1918
1919 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1920 switch (res) {
1921 case SMP_RESP_NO_PHY:
1922 phy->phy_state = PHY_NOT_PRESENT;
Tom Peng19252de2009-07-17 16:02:04 +08001923 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001924 goto out; break;
1925 case SMP_RESP_PHY_VACANT:
1926 phy->phy_state = PHY_VACANT;
Tom Peng19252de2009-07-17 16:02:04 +08001927 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001928 goto out; break;
1929 case SMP_RESP_FUNC_ACC:
1930 break;
1931 }
1932
1933 if (SAS_ADDR(attached_sas_addr) == 0) {
1934 phy->phy_state = PHY_EMPTY;
Tom Peng19252de2009-07-17 16:02:04 +08001935 sas_unregister_devs_sas_addr(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001936 } else if (SAS_ADDR(attached_sas_addr) ==
1937 SAS_ADDR(phy->attached_sas_addr)) {
1938 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1939 SAS_ADDR(dev->sas_addr), phy_id);
James Bottomleya01e70e2006-09-06 19:28:07 -05001940 sas_ex_phy_discover(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001941 } else
1942 res = sas_discover_new(dev, phy_id);
1943out:
1944 return res;
1945}
1946
Tom Peng19252de2009-07-17 16:02:04 +08001947/**
1948 * sas_rediscover - revalidate the domain.
1949 * @dev:domain device to be detect.
1950 * @phy_id: the phy id will be detected.
1951 *
1952 * NOTE: this process _must_ quit (return) as soon as any connection
1953 * errors are encountered. Connection recovery is done elsewhere.
1954 * Discover process only interrogates devices in order to discover the
1955 * domain.For plugging out, we un-register the device only when it is
1956 * the last phy in the port, for other phys in this port, we just delete it
1957 * from the port.For inserting, we do discovery when it is the
1958 * first phy,for other phys in this port, we add it to the port to
1959 * forming the wide-port.
1960 */
James Bottomley2908d772006-08-29 09:22:51 -05001961static int sas_rediscover(struct domain_device *dev, const int phy_id)
1962{
1963 struct expander_device *ex = &dev->ex_dev;
1964 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1965 int res = 0;
1966 int i;
Tom Peng19252de2009-07-17 16:02:04 +08001967 bool last = true; /* is this the last phy of the port */
James Bottomley2908d772006-08-29 09:22:51 -05001968
1969 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1970 SAS_ADDR(dev->sas_addr), phy_id);
1971
1972 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1973 for (i = 0; i < ex->num_phys; i++) {
1974 struct ex_phy *phy = &ex->ex_phy[i];
1975
1976 if (i == phy_id)
1977 continue;
1978 if (SAS_ADDR(phy->attached_sas_addr) ==
1979 SAS_ADDR(changed_phy->attached_sas_addr)) {
1980 SAS_DPRINTK("phy%d part of wide port with "
1981 "phy%d\n", phy_id, i);
Tom Peng19252de2009-07-17 16:02:04 +08001982 last = false;
1983 break;
James Bottomley2908d772006-08-29 09:22:51 -05001984 }
1985 }
Tom Peng19252de2009-07-17 16:02:04 +08001986 res = sas_rediscover_dev(dev, phy_id, last);
James Bottomley2908d772006-08-29 09:22:51 -05001987 } else
1988 res = sas_discover_new(dev, phy_id);
James Bottomley2908d772006-08-29 09:22:51 -05001989 return res;
1990}
1991
1992/**
1993 * sas_revalidate_domain -- revalidate the domain
1994 * @port: port to the domain of interest
1995 *
1996 * NOTE: this process _must_ quit (return) as soon as any connection
1997 * errors are encountered. Connection recovery is done elsewhere.
1998 * Discover process only interrogates devices in order to discover the
1999 * domain.
2000 */
2001int sas_ex_revalidate_domain(struct domain_device *port_dev)
2002{
2003 int res;
2004 struct domain_device *dev = NULL;
2005
2006 res = sas_find_bcast_dev(port_dev, &dev);
2007 if (res)
2008 goto out;
2009 if (dev) {
2010 struct expander_device *ex = &dev->ex_dev;
2011 int i = 0, phy_id;
2012
2013 do {
2014 phy_id = -1;
Tom Peng19252de2009-07-17 16:02:04 +08002015 res = sas_find_bcast_phy(dev, &phy_id, i, true);
James Bottomley2908d772006-08-29 09:22:51 -05002016 if (phy_id == -1)
2017 break;
2018 res = sas_rediscover(dev, phy_id);
2019 i = phy_id + 1;
2020 } while (i < ex->num_phys);
2021 }
2022out:
2023 return res;
2024}
2025
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002026int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2027 struct request *req)
2028{
2029 struct domain_device *dev;
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002030 int ret, type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002031 struct request *rsp = req->next_rq;
2032
2033 if (!rsp) {
2034 printk("%s: space for a smp response is missing\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002035 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002036 return -EINVAL;
2037 }
2038
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002039 /* no rphy means no smp target support (ie aic94xx host) */
James Bottomleyb98e66f2007-12-28 16:35:17 -06002040 if (!rphy)
2041 return sas_smp_host_handler(shost, req, rsp);
2042
Darrick J. Wong2cd614c2007-07-24 09:33:49 -07002043 type = rphy->identify.device_type;
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002044
2045 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2046 type != SAS_FANOUT_EXPANDER_DEVICE) {
2047 printk("%s: can we send a smp request to a device?\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002048 __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002049 return -EINVAL;
2050 }
2051
2052 dev = sas_find_dev_by_rphy(rphy);
2053 if (!dev) {
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002054 printk("%s: fail to find a domain_device?\n", __func__);
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002055 return -EINVAL;
2056 }
2057
2058 /* do we need to support multiple segments? */
2059 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2060 printk("%s: multiple segments req %u %u, rsp %u %u\n",
Tejun Heob0790412009-05-07 22:24:42 +09002061 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2062 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002063 return -EINVAL;
2064 }
2065
Tejun Heob0790412009-05-07 22:24:42 +09002066 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2067 bio_data(rsp->bio), blk_rq_bytes(rsp));
James Bottomley2d4b63e2007-12-29 11:49:53 -06002068 if (ret > 0) {
2069 /* positive number is the untransferred residual */
Tejun Heoc3a4d782009-05-07 22:24:37 +09002070 rsp->resid_len = ret;
Tejun Heo5f49f632009-05-19 18:33:05 +09002071 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002072 ret = 0;
Tejun Heo5f49f632009-05-19 18:33:05 +09002073 } else if (ret == 0) {
2074 rsp->resid_len = 0;
2075 req->resid_len = 0;
James Bottomley2d4b63e2007-12-29 11:49:53 -06002076 }
FUJITA Tomonoriba1fc172007-07-09 12:52:08 +09002077
2078 return ret;
2079}