Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/drivers/scsi/libsas/sas_expander.c (Version 2.6.31.13) and /linux/drivers/scsi/libsas/sas_expander.c (Version 2.6.25.8)


  1 /*                                                  1 /*
  2  * Serial Attached SCSI (SAS) Expander discove      2  * Serial Attached SCSI (SAS) Expander discovery and configuration
  3  *                                                  3  *
  4  * Copyright (C) 2005 Adaptec, Inc.  All right      4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
  5  * Copyright (C) 2005 Luben Tuikov <luben_tuik      5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
  6  *                                                  6  *
  7  * This file is licensed under GPLv2.               7  * This file is licensed under GPLv2.
  8  *                                                  8  *
  9  * This program is free software; you can redi      9  * This program is free software; you can redistribute it and/or
 10  * modify it under the terms of the GNU Genera     10  * modify it under the terms of the GNU General Public License as
 11  * published by the Free Software Foundation;      11  * published by the Free Software Foundation; either version 2 of the
 12  * License, or (at your option) any later vers     12  * License, or (at your option) any later version.
 13  *                                                 13  *
 14  * This program is distributed in the hope tha     14  * This program is distributed in the hope that it will be useful, but
 15  * WITHOUT ANY WARRANTY; without even the impl     15  * WITHOUT ANY WARRANTY; without even the implied warranty of
 16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17  * General Public License for more details.        17  * General Public License for more details.
 18  *                                                 18  *
 19  * You should have received a copy of the GNU      19  * You should have received a copy of the GNU General Public License
 20  * along with this program; if not, write to t     20  * along with this program; if not, write to the Free Software
 21  * Foundation, Inc., 51 Franklin St, Fifth Flo     21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 22  *                                                 22  *
 23  */                                                23  */
 24                                                    24 
 25 #include <linux/scatterlist.h>                     25 #include <linux/scatterlist.h>
 26 #include <linux/blkdev.h>                          26 #include <linux/blkdev.h>
 27                                                    27 
 28 #include "sas_internal.h"                          28 #include "sas_internal.h"
 29                                                    29 
 30 #include <scsi/scsi_transport.h>                   30 #include <scsi/scsi_transport.h>
 31 #include <scsi/scsi_transport_sas.h>               31 #include <scsi/scsi_transport_sas.h>
 32 #include "../scsi_sas_internal.h"                  32 #include "../scsi_sas_internal.h"
 33                                                    33 
 34 static int sas_discover_expander(struct domain     34 static int sas_discover_expander(struct domain_device *dev);
 35 static int sas_configure_routing(struct domain     35 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
 36 static int sas_configure_phy(struct domain_dev     36 static int sas_configure_phy(struct domain_device *dev, int phy_id,
 37                              u8 *sas_addr, int     37                              u8 *sas_addr, int include);
 38 static int sas_disable_routing(struct domain_d     38 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr);
 39                                                    39 
 40 /* ---------- SMP task management ---------- *     40 /* ---------- SMP task management ---------- */
 41                                                    41 
 42 static void smp_task_timedout(unsigned long _t     42 static void smp_task_timedout(unsigned long _task)
 43 {                                                  43 {
 44         struct sas_task *task = (void *) _task     44         struct sas_task *task = (void *) _task;
 45         unsigned long flags;                       45         unsigned long flags;
 46                                                    46 
 47         spin_lock_irqsave(&task->task_state_lo     47         spin_lock_irqsave(&task->task_state_lock, flags);
 48         if (!(task->task_state_flags & SAS_TAS     48         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
 49                 task->task_state_flags |= SAS_     49                 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
 50         spin_unlock_irqrestore(&task->task_sta     50         spin_unlock_irqrestore(&task->task_state_lock, flags);
 51                                                    51 
 52         complete(&task->completion);               52         complete(&task->completion);
 53 }                                                  53 }
 54                                                    54 
 55 static void smp_task_done(struct sas_task *tas     55 static void smp_task_done(struct sas_task *task)
 56 {                                                  56 {
 57         if (!del_timer(&task->timer))              57         if (!del_timer(&task->timer))
 58                 return;                            58                 return;
 59         complete(&task->completion);               59         complete(&task->completion);
 60 }                                                  60 }
 61                                                    61 
 62 /* Give it some long enough timeout. In second     62 /* Give it some long enough timeout. In seconds. */
 63 #define SMP_TIMEOUT 10                             63 #define SMP_TIMEOUT 10
 64                                                    64 
 65 static int smp_execute_task(struct domain_devi     65 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
 66                             void *resp, int re     66                             void *resp, int resp_size)
 67 {                                                  67 {
 68         int res, retry;                            68         int res, retry;
 69         struct sas_task *task = NULL;              69         struct sas_task *task = NULL;
 70         struct sas_internal *i =                   70         struct sas_internal *i =
 71                 to_sas_internal(dev->port->ha-     71                 to_sas_internal(dev->port->ha->core.shost->transportt);
 72                                                    72 
 73         for (retry = 0; retry < 3; retry++) {      73         for (retry = 0; retry < 3; retry++) {
 74                 task = sas_alloc_task(GFP_KERN     74                 task = sas_alloc_task(GFP_KERNEL);
 75                 if (!task)                         75                 if (!task)
 76                         return -ENOMEM;            76                         return -ENOMEM;
 77                                                    77 
 78                 task->dev = dev;                   78                 task->dev = dev;
 79                 task->task_proto = dev->tproto     79                 task->task_proto = dev->tproto;
 80                 sg_init_one(&task->smp_task.sm     80                 sg_init_one(&task->smp_task.smp_req, req, req_size);
 81                 sg_init_one(&task->smp_task.sm     81                 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
 82                                                    82 
 83                 task->task_done = smp_task_don     83                 task->task_done = smp_task_done;
 84                                                    84 
 85                 task->timer.data = (unsigned l     85                 task->timer.data = (unsigned long) task;
 86                 task->timer.function = smp_tas     86                 task->timer.function = smp_task_timedout;
 87                 task->timer.expires = jiffies      87                 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
 88                 add_timer(&task->timer);           88                 add_timer(&task->timer);
 89                                                    89 
 90                 res = i->dft->lldd_execute_tas     90                 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
 91                                                    91 
 92                 if (res) {                         92                 if (res) {
 93                         del_timer(&task->timer     93                         del_timer(&task->timer);
 94                         SAS_DPRINTK("executing     94                         SAS_DPRINTK("executing SMP task failed:%d\n", res);
 95                         goto ex_err;               95                         goto ex_err;
 96                 }                                  96                 }
 97                                                    97 
 98                 wait_for_completion(&task->com     98                 wait_for_completion(&task->completion);
 99                 res = -ECOMM;                      99                 res = -ECOMM;
100                 if ((task->task_state_flags &     100                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
101                         SAS_DPRINTK("smp task     101                         SAS_DPRINTK("smp task timed out or aborted\n");
102                         i->dft->lldd_abort_tas    102                         i->dft->lldd_abort_task(task);
103                         if (!(task->task_state    103                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
104                                 SAS_DPRINTK("S    104                                 SAS_DPRINTK("SMP task aborted and not done\n");
105                                 goto ex_err;      105                                 goto ex_err;
106                         }                         106                         }
107                 }                                 107                 }
108                 if (task->task_status.resp ==     108                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
109                     task->task_status.stat ==     109                     task->task_status.stat == SAM_GOOD) {
110                         res = 0;                  110                         res = 0;
111                         break;                    111                         break;
112                 } if (task->task_status.resp =    112                 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
113                       task->task_status.stat =    113                       task->task_status.stat == SAS_DATA_UNDERRUN) {
114                         /* no error, but retur    114                         /* no error, but return the number of bytes of
115                          * underrun */            115                          * underrun */
116                         res = task->task_statu    116                         res = task->task_status.residual;
117                         break;                    117                         break;
118                 } if (task->task_status.resp =    118                 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
119                       task->task_status.stat =    119                       task->task_status.stat == SAS_DATA_OVERRUN) {
120                         res = -EMSGSIZE;          120                         res = -EMSGSIZE;
121                         break;                    121                         break;
122                 } else {                          122                 } else {
123                         SAS_DPRINTK("%s: task     123                         SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
124                                     "status 0x !! 124                                     "status 0x%x\n", __FUNCTION__,
125                                     SAS_ADDR(d    125                                     SAS_ADDR(dev->sas_addr),
126                                     task->task    126                                     task->task_status.resp,
127                                     task->task    127                                     task->task_status.stat);
128                         sas_free_task(task);      128                         sas_free_task(task);
129                         task = NULL;              129                         task = NULL;
130                 }                                 130                 }
131         }                                         131         }
132 ex_err:                                           132 ex_err:
133         BUG_ON(retry == 3 && task != NULL);       133         BUG_ON(retry == 3 && task != NULL);
134         if (task != NULL) {                       134         if (task != NULL) {
135                 sas_free_task(task);              135                 sas_free_task(task);
136         }                                         136         }
137         return res;                               137         return res;
138 }                                                 138 }
139                                                   139 
140 /* ---------- Allocations ---------- */           140 /* ---------- Allocations ---------- */
141                                                   141 
142 static inline void *alloc_smp_req(int size)       142 static inline void *alloc_smp_req(int size)
143 {                                                 143 {
144         u8 *p = kzalloc(size, GFP_KERNEL);        144         u8 *p = kzalloc(size, GFP_KERNEL);
145         if (p)                                    145         if (p)
146                 p[0] = SMP_REQUEST;               146                 p[0] = SMP_REQUEST;
147         return p;                                 147         return p;
148 }                                                 148 }
149                                                   149 
150 static inline void *alloc_smp_resp(int size)      150 static inline void *alloc_smp_resp(int size)
151 {                                                 151 {
152         return kzalloc(size, GFP_KERNEL);         152         return kzalloc(size, GFP_KERNEL);
153 }                                                 153 }
154                                                   154 
155 /* ---------- Expander configuration ---------    155 /* ---------- Expander configuration ---------- */
156                                                   156 
157 static void sas_set_ex_phy(struct domain_devic    157 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
158                            void *disc_resp)       158                            void *disc_resp)
159 {                                                 159 {
160         struct expander_device *ex = &dev->ex_    160         struct expander_device *ex = &dev->ex_dev;
161         struct ex_phy *phy = &ex->ex_phy[phy_i    161         struct ex_phy *phy = &ex->ex_phy[phy_id];
162         struct smp_resp *resp = disc_resp;        162         struct smp_resp *resp = disc_resp;
163         struct discover_resp *dr = &resp->disc    163         struct discover_resp *dr = &resp->disc;
164         struct sas_rphy *rphy = dev->rphy;        164         struct sas_rphy *rphy = dev->rphy;
165         int rediscover = (phy->phy != NULL);      165         int rediscover = (phy->phy != NULL);
166                                                   166 
167         if (!rediscover) {                        167         if (!rediscover) {
168                 phy->phy = sas_phy_alloc(&rphy    168                 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
169                                                   169 
170                 /* FIXME: error_handling */       170                 /* FIXME: error_handling */
171                 BUG_ON(!phy->phy);                171                 BUG_ON(!phy->phy);
172         }                                         172         }
173                                                   173 
174         switch (resp->result) {                   174         switch (resp->result) {
175         case SMP_RESP_PHY_VACANT:                 175         case SMP_RESP_PHY_VACANT:
176                 phy->phy_state = PHY_VACANT;      176                 phy->phy_state = PHY_VACANT;
177                 return;                           177                 return;
178         default:                                  178         default:
179                 phy->phy_state = PHY_NOT_PRESE    179                 phy->phy_state = PHY_NOT_PRESENT;
180                 return;                           180                 return;
181         case SMP_RESP_FUNC_ACC:                   181         case SMP_RESP_FUNC_ACC:
182                 phy->phy_state = PHY_EMPTY; /*    182                 phy->phy_state = PHY_EMPTY; /* do not know yet */
183                 break;                            183                 break;
184         }                                         184         }
185                                                   185 
186         phy->phy_id = phy_id;                     186         phy->phy_id = phy_id;
187         phy->attached_dev_type = dr->attached_    187         phy->attached_dev_type = dr->attached_dev_type;
188         phy->linkrate = dr->linkrate;             188         phy->linkrate = dr->linkrate;
189         phy->attached_sata_host = dr->attached    189         phy->attached_sata_host = dr->attached_sata_host;
190         phy->attached_sata_dev  = dr->attached    190         phy->attached_sata_dev  = dr->attached_sata_dev;
191         phy->attached_sata_ps   = dr->attached    191         phy->attached_sata_ps   = dr->attached_sata_ps;
192         phy->attached_iproto = dr->iproto << 1    192         phy->attached_iproto = dr->iproto << 1;
193         phy->attached_tproto = dr->tproto << 1    193         phy->attached_tproto = dr->tproto << 1;
194         memcpy(phy->attached_sas_addr, dr->att    194         memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
195         phy->attached_phy_id = dr->attached_ph    195         phy->attached_phy_id = dr->attached_phy_id;
196         phy->phy_change_count = dr->change_cou    196         phy->phy_change_count = dr->change_count;
197         phy->routing_attr = dr->routing_attr;     197         phy->routing_attr = dr->routing_attr;
198         phy->virtual = dr->virtual;               198         phy->virtual = dr->virtual;
199         phy->last_da_index = -1;                  199         phy->last_da_index = -1;
200                                                   200 
201         phy->phy->identify.initiator_port_prot    201         phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
202         phy->phy->identify.target_port_protoco    202         phy->phy->identify.target_port_protocols = phy->attached_tproto;
203         phy->phy->identify.phy_identifier = ph    203         phy->phy->identify.phy_identifier = phy_id;
204         phy->phy->minimum_linkrate_hw = dr->hm    204         phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
205         phy->phy->maximum_linkrate_hw = dr->hm    205         phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
206         phy->phy->minimum_linkrate = dr->pmin_    206         phy->phy->minimum_linkrate = dr->pmin_linkrate;
207         phy->phy->maximum_linkrate = dr->pmax_    207         phy->phy->maximum_linkrate = dr->pmax_linkrate;
208         phy->phy->negotiated_linkrate = phy->l    208         phy->phy->negotiated_linkrate = phy->linkrate;
209                                                   209 
210         if (!rediscover)                          210         if (!rediscover)
211                 sas_phy_add(phy->phy);            211                 sas_phy_add(phy->phy);
212                                                   212 
213         SAS_DPRINTK("ex %016llx phy%02d:%c att    213         SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
214                     SAS_ADDR(dev->sas_addr), p    214                     SAS_ADDR(dev->sas_addr), phy->phy_id,
215                     phy->routing_attr == TABLE    215                     phy->routing_attr == TABLE_ROUTING ? 'T' :
216                     phy->routing_attr == DIREC    216                     phy->routing_attr == DIRECT_ROUTING ? 'D' :
217                     phy->routing_attr == SUBTR    217                     phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
218                     SAS_ADDR(phy->attached_sas    218                     SAS_ADDR(phy->attached_sas_addr));
219                                                   219 
220         return;                                   220         return;
221 }                                                 221 }
222                                                   222 
223 #define DISCOVER_REQ_SIZE  16                     223 #define DISCOVER_REQ_SIZE  16
224 #define DISCOVER_RESP_SIZE 56                     224 #define DISCOVER_RESP_SIZE 56
225                                                   225 
226 static int sas_ex_phy_discover_helper(struct d    226 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
227                                       u8 *disc    227                                       u8 *disc_resp, int single)
228 {                                                 228 {
229         int i, res;                               229         int i, res;
230                                                   230 
231         disc_req[9] = single;                     231         disc_req[9] = single;
232         for (i = 1 ; i < 3; i++) {                232         for (i = 1 ; i < 3; i++) {
233                 struct discover_resp *dr;         233                 struct discover_resp *dr;
234                                                   234 
235                 res = smp_execute_task(dev, di    235                 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
236                                        disc_re    236                                        disc_resp, DISCOVER_RESP_SIZE);
237                 if (res)                          237                 if (res)
238                         return res;               238                         return res;
239                 /* This is detecting a failure    239                 /* This is detecting a failure to transmit inital
240                  * dev to host FIS as describe    240                  * dev to host FIS as described in section G.5 of
241                  * sas-2 r 04b */                 241                  * sas-2 r 04b */
242                 dr = &((struct smp_resp *)disc    242                 dr = &((struct smp_resp *)disc_resp)->disc;
243                 if (!(dr->attached_dev_type ==    243                 if (!(dr->attached_dev_type == 0 &&
244                       dr->attached_sata_dev))     244                       dr->attached_sata_dev))
245                         break;                    245                         break;
246                 /* In order to generate the de    246                 /* In order to generate the dev to host FIS, we
247                  * send a link reset to the ex    247                  * send a link reset to the expander port */
248                 sas_smp_phy_control(dev, singl    248                 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
249                 /* Wait for the reset to trigg    249                 /* Wait for the reset to trigger the negotiation */
250                 msleep(500);                      250                 msleep(500);
251         }                                         251         }
252         sas_set_ex_phy(dev, single, disc_resp)    252         sas_set_ex_phy(dev, single, disc_resp);
253         return 0;                                 253         return 0;
254 }                                                 254 }
255                                                   255 
256 static int sas_ex_phy_discover(struct domain_d    256 static int sas_ex_phy_discover(struct domain_device *dev, int single)
257 {                                                 257 {
258         struct expander_device *ex = &dev->ex_    258         struct expander_device *ex = &dev->ex_dev;
259         int  res = 0;                             259         int  res = 0;
260         u8   *disc_req;                           260         u8   *disc_req;
261         u8   *disc_resp;                          261         u8   *disc_resp;
262                                                   262 
263         disc_req = alloc_smp_req(DISCOVER_REQ_    263         disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
264         if (!disc_req)                            264         if (!disc_req)
265                 return -ENOMEM;                   265                 return -ENOMEM;
266                                                   266 
267         disc_resp = alloc_smp_req(DISCOVER_RES    267         disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
268         if (!disc_resp) {                         268         if (!disc_resp) {
269                 kfree(disc_req);                  269                 kfree(disc_req);
270                 return -ENOMEM;                   270                 return -ENOMEM;
271         }                                         271         }
272                                                   272 
273         disc_req[1] = SMP_DISCOVER;               273         disc_req[1] = SMP_DISCOVER;
274                                                   274 
275         if (0 <= single && single < ex->num_ph    275         if (0 <= single && single < ex->num_phys) {
276                 res = sas_ex_phy_discover_help    276                 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
277         } else {                                  277         } else {
278                 int i;                            278                 int i;
279                                                   279 
280                 for (i = 0; i < ex->num_phys;     280                 for (i = 0; i < ex->num_phys; i++) {
281                         res = sas_ex_phy_disco    281                         res = sas_ex_phy_discover_helper(dev, disc_req,
282                                                   282                                                          disc_resp, i);
283                         if (res)                  283                         if (res)
284                                 goto out_err;     284                                 goto out_err;
285                 }                                 285                 }
286         }                                         286         }
287 out_err:                                          287 out_err:
288         kfree(disc_resp);                         288         kfree(disc_resp);
289         kfree(disc_req);                          289         kfree(disc_req);
290         return res;                               290         return res;
291 }                                                 291 }
292                                                   292 
293 static int sas_expander_discover(struct domain    293 static int sas_expander_discover(struct domain_device *dev)
294 {                                                 294 {
295         struct expander_device *ex = &dev->ex_    295         struct expander_device *ex = &dev->ex_dev;
296         int res = -ENOMEM;                        296         int res = -ENOMEM;
297                                                   297 
298         ex->ex_phy = kzalloc(sizeof(*ex->ex_ph    298         ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
299         if (!ex->ex_phy)                          299         if (!ex->ex_phy)
300                 return -ENOMEM;                   300                 return -ENOMEM;
301                                                   301 
302         res = sas_ex_phy_discover(dev, -1);       302         res = sas_ex_phy_discover(dev, -1);
303         if (res)                                  303         if (res)
304                 goto out_err;                     304                 goto out_err;
305                                                   305 
306         return 0;                                 306         return 0;
307  out_err:                                         307  out_err:
308         kfree(ex->ex_phy);                        308         kfree(ex->ex_phy);
309         ex->ex_phy = NULL;                        309         ex->ex_phy = NULL;
310         return res;                               310         return res;
311 }                                                 311 }
312                                                   312 
313 #define MAX_EXPANDER_PHYS 128                     313 #define MAX_EXPANDER_PHYS 128
314                                                   314 
315 static void ex_assign_report_general(struct do    315 static void ex_assign_report_general(struct domain_device *dev,
316                                             st    316                                             struct smp_resp *resp)
317 {                                                 317 {
318         struct report_general_resp *rg = &resp    318         struct report_general_resp *rg = &resp->rg;
319                                                   319 
320         dev->ex_dev.ex_change_count = be16_to_    320         dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
321         dev->ex_dev.max_route_indexes = be16_t    321         dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
322         dev->ex_dev.num_phys = min(rg->num_phy    322         dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
323         dev->ex_dev.conf_route_table = rg->con    323         dev->ex_dev.conf_route_table = rg->conf_route_table;
324         dev->ex_dev.configuring = rg->configur    324         dev->ex_dev.configuring = rg->configuring;
325         memcpy(dev->ex_dev.enclosure_logical_i    325         memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
326 }                                                 326 }
327                                                   327 
328 #define RG_REQ_SIZE   8                           328 #define RG_REQ_SIZE   8
329 #define RG_RESP_SIZE 32                           329 #define RG_RESP_SIZE 32
330                                                   330 
331 static int sas_ex_general(struct domain_device    331 static int sas_ex_general(struct domain_device *dev)
332 {                                                 332 {
333         u8 *rg_req;                               333         u8 *rg_req;
334         struct smp_resp *rg_resp;                 334         struct smp_resp *rg_resp;
335         int res;                                  335         int res;
336         int i;                                    336         int i;
337                                                   337 
338         rg_req = alloc_smp_req(RG_REQ_SIZE);      338         rg_req = alloc_smp_req(RG_REQ_SIZE);
339         if (!rg_req)                              339         if (!rg_req)
340                 return -ENOMEM;                   340                 return -ENOMEM;
341                                                   341 
342         rg_resp = alloc_smp_resp(RG_RESP_SIZE)    342         rg_resp = alloc_smp_resp(RG_RESP_SIZE);
343         if (!rg_resp) {                           343         if (!rg_resp) {
344                 kfree(rg_req);                    344                 kfree(rg_req);
345                 return -ENOMEM;                   345                 return -ENOMEM;
346         }                                         346         }
347                                                   347 
348         rg_req[1] = SMP_REPORT_GENERAL;           348         rg_req[1] = SMP_REPORT_GENERAL;
349                                                   349 
350         for (i = 0; i < 5; i++) {                 350         for (i = 0; i < 5; i++) {
351                 res = smp_execute_task(dev, rg    351                 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
352                                        RG_RESP    352                                        RG_RESP_SIZE);
353                                                   353 
354                 if (res) {                        354                 if (res) {
355                         SAS_DPRINTK("RG to ex     355                         SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
356                                     SAS_ADDR(d    356                                     SAS_ADDR(dev->sas_addr), res);
357                         goto out;                 357                         goto out;
358                 } else if (rg_resp->result !=     358                 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
359                         SAS_DPRINTK("RG:ex %01    359                         SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
360                                     SAS_ADDR(d    360                                     SAS_ADDR(dev->sas_addr), rg_resp->result);
361                         res = rg_resp->result;    361                         res = rg_resp->result;
362                         goto out;                 362                         goto out;
363                 }                                 363                 }
364                                                   364 
365                 ex_assign_report_general(dev,     365                 ex_assign_report_general(dev, rg_resp);
366                                                   366 
367                 if (dev->ex_dev.configuring) {    367                 if (dev->ex_dev.configuring) {
368                         SAS_DPRINTK("RG: ex %l    368                         SAS_DPRINTK("RG: ex %llx self-configuring...\n",
369                                     SAS_ADDR(d    369                                     SAS_ADDR(dev->sas_addr));
370                         schedule_timeout_inter    370                         schedule_timeout_interruptible(5*HZ);
371                 } else                            371                 } else
372                         break;                    372                         break;
373         }                                         373         }
374 out:                                              374 out:
375         kfree(rg_req);                            375         kfree(rg_req);
376         kfree(rg_resp);                           376         kfree(rg_resp);
377         return res;                               377         return res;
378 }                                                 378 }
379                                                   379 
380 static void ex_assign_manuf_info(struct domain    380 static void ex_assign_manuf_info(struct domain_device *dev, void
381                                         *_mi_r    381                                         *_mi_resp)
382 {                                                 382 {
383         u8 *mi_resp = _mi_resp;                   383         u8 *mi_resp = _mi_resp;
384         struct sas_rphy *rphy = dev->rphy;        384         struct sas_rphy *rphy = dev->rphy;
385         struct sas_expander_device *edev = rph    385         struct sas_expander_device *edev = rphy_to_expander_device(rphy);
386                                                   386 
387         memcpy(edev->vendor_id, mi_resp + 12,     387         memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
388         memcpy(edev->product_id, mi_resp + 20,    388         memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
389         memcpy(edev->product_rev, mi_resp + 36    389         memcpy(edev->product_rev, mi_resp + 36,
390                SAS_EXPANDER_PRODUCT_REV_LEN);     390                SAS_EXPANDER_PRODUCT_REV_LEN);
391                                                   391 
392         if (mi_resp[8] & 1) {                     392         if (mi_resp[8] & 1) {
393                 memcpy(edev->component_vendor_    393                 memcpy(edev->component_vendor_id, mi_resp + 40,
394                        SAS_EXPANDER_COMPONENT_    394                        SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
395                 edev->component_id = mi_resp[4    395                 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
396                 edev->component_revision_id =     396                 edev->component_revision_id = mi_resp[50];
397         }                                         397         }
398 }                                                 398 }
399                                                   399 
400 #define MI_REQ_SIZE   8                           400 #define MI_REQ_SIZE   8
401 #define MI_RESP_SIZE 64                           401 #define MI_RESP_SIZE 64
402                                                   402 
403 static int sas_ex_manuf_info(struct domain_dev    403 static int sas_ex_manuf_info(struct domain_device *dev)
404 {                                                 404 {
405         u8 *mi_req;                               405         u8 *mi_req;
406         u8 *mi_resp;                              406         u8 *mi_resp;
407         int res;                                  407         int res;
408                                                   408 
409         mi_req = alloc_smp_req(MI_REQ_SIZE);      409         mi_req = alloc_smp_req(MI_REQ_SIZE);
410         if (!mi_req)                              410         if (!mi_req)
411                 return -ENOMEM;                   411                 return -ENOMEM;
412                                                   412 
413         mi_resp = alloc_smp_resp(MI_RESP_SIZE)    413         mi_resp = alloc_smp_resp(MI_RESP_SIZE);
414         if (!mi_resp) {                           414         if (!mi_resp) {
415                 kfree(mi_req);                    415                 kfree(mi_req);
416                 return -ENOMEM;                   416                 return -ENOMEM;
417         }                                         417         }
418                                                   418 
419         mi_req[1] = SMP_REPORT_MANUF_INFO;        419         mi_req[1] = SMP_REPORT_MANUF_INFO;
420                                                   420 
421         res = smp_execute_task(dev, mi_req, MI    421         res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
422         if (res) {                                422         if (res) {
423                 SAS_DPRINTK("MI: ex %016llx fa    423                 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
424                             SAS_ADDR(dev->sas_    424                             SAS_ADDR(dev->sas_addr), res);
425                 goto out;                         425                 goto out;
426         } else if (mi_resp[2] != SMP_RESP_FUNC    426         } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
427                 SAS_DPRINTK("MI ex %016llx ret    427                 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
428                             SAS_ADDR(dev->sas_    428                             SAS_ADDR(dev->sas_addr), mi_resp[2]);
429                 goto out;                         429                 goto out;
430         }                                         430         }
431                                                   431 
432         ex_assign_manuf_info(dev, mi_resp);       432         ex_assign_manuf_info(dev, mi_resp);
433 out:                                              433 out:
434         kfree(mi_req);                            434         kfree(mi_req);
435         kfree(mi_resp);                           435         kfree(mi_resp);
436         return res;                               436         return res;
437 }                                                 437 }
438                                                   438 
439 #define PC_REQ_SIZE  44                           439 #define PC_REQ_SIZE  44
440 #define PC_RESP_SIZE 8                            440 #define PC_RESP_SIZE 8
441                                                   441 
442 int sas_smp_phy_control(struct domain_device *    442 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
443                         enum phy_func phy_func    443                         enum phy_func phy_func,
444                         struct sas_phy_linkrat    444                         struct sas_phy_linkrates *rates)
445 {                                                 445 {
446         u8 *pc_req;                               446         u8 *pc_req;
447         u8 *pc_resp;                              447         u8 *pc_resp;
448         int res;                                  448         int res;
449                                                   449 
450         pc_req = alloc_smp_req(PC_REQ_SIZE);      450         pc_req = alloc_smp_req(PC_REQ_SIZE);
451         if (!pc_req)                              451         if (!pc_req)
452                 return -ENOMEM;                   452                 return -ENOMEM;
453                                                   453 
454         pc_resp = alloc_smp_resp(PC_RESP_SIZE)    454         pc_resp = alloc_smp_resp(PC_RESP_SIZE);
455         if (!pc_resp) {                           455         if (!pc_resp) {
456                 kfree(pc_req);                    456                 kfree(pc_req);
457                 return -ENOMEM;                   457                 return -ENOMEM;
458         }                                         458         }
459                                                   459 
460         pc_req[1] = SMP_PHY_CONTROL;              460         pc_req[1] = SMP_PHY_CONTROL;
461         pc_req[9] = phy_id;                       461         pc_req[9] = phy_id;
462         pc_req[10]= phy_func;                     462         pc_req[10]= phy_func;
463         if (rates) {                              463         if (rates) {
464                 pc_req[32] = rates->minimum_li    464                 pc_req[32] = rates->minimum_linkrate << 4;
465                 pc_req[33] = rates->maximum_li    465                 pc_req[33] = rates->maximum_linkrate << 4;
466         }                                         466         }
467                                                   467 
468         res = smp_execute_task(dev, pc_req, PC    468         res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
469                                                   469 
470         kfree(pc_resp);                           470         kfree(pc_resp);
471         kfree(pc_req);                            471         kfree(pc_req);
472         return res;                               472         return res;
473 }                                                 473 }
474                                                   474 
475 static void sas_ex_disable_phy(struct domain_d    475 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
476 {                                                 476 {
477         struct expander_device *ex = &dev->ex_    477         struct expander_device *ex = &dev->ex_dev;
478         struct ex_phy *phy = &ex->ex_phy[phy_i    478         struct ex_phy *phy = &ex->ex_phy[phy_id];
479                                                   479 
480         sas_smp_phy_control(dev, phy_id, PHY_F    480         sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
481         phy->linkrate = SAS_PHY_DISABLED;         481         phy->linkrate = SAS_PHY_DISABLED;
482 }                                                 482 }
483                                                   483 
484 static void sas_ex_disable_port(struct domain_    484 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
485 {                                                 485 {
486         struct expander_device *ex = &dev->ex_    486         struct expander_device *ex = &dev->ex_dev;
487         int i;                                    487         int i;
488                                                   488 
489         for (i = 0; i < ex->num_phys; i++) {      489         for (i = 0; i < ex->num_phys; i++) {
490                 struct ex_phy *phy = &ex->ex_p    490                 struct ex_phy *phy = &ex->ex_phy[i];
491                                                   491 
492                 if (phy->phy_state == PHY_VACA    492                 if (phy->phy_state == PHY_VACANT ||
493                     phy->phy_state == PHY_NOT_    493                     phy->phy_state == PHY_NOT_PRESENT)
494                         continue;                 494                         continue;
495                                                   495 
496                 if (SAS_ADDR(phy->attached_sas    496                 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
497                         sas_ex_disable_phy(dev    497                         sas_ex_disable_phy(dev, i);
498         }                                         498         }
499 }                                                 499 }
500                                                   500 
501 static int sas_dev_present_in_domain(struct as    501 static int sas_dev_present_in_domain(struct asd_sas_port *port,
502                                             u8    502                                             u8 *sas_addr)
503 {                                                 503 {
504         struct domain_device *dev;                504         struct domain_device *dev;
505                                                   505 
506         if (SAS_ADDR(port->sas_addr) == SAS_AD    506         if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
507                 return 1;                         507                 return 1;
508         list_for_each_entry(dev, &port->dev_li    508         list_for_each_entry(dev, &port->dev_list, dev_list_node) {
509                 if (SAS_ADDR(dev->sas_addr) ==    509                 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
510                         return 1;                 510                         return 1;
511         }                                         511         }
512         return 0;                                 512         return 0;
513 }                                                 513 }
514                                                   514 
515 #define RPEL_REQ_SIZE   16                        515 #define RPEL_REQ_SIZE   16
516 #define RPEL_RESP_SIZE  32                        516 #define RPEL_RESP_SIZE  32
517 int sas_smp_get_phy_events(struct sas_phy *phy    517 int sas_smp_get_phy_events(struct sas_phy *phy)
518 {                                                 518 {
519         int res;                                  519         int res;
520         u8 *req;                                  520         u8 *req;
521         u8 *resp;                                 521         u8 *resp;
522         struct sas_rphy *rphy = dev_to_rphy(ph    522         struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
523         struct domain_device *dev = sas_find_d    523         struct domain_device *dev = sas_find_dev_by_rphy(rphy);
524                                                   524 
525         req = alloc_smp_req(RPEL_REQ_SIZE);       525         req = alloc_smp_req(RPEL_REQ_SIZE);
526         if (!req)                                 526         if (!req)
527                 return -ENOMEM;                   527                 return -ENOMEM;
528                                                   528 
529         resp = alloc_smp_resp(RPEL_RESP_SIZE);    529         resp = alloc_smp_resp(RPEL_RESP_SIZE);
530         if (!resp) {                              530         if (!resp) {
531                 kfree(req);                       531                 kfree(req);
532                 return -ENOMEM;                   532                 return -ENOMEM;
533         }                                         533         }
534                                                   534 
535         req[1] = SMP_REPORT_PHY_ERR_LOG;          535         req[1] = SMP_REPORT_PHY_ERR_LOG;
536         req[9] = phy->number;                     536         req[9] = phy->number;
537                                                   537 
538         res = smp_execute_task(dev, req, RPEL_    538         res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
539                                     resp, RPEL    539                                     resp, RPEL_RESP_SIZE);
540                                                   540 
541         if (!res)                                 541         if (!res)
542                 goto out;                         542                 goto out;
543                                                   543 
544         phy->invalid_dword_count = scsi_to_u32    544         phy->invalid_dword_count = scsi_to_u32(&resp[12]);
545         phy->running_disparity_error_count = s    545         phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
546         phy->loss_of_dword_sync_count = scsi_t    546         phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
547         phy->phy_reset_problem_count = scsi_to    547         phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
548                                                   548 
549  out:                                             549  out:
550         kfree(resp);                              550         kfree(resp);
551         return res;                               551         return res;
552                                                   552 
553 }                                                 553 }
554                                                   554 
555 #ifdef CONFIG_SCSI_SAS_ATA                        555 #ifdef CONFIG_SCSI_SAS_ATA
556                                                   556 
557 #define RPS_REQ_SIZE  16                          557 #define RPS_REQ_SIZE  16
558 #define RPS_RESP_SIZE 60                          558 #define RPS_RESP_SIZE 60
559                                                   559 
560 static int sas_get_report_phy_sata(struct doma    560 static int sas_get_report_phy_sata(struct domain_device *dev,
561                                           int     561                                           int phy_id,
562                                           stru    562                                           struct smp_resp *rps_resp)
563 {                                                 563 {
564         int res;                                  564         int res;
565         u8 *rps_req = alloc_smp_req(RPS_REQ_SI    565         u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
566         u8 *resp = (u8 *)rps_resp;                566         u8 *resp = (u8 *)rps_resp;
567                                                   567 
568         if (!rps_req)                             568         if (!rps_req)
569                 return -ENOMEM;                   569                 return -ENOMEM;
570                                                   570 
571         rps_req[1] = SMP_REPORT_PHY_SATA;         571         rps_req[1] = SMP_REPORT_PHY_SATA;
572         rps_req[9] = phy_id;                      572         rps_req[9] = phy_id;
573                                                   573 
574         res = smp_execute_task(dev, rps_req, R    574         res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
575                                     rps_resp,     575                                     rps_resp, RPS_RESP_SIZE);
576                                                   576 
577         /* 0x34 is the FIS type for the D2H fi    577         /* 0x34 is the FIS type for the D2H fis.  There's a potential
578          * standards cockup here.  sas-2 expli    578          * standards cockup here.  sas-2 explicitly specifies the FIS
579          * should be encoded so that FIS type     579          * should be encoded so that FIS type is in resp[24].
580          * However, some expanders endian reve    580          * However, some expanders endian reverse this.  Undo the
581          * reversal here */                       581          * reversal here */
582         if (!res && resp[27] == 0x34 && resp[2    582         if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
583                 int i;                            583                 int i;
584                                                   584 
585                 for (i = 0; i < 5; i++) {         585                 for (i = 0; i < 5; i++) {
586                         int j = 24 + (i*4);       586                         int j = 24 + (i*4);
587                         u8 a, b;                  587                         u8 a, b;
588                         a = resp[j + 0];          588                         a = resp[j + 0];
589                         b = resp[j + 1];          589                         b = resp[j + 1];
590                         resp[j + 0] = resp[j +    590                         resp[j + 0] = resp[j + 3];
591                         resp[j + 1] = resp[j +    591                         resp[j + 1] = resp[j + 2];
592                         resp[j + 2] = b;          592                         resp[j + 2] = b;
593                         resp[j + 3] = a;          593                         resp[j + 3] = a;
594                 }                                 594                 }
595         }                                         595         }
596                                                   596 
597         kfree(rps_req);                           597         kfree(rps_req);
598         return res;                               598         return res;
599 }                                                 599 }
600 #endif                                            600 #endif
601                                                   601 
602 static void sas_ex_get_linkrate(struct domain_    602 static void sas_ex_get_linkrate(struct domain_device *parent,
603                                        struct     603                                        struct domain_device *child,
604                                        struct     604                                        struct ex_phy *parent_phy)
605 {                                                 605 {
606         struct expander_device *parent_ex = &p    606         struct expander_device *parent_ex = &parent->ex_dev;
607         struct sas_port *port;                    607         struct sas_port *port;
608         int i;                                    608         int i;
609                                                   609 
610         child->pathways = 0;                      610         child->pathways = 0;
611                                                   611 
612         port = parent_phy->port;                  612         port = parent_phy->port;
613                                                   613 
614         for (i = 0; i < parent_ex->num_phys; i    614         for (i = 0; i < parent_ex->num_phys; i++) {
615                 struct ex_phy *phy = &parent_e    615                 struct ex_phy *phy = &parent_ex->ex_phy[i];
616                                                   616 
617                 if (phy->phy_state == PHY_VACA    617                 if (phy->phy_state == PHY_VACANT ||
618                     phy->phy_state == PHY_NOT_    618                     phy->phy_state == PHY_NOT_PRESENT)
619                         continue;                 619                         continue;
620                                                   620 
621                 if (SAS_ADDR(phy->attached_sas    621                 if (SAS_ADDR(phy->attached_sas_addr) ==
622                     SAS_ADDR(child->sas_addr))    622                     SAS_ADDR(child->sas_addr)) {
623                                                   623 
624                         child->min_linkrate =     624                         child->min_linkrate = min(parent->min_linkrate,
625                                                   625                                                   phy->linkrate);
626                         child->max_linkrate =     626                         child->max_linkrate = max(parent->max_linkrate,
627                                                   627                                                   phy->linkrate);
628                         child->pathways++;        628                         child->pathways++;
629                         sas_port_add_phy(port,    629                         sas_port_add_phy(port, phy->phy);
630                 }                                 630                 }
631         }                                         631         }
632         child->linkrate = min(parent_phy->link    632         child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
633         child->pathways = min(child->pathways,    633         child->pathways = min(child->pathways, parent->pathways);
634 }                                                 634 }
635                                                   635 
636 static struct domain_device *sas_ex_discover_e    636 static struct domain_device *sas_ex_discover_end_dev(
637         struct domain_device *parent, int phy_    637         struct domain_device *parent, int phy_id)
638 {                                                 638 {
639         struct expander_device *parent_ex = &p    639         struct expander_device *parent_ex = &parent->ex_dev;
640         struct ex_phy *phy = &parent_ex->ex_ph    640         struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
641         struct domain_device *child = NULL;       641         struct domain_device *child = NULL;
642         struct sas_rphy *rphy;                    642         struct sas_rphy *rphy;
643         int res;                                  643         int res;
644                                                   644 
645         if (phy->attached_sata_host || phy->at    645         if (phy->attached_sata_host || phy->attached_sata_ps)
646                 return NULL;                      646                 return NULL;
647                                                   647 
648         child = kzalloc(sizeof(*child), GFP_KE    648         child = kzalloc(sizeof(*child), GFP_KERNEL);
649         if (!child)                               649         if (!child)
650                 return NULL;                      650                 return NULL;
651                                                   651 
652         child->parent = parent;                   652         child->parent = parent;
653         child->port   = parent->port;             653         child->port   = parent->port;
654         child->iproto = phy->attached_iproto;     654         child->iproto = phy->attached_iproto;
655         memcpy(child->sas_addr, phy->attached_    655         memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
656         sas_hash_addr(child->hashed_sas_addr,     656         sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
657         if (!phy->port) {                         657         if (!phy->port) {
658                 phy->port = sas_port_alloc(&pa    658                 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
659                 if (unlikely(!phy->port))         659                 if (unlikely(!phy->port))
660                         goto out_err;             660                         goto out_err;
661                 if (unlikely(sas_port_add(phy-    661                 if (unlikely(sas_port_add(phy->port) != 0)) {
662                         sas_port_free(phy->por    662                         sas_port_free(phy->port);
663                         goto out_err;             663                         goto out_err;
664                 }                                 664                 }
665         }                                         665         }
666         sas_ex_get_linkrate(parent, child, phy    666         sas_ex_get_linkrate(parent, child, phy);
667                                                   667 
668 #ifdef CONFIG_SCSI_SAS_ATA                        668 #ifdef CONFIG_SCSI_SAS_ATA
669         if ((phy->attached_tproto & SAS_PROTOC    669         if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
670                 child->dev_type = SATA_DEV;       670                 child->dev_type = SATA_DEV;
671                 if (phy->attached_tproto & SAS    671                 if (phy->attached_tproto & SAS_PROTOCOL_STP)
672                         child->tproto = phy->a    672                         child->tproto = phy->attached_tproto;
673                 if (phy->attached_sata_dev)       673                 if (phy->attached_sata_dev)
674                         child->tproto |= SATA_    674                         child->tproto |= SATA_DEV;
675                 res = sas_get_report_phy_sata(    675                 res = sas_get_report_phy_sata(parent, phy_id,
676                                                   676                                               &child->sata_dev.rps_resp);
677                 if (res) {                        677                 if (res) {
678                         SAS_DPRINTK("report ph    678                         SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
679                                     "0x%x\n",     679                                     "0x%x\n", SAS_ADDR(parent->sas_addr),
680                                     phy_id, re    680                                     phy_id, res);
681                         goto out_free;            681                         goto out_free;
682                 }                                 682                 }
683                 memcpy(child->frame_rcvd, &chi    683                 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
684                        sizeof(struct dev_to_ho    684                        sizeof(struct dev_to_host_fis));
685                                                   685 
686                 rphy = sas_end_device_alloc(ph    686                 rphy = sas_end_device_alloc(phy->port);
687                 if (unlikely(!rphy))              687                 if (unlikely(!rphy))
688                         goto out_free;            688                         goto out_free;
689                                                   689 
690                 sas_init_dev(child);              690                 sas_init_dev(child);
691                                                   691 
692                 child->rphy = rphy;               692                 child->rphy = rphy;
693                                                   693 
694                 spin_lock_irq(&parent->port->d    694                 spin_lock_irq(&parent->port->dev_list_lock);
695                 list_add_tail(&child->dev_list    695                 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
696                 spin_unlock_irq(&parent->port-    696                 spin_unlock_irq(&parent->port->dev_list_lock);
697                                                   697 
698                 res = sas_discover_sata(child)    698                 res = sas_discover_sata(child);
699                 if (res) {                        699                 if (res) {
700                         SAS_DPRINTK("sas_disco    700                         SAS_DPRINTK("sas_discover_sata() for device %16llx at "
701                                     "%016llx:0    701                                     "%016llx:0x%x returned 0x%x\n",
702                                     SAS_ADDR(c    702                                     SAS_ADDR(child->sas_addr),
703                                     SAS_ADDR(p    703                                     SAS_ADDR(parent->sas_addr), phy_id, res);
704                         goto out_list_del;        704                         goto out_list_del;
705                 }                                 705                 }
706         } else                                    706         } else
707 #endif                                            707 #endif
708           if (phy->attached_tproto & SAS_PROTO    708           if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
709                 child->dev_type = SAS_END_DEV;    709                 child->dev_type = SAS_END_DEV;
710                 rphy = sas_end_device_alloc(ph    710                 rphy = sas_end_device_alloc(phy->port);
711                 /* FIXME: error handling */       711                 /* FIXME: error handling */
712                 if (unlikely(!rphy))              712                 if (unlikely(!rphy))
713                         goto out_free;            713                         goto out_free;
714                 child->tproto = phy->attached_    714                 child->tproto = phy->attached_tproto;
715                 sas_init_dev(child);              715                 sas_init_dev(child);
716                                                   716 
717                 child->rphy = rphy;               717                 child->rphy = rphy;
718                 sas_fill_in_rphy(child, rphy);    718                 sas_fill_in_rphy(child, rphy);
719                                                   719 
720                 spin_lock_irq(&parent->port->d    720                 spin_lock_irq(&parent->port->dev_list_lock);
721                 list_add_tail(&child->dev_list    721                 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
722                 spin_unlock_irq(&parent->port-    722                 spin_unlock_irq(&parent->port->dev_list_lock);
723                                                   723 
724                 res = sas_discover_end_dev(chi    724                 res = sas_discover_end_dev(child);
725                 if (res) {                        725                 if (res) {
726                         SAS_DPRINTK("sas_disco    726                         SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
727                                     "at %016ll    727                                     "at %016llx:0x%x returned 0x%x\n",
728                                     SAS_ADDR(c    728                                     SAS_ADDR(child->sas_addr),
729                                     SAS_ADDR(p    729                                     SAS_ADDR(parent->sas_addr), phy_id, res);
730                         goto out_list_del;        730                         goto out_list_del;
731                 }                                 731                 }
732         } else {                                  732         } else {
733                 SAS_DPRINTK("target proto 0x%x    733                 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
734                             phy->attached_tpro    734                             phy->attached_tproto, SAS_ADDR(parent->sas_addr),
735                             phy_id);              735                             phy_id);
736                 goto out_free;                    736                 goto out_free;
737         }                                         737         }
738                                                   738 
739         list_add_tail(&child->siblings, &paren    739         list_add_tail(&child->siblings, &parent_ex->children);
740         return child;                             740         return child;
741                                                   741 
742  out_list_del:                                    742  out_list_del:
743         sas_rphy_free(child->rphy);               743         sas_rphy_free(child->rphy);
744         child->rphy = NULL;                       744         child->rphy = NULL;
745         list_del(&child->dev_list_node);          745         list_del(&child->dev_list_node);
746  out_free:                                        746  out_free:
747         sas_port_delete(phy->port);               747         sas_port_delete(phy->port);
748  out_err:                                         748  out_err:
749         phy->port = NULL;                         749         phy->port = NULL;
750         kfree(child);                             750         kfree(child);
751         return NULL;                              751         return NULL;
752 }                                                 752 }
753                                                   753 
754 /* See if this phy is part of a wide port */      754 /* See if this phy is part of a wide port */
755 static int sas_ex_join_wide_port(struct domain    755 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
756 {                                                 756 {
757         struct ex_phy *phy = &parent->ex_dev.e    757         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
758         int i;                                    758         int i;
759                                                   759 
760         for (i = 0; i < parent->ex_dev.num_phy    760         for (i = 0; i < parent->ex_dev.num_phys; i++) {
761                 struct ex_phy *ephy = &parent-    761                 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
762                                                   762 
763                 if (ephy == phy)                  763                 if (ephy == phy)
764                         continue;                 764                         continue;
765                                                   765 
766                 if (!memcmp(phy->attached_sas_    766                 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
767                             SAS_ADDR_SIZE) &&     767                             SAS_ADDR_SIZE) && ephy->port) {
768                         sas_port_add_phy(ephy-    768                         sas_port_add_phy(ephy->port, phy->phy);
769                         phy->port = ephy->port << 
770                         phy->phy_state = PHY_D    769                         phy->phy_state = PHY_DEVICE_DISCOVERED;
771                         return 0;                 770                         return 0;
772                 }                                 771                 }
773         }                                         772         }
774                                                   773 
775         return -ENODEV;                           774         return -ENODEV;
776 }                                                 775 }
777                                                   776 
778 static struct domain_device *sas_ex_discover_e    777 static struct domain_device *sas_ex_discover_expander(
779         struct domain_device *parent, int phy_    778         struct domain_device *parent, int phy_id)
780 {                                                 779 {
781         struct sas_expander_device *parent_ex     780         struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
782         struct ex_phy *phy = &parent->ex_dev.e    781         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
783         struct domain_device *child = NULL;       782         struct domain_device *child = NULL;
784         struct sas_rphy *rphy;                    783         struct sas_rphy *rphy;
785         struct sas_expander_device *edev;         784         struct sas_expander_device *edev;
786         struct asd_sas_port *port;                785         struct asd_sas_port *port;
787         int res;                                  786         int res;
788                                                   787 
789         if (phy->routing_attr == DIRECT_ROUTIN    788         if (phy->routing_attr == DIRECT_ROUTING) {
790                 SAS_DPRINTK("ex %016llx:0x%x:D    789                 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
791                             "allowed\n",          790                             "allowed\n",
792                             SAS_ADDR(parent->s    791                             SAS_ADDR(parent->sas_addr), phy_id,
793                             SAS_ADDR(phy->atta    792                             SAS_ADDR(phy->attached_sas_addr),
794                             phy->attached_phy_    793                             phy->attached_phy_id);
795                 return NULL;                      794                 return NULL;
796         }                                         795         }
797         child = kzalloc(sizeof(*child), GFP_KE    796         child = kzalloc(sizeof(*child), GFP_KERNEL);
798         if (!child)                               797         if (!child)
799                 return NULL;                      798                 return NULL;
800                                                   799 
801         phy->port = sas_port_alloc(&parent->rp    800         phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
802         /* FIXME: better error handling */        801         /* FIXME: better error handling */
803         BUG_ON(sas_port_add(phy->port) != 0);     802         BUG_ON(sas_port_add(phy->port) != 0);
804                                                   803 
805                                                   804 
806         switch (phy->attached_dev_type) {         805         switch (phy->attached_dev_type) {
807         case EDGE_DEV:                            806         case EDGE_DEV:
808                 rphy = sas_expander_alloc(phy-    807                 rphy = sas_expander_alloc(phy->port,
809                                           SAS_    808                                           SAS_EDGE_EXPANDER_DEVICE);
810                 break;                            809                 break;
811         case FANOUT_DEV:                          810         case FANOUT_DEV:
812                 rphy = sas_expander_alloc(phy-    811                 rphy = sas_expander_alloc(phy->port,
813                                           SAS_    812                                           SAS_FANOUT_EXPANDER_DEVICE);
814                 break;                            813                 break;
815         default:                                  814         default:
816                 rphy = NULL;    /* shut gcc up    815                 rphy = NULL;    /* shut gcc up */
817                 BUG();                            816                 BUG();
818         }                                         817         }
819         port = parent->port;                      818         port = parent->port;
820         child->rphy = rphy;                       819         child->rphy = rphy;
821         edev = rphy_to_expander_device(rphy);     820         edev = rphy_to_expander_device(rphy);
822         child->dev_type = phy->attached_dev_ty    821         child->dev_type = phy->attached_dev_type;
823         child->parent = parent;                   822         child->parent = parent;
824         child->port = port;                       823         child->port = port;
825         child->iproto = phy->attached_iproto;     824         child->iproto = phy->attached_iproto;
826         child->tproto = phy->attached_tproto;     825         child->tproto = phy->attached_tproto;
827         memcpy(child->sas_addr, phy->attached_    826         memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
828         sas_hash_addr(child->hashed_sas_addr,     827         sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
829         sas_ex_get_linkrate(parent, child, phy    828         sas_ex_get_linkrate(parent, child, phy);
830         edev->level = parent_ex->level + 1;       829         edev->level = parent_ex->level + 1;
831         parent->port->disc.max_level = max(par    830         parent->port->disc.max_level = max(parent->port->disc.max_level,
832                                            ede    831                                            edev->level);
833         sas_init_dev(child);                      832         sas_init_dev(child);
834         sas_fill_in_rphy(child, rphy);            833         sas_fill_in_rphy(child, rphy);
835         sas_rphy_add(rphy);                       834         sas_rphy_add(rphy);
836                                                   835 
837         spin_lock_irq(&parent->port->dev_list_    836         spin_lock_irq(&parent->port->dev_list_lock);
838         list_add_tail(&child->dev_list_node, &    837         list_add_tail(&child->dev_list_node, &parent->port->dev_list);
839         spin_unlock_irq(&parent->port->dev_lis    838         spin_unlock_irq(&parent->port->dev_list_lock);
840                                                   839 
841         res = sas_discover_expander(child);       840         res = sas_discover_expander(child);
842         if (res) {                                841         if (res) {
843                 kfree(child);                     842                 kfree(child);
844                 return NULL;                      843                 return NULL;
845         }                                         844         }
846         list_add_tail(&child->siblings, &paren    845         list_add_tail(&child->siblings, &parent->ex_dev.children);
847         return child;                             846         return child;
848 }                                                 847 }
849                                                   848 
850 static int sas_ex_discover_dev(struct domain_d    849 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
851 {                                                 850 {
852         struct expander_device *ex = &dev->ex_    851         struct expander_device *ex = &dev->ex_dev;
853         struct ex_phy *ex_phy = &ex->ex_phy[ph    852         struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
854         struct domain_device *child = NULL;       853         struct domain_device *child = NULL;
855         int res = 0;                              854         int res = 0;
856                                                   855 
857         /* Phy state */                           856         /* Phy state */
858         if (ex_phy->linkrate == SAS_SATA_SPINU    857         if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
859                 if (!sas_smp_phy_control(dev,     858                 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
860                         res = sas_ex_phy_disco    859                         res = sas_ex_phy_discover(dev, phy_id);
861                 if (res)                          860                 if (res)
862                         return res;               861                         return res;
863         }                                         862         }
864                                                   863 
865         /* Parent and domain coherency */         864         /* Parent and domain coherency */
866         if (!dev->parent && (SAS_ADDR(ex_phy->    865         if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
867                              SAS_ADDR(dev->por    866                              SAS_ADDR(dev->port->sas_addr))) {
868                 sas_add_parent_port(dev, phy_i    867                 sas_add_parent_port(dev, phy_id);
869                 return 0;                         868                 return 0;
870         }                                         869         }
871         if (dev->parent && (SAS_ADDR(ex_phy->a    870         if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
872                             SAS_ADDR(dev->pare    871                             SAS_ADDR(dev->parent->sas_addr))) {
873                 sas_add_parent_port(dev, phy_i    872                 sas_add_parent_port(dev, phy_id);
874                 if (ex_phy->routing_attr == TA    873                 if (ex_phy->routing_attr == TABLE_ROUTING)
875                         sas_configure_phy(dev,    874                         sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
876                 return 0;                         875                 return 0;
877         }                                         876         }
878                                                   877 
879         if (sas_dev_present_in_domain(dev->por    878         if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
880                 sas_ex_disable_port(dev, ex_ph    879                 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
881                                                   880 
882         if (ex_phy->attached_dev_type == NO_DE    881         if (ex_phy->attached_dev_type == NO_DEVICE) {
883                 if (ex_phy->routing_attr == DI    882                 if (ex_phy->routing_attr == DIRECT_ROUTING) {
884                         memset(ex_phy->attache    883                         memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
885                         sas_configure_routing(    884                         sas_configure_routing(dev, ex_phy->attached_sas_addr);
886                 }                                 885                 }
887                 return 0;                         886                 return 0;
888         } else if (ex_phy->linkrate == SAS_LIN    887         } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
889                 return 0;                         888                 return 0;
890                                                   889 
891         if (ex_phy->attached_dev_type != SAS_E    890         if (ex_phy->attached_dev_type != SAS_END_DEV &&
892             ex_phy->attached_dev_type != FANOU    891             ex_phy->attached_dev_type != FANOUT_DEV &&
893             ex_phy->attached_dev_type != EDGE_    892             ex_phy->attached_dev_type != EDGE_DEV) {
894                 SAS_DPRINTK("unknown device ty    893                 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
895                             "phy 0x%x\n", ex_p    894                             "phy 0x%x\n", ex_phy->attached_dev_type,
896                             SAS_ADDR(dev->sas_    895                             SAS_ADDR(dev->sas_addr),
897                             phy_id);              896                             phy_id);
898                 return 0;                         897                 return 0;
899         }                                         898         }
900                                                   899 
901         res = sas_configure_routing(dev, ex_ph    900         res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
902         if (res) {                                901         if (res) {
903                 SAS_DPRINTK("configure routing    902                 SAS_DPRINTK("configure routing for dev %016llx "
904                             "reported 0x%x. Fo    903                             "reported 0x%x. Forgotten\n",
905                             SAS_ADDR(ex_phy->a    904                             SAS_ADDR(ex_phy->attached_sas_addr), res);
906                 sas_disable_routing(dev, ex_ph    905                 sas_disable_routing(dev, ex_phy->attached_sas_addr);
907                 return res;                       906                 return res;
908         }                                         907         }
909                                                   908 
910         res = sas_ex_join_wide_port(dev, phy_i    909         res = sas_ex_join_wide_port(dev, phy_id);
911         if (!res) {                               910         if (!res) {
912                 SAS_DPRINTK("Attaching ex phy%    911                 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
913                             phy_id, SAS_ADDR(e    912                             phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
914                 return res;                       913                 return res;
915         }                                         914         }
916                                                   915 
917         switch (ex_phy->attached_dev_type) {      916         switch (ex_phy->attached_dev_type) {
918         case SAS_END_DEV:                         917         case SAS_END_DEV:
919                 child = sas_ex_discover_end_de    918                 child = sas_ex_discover_end_dev(dev, phy_id);
920                 break;                            919                 break;
921         case FANOUT_DEV:                          920         case FANOUT_DEV:
922                 if (SAS_ADDR(dev->port->disc.f    921                 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
923                         SAS_DPRINTK("second fa    922                         SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
924                                     "attached     923                                     "attached to ex %016llx phy 0x%x\n",
925                                     SAS_ADDR(e    924                                     SAS_ADDR(ex_phy->attached_sas_addr),
926                                     ex_phy->at    925                                     ex_phy->attached_phy_id,
927                                     SAS_ADDR(d    926                                     SAS_ADDR(dev->sas_addr),
928                                     phy_id);      927                                     phy_id);
929                         sas_ex_disable_phy(dev    928                         sas_ex_disable_phy(dev, phy_id);
930                         break;                    929                         break;
931                 } else                            930                 } else
932                         memcpy(dev->port->disc    931                         memcpy(dev->port->disc.fanout_sas_addr,
933                                ex_phy->attache    932                                ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
934                 /* fallthrough */                 933                 /* fallthrough */
935         case EDGE_DEV:                            934         case EDGE_DEV:
936                 child = sas_ex_discover_expand    935                 child = sas_ex_discover_expander(dev, phy_id);
937                 break;                            936                 break;
938         default:                                  937         default:
939                 break;                            938                 break;
940         }                                         939         }
941                                                   940 
942         if (child) {                              941         if (child) {
943                 int i;                            942                 int i;
944                                                   943 
945                 for (i = 0; i < ex->num_phys;     944                 for (i = 0; i < ex->num_phys; i++) {
946                         if (ex->ex_phy[i].phy_    945                         if (ex->ex_phy[i].phy_state == PHY_VACANT ||
947                             ex->ex_phy[i].phy_    946                             ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
948                                 continue;         947                                 continue;
949                         /*                     !! 948 
950                          * Due to races, the p << 
951                          * wide port, so we ad << 
952                          */                    << 
953                         if (SAS_ADDR(ex->ex_ph    949                         if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
954                             SAS_ADDR(child->sa !! 950                             SAS_ADDR(child->sas_addr))
955                                 ex->ex_phy[i].    951                                 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
956                                 res = sas_ex_j << 
957                                 if (!res)      << 
958                                         SAS_DP << 
959                                                << 
960                                                << 
961                         }                      << 
962                 }                                 952                 }
963                 res = 0;                       << 
964         }                                         953         }
965                                                   954 
966         return res;                               955         return res;
967 }                                                 956 }
968                                                   957 
969 static int sas_find_sub_addr(struct domain_dev    958 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
970 {                                                 959 {
971         struct expander_device *ex = &dev->ex_    960         struct expander_device *ex = &dev->ex_dev;
972         int i;                                    961         int i;
973                                                   962 
974         for (i = 0; i < ex->num_phys; i++) {      963         for (i = 0; i < ex->num_phys; i++) {
975                 struct ex_phy *phy = &ex->ex_p    964                 struct ex_phy *phy = &ex->ex_phy[i];
976                                                   965 
977                 if (phy->phy_state == PHY_VACA    966                 if (phy->phy_state == PHY_VACANT ||
978                     phy->phy_state == PHY_NOT_    967                     phy->phy_state == PHY_NOT_PRESENT)
979                         continue;                 968                         continue;
980                                                   969 
981                 if ((phy->attached_dev_type ==    970                 if ((phy->attached_dev_type == EDGE_DEV ||
982                      phy->attached_dev_type ==    971                      phy->attached_dev_type == FANOUT_DEV) &&
983                     phy->routing_attr == SUBTR    972                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
984                                                   973 
985                         memcpy(sub_addr, phy->    974                         memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
986                                                   975 
987                         return 1;                 976                         return 1;
988                 }                                 977                 }
989         }                                         978         }
990         return 0;                                 979         return 0;
991 }                                                 980 }
992                                                   981 
993 static int sas_check_level_subtractive_boundar    982 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
994 {                                                 983 {
995         struct expander_device *ex = &dev->ex_    984         struct expander_device *ex = &dev->ex_dev;
996         struct domain_device *child;              985         struct domain_device *child;
997         u8 sub_addr[8] = {0, };                   986         u8 sub_addr[8] = {0, };
998                                                   987 
999         list_for_each_entry(child, &ex->childr    988         list_for_each_entry(child, &ex->children, siblings) {
1000                 if (child->dev_type != EDGE_D    989                 if (child->dev_type != EDGE_DEV &&
1001                     child->dev_type != FANOUT    990                     child->dev_type != FANOUT_DEV)
1002                         continue;                991                         continue;
1003                 if (sub_addr[0] == 0) {          992                 if (sub_addr[0] == 0) {
1004                         sas_find_sub_addr(chi    993                         sas_find_sub_addr(child, sub_addr);
1005                         continue;                994                         continue;
1006                 } else {                         995                 } else {
1007                         u8 s2[8];                996                         u8 s2[8];
1008                                                  997 
1009                         if (sas_find_sub_addr    998                         if (sas_find_sub_addr(child, s2) &&
1010                             (SAS_ADDR(sub_add    999                             (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1011                                                  1000 
1012                                 SAS_DPRINTK("    1001                                 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1013                                             "    1002                                             "diverges from subtractive "
1014                                             "    1003                                             "boundary %016llx\n",
1015                                             S    1004                                             SAS_ADDR(dev->sas_addr),
1016                                             S    1005                                             SAS_ADDR(child->sas_addr),
1017                                             S    1006                                             SAS_ADDR(s2),
1018                                             S    1007                                             SAS_ADDR(sub_addr));
1019                                                  1008 
1020                                 sas_ex_disabl    1009                                 sas_ex_disable_port(child, s2);
1021                         }                        1010                         }
1022                 }                                1011                 }
1023         }                                        1012         }
1024         return 0;                                1013         return 0;
1025 }                                                1014 }
1026 /**                                              1015 /**
1027  * sas_ex_discover_devices -- discover device    1016  * sas_ex_discover_devices -- discover devices attached to this expander
1028  * dev: pointer to the expander domain device    1017  * dev: pointer to the expander domain device
1029  * single: if you want to do a single phy, el    1018  * single: if you want to do a single phy, else set to -1;
1030  *                                               1019  *
1031  * Configure this expander for use with its d    1020  * Configure this expander for use with its devices and register the
1032  * devices of this expander.                     1021  * devices of this expander.
1033  */                                              1022  */
1034 static int sas_ex_discover_devices(struct dom    1023 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1035 {                                                1024 {
1036         struct expander_device *ex = &dev->ex    1025         struct expander_device *ex = &dev->ex_dev;
1037         int i = 0, end = ex->num_phys;           1026         int i = 0, end = ex->num_phys;
1038         int res = 0;                             1027         int res = 0;
1039                                                  1028 
1040         if (0 <= single && single < end) {       1029         if (0 <= single && single < end) {
1041                 i = single;                      1030                 i = single;
1042                 end = i+1;                       1031                 end = i+1;
1043         }                                        1032         }
1044                                                  1033 
1045         for ( ; i < end; i++) {                  1034         for ( ; i < end; i++) {
1046                 struct ex_phy *ex_phy = &ex->    1035                 struct ex_phy *ex_phy = &ex->ex_phy[i];
1047                                                  1036 
1048                 if (ex_phy->phy_state == PHY_    1037                 if (ex_phy->phy_state == PHY_VACANT ||
1049                     ex_phy->phy_state == PHY_    1038                     ex_phy->phy_state == PHY_NOT_PRESENT ||
1050                     ex_phy->phy_state == PHY_    1039                     ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1051                         continue;                1040                         continue;
1052                                                  1041 
1053                 switch (ex_phy->linkrate) {      1042                 switch (ex_phy->linkrate) {
1054                 case SAS_PHY_DISABLED:           1043                 case SAS_PHY_DISABLED:
1055                 case SAS_PHY_RESET_PROBLEM:      1044                 case SAS_PHY_RESET_PROBLEM:
1056                 case SAS_SATA_PORT_SELECTOR:     1045                 case SAS_SATA_PORT_SELECTOR:
1057                         continue;                1046                         continue;
1058                 default:                         1047                 default:
1059                         res = sas_ex_discover    1048                         res = sas_ex_discover_dev(dev, i);
1060                         if (res)                 1049                         if (res)
1061                                 break;           1050                                 break;
1062                         continue;                1051                         continue;
1063                 }                                1052                 }
1064         }                                        1053         }
1065                                                  1054 
1066         if (!res)                                1055         if (!res)
1067                 sas_check_level_subtractive_b    1056                 sas_check_level_subtractive_boundary(dev);
1068                                                  1057 
1069         return res;                              1058         return res;
1070 }                                                1059 }
1071                                                  1060 
1072 static int sas_check_ex_subtractive_boundary(    1061 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1073 {                                                1062 {
1074         struct expander_device *ex = &dev->ex    1063         struct expander_device *ex = &dev->ex_dev;
1075         int i;                                   1064         int i;
1076         u8  *sub_sas_addr = NULL;                1065         u8  *sub_sas_addr = NULL;
1077                                                  1066 
1078         if (dev->dev_type != EDGE_DEV)           1067         if (dev->dev_type != EDGE_DEV)
1079                 return 0;                        1068                 return 0;
1080                                                  1069 
1081         for (i = 0; i < ex->num_phys; i++) {     1070         for (i = 0; i < ex->num_phys; i++) {
1082                 struct ex_phy *phy = &ex->ex_    1071                 struct ex_phy *phy = &ex->ex_phy[i];
1083                                                  1072 
1084                 if (phy->phy_state == PHY_VAC    1073                 if (phy->phy_state == PHY_VACANT ||
1085                     phy->phy_state == PHY_NOT    1074                     phy->phy_state == PHY_NOT_PRESENT)
1086                         continue;                1075                         continue;
1087                                                  1076 
1088                 if ((phy->attached_dev_type =    1077                 if ((phy->attached_dev_type == FANOUT_DEV ||
1089                      phy->attached_dev_type =    1078                      phy->attached_dev_type == EDGE_DEV) &&
1090                     phy->routing_attr == SUBT    1079                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
1091                                                  1080 
1092                         if (!sub_sas_addr)       1081                         if (!sub_sas_addr)
1093                                 sub_sas_addr     1082                                 sub_sas_addr = &phy->attached_sas_addr[0];
1094                         else if (SAS_ADDR(sub    1083                         else if (SAS_ADDR(sub_sas_addr) !=
1095                                  SAS_ADDR(phy    1084                                  SAS_ADDR(phy->attached_sas_addr)) {
1096                                                  1085 
1097                                 SAS_DPRINTK("    1086                                 SAS_DPRINTK("ex %016llx phy 0x%x "
1098                                             "    1087                                             "diverges(%016llx) on subtractive "
1099                                             "    1088                                             "boundary(%016llx). Disabled\n",
1100                                             S    1089                                             SAS_ADDR(dev->sas_addr), i,
1101                                             S    1090                                             SAS_ADDR(phy->attached_sas_addr),
1102                                             S    1091                                             SAS_ADDR(sub_sas_addr));
1103                                 sas_ex_disabl    1092                                 sas_ex_disable_phy(dev, i);
1104                         }                        1093                         }
1105                 }                                1094                 }
1106         }                                        1095         }
1107         return 0;                                1096         return 0;
1108 }                                                1097 }
1109                                                  1098 
1110 static void sas_print_parent_topology_bug(str    1099 static void sas_print_parent_topology_bug(struct domain_device *child,
1111                                                  1100                                                  struct ex_phy *parent_phy,
1112                                                  1101                                                  struct ex_phy *child_phy)
1113 {                                                1102 {
1114         static const char ra_char[] = {          1103         static const char ra_char[] = {
1115                 [DIRECT_ROUTING] = 'D',          1104                 [DIRECT_ROUTING] = 'D',
1116                 [SUBTRACTIVE_ROUTING] = 'S',     1105                 [SUBTRACTIVE_ROUTING] = 'S',
1117                 [TABLE_ROUTING] = 'T',           1106                 [TABLE_ROUTING] = 'T',
1118         };                                       1107         };
1119         static const char *ex_type[] = {         1108         static const char *ex_type[] = {
1120                 [EDGE_DEV] = "edge",             1109                 [EDGE_DEV] = "edge",
1121                 [FANOUT_DEV] = "fanout",         1110                 [FANOUT_DEV] = "fanout",
1122         };                                       1111         };
1123         struct domain_device *parent = child-    1112         struct domain_device *parent = child->parent;
1124                                                  1113 
1125         sas_printk("%s ex %016llx phy 0x%x <-    1114         sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1126                    "has %c:%c routing link!\n    1115                    "has %c:%c routing link!\n",
1127                                                  1116 
1128                    ex_type[parent->dev_type],    1117                    ex_type[parent->dev_type],
1129                    SAS_ADDR(parent->sas_addr)    1118                    SAS_ADDR(parent->sas_addr),
1130                    parent_phy->phy_id,           1119                    parent_phy->phy_id,
1131                                                  1120 
1132                    ex_type[child->dev_type],     1121                    ex_type[child->dev_type],
1133                    SAS_ADDR(child->sas_addr),    1122                    SAS_ADDR(child->sas_addr),
1134                    child_phy->phy_id,            1123                    child_phy->phy_id,
1135                                                  1124 
1136                    ra_char[parent_phy->routin    1125                    ra_char[parent_phy->routing_attr],
1137                    ra_char[child_phy->routing    1126                    ra_char[child_phy->routing_attr]);
1138 }                                                1127 }
1139                                                  1128 
1140 static int sas_check_eeds(struct domain_devic    1129 static int sas_check_eeds(struct domain_device *child,
1141                                  struct ex_ph    1130                                  struct ex_phy *parent_phy,
1142                                  struct ex_ph    1131                                  struct ex_phy *child_phy)
1143 {                                                1132 {
1144         int res = 0;                             1133         int res = 0;
1145         struct domain_device *parent = child-    1134         struct domain_device *parent = child->parent;
1146                                                  1135 
1147         if (SAS_ADDR(parent->port->disc.fanou    1136         if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1148                 res = -ENODEV;                   1137                 res = -ENODEV;
1149                 SAS_DPRINTK("edge ex %016llx     1138                 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1150                             "phy S:0x%x, whil    1139                             "phy S:0x%x, while there is a fanout ex %016llx\n",
1151                             SAS_ADDR(parent->    1140                             SAS_ADDR(parent->sas_addr),
1152                             parent_phy->phy_i    1141                             parent_phy->phy_id,
1153                             SAS_ADDR(child->s    1142                             SAS_ADDR(child->sas_addr),
1154                             child_phy->phy_id    1143                             child_phy->phy_id,
1155                             SAS_ADDR(parent->    1144                             SAS_ADDR(parent->port->disc.fanout_sas_addr));
1156         } else if (SAS_ADDR(parent->port->dis    1145         } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1157                 memcpy(parent->port->disc.eed    1146                 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1158                        SAS_ADDR_SIZE);           1147                        SAS_ADDR_SIZE);
1159                 memcpy(parent->port->disc.eed    1148                 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1160                        SAS_ADDR_SIZE);           1149                        SAS_ADDR_SIZE);
1161         } else if (((SAS_ADDR(parent->port->d    1150         } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1162                     SAS_ADDR(parent->sas_addr    1151                     SAS_ADDR(parent->sas_addr)) ||
1163                    (SAS_ADDR(parent->port->di    1152                    (SAS_ADDR(parent->port->disc.eeds_a) ==
1164                     SAS_ADDR(child->sas_addr)    1153                     SAS_ADDR(child->sas_addr)))
1165                    &&                            1154                    &&
1166                    ((SAS_ADDR(parent->port->d    1155                    ((SAS_ADDR(parent->port->disc.eeds_b) ==
1167                      SAS_ADDR(parent->sas_add    1156                      SAS_ADDR(parent->sas_addr)) ||
1168                     (SAS_ADDR(parent->port->d    1157                     (SAS_ADDR(parent->port->disc.eeds_b) ==
1169                      SAS_ADDR(child->sas_addr    1158                      SAS_ADDR(child->sas_addr))))
1170                 ;                                1159                 ;
1171         else {                                   1160         else {
1172                 res = -ENODEV;                   1161                 res = -ENODEV;
1173                 SAS_DPRINTK("edge ex %016llx     1162                 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1174                             "phy 0x%x link fo    1163                             "phy 0x%x link forms a third EEDS!\n",
1175                             SAS_ADDR(parent->    1164                             SAS_ADDR(parent->sas_addr),
1176                             parent_phy->phy_i    1165                             parent_phy->phy_id,
1177                             SAS_ADDR(child->s    1166                             SAS_ADDR(child->sas_addr),
1178                             child_phy->phy_id    1167                             child_phy->phy_id);
1179         }                                        1168         }
1180                                                  1169 
1181         return res;                              1170         return res;
1182 }                                                1171 }
1183                                                  1172 
1184 /* Here we spill over 80 columns.  It is inte    1173 /* Here we spill over 80 columns.  It is intentional.
1185  */                                              1174  */
1186 static int sas_check_parent_topology(struct d    1175 static int sas_check_parent_topology(struct domain_device *child)
1187 {                                                1176 {
1188         struct expander_device *child_ex = &c    1177         struct expander_device *child_ex = &child->ex_dev;
1189         struct expander_device *parent_ex;       1178         struct expander_device *parent_ex;
1190         int i;                                   1179         int i;
1191         int res = 0;                             1180         int res = 0;
1192                                                  1181 
1193         if (!child->parent)                      1182         if (!child->parent)
1194                 return 0;                        1183                 return 0;
1195                                                  1184 
1196         if (child->parent->dev_type != EDGE_D    1185         if (child->parent->dev_type != EDGE_DEV &&
1197             child->parent->dev_type != FANOUT    1186             child->parent->dev_type != FANOUT_DEV)
1198                 return 0;                        1187                 return 0;
1199                                                  1188 
1200         parent_ex = &child->parent->ex_dev;      1189         parent_ex = &child->parent->ex_dev;
1201                                                  1190 
1202         for (i = 0; i < parent_ex->num_phys;     1191         for (i = 0; i < parent_ex->num_phys; i++) {
1203                 struct ex_phy *parent_phy = &    1192                 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1204                 struct ex_phy *child_phy;        1193                 struct ex_phy *child_phy;
1205                                                  1194 
1206                 if (parent_phy->phy_state ==     1195                 if (parent_phy->phy_state == PHY_VACANT ||
1207                     parent_phy->phy_state ==     1196                     parent_phy->phy_state == PHY_NOT_PRESENT)
1208                         continue;                1197                         continue;
1209                                                  1198 
1210                 if (SAS_ADDR(parent_phy->atta    1199                 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1211                         continue;                1200                         continue;
1212                                                  1201 
1213                 child_phy = &child_ex->ex_phy    1202                 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1214                                                  1203 
1215                 switch (child->parent->dev_ty    1204                 switch (child->parent->dev_type) {
1216                 case EDGE_DEV:                   1205                 case EDGE_DEV:
1217                         if (child->dev_type =    1206                         if (child->dev_type == FANOUT_DEV) {
1218                                 if (parent_ph    1207                                 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1219                                     child_phy    1208                                     child_phy->routing_attr != TABLE_ROUTING) {
1220                                         sas_p    1209                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1221                                         res =    1210                                         res = -ENODEV;
1222                                 }                1211                                 }
1223                         } else if (parent_phy    1212                         } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1224                                 if (child_phy    1213                                 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1225                                         res =    1214                                         res = sas_check_eeds(child, parent_phy, child_phy);
1226                                 } else if (ch    1215                                 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1227                                         sas_p    1216                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1228                                         res =    1217                                         res = -ENODEV;
1229                                 }                1218                                 }
1230                         } else if (parent_phy    1219                         } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1231                                    child_phy-    1220                                    child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1232                                 sas_print_par    1221                                 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1233                                 res = -ENODEV    1222                                 res = -ENODEV;
1234                         }                        1223                         }
1235                         break;                   1224                         break;
1236                 case FANOUT_DEV:                 1225                 case FANOUT_DEV:
1237                         if (parent_phy->routi    1226                         if (parent_phy->routing_attr != TABLE_ROUTING ||
1238                             child_phy->routin    1227                             child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1239                                 sas_print_par    1228                                 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1240                                 res = -ENODEV    1229                                 res = -ENODEV;
1241                         }                        1230                         }
1242                         break;                   1231                         break;
1243                 default:                         1232                 default:
1244                         break;                   1233                         break;
1245                 }                                1234                 }
1246         }                                        1235         }
1247                                                  1236 
1248         return res;                              1237         return res;
1249 }                                                1238 }
1250                                                  1239 
1251 #define RRI_REQ_SIZE  16                         1240 #define RRI_REQ_SIZE  16
1252 #define RRI_RESP_SIZE 44                         1241 #define RRI_RESP_SIZE 44
1253                                                  1242 
1254 static int sas_configure_present(struct domai    1243 static int sas_configure_present(struct domain_device *dev, int phy_id,
1255                                  u8 *sas_addr    1244                                  u8 *sas_addr, int *index, int *present)
1256 {                                                1245 {
1257         int i, res = 0;                          1246         int i, res = 0;
1258         struct expander_device *ex = &dev->ex    1247         struct expander_device *ex = &dev->ex_dev;
1259         struct ex_phy *phy = &ex->ex_phy[phy_    1248         struct ex_phy *phy = &ex->ex_phy[phy_id];
1260         u8 *rri_req;                             1249         u8 *rri_req;
1261         u8 *rri_resp;                            1250         u8 *rri_resp;
1262                                                  1251 
1263         *present = 0;                            1252         *present = 0;
1264         *index = 0;                              1253         *index = 0;
1265                                                  1254 
1266         rri_req = alloc_smp_req(RRI_REQ_SIZE)    1255         rri_req = alloc_smp_req(RRI_REQ_SIZE);
1267         if (!rri_req)                            1256         if (!rri_req)
1268                 return -ENOMEM;                  1257                 return -ENOMEM;
1269                                                  1258 
1270         rri_resp = alloc_smp_resp(RRI_RESP_SI    1259         rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1271         if (!rri_resp) {                         1260         if (!rri_resp) {
1272                 kfree(rri_req);                  1261                 kfree(rri_req);
1273                 return -ENOMEM;                  1262                 return -ENOMEM;
1274         }                                        1263         }
1275                                                  1264 
1276         rri_req[1] = SMP_REPORT_ROUTE_INFO;      1265         rri_req[1] = SMP_REPORT_ROUTE_INFO;
1277         rri_req[9] = phy_id;                     1266         rri_req[9] = phy_id;
1278                                                  1267 
1279         for (i = 0; i < ex->max_route_indexes    1268         for (i = 0; i < ex->max_route_indexes ; i++) {
1280                 *(__be16 *)(rri_req+6) = cpu_    1269                 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1281                 res = smp_execute_task(dev, r    1270                 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1282                                        RRI_RE    1271                                        RRI_RESP_SIZE);
1283                 if (res)                         1272                 if (res)
1284                         goto out;                1273                         goto out;
1285                 res = rri_resp[2];               1274                 res = rri_resp[2];
1286                 if (res == SMP_RESP_NO_INDEX)    1275                 if (res == SMP_RESP_NO_INDEX) {
1287                         SAS_DPRINTK("overflow    1276                         SAS_DPRINTK("overflow of indexes: dev %016llx "
1288                                     "phy 0x%x    1277                                     "phy 0x%x index 0x%x\n",
1289                                     SAS_ADDR(    1278                                     SAS_ADDR(dev->sas_addr), phy_id, i);
1290                         goto out;                1279                         goto out;
1291                 } else if (res != SMP_RESP_FU    1280                 } else if (res != SMP_RESP_FUNC_ACC) {
1292                         SAS_DPRINTK("%s: dev     1281                         SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1293                                     "result 0 !! 1282                                     "result 0x%x\n", __FUNCTION__,
1294                                     SAS_ADDR(    1283                                     SAS_ADDR(dev->sas_addr), phy_id, i, res);
1295                         goto out;                1284                         goto out;
1296                 }                                1285                 }
1297                 if (SAS_ADDR(sas_addr) != 0)     1286                 if (SAS_ADDR(sas_addr) != 0) {
1298                         if (SAS_ADDR(rri_resp    1287                         if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1299                                 *index = i;      1288                                 *index = i;
1300                                 if ((rri_resp    1289                                 if ((rri_resp[12] & 0x80) == 0x80)
1301                                         *pres    1290                                         *present = 0;
1302                                 else             1291                                 else
1303                                         *pres    1292                                         *present = 1;
1304                                 goto out;        1293                                 goto out;
1305                         } else if (SAS_ADDR(r    1294                         } else if (SAS_ADDR(rri_resp+16) == 0) {
1306                                 *index = i;      1295                                 *index = i;
1307                                 *present = 0;    1296                                 *present = 0;
1308                                 goto out;        1297                                 goto out;
1309                         }                        1298                         }
1310                 } else if (SAS_ADDR(rri_resp+    1299                 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1311                            phy->last_da_index    1300                            phy->last_da_index < i) {
1312                         phy->last_da_index =     1301                         phy->last_da_index = i;
1313                         *index = i;              1302                         *index = i;
1314                         *present = 0;            1303                         *present = 0;
1315                         goto out;                1304                         goto out;
1316                 }                                1305                 }
1317         }                                        1306         }
1318         res = -1;                                1307         res = -1;
1319 out:                                             1308 out:
1320         kfree(rri_req);                          1309         kfree(rri_req);
1321         kfree(rri_resp);                         1310         kfree(rri_resp);
1322         return res;                              1311         return res;
1323 }                                                1312 }
1324                                                  1313 
1325 #define CRI_REQ_SIZE  44                         1314 #define CRI_REQ_SIZE  44
1326 #define CRI_RESP_SIZE  8                         1315 #define CRI_RESP_SIZE  8
1327                                                  1316 
1328 static int sas_configure_set(struct domain_de    1317 static int sas_configure_set(struct domain_device *dev, int phy_id,
1329                              u8 *sas_addr, in    1318                              u8 *sas_addr, int index, int include)
1330 {                                                1319 {
1331         int res;                                 1320         int res;
1332         u8 *cri_req;                             1321         u8 *cri_req;
1333         u8 *cri_resp;                            1322         u8 *cri_resp;
1334                                                  1323 
1335         cri_req = alloc_smp_req(CRI_REQ_SIZE)    1324         cri_req = alloc_smp_req(CRI_REQ_SIZE);
1336         if (!cri_req)                            1325         if (!cri_req)
1337                 return -ENOMEM;                  1326                 return -ENOMEM;
1338                                                  1327 
1339         cri_resp = alloc_smp_resp(CRI_RESP_SI    1328         cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1340         if (!cri_resp) {                         1329         if (!cri_resp) {
1341                 kfree(cri_req);                  1330                 kfree(cri_req);
1342                 return -ENOMEM;                  1331                 return -ENOMEM;
1343         }                                        1332         }
1344                                                  1333 
1345         cri_req[1] = SMP_CONF_ROUTE_INFO;        1334         cri_req[1] = SMP_CONF_ROUTE_INFO;
1346         *(__be16 *)(cri_req+6) = cpu_to_be16(    1335         *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1347         cri_req[9] = phy_id;                     1336         cri_req[9] = phy_id;
1348         if (SAS_ADDR(sas_addr) == 0 || !inclu    1337         if (SAS_ADDR(sas_addr) == 0 || !include)
1349                 cri_req[12] |= 0x80;             1338                 cri_req[12] |= 0x80;
1350         memcpy(cri_req+16, sas_addr, SAS_ADDR    1339         memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1351                                                  1340 
1352         res = smp_execute_task(dev, cri_req,     1341         res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1353                                CRI_RESP_SIZE)    1342                                CRI_RESP_SIZE);
1354         if (res)                                 1343         if (res)
1355                 goto out;                        1344                 goto out;
1356         res = cri_resp[2];                       1345         res = cri_resp[2];
1357         if (res == SMP_RESP_NO_INDEX) {          1346         if (res == SMP_RESP_NO_INDEX) {
1358                 SAS_DPRINTK("overflow of inde    1347                 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1359                             "index 0x%x\n",      1348                             "index 0x%x\n",
1360                             SAS_ADDR(dev->sas    1349                             SAS_ADDR(dev->sas_addr), phy_id, index);
1361         }                                        1350         }
1362 out:                                             1351 out:
1363         kfree(cri_req);                          1352         kfree(cri_req);
1364         kfree(cri_resp);                         1353         kfree(cri_resp);
1365         return res;                              1354         return res;
1366 }                                                1355 }
1367                                                  1356 
1368 static int sas_configure_phy(struct domain_de    1357 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1369                                     u8 *sas_a    1358                                     u8 *sas_addr, int include)
1370 {                                                1359 {
1371         int index;                               1360         int index;
1372         int present;                             1361         int present;
1373         int res;                                 1362         int res;
1374                                                  1363 
1375         res = sas_configure_present(dev, phy_    1364         res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1376         if (res)                                 1365         if (res)
1377                 return res;                      1366                 return res;
1378         if (include ^ present)                   1367         if (include ^ present)
1379                 return sas_configure_set(dev,    1368                 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1380                                                  1369 
1381         return res;                              1370         return res;
1382 }                                                1371 }
1383                                                  1372 
1384 /**                                              1373 /**
1385  * sas_configure_parent -- configure routing     1374  * sas_configure_parent -- configure routing table of parent
1386  * parent: parent expander                       1375  * parent: parent expander
1387  * child: child expander                         1376  * child: child expander
1388  * sas_addr: SAS port identifier of device di    1377  * sas_addr: SAS port identifier of device directly attached to child
1389  */                                              1378  */
1390 static int sas_configure_parent(struct domain    1379 static int sas_configure_parent(struct domain_device *parent,
1391                                 struct domain    1380                                 struct domain_device *child,
1392                                 u8 *sas_addr,    1381                                 u8 *sas_addr, int include)
1393 {                                                1382 {
1394         struct expander_device *ex_parent = &    1383         struct expander_device *ex_parent = &parent->ex_dev;
1395         int res = 0;                             1384         int res = 0;
1396         int i;                                   1385         int i;
1397                                                  1386 
1398         if (parent->parent) {                    1387         if (parent->parent) {
1399                 res = sas_configure_parent(pa    1388                 res = sas_configure_parent(parent->parent, parent, sas_addr,
1400                                            in    1389                                            include);
1401                 if (res)                         1390                 if (res)
1402                         return res;              1391                         return res;
1403         }                                        1392         }
1404                                                  1393 
1405         if (ex_parent->conf_route_table == 0)    1394         if (ex_parent->conf_route_table == 0) {
1406                 SAS_DPRINTK("ex %016llx has s    1395                 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1407                             SAS_ADDR(parent->    1396                             SAS_ADDR(parent->sas_addr));
1408                 return 0;                        1397                 return 0;
1409         }                                        1398         }
1410                                                  1399 
1411         for (i = 0; i < ex_parent->num_phys;     1400         for (i = 0; i < ex_parent->num_phys; i++) {
1412                 struct ex_phy *phy = &ex_pare    1401                 struct ex_phy *phy = &ex_parent->ex_phy[i];
1413                                                  1402 
1414                 if ((phy->routing_attr == TAB    1403                 if ((phy->routing_attr == TABLE_ROUTING) &&
1415                     (SAS_ADDR(phy->attached_s    1404                     (SAS_ADDR(phy->attached_sas_addr) ==
1416                      SAS_ADDR(child->sas_addr    1405                      SAS_ADDR(child->sas_addr))) {
1417                         res = sas_configure_p    1406                         res = sas_configure_phy(parent, i, sas_addr, include);
1418                         if (res)                 1407                         if (res)
1419                                 return res;      1408                                 return res;
1420                 }                                1409                 }
1421         }                                        1410         }
1422                                                  1411 
1423         return res;                              1412         return res;
1424 }                                                1413 }
1425                                                  1414 
1426 /**                                              1415 /**
1427  * sas_configure_routing -- configure routing    1416  * sas_configure_routing -- configure routing
1428  * dev: expander device                          1417  * dev: expander device
1429  * sas_addr: port identifier of device direct    1418  * sas_addr: port identifier of device directly attached to the expander device
1430  */                                              1419  */
1431 static int sas_configure_routing(struct domai    1420 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1432 {                                                1421 {
1433         if (dev->parent)                         1422         if (dev->parent)
1434                 return sas_configure_parent(d    1423                 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1435         return 0;                                1424         return 0;
1436 }                                                1425 }
1437                                                  1426 
1438 static int sas_disable_routing(struct domain_    1427 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr)
1439 {                                                1428 {
1440         if (dev->parent)                         1429         if (dev->parent)
1441                 return sas_configure_parent(d    1430                 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1442         return 0;                                1431         return 0;
1443 }                                                1432 }
1444                                                  1433 
1445 /**                                              1434 /**
1446  * sas_discover_expander -- expander discover    1435  * sas_discover_expander -- expander discovery
1447  * @ex: pointer to expander domain device        1436  * @ex: pointer to expander domain device
1448  *                                               1437  *
1449  * See comment in sas_discover_sata().           1438  * See comment in sas_discover_sata().
1450  */                                              1439  */
1451 static int sas_discover_expander(struct domai    1440 static int sas_discover_expander(struct domain_device *dev)
1452 {                                                1441 {
1453         int res;                                 1442         int res;
1454                                                  1443 
1455         res = sas_notify_lldd_dev_found(dev);    1444         res = sas_notify_lldd_dev_found(dev);
1456         if (res)                                 1445         if (res)
1457                 return res;                      1446                 return res;
1458                                                  1447 
1459         res = sas_ex_general(dev);               1448         res = sas_ex_general(dev);
1460         if (res)                                 1449         if (res)
1461                 goto out_err;                    1450                 goto out_err;
1462         res = sas_ex_manuf_info(dev);            1451         res = sas_ex_manuf_info(dev);
1463         if (res)                                 1452         if (res)
1464                 goto out_err;                    1453                 goto out_err;
1465                                                  1454 
1466         res = sas_expander_discover(dev);        1455         res = sas_expander_discover(dev);
1467         if (res) {                               1456         if (res) {
1468                 SAS_DPRINTK("expander %016llx    1457                 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1469                             SAS_ADDR(dev->sas    1458                             SAS_ADDR(dev->sas_addr), res);
1470                 goto out_err;                    1459                 goto out_err;
1471         }                                        1460         }
1472                                                  1461 
1473         sas_check_ex_subtractive_boundary(dev    1462         sas_check_ex_subtractive_boundary(dev);
1474         res = sas_check_parent_topology(dev);    1463         res = sas_check_parent_topology(dev);
1475         if (res)                                 1464         if (res)
1476                 goto out_err;                    1465                 goto out_err;
1477         return 0;                                1466         return 0;
1478 out_err:                                         1467 out_err:
1479         sas_notify_lldd_dev_gone(dev);           1468         sas_notify_lldd_dev_gone(dev);
1480         return res;                              1469         return res;
1481 }                                                1470 }
1482                                                  1471 
1483 static int sas_ex_level_discovery(struct asd_    1472 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1484 {                                                1473 {
1485         int res = 0;                             1474         int res = 0;
1486         struct domain_device *dev;               1475         struct domain_device *dev;
1487                                                  1476 
1488         list_for_each_entry(dev, &port->dev_l    1477         list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1489                 if (dev->dev_type == EDGE_DEV    1478                 if (dev->dev_type == EDGE_DEV ||
1490                     dev->dev_type == FANOUT_D    1479                     dev->dev_type == FANOUT_DEV) {
1491                         struct sas_expander_d    1480                         struct sas_expander_device *ex =
1492                                 rphy_to_expan    1481                                 rphy_to_expander_device(dev->rphy);
1493                                                  1482 
1494                         if (level == ex->leve    1483                         if (level == ex->level)
1495                                 res = sas_ex_    1484                                 res = sas_ex_discover_devices(dev, -1);
1496                         else if (level > 0)      1485                         else if (level > 0)
1497                                 res = sas_ex_    1486                                 res = sas_ex_discover_devices(port->port_dev, -1);
1498                                                  1487 
1499                 }                                1488                 }
1500         }                                        1489         }
1501                                                  1490 
1502         return res;                              1491         return res;
1503 }                                                1492 }
1504                                                  1493 
1505 static int sas_ex_bfs_disc(struct asd_sas_por    1494 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1506 {                                                1495 {
1507         int res;                                 1496         int res;
1508         int level;                               1497         int level;
1509                                                  1498 
1510         do {                                     1499         do {
1511                 level = port->disc.max_level;    1500                 level = port->disc.max_level;
1512                 res = sas_ex_level_discovery(    1501                 res = sas_ex_level_discovery(port, level);
1513                 mb();                            1502                 mb();
1514         } while (level < port->disc.max_level    1503         } while (level < port->disc.max_level);
1515                                                  1504 
1516         return res;                              1505         return res;
1517 }                                                1506 }
1518                                                  1507 
1519 int sas_discover_root_expander(struct domain_    1508 int sas_discover_root_expander(struct domain_device *dev)
1520 {                                                1509 {
1521         int res;                                 1510         int res;
1522         struct sas_expander_device *ex = rphy    1511         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1523                                                  1512 
1524         res = sas_rphy_add(dev->rphy);           1513         res = sas_rphy_add(dev->rphy);
1525         if (res)                                 1514         if (res)
1526                 goto out_err;                    1515                 goto out_err;
1527                                                  1516 
1528         ex->level = dev->port->disc.max_level    1517         ex->level = dev->port->disc.max_level; /* 0 */
1529         res = sas_discover_expander(dev);        1518         res = sas_discover_expander(dev);
1530         if (res)                                 1519         if (res)
1531                 goto out_err2;                   1520                 goto out_err2;
1532                                                  1521 
1533         sas_ex_bfs_disc(dev->port);              1522         sas_ex_bfs_disc(dev->port);
1534                                                  1523 
1535         return res;                              1524         return res;
1536                                                  1525 
1537 out_err2:                                        1526 out_err2:
1538         sas_rphy_remove(dev->rphy);              1527         sas_rphy_remove(dev->rphy);
1539 out_err:                                         1528 out_err:
1540         return res;                              1529         return res;
1541 }                                                1530 }
1542                                                  1531 
1543 /* ---------- Domain revalidation ----------     1532 /* ---------- Domain revalidation ---------- */
1544                                                  1533 
1545 static int sas_get_phy_discover(struct domain    1534 static int sas_get_phy_discover(struct domain_device *dev,
1546                                 int phy_id, s    1535                                 int phy_id, struct smp_resp *disc_resp)
1547 {                                                1536 {
1548         int res;                                 1537         int res;
1549         u8 *disc_req;                            1538         u8 *disc_req;
1550                                                  1539 
1551         disc_req = alloc_smp_req(DISCOVER_REQ    1540         disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1552         if (!disc_req)                           1541         if (!disc_req)
1553                 return -ENOMEM;                  1542                 return -ENOMEM;
1554                                                  1543 
1555         disc_req[1] = SMP_DISCOVER;              1544         disc_req[1] = SMP_DISCOVER;
1556         disc_req[9] = phy_id;                    1545         disc_req[9] = phy_id;
1557                                                  1546 
1558         res = smp_execute_task(dev, disc_req,    1547         res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1559                                disc_resp, DIS    1548                                disc_resp, DISCOVER_RESP_SIZE);
1560         if (res)                                 1549         if (res)
1561                 goto out;                        1550                 goto out;
1562         else if (disc_resp->result != SMP_RES    1551         else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1563                 res = disc_resp->result;         1552                 res = disc_resp->result;
1564                 goto out;                        1553                 goto out;
1565         }                                        1554         }
1566 out:                                             1555 out:
1567         kfree(disc_req);                         1556         kfree(disc_req);
1568         return res;                              1557         return res;
1569 }                                                1558 }
1570                                                  1559 
1571 static int sas_get_phy_change_count(struct do    1560 static int sas_get_phy_change_count(struct domain_device *dev,
1572                                     int phy_i    1561                                     int phy_id, int *pcc)
1573 {                                                1562 {
1574         int res;                                 1563         int res;
1575         struct smp_resp *disc_resp;              1564         struct smp_resp *disc_resp;
1576                                                  1565 
1577         disc_resp = alloc_smp_resp(DISCOVER_R    1566         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1578         if (!disc_resp)                          1567         if (!disc_resp)
1579                 return -ENOMEM;                  1568                 return -ENOMEM;
1580                                                  1569 
1581         res = sas_get_phy_discover(dev, phy_i    1570         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1582         if (!res)                                1571         if (!res)
1583                 *pcc = disc_resp->disc.change    1572                 *pcc = disc_resp->disc.change_count;
1584                                                  1573 
1585         kfree(disc_resp);                        1574         kfree(disc_resp);
1586         return res;                              1575         return res;
1587 }                                                1576 }
1588                                                  1577 
1589 static int sas_get_phy_attached_sas_addr(stru    1578 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1590                                          int     1579                                          int phy_id, u8 *attached_sas_addr)
1591 {                                                1580 {
1592         int res;                                 1581         int res;
1593         struct smp_resp *disc_resp;              1582         struct smp_resp *disc_resp;
1594         struct discover_resp *dr;                1583         struct discover_resp *dr;
1595                                                  1584 
1596         disc_resp = alloc_smp_resp(DISCOVER_R    1585         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1597         if (!disc_resp)                          1586         if (!disc_resp)
1598                 return -ENOMEM;                  1587                 return -ENOMEM;
1599         dr = &disc_resp->disc;                   1588         dr = &disc_resp->disc;
1600                                                  1589 
1601         res = sas_get_phy_discover(dev, phy_i    1590         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1602         if (!res) {                              1591         if (!res) {
1603                 memcpy(attached_sas_addr,disc    1592                 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1604                 if (dr->attached_dev_type ==     1593                 if (dr->attached_dev_type == 0)
1605                         memset(attached_sas_a    1594                         memset(attached_sas_addr, 0, 8);
1606         }                                        1595         }
1607         kfree(disc_resp);                        1596         kfree(disc_resp);
1608         return res;                              1597         return res;
1609 }                                                1598 }
1610                                                  1599 
1611 static int sas_find_bcast_phy(struct domain_d    1600 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1612                               int from_phy, b !! 1601                               int from_phy)
1613 {                                                1602 {
1614         struct expander_device *ex = &dev->ex    1603         struct expander_device *ex = &dev->ex_dev;
1615         int res = 0;                             1604         int res = 0;
1616         int i;                                   1605         int i;
1617                                                  1606 
1618         for (i = from_phy; i < ex->num_phys;     1607         for (i = from_phy; i < ex->num_phys; i++) {
1619                 int phy_change_count = 0;        1608                 int phy_change_count = 0;
1620                                                  1609 
1621                 res = sas_get_phy_change_coun    1610                 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1622                 if (res)                         1611                 if (res)
1623                         goto out;                1612                         goto out;
1624                 else if (phy_change_count !=     1613                 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1625                         if (update)           !! 1614                         ex->ex_phy[i].phy_change_count = phy_change_count;
1626                                 ex->ex_phy[i] << 
1627                                         phy_c << 
1628                         *phy_id = i;             1615                         *phy_id = i;
1629                         return 0;                1616                         return 0;
1630                 }                                1617                 }
1631         }                                        1618         }
1632 out:                                             1619 out:
1633         return res;                              1620         return res;
1634 }                                                1621 }
1635                                                  1622 
1636 static int sas_get_ex_change_count(struct dom    1623 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1637 {                                                1624 {
1638         int res;                                 1625         int res;
1639         u8  *rg_req;                             1626         u8  *rg_req;
1640         struct smp_resp  *rg_resp;               1627         struct smp_resp  *rg_resp;
1641                                                  1628 
1642         rg_req = alloc_smp_req(RG_REQ_SIZE);     1629         rg_req = alloc_smp_req(RG_REQ_SIZE);
1643         if (!rg_req)                             1630         if (!rg_req)
1644                 return -ENOMEM;                  1631                 return -ENOMEM;
1645                                                  1632 
1646         rg_resp = alloc_smp_resp(RG_RESP_SIZE    1633         rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1647         if (!rg_resp) {                          1634         if (!rg_resp) {
1648                 kfree(rg_req);                   1635                 kfree(rg_req);
1649                 return -ENOMEM;                  1636                 return -ENOMEM;
1650         }                                        1637         }
1651                                                  1638 
1652         rg_req[1] = SMP_REPORT_GENERAL;          1639         rg_req[1] = SMP_REPORT_GENERAL;
1653                                                  1640 
1654         res = smp_execute_task(dev, rg_req, R    1641         res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1655                                RG_RESP_SIZE);    1642                                RG_RESP_SIZE);
1656         if (res)                                 1643         if (res)
1657                 goto out;                        1644                 goto out;
1658         if (rg_resp->result != SMP_RESP_FUNC_    1645         if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1659                 res = rg_resp->result;           1646                 res = rg_resp->result;
1660                 goto out;                        1647                 goto out;
1661         }                                        1648         }
1662                                                  1649 
1663         *ecc = be16_to_cpu(rg_resp->rg.change    1650         *ecc = be16_to_cpu(rg_resp->rg.change_count);
1664 out:                                             1651 out:
1665         kfree(rg_resp);                          1652         kfree(rg_resp);
1666         kfree(rg_req);                           1653         kfree(rg_req);
1667         return res;                              1654         return res;
1668 }                                                1655 }
1669 /**                                           << 
1670  * sas_find_bcast_dev -  find the device issu << 
1671  * @dev:domain device to be detect.           << 
1672  * @src_dev: the device which originated BROA << 
1673  *                                            << 
1674  * Add self-configuration expander suport. Su << 
1675  * when the first level expander is self-conf << 
1676  * second level expander, BROADCAST(CHANGE) w << 
1677  * in the second level expander, but also be  << 
1678  * expander (see SAS protocol SAS 2r-14, 7.11 << 
1679  * expander changed count in two level expand << 
1680  * once, but the phy which chang count has ch << 
1681  * we concerned.                              << 
1682  */                                           << 
1683                                                  1656 
1684 static int sas_find_bcast_dev(struct domain_d    1657 static int sas_find_bcast_dev(struct domain_device *dev,
1685                               struct domain_d    1658                               struct domain_device **src_dev)
1686 {                                                1659 {
1687         struct expander_device *ex = &dev->ex    1660         struct expander_device *ex = &dev->ex_dev;
1688         int ex_change_count = -1;                1661         int ex_change_count = -1;
1689         int phy_id = -1;                      << 
1690         int res;                                 1662         int res;
1691         struct domain_device *ch;             << 
1692                                                  1663 
1693         res = sas_get_ex_change_count(dev, &e    1664         res = sas_get_ex_change_count(dev, &ex_change_count);
1694         if (res)                                 1665         if (res)
1695                 goto out;                        1666                 goto out;
1696         if (ex_change_count != -1 && ex_chang !! 1667         if (ex_change_count != -1 &&
1697                 /* Just detect if this expand !! 1668             ex_change_count != ex->ex_change_count) {
1698                 * in order to determine if th !! 1669                 *src_dev = dev;
1699                 * and do not update phy chang !! 1670                 ex->ex_change_count = ex_change_count;
1700                 */                            !! 1671         } else {
1701                 res = sas_find_bcast_phy(dev, !! 1672                 struct domain_device *ch;
1702                 if (phy_id != -1) {           !! 1673 
1703                         *src_dev = dev;       !! 1674                 list_for_each_entry(ch, &ex->children, siblings) {
1704                         ex->ex_change_count = !! 1675                         if (ch->dev_type == EDGE_DEV ||
1705                         SAS_DPRINTK("Expander !! 1676                             ch->dev_type == FANOUT_DEV) {
1706                         return res;           !! 1677                                 res = sas_find_bcast_dev(ch, src_dev);
1707                 } else                        !! 1678                                 if (src_dev)
1708                         SAS_DPRINTK("Expander !! 1679                                         return res;
1709         }                                     !! 1680                         }
1710         list_for_each_entry(ch, &ex->children << 
1711                 if (ch->dev_type == EDGE_DEV  << 
1712                         res = sas_find_bcast_ << 
1713                         if (src_dev)          << 
1714                                 return res;   << 
1715                 }                                1681                 }
1716         }                                        1682         }
1717 out:                                             1683 out:
1718         return res;                              1684         return res;
1719 }                                                1685 }
1720                                                  1686 
1721 static void sas_unregister_ex_tree(struct dom    1687 static void sas_unregister_ex_tree(struct domain_device *dev)
1722 {                                                1688 {
1723         struct expander_device *ex = &dev->ex    1689         struct expander_device *ex = &dev->ex_dev;
1724         struct domain_device *child, *n;         1690         struct domain_device *child, *n;
1725                                                  1691 
1726         list_for_each_entry_safe(child, n, &e    1692         list_for_each_entry_safe(child, n, &ex->children, siblings) {
1727                 if (child->dev_type == EDGE_D    1693                 if (child->dev_type == EDGE_DEV ||
1728                     child->dev_type == FANOUT    1694                     child->dev_type == FANOUT_DEV)
1729                         sas_unregister_ex_tre    1695                         sas_unregister_ex_tree(child);
1730                 else                             1696                 else
1731                         sas_unregister_dev(ch    1697                         sas_unregister_dev(child);
1732         }                                        1698         }
1733         sas_unregister_dev(dev);                 1699         sas_unregister_dev(dev);
1734 }                                                1700 }
1735                                                  1701 
1736 static void sas_unregister_devs_sas_addr(stru    1702 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1737                                          int  !! 1703                                          int phy_id)
1738 {                                                1704 {
1739         struct expander_device *ex_dev = &par    1705         struct expander_device *ex_dev = &parent->ex_dev;
1740         struct ex_phy *phy = &ex_dev->ex_phy[    1706         struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1741         struct domain_device *child, *n;         1707         struct domain_device *child, *n;
1742         if (last) {                           !! 1708 
1743                 list_for_each_entry_safe(chil !! 1709         list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1744                         &ex_dev->children, si !! 1710                 if (SAS_ADDR(child->sas_addr) ==
1745                         if (SAS_ADDR(child->s !! 1711                     SAS_ADDR(phy->attached_sas_addr)) {
1746                             SAS_ADDR(phy->att !! 1712                         if (child->dev_type == EDGE_DEV ||
1747                                 if (child->de !! 1713                             child->dev_type == FANOUT_DEV)
1748                                     child->de !! 1714                                 sas_unregister_ex_tree(child);
1749                                         sas_u !! 1715                         else
1750                                 else          !! 1716                                 sas_unregister_dev(child);
1751                                         sas_u !! 1717                         break;
1752                                 break;        << 
1753                         }                     << 
1754                 }                                1718                 }
1755                 sas_disable_routing(parent, p << 
1756         }                                        1719         }
                                                   >> 1720         sas_disable_routing(parent, phy->attached_sas_addr);
1757         memset(phy->attached_sas_addr, 0, SAS    1721         memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1758         sas_port_delete_phy(phy->port, phy->p    1722         sas_port_delete_phy(phy->port, phy->phy);
1759         if (phy->port->num_phys == 0)            1723         if (phy->port->num_phys == 0)
1760                 sas_port_delete(phy->port);      1724                 sas_port_delete(phy->port);
1761         phy->port = NULL;                        1725         phy->port = NULL;
1762 }                                                1726 }
1763                                                  1727 
1764 static int sas_discover_bfs_by_root_level(str    1728 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1765                                           con    1729                                           const int level)
1766 {                                                1730 {
1767         struct expander_device *ex_root = &ro    1731         struct expander_device *ex_root = &root->ex_dev;
1768         struct domain_device *child;             1732         struct domain_device *child;
1769         int res = 0;                             1733         int res = 0;
1770                                                  1734 
1771         list_for_each_entry(child, &ex_root->    1735         list_for_each_entry(child, &ex_root->children, siblings) {
1772                 if (child->dev_type == EDGE_D    1736                 if (child->dev_type == EDGE_DEV ||
1773                     child->dev_type == FANOUT    1737                     child->dev_type == FANOUT_DEV) {
1774                         struct sas_expander_d    1738                         struct sas_expander_device *ex =
1775                                 rphy_to_expan    1739                                 rphy_to_expander_device(child->rphy);
1776                                                  1740 
1777                         if (level > ex->level    1741                         if (level > ex->level)
1778                                 res = sas_dis    1742                                 res = sas_discover_bfs_by_root_level(child,
1779                                                  1743                                                                      level);
1780                         else if (level == ex-    1744                         else if (level == ex->level)
1781                                 res = sas_ex_    1745                                 res = sas_ex_discover_devices(child, -1);
1782                 }                                1746                 }
1783         }                                        1747         }
1784         return res;                              1748         return res;
1785 }                                                1749 }
1786                                                  1750 
1787 static int sas_discover_bfs_by_root(struct do    1751 static int sas_discover_bfs_by_root(struct domain_device *dev)
1788 {                                                1752 {
1789         int res;                                 1753         int res;
1790         struct sas_expander_device *ex = rphy    1754         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1791         int level = ex->level+1;                 1755         int level = ex->level+1;
1792                                                  1756 
1793         res = sas_ex_discover_devices(dev, -1    1757         res = sas_ex_discover_devices(dev, -1);
1794         if (res)                                 1758         if (res)
1795                 goto out;                        1759                 goto out;
1796         do {                                     1760         do {
1797                 res = sas_discover_bfs_by_roo    1761                 res = sas_discover_bfs_by_root_level(dev, level);
1798                 mb();                            1762                 mb();
1799                 level += 1;                      1763                 level += 1;
1800         } while (level <= dev->port->disc.max    1764         } while (level <= dev->port->disc.max_level);
1801 out:                                             1765 out:
1802         return res;                              1766         return res;
1803 }                                                1767 }
1804                                                  1768 
1805 static int sas_discover_new(struct domain_dev    1769 static int sas_discover_new(struct domain_device *dev, int phy_id)
1806 {                                                1770 {
1807         struct ex_phy *ex_phy = &dev->ex_dev.    1771         struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1808         struct domain_device *child;             1772         struct domain_device *child;
1809         bool found = false;                   !! 1773         int res;
1810         int res, i;                           << 
1811                                                  1774 
1812         SAS_DPRINTK("ex %016llx phy%d new dev    1775         SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1813                     SAS_ADDR(dev->sas_addr),     1776                     SAS_ADDR(dev->sas_addr), phy_id);
1814         res = sas_ex_phy_discover(dev, phy_id    1777         res = sas_ex_phy_discover(dev, phy_id);
1815         if (res)                                 1778         if (res)
1816                 goto out;                        1779                 goto out;
1817         /* to support the wide port inserted  << 
1818         for (i = 0; i < dev->ex_dev.num_phys; << 
1819                 struct ex_phy *ex_phy_temp =  << 
1820                 if (i == phy_id)              << 
1821                         continue;             << 
1822                 if (SAS_ADDR(ex_phy_temp->att << 
1823                     SAS_ADDR(ex_phy->attached << 
1824                         found = true;         << 
1825                         break;                << 
1826                 }                             << 
1827         }                                     << 
1828         if (found) {                          << 
1829                 sas_ex_join_wide_port(dev, ph << 
1830                 return 0;                     << 
1831         }                                     << 
1832         res = sas_ex_discover_devices(dev, ph    1780         res = sas_ex_discover_devices(dev, phy_id);
1833         if (!res)                             !! 1781         if (res)
1834                 goto out;                        1782                 goto out;
1835         list_for_each_entry(child, &dev->ex_d    1783         list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1836                 if (SAS_ADDR(child->sas_addr)    1784                 if (SAS_ADDR(child->sas_addr) ==
1837                     SAS_ADDR(ex_phy->attached    1785                     SAS_ADDR(ex_phy->attached_sas_addr)) {
1838                         if (child->dev_type =    1786                         if (child->dev_type == EDGE_DEV ||
1839                             child->dev_type =    1787                             child->dev_type == FANOUT_DEV)
1840                                 res = sas_dis    1788                                 res = sas_discover_bfs_by_root(child);
1841                         break;                   1789                         break;
1842                 }                                1790                 }
1843         }                                        1791         }
1844 out:                                             1792 out:
1845         return res;                              1793         return res;
1846 }                                                1794 }
1847                                                  1795 
1848 static int sas_rediscover_dev(struct domain_d !! 1796 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1849 {                                                1797 {
1850         struct expander_device *ex = &dev->ex    1798         struct expander_device *ex = &dev->ex_dev;
1851         struct ex_phy *phy = &ex->ex_phy[phy_    1799         struct ex_phy *phy = &ex->ex_phy[phy_id];
1852         u8 attached_sas_addr[8];                 1800         u8 attached_sas_addr[8];
1853         int res;                                 1801         int res;
1854                                                  1802 
1855         res = sas_get_phy_attached_sas_addr(d    1803         res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1856         switch (res) {                           1804         switch (res) {
1857         case SMP_RESP_NO_PHY:                    1805         case SMP_RESP_NO_PHY:
1858                 phy->phy_state = PHY_NOT_PRES    1806                 phy->phy_state = PHY_NOT_PRESENT;
1859                 sas_unregister_devs_sas_addr( !! 1807                 sas_unregister_devs_sas_addr(dev, phy_id);
1860                 goto out; break;                 1808                 goto out; break;
1861         case SMP_RESP_PHY_VACANT:                1809         case SMP_RESP_PHY_VACANT:
1862                 phy->phy_state = PHY_VACANT;     1810                 phy->phy_state = PHY_VACANT;
1863                 sas_unregister_devs_sas_addr( !! 1811                 sas_unregister_devs_sas_addr(dev, phy_id);
1864                 goto out; break;                 1812                 goto out; break;
1865         case SMP_RESP_FUNC_ACC:                  1813         case SMP_RESP_FUNC_ACC:
1866                 break;                           1814                 break;
1867         }                                        1815         }
1868                                                  1816 
1869         if (SAS_ADDR(attached_sas_addr) == 0)    1817         if (SAS_ADDR(attached_sas_addr) == 0) {
1870                 phy->phy_state = PHY_EMPTY;      1818                 phy->phy_state = PHY_EMPTY;
1871                 sas_unregister_devs_sas_addr( !! 1819                 sas_unregister_devs_sas_addr(dev, phy_id);
1872         } else if (SAS_ADDR(attached_sas_addr    1820         } else if (SAS_ADDR(attached_sas_addr) ==
1873                    SAS_ADDR(phy->attached_sas    1821                    SAS_ADDR(phy->attached_sas_addr)) {
1874                 SAS_DPRINTK("ex %016llx phy 0    1822                 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1875                             SAS_ADDR(dev->sas    1823                             SAS_ADDR(dev->sas_addr), phy_id);
1876                 sas_ex_phy_discover(dev, phy_    1824                 sas_ex_phy_discover(dev, phy_id);
1877         } else                                   1825         } else
1878                 res = sas_discover_new(dev, p    1826                 res = sas_discover_new(dev, phy_id);
1879 out:                                             1827 out:
1880         return res;                              1828         return res;
1881 }                                                1829 }
1882                                                  1830 
1883 /**                                           << 
1884  * sas_rediscover - revalidate the domain.    << 
1885  * @dev:domain device to be detect.           << 
1886  * @phy_id: the phy id will be detected.      << 
1887  *                                            << 
1888  * NOTE: this process _must_ quit (return) as << 
1889  * errors are encountered.  Connection recove << 
1890  * Discover process only interrogates devices << 
1891  * domain.For plugging out, we un-register th << 
1892  * the last phy in the port, for other phys i << 
1893  * from the port.For inserting, we do discove << 
1894  * first phy,for other phys in this port, we  << 
1895  * forming the wide-port.                     << 
1896  */                                           << 
1897 static int sas_rediscover(struct domain_devic    1831 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1898 {                                                1832 {
1899         struct expander_device *ex = &dev->ex    1833         struct expander_device *ex = &dev->ex_dev;
1900         struct ex_phy *changed_phy = &ex->ex_    1834         struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1901         int res = 0;                             1835         int res = 0;
1902         int i;                                   1836         int i;
1903         bool last = true;       /* is this th << 
1904                                                  1837 
1905         SAS_DPRINTK("ex %016llx phy%d origina    1838         SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1906                     SAS_ADDR(dev->sas_addr),     1839                     SAS_ADDR(dev->sas_addr), phy_id);
1907                                                  1840 
1908         if (SAS_ADDR(changed_phy->attached_sa    1841         if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1909                 for (i = 0; i < ex->num_phys;    1842                 for (i = 0; i < ex->num_phys; i++) {
1910                         struct ex_phy *phy =     1843                         struct ex_phy *phy = &ex->ex_phy[i];
1911                                                  1844 
1912                         if (i == phy_id)         1845                         if (i == phy_id)
1913                                 continue;        1846                                 continue;
1914                         if (SAS_ADDR(phy->att    1847                         if (SAS_ADDR(phy->attached_sas_addr) ==
1915                             SAS_ADDR(changed_    1848                             SAS_ADDR(changed_phy->attached_sas_addr)) {
1916                                 SAS_DPRINTK("    1849                                 SAS_DPRINTK("phy%d part of wide port with "
1917                                             "    1850                                             "phy%d\n", phy_id, i);
1918                                 last = false; !! 1851                                 goto out;
1919                                 break;        << 
1920                         }                        1852                         }
1921                 }                                1853                 }
1922                 res = sas_rediscover_dev(dev, !! 1854                 res = sas_rediscover_dev(dev, phy_id);
1923         } else                                   1855         } else
1924                 res = sas_discover_new(dev, p    1856                 res = sas_discover_new(dev, phy_id);
                                                   >> 1857 out:
1925         return res;                              1858         return res;
1926 }                                                1859 }
1927                                                  1860 
1928 /**                                              1861 /**
1929  * sas_revalidate_domain -- revalidate the do    1862  * sas_revalidate_domain -- revalidate the domain
1930  * @port: port to the domain of interest         1863  * @port: port to the domain of interest
1931  *                                               1864  *
1932  * NOTE: this process _must_ quit (return) as    1865  * NOTE: this process _must_ quit (return) as soon as any connection
1933  * errors are encountered.  Connection recove    1866  * errors are encountered.  Connection recovery is done elsewhere.
1934  * Discover process only interrogates devices    1867  * Discover process only interrogates devices in order to discover the
1935  * domain.                                       1868  * domain.
1936  */                                              1869  */
1937 int sas_ex_revalidate_domain(struct domain_de    1870 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1938 {                                                1871 {
1939         int res;                                 1872         int res;
1940         struct domain_device *dev = NULL;        1873         struct domain_device *dev = NULL;
1941                                                  1874 
1942         res = sas_find_bcast_dev(port_dev, &d    1875         res = sas_find_bcast_dev(port_dev, &dev);
1943         if (res)                                 1876         if (res)
1944                 goto out;                        1877                 goto out;
1945         if (dev) {                               1878         if (dev) {
1946                 struct expander_device *ex =     1879                 struct expander_device *ex = &dev->ex_dev;
1947                 int i = 0, phy_id;               1880                 int i = 0, phy_id;
1948                                                  1881 
1949                 do {                             1882                 do {
1950                         phy_id = -1;             1883                         phy_id = -1;
1951                         res = sas_find_bcast_ !! 1884                         res = sas_find_bcast_phy(dev, &phy_id, i);
1952                         if (phy_id == -1)        1885                         if (phy_id == -1)
1953                                 break;           1886                                 break;
1954                         res = sas_rediscover(    1887                         res = sas_rediscover(dev, phy_id);
1955                         i = phy_id + 1;          1888                         i = phy_id + 1;
1956                 } while (i < ex->num_phys);      1889                 } while (i < ex->num_phys);
1957         }                                        1890         }
1958 out:                                             1891 out:
1959         return res;                              1892         return res;
1960 }                                                1893 }
1961                                                  1894 
1962 int sas_smp_handler(struct Scsi_Host *shost,     1895 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1963                     struct request *req)         1896                     struct request *req)
1964 {                                                1897 {
1965         struct domain_device *dev;               1898         struct domain_device *dev;
1966         int ret, type;                           1899         int ret, type;
1967         struct request *rsp = req->next_rq;      1900         struct request *rsp = req->next_rq;
1968                                                  1901 
1969         if (!rsp) {                              1902         if (!rsp) {
1970                 printk("%s: space for a smp r    1903                 printk("%s: space for a smp response is missing\n",
1971                        __func__);             !! 1904                        __FUNCTION__);
1972                 return -EINVAL;                  1905                 return -EINVAL;
1973         }                                        1906         }
1974                                                  1907 
1975         /* no rphy means no smp target suppor    1908         /* no rphy means no smp target support (ie aic94xx host) */
1976         if (!rphy)                               1909         if (!rphy)
1977                 return sas_smp_host_handler(s    1910                 return sas_smp_host_handler(shost, req, rsp);
1978                                                  1911 
1979         type = rphy->identify.device_type;       1912         type = rphy->identify.device_type;
1980                                                  1913 
1981         if (type != SAS_EDGE_EXPANDER_DEVICE     1914         if (type != SAS_EDGE_EXPANDER_DEVICE &&
1982             type != SAS_FANOUT_EXPANDER_DEVIC    1915             type != SAS_FANOUT_EXPANDER_DEVICE) {
1983                 printk("%s: can we send a smp    1916                 printk("%s: can we send a smp request to a device?\n",
1984                        __func__);             !! 1917                        __FUNCTION__);
1985                 return -EINVAL;                  1918                 return -EINVAL;
1986         }                                        1919         }
1987                                                  1920 
1988         dev = sas_find_dev_by_rphy(rphy);        1921         dev = sas_find_dev_by_rphy(rphy);
1989         if (!dev) {                              1922         if (!dev) {
1990                 printk("%s: fail to find a do !! 1923                 printk("%s: fail to find a domain_device?\n", __FUNCTION__);
1991                 return -EINVAL;                  1924                 return -EINVAL;
1992         }                                        1925         }
1993                                                  1926 
1994         /* do we need to support multiple seg    1927         /* do we need to support multiple segments? */
1995         if (req->bio->bi_vcnt > 1 || rsp->bio    1928         if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1996                 printk("%s: multiple segments    1929                 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1997                        __func__, req->bio->bi !! 1930                        __FUNCTION__, req->bio->bi_vcnt, req->data_len,
1998                        rsp->bio->bi_vcnt, blk !! 1931                        rsp->bio->bi_vcnt, rsp->data_len);
1999                 return -EINVAL;                  1932                 return -EINVAL;
2000         }                                        1933         }
2001                                                  1934 
2002         ret = smp_execute_task(dev, bio_data( !! 1935         ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
2003                                bio_data(rsp-> !! 1936                                bio_data(rsp->bio), rsp->data_len);
2004         if (ret > 0) {                           1937         if (ret > 0) {
2005                 /* positive number is the unt    1938                 /* positive number is the untransferred residual */
2006                 rsp->resid_len = ret;         !! 1939                 rsp->data_len = ret;
2007                 req->resid_len = 0;           !! 1940                 req->data_len = 0;
2008                 ret = 0;                         1941                 ret = 0;
2009         } else if (ret == 0) {                   1942         } else if (ret == 0) {
2010                 rsp->resid_len = 0;           !! 1943                 rsp->data_len = 0;
2011                 req->resid_len = 0;           !! 1944                 req->data_len = 0;
2012         }                                        1945         }
2013                                                  1946 
2014         return ret;                              1947         return ret;
2015 }                                                1948 }
2016                                                  1949 
  This page was automatically generated by the LXR engine.