]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/libsas/sas_expander.c
[SCSI] libsas: fix sas_find_local_phy(), take phy references
[karo-tx-linux.git] / drivers / scsi / libsas / sas_expander.c
1 /*
2  * Serial Attached SCSI (SAS) Expander discovery and configuration
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <linux/scatterlist.h>
26 #include <linux/blkdev.h>
27 #include <linux/slab.h>
28
29 #include "sas_internal.h"
30
31 #include <scsi/sas_ata.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_sas.h>
34 #include "../scsi_sas_internal.h"
35
36 static int sas_discover_expander(struct domain_device *dev);
37 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38 static int sas_configure_phy(struct domain_device *dev, int phy_id,
39                              u8 *sas_addr, int include);
40 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr);
41
42 /* ---------- SMP task management ---------- */
43
44 static 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
57 static 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
67 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68                             void *resp, int resp_size)
69 {
70         int res, retry;
71         struct sas_task *task = NULL;
72         struct sas_internal *i =
73                 to_sas_internal(dev->port->ha->core.shost->transportt);
74
75         mutex_lock(&dev->ex_dev.cmd_mutex);
76         for (retry = 0; retry < 3; retry++) {
77                 if (test_bit(SAS_DEV_GONE, &dev->state)) {
78                         res = -ECOMM;
79                         break;
80                 }
81
82                 task = sas_alloc_task(GFP_KERNEL);
83                 if (!task) {
84                         res = -ENOMEM;
85                         break;
86                 }
87                 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);
91
92                 task->task_done = smp_task_done;
93
94                 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);
98
99                 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
100
101                 if (res) {
102                         del_timer(&task->timer);
103                         SAS_DPRINTK("executing SMP task failed:%d\n", res);
104                         break;
105                 }
106
107                 wait_for_completion(&task->completion);
108                 res = -ECOMM;
109                 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");
114                                 break;
115                         }
116                 }
117                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
118                     task->task_status.stat == SAM_STAT_GOOD) {
119                         res = 0;
120                         break;
121                 }
122                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123                     task->task_status.stat == SAS_DATA_UNDERRUN) {
124                         /* no error, but return the number of bytes of
125                          * underrun */
126                         res = task->task_status.residual;
127                         break;
128                 }
129                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130                     task->task_status.stat == SAS_DATA_OVERRUN) {
131                         res = -EMSGSIZE;
132                         break;
133                 }
134                 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135                     task->task_status.stat == SAS_DEVICE_UNKNOWN)
136                         break;
137                 else {
138                         SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
139                                     "status 0x%x\n", __func__,
140                                     SAS_ADDR(dev->sas_addr),
141                                     task->task_status.resp,
142                                     task->task_status.stat);
143                         sas_free_task(task);
144                         task = NULL;
145                 }
146         }
147         mutex_unlock(&dev->ex_dev.cmd_mutex);
148
149         BUG_ON(retry == 3 && task != NULL);
150         sas_free_task(task);
151         return res;
152 }
153
154 /* ---------- Allocations ---------- */
155
156 static 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
164 static inline void *alloc_smp_resp(int size)
165 {
166         return kzalloc(size, GFP_KERNEL);
167 }
168
169 /* ---------- Expander configuration ---------- */
170
171 static 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;
191                 break;
192         default:
193                 phy->phy_state = PHY_NOT_PRESENT;
194                 break;
195         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
215         phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
216         phy->phy->identify.device_type = phy->attached_dev_type;
217         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;
220         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;
224         phy->phy->negotiated_linkrate = phy->linkrate;
225
226         if (!rediscover)
227                 if (sas_phy_add(phy->phy)) {
228                         sas_phy_free(phy->phy);
229                         return;
230                 }
231
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
242 /* check if we have an existing attached ata device on this expander phy */
243 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
244 {
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
264 #define DISCOVER_REQ_SIZE  16
265 #define DISCOVER_RESP_SIZE 56
266
267 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
268                                       u8 *disc_resp, int single)
269 {
270         struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
271         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;
281                 dr = &((struct smp_resp *)disc_resp)->disc;
282                 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                 }
287
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                  */
292                 if (!(dr->attached_dev_type == 0 &&
293                       dr->attached_sata_dev))
294                         break;
295
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                 }
305                 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
306                 /* 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
313 static 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) {
333                 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
334         } else {
335                 int i;
336
337                 for (i = 0; i < ex->num_phys; i++) {
338                         res = sas_ex_phy_discover_helper(dev, disc_req,
339                                                          disc_resp, i);
340                         if (res)
341                                 goto out_err;
342                 }
343         }
344 out_err:
345         kfree(disc_resp);
346         kfree(disc_req);
347         return res;
348 }
349
350 static 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
372 static 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);
380         dev->ex_dev.t2t_supp = rg->t2t_supp;
381         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
389 static 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         }
432 out:
433         kfree(rg_req);
434         kfree(rg_resp);
435         return res;
436 }
437
438 static 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
461 static 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);
491 out:
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
500 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
501                         enum phy_func phy_func,
502                         struct sas_phy_linkrates *rates)
503 {
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;
521         if (rates) {
522                 pc_req[32] = rates->minimum_linkrate << 4;
523                 pc_req[33] = rates->maximum_linkrate << 4;
524         }
525
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
533 static 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
538         sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
539         phy->linkrate = SAS_PHY_DISABLED;
540 }
541
542 static 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
559 static 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
575 int sas_smp_get_phy_events(struct sas_phy *phy)
576 {
577         int res;
578         u8 *req;
579         u8 *resp;
580         struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
581         struct domain_device *dev = sas_find_dev_by_rphy(rphy);
582
583         req = alloc_smp_req(RPEL_REQ_SIZE);
584         if (!req)
585                 return -ENOMEM;
586
587         resp = alloc_smp_resp(RPEL_RESP_SIZE);
588         if (!resp) {
589                 kfree(req);
590                 return -ENOMEM;
591         }
592
593         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
613 #ifdef CONFIG_SCSI_SAS_ATA
614
615 #define RPS_REQ_SIZE  16
616 #define RPS_RESP_SIZE 60
617
618 static 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);
624         u8 *resp = (u8 *)rps_resp;
625
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
635         /* 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
655         kfree(rps_req);
656         return res;
657 }
658 #endif
659
660 static 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
694 static 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
706         child = sas_alloc_device();
707         if (!child)
708                 return NULL;
709
710         kref_get(&parent->kref);
711         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);
716         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         }
725         sas_ex_get_linkrate(parent, child, phy);
726         sas_device_set_phy(child, phy->port);
727
728 #ifdef CONFIG_SCSI_SAS_ATA
729         if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
730                 child->dev_type = SATA_DEV;
731                 if (phy->attached_tproto & SAS_PROTOCOL_STP)
732                         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);
741                         goto out_free;
742                 }
743                 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
744                        sizeof(struct dev_to_host_fis));
745
746                 rphy = sas_end_device_alloc(phy->port);
747                 if (unlikely(!rphy))
748                         goto out_free;
749
750                 sas_init_dev(child);
751
752                 child->rphy = rphy;
753
754                 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
755
756                 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);
762                         goto out_list_del;
763                 }
764         } else
765 #endif
766           if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
767                 child->dev_type = SAS_END_DEV;
768                 rphy = sas_end_device_alloc(phy->port);
769                 /* FIXME: error handling */
770                 if (unlikely(!rphy))
771                         goto out_free;
772                 child->tproto = phy->attached_tproto;
773                 sas_init_dev(child);
774
775                 child->rphy = rphy;
776                 sas_fill_in_rphy(child, rphy);
777
778                 spin_lock_irq(&parent->port->dev_list_lock);
779                 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
780                 spin_unlock_irq(&parent->port->dev_list_lock);
781
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);
788                         goto out_list_del;
789                 }
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);
794                 goto out_free;
795         }
796
797         list_add_tail(&child->siblings, &parent_ex->children);
798         return child;
799
800  out_list_del:
801         sas_rphy_free(child->rphy);
802         child->rphy = NULL;
803
804         list_del(&child->disco_list_node);
805         spin_lock_irq(&parent->port->dev_list_lock);
806         list_del(&child->dev_list_node);
807         spin_unlock_irq(&parent->port->dev_list_lock);
808  out_free:
809         sas_port_delete(phy->port);
810  out_err:
811         phy->port = NULL;
812         sas_put_device(child);
813         return NULL;
814 }
815
816 /* See if this phy is part of a wide port */
817 static 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);
831                         phy->port = ephy->port;
832                         phy->phy_state = PHY_DEVICE_DISCOVERED;
833                         return 0;
834                 }
835         }
836
837         return -ENODEV;
838 }
839
840 static 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         }
859         child = sas_alloc_device();
860         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;
885         kref_get(&parent->kref);
886         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
900         spin_lock_irq(&parent->port->dev_list_lock);
901         list_add_tail(&child->dev_list_node, &parent->port->dev_list);
902         spin_unlock_irq(&parent->port->dev_list_lock);
903
904         res = sas_discover_expander(child);
905         if (res) {
906                 spin_lock_irq(&parent->port->dev_list_lock);
907                 list_del(&child->dev_list_node);
908                 spin_unlock_irq(&parent->port->dev_list_lock);
909                 sas_put_device(child);
910                 return NULL;
911         }
912         list_add_tail(&child->siblings, &parent->ex_dev.children);
913         return child;
914 }
915
916 static 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 */
924         if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
925                 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
926                         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;
954         } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
955                 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
976         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
983         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;
1015                         /*
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                          */
1019                         if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1020                             SAS_ADDR(child->sas_addr)) {
1021                                 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1022                                 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                         }
1028                 }
1029         }
1030
1031         return res;
1032 }
1033
1034 static 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
1058 static 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  */
1099 static 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) {
1119                 case SAS_PHY_DISABLED:
1120                 case SAS_PHY_RESET_PROBLEM:
1121                 case SAS_SATA_PORT_SELECTOR:
1122                         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
1137 static 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
1175 static 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
1190         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",
1192
1193                    ex_type[parent->dev_type],
1194                    SAS_ADDR(parent->sas_addr),
1195                    parent->ex_dev.t2t_supp,
1196                    parent_phy->phy_id,
1197
1198                    ex_type[child->dev_type],
1199                    SAS_ADDR(child->sas_addr),
1200                    child->ex_dev.t2t_supp,
1201                    child_phy->phy_id,
1202
1203                    ra_char[parent_phy->routing_attr],
1204                    ra_char[child_phy->routing_attr]);
1205 }
1206
1207 static 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  */
1253 static 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                                 }
1297                         } 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                                 }
1306                         }
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
1326 static 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 "
1365                                     "result 0x%x\n", __func__,
1366                                     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;
1391 out:
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
1400 static 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         }
1434 out:
1435         kfree(cri_req);
1436         kfree(cri_resp);
1437         return res;
1438 }
1439
1440 static 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  */
1462 static 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  */
1503 static 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
1510 static 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
1517 /**
1518  * sas_discover_expander -- expander discovery
1519  * @ex: pointer to expander domain device
1520  *
1521  * See comment in sas_discover_sata().
1522  */
1523 static 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;
1550 out_err:
1551         sas_notify_lldd_dev_gone(dev);
1552         return res;
1553 }
1554
1555 static 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
1577 static 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
1591 int 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
1596         res = sas_rphy_add(dev->rphy);
1597         if (res)
1598                 goto out_err;
1599
1600         ex->level = dev->port->disc.max_level; /* 0 */
1601         res = sas_discover_expander(dev);
1602         if (res)
1603                 goto out_err2;
1604
1605         sas_ex_bfs_disc(dev->port);
1606
1607         return res;
1608
1609 out_err2:
1610         sas_rphy_remove(dev->rphy);
1611 out_err:
1612         return res;
1613 }
1614
1615 /* ---------- Domain revalidation ---------- */
1616
1617 static 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         }
1638 out:
1639         kfree(disc_req);
1640         return res;
1641 }
1642
1643 static 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
1661 int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1662                                   u8 *attached_sas_addr)
1663 {
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
1683 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1684                               int from_phy, bool update)
1685 {
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) {
1697                         if (update)
1698                                 ex->ex_phy[i].phy_change_count =
1699                                         phy_change_count;
1700                         *phy_id = i;
1701                         return 0;
1702                 }
1703         }
1704 out:
1705         return res;
1706 }
1707
1708 static 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);
1736 out:
1737         kfree(rg_resp);
1738         kfree(rg_req);
1739         return res;
1740 }
1741 /**
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  */
1755
1756 static 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;
1761         int phy_id = -1;
1762         int res;
1763         struct domain_device *ch;
1764
1765         res = sas_get_ex_change_count(dev, &ex_change_count);
1766         if (res)
1767                 goto out;
1768         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);
1785                         if (*src_dev)
1786                                 return res;
1787                 }
1788         }
1789 out:
1790         return res;
1791 }
1792
1793 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1794 {
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) {
1799                 set_bit(SAS_DEV_GONE, &child->state);
1800                 if (child->dev_type == EDGE_DEV ||
1801                     child->dev_type == FANOUT_DEV)
1802                         sas_unregister_ex_tree(port, child);
1803                 else
1804                         sas_unregister_dev(port, child);
1805         }
1806         sas_unregister_dev(port, dev);
1807 }
1808
1809 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1810                                          int phy_id, bool last)
1811 {
1812         struct expander_device *ex_dev = &parent->ex_dev;
1813         struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1814         struct domain_device *child, *n, *found = NULL;
1815         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)) {
1820                                 set_bit(SAS_DEV_GONE, &child->state);
1821                                 if (child->dev_type == EDGE_DEV ||
1822                                     child->dev_type == FANOUT_DEV)
1823                                         sas_unregister_ex_tree(parent->port, child);
1824                                 else
1825                                         sas_unregister_dev(parent->port, child);
1826                                 found = child;
1827                                 break;
1828                         }
1829                 }
1830                 sas_disable_routing(parent, phy->attached_sas_addr);
1831         }
1832         memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1833         if (phy->port) {
1834                 sas_port_delete_phy(phy->port, phy->phy);
1835                 sas_device_set_phy(found, phy->port);
1836                 if (phy->port->num_phys == 0)
1837                         sas_port_delete(phy->port);
1838                 phy->port = NULL;
1839         }
1840 }
1841
1842 static 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
1865 static 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);
1879 out:
1880         return res;
1881 }
1882
1883 static 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;
1887         bool found = false;
1888         int res, i;
1889
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;
1895         /* 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         }
1910         res = sas_ex_discover_devices(dev, phy_id);
1911         if (!res)
1912                 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         }
1922 out:
1923         return res;
1924 }
1925
1926 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1927 {
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;
1937                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1938                 goto out; break;
1939         case SMP_RESP_PHY_VACANT:
1940                 phy->phy_state = PHY_VACANT;
1941                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1942                 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;
1949                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1950         } 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);
1954                 sas_ex_phy_discover(dev, phy_id);
1955         } else
1956                 res = sas_discover_new(dev, phy_id);
1957 out:
1958         return res;
1959 }
1960
1961 /**
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  */
1975 static 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;
1981         bool last = true;       /* is this the last phy of the port */
1982
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);
1996                                 last = false;
1997                                 break;
1998                         }
1999                 }
2000                 res = sas_rediscover_dev(dev, phy_id, last);
2001         } else
2002                 res = sas_discover_new(dev, phy_id);
2003         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  */
2015 int 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;
2029                         res = sas_find_bcast_phy(dev, &phy_id, i, true);
2030                         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         }
2036 out:
2037         return res;
2038 }
2039
2040 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2041                     struct request *req)
2042 {
2043         struct domain_device *dev;
2044         int ret, type;
2045         struct request *rsp = req->next_rq;
2046
2047         if (!rsp) {
2048                 printk("%s: space for a smp response is missing\n",
2049                        __func__);
2050                 return -EINVAL;
2051         }
2052
2053         /* no rphy means no smp target support (ie aic94xx host) */
2054         if (!rphy)
2055                 return sas_smp_host_handler(shost, req, rsp);
2056
2057         type = rphy->identify.device_type;
2058
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",
2062                        __func__);
2063                 return -EINVAL;
2064         }
2065
2066         dev = sas_find_dev_by_rphy(rphy);
2067         if (!dev) {
2068                 printk("%s: fail to find a domain_device?\n", __func__);
2069                 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",
2075                        __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2076                        rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2077                 return -EINVAL;
2078         }
2079
2080         ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2081                                bio_data(rsp->bio), blk_rq_bytes(rsp));
2082         if (ret > 0) {
2083                 /* positive number is the untransferred residual */
2084                 rsp->resid_len = ret;
2085                 req->resid_len = 0;
2086                 ret = 0;
2087         } else if (ret == 0) {
2088                 rsp->resid_len = 0;
2089                 req->resid_len = 0;
2090         }
2091
2092         return ret;
2093 }